2 * Copyright 2008 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
21 #include "wine/debug.h"
30 #include "winhttp_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
34 void set_last_error( DWORD error )
37 SetLastError( error );
40 DWORD get_last_error( void )
43 return GetLastError();
46 void send_callback( object_header_t *hdr, DWORD status, LPVOID info, DWORD buflen )
48 TRACE("%p, 0x%08x, %p, %u\n", hdr, status, info, buflen);
50 if (hdr->notify_mask & status) hdr->callback( hdr->handle, hdr->context, status, info, buflen );
53 /***********************************************************************
54 * WinHttpCheckPlatform (winhttp.@)
56 BOOL WINAPI WinHttpCheckPlatform( void )
62 /***********************************************************************
63 * session_destroy (internal)
65 static void session_destroy( object_header_t *hdr )
67 session_t *session = (session_t *)hdr;
68 struct list *item, *next;
71 TRACE("%p\n", session);
73 LIST_FOR_EACH_SAFE( item, next, &session->cookie_cache )
75 domain = LIST_ENTRY( item, domain_t, entry );
76 delete_domain( domain );
78 heap_free( session->agent );
79 heap_free( session->proxy_server );
80 heap_free( session->proxy_bypass );
81 heap_free( session->proxy_username );
82 heap_free( session->proxy_password );
86 static BOOL session_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
90 case WINHTTP_OPTION_REDIRECT_POLICY:
92 if (!buffer || *buflen < sizeof(DWORD))
94 *buflen = sizeof(DWORD);
95 set_last_error( ERROR_INSUFFICIENT_BUFFER );
99 *(DWORD *)buffer = hdr->redirect_policy;
100 *buflen = sizeof(DWORD);
104 FIXME("unimplemented option %u\n", option);
105 set_last_error( ERROR_INVALID_PARAMETER );
110 static BOOL session_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
114 case WINHTTP_OPTION_PROXY:
116 WINHTTP_PROXY_INFO *pi = buffer;
118 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
121 case WINHTTP_OPTION_REDIRECT_POLICY:
125 if (buflen != sizeof(policy))
127 set_last_error( ERROR_INSUFFICIENT_BUFFER );
131 policy = *(DWORD *)buffer;
132 TRACE("0x%x\n", policy);
133 hdr->redirect_policy = policy;
136 case WINHTTP_OPTION_DISABLE_FEATURE:
137 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
140 FIXME("unimplemented option %u\n", option);
141 set_last_error( ERROR_INVALID_PARAMETER );
146 static const object_vtbl_t session_vtbl =
149 session_query_option,
153 /***********************************************************************
154 * WinHttpOpen (winhttp.@)
156 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
159 HINTERNET handle = NULL;
161 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
163 if (!(session = heap_alloc_zero( sizeof(session_t) ))) return NULL;
165 session->hdr.type = WINHTTP_HANDLE_TYPE_SESSION;
166 session->hdr.vtbl = &session_vtbl;
167 session->hdr.flags = flags;
168 session->hdr.refs = 1;
169 session->access = access;
170 session->hdr.redirect_policy = WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP;
171 list_init( &session->cookie_cache );
173 if (agent && !(session->agent = strdupW( agent ))) goto end;
174 if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;
175 if (bypass && !(session->proxy_bypass = strdupW( bypass ))) goto end;
177 if (!(handle = alloc_handle( &session->hdr ))) goto end;
178 session->hdr.handle = handle;
181 release_object( &session->hdr );
182 TRACE("returning %p\n", handle);
186 /***********************************************************************
187 * connect_destroy (internal)
189 static void connect_destroy( object_header_t *hdr )
191 connect_t *connect = (connect_t *)hdr;
193 TRACE("%p\n", connect);
195 release_object( &connect->session->hdr );
197 heap_free( connect->hostname );
198 heap_free( connect->servername );
199 heap_free( connect->username );
200 heap_free( connect->password );
201 heap_free( connect );
204 static const object_vtbl_t connect_vtbl =
211 /***********************************************************************
212 * WinHttpConnect (winhttp.@)
214 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
218 HINTERNET hconnect = NULL;
220 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
224 set_last_error( ERROR_INVALID_PARAMETER );
227 if (!(session = (session_t *)grab_object( hsession )))
229 set_last_error( ERROR_INVALID_HANDLE );
232 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
234 release_object( &session->hdr );
235 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
238 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
240 release_object( &session->hdr );
243 connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
244 connect->hdr.vtbl = &connect_vtbl;
245 connect->hdr.refs = 1;
246 connect->hdr.flags = session->hdr.flags;
247 connect->hdr.callback = session->hdr.callback;
248 connect->hdr.notify_mask = session->hdr.notify_mask;
249 connect->hdr.context = session->hdr.context;
251 addref_object( &session->hdr );
252 connect->session = session;
253 list_add_head( &session->hdr.children, &connect->hdr.entry );
255 if (server && !(connect->hostname = strdupW( server ))) goto end;
256 connect->hostport = port ? port : (connect->hdr.flags & WINHTTP_FLAG_SECURE ? 443 : 80);
258 if (server && !(connect->servername = strdupW( server ))) goto end;
259 connect->serverport = port ? port : (connect->hdr.flags & WINHTTP_FLAG_SECURE ? 443 : 80);
261 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
262 connect->hdr.handle = hconnect;
264 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
267 release_object( &connect->hdr );
269 TRACE("returning %p\n", hconnect);
273 /***********************************************************************
274 * request_destroy (internal)
276 static void request_destroy( object_header_t *hdr )
278 request_t *request = (request_t *)hdr;
281 TRACE("%p\n", request);
283 release_object( &request->connect->hdr );
285 heap_free( request->verb );
286 heap_free( request->path );
287 heap_free( request->version );
288 heap_free( request->raw_headers );
289 heap_free( request->status_text );
290 for (i = 0; i < request->num_headers; i++)
292 heap_free( request->headers[i].field );
293 heap_free( request->headers[i].value );
295 heap_free( request->headers );
296 heap_free( request );
299 static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
303 case WINHTTP_OPTION_SECURITY_FLAGS:
307 if (!buffer || *buflen < sizeof(flags))
309 *buflen = sizeof(flags);
310 set_last_error( ERROR_INSUFFICIENT_BUFFER );
315 if (hdr->flags & WINHTTP_FLAG_SECURE) flags |= SECURITY_FLAG_SECURE;
316 *(DWORD *)buffer = flags;
317 *buflen = sizeof(flags);
320 case WINHTTP_OPTION_SERVER_CERT_CONTEXT:
322 const CERT_CONTEXT *cert;
323 request_t *request = (request_t *)hdr;
325 if (!buffer || *buflen < sizeof(cert))
327 *buflen = sizeof(cert);
328 set_last_error( ERROR_INSUFFICIENT_BUFFER );
332 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
333 *(CERT_CONTEXT **)buffer = (CERT_CONTEXT *)cert;
334 *buflen = sizeof(cert);
337 case WINHTTP_OPTION_SECURITY_KEY_BITNESS:
339 if (!buffer || *buflen < sizeof(DWORD))
341 *buflen = sizeof(DWORD);
342 set_last_error( ERROR_INSUFFICIENT_BUFFER );
346 *(DWORD *)buffer = 128; /* FIXME */
347 *buflen = sizeof(DWORD);
351 FIXME("unimplemented option %u\n", option);
352 set_last_error( ERROR_INVALID_PARAMETER );
357 static BOOL request_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
361 case WINHTTP_OPTION_PROXY:
363 WINHTTP_PROXY_INFO *pi = buffer;
365 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
368 case WINHTTP_OPTION_DISABLE_FEATURE:
372 if (buflen != sizeof(DWORD))
374 set_last_error( ERROR_INSUFFICIENT_BUFFER );
378 disable = *(DWORD *)buffer;
379 TRACE("0x%x\n", disable);
380 hdr->disable_flags |= disable;
383 case WINHTTP_OPTION_AUTOLOGON_POLICY:
387 if (buflen != sizeof(DWORD))
389 set_last_error( ERROR_INSUFFICIENT_BUFFER );
393 policy = *(DWORD *)buffer;
394 TRACE("0x%x\n", policy);
395 hdr->logon_policy = policy;
398 case WINHTTP_OPTION_REDIRECT_POLICY:
402 if (buflen != sizeof(DWORD))
404 set_last_error( ERROR_INSUFFICIENT_BUFFER );
408 policy = *(DWORD *)buffer;
409 TRACE("0x%x\n", policy);
410 hdr->redirect_policy = policy;
414 FIXME("unimplemented option %u\n", option);
415 set_last_error( ERROR_INVALID_PARAMETER );
420 static const object_vtbl_t request_vtbl =
423 request_query_option,
427 /***********************************************************************
428 * WinHttpOpenRequest (winhttp.@)
430 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
431 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
435 HINTERNET hrequest = NULL;
437 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
438 debugstr_w(version), debugstr_w(referrer), types, flags);
440 if (!(connect = (connect_t *)grab_object( hconnect )))
442 set_last_error( ERROR_INVALID_HANDLE );
445 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
447 release_object( &connect->hdr );
448 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
451 if (!(request = heap_alloc_zero( sizeof(request_t) )))
453 release_object( &connect->hdr );
456 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
457 request->hdr.vtbl = &request_vtbl;
458 request->hdr.refs = 1;
459 request->hdr.flags = flags;
460 request->hdr.callback = connect->hdr.callback;
461 request->hdr.notify_mask = connect->hdr.notify_mask;
462 request->hdr.context = connect->hdr.context;
464 addref_object( &connect->hdr );
465 request->connect = connect;
466 list_add_head( &connect->hdr.children, &request->hdr.entry );
468 if (!netconn_init( &request->netconn, request->hdr.flags & WINHTTP_FLAG_SECURE )) goto end;
470 if (!verb || !verb[0]) verb = getW;
471 if (!object || !object[0]) object = slashW;
472 if (!version || !version[0]) version = http1_1;
474 if (!(request->verb = strdupW( verb ))) goto end;
475 if (!(request->path = strdupW( object ))) goto end;
476 if (!(request->version = strdupW( version ))) goto end;
478 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
479 request->hdr.handle = hrequest;
481 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
484 release_object( &request->hdr );
486 TRACE("returning %p\n", hrequest);
490 /***********************************************************************
491 * WinHttpCloseHandle (winhttp.@)
493 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
495 object_header_t *hdr;
497 TRACE("%p\n", handle);
499 if (!(hdr = grab_object( handle )))
501 set_last_error( ERROR_INVALID_HANDLE );
504 release_object( hdr );
505 free_handle( handle );
509 static BOOL query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
515 set_last_error( ERROR_INVALID_PARAMETER );
521 case WINHTTP_OPTION_CONTEXT_VALUE:
523 if (!buffer || *buflen < sizeof(DWORD_PTR))
525 *buflen = sizeof(DWORD_PTR);
526 set_last_error( ERROR_INSUFFICIENT_BUFFER );
530 *(DWORD_PTR *)buffer = hdr->context;
531 *buflen = sizeof(DWORD_PTR);
535 if (hdr->vtbl->query_option) ret = hdr->vtbl->query_option( hdr, option, buffer, buflen );
538 FIXME("unimplemented option %u\n", option);
539 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
547 /***********************************************************************
548 * WinHttpQueryOption (winhttp.@)
550 BOOL WINAPI WinHttpQueryOption( HINTERNET handle, DWORD option, LPVOID buffer, LPDWORD buflen )
553 object_header_t *hdr;
555 TRACE("%p, %u, %p, %p\n", handle, option, buffer, buflen);
557 if (!(hdr = grab_object( handle )))
559 set_last_error( ERROR_INVALID_HANDLE );
563 ret = query_option( hdr, option, buffer, buflen );
565 release_object( hdr );
569 static BOOL set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
575 set_last_error( ERROR_INVALID_PARAMETER );
581 case WINHTTP_OPTION_CONTEXT_VALUE:
583 if (buflen != sizeof(DWORD_PTR))
585 set_last_error( ERROR_INSUFFICIENT_BUFFER );
589 hdr->context = *(DWORD_PTR *)buffer;
593 if (hdr->vtbl->set_option) ret = hdr->vtbl->set_option( hdr, option, buffer, buflen );
596 FIXME("unimplemented option %u\n", option);
597 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
605 /***********************************************************************
606 * WinHttpSetOption (winhttp.@)
608 BOOL WINAPI WinHttpSetOption( HINTERNET handle, DWORD option, LPVOID buffer, DWORD buflen )
611 object_header_t *hdr;
613 TRACE("%p, %u, %p, %u\n", handle, option, buffer, buflen);
615 if (!(hdr = grab_object( handle )))
617 set_last_error( ERROR_INVALID_HANDLE );
621 ret = set_option( hdr, option, buffer, buflen );
623 release_object( hdr );
627 /***********************************************************************
628 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
630 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
632 FIXME("0x%08x, %p\n", flags, url);
634 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
638 /***********************************************************************
639 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
641 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
645 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
646 info->lpszProxy = NULL;
647 info->lpszProxyBypass = NULL;
652 /***********************************************************************
653 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
655 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
657 TRACE("%p\n", config);
661 set_last_error( ERROR_INVALID_PARAMETER );
665 /* FIXME: read from HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings */
667 FIXME("returning no proxy used\n");
668 config->fAutoDetect = FALSE;
669 config->lpszAutoConfigUrl = NULL;
670 config->lpszProxy = NULL;
671 config->lpszProxyBypass = NULL;
676 /***********************************************************************
677 * WinHttpGetProxyForUrl (winhttp.@)
679 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
680 WINHTTP_PROXY_INFO *info )
682 FIXME("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
684 set_last_error( ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR );
688 /***********************************************************************
689 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
691 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
693 FIXME("%p [%u, %s, %s]\n", info, info->dwAccessType, debugstr_w(info->lpszProxy),
694 debugstr_w(info->lpszProxyBypass));
698 /***********************************************************************
699 * WinHttpSetStatusCallback (winhttp.@)
701 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
702 DWORD flags, DWORD_PTR reserved )
704 object_header_t *hdr;
705 WINHTTP_STATUS_CALLBACK ret;
707 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
709 if (!(hdr = grab_object( handle )))
711 set_last_error( ERROR_INVALID_HANDLE );
712 return WINHTTP_INVALID_STATUS_CALLBACK;
715 hdr->callback = callback;
716 hdr->notify_mask = flags;
718 release_object( hdr );
722 /***********************************************************************
723 * WinHttpSetTimeouts (winhttp.@)
725 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
727 FIXME("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
731 static const WCHAR wkday[7][4] =
732 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
733 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
734 static const WCHAR month[12][4] =
735 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
736 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
737 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
739 /***********************************************************************
740 * WinHttpTimeFromSystemTime (WININET.@)
742 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
744 static const WCHAR format[] =
745 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
746 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
748 TRACE("%p, %p\n", time, string);
750 if (!time || !string) return FALSE;
752 sprintfW( string, format,
753 wkday[time->wDayOfWeek],
755 month[time->wMonth - 1],
764 /***********************************************************************
765 * WinHttpTimeToSystemTime (WININET.@)
767 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
770 const WCHAR *s = string;
773 TRACE("%s, %p\n", debugstr_w(string), time);
775 if (!string || !time) return FALSE;
777 /* Windows does this too */
778 GetSystemTime( time );
780 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
781 * a SYSTEMTIME structure.
784 while (*s && !isalphaW( *s )) s++;
785 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
786 time->wDayOfWeek = 7;
788 for (i = 0; i < 7; i++)
790 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
791 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
792 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
794 time->wDayOfWeek = i;
799 if (time->wDayOfWeek > 6) return TRUE;
800 while (*s && !isdigitW( *s )) s++;
801 time->wDay = strtolW( s, &end, 10 );
804 while (*s && !isalphaW( *s )) s++;
805 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
808 for (i = 0; i < 12; i++)
810 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
811 toupperW( month[i][1]) == toupperW( s[1] ) &&
812 toupperW( month[i][2]) == toupperW( s[2] ) )
814 time->wMonth = i + 1;
818 if (time->wMonth == 0) return TRUE;
820 while (*s && !isdigitW( *s )) s++;
821 if (*s == '\0') return TRUE;
822 time->wYear = strtolW( s, &end, 10 );
825 while (*s && !isdigitW( *s )) s++;
826 if (*s == '\0') return TRUE;
827 time->wHour = strtolW( s, &end, 10 );
830 while (*s && !isdigitW( *s )) s++;
831 if (*s == '\0') return TRUE;
832 time->wMinute = strtolW( s, &end, 10 );
835 while (*s && !isdigitW( *s )) s++;
836 if (*s == '\0') return TRUE;
837 time->wSecond = strtolW( s, &end, 10 );
840 time->wMilliseconds = 0;