int web_client_timeout = DEFAULT_DISCONNECT_IDLE_WEB_CLIENTS_AFTER_SECONDS;
int web_enable_gzip = 1;
+extern int netdata_exit;
+
struct web_client *web_clients = NULL;
unsigned long long web_clients_count = 0;
if(w == web_clients) web_clients = w->next;
+ if(w->response.header_output) buffer_free(w->response.header_output);
if(w->response.header) buffer_free(w->response.header);
if(w->response.data) buffer_free(w->response.data);
close(w->ifd);
else {
struct passwd *pw = getpwnam(web_owner);
if(!pw) {
- error("User %s is not present. Ignoring option. Error: %s\n", web_owner, strerror(errno));
+ error("User %s is not present. Ignoring option.", web_owner);
owner_uid = geteuid();
}
else {
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;
ret |= RRDR_OPTION_NONZERO;
else if(!strcmp(tok, "flip") || !strcmp(tok, "reversed") || !strcmp(tok, "reverse"))
ret |= RRDR_OPTION_REVERSED;
- else if(!strcmp(tok, "abs") || !strcmp(tok, "absolute") || !strcmp(tok, "absolute_sum") || !strcmp(tok, "absolute-sum"))
- ret |= RRDR_OPTION_ABSOLUTE;
+ else if(!strcmp(tok, "jsonwrap"))
+ ret |= RRDR_OPTION_JSON_WRAP;
else if(!strcmp(tok, "min2max"))
ret |= RRDR_OPTION_MIN2MAX;
- else if(!strcmp(tok, "seconds"))
- ret |= RRDR_OPTION_SECONDS;
else if(!strcmp(tok, "ms") || !strcmp(tok, "milliseconds"))
ret |= RRDR_OPTION_MILLISECONDS;
+ else if(!strcmp(tok, "abs") || !strcmp(tok, "absolute") || !strcmp(tok, "absolute_sum") || !strcmp(tok, "absolute-sum"))
+ ret |= RRDR_OPTION_ABSOLUTE;
+ else if(!strcmp(tok, "seconds"))
+ ret |= RRDR_OPTION_SECONDS;
else if(!strcmp(tok, "null2zero"))
ret |= RRDR_OPTION_NULL2ZERO;
else if(!strcmp(tok, "objectrows"))
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, "datatable"))
+ if(!strcmp(name, DATASOURCE_FORMAT_DATATABLE_JSON)) // datatable
return DATASOURCE_DATATABLE_JSON;
- else if(!strcmp(name, "datasource"))
+ else if(!strcmp(name, DATASOURCE_FORMAT_DATATABLE_JSONP)) // datasource
return DATASOURCE_DATATABLE_JSONP;
- else if(!strcmp(name, "json"))
+ else if(!strcmp(name, DATASOURCE_FORMAT_JSON)) // json
return DATASOURCE_JSON;
- else if(!strcmp(name, "jsonp"))
+ else if(!strcmp(name, DATASOURCE_FORMAT_JSONP)) // jsonp
return DATASOURCE_JSONP;
- else if(!strcmp(name, "ssv"))
+ else if(!strcmp(name, DATASOURCE_FORMAT_SSV)) // ssv
return DATASOURCE_SSV;
- else if(!strcmp(name, "csv"))
+ else if(!strcmp(name, DATASOURCE_FORMAT_CSV)) // csv
return DATASOURCE_CSV;
- else if(!strcmp(name, "tsv"))
+ else if(!strcmp(name, DATASOURCE_FORMAT_TSV) || !strcmp(name, "tsv-excel")) // tsv
return DATASOURCE_TSV;
- else if(!strcmp(name, "tsv-excel"))
- return DATASOURCE_TSV;
-
- else if(!strcmp(name, "html"))
+ else if(!strcmp(name, DATASOURCE_FORMAT_HTML)) // html
return DATASOURCE_HTML;
- else if(!strcmp(name, "array"))
+ else if(!strcmp(name, DATASOURCE_FORMAT_JS_ARRAY)) // array
return DATASOURCE_JS_ARRAY;
- else if(!strcmp(name, "ssvcomma"))
+ else if(!strcmp(name, DATASOURCE_FORMAT_SSV_COMMA)) // ssvcomma
return DATASOURCE_SSV_COMMA;
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;
if(url) { ; }
buffer_flush(w->response.data);
+ w->response.data->contenttype = CT_APPLICATION_JSON;
rrd_stats_api_v1_charts(w->response.data);
return 200;
}
// they are not null and not empty
if(!strcmp(name, "chart")) chart = value;
- else {
- buffer_sprintf(w->response.data, "Unknown parameter '%s' in request.", name);
- goto cleanup;
- }
+ //else {
+ /// buffer_sprintf(w->response.data, "Unknown parameter '%s' in request.", name);
+ // goto cleanup;
+ //}
}
if(!chart || !*chart) {
*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;
//if(strstr(w->response.data->buffer, "X-DataSource-Auth"))
// datasource_type = DATASOURCE_GOOGLE_JSON;
- char *buf = w->response.data->buffer;
+ char *buf = (char *)buffer_tostring(w->response.data);
char *tok = strsep(&buf, " \r\n");
char *url = NULL;
char *pointer_to_free = NULL; // keep url_decode() allocated buffer
datasource_type = DATASOURCE_JSON;
code = web_client_api_request(w, url);
}
+#ifdef NETDATA_INTERNAL_CHECKS
+ else if(strcmp(tok, "exit") == 0) {
+ netdata_exit = 1;
+ code = 200;
+ w->response.data->contenttype = CT_TEXT_PLAIN;
+ buffer_flush(w->response.data);
+ buffer_strcat(w->response.data, "will do");
+ }
+#endif
else if(strcmp(tok, WEB_PATH_DATA) == 0) { // "data"
// the client is requesting rrd data
datasource_type = DATASOURCE_JSON;
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";
}
char date[100];
- struct tm tm = *gmtime(&w->response.data->date);
- strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %Z", &tm);
+ struct tm tmbuf, *tm = gmtime_r(&w->response.data->date, &tmbuf);
+ strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %Z", tm);
buffer_sprintf(w->response.header_output,
"HTTP/1.1 %d %s\r\n"
"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);
- else debug(D_DEFLATE, "%llu: Failed to send chunk header to client. Reason: %s", w->id, strerror(errno));
+ else debug(D_DEFLATE, "%llu: Failed to send chunk header to client.", w->id);
return bytes;
}
{
//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);
- else debug(D_DEFLATE, "%llu: Failed to send chunk suffix to client. Reason: %s", w->id, strerror(errno));
+ else debug(D_DEFLATE, "%llu: Failed to send chunk suffix to client.", w->id);
return bytes;
}
{
//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);
- else debug(D_DEFLATE, "%llu: Failed to send chunk suffix to client. Reason: %s", w->id, strerror(errno));
+ else debug(D_DEFLATE, "%llu: Failed to send chunk suffix to client.", w->id);
return bytes;
}
// 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;
debug(D_WEB_CLIENT, "%llu: Sent %d bytes.", w->id, bytes);
}
else if(likely(bytes == 0)) debug(D_WEB_CLIENT, "%llu: Did not send any bytes to the client.", w->id);
- else debug(D_WEB_CLIENT, "%llu: Failed to send data to client. Reason: %s", w->id, strerror(errno));
+ else debug(D_WEB_CLIENT, "%llu: Failed to send data to client.", w->id);
return(bytes);
}
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';
}
if(FD_ISSET(w->ifd, &efds)) {
- debug(D_WEB_CLIENT_ACCESS, "%llu: Received error on input socket (%s).", w->id, strerror(errno));
+ debug(D_WEB_CLIENT_ACCESS, "%llu: Received error on input socket.", w->id);
break;
}
if(FD_ISSET(w->ofd, &efds)) {
- debug(D_WEB_CLIENT_ACCESS, "%llu: Received error on output socket (%s).", w->id, strerror(errno));
+ debug(D_WEB_CLIENT_ACCESS, "%llu: Received error on output socket.", w->id);
break;
}
if(w->wait_send && FD_ISSET(w->ofd, &ofds)) {
long bytes;
if((bytes = web_client_send(w)) < 0) {
- debug(D_WEB_CLIENT, "%llu: Cannot send data to client. Closing client (ouput: %s).", w->id, strerror(errno));
+ debug(D_WEB_CLIENT, "%llu: Cannot send data to client. Closing client.", w->id);
errno = 0;
break;
}
if(w->wait_receive && FD_ISSET(w->ifd, &ifds)) {
long bytes;
if((bytes = web_client_receive(w)) < 0) {
- debug(D_WEB_CLIENT, "%llu: Cannot receive data from client. Closing client (input: %s).", w->id, strerror(errno));
+ debug(D_WEB_CLIENT, "%llu: Cannot receive data from client. Closing client.", w->id);
errno = 0;
break;
}