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