rename `NativeString` to `CString`
[nit.git] / lib / curl / native_curl.nit
index a474674..f802e75 100644 (file)
@@ -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 <curl/curl.h>
@@ -45,7 +45,7 @@ in "C body" `{
        }
 `}
 
-redef extern class NativeString
+redef extern class CString
        private fun native_callback_header(size, count: Int, target: NativeCurlCallbacks): Int
        do
                target.header_callback to_s_with_length(size*count)
@@ -113,76 +113,76 @@ extern class NativeCurl `{ 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[CString]("".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[CString]): CURLCode
+       import Ref[CString].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_CString_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
@@ -204,11 +204,11 @@ extern class NativeCurl `{ CURL * `}
 
        # Register `delegate` to get callbacks about the CURL transfer
        fun register_callback_header(delegate: NativeCurlCallbacks): CURLCode
-       import NativeString.native_callback_header `{
+       import CString.native_callback_header `{
                CURLcode e;
                NativeCurlCallbacks_incr_ref(delegate); // FIXME deallocated these when download completes?
 
-               e = curl_easy_setopt(self, CURLOPT_HEADERFUNCTION, (curl_write_callback)&NativeString_native_callback_header);
+               e = curl_easy_setopt(self, CURLOPT_HEADERFUNCTION, (curl_write_callback)&CString_native_callback_header);
                if(e != CURLE_OK) return e;
 
                e = curl_easy_setopt(self, CURLOPT_WRITEHEADER, delegate);
@@ -217,11 +217,11 @@ extern class NativeCurl `{ CURL * `}
 
        # Register `delegate` to get callbacks about the CURL transfer
        fun register_callback_body(delegate: NativeCurlCallbacks): CURLCode
-       import NativeString.native_callback_body `{
+       import CString.native_callback_body `{
                CURLcode e;
                NativeCurlCallbacks_incr_ref(delegate);
 
-               e = curl_easy_setopt(self, CURLOPT_WRITEFUNCTION, (curl_write_callback)&NativeString_native_callback_body);
+               e = curl_easy_setopt(self, CURLOPT_WRITEFUNCTION, (curl_write_callback)&CString_native_callback_body);
                if(e != CURLE_OK) return e;
 
                e = curl_easy_setopt(self, CURLOPT_WRITEDATA, delegate);
@@ -230,11 +230,11 @@ extern class NativeCurl `{ CURL * `}
 
        # Register `delegate` to get callbacks about the CURL transfer
        fun register_callback_stream(delegate: NativeCurlCallbacks): CURLCode
-       import NativeString.native_callback_stream `{
+       import CString.native_callback_stream `{
                CURLcode e;
                NativeCurlCallbacks_incr_ref(delegate);
 
-               e = curl_easy_setopt(self, CURLOPT_WRITEFUNCTION, (curl_write_callback)&NativeString_native_callback_stream);
+               e = curl_easy_setopt(self, CURLOPT_WRITEFUNCTION, (curl_write_callback)&CString_native_callback_stream);
                if(e != CURLE_OK) return e;
 
                e = curl_easy_setopt(self, CURLOPT_WRITEDATA, delegate);
@@ -243,18 +243,18 @@ extern class NativeCurl `{ CURL * `}
 
        # Register `delegate` to get callbacks about the CURL transfer
        fun register_callback_read(delegate: NativeCurlCallbacks): CURLCode
-       import NativeString.native_callback_stream `{
+       import CString.native_callback_stream `{
                NativeCurlCallbacks_incr_ref(delegate);
 
                return curl_easy_setopt(self, CURLOPT_READFUNCTION, (curl_write_callback)&nit_curl_callback_read_func);
        `}
 
        # Convert given string to URL encoded string
-       fun escape(url: String): String import String.to_cstring, NativeString.to_s_with_copy `{
+       fun escape(url: String): String import String.to_cstring, CString.to_s_with_copy `{
                char *orig_url, *encoded_url = NULL;
                orig_url = String_to_cstring(url);
                encoded_url = curl_easy_escape( self, orig_url, strlen(orig_url));
-               String b_url = NativeString_to_s_with_copy(encoded_url);
+               String b_url = CString_to_s_with_copy(encoded_url);
                curl_free(encoded_url);
                return b_url;
        `}
@@ -279,9 +279,9 @@ extern class CURLCode `{ CURLcode `}
        fun is_valid_protocol: Bool `{ return self == CURLE_UNSUPPORTED_PROTOCOL; `}
        fun is_valid_init: Bool `{ return self == CURLE_FAILED_INIT; `}
        fun to_i: Int do return code end
-       redef fun to_s import NativeString.to_s_with_copy `{
+       redef fun to_s import CString.to_s_with_copy `{
                char *c = (char*)curl_easy_strerror(self);
-               return NativeString_to_s_with_copy(c);
+               return CString_to_s_with_copy(c);
        `}
 end
 
@@ -313,7 +313,7 @@ extern class CURLSList `{ struct curl_slist * `}
        private fun native_next_reachable(c: CURLSList): Bool `{ return (c != NULL && c->next != NULL); `}
 
        # Internal method to get current data
-       private fun native_data(c: CURLSList): String import NativeString.to_s `{ return NativeString_to_s(c->data); `}
+       private fun native_data(c: CURLSList): String import CString.to_s `{ return CString_to_s(c->data); `}
 
        # Internal method to get next element
        private fun native_next(c: CURLSList): CURLSList `{ return c->next; `}
@@ -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,27 +353,6 @@ redef class Collection[E]
        end
 end
 
-# Array Response type of NativeCurl.easy_getinfo method
-class CURLInfoResponseArray
-       var response: Array[String] = new Array[String]
-       private var prim_response: CURLSList = new CURLSList
-end
-
-# Long Response type of NativeCurl.easy_getinfo method
-class CURLInfoResponseLong
-       var response = 0
-end
-
-# Double Response type of NativeCurl.easy_getinfo method
-class CURLInfoResponseDouble
-       var response = 0
-end
-
-# String Response type of NativeCurl::easy_getinfo method
-class CURLInfoResponseString
-       var response = ""
-end
-
 # Reproduce Enum of available CURL SList information, used for NativeCurl.easy_getinfo
 extern class CURLInfoSList `{ CURLINFO `}
        new ssl_engines `{ return CURLINFO_SSL_ENGINES; `}
@@ -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; `}