soup-auth-manager: add soup_auth_manager_use_auth()
[platform/upstream/libsoup.git] / libsoup / soup-status.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * soup-status.c: Status code descriptions
4  *
5  * Copyright (C) 2001-2003, Ximian, Inc.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include "soup-status.h"
13 #include "soup.h"
14
15 /**
16  * SECTION:soup-status
17  * @short_description: HTTP (and libsoup) status codes
18  *
19  **/
20
21 /**
22  * SOUP_STATUS_IS_TRANSPORT_ERROR:
23  * @status: a status code
24  *
25  * Tests if @status is a libsoup transport error.
26  *
27  * Return value: %TRUE or %FALSE
28  **/
29 /**
30  * SOUP_STATUS_IS_INFORMATIONAL:
31  * @status: an HTTP status code
32  *
33  * Tests if @status is an Informational (1xx) response.
34  *
35  * Return value: %TRUE or %FALSE
36  **/
37 /**
38  * SOUP_STATUS_IS_SUCCESSFUL:
39  * @status: an HTTP status code
40  *
41  * Tests if @status is a Successful (2xx) response.
42  *
43  * Return value: %TRUE or %FALSE
44  **/
45 /**
46  * SOUP_STATUS_IS_REDIRECTION:
47  * @status: an HTTP status code
48  *
49  * Tests if @status is a Redirection (3xx) response.
50  *
51  * Return value: %TRUE or %FALSE
52  **/
53 /**
54  * SOUP_STATUS_IS_CLIENT_ERROR:
55  * @status: an HTTP status code
56  *
57  * Tests if @status is a Client Error (4xx) response.
58  *
59  * Return value: %TRUE or %FALSE
60  **/
61 /**
62  * SOUP_STATUS_IS_SERVER_ERROR:
63  * @status: an HTTP status code
64  *
65  * Tests if @status is a Server Error (5xx) response.
66  *
67  * Return value: %TRUE or %FALSE
68  **/
69
70 /**
71  * SoupKnownStatusCode:
72  * @SOUP_STATUS_NONE: No status available. (Eg, the message has not
73  * been sent yet)
74  * @SOUP_STATUS_CANCELLED: Message was cancelled locally
75  * @SOUP_STATUS_CANT_RESOLVE: Unable to resolve destination host name
76  * @SOUP_STATUS_CANT_RESOLVE_PROXY: Unable to resolve proxy host name
77  * @SOUP_STATUS_CANT_CONNECT: Unable to connect to remote host
78  * @SOUP_STATUS_CANT_CONNECT_PROXY: Unable to connect to proxy
79  * @SOUP_STATUS_SSL_FAILED: SSL/TLS negotiation failed
80  * @SOUP_STATUS_IO_ERROR: A network error occurred, or the other end
81  * closed the connection unexpectedly
82  * @SOUP_STATUS_MALFORMED: Malformed data (usually a programmer error)
83  * @SOUP_STATUS_TRY_AGAIN: Used internally
84  * @SOUP_STATUS_TOO_MANY_REDIRECTS: There were too many redirections
85  * @SOUP_STATUS_TLS_FAILED: Used internally
86  * @SOUP_STATUS_CONTINUE: 100 Continue (HTTP)
87  * @SOUP_STATUS_SWITCHING_PROTOCOLS: 101 Switching Protocols (HTTP)
88  * @SOUP_STATUS_PROCESSING: 102 Processing (WebDAV)
89  * @SOUP_STATUS_OK: 200 Success (HTTP). Also used by many lower-level
90  * soup routines to indicate success.
91  * @SOUP_STATUS_CREATED: 201 Created (HTTP)
92  * @SOUP_STATUS_ACCEPTED: 202 Accepted (HTTP)
93  * @SOUP_STATUS_NON_AUTHORITATIVE: 203 Non-Authoritative Information
94  * (HTTP)
95  * @SOUP_STATUS_NO_CONTENT: 204 No Content (HTTP)
96  * @SOUP_STATUS_RESET_CONTENT: 205 Reset Content (HTTP)
97  * @SOUP_STATUS_PARTIAL_CONTENT: 206 Partial Content (HTTP)
98  * @SOUP_STATUS_MULTI_STATUS: 207 Multi-Status (WebDAV)
99  * @SOUP_STATUS_MULTIPLE_CHOICES: 300 Multiple Choices (HTTP)
100  * @SOUP_STATUS_MOVED_PERMANENTLY: 301 Moved Permanently (HTTP)
101  * @SOUP_STATUS_FOUND: 302 Found (HTTP)
102  * @SOUP_STATUS_MOVED_TEMPORARILY: 302 Moved Temporarily (old name,
103  * RFC 2068)
104  * @SOUP_STATUS_SEE_OTHER: 303 See Other (HTTP)
105  * @SOUP_STATUS_NOT_MODIFIED: 304 Not Modified (HTTP)
106  * @SOUP_STATUS_USE_PROXY: 305 Use Proxy (HTTP)
107  * @SOUP_STATUS_NOT_APPEARING_IN_THIS_PROTOCOL: 306 [Unused] (HTTP)
108  * @SOUP_STATUS_TEMPORARY_REDIRECT: 307 Temporary Redirect (HTTP)
109  * @SOUP_STATUS_BAD_REQUEST: 400 Bad Request (HTTP)
110  * @SOUP_STATUS_UNAUTHORIZED: 401 Unauthorized (HTTP)
111  * @SOUP_STATUS_PAYMENT_REQUIRED: 402 Payment Required (HTTP)
112  * @SOUP_STATUS_FORBIDDEN: 403 Forbidden (HTTP)
113  * @SOUP_STATUS_NOT_FOUND: 404 Not Found (HTTP)
114  * @SOUP_STATUS_METHOD_NOT_ALLOWED: 405 Method Not Allowed (HTTP)
115  * @SOUP_STATUS_NOT_ACCEPTABLE: 406 Not Acceptable (HTTP)
116  * @SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED: 407 Proxy Authentication
117  * Required (HTTP)
118  * @SOUP_STATUS_PROXY_UNAUTHORIZED: shorter alias for
119  * %SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED
120  * @SOUP_STATUS_REQUEST_TIMEOUT: 408 Request Timeout (HTTP)
121  * @SOUP_STATUS_CONFLICT: 409 Conflict (HTTP)
122  * @SOUP_STATUS_GONE: 410 Gone (HTTP)
123  * @SOUP_STATUS_LENGTH_REQUIRED: 411 Length Required (HTTP)
124  * @SOUP_STATUS_PRECONDITION_FAILED: 412 Precondition Failed (HTTP)
125  * @SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE: 413 Request Entity Too Large
126  * (HTTP)
127  * @SOUP_STATUS_REQUEST_URI_TOO_LONG: 414 Request-URI Too Long (HTTP)
128  * @SOUP_STATUS_UNSUPPORTED_MEDIA_TYPE: 415 Unsupported Media Type
129  * (HTTP)
130  * @SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE: 416 Requested Range
131  * Not Satisfiable (HTTP)
132  * @SOUP_STATUS_INVALID_RANGE: shorter alias for
133  * %SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE
134  * @SOUP_STATUS_EXPECTATION_FAILED: 417 Expectation Failed (HTTP)
135  * @SOUP_STATUS_UNPROCESSABLE_ENTITY: 422 Unprocessable Entity
136  * (WebDAV)
137  * @SOUP_STATUS_LOCKED: 423 Locked (WebDAV)
138  * @SOUP_STATUS_FAILED_DEPENDENCY: 424 Failed Dependency (WebDAV)
139  * @SOUP_STATUS_INTERNAL_SERVER_ERROR: 500 Internal Server Error
140  * (HTTP)
141  * @SOUP_STATUS_NOT_IMPLEMENTED: 501 Not Implemented (HTTP)
142  * @SOUP_STATUS_BAD_GATEWAY: 502 Bad Gateway (HTTP)
143  * @SOUP_STATUS_SERVICE_UNAVAILABLE: 503 Service Unavailable (HTTP)
144  * @SOUP_STATUS_GATEWAY_TIMEOUT: 504 Gateway Timeout (HTTP)
145  * @SOUP_STATUS_HTTP_VERSION_NOT_SUPPORTED: 505 HTTP Version Not
146  * Supported (HTTP)
147  * @SOUP_STATUS_INSUFFICIENT_STORAGE: 507 Insufficient Storage
148  * (WebDAV)
149  * @SOUP_STATUS_NOT_EXTENDED: 510 Not Extended (RFC 2774)
150  * 
151  * These represent the known HTTP status code values, plus various
152  * network and internal errors.
153  **/
154
155 /**
156  * SOUP_HTTP_ERROR:
157  *
158  * A #GError domain representing an HTTP status. Use a
159  * #SoupKnownStatusCode for the <structfield>code</structfield>
160  * value.
161  **/
162
163
164 /* The reason_phrases are not localized because:
165  *
166  * 1. Only ASCII can be used portably in the HTTP Status-Line, so we
167  *    would not be able to return localized reason phrases from
168  *    SoupServer anyway.
169  *
170  * 2. Having a way for clients to get a localized version of a status
171  *    code would just encourage them to present those strings to the
172  *    user, which is bad because many of them are fairly
173  *    incomprehensible anyway.
174  */
175
176 static const struct {
177         guint code;
178         const char *phrase;
179 } reason_phrases [] = {
180         /* Transport errors */
181         { SOUP_STATUS_CANCELLED,                  "Cancelled" },
182         { SOUP_STATUS_CANT_RESOLVE,               "Cannot resolve hostname" },
183         { SOUP_STATUS_CANT_RESOLVE_PROXY,         "Cannot resolve proxy hostname" },
184         { SOUP_STATUS_CANT_CONNECT,               "Cannot connect to destination" },
185         { SOUP_STATUS_CANT_CONNECT_PROXY,         "Cannot connect to proxy" },
186         { SOUP_STATUS_SSL_FAILED,                 "SSL handshake failed" },
187         { SOUP_STATUS_IO_ERROR,                   "Connection terminated unexpectedly" },
188         { SOUP_STATUS_MALFORMED,                  "Message Corrupt" },
189         { SOUP_STATUS_TOO_MANY_REDIRECTS,         "Too many redirects" },
190
191         /* Informational */
192         { SOUP_STATUS_CONTINUE,                   "Continue" },
193         { SOUP_STATUS_SWITCHING_PROTOCOLS,        "Switching Protocols" },
194         { SOUP_STATUS_PROCESSING,                 "Processing" },
195
196         /* Success */
197         { SOUP_STATUS_OK,                         "OK" },
198         { SOUP_STATUS_CREATED,                    "Created" },
199         { SOUP_STATUS_ACCEPTED,                   "Accepted" },
200         { SOUP_STATUS_NON_AUTHORITATIVE,          "Non-Authoritative Information" },
201         { SOUP_STATUS_NO_CONTENT,                 "No Content" },
202         { SOUP_STATUS_RESET_CONTENT,              "Reset Content" },
203         { SOUP_STATUS_PARTIAL_CONTENT,            "Partial Content" },
204         { SOUP_STATUS_MULTI_STATUS,               "Multi-Status" },
205
206         /* Redirection */
207         { SOUP_STATUS_MULTIPLE_CHOICES,           "Multiple Choices" },
208         { SOUP_STATUS_MOVED_PERMANENTLY,          "Moved Permanently" },
209         { SOUP_STATUS_FOUND,                      "Found" },
210         { SOUP_STATUS_SEE_OTHER,                  "See Other" },
211         { SOUP_STATUS_NOT_MODIFIED,               "Not Modified" },
212         { SOUP_STATUS_USE_PROXY,                  "Use Proxy" },
213         { SOUP_STATUS_TEMPORARY_REDIRECT,         "Temporary Redirect" },
214
215         /* Client error */
216         { SOUP_STATUS_BAD_REQUEST,                "Bad Request" },
217         { SOUP_STATUS_UNAUTHORIZED,               "Unauthorized" },
218         { SOUP_STATUS_PAYMENT_REQUIRED,           "Payment Required" },
219         { SOUP_STATUS_FORBIDDEN,                  "Forbidden" },
220         { SOUP_STATUS_NOT_FOUND,                  "Not Found" },
221         { SOUP_STATUS_METHOD_NOT_ALLOWED,         "Method Not Allowed" },
222         { SOUP_STATUS_NOT_ACCEPTABLE,             "Not Acceptable" },
223         { SOUP_STATUS_PROXY_UNAUTHORIZED,         "Proxy Authentication Required" },
224         { SOUP_STATUS_REQUEST_TIMEOUT,            "Request Timeout" },
225         { SOUP_STATUS_CONFLICT,                   "Conflict" },
226         { SOUP_STATUS_GONE,                       "Gone" },
227         { SOUP_STATUS_LENGTH_REQUIRED,            "Length Required" },
228         { SOUP_STATUS_PRECONDITION_FAILED,        "Precondition Failed" },
229         { SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE,   "Request Entity Too Large" },
230         { SOUP_STATUS_REQUEST_URI_TOO_LONG,       "Request-URI Too Long" },
231         { SOUP_STATUS_UNSUPPORTED_MEDIA_TYPE,     "Unsupported Media Type" },
232         { SOUP_STATUS_INVALID_RANGE,              "Requested Range Not Satisfiable" },
233         { SOUP_STATUS_EXPECTATION_FAILED,         "Expectation Failed" },
234         { SOUP_STATUS_UNPROCESSABLE_ENTITY,       "Unprocessable Entity" },
235         { SOUP_STATUS_LOCKED,                     "Locked" },
236         { SOUP_STATUS_FAILED_DEPENDENCY,          "Failed Dependency" },
237
238         /* Server error */
239         { SOUP_STATUS_INTERNAL_SERVER_ERROR,      "Internal Server Error" },
240         { SOUP_STATUS_NOT_IMPLEMENTED,            "Not Implemented" },
241         { SOUP_STATUS_BAD_GATEWAY,                "Bad Gateway" },
242         { SOUP_STATUS_SERVICE_UNAVAILABLE,        "Service Unavailable" },
243         { SOUP_STATUS_GATEWAY_TIMEOUT,            "Gateway Timeout" },
244         { SOUP_STATUS_HTTP_VERSION_NOT_SUPPORTED, "HTTP Version Not Supported" },
245         { SOUP_STATUS_INSUFFICIENT_STORAGE,       "Insufficient Storage" },
246         { SOUP_STATUS_NOT_EXTENDED,               "Not Extended" },
247
248         { 0 }
249 };
250
251 /**
252  * soup_status_get_phrase:
253  * @status_code: an HTTP status code
254  *
255  * Looks up the stock HTTP description of @status_code. This is used
256  * by soup_message_set_status() to get the correct text to go with a
257  * given status code.
258  *
259  * <emphasis>There is no reason for you to ever use this
260  * function.</emphasis> If you wanted the textual description for the
261  * #SoupMessage:status_code of a given #SoupMessage, you should just
262  * look at the message's #SoupMessage:reason_phrase. However, you
263  * should only do that for use in debugging messages; HTTP reason
264  * phrases are not localized, and are not generally very descriptive
265  * anyway, and so they should never be presented to the user directly.
266  * Instead, you should create you own error messages based on the
267  * status code, and on what you were trying to do.
268  *
269  * Return value: the (terse, English) description of @status_code
270  **/
271 const char *
272 soup_status_get_phrase (guint status_code)
273 {
274         int i;
275
276         for (i = 0; reason_phrases [i].code; i++) {
277                 if (reason_phrases [i].code == status_code)
278                         return reason_phrases [i].phrase;
279         }
280
281         return "Unknown Error";
282 }
283
284 /**
285  * soup_status_proxify:
286  * @status_code: a status code
287  *
288  * Turns %SOUP_STATUS_CANT_RESOLVE into
289  * %SOUP_STATUS_CANT_RESOLVE_PROXY and %SOUP_STATUS_CANT_CONNECT into
290  * %SOUP_STATUS_CANT_CONNECT_PROXY. Other status codes are passed
291  * through unchanged.
292  *
293  * Return value: the "proxified" equivalent of @status_code.
294  *
295  * Since: 2.26
296  **/
297 guint
298 soup_status_proxify (guint status_code)
299 {
300         if (status_code == SOUP_STATUS_CANT_RESOLVE)
301                 return SOUP_STATUS_CANT_RESOLVE_PROXY;
302         else if (status_code == SOUP_STATUS_CANT_CONNECT)
303                 return SOUP_STATUS_CANT_CONNECT_PROXY;
304         else
305                 return status_code;
306 }
307
308 GQuark
309 soup_http_error_quark (void)
310 {
311         static GQuark error;
312         if (!error)
313                 error = g_quark_from_static_string ("soup_http_error_quark");
314         return error;
315 }