]>
code.delx.au - pulseaudio/blob - src/polypcore/protocol-http.c
4 This file is part of polypaudio.
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.
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.
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
31 #include "protocol-http.h"
38 /* Don't allow more than this many concurrent connections */
39 #define MAX_CONNECTIONS 10
41 #define internal_server_error(c) http_message((c), 500, "Internal Server Error", NULL)
44 #define URL_CSS "/style"
45 #define URL_STATUS "/status"
48 pa_protocol_http
*protocol
;
50 enum { REQUEST_LINE
, MIME_HEADER
, DATA
} state
;
54 struct pa_protocol_http
{
57 pa_socket_server
*server
;
58 pa_idxset
*connections
;
61 static void http_response(struct connection
*c
, int code
, const char *msg
, const char *mime
) {
67 snprintf(s
, sizeof(s
),
71 "Cache-Control: no-cache\n"
73 "Server: "PACKAGE_NAME
"/"PACKAGE_VERSION
"\n"
74 "\n", code
, msg
, mime
);
76 pa_ioline_puts(c
->line
, s
);
79 static void http_message(struct connection
*c
, int code
, const char *msg
, const char *text
) {
83 http_response(c
, code
, msg
, "text/html");
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",
95 pa_ioline_puts(c
->line
, s
);
96 pa_ioline_defer_close(c
->line
);
100 static void connection_free(struct connection
*c
, int del
) {
107 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
108 pa_ioline_unref(c
->line
);
112 static void line_callback(pa_ioline
*line
, const char *s
, void *userdata
) {
113 struct connection
*c
= userdata
;
119 connection_free(c
, 1);
125 if (memcmp(s
, "GET ", 4))
130 c
->url
= pa_xstrndup(s
, strcspn(s
, " \r\n\t?"));
131 c
->state
= MIME_HEADER
;
138 /* Ignore MIME headers */
139 if (strcspn(s
, " \r\n") != 0)
145 pa_log_info(__FILE__
": request for %s\n", c
->url
);
147 if (!strcmp(c
->url
, URL_ROOT
)) {
149 http_response(c
, 200, "OK", "text/html");
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");
157 pa_ioline_puts(c
->line
,
158 "<h1>"PACKAGE_NAME
" "PACKAGE_VERSION
"</h1>\n"
161 #define PRINTF_FIELD(a,b) pa_ioline_printf(c->line, "<tr><td><b>%s</b></td><td>%s</td></tr>\n",(a),(b))
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
));
169 pa_ioline_puts(c
->line
, "</table>");
171 pa_ioline_puts(c
->line
, "<p><a href=\"/status\">Click here</a> for an extensive server status report.</p>");
173 pa_ioline_puts(c
->line
, "</body></html>\n");
175 pa_ioline_defer_close(c
->line
);
176 } else if (!strcmp(c
->url
, URL_CSS
)) {
177 http_response(c
, 200, "OK", "text/css");
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");
192 pa_ioline_defer_close(c
->line
);
193 } else if (!strcmp(c
->url
, URL_STATUS
)) {
196 http_response(c
, 200, "OK", "text/plain");
197 r
= pa_full_status_string(c
->protocol
->core
);
198 pa_ioline_puts(c
->line
, r
);
201 pa_ioline_defer_close(c
->line
);
203 http_message(c
, 404, "Not Found", NULL
);
215 internal_server_error(c
);
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
);
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
);
229 c
= pa_xmalloc(sizeof(struct connection
));
231 c
->line
= pa_ioline_new(io
);
232 c
->state
= REQUEST_LINE
;
235 pa_ioline_set_callback(c
->line
, line_callback
, c
);
236 pa_idxset_put(p
->connections
, c
, NULL
);
239 pa_protocol_http
* pa_protocol_http_new(pa_core
*core
, pa_socket_server
*server
, pa_module
*m
, PA_GCC_UNUSED pa_modargs
*ma
) {
241 assert(core
&& server
);
243 p
= pa_xmalloc(sizeof(pa_protocol_http
));
247 p
->connections
= pa_idxset_new(NULL
, NULL
);
249 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
254 static void free_connection(void *p
, PA_GCC_UNUSED
void *userdata
) {
256 connection_free(p
, 0);
259 void pa_protocol_http_free(pa_protocol_http
*p
) {
262 pa_idxset_free(p
->connections
, free_connection
, NULL
);
263 pa_socket_server_unref(p
->server
);