@cryptotaxi247 / netdata-1 / commits / 4bd2a4f45

added CORS OPTIONS support

Costa Tsaousis (ktsaou) committed Jan 4, 2016 at 03:22 UTC 4bd2a4f453964ab8602cbe605f4f5e2e5ed458a7
2 files changed +34 -6
src/web_client.c
+33 -6
@@ -156,7 +156,7 @@ void web_client_reset(struct web_client *w)
156 (float)usecdiff(&w->tv_ready, &w->tv_in) / 1000.0,
157 (float)usecdiff(&tv, &w->tv_ready) / 1000.0,
158 (float)usecdiff(&tv, &w->tv_in) / 1000.0,
159 - (w->mode == WEB_CLIENT_MODE_FILECOPY)?"filecopy":"data",
159 + (w->mode == WEB_CLIENT_MODE_FILECOPY)?"filecopy":((w->mode == WEB_CLIENT_MODE_OPTIONS)?"options":"data"),
160 w->response.code,
161 w->last_url
162 );
@@ -971,6 +971,7 @@ cleanup:
971 void web_client_process(struct web_client *w) {
972 int code = 500;
973 ssize_t bytes;
974 + int enable_gzip = 0;
975
976 w->wait_receive = 0;
977
@@ -990,7 +991,7 @@ void web_client_process(struct web_client *w) {
991 #ifdef NETDATA_WITH_ZLIB
992 // check if the client accepts deflate
993 if(web_enable_gzip && strstr(w->response.data->buffer, "gzip"))
993 - web_client_enable_deflate(w);
994 + enable_gzip = 1;
995 #endif // NETDATA_WITH_ZLIB
996
997 int datasource_type = DATASOURCE_DATATABLE_JSONP;
@@ -1002,21 +1003,43 @@ void web_client_process(struct web_client *w) {
1003 char *url = NULL;
1004 char *pointer_to_free = NULL; // keep url_decode() allocated buffer
1005
1006 + w->mode = WEB_CLIENT_MODE_NORMAL;
1007 +
1008 if(buf && strcmp(tok, "GET") == 0) {
1009 tok = strsep(&buf, " \r\n");
1010 pointer_to_free = url = url_decode(tok);
1011 debug(D_WEB_CLIENT, "%llu: Processing HTTP GET on url '%s'.", w->id, url);
1012 }
1013 + else if(buf && strcmp(tok, "OPTIONS") == 0) {
1014 + tok = strsep(&buf, " \r\n");
1015 + pointer_to_free = url = url_decode(tok);
1016 + debug(D_WEB_CLIENT, "%llu: Processing HTTP OPTIONS on url '%s'.", w->id, url);
1017 + w->mode = WEB_CLIENT_MODE_OPTIONS;
1018 + }
1019 else if (buf && strcmp(tok, "POST") == 0) {
1020 w->keepalive = 0;
1021 tok = strsep(&buf, " \r\n");
1022 pointer_to_free = url = url_decode(tok);
1014 -
1023 debug(D_WEB_CLIENT, "%llu: I don't know how to handle POST with form data. Assuming it is a GET on url '%s'.", w->id, url);
1024 }
1025
1026 w->last_url[0] = '\0';
1019 - if(url) {
1027 +
1028 + if(w->mode == WEB_CLIENT_MODE_OPTIONS) {
1029 + strncpy(w->last_url, url, URL_MAX);
1030 + w->last_url[URL_MAX] = '\0';
1031 +
1032 + code = 200;
1033 + w->response.data->contenttype = CT_TEXT_PLAIN;
1034 + buffer_flush(w->response.data);
1035 + buffer_strcat(w->response.data, "OK");
1036 + }
1037 + else if(url) {
1038 +#ifdef NETDATA_WITH_ZLIB
1039 + if(enable_gzip)
1040 + web_client_enable_deflate(w);
1041 +#endif
1042 +
1043 strncpy(w->last_url, url, URL_MAX);
1044 w->last_url[URL_MAX] = '\0';
1045
@@ -1281,8 +1304,8 @@ void web_client_process(struct web_client *w) {
1304 "Server: NetData Embedded HTTP Server\r\n"
1305 "Content-Type: %s\r\n"
1306 "Access-Control-Allow-Origin: *\r\n"
1284 - "Access-Control-Allow-Methods: GET\r\n"
1285 - "Access-Control-Allow-Headers: x-requested-with\r\n"
1307 + "Access-Control-Allow-Methods: GET, OPTIONS\r\n"
1308 + "Access-Control-Allow-Headers: accept, x-requested-with\r\n"
1309 "Date: %s\r\n"
1310 , code, code_msg
1311 , w->keepalive?"keep-alive":"close"
@@ -1357,6 +1380,10 @@ void web_client_process(struct web_client *w) {
1380
1381 // pretty logging
1382 switch(w->mode) {
1383 + case WEB_CLIENT_MODE_OPTIONS:
1384 + debug(D_WEB_CLIENT, "%llu: Done preparing the OPTIONS response. Sending data (%d bytes) to client.", w->id, w->response.data->len);
1385 + break;
1386 +
1387 case WEB_CLIENT_MODE_NORMAL:
1388 debug(D_WEB_CLIENT, "%llu: Done preparing the response. Sending data (%d bytes) to client.", w->id, w->response.data->len);
1389 break;
src/web_client.h
+1
@@ -21,6 +21,7 @@ extern int web_enable_gzip;
21
22 #define WEB_CLIENT_MODE_NORMAL 0
23 #define WEB_CLIENT_MODE_FILECOPY 1
24 +#define WEB_CLIENT_MODE_OPTIONS 2
25
26 #define URL_MAX 8192
27 #define ZLIB_CHUNK 16384