lib/curl: replace the `C` prefix by `Native`
[nit.git] / lib / curl / curl.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Matthieu Lucas <lucasmatthieu@gmail.com>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Network functionnalities based on Curl_c module.
18 module curl
19
20 import native_curl
21
22 # Top level of Curl
23 class Curl
24
25 init
26 do
27 assert curlInstance:self.prim_curl.is_init else
28 print "Curl must be instancied to be used"
29 end
30 end
31 protected var native = new NativeCurl.easy_init
32
33 # Check for correct initialization
34 fun is_ok: Bool do return self.native.is_init
35
36 # Release Curl instance
37 fun destroy do self.native.easy_clean
38 end
39
40 # CURL Request
41 class CurlRequest
42
43 var verbose: Bool = false is writable
44 private var curl: nullable Curl = null
45
46 # Launch request method
47 fun execute: CurlResponse is abstract
48
49 # Intern perform method, lowest level of request launching
50 private fun perform: nullable CurlResponseFailed
51 do
52 if not self.curl.is_ok then return answer_failure(0, "Curl instance is not correctly initialized")
53
54 var err
55
56 err = self.curl.native.easy_setopt(new CURLOption.verbose, self.verbose)
57 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
58
59 err = self.curl.native.easy_perform
60 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
61
62 return null
63 end
64
65 # Intern method with return a failed answer with given code and message
66 private fun answer_failure(error_code: Int, error_msg: String): CurlResponseFailed
67 do
68 return new CurlResponseFailed(error_code, error_msg)
69 end
70 end
71
72 # CURL HTTP Request
73 class CurlHTTPRequest
74 super CurlRequest
75 super CurlCallbacksRegisterIntern
76 super NativeCurlCallbacks
77
78 var url: String
79 var datas: nullable HeaderMap = null is writable
80 var headers: nullable HeaderMap = null is writable
81
82 # Set the user agent for all following HTTP requests
83 fun user_agent=(name: String)
84 do
85 curl.native.easy_setopt(new CURLOption.user_agent, name)
86 end
87
88 init (url: String, curl: nullable Curl) is old_style_init do
89 self.url = url
90 self.curl = curl
91 end
92
93 # Execute HTTP request with settings configured through attribute
94 redef fun execute
95 do
96 if not self.curl.is_ok then return answer_failure(0, "Curl instance is not correctly initialized")
97
98 var success_response = new CurlResponseSuccess
99 var callback_receiver: CurlCallbacks = success_response
100 if self.delegate != null then callback_receiver = self.delegate.as(not null)
101
102 var err
103
104 err = self.curl.native.easy_setopt(new CURLOption.follow_location, 1)
105 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
106
107 err = self.curl.native.easy_setopt(new CURLOption.url, url)
108 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
109
110 # Callbacks
111 err = self.curl.native.register_callback_header(callback_receiver)
112 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
113
114 err = self.curl.native.register_callback_body(callback_receiver)
115 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
116
117 # HTTP Header
118 if self.headers != null then
119 var headers_joined = self.headers.join_pairs(": ")
120 err = self.curl.native.easy_setopt(new CURLOption.httpheader, headers_joined.to_curlslist)
121 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
122 end
123
124 # Datas
125 if self.datas != null then
126 var postdatas = self.datas.to_url_encoded(self.curl.native)
127 err = self.curl.native.easy_setopt(new CURLOption.postfields, postdatas)
128 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
129 end
130
131 var err_resp = perform
132 if err_resp != null then return err_resp
133
134 var st_code = self.curl.native.easy_getinfo_long(new CURLInfoLong.response_code)
135 if not st_code == null then success_response.status_code = st_code.response
136
137 return success_response
138 end
139
140 # Download to file given resource
141 fun download_to_file(output_file_name: nullable String): CurlResponse
142 do
143 var success_response = new CurlFileResponseSuccess
144
145 var callback_receiver: CurlCallbacks = success_response
146 if self.delegate != null then callback_receiver = self.delegate.as(not null)
147
148 var err
149
150 err = self.curl.native.easy_setopt(new CURLOption.follow_location, 1)
151 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
152
153 err = self.curl.native.easy_setopt(new CURLOption.url, url)
154 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
155
156 err = self.curl.native.register_callback_header(callback_receiver)
157 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
158
159 err = self.curl.native.register_callback_stream(callback_receiver)
160 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
161
162 var opt_name
163 if not output_file_name == null then
164 opt_name = output_file_name
165 else if not self.url.substring(self.url.length-1, self.url.length) == "/" then
166 opt_name = self.url.basename("")
167 else
168 return answer_failure(0, "Unable to extract file name, please specify one")
169 end
170
171 success_response.i_file = new FileWriter.open(opt_name)
172 if not success_response.i_file.is_writable then
173 return answer_failure(0, "Unable to create associated file")
174 end
175
176 var err_resp = perform
177 if err_resp != null then return err_resp
178
179 var st_code = self.curl.native.easy_getinfo_long(new CURLInfoLong.response_code)
180 if not st_code == null then success_response.status_code = st_code.response
181
182 var speed = self.curl.native.easy_getinfo_double(new CURLInfoDouble.speed_download)
183 if not speed == null then success_response.speed_download = speed.response
184
185 var size = self.curl.native.easy_getinfo_double(new CURLInfoDouble.size_download)
186 if not size == null then success_response.size_download = size.response
187
188 var time = self.curl.native.easy_getinfo_double(new CURLInfoDouble.total_time)
189 if not time == null then success_response.total_time = time.response
190
191 success_response.i_file.close
192
193 return success_response
194 end
195 end
196
197 # CURL Mail Request
198 class CurlMailRequest
199 super CurlRequest
200 super NativeCurlCallbacks
201
202 var headers: nullable HeaderMap = null is writable
203 var headers_body: nullable HeaderMap = null is writable
204 var from: nullable String = null is writable
205 var to: nullable Array[String] = null is writable
206 var cc: nullable Array[String] = null is writable
207 var bcc: nullable Array[String] = null is writable
208 var subject: nullable String = "" is writable
209 var body: nullable String = "" is writable
210 private var supported_outgoing_protocol: Array[String] = ["smtp", "smtps"]
211
212 init (curl: nullable Curl) is old_style_init do
213 self.curl = curl
214 end
215
216 # Helper method to add conventional space while building entire mail
217 private fun add_conventional_space(str: String):String do return "{str}\n" end
218
219 # Helper method to add pair values to mail content while building it (ex: "To:", "address@mail.com")
220 private fun add_pair_to_content(str: String, att: String, val: nullable String):String
221 do
222 if val != null then return "{str}{att}{val}\n"
223 return "{str}{att}\n"
224 end
225
226 # Helper method to add entire list of pairs to mail content
227 private fun add_pairs_to_content(content: String, pairs: HeaderMap):String
228 do
229 for h_key, h_val in pairs do content = add_pair_to_content(content, h_key, h_val)
230 return content
231 end
232
233 # Check for host and protocol availability
234 private fun is_supported_outgoing_protocol(host: String):CURLCode
235 do
236 var host_reach = host.split_with("://")
237 if host_reach.length > 1 and supported_outgoing_protocol.has(host_reach[0]) then return once new CURLCode.ok
238 return once new CURLCode.unsupported_protocol
239 end
240
241 # Configure server host and user credentials if needed.
242 fun set_outgoing_server(host: String, user: nullable String, pwd: nullable String): nullable CurlResponseFailed
243 do
244 # Check Curl initialisation
245 if not self.curl.is_ok then return answer_failure(0, "Curl instance is not correctly initialized")
246
247 var err
248
249 # Host & Protocol
250 err = is_supported_outgoing_protocol(host)
251 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
252 err = self.curl.native.easy_setopt(new CURLOption.url, host)
253 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
254
255 # Credentials
256 if not user == null and not pwd == null then
257 err = self.curl.native.easy_setopt(new CURLOption.username, user)
258 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
259 err = self.curl.native.easy_setopt(new CURLOption.password, pwd)
260 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
261 end
262
263 return null
264 end
265
266 # Execute Mail request with settings configured through attribute
267 redef fun execute
268 do
269 if not self.curl.is_ok then return answer_failure(0, "Curl instance is not correctly initialized")
270
271 var success_response = new CurlMailResponseSuccess
272 var content = ""
273 # Headers
274 if self.headers != null then
275 content = add_pairs_to_content(content, self.headers.as(not null))
276 end
277
278 # Recipients
279 var g_rec = new Array[String]
280 if self.to != null and self.to.length > 0 then
281 content = add_pair_to_content(content, "To:", self.to.join(","))
282 g_rec.append(self.to.as(not null))
283 end
284 if self.cc != null and self.cc.length > 0 then
285 content = add_pair_to_content(content, "Cc:", self.cc.join(","))
286 g_rec.append(self.cc.as(not null))
287 end
288 if self.bcc != null and self.bcc.length > 0 then g_rec.append(self.bcc.as(not null))
289
290 if g_rec.length < 1 then return answer_failure(0, "The mail recipients can not be empty")
291
292 var err
293
294 err = self.curl.native.easy_setopt(new CURLOption.follow_location, 1)
295 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
296
297 err = self.curl.native.easy_setopt(new CURLOption.mail_rcpt, g_rec.to_curlslist)
298 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
299
300 # From
301 if not self.from == null then
302 content = add_pair_to_content(content, "From:", self.from)
303 err = self.curl.native.easy_setopt(new CURLOption.mail_from, self.from.as(not null))
304 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
305 end
306
307 # Subject
308 content = add_pair_to_content(content, "Subject:", self.subject)
309
310 # Headers body
311 if self.headers_body != null then
312 content = add_pairs_to_content(content, self.headers_body.as(not null))
313 end
314
315 # Body
316 content = add_conventional_space(content)
317 content = add_pair_to_content(content, "", self.body)
318 content = add_conventional_space(content)
319
320 err = self.curl.native.register_callback_read(self)
321 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
322
323 err = self.curl.native.register_read_datas_callback(self, content)
324 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
325
326 var err_resp = perform
327 if err_resp != null then return err_resp
328
329 return success_response
330 end
331 end
332
333 # Callbacks Interface, allow you to manage in your way the different streams
334 interface CurlCallbacks
335 end
336
337 # Callbacks attributes
338 abstract class CurlCallbacksRegisterIntern
339 var delegate: nullable CurlCallbacks = null is writable
340 super NativeCurlCallbacks
341 end
342
343 # Abstract Curl request response
344 abstract class CurlResponse
345 end
346
347 # Failed Response Class returned when errors during configuration are raised
348 class CurlResponseFailed
349 super CurlResponse
350
351 var error_code: Int
352 var error_msg: String
353 end
354
355 # Success Abstract Response Success Class
356 abstract class CurlResponseSuccessIntern
357 super CurlCallbacks
358 super CurlResponse
359
360 var headers = new HashMap[String, String]
361
362 # Receive headers from request due to headers callback registering
363 redef fun header_callback(line)
364 do
365 var splitted = line.split_with(':')
366 if splitted.length > 1 then
367 var key = splitted.shift
368 self.headers[key] = splitted.to_s
369 end
370 end
371 end
372
373 # Success Response Class of a basic response
374 class CurlResponseSuccess
375 super CurlResponseSuccessIntern
376
377 var body_str = ""
378 var status_code = 0
379
380 # Receive body from request due to body callback registering
381 redef fun body_callback(line) do
382 self.body_str = "{self.body_str}{line}"
383 end
384 end
385
386 # Success Response Class of mail request
387 class CurlMailResponseSuccess
388 super CurlResponseSuccessIntern
389 end
390
391 # Success Response Class of a downloaded File
392 class CurlFileResponseSuccess
393 super CurlResponseSuccessIntern
394
395 var status_code = 0
396 var speed_download = 0
397 var size_download = 0
398 var total_time = 0
399 private var i_file: nullable FileWriter = null
400
401 # Receive bytes stream from request due to stream callback registering
402 redef fun stream_callback(buffer)
403 do
404 i_file.write buffer
405 end
406 end
407
408 # Pseudo map associating Strings to Strings,
409 # each key can have multiple associations
410 # and the order of insertion is important.
411 class HeaderMap
412 private var arr = new Array[Couple[String, String]]
413
414 fun []=(k, v: String) do arr.add(new Couple[String, String](k, v))
415
416 fun [](k: String): Array[String]
417 do
418 var res = new Array[String]
419 for c in arr do if c.first == k then res.add(c.second)
420 return res
421 end
422
423 fun iterator: MapIterator[String, String] do return new HeaderMapIterator(self)
424
425 # Convert Self to a single string used to post http fields
426 fun to_url_encoded(curl: NativeCurl): String
427 do
428 assert curlNotInitialized: curl.is_init else
429 print "to_url_encoded required a valid instance of NativeCurl Object."
430 end
431 var str = ""
432 var length = self.length
433 var i = 0
434 for k, v in self do
435 if k.length > 0 then
436 k = curl.escape(k)
437 v = curl.escape(v)
438 str = "{str}{k}={v}"
439 if i < length-1 then str = "{str}&"
440 end
441 i += 1
442 end
443 return str
444 end
445
446 # Concatenate couple of 'key value' separated by 'sep' in Array
447 fun join_pairs(sep: String): Array[String]
448 do
449 var col = new Array[String]
450 for k, v in self do col.add("{k}{sep}{v}")
451 return col
452 end
453
454 fun length: Int do return arr.length
455 fun is_empty: Bool do return arr.is_empty
456 end
457
458 class HeaderMapIterator
459 super MapIterator[String, String]
460
461 private var iterator: Iterator[Couple[String, String]]
462 init(map: HeaderMap) is old_style_init do self.iterator = map.arr.iterator
463
464 redef fun is_ok do return self.iterator.is_ok
465 redef fun next do self.iterator.next
466 redef fun item do return self.iterator.item.second
467 redef fun key do return self.iterator.item.first
468 end