X-Git-Url: http://nitlanguage.org?ds=inline diff --git a/lib/curl/native_curl.nit b/lib/curl/native_curl.nit index 7d98466..8fc9ccb 100644 --- a/lib/curl/native_curl.nit +++ b/lib/curl/native_curl.nit @@ -17,8 +17,8 @@ # Binding of C libCurl which allow us to interact with network. module native_curl is pkgconfig "libcurl" -intrude import standard::file -import standard +intrude import core::file +import core in "C header" `{ #include @@ -46,7 +46,7 @@ in "C body" `{ `} redef extern class NativeString - private fun native_callback_header(size, count: Int, target: CCurlCallbacks): Int + private fun native_callback_header(size, count: Int, target: NativeCurlCallbacks): Int do target.header_callback to_s_with_length(size*count) @@ -54,14 +54,14 @@ redef extern class NativeString return count end - private fun native_callback_body(size, count: Int, target: CCurlCallbacks): Int + private fun native_callback_body(size, count: Int, target: NativeCurlCallbacks): Int do target.body_callback to_s_with_length(size*count) return count end - private fun native_callback_stream(size, count: Int, target: CCurlCallbacks): Int + private fun native_callback_stream(size, count: Int, target: NativeCurlCallbacks): Int do target.stream_callback to_s_with_length(size*count) @@ -70,7 +70,7 @@ redef extern class NativeString end # CURL Extern Type, reproduce CURL low level behaviors -extern class CCurl `{ CURL * `} +extern class NativeCurl `{ CURL * `} # Constructor, CURL low level initializer new easy_init `{ return curl_easy_init(); `} @@ -113,87 +113,87 @@ extern class CCurl `{ CURL * `} `} # Request Chars internal information from the CURL session - fun easy_getinfo_chars(opt: CURLInfoChars): nullable CURLInfoResponseString + fun easy_getinfo_chars(opt: CURLInfoChars): nullable String do - var answ = new CURLInfoResponseString + var answ = new Ref[NativeString]("".to_cstring) if not native_getinfo_chars(opt, answ).is_ok then return null - return answ + if answ.item.address_is_null then return null + return answ.item.to_s end # Internal method used to get String object information initially knowns as C Chars type - private fun native_getinfo_chars(opt: CURLInfoChars, res: CURLInfoResponseString): CURLCode import CURLInfoResponseString.response=, NativeString.to_s_with_copy `{ - char *r = NULL; + private fun native_getinfo_chars(opt: CURLInfoChars, res: Ref[NativeString]): CURLCode + import Ref[NativeString].item= `{ + char *r; CURLcode c = curl_easy_getinfo( self, opt, &r); - if((c == CURLE_OK) && r != NULL){ - String ro = NativeString_to_s_with_copy(r); - CURLInfoResponseString_response__assign( res, ro); - } + if (c == CURLE_OK) Ref_of_NativeString_item__assign(res, r); return c; `} # Request Long internal information from the CURL session - fun easy_getinfo_long(opt: CURLInfoLong): nullable CURLInfoResponseLong + fun easy_getinfo_long(opt: CURLInfoLong): nullable Int do - var answ = new CURLInfoResponseLong + var answ = new Ref[Int](0) if not native_getinfo_long(opt, answ).is_ok then return null - return answ + return answ.item end # Internal method used to get Int object information initially knowns as C Long type - private fun native_getinfo_long(opt: CURLInfoLong, res: CURLInfoResponseLong): CURLCode import CURLInfoResponseLong.response= `{ - long *r = NULL; - r = malloc(sizeof(long)); - CURLcode c = curl_easy_getinfo( self, opt, r); - if((c == CURLE_OK) && r != NULL) CURLInfoResponseLong_response__assign( res, *r); - free(r); + private fun native_getinfo_long(opt: CURLInfoLong, res: Ref[Int]): CURLCode + import Ref[Int].item= `{ + long r; + CURLcode c = curl_easy_getinfo( self, opt, &r); + if (c == CURLE_OK) Ref_of_Int_item__assign(res, r); return c; `} # Request Double internal information from the CURL session - fun easy_getinfo_double(opt: CURLInfoDouble): nullable CURLInfoResponseDouble + fun easy_getinfo_double(opt: CURLInfoDouble): nullable Float do - var answ = new CURLInfoResponseDouble + var answ = new Ref[Float](0.0) if not native_getinfo_double(opt, answ).is_ok then return null - return answ + return answ.item end # Internal method used to get Int object information initially knowns as C Double type - private fun native_getinfo_double(opt: CURLInfoDouble, res: CURLInfoResponseDouble): CURLCode import CURLInfoResponseDouble.response= `{ - double *r = NULL; - r = malloc(sizeof(double)); - CURLcode c = curl_easy_getinfo( self, opt, r); - if((c == CURLE_OK) && r != NULL) CURLInfoResponseDouble_response__assign( res, *r); - free(r); + private fun native_getinfo_double(opt: CURLInfoDouble, res: Ref[Float]): CURLCode + import Ref[Float].item= `{ + double r; + CURLcode c = curl_easy_getinfo(self, opt, &r); + if (c == CURLE_OK) Ref_of_Float_item__assign(res, r); return c; `} # Request SList internal information from the CURL session - fun easy_getinfo_slist(opt: CURLInfoSList): nullable CURLInfoResponseArray + fun easy_getinfo_slist(opt: CURLInfoSList): nullable Array[String] do - var answ = new CURLInfoResponseArray + var answ = new Ref[CURLSList](new CURLSList) if not native_getinfo_slist(opt, answ).is_ok then return null - answ.response = answ.prim_response.to_a - answ.prim_response.destroy - return answ + + var native = answ.item + var nity = native.to_a + native.destroy + return nity end # Internal method used to get Array[String] object information initially knowns as C SList type - private fun native_getinfo_slist(opt: CURLInfoSList, res: CURLInfoResponseArray): CURLCode import CURLInfoResponseArray.prim_response= `{ - struct curl_slist* csl = NULL; - CURLcode ce = curl_easy_getinfo( self, opt, &csl); - CURLInfoResponseArray_prim_response__assign(res, csl); - return ce; + private fun native_getinfo_slist(opt: CURLInfoSList, res: Ref[CURLSList]): CURLCode + import Ref[CURLSList].item= `{ + struct curl_slist* csl; + CURLcode c = curl_easy_getinfo(self, opt, &csl); + if (c == CURLE_OK) Ref_of_CURLSList_item__assign(res, csl); + return c; `} # Register delegate to read datas from given buffer - fun register_read_datas_callback(delegate: CCurlCallbacks, datas: String): CURLCode + fun register_read_datas_callback(delegate: NativeCurlCallbacks, datas: String): CURLCode do if datas.length > 0 then return native_register_read_datas_callback(delegate, datas, datas.length) return once new CURLCode.unknown_option end # Internal method used to configure read callback - private fun native_register_read_datas_callback(delegate: CCurlCallbacks, datas: String, size: Int): CURLCode import String.to_cstring `{ + private fun native_register_read_datas_callback(delegate: NativeCurlCallbacks, datas: String, size: Int): CURLCode import String.to_cstring `{ CURLCallbackReadDatas *d = NULL; d = malloc(sizeof(CURLCallbackReadDatas)); d->data = (char*)String_to_cstring(datas); @@ -203,10 +203,10 @@ extern class CCurl `{ CURL * `} `} # Register `delegate` to get callbacks about the CURL transfer - fun register_callback_header(delegate: CCurlCallbacks): CURLCode + fun register_callback_header(delegate: NativeCurlCallbacks): CURLCode import NativeString.native_callback_header `{ CURLcode e; - CCurlCallbacks_incr_ref(delegate); // FIXME deallocated these when download completes? + NativeCurlCallbacks_incr_ref(delegate); // FIXME deallocated these when download completes? e = curl_easy_setopt(self, CURLOPT_HEADERFUNCTION, (curl_write_callback)&NativeString_native_callback_header); if(e != CURLE_OK) return e; @@ -216,10 +216,10 @@ extern class CCurl `{ CURL * `} `} # Register `delegate` to get callbacks about the CURL transfer - fun register_callback_body(delegate: CCurlCallbacks): CURLCode + fun register_callback_body(delegate: NativeCurlCallbacks): CURLCode import NativeString.native_callback_body `{ CURLcode e; - CCurlCallbacks_incr_ref(delegate); + NativeCurlCallbacks_incr_ref(delegate); e = curl_easy_setopt(self, CURLOPT_WRITEFUNCTION, (curl_write_callback)&NativeString_native_callback_body); if(e != CURLE_OK) return e; @@ -229,10 +229,10 @@ extern class CCurl `{ CURL * `} `} # Register `delegate` to get callbacks about the CURL transfer - fun register_callback_stream(delegate: CCurlCallbacks): CURLCode + fun register_callback_stream(delegate: NativeCurlCallbacks): CURLCode import NativeString.native_callback_stream `{ CURLcode e; - CCurlCallbacks_incr_ref(delegate); + NativeCurlCallbacks_incr_ref(delegate); e = curl_easy_setopt(self, CURLOPT_WRITEFUNCTION, (curl_write_callback)&NativeString_native_callback_stream); if(e != CURLE_OK) return e; @@ -242,9 +242,9 @@ extern class CCurl `{ CURL * `} `} # Register `delegate` to get callbacks about the CURL transfer - fun register_callback_read(delegate: CCurlCallbacks): CURLCode + fun register_callback_read(delegate: NativeCurlCallbacks): CURLCode import NativeString.native_callback_stream `{ - CCurlCallbacks_incr_ref(delegate); + NativeCurlCallbacks_incr_ref(delegate); return curl_easy_setopt(self, CURLOPT_READFUNCTION, (curl_write_callback)&nit_curl_callback_read_func); `} @@ -261,7 +261,7 @@ extern class CCurl `{ CURL * `} end # Interface for internal information callbacks methods -interface CCurlCallbacks +interface NativeCurlCallbacks fun header_callback(buffer: String) do end fun body_callback(buffer: String) do end fun stream_callback(buffer: String) do end @@ -342,6 +342,7 @@ redef class Collection[E] assert collectionItemType: self isa Collection[String] else print "Collection item must be strings." end + if is_empty then return new CURLSList var primList = new CURLSList.with_str(self.first) var is_first = true for s in self do @@ -352,34 +353,13 @@ redef class Collection[E] end end -# Array Response type of CCurl.easy_getinfo method -class CURLInfoResponseArray - var response: Array[String] = new Array[String] - private var prim_response: CURLSList = new CURLSList -end - -# Long Response type of CCurl.easy_getinfo method -class CURLInfoResponseLong - var response = 0 -end - -# Double Response type of CCurl.easy_getinfo method -class CURLInfoResponseDouble - var response = 0 -end - -# String Response type of CCurl::easy_getinfo method -class CURLInfoResponseString - var response = "" -end - -# Reproduce Enum of available CURL SList information, used for CCurl.easy_getinfo +# Reproduce Enum of available CURL SList information, used for NativeCurl.easy_getinfo extern class CURLInfoSList `{ CURLINFO `} new ssl_engines `{ return CURLINFO_SSL_ENGINES; `} new cookielist `{ return CURLINFO_COOKIELIST; `} end -# Reproduce Enum of available CURL Long information, used for CCurl.easy_getinfo +# Reproduce Enum of available CURL Long information, used for NativeCurl.easy_getinfo extern class CURLInfoLong `{ CURLINFO `} new response_code `{ return CURLINFO_RESPONSE_CODE; `} new header_size `{ return CURLINFO_HEADER_SIZE; `} @@ -401,7 +381,7 @@ extern class CURLInfoLong `{ CURLINFO `} new rtsp_cseq_self `{ return CURLINFO_RTSP_CSEQ_RECV; `} end -# Reproduce Enum of available CURL Double information, used for CCurl.easy_getinfo +# Reproduce Enum of available CURL Double information, used for NativeCurl.easy_getinfo extern class CURLInfoDouble `{ CURLINFO `} new total_time `{ return CURLINFO_TOTAL_TIME; `} new namelookup_time `{ return CURLINFO_NAMELOOKUP_TIME; `} @@ -418,7 +398,7 @@ extern class CURLInfoDouble `{ CURLINFO `} new content_length_upload `{ return CURLINFO_CONTENT_LENGTH_UPLOAD; `} end -# Reproduce Enum of available CURL Chars information, used for CCurl.easy_getinfo +# Reproduce Enum of available CURL Chars information, used for NativeCurl.easy_getinfo extern class CURLInfoChars `{ CURLINFO `} new content_type `{ return CURLINFO_CONTENT_TYPE; `} new effective_url `{ return CURLINFO_EFFECTIVE_URL; `} @@ -472,7 +452,7 @@ extern class CURLStatusCode `{ int `} fun to_i: Int `{ return self; `} end -# Reproduce Enum of CURL Options usable, used for CCurl.easy_setopt +# Reproduce Enum of CURL Options usable, used for NativeCurl.easy_setopt extern class CURLOption `{ CURLoption `} # Behavior options @@ -717,8 +697,12 @@ extern class CURLOption `{ CURLoption `} # Connection Options -# new `{ return CURLOPT_TIMEOUT; `} -# new `{ return CURLOPT_TIMEOUT_MS; `} + # Set maximum time the request is allowed to take. + new timeout `{ return CURLOPT_TIMEOUT; `} + + # Set maximum time the request is allowed to take (in ms). + new timeout_ms `{ return CURLOPT_TIMEOUT_MS; `} + # new `{ return CURLOPT_LOW_SPEED_LIMIT; `} # new `{ return CURLOPT_LOW_SPEED_TIME; `} # new `{ return CURLOPT_MAX_SEND_SPEED_LARGE; `}