-
Notifications
You must be signed in to change notification settings - Fork 7
/
lmhttpd.c
311 lines (265 loc) · 8.69 KB
/
lmhttpd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef LMHTTPD_H
#define LMHTTPD_H
#define MAX_CLIENTS 16
#define INITIAL_BUFSIZE 16 * 1024 * 1024
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <string.h>
#include <stdio.h>
#include <pthread.h>
#include <microhttpd.h>
#include <libavutil/log.h>
#include <libavutil/fifo.h>
#include <libavutil/mem.h>
#include "httpd.h"
struct MHDServer {
struct MHD_Daemon *daemon;
AVFifo *clients;
};
struct ConnectionInfo {
AVFifo *buffer;
pthread_mutex_t buffer_lock;
struct MHD_Connection *connection;
int close_connection;
};
/**
* Helper callback that fills the libmicrohttpd-client buffer with data.
*/
ssize_t helper_callback(void *cls, uint64_t pos, char *buf, size_t max)
{
struct ConnectionInfo *cinfo = (struct ConnectionInfo*) cls;
pthread_mutex_lock(&cinfo->buffer_lock);
int buf_size = av_fifo_can_read(cinfo->buffer);
if (buf_size > 0) {
max = max > buf_size ? buf_size : max;
av_fifo_read(cinfo->buffer, buf, max);
} else {
max = 0;
}
if (max == 0 && cinfo->close_connection) {
av_fifo_freep2(&cinfo->buffer);
pthread_mutex_unlock(&cinfo->buffer_lock);
av_free(cinfo);
return MHD_CONTENT_READER_END_OF_STREAM;
}
pthread_mutex_unlock(&cinfo->buffer_lock);
return max;
}
/**
* Free allocated callback params. Usually passed to MHD_create_response_from_callback, however it frees data too soon.
*/
static void free_callback_param (void *cls)
{
av_free(cls);
}
/**
* Callback that handles incoming connections.
*
* Incoming connections are initialized and added to a queue of new clients.
*/
static enum MHD_Result answer_to_connection (void *cls, struct MHD_Connection *connection,
const char *url, const char *method,
const char *version, const char *upload_data,
size_t *upload_data_size, void **con_cls)
{
static int aptr;
struct MHD_Response *response;
struct HTTPClient *client;
int ret;
struct MHDServer *server = (struct MHDServer*) cls;
if (&aptr != *con_cls)
{
/* do never respond on first call (why? this is in every example... something about keeping track of con_cls?) */
*con_cls = &aptr;
return MHD_YES;
}
*con_cls = NULL;
struct ConnectionInfo *cinfo = av_malloc(sizeof(struct ConnectionInfo));
if (!cinfo)
return MHD_NO;
pthread_mutex_init(&cinfo->buffer_lock, NULL);
if (MHD_set_connection_option(connection, MHD_CONNECTION_OPTION_TIMEOUT, (unsigned int) 10) != MHD_YES)
return MHD_NO;
av_log(NULL, AV_LOG_ERROR, "Accepted new client %s %s %p\n", method, url, connection);
client = av_malloc(sizeof(struct HTTPClient));
if (!client)
return MHD_NO;
// no locking needed, running on the same thread
if (!strcmp("GET", method)) {
if (av_fifo_can_write(server->clients) > sizeof(struct HTTPClient*)) {
cinfo->buffer = av_fifo_alloc2(INITIAL_BUFSIZE, 1, 0);
cinfo->connection = connection;
cinfo->close_connection = 0;
if (!cinfo->buffer)
return MHD_NO;
*con_cls = cinfo;
client->resource = av_strdup(url);
client->method = av_strdup(method);
client->httpd_data = cinfo;
av_fifo_write(server->clients, &client, 1);
}
response = MHD_create_response_from_callback(MHD_SIZE_UNKNOWN,
1024,
&helper_callback,
cinfo,
NULL);
} else {
response = MHD_create_response_from_buffer (0,
(void *) "",
MHD_RESPMEM_MUST_COPY);
}
ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
MHD_destroy_response (response);
return ret;
}
/**
* Initialize the libmicrohttpd server with a config.
*
* Allocates and starts daemon.
*/
int lmhttpd_init(void **server, struct HTTPDConfig config) {
struct MHDServer *server_p = av_malloc(sizeof(struct MHDServer));
*server = NULL;
if (!server_p) {
av_log(NULL, AV_LOG_ERROR, "Could not allocate MHDServer struct\n");
return -1;
}
server_p->clients = av_fifo_alloc2(MAX_CLIENTS, sizeof(struct HTTPClient), 0);
server_p->daemon = MHD_start_daemon (0, config.port, NULL, NULL,
&answer_to_connection, server_p, MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 10, MHD_OPTION_END);
if (!server_p->daemon || !server_p->clients) {
av_log(NULL, AV_LOG_ERROR, "Could not allocate MHD_Daemon\n");
return -1;
}
*server = server_p;
return 0;
}
/**
* Return a single new client that connected and run webserver write and read operations.
*
* This basically synchronizes the libmicrohttpd client API through a queue.
*/
int lmhttpd_accept(void *server, struct HTTPClient **client, const char **valid_files)
{
fd_set rs;
fd_set ws;
fd_set es;
struct timeval tv;
MHD_socket max;
//MHD_UNSIGNED_LONG_LONG mhd_timeout;
struct MHDServer *s = (struct MHDServer*) server;
max = 0;
FD_ZERO (&rs);
FD_ZERO (&ws);
FD_ZERO (&es);
if (MHD_get_fdset (s->daemon, &rs, &ws, &es, &max) != MHD_YES)
return HTTPD_OTHER_ERROR;
tv.tv_sec = 0;
tv.tv_usec = 500000; // 0.5 seconds
if (select (max + 1, &rs, &ws, &es, &tv) == -1)
return HTTPD_OTHER_ERROR;
// run read and write operations
if (MHD_run_from_select(s->daemon, &rs, &ws, &es) != MHD_YES)
return HTTPD_OTHER_ERROR;
if(av_fifo_can_read(s->clients)) {
av_fifo_read(s->clients, client, 1);
return 0;
}
return HTTPD_LISTEN_TIMEOUT;
}
/**
* Write data into a client buffer managed by libmicrohttpd.
*/
int lmhttpd_write(void *server, struct HTTPClient *client, const unsigned char *buf, int size)
{
struct ConnectionInfo* cinfo = (struct ConnectionInfo*) client->httpd_data;
int ret;
pthread_mutex_lock(&cinfo->buffer_lock);
if (!cinfo->close_connection && av_fifo_can_write(cinfo->buffer) >= size) {
ret = av_fifo_write(cinfo->buffer, (void*) buf, size);
} else {
ret = -1;
}
pthread_mutex_unlock(&cinfo->buffer_lock);
if (cinfo->close_connection) {
free_callback_param(cinfo);
ret = -1;
}
return ret;
}
/**
* Unimplemented
*/
int lmhttpd_read(void *server, struct HTTPClient *client, unsigned char *buf, int size)
{
return 0;
}
/**
* Close a connection by signaling to close it through the ConnectionInfo struct.
*/
void lmhttpd_close(void *server, struct HTTPClient *client)
{
struct ConnectionInfo* cinfo = (struct ConnectionInfo*) client->httpd_data;
cinfo->close_connection = 1;
av_free(client->method);
av_free(client->resource);
av_free(client);
}
/**
* Shutdown the libmicrohttpd daemon.
*/
void lmhttpd_shutdown(void *server)
{
struct MHDServer *mhd_server = (struct MHDServer*) server;
fd_set rs;
fd_set ws;
fd_set es;
struct timeval tv;
MHD_socket max;
tv.tv_sec = 0;
tv.tv_usec = 500000; // 0.5 seconds
MHD_quiesce_daemon(mhd_server->daemon);
while (1) {
max = 0;
FD_ZERO (&rs);
FD_ZERO (&ws);
FD_ZERO (&es);
if (MHD_get_fdset (mhd_server->daemon, &rs, &ws, &es, &max) != MHD_YES)
break;
if (max == 0)
break;
if (select (max + 1, &rs, &ws, &es, &tv) == -1)
break;
if (MHD_run_from_select(mhd_server->daemon, &rs, &ws, &es) != MHD_YES)
break;
}
MHD_stop_daemon(mhd_server->daemon);
av_fifo_freep2(&mhd_server->clients);
av_free(mhd_server);
}
struct HTTPDInterface lmhttpd = {
.init = lmhttpd_init,
.accept = lmhttpd_accept,
.write = lmhttpd_write,
.close = lmhttpd_close,
.shutdown = lmhttpd_shutdown,
};
#endif