Skip to content

Commit

Permalink
Webserver send mimetype in chunked data
Browse files Browse the repository at this point in the history
  • Loading branch information
CurlyMoo committed Sep 7, 2017
1 parent 1f6b7d1 commit f09c4c8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 37 deletions.
38 changes: 20 additions & 18 deletions libs/pilight/core/webserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,15 @@ static size_t send_chunked_data(uv_poll_t *req, void *data, unsigned long data_l
struct connection_t *conn = custom_poll_data->data;

if(conn->flags == 0) {
char *a = "HTTP/1.1 200 OK\r\nKeep-Alive: timeout=15, max=100\r\nTransfer-Encoding: chunked\r\n\r\n";
iobuf_append(&custom_poll_data->send_iobuf, a, strlen(a));
char a[255], *p = a;
memset(p, '\0', 255);
int i = snprintf(p, 255,
"HTTP/1.1 200 OK\r\nKeep-Alive: timeout=15, max=100\r\n"\
"Content-Type: %s\r\nTransfer-Encoding: chunked\r\n\r\n",
conn->mimetype
);

iobuf_append(&custom_poll_data->send_iobuf, p, i);
conn->flags = 1;
}
write_chunk(req, data, data_len);
Expand Down Expand Up @@ -721,7 +728,6 @@ static int request_handler(uv_poll_t *req) {
struct connection_t *conn = custom_poll_data->data;

char *ext = NULL;
char *mimetype = NULL;
char buffer[4096], *p = buffer;

memset(buffer, '\0', 4096);
Expand Down Expand Up @@ -815,7 +821,7 @@ static int request_handler(uv_poll_t *req) {
/* Retrieve the extension of the requested file and create a mimetype accordingly */
dot = strrchr(conn->request, '.');
if(dot == NULL || dot == conn->request) {
mimetype = STRDUP("text/plain");
strcpy(conn->mimetype, "text/plain");
} else {
if((ext = REALLOC(ext, strlen(dot)+1)) == NULL) {
OUT_OF_MEMORY /*LCOV_EXCL_LINE*/
Expand All @@ -824,25 +830,25 @@ static int request_handler(uv_poll_t *req) {
strcpy(ext, dot+1);

if(strcmp(ext, "html") == 0) {
mimetype = STRDUP("text/html");
strcpy(conn->mimetype, "text/html");
} else if(strcmp(ext, "xml") == 0) {
mimetype = STRDUP("text/xml");
strcpy(conn->mimetype, "text/xml");
} else if(strcmp(ext, "png") == 0) {
mimetype = STRDUP("image/png");
strcpy(conn->mimetype, "image/png");
} else if(strcmp(ext, "gif") == 0) {
mimetype = STRDUP("image/gif");
strcpy(conn->mimetype, "image/gif");
} else if(strcmp(ext, "ico") == 0) {
mimetype = STRDUP("image/x-icon");
strcpy(conn->mimetype, "image/x-icon");
} else if(strcmp(ext, "jpg") == 0) {
mimetype = STRDUP("image/jpg");
strcpy(conn->mimetype, "image/jpg");
} else if(strcmp(ext, "css") == 0) {
mimetype = STRDUP("text/css");
strcpy(conn->mimetype, "text/css");
} else if(strcmp(ext, "js") == 0) {
mimetype = STRDUP("text/javascript");
strcpy(conn->mimetype, "text/javascript");
} else if(strcmp(ext, "php") == 0) {
mimetype = STRDUP("application/x-httpd-php");
strcpy(conn->mimetype, "application/x-httpd-php");
} else {
mimetype = STRDUP("text/plain");
strcpy(conn->mimetype, "text/plain");
}
}
FREE(ext);
Expand All @@ -851,15 +857,13 @@ static int request_handler(uv_poll_t *req) {
p = buffer;

if(access(conn->request, F_OK) != 0) {
FREE(mimetype);
goto filenotfound;
}

const char *cl = NULL;
if((cl = http_get_header(conn, "Content-Length"))) {
if(atoi(cl) > MAX_UPLOAD_FILESIZE) {
char line[1024] = {'\0'};
FREE(mimetype);
sprintf(line, "Webserver Warning: POST Content-Length of %d bytes exceeds the limit of %d bytes in Unknown on line 0", MAX_UPLOAD_FILESIZE, atoi(cl));
webserver_create_header(&p, "200 OK", "text/plain", strlen(line));
iobuf_append(&custom_poll_data->send_iobuf, buffer, (int)(p-buffer));
Expand Down Expand Up @@ -899,7 +903,6 @@ static int request_handler(uv_poll_t *req) {
long arg = fcntl(conn->file_fd, F_GETFL, NULL);
fcntl(conn->file_fd, F_SETFL, arg | O_NONBLOCK);
#endif
FREE(mimetype);

if(file_read_cb(conn->file_fd, req) == 0) {
return MG_MORE;
Expand All @@ -908,7 +911,6 @@ static int request_handler(uv_poll_t *req) {
}
}

FREE(mimetype);
return MG_MORE;
}
} else if(websockets == WEBGUI_WEBSOCKETS) {
Expand Down
1 change: 1 addition & 0 deletions libs/pilight/core/webserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ typedef struct connection_t {

char *content;
size_t content_len;
char mimetype[255];

int is_websocket;
int ping;
Expand Down
38 changes: 19 additions & 19 deletions tests/webserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,34 +84,34 @@ static struct tests_t get_tests[] = {
/*
* Regular pilight webgui loading until config call
*/
{ "http://127.0.0.1:10080/", 1674, "", 200 },
{ "http://127.0.0.1:10080/jquery-2.1.4.min.js", 84345, "", 200 },
{ "http://127.0.0.1:10080/jquery.mobile-1.4.5.min.js", 200144, "", 200 },
{ "http://127.0.0.1:10080/moment.min.js", 35416, "", 200 },
{ "http://127.0.0.1:10080/pilight.js", 48689, "", 200 },
{ "http://127.0.0.1:10080/jquery.mobile-1.4.5.min.css", 207466, "", 200 },
{ "http://127.0.0.1:10080/pilight.css", 6261, "", 200 },
{ "http://127.0.0.1:10080/pilight.jquery.theme.css", 25683, "", 200 },
{ "http://127.0.0.1:10080/images/ajax-loader.gif", 6242, "", 200 },
{ "http://127.0.0.1:10080/", 1674, "text/html", 200 },
{ "http://127.0.0.1:10080/jquery-2.1.4.min.js", 84345, "text/javascript", 200 },
{ "http://127.0.0.1:10080/jquery.mobile-1.4.5.min.js", 200144, "text/javascript", 200 },
{ "http://127.0.0.1:10080/moment.min.js", 35416, "text/javascript", 200 },
{ "http://127.0.0.1:10080/pilight.js", 48689, "text/javascript", 200 },
{ "http://127.0.0.1:10080/jquery.mobile-1.4.5.min.css", 207466, "text/css", 200 },
{ "http://127.0.0.1:10080/pilight.css", 6261, "text/css", 200 },
{ "http://127.0.0.1:10080/pilight.jquery.theme.css", 25683, "text/css", 200 },
{ "http://127.0.0.1:10080/images/ajax-loader.gif", 6242, "image/gif", 200 },
{ "http://127.0.0.1:10080/config", 220, "application/json", 200 },
/*
* Regular pilight ssl webgui loading until config call
*/
{ "https://127.0.0.1:10443/", 1674, "", 200 },
{ "https://127.0.0.1:10443/jquery-2.1.4.min.js", 84345, "", 200 },
{ "https://127.0.0.1:10443/jquery.mobile-1.4.5.min.js", 200144, "", 200 },
{ "https://127.0.0.1:10443/moment.min.js", 35416, "", 200 },
{ "https://127.0.0.1:10443/pilight.js", 48689, "", 200 },
{ "https://127.0.0.1:10443/jquery.mobile-1.4.5.min.css", 207466, "", 200 },
{ "https://127.0.0.1:10443/pilight.css", 6261, "", 200 },
{ "https://127.0.0.1:10443/pilight.jquery.theme.css", 25683, "", 200 },
{ "https://127.0.0.1:10443/images/ajax-loader.gif", 6242, "", 200 },
{ "https://127.0.0.1:10443/", 1674, "text/html", 200 },
{ "https://127.0.0.1:10443/jquery-2.1.4.min.js", 84345, "text/javascript", 200 },
{ "https://127.0.0.1:10443/jquery.mobile-1.4.5.min.js", 200144, "text/javascript", 200 },
{ "https://127.0.0.1:10443/moment.min.js", 35416, "text/javascript", 200 },
{ "https://127.0.0.1:10443/pilight.js", 48689, "text/javascript", 200 },
{ "https://127.0.0.1:10443/jquery.mobile-1.4.5.min.css", 207466, "text/css", 200 },
{ "https://127.0.0.1:10443/pilight.css", 6261, "text/css", 200 },
{ "https://127.0.0.1:10443/pilight.jquery.theme.css", 25683, "text/css", 200 },
{ "https://127.0.0.1:10443/images/ajax-loader.gif", 6242, "image/gif", 200 },
{ "https://127.0.0.1:10443/config", 220, "application/json", 200 },
{ "https://127.0.0.1:10443/nonexisting", 214, "text/html", 404 }
};

static struct tests_t auth_tests[] = {
{ "http://test:test@127.0.0.1:10080/", 1674, "", 200 },
{ "http://test:test@127.0.0.1:10080/", 1674, "text/html", 200 },
{ "http://test1:test1@127.0.0.1:10080/", 0, "", 401 }
};

Expand Down

0 comments on commit f09c4c8

Please sign in to comment.