quartz: Free two assert calls from having side effects.
[wine/testsucceed.git] / dlls / winhttp / session.c
blobb7cc31b1127976e5c552a36d6510c86890ed5dae
1 /*
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
19 #include "config.h"
20 #include "wine/port.h"
21 #include "wine/debug.h"
23 #include <stdarg.h>
24 #include <stdlib.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winhttp.h"
29 #include "wincrypt.h"
30 #include "winreg.h"
32 #include "winhttp_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
36 #define DEFAULT_RESOLVE_TIMEOUT 0
37 #define DEFAULT_CONNECT_TIMEOUT 20000
38 #define DEFAULT_SEND_TIMEOUT 30000
39 #define DEFAULT_RECEIVE_TIMEOUT 30000
41 void set_last_error( DWORD error )
43 /* FIXME */
44 SetLastError( error );
47 DWORD get_last_error( void )
49 /* FIXME */
50 return GetLastError();
53 void send_callback( object_header_t *hdr, DWORD status, LPVOID info, DWORD buflen )
55 TRACE("%p, 0x%08x, %p, %u\n", hdr, status, info, buflen);
57 if (hdr->callback && (hdr->notify_mask & status)) hdr->callback( hdr->handle, hdr->context, status, info, buflen );
60 /***********************************************************************
61 * WinHttpCheckPlatform (winhttp.@)
63 BOOL WINAPI WinHttpCheckPlatform( void )
65 TRACE("\n");
66 return TRUE;
69 /***********************************************************************
70 * session_destroy (internal)
72 static void session_destroy( object_header_t *hdr )
74 session_t *session = (session_t *)hdr;
75 struct list *item, *next;
76 domain_t *domain;
78 TRACE("%p\n", session);
80 LIST_FOR_EACH_SAFE( item, next, &session->cookie_cache )
82 domain = LIST_ENTRY( item, domain_t, entry );
83 delete_domain( domain );
85 heap_free( session->agent );
86 heap_free( session->proxy_server );
87 heap_free( session->proxy_bypass );
88 heap_free( session->proxy_username );
89 heap_free( session->proxy_password );
90 heap_free( session );
93 static BOOL session_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
95 session_t *session = (session_t *)hdr;
97 switch (option)
99 case WINHTTP_OPTION_REDIRECT_POLICY:
101 if (!buffer || *buflen < sizeof(DWORD))
103 *buflen = sizeof(DWORD);
104 set_last_error( ERROR_INSUFFICIENT_BUFFER );
105 return FALSE;
108 *(DWORD *)buffer = hdr->redirect_policy;
109 *buflen = sizeof(DWORD);
110 return TRUE;
112 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
113 *(DWORD *)buffer = session->resolve_timeout;
114 *buflen = sizeof(DWORD);
115 return TRUE;
116 case WINHTTP_OPTION_CONNECT_TIMEOUT:
117 *(DWORD *)buffer = session->connect_timeout;
118 *buflen = sizeof(DWORD);
119 return TRUE;
120 case WINHTTP_OPTION_SEND_TIMEOUT:
121 *(DWORD *)buffer = session->send_timeout;
122 *buflen = sizeof(DWORD);
123 return TRUE;
124 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
125 *(DWORD *)buffer = session->recv_timeout;
126 *buflen = sizeof(DWORD);
127 return TRUE;
128 default:
129 FIXME("unimplemented option %u\n", option);
130 set_last_error( ERROR_INVALID_PARAMETER );
131 return FALSE;
135 static BOOL session_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
137 session_t *session = (session_t *)hdr;
139 switch (option)
141 case WINHTTP_OPTION_PROXY:
143 WINHTTP_PROXY_INFO *pi = buffer;
145 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
146 return TRUE;
148 case WINHTTP_OPTION_REDIRECT_POLICY:
150 DWORD policy;
152 if (buflen != sizeof(policy))
154 set_last_error( ERROR_INSUFFICIENT_BUFFER );
155 return FALSE;
158 policy = *(DWORD *)buffer;
159 TRACE("0x%x\n", policy);
160 hdr->redirect_policy = policy;
161 return TRUE;
163 case WINHTTP_OPTION_DISABLE_FEATURE:
164 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
165 return FALSE;
166 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
167 session->resolve_timeout = *(DWORD *)buffer;
168 return TRUE;
169 case WINHTTP_OPTION_CONNECT_TIMEOUT:
170 session->connect_timeout = *(DWORD *)buffer;
171 return TRUE;
172 case WINHTTP_OPTION_SEND_TIMEOUT:
173 session->send_timeout = *(DWORD *)buffer;
174 return TRUE;
175 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
176 session->recv_timeout = *(DWORD *)buffer;
177 return TRUE;
178 default:
179 FIXME("unimplemented option %u\n", option);
180 set_last_error( ERROR_INVALID_PARAMETER );
181 return FALSE;
185 static const object_vtbl_t session_vtbl =
187 session_destroy,
188 session_query_option,
189 session_set_option
192 /***********************************************************************
193 * WinHttpOpen (winhttp.@)
195 HINTERNET WINAPI WinHttpOpen( LPCWSTR agent, DWORD access, LPCWSTR proxy, LPCWSTR bypass, DWORD flags )
197 session_t *session;
198 HINTERNET handle = NULL;
200 TRACE("%s, %u, %s, %s, 0x%08x\n", debugstr_w(agent), access, debugstr_w(proxy), debugstr_w(bypass), flags);
202 if (!(session = heap_alloc_zero( sizeof(session_t) ))) return NULL;
204 session->hdr.type = WINHTTP_HANDLE_TYPE_SESSION;
205 session->hdr.vtbl = &session_vtbl;
206 session->hdr.flags = flags;
207 session->hdr.refs = 1;
208 session->hdr.redirect_policy = WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP;
209 list_init( &session->hdr.children );
210 session->resolve_timeout = DEFAULT_RESOLVE_TIMEOUT;
211 session->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
212 session->send_timeout = DEFAULT_SEND_TIMEOUT;
213 session->recv_timeout = DEFAULT_RECEIVE_TIMEOUT;
214 list_init( &session->cookie_cache );
216 if (agent && !(session->agent = strdupW( agent ))) goto end;
217 if (access == WINHTTP_ACCESS_TYPE_DEFAULT_PROXY)
219 WINHTTP_PROXY_INFO info;
221 WinHttpGetDefaultProxyConfiguration( &info );
222 session->access = info.dwAccessType;
223 if (info.lpszProxy && !(session->proxy_server = strdupW( info.lpszProxy )))
225 GlobalFree( (LPWSTR)info.lpszProxy );
226 GlobalFree( (LPWSTR)info.lpszProxyBypass );
227 goto end;
229 if (info.lpszProxyBypass && !(session->proxy_bypass = strdupW( info.lpszProxyBypass )))
231 GlobalFree( (LPWSTR)info.lpszProxy );
232 GlobalFree( (LPWSTR)info.lpszProxyBypass );
233 goto end;
236 else if (access == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
238 session->access = access;
239 if (proxy && !(session->proxy_server = strdupW( proxy ))) goto end;
240 if (bypass && !(session->proxy_bypass = strdupW( bypass ))) goto end;
243 if (!(handle = alloc_handle( &session->hdr ))) goto end;
244 session->hdr.handle = handle;
246 end:
247 release_object( &session->hdr );
248 TRACE("returning %p\n", handle);
249 return handle;
252 /***********************************************************************
253 * connect_destroy (internal)
255 static void connect_destroy( object_header_t *hdr )
257 connect_t *connect = (connect_t *)hdr;
259 TRACE("%p\n", connect);
261 release_object( &connect->session->hdr );
263 heap_free( connect->hostname );
264 heap_free( connect->servername );
265 heap_free( connect->username );
266 heap_free( connect->password );
267 heap_free( connect );
270 static BOOL connect_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
272 connect_t *connect = (connect_t *)hdr;
274 switch (option)
276 case WINHTTP_OPTION_PARENT_HANDLE:
278 if (!buffer || *buflen < sizeof(HINTERNET))
280 *buflen = sizeof(HINTERNET);
281 set_last_error( ERROR_INSUFFICIENT_BUFFER );
282 return FALSE;
285 *(HINTERNET *)buffer = ((object_header_t *)connect->session)->handle;
286 *buflen = sizeof(HINTERNET);
287 return TRUE;
289 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
290 *(DWORD *)buffer = connect->session->resolve_timeout;
291 *buflen = sizeof(DWORD);
292 return TRUE;
293 case WINHTTP_OPTION_CONNECT_TIMEOUT:
294 *(DWORD *)buffer = connect->session->connect_timeout;
295 *buflen = sizeof(DWORD);
296 return TRUE;
297 case WINHTTP_OPTION_SEND_TIMEOUT:
298 *(DWORD *)buffer = connect->session->send_timeout;
299 *buflen = sizeof(DWORD);
300 return TRUE;
301 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
302 *(DWORD *)buffer = connect->session->recv_timeout;
303 *buflen = sizeof(DWORD);
304 return TRUE;
305 default:
306 FIXME("unimplemented option %u\n", option);
307 set_last_error( ERROR_INVALID_PARAMETER );
308 return FALSE;
312 static const object_vtbl_t connect_vtbl =
314 connect_destroy,
315 connect_query_option,
316 NULL
319 static BOOL domain_matches(LPCWSTR server, LPCWSTR domain)
321 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
322 BOOL ret = FALSE;
324 if (!strcmpiW( domain, localW ) && !strchrW( server, '.' ))
325 ret = TRUE;
326 else if (*domain == '*')
328 if (domain[1] == '.')
330 LPCWSTR dot;
332 /* For a hostname to match a wildcard, the last domain must match
333 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
334 * hostname is www.foo.a.b, it matches, but a.b does not.
336 dot = strchrW( server, '.' );
337 if (dot)
339 int len = strlenW( dot + 1 );
341 if (len > strlenW( domain + 2 ))
343 LPCWSTR ptr;
345 /* The server's domain is longer than the wildcard, so it
346 * could be a subdomain. Compare the last portion of the
347 * server's domain.
349 ptr = dot + len + 1 - strlenW( domain + 2 );
350 if (!strcmpiW( ptr, domain + 2 ))
352 /* This is only a match if the preceding character is
353 * a '.', i.e. that it is a matching domain. E.g.
354 * if domain is '*.b.c' and server is 'www.ab.c' they
355 * do not match.
357 ret = *(ptr - 1) == '.';
360 else
361 ret = !strcmpiW( dot + 1, domain + 2 );
365 else
366 ret = !strcmpiW( server, domain );
367 return ret;
370 /* Matches INTERNET_MAX_HOST_NAME_LENGTH in wininet.h, also RFC 1035 */
371 #define MAX_HOST_NAME_LENGTH 256
373 static BOOL should_bypass_proxy(session_t *session, LPCWSTR server)
375 LPCWSTR ptr;
376 BOOL ret = FALSE;
378 if (!session->proxy_bypass) return FALSE;
379 ptr = session->proxy_bypass;
380 do {
381 LPCWSTR tmp = ptr;
383 ptr = strchrW( ptr, ';' );
384 if (!ptr)
385 ptr = strchrW( tmp, ' ' );
386 if (ptr)
388 if (ptr - tmp < MAX_HOST_NAME_LENGTH)
390 WCHAR domain[MAX_HOST_NAME_LENGTH];
392 memcpy( domain, tmp, (ptr - tmp) * sizeof(WCHAR) );
393 domain[ptr - tmp] = 0;
394 ret = domain_matches( server, domain );
396 ptr += 1;
398 else if (*tmp)
399 ret = domain_matches( server, tmp );
400 } while (ptr && !ret);
401 return ret;
404 BOOL set_server_for_hostname( connect_t *connect, LPCWSTR server, INTERNET_PORT port )
406 session_t *session = connect->session;
407 BOOL ret = TRUE;
409 if (session->proxy_server && !should_bypass_proxy(session, server))
411 LPCWSTR colon;
413 if ((colon = strchrW( session->proxy_server, ':' )))
415 if (!connect->servername || strncmpiW( connect->servername,
416 session->proxy_server, colon - session->proxy_server - 1 ))
418 heap_free( connect->servername );
419 if (!(connect->servername = heap_alloc(
420 (colon - session->proxy_server + 1) * sizeof(WCHAR) )))
422 ret = FALSE;
423 goto end;
425 memcpy( connect->servername, session->proxy_server,
426 (colon - session->proxy_server) * sizeof(WCHAR) );
427 connect->servername[colon - session->proxy_server] = 0;
428 if (*(colon + 1))
429 connect->serverport = atoiW( colon + 1 );
430 else
431 connect->serverport = INTERNET_DEFAULT_PORT;
434 else
436 if (!connect->servername || strcmpiW( connect->servername,
437 session->proxy_server ))
439 heap_free( connect->servername );
440 if (!(connect->servername = strdupW( session->proxy_server )))
442 ret = FALSE;
443 goto end;
445 connect->serverport = INTERNET_DEFAULT_PORT;
449 else if (server)
451 heap_free( connect->servername );
452 if (!(connect->servername = strdupW( server )))
454 ret = FALSE;
455 goto end;
457 connect->serverport = port;
459 end:
460 return ret;
463 /***********************************************************************
464 * WinHttpConnect (winhttp.@)
466 HINTERNET WINAPI WinHttpConnect( HINTERNET hsession, LPCWSTR server, INTERNET_PORT port, DWORD reserved )
468 connect_t *connect;
469 session_t *session;
470 HINTERNET hconnect = NULL;
472 TRACE("%p, %s, %u, %x\n", hsession, debugstr_w(server), port, reserved);
474 if (!server)
476 set_last_error( ERROR_INVALID_PARAMETER );
477 return NULL;
479 if (!(session = (session_t *)grab_object( hsession )))
481 set_last_error( ERROR_INVALID_HANDLE );
482 return NULL;
484 if (session->hdr.type != WINHTTP_HANDLE_TYPE_SESSION)
486 release_object( &session->hdr );
487 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
488 return NULL;
490 if (!(connect = heap_alloc_zero( sizeof(connect_t) )))
492 release_object( &session->hdr );
493 return NULL;
495 connect->hdr.type = WINHTTP_HANDLE_TYPE_CONNECT;
496 connect->hdr.vtbl = &connect_vtbl;
497 connect->hdr.refs = 1;
498 connect->hdr.flags = session->hdr.flags;
499 connect->hdr.callback = session->hdr.callback;
500 connect->hdr.notify_mask = session->hdr.notify_mask;
501 connect->hdr.context = session->hdr.context;
502 list_init( &connect->hdr.children );
504 addref_object( &session->hdr );
505 connect->session = session;
506 list_add_head( &session->hdr.children, &connect->hdr.entry );
508 if (!(connect->hostname = strdupW( server ))) goto end;
509 connect->hostport = port;
510 if (!set_server_for_hostname( connect, server, port )) goto end;
512 if (!(hconnect = alloc_handle( &connect->hdr ))) goto end;
513 connect->hdr.handle = hconnect;
515 send_callback( &session->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hconnect, sizeof(hconnect) );
517 end:
518 release_object( &connect->hdr );
519 release_object( &session->hdr );
520 TRACE("returning %p\n", hconnect);
521 return hconnect;
524 /***********************************************************************
525 * request_destroy (internal)
527 static void request_destroy( object_header_t *hdr )
529 request_t *request = (request_t *)hdr;
530 unsigned int i;
532 TRACE("%p\n", request);
534 release_object( &request->connect->hdr );
536 heap_free( request->verb );
537 heap_free( request->path );
538 heap_free( request->version );
539 heap_free( request->raw_headers );
540 heap_free( request->status_text );
541 for (i = 0; i < request->num_headers; i++)
543 heap_free( request->headers[i].field );
544 heap_free( request->headers[i].value );
546 heap_free( request->headers );
547 for (i = 0; i < request->num_accept_types; i++) heap_free( request->accept_types[i] );
548 heap_free( request->accept_types );
549 heap_free( request );
552 static void str_to_buffer( WCHAR *buffer, const WCHAR *str, LPDWORD buflen )
554 int len = 0;
555 if (str) len = strlenW( str );
556 if (buffer && *buflen > len)
558 memcpy( buffer, str, len * sizeof(WCHAR) );
559 buffer[len] = 0;
561 *buflen = len * sizeof(WCHAR);
564 static WCHAR *blob_to_str( DWORD encoding, CERT_NAME_BLOB *blob )
566 WCHAR *ret;
567 DWORD size, format = CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG;
569 size = CertNameToStrW( encoding, blob, format, NULL, 0 );
570 if ((ret = LocalAlloc( 0, size * sizeof(WCHAR) )))
571 CertNameToStrW( encoding, blob, format, ret, size );
573 return ret;
576 static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
578 request_t *request = (request_t *)hdr;
580 switch (option)
582 case WINHTTP_OPTION_SECURITY_FLAGS:
584 DWORD flags;
585 int bits;
587 if (!buffer || *buflen < sizeof(flags))
589 *buflen = sizeof(flags);
590 set_last_error( ERROR_INSUFFICIENT_BUFFER );
591 return FALSE;
594 flags = 0;
595 if (hdr->flags & WINHTTP_FLAG_SECURE) flags |= SECURITY_FLAG_SECURE;
596 flags |= request->netconn.security_flags;
597 bits = netconn_get_cipher_strength( &request->netconn );
598 if (bits >= 128)
599 flags |= SECURITY_FLAG_STRENGTH_STRONG;
600 else if (bits >= 56)
601 flags |= SECURITY_FLAG_STRENGTH_MEDIUM;
602 else
603 flags |= SECURITY_FLAG_STRENGTH_WEAK;
604 *(DWORD *)buffer = flags;
605 *buflen = sizeof(flags);
606 return TRUE;
608 case WINHTTP_OPTION_SERVER_CERT_CONTEXT:
610 const CERT_CONTEXT *cert;
612 if (!buffer || *buflen < sizeof(cert))
614 *buflen = sizeof(cert);
615 set_last_error( ERROR_INSUFFICIENT_BUFFER );
616 return FALSE;
619 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
620 *(CERT_CONTEXT **)buffer = (CERT_CONTEXT *)cert;
621 *buflen = sizeof(cert);
622 return TRUE;
624 case WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT:
626 const CERT_CONTEXT *cert;
627 const CRYPT_OID_INFO *oidInfo;
628 WINHTTP_CERTIFICATE_INFO *ci = buffer;
630 FIXME("partial stub\n");
632 if (!buffer || *buflen < sizeof(*ci))
634 *buflen = sizeof(*ci);
635 set_last_error( ERROR_INSUFFICIENT_BUFFER );
636 return FALSE;
638 if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
640 ci->ftExpiry = cert->pCertInfo->NotAfter;
641 ci->ftStart = cert->pCertInfo->NotBefore;
642 ci->lpszSubjectInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Subject );
643 ci->lpszIssuerInfo = blob_to_str( cert->dwCertEncodingType, &cert->pCertInfo->Issuer );
644 ci->lpszProtocolName = NULL;
645 oidInfo = CryptFindOIDInfo( CRYPT_OID_INFO_OID_KEY,
646 cert->pCertInfo->SignatureAlgorithm.pszObjId,
647 0 );
648 if (oidInfo)
649 ci->lpszSignatureAlgName = (LPWSTR)oidInfo->pwszName;
650 else
651 ci->lpszSignatureAlgName = NULL;
652 ci->lpszEncryptionAlgName = NULL;
653 ci->dwKeySize = netconn_get_cipher_strength( &request->netconn );
655 CertFreeCertificateContext( cert );
656 *buflen = sizeof(*ci);
657 return TRUE;
659 case WINHTTP_OPTION_SECURITY_KEY_BITNESS:
661 if (!buffer || *buflen < sizeof(DWORD))
663 *buflen = sizeof(DWORD);
664 set_last_error( ERROR_INSUFFICIENT_BUFFER );
665 return FALSE;
668 *(DWORD *)buffer = netconn_get_cipher_strength( &request->netconn );
669 *buflen = sizeof(DWORD);
670 return TRUE;
672 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
673 *(DWORD *)buffer = request->resolve_timeout;
674 *buflen = sizeof(DWORD);
675 return TRUE;
676 case WINHTTP_OPTION_CONNECT_TIMEOUT:
677 *(DWORD *)buffer = request->connect_timeout;
678 *buflen = sizeof(DWORD);
679 return TRUE;
680 case WINHTTP_OPTION_SEND_TIMEOUT:
681 *(DWORD *)buffer = request->send_timeout;
682 *buflen = sizeof(DWORD);
683 return TRUE;
684 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
685 *(DWORD *)buffer = request->recv_timeout;
686 *buflen = sizeof(DWORD);
687 return TRUE;
689 case WINHTTP_OPTION_USERNAME:
690 str_to_buffer( buffer, request->connect->username, buflen );
691 return TRUE;
693 case WINHTTP_OPTION_PASSWORD:
694 str_to_buffer( buffer, request->connect->password, buflen );
695 return TRUE;
697 case WINHTTP_OPTION_PROXY_USERNAME:
698 str_to_buffer( buffer, request->connect->session->proxy_username, buflen );
699 return TRUE;
701 case WINHTTP_OPTION_PROXY_PASSWORD:
702 str_to_buffer( buffer, request->connect->session->proxy_password, buflen );
703 return TRUE;
705 default:
706 FIXME("unimplemented option %u\n", option);
707 set_last_error( ERROR_INVALID_PARAMETER );
708 return FALSE;
712 static WCHAR *buffer_to_str( WCHAR *buffer, DWORD buflen )
714 WCHAR *ret;
715 if ((ret = heap_alloc( (buflen + 1) * sizeof(WCHAR))))
717 memcpy( ret, buffer, buflen * sizeof(WCHAR) );
718 ret[buflen] = 0;
719 return ret;
721 set_last_error( ERROR_OUTOFMEMORY );
722 return NULL;
725 static BOOL request_set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
727 request_t *request = (request_t *)hdr;
729 switch (option)
731 case WINHTTP_OPTION_PROXY:
733 WINHTTP_PROXY_INFO *pi = buffer;
735 FIXME("%u %s %s\n", pi->dwAccessType, debugstr_w(pi->lpszProxy), debugstr_w(pi->lpszProxyBypass));
736 return TRUE;
738 case WINHTTP_OPTION_DISABLE_FEATURE:
740 DWORD disable;
742 if (buflen != sizeof(DWORD))
744 set_last_error( ERROR_INSUFFICIENT_BUFFER );
745 return FALSE;
748 disable = *(DWORD *)buffer;
749 TRACE("0x%x\n", disable);
750 hdr->disable_flags |= disable;
751 return TRUE;
753 case WINHTTP_OPTION_AUTOLOGON_POLICY:
755 DWORD policy;
757 if (buflen != sizeof(DWORD))
759 set_last_error( ERROR_INSUFFICIENT_BUFFER );
760 return FALSE;
763 policy = *(DWORD *)buffer;
764 TRACE("0x%x\n", policy);
765 hdr->logon_policy = policy;
766 return TRUE;
768 case WINHTTP_OPTION_REDIRECT_POLICY:
770 DWORD policy;
772 if (buflen != sizeof(DWORD))
774 set_last_error( ERROR_INSUFFICIENT_BUFFER );
775 return FALSE;
778 policy = *(DWORD *)buffer;
779 TRACE("0x%x\n", policy);
780 hdr->redirect_policy = policy;
781 return TRUE;
783 case WINHTTP_OPTION_SECURITY_FLAGS:
785 DWORD flags;
787 if (buflen < sizeof(DWORD))
789 set_last_error( ERROR_INSUFFICIENT_BUFFER );
790 return FALSE;
792 flags = *(DWORD *)buffer;
793 TRACE("0x%x\n", flags);
794 if (!(flags & (SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
795 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
796 SECURITY_FLAG_IGNORE_UNKNOWN_CA |
797 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE)))
799 set_last_error( ERROR_INVALID_PARAMETER );
800 return FALSE;
802 request->netconn.security_flags = flags;
803 return TRUE;
805 case WINHTTP_OPTION_RESOLVE_TIMEOUT:
806 request->resolve_timeout = *(DWORD *)buffer;
807 return TRUE;
808 case WINHTTP_OPTION_CONNECT_TIMEOUT:
809 request->connect_timeout = *(DWORD *)buffer;
810 return TRUE;
811 case WINHTTP_OPTION_SEND_TIMEOUT:
812 request->send_timeout = *(DWORD *)buffer;
813 return TRUE;
814 case WINHTTP_OPTION_RECEIVE_TIMEOUT:
815 request->recv_timeout = *(DWORD *)buffer;
816 return TRUE;
818 case WINHTTP_OPTION_USERNAME:
820 connect_t *connect = request->connect;
822 heap_free( connect->username );
823 if (!(connect->username = buffer_to_str( buffer, buflen ))) return FALSE;
824 return TRUE;
826 case WINHTTP_OPTION_PASSWORD:
828 connect_t *connect = request->connect;
830 heap_free( connect->password );
831 if (!(connect->password = buffer_to_str( buffer, buflen ))) return FALSE;
832 return TRUE;
834 case WINHTTP_OPTION_PROXY_USERNAME:
836 session_t *session = request->connect->session;
838 heap_free( session->proxy_username );
839 if (!(session->proxy_username = buffer_to_str( buffer, buflen ))) return FALSE;
840 return TRUE;
842 case WINHTTP_OPTION_PROXY_PASSWORD:
844 session_t *session = request->connect->session;
846 heap_free( session->proxy_password );
847 if (!(session->proxy_password = buffer_to_str( buffer, buflen ))) return FALSE;
848 return TRUE;
850 default:
851 FIXME("unimplemented option %u\n", option);
852 set_last_error( ERROR_INVALID_PARAMETER );
853 return TRUE;
857 static const object_vtbl_t request_vtbl =
859 request_destroy,
860 request_query_option,
861 request_set_option
864 static BOOL store_accept_types( request_t *request, const WCHAR **accept_types )
866 const WCHAR **types = accept_types;
867 int i;
869 if (!types) return TRUE;
870 while (*types)
872 request->num_accept_types++;
873 types++;
875 if (!request->num_accept_types) return TRUE;
876 if (!(request->accept_types = heap_alloc( request->num_accept_types * sizeof(WCHAR *))))
878 request->num_accept_types = 0;
879 return FALSE;
881 types = accept_types;
882 for (i = 0; i < request->num_accept_types; i++)
884 if (!(request->accept_types[i] = strdupW( *types )))
886 for (; i >= 0; i--) heap_free( request->accept_types[i] );
887 heap_free( request->accept_types );
888 request->accept_types = NULL;
889 request->num_accept_types = 0;
890 return FALSE;
892 types++;
894 return TRUE;
897 /***********************************************************************
898 * WinHttpOpenRequest (winhttp.@)
900 HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR object, LPCWSTR version,
901 LPCWSTR referrer, LPCWSTR *types, DWORD flags )
903 request_t *request;
904 connect_t *connect;
905 HINTERNET hrequest = NULL;
907 TRACE("%p, %s, %s, %s, %s, %p, 0x%08x\n", hconnect, debugstr_w(verb), debugstr_w(object),
908 debugstr_w(version), debugstr_w(referrer), types, flags);
910 if (!(connect = (connect_t *)grab_object( hconnect )))
912 set_last_error( ERROR_INVALID_HANDLE );
913 return NULL;
915 if (connect->hdr.type != WINHTTP_HANDLE_TYPE_CONNECT)
917 release_object( &connect->hdr );
918 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
919 return NULL;
921 if (!(request = heap_alloc_zero( sizeof(request_t) )))
923 release_object( &connect->hdr );
924 return NULL;
926 request->hdr.type = WINHTTP_HANDLE_TYPE_REQUEST;
927 request->hdr.vtbl = &request_vtbl;
928 request->hdr.refs = 1;
929 request->hdr.flags = flags;
930 request->hdr.callback = connect->hdr.callback;
931 request->hdr.notify_mask = connect->hdr.notify_mask;
932 request->hdr.context = connect->hdr.context;
933 list_init( &request->hdr.children );
935 addref_object( &connect->hdr );
936 request->connect = connect;
937 list_add_head( &connect->hdr.children, &request->hdr.entry );
939 if (!netconn_init( &request->netconn, request->hdr.flags & WINHTTP_FLAG_SECURE )) goto end;
940 request->resolve_timeout = connect->session->resolve_timeout;
941 request->connect_timeout = connect->session->connect_timeout;
942 request->send_timeout = connect->session->send_timeout;
943 request->recv_timeout = connect->session->recv_timeout;
945 if (!verb || !verb[0]) verb = getW;
946 if (!(request->verb = strdupW( verb ))) goto end;
948 if (object)
950 WCHAR *path, *p;
951 unsigned int len;
953 len = strlenW( object ) + 1;
954 if (object[0] != '/') len++;
955 if (!(p = path = heap_alloc( len * sizeof(WCHAR) ))) goto end;
957 if (object[0] != '/') *p++ = '/';
958 strcpyW( p, object );
959 request->path = path;
961 else if (!(request->path = strdupW( slashW ))) goto end;
963 if (!version || !version[0]) version = http1_1;
964 if (!(request->version = strdupW( version ))) goto end;
965 if (!(store_accept_types( request, types ))) goto end;
967 if (!(hrequest = alloc_handle( &request->hdr ))) goto end;
968 request->hdr.handle = hrequest;
970 send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CREATED, &hrequest, sizeof(hrequest) );
972 end:
973 release_object( &request->hdr );
974 release_object( &connect->hdr );
975 TRACE("returning %p\n", hrequest);
976 return hrequest;
979 /***********************************************************************
980 * WinHttpCloseHandle (winhttp.@)
982 BOOL WINAPI WinHttpCloseHandle( HINTERNET handle )
984 object_header_t *hdr;
986 TRACE("%p\n", handle);
988 if (!(hdr = grab_object( handle )))
990 set_last_error( ERROR_INVALID_HANDLE );
991 return FALSE;
993 release_object( hdr );
994 free_handle( handle );
995 return TRUE;
998 static BOOL query_option( object_header_t *hdr, DWORD option, LPVOID buffer, LPDWORD buflen )
1000 BOOL ret = FALSE;
1002 if (!buflen)
1004 set_last_error( ERROR_INVALID_PARAMETER );
1005 return FALSE;
1008 switch (option)
1010 case WINHTTP_OPTION_CONTEXT_VALUE:
1012 if (!buffer || *buflen < sizeof(DWORD_PTR))
1014 *buflen = sizeof(DWORD_PTR);
1015 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1016 return FALSE;
1019 *(DWORD_PTR *)buffer = hdr->context;
1020 *buflen = sizeof(DWORD_PTR);
1021 return TRUE;
1023 default:
1024 if (hdr->vtbl->query_option) ret = hdr->vtbl->query_option( hdr, option, buffer, buflen );
1025 else
1027 FIXME("unimplemented option %u\n", option);
1028 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1029 return FALSE;
1031 break;
1033 return ret;
1036 /***********************************************************************
1037 * WinHttpQueryOption (winhttp.@)
1039 BOOL WINAPI WinHttpQueryOption( HINTERNET handle, DWORD option, LPVOID buffer, LPDWORD buflen )
1041 BOOL ret = FALSE;
1042 object_header_t *hdr;
1044 TRACE("%p, %u, %p, %p\n", handle, option, buffer, buflen);
1046 if (!(hdr = grab_object( handle )))
1048 set_last_error( ERROR_INVALID_HANDLE );
1049 return FALSE;
1052 ret = query_option( hdr, option, buffer, buflen );
1054 release_object( hdr );
1055 return ret;
1058 static BOOL set_option( object_header_t *hdr, DWORD option, LPVOID buffer, DWORD buflen )
1060 BOOL ret = TRUE;
1062 if (!buffer)
1064 set_last_error( ERROR_INVALID_PARAMETER );
1065 return FALSE;
1068 switch (option)
1070 case WINHTTP_OPTION_CONTEXT_VALUE:
1072 if (buflen != sizeof(DWORD_PTR))
1074 set_last_error( ERROR_INSUFFICIENT_BUFFER );
1075 return FALSE;
1078 hdr->context = *(DWORD_PTR *)buffer;
1079 return TRUE;
1081 default:
1082 if (hdr->vtbl->set_option) ret = hdr->vtbl->set_option( hdr, option, buffer, buflen );
1083 else
1085 FIXME("unimplemented option %u\n", option);
1086 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1087 return FALSE;
1089 break;
1091 return ret;
1094 /***********************************************************************
1095 * WinHttpSetOption (winhttp.@)
1097 BOOL WINAPI WinHttpSetOption( HINTERNET handle, DWORD option, LPVOID buffer, DWORD buflen )
1099 BOOL ret = FALSE;
1100 object_header_t *hdr;
1102 TRACE("%p, %u, %p, %u\n", handle, option, buffer, buflen);
1104 if (!(hdr = grab_object( handle )))
1106 set_last_error( ERROR_INVALID_HANDLE );
1107 return FALSE;
1110 ret = set_option( hdr, option, buffer, buflen );
1112 release_object( hdr );
1113 return ret;
1116 /***********************************************************************
1117 * WinHttpDetectAutoProxyConfigUrl (winhttp.@)
1119 BOOL WINAPI WinHttpDetectAutoProxyConfigUrl( DWORD flags, LPWSTR *url )
1121 FIXME("0x%08x, %p\n", flags, url);
1123 set_last_error( ERROR_WINHTTP_AUTODETECTION_FAILED );
1124 return FALSE;
1127 static const WCHAR Connections[] = {
1128 'S','o','f','t','w','a','r','e','\\',
1129 'M','i','c','r','o','s','o','f','t','\\',
1130 'W','i','n','d','o','w','s','\\',
1131 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1132 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s','\\',
1133 'C','o','n','n','e','c','t','i','o','n','s',0 };
1134 static const WCHAR WinHttpSettings[] = {
1135 'W','i','n','H','t','t','p','S','e','t','t','i','n','g','s',0 };
1136 static const DWORD WINHTTPSETTINGS_MAGIC = 0x18;
1137 static const DWORD WINHTTP_PROXY_TYPE_DIRECT = 1;
1138 static const DWORD WINHTTP_PROXY_TYPE_PROXY = 2;
1140 struct winhttp_settings_header
1142 DWORD magic;
1143 DWORD unknown; /* always zero? */
1144 DWORD flags; /* one of WINHTTP_PROXY_TYPE_* */
1147 static inline void copy_char_to_wchar_sz(const BYTE *src, DWORD len, WCHAR *dst)
1149 const BYTE *begin;
1151 for (begin = src; src - begin < len; src++, dst++)
1152 *dst = *src;
1153 *dst = 0;
1156 /***********************************************************************
1157 * WinHttpGetDefaultProxyConfiguration (winhttp.@)
1159 BOOL WINAPI WinHttpGetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1161 LONG l;
1162 HKEY key;
1163 BOOL got_from_reg = FALSE, direct = TRUE;
1164 char *envproxy;
1166 TRACE("%p\n", info);
1168 l = RegOpenKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, KEY_READ, &key );
1169 if (!l)
1171 DWORD type, size = 0;
1173 l = RegQueryValueExW( key, WinHttpSettings, NULL, &type, NULL, &size );
1174 if (!l && type == REG_BINARY &&
1175 size >= sizeof(struct winhttp_settings_header) + 2 * sizeof(DWORD))
1177 BYTE *buf = heap_alloc( size );
1179 if (buf)
1181 struct winhttp_settings_header *hdr =
1182 (struct winhttp_settings_header *)buf;
1183 DWORD *len = (DWORD *)(hdr + 1);
1185 l = RegQueryValueExW( key, WinHttpSettings, NULL, NULL, buf,
1186 &size );
1187 if (!l && hdr->magic == WINHTTPSETTINGS_MAGIC &&
1188 hdr->unknown == 0)
1190 if (hdr->flags & WINHTTP_PROXY_TYPE_PROXY)
1192 BOOL sane = FALSE;
1193 LPWSTR proxy = NULL;
1194 LPWSTR proxy_bypass = NULL;
1196 /* Sanity-check length of proxy string */
1197 if ((BYTE *)len - buf + *len <= size)
1199 sane = TRUE;
1200 proxy = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1201 if (proxy)
1202 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy );
1203 len = (DWORD *)((BYTE *)(len + 1) + *len);
1205 if (sane)
1207 /* Sanity-check length of proxy bypass string */
1208 if ((BYTE *)len - buf + *len <= size)
1210 proxy_bypass = GlobalAlloc( 0, (*len + 1) * sizeof(WCHAR) );
1211 if (proxy_bypass)
1212 copy_char_to_wchar_sz( (BYTE *)(len + 1), *len, proxy_bypass );
1214 else
1216 sane = FALSE;
1217 GlobalFree( proxy );
1218 proxy = NULL;
1221 info->lpszProxy = proxy;
1222 info->lpszProxyBypass = proxy_bypass;
1223 if (sane)
1225 got_from_reg = TRUE;
1226 direct = FALSE;
1227 info->dwAccessType =
1228 WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1229 TRACE("http proxy (from registry) = %s, bypass = %s\n",
1230 debugstr_w(info->lpszProxy),
1231 debugstr_w(info->lpszProxyBypass));
1235 heap_free( buf );
1238 RegCloseKey( key );
1240 if (!got_from_reg && (envproxy = getenv( "http_proxy" )))
1242 char *colon, *http_proxy;
1244 if ((colon = strchr( envproxy, ':' )))
1246 if (*(colon + 1) == '/' && *(colon + 2) == '/')
1248 static const char http[] = "http://";
1250 /* It's a scheme, check that it's http */
1251 if (!strncmp( envproxy, http, strlen( http ) ))
1252 http_proxy = envproxy + strlen( http );
1253 else
1255 WARN("unsupported scheme in $http_proxy: %s\n", envproxy);
1256 http_proxy = NULL;
1259 else
1260 http_proxy = envproxy;
1262 else
1263 http_proxy = envproxy;
1264 if (http_proxy)
1266 WCHAR *http_proxyW;
1267 int len;
1269 len = MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, NULL, 0 );
1270 if ((http_proxyW = GlobalAlloc( 0, len * sizeof(WCHAR))))
1272 MultiByteToWideChar( CP_UNIXCP, 0, http_proxy, -1, http_proxyW, len );
1273 direct = FALSE;
1274 info->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
1275 info->lpszProxy = http_proxyW;
1276 info->lpszProxyBypass = NULL;
1277 TRACE("http proxy (from environment) = %s\n",
1278 debugstr_w(info->lpszProxy));
1282 if (direct)
1284 info->dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
1285 info->lpszProxy = NULL;
1286 info->lpszProxyBypass = NULL;
1288 return TRUE;
1291 /***********************************************************************
1292 * WinHttpGetIEProxyConfigForCurrentUser (winhttp.@)
1294 BOOL WINAPI WinHttpGetIEProxyConfigForCurrentUser( WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *config )
1296 TRACE("%p\n", config);
1298 if (!config)
1300 set_last_error( ERROR_INVALID_PARAMETER );
1301 return FALSE;
1304 /* FIXME: read from HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings */
1306 FIXME("returning no proxy used\n");
1307 config->fAutoDetect = FALSE;
1308 config->lpszAutoConfigUrl = NULL;
1309 config->lpszProxy = NULL;
1310 config->lpszProxyBypass = NULL;
1312 return TRUE;
1315 /***********************************************************************
1316 * WinHttpGetProxyForUrl (winhttp.@)
1318 BOOL WINAPI WinHttpGetProxyForUrl( HINTERNET hsession, LPCWSTR url, WINHTTP_AUTOPROXY_OPTIONS *options,
1319 WINHTTP_PROXY_INFO *info )
1321 FIXME("%p, %s, %p, %p\n", hsession, debugstr_w(url), options, info);
1323 set_last_error( ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR );
1324 return FALSE;
1327 /***********************************************************************
1328 * WinHttpSetDefaultProxyConfiguration (winhttp.@)
1330 BOOL WINAPI WinHttpSetDefaultProxyConfiguration( WINHTTP_PROXY_INFO *info )
1332 LONG l;
1333 HKEY key;
1334 BOOL ret = FALSE;
1335 const WCHAR *src;
1337 TRACE("%p\n", info);
1339 if (!info)
1341 set_last_error( ERROR_INVALID_PARAMETER );
1342 return FALSE;
1344 switch (info->dwAccessType)
1346 case WINHTTP_ACCESS_TYPE_NO_PROXY:
1347 break;
1348 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
1349 if (!info->lpszProxy)
1351 set_last_error( ERROR_INVALID_PARAMETER );
1352 return FALSE;
1354 /* Only ASCII characters are allowed */
1355 for (src = info->lpszProxy; *src; src++)
1356 if (*src > 0x7f)
1358 set_last_error( ERROR_INVALID_PARAMETER );
1359 return FALSE;
1361 if (info->lpszProxyBypass)
1363 for (src = info->lpszProxyBypass; *src; src++)
1364 if (*src > 0x7f)
1366 set_last_error( ERROR_INVALID_PARAMETER );
1367 return FALSE;
1370 break;
1371 default:
1372 set_last_error( ERROR_INVALID_PARAMETER );
1373 return FALSE;
1376 l = RegCreateKeyExW( HKEY_LOCAL_MACHINE, Connections, 0, NULL, 0,
1377 KEY_WRITE, NULL, &key, NULL );
1378 if (!l)
1380 DWORD size = sizeof(struct winhttp_settings_header) + 2 * sizeof(DWORD);
1381 BYTE *buf;
1383 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
1385 size += strlenW( info->lpszProxy );
1386 if (info->lpszProxyBypass)
1387 size += strlenW( info->lpszProxyBypass );
1389 buf = heap_alloc( size );
1390 if (buf)
1392 struct winhttp_settings_header *hdr =
1393 (struct winhttp_settings_header *)buf;
1394 DWORD *len = (DWORD *)(hdr + 1);
1396 hdr->magic = WINHTTPSETTINGS_MAGIC;
1397 hdr->unknown = 0;
1398 if (info->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY)
1400 BYTE *dst;
1402 hdr->flags = WINHTTP_PROXY_TYPE_PROXY;
1403 *len++ = strlenW( info->lpszProxy );
1404 for (dst = (BYTE *)len, src = info->lpszProxy; *src;
1405 src++, dst++)
1406 *dst = *src;
1407 len = (DWORD *)dst;
1408 if (info->lpszProxyBypass)
1410 *len++ = strlenW( info->lpszProxyBypass );
1411 for (dst = (BYTE *)len, src = info->lpszProxyBypass; *src;
1412 src++, dst++)
1413 *dst = *src;
1415 else
1416 *len++ = 0;
1418 else
1420 hdr->flags = WINHTTP_PROXY_TYPE_DIRECT;
1421 *len++ = 0;
1422 *len++ = 0;
1424 l = RegSetValueExW( key, WinHttpSettings, 0, REG_BINARY, buf, size );
1425 if (!l)
1426 ret = TRUE;
1427 heap_free( buf );
1429 RegCloseKey( key );
1431 return ret;
1434 /***********************************************************************
1435 * WinHttpSetStatusCallback (winhttp.@)
1437 WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback( HINTERNET handle, WINHTTP_STATUS_CALLBACK callback,
1438 DWORD flags, DWORD_PTR reserved )
1440 object_header_t *hdr;
1441 WINHTTP_STATUS_CALLBACK ret;
1443 TRACE("%p, %p, 0x%08x, 0x%lx\n", handle, callback, flags, reserved);
1445 if (!(hdr = grab_object( handle )))
1447 set_last_error( ERROR_INVALID_HANDLE );
1448 return WINHTTP_INVALID_STATUS_CALLBACK;
1450 ret = hdr->callback;
1451 hdr->callback = callback;
1452 hdr->notify_mask = flags;
1454 release_object( hdr );
1455 return ret;
1458 /***********************************************************************
1459 * WinHttpSetTimeouts (winhttp.@)
1461 BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int send, int receive )
1463 BOOL ret = TRUE;
1464 object_header_t *hdr;
1465 request_t *request;
1466 session_t *session;
1468 TRACE("%p, %d, %d, %d, %d\n", handle, resolve, connect, send, receive);
1470 if (resolve < -1 || connect < -1 || send < -1 || receive < -1)
1472 set_last_error( ERROR_INVALID_PARAMETER );
1473 return FALSE;
1476 if (!(hdr = grab_object( handle )))
1478 set_last_error( ERROR_INVALID_HANDLE );
1479 return FALSE;
1482 switch(hdr->type)
1484 case WINHTTP_HANDLE_TYPE_REQUEST:
1485 request = (request_t *)hdr;
1486 request->connect_timeout = connect;
1488 if (resolve < 0) resolve = 0;
1489 request->resolve_timeout = resolve;
1491 if (send < 0) send = 0;
1492 request->send_timeout = send;
1494 if (receive < 0) receive = 0;
1495 request->recv_timeout = receive;
1497 if (netconn_connected( &request->netconn ))
1499 if (netconn_set_timeout( &request->netconn, TRUE, send )) ret = FALSE;
1500 if (netconn_set_timeout( &request->netconn, FALSE, receive )) ret = FALSE;
1503 release_object( &request->hdr );
1504 break;
1506 case WINHTTP_HANDLE_TYPE_SESSION:
1507 session = (session_t *)hdr;
1508 session->connect_timeout = connect;
1510 if (resolve < 0) resolve = 0;
1511 session->resolve_timeout = resolve;
1513 if (send < 0) send = 0;
1514 session->send_timeout = send;
1516 if (receive < 0) receive = 0;
1517 session->recv_timeout = receive;
1518 break;
1520 default:
1521 release_object( hdr );
1522 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE );
1523 return FALSE;
1525 return ret;
1528 static const WCHAR wkday[7][4] =
1529 {{'S','u','n', 0}, {'M','o','n', 0}, {'T','u','e', 0}, {'W','e','d', 0},
1530 {'T','h','u', 0}, {'F','r','i', 0}, {'S','a','t', 0}};
1531 static const WCHAR month[12][4] =
1532 {{'J','a','n', 0}, {'F','e','b', 0}, {'M','a','r', 0}, {'A','p','r', 0},
1533 {'M','a','y', 0}, {'J','u','n', 0}, {'J','u','l', 0}, {'A','u','g', 0},
1534 {'S','e','p', 0}, {'O','c','t', 0}, {'N','o','v', 0}, {'D','e','c', 0}};
1536 /***********************************************************************
1537 * WinHttpTimeFromSystemTime (WININET.@)
1539 BOOL WINAPI WinHttpTimeFromSystemTime( const SYSTEMTIME *time, LPWSTR string )
1541 static const WCHAR format[] =
1542 {'%','s',',',' ','%','0','2','d',' ','%','s',' ','%','4','d',' ','%','0',
1543 '2','d',':','%','0','2','d',':','%','0','2','d',' ','G','M','T', 0};
1545 TRACE("%p, %p\n", time, string);
1547 if (!time || !string) return FALSE;
1549 sprintfW( string, format,
1550 wkday[time->wDayOfWeek],
1551 time->wDay,
1552 month[time->wMonth - 1],
1553 time->wYear,
1554 time->wHour,
1555 time->wMinute,
1556 time->wSecond );
1558 return TRUE;
1561 /***********************************************************************
1562 * WinHttpTimeToSystemTime (WININET.@)
1564 BOOL WINAPI WinHttpTimeToSystemTime( LPCWSTR string, SYSTEMTIME *time )
1566 unsigned int i;
1567 const WCHAR *s = string;
1568 WCHAR *end;
1570 TRACE("%s, %p\n", debugstr_w(string), time);
1572 if (!string || !time) return FALSE;
1574 /* Windows does this too */
1575 GetSystemTime( time );
1577 /* Convert an RFC1123 time such as 'Fri, 07 Jan 2005 12:06:35 GMT' into
1578 * a SYSTEMTIME structure.
1581 while (*s && !isalphaW( *s )) s++;
1582 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
1583 time->wDayOfWeek = 7;
1585 for (i = 0; i < 7; i++)
1587 if (toupperW( wkday[i][0] ) == toupperW( s[0] ) &&
1588 toupperW( wkday[i][1] ) == toupperW( s[1] ) &&
1589 toupperW( wkday[i][2] ) == toupperW( s[2] ) )
1591 time->wDayOfWeek = i;
1592 break;
1596 if (time->wDayOfWeek > 6) return TRUE;
1597 while (*s && !isdigitW( *s )) s++;
1598 time->wDay = strtolW( s, &end, 10 );
1599 s = end;
1601 while (*s && !isalphaW( *s )) s++;
1602 if (s[0] == '\0' || s[1] == '\0' || s[2] == '\0') return TRUE;
1603 time->wMonth = 0;
1605 for (i = 0; i < 12; i++)
1607 if (toupperW( month[i][0]) == toupperW( s[0] ) &&
1608 toupperW( month[i][1]) == toupperW( s[1] ) &&
1609 toupperW( month[i][2]) == toupperW( s[2] ) )
1611 time->wMonth = i + 1;
1612 break;
1615 if (time->wMonth == 0) return TRUE;
1617 while (*s && !isdigitW( *s )) s++;
1618 if (*s == '\0') return TRUE;
1619 time->wYear = strtolW( s, &end, 10 );
1620 s = end;
1622 while (*s && !isdigitW( *s )) s++;
1623 if (*s == '\0') return TRUE;
1624 time->wHour = strtolW( s, &end, 10 );
1625 s = end;
1627 while (*s && !isdigitW( *s )) s++;
1628 if (*s == '\0') return TRUE;
1629 time->wMinute = strtolW( s, &end, 10 );
1630 s = end;
1632 while (*s && !isdigitW( *s )) s++;
1633 if (*s == '\0') return TRUE;
1634 time->wSecond = strtolW( s, &end, 10 );
1636 time->wMilliseconds = 0;
1637 return TRUE;