![]() |
![]() |
![]() |
libsoup Reference Manual | ![]() |
---|
Soup Client BasicsSoup Client Basics — Client-side tutorial |
The first step in using the client API is to create a SoupSession. The session object encapsulates all of the state that libsoup is keeping on behalf of your program; cached HTTP connections, authentication information, etc.
There are two subclasses of SoupSession that you can use, with slightly different behavior:
SoupSessionAsync, which uses callbacks and the glib main loop to provide asynchronous I/O.
SoupSessionSync, which uses blocking I/O rather than callbacks, making it more suitable for threaded applications.
If you want to do a mix of mainloop-based and blocking I/O, you will need to create two different session objects.
When you create the session (with soup_session_async_new_with_options
or soup_session_sync_new_with_options
),
you can specify various additional options:
Tells the session to use an HTTP proxy rather than directly connecting to HTTP servers. |
|
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.) |
|
Allows you to set the maximum total number of connections the session will have open to a single host at one time. |
|
If |
|
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.) |
|
A GMainContext which the session will use for asynchronous operations. This can be set if you want to use a SoupSessionAsync in a thread other than the main thread. |
If you don't need to specify any options, you can just use soup_session_async_new
or
soup_session_sync_new
,
which take no arguments.
Once you have a session, you do HTTP traffic using SoupMessage. In the simplest case, you only need to create the message and it's ready to send:
SoupMessage *msg; msg = soup_message_new ("GET", "http://example.com/");
In more complicated cases, you can use various SoupMessage, SoupMessageHeaders, and SoupMessageBody methods to set the request headers and body of the message:
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);
You can also use soup_message_set_flags
to change some default behaviors. For example, by default,
SoupSession automatically handles responses from the
server that redirect to another URL. If you would like to handle these
yourself, you can set the SOUP_MESSAGE_NO_REDIRECT
flag.
To send a message and wait for the response, use soup_session_send_message
:
guint status; status = soup_session_send (session, msg);
(If you use soup_session_send_message
with a
SoupSessionAsync,
it will run the main loop itself until the message is complete.)
The return value from soup_session_send
is a
soup status code, 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
msg->status_code
.)
To send a message asynchronously, use soup_session_queue_message
:
... 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 */ }
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,
callback
will be invoked, along with the data you
passed to soup_session_queue_message
.
(If you use soup_session_queue_message
with a SoupSessionSync, the
message will be sent in another thread, with the callback eventually
being invoked in the session's SOUP_SESSION_ASYNC_CONTEXT
.)
Once you have received the response from the server, synchronously or
asynchronously, you can look at the response fields in the
SoupMessage
to decide what to do next. The
status_code
and
reason_phrase
fields contain the numeric
status and textual status response from the server.
response_headers
contains the response
headers, which you can investigate using soup_message_headers_get
and
soup_message_headers_foreach
.
The response body (if any) is in the
response_body
field.
If you send the message with soup_session_queue_message
,
libsoup will steal a reference to the
message object, and unref the message after the last callback is
invoked on it. So in the usual case, messages will be automatically
freed for you without you needing to do anything. Of course, this
won't work when using the synchronous API, since you will usually need
continue working with the message after calling soup_session_send_message
,
so in that case, you must unref it explicitly when you are done with
it.
You can also connect to various SoupMessage
signals
to do processing at intermediate stages of HTTP I/O.
SoupMessage
also provides two convenience methods,
soup_message_add_header_handler
,
and soup_message_add_status_code_handler
,
which allow you to set up a signal handler that will only be invoked
for messages with certain response headers or status codes.
SoupSession uses this internally to handle authentication
and redirection.
When using the synchronous API, the callbacks and signal handlers will
be invoked during the call to soup_session_send_message
.
To automatically set up handlers on all messages sent via a session,
you can connect to the session's request_started
signal, and add handlers to each message from there.
SoupSession 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
authenticate signal,
providing you with a SoupAuth 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 soup_auth_authenticate
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
soup_auth_authenticate
, the session will just
return the message to the application with its 401 or 407 status.)
If the server doesn't accept the username and password provided, the
session will emit authenticate again, with the
retrying
parameter set to TRUE
. 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 authenticate
again and again until the
provided username/password successfully authenticates, or until the
signal handler fails to call soup_auth_authenticate
,
at which point libsoup will allow the
message to fail (with status 401 or 407).
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 soup_session_pause_message
on the message before returning from the signal handler, and
g_object_ref
the SoupAuth. Then,
later on, after calling soup_auth_authenticate
(or deciding not to), call soup_session_unpause_message
to resume the paused message.
A few sample programs are available in the libsoup sources:
get
is a simple command-line
HTTP GET utility using the asynchronous API.
getbug
is a trivial
demonstration of the XMLRPC interface.
(xmlrpc-test
provides
a slightly more complicated example.)
auth-test
shows how to use
authentication handlers and status-code handlers, although in
a fairly unusual way.
simple-proxy
uses both the
client and server APIs to create a simple (and not very
RFC-compliant) proxy server. It shows how to use the SOUP_MESSAGE_OVERWRITE_CHUNKS
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.
More complicated examples are available in GNOME CVS. The libsoup pages on the GNOME wiki include a list of applications using libsoup.