Function receiveHead [src]
The header bytes reference the read buffer that Server was initialized with
and remain alive until the next call to receiveHead.
Prototype
pub fn receiveHead(s: *Server) ReceiveHeadError!Request
Parameters
s: *Server
Possible Errors
The client sent 0 bytes of headers before closing the stream. In other words, a keep-alive connection was finally closed.
Client sent headers that did not conform to the HTTP protocol.
Client sent too many bytes of HTTP headers. The HTTP specification suggests to respond with a 431 status code before closing the connection.
A low level I/O error occurred trying to read the headers.
Partial HTTP request was received but the connection was closed before fully receiving the headers.
Source
pub fn receiveHead(s: *Server) ReceiveHeadError!Request {
assert(s.state == .ready);
s.state = .received_head;
errdefer s.state = .receiving_head;
// In case of a reused connection, move the next request's bytes to the
// beginning of the buffer.
if (s.next_request_start > 0) {
if (s.read_buffer_len > s.next_request_start) {
rebase(s, 0);
} else {
s.read_buffer_len = 0;
}
}
var hp: http.HeadParser = .{};
if (s.read_buffer_len > 0) {
const bytes = s.read_buffer[0..s.read_buffer_len];
const end = hp.feed(bytes);
if (hp.state == .finished)
return finishReceivingHead(s, end);
}
while (true) {
const buf = s.read_buffer[s.read_buffer_len..];
if (buf.len == 0)
return error.HttpHeadersOversize;
const read_n = s.connection.stream.read(buf) catch
return error.HttpHeadersUnreadable;
if (read_n == 0) {
if (s.read_buffer_len > 0) {
return error.HttpRequestTruncated;
} else {
return error.HttpConnectionClosing;
}
}
s.read_buffer_len += read_n;
const bytes = buf[0..read_n];
const end = hp.feed(bytes);
if (hp.state == .finished)
return finishReceivingHead(s, s.read_buffer_len - bytes.len + end);
}
}