]> code.delx.au - pulseaudio/blob - src/pulsecore/protocol-http.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / pulsecore / protocol-http.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2005-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include <pulse/util.h>
33 #include <pulse/xmalloc.h>
34
35 #include <pulsecore/ioline.h>
36 #include <pulsecore/macro.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/namereg.h>
39 #include <pulsecore/cli-text.h>
40
41 #include "protocol-http.h"
42
43 /* Don't allow more than this many concurrent connections */
44 #define MAX_CONNECTIONS 10
45
46 #define internal_server_error(c) http_message((c), 500, "Internal Server Error", NULL)
47
48 #define URL_ROOT "/"
49 #define URL_CSS "/style"
50 #define URL_STATUS "/status"
51
52 struct connection {
53 pa_protocol_http *protocol;
54 pa_ioline *line;
55 enum { REQUEST_LINE, MIME_HEADER, DATA } state;
56 char *url;
57 };
58
59 struct pa_protocol_http {
60 pa_module *module;
61 pa_core *core;
62 pa_socket_server*server;
63 pa_idxset *connections;
64 };
65
66 static void http_response(struct connection *c, int code, const char *msg, const char *mime) {
67 char s[256];
68
69 pa_assert(c);
70 pa_assert(msg);
71 pa_assert(mime);
72
73 pa_snprintf(s, sizeof(s),
74 "HTTP/1.0 %i %s\n"
75 "Connection: close\n"
76 "Content-Type: %s\n"
77 "Cache-Control: no-cache\n"
78 "Expires: 0\n"
79 "Server: "PACKAGE_NAME"/"PACKAGE_VERSION"\n"
80 "\n", code, msg, mime);
81
82 pa_ioline_puts(c->line, s);
83 }
84
85 static void http_message(struct connection *c, int code, const char *msg, const char *text) {
86 char s[256];
87 pa_assert(c);
88
89 http_response(c, code, msg, "text/html");
90
91 if (!text)
92 text = msg;
93
94 pa_snprintf(s, sizeof(s),
95 "<?xml version=\"1.0\"?>\n"
96 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
97 "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>%s</title></head>\n"
98 "<body>%s</body></html>\n",
99 text, text);
100
101 pa_ioline_puts(c->line, s);
102 pa_ioline_defer_close(c->line);
103 }
104
105
106 static void connection_free(struct connection *c, int del) {
107 pa_assert(c);
108
109 if (c->url)
110 pa_xfree(c->url);
111
112 if (del)
113 pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
114
115 pa_ioline_unref(c->line);
116 pa_xfree(c);
117 }
118
119 static void line_callback(pa_ioline *line, const char *s, void *userdata) {
120 struct connection *c = userdata;
121 pa_assert(line);
122 pa_assert(c);
123
124 if (!s) {
125 /* EOF */
126 connection_free(c, 1);
127 return;
128 }
129
130 switch (c->state) {
131 case REQUEST_LINE: {
132 if (memcmp(s, "GET ", 4))
133 goto fail;
134
135 s +=4;
136
137 c->url = pa_xstrndup(s, strcspn(s, " \r\n\t?"));
138 c->state = MIME_HEADER;
139 break;
140
141 }
142
143 case MIME_HEADER: {
144
145 /* Ignore MIME headers */
146 if (strcspn(s, " \r\n") != 0)
147 break;
148
149 /* We're done */
150 c->state = DATA;
151
152 pa_log_info("request for %s", c->url);
153
154 if (!strcmp(c->url, URL_ROOT)) {
155 char txt[256];
156 http_response(c, 200, "OK", "text/html");
157
158 pa_ioline_puts(c->line,
159 "<?xml version=\"1.0\"?>\n"
160 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
161 "<html xmlns=\"http://www.w3.org/1999/xhtml\"><title>"PACKAGE_NAME" "PACKAGE_VERSION"</title>\n"
162 "<link rel=\"stylesheet\" type=\"text/css\" href=\"style\"/></head><body>\n");
163
164 pa_ioline_puts(c->line,
165 "<h1>"PACKAGE_NAME" "PACKAGE_VERSION"</h1>\n"
166 "<table>");
167
168 #define PRINTF_FIELD(a,b) pa_ioline_printf(c->line, "<tr><td><b>%s</b></td><td>%s</td></tr>\n",(a),(b))
169
170 PRINTF_FIELD("User Name:", pa_get_user_name(txt, sizeof(txt)));
171 PRINTF_FIELD("Fully Qualified Domain Name:", pa_get_fqdn(txt, sizeof(txt)));
172 PRINTF_FIELD("Default Sample Specification:", pa_sample_spec_snprint(txt, sizeof(txt), &c->protocol->core->default_sample_spec));
173 PRINTF_FIELD("Default Sink:", pa_namereg_get_default_sink_name(c->protocol->core));
174 PRINTF_FIELD("Default Source:", pa_namereg_get_default_source_name(c->protocol->core));
175
176 pa_ioline_puts(c->line, "</table>");
177
178 pa_ioline_puts(c->line, "<p><a href=\"/status\">Click here</a> for an extensive server status report.</p>");
179
180 pa_ioline_puts(c->line, "</body></html>\n");
181
182 pa_ioline_defer_close(c->line);
183 } else if (!strcmp(c->url, URL_CSS)) {
184 http_response(c, 200, "OK", "text/css");
185
186 pa_ioline_puts(c->line,
187 "body { color: black; background-color: white; margin: 0.5cm; }\n"
188 "a:link, a:visited { color: #900000; }\n"
189 "p { margin-left: 0.5cm; margin-right: 0.5cm; }\n"
190 "h1 { color: #00009F; }\n"
191 "h2 { color: #00009F; }\n"
192 "ul { margin-left: .5cm; }\n"
193 "ol { margin-left: .5cm; }\n"
194 "pre { margin-left: .5cm; background-color: #f0f0f0; padding: 0.4cm;}\n"
195 ".grey { color: #afafaf; }\n"
196 "table { margin-left: 1cm; border:1px solid lightgrey; padding: 0.2cm; }\n"
197 "td { padding-left:10px; padding-right:10px; }\n");
198
199 pa_ioline_defer_close(c->line);
200 } else if (!strcmp(c->url, URL_STATUS)) {
201 char *r;
202
203 http_response(c, 200, "OK", "text/plain");
204 r = pa_full_status_string(c->protocol->core);
205 pa_ioline_puts(c->line, r);
206 pa_xfree(r);
207
208 pa_ioline_defer_close(c->line);
209 } else
210 http_message(c, 404, "Not Found", NULL);
211
212 break;
213 }
214
215 default:
216 ;
217 }
218
219 return;
220
221 fail:
222 internal_server_error(c);
223 }
224
225 static void on_connection(pa_socket_server*s, pa_iochannel *io, void *userdata) {
226 pa_protocol_http *p = userdata;
227 struct connection *c;
228
229 pa_assert(s);
230 pa_assert(io);
231 pa_assert(p);
232
233 if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
234 pa_log_warn("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
235 pa_iochannel_free(io);
236 return;
237 }
238
239 c = pa_xnew(struct connection, 1);
240 c->protocol = p;
241 c->line = pa_ioline_new(io);
242 c->state = REQUEST_LINE;
243 c->url = NULL;
244
245 pa_ioline_set_callback(c->line, line_callback, c);
246 pa_idxset_put(p->connections, c, NULL);
247 }
248
249 pa_protocol_http* pa_protocol_http_new(pa_core *core, pa_socket_server *server, pa_module *m, PA_GCC_UNUSED pa_modargs *ma) {
250 pa_protocol_http* p;
251
252 pa_core_assert_ref(core);
253 pa_assert(server);
254
255 p = pa_xnew(pa_protocol_http, 1);
256 p->module = m;
257 p->core = core;
258 p->server = server;
259 p->connections = pa_idxset_new(NULL, NULL);
260
261 pa_socket_server_set_callback(p->server, on_connection, p);
262
263 return p;
264 }
265
266 static void free_connection(void *p, PA_GCC_UNUSED void *userdata) {
267 pa_assert(p);
268 connection_free(p, 0);
269 }
270
271 void pa_protocol_http_free(pa_protocol_http *p) {
272 pa_assert(p);
273
274 pa_idxset_free(p->connections, free_connection, NULL);
275 pa_socket_server_unref(p->server);
276 pa_xfree(p);
277 }