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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* soup-connection.c: Connection-handling code.
*
* Copyright (C) 2000-2003, Ximian, Inc.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "soup-auth-context.h"
#include "soup-connection.h"
#include "soup-private.h"
#include "soup-socket.h"
#include "soup-ssl.h"
struct _SoupConnectionPrivate {
SoupAuthContext *ac;
guint watch_tag;
gboolean in_use;
};
#define PARENT_TYPE SOUP_TYPE_SOCKET
static SoupSocketClass *parent_class;
enum {
PROP_0,
PROP_AUTH_CONTEXT
};
static void connected (SoupSocket *sock, SoupKnownErrorCode status);
static SoupAddress *get_remote_address (SoupConnection *conn);
static void start_request (SoupConnection *conn, SoupMessage *msg);
static void set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec);
static void
init (GObject *object)
{
SoupConnection *conn = SOUP_CONNECTION (object);
conn->priv = g_new0 (SoupConnectionPrivate, 1);
}
static void
finalize (GObject *object)
{
SoupConnection *conn = SOUP_CONNECTION (object);
if (conn->priv->ac)
g_object_unref (conn->priv->ac);
if (conn->priv->watch_tag)
g_source_remove (conn->priv->watch_tag);
g_free (conn->priv);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
class_init (GObjectClass *object_class)
{
SoupSocketClass *socket_class =
SOUP_SOCKET_CLASS (object_class);
SoupConnectionClass *connection_class =
SOUP_CONNECTION_CLASS (object_class);
parent_class = g_type_class_ref (PARENT_TYPE);
/* virtual method definition */
connection_class->get_remote_address = get_remote_address;
connection_class->start_request = start_request;
/* virtual method override */
socket_class->connected = connected;
object_class->finalize = finalize;
object_class->set_property = set_property;
/* properties */
g_object_class_install_property (
object_class, PROP_AUTH_CONTEXT,
g_param_spec_object ("auth_context",
"Authentication context",
"Object that manages authentication for th e connection",
SOUP_TYPE_AUTH_CONTEXT,
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
}
SOUP_MAKE_TYPE (soup_connection, SoupConnection, class_init, init, PARENT_TYPE)
static void
set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec)
{
SoupConnection *conn = SOUP_CONNECTION (object);
switch (prop_id) {
case PROP_AUTH_CONTEXT:
conn->priv->ac =
g_object_ref (g_value_get_object (value));
break;
default:
break;
}
}
static gboolean
connection_watch (GIOChannel *iochannel, GIOCondition condition,
gpointer user_data)
{
SoupConnection *conn = user_data;
if (!conn->priv->in_use) {
g_object_unref (conn);
return FALSE;
}
return TRUE;
}
static void
connected (SoupSocket *sock, SoupKnownErrorCode status)
{
SoupConnection *conn = SOUP_CONNECTION (sock);
GIOChannel *chan;
if (status != SOUP_ERROR_OK)
return;
chan = soup_socket_get_iochannel (sock);
conn->priv->watch_tag =
g_io_add_watch (chan, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
connection_watch, conn);
}
/**
* soup_connection_new:
* @uri: URI of the server to connect to
* @ac: auth context for that server
*
* Initiates the process of establishing a connection to the server
* referenced by @uri.
*
* Return value: the new #SoupConnection.
*/
SoupConnection *
soup_connection_new (const SoupUri *uri, SoupAuthContext *ac)
{
SoupConnection *conn;
g_return_val_if_fail (uri != NULL, NULL);
conn = g_object_new (SOUP_TYPE_CONNECTION,
"auth_context", ac,
NULL);
soup_socket_client_connect (SOUP_SOCKET (conn), uri);
return conn;
}
/**
* soup_connection_get_iochannel:
* @conn: a #SoupConnection.
*
* Returns a GIOChannel used for IO operations on the network connection
* represented by @conn. The caller should not ref or unref the channel;
* it belongs to the connection.
*
* Return value: a pointer to the GIOChannel used for IO on @conn.
*/
GIOChannel *
soup_connection_get_iochannel (SoupConnection *conn)
{
g_return_val_if_fail (SOUP_IS_CONNECTION (conn), NULL);
return soup_socket_get_iochannel (SOUP_SOCKET (conn));
}
/**
* soup_connection_set_in_use:
* @conn: a #SoupConnection.
* @in_use: whether or not @conn is in use
*
* Sets the in-use flag on the #SoupConnection pointed to by @conn.
*/
void
soup_connection_set_in_use (SoupConnection *conn, gboolean in_use)
{
g_return_if_fail (SOUP_IS_CONNECTION (conn));
if (conn->priv->in_use == in_use)
return;
conn->priv->in_use = in_use;
if (in_use)
g_object_ref (conn);
else
g_object_unref (conn);
}
/**
* soup_connection_is_in_use:
* @conn: a #SoupConnection.
*
* Return value: the in-use flag for @conn.
*/
gboolean
soup_connection_is_in_use (SoupConnection *conn)
{
g_return_val_if_fail (SOUP_IS_CONNECTION (conn), FALSE);
return conn->priv->in_use;
}
static SoupAddress *
get_remote_address (SoupConnection *conn)
{
return soup_socket_get_remote_address (SOUP_SOCKET (conn));
}
/**
* soup_connection_get_remote_address:
* @conn: a #SoupConnection
*
* Return value: the address of the remote server @conn is connected
* to, or %NULL if it is connected to a proxy that can talk to any
* server (or if @conn is invalid or not connected).
**/
SoupAddress *
soup_connection_get_remote_address (SoupConnection *conn)
{
g_return_val_if_fail (SOUP_IS_CONNECTION (conn), NULL);
return SOUP_CONNECTION_GET_CLASS (conn)->get_remote_address (conn);
}
static void
authorize_handler (SoupMessage *msg, gpointer ac)
{
if (soup_auth_context_handle_unauthorized (ac, msg))
soup_message_requeue (msg);
}
static void
start_request (SoupConnection *conn, SoupMessage *msg)
{
msg->connection = conn;
/* Handle authorization */
if (conn->priv->ac) {
soup_auth_context_authorize_message (conn->priv->ac, msg);
soup_message_remove_handler (
msg, SOUP_HANDLER_PRE_BODY,
authorize_handler, conn->priv->ac);
soup_message_add_error_code_handler (
msg, SOUP_ERROR_UNAUTHORIZED,
SOUP_HANDLER_PRE_BODY,
authorize_handler, conn->priv->ac);
}
soup_message_send_request (msg);
}
/**
* soup_connection_start_request:
* @conn: a #SoupConnection
* @msg: a #SoupMessage
*
* Queues @msg on @conn.
**/
void
soup_connection_start_request (SoupConnection *conn, SoupMessage *msg)
{
g_return_if_fail (SOUP_IS_CONNECTION (conn));
g_return_if_fail (SOUP_IS_MESSAGE (msg));
return SOUP_CONNECTION_GET_CLASS (conn)->start_request (conn, msg);
}
/**
* soup_connection_close:
* @conn: a #SoupConnection
*
* Marks @conn not in use and then unrefs it, which should cause
* the connection to be destroyed.
*/
void
soup_connection_close (SoupConnection *conn)
{
g_return_if_fail (SOUP_IS_CONNECTION (conn));
soup_connection_set_in_use (conn, FALSE);
g_object_unref (conn);
}
|