1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2013 Matthieu Lucas <lucasmatthieu@gmail.com>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Data transfer with URL syntax
19 # Download or upload over HTTP with `CurlHTTPRequest` and send emails with `CurlMail`.
25 # Shared Curl library handle
27 # Usually, you do not have to use this attribute, it instancied by `CurlHTTPRequest` and `CurlMail`.
28 # But in some cases you may want to finalize it to free some small resources.
29 # However, if Curl services are needed once again, this attribute must be manually set.
30 var curl
: Curl = new Curl is lazy
, writable
33 # Curl library handle, it is initialized and released with this class
37 private var native
= new NativeCurl.easy_init
39 # Check for correct initialization
40 fun is_ok
: Bool do return self.native
.is_init
42 redef fun finalize_once
do if is_ok
then native
.easy_clean
48 private var curl
: Curl = sys
.curl
50 # Shall this request be verbose?
51 var verbose
: Bool = false is writable
53 # Intern perform method, lowest level of request launching
54 private fun perform
: nullable CurlResponseFailed
56 if not self.curl
.is_ok
then return answer_failure
(0, "Curl instance is not correctly initialized")
60 err
= self.curl
.native
.easy_setopt
(new CURLOption.verbose
, self.verbose
)
61 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
63 err
= self.curl
.native
.easy_perform
64 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
69 # Intern method with return a failed answer with given code and message
70 private fun answer_failure
(error_code
: Int, error_msg
: String): CurlResponseFailed
72 return new CurlResponseFailed(error_code
, error_msg
)
79 super NativeCurlCallbacks
82 var datas
: nullable HeaderMap is writable
83 var headers
: nullable HeaderMap is writable
84 var delegate
: nullable CurlCallbacks is writable
86 # Set the user agent for all following HTTP requests
87 var user_agent
: nullable String is writable
89 # Execute HTTP request with settings configured through attribute
90 fun execute
: CurlResponse
92 if not self.curl
.is_ok
then return answer_failure
(0, "Curl instance is not correctly initialized")
94 var success_response
= new CurlResponseSuccess
95 var callback_receiver
: CurlCallbacks = success_response
96 if self.delegate
!= null then callback_receiver
= self.delegate
.as(not null)
100 err
= self.curl
.native
.easy_setopt
(new CURLOption.follow_location
, 1)
101 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
103 err
= self.curl
.native
.easy_setopt
(new CURLOption.url
, url
)
104 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
106 var user_agent
= user_agent
107 if user_agent
!= null then
108 err
= curl
.native
.easy_setopt
(new CURLOption.user_agent
, user_agent
)
109 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
113 err
= self.curl
.native
.register_callback_header
(callback_receiver
)
114 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
116 err
= self.curl
.native
.register_callback_body
(callback_receiver
)
117 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
120 var headers
= self.headers
121 if headers
!= null then
122 var headers_joined
= headers
.join_pairs
(": ")
123 err
= self.curl
.native
.easy_setopt
(new CURLOption.httpheader
, headers_joined
.to_curlslist
)
124 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
128 var datas
= self.datas
129 if datas
!= null then
130 var postdatas
= datas
.to_url_encoded
(self.curl
)
131 err
= self.curl
.native
.easy_setopt
(new CURLOption.postfields
, postdatas
)
132 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
135 var err_resp
= perform
136 if err_resp
!= null then return err_resp
138 var st_code
= self.curl
.native
.easy_getinfo_long
(new CURLInfoLong.response_code
)
139 if not st_code
== null then success_response
.status_code
= st_code
141 return success_response
144 # Download to file given resource
145 fun download_to_file
(output_file_name
: nullable String): CurlResponse
147 var success_response
= new CurlFileResponseSuccess
149 var callback_receiver
: CurlCallbacks = success_response
150 if self.delegate
!= null then callback_receiver
= self.delegate
.as(not null)
154 err
= self.curl
.native
.easy_setopt
(new CURLOption.follow_location
, 1)
155 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
157 err
= self.curl
.native
.easy_setopt
(new CURLOption.url
, url
)
158 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
160 err
= self.curl
.native
.register_callback_header
(callback_receiver
)
161 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
163 err
= self.curl
.native
.register_callback_stream
(callback_receiver
)
164 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
167 if not output_file_name
== null then
168 opt_name
= output_file_name
169 else if not self.url
.substring
(self.url
.length-1
, self.url
.length
) == "/" then
170 opt_name
= self.url
.basename
172 return answer_failure
(0, "Unable to extract file name, please specify one")
175 success_response
.file
= new FileWriter.open
(opt_name
)
176 if not success_response
.file
.is_writable
then
177 return answer_failure
(0, "Unable to create associated file")
180 var err_resp
= perform
181 if err_resp
!= null then return err_resp
183 var st_code
= self.curl
.native
.easy_getinfo_long
(new CURLInfoLong.response_code
)
184 if not st_code
== null then success_response
.status_code
= st_code
186 var speed
= self.curl
.native
.easy_getinfo_double
(new CURLInfoDouble.speed_download
)
187 if not speed
== null then success_response
.speed_download
= speed
189 var size
= self.curl
.native
.easy_getinfo_double
(new CURLInfoDouble.size_download
)
190 if not size
== null then success_response
.size_download
= size
192 var time
= self.curl
.native
.easy_getinfo_double
(new CURLInfoDouble.total_time
)
193 if not time
== null then success_response
.total_time
= time
195 success_response
.file
.close
197 return success_response
205 # var mail = new CurlMail("sender@example.org",
206 # to=["to@example.org"], cc=["bob@example.org"])
208 # mail.headers_body["Content-Type:"] = """text/html; charset="UTF-8""""
209 # mail.headers_body["Content-Transfer-Encoding:"] = "quoted-printable"
211 # mail.body = "<h1>Here you can write HTML stuff.</h1>"
212 # mail.subject = "Hello From My Nit Program"
215 # var error = mail.set_outgoing_server("smtps://smtp.example.org:465",
216 # "user@example.org", "mypassword")
217 # if error != null then
218 # print "Mail Server Error: {error}"
223 # error = mail.execute
224 # if error != null then
225 # print "Transfer Error: {error}"
231 super NativeCurlCallbacks
233 # Address of the sender
234 var from
: nullable String is writable
237 var to
: nullable Array[String] is writable
240 var subject
: nullable String is writable
243 var body
: nullable String is writable
246 var cc
: nullable Array[String] is writable
248 # BCC recipients (hidden from other recipients)
249 var bcc
: nullable Array[String] is writable
252 var headers
= new HeaderMap is lazy
, writable
255 var headers_body
= new HeaderMap is lazy
, writable
257 # Protocols supported to send mail to a server
259 # Default value at `["smtp", "smtps"]`
260 var supported_outgoing_protocol
: Array[String] = ["smtp", "smtps"]
262 # Helper method to add pair values to mail content while building it (ex: "To:", "address@mail.com")
263 private fun add_pair_to_content
(str
: String, att
: String, val
: nullable String): String
265 if val
!= null then return "{str}{att}{val}\n"
266 return "{str}{att}\n"
269 # Helper method to add entire list of pairs to mail content
270 private fun add_pairs_to_content
(content
: String, pairs
: HeaderMap): String
272 for h_key
, h_val
in pairs
do content
= add_pair_to_content
(content
, h_key
, h_val
)
276 # Check for host and protocol availability
277 private fun is_supported_outgoing_protocol
(host
: String): CURLCode
279 var host_reach
= host
.split_with
("://")
280 if host_reach
.length
> 1 and supported_outgoing_protocol
.has
(host_reach
[0]) then return once
new CURLCode.ok
281 return once
new CURLCode.unsupported_protocol
284 # Configure server host and user credentials if needed.
285 fun set_outgoing_server
(host
: String, user
: nullable String, pwd
: nullable String): nullable CurlResponseFailed
287 # Check Curl initialisation
288 if not self.curl
.is_ok
then return answer_failure
(0, "Curl instance is not correctly initialized")
293 err
= is_supported_outgoing_protocol
(host
)
294 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
296 err
= self.curl
.native
.easy_setopt
(new CURLOption.url
, host
)
297 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
300 if not user
== null and not pwd
== null then
301 err
= self.curl
.native
.easy_setopt
(new CURLOption.username
, user
)
302 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
304 err
= self.curl
.native
.easy_setopt
(new CURLOption.password
, pwd
)
305 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
311 # Execute Mail request with settings configured through attribute
312 fun execute
: nullable CurlResponseFailed
314 if not self.curl
.is_ok
then return answer_failure
(0, "Curl instance is not correctly initialized")
316 var lines
= new Array[String]
319 var headers
= self.headers
320 if not headers
.is_empty
then
321 for k
, v
in headers
do lines
.add
"{k}{v}"
325 var all_recipients
= new Array[String]
327 if to
!= null and to
.length
> 0 then
328 lines
.add
"To:{to.join(",")}"
329 all_recipients
.append to
333 if cc
!= null and cc
.length
> 0 then
334 lines
.add
"Cc:{cc.join(",")}"
335 all_recipients
.append cc
339 if bcc
!= null and bcc
.length
> 0 then all_recipients
.append bcc
341 if all_recipients
.is_empty
then return answer_failure
(0, "There must be at lease one recipient")
343 var err
= self.curl
.native
.easy_setopt
(new CURLOption.follow_location
, 1)
344 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
346 err
= self.curl
.native
.easy_setopt
(new CURLOption.mail_rcpt
, all_recipients
.to_curlslist
)
347 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
351 if not from
== null then
352 lines
.add
"From:{from}"
354 err
= self.curl
.native
.easy_setopt
(new CURLOption.mail_from
, from
)
355 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
359 var subject
= self.subject
360 if subject
== null then subject
= "" # Default
361 lines
.add
"Subject: {subject}"
364 var headers_body
= self.headers_body
365 if not headers_body
.is_empty
then
366 for k
, v
in headers_body
do lines
.add
"{k}{v}"
371 if body
== null then body
= "" # Default
377 err
= self.curl
.native
.register_callback_read
(self)
378 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
380 var content
= lines
.join
("\n")
381 err
= self.curl
.native
.register_read_datas_callback
(self, content
)
382 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
384 var err_resp
= perform
385 if err_resp
!= null then return err_resp
391 # Callbacks Interface, allow you to manage in your way the different streams
392 interface CurlCallbacks
393 super NativeCurlCallbacks
396 # Abstract Curl request response
397 abstract class CurlResponse
400 # Failed Response Class returned when errors during configuration are raised
401 class CurlResponseFailed
405 var error_msg
: String
407 redef fun to_s
do return "{error_msg} ({error_code})"
410 # Success Abstract Response Success Class
411 abstract class CurlResponseSuccessIntern
415 var headers
= new HashMap[String, String]
417 # Receive headers from request due to headers callback registering
418 redef fun header_callback
(line
)
420 var splitted
= line
.split_with
(':')
421 if splitted
.length
> 1 then
422 var key
= splitted
.shift
423 self.headers
[key
] = splitted
.to_s
428 # Success Response Class of a basic response
429 class CurlResponseSuccess
430 super CurlResponseSuccessIntern
435 # Receive body from request due to body callback registering
436 redef fun body_callback
(line
) do
437 self.body_str
= "{self.body_str}{line}"
441 # Success Response Class of a downloaded File
442 class CurlFileResponseSuccess
443 super CurlResponseSuccessIntern
446 var speed_download
= 0.0
447 var size_download
= 0.0
449 private var file
: nullable FileWriter = null
451 # Receive bytes stream from request due to stream callback registering
452 redef fun stream_callback
(buffer
)
458 # Pseudo map associating `String` to `String` for HTTP exchanges
460 # This structure differs from `Map` as each key can have multiple associations
461 # and the order of insertion is important to some services.
463 private var array
= new Array[Couple[String, String]]
465 # Add a `value` associated to `key`
466 fun []=(key
, value
: String)
468 array
.add
new Couple[String, String](key
, value
)
471 # Get a list of the keys associated to `key`
472 fun [](k
: String): Array[String]
474 var res
= new Array[String]
475 for c
in array
do if c
.first
== k
then res
.add c
.second
479 # Iterate over all the associations in `self`
480 fun iterator
: MapIterator[String, String] do return new HeaderMapIterator(self)
482 # Get `self` as a single string for HTTP POST
484 # Require: `curl.is_ok`
485 fun to_url_encoded
(curl
: Curl): String
489 var lines
= new Array[String]
491 if k
.length
== 0 then continue
493 k
= curl
.native
.escape
(k
)
494 v
= curl
.native
.escape
(v
)
497 return lines
.join
("&")
500 # Concatenate couple of 'key value' separated by 'sep' in Array
501 fun join_pairs
(sep
: String): Array[String]
503 var col
= new Array[String]
504 for k
, v
in self do col
.add
("{k}{sep}{v}")
508 # Number of values in `self`
509 fun length
: Int do return array
.length
512 fun is_empty
: Bool do return array
.is_empty
515 private class HeaderMapIterator
516 super MapIterator[String, String]
519 var iterator
: Iterator[Couple[String, String]] = map
.array
.iterator
is lazy
521 redef fun is_ok
do return self.iterator
.is_ok
522 redef fun next
do self.iterator
.next
523 redef fun item
do return self.iterator
.item
.second
524 redef fun key
do return self.iterator
.item
.first