Tizen 2.1 base
[platform/upstream/glib2.0.git] / gio / win32 / gwinhttpvfs.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  * Copyright (C) 2008 Novell, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General
17  * Public License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  * Author: Alexander Larsson <alexl@redhat.com>
22  * Author: Tor Lillqvist <tml@novell.com>
23  */
24
25 #include "config.h"
26
27 #include <wchar.h>
28
29 #include "gio/gioerror.h"
30 #include "gio/giomodule.h"
31 #include "gio/gvfs.h"
32
33 #include "gwinhttpfile.h"
34 #include "gwinhttpvfs.h"
35
36 static gboolean lookup_done = FALSE;
37 static gboolean funcs_found = FALSE;
38 static GWinHttpDllFuncs funcs;
39
40 static void
41 lookup_funcs (void)
42 {
43   HMODULE winhttp = NULL;
44   char winhttp_dll[MAX_PATH + 100];
45   int n;
46
47   if (lookup_done)
48     return;
49
50   n = GetSystemDirectory (winhttp_dll, MAX_PATH);
51   if (n > 0 && n < MAX_PATH)
52     {
53         if (winhttp_dll[n-1] != '\\' &&
54             winhttp_dll[n-1] != '/')
55             strcat (winhttp_dll, "\\");
56         strcat (winhttp_dll, "winhttp.dll");
57         winhttp = LoadLibrary (winhttp_dll);
58     }
59
60   if (winhttp != NULL)
61     {
62       funcs.pWinHttpCloseHandle = (BOOL (WINAPI *) (HINTERNET)) GetProcAddress (winhttp, "WinHttpCloseHandle");
63       funcs.pWinHttpCrackUrl = (BOOL (WINAPI *) (LPCWSTR,DWORD,DWORD,LPURL_COMPONENTS)) GetProcAddress (winhttp, "WinHttpCrackUrl");
64       funcs.pWinHttpConnect = (HINTERNET (WINAPI *) (HINTERNET,LPCWSTR,INTERNET_PORT,DWORD)) GetProcAddress (winhttp, "WinHttpConnect");
65       funcs.pWinHttpCreateUrl = (BOOL (WINAPI *) (LPURL_COMPONENTS,DWORD,LPWSTR,LPDWORD)) GetProcAddress (winhttp, "WinHttpCreateUrl");
66       funcs.pWinHttpOpen = (HINTERNET (WINAPI *) (LPCWSTR,DWORD,LPCWSTR,LPCWSTR,DWORD)) GetProcAddress (winhttp, "WinHttpOpen");
67       funcs.pWinHttpOpenRequest = (HINTERNET (WINAPI *) (HINTERNET,LPCWSTR,LPCWSTR,LPCWSTR,LPCWSTR,LPCWSTR*,DWORD)) GetProcAddress (winhttp, "WinHttpOpenRequest");
68       funcs.pWinHttpQueryDataAvailable = (BOOL (WINAPI *) (HINTERNET,LPDWORD)) GetProcAddress (winhttp, "WinHttpQueryDataAvailable");
69       funcs.pWinHttpQueryHeaders = (BOOL (WINAPI *) (HINTERNET,DWORD,LPCWSTR,LPVOID,LPDWORD,LPDWORD)) GetProcAddress (winhttp, "WinHttpQueryHeaders");
70       funcs.pWinHttpReadData = (BOOL (WINAPI *) (HINTERNET,LPVOID,DWORD,LPDWORD)) GetProcAddress (winhttp, "WinHttpReadData");
71       funcs.pWinHttpReceiveResponse = (BOOL (WINAPI *) (HINTERNET,LPVOID)) GetProcAddress (winhttp, "WinHttpReceiveResponse");
72       funcs.pWinHttpSendRequest = (BOOL (WINAPI *) (HINTERNET,LPCWSTR,DWORD,LPVOID,DWORD,DWORD,DWORD_PTR)) GetProcAddress (winhttp, "WinHttpSendRequest");
73       funcs.pWinHttpWriteData = (BOOL (WINAPI *) (HINTERNET,LPCVOID,DWORD,LPDWORD)) GetProcAddress (winhttp, "WinHttpWriteData");
74
75       if (funcs.pWinHttpCloseHandle &&
76           funcs.pWinHttpCrackUrl &&
77           funcs.pWinHttpConnect &&
78           funcs.pWinHttpCreateUrl &&
79           funcs.pWinHttpOpen &&
80           funcs.pWinHttpOpenRequest &&
81           funcs.pWinHttpQueryDataAvailable &&
82           funcs.pWinHttpQueryHeaders &&
83           funcs.pWinHttpReadData &&
84           funcs.pWinHttpReceiveResponse &&
85           funcs.pWinHttpSendRequest &&
86           funcs.pWinHttpWriteData)
87         funcs_found = TRUE;
88     }
89   lookup_done = TRUE;
90 }
91
92 #define g_winhttp_vfs_get_type _g_winhttp_vfs_get_type
93 G_DEFINE_TYPE_WITH_CODE (GWinHttpVfs, g_winhttp_vfs, G_TYPE_VFS,
94                          {
95                            lookup_funcs ();
96                            if (funcs_found)
97                              g_io_extension_point_implement (G_VFS_EXTENSION_POINT_NAME,
98                                                              g_define_type_id,
99                                                              "winhttp",
100                                                              10);
101                          })
102
103 static const gchar *winhttp_uri_schemes[] = { "http", "https" };
104
105 static void
106 g_winhttp_vfs_finalize (GObject *object)
107 {
108   GWinHttpVfs *vfs;
109
110   vfs = G_WINHTTP_VFS (object);
111
112   (G_WINHTTP_VFS_GET_CLASS (vfs)->funcs->pWinHttpCloseHandle) (vfs->session);
113   vfs->session = NULL;
114
115   if (vfs->wrapped_vfs)
116     g_object_unref (vfs->wrapped_vfs);
117   vfs->wrapped_vfs = NULL;
118
119   G_OBJECT_CLASS (g_winhttp_vfs_parent_class)->finalize (object);
120 }
121
122 static void
123 g_winhttp_vfs_init (GWinHttpVfs *vfs)
124 {
125   wchar_t *wagent;
126
127   vfs->wrapped_vfs = g_vfs_get_local ();
128
129   wagent = g_utf8_to_utf16 (g_get_prgname (), -1, NULL, NULL, NULL);
130
131   if (!wagent)
132     wagent = g_utf8_to_utf16 ("GWinHttpVfs", -1, NULL, NULL, NULL);
133
134   vfs->session = (G_WINHTTP_VFS_GET_CLASS (vfs)->funcs->pWinHttpOpen)
135     (wagent,
136      WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
137      WINHTTP_NO_PROXY_NAME,
138      WINHTTP_NO_PROXY_BYPASS,
139      0);
140 }
141
142 /*
143  * g_winhttp_vfs_new:
144  *
145  * Returns a new #GVfs handle for a WinHttp vfs.
146  *
147  * Returns: a new #GVfs handle.
148  **/
149 GVfs *
150 _g_winhttp_vfs_new (void)
151 {
152   return g_object_new (G_TYPE_WINHTTP_VFS, NULL);
153 }
154
155 static GFile *
156 g_winhttp_vfs_get_file_for_path (GVfs       *vfs,
157                                  const char *path)
158 {
159   return g_vfs_get_file_for_path (G_WINHTTP_VFS (vfs)->wrapped_vfs, path);
160 }
161
162 static GFile *
163 g_winhttp_vfs_get_file_for_uri (GVfs       *vfs,
164                                 const char *uri)
165 {
166   GWinHttpVfs *winhttp_vfs = G_WINHTTP_VFS (vfs);
167   int i;
168
169   /* If it matches one of "our" schemes, handle it */
170   for (i = 0; i < G_N_ELEMENTS (winhttp_uri_schemes); i++)
171     if (g_ascii_strncasecmp (uri, winhttp_uri_schemes[i], strlen (winhttp_uri_schemes[i])) == 0 &&
172         uri[strlen (winhttp_uri_schemes[i])] == ':')
173       return _g_winhttp_file_new (winhttp_vfs, uri);
174
175   /* For other URIs fallback to the wrapped GVfs */
176   return g_vfs_parse_name (winhttp_vfs->wrapped_vfs, uri);
177 }
178
179 static const gchar * const *
180 g_winhttp_vfs_get_supported_uri_schemes (GVfs *vfs)
181 {
182   GWinHttpVfs *winhttp_vfs = G_WINHTTP_VFS (vfs);
183   const gchar * const *wrapped_vfs_uri_schemes = g_vfs_get_supported_uri_schemes (winhttp_vfs->wrapped_vfs);
184   int i, n;
185   const gchar **retval;
186
187   n = 0;
188   while (wrapped_vfs_uri_schemes[n] != NULL)
189     n++;
190
191   retval = g_new (const gchar *, n + G_N_ELEMENTS (winhttp_uri_schemes) + 1);
192   n = 0;
193   while (wrapped_vfs_uri_schemes[n] != NULL)
194     {
195       retval[n] = wrapped_vfs_uri_schemes[n];
196       n++;
197     }
198
199   for (i = 0; i < G_N_ELEMENTS (winhttp_uri_schemes); i++)
200     {
201       retval[n] = winhttp_uri_schemes[i];
202       n++;
203     }
204
205   retval[n] = NULL;
206
207   return retval;
208 }
209
210 static GFile *
211 g_winhttp_vfs_parse_name (GVfs       *vfs,
212                           const char *parse_name)
213 {
214   GWinHttpVfs *winhttp_vfs = G_WINHTTP_VFS (vfs);
215
216   g_return_val_if_fail (G_IS_VFS (vfs), NULL);
217   g_return_val_if_fail (parse_name != NULL, NULL);
218
219   /* For plain file paths fallback to the wrapped GVfs */
220   if (g_path_is_absolute (parse_name))
221     return g_vfs_parse_name (winhttp_vfs->wrapped_vfs, parse_name);
222
223   /* Otherwise assume it is an URI, so pass on to
224    * g_winhttp_vfs_get_file_for_uri().
225    */
226   return g_winhttp_vfs_get_file_for_uri (vfs, parse_name);
227 }
228
229 static gboolean
230 g_winhttp_vfs_is_active (GVfs *vfs)
231 {
232   return TRUE;
233 }
234
235 static void
236 g_winhttp_vfs_class_init (GWinHttpVfsClass *class)
237 {
238   GObjectClass *object_class;
239   GVfsClass *vfs_class;
240
241   object_class = (GObjectClass *) class;
242
243   object_class->finalize = g_winhttp_vfs_finalize;
244
245   vfs_class = G_VFS_CLASS (class);
246
247   vfs_class->is_active = g_winhttp_vfs_is_active;
248   vfs_class->get_file_for_path = g_winhttp_vfs_get_file_for_path;
249   vfs_class->get_file_for_uri = g_winhttp_vfs_get_file_for_uri;
250   vfs_class->get_supported_uri_schemes = g_winhttp_vfs_get_supported_uri_schemes;
251   vfs_class->parse_name = g_winhttp_vfs_parse_name;
252
253   lookup_funcs ();
254   if (funcs_found)
255     class->funcs = &funcs;
256   else
257     class->funcs = NULL;
258 }
259
260 char *
261 _g_winhttp_error_message (DWORD error_code)
262 {
263   /* The FormatMessage() API that g_win32_error_message() uses doesn't
264    * seem to know about WinHttp errors, unfortunately.
265    */
266   if (error_code >= WINHTTP_ERROR_BASE && error_code < WINHTTP_ERROR_BASE + 200)
267     {
268       switch (error_code)
269         {
270           /* FIXME: Use meaningful error messages */
271 #define CASE(x) case ERROR_WINHTTP_##x: return g_strdup ("WinHttp error: " #x);
272           CASE (AUTO_PROXY_SERVICE_ERROR);
273           CASE (AUTODETECTION_FAILED);
274           CASE (BAD_AUTO_PROXY_SCRIPT);
275           CASE (CANNOT_CALL_AFTER_OPEN);
276           CASE (CANNOT_CALL_AFTER_SEND);
277           CASE (CANNOT_CALL_BEFORE_OPEN);
278           CASE (CANNOT_CALL_BEFORE_SEND);
279           CASE (CANNOT_CONNECT);
280           CASE (CHUNKED_ENCODING_HEADER_SIZE_OVERFLOW);
281           CASE (CLIENT_AUTH_CERT_NEEDED);
282           CASE (CONNECTION_ERROR);
283           CASE (HEADER_ALREADY_EXISTS);
284           CASE (HEADER_COUNT_EXCEEDED);
285           CASE (HEADER_NOT_FOUND);
286           CASE (HEADER_SIZE_OVERFLOW);
287           CASE (INCORRECT_HANDLE_STATE);
288           CASE (INCORRECT_HANDLE_TYPE);
289           CASE (INTERNAL_ERROR);
290           CASE (INVALID_OPTION);
291           CASE (INVALID_QUERY_REQUEST);
292           CASE (INVALID_SERVER_RESPONSE);
293           CASE (INVALID_URL);
294           CASE (LOGIN_FAILURE);
295           CASE (NAME_NOT_RESOLVED);
296           CASE (NOT_INITIALIZED);
297           CASE (OPERATION_CANCELLED);
298           CASE (OPTION_NOT_SETTABLE);
299           CASE (OUT_OF_HANDLES);
300           CASE (REDIRECT_FAILED);
301           CASE (RESEND_REQUEST);
302           CASE (RESPONSE_DRAIN_OVERFLOW);
303           CASE (SECURE_CERT_CN_INVALID);
304           CASE (SECURE_CERT_DATE_INVALID);
305           CASE (SECURE_CERT_REV_FAILED);
306           CASE (SECURE_CERT_REVOKED);
307           CASE (SECURE_CERT_WRONG_USAGE);
308           CASE (SECURE_CHANNEL_ERROR);
309           CASE (SECURE_FAILURE);
310           CASE (SECURE_INVALID_CA);
311           CASE (SECURE_INVALID_CERT);
312           CASE (SHUTDOWN);
313           CASE (TIMEOUT);
314           CASE (UNABLE_TO_DOWNLOAD_SCRIPT);
315           CASE (UNRECOGNIZED_SCHEME);
316           #undef CASE
317         default:
318           return g_strdup_printf ("WinHttp error %ld", error_code);
319         }
320     }
321   else
322     return g_win32_error_message (error_code);
323 }
324
325 void
326 _g_winhttp_set_error (GError     **error,
327                       DWORD        error_code,
328                       const char  *what)
329 {
330   char *emsg = _g_winhttp_error_message (error_code);
331
332   g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
333                "%s failed: %s", what, emsg);
334   g_free (emsg);
335 }
336
337 gboolean
338 _g_winhttp_response (GWinHttpVfs *vfs,
339                      HINTERNET    request,
340                      GError     **error,
341                      const char  *what)
342 {
343   wchar_t *status_code;
344   DWORD status_code_len;
345
346   if (!G_WINHTTP_VFS_GET_CLASS (vfs)->funcs->pWinHttpReceiveResponse (request, NULL))
347     {
348       _g_winhttp_set_error (error, GetLastError (), what);
349
350       return FALSE;
351     }
352
353   status_code_len = 0;
354   if (!G_WINHTTP_VFS_GET_CLASS (vfs)->funcs->pWinHttpQueryHeaders
355       (request,
356        WINHTTP_QUERY_STATUS_CODE,
357        NULL,
358        NULL,
359        &status_code_len,
360        NULL) &&
361       GetLastError () != ERROR_INSUFFICIENT_BUFFER)
362     {
363       _g_winhttp_set_error (error, GetLastError (), what);
364
365       return FALSE;
366     }
367
368   status_code = g_malloc (status_code_len);
369
370   if (!G_WINHTTP_VFS_GET_CLASS (vfs)->funcs->pWinHttpQueryHeaders
371       (request,
372        WINHTTP_QUERY_STATUS_CODE,
373        NULL,
374        status_code,
375        &status_code_len,
376        NULL))
377     {
378       _g_winhttp_set_error (error, GetLastError (), what);
379       g_free (status_code);
380
381       return FALSE;
382     }
383
384   if (status_code[0] != L'2')
385     {
386       wchar_t *status_text = NULL;
387       DWORD status_text_len;
388
389       if (!G_WINHTTP_VFS_GET_CLASS (vfs)->funcs->pWinHttpQueryHeaders
390           (request,
391            WINHTTP_QUERY_STATUS_TEXT,
392            NULL,
393            NULL,
394            &status_text_len,
395            NULL) &&
396           GetLastError () == ERROR_INSUFFICIENT_BUFFER)
397         {
398           status_text = g_malloc (status_text_len);
399
400           if (!G_WINHTTP_VFS_GET_CLASS (vfs)->funcs->pWinHttpQueryHeaders
401               (request,
402                WINHTTP_QUERY_STATUS_TEXT,
403                NULL,
404                status_text,
405                &status_text_len,
406                NULL))
407             {
408               g_free (status_text);
409               status_text = NULL;
410             }
411         }
412
413       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
414                    "%s failed: %S %S",
415                    what, status_code, status_text ? status_text : L"");
416       g_free (status_code);
417       g_free (status_text);
418
419       return FALSE;
420     }
421
422   g_free (status_code);
423
424   return TRUE;
425 }
426
427 gboolean
428 _g_winhttp_query_header (GWinHttpVfs *vfs,
429                          HINTERNET    request,
430                          const char  *request_description,
431                          DWORD        which_header,
432                          wchar_t    **header,
433                          GError     **error)
434 {
435   DWORD header_len = 0;
436
437   if (!G_WINHTTP_VFS_GET_CLASS (vfs)->funcs->pWinHttpQueryHeaders
438       (request,
439        which_header,
440        NULL,
441        NULL,
442        &header_len,
443        NULL) &&
444       GetLastError () != ERROR_INSUFFICIENT_BUFFER)
445     {
446       _g_winhttp_set_error (error, GetLastError (), request_description);
447
448       return FALSE;
449     }
450
451   *header = g_malloc (header_len);
452   if (!G_WINHTTP_VFS_GET_CLASS (vfs)->funcs->pWinHttpQueryHeaders
453       (request,
454        which_header,
455        NULL,
456        *header,
457        &header_len,
458        NULL))
459     {
460       _g_winhttp_set_error (error, GetLastError (), request_description);
461       g_free (*header);
462       *header = NULL;
463
464       return FALSE;
465     }
466
467   return TRUE;
468 }