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