-// enable strcasestr()
-#define _GNU_SOURCE
-
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "common.h"
#include "log.h"
-#include "config.h"
+#include "appconfig.h"
#include "url.h"
#include "web_buffer.h"
#include "web_server.h"
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);
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;
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;
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);
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)
{
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"))
return bytes;
}
+#ifdef NETDATA_WITH_ZLIB
long web_client_send_deflate(struct web_client *w)
{
long bytes = 0, t = 0;
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);
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);
}
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);
}
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';
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.
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);
}
}
+ 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);