1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2006 VLC authors and VideoLAN
5 * Copyright © 2004-2007 Rémi Denis-Courmont
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 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_httpd.h>
35 #include <vlc_network.h>
37 #include <vlc_strings.h>
39 #include <vlc_charset.h>
42 #include <vlc_block.h>
43 #include "../libvlc.h"
57 # include <winsock2.h>
58 #elif defined(HAVE_SYS_SOCKET_H)
59 # include <sys/socket.h>
63 /* We need HUGE buffer otherwise TCP throughput is very limited */
64 #define HTTPD_CL_BUFSIZE 1000000
66 #define HTTPD_CL_BUFSIZE 10000
69 static void httpd_ClientDestroy(httpd_client_t
*cl
);
70 static void httpd_AppendData(httpd_stream_t
*stream
, uint8_t *p_data
, int i_data
);
72 /* each host run in his own thread */
75 struct vlc_common_members obj
;
80 /* address/port and socket for listening at connections */
89 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
90 * This will slow down the url research but make my live easier
91 * All url will have their cb trigger, but only the first one can answer
97 httpd_client_t
**client
;
100 vlc_tls_creds_t
*p_tls
;
117 httpd_callback_sys_t
*p_sys
;
118 } catch[HTTPD_MSG_MAX
];
124 HTTPD_CLIENT_RECEIVING
,
125 HTTPD_CLIENT_RECEIVE_DONE
,
127 HTTPD_CLIENT_SENDING
,
128 HTTPD_CLIENT_SEND_DONE
,
130 HTTPD_CLIENT_WAITING
,
134 HTTPD_CLIENT_TLS_HS_IN
,
135 HTTPD_CLIENT_TLS_HS_OUT
138 struct httpd_client_t
148 mtime_t i_activity_date
;
149 mtime_t i_activity_timeout
;
151 /* buffer for reading header */
157 * If waiting for a keyframe, this is the position (in bytes) of the
158 * last keyframe the stream saw before this client connected.
161 int64_t i_keyframe_wait_to_pass
;
164 httpd_message_t query
; /* client -> httpd */
165 httpd_message_t answer
; /* httpd -> client */
170 /*****************************************************************************
172 *****************************************************************************/
173 static const char *httpd_ReasonFromCode(unsigned i_code
)
178 const char psz_reason
[36];
181 static const http_status_info http_reason
[] =
183 /*{ 100, "Continue" },
184 { 101, "Switching Protocols" },*/
186 /*{ 201, "Created" },
188 { 203, "Non-authoritative information" },
189 { 204, "No content" },
190 { 205, "Reset content" },
191 { 206, "Partial content" },
192 { 250, "Low on storage space" },
193 { 300, "Multiple choices" },*/
194 { 301, "Moved permanently" },
195 /*{ 302, "Moved temporarily" },
196 { 303, "See other" },
197 { 304, "Not modified" },
198 { 305, "Use proxy" },
199 { 307, "Temporary redirect" },
200 { 400, "Bad request" },*/
201 { 401, "Unauthorized" },
202 /*{ 402, "Payment Required" },*/
203 { 403, "Forbidden" },
204 { 404, "Not found" },
205 { 405, "Method not allowed" },
206 /*{ 406, "Not acceptable" },
207 { 407, "Proxy authentication required" },
208 { 408, "Request time-out" },
211 { 411, "Length required" },
212 { 412, "Precondition failed" },
213 { 413, "Request entity too large" },
214 { 414, "Request-URI too large" },
215 { 415, "Unsupported media Type" },
216 { 416, "Requested range not satisfiable" },
217 { 417, "Expectation failed" },
218 { 451, "Parameter not understood" },
219 { 452, "Conference not found" },
220 { 453, "Not enough bandwidth" },*/
221 { 454, "Session not found" },
222 { 455, "Method not valid in this State" },
223 { 456, "Header field not valid for resource" },
224 { 457, "Invalid range" },
225 /*{ 458, "Read-only parameter" },*/
226 { 459, "Aggregate operation not allowed" },
227 { 460, "Non-aggregate operation not allowed" },
228 { 461, "Unsupported transport" },
229 /*{ 462, "Destination unreachable" },*/
230 { 500, "Internal server error" },
231 { 501, "Not implemented" },
232 /*{ 502, "Bad gateway" },*/
233 { 503, "Service unavailable" },
234 /*{ 504, "Gateway time-out" },*/
235 { 505, "Protocol version not supported" },
236 { 551, "Option not supported" },
240 static const char psz_fallback_reason
[5][16] = {
241 "Continue", "OK", "Found", "Client error", "Server error"
244 assert((i_code
>= 100) && (i_code
<= 599));
246 const http_status_info
*p
= http_reason
;
247 while (i_code
< p
->i_code
)
250 if (p
->i_code
== i_code
)
251 return p
->psz_reason
;
253 return psz_fallback_reason
[(i_code
/ 100) - 1];
256 static size_t httpd_HtmlError (char **body
, int code
, const char *url
)
258 const char *errname
= httpd_ReasonFromCode (code
);
261 char *url_Encoded
= vlc_xml_encode (url
? url
: "");
263 int res
= asprintf (body
,
264 "<?xml version=\"1.0\" encoding=\"ascii\" ?>\n"
265 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
266 " \"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
267 "<html lang=\"en\">\n"
269 "<title>%s</title>\n"
272 "<h1>%d %s%s%s%s</h1>\n"
274 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
276 "</html>\n", errname
, code
, errname
,
277 (url_Encoded
? " (" : ""), (url_Encoded
? url_Encoded
: ""), (url_Encoded
? ")" : ""));
290 /*****************************************************************************
291 * High Level Functions: httpd_file_t
292 *****************************************************************************/
296 httpd_file_callback_t pf_fill
;
297 httpd_file_sys_t
*p_sys
;
302 httpd_FileCallBack(httpd_callback_sys_t
*p_sys
, httpd_client_t
*cl
,
303 httpd_message_t
*answer
, const httpd_message_t
*query
)
305 httpd_file_t
*file
= (httpd_file_t
*)p_sys
;
306 uint8_t **pp_body
, *p_body
;
307 int *pi_body
, i_body
;
309 if (!answer
|| !query
)
312 answer
->i_proto
= HTTPD_PROTO_HTTP
;
313 answer
->i_version
= 1;
314 answer
->i_type
= HTTPD_MSG_ANSWER
;
316 answer
->i_status
= 200;
318 httpd_MsgAdd(answer
, "Content-type", "%s", file
->mime
);
319 httpd_MsgAdd(answer
, "Cache-Control", "%s", "no-cache");
321 if (query
->i_type
!= HTTPD_MSG_HEAD
) {
322 pp_body
= &answer
->p_body
;
323 pi_body
= &answer
->i_body
;
325 /* The file still needs to be executed. */
332 if (query
->i_type
== HTTPD_MSG_POST
) {
333 /* msg_Warn not supported */
336 uint8_t *psz_args
= query
->psz_args
;
337 file
->pf_fill(file
->p_sys
, file
, psz_args
, pp_body
, pi_body
);
339 if (query
->i_type
== HTTPD_MSG_HEAD
)
342 /* We respect client request */
343 if (httpd_MsgGet(&cl
->query
, "Connection") != NULL
)
344 httpd_MsgAdd(answer
, "Connection", "close");
346 httpd_MsgAdd(answer
, "Content-Length", "%d", answer
->i_body
);
351 httpd_file_t
*httpd_FileNew(httpd_host_t
*host
,
352 const char *psz_url
, const char *psz_mime
,
353 const char *psz_user
, const char *psz_password
,
354 httpd_file_callback_t pf_fill
,
355 httpd_file_sys_t
*p_sys
)
357 const char *mime
= psz_mime
;
358 if (mime
== NULL
|| mime
[0] == '\0')
359 mime
= vlc_mime_Ext2Mime(psz_url
);
361 size_t mimelen
= strlen(mime
);
362 httpd_file_t
*file
= malloc(sizeof(*file
) + mimelen
);
363 if (unlikely(file
== NULL
))
366 file
->url
= httpd_UrlNew(host
, psz_url
, psz_user
, psz_password
);
372 file
->pf_fill
= pf_fill
;
374 memcpy(file
->mime
, mime
, mimelen
+ 1);
376 httpd_UrlCatch(file
->url
, HTTPD_MSG_HEAD
, httpd_FileCallBack
,
377 (httpd_callback_sys_t
*)file
);
378 httpd_UrlCatch(file
->url
, HTTPD_MSG_GET
, httpd_FileCallBack
,
379 (httpd_callback_sys_t
*)file
);
380 httpd_UrlCatch(file
->url
, HTTPD_MSG_POST
, httpd_FileCallBack
,
381 (httpd_callback_sys_t
*)file
);
386 httpd_file_sys_t
*httpd_FileDelete(httpd_file_t
*file
)
388 httpd_file_sys_t
*p_sys
= file
->p_sys
;
390 httpd_UrlDelete(file
->url
);
395 /*****************************************************************************
396 * High Level Functions: httpd_handler_t (for CGIs)
397 *****************************************************************************/
398 struct httpd_handler_t
402 httpd_handler_callback_t pf_fill
;
408 httpd_HandlerCallBack(httpd_callback_sys_t
*p_sys
, httpd_client_t
*cl
,
409 httpd_message_t
*answer
, const httpd_message_t
*query
)
411 httpd_handler_t
*handler
= (httpd_handler_t
*)p_sys
;
412 char psz_remote_addr
[NI_MAXNUMERICHOST
];
414 if (!answer
|| !query
)
417 answer
->i_proto
= HTTPD_PROTO_NONE
;
418 answer
->i_type
= HTTPD_MSG_ANSWER
;
420 /* We do it ourselves, thanks */
421 answer
->i_status
= 0;
423 if (!httpd_ClientIP(cl
, psz_remote_addr
, NULL
))
424 *psz_remote_addr
= '\0';
426 uint8_t *psz_args
= query
->psz_args
;
427 handler
->pf_fill(handler
->p_sys
, handler
, query
->psz_url
, psz_args
,
428 query
->i_type
, query
->p_body
, query
->i_body
,
429 psz_remote_addr
, NULL
,
430 &answer
->p_body
, &answer
->i_body
);
432 if (query
->i_type
== HTTPD_MSG_HEAD
) {
433 char *p
= (char *)answer
->p_body
;
435 /* Looks for end of header (i.e. one empty line) */
436 while ((p
= strchr(p
, '\r')))
437 if (p
[1] == '\n' && p
[2] == '\r' && p
[3] == '\n')
442 answer
->i_body
= strlen((char*)answer
->p_body
) + 1;
443 answer
->p_body
= xrealloc(answer
->p_body
, answer
->i_body
);
447 if (strncmp((char *)answer
->p_body
, "HTTP/1.", 7)) {
448 int i_status
, i_headers
;
449 char *psz_headers
, *psz_new
;
450 const char *psz_status
;
452 if (!strncmp((char *)answer
->p_body
, "Status: ", 8)) {
454 i_status
= strtol((char *)&answer
->p_body
[8], &psz_headers
, 0);
455 if (*psz_headers
== '\r' || *psz_headers
== '\n') psz_headers
++;
456 if (*psz_headers
== '\n') psz_headers
++;
457 i_headers
= answer
->i_body
- (psz_headers
- (char *)answer
->p_body
);
460 psz_headers
= (char *)answer
->p_body
;
461 i_headers
= answer
->i_body
;
464 psz_status
= httpd_ReasonFromCode(i_status
);
465 answer
->i_body
= sizeof("HTTP/1.0 xxx \r\n")
466 + strlen(psz_status
) + i_headers
- 1;
467 psz_new
= (char *)xmalloc(answer
->i_body
+ 1);
468 sprintf(psz_new
, "HTTP/1.0 %03d %s\r\n", i_status
, psz_status
);
469 memcpy(&psz_new
[strlen(psz_new
)], psz_headers
, i_headers
);
470 free(answer
->p_body
);
471 answer
->p_body
= (uint8_t *)psz_new
;
477 httpd_handler_t
*httpd_HandlerNew(httpd_host_t
*host
, const char *psz_url
,
478 const char *psz_user
,
479 const char *psz_password
,
480 httpd_handler_callback_t pf_fill
,
483 httpd_handler_t
*handler
= malloc(sizeof(*handler
));
487 handler
->url
= httpd_UrlNew(host
, psz_url
, psz_user
, psz_password
);
493 handler
->pf_fill
= pf_fill
;
494 handler
->p_sys
= p_sys
;
496 httpd_UrlCatch(handler
->url
, HTTPD_MSG_HEAD
, httpd_HandlerCallBack
,
497 (httpd_callback_sys_t
*)handler
);
498 httpd_UrlCatch(handler
->url
, HTTPD_MSG_GET
, httpd_HandlerCallBack
,
499 (httpd_callback_sys_t
*)handler
);
500 httpd_UrlCatch(handler
->url
, HTTPD_MSG_POST
, httpd_HandlerCallBack
,
501 (httpd_callback_sys_t
*)handler
);
506 void *httpd_HandlerDelete(httpd_handler_t
*handler
)
508 void *p_sys
= handler
->p_sys
;
509 httpd_UrlDelete(handler
->url
);
514 /*****************************************************************************
515 * High Level Functions: httpd_redirect_t
516 *****************************************************************************/
517 struct httpd_redirect_t
523 static int httpd_RedirectCallBack(httpd_callback_sys_t
*p_sys
,
524 httpd_client_t
*cl
, httpd_message_t
*answer
,
525 const httpd_message_t
*query
)
527 httpd_redirect_t
*rdir
= (httpd_redirect_t
*)p_sys
;
531 if (!answer
|| !query
)
534 answer
->i_proto
= HTTPD_PROTO_HTTP
;
535 answer
->i_version
= 1;
536 answer
->i_type
= HTTPD_MSG_ANSWER
;
537 answer
->i_status
= 301;
539 answer
->i_body
= httpd_HtmlError (&p_body
, 301, rdir
->dst
);
540 answer
->p_body
= (unsigned char *)p_body
;
542 /* XXX check if it's ok or we need to set an absolute url */
543 httpd_MsgAdd(answer
, "Location", "%s", rdir
->dst
);
545 httpd_MsgAdd(answer
, "Content-Length", "%d", answer
->i_body
);
547 if (httpd_MsgGet(&cl
->query
, "Connection") != NULL
)
548 httpd_MsgAdd(answer
, "Connection", "close");
553 httpd_redirect_t
*httpd_RedirectNew(httpd_host_t
*host
, const char *psz_url_dst
,
554 const char *psz_url_src
)
556 size_t dstlen
= strlen(psz_url_dst
);
558 httpd_redirect_t
*rdir
= malloc(sizeof(*rdir
) + dstlen
);
559 if (unlikely(rdir
== NULL
))
562 rdir
->url
= httpd_UrlNew(host
, psz_url_src
, NULL
, NULL
);
567 memcpy(rdir
->dst
, psz_url_dst
, dstlen
+ 1);
569 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
570 httpd_UrlCatch(rdir
->url
, HTTPD_MSG_HEAD
, httpd_RedirectCallBack
,
571 (httpd_callback_sys_t
*)rdir
);
572 httpd_UrlCatch(rdir
->url
, HTTPD_MSG_GET
, httpd_RedirectCallBack
,
573 (httpd_callback_sys_t
*)rdir
);
574 httpd_UrlCatch(rdir
->url
, HTTPD_MSG_POST
, httpd_RedirectCallBack
,
575 (httpd_callback_sys_t
*)rdir
);
576 httpd_UrlCatch(rdir
->url
, HTTPD_MSG_DESCRIBE
, httpd_RedirectCallBack
,
577 (httpd_callback_sys_t
*)rdir
);
581 void httpd_RedirectDelete(httpd_redirect_t
*rdir
)
583 httpd_UrlDelete(rdir
->url
);
587 /*****************************************************************************
588 * High Level Funtions: httpd_stream_t
589 *****************************************************************************/
590 struct httpd_stream_t
597 /* Header to send as first packet */
601 /* Some muxes, in particular the avformat mux, can mark given blocks
602 * as keyframes, to ensure that the stream starts with one.
603 * (This is particularly important for WebM streaming to certain
604 * browsers.) Store if we've ever seen any such keyframe blocks,
605 * and if so, the byte position of the start of the last one. */
606 bool b_has_keyframes
;
607 int64_t i_last_keyframe_seen_pos
;
609 /* circular buffer */
610 int i_buffer_size
; /* buffer size, can't be reallocated smaller */
611 uint8_t *p_buffer
; /* buffer */
612 int64_t i_buffer_pos
; /* absolute position from beginning */
613 int64_t i_buffer_last_pos
; /* a new connection will start with that */
616 size_t i_http_headers
;
617 httpd_header
* p_http_headers
;
620 static int httpd_StreamCallBack(httpd_callback_sys_t
*p_sys
,
621 httpd_client_t
*cl
, httpd_message_t
*answer
,
622 const httpd_message_t
*query
)
624 httpd_stream_t
*stream
= (httpd_stream_t
*)p_sys
;
626 if (!answer
|| !query
|| !cl
)
629 if (answer
->i_body_offset
> 0) {
632 if (answer
->i_body_offset
>= stream
->i_buffer_pos
)
633 return VLC_EGENERIC
; /* wait, no data available */
635 if (cl
->i_keyframe_wait_to_pass
>= 0) {
636 if (stream
->i_last_keyframe_seen_pos
<= cl
->i_keyframe_wait_to_pass
)
637 /* still waiting for the next keyframe */
640 /* seek to the new keyframe */
641 answer
->i_body_offset
= stream
->i_last_keyframe_seen_pos
;
642 cl
->i_keyframe_wait_to_pass
= -1;
645 if (answer
->i_body_offset
+ stream
->i_buffer_size
< stream
->i_buffer_pos
)
646 answer
->i_body_offset
= stream
->i_buffer_last_pos
; /* this client isn't fast enough */
648 i_pos
= answer
->i_body_offset
% stream
->i_buffer_size
;
649 int64_t i_write
= stream
->i_buffer_pos
- answer
->i_body_offset
;
651 if (i_write
> HTTPD_CL_BUFSIZE
)
652 i_write
= HTTPD_CL_BUFSIZE
;
653 else if (i_write
<= 0)
654 return VLC_EGENERIC
; /* wait, no data available */
656 /* Don't go past the end of the circular buffer */
657 i_write
= __MIN(i_write
, stream
->i_buffer_size
- i_pos
);
659 /* using HTTPD_MSG_ANSWER -> data available */
660 answer
->i_proto
= HTTPD_PROTO_HTTP
;
661 answer
->i_version
= 0;
662 answer
->i_type
= HTTPD_MSG_ANSWER
;
664 answer
->i_body
= i_write
;
665 answer
->p_body
= xmalloc(i_write
);
666 memcpy(answer
->p_body
, &stream
->p_buffer
[i_pos
], i_write
);
668 answer
->i_body_offset
+= i_write
;
672 answer
->i_proto
= HTTPD_PROTO_HTTP
;
673 answer
->i_version
= 0;
674 answer
->i_type
= HTTPD_MSG_ANSWER
;
676 answer
->i_status
= 200;
678 bool b_has_content_type
= false;
679 bool b_has_cache_control
= false;
681 vlc_mutex_lock(&stream
->lock
);
682 for (size_t i
= 0; i
< stream
->i_http_headers
; i
++)
683 if (strncasecmp(stream
->p_http_headers
[i
].name
, "Content-Length", 14)) {
684 httpd_MsgAdd(answer
, stream
->p_http_headers
[i
].name
, "%s",
685 stream
->p_http_headers
[i
].value
);
687 if (!strncasecmp(stream
->p_http_headers
[i
].name
, "Content-Type", 12))
688 b_has_content_type
= true;
689 else if (!strncasecmp(stream
->p_http_headers
[i
].name
, "Cache-Control", 13))
690 b_has_cache_control
= true;
692 vlc_mutex_unlock(&stream
->lock
);
694 if (query
->i_type
!= HTTPD_MSG_HEAD
) {
695 cl
->b_stream_mode
= true;
696 vlc_mutex_lock(&stream
->lock
);
697 /* Send the header */
698 if (stream
->i_header
> 0) {
699 answer
->i_body
= stream
->i_header
;
700 answer
->p_body
= xmalloc(stream
->i_header
);
701 memcpy(answer
->p_body
, stream
->p_header
, stream
->i_header
);
703 answer
->i_body_offset
= stream
->i_buffer_last_pos
;
704 if (stream
->b_has_keyframes
)
705 cl
->i_keyframe_wait_to_pass
= stream
->i_last_keyframe_seen_pos
;
707 cl
->i_keyframe_wait_to_pass
= -1;
708 vlc_mutex_unlock(&stream
->lock
);
710 httpd_MsgAdd(answer
, "Content-Length", "0");
711 answer
->i_body_offset
= 0;
714 /* FIXME: move to http access_output */
715 if (!strcmp(stream
->psz_mime
, "video/x-ms-asf-stream")) {
716 bool b_xplaystream
= false;
718 httpd_MsgAdd(answer
, "Content-type", "application/octet-stream");
719 httpd_MsgAdd(answer
, "Server", "Cougar 4.1.0.3921");
720 httpd_MsgAdd(answer
, "Pragma", "no-cache");
721 httpd_MsgAdd(answer
, "Pragma", "client-id=%lu",
722 vlc_mrand48()&0x7fff);
723 httpd_MsgAdd(answer
, "Pragma", "features=\"broadcast\"");
725 /* Check if there is a xPlayStrm=1 */
726 for (size_t i
= 0; i
< query
->i_headers
; i
++)
727 if (!strcasecmp(query
->p_headers
[i
].name
, "Pragma") &&
728 strstr(query
->p_headers
[i
].value
, "xPlayStrm=1"))
729 b_xplaystream
= true;
732 answer
->i_body_offset
= 0;
733 } else if (!b_has_content_type
)
734 httpd_MsgAdd(answer
, "Content-type", "%s", stream
->psz_mime
);
736 if (!b_has_cache_control
)
737 httpd_MsgAdd(answer
, "Cache-Control", "no-cache");
739 httpd_MsgAdd(answer
, "Connection", "close");
745 httpd_stream_t
*httpd_StreamNew(httpd_host_t
*host
,
746 const char *psz_url
, const char *psz_mime
,
747 const char *psz_user
, const char *psz_password
)
749 httpd_stream_t
*stream
= malloc(sizeof(*stream
));
753 stream
->url
= httpd_UrlNew(host
, psz_url
, psz_user
, psz_password
);
759 vlc_mutex_init(&stream
->lock
);
760 if (psz_mime
== NULL
|| psz_mime
[0] == '\0')
761 psz_mime
= vlc_mime_Ext2Mime(psz_url
);
762 stream
->psz_mime
= xstrdup(psz_mime
);
764 stream
->i_header
= 0;
765 stream
->p_header
= NULL
;
766 stream
->i_buffer_size
= 5000000; /* 5 Mo per stream */
767 stream
->p_buffer
= xmalloc(stream
->i_buffer_size
);
768 /* We set to 1 to make life simpler
769 * (this way i_body_offset can never be 0) */
770 stream
->i_buffer_pos
= 1;
771 stream
->i_buffer_last_pos
= 1;
772 stream
->b_has_keyframes
= false;
773 stream
->i_last_keyframe_seen_pos
= 0;
774 stream
->i_http_headers
= 0;
775 stream
->p_http_headers
= NULL
;
777 httpd_UrlCatch(stream
->url
, HTTPD_MSG_HEAD
, httpd_StreamCallBack
,
778 (httpd_callback_sys_t
*)stream
);
779 httpd_UrlCatch(stream
->url
, HTTPD_MSG_GET
, httpd_StreamCallBack
,
780 (httpd_callback_sys_t
*)stream
);
781 httpd_UrlCatch(stream
->url
, HTTPD_MSG_POST
, httpd_StreamCallBack
,
782 (httpd_callback_sys_t
*)stream
);
787 int httpd_StreamHeader(httpd_stream_t
*stream
, uint8_t *p_data
, int i_data
)
789 vlc_mutex_lock(&stream
->lock
);
790 free(stream
->p_header
);
791 stream
->p_header
= NULL
;
793 stream
->i_header
= i_data
;
795 stream
->p_header
= xmalloc(i_data
);
796 memcpy(stream
->p_header
, p_data
, i_data
);
798 vlc_mutex_unlock(&stream
->lock
);
803 static void httpd_AppendData(httpd_stream_t
*stream
, uint8_t *p_data
, int i_data
)
805 int i_pos
= stream
->i_buffer_pos
% stream
->i_buffer_size
;
806 int i_count
= i_data
;
807 while (i_count
> 0) {
808 int i_copy
= __MIN(i_count
, stream
->i_buffer_size
- i_pos
);
810 /* Ok, we can't go past the end of our buffer */
811 memcpy(&stream
->p_buffer
[i_pos
], p_data
, i_copy
);
813 i_pos
= (i_pos
+ i_copy
) % stream
->i_buffer_size
;
818 stream
->i_buffer_pos
+= i_data
;
821 int httpd_StreamSend(httpd_stream_t
*stream
, const block_t
*p_block
)
823 if (!p_block
|| !p_block
->p_buffer
)
826 vlc_mutex_lock(&stream
->lock
);
828 /* save this pointer (to be used by new connection) */
829 stream
->i_buffer_last_pos
= stream
->i_buffer_pos
;
831 if (p_block
->i_flags
& BLOCK_FLAG_TYPE_I
) {
832 stream
->b_has_keyframes
= true;
833 stream
->i_last_keyframe_seen_pos
= stream
->i_buffer_pos
;
836 httpd_AppendData(stream
, p_block
->p_buffer
, p_block
->i_buffer
);
838 vlc_mutex_unlock(&stream
->lock
);
842 void httpd_StreamDelete(httpd_stream_t
*stream
)
844 httpd_UrlDelete(stream
->url
);
845 for (size_t i
= 0; i
< stream
->i_http_headers
; i
++) {
846 free(stream
->p_http_headers
[i
].name
);
847 free(stream
->p_http_headers
[i
].value
);
849 free(stream
->p_http_headers
);
850 vlc_mutex_destroy(&stream
->lock
);
851 free(stream
->psz_mime
);
852 free(stream
->p_header
);
853 free(stream
->p_buffer
);
857 /*****************************************************************************
859 *****************************************************************************/
860 static void* httpd_HostThread(void *);
861 static httpd_host_t
*httpd_HostCreate(vlc_object_t
*, const char *,
862 const char *, vlc_tls_creds_t
*);
864 /* create a new host */
865 httpd_host_t
*vlc_http_HostNew(vlc_object_t
*p_this
)
867 return httpd_HostCreate(p_this
, "http-host", "http-port", NULL
);
870 httpd_host_t
*vlc_https_HostNew(vlc_object_t
*obj
)
872 char *cert
= var_InheritString(obj
, "http-cert");
874 msg_Err(obj
, "HTTP/TLS certificate not specified!");
878 char *key
= var_InheritString(obj
, "http-key");
879 vlc_tls_creds_t
*tls
= vlc_tls_ServerCreate(obj
, cert
, key
);
882 msg_Err(obj
, "HTTP/TLS certificate error (%s and %s)",
883 cert
, key
? key
: cert
);
891 return httpd_HostCreate(obj
, "http-host", "https-port", tls
);
894 httpd_host_t
*vlc_rtsp_HostNew(vlc_object_t
*p_this
)
896 return httpd_HostCreate(p_this
, "rtsp-host", "rtsp-port", NULL
);
905 } httpd
= { VLC_STATIC_MUTEX
, NULL
, 0 };
907 static httpd_host_t
*httpd_HostCreate(vlc_object_t
*p_this
,
910 vlc_tls_creds_t
*p_tls
)
913 char *hostname
= var_InheritString(p_this
, hostvar
);
914 unsigned port
= var_InheritInteger(p_this
, portvar
);
917 vlc_UrlParse(&url
, hostname
);
919 if (url
.i_port
!= 0) {
920 msg_Err(p_this
, "Ignoring port %d (using %d)", url
.i_port
, port
);
921 msg_Info(p_this
, "Specify port %d separately with the "
922 "%s option instead.", url
.i_port
, portvar
);
925 /* to be sure to avoid multiple creation */
926 vlc_mutex_lock(&httpd
.mutex
);
928 /* verify if it already exist */
929 for (int i
= 0; i
< httpd
.i_host
; i
++) {
930 host
= httpd
.host
[i
];
932 /* cannot mix TLS and non-TLS hosts */
933 if (host
->port
!= port
934 || (host
->p_tls
!= NULL
) != (p_tls
!= NULL
))
937 /* Increase existing matching host reference count.
938 * The reference count is written under both the global httpd and the
939 * host lock. It is read with either or both locks held. The global
940 * lock is always acquired first. */
941 vlc_mutex_lock(&host
->lock
);
943 vlc_mutex_unlock(&host
->lock
);
945 vlc_mutex_unlock(&httpd
.mutex
);
947 vlc_tls_Delete(p_tls
);
951 /* create the new host */
952 host
= (httpd_host_t
*)vlc_custom_create(p_this
, sizeof (*host
),
957 vlc_mutex_init(&host
->lock
);
958 vlc_cond_init(&host
->wait
);
961 host
->fds
= net_ListenTCP(p_this
, url
.psz_host
, port
);
963 msg_Err(p_this
, "cannot create socket(s) for HTTP host");
966 for (host
->nfd
= 0; host
->fds
[host
->nfd
] != -1; host
->nfd
++);
975 /* create the thread */
976 if (vlc_clone(&host
->thread
, httpd_HostThread
, host
,
977 VLC_THREAD_PRIORITY_LOW
)) {
978 msg_Err(p_this
, "cannot spawn http host thread");
982 /* now add it to httpd */
983 TAB_APPEND(httpd
.i_host
, httpd
.host
, host
);
984 vlc_mutex_unlock(&httpd
.mutex
);
991 vlc_mutex_unlock(&httpd
.mutex
);
994 net_ListenClose(host
->fds
);
995 vlc_cond_destroy(&host
->wait
);
996 vlc_mutex_destroy(&host
->lock
);
997 vlc_object_release(host
);
1001 vlc_tls_Delete(p_tls
);
1006 void httpd_HostDelete(httpd_host_t
*host
)
1008 bool delete = false;
1010 vlc_mutex_lock(&httpd
.mutex
);
1012 vlc_mutex_lock(&host
->lock
);
1014 if (host
->i_ref
== 0)
1016 vlc_mutex_unlock(&host
->lock
);
1019 vlc_mutex_unlock(&httpd
.mutex
);
1020 msg_Dbg(host
, "httpd_HostDelete: host still in use");
1023 TAB_REMOVE(httpd
.i_host
, httpd
.host
, host
);
1025 vlc_cancel(host
->thread
);
1026 vlc_join(host
->thread
, NULL
);
1028 msg_Dbg(host
, "HTTP host removed");
1030 for (int i
= 0; i
< host
->i_url
; i
++)
1031 msg_Err(host
, "url still registered: %s", host
->url
[i
]->psz_url
);
1033 for (int i
= 0; i
< host
->i_client
; i
++) {
1034 msg_Warn(host
, "client still connected");
1035 httpd_ClientDestroy(host
->client
[i
]);
1037 TAB_CLEAN(host
->i_client
, host
->client
);
1039 vlc_tls_Delete(host
->p_tls
);
1040 net_ListenClose(host
->fds
);
1041 vlc_cond_destroy(&host
->wait
);
1042 vlc_mutex_destroy(&host
->lock
);
1043 vlc_object_release(host
);
1044 vlc_mutex_unlock(&httpd
.mutex
);
1047 /* register a new url */
1048 httpd_url_t
*httpd_UrlNew(httpd_host_t
*host
, const char *psz_url
,
1049 const char *psz_user
, const char *psz_password
)
1055 vlc_mutex_lock(&host
->lock
);
1056 for (int i
= 0; i
< host
->i_url
; i
++)
1057 if (!strcmp(psz_url
, host
->url
[i
]->psz_url
)) {
1058 msg_Warn(host
, "cannot add '%s' (url already defined)", psz_url
);
1059 vlc_mutex_unlock(&host
->lock
);
1063 url
= xmalloc(sizeof(httpd_url_t
));
1066 vlc_mutex_init(&url
->lock
);
1067 url
->psz_url
= xstrdup(psz_url
);
1068 url
->psz_user
= xstrdup(psz_user
? psz_user
: "");
1069 url
->psz_password
= xstrdup(psz_password
? psz_password
: "");
1070 for (int i
= 0; i
< HTTPD_MSG_MAX
; i
++) {
1071 url
->catch[i
].cb
= NULL
;
1072 url
->catch[i
].p_sys
= NULL
;
1075 TAB_APPEND(host
->i_url
, host
->url
, url
);
1076 vlc_cond_signal(&host
->wait
);
1077 vlc_mutex_unlock(&host
->lock
);
1082 /* register callback on a url */
1083 int httpd_UrlCatch(httpd_url_t
*url
, int i_msg
, httpd_callback_t cb
,
1084 httpd_callback_sys_t
*p_sys
)
1086 vlc_mutex_lock(&url
->lock
);
1087 url
->catch[i_msg
].cb
= cb
;
1088 url
->catch[i_msg
].p_sys
= p_sys
;
1089 vlc_mutex_unlock(&url
->lock
);
1095 void httpd_UrlDelete(httpd_url_t
*url
)
1097 httpd_host_t
*host
= url
->host
;
1099 vlc_mutex_lock(&host
->lock
);
1100 TAB_REMOVE(host
->i_url
, host
->url
, url
);
1102 vlc_mutex_destroy(&url
->lock
);
1104 free(url
->psz_user
);
1105 free(url
->psz_password
);
1107 for (int i
= 0; i
< host
->i_client
; i
++) {
1108 httpd_client_t
*client
= host
->client
[i
];
1110 if (client
->url
!= url
)
1113 /* TODO complete it */
1114 msg_Warn(host
, "force closing connections");
1115 TAB_REMOVE(host
->i_client
, host
->client
, client
);
1116 httpd_ClientDestroy(client
);
1120 vlc_mutex_unlock(&host
->lock
);
1123 static void httpd_MsgInit(httpd_message_t
*msg
)
1126 msg
->i_type
= HTTPD_MSG_NONE
;
1127 msg
->i_proto
= HTTPD_PROTO_NONE
;
1128 msg
->i_version
= -1; /* FIXME */
1132 msg
->psz_url
= NULL
;
1133 msg
->psz_args
= NULL
;
1136 msg
->p_headers
= NULL
;
1138 msg
->i_body_offset
= 0;
1143 static void httpd_MsgClean(httpd_message_t
*msg
)
1146 free(msg
->psz_args
);
1147 for (size_t i
= 0; i
< msg
->i_headers
; i
++) {
1148 free(msg
->p_headers
[i
].name
);
1149 free(msg
->p_headers
[i
].value
);
1151 free(msg
->p_headers
);
1156 const char *httpd_MsgGet(const httpd_message_t
*msg
, const char *name
)
1158 for (size_t i
= 0; i
< msg
->i_headers
; i
++)
1159 if (!strcasecmp(msg
->p_headers
[i
].name
, name
))
1160 return msg
->p_headers
[i
].value
;
1164 void httpd_MsgAdd(httpd_message_t
*msg
, const char *name
, const char *psz_value
, ...)
1166 httpd_header
*p_tmp
= realloc(msg
->p_headers
, sizeof(httpd_header
) * (msg
->i_headers
+ 1));
1170 msg
->p_headers
= p_tmp
;
1172 httpd_header
*h
= &msg
->p_headers
[msg
->i_headers
];
1173 h
->name
= strdup(name
);
1180 va_start(args
, psz_value
);
1181 int ret
= us_vasprintf(&h
->value
, psz_value
, args
);
1192 static void httpd_ClientInit(httpd_client_t
*cl
, mtime_t now
)
1194 cl
->i_state
= HTTPD_CLIENT_RECEIVING
;
1195 cl
->i_activity_date
= now
;
1196 cl
->i_activity_timeout
= CLOCK_FREQ
*10;
1197 cl
->i_buffer_size
= HTTPD_CL_BUFSIZE
;
1199 cl
->p_buffer
= xmalloc(cl
->i_buffer_size
);
1200 cl
->i_keyframe_wait_to_pass
= -1;
1201 cl
->b_stream_mode
= false;
1203 httpd_MsgInit(&cl
->query
);
1204 httpd_MsgInit(&cl
->answer
);
1207 char* httpd_ClientIP(const httpd_client_t
*cl
, char *ip
, int *port
)
1209 return net_GetPeerAddress(vlc_tls_GetFD(cl
->sock
), ip
, port
) ? NULL
: ip
;
1212 char* httpd_ServerIP(const httpd_client_t
*cl
, char *ip
, int *port
)
1214 return net_GetSockAddress(vlc_tls_GetFD(cl
->sock
), ip
, port
) ? NULL
: ip
;
1217 static void httpd_ClientDestroy(httpd_client_t
*cl
)
1219 vlc_tls_Close(cl
->sock
);
1220 httpd_MsgClean(&cl
->answer
);
1221 httpd_MsgClean(&cl
->query
);
1227 static httpd_client_t
*httpd_ClientNew(vlc_tls_t
*sock
, mtime_t now
)
1229 httpd_client_t
*cl
= malloc(sizeof(httpd_client_t
));
1231 if (!cl
) return NULL
;
1237 httpd_ClientInit(cl
, now
);
1242 ssize_t
httpd_NetRecv (httpd_client_t
*cl
, uint8_t *p
, size_t i_len
)
1244 vlc_tls_t
*sock
= cl
->sock
;
1245 struct iovec iov
= { .iov_base
= p
, .iov_len
= i_len
};
1246 return sock
->readv(sock
, &iov
, 1);
1250 ssize_t
httpd_NetSend (httpd_client_t
*cl
, const uint8_t *p
, size_t i_len
)
1252 vlc_tls_t
*sock
= cl
->sock
;
1253 const struct iovec iov
= { .iov_base
= (void *)p
, .iov_len
= i_len
};
1254 return sock
->writev(sock
, &iov
, 1);
1260 const char name
[16];
1266 { "OPTIONS", HTTPD_MSG_OPTIONS
, HTTPD_PROTO_RTSP
},
1267 { "DESCRIBE", HTTPD_MSG_DESCRIBE
, HTTPD_PROTO_RTSP
},
1268 { "SETUP", HTTPD_MSG_SETUP
, HTTPD_PROTO_RTSP
},
1269 { "PLAY", HTTPD_MSG_PLAY
, HTTPD_PROTO_RTSP
},
1270 { "PAUSE", HTTPD_MSG_PAUSE
, HTTPD_PROTO_RTSP
},
1271 { "GET_PARAMETER", HTTPD_MSG_GETPARAMETER
, HTTPD_PROTO_RTSP
},
1272 { "TEARDOWN", HTTPD_MSG_TEARDOWN
, HTTPD_PROTO_RTSP
},
1273 { "GET", HTTPD_MSG_GET
, HTTPD_PROTO_HTTP
},
1274 { "HEAD", HTTPD_MSG_HEAD
, HTTPD_PROTO_HTTP
},
1275 { "POST", HTTPD_MSG_POST
, HTTPD_PROTO_HTTP
},
1276 { "", HTTPD_MSG_NONE
, HTTPD_PROTO_NONE
}
1280 static void httpd_ClientRecv(httpd_client_t
*cl
)
1284 /* ignore leading whites */
1285 if (cl
->query
.i_proto
== HTTPD_PROTO_NONE
&& cl
->i_buffer
== 0) {
1288 i_len
= httpd_NetRecv(cl
, &c
, 1);
1290 if (i_len
> 0 && !strchr("\r\n\t ", c
)) {
1291 cl
->p_buffer
[0] = c
;
1294 } else if (cl
->query
.i_proto
== HTTPD_PROTO_NONE
) {
1295 /* enough to see if it's Interleaved RTP over RTSP or RTSP/HTTP */
1296 i_len
= httpd_NetRecv(cl
, &cl
->p_buffer
[cl
->i_buffer
],
1299 cl
->i_buffer
+= i_len
;
1301 /* The smallest legal request is 7 bytes ("GET /\r\n"),
1302 * this is the maximum we can ask at this point. */
1303 if (cl
->i_buffer
>= 7) {
1304 if (!memcmp(cl
->p_buffer
, "HTTP/1.", 7)) {
1305 cl
->query
.i_proto
= HTTPD_PROTO_HTTP
;
1306 cl
->query
.i_type
= HTTPD_MSG_ANSWER
;
1307 } else if (!memcmp(cl
->p_buffer
, "RTSP/1.", 7)) {
1308 cl
->query
.i_proto
= HTTPD_PROTO_RTSP
;
1309 cl
->query
.i_type
= HTTPD_MSG_ANSWER
;
1311 /* We need the full request line to determine the protocol. */
1312 cl
->query
.i_proto
= HTTPD_PROTO_HTTP0
;
1313 cl
->query
.i_type
= HTTPD_MSG_NONE
;
1316 } else if (cl
->query
.i_body
> 0) {
1317 /* we are reading the body of a request or a channel */
1318 assert (cl
->query
.p_body
!= NULL
);
1319 i_len
= httpd_NetRecv(cl
, &cl
->query
.p_body
[cl
->i_buffer
],
1320 cl
->query
.i_body
- cl
->i_buffer
);
1322 cl
->i_buffer
+= i_len
;
1324 if (cl
->i_buffer
>= cl
->query
.i_body
)
1325 cl
->i_state
= HTTPD_CLIENT_RECEIVE_DONE
;
1326 } else for (;;) { /* we are reading a header -> char by char */
1327 if (cl
->i_buffer
== cl
->i_buffer_size
) {
1328 uint8_t *newbuf
= realloc(cl
->p_buffer
, cl
->i_buffer_size
+ 1024);
1334 cl
->p_buffer
= newbuf
;
1335 cl
->i_buffer_size
+= 1024;
1338 i_len
= httpd_NetRecv (cl
, &cl
->p_buffer
[cl
->i_buffer
], 1);
1344 if ((cl
->query
.i_proto
== HTTPD_PROTO_HTTP0
)
1345 && (cl
->p_buffer
[cl
->i_buffer
- 1] == '\n'))
1347 /* Request line is now complete */
1348 const char *p
= memchr(cl
->p_buffer
, ' ', cl
->i_buffer
);
1351 assert(cl
->query
.i_type
== HTTPD_MSG_NONE
);
1353 if (!p
) { /* no URI: evil guy */
1354 i_len
= 0; /* drop connection */
1359 p
++; /* skips extra spaces */
1362 p
= memchr(p
, ' ', ((char *)cl
->p_buffer
) + cl
->i_buffer
- p
);
1363 if (!p
) { /* no explicit protocol: HTTP/0.9 */
1364 i_len
= 0; /* not supported currently -> drop */
1369 p
++; /* skips extra spaces ever again */
1372 len
= ((char *)cl
->p_buffer
) + cl
->i_buffer
- p
;
1373 if (len
< 7) /* foreign protocol */
1374 i_len
= 0; /* I don't understand -> drop */
1375 else if (!memcmp(p
, "HTTP/1.", 7)) {
1376 cl
->query
.i_proto
= HTTPD_PROTO_HTTP
;
1377 cl
->query
.i_version
= atoi(p
+ 7);
1378 } else if (!memcmp(p
, "RTSP/1.", 7)) {
1379 cl
->query
.i_proto
= HTTPD_PROTO_RTSP
;
1380 cl
->query
.i_version
= atoi(p
+ 7);
1381 } else if (!memcmp(p
, "HTTP/", 5)) {
1382 const uint8_t sorry
[] =
1383 "HTTP/1.1 505 Unknown HTTP version\r\n\r\n";
1384 httpd_NetSend(cl
, sorry
, sizeof(sorry
) - 1);
1385 i_len
= 0; /* drop */
1386 } else if (!memcmp(p
, "RTSP/", 5)) {
1387 const uint8_t sorry
[] =
1388 "RTSP/1.0 505 Unknown RTSP version\r\n\r\n";
1389 httpd_NetSend(cl
, sorry
, sizeof(sorry
) - 1);
1390 i_len
= 0; /* drop */
1391 } else /* yet another foreign protocol */
1398 if ((cl
->i_buffer
>= 2 && !memcmp(&cl
->p_buffer
[cl
->i_buffer
-2], "\n\n", 2))||
1399 (cl
->i_buffer
>= 4 && !memcmp(&cl
->p_buffer
[cl
->i_buffer
-4], "\r\n\r\n", 4)))
1403 /* we have finished the header so parse it and set i_body */
1404 cl
->p_buffer
[cl
->i_buffer
] = '\0';
1406 if (cl
->query
.i_type
== HTTPD_MSG_ANSWER
) {
1408 * assume strlen("HTTP/1.x") = 8
1410 cl
->query
.i_status
=
1411 strtol((char *)&cl
->p_buffer
[8],
1417 cl
->query
.i_type
= HTTPD_MSG_NONE
;
1419 for (unsigned i
= 0; msg_type
[i
].name
[0]; i
++)
1420 if (!strncmp((char *)cl
->p_buffer
, msg_type
[i
].name
,
1421 strlen(msg_type
[i
].name
))) {
1422 p
= (char *)&cl
->p_buffer
[strlen(msg_type
[i
].name
) + 1 ];
1423 cl
->query
.i_type
= msg_type
[i
].i_type
;
1424 if (cl
->query
.i_proto
!= msg_type
[i
].i_proto
) {
1426 cl
->query
.i_proto
= HTTPD_PROTO_NONE
;
1427 cl
->query
.i_type
= HTTPD_MSG_NONE
;
1433 if (strstr((char *)cl
->p_buffer
, "HTTP/1."))
1434 cl
->query
.i_proto
= HTTPD_PROTO_HTTP
;
1435 else if (strstr((char *)cl
->p_buffer
, "RTSP/1."))
1436 cl
->query
.i_proto
= HTTPD_PROTO_RTSP
;
1444 p2
= strchr(p
, ' ');
1448 if (!strncasecmp(p
, (cl
->query
.i_proto
== HTTPD_PROTO_HTTP
) ? "http:" : "rtsp:", 5)) {
1449 /* Skip hier-part of URL (if present) */
1451 if (!strncmp(p
, "//", 2)) { /* skip authority */
1452 /* see RFC3986 §3.2 */
1454 p
+= strcspn(p
, "/?#");
1457 else if (!strncasecmp(p
, (cl
->query
.i_proto
== HTTPD_PROTO_HTTP
) ? "https:" : "rtsps:", 6)) {
1458 /* Skip hier-part of URL (if present) */
1460 if (!strncmp(p
, "//", 2)) { /* skip authority */
1461 /* see RFC3986 §3.2 */
1463 p
+= strcspn(p
, "/?#");
1467 cl
->query
.psz_url
= strdup(p
);
1468 if ((p3
= strchr(cl
->query
.psz_url
, '?')) ) {
1470 cl
->query
.psz_args
= (uint8_t *)strdup(p3
);
1476 p
= strchr(p
, '\n');
1479 while (*p
== '\n' || *p
== '\r')
1484 char *eol
= p
= strchr(p
, '\n');
1487 while (eol
&& eol
>= line
&& (*eol
== '\n' || *eol
== '\r'))
1490 if ((colon
= strchr(line
, ':'))) {
1492 while (*colon
== ' ')
1494 httpd_MsgAdd(&cl
->query
, line
, "%s", colon
);
1496 if (!strcasecmp(line
, "Content-Length"))
1497 cl
->query
.i_body
= atol(colon
);
1502 while (*p
== '\n' || *p
== '\r')
1507 if (cl
->query
.i_body
> 0) {
1508 /* TODO Mhh, handle the case where the client only
1509 * sends a request and closes the connection to
1510 * mark the end of the body (probably only RTSP) */
1511 if (cl
->query
.i_body
< 65536)
1512 cl
->query
.p_body
= malloc(cl
->query
.i_body
);
1514 cl
->query
.p_body
= NULL
;
1516 if (!cl
->query
.p_body
) {
1517 switch (cl
->query
.i_proto
) {
1518 case HTTPD_PROTO_HTTP
: {
1519 const uint8_t sorry
[] = "HTTP/1.1 413 Request Entity Too Large\r\n\r\n";
1520 httpd_NetSend(cl
, sorry
, sizeof(sorry
) - 1);
1523 case HTTPD_PROTO_RTSP
: {
1524 const uint8_t sorry
[] = "RTSP/1.0 413 Request Entity Too Large\r\n\r\n";
1525 httpd_NetSend(cl
, sorry
, sizeof(sorry
) - 1);
1529 vlc_assert_unreachable();
1531 i_len
= 0; /* drop */
1535 cl
->i_state
= HTTPD_CLIENT_RECEIVE_DONE
;
1539 /* check if the client is to be set to dead */
1541 if ((i_len
< 0 && WSAGetLastError() != WSAEWOULDBLOCK
) || (i_len
== 0))
1543 if ((i_len
< 0 && errno
!= EAGAIN
) || (i_len
== 0))
1546 if (cl
->query
.i_proto
!= HTTPD_PROTO_NONE
&& cl
->query
.i_type
!= HTTPD_MSG_NONE
) {
1547 /* connection closed -> end of data */
1548 if (cl
->query
.i_body
> 0)
1549 cl
->query
.i_body
= cl
->i_buffer
;
1550 cl
->i_state
= HTTPD_CLIENT_RECEIVE_DONE
;
1553 cl
->i_state
= HTTPD_CLIENT_DEAD
;
1556 /* XXX: for QT I have to disable timeout. Try to find why */
1557 if (cl
->query
.i_proto
== HTTPD_PROTO_RTSP
)
1558 cl
->i_activity_timeout
= 0;
1561 static void httpd_ClientSend(httpd_client_t
*cl
)
1565 if (cl
->i_buffer
< 0) {
1566 /* We need to create the header */
1569 const char *psz_status
= httpd_ReasonFromCode(cl
->answer
.i_status
);
1571 i_size
= strlen("HTTP/1.") + 10 + 10 + strlen(psz_status
) + 5;
1572 for (size_t i
= 0; i
< cl
->answer
.i_headers
; i
++)
1573 i_size
+= strlen(cl
->answer
.p_headers
[i
].name
) + 2 +
1574 strlen(cl
->answer
.p_headers
[i
].value
) + 2;
1576 if (cl
->i_buffer_size
< i_size
) {
1577 cl
->i_buffer_size
= i_size
;
1579 cl
->p_buffer
= xmalloc(i_size
);
1581 p
= (char *)cl
->p_buffer
;
1583 p
+= sprintf(p
, "%s.%u %d %s\r\n",
1584 cl
->answer
.i_proto
== HTTPD_PROTO_HTTP
? "HTTP/1" : "RTSP/1",
1585 cl
->answer
.i_version
,
1586 cl
->answer
.i_status
, psz_status
);
1587 for (size_t i
= 0; i
< cl
->answer
.i_headers
; i
++)
1588 p
+= sprintf(p
, "%s: %s\r\n", cl
->answer
.p_headers
[i
].name
,
1589 cl
->answer
.p_headers
[i
].value
);
1590 p
+= sprintf(p
, "\r\n");
1593 cl
->i_buffer_size
= (uint8_t*)p
- cl
->p_buffer
;
1596 i_len
= httpd_NetSend(cl
, &cl
->p_buffer
[cl
->i_buffer
],
1597 cl
->i_buffer_size
- cl
->i_buffer
);
1599 cl
->i_buffer
+= i_len
;
1601 if (cl
->i_buffer
>= cl
->i_buffer_size
) {
1602 if (cl
->answer
.i_body
== 0 && cl
->answer
.i_body_offset
> 0) {
1603 /* catch more body data */
1604 int i_msg
= cl
->query
.i_type
;
1605 int64_t i_offset
= cl
->answer
.i_body_offset
;
1607 httpd_MsgClean(&cl
->answer
);
1608 cl
->answer
.i_body_offset
= i_offset
;
1610 cl
->url
->catch[i_msg
].cb(cl
->url
->catch[i_msg
].p_sys
, cl
,
1611 &cl
->answer
, &cl
->query
);
1614 if (cl
->answer
.i_body
> 0) {
1615 /* send the body data */
1617 cl
->p_buffer
= cl
->answer
.p_body
;
1618 cl
->i_buffer_size
= cl
->answer
.i_body
;
1621 cl
->answer
.i_body
= 0;
1622 cl
->answer
.p_body
= NULL
;
1623 } else /* send finished */
1624 cl
->i_state
= HTTPD_CLIENT_SEND_DONE
;
1628 if ((i_len
< 0 && WSAGetLastError() != WSAEWOULDBLOCK
) || (i_len
== 0))
1630 if ((i_len
< 0 && errno
!= EAGAIN
) || (i_len
== 0))
1634 cl
->i_state
= HTTPD_CLIENT_DEAD
;
1639 static void httpd_ClientTlsHandshake(httpd_host_t
*host
, httpd_client_t
*cl
)
1641 switch (vlc_tls_SessionHandshake(host
->p_tls
, cl
->sock
))
1643 case -1: cl
->i_state
= HTTPD_CLIENT_DEAD
; break;
1644 case 0: cl
->i_state
= HTTPD_CLIENT_RECEIVING
; break;
1645 case 1: cl
->i_state
= HTTPD_CLIENT_TLS_HS_IN
; break;
1646 case 2: cl
->i_state
= HTTPD_CLIENT_TLS_HS_OUT
; break;
1650 static bool httpdAuthOk(const char *user
, const char *pass
, const char *b64
)
1652 if (!*user
&& !*pass
)
1658 if (strncasecmp(b64
, "BASIC", 5))
1665 char *given_user
= vlc_b64_decode(b64
);
1669 char *given_pass
= NULL
;
1670 given_pass
= strchr (given_user
, ':');
1674 *given_pass
++ = '\0';
1676 if (strcmp (given_user
, user
))
1679 if (strcmp (given_pass
, pass
))
1690 static void httpdLoop(httpd_host_t
*host
)
1692 struct pollfd ufd
[host
->nfd
+ host
->i_client
];
1694 for (nfd
= 0; nfd
< host
->nfd
; nfd
++) {
1695 ufd
[nfd
].fd
= host
->fds
[nfd
];
1696 ufd
[nfd
].events
= POLLIN
;
1697 ufd
[nfd
].revents
= 0;
1700 /* add all socket that should be read/write and close dead connection */
1701 while (host
->i_url
<= 0) {
1702 mutex_cleanup_push(&host
->lock
);
1703 vlc_cond_wait(&host
->wait
, &host
->lock
);
1707 mtime_t now
= mdate();
1708 bool b_low_delay
= false;
1710 int canc
= vlc_savecancel();
1711 for (int i_client
= 0; i_client
< host
->i_client
; i_client
++) {
1713 httpd_client_t
*cl
= host
->client
[i_client
];
1714 if (cl
->i_ref
< 0 || (cl
->i_ref
== 0 &&
1715 (cl
->i_state
== HTTPD_CLIENT_DEAD
||
1716 (cl
->i_activity_timeout
> 0 &&
1717 cl
->i_activity_date
+cl
->i_activity_timeout
< now
)))) {
1718 TAB_REMOVE(host
->i_client
, host
->client
, cl
);
1720 httpd_ClientDestroy(cl
);
1724 struct pollfd
*pufd
= ufd
+ nfd
;
1725 assert (pufd
< ufd
+ (sizeof (ufd
) / sizeof (ufd
[0])));
1727 pufd
->fd
= vlc_tls_GetFD(cl
->sock
);
1728 pufd
->events
= pufd
->revents
= 0;
1730 switch (cl
->i_state
) {
1731 case HTTPD_CLIENT_RECEIVING
:
1732 case HTTPD_CLIENT_TLS_HS_IN
:
1733 pufd
->events
= POLLIN
;
1736 case HTTPD_CLIENT_SENDING
:
1737 case HTTPD_CLIENT_TLS_HS_OUT
:
1738 pufd
->events
= POLLOUT
;
1741 case HTTPD_CLIENT_RECEIVE_DONE
: {
1742 httpd_message_t
*answer
= &cl
->answer
;
1743 httpd_message_t
*query
= &cl
->query
;
1745 httpd_MsgInit(answer
);
1747 /* Handle what we received */
1748 switch (query
->i_type
) {
1749 case HTTPD_MSG_ANSWER
:
1751 cl
->i_state
= HTTPD_CLIENT_DEAD
;
1754 case HTTPD_MSG_OPTIONS
:
1755 answer
->i_type
= HTTPD_MSG_ANSWER
;
1756 answer
->i_proto
= query
->i_proto
;
1757 answer
->i_status
= 200;
1759 answer
->p_body
= NULL
;
1761 httpd_MsgAdd(answer
, "Server", "VLC/%s", VERSION
);
1762 httpd_MsgAdd(answer
, "Content-Length", "0");
1764 switch(query
->i_proto
) {
1765 case HTTPD_PROTO_HTTP
:
1766 answer
->i_version
= 1;
1767 httpd_MsgAdd(answer
, "Allow", "GET,HEAD,POST,OPTIONS");
1770 case HTTPD_PROTO_RTSP
:
1771 answer
->i_version
= 0;
1773 const char *p
= httpd_MsgGet(query
, "Cseq");
1775 httpd_MsgAdd(answer
, "Cseq", "%s", p
);
1776 p
= httpd_MsgGet(query
, "Timestamp");
1778 httpd_MsgAdd(answer
, "Timestamp", "%s", p
);
1780 p
= httpd_MsgGet(query
, "Require");
1782 answer
->i_status
= 551;
1783 httpd_MsgAdd(query
, "Unsupported", "%s", p
);
1786 httpd_MsgAdd(answer
, "Public", "DESCRIBE,SETUP,"
1787 "TEARDOWN,PLAY,PAUSE,GET_PARAMETER");
1791 if (httpd_MsgGet(&cl
->query
, "Connection") != NULL
)
1792 httpd_MsgAdd(answer
, "Connection", "close");
1794 cl
->i_buffer
= -1; /* Force the creation of the answer in
1795 * httpd_ClientSend */
1796 cl
->i_state
= HTTPD_CLIENT_SENDING
;
1799 case HTTPD_MSG_NONE
:
1800 if (query
->i_proto
== HTTPD_PROTO_NONE
) {
1802 cl
->i_state
= HTTPD_CLIENT_DEAD
;
1805 answer
->i_proto
= query
->i_proto
;
1806 answer
->i_type
= HTTPD_MSG_ANSWER
;
1807 answer
->i_version
= 0;
1808 answer
->i_status
= 501;
1811 answer
->i_body
= httpd_HtmlError (&p
, 501, NULL
);
1812 answer
->p_body
= (uint8_t *)p
;
1813 httpd_MsgAdd(answer
, "Content-Length", "%d", answer
->i_body
);
1814 httpd_MsgAdd(answer
, "Connection", "close");
1816 cl
->i_buffer
= -1; /* Force the creation of the answer in httpd_ClientSend */
1817 cl
->i_state
= HTTPD_CLIENT_SENDING
;
1822 int i_msg
= query
->i_type
;
1823 bool b_auth_failed
= false;
1825 /* Search the url and trigger callbacks */
1826 for (int i
= 0; i
< host
->i_url
; i
++) {
1827 httpd_url_t
*url
= host
->url
[i
];
1829 if (strcmp(url
->psz_url
, query
->psz_url
))
1831 if (!url
->catch[i_msg
].cb
)
1835 b_auth_failed
= !httpdAuthOk(url
->psz_user
,
1837 httpd_MsgGet(query
, "Authorization")); /* BASIC id */
1842 if (url
->catch[i_msg
].cb(url
->catch[i_msg
].p_sys
, cl
, answer
, query
))
1845 if (answer
->i_proto
== HTTPD_PROTO_NONE
)
1846 cl
->i_buffer
= cl
->i_buffer_size
; /* Raw answer from a CGI */
1850 /* only one url can answer */
1857 answer
->i_proto
= query
->i_proto
;
1858 answer
->i_type
= HTTPD_MSG_ANSWER
;
1859 answer
->i_version
= 0;
1861 if (b_auth_failed
) {
1862 httpd_MsgAdd(answer
, "WWW-Authenticate",
1863 "Basic realm=\"VLC stream\"");
1864 answer
->i_status
= 401;
1866 answer
->i_status
= 404; /* no url registered */
1869 answer
->i_body
= httpd_HtmlError (&p
, answer
->i_status
,
1871 answer
->p_body
= (uint8_t *)p
;
1873 cl
->i_buffer
= -1; /* Force the creation of the answer in httpd_ClientSend */
1874 httpd_MsgAdd(answer
, "Content-Length", "%d", answer
->i_body
);
1875 httpd_MsgAdd(answer
, "Content-Type", "%s", "text/html");
1876 if (httpd_MsgGet(&cl
->query
, "Connection") != NULL
)
1877 httpd_MsgAdd(answer
, "Connection", "close");
1880 cl
->i_state
= HTTPD_CLIENT_SENDING
;
1886 case HTTPD_CLIENT_SEND_DONE
:
1887 if (!cl
->b_stream_mode
|| cl
->answer
.i_body_offset
== 0) {
1888 bool do_close
= false;
1892 if (cl
->query
.i_proto
!= HTTPD_PROTO_HTTP
1893 || cl
->query
.i_version
> 0)
1895 const char *psz_connection
= httpd_MsgGet(&cl
->answer
,
1897 if (psz_connection
!= NULL
)
1898 do_close
= !strcasecmp(psz_connection
, "close");
1904 httpd_MsgClean(&cl
->query
);
1905 httpd_MsgInit(&cl
->query
);
1908 cl
->i_buffer_size
= 1000;
1910 cl
->p_buffer
= xmalloc(cl
->i_buffer_size
);
1911 cl
->i_state
= HTTPD_CLIENT_RECEIVING
;
1913 cl
->i_state
= HTTPD_CLIENT_DEAD
;
1914 httpd_MsgClean(&cl
->answer
);
1916 i_offset
= cl
->answer
.i_body_offset
;
1917 httpd_MsgClean(&cl
->answer
);
1919 cl
->answer
.i_body_offset
= i_offset
;
1921 cl
->p_buffer
= NULL
;
1923 cl
->i_buffer_size
= 0;
1925 cl
->i_state
= HTTPD_CLIENT_WAITING
;
1929 case HTTPD_CLIENT_WAITING
:
1930 i_offset
= cl
->answer
.i_body_offset
;
1931 int i_msg
= cl
->query
.i_type
;
1933 httpd_MsgInit(&cl
->answer
);
1934 cl
->answer
.i_body_offset
= i_offset
;
1936 cl
->url
->catch[i_msg
].cb(cl
->url
->catch[i_msg
].p_sys
, cl
,
1937 &cl
->answer
, &cl
->query
);
1938 if (cl
->answer
.i_type
!= HTTPD_MSG_NONE
) {
1939 /* we have new data, so re-enter send mode */
1941 cl
->p_buffer
= cl
->answer
.p_body
;
1942 cl
->i_buffer_size
= cl
->answer
.i_body
;
1943 cl
->answer
.p_body
= NULL
;
1944 cl
->answer
.i_body
= 0;
1945 cl
->i_state
= HTTPD_CLIENT_SENDING
;
1949 if (pufd
->events
!= 0)
1954 vlc_mutex_unlock(&host
->lock
);
1955 vlc_restorecancel(canc
);
1957 /* we will wait 20ms (not too big) if HTTPD_CLIENT_WAITING */
1958 while (poll(ufd
, nfd
, b_low_delay
? 20 : -1) < 0)
1961 msg_Err(host
, "polling error: %s", vlc_strerror_c(errno
));
1964 canc
= vlc_savecancel();
1965 vlc_mutex_lock(&host
->lock
);
1967 /* Handle client sockets */
1971 for (int i_client
= 0; i_client
< host
->i_client
; i_client
++) {
1972 httpd_client_t
*cl
= host
->client
[i_client
];
1973 const struct pollfd
*pufd
= &ufd
[nfd
];
1975 assert(pufd
< &ufd
[sizeof(ufd
) / sizeof(ufd
[0])]);
1977 if (vlc_tls_GetFD(cl
->sock
) != pufd
->fd
)
1978 continue; // we were not waiting for this client
1980 if (pufd
->revents
== 0)
1981 continue; // no event received
1983 cl
->i_activity_date
= now
;
1985 switch (cl
->i_state
) {
1986 case HTTPD_CLIENT_RECEIVING
: httpd_ClientRecv(cl
); break;
1987 case HTTPD_CLIENT_SENDING
: httpd_ClientSend(cl
); break;
1988 case HTTPD_CLIENT_TLS_HS_IN
:
1989 case HTTPD_CLIENT_TLS_HS_OUT
:
1990 httpd_ClientTlsHandshake(host
, cl
);
1995 /* Handle server sockets (accept new connections) */
1996 for (nfd
= 0; nfd
< host
->nfd
; nfd
++) {
1998 int fd
= ufd
[nfd
].fd
;
2000 assert (fd
== host
->fds
[nfd
]);
2002 if (ufd
[nfd
].revents
== 0)
2006 fd
= vlc_accept (fd
, NULL
, NULL
, true);
2009 setsockopt (fd
, SOL_SOCKET
, SO_REUSEADDR
,
2010 &(int){ 1 }, sizeof(int));
2012 vlc_tls_t
*sk
= vlc_tls_SocketOpen(fd
);
2013 if (unlikely(sk
== NULL
))
2019 if (host
->p_tls
!= NULL
)
2021 const char *alpn
[] = { "http/1.1", NULL
};
2024 tls
= vlc_tls_ServerSessionCreate(host
->p_tls
, sk
, alpn
);
2027 vlc_tls_SessionDelete(sk
);
2033 cl
= httpd_ClientNew(sk
, now
);
2035 if (host
->p_tls
!= NULL
)
2036 cl
->i_state
= HTTPD_CLIENT_TLS_HS_OUT
;
2038 TAB_APPEND(host
->i_client
, host
->client
, cl
);
2041 vlc_restorecancel(canc
);
2044 static void* httpd_HostThread(void *data
)
2046 httpd_host_t
*host
= data
;
2048 vlc_mutex_lock(&host
->lock
);
2049 while (host
->i_ref
> 0)
2051 vlc_mutex_unlock(&host
->lock
);
2055 int httpd_StreamSetHTTPHeaders(httpd_stream_t
* p_stream
,
2056 const httpd_header
*p_headers
, size_t i_headers
)
2059 return VLC_EGENERIC
;
2061 vlc_mutex_lock(&p_stream
->lock
);
2062 if (p_stream
->p_http_headers
) {
2063 for (size_t i
= 0; i
< p_stream
->i_http_headers
; i
++) {
2064 free(p_stream
->p_http_headers
[i
].name
);
2065 free(p_stream
->p_http_headers
[i
].value
);
2067 free(p_stream
->p_http_headers
);
2068 p_stream
->p_http_headers
= NULL
;
2069 p_stream
->i_http_headers
= 0;
2072 if (!p_headers
|| !i_headers
) {
2073 vlc_mutex_unlock(&p_stream
->lock
);
2077 p_stream
->p_http_headers
= vlc_alloc(i_headers
, sizeof(httpd_header
));
2078 if (!p_stream
->p_http_headers
) {
2079 vlc_mutex_unlock(&p_stream
->lock
);
2084 for (size_t i
= 0; i
< i_headers
; i
++) {
2085 if (unlikely(!p_headers
[i
].name
|| !p_headers
[i
].value
))
2088 p_stream
->p_http_headers
[j
].name
= strdup(p_headers
[i
].name
);
2089 p_stream
->p_http_headers
[j
].value
= strdup(p_headers
[i
].value
);
2091 if (unlikely(!p_stream
->p_http_headers
[j
].name
||
2092 !p_stream
->p_http_headers
[j
].value
)) {
2093 free(p_stream
->p_http_headers
[j
].name
);
2094 free(p_stream
->p_http_headers
[j
].value
);
2099 p_stream
->i_http_headers
= j
;
2100 vlc_mutex_unlock(&p_stream
->lock
);