X-Git-Url: http://nitlanguage.org diff --git a/lib/nitcorn/http_request_buffer.nit b/lib/nitcorn/http_request_buffer.nit index 6d50b5f..84fb8ca 100644 --- a/lib/nitcorn/http_request_buffer.nit +++ b/lib/nitcorn/http_request_buffer.nit @@ -12,12 +12,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -# Http request parsing for bufferized inputs. +# Http request parsing for buffered inputs. module http_request_buffer intrude import libevent -redef class Connection +# Connection rebuilding HTTP requests +# +# Subclass should refine `read_full_request` and avoid `read_callback`. +class HTTPConnection + super Connection private var in_request = false private var in_header = false @@ -27,14 +31,12 @@ redef class Connection private var content_length = 0 private var current_length = 0 - # FIXME will not work if the header/body delimiter fall between two watermarks windows. - redef fun read_callback_native(cstr, len) + # FIXME will not work if the header/body delimiter falls between two watermarks windows. + redef fun read_callback(str) do # is this the start of a request? - if not in_request then - parse_start - end - var str = cstr.to_s_with_length(len) + if not in_request then parse_start + var body: String # parsing header if in_header then @@ -42,14 +44,15 @@ redef class Connection else body = str end + # parsing body - if in_body then - parse_body(body) - end + if in_body then parse_body(body) end + # Callback when a full HTTP request is received + fun read_http_request(str: String) do end - # We have a new request entering + # Prepare for a new request private fun parse_start do in_request = true # reset values @@ -64,7 +67,7 @@ redef class Connection # We are receiving the header of a request # - # Return parsed body foud in header window + # Return parsed body found in header window private fun parse_header(str: String): String do # split in CRLF var parts = str.split("\r\n\r\n") @@ -97,7 +100,7 @@ redef class Connection # We are receiving body parts. private fun parse_body(str: String) do - current_length += str.length + current_length += str.byte_length current_body.add str if current_length >= content_length then parse_end @@ -110,7 +113,7 @@ redef class Connection for ch in current_header do res.append ch.write_to_string res.append "\r\n\r\n" for cb in current_body do res.append cb.write_to_string - read_callback(res.write_to_string) + read_http_request res.to_s in_request = false end end