From 4590f09d0b44aeb7cef3eed72b419444ea36d8e0 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 12 Jan 2005 17:37:31 +0000 Subject: * make pa_sample_spec_snprint return point to written string * first try of a http module git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@339 fefdeb5f-60dc-0310-8127-8f9354f1896f --- polyp/Makefile.am | 30 +++++- polyp/caps.c | 3 - polyp/ioline.c | 33 +++++- polyp/ioline.h | 7 ++ polyp/module-protocol-stub.c | 17 +++ polyp/protocol-http.c | 244 +++++++++++++++++++++++++++++++++++++++++++ polyp/protocol-http.h | 35 +++++++ polyp/sample.c | 12 +-- polyp/sample.h | 2 +- polyp/socket-client.c | 9 +- 10 files changed, 374 insertions(+), 18 deletions(-) create mode 100644 polyp/protocol-http.c create mode 100644 polyp/protocol-http.h (limited to 'polyp') diff --git a/polyp/Makefile.am b/polyp/Makefile.am index 72731c1d..009707c2 100644 --- a/polyp/Makefile.am +++ b/polyp/Makefile.am @@ -103,6 +103,7 @@ modlib_LTLIBRARIES= \ libprotocol-simple.la \ libprotocol-esound.la \ libprotocol-native.la \ + libprotocol-http.la \ module-cli.la \ module-cli-protocol-tcp.la \ module-cli-protocol-tcp6.la \ @@ -129,7 +130,10 @@ modlib_LTLIBRARIES= \ module-tunnel-sink.la \ module-tunnel-source.la \ module-null-sink.la \ - module-esound-sink.la + module-esound-sink.la \ + module-http-protocol-tcp.la \ + module-http-protocol-tcp6.la \ + module-http-protocol-unix.la SYMDEF_FILES= \ module-cli-symdef.h \ @@ -161,7 +165,10 @@ SYMDEF_FILES= \ module-esound-sink-symdef.h \ module-zeroconf-publish-symdef.h \ module-lirc-symdef.h \ - module-mmkbd-evdev-symdef.h + module-mmkbd-evdev-symdef.h \ + module-http-protocol-tcp-symdef.h \ + module-http-protocol-tcp6-symdef.h \ + module-http-protocol-unix-symdef.h EXTRA_DIST+=$(SYMDEF_FILES) BUILT_SOURCES+=$(SYMDEF_FILES) @@ -288,6 +295,10 @@ libprotocol_cli_la_SOURCES = protocol-cli.c protocol-cli.h libprotocol_cli_la_LDFLAGS = -avoid-version libprotocol_cli_la_LIBADD = $(AM_LIBADD) libsocket-server.la libiochannel.la libcli.la +libprotocol_http_la_SOURCES = protocol-http.c protocol-http.h +libprotocol_http_la_LDFLAGS = -avoid-version +libprotocol_http_la_LIBADD = $(AM_LIBADD) libsocket-server.la libioline.la + libprotocol_native_la_SOURCES = protocol-native.c protocol-native.h native-common.h libprotocol_native_la_LDFLAGS = -avoid-version libprotocol_native_la_LIBADD = $(AM_LIBADD) libsocket-server.la libpstream.la libpstream-util.la libpdispatch.la libtagstruct.la libauthkey.la libauthkey-prop.la libstrlist.la @@ -338,6 +349,21 @@ module_cli_protocol_unix_la_CFLAGS = -DUSE_UNIX_SOCKETS -DUSE_PROTOCOL_CLI $(AM_ module_cli_protocol_unix_la_LDFLAGS = -module -avoid-version module_cli_protocol_unix_la_LIBADD = $(AM_LIBADD) libprotocol-cli.la libsocket-server.la libsocket-util.la +module_http_protocol_tcp_la_SOURCES = module-protocol-stub.c +module_http_protocol_tcp_la_CFLAGS = -DUSE_TCP_SOCKETS -DUSE_PROTOCOL_HTTP $(AM_CFLAGS) +module_http_protocol_tcp_la_LDFLAGS = -module -avoid-version +module_http_protocol_tcp_la_LIBADD = $(AM_LIBADD) libprotocol-http.la libsocket-server.la + +module_http_protocol_tcp6_la_SOURCES = module-protocol-stub.c +module_http_protocol_tcp6_la_CFLAGS = -DUSE_TCP6_SOCKETS -DUSE_PROTOCOL_HTTP $(AM_CFLAGS) +module_http_protocol_tcp6_la_LDFLAGS = -module -avoid-version +module_http_protocol_tcp6_la_LIBADD = $(AM_LIBADD) libprotocol-http.la libsocket-server.la + +module_http_protocol_unix_la_SOURCES = module-protocol-stub.c +module_http_protocol_unix_la_CFLAGS = -DUSE_UNIX_SOCKETS -DUSE_PROTOCOL_HTTP $(AM_CFLAGS) +module_http_protocol_unix_la_LDFLAGS = -module -avoid-version +module_http_protocol_unix_la_LIBADD = $(AM_LIBADD) libprotocol-http.la libsocket-server.la libsocket-util.la + module_native_protocol_tcp_la_SOURCES = module-protocol-stub.c module_native_protocol_tcp_la_CFLAGS = -DUSE_TCP_SOCKETS -DUSE_PROTOCOL_NATIVE $(AM_CFLAGS) module_native_protocol_tcp_la_LDFLAGS = -module -avoid-version diff --git a/polyp/caps.c b/polyp/caps.c index 01ed1519..739e7071 100644 --- a/polyp/caps.c +++ b/polyp/caps.c @@ -23,9 +23,6 @@ #include #endif -/* setresuid() is only available on GNU */ -#define _GNU_SOURCE - #include #include #include diff --git a/polyp/ioline.c b/polyp/ioline.c index f52af2db..6f7886da 100644 --- a/polyp/ioline.c +++ b/polyp/ioline.c @@ -51,6 +51,8 @@ struct pa_ioline { void (*callback)(struct pa_ioline*io, const char *s, void *userdata); void *userdata; + + int defer_close; }; static void io_callback(struct pa_iochannel*io, void *userdata); @@ -78,6 +80,8 @@ struct pa_ioline* pa_ioline_new(struct pa_iochannel *io) { l->defer_event = l->mainloop->defer_new(l->mainloop, defer_callback, l); l->mainloop->defer_enable(l->defer_event, 0); + + l->defer_close = 0; pa_iochannel_set_callback(io, io_callback, l); @@ -181,8 +185,10 @@ static void failure(struct pa_ioline *l) { pa_ioline_close(l); - if (l->callback) + if (l->callback) { l->callback(l, NULL, l->userdata); + l->callback = NULL; + } } static void scan_for_lines(struct pa_ioline *l, size_t skip) { @@ -309,6 +315,9 @@ static void do_work(struct pa_ioline *l) { if (!l->dead) do_read(l); + if (l->defer_close && !l->wbuf_valid_length) + failure(l); + pa_ioline_unref(l); } @@ -325,3 +334,25 @@ static void defer_callback(struct pa_mainloop_api*m, struct pa_defer_event*e, vo do_work(l); } + +void pa_ioline_defer_close(struct pa_ioline *l) { + assert(l); + + l->defer_close = 1; + + if (!l->wbuf_valid_length) + l->mainloop->defer_enable(l->defer_event, 1); +} + +void pa_ioline_printf(struct pa_ioline *s, const char *format, ...) { + char *t; + va_list ap; + + + va_start(ap, format); + t = pa_vsprintf_malloc(format, ap); + va_end(ap); + + pa_ioline_puts(s, t); + pa_xfree(t); +} diff --git a/polyp/ioline.h b/polyp/ioline.h index f652dddb..6e9c76d0 100644 --- a/polyp/ioline.h +++ b/polyp/ioline.h @@ -23,6 +23,7 @@ ***/ #include "iochannel.h" +#include "util.h" /* An ioline wraps an iochannel for line based communication. A * callback function is called whenever a new line has been recieved @@ -38,7 +39,13 @@ void pa_ioline_close(struct pa_ioline *l); /* Write a string to the channel */ void pa_ioline_puts(struct pa_ioline *s, const char *c); +/* Write a string to the channel */ +void pa_ioline_printf(struct pa_ioline *s, const char *format, ...) PA_GCC_PRINTF_ATTR(2,3); + /* Set the callback function that is called for every recieved line */ void pa_ioline_set_callback(struct pa_ioline*io, void (*callback)(struct pa_ioline*io, const char *s, void *userdata), void *userdata); +/* Make sure to close the ioline object as soon as the send buffer is emptied */ +void pa_ioline_defer_close(struct pa_ioline *io); + #endif diff --git a/polyp/module-protocol-stub.c b/polyp/module-protocol-stub.c index 93e576af..be27b8e2 100644 --- a/polyp/module-protocol-stub.c +++ b/polyp/module-protocol-stub.c @@ -85,6 +85,23 @@ #endif PA_MODULE_DESCRIPTION("Command line interface protocol "SOCKET_DESCRIPTION) PA_MODULE_USAGE(SOCKET_USAGE) +#elif defined(USE_PROTOCOL_HTTP) + #include "protocol-http.h" + #define protocol_new pa_protocol_http_new + #define protocol_free pa_protocol_http_free + #define TCPWRAP_SERVICE "polypaudio-http" + #define IPV4_PORT 4714 + #define UNIX_SOCKET "http" + #define MODULE_ARGUMENTS + #ifdef USE_TCP_SOCKETS + #include "module-http-protocol-tcp-symdef.h" + #elif defined(USE_TCP6_SOCKETS) + #include "module-http-protocol-tcp6-symdef.h" + #else + #include "module-http-protocol-unix-symdef.h" + #endif + PA_MODULE_DESCRIPTION("HTTP "SOCKET_DESCRIPTION) + PA_MODULE_USAGE(SOCKET_USAGE) #elif defined(USE_PROTOCOL_NATIVE) #include "protocol-native.h" #define protocol_new pa_protocol_native_new diff --git a/polyp/protocol-http.c b/polyp/protocol-http.c new file mode 100644 index 00000000..768b7588 --- /dev/null +++ b/polyp/protocol-http.c @@ -0,0 +1,244 @@ +/* $Id$ */ + +/*** + This file is part of polypaudio. + + polypaudio is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + polypaudio is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with polypaudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include + +#include "protocol-http.h" +#include "ioline.h" +#include "xmalloc.h" +#include "log.h" +#include "namereg.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 "/" +#define URL_CSS "/style.css" + +struct connection { + struct pa_protocol_http *protocol; + struct pa_ioline *line; + enum { REQUEST_LINE, MIME_HEADER, DATA } state; + char *url; +}; + +struct pa_protocol_http { + struct pa_module *module; + struct pa_core *core; + struct pa_socket_server*server; + struct pa_idxset *connections; +}; + +static void http_response(struct connection *c, int code, const char *msg, const char *mime) { + char s[256]; + assert(c); + assert(msg); + assert(mime); + + snprintf(s, sizeof(s), + "HTTP/1.0 %i %s\n" + "Connection: close\n" + "Content-Type: %s\n" + "\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]; + assert(c); + + http_response(c, code, msg, "text/html"); + + if (!text) + text = msg; + + snprintf(s, sizeof(s), + "%s\n" + "%s\n", + text, text); + + pa_ioline_puts(c->line, s); + pa_ioline_defer_close(c->line); +} + + +static void connection_free(struct connection *c, int del) { + assert(c); + + if (c->url) + pa_xfree(c->url); + + if (del) + pa_idxset_remove_by_data(c->protocol->connections, c, NULL); + pa_ioline_unref(c->line); + pa_xfree(c); +} + +static void line_callback(struct pa_ioline *line, const char *s, void *userdata) { + struct connection *c = userdata; + assert(line); + assert(c); + + if (!s) { + /* EOF */ + connection_free(c, 1); + return; + } + + switch (c->state) { + case REQUEST_LINE: { + if (memcmp(s, "GET ", 4)) + goto fail; + + s +=4; + + c->url = pa_xstrndup(s, strcspn(s, " \r\n\t")); + c->state = MIME_HEADER; + break; + + } + + case MIME_HEADER: { + + /* Ignore MIME headers */ + if (strcspn(s, " \r\n") != 0) + break; + + /* We're done */ + c->state = DATA; + + pa_log("req for %s\n", c->url); + + if (!strcmp(c->url, URL_ROOT)) { + char txt[256]; + http_response(c, 200, "OK", "text/html"); + + pa_ioline_puts(c->line, + ""PACKAGE_NAME" "PACKAGE_VERSION"\n" + "\n"); + + pa_ioline_puts(c->line, + "

"PACKAGE_NAME" "PACKAGE_VERSION"

\n" + "

Server Information

\n" + ""); + +#define PRINTF_FIELD(a,b) pa_ioline_printf(c->line, "\n",(a),(b)) + + PRINTF_FIELD("User Name:", pa_get_user_name(txt, sizeof(txt))); + PRINTF_FIELD("Fully Qualified Domain Name:", pa_get_fqdn(txt, sizeof(txt))); + PRINTF_FIELD("Default Sample Specification:", pa_sample_spec_snprint(txt, sizeof(txt), &c->protocol->core->default_sample_spec)); + PRINTF_FIELD("Default Sink:", pa_namereg_get_default_sink_name(c->protocol->core)); + PRINTF_FIELD("Default Source:", pa_namereg_get_default_source_name(c->protocol->core)); + pa_ioline_puts(c->line, "
%s%s
"); + pa_ioline_puts(c->line, "\n"); + + pa_ioline_defer_close(c->line); + } else if (!strcmp(c->url, URL_CSS)) { + http_response(c, 200, "OK", "text/css"); + + pa_ioline_puts(c->line, + "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"); + + pa_ioline_defer_close(c->line); + } else + http_message(c, 404, "Not Found", NULL); + + break; + } + + default: + ; + } + + return; + +fail: + internal_server_error(c); +} + +static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) { + struct pa_protocol_http *p = userdata; + struct connection *c; + assert(s && io && p); + + if (pa_idxset_ncontents(p->connections)+1 > MAX_CONNECTIONS) { + pa_log(__FILE__": Warning! Too many connections (%u), dropping incoming connection.\n", MAX_CONNECTIONS); + pa_iochannel_free(io); + return; + } + + c = pa_xmalloc(sizeof(struct connection)); + c->protocol = p; + c->line = pa_ioline_new(io); + c->state = REQUEST_LINE; + c->url = NULL; + + pa_ioline_set_callback(c->line, line_callback, c); + pa_idxset_put(p->connections, c, NULL); +} + +struct pa_protocol_http* pa_protocol_http_new(struct pa_core *core, struct pa_socket_server *server, struct pa_module *m, struct pa_modargs *ma) { + struct pa_protocol_http* p; + assert(core && server); + + p = pa_xmalloc(sizeof(struct pa_protocol_http)); + p->module = m; + p->core = core; + p->server = server; + p->connections = pa_idxset_new(NULL, NULL); + + pa_socket_server_set_callback(p->server, on_connection, p); + + return p; +} + +static void free_connection(void *p, void *userdata) { + assert(p); + connection_free(p, 0); +} + +void pa_protocol_http_free(struct pa_protocol_http *p) { + assert(p); + + pa_idxset_free(p->connections, free_connection, NULL); + pa_socket_server_unref(p->server); + pa_xfree(p); +} diff --git a/polyp/protocol-http.h b/polyp/protocol-http.h new file mode 100644 index 00000000..3c9b8d76 --- /dev/null +++ b/polyp/protocol-http.h @@ -0,0 +1,35 @@ +#ifndef fooprotocolhttphfoo +#define fooprotocolhttphfoo + +/* $Id$ */ + +/*** + This file is part of polypaudio. + + polypaudio is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + polypaudio is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with polypaudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#include "core.h" +#include "socket-server.h" +#include "module.h" +#include "modargs.h" + +struct pa_protocol_http; + +struct pa_protocol_http* pa_protocol_http_new(struct pa_core *core, struct pa_socket_server *server, struct pa_module *m, struct pa_modargs *ma); +void pa_protocol_http_free(struct pa_protocol_http *n); + +#endif diff --git a/polyp/sample.c b/polyp/sample.c index 51afaa01..f9d0c458 100644 --- a/polyp/sample.c +++ b/polyp/sample.c @@ -101,15 +101,15 @@ const char *pa_sample_format_to_string(enum pa_sample_format f) { return table[f]; } -void pa_sample_spec_snprint(char *s, size_t l, const struct pa_sample_spec *spec) { +char *pa_sample_spec_snprint(char *s, size_t l, const struct pa_sample_spec *spec) { assert(s && l && spec); - if (!pa_sample_spec_valid(spec)) { + if (!pa_sample_spec_valid(spec)) snprintf(s, l, "Invalid"); - return; - } - - snprintf(s, l, "%s %uch %uHz", pa_sample_format_to_string(spec->format), spec->channels, spec->rate); + else + snprintf(s, l, "%s %uch %uHz", pa_sample_format_to_string(spec->format), spec->channels, spec->rate); + + return s; } pa_volume_t pa_volume_multiply(pa_volume_t a, pa_volume_t b) { diff --git a/polyp/sample.h b/polyp/sample.h index d4873eb5..0494c7de 100644 --- a/polyp/sample.h +++ b/polyp/sample.h @@ -93,7 +93,7 @@ const char *pa_sample_format_to_string(enum pa_sample_format f); #define PA_SAMPLE_SPEC_SNPRINT_MAX 32 /** Pretty print a sample type specification to a string */ -void pa_sample_spec_snprint(char *s, size_t l, const struct pa_sample_spec *spec); +char* pa_sample_spec_snprint(char *s, size_t l, const struct pa_sample_spec *spec); /** Volume specification: 0: silence; < 256: diminished volume; 256: normal volume; > 256 amplified volume */ typedef uint32_t pa_volume_t; diff --git a/polyp/socket-client.c b/polyp/socket-client.c index 01f66371..1bcf82e3 100644 --- a/polyp/socket-client.c +++ b/polyp/socket-client.c @@ -395,8 +395,8 @@ struct pa_socket_client* pa_socket_client_new_string(struct pa_mainloop_api *m, switch (a.type) { case PA_PARSED_ADDRESS_UNIX: - c = pa_socket_client_new_unix(m, a.path_or_host); - start_timeout(c); + if ((c = pa_socket_client_new_unix(m, a.path_or_host))) + start_timeout(c); break; case PA_PARSED_ADDRESS_TCP4: /* Fallthrough */ @@ -437,9 +437,8 @@ struct pa_socket_client* pa_socket_client_new_string(struct pa_mainloop_api *m, goto finish; if (res->ai_addr) { - c = pa_socket_client_new_sockaddr(m, res->ai_addr, res->ai_addrlen); - start_timeout(c); - } + if ((c = pa_socket_client_new_sockaddr(m, res->ai_addr, res->ai_addrlen))) + tart_timeout(c); freeaddrinfo(res); } -- cgit