summaryrefslogtreecommitdiff
path: root/docs/reference/client-howto.xml
blob: a53f2ac329a53c48c453fc464e9fa96e08eb82de (plain)
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
<?xml version="1.0"?>
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" 
               "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">
<refentry id="libsoup-client-howto">
<refmeta>
<refentrytitle>Soup Client Basics</refentrytitle>
<manvolnum>3</manvolnum>
<refmiscinfo>LIBSOUP Library</refmiscinfo>
</refmeta>

<refnamediv>
<refname>Soup Client Basics</refname><refpurpose>Client-side tutorial</refpurpose>
</refnamediv>

<refsect2>
<title>Creating a <type>SoupSession</type></title>

<para>
The first step in using the client API is to create a <link
linkend="SoupSession"><type>SoupSession</type></link>. The session object
encapsulates all of the state that <application>libsoup</application>
is keeping on behalf of your program; cached HTTP connections,
authentication information, etc.
</para>

<para>
There are two subclasses of <link
linkend="SoupSession"><type>SoupSession</type></link> that you can use, with
slightly different behavior:
</para>

<itemizedlist>
    <listitem><para>
	<link linkend="SoupSessionAsync"><type>SoupSessionAsync</type></link>,
	which uses callbacks and the glib main loop to provide
	asynchronous I/O.
    </para></listitem>

    <listitem><para>
	<link linkend="SoupSessionSync"><type>SoupSessionSync</type></link>,
	which uses blocking I/O rather than callbacks, making it more
	suitable for threaded applications.
    </para></listitem>
</itemizedlist>

<para>
If you want to do a mix of mainloop-based and blocking I/O, you will
need to create two different session objects.
</para>

<para>
When you create the session (with <link
linkend="soup-session-async-new-with-options"><function>soup_session_async_new_with_options</function></link>
or <link
linkend="soup-session-sync-new-with-options"><function>soup_session_sync_new_with_options</function></link>),
you can specify various additional options:
</para>

<variablelist>
    <varlistentry>
	<term><link linkend="SOUP-SESSION-MAX-CONNS:CAPS"><literal>SOUP_SESSION_MAX_CONNS</literal></link></term>
	<listitem><para>
	    Allows you to set the maximum total number of connections
	    the session will have open at one time. (Once it reaches
	    this limit, it will either close idle connections, or
	    wait for existing connections to free up before starting
	    new requests.)
	</para></listitem>
    </varlistentry>
    <varlistentry>
	<term><link linkend="SOUP-SESSION-MAX-CONNS-PER-HOST:CAPS"><literal>SOUP_SESSION_MAX_CONNS_PER_HOST</literal></link></term>
	<listitem><para>
	    Allows you to set the maximum total number of connections
	    the session will have open <emphasis>to a single
	    host</emphasis> at one time.
	</para></listitem>
    </varlistentry>
    <varlistentry>
	<term><link linkend="SOUP-SESSION-USE-NTLM:CAPS"><literal>SOUP_SESSION_USE_NTLM</literal></link></term>
	<listitem><para>
	    If <literal>TRUE</literal>, then Microsoft NTLM
	    authentication will be used if available (and will be
	    preferred to HTTP Basic or Digest authentication).
	    If <literal>FALSE</literal>, NTLM authentication won't be
	    used, even if it's the only authentication type available.
	    (NTLM works differently from the standard HTTP
	    authentication types, so it needs to be handled
	    specially.)
	</para></listitem>
    </varlistentry>
    <varlistentry>
	<term><link linkend="SOUP-SESSION-SSL-CA-FILE:CAPS"><literal>SOUP_SESSION_SSL_CA_FILE</literal></link></term>
	<listitem><para>
	    Points to a file containing certificates for recognized
	    SSL Certificate Authorities. If this is set, then HTTPS
	    connections will be checked against these authorities, and
	    rejected if they can't be verified. (Otherwise all SSL
	    certificates will be accepted automatically.)
	</para></listitem>
    </varlistentry>
    <varlistentry>
	<term><link linkend="SOUP-SESSION-ASYNC-CONTEXT:CAPS"><literal>SOUP_SESSION_ASYNC_CONTEXT</literal></link></term>
	<listitem><para>
	    A <link
	    linkend="GMainContext"><type>GMainContext</type></link>
	    which the session will use for asynchronous operations.
	    This can be set if you want to use a
	    <type>SoupSessionAsync</type> in a thread other than the
	    main thread.
	</para></listitem>
    </varlistentry>
    <varlistentry>
	<term><link linkend="SOUP-SESSION-ADD-FEATURE:CAPS"><literal>SOUP_SESSION_ADD_FEATURE</literal></link> and <link linkend="SOUP-SESSION-ADD-FEATURE-BY-TYPE:CAPS"><literal>SOUP_SESSION_ADD_FEATURE_BY_TYPE</literal></link></term>
	<listitem><para>
	    These allow you to specify <link
	    linkend="SoupSessionFeature"><type>SoupSessionFeature</type></link>s
	    (discussed <link linkend="session-features">below</link>)
	    to add at construct-time.
	</para></listitem>
    </varlistentry>
</variablelist>

<para>
If you don't need to specify any options, you can just use <link
linkend="soup-session-async-new"><function>soup_session_async_new</function></link> or
<link linkend="soup-session-sync-new"><function>soup_session_sync_new</function></link>,
which take no arguments.
</para>

</refsect2>

<refsect2 id="session-features">
<title>Session features</title>

<para>
Additional session functionality is provided as <link
linkend="SoupSessionFeature"><type>SoupSessionFeature</type></link>s,
which can be added to a session, via the <link
linkend="SOUP-SESSION-ADD-FEATURE:CAPS"><literal>SOUP_SESSION_ADD_FEATURE</literal></link>
and <link
linkend="SOUP-SESSION-ADD-FEATURE-BY-TYPE:CAPS"><literal>SOUP_SESSION_ADD_FEATURE_BY_TYPE</literal></link>
options at session-construction-time, or afterward via the <link
linkend="soup-session-add-feature"><function>soup_session_add_feature</function></link>
and <link
linkend="soup-session-add-feature-by-type"><function>soup_session_add_feature_by_type</function></link>
functions. Some of the features available in
<application>libsoup</application> are:
</para>

<variablelist>
    <varlistentry>
	<term><link linkend="SoupLogger"><type>SoupLogger</type></link></term>
	<listitem><para>
	    A debugging aid, which logs all of libsoup's HTTP traffic
	    to <literal>stdout</literal> (or another place you specify).
	</para></listitem>
    </varlistentry>
    <varlistentry>
	<term><link linkend="SoupCookieJar"><type>SoupCookieJar</type></link> and <link linkend="SoupCookieJarText"><type>SoupCookieJarText</type></link></term>
	<listitem><para>
	    Support for HTTP cookies. <type>SoupCookieJar</type>
	    provides non-persistent cookie storage, while
	    <type>SoupCookieJarText</type> uses a text file to keep
	    track of cookies between sessions.
	</para></listitem>
    </varlistentry>
</variablelist>

<para>
And in <application>libsoup-gnome</application>:
</para>

<variablelist>
    <varlistentry>
	<term><link linkend="SOUP-TYPE-PROXY-RESOLVER-GNOME:CAPS"><type>SoupProxyResolverGNOME</type></link></term>
	<listitem><para>
	    A feature that automatically determines the correct HTTP
	    proxy to use for requests.
	</para></listitem>
    </varlistentry>
    <varlistentry>
	<term><link linkend="SoupCookieJarSqlite"><type>SoupCookieJarSqlite</type></link></term>
	<listitem><para>
	    Support for HTTP cookies stored in an
	    <application>SQLite</application> database.
	</para></listitem>
    </varlistentry>
</variablelist>

<para>
Use the "add_feature_by_type" property/function to add features that
don't require any configuration (such as <link
linkend="SOUP-TYPE-PROXY-RESOLVER-GNOME:CAPS"><type>SoupProxyResolverGNOME</type></link>),
and the "add_feature" property/function to add features that must be
constructed first (such as <link
linkend="SoupLogger"><type>SoupLogger</type></link>). For example, an
application might do something like the following:
</para>

<informalexample><programlisting>
	session = soup_session_async_new_with_options (
#ifdef HAVE_LIBSOUP_GNOME
		SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_PROXY_RESOLVER_GNOME,
#endif
		NULL);
	if (debug_level) {
		SoupLogger *logger;

		logger = soup_logger_new (debug_level, -1);
		soup_session_add_feature (session, SOUP_SESSION_FEATURE (logger));
		g_object_unref (logger);
	}
</programlisting></informalexample>

</refsect2>

<refsect2>
<title>Creating and Sending SoupMessages</title>

<para>
Once you have a session, you do HTTP traffic using <link
linkend="SoupMessage"><type>SoupMessage</type></link>. In the simplest
case, you only need to create the message and it's ready to send:
</para>

<informalexample><programlisting>
	SoupMessage *msg;

	msg = soup_message_new ("GET", "http://example.com/");
</programlisting></informalexample>

<para>
In more complicated cases, you can use various <link
linkend="SoupMessage">SoupMessage</link>, <link
linkend="SoupMessageHeaders">SoupMessageHeaders</link>, and <link
linkend="SoupMessageBody">SoupMessageBody</link> methods to set the
request headers and body of the message:
</para>

<informalexample><programlisting>
	SoupMessage *msg;

	msg = soup_message_new ("POST", "http://example.com/form.cgi");
	soup_message_set_request (msg, "application/x-www-form-urlencoded",
				  SOUP_MEMORY_COPY, formdata, strlen (formdata));
	soup_message_headers_append (msg->request_headers, "Referer", referring_url);
</programlisting></informalexample>

<para>
(Although this is a bad example, because
<application>libsoup</application> actually has convenience methods
for dealing with <link linkend="libsoup-2.4-HTML-Form-Support">HTML
forms</link>, as well as <link
linkend="libsoup-2.4-XMLRPC-Support">XML-RPC</link>.)
</para>

<para>
You can also use <link
linkend="soup-message-set-flags"><function>soup_message_set_flags</function></link>
to change some default behaviors. For example, by default,
<type>SoupSession</type> automatically handles responses from the
server that redirect to another URL. If you would like to handle these
yourself, you can set the <link linkend="SOUP-MESSAGE-NO-REDIRECT:CAPS"><literal>SOUP_MESSAGE_NO_REDIRECT</literal></link>
flag.
</para>

<refsect3>
<title>Sending a Message Synchronously</title>

<para>
To send a message and wait for the response, use <link
linkend="soup-session-send-message"><function>soup_session_send_message</function></link>:
</para>

<informalexample><programlisting>
	guint status;

	status = soup_session_send_message (session, msg);
</programlisting></informalexample>

<para>
(If you use <function>soup_session_send_message</function> with a
<link linkend="SoupSessionAsync"><type>SoupSessionAsync</type></link>,
it will run the main loop itself until the message is complete.)
</para>

<para>
The return value from <function>soup_session_send_message</function>
is a <link linkend="libsoup-2.4-soup-status">libsoup status code</link>,
indicating either a transport error that prevented the message from
being sent, or the HTTP status that was returned by the server in
response to the message. (The status is also available as
<literal>msg->status_code</literal>.)
</para>

</refsect3>

<refsect3>
<title>Sending a Message Asynchronously</title>

<para>
To send a message asynchronously, use <link
linkend="soup-session-queue-message"><function>soup_session_queue_message</function></link>:
</para>

<informalexample><programlisting>
	...
	soup_session_queue_message (session, msg, my_callback, my_callback_data);
	...
}

static void
my_callback (SoupSession *session, SoupMessage *msg, gpointer user_data)
{
	/* Handle the response here */
}
</programlisting></informalexample>

<para>
The message will be added to the session's queue, and eventually (when
control is returned back to the main loop), it will be sent and the
response be will be read. When the message is complete,
<literal>callback</literal> will be invoked, along with the data you
passed to <function>soup_session_queue_message</function>.
</para>

<para>
<link
linkend="soup-session-queue-message"><function>soup_session_queue_message</function></link>
steals a reference to the message object, and unrefs it after the last
callback is invoked on it. So in the usual case, messages sent
asynchronously will be automatically freed for you without you needing
to do anything. (Of course, this wouldn't work when using the synchronous
API, since you will usually need continue working with the message
after calling <link
linkend="soup-session-send-message"><function>soup_session_send_message</function></link>,
so in that case, you must unref it explicitly when you are done with
it.)
</para>

<para>
(If you use <link
linkend="soup-session-queue-message"><function>soup_session_queue_message</function></link>
with a <link
linkend="SoupSessionSync"><type>SoupSessionSync</type></link>, the
message will be sent in another thread, with the callback eventually
being invoked in the session's <link linkend="SOUP-SESSION-ASYNC-CONTEXT:CAPS"><literal>SOUP_SESSION_ASYNC_CONTEXT</literal></link>.)
</para>

</refsect3>

</refsect2>

<refsect2>
<title>Processing the Response</title>

<para>
Once you have received the response from the server, synchronously or
asynchronously, you can look at the response fields in the
<literal>SoupMessage</literal> to decide what to do next. The
<structfield>status_code</structfield> and
<structfield>reason_phrase</structfield> fields contain the numeric
status and textual status response from the server.
<structfield>response_headers</structfield> contains the response
headers, which you can investigate using <link
linkend="soup-message-headers-get"><function>soup_message_headers_get</function></link> and
<link
    linkend="soup-message-headers-foreach"><function>soup_message_headers_foreach</function></link>.
The response body (if any) is in the
<structfield>response_body</structfield> field.
</para>

<para>
<link
linkend="SoupMessageHeaders"><type>SoupMessageHeaders</type></link>
automatically parses several important headers in
<structfield>response_headers</structfield> for you and provides
specialized accessors for them. Eg, <link
linkend="soup-message-headers-get-content-type"><function>soup_message_headers_get_content_type</function></link>.
There are several generic methods such as <link
linkend="soup-header-parse-param-list"><function>soup_header_parse_param_list</function></link>
(for parsing an attribute-list-type header) and <link
linkend="soup-header-contains"><function>soup_header_contains</function></link>
(for quickly testing if a list-type header contains a particular
token). These handle the various syntactical oddities of parsing HTTP
headers much better than functions like
<function>g_strsplit</function> or <function>strstr</function>.
</para>

</refsect2>

<refsect2>
<title>Intermediate/Automatic Processing</title>

<para>
You can also connect to various <type>SoupMessage</type> signals to do
processing at intermediate stages of HTTP I/O. Eg, the <link
linkend="SoupMessage-got-chunk"><literal>got-chunk</literal></link>
signal is emitted as each piece of the response body is read (allowing
you to provide progress information when receiving a large response,
for example). <type>SoupMessage</type> also provides two convenience
methods, <link
linkend="soup-message-add-header-handler"><function>soup_message_add_header_handler</function></link>,
and <link
linkend="soup-message-add-status-code-handler"><function>soup_message_add_status_code_handler</function></link>,
which allow you to set up a signal handler that will only be invoked
for messages with certain response headers or status codes.
<type>SoupSession</type> uses this internally to handle authentication
and redirection.
</para>

<para>
When using the synchronous API, the callbacks and signal handlers will
be invoked during the call to <link
linkend="soup-session-send-message"><function>soup_session_send_message</function></link>.
</para>

<para>
To automatically set up handlers on all messages sent via a session,
you can connect to the session's <link
linkend="SoupSession-request-started"><literal>request_started</literal></link>
signal, and add handlers to each message from there.
</para>

</refsect2>

<refsect2>
<title>Handling Authentication</title>

<para>
<type>SoupSession</type> handles most of the details of HTTP
authentication for you. If it receives a 401 ("Unauthorized") or 407
("Proxy Authentication Required") response, the session will emit the
<link linkend="SoupSession-authenticate">authenticate</link> signal,
providing you with a <link
linkend="SoupAuth"><type>SoupAuth</type></link> object indicating the
authentication type ("Basic", "Digest", or "NTLM") and the realm name
provided by the server. If you have a username and password available
(or can generate one), call <link
linkend="soup-auth-authenticate"><function>soup_auth_authenticate</function></link>
to give the information to libsoup. The session will automatically
requeue the message and try it again with that authentication
information. (If you don't call
<function>soup_auth_authenticate</function>, the session will just
return the message to the application with its 401 or 407 status.)
</para>

<para>
If the server doesn't accept the username and password provided, the
session will emit <link
linkend="SoupSession-authenticate">authenticate</link> again, with the
<literal>retrying</literal> parameter set to <literal>TRUE</literal>. This lets the
application know that the information it provided earlier was
incorrect, and gives it a chance to try again. If this
username/password pair also doesn't work, the session will contine to
emit <literal>authenticate</literal> again and again until the
provided username/password successfully authenticates, or until the
signal handler fails to call <link
linkend="soup-auth-authenticate"><function>soup_auth_authenticate</function></link>,
at which point <application>libsoup</application> will allow the
message to fail (with status 401 or 407).
</para>

<para>
If you need to handle authentication asynchronously (eg, to pop up a
password dialog without recursively entering the main loop), you can
do that as well. Just call <link
linkend="soup-session-pause-message"><function>soup_session_pause_message</function></link>
on the message before returning from the signal handler, and
<function>g_object_ref</function> the <type>SoupAuth</type>. Then,
later on, after calling <function>soup_auth_authenticate</function>
(or deciding not to), call <link
linkend="soup-session-unpause-message"><function>soup_session_unpause_message</function></link>
to resume the paused message.
</para>

</refsect2>

<refsect2>
<title>Multi-threaded usage</title>

<para>
The only explicitly thread-safe operations in
<application>libsoup</application> are <link
linkend="SoupSessionSync"><type>SoupSessionSync</type></link>'s
implementations of the <link
linkend="SoupSession"><type>SoupSession</type></link> methods. So
after creating a <type>SoupSessionSync</type>, you can call <link
linkend="soup-session-send-message"><function>soup_session_send_message</function></link>
and <link
linkend="soup-session-cancel-message"><function>soup_session_cancel_message</function></link>
on it from any thread. But, eg, while the session is processing a
message, you should not call any <link
linkend="SoupMessage"><type>SoupMessage</type></link> methods on it
from any thread other than the one in which it is being sent. (That
is, you should not call any <type>SoupMessage</type> methods on it
except from a message or session callback or signal handler.)
</para>

<para>
All other objects (including <link
linkend="SoupSessionAsync"><type>SoupSessionAsync</type></link>)
should only be used from a single thread, with objects that are also
only be used from that thread. (And in particular, if you set a
non-default <link
linkend="GMainContext"><type>GMainContext</type></link> on a session,
socket, etc, then you can only use that object from the thread in
which that <type>GMainContext</type> is running.)
</para>

</refsect2>

<refsect2>
<title>Sample Programs</title>

<para>
A few sample programs are available in the
<application>libsoup</application> sources:
</para>

<itemizedlist>
    <listitem><para>
	<emphasis role="bold"><literal>get</literal></emphasis> is a simple command-line
	HTTP GET utility using the asynchronous API.
    </para></listitem>

    <listitem><para>
	<emphasis role="bold"><literal>getbug</literal></emphasis> is a trivial
	demonstration of the <link
	linkend="libsoup-2.4-XMLRPC-Support">XMLRPC</link> interface.
	(<emphasis
	role="bold"><literal>xmlrpc-test</literal></emphasis> provides
	a slightly more complicated example.)
    </para></listitem>

    <listitem><para>
	<emphasis role="bold"><literal>auth-test</literal></emphasis> shows how to use
	authentication handlers and status-code handlers, although in
	a fairly unusual way.
    </para></listitem>

    <listitem><para>
	<emphasis role="bold"><literal>simple-proxy</literal></emphasis> uses both the
	client and server APIs to create a simple (and not very
	RFC-compliant) proxy server. It shows how to use the <link
	linkend="SoupMessageFlags"><literal>SOUP_MESSAGE_OVERWRITE_CHUNKS</literal></link>
	flag when reading a message to save memory by processing each
	chunk of the message as it is read, rather than accumulating
	them all into a single buffer to process all at the end.
    </para></listitem>
</itemizedlist>

<para>
More complicated examples are available in GNOME CVS. The <ulink
url="http://live.gnome.org/LibSoup"><application>libsoup</application>
pages</ulink> on the GNOME wiki include a <ulink
url="http://live.gnome.org/LibSoup/Users">list of applications using
<application>libsoup</application></ulink>.
</para>

</refsect2>

</refentry>