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 # Sample of the Curl module.
22 # Small class to represent an Http Fetcher
27 var our_body
: String = ""
29 init(curl
: Curl) do self.curl
= curl
32 fun destroy
do self.curl
.destroy
35 redef fun header_callback
(line
: String) do
36 # We keep this callback silent for testing purposes
37 #if not line.has_prefix("Date:") then print "Header_callback : {line}"
41 redef fun body_callback
(line
: String) do self.our_body
= "{self.our_body}{line}"
43 # Stream callback - Cf : No one is registered
44 redef fun stream_callback
(buffer
: String, size
: Int, count
: Int) do print
"Stream_callback : {buffer} - {size} - {count}"
49 if args
.length
< 2 then
50 print
"Usage: curl_http <method wished [POST, GET, GET_FILE]> <target url>"
54 var request
= new CurlHTTPRequest(url
, curl
)
57 if args
[0] == "GET" then
58 request
.verbose
= false
59 var getResponse
= request
.execute
61 if getResponse
isa CurlResponseSuccess then
62 print
"Status code : {getResponse.status_code}"
63 print
"Body : {getResponse.body_str}"
64 else if getResponse
isa CurlResponseFailed then
65 print
"Error code : {getResponse.error_code}"
66 print
"Error msg : {getResponse.error_msg}"
70 else if args
[0] == "POST" then
71 var myHttpFetcher
= new MyHttpFetcher(curl
)
72 request
.delegate
= myHttpFetcher
74 var postDatas
= new HeaderMap
75 postDatas
["Bugs Bunny"] = "Daffy Duck"
76 postDatas
["Batman"] = "Robin likes special characters @#ùà!è§'(\"é
&://,;<>∞~
*"
77 postDatas["Batman"] = "Yes you can set multiple identical keys
, but
APACHE will consider only once
, the last one
"
78 request.datas = postDatas
79 request.verbose = false
80 var postResponse = request.execute
82 print "Our body from the callback
: {myHttpFetcher.our_body}"
84 if postResponse isa CurlResponseSuccess then
85 print "*** Answer ***"
86 print "Status code
: {postResponse.status_code}"
87 print "Body should be empty
, because we decided to manage callbacks
: {postResponse.body_str.length}"
88 else if postResponse isa CurlResponseFailed then
89 print "Error code
: {postResponse.error_code}"
90 print "Error msg
: {postResponse.error_msg}"
93 # HTTP Get to file Request
94 else if args[0] == "GET_FILE" then
95 var headers = new HeaderMap
96 headers["Accept"] = "Moo"
97 request.headers = headers
98 request.verbose = false
99 var downloadResponse = request.download_to_file(null)
101 if downloadResponse isa CurlFileResponseSuccess then
102 print "*** Answer ***"
103 print "Status code
: {downloadResponse.status_code}"
104 print "Size downloaded
: {downloadResponse.size_download}"
105 else if downloadResponse isa CurlResponseFailed then
106 print "Error code
: {downloadResponse.error_code}"
107 print "Error msg
: {downloadResponse.error_msg}"
111 print "Usage : Method[POST, GET, GET_FILE]"