]> git.sesse.net Git - nageru/blob - httpd.cpp
Shutdown microhttpd on shutdown.
[nageru] / httpd.cpp
1 #include <arpa/inet.h>
2 #include <assert.h>
3 #include <microhttpd.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include <vector>
9
10 #include "httpd.h"
11
12 #include "defs.h"
13 #include "flags.h"
14 #include "metacube2.h"
15 #include "timebase.h"
16
17 struct MHD_Connection;
18 struct MHD_Response;
19
20 using namespace std;
21
22 HTTPD::HTTPD()
23 {
24 }
25
26 HTTPD::~HTTPD()
27 {
28         MHD_quiesce_daemon(mhd);
29         for (Stream *stream : streams) {
30                 stream->stop();
31         }
32         MHD_stop_daemon(mhd);
33 }
34
35 void HTTPD::start(int port)
36 {
37         mhd = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL_INTERNALLY | MHD_USE_DUAL_STACK,
38                                port,
39                                nullptr, nullptr,
40                                &answer_to_connection_thunk, this,
41                                MHD_OPTION_NOTIFY_COMPLETED, &request_completed_thunk, this,
42                                MHD_OPTION_END);
43 }
44
45 void HTTPD::add_data(const char *buf, size_t size, bool keyframe)
46 {
47         unique_lock<mutex> lock(streams_mutex);
48         for (Stream *stream : streams) {
49                 stream->add_data(buf, size, keyframe ? Stream::DATA_TYPE_KEYFRAME : Stream::DATA_TYPE_OTHER);
50         }
51 }
52
53 int HTTPD::answer_to_connection_thunk(void *cls, MHD_Connection *connection,
54                                       const char *url, const char *method,
55                                       const char *version, const char *upload_data,
56                                       size_t *upload_data_size, void **con_cls)
57 {
58         HTTPD *httpd = (HTTPD *)cls;
59         return httpd->answer_to_connection(connection, url, method, version, upload_data, upload_data_size, con_cls);
60 }
61
62 int HTTPD::answer_to_connection(MHD_Connection *connection,
63                                 const char *url, const char *method,
64                                 const char *version, const char *upload_data,
65                                 size_t *upload_data_size, void **con_cls)
66 {
67         // See if the URL ends in “.metacube”.
68         HTTPD::Stream::Framing framing;
69         if (strstr(url, ".metacube") == url + strlen(url) - strlen(".metacube")) {
70                 framing = HTTPD::Stream::FRAMING_METACUBE;
71         } else {
72                 framing = HTTPD::Stream::FRAMING_RAW;
73         }
74
75         HTTPD::Stream *stream = new HTTPD::Stream(framing);
76         stream->add_data(header.data(), header.size(), Stream::DATA_TYPE_HEADER);
77         {
78                 unique_lock<mutex> lock(streams_mutex);
79                 streams.insert(stream);
80         }
81         *con_cls = stream;
82
83         // Does not strictly have to be equal to MUX_BUFFER_SIZE.
84         MHD_Response *response = MHD_create_response_from_callback(
85                 (size_t)-1, MUX_BUFFER_SIZE, &HTTPD::Stream::reader_callback_thunk, stream, &HTTPD::free_stream);
86
87         // TODO: Content-type?
88         if (framing == HTTPD::Stream::FRAMING_METACUBE) {
89                 MHD_add_response_header(response, "Content-encoding", "metacube");
90         }
91         int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
92         //MHD_destroy_response(response);
93
94         return ret;
95 }
96
97 void HTTPD::free_stream(void *cls)
98 {
99         // FIXME: When is this actually called, if ever?
100         // Also, shouldn't we remove it from streams?
101         HTTPD::Stream *stream = (HTTPD::Stream *)cls;
102         delete stream;
103 }
104
105 void HTTPD::request_completed_thunk(void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
106 {
107         HTTPD *httpd = (HTTPD *)cls;
108         return httpd->request_completed(connection, con_cls, toe);
109 }
110
111 void HTTPD::request_completed(struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
112 {
113         if (con_cls == nullptr) {
114                 // Request was never set up.
115                 return;
116         }
117         HTTPD::Stream *stream = (HTTPD::Stream *)*con_cls;
118         {
119                 unique_lock<mutex> lock(streams_mutex);
120                 delete stream;
121                 streams.erase(stream);
122         }
123 }
124
125 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)
126 {
127         HTTPD::Stream *stream = (HTTPD::Stream *)cls;
128         return stream->reader_callback(pos, buf, max);
129 }
130
131 ssize_t HTTPD::Stream::reader_callback(uint64_t pos, char *buf, size_t max)
132 {
133         unique_lock<mutex> lock(buffer_mutex);
134         has_buffered_data.wait(lock, [this]{ return should_quit || !buffered_data.empty(); });
135         if (should_quit) {
136                 return 0;
137         }
138
139         ssize_t ret = 0;
140         while (max > 0 && !buffered_data.empty()) {
141                 const string &s = buffered_data.front();
142                 assert(s.size() > used_of_buffered_data);
143                 size_t len = s.size() - used_of_buffered_data;
144                 if (max >= len) {
145                         // Consume the entire (rest of the) string.
146                         memcpy(buf, s.data() + used_of_buffered_data, len);
147                         buf += len;
148                         ret += len;
149                         max -= len;
150                         buffered_data.pop_front();
151                         used_of_buffered_data = 0;
152                 } else {
153                         // We don't need the entire string; just use the first part of it.
154                         memcpy(buf, s.data() + used_of_buffered_data, max);
155                         buf += max;
156                         used_of_buffered_data += max;
157                         ret += max;
158                         max = 0;
159                 }
160         }
161
162         return ret;
163 }
164
165 void HTTPD::Stream::add_data(const char *buf, size_t buf_size, HTTPD::Stream::DataType data_type)
166 {
167         if (buf_size == 0) {
168                 return;
169         }
170         if (data_type == DATA_TYPE_KEYFRAME) {
171                 seen_keyframe = true;
172         } else if (data_type == DATA_TYPE_OTHER && !seen_keyframe) {
173                 // Start sending only once we see a keyframe.
174                 return;
175         }
176
177         unique_lock<mutex> lock(buffer_mutex);
178
179         if (framing == FRAMING_METACUBE) {
180                 metacube2_block_header hdr;
181                 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
182                 hdr.size = htonl(buf_size);
183                 int flags = 0;
184                 if (data_type == DATA_TYPE_HEADER) {
185                         flags |= METACUBE_FLAGS_HEADER;
186                 } else if (data_type == DATA_TYPE_OTHER) {
187                         flags |= METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START;
188                 }
189                 hdr.flags = htons(flags);
190                 hdr.csum = htons(metacube2_compute_crc(&hdr));
191                 buffered_data.emplace_back((char *)&hdr, sizeof(hdr));
192         }
193         buffered_data.emplace_back(buf, buf_size);
194         has_buffered_data.notify_all(); 
195 }
196
197 void HTTPD::Stream::stop()
198 {
199         unique_lock<mutex> lock(buffer_mutex);
200         should_quit = true;
201         has_buffered_data.notify_all();
202 }