lib/curl: remove useless types
[nit.git] / lib / curl / curl.nit
index 6b6b34c..dbfc157 100644 (file)
@@ -21,11 +21,10 @@ import curl_c
 
 # Top level of Curl
 class Curl
-       protected var prim_curl: CCurl
+       protected var prim_curl = new CCurl.easy_init
 
        init
        do
-               self.prim_curl = new CCurl.easy_init
                assert curlInstance:self.prim_curl.is_init else
                        print "Curl must be instancied to be used"
                end
@@ -34,29 +33,6 @@ class Curl
        # Check for correct initialization
        fun is_ok: Bool do return self.prim_curl.is_init
 
-       # Get an HTTP Request object to perform your own
-       fun http_request(url: String): nullable CurlRequest
-       do
-               var err: CURLCode
-               err = self.prim_curl.easy_setopt(new CURLOption.follow_location, 1)
-               if not err.is_ok then return null
-
-               err = self.prim_curl.easy_setopt(new CURLOption.url, url)
-               if not err.is_ok then return null
-
-               return new CurlHTTPRequest(url, self)
-       end
-
-  # Get a MAIL Request Object
-  fun mail_request: nullable CurlMailRequest
-  do
-    var err: CURLCode
-    err = self.prim_curl.easy_setopt(new CURLOption.follow_location, 1)
-    if not err.is_ok then return null
-
-    return new CurlMailRequest(self)
-  end
-
        # Release Curl instance
        fun destroy do self.prim_curl.easy_clean
 end
@@ -64,18 +40,18 @@ end
 # CURL Request
 class CurlRequest
 
-       var verbose: Bool writable = false
+       var verbose: Bool = false is writable
        private var curl: nullable Curl = null
 
        # Launch request method
        fun execute: CurlResponse is abstract
 
        # Intern perform method, lowest level of request launching
-       private fun perform: nullable CurlResponse
+       private fun perform: nullable CurlResponseFailed
        do
                if not self.curl.is_ok then return answer_failure(0, "Curl instance is not correctly initialized")
 
-               var err: CURLCode
+               var err
 
                err = self.curl.prim_curl.easy_setopt(new CURLOption.verbose, self.verbose)
                if not err.is_ok then return answer_failure(err.to_i, err.to_s)
@@ -87,7 +63,7 @@ class CurlRequest
        end
 
        # Intern method with return a failed answer with given code and message
-       private fun answer_failure(error_code: Int, error_msg: String): CurlResponse
+       private fun answer_failure(error_code: Int, error_msg: String): CurlResponseFailed
        do
                return new CurlResponseFailed(error_code, error_msg)
        end
@@ -96,45 +72,56 @@ end
 # CURL HTTP Request
 class CurlHTTPRequest
        super CurlRequest
-  super CCurlCallbacks
-  super CurlCallbacksRegisterIntern
+       super CCurlCallbacks
+       super CurlCallbacksRegisterIntern
 
-  var url: String
-       var datas: nullable HeaderMap writable = null
-       var headers: nullable HeaderMap writable = null
+       var url: String
+       var datas: nullable HeaderMap = null is writable
+       var headers: nullable HeaderMap = null is writable
 
-  init (url: String, curl: nullable Curl)
-  do
-    self.url = url
-    self.curl = curl
-  end
+       # Set the user agent for all following HTTP requests
+       fun user_agent=(name: String)
+       do
+               curl.prim_curl.easy_setopt(new CURLOption.user_agent, name)
+       end
+
+       init (url: String, curl: nullable Curl) is old_style_init do
+               self.url = url
+               self.curl = curl
+       end
 
        # Execute HTTP request with settings configured through attribute
-       redef fun execute: CurlResponse
+       redef fun execute
        do
                if not self.curl.is_ok then return answer_failure(0, "Curl instance is not correctly initialized")
 
-               var success_response: CurlResponseSuccess = new CurlResponseSuccess
-
+               var success_response = new CurlResponseSuccess
                var callback_receiver: CurlCallbacks = success_response
                if self.delegate != null then callback_receiver = self.delegate.as(not null)
 
-               var err: CURLCode
-    # Callbacks
+               var err
+
+               err = self.curl.prim_curl.easy_setopt(new CURLOption.follow_location, 1)
+               if not err.is_ok then return answer_failure(err.to_i, err.to_s)
+
+               err = self.curl.prim_curl.easy_setopt(new CURLOption.url, url)
+               if not err.is_ok then return answer_failure(err.to_i, err.to_s)
+
+               # Callbacks
                err = self.curl.prim_curl.register_callback(callback_receiver, new CURLCallbackType.header)
                if not err.is_ok then return answer_failure(err.to_i, err.to_s)
 
                err = self.curl.prim_curl.register_callback(callback_receiver, new CURLCallbackType.body)
                if not err.is_ok then return answer_failure(err.to_i, err.to_s)
 
-    # HTTP Header
+               # HTTP Header
                if self.headers != null then
                        var headers_joined = self.headers.join_pairs(": ")
                        err = self.curl.prim_curl.easy_setopt(new CURLOption.httpheader, headers_joined.to_curlslist)
                        if not err.is_ok then return answer_failure(err.to_i, err.to_s)
                end
 
-    # Datas
+               # Datas
                if self.datas != null then
                        var postdatas = self.datas.to_url_encoded(self.curl.prim_curl)
                        err = self.curl.prim_curl.easy_setopt(new CURLOption.postfields, postdatas)
@@ -153,19 +140,26 @@ class CurlHTTPRequest
        # Download to file given resource
        fun download_to_file(output_file_name: nullable String): CurlResponse
        do
-               var success_response: CurlFileResponseSuccess = new CurlFileResponseSuccess
+               var success_response = new CurlFileResponseSuccess
 
                var callback_receiver: CurlCallbacks = success_response
                if self.delegate != null then callback_receiver = self.delegate.as(not null)
 
-               var err: CURLCode
+               var err
+
+               err = self.curl.prim_curl.easy_setopt(new CURLOption.follow_location, 1)
+               if not err.is_ok then return answer_failure(err.to_i, err.to_s)
+
+               err = self.curl.prim_curl.easy_setopt(new CURLOption.url, url)
+               if not err.is_ok then return answer_failure(err.to_i, err.to_s)
+
                err = self.curl.prim_curl.register_callback(callback_receiver, new CURLCallbackType.header)
                if not err.is_ok then return answer_failure(err.to_i, err.to_s)
 
                err = self.curl.prim_curl.register_callback(callback_receiver, new CURLCallbackType.stream)
                if not err.is_ok then return answer_failure(err.to_i, err.to_s)
 
-               var opt_name:nullable String
+               var opt_name
                if not output_file_name == null then
                        opt_name = output_file_name
                else if not self.url.substring(self.url.length-1, self.url.length) == "/" then
@@ -203,24 +197,22 @@ end
 
 # CURL Mail Request
 class CurlMailRequest
-  super CurlRequest
-  super CCurlCallbacks
-
-  var headers: nullable HeaderMap writable = null
-       var headers_body: nullable HeaderMap writable = null
-       var from: nullable String writable = null
-       var to: nullable Array[String] writable = null
-       var cc: nullable Array[String] writable = null
-       var bcc: nullable Array[String] writable = null
-       var subject: nullable String writable = ""
-       var body: nullable String writable = ""
-       private var supported_outgoing_protocol: Array[String]
-
-  init (curl: nullable Curl)
-  do
-    self.curl = curl
-               self.supported_outgoing_protocol = once ["smtp", "smtps"]
-  end
+       super CurlRequest
+       super CCurlCallbacks
+
+       var headers: nullable HeaderMap = null is writable
+       var headers_body: nullable HeaderMap = null is writable
+       var from: nullable String = null is writable
+       var to: nullable Array[String] = null is writable
+       var cc: nullable Array[String] = null is writable
+       var bcc: nullable Array[String] = null is writable
+       var subject: nullable String = "" is writable
+       var body: nullable String = "" is writable
+       private var supported_outgoing_protocol: Array[String] = ["smtp", "smtps"]
+
+       init (curl: nullable Curl) is old_style_init do
+               self.curl = curl
+       end
 
        # Helper method to add conventional space while building entire mail
        private fun add_conventional_space(str: String):String do return "{str}\n" end
@@ -232,12 +224,12 @@ class CurlMailRequest
                return "{str}{att}\n"
        end
 
-  # Helper method to add entire list of pairs to mail content
-  private fun add_pairs_to_content(content: String, pairs: HeaderMap):String
-  do
-    for h_key, h_val in pairs do content = add_pair_to_content(content, h_key, h_val)
-    return content
-  end
+       # Helper method to add entire list of pairs to mail content
+       private fun add_pairs_to_content(content: String, pairs: HeaderMap):String
+       do
+               for h_key, h_val in pairs do content = add_pair_to_content(content, h_key, h_val)
+               return content
+       end
 
        # Check for host and protocol availability
        private fun is_supported_outgoing_protocol(host: String):CURLCode
@@ -248,14 +240,14 @@ class CurlMailRequest
        end
 
        # Configure server host and user credentials if needed.
-       fun set_outgoing_server(host: String, user: nullable String, pwd: nullable String):nullable CurlResponse
+       fun set_outgoing_server(host: String, user: nullable String, pwd: nullable String): nullable CurlResponseFailed
        do
                # Check Curl initialisation
                if not self.curl.is_ok then return answer_failure(0, "Curl instance is not correctly initialized")
 
-               var err: CURLCode
+               var err
 
-    # Host & Protocol
+               # Host & Protocol
                err = is_supported_outgoing_protocol(host)
                if not err.is_ok then return answer_failure(err.to_i, err.to_s)
                err = self.curl.prim_curl.easy_setopt(new CURLOption.url, host)
@@ -273,16 +265,15 @@ class CurlMailRequest
        end
 
        # Execute Mail request with settings configured through attribute
-       redef fun execute: CurlResponse
+       redef fun execute
        do
                if not self.curl.is_ok then return answer_failure(0, "Curl instance is not correctly initialized")
 
-               var success_response: CurlMailResponseSuccess = new CurlMailResponseSuccess
-               var err: CURLCode
-    var content = ""
-    # Headers
+               var success_response = new CurlMailResponseSuccess
+               var content = ""
+               # Headers
                if self.headers != null then
-      content = add_pairs_to_content(content, self.headers.as(not null))
+                       content = add_pairs_to_content(content, self.headers.as(not null))
                end
 
                # Recipients
@@ -298,10 +289,16 @@ class CurlMailRequest
                if self.bcc != null and self.bcc.length > 0 then g_rec.append(self.bcc.as(not null))
 
                if g_rec.length < 1 then return answer_failure(0, "The mail recipients can not be empty")
+
+               var err
+
+               err = self.curl.prim_curl.easy_setopt(new CURLOption.follow_location, 1)
+               if not err.is_ok then return answer_failure(err.to_i, err.to_s)
+
                err = self.curl.prim_curl.easy_setopt(new CURLOption.mail_rcpt, g_rec.to_curlslist)
                if not err.is_ok then return answer_failure(err.to_i, err.to_s)
 
-    # From
+               # From
                if not self.from == null then
                        content = add_pair_to_content(content, "From:", self.from)
                        err = self.curl.prim_curl.easy_setopt(new CURLOption.mail_from, self.from.as(not null))
@@ -311,9 +308,9 @@ class CurlMailRequest
                # Subject
                content = add_pair_to_content(content, "Subject:", self.subject)
 
-    # Headers body
+               # Headers body
                if self.headers_body != null then
-      content = add_pairs_to_content(content, self.headers_body.as(not null))
+                       content = add_pairs_to_content(content, self.headers_body.as(not null))
                end
 
                # Body
@@ -339,7 +336,7 @@ end
 
 # Callbacks attributes
 abstract class CurlCallbacksRegisterIntern
-  var delegate: nullable CurlCallbacks writable = null
+       var delegate: nullable CurlCallbacks = null is writable
 end
 
 # Abstract Curl request response
@@ -352,12 +349,6 @@ class CurlResponseFailed
 
        var error_code: Int
        var error_msg: String
-
-       init (err_code: Int, err_msg: String)
-       do
-               self.error_code = err_code
-               self.error_msg = err_msg
-       end
 end
 
 # Success Abstract Response Success Class
@@ -365,10 +356,10 @@ abstract class CurlResponseSuccessIntern
        super CurlCallbacks
        super CurlResponse
 
-       var headers: HashMap[String, String] = new HashMap[String, String]
+       var headers = new HashMap[String, String]
 
        # Receive headers from request due to headers callback registering
-       redef fun header_callback(line: String)
+       redef fun header_callback(line)
        do
                var splitted = line.split_with(':')
                if splitted.length > 1 then
@@ -382,33 +373,32 @@ end
 class CurlResponseSuccess
        super CurlResponseSuccessIntern
 
-       var body_str: String = ""
-       var status_code: Int = 0
+       var body_str = ""
+       var status_code = 0
 
        # Receive body from request due to body callback registering
-       redef fun body_callback(line: String)
-       do
+       redef fun body_callback(line) do
                self.body_str = "{self.body_str}{line}"
        end
 end
 
 # Success Response Class of mail request
 class CurlMailResponseSuccess
-  super CurlResponseSuccessIntern
+       super CurlResponseSuccessIntern
 end
 
 # Success Response Class of a downloaded File
 class CurlFileResponseSuccess
        super CurlResponseSuccessIntern
 
-       var status_code: Int = 0
-       var speed_download: Int = 0
-       var size_download: Int = 0
-       var total_time: Int = 0
+       var status_code = 0
+       var speed_download = 0
+       var size_download = 0
+       var total_time = 0
        private var i_file: nullable OFile = null
 
        # Receive bytes stream from request due to stream callback registering
-       redef fun stream_callback(buffer: String, size: Int, count: Int)
+       redef fun stream_callback(buffer, size, count)
        do
                self.i_file.write(buffer, size, count)
        end
@@ -429,16 +419,6 @@ class HeaderMap
                return res
        end
 
-       fun iterate !each(k, v: String)
-       do
-               var i = arr.iterator
-               while i.is_ok do
-                       var item = i.item
-                       each(item.first, item.second)
-                       i.next
-               end
-       end
-
        fun iterator: MapIterator[String, String] do return new HeaderMapIterator(self)
 
        # Convert Self to a single string used to post http fields
@@ -447,7 +427,7 @@ class HeaderMap
                assert curlNotInitialized: curl.is_init else
                        print "to_url_encoded required a valid instance of CCurl Object."
                end
-               var str: String = ""
+               var str = ""
                var length = self.length
                var i = 0
                for k, v in self do
@@ -478,7 +458,7 @@ class HeaderMapIterator
        super MapIterator[String, String]
 
        private var iterator: Iterator[Couple[String, String]]
-       init(map: HeaderMap) do self.iterator = map.arr.iterator
+       init(map: HeaderMap) is old_style_init do self.iterator = map.arr.iterator
 
        redef fun is_ok do return self.iterator.is_ok
        redef fun next do self.iterator.next