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
28 init do self.curl
= new Curl
31 fun destroy
do self.curl
.destroy
33 fun request
(url
: String): nullable CurlRequest do return self.curl
.http_request
(url
)
36 redef fun header_callback
(line
: String) do
37 # We keep this callback silent for testing purposes
38 #if not line.has_prefix("Date:") then print "Header_callback : {line}"
42 redef fun body_callback
(line
: String) do print
"Body_callback : {line}"
44 # Stream callback - Cf : No one is registered
45 redef fun stream_callback
(buffer
: String, size
: Int, count
: Int) do print
"Stream_callback : {buffer} - {size} - {count}"
50 if args
.length
< 2 then
51 print
"Usage {sys.program_name} <method wished [POST, GET, GET_FILE]> <target url>"
54 var myHttpFetcher
= new MyHttpFetcher
58 if args
[0] == "GET" then
59 var getContentRequest
= myHttpFetcher
.request
(url
)
60 if getContentRequest
isa CurlHTTPRequest then
62 getContentRequest
.verbose
= false
63 var getResponse
= getContentRequest
.execute
65 if getResponse
isa CurlResponseSuccess then
66 print
"Status code : {getResponse.status_code}"
67 print
"Body : {getResponse.body_str}"
68 else if getResponse
isa CurlResponseFailed then
69 print
"Error code : {getResponse.error_code}"
70 print
"Error msg : {getResponse.error_msg}"
74 print
"Wrong init with Curl HTTP request"
78 else if args
[0] == "POST" then
79 var postContentRequest
= myHttpFetcher
.request
(url
)
80 if postContentRequest
isa CurlHTTPRequest then
82 postContentRequest
.delegate
= myHttpFetcher
83 var postDatas
= new HeaderMap
84 postDatas
["Bugs Bunny"] = "Daffy Duck"
85 postDatas
["Batman"] = "Robin likes special characters @#ùà!è§'(\"é
&://,;<>∞~
*"
86 postDatas["Batman"] = "Yes you can set multiple identical keys
, but
APACHE will consider only once
, the last one
"
87 postContentRequest.datas = postDatas
88 postContentRequest.verbose = false
89 var postResponse = postContentRequest.execute
91 if postResponse isa CurlResponseSuccess then
92 print "*** Answer ***"
93 print "Status code
: {postResponse.status_code}"
94 print "Body should be empty
, because we decided to manage callbacks
: {postResponse.body_str.length}"
95 else if postResponse isa CurlResponseFailed then
96 print "Error code
: {postResponse.error_code}"
97 print "Error msg
: {postResponse.error_msg}"
101 print "Wrong init with
Curl HTTP request
"
104 # HTTP Get to file Request
105 else if args[0] == "GET_FILE" then
106 var downloadFileRequest = myHttpFetcher.request(url)
107 if downloadFileRequest isa CurlHTTPRequest then
109 var headers = new HeaderMap
110 headers["Accept"] = "Moo"
111 downloadFileRequest.headers = headers
112 downloadFileRequest.verbose = false
113 var downloadResponse = downloadFileRequest.download_to_file(null)
115 if downloadResponse isa CurlFileResponseSuccess then
116 print "*** Answer ***"
117 print "Status code
: {downloadResponse.status_code}"
118 print "Size downloaded
: {downloadResponse.size_download}"
119 else if downloadResponse isa CurlResponseFailed then
120 print "Error code
: {downloadResponse.error_code}"
121 print "Error msg
: {downloadResponse.error_msg}"
125 print "Wrong init with
Curl HTTP request
"
130 print "Usage : Method[POST, GET, GET_FILE]"