lib/curl: remove the CurlCallbaksRegisterIntern class
[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 NativeCurlCallbacks
76
77 var url: String
78 var datas: nullable HeaderMap = null is writable
79 var headers: nullable HeaderMap = null is writable
80 var delegate: nullable CurlCallbacks = 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 super NativeCurlCallbacks
336 end
337
338 # Abstract Curl request response
339 abstract class CurlResponse
340 end
341
342 # Failed Response Class returned when errors during configuration are raised
343 class CurlResponseFailed
344 super CurlResponse
345
346 var error_code: Int
347 var error_msg: String
348 end
349
350 # Success Abstract Response Success Class
351 abstract class CurlResponseSuccessIntern
352 super CurlCallbacks
353 super CurlResponse
354
355 var headers = new HashMap[String, String]
356
357 # Receive headers from request due to headers callback registering
358 redef fun header_callback(line)
359 do
360 var splitted = line.split_with(':')
361 if splitted.length > 1 then
362 var key = splitted.shift
363 self.headers[key] = splitted.to_s
364 end
365 end
366 end
367
368 # Success Response Class of a basic response
369 class CurlResponseSuccess
370 super CurlResponseSuccessIntern
371
372 var body_str = ""
373 var status_code = 0
374
375 # Receive body from request due to body callback registering
376 redef fun body_callback(line) do
377 self.body_str = "{self.body_str}{line}"
378 end
379 end
380
381 # Success Response Class of mail request
382 class CurlMailResponseSuccess
383 super CurlResponseSuccessIntern
384 end
385
386 # Success Response Class of a downloaded File
387 class CurlFileResponseSuccess
388 super CurlResponseSuccessIntern
389
390 var status_code = 0
391 var speed_download = 0
392 var size_download = 0
393 var total_time = 0
394 private var i_file: nullable FileWriter = null
395
396 # Receive bytes stream from request due to stream callback registering
397 redef fun stream_callback(buffer)
398 do
399 i_file.write buffer
400 end
401 end
402
403 # Pseudo map associating Strings to Strings,
404 # each key can have multiple associations
405 # and the order of insertion is important.
406 class HeaderMap
407 private var arr = new Array[Couple[String, String]]
408
409 fun []=(k, v: String) do arr.add(new Couple[String, String](k, v))
410
411 fun [](k: String): Array[String]
412 do
413 var res = new Array[String]
414 for c in arr do if c.first == k then res.add(c.second)
415 return res
416 end
417
418 fun iterator: MapIterator[String, String] do return new HeaderMapIterator(self)
419
420 # Convert Self to a single string used to post http fields
421 fun to_url_encoded(curl: NativeCurl): String
422 do
423 assert curlNotInitialized: curl.is_init else
424 print "to_url_encoded required a valid instance of NativeCurl Object."
425 end
426 var str = ""
427 var length = self.length
428 var i = 0
429 for k, v in self do
430 if k.length > 0 then
431 k = curl.escape(k)
432 v = curl.escape(v)
433 str = "{str}{k}={v}"
434 if i < length-1 then str = "{str}&"
435 end
436 i += 1
437 end
438 return str
439 end
440
441 # Concatenate couple of 'key value' separated by 'sep' in Array
442 fun join_pairs(sep: String): Array[String]
443 do
444 var col = new Array[String]
445 for k, v in self do col.add("{k}{sep}{v}")
446 return col
447 end
448
449 fun length: Int do return arr.length
450 fun is_empty: Bool do return arr.is_empty
451 end
452
453 class HeaderMapIterator
454 super MapIterator[String, String]
455
456 private var iterator: Iterator[Couple[String, String]]
457 init(map: HeaderMap) is old_style_init do self.iterator = map.arr.iterator
458
459 redef fun is_ok do return self.iterator.is_ok
460 redef fun next do self.iterator.next
461 redef fun item do return self.iterator.item.second
462 redef fun key do return self.iterator.item.first
463 end