Skip to content

Commit

Permalink
test: add httparse tests and make errors identical
Browse files Browse the repository at this point in the history
  • Loading branch information
jbr committed May 31, 2024
1 parent 7002c48 commit 1b658dd
Show file tree
Hide file tree
Showing 530 changed files with 5,540 additions and 38 deletions.
33 changes: 14 additions & 19 deletions http/src/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,25 +713,20 @@ 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..])?;

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
11 changes: 7 additions & 4 deletions http/tests/corpus.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::{env, net::Shutdown, path::PathBuf};

use indoc::formatdoc;
use pretty_assertions::assert_eq;
use pretty_assertions::{assert_eq, assert_str_eq};

Check warning on line 4 in http/tests/corpus.rs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

unused import: `assert_eq`

Check warning on line 4 in http/tests/corpus.rs

View workflow job for this annotation

GitHub Actions / Build and Test (macOS-latest)

unused import: `assert_eq`

Check warning on line 4 in http/tests/corpus.rs

View workflow job for this annotation

GitHub Actions / Build and Test (macOS-latest)

unused import: `assert_eq`
use test_harness::test;
use trillium_http::{Conn, KnownHeaderName, Swansong};
use trillium_testing::{harness, RuntimeTrait, TestTransport};
Expand Down Expand Up @@ -45,8 +47,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 +76,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 +97,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
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
3 changes: 3 additions & 0 deletions http/tests/corpus/httparse-018.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
Loading

0 comments on commit 1b658dd

Please sign in to comment.