Permit close&open of 'HttpFileHandle' in-place (no address change).
authorshort <>
Mon, 1 Sep 2003 15:23:22 +0000 (15:23 +0000)
committershort <>
Mon, 1 Sep 2003 15:23:22 +0000 (15:23 +0000)
modules/http-method.c

index c2b3dc7..317513d 100644 (file)
@@ -283,25 +283,24 @@ defaults_file_info_new (void)
        return ret;
 }
 
-static HttpFileHandle *
-http_file_handle_new (GnomeVFSSocketBuffer *socket_buffer,
+/* Does not allocate the 'handle' memory. */
+static void
+http_file_handle_new (HttpFileHandle *handle,
+                     GnomeVFSSocketBuffer *socket_buffer,
                      GnomeVFSURI *uri)
 {
-       HttpFileHandle *result;
-
-       result = g_new0 (HttpFileHandle, 1);
+       memset (handle, 0, sizeof (*handle));
 
-       result->socket_buffer = socket_buffer;
-       result->uri_string = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE );
-       result->uri = uri;
-       gnome_vfs_uri_ref(result->uri);
+       handle->socket_buffer = socket_buffer;
+       handle->uri_string = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE );
+       handle->uri = uri;
+       gnome_vfs_uri_ref(handle->uri);
 
-       result->file_info = defaults_file_info_new();
-       result->file_info->name = gnome_vfs_uri_extract_short_name (uri);
-
-       return result;
+       handle->file_info = defaults_file_info_new();
+       handle->file_info->name = gnome_vfs_uri_extract_short_name (uri);
 }
 
+/* Does not free the 'handle' memory. */
 static void
 http_file_handle_destroy (HttpFileHandle *handle)
 {
@@ -321,8 +320,6 @@ http_file_handle_destroy (HttpFileHandle *handle)
 
        g_list_foreach(handle->files, (GFunc)gnome_vfs_file_info_unref, NULL);
        g_list_free(handle->files);
-
-       g_free (handle);
 }
 
 /* The following comes from GNU Wget with minor changes by myself.
@@ -652,15 +649,15 @@ static GnomeVFSResult
 create_handle (GnomeVFSURI *uri,
               GnomeVFSSocketBuffer *socket_buffer,
               GnomeVFSContext *context,
-              /* OUT */ HttpFileHandle **p_handle)
+              /* OUT */ HttpFileHandle *handle)
 {
        GString *header_string;
        GnomeVFSResult result;
        guint server_status;
 
-       g_return_val_if_fail (p_handle != NULL, GNOME_VFS_ERROR_INTERNAL);
+       g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_INTERNAL);
 
-       *p_handle = http_file_handle_new (socket_buffer, uri);
+       http_file_handle_new (handle, socket_buffer, uri);
 
        header_string = g_string_new (NULL);
 
@@ -678,7 +675,7 @@ create_handle (GnomeVFSURI *uri,
                goto error;
        }
 
-       (*p_handle)->server_status = server_status;
+       handle->server_status = server_status;
 
        ANALYZE_HTTP ("==> +create_handle: fetching headers");
 
@@ -694,7 +691,7 @@ create_handle (GnomeVFSURI *uri,
                        break;
                }
 
-               (*p_handle)->response_headers = g_list_prepend ((*p_handle)->response_headers, 
+               handle->response_headers = g_list_prepend (handle->response_headers, 
                                                        g_strdup (header_string->str));
 
                /* We don't really care if we successfully parse the
@@ -704,10 +701,10 @@ create_handle (GnomeVFSURI *uri,
                 * past we would return NOT_FOUND if any header could
                 * not be parsed, but that seems wrong.
                 */
-               parse_header (*p_handle, header_string->str);
+               parse_header (handle, header_string->str);
        }
 
-       invoke_callback_headers_received (*p_handle);
+       invoke_callback_headers_received (handle);
 
        ANALYZE_HTTP ("==> -create_handle: fetching headers");
 
@@ -1499,7 +1496,7 @@ error:
 }
 
 static GnomeVFSResult
-make_request (HttpFileHandle **handle_return,
+make_request (HttpFileHandle *handle,
              GnomeVFSURI *uri,
              const gchar *method,
              GByteArray *data,
@@ -1514,8 +1511,7 @@ make_request (HttpFileHandle **handle_return,
        char *authn_header_request;
        char *authn_header_proxy;
 
-       g_return_val_if_fail (handle_return != NULL, GNOME_VFS_ERROR_INTERNAL);
-       *handle_return = NULL;
+       g_return_val_if_fail (handle != NULL, GNOME_VFS_ERROR_INTERNAL);
 
        ANALYZE_HTTP ("==> +make_request");
 
@@ -1593,33 +1589,33 @@ make_request (HttpFileHandle **handle_return,
                }
 
                /* Read the headers and create our internal HTTP file handle.  */
-               result = create_handle (uri, socket_buffer, context, handle_return);
+               result = create_handle (uri, socket_buffer, context, handle);
 
                if (result == GNOME_VFS_OK) {
                        socket_buffer = NULL;
                        break;
                }
-               if ((*handle_return)->server_status == HTTP_STATUS_UNAUTHORIZED) {
-                       if (! check_authn_retry_request (*handle_return, AuthnHeader_WWW, authn_header_request)) {
+               if (handle->server_status == HTTP_STATUS_UNAUTHORIZED) {
+                       if (! check_authn_retry_request (handle, AuthnHeader_WWW, authn_header_request)) {
                                break;
                        }
-               } else if ((*handle_return)->server_status == HTTP_STATUS_PROXY_AUTH_REQUIRED) {
-                       if (! check_authn_retry_request (*handle_return, AuthnHeader_WWW, authn_header_proxy)) {
+               } else if (handle->server_status == HTTP_STATUS_PROXY_AUTH_REQUIRED) {
+                       if (! check_authn_retry_request (handle, AuthnHeader_WWW, authn_header_proxy)) {
                                break;
                        }
                } else {
                        break;
                }
-               http_file_handle_destroy (*handle_return);
-               *handle_return = NULL;
+               http_file_handle_destroy (handle);
+               handle = NULL;
        }
 
        g_free (authn_header_request);
        g_free (authn_header_proxy);
 
-       if (result != GNOME_VFS_OK && *handle_return != NULL) {
-               http_file_handle_destroy (*handle_return);
-               *handle_return = NULL;
+       if (result != GNOME_VFS_OK && handle != NULL) {
+               http_file_handle_destroy (handle);
+               handle = NULL;
        }
 
        if (request != NULL) {
@@ -1672,12 +1668,13 @@ do_open (GnomeVFSMethod *method,
        ANALYZE_HTTP ("==> +do_open");
        DEBUG_HTTP (("+Open URI: '%s' mode:'%c'", gnome_vfs_uri_to_string(uri, 0), 
                     mode & GNOME_VFS_OPEN_READ ? 'R' : 'W'));
-       
+
+       handle = g_new (HttpFileHandle, 1);
        if (mode & GNOME_VFS_OPEN_READ) {
-               result = make_request (&handle, uri, "GET", NULL, NULL,
+               result = make_request (handle, uri, "GET", NULL, NULL,
                                       context);
        } else {
-               handle = http_file_handle_new(NULL, uri); /* shrug */
+               http_file_handle_new(handle, NULL, uri); /* shrug */
        }
        if (result == GNOME_VFS_OK) {
                *method_handle = (GnomeVFSMethodHandle *) handle;
@@ -1722,9 +1719,11 @@ do_create (GnomeVFSMethod *method,
                
                ANALYZE_HTTP ("==> Checking to see if file exists");
                
-               result = make_request (&handle, uri, "HEAD", NULL, NULL,
+               handle = g_new (HttpFileHandle, 1);
+               result = make_request (handle, uri, "HEAD", NULL, NULL,
                                       context);
                http_handle_close (handle, context);
+               g_free (handle);
                
                if (result != GNOME_VFS_OK &&
                    result != GNOME_VFS_ERROR_NOT_FOUND) {
@@ -1737,8 +1736,10 @@ do_create (GnomeVFSMethod *method,
        
        ANALYZE_HTTP ("==> Creating initial file");
        
-       result = make_request (&handle, uri, "PUT", bytes, NULL, context);
+       handle = g_new (HttpFileHandle, 1);
+       result = make_request (handle, uri, "PUT", bytes, NULL, context);
        http_handle_close(handle, context);
+       g_free (handle);
        
        if (result != GNOME_VFS_OK) {
                /* the PUT failed */
@@ -1815,15 +1816,18 @@ do_close (GnomeVFSMethod *method,
                http_cache_invalidate_uri (uri);
 
                ANALYZE_HTTP ("==> doing PUT");
-               result = make_request (&new_handle, uri, "PUT", bytes, 
+               new_handle = g_new (HttpFileHandle, 1);
+               result = make_request (new_handle, uri, "PUT", bytes, 
                                       extraheader, context);
                g_free (extraheader);
                http_handle_close (new_handle, context);
+               g_free (new_handle);
        } else {
                result = GNOME_VFS_OK;
        }
        
        http_handle_close (old_handle, context);
+       g_free (old_handle);
        
        DEBUG_HTTP (("-Close (%d)", result));
        ANALYZE_HTTP ("==> -do_close");
@@ -2150,7 +2154,7 @@ process_propfind_response(xmlNodePtr n,
 
 
 static GnomeVFSResult
-make_propfind_request (HttpFileHandle **handle_return,
+make_propfind_request (HttpFileHandle *handle,
        GnomeVFSURI *uri,
        gint depth,
        GnomeVFSContext *context)
@@ -2187,7 +2191,7 @@ make_propfind_request (HttpFileHandle **handle_return,
                http_cache_invalidate_uri_and_children (uri);
        }
 
-       result = make_request (handle_return, uri, "PROPFIND", request, 
+       result = make_request (handle, uri, "PROPFIND", request, 
                               extraheaders, context);
        
        /* FIXME bugzilla.gnome.org 43834: It looks like some http
@@ -2196,14 +2200,14 @@ make_propfind_request (HttpFileHandle **handle_return,
         * redirects or any other legal response. This case probably
         * needs to be made more robust.
         */
-       if (result == GNOME_VFS_OK && (*handle_return)->server_status != 207) { /* Multi-Status */
-               DEBUG_HTTP (("HTTP server returned an invalid PROPFIND response: %d", (*handle_return)->server_status));
+       if (result == GNOME_VFS_OK && handle->server_status != 207) { /* Multi-Status */
+               DEBUG_HTTP (("HTTP server returned an invalid PROPFIND response: %d", handle->server_status));
                result = GNOME_VFS_ERROR_NOT_SUPPORTED;
        }
        
        if (result == GNOME_VFS_OK) {
                do {
-                       result = do_read (NULL, (GnomeVFSMethodHandle *) *handle_return,
+                       result = do_read (NULL, (GnomeVFSMethodHandle *) handle,
                                          buffer, num_bytes, &bytes_read, context);
                        
                        if (result != GNOME_VFS_OK ) {
@@ -2257,15 +2261,15 @@ make_propfind_request (HttpFileHandle **handle_return,
                                process_propfind_response (cur->xmlChildrenNode, uri);
                        
                        if (file_info->name != NULL) { 
-                               (*handle_return)->files = g_list_append ((*handle_return)->files, file_info);
+                               handle->files = g_list_append (handle->files, file_info);
                        } else {
                                /* This response refers to the root node */
                                /* Abandon the old information that came from create_handle*/
                                
-                               file_info->name = (*handle_return)->file_info->name;
-                               (*handle_return)->file_info->name = NULL;
-                               gnome_vfs_file_info_unref ((*handle_return)->file_info);
-                               (*handle_return)->file_info = file_info;
+                               file_info->name = handle->file_info->name;
+                               handle->file_info->name = NULL;
+                               gnome_vfs_file_info_unref (handle->file_info);
+                               handle->file_info = file_info;
                                found_root_node_props = TRUE;
                        }
                        
@@ -2289,9 +2293,9 @@ make_propfind_request (HttpFileHandle **handle_return,
         */
        
        if (depth == 0) {
-               http_cache_add_uri (uri, (*handle_return)->file_info, TRUE);
+               http_cache_add_uri (uri, handle->file_info, TRUE);
        } else {
-               http_cache_add_uri_and_children (uri, (*handle_return)->file_info, (*handle_return)->files);
+               http_cache_add_uri_and_children (uri, handle->file_info, handle->files);
        }
 
 cleanup:
@@ -2300,8 +2304,8 @@ cleanup:
        xmlFreeParserCtxt(parserContext);
        
        if (result != GNOME_VFS_OK) {
-               http_handle_close (*handle_return, context);
-               *handle_return = NULL;
+               http_handle_close (handle, context);
+               g_free (handle);
        }
        
        ANALYZE_HTTP ("==> -make_propfind_request");
@@ -2347,13 +2351,15 @@ do_open_directory(GnomeVFSMethod *method,
        file_info_cached = http_cache_check_directory_uri (uri, &child_file_info_cached_list);
 
        if (file_info_cached) {
-               handle = http_file_handle_new (NULL, uri);
+               handle = g_new (HttpFileHandle, 1);
+               http_file_handle_new (handle, NULL, uri);
                gnome_vfs_file_info_unref (handle->file_info);
                handle->file_info = file_info_cached;
                handle->files = child_file_info_cached_list;
                result = GNOME_VFS_OK;
        } else {
-               result = make_propfind_request(&handle, uri, 1, context);
+               handle = g_new (HttpFileHandle, 1);
+               result = make_propfind_request(handle, uri, 1, context);
                /* mfleming -- is this necessary?  Most DAV server's I've seen don't have the horrible
                 * lack-of-trailing-/-is-a-301 problem for PROPFIND's
                 */
@@ -2371,6 +2377,7 @@ do_open_directory(GnomeVFSMethod *method,
                    && handle->file_info->type != GNOME_VFS_FILE_TYPE_DIRECTORY) {
                        result = GNOME_VFS_ERROR_NOT_A_DIRECTORY;
                        http_handle_close (handle, context);
+                       g_free (handle);
                        handle = NULL;
                }
        }
@@ -2396,6 +2403,7 @@ do_close_directory (GnomeVFSMethod *method,
        handle = (HttpFileHandle *) method_handle;
        
        http_handle_close(handle, context);
+       g_free (handle);
 
        DEBUG_HTTP (("-Close_Directory (0) handle:0x%08x", (unsigned int) method_handle));
 
@@ -2470,7 +2478,8 @@ do_get_file_info (GnomeVFSMethod *method,
                 * Start off by making a PROPFIND request.  Fall back to a HEAD if it fails
                 */
                
-               result = make_propfind_request (&handle, uri, 0, context);
+               handle = g_new (HttpFileHandle, 1);
+               result = make_propfind_request (handle, uri, 0, context);
                
                /* Note that theoretically we could not bother with this request if we get a 404 back,
                 * but since some servers seem to return wierd things on PROPFIND (mostly 200 OK's...)
@@ -2479,6 +2488,7 @@ do_get_file_info (GnomeVFSMethod *method,
                if (result == GNOME_VFS_OK) {
                        gnome_vfs_file_info_copy (file_info, handle->file_info);
                        http_handle_close (handle, context);
+                       g_free (handle);
                        handle = NULL;
                } else {
                        g_assert (handle == NULL); /* Make sure we're not leaking some old one */
@@ -2493,11 +2503,13 @@ do_get_file_info (GnomeVFSMethod *method,
 
                                ANALYZE_HTTP ("==> do_get_file_info: do GET ");
 
-                               result = make_request (&handle, uri, "GET", NULL, NULL, context);
+                               handle = g_new (HttpFileHandle, 1);
+                               result = make_request (handle, uri, "GET", NULL, NULL, context);
                                if (result == GNOME_VFS_OK) {
                                        gnome_vfs_file_info_copy (file_info, handle->file_info);
                                        http_cache_add_uri (uri, handle->file_info, FALSE);
                                        http_handle_close (handle, context);
+                                       g_free (handle);
                                }
 
                                /* If we get a redirect, we should be
@@ -2589,7 +2601,8 @@ do_make_directory (GnomeVFSMethod *method,
         * So we do a PROPFIND first to find out
         */
        /* FIXME check cache here */
-       result = make_propfind_request(&handle, uri, 0, context);
+       handle = g_new (HttpFileHandle, 1);
+       result = make_propfind_request(handle, uri, 0, context);
 
        if (result == GNOME_VFS_OK) {
                result = GNOME_VFS_ERROR_FILE_EXISTS;
@@ -2599,10 +2612,12 @@ do_make_directory (GnomeVFSMethod *method,
                
                if (result == GNOME_VFS_ERROR_NOT_FOUND) {
                        http_cache_invalidate_uri_parent (uri);
-                       result = make_request (&handle, uri, "MKCOL", NULL, NULL, context);
+                       handle = g_new (HttpFileHandle, 1);
+                       result = make_request (handle, uri, "MKCOL", NULL, NULL, context);
                }
        }
        http_handle_close (handle, context);
+       g_free (handle);
        
        if (result == GNOME_VFS_ERROR_NOT_FOUND) {
                result = resolve_409 (method, uri, context);
@@ -2631,9 +2646,11 @@ do_remove_directory(GnomeVFSMethod *method,
        /* FIXME this should return GNOME_VFS_ERROR_DIRECTORY_NOT_EMPTY if the
         * directory is not empty
         */
-       result = make_request (&handle, uri, "DELETE", NULL, NULL,
+       handle = g_new (HttpFileHandle, 1);
+       result = make_request (handle, uri, "DELETE", NULL, NULL,
                               context);
        http_handle_close (handle, context);
+       g_free (handle);
        
        DEBUG_HTTP (("-Remove_Directory (%d)", result));
        ANALYZE_HTTP ("==> -do_remove_directory");
@@ -2683,8 +2700,10 @@ do_move (GnomeVFSMethod *method,
        destpath = gnome_vfs_uri_to_string (new_uri, GNOME_VFS_URI_HIDE_USER_NAME|GNOME_VFS_URI_HIDE_PASSWORD);
        destheader = g_strdup_printf ("Destination: %s\r\nOverwrite: %c\r\n", destpath, force_replace ? 'T' : 'F' );
 
-       result = make_request (&handle, old_uri, "MOVE", NULL, destheader, context);
+       handle = g_new (HttpFileHandle, 1);
+       result = make_request (handle, old_uri, "MOVE", NULL, destheader, context);
        http_handle_close (handle, context);
+       g_free (handle);
        handle = NULL;
 
        if (result == GNOME_VFS_ERROR_NOT_FOUND) {