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
22 #include "winhttp_private.h"
24 #ifdef HAVE_ARPA_INET_H
25 # include <arpa/inet.h>
30 #include <httprequest.h>
31 #include <httprequestid.h>
33 #include "inet_ntop.c"
35 static const WCHAR attr_accept
[] = {'A','c','c','e','p','t',0};
36 static const WCHAR attr_accept_charset
[] = {'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0};
37 static const WCHAR attr_accept_encoding
[] = {'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0};
38 static const WCHAR attr_accept_language
[] = {'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0};
39 static const WCHAR attr_accept_ranges
[] = {'A','c','c','e','p','t','-','R','a','n','g','e','s',0};
40 static const WCHAR attr_age
[] = {'A','g','e',0};
41 static const WCHAR attr_allow
[] = {'A','l','l','o','w',0};
42 static const WCHAR attr_authorization
[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',0};
43 static const WCHAR attr_cache_control
[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l',0};
44 static const WCHAR attr_connection
[] = {'C','o','n','n','e','c','t','i','o','n',0};
45 static const WCHAR attr_content_base
[] = {'C','o','n','t','e','n','t','-','B','a','s','e',0};
46 static const WCHAR attr_content_encoding
[] = {'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0};
47 static const WCHAR attr_content_id
[] = {'C','o','n','t','e','n','t','-','I','D',0};
48 static const WCHAR attr_content_language
[] = {'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0};
49 static const WCHAR attr_content_length
[] = {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
50 static const WCHAR attr_content_location
[] = {'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0};
51 static const WCHAR attr_content_md5
[] = {'C','o','n','t','e','n','t','-','M','D','5',0};
52 static const WCHAR attr_content_range
[] = {'C','o','n','t','e','n','t','-','R','a','n','g','e',0};
53 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};
54 static const WCHAR attr_content_type
[] = {'C','o','n','t','e','n','t','-','T','y','p','e',0};
55 static const WCHAR attr_cookie
[] = {'C','o','o','k','i','e',0};
56 static const WCHAR attr_date
[] = {'D','a','t','e',0};
57 static const WCHAR attr_from
[] = {'F','r','o','m',0};
58 static const WCHAR attr_etag
[] = {'E','T','a','g',0};
59 static const WCHAR attr_expect
[] = {'E','x','p','e','c','t',0};
60 static const WCHAR attr_expires
[] = {'E','x','p','i','r','e','s',0};
61 static const WCHAR attr_host
[] = {'H','o','s','t',0};
62 static const WCHAR attr_if_match
[] = {'I','f','-','M','a','t','c','h',0};
63 static const WCHAR attr_if_modified_since
[] = {'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0};
64 static const WCHAR attr_if_none_match
[] = {'I','f','-','N','o','n','e','-','M','a','t','c','h',0};
65 static const WCHAR attr_if_range
[] = {'I','f','-','R','a','n','g','e',0};
66 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};
67 static const WCHAR attr_last_modified
[] = {'L','a','s','t','-','M','o','d','i','f','i','e','d',0};
68 static const WCHAR attr_location
[] = {'L','o','c','a','t','i','o','n',0};
69 static const WCHAR attr_max_forwards
[] = {'M','a','x','-','F','o','r','w','a','r','d','s',0};
70 static const WCHAR attr_mime_version
[] = {'M','i','m','e','-','V','e','r','s','i','o','n',0};
71 static const WCHAR attr_pragma
[] = {'P','r','a','g','m','a',0};
72 static const WCHAR attr_proxy_authenticate
[] = {'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0};
73 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};
74 static const WCHAR attr_proxy_connection
[] = {'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0};
75 static const WCHAR attr_public
[] = {'P','u','b','l','i','c',0};
76 static const WCHAR attr_range
[] = {'R','a','n','g','e',0};
77 static const WCHAR attr_referer
[] = {'R','e','f','e','r','e','r',0};
78 static const WCHAR attr_retry_after
[] = {'R','e','t','r','y','-','A','f','t','e','r',0};
79 static const WCHAR attr_server
[] = {'S','e','r','v','e','r',0};
80 static const WCHAR attr_set_cookie
[] = {'S','e','t','-','C','o','o','k','i','e',0};
81 static const WCHAR attr_status
[] = {'S','t','a','t','u','s',0};
82 static const WCHAR attr_transfer_encoding
[] = {'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0};
83 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};
84 static const WCHAR attr_upgrade
[] = {'U','p','g','r','a','d','e',0};
85 static const WCHAR attr_uri
[] = {'U','R','I',0};
86 static const WCHAR attr_user_agent
[] = {'U','s','e','r','-','A','g','e','n','t',0};
87 static const WCHAR attr_vary
[] = {'V','a','r','y',0};
88 static const WCHAR attr_via
[] = {'V','i','a',0};
89 static const WCHAR attr_warning
[] = {'W','a','r','n','i','n','g',0};
90 static const WCHAR attr_www_authenticate
[] = {'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0};
92 static const WCHAR
*attribute_table
[] =
94 attr_mime_version
, /* WINHTTP_QUERY_MIME_VERSION = 0 */
95 attr_content_type
, /* WINHTTP_QUERY_CONTENT_TYPE = 1 */
96 attr_content_transfer_encoding
, /* WINHTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
97 attr_content_id
, /* WINHTTP_QUERY_CONTENT_ID = 3 */
98 NULL
, /* WINHTTP_QUERY_CONTENT_DESCRIPTION = 4 */
99 attr_content_length
, /* WINHTTP_QUERY_CONTENT_LENGTH = 5 */
100 attr_content_language
, /* WINHTTP_QUERY_CONTENT_LANGUAGE = 6 */
101 attr_allow
, /* WINHTTP_QUERY_ALLOW = 7 */
102 attr_public
, /* WINHTTP_QUERY_PUBLIC = 8 */
103 attr_date
, /* WINHTTP_QUERY_DATE = 9 */
104 attr_expires
, /* WINHTTP_QUERY_EXPIRES = 10 */
105 attr_last_modified
, /* WINHTTP_QUERY_LAST_MODIFIEDcw = 11 */
106 NULL
, /* WINHTTP_QUERY_MESSAGE_ID = 12 */
107 attr_uri
, /* WINHTTP_QUERY_URI = 13 */
108 attr_from
, /* WINHTTP_QUERY_DERIVED_FROM = 14 */
109 NULL
, /* WINHTTP_QUERY_COST = 15 */
110 NULL
, /* WINHTTP_QUERY_LINK = 16 */
111 attr_pragma
, /* WINHTTP_QUERY_PRAGMA = 17 */
112 NULL
, /* WINHTTP_QUERY_VERSION = 18 */
113 attr_status
, /* WINHTTP_QUERY_STATUS_CODE = 19 */
114 NULL
, /* WINHTTP_QUERY_STATUS_TEXT = 20 */
115 NULL
, /* WINHTTP_QUERY_RAW_HEADERS = 21 */
116 NULL
, /* WINHTTP_QUERY_RAW_HEADERS_CRLF = 22 */
117 attr_connection
, /* WINHTTP_QUERY_CONNECTION = 23 */
118 attr_accept
, /* WINHTTP_QUERY_ACCEPT = 24 */
119 attr_accept_charset
, /* WINHTTP_QUERY_ACCEPT_CHARSET = 25 */
120 attr_accept_encoding
, /* WINHTTP_QUERY_ACCEPT_ENCODING = 26 */
121 attr_accept_language
, /* WINHTTP_QUERY_ACCEPT_LANGUAGE = 27 */
122 attr_authorization
, /* WINHTTP_QUERY_AUTHORIZATION = 28 */
123 attr_content_encoding
, /* WINHTTP_QUERY_CONTENT_ENCODING = 29 */
124 NULL
, /* WINHTTP_QUERY_FORWARDED = 30 */
125 NULL
, /* WINHTTP_QUERY_FROM = 31 */
126 attr_if_modified_since
, /* WINHTTP_QUERY_IF_MODIFIED_SINCE = 32 */
127 attr_location
, /* WINHTTP_QUERY_LOCATION = 33 */
128 NULL
, /* WINHTTP_QUERY_ORIG_URI = 34 */
129 attr_referer
, /* WINHTTP_QUERY_REFERER = 35 */
130 attr_retry_after
, /* WINHTTP_QUERY_RETRY_AFTER = 36 */
131 attr_server
, /* WINHTTP_QUERY_SERVER = 37 */
132 NULL
, /* WINHTTP_TITLE = 38 */
133 attr_user_agent
, /* WINHTTP_QUERY_USER_AGENT = 39 */
134 attr_www_authenticate
, /* WINHTTP_QUERY_WWW_AUTHENTICATE = 40 */
135 attr_proxy_authenticate
, /* WINHTTP_QUERY_PROXY_AUTHENTICATE = 41 */
136 attr_accept_ranges
, /* WINHTTP_QUERY_ACCEPT_RANGES = 42 */
137 attr_set_cookie
, /* WINHTTP_QUERY_SET_COOKIE = 43 */
138 attr_cookie
, /* WINHTTP_QUERY_COOKIE = 44 */
139 NULL
, /* WINHTTP_QUERY_REQUEST_METHOD = 45 */
140 NULL
, /* WINHTTP_QUERY_REFRESH = 46 */
141 NULL
, /* WINHTTP_QUERY_CONTENT_DISPOSITION = 47 */
142 attr_age
, /* WINHTTP_QUERY_AGE = 48 */
143 attr_cache_control
, /* WINHTTP_QUERY_CACHE_CONTROL = 49 */
144 attr_content_base
, /* WINHTTP_QUERY_CONTENT_BASE = 50 */
145 attr_content_location
, /* WINHTTP_QUERY_CONTENT_LOCATION = 51 */
146 attr_content_md5
, /* WINHTTP_QUERY_CONTENT_MD5 = 52 */
147 attr_content_range
, /* WINHTTP_QUERY_CONTENT_RANGE = 53 */
148 attr_etag
, /* WINHTTP_QUERY_ETAG = 54 */
149 attr_host
, /* WINHTTP_QUERY_HOST = 55 */
150 attr_if_match
, /* WINHTTP_QUERY_IF_MATCH = 56 */
151 attr_if_none_match
, /* WINHTTP_QUERY_IF_NONE_MATCH = 57 */
152 attr_if_range
, /* WINHTTP_QUERY_IF_RANGE = 58 */
153 attr_if_unmodified_since
, /* WINHTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
154 attr_max_forwards
, /* WINHTTP_QUERY_MAX_FORWARDS = 60 */
155 attr_proxy_authorization
, /* WINHTTP_QUERY_PROXY_AUTHORIZATION = 61 */
156 attr_range
, /* WINHTTP_QUERY_RANGE = 62 */
157 attr_transfer_encoding
, /* WINHTTP_QUERY_TRANSFER_ENCODING = 63 */
158 attr_upgrade
, /* WINHTTP_QUERY_UPGRADE = 64 */
159 attr_vary
, /* WINHTTP_QUERY_VARY = 65 */
160 attr_via
, /* WINHTTP_QUERY_VIA = 66 */
161 attr_warning
, /* WINHTTP_QUERY_WARNING = 67 */
162 attr_expect
, /* WINHTTP_QUERY_EXPECT = 68 */
163 attr_proxy_connection
, /* WINHTTP_QUERY_PROXY_CONNECTION = 69 */
164 attr_unless_modified_since
, /* WINHTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
165 NULL
, /* WINHTTP_QUERY_PROXY_SUPPORT = 75 */
166 NULL
, /* WINHTTP_QUERY_AUTHENTICATION_INFO = 76 */
167 NULL
, /* WINHTTP_QUERY_PASSPORT_URLS = 77 */
168 NULL
/* WINHTTP_QUERY_PASSPORT_CONFIG = 78 */
171 static task_header_t
*dequeue_task( request_t
*request
)
175 EnterCriticalSection( &request
->task_cs
);
176 TRACE("%u tasks queued\n", list_count( &request
->task_queue
));
177 task
= LIST_ENTRY( list_head( &request
->task_queue
), task_header_t
, entry
);
178 if (task
) list_remove( &task
->entry
);
179 LeaveCriticalSection( &request
->task_cs
);
181 TRACE("returning task %p\n", task
);
185 static DWORD CALLBACK
task_proc( LPVOID param
)
187 request_t
*request
= param
;
190 handles
[0] = request
->task_wait
;
191 handles
[1] = request
->task_cancel
;
194 DWORD err
= WaitForMultipleObjects( 2, handles
, FALSE
, INFINITE
);
200 while ((task
= dequeue_task( request
)))
203 release_object( &task
->request
->hdr
);
208 case WAIT_OBJECT_0
+ 1:
210 CloseHandle( request
->task_cancel
);
211 CloseHandle( request
->task_wait
);
212 request
->task_cs
.DebugInfo
->Spare
[0] = 0;
213 DeleteCriticalSection( &request
->task_cs
);
214 request
->hdr
.vtbl
->destroy( &request
->hdr
);
218 ERR("wait failed %u (%u)\n", err
, GetLastError());
225 static BOOL
queue_task( task_header_t
*task
)
227 request_t
*request
= task
->request
;
229 if (!request
->task_thread
)
231 if (!(request
->task_wait
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
))) return FALSE
;
232 if (!(request
->task_cancel
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
)))
234 CloseHandle( request
->task_wait
);
235 request
->task_wait
= NULL
;
238 if (!(request
->task_thread
= CreateThread( NULL
, 0, task_proc
, request
, 0, NULL
)))
240 CloseHandle( request
->task_wait
);
241 request
->task_wait
= NULL
;
242 CloseHandle( request
->task_cancel
);
243 request
->task_cancel
= NULL
;
246 InitializeCriticalSection( &request
->task_cs
);
247 request
->task_cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": request.task_cs");
250 EnterCriticalSection( &request
->task_cs
);
251 TRACE("queueing task %p\n", task
);
252 list_add_tail( &request
->task_queue
, &task
->entry
);
253 LeaveCriticalSection( &request
->task_cs
);
255 SetEvent( request
->task_wait
);
259 static void free_header( header_t
*header
)
261 heap_free( header
->field
);
262 heap_free( header
->value
);
266 static BOOL
valid_token_char( WCHAR c
)
268 if (c
< 32 || c
== 127) return FALSE
;
275 case '\\': case '\"':
287 static header_t
*parse_header( LPCWSTR string
)
294 if (!(q
= strchrW( p
, ':' )))
296 WARN("no ':' in line %s\n", debugstr_w(string
));
301 WARN("empty field name in line %s\n", debugstr_w(string
));
306 if (!valid_token_char( *p
))
308 WARN("invalid character in field name %s\n", debugstr_w(string
));
314 if (!(header
= heap_alloc_zero( sizeof(header_t
) ))) return NULL
;
315 if (!(header
->field
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) )))
320 memcpy( header
->field
, string
, len
* sizeof(WCHAR
) );
321 header
->field
[len
] = 0;
323 q
++; /* skip past colon */
324 while (*q
== ' ') q
++;
327 if (!(header
->value
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) )))
329 free_header( header
);
332 memcpy( header
->value
, q
, len
* sizeof(WCHAR
) );
333 header
->value
[len
] = 0;
338 static int get_header_index( request_t
*request
, LPCWSTR field
, int requested_index
, BOOL request_only
)
342 TRACE("%s\n", debugstr_w(field
));
344 for (index
= 0; index
< request
->num_headers
; index
++)
346 if (strcmpiW( request
->headers
[index
].field
, field
)) continue;
347 if (request_only
&& !request
->headers
[index
].is_request
) continue;
348 if (!request_only
&& request
->headers
[index
].is_request
) continue;
350 if (!requested_index
) break;
353 if (index
>= request
->num_headers
) index
= -1;
354 TRACE("returning %d\n", index
);
358 static BOOL
insert_header( request_t
*request
, header_t
*header
)
363 count
= request
->num_headers
+ 1;
365 hdrs
= heap_realloc_zero( request
->headers
, sizeof(header_t
) * count
);
367 hdrs
= heap_alloc_zero( sizeof(header_t
) * count
);
371 request
->headers
= hdrs
;
372 request
->headers
[count
- 1].field
= strdupW( header
->field
);
373 request
->headers
[count
- 1].value
= strdupW( header
->value
);
374 request
->headers
[count
- 1].is_request
= header
->is_request
;
375 request
->num_headers
++;
381 static BOOL
delete_header( request_t
*request
, DWORD index
)
383 if (!request
->num_headers
) return FALSE
;
384 if (index
>= request
->num_headers
) return FALSE
;
385 request
->num_headers
--;
387 heap_free( request
->headers
[index
].field
);
388 heap_free( request
->headers
[index
].value
);
390 memmove( &request
->headers
[index
], &request
->headers
[index
+ 1], (request
->num_headers
- index
) * sizeof(header_t
) );
391 memset( &request
->headers
[request
->num_headers
], 0, sizeof(header_t
) );
395 static BOOL
process_header( request_t
*request
, LPCWSTR field
, LPCWSTR value
, DWORD flags
, BOOL request_only
)
400 TRACE("%s: %s 0x%08x\n", debugstr_w(field
), debugstr_w(value
), flags
);
402 if ((index
= get_header_index( request
, field
, 0, request_only
)) >= 0)
404 if (flags
& WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
) return FALSE
;
407 if (flags
& WINHTTP_ADDREQ_FLAG_REPLACE
)
411 delete_header( request
, index
);
412 if (!value
|| !value
[0]) return TRUE
;
414 else if (!(flags
& WINHTTP_ADDREQ_FLAG_ADD
))
416 set_last_error( ERROR_WINHTTP_HEADER_NOT_FOUND
);
420 hdr
.field
= (LPWSTR
)field
;
421 hdr
.value
= (LPWSTR
)value
;
422 hdr
.is_request
= request_only
;
423 return insert_header( request
, &hdr
);
428 if ((flags
& (WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA
| WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON
)) &&
432 int len
, len_orig
, len_value
;
433 header_t
*header
= &request
->headers
[index
];
435 len_orig
= strlenW( header
->value
);
436 len_value
= strlenW( value
);
438 len
= len_orig
+ len_value
+ 2;
439 if (!(tmp
= heap_realloc( header
->value
, (len
+ 1) * sizeof(WCHAR
) ))) return FALSE
;
441 header
->value
[len_orig
++] = (flags
& WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA
) ? ',' : ';';
442 header
->value
[len_orig
++] = ' ';
444 memcpy( &header
->value
[len_orig
], value
, len_value
* sizeof(WCHAR
) );
445 header
->value
[len
] = 0;
450 hdr
.field
= (LPWSTR
)field
;
451 hdr
.value
= (LPWSTR
)value
;
452 hdr
.is_request
= request_only
;
453 return insert_header( request
, &hdr
);
460 BOOL
add_request_headers( request_t
*request
, LPCWSTR headers
, DWORD len
, DWORD flags
)
463 WCHAR
*buffer
, *p
, *q
;
466 if (len
== ~0u) len
= strlenW( headers
);
467 if (!len
) return TRUE
;
468 if (!(buffer
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) return FALSE
;
469 memcpy( buffer
, headers
, len
* sizeof(WCHAR
) );
478 if (q
[0] == '\n' && q
[1] == '\r')
483 if (q
[0] == '\r' && q
[1] == '\n') break;
490 q
+= 2; /* jump over \r\n */
492 if ((header
= parse_header( p
)))
494 ret
= process_header( request
, header
->field
, header
->value
, flags
, TRUE
);
495 free_header( header
);
504 /***********************************************************************
505 * WinHttpAddRequestHeaders (winhttp.@)
507 BOOL WINAPI
WinHttpAddRequestHeaders( HINTERNET hrequest
, LPCWSTR headers
, DWORD len
, DWORD flags
)
512 TRACE("%p, %s, 0x%x, 0x%08x\n", hrequest
, debugstr_w(headers
), len
, flags
);
514 if (!headers
|| !len
)
516 set_last_error( ERROR_INVALID_PARAMETER
);
519 if (!(request
= (request_t
*)grab_object( hrequest
)))
521 set_last_error( ERROR_INVALID_HANDLE
);
524 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
526 release_object( &request
->hdr
);
527 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
531 ret
= add_request_headers( request
, headers
, len
, flags
);
533 release_object( &request
->hdr
);
534 if (ret
) set_last_error( ERROR_SUCCESS
);
538 static WCHAR
*build_request_path( request_t
*request
)
542 if (strcmpiW( request
->connect
->hostname
, request
->connect
->servername
))
544 static const WCHAR http
[] = { 'h','t','t','p',0 };
545 static const WCHAR https
[] = { 'h','t','t','p','s',0 };
546 static const WCHAR fmt
[] = { '%','s',':','/','/','%','s',0 };
547 LPCWSTR scheme
= request
->netconn
.secure
? https
: http
;
550 len
= strlenW( scheme
) + strlenW( request
->connect
->hostname
);
551 /* 3 characters for '://', 1 for NUL. */
553 if (request
->connect
->hostport
)
555 /* 1 for ':' between host and port, up to 5 for port */
559 len
+= strlenW( request
->path
);
560 if ((ret
= heap_alloc( len
* sizeof(WCHAR
) )))
562 sprintfW( ret
, fmt
, scheme
, request
->connect
->hostname
);
563 if (request
->connect
->hostport
)
565 static const WCHAR colonFmt
[] = { ':','%','u',0 };
567 sprintfW( ret
+ strlenW( ret
), colonFmt
,
568 request
->connect
->hostport
);
571 strcatW( ret
, request
->path
);
579 static WCHAR
*build_request_string( request_t
*request
)
581 static const WCHAR space
[] = {' ',0};
582 static const WCHAR crlf
[] = {'\r','\n',0};
583 static const WCHAR colon
[] = {':',' ',0};
584 static const WCHAR twocrlf
[] = {'\r','\n','\r','\n',0};
587 const WCHAR
**headers
, **p
;
588 unsigned int len
, i
= 0, j
;
590 /* allocate space for an array of all the string pointers to be added */
591 len
= request
->num_headers
* 4 + 7;
592 if (!(headers
= heap_alloc( len
* sizeof(LPCWSTR
) ))) return NULL
;
594 path
= build_request_path( request
);
595 headers
[i
++] = request
->verb
;
596 headers
[i
++] = space
;
598 headers
[i
++] = space
;
599 headers
[i
++] = request
->version
;
601 for (j
= 0; j
< request
->num_headers
; j
++)
603 if (request
->headers
[j
].is_request
)
606 headers
[i
++] = request
->headers
[j
].field
;
607 headers
[i
++] = colon
;
608 headers
[i
++] = request
->headers
[j
].value
;
610 TRACE("adding header %s (%s)\n", debugstr_w(request
->headers
[j
].field
),
611 debugstr_w(request
->headers
[j
].value
));
614 headers
[i
++] = twocrlf
;
618 for (p
= headers
; *p
; p
++) len
+= strlenW( *p
);
621 if (!(ret
= heap_alloc( len
* sizeof(WCHAR
) )))
624 for (p
= headers
; *p
; p
++) strcatW( ret
, *p
);
627 if (path
!= request
->path
)
629 heap_free( headers
);
633 #define QUERY_MODIFIER_MASK (WINHTTP_QUERY_FLAG_REQUEST_HEADERS | WINHTTP_QUERY_FLAG_SYSTEMTIME | WINHTTP_QUERY_FLAG_NUMBER)
635 static BOOL
query_headers( request_t
*request
, DWORD level
, LPCWSTR name
, LPVOID buffer
, LPDWORD buflen
, LPDWORD index
)
637 header_t
*header
= NULL
;
638 BOOL request_only
, ret
= FALSE
;
639 int requested_index
, header_index
= -1;
642 request_only
= level
& WINHTTP_QUERY_FLAG_REQUEST_HEADERS
;
643 requested_index
= index
? *index
: 0;
645 attr
= level
& ~QUERY_MODIFIER_MASK
;
648 case WINHTTP_QUERY_CUSTOM
:
650 header_index
= get_header_index( request
, name
, requested_index
, request_only
);
653 case WINHTTP_QUERY_RAW_HEADERS
:
655 WCHAR
*headers
, *p
, *q
;
658 headers
= build_request_string( request
);
660 headers
= request
->raw_headers
;
662 if (!(p
= headers
)) return FALSE
;
663 for (len
= 0; *p
; p
++) if (*p
!= '\r') len
++;
665 if (!buffer
|| len
* sizeof(WCHAR
) > *buflen
)
666 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
669 for (p
= headers
, q
= buffer
; *p
; p
++, q
++)
671 if (*p
!= '\r') *q
= *p
;
678 TRACE("returning data: %s\n", debugstr_wn(buffer
, len
));
682 *buflen
= len
* sizeof(WCHAR
);
683 if (request_only
) heap_free( headers
);
686 case WINHTTP_QUERY_RAW_HEADERS_CRLF
:
691 headers
= build_request_string( request
);
693 headers
= request
->raw_headers
;
695 if (!headers
) return FALSE
;
696 len
= strlenW( headers
) * sizeof(WCHAR
);
697 if (!buffer
|| len
+ sizeof(WCHAR
) > *buflen
)
699 len
+= sizeof(WCHAR
);
700 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
704 memcpy( buffer
, headers
, len
+ sizeof(WCHAR
) );
705 TRACE("returning data: %s\n", debugstr_wn(buffer
, len
/ sizeof(WCHAR
)));
709 if (request_only
) heap_free( headers
);
712 case WINHTTP_QUERY_VERSION
:
713 len
= strlenW( request
->version
) * sizeof(WCHAR
);
714 if (!buffer
|| len
+ sizeof(WCHAR
) > *buflen
)
716 len
+= sizeof(WCHAR
);
717 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
721 strcpyW( buffer
, request
->version
);
722 TRACE("returning string: %s\n", debugstr_w(buffer
));
728 case WINHTTP_QUERY_STATUS_TEXT
:
729 len
= strlenW( request
->status_text
) * sizeof(WCHAR
);
730 if (!buffer
|| len
+ sizeof(WCHAR
) > *buflen
)
732 len
+= sizeof(WCHAR
);
733 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
737 strcpyW( buffer
, request
->status_text
);
738 TRACE("returning string: %s\n", debugstr_w(buffer
));
745 if (attr
>= sizeof(attribute_table
)/sizeof(attribute_table
[0]) || !attribute_table
[attr
])
747 FIXME("attribute %u not implemented\n", attr
);
750 TRACE("attribute %s\n", debugstr_w(attribute_table
[attr
]));
751 header_index
= get_header_index( request
, attribute_table
[attr
], requested_index
, request_only
);
755 if (header_index
>= 0)
757 header
= &request
->headers
[header_index
];
759 if (!header
|| (request_only
&& !header
->is_request
))
761 set_last_error( ERROR_WINHTTP_HEADER_NOT_FOUND
);
764 if (index
) *index
+= 1;
765 if (level
& WINHTTP_QUERY_FLAG_NUMBER
)
767 if (!buffer
|| sizeof(int) > *buflen
)
769 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
773 int *number
= buffer
;
774 *number
= atoiW( header
->value
);
775 TRACE("returning number: %d\n", *number
);
778 *buflen
= sizeof(int);
780 else if (level
& WINHTTP_QUERY_FLAG_SYSTEMTIME
)
782 SYSTEMTIME
*st
= buffer
;
783 if (!buffer
|| sizeof(SYSTEMTIME
) > *buflen
)
785 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
787 else if ((ret
= WinHttpTimeToSystemTime( header
->value
, st
)))
789 TRACE("returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
790 st
->wYear
, st
->wMonth
, st
->wDay
, st
->wDayOfWeek
,
791 st
->wHour
, st
->wMinute
, st
->wSecond
, st
->wMilliseconds
);
793 *buflen
= sizeof(SYSTEMTIME
);
795 else if (header
->value
)
797 len
= strlenW( header
->value
) * sizeof(WCHAR
);
798 if (!buffer
|| len
+ sizeof(WCHAR
) > *buflen
)
800 len
+= sizeof(WCHAR
);
801 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
805 strcpyW( buffer
, header
->value
);
806 TRACE("returning string: %s\n", debugstr_w(buffer
));
814 /***********************************************************************
815 * WinHttpQueryHeaders (winhttp.@)
817 BOOL WINAPI
WinHttpQueryHeaders( HINTERNET hrequest
, DWORD level
, LPCWSTR name
, LPVOID buffer
, LPDWORD buflen
, LPDWORD index
)
822 TRACE("%p, 0x%08x, %s, %p, %p, %p\n", hrequest
, level
, debugstr_w(name
), buffer
, buflen
, index
);
824 if (!(request
= (request_t
*)grab_object( hrequest
)))
826 set_last_error( ERROR_INVALID_HANDLE
);
829 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
831 release_object( &request
->hdr
);
832 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
836 ret
= query_headers( request
, level
, name
, buffer
, buflen
, index
);
838 release_object( &request
->hdr
);
839 if (ret
) set_last_error( ERROR_SUCCESS
);
843 static LPWSTR
concatenate_string_list( LPCWSTR
*list
, int len
)
848 for( t
= list
; *t
; t
++ )
849 len
+= strlenW( *t
);
852 str
= heap_alloc( len
* sizeof(WCHAR
) );
853 if (!str
) return NULL
;
856 for( t
= list
; *t
; t
++ )
862 static LPWSTR
build_header_request_string( request_t
*request
, LPCWSTR verb
,
863 LPCWSTR path
, LPCWSTR version
)
865 static const WCHAR crlf
[] = {'\r','\n',0};
866 static const WCHAR space
[] = { ' ',0 };
867 static const WCHAR colon
[] = { ':',' ',0 };
868 static const WCHAR twocrlf
[] = {'\r','\n','\r','\n', 0};
869 LPWSTR requestString
;
875 /* allocate space for an array of all the string pointers to be added */
876 len
= (request
->num_headers
) * 4 + 10;
877 req
= heap_alloc( len
* sizeof(LPCWSTR
) );
878 if (!req
) return NULL
;
880 /* add the verb, path and HTTP version string */
888 /* Append custom request headers */
889 for (i
= 0; i
< request
->num_headers
; i
++)
891 if (request
->headers
[i
].is_request
)
894 req
[n
++] = request
->headers
[i
].field
;
896 req
[n
++] = request
->headers
[i
].value
;
898 TRACE("Adding custom header %s (%s)\n",
899 debugstr_w(request
->headers
[i
].field
),
900 debugstr_w(request
->headers
[i
].value
));
905 ERR("oops. buffer overrun\n");
908 requestString
= concatenate_string_list( req
, 4 );
910 if (!requestString
) return NULL
;
913 * Set (header) termination string for request
914 * Make sure there are exactly two new lines at the end of the request
916 p
= &requestString
[strlenW(requestString
)-1];
917 while ( (*p
== '\n') || (*p
== '\r') )
919 strcpyW( p
+1, twocrlf
);
921 return requestString
;
924 static BOOL
read_reply( request_t
*request
);
926 static BOOL
secure_proxy_connect( request_t
*request
)
928 static const WCHAR verbConnect
[] = {'C','O','N','N','E','C','T',0};
929 static const WCHAR fmt
[] = {'%','s',':','%','u',0};
932 connect_t
*connect
= request
->connect
;
934 path
= heap_alloc( (strlenW( connect
->hostname
) + 13) * sizeof(WCHAR
) );
937 LPWSTR requestString
;
939 sprintfW( path
, fmt
, connect
->hostname
, connect
->hostport
);
940 requestString
= build_header_request_string( request
, verbConnect
,
945 LPSTR req_ascii
= strdupWA( requestString
);
947 heap_free( requestString
);
950 int len
= strlen( req_ascii
), bytes_sent
;
952 ret
= netconn_send( &request
->netconn
, req_ascii
, len
, &bytes_sent
);
953 heap_free( req_ascii
);
955 ret
= read_reply( request
);
962 #ifndef INET6_ADDRSTRLEN
963 #define INET6_ADDRSTRLEN 46
966 static WCHAR
*addr_to_str( struct sockaddr
*addr
)
968 char buf
[INET6_ADDRSTRLEN
];
971 switch (addr
->sa_family
)
974 src
= &((struct sockaddr_in
*)addr
)->sin_addr
;
977 src
= &((struct sockaddr_in6
*)addr
)->sin6_addr
;
980 WARN("unsupported address family %d\n", addr
->sa_family
);
983 if (!inet_ntop( addr
->sa_family
, src
, buf
, sizeof(buf
) )) return NULL
;
984 return strdupAW( buf
);
987 static BOOL
open_connection( request_t
*request
)
990 WCHAR
*addressW
= NULL
;
993 struct sockaddr
*saddr
;
996 if (netconn_connected( &request
->netconn
)) goto done
;
998 connect
= request
->connect
;
999 port
= connect
->serverport
? connect
->serverport
: (request
->hdr
.flags
& WINHTTP_FLAG_SECURE
? 443 : 80);
1000 saddr
= (struct sockaddr
*)&connect
->sockaddr
;
1001 slen
= sizeof(struct sockaddr
);
1003 if (!connect
->resolved
)
1005 len
= strlenW( connect
->servername
) + 1;
1006 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_RESOLVING_NAME
, connect
->servername
, len
);
1008 if (!netconn_resolve( connect
->servername
, port
, saddr
, &slen
, request
->resolve_timeout
)) return FALSE
;
1009 connect
->resolved
= TRUE
;
1011 if (!(addressW
= addr_to_str( saddr
))) return FALSE
;
1012 len
= strlenW( addressW
) + 1;
1013 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_NAME_RESOLVED
, addressW
, len
);
1015 if (!addressW
&& !(addressW
= addr_to_str( saddr
))) return FALSE
;
1016 TRACE("connecting to %s:%u\n", debugstr_w(addressW
), port
);
1018 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER
, addressW
, 0 );
1020 if (!netconn_create( &request
->netconn
, saddr
->sa_family
, SOCK_STREAM
, 0 ))
1022 heap_free( addressW
);
1025 netconn_set_timeout( &request
->netconn
, TRUE
, request
->send_timeout
);
1026 netconn_set_timeout( &request
->netconn
, FALSE
, request
->recv_timeout
);
1027 if (!netconn_connect( &request
->netconn
, saddr
, slen
, request
->connect_timeout
))
1029 netconn_close( &request
->netconn
);
1030 heap_free( addressW
);
1033 if (request
->hdr
.flags
& WINHTTP_FLAG_SECURE
)
1035 if (connect
->session
->proxy_server
&&
1036 strcmpiW( connect
->hostname
, connect
->servername
))
1038 if (!secure_proxy_connect( request
))
1040 heap_free( addressW
);
1044 if (!netconn_secure_connect( &request
->netconn
, connect
->hostname
))
1046 netconn_close( &request
->netconn
);
1047 heap_free( addressW
);
1052 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER
, addressW
, strlenW(addressW
) + 1 );
1055 request
->read_pos
= request
->read_size
= 0;
1056 request
->read_chunked
= FALSE
;
1057 request
->read_chunked_size
= ~0u;
1058 request
->read_chunked_eof
= FALSE
;
1059 heap_free( addressW
);
1063 void close_connection( request_t
*request
)
1065 if (!netconn_connected( &request
->netconn
)) return;
1067 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION
, 0, 0 );
1068 netconn_close( &request
->netconn
);
1069 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED
, 0, 0 );
1072 static BOOL
add_host_header( request_t
*request
, DWORD modifier
)
1077 static const WCHAR fmt
[] = {'%','s',':','%','u',0};
1078 connect_t
*connect
= request
->connect
;
1081 port
= connect
->hostport
? connect
->hostport
: (request
->hdr
.flags
& WINHTTP_FLAG_SECURE
? 443 : 80);
1083 if (port
== INTERNET_DEFAULT_HTTP_PORT
|| port
== INTERNET_DEFAULT_HTTPS_PORT
)
1085 return process_header( request
, attr_host
, connect
->hostname
, modifier
, TRUE
);
1087 len
= strlenW( connect
->hostname
) + 7; /* sizeof(":65335") */
1088 if (!(host
= heap_alloc( len
* sizeof(WCHAR
) ))) return FALSE
;
1089 sprintfW( host
, fmt
, connect
->hostname
, port
);
1090 ret
= process_header( request
, attr_host
, host
, modifier
, TRUE
);
1095 static void clear_response_headers( request_t
*request
)
1099 for (i
= 0; i
< request
->num_headers
; i
++)
1101 if (!request
->headers
[i
].field
) continue;
1102 if (!request
->headers
[i
].value
) continue;
1103 if (request
->headers
[i
].is_request
) continue;
1104 delete_header( request
, i
);
1109 static BOOL
send_request( request_t
*request
, LPCWSTR headers
, DWORD headers_len
, LPVOID optional
,
1110 DWORD optional_len
, DWORD total_len
, DWORD_PTR context
, BOOL async
)
1112 static const WCHAR keep_alive
[] = {'K','e','e','p','-','A','l','i','v','e',0};
1113 static const WCHAR no_cache
[] = {'n','o','-','c','a','c','h','e',0};
1114 static const WCHAR length_fmt
[] = {'%','l','d',0};
1117 connect_t
*connect
= request
->connect
;
1118 session_t
*session
= connect
->session
;
1122 DWORD len
, i
, flags
;
1124 clear_response_headers( request
);
1126 flags
= WINHTTP_ADDREQ_FLAG_ADD
|WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA
;
1127 for (i
= 0; i
< request
->num_accept_types
; i
++)
1129 process_header( request
, attr_accept
, request
->accept_types
[i
], flags
, TRUE
);
1132 process_header( request
, attr_user_agent
, session
->agent
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
1134 if (connect
->hostname
)
1135 add_host_header( request
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
);
1137 if (total_len
|| (request
->verb
&& !strcmpW( request
->verb
, postW
)))
1139 WCHAR length
[21]; /* decimal long int + null */
1140 sprintfW( length
, length_fmt
, total_len
);
1141 process_header( request
, attr_content_length
, length
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
1143 if (!(request
->hdr
.disable_flags
& WINHTTP_DISABLE_KEEP_ALIVE
))
1145 process_header( request
, attr_connection
, keep_alive
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
1147 if (request
->hdr
.flags
& WINHTTP_FLAG_REFRESH
)
1149 process_header( request
, attr_pragma
, no_cache
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
1150 process_header( request
, attr_cache_control
, no_cache
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
1152 if (headers
&& !add_request_headers( request
, headers
, headers_len
, WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
))
1154 TRACE("failed to add request headers\n");
1157 if (!(request
->hdr
.disable_flags
& WINHTTP_DISABLE_COOKIES
) && !add_cookie_headers( request
))
1159 WARN("failed to add cookie headers\n");
1163 if (context
) request
->hdr
.context
= context
;
1165 if (!(ret
= open_connection( request
))) goto end
;
1166 if (!(req
= build_request_string( request
))) goto end
;
1168 if (!(req_ascii
= strdupWA( req
))) goto end
;
1169 TRACE("full request: %s\n", debugstr_a(req_ascii
));
1170 len
= strlen(req_ascii
);
1172 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_SENDING_REQUEST
, NULL
, 0 );
1174 ret
= netconn_send( &request
->netconn
, req_ascii
, len
, &bytes_sent
);
1175 heap_free( req_ascii
);
1180 if (!netconn_send( &request
->netconn
, optional
, optional_len
, &bytes_sent
)) goto end
;
1181 request
->optional
= optional
;
1182 request
->optional_len
= optional_len
;
1183 len
+= optional_len
;
1185 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_REQUEST_SENT
, &len
, sizeof(len
) );
1190 if (ret
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE
, NULL
, 0 );
1193 WINHTTP_ASYNC_RESULT result
;
1194 result
.dwResult
= API_SEND_REQUEST
;
1195 result
.dwError
= get_last_error();
1196 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_REQUEST_ERROR
, &result
, sizeof(result
) );
1203 static void task_send_request( task_header_t
*task
)
1205 send_request_t
*s
= (send_request_t
*)task
;
1206 send_request( s
->hdr
.request
, s
->headers
, s
->headers_len
, s
->optional
, s
->optional_len
, s
->total_len
, s
->context
, TRUE
);
1207 heap_free( s
->headers
);
1210 /***********************************************************************
1211 * WinHttpSendRequest (winhttp.@)
1213 BOOL WINAPI
WinHttpSendRequest( HINTERNET hrequest
, LPCWSTR headers
, DWORD headers_len
,
1214 LPVOID optional
, DWORD optional_len
, DWORD total_len
, DWORD_PTR context
)
1219 TRACE("%p, %s, 0x%x, %u, %u, %lx\n",
1220 hrequest
, debugstr_w(headers
), headers_len
, optional_len
, total_len
, context
);
1222 if (!(request
= (request_t
*)grab_object( hrequest
)))
1224 set_last_error( ERROR_INVALID_HANDLE
);
1227 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
1229 release_object( &request
->hdr
);
1230 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1234 if (headers
&& !headers_len
) headers_len
= strlenW( headers
);
1236 if (request
->connect
->hdr
.flags
& WINHTTP_FLAG_ASYNC
)
1240 if (!(s
= heap_alloc( sizeof(send_request_t
) ))) return FALSE
;
1241 s
->hdr
.request
= request
;
1242 s
->hdr
.proc
= task_send_request
;
1243 s
->headers
= strdupW( headers
);
1244 s
->headers_len
= headers_len
;
1245 s
->optional
= optional
;
1246 s
->optional_len
= optional_len
;
1247 s
->total_len
= total_len
;
1248 s
->context
= context
;
1250 addref_object( &request
->hdr
);
1251 ret
= queue_task( (task_header_t
*)s
);
1254 ret
= send_request( request
, headers
, headers_len
, optional
, optional_len
, total_len
, context
, FALSE
);
1256 release_object( &request
->hdr
);
1257 if (ret
) set_last_error( ERROR_SUCCESS
);
1262 #define ARRAYSIZE(array) (sizeof(array) / sizeof((array)[0]))
1264 static const WCHAR basicW
[] = {'B','a','s','i','c',0};
1265 static const WCHAR ntlmW
[] = {'N','T','L','M',0};
1266 static const WCHAR passportW
[] = {'P','a','s','s','p','o','r','t',0};
1267 static const WCHAR digestW
[] = {'D','i','g','e','s','t',0};
1268 static const WCHAR negotiateW
[] = {'N','e','g','o','t','i','a','t','e',0};
1278 { basicW
, ARRAYSIZE(basicW
) - 1, WINHTTP_AUTH_SCHEME_BASIC
},
1279 { ntlmW
, ARRAYSIZE(ntlmW
) - 1, WINHTTP_AUTH_SCHEME_NTLM
},
1280 { passportW
, ARRAYSIZE(passportW
) - 1, WINHTTP_AUTH_SCHEME_PASSPORT
},
1281 { digestW
, ARRAYSIZE(digestW
) - 1, WINHTTP_AUTH_SCHEME_DIGEST
},
1282 { negotiateW
, ARRAYSIZE(negotiateW
) - 1, WINHTTP_AUTH_SCHEME_NEGOTIATE
}
1284 static const unsigned int num_auth_schemes
= sizeof(auth_schemes
)/sizeof(auth_schemes
[0]);
1286 static enum auth_scheme
scheme_from_flag( DWORD flag
)
1290 for (i
= 0; i
< num_auth_schemes
; i
++) if (flag
== auth_schemes
[i
].scheme
) return i
;
1291 return SCHEME_INVALID
;
1294 static DWORD
auth_scheme_from_header( WCHAR
*header
)
1298 for (i
= 0; i
< num_auth_schemes
; i
++)
1300 if (!strncmpiW( header
, auth_schemes
[i
].str
, auth_schemes
[i
].len
) &&
1301 (header
[auth_schemes
[i
].len
] == ' ' || !header
[auth_schemes
[i
].len
])) return auth_schemes
[i
].scheme
;
1306 static BOOL
query_auth_schemes( request_t
*request
, DWORD level
, LPDWORD supported
, LPDWORD first
)
1317 query_headers( request
, level
, NULL
, NULL
, &size
, &index
);
1318 if (get_last_error() != ERROR_INSUFFICIENT_BUFFER
) break;
1321 if (!(buffer
= heap_alloc( size
))) return FALSE
;
1322 if (!query_headers( request
, level
, NULL
, buffer
, &size
, &index
))
1324 heap_free( buffer
);
1327 scheme
= auth_scheme_from_header( buffer
);
1328 heap_free( buffer
);
1331 if (first
&& index
== 1)
1332 *first
= *supported
= scheme
;
1334 *supported
|= scheme
;
1341 /***********************************************************************
1342 * WinHttpQueryAuthSchemes (winhttp.@)
1344 BOOL WINAPI
WinHttpQueryAuthSchemes( HINTERNET hrequest
, LPDWORD supported
, LPDWORD first
, LPDWORD target
)
1349 TRACE("%p, %p, %p, %p\n", hrequest
, supported
, first
, target
);
1351 if (!(request
= (request_t
*)grab_object( hrequest
)))
1353 set_last_error( ERROR_INVALID_HANDLE
);
1356 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
1358 release_object( &request
->hdr
);
1359 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1362 if (!supported
|| !first
|| !target
)
1364 release_object( &request
->hdr
);
1365 set_last_error( ERROR_INVALID_PARAMETER
);
1370 if (query_auth_schemes( request
, WINHTTP_QUERY_WWW_AUTHENTICATE
, supported
, first
))
1372 *target
= WINHTTP_AUTH_TARGET_SERVER
;
1375 else if (query_auth_schemes( request
, WINHTTP_QUERY_PROXY_AUTHENTICATE
, supported
, first
))
1377 *target
= WINHTTP_AUTH_TARGET_PROXY
;
1381 release_object( &request
->hdr
);
1382 if (ret
) set_last_error( ERROR_SUCCESS
);
1386 static UINT
encode_base64( const char *bin
, unsigned int len
, WCHAR
*base64
)
1389 static const char base64enc
[] =
1390 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1394 /* first 6 bits, all from bin[0] */
1395 base64
[n
++] = base64enc
[(bin
[0] & 0xfc) >> 2];
1396 x
= (bin
[0] & 3) << 4;
1398 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1401 base64
[n
++] = base64enc
[x
];
1406 base64
[n
++] = base64enc
[x
| ((bin
[1] & 0xf0) >> 4)];
1407 x
= (bin
[1] & 0x0f) << 2;
1409 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1412 base64
[n
++] = base64enc
[x
];
1416 base64
[n
++] = base64enc
[x
| ((bin
[2] & 0xc0) >> 6)];
1418 /* last 6 bits, all from bin [2] */
1419 base64
[n
++] = base64enc
[bin
[2] & 0x3f];
1427 static inline char decode_char( WCHAR c
)
1429 if (c
>= 'A' && c
<= 'Z') return c
- 'A';
1430 if (c
>= 'a' && c
<= 'z') return c
- 'a' + 26;
1431 if (c
>= '0' && c
<= '9') return c
- '0' + 52;
1432 if (c
== '+') return 62;
1433 if (c
== '/') return 63;
1437 static unsigned int decode_base64( const WCHAR
*base64
, unsigned int len
, char *buf
)
1440 char c0
, c1
, c2
, c3
;
1441 const WCHAR
*p
= base64
;
1445 if ((c0
= decode_char( p
[0] )) > 63) return 0;
1446 if ((c1
= decode_char( p
[1] )) > 63) return 0;
1447 if ((c2
= decode_char( p
[2] )) > 63) return 0;
1448 if ((c3
= decode_char( p
[3] )) > 63) return 0;
1452 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
1453 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
1454 buf
[i
+ 2] = (c2
<< 6) | c3
;
1462 if ((c0
= decode_char( p
[0] )) > 63) return 0;
1463 if ((c1
= decode_char( p
[1] )) > 63) return 0;
1465 if (buf
) buf
[i
] = (c0
<< 2) | (c1
>> 4);
1468 else if (p
[3] == '=')
1470 if ((c0
= decode_char( p
[0] )) > 63) return 0;
1471 if ((c1
= decode_char( p
[1] )) > 63) return 0;
1472 if ((c2
= decode_char( p
[2] )) > 63) return 0;
1476 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
1477 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
1483 if ((c0
= decode_char( p
[0] )) > 63) return 0;
1484 if ((c1
= decode_char( p
[1] )) > 63) return 0;
1485 if ((c2
= decode_char( p
[2] )) > 63) return 0;
1486 if ((c3
= decode_char( p
[3] )) > 63) return 0;
1490 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
1491 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
1492 buf
[i
+ 2] = (c2
<< 6) | c3
;
1499 static struct authinfo
*alloc_authinfo(void)
1501 struct authinfo
*ret
;
1503 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
1505 SecInvalidateHandle( &ret
->cred
);
1506 SecInvalidateHandle( &ret
->ctx
);
1507 memset( &ret
->exp
, 0, sizeof(ret
->exp
) );
1513 ret
->finished
= FALSE
;
1517 void destroy_authinfo( struct authinfo
*authinfo
)
1519 if (!authinfo
) return;
1521 if (SecIsValidHandle( &authinfo
->ctx
))
1522 DeleteSecurityContext( &authinfo
->ctx
);
1523 if (SecIsValidHandle( &authinfo
->cred
))
1524 FreeCredentialsHandle( &authinfo
->cred
);
1526 heap_free( authinfo
->data
);
1527 heap_free( authinfo
);
1530 static BOOL
get_authvalue( request_t
*request
, DWORD level
, DWORD scheme
, WCHAR
*buffer
, DWORD len
)
1532 DWORD size
, index
= 0;
1536 if (!query_headers( request
, level
, NULL
, buffer
, &size
, &index
)) return FALSE
;
1537 if (auth_scheme_from_header( buffer
) == scheme
) break;
1542 static BOOL
do_authorization( request_t
*request
, DWORD target
, DWORD scheme_flag
)
1544 struct authinfo
*authinfo
, **auth_ptr
;
1545 enum auth_scheme scheme
= scheme_from_flag( scheme_flag
);
1546 const WCHAR
*auth_target
, *username
, *password
;
1547 WCHAR auth_value
[2048], *auth_reply
;
1548 DWORD len
= sizeof(auth_value
), len_scheme
, flags
;
1551 if (scheme
== SCHEME_INVALID
) return FALSE
;
1555 case WINHTTP_AUTH_TARGET_SERVER
:
1556 if (!get_authvalue( request
, WINHTTP_QUERY_WWW_AUTHENTICATE
, scheme_flag
, auth_value
, len
))
1558 auth_ptr
= &request
->authinfo
;
1559 auth_target
= attr_authorization
;
1560 if (request
->creds
[TARGET_SERVER
][scheme
].username
)
1562 username
= request
->creds
[TARGET_SERVER
][scheme
].username
;
1563 password
= request
->creds
[TARGET_SERVER
][scheme
].password
;
1567 username
= request
->connect
->username
;
1568 password
= request
->connect
->password
;
1572 case WINHTTP_AUTH_TARGET_PROXY
:
1573 if (!get_authvalue( request
, WINHTTP_QUERY_PROXY_AUTHENTICATE
, scheme_flag
, auth_value
, len
))
1575 auth_ptr
= &request
->proxy_authinfo
;
1576 auth_target
= attr_proxy_authorization
;
1577 if (request
->creds
[TARGET_PROXY
][scheme
].username
)
1579 username
= request
->creds
[TARGET_PROXY
][scheme
].username
;
1580 password
= request
->creds
[TARGET_PROXY
][scheme
].password
;
1584 username
= request
->connect
->session
->proxy_username
;
1585 password
= request
->connect
->session
->proxy_password
;
1590 WARN("unknown target %x\n", target
);
1593 authinfo
= *auth_ptr
;
1599 int userlen
, passlen
;
1601 if (!username
|| !password
) return FALSE
;
1602 if ((!authinfo
&& !(authinfo
= alloc_authinfo())) || authinfo
->finished
) return FALSE
;
1604 userlen
= WideCharToMultiByte( CP_UTF8
, 0, username
, strlenW( username
), NULL
, 0, NULL
, NULL
);
1605 passlen
= WideCharToMultiByte( CP_UTF8
, 0, password
, strlenW( password
), NULL
, 0, NULL
, NULL
);
1607 authinfo
->data_len
= userlen
+ 1 + passlen
;
1608 if (!(authinfo
->data
= heap_alloc( authinfo
->data_len
))) return FALSE
;
1610 WideCharToMultiByte( CP_UTF8
, 0, username
, -1, authinfo
->data
, userlen
, NULL
, NULL
);
1611 authinfo
->data
[userlen
] = ':';
1612 WideCharToMultiByte( CP_UTF8
, 0, password
, -1, authinfo
->data
+ userlen
+ 1, passlen
, NULL
, NULL
);
1614 authinfo
->scheme
= SCHEME_BASIC
;
1615 authinfo
->finished
= TRUE
;
1619 case SCHEME_NEGOTIATE
:
1621 SECURITY_STATUS status
;
1622 SecBufferDesc out_desc
, in_desc
;
1624 ULONG flags
= ISC_REQ_CONNECTION
|ISC_REQ_USE_DCE_STYLE
|ISC_REQ_MUTUAL_AUTH
|ISC_REQ_DELEGATE
;
1631 SEC_WINNT_AUTH_IDENTITY_W id
;
1632 WCHAR
*domain
, *user
;
1634 if (!username
|| !password
|| !(authinfo
= alloc_authinfo())) return FALSE
;
1637 domain
= (WCHAR
*)username
;
1638 user
= strchrW( username
, '\\' );
1643 user
= (WCHAR
*)username
;
1646 id
.Flags
= SEC_WINNT_AUTH_IDENTITY_UNICODE
;
1648 id
.UserLength
= strlenW( user
);
1650 id
.DomainLength
= domain
? user
- domain
- 1 : 0;
1651 id
.Password
= (WCHAR
*)password
;
1652 id
.PasswordLength
= strlenW( password
);
1654 status
= AcquireCredentialsHandleW( NULL
, (SEC_WCHAR
*)auth_schemes
[scheme
].str
,
1655 SECPKG_CRED_OUTBOUND
, NULL
, &id
, NULL
, NULL
,
1656 &authinfo
->cred
, &exp
);
1657 if (status
== SEC_E_OK
)
1660 status
= QuerySecurityPackageInfoW( (SEC_WCHAR
*)auth_schemes
[scheme
].str
, &info
);
1661 if (status
== SEC_E_OK
)
1663 authinfo
->max_token
= info
->cbMaxToken
;
1664 FreeContextBuffer( info
);
1667 if (status
!= SEC_E_OK
)
1669 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
1670 debugstr_w(auth_schemes
[scheme
].str
), status
);
1671 heap_free( authinfo
);
1674 authinfo
->scheme
= scheme
;
1676 else if (authinfo
->finished
) return FALSE
;
1678 if ((strlenW( auth_value
) < auth_schemes
[authinfo
->scheme
].len
||
1679 strncmpiW( auth_value
, auth_schemes
[authinfo
->scheme
].str
, auth_schemes
[authinfo
->scheme
].len
)))
1681 ERR("authentication scheme changed from %s to %s\n",
1682 debugstr_w(auth_schemes
[authinfo
->scheme
].str
), debugstr_w(auth_value
));
1683 destroy_authinfo( authinfo
);
1687 in
.BufferType
= SECBUFFER_TOKEN
;
1691 in_desc
.ulVersion
= 0;
1692 in_desc
.cBuffers
= 1;
1693 in_desc
.pBuffers
= &in
;
1695 p
= auth_value
+ auth_schemes
[scheme
].len
;
1698 int len
= strlenW( ++p
);
1699 in
.cbBuffer
= decode_base64( p
, len
, NULL
);
1700 if (!(in
.pvBuffer
= heap_alloc( in
.cbBuffer
))) {
1701 destroy_authinfo( authinfo
);
1705 decode_base64( p
, len
, in
.pvBuffer
);
1707 out
.BufferType
= SECBUFFER_TOKEN
;
1708 out
.cbBuffer
= authinfo
->max_token
;
1709 if (!(out
.pvBuffer
= heap_alloc( authinfo
->max_token
)))
1711 heap_free( in
.pvBuffer
);
1712 destroy_authinfo( authinfo
);
1716 out_desc
.ulVersion
= 0;
1717 out_desc
.cBuffers
= 1;
1718 out_desc
.pBuffers
= &out
;
1720 status
= InitializeSecurityContextW( first
? &authinfo
->cred
: NULL
, first
? NULL
: &authinfo
->ctx
,
1721 first
? request
->connect
->servername
: NULL
, flags
, 0,
1722 SECURITY_NETWORK_DREP
, in
.pvBuffer
? &in_desc
: NULL
, 0,
1723 &authinfo
->ctx
, &out_desc
, &authinfo
->attr
, &authinfo
->exp
);
1724 heap_free( in
.pvBuffer
);
1725 if (status
== SEC_E_OK
)
1727 heap_free( authinfo
->data
);
1728 authinfo
->data
= out
.pvBuffer
;
1729 authinfo
->data_len
= out
.cbBuffer
;
1730 authinfo
->finished
= TRUE
;
1731 TRACE("sending last auth packet\n");
1733 else if (status
== SEC_I_CONTINUE_NEEDED
)
1735 heap_free( authinfo
->data
);
1736 authinfo
->data
= out
.pvBuffer
;
1737 authinfo
->data_len
= out
.cbBuffer
;
1738 TRACE("sending next auth packet\n");
1742 ERR("InitializeSecurityContextW failed with error 0x%08x\n", status
);
1743 heap_free( out
.pvBuffer
);
1744 destroy_authinfo( authinfo
);
1751 ERR("invalid scheme %u\n", scheme
);
1754 *auth_ptr
= authinfo
;
1756 len_scheme
= auth_schemes
[authinfo
->scheme
].len
;
1757 len
= len_scheme
+ 1 + ((authinfo
->data_len
+ 2) * 4) / 3;
1758 if (!(auth_reply
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) return FALSE
;
1760 memcpy( auth_reply
, auth_schemes
[authinfo
->scheme
].str
, len_scheme
* sizeof(WCHAR
) );
1761 auth_reply
[len_scheme
] = ' ';
1762 encode_base64( authinfo
->data
, authinfo
->data_len
, auth_reply
+ len_scheme
+ 1 );
1764 flags
= WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
;
1765 ret
= process_header( request
, auth_target
, auth_reply
, flags
, TRUE
);
1766 heap_free( auth_reply
);
1770 static BOOL
set_credentials( request_t
*request
, DWORD target
, DWORD scheme_flag
, const WCHAR
*username
,
1771 const WCHAR
*password
)
1773 enum auth_scheme scheme
= scheme_from_flag( scheme_flag
);
1775 if (scheme
== SCHEME_INVALID
|| ((scheme
== SCHEME_BASIC
|| scheme
== SCHEME_DIGEST
) && (!username
|| !password
)))
1777 set_last_error( ERROR_INVALID_PARAMETER
);
1782 case WINHTTP_AUTH_TARGET_SERVER
:
1784 heap_free( request
->creds
[TARGET_SERVER
][scheme
].username
);
1785 if (!username
) request
->creds
[TARGET_SERVER
][scheme
].username
= NULL
;
1786 else if (!(request
->creds
[TARGET_SERVER
][scheme
].username
= strdupW( username
))) return FALSE
;
1788 heap_free( request
->creds
[TARGET_SERVER
][scheme
].password
);
1789 if (!password
) request
->creds
[TARGET_SERVER
][scheme
].password
= NULL
;
1790 else if (!(request
->creds
[TARGET_SERVER
][scheme
].password
= strdupW( password
))) return FALSE
;
1793 case WINHTTP_AUTH_TARGET_PROXY
:
1795 heap_free( request
->creds
[TARGET_PROXY
][scheme
].username
);
1796 if (!username
) request
->creds
[TARGET_PROXY
][scheme
].username
= NULL
;
1797 else if (!(request
->creds
[TARGET_PROXY
][scheme
].username
= strdupW( username
))) return FALSE
;
1799 heap_free( request
->creds
[TARGET_PROXY
][scheme
].password
);
1800 if (!password
) request
->creds
[TARGET_PROXY
][scheme
].password
= NULL
;
1801 else if (!(request
->creds
[TARGET_PROXY
][scheme
].password
= strdupW( password
))) return FALSE
;
1805 WARN("unknown target %u\n", target
);
1811 /***********************************************************************
1812 * WinHttpSetCredentials (winhttp.@)
1814 BOOL WINAPI
WinHttpSetCredentials( HINTERNET hrequest
, DWORD target
, DWORD scheme
, LPCWSTR username
,
1815 LPCWSTR password
, LPVOID params
)
1820 TRACE("%p, %x, 0x%08x, %s, %p, %p\n", hrequest
, target
, scheme
, debugstr_w(username
), password
, params
);
1822 if (!(request
= (request_t
*)grab_object( hrequest
)))
1824 set_last_error( ERROR_INVALID_HANDLE
);
1827 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
1829 release_object( &request
->hdr
);
1830 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
1834 ret
= set_credentials( request
, target
, scheme
, username
, password
);
1836 release_object( &request
->hdr
);
1837 if (ret
) set_last_error( ERROR_SUCCESS
);
1841 static BOOL
handle_authorization( request_t
*request
, DWORD status
)
1843 DWORD i
, schemes
, first
, level
, target
;
1847 case HTTP_STATUS_DENIED
:
1848 target
= WINHTTP_AUTH_TARGET_SERVER
;
1849 level
= WINHTTP_QUERY_WWW_AUTHENTICATE
;
1852 case HTTP_STATUS_PROXY_AUTH_REQ
:
1853 target
= WINHTTP_AUTH_TARGET_PROXY
;
1854 level
= WINHTTP_QUERY_PROXY_AUTHENTICATE
;
1858 WARN("unhandled status %u\n", status
);
1862 if (!query_auth_schemes( request
, level
, &schemes
, &first
)) return FALSE
;
1863 if (do_authorization( request
, target
, first
)) return TRUE
;
1866 for (i
= 0; i
< num_auth_schemes
; i
++)
1868 if (!(schemes
& auth_schemes
[i
].scheme
)) continue;
1869 if (do_authorization( request
, target
, auth_schemes
[i
].scheme
)) return TRUE
;
1874 /* set the request content length based on the headers */
1875 static DWORD
set_content_length( request_t
*request
, DWORD status
)
1878 DWORD buflen
= sizeof(request
->content_length
);
1880 if (status
== HTTP_STATUS_NO_CONTENT
|| status
== HTTP_STATUS_NOT_MODIFIED
|| !strcmpW( request
->verb
, headW
))
1881 request
->content_length
= 0;
1884 if (!query_headers( request
, WINHTTP_QUERY_CONTENT_LENGTH
|WINHTTP_QUERY_FLAG_NUMBER
,
1885 NULL
, &request
->content_length
, &buflen
, NULL
))
1886 request
->content_length
= ~0u;
1888 buflen
= sizeof(encoding
);
1889 if (query_headers( request
, WINHTTP_QUERY_TRANSFER_ENCODING
, NULL
, encoding
, &buflen
, NULL
) &&
1890 !strcmpiW( encoding
, chunkedW
))
1892 request
->content_length
= ~0u;
1893 request
->read_chunked
= TRUE
;
1894 request
->read_chunked_size
= ~0u;
1895 request
->read_chunked_eof
= FALSE
;
1898 request
->content_read
= 0;
1899 return request
->content_length
;
1902 /* read some more data into the read buffer */
1903 static BOOL
read_more_data( request_t
*request
, int maxlen
, BOOL notify
)
1908 if (request
->read_chunked_eof
) return FALSE
;
1910 if (request
->read_size
&& request
->read_pos
)
1912 /* move existing data to the start of the buffer */
1913 memmove( request
->read_buf
, request
->read_buf
+ request
->read_pos
, request
->read_size
);
1914 request
->read_pos
= 0;
1916 if (maxlen
== -1) maxlen
= sizeof(request
->read_buf
);
1918 if (notify
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE
, NULL
, 0 );
1920 ret
= netconn_recv( &request
->netconn
, request
->read_buf
+ request
->read_size
,
1921 maxlen
- request
->read_size
, 0, &len
);
1923 if (notify
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED
, &len
, sizeof(len
) );
1925 request
->read_size
+= len
;
1929 /* remove some amount of data from the read buffer */
1930 static void remove_data( request_t
*request
, int count
)
1932 if (!(request
->read_size
-= count
)) request
->read_pos
= 0;
1933 else request
->read_pos
+= count
;
1936 static BOOL
read_line( request_t
*request
, char *buffer
, DWORD
*len
)
1938 int count
, bytes_read
, pos
= 0;
1942 char *eol
= memchr( request
->read_buf
+ request
->read_pos
, '\n', request
->read_size
);
1945 count
= eol
- (request
->read_buf
+ request
->read_pos
);
1946 bytes_read
= count
+ 1;
1948 else count
= bytes_read
= request
->read_size
;
1950 count
= min( count
, *len
- pos
);
1951 memcpy( buffer
+ pos
, request
->read_buf
+ request
->read_pos
, count
);
1953 remove_data( request
, bytes_read
);
1956 if (!read_more_data( request
, -1, TRUE
)) return FALSE
;
1957 if (!request
->read_size
)
1960 TRACE("returning empty string\n");
1966 if (pos
&& buffer
[pos
- 1] == '\r') pos
--;
1969 buffer
[*len
- 1] = 0;
1970 TRACE("returning %s\n", debugstr_a(buffer
));
1974 /* discard data contents until we reach end of line */
1975 static BOOL
discard_eol( request_t
*request
, BOOL notify
)
1979 char *eol
= memchr( request
->read_buf
+ request
->read_pos
, '\n', request
->read_size
);
1982 remove_data( request
, (eol
+ 1) - (request
->read_buf
+ request
->read_pos
) );
1985 request
->read_pos
= request
->read_size
= 0; /* discard everything */
1986 if (!read_more_data( request
, -1, notify
)) return FALSE
;
1987 } while (request
->read_size
);
1991 /* read the size of the next chunk */
1992 static BOOL
start_next_chunk( request_t
*request
, BOOL notify
)
1994 DWORD chunk_size
= 0;
1996 assert(!request
->read_chunked_size
|| request
->read_chunked_size
== ~0u);
1998 if (request
->read_chunked_eof
) return FALSE
;
2000 /* read terminator for the previous chunk */
2001 if (!request
->read_chunked_size
&& !discard_eol( request
, notify
)) return FALSE
;
2005 while (request
->read_size
)
2007 char ch
= request
->read_buf
[request
->read_pos
];
2008 if (ch
>= '0' && ch
<= '9') chunk_size
= chunk_size
* 16 + ch
- '0';
2009 else if (ch
>= 'a' && ch
<= 'f') chunk_size
= chunk_size
* 16 + ch
- 'a' + 10;
2010 else if (ch
>= 'A' && ch
<= 'F') chunk_size
= chunk_size
* 16 + ch
- 'A' + 10;
2011 else if (ch
== ';' || ch
== '\r' || ch
== '\n')
2013 TRACE("reading %u byte chunk\n", chunk_size
);
2015 if (request
->content_length
== ~0u) request
->content_length
= chunk_size
;
2016 else request
->content_length
+= chunk_size
;
2018 request
->read_chunked_size
= chunk_size
;
2019 if (!chunk_size
) request
->read_chunked_eof
= TRUE
;
2021 return discard_eol( request
, notify
);
2023 remove_data( request
, 1 );
2025 if (!read_more_data( request
, -1, notify
)) return FALSE
;
2026 if (!request
->read_size
)
2028 request
->content_length
= request
->content_read
= 0;
2029 request
->read_chunked_size
= 0;
2035 /* return the size of data available to be read immediately */
2036 static DWORD
get_available_data( request_t
*request
)
2038 if (request
->read_chunked
) return min( request
->read_chunked_size
, request
->read_size
);
2039 return request
->read_size
;
2042 /* check if we have reached the end of the data to read */
2043 static BOOL
end_of_read_data( request_t
*request
)
2045 if (!request
->content_length
) return TRUE
;
2046 if (request
->read_chunked
) return request
->read_chunked_eof
;
2047 if (request
->content_length
== ~0u) return FALSE
;
2048 return (request
->content_length
== request
->content_read
);
2051 static BOOL
refill_buffer( request_t
*request
, BOOL notify
)
2053 int len
= sizeof(request
->read_buf
);
2055 if (request
->read_chunked
)
2057 if (request
->read_chunked_eof
) return FALSE
;
2058 if (request
->read_chunked_size
== ~0u || !request
->read_chunked_size
)
2060 if (!start_next_chunk( request
, notify
)) return FALSE
;
2062 len
= min( len
, request
->read_chunked_size
);
2064 else if (request
->content_length
!= ~0u)
2066 len
= min( len
, request
->content_length
- request
->content_read
);
2069 if (len
<= request
->read_size
) return TRUE
;
2070 if (!read_more_data( request
, len
, notify
)) return FALSE
;
2071 if (!request
->read_size
) request
->content_length
= request
->content_read
= 0;
2075 #define MAX_REPLY_LEN 1460
2076 #define INITIAL_HEADER_BUFFER_LEN 512
2078 static BOOL
read_reply( request_t
*request
)
2080 static const WCHAR crlf
[] = {'\r','\n',0};
2082 char buffer
[MAX_REPLY_LEN
];
2083 DWORD buflen
, len
, offset
, crlf_len
= 2; /* strlenW(crlf) */
2084 char *status_code
, *status_text
;
2085 WCHAR
*versionW
, *status_textW
, *raw_headers
;
2086 WCHAR status_codeW
[4]; /* sizeof("nnn") */
2088 if (!netconn_connected( &request
->netconn
)) return FALSE
;
2092 buflen
= MAX_REPLY_LEN
;
2093 if (!read_line( request
, buffer
, &buflen
)) return FALSE
;
2095 /* first line should look like 'HTTP/1.x nnn OK' where nnn is the status code */
2096 if (!(status_code
= strchr( buffer
, ' ' ))) return FALSE
;
2098 if (!(status_text
= strchr( status_code
, ' ' ))) return FALSE
;
2099 if ((len
= status_text
- status_code
) != sizeof("nnn") - 1) return FALSE
;
2102 TRACE("version [%s] status code [%s] status text [%s]\n",
2103 debugstr_an(buffer
, status_code
- buffer
- 1),
2104 debugstr_an(status_code
, len
),
2105 debugstr_a(status_text
));
2107 } while (!memcmp( status_code
, "100", len
)); /* ignore "100 Continue" responses */
2109 /* we rely on the fact that the protocol is ascii */
2110 MultiByteToWideChar( CP_ACP
, 0, status_code
, len
, status_codeW
, len
);
2111 status_codeW
[len
] = 0;
2112 if (!(process_header( request
, attr_status
, status_codeW
,
2113 WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
, FALSE
)))
2116 len
= status_code
- buffer
;
2117 if (!(versionW
= heap_alloc( len
* sizeof(WCHAR
) ))) return FALSE
;
2118 MultiByteToWideChar( CP_ACP
, 0, buffer
, len
- 1, versionW
, len
-1 );
2119 versionW
[len
- 1] = 0;
2121 heap_free( request
->version
);
2122 request
->version
= versionW
;
2124 len
= buflen
- (status_text
- buffer
);
2125 if (!(status_textW
= heap_alloc( len
* sizeof(WCHAR
) ))) return FALSE
;
2126 MultiByteToWideChar( CP_ACP
, 0, status_text
, len
, status_textW
, len
);
2128 heap_free( request
->status_text
);
2129 request
->status_text
= status_textW
;
2131 len
= max( buflen
+ crlf_len
, INITIAL_HEADER_BUFFER_LEN
);
2132 if (!(raw_headers
= heap_alloc( len
* sizeof(WCHAR
) ))) return FALSE
;
2133 MultiByteToWideChar( CP_ACP
, 0, buffer
, buflen
, raw_headers
, buflen
);
2134 memcpy( raw_headers
+ buflen
- 1, crlf
, sizeof(crlf
) );
2136 heap_free( request
->raw_headers
);
2137 request
->raw_headers
= raw_headers
;
2139 offset
= buflen
+ crlf_len
- 1;
2144 buflen
= MAX_REPLY_LEN
;
2145 if (!read_line( request
, buffer
, &buflen
)) return TRUE
;
2146 if (!*buffer
) buflen
= 1;
2148 while (len
- offset
< buflen
+ crlf_len
)
2152 if (!(tmp
= heap_realloc( raw_headers
, len
* sizeof(WCHAR
) ))) return FALSE
;
2153 request
->raw_headers
= raw_headers
= tmp
;
2157 memcpy( raw_headers
+ offset
, crlf
, sizeof(crlf
) );
2160 MultiByteToWideChar( CP_ACP
, 0, buffer
, buflen
, raw_headers
+ offset
, buflen
);
2162 if (!(header
= parse_header( raw_headers
+ offset
))) break;
2163 if (!(process_header( request
, header
->field
, header
->value
, WINHTTP_ADDREQ_FLAG_ADD
, FALSE
)))
2165 free_header( header
);
2168 free_header( header
);
2169 memcpy( raw_headers
+ offset
+ buflen
- 1, crlf
, sizeof(crlf
) );
2170 offset
+= buflen
+ crlf_len
- 1;
2173 TRACE("raw headers: %s\n", debugstr_w(raw_headers
));
2177 static void finished_reading( request_t
*request
)
2179 static const WCHAR closeW
[] = {'c','l','o','s','e',0};
2182 WCHAR connection
[20];
2183 DWORD size
= sizeof(connection
);
2185 if (request
->hdr
.disable_flags
& WINHTTP_DISABLE_KEEP_ALIVE
) close
= TRUE
;
2186 else if (query_headers( request
, WINHTTP_QUERY_CONNECTION
, NULL
, connection
, &size
, NULL
) ||
2187 query_headers( request
, WINHTTP_QUERY_PROXY_CONNECTION
, NULL
, connection
, &size
, NULL
))
2189 if (!strcmpiW( connection
, closeW
)) close
= TRUE
;
2191 else if (!strcmpW( request
->version
, http1_0
)) close
= TRUE
;
2192 if (close
) close_connection( request
);
2195 static BOOL
read_data( request_t
*request
, void *buffer
, DWORD size
, DWORD
*read
, BOOL async
)
2197 int count
, bytes_read
= 0;
2199 if (end_of_read_data( request
)) goto done
;
2203 if (!(count
= get_available_data( request
)))
2205 if (!refill_buffer( request
, async
)) goto done
;
2206 if (!(count
= get_available_data( request
))) goto done
;
2208 count
= min( count
, size
);
2209 memcpy( (char *)buffer
+ bytes_read
, request
->read_buf
+ request
->read_pos
, count
);
2210 remove_data( request
, count
);
2211 if (request
->read_chunked
) request
->read_chunked_size
-= count
;
2213 bytes_read
+= count
;
2214 request
->content_read
+= count
;
2215 if (end_of_read_data( request
)) goto done
;
2217 if (request
->read_chunked
&& !request
->read_chunked_size
) refill_buffer( request
, async
);
2220 TRACE( "retrieved %u bytes (%u/%u)\n", bytes_read
, request
->content_read
, request
->content_length
);
2222 if (async
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_READ_COMPLETE
, buffer
, bytes_read
);
2223 if (read
) *read
= bytes_read
;
2224 if (end_of_read_data( request
)) finished_reading( request
);
2228 /* read any content returned by the server so that the connection can be reused */
2229 static void drain_content( request_t
*request
)
2234 refill_buffer( request
, FALSE
);
2237 if (!read_data( request
, buffer
, sizeof(buffer
), &bytes_read
, FALSE
) || !bytes_read
) return;
2241 static void record_cookies( request_t
*request
)
2245 for (i
= 0; i
< request
->num_headers
; i
++)
2247 header_t
*set_cookie
= &request
->headers
[i
];
2248 if (!strcmpiW( set_cookie
->field
, attr_set_cookie
) && !set_cookie
->is_request
)
2250 set_cookies( request
, set_cookie
->value
);
2255 static WCHAR
*get_redirect_url( request_t
*request
, DWORD
*len
)
2260 query_headers( request
, WINHTTP_QUERY_LOCATION
, NULL
, NULL
, &size
, NULL
);
2261 if (get_last_error() != ERROR_INSUFFICIENT_BUFFER
) return FALSE
;
2262 if (!(ret
= heap_alloc( size
))) return NULL
;
2263 *len
= size
/ sizeof(WCHAR
);
2264 if (query_headers( request
, WINHTTP_QUERY_LOCATION
, NULL
, ret
, &size
, NULL
)) return ret
;
2269 static BOOL
handle_redirect( request_t
*request
, DWORD status
)
2274 connect_t
*connect
= request
->connect
;
2276 WCHAR
*hostname
= NULL
, *location
;
2279 if (!(location
= get_redirect_url( request
, &len_url
))) return FALSE
;
2281 memset( &uc
, 0, sizeof(uc
) );
2282 uc
.dwStructSize
= sizeof(uc
);
2283 uc
.dwSchemeLength
= uc
.dwHostNameLength
= uc
.dwUrlPathLength
= uc
.dwExtraInfoLength
= ~0u;
2285 if (!WinHttpCrackUrl( location
, len_url
, 0, &uc
)) /* assume relative redirect */
2289 if (location
[0] == '/')
2291 len
= strlenW( location
);
2292 if (!(path
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto end
;
2293 strcpyW( path
, location
);
2297 if ((p
= strrchrW( request
->path
, '/' ))) *p
= 0;
2298 len
= strlenW( request
->path
) + 1 + strlenW( location
);
2299 if (!(path
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto end
;
2300 strcpyW( path
, request
->path
);
2301 strcatW( path
, slashW
);
2302 strcatW( path
, location
);
2304 heap_free( request
->path
);
2305 request
->path
= path
;
2307 drain_content( request
);
2308 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_REDIRECT
, location
, len_url
+ 1 );
2312 if (uc
.nScheme
== INTERNET_SCHEME_HTTP
&& request
->hdr
.flags
& WINHTTP_FLAG_SECURE
)
2314 if (request
->hdr
.redirect_policy
== WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP
) goto end
;
2315 TRACE("redirect from secure page to non-secure page\n");
2316 request
->hdr
.flags
&= ~WINHTTP_FLAG_SECURE
;
2318 else if (uc
.nScheme
== INTERNET_SCHEME_HTTPS
&& !(request
->hdr
.flags
& WINHTTP_FLAG_SECURE
))
2320 TRACE("redirect from non-secure page to secure page\n");
2321 request
->hdr
.flags
|= WINHTTP_FLAG_SECURE
;
2324 drain_content( request
);
2325 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_REDIRECT
, location
, len_url
+ 1 );
2327 len
= uc
.dwHostNameLength
;
2328 if (!(hostname
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto end
;
2329 memcpy( hostname
, uc
.lpszHostName
, len
* sizeof(WCHAR
) );
2332 port
= uc
.nPort
? uc
.nPort
: (uc
.nScheme
== INTERNET_SCHEME_HTTPS
? 443 : 80);
2333 if (strcmpiW( connect
->hostname
, hostname
) || connect
->serverport
!= port
)
2335 heap_free( connect
->hostname
);
2336 connect
->hostname
= hostname
;
2337 connect
->hostport
= port
;
2338 if (!(ret
= set_server_for_hostname( connect
, hostname
, port
))) goto end
;
2340 netconn_close( &request
->netconn
);
2341 if (!(ret
= netconn_init( &request
->netconn
))) goto end
;
2342 request
->read_pos
= request
->read_size
= 0;
2343 request
->read_chunked
= FALSE
;
2344 request
->read_chunked_eof
= FALSE
;
2346 else heap_free( hostname
);
2348 if (!(ret
= add_host_header( request
, WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
))) goto end
;
2349 if (!(ret
= open_connection( request
))) goto end
;
2351 heap_free( request
->path
);
2352 request
->path
= NULL
;
2353 if (uc
.dwUrlPathLength
)
2355 len
= uc
.dwUrlPathLength
+ uc
.dwExtraInfoLength
;
2356 if (!(request
->path
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto end
;
2357 strcpyW( request
->path
, uc
.lpszUrlPath
);
2359 else request
->path
= strdupW( slashW
);
2362 /* remove content-type/length headers */
2363 if ((index
= get_header_index( request
, attr_content_type
, 0, TRUE
)) >= 0) delete_header( request
, index
);
2364 if ((index
= get_header_index( request
, attr_content_length
, 0, TRUE
)) >= 0 ) delete_header( request
, index
);
2366 if (status
!= HTTP_STATUS_REDIRECT_KEEP_VERB
&& !strcmpW( request
->verb
, postW
))
2368 heap_free( request
->verb
);
2369 request
->verb
= strdupW( getW
);
2370 request
->optional
= NULL
;
2371 request
->optional_len
= 0;
2376 heap_free( location
);
2380 static BOOL
receive_response( request_t
*request
, BOOL async
)
2383 DWORD size
, query
, status
;
2387 if (!(ret
= read_reply( request
)))
2389 set_last_error( ERROR_WINHTTP_INVALID_SERVER_RESPONSE
);
2392 size
= sizeof(DWORD
);
2393 query
= WINHTTP_QUERY_STATUS_CODE
| WINHTTP_QUERY_FLAG_NUMBER
;
2394 if (!(ret
= query_headers( request
, query
, NULL
, &status
, &size
, NULL
))) break;
2396 set_content_length( request
, status
);
2398 if (!(request
->hdr
.disable_flags
& WINHTTP_DISABLE_COOKIES
)) record_cookies( request
);
2400 if (status
== HTTP_STATUS_MOVED
|| status
== HTTP_STATUS_REDIRECT
|| status
== HTTP_STATUS_REDIRECT_KEEP_VERB
)
2402 if (request
->hdr
.disable_flags
& WINHTTP_DISABLE_REDIRECTS
||
2403 request
->hdr
.redirect_policy
== WINHTTP_OPTION_REDIRECT_POLICY_NEVER
) break;
2405 if (!(ret
= handle_redirect( request
, status
))) break;
2407 /* recurse synchronously */
2408 if ((ret
= send_request( request
, NULL
, 0, request
->optional
, request
->optional_len
, 0, 0, FALSE
))) continue;
2410 else if (status
== HTTP_STATUS_DENIED
|| status
== HTTP_STATUS_PROXY_AUTH_REQ
)
2412 if (request
->hdr
.disable_flags
& WINHTTP_DISABLE_AUTHENTICATION
) break;
2414 if (!handle_authorization( request
, status
)) break;
2415 drain_content( request
);
2417 /* recurse synchronously */
2418 if ((ret
= send_request( request
, NULL
, 0, request
->optional
, request
->optional_len
, 0, 0, FALSE
))) continue;
2423 if (request
->content_length
) refill_buffer( request
, FALSE
);
2427 if (ret
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE
, NULL
, 0 );
2430 WINHTTP_ASYNC_RESULT result
;
2431 result
.dwResult
= API_RECEIVE_RESPONSE
;
2432 result
.dwError
= get_last_error();
2433 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_REQUEST_ERROR
, &result
, sizeof(result
) );
2439 static void task_receive_response( task_header_t
*task
)
2441 receive_response_t
*r
= (receive_response_t
*)task
;
2442 receive_response( r
->hdr
.request
, TRUE
);
2445 /***********************************************************************
2446 * WinHttpReceiveResponse (winhttp.@)
2448 BOOL WINAPI
WinHttpReceiveResponse( HINTERNET hrequest
, LPVOID reserved
)
2453 TRACE("%p, %p\n", hrequest
, reserved
);
2455 if (!(request
= (request_t
*)grab_object( hrequest
)))
2457 set_last_error( ERROR_INVALID_HANDLE
);
2460 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
2462 release_object( &request
->hdr
);
2463 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2467 if (request
->connect
->hdr
.flags
& WINHTTP_FLAG_ASYNC
)
2469 receive_response_t
*r
;
2471 if (!(r
= heap_alloc( sizeof(receive_response_t
) ))) return FALSE
;
2472 r
->hdr
.request
= request
;
2473 r
->hdr
.proc
= task_receive_response
;
2475 addref_object( &request
->hdr
);
2476 ret
= queue_task( (task_header_t
*)r
);
2479 ret
= receive_response( request
, FALSE
);
2481 release_object( &request
->hdr
);
2482 if (ret
) set_last_error( ERROR_SUCCESS
);
2486 static BOOL
query_data_available( request_t
*request
, DWORD
*available
, BOOL async
)
2490 if (end_of_read_data( request
)) goto done
;
2492 count
= get_available_data( request
);
2493 if (!request
->read_chunked
)
2494 count
+= netconn_query_data_available( &request
->netconn
);
2497 refill_buffer( request
, async
);
2498 count
= get_available_data( request
);
2499 if (!request
->read_chunked
)
2500 count
+= netconn_query_data_available( &request
->netconn
);
2504 if (async
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE
, &count
, sizeof(count
) );
2505 TRACE("%u bytes available\n", count
);
2506 if (available
) *available
= count
;
2510 static void task_query_data_available( task_header_t
*task
)
2512 query_data_t
*q
= (query_data_t
*)task
;
2513 query_data_available( q
->hdr
.request
, q
->available
, TRUE
);
2516 /***********************************************************************
2517 * WinHttpQueryDataAvailable (winhttp.@)
2519 BOOL WINAPI
WinHttpQueryDataAvailable( HINTERNET hrequest
, LPDWORD available
)
2524 TRACE("%p, %p\n", hrequest
, available
);
2526 if (!(request
= (request_t
*)grab_object( hrequest
)))
2528 set_last_error( ERROR_INVALID_HANDLE
);
2531 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
2533 release_object( &request
->hdr
);
2534 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2538 if (request
->connect
->hdr
.flags
& WINHTTP_FLAG_ASYNC
)
2542 if (!(q
= heap_alloc( sizeof(query_data_t
) ))) return FALSE
;
2543 q
->hdr
.request
= request
;
2544 q
->hdr
.proc
= task_query_data_available
;
2545 q
->available
= available
;
2547 addref_object( &request
->hdr
);
2548 ret
= queue_task( (task_header_t
*)q
);
2551 ret
= query_data_available( request
, available
, FALSE
);
2553 release_object( &request
->hdr
);
2554 if (ret
) set_last_error( ERROR_SUCCESS
);
2558 static void task_read_data( task_header_t
*task
)
2560 read_data_t
*r
= (read_data_t
*)task
;
2561 read_data( r
->hdr
.request
, r
->buffer
, r
->to_read
, r
->read
, TRUE
);
2564 /***********************************************************************
2565 * WinHttpReadData (winhttp.@)
2567 BOOL WINAPI
WinHttpReadData( HINTERNET hrequest
, LPVOID buffer
, DWORD to_read
, LPDWORD read
)
2572 TRACE("%p, %p, %d, %p\n", hrequest
, buffer
, to_read
, read
);
2574 if (!(request
= (request_t
*)grab_object( hrequest
)))
2576 set_last_error( ERROR_INVALID_HANDLE
);
2579 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
2581 release_object( &request
->hdr
);
2582 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2586 if (request
->connect
->hdr
.flags
& WINHTTP_FLAG_ASYNC
)
2590 if (!(r
= heap_alloc( sizeof(read_data_t
) ))) return FALSE
;
2591 r
->hdr
.request
= request
;
2592 r
->hdr
.proc
= task_read_data
;
2594 r
->to_read
= to_read
;
2597 addref_object( &request
->hdr
);
2598 ret
= queue_task( (task_header_t
*)r
);
2601 ret
= read_data( request
, buffer
, to_read
, read
, FALSE
);
2603 release_object( &request
->hdr
);
2604 if (ret
) set_last_error( ERROR_SUCCESS
);
2608 static BOOL
write_data( request_t
*request
, LPCVOID buffer
, DWORD to_write
, LPDWORD written
, BOOL async
)
2613 ret
= netconn_send( &request
->netconn
, buffer
, to_write
, &num_bytes
);
2617 if (ret
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE
, &num_bytes
, sizeof(num_bytes
) );
2620 WINHTTP_ASYNC_RESULT result
;
2621 result
.dwResult
= API_WRITE_DATA
;
2622 result
.dwError
= get_last_error();
2623 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_REQUEST_ERROR
, &result
, sizeof(result
) );
2626 if (ret
&& written
) *written
= num_bytes
;
2630 static void task_write_data( task_header_t
*task
)
2632 write_data_t
*w
= (write_data_t
*)task
;
2633 write_data( w
->hdr
.request
, w
->buffer
, w
->to_write
, w
->written
, TRUE
);
2636 /***********************************************************************
2637 * WinHttpWriteData (winhttp.@)
2639 BOOL WINAPI
WinHttpWriteData( HINTERNET hrequest
, LPCVOID buffer
, DWORD to_write
, LPDWORD written
)
2644 TRACE("%p, %p, %d, %p\n", hrequest
, buffer
, to_write
, written
);
2646 if (!(request
= (request_t
*)grab_object( hrequest
)))
2648 set_last_error( ERROR_INVALID_HANDLE
);
2651 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
2653 release_object( &request
->hdr
);
2654 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2658 if (request
->connect
->hdr
.flags
& WINHTTP_FLAG_ASYNC
)
2662 if (!(w
= heap_alloc( sizeof(write_data_t
) ))) return FALSE
;
2663 w
->hdr
.request
= request
;
2664 w
->hdr
.proc
= task_write_data
;
2666 w
->to_write
= to_write
;
2667 w
->written
= written
;
2669 addref_object( &request
->hdr
);
2670 ret
= queue_task( (task_header_t
*)w
);
2673 ret
= write_data( request
, buffer
, to_write
, written
, FALSE
);
2675 release_object( &request
->hdr
);
2676 if (ret
) set_last_error( ERROR_SUCCESS
);
2682 REQUEST_STATE_UNINITIALIZED
,
2683 REQUEST_STATE_INITIALIZED
,
2684 REQUEST_STATE_CANCELLED
,
2687 REQUEST_STATE_RESPONSE_RECEIVED
2690 struct winhttp_request
2692 IWinHttpRequest IWinHttpRequest_iface
;
2694 CRITICAL_SECTION cs
;
2695 enum request_state state
;
2706 DWORD bytes_available
;
2710 DWORD disable_feature
;
2711 LONG resolve_timeout
;
2712 LONG connect_timeout
;
2714 LONG receive_timeout
;
2715 WINHTTP_PROXY_INFO proxy
;
2720 static inline struct winhttp_request
*impl_from_IWinHttpRequest( IWinHttpRequest
*iface
)
2722 return CONTAINING_RECORD( iface
, struct winhttp_request
, IWinHttpRequest_iface
);
2725 static ULONG WINAPI
winhttp_request_AddRef(
2726 IWinHttpRequest
*iface
)
2728 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
2729 return InterlockedIncrement( &request
->refs
);
2732 /* critical section must be held */
2733 static void cancel_request( struct winhttp_request
*request
)
2735 if (request
->state
<= REQUEST_STATE_CANCELLED
) return;
2737 SetEvent( request
->cancel
);
2738 LeaveCriticalSection( &request
->cs
);
2739 WaitForSingleObject( request
->thread
, INFINITE
);
2740 EnterCriticalSection( &request
->cs
);
2742 request
->state
= REQUEST_STATE_CANCELLED
;
2744 CloseHandle( request
->thread
);
2745 request
->thread
= NULL
;
2746 CloseHandle( request
->wait
);
2747 request
->wait
= NULL
;
2748 CloseHandle( request
->cancel
);
2749 request
->cancel
= NULL
;
2752 /* critical section must be held */
2753 static void free_request( struct winhttp_request
*request
)
2755 if (request
->state
< REQUEST_STATE_INITIALIZED
) return;
2756 WinHttpCloseHandle( request
->hrequest
);
2757 WinHttpCloseHandle( request
->hconnect
);
2758 WinHttpCloseHandle( request
->hsession
);
2759 CloseHandle( request
->thread
);
2760 CloseHandle( request
->wait
);
2761 CloseHandle( request
->cancel
);
2762 heap_free( (WCHAR
*)request
->proxy
.lpszProxy
);
2763 heap_free( (WCHAR
*)request
->proxy
.lpszProxyBypass
);
2764 heap_free( request
->buffer
);
2765 heap_free( request
->verb
);
2766 VariantClear( &request
->data
);
2769 static ULONG WINAPI
winhttp_request_Release(
2770 IWinHttpRequest
*iface
)
2772 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
2773 LONG refs
= InterlockedDecrement( &request
->refs
);
2776 TRACE("destroying %p\n", request
);
2778 EnterCriticalSection( &request
->cs
);
2779 cancel_request( request
);
2780 free_request( request
);
2781 LeaveCriticalSection( &request
->cs
);
2782 request
->cs
.DebugInfo
->Spare
[0] = 0;
2783 DeleteCriticalSection( &request
->cs
);
2784 heap_free( request
);
2789 static HRESULT WINAPI
winhttp_request_QueryInterface(
2790 IWinHttpRequest
*iface
,
2794 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
2796 TRACE("%p, %s, %p\n", request
, debugstr_guid(riid
), obj
);
2798 if (IsEqualGUID( riid
, &IID_IWinHttpRequest
) ||
2799 IsEqualGUID( riid
, &IID_IDispatch
) ||
2800 IsEqualGUID( riid
, &IID_IUnknown
))
2806 FIXME("interface %s not implemented\n", debugstr_guid(riid
));
2807 return E_NOINTERFACE
;
2809 IWinHttpRequest_AddRef( iface
);
2813 static HRESULT WINAPI
winhttp_request_GetTypeInfoCount(
2814 IWinHttpRequest
*iface
,
2817 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
2819 TRACE("%p, %p\n", request
, count
);
2826 IWinHttpRequest_tid
,
2830 static ITypeLib
*winhttp_typelib
;
2831 static ITypeInfo
*winhttp_typeinfo
[last_tid
];
2833 static REFIID winhttp_tid_id
[] =
2835 &IID_IWinHttpRequest
2838 static HRESULT
get_typeinfo( enum type_id tid
, ITypeInfo
**ret
)
2842 if (!winhttp_typelib
)
2846 hr
= LoadRegTypeLib( &LIBID_WinHttp
, 5, 1, LOCALE_SYSTEM_DEFAULT
, &typelib
);
2849 ERR("LoadRegTypeLib failed: %08x\n", hr
);
2852 if (InterlockedCompareExchangePointer( (void **)&winhttp_typelib
, typelib
, NULL
))
2853 ITypeLib_Release( typelib
);
2855 if (!winhttp_typeinfo
[tid
])
2857 ITypeInfo
*typeinfo
;
2859 hr
= ITypeLib_GetTypeInfoOfGuid( winhttp_typelib
, winhttp_tid_id
[tid
], &typeinfo
);
2862 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(winhttp_tid_id
[tid
]), hr
);
2865 if (InterlockedCompareExchangePointer( (void **)(winhttp_typeinfo
+ tid
), typeinfo
, NULL
))
2866 ITypeInfo_Release( typeinfo
);
2868 *ret
= winhttp_typeinfo
[tid
];
2869 ITypeInfo_AddRef(winhttp_typeinfo
[tid
]);
2873 void release_typelib(void)
2877 for (i
= 0; i
< sizeof(winhttp_typeinfo
)/sizeof(*winhttp_typeinfo
); i
++)
2878 if (winhttp_typeinfo
[i
])
2879 ITypeInfo_Release(winhttp_typeinfo
[i
]);
2881 if (winhttp_typelib
)
2882 ITypeLib_Release(winhttp_typelib
);
2885 static HRESULT WINAPI
winhttp_request_GetTypeInfo(
2886 IWinHttpRequest
*iface
,
2891 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
2892 TRACE("%p, %u, %u, %p\n", request
, index
, lcid
, info
);
2894 return get_typeinfo( IWinHttpRequest_tid
, info
);
2897 static HRESULT WINAPI
winhttp_request_GetIDsOfNames(
2898 IWinHttpRequest
*iface
,
2905 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
2906 ITypeInfo
*typeinfo
;
2909 TRACE("%p, %s, %p, %u, %u, %p\n", request
, debugstr_guid(riid
), names
, count
, lcid
, dispid
);
2911 if (!names
|| !count
|| !dispid
) return E_INVALIDARG
;
2913 hr
= get_typeinfo( IWinHttpRequest_tid
, &typeinfo
);
2916 hr
= ITypeInfo_GetIDsOfNames( typeinfo
, names
, count
, dispid
);
2917 ITypeInfo_Release( typeinfo
);
2922 static HRESULT WINAPI
winhttp_request_Invoke(
2923 IWinHttpRequest
*iface
,
2930 EXCEPINFO
*excep_info
,
2933 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
2934 ITypeInfo
*typeinfo
;
2937 TRACE("%p, %d, %s, %d, %d, %p, %p, %p, %p\n", request
, member
, debugstr_guid(riid
),
2938 lcid
, flags
, params
, result
, excep_info
, arg_err
);
2940 if (!IsEqualIID( riid
, &IID_NULL
)) return DISP_E_UNKNOWNINTERFACE
;
2942 if (member
== DISPID_HTTPREQUEST_OPTION
)
2944 VARIANT ret_value
, option
;
2947 if (!result
) result
= &ret_value
;
2948 if (!arg_err
) arg_err
= &err_pos
;
2950 VariantInit( &option
);
2951 VariantInit( result
);
2953 if (!flags
) return S_OK
;
2955 if (flags
== DISPATCH_PROPERTYPUT
)
2957 hr
= DispGetParam( params
, 0, VT_I4
, &option
, arg_err
);
2958 if (FAILED(hr
)) return hr
;
2960 hr
= IWinHttpRequest_put_Option( &request
->IWinHttpRequest_iface
, V_I4( &option
), params
->rgvarg
[0] );
2962 WARN("put_Option(%d) failed: %x\n", V_I4( &option
), hr
);
2965 else if (flags
& (DISPATCH_PROPERTYGET
| DISPATCH_METHOD
))
2967 hr
= DispGetParam( params
, 0, VT_I4
, &option
, arg_err
);
2968 if (FAILED(hr
)) return hr
;
2970 hr
= IWinHttpRequest_get_Option( &request
->IWinHttpRequest_iface
, V_I4( &option
), result
);
2972 WARN("get_Option(%d) failed: %x\n", V_I4( &option
), hr
);
2976 FIXME("unsupported flags %x\n", flags
);
2980 /* fallback to standard implementation */
2982 hr
= get_typeinfo( IWinHttpRequest_tid
, &typeinfo
);
2985 hr
= ITypeInfo_Invoke( typeinfo
, &request
->IWinHttpRequest_iface
, member
, flags
,
2986 params
, result
, excep_info
, arg_err
);
2987 ITypeInfo_Release( typeinfo
);
2992 static HRESULT WINAPI
winhttp_request_SetProxy(
2993 IWinHttpRequest
*iface
,
2994 HTTPREQUEST_PROXY_SETTING proxy_setting
,
2995 VARIANT proxy_server
,
2996 VARIANT bypass_list
)
2998 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
2999 DWORD err
= ERROR_SUCCESS
;
3001 TRACE("%p, %u, %s, %s\n", request
, proxy_setting
, debugstr_variant(&proxy_server
),
3002 debugstr_variant(&bypass_list
));
3004 EnterCriticalSection( &request
->cs
);
3005 switch (proxy_setting
)
3007 case HTTPREQUEST_PROXYSETTING_DEFAULT
:
3008 request
->proxy
.dwAccessType
= WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
;
3009 heap_free( (WCHAR
*)request
->proxy
.lpszProxy
);
3010 heap_free( (WCHAR
*)request
->proxy
.lpszProxyBypass
);
3011 request
->proxy
.lpszProxy
= NULL
;
3012 request
->proxy
.lpszProxyBypass
= NULL
;
3015 case HTTPREQUEST_PROXYSETTING_DIRECT
:
3016 request
->proxy
.dwAccessType
= WINHTTP_ACCESS_TYPE_NO_PROXY
;
3017 heap_free( (WCHAR
*)request
->proxy
.lpszProxy
);
3018 heap_free( (WCHAR
*)request
->proxy
.lpszProxyBypass
);
3019 request
->proxy
.lpszProxy
= NULL
;
3020 request
->proxy
.lpszProxyBypass
= NULL
;
3023 case HTTPREQUEST_PROXYSETTING_PROXY
:
3024 request
->proxy
.dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
3025 if (V_VT( &proxy_server
) == VT_BSTR
)
3027 heap_free( (WCHAR
*)request
->proxy
.lpszProxy
);
3028 request
->proxy
.lpszProxy
= strdupW( V_BSTR( &proxy_server
) );
3030 if (V_VT( &bypass_list
) == VT_BSTR
)
3032 heap_free( (WCHAR
*)request
->proxy
.lpszProxyBypass
);
3033 request
->proxy
.lpszProxyBypass
= strdupW( V_BSTR( &bypass_list
) );
3038 err
= ERROR_INVALID_PARAMETER
;
3041 LeaveCriticalSection( &request
->cs
);
3042 return HRESULT_FROM_WIN32( err
);
3045 static HRESULT WINAPI
winhttp_request_SetCredentials(
3046 IWinHttpRequest
*iface
,
3049 HTTPREQUEST_SETCREDENTIALS_FLAGS flags
)
3051 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3052 DWORD target
, scheme
= WINHTTP_AUTH_SCHEME_BASIC
; /* FIXME: query supported schemes */
3053 DWORD err
= ERROR_SUCCESS
;
3055 TRACE("%p, %s, %p, 0x%08x\n", request
, debugstr_w(username
), password
, flags
);
3057 EnterCriticalSection( &request
->cs
);
3058 if (request
->state
< REQUEST_STATE_OPEN
)
3060 err
= ERROR_WINHTTP_CANNOT_CALL_BEFORE_OPEN
;
3065 case HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
:
3066 target
= WINHTTP_AUTH_TARGET_SERVER
;
3068 case HTTPREQUEST_SETCREDENTIALS_FOR_PROXY
:
3069 target
= WINHTTP_AUTH_TARGET_PROXY
;
3072 err
= ERROR_INVALID_PARAMETER
;
3075 if (!WinHttpSetCredentials( request
->hrequest
, target
, scheme
, username
, password
, NULL
))
3077 err
= get_last_error();
3080 LeaveCriticalSection( &request
->cs
);
3081 return HRESULT_FROM_WIN32( err
);
3084 static void initialize_request( struct winhttp_request
*request
)
3086 request
->hrequest
= NULL
;
3087 request
->hconnect
= NULL
;
3088 request
->hsession
= NULL
;
3089 request
->thread
= NULL
;
3090 request
->wait
= NULL
;
3091 request
->cancel
= NULL
;
3092 request
->buffer
= NULL
;
3093 request
->verb
= NULL
;
3094 request
->offset
= 0;
3095 request
->bytes_available
= 0;
3096 request
->bytes_read
= 0;
3097 request
->error
= ERROR_SUCCESS
;
3098 request
->async
= FALSE
;
3099 request
->logon_policy
= WINHTTP_AUTOLOGON_SECURITY_LEVEL_MEDIUM
;
3100 request
->disable_feature
= 0;
3101 request
->proxy
.dwAccessType
= WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
;
3102 request
->proxy
.lpszProxy
= NULL
;
3103 request
->proxy
.lpszProxyBypass
= NULL
;
3104 request
->resolve_timeout
= 0;
3105 request
->connect_timeout
= 60000;
3106 request
->send_timeout
= 30000;
3107 request
->receive_timeout
= 30000;
3108 request
->url_codepage
= CP_UTF8
;
3109 VariantInit( &request
->data
);
3110 request
->state
= REQUEST_STATE_INITIALIZED
;
3113 static void reset_request( struct winhttp_request
*request
)
3115 cancel_request( request
);
3116 WinHttpCloseHandle( request
->hrequest
);
3117 request
->hrequest
= NULL
;
3118 WinHttpCloseHandle( request
->hconnect
);
3119 request
->hconnect
= NULL
;
3120 heap_free( request
->buffer
);
3121 request
->buffer
= NULL
;
3122 heap_free( request
->verb
);
3123 request
->verb
= NULL
;
3124 request
->offset
= 0;
3125 request
->bytes_available
= 0;
3126 request
->bytes_read
= 0;
3127 request
->error
= ERROR_SUCCESS
;
3128 request
->async
= FALSE
;
3129 request
->url_codepage
= CP_UTF8
;
3130 VariantClear( &request
->data
);
3131 request
->state
= REQUEST_STATE_INITIALIZED
;
3134 static HRESULT WINAPI
winhttp_request_Open(
3135 IWinHttpRequest
*iface
,
3140 static const WCHAR typeW
[] = {'*','/','*',0};
3141 static const WCHAR
*acceptW
[] = {typeW
, NULL
};
3142 static const WCHAR httpsW
[] = {'h','t','t','p','s'};
3143 static const WCHAR user_agentW
[] = {
3144 'M','o','z','i','l','l','a','/','4','.','0',' ','(','c','o','m','p','a','t','i','b','l','e',';',' ',
3145 'W','i','n','3','2',';',' ','W','i','n','H','t','t','p','.','W','i','n','H','t','t','p',
3146 'R','e','q','u','e','s','t','.','5',')',0};
3147 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3149 WCHAR
*hostname
, *path
= NULL
, *verb
= NULL
;
3150 DWORD err
= ERROR_OUTOFMEMORY
, len
, flags
= 0;
3152 TRACE("%p, %s, %s, %s\n", request
, debugstr_w(method
), debugstr_w(url
),
3153 debugstr_variant(&async
));
3155 if (!method
|| !url
) return E_INVALIDARG
;
3157 memset( &uc
, 0, sizeof(uc
) );
3158 uc
.dwStructSize
= sizeof(uc
);
3159 uc
.dwSchemeLength
= ~0u;
3160 uc
.dwHostNameLength
= ~0u;
3161 uc
.dwUrlPathLength
= ~0u;
3162 uc
.dwExtraInfoLength
= ~0u;
3163 if (!WinHttpCrackUrl( url
, 0, 0, &uc
)) return HRESULT_FROM_WIN32( get_last_error() );
3165 EnterCriticalSection( &request
->cs
);
3166 if (request
->state
< REQUEST_STATE_INITIALIZED
) initialize_request( request
);
3167 else reset_request( request
);
3169 if (!(hostname
= heap_alloc( (uc
.dwHostNameLength
+ 1) * sizeof(WCHAR
) ))) goto error
;
3170 memcpy( hostname
, uc
.lpszHostName
, uc
.dwHostNameLength
* sizeof(WCHAR
) );
3171 hostname
[uc
.dwHostNameLength
] = 0;
3173 if (!(path
= heap_alloc( (uc
.dwUrlPathLength
+ uc
.dwExtraInfoLength
+ 1) * sizeof(WCHAR
) ))) goto error
;
3174 memcpy( path
, uc
.lpszUrlPath
, (uc
.dwUrlPathLength
+ uc
.dwExtraInfoLength
) * sizeof(WCHAR
) );
3175 path
[uc
.dwUrlPathLength
+ uc
.dwExtraInfoLength
] = 0;
3177 if (!(verb
= strdupW( method
))) goto error
;
3178 if (SUCCEEDED( VariantChangeType( &async
, &async
, 0, VT_BOOL
)) && V_BOOL( &async
)) request
->async
= TRUE
;
3179 else request
->async
= FALSE
;
3181 if (!request
->hsession
)
3183 if (!(request
->hsession
= WinHttpOpen( user_agentW
, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
, NULL
, NULL
,
3184 WINHTTP_FLAG_ASYNC
)))
3186 err
= get_last_error();
3189 if (!(request
->hconnect
= WinHttpConnect( request
->hsession
, hostname
, uc
.nPort
, 0 )))
3191 WinHttpCloseHandle( request
->hsession
);
3192 request
->hsession
= NULL
;
3193 err
= get_last_error();
3197 else if (!(request
->hconnect
= WinHttpConnect( request
->hsession
, hostname
, uc
.nPort
, 0 )))
3199 err
= get_last_error();
3203 len
= sizeof(httpsW
) / sizeof(WCHAR
);
3204 if (uc
.dwSchemeLength
== len
&& !memcmp( uc
.lpszScheme
, httpsW
, len
* sizeof(WCHAR
) ))
3206 flags
|= WINHTTP_FLAG_SECURE
;
3208 if (!(request
->hrequest
= WinHttpOpenRequest( request
->hconnect
, method
, path
, NULL
, NULL
, acceptW
, flags
)))
3210 err
= get_last_error();
3213 WinHttpSetOption( request
->hrequest
, WINHTTP_OPTION_CONTEXT_VALUE
, &request
, sizeof(request
) );
3215 request
->state
= REQUEST_STATE_OPEN
;
3216 request
->verb
= verb
;
3217 heap_free( hostname
);
3219 LeaveCriticalSection( &request
->cs
);
3223 WinHttpCloseHandle( request
->hconnect
);
3224 request
->hconnect
= NULL
;
3225 heap_free( hostname
);
3228 LeaveCriticalSection( &request
->cs
);
3229 return HRESULT_FROM_WIN32( err
);
3232 static HRESULT WINAPI
winhttp_request_SetRequestHeader(
3233 IWinHttpRequest
*iface
,
3237 static const WCHAR fmtW
[] = {'%','s',':',' ','%','s','\r','\n',0};
3238 static const WCHAR emptyW
[] = {0};
3239 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3240 DWORD len
, err
= ERROR_SUCCESS
;
3243 TRACE("%p, %s, %s\n", request
, debugstr_w(header
), debugstr_w(value
));
3245 if (!header
) return E_INVALIDARG
;
3247 EnterCriticalSection( &request
->cs
);
3248 if (request
->state
< REQUEST_STATE_OPEN
)
3250 err
= ERROR_WINHTTP_CANNOT_CALL_BEFORE_OPEN
;
3253 if (request
->state
>= REQUEST_STATE_SENT
)
3255 err
= ERROR_WINHTTP_CANNOT_CALL_AFTER_SEND
;
3258 len
= strlenW( header
) + 4;
3259 if (value
) len
+= strlenW( value
);
3260 if (!(str
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) )))
3262 err
= ERROR_OUTOFMEMORY
;
3265 sprintfW( str
, fmtW
, header
, value
? value
: emptyW
);
3266 if (!WinHttpAddRequestHeaders( request
->hrequest
, str
, len
,
3267 WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
))
3269 err
= get_last_error();
3274 LeaveCriticalSection( &request
->cs
);
3275 return HRESULT_FROM_WIN32( err
);
3278 static HRESULT WINAPI
winhttp_request_GetResponseHeader(
3279 IWinHttpRequest
*iface
,
3283 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3284 DWORD size
, err
= ERROR_SUCCESS
;
3286 TRACE("%p, %p\n", request
, header
);
3288 EnterCriticalSection( &request
->cs
);
3289 if (request
->state
< REQUEST_STATE_SENT
)
3291 err
= ERROR_WINHTTP_CANNOT_CALL_BEFORE_SEND
;
3294 if (!header
|| !value
)
3296 err
= ERROR_INVALID_PARAMETER
;
3300 if (!WinHttpQueryHeaders( request
->hrequest
, WINHTTP_QUERY_CUSTOM
, header
, NULL
, &size
, NULL
))
3302 err
= get_last_error();
3303 if (err
!= ERROR_INSUFFICIENT_BUFFER
) goto done
;
3305 if (!(*value
= SysAllocStringLen( NULL
, size
/ sizeof(WCHAR
) )))
3307 err
= ERROR_OUTOFMEMORY
;
3310 err
= ERROR_SUCCESS
;
3311 if (!WinHttpQueryHeaders( request
->hrequest
, WINHTTP_QUERY_CUSTOM
, header
, *value
, &size
, NULL
))
3313 err
= get_last_error();
3314 SysFreeString( *value
);
3317 LeaveCriticalSection( &request
->cs
);
3318 return HRESULT_FROM_WIN32( err
);
3321 static HRESULT WINAPI
winhttp_request_GetAllResponseHeaders(
3322 IWinHttpRequest
*iface
,
3325 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3326 DWORD size
, err
= ERROR_SUCCESS
;
3328 TRACE("%p, %p\n", request
, headers
);
3330 if (!headers
) return E_INVALIDARG
;
3332 EnterCriticalSection( &request
->cs
);
3333 if (request
->state
< REQUEST_STATE_SENT
)
3335 err
= ERROR_WINHTTP_CANNOT_CALL_BEFORE_SEND
;
3339 if (!WinHttpQueryHeaders( request
->hrequest
, WINHTTP_QUERY_RAW_HEADERS_CRLF
, NULL
, NULL
, &size
, NULL
))
3341 err
= get_last_error();
3342 if (err
!= ERROR_INSUFFICIENT_BUFFER
) goto done
;
3344 if (!(*headers
= SysAllocStringLen( NULL
, size
/ sizeof(WCHAR
) )))
3346 err
= ERROR_OUTOFMEMORY
;
3349 err
= ERROR_SUCCESS
;
3350 if (!WinHttpQueryHeaders( request
->hrequest
, WINHTTP_QUERY_RAW_HEADERS_CRLF
, NULL
, *headers
, &size
, NULL
))
3352 err
= get_last_error();
3353 SysFreeString( *headers
);
3356 LeaveCriticalSection( &request
->cs
);
3357 return HRESULT_FROM_WIN32( err
);
3360 static void CALLBACK
wait_status_callback( HINTERNET handle
, DWORD_PTR context
, DWORD status
, LPVOID buffer
, DWORD size
)
3362 struct winhttp_request
*request
= (struct winhttp_request
*)context
;
3366 case WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE
:
3367 request
->bytes_available
= *(DWORD
*)buffer
;
3368 request
->error
= ERROR_SUCCESS
;
3370 case WINHTTP_CALLBACK_STATUS_READ_COMPLETE
:
3371 request
->bytes_read
= size
;
3372 request
->error
= ERROR_SUCCESS
;
3374 case WINHTTP_CALLBACK_STATUS_REQUEST_ERROR
:
3376 WINHTTP_ASYNC_RESULT
*result
= (WINHTTP_ASYNC_RESULT
*)buffer
;
3377 request
->error
= result
->dwError
;
3382 SetEvent( request
->wait
);
3385 static void wait_set_status_callback( struct winhttp_request
*request
, DWORD status
)
3387 status
|= WINHTTP_CALLBACK_STATUS_REQUEST_ERROR
;
3388 WinHttpSetStatusCallback( request
->hrequest
, wait_status_callback
, status
, 0 );
3391 static DWORD
wait_for_completion( struct winhttp_request
*request
)
3393 HANDLE handles
[2] = { request
->wait
, request
->cancel
};
3395 switch (WaitForMultipleObjects( 2, handles
, FALSE
, INFINITE
))
3399 case WAIT_OBJECT_0
+ 1:
3400 request
->error
= ERROR_CANCELLED
;
3403 request
->error
= get_last_error();
3406 return request
->error
;
3409 static HRESULT
request_receive( struct winhttp_request
*request
)
3411 DWORD err
, size
, buflen
= 4096;
3413 wait_set_status_callback( request
, WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE
);
3414 if (!WinHttpReceiveResponse( request
->hrequest
, NULL
))
3416 return HRESULT_FROM_WIN32( get_last_error() );
3418 if ((err
= wait_for_completion( request
))) return HRESULT_FROM_WIN32( err
);
3419 if (!strcmpW( request
->verb
, headW
))
3421 request
->state
= REQUEST_STATE_RESPONSE_RECEIVED
;
3424 if (!(request
->buffer
= heap_alloc( buflen
))) return E_OUTOFMEMORY
;
3425 request
->buffer
[0] = 0;
3429 wait_set_status_callback( request
, WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE
);
3430 if (!WinHttpQueryDataAvailable( request
->hrequest
, &request
->bytes_available
))
3432 err
= get_last_error();
3435 if ((err
= wait_for_completion( request
))) goto error
;
3436 if (!request
->bytes_available
) break;
3437 size
+= request
->bytes_available
;
3441 while (buflen
< size
) buflen
*= 2;
3442 if (!(tmp
= heap_realloc( request
->buffer
, buflen
)))
3444 err
= ERROR_OUTOFMEMORY
;
3447 request
->buffer
= tmp
;
3449 wait_set_status_callback( request
, WINHTTP_CALLBACK_STATUS_READ_COMPLETE
);
3450 if (!WinHttpReadData( request
->hrequest
, request
->buffer
+ request
->offset
,
3451 request
->bytes_available
, &request
->bytes_read
))
3453 err
= get_last_error();
3456 if ((err
= wait_for_completion( request
))) goto error
;
3457 request
->offset
+= request
->bytes_read
;
3458 } while (request
->bytes_read
);
3460 request
->state
= REQUEST_STATE_RESPONSE_RECEIVED
;
3464 heap_free( request
->buffer
);
3465 request
->buffer
= NULL
;
3466 return HRESULT_FROM_WIN32( err
);
3469 static DWORD
request_set_parameters( struct winhttp_request
*request
)
3471 if (!WinHttpSetOption( request
->hrequest
, WINHTTP_OPTION_PROXY
, &request
->proxy
,
3472 sizeof(request
->proxy
) )) return get_last_error();
3474 if (!WinHttpSetOption( request
->hrequest
, WINHTTP_OPTION_AUTOLOGON_POLICY
, &request
->logon_policy
,
3475 sizeof(request
->logon_policy
) )) return get_last_error();
3477 if (!WinHttpSetOption( request
->hrequest
, WINHTTP_OPTION_DISABLE_FEATURE
, &request
->disable_feature
,
3478 sizeof(request
->disable_feature
) )) return get_last_error();
3480 if (!WinHttpSetTimeouts( request
->hrequest
,
3481 request
->resolve_timeout
,
3482 request
->connect_timeout
,
3483 request
->send_timeout
,
3484 request
->receive_timeout
)) return get_last_error();
3485 return ERROR_SUCCESS
;
3488 static void request_set_utf8_content_type( struct winhttp_request
*request
)
3490 static const WCHAR fmtW
[] = {'%','s',':',' ','%','s',0};
3491 static const WCHAR text_plainW
[] = {'t','e','x','t','/','p','l','a','i','n',0};
3492 static const WCHAR charset_utf8W
[] = {'c','h','a','r','s','e','t','=','u','t','f','-','8',0};
3496 len
= sprintfW( headerW
, fmtW
, attr_content_type
, text_plainW
);
3497 WinHttpAddRequestHeaders( request
->hrequest
, headerW
, len
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
);
3498 len
= sprintfW( headerW
, fmtW
, attr_content_type
, charset_utf8W
);
3499 WinHttpAddRequestHeaders( request
->hrequest
, headerW
, len
, WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON
);
3502 static HRESULT
request_send( struct winhttp_request
*request
)
3504 SAFEARRAY
*sa
= NULL
;
3511 if ((err
= request_set_parameters( request
))) return HRESULT_FROM_WIN32( err
);
3512 if (strcmpW( request
->verb
, getW
))
3514 VariantInit( &data
);
3515 if (V_VT( &request
->data
) == VT_BSTR
)
3518 const WCHAR
*str
= V_BSTR( &request
->data
);
3519 int i
, len
= strlenW( str
);
3521 for (i
= 0; i
< len
; i
++)
3529 size
= WideCharToMultiByte( cp
, 0, str
, len
, NULL
, 0, NULL
, NULL
);
3530 if (!(ptr
= heap_alloc( size
))) return E_OUTOFMEMORY
;
3531 WideCharToMultiByte( cp
, 0, str
, len
, ptr
, size
, NULL
, NULL
);
3532 if (cp
== CP_UTF8
) request_set_utf8_content_type( request
);
3534 else if (VariantChangeType( &data
, &request
->data
, 0, VT_ARRAY
|VT_UI1
) == S_OK
)
3536 sa
= V_ARRAY( &data
);
3537 if ((hr
= SafeArrayAccessData( sa
, (void **)&ptr
)) != S_OK
) return hr
;
3538 if ((hr
= SafeArrayGetUBound( sa
, 1, &size
)) != S_OK
)
3540 SafeArrayUnaccessData( sa
);
3546 wait_set_status_callback( request
, WINHTTP_CALLBACK_STATUS_REQUEST_SENT
);
3547 if (!WinHttpSendRequest( request
->hrequest
, NULL
, 0, ptr
, size
, size
, 0 ))
3549 err
= get_last_error();
3552 if ((err
= wait_for_completion( request
))) goto error
;
3553 if (sa
) SafeArrayUnaccessData( sa
);
3554 else heap_free( ptr
);
3555 request
->state
= REQUEST_STATE_SENT
;
3559 if (sa
) SafeArrayUnaccessData( sa
);
3560 else heap_free( ptr
);
3561 return HRESULT_FROM_WIN32( err
);
3564 static HRESULT
request_send_and_receive( struct winhttp_request
*request
)
3566 HRESULT hr
= request_send( request
);
3567 if (hr
== S_OK
) hr
= request_receive( request
);
3571 static DWORD CALLBACK
send_and_receive_proc( void *arg
)
3573 struct winhttp_request
*request
= (struct winhttp_request
*)arg
;
3574 return request_send_and_receive( request
);
3577 /* critical section must be held */
3578 static DWORD
request_wait( struct winhttp_request
*request
, DWORD timeout
)
3580 HANDLE thread
= request
->thread
;
3583 LeaveCriticalSection( &request
->cs
);
3584 while ((err
= MsgWaitForMultipleObjects( 1, &thread
, FALSE
, timeout
, QS_ALLINPUT
)) == WAIT_OBJECT_0
+ 1)
3587 while (PeekMessageW( &msg
, NULL
, 0, 0, PM_REMOVE
))
3589 TranslateMessage( &msg
);
3590 DispatchMessageW( &msg
);
3596 ret
= ERROR_SUCCESS
;
3599 ret
= ERROR_TIMEOUT
;
3603 ret
= get_last_error();
3606 EnterCriticalSection( &request
->cs
);
3610 static HRESULT WINAPI
winhttp_request_Send(
3611 IWinHttpRequest
*iface
,
3614 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3617 TRACE("%p, %s\n", request
, debugstr_variant(&body
));
3619 EnterCriticalSection( &request
->cs
);
3620 if (request
->state
< REQUEST_STATE_OPEN
)
3622 LeaveCriticalSection( &request
->cs
);
3623 return HRESULT_FROM_WIN32( ERROR_WINHTTP_CANNOT_CALL_BEFORE_OPEN
);
3625 if (request
->state
>= REQUEST_STATE_SENT
)
3627 LeaveCriticalSection( &request
->cs
);
3630 VariantClear( &request
->data
);
3631 if ((hr
= VariantCopyInd( &request
->data
, &body
)) != S_OK
)
3633 LeaveCriticalSection( &request
->cs
);
3636 if (!(request
->thread
= CreateThread( NULL
, 0, send_and_receive_proc
, request
, 0, NULL
)))
3638 LeaveCriticalSection( &request
->cs
);
3639 return HRESULT_FROM_WIN32( get_last_error() );
3641 request
->wait
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
);
3642 request
->cancel
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
);
3643 if (!request
->async
)
3645 hr
= HRESULT_FROM_WIN32( request_wait( request
, INFINITE
) );
3647 LeaveCriticalSection( &request
->cs
);
3651 static HRESULT WINAPI
winhttp_request_get_Status(
3652 IWinHttpRequest
*iface
,
3655 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3656 DWORD err
= ERROR_SUCCESS
, flags
, status_code
, len
= sizeof(status_code
), index
= 0;
3658 TRACE("%p, %p\n", request
, status
);
3660 if (!status
) return E_INVALIDARG
;
3662 EnterCriticalSection( &request
->cs
);
3663 if (request
->state
< REQUEST_STATE_SENT
)
3665 err
= ERROR_WINHTTP_CANNOT_CALL_BEFORE_SEND
;
3668 flags
= WINHTTP_QUERY_STATUS_CODE
| WINHTTP_QUERY_FLAG_NUMBER
;
3669 if (!WinHttpQueryHeaders( request
->hrequest
, flags
, NULL
, &status_code
, &len
, &index
))
3671 err
= get_last_error();
3674 *status
= status_code
;
3677 LeaveCriticalSection( &request
->cs
);
3678 return HRESULT_FROM_WIN32( err
);
3681 static HRESULT WINAPI
winhttp_request_get_StatusText(
3682 IWinHttpRequest
*iface
,
3685 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3686 DWORD err
= ERROR_SUCCESS
, len
= 0, index
= 0;
3688 TRACE("%p, %p\n", request
, status
);
3690 if (!status
) return E_INVALIDARG
;
3692 EnterCriticalSection( &request
->cs
);
3693 if (request
->state
< REQUEST_STATE_SENT
)
3695 err
= ERROR_WINHTTP_CANNOT_CALL_BEFORE_SEND
;
3698 if (!WinHttpQueryHeaders( request
->hrequest
, WINHTTP_QUERY_STATUS_TEXT
, NULL
, NULL
, &len
, &index
))
3700 err
= get_last_error();
3701 if (err
!= ERROR_INSUFFICIENT_BUFFER
) goto done
;
3703 if (!(*status
= SysAllocStringLen( NULL
, len
/ sizeof(WCHAR
) )))
3705 err
= ERROR_OUTOFMEMORY
;
3709 err
= ERROR_SUCCESS
;
3710 if (!WinHttpQueryHeaders( request
->hrequest
, WINHTTP_QUERY_STATUS_TEXT
, NULL
, *status
, &len
, &index
))
3712 err
= get_last_error();
3713 SysFreeString( *status
);
3716 LeaveCriticalSection( &request
->cs
);
3717 return HRESULT_FROM_WIN32( err
);
3720 static DWORD
request_get_codepage( struct winhttp_request
*request
, UINT
*codepage
)
3722 static const WCHAR utf8W
[] = {'u','t','f','-','8',0};
3723 static const WCHAR charsetW
[] = {'c','h','a','r','s','e','t',0};
3728 if (!WinHttpQueryHeaders( request
->hrequest
, WINHTTP_QUERY_CONTENT_TYPE
, NULL
, NULL
, &size
, NULL
) &&
3729 get_last_error() == ERROR_INSUFFICIENT_BUFFER
)
3731 if (!(buffer
= heap_alloc( size
))) return ERROR_OUTOFMEMORY
;
3732 if (!WinHttpQueryHeaders( request
->hrequest
, WINHTTP_QUERY_CONTENT_TYPE
, NULL
, buffer
, &size
, NULL
))
3734 return get_last_error();
3736 if ((p
= strstrW( buffer
, charsetW
)))
3738 p
+= strlenW( charsetW
);
3739 while (*p
== ' ') p
++;
3742 while (*p
== ' ') p
++;
3743 if (!strcmpiW( p
, utf8W
)) *codepage
= CP_UTF8
;
3746 heap_free( buffer
);
3748 return ERROR_SUCCESS
;
3751 static HRESULT WINAPI
winhttp_request_get_ResponseText(
3752 IWinHttpRequest
*iface
,
3755 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3757 DWORD err
= ERROR_SUCCESS
;
3760 TRACE("%p, %p\n", request
, body
);
3762 if (!body
) return E_INVALIDARG
;
3764 EnterCriticalSection( &request
->cs
);
3765 if (request
->state
< REQUEST_STATE_SENT
)
3767 err
= ERROR_WINHTTP_CANNOT_CALL_BEFORE_SEND
;
3770 if ((err
= request_get_codepage( request
, &codepage
))) goto done
;
3771 len
= MultiByteToWideChar( codepage
, 0, request
->buffer
, request
->offset
, NULL
, 0 );
3772 if (!(*body
= SysAllocStringLen( NULL
, len
)))
3774 err
= ERROR_OUTOFMEMORY
;
3777 MultiByteToWideChar( codepage
, 0, request
->buffer
, request
->offset
, *body
, len
);
3781 LeaveCriticalSection( &request
->cs
);
3782 return HRESULT_FROM_WIN32( err
);
3785 static HRESULT WINAPI
winhttp_request_get_ResponseBody(
3786 IWinHttpRequest
*iface
,
3789 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3792 DWORD err
= ERROR_SUCCESS
;
3795 TRACE("%p, %p\n", request
, body
);
3797 if (!body
) return E_INVALIDARG
;
3799 EnterCriticalSection( &request
->cs
);
3800 if (request
->state
< REQUEST_STATE_SENT
)
3802 err
= ERROR_WINHTTP_CANNOT_CALL_BEFORE_SEND
;
3805 if (!(sa
= SafeArrayCreateVector( VT_UI1
, 0, request
->offset
)))
3807 err
= ERROR_OUTOFMEMORY
;
3810 if ((hr
= SafeArrayAccessData( sa
, (void **)&ptr
)) != S_OK
)
3812 SafeArrayDestroy( sa
);
3813 LeaveCriticalSection( &request
->cs
);
3816 memcpy( ptr
, request
->buffer
, request
->offset
);
3817 if ((hr
= SafeArrayUnaccessData( sa
)) != S_OK
)
3819 SafeArrayDestroy( sa
);
3820 LeaveCriticalSection( &request
->cs
);
3823 V_VT( body
) = VT_ARRAY
|VT_UI1
;
3824 V_ARRAY( body
) = sa
;
3827 LeaveCriticalSection( &request
->cs
);
3828 return HRESULT_FROM_WIN32( err
);
3833 IStream IStream_iface
;
3836 ULARGE_INTEGER pos
, size
;
3839 static inline struct stream
*impl_from_IStream( IStream
*iface
)
3841 return CONTAINING_RECORD( iface
, struct stream
, IStream_iface
);
3844 static HRESULT WINAPI
stream_QueryInterface( IStream
*iface
, REFIID riid
, void **obj
)
3846 struct stream
*stream
= impl_from_IStream( iface
);
3848 TRACE("%p, %s, %p\n", stream
, debugstr_guid(riid
), obj
);
3850 if (IsEqualGUID( riid
, &IID_IStream
) || IsEqualGUID( riid
, &IID_IUnknown
))
3856 FIXME("interface %s not implemented\n", debugstr_guid(riid
));
3857 return E_NOINTERFACE
;
3859 IStream_AddRef( iface
);
3863 static ULONG WINAPI
stream_AddRef( IStream
*iface
)
3865 struct stream
*stream
= impl_from_IStream( iface
);
3866 return InterlockedIncrement( &stream
->refs
);
3869 static ULONG WINAPI
stream_Release( IStream
*iface
)
3871 struct stream
*stream
= impl_from_IStream( iface
);
3872 LONG refs
= InterlockedDecrement( &stream
->refs
);
3875 heap_free( stream
->data
);
3876 heap_free( stream
);
3881 static HRESULT WINAPI
stream_Read( IStream
*iface
, void *buf
, ULONG len
, ULONG
*read
)
3883 struct stream
*stream
= impl_from_IStream( iface
);
3886 if (stream
->pos
.QuadPart
>= stream
->size
.QuadPart
)
3892 size
= min( stream
->size
.QuadPart
- stream
->pos
.QuadPart
, len
);
3893 memcpy( buf
, stream
->data
+ stream
->pos
.QuadPart
, size
);
3894 stream
->pos
.QuadPart
+= size
;
3900 static HRESULT WINAPI
stream_Write( IStream
*iface
, const void *buf
, ULONG len
, ULONG
*written
)
3906 static HRESULT WINAPI
stream_Seek( IStream
*iface
, LARGE_INTEGER move
, DWORD origin
, ULARGE_INTEGER
*newpos
)
3908 struct stream
*stream
= impl_from_IStream( iface
);
3910 if (origin
== STREAM_SEEK_SET
)
3911 stream
->pos
.QuadPart
= move
.QuadPart
;
3912 else if (origin
== STREAM_SEEK_CUR
)
3913 stream
->pos
.QuadPart
+= move
.QuadPart
;
3914 else if (origin
== STREAM_SEEK_END
)
3915 stream
->pos
.QuadPart
= stream
->size
.QuadPart
- move
.QuadPart
;
3917 if (newpos
) newpos
->QuadPart
= stream
->pos
.QuadPart
;
3921 static HRESULT WINAPI
stream_SetSize( IStream
*iface
, ULARGE_INTEGER newsize
)
3927 static HRESULT WINAPI
stream_CopyTo( IStream
*iface
, IStream
*stream
, ULARGE_INTEGER len
, ULARGE_INTEGER
*read
,
3928 ULARGE_INTEGER
*written
)
3934 static HRESULT WINAPI
stream_Commit( IStream
*iface
, DWORD flags
)
3940 static HRESULT WINAPI
stream_Revert( IStream
*iface
)
3946 static HRESULT WINAPI
stream_LockRegion( IStream
*iface
, ULARGE_INTEGER offset
, ULARGE_INTEGER len
, DWORD locktype
)
3952 static HRESULT WINAPI
stream_UnlockRegion( IStream
*iface
, ULARGE_INTEGER offset
, ULARGE_INTEGER len
, DWORD locktype
)
3958 static HRESULT WINAPI
stream_Stat( IStream
*iface
, STATSTG
*stg
, DWORD flag
)
3964 static HRESULT WINAPI
stream_Clone( IStream
*iface
, IStream
**stream
)
3970 static const IStreamVtbl stream_vtbl
=
3972 stream_QueryInterface
,
3983 stream_UnlockRegion
,
3988 static HRESULT WINAPI
winhttp_request_get_ResponseStream(
3989 IWinHttpRequest
*iface
,
3992 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
3993 DWORD err
= ERROR_SUCCESS
;
3994 struct stream
*stream
;
3996 TRACE("%p, %p\n", request
, body
);
3998 if (!body
) return E_INVALIDARG
;
4000 EnterCriticalSection( &request
->cs
);
4001 if (request
->state
< REQUEST_STATE_SENT
)
4003 err
= ERROR_WINHTTP_CANNOT_CALL_BEFORE_SEND
;
4006 if (!(stream
= heap_alloc( sizeof(*stream
) )))
4008 err
= ERROR_OUTOFMEMORY
;
4011 stream
->IStream_iface
.lpVtbl
= &stream_vtbl
;
4013 if (!(stream
->data
= heap_alloc( request
->offset
)))
4015 heap_free( stream
);
4016 err
= ERROR_OUTOFMEMORY
;
4019 memcpy( stream
->data
, request
->buffer
, request
->offset
);
4020 stream
->pos
.QuadPart
= 0;
4021 stream
->size
.QuadPart
= request
->offset
;
4022 V_VT( body
) = VT_UNKNOWN
;
4023 V_UNKNOWN( body
) = (IUnknown
*)&stream
->IStream_iface
;
4026 LeaveCriticalSection( &request
->cs
);
4027 return HRESULT_FROM_WIN32( err
);
4030 static HRESULT WINAPI
winhttp_request_get_Option(
4031 IWinHttpRequest
*iface
,
4032 WinHttpRequestOption option
,
4035 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
4038 TRACE("%p, %u, %p\n", request
, option
, value
);
4040 EnterCriticalSection( &request
->cs
);
4043 case WinHttpRequestOption_URLCodePage
:
4044 V_VT( value
) = VT_I4
;
4045 V_I4( value
) = request
->url_codepage
;
4048 FIXME("unimplemented option %u\n", option
);
4052 LeaveCriticalSection( &request
->cs
);
4056 static HRESULT WINAPI
winhttp_request_put_Option(
4057 IWinHttpRequest
*iface
,
4058 WinHttpRequestOption option
,
4061 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
4064 TRACE("%p, %u, %s\n", request
, option
, debugstr_variant(&value
));
4066 EnterCriticalSection( &request
->cs
);
4069 case WinHttpRequestOption_EnableRedirects
:
4071 if (V_BOOL( &value
))
4072 request
->disable_feature
&= ~WINHTTP_DISABLE_REDIRECTS
;
4074 request
->disable_feature
|= WINHTTP_DISABLE_REDIRECTS
;
4077 case WinHttpRequestOption_URLCodePage
:
4079 static const WCHAR utf8W
[] = {'u','t','f','-','8',0};
4083 hr
= VariantChangeType( &cp
, &value
, 0, VT_UI4
);
4084 if (SUCCEEDED( hr
))
4086 request
->url_codepage
= V_UI4( &cp
);
4087 TRACE("URL codepage: %u\n", request
->url_codepage
);
4089 else if (V_VT( &value
) == VT_BSTR
&& !strcmpiW( V_BSTR( &value
), utf8W
))
4091 TRACE("URL codepage: UTF-8\n");
4092 request
->url_codepage
= CP_UTF8
;
4096 FIXME("URL codepage %s is not recognized\n", debugstr_variant( &value
));
4100 FIXME("unimplemented option %u\n", option
);
4104 LeaveCriticalSection( &request
->cs
);
4108 static HRESULT WINAPI
winhttp_request_WaitForResponse(
4109 IWinHttpRequest
*iface
,
4111 VARIANT_BOOL
*succeeded
)
4113 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
4114 DWORD err
, msecs
= (V_I4(&timeout
) == -1) ? INFINITE
: V_I4(&timeout
) * 1000;
4116 TRACE("%p, %s, %p\n", request
, debugstr_variant(&timeout
), succeeded
);
4118 EnterCriticalSection( &request
->cs
);
4119 if (request
->state
>= REQUEST_STATE_RESPONSE_RECEIVED
)
4121 LeaveCriticalSection( &request
->cs
);
4124 switch ((err
= request_wait( request
, msecs
)))
4127 if (succeeded
) *succeeded
= VARIANT_FALSE
;
4128 err
= ERROR_SUCCESS
;
4132 if (succeeded
) *succeeded
= VARIANT_TRUE
;
4137 LeaveCriticalSection( &request
->cs
);
4138 return HRESULT_FROM_WIN32( err
);
4141 static HRESULT WINAPI
winhttp_request_Abort(
4142 IWinHttpRequest
*iface
)
4144 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
4146 TRACE("%p\n", request
);
4148 EnterCriticalSection( &request
->cs
);
4149 cancel_request( request
);
4150 LeaveCriticalSection( &request
->cs
);
4154 static HRESULT WINAPI
winhttp_request_SetTimeouts(
4155 IWinHttpRequest
*iface
,
4156 LONG resolve_timeout
,
4157 LONG connect_timeout
,
4159 LONG receive_timeout
)
4161 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
4163 TRACE("%p, %d, %d, %d, %d\n", request
, resolve_timeout
, connect_timeout
, send_timeout
, receive_timeout
);
4165 EnterCriticalSection( &request
->cs
);
4166 request
->resolve_timeout
= resolve_timeout
;
4167 request
->connect_timeout
= connect_timeout
;
4168 request
->send_timeout
= send_timeout
;
4169 request
->receive_timeout
= receive_timeout
;
4170 LeaveCriticalSection( &request
->cs
);
4174 static HRESULT WINAPI
winhttp_request_SetClientCertificate(
4175 IWinHttpRequest
*iface
,
4182 static HRESULT WINAPI
winhttp_request_SetAutoLogonPolicy(
4183 IWinHttpRequest
*iface
,
4184 WinHttpRequestAutoLogonPolicy policy
)
4186 struct winhttp_request
*request
= impl_from_IWinHttpRequest( iface
);
4189 TRACE("%p, %u\n", request
, policy
);
4191 EnterCriticalSection( &request
->cs
);
4194 case AutoLogonPolicy_Always
:
4195 request
->logon_policy
= WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW
;
4197 case AutoLogonPolicy_OnlyIfBypassProxy
:
4198 request
->logon_policy
= WINHTTP_AUTOLOGON_SECURITY_LEVEL_MEDIUM
;
4200 case AutoLogonPolicy_Never
:
4201 request
->logon_policy
= WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH
;
4203 default: hr
= E_INVALIDARG
;
4206 LeaveCriticalSection( &request
->cs
);
4210 static const struct IWinHttpRequestVtbl winhttp_request_vtbl
=
4212 winhttp_request_QueryInterface
,
4213 winhttp_request_AddRef
,
4214 winhttp_request_Release
,
4215 winhttp_request_GetTypeInfoCount
,
4216 winhttp_request_GetTypeInfo
,
4217 winhttp_request_GetIDsOfNames
,
4218 winhttp_request_Invoke
,
4219 winhttp_request_SetProxy
,
4220 winhttp_request_SetCredentials
,
4221 winhttp_request_Open
,
4222 winhttp_request_SetRequestHeader
,
4223 winhttp_request_GetResponseHeader
,
4224 winhttp_request_GetAllResponseHeaders
,
4225 winhttp_request_Send
,
4226 winhttp_request_get_Status
,
4227 winhttp_request_get_StatusText
,
4228 winhttp_request_get_ResponseText
,
4229 winhttp_request_get_ResponseBody
,
4230 winhttp_request_get_ResponseStream
,
4231 winhttp_request_get_Option
,
4232 winhttp_request_put_Option
,
4233 winhttp_request_WaitForResponse
,
4234 winhttp_request_Abort
,
4235 winhttp_request_SetTimeouts
,
4236 winhttp_request_SetClientCertificate
,
4237 winhttp_request_SetAutoLogonPolicy
4240 HRESULT
WinHttpRequest_create( void **obj
)
4242 struct winhttp_request
*request
;
4246 if (!(request
= heap_alloc( sizeof(*request
) ))) return E_OUTOFMEMORY
;
4247 request
->IWinHttpRequest_iface
.lpVtbl
= &winhttp_request_vtbl
;
4249 request
->state
= REQUEST_STATE_UNINITIALIZED
;
4250 request
->proxy
.lpszProxy
= NULL
;
4251 request
->proxy
.lpszProxyBypass
= NULL
;
4252 request
->url_codepage
= CP_UTF8
;
4253 InitializeCriticalSection( &request
->cs
);
4254 request
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": winhttp_request.cs");
4256 *obj
= &request
->IWinHttpRequest_iface
;
4257 TRACE("returning iface %p\n", *obj
);