@@ -51,6 +51,12 @@ pub extern "c" fn HttpRequestInternal::on_complete(
5151#owned (self)
5252extern " c " fn HttpRequestInternal ::body(self : HttpRequestInternal ) -> Bytes = " req_body "
5353
54+ ///|
55+ #owned (self)
56+ extern " c " fn HttpRequestInternal ::req_body_len(
57+ self : HttpRequestInternal ,
58+ ) -> Int = " req_body_len "
59+
5460///|
5561#owned (self, body)
5662extern " c " fn HttpResponseInternal ::end(
@@ -127,21 +133,19 @@ fn handle_request_native(
127133 // res.status(200)
128134 // res.end(to_cstr("{\"status\":\"ok\"}"))
129135
130- let headers = from_cstr(req.headers())
131- println(headers)
132- let string_headers = {}
133- // guard (req.headers()) is Ok(Object(headers)) else {
134- // res.status(400)
135- // res.end(to_cstr("Invalid headers"))
136- // return
137- // }
138-
139- // 批量转换 headers,减少单个处理的开销
140- // headers.each(fn(key, value) {
141- // if value is String(v) {
142- // string_headers.set(key, v)
143- // }
144- // })
136+ let headers_str = from_cstr(req.headers())
137+ let req_headers = headers_str
138+ .split("\n")
139+ .map(fn(pair) {
140+ if pair.length() > 0 && pair.split(": ").to_array() is [key, value] {
141+ (key.to_string(), value.to_string())
142+ } else {
143+ ("", "")
144+ }
145+ })
146+ .filter(fn(pair) { pair.0.length() > 0 })
147+ .to_array()
148+ |> Map ::from_array
145149 let (params, handler) = match mocket.find_route(http_method, path) {
146150 Some ((h, p)) => (p, h)
147151 _ => {
@@ -151,43 +155,19 @@ fn handle_request_native(
151155 }
152156 }
153157 let event = {
154- req: { http_method, url, body: Empty , headers: string_headers },
158+ req: { http_method, url, body: Empty , headers: req_headers },
155159 res: { status_code: 200, headers: {} },
156160 params,
157161 }
158162 if http_method == "POST " {
159- let body_bytes = req.body()
160- let content_type = Some ("text/plain") // req.headers().get("Content-Type")
161- let body = match content_type {
162- Some ("application/json") => {
163- let json = @encoding.decoder(UTF8 ).decode(body_bytes) catch {
164- _ => {
165- res.status(400)
166- res.end(to_cstr("Invalid JSON charset"))
167- return
168- }
169- }
170- Json (
171- @json.parse(json) catch {
172- _ => {
173- res.status(400)
174- res.end(to_cstr("Invalid JSON "))
175- return
176- }
177- },
178- )
163+ let req_body_len = req.req_body_len()
164+ let body_bytes = req.body()[0:req_body_len]
165+ let body = read_body(req_headers, body_bytes) catch {
166+ _ => {
167+ res.status(400)
168+ res.end(to_cstr("Invalid body"))
169+ return
179170 }
180- Some ("text/plain" | "text/html") =>
181- Text (
182- @encoding.decoder(UTF8 ).decode(body_bytes) catch {
183- _ => {
184- res.status(400)
185- res.end(to_cstr("Invalid text charset"))
186- return
187- }
188- },
189- )
190- _ => Bytes (body_bytes)
191171 }
192172 event.req.body = body
193173 }
@@ -212,12 +192,12 @@ fn handle_request_native(
212192 res.set_header(to_cstr(key), to_cstr(value))
213193 })
214194 if body is Bytes (bytes) {
215- res.end_bytes(bytes)
195+ res.end_bytes(bytes.to_bytes() )
216196 } else {
217197 res.end(
218198 match body {
219- HTML (s) => to_cstr(s)
220- Text (s) => to_cstr(s)
199+ HTML (s) => to_cstr(s.to_string() )
200+ Text (s) => to_cstr(s.to_string() )
221201 Json (j) => to_cstr(j.stringify())
222202 _ => to_cstr("")
223203 },
@@ -227,8 +207,8 @@ fn handle_request_native(
227207}
228208
229209///|
230- fn to_cstr(s : String ) -> @native.CStr {
231- let bytes = @encoding.encode(UTF8 , s)
210+ fn [ T : Show ] to_cstr(s : T ) -> @native.CStr {
211+ let bytes = @encoding.encode(UTF8 , s.to_string() )
232212 let utf8_ptr = @native.unsafe_coerce(bytes)
233213 utf8_ptr
234214}
0 commit comments