make log lines more uniform

Ensure there is something logged for the request if reading it actually failed.
Instead just write an empty quoted string.
This commit is contained in:
Johann150 2021-03-06 23:25:21 +01:00
parent c6b34055e9
commit d4324233c7
No known key found for this signature in database
GPG key ID: 9EE6577A2A06F8F1

View file

@ -288,22 +288,31 @@ impl RequestHandle {
let mut len = 0; let mut len = 0;
// Read until CRLF, end-of-stream, or there's no buffer space left. // Read until CRLF, end-of-stream, or there's no buffer space left.
loop { //
let bytes_read = self // Since neither CR nor LF can be part of a URI according to
.stream // ISOC-RFC 3986, we could use BufRead::read_line here, but that does
.read(buf) // not allow us to cap the number of read bytes at 1024+2.
.await let result = loop {
.or(Err((59, "Request ended unexpectedly")))?; let bytes_read = if let Ok(read) = self.stream.read(buf).await {
read
} else {
break Err((59, "Request ended unexpectedly"));
};
len += bytes_read; len += bytes_read;
if request[..len].ends_with(b"\r\n") { if request[..len].ends_with(b"\r\n") {
break; break Ok(());
} else if bytes_read == 0 { } else if bytes_read == 0 {
return Err((59, "Request ended unexpectedly")); break Err((59, "Request ended unexpectedly"));
} }
buf = &mut request[len..]; buf = &mut request[len..];
} }
let request = .and_then(|()| std::str::from_utf8(&request[..len - 2]).or(Err((59, "Non-UTF-8 request"))));
std::str::from_utf8(&request[..len - 2]).or(Err((59, "Non-UTF-8 request")))?;
let request = result.map_err(|e| {
// write empty request to log line for uniformity
write!(self.log_line, " \"\"").unwrap();
e
})?;
// log literal request (might be different from or not an actual URL) // log literal request (might be different from or not an actual URL)
write!(self.log_line, " \"{}\"", request).unwrap(); write!(self.log_line, " \"{}\"", request).unwrap();