]> arthur.barton.de Git - netdata.git/blobdiff - src/web_client.c
fixed minor issues throughout the code (mainly types); dashboard has now a watermark...
[netdata.git] / src / web_client.c
index 633d04baac4b1ae6a1543ab46019eeea6e2ff573..5f5ff78afa2da6ef5025cce79a4d4b586caf7fd9 100755 (executable)
@@ -338,6 +338,7 @@ int mysendfile(struct web_client *w, char *filename)
        else if(strstr(filename, ".svg")  != NULL)  w->response.data->contenttype = CT_IMAGE_SVG_XML;
        else if(strstr(filename, ".ttf")  != NULL)  w->response.data->contenttype = CT_APPLICATION_X_FONT_TRUETYPE;
        else if(strstr(filename, ".otf")  != NULL)  w->response.data->contenttype = CT_APPLICATION_X_FONT_OPENTYPE;
+       else if(strstr(filename, ".woff2")!= NULL)  w->response.data->contenttype = CT_APPLICATION_FONT_WOFF2;
        else if(strstr(filename, ".woff") != NULL)  w->response.data->contenttype = CT_APPLICATION_FONT_WOFF;
        else if(strstr(filename, ".eot")  != NULL)  w->response.data->contenttype = CT_APPLICATION_VND_MS_FONTOBJ;
        else w->response.data->contenttype = CT_APPLICATION_OCTET_STREAM;
@@ -435,7 +436,7 @@ uint32_t web_client_api_request_v1_data_options(char *o)
        return ret;
 }
 
-int web_client_api_request_v1_data_format(char *name)
+uint32_t web_client_api_request_v1_data_format(char *name)
 {
        if(!strcmp(name, DATASOURCE_FORMAT_DATATABLE_JSON)) // datatable
                return DATASOURCE_DATATABLE_JSON;
@@ -470,7 +471,7 @@ int web_client_api_request_v1_data_format(char *name)
        return DATASOURCE_JSON;
 }
 
-int web_client_api_request_v1_data_google_format(char *name)
+uint32_t web_client_api_request_v1_data_google_format(char *name)
 {
        if(!strcmp(name, "json"))
                return DATASOURCE_DATATABLE_JSONP;
@@ -568,8 +569,8 @@ int web_client_api_request_v1_data(struct web_client *w, char *url)
                        *google_reqId = "0",
                        *google_sig = "0",
                        *google_out = "json",
-                       *google_responseHandler = "google.visualization.Query.setResponse",
-                       *google_outFileName = NULL;
+                       *responseHandler = NULL,
+                       *outFileName = NULL;
 
        time_t last_timestamp_in_data = 0, google_timestamp = 0;
 
@@ -578,7 +579,8 @@ int web_client_api_request_v1_data(struct web_client *w, char *url)
                        , *after_str = NULL
                        , *points_str = NULL;
 
-       int format = DATASOURCE_JSON, group = GROUP_MAX;
+       int group = GROUP_MAX;
+       uint32_t format = DATASOURCE_JSON;
        uint32_t options = 0x00000000;
 
        while(url) {
@@ -614,6 +616,12 @@ int web_client_api_request_v1_data(struct web_client *w, char *url)
                else if(!strcmp(name, "options")) {
                        options |= web_client_api_request_v1_data_options(value);
                }
+               else if(!strcmp(name, "callback")) {
+                       responseHandler = value;
+               }
+               else if(!strcmp(name, "filename")) {
+                       outFileName = value;
+               }
                else if(!strcmp(name, "tqx")) {
                        // parse Google Visualization API options
                        // https://developers.google.com/chart/interactive/docs/dev/implementing_data_source
@@ -640,9 +648,9 @@ int web_client_api_request_v1_data(struct web_client *w, char *url)
                                        format = web_client_api_request_v1_data_google_format(google_out);
                                }
                                else if(!strcmp(tqx_name, "responseHandler"))
-                                       google_responseHandler = tqx_value;
+                                       responseHandler = tqx_value;
                                else if(!strcmp(tqx_name, "outFileName"))
-                                       google_outFileName = tqx_value;
+                                       outFileName = tqx_value;
                        }
                }
        }
@@ -676,19 +684,29 @@ int web_client_api_request_v1_data(struct web_client *w, char *url)
                        , options
                        );
 
-       if(google_outFileName && *google_outFileName) {
-               buffer_sprintf(w->response.header, "Content-Disposition: attachment; filename=\"%s\"\r\n", google_outFileName);
-               error("generating outfilename header: '%s'", google_outFileName);
+       if(outFileName && *outFileName) {
+               buffer_sprintf(w->response.header, "Content-Disposition: attachment; filename=\"%s\"\r\n", outFileName);
+               error("generating outfilename header: '%s'", outFileName);
        }
 
        if(format == DATASOURCE_DATATABLE_JSONP) {
+               if(responseHandler == NULL)
+                       responseHandler = "google.visualization.Query.setResponse";
+
                debug(D_WEB_CLIENT_ACCESS, "%llu: GOOGLE JSON/JSONP: version = '%s', reqId = '%s', sig = '%s', out = '%s', responseHandler = '%s', outFileName = '%s'",
-                               w->id, google_version, google_reqId, google_sig, google_out, google_responseHandler, google_outFileName
+                               w->id, google_version, google_reqId, google_sig, google_out, responseHandler, outFileName
                        );
 
                buffer_sprintf(w->response.data,
                        "%s({version:'%s',reqId:'%s',status:'ok',sig:'%lu',table:",
-                       google_responseHandler, google_version, google_reqId, st->last_updated.tv_sec);
+                       responseHandler, google_version, google_reqId, st->last_updated.tv_sec);
+       }
+       else if(format == DATASOURCE_JSONP) {
+               if(responseHandler == NULL)
+                       responseHandler = "callback";
+
+               buffer_strcat(w->response.data, responseHandler);
+               buffer_strcat(w->response.data, "(");
        }
 
        ret = rrd2format(st, w->response.data, dimensions, format, points, after, before, group, options, &last_timestamp_in_data);
@@ -702,9 +720,11 @@ int web_client_api_request_v1_data(struct web_client *w, char *url)
                        buffer_flush(w->response.data);
                        buffer_sprintf(w->response.data,
                                "%s({version:'%s',reqId:'%s',status:'error',errors:[{reason:'not_modified',message:'Data not modified'}]});",
-                               google_responseHandler, google_version, google_reqId);
+                               responseHandler, google_version, google_reqId);
                }
        }
+       else if(format == DATASOURCE_JSONP)
+               buffer_strcat(w->response.data, ");");
 
 cleanup:
        if(dimensions) buffer_free(dimensions);
@@ -823,7 +843,7 @@ int web_client_data_request(struct web_client *w, char *url, int datasource_type
        char *google_out = "json";
        char *google_responseHandler = "google.visualization.Query.setResponse";
        char *google_outFileName = NULL;
-       unsigned long last_timestamp_in_data = 0;
+       time_t last_timestamp_in_data = 0;
        if(datasource_type == DATASOURCE_DATATABLE_JSON || datasource_type == DATASOURCE_DATATABLE_JSONP) {
 
                w->response.data->contenttype = CT_APPLICATION_X_JAVASCRIPT;
@@ -882,7 +902,7 @@ int web_client_data_request(struct web_client *w, char *url, int datasource_type
        }
 
        debug(D_WEB_CLIENT_ACCESS, "%llu: Sending RRD data '%s' (id %s, %d lines, %d group, %d group_method, %lu after, %lu before).", w->id, st->name, st->id, lines, group_count, group_method, after, before);
-       unsigned long timestamp_in_data = rrd_stats_json(datasource_type, st, w->response.data, lines, group_count, group_method, after, before, nonzero);
+       time_t timestamp_in_data = rrd_stats_json(datasource_type, st, w->response.data, lines, group_count, group_method, after, before, nonzero);
 
        if(datasource_type == DATASOURCE_DATATABLE_JSONP) {
                if(timestamp_in_data > last_timestamp_in_data)
@@ -950,7 +970,7 @@ cleanup:
 
 void web_client_process(struct web_client *w) {
        int code = 500;
-       int bytes;
+       ssize_t bytes;
 
        w->wait_receive = 0;
 
@@ -1051,6 +1071,7 @@ void web_client_process(struct web_client *w) {
                                        rrd_stats_graph_json(st, url, w->response.data);
                                }
                        }
+#ifdef NETDATA_INTERNAL_CHECKS
                        else if(strcmp(tok, "debug") == 0) {
                                buffer_flush(w->response.data);
 
@@ -1085,6 +1106,7 @@ void web_client_process(struct web_client *w) {
                                // just leave the buffer as is
                                // it will be copied back to the client
                        }
+#endif
                        else if(strcmp(tok, "list") == 0) {
                                code = 200;
 
@@ -1158,7 +1180,7 @@ void web_client_process(struct web_client *w) {
        // prepare the HTTP response header
        debug(D_WEB_CLIENT, "%llu: Generating HTTP header with response %d.", w->id, code);
 
-       char *content_type_string = "";
+       char *content_type_string;
        switch(w->response.data->contenttype) {
                case CT_TEXT_HTML:
                        content_type_string = "text/html; charset=utf-8";
@@ -1208,6 +1230,10 @@ void web_client_process(struct web_client *w) {
                        content_type_string = "application/font-woff";
                        break;
 
+               case CT_APPLICATION_FONT_WOFF2:
+                       content_type_string = "application/font-woff2";
+                       break;
+
                case CT_APPLICATION_VND_MS_FONTOBJ:
                        content_type_string = "application/vnd.ms-fontobject";
                        break;
@@ -1218,7 +1244,7 @@ void web_client_process(struct web_client *w) {
                        break;
        }
 
-       char *code_msg = "";
+       char *code_msg;
        switch(code) {
                case 200:
                        code_msg = "OK";
@@ -1255,6 +1281,8 @@ void web_client_process(struct web_client *w) {
                "Server: NetData Embedded HTTP Server\r\n"
                "Content-Type: %s\r\n"
                "Access-Control-Allow-Origin: *\r\n"
+               "Access-Control-Allow-Methods: GET\r\n"
+               "Access-Control-Allow-Headers: x-requested-with\r\n"
                "Date: %s\r\n"
                , code, code_msg
                , w->keepalive?"keep-alive":"close"
@@ -1269,16 +1297,19 @@ void web_client_process(struct web_client *w) {
                buffer_sprintf(w->response.header_output,
                        "Expires: %s\r\n"
                        "Cache-Control: no-cache\r\n"
+                       "Access-Control-Max-Age: 0\r\n"
                        , date);
        }
-       else
+       else {
                buffer_strcat(w->response.header_output, "Cache-Control: public\r\n");
+               buffer_strcat(w->response.header_output, "Access-Control-Max-Age: 3600\r\n");
+       }
 
        // if we know the content length, put it
        if(!w->response.zoutput && (w->response.data->len || w->response.rlen))
                buffer_sprintf(w->response.header_output,
                        "Content-Length: %ld\r\n"
-                       , w->response.data->len?w->response.data->len:w->response.rlen
+                       , w->response.data->len? w->response.data->len: w->response.rlen
                        );
        else if(!w->response.zoutput)
                w->keepalive = 0;       // content-length is required for keep-alive
@@ -1305,7 +1336,7 @@ void web_client_process(struct web_client *w) {
                        );
 
        bytes = send(w->ofd, buffer_tostring(w->response.header_output), buffer_strlen(w->response.header_output), 0);
-       if(bytes != buffer_strlen(w->response.header_output))
+       if(bytes != (ssize_t) buffer_strlen(w->response.header_output))
                error("%llu: HTTP Header failed to be sent (I sent %d bytes but the system sent %d bytes)."
                                , w->id
                                , buffer_strlen(w->response.header_output)
@@ -1356,12 +1387,12 @@ void web_client_process(struct web_client *w) {
        }
 }
 
-long web_client_send_chunk_header(struct web_client *w, int len)
+long web_client_send_chunk_header(struct web_client *w, long len)
 {
        debug(D_DEFLATE, "%llu: OPEN CHUNK of %d bytes (hex: %x).", w->id, len, len);
        char buf[1024];
-       sprintf(buf, "%X\r\n", len);
-       int bytes = send(w->ofd, buf, strlen(buf), MSG_DONTWAIT);
+       sprintf(buf, "%lX\r\n", len);
+       ssize_t bytes = send(w->ofd, buf, strlen(buf), MSG_DONTWAIT);
 
        if(bytes > 0) debug(D_DEFLATE, "%llu: Sent chunk header %d bytes.", w->id, bytes);
        else if(bytes == 0) debug(D_DEFLATE, "%llu: Did not send chunk header to the client.", w->id);
@@ -1374,7 +1405,7 @@ long web_client_send_chunk_close(struct web_client *w)
 {
        //debug(D_DEFLATE, "%llu: CLOSE CHUNK.", w->id);
 
-       int bytes = send(w->ofd, "\r\n", 2, MSG_DONTWAIT);
+       ssize_t bytes = send(w->ofd, "\r\n", 2, MSG_DONTWAIT);
 
        if(bytes > 0) debug(D_DEFLATE, "%llu: Sent chunk suffix %d bytes.", w->id, bytes);
        else if(bytes == 0) debug(D_DEFLATE, "%llu: Did not send chunk suffix to the client.", w->id);
@@ -1387,7 +1418,7 @@ long web_client_send_chunk_finalize(struct web_client *w)
 {
        //debug(D_DEFLATE, "%llu: FINALIZE CHUNK.", w->id);
 
-       int bytes = send(w->ofd, "\r\n0\r\n\r\n", 7, MSG_DONTWAIT);
+       ssize_t bytes = send(w->ofd, "\r\n0\r\n\r\n", 7, MSG_DONTWAIT);
 
        if(bytes > 0) debug(D_DEFLATE, "%llu: Sent chunk suffix %d bytes.", w->id, bytes);
        else if(bytes == 0) debug(D_DEFLATE, "%llu: Did not send chunk suffix to the client.", w->id);
@@ -1449,7 +1480,7 @@ long web_client_send_deflate(struct web_client *w)
                // give the compressor all the data not passed through the compressor yet
                if(w->response.data->len > w->response.sent) {
                        w->response.zstream.next_in = (Bytef *)&w->response.data->buffer[w->response.sent];
-                       w->response.zstream.avail_in = (w->response.data->len - w->response.sent);
+                       w->response.zstream.avail_in = (uInt) (w->response.data->len - w->response.sent);
                }
 
                // reset the compressor output buffer
@@ -1486,7 +1517,7 @@ long web_client_send_deflate(struct web_client *w)
                t += web_client_send_chunk_header(w, w->response.zhave);
        }
 
-       len = send(w->ofd, &w->response.zbuffer[w->response.zsent], w->response.zhave - w->response.zsent, MSG_DONTWAIT);
+       len = send(w->ofd, &w->response.zbuffer[w->response.zsent], (size_t) (w->response.zhave - w->response.zsent), MSG_DONTWAIT);
        if(len > 0) {
                w->response.zsent += len;
                if(t > 0) len += t;
@@ -1554,12 +1585,12 @@ long web_client_receive(struct web_client *w)
        long bytes;
 
        if(unlikely(w->mode == WEB_CLIENT_MODE_FILECOPY))
-               bytes = read(w->ifd, &w->response.data->buffer[w->response.data->len], (left-1));
+               bytes = read(w->ifd, &w->response.data->buffer[w->response.data->len], (size_t) (left - 1));
        else
-               bytes = recv(w->ifd, &w->response.data->buffer[w->response.data->len], left-1, MSG_DONTWAIT);
+               bytes = recv(w->ifd, &w->response.data->buffer[w->response.data->len], (size_t) (left - 1), MSG_DONTWAIT);
 
        if(likely(bytes > 0)) {
-               int old = w->response.data->len;
+               size_t old = w->response.data->len;
                w->response.data->len += bytes;
                w->response.data->buffer[w->response.data->len] = '\0';