Skip to content

Commit

Permalink
Fix bug: Parsing error for fragment data
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
  • Loading branch information
zhaojh329 committed Aug 25, 2020
1 parent 1017f10 commit f8d348d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,14 @@ static int on_url_cb(struct http_parser *parser, const char *at, size_t length)
struct uh_connection *conn = (struct uh_connection *)parser->data;
struct uh_request *req = &conn->req;

req->url.offset = ROF(conn, at);
if (req->url.offset == 0) {
req->url.offset = ROF(conn, at);
http_parser_url_init(&conn->url_parser);
}

http_parser_url_init(&conn->url_parser);
req->url.length += length;

return http_parser_parse_url(at, length, false, &conn->url_parser);
return 0;
}

static int on_header_field_cb(struct http_parser *parser, const char *at, size_t length)
Expand Down Expand Up @@ -314,6 +317,8 @@ static int on_message_complete_cb(struct http_parser *parser)
struct uh_request *req = &conn->req;
int i;

http_parser_parse_url(O2D(conn, req->url.offset), req->url.length, false, &conn->url_parser);

if (!run_plugins(conn)) {
if (conn->srv->on_request)
conn->srv->on_request(conn);
Expand Down Expand Up @@ -460,9 +465,8 @@ static void conn_read_cb(struct ev_loop *loop, struct ev_io *w, int revents)
{
struct uh_connection *conn = container_of(w, struct uh_connection, ior);
struct http_parser *parser = &conn->parser;
static uint8_t sep[] = {'\r', '\n', '\r', '\n'};
struct buffer *rb = &conn->rb;
int ret, length, nparsed;
int ret, nread, length, nparsed;
bool eof;

if (conn->flags & CONN_F_SEND_AND_CLOSE) {
Expand All @@ -485,6 +489,8 @@ static void conn_read_cb(struct ev_loop *loop, struct ev_io *w, int revents)

conn->activity = ev_now(loop);

length = buffer_length(rb);

#if UHTTPD_SSL_SUPPORT
if (conn->ssl)
ret = buffer_put_fd_ex(rb, w->fd, -1, &eof, conn_ssl_read, conn->ssl);
Expand All @@ -503,14 +509,12 @@ static void conn_read_cb(struct ev_loop *loop, struct ev_io *w, int revents)
return;
}

if (buffer_find(rb, 0, 1024, sep, 4) < 0)
return;
nread = buffer_length(rb) - length;

length = buffer_length(rb);
nparsed = http_parser_execute(parser, &settings, (const char *)rb->data, length);
nparsed = http_parser_execute(parser, &settings, (const char *)rb->data + length, nread);
if (parser->upgrade)
conn_error(conn, HTTP_STATUS_NOT_IMPLEMENTED, NULL);
else if (nparsed != length)
else if (nparsed != nread)
conn_error(conn, HTTP_STATUS_BAD_REQUEST, http_errno_description(parser->http_errno));
}

Expand Down Expand Up @@ -591,4 +595,3 @@ struct uh_connection *uh_new_connection(struct uh_server *srv, int sock, struct

return conn;
}

1 change: 1 addition & 0 deletions src/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct uh_server;

struct uh_request {
struct {
int length;
int offset;
char *path;
char *query;
Expand Down

0 comments on commit f8d348d

Please sign in to comment.