2 #include <microhttpd.h>
15 struct MHD_Connection;
24 void HTTPD::start(int port)
26 MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL_INTERNALLY | MHD_USE_DUAL_STACK,
29 &answer_to_connection_thunk, this,
30 MHD_OPTION_NOTIFY_COMPLETED, &request_completed_thunk, this,
34 void HTTPD::add_data(const char *buf, size_t size, bool keyframe)
36 unique_lock<mutex> lock(streams_mutex);
37 for (Stream *stream : streams) {
38 stream->add_data(buf, size, keyframe ? Stream::DATA_TYPE_KEYFRAME : Stream::DATA_TYPE_OTHER);
42 int HTTPD::answer_to_connection_thunk(void *cls, MHD_Connection *connection,
43 const char *url, const char *method,
44 const char *version, const char *upload_data,
45 size_t *upload_data_size, void **con_cls)
47 HTTPD *httpd = (HTTPD *)cls;
48 return httpd->answer_to_connection(connection, url, method, version, upload_data, upload_data_size, con_cls);
51 int HTTPD::answer_to_connection(MHD_Connection *connection,
52 const char *url, const char *method,
53 const char *version, const char *upload_data,
54 size_t *upload_data_size, void **con_cls)
56 HTTPD::Stream *stream = new HTTPD::Stream;
57 stream->add_data(header.data(), header.size(), Stream::DATA_TYPE_HEADER);
59 unique_lock<mutex> lock(streams_mutex);
60 streams.insert(stream);
64 // Does not strictly have to be equal to MUX_BUFFER_SIZE.
65 MHD_Response *response = MHD_create_response_from_callback(
66 (size_t)-1, MUX_BUFFER_SIZE, &HTTPD::Stream::reader_callback_thunk, stream, &HTTPD::free_stream);
67 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
68 //MHD_destroy_response(response);
73 void HTTPD::free_stream(void *cls)
75 // FIXME: When is this actually called, if ever?
76 // Also, shouldn't we remove it from streams?
77 HTTPD::Stream *stream = (HTTPD::Stream *)cls;
81 void HTTPD::request_completed_thunk(void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
83 HTTPD *httpd = (HTTPD *)cls;
84 return httpd->request_completed(connection, con_cls, toe);
87 void HTTPD::request_completed(struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
89 if (con_cls == nullptr) {
90 // Request was never set up.
93 HTTPD::Stream *stream = (HTTPD::Stream *)*con_cls;
95 unique_lock<mutex> lock(streams_mutex);
97 streams.erase(stream);
101 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)
103 HTTPD::Stream *stream = (HTTPD::Stream *)cls;
104 return stream->reader_callback(pos, buf, max);
107 ssize_t HTTPD::Stream::reader_callback(uint64_t pos, char *buf, size_t max)
109 unique_lock<mutex> lock(buffer_mutex);
110 has_buffered_data.wait(lock, [this]{ return !buffered_data.empty(); });
113 while (max > 0 && !buffered_data.empty()) {
114 const string &s = buffered_data.front();
115 assert(s.size() > used_of_buffered_data);
116 size_t len = s.size() - used_of_buffered_data;
118 // Consume the entire (rest of the) string.
119 memcpy(buf, s.data() + used_of_buffered_data, len);
123 buffered_data.pop_front();
124 used_of_buffered_data = 0;
126 // We don't need the entire string; just use the first part of it.
127 memcpy(buf, s.data() + used_of_buffered_data, max);
129 used_of_buffered_data += max;
138 void HTTPD::Stream::add_data(const char *buf, size_t buf_size, HTTPD::Stream::DataType data_type)
143 if (data_type == DATA_TYPE_KEYFRAME) {
144 seen_keyframe = true;
145 } else if (data_type == DATA_TYPE_OTHER && !seen_keyframe) {
146 // Start sending only once we see a keyframe.
150 unique_lock<mutex> lock(buffer_mutex);
151 buffered_data.emplace_back(buf, buf_size);
152 has_buffered_data.notify_all();