]> git.sesse.net Git - nageru/blob - httpd.cpp
Fix a leak when getting metrics.
[nageru] / httpd.cpp
1 #include "httpd.h"
2
3 #include <assert.h>
4 #include <byteswap.h>
5 #include <endian.h>
6 #include <microhttpd.h>
7 #include <netinet/in.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <sys/time.h>
11 #include <time.h>
12 #include <memory>
13
14 #include "defs.h"
15 #include "metacube2.h"
16 #include "metrics.h"
17
18 struct MHD_Connection;
19 struct MHD_Response;
20
21 using namespace std;
22
23 HTTPD::HTTPD()
24 {
25 }
26
27 HTTPD::~HTTPD()
28 {
29         if (mhd) {
30                 MHD_quiesce_daemon(mhd);
31                 for (Stream *stream : streams) {
32                         stream->stop();
33                 }
34                 MHD_stop_daemon(mhd);
35         }
36 }
37
38 void HTTPD::start(int port)
39 {
40         mhd = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL_INTERNALLY | MHD_USE_DUAL_STACK,
41                                port,
42                                nullptr, nullptr,
43                                &answer_to_connection_thunk, this,
44                                MHD_OPTION_NOTIFY_COMPLETED, nullptr, this,
45                                MHD_OPTION_END);
46         if (mhd == nullptr) {
47                 fprintf(stderr, "Warning: Could not open HTTP server. (Port already in use?)\n");
48         }
49 }
50
51 void HTTPD::add_data(const char *buf, size_t size, bool keyframe)
52 {
53         unique_lock<mutex> lock(streams_mutex);
54         for (Stream *stream : streams) {
55                 stream->add_data(buf, size, keyframe ? Stream::DATA_TYPE_KEYFRAME : Stream::DATA_TYPE_OTHER);
56         }
57 }
58
59 int HTTPD::answer_to_connection_thunk(void *cls, MHD_Connection *connection,
60                                       const char *url, const char *method,
61                                       const char *version, const char *upload_data,
62                                       size_t *upload_data_size, void **con_cls)
63 {
64         HTTPD *httpd = (HTTPD *)cls;
65         return httpd->answer_to_connection(connection, url, method, version, upload_data, upload_data_size, con_cls);
66 }
67
68 int HTTPD::answer_to_connection(MHD_Connection *connection,
69                                 const char *url, const char *method,
70                                 const char *version, const char *upload_data,
71                                 size_t *upload_data_size, void **con_cls)
72 {
73         // See if the URL ends in “.metacube”.
74         HTTPD::Stream::Framing framing;
75         if (strstr(url, ".metacube") == url + strlen(url) - strlen(".metacube")) {
76                 framing = HTTPD::Stream::FRAMING_METACUBE;
77         } else {
78                 framing = HTTPD::Stream::FRAMING_RAW;
79         }
80
81         if (strcmp(url, "/metrics") == 0) {
82                 string contents = global_metrics.serialize();
83                 MHD_Response *response = MHD_create_response_from_buffer(
84                         contents.size(), &contents[0], MHD_RESPMEM_MUST_COPY);
85                 MHD_add_response_header(response, "Content-type", "text/plain");
86                 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
87                 MHD_destroy_response(response);  // Only decreases the refcount; actual free is after the request is done.
88                 return ret;
89         }
90
91         HTTPD::Stream *stream = new HTTPD::Stream(this, framing);
92         stream->add_data(header.data(), header.size(), Stream::DATA_TYPE_HEADER);
93         {
94                 unique_lock<mutex> lock(streams_mutex);
95                 streams.insert(stream);
96         }
97         *con_cls = stream;
98
99         // Does not strictly have to be equal to MUX_BUFFER_SIZE.
100         MHD_Response *response = MHD_create_response_from_callback(
101                 (size_t)-1, MUX_BUFFER_SIZE, &HTTPD::Stream::reader_callback_thunk, stream, &HTTPD::free_stream);
102         // TODO: Content-type?
103         if (framing == HTTPD::Stream::FRAMING_METACUBE) {
104                 MHD_add_response_header(response, "Content-encoding", "metacube");
105         }
106
107         int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
108         MHD_destroy_response(response);  // Only decreases the refcount; actual free is after the request is done.
109
110         return ret;
111 }
112
113 void HTTPD::free_stream(void *cls)
114 {
115         HTTPD::Stream *stream = (HTTPD::Stream *)cls;
116         HTTPD *httpd = stream->get_parent();
117         {
118                 unique_lock<mutex> lock(httpd->streams_mutex);
119                 delete stream;
120                 httpd->streams.erase(stream);
121         }
122 }
123
124 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)
125 {
126         HTTPD::Stream *stream = (HTTPD::Stream *)cls;
127         return stream->reader_callback(pos, buf, max);
128 }
129
130 ssize_t HTTPD::Stream::reader_callback(uint64_t pos, char *buf, size_t max)
131 {
132         unique_lock<mutex> lock(buffer_mutex);
133         has_buffered_data.wait(lock, [this]{ return should_quit || !buffered_data.empty(); });
134         if (should_quit) {
135                 return 0;
136         }
137
138         ssize_t ret = 0;
139         while (max > 0 && !buffered_data.empty()) {
140                 const string &s = buffered_data.front();
141                 assert(s.size() > used_of_buffered_data);
142                 size_t len = s.size() - used_of_buffered_data;
143                 if (max >= len) {
144                         // Consume the entire (rest of the) string.
145                         memcpy(buf, s.data() + used_of_buffered_data, len);
146                         buf += len;
147                         ret += len;
148                         max -= len;
149                         buffered_data.pop_front();
150                         used_of_buffered_data = 0;
151                 } else {
152                         // We don't need the entire string; just use the first part of it.
153                         memcpy(buf, s.data() + used_of_buffered_data, max);
154                         buf += max;
155                         used_of_buffered_data += max;
156                         ret += max;
157                         max = 0;
158                 }
159         }
160
161         return ret;
162 }
163
164 void HTTPD::Stream::add_data(const char *buf, size_t buf_size, HTTPD::Stream::DataType data_type)
165 {
166         if (buf_size == 0) {
167                 return;
168         }
169         if (data_type == DATA_TYPE_KEYFRAME) {
170                 seen_keyframe = true;
171         } else if (data_type == DATA_TYPE_OTHER && !seen_keyframe) {
172                 // Start sending only once we see a keyframe.
173                 return;
174         }
175
176         unique_lock<mutex> lock(buffer_mutex);
177
178         if (framing == FRAMING_METACUBE) {
179                 metacube2_block_header hdr;
180                 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
181                 hdr.size = htonl(buf_size);
182                 int flags = 0;
183                 if (data_type == DATA_TYPE_HEADER) {
184                         flags |= METACUBE_FLAGS_HEADER;
185                 } else if (data_type == DATA_TYPE_OTHER) {
186                         flags |= METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START;
187                 }
188                 hdr.flags = htons(flags);
189                 hdr.csum = htons(metacube2_compute_crc(&hdr));
190                 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
191         }
192         buffered_data.emplace_back(buf, buf_size);
193
194         // Send a Metacube2 timestamp every keyframe.
195         if (framing == FRAMING_METACUBE && data_type == DATA_TYPE_KEYFRAME) {
196                 timespec now;
197                 clock_gettime(CLOCK_REALTIME, &now);
198
199                 metacube2_timestamp_packet packet;
200                 packet.type = htobe64(METACUBE_METADATA_TYPE_ENCODER_TIMESTAMP);
201                 packet.tv_sec = htobe64(now.tv_sec);
202                 packet.tv_nsec = htobe64(now.tv_nsec);
203
204                 metacube2_block_header hdr;
205                 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
206                 hdr.size = htonl(sizeof(packet));
207                 hdr.flags = htons(METACUBE_FLAGS_METADATA);
208                 hdr.csum = htons(metacube2_compute_crc(&hdr));
209                 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
210                 buffered_data.emplace_back((char *)&packet, sizeof(packet));
211         }
212
213         has_buffered_data.notify_all(); 
214 }
215
216 void HTTPD::Stream::stop()
217 {
218         unique_lock<mutex> lock(buffer_mutex);
219         should_quit = true;
220         has_buffered_data.notify_all();
221 }