1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * soup-content-decoder.c
5 * Copyright (C) 2009 Red Hat, Inc.
15 #include "soup-content-decoder.h"
16 #include "soup-enum-types.h"
17 #include "soup-message.h"
18 #include "soup-message-private.h"
19 #include "soup-session-feature.h"
23 * SECTION:soup-content-decoder
24 * @short_description: Content-Encoding handler
26 * #SoupContentDecoder handles the "Accept-Encoding" header on
27 * outgoing messages, and the "Content-Encoding" header on incoming
28 * ones. If you add it to a session with soup_session_add_feature() or
29 * soup_session_add_feature_by_type(), the session will automatically
30 * use Content-Encoding as appropriate.
32 * (Note that currently there is no way to (automatically) use
33 * Content-Encoding when sending a request body, or to pick specific
34 * encoding types to support.)
36 * If #SoupContentDecoder successfully decodes the Content-Encoding,
37 * it will set the %SOUP_MESSAGE_CONTENT_DECODED flag on the message,
38 * and the message body and the chunks in the #SoupMessage::got_chunk
39 * signals will contain the decoded data; however, the message headers
40 * will be unchanged (and so "Content-Encoding" will still be present,
41 * "Content-Length" will describe the original encoded length, etc).
43 * If "Content-Encoding" contains any encoding types that
44 * #SoupContentDecoder doesn't recognize, then none of the encodings
45 * will be decoded (and the %SOUP_MESSAGE_CONTENT_DECODED flag will
51 struct _SoupContentDecoderPrivate {
55 typedef GConverter * (*SoupContentDecoderCreator) (void);
57 static void soup_content_decoder_session_feature_init (SoupSessionFeatureInterface *feature_interface, gpointer interface_data);
59 static void request_queued (SoupSessionFeature *feature, SoupSession *session, SoupMessage *msg);
60 static void request_unqueued (SoupSessionFeature *feature, SoupSession *session, SoupMessage *msg);
62 static void finalize (GObject *object);
64 G_DEFINE_TYPE_WITH_CODE (SoupContentDecoder, soup_content_decoder, G_TYPE_OBJECT,
65 G_IMPLEMENT_INTERFACE (SOUP_TYPE_SESSION_FEATURE,
66 soup_content_decoder_session_feature_init))
68 /* This is constant for now */
69 #define ACCEPT_ENCODING_HEADER "gzip, deflate"
72 gzip_decoder_creator (void)
74 return (GConverter *)g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP);
78 zlib_decoder_creator (void)
80 return (GConverter *)g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
84 soup_content_decoder_init (SoupContentDecoder *decoder)
86 decoder->priv = G_TYPE_INSTANCE_GET_PRIVATE (decoder,
87 SOUP_TYPE_CONTENT_DECODER,
88 SoupContentDecoderPrivate);
90 decoder->priv->decoders = g_hash_table_new (g_str_hash, g_str_equal);
91 /* Hardcoded for now */
92 g_hash_table_insert (decoder->priv->decoders, "gzip",
93 gzip_decoder_creator);
94 g_hash_table_insert (decoder->priv->decoders, "x-gzip",
95 gzip_decoder_creator);
96 g_hash_table_insert (decoder->priv->decoders, "deflate",
97 zlib_decoder_creator);
101 soup_content_decoder_class_init (SoupContentDecoderClass *decoder_class)
103 GObjectClass *object_class = G_OBJECT_CLASS (decoder_class);
105 g_type_class_add_private (decoder_class, sizeof (SoupContentDecoderPrivate));
107 object_class->finalize = finalize;
111 soup_content_decoder_session_feature_init (SoupSessionFeatureInterface *feature_interface,
112 gpointer interface_data)
114 feature_interface->request_queued = request_queued;
115 feature_interface->request_unqueued = request_unqueued;
119 finalize (GObject *object)
121 SoupContentDecoder *decoder = SOUP_CONTENT_DECODER (object);
123 g_hash_table_destroy (decoder->priv->decoders);
125 G_OBJECT_CLASS (soup_content_decoder_parent_class)->finalize (object);
129 soup_content_decoder_got_headers_cb (SoupMessage *msg, SoupContentDecoder *decoder)
131 SoupMessagePrivate *msgpriv = SOUP_MESSAGE_GET_PRIVATE (msg);
133 GSList *encodings, *e;
134 SoupContentDecoderCreator converter_creator;
135 GConverter *converter;
137 header = soup_message_headers_get_list (msg->response_headers,
142 /* Workaround for an apache bug (bgo 613361) */
143 if (!g_ascii_strcasecmp (header, "gzip") ||
144 !g_ascii_strcasecmp (header, "x-gzip")) {
145 const char *content_type = soup_message_headers_get_content_type (msg->response_headers, NULL);
148 (!g_ascii_strcasecmp (content_type, "application/gzip") ||
149 !g_ascii_strcasecmp (content_type, "application/x-gzip")))
153 /* OK, really, no one is ever going to use more than one
154 * encoding, but we'll be robust.
156 encodings = soup_header_parse_list (header);
160 for (e = encodings; e; e = e->next) {
161 if (!g_hash_table_lookup (decoder->priv->decoders, e->data)) {
162 soup_header_free_list (encodings);
167 /* msgpriv->decoders should be empty at this point anyway, but
168 * clean it up if it's not.
170 while (msgpriv->decoders) {
171 g_object_unref (msgpriv->decoders->data);
172 msgpriv->decoders = g_slist_delete_link (msgpriv->decoders, msgpriv->decoders);
175 for (e = encodings; e; e = e->next) {
176 converter_creator = g_hash_table_lookup (decoder->priv->decoders, e->data);
177 converter = converter_creator ();
179 /* Content-Encoding lists the codings in the order
180 * they were applied in, so we put decoders in reverse
181 * order so the last-applied will be the first
184 msgpriv->decoders = g_slist_prepend (msgpriv->decoders, converter);
186 soup_header_free_list (encodings);
190 request_queued (SoupSessionFeature *feature, SoupSession *session,
193 SoupContentDecoder *decoder = SOUP_CONTENT_DECODER (feature);
195 if (!soup_message_headers_get_one (msg->request_headers,
196 "Accept-Encoding")) {
197 soup_message_headers_append (msg->request_headers,
199 ACCEPT_ENCODING_HEADER);
202 g_signal_connect (msg, "got-headers",
203 G_CALLBACK (soup_content_decoder_got_headers_cb),
208 request_unqueued (SoupSessionFeature *feature, SoupSession *session,
211 g_signal_handlers_disconnect_by_func (msg, soup_content_decoder_got_headers_cb, feature);