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;
1519 gboolean handle_valid = FALSE;
1521 g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_INTERNAL);
1523 ANALYZE_HTTP ("==> +make_request");
1526 proxy_connect = FALSE;
1527 authn_header_request = NULL;
1528 authn_header_proxy = NULL;
1530 toplevel_uri = (GnomeVFSToplevelURI *) uri;
1535 g_free (authn_header_request);
1536 g_free (authn_header_proxy);
1538 socket_buffer = NULL;
1539 result = connect_to_uri (toplevel_uri, &socket_buffer,
1542 if (result != GNOME_VFS_OK) {
1546 request = build_request (method, toplevel_uri, proxy_connect);
1548 authn_header_request = http_authn_get_header_for_uri (uri);
1550 if (authn_header_request != NULL) {
1551 g_string_append (request, authn_header_request);
1554 if (proxy_connect) {
1555 authn_header_proxy = proxy_get_authn_header_for_uri (uri);
1557 if (authn_header_proxy != NULL) {
1558 g_string_append (request, authn_header_proxy);
1562 /* `Content-Length' header. */
1564 g_string_append_printf (request, "Content-Length: %d\r\n", data->len);
1567 /* Extra headers. */
1568 if (extra_headers != NULL) {
1569 g_string_append (request, extra_headers);
1572 /* Extra headers from user */
1575 if (invoke_callback_send_additional_headers (uri, &list)) {
1578 for (i = list; i; i = i->next) {
1579 g_string_append (request, i->data);
1587 /* Empty line ends header section. */
1588 g_string_append (request, "\r\n");
1590 result = xmit_request (socket_buffer, request, data);
1591 g_string_free (request, TRUE);
1594 if (result != GNOME_VFS_OK) {
1598 /* Read the headers and create our internal HTTP file handle. */
1599 result = create_handle (uri, socket_buffer, context, handle);
1600 handle_valid = TRUE;
1602 if (result == GNOME_VFS_OK) {
1603 socket_buffer = NULL;
1606 if (handle->server_status == HTTP_STATUS_UNAUTHORIZED) {
1607 if (! check_authn_retry_request (handle, AuthnHeader_WWW, authn_header_request)) {
1610 } else if (handle->server_status == HTTP_STATUS_PROXY_AUTH_REQUIRED) {
1611 if (! check_authn_retry_request (handle, AuthnHeader_WWW, authn_header_proxy)) {
1617 http_file_handle_destroy (handle);
1618 handle_valid = FALSE;
1621 g_free (authn_header_request);
1622 g_free (authn_header_proxy);
1624 if (result != GNOME_VFS_OK && handle_valid) {
1625 http_file_handle_destroy (handle);
1626 handle_valid = FALSE;
1629 if (request != NULL) {
1630 g_string_free (request, TRUE);
1633 if (socket_buffer != NULL) {
1634 gnome_vfs_socket_buffer_destroy (socket_buffer, TRUE);
1637 ANALYZE_HTTP ("==> -make_request");
1642 http_handle_close (HttpFileHandle *handle,
1643 GnomeVFSContext *context)
1645 ANALYZE_HTTP ("==> +http_handle_close");
1647 if (handle != NULL) {
1648 if (handle->socket_buffer) {
1649 gnome_vfs_socket_buffer_flush (handle->socket_buffer);
1650 gnome_vfs_socket_buffer_destroy (handle->socket_buffer,
1652 handle->socket_buffer = NULL;
1655 http_file_handle_destroy (handle);
1658 ANALYZE_HTTP ("==> -http_handle_close");
1661 static GnomeVFSResult
1662 do_open (GnomeVFSMethod *method,
1663 GnomeVFSMethodHandle **method_handle,
1665 GnomeVFSOpenMode mode,
1666 GnomeVFSContext *context)
1668 HttpFileHandle *handle;
1669 GnomeVFSResult result = GNOME_VFS_OK;
1671 g_return_val_if_fail (uri->parent == NULL, GNOME_VFS_ERROR_INVALID_URI);
1672 g_return_val_if_fail (!(mode & GNOME_VFS_OPEN_READ &&
1673 mode & GNOME_VFS_OPEN_WRITE),
1674 GNOME_VFS_ERROR_INVALID_OPEN_MODE);
1676 ANALYZE_HTTP ("==> +do_open");
1677 DEBUG_HTTP (("+Open URI: '%s' mode:'%c'", gnome_vfs_uri_to_string(uri, 0),
1678 mode & GNOME_VFS_OPEN_READ ? 'R' : 'W'));
1680 handle = g_new (HttpFileHandle, 1);
1681 if (mode & GNOME_VFS_OPEN_READ) {
1682 result = make_request (handle, uri, "GET", NULL, NULL,
1685 http_file_handle_new(handle, NULL, uri); /* shrug */
1687 if (result == GNOME_VFS_OK) {
1688 *method_handle = (GnomeVFSMethodHandle *) handle;
1690 *method_handle = NULL;
1693 DEBUG_HTTP (("-Open (%d) handle:0x%08x", result, (unsigned int)handle));
1694 ANALYZE_HTTP ("==> -do_open");
1699 static GnomeVFSResult
1700 do_create (GnomeVFSMethod *method,
1701 GnomeVFSMethodHandle **method_handle,
1703 GnomeVFSOpenMode mode,
1706 GnomeVFSContext *context)
1708 /* try to write a zero length file - this appears to be the
1709 * only reliable way of testing if a put will succeed.
1710 * Xythos can apparently tell us if we have write permission by
1711 * playing with LOCK, but mod_dav cannot. */
1712 HttpFileHandle *handle;
1713 GnomeVFSResult result;
1714 GByteArray *bytes = g_byte_array_new();
1716 ANALYZE_HTTP ("==> +do_create");
1717 DEBUG_HTTP (("+Create URI: '%s'", gnome_vfs_uri_get_path (uri)));
1719 http_cache_invalidate_uri_parent (uri);
1721 /* Don't ignore exclusive; it should check first whether
1722 the file exists, since the http protocol default is to
1723 overwrite by default */
1724 /* FIXME we've stopped using HEAD -- we should use GET instead */
1725 /* FIXME we should check the cache here */
1728 ANALYZE_HTTP ("==> Checking to see if file exists");
1730 handle = g_new (HttpFileHandle, 1);
1731 result = make_request (handle, uri, "HEAD", NULL, NULL,
1733 http_handle_close (handle, context);
1736 if (result != GNOME_VFS_OK &&
1737 result != GNOME_VFS_ERROR_NOT_FOUND) {
1740 if (result == GNOME_VFS_OK) {
1741 return GNOME_VFS_ERROR_FILE_EXISTS;
1745 ANALYZE_HTTP ("==> Creating initial file");
1747 handle = g_new (HttpFileHandle, 1);
1748 result = make_request (handle, uri, "PUT", bytes, NULL, context);
1749 http_handle_close(handle, context);
1752 if (result != GNOME_VFS_OK) {
1753 /* the PUT failed */
1755 /* FIXME bugzilla.gnome.org 45131
1756 * If you PUT a file with an invalid name to Xythos, it
1757 * returns a 403 Forbidden, which is different from the behaviour
1758 * in MKCOL or MOVE. Unfortunately, it is not possible to discern whether
1759 * that 403 Forbidden is being returned because of invalid characters in the name
1760 * or because of permissions problems
1763 if (result == GNOME_VFS_ERROR_NOT_FOUND) {
1764 result = resolve_409 (method, uri, context);
1771 g_byte_array_free (bytes, TRUE);
1773 /* FIXME bugzilla.gnome.org 41159: do we need to do something more intelligent here? */
1774 result = do_open (method, method_handle, uri, GNOME_VFS_OPEN_WRITE, context);
1776 DEBUG_HTTP (("-Create (%d) handle:0x%08x", result, (unsigned int)handle));
1777 ANALYZE_HTTP ("==> -do_create");
1782 static GnomeVFSResult
1783 do_close (GnomeVFSMethod *method,
1784 GnomeVFSMethodHandle *method_handle,
1785 GnomeVFSContext *context)
1787 HttpFileHandle *old_handle;
1788 HttpFileHandle *new_handle;
1789 GnomeVFSResult result;
1791 ANALYZE_HTTP ("==> +do_close");
1792 DEBUG_HTTP (("+Close handle:0x%08x", (unsigned int)method_handle));
1794 old_handle = (HttpFileHandle *) method_handle;
1796 /* if the handle was opened in write mode then:
1797 * 1) there won't be a connection open, and
1798 * 2) there will be data to_be_written...
1800 if (old_handle->to_be_written != NULL) {
1801 GnomeVFSURI *uri = old_handle->uri;
1802 GByteArray *bytes = old_handle->to_be_written;
1803 GnomeVFSMimeSniffBuffer *sniff_buffer;
1804 char *extraheader = NULL;
1805 const char *mime_type = NULL;
1808 gnome_vfs_mime_sniff_buffer_new_from_existing_data (bytes->data,
1811 if (sniff_buffer != NULL) {
1813 gnome_vfs_get_mime_type_for_buffer (
1815 if (mime_type != NULL) {
1816 extraheader = g_strdup_printf(
1817 "Content-type: %s\r\n",
1820 gnome_vfs_mime_sniff_buffer_free (sniff_buffer);
1824 http_cache_invalidate_uri (uri);
1826 ANALYZE_HTTP ("==> doing PUT");
1827 new_handle = g_new (HttpFileHandle, 1);
1828 result = make_request (new_handle, uri, "PUT", bytes,
1829 extraheader, context);
1830 g_free (extraheader);
1831 http_handle_close (new_handle, context);
1832 g_free (new_handle);
1834 result = GNOME_VFS_OK;
1837 http_handle_close (old_handle, context);
1838 g_free (old_handle);
1840 DEBUG_HTTP (("-Close (%d)", result));
1841 ANALYZE_HTTP ("==> -do_close");
1846 static GnomeVFSResult
1847 do_write (GnomeVFSMethod *method,
1848 GnomeVFSMethodHandle *method_handle,
1849 gconstpointer buffer,
1850 GnomeVFSFileSize num_bytes,
1851 GnomeVFSFileSize *bytes_written,
1852 GnomeVFSContext *context)
1854 HttpFileHandle *handle;
1856 DEBUG_HTTP (("+Write handle:0x%08x", (unsigned int)method_handle));
1858 handle = (HttpFileHandle *) method_handle;
1860 if (handle->offset != 0)
1861 return GNOME_VFS_ERROR_NOT_SUPPORTED;
1863 if(handle->to_be_written == NULL) {
1864 handle->to_be_written = g_byte_array_new();
1866 handle->to_be_written = g_byte_array_append(handle->to_be_written, buffer, num_bytes);
1867 *bytes_written = num_bytes;
1869 DEBUG_HTTP (("-Write (0)"));
1871 return GNOME_VFS_OK;
1875 static GnomeVFSResult
1876 do_read (GnomeVFSMethod *method,
1877 GnomeVFSMethodHandle *method_handle,
1879 GnomeVFSFileSize num_bytes,
1880 GnomeVFSFileSize *bytes_read,
1881 GnomeVFSContext *context)
1883 HttpFileHandle *handle;
1884 GnomeVFSResult result;
1886 ANALYZE_HTTP ("==> +do_read");
1887 DEBUG_HTTP (("+Read handle=0x%08x", (unsigned int) method_handle));
1889 handle = (HttpFileHandle *) method_handle;
1891 if (handle->file_info->flags & GNOME_VFS_FILE_INFO_FIELDS_SIZE) {
1892 GnomeVFSFileSize max_bytes;
1894 max_bytes = MAX (0, handle->file_info->size - handle->offset);
1895 num_bytes = MIN (max_bytes, num_bytes);
1900 return GNOME_VFS_ERROR_EOF;
1904 && handle->offset > handle->socket_buffer_offset
1905 && handle->offset <= handle->socket_buffer_offset+MAX_BUFFER_SEEK_SKIP_READ) {
1906 static char drop_buffer[0x1000];
1907 GnomeVFSFileSize bytes, bytes_read;
1908 GnomeVFSResult result;
1910 while ((bytes=MIN(sizeof(drop_buffer), handle->offset - handle->socket_buffer_offset))) {
1911 result = gnome_vfs_socket_buffer_read (handle->socket_buffer, drop_buffer,
1912 bytes, &bytes_read);
1913 if (result != GNOME_VFS_OK)
1915 handle->socket_buffer_offset += bytes_read;
1919 if (handle->offset != handle->socket_buffer_offset) {
1920 GnomeVFSURI *uri = handle->uri;
1921 gchar *extra_headers;
1922 GnomeVFSFileOffset offset_save;
1924 offset_save = handle->offset;
1925 gnome_vfs_uri_ref(uri);
1926 http_handle_close (handle, context);
1927 extra_headers = g_strdup_printf("Range: bytes=%" G_GINT64_FORMAT "-\r\n",(gint64)handle->offset);
1928 result = make_request (handle, uri, "GET", NULL, extra_headers,
1930 g_free (extra_headers);
1931 gnome_vfs_uri_unref(uri);
1932 handle->offset = offset_save;
1933 if (result != GNOME_VFS_OK) {
1934 /* FIXME: 'method_handle' is now broken! */
1935 memset(handle, 0, sizeof (*handle));
1938 handle->socket_buffer_offset = handle->offset;
1941 result = gnome_vfs_socket_buffer_read (handle->socket_buffer, buffer,
1942 num_bytes, bytes_read);
1944 if (*bytes_read == 0) {
1945 return GNOME_VFS_ERROR_EOF;
1948 handle->socket_buffer_offset += *bytes_read;
1949 handle->offset += *bytes_read;
1951 DEBUG_HTTP (("-Read (%d)", result));
1952 ANALYZE_HTTP ("==> -do_read");
1957 static GnomeVFSResult
1958 do_seek (GnomeVFSMethod *method,
1959 GnomeVFSMethodHandle *method_handle,
1960 GnomeVFSSeekPosition whence,
1961 GnomeVFSFileOffset offset,
1962 GnomeVFSContext *context)
1964 HttpFileHandle *handle;
1966 handle = (HttpFileHandle *) method_handle;
1968 if (handle->to_be_written != NULL)
1969 return GNOME_VFS_ERROR_NOT_SUPPORTED;
1972 case GNOME_VFS_SEEK_START:
1973 handle->offset = offset;
1975 case GNOME_VFS_SEEK_CURRENT:
1976 handle->offset += offset;
1978 case GNOME_VFS_SEEK_END:
1979 if (!(handle->file_info->flags & GNOME_VFS_FILE_INFO_FIELDS_SIZE))
1980 return GNOME_VFS_ERROR_NOT_SUPPORTED;
1981 handle->offset = handle->file_info->size + offset;
1984 g_return_val_if_reached(GNOME_VFS_ERROR_NOT_SUPPORTED);
1987 return GNOME_VFS_OK;
1990 static GnomeVFSResult
1991 do_tell (GnomeVFSMethod *method,
1992 GnomeVFSMethodHandle *method_handle,
1993 GnomeVFSFileOffset *offset_return)
1995 HttpFileHandle *handle;
1997 handle = (HttpFileHandle *) method_handle;
1999 *offset_return = handle->offset;
2001 return GNOME_VFS_OK;
2004 /* Directory handling - WebDAV servers only */
2007 process_propfind_propstat (xmlNodePtr node,
2008 GnomeVFSFileInfo *file_info)
2011 gboolean treat_as_directory;
2013 treat_as_directory = FALSE;
2015 while (node != NULL) {
2016 if (strcmp ((char *)node->name, "prop") != 0) {
2017 /* node name != "prop" - prop is all we care about */
2021 /* properties of the file */
2022 l = node->xmlChildrenNode;
2024 char *node_content_xml = xmlNodeGetContent(l);
2025 if (node_content_xml) {
2026 if (strcmp ((char *)l->name, "getcontenttype") == 0) {
2028 file_info->valid_fields |=
2029 GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE;
2031 if (!file_info->mime_type) {
2032 file_info->mime_type = strip_semicolon (node_content_xml);
2034 } else if (strcmp ((char *)l->name, "getcontentlength") == 0){
2035 file_info->valid_fields |=
2036 GNOME_VFS_FILE_INFO_FIELDS_SIZE;
2037 file_info->size = atoi(node_content_xml);
2038 } else if (strcmp((char *)l->name, "getlastmodified") == 0) {
2039 if (gnome_vfs_atotm (node_content_xml, &(file_info->mtime))) {
2040 file_info->ctime = file_info->mtime;
2041 file_info->valid_fields |=
2042 GNOME_VFS_FILE_INFO_FIELDS_MTIME
2043 | GNOME_VFS_FILE_INFO_FIELDS_CTIME;
2046 /* Unfortunately, we don't have a mapping for "creationdate" */
2048 xmlFree (node_content_xml);
2049 node_content_xml = NULL;
2051 if (strcmp ((char *)l->name, "resourcetype") == 0) {
2052 file_info->valid_fields |=
2053 GNOME_VFS_FILE_INFO_FIELDS_TYPE;
2054 file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
2056 if (l->xmlChildrenNode && l->xmlChildrenNode->name
2057 && strcmp ((char *)l->xmlChildrenNode->name, "collection") == 0) {
2058 file_info->type = GNOME_VFS_FILE_TYPE_DIRECTORY;
2066 /* If this is a DAV collection, do we tell nautilus to treat it
2067 * as a directory or as a web page?
2069 if (file_info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE
2070 && file_info->type == GNOME_VFS_FILE_TYPE_DIRECTORY) {
2071 g_free (file_info->mime_type);
2072 if (treat_as_directory) {
2073 file_info->mime_type = g_strdup ("x-directory/webdav-prefer-directory");
2074 file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE;
2076 file_info->mime_type = g_strdup ("x-directory/webdav");
2077 file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE;
2082 if ((file_info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE) == 0) {
2083 file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE;
2084 file_info->mime_type = g_strdup (gnome_vfs_mime_type_from_name_or_default (file_info->name, "text/plain"));
2087 if ((file_info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE) == 0) {
2088 /* Is this a reasonable assumption ? */
2089 file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_TYPE;
2090 file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
2094 /* a strcmp that doesn't barf on NULLs */
2096 null_handling_strcmp (const char *a, const char *b)
2098 if ((a == NULL) != (b == NULL)) {
2102 if (a == NULL && b == NULL) {
2106 return strcmp (a, b);
2111 unescape_unreserved_chars (const char *in_string)
2113 /* RFC 2396 section 2.2 */
2114 static const char * reserved_chars = "%;/?:@&=+$,";
2116 char *ret, *write_char;
2117 const char * read_char;
2119 if (in_string == NULL) {
2123 ret = g_new (char, strlen (in_string) + 1);
2125 for (read_char = in_string, write_char = ret ; *read_char != '\0' ; read_char++) {
2126 if (read_char[0] == '%'
2127 && g_ascii_isxdigit (read_char[1])
2128 && g_ascii_isxdigit (read_char[2])) {
2131 unescaped = (g_ascii_xdigit_value (read_char[1]) << 4) | g_ascii_xdigit_value (read_char[2]);
2133 if (strchr (reserved_chars, unescaped)) {
2134 *write_char++ = *read_char++;
2135 *write_char++ = *read_char++;
2136 *write_char++ = *read_char; /*The last ++ is done in the for statement */
2138 *write_char++ = unescaped;
2139 read_char += 2; /*The last ++ is done in the for statement */
2142 *write_char++ = *read_char;
2145 *write_char++ = '\0';
2152 find_child_node_named (xmlNodePtr node,
2153 const char *child_node_name)
2157 child = node->xmlChildrenNode;
2159 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
2160 if (0 == strcmp (child->name, child_node_name)) {
2168 /* Look for a <status> tag in the children of node, and returns
2169 * the corresponding (HTTP) error code
2172 get_status_node (xmlNodePtr node, guint *status_code)
2174 xmlNodePtr status_node;
2175 char *status_string;
2178 status_node = find_child_node_named (node, "status");
2180 if (status_node != NULL) {
2181 status_string = xmlNodeGetContent (status_node);
2182 ret = parse_status (status_string, status_code);
2183 xmlFree (status_string);
2191 static GnomeVFSFileInfo *
2192 process_propfind_response(xmlNodePtr n,
2193 GnomeVFSURI *base_uri)
2195 GnomeVFSFileInfo *file_info = defaults_file_info_new ();
2196 GnomeVFSURI *second_base = gnome_vfs_uri_append_path (base_uri, "/");
2199 file_info->valid_fields = GNOME_VFS_FILE_INFO_FIELDS_NONE;
2202 if (strcmp ((char *)n->name, "href") == 0) {
2203 char *nodecontent = xmlNodeGetContent (n);
2206 rv = gnome_vfs_remove_optional_escapes (nodecontent);
2208 if (nodecontent != NULL && *nodecontent != '\0' && rv == GNOME_VFS_OK) {
2210 GnomeVFSURI *uri = gnome_vfs_uri_new (nodecontent);
2213 if ((0 == null_handling_strcmp (base_uri->text, uri->text)) ||
2214 (0 == null_handling_strcmp (second_base->text, uri->text))) {
2215 file_info->name = NULL; /* this file is the . directory */
2217 if (file_info->name != NULL) {
2218 /* Don't leak if a (potentially malicious)
2219 * server returns several href in its answer
2221 g_free (file_info->name);
2223 file_info->name = gnome_vfs_uri_extract_short_name (uri);
2224 if (file_info->name != NULL) {
2225 len = strlen (file_info->name) -1;
2226 if (file_info->name[len] == '/') {
2227 /* trim trailing `/` - it confuses stuff */
2228 file_info->name[len] = '\0';
2231 g_warning ("Invalid filename in PROPFIND '%s'; silently skipping", nodecontent);
2234 gnome_vfs_uri_unref (uri);
2236 g_warning ("Can't make URI from href in PROPFIND '%s'; silently skipping", nodecontent);
2239 g_warning ("got href without contents in PROPFIND response");
2242 xmlFree (nodecontent);
2243 } else if (strcmp ((char *)n->name, "propstat") == 0) {
2244 if (get_status_node (n, &status_code) && status_code == 200) {
2245 process_propfind_propstat (n->xmlChildrenNode, file_info);
2251 gnome_vfs_uri_unref (second_base);
2258 static GnomeVFSResult
2259 make_propfind_request (HttpFileHandle *handle,
2262 GnomeVFSContext *context)
2264 GnomeVFSResult result = GNOME_VFS_OK;
2265 GnomeVFSFileSize bytes_read, num_bytes=(64*1024);
2266 char *buffer = g_malloc(num_bytes);
2267 xmlParserCtxtPtr parserContext;
2268 xmlDocPtr doc = NULL;
2269 xmlNodePtr cur = NULL;
2270 char *extraheaders = g_strdup_printf("Depth: %d\r\n", depth);
2271 gboolean found_root_node_props;
2272 gboolean handle_valid = FALSE;
2274 GByteArray *request = g_byte_array_new();
2275 char *request_str = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
2276 "<D:propfind xmlns:D=\"DAV:\" >"
2279 "<D:getcontentlength/>"
2280 "<D:getcontenttype/>"
2281 "<D:getlastmodified/>"
2286 ANALYZE_HTTP ("==> +make_propfind_request");
2288 request = g_byte_array_append(request, request_str,
2289 strlen(request_str));
2291 parserContext = xmlCreatePushParserCtxt(NULL, NULL, "", 0, "PROPFIND");
2294 http_cache_invalidate_uri_and_children (uri);
2297 result = make_request (handle, uri, "PROPFIND", request,
2298 extraheaders, context);
2299 if (result == GNOME_VFS_OK)
2300 handle_valid = TRUE;
2302 /* FIXME bugzilla.gnome.org 43834: It looks like some http
2303 * servers (eg, www.yahoo.com) treat PROPFIND as a GET and
2304 * return a 200 OK. Others may return access denied errors or
2305 * redirects or any other legal response. This case probably
2306 * needs to be made more robust.
2308 if (result == GNOME_VFS_OK && handle->server_status != 207) { /* Multi-Status */
2309 DEBUG_HTTP (("HTTP server returned an invalid PROPFIND response: %d", handle->server_status));
2310 result = GNOME_VFS_ERROR_NOT_SUPPORTED;
2313 if (result == GNOME_VFS_OK) {
2315 result = do_read (NULL, (GnomeVFSMethodHandle *) handle,
2316 buffer, num_bytes, &bytes_read, context);
2318 if (result != GNOME_VFS_OK ) {
2321 xmlParseChunk (parserContext, buffer, bytes_read, 0);
2322 buffer[bytes_read]=0;
2323 } while (bytes_read > 0);
2326 if (result == GNOME_VFS_ERROR_EOF) {
2327 result = GNOME_VFS_OK;
2330 if (result != GNOME_VFS_OK) {
2334 xmlParseChunk (parserContext, "", 0, 1);
2336 doc = parserContext->myDoc;
2338 result = GNOME_VFS_ERROR_GENERIC;
2342 cur = doc->xmlRootNode;
2343 if (strcmp ((char *)cur->name, "multistatus") != 0) {
2344 DEBUG_HTTP (("Couldn't find <multistatus>.\n"));
2345 result = GNOME_VFS_ERROR_GENERIC;
2348 cur = cur->xmlChildrenNode;
2350 found_root_node_props = FALSE;
2351 while (cur != NULL) {
2352 if (strcmp ((char *)cur->name, "response") == 0) {
2353 GnomeVFSFileInfo *file_info;
2356 /* Some webdav servers (eg resin) put the HTTP status
2357 * code for PROPFIND request in the xml answer instead
2358 * of directly sending a 404
2360 if (get_status_node (cur, &status) && !HTTP_20X(status)) {
2361 result = http_status_to_vfs_result (status);
2366 process_propfind_response (cur->xmlChildrenNode, uri);
2368 if (file_info->name != NULL) {
2369 handle->files = g_list_append (handle->files, file_info);
2371 /* This response refers to the root node */
2372 /* Abandon the old information that came from create_handle*/
2374 file_info->name = handle->file_info->name;
2375 handle->file_info->name = NULL;
2376 gnome_vfs_file_info_unref (handle->file_info);
2377 handle->file_info = file_info;
2378 found_root_node_props = TRUE;
2382 DEBUG_HTTP(("expecting <response> got <%s>\n", cur->name));
2387 if (!found_root_node_props) {
2388 DEBUG_HTTP (("Failed to find root request node properties during propfind"));
2389 result = GNOME_VFS_ERROR_GENERIC;
2395 * Section 8.1, final line
2396 * "The results of this method [PROPFIND] SHOULD NOT be cached"
2397 * Well, at least its not "MUST NOT"
2401 http_cache_add_uri (uri, handle->file_info, TRUE);
2403 http_cache_add_uri_and_children (uri, handle->file_info, handle->files);
2408 g_free(extraheaders);
2409 xmlFreeParserCtxt(parserContext);
2411 if (result != GNOME_VFS_OK && handle_valid) {
2412 http_handle_close (handle, context);
2415 ANALYZE_HTTP ("==> -make_propfind_request");
2420 static GnomeVFSResult
2421 do_open_directory(GnomeVFSMethod *method,
2422 GnomeVFSMethodHandle **method_handle,
2424 GnomeVFSFileInfoOptions options,
2425 GnomeVFSContext *context)
2427 /* TODO move to using the gnome_vfs_file_info_list family of functions */
2428 GnomeVFSResult result;
2429 HttpFileHandle *handle = NULL;
2430 GnomeVFSFileInfo * file_info_cached;
2431 GList *child_file_info_cached_list = NULL;
2433 ANALYZE_HTTP ("==> +do_open_directory");
2434 DEBUG_HTTP (("+Open_Directory options: %d URI: '%s'", options, gnome_vfs_uri_to_string (uri, 0)));
2436 /* Check the cache--is this even a directory?
2437 * (Nautilus, in particular, seems to like to make this call on non directories
2440 file_info_cached = http_cache_check_uri (uri);
2442 if (file_info_cached) {
2443 if (GNOME_VFS_FILE_TYPE_DIRECTORY != file_info_cached->type) {
2444 ANALYZE_HTTP ("==> Cache Hit (Negative)");
2445 gnome_vfs_file_info_unref (file_info_cached);
2446 result = GNOME_VFS_ERROR_NOT_A_DIRECTORY;
2449 gnome_vfs_file_info_unref (file_info_cached);
2450 file_info_cached = NULL;
2454 /* The check for directory contents is more stringent */
2455 file_info_cached = http_cache_check_directory_uri (uri, &child_file_info_cached_list);
2457 if (file_info_cached) {
2458 handle = g_new (HttpFileHandle, 1);
2459 http_file_handle_new (handle, NULL, uri);
2460 gnome_vfs_file_info_unref (handle->file_info);
2461 handle->file_info = file_info_cached;
2462 handle->files = child_file_info_cached_list;
2463 result = GNOME_VFS_OK;
2465 handle = g_new (HttpFileHandle, 1);
2466 result = make_propfind_request(handle, uri, 1, context);
2467 /* mfleming -- is this necessary? Most DAV server's I've seen don't have the horrible
2468 * lack-of-trailing-/-is-a-301 problem for PROPFIND's
2470 if (result == GNOME_VFS_ERROR_NOT_FOUND) { /* 404 not found */
2471 if (uri->text != NULL && *uri->text != '\0'
2472 && uri->text[strlen (uri->text) - 1] != '/') {
2473 GnomeVFSURI *tmpuri = gnome_vfs_uri_append_path (uri, "/");
2474 result = do_open_directory (method, (GnomeVFSMethodHandle **)&handle, tmpuri, options, context);
2475 gnome_vfs_uri_unref (tmpuri);
2480 if (result == GNOME_VFS_OK
2481 && handle->file_info->type != GNOME_VFS_FILE_TYPE_DIRECTORY) {
2482 result = GNOME_VFS_ERROR_NOT_A_DIRECTORY;
2483 http_handle_close (handle, context);
2489 *method_handle = (GnomeVFSMethodHandle *)handle;
2492 DEBUG_HTTP (("-Open_Directory (%d) handle:0x%08x", result, (unsigned int)handle));
2493 ANALYZE_HTTP ("==> -do_open_directory");
2498 static GnomeVFSResult
2499 do_close_directory (GnomeVFSMethod *method,
2500 GnomeVFSMethodHandle *method_handle,
2501 GnomeVFSContext *context)
2503 HttpFileHandle *handle;
2505 DEBUG_HTTP (("+Close_Directory"));
2507 handle = (HttpFileHandle *) method_handle;
2509 http_handle_close(handle, context);
2512 DEBUG_HTTP (("-Close_Directory (0) handle:0x%08x", (unsigned int) method_handle));
2514 return GNOME_VFS_OK;
2517 static GnomeVFSResult
2518 do_read_directory (GnomeVFSMethod *method,
2519 GnomeVFSMethodHandle *method_handle,
2520 GnomeVFSFileInfo *file_info,
2521 GnomeVFSContext *context)
2523 HttpFileHandle *handle;
2524 GnomeVFSResult result;
2526 DEBUG_HTTP (("+Read_Directory handle:0x%08x", (unsigned int) method_handle));
2528 handle = (HttpFileHandle *) method_handle;
2530 if (handle->files && g_list_length (handle->files)) {
2531 GnomeVFSFileInfo *original_info = g_list_nth_data (handle->files, 0);
2532 gboolean found_entry = FALSE;
2534 /* mfleming -- Why is this check here? Does anyone set original_info->name to NULL? */
2535 if (original_info->name != NULL && original_info->name[0]) {
2536 gnome_vfs_file_info_copy (file_info, original_info);
2540 /* remove our GnomeVFSFileInfo from the list */
2541 handle->files = g_list_remove (handle->files, original_info);
2542 gnome_vfs_file_info_unref (original_info);
2544 /* mfleming -- Is this necessary? */
2546 result = GNOME_VFS_OK;
2548 result = do_read_directory (method, method_handle, file_info, context);
2551 result = GNOME_VFS_ERROR_EOF;
2554 DEBUG_HTTP (("-Read_Directory (%d)", result));
2559 static GnomeVFSResult
2560 do_get_file_info (GnomeVFSMethod *method,
2562 GnomeVFSFileInfo *file_info,
2563 GnomeVFSFileInfoOptions options,
2564 GnomeVFSContext *context)
2566 HttpFileHandle *handle;
2567 GnomeVFSResult result;
2568 GnomeVFSFileInfo * file_info_cached;
2570 ANALYZE_HTTP ("==> +do_get_file_info");
2571 DEBUG_HTTP (("+Get_File_Info options: %d URI: '%s'", options, gnome_vfs_uri_to_string( uri, 0)));
2573 file_info_cached = http_cache_check_uri (uri);
2575 if (file_info_cached != NULL) {
2576 gnome_vfs_file_info_copy (file_info, file_info_cached);
2577 gnome_vfs_file_info_unref (file_info_cached);
2578 ANALYZE_HTTP ("==> Cache Hit");
2579 result = GNOME_VFS_OK;
2582 * Start off by making a PROPFIND request. Fall back to a HEAD if it fails
2585 handle = g_new (HttpFileHandle, 1);
2586 result = make_propfind_request (handle, uri, 0, context);
2588 /* Note that theoretically we could not bother with this request if we get a 404 back,
2589 * but since some servers seem to return wierd things on PROPFIND (mostly 200 OK's...)
2590 * I'm not going to count on the PROPFIND response....
2592 if (result == GNOME_VFS_OK) {
2593 gnome_vfs_file_info_copy (file_info, handle->file_info);
2594 http_handle_close (handle, context);
2600 g_assert (handle == NULL); /* Make sure we're not leaking some old one */
2602 /* Lame buggy servers (eg: www.mozilla.org,
2603 * www.corel.com)) return an HTTP error for a
2604 * HEAD where a GET would succeed. In these
2605 * cases lets try to do a GET.
2607 if (result != GNOME_VFS_OK) {
2608 g_assert (handle == NULL); /* Make sure we're not leaking some old one */
2610 ANALYZE_HTTP ("==> do_get_file_info: do GET ");
2612 handle = g_new (HttpFileHandle, 1);
2613 result = make_request (handle, uri, "GET", NULL, NULL, context);
2614 if (result == GNOME_VFS_OK) {
2615 gnome_vfs_file_info_copy (file_info, handle->file_info);
2616 http_cache_add_uri (uri, handle->file_info, FALSE);
2617 http_handle_close (handle, context);
2622 /* If we get a redirect, we should be
2623 * basing the MIME type on the type of
2624 * the page we'll be redirected
2625 * too. Maybe we even want to take the
2626 * "follow_links" setting into account.
2628 /* FIXME: For now we treat all
2629 * redirects as if they lead to a
2630 * text/html. That works pretty well,
2631 * but it's not correct.
2633 if (handle != NULL && HTTP_REDIRECTED (handle->server_status)) {
2634 g_free (file_info->mime_type);
2635 file_info->mime_type = g_strdup ("text/html");
2639 if (result == GNOME_VFS_ERROR_NOT_FOUND) { /* 404 not found */
2640 /* FIXME bugzilla.gnome.org 43835: mfleming: Is this code really appropriate?
2641 * In any case, it doesn't seem to be appropriate for a DAV-enabled
2642 * server, since they don't seem to send 301's when you PROPFIND collections
2643 * without a trailing '/'.
2645 if (uri->text != NULL && *uri->text != '\0'
2646 && uri->text[strlen(uri->text)-1] != '/') {
2647 GnomeVFSURI *tmpuri = gnome_vfs_uri_append_path (uri, "/");
2649 result = do_get_file_info (method, tmpuri, file_info, options, context);
2650 gnome_vfs_uri_unref (tmpuri);
2656 DEBUG_HTTP (("-Get_File_Info (%d)", result));
2657 ANALYZE_HTTP ("==> -do_get_file_info");
2662 static GnomeVFSResult
2663 do_get_file_info_from_handle (GnomeVFSMethod *method,
2664 GnomeVFSMethodHandle *method_handle,
2665 GnomeVFSFileInfo *file_info,
2666 GnomeVFSFileInfoOptions options,
2667 GnomeVFSContext *context)
2669 HttpFileHandle *handle;
2671 DEBUG_HTTP (("+Get_File_Info_From_Handle"));
2673 handle = (HttpFileHandle *) method_handle;
2675 gnome_vfs_file_info_copy (file_info, handle->file_info);
2677 DEBUG_HTTP (("-Get_File_Info_From_Handle"));
2679 return GNOME_VFS_OK;
2683 do_is_local (GnomeVFSMethod *method,
2684 const GnomeVFSURI *uri)
2686 DEBUG_HTTP (("+Is_Local"));
2690 static GnomeVFSResult
2691 do_make_directory (GnomeVFSMethod *method,
2694 GnomeVFSContext *context)
2696 /* MKCOL /path HTTP/1.0 */
2698 HttpFileHandle *handle;
2699 GnomeVFSResult result;
2701 DEBUG_HTTP (("+Make_Directory URI: '%s'", gnome_vfs_uri_to_string (uri, 0)));
2702 ANALYZE_HTTP ("==> +do_make_directory");
2705 * MKCOL returns a 405 if you try to MKCOL on something that
2706 * already exists. Of course, we don't know whether that means that
2707 * the server doesn't support DAV or the collection already exists.
2708 * So we do a PROPFIND first to find out
2710 /* FIXME check cache here */
2711 handle = g_new (HttpFileHandle, 1);
2712 result = make_propfind_request(handle, uri, 0, context);
2714 if (result == GNOME_VFS_OK) {
2715 result = GNOME_VFS_ERROR_FILE_EXISTS;
2717 /* Make sure we're not leaking an old one */
2718 g_assert (handle == NULL);
2720 if (result == GNOME_VFS_ERROR_NOT_FOUND) {
2721 http_cache_invalidate_uri_parent (uri);
2722 handle = g_new (HttpFileHandle, 1);
2723 result = make_request (handle, uri, "MKCOL", NULL, NULL, context);
2726 http_handle_close (handle, context);
2729 if (result == GNOME_VFS_ERROR_NOT_FOUND) {
2730 result = resolve_409 (method, uri, context);
2733 ANALYZE_HTTP ("==> -do_make_directory");
2734 DEBUG_HTTP (("-Make_Directory (%d)", result));
2739 static GnomeVFSResult
2740 do_remove_directory(GnomeVFSMethod *method,
2742 GnomeVFSContext *context)
2744 /* DELETE /path HTTP/1.0 */
2745 HttpFileHandle *handle;
2746 GnomeVFSResult result;
2748 ANALYZE_HTTP ("==> +do_remove_directory");
2749 DEBUG_HTTP (("+Remove_Directory URI: '%s'", gnome_vfs_uri_to_string (uri, 0)));
2751 http_cache_invalidate_uri_parent (uri);
2753 /* FIXME this should return GNOME_VFS_ERROR_DIRECTORY_NOT_EMPTY if the
2754 * directory is not empty
2756 handle = g_new (HttpFileHandle, 1);
2757 result = make_request (handle, uri, "DELETE", NULL, NULL,
2759 http_handle_close (handle, context);
2762 DEBUG_HTTP (("-Remove_Directory (%d)", result));
2763 ANALYZE_HTTP ("==> -do_remove_directory");
2769 is_same_fs (const GnomeVFSURI *a,
2770 const GnomeVFSURI *b)
2772 return null_handling_strcmp (gnome_vfs_uri_get_scheme (a), gnome_vfs_uri_get_scheme (b)) == 0
2773 && null_handling_strcmp (gnome_vfs_uri_get_host_name (a), gnome_vfs_uri_get_host_name (b)) == 0
2774 && null_handling_strcmp (gnome_vfs_uri_get_user_name (a), gnome_vfs_uri_get_user_name (b)) == 0
2775 && null_handling_strcmp (gnome_vfs_uri_get_password (a), gnome_vfs_uri_get_password (b)) == 0
2776 && (gnome_vfs_uri_get_host_port (a) == gnome_vfs_uri_get_host_port (b));
2779 static GnomeVFSResult
2780 do_move (GnomeVFSMethod *method,
2781 GnomeVFSURI *old_uri,
2782 GnomeVFSURI *new_uri,
2783 gboolean force_replace,
2784 GnomeVFSContext *context)
2788 * MOVE /path1 HTTP/1.0
2789 * Destination: /path2
2793 HttpFileHandle *handle;
2794 GnomeVFSResult result;
2796 char *destpath, *destheader;
2798 ANALYZE_HTTP ("==> +do_move");
2799 DEBUG_HTTP (("+Move URI: '%s' Dest: '%s'",
2800 gnome_vfs_uri_to_string (old_uri, 0),
2801 gnome_vfs_uri_to_string (new_uri, 0)));
2803 if (!is_same_fs (old_uri, new_uri)) {
2804 return GNOME_VFS_ERROR_NOT_SAME_FILE_SYSTEM;
2807 destpath = gnome_vfs_uri_to_string (new_uri, GNOME_VFS_URI_HIDE_USER_NAME|GNOME_VFS_URI_HIDE_PASSWORD);
2808 destheader = g_strdup_printf ("Destination: %s\r\nOverwrite: %c\r\n", destpath, force_replace ? 'T' : 'F' );
2810 handle = g_new (HttpFileHandle, 1);
2811 result = make_request (handle, old_uri, "MOVE", NULL, destheader, context);
2812 http_handle_close (handle, context);
2816 if (result == GNOME_VFS_ERROR_NOT_FOUND) {
2817 result = resolve_409 (method, new_uri, context);
2820 http_cache_invalidate_uri_parent (old_uri);
2821 http_cache_invalidate_uri_parent (new_uri);
2823 DEBUG_HTTP (("-Move (%d)", result));
2824 ANALYZE_HTTP ("==> -do_move");
2830 static GnomeVFSResult
2831 do_unlink(GnomeVFSMethod *method,
2833 GnomeVFSContext *context)
2835 GnomeVFSResult result;
2837 /* FIXME need to make sure this fails on directories */
2838 ANALYZE_HTTP ("==> +do_unlink");
2839 DEBUG_HTTP (("+Unlink URI: '%s'", gnome_vfs_uri_to_string (uri, 0)));
2840 result = do_remove_directory (method, uri, context);
2841 DEBUG_HTTP (("-Unlink (%d)", result));
2842 ANALYZE_HTTP ("==> -do_unlink");
2847 static GnomeVFSResult
2848 do_check_same_fs (GnomeVFSMethod *method,
2851 gboolean *same_fs_return,
2852 GnomeVFSContext *context)
2854 *same_fs_return = is_same_fs (a, b);
2856 return GNOME_VFS_OK;
2859 static GnomeVFSResult
2860 do_set_file_info (GnomeVFSMethod *method,
2862 const GnomeVFSFileInfo *info,
2863 GnomeVFSSetFileInfoMask mask,
2864 GnomeVFSContext *context)
2866 GnomeVFSURI *parent_uri, *new_uri;
2867 GnomeVFSResult result;
2869 /* FIXME: For now, we only support changing the name. */
2870 if ((mask & ~(GNOME_VFS_SET_FILE_INFO_NAME)) != 0) {
2871 return GNOME_VFS_ERROR_NOT_SUPPORTED;
2874 /* FIXME bugzillagnome.org 40645: Make sure this returns an
2875 * error for incoming names with "/" characters in them,
2876 * instead of moving the file.
2879 /* Share code with do_move. */
2880 parent_uri = gnome_vfs_uri_get_parent (uri);
2881 if (parent_uri == NULL) {
2882 return GNOME_VFS_ERROR_NOT_FOUND;
2884 new_uri = gnome_vfs_uri_append_file_name (parent_uri, info->name);
2885 gnome_vfs_uri_unref (parent_uri);
2886 result = do_move (method, uri, new_uri, FALSE, context);
2887 gnome_vfs_uri_unref (new_uri);
2891 static GnomeVFSMethod method = {
2892 sizeof (GnomeVFSMethod),
2900 NULL, /* truncate_handle */
2905 do_get_file_info_from_handle,
2908 do_remove_directory,
2913 NULL, /* truncate */
2914 NULL, /* find_directory */
2915 NULL /* create_symbolic_link */
2919 vfs_module_init (const char *method_name,
2922 GError *gconf_error = NULL;
2924 gboolean use_proxy_auth;
2928 gl_client = gconf_client_get_default ();
2930 gl_mutex = g_mutex_new ();
2932 gconf_client_add_dir (gl_client, PATH_GCONF_GNOME_VFS, GCONF_CLIENT_PRELOAD_ONELEVEL, &gconf_error);
2934 DEBUG_HTTP (("GConf error during client_add_dir '%s'", gconf_error->message));
2935 g_error_free (gconf_error);
2939 gconf_client_notify_add (gl_client, PATH_GCONF_GNOME_VFS, notify_gconf_value_changed, NULL, NULL, &gconf_error);
2941 DEBUG_HTTP (("GConf error during notify_error '%s'", gconf_error->message));
2942 g_error_free (gconf_error);
2946 /* Load the http proxy setting */
2947 use_proxy = gconf_client_get_bool (gl_client, KEY_GCONF_USE_HTTP_PROXY, &gconf_error);
2949 if (gconf_error != NULL) {
2950 DEBUG_HTTP (("GConf error during client_get_bool '%s'", gconf_error->message));
2951 g_error_free (gconf_error);
2954 construct_gl_http_proxy (use_proxy);
2957 use_proxy_auth = gconf_client_get_bool (gl_client, KEY_GCONF_HTTP_USE_AUTH, &gconf_error);
2959 if (gconf_error != NULL) {
2960 DEBUG_HTTP (("GConf error during client_get_bool '%s'", gconf_error->message));
2961 g_error_free (gconf_error);
2964 set_proxy_auth (use_proxy_auth);
2974 vfs_module_shutdown (GnomeVFSMethod *method)
2976 g_object_unref (G_OBJECT (gl_client));
2978 http_authn_shutdown ();
2980 http_cache_shutdown();
2982 g_mutex_free (gl_mutex);
2987 /* A "409 Conflict" currently maps to GNOME_VFS_ERROR_NOT_FOUND because it can be returned
2988 * when the parent collection/directory does not exist. Unfortunately, Xythos also returns
2989 * this code when the destination filename of a PUT, MKCOL, or MOVE contains illegal characters,
2990 * eg "my*file:name".
2992 * The only way to resolve this is to ask...
2995 static GnomeVFSResult
2996 resolve_409 (GnomeVFSMethod *method, GnomeVFSURI *uri, GnomeVFSContext *context)
2998 GnomeVFSFileInfo *file_info;
2999 GnomeVFSURI *parent_dest_uri;
3000 GnomeVFSResult result;
3003 ANALYZE_HTTP ("==> +resolving 409");
3005 file_info = gnome_vfs_file_info_new ();
3006 parent_dest_uri = gnome_vfs_uri_get_parent (uri);
3008 if (parent_dest_uri != NULL) {
3009 result = do_get_file_info (method,
3012 GNOME_VFS_FILE_INFO_DEFAULT,
3015 gnome_vfs_file_info_unref (file_info);
3018 gnome_vfs_uri_unref (parent_dest_uri);
3019 parent_dest_uri = NULL;
3021 result = GNOME_VFS_ERROR_NOT_FOUND;
3024 if (result == GNOME_VFS_OK) {
3025 /* The destination filename contains characters that are not allowed
3026 * by the server. This is a bummer mapping, but EINVAL is what
3027 * the Linux FAT filesystems return on bad filenames, so at least
3028 * its not without precedent...
3030 result = GNOME_VFS_ERROR_BAD_PARAMETERS;
3032 /* The destination's parent path does not exist */
3033 result = GNOME_VFS_ERROR_NOT_FOUND;
3036 ANALYZE_HTTP ("==> -resolving 409");
3042 invoke_callback_headers_received (HttpFileHandle *handle)
3044 GnomeVFSModuleCallbackReceivedHeadersIn in_args;
3045 GnomeVFSModuleCallbackReceivedHeadersOut out_args;
3046 gboolean ret = FALSE;
3048 memset (&in_args, 0, sizeof (in_args));
3049 memset (&out_args, 0, sizeof (out_args));
3051 in_args.uri = handle->uri;
3052 in_args.headers = handle->response_headers;
3054 ret = gnome_vfs_module_callback_invoke (GNOME_VFS_MODULE_CALLBACK_HTTP_RECEIVED_HEADERS,
3055 &in_args, sizeof (in_args),
3056 &out_args, sizeof (out_args));
3062 invoke_callback_send_additional_headers (GnomeVFSURI *uri,
3065 GnomeVFSModuleCallbackAdditionalHeadersIn in_args;
3066 GnomeVFSModuleCallbackAdditionalHeadersOut out_args;
3067 gboolean ret = FALSE;
3069 memset (&in_args, 0, sizeof (in_args));
3070 memset (&out_args, 0, sizeof (out_args));
3074 ret = gnome_vfs_module_callback_invoke (GNOME_VFS_MODULE_CALLBACK_HTTP_SEND_ADDITIONAL_HEADERS,
3075 &in_args, sizeof (in_args),
3076 &out_args, sizeof (out_args));
3079 *headers = out_args.headers;
3083 if (out_args.headers) {
3084 g_list_foreach (out_args.headers, (GFunc)g_free, NULL);
3085 g_list_free (out_args.headers);
3094 invoke_callback_basic_authn (HttpFileHandle *handle,
3095 enum AuthnHeaderType authn_which,
3096 gboolean previous_attempt_failed)
3098 GnomeVFSModuleCallbackAuthenticationIn in_args;
3099 GnomeVFSModuleCallbackAuthenticationOut out_args;
3104 memset (&in_args, 0, sizeof (in_args));
3105 memset (&out_args, 0, sizeof (out_args));
3107 in_args.previous_attempt_failed = previous_attempt_failed;
3109 in_args.uri = gnome_vfs_uri_to_string (handle->uri, GNOME_VFS_URI_HIDE_NONE);
3111 ret = http_authn_parse_response_header_basic (authn_which, handle->response_headers, &in_args.realm);
3117 DEBUG_HTTP (("Invoking %s authentication callback for uri %s",
3118 authn_which == AuthnHeader_WWW ? "basic" : "proxy", in_args.uri));
3120 in_args.auth_type = AuthTypeBasic;
3122 ret = gnome_vfs_module_callback_invoke (authn_which == AuthnHeader_WWW
3123 ? GNOME_VFS_MODULE_CALLBACK_AUTHENTICATION
3124 : GNOME_VFS_MODULE_CALLBACK_HTTP_PROXY_AUTHENTICATION,
3125 &in_args, sizeof (in_args),
3126 &out_args, sizeof (out_args));
3129 DEBUG_HTTP (("No callback registered"));
3133 ret = (out_args.username != NULL);
3136 DEBUG_HTTP (("No username provided by callback"));
3140 DEBUG_HTTP (("Back from authentication callback, adding credentials"));
3142 if (authn_which == AuthnHeader_WWW) {
3143 http_authn_session_add_credentials (handle->uri, out_args.username, out_args.password);
3144 } else /* if (authn_which == AuthnHeader_Proxy) */ {
3145 proxy_set_authn (out_args.username, out_args.password);
3148 g_free (in_args.uri);
3149 g_free (in_args.realm);
3150 g_free (out_args.username);
3151 g_free (out_args.password);
3157 strcmp_allow_nulls (const char *s1, const char *s2)
3159 return strcmp (s1 == NULL ? "" : s1, s2 == NULL ? "" : s2);
3163 /* Returns TRUE if the given URL has changed authentication credentials
3164 * from the last request (eg, another thread updated the authn information)
3165 * or if the application provided new credentials via a callback
3167 * prev_authn_header is NULL if the previous request contained no authn information.
3171 check_authn_retry_request (HttpFileHandle * http_handle,
3172 enum AuthnHeaderType authn_which,
3173 const char *prev_authn_header)
3176 char *current_authn_header;
3178 current_authn_header = NULL;
3180 g_mutex_lock (gl_mutex);
3182 if (authn_which == AuthnHeader_WWW) {
3183 current_authn_header = http_authn_get_header_for_uri (http_handle->uri);
3184 } else if (authn_which == AuthnHeader_Proxy) {
3185 current_authn_header = proxy_get_authn_header_for_uri_nolock (http_handle->uri);
3187 g_assert_not_reached ();
3191 if (0 == strcmp_allow_nulls (current_authn_header, prev_authn_header)) {
3192 ret = invoke_callback_basic_authn (http_handle, authn_which, prev_authn_header == NULL);
3197 g_mutex_unlock (gl_mutex);
3199 g_free (current_authn_header);
3206 http_util_get_utime (void)
3209 gettimeofday (&tmp, NULL);
3210 return (utime_t)tmp.tv_usec + ((gint64)tmp.tv_sec) * 1000000LL;
3214 /* BASE64 code ported from neon (http://www.webdav.org/neon) */
3215 static const gchar b64_alphabet[65] = {
3216 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
3217 "abcdefghijklmnopqrstuvwxyz"
3221 http_util_base64 (const gchar *text)
3223 /* The tricky thing about this is doing the padding at the end,
3224 * doing the bit manipulation requires a bit of concentration only */
3225 gchar *buffer, *point;
3228 /* Use 'buffer' to store the output. Work out how big it should be...
3229 * This must be a multiple of 4 bytes
3232 inlen = strlen (text);
3233 outlen = (inlen*4)/3;
3234 if ((inlen % 3) > 0) { /* got to pad */
3235 outlen += 4 - (inlen % 3);
3238 buffer = g_malloc (outlen + 1); /* +1 for the \0 */
3240 /* now do the main stage of conversion, 3 bytes at a time,
3241 * leave the trailing bytes (if there are any) for later
3244 for (point=buffer; inlen>=3; inlen-=3, text+=3) {
3245 *(point++) = b64_alphabet[ (*text)>>2 ];
3246 *(point++) = b64_alphabet[ ((*text)<<4 & 0x30) | (*(text+1))>>4 ];
3247 *(point++) = b64_alphabet[ ((*(text+1))<<2 & 0x3c) | (*(text+2))>>6 ];
3248 *(point++) = b64_alphabet[ (*(text+2)) & 0x3f ];
3251 /* Now deal with the trailing bytes */
3253 /* We always have one trailing byte */
3254 *(point++) = b64_alphabet[ (*text)>>2 ];
3255 *(point++) = b64_alphabet[ ( ((*text)<<4 & 0x30) |
3256 (inlen==2?(*(text+1))>>4:0) ) ];
3257 *(point++) = (inlen==1?'=':b64_alphabet[ (*(text+1))<<2 & 0x3c ] );
3261 /* Null-terminate */
3267 static gboolean at_least_one_test_failed = FALSE;
3270 test_failed (const char *format, ...)
3275 va_start (arguments, format);
3276 message = g_strdup_vprintf (format, arguments);
3279 g_message ("test failed: %s", message);
3280 at_least_one_test_failed = TRUE;
3283 #define VERIFY_BOOLEAN_RESULT(function, expected) \
3285 gboolean result = function; \
3286 if (! ((result && expected) || (!result && !expected))) { \
3287 test_failed ("%s: returned '%d' expected '%d'", #function, (int)result, (int)expected); \
3293 http_self_test (void)
3295 g_message ("self-test: http\n");
3297 VERIFY_BOOLEAN_RESULT (proxy_should_for_hostname ("localhost"), FALSE);
3298 VERIFY_BOOLEAN_RESULT (proxy_should_for_hostname ("LocalHost"), FALSE);
3299 VERIFY_BOOLEAN_RESULT (proxy_should_for_hostname ("127.0.0.1"), FALSE);
3300 VERIFY_BOOLEAN_RESULT (proxy_should_for_hostname ("127.127.0.1"), FALSE);
3301 VERIFY_BOOLEAN_RESULT (proxy_should_for_hostname ("www.yahoo.com"), TRUE);
3303 return !at_least_one_test_failed;
3306 gboolean vfs_module_self_test (void);
3309 vfs_module_self_test (void)
3315 ret = http_authn_self_test () && ret;
3317 ret = http_self_test () && ret;