mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-11-10 11:26:16 +01:00
7261372b45
2008-08-13 Tor Lillqvist <tml@novell.com> * win32/gwinhttpvfs.c * win32/gwinhttpvfs.h * win32/gwinhttpfile.c * win32/gwinhttpfile.h * win32/gwinhttpfileinputstream.c * win32/gwinhttpfileinputstream.h * win32/gwinhttpfileoutputstream.c * win32/gwinhttpfileoutputstream.h: New files implementing GWinHttpVfs and related classes, a GVfs for HTTP and HTTPS URIs on Windows. The implementation uses the WinHttp API. Both reading and writing are supported, i.e. GET and PUT requests. When writing, each write call is done using a separate PUT request with a Content-Range header. Requests for file URIs and plain pathnames are forwarded to GLocalVfs. * win32/winhttp.h: Reverse engineered <winhttp.h>, borrowed from WINE. Used as there is no <winhttp.h> bundled with mingw, and requiring people to download the Windows SDK just for this one header is not reasonable. * win32/Makefile.am: Add above files. * giomodule.c: Call _g_winhttp_vfs_get_type() on Windows to set up the plumbing for the above. svn path=/trunk/; revision=7344
275 lines
9.2 KiB
C
275 lines
9.2 KiB
C
/* GIO - GLib Input, Output and Streaming Library
|
|
*
|
|
* Copyright (C) 2006-2007 Red Hat, Inc.
|
|
* Copyright (C) 2008 Novell, Inc.
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2 of the License, or (at your option) any later version.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General
|
|
* Public License along with this library; if not, write to the
|
|
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
* Boston, MA 02111-1307, USA.
|
|
*
|
|
* Author: Alexander Larsson <alexl@redhat.com>
|
|
* Author: Tor Lillqvist <tml@novell.com>
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
#include "giomodule.h"
|
|
#include "gvfs.h"
|
|
|
|
#include "gwinhttpfile.h"
|
|
#include "gwinhttpvfs.h"
|
|
|
|
#include "gioalias.h"
|
|
|
|
#define g_winhttp_vfs_get_type _g_winhttp_vfs_get_type
|
|
G_DEFINE_TYPE_WITH_CODE (GWinHttpVfs, g_winhttp_vfs, G_TYPE_VFS,
|
|
g_io_extension_point_implement (G_VFS_EXTENSION_POINT_NAME,
|
|
g_define_type_id,
|
|
"winhttp",
|
|
10))
|
|
|
|
static const gchar *winhttp_uri_schemes[] = { "http", "https" };
|
|
|
|
static void
|
|
g_winhttp_vfs_finalize (GObject *object)
|
|
{
|
|
GWinHttpVfs *vfs;
|
|
|
|
vfs = G_WINHTTP_VFS (object);
|
|
|
|
(G_WINHTTP_VFS_GET_CLASS (vfs)->pWinHttpCloseHandle) (vfs->session);
|
|
vfs->session = NULL;
|
|
|
|
if (vfs->wrapped_vfs)
|
|
g_object_unref (vfs->wrapped_vfs);
|
|
vfs->wrapped_vfs = NULL;
|
|
|
|
G_OBJECT_CLASS (g_winhttp_vfs_parent_class)->finalize (object);
|
|
}
|
|
|
|
static void
|
|
g_winhttp_vfs_init (GWinHttpVfs *vfs)
|
|
{
|
|
wchar_t *wagent;
|
|
|
|
vfs->wrapped_vfs = g_vfs_get_local ();
|
|
|
|
wagent = g_utf8_to_utf16 (g_get_prgname (), -1, NULL, NULL, NULL);
|
|
|
|
if (!wagent)
|
|
wagent = g_utf8_to_utf16 ("GWinHttpVfs", -1, NULL, NULL, NULL);
|
|
|
|
vfs->session = (G_WINHTTP_VFS_GET_CLASS (vfs)->pWinHttpOpen)
|
|
(wagent,
|
|
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
|
|
WINHTTP_NO_PROXY_NAME,
|
|
WINHTTP_NO_PROXY_BYPASS,
|
|
0);
|
|
}
|
|
|
|
/**
|
|
* g_winhttp_vfs_new:
|
|
*
|
|
* Returns a new #GVfs handle for a WinHttp vfs.
|
|
*
|
|
* Returns: a new #GVfs handle.
|
|
**/
|
|
GVfs *
|
|
_g_winhttp_vfs_new (void)
|
|
{
|
|
return g_object_new (G_TYPE_WINHTTP_VFS, NULL);
|
|
}
|
|
|
|
static GFile *
|
|
g_winhttp_vfs_get_file_for_path (GVfs *vfs,
|
|
const char *path)
|
|
{
|
|
return g_vfs_get_file_for_path (G_WINHTTP_VFS (vfs)->wrapped_vfs, path);
|
|
}
|
|
|
|
static GFile *
|
|
g_winhttp_vfs_get_file_for_uri (GVfs *vfs,
|
|
const char *uri)
|
|
{
|
|
GWinHttpVfs *winhttp_vfs = G_WINHTTP_VFS (vfs);
|
|
int i;
|
|
|
|
/* If it matches one of "our" schemes, handle it */
|
|
for (i = 0; i < G_N_ELEMENTS (winhttp_uri_schemes); i++)
|
|
if (g_ascii_strncasecmp (uri, winhttp_uri_schemes[i], strlen (winhttp_uri_schemes[i])) == 0 &&
|
|
uri[strlen (winhttp_uri_schemes[i])] == ':')
|
|
return _g_winhttp_file_new (winhttp_vfs, uri);
|
|
|
|
/* For other URIs fallback to the wrapped GVfs */
|
|
return g_vfs_parse_name (winhttp_vfs->wrapped_vfs, uri);
|
|
}
|
|
|
|
static const gchar * const *
|
|
g_winhttp_vfs_get_supported_uri_schemes (GVfs *vfs)
|
|
{
|
|
GWinHttpVfs *winhttp_vfs = G_WINHTTP_VFS (vfs);
|
|
const gchar * const *wrapped_vfs_uri_schemes = g_vfs_get_supported_uri_schemes (winhttp_vfs->wrapped_vfs);
|
|
int i, n;
|
|
const gchar **retval;
|
|
|
|
n = 0;
|
|
while (wrapped_vfs_uri_schemes[n] != NULL)
|
|
n++;
|
|
|
|
retval = g_new (const gchar *, n + G_N_ELEMENTS (winhttp_uri_schemes) + 1);
|
|
n = 0;
|
|
while (wrapped_vfs_uri_schemes[n] != NULL)
|
|
{
|
|
retval[n] = wrapped_vfs_uri_schemes[n];
|
|
n++;
|
|
}
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (winhttp_uri_schemes); i++)
|
|
{
|
|
retval[n] = winhttp_uri_schemes[i];
|
|
n++;
|
|
}
|
|
|
|
retval[n] = NULL;
|
|
|
|
return retval;
|
|
}
|
|
|
|
static GFile *
|
|
g_winhttp_vfs_parse_name (GVfs *vfs,
|
|
const char *parse_name)
|
|
{
|
|
GWinHttpVfs *winhttp_vfs = G_WINHTTP_VFS (vfs);
|
|
|
|
g_return_val_if_fail (G_IS_VFS (vfs), NULL);
|
|
g_return_val_if_fail (parse_name != NULL, NULL);
|
|
|
|
/* For plain file paths fallback to the wrapped GVfs */
|
|
if (g_path_is_absolute (parse_name))
|
|
return g_vfs_parse_name (winhttp_vfs->wrapped_vfs, parse_name);
|
|
|
|
/* Otherwise assume it is an URI, so pass on to
|
|
* g_winhttp_vfs_get_file_for_uri().
|
|
*/
|
|
return g_winhttp_vfs_get_file_for_uri (vfs, parse_name);
|
|
}
|
|
|
|
static gboolean
|
|
g_winhttp_vfs_is_active (GVfs *vfs)
|
|
{
|
|
return TRUE;
|
|
}
|
|
|
|
static void
|
|
g_winhttp_vfs_class_init (GWinHttpVfsClass *class)
|
|
{
|
|
GObjectClass *object_class;
|
|
GVfsClass *vfs_class;
|
|
HMODULE winhttp;
|
|
|
|
object_class = (GObjectClass *) class;
|
|
|
|
object_class->finalize = g_winhttp_vfs_finalize;
|
|
|
|
vfs_class = G_VFS_CLASS (class);
|
|
|
|
vfs_class->is_active = g_winhttp_vfs_is_active;
|
|
vfs_class->get_file_for_path = g_winhttp_vfs_get_file_for_path;
|
|
vfs_class->get_file_for_uri = g_winhttp_vfs_get_file_for_uri;
|
|
vfs_class->get_supported_uri_schemes = g_winhttp_vfs_get_supported_uri_schemes;
|
|
vfs_class->parse_name = g_winhttp_vfs_parse_name;
|
|
|
|
winhttp = LoadLibrary ("winhttp.dll");
|
|
if (winhttp != NULL)
|
|
{
|
|
class->pWinHttpCloseHandle = (BOOL (WINAPI *) (HINTERNET)) GetProcAddress (winhttp, "WinHttpCloseHandle");
|
|
class->pWinHttpCrackUrl = (BOOL (WINAPI *) (LPCWSTR,DWORD,DWORD,LPURL_COMPONENTS)) GetProcAddress (winhttp, "WinHttpCrackUrl");
|
|
class->pWinHttpConnect = (HINTERNET (WINAPI *) (HINTERNET,LPCWSTR,INTERNET_PORT,DWORD)) GetProcAddress (winhttp, "WinHttpConnect");
|
|
class->pWinHttpCreateUrl = (BOOL (WINAPI *) (LPURL_COMPONENTS,DWORD,LPWSTR,LPDWORD)) GetProcAddress (winhttp, "WinHttpCreateUrl");
|
|
class->pWinHttpOpen = (HINTERNET (WINAPI *) (LPCWSTR,DWORD,LPCWSTR,LPCWSTR,DWORD)) GetProcAddress (winhttp, "WinHttpOpen");
|
|
class->pWinHttpOpenRequest = (HINTERNET (WINAPI *) (HINTERNET,LPCWSTR,LPCWSTR,LPCWSTR,LPCWSTR,LPCWSTR*,DWORD)) GetProcAddress (winhttp, "WinHttpOpenRequest");
|
|
class->pWinHttpQueryDataAvailable = (BOOL (WINAPI *) (HINTERNET,LPDWORD)) GetProcAddress (winhttp, "WinHttpQueryDataAvailable");
|
|
class->pWinHttpQueryHeaders = (BOOL (WINAPI *) (HINTERNET,DWORD,LPCWSTR,LPVOID,LPDWORD,LPDWORD)) GetProcAddress (winhttp, "WinHttpQueryHeaders");
|
|
class->pWinHttpReadData = (BOOL (WINAPI *) (HINTERNET,LPVOID,DWORD,LPDWORD)) GetProcAddress (winhttp, "WinHttpReadData");
|
|
class->pWinHttpReceiveResponse = (BOOL (WINAPI *) (HINTERNET,LPVOID)) GetProcAddress (winhttp, "WinHttpReceiveResponse");
|
|
class->pWinHttpSendRequest = (BOOL (WINAPI *) (HINTERNET,LPCWSTR,DWORD,LPVOID,DWORD,DWORD,DWORD_PTR)) GetProcAddress (winhttp, "WinHttpSendRequest");
|
|
class->pWinHttpWriteData = (BOOL (WINAPI *) (HINTERNET,LPCVOID,DWORD,LPDWORD)) GetProcAddress (winhttp, "WinHttpWriteData");
|
|
}
|
|
}
|
|
|
|
char *
|
|
_g_winhttp_error_message (DWORD error_code)
|
|
{
|
|
/* The FormatMessage() API that g_win32_error_message() uses doesn't
|
|
* seem to know about WinHttp errors, unfortunately.
|
|
*/
|
|
if (error_code >= WINHTTP_ERROR_BASE && error_code < WINHTTP_ERROR_BASE + 200)
|
|
{
|
|
switch (error_code)
|
|
{
|
|
/* FIXME: Use meaningful error messages */
|
|
#define CASE(x) case ERROR_WINHTTP_##x: return g_strdup ("WinHttp error: " #x);
|
|
CASE (AUTO_PROXY_SERVICE_ERROR);
|
|
CASE (AUTODETECTION_FAILED);
|
|
CASE (BAD_AUTO_PROXY_SCRIPT);
|
|
CASE (CANNOT_CALL_AFTER_OPEN);
|
|
CASE (CANNOT_CALL_AFTER_SEND);
|
|
CASE (CANNOT_CALL_BEFORE_OPEN);
|
|
CASE (CANNOT_CALL_BEFORE_SEND);
|
|
CASE (CANNOT_CONNECT);
|
|
CASE (CHUNKED_ENCODING_HEADER_SIZE_OVERFLOW);
|
|
CASE (CLIENT_AUTH_CERT_NEEDED);
|
|
CASE (CONNECTION_ERROR);
|
|
CASE (HEADER_ALREADY_EXISTS);
|
|
CASE (HEADER_COUNT_EXCEEDED);
|
|
CASE (HEADER_NOT_FOUND);
|
|
CASE (HEADER_SIZE_OVERFLOW);
|
|
CASE (INCORRECT_HANDLE_STATE);
|
|
CASE (INCORRECT_HANDLE_TYPE);
|
|
CASE (INTERNAL_ERROR);
|
|
CASE (INVALID_OPTION);
|
|
CASE (INVALID_QUERY_REQUEST);
|
|
CASE (INVALID_SERVER_RESPONSE);
|
|
CASE (INVALID_URL);
|
|
CASE (LOGIN_FAILURE);
|
|
CASE (NAME_NOT_RESOLVED);
|
|
CASE (NOT_INITIALIZED);
|
|
CASE (OPERATION_CANCELLED);
|
|
CASE (OPTION_NOT_SETTABLE);
|
|
CASE (OUT_OF_HANDLES);
|
|
CASE (REDIRECT_FAILED);
|
|
CASE (RESEND_REQUEST);
|
|
CASE (RESPONSE_DRAIN_OVERFLOW);
|
|
CASE (SECURE_CERT_CN_INVALID);
|
|
CASE (SECURE_CERT_DATE_INVALID);
|
|
CASE (SECURE_CERT_REV_FAILED);
|
|
CASE (SECURE_CERT_REVOKED);
|
|
CASE (SECURE_CERT_WRONG_USAGE);
|
|
CASE (SECURE_CHANNEL_ERROR);
|
|
CASE (SECURE_FAILURE);
|
|
CASE (SECURE_INVALID_CA);
|
|
CASE (SECURE_INVALID_CERT);
|
|
CASE (SHUTDOWN);
|
|
CASE (TIMEOUT);
|
|
CASE (UNABLE_TO_DOWNLOAD_SCRIPT);
|
|
CASE (UNRECOGNIZED_SCHEME);
|
|
#undef CASE
|
|
default:
|
|
return g_strdup_printf ("WinHttp error %ld", error_code);
|
|
}
|
|
}
|
|
else
|
|
return g_win32_error_message (error_code);
|
|
}
|