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