summaryrefslogtreecommitdiff
path: root/libgupnp/http-headers.c
blob: ab0d5b1132aed63ccd1f4e939aa3869a1e99317d (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
/*
 * Copyright (C) 2007, 2008 OpenedHand Ltd.
 *
 * Author: Jorn Baayen <jorn@openedhand.com>
 *
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 */

#include <config.h>

#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>

#include <libsoup/soup.h>

#include "http-headers.h"

/* Converts @lang from HTTP language tag format into locale format.
 * Return value: The index of the '-' character. */
static int
http_language_from_locale (char *lang)
{
        gboolean tolower;
        int i, dash_index;

        tolower = FALSE;
        dash_index = -1;

        for (i = 0; lang[i] != '\0'; i++) {
                switch (lang[i]) {
                case '_':
                        /* Underscores to dashes */
                        lang[i] = '-';

                        /* Lowercase country bit */
                        tolower = TRUE;

                        /* Save dash index */
                        dash_index = i;

                        break;
                case '.':
                case '@':
                        /* Terminate our string here */
                        lang[i] = '\0';

                        return dash_index;
                default:
                        if (tolower)
                                lang[i] = g_ascii_tolower (lang[i]);

                        break;
                }
        }

        return dash_index;
}

/* Converts @lang from locale format into HTTP language tag format.
 * Return value: The index of the '_' character. */
static int
locale_from_http_language (char *lang)
{
        gboolean toupper;
        int i, underscore_index;

        toupper = FALSE;
        underscore_index = -1;

        for (i = 0; lang[i] != '\0'; i++) {
                switch (lang[i]) {
                case '-':
                        /* Dashes to underscores */
                        lang[i] = '_';

                        /* Uppercase country bit */
                        toupper = TRUE;

                        /* Save underscore index */
                        underscore_index = i;

                        break;
                case ';':
                        /* Terminate our string here */
                        lang[i] = '\0';

                        return underscore_index;
                default:
                        if (toupper)
                                lang[i] = g_ascii_toupper (lang[i]);

                        break;
                }
        }

        return underscore_index;
}

/* Sets the Accept-Language on @message with the language taken from the
 * current locale. */
void
http_request_set_accept_language (SoupMessage *message)
{
#ifdef G_OS_WIN32
        /* TODO: Use GetSystemDefaultLangID or similar */
        return;
#else
        char *locale, *lang;
        int dash_index;
        GString *tmp;


        locale = setlocale (LC_MESSAGES, NULL);

        if (locale == NULL)
                return;

        if (strcmp (locale, "C") == 0)
                return;

        lang = g_strdup (locale);

        dash_index = http_language_from_locale (lang);

        tmp = g_string_new (lang);
        g_string_append (tmp, ";q=1");

        /* Append preference for basic (non-country specific) language version
         * if applicable */
        if (dash_index > 0) {
                g_string_append (tmp, ", ");

                lang[dash_index] = '\0';
                g_string_append (tmp, lang);
                g_string_append (tmp, ";q=0.5");
        }

        g_free (lang);

        SoupMessageHeaders *request_headers =
                soup_message_get_request_headers (message);

        soup_message_headers_append (request_headers,
                                     "Accept-Language",
                                     tmp->str);

        g_string_free (tmp, TRUE);
#endif
}

static double
get_quality (const char *val)
{
        val = strstr (val, ";q=");
        if (!val)
                return 1;

        val += strlen (";q=");
        return atof (val);
}

static int
sort_locales_by_quality (const char *a,
                         const char *b)
{
        const double diff = get_quality (a) - get_quality (b);

        if (diff == 0.0)
                return 0;
        else if (diff > 0)
                return -1;

        return 1;
}

/* Parses the Accept-Language header in @message, and returns its values
 * in an ordered list in UNIX locale format */
GList *
http_request_get_accept_locales (SoupMessageHeaders *request_headers)
{
        const char *header;
        char **bits;
        int i;
        GList *locales;

        header = soup_message_headers_get_one (request_headers,
                                               "Accept-Language");
        if (header == NULL)
                return NULL;

        locales = NULL;

        bits = g_strsplit (header, ",", -1);

        /* Transform to list */
        for (i = 0; bits[i] != NULL; i++) {
                bits[i] = g_strstrip (bits[i]);

                switch (bits[i][0]) {
                case '\0':
                        /* Empty */
                case '*':
                        /* Wildcard: ignore */
                        g_free (bits[i]);

                        break;
                default:
                        locale_from_http_language (bits[i]);

                        /* Because bits is sorted in ascending order */
                        locales = g_list_prepend (locales, bits[i]);

                        break;
                }
        }

        g_free (bits);

        locales = g_list_sort (locales, (GCompareFunc) sort_locales_by_quality);

        return locales;
}

/* Set Accept-Language header according to @locale. */
void
http_response_set_content_locale (SoupMessageHeaders *response_headers,
                                  const char *locale)
{
        char *lang;

        lang = g_strdup (locale);
        http_language_from_locale (lang);

        soup_message_headers_append (response_headers,
                                     "Content-Language",
                                     lang);

        g_free (lang);
}

/* Set Content-Type header guessed from @path, @data and @data_size using
 * g_content_type_guess(). */
void
http_response_set_content_type (SoupMessageHeaders *response_headers,
                                const char *path,
                                const guchar *data,
                                gsize data_size)
{
        char *content_type, *mime;

        content_type = g_content_type_guess
                                (path,
                                 data,
                                 data_size,
                                 NULL);
        mime = g_content_type_get_mime_type (content_type);
        if (mime == NULL)
                mime = g_strdup ("application/octet-stream");
        else if (strcmp (mime, "application/xml") == 0) {
                g_free (mime);
                mime = g_strdup ("text/xml; charset=\"utf-8\"");
        }

        soup_message_headers_append (response_headers, "Content-Type", mime);

        g_free (mime);
        g_free (content_type);
}

/* Set Content-Encoding header to gzip and append compressed body */
void
http_response_set_body_gzip (SoupServerMessage *msg,
                             const char *body,
                             const gsize length)
{
        GZlibCompressor *compressor;
        gboolean finished = FALSE;
        gsize converted = 0;

        SoupMessageBody *message_body =
                soup_server_message_get_response_body (msg);
        SoupMessageHeaders *response_headers =
                soup_server_message_get_response_headers (msg);

        soup_message_headers_append (response_headers,
                                     "Content-Encoding",
                                     "gzip");

        compressor = g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP, -1);

        while (! finished) {
                GError *error = NULL;
                char buf[65536];
                gsize bytes_read = 0;
                gsize bytes_written = 0;

                switch (g_converter_convert (G_CONVERTER (compressor),
                                             body + converted,
                                             length - converted,
                                             buf, sizeof (buf),
                                             G_CONVERTER_INPUT_AT_END,
                                             &bytes_read, &bytes_written,
                                             &error)) {
                case G_CONVERTER_ERROR:
                        g_warning ("Error compressing response: %s",
                                   error->message);
                        g_error_free (error);
                        g_object_unref (compressor);
                        return;
                case G_CONVERTER_CONVERTED:
                        converted += bytes_read;
                        break;
                case G_CONVERTER_FINISHED:
                        finished = TRUE;
                        break;
                case G_CONVERTER_FLUSHED:
                        break;
                default:
                        break;
                }

                if (bytes_written)
                        soup_message_body_append (message_body,
                                                  SOUP_MEMORY_COPY,
                                                  buf,
                                                  bytes_written);
        }

        g_object_unref (compressor);
}