1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* http-method.c - The HTTP method implementation for the GNOME Virtual File
5 Copyright (C) 1999 Free Software Foundation
6 Copyright (C) 2000-2001 Eazel, Inc
8 The Gnome Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
13 The Gnome Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public
19 License along with the Gnome Library; see the file COPYING.LIB. If not,
20 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
24 Ettore Perazzoli <ettore@gnu.org> (core HTTP)
25 Ian McKellar <yakk@yakk.net> (WebDAV/PUT)
26 Michael Fleming <mfleming@eazel.com> (Caching, Cleanup)
27 The friendly GNU Wget sources
32 - Handle persistent connections. */
35 #include "http-method.h"
37 #include "http-authn.h"
38 #include "http-cache.h"
39 /* Keep <sys/types.h> above any network includes for FreeBSD. */
40 #include <sys/types.h>
41 /* Keep <netinet/in.h> above <arpa/inet.h> for FreeBSD. */
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <gconf/gconf-client.h>
45 #include <libgnomevfs/gnome-vfs-inet-connection.h>
46 #include <libgnomevfs/gnome-vfs-mime-sniff-buffer.h>
47 #include <libgnomevfs/gnome-vfs-mime.h>
48 #include <libgnomevfs/gnome-vfs-module-callback-module-api.h>
49 #include <libgnomevfs/gnome-vfs-module.h>
50 #include <libgnomevfs/gnome-vfs-private-utils.h>
51 #include <libgnomevfs/gnome-vfs-socket-buffer.h>
52 #include <libgnomevfs/gnome-vfs-socket.h>
53 #include <libgnomevfs/gnome-vfs-ssl.h>
54 #include <libgnomevfs/gnome-vfs-standard-callbacks.h>
55 #include <libxml/parser.h>
56 #include <libxml/tree.h>
57 #include <libxml/xmlmemory.h>
60 #include <stdlib.h> /* for atoi */
62 #include <sys/socket.h>
67 #ifdef DEBUG_HTTP_ENABLE
69 http_debug_printf (char *fmt, ...)
78 out = g_strdup_vprintf (fmt, args);
80 fprintf (stderr, "HTTP: [0x%08x] [%p] %s\n",
81 (unsigned int) http_util_get_utime (),
82 g_thread_self (), out);
87 #endif /* DEBUG_HTTP_ENABLE */
89 /* What do we qualify ourselves as? */
90 /* FIXME bugzilla.gnome.org 41160: "gnome-vfs/1.0.0" may not be good. */
91 #define USER_AGENT_STRING "gnome-vfs/" VERSION
93 /* Custom User-Agent environment variable */
94 #define CUSTOM_USER_AGENT_VARIABLE "GNOME_VFS_HTTP_USER_AGENT"
96 /* Standard HTTP[S] port. */
97 #define DEFAULT_HTTP_PORT 80
98 #define DEFAULT_HTTPS_PORT 443
100 /* Standard HTTP proxy port */
101 #define DEFAULT_HTTP_PROXY_PORT 8080
103 /* Maximum amount of data to read if seek()ed forward. Otherwise make a new connection. */
104 #define MAX_BUFFER_SEEK_SKIP_READ 0x10000
106 /* GConf paths and keys */
107 #define PATH_GCONF_GNOME_VFS "/system/http_proxy"
108 #define ITEM_GCONF_HTTP_PROXY_PORT "port"
109 #define ITEM_GCONF_HTTP_PROXY_HOST "host"
110 #define KEY_GCONF_HTTP_PROXY_PORT (PATH_GCONF_GNOME_VFS "/" ITEM_GCONF_HTTP_PROXY_PORT)
111 #define KEY_GCONF_HTTP_PROXY_HOST (PATH_GCONF_GNOME_VFS "/" ITEM_GCONF_HTTP_PROXY_HOST)
113 #define ITEM_GCONF_USE_HTTP_PROXY "use_http_proxy"
114 #define KEY_GCONF_USE_HTTP_PROXY (PATH_GCONF_GNOME_VFS "/" ITEM_GCONF_USE_HTTP_PROXY)
116 #define KEY_GCONF_HTTP_AUTH_USER (PATH_GCONF_GNOME_VFS "/" "authentication_user")
117 #define KEY_GCONF_HTTP_AUTH_PW (PATH_GCONF_GNOME_VFS "/" "authentication_password")
118 #define KEY_GCONF_HTTP_USE_AUTH (PATH_GCONF_GNOME_VFS "/" "use_authentication")
120 #define KEY_GCONF_HTTP_PROXY_IGNORE_HOSTS (PATH_GCONF_GNOME_VFS "/" "ignore_hosts")
123 /* Some status code validation macros. */
124 #define HTTP_20X(x) (((x) >= 200) && ((x) < 300))
125 #define HTTP_PARTIAL(x) ((x) == HTTP_STATUS_PARTIAL_CONTENTS)
126 #define HTTP_REDIRECTED(x) (((x) == HTTP_STATUS_MOVED_PERMANENTLY) \
127 || ((x) == HTTP_STATUS_MOVED_TEMPORARILY))
129 /* HTTP/1.1 status codes from RFC2068, provided for reference. */
130 /* Successful 2xx. */
131 #define HTTP_STATUS_OK 200
132 #define HTTP_STATUS_CREATED 201
133 #define HTTP_STATUS_ACCEPTED 202
134 #define HTTP_STATUS_NON_AUTHORITATIVE 203
135 #define HTTP_STATUS_NO_CONTENT 204
136 #define HTTP_STATUS_RESET_CONTENT 205
137 #define HTTP_STATUS_PARTIAL_CONTENTS 206
139 /* Redirection 3xx. */
140 #define HTTP_STATUS_MULTIPLE_CHOICES 300
141 #define HTTP_STATUS_MOVED_PERMANENTLY 301
142 #define HTTP_STATUS_MOVED_TEMPORARILY 302
143 #define HTTP_STATUS_SEE_OTHER 303
144 #define HTTP_STATUS_NOT_MODIFIED 304
145 #define HTTP_STATUS_USE_PROXY 305
147 /* Client error 4xx. */
148 #define HTTP_STATUS_BAD_REQUEST 400
149 #define HTTP_STATUS_UNAUTHORIZED 401
150 #define HTTP_STATUS_PAYMENT_REQUIRED 402
151 #define HTTP_STATUS_FORBIDDEN 403
152 #define HTTP_STATUS_NOT_FOUND 404
153 #define HTTP_STATUS_METHOD_NOT_ALLOWED 405
154 #define HTTP_STATUS_NOT_ACCEPTABLE 406
155 #define HTTP_STATUS_PROXY_AUTH_REQUIRED 407
156 #define HTTP_STATUS_REQUEST_TIMEOUT 408
157 #define HTTP_STATUS_CONFLICT 409
158 #define HTTP_STATUS_GONE 410
159 #define HTTP_STATUS_LENGTH_REQUIRED 411
160 #define HTTP_STATUS_PRECONDITION_FAILED 412
161 #define HTTP_STATUS_REQENTITY_TOO_LARGE 413
162 #define HTTP_STATUS_REQURI_TOO_LARGE 414
163 #define HTTP_STATUS_UNSUPPORTED_MEDIA 415
164 #define HTTP_STATUS_LOCKED 423
166 /* Server errors 5xx. */
167 #define HTTP_STATUS_INTERNAL 500
168 #define HTTP_STATUS_NOT_IMPLEMENTED 501
169 #define HTTP_STATUS_BAD_GATEWAY 502
170 #define HTTP_STATUS_UNAVAILABLE 503
171 #define HTTP_STATUS_GATEWAY_TIMEOUT 504
172 #define HTTP_STATUS_UNSUPPORTED_VERSION 505
173 #define HTTP_STATUS_INSUFFICIENT_STORAGE 507
179 /* Global variables used by the HTTP proxy config */
180 static GConfClient * gl_client = NULL;
181 static GMutex *gl_mutex = NULL; /* This mutex protects preference values
182 * and ensures serialization of authentication
185 static gchar *gl_http_proxy = NULL;
186 static gchar *gl_http_proxy_auth = NULL;
187 static GSList *gl_ignore_hosts = NULL; /* Elements are strings. */
188 static GSList *gl_ignore_addrs = NULL; /* Elements are ProxyHostAddrs */
190 /* Store IP addresses that may represent network or host addresses and may be
202 struct in6_addr addr6;
203 struct in6_addr mask6;
208 GnomeVFSSocketBuffer *socket_buffer;
211 /* The list of headers returned with this response, newlines removed */
212 GList *response_headers;
214 /* File info for this file */
215 GnomeVFSFileInfo *file_info;
217 /* File offset of current pointer of 'socket_buffer'. */
218 GnomeVFSFileOffset socket_buffer_offset;
220 /* Offset; Current file position. */
221 GnomeVFSFileOffset offset;
223 /* Bytes to be written... */
224 GByteArray *to_be_written;
226 /* List of GnomeVFSFileInfo from a directory listing */
229 /* The last HTTP status code returned */
233 static GnomeVFSResult resolve_409 (GnomeVFSMethod *method,
235 GnomeVFSContext *context);
236 static void proxy_set_authn (const char *username,
237 const char *password);
238 static void proxy_unset_authn (void);
239 static gboolean invoke_callback_send_additional_headers (GnomeVFSURI *uri,
241 static gboolean invoke_callback_headers_received (HttpFileHandle *handle);
242 static gboolean invoke_callback_basic_authn (HttpFileHandle *handle,
243 enum AuthnHeaderType authn_which,
244 gboolean previous_attempt_failed);
245 static gboolean check_authn_retry_request (HttpFileHandle * http_handle,
246 enum AuthnHeaderType authn_which,
247 const char *prev_authn_header);
248 static void parse_ignore_host (gpointer data,
251 static void ipv6_network_addr (const struct in6_addr *addr,
252 const struct in6_addr *mask,
253 struct in6_addr *res);
255 /*Check whether the node is IPv6 enabled.*/
261 s = socket (AF_INET6, SOCK_STREAM, 0);
271 static GnomeVFSFileInfo *
272 defaults_file_info_new (void)
274 GnomeVFSFileInfo *ret;
276 /* Fill up the file info structure with default values */
277 /* Default to REGULAR unless we find out later via a PROPFIND that it's a collection */
279 ret = gnome_vfs_file_info_new();
281 ret->type = GNOME_VFS_FILE_TYPE_REGULAR;
282 ret->flags = GNOME_VFS_FILE_FLAGS_NONE;
285 GNOME_VFS_FILE_INFO_FIELDS_TYPE
286 | GNOME_VFS_FILE_INFO_FIELDS_FLAGS;
292 /* Does not allocate the 'handle' memory. */
294 http_file_handle_new (HttpFileHandle *handle,
295 GnomeVFSSocketBuffer *socket_buffer,
298 memset (handle, 0, sizeof (*handle));
300 handle->socket_buffer = socket_buffer;
301 handle->uri_string = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE );
303 gnome_vfs_uri_ref(handle->uri);
305 handle->file_info = defaults_file_info_new();
306 handle->file_info->name = gnome_vfs_uri_extract_short_name (uri);
309 /* Does not free the 'handle' memory. */
311 http_file_handle_destroy (HttpFileHandle *handle)
313 if (handle == NULL) {
317 gnome_vfs_uri_unref(handle->uri);
318 gnome_vfs_file_info_unref (handle->file_info);
319 g_free (handle->uri_string);
320 if (handle->to_be_written) {
321 g_byte_array_free(handle->to_be_written, TRUE);
324 g_list_foreach (handle->response_headers, (GFunc) g_free, NULL);
325 g_list_free (handle->response_headers);
327 g_list_foreach(handle->files, (GFunc)gnome_vfs_file_info_unref, NULL);
328 g_list_free(handle->files);
331 /* The following comes from GNU Wget with minor changes by myself.
332 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc. */
333 /* Parse the HTTP status line, which is of format:
335 HTTP-Version SP Status-Code SP Reason-Phrase
337 The function returns the status-code, or -1 if the status line is
338 malformed. The pointer to reason-phrase is returned in RP. */
340 parse_status (const char *cline,
341 guint *status_return)
343 /* (the variables must not be named `major' and `minor', because
344 that breaks compilation with SunOS4 cc.) */
347 const guchar *p, *line;
349 line = (const guchar *)cline;
351 /* The standard format of HTTP-Version is: `HTTP/X.Y', where X is
352 major version, and Y is minor version. */
353 if (strncmp (line, "HTTP/", 5) == 0) {
356 /* Calculate major HTTP version. */
358 for (mjr = 0; g_ascii_isdigit (*line); line++)
359 mjr = 10 * mjr + (*line - '0');
360 if (*line != '.' || p == line)
364 /* Calculate minor HTTP version. */
366 for (mnr = 0; g_ascii_isdigit (*line); line++)
367 mnr = 10 * mnr + (*line - '0');
368 if (*line != ' ' || p == line)
370 /* Wget will accept only 1.0 and higher HTTP-versions. The value of
371 minor version can be safely ignored. */
375 } else if (strncmp (line, "ICY ", 4) == 0) {
376 /* FIXME: workaround for broken ShoutCast and IceCast status replies.
377 * They send things like "ICY 200 OK" instead of "HTTP/1.0 200 OK".
378 * Is there a better way to handle this?
387 /* Calculate status code. */
388 if (!(g_ascii_isdigit (*line) && g_ascii_isdigit (line[1]) && g_ascii_isdigit (line[2])))
390 statcode = 100 * (*line - '0') + 10 * (line[1] - '0') + (line[2] - '0');
392 *status_return = statcode;
396 static GnomeVFSResult
397 http_status_to_vfs_result (guint status)
399 if (HTTP_20X (status))
402 /* FIXME bugzilla.gnome.org 41163 */
403 /* mfleming--I've improved the situation slightly, but more
404 * test cases need to be written to ensure that HTTP (esp DAV) does compatibile
405 * things with the normal file method
409 case HTTP_STATUS_PRECONDITION_FAILED:
410 /* This mapping is certainly true for MOVE with Overwrite: F, otherwise not so true */
411 return GNOME_VFS_ERROR_FILE_EXISTS;
412 case HTTP_STATUS_UNAUTHORIZED:
413 case HTTP_STATUS_PROXY_AUTH_REQUIRED:
414 case HTTP_STATUS_FORBIDDEN:
415 /* Note that FORBIDDEN can also be returned on a MOVE in a case which
416 * should be VFS_ERROR_BAD_PARAMETERS
418 return GNOME_VFS_ERROR_ACCESS_DENIED;
419 case HTTP_STATUS_NOT_FOUND:
420 return GNOME_VFS_ERROR_NOT_FOUND;
421 case HTTP_STATUS_METHOD_NOT_ALLOWED:
422 /* Note that METHOD_NOT_ALLOWED is also returned in a PROPFIND in a case which
423 * should be FILE_EXISTS. This is handled in do_make_directory
425 case HTTP_STATUS_BAD_REQUEST:
426 case HTTP_STATUS_NOT_IMPLEMENTED:
427 case HTTP_STATUS_UNSUPPORTED_VERSION:
428 return GNOME_VFS_ERROR_NOT_SUPPORTED;
429 case HTTP_STATUS_CONFLICT:
430 /* _CONFLICT's usually happen when collection paths don't exist */
431 return GNOME_VFS_ERROR_NOT_FOUND;
432 case HTTP_STATUS_LOCKED:
433 /* Maybe we need a separate GNOME_VFS_ERROR_LOCKED? */
434 return GNOME_VFS_ERROR_DIRECTORY_BUSY;
435 case HTTP_STATUS_INSUFFICIENT_STORAGE:
436 return GNOME_VFS_ERROR_NO_SPACE;
438 return GNOME_VFS_ERROR_GENERIC;
442 /* Header parsing routines. */
445 header_value_to_number (const char *header_value,
453 for (result = 0; g_ascii_isdigit (*p); p++)
454 result = 10 * result + (*p - '0');
464 set_content_length (HttpFileHandle *handle,
470 result = header_value_to_number (value, &size);
474 DEBUG_HTTP (("Expected size is %lu.", size));
475 handle->file_info->size = size;
476 handle->file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_SIZE;
481 strip_semicolon (const char *value)
485 p = strchr (value, ';');
488 return g_strndup (value, p - value);
491 return g_strdup (value);
496 set_content_type (HttpFileHandle *handle,
499 g_free (handle->file_info->mime_type);
501 handle->file_info->mime_type = strip_semicolon (value);
502 handle->file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE;
508 set_last_modified (HttpFileHandle *handle,
513 if (! gnome_vfs_atotm (value, &time))
516 handle->file_info->mtime = time;
517 handle->file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_MTIME;
522 set_access_time (HttpFileHandle *handle,
527 if (! gnome_vfs_atotm (value, &time))
530 handle->file_info->atime = time;
531 handle->file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_ATIME;
537 gboolean (* set_func) (HttpFileHandle *handle, const char *value);
539 typedef struct _Header Header;
541 static Header headers[] = {
542 { "Content-Length", set_content_length },
543 { "Content-Type", set_content_type },
544 { "Last-Modified", set_last_modified },
545 { "Date", set_access_time },
550 check_header (const char *header,
555 for (p = header, q = name; *p != '\0' && *q != '\0'; p++, q++) {
556 if (g_ascii_tolower (*p) != g_ascii_tolower (*q))
560 if (*q != '\0' || *p != ':')
564 while (*p == ' ' || *p == '\t')
571 parse_header (HttpFileHandle *handle,
576 for (i = 0; headers[i].name != NULL; i++) {
579 value = check_header (header, headers[i].name);
581 return (* headers[i].set_func) (handle, value);
584 /* Simply ignore headers we don't know. */
588 /* Header/status reading. */
590 static GnomeVFSResult
591 get_header (GnomeVFSSocketBuffer *socket_buffer,
594 GnomeVFSResult result;
595 GnomeVFSFileSize bytes_read;
598 ANALYZE_HTTP ("==> +get_header");
600 g_string_truncate (s, 0);
606 /* ANALYZE_HTTP ("==> +get_header read"); */
607 result = gnome_vfs_socket_buffer_read (socket_buffer, &c, 1,
609 /* ANALYZE_HTTP ("==> -get_header read"); */
611 if (result != GNOME_VFS_OK) {
614 if (bytes_read == 0) {
615 return GNOME_VFS_ERROR_EOF;
619 /* Handle continuation lines. */
620 if (count != 0 && (count != 1 || s->str[0] != '\r')) {
623 result = gnome_vfs_socket_buffer_peekc (
624 socket_buffer, &next);
625 if (result != GNOME_VFS_OK) {
629 if (next == '\t' || next == ' ') {
631 && s->str[count - 1] == '\r')
632 s->str[count - 1] = '\0';
637 if (count > 0 && s->str[count - 1] == '\r')
638 s->str[count - 1] = '\0';
641 g_string_append_c (s, c);
648 ANALYZE_HTTP ("==> -get_header");
653 /* rename this function? */
654 static GnomeVFSResult
655 create_handle (GnomeVFSURI *uri,
656 GnomeVFSSocketBuffer *socket_buffer,
657 GnomeVFSContext *context,
658 /* OUT */ HttpFileHandle *handle)
660 GString *header_string;
661 GnomeVFSResult result;
664 g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_INTERNAL);
666 http_file_handle_new (handle, socket_buffer, uri);
668 header_string = g_string_new (NULL);
670 ANALYZE_HTTP ("==> +create_handle");
672 /* This is the status report string, which is the first header. */
673 result = get_header (socket_buffer, header_string);
674 if (result != GNOME_VFS_OK) {
678 if (!parse_status (header_string->str, &server_status)) {
679 /* An unparsable status line is fatal */
680 result = GNOME_VFS_ERROR_GENERIC;
684 handle->server_status = server_status;
686 ANALYZE_HTTP ("==> +create_handle: fetching headers");
688 /* Header fetching loop. */
690 result = get_header (socket_buffer, header_string);
691 if (result != GNOME_VFS_OK) {
695 /* Empty header ends header section. */
696 if (header_string->str[0] == '\0') {
700 handle->response_headers = g_list_prepend (handle->response_headers,
701 g_strdup (header_string->str));
703 /* We don't really care if we successfully parse the
704 * header or not. It might be nice to tell someone we
705 * found a header we can't parse, but it's not clear
706 * who would be interested or how we tell them. In the
707 * past we would return NOT_FOUND if any header could
708 * not be parsed, but that seems wrong.
710 parse_header (handle, header_string->str);
713 invoke_callback_headers_received (handle);
715 ANALYZE_HTTP ("==> -create_handle: fetching headers");
717 if (result != GNOME_VFS_OK) {
721 if (! HTTP_20X (server_status) && !HTTP_REDIRECTED(server_status)) {
722 result = http_status_to_vfs_result (server_status);
726 result = GNOME_VFS_OK;
728 g_string_free (header_string, TRUE);
730 ANALYZE_HTTP ("==> -create_handle");
735 * Here's how the gconf gnome-vfs HTTP proxy variables
736 * are intended to be used
738 * /system/http_proxy/use_http_proxy
740 * If set to TRUE, the client should use an HTTP proxy to connect to all
741 * servers (except those specified in the ignore_hosts key -- see below).
742 * The proxy is specified in other gconf variables below.
744 * /system/http_proxy/host
746 * The hostname of the HTTP proxy this client should use. If
747 * use-http-proxy is TRUE, this should be set. If it is not set, the
748 * application should behave as if use-http-proxy is was set to FALSE.
750 * /system/http_proxy/port
752 * The port number on the HTTP proxy host that the client should connect to
753 * If use_http_proxy and host are set but this is not set, the application
754 * should use a default port value of 8080
756 * /system/http_proxy/authentication-user
758 * Username to pass to an authenticating HTTP proxy.
760 * /system/http_proxy/authentication_password
762 * Password to pass to an authenticating HTTP proxy.
764 * /system/http_proxy/use-authentication
766 * TRUE if the client should pass http-proxy-authorization-user and
767 * http-proxy-authorization-password an HTTP proxy
769 * /system/http_proxy/ignore_hosts
770 * Type: list of strings
771 * A list of hosts (hostnames, wildcard domains, IP addresses, and CIDR
772 * network addresses) that should be accessed directly.
776 construct_gl_http_proxy (gboolean use_proxy)
778 g_free (gl_http_proxy);
779 gl_http_proxy = NULL;
781 g_slist_foreach (gl_ignore_hosts, (GFunc) g_free, NULL);
782 g_slist_free (gl_ignore_hosts);
783 gl_ignore_hosts = NULL;
784 g_slist_foreach (gl_ignore_addrs, (GFunc) g_free, NULL);
785 g_slist_free (gl_ignore_addrs);
786 gl_ignore_addrs = NULL;
793 proxy_host = gconf_client_get_string (gl_client, KEY_GCONF_HTTP_PROXY_HOST, NULL);
794 proxy_port = gconf_client_get_int (gl_client, KEY_GCONF_HTTP_PROXY_PORT, NULL);
797 if (0 != proxy_port && 0xffff >= (unsigned) proxy_port) {
798 gl_http_proxy = g_strdup_printf ("%s:%u", proxy_host, (unsigned)proxy_port);
800 gl_http_proxy = g_strdup_printf ("%s:%u", proxy_host, (unsigned)DEFAULT_HTTP_PROXY_PORT);
802 DEBUG_HTTP (("New HTTP proxy: '%s'", gl_http_proxy));
804 DEBUG_HTTP (("HTTP proxy unset"));
810 ignore = gconf_client_get_list (gl_client, KEY_GCONF_HTTP_PROXY_IGNORE_HOSTS, GCONF_VALUE_STRING, NULL);
811 g_slist_foreach (ignore, (GFunc) parse_ignore_host, NULL);
812 g_slist_foreach (ignore, (GFunc) g_free, NULL);
813 g_slist_free (ignore);
819 parse_ignore_host (gpointer data, gpointer user_data)
821 gchar *hostname, *input, *netmask;
822 gboolean ip_addr = FALSE, has_error = FALSE;
823 struct in_addr host, mask;
825 struct in6_addr host6, mask6;
830 input = (gchar*) data;
831 elt = g_new0 (ProxyHostAddr, 1);
832 if ((netmask = strchr (input, '/')) != NULL) {
833 hostname = g_strndup (input, netmask - input);
837 hostname = g_ascii_strdown (input, -1);
839 if (inet_pton (AF_INET, hostname, &host) > 0) {
841 elt->type = PROXY_IPv4;
842 elt->addr.s_addr = host.s_addr;
845 gint width = strtol (netmask, &endptr, 10);
847 if (*endptr != '\0' || width < 0 || width > 32) {
850 elt->mask.s_addr = htonl(~0 << width);
851 elt->addr.s_addr &= mask.s_addr;
854 elt->mask.s_addr = 0xffffffff;
858 else if (have_ipv6 () && inet_pton (AF_INET6, hostname, &host6) > 0) {
860 elt->type = PROXY_IPv6;
861 for (i = 0; i < 16; ++i) {
862 elt->addr6.s6_addr[i] = host6.s6_addr[i];
866 gint width = strtol (netmask, &endptr, 10);
868 if (*endptr != '\0' || width < 0 || width > 128) {
871 for (i = 0; i < 16; ++i) {
872 elt->mask6.s6_addr[i] = 0;
874 for (i=0; i < width/8; i++) {
875 elt->mask6.s6_addr[i] = 0xff;
877 elt->mask6.s6_addr[i] = (0xff << (8 - width % 8)) & 0xff;
878 ipv6_network_addr (&elt->addr6, &mask6, &elt->addr6);
881 for (i = 0; i < 16; ++i) {
882 elt->mask6.s6_addr[i] = 0xff;
890 gchar *dst = g_new0 (gchar, INET_ADDRSTRLEN);
892 gl_ignore_addrs = g_slist_append (gl_ignore_addrs, elt);
893 DEBUG_HTTP (("Host %s/%s does not go through proxy.",
895 inet_ntop(AF_INET, &elt->mask, dst, INET_ADDRSTRLEN)));
900 /* It is a hostname. */
901 gl_ignore_hosts = g_slist_append (gl_ignore_hosts, hostname);
902 DEBUG_HTTP (("Host %s does not go through proxy.", hostname));
907 set_proxy_auth (gboolean use_proxy_auth)
912 auth_user = gconf_client_get_string (gl_client, KEY_GCONF_HTTP_AUTH_USER, NULL);
913 auth_pw = gconf_client_get_string (gl_client, KEY_GCONF_HTTP_AUTH_PW, NULL);
915 if (use_proxy_auth) {
916 proxy_set_authn (auth_user, auth_pw);
917 DEBUG_HTTP (("New HTTP proxy auth user: '%s'", auth_user));
919 proxy_unset_authn ();
920 DEBUG_HTTP (("HTTP proxy auth unset"));
928 * sig_gconf_value_changed
929 * GGconf notify function for when HTTP proxy GConf key has changed.
932 notify_gconf_value_changed (GConfClient *client,
939 key = gconf_entry_get_key (entry);
941 if (strcmp (key, KEY_GCONF_USE_HTTP_PROXY) == 0
942 || strcmp (key, KEY_GCONF_HTTP_PROXY_IGNORE_HOSTS) == 0
943 || strcmp (key, KEY_GCONF_HTTP_PROXY_HOST) == 0
944 || strcmp (key, KEY_GCONF_HTTP_PROXY_PORT) == 0) {
945 gboolean use_proxy_value;
947 g_mutex_lock (gl_mutex);
949 /* Check and see if we are using the proxy */
950 use_proxy_value = gconf_client_get_bool (gl_client, KEY_GCONF_USE_HTTP_PROXY, NULL);
951 construct_gl_http_proxy (use_proxy_value);
953 g_mutex_unlock (gl_mutex);
954 } else if (strcmp (key, KEY_GCONF_HTTP_AUTH_USER) == 0
955 || strcmp (key, KEY_GCONF_HTTP_AUTH_PW) == 0
956 || strcmp (key, KEY_GCONF_HTTP_USE_AUTH) == 0) {
957 gboolean use_proxy_auth;
959 g_mutex_lock (gl_mutex);
961 use_proxy_auth = gconf_client_get_bool (gl_client, KEY_GCONF_HTTP_USE_AUTH, NULL);
962 set_proxy_auth (use_proxy_auth);
964 g_mutex_unlock (gl_mutex);
969 * host_port_from_string
970 * splits a <host>:<port> formatted string into its separate components
973 host_port_from_string (const char *http_proxy,
979 port_part = strchr (http_proxy, ':');
981 if (port_part && '\0' != ++port_part && p_proxy_port) {
982 *p_proxy_port = (guint) strtoul (port_part, NULL, 10);
983 } else if (p_proxy_port) {
984 *p_proxy_port = DEFAULT_HTTP_PROXY_PORT;
988 if ( port_part != http_proxy ) {
989 *p_proxy_host = g_strndup (http_proxy, port_part - http_proxy - 1);
998 /* FIXME: should be done using AC_REPLACE_FUNCS */
999 #ifndef HAVE_INET_PTON
1001 inet_pton(int af, const char *hostname, void *pton)
1004 if (!inet_aton(hostname, &in))
1006 memcpy(pton, &in, sizeof(in));
1013 ipv6_network_addr (const struct in6_addr *addr, const struct in6_addr *mask, struct in6_addr *res)
1017 for (i = 0; i < 16; ++i) {
1018 res->s6_addr[i] = addr->s6_addr[i] & mask->s6_addr[i];
1024 proxy_should_for_hostname (const char *hostname)
1027 struct in6_addr in6, net6;
1031 ProxyHostAddr *addr;
1035 if (inet_pton (AF_INET, hostname, &in) > 0) {
1036 for (elt = gl_ignore_addrs; elt; elt = g_slist_next (elt)) {
1037 addr = (ProxyHostAddr*) (elt->data);
1038 if (addr->type == PROXY_IPv4
1039 && (in.s_addr & addr->mask.s_addr) == addr->addr.s_addr) {
1040 DEBUG_HTTP (("Host %s using direct connection.", hostname));
1046 else if (have_ipv6 () && inet_pton (AF_INET6, hostname, &in6)) {
1047 for (elt = gl_ignore_addrs; elt; elt = g_slist_next (elt)) {
1048 addr = (ProxyHostAddr*) (elt->data);
1049 ipv6_network_addr (&in6, &addr->mask6, &net6);
1050 if (addr->type == PROXY_IPv6
1051 && IN6_ARE_ADDR_EQUAL (&net6, &addr->addr6)) {
1052 DEBUG_HTTP (("Host %s using direct connection.", hostname));
1055 /* Handle IPv6-wrapped IPv4 addresses. */
1056 else if (addr->type == PROXY_IPv4
1057 && IN6_IS_ADDR_V4MAPPED (&net6)) {
1060 v4addr = net6.s6_addr[12] << 24 | net6.s6_addr[13] << 16 | net6.s6_addr[14] << 8 | net6.s6_addr[15];
1061 if ((v4addr & addr->mask.s_addr) != addr->addr.s_addr) {
1062 DEBUG_HTTP (("Host %s using direct connection.", hostname));
1069 /* All hostnames (foo.bar.com) -- independent of IPv4 or IPv6 */
1071 /* If there are IPv6 addresses in the ignore_hosts list but we do not
1072 * have IPv6 available at runtime, then those addresses will also fall
1073 * through to here (and harmlessly fail to match). */
1075 gchar *hn = g_ascii_strdown (hostname, -1);
1077 for (elt = gl_ignore_hosts; elt; elt = g_slist_next (elt)) {
1078 if (*(gchar*) (elt->data) == '*' ) {
1079 if (g_str_has_suffix (hn,
1080 (gchar*) (elt->data) + 1)) {
1081 DEBUG_HTTP (("Host %s using direct connection.", hn));
1086 else if (strcmp (hn, elt->data) == 0) {
1087 DEBUG_HTTP (("Host %s using direct connection.", hn));
1098 proxy_get_authn_header_for_uri_nolock (GnomeVFSURI * uri)
1104 /* FIXME this needs to be atomic */
1105 if (gl_http_proxy_auth != NULL) {
1106 ret = g_strdup_printf ("Proxy-Authorization: Basic %s\r\n", gl_http_proxy_auth);
1113 proxy_get_authn_header_for_uri (GnomeVFSURI * uri)
1117 g_mutex_lock (gl_mutex);
1119 ret = proxy_get_authn_header_for_uri_nolock (uri);
1121 g_mutex_unlock (gl_mutex);
1128 * Retrives an appropriate HTTP proxy for a given toplevel uri
1129 * Currently, only a single HTTP proxy is implemented (there's no way for
1130 * specifying non-proxy domain names's). Returns FALSE if the connect should
1131 * take place directly
1135 GnomeVFSToplevelURI * toplevel_uri,
1136 gchar **p_proxy_host, /* Callee must free */
1137 guint *p_proxy_port) /* Callee must free */
1141 ret = proxy_should_for_hostname (toplevel_uri->host_name);
1143 g_mutex_lock (gl_mutex);
1145 if (ret && gl_http_proxy != NULL) {
1146 ret = host_port_from_string (gl_http_proxy, p_proxy_host, p_proxy_port);
1148 p_proxy_host = NULL;
1149 p_proxy_port = NULL;
1153 g_mutex_unlock (gl_mutex);
1159 proxy_set_authn (const char *username, const char *password)
1163 g_free (gl_http_proxy_auth);
1164 gl_http_proxy_auth = NULL;
1166 credentials = g_strdup_printf ("%s:%s",
1167 username == NULL ? "" : username,
1168 password == NULL ? "" : password);
1170 gl_http_proxy_auth = http_util_base64 (credentials);
1172 g_free (credentials);
1176 proxy_unset_authn (void)
1178 g_free (gl_http_proxy_auth);
1179 gl_http_proxy_auth = NULL;
1183 static GnomeVFSResult
1184 https_proxy (GnomeVFSSocket **socket_return,
1190 /* use CONNECT to do https proxying. It goes something like this:
1191 * >CONNECT server:port HTTP/1.0
1193 * <HTTP/1.0 200 Connection-established
1194 * <Proxy-agent: Apache/1.3.19 (Unix) Debian/GNU
1196 * and then we've got an open connection.
1198 * So we sent "CONNECT server:port HTTP/1.0\r\n\r\n"
1199 * Check the HTTP status.
1200 * Wait for "\r\n\r\n"
1201 * Start doing the SSL dance.
1204 GnomeVFSResult result;
1205 GnomeVFSInetConnection *http_connection;
1206 GnomeVFSSocket *http_socket;
1207 GnomeVFSSocket *https_socket;
1210 GnomeVFSFileSize bytes;
1214 result = gnome_vfs_inet_connection_create (&http_connection,
1215 proxy_host, proxy_port, NULL);
1217 if (result != GNOME_VFS_OK) {
1221 fd = gnome_vfs_inet_connection_get_fd (http_connection);
1223 http_socket = gnome_vfs_inet_connection_to_socket (http_connection);
1225 buffer = g_strdup_printf ("CONNECT %s:%d HTTP/1.0\r\n\r\n",
1226 server_host, server_port);
1227 result = gnome_vfs_socket_write (http_socket, buffer, strlen(buffer),
1231 if (result != GNOME_VFS_OK) {
1232 gnome_vfs_socket_close (http_socket);
1236 buffer = proxy_get_authn_header_for_uri (NULL); /* FIXME need uri */
1237 if (buffer != NULL) {
1238 result = gnome_vfs_socket_write (http_socket, buffer,
1239 strlen(buffer), &bytes);
1243 if (result != GNOME_VFS_OK) {
1244 gnome_vfs_socket_close (http_socket);
1249 buffer = g_malloc0 (bytes);
1251 result = gnome_vfs_socket_read (http_socket, buffer, bytes-1, &bytes);
1253 if (result != GNOME_VFS_OK) {
1254 gnome_vfs_socket_close (http_socket);
1259 if (!parse_status (buffer, &status_code)) {
1260 gnome_vfs_socket_close (http_socket);
1262 return GNOME_VFS_ERROR_PROTOCOL_ERROR;
1265 result = http_status_to_vfs_result (status_code);
1267 if (result != GNOME_VFS_OK) {
1268 gnome_vfs_socket_close (http_socket);
1273 /* okay - at this point we've read some stuff from the socket.. */
1274 /* FIXME: for now we'll assume thats all the headers and nothing but. */
1278 result = gnome_vfs_ssl_create_from_fd (&ssl, fd);
1280 if (result != GNOME_VFS_OK) {
1281 gnome_vfs_socket_close (http_socket);
1285 https_socket = gnome_vfs_ssl_to_socket (ssl);
1287 *socket_return = https_socket;
1289 return GNOME_VFS_OK;
1294 static GnomeVFSResult
1296 GnomeVFSToplevelURI *toplevel_uri,
1297 /* OUT */ GnomeVFSSocketBuffer **p_socket_buffer,
1298 /* OUT */ gboolean * p_proxy_connect)
1303 GnomeVFSResult result;
1304 GnomeVFSCancellation * cancellation;
1305 GnomeVFSInetConnection *connection;
1307 GnomeVFSSocket *socket;
1308 gboolean https = FALSE;
1310 cancellation = gnome_vfs_context_get_cancellation (
1311 gnome_vfs_context_peek_current ());
1313 g_return_val_if_fail (p_socket_buffer != NULL, GNOME_VFS_ERROR_INTERNAL);
1314 g_return_val_if_fail (p_proxy_connect != NULL, GNOME_VFS_ERROR_INTERNAL);
1315 g_return_val_if_fail (toplevel_uri != NULL, GNOME_VFS_ERROR_INTERNAL);
1317 if (!g_ascii_strcasecmp (gnome_vfs_uri_get_scheme (&toplevel_uri->uri),
1319 if (!gnome_vfs_ssl_enabled ()) {
1320 return GNOME_VFS_ERROR_NOT_SUPPORTED;
1325 if (toplevel_uri->host_port == 0) {
1327 host_port = DEFAULT_HTTPS_PORT;
1329 host_port = DEFAULT_HTTP_PORT;
1332 host_port = toplevel_uri->host_port;
1335 ANALYZE_HTTP ("==> +Making connection");
1337 if (toplevel_uri->host_name == NULL) {
1338 result = GNOME_VFS_ERROR_INVALID_URI;
1342 if (proxy_for_uri (toplevel_uri, &proxy_host, &proxy_port)) {
1344 *p_proxy_connect = FALSE;
1346 result = https_proxy (&socket, proxy_host, proxy_port,
1347 toplevel_uri->host_name, host_port);
1349 g_free (proxy_host);
1352 if (result != GNOME_VFS_OK) {
1357 *p_proxy_connect = TRUE;
1359 result = gnome_vfs_inet_connection_create (&connection,
1363 if (result != GNOME_VFS_OK) {
1366 socket = gnome_vfs_inet_connection_to_socket
1369 g_free (proxy_host);
1373 *p_proxy_connect = FALSE;
1376 result = gnome_vfs_ssl_create (&ssl,
1377 toplevel_uri->host_name, host_port);
1379 if (result != GNOME_VFS_OK) {
1382 socket = gnome_vfs_ssl_to_socket (ssl);
1384 result = gnome_vfs_inet_connection_create (&connection,
1385 toplevel_uri->host_name,
1388 if (result != GNOME_VFS_OK) {
1391 socket = gnome_vfs_inet_connection_to_socket
1396 *p_socket_buffer = gnome_vfs_socket_buffer_new (socket);
1398 if (*p_socket_buffer == NULL) {
1399 gnome_vfs_socket_close (socket);
1400 return GNOME_VFS_ERROR_INTERNAL;
1403 ANALYZE_HTTP ("==> -Making connection");
1410 build_request (const char * method, GnomeVFSToplevelURI * toplevel_uri, gboolean proxy_connect)
1412 gchar *uri_string = NULL;
1417 uri = (GnomeVFSURI *)toplevel_uri;
1419 if (proxy_connect) {
1420 uri_string = gnome_vfs_uri_to_string (uri,
1421 GNOME_VFS_URI_HIDE_USER_NAME
1422 | GNOME_VFS_URI_HIDE_PASSWORD);
1425 uri_string = gnome_vfs_uri_to_string (uri,
1426 GNOME_VFS_URI_HIDE_USER_NAME
1427 | GNOME_VFS_URI_HIDE_PASSWORD
1428 | GNOME_VFS_URI_HIDE_HOST_NAME
1429 | GNOME_VFS_URI_HIDE_HOST_PORT
1430 | GNOME_VFS_URI_HIDE_TOPLEVEL_METHOD);
1434 request = g_string_new ("");
1436 g_string_append_printf (request, "%s %s%s HTTP/1.0\r\n", method, uri_string,
1437 gnome_vfs_uri_get_path (uri)[0] == '\0' ? "/" : "" );
1439 DEBUG_HTTP (("-->Making request '%s %s'", method, uri_string));
1441 g_free (uri_string);
1444 /* `Host:' header. */
1445 if(toplevel_uri->host_port && toplevel_uri->host_port != 0) {
1446 g_string_append_printf (request, "Host: %s:%d\r\n",
1447 toplevel_uri->host_name, toplevel_uri->host_port);
1449 g_string_append_printf (request, "Host: %s:80\r\n",
1450 toplevel_uri->host_name);
1453 /* `Accept:' header. */
1454 g_string_append (request, "Accept: */*\r\n");
1456 /* `User-Agent:' header. */
1457 user_agent = getenv (CUSTOM_USER_AGENT_VARIABLE);
1459 if(user_agent == NULL) {
1460 user_agent = USER_AGENT_STRING;
1463 g_string_append_printf (request, "User-Agent: %s\r\n", user_agent);
1468 static GnomeVFSResult
1469 xmit_request (GnomeVFSSocketBuffer *socket_buffer,
1473 GnomeVFSResult result;
1474 GnomeVFSFileSize bytes_written;
1476 ANALYZE_HTTP ("==> Writing request and header");
1478 /* Transmit the request headers. */
1479 result = gnome_vfs_socket_buffer_write (socket_buffer, request->str,
1480 request->len, &bytes_written);
1482 if (result != GNOME_VFS_OK) {
1486 /* Transmit the body */
1487 if(data && data->data) {
1488 ANALYZE_HTTP ("==> Writing data");
1490 result = gnome_vfs_socket_buffer_write (socket_buffer,
1491 data->data, data->len, &bytes_written);
1494 if (result != GNOME_VFS_OK) {
1498 result = gnome_vfs_socket_buffer_flush (socket_buffer);
1504 static GnomeVFSResult
1505 make_request (HttpFileHandle *handle,
1507 const gchar *method,
1509 gchar *extra_headers,
1510 GnomeVFSContext *context)
1512 GnomeVFSSocketBuffer *socket_buffer;
1513 GnomeVFSResult result;
1514 GnomeVFSToplevelURI *toplevel_uri;
1516 gboolean proxy_connect;
1517 char *authn_header_request;
1518 char *authn_header_proxy;
1520 g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_INTERNAL);
1522 ANALYZE_HTTP ("==> +make_request");
1525 proxy_connect = FALSE;
1526 authn_header_request = NULL;
1527 authn_header_proxy = NULL;
1529 toplevel_uri = (GnomeVFSToplevelURI *) uri;
1534 g_free (authn_header_request);
1535 g_free (authn_header_proxy);
1537 socket_buffer = NULL;
1538 result = connect_to_uri (toplevel_uri, &socket_buffer,
1541 if (result != GNOME_VFS_OK) {
1545 request = build_request (method, toplevel_uri, proxy_connect);
1547 authn_header_request = http_authn_get_header_for_uri (uri);
1549 if (authn_header_request != NULL) {
1550 g_string_append (request, authn_header_request);
1553 if (proxy_connect) {
1554 authn_header_proxy = proxy_get_authn_header_for_uri (uri);
1556 if (authn_header_proxy != NULL) {
1557 g_string_append (request, authn_header_proxy);
1561 /* `Content-Length' header. */
1563 g_string_append_printf (request, "Content-Length: %d\r\n", data->len);
1566 /* Extra headers. */
1567 if (extra_headers != NULL) {
1568 g_string_append (request, extra_headers);
1571 /* Extra headers from user */
1574 if (invoke_callback_send_additional_headers (uri, &list)) {
1577 for (i = list; i; i = i->next) {
1578 g_string_append (request, i->data);
1586 /* Empty line ends header section. */
1587 g_string_append (request, "\r\n");
1589 result = xmit_request (socket_buffer, request, data);
1590 g_string_free (request, TRUE);
1593 if (result != GNOME_VFS_OK) {
1597 /* Read the headers and create our internal HTTP file handle. */
1598 result = create_handle (uri, socket_buffer, context, handle);
1600 if (result == GNOME_VFS_OK) {
1601 socket_buffer = NULL;
1604 if (handle->server_status == HTTP_STATUS_UNAUTHORIZED) {
1605 if (! check_authn_retry_request (handle, AuthnHeader_WWW, authn_header_request)) {
1608 } else if (handle->server_status == HTTP_STATUS_PROXY_AUTH_REQUIRED) {
1609 if (! check_authn_retry_request (handle, AuthnHeader_WWW, authn_header_proxy)) {
1615 http_file_handle_destroy (handle);
1619 g_free (authn_header_request);
1620 g_free (authn_header_proxy);
1622 if (result != GNOME_VFS_OK && handle != NULL) {
1623 http_file_handle_destroy (handle);
1627 if (request != NULL) {
1628 g_string_free (request, TRUE);
1631 if (socket_buffer != NULL) {
1632 gnome_vfs_socket_buffer_destroy (socket_buffer, TRUE);
1635 ANALYZE_HTTP ("==> -make_request");
1640 http_handle_close (HttpFileHandle *handle,
1641 GnomeVFSContext *context)
1643 ANALYZE_HTTP ("==> +http_handle_close");
1645 if (handle != NULL) {
1646 if (handle->socket_buffer) {
1647 gnome_vfs_socket_buffer_flush (handle->socket_buffer);
1648 gnome_vfs_socket_buffer_destroy (handle->socket_buffer,
1650 handle->socket_buffer = NULL;
1653 http_file_handle_destroy (handle);
1656 ANALYZE_HTTP ("==> -http_handle_close");
1659 static GnomeVFSResult
1660 do_open (GnomeVFSMethod *method,
1661 GnomeVFSMethodHandle **method_handle,
1663 GnomeVFSOpenMode mode,
1664 GnomeVFSContext *context)
1666 HttpFileHandle *handle;
1667 GnomeVFSResult result = GNOME_VFS_OK;
1669 g_return_val_if_fail (uri->parent == NULL, GNOME_VFS_ERROR_INVALID_URI);
1670 g_return_val_if_fail (!(mode & GNOME_VFS_OPEN_READ &&
1671 mode & GNOME_VFS_OPEN_WRITE),
1672 GNOME_VFS_ERROR_INVALID_OPEN_MODE);
1674 ANALYZE_HTTP ("==> +do_open");
1675 DEBUG_HTTP (("+Open URI: '%s' mode:'%c'", gnome_vfs_uri_to_string(uri, 0),
1676 mode & GNOME_VFS_OPEN_READ ? 'R' : 'W'));
1678 handle = g_new (HttpFileHandle, 1);
1679 if (mode & GNOME_VFS_OPEN_READ) {
1680 result = make_request (handle, uri, "GET", NULL, NULL,
1683 http_file_handle_new(handle, NULL, uri); /* shrug */
1685 if (result == GNOME_VFS_OK) {
1686 *method_handle = (GnomeVFSMethodHandle *) handle;
1688 *method_handle = NULL;
1691 DEBUG_HTTP (("-Open (%d) handle:0x%08x", result, (unsigned int)handle));
1692 ANALYZE_HTTP ("==> -do_open");
1697 static GnomeVFSResult
1698 do_create (GnomeVFSMethod *method,
1699 GnomeVFSMethodHandle **method_handle,
1701 GnomeVFSOpenMode mode,
1704 GnomeVFSContext *context)
1706 /* try to write a zero length file - this appears to be the
1707 * only reliable way of testing if a put will succeed.
1708 * Xythos can apparently tell us if we have write permission by
1709 * playing with LOCK, but mod_dav cannot. */
1710 HttpFileHandle *handle;
1711 GnomeVFSResult result;
1712 GByteArray *bytes = g_byte_array_new();
1714 ANALYZE_HTTP ("==> +do_create");
1715 DEBUG_HTTP (("+Create URI: '%s'", gnome_vfs_uri_get_path (uri)));
1717 http_cache_invalidate_uri_parent (uri);
1719 /* Don't ignore exclusive; it should check first whether
1720 the file exists, since the http protocol default is to
1721 overwrite by default */
1722 /* FIXME we've stopped using HEAD -- we should use GET instead */
1723 /* FIXME we should check the cache here */
1726 ANALYZE_HTTP ("==> Checking to see if file exists");
1728 handle = g_new (HttpFileHandle, 1);
1729 result = make_request (handle, uri, "HEAD", NULL, NULL,
1731 http_handle_close (handle, context);
1734 if (result != GNOME_VFS_OK &&
1735 result != GNOME_VFS_ERROR_NOT_FOUND) {
1738 if (result == GNOME_VFS_OK) {
1739 return GNOME_VFS_ERROR_FILE_EXISTS;
1743 ANALYZE_HTTP ("==> Creating initial file");
1745 handle = g_new (HttpFileHandle, 1);
1746 result = make_request (handle, uri, "PUT", bytes, NULL, context);
1747 http_handle_close(handle, context);
1750 if (result != GNOME_VFS_OK) {
1751 /* the PUT failed */
1753 /* FIXME bugzilla.gnome.org 45131
1754 * If you PUT a file with an invalid name to Xythos, it
1755 * returns a 403 Forbidden, which is different from the behaviour
1756 * in MKCOL or MOVE. Unfortunately, it is not possible to discern whether
1757 * that 403 Forbidden is being returned because of invalid characters in the name
1758 * or because of permissions problems
1761 if (result == GNOME_VFS_ERROR_NOT_FOUND) {
1762 result = resolve_409 (method, uri, context);
1769 g_byte_array_free (bytes, TRUE);
1771 /* FIXME bugzilla.gnome.org 41159: do we need to do something more intelligent here? */
1772 result = do_open (method, method_handle, uri, GNOME_VFS_OPEN_WRITE, context);
1774 DEBUG_HTTP (("-Create (%d) handle:0x%08x", result, (unsigned int)handle));
1775 ANALYZE_HTTP ("==> -do_create");
1780 static GnomeVFSResult
1781 do_close (GnomeVFSMethod *method,
1782 GnomeVFSMethodHandle *method_handle,
1783 GnomeVFSContext *context)
1785 HttpFileHandle *old_handle;
1786 HttpFileHandle *new_handle;
1787 GnomeVFSResult result;
1789 ANALYZE_HTTP ("==> +do_close");
1790 DEBUG_HTTP (("+Close handle:0x%08x", (unsigned int)method_handle));
1792 old_handle = (HttpFileHandle *) method_handle;
1794 /* if the handle was opened in write mode then:
1795 * 1) there won't be a connection open, and
1796 * 2) there will be data to_be_written...
1798 if (old_handle->to_be_written != NULL) {
1799 GnomeVFSURI *uri = old_handle->uri;
1800 GByteArray *bytes = old_handle->to_be_written;
1801 GnomeVFSMimeSniffBuffer *sniff_buffer;
1802 char *extraheader = NULL;
1803 const char *mime_type = NULL;
1806 gnome_vfs_mime_sniff_buffer_new_from_existing_data (bytes->data,
1809 if (sniff_buffer != NULL) {
1811 gnome_vfs_get_mime_type_for_buffer (
1813 if (mime_type != NULL) {
1814 extraheader = g_strdup_printf(
1815 "Content-type: %s\r\n",
1818 gnome_vfs_mime_sniff_buffer_free (sniff_buffer);
1822 http_cache_invalidate_uri (uri);
1824 ANALYZE_HTTP ("==> doing PUT");
1825 new_handle = g_new (HttpFileHandle, 1);
1826 result = make_request (new_handle, uri, "PUT", bytes,
1827 extraheader, context);
1828 g_free (extraheader);
1829 http_handle_close (new_handle, context);
1830 g_free (new_handle);
1832 result = GNOME_VFS_OK;
1835 http_handle_close (old_handle, context);
1836 g_free (old_handle);
1838 DEBUG_HTTP (("-Close (%d)", result));
1839 ANALYZE_HTTP ("==> -do_close");
1844 static GnomeVFSResult
1845 do_write (GnomeVFSMethod *method,
1846 GnomeVFSMethodHandle *method_handle,
1847 gconstpointer buffer,
1848 GnomeVFSFileSize num_bytes,
1849 GnomeVFSFileSize *bytes_written,
1850 GnomeVFSContext *context)
1852 HttpFileHandle *handle;
1854 DEBUG_HTTP (("+Write handle:0x%08x", (unsigned int)method_handle));
1856 handle = (HttpFileHandle *) method_handle;
1858 if (handle->offset != 0)
1859 return GNOME_VFS_ERROR_NOT_SUPPORTED;
1861 if(handle->to_be_written == NULL) {
1862 handle->to_be_written = g_byte_array_new();
1864 handle->to_be_written = g_byte_array_append(handle->to_be_written, buffer, num_bytes);
1865 *bytes_written = num_bytes;
1867 DEBUG_HTTP (("-Write (0)"));
1869 return GNOME_VFS_OK;
1873 static GnomeVFSResult
1874 do_read (GnomeVFSMethod *method,
1875 GnomeVFSMethodHandle *method_handle,
1877 GnomeVFSFileSize num_bytes,
1878 GnomeVFSFileSize *bytes_read,
1879 GnomeVFSContext *context)
1881 HttpFileHandle *handle;
1882 GnomeVFSResult result;
1884 ANALYZE_HTTP ("==> +do_read");
1885 DEBUG_HTTP (("+Read handle=0x%08x", (unsigned int) method_handle));
1887 handle = (HttpFileHandle *) method_handle;
1889 if (handle->file_info->flags & GNOME_VFS_FILE_INFO_FIELDS_SIZE) {
1890 GnomeVFSFileSize max_bytes;
1892 max_bytes = MAX (0, handle->file_info->size - handle->offset);
1893 num_bytes = MIN (max_bytes, num_bytes);
1898 return GNOME_VFS_ERROR_EOF;
1902 && handle->offset > handle->socket_buffer_offset
1903 && handle->offset <= handle->socket_buffer_offset+MAX_BUFFER_SEEK_SKIP_READ) {
1904 static char drop_buffer[0x1000];
1905 GnomeVFSFileSize bytes, bytes_read;
1906 GnomeVFSResult result;
1908 while ((bytes=MIN(sizeof(drop_buffer), handle->offset - handle->socket_buffer_offset))) {
1909 result = gnome_vfs_socket_buffer_read (handle->socket_buffer, drop_buffer,
1910 bytes, &bytes_read);
1911 if (result != GNOME_VFS_OK)
1913 handle->socket_buffer_offset += bytes_read;
1917 if (handle->offset != handle->socket_buffer_offset) {
1918 GnomeVFSURI *uri = handle->uri;
1919 gchar *extra_headers;
1921 gnome_vfs_uri_ref(uri);
1922 http_handle_close (handle, context);
1923 extra_headers = g_strdup_printf("Range: bytes=%" G_GINT64_FORMAT "-\r\n",(gint64)handle->offset);
1924 result = make_request (handle, uri, "GET", NULL, extra_headers,
1926 g_free (extra_headers);
1927 gnome_vfs_uri_unref(uri);
1928 if (result != GNOME_VFS_OK) {
1929 /* FIXME: 'method_handle' is now broken! */
1930 memset(handle, 0, sizeof (*handle));
1933 handle->socket_buffer_offset = handle->offset;
1936 result = gnome_vfs_socket_buffer_read (handle->socket_buffer, buffer,
1937 num_bytes, bytes_read);
1939 if (*bytes_read == 0) {
1940 return GNOME_VFS_ERROR_EOF;
1943 handle->socket_buffer_offset += *bytes_read;
1944 handle->offset += *bytes_read;
1946 DEBUG_HTTP (("-Read (%d)", result));
1947 ANALYZE_HTTP ("==> -do_read");
1952 static GnomeVFSResult
1953 do_seek (GnomeVFSMethod *method,
1954 GnomeVFSMethodHandle *method_handle,
1955 GnomeVFSSeekPosition whence,
1956 GnomeVFSFileOffset offset,
1957 GnomeVFSContext *context)
1959 HttpFileHandle *handle;
1961 handle = (HttpFileHandle *) method_handle;
1963 if (handle->to_be_written != NULL)
1964 return GNOME_VFS_ERROR_NOT_SUPPORTED;
1967 case GNOME_VFS_SEEK_START:
1968 handle->offset = offset;
1970 case GNOME_VFS_SEEK_CURRENT:
1971 handle->offset += offset;
1973 case GNOME_VFS_SEEK_END:
1974 if (!(handle->file_info->flags & GNOME_VFS_FILE_INFO_FIELDS_SIZE))
1975 return GNOME_VFS_ERROR_NOT_SUPPORTED;
1976 handle->offset = handle->file_info->size + offset;
1979 g_return_val_if_reached(GNOME_VFS_ERROR_NOT_SUPPORTED);
1982 return GNOME_VFS_OK;
1985 static GnomeVFSResult
1986 do_tell (GnomeVFSMethod *method,
1987 GnomeVFSMethodHandle *method_handle,
1988 GnomeVFSFileOffset *offset_return)
1990 HttpFileHandle *handle;
1992 handle = (HttpFileHandle *) method_handle;
1994 *offset_return = handle->offset;
1996 return GNOME_VFS_OK;
1999 /* Directory handling - WebDAV servers only */
2002 process_propfind_propstat (xmlNodePtr node,
2003 GnomeVFSFileInfo *file_info)
2006 gboolean treat_as_directory;
2008 treat_as_directory = FALSE;
2010 while (node != NULL) {
2011 if (strcmp ((char *)node->name, "prop") != 0) {
2012 /* node name != "prop" - prop is all we care about */
2016 /* properties of the file */
2017 l = node->xmlChildrenNode;
2019 char *node_content_xml = xmlNodeGetContent(l);
2020 if (node_content_xml) {
2021 if (strcmp ((char *)l->name, "getcontenttype") == 0) {
2023 file_info->valid_fields |=
2024 GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE;
2026 if (!file_info->mime_type) {
2027 file_info->mime_type = strip_semicolon (node_content_xml);
2029 } else if (strcmp ((char *)l->name, "getcontentlength") == 0){
2030 file_info->valid_fields |=
2031 GNOME_VFS_FILE_INFO_FIELDS_SIZE;
2032 file_info->size = atoi(node_content_xml);
2033 } else if (strcmp((char *)l->name, "getlastmodified") == 0) {
2034 if (gnome_vfs_atotm (node_content_xml, &(file_info->mtime))) {
2035 file_info->ctime = file_info->mtime;
2036 file_info->valid_fields |=
2037 GNOME_VFS_FILE_INFO_FIELDS_MTIME
2038 | GNOME_VFS_FILE_INFO_FIELDS_CTIME;
2041 /* Unfortunately, we don't have a mapping for "creationdate" */
2043 xmlFree (node_content_xml);
2044 node_content_xml = NULL;
2046 if (strcmp ((char *)l->name, "resourcetype") == 0) {
2047 file_info->valid_fields |=
2048 GNOME_VFS_FILE_INFO_FIELDS_TYPE;
2049 file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
2051 if (l->xmlChildrenNode && l->xmlChildrenNode->name
2052 && strcmp ((char *)l->xmlChildrenNode->name, "collection") == 0) {
2053 file_info->type = GNOME_VFS_FILE_TYPE_DIRECTORY;
2061 /* If this is a DAV collection, do we tell nautilus to treat it
2062 * as a directory or as a web page?
2064 if (file_info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE
2065 && file_info->type == GNOME_VFS_FILE_TYPE_DIRECTORY) {
2066 g_free (file_info->mime_type);
2067 if (treat_as_directory) {
2068 file_info->mime_type = g_strdup ("x-directory/webdav-prefer-directory");
2069 file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE;
2071 file_info->mime_type = g_strdup ("x-directory/webdav");
2072 file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE;
2077 if ((file_info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE) == 0) {
2078 file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE;
2079 file_info->mime_type = g_strdup (gnome_vfs_mime_type_from_name_or_default (file_info->name, "text/plain"));
2082 if ((file_info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE) == 0) {
2083 /* Is this a reasonable assumption ? */
2084 file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_TYPE;
2085 file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
2089 /* a strcmp that doesn't barf on NULLs */
2091 null_handling_strcmp (const char *a, const char *b)
2093 if ((a == NULL) != (b == NULL)) {
2097 if (a == NULL && b == NULL) {
2101 return strcmp (a, b);
2106 unescape_unreserved_chars (const char *in_string)
2108 /* RFC 2396 section 2.2 */
2109 static const char * reserved_chars = "%;/?:@&=+$,";
2111 char *ret, *write_char;
2112 const char * read_char;
2114 if (in_string == NULL) {
2118 ret = g_new (char, strlen (in_string) + 1);
2120 for (read_char = in_string, write_char = ret ; *read_char != '\0' ; read_char++) {
2121 if (read_char[0] == '%'
2122 && g_ascii_isxdigit (read_char[1])
2123 && g_ascii_isxdigit (read_char[2])) {
2126 unescaped = (g_ascii_xdigit_value (read_char[1]) << 4) | g_ascii_xdigit_value (read_char[2]);
2128 if (strchr (reserved_chars, unescaped)) {
2129 *write_char++ = *read_char++;
2130 *write_char++ = *read_char++;
2131 *write_char++ = *read_char; /*The last ++ is done in the for statement */
2133 *write_char++ = unescaped;
2134 read_char += 2; /*The last ++ is done in the for statement */
2137 *write_char++ = *read_char;
2140 *write_char++ = '\0';
2147 find_child_node_named (xmlNodePtr node,
2148 const char *child_node_name)
2152 child = node->xmlChildrenNode;
2154 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
2155 if (0 == strcmp (child->name, child_node_name)) {
2163 /* Look for a <status> tag in the children of node, and returns
2164 * the corresponding (HTTP) error code
2167 get_status_node (xmlNodePtr node, guint *status_code)
2169 xmlNodePtr status_node;
2170 char *status_string;
2173 status_node = find_child_node_named (node, "status");
2175 if (status_node != NULL) {
2176 status_string = xmlNodeGetContent (status_node);
2177 ret = parse_status (status_string, status_code);
2178 xmlFree (status_string);
2186 static GnomeVFSFileInfo *
2187 process_propfind_response(xmlNodePtr n,
2188 GnomeVFSURI *base_uri)
2190 GnomeVFSFileInfo *file_info = defaults_file_info_new ();
2191 GnomeVFSURI *second_base = gnome_vfs_uri_append_path (base_uri, "/");
2194 file_info->valid_fields = GNOME_VFS_FILE_INFO_FIELDS_NONE;
2197 if (strcmp ((char *)n->name, "href") == 0) {
2198 char *nodecontent = xmlNodeGetContent (n);
2201 rv = gnome_vfs_remove_optional_escapes (nodecontent);
2203 if (nodecontent != NULL && *nodecontent != '\0' && rv == GNOME_VFS_OK) {
2205 GnomeVFSURI *uri = gnome_vfs_uri_new (nodecontent);
2208 if ((0 == null_handling_strcmp (base_uri->text, uri->text)) ||
2209 (0 == null_handling_strcmp (second_base->text, uri->text))) {
2210 file_info->name = NULL; /* this file is the . directory */
2212 if (file_info->name != NULL) {
2213 /* Don't leak if a (potentially malicious)
2214 * server returns several href in its answer
2216 g_free (file_info->name);
2218 file_info->name = gnome_vfs_uri_extract_short_name (uri);
2219 if (file_info->name != NULL) {
2220 len = strlen (file_info->name) -1;
2221 if (file_info->name[len] == '/') {
2222 /* trim trailing `/` - it confuses stuff */
2223 file_info->name[len] = '\0';
2226 g_warning ("Invalid filename in PROPFIND '%s'; silently skipping", nodecontent);
2229 gnome_vfs_uri_unref (uri);
2231 g_warning ("Can't make URI from href in PROPFIND '%s'; silently skipping", nodecontent);
2234 g_warning ("got href without contents in PROPFIND response");
2237 xmlFree (nodecontent);
2238 } else if (strcmp ((char *)n->name, "propstat") == 0) {
2239 if (get_status_node (n, &status_code) && status_code == 200) {
2240 process_propfind_propstat (n->xmlChildrenNode, file_info);
2246 gnome_vfs_uri_unref (second_base);
2253 static GnomeVFSResult
2254 make_propfind_request (HttpFileHandle *handle,
2257 GnomeVFSContext *context)
2259 GnomeVFSResult result = GNOME_VFS_OK;
2260 GnomeVFSFileSize bytes_read, num_bytes=(64*1024);
2261 char *buffer = g_malloc(num_bytes);
2262 xmlParserCtxtPtr parserContext;
2263 xmlDocPtr doc = NULL;
2264 xmlNodePtr cur = NULL;
2265 char *extraheaders = g_strdup_printf("Depth: %d\r\n", depth);
2266 gboolean found_root_node_props;
2267 gboolean handle_valid = FALSE;
2269 GByteArray *request = g_byte_array_new();
2270 char *request_str = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
2271 "<D:propfind xmlns:D=\"DAV:\" >"
2274 "<D:getcontentlength/>"
2275 "<D:getcontenttype/>"
2276 "<D:getlastmodified/>"
2281 ANALYZE_HTTP ("==> +make_propfind_request");
2283 request = g_byte_array_append(request, request_str,
2284 strlen(request_str));
2286 parserContext = xmlCreatePushParserCtxt(NULL, NULL, "", 0, "PROPFIND");
2289 http_cache_invalidate_uri_and_children (uri);
2292 result = make_request (handle, uri, "PROPFIND", request,
2293 extraheaders, context);
2294 if (result == GNOME_VFS_OK)
2295 handle_valid = TRUE;
2297 /* FIXME bugzilla.gnome.org 43834: It looks like some http
2298 * servers (eg, www.yahoo.com) treat PROPFIND as a GET and
2299 * return a 200 OK. Others may return access denied errors or
2300 * redirects or any other legal response. This case probably
2301 * needs to be made more robust.
2303 if (result == GNOME_VFS_OK && handle->server_status != 207) { /* Multi-Status */
2304 DEBUG_HTTP (("HTTP server returned an invalid PROPFIND response: %d", handle->server_status));
2305 result = GNOME_VFS_ERROR_NOT_SUPPORTED;
2308 if (result == GNOME_VFS_OK) {
2310 result = do_read (NULL, (GnomeVFSMethodHandle *) handle,
2311 buffer, num_bytes, &bytes_read, context);
2313 if (result != GNOME_VFS_OK ) {
2316 xmlParseChunk (parserContext, buffer, bytes_read, 0);
2317 buffer[bytes_read]=0;
2318 } while (bytes_read > 0);
2321 if (result == GNOME_VFS_ERROR_EOF) {
2322 result = GNOME_VFS_OK;
2325 if (result != GNOME_VFS_OK) {
2329 xmlParseChunk (parserContext, "", 0, 1);
2331 doc = parserContext->myDoc;
2333 result = GNOME_VFS_ERROR_GENERIC;
2337 cur = doc->xmlRootNode;
2338 if (strcmp ((char *)cur->name, "multistatus") != 0) {
2339 DEBUG_HTTP (("Couldn't find <multistatus>.\n"));
2340 result = GNOME_VFS_ERROR_GENERIC;
2343 cur = cur->xmlChildrenNode;
2345 found_root_node_props = FALSE;
2346 while (cur != NULL) {
2347 if (strcmp ((char *)cur->name, "response") == 0) {
2348 GnomeVFSFileInfo *file_info;
2351 /* Some webdav servers (eg resin) put the HTTP status
2352 * code for PROPFIND request in the xml answer instead
2353 * of directly sending a 404
2355 if (get_status_node (cur, &status) && !HTTP_20X(status)) {
2356 result = http_status_to_vfs_result (status);
2361 process_propfind_response (cur->xmlChildrenNode, uri);
2363 if (file_info->name != NULL) {
2364 handle->files = g_list_append (handle->files, file_info);
2366 /* This response refers to the root node */
2367 /* Abandon the old information that came from create_handle*/
2369 file_info->name = handle->file_info->name;
2370 handle->file_info->name = NULL;
2371 gnome_vfs_file_info_unref (handle->file_info);
2372 handle->file_info = file_info;
2373 found_root_node_props = TRUE;
2377 DEBUG_HTTP(("expecting <response> got <%s>\n", cur->name));
2382 if (!found_root_node_props) {
2383 DEBUG_HTTP (("Failed to find root request node properties during propfind"));
2384 result = GNOME_VFS_ERROR_GENERIC;
2390 * Section 8.1, final line
2391 * "The results of this method [PROPFIND] SHOULD NOT be cached"
2392 * Well, at least its not "MUST NOT"
2396 http_cache_add_uri (uri, handle->file_info, TRUE);
2398 http_cache_add_uri_and_children (uri, handle->file_info, handle->files);
2403 g_free(extraheaders);
2404 xmlFreeParserCtxt(parserContext);
2406 if (result != GNOME_VFS_OK && handle_valid) {
2407 http_handle_close (handle, context);
2410 ANALYZE_HTTP ("==> -make_propfind_request");
2415 static GnomeVFSResult
2416 do_open_directory(GnomeVFSMethod *method,
2417 GnomeVFSMethodHandle **method_handle,
2419 GnomeVFSFileInfoOptions options,
2420 GnomeVFSContext *context)
2422 /* TODO move to using the gnome_vfs_file_info_list family of functions */
2423 GnomeVFSResult result;
2424 HttpFileHandle *handle = NULL;
2425 GnomeVFSFileInfo * file_info_cached;
2426 GList *child_file_info_cached_list = NULL;
2428 ANALYZE_HTTP ("==> +do_open_directory");
2429 DEBUG_HTTP (("+Open_Directory options: %d URI: '%s'", options, gnome_vfs_uri_to_string (uri, 0)));
2431 /* Check the cache--is this even a directory?
2432 * (Nautilus, in particular, seems to like to make this call on non directories
2435 file_info_cached = http_cache_check_uri (uri);
2437 if (file_info_cached) {
2438 if (GNOME_VFS_FILE_TYPE_DIRECTORY != file_info_cached->type) {
2439 ANALYZE_HTTP ("==> Cache Hit (Negative)");
2440 gnome_vfs_file_info_unref (file_info_cached);
2441 result = GNOME_VFS_ERROR_NOT_A_DIRECTORY;
2444 gnome_vfs_file_info_unref (file_info_cached);
2445 file_info_cached = NULL;
2449 /* The check for directory contents is more stringent */
2450 file_info_cached = http_cache_check_directory_uri (uri, &child_file_info_cached_list);
2452 if (file_info_cached) {
2453 handle = g_new (HttpFileHandle, 1);
2454 http_file_handle_new (handle, NULL, uri);
2455 gnome_vfs_file_info_unref (handle->file_info);
2456 handle->file_info = file_info_cached;
2457 handle->files = child_file_info_cached_list;
2458 result = GNOME_VFS_OK;
2460 handle = g_new (HttpFileHandle, 1);
2461 result = make_propfind_request(handle, uri, 1, context);
2462 /* mfleming -- is this necessary? Most DAV server's I've seen don't have the horrible
2463 * lack-of-trailing-/-is-a-301 problem for PROPFIND's
2465 if (result == GNOME_VFS_ERROR_NOT_FOUND) { /* 404 not found */
2466 if (uri->text != NULL && *uri->text != '\0'
2467 && uri->text[strlen (uri->text) - 1] != '/') {
2468 GnomeVFSURI *tmpuri = gnome_vfs_uri_append_path (uri, "/");
2469 result = do_open_directory (method, (GnomeVFSMethodHandle **)&handle, tmpuri, options, context);
2470 gnome_vfs_uri_unref (tmpuri);
2475 if (result == GNOME_VFS_OK
2476 && handle->file_info->type != GNOME_VFS_FILE_TYPE_DIRECTORY) {
2477 result = GNOME_VFS_ERROR_NOT_A_DIRECTORY;
2478 http_handle_close (handle, context);
2484 *method_handle = (GnomeVFSMethodHandle *)handle;
2487 DEBUG_HTTP (("-Open_Directory (%d) handle:0x%08x", result, (unsigned int)handle));
2488 ANALYZE_HTTP ("==> -do_open_directory");
2493 static GnomeVFSResult
2494 do_close_directory (GnomeVFSMethod *method,
2495 GnomeVFSMethodHandle *method_handle,
2496 GnomeVFSContext *context)
2498 HttpFileHandle *handle;
2500 DEBUG_HTTP (("+Close_Directory"));
2502 handle = (HttpFileHandle *) method_handle;
2504 http_handle_close(handle, context);
2507 DEBUG_HTTP (("-Close_Directory (0) handle:0x%08x", (unsigned int) method_handle));
2509 return GNOME_VFS_OK;
2512 static GnomeVFSResult
2513 do_read_directory (GnomeVFSMethod *method,
2514 GnomeVFSMethodHandle *method_handle,
2515 GnomeVFSFileInfo *file_info,
2516 GnomeVFSContext *context)
2518 HttpFileHandle *handle;
2519 GnomeVFSResult result;
2521 DEBUG_HTTP (("+Read_Directory handle:0x%08x", (unsigned int) method_handle));
2523 handle = (HttpFileHandle *) method_handle;
2525 if (handle->files && g_list_length (handle->files)) {
2526 GnomeVFSFileInfo *original_info = g_list_nth_data (handle->files, 0);
2527 gboolean found_entry = FALSE;
2529 /* mfleming -- Why is this check here? Does anyone set original_info->name to NULL? */
2530 if (original_info->name != NULL && original_info->name[0]) {
2531 gnome_vfs_file_info_copy (file_info, original_info);
2535 /* remove our GnomeVFSFileInfo from the list */
2536 handle->files = g_list_remove (handle->files, original_info);
2537 gnome_vfs_file_info_unref (original_info);
2539 /* mfleming -- Is this necessary? */
2541 result = GNOME_VFS_OK;
2543 result = do_read_directory (method, method_handle, file_info, context);
2546 result = GNOME_VFS_ERROR_EOF;
2549 DEBUG_HTTP (("-Read_Directory (%d)", result));
2554 static GnomeVFSResult
2555 do_get_file_info (GnomeVFSMethod *method,
2557 GnomeVFSFileInfo *file_info,
2558 GnomeVFSFileInfoOptions options,
2559 GnomeVFSContext *context)
2561 HttpFileHandle *handle;
2562 GnomeVFSResult result;
2563 GnomeVFSFileInfo * file_info_cached;
2565 ANALYZE_HTTP ("==> +do_get_file_info");
2566 DEBUG_HTTP (("+Get_File_Info options: %d URI: '%s'", options, gnome_vfs_uri_to_string( uri, 0)));
2568 file_info_cached = http_cache_check_uri (uri);
2570 if (file_info_cached != NULL) {
2571 gnome_vfs_file_info_copy (file_info, file_info_cached);
2572 gnome_vfs_file_info_unref (file_info_cached);
2573 ANALYZE_HTTP ("==> Cache Hit");
2574 result = GNOME_VFS_OK;
2577 * Start off by making a PROPFIND request. Fall back to a HEAD if it fails
2580 handle = g_new (HttpFileHandle, 1);
2581 result = make_propfind_request (handle, uri, 0, context);
2583 /* Note that theoretically we could not bother with this request if we get a 404 back,
2584 * but since some servers seem to return wierd things on PROPFIND (mostly 200 OK's...)
2585 * I'm not going to count on the PROPFIND response....
2587 if (result == GNOME_VFS_OK) {
2588 gnome_vfs_file_info_copy (file_info, handle->file_info);
2589 http_handle_close (handle, context);
2595 g_assert (handle == NULL); /* Make sure we're not leaking some old one */
2597 /* Lame buggy servers (eg: www.mozilla.org,
2598 * www.corel.com)) return an HTTP error for a
2599 * HEAD where a GET would succeed. In these
2600 * cases lets try to do a GET.
2602 if (result != GNOME_VFS_OK) {
2603 g_assert (handle == NULL); /* Make sure we're not leaking some old one */
2605 ANALYZE_HTTP ("==> do_get_file_info: do GET ");
2607 handle = g_new (HttpFileHandle, 1);
2608 result = make_request (handle, uri, "GET", NULL, NULL, context);
2609 if (result == GNOME_VFS_OK) {
2610 gnome_vfs_file_info_copy (file_info, handle->file_info);
2611 http_cache_add_uri (uri, handle->file_info, FALSE);
2612 http_handle_close (handle, context);
2617 /* If we get a redirect, we should be
2618 * basing the MIME type on the type of
2619 * the page we'll be redirected
2620 * too. Maybe we even want to take the
2621 * "follow_links" setting into account.
2623 /* FIXME: For now we treat all
2624 * redirects as if they lead to a
2625 * text/html. That works pretty well,
2626 * but it's not correct.
2628 if (handle != NULL && HTTP_REDIRECTED (handle->server_status)) {
2629 g_free (file_info->mime_type);
2630 file_info->mime_type = g_strdup ("text/html");
2634 if (result == GNOME_VFS_ERROR_NOT_FOUND) { /* 404 not found */
2635 /* FIXME bugzilla.gnome.org 43835: mfleming: Is this code really appropriate?
2636 * In any case, it doesn't seem to be appropriate for a DAV-enabled
2637 * server, since they don't seem to send 301's when you PROPFIND collections
2638 * without a trailing '/'.
2640 if (uri->text != NULL && *uri->text != '\0'
2641 && uri->text[strlen(uri->text)-1] != '/') {
2642 GnomeVFSURI *tmpuri = gnome_vfs_uri_append_path (uri, "/");
2644 result = do_get_file_info (method, tmpuri, file_info, options, context);
2645 gnome_vfs_uri_unref (tmpuri);
2651 DEBUG_HTTP (("-Get_File_Info (%d)", result));
2652 ANALYZE_HTTP ("==> -do_get_file_info");
2657 static GnomeVFSResult
2658 do_get_file_info_from_handle (GnomeVFSMethod *method,
2659 GnomeVFSMethodHandle *method_handle,
2660 GnomeVFSFileInfo *file_info,
2661 GnomeVFSFileInfoOptions options,
2662 GnomeVFSContext *context)
2664 HttpFileHandle *handle;
2666 DEBUG_HTTP (("+Get_File_Info_From_Handle"));
2668 handle = (HttpFileHandle *) method_handle;
2670 gnome_vfs_file_info_copy (file_info, handle->file_info);
2672 DEBUG_HTTP (("-Get_File_Info_From_Handle"));
2674 return GNOME_VFS_OK;
2678 do_is_local (GnomeVFSMethod *method,
2679 const GnomeVFSURI *uri)
2681 DEBUG_HTTP (("+Is_Local"));
2685 static GnomeVFSResult
2686 do_make_directory (GnomeVFSMethod *method,
2689 GnomeVFSContext *context)
2691 /* MKCOL /path HTTP/1.0 */
2693 HttpFileHandle *handle;
2694 GnomeVFSResult result;
2696 DEBUG_HTTP (("+Make_Directory URI: '%s'", gnome_vfs_uri_to_string (uri, 0)));
2697 ANALYZE_HTTP ("==> +do_make_directory");
2700 * MKCOL returns a 405 if you try to MKCOL on something that
2701 * already exists. Of course, we don't know whether that means that
2702 * the server doesn't support DAV or the collection already exists.
2703 * So we do a PROPFIND first to find out
2705 /* FIXME check cache here */
2706 handle = g_new (HttpFileHandle, 1);
2707 result = make_propfind_request(handle, uri, 0, context);
2709 if (result == GNOME_VFS_OK) {
2710 result = GNOME_VFS_ERROR_FILE_EXISTS;
2712 /* Make sure we're not leaking an old one */
2713 g_assert (handle == NULL);
2715 if (result == GNOME_VFS_ERROR_NOT_FOUND) {
2716 http_cache_invalidate_uri_parent (uri);
2717 handle = g_new (HttpFileHandle, 1);
2718 result = make_request (handle, uri, "MKCOL", NULL, NULL, context);
2721 http_handle_close (handle, context);
2724 if (result == GNOME_VFS_ERROR_NOT_FOUND) {
2725 result = resolve_409 (method, uri, context);
2728 ANALYZE_HTTP ("==> -do_make_directory");
2729 DEBUG_HTTP (("-Make_Directory (%d)", result));
2734 static GnomeVFSResult
2735 do_remove_directory(GnomeVFSMethod *method,
2737 GnomeVFSContext *context)
2739 /* DELETE /path HTTP/1.0 */
2740 HttpFileHandle *handle;
2741 GnomeVFSResult result;
2743 ANALYZE_HTTP ("==> +do_remove_directory");
2744 DEBUG_HTTP (("+Remove_Directory URI: '%s'", gnome_vfs_uri_to_string (uri, 0)));
2746 http_cache_invalidate_uri_parent (uri);
2748 /* FIXME this should return GNOME_VFS_ERROR_DIRECTORY_NOT_EMPTY if the
2749 * directory is not empty
2751 handle = g_new (HttpFileHandle, 1);
2752 result = make_request (handle, uri, "DELETE", NULL, NULL,
2754 http_handle_close (handle, context);
2757 DEBUG_HTTP (("-Remove_Directory (%d)", result));
2758 ANALYZE_HTTP ("==> -do_remove_directory");
2764 is_same_fs (const GnomeVFSURI *a,
2765 const GnomeVFSURI *b)
2767 return null_handling_strcmp (gnome_vfs_uri_get_scheme (a), gnome_vfs_uri_get_scheme (b)) == 0
2768 && null_handling_strcmp (gnome_vfs_uri_get_host_name (a), gnome_vfs_uri_get_host_name (b)) == 0
2769 && null_handling_strcmp (gnome_vfs_uri_get_user_name (a), gnome_vfs_uri_get_user_name (b)) == 0
2770 && null_handling_strcmp (gnome_vfs_uri_get_password (a), gnome_vfs_uri_get_password (b)) == 0
2771 && (gnome_vfs_uri_get_host_port (a) == gnome_vfs_uri_get_host_port (b));
2774 static GnomeVFSResult
2775 do_move (GnomeVFSMethod *method,
2776 GnomeVFSURI *old_uri,
2777 GnomeVFSURI *new_uri,
2778 gboolean force_replace,
2779 GnomeVFSContext *context)
2783 * MOVE /path1 HTTP/1.0
2784 * Destination: /path2
2788 HttpFileHandle *handle;
2789 GnomeVFSResult result;
2791 char *destpath, *destheader;
2793 ANALYZE_HTTP ("==> +do_move");
2794 DEBUG_HTTP (("+Move URI: '%s' Dest: '%s'",
2795 gnome_vfs_uri_to_string (old_uri, 0),
2796 gnome_vfs_uri_to_string (new_uri, 0)));
2798 if (!is_same_fs (old_uri, new_uri)) {
2799 return GNOME_VFS_ERROR_NOT_SAME_FILE_SYSTEM;
2802 destpath = gnome_vfs_uri_to_string (new_uri, GNOME_VFS_URI_HIDE_USER_NAME|GNOME_VFS_URI_HIDE_PASSWORD);
2803 destheader = g_strdup_printf ("Destination: %s\r\nOverwrite: %c\r\n", destpath, force_replace ? 'T' : 'F' );
2805 handle = g_new (HttpFileHandle, 1);
2806 result = make_request (handle, old_uri, "MOVE", NULL, destheader, context);
2807 http_handle_close (handle, context);
2811 if (result == GNOME_VFS_ERROR_NOT_FOUND) {
2812 result = resolve_409 (method, new_uri, context);
2815 http_cache_invalidate_uri_parent (old_uri);
2816 http_cache_invalidate_uri_parent (new_uri);
2818 DEBUG_HTTP (("-Move (%d)", result));
2819 ANALYZE_HTTP ("==> -do_move");
2825 static GnomeVFSResult
2826 do_unlink(GnomeVFSMethod *method,
2828 GnomeVFSContext *context)
2830 GnomeVFSResult result;
2832 /* FIXME need to make sure this fails on directories */
2833 ANALYZE_HTTP ("==> +do_unlink");
2834 DEBUG_HTTP (("+Unlink URI: '%s'", gnome_vfs_uri_to_string (uri, 0)));
2835 result = do_remove_directory (method, uri, context);
2836 DEBUG_HTTP (("-Unlink (%d)", result));
2837 ANALYZE_HTTP ("==> -do_unlink");
2842 static GnomeVFSResult
2843 do_check_same_fs (GnomeVFSMethod *method,
2846 gboolean *same_fs_return,
2847 GnomeVFSContext *context)
2849 *same_fs_return = is_same_fs (a, b);
2851 return GNOME_VFS_OK;
2854 static GnomeVFSResult
2855 do_set_file_info (GnomeVFSMethod *method,
2857 const GnomeVFSFileInfo *info,
2858 GnomeVFSSetFileInfoMask mask,
2859 GnomeVFSContext *context)
2861 GnomeVFSURI *parent_uri, *new_uri;
2862 GnomeVFSResult result;
2864 /* FIXME: For now, we only support changing the name. */
2865 if ((mask & ~(GNOME_VFS_SET_FILE_INFO_NAME)) != 0) {
2866 return GNOME_VFS_ERROR_NOT_SUPPORTED;
2869 /* FIXME bugzillagnome.org 40645: Make sure this returns an
2870 * error for incoming names with "/" characters in them,
2871 * instead of moving the file.
2874 /* Share code with do_move. */
2875 parent_uri = gnome_vfs_uri_get_parent (uri);
2876 if (parent_uri == NULL) {
2877 return GNOME_VFS_ERROR_NOT_FOUND;
2879 new_uri = gnome_vfs_uri_append_file_name (parent_uri, info->name);
2880 gnome_vfs_uri_unref (parent_uri);
2881 result = do_move (method, uri, new_uri, FALSE, context);
2882 gnome_vfs_uri_unref (new_uri);
2886 static GnomeVFSMethod method = {
2887 sizeof (GnomeVFSMethod),
2895 NULL, /* truncate_handle */
2900 do_get_file_info_from_handle,
2903 do_remove_directory,
2908 NULL, /* truncate */
2909 NULL, /* find_directory */
2910 NULL /* create_symbolic_link */
2914 vfs_module_init (const char *method_name,
2917 GError *gconf_error = NULL;
2919 gboolean use_proxy_auth;
2923 gl_client = gconf_client_get_default ();
2925 gl_mutex = g_mutex_new ();
2927 gconf_client_add_dir (gl_client, PATH_GCONF_GNOME_VFS, GCONF_CLIENT_PRELOAD_ONELEVEL, &gconf_error);
2929 DEBUG_HTTP (("GConf error during client_add_dir '%s'", gconf_error->message));
2930 g_error_free (gconf_error);
2934 gconf_client_notify_add (gl_client, PATH_GCONF_GNOME_VFS, notify_gconf_value_changed, NULL, NULL, &gconf_error);
2936 DEBUG_HTTP (("GConf error during notify_error '%s'", gconf_error->message));
2937 g_error_free (gconf_error);
2941 /* Load the http proxy setting */
2942 use_proxy = gconf_client_get_bool (gl_client, KEY_GCONF_USE_HTTP_PROXY, &gconf_error);
2944 if (gconf_error != NULL) {
2945 DEBUG_HTTP (("GConf error during client_get_bool '%s'", gconf_error->message));
2946 g_error_free (gconf_error);
2949 construct_gl_http_proxy (use_proxy);
2952 use_proxy_auth = gconf_client_get_bool (gl_client, KEY_GCONF_HTTP_USE_AUTH, &gconf_error);
2954 if (gconf_error != NULL) {
2955 DEBUG_HTTP (("GConf error during client_get_bool '%s'", gconf_error->message));
2956 g_error_free (gconf_error);
2959 set_proxy_auth (use_proxy_auth);
2969 vfs_module_shutdown (GnomeVFSMethod *method)
2971 g_object_unref (G_OBJECT (gl_client));
2973 http_authn_shutdown ();
2975 http_cache_shutdown();
2977 g_mutex_free (gl_mutex);
2982 /* A "409 Conflict" currently maps to GNOME_VFS_ERROR_NOT_FOUND because it can be returned
2983 * when the parent collection/directory does not exist. Unfortunately, Xythos also returns
2984 * this code when the destination filename of a PUT, MKCOL, or MOVE contains illegal characters,
2985 * eg "my*file:name".
2987 * The only way to resolve this is to ask...
2990 static GnomeVFSResult
2991 resolve_409 (GnomeVFSMethod *method, GnomeVFSURI *uri, GnomeVFSContext *context)
2993 GnomeVFSFileInfo *file_info;
2994 GnomeVFSURI *parent_dest_uri;
2995 GnomeVFSResult result;
2998 ANALYZE_HTTP ("==> +resolving 409");
3000 file_info = gnome_vfs_file_info_new ();
3001 parent_dest_uri = gnome_vfs_uri_get_parent (uri);
3003 if (parent_dest_uri != NULL) {
3004 result = do_get_file_info (method,
3007 GNOME_VFS_FILE_INFO_DEFAULT,
3010 gnome_vfs_file_info_unref (file_info);
3013 gnome_vfs_uri_unref (parent_dest_uri);
3014 parent_dest_uri = NULL;
3016 result = GNOME_VFS_ERROR_NOT_FOUND;
3019 if (result == GNOME_VFS_OK) {
3020 /* The destination filename contains characters that are not allowed
3021 * by the server. This is a bummer mapping, but EINVAL is what
3022 * the Linux FAT filesystems return on bad filenames, so at least
3023 * its not without precedent...
3025 result = GNOME_VFS_ERROR_BAD_PARAMETERS;
3027 /* The destination's parent path does not exist */
3028 result = GNOME_VFS_ERROR_NOT_FOUND;
3031 ANALYZE_HTTP ("==> -resolving 409");
3037 invoke_callback_headers_received (HttpFileHandle *handle)
3039 GnomeVFSModuleCallbackReceivedHeadersIn in_args;
3040 GnomeVFSModuleCallbackReceivedHeadersOut out_args;
3041 gboolean ret = FALSE;
3043 memset (&in_args, 0, sizeof (in_args));
3044 memset (&out_args, 0, sizeof (out_args));
3046 in_args.uri = handle->uri;
3047 in_args.headers = handle->response_headers;
3049 ret = gnome_vfs_module_callback_invoke (GNOME_VFS_MODULE_CALLBACK_HTTP_RECEIVED_HEADERS,
3050 &in_args, sizeof (in_args),
3051 &out_args, sizeof (out_args));
3057 invoke_callback_send_additional_headers (GnomeVFSURI *uri,
3060 GnomeVFSModuleCallbackAdditionalHeadersIn in_args;
3061 GnomeVFSModuleCallbackAdditionalHeadersOut out_args;
3062 gboolean ret = FALSE;
3064 memset (&in_args, 0, sizeof (in_args));
3065 memset (&out_args, 0, sizeof (out_args));
3069 ret = gnome_vfs_module_callback_invoke (GNOME_VFS_MODULE_CALLBACK_HTTP_SEND_ADDITIONAL_HEADERS,
3070 &in_args, sizeof (in_args),
3071 &out_args, sizeof (out_args));
3074 *headers = out_args.headers;
3078 if (out_args.headers) {
3079 g_list_foreach (out_args.headers, (GFunc)g_free, NULL);
3080 g_list_free (out_args.headers);
3089 invoke_callback_basic_authn (HttpFileHandle *handle,
3090 enum AuthnHeaderType authn_which,
3091 gboolean previous_attempt_failed)
3093 GnomeVFSModuleCallbackAuthenticationIn in_args;
3094 GnomeVFSModuleCallbackAuthenticationOut out_args;
3099 memset (&in_args, 0, sizeof (in_args));
3100 memset (&out_args, 0, sizeof (out_args));
3102 in_args.previous_attempt_failed = previous_attempt_failed;
3104 in_args.uri = gnome_vfs_uri_to_string (handle->uri, GNOME_VFS_URI_HIDE_NONE);
3106 ret = http_authn_parse_response_header_basic (authn_which, handle->response_headers, &in_args.realm);
3112 DEBUG_HTTP (("Invoking %s authentication callback for uri %s",
3113 authn_which == AuthnHeader_WWW ? "basic" : "proxy", in_args.uri));
3115 in_args.auth_type = AuthTypeBasic;
3117 ret = gnome_vfs_module_callback_invoke (authn_which == AuthnHeader_WWW
3118 ? GNOME_VFS_MODULE_CALLBACK_AUTHENTICATION
3119 : GNOME_VFS_MODULE_CALLBACK_HTTP_PROXY_AUTHENTICATION,
3120 &in_args, sizeof (in_args),
3121 &out_args, sizeof (out_args));
3124 DEBUG_HTTP (("No callback registered"));
3128 ret = (out_args.username != NULL);
3131 DEBUG_HTTP (("No username provided by callback"));
3135 DEBUG_HTTP (("Back from authentication callback, adding credentials"));
3137 if (authn_which == AuthnHeader_WWW) {
3138 http_authn_session_add_credentials (handle->uri, out_args.username, out_args.password);
3139 } else /* if (authn_which == AuthnHeader_Proxy) */ {
3140 proxy_set_authn (out_args.username, out_args.password);
3143 g_free (in_args.uri);
3144 g_free (in_args.realm);
3145 g_free (out_args.username);
3146 g_free (out_args.password);
3152 strcmp_allow_nulls (const char *s1, const char *s2)
3154 return strcmp (s1 == NULL ? "" : s1, s2 == NULL ? "" : s2);
3158 /* Returns TRUE if the given URL has changed authentication credentials
3159 * from the last request (eg, another thread updated the authn information)
3160 * or if the application provided new credentials via a callback
3162 * prev_authn_header is NULL if the previous request contained no authn information.
3166 check_authn_retry_request (HttpFileHandle * http_handle,
3167 enum AuthnHeaderType authn_which,
3168 const char *prev_authn_header)
3171 char *current_authn_header;
3173 current_authn_header = NULL;
3175 g_mutex_lock (gl_mutex);
3177 if (authn_which == AuthnHeader_WWW) {
3178 current_authn_header = http_authn_get_header_for_uri (http_handle->uri);
3179 } else if (authn_which == AuthnHeader_Proxy) {
3180 current_authn_header = proxy_get_authn_header_for_uri_nolock (http_handle->uri);
3182 g_assert_not_reached ();
3186 if (0 == strcmp_allow_nulls (current_authn_header, prev_authn_header)) {
3187 ret = invoke_callback_basic_authn (http_handle, authn_which, prev_authn_header == NULL);
3192 g_mutex_unlock (gl_mutex);
3194 g_free (current_authn_header);
3201 http_util_get_utime (void)
3204 gettimeofday (&tmp, NULL);
3205 return (utime_t)tmp.tv_usec + ((gint64)tmp.tv_sec) * 1000000LL;
3209 /* BASE64 code ported from neon (http://www.webdav.org/neon) */
3210 static const gchar b64_alphabet[65] = {
3211 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
3212 "abcdefghijklmnopqrstuvwxyz"
3216 http_util_base64 (const gchar *text)
3218 /* The tricky thing about this is doing the padding at the end,
3219 * doing the bit manipulation requires a bit of concentration only */
3220 gchar *buffer, *point;
3223 /* Use 'buffer' to store the output. Work out how big it should be...
3224 * This must be a multiple of 4 bytes
3227 inlen = strlen (text);
3228 outlen = (inlen*4)/3;
3229 if ((inlen % 3) > 0) { /* got to pad */
3230 outlen += 4 - (inlen % 3);
3233 buffer = g_malloc (outlen + 1); /* +1 for the \0 */
3235 /* now do the main stage of conversion, 3 bytes at a time,
3236 * leave the trailing bytes (if there are any) for later
3239 for (point=buffer; inlen>=3; inlen-=3, text+=3) {
3240 *(point++) = b64_alphabet[ (*text)>>2 ];
3241 *(point++) = b64_alphabet[ ((*text)<<4 & 0x30) | (*(text+1))>>4 ];
3242 *(point++) = b64_alphabet[ ((*(text+1))<<2 & 0x3c) | (*(text+2))>>6 ];
3243 *(point++) = b64_alphabet[ (*(text+2)) & 0x3f ];
3246 /* Now deal with the trailing bytes */
3248 /* We always have one trailing byte */
3249 *(point++) = b64_alphabet[ (*text)>>2 ];
3250 *(point++) = b64_alphabet[ ( ((*text)<<4 & 0x30) |
3251 (inlen==2?(*(text+1))>>4:0) ) ];
3252 *(point++) = (inlen==1?'=':b64_alphabet[ (*(text+1))<<2 & 0x3c ] );
3256 /* Null-terminate */
3262 static gboolean at_least_one_test_failed = FALSE;
3265 test_failed (const char *format, ...)
3270 va_start (arguments, format);
3271 message = g_strdup_vprintf (format, arguments);
3274 g_message ("test failed: %s", message);
3275 at_least_one_test_failed = TRUE;
3278 #define VERIFY_BOOLEAN_RESULT(function, expected) \
3280 gboolean result = function; \
3281 if (! ((result && expected) || (!result && !expected))) { \
3282 test_failed ("%s: returned '%d' expected '%d'", #function, (int)result, (int)expected); \
3288 http_self_test (void)
3290 g_message ("self-test: http\n");
3292 VERIFY_BOOLEAN_RESULT (proxy_should_for_hostname ("localhost"), FALSE);
3293 VERIFY_BOOLEAN_RESULT (proxy_should_for_hostname ("LocalHost"), FALSE);
3294 VERIFY_BOOLEAN_RESULT (proxy_should_for_hostname ("127.0.0.1"), FALSE);
3295 VERIFY_BOOLEAN_RESULT (proxy_should_for_hostname ("127.127.0.1"), FALSE);
3296 VERIFY_BOOLEAN_RESULT (proxy_should_for_hostname ("www.yahoo.com"), TRUE);
3298 return !at_least_one_test_failed;
3301 gboolean vfs_module_self_test (void);
3304 vfs_module_self_test (void)
3310 ret = http_authn_self_test () && ret;
3312 ret = http_self_test () && ret;