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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* soup-client-input-stream.c
*
* Copyright 2010-2012 Red Hat, Inc.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "soup-client-input-stream.h"
#include "soup.h"
#include "soup-marshal.h"
#include "soup-message-private.h"
struct _SoupClientInputStreamPrivate {
SoupMessage *msg;
};
enum {
EOF,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
enum {
PROP_0,
PROP_MESSAGE
};
static GPollableInputStreamInterface *soup_client_input_stream_parent_pollable_interface;
static void soup_client_input_stream_pollable_init (GPollableInputStreamInterface *pollable_interface, gpointer interface_data);
G_DEFINE_TYPE_WITH_CODE (SoupClientInputStream, soup_client_input_stream, SOUP_TYPE_FILTER_INPUT_STREAM,
G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM,
soup_client_input_stream_pollable_init))
static void
soup_client_input_stream_init (SoupClientInputStream *stream)
{
stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
SOUP_TYPE_CLIENT_INPUT_STREAM,
SoupClientInputStreamPrivate);
}
static void
soup_client_input_stream_finalize (GObject *object)
{
SoupClientInputStream *cistream = SOUP_CLIENT_INPUT_STREAM (object);
g_clear_object (&cistream->priv->msg);
G_OBJECT_CLASS (soup_client_input_stream_parent_class)->finalize (object);
}
static void
soup_client_input_stream_set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec)
{
SoupClientInputStream *cistream = SOUP_CLIENT_INPUT_STREAM (object);
switch (prop_id) {
case PROP_MESSAGE:
cistream->priv->msg = g_value_dup_object (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
soup_client_input_stream_get_property (GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec)
{
SoupClientInputStream *cistream = SOUP_CLIENT_INPUT_STREAM (object);
switch (prop_id) {
case PROP_MESSAGE:
g_value_set_object (value, cistream->priv->msg);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static gssize
soup_client_input_stream_read_fn (GInputStream *stream,
void *buffer,
gsize count,
GCancellable *cancellable,
GError **error)
{
gssize nread;
nread = G_INPUT_STREAM_CLASS (soup_client_input_stream_parent_class)->
read_fn (stream, buffer, count, cancellable, error);
if (nread == 0)
g_signal_emit (stream, signals[EOF], 0);
return nread;
}
static gssize
soup_client_input_stream_read_nonblocking (GPollableInputStream *stream,
void *buffer,
gsize count,
GError **error)
{
gssize nread;
nread = soup_client_input_stream_parent_pollable_interface->
read_nonblocking (stream, buffer, count, error);
if (nread == 0)
g_signal_emit (stream, signals[EOF], 0);
return nread;
}
static gboolean
soup_client_input_stream_close_fn (GInputStream *stream,
GCancellable *cancellable,
GError **error)
{
SoupClientInputStream *cistream = SOUP_CLIENT_INPUT_STREAM (stream);
return soup_message_io_run_until_finish (cistream->priv->msg,
cancellable, error);
}
static gboolean
idle_finish_close (gpointer user_data)
{
GTask *task = user_data;
g_task_return_boolean (task, TRUE);
g_object_unref (task);
return FALSE;
}
static gboolean
close_async_ready (SoupMessage *msg, gpointer user_data)
{
GTask *task = user_data;
SoupClientInputStream *cistream = g_task_get_source_object (task);
GError *error = NULL;
if (!soup_message_io_run_until_finish (cistream->priv->msg,
g_task_get_cancellable (task),
&error) &&
g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) {
g_error_free (error);
return TRUE;
}
if (error) {
g_task_return_error (task, error);
g_object_unref (task);
return FALSE;
}
/* Due to a historical accident, SoupSessionAsync relies on us
* waiting one extra cycle after run_until_finish() returns.
* Ugh. FIXME later when it's easier to do.
*/
soup_add_idle (g_main_context_get_thread_default (),
idle_finish_close, task);
return FALSE;
}
static void
soup_client_input_stream_close_async (GInputStream *stream,
gint priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
SoupClientInputStream *cistream = SOUP_CLIENT_INPUT_STREAM (stream);
GTask *task;
GSource *source;
task = g_task_new (stream, cancellable, callback, user_data);
g_task_set_priority (task, priority);
source = soup_message_io_get_source (cistream->priv->msg,
cancellable, NULL, NULL);
g_task_attach_source (task, source, (GSourceFunc) close_async_ready);
g_source_unref (source);
}
static gboolean
soup_client_input_stream_close_finish (GInputStream *stream,
GAsyncResult *result,
GError **error)
{
return g_task_propagate_boolean (G_TASK (result), error);
}
static void
soup_client_input_stream_class_init (SoupClientInputStreamClass *stream_class)
{
GObjectClass *object_class = G_OBJECT_CLASS (stream_class);
GInputStreamClass *input_stream_class = G_INPUT_STREAM_CLASS (stream_class);
g_type_class_add_private (stream_class, sizeof (SoupClientInputStreamPrivate));
object_class->finalize = soup_client_input_stream_finalize;
object_class->set_property = soup_client_input_stream_set_property;
object_class->get_property = soup_client_input_stream_get_property;
input_stream_class->read_fn = soup_client_input_stream_read_fn;
input_stream_class->close_fn = soup_client_input_stream_close_fn;
input_stream_class->close_async = soup_client_input_stream_close_async;
input_stream_class->close_finish = soup_client_input_stream_close_finish;
signals[EOF] =
g_signal_new ("eof",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
0,
NULL, NULL,
_soup_marshal_NONE__NONE,
G_TYPE_NONE, 0);
g_object_class_install_property (
object_class, PROP_MESSAGE,
g_param_spec_object ("message",
"Message",
"Message",
SOUP_TYPE_MESSAGE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}
static void
soup_client_input_stream_pollable_init (GPollableInputStreamInterface *pollable_interface,
gpointer interface_data)
{
soup_client_input_stream_parent_pollable_interface =
g_type_interface_peek_parent (pollable_interface);
pollable_interface->read_nonblocking = soup_client_input_stream_read_nonblocking;
}
GInputStream *
soup_client_input_stream_new (GInputStream *base_stream,
SoupMessage *msg)
{
return g_object_new (SOUP_TYPE_CLIENT_INPUT_STREAM,
"base-stream", base_stream,
"message", msg,
NULL);
}
|