1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
/*
* Copyright (C) 2003-2011 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "input/soup_input_plugin.h"
#include "input_internal.h"
#include "input_plugin.h"
#include "io_thread.h"
#include "conf.h"
#include <libsoup/soup-uri.h>
#include <libsoup/soup-session-async.h>
#include <assert.h>
#include <string.h>
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "input_soup"
/**
* Do not buffer more than this number of bytes. It should be a
* reasonable limit that doesn't make low-end machines suffer too
* much, but doesn't cause stuttering on high-latency lines.
*/
static const size_t SOUP_MAX_BUFFERED = 512 * 1024;
static SoupURI *soup_proxy;
static SoupSession *soup_session;
struct input_soup {
struct input_stream base;
GMutex *mutex;
GCond *cond;
SoupMessage *msg;
GQueue *buffers;
size_t current_consumed;
size_t total_buffered;
bool alive, pause, eof;
/**
* Set when the session callback has been invoked, when it is
* safe to free this object.
*/
bool completed;
GError *postponed_error;
};
static inline GQuark
soup_quark(void)
{
return g_quark_from_static_string("soup");
}
static bool
input_soup_init(const struct config_param *param, GError **error_r)
{
assert(soup_proxy == NULL);
assert(soup_session == NULL);
g_type_init();
const char *proxy = config_get_block_string(param, "proxy", NULL);
if (proxy != NULL) {
soup_proxy = soup_uri_new(proxy);
if (soup_proxy == NULL) {
g_set_error(error_r, soup_quark(), 0,
"failed to parse proxy setting");
return false;
}
}
soup_session =
soup_session_async_new_with_options(SOUP_SESSION_PROXY_URI,
soup_proxy,
SOUP_SESSION_ASYNC_CONTEXT,
io_thread_context(),
NULL);
return true;
}
static void
input_soup_finish(void)
{
assert(soup_session != NULL);
soup_session_abort(soup_session);
g_object_unref(G_OBJECT(soup_session));
if (soup_proxy != NULL)
soup_uri_free(soup_proxy);
}
static void
input_soup_session_callback(G_GNUC_UNUSED SoupSession *session,
G_GNUC_UNUSED SoupMessage *msg, gpointer user_data)
{
struct input_soup *s = user_data;
assert(msg == s->msg);
assert(!s->completed);
g_mutex_lock(s->mutex);
s->base.ready = true;
s->alive = false;
s->completed = true;
g_cond_broadcast(s->cond);
g_mutex_unlock(s->mutex);
}
static void
input_soup_got_headers(SoupMessage *msg, gpointer user_data)
{
struct input_soup *s = user_data;
if (!SOUP_STATUS_IS_SUCCESSFUL(msg->status_code)) {
g_mutex_lock(s->mutex);
if (s->postponed_error == NULL)
s->postponed_error =
g_error_new(soup_quark(), msg->status_code,
"got HTTP status %d",
msg->status_code);
g_mutex_unlock(s->mutex);
soup_session_cancel_message(soup_session, msg,
SOUP_STATUS_CANCELLED);
return;
}
soup_message_body_set_accumulate(msg->response_body, false);
g_mutex_lock(s->mutex);
s->base.ready = true;
g_cond_broadcast(s->cond);
g_mutex_unlock(s->mutex);
}
static void
input_soup_got_chunk(SoupMessage *msg, SoupBuffer *chunk, gpointer user_data)
{
struct input_soup *s = user_data;
assert(msg == s->msg);
g_mutex_lock(s->mutex);
g_queue_push_tail(s->buffers, soup_buffer_copy(chunk));
s->total_buffered += chunk->length;
if (s->total_buffered >= SOUP_MAX_BUFFERED && !s->pause) {
s->pause = true;
soup_session_pause_message(soup_session, msg);
}
g_cond_broadcast(s->cond);
g_mutex_unlock(s->mutex);
}
static void
input_soup_got_body(G_GNUC_UNUSED SoupMessage *msg, gpointer user_data)
{
struct input_soup *s = user_data;
assert(msg == s->msg);
g_mutex_lock(s->mutex);
s->base.ready = true;
s->eof = true;
s->alive = false;
g_cond_broadcast(s->cond);
g_mutex_unlock(s->mutex);
}
static bool
input_soup_wait_data(struct input_soup *s)
{
while (true) {
if (s->eof)
return true;
if (!s->alive)
return false;
if (!g_queue_is_empty(s->buffers))
return true;
assert(s->current_consumed == 0);
g_cond_wait(s->cond, s->mutex);
}
}
static struct input_stream *
input_soup_open(const char *uri, G_GNUC_UNUSED GError **error_r)
{
if (strncmp(uri, "http://", 7) != 0)
return NULL;
struct input_soup *s = g_new(struct input_soup, 1);
input_stream_init(&s->base, &input_plugin_soup, uri);
s->mutex = g_mutex_new();
s->cond = g_cond_new();
s->buffers = g_queue_new();
s->current_consumed = 0;
s->total_buffered = 0;
s->msg = soup_message_new(SOUP_METHOD_GET, uri);
soup_message_set_flags(s->msg, SOUP_MESSAGE_NO_REDIRECT);
soup_message_headers_append(s->msg->request_headers, "User-Agent",
"Music Player Daemon " VERSION);
g_signal_connect(s->msg, "got-headers",
G_CALLBACK(input_soup_got_headers), s);
g_signal_connect(s->msg, "got-chunk",
G_CALLBACK(input_soup_got_chunk), s);
g_signal_connect(s->msg, "got-body",
G_CALLBACK(input_soup_got_body), s);
s->alive = true;
s->pause = false;
s->eof = false;
s->completed = false;
s->postponed_error = NULL;
soup_session_queue_message(soup_session, s->msg,
input_soup_session_callback, s);
return &s->base;
}
static void
input_soup_close(struct input_stream *is)
{
struct input_soup *s = (struct input_soup *)is;
g_mutex_lock(s->mutex);
if (!s->completed) {
/* the messages's session callback hasn't been invoked
yet; cancel it and wait for completion */
g_mutex_unlock(s->mutex);
soup_session_cancel_message(soup_session, s->msg,
SOUP_STATUS_CANCELLED);
g_mutex_lock(s->mutex);
while (!s->completed)
g_cond_wait(s->cond, s->mutex);
}
g_mutex_unlock(s->mutex);
g_mutex_free(s->mutex);
g_cond_free(s->cond);
SoupBuffer *buffer;
while ((buffer = g_queue_pop_head(s->buffers)) != NULL)
soup_buffer_free(buffer);
g_queue_free(s->buffers);
input_stream_deinit(&s->base);
g_free(s);
}
static int
input_soup_buffer(struct input_stream *is, GError **error_r)
{
struct input_soup *s = (struct input_soup *)is;
g_mutex_lock(s->mutex);
if (s->pause) {
if (s->total_buffered >= SOUP_MAX_BUFFERED) {
g_mutex_unlock(s->mutex);
return 1;
}
s->pause = false;
soup_session_unpause_message(soup_session, s->msg);
}
bool success = input_soup_wait_data(s);
if (!success) {
if (s->postponed_error != NULL) {
g_propagate_error(error_r, s->postponed_error);
s->postponed_error = NULL;
} else
g_set_error_literal(error_r, soup_quark(), 0,
"HTTP failure");
}
g_mutex_unlock(s->mutex);
if (!success) {
return -1;
}
return 1;
}
static size_t
input_soup_read(struct input_stream *is, void *ptr, size_t size,
G_GNUC_UNUSED GError **error_r)
{
struct input_soup *s = (struct input_soup *)is;
g_mutex_lock(s->mutex);
if (!input_soup_wait_data(s)) {
assert(!s->alive);
if (s->postponed_error != NULL) {
g_propagate_error(error_r, s->postponed_error);
s->postponed_error = NULL;
} else
g_set_error_literal(error_r, soup_quark(), 0,
"HTTP failure");
g_mutex_unlock(s->mutex);
return 0;
}
char *p0 = ptr, *p = p0, *p_end = p0 + size;
while (p < p_end) {
SoupBuffer *buffer = g_queue_pop_head(s->buffers);
if (buffer == NULL) {
assert(s->current_consumed == 0);
break;
}
assert(s->current_consumed < buffer->length);
assert(s->total_buffered >= buffer->length);
const char *q = buffer->data;
q += s->current_consumed;
size_t remaining = buffer->length - s->current_consumed;
size_t nbytes = p_end - p;
if (nbytes > remaining)
nbytes = remaining;
memcpy(p, q, nbytes);
p += nbytes;
s->current_consumed += remaining;
if (s->current_consumed >= buffer->length) {
/* done with this buffer */
s->total_buffered -= buffer->length;
soup_buffer_free(buffer);
s->current_consumed = 0;
} else {
/* partial read */
assert(p == p_end);
g_queue_push_head(s->buffers, buffer);
}
}
if (s->pause && s->total_buffered < SOUP_MAX_BUFFERED) {
s->pause = false;
soup_session_unpause_message(soup_session, s->msg);
}
size_t nbytes = p - p0;
s->base.offset += nbytes;
g_mutex_unlock(s->mutex);
return nbytes;
}
static bool
input_soup_eof(G_GNUC_UNUSED struct input_stream *is)
{
struct input_soup *s = (struct input_soup *)is;
return !s->alive && g_queue_is_empty(s->buffers);
}
const struct input_plugin input_plugin_soup = {
.name = "soup",
.init = input_soup_init,
.finish = input_soup_finish,
.open = input_soup_open,
.close = input_soup_close,
.buffer = input_soup_buffer,
.read = input_soup_read,
.eof = input_soup_eof,
};
|