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;
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;
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;
*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;
, *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) {
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
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;
}
}
}
, 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);
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);
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;
}
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)
void web_client_process(struct web_client *w) {
int code = 500;
- int bytes;
+ ssize_t bytes;
w->wait_receive = 0;
rrd_stats_graph_json(st, url, w->response.data);
}
}
+#ifdef NETDATA_INTERNAL_CHECKS
else if(strcmp(tok, "debug") == 0) {
buffer_flush(w->response.data);
// just leave the buffer as is
// it will be copied back to the client
}
+#endif
else if(strcmp(tok, "list") == 0) {
code = 200;
// 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";
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;
break;
}
- char *code_msg = "";
+ char *code_msg;
switch(code) {
case 200:
code_msg = "OK";
"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"
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
);
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)
}
}
-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);
{
//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);
{
//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);
// 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
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;
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';