make chunked encoding request possible
This commit is contained in:
parent
36b6440f7e
commit
81dfb37b25
1 changed files with 14 additions and 4 deletions
|
@ -46,6 +46,7 @@ pub const Server = struct {
|
||||||
// method is chosen by the client.
|
// method is chosen by the client.
|
||||||
var headers_finished = false;
|
var headers_finished = false;
|
||||||
var content_length: usize = 0;
|
var content_length: usize = 0;
|
||||||
|
var transfer_encoding_chunked = false;
|
||||||
var header_end: usize = 0;
|
var header_end: usize = 0;
|
||||||
var header_string: []const u8 = undefined;
|
var header_string: []const u8 = undefined;
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -67,14 +68,23 @@ pub const Server = struct {
|
||||||
// If not, exit loop. A Request body would not be accepted.
|
// If not, exit loop. A Request body would not be accepted.
|
||||||
if (req.header("Content-Length")) |length| {
|
if (req.header("Content-Length")) |length| {
|
||||||
content_length = try std.fmt.parseUnsigned(u8, length, 0);
|
content_length = try std.fmt.parseUnsigned(u8, length, 0);
|
||||||
|
} else if (req.header("Transfer-Encoding")) |value| {
|
||||||
|
if (!eql(u8, value, "chunked")) break else transfer_encoding_chunked = true;
|
||||||
} else break;
|
} else break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if (!transfer_encoding_chunked) {
|
||||||
// read body. Check length and add 4 because this is the length of "\r\n\r\n"
|
// read body. Check length and add 4 because this is the length of "\r\n\r\n"
|
||||||
if (buffer.items.len - header_end >= content_length + 4) {
|
if (buffer.items.len - header_end >= content_length + 4) {
|
||||||
req.body = buffer.items[header_end .. header_end + content_length + 4];
|
req.body = buffer.items[header_end .. header_end + content_length + 4];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if (std.mem.endsWith(u8, buffer.items, "0\r\n\r\n")) {
|
||||||
|
req.body = buffer.items;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
defer allocator.free(req.headers);
|
defer allocator.free(req.headers);
|
||||||
|
|
Loading…
Add table
Reference in a new issue