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
|
/* soup-output-stream.c, based on gunixoutputstream.c
*
* Copyright (C) 2006-2008 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <string.h>
#include <glib.h>
#include <gio/gio.h>
#include <libsoup/soup.h>
#include "soup-output-stream.h"
#include "soup-input-stream.h"
G_DEFINE_TYPE (SoupOutputStream, soup_output_stream, G_TYPE_OUTPUT_STREAM)
typedef void (*SoupOutputStreamCallback) (GOutputStream *);
typedef struct {
SoupSession *session;
GMainContext *async_context;
SoupMessage *msg;
gboolean finished;
goffset size, offset;
GByteArray *ba;
GCancellable *cancellable;
GSource *cancel_watch;
SoupOutputStreamCallback finished_cb;
SoupOutputStreamCallback cancelled_cb;
GSimpleAsyncResult *result;
} SoupOutputStreamPrivate;
#define SOUP_OUTPUT_STREAM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_OUTPUT_STREAM, SoupOutputStreamPrivate))
static gssize soup_output_stream_write (GOutputStream *stream,
const void *buffer,
gsize count,
GCancellable *cancellable,
GError **error);
static gboolean soup_output_stream_close (GOutputStream *stream,
GCancellable *cancellable,
GError **error);
static void soup_output_stream_write_async (GOutputStream *stream,
const void *buffer,
gsize count,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer data);
static gssize soup_output_stream_write_finish (GOutputStream *stream,
GAsyncResult *result,
GError **error);
static void soup_output_stream_close_async (GOutputStream *stream,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer data);
static gboolean soup_output_stream_close_finish (GOutputStream *stream,
GAsyncResult *result,
GError **error);
static void soup_output_stream_finished (SoupMessage *msg, gpointer stream);
static void
soup_output_stream_finalize (GObject *object)
{
SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (object);
g_object_unref (priv->session);
g_signal_handlers_disconnect_by_func (priv->msg, G_CALLBACK (soup_output_stream_finished), object);
g_object_unref (priv->msg);
if (priv->ba)
g_byte_array_free (priv->ba, TRUE);
if (G_OBJECT_CLASS (soup_output_stream_parent_class)->finalize)
(*G_OBJECT_CLASS (soup_output_stream_parent_class)->finalize) (object);
}
static void
soup_output_stream_class_init (SoupOutputStreamClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
g_type_class_add_private (klass, sizeof (SoupOutputStreamPrivate));
gobject_class->finalize = soup_output_stream_finalize;
stream_class->write_fn = soup_output_stream_write;
stream_class->close_fn = soup_output_stream_close;
stream_class->write_async = soup_output_stream_write_async;
stream_class->write_finish = soup_output_stream_write_finish;
stream_class->close_async = soup_output_stream_close_async;
stream_class->close_finish = soup_output_stream_close_finish;
}
static void
soup_output_stream_init (SoupOutputStream *stream)
{
SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
priv->ba = g_byte_array_new ();
}
/**
* soup_output_stream_new:
* @session: the #SoupSession to use
* @msg: the #SoupMessage whose request will be streamed
* @size: the total size of the request body, or -1 if not known
*
* Prepares to send @msg over @session, and returns a #GOutputStream
* that can be used to write the response. The server's response will
* be available in @msg after calling soup_output_stream_close()
* (which will return a %SOUP_OUTPUT_STREAM_HTTP_ERROR #GError if the
* status is not 2xx).
*
* If you know the total number of bytes that will be written, pass
* that in @size. Otherwise, pass -1. (If you pass a size, you MUST
* write that many bytes to the stream; Trying to write more than
* that, or closing the stream without having written enough, will
* result in an error.
*
* In some situations, the request will not actually be sent until you
* call g_output_stream_close(). (In fact, currently this is *always*
* true.)
*
* Internally, #SoupOutputStream is implemented using asynchronous
* I/O, so if you are using the synchronous API (eg,
* g_output_stream_write()), you should create a new #GMainContext and
* set it as the %SOUP_SESSION_ASYNC_CONTEXT property on @session. (If
* you don't, then synchronous #GOutputStream calls will cause the
* main loop to be run recursively.) The async #GOutputStream API
* works fine with %SOUP_SESSION_ASYNC_CONTEXT either set or unset.
*
* Returns: a new #GOutputStream.
**/
GOutputStream *
soup_output_stream_new (SoupSession *session, SoupMessage *msg, goffset size)
{
SoupOutputStream *stream;
SoupOutputStreamPrivate *priv;
g_return_val_if_fail (SOUP_IS_MESSAGE (msg), NULL);
stream = g_object_new (SOUP_TYPE_OUTPUT_STREAM, NULL);
priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
priv->session = g_object_ref (session);
priv->async_context = soup_session_get_async_context (session);
priv->msg = g_object_ref (msg);
priv->size = size;
return G_OUTPUT_STREAM (stream);
}
static gboolean
soup_output_stream_cancelled (GIOChannel *chan, GIOCondition condition,
gpointer stream)
{
SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
priv->cancel_watch = NULL;
soup_session_pause_message (priv->session, priv->msg);
if (priv->cancelled_cb)
priv->cancelled_cb (stream);
return FALSE;
}
static void
soup_output_stream_prepare_for_io (GOutputStream *stream, GCancellable *cancellable)
{
SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
int cancel_fd;
/* Move the buffer to the SoupMessage */
soup_message_body_append (priv->msg->request_body, SOUP_MEMORY_TAKE,
priv->ba->data, priv->ba->len);
g_byte_array_free (priv->ba, FALSE);
priv->ba = NULL;
/* Set up cancellation */
priv->cancellable = cancellable;
cancel_fd = g_cancellable_get_fd (cancellable);
if (cancel_fd != -1)
{
GIOChannel *chan = g_io_channel_unix_new (cancel_fd);
priv->cancel_watch = soup_add_io_watch (priv->async_context, chan,
G_IO_IN | G_IO_ERR | G_IO_HUP,
soup_output_stream_cancelled,
stream);
g_io_channel_unref (chan);
}
/* Add an extra ref since soup_session_queue_message steals one */
g_object_ref (priv->msg);
soup_session_queue_message (priv->session, priv->msg, NULL, NULL);
}
static void
soup_output_stream_done_io (GOutputStream *stream)
{
SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
if (priv->cancel_watch)
{
g_source_destroy (priv->cancel_watch);
priv->cancel_watch = NULL;
g_cancellable_release_fd (priv->cancellable);
}
priv->cancellable = NULL;
}
static gboolean
set_error_if_http_failed (SoupMessage *msg, GError **error)
{
if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
{
g_set_error_literal (error, SOUP_HTTP_ERROR,
msg->status_code, msg->reason_phrase);
return TRUE;
}
return FALSE;
}
static gssize
soup_output_stream_write (GOutputStream *stream,
const void *buffer,
gsize count,
GCancellable *cancellable,
GError **error)
{
SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
if (priv->size > 0 && priv->offset + count > priv->size) {
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
"Write would exceed caller-defined file size");
return -1;
}
g_byte_array_append (priv->ba, buffer, count);
priv->offset += count;
return count;
}
static int
soup_output_stream_close (GOutputStream *stream,
GCancellable *cancellable,
GError **error)
{
SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
if (priv->size > 0 && priv->offset != priv->size) {
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
"File is incomplete");
return -1;
}
soup_output_stream_prepare_for_io (stream, cancellable);
while (!priv->finished && !g_cancellable_is_cancelled (cancellable))
g_main_context_iteration (priv->async_context, TRUE);
soup_output_stream_done_io (stream);
return !set_error_if_http_failed (priv->msg, error);
}
static void
soup_output_stream_write_async (GOutputStream *stream,
const void *buffer,
gsize count,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
GSimpleAsyncResult *result;
result = g_simple_async_result_new (G_OBJECT (stream),
callback, user_data,
soup_output_stream_write_async);
if (priv->size > 0 && priv->offset + count > priv->size)
{
GError *error;
error = g_error_new (G_IO_ERROR, G_IO_ERROR_NO_SPACE,
"Write would exceed caller-defined file size");
g_simple_async_result_set_from_error (result, error);
g_error_free (error);
}
else
{
g_byte_array_append (priv->ba, buffer, count);
priv->offset += count;
g_simple_async_result_set_op_res_gssize (result, count);
}
g_simple_async_result_complete_in_idle (result);
}
static gssize
soup_output_stream_write_finish (GOutputStream *stream,
GAsyncResult *result,
GError **error)
{
GSimpleAsyncResult *simple;
gssize nwritten;
simple = G_SIMPLE_ASYNC_RESULT (result);
g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == soup_output_stream_write_async);
nwritten = g_simple_async_result_get_op_res_gssize (simple);
return nwritten;
}
static void
close_async_done (GOutputStream *stream)
{
SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
GSimpleAsyncResult *result;
GError *error = NULL;
result = priv->result;
priv->result = NULL;
if (g_cancellable_set_error_if_cancelled (priv->cancellable, &error) ||
set_error_if_http_failed (priv->msg, &error))
{
g_simple_async_result_set_from_error (result, error);
g_error_free (error);
}
else
g_simple_async_result_set_op_res_gboolean (result, TRUE);
priv->finished_cb = NULL;
priv->cancelled_cb = NULL;
soup_output_stream_done_io (stream);
g_simple_async_result_complete (result);
}
static void
soup_output_stream_finished (SoupMessage *msg, gpointer stream)
{
SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
priv->finished = TRUE;
g_signal_handlers_disconnect_by_func (priv->msg, G_CALLBACK (soup_output_stream_finished), stream);
close_async_done (stream);
}
static void
soup_output_stream_close_async (GOutputStream *stream,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream);
GSimpleAsyncResult *result;
result = g_simple_async_result_new (G_OBJECT (stream),
callback, user_data,
soup_output_stream_close_async);
if (priv->size > 0 && priv->offset != priv->size)
{
GError *error;
error = g_error_new (G_IO_ERROR, G_IO_ERROR_NO_SPACE,
"File is incomplete");
g_simple_async_result_set_from_error (result, error);
g_error_free (error);
g_simple_async_result_complete_in_idle (result);
return;
}
priv->result = result;
priv->cancelled_cb = close_async_done;
g_signal_connect (priv->msg, "finished",
G_CALLBACK (soup_output_stream_finished), stream);
soup_output_stream_prepare_for_io (stream, cancellable);
}
static gboolean
soup_output_stream_close_finish (GOutputStream *stream,
GAsyncResult *result,
GError **error)
{
/* Failures handled in generic close_finish code */
return TRUE;
}
|