lib/curl: use File and NativeFile instead of the local OFile
[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 curl_c
21
22 # Top level of Curl
23 class Curl
24 protected var prim_curl = new CCurl.easy_init
25
26 init
27 do
28 assert curlInstance:self.prim_curl.is_init else
29 print "Curl must be instancied to be used"
30 end
31 end
32
33 # Check for correct initialization
34 fun is_ok: Bool do return self.prim_curl.is_init
35
36 # Release Curl instance
37 fun destroy do self.prim_curl.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.prim_curl.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.prim_curl.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 CCurlCallbacks
76 super CurlCallbacksRegisterIntern
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.prim_curl.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.prim_curl.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.prim_curl.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.prim_curl.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.prim_curl.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.prim_curl.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.prim_curl)
127 err = self.curl.prim_curl.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.prim_curl.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.prim_curl.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.prim_curl.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.prim_curl.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.prim_curl.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.prim_curl.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.prim_curl.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.prim_curl.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.prim_curl.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 CCurlCallbacks
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.prim_curl.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.prim_curl.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.prim_curl.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.prim_curl.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.prim_curl.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.prim_curl.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 err = self.curl.prim_curl.register_callback_read(self)
320 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
321 err = self.curl.prim_curl.register_read_datas_callback(self, content)
322 if not err.is_ok then return answer_failure(err.to_i, err.to_s)
323
324 var err_resp = perform
325 if err_resp != null then return err_resp
326
327 return success_response
328 end
329 end
330
331 # Callbacks Interface, allow you to manage in your way the different streams
332 interface CurlCallbacks
333 super CCurlCallbacks
334 end
335
336 # Callbacks attributes
337 abstract class CurlCallbacksRegisterIntern
338 var delegate: nullable CurlCallbacks = null is writable
339 end
340
341 # Abstract Curl request response
342 abstract class CurlResponse
343 end
344
345 # Failed Response Class returned when errors during configuration are raised
346 class CurlResponseFailed
347 super CurlResponse
348
349 var error_code: Int
350 var error_msg: String
351 end
352
353 # Success Abstract Response Success Class
354 abstract class CurlResponseSuccessIntern
355 super CurlCallbacks
356 super CurlResponse
357
358 var headers = new HashMap[String, String]
359
360 # Receive headers from request due to headers callback registering
361 redef fun header_callback(line)
362 do
363 var splitted = line.split_with(':')
364 if splitted.length > 1 then
365 var key = splitted.shift
366 self.headers[key] = splitted.to_s
367 end
368 end
369 end
370
371 # Success Response Class of a basic response
372 class CurlResponseSuccess
373 super CurlResponseSuccessIntern
374
375 var body_str = ""
376 var status_code = 0
377
378 # Receive body from request due to body callback registering
379 redef fun body_callback(line) do
380 self.body_str = "{self.body_str}{line}"
381 end
382 end
383
384 # Success Response Class of mail request
385 class CurlMailResponseSuccess
386 super CurlResponseSuccessIntern
387 end
388
389 # Success Response Class of a downloaded File
390 class CurlFileResponseSuccess
391 super CurlResponseSuccessIntern
392
393 var status_code = 0
394 var speed_download = 0
395 var size_download = 0
396 var total_time = 0
397 private var i_file: nullable FileWriter = null
398
399 # Receive bytes stream from request due to stream callback registering
400 redef fun stream_callback(buffer)
401 do
402 i_file.write buffer
403 end
404 end
405
406 # Pseudo map associating Strings to Strings,
407 # each key can have multiple associations
408 # and the order of insertion is important.
409 class HeaderMap
410 private var arr = new Array[Couple[String, String]]
411
412 fun []=(k, v: String) do arr.add(new Couple[String, String](k, v))
413
414 fun [](k: String): Array[String]
415 do
416 var res = new Array[String]
417 for c in arr do if c.first == k then res.add(c.second)
418 return res
419 end
420
421 fun iterator: MapIterator[String, String] do return new HeaderMapIterator(self)
422
423 # Convert Self to a single string used to post http fields
424 fun to_url_encoded(curl: CCurl): String
425 do
426 assert curlNotInitialized: curl.is_init else
427 print "to_url_encoded required a valid instance of CCurl Object."
428 end
429 var str = ""
430 var length = self.length
431 var i = 0
432 for k, v in self do
433 if k.length > 0 then
434 k = curl.escape(k)
435 v = curl.escape(v)
436 str = "{str}{k}={v}"
437 if i < length-1 then str = "{str}&"
438 end
439 i += 1
440 end
441 return str
442 end
443
444 # Concatenate couple of 'key value' separated by 'sep' in Array
445 fun join_pairs(sep: String): Array[String]
446 do
447 var col = new Array[String]
448 for k, v in self do col.add("{k}{sep}{v}")
449 return col
450 end
451
452 fun length: Int do return arr.length
453 fun is_empty: Bool do return arr.is_empty
454 end
455
456 class HeaderMapIterator
457 super MapIterator[String, String]
458
459 private var iterator: Iterator[Couple[String, String]]
460 init(map: HeaderMap) is old_style_init do self.iterator = map.arr.iterator
461
462 redef fun is_ok do return self.iterator.is_ok
463 redef fun next do self.iterator.next
464 redef fun item do return self.iterator.item.second
465 redef fun key do return self.iterator.item.first
466 end