]> git.sesse.net Git - nageru/blob - httpd.cpp
Separate muxing entirely out of the HTTPD class.
[nageru] / httpd.cpp
1 #include <assert.h>
2 #include <microhttpd.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include <vector>
8
9 #include "httpd.h"
10
11 #include "defs.h"
12 #include "flags.h"
13 #include "timebase.h"
14
15 struct MHD_Connection;
16 struct MHD_Response;
17
18 using namespace std;
19
20 HTTPD::HTTPD()
21 {
22 }
23
24 void HTTPD::start(int port)
25 {
26         MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL_INTERNALLY | MHD_USE_DUAL_STACK,
27                          port,
28                          nullptr, nullptr,
29                          &answer_to_connection_thunk, this,
30                          MHD_OPTION_NOTIFY_COMPLETED, &request_completed_thunk, this, 
31                          MHD_OPTION_END);
32 }
33
34 void HTTPD::add_data(const char *buf, size_t size, bool keyframe)
35 {
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);
39         }
40 }
41
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)
46 {
47         HTTPD *httpd = (HTTPD *)cls;
48         return httpd->answer_to_connection(connection, url, method, version, upload_data, upload_data_size, con_cls);
49 }
50
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)
55 {
56         HTTPD::Stream *stream = new HTTPD::Stream;
57         stream->add_data(header.data(), header.size(), Stream::DATA_TYPE_HEADER);
58         {
59                 unique_lock<mutex> lock(streams_mutex);
60                 streams.insert(stream);
61         }
62         *con_cls = stream;
63
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);
69
70         return ret;
71 }
72
73 void HTTPD::free_stream(void *cls)
74 {
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;
78         delete stream;
79 }
80
81 void HTTPD::request_completed_thunk(void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
82 {
83         HTTPD *httpd = (HTTPD *)cls;
84         return httpd->request_completed(connection, con_cls, toe);
85 }
86
87 void HTTPD::request_completed(struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
88 {
89         if (con_cls == nullptr) {
90                 // Request was never set up.
91                 return;
92         }
93         HTTPD::Stream *stream = (HTTPD::Stream *)*con_cls;
94         {
95                 unique_lock<mutex> lock(streams_mutex);
96                 delete stream;
97                 streams.erase(stream);
98         }
99 }
100
101 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)
102 {
103         HTTPD::Stream *stream = (HTTPD::Stream *)cls;
104         return stream->reader_callback(pos, buf, max);
105 }
106
107 ssize_t HTTPD::Stream::reader_callback(uint64_t pos, char *buf, size_t max)
108 {
109         unique_lock<mutex> lock(buffer_mutex);
110         has_buffered_data.wait(lock, [this]{ return !buffered_data.empty(); });
111
112         ssize_t ret = 0;
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;
117                 if (max >= len) {
118                         // Consume the entire (rest of the) string.
119                         memcpy(buf, s.data() + used_of_buffered_data, len);
120                         buf += len;
121                         ret += len;
122                         max -= len;
123                         buffered_data.pop_front();
124                         used_of_buffered_data = 0;
125                 } else {
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);
128                         buf += max;
129                         used_of_buffered_data += max;
130                         ret += max;
131                         max = 0;
132                 }
133         }
134
135         return ret;
136 }
137
138 void HTTPD::Stream::add_data(const char *buf, size_t buf_size, HTTPD::Stream::DataType data_type)
139 {
140         if (buf_size == 0) {
141                 return;
142         }
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.
147                 return;
148         }
149
150         unique_lock<mutex> lock(buffer_mutex);
151         buffered_data.emplace_back(buf, buf_size);
152         has_buffered_data.notify_all(); 
153 }
154