X-Git-Url: https://arthur.barton.de/gitweb/?a=blobdiff_plain;f=src%2Fweb_client.c;h=48fd47be4bd51cec3ab9edbc6637ec9eb64b64b7;hb=6372ed837298a34f6d16e0cde929822879863195;hp=02439c3c0b70a1e4b63e979ff70ae26977bfbbfd;hpb=478472f37f17de35f58c926d92dac67242f9a224;p=netdata.git diff --git a/src/web_client.c b/src/web_client.c index 02439c3c..48fd47be 100755 --- a/src/web_client.c +++ b/src/web_client.c @@ -1,6 +1,7 @@ -// enable strcasestr() -#define _GNU_SOURCE - +#ifdef HAVE_CONFIG_H +#include +#endif +#include #include #include #include @@ -15,7 +16,7 @@ #include "common.h" #include "log.h" -#include "config.h" +#include "appconfig.h" #include "url.h" #include "web_buffer.h" #include "web_server.h" @@ -135,7 +136,7 @@ struct web_client *web_client_free(struct web_client *w) int mysendfile(struct web_client *w, char *filename) { static char *web_dir = NULL; - if(!web_dir) web_dir = config_get("global", "web files directory", "web"); + if(!web_dir) web_dir = config_get("global", "web files directory", WEB_DIR); debug(D_WEB_CLIENT, "%llu: Looking for file '%s'...", w->id, filename); @@ -224,22 +225,28 @@ void web_client_reset(struct web_client *w) struct timeval tv; gettimeofday(&tv, NULL); - long sent = w->zoutput?(long)w->zstream.total_out:((w->mode == WEB_CLIENT_MODE_FILECOPY)?w->data->rbytes:w->data->bytes); + long sent = (w->mode == WEB_CLIENT_MODE_FILECOPY)?w->data->rbytes:w->data->bytes; + +#ifdef NETDATA_WITH_ZLIB + if(likely(w->zoutput)) sent = (long)w->zstream.total_out; +#endif + long size = (w->mode == WEB_CLIENT_MODE_FILECOPY)?w->data->rbytes:w->data->bytes; - if(w->last_url[0]) log_access("%llu: (sent/all = %ld/%ld bytes %0.0f%%, prep/sent/total = %0.2f/%0.2f/%0.2f ms) %s: '%s'", - w->id, - sent, size, -((size>0)?((float)(size-sent)/(float)size * 100.0):0.0), - (float)usecdiff(&w->tv_ready, &w->tv_in) / 1000.0, - (float)usecdiff(&tv, &w->tv_ready) / 1000.0, - (float)usecdiff(&tv, &w->tv_in) / 1000.0, - (w->mode == WEB_CLIENT_MODE_FILECOPY)?"filecopy":"data", - w->last_url + if(likely(w->last_url[0])) + log_access("%llu: (sent/all = %ld/%ld bytes %0.0f%%, prep/sent/total = %0.2f/%0.2f/%0.2f ms) %s: '%s'", + w->id, + sent, size, -((size>0)?((float)(size-sent)/(float)size * 100.0):0.0), + (float)usecdiff(&w->tv_ready, &w->tv_in) / 1000.0, + (float)usecdiff(&tv, &w->tv_ready) / 1000.0, + (float)usecdiff(&tv, &w->tv_in) / 1000.0, + (w->mode == WEB_CLIENT_MODE_FILECOPY)?"filecopy":"data", + w->last_url ); debug(D_WEB_CLIENT, "%llu: Reseting client.", w->id); - if(w->mode == WEB_CLIENT_MODE_FILECOPY) { + if(unlikely(w->mode == WEB_CLIENT_MODE_FILECOPY)) { debug(D_WEB_CLIENT, "%llu: Closing filecopy input file.", w->id); close(w->ifd); w->ifd = w->ofd; @@ -260,11 +267,13 @@ void web_client_reset(struct web_client *w) w->wait_receive = 1; w->wait_send = 0; + w->zoutput = 0; + // if we had enabled compression, release it +#ifdef NETDATA_WITH_ZLIB if(w->zinitialized) { debug(D_DEFLATE, "%llu: Reseting compression.", w->id); deflateEnd(&w->zstream); - w->zoutput = 0; w->zsent = 0; w->zhave = 0; w->zstream.avail_in = 0; @@ -273,8 +282,10 @@ void web_client_reset(struct web_client *w) w->zstream.total_out = 0; w->zinitialized = 0; } +#endif // NETDATA_WITH_ZLIB } +#ifdef NETDATA_WITH_ZLIB void web_client_enable_deflate(struct web_client *w) { if(w->zinitialized == 1) { error("%llu: Compression has already be initialized for this client.", w->id); @@ -319,6 +330,7 @@ void web_client_enable_deflate(struct web_client *w) { debug(D_DEFLATE, "%llu: Initialized compression.", w->id); } +#endif // NETDATA_WITH_ZLIB int web_client_data_request(struct web_client *w, char *url, int datasource_type) { @@ -543,9 +555,11 @@ void web_client_process(struct web_client *w) { if(strcasestr(w->data->buffer, "Connection: keep-alive")) w->keepalive = 1; else w->keepalive = 0; +#ifdef NETDATA_WITH_ZLIB // check if the client accepts deflate if(web_enable_gzip && strstr(w->data->buffer, "gzip")) web_client_enable_deflate(w); +#endif // NETDATA_WITH_ZLIB int datasource_type = DATASOURCE_GOOGLE_JSONP; //if(strstr(w->data->buffer, "X-DataSource-Auth")) @@ -974,6 +988,7 @@ long web_client_send_chunk_finalize(struct web_client *w) return bytes; } +#ifdef NETDATA_WITH_ZLIB long web_client_send_deflate(struct web_client *w) { long bytes = 0, t = 0; @@ -1074,14 +1089,17 @@ long web_client_send_deflate(struct web_client *w) return(bytes); } +#endif // NETDATA_WITH_ZLIB long web_client_send(struct web_client *w) { - if(w->zoutput) return web_client_send_deflate(w); +#ifdef NETDATA_WITH_ZLIB + if(likely(w->zoutput)) return web_client_send_deflate(w); +#endif // NETDATA_WITH_ZLIB long bytes; - if(w->data->bytes - w->data->sent == 0) { + if(unlikely(w->data->bytes - w->data->sent == 0)) { // there is nothing to send debug(D_WEB_CLIENT, "%llu: Out of output data.", w->id); @@ -1097,7 +1115,7 @@ long web_client_send(struct web_client *w) return(0); } - if(w->keepalive == 0) { + if(unlikely(w->keepalive == 0)) { debug(D_WEB_CLIENT, "%llu: Closing (keep-alive is not enabled). %ld bytes sent.", w->id, w->data->sent); errno = 0; return(-1); @@ -1109,14 +1127,13 @@ long web_client_send(struct web_client *w) } bytes = send(w->ofd, &w->data->buffer[w->data->sent], w->data->bytes - w->data->sent, MSG_DONTWAIT); - if(bytes > 0) { + if(likely(bytes > 0)) { w->data->sent += bytes; debug(D_WEB_CLIENT, "%llu: Sent %d bytes.", w->id, bytes); } - else if(bytes == 0) debug(D_WEB_CLIENT, "%llu: Did not send any bytes to the client.", w->id); + 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)); - return(bytes); } @@ -1128,12 +1145,12 @@ long web_client_receive(struct web_client *w) long left = w->data->size - w->data->bytes; long bytes; - if(w->mode == WEB_CLIENT_MODE_FILECOPY) + if(unlikely(w->mode == WEB_CLIENT_MODE_FILECOPY)) bytes = read(w->ifd, &w->data->buffer[w->data->bytes], (left-1)); else bytes = recv(w->ifd, &w->data->buffer[w->data->bytes], left-1, MSG_DONTWAIT); - if(bytes > 0) { + if(likely(bytes > 0)) { int old = w->data->bytes; w->data->bytes += bytes; w->data->buffer[w->data->bytes] = '\0'; @@ -1146,7 +1163,7 @@ long web_client_receive(struct web_client *w) if(w->data->rbytes && w->data->bytes >= w->data->rbytes) w->wait_receive = 0; } } - else if(bytes == 0) { + else if(likely(bytes == 0)) { debug(D_WEB_CLIENT, "%llu: Out of input data.", w->id); // if we cannot read, it means we have an error on input. @@ -1191,6 +1208,8 @@ void *web_client_main(void *ptr) fd_set ifds, ofds, efds; int fdmax = 0; + log_access("%llu: %s port %s connected on thread task id %d", w->id, w->client_ip, w->client_port, gettid()); + for(;;) { FD_ZERO (&ifds); FD_ZERO (&ofds); @@ -1269,6 +1288,7 @@ void *web_client_main(void *ptr) } } + log_access("%llu: %s port %s disconnected from thread task id %d", w->id, w->client_ip, w->client_port, gettid()); debug(D_WEB_CLIENT, "%llu: done...", w->id); web_client_reset(w);