Rename header_or_error to header_or_short_response, as it will soon be able to contai...
[cubemap] / client.cpp
1 #include <stdio.h>
2 #include <arpa/inet.h>
3 #include <netinet/in.h>
4 #include <stdint.h>
5 #include <sys/socket.h>
6
7 #include "client.h"
8 #include "log.h"
9 #include "state.pb.h"
10 #include "stream.h"
11
12 #ifndef SO_MAX_PACING_RATE
13 #define SO_MAX_PACING_RATE 47
14 #endif
15
16 using namespace std;
17
18 Client::Client(int sock)
19         : sock(sock),
20           state(Client::READING_REQUEST),
21           stream(NULL),
22           header_or_short_response_bytes_sent(0),
23           stream_pos(0),
24           bytes_sent(0),
25           bytes_lost(0),
26           num_loss_events(0)
27 {
28         request.reserve(1024);
29
30         if (clock_gettime(CLOCK_MONOTONIC_COARSE, &connect_time) == -1) {
31                 log_perror("clock_gettime(CLOCK_MONOTONIC_COARSE)");
32                 return;
33         }
34
35         // Find the remote address, and convert it to ASCII.
36         sockaddr_in6 addr;
37         socklen_t addr_len = sizeof(addr);
38
39         if (getpeername(sock, reinterpret_cast<sockaddr *>(&addr), &addr_len) == -1) {
40                 log_perror("getpeername");
41                 remote_addr = "";
42                 return;
43         }
44
45         char buf[INET6_ADDRSTRLEN];
46         if (IN6_IS_ADDR_V4MAPPED(&addr.sin6_addr)) {
47                 // IPv4 address, really.
48                 if (inet_ntop(AF_INET, &addr.sin6_addr.s6_addr32[3], buf, sizeof(buf)) == NULL) {
49                         log_perror("inet_ntop");
50                         remote_addr = "";
51                 } else {
52                         remote_addr = buf;
53                 }
54         } else {
55                 if (inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf)) == NULL) {
56                         log_perror("inet_ntop");
57                         remote_addr = "";
58                 } else {
59                         remote_addr = buf;
60                 }
61         }
62 }
63         
64 Client::Client(const ClientProto &serialized, Stream *stream)
65         : sock(serialized.sock()),
66           remote_addr(serialized.remote_addr()),
67           referer(serialized.referer()),
68           user_agent(serialized.user_agent()),
69           state(State(serialized.state())),
70           request(serialized.request()),
71           url(serialized.url()),
72           stream(stream),
73           header_or_short_response(serialized.header_or_short_response()),
74           header_or_short_response_bytes_sent(serialized.header_or_short_response_bytes_sent()),
75           stream_pos(serialized.stream_pos()),
76           bytes_sent(serialized.bytes_sent()),
77           bytes_lost(serialized.bytes_lost()),
78           num_loss_events(serialized.num_loss_events())
79 {
80         if (stream != NULL) {
81                 if (setsockopt(sock, SOL_SOCKET, SO_MAX_PACING_RATE, &stream->pacing_rate, sizeof(stream->pacing_rate)) == -1) {
82                         if (stream->pacing_rate != ~0U) {
83                                 log_perror("setsockopt(SO_MAX_PACING_RATE)");
84                         }
85                 }
86         }
87         connect_time.tv_sec = serialized.connect_time_sec();
88         connect_time.tv_nsec = serialized.connect_time_nsec();
89 }
90
91 ClientProto Client::serialize() const
92 {
93         ClientProto serialized;
94         serialized.set_sock(sock);
95         serialized.set_remote_addr(remote_addr);
96         serialized.set_referer(referer);
97         serialized.set_user_agent(user_agent);
98         serialized.set_connect_time_sec(connect_time.tv_sec);
99         serialized.set_connect_time_nsec(connect_time.tv_nsec);
100         serialized.set_state(state);
101         serialized.set_request(request);
102         serialized.set_url(url);
103         serialized.set_header_or_short_response(header_or_short_response);
104         serialized.set_header_or_short_response_bytes_sent(serialized.header_or_short_response_bytes_sent());
105         serialized.set_stream_pos(stream_pos);
106         serialized.set_bytes_sent(bytes_sent);
107         serialized.set_bytes_lost(bytes_lost);
108         serialized.set_num_loss_events(num_loss_events);
109         return serialized;
110 }
111
112 namespace {
113
114 string escape_string(const string &str) {
115         string ret;
116         for (size_t i = 0; i < str.size(); ++i) {
117                 char buf[16];
118                 if (isprint(str[i]) && str[i] >= 32 && str[i] != '"' && str[i] != '\\') {
119                         ret.push_back(str[i]);
120                 } else {
121                         snprintf(buf, sizeof(buf), "\\x%02x", (unsigned char)str[i]);
122                         ret += buf;
123                 }
124         }
125         return ret;
126 }
127
128 } // namespace
129         
130 ClientStats Client::get_stats() const
131 {
132         ClientStats stats;
133         if (url.empty()) {
134                 stats.url = "-";
135         } else {
136                 stats.url = url;
137         }
138         stats.sock = sock;
139         stats.remote_addr = remote_addr;
140         stats.referer = escape_string(referer);
141         stats.user_agent = escape_string(user_agent);
142         stats.connect_time = connect_time;
143         stats.bytes_sent = bytes_sent;
144         stats.bytes_lost = bytes_lost;
145         stats.num_loss_events = num_loss_events;
146         return stats;
147 }