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
)
76 # HTTP request builder
78 # The request itself is sent by either `execute` or `download_to_file`.
79 # The attributes of this class must be set before calling either of these two methods.
81 # ## Minimal usage example
84 # var request = new CurlHTTPRequest("http://example.org/")
85 # var response = request.execute
86 # if response isa CurlResponseSuccess then
87 # print "Response status code: {response.status_code}"
88 # print response.body_str
89 # else if response isa CurlResponseFailed then
90 # print_error response.error_msg
95 super NativeCurlCallbacks
97 # Address of the remote resource to request
100 # Data for the body of a POST request
101 var data
: nullable HeaderMap is writable
103 # Header content of the request
104 var headers
: nullable HeaderMap is writable
106 # Delegates to customize the behavior when running `execute`
107 var delegate
: nullable CurlCallbacks is writable
109 # Set the user agent for all following HTTP requests
110 var user_agent
: nullable String is writable
112 # Execute HTTP request
114 # By default, the response body is returned in an instance of `CurlResponse`.
115 # This behavior can be customized by setting a custom `delegate`.
116 fun execute
: CurlResponse
118 if not self.curl
.is_ok
then return answer_failure
(0, "Curl instance is not correctly initialized")
120 var success_response
= new CurlResponseSuccess
121 var callback_receiver
: CurlCallbacks = success_response
122 if self.delegate
!= null then callback_receiver
= self.delegate
.as(not null)
126 err
= self.curl
.native
.easy_setopt
(new CURLOption.follow_location
, 1)
127 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
129 err
= self.curl
.native
.easy_setopt
(new CURLOption.url
, url
)
130 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
132 var user_agent
= user_agent
133 if user_agent
!= null then
134 err
= curl
.native
.easy_setopt
(new CURLOption.user_agent
, user_agent
)
135 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
139 err
= self.curl
.native
.register_callback_header
(callback_receiver
)
140 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
142 err
= self.curl
.native
.register_callback_body
(callback_receiver
)
143 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
146 var headers
= self.headers
147 if headers
!= null then
148 var headers_joined
= headers
.join_pairs
(": ")
149 err
= self.curl
.native
.easy_setopt
(new CURLOption.httpheader
, headers_joined
.to_curlslist
)
150 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
156 var postdatas
= data
.to_url_encoded
(self.curl
)
157 err
= self.curl
.native
.easy_setopt
(new CURLOption.postfields
, postdatas
)
158 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
161 var err_resp
= perform
162 if err_resp
!= null then return err_resp
164 var st_code
= self.curl
.native
.easy_getinfo_long
(new CURLInfoLong.response_code
)
165 if not st_code
== null then success_response
.status_code
= st_code
167 return success_response
170 # Download to file given resource
171 fun download_to_file
(output_file_name
: nullable String): CurlResponse
173 var success_response
= new CurlFileResponseSuccess
175 var callback_receiver
: CurlCallbacks = success_response
176 if self.delegate
!= null then callback_receiver
= self.delegate
.as(not null)
180 err
= self.curl
.native
.easy_setopt
(new CURLOption.follow_location
, 1)
181 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
183 err
= self.curl
.native
.easy_setopt
(new CURLOption.url
, url
)
184 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
186 err
= self.curl
.native
.register_callback_header
(callback_receiver
)
187 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
189 err
= self.curl
.native
.register_callback_stream
(callback_receiver
)
190 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
193 if not output_file_name
== null then
194 opt_name
= output_file_name
195 else if not self.url
.substring
(self.url
.length-1
, self.url
.length
) == "/" then
196 opt_name
= self.url
.basename
198 return answer_failure
(0, "Unable to extract file name, please specify one")
201 success_response
.file
= new FileWriter.open
(opt_name
)
202 if not success_response
.file
.is_writable
then
203 return answer_failure
(0, "Unable to create associated file")
206 var err_resp
= perform
207 if err_resp
!= null then return err_resp
209 var st_code
= self.curl
.native
.easy_getinfo_long
(new CURLInfoLong.response_code
)
210 if not st_code
== null then success_response
.status_code
= st_code
212 var speed
= self.curl
.native
.easy_getinfo_double
(new CURLInfoDouble.speed_download
)
213 if not speed
== null then success_response
.speed_download
= speed
215 var size
= self.curl
.native
.easy_getinfo_double
(new CURLInfoDouble.size_download
)
216 if not size
== null then success_response
.size_download
= size
218 var time
= self.curl
.native
.easy_getinfo_double
(new CURLInfoDouble.total_time
)
219 if not time
== null then success_response
.total_time
= time
221 success_response
.file
.close
223 return success_response
231 # var mail = new CurlMail("sender@example.org",
232 # to=["to@example.org"], cc=["bob@example.org"])
234 # mail.headers_body["Content-Type:"] = """text/html; charset="UTF-8""""
235 # mail.headers_body["Content-Transfer-Encoding:"] = "quoted-printable"
237 # mail.body = "<h1>Here you can write HTML stuff.</h1>"
238 # mail.subject = "Hello From My Nit Program"
241 # var error = mail.set_outgoing_server("smtps://smtp.example.org:465",
242 # "user@example.org", "mypassword")
243 # if error != null then
244 # print "Mail Server Error: {error}"
249 # error = mail.execute
250 # if error != null then
251 # print "Transfer Error: {error}"
257 super NativeCurlCallbacks
259 # Address of the sender
260 var from
: nullable String is writable
263 var to
: nullable Array[String] is writable
266 var subject
: nullable String is writable
269 var body
: nullable String is writable
272 var cc
: nullable Array[String] is writable
274 # BCC recipients (hidden from other recipients)
275 var bcc
: nullable Array[String] is writable
278 var headers
= new HeaderMap is lazy
, writable
281 var headers_body
= new HeaderMap is lazy
, writable
283 # Protocols supported to send mail to a server
285 # Default value at `["smtp", "smtps"]`
286 var supported_outgoing_protocol
: Array[String] = ["smtp", "smtps"]
288 # Helper method to add pair values to mail content while building it (ex: "To:", "address@mail.com")
289 private fun add_pair_to_content
(str
: String, att
: String, val
: nullable String): String
291 if val
!= null then return "{str}{att}{val}\n"
292 return "{str}{att}\n"
295 # Helper method to add entire list of pairs to mail content
296 private fun add_pairs_to_content
(content
: String, pairs
: HeaderMap): String
298 for h_key
, h_val
in pairs
do content
= add_pair_to_content
(content
, h_key
, h_val
)
302 # Check for host and protocol availability
303 private fun is_supported_outgoing_protocol
(host
: String): CURLCode
305 var host_reach
= host
.split_with
("://")
306 if host_reach
.length
> 1 and supported_outgoing_protocol
.has
(host_reach
[0]) then return once
new CURLCode.ok
307 return once
new CURLCode.unsupported_protocol
310 # Configure server host and user credentials if needed.
311 fun set_outgoing_server
(host
: String, user
: nullable String, pwd
: nullable String): nullable CurlResponseFailed
313 # Check Curl initialisation
314 if not self.curl
.is_ok
then return answer_failure
(0, "Curl instance is not correctly initialized")
319 err
= is_supported_outgoing_protocol
(host
)
320 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
322 err
= self.curl
.native
.easy_setopt
(new CURLOption.url
, host
)
323 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
326 if not user
== null and not pwd
== null then
327 err
= self.curl
.native
.easy_setopt
(new CURLOption.username
, user
)
328 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
330 err
= self.curl
.native
.easy_setopt
(new CURLOption.password
, pwd
)
331 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
337 # Execute Mail request with settings configured through attribute
338 fun execute
: nullable CurlResponseFailed
340 if not self.curl
.is_ok
then return answer_failure
(0, "Curl instance is not correctly initialized")
342 var lines
= new Array[String]
345 var headers
= self.headers
346 if not headers
.is_empty
then
347 for k
, v
in headers
do lines
.add
"{k}{v}"
351 var all_recipients
= new Array[String]
353 if to
!= null and to
.length
> 0 then
354 lines
.add
"To:{to.join(",")}"
355 all_recipients
.append to
359 if cc
!= null and cc
.length
> 0 then
360 lines
.add
"Cc:{cc.join(",")}"
361 all_recipients
.append cc
365 if bcc
!= null and bcc
.length
> 0 then all_recipients
.append bcc
367 if all_recipients
.is_empty
then return answer_failure
(0, "There must be at lease one recipient")
369 var err
= self.curl
.native
.easy_setopt
(new CURLOption.follow_location
, 1)
370 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
372 err
= self.curl
.native
.easy_setopt
(new CURLOption.mail_rcpt
, all_recipients
.to_curlslist
)
373 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
377 if not from
== null then
378 lines
.add
"From:{from}"
380 err
= self.curl
.native
.easy_setopt
(new CURLOption.mail_from
, from
)
381 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
385 var subject
= self.subject
386 if subject
== null then subject
= "" # Default
387 lines
.add
"Subject: {subject}"
390 var headers_body
= self.headers_body
391 if not headers_body
.is_empty
then
392 for k
, v
in headers_body
do lines
.add
"{k}{v}"
397 if body
== null then body
= "" # Default
403 err
= self.curl
.native
.register_callback_read
(self)
404 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
406 var content
= lines
.join
("\n")
407 err
= self.curl
.native
.register_read_datas_callback
(self, content
)
408 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
410 var err_resp
= perform
411 if err_resp
!= null then return err_resp
417 # Callbacks Interface, allow you to manage in your way the different streams
418 interface CurlCallbacks
419 super NativeCurlCallbacks
422 # Abstract Curl request response
423 abstract class CurlResponse
426 # Failed Response Class returned when errors during configuration are raised
427 class CurlResponseFailed
431 var error_msg
: String
433 redef fun to_s
do return "{error_msg} ({error_code})"
436 # Success Abstract Response Success Class
437 abstract class CurlResponseSuccessIntern
441 var headers
= new HashMap[String, String]
443 # Receive headers from request due to headers callback registering
444 redef fun header_callback
(line
)
446 var splitted
= line
.split_with
(':')
447 if splitted
.length
> 1 then
448 var key
= splitted
.shift
449 self.headers
[key
] = splitted
.to_s
454 # Success Response Class of a basic response
455 class CurlResponseSuccess
456 super CurlResponseSuccessIntern
461 # Receive body from request due to body callback registering
462 redef fun body_callback
(line
) do
463 self.body_str
= "{self.body_str}{line}"
467 # Success Response Class of a downloaded File
468 class CurlFileResponseSuccess
469 super CurlResponseSuccessIntern
472 var speed_download
= 0.0
473 var size_download
= 0.0
475 private var file
: nullable FileWriter = null
477 # Receive bytes stream from request due to stream callback registering
478 redef fun stream_callback
(buffer
)
484 # Pseudo map associating `String` to `String` for HTTP exchanges
486 # This structure differs from `Map` as each key can have multiple associations
487 # and the order of insertion is important to some services.
489 private var array
= new Array[Couple[String, String]]
491 # Add a `value` associated to `key`
492 fun []=(key
, value
: String)
494 array
.add
new Couple[String, String](key
, value
)
497 # Get a list of the keys associated to `key`
498 fun [](k
: String): Array[String]
500 var res
= new Array[String]
501 for c
in array
do if c
.first
== k
then res
.add c
.second
505 # Iterate over all the associations in `self`
506 fun iterator
: MapIterator[String, String] do return new HeaderMapIterator(self)
508 # Get `self` as a single string for HTTP POST
510 # Require: `curl.is_ok`
511 fun to_url_encoded
(curl
: Curl): String
515 var lines
= new Array[String]
517 if k
.length
== 0 then continue
519 k
= curl
.native
.escape
(k
)
520 v
= curl
.native
.escape
(v
)
523 return lines
.join
("&")
526 # Concatenate couple of 'key value' separated by 'sep' in Array
527 fun join_pairs
(sep
: String): Array[String]
529 var col
= new Array[String]
530 for k
, v
in self do col
.add
("{k}{sep}{v}")
534 # Number of values in `self`
535 fun length
: Int do return array
.length
538 fun is_empty
: Bool do return array
.is_empty
541 private class HeaderMapIterator
542 super MapIterator[String, String]
545 var iterator
: Iterator[Couple[String, String]] = map
.array
.iterator
is lazy
547 redef fun is_ok
do return self.iterator
.is_ok
548 redef fun next
do self.iterator
.next
549 redef fun item
do return self.iterator
.item
.second
550 redef fun key
do return self.iterator
.item
.first