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->callback && (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;
258 if (server && !(connect->servername = strdupW( server ))) goto end;
259 connect->serverport = port;
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 (!(request->verb = strdupW( verb ))) goto end;
478 len = strlenW( object ) + 1;
479 if (object[0] != '/') len++;
480 if (!(p = path = heap_alloc( len * sizeof(WCHAR) ))) goto end;
482 if (object[0] != '/') *p++ = '/';
483 strcpyW( p, object );
484 request->path = path;
486 else if (!(request->path = strdupW( slashW ))) goto end;
488 if (!version || !version[0]) version = http1_1;
489 if (!(request->version = strdupW( version ))) goto end;
491 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
492 request->hdr.handle = hrequest;
494 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
497 release_object( &request->hdr );
499 TRACE("returning %p\n", hrequest);
503 /***********************************************************************
504 * WinHttpCloseHandle (winhttp.@)
506 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
508 object_header_t *hdr;
510 TRACE("%p\n", handle);
512 if (!(hdr = grab_object( handle )))
514 set_last_error( ERROR_INVALID_HANDLE );
517 release_object( hdr );
518 free_handle( handle );
522 static BOOL query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
528 set_last_error( ERROR_INVALID_PARAMETER );
534 case WINHTTP_OPTION_CONTEXT_VALUE:
536 if (!buffer || *buflen < sizeof(DWORD_PTR))
538 *buflen = sizeof(DWORD_PTR);
539 set_last_error( ERROR_INSUFFICIENT_BUFFER );
543 *(DWORD_PTR *)buffer = hdr->context;
544 *buflen = sizeof(DWORD_PTR);
548 if (hdr->vtbl->query_option) ret = hdr->vtbl->query_option( hdr, option, buffer, buflen );
551 FIXME("unimplemented option %u\n", option);
552 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
560 /***********************************************************************
561 * WinHttpQueryOption (winhttp.@)
563 BOOL WINAPI WinHttpQueryOption( HINTERNET handle, DWORD option, LPVOID buffer, LPDWORD buflen )
566 object_header_t *hdr;
568 TRACE("%p, %u, %p, %p\n", handle, option, buffer, buflen);
570 if (!(hdr = grab_object( handle )))
572 set_last_error( ERROR_INVALID_HANDLE );
576 ret = query_option( hdr, option, buffer, buflen );
578 release_object( hdr );
582 static BOOL set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
588 set_last_error( ERROR_INVALID_PARAMETER );
594 case WINHTTP_OPTION_CONTEXT_VALUE:
596 if (buflen != sizeof(DWORD_PTR))
598 set_last_error( ERROR_INSUFFICIENT_BUFFER );
602 hdr->context = *(DWORD_PTR *)buffer;
606 if (hdr->vtbl->set_option) ret = hdr->vtbl->set_option( hdr, option, buffer, buflen );
609 FIXME("unimplemented option %u\n", option);
610 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
618 /***********************************************************************
619 * WinHttpSetOption (winhttp.@)
621 BOOL WINAPI WinHttpSetOption( HINTERNET handle, DWORD option, LPVOID buffer, DWORD buflen )
624 object_header_t *hdr;
626 TRACE("%p, %u, %p, %u\n", handle, option, buffer, buflen);
628 if (!(hdr = grab_object( handle )))
630 set_last_error( ERROR_INVALID_HANDLE );
634 ret = set_option( hdr, option, buffer, buflen );
636 release_object( hdr );
640 /***********************************************************************
641 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
643 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
645 FIXME("0x%08x, %p\n", flags, url);
647 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
651 /***********************************************************************
652 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
654 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
658 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
659 info->lpszProxy = NULL;
660 info->lpszProxyBypass = NULL;
665 /***********************************************************************
666 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
668 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
670 TRACE("%p\n", config);
674 set_last_error( ERROR_INVALID_PARAMETER );
678 /* FIXME: read from HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings */
680 FIXME("returning no proxy used\n");
681 config->fAutoDetect = FALSE;
682 config->lpszAutoConfigUrl = NULL;
683 config->lpszProxy = NULL;
684 config->lpszProxyBypass = NULL;
689 /***********************************************************************
690 * WinHttpGetProxyForUrl (winhttp.@)
692 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
693 WINHTTP_PROXY_INFO *info )
695 FIXME("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
697 set_last_error( ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR );
701 /***********************************************************************
702 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
704 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
706 FIXME("%p [%u, %s, %s]\n", info, info->dwAccessType, debugstr_w(info->lpszProxy),
707 debugstr_w(info->lpszProxyBypass));
711 /***********************************************************************
712 * WinHttpSetStatusCallback (winhttp.@)
714 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
715 DWORD flags, DWORD_PTR reserved )
717 object_header_t *hdr;
718 WINHTTP_STATUS_CALLBACK ret;
720 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
722 if (!(hdr = grab_object( handle )))
724 set_last_error( ERROR_INVALID_HANDLE );
725 return WINHTTP_INVALID_STATUS_CALLBACK;
728 hdr->callback = callback;
729 hdr->notify_mask = flags;
731 release_object( hdr );
735 /***********************************************************************
736 * WinHttpSetTimeouts (winhttp.@)
738 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
740 FIXME("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
744 static const WCHAR wkday[7][4] =
745 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
746 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
747 static const WCHAR month[12][4] =
748 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
749 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
750 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
752 /***********************************************************************
753 * WinHttpTimeFromSystemTime (WININET.@)
755 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
757 static const WCHAR format[] =
758 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
759 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
761 TRACE("%p, %p\n", time, string);
763 if (!time || !string) return FALSE;
765 sprintfW( string, format,
766 wkday[time->wDayOfWeek],
768 month[time->wMonth - 1],
777 /***********************************************************************
778 * WinHttpTimeToSystemTime (WININET.@)
780 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
783 const WCHAR *s = string;
786 TRACE("%s, %p\n", debugstr_w(string), time);
788 if (!string || !time) return FALSE;
790 /* Windows does this too */
791 GetSystemTime( time );
793 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
794 * a SYSTEMTIME structure.
797 while (*s && !isalphaW( *s )) s++;
798 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
799 time->wDayOfWeek = 7;
801 for (i = 0; i < 7; i++)
803 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
804 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
805 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
807 time->wDayOfWeek = i;
812 if (time->wDayOfWeek > 6) return TRUE;
813 while (*s && !isdigitW( *s )) s++;
814 time->wDay = strtolW( s, &end, 10 );
817 while (*s && !isalphaW( *s )) s++;
818 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
821 for (i = 0; i < 12; i++)
823 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
824 toupperW( month[i][1]) == toupperW( s[1] ) &&
825 toupperW( month[i][2]) == toupperW( s[2] ) )
827 time->wMonth = i + 1;
831 if (time->wMonth == 0) return TRUE;
833 while (*s && !isdigitW( *s )) s++;
834 if (*s == '\0') return TRUE;
835 time->wYear = strtolW( s, &end, 10 );
838 while (*s && !isdigitW( *s )) s++;
839 if (*s == '\0') return TRUE;
840 time->wHour = strtolW( s, &end, 10 );
843 while (*s && !isdigitW( *s )) s++;
844 if (*s == '\0') return TRUE;
845 time->wMinute = strtolW( s, &end, 10 );
848 while (*s && !isdigitW( *s )) s++;
849 if (*s == '\0') return TRUE;
850 time->wSecond = strtolW( s, &end, 10 );
853 time->wMilliseconds = 0;