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
105 # Set this value to send raw data instead of the POST formatted `data`.
107 # If `data` is set, the body will not be sent.
108 var body
: nullable String is writable
110 # Header content of the request
111 var headers
: nullable HeaderMap is writable
113 # Delegates to customize the behavior when running `execute`
114 var delegate
: nullable CurlCallbacks is writable
116 # Set the user agent for all following HTTP requests
117 var user_agent
: nullable String is writable
119 # Execute HTTP request
121 # By default, the response body is returned in an instance of `CurlResponse`.
122 # This behavior can be customized by setting a custom `delegate`.
123 fun execute
: CurlResponse
125 if not self.curl
.is_ok
then return answer_failure
(0, "Curl instance is not correctly initialized")
127 var success_response
= new CurlResponseSuccess
128 var callback_receiver
: CurlCallbacks = success_response
129 if self.delegate
!= null then callback_receiver
= self.delegate
.as(not null)
133 err
= self.curl
.native
.easy_setopt
(new CURLOption.follow_location
, 1)
134 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
136 err
= self.curl
.native
.easy_setopt
(new CURLOption.url
, url
)
137 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
139 var user_agent
= user_agent
140 if user_agent
!= null then
141 err
= curl
.native
.easy_setopt
(new CURLOption.user_agent
, user_agent
)
142 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
146 err
= self.curl
.native
.register_callback_header
(callback_receiver
)
147 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
149 err
= self.curl
.native
.register_callback_body
(callback_receiver
)
150 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
153 var headers
= self.headers
154 if headers
!= null then
155 var headers_joined
= headers
.join_pairs
(": ")
156 err
= self.curl
.native
.easy_setopt
(new CURLOption.httpheader
, headers_joined
.to_curlslist
)
157 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
163 var postdatas
= data
.to_url_encoded
(self.curl
)
164 err
= self.curl
.native
.easy_setopt
(new CURLOption.postfields
, postdatas
)
165 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
166 else if body
!= null then
167 err
= self.curl
.native
.easy_setopt
(new CURLOption.postfields
, body
.as(not null))
168 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
171 var err_resp
= perform
172 if err_resp
!= null then return err_resp
174 var st_code
= self.curl
.native
.easy_getinfo_long
(new CURLInfoLong.response_code
)
175 if not st_code
== null then success_response
.status_code
= st_code
177 return success_response
180 # Download to file given resource
181 fun download_to_file
(output_file_name
: nullable String): CurlResponse
183 var success_response
= new CurlFileResponseSuccess
185 var callback_receiver
: CurlCallbacks = success_response
186 if self.delegate
!= null then callback_receiver
= self.delegate
.as(not null)
190 err
= self.curl
.native
.easy_setopt
(new CURLOption.follow_location
, 1)
191 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
193 err
= self.curl
.native
.easy_setopt
(new CURLOption.url
, url
)
194 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
196 err
= self.curl
.native
.register_callback_header
(callback_receiver
)
197 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
199 err
= self.curl
.native
.register_callback_stream
(callback_receiver
)
200 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
203 if not output_file_name
== null then
204 opt_name
= output_file_name
205 else if not self.url
.substring
(self.url
.length-1
, self.url
.length
) == "/" then
206 opt_name
= self.url
.basename
208 return answer_failure
(0, "Unable to extract file name, please specify one")
211 success_response
.file
= new FileWriter.open
(opt_name
)
212 if not success_response
.file
.is_writable
then
213 return answer_failure
(0, "Unable to create associated file")
216 var err_resp
= perform
217 if err_resp
!= null then return err_resp
219 var st_code
= self.curl
.native
.easy_getinfo_long
(new CURLInfoLong.response_code
)
220 if not st_code
== null then success_response
.status_code
= st_code
222 var speed
= self.curl
.native
.easy_getinfo_double
(new CURLInfoDouble.speed_download
)
223 if not speed
== null then success_response
.speed_download
= speed
225 var size
= self.curl
.native
.easy_getinfo_double
(new CURLInfoDouble.size_download
)
226 if not size
== null then success_response
.size_download
= size
228 var time
= self.curl
.native
.easy_getinfo_double
(new CURLInfoDouble.total_time
)
229 if not time
== null then success_response
.total_time
= time
231 success_response
.file
.close
233 return success_response
241 # var mail = new CurlMail("sender@example.org",
242 # to=["to@example.org"], cc=["bob@example.org"])
244 # mail.headers_body["Content-Type:"] = """text/html; charset="UTF-8""""
245 # mail.headers_body["Content-Transfer-Encoding:"] = "quoted-printable"
247 # mail.body = "<h1>Here you can write HTML stuff.</h1>"
248 # mail.subject = "Hello From My Nit Program"
251 # var error = mail.set_outgoing_server("smtps://smtp.example.org:465",
252 # "user@example.org", "mypassword")
253 # if error != null then
254 # print "Mail Server Error: {error}"
259 # error = mail.execute
260 # if error != null then
261 # print "Transfer Error: {error}"
267 super NativeCurlCallbacks
269 # Address of the sender
270 var from
: nullable String is writable
273 var to
: nullable Array[String] is writable
276 var subject
: nullable String is writable
279 var body
: nullable String is writable
282 var cc
: nullable Array[String] is writable
284 # BCC recipients (hidden from other recipients)
285 var bcc
: nullable Array[String] is writable
288 var headers
= new HeaderMap is lazy
, writable
291 var headers_body
= new HeaderMap is lazy
, writable
293 # Protocols supported to send mail to a server
295 # Default value at `["smtp", "smtps"]`
296 var supported_outgoing_protocol
= ["smtp", "smtps"]
298 # Helper method to add pair values to mail content while building it (ex: "To:", "address@mail.com")
299 private fun add_pair_to_content
(str
: String, att
: String, val
: nullable String): String
301 if val
!= null then return "{str}{att}{val}\n"
302 return "{str}{att}\n"
305 # Helper method to add entire list of pairs to mail content
306 private fun add_pairs_to_content
(content
: String, pairs
: HeaderMap): String
308 for h_key
, h_val
in pairs
do content
= add_pair_to_content
(content
, h_key
, h_val
)
312 # Check for host and protocol availability
313 private fun is_supported_outgoing_protocol
(host
: String): CURLCode
315 var host_reach
= host
.split_with
("://")
316 if host_reach
.length
> 1 and supported_outgoing_protocol
.has
(host_reach
[0]) then return once
new CURLCode.ok
317 return once
new CURLCode.unsupported_protocol
320 # Configure server host and user credentials if needed.
321 fun set_outgoing_server
(host
: String, user
: nullable String, pwd
: nullable String): nullable CurlResponseFailed
323 # Check Curl initialisation
324 if not self.curl
.is_ok
then return answer_failure
(0, "Curl instance is not correctly initialized")
329 err
= is_supported_outgoing_protocol
(host
)
330 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
332 err
= self.curl
.native
.easy_setopt
(new CURLOption.url
, host
)
333 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
336 if not user
== null and not pwd
== null then
337 err
= self.curl
.native
.easy_setopt
(new CURLOption.username
, user
)
338 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
340 err
= self.curl
.native
.easy_setopt
(new CURLOption.password
, pwd
)
341 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
347 # Execute Mail request with settings configured through attribute
348 fun execute
: nullable CurlResponseFailed
350 if not self.curl
.is_ok
then return answer_failure
(0, "Curl instance is not correctly initialized")
352 var lines
= new Array[String]
355 var headers
= self.headers
356 if not headers
.is_empty
then
357 for k
, v
in headers
do lines
.add
"{k}{v}"
361 var all_recipients
= new Array[String]
363 if to
!= null and to
.length
> 0 then
364 lines
.add
"To:{to.join(",")}"
365 all_recipients
.append to
369 if cc
!= null and cc
.length
> 0 then
370 lines
.add
"Cc:{cc.join(",")}"
371 all_recipients
.append cc
375 if bcc
!= null and bcc
.length
> 0 then all_recipients
.append bcc
377 if all_recipients
.is_empty
then return answer_failure
(0, "There must be at lease one recipient")
379 var err
= self.curl
.native
.easy_setopt
(new CURLOption.follow_location
, 1)
380 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
382 err
= self.curl
.native
.easy_setopt
(new CURLOption.mail_rcpt
, all_recipients
.to_curlslist
)
383 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
387 if not from
== null then
388 lines
.add
"From:{from}"
390 err
= self.curl
.native
.easy_setopt
(new CURLOption.mail_from
, from
)
391 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
395 var subject
= self.subject
396 if subject
== null then subject
= "" # Default
397 lines
.add
"Subject: {subject}"
400 var headers_body
= self.headers_body
401 if not headers_body
.is_empty
then
402 for k
, v
in headers_body
do lines
.add
"{k}{v}"
407 if body
== null then body
= "" # Default
413 err
= self.curl
.native
.register_callback_read
(self)
414 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
416 var content
= lines
.join
("\n")
417 err
= self.curl
.native
.register_read_datas_callback
(self, content
)
418 if not err
.is_ok
then return answer_failure
(err
.to_i
, err
.to_s
)
420 var err_resp
= perform
421 if err_resp
!= null then return err_resp
427 # Callbacks Interface, allow you to manage in your way the different streams
428 interface CurlCallbacks
429 super NativeCurlCallbacks
432 # Abstract Curl request response
433 abstract class CurlResponse
436 # Failed Response Class returned when errors during configuration are raised
437 class CurlResponseFailed
441 var error_msg
: String
443 redef fun to_s
do return "{error_msg} ({error_code})"
446 # Success Abstract Response Success Class
447 abstract class CurlResponseSuccessIntern
451 var headers
= new HashMap[String, String]
453 # Receive headers from request due to headers callback registering
454 redef fun header_callback
(line
)
456 var splitted
= line
.split_with
(':')
457 if splitted
.length
> 1 then
458 var key
= splitted
.shift
459 self.headers
[key
] = splitted
.to_s
464 # Success Response Class of a basic response
465 class CurlResponseSuccess
466 super CurlResponseSuccessIntern
471 # Receive body from request due to body callback registering
472 redef fun body_callback
(line
) do
473 self.body_str
= "{self.body_str}{line}"
477 # Success Response Class of a downloaded File
478 class CurlFileResponseSuccess
479 super CurlResponseSuccessIntern
482 var speed_download
= 0.0
483 var size_download
= 0.0
485 private var file
: nullable FileWriter = null
487 # Receive bytes stream from request due to stream callback registering
488 redef fun stream_callback
(buffer
)
494 # Pseudo map associating `String` to `String` for HTTP exchanges
496 # This structure differs from `Map` as each key can have multiple associations
497 # and the order of insertion is important to some services.
499 private var array
= new Array[Couple[String, String]]
501 # Add a `value` associated to `key`
502 fun []=(key
, value
: String)
504 array
.add
new Couple[String, String](key
, value
)
507 # Get a list of the keys associated to `key`
508 fun [](k
: String): Array[String]
510 var res
= new Array[String]
511 for c
in array
do if c
.first
== k
then res
.add c
.second
515 # Iterate over all the associations in `self`
516 fun iterator
: MapIterator[String, String] do return new HeaderMapIterator(self)
518 # Get `self` as a single string for HTTP POST
520 # Require: `curl.is_ok`
521 fun to_url_encoded
(curl
: Curl): String
525 var lines
= new Array[String]
527 if k
.length
== 0 then continue
529 k
= curl
.native
.escape
(k
)
530 v
= curl
.native
.escape
(v
)
533 return lines
.join
("&")
536 # Concatenate couple of 'key value' separated by 'sep' in Array
537 fun join_pairs
(sep
: String): Array[String]
539 var col
= new Array[String]
540 for k
, v
in self do col
.add
("{k}{sep}{v}")
544 # Number of values in `self`
545 fun length
: Int do return array
.length
548 fun is_empty
: Bool do return array
.is_empty
551 private class HeaderMapIterator
552 super MapIterator[String, String]
555 var iterator
: Iterator[Couple[String, String]] = map
.array
.iterator
is lazy
557 redef fun is_ok
do return self.iterator
.is_ok
558 redef fun next
do self.iterator
.next
559 redef fun item
do return self.iterator
.item
.second
560 redef fun key
do return self.iterator
.item
.first