1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Http request parsing for buffered inputs.
16 module http_request_buffer
18 intrude import libevent
20 # Connection rebuilding HTTP requests
22 # Subclass should refine `read_full_request` and avoid `read_callback`.
26 private var in_request
= false
27 private var in_header
= false
28 private var in_body
= false
29 private var current_header
= new Array[Writable]
30 private var current_body
= new Array[Writable]
31 private var content_length
= 0
32 private var current_length
= 0
34 # FIXME will not work if the header/body delimiter falls between two watermarks windows.
35 redef fun read_callback
(str
)
37 # is this the start of a request?
38 if not in_request
then parse_start
43 body
= parse_header
(str
)
49 if in_body
then parse_body
(body
)
52 # Callback when a full HTTP request is received
53 fun read_http_request
(str
: String) do end
55 # Prepare for a new request
56 private fun parse_start
do
63 # next step is to find the header part
68 # We are receiving the header of a request
70 # Return parsed body found in header window
71 private fun parse_header
(str
: String): String do
73 var parts
= str
.split
("\r\n\r\n")
74 # first part go in the header
75 current_header
.add parts
.shift
77 # if there is more part we are done with headers
78 if not parts
.is_empty
then
80 parse_content_length current_header
.join
81 # next step if to parse body
84 # return rest of the body
85 return parts
.join
("\r\n")
90 # Extract and set `content_length` from header.
91 private fun parse_content_length
(head
: String) do
92 var hlines
= head
.split
("\r\n")
93 for hline
in hlines
do
94 var hparts
= hline
.split
(": ")
95 if hparts
.first
== "Content-Length" then
96 content_length
= hparts
[1].to_i
101 # We are receiving body parts.
102 private fun parse_body
(str
: String) do
103 current_length
+= str
.byte_length
105 if current_length
>= content_length
then
110 # We have reached the end of the body
111 private fun parse_end
do
112 var res
= new FlatBuffer.with_capacity
(content_length
)
113 for ch
in current_header
do res
.append ch
.write_to_string
114 res
.append
"\r\n\r\n"
115 for cb
in current_body
do res
.append cb
.write_to_string
116 read_http_request res
.to_s