From 21a1f5e02c3660c0906769a4bd8fbe8717517cb0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Sat, 6 Jun 2015 21:13:46 -0400 Subject: [PATCH] lib/curl: revamp the curl_http example MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/curl/examples/curl_http.nit | 135 ++++++++++++++++++--------------------- tests/sav/curl_http.res | 2 +- tests/sav/curl_http_args1.res | 4 +- tests/sav/curl_http_args2.res | 6 +- tests/sav/curl_http_args3.res | 4 +- 5 files changed, 71 insertions(+), 80 deletions(-) diff --git a/lib/curl/examples/curl_http.nit b/lib/curl/examples/curl_http.nit index 948ba38..d19ee96 100644 --- a/lib/curl/examples/curl_http.nit +++ b/lib/curl/examples/curl_http.nit @@ -14,100 +14,91 @@ # See the License for the specific language governing permissions and # limitations under the License. -# Sample of the Curl module. +# Example use of the Curl module module curl_http import curl -# Small class to represent an Http Fetcher +# Custom delegate to receive callbacks from a Curl transfer class MyHttpFetcher super CurlCallbacks - var curl: Curl - var our_body: String = "" + # Body of the downloaded file + var fetched_body = "" - init(curl: Curl) do self.curl = curl - - # Release curl object - fun destroy do self.curl.destroy - - # Header callback redef fun header_callback(line) do # We keep this callback silent for testing purposes - #if not line.has_prefix("Date:") then print "Header_callback : {line}" + #if not line.has_prefix("Date:") then print "Header_callback: {line}" end - # Body callback - redef fun body_callback(line) do self.our_body = "{self.our_body}{line}" + redef fun body_callback(line) do self.fetched_body += line - # Stream callback - Cf : No one is registered - redef fun stream_callback(buffer, size, count) do print "Stream_callback : {buffer} - {size} - {count}" + redef fun stream_callback(buffer) do print "Stream_callback: {buffer}" end +private fun print_usage do print "Usage: curl_http [POST|GET|GET_FILE] url" -# Program if args.length < 2 then - print "Usage: curl_http " -else - var curl = new Curl - var url = args[1] - var request = new CurlHTTPRequest(url, curl) + print_usage + exit 1 +end + +var url = args[1] +var request = new CurlHTTPRequest(url) +request.verbose = false # Set to `true` to debug +if args[0] == "GET" then # HTTP Get Request - if args[0] == "GET" then - request.verbose = false - var getResponse = request.execute - - if getResponse isa CurlResponseSuccess then - print "Status code : {getResponse.status_code}" - print "Body : {getResponse.body_str}" - else if getResponse isa CurlResponseFailed then - print "Error code : {getResponse.error_code}" - print "Error msg : {getResponse.error_msg}" - end + var response = request.execute + + if response isa CurlResponseSuccess then + print "Status code: {response.status_code}" + print "Body: {response.body_str}" + else if response isa CurlResponseFailed then + print "Error code: {response.error_code}" + print "Error msg: {response.error_msg}" + end +else if args[0] == "POST" then # HTTP Post Request - else if args[0] == "POST" then - var myHttpFetcher = new MyHttpFetcher(curl) - request.delegate = myHttpFetcher - - var postDatas = new HeaderMap - postDatas["Bugs Bunny"] = "Daffy Duck" - postDatas["Batman"] = "Robin likes special characters @#ùà!è§'(\"é&://,;<>∞~*" - postDatas["Batman"] = "Yes you can set multiple identical keys, but APACHE will consider only once, the last one" - request.datas = postDatas - request.verbose = false - var postResponse = request.execute - - print "Our body from the callback : {myHttpFetcher.our_body}" - - if postResponse isa CurlResponseSuccess then - print "*** Answer ***" - print "Status code : {postResponse.status_code}" - print "Body should be empty, because we decided to manage callbacks : {postResponse.body_str.length}" - else if postResponse isa CurlResponseFailed then - print "Error code : {postResponse.error_code}" - print "Error msg : {postResponse.error_msg}" - end + var my_http_fetcher = new MyHttpFetcher + request.delegate = my_http_fetcher + + var post_datas = new HeaderMap + post_datas["Bugs Bunny"] = "Daffy Duck" + post_datas["Batman"] = "Robin likes special characters @#ùà!è§'(\"é&://,;<>∞~*" + post_datas["Batman"] = "Yes you can set multiple identical keys, but APACHE will consider only one, the last one" + request.datas = post_datas + var response = request.execute + + print "Our body from the callback: {my_http_fetcher.fetched_body}" + + if response isa CurlResponseSuccess then + print "*** Answer ***" + print "Status code: {response.status_code}" + print "Body should be empty, because we decided to manage callbacks: {response.body_str.length}" + else if response isa CurlResponseFailed then + print "Error code: {response.error_code}" + print "Error msg: {response.error_msg}" + end +else if args[0] == "GET_FILE" then # HTTP Get to file Request - else if args[0] == "GET_FILE" then - var headers = new HeaderMap - headers["Accept"] = "Moo" - request.headers = headers - request.verbose = false - var downloadResponse = request.download_to_file(null) - - if downloadResponse isa CurlFileResponseSuccess then - print "*** Answer ***" - print "Status code : {downloadResponse.status_code}" - print "Size downloaded : {downloadResponse.size_download}" - else if downloadResponse isa CurlResponseFailed then - print "Error code : {downloadResponse.error_code}" - print "Error msg : {downloadResponse.error_msg}" - end - # Program logic - else - print "Usage : Method[POST, GET, GET_FILE]" + var headers = new HeaderMap + headers["Accept"] = "Moo" + request.headers = headers + var response = request.download_to_file(null) + + if response isa CurlFileResponseSuccess then + print "*** Answer ***" + print "Status code: {response.status_code}" + print "Size downloaded: {response.size_download}" + else if response isa CurlResponseFailed then + print "Error code: {response.error_code}" + print "Error msg: {response.error_msg}" end + +else + print_usage + exit 1 end diff --git a/tests/sav/curl_http.res b/tests/sav/curl_http.res index e4d9464..5118616 100644 --- a/tests/sav/curl_http.res +++ b/tests/sav/curl_http.res @@ -1 +1 @@ -Usage: curl_http +Usage: curl_http [POST|GET|GET_FILE] url diff --git a/tests/sav/curl_http_args1.res b/tests/sav/curl_http_args1.res index c47c136..587f274 100644 --- a/tests/sav/curl_http_args1.res +++ b/tests/sav/curl_http_args1.res @@ -1,5 +1,5 @@ -Status code : 200 -Body : +Status code: 200 +Body: Example Domain diff --git a/tests/sav/curl_http_args2.res b/tests/sav/curl_http_args2.res index a7b3f76..7fa85c5 100644 --- a/tests/sav/curl_http_args2.res +++ b/tests/sav/curl_http_args2.res @@ -1,4 +1,4 @@ -Our body from the callback : +Our body from the callback: Example Domain @@ -50,5 +50,5 @@ Our body from the callback : *** Answer *** -Status code : 200 -Body should be empty, because we decided to manage callbacks : 0 +Status code: 200 +Body should be empty, because we decided to manage callbacks: 0 diff --git a/tests/sav/curl_http_args3.res b/tests/sav/curl_http_args3.res index 0c72a82..bf03427 100644 --- a/tests/sav/curl_http_args3.res +++ b/tests/sav/curl_http_args3.res @@ -1,3 +1,3 @@ *** Answer *** -Status code : 200 -Size downloaded : 1270 +Status code: 200 +Size downloaded: 1270.0 -- 1.7.9.5