Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add httparse tests and make errors identical #653

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
35 changes: 15 additions & 20 deletions http/src/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,27 +713,22 @@ where
.find(&buffer)
.ok_or(Error::InvalidHead)?;

let (method, path, version) = match &memchr::memchr_iter(b' ', &buffer[..first_line_index])
.collect::<Vec<usize>>()[..]
{
[first, second] => {
let (first, second) = (*first, *second);
let method = Method::parse(&buffer[0..first])?;
let path = str::from_utf8(&buffer[first + 1..second])
.map_err(|_| Error::RequestPathMissing)?
.to_string();
let version = Version::parse(&buffer[second + 1..first_line_index])?;

if !matches!(version, Version::Http1_1 | Version::Http1_0) {
return Err(Error::UnsupportedVersion(version));
}

(method, path, version)
}
_ => return Err(Error::InvalidHead),
};
let mut spaces = memchr::memchr_iter(b' ', &buffer[..first_line_index]);
let first_space = spaces.next().ok_or(Error::MissingMethod)?;
let method = Method::parse(&buffer[0..first_space])?;
let second_space = spaces.next().ok_or(Error::RequestPathMissing)?;
let path = str::from_utf8(&buffer[first_space + 1..second_space])
.map_err(|_| Error::RequestPathMissing)?
.to_string();
if path.is_empty() {
return Err(Error::InvalidHead);
}
let version = Version::parse(&buffer[second_space + 1..first_line_index])?;
if !matches!(version, Version::Http1_1 | Version::Http1_0) {
return Err(Error::UnsupportedVersion(version));
}

let request_headers = Headers::parse(&buffer[first_line_index + 2..])?;
let request_headers = Headers::parse(&buffer[first_line_index + 2..head_size])?;

Self::validate_headers(&request_headers)?;

Expand Down
6 changes: 5 additions & 1 deletion http/src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ impl Headers {
}

let mut value_start = token_end + 1;
while (bytes[value_start] as char).is_whitespace() {

while bytes
.get(value_start)
.is_some_and(|b| matches!(b, b'\t' | b' '))
{
value_start += 1;
}

Expand Down
10 changes: 6 additions & 4 deletions http/tests/corpus.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use indoc::formatdoc;
use pretty_assertions::assert_eq;
use pretty_assertions::assert_str_eq;
use std::{env, net::Shutdown, path::PathBuf};
use test_harness::test;
use trillium_http::{Conn, KnownHeaderName, Swansong};
use trillium_testing::{harness, RuntimeTrait, TestTransport};
Expand Down Expand Up @@ -45,8 +46,8 @@ async fn handler(mut conn: Conn<TestTransport>) -> Conn<TestTransport> {
async fn corpus_test() {
env_logger::init();
let runtime = trillium_testing::runtime();
let dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/corpus");
let filter = std::env::var("CORPUS_TEST_FILTER").unwrap_or_default();
let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/corpus");
let filter = env::var("CORPUS_TEST_FILTER").unwrap_or_default();
let corpus_request_files = std::fs::read_dir(dir)
.unwrap()
.filter_map(|f| {
Expand Down Expand Up @@ -74,6 +75,7 @@ async fn corpus_test() {
});

client.write_all(request);
client.shutdown(Shutdown::Write);
let (response, extension) = match res.await.unwrap() {
Ok(None) => (client.read_available_string().await, "response"),
Err(e) => (e.to_string(), "error"),
Expand All @@ -94,7 +96,7 @@ async fn corpus_test() {
.replace(['\n', '\r'], "")
.replace("\\r", "\r")
.replace("\\n", "\n");
assert_eq!(expected_response, response, "{file:?}");
assert_str_eq!(expected_response, response, "\n\n{file:?}");
}

swansong.shut_down();
Expand Down
1 change: 1 addition & 0 deletions http/tests/corpus/cl-and-tec.error
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unexpected header: Content-Length
8 changes: 8 additions & 0 deletions http/tests/corpus/cl-and-tec.request
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
POST http://example.com:8080/ogdlqgxkbepd HTTP/1.1\r\n
Host: example.com:8080\r\n
Transfer-Encoding: chunked\r\n
Content-Length: 100\r\n
Connection: close\r\n
\r\n
0; name=value\r\n
\r\n
14 changes: 0 additions & 14 deletions http/tests/corpus/footers.request

This file was deleted.

3 changes: 3 additions & 0 deletions http/tests/corpus/httparse-001.request
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET /bar;par?b HTTP/1.1\r\n
Host: foo\r\n
\r\n
14 changes: 14 additions & 0 deletions http/tests/corpus/httparse-001.response
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
HTTP/1.1 200 OK\r\n
Date: Tue, 21 Nov 2023 21:27:21 GMT\r\n
Content-Length: 97\r\n
Server: corpus-test\r\n
\r\n
===request===\n
method: GET\n
path: /bar;par\n
version: HTTP/1.1\n
\n
===headers===\n
Host: foo\r\n
\n
===body===\n
3 changes: 3 additions & 0 deletions http/tests/corpus/httparse-002.request
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET /x HTTP/1.1\r\n
Host: test\r\n
\r\n
14 changes: 14 additions & 0 deletions http/tests/corpus/httparse-002.response
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
HTTP/1.1 200 OK\r\n
Date: Tue, 21 Nov 2023 21:27:21 GMT\r\n
Content-Length: 92\r\n
Server: corpus-test\r\n
\r\n
===request===\n
method: GET\n
path: /x\n
version: HTTP/1.1\n
\n
===headers===\n
Host: test\r\n
\n
===body===\n
3 changes: 3 additions & 0 deletions http/tests/corpus/httparse-004.request
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET /foo/foo.com HTTP/1.1\r\n
Host: example.org\r\n
\r\n
14 changes: 14 additions & 0 deletions http/tests/corpus/httparse-004.response
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
HTTP/1.1 200 OK\r\n
Date: Tue, 21 Nov 2023 21:27:21 GMT\r\n
Content-Length: 109\r\n
Server: corpus-test\r\n
\r\n
===request===\n
method: GET\n
path: /foo/foo.com\n
version: HTTP/1.1\n
\n
===headers===\n
Host: example.org\r\n
\n
===body===\n
3 changes: 3 additions & 0 deletions http/tests/corpus/httparse-005.request
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET /foo/:foo.com HTTP/1.1\r\n
Host: example.org\r\n
\r\n
14 changes: 14 additions & 0 deletions http/tests/corpus/httparse-005.response
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
HTTP/1.1 200 OK\r\n
Date: Tue, 21 Nov 2023 21:27:21 GMT\r\n
Content-Length: 110\r\n
Server: corpus-test\r\n
\r\n
===request===\n
method: GET\n
path: /foo/:foo.com\n
version: HTTP/1.1\n
\n
===headers===\n
Host: example.org\r\n
\n
===body===\n
3 changes: 3 additions & 0 deletions http/tests/corpus/httparse-006.request
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET /foo/foo.com HTTP/1.1\r\n
Host: example.org\r\n
\r\n
14 changes: 14 additions & 0 deletions http/tests/corpus/httparse-006.response
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
HTTP/1.1 200 OK\r\n
Date: Tue, 21 Nov 2023 21:27:21 GMT\r\n
Content-Length: 109\r\n
Server: corpus-test\r\n
\r\n
===request===\n
method: GET\n
path: /foo/foo.com\n
version: HTTP/1.1\n
\n
===headers===\n
Host: example.org\r\n
\n
===body===\n
File renamed without changes.
3 changes: 3 additions & 0 deletions http/tests/corpus/httparse-007.request
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET foo.com HTTP/1.1\r\n
Host: \r\n
\r\n
3 changes: 3 additions & 0 deletions http/tests/corpus/httparse-008.request
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET /%20b%20?%20d%20 HTTP/1.1\r\n
Host: f\r\n
\r\n
14 changes: 14 additions & 0 deletions http/tests/corpus/httparse-008.response
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
HTTP/1.1 200 OK\r\n
Date: Tue, 21 Nov 2023 21:27:21 GMT\r\n
Content-Length: 95\r\n
Server: corpus-test\r\n
\r\n
===request===\n
method: GET\n
path: /%20b%20\n
version: HTTP/1.1\n
\n
===headers===\n
Host: f\r\n
\n
===body===\n
1 change: 1 addition & 0 deletions http/tests/corpus/httparse-009.error
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Invalid or missing version
3 changes: 3 additions & 0 deletions http/tests/corpus/httparse-009.request
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET x x HTTP/1.1\r\n
Host: \r\n
\r\n
3 changes: 3 additions & 0 deletions http/tests/corpus/httparse-010.request
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET /c HTTP/1.1\r\n
Host: f\r\n
\r\n
14 changes: 14 additions & 0 deletions http/tests/corpus/httparse-010.response
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
HTTP/1.1 200 OK\r\n
Date: Tue, 21 Nov 2023 21:27:21 GMT\r\n
Content-Length: 89\r\n
Server: corpus-test\r\n
\r\n
===request===\n
method: GET\n
path: /c\n
version: HTTP/1.1\n
\n
===headers===\n
Host: f\r\n
\n
===body===\n
3 changes: 3 additions & 0 deletions http/tests/corpus/httparse-011.request
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET /c HTTP/1.1\r\n
Host: f\r\n
\r\n
14 changes: 14 additions & 0 deletions http/tests/corpus/httparse-011.response
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
HTTP/1.1 200 OK\r\n
Date: Tue, 21 Nov 2023 21:27:21 GMT\r\n
Content-Length: 89\r\n
Server: corpus-test\r\n
\r\n
===request===\n
method: GET\n
path: /c\n
version: HTTP/1.1\n
\n
===headers===\n
Host: f\r\n
\n
===body===\n
3 changes: 3 additions & 0 deletions http/tests/corpus/httparse-012.request
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET /c HTTP/1.1\r\n
Host: f\r\n
\r\n
14 changes: 14 additions & 0 deletions http/tests/corpus/httparse-012.response
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
HTTP/1.1 200 OK\r\n
Date: Tue, 21 Nov 2023 21:27:21 GMT\r\n
Content-Length: 89\r\n
Server: corpus-test\r\n
\r\n
===request===\n
method: GET\n
path: /c\n
version: HTTP/1.1\n
\n
===headers===\n
Host: f\r\n
\n
===body===\n
3 changes: 3 additions & 0 deletions http/tests/corpus/httparse-013.request
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET /c HTTP/1.1\r\n
Host: f\r\n
\r\n
14 changes: 14 additions & 0 deletions http/tests/corpus/httparse-013.response
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
HTTP/1.1 200 OK\r\n
Date: Tue, 21 Nov 2023 21:27:21 GMT\r\n
Content-Length: 89\r\n
Server: corpus-test\r\n
\r\n
===request===\n
method: GET\n
path: /c\n
version: HTTP/1.1\n
\n
===headers===\n
Host: f\r\n
\n
===body===\n
3 changes: 3 additions & 0 deletions http/tests/corpus/httparse-014.request
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET /c HTTP/1.1\r\n
Host: f\r\n
\r\n
14 changes: 14 additions & 0 deletions http/tests/corpus/httparse-014.response
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
HTTP/1.1 200 OK\r\n
Date: Tue, 21 Nov 2023 21:27:21 GMT\r\n
Content-Length: 89\r\n
Server: corpus-test\r\n
\r\n
===request===\n
method: GET\n
path: /c\n
version: HTTP/1.1\n
\n
===headers===\n
Host: f\r\n
\n
===body===\n
3 changes: 3 additions & 0 deletions http/tests/corpus/httparse-015.request
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET /foo/bar HTTP/1.1\r\n
Host: example.org\r\n
\r\n
14 changes: 14 additions & 0 deletions http/tests/corpus/httparse-015.response
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
HTTP/1.1 200 OK\r\n
Date: Tue, 21 Nov 2023 21:27:21 GMT\r\n
Content-Length: 105\r\n
Server: corpus-test\r\n
\r\n
===request===\n
method: GET\n
path: /foo/bar\n
version: HTTP/1.1\n
\n
===headers===\n
Host: example.org\r\n
\n
===body===\n
3 changes: 3 additions & 0 deletions http/tests/corpus/httparse-016.request
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET /foo/bar HTTP/1.1\r\n
Host: example.org\r\n
\r\n
14 changes: 14 additions & 0 deletions http/tests/corpus/httparse-016.response
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
HTTP/1.1 200 OK\r\n
Date: Tue, 21 Nov 2023 21:27:21 GMT\r\n
Content-Length: 105\r\n
Server: corpus-test\r\n
\r\n
===request===\n
method: GET\n
path: /foo/bar\n
version: HTTP/1.1\n
\n
===headers===\n
Host: example.org\r\n
\n
===body===\n
3 changes: 3 additions & 0 deletions http/tests/corpus/httparse-017.request
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET /foo/:foo.com/ HTTP/1.1\r\n
Host: example.org\r\n
\r\n
14 changes: 14 additions & 0 deletions http/tests/corpus/httparse-017.response
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
HTTP/1.1 200 OK\r\n
Date: Tue, 21 Nov 2023 21:27:21 GMT\r\n
Content-Length: 111\r\n
Server: corpus-test\r\n
\r\n
===request===\n
method: GET\n
path: /foo/:foo.com/\n
version: HTTP/1.1\n
\n
===headers===\n
Host: example.org\r\n
\n
===body===\n
Loading
Loading