mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-14 19:03:20 +00:00
Do not send 500 when timing out getreq()
This commit is contained in:
1 parent
88cf329490
commit
ebf9ee089b
5 files changed
+55
-39
No files matched your search
+23
-15
@@ -1023,22 +1023,24 @@ static int is_valid_uri(const char *uri) {
|
||||
return uri[0] == '/' || (uri[0] == '*' && uri[1] == '\0');
|
||||
}
|
||||
|
||||
static int getreq(struct mg_connection *conn, char *ebuf, size_t ebuf_len) {
|
||||
const char *cl;
|
||||
static const char *getreq(struct mg_connection *conn) {
|
||||
const char *cl, *ret = NULL;
|
||||
|
||||
ebuf[0] = '\0';
|
||||
reset_per_request_attributes(conn);
|
||||
conn->request_len = read_request(NULL, conn, conn->buf, conn->buf_size,
|
||||
&conn->data_len);
|
||||
assert(conn->request_len < 0 || conn->data_len >= conn->request_len);
|
||||
|
||||
if (conn->request_len == 0 && conn->data_len == conn->buf_size) {
|
||||
snprintf(ebuf, ebuf_len, "%s", "Request Too Large");
|
||||
conn->status_code = 413;
|
||||
ret = "Request Entity Too Large";
|
||||
} else if (conn->request_len <= 0) {
|
||||
snprintf(ebuf, ebuf_len, "%s", "Client closed connection");
|
||||
conn->status_code = 0;
|
||||
ret = "Client closed connection";
|
||||
} else if (parse_http_message(conn->buf, conn->buf_size,
|
||||
&conn->request_info) <= 0) {
|
||||
snprintf(ebuf, ebuf_len, "Bad request: [%.*s]", conn->data_len, conn->buf);
|
||||
conn->status_code = 400;
|
||||
ret = "Bad Request";
|
||||
} else {
|
||||
// Request is valid. Set content_len attribute by parsing Content-Length
|
||||
// If Content-Length is absent, set content_len to 0 if request is GET,
|
||||
@@ -1057,13 +1059,14 @@ static int getreq(struct mg_connection *conn, char *ebuf, size_t ebuf_len) {
|
||||
}
|
||||
conn->birth_time = time(NULL);
|
||||
}
|
||||
return ebuf[0] == '\0';
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void process_new_connection(struct mg_connection *conn) {
|
||||
struct mg_request_info *ri = &conn->request_info;
|
||||
int keep_alive_enabled, keep_alive, discard_len;
|
||||
char ebuf[100];
|
||||
const char *msg = NULL;
|
||||
|
||||
keep_alive_enabled = !strcmp(conn->ctx->config[ENABLE_KEEP_ALIVE], "yes");
|
||||
keep_alive = 0;
|
||||
@@ -1072,19 +1075,24 @@ static void process_new_connection(struct mg_connection *conn) {
|
||||
// to crule42.
|
||||
conn->data_len = 0;
|
||||
do {
|
||||
if (!getreq(conn, ebuf, sizeof(ebuf))) {
|
||||
send_http_error(conn, 500, "Server Error", "%s", ebuf);
|
||||
if ((msg = getreq(conn)) != NULL) {
|
||||
conn->must_close = 1;
|
||||
} else if (!is_valid_uri(conn->request_info.uri)) {
|
||||
snprintf(ebuf, sizeof(ebuf), "Invalid URI: [%s]", ri->uri);
|
||||
send_http_error(conn, 400, "Bad Request", "%s", ebuf);
|
||||
msg = "Bad Request";
|
||||
conn->status_code = 400;
|
||||
} else if (strcmp(ri->http_version, "1.0") &&
|
||||
strcmp(ri->http_version, "1.1")) {
|
||||
snprintf(ebuf, sizeof(ebuf), "Bad HTTP version: [%s]", ri->http_version);
|
||||
send_http_error(conn, 505, "Bad HTTP version", "%s", ebuf);
|
||||
msg = "Bad HTTP Version";
|
||||
conn->status_code = 505;
|
||||
}
|
||||
|
||||
if (ebuf[0] == '\0') {
|
||||
if (msg != NULL) {
|
||||
// Do not send anything to the client on timeout
|
||||
// see https://github.com/cesanta/mongoose/issues/261
|
||||
if (conn->status_code > 0) {
|
||||
send_http_error(conn, conn->status_code, msg, "%s", "");
|
||||
}
|
||||
} else {
|
||||
handle_request(conn);
|
||||
call_user(MG_REQUEST_END, conn, (void *) (long) conn->status_code);
|
||||
log_access(conn);
|
||||
|
||||
Reference in new issue
Block a user