2 * Copyright 2004 Mike McCormack for CodeWeavers
3 * Copyright 2006 Rob Shearman for CodeWeavers
4 * Copyright 2008, 2011 Hans Leidekker for CodeWeavers
5 * Copyright 2009 Juan Lang
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
28 #ifdef HAVE_ARPA_INET_H
29 # include <arpa/inet.h>
36 #include "httprequest.h"
39 #include "winhttp_private.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(winhttp
);
45 static const WCHAR attr_accept
[] = {'A','c','c','e','p','t',0};
46 static const WCHAR attr_accept_charset
[] = {'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0};
47 static const WCHAR attr_accept_encoding
[] = {'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0};
48 static const WCHAR attr_accept_language
[] = {'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0};
49 static const WCHAR attr_accept_ranges
[] = {'A','c','c','e','p','t','-','R','a','n','g','e','s',0};
50 static const WCHAR attr_age
[] = {'A','g','e',0};
51 static const WCHAR attr_allow
[] = {'A','l','l','o','w',0};
52 static const WCHAR attr_authorization
[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',0};
53 static const WCHAR attr_cache_control
[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l',0};
54 static const WCHAR attr_connection
[] = {'C','o','n','n','e','c','t','i','o','n',0};
55 static const WCHAR attr_content_base
[] = {'C','o','n','t','e','n','t','-','B','a','s','e',0};
56 static const WCHAR attr_content_encoding
[] = {'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0};
57 static const WCHAR attr_content_id
[] = {'C','o','n','t','e','n','t','-','I','D',0};
58 static const WCHAR attr_content_language
[] = {'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0};
59 static const WCHAR attr_content_length
[] = {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
60 static const WCHAR attr_content_location
[] = {'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0};
61 static const WCHAR attr_content_md5
[] = {'C','o','n','t','e','n','t','-','M','D','5',0};
62 static const WCHAR attr_content_range
[] = {'C','o','n','t','e','n','t','-','R','a','n','g','e',0};
63 static const WCHAR attr_content_transfer_encoding
[] = {'C','o','n','t','e','n','t','-','T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0};
64 static const WCHAR attr_content_type
[] = {'C','o','n','t','e','n','t','-','T','y','p','e',0};
65 static const WCHAR attr_cookie
[] = {'C','o','o','k','i','e',0};
66 static const WCHAR attr_date
[] = {'D','a','t','e',0};
67 static const WCHAR attr_from
[] = {'F','r','o','m',0};
68 static const WCHAR attr_etag
[] = {'E','T','a','g',0};
69 static const WCHAR attr_expect
[] = {'E','x','p','e','c','t',0};
70 static const WCHAR attr_expires
[] = {'E','x','p','i','r','e','s',0};
71 static const WCHAR attr_host
[] = {'H','o','s','t',0};
72 static const WCHAR attr_if_match
[] = {'I','f','-','M','a','t','c','h',0};
73 static const WCHAR attr_if_modified_since
[] = {'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0};
74 static const WCHAR attr_if_none_match
[] = {'I','f','-','N','o','n','e','-','M','a','t','c','h',0};
75 static const WCHAR attr_if_range
[] = {'I','f','-','R','a','n','g','e',0};
76 static const WCHAR attr_if_unmodified_since
[] = {'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0};
77 static const WCHAR attr_last_modified
[] = {'L','a','s','t','-','M','o','d','i','f','i','e','d',0};
78 static const WCHAR attr_location
[] = {'L','o','c','a','t','i','o','n',0};
79 static const WCHAR attr_max_forwards
[] = {'M','a','x','-','F','o','r','w','a','r','d','s',0};
80 static const WCHAR attr_mime_version
[] = {'M','i','m','e','-','V','e','r','s','i','o','n',0};
81 static const WCHAR attr_pragma
[] = {'P','r','a','g','m','a',0};
82 static const WCHAR attr_proxy_authenticate
[] = {'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0};
83 static const WCHAR attr_proxy_authorization
[] = {'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0};
84 static const WCHAR attr_proxy_connection
[] = {'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0};
85 static const WCHAR attr_public
[] = {'P','u','b','l','i','c',0};
86 static const WCHAR attr_range
[] = {'R','a','n','g','e',0};
87 static const WCHAR attr_referer
[] = {'R','e','f','e','r','e','r',0};
88 static const WCHAR attr_retry_after
[] = {'R','e','t','r','y','-','A','f','t','e','r',0};
89 static const WCHAR attr_server
[] = {'S','e','r','v','e','r',0};
90 static const WCHAR attr_set_cookie
[] = {'S','e','t','-','C','o','o','k','i','e',0};
91 static const WCHAR attr_status
[] = {'S','t','a','t','u','s',0};
92 static const WCHAR attr_transfer_encoding
[] = {'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0};
93 static const WCHAR attr_unless_modified_since
[] = {'U','n','l','e','s','s','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0};
94 static const WCHAR attr_upgrade
[] = {'U','p','g','r','a','d','e',0};
95 static const WCHAR attr_uri
[] = {'U','R','I',0};
96 static const WCHAR attr_user_agent
[] = {'U','s','e','r','-','A','g','e','n','t',0};
97 static const WCHAR attr_vary
[] = {'V','a','r','y',0};
98 static const WCHAR attr_via
[] = {'V','i','a',0};
99 static const WCHAR attr_warning
[] = {'W','a','r','n','i','n','g',0};
100 static const WCHAR attr_www_authenticate
[] = {'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0};
102 static const WCHAR
*attribute_table
[] =
104 attr_mime_version
, /* WINHTTP_QUERY_MIME_VERSION = 0 */
105 attr_content_type
, /* WINHTTP_QUERY_CONTENT_TYPE = 1 */
106 attr_content_transfer_encoding
, /* WINHTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
107 attr_content_id
, /* WINHTTP_QUERY_CONTENT_ID = 3 */
108 NULL
, /* WINHTTP_QUERY_CONTENT_DESCRIPTION = 4 */
109 attr_content_length
, /* WINHTTP_QUERY_CONTENT_LENGTH = 5 */
110 attr_content_language
, /* WINHTTP_QUERY_CONTENT_LANGUAGE = 6 */
111 attr_allow
, /* WINHTTP_QUERY_ALLOW = 7 */
112 attr_public
, /* WINHTTP_QUERY_PUBLIC = 8 */
113 attr_date
, /* WINHTTP_QUERY_DATE = 9 */
114 attr_expires
, /* WINHTTP_QUERY_EXPIRES = 10 */
115 attr_last_modified
, /* WINHTTP_QUERY_LAST_MODIFIEDcw = 11 */
116 NULL
, /* WINHTTP_QUERY_MESSAGE_ID = 12 */
117 attr_uri
, /* WINHTTP_QUERY_URI = 13 */
118 attr_from
, /* WINHTTP_QUERY_DERIVED_FROM = 14 */
119 NULL
, /* WINHTTP_QUERY_COST = 15 */
120 NULL
, /* WINHTTP_QUERY_LINK = 16 */
121 attr_pragma
, /* WINHTTP_QUERY_PRAGMA = 17 */
122 NULL
, /* WINHTTP_QUERY_VERSION = 18 */
123 attr_status
, /* WINHTTP_QUERY_STATUS_CODE = 19 */
124 NULL
, /* WINHTTP_QUERY_STATUS_TEXT = 20 */
125 NULL
, /* WINHTTP_QUERY_RAW_HEADERS = 21 */
126 NULL
, /* WINHTTP_QUERY_RAW_HEADERS_CRLF = 22 */
127 attr_connection
, /* WINHTTP_QUERY_CONNECTION = 23 */
128 attr_accept
, /* WINHTTP_QUERY_ACCEPT = 24 */
129 attr_accept_charset
, /* WINHTTP_QUERY_ACCEPT_CHARSET = 25 */
130 attr_accept_encoding
, /* WINHTTP_QUERY_ACCEPT_ENCODING = 26 */
131 attr_accept_language
, /* WINHTTP_QUERY_ACCEPT_LANGUAGE = 27 */
132 attr_authorization
, /* WINHTTP_QUERY_AUTHORIZATION = 28 */
133 attr_content_encoding
, /* WINHTTP_QUERY_CONTENT_ENCODING = 29 */
134 NULL
, /* WINHTTP_QUERY_FORWARDED = 30 */
135 NULL
, /* WINHTTP_QUERY_FROM = 31 */
136 attr_if_modified_since
, /* WINHTTP_QUERY_IF_MODIFIED_SINCE = 32 */
137 attr_location
, /* WINHTTP_QUERY_LOCATION = 33 */
138 NULL
, /* WINHTTP_QUERY_ORIG_URI = 34 */
139 attr_referer
, /* WINHTTP_QUERY_REFERER = 35 */
140 attr_retry_after
, /* WINHTTP_QUERY_RETRY_AFTER = 36 */
141 attr_server
, /* WINHTTP_QUERY_SERVER = 37 */
142 NULL
, /* WINHTTP_TITLE = 38 */
143 attr_user_agent
, /* WINHTTP_QUERY_USER_AGENT = 39 */
144 attr_www_authenticate
, /* WINHTTP_QUERY_WWW_AUTHENTICATE = 40 */
145 attr_proxy_authenticate
, /* WINHTTP_QUERY_PROXY_AUTHENTICATE = 41 */
146 attr_accept_ranges
, /* WINHTTP_QUERY_ACCEPT_RANGES = 42 */
147 attr_set_cookie
, /* WINHTTP_QUERY_SET_COOKIE = 43 */
148 attr_cookie
, /* WINHTTP_QUERY_COOKIE = 44 */
149 NULL
, /* WINHTTP_QUERY_REQUEST_METHOD = 45 */
150 NULL
, /* WINHTTP_QUERY_REFRESH = 46 */
151 NULL
, /* WINHTTP_QUERY_CONTENT_DISPOSITION = 47 */
152 attr_age
, /* WINHTTP_QUERY_AGE = 48 */
153 attr_cache_control
, /* WINHTTP_QUERY_CACHE_CONTROL = 49 */
154 attr_content_base
, /* WINHTTP_QUERY_CONTENT_BASE = 50 */
155 attr_content_location
, /* WINHTTP_QUERY_CONTENT_LOCATION = 51 */
156 attr_content_md5
, /* WINHTTP_QUERY_CONTENT_MD5 = 52 */
157 attr_content_range
, /* WINHTTP_QUERY_CONTENT_RANGE = 53 */
158 attr_etag
, /* WINHTTP_QUERY_ETAG = 54 */
159 attr_host
, /* WINHTTP_QUERY_HOST = 55 */
160 attr_if_match
, /* WINHTTP_QUERY_IF_MATCH = 56 */
161 attr_if_none_match
, /* WINHTTP_QUERY_IF_NONE_MATCH = 57 */
162 attr_if_range
, /* WINHTTP_QUERY_IF_RANGE = 58 */
163 attr_if_unmodified_since
, /* WINHTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
164 attr_max_forwards
, /* WINHTTP_QUERY_MAX_FORWARDS = 60 */
165 attr_proxy_authorization
, /* WINHTTP_QUERY_PROXY_AUTHORIZATION = 61 */
166 attr_range
, /* WINHTTP_QUERY_RANGE = 62 */
167 attr_transfer_encoding
, /* WINHTTP_QUERY_TRANSFER_ENCODING = 63 */
168 attr_upgrade
, /* WINHTTP_QUERY_UPGRADE = 64 */
169 attr_vary
, /* WINHTTP_QUERY_VARY = 65 */
170 attr_via
, /* WINHTTP_QUERY_VIA = 66 */
171 attr_warning
, /* WINHTTP_QUERY_WARNING = 67 */
172 attr_expect
, /* WINHTTP_QUERY_EXPECT = 68 */
173 attr_proxy_connection
, /* WINHTTP_QUERY_PROXY_CONNECTION = 69 */
174 attr_unless_modified_since
, /* WINHTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
175 NULL
, /* WINHTTP_QUERY_PROXY_SUPPORT = 75 */
176 NULL
, /* WINHTTP_QUERY_AUTHENTICATION_INFO = 76 */
177 NULL
, /* WINHTTP_QUERY_PASSPORT_URLS = 77 */
178 NULL
/* WINHTTP_QUERY_PASSPORT_CONFIG = 78 */
181 static task_header_t
*dequeue_task( request_t
*request
)
185 EnterCriticalSection( &request
->task_cs
);
186 TRACE("%u tasks queued\n", list_count( &request
->task_queue
));
187 task
= LIST_ENTRY( list_head( &request
->task_queue
), task_header_t
, entry
);
188 if (task
) list_remove( &task
->entry
);
189 LeaveCriticalSection( &request
->task_cs
);
191 TRACE("returning task %p\n", task
);
195 static DWORD CALLBACK
task_proc( LPVOID param
)
197 request_t
*request
= param
;
200 handles
[0] = request
->task_wait
;
201 handles
[1] = request
->task_cancel
;
204 DWORD err
= WaitForMultipleObjects( 2, handles
, FALSE
, INFINITE
);
210 while ((task
= dequeue_task( request
)))
213 release_object( &task
->request
->hdr
);
218 case WAIT_OBJECT_0
+ 1:
220 CloseHandle( request
->task_cancel
);
221 CloseHandle( request
->task_wait
);
222 request
->task_cs
.DebugInfo
->Spare
[0] = 0;
223 DeleteCriticalSection( &request
->task_cs
);
224 request
->hdr
.vtbl
->destroy( &request
->hdr
);
228 ERR("wait failed %u (%u)\n", err
, GetLastError());
235 static BOOL
queue_task( task_header_t
*task
)
237 request_t
*request
= task
->request
;
239 if (!request
->task_thread
)
241 if (!(request
->task_wait
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
))) return FALSE
;
242 if (!(request
->task_cancel
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
)))
244 CloseHandle( request
->task_wait
);
245 request
->task_wait
= NULL
;
248 if (!(request
->task_thread
= CreateThread( NULL
, 0, task_proc
, request
, 0, NULL
)))
250 CloseHandle( request
->task_wait
);
251 request
->task_wait
= NULL
;
252 CloseHandle( request
->task_cancel
);
253 request
->task_cancel
= NULL
;
256 InitializeCriticalSection( &request
->task_cs
);
257 request
->task_cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": request.task_cs");
260 EnterCriticalSection( &request
->task_cs
);
261 TRACE("queueing task %p\n", task
);
262 list_add_tail( &request
->task_queue
, &task
->entry
);
263 LeaveCriticalSection( &request
->task_cs
);
265 SetEvent( request
->task_wait
);
269 static void free_header( header_t
*header
)
271 heap_free( header
->field
);
272 heap_free( header
->value
);
276 static BOOL
valid_token_char( WCHAR c
)
278 if (c
< 32 || c
== 127) return FALSE
;
285 case '\\': case '\"':
297 static header_t
*parse_header( LPCWSTR string
)
304 if (!(q
= strchrW( p
, ':' )))
306 WARN("no ':' in line %s\n", debugstr_w(string
));
311 WARN("empty field name in line %s\n", debugstr_w(string
));
316 if (!valid_token_char( *p
))
318 WARN("invalid character in field name %s\n", debugstr_w(string
));
324 if (!(header
= heap_alloc_zero( sizeof(header_t
) ))) return NULL
;
325 if (!(header
->field
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) )))
330 memcpy( header
->field
, string
, len
* sizeof(WCHAR
) );
331 header
->field
[len
] = 0;
333 q
++; /* skip past colon */
334 while (*q
== ' ') q
++;
337 if (!(header
->value
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) )))
339 free_header( header
);
342 memcpy( header
->value
, q
, len
* sizeof(WCHAR
) );
343 header
->value
[len
] = 0;
348 static int get_header_index( request_t
*request
, LPCWSTR field
, int requested_index
, BOOL request_only
)
352 TRACE("%s\n", debugstr_w(field
));
354 for (index
= 0; index
< request
->num_headers
; index
++)
356 if (strcmpiW( request
->headers
[index
].field
, field
)) continue;
357 if (request_only
&& !request
->headers
[index
].is_request
) continue;
358 if (!request_only
&& request
->headers
[index
].is_request
) continue;
360 if (!requested_index
) break;
363 if (index
>= request
->num_headers
) index
= -1;
364 TRACE("returning %d\n", index
);
368 static BOOL
insert_header( request_t
*request
, header_t
*header
)
373 count
= request
->num_headers
+ 1;
375 hdrs
= heap_realloc_zero( request
->headers
, sizeof(header_t
) * count
);
377 hdrs
= heap_alloc_zero( sizeof(header_t
) * count
);
381 request
->headers
= hdrs
;
382 request
->headers
[count
- 1].field
= strdupW( header
->field
);
383 request
->headers
[count
- 1].value
= strdupW( header
->value
);
384 request
->headers
[count
- 1].is_request
= header
->is_request
;
385 request
->num_headers
++;
391 static BOOL
delete_header( request_t
*request
, DWORD index
)
393 if (!request
->num_headers
) return FALSE
;
394 if (index
>= request
->num_headers
) return FALSE
;
395 request
->num_headers
--;
397 heap_free( request
->headers
[index
].field
);
398 heap_free( request
->headers
[index
].value
);
400 memmove( &request
->headers
[index
], &request
->headers
[index
+ 1], (request
->num_headers
- index
) * sizeof(header_t
) );
401 memset( &request
->headers
[request
->num_headers
], 0, sizeof(header_t
) );
405 static BOOL
process_header( request_t
*request
, LPCWSTR field
, LPCWSTR value
, DWORD flags
, BOOL request_only
)
410 TRACE("%s: %s 0x%08x\n", debugstr_w(field
), debugstr_w(value
), flags
);
412 if ((index
= get_header_index( request
, field
, 0, request_only
)) >= 0)
414 if (flags
& WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
) return FALSE
;
417 if (flags
& WINHTTP_ADDREQ_FLAG_REPLACE
)
421 delete_header( request
, index
);
422 if (!value
|| !value
[0]) return TRUE
;
424 else if (!(flags
& WINHTTP_ADDREQ_FLAG_ADD
))
426 set_last_error( ERROR_WINHTTP_HEADER_NOT_FOUND
);
430 hdr
.field
= (LPWSTR
)field
;
431 hdr
.value
= (LPWSTR
)value
;
432 hdr
.is_request
= request_only
;
433 return insert_header( request
, &hdr
);
438 if ((flags
& (WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA
| WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON
)) &&
442 int len
, len_orig
, len_value
;
443 header_t
*header
= &request
->headers
[index
];
445 len_orig
= strlenW( header
->value
);
446 len_value
= strlenW( value
);
448 len
= len_orig
+ len_value
+ 2;
449 if (!(tmp
= heap_realloc( header
->value
, (len
+ 1) * sizeof(WCHAR
) ))) return FALSE
;
451 header
->value
[len_orig
++] = (flags
& WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA
) ? ',' : ';';
452 header
->value
[len_orig
++] = ' ';
454 memcpy( &header
->value
[len_orig
], value
, len_value
* sizeof(WCHAR
) );
455 header
->value
[len
] = 0;
460 hdr
.field
= (LPWSTR
)field
;
461 hdr
.value
= (LPWSTR
)value
;
462 hdr
.is_request
= request_only
;
463 return insert_header( request
, &hdr
);
470 BOOL
add_request_headers( request_t
*request
, LPCWSTR headers
, DWORD len
, DWORD flags
)
473 WCHAR
*buffer
, *p
, *q
;
476 if (len
== ~0u) len
= strlenW( headers
);
477 if (!len
) return TRUE
;
478 if (!(buffer
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) return FALSE
;
479 memcpy( buffer
, headers
, len
* sizeof(WCHAR
) );
488 if (q
[0] == '\n' && q
[1] == '\r')
493 if (q
[0] == '\r' && q
[1] == '\n') break;
500 q
+= 2; /* jump over \r\n */
502 if ((header
= parse_header( p
)))
504 ret
= process_header( request
, header
->field
, header
->value
, flags
, TRUE
);
505 free_header( header
);
514 /***********************************************************************
515 * WinHttpAddRequestHeaders (winhttp.@)
517 BOOL WINAPI
WinHttpAddRequestHeaders( HINTERNET hrequest
, LPCWSTR headers
, DWORD len
, DWORD flags
)
522 TRACE("%p, %s, 0x%x, 0x%08x\n", hrequest
, debugstr_w(headers
), len
, flags
);
524 if (!headers
|| !len
)
526 set_last_error( ERROR_INVALID_PARAMETER
);
529 if (!(request
= (request_t
*)grab_object( hrequest
)))
531 set_last_error( ERROR_INVALID_HANDLE
);
534 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
536 release_object( &request
->hdr
);
537 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
541 ret
= add_request_headers( request
, headers
, len
, flags
);
543 release_object( &request
->hdr
);
544 if (ret
) set_last_error( ERROR_SUCCESS
);
548 static WCHAR
*build_request_path( request_t
*request
)
552 if (strcmpiW( request
->connect
->hostname
, request
->connect
->servername
))
554 static const WCHAR http
[] = { 'h','t','t','p',0 };
555 static const WCHAR https
[] = { 'h','t','t','p','s',0 };
556 static const WCHAR fmt
[] = { '%','s',':','/','/','%','s',0 };
557 LPCWSTR scheme
= request
->netconn
.secure
? https
: http
;
560 len
= strlenW( scheme
) + strlenW( request
->connect
->hostname
);
561 /* 3 characters for '://', 1 for NUL. */
563 if (request
->connect
->hostport
)
565 /* 1 for ':' between host and port, up to 5 for port */
569 len
+= strlenW( request
->path
);
570 if ((ret
= heap_alloc( len
* sizeof(WCHAR
) )))
572 sprintfW( ret
, fmt
, scheme
, request
->connect
->hostname
);
573 if (request
->connect
->hostport
)
575 static const WCHAR colonFmt
[] = { ':','%','u',0 };
577 sprintfW( ret
+ strlenW( ret
), colonFmt
,
578 request
->connect
->hostport
);
581 strcatW( ret
, request
->path
);
589 static WCHAR
*build_request_string( request_t
*request
)
591 static const WCHAR space
[] = {' ',0};
592 static const WCHAR crlf
[] = {'\r','\n',0};
593 static const WCHAR colon
[] = {':',' ',0};
594 static const WCHAR twocrlf
[] = {'\r','\n','\r','\n',0};
597 const WCHAR
**headers
, **p
;
598 unsigned int len
, i
= 0, j
;
600 /* allocate space for an array of all the string pointers to be added */
601 len
= request
->num_headers
* 4 + 7;
602 if (!(headers
= heap_alloc( len
* sizeof(LPCWSTR
) ))) return NULL
;
604 path
= build_request_path( request
);
605 headers
[i
++] = request
->verb
;
606 headers
[i
++] = space
;
608 headers
[i
++] = space
;
609 headers
[i
++] = request
->version
;
611 for (j
= 0; j
< request
->num_headers
; j
++)
613 if (request
->headers
[j
].is_request
)
616 headers
[i
++] = request
->headers
[j
].field
;
617 headers
[i
++] = colon
;
618 headers
[i
++] = request
->headers
[j
].value
;
620 TRACE("adding header %s (%s)\n", debugstr_w(request
->headers
[j
].field
),
621 debugstr_w(request
->headers
[j
].value
));
624 headers
[i
++] = twocrlf
;
628 for (p
= headers
; *p
; p
++) len
+= strlenW( *p
);
631 if (!(ret
= heap_alloc( len
* sizeof(WCHAR
) )))
634 for (p
= headers
; *p
; p
++) strcatW( ret
, *p
);
637 if (path
!= request
->path
)
639 heap_free( headers
);
643 #define QUERY_MODIFIER_MASK (WINHTTP_QUERY_FLAG_REQUEST_HEADERS | WINHTTP_QUERY_FLAG_SYSTEMTIME | WINHTTP_QUERY_FLAG_NUMBER)
645 static BOOL
query_headers( request_t
*request
, DWORD level
, LPCWSTR name
, LPVOID buffer
, LPDWORD buflen
, LPDWORD index
)
647 header_t
*header
= NULL
;
648 BOOL request_only
, ret
= FALSE
;
649 int requested_index
, header_index
= -1;
652 request_only
= level
& WINHTTP_QUERY_FLAG_REQUEST_HEADERS
;
653 requested_index
= index
? *index
: 0;
655 attr
= level
& ~QUERY_MODIFIER_MASK
;
658 case WINHTTP_QUERY_CUSTOM
:
660 header_index
= get_header_index( request
, name
, requested_index
, request_only
);
663 case WINHTTP_QUERY_RAW_HEADERS
:
665 WCHAR
*headers
, *p
, *q
;
668 headers
= build_request_string( request
);
670 headers
= request
->raw_headers
;
672 if (!(p
= headers
)) return FALSE
;
673 for (len
= 0; *p
; p
++) if (*p
!= '\r') len
++;
675 if (!buffer
|| len
* sizeof(WCHAR
) > *buflen
)
676 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
679 for (p
= headers
, q
= buffer
; *p
; p
++, q
++)
681 if (*p
!= '\r') *q
= *p
;
688 TRACE("returning data: %s\n", debugstr_wn(buffer
, len
));
692 *buflen
= len
* sizeof(WCHAR
);
693 if (request_only
) heap_free( headers
);
696 case WINHTTP_QUERY_RAW_HEADERS_CRLF
:
701 headers
= build_request_string( request
);
703 headers
= request
->raw_headers
;
705 if (!headers
) return FALSE
;
706 len
= strlenW( headers
) * sizeof(WCHAR
);
707 if (!buffer
|| len
+ sizeof(WCHAR
) > *buflen
)
709 len
+= sizeof(WCHAR
);
710 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
714 memcpy( buffer
, headers
, len
+ sizeof(WCHAR
) );
715 TRACE("returning data: %s\n", debugstr_wn(buffer
, len
/ sizeof(WCHAR
)));
719 if (request_only
) heap_free( headers
);
722 case WINHTTP_QUERY_VERSION
:
723 len
= strlenW( request
->version
) * sizeof(WCHAR
);
724 if (!buffer
|| len
+ sizeof(WCHAR
) > *buflen
)
726 len
+= sizeof(WCHAR
);
727 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
731 strcpyW( buffer
, request
->version
);
732 TRACE("returning string: %s\n", debugstr_w(buffer
));
738 case WINHTTP_QUERY_STATUS_TEXT
:
739 len
= strlenW( request
->status_text
) * sizeof(WCHAR
);
740 if (!buffer
|| len
+ sizeof(WCHAR
) > *buflen
)
742 len
+= sizeof(WCHAR
);
743 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
747 strcpyW( buffer
, request
->status_text
);
748 TRACE("returning string: %s\n", debugstr_w(buffer
));
755 if (attr
>= sizeof(attribute_table
)/sizeof(attribute_table
[0]) || !attribute_table
[attr
])
757 FIXME("attribute %u not implemented\n", attr
);
760 TRACE("attribute %s\n", debugstr_w(attribute_table
[attr
]));
761 header_index
= get_header_index( request
, attribute_table
[attr
], requested_index
, request_only
);
765 if (header_index
>= 0)
767 header
= &request
->headers
[header_index
];
769 if (!header
|| (request_only
&& !header
->is_request
))
771 set_last_error( ERROR_WINHTTP_HEADER_NOT_FOUND
);
774 if (index
) *index
+= 1;
775 if (level
& WINHTTP_QUERY_FLAG_NUMBER
)
777 if (!buffer
|| sizeof(int) > *buflen
)
779 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
783 int *number
= buffer
;
784 *number
= atoiW( header
->value
);
785 TRACE("returning number: %d\n", *number
);
788 *buflen
= sizeof(int);
790 else if (level
& WINHTTP_QUERY_FLAG_SYSTEMTIME
)
792 SYSTEMTIME
*st
= buffer
;
793 if (!buffer
|| sizeof(SYSTEMTIME
) > *buflen
)
795 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
797 else if ((ret
= WinHttpTimeToSystemTime( header
->value
, st
)))
799 TRACE("returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
800 st
->wYear
, st
->wMonth
, st
->wDay
, st
->wDayOfWeek
,
801 st
->wHour
, st
->wMinute
, st
->wSecond
, st
->wMilliseconds
);
803 *buflen
= sizeof(SYSTEMTIME
);
805 else if (header
->value
)
807 len
= strlenW( header
->value
) * sizeof(WCHAR
);
808 if (!buffer
|| len
+ sizeof(WCHAR
) > *buflen
)
810 len
+= sizeof(WCHAR
);
811 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
815 strcpyW( buffer
, header
->value
);
816 TRACE("returning string: %s\n", debugstr_w(buffer
));
824 /***********************************************************************
825 * WinHttpQueryHeaders (winhttp.@)
827 BOOL WINAPI
WinHttpQueryHeaders( HINTERNET hrequest
, DWORD level
, LPCWSTR name
, LPVOID buffer
, LPDWORD buflen
, LPDWORD index
)
832 TRACE("%p, 0x%08x, %s, %p, %p, %p\n", hrequest
, level
, debugstr_w(name
), buffer
, buflen
, index
);
834 if (!(request
= (request_t
*)grab_object( hrequest
)))
836 set_last_error( ERROR_INVALID_HANDLE
);
839 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
841 release_object( &request
->hdr
);
842 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
846 ret
= query_headers( request
, level
, name
, buffer
, buflen
, index
);
848 release_object( &request
->hdr
);
849 if (ret
) set_last_error( ERROR_SUCCESS
);
853 static LPWSTR
concatenate_string_list( LPCWSTR
*list
, int len
)
858 for( t
= list
; *t
; t
++ )
859 len
+= strlenW( *t
);
862 str
= heap_alloc( len
* sizeof(WCHAR
) );
863 if (!str
) return NULL
;
866 for( t
= list
; *t
; t
++ )
872 static LPWSTR
build_header_request_string( request_t
*request
, LPCWSTR verb
,
873 LPCWSTR path
, LPCWSTR version
)
875 static const WCHAR crlf
[] = {'\r','\n',0};
876 static const WCHAR space
[] = { ' ',0 };
877 static const WCHAR colon
[] = { ':',' ',0 };
878 static const WCHAR twocrlf
[] = {'\r','\n','\r','\n', 0};
879 LPWSTR requestString
;
885 /* allocate space for an array of all the string pointers to be added */
886 len
= (request
->num_headers
) * 4 + 10;
887 req
= heap_alloc( len
* sizeof(LPCWSTR
) );
888 if (!req
) return NULL
;
890 /* add the verb, path and HTTP version string */
898 /* Append custom request headers */
899 for (i
= 0; i
< request
->num_headers
; i
++)
901 if (request
->headers
[i
].is_request
)
904 req
[n
++] = request
->headers
[i
].field
;
906 req
[n
++] = request
->headers
[i
].value
;
908 TRACE("Adding custom header %s (%s)\n",
909 debugstr_w(request
->headers
[i
].field
),
910 debugstr_w(request
->headers
[i
].value
));
915 ERR("oops. buffer overrun\n");
918 requestString
= concatenate_string_list( req
, 4 );
920 if (!requestString
) return NULL
;
923 * Set (header) termination string for request
924 * Make sure there are exactly two new lines at the end of the request
926 p
= &requestString
[strlenW(requestString
)-1];
927 while ( (*p
== '\n') || (*p
== '\r') )
929 strcpyW( p
+1, twocrlf
);
931 return requestString
;
934 static BOOL
read_reply( request_t
*request
);
936 static BOOL
secure_proxy_connect( request_t
*request
)
938 static const WCHAR verbConnect
[] = {'C','O','N','N','E','C','T',0};
939 static const WCHAR fmt
[] = {'%','s',':','%','u',0};
942 connect_t
*connect
= request
->connect
;
944 path
= heap_alloc( (strlenW( connect
->hostname
) + 13) * sizeof(WCHAR
) );
947 LPWSTR requestString
;
949 sprintfW( path
, fmt
, connect
->hostname
, connect
->hostport
);
950 requestString
= build_header_request_string( request
, verbConnect
,
955 LPSTR req_ascii
= strdupWA( requestString
);
957 heap_free( requestString
);
960 int len
= strlen( req_ascii
), bytes_sent
;
962 ret
= netconn_send( &request
->netconn
, req_ascii
, len
, &bytes_sent
);
963 heap_free( req_ascii
);
965 ret
= read_reply( request
);
972 #ifndef INET6_ADDRSTRLEN
973 #define INET6_ADDRSTRLEN 46
976 static WCHAR
*addr_to_str( struct sockaddr
*addr
)
978 char buf
[INET6_ADDRSTRLEN
];
981 switch (addr
->sa_family
)
984 src
= &((struct sockaddr_in
*)addr
)->sin_addr
;
987 src
= &((struct sockaddr_in6
*)addr
)->sin6_addr
;
990 WARN("unsupported address family %d\n", addr
->sa_family
);
993 if (!inet_ntop( addr
->sa_family
, src
, buf
, sizeof(buf
) )) return NULL
;
994 return strdupAW( buf
);
997 static BOOL
open_connection( request_t
*request
)
1000 WCHAR
*addressW
= NULL
;
1003 struct sockaddr
*saddr
;
1006 if (netconn_connected( &request
->netconn
)) goto done
;
1008 connect
= request
->connect
;
1009 port
= connect
->serverport
? connect
->serverport
: (request
->hdr
.flags
& WINHTTP_FLAG_SECURE
? 443 : 80);
1010 saddr
= (struct sockaddr
*)&connect
->sockaddr
;
1011 slen
= sizeof(struct sockaddr
);
1013 if (!connect
->resolved
)
1015 len
= strlenW( connect
->servername
) + 1;
1016 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_RESOLVING_NAME
, connect
->servername
, len
);
1018 if (!netconn_resolve( connect
->servername
, port
, saddr
, &slen
, request
->resolve_timeout
)) return FALSE
;
1019 connect
->resolved
= TRUE
;
1021 if (!(addressW
= addr_to_str( saddr
))) return FALSE
;
1022 len
= strlenW( addressW
) + 1;
1023 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_NAME_RESOLVED
, addressW
, len
);
1025 if (!addressW
&& !(addressW
= addr_to_str( saddr
))) return FALSE
;
1026 TRACE("connecting to %s:%u\n", debugstr_w(addressW
), port
);
1028 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER
, addressW
, 0 );
1030 if (!netconn_create( &request
->netconn
, saddr
->sa_family
, SOCK_STREAM
, 0 ))
1032 heap_free( addressW
);
1035 netconn_set_timeout( &request
->netconn
, TRUE
, request
->send_timeout
);
1036 netconn_set_timeout( &request
->netconn
, FALSE
, request
->recv_timeout
);
1037 if (!netconn_connect( &request
->netconn
, saddr
, slen
, request
->connect_timeout
))
1039 netconn_close( &request
->netconn
);
1040 heap_free( addressW
);
1043 if (request
->hdr
.flags
& WINHTTP_FLAG_SECURE
)
1045 if (connect
->session
->proxy_server
&&
1046 strcmpiW( connect
->hostname
, connect
->servername
))
1048 if (!secure_proxy_connect( request
))
1050 heap_free( addressW
);
1054 if (!netconn_secure_connect( &request
->netconn
, connect
->servername
))
1056 netconn_close( &request
->netconn
);
1057 heap_free( addressW
);
1062 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER
, addressW
, strlenW(addressW
) + 1 );
1065 request
->read_pos
= request
->read_size
= 0;
1066 request
->read_chunked
= FALSE
;
1067 request
->read_chunked_size
= ~0u;
1068 request
->read_chunked_eof
= FALSE
;
1069 heap_free( addressW
);
1073 void close_connection( request_t
*request
)
1075 if (!netconn_connected( &request
->netconn
)) return;
1077 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION
, 0, 0 );
1078 netconn_close( &request
->netconn
);
1079 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED
, 0, 0 );
1082 static BOOL
add_host_header( request_t
*request
, DWORD modifier
)
1087 static const WCHAR fmt
[] = {'%','s',':','%','u',0};
1088 connect_t
*connect
= request
->connect
;
1091 port
= connect
->hostport
? connect
->hostport
: (request
->hdr
.flags
& WINHTTP_FLAG_SECURE
? 443 : 80);
1093 if (port
== INTERNET_DEFAULT_HTTP_PORT
|| port
== INTERNET_DEFAULT_HTTPS_PORT
)
1095 return process_header( request
, attr_host
, connect
->hostname
, modifier
, TRUE
);
1097 len
= strlenW( connect
->hostname
) + 7; /* sizeof(":65335") */
1098 if (!(host
= heap_alloc( len
* sizeof(WCHAR
) ))) return FALSE
;
1099 sprintfW( host
, fmt
, connect
->hostname
, port
);
1100 ret
= process_header( request
, attr_host
, host
, modifier
, TRUE
);
1105 static void clear_response_headers( request_t
*request
)
1109 for (i
= 0; i
< request
->num_headers
; i
++)
1111 if (!request
->headers
[i
].field
) continue;
1112 if (!request
->headers
[i
].value
) continue;
1113 if (request
->headers
[i
].is_request
) continue;
1114 delete_header( request
, i
);
1119 static BOOL
send_request( request_t
*request
, LPCWSTR headers
, DWORD headers_len
, LPVOID optional
,
1120 DWORD optional_len
, DWORD total_len
, DWORD_PTR context
, BOOL async
)
1122 static const WCHAR keep_alive
[] = {'K','e','e','p','-','A','l','i','v','e',0};
1123 static const WCHAR no_cache
[] = {'n','o','-','c','a','c','h','e',0};
1124 static const WCHAR length_fmt
[] = {'%','l','d',0};
1127 connect_t
*connect
= request
->connect
;
1128 session_t
*session
= connect
->session
;
1132 DWORD len
, i
, flags
;
1134 clear_response_headers( request
);
1136 flags
= WINHTTP_ADDREQ_FLAG_ADD
|WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA
;
1137 for (i
= 0; i
< request
->num_accept_types
; i
++)
1139 process_header( request
, attr_accept
, request
->accept_types
[i
], flags
, TRUE
);
1142 process_header( request
, attr_user_agent
, session
->agent
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
1144 if (connect
->hostname
)
1145 add_host_header( request
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
);
1147 if (total_len
|| (request
->verb
&& !strcmpW( request
->verb
, postW
)))
1149 WCHAR length
[21]; /* decimal long int + null */
1150 sprintfW( length
, length_fmt
, total_len
);
1151 process_header( request
, attr_content_length
, length
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
1153 if (!(request
->hdr
.disable_flags
& WINHTTP_DISABLE_KEEP_ALIVE
))
1155 process_header( request
, attr_connection
, keep_alive
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
1157 if (request
->hdr
.flags
& WINHTTP_FLAG_REFRESH
)
1159 process_header( request
, attr_pragma
, no_cache
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
1160 process_header( request
, attr_cache_control
, no_cache
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
1162 if (headers
&& !add_request_headers( request
, headers
, headers_len
, WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
))
1164 TRACE("failed to add request headers\n");
1167 if (!(request
->hdr
.disable_flags
& WINHTTP_DISABLE_COOKIES
) && !add_cookie_headers( request
))
1169 WARN("failed to add cookie headers\n");
1173 if (context
) request
->hdr
.context
= context
;
1175 if (!(ret
= open_connection( request
))) goto end
;
1176 if (!(req
= build_request_string( request
))) goto end
;
1178 if (!(req_ascii
= strdupWA( req
))) goto end
;
1179 TRACE("full request: %s\n", debugstr_a(req_ascii
));
1180 len
= strlen(req_ascii
);
1182 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_SENDING_REQUEST
, NULL
, 0 );
1184 ret
= netconn_send( &request
->netconn
, req_ascii
, len
, &bytes_sent
);
1185 heap_free( req_ascii
);
1190 if (!netconn_send( &request
->netconn
, optional
, optional_len
, &bytes_sent
)) goto end
;
1191 request
->optional
= optional
;
1192 request
->optional_len
= optional_len
;
1193 len
+= optional_len
;
1195 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_REQUEST_SENT
, &len
, sizeof(len
) );
1200 if (ret
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE
, NULL
, 0 );
1203 WINHTTP_ASYNC_RESULT result
;
1204 result
.dwResult
= API_SEND_REQUEST
;
1205 result
.dwError
= get_last_error();
1206 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_REQUEST_ERROR
, &result
, sizeof(result
) );
1213 static void task_send_request( task_header_t
*task
)
1215 send_request_t
*s
= (send_request_t
*)task
;
1216 send_request( s
->hdr
.request
, s
->headers
, s
->headers_len
, s
->optional
, s
->optional_len
, s
->total_len
, s
->context
, TRUE
);
1217 heap_free( s
->headers
);
1220 /***********************************************************************
1221 * WinHttpSendRequest (winhttp.@)
1223 BOOL WINAPI
WinHttpSendRequest( HINTERNET hrequest
, LPCWSTR headers
, DWORD headers_len
,
1224 LPVOID optional
, DWORD optional_len
, DWORD total_len
, DWORD_PTR context
)
1229 TRACE("%p, %s, 0x%x, %u, %u, %lx\n",
1230 hrequest
, debugstr_w(headers
), headers_len
, optional_len
, total_len
, context
);
1232 if (!(request
= (request_t
*)grab_object( hrequest
)))
1234 set_last_error( ERROR_INVALID_HANDLE
);
1237 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
1239 release_object( &request
->hdr
);
1240 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1244 if (headers
&& !headers_len
) headers_len
= strlenW( headers
);
1246 if (request
->connect
->hdr
.flags
& WINHTTP_FLAG_ASYNC
)
1250 if (!(s
= heap_alloc( sizeof(send_request_t
) ))) return FALSE
;
1251 s
->hdr
.request
= request
;
1252 s
->hdr
.proc
= task_send_request
;
1253 s
->headers
= strdupW( headers
);
1254 s
->headers_len
= headers_len
;
1255 s
->optional
= optional
;
1256 s
->optional_len
= optional_len
;
1257 s
->total_len
= total_len
;
1258 s
->context
= context
;
1260 addref_object( &request
->hdr
);
1261 ret
= queue_task( (task_header_t
*)s
);
1264 ret
= send_request( request
, headers
, headers_len
, optional
, optional_len
, total_len
, context
, FALSE
);
1266 release_object( &request
->hdr
);
1267 if (ret
) set_last_error( ERROR_SUCCESS
);
1271 #define ARRAYSIZE(array) (sizeof(array) / sizeof((array)[0]))
1273 static const WCHAR basicW
[] = {'B','a','s','i','c',0};
1274 static const WCHAR ntlmW
[] = {'N','T','L','M',0};
1275 static const WCHAR passportW
[] = {'P','a','s','s','p','o','r','t',0};
1276 static const WCHAR digestW
[] = {'D','i','g','e','s','t',0};
1277 static const WCHAR negotiateW
[] = {'N','e','g','o','t','i','a','t','e',0};
1287 { basicW
, ARRAYSIZE(basicW
) - 1, WINHTTP_AUTH_SCHEME_BASIC
},
1288 { ntlmW
, ARRAYSIZE(ntlmW
) - 1, WINHTTP_AUTH_SCHEME_NTLM
},
1289 { passportW
, ARRAYSIZE(passportW
) - 1, WINHTTP_AUTH_SCHEME_PASSPORT
},
1290 { digestW
, ARRAYSIZE(digestW
) - 1, WINHTTP_AUTH_SCHEME_DIGEST
},
1291 { negotiateW
, ARRAYSIZE(negotiateW
) - 1, WINHTTP_AUTH_SCHEME_NEGOTIATE
}
1293 static const unsigned int num_auth_schemes
= sizeof(auth_schemes
)/sizeof(auth_schemes
[0]);
1295 static enum auth_scheme
scheme_from_flag( DWORD flag
)
1299 for (i
= 0; i
< num_auth_schemes
; i
++) if (flag
== auth_schemes
[i
].scheme
) return i
;
1300 return SCHEME_INVALID
;
1303 static DWORD
auth_scheme_from_header( WCHAR
*header
)
1307 for (i
= 0; i
< num_auth_schemes
; i
++)
1309 if (!strncmpiW( header
, auth_schemes
[i
].str
, auth_schemes
[i
].len
) &&
1310 (header
[auth_schemes
[i
].len
] == ' ' || !header
[auth_schemes
[i
].len
])) return auth_schemes
[i
].scheme
;
1315 static BOOL
query_auth_schemes( request_t
*request
, DWORD level
, LPDWORD supported
, LPDWORD first
)
1326 query_headers( request
, level
, NULL
, NULL
, &size
, &index
);
1327 if (get_last_error() != ERROR_INSUFFICIENT_BUFFER
) break;
1330 if (!(buffer
= heap_alloc( size
))) return FALSE
;
1331 if (!query_headers( request
, level
, NULL
, buffer
, &size
, &index
))
1333 heap_free( buffer
);
1336 scheme
= auth_scheme_from_header( buffer
);
1337 heap_free( buffer
);
1340 if (first
&& index
== 1)
1341 *first
= *supported
= scheme
;
1343 *supported
|= scheme
;
1350 /***********************************************************************
1351 * WinHttpQueryAuthSchemes (winhttp.@)
1353 BOOL WINAPI
WinHttpQueryAuthSchemes( HINTERNET hrequest
, LPDWORD supported
, LPDWORD first
, LPDWORD target
)
1358 TRACE("%p, %p, %p, %p\n", hrequest
, supported
, first
, target
);
1360 if (!(request
= (request_t
*)grab_object( hrequest
)))
1362 set_last_error( ERROR_INVALID_HANDLE
);
1365 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
1367 release_object( &request
->hdr
);
1368 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1371 if (!supported
|| !first
|| !target
)
1373 release_object( &request
->hdr
);
1374 set_last_error( ERROR_INVALID_PARAMETER
);
1379 if (query_auth_schemes( request
, WINHTTP_QUERY_WWW_AUTHENTICATE
, supported
, first
))
1381 *target
= WINHTTP_AUTH_TARGET_SERVER
;
1384 else if (query_auth_schemes( request
, WINHTTP_QUERY_PROXY_AUTHENTICATE
, supported
, first
))
1386 *target
= WINHTTP_AUTH_TARGET_PROXY
;
1390 release_object( &request
->hdr
);
1391 if (ret
) set_last_error( ERROR_SUCCESS
);
1395 static UINT
encode_base64( const char *bin
, unsigned int len
, WCHAR
*base64
)
1398 static const char base64enc
[] =
1399 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1403 /* first 6 bits, all from bin[0] */
1404 base64
[n
++] = base64enc
[(bin
[0] & 0xfc) >> 2];
1405 x
= (bin
[0] & 3) << 4;
1407 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1410 base64
[n
++] = base64enc
[x
];
1415 base64
[n
++] = base64enc
[x
| ((bin
[1] & 0xf0) >> 4)];
1416 x
= (bin
[1] & 0x0f) << 2;
1418 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1421 base64
[n
++] = base64enc
[x
];
1425 base64
[n
++] = base64enc
[x
| ((bin
[2] & 0xc0) >> 6)];
1427 /* last 6 bits, all from bin [2] */
1428 base64
[n
++] = base64enc
[bin
[2] & 0x3f];
1436 static inline char decode_char( WCHAR c
)
1438 if (c
>= 'A' && c
<= 'Z') return c
- 'A';
1439 if (c
>= 'a' && c
<= 'z') return c
- 'a' + 26;
1440 if (c
>= '0' && c
<= '9') return c
- '0' + 52;
1441 if (c
== '+') return 62;
1442 if (c
== '/') return 63;
1446 static unsigned int decode_base64( const WCHAR
*base64
, unsigned int len
, char *buf
)
1449 char c0
, c1
, c2
, c3
;
1450 const WCHAR
*p
= base64
;
1454 if ((c0
= decode_char( p
[0] )) > 63) return 0;
1455 if ((c1
= decode_char( p
[1] )) > 63) return 0;
1456 if ((c2
= decode_char( p
[2] )) > 63) return 0;
1457 if ((c3
= decode_char( p
[3] )) > 63) return 0;
1461 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
1462 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
1463 buf
[i
+ 2] = (c2
<< 6) | c3
;
1471 if ((c0
= decode_char( p
[0] )) > 63) return 0;
1472 if ((c1
= decode_char( p
[1] )) > 63) return 0;
1474 if (buf
) buf
[i
] = (c0
<< 2) | (c1
>> 4);
1477 else if (p
[3] == '=')
1479 if ((c0
= decode_char( p
[0] )) > 63) return 0;
1480 if ((c1
= decode_char( p
[1] )) > 63) return 0;
1481 if ((c2
= decode_char( p
[2] )) > 63) return 0;
1485 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
1486 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
1492 if ((c0
= decode_char( p
[0] )) > 63) return 0;
1493 if ((c1
= decode_char( p
[1] )) > 63) return 0;
1494 if ((c2
= decode_char( p
[2] )) > 63) return 0;
1495 if ((c3
= decode_char( p
[3] )) > 63) return 0;
1499 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
1500 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
1501 buf
[i
+ 2] = (c2
<< 6) | c3
;
1508 static struct authinfo
*alloc_authinfo(void)
1510 struct authinfo
*ret
;
1512 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
1514 SecInvalidateHandle( &ret
->cred
);
1515 SecInvalidateHandle( &ret
->ctx
);
1516 memset( &ret
->exp
, 0, sizeof(ret
->exp
) );
1522 ret
->finished
= FALSE
;
1526 void destroy_authinfo( struct authinfo
*authinfo
)
1528 if (!authinfo
) return;
1530 if (SecIsValidHandle( &authinfo
->ctx
))
1531 DeleteSecurityContext( &authinfo
->ctx
);
1532 if (SecIsValidHandle( &authinfo
->cred
))
1533 FreeCredentialsHandle( &authinfo
->cred
);
1535 heap_free( authinfo
->data
);
1536 heap_free( authinfo
);
1539 static BOOL
get_authvalue( request_t
*request
, DWORD level
, DWORD scheme
, WCHAR
*buffer
, DWORD len
)
1541 DWORD size
, index
= 0;
1545 if (!query_headers( request
, level
, NULL
, buffer
, &size
, &index
)) return FALSE
;
1546 if (auth_scheme_from_header( buffer
) == scheme
) break;
1551 static BOOL
do_authorization( request_t
*request
, DWORD target
, DWORD scheme_flag
)
1553 struct authinfo
*authinfo
, **auth_ptr
;
1554 enum auth_scheme scheme
= scheme_from_flag( scheme_flag
);
1555 const WCHAR
*auth_target
, *username
, *password
;
1556 WCHAR auth_value
[2048], *auth_reply
;
1557 DWORD len
= sizeof(auth_value
), len_scheme
, flags
;
1560 if (scheme
== SCHEME_INVALID
) return FALSE
;
1564 case WINHTTP_AUTH_TARGET_SERVER
:
1565 if (!get_authvalue( request
, WINHTTP_QUERY_WWW_AUTHENTICATE
, scheme_flag
, auth_value
, len
))
1567 auth_ptr
= &request
->authinfo
;
1568 auth_target
= attr_authorization
;
1569 if (request
->creds
[TARGET_SERVER
][scheme
].username
)
1571 username
= request
->creds
[TARGET_SERVER
][scheme
].username
;
1572 password
= request
->creds
[TARGET_SERVER
][scheme
].password
;
1576 username
= request
->connect
->username
;
1577 password
= request
->connect
->password
;
1581 case WINHTTP_AUTH_TARGET_PROXY
:
1582 if (!get_authvalue( request
, WINHTTP_QUERY_PROXY_AUTHENTICATE
, scheme_flag
, auth_value
, len
))
1584 auth_ptr
= &request
->proxy_authinfo
;
1585 auth_target
= attr_proxy_authorization
;
1586 if (request
->creds
[TARGET_PROXY
][scheme
].username
)
1588 username
= request
->creds
[TARGET_PROXY
][scheme
].username
;
1589 password
= request
->creds
[TARGET_PROXY
][scheme
].password
;
1593 username
= request
->connect
->session
->proxy_username
;
1594 password
= request
->connect
->session
->proxy_password
;
1599 WARN("unknown target %x\n", target
);
1602 authinfo
= *auth_ptr
;
1608 int userlen
, passlen
;
1610 if (!username
|| !password
) return FALSE
;
1611 if ((!authinfo
&& !(authinfo
= alloc_authinfo())) || authinfo
->finished
) return FALSE
;
1613 userlen
= WideCharToMultiByte( CP_UTF8
, 0, username
, strlenW( username
), NULL
, 0, NULL
, NULL
);
1614 passlen
= WideCharToMultiByte( CP_UTF8
, 0, password
, strlenW( password
), NULL
, 0, NULL
, NULL
);
1616 authinfo
->data_len
= userlen
+ 1 + passlen
;
1617 if (!(authinfo
->data
= heap_alloc( authinfo
->data_len
))) return FALSE
;
1619 WideCharToMultiByte( CP_UTF8
, 0, username
, -1, authinfo
->data
, userlen
, NULL
, NULL
);
1620 authinfo
->data
[userlen
] = ':';
1621 WideCharToMultiByte( CP_UTF8
, 0, password
, -1, authinfo
->data
+ userlen
+ 1, passlen
, NULL
, NULL
);
1623 authinfo
->scheme
= SCHEME_BASIC
;
1624 authinfo
->finished
= TRUE
;
1628 case SCHEME_NEGOTIATE
:
1630 SECURITY_STATUS status
;
1631 SecBufferDesc out_desc
, in_desc
;
1633 ULONG flags
= ISC_REQ_CONNECTION
|ISC_REQ_USE_DCE_STYLE
|ISC_REQ_MUTUAL_AUTH
|ISC_REQ_DELEGATE
;
1640 SEC_WINNT_AUTH_IDENTITY_W id
;
1641 WCHAR
*domain
, *user
;
1643 if (!username
|| !password
|| !(authinfo
= alloc_authinfo())) return FALSE
;
1646 domain
= (WCHAR
*)username
;
1647 user
= strchrW( username
, '\\' );
1652 user
= (WCHAR
*)username
;
1655 id
.Flags
= SEC_WINNT_AUTH_IDENTITY_UNICODE
;
1657 id
.UserLength
= strlenW( user
);
1659 id
.DomainLength
= domain
? user
- domain
- 1 : 0;
1660 id
.Password
= (WCHAR
*)password
;
1661 id
.PasswordLength
= strlenW( password
);
1663 status
= AcquireCredentialsHandleW( NULL
, (SEC_WCHAR
*)auth_schemes
[scheme
].str
,
1664 SECPKG_CRED_OUTBOUND
, NULL
, &id
, NULL
, NULL
,
1665 &authinfo
->cred
, &exp
);
1666 if (status
== SEC_E_OK
)
1669 status
= QuerySecurityPackageInfoW( (SEC_WCHAR
*)auth_schemes
[scheme
].str
, &info
);
1670 if (status
== SEC_E_OK
)
1672 authinfo
->max_token
= info
->cbMaxToken
;
1673 FreeContextBuffer( info
);
1676 if (status
!= SEC_E_OK
)
1678 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
1679 debugstr_w(auth_schemes
[scheme
].str
), status
);
1680 heap_free( authinfo
);
1683 authinfo
->scheme
= scheme
;
1685 else if (authinfo
->finished
) return FALSE
;
1687 if ((strlenW( auth_value
) < auth_schemes
[authinfo
->scheme
].len
||
1688 strncmpiW( auth_value
, auth_schemes
[authinfo
->scheme
].str
, auth_schemes
[authinfo
->scheme
].len
)))
1690 ERR("authentication scheme changed from %s to %s\n",
1691 debugstr_w(auth_schemes
[authinfo
->scheme
].str
), debugstr_w(auth_value
));
1692 destroy_authinfo( authinfo
);
1696 in
.BufferType
= SECBUFFER_TOKEN
;
1700 in_desc
.ulVersion
= 0;
1701 in_desc
.cBuffers
= 1;
1702 in_desc
.pBuffers
= &in
;
1704 p
= auth_value
+ auth_schemes
[scheme
].len
;
1707 int len
= strlenW( ++p
);
1708 in
.cbBuffer
= decode_base64( p
, len
, NULL
);
1709 if (!(in
.pvBuffer
= heap_alloc( in
.cbBuffer
))) {
1710 destroy_authinfo( authinfo
);
1714 decode_base64( p
, len
, in
.pvBuffer
);
1716 out
.BufferType
= SECBUFFER_TOKEN
;
1717 out
.cbBuffer
= authinfo
->max_token
;
1718 if (!(out
.pvBuffer
= heap_alloc( authinfo
->max_token
)))
1720 heap_free( in
.pvBuffer
);
1721 destroy_authinfo( authinfo
);
1725 out_desc
.ulVersion
= 0;
1726 out_desc
.cBuffers
= 1;
1727 out_desc
.pBuffers
= &out
;
1729 status
= InitializeSecurityContextW( first
? &authinfo
->cred
: NULL
, first
? NULL
: &authinfo
->ctx
,
1730 first
? request
->connect
->servername
: NULL
, flags
, 0,
1731 SECURITY_NETWORK_DREP
, in
.pvBuffer
? &in_desc
: NULL
, 0,
1732 &authinfo
->ctx
, &out_desc
, &authinfo
->attr
, &authinfo
->exp
);
1733 heap_free( in
.pvBuffer
);
1734 if (status
== SEC_E_OK
)
1736 heap_free( authinfo
->data
);
1737 authinfo
->data
= out
.pvBuffer
;
1738 authinfo
->data_len
= out
.cbBuffer
;
1739 authinfo
->finished
= TRUE
;
1740 TRACE("sending last auth packet\n");
1742 else if (status
== SEC_I_CONTINUE_NEEDED
)
1744 heap_free( authinfo
->data
);
1745 authinfo
->data
= out
.pvBuffer
;
1746 authinfo
->data_len
= out
.cbBuffer
;
1747 TRACE("sending next auth packet\n");
1751 ERR("InitializeSecurityContextW failed with error 0x%08x\n", status
);
1752 heap_free( out
.pvBuffer
);
1753 destroy_authinfo( authinfo
);
1760 ERR("invalid scheme %u\n", scheme
);
1763 *auth_ptr
= authinfo
;
1765 len_scheme
= auth_schemes
[authinfo
->scheme
].len
;
1766 len
= len_scheme
+ 1 + ((authinfo
->data_len
+ 2) * 4) / 3;
1767 if (!(auth_reply
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) return FALSE
;
1769 memcpy( auth_reply
, auth_schemes
[authinfo
->scheme
].str
, len_scheme
* sizeof(WCHAR
) );
1770 auth_reply
[len_scheme
] = ' ';
1771 encode_base64( authinfo
->data
, authinfo
->data_len
, auth_reply
+ len_scheme
+ 1 );
1773 flags
= WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
;
1774 ret
= process_header( request
, auth_target
, auth_reply
, flags
, TRUE
);
1775 heap_free( auth_reply
);
1779 static BOOL
set_credentials( request_t
*request
, DWORD target
, DWORD scheme_flag
, const WCHAR
*username
,
1780 const WCHAR
*password
)
1782 enum auth_scheme scheme
= scheme_from_flag( scheme_flag
);
1784 if (scheme
== SCHEME_INVALID
|| ((scheme
== SCHEME_BASIC
|| scheme
== SCHEME_DIGEST
) && (!username
|| !password
)))
1786 set_last_error( ERROR_INVALID_PARAMETER
);
1791 case WINHTTP_AUTH_TARGET_SERVER
:
1793 heap_free( request
->creds
[TARGET_SERVER
][scheme
].username
);
1794 if (!username
) request
->creds
[TARGET_SERVER
][scheme
].username
= NULL
;
1795 else if (!(request
->creds
[TARGET_SERVER
][scheme
].username
= strdupW( username
))) return FALSE
;
1797 heap_free( request
->creds
[TARGET_SERVER
][scheme
].password
);
1798 if (!password
) request
->creds
[TARGET_SERVER
][scheme
].password
= NULL
;
1799 else if (!(request
->creds
[TARGET_SERVER
][scheme
].password
= strdupW( password
))) return FALSE
;
1802 case WINHTTP_AUTH_TARGET_PROXY
:
1804 heap_free( request
->creds
[TARGET_PROXY
][scheme
].username
);
1805 if (!username
) request
->creds
[TARGET_PROXY
][scheme
].username
= NULL
;
1806 else if (!(request
->creds
[TARGET_PROXY
][scheme
].username
= strdupW( username
))) return FALSE
;
1808 heap_free( request
->creds
[TARGET_PROXY
][scheme
].password
);
1809 if (!password
) request
->creds
[TARGET_PROXY
][scheme
].password
= NULL
;
1810 else if (!(request
->creds
[TARGET_PROXY
][scheme
].password
= strdupW( password
))) return FALSE
;
1814 WARN("unknown target %u\n", target
);
1820 /***********************************************************************
1821 * WinHttpSetCredentials (winhttp.@)
1823 BOOL WINAPI
WinHttpSetCredentials( HINTERNET hrequest
, DWORD target
, DWORD scheme
, LPCWSTR username
,
1824 LPCWSTR password
, LPVOID params
)
1829 TRACE("%p, %x, 0x%08x, %s, %p, %p\n", hrequest
, target
, scheme
, debugstr_w(username
), password
, params
);
1831 if (!(request
= (request_t
*)grab_object( hrequest
)))
1833 set_last_error( ERROR_INVALID_HANDLE
);
1836 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
1838 release_object( &request
->hdr
);
1839 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1843 ret
= set_credentials( request
, target
, scheme
, username
, password
);
1845 release_object( &request
->hdr
);
1846 if (ret
) set_last_error( ERROR_SUCCESS
);
1850 static BOOL
handle_authorization( request_t
*request
, DWORD status
)
1852 DWORD i
, schemes
, first
, level
, target
;
1856 case HTTP_STATUS_DENIED
:
1857 target
= WINHTTP_AUTH_TARGET_SERVER
;
1858 level
= WINHTTP_QUERY_WWW_AUTHENTICATE
;
1861 case HTTP_STATUS_PROXY_AUTH_REQ
:
1862 target
= WINHTTP_AUTH_TARGET_PROXY
;
1863 level
= WINHTTP_QUERY_PROXY_AUTHENTICATE
;
1867 WARN("unhandled status %u\n", status
);
1871 if (!query_auth_schemes( request
, level
, &schemes
, &first
)) return FALSE
;
1872 if (do_authorization( request
, target
, first
)) return TRUE
;
1875 for (i
= 0; i
< num_auth_schemes
; i
++)
1877 if (!(schemes
& auth_schemes
[i
].scheme
)) continue;
1878 if (do_authorization( request
, target
, auth_schemes
[i
].scheme
)) return TRUE
;
1883 /* set the request content length based on the headers */
1884 static DWORD
set_content_length( request_t
*request
, DWORD status
)
1887 DWORD buflen
= sizeof(request
->content_length
);
1889 if (status
== HTTP_STATUS_NO_CONTENT
|| status
== HTTP_STATUS_NOT_MODIFIED
|| !strcmpW( request
->verb
, headW
))
1890 request
->content_length
= 0;
1893 if (!query_headers( request
, WINHTTP_QUERY_CONTENT_LENGTH
|WINHTTP_QUERY_FLAG_NUMBER
,
1894 NULL
, &request
->content_length
, &buflen
, NULL
))
1895 request
->content_length
= ~0u;
1897 buflen
= sizeof(encoding
);
1898 if (query_headers( request
, WINHTTP_QUERY_TRANSFER_ENCODING
, NULL
, encoding
, &buflen
, NULL
) &&
1899 !strcmpiW( encoding
, chunkedW
))
1901 request
->content_length
= ~0u;
1902 request
->read_chunked
= TRUE
;
1903 request
->read_chunked_size
= ~0u;
1904 request
->read_chunked_eof
= FALSE
;
1907 request
->content_read
= 0;
1908 return request
->content_length
;
1911 /* read some more data into the read buffer */
1912 static BOOL
read_more_data( request_t
*request
, int maxlen
, BOOL notify
)
1917 if (request
->read_chunked_eof
) return FALSE
;
1919 if (request
->read_size
&& request
->read_pos
)
1921 /* move existing data to the start of the buffer */
1922 memmove( request
->read_buf
, request
->read_buf
+ request
->read_pos
, request
->read_size
);
1923 request
->read_pos
= 0;
1925 if (maxlen
== -1) maxlen
= sizeof(request
->read_buf
);
1927 if (notify
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE
, NULL
, 0 );
1929 ret
= netconn_recv( &request
->netconn
, request
->read_buf
+ request
->read_size
,
1930 maxlen
- request
->read_size
, 0, &len
);
1932 if (notify
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED
, &len
, sizeof(len
) );
1934 request
->read_size
+= len
;
1938 /* remove some amount of data from the read buffer */
1939 static void remove_data( request_t
*request
, int count
)
1941 if (!(request
->read_size
-= count
)) request
->read_pos
= 0;
1942 else request
->read_pos
+= count
;
1945 static BOOL
read_line( request_t
*request
, char *buffer
, DWORD
*len
)
1947 int count
, bytes_read
, pos
= 0;
1951 char *eol
= memchr( request
->read_buf
+ request
->read_pos
, '\n', request
->read_size
);
1954 count
= eol
- (request
->read_buf
+ request
->read_pos
);
1955 bytes_read
= count
+ 1;
1957 else count
= bytes_read
= request
->read_size
;
1959 count
= min( count
, *len
- pos
);
1960 memcpy( buffer
+ pos
, request
->read_buf
+ request
->read_pos
, count
);
1962 remove_data( request
, bytes_read
);
1965 if (!read_more_data( request
, -1, TRUE
)) return FALSE
;
1966 if (!request
->read_size
)
1969 TRACE("returning empty string\n");
1975 if (pos
&& buffer
[pos
- 1] == '\r') pos
--;
1978 buffer
[*len
- 1] = 0;
1979 TRACE("returning %s\n", debugstr_a(buffer
));
1983 /* discard data contents until we reach end of line */
1984 static BOOL
discard_eol( request_t
*request
, BOOL notify
)
1988 char *eol
= memchr( request
->read_buf
+ request
->read_pos
, '\n', request
->read_size
);
1991 remove_data( request
, (eol
+ 1) - (request
->read_buf
+ request
->read_pos
) );
1994 request
->read_pos
= request
->read_size
= 0; /* discard everything */
1995 if (!read_more_data( request
, -1, notify
)) return FALSE
;
1996 } while (request
->read_size
);
2000 /* read the size of the next chunk */
2001 static BOOL
start_next_chunk( request_t
*request
, BOOL notify
)
2003 DWORD chunk_size
= 0;
2005 assert(!request
->read_chunked_size
|| request
->read_chunked_size
== ~0u);
2007 if (request
->read_chunked_eof
) return FALSE
;
2009 /* read terminator for the previous chunk */
2010 if (!request
->read_chunked_size
&& !discard_eol( request
, notify
)) return FALSE
;
2014 while (request
->read_size
)
2016 char ch
= request
->read_buf
[request
->read_pos
];
2017 if (ch
>= '0' && ch
<= '9') chunk_size
= chunk_size
* 16 + ch
- '0';
2018 else if (ch
>= 'a' && ch
<= 'f') chunk_size
= chunk_size
* 16 + ch
- 'a' + 10;
2019 else if (ch
>= 'A' && ch
<= 'F') chunk_size
= chunk_size
* 16 + ch
- 'A' + 10;
2020 else if (ch
== ';' || ch
== '\r' || ch
== '\n')
2022 TRACE("reading %u byte chunk\n", chunk_size
);
2024 if (request
->content_length
== ~0u) request
->content_length
= chunk_size
;
2025 else request
->content_length
+= chunk_size
;
2027 request
->read_chunked_size
= chunk_size
;
2028 if (!chunk_size
) request
->read_chunked_eof
= TRUE
;
2030 return discard_eol( request
, notify
);
2032 remove_data( request
, 1 );
2034 if (!read_more_data( request
, -1, notify
)) return FALSE
;
2035 if (!request
->read_size
)
2037 request
->content_length
= request
->content_read
= 0;
2038 request
->read_chunked_size
= 0;
2044 /* return the size of data available to be read immediately */
2045 static DWORD
get_available_data( request_t
*request
)
2047 if (request
->read_chunked
) return min( request
->read_chunked_size
, request
->read_size
);
2048 return request
->read_size
;
2051 /* check if we have reached the end of the data to read */
2052 static BOOL
end_of_read_data( request_t
*request
)
2054 if (!request
->content_length
) return TRUE
;
2055 if (request
->read_chunked
) return request
->read_chunked_eof
;
2056 if (request
->content_length
== ~0u) return FALSE
;
2057 return (request
->content_length
== request
->content_read
);
2060 static BOOL
refill_buffer( request_t
*request
, BOOL notify
)
2062 int len
= sizeof(request
->read_buf
);
2064 if (request
->read_chunked
)
2066 if (request
->read_chunked_eof
) return FALSE
;
2067 if (request
->read_chunked_size
== ~0u || !request
->read_chunked_size
)
2069 if (!start_next_chunk( request
, notify
)) return FALSE
;
2071 len
= min( len
, request
->read_chunked_size
);
2073 else if (request
->content_length
!= ~0u)
2075 len
= min( len
, request
->content_length
- request
->content_read
);
2078 if (len
<= request
->read_size
) return TRUE
;
2079 if (!read_more_data( request
, len
, notify
)) return FALSE
;
2080 if (!request
->read_size
) request
->content_length
= request
->content_read
= 0;
2084 #define MAX_REPLY_LEN 1460
2085 #define INITIAL_HEADER_BUFFER_LEN 512
2087 static BOOL
read_reply( request_t
*request
)
2089 static const WCHAR crlf
[] = {'\r','\n',0};
2091 char buffer
[MAX_REPLY_LEN
];
2092 DWORD buflen
, len
, offset
, crlf_len
= 2; /* strlenW(crlf) */
2093 char *status_code
, *status_text
;
2094 WCHAR
*versionW
, *status_textW
, *raw_headers
;
2095 WCHAR status_codeW
[4]; /* sizeof("nnn") */
2097 if (!netconn_connected( &request
->netconn
)) return FALSE
;
2101 buflen
= MAX_REPLY_LEN
;
2102 if (!read_line( request
, buffer
, &buflen
)) return FALSE
;
2104 /* first line should look like 'HTTP/1.x nnn OK' where nnn is the status code */
2105 if (!(status_code
= strchr( buffer
, ' ' ))) return FALSE
;
2107 if (!(status_text
= strchr( status_code
, ' ' ))) return FALSE
;
2108 if ((len
= status_text
- status_code
) != sizeof("nnn") - 1) return FALSE
;
2111 TRACE("version [%s] status code [%s] status text [%s]\n",
2112 debugstr_an(buffer
, status_code
- buffer
- 1),
2113 debugstr_an(status_code
, len
),
2114 debugstr_a(status_text
));
2116 } while (!memcmp( status_code
, "100", len
)); /* ignore "100 Continue" responses */
2118 /* we rely on the fact that the protocol is ascii */
2119 MultiByteToWideChar( CP_ACP
, 0, status_code
, len
, status_codeW
, len
);
2120 status_codeW
[len
] = 0;
2121 if (!(process_header( request
, attr_status
, status_codeW
,
2122 WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
, FALSE
)))
2125 len
= status_code
- buffer
;
2126 if (!(versionW
= heap_alloc( len
* sizeof(WCHAR
) ))) return FALSE
;
2127 MultiByteToWideChar( CP_ACP
, 0, buffer
, len
- 1, versionW
, len
-1 );
2128 versionW
[len
- 1] = 0;
2130 heap_free( request
->version
);
2131 request
->version
= versionW
;
2133 len
= buflen
- (status_text
- buffer
);
2134 if (!(status_textW
= heap_alloc( len
* sizeof(WCHAR
) ))) return FALSE
;
2135 MultiByteToWideChar( CP_ACP
, 0, status_text
, len
, status_textW
, len
);
2137 heap_free( request
->status_text
);
2138 request
->status_text
= status_textW
;
2140 len
= max( buflen
+ crlf_len
, INITIAL_HEADER_BUFFER_LEN
);
2141 if (!(raw_headers
= heap_alloc( len
* sizeof(WCHAR
) ))) return FALSE
;
2142 MultiByteToWideChar( CP_ACP
, 0, buffer
, buflen
, raw_headers
, buflen
);
2143 memcpy( raw_headers
+ buflen
- 1, crlf
, sizeof(crlf
) );
2145 heap_free( request
->raw_headers
);
2146 request
->raw_headers
= raw_headers
;
2148 offset
= buflen
+ crlf_len
- 1;
2153 buflen
= MAX_REPLY_LEN
;
2154 if (!read_line( request
, buffer
, &buflen
)) return TRUE
;
2155 if (!*buffer
) buflen
= 1;
2157 while (len
- offset
< buflen
+ crlf_len
)
2161 if (!(tmp
= heap_realloc( raw_headers
, len
* sizeof(WCHAR
) ))) return FALSE
;
2162 request
->raw_headers
= raw_headers
= tmp
;
2166 memcpy( raw_headers
+ offset
, crlf
, sizeof(crlf
) );
2169 MultiByteToWideChar( CP_ACP
, 0, buffer
, buflen
, raw_headers
+ offset
, buflen
);
2171 if (!(header
= parse_header( raw_headers
+ offset
))) break;
2172 if (!(process_header( request
, header
->field
, header
->value
, WINHTTP_ADDREQ_FLAG_ADD
, FALSE
)))
2174 free_header( header
);
2177 free_header( header
);
2178 memcpy( raw_headers
+ offset
+ buflen
- 1, crlf
, sizeof(crlf
) );
2179 offset
+= buflen
+ crlf_len
- 1;
2182 TRACE("raw headers: %s\n", debugstr_w(raw_headers
));
2186 static void finished_reading( request_t
*request
)
2188 static const WCHAR closeW
[] = {'c','l','o','s','e',0};
2191 WCHAR connection
[20];
2192 DWORD size
= sizeof(connection
);
2194 if (request
->hdr
.disable_flags
& WINHTTP_DISABLE_KEEP_ALIVE
) close
= TRUE
;
2195 else if (query_headers( request
, WINHTTP_QUERY_CONNECTION
, NULL
, connection
, &size
, NULL
) ||
2196 query_headers( request
, WINHTTP_QUERY_PROXY_CONNECTION
, NULL
, connection
, &size
, NULL
))
2198 if (!strcmpiW( connection
, closeW
)) close
= TRUE
;
2200 else if (!strcmpW( request
->version
, http1_0
)) close
= TRUE
;
2201 if (close
) close_connection( request
);
2204 static BOOL
read_data( request_t
*request
, void *buffer
, DWORD size
, DWORD
*read
, BOOL async
)
2206 int count
, bytes_read
= 0;
2208 if (end_of_read_data( request
)) goto done
;
2212 if (!(count
= get_available_data( request
)))
2214 if (!refill_buffer( request
, async
)) goto done
;
2215 if (!(count
= get_available_data( request
))) goto done
;
2217 count
= min( count
, size
);
2218 memcpy( (char *)buffer
+ bytes_read
, request
->read_buf
+ request
->read_pos
, count
);
2219 remove_data( request
, count
);
2220 if (request
->read_chunked
) request
->read_chunked_size
-= count
;
2222 bytes_read
+= count
;
2223 request
->content_read
+= count
;
2224 if (end_of_read_data( request
)) goto done
;
2226 if (request
->read_chunked
&& !request
->read_chunked_size
) refill_buffer( request
, async
);
2229 TRACE( "retrieved %u bytes (%u/%u)\n", bytes_read
, request
->content_read
, request
->content_length
);
2231 if (async
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_READ_COMPLETE
, buffer
, bytes_read
);
2232 if (read
) *read
= bytes_read
;
2233 if (end_of_read_data( request
)) finished_reading( request
);
2237 /* read any content returned by the server so that the connection can be reused */
2238 static void drain_content( request_t
*request
)
2243 refill_buffer( request
, FALSE
);
2246 if (!read_data( request
, buffer
, sizeof(buffer
), &bytes_read
, FALSE
) || !bytes_read
) return;
2250 static void record_cookies( request_t
*request
)
2254 for (i
= 0; i
< request
->num_headers
; i
++)
2256 header_t
*set_cookie
= &request
->headers
[i
];
2257 if (!strcmpiW( set_cookie
->field
, attr_set_cookie
) && !set_cookie
->is_request
)
2259 set_cookies( request
, set_cookie
->value
);
2264 static WCHAR
*get_redirect_url( request_t
*request
, DWORD
*len
)
2269 query_headers( request
, WINHTTP_QUERY_LOCATION
, NULL
, NULL
, &size
, NULL
);
2270 if (get_last_error() != ERROR_INSUFFICIENT_BUFFER
) return FALSE
;
2271 if (!(ret
= heap_alloc( size
))) return NULL
;
2272 *len
= size
/ sizeof(WCHAR
);
2273 if (query_headers( request
, WINHTTP_QUERY_LOCATION
, NULL
, ret
, &size
, NULL
)) return ret
;
2278 static BOOL
handle_redirect( request_t
*request
, DWORD status
)
2283 connect_t
*connect
= request
->connect
;
2285 WCHAR
*hostname
= NULL
, *location
;
2288 if (!(location
= get_redirect_url( request
, &len_url
))) return FALSE
;
2290 memset( &uc
, 0, sizeof(uc
) );
2291 uc
.dwStructSize
= sizeof(uc
);
2292 uc
.dwSchemeLength
= uc
.dwHostNameLength
= uc
.dwUrlPathLength
= uc
.dwExtraInfoLength
= ~0u;
2294 if (!WinHttpCrackUrl( location
, len_url
, 0, &uc
)) /* assume relative redirect */
2298 if (location
[0] == '/')
2300 len
= strlenW( location
);
2301 if (!(path
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto end
;
2302 strcpyW( path
, location
);
2306 if ((p
= strrchrW( request
->path
, '/' ))) *p
= 0;
2307 len
= strlenW( request
->path
) + 1 + strlenW( location
);
2308 if (!(path
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto end
;
2309 strcpyW( path
, request
->path
);
2310 strcatW( path
, slashW
);
2311 strcatW( path
, location
);
2313 heap_free( request
->path
);
2314 request
->path
= path
;
2316 drain_content( request
);
2317 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_REDIRECT
, location
, len_url
+ 1 );
2321 if (uc
.nScheme
== INTERNET_SCHEME_HTTP
&& request
->hdr
.flags
& WINHTTP_FLAG_SECURE
)
2323 if (request
->hdr
.redirect_policy
== WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP
) goto end
;
2324 TRACE("redirect from secure page to non-secure page\n");
2325 request
->hdr
.flags
&= ~WINHTTP_FLAG_SECURE
;
2327 else if (uc
.nScheme
== INTERNET_SCHEME_HTTPS
&& !(request
->hdr
.flags
& WINHTTP_FLAG_SECURE
))
2329 TRACE("redirect from non-secure page to secure page\n");
2330 request
->hdr
.flags
|= WINHTTP_FLAG_SECURE
;
2333 drain_content( request
);
2334 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_REDIRECT
, location
, len_url
+ 1 );
2336 len
= uc
.dwHostNameLength
;
2337 if (!(hostname
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto end
;
2338 memcpy( hostname
, uc
.lpszHostName
, len
* sizeof(WCHAR
) );
2341 port
= uc
.nPort
? uc
.nPort
: (uc
.nScheme
== INTERNET_SCHEME_HTTPS
? 443 : 80);
2342 if (strcmpiW( connect
->hostname
, hostname
) || connect
->serverport
!= port
)
2344 heap_free( connect
->hostname
);
2345 connect
->hostname
= hostname
;
2346 connect
->hostport
= port
;
2347 if (!(ret
= set_server_for_hostname( connect
, hostname
, port
))) goto end
;
2349 netconn_close( &request
->netconn
);
2350 if (!(ret
= netconn_init( &request
->netconn
))) goto end
;
2351 request
->read_pos
= request
->read_size
= 0;
2352 request
->read_chunked
= FALSE
;
2353 request
->read_chunked_eof
= FALSE
;
2355 else heap_free( hostname
);
2357 if (!(ret
= add_host_header( request
, WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
))) goto end
;
2358 if (!(ret
= open_connection( request
))) goto end
;
2360 heap_free( request
->path
);
2361 request
->path
= NULL
;
2362 if (uc
.dwUrlPathLength
)
2364 len
= uc
.dwUrlPathLength
+ uc
.dwExtraInfoLength
;
2365 if (!(request
->path
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto end
;
2366 strcpyW( request
->path
, uc
.lpszUrlPath
);
2368 else request
->path
= strdupW( slashW
);
2371 /* remove content-type/length headers */
2372 if ((index
= get_header_index( request
, attr_content_type
, 0, TRUE
)) >= 0) delete_header( request
, index
);
2373 if ((index
= get_header_index( request
, attr_content_length
, 0, TRUE
)) >= 0 ) delete_header( request
, index
);
2375 if (status
!= HTTP_STATUS_REDIRECT_KEEP_VERB
&& !strcmpW( request
->verb
, postW
))
2377 heap_free( request
->verb
);
2378 request
->verb
= strdupW( getW
);
2379 request
->optional
= NULL
;
2380 request
->optional_len
= 0;
2385 heap_free( location
);
2389 static BOOL
receive_response( request_t
*request
, BOOL async
)
2392 DWORD size
, query
, status
;
2396 if (!(ret
= read_reply( request
)))
2398 set_last_error( ERROR_WINHTTP_INVALID_SERVER_RESPONSE
);
2401 size
= sizeof(DWORD
);
2402 query
= WINHTTP_QUERY_STATUS_CODE
| WINHTTP_QUERY_FLAG_NUMBER
;
2403 if (!(ret
= query_headers( request
, query
, NULL
, &status
, &size
, NULL
))) break;
2405 set_content_length( request
, status
);
2407 if (!(request
->hdr
.disable_flags
& WINHTTP_DISABLE_COOKIES
)) record_cookies( request
);
2409 if (status
== HTTP_STATUS_MOVED
|| status
== HTTP_STATUS_REDIRECT
|| status
== HTTP_STATUS_REDIRECT_KEEP_VERB
)
2411 if (request
->hdr
.disable_flags
& WINHTTP_DISABLE_REDIRECTS
||
2412 request
->hdr
.redirect_policy
== WINHTTP_OPTION_REDIRECT_POLICY_NEVER
) break;
2414 if (!(ret
= handle_redirect( request
, status
))) break;
2416 /* recurse synchronously */
2417 if ((ret
= send_request( request
, NULL
, 0, request
->optional
, request
->optional_len
, 0, 0, FALSE
))) continue;
2419 else if (status
== HTTP_STATUS_DENIED
|| status
== HTTP_STATUS_PROXY_AUTH_REQ
)
2421 if (request
->hdr
.disable_flags
& WINHTTP_DISABLE_AUTHENTICATION
) break;
2423 drain_content( request
);
2424 if (!handle_authorization( request
, status
)) break;
2426 /* recurse synchronously */
2427 if ((ret
= send_request( request
, NULL
, 0, request
->optional
, request
->optional_len
, 0, 0, FALSE
))) continue;
2432 if (request
->content_length
) refill_buffer( request
, FALSE
);
2436 if (ret
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE
, NULL
, 0 );
2439 WINHTTP_ASYNC_RESULT result
;
2440 result
.dwResult
= API_RECEIVE_RESPONSE
;
2441 result
.dwError
= get_last_error();
2442 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_REQUEST_ERROR
, &result
, sizeof(result
) );
2448 static void task_receive_response( task_header_t
*task
)
2450 receive_response_t
*r
= (receive_response_t
*)task
;
2451 receive_response( r
->hdr
.request
, TRUE
);
2454 /***********************************************************************
2455 * WinHttpReceiveResponse (winhttp.@)
2457 BOOL WINAPI
WinHttpReceiveResponse( HINTERNET hrequest
, LPVOID reserved
)
2462 TRACE("%p, %p\n", hrequest
, reserved
);
2464 if (!(request
= (request_t
*)grab_object( hrequest
)))
2466 set_last_error( ERROR_INVALID_HANDLE
);
2469 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
2471 release_object( &request
->hdr
);
2472 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2476 if (request
->connect
->hdr
.flags
& WINHTTP_FLAG_ASYNC
)
2478 receive_response_t
*r
;
2480 if (!(r
= heap_alloc( sizeof(receive_response_t
) ))) return FALSE
;
2481 r
->hdr
.request
= request
;
2482 r
->hdr
.proc
= task_receive_response
;
2484 addref_object( &request
->hdr
);
2485 ret
= queue_task( (task_header_t
*)r
);
2488 ret
= receive_response( request
, FALSE
);
2490 release_object( &request
->hdr
);
2491 if (ret
) set_last_error( ERROR_SUCCESS
);
2495 static BOOL
query_data_available( request_t
*request
, DWORD
*available
, BOOL async
)
2499 if (end_of_read_data( request
)) goto done
;
2501 count
= get_available_data( request
);
2502 if (!request
->read_chunked
)
2503 count
+= netconn_query_data_available( &request
->netconn
);
2506 refill_buffer( request
, async
);
2507 count
= get_available_data( request
);
2508 if (!request
->read_chunked
)
2509 count
+= netconn_query_data_available( &request
->netconn
);
2513 if (async
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE
, &count
, sizeof(count
) );
2514 TRACE("%u bytes available\n", count
);
2515 if (available
) *available
= count
;
2519 static void task_query_data_available( task_header_t
*task
)
2521 query_data_t
*q
= (query_data_t
*)task
;
2522 query_data_available( q
->hdr
.request
, q
->available
, TRUE
);
2525 /***********************************************************************
2526 * WinHttpQueryDataAvailable (winhttp.@)
2528 BOOL WINAPI
WinHttpQueryDataAvailable( HINTERNET hrequest
, LPDWORD available
)
2533 TRACE("%p, %p\n", hrequest
, available
);
2535 if (!(request
= (request_t
*)grab_object( hrequest
)))
2537 set_last_error( ERROR_INVALID_HANDLE
);
2540 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
2542 release_object( &request
->hdr
);
2543 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2547 if (request
->connect
->hdr
.flags
& WINHTTP_FLAG_ASYNC
)
2551 if (!(q
= heap_alloc( sizeof(query_data_t
) ))) return FALSE
;
2552 q
->hdr
.request
= request
;
2553 q
->hdr
.proc
= task_query_data_available
;
2554 q
->available
= available
;
2556 addref_object( &request
->hdr
);
2557 ret
= queue_task( (task_header_t
*)q
);
2560 ret
= query_data_available( request
, available
, FALSE
);
2562 release_object( &request
->hdr
);
2563 if (ret
) set_last_error( ERROR_SUCCESS
);
2567 static void task_read_data( task_header_t
*task
)
2569 read_data_t
*r
= (read_data_t
*)task
;
2570 read_data( r
->hdr
.request
, r
->buffer
, r
->to_read
, r
->read
, TRUE
);
2573 /***********************************************************************
2574 * WinHttpReadData (winhttp.@)
2576 BOOL WINAPI
WinHttpReadData( HINTERNET hrequest
, LPVOID buffer
, DWORD to_read
, LPDWORD read
)
2581 TRACE("%p, %p, %d, %p\n", hrequest
, buffer
, to_read
, read
);
2583 if (!(request
= (request_t
*)grab_object( hrequest
)))
2585 set_last_error( ERROR_INVALID_HANDLE
);
2588 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
2590 release_object( &request
->hdr
);
2591 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2595 if (request
->connect
->hdr
.flags
& WINHTTP_FLAG_ASYNC
)
2599 if (!(r
= heap_alloc( sizeof(read_data_t
) ))) return FALSE
;
2600 r
->hdr
.request
= request
;
2601 r
->hdr
.proc
= task_read_data
;
2603 r
->to_read
= to_read
;
2606 addref_object( &request
->hdr
);
2607 ret
= queue_task( (task_header_t
*)r
);
2610 ret
= read_data( request
, buffer
, to_read
, read
, FALSE
);
2612 release_object( &request
->hdr
);
2613 if (ret
) set_last_error( ERROR_SUCCESS
);
2617 static BOOL
write_data( request_t
*request
, LPCVOID buffer
, DWORD to_write
, LPDWORD written
, BOOL async
)
2622 ret
= netconn_send( &request
->netconn
, buffer
, to_write
, &num_bytes
);
2626 if (ret
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE
, &num_bytes
, sizeof(num_bytes
) );
2629 WINHTTP_ASYNC_RESULT result
;
2630 result
.dwResult
= API_WRITE_DATA
;
2631 result
.dwError
= get_last_error();
2632 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_REQUEST_ERROR
, &result
, sizeof(result
) );
2635 if (ret
&& written
) *written
= num_bytes
;
2639 static void task_write_data( task_header_t
*task
)
2641 write_data_t
*w
= (write_data_t
*)task
;
2642 write_data( w
->hdr
.request
, w
->buffer
, w
->to_write
, w
->written
, TRUE
);
2645 /***********************************************************************
2646 * WinHttpWriteData (winhttp.@)
2648 BOOL WINAPI
WinHttpWriteData( HINTERNET hrequest
, LPCVOID buffer
, DWORD to_write
, LPDWORD written
)
2653 TRACE("%p, %p, %d, %p\n", hrequest
, buffer
, to_write
, written
);
2655 if (!(request
= (request_t
*)grab_object( hrequest
)))
2657 set_last_error( ERROR_INVALID_HANDLE
);
2660 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
2662 release_object( &request
->hdr
);
2663 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2667 if (request
->connect
->hdr
.flags
& WINHTTP_FLAG_ASYNC
)
2671 if (!(w
= heap_alloc( sizeof(write_data_t
) ))) return FALSE
;
2672 w
->hdr
.request
= request
;
2673 w
->hdr
.proc
= task_write_data
;
2675 w
->to_write
= to_write
;
2676 w
->written
= written
;
2678 addref_object( &request
->hdr
);
2679 ret
= queue_task( (task_header_t
*)w
);
2682 ret
= write_data( request
, buffer
, to_write
, written
, FALSE
);
2684 release_object( &request
->hdr
);
2685 if (ret
) set_last_error( ERROR_SUCCESS
);
2691 REQUEST_STATE_UNINITIALIZED
,
2692 REQUEST_STATE_INITIALIZED
,
2693 REQUEST_STATE_CANCELLED
,
2696 REQUEST_STATE_RESPONSE_RECEIVED
2699 struct winhttp_request
2701 IWinHttpRequest IWinHttpRequest_iface
;
2703 CRITICAL_SECTION cs
;
2704 enum request_state state
;
2715 DWORD bytes_available
;
2719 DWORD disable_feature
;
2720 LONG resolve_timeout
;
2721 LONG connect_timeout
;
2723 LONG receive_timeout
;
2724 WINHTTP_PROXY_INFO proxy
;
2729 static inline struct winhttp_request
*impl_from_IWinHttpRequest( IWinHttpRequest
*iface
)
2731 return CONTAINING_RECORD( iface
, struct winhttp_request
, IWinHttpRequest_iface
);
2734 static ULONG WINAPI
winhttp_request_AddRef(
2735 IWinHttpRequest
*iface
)
2737 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
2738 return InterlockedIncrement( &request
->refs
);
2741 /* critical section must be held */
2742 static void cancel_request( struct winhttp_request
*request
)
2744 if (request
->state
<= REQUEST_STATE_CANCELLED
) return;
2746 SetEvent( request
->cancel
);
2747 LeaveCriticalSection( &request
->cs
);
2748 WaitForSingleObject( request
->thread
, INFINITE
);
2749 EnterCriticalSection( &request
->cs
);
2751 request
->state
= REQUEST_STATE_CANCELLED
;
2753 CloseHandle( request
->thread
);
2754 request
->thread
= NULL
;
2755 CloseHandle( request
->wait
);
2756 request
->wait
= NULL
;
2757 CloseHandle( request
->cancel
);
2758 request
->cancel
= NULL
;
2761 /* critical section must be held */
2762 static void free_request( struct winhttp_request
*request
)
2764 if (request
->state
< REQUEST_STATE_INITIALIZED
) return;
2765 WinHttpCloseHandle( request
->hrequest
);
2766 WinHttpCloseHandle( request
->hconnect
);
2767 WinHttpCloseHandle( request
->hsession
);
2768 CloseHandle( request
->thread
);
2769 CloseHandle( request
->wait
);
2770 CloseHandle( request
->cancel
);
2771 heap_free( request
->proxy
.lpszProxy
);
2772 heap_free( request
->proxy
.lpszProxyBypass
);
2773 heap_free( request
->buffer
);
2774 heap_free( request
->verb
);
2775 VariantClear( &request
->data
);
2778 static ULONG WINAPI
winhttp_request_Release(
2779 IWinHttpRequest
*iface
)
2781 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
2782 LONG refs
= InterlockedDecrement( &request
->refs
);
2785 TRACE("destroying %p\n", request
);
2787 EnterCriticalSection( &request
->cs
);
2788 cancel_request( request
);
2789 free_request( request
);
2790 LeaveCriticalSection( &request
->cs
);
2791 request
->cs
.DebugInfo
->Spare
[0] = 0;
2792 DeleteCriticalSection( &request
->cs
);
2793 heap_free( request
);
2798 static HRESULT WINAPI
winhttp_request_QueryInterface(
2799 IWinHttpRequest
*iface
,
2803 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
2805 TRACE("%p, %s, %p\n", request
, debugstr_guid(riid
), obj
);
2807 if (IsEqualGUID( riid
, &IID_IWinHttpRequest
) ||
2808 IsEqualGUID( riid
, &IID_IDispatch
) ||
2809 IsEqualGUID( riid
, &IID_IUnknown
))
2815 FIXME("interface %s not implemented\n", debugstr_guid(riid
));
2816 return E_NOINTERFACE
;
2818 IWinHttpRequest_AddRef( iface
);
2822 static HRESULT WINAPI
winhttp_request_GetTypeInfoCount(
2823 IWinHttpRequest
*iface
,
2826 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
2828 TRACE("%p, %p\n", request
, count
);
2835 IWinHttpRequest_tid
,
2839 static ITypeLib
*winhttp_typelib
;
2840 static ITypeInfo
*winhttp_typeinfo
[last_tid
];
2842 static REFIID winhttp_tid_id
[] =
2844 &IID_IWinHttpRequest
2847 static HRESULT
get_typeinfo( enum type_id tid
, ITypeInfo
**ret
)
2851 if (!winhttp_typelib
)
2855 hr
= LoadRegTypeLib( &LIBID_WinHttp
, 5, 1, LOCALE_SYSTEM_DEFAULT
, &typelib
);
2858 ERR("LoadRegTypeLib failed: %08x\n", hr
);
2861 if (InterlockedCompareExchangePointer( (void **)&winhttp_typelib
, typelib
, NULL
))
2862 ITypeLib_Release( typelib
);
2864 if (!winhttp_typeinfo
[tid
])
2866 ITypeInfo
*typeinfo
;
2868 hr
= ITypeLib_GetTypeInfoOfGuid( winhttp_typelib
, winhttp_tid_id
[tid
], &typeinfo
);
2871 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(winhttp_tid_id
[tid
]), hr
);
2874 if (InterlockedCompareExchangePointer( (void **)(winhttp_typeinfo
+ tid
), typeinfo
, NULL
))
2875 ITypeInfo_Release( typeinfo
);
2877 *ret
= winhttp_typeinfo
[tid
];
2878 ITypeInfo_AddRef(winhttp_typeinfo
[tid
]);
2882 void release_typelib(void)
2886 for (i
= 0; i
< sizeof(winhttp_typeinfo
)/sizeof(*winhttp_typeinfo
); i
++)
2887 if (winhttp_typeinfo
[i
])
2888 ITypeInfo_Release(winhttp_typeinfo
[i
]);
2890 if (winhttp_typelib
)
2891 ITypeLib_Release(winhttp_typelib
);
2894 static HRESULT WINAPI
winhttp_request_GetTypeInfo(
2895 IWinHttpRequest
*iface
,
2900 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
2901 TRACE("%p, %u, %u, %p\n", request
, index
, lcid
, info
);
2903 return get_typeinfo( IWinHttpRequest_tid
, info
);
2906 static HRESULT WINAPI
winhttp_request_GetIDsOfNames(
2907 IWinHttpRequest
*iface
,
2914 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
2915 ITypeInfo
*typeinfo
;
2918 TRACE("%p, %s, %p, %u, %u, %p\n", request
, debugstr_guid(riid
), names
, count
, lcid
, dispid
);
2920 if (!names
|| !count
|| !dispid
) return E_INVALIDARG
;
2922 hr
= get_typeinfo( IWinHttpRequest_tid
, &typeinfo
);
2925 hr
= ITypeInfo_GetIDsOfNames( typeinfo
, names
, count
, dispid
);
2926 ITypeInfo_Release( typeinfo
);
2931 static HRESULT WINAPI
winhttp_request_Invoke(
2932 IWinHttpRequest
*iface
,
2939 EXCEPINFO
*excep_info
,
2942 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
2943 ITypeInfo
*typeinfo
;
2946 TRACE("%p, %d, %s, %d, %d, %p, %p, %p, %p\n", request
, member
, debugstr_guid(riid
),
2947 lcid
, flags
, params
, result
, excep_info
, arg_err
);
2949 hr
= get_typeinfo( IWinHttpRequest_tid
, &typeinfo
);
2952 hr
= ITypeInfo_Invoke( typeinfo
, &request
->IWinHttpRequest_iface
, member
, flags
,
2953 params
, result
, excep_info
, arg_err
);
2954 ITypeInfo_Release( typeinfo
);
2959 static HRESULT WINAPI
winhttp_request_SetProxy(
2960 IWinHttpRequest
*iface
,
2961 HTTPREQUEST_PROXY_SETTING proxy_setting
,
2962 VARIANT proxy_server
,
2963 VARIANT bypass_list
)
2965 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
2966 DWORD err
= ERROR_SUCCESS
;
2968 TRACE("%p, %u, %s, %s\n", request
, proxy_setting
, debugstr_variant(&proxy_server
),
2969 debugstr_variant(&bypass_list
));
2971 EnterCriticalSection( &request
->cs
);
2972 switch (proxy_setting
)
2974 case HTTPREQUEST_PROXYSETTING_DEFAULT
:
2975 request
->proxy
.dwAccessType
= WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
;
2976 heap_free( request
->proxy
.lpszProxy
);
2977 heap_free( request
->proxy
.lpszProxyBypass
);
2978 request
->proxy
.lpszProxy
= NULL
;
2979 request
->proxy
.lpszProxyBypass
= NULL
;
2982 case HTTPREQUEST_PROXYSETTING_DIRECT
:
2983 request
->proxy
.dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
2984 heap_free( request
->proxy
.lpszProxy
);
2985 heap_free( request
->proxy
.lpszProxyBypass
);
2986 request
->proxy
.lpszProxy
= NULL
;
2987 request
->proxy
.lpszProxyBypass
= NULL
;
2990 case HTTPREQUEST_PROXYSETTING_PROXY
:
2991 request
->proxy
.dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
2992 if (V_VT( &proxy_server
) == VT_BSTR
)
2994 heap_free( request
->proxy
.lpszProxy
);
2995 request
->proxy
.lpszProxy
= strdupW( V_BSTR( &proxy_server
) );
2997 if (V_VT( &bypass_list
) == VT_BSTR
)
2999 heap_free( request
->proxy
.lpszProxyBypass
);
3000 request
->proxy
.lpszProxyBypass
= strdupW( V_BSTR( &bypass_list
) );
3005 err
= ERROR_INVALID_PARAMETER
;
3008 LeaveCriticalSection( &request
->cs
);
3009 return HRESULT_FROM_WIN32( err
);
3012 static HRESULT WINAPI
winhttp_request_SetCredentials(
3013 IWinHttpRequest
*iface
,
3016 HTTPREQUEST_SETCREDENTIALS_FLAGS flags
)
3018 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3019 DWORD target
, scheme
= WINHTTP_AUTH_SCHEME_BASIC
; /* FIXME: query supported schemes */
3020 DWORD err
= ERROR_SUCCESS
;
3022 TRACE("%p, %s, %p\n", request
, debugstr_w(username
), password
);
3024 EnterCriticalSection( &request
->cs
);
3025 if (request
->state
< REQUEST_STATE_OPEN
)
3027 err
= ERROR_WINHTTP_CANNOT_CALL_BEFORE_OPEN
;
3032 case HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
:
3033 target
= WINHTTP_AUTH_TARGET_SERVER
;
3035 case HTTPREQUEST_SETCREDENTIALS_FOR_PROXY
:
3036 target
= WINHTTP_AUTH_TARGET_PROXY
;
3039 err
= ERROR_INVALID_PARAMETER
;
3042 if (!WinHttpSetCredentials( request
->hrequest
, target
, scheme
, username
, password
, NULL
))
3044 err
= get_last_error();
3047 LeaveCriticalSection( &request
->cs
);
3048 return HRESULT_FROM_WIN32( err
);
3051 static void initialize_request( struct winhttp_request
*request
)
3053 request
->hrequest
= NULL
;
3054 request
->hconnect
= NULL
;
3055 request
->hsession
= NULL
;
3056 request
->thread
= NULL
;
3057 request
->wait
= NULL
;
3058 request
->cancel
= NULL
;
3059 request
->buffer
= NULL
;
3060 request
->verb
= NULL
;
3061 request
->offset
= 0;
3062 request
->bytes_available
= 0;
3063 request
->bytes_read
= 0;
3064 request
->error
= ERROR_SUCCESS
;
3065 request
->async
= FALSE
;
3066 request
->logon_policy
= WINHTTP_AUTOLOGON_SECURITY_LEVEL_MEDIUM
;
3067 request
->disable_feature
= 0;
3068 request
->proxy
.dwAccessType
= WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
;
3069 request
->proxy
.lpszProxy
= NULL
;
3070 request
->proxy
.lpszProxyBypass
= NULL
;
3071 request
->resolve_timeout
= 0;
3072 request
->connect_timeout
= 60000;
3073 request
->send_timeout
= 30000;
3074 request
->receive_timeout
= 30000;
3075 request
->url_codepage
= CP_UTF8
;
3076 VariantInit( &request
->data
);
3077 request
->state
= REQUEST_STATE_INITIALIZED
;
3080 static void reset_request( struct winhttp_request
*request
)
3082 cancel_request( request
);
3083 WinHttpCloseHandle( request
->hrequest
);
3084 request
->hrequest
= NULL
;
3085 WinHttpCloseHandle( request
->hconnect
);
3086 request
->hconnect
= NULL
;
3087 heap_free( request
->buffer
);
3088 request
->buffer
= NULL
;
3089 heap_free( request
->verb
);
3090 request
->verb
= NULL
;
3091 request
->offset
= 0;
3092 request
->bytes_available
= 0;
3093 request
->bytes_read
= 0;
3094 request
->error
= ERROR_SUCCESS
;
3095 request
->async
= FALSE
;
3096 request
->url_codepage
= CP_UTF8
;
3097 VariantClear( &request
->data
);
3098 request
->state
= REQUEST_STATE_INITIALIZED
;
3101 static HRESULT WINAPI
winhttp_request_Open(
3102 IWinHttpRequest
*iface
,
3107 static const WCHAR typeW
[] = {'*','/','*',0};
3108 static const WCHAR
*acceptW
[] = {typeW
, NULL
};
3109 static const WCHAR httpsW
[] = {'h','t','t','p','s'};
3110 static const WCHAR user_agentW
[] = {
3111 'M','o','z','i','l','l','a','/','4','.','0',' ','(','c','o','m','p','a','t','i','b','l','e',';',' ',
3112 'W','i','n','3','2',';',' ','W','i','n','H','t','t','p','.','W','i','n','H','t','t','p',
3113 'R','e','q','u','e','s','t','.','5',')',0};
3114 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3116 WCHAR
*hostname
, *path
= NULL
, *verb
= NULL
;
3117 DWORD err
= ERROR_OUTOFMEMORY
, len
, flags
= 0;
3119 TRACE("%p, %s, %s, %s\n", request
, debugstr_w(method
), debugstr_w(url
),
3120 debugstr_variant(&async
));
3122 if (!method
|| !url
) return E_INVALIDARG
;
3124 memset( &uc
, 0, sizeof(uc
) );
3125 uc
.dwStructSize
= sizeof(uc
);
3126 uc
.dwSchemeLength
= ~0u;
3127 uc
.dwHostNameLength
= ~0u;
3128 uc
.dwUrlPathLength
= ~0u;
3129 uc
.dwExtraInfoLength
= ~0u;
3130 if (!WinHttpCrackUrl( url
, 0, 0, &uc
)) return HRESULT_FROM_WIN32( get_last_error() );
3132 EnterCriticalSection( &request
->cs
);
3133 if (request
->state
< REQUEST_STATE_INITIALIZED
) initialize_request( request
);
3134 else reset_request( request
);
3136 if (!(hostname
= heap_alloc( (uc
.dwHostNameLength
+ 1) * sizeof(WCHAR
) ))) goto error
;
3137 memcpy( hostname
, uc
.lpszHostName
, uc
.dwHostNameLength
* sizeof(WCHAR
) );
3138 hostname
[uc
.dwHostNameLength
] = 0;
3140 if (!(path
= heap_alloc( (uc
.dwUrlPathLength
+ uc
.dwExtraInfoLength
+ 1) * sizeof(WCHAR
) ))) goto error
;
3141 memcpy( path
, uc
.lpszUrlPath
, (uc
.dwUrlPathLength
+ uc
.dwExtraInfoLength
) * sizeof(WCHAR
) );
3142 path
[uc
.dwUrlPathLength
+ uc
.dwExtraInfoLength
] = 0;
3144 if (!(verb
= strdupW( method
))) goto error
;
3145 if (SUCCEEDED( VariantChangeType( &async
, &async
, 0, VT_BOOL
)) && V_BOOL( &async
)) request
->async
= TRUE
;
3146 else request
->async
= FALSE
;
3148 if (!request
->hsession
)
3150 if (!(request
->hsession
= WinHttpOpen( user_agentW
, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
, NULL
, NULL
,
3151 WINHTTP_FLAG_ASYNC
)))
3153 err
= get_last_error();
3156 if (!(request
->hconnect
= WinHttpConnect( request
->hsession
, hostname
, uc
.nPort
, 0 )))
3158 WinHttpCloseHandle( request
->hsession
);
3159 request
->hsession
= NULL
;
3160 err
= get_last_error();
3164 else if (!(request
->hconnect
= WinHttpConnect( request
->hsession
, hostname
, uc
.nPort
, 0 )))
3166 err
= get_last_error();
3170 len
= sizeof(httpsW
) / sizeof(WCHAR
);
3171 if (uc
.dwSchemeLength
== len
&& !memcmp( uc
.lpszScheme
, httpsW
, len
* sizeof(WCHAR
) ))
3173 flags
|= WINHTTP_FLAG_SECURE
;
3175 if (!(request
->hrequest
= WinHttpOpenRequest( request
->hconnect
, method
, path
, NULL
, NULL
, acceptW
, flags
)))
3177 err
= get_last_error();
3180 WinHttpSetOption( request
->hrequest
, WINHTTP_OPTION_CONTEXT_VALUE
, &request
, sizeof(request
) );
3182 request
->state
= REQUEST_STATE_OPEN
;
3183 request
->verb
= verb
;
3184 heap_free( hostname
);
3186 LeaveCriticalSection( &request
->cs
);
3190 WinHttpCloseHandle( request
->hconnect
);
3191 request
->hconnect
= NULL
;
3192 heap_free( hostname
);
3195 LeaveCriticalSection( &request
->cs
);
3196 return HRESULT_FROM_WIN32( err
);
3199 static HRESULT WINAPI
winhttp_request_SetRequestHeader(
3200 IWinHttpRequest
*iface
,
3204 static const WCHAR fmtW
[] = {'%','s',':',' ','%','s','\r','\n',0};
3205 static const WCHAR emptyW
[] = {0};
3206 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3207 DWORD len
, err
= ERROR_SUCCESS
;
3210 TRACE("%p, %s, %s\n", request
, debugstr_w(header
), debugstr_w(value
));
3212 if (!header
) return E_INVALIDARG
;
3214 EnterCriticalSection( &request
->cs
);
3215 if (request
->state
< REQUEST_STATE_OPEN
)
3217 err
= ERROR_WINHTTP_CANNOT_CALL_BEFORE_OPEN
;
3220 if (request
->state
>= REQUEST_STATE_SENT
)
3222 err
= ERROR_WINHTTP_CANNOT_CALL_AFTER_SEND
;
3225 len
= strlenW( header
) + 4;
3226 if (value
) len
+= strlenW( value
);
3227 if (!(str
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) )))
3229 err
= ERROR_OUTOFMEMORY
;
3232 sprintfW( str
, fmtW
, header
, value
? value
: emptyW
);
3233 if (!WinHttpAddRequestHeaders( request
->hrequest
, str
, len
,
3234 WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
))
3236 err
= get_last_error();
3241 LeaveCriticalSection( &request
->cs
);
3242 return HRESULT_FROM_WIN32( err
);
3245 static HRESULT WINAPI
winhttp_request_GetResponseHeader(
3246 IWinHttpRequest
*iface
,
3250 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3251 DWORD size
, err
= ERROR_SUCCESS
;
3253 TRACE("%p, %p\n", request
, header
);
3255 EnterCriticalSection( &request
->cs
);
3256 if (request
->state
< REQUEST_STATE_SENT
)
3258 err
= ERROR_WINHTTP_CANNOT_CALL_BEFORE_SEND
;
3261 if (!header
|| !value
)
3263 err
= ERROR_INVALID_PARAMETER
;
3267 if (!WinHttpQueryHeaders( request
->hrequest
, WINHTTP_QUERY_CUSTOM
, header
, NULL
, &size
, NULL
))
3269 err
= get_last_error();
3270 if (err
!= ERROR_INSUFFICIENT_BUFFER
) goto done
;
3272 if (!(*value
= SysAllocStringLen( NULL
, size
/ sizeof(WCHAR
) )))
3274 err
= ERROR_OUTOFMEMORY
;
3277 err
= ERROR_SUCCESS
;
3278 if (!WinHttpQueryHeaders( request
->hrequest
, WINHTTP_QUERY_CUSTOM
, header
, *value
, &size
, NULL
))
3280 err
= get_last_error();
3281 SysFreeString( *value
);
3284 LeaveCriticalSection( &request
->cs
);
3285 return HRESULT_FROM_WIN32( err
);
3288 static HRESULT WINAPI
winhttp_request_GetAllResponseHeaders(
3289 IWinHttpRequest
*iface
,
3292 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3293 DWORD size
, err
= ERROR_SUCCESS
;
3295 TRACE("%p, %p\n", request
, headers
);
3297 if (!headers
) return E_INVALIDARG
;
3299 EnterCriticalSection( &request
->cs
);
3300 if (request
->state
< REQUEST_STATE_SENT
)
3302 err
= ERROR_WINHTTP_CANNOT_CALL_BEFORE_SEND
;
3306 if (!WinHttpQueryHeaders( request
->hrequest
, WINHTTP_QUERY_RAW_HEADERS_CRLF
, NULL
, NULL
, &size
, NULL
))
3308 err
= get_last_error();
3309 if (err
!= ERROR_INSUFFICIENT_BUFFER
) goto done
;
3311 if (!(*headers
= SysAllocStringLen( NULL
, size
/ sizeof(WCHAR
) )))
3313 err
= ERROR_OUTOFMEMORY
;
3316 err
= ERROR_SUCCESS
;
3317 if (!WinHttpQueryHeaders( request
->hrequest
, WINHTTP_QUERY_RAW_HEADERS_CRLF
, NULL
, *headers
, &size
, NULL
))
3319 err
= get_last_error();
3320 SysFreeString( *headers
);
3323 LeaveCriticalSection( &request
->cs
);
3324 return HRESULT_FROM_WIN32( err
);
3327 static void CALLBACK
wait_status_callback( HINTERNET handle
, DWORD_PTR context
, DWORD status
, LPVOID buffer
, DWORD size
)
3329 struct winhttp_request
*request
= (struct winhttp_request
*)context
;
3333 case WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE
:
3334 request
->bytes_available
= *(DWORD
*)buffer
;
3335 request
->error
= ERROR_SUCCESS
;
3337 case WINHTTP_CALLBACK_STATUS_READ_COMPLETE
:
3338 request
->bytes_read
= size
;
3339 request
->error
= ERROR_SUCCESS
;
3341 case WINHTTP_CALLBACK_STATUS_REQUEST_ERROR
:
3343 WINHTTP_ASYNC_RESULT
*result
= (WINHTTP_ASYNC_RESULT
*)buffer
;
3344 request
->error
= result
->dwError
;
3349 SetEvent( request
->wait
);
3352 static void wait_set_status_callback( struct winhttp_request
*request
, DWORD status
)
3354 status
|= WINHTTP_CALLBACK_STATUS_REQUEST_ERROR
;
3355 WinHttpSetStatusCallback( request
->hrequest
, wait_status_callback
, status
, 0 );
3358 static DWORD
wait_for_completion( struct winhttp_request
*request
)
3360 HANDLE handles
[2] = { request
->wait
, request
->cancel
};
3362 switch (WaitForMultipleObjects( 2, handles
, FALSE
, INFINITE
))
3366 case WAIT_OBJECT_0
+ 1:
3367 request
->error
= ERROR_CANCELLED
;
3370 request
->error
= get_last_error();
3373 return request
->error
;
3376 static HRESULT
request_receive( struct winhttp_request
*request
)
3378 DWORD err
, size
, buflen
= 4096;
3380 wait_set_status_callback( request
, WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE
);
3381 if (!WinHttpReceiveResponse( request
->hrequest
, NULL
))
3383 return HRESULT_FROM_WIN32( get_last_error() );
3385 if ((err
= wait_for_completion( request
))) return HRESULT_FROM_WIN32( err
);
3386 if (!strcmpW( request
->verb
, headW
))
3388 request
->state
= REQUEST_STATE_RESPONSE_RECEIVED
;
3391 if (!(request
->buffer
= heap_alloc( buflen
))) return E_OUTOFMEMORY
;
3392 request
->buffer
[0] = 0;
3396 wait_set_status_callback( request
, WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE
);
3397 if (!WinHttpQueryDataAvailable( request
->hrequest
, &request
->bytes_available
))
3399 err
= get_last_error();
3402 if ((err
= wait_for_completion( request
))) goto error
;
3403 if (!request
->bytes_available
) break;
3404 size
+= request
->bytes_available
;
3408 while (buflen
< size
) buflen
*= 2;
3409 if (!(tmp
= heap_realloc( request
->buffer
, buflen
)))
3411 err
= ERROR_OUTOFMEMORY
;
3414 request
->buffer
= tmp
;
3416 wait_set_status_callback( request
, WINHTTP_CALLBACK_STATUS_READ_COMPLETE
);
3417 if (!WinHttpReadData( request
->hrequest
, request
->buffer
+ request
->offset
,
3418 request
->bytes_available
, &request
->bytes_read
))
3420 err
= get_last_error();
3423 if ((err
= wait_for_completion( request
))) goto error
;
3424 request
->offset
+= request
->bytes_read
;
3425 } while (request
->bytes_read
);
3427 request
->state
= REQUEST_STATE_RESPONSE_RECEIVED
;
3431 heap_free( request
->buffer
);
3432 request
->buffer
= NULL
;
3433 return HRESULT_FROM_WIN32( err
);
3436 static DWORD
request_set_parameters( struct winhttp_request
*request
)
3438 if (!WinHttpSetOption( request
->hrequest
, WINHTTP_OPTION_PROXY
, &request
->proxy
,
3439 sizeof(request
->proxy
) )) return get_last_error();
3441 if (!WinHttpSetOption( request
->hrequest
, WINHTTP_OPTION_AUTOLOGON_POLICY
, &request
->logon_policy
,
3442 sizeof(request
->logon_policy
) )) return get_last_error();
3444 if (!WinHttpSetOption( request
->hrequest
, WINHTTP_OPTION_DISABLE_FEATURE
, &request
->disable_feature
,
3445 sizeof(request
->disable_feature
) )) return get_last_error();
3447 if (!WinHttpSetTimeouts( request
->hrequest
,
3448 request
->resolve_timeout
,
3449 request
->connect_timeout
,
3450 request
->send_timeout
,
3451 request
->receive_timeout
)) return get_last_error();
3452 return ERROR_SUCCESS
;
3455 static void request_set_utf8_content_type( struct winhttp_request
*request
)
3457 static const WCHAR fmtW
[] = {'%','s',':',' ','%','s',0};
3458 static const WCHAR text_plainW
[] = {'t','e','x','t','/','p','l','a','i','n',0};
3459 static const WCHAR charset_utf8W
[] = {'c','h','a','r','s','e','t','=','u','t','f','-','8',0};
3463 len
= sprintfW( headerW
, fmtW
, attr_content_type
, text_plainW
);
3464 WinHttpAddRequestHeaders( request
->hrequest
, headerW
, len
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
);
3465 len
= sprintfW( headerW
, fmtW
, attr_content_type
, charset_utf8W
);
3466 WinHttpAddRequestHeaders( request
->hrequest
, headerW
, len
, WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON
);
3469 static HRESULT
request_send( struct winhttp_request
*request
)
3471 SAFEARRAY
*sa
= NULL
;
3478 if ((err
= request_set_parameters( request
))) return HRESULT_FROM_WIN32( err
);
3479 if (strcmpW( request
->verb
, getW
))
3481 VariantInit( &data
);
3482 if (V_VT( &request
->data
) == VT_BSTR
)
3485 const WCHAR
*str
= V_BSTR( &request
->data
);
3486 int i
, len
= strlenW( str
);
3488 for (i
= 0; i
< len
; i
++)
3496 size
= WideCharToMultiByte( cp
, 0, str
, len
, NULL
, 0, NULL
, NULL
);
3497 if (!(ptr
= heap_alloc( size
))) return E_OUTOFMEMORY
;
3498 WideCharToMultiByte( cp
, 0, str
, len
, ptr
, size
, NULL
, NULL
);
3499 if (cp
== CP_UTF8
) request_set_utf8_content_type( request
);
3501 else if (VariantChangeType( &data
, &request
->data
, 0, VT_ARRAY
|VT_UI1
) == S_OK
)
3503 sa
= V_ARRAY( &data
);
3504 if ((hr
= SafeArrayAccessData( sa
, (void **)&ptr
)) != S_OK
) return hr
;
3505 if ((hr
= SafeArrayGetUBound( sa
, 1, &size
)) != S_OK
)
3507 SafeArrayUnaccessData( sa
);
3513 wait_set_status_callback( request
, WINHTTP_CALLBACK_STATUS_REQUEST_SENT
);
3514 if (!WinHttpSendRequest( request
->hrequest
, NULL
, 0, ptr
, size
, size
, 0 ))
3516 err
= get_last_error();
3519 if ((err
= wait_for_completion( request
))) goto error
;
3520 if (sa
) SafeArrayUnaccessData( sa
);
3521 else heap_free( ptr
);
3522 request
->state
= REQUEST_STATE_SENT
;
3526 if (sa
) SafeArrayUnaccessData( sa
);
3527 else heap_free( ptr
);
3528 return HRESULT_FROM_WIN32( err
);
3531 static HRESULT
request_send_and_receive( struct winhttp_request
*request
)
3533 HRESULT hr
= request_send( request
);
3534 if (hr
== S_OK
) hr
= request_receive( request
);
3538 static DWORD CALLBACK
send_and_receive_proc( void *arg
)
3540 struct winhttp_request
*request
= (struct winhttp_request
*)arg
;
3541 return request_send_and_receive( request
);
3544 /* critical section must be held */
3545 static DWORD
request_wait( struct winhttp_request
*request
, DWORD timeout
)
3547 HANDLE thread
= request
->thread
;
3550 LeaveCriticalSection( &request
->cs
);
3551 while ((err
= MsgWaitForMultipleObjects( 1, &thread
, FALSE
, timeout
, QS_ALLINPUT
)) == WAIT_OBJECT_0
+ 1)
3554 while (PeekMessageW( &msg
, NULL
, 0, 0, PM_REMOVE
))
3556 TranslateMessage( &msg
);
3557 DispatchMessageW( &msg
);
3563 ret
= ERROR_SUCCESS
;
3566 ret
= ERROR_TIMEOUT
;
3570 ret
= get_last_error();
3573 EnterCriticalSection( &request
->cs
);
3577 static HRESULT WINAPI
winhttp_request_Send(
3578 IWinHttpRequest
*iface
,
3581 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3584 TRACE("%p, %s\n", request
, debugstr_variant(&body
));
3586 EnterCriticalSection( &request
->cs
);
3587 if (request
->state
< REQUEST_STATE_OPEN
)
3589 LeaveCriticalSection( &request
->cs
);
3590 return HRESULT_FROM_WIN32( ERROR_WINHTTP_CANNOT_CALL_BEFORE_OPEN
);
3592 if (request
->state
>= REQUEST_STATE_SENT
)
3594 LeaveCriticalSection( &request
->cs
);
3597 VariantClear( &request
->data
);
3598 if ((hr
= VariantCopyInd( &request
->data
, &body
)) != S_OK
)
3600 LeaveCriticalSection( &request
->cs
);
3603 if (!(request
->thread
= CreateThread( NULL
, 0, send_and_receive_proc
, request
, 0, NULL
)))
3605 LeaveCriticalSection( &request
->cs
);
3606 return HRESULT_FROM_WIN32( get_last_error() );
3608 request
->wait
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
);
3609 request
->cancel
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
);
3610 if (!request
->async
)
3612 hr
= HRESULT_FROM_WIN32( request_wait( request
, INFINITE
) );
3614 LeaveCriticalSection( &request
->cs
);
3618 static HRESULT WINAPI
winhttp_request_get_Status(
3619 IWinHttpRequest
*iface
,
3622 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3623 DWORD err
= ERROR_SUCCESS
, flags
, status_code
, len
= sizeof(status_code
), index
= 0;
3625 TRACE("%p, %p\n", request
, status
);
3627 if (!status
) return E_INVALIDARG
;
3629 EnterCriticalSection( &request
->cs
);
3630 if (request
->state
< REQUEST_STATE_SENT
)
3632 err
= ERROR_WINHTTP_CANNOT_CALL_BEFORE_SEND
;
3635 flags
= WINHTTP_QUERY_STATUS_CODE
| WINHTTP_QUERY_FLAG_NUMBER
;
3636 if (!WinHttpQueryHeaders( request
->hrequest
, flags
, NULL
, &status_code
, &len
, &index
))
3638 err
= get_last_error();
3641 *status
= status_code
;
3644 LeaveCriticalSection( &request
->cs
);
3645 return HRESULT_FROM_WIN32( err
);
3648 static HRESULT WINAPI
winhttp_request_get_StatusText(
3649 IWinHttpRequest
*iface
,
3652 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3653 DWORD err
= ERROR_SUCCESS
, len
= 0, index
= 0;
3655 TRACE("%p, %p\n", request
, status
);
3657 if (!status
) return E_INVALIDARG
;
3659 EnterCriticalSection( &request
->cs
);
3660 if (request
->state
< REQUEST_STATE_SENT
)
3662 err
= ERROR_WINHTTP_CANNOT_CALL_BEFORE_SEND
;
3665 if (!WinHttpQueryHeaders( request
->hrequest
, WINHTTP_QUERY_STATUS_TEXT
, NULL
, NULL
, &len
, &index
))
3667 err
= get_last_error();
3668 if (err
!= ERROR_INSUFFICIENT_BUFFER
) goto done
;
3670 if (!(*status
= SysAllocStringLen( NULL
, len
/ sizeof(WCHAR
) )))
3672 err
= ERROR_OUTOFMEMORY
;
3676 err
= ERROR_SUCCESS
;
3677 if (!WinHttpQueryHeaders( request
->hrequest
, WINHTTP_QUERY_STATUS_TEXT
, NULL
, *status
, &len
, &index
))
3679 err
= get_last_error();
3680 SysFreeString( *status
);
3683 LeaveCriticalSection( &request
->cs
);
3684 return HRESULT_FROM_WIN32( err
);
3687 static DWORD
request_get_codepage( struct winhttp_request
*request
, UINT
*codepage
)
3689 static const WCHAR utf8W
[] = {'u','t','f','-','8',0};
3690 static const WCHAR charsetW
[] = {'c','h','a','r','s','e','t',0};
3695 if (!WinHttpQueryHeaders( request
->hrequest
, WINHTTP_QUERY_CONTENT_TYPE
, NULL
, NULL
, &size
, NULL
) &&
3696 get_last_error() == ERROR_INSUFFICIENT_BUFFER
)
3698 if (!(buffer
= heap_alloc( size
))) return ERROR_OUTOFMEMORY
;
3699 if (!WinHttpQueryHeaders( request
->hrequest
, WINHTTP_QUERY_CONTENT_TYPE
, NULL
, buffer
, &size
, NULL
))
3701 return get_last_error();
3703 if ((p
= strstrW( buffer
, charsetW
)))
3705 p
+= strlenW( charsetW
);
3706 while (*p
== ' ') p
++;
3709 while (*p
== ' ') p
++;
3710 if (!strcmpiW( p
, utf8W
)) *codepage
= CP_UTF8
;
3713 heap_free( buffer
);
3715 return ERROR_SUCCESS
;
3718 static HRESULT WINAPI
winhttp_request_get_ResponseText(
3719 IWinHttpRequest
*iface
,
3722 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3724 DWORD err
= ERROR_SUCCESS
;
3727 TRACE("%p, %p\n", request
, body
);
3729 if (!body
) return E_INVALIDARG
;
3731 EnterCriticalSection( &request
->cs
);
3732 if (request
->state
< REQUEST_STATE_SENT
)
3734 err
= ERROR_WINHTTP_CANNOT_CALL_BEFORE_SEND
;
3737 if ((err
= request_get_codepage( request
, &codepage
))) goto done
;
3738 len
= MultiByteToWideChar( codepage
, 0, request
->buffer
, request
->offset
, NULL
, 0 );
3739 if (!(*body
= SysAllocStringLen( NULL
, len
)))
3741 err
= ERROR_OUTOFMEMORY
;
3744 MultiByteToWideChar( codepage
, 0, request
->buffer
, request
->offset
, *body
, len
);
3748 LeaveCriticalSection( &request
->cs
);
3749 return HRESULT_FROM_WIN32( err
);
3752 static HRESULT WINAPI
winhttp_request_get_ResponseBody(
3753 IWinHttpRequest
*iface
,
3756 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3759 DWORD err
= ERROR_SUCCESS
;
3762 TRACE("%p, %p\n", request
, body
);
3764 if (!body
) return E_INVALIDARG
;
3766 EnterCriticalSection( &request
->cs
);
3767 if (request
->state
< REQUEST_STATE_SENT
)
3769 err
= ERROR_WINHTTP_CANNOT_CALL_BEFORE_SEND
;
3772 if (!(sa
= SafeArrayCreateVector( VT_UI1
, 0, request
->offset
)))
3774 err
= ERROR_OUTOFMEMORY
;
3777 if ((hr
= SafeArrayAccessData( sa
, (void **)&ptr
)) != S_OK
)
3779 SafeArrayDestroy( sa
);
3780 LeaveCriticalSection( &request
->cs
);
3783 memcpy( ptr
, request
->buffer
, request
->offset
);
3784 if ((hr
= SafeArrayUnaccessData( sa
)) != S_OK
)
3786 SafeArrayDestroy( sa
);
3787 LeaveCriticalSection( &request
->cs
);
3790 V_VT( body
) = VT_ARRAY
|VT_UI1
;
3791 V_ARRAY( body
) = sa
;
3794 LeaveCriticalSection( &request
->cs
);
3795 return HRESULT_FROM_WIN32( err
);
3800 IStream IStream_iface
;
3803 ULARGE_INTEGER pos
, size
;
3806 static inline struct stream
*impl_from_IStream( IStream
*iface
)
3808 return CONTAINING_RECORD( iface
, struct stream
, IStream_iface
);
3811 static HRESULT WINAPI
stream_QueryInterface( IStream
*iface
, REFIID riid
, void **obj
)
3813 struct stream
*stream
= impl_from_IStream( iface
);
3815 TRACE("%p, %s, %p\n", stream
, debugstr_guid(riid
), obj
);
3817 if (IsEqualGUID( riid
, &IID_IStream
) || IsEqualGUID( riid
, &IID_IUnknown
))
3823 FIXME("interface %s not implemented\n", debugstr_guid(riid
));
3824 return E_NOINTERFACE
;
3826 IStream_AddRef( iface
);
3830 static ULONG WINAPI
stream_AddRef( IStream
*iface
)
3832 struct stream
*stream
= impl_from_IStream( iface
);
3833 return InterlockedIncrement( &stream
->refs
);
3836 static ULONG WINAPI
stream_Release( IStream
*iface
)
3838 struct stream
*stream
= impl_from_IStream( iface
);
3839 LONG refs
= InterlockedDecrement( &stream
->refs
);
3842 heap_free( stream
->data
);
3843 heap_free( stream
);
3848 static HRESULT WINAPI
stream_Read( IStream
*iface
, void *buf
, ULONG len
, ULONG
*read
)
3850 struct stream
*stream
= impl_from_IStream( iface
);
3853 if (stream
->pos
.QuadPart
>= stream
->size
.QuadPart
)
3859 size
= min( stream
->size
.QuadPart
- stream
->pos
.QuadPart
, len
);
3860 memcpy( buf
, stream
->data
+ stream
->pos
.QuadPart
, size
);
3861 stream
->pos
.QuadPart
+= size
;
3867 static HRESULT WINAPI
stream_Write( IStream
*iface
, const void *buf
, ULONG len
, ULONG
*written
)
3873 static HRESULT WINAPI
stream_Seek( IStream
*iface
, LARGE_INTEGER move
, DWORD origin
, ULARGE_INTEGER
*newpos
)
3875 struct stream
*stream
= impl_from_IStream( iface
);
3877 if (origin
== STREAM_SEEK_SET
)
3878 stream
->pos
.QuadPart
= move
.QuadPart
;
3879 else if (origin
== STREAM_SEEK_CUR
)
3880 stream
->pos
.QuadPart
+= move
.QuadPart
;
3881 else if (origin
== STREAM_SEEK_END
)
3882 stream
->pos
.QuadPart
= stream
->size
.QuadPart
- move
.QuadPart
;
3884 if (newpos
) newpos
->QuadPart
= stream
->pos
.QuadPart
;
3888 static HRESULT WINAPI
stream_SetSize( IStream
*iface
, ULARGE_INTEGER newsize
)
3894 static HRESULT WINAPI
stream_CopyTo( IStream
*iface
, IStream
*stream
, ULARGE_INTEGER len
, ULARGE_INTEGER
*read
,
3895 ULARGE_INTEGER
*written
)
3901 static HRESULT WINAPI
stream_Commit( IStream
*iface
, DWORD flags
)
3907 static HRESULT WINAPI
stream_Revert( IStream
*iface
)
3913 static HRESULT WINAPI
stream_LockRegion( IStream
*iface
, ULARGE_INTEGER offset
, ULARGE_INTEGER len
, DWORD locktype
)
3919 static HRESULT WINAPI
stream_UnlockRegion( IStream
*iface
, ULARGE_INTEGER offset
, ULARGE_INTEGER len
, DWORD locktype
)
3925 static HRESULT WINAPI
stream_Stat( IStream
*iface
, STATSTG
*stg
, DWORD flag
)
3931 static HRESULT WINAPI
stream_Clone( IStream
*iface
, IStream
**stream
)
3937 static const IStreamVtbl stream_vtbl
=
3939 stream_QueryInterface
,
3950 stream_UnlockRegion
,
3955 static HRESULT WINAPI
winhttp_request_get_ResponseStream(
3956 IWinHttpRequest
*iface
,
3959 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3960 DWORD err
= ERROR_SUCCESS
;
3961 struct stream
*stream
;
3963 TRACE("%p, %p\n", request
, body
);
3965 if (!body
) return E_INVALIDARG
;
3967 EnterCriticalSection( &request
->cs
);
3968 if (request
->state
< REQUEST_STATE_SENT
)
3970 err
= ERROR_WINHTTP_CANNOT_CALL_BEFORE_SEND
;
3973 if (!(stream
= heap_alloc( sizeof(*stream
) )))
3975 err
= ERROR_OUTOFMEMORY
;
3978 stream
->IStream_iface
.lpVtbl
= &stream_vtbl
;
3980 if (!(stream
->data
= heap_alloc( request
->offset
)))
3982 heap_free( stream
);
3983 err
= ERROR_OUTOFMEMORY
;
3986 memcpy( stream
->data
, request
->buffer
, request
->offset
);
3987 stream
->pos
.QuadPart
= 0;
3988 stream
->size
.QuadPart
= request
->offset
;
3989 V_VT( body
) = VT_UNKNOWN
;
3990 V_UNKNOWN( body
) = (IUnknown
*)&stream
->IStream_iface
;
3993 LeaveCriticalSection( &request
->cs
);
3994 return HRESULT_FROM_WIN32( err
);
3997 static HRESULT WINAPI
winhttp_request_get_Option(
3998 IWinHttpRequest
*iface
,
3999 WinHttpRequestOption option
,
4002 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
4005 TRACE("%p, %u, %p\n", request
, option
, value
);
4007 EnterCriticalSection( &request
->cs
);
4010 case WinHttpRequestOption_URLCodePage
:
4011 V_VT( value
) = VT_I4
;
4012 V_I4( value
) = request
->url_codepage
;
4015 FIXME("unimplemented option %u\n", option
);
4019 LeaveCriticalSection( &request
->cs
);
4023 static HRESULT WINAPI
winhttp_request_put_Option(
4024 IWinHttpRequest
*iface
,
4025 WinHttpRequestOption option
,
4028 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
4031 TRACE("%p, %u, %s\n", request
, option
, debugstr_variant(&value
));
4033 EnterCriticalSection( &request
->cs
);
4036 case WinHttpRequestOption_EnableRedirects
:
4038 if (V_BOOL( &value
))
4039 request
->disable_feature
&= ~WINHTTP_DISABLE_REDIRECTS
;
4041 request
->disable_feature
|= WINHTTP_DISABLE_REDIRECTS
;
4044 case WinHttpRequestOption_URLCodePage
:
4046 static const WCHAR utf8W
[] = {'u','t','f','-','8',0};
4050 hr
= VariantChangeType( &cp
, &value
, 0, VT_UI4
);
4051 if (SUCCEEDED( hr
))
4053 request
->url_codepage
= V_UI4( &cp
);
4054 TRACE("URL codepage: %u\n", request
->url_codepage
);
4056 else if (V_VT( &value
) == VT_BSTR
&& !strcmpiW( V_BSTR( &value
), utf8W
))
4058 TRACE("URL codepage: UTF-8\n");
4059 request
->url_codepage
= CP_UTF8
;
4063 FIXME("URL codepage %s is not recognized\n", debugstr_variant( &value
));
4067 FIXME("unimplemented option %u\n", option
);
4071 LeaveCriticalSection( &request
->cs
);
4075 static HRESULT WINAPI
winhttp_request_WaitForResponse(
4076 IWinHttpRequest
*iface
,
4078 VARIANT_BOOL
*succeeded
)
4080 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
4081 DWORD err
, msecs
= (V_I4(&timeout
) == -1) ? INFINITE
: V_I4(&timeout
) * 1000;
4083 TRACE("%p, %s, %p\n", request
, debugstr_variant(&timeout
), succeeded
);
4085 EnterCriticalSection( &request
->cs
);
4086 if (request
->state
>= REQUEST_STATE_RESPONSE_RECEIVED
)
4088 LeaveCriticalSection( &request
->cs
);
4091 switch ((err
= request_wait( request
, msecs
)))
4094 if (succeeded
) *succeeded
= VARIANT_FALSE
;
4095 err
= ERROR_SUCCESS
;
4099 if (succeeded
) *succeeded
= VARIANT_TRUE
;
4104 LeaveCriticalSection( &request
->cs
);
4105 return HRESULT_FROM_WIN32( err
);
4108 static HRESULT WINAPI
winhttp_request_Abort(
4109 IWinHttpRequest
*iface
)
4111 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
4113 TRACE("%p\n", request
);
4115 EnterCriticalSection( &request
->cs
);
4116 cancel_request( request
);
4117 LeaveCriticalSection( &request
->cs
);
4121 static HRESULT WINAPI
winhttp_request_SetTimeouts(
4122 IWinHttpRequest
*iface
,
4123 LONG resolve_timeout
,
4124 LONG connect_timeout
,
4126 LONG receive_timeout
)
4128 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
4130 TRACE("%p, %d, %d, %d, %d\n", request
, resolve_timeout
, connect_timeout
, send_timeout
, receive_timeout
);
4132 EnterCriticalSection( &request
->cs
);
4133 request
->resolve_timeout
= resolve_timeout
;
4134 request
->connect_timeout
= connect_timeout
;
4135 request
->send_timeout
= send_timeout
;
4136 request
->receive_timeout
= receive_timeout
;
4137 LeaveCriticalSection( &request
->cs
);
4141 static HRESULT WINAPI
winhttp_request_SetClientCertificate(
4142 IWinHttpRequest
*iface
,
4149 static HRESULT WINAPI
winhttp_request_SetAutoLogonPolicy(
4150 IWinHttpRequest
*iface
,
4151 WinHttpRequestAutoLogonPolicy policy
)
4153 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
4156 TRACE("%p, %u\n", request
, policy
);
4158 EnterCriticalSection( &request
->cs
);
4161 case AutoLogonPolicy_Always
:
4162 request
->logon_policy
= WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW
;
4164 case AutoLogonPolicy_OnlyIfBypassProxy
:
4165 request
->logon_policy
= WINHTTP_AUTOLOGON_SECURITY_LEVEL_MEDIUM
;
4167 case AutoLogonPolicy_Never
:
4168 request
->logon_policy
= WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH
;
4170 default: hr
= E_INVALIDARG
;
4173 LeaveCriticalSection( &request
->cs
);
4177 static const struct IWinHttpRequestVtbl winhttp_request_vtbl
=
4179 winhttp_request_QueryInterface
,
4180 winhttp_request_AddRef
,
4181 winhttp_request_Release
,
4182 winhttp_request_GetTypeInfoCount
,
4183 winhttp_request_GetTypeInfo
,
4184 winhttp_request_GetIDsOfNames
,
4185 winhttp_request_Invoke
,
4186 winhttp_request_SetProxy
,
4187 winhttp_request_SetCredentials
,
4188 winhttp_request_Open
,
4189 winhttp_request_SetRequestHeader
,
4190 winhttp_request_GetResponseHeader
,
4191 winhttp_request_GetAllResponseHeaders
,
4192 winhttp_request_Send
,
4193 winhttp_request_get_Status
,
4194 winhttp_request_get_StatusText
,
4195 winhttp_request_get_ResponseText
,
4196 winhttp_request_get_ResponseBody
,
4197 winhttp_request_get_ResponseStream
,
4198 winhttp_request_get_Option
,
4199 winhttp_request_put_Option
,
4200 winhttp_request_WaitForResponse
,
4201 winhttp_request_Abort
,
4202 winhttp_request_SetTimeouts
,
4203 winhttp_request_SetClientCertificate
,
4204 winhttp_request_SetAutoLogonPolicy
4207 HRESULT
WinHttpRequest_create( void **obj
)
4209 struct winhttp_request
*request
;
4213 if (!(request
= heap_alloc( sizeof(*request
) ))) return E_OUTOFMEMORY
;
4214 request
->IWinHttpRequest_iface
.lpVtbl
= &winhttp_request_vtbl
;
4216 request
->state
= REQUEST_STATE_UNINITIALIZED
;
4217 request
->proxy
.lpszProxy
= NULL
;
4218 request
->proxy
.lpszProxyBypass
= NULL
;
4219 InitializeCriticalSection( &request
->cs
);
4220 request
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": winhttp_request.cs");
4222 *obj
= &request
->IWinHttpRequest_iface
;
4223 TRACE("returning iface %p\n", *obj
);