Skip to content

Commit e2544ac

Browse files
Fix issue introduced in http-parser -> llhttp conversion
http_parser_execute() returns the number of parsed bytes, while llhttp_execute() returns an error code. Signed-off-by: Sergio Correia <[email protected]>
1 parent 4b7656b commit e2544ac

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

src/http.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ typedef llhttp_status_t http_status_t;
3030
typedef llhttp_settings_t http_settings_t;
3131
typedef llhttp_t http_parser_t;
3232
#define tang_http_parser_init(parser, settings) llhttp_init(parser, HTTP_REQUEST, settings)
33-
#define tang_http_parser_execute(parser, settings, req, rcvd) llhttp_execute(parser, req, rcvd)
3433
#define tang_http_parser_errno(parser) parser.error
3534
#define tang_http_errno_description(parser, errno) llhttp_get_error_reason(parser)
36-
35+
#define tang_http_parser_resume(parser) llhttp_resume(parser)
3736
#else
3837
/* Legacy http-parser. */
3938
#include <http_parser.h>
@@ -44,10 +43,9 @@ typedef http_parser_settings http_settings_t;
4443
typedef struct http_parser http_parser_t;
4544

4645
#define tang_http_parser_init(parser, settings) http_parser_init(parser, HTTP_REQUEST)
47-
#define tang_http_parser_execute(parser, settings, req, rcvd) http_parser_execute(parser, settings, req, rcvd)
4846
#define tang_http_parser_errno(parser) parser.http_errno
4947
#define tang_http_errno_description(parser, errno) http_errno_description(errno)
50-
48+
#define tang_http_parser_resume(parser) http_parser_pause(parser, 0)
5149
#endif /* USE_LLHTTP */
5250

5351
struct http_dispatch {

src/tangd.c

+46-2
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,44 @@ static struct http_dispatch s_dispatch[] = {
197197

198198
#define DEFAULT_PORT 9090
199199

200+
static size_t
201+
tang_http_parser_execute(http_parser_t *parser, const char* data, size_t len)
202+
{
203+
#ifdef USE_LLHTTP
204+
llhttp_errno_t error;
205+
size_t parsed_len;
206+
207+
/*
208+
* Unlike http_parser, which returns the number of parsed
209+
* bytes in the _execute() call, llhttp returns an error
210+
* code.
211+
*/
212+
213+
if (data == NULL || len == 0) {
214+
error = llhttp_finish(parser);
215+
} else {
216+
error = llhttp_execute(parser, data, len);
217+
}
218+
219+
parsed_len = len;
220+
/*
221+
* Adjust number of parsed bytes in case of error.
222+
*/
223+
if (error != HPE_OK) {
224+
parsed_len = llhttp_get_error_pos(parser) - data;
225+
226+
/* This isn't a real pause, just a way to stop parsing early. */
227+
if (error == HPE_PAUSED_UPGRADE) {
228+
llhttp_resume_after_upgrade(parser);
229+
}
230+
}
231+
232+
return parsed_len;
233+
#else
234+
return http_parser_execute(parser, &http_settings, data, len);
235+
#endif
236+
}
237+
200238
static int
201239
process_request(const char *jwkdir, int in_fileno)
202240
{
@@ -229,8 +267,14 @@ process_request(const char *jwkdir, int in_fileno)
229267

230268
rcvd += r;
231269

232-
r = tang_http_parser_execute(&parser, &http_settings, req, rcvd);
233-
if (tang_http_parser_errno(parser) != 0) {
270+
r = tang_http_parser_execute(&parser, req, rcvd);
271+
switch (tang_http_parser_errno(parser)) {
272+
case HPE_OK:
273+
break;
274+
case HPE_PAUSED:
275+
tang_http_parser_resume(&parser);
276+
break;
277+
default:
234278
fprintf(stderr, "HTTP Parsing Error: %s\n",
235279
tang_http_errno_description(&parser, tang_http_parser_errno(parser)));
236280
return EXIT_SUCCESS;

0 commit comments

Comments
 (0)