mediacodec: check exceptions after createByCodecName()
[vlc.git] / src / network / httpd.c
blobb8fb9790de9102419ea5b3651745a5ee7b4ef8ec
1 /*****************************************************************************
2 * httpd.c
3 *****************************************************************************
4 * Copyright (C) 2004-2006 VLC authors and VideoLAN
5 * Copyright © 2004-2007 Rémi Denis-Courmont
6 * $Id$
8 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * Rémi Denis-Courmont <rem # videolan.org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <vlc_common.h>
31 #include <vlc_httpd.h>
33 #include <assert.h>
35 #include <vlc_network.h>
36 #include <vlc_tls.h>
37 #include <vlc_strings.h>
38 #include <vlc_rand.h>
39 #include <vlc_charset.h>
40 #include <vlc_url.h>
41 #include <vlc_mime.h>
42 #include <vlc_block.h>
43 #include "../libvlc.h"
45 #include <string.h>
46 #include <errno.h>
47 #include <unistd.h>
49 #ifdef HAVE_POLL
50 # include <poll.h>
51 #endif
53 #if defined(_WIN32)
54 # include <winsock2.h>
55 #else
56 # include <sys/socket.h>
57 #endif
59 #if defined(_WIN32)
60 /* We need HUGE buffer otherwise TCP throughput is very limited */
61 #define HTTPD_CL_BUFSIZE 1000000
62 #else
63 #define HTTPD_CL_BUFSIZE 10000
64 #endif
66 static void httpd_ClientClean(httpd_client_t *cl);
67 static void httpd_AppendData(httpd_stream_t *stream, uint8_t *p_data, int i_data);
69 /* each host run in his own thread */
70 struct httpd_host_t
72 VLC_COMMON_MEMBERS
74 /* ref count */
75 unsigned i_ref;
77 /* address/port and socket for listening at connections */
78 int *fds;
79 unsigned nfd;
80 unsigned port;
82 vlc_thread_t thread;
83 vlc_mutex_t lock;
84 vlc_cond_t wait;
86 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
87 * This will slow down the url research but make my live easier
88 * All url will have their cb trigger, but only the first one can answer
89 * */
90 int i_url;
91 httpd_url_t **url;
93 int i_client;
94 httpd_client_t **client;
96 /* TLS data */
97 vlc_tls_creds_t *p_tls;
101 struct httpd_url_t
103 httpd_host_t *host;
105 vlc_mutex_t lock;
107 char *psz_url;
108 char *psz_user;
109 char *psz_password;
111 struct
113 httpd_callback_t cb;
114 httpd_callback_sys_t *p_sys;
115 } catch[HTTPD_MSG_MAX];
118 /* status */
119 enum
121 HTTPD_CLIENT_RECEIVING,
122 HTTPD_CLIENT_RECEIVE_DONE,
124 HTTPD_CLIENT_SENDING,
125 HTTPD_CLIENT_SEND_DONE,
127 HTTPD_CLIENT_WAITING,
129 HTTPD_CLIENT_DEAD,
131 HTTPD_CLIENT_TLS_HS_IN,
132 HTTPD_CLIENT_TLS_HS_OUT
135 /* mode */
136 enum
138 HTTPD_CLIENT_FILE, /* default */
139 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
142 struct httpd_client_t
144 httpd_url_t *url;
146 int i_ref;
148 int fd;
150 bool b_stream_mode;
151 uint8_t i_state;
153 mtime_t i_activity_date;
154 mtime_t i_activity_timeout;
156 /* buffer for reading header */
157 int i_buffer_size;
158 int i_buffer;
159 uint8_t *p_buffer;
162 * If waiting for a keyframe, this is the position (in bytes) of the
163 * last keyframe the stream saw before this client connected.
164 * Otherwise, -1.
166 int64_t i_keyframe_wait_to_pass;
168 /* */
169 httpd_message_t query; /* client -> httpd */
170 httpd_message_t answer; /* httpd -> client */
172 /* TLS data */
173 vlc_tls_t *p_tls;
177 /*****************************************************************************
178 * Various functions
179 *****************************************************************************/
180 static const char *httpd_ReasonFromCode(unsigned i_code)
182 typedef struct
184 unsigned i_code;
185 const char psz_reason[36];
186 } http_status_info;
188 static const http_status_info http_reason[] =
190 /*{ 100, "Continue" },
191 { 101, "Switching Protocols" },*/
192 { 200, "OK" },
193 /*{ 201, "Created" },
194 { 202, "Accepted" },
195 { 203, "Non-authoritative information" },
196 { 204, "No content" },
197 { 205, "Reset content" },
198 { 206, "Partial content" },
199 { 250, "Low on storage space" },
200 { 300, "Multiple choices" },*/
201 { 301, "Moved permanently" },
202 /*{ 302, "Moved temporarily" },
203 { 303, "See other" },
204 { 304, "Not modified" },
205 { 305, "Use proxy" },
206 { 307, "Temporary redirect" },
207 { 400, "Bad request" },*/
208 { 401, "Unauthorized" },
209 /*{ 402, "Payment Required" },*/
210 { 403, "Forbidden" },
211 { 404, "Not found" },
212 { 405, "Method not allowed" },
213 /*{ 406, "Not acceptable" },
214 { 407, "Proxy authentication required" },
215 { 408, "Request time-out" },
216 { 409, "Conflict" },
217 { 410, "Gone" },
218 { 411, "Length required" },
219 { 412, "Precondition failed" },
220 { 413, "Request entity too large" },
221 { 414, "Request-URI too large" },
222 { 415, "Unsupported media Type" },
223 { 416, "Requested range not satisfiable" },
224 { 417, "Expectation failed" },
225 { 451, "Parameter not understood" },
226 { 452, "Conference not found" },
227 { 453, "Not enough bandwidth" },*/
228 { 454, "Session not found" },
229 { 455, "Method not valid in this State" },
230 { 456, "Header field not valid for resource" },
231 { 457, "Invalid range" },
232 /*{ 458, "Read-only parameter" },*/
233 { 459, "Aggregate operation not allowed" },
234 { 460, "Non-aggregate operation not allowed" },
235 { 461, "Unsupported transport" },
236 /*{ 462, "Destination unreachable" },*/
237 { 500, "Internal server error" },
238 { 501, "Not implemented" },
239 /*{ 502, "Bad gateway" },*/
240 { 503, "Service unavailable" },
241 /*{ 504, "Gateway time-out" },*/
242 { 505, "Protocol version not supported" },
243 { 551, "Option not supported" },
244 { 999, "" }
247 static const char psz_fallback_reason[5][16] = {
248 "Continue", "OK", "Found", "Client error", "Server error"
251 assert((i_code >= 100) && (i_code <= 599));
253 const http_status_info *p = http_reason;
254 while (i_code < p->i_code)
255 p++;
257 if (p->i_code == i_code)
258 return p->psz_reason;
260 return psz_fallback_reason[(i_code / 100) - 1];
263 static size_t httpd_HtmlError (char **body, int code, const char *url)
265 const char *errname = httpd_ReasonFromCode (code);
266 assert (errname);
268 char *url_Encoded = convert_xml_special_chars (url ? url : "");
270 int res = asprintf (body,
271 "<?xml version=\"1.0\" encoding=\"ascii\" ?>\n"
272 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
273 " \"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
274 "<html lang=\"en\">\n"
275 "<head>\n"
276 "<title>%s</title>\n"
277 "</head>\n"
278 "<body>\n"
279 "<h1>%d %s%s%s%s</h1>\n"
280 "<hr />\n"
281 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
282 "</body>\n"
283 "</html>\n", errname, code, errname,
284 (url_Encoded ? " (" : ""), (url_Encoded ? url_Encoded : ""), (url_Encoded ? ")" : ""));
286 free (url_Encoded);
288 if (res == -1) {
289 *body = NULL;
290 return 0;
293 return (size_t)res;
297 /*****************************************************************************
298 * High Level Functions: httpd_file_t
299 *****************************************************************************/
300 struct httpd_file_t
302 httpd_url_t *url;
303 httpd_file_callback_t pf_fill;
304 httpd_file_sys_t *p_sys;
305 char mime[1];
308 static int
309 httpd_FileCallBack(httpd_callback_sys_t *p_sys, httpd_client_t *cl,
310 httpd_message_t *answer, const httpd_message_t *query)
312 httpd_file_t *file = (httpd_file_t*)p_sys;
313 uint8_t **pp_body, *p_body; const char *psz_connection;
314 int *pi_body, i_body;
316 if (!answer || !query )
317 return VLC_SUCCESS;
319 answer->i_proto = HTTPD_PROTO_HTTP;
320 answer->i_version= 1;
321 answer->i_type = HTTPD_MSG_ANSWER;
323 answer->i_status = 200;
325 httpd_MsgAdd(answer, "Content-type", "%s", file->mime);
326 httpd_MsgAdd(answer, "Cache-Control", "%s", "no-cache");
328 if (query->i_type != HTTPD_MSG_HEAD) {
329 pp_body = &answer->p_body;
330 pi_body = &answer->i_body;
331 } else {
332 /* The file still needs to be executed. */
333 p_body = NULL;
334 i_body = 0;
335 pp_body = &p_body;
336 pi_body = &i_body;
339 if (query->i_type == HTTPD_MSG_POST) {
340 /* msg_Warn not supported */
343 uint8_t *psz_args = query->psz_args;
344 file->pf_fill(file->p_sys, file, psz_args, pp_body, pi_body);
346 if (query->i_type == HTTPD_MSG_HEAD)
347 free(p_body);
349 /* We respect client request */
350 psz_connection = httpd_MsgGet(&cl->query, "Connection");
351 if (!psz_connection)
352 httpd_MsgAdd(answer, "Connection", "%s", psz_connection);
354 httpd_MsgAdd(answer, "Content-Length", "%d", answer->i_body);
356 return VLC_SUCCESS;
359 httpd_file_t *httpd_FileNew(httpd_host_t *host,
360 const char *psz_url, const char *psz_mime,
361 const char *psz_user, const char *psz_password,
362 httpd_file_callback_t pf_fill,
363 httpd_file_sys_t *p_sys)
365 const char *mime = psz_mime;
366 if (mime == NULL || mime[0] == '\0')
367 mime = vlc_mime_Ext2Mime(psz_url);
369 size_t mimelen = strlen(mime);
370 httpd_file_t *file = malloc(sizeof(*file) + mimelen);
371 if (unlikely(file == NULL))
372 return NULL;
374 file->url = httpd_UrlNew(host, psz_url, psz_user, psz_password);
375 if (!file->url) {
376 free(file);
377 return NULL;
380 file->pf_fill = pf_fill;
381 file->p_sys = p_sys;
382 memcpy(file->mime, mime, mimelen + 1);
384 httpd_UrlCatch(file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
385 (httpd_callback_sys_t*)file);
386 httpd_UrlCatch(file->url, HTTPD_MSG_GET, httpd_FileCallBack,
387 (httpd_callback_sys_t*)file);
388 httpd_UrlCatch(file->url, HTTPD_MSG_POST, httpd_FileCallBack,
389 (httpd_callback_sys_t*)file);
391 return file;
394 httpd_file_sys_t *httpd_FileDelete(httpd_file_t *file)
396 httpd_file_sys_t *p_sys = file->p_sys;
398 httpd_UrlDelete(file->url);
399 free(file);
400 return p_sys;
403 /*****************************************************************************
404 * High Level Functions: httpd_handler_t (for CGIs)
405 *****************************************************************************/
406 struct httpd_handler_t
408 httpd_url_t *url;
410 httpd_handler_callback_t pf_fill;
411 httpd_handler_sys_t *p_sys;
415 static int
416 httpd_HandlerCallBack(httpd_callback_sys_t *p_sys, httpd_client_t *cl,
417 httpd_message_t *answer, const httpd_message_t *query)
419 httpd_handler_t *handler = (httpd_handler_t*)p_sys;
420 char psz_remote_addr[NI_MAXNUMERICHOST];
422 if (!answer || !query)
423 return VLC_SUCCESS;
425 answer->i_proto = HTTPD_PROTO_NONE;
426 answer->i_type = HTTPD_MSG_ANSWER;
428 /* We do it ourselves, thanks */
429 answer->i_status = 0;
431 if (!httpd_ClientIP(cl, psz_remote_addr, NULL))
432 *psz_remote_addr = '\0';
434 uint8_t *psz_args = query->psz_args;
435 handler->pf_fill(handler->p_sys, handler, query->psz_url, psz_args,
436 query->i_type, query->p_body, query->i_body,
437 psz_remote_addr, NULL,
438 &answer->p_body, &answer->i_body);
440 if (query->i_type == HTTPD_MSG_HEAD) {
441 char *p = (char *)answer->p_body;
443 /* Looks for end of header (i.e. one empty line) */
444 while ((p = strchr(p, '\r')))
445 if (p[1] == '\n' && p[2] == '\r' && p[3] == '\n')
446 break;
448 if (p) {
449 p[4] = '\0';
450 answer->i_body = strlen((char*)answer->p_body) + 1;
451 answer->p_body = xrealloc(answer->p_body, answer->i_body);
455 if (strncmp((char *)answer->p_body, "HTTP/1.", 7)) {
456 int i_status, i_headers;
457 char *psz_headers, *psz_new;
458 const char *psz_status;
460 if (!strncmp((char *)answer->p_body, "Status: ", 8)) {
461 /* Apache-style */
462 i_status = strtol((char *)&answer->p_body[8], &psz_headers, 0);
463 if (*psz_headers == '\r' || *psz_headers == '\n') psz_headers++;
464 if (*psz_headers == '\n') psz_headers++;
465 i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
466 } else {
467 i_status = 200;
468 psz_headers = (char *)answer->p_body;
469 i_headers = answer->i_body;
472 psz_status = httpd_ReasonFromCode(i_status);
473 answer->i_body = sizeof("HTTP/1.0 xxx \r\n")
474 + strlen(psz_status) + i_headers - 1;
475 psz_new = (char *)xmalloc(answer->i_body + 1);
476 sprintf(psz_new, "HTTP/1.0 %03d %s\r\n", i_status, psz_status);
477 memcpy(&psz_new[strlen(psz_new)], psz_headers, i_headers);
478 free(answer->p_body);
479 answer->p_body = (uint8_t *)psz_new;
482 return VLC_SUCCESS;
485 httpd_handler_t *httpd_HandlerNew(httpd_host_t *host, const char *psz_url,
486 const char *psz_user,
487 const char *psz_password,
488 httpd_handler_callback_t pf_fill,
489 httpd_handler_sys_t *p_sys)
491 httpd_handler_t *handler = malloc(sizeof(*handler));
492 if (!handler)
493 return NULL;
495 handler->url = httpd_UrlNew(host, psz_url, psz_user, psz_password);
496 if (!handler->url) {
497 free(handler);
498 return NULL;
501 handler->pf_fill = pf_fill;
502 handler->p_sys = p_sys;
504 httpd_UrlCatch(handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
505 (httpd_callback_sys_t*)handler);
506 httpd_UrlCatch(handler->url, HTTPD_MSG_GET, httpd_HandlerCallBack,
507 (httpd_callback_sys_t*)handler);
508 httpd_UrlCatch(handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
509 (httpd_callback_sys_t*)handler);
511 return handler;
514 httpd_handler_sys_t *httpd_HandlerDelete(httpd_handler_t *handler)
516 httpd_handler_sys_t *p_sys = handler->p_sys;
517 httpd_UrlDelete(handler->url);
518 free(handler);
519 return p_sys;
522 /*****************************************************************************
523 * High Level Functions: httpd_redirect_t
524 *****************************************************************************/
525 struct httpd_redirect_t
527 httpd_url_t *url;
528 char dst[1];
531 static int httpd_RedirectCallBack(httpd_callback_sys_t *p_sys,
532 httpd_client_t *cl, httpd_message_t *answer,
533 const httpd_message_t *query)
535 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
536 char *p_body;
537 (void)cl;
539 if (!answer || !query)
540 return VLC_SUCCESS;
542 answer->i_proto = HTTPD_PROTO_HTTP;
543 answer->i_version= 1;
544 answer->i_type = HTTPD_MSG_ANSWER;
545 answer->i_status = 301;
547 answer->i_body = httpd_HtmlError (&p_body, 301, rdir->dst);
548 answer->p_body = (unsigned char *)p_body;
550 /* XXX check if it's ok or we need to set an absolute url */
551 httpd_MsgAdd(answer, "Location", "%s", rdir->dst);
553 httpd_MsgAdd(answer, "Content-Length", "%d", answer->i_body);
555 return VLC_SUCCESS;
558 httpd_redirect_t *httpd_RedirectNew(httpd_host_t *host, const char *psz_url_dst,
559 const char *psz_url_src)
561 size_t dstlen = strlen(psz_url_dst);
563 httpd_redirect_t *rdir = malloc(sizeof(*rdir) + dstlen);
564 if (unlikely(rdir == NULL))
565 return NULL;
567 rdir->url = httpd_UrlNew(host, psz_url_src, NULL, NULL);
568 if (!rdir->url) {
569 free(rdir);
570 return NULL;
572 memcpy(rdir->dst, psz_url_dst, dstlen + 1);
574 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
575 httpd_UrlCatch(rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
576 (httpd_callback_sys_t*)rdir);
577 httpd_UrlCatch(rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
578 (httpd_callback_sys_t*)rdir);
579 httpd_UrlCatch(rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
580 (httpd_callback_sys_t*)rdir);
581 httpd_UrlCatch(rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
582 (httpd_callback_sys_t*)rdir);
584 return rdir;
586 void httpd_RedirectDelete(httpd_redirect_t *rdir)
588 httpd_UrlDelete(rdir->url);
589 free(rdir);
592 /*****************************************************************************
593 * High Level Funtions: httpd_stream_t
594 *****************************************************************************/
595 struct httpd_stream_t
597 vlc_mutex_t lock;
598 httpd_url_t *url;
600 char *psz_mime;
602 /* Header to send as first packet */
603 uint8_t *p_header;
604 int i_header;
606 /* Some muxes, in particular the avformat mux, can mark given blocks
607 * as keyframes, to ensure that the stream starts with one.
608 * (This is particularly important for WebM streaming to certain
609 * browsers.) Store if we've ever seen any such keyframe blocks,
610 * and if so, the byte position of the start of the last one. */
611 bool b_has_keyframes;
612 int64_t i_last_keyframe_seen_pos;
614 /* circular buffer */
615 int i_buffer_size; /* buffer size, can't be reallocated smaller */
616 uint8_t *p_buffer; /* buffer */
617 int64_t i_buffer_pos; /* absolute position from begining */
618 int64_t i_buffer_last_pos; /* a new connection will start with that */
620 /* custom headers */
621 size_t i_http_headers;
622 httpd_header * p_http_headers;
625 static int httpd_StreamCallBack(httpd_callback_sys_t *p_sys,
626 httpd_client_t *cl, httpd_message_t *answer,
627 const httpd_message_t *query)
629 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
631 if (!answer || !query || !cl)
632 return VLC_SUCCESS;
634 if (answer->i_body_offset > 0) {
635 int i_pos;
637 if (answer->i_body_offset >= stream->i_buffer_pos)
638 return VLC_EGENERIC; /* wait, no data available */
640 if (cl->i_keyframe_wait_to_pass >= 0) {
641 if (stream->i_last_keyframe_seen_pos <= cl->i_keyframe_wait_to_pass)
642 /* still waiting for the next keyframe */
643 return VLC_EGENERIC;
645 /* seek to the new keyframe */
646 answer->i_body_offset = stream->i_last_keyframe_seen_pos;
647 cl->i_keyframe_wait_to_pass = -1;
650 if (answer->i_body_offset + stream->i_buffer_size < stream->i_buffer_pos)
651 answer->i_body_offset = stream->i_buffer_last_pos; /* this client isn't fast enough */
653 i_pos = answer->i_body_offset % stream->i_buffer_size;
654 int64_t i_write = stream->i_buffer_pos - answer->i_body_offset;
656 if (i_write > HTTPD_CL_BUFSIZE)
657 i_write = HTTPD_CL_BUFSIZE;
658 else if (i_write <= 0)
659 return VLC_EGENERIC; /* wait, no data available */
661 /* Don't go past the end of the circular buffer */
662 i_write = __MIN(i_write, stream->i_buffer_size - i_pos);
664 /* using HTTPD_MSG_ANSWER -> data available */
665 answer->i_proto = HTTPD_PROTO_HTTP;
666 answer->i_version= 0;
667 answer->i_type = HTTPD_MSG_ANSWER;
669 answer->i_body = i_write;
670 answer->p_body = xmalloc(i_write);
671 memcpy(answer->p_body, &stream->p_buffer[i_pos], i_write);
673 answer->i_body_offset += i_write;
675 return VLC_SUCCESS;
676 } else {
677 answer->i_proto = HTTPD_PROTO_HTTP;
678 answer->i_version= 0;
679 answer->i_type = HTTPD_MSG_ANSWER;
681 answer->i_status = 200;
683 bool b_has_content_type = false;
684 bool b_has_cache_control = false;
686 vlc_mutex_lock(&stream->lock);
687 for (size_t i = 0; i < stream->i_http_headers; i++)
688 if (strncasecmp(stream->p_http_headers[i].name, "Content-Length", 14)) {
689 httpd_MsgAdd(answer, stream->p_http_headers[i].name, "%s",
690 stream->p_http_headers[i].value);
692 if (!strncasecmp(stream->p_http_headers[i].name, "Content-Type", 12))
693 b_has_content_type = true;
694 else if (!strncasecmp(stream->p_http_headers[i].name, "Cache-Control", 13))
695 b_has_cache_control = true;
697 vlc_mutex_unlock(&stream->lock);
699 if (query->i_type != HTTPD_MSG_HEAD) {
700 cl->b_stream_mode = true;
701 vlc_mutex_lock(&stream->lock);
702 /* Send the header */
703 if (stream->i_header > 0) {
704 answer->i_body = stream->i_header;
705 answer->p_body = xmalloc(stream->i_header);
706 memcpy(answer->p_body, stream->p_header, stream->i_header);
708 answer->i_body_offset = stream->i_buffer_last_pos;
709 if (stream->b_has_keyframes)
710 cl->i_keyframe_wait_to_pass = stream->i_last_keyframe_seen_pos;
711 else
712 cl->i_keyframe_wait_to_pass = -1;
713 vlc_mutex_unlock(&stream->lock);
714 } else {
715 httpd_MsgAdd(answer, "Content-Length", "0");
716 answer->i_body_offset = 0;
719 /* FIXME: move to http access_output */
720 if (!strcmp(stream->psz_mime, "video/x-ms-asf-stream")) {
721 bool b_xplaystream = false;
723 httpd_MsgAdd(answer, "Content-type", "application/octet-stream");
724 httpd_MsgAdd(answer, "Server", "Cougar 4.1.0.3921");
725 httpd_MsgAdd(answer, "Pragma", "no-cache");
726 httpd_MsgAdd(answer, "Pragma", "client-id=%lu",
727 vlc_mrand48()&0x7fff);
728 httpd_MsgAdd(answer, "Pragma", "features=\"broadcast\"");
730 /* Check if there is a xPlayStrm=1 */
731 for (size_t i = 0; i < query->i_headers; i++)
732 if (!strcasecmp(query->p_headers[i].name, "Pragma") &&
733 strstr(query->p_headers[i].value, "xPlayStrm=1"))
734 b_xplaystream = true;
736 if (!b_xplaystream)
737 answer->i_body_offset = 0;
738 } else if (!b_has_content_type)
739 httpd_MsgAdd(answer, "Content-type", "%s", stream->psz_mime);
741 if (!b_has_cache_control)
742 httpd_MsgAdd(answer, "Cache-Control", "no-cache");
743 return VLC_SUCCESS;
747 httpd_stream_t *httpd_StreamNew(httpd_host_t *host,
748 const char *psz_url, const char *psz_mime,
749 const char *psz_user, const char *psz_password)
751 httpd_stream_t *stream = malloc(sizeof(*stream));
752 if (!stream)
753 return NULL;
755 stream->url = httpd_UrlNew(host, psz_url, psz_user, psz_password);
756 if (!stream->url) {
757 free(stream);
758 return NULL;
761 vlc_mutex_init(&stream->lock);
762 if (psz_mime == NULL || psz_mime[0] == '\0')
763 psz_mime = vlc_mime_Ext2Mime(psz_url);
764 stream->psz_mime = xstrdup(psz_mime);
766 stream->i_header = 0;
767 stream->p_header = NULL;
768 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
769 stream->p_buffer = xmalloc(stream->i_buffer_size);
770 /* We set to 1 to make life simpler
771 * (this way i_body_offset can never be 0) */
772 stream->i_buffer_pos = 1;
773 stream->i_buffer_last_pos = 1;
774 stream->b_has_keyframes = false;
775 stream->i_last_keyframe_seen_pos = 0;
776 stream->i_http_headers = 0;
777 stream->p_http_headers = NULL;
779 httpd_UrlCatch(stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
780 (httpd_callback_sys_t*)stream);
781 httpd_UrlCatch(stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
782 (httpd_callback_sys_t*)stream);
783 httpd_UrlCatch(stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
784 (httpd_callback_sys_t*)stream);
786 return stream;
789 int httpd_StreamHeader(httpd_stream_t *stream, uint8_t *p_data, int i_data)
791 vlc_mutex_lock(&stream->lock);
792 free(stream->p_header);
793 stream->p_header = NULL;
795 stream->i_header = i_data;
796 if (i_data > 0) {
797 stream->p_header = xmalloc(i_data);
798 memcpy(stream->p_header, p_data, i_data);
800 vlc_mutex_unlock(&stream->lock);
802 return VLC_SUCCESS;
805 static void httpd_AppendData(httpd_stream_t *stream, uint8_t *p_data, int i_data)
807 int i_pos = stream->i_buffer_pos % stream->i_buffer_size;
808 int i_count = i_data;
809 while (i_count > 0) {
810 int i_copy = __MIN(i_count, stream->i_buffer_size - i_pos);
812 /* Ok, we can't go past the end of our buffer */
813 memcpy(&stream->p_buffer[i_pos], p_data, i_copy);
815 i_pos = (i_pos + i_copy) % stream->i_buffer_size;
816 i_count -= i_copy;
817 p_data += i_copy;
820 stream->i_buffer_pos += i_data;
823 int httpd_StreamSend(httpd_stream_t *stream, const block_t *p_block)
825 if (!p_block || !p_block->p_buffer)
826 return VLC_SUCCESS;
828 vlc_mutex_lock(&stream->lock);
830 /* save this pointer (to be used by new connection) */
831 stream->i_buffer_last_pos = stream->i_buffer_pos;
833 if (p_block->i_flags & BLOCK_FLAG_TYPE_I) {
834 stream->b_has_keyframes = true;
835 stream->i_last_keyframe_seen_pos = stream->i_buffer_pos;
838 httpd_AppendData(stream, p_block->p_buffer, p_block->i_buffer);
840 vlc_mutex_unlock(&stream->lock);
841 return VLC_SUCCESS;
844 void httpd_StreamDelete(httpd_stream_t *stream)
846 httpd_UrlDelete(stream->url);
847 for (size_t i = 0; i < stream->i_http_headers; i++) {
848 free(stream->p_http_headers[i].name);
849 free(stream->p_http_headers[i].value);
851 free(stream->p_http_headers);
852 vlc_mutex_destroy(&stream->lock);
853 free(stream->psz_mime);
854 free(stream->p_header);
855 free(stream->p_buffer);
856 free(stream);
859 /*****************************************************************************
860 * Low level
861 *****************************************************************************/
862 static void* httpd_HostThread(void *);
863 static httpd_host_t *httpd_HostCreate(vlc_object_t *, const char *,
864 const char *, vlc_tls_creds_t *);
866 /* create a new host */
867 httpd_host_t *vlc_http_HostNew(vlc_object_t *p_this)
869 return httpd_HostCreate(p_this, "http-host", "http-port", NULL);
872 httpd_host_t *vlc_https_HostNew(vlc_object_t *obj)
874 char *cert = var_InheritString(obj, "http-cert");
875 if (!cert) {
876 msg_Err(obj, "HTTP/TLS certificate not specified!");
877 return NULL;
880 char *key = var_InheritString(obj, "http-key");
881 vlc_tls_creds_t *tls = vlc_tls_ServerCreate(obj, cert, key);
883 if (!tls) {
884 msg_Err(obj, "HTTP/TLS certificate error (%s and %s)",
885 cert, key ? key : cert);
886 free(key);
887 free(cert);
888 return NULL;
890 free(key);
891 free(cert);
893 char *ca = var_InheritString(obj, "http-ca");
894 if (ca) {
895 if (vlc_tls_ServerAddCA(tls, ca)) {
896 msg_Err(obj, "HTTP/TLS CA error (%s)", ca);
897 free(ca);
898 goto error;
900 free(ca);
903 char *crl = var_InheritString(obj, "http-crl");
904 if (crl) {
905 if (vlc_tls_ServerAddCRL(tls, crl)) {
906 msg_Err(obj, "TLS CRL error (%s)", crl);
907 free(crl);
908 goto error;
910 free(crl);
913 return httpd_HostCreate(obj, "http-host", "https-port", tls);
915 error:
916 vlc_tls_Delete(tls);
917 return NULL;
920 httpd_host_t *vlc_rtsp_HostNew(vlc_object_t *p_this)
922 return httpd_HostCreate(p_this, "rtsp-host", "rtsp-port", NULL);
925 static struct httpd
927 vlc_mutex_t mutex;
929 httpd_host_t **host;
930 int i_host;
931 } httpd = { VLC_STATIC_MUTEX, NULL, 0 };
933 static httpd_host_t *httpd_HostCreate(vlc_object_t *p_this,
934 const char *hostvar,
935 const char *portvar,
936 vlc_tls_creds_t *p_tls)
938 httpd_host_t *host;
939 char *hostname = var_InheritString(p_this, hostvar);
940 unsigned port = var_InheritInteger(p_this, portvar);
942 vlc_url_t url;
943 vlc_UrlParse(&url, hostname, 0);
944 free(hostname);
945 if (url.i_port != 0) {
946 msg_Err(p_this, "Ignoring port %d (using %d)", url.i_port, port);
947 msg_Info(p_this, "Specify port %d separately with the "
948 "%s option instead.", url.i_port, portvar);
951 /* to be sure to avoid multiple creation */
952 vlc_mutex_lock(&httpd.mutex);
954 /* verify if it already exist */
955 for (int i = 0; i < httpd.i_host; i++) {
956 host = httpd.host[i];
958 /* cannot mix TLS and non-TLS hosts */
959 if (host->port != port
960 || (host->p_tls != NULL) != (p_tls != NULL))
961 continue;
963 /* Increase existing matching host reference count.
964 * The reference count is written under both the global httpd and the
965 * host lock. It is read with either or both locks held. The global
966 * lock is always acquired first. */
967 vlc_mutex_lock(&host->lock);
968 host->i_ref++;
969 vlc_mutex_unlock(&host->lock);
971 vlc_mutex_unlock(&httpd.mutex);
972 vlc_UrlClean(&url);
973 vlc_tls_Delete(p_tls);
974 return host;
977 /* create the new host */
978 host = (httpd_host_t *)vlc_custom_create(p_this, sizeof (*host),
979 "http host");
980 if (!host)
981 goto error;
983 vlc_mutex_init(&host->lock);
984 vlc_cond_init(&host->wait);
985 host->i_ref = 1;
987 host->fds = net_ListenTCP(p_this, url.psz_host, port);
988 if (!host->fds) {
989 msg_Err(p_this, "cannot create socket(s) for HTTP host");
990 goto error;
992 for (host->nfd = 0; host->fds[host->nfd] != -1; host->nfd++);
994 if (vlc_object_waitpipe(VLC_OBJECT(host)) == -1) {
995 msg_Err(host, "signaling pipe error: %s", vlc_strerror_c(errno));
996 goto error;
999 host->port = port;
1000 host->i_url = 0;
1001 host->url = NULL;
1002 host->i_client = 0;
1003 host->client = NULL;
1004 host->p_tls = p_tls;
1006 /* create the thread */
1007 if (vlc_clone(&host->thread, httpd_HostThread, host,
1008 VLC_THREAD_PRIORITY_LOW)) {
1009 msg_Err(p_this, "cannot spawn http host thread");
1010 goto error;
1013 /* now add it to httpd */
1014 TAB_APPEND(httpd.i_host, httpd.host, host);
1015 vlc_mutex_unlock(&httpd.mutex);
1017 vlc_UrlClean(&url);
1019 return host;
1021 error:
1022 vlc_mutex_unlock(&httpd.mutex);
1024 if (host) {
1025 net_ListenClose(host->fds);
1026 vlc_cond_destroy(&host->wait);
1027 vlc_mutex_destroy(&host->lock);
1028 vlc_object_release(host);
1031 vlc_UrlClean(&url);
1032 vlc_tls_Delete(p_tls);
1033 return NULL;
1036 /* delete a host */
1037 void httpd_HostDelete(httpd_host_t *host)
1039 bool delete = false;
1041 vlc_mutex_lock(&httpd.mutex);
1043 vlc_mutex_lock(&host->lock);
1044 host->i_ref--;
1045 if (host->i_ref == 0)
1046 delete = true;
1047 vlc_mutex_unlock(&host->lock);
1048 if (!delete) {
1049 /* still used */
1050 vlc_mutex_unlock(&httpd.mutex);
1051 msg_Dbg(host, "httpd_HostDelete: host still in use");
1052 return;
1054 TAB_REMOVE(httpd.i_host, httpd.host, host);
1056 vlc_cancel(host->thread);
1057 vlc_join(host->thread, NULL);
1059 msg_Dbg(host, "HTTP host removed");
1061 for (int i = 0; i < host->i_url; i++)
1062 msg_Err(host, "url still registered: %s", host->url[i]->psz_url);
1064 for (int i = 0; i < host->i_client; i++) {
1065 httpd_client_t *cl = host->client[i];
1066 msg_Warn(host, "client still connected");
1067 httpd_ClientClean(cl);
1068 TAB_REMOVE(host->i_client, host->client, cl);
1069 free(cl);
1070 i--;
1071 /* TODO */
1074 vlc_tls_Delete(host->p_tls);
1075 net_ListenClose(host->fds);
1076 vlc_cond_destroy(&host->wait);
1077 vlc_mutex_destroy(&host->lock);
1078 vlc_object_release(host);
1079 vlc_mutex_unlock(&httpd.mutex);
1082 /* register a new url */
1083 httpd_url_t *httpd_UrlNew(httpd_host_t *host, const char *psz_url,
1084 const char *psz_user, const char *psz_password)
1086 httpd_url_t *url;
1088 assert(psz_url);
1090 vlc_mutex_lock(&host->lock);
1091 for (int i = 0; i < host->i_url; i++)
1092 if (!strcmp(psz_url, host->url[i]->psz_url)) {
1093 msg_Warn(host, "cannot add '%s' (url already defined)", psz_url);
1094 vlc_mutex_unlock(&host->lock);
1095 return NULL;
1098 url = xmalloc(sizeof(httpd_url_t));
1099 url->host = host;
1101 vlc_mutex_init(&url->lock);
1102 url->psz_url = xstrdup(psz_url);
1103 url->psz_user = xstrdup(psz_user ? psz_user : "");
1104 url->psz_password = xstrdup(psz_password ? psz_password : "");
1105 for (int i = 0; i < HTTPD_MSG_MAX; i++) {
1106 url->catch[i].cb = NULL;
1107 url->catch[i].p_sys = NULL;
1110 TAB_APPEND(host->i_url, host->url, url);
1111 vlc_cond_signal(&host->wait);
1112 vlc_mutex_unlock(&host->lock);
1114 return url;
1117 /* register callback on a url */
1118 int httpd_UrlCatch(httpd_url_t *url, int i_msg, httpd_callback_t cb,
1119 httpd_callback_sys_t *p_sys)
1121 vlc_mutex_lock(&url->lock);
1122 url->catch[i_msg].cb = cb;
1123 url->catch[i_msg].p_sys= p_sys;
1124 vlc_mutex_unlock(&url->lock);
1126 return VLC_SUCCESS;
1129 /* delete a url */
1130 void httpd_UrlDelete(httpd_url_t *url)
1132 httpd_host_t *host = url->host;
1134 vlc_mutex_lock(&host->lock);
1135 TAB_REMOVE(host->i_url, host->url, url);
1137 vlc_mutex_destroy(&url->lock);
1138 free(url->psz_url);
1139 free(url->psz_user);
1140 free(url->psz_password);
1142 for (int i = 0; i < host->i_client; i++) {
1143 httpd_client_t *client = host->client[i];
1145 if (client->url != url)
1146 continue;
1148 /* TODO complete it */
1149 msg_Warn(host, "force closing connections");
1150 httpd_ClientClean(client);
1151 TAB_REMOVE(host->i_client, host->client, client);
1152 free(client);
1153 i--;
1155 free(url);
1156 vlc_mutex_unlock(&host->lock);
1159 static void httpd_MsgInit(httpd_message_t *msg)
1161 msg->cl = NULL;
1162 msg->i_type = HTTPD_MSG_NONE;
1163 msg->i_proto = HTTPD_PROTO_NONE;
1164 msg->i_version = -1; /* FIXME */
1166 msg->i_status = 0;
1168 msg->psz_url = NULL;
1169 msg->psz_args = NULL;
1171 msg->i_headers = 0;
1172 msg->p_headers = NULL;
1174 msg->i_body_offset = 0;
1175 msg->i_body = 0;
1176 msg->p_body = NULL;
1179 static void httpd_MsgClean(httpd_message_t *msg)
1181 free(msg->psz_url);
1182 free(msg->psz_args);
1183 for (size_t i = 0; i < msg->i_headers; i++) {
1184 free(msg->p_headers[i].name);
1185 free(msg->p_headers[i].value);
1187 free(msg->p_headers);
1188 free(msg->p_body);
1189 httpd_MsgInit(msg);
1192 const char *httpd_MsgGet(const httpd_message_t *msg, const char *name)
1194 for (size_t i = 0; i < msg->i_headers; i++)
1195 if (!strcasecmp(msg->p_headers[i].name, name))
1196 return msg->p_headers[i].value;
1197 return NULL;
1200 void httpd_MsgAdd(httpd_message_t *msg, const char *name, const char *psz_value, ...)
1202 httpd_header *p_tmp = realloc(msg->p_headers, sizeof(httpd_header) * (msg->i_headers + 1));
1203 if (!p_tmp)
1204 return;
1206 msg->p_headers = p_tmp;
1208 httpd_header *h = &msg->p_headers[msg->i_headers];
1209 h->name = strdup(name);
1210 if (!h->name)
1211 return;
1213 h->value = NULL;
1215 va_list args;
1216 va_start(args, psz_value);
1217 int ret = us_vasprintf(&h->value, psz_value, args);
1218 va_end(args);
1220 if (ret == -1) {
1221 free(h->name);
1222 return;
1225 msg->i_headers++;
1228 static void httpd_ClientInit(httpd_client_t *cl, mtime_t now)
1230 cl->i_state = HTTPD_CLIENT_RECEIVING;
1231 cl->i_activity_date = now;
1232 cl->i_activity_timeout = INT64_C(10000000);
1233 cl->i_buffer_size = HTTPD_CL_BUFSIZE;
1234 cl->i_buffer = 0;
1235 cl->p_buffer = xmalloc(cl->i_buffer_size);
1236 cl->i_keyframe_wait_to_pass = -1;
1237 cl->b_stream_mode = false;
1239 httpd_MsgInit(&cl->query);
1240 httpd_MsgInit(&cl->answer);
1243 char* httpd_ClientIP(const httpd_client_t *cl, char *ip, int *port)
1245 return net_GetPeerAddress(cl->fd, ip, port) ? NULL : ip;
1248 char* httpd_ServerIP(const httpd_client_t *cl, char *ip, int *port)
1250 return net_GetSockAddress(cl->fd, ip, port) ? NULL : ip;
1253 static void httpd_ClientClean(httpd_client_t *cl)
1255 if (cl->fd >= 0) {
1256 if (cl->p_tls)
1257 vlc_tls_SessionDelete(cl->p_tls);
1258 net_Close(cl->fd);
1259 cl->fd = -1;
1262 httpd_MsgClean(&cl->answer);
1263 httpd_MsgClean(&cl->query);
1265 free(cl->p_buffer);
1266 cl->p_buffer = NULL;
1269 static httpd_client_t *httpd_ClientNew(int fd, vlc_tls_t *p_tls, mtime_t now)
1271 httpd_client_t *cl = malloc(sizeof(httpd_client_t));
1273 if (!cl) return NULL;
1275 cl->i_ref = 0;
1276 cl->fd = fd;
1277 cl->url = NULL;
1278 cl->p_tls = p_tls;
1280 httpd_ClientInit(cl, now);
1281 if (p_tls)
1282 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
1284 return cl;
1287 static
1288 ssize_t httpd_NetRecv (httpd_client_t *cl, uint8_t *p, size_t i_len)
1290 vlc_tls_t *p_tls;
1291 ssize_t val;
1293 p_tls = cl->p_tls;
1295 val = p_tls ? tls_Recv (p_tls, p, i_len)
1296 : recv (cl->fd, p, i_len, 0);
1297 while (val == -1 && errno == EINTR);
1298 return val;
1301 static
1302 ssize_t httpd_NetSend (httpd_client_t *cl, const uint8_t *p, size_t i_len)
1304 vlc_tls_t *p_tls;
1305 ssize_t val;
1307 p_tls = cl->p_tls;
1309 val = p_tls ? tls_Send(p_tls, p, i_len)
1310 : send (cl->fd, p, i_len, 0);
1311 while (val == -1 && errno == EINTR);
1312 return val;
1316 static const struct
1318 const char name[16];
1319 int i_type;
1320 int i_proto;
1322 msg_type[] =
1324 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1325 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1326 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1327 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1328 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1329 { "GET_PARAMETER", HTTPD_MSG_GETPARAMETER, HTTPD_PROTO_RTSP },
1330 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1331 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1332 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1333 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1334 { "", HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1338 static void httpd_ClientRecv(httpd_client_t *cl)
1340 int i_len;
1342 /* ignore leading whites */
1343 if (cl->query.i_proto == HTTPD_PROTO_NONE && cl->i_buffer == 0) {
1344 unsigned char c;
1346 i_len = httpd_NetRecv(cl, &c, 1);
1348 if (i_len > 0 && !strchr("\r\n\t ", c)) {
1349 cl->p_buffer[0] = c;
1350 cl->i_buffer++;
1352 } else if (cl->query.i_proto == HTTPD_PROTO_NONE) {
1353 /* enough to see if it's Interleaved RTP over RTSP or RTSP/HTTP */
1354 i_len = httpd_NetRecv(cl, &cl->p_buffer[cl->i_buffer],
1355 7 - cl->i_buffer);
1356 if (i_len > 0)
1357 cl->i_buffer += i_len;
1359 /* The smallest legal request is 7 bytes ("GET /\r\n"),
1360 * this is the maximum we can ask at this point. */
1361 if (cl->i_buffer >= 7) {
1362 if (!memcmp(cl->p_buffer, "HTTP/1.", 7)) {
1363 cl->query.i_proto = HTTPD_PROTO_HTTP;
1364 cl->query.i_type = HTTPD_MSG_ANSWER;
1365 } else if (!memcmp(cl->p_buffer, "RTSP/1.", 7)) {
1366 cl->query.i_proto = HTTPD_PROTO_RTSP;
1367 cl->query.i_type = HTTPD_MSG_ANSWER;
1368 } else {
1369 /* We need the full request line to determine the protocol. */
1370 cl->query.i_proto = HTTPD_PROTO_HTTP0;
1371 cl->query.i_type = HTTPD_MSG_NONE;
1374 } else if (cl->query.i_body > 0) {
1375 /* we are reading the body of a request or a channel */
1376 i_len = httpd_NetRecv(cl, &cl->query.p_body[cl->i_buffer],
1377 cl->query.i_body - cl->i_buffer);
1378 if (i_len > 0)
1379 cl->i_buffer += i_len;
1381 if (cl->i_buffer >= cl->query.i_body)
1382 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1383 } else for (;;) { /* we are reading a header -> char by char */
1384 if (cl->i_buffer == cl->i_buffer_size) {
1385 uint8_t *newbuf = realloc(cl->p_buffer, cl->i_buffer_size + 1024);
1386 if (!newbuf) {
1387 i_len = 0;
1388 break;
1391 cl->p_buffer = newbuf;
1392 cl->i_buffer_size += 1024;
1395 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1);
1396 if (i_len <= 0)
1397 break;
1399 cl->i_buffer++;
1401 if ((cl->query.i_proto == HTTPD_PROTO_HTTP0)
1402 && (cl->p_buffer[cl->i_buffer - 1] == '\n'))
1404 /* Request line is now complete */
1405 const char *p = memchr(cl->p_buffer, ' ', cl->i_buffer);
1406 size_t len;
1408 assert(cl->query.i_type == HTTPD_MSG_NONE);
1410 if (!p) { /* no URI: evil guy */
1411 i_len = 0; /* drop connection */
1412 break;
1416 p++; /* skips extra spaces */
1417 while (*p == ' ');
1419 p = memchr(p, ' ', ((char *)cl->p_buffer) + cl->i_buffer - p);
1420 if (!p) { /* no explicit protocol: HTTP/0.9 */
1421 i_len = 0; /* not supported currently -> drop */
1422 break;
1426 p++; /* skips extra spaces ever again */
1427 while (*p == ' ');
1429 len = ((char *)cl->p_buffer) + cl->i_buffer - p;
1430 if (len < 7) /* foreign protocol */
1431 i_len = 0; /* I don't understand -> drop */
1432 else if (!memcmp(p, "HTTP/1.", 7)) {
1433 cl->query.i_proto = HTTPD_PROTO_HTTP;
1434 cl->query.i_version = atoi(p + 7);
1435 } else if (!memcmp(p, "RTSP/1.", 7)) {
1436 cl->query.i_proto = HTTPD_PROTO_RTSP;
1437 cl->query.i_version = atoi(p + 7);
1438 } else if (!memcmp(p, "HTTP/", 5)) {
1439 const uint8_t sorry[] =
1440 "HTTP/1.1 505 Unknown HTTP version\r\n\r\n";
1441 httpd_NetSend(cl, sorry, sizeof(sorry) - 1);
1442 i_len = 0; /* drop */
1443 } else if (!memcmp(p, "RTSP/", 5)) {
1444 const uint8_t sorry[] =
1445 "RTSP/1.0 505 Unknown RTSP version\r\n\r\n";
1446 httpd_NetSend(cl, sorry, sizeof(sorry) - 1);
1447 i_len = 0; /* drop */
1448 } else /* yet another foreign protocol */
1449 i_len = 0;
1451 if (i_len == 0)
1452 break;
1455 if ((cl->i_buffer >= 2 && !memcmp(&cl->p_buffer[cl->i_buffer-2], "\n\n", 2))||
1456 (cl->i_buffer >= 4 && !memcmp(&cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4)))
1458 char *p;
1460 /* we have finished the header so parse it and set i_body */
1461 cl->p_buffer[cl->i_buffer] = '\0';
1463 if (cl->query.i_type == HTTPD_MSG_ANSWER) {
1464 /* FIXME:
1465 * assume strlen("HTTP/1.x") = 8
1467 cl->query.i_status =
1468 strtol((char *)&cl->p_buffer[8],
1469 &p, 0);
1470 while (*p == ' ')
1471 p++;
1472 } else {
1473 p = NULL;
1474 cl->query.i_type = HTTPD_MSG_NONE;
1476 for (unsigned i = 0; msg_type[i].name[0]; i++)
1477 if (!strncmp((char *)cl->p_buffer, msg_type[i].name,
1478 strlen(msg_type[i].name))) {
1479 p = (char *)&cl->p_buffer[strlen(msg_type[i].name) + 1 ];
1480 cl->query.i_type = msg_type[i].i_type;
1481 if (cl->query.i_proto != msg_type[i].i_proto) {
1482 p = NULL;
1483 cl->query.i_proto = HTTPD_PROTO_NONE;
1484 cl->query.i_type = HTTPD_MSG_NONE;
1486 break;
1489 if (!p) {
1490 if (strstr((char *)cl->p_buffer, "HTTP/1."))
1491 cl->query.i_proto = HTTPD_PROTO_HTTP;
1492 else if (strstr((char *)cl->p_buffer, "RTSP/1."))
1493 cl->query.i_proto = HTTPD_PROTO_RTSP;
1494 } else {
1495 char *p2;
1496 char *p3;
1498 while (*p == ' ')
1499 p++;
1501 p2 = strchr(p, ' ');
1502 if (p2)
1503 *p2++ = '\0';
1505 if (!strncasecmp(p, (cl->query.i_proto == HTTPD_PROTO_HTTP) ? "http:" : "rtsp:", 5)) {
1506 /* Skip hier-part of URL (if present) */
1507 p += 5;
1508 if (!strncmp(p, "//", 2)) { /* skip authority */
1509 /* see RFC3986 §3.2 */
1510 p += 2;
1511 p += strcspn(p, "/?#");
1514 else if (!strncasecmp(p, (cl->query.i_proto == HTTPD_PROTO_HTTP) ? "https:" : "rtsps:", 6)) {
1515 /* Skip hier-part of URL (if present) */
1516 p += 6;
1517 if (!strncmp(p, "//", 2)) { /* skip authority */
1518 /* see RFC3986 §3.2 */
1519 p += 2;
1520 p += strcspn(p, "/?#");
1524 cl->query.psz_url = strdup(p);
1525 if ((p3 = strchr(cl->query.psz_url, '?')) ) {
1526 *p3++ = '\0';
1527 cl->query.psz_args = (uint8_t *)strdup(p3);
1529 p = p2;
1532 if (p)
1533 p = strchr(p, '\n');
1535 if (p) {
1536 while (*p == '\n' || *p == '\r')
1537 p++;
1539 while (p && *p) {
1540 char *line = p;
1541 char *eol = p = strchr(p, '\n');
1542 char *colon;
1544 while (eol && eol >= line && (*eol == '\n' || *eol == '\r'))
1545 *eol-- = '\0';
1547 if ((colon = strchr(line, ':'))) {
1548 *colon++ = '\0';
1549 while (*colon == ' ')
1550 colon++;
1551 httpd_MsgAdd(&cl->query, line, "%s", colon);
1553 if (!strcasecmp(line, "Content-Length"))
1554 cl->query.i_body = atol(colon);
1557 if (p) {
1558 p++;
1559 while (*p == '\n' || *p == '\r')
1560 p++;
1564 if (cl->query.i_body > 0) {
1565 /* TODO Mhh, handle the case where the client only
1566 * sends a request and closes the connection to
1567 * mark the end of the body (probably only RTSP) */
1568 cl->query.p_body = malloc(cl->query.i_body);
1569 cl->i_buffer = 0;
1570 if (!cl->query.p_body) {
1571 switch (cl->query.i_proto) {
1572 case HTTPD_PROTO_HTTP: {
1573 const uint8_t sorry[] =
1574 "HTTP/1.1 413 Request Entity Too Large\r\n\r\n";
1575 httpd_NetSend(cl, sorry, sizeof(sorry) - 1);
1576 break;
1578 case HTTPD_PROTO_RTSP: {
1579 const uint8_t sorry[] =
1580 "RTSP/1.0 413 Request Entity Too Large\r\n\r\n";
1581 httpd_NetSend(cl, sorry, sizeof(sorry) - 1);
1582 break;
1584 default:
1585 assert(0);
1587 i_len = 0; /* drop */
1589 break;
1590 } else
1591 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1595 /* check if the client is to be set to dead */
1596 #if defined(_WIN32)
1597 if ((i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK) || (i_len == 0))
1598 #else
1599 if ((i_len < 0 && errno != EAGAIN) || (i_len == 0))
1600 #endif
1602 if (cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE) {
1603 /* connection closed -> end of data */
1604 if (cl->query.i_body > 0)
1605 cl->query.i_body = cl->i_buffer;
1606 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1608 else
1609 cl->i_state = HTTPD_CLIENT_DEAD;
1612 /* XXX: for QT I have to disable timeout. Try to find why */
1613 if (cl->query.i_proto == HTTPD_PROTO_RTSP)
1614 cl->i_activity_timeout = 0;
1617 static void httpd_ClientSend(httpd_client_t *cl)
1619 int i_len;
1621 if (cl->i_buffer < 0) {
1622 /* We need to create the header */
1623 int i_size = 0;
1624 char *p;
1625 const char *psz_status = httpd_ReasonFromCode(cl->answer.i_status);
1627 i_size = strlen("HTTP/1.") + 10 + 10 + strlen(psz_status) + 5;
1628 for (size_t i = 0; i < cl->answer.i_headers; i++)
1629 i_size += strlen(cl->answer.p_headers[i].name) + 2 +
1630 strlen(cl->answer.p_headers[i].value) + 2;
1632 if (cl->i_buffer_size < i_size) {
1633 cl->i_buffer_size = i_size;
1634 free(cl->p_buffer);
1635 cl->p_buffer = xmalloc(i_size);
1637 p = (char *)cl->p_buffer;
1639 p += sprintf(p, "%s.%u %d %s\r\n",
1640 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP/1" : "RTSP/1",
1641 cl->answer.i_version,
1642 cl->answer.i_status, psz_status);
1643 for (size_t i = 0; i < cl->answer.i_headers; i++)
1644 p += sprintf(p, "%s: %s\r\n", cl->answer.p_headers[i].name,
1645 cl->answer.p_headers[i].value);
1646 p += sprintf(p, "\r\n");
1648 cl->i_buffer = 0;
1649 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1652 i_len = httpd_NetSend(cl, &cl->p_buffer[cl->i_buffer],
1653 cl->i_buffer_size - cl->i_buffer);
1654 if (i_len >= 0) {
1655 cl->i_buffer += i_len;
1657 if (cl->i_buffer >= cl->i_buffer_size) {
1658 if (cl->answer.i_body == 0 && cl->answer.i_body_offset > 0) {
1659 /* catch more body data */
1660 int i_msg = cl->query.i_type;
1661 int64_t i_offset = cl->answer.i_body_offset;
1663 httpd_MsgClean(&cl->answer);
1664 cl->answer.i_body_offset = i_offset;
1666 cl->url->catch[i_msg].cb(cl->url->catch[i_msg].p_sys, cl,
1667 &cl->answer, &cl->query);
1670 if (cl->answer.i_body > 0) {
1671 /* send the body data */
1672 free(cl->p_buffer);
1673 cl->p_buffer = cl->answer.p_body;
1674 cl->i_buffer_size = cl->answer.i_body;
1675 cl->i_buffer = 0;
1677 cl->answer.i_body = 0;
1678 cl->answer.p_body = NULL;
1679 } else /* send finished */
1680 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1682 } else {
1683 #if defined(_WIN32)
1684 if ((i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK) || (i_len == 0))
1685 #else
1686 if ((i_len < 0 && errno != EAGAIN) || (i_len == 0))
1687 #endif
1689 /* error */
1690 cl->i_state = HTTPD_CLIENT_DEAD;
1695 static void httpd_ClientTlsHandshake(httpd_client_t *cl)
1697 switch(vlc_tls_SessionHandshake(cl->p_tls, NULL, NULL)) {
1698 case -1: cl->i_state = HTTPD_CLIENT_DEAD; break;
1699 case 0: cl->i_state = HTTPD_CLIENT_RECEIVING; break;
1700 case 1: cl->i_state = HTTPD_CLIENT_TLS_HS_IN; break;
1701 case 2: cl->i_state = HTTPD_CLIENT_TLS_HS_OUT; break;
1705 static bool httpdAuthOk(const char *user, const char *pass, const char *b64)
1707 if (!*user && !*pass)
1708 return true;
1710 if (!b64)
1711 return false;
1713 if (strncasecmp(b64, "BASIC", 5))
1714 return false;
1716 b64 += 5;
1717 while (*b64 == ' ')
1718 b64++;
1720 char *given_user = vlc_b64_decode(b64);
1721 if (!given_user)
1722 return false;
1724 char *given_pass = NULL;
1725 given_pass = strchr (given_user, ':');
1726 if (!given_pass)
1727 goto auth_failed;
1729 *given_pass++ = '\0';
1731 if (strcmp (given_user, user))
1732 goto auth_failed;
1734 if (strcmp (given_pass, pass))
1735 goto auth_failed;
1737 free(given_user);
1738 return true;
1740 auth_failed:
1741 free(given_user);
1742 return false;
1745 static void httpdLoop(httpd_host_t *host)
1747 struct pollfd ufd[host->nfd + host->i_client];
1748 unsigned nfd;
1749 for (nfd = 0; nfd < host->nfd; nfd++) {
1750 ufd[nfd].fd = host->fds[nfd];
1751 ufd[nfd].events = POLLIN;
1752 ufd[nfd].revents = 0;
1755 /* add all socket that should be read/write and close dead connection */
1756 while (host->i_url <= 0) {
1757 mutex_cleanup_push(&host->lock);
1758 vlc_cond_wait(&host->wait, &host->lock);
1759 vlc_cleanup_pop();
1762 mtime_t now = mdate();
1763 bool b_low_delay = false;
1765 int canc = vlc_savecancel();
1766 for (int i_client = 0; i_client < host->i_client; i_client++) {
1767 int64_t i_offset;
1768 httpd_client_t *cl = host->client[i_client];
1769 if (cl->i_ref < 0 || (cl->i_ref == 0 &&
1770 (cl->i_state == HTTPD_CLIENT_DEAD ||
1771 (cl->i_activity_timeout > 0 &&
1772 cl->i_activity_date+cl->i_activity_timeout < now)))) {
1773 httpd_ClientClean(cl);
1774 TAB_REMOVE(host->i_client, host->client, cl);
1775 free(cl);
1776 i_client--;
1777 continue;
1780 struct pollfd *pufd = ufd + nfd;
1781 assert (pufd < ufd + (sizeof (ufd) / sizeof (ufd[0])));
1783 pufd->fd = cl->fd;
1784 pufd->events = pufd->revents = 0;
1786 switch (cl->i_state) {
1787 case HTTPD_CLIENT_RECEIVING:
1788 case HTTPD_CLIENT_TLS_HS_IN:
1789 pufd->events = POLLIN;
1790 break;
1792 case HTTPD_CLIENT_SENDING:
1793 case HTTPD_CLIENT_TLS_HS_OUT:
1794 pufd->events = POLLOUT;
1795 break;
1797 case HTTPD_CLIENT_RECEIVE_DONE: {
1798 httpd_message_t *answer = &cl->answer;
1799 httpd_message_t *query = &cl->query;
1801 httpd_MsgInit(answer);
1803 /* Handle what we received */
1804 switch (query->i_type) {
1805 case HTTPD_MSG_ANSWER:
1806 cl->url = NULL;
1807 cl->i_state = HTTPD_CLIENT_DEAD;
1808 break;
1810 case HTTPD_MSG_OPTIONS:
1811 answer->i_type = HTTPD_MSG_ANSWER;
1812 answer->i_proto = query->i_proto;
1813 answer->i_status = 200;
1814 answer->i_body = 0;
1815 answer->p_body = NULL;
1817 httpd_MsgAdd(answer, "Server", "VLC/%s", VERSION);
1818 httpd_MsgAdd(answer, "Content-Length", "0");
1820 switch(query->i_proto) {
1821 case HTTPD_PROTO_HTTP:
1822 answer->i_version = 1;
1823 httpd_MsgAdd(answer, "Allow", "GET,HEAD,POST,OPTIONS");
1824 break;
1826 case HTTPD_PROTO_RTSP:
1827 answer->i_version = 0;
1829 const char *p = httpd_MsgGet(query, "Cseq");
1830 if (p)
1831 httpd_MsgAdd(answer, "Cseq", "%s", p);
1832 p = httpd_MsgGet(query, "Timestamp");
1833 if (p)
1834 httpd_MsgAdd(answer, "Timestamp", "%s", p);
1836 p = httpd_MsgGet(query, "Require");
1837 if (p) {
1838 answer->i_status = 551;
1839 httpd_MsgAdd(query, "Unsupported", "%s", p);
1842 httpd_MsgAdd(answer, "Public", "DESCRIBE,SETUP,"
1843 "TEARDOWN,PLAY,PAUSE,GET_PARAMETER");
1844 break;
1847 cl->i_buffer = -1; /* Force the creation of the answer in
1848 * httpd_ClientSend */
1849 cl->i_state = HTTPD_CLIENT_SENDING;
1850 break;
1852 case HTTPD_MSG_NONE:
1853 if (query->i_proto == HTTPD_PROTO_NONE) {
1854 cl->url = NULL;
1855 cl->i_state = HTTPD_CLIENT_DEAD;
1856 } else {
1857 /* unimplemented */
1858 answer->i_proto = query->i_proto ;
1859 answer->i_type = HTTPD_MSG_ANSWER;
1860 answer->i_version= 0;
1861 answer->i_status = 501;
1863 char *p;
1864 answer->i_body = httpd_HtmlError (&p, 501, NULL);
1865 answer->p_body = (uint8_t *)p;
1866 httpd_MsgAdd(answer, "Content-Length", "%d", answer->i_body);
1868 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
1869 cl->i_state = HTTPD_CLIENT_SENDING;
1871 break;
1873 default: {
1874 int i_msg = query->i_type;
1875 bool b_auth_failed = false;
1877 /* Search the url and trigger callbacks */
1878 for (int i = 0; i < host->i_url; i++) {
1879 httpd_url_t *url = host->url[i];
1881 if (strcmp(url->psz_url, query->psz_url))
1882 continue;
1883 if (!url->catch[i_msg].cb)
1884 continue;
1886 if (answer) {
1887 b_auth_failed = !httpdAuthOk(url->psz_user,
1888 url->psz_password,
1889 httpd_MsgGet(query, "Authorization")); /* BASIC id */
1890 if (b_auth_failed)
1891 break;
1894 if (url->catch[i_msg].cb(url->catch[i_msg].p_sys, cl, answer, query))
1895 continue;
1897 if (answer->i_proto == HTTPD_PROTO_NONE)
1898 cl->i_buffer = cl->i_buffer_size; /* Raw answer from a CGI */
1899 else
1900 cl->i_buffer = -1;
1902 /* only one url can answer */
1903 answer = NULL;
1904 if (!cl->url)
1905 cl->url = url;
1908 if (answer) {
1909 answer->i_proto = query->i_proto;
1910 answer->i_type = HTTPD_MSG_ANSWER;
1911 answer->i_version= 0;
1913 if (b_auth_failed) {
1914 httpd_MsgAdd(answer, "WWW-Authenticate",
1915 "Basic realm=\"VLC stream\"");
1916 answer->i_status = 401;
1917 } else
1918 answer->i_status = 404; /* no url registered */
1920 char *p;
1921 answer->i_body = httpd_HtmlError (&p, answer->i_status,
1922 query->psz_url);
1923 answer->p_body = (uint8_t *)p;
1925 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
1926 httpd_MsgAdd(answer, "Content-Length", "%d", answer->i_body);
1927 httpd_MsgAdd(answer, "Content-Type", "%s", "text/html");
1930 cl->i_state = HTTPD_CLIENT_SENDING;
1933 break;
1936 case HTTPD_CLIENT_SEND_DONE:
1937 if (!cl->b_stream_mode || cl->answer.i_body_offset == 0) {
1938 const char *psz_connection = httpd_MsgGet(&cl->answer, "Connection");
1939 const char *psz_query = httpd_MsgGet(&cl->query, "Connection");
1940 bool b_connection = false;
1941 bool b_keepalive = false;
1942 bool b_query = false;
1944 cl->url = NULL;
1945 if (psz_connection) {
1946 b_connection = (strcasecmp(psz_connection, "Close") == 0);
1947 b_keepalive = (strcasecmp(psz_connection, "Keep-Alive") == 0);
1950 if (psz_query)
1951 b_query = (strcasecmp(psz_query, "Close") == 0);
1953 if (((cl->query.i_proto == HTTPD_PROTO_HTTP) &&
1954 ((cl->query.i_version == 0 && b_keepalive) ||
1955 (cl->query.i_version == 1 && !b_connection))) ||
1956 ((cl->query.i_proto == HTTPD_PROTO_RTSP) &&
1957 !b_query && !b_connection)) {
1958 httpd_MsgClean(&cl->query);
1959 httpd_MsgInit(&cl->query);
1961 cl->i_buffer = 0;
1962 cl->i_buffer_size = 1000;
1963 free(cl->p_buffer);
1964 cl->p_buffer = xmalloc(cl->i_buffer_size);
1965 cl->i_state = HTTPD_CLIENT_RECEIVING;
1966 } else
1967 cl->i_state = HTTPD_CLIENT_DEAD;
1968 httpd_MsgClean(&cl->answer);
1969 } else {
1970 i_offset = cl->answer.i_body_offset;
1971 httpd_MsgClean(&cl->answer);
1973 cl->answer.i_body_offset = i_offset;
1974 free(cl->p_buffer);
1975 cl->p_buffer = NULL;
1976 cl->i_buffer = 0;
1977 cl->i_buffer_size = 0;
1979 cl->i_state = HTTPD_CLIENT_WAITING;
1981 break;
1983 case HTTPD_CLIENT_WAITING:
1984 i_offset = cl->answer.i_body_offset;
1985 int i_msg = cl->query.i_type;
1987 httpd_MsgInit(&cl->answer);
1988 cl->answer.i_body_offset = i_offset;
1990 cl->url->catch[i_msg].cb(cl->url->catch[i_msg].p_sys, cl,
1991 &cl->answer, &cl->query);
1992 if (cl->answer.i_type != HTTPD_MSG_NONE) {
1993 /* we have new data, so re-enter send mode */
1994 cl->i_buffer = 0;
1995 cl->p_buffer = cl->answer.p_body;
1996 cl->i_buffer_size = cl->answer.i_body;
1997 cl->answer.p_body = NULL;
1998 cl->answer.i_body = 0;
1999 cl->i_state = HTTPD_CLIENT_SENDING;
2003 if (pufd->events != 0)
2004 nfd++;
2005 else
2006 b_low_delay = true;
2008 vlc_mutex_unlock(&host->lock);
2009 vlc_restorecancel(canc);
2011 /* we will wait 20ms (not too big) if HTTPD_CLIENT_WAITING */
2012 int ret = poll(ufd, nfd, b_low_delay ? 20 : -1);
2014 canc = vlc_savecancel();
2015 vlc_mutex_lock(&host->lock);
2016 switch(ret) {
2017 case -1:
2018 if (errno != EINTR) {
2019 /* Kernel on low memory or a bug: pace */
2020 msg_Err(host, "polling error: %s", vlc_strerror_c(errno));
2021 msleep(100000);
2023 case 0:
2024 vlc_restorecancel(canc);
2025 return;
2028 /* Handle client sockets */
2029 now = mdate();
2030 nfd = host->nfd;
2032 for (int i_client = 0; i_client < host->i_client; i_client++) {
2033 httpd_client_t *cl = host->client[i_client];
2034 const struct pollfd *pufd = &ufd[nfd];
2036 assert(pufd < &ufd[sizeof(ufd) / sizeof(ufd[0])]);
2038 if (cl->fd != pufd->fd)
2039 continue; // we were not waiting for this client
2040 ++nfd;
2041 if (pufd->revents == 0)
2042 continue; // no event received
2044 cl->i_activity_date = now;
2046 switch (cl->i_state) {
2047 case HTTPD_CLIENT_RECEIVING: httpd_ClientRecv(cl); break;
2048 case HTTPD_CLIENT_SENDING: httpd_ClientSend(cl); break;
2049 case HTTPD_CLIENT_TLS_HS_IN:
2050 case HTTPD_CLIENT_TLS_HS_OUT: httpd_ClientTlsHandshake(cl); break;
2054 /* Handle server sockets (accept new connections) */
2055 for (nfd = 0; nfd < host->nfd; nfd++) {
2056 httpd_client_t *cl;
2057 int fd = ufd[nfd].fd;
2059 assert (fd == host->fds[nfd]);
2061 if (ufd[nfd].revents == 0)
2062 continue;
2064 /* */
2065 fd = vlc_accept (fd, NULL, NULL, true);
2066 if (fd == -1)
2067 continue;
2068 setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
2069 &(int){ 1 }, sizeof(int));
2071 vlc_tls_t *p_tls;
2073 if (host->p_tls)
2074 p_tls = vlc_tls_SessionCreate(host->p_tls, fd, NULL);
2075 else
2076 p_tls = NULL;
2078 cl = httpd_ClientNew(fd, p_tls, now);
2080 TAB_APPEND(host->i_client, host->client, cl);
2083 vlc_restorecancel(canc);
2086 static void* httpd_HostThread(void *data)
2088 httpd_host_t *host = data;
2090 vlc_mutex_lock(&host->lock);
2091 while (host->i_ref > 0)
2092 httpdLoop(host);
2093 vlc_mutex_unlock(&host->lock);
2094 return NULL;
2097 int httpd_StreamSetHTTPHeaders(httpd_stream_t * p_stream, httpd_header * p_headers, size_t i_headers)
2099 if (!p_stream)
2100 return VLC_EGENERIC;
2102 vlc_mutex_lock(&p_stream->lock);
2103 if (p_stream->p_http_headers) {
2104 for (size_t i = 0; i < p_stream->i_http_headers; i++) {
2105 free(p_stream->p_http_headers[i].name);
2106 free(p_stream->p_http_headers[i].value);
2108 free(p_stream->p_http_headers);
2109 p_stream->p_http_headers = NULL;
2110 p_stream->i_http_headers = 0;
2113 if (!p_headers || !i_headers) {
2114 vlc_mutex_unlock(&p_stream->lock);
2115 return VLC_SUCCESS;
2118 p_stream->p_http_headers = malloc(sizeof(httpd_header) * i_headers);
2119 if (!p_stream->p_http_headers) {
2120 vlc_mutex_unlock(&p_stream->lock);
2121 return VLC_ENOMEM;
2124 size_t j = 0;
2125 for (size_t i = 0; i < i_headers; i++) {
2126 if (unlikely(!p_headers[i].name || !p_headers[i].value))
2127 continue;
2129 p_stream->p_http_headers[j].name = strdup(p_headers[i].name);
2130 p_stream->p_http_headers[j].value = strdup(p_headers[i].value);
2132 if (unlikely(!p_stream->p_http_headers[j].name ||
2133 !p_stream->p_http_headers[j].value)) {
2134 free(p_stream->p_http_headers[j].name);
2135 free(p_stream->p_http_headers[j].value);
2136 break;
2138 j++;
2140 p_stream->i_http_headers = j;
2141 vlc_mutex_unlock(&p_stream->lock);
2142 return VLC_SUCCESS;