]> git.sesse.net Git - cubemap/blobdiff - client.cpp
Rename header_or_error to header_or_short_response, as it will soon be able to contai...
[cubemap] / client.cpp
index b226d5e3fb916478f9dd5edb94cadb358d9c0dd1..77fad722a6ba9881ad44cc1e3c949beba1a580e9 100644 (file)
@@ -1,3 +1,4 @@
+#include <stdio.h>
 #include <arpa/inet.h>
 #include <netinet/in.h>
 #include <stdint.h>
@@ -16,10 +17,9 @@ using namespace std;
 
 Client::Client(int sock)
        : sock(sock),
-         connect_time(time(NULL)),
          state(Client::READING_REQUEST),
          stream(NULL),
-         header_or_error_bytes_sent(0),
+         header_or_short_response_bytes_sent(0),
          stream_pos(0),
          bytes_sent(0),
          bytes_lost(0),
@@ -27,6 +27,11 @@ Client::Client(int sock)
 {
        request.reserve(1024);
 
+       if (clock_gettime(CLOCK_MONOTONIC_COARSE, &connect_time) == -1) {
+               log_perror("clock_gettime(CLOCK_MONOTONIC_COARSE)");
+               return;
+       }
+
        // Find the remote address, and convert it to ASCII.
        sockaddr_in6 addr;
        socklen_t addr_len = sizeof(addr);
@@ -59,13 +64,14 @@ Client::Client(int sock)
 Client::Client(const ClientProto &serialized, Stream *stream)
        : sock(serialized.sock()),
          remote_addr(serialized.remote_addr()),
-         connect_time(serialized.connect_time()),
+         referer(serialized.referer()),
+         user_agent(serialized.user_agent()),
          state(State(serialized.state())),
          request(serialized.request()),
          url(serialized.url()),
          stream(stream),
-         header_or_error(serialized.header_or_error()),
-         header_or_error_bytes_sent(serialized.header_or_error_bytes_sent()),
+         header_or_short_response(serialized.header_or_short_response()),
+         header_or_short_response_bytes_sent(serialized.header_or_short_response_bytes_sent()),
          stream_pos(serialized.stream_pos()),
          bytes_sent(serialized.bytes_sent()),
          bytes_lost(serialized.bytes_lost()),
@@ -78,6 +84,8 @@ Client::Client(const ClientProto &serialized, Stream *stream)
                        }
                }
        }
+       connect_time.tv_sec = serialized.connect_time_sec();
+       connect_time.tv_nsec = serialized.connect_time_nsec();
 }
 
 ClientProto Client::serialize() const
@@ -85,18 +93,39 @@ ClientProto Client::serialize() const
        ClientProto serialized;
        serialized.set_sock(sock);
        serialized.set_remote_addr(remote_addr);
-       serialized.set_connect_time(connect_time);
+       serialized.set_referer(referer);
+       serialized.set_user_agent(user_agent);
+       serialized.set_connect_time_sec(connect_time.tv_sec);
+       serialized.set_connect_time_nsec(connect_time.tv_nsec);
        serialized.set_state(state);
        serialized.set_request(request);
        serialized.set_url(url);
-       serialized.set_header_or_error(header_or_error);
-       serialized.set_header_or_error_bytes_sent(serialized.header_or_error_bytes_sent());
+       serialized.set_header_or_short_response(header_or_short_response);
+       serialized.set_header_or_short_response_bytes_sent(serialized.header_or_short_response_bytes_sent());
        serialized.set_stream_pos(stream_pos);
        serialized.set_bytes_sent(bytes_sent);
        serialized.set_bytes_lost(bytes_lost);
        serialized.set_num_loss_events(num_loss_events);
        return serialized;
 }
+
+namespace {
+
+string escape_string(const string &str) {
+       string ret;
+       for (size_t i = 0; i < str.size(); ++i) {
+               char buf[16];
+               if (isprint(str[i]) && str[i] >= 32 && str[i] != '"' && str[i] != '\\') {
+                       ret.push_back(str[i]);
+               } else {
+                       snprintf(buf, sizeof(buf), "\\x%02x", (unsigned char)str[i]);
+                       ret += buf;
+               }
+       }
+       return ret;
+}
+
+} // namespace
        
 ClientStats Client::get_stats() const
 {
@@ -108,6 +137,8 @@ ClientStats Client::get_stats() const
        }
        stats.sock = sock;
        stats.remote_addr = remote_addr;
+       stats.referer = escape_string(referer);
+       stats.user_agent = escape_string(user_agent);
        stats.connect_time = connect_time;
        stats.bytes_sent = bytes_sent;
        stats.bytes_lost = bytes_lost;