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 do self.curl
= new Curl
32 fun destroy
do self.curl
.destroy
34 fun request
(url
: String): nullable CurlRequest do return self.curl
.http_request
(url
)
37 redef fun header_callback
(line
: String) do
38 # We keep this callback silent for testing purposes
39 #if not line.has_prefix("Date:") then print "Header_callback : {line}"
43 redef fun body_callback
(line
: String) do self.our_body
= "{self.our_body}{line}"
45 # Stream callback - Cf : No one is registered
46 redef fun stream_callback
(buffer
: String, size
: Int, count
: Int) do print
"Stream_callback : {buffer} - {size} - {count}"
51 if args
.length
< 2 then
52 print
"Usage: curl_http <method wished [POST, GET, GET_FILE]> <target url>"
55 var myHttpFetcher
= new MyHttpFetcher
59 if args
[0] == "GET" then
60 var getContentRequest
= myHttpFetcher
.request
(url
)
61 if getContentRequest
isa CurlHTTPRequest then
63 getContentRequest
.verbose
= false
64 var getResponse
= getContentRequest
.execute
66 if getResponse
isa CurlResponseSuccess then
67 print
"Status code : {getResponse.status_code}"
68 print
"Body : {getResponse.body_str}"
69 else if getResponse
isa CurlResponseFailed then
70 print
"Error code : {getResponse.error_code}"
71 print
"Error msg : {getResponse.error_msg}"
75 print
"Wrong init with Curl HTTP request"
79 else if args
[0] == "POST" then
80 var postContentRequest
= myHttpFetcher
.request
(url
)
81 if postContentRequest
isa CurlHTTPRequest then
83 postContentRequest
.delegate
= myHttpFetcher
84 var postDatas
= new HeaderMap
85 postDatas
["Bugs Bunny"] = "Daffy Duck"
86 postDatas
["Batman"] = "Robin likes special characters @#ùà!è§'(\"é
&://,;<>∞~
*"
87 postDatas["Batman"] = "Yes you can set multiple identical keys
, but
APACHE will consider only once
, the last one
"
88 postContentRequest.datas = postDatas
89 postContentRequest.verbose = false
90 var postResponse = postContentRequest.execute
92 print "Our body from the callback
: {myHttpFetcher.our_body}"
94 if postResponse isa CurlResponseSuccess then
95 print "*** Answer ***"
96 print "Status code
: {postResponse.status_code}"
97 print "Body should be empty
, because we decided to manage callbacks
: {postResponse.body_str.length}"
98 else if postResponse isa CurlResponseFailed then
99 print "Error code
: {postResponse.error_code}"
100 print "Error msg
: {postResponse.error_msg}"
104 print "Wrong init with
Curl HTTP request
"
107 # HTTP Get to file Request
108 else if args[0] == "GET_FILE" then
109 var downloadFileRequest = myHttpFetcher.request(url)
110 if downloadFileRequest isa CurlHTTPRequest then
112 var headers = new HeaderMap
113 headers["Accept"] = "Moo"
114 downloadFileRequest.headers = headers
115 downloadFileRequest.verbose = false
116 var downloadResponse = downloadFileRequest.download_to_file(null)
118 if downloadResponse isa CurlFileResponseSuccess then
119 print "*** Answer ***"
120 print "Status code
: {downloadResponse.status_code}"
121 print "Size downloaded
: {downloadResponse.size_download}"
122 else if downloadResponse isa CurlResponseFailed then
123 print "Error code
: {downloadResponse.error_code}"
124 print "Error msg
: {downloadResponse.error_msg}"
128 print "Wrong init with
Curl HTTP request
"
133 print "Usage : Method[POST, GET, GET_FILE]"