2005-01-12 17:37:31 +00:00
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2005-2006 Lennart Poettering
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2005-01-12 17:37:31 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2009-03-03 20:23:02 +00:00
|
|
|
by the Free Software Foundation; either version 2.1 of the License,
|
2005-01-12 17:37:31 +00:00
|
|
|
or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2005-01-12 17:37:31 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2005-01-12 17:37:31 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2005-01-12 17:37:31 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/util.h>
|
|
|
|
|
#include <pulse/xmalloc.h>
|
2006-05-17 16:34:18 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/ioline.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/macro.h>
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/log.h>
|
|
|
|
|
#include <pulsecore/namereg.h>
|
|
|
|
|
#include <pulsecore/cli-text.h>
|
2008-08-03 16:44:38 +02:00
|
|
|
#include <pulsecore/shared.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
2005-01-12 17:37:31 +00:00
|
|
|
#include "protocol-http.h"
|
|
|
|
|
|
|
|
|
|
/* Don't allow more than this many concurrent connections */
|
|
|
|
|
#define MAX_CONNECTIONS 10
|
|
|
|
|
|
|
|
|
|
#define internal_server_error(c) http_message((c), 500, "Internal Server Error", NULL)
|
|
|
|
|
|
|
|
|
|
#define URL_ROOT "/"
|
2005-01-12 18:51:38 +00:00
|
|
|
#define URL_CSS "/style"
|
|
|
|
|
#define URL_STATUS "/status"
|
2005-01-12 17:37:31 +00:00
|
|
|
|
|
|
|
|
struct connection {
|
2008-08-03 16:44:38 +02:00
|
|
|
pa_http_protocol *protocol;
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_ioline *line;
|
2008-08-03 16:44:38 +02:00
|
|
|
enum {
|
|
|
|
|
REQUEST_LINE,
|
|
|
|
|
MIME_HEADER,
|
|
|
|
|
DATA
|
|
|
|
|
} state;
|
2005-01-12 17:37:31 +00:00
|
|
|
char *url;
|
2008-08-03 16:44:38 +02:00
|
|
|
pa_module *module;
|
2005-01-12 17:37:31 +00:00
|
|
|
};
|
|
|
|
|
|
2008-08-03 16:44:38 +02:00
|
|
|
struct pa_http_protocol {
|
|
|
|
|
PA_REFCNT_DECLARE;
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_core *core;
|
|
|
|
|
pa_idxset *connections;
|
2005-01-12 17:37:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void http_response(struct connection *c, int code, const char *msg, const char *mime) {
|
|
|
|
|
char s[256];
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(c);
|
|
|
|
|
pa_assert(msg);
|
|
|
|
|
pa_assert(mime);
|
|
|
|
|
|
|
|
|
|
pa_snprintf(s, sizeof(s),
|
2005-01-12 17:37:31 +00:00
|
|
|
"HTTP/1.0 %i %s\n"
|
|
|
|
|
"Connection: close\n"
|
|
|
|
|
"Content-Type: %s\n"
|
2005-01-12 18:51:38 +00:00
|
|
|
"Cache-Control: no-cache\n"
|
|
|
|
|
"Expires: 0\n"
|
|
|
|
|
"Server: "PACKAGE_NAME"/"PACKAGE_VERSION"\n"
|
2005-01-12 17:37:31 +00:00
|
|
|
"\n", code, msg, mime);
|
|
|
|
|
|
|
|
|
|
pa_ioline_puts(c->line, s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void http_message(struct connection *c, int code, const char *msg, const char *text) {
|
|
|
|
|
char s[256];
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(c);
|
2005-01-12 17:37:31 +00:00
|
|
|
|
|
|
|
|
http_response(c, code, msg, "text/html");
|
|
|
|
|
|
|
|
|
|
if (!text)
|
|
|
|
|
text = msg;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_snprintf(s, sizeof(s),
|
2005-01-12 18:51:38 +00:00
|
|
|
"<?xml version=\"1.0\"?>\n"
|
|
|
|
|
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
|
|
|
|
|
"<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>%s</title></head>\n"
|
2005-01-12 17:37:31 +00:00
|
|
|
"<body>%s</body></html>\n",
|
|
|
|
|
text, text);
|
|
|
|
|
|
|
|
|
|
pa_ioline_puts(c->line, s);
|
|
|
|
|
pa_ioline_defer_close(c->line);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-08-03 16:44:38 +02:00
|
|
|
static void connection_unlink(struct connection *c) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(c);
|
2005-01-12 17:37:31 +00:00
|
|
|
|
|
|
|
|
if (c->url)
|
|
|
|
|
pa_xfree(c->url);
|
|
|
|
|
|
2008-08-03 16:44:38 +02:00
|
|
|
pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2005-01-12 17:37:31 +00:00
|
|
|
pa_ioline_unref(c->line);
|
|
|
|
|
pa_xfree(c);
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void line_callback(pa_ioline *line, const char *s, void *userdata) {
|
2005-01-12 17:37:31 +00:00
|
|
|
struct connection *c = userdata;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(line);
|
|
|
|
|
pa_assert(c);
|
2005-01-12 17:37:31 +00:00
|
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
|
/* EOF */
|
2008-08-03 16:44:38 +02:00
|
|
|
connection_unlink(c);
|
2005-01-12 17:37:31 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (c->state) {
|
|
|
|
|
case REQUEST_LINE: {
|
|
|
|
|
if (memcmp(s, "GET ", 4))
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
s +=4;
|
|
|
|
|
|
2005-01-12 18:51:38 +00:00
|
|
|
c->url = pa_xstrndup(s, strcspn(s, " \r\n\t?"));
|
2005-01-12 17:37:31 +00:00
|
|
|
c->state = MIME_HEADER;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case MIME_HEADER: {
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2005-01-12 17:37:31 +00:00
|
|
|
/* Ignore MIME headers */
|
|
|
|
|
if (strcspn(s, " \r\n") != 0)
|
|
|
|
|
break;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2005-01-12 17:37:31 +00:00
|
|
|
/* We're done */
|
|
|
|
|
c->state = DATA;
|
|
|
|
|
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log_info("request for %s", c->url);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2005-01-12 17:37:31 +00:00
|
|
|
if (!strcmp(c->url, URL_ROOT)) {
|
|
|
|
|
char txt[256];
|
2009-01-28 01:46:27 +01:00
|
|
|
pa_sink *def_sink;
|
|
|
|
|
pa_source *def_source;
|
2005-01-12 17:37:31 +00:00
|
|
|
http_response(c, 200, "OK", "text/html");
|
|
|
|
|
|
|
|
|
|
pa_ioline_puts(c->line,
|
2005-01-12 18:51:38 +00:00
|
|
|
"<?xml version=\"1.0\"?>\n"
|
|
|
|
|
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
|
|
|
|
|
"<html xmlns=\"http://www.w3.org/1999/xhtml\"><title>"PACKAGE_NAME" "PACKAGE_VERSION"</title>\n"
|
|
|
|
|
"<link rel=\"stylesheet\" type=\"text/css\" href=\"style\"/></head><body>\n");
|
2005-01-12 17:37:31 +00:00
|
|
|
|
|
|
|
|
pa_ioline_puts(c->line,
|
|
|
|
|
"<h1>"PACKAGE_NAME" "PACKAGE_VERSION"</h1>\n"
|
|
|
|
|
"<table>");
|
|
|
|
|
|
|
|
|
|
#define PRINTF_FIELD(a,b) pa_ioline_printf(c->line, "<tr><td><b>%s</b></td><td>%s</td></tr>\n",(a),(b))
|
|
|
|
|
|
|
|
|
|
PRINTF_FIELD("User Name:", pa_get_user_name(txt, sizeof(txt)));
|
2008-05-15 23:34:41 +00:00
|
|
|
PRINTF_FIELD("Host name:", pa_get_host_name(txt, sizeof(txt)));
|
2005-01-12 17:37:31 +00:00
|
|
|
PRINTF_FIELD("Default Sample Specification:", pa_sample_spec_snprint(txt, sizeof(txt), &c->protocol->core->default_sample_spec));
|
2009-01-28 01:46:27 +01:00
|
|
|
|
|
|
|
|
def_sink = pa_namereg_get_default_sink(c->protocol->core);
|
|
|
|
|
def_source = pa_namereg_get_default_source(c->protocol->core);
|
|
|
|
|
|
|
|
|
|
PRINTF_FIELD("Default Sink:", def_sink ? def_sink->name : "n/a");
|
|
|
|
|
PRINTF_FIELD("Default Source:", def_source ? def_source->name : "n/a");
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2005-01-12 17:37:31 +00:00
|
|
|
pa_ioline_puts(c->line, "</table>");
|
2005-01-12 18:51:38 +00:00
|
|
|
|
|
|
|
|
pa_ioline_puts(c->line, "<p><a href=\"/status\">Click here</a> for an extensive server status report.</p>");
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2005-01-12 17:37:31 +00:00
|
|
|
pa_ioline_puts(c->line, "</body></html>\n");
|
2007-01-04 13:43:45 +00:00
|
|
|
|
|
|
|
|
pa_ioline_defer_close(c->line);
|
2005-01-12 17:37:31 +00:00
|
|
|
} else if (!strcmp(c->url, URL_CSS)) {
|
|
|
|
|
http_response(c, 200, "OK", "text/css");
|
|
|
|
|
|
2007-01-04 13:43:45 +00:00
|
|
|
pa_ioline_puts(c->line,
|
2005-01-12 17:37:31 +00:00
|
|
|
"body { color: black; background-color: white; margin: 0.5cm; }\n"
|
|
|
|
|
"a:link, a:visited { color: #900000; }\n"
|
|
|
|
|
"p { margin-left: 0.5cm; margin-right: 0.5cm; }\n"
|
|
|
|
|
"h1 { color: #00009F; }\n"
|
|
|
|
|
"h2 { color: #00009F; }\n"
|
|
|
|
|
"ul { margin-left: .5cm; }\n"
|
|
|
|
|
"ol { margin-left: .5cm; }\n"
|
|
|
|
|
"pre { margin-left: .5cm; background-color: #f0f0f0; padding: 0.4cm;}\n"
|
|
|
|
|
".grey { color: #afafaf; }\n"
|
|
|
|
|
"table { margin-left: 1cm; border:1px solid lightgrey; padding: 0.2cm; }\n"
|
|
|
|
|
"td { padding-left:10px; padding-right:10px; }\n");
|
|
|
|
|
|
2005-01-12 18:51:38 +00:00
|
|
|
pa_ioline_defer_close(c->line);
|
|
|
|
|
} else if (!strcmp(c->url, URL_STATUS)) {
|
2006-01-11 01:17:39 +00:00
|
|
|
char *r;
|
2005-01-12 18:51:38 +00:00
|
|
|
|
|
|
|
|
http_response(c, 200, "OK", "text/plain");
|
2006-01-11 01:17:39 +00:00
|
|
|
r = pa_full_status_string(c->protocol->core);
|
|
|
|
|
pa_ioline_puts(c->line, r);
|
|
|
|
|
pa_xfree(r);
|
2005-01-12 18:51:38 +00:00
|
|
|
|
|
|
|
|
pa_ioline_defer_close(c->line);
|
2005-01-12 17:37:31 +00:00
|
|
|
} else
|
|
|
|
|
http_message(c, 404, "Not Found", NULL);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2005-01-12 17:37:31 +00:00
|
|
|
default:
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2005-01-12 17:37:31 +00:00
|
|
|
fail:
|
|
|
|
|
internal_server_error(c);
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-03 16:44:38 +02:00
|
|
|
void pa_http_protocol_connect(pa_http_protocol *p, pa_iochannel *io, pa_module *m) {
|
2005-01-12 17:37:31 +00:00
|
|
|
struct connection *c;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_assert(p);
|
2008-08-03 16:44:38 +02:00
|
|
|
pa_assert(io);
|
|
|
|
|
pa_assert(m);
|
2005-01-12 17:37:31 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
|
2008-08-03 16:44:38 +02:00
|
|
|
pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
|
2005-01-12 17:37:31 +00:00
|
|
|
pa_iochannel_free(io);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
c = pa_xnew(struct connection, 1);
|
2005-01-12 17:37:31 +00:00
|
|
|
c->protocol = p;
|
|
|
|
|
c->line = pa_ioline_new(io);
|
|
|
|
|
c->state = REQUEST_LINE;
|
|
|
|
|
c->url = NULL;
|
2008-08-03 16:44:38 +02:00
|
|
|
c->module = m;
|
2005-01-12 17:37:31 +00:00
|
|
|
|
|
|
|
|
pa_ioline_set_callback(c->line, line_callback, c);
|
2008-08-03 16:44:38 +02:00
|
|
|
|
2005-01-12 17:37:31 +00:00
|
|
|
pa_idxset_put(p->connections, c, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-03 16:44:38 +02:00
|
|
|
void pa_http_protocol_disconnect(pa_http_protocol *p, pa_module *m) {
|
|
|
|
|
struct connection *c;
|
|
|
|
|
void *state = NULL;
|
|
|
|
|
|
|
|
|
|
pa_assert(p);
|
|
|
|
|
pa_assert(m);
|
|
|
|
|
|
|
|
|
|
while ((c = pa_idxset_iterate(p->connections, &state, NULL)))
|
|
|
|
|
if (c->module == m)
|
|
|
|
|
connection_unlink(c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static pa_http_protocol* http_protocol_new(pa_core *c) {
|
|
|
|
|
pa_http_protocol *p;
|
2005-01-12 17:37:31 +00:00
|
|
|
|
2008-08-03 16:44:38 +02:00
|
|
|
pa_assert(c);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2008-08-03 16:44:38 +02:00
|
|
|
p = pa_xnew(pa_http_protocol, 1);
|
|
|
|
|
PA_REFCNT_INIT(p);
|
|
|
|
|
p->core = c;
|
2005-01-12 17:37:31 +00:00
|
|
|
p->connections = pa_idxset_new(NULL, NULL);
|
|
|
|
|
|
2008-08-03 16:44:38 +02:00
|
|
|
pa_assert_se(pa_shared_set(c, "http-protocol", p) >= 0);
|
2005-01-12 17:37:31 +00:00
|
|
|
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-03 16:44:38 +02:00
|
|
|
pa_http_protocol* pa_http_protocol_get(pa_core *c) {
|
|
|
|
|
pa_http_protocol *p;
|
|
|
|
|
|
|
|
|
|
if ((p = pa_shared_get(c, "http-protocol")))
|
|
|
|
|
return pa_http_protocol_ref(p);
|
|
|
|
|
|
|
|
|
|
return http_protocol_new(c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_http_protocol* pa_http_protocol_ref(pa_http_protocol *p) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(p);
|
2008-08-03 16:44:38 +02:00
|
|
|
pa_assert(PA_REFCNT_VALUE(p) >= 1);
|
|
|
|
|
|
|
|
|
|
PA_REFCNT_INC(p);
|
|
|
|
|
|
|
|
|
|
return p;
|
2005-01-12 17:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
2008-08-03 16:44:38 +02:00
|
|
|
void pa_http_protocol_unref(pa_http_protocol *p) {
|
|
|
|
|
struct connection *c;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(p);
|
2008-08-03 16:44:38 +02:00
|
|
|
pa_assert(PA_REFCNT_VALUE(p) >= 1);
|
|
|
|
|
|
|
|
|
|
if (PA_REFCNT_DEC(p) > 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
while ((c = pa_idxset_first(p->connections, NULL)))
|
|
|
|
|
connection_unlink(c);
|
|
|
|
|
|
|
|
|
|
pa_idxset_free(p->connections, NULL, NULL);
|
|
|
|
|
|
|
|
|
|
pa_assert_se(pa_shared_remove(p->core, "http-protocol") >= 0);
|
2005-01-12 17:37:31 +00:00
|
|
|
|
|
|
|
|
pa_xfree(p);
|
|
|
|
|
}
|