Skip to content

Commit

Permalink
handler: add new event: UH_EV_HEAD_COMPLETE
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
  • Loading branch information
zhaojh329 committed Dec 26, 2020
1 parent 0a7a38a commit 06e3d76
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
18 changes: 16 additions & 2 deletions example/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,21 @@ static void upload_handler(struct uh_connection *conn, int event)
{
static int fd = -1;

if (event == UH_EV_BODY) {
if (event == UH_EV_HEAD_COMPLETE) {
struct uh_str str = conn->get_header(conn, "Content-Length");
int content_length;
char buf[128];

sprintf(buf, "%.*s\n", (int)str.len, str.p);

content_length = atoi(buf);

if (content_length > 1024 * 1024 * 1024) {
conn->error(conn, HTTP_STATUS_INTERNAL_SERVER_ERROR, "Too big");
return;
}

} if (event == UH_EV_BODY) {
struct uh_str body = conn->extract_body(conn);

if (fd < 0) {
Expand All @@ -80,7 +94,7 @@ static void upload_handler(struct uh_connection *conn, int event)
close(fd);
return;
}
} else {
} else if (event == UH_EV_COMPLETE) {
struct stat st;
size_t size = 0;

Expand Down
5 changes: 5 additions & 0 deletions src/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,11 @@ static int on_headers_complete(struct http_parser *parser)
return -1;
}

conn->handler(conn, UH_EV_HEAD_COMPLETE);

if (conn->flags & CONN_F_SEND_AND_CLOSE)
return -1;

return 0;
}

Expand Down
1 change: 1 addition & 0 deletions src/uhttpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct uh_plugin {
};

enum {
UH_EV_HEAD_COMPLETE,
UH_EV_BODY,
UH_EV_COMPLETE
};
Expand Down

0 comments on commit 06e3d76

Please sign in to comment.