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