2 This file is part of PulseAudio.
4 Copyright 2005-2009 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <pulse/util.h>
32 #include <pulse/xmalloc.h>
33 #include <pulse/timeval.h>
35 #include <pulsecore/ioline.h>
36 #include <pulsecore/thread-mq.h>
37 #include <pulsecore/macro.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/namereg.h>
40 #include <pulsecore/cli-text.h>
41 #include <pulsecore/shared.h>
42 #include <pulsecore/core-error.h>
43 #include <pulsecore/mime-type.h>
45 #include "protocol-http.h"
47 /* Don't allow more than this many concurrent connections */
48 #define MAX_CONNECTIONS 10
51 #define URL_CSS "/style"
52 #define URL_STATUS "/status"
53 #define URL_LISTEN "/listen"
54 #define URL_LISTEN_SOURCE "/listen/source/"
56 #define MIME_HTML "text/html; charset=utf-8"
57 #define MIME_TEXT "text/plain; charset=utf-8"
58 #define MIME_CSS "text/css"
60 #define HTML_HEADER(t) \
61 "<?xml version=\"1.0\"?>\n" \
62 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" \
63 "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" \
65 " <title>"t"</title>\n" \
66 " <link rel=\"stylesheet\" type=\"text/css\" href=\"style\"/>\n" \
74 #define RECORD_BUFFER_SECONDS (5)
75 #define DEFAULT_SOURCE_LATENCY (300*PA_USEC_PER_MSEC)
89 pa_http_protocol
*protocol
;
92 pa_memblockq
*output_memblockq
;
93 pa_source_output
*source_output
;
101 struct pa_http_protocol
{
105 pa_idxset
*connections
;
111 SOURCE_OUTPUT_MESSAGE_POST_DATA
= PA_SOURCE_OUTPUT_MESSAGE_MAX
114 /* Called from main context */
115 static void connection_unlink(struct connection
*c
) {
118 if (c
->source_output
) {
119 pa_source_output_unlink(c
->source_output
);
120 c
->source_output
->userdata
= NULL
;
121 pa_source_output_unref(c
->source_output
);
125 pa_client_free(c
->client
);
130 pa_ioline_unref(c
->line
);
133 pa_iochannel_free(c
->io
);
135 if (c
->output_memblockq
)
136 pa_memblockq_free(c
->output_memblockq
);
138 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
143 /* Called from main context */
144 static int do_write(struct connection
*c
) {
151 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
154 pa_assert(chunk
.memblock
);
155 pa_assert(chunk
.length
> 0);
157 p
= pa_memblock_acquire(chunk
.memblock
);
158 r
= pa_iochannel_write(c
->io
, (uint8_t*) p
+chunk
.index
, chunk
.length
);
159 pa_memblock_release(chunk
.memblock
);
161 pa_memblock_unref(chunk
.memblock
);
165 if (errno
== EINTR
|| errno
== EAGAIN
)
168 pa_log("write(): %s", pa_cstrerror(errno
));
172 pa_memblockq_drop(c
->output_memblockq
, (size_t) r
);
177 /* Called from main context */
178 static void do_work(struct connection
*c
) {
181 if (pa_iochannel_is_hungup(c
->io
))
184 if (pa_iochannel_is_writable(c
->io
))
191 connection_unlink(c
);
194 /* Called from thread context, except when it is not */
195 static int source_output_process_msg(pa_msgobject
*m
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
196 pa_source_output
*o
= PA_SOURCE_OUTPUT(m
);
197 struct connection
*c
;
199 pa_source_output_assert_ref(o
);
201 if (!(c
= o
->userdata
))
206 case SOURCE_OUTPUT_MESSAGE_POST_DATA
:
207 /* While this function is usually called from IO thread
208 * context, this specific command is not! */
209 pa_memblockq_push_align(c
->output_memblockq
, chunk
);
214 return pa_source_output_process_msg(m
, code
, userdata
, offset
, chunk
);
220 /* Called from thread context */
221 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
222 struct connection
*c
;
224 pa_source_output_assert_ref(o
);
225 pa_assert_se(c
= o
->userdata
);
228 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(o
), SOURCE_OUTPUT_MESSAGE_POST_DATA
, NULL
, 0, chunk
, NULL
);
231 /* Called from main context */
232 static void source_output_kill_cb(pa_source_output
*o
) {
235 pa_source_output_assert_ref(o
);
236 pa_assert_se(c
= o
->userdata
);
238 connection_unlink(c
);
241 /* Called from main context */
242 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
245 pa_source_output_assert_ref(o
);
246 pa_assert_se(c
= o
->userdata
);
248 return pa_bytes_to_usec(pa_memblockq_get_length(c
->output_memblockq
), &c
->source_output
->sample_spec
);
251 /*** client callbacks ***/
252 static void client_kill_cb(pa_client
*client
) {
256 pa_assert_se(c
= client
->userdata
);
258 connection_unlink(c
);
261 /*** pa_iochannel callbacks ***/
262 static void io_callback(pa_iochannel
*io
, void *userdata
) {
263 struct connection
*c
= userdata
;
271 static char *escape_html(const char *t
) {
275 sb
= pa_strbuf_new();
277 for (e
= p
= t
; *p
; p
++) {
279 if (*p
== '>' || *p
== '<' || *p
== '&') {
282 pa_strbuf_putsn(sb
, e
, p
-e
);
287 pa_strbuf_puts(sb
, ">");
289 pa_strbuf_puts(sb
, "<");
291 pa_strbuf_puts(sb
, "&");
296 pa_strbuf_putsn(sb
, e
, p
-e
);
298 return pa_strbuf_tostring_free(sb
);
301 static void http_response(
302 struct connection
*c
,
313 s
= pa_sprintf_malloc(
315 "Connection: close\n"
317 "Cache-Control: no-cache\n"
319 "Server: "PACKAGE_NAME
"/"PACKAGE_VERSION
"\n"
320 "\n", code
, msg
, mime
);
321 pa_ioline_puts(c
->line
, s
);
325 static void html_response(
326 struct connection
*c
,
334 http_response(c
, code
, msg
, MIME_HTML
);
336 if (c
->method
== METHOD_HEAD
) {
337 pa_ioline_defer_close(c
->line
);
344 s
= pa_sprintf_malloc(
350 pa_ioline_puts(c
->line
, s
);
353 pa_ioline_defer_close(c
->line
);
356 static void html_print_field(pa_ioline
*line
, const char *left
, const char *right
) {
357 char *eleft
, *eright
;
359 eleft
= escape_html(left
);
360 eright
= escape_html(right
);
362 pa_ioline_printf(line
,
363 "<tr><td><b>%s</b></td>"
364 "<td>%s</td></tr>\n", eleft
, eright
);
370 static void handle_root(struct connection
*c
) {
375 http_response(c
, 200, "OK", MIME_HTML
);
377 if (c
->method
== METHOD_HEAD
) {
378 pa_ioline_defer_close(c
->line
);
382 pa_ioline_puts(c
->line
,
383 HTML_HEADER(PACKAGE_NAME
" "PACKAGE_VERSION
)
384 "<h1>"PACKAGE_NAME
" "PACKAGE_VERSION
"</h1>\n"
387 t
= pa_get_user_name_malloc();
388 html_print_field(c
->line
, "User Name:", t
);
391 t
= pa_get_host_name_malloc();
392 html_print_field(c
->line
, "Host name:", t
);
396 html_print_field(c
->line
, "Machine ID:", t
);
399 t
= pa_uname_string();
400 html_print_field(c
->line
, "System:", t
);
403 t
= pa_sprintf_malloc("%lu", (unsigned long) getpid());
404 html_print_field(c
->line
, "Process ID:", t
);
407 pa_ioline_puts(c
->line
,
409 "<p><a href=\"" URL_STATUS
"\">Show an extensive server status report</a></p>\n"
410 "<p><a href=\"" URL_LISTEN
"\">Monitor sinks and sources</a></p>\n"
413 pa_ioline_defer_close(c
->line
);
416 static void handle_css(struct connection
*c
) {
419 http_response(c
, 200, "OK", MIME_CSS
);
421 if (c
->method
== METHOD_HEAD
) {
422 pa_ioline_defer_close(c
->line
);
426 pa_ioline_puts(c
->line
,
427 "body { color: black; background-color: white; }\n"
428 "a:link, a:visited { color: #900000; }\n"
429 "div.news-date { font-size: 80%; font-style: italic; }\n"
430 "pre { background-color: #f0f0f0; padding: 0.4cm; }\n"
431 ".grey { color: #8f8f8f; font-size: 80%; }"
432 "table { margin-left: 1cm; border:1px solid lightgrey; padding: 0.2cm; }\n"
433 "td { padding-left:10px; padding-right:10px; }\n");
435 pa_ioline_defer_close(c
->line
);
438 static void handle_status(struct connection
*c
) {
443 http_response(c
, 200, "OK", MIME_TEXT
);
445 if (c
->method
== METHOD_HEAD
) {
446 pa_ioline_defer_close(c
->line
);
450 r
= pa_full_status_string(c
->protocol
->core
);
451 pa_ioline_puts(c
->line
, r
);
454 pa_ioline_defer_close(c
->line
);
457 static void handle_listen(struct connection
*c
) {
462 http_response(c
, 200, "OK", MIME_HTML
);
464 pa_ioline_puts(c
->line
,
465 HTML_HEADER("Listen")
469 if (c
->method
== METHOD_HEAD
) {
470 pa_ioline_defer_close(c
->line
);
474 PA_IDXSET_FOREACH(sink
, c
->protocol
->core
->sinks
, idx
) {
477 t
= escape_html(pa_strna(pa_proplist_gets(sink
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)));
478 m
= pa_sample_spec_to_mime_type_mimefy(&sink
->sample_spec
, &sink
->channel_map
);
480 pa_ioline_printf(c
->line
,
481 "<a href=\"" URL_LISTEN_SOURCE
"%s\" title=\"%s\">%s</a><br/>\n",
482 sink
->monitor_source
->name
, m
, t
);
488 pa_ioline_puts(c
->line
,
493 PA_IDXSET_FOREACH(source
, c
->protocol
->core
->sources
, idx
) {
496 if (source
->monitor_of
)
499 t
= escape_html(pa_strna(pa_proplist_gets(source
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)));
500 m
= pa_sample_spec_to_mime_type_mimefy(&source
->sample_spec
, &source
->channel_map
);
502 pa_ioline_printf(c
->line
,
503 "<a href=\"" URL_LISTEN_SOURCE
"%s\" title=\"%s\">%s</a><br/>\n",
511 pa_ioline_puts(c
->line
,
515 pa_ioline_defer_close(c
->line
);
518 static void line_drain_callback(pa_ioline
*l
, void *userdata
) {
519 struct connection
*c
;
522 pa_assert_se(c
= userdata
);
524 /* We don't need the line reader anymore, instead we need a real
525 * binary io channel */
526 pa_assert_se(c
->io
= pa_ioline_detach_iochannel(c
->line
));
527 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
529 pa_iochannel_socket_set_sndbuf(c
->io
, pa_memblockq_get_length(c
->output_memblockq
));
531 pa_ioline_unref(c
->line
);
535 static void handle_listen_prefix(struct connection
*c
, const char *source_name
) {
537 pa_source_output_new_data data
;
544 pa_assert(source_name
);
549 if (!(source
= pa_namereg_get(c
->protocol
->core
, source_name
, PA_NAMEREG_SOURCE
))) {
550 html_response(c
, 404, "Source not found", NULL
);
554 ss
= source
->sample_spec
;
555 cm
= source
->channel_map
;
557 pa_sample_spec_mimefy(&ss
, &cm
);
559 pa_source_output_new_data_init(&data
);
560 data
.driver
= __FILE__
;
561 data
.module
= c
->module
;
562 data
.client
= c
->client
;
563 pa_source_output_new_data_set_source(&data
, source
, FALSE
);
564 pa_proplist_update(data
.proplist
, PA_UPDATE_MERGE
, c
->client
->proplist
);
565 pa_source_output_new_data_set_sample_spec(&data
, &ss
);
566 pa_source_output_new_data_set_channel_map(&data
, &cm
);
568 pa_source_output_new(&c
->source_output
, c
->protocol
->core
, &data
);
569 pa_source_output_new_data_done(&data
);
571 if (!c
->source_output
) {
572 html_response(c
, 403, "Cannot create source output", NULL
);
576 c
->source_output
->parent
.process_msg
= source_output_process_msg
;
577 c
->source_output
->push
= source_output_push_cb
;
578 c
->source_output
->kill
= source_output_kill_cb
;
579 c
->source_output
->get_latency
= source_output_get_latency_cb
;
580 c
->source_output
->userdata
= c
;
582 pa_source_output_set_requested_latency(c
->source_output
, DEFAULT_SOURCE_LATENCY
);
584 l
= (size_t) (pa_bytes_per_second(&ss
)*RECORD_BUFFER_SECONDS
);
585 c
->output_memblockq
= pa_memblockq_new(
595 pa_source_output_put(c
->source_output
);
597 t
= pa_sample_spec_to_mime_type(&ss
, &cm
);
598 http_response(c
, 200, "OK", t
);
601 if(c
->method
== METHOD_HEAD
) {
602 connection_unlink(c
);
605 pa_ioline_set_callback(c
->line
, NULL
, NULL
);
607 if (pa_ioline_is_drained(c
->line
))
608 line_drain_callback(c
->line
, c
);
610 pa_ioline_set_drain_callback(c
->line
, line_drain_callback
, c
);
613 static void handle_url(struct connection
*c
) {
616 pa_log_debug("Request for %s", c
->url
);
618 if (pa_streq(c
->url
, URL_ROOT
))
620 else if (pa_streq(c
->url
, URL_CSS
))
622 else if (pa_streq(c
->url
, URL_STATUS
))
624 else if (pa_streq(c
->url
, URL_LISTEN
))
626 else if (pa_startswith(c
->url
, URL_LISTEN_SOURCE
))
627 handle_listen_prefix(c
, c
->url
+ sizeof(URL_LISTEN_SOURCE
)-1);
629 html_response(c
, 404, "Not Found", NULL
);
632 static void line_callback(pa_ioline
*line
, const char *s
, void *userdata
) {
633 struct connection
*c
= userdata
;
639 connection_unlink(c
);
644 case STATE_REQUEST_LINE
: {
645 if (pa_startswith(s
, "GET ")) {
646 c
->method
= METHOD_GET
;
648 } else if (pa_startswith(s
, "HEAD ")) {
649 c
->method
= METHOD_HEAD
;
655 c
->url
= pa_xstrndup(s
, strcspn(s
, " \r\n\t?"));
656 c
->state
= STATE_MIME_HEADER
;
660 case STATE_MIME_HEADER
: {
662 /* Ignore MIME headers */
663 if (strcspn(s
, " \r\n") != 0)
667 c
->state
= STATE_DATA
;
680 html_response(c
, 500, "Internal Server Error", NULL
);
683 void pa_http_protocol_connect(pa_http_protocol
*p
, pa_iochannel
*io
, pa_module
*m
) {
684 struct connection
*c
;
685 pa_client_new_data client_data
;
692 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
693 pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
694 pa_iochannel_free(io
);
698 c
= pa_xnew0(struct connection
, 1);
700 c
->state
= STATE_REQUEST_LINE
;
703 c
->line
= pa_ioline_new(io
);
704 pa_ioline_set_callback(c
->line
, line_callback
, c
);
706 pa_client_new_data_init(&client_data
);
707 client_data
.module
= c
->module
;
708 client_data
.driver
= __FILE__
;
709 pa_iochannel_socket_peer_to_string(io
, pname
, sizeof(pname
));
710 pa_proplist_setf(client_data
.proplist
, PA_PROP_APPLICATION_NAME
, "HTTP client (%s)", pname
);
711 pa_proplist_sets(client_data
.proplist
, "http-protocol.peer", pname
);
712 c
->client
= pa_client_new(p
->core
, &client_data
);
713 pa_client_new_data_done(&client_data
);
718 c
->client
->kill
= client_kill_cb
;
719 c
->client
->userdata
= c
;
721 pa_idxset_put(p
->connections
, c
, NULL
);
727 connection_unlink(c
);
730 void pa_http_protocol_disconnect(pa_http_protocol
*p
, pa_module
*m
) {
731 struct connection
*c
;
737 PA_IDXSET_FOREACH(c
, p
->connections
, idx
)
739 connection_unlink(c
);
742 static pa_http_protocol
* http_protocol_new(pa_core
*c
) {
747 p
= pa_xnew0(pa_http_protocol
, 1);
750 p
->connections
= pa_idxset_new(NULL
, NULL
);
752 pa_assert_se(pa_shared_set(c
, "http-protocol", p
) >= 0);
757 pa_http_protocol
* pa_http_protocol_get(pa_core
*c
) {
760 if ((p
= pa_shared_get(c
, "http-protocol")))
761 return pa_http_protocol_ref(p
);
763 return http_protocol_new(c
);
766 pa_http_protocol
* pa_http_protocol_ref(pa_http_protocol
*p
) {
768 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
775 void pa_http_protocol_unref(pa_http_protocol
*p
) {
776 struct connection
*c
;
779 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
781 if (PA_REFCNT_DEC(p
) > 0)
784 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
785 connection_unlink(c
);
787 pa_idxset_free(p
->connections
, NULL
, NULL
);
789 pa_strlist_free(p
->servers
);
791 pa_assert_se(pa_shared_remove(p
->core
, "http-protocol") >= 0);
796 void pa_http_protocol_add_server_string(pa_http_protocol
*p
, const char *name
) {
798 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
801 p
->servers
= pa_strlist_prepend(p
->servers
, name
);
804 void pa_http_protocol_remove_server_string(pa_http_protocol
*p
, const char *name
) {
806 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
809 p
->servers
= pa_strlist_remove(p
->servers
, name
);
812 pa_strlist
*pa_http_protocol_servers(pa_http_protocol
*p
) {
814 pa_assert(PA_REFCNT_VALUE(p
) >= 1);