4d29ee3e2aaf213547ff01037e031c6734604171
[nit.git] / lib / nitcorn / http_request_buffer.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Http request parsing for buffered inputs.
16 module http_request_buffer
17
18 intrude import libevent
19
20 # Connection rebuilding HTTP requests
21 #
22 # Subclass should refine `read_full_request` and avoid `read_callback`.
23 class HTTPConnection
24 super Connection
25
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
33
34 redef fun read_callback_native(cstr, len)
35 # FIXME will not work if the header/body delimiter falls between two watermarks windows.
36 do
37 # is this the start of a request?
38 var str = cstr.to_s_with_length(len)
39 if not in_request then parse_start
40
41 var body: String
42 # parsing header
43 if in_header then
44 body = parse_header(str)
45 else
46 body = str
47 end
48 # parsing body
49 if in_body then parse_body(body)
50 end
51
52
53 # Prepare for a new request
54 private fun parse_start do
55 in_request = true
56 # reset values
57 current_header.clear
58 current_body.clear
59 current_length = 0
60 content_length = 0
61 # next step is to find the header part
62 in_header = true
63 in_body = false
64 end
65
66 # We are receiving the header of a request
67 #
68 # Return parsed body found in header window
69 private fun parse_header(str: String): String do
70 # split in CRLF
71 var parts = str.split("\r\n\r\n")
72 # first part go in the header
73 current_header.add parts.shift
74
75 # if there is more part we are done with headers
76 if not parts.is_empty then
77 # get content-length
78 parse_content_length current_header.join
79 # next step if to parse body
80 in_header = false
81 in_body = true
82 # return rest of the body
83 return parts.join("\r\n")
84 end
85 return ""
86 end
87
88 # Extract and set `content_length` from header.
89 private fun parse_content_length(head: String) do
90 var hlines = head.split("\r\n")
91 for hline in hlines do
92 var hparts = hline.split(": ")
93 if hparts.first == "Content-Length" then
94 content_length = hparts[1].to_i
95 end
96 end
97 end
98
99 # We are receiving body parts.
100 private fun parse_body(str: String) do
101 current_length += str.length
102 current_body.add str
103 if current_length >= content_length then
104 parse_end
105 end
106 end
107
108 # We have reached the end of the body
109 private fun parse_end do
110 var res = new FlatBuffer.with_capacity(content_length)
111 for ch in current_header do res.append ch.write_to_string
112 res.append "\r\n\r\n"
113 for cb in current_body do res.append cb.write_to_string
114 read_callback(res.write_to_string)
115 in_request = false
116 end
117 end