37
35
#include <pulsecore/log.h>
38
36
#include <pulsecore/namereg.h>
39
37
#include <pulsecore/cli-text.h>
38
#include <pulsecore/shared.h>
41
40
#include "protocol-http.h"
50
49
#define URL_STATUS "/status"
52
51
struct connection {
53
pa_protocol_http *protocol;
52
pa_http_protocol *protocol;
55
enum { REQUEST_LINE, MIME_HEADER, DATA } state;
59
struct pa_protocol_http {
63
struct pa_http_protocol {
62
pa_socket_server*server;
63
67
pa_idxset *connections;
106
static void connection_free(struct connection *c, int del) {
110
static void connection_unlink(struct connection *c) {
110
114
pa_xfree(c->url);
113
pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
116
pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
115
118
pa_ioline_unref(c->line);
168
171
#define PRINTF_FIELD(a,b) pa_ioline_printf(c->line, "<tr><td><b>%s</b></td><td>%s</td></tr>\n",(a),(b))
170
173
PRINTF_FIELD("User Name:", pa_get_user_name(txt, sizeof(txt)));
171
PRINTF_FIELD("Fully Qualified Domain Name:", pa_get_fqdn(txt, sizeof(txt)));
174
PRINTF_FIELD("Host name:", pa_get_host_name(txt, sizeof(txt)));
172
175
PRINTF_FIELD("Default Sample Specification:", pa_sample_spec_snprint(txt, sizeof(txt), &c->protocol->core->default_sample_spec));
173
176
PRINTF_FIELD("Default Sink:", pa_namereg_get_default_sink_name(c->protocol->core));
174
177
PRINTF_FIELD("Default Source:", pa_namereg_get_default_source_name(c->protocol->core));
222
225
internal_server_error(c);
225
static void on_connection(pa_socket_server*s, pa_iochannel *io, void *userdata) {
226
pa_protocol_http *p = userdata;
228
void pa_http_protocol_connect(pa_http_protocol *p, pa_iochannel *io, pa_module *m) {
227
229
struct connection *c;
233
235
if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
234
pa_log_warn("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
236
pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
235
237
pa_iochannel_free(io);
241
243
c->line = pa_ioline_new(io);
242
244
c->state = REQUEST_LINE;
245
248
pa_ioline_set_callback(c->line, line_callback, c);
246
250
pa_idxset_put(p->connections, c, NULL);
249
pa_protocol_http* pa_protocol_http_new(pa_core *core, pa_socket_server *server, pa_module *m, PA_GCC_UNUSED pa_modargs *ma) {
252
pa_core_assert_ref(core);
255
p = pa_xnew(pa_protocol_http, 1);
253
void pa_http_protocol_disconnect(pa_http_protocol *p, pa_module *m) {
254
struct connection *c;
260
while ((c = pa_idxset_iterate(p->connections, &state, NULL)))
262
connection_unlink(c);
265
static pa_http_protocol* http_protocol_new(pa_core *c) {
270
p = pa_xnew(pa_http_protocol, 1);
259
273
p->connections = pa_idxset_new(NULL, NULL);
261
pa_socket_server_set_callback(p->server, on_connection, p);
266
static void free_connection(void *p, PA_GCC_UNUSED void *userdata) {
268
connection_free(p, 0);
271
void pa_protocol_http_free(pa_protocol_http *p) {
274
pa_idxset_free(p->connections, free_connection, NULL);
275
pa_socket_server_unref(p->server);
275
pa_assert_se(pa_shared_set(c, "http-protocol", p) >= 0);
280
pa_http_protocol* pa_http_protocol_get(pa_core *c) {
283
if ((p = pa_shared_get(c, "http-protocol")))
284
return pa_http_protocol_ref(p);
286
return http_protocol_new(c);
289
pa_http_protocol* pa_http_protocol_ref(pa_http_protocol *p) {
291
pa_assert(PA_REFCNT_VALUE(p) >= 1);
298
void pa_http_protocol_unref(pa_http_protocol *p) {
299
struct connection *c;
302
pa_assert(PA_REFCNT_VALUE(p) >= 1);
304
if (PA_REFCNT_DEC(p) > 0)
307
while ((c = pa_idxset_first(p->connections, NULL)))
308
connection_unlink(c);
310
pa_idxset_free(p->connections, NULL, NULL);
312
pa_assert_se(pa_shared_remove(p->core, "http-protocol") >= 0);