Skip to content

Commit ba04c96

Browse files
author
pfeatherstone
committed
bug fix in parser in the bad path
1 parent 7541c9b commit ba04c96

File tree

4 files changed

+114
-8
lines changed

4 files changed

+114
-8
lines changed

.github/workflows/ubuntu22_clang.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ jobs:
2121

2222
- name: Run CMake configuration and build
2323
run: |
24-
cmake examples -B build -G Ninja \
25-
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
26-
-DCMAKE_C_COMPILER=clang \
27-
-DCMAKE_CXX_COMPILER=clang++
24+
cmake examples -B build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
2825
cmake --build build --parallel
2926
3027
- name: Run Unit Tests

.github/workflows/ubuntu24_clang.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ jobs:
2121

2222
- name: Run CMake configuration and build
2323
run: |
24-
cmake examples -B build -G Ninja \
25-
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
26-
-DCMAKE_C_COMPILER=clang \
27-
-DCMAKE_CXX_COMPILER=clang++
24+
cmake examples -B build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
2825
cmake --build build --parallel
2926
3027
- name: Run Unit Tests

examples/unit_tests/message.cpp

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,117 @@ TEST_SUITE("[MESSAGE]")
102102
}
103103
}
104104

105+
TEST_CASE("parse bad requests")
106+
{
107+
{
108+
std::string bad_req = "SHUV / HTTP/1.1\r\n";
109+
bad_req += "Host: developer.mozilla.org\r\n";
110+
bad_req += "User-Agent: curl/8.6.0\r\n";
111+
bad_req += "Content-Type: application/json\r\n";
112+
bad_req += "Content-Length: 32\r\n";
113+
bad_req += "\r\n";
114+
bad_req += "{\"name\": \"Obi\", \"creed\": \"Jedi\"}";
115+
116+
http::request req;
117+
std::error_code ec{};
118+
bool finished = http::parser<http::request>{}.parse(req, bad_req, ec);
119+
REQUIRE(ec == http::http_read_bad_method);
120+
}
121+
122+
{
123+
std::string bad_req = "POST / HTTP/2.1\r\n";
124+
bad_req += "Host: developer.mozilla.org\r\n";
125+
bad_req += "User-Agent: curl/8.6.0\r\n";
126+
bad_req += "Content-Type: application/json\r\n";
127+
bad_req += "Content-Length: 32\r\n";
128+
bad_req += "\r\n";
129+
bad_req += "{\"name\": \"Obi\", \"creed\": \"Jedi\"}";
130+
131+
http::request req;
132+
std::error_code ec{};
133+
bool finished = http::parser<http::request>{}.parse(req, bad_req, ec);
134+
REQUIRE(ec == http::http_read_unsupported_http_version);
135+
}
136+
137+
{
138+
std::string bad_req = "POST / HTTP/11\r\n";
139+
bad_req += "Host: developer.mozilla.org\r\n";
140+
bad_req += "User-Agent: curl/8.6.0\r\n";
141+
bad_req += "Content-Type: application/json\r\n";
142+
bad_req += "Content-Length: 32\r\n";
143+
bad_req += "\r\n";
144+
bad_req += "{\"name\": \"Obi\", \"creed\": \"Jedi\"}";
145+
146+
http::request req;
147+
std::error_code ec{};
148+
bool finished = http::parser<http::request>{}.parse(req, bad_req, ec);
149+
REQUIRE(ec == http::http_read_unsupported_http_version);
150+
}
151+
152+
{
153+
std::string bad_req = "POST / PROTOCOL/1.1\r\n";
154+
bad_req += "Host: developer.mozilla.org\r\n";
155+
bad_req += "User-Agent: curl/8.6.0\r\n";
156+
bad_req += "Content-Type: application/json\r\n";
157+
bad_req += "Content-Length: 32\r\n";
158+
bad_req += "\r\n";
159+
bad_req += "{\"name\": \"Obi\", \"creed\": \"Jedi\"}";
160+
161+
http::request req;
162+
std::error_code ec{};
163+
bool finished = http::parser<http::request>{}.parse(req, bad_req, ec);
164+
REQUIRE(ec == http::http_read_unsupported_http_version);
165+
}
166+
167+
{
168+
std::string bad_req = "POST / HTTP/1.1\r\n";
169+
bad_req += "Host - developer.mozilla.org\r\n";
170+
bad_req += "User-Agent: curl/8.6.0\r\n";
171+
bad_req += "Content-Type: application/json\r\n";
172+
bad_req += "Content-Length: 32\r\n";
173+
bad_req += "\r\n";
174+
bad_req += "{\"name\": \"Obi\", \"creed\": \"Jedi\"}";
175+
176+
http::request req;
177+
std::error_code ec{};
178+
bool finished = http::parser<http::request>{}.parse(req, bad_req, ec);
179+
REQUIRE(ec == http::http_read_header_kv_delimiter_not_found);
180+
}
181+
182+
{
183+
std::string bad_req = "POST / HTTP/1.1\r\n";
184+
bad_req += "Host : developer.mozilla.org\r\n";
185+
bad_req += "User-Agent: curl/8.6.0\r\n";
186+
bad_req += "Content-Type: application/json\r\n";
187+
bad_req += "Content-Length: 32\r\n";
188+
bad_req += "Sith-Code: 66\r\n";
189+
bad_req += "\r\n";
190+
bad_req += "{\"name\": \"Obi\", \"creed\": \"Jedi\"}";
191+
192+
http::request req;
193+
std::error_code ec{};
194+
bool finished = http::parser<http::request>{}.parse(req, bad_req, ec);
195+
REQUIRE(ec == http::http_read_header_unsupported_field);
196+
}
197+
198+
{
199+
std::string bad_req = "POST / HTTP/1.1\r\n";
200+
bad_req += "Host: developer.mozilla.org\r\n";
201+
bad_req += "User-Agent: curl/8.6.0\r\n";
202+
bad_req += "Content-Type: application/json\r\n";
203+
bad_req += "Content-Length: 64\r\n";
204+
bad_req += "\r\n";
205+
bad_req += "{\"name\": \"Obi\", \"creed\": \"Jedi\"}";
206+
207+
http::request req;
208+
std::error_code ec{};
209+
bool finished = http::parser<http::request>{}.parse(req, bad_req, ec);
210+
REQUIRE(!bool(ec));
211+
REQUIRE(!finished);
212+
REQUIRE(bad_req.empty());
213+
}
214+
}
215+
105216
TEST_CASE("serialise bad requests")
106217
{
107218
http::request req;

src/http.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,7 @@ namespace http
11371137
// Sufficient
11381138
if (end != nullptr)
11391139
{
1140+
*end = '\0';
11401141
const size_t line_length = std::distance(&buf[0], end);
11411142

11421143
// Found header

0 commit comments

Comments
 (0)