Change the way we handle HTTP response headers

This commit is contained in:
George Oikonomou 2015-03-23 04:10:33 +00:00
parent 733378679f
commit 90a3cd8e14

View file

@ -163,11 +163,6 @@ static const char *http_header_srv_str[] = {
NULL NULL
}; };
static const char *http_header_redir_location[] = {
"Location: /config.html\r\n",
NULL
};
static const char *http_header_con_close[] = { static const char *http_header_con_close[] = {
CONN_CLOSE, CONN_CLOSE,
NULL NULL
@ -1003,7 +998,8 @@ PT_THREAD(send_string(struct httpd_state *s, const char *str))
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static static
PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr, PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr,
const char *content_type, const char **additional)) const char *content_type, const char *redir,
const char **additional))
{ {
PT_BEGIN(&s->generate_pt); PT_BEGIN(&s->generate_pt);
@ -1013,6 +1009,11 @@ PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr,
PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, *(s->ptr))); PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, *(s->ptr)));
} }
if(redir) {
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "Location: %s\r\n", redir));
}
if(additional) { if(additional) {
for(s->ptr = additional; *(s->ptr) != NULL; s->ptr++) { for(s->ptr = additional; *(s->ptr) != NULL; s->ptr++) {
PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, *(s->ptr))); PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, *(s->ptr)));
@ -1041,25 +1042,30 @@ PT_THREAD(handle_output(struct httpd_state *s))
if(s->return_code == RETURN_CODE_OK) { if(s->return_code == RETURN_CODE_OK) {
PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_302, PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_302,
http_content_type_plain, http_content_type_plain,
http_header_redir_location)); s->filename,
NULL));
} else if(s->return_code == RETURN_CODE_LR) { } else if(s->return_code == RETURN_CODE_LR) {
PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_411, PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_411,
http_content_type_plain, http_content_type_plain,
NULL,
http_header_con_close)); http_header_con_close));
PT_WAIT_THREAD(&s->outputpt, send_string(s, "Content-Length Required\n")); PT_WAIT_THREAD(&s->outputpt, send_string(s, "Content-Length Required\n"));
} else if(s->return_code == RETURN_CODE_TL) { } else if(s->return_code == RETURN_CODE_TL) {
PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_413, PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_413,
http_content_type_plain, http_content_type_plain,
NULL,
http_header_con_close)); http_header_con_close));
PT_WAIT_THREAD(&s->outputpt, send_string(s, "Content-Length too Large\n")); PT_WAIT_THREAD(&s->outputpt, send_string(s, "Content-Length too Large\n"));
} else if(s->return_code == RETURN_CODE_SU) { } else if(s->return_code == RETURN_CODE_SU) {
PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_503, PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_503,
http_content_type_plain, http_content_type_plain,
NULL,
http_header_con_close)); http_header_con_close));
PT_WAIT_THREAD(&s->outputpt, send_string(s, "Service Unavailable\n")); PT_WAIT_THREAD(&s->outputpt, send_string(s, "Service Unavailable\n"));
} else { } else {
PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_400, PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_400,
http_content_type_plain, http_content_type_plain,
NULL,
http_header_con_close)); http_header_con_close));
PT_WAIT_THREAD(&s->outputpt, send_string(s, "Bad Request\n")); PT_WAIT_THREAD(&s->outputpt, send_string(s, "Bad Request\n"));
} }
@ -1069,6 +1075,7 @@ PT_THREAD(handle_output(struct httpd_state *s))
strncpy(s->filename, "/notfound.html", sizeof(s->filename)); strncpy(s->filename, "/notfound.html", sizeof(s->filename));
PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_404, PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_404,
http_content_type_html, http_content_type_html,
NULL,
http_header_con_close)); http_header_con_close));
PT_WAIT_THREAD(&s->outputpt, PT_WAIT_THREAD(&s->outputpt,
send_string(s, NOT_FOUND)); send_string(s, NOT_FOUND));
@ -1077,6 +1084,7 @@ PT_THREAD(handle_output(struct httpd_state *s))
} else { } else {
PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_200, PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_200,
http_content_type_html, http_content_type_html,
NULL,
http_header_con_close)); http_header_con_close));
PT_WAIT_THREAD(&s->outputpt, s->script(s)); PT_WAIT_THREAD(&s->outputpt, s->script(s));
} }