2 * Copyright 2004 Mike McCormack for CodeWeavers
3 * Copyright 2006 Rob Shearman for CodeWeavers
4 * Copyright 2008, 2011 Hans Leidekker for CodeWeavers
5 * Copyright 2009 Juan Lang
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
28 #ifdef HAVE_ARPA_INET_H
29 # include <arpa/inet.h>
36 #include "httprequest.h"
37 #include "httprequestid.h"
41 #include "winhttp_private.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(winhttp
);
48 #include "inet_ntop.c"
51 #define DEFAULT_KEEP_ALIVE_TIMEOUT 30000
53 static const WCHAR attr_accept
[] = {'A','c','c','e','p','t',0};
54 static const WCHAR attr_accept_charset
[] = {'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0};
55 static const WCHAR attr_accept_encoding
[] = {'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0};
56 static const WCHAR attr_accept_language
[] = {'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0};
57 static const WCHAR attr_accept_ranges
[] = {'A','c','c','e','p','t','-','R','a','n','g','e','s',0};
58 static const WCHAR attr_age
[] = {'A','g','e',0};
59 static const WCHAR attr_allow
[] = {'A','l','l','o','w',0};
60 static const WCHAR attr_authorization
[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',0};
61 static const WCHAR attr_cache_control
[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l',0};
62 static const WCHAR attr_connection
[] = {'C','o','n','n','e','c','t','i','o','n',0};
63 static const WCHAR attr_content_base
[] = {'C','o','n','t','e','n','t','-','B','a','s','e',0};
64 static const WCHAR attr_content_encoding
[] = {'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0};
65 static const WCHAR attr_content_id
[] = {'C','o','n','t','e','n','t','-','I','D',0};
66 static const WCHAR attr_content_language
[] = {'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0};
67 static const WCHAR attr_content_length
[] = {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
68 static const WCHAR attr_content_location
[] = {'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0};
69 static const WCHAR attr_content_md5
[] = {'C','o','n','t','e','n','t','-','M','D','5',0};
70 static const WCHAR attr_content_range
[] = {'C','o','n','t','e','n','t','-','R','a','n','g','e',0};
71 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};
72 static const WCHAR attr_content_type
[] = {'C','o','n','t','e','n','t','-','T','y','p','e',0};
73 static const WCHAR attr_cookie
[] = {'C','o','o','k','i','e',0};
74 static const WCHAR attr_date
[] = {'D','a','t','e',0};
75 static const WCHAR attr_from
[] = {'F','r','o','m',0};
76 static const WCHAR attr_etag
[] = {'E','T','a','g',0};
77 static const WCHAR attr_expect
[] = {'E','x','p','e','c','t',0};
78 static const WCHAR attr_expires
[] = {'E','x','p','i','r','e','s',0};
79 static const WCHAR attr_host
[] = {'H','o','s','t',0};
80 static const WCHAR attr_if_match
[] = {'I','f','-','M','a','t','c','h',0};
81 static const WCHAR attr_if_modified_since
[] = {'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0};
82 static const WCHAR attr_if_none_match
[] = {'I','f','-','N','o','n','e','-','M','a','t','c','h',0};
83 static const WCHAR attr_if_range
[] = {'I','f','-','R','a','n','g','e',0};
84 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};
85 static const WCHAR attr_last_modified
[] = {'L','a','s','t','-','M','o','d','i','f','i','e','d',0};
86 static const WCHAR attr_location
[] = {'L','o','c','a','t','i','o','n',0};
87 static const WCHAR attr_max_forwards
[] = {'M','a','x','-','F','o','r','w','a','r','d','s',0};
88 static const WCHAR attr_mime_version
[] = {'M','i','m','e','-','V','e','r','s','i','o','n',0};
89 static const WCHAR attr_pragma
[] = {'P','r','a','g','m','a',0};
90 static const WCHAR attr_proxy_authenticate
[] = {'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0};
91 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};
92 static const WCHAR attr_proxy_connection
[] = {'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0};
93 static const WCHAR attr_public
[] = {'P','u','b','l','i','c',0};
94 static const WCHAR attr_range
[] = {'R','a','n','g','e',0};
95 static const WCHAR attr_referer
[] = {'R','e','f','e','r','e','r',0};
96 static const WCHAR attr_retry_after
[] = {'R','e','t','r','y','-','A','f','t','e','r',0};
97 static const WCHAR attr_server
[] = {'S','e','r','v','e','r',0};
98 static const WCHAR attr_set_cookie
[] = {'S','e','t','-','C','o','o','k','i','e',0};
99 static const WCHAR attr_status
[] = {'S','t','a','t','u','s',0};
100 static const WCHAR attr_transfer_encoding
[] = {'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0};
101 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};
102 static const WCHAR attr_upgrade
[] = {'U','p','g','r','a','d','e',0};
103 static const WCHAR attr_uri
[] = {'U','R','I',0};
104 static const WCHAR attr_user_agent
[] = {'U','s','e','r','-','A','g','e','n','t',0};
105 static const WCHAR attr_vary
[] = {'V','a','r','y',0};
106 static const WCHAR attr_via
[] = {'V','i','a',0};
107 static const WCHAR attr_warning
[] = {'W','a','r','n','i','n','g',0};
108 static const WCHAR attr_www_authenticate
[] = {'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0};
110 static const WCHAR
*attribute_table
[] =
112 attr_mime_version
, /* WINHTTP_QUERY_MIME_VERSION = 0 */
113 attr_content_type
, /* WINHTTP_QUERY_CONTENT_TYPE = 1 */
114 attr_content_transfer_encoding
, /* WINHTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
115 attr_content_id
, /* WINHTTP_QUERY_CONTENT_ID = 3 */
116 NULL
, /* WINHTTP_QUERY_CONTENT_DESCRIPTION = 4 */
117 attr_content_length
, /* WINHTTP_QUERY_CONTENT_LENGTH = 5 */
118 attr_content_language
, /* WINHTTP_QUERY_CONTENT_LANGUAGE = 6 */
119 attr_allow
, /* WINHTTP_QUERY_ALLOW = 7 */
120 attr_public
, /* WINHTTP_QUERY_PUBLIC = 8 */
121 attr_date
, /* WINHTTP_QUERY_DATE = 9 */
122 attr_expires
, /* WINHTTP_QUERY_EXPIRES = 10 */
123 attr_last_modified
, /* WINHTTP_QUERY_LAST_MODIFIEDcw = 11 */
124 NULL
, /* WINHTTP_QUERY_MESSAGE_ID = 12 */
125 attr_uri
, /* WINHTTP_QUERY_URI = 13 */
126 attr_from
, /* WINHTTP_QUERY_DERIVED_FROM = 14 */
127 NULL
, /* WINHTTP_QUERY_COST = 15 */
128 NULL
, /* WINHTTP_QUERY_LINK = 16 */
129 attr_pragma
, /* WINHTTP_QUERY_PRAGMA = 17 */
130 NULL
, /* WINHTTP_QUERY_VERSION = 18 */
131 attr_status
, /* WINHTTP_QUERY_STATUS_CODE = 19 */
132 NULL
, /* WINHTTP_QUERY_STATUS_TEXT = 20 */
133 NULL
, /* WINHTTP_QUERY_RAW_HEADERS = 21 */
134 NULL
, /* WINHTTP_QUERY_RAW_HEADERS_CRLF = 22 */
135 attr_connection
, /* WINHTTP_QUERY_CONNECTION = 23 */
136 attr_accept
, /* WINHTTP_QUERY_ACCEPT = 24 */
137 attr_accept_charset
, /* WINHTTP_QUERY_ACCEPT_CHARSET = 25 */
138 attr_accept_encoding
, /* WINHTTP_QUERY_ACCEPT_ENCODING = 26 */
139 attr_accept_language
, /* WINHTTP_QUERY_ACCEPT_LANGUAGE = 27 */
140 attr_authorization
, /* WINHTTP_QUERY_AUTHORIZATION = 28 */
141 attr_content_encoding
, /* WINHTTP_QUERY_CONTENT_ENCODING = 29 */
142 NULL
, /* WINHTTP_QUERY_FORWARDED = 30 */
143 NULL
, /* WINHTTP_QUERY_FROM = 31 */
144 attr_if_modified_since
, /* WINHTTP_QUERY_IF_MODIFIED_SINCE = 32 */
145 attr_location
, /* WINHTTP_QUERY_LOCATION = 33 */
146 NULL
, /* WINHTTP_QUERY_ORIG_URI = 34 */
147 attr_referer
, /* WINHTTP_QUERY_REFERER = 35 */
148 attr_retry_after
, /* WINHTTP_QUERY_RETRY_AFTER = 36 */
149 attr_server
, /* WINHTTP_QUERY_SERVER = 37 */
150 NULL
, /* WINHTTP_TITLE = 38 */
151 attr_user_agent
, /* WINHTTP_QUERY_USER_AGENT = 39 */
152 attr_www_authenticate
, /* WINHTTP_QUERY_WWW_AUTHENTICATE = 40 */
153 attr_proxy_authenticate
, /* WINHTTP_QUERY_PROXY_AUTHENTICATE = 41 */
154 attr_accept_ranges
, /* WINHTTP_QUERY_ACCEPT_RANGES = 42 */
155 attr_set_cookie
, /* WINHTTP_QUERY_SET_COOKIE = 43 */
156 attr_cookie
, /* WINHTTP_QUERY_COOKIE = 44 */
157 NULL
, /* WINHTTP_QUERY_REQUEST_METHOD = 45 */
158 NULL
, /* WINHTTP_QUERY_REFRESH = 46 */
159 NULL
, /* WINHTTP_QUERY_CONTENT_DISPOSITION = 47 */
160 attr_age
, /* WINHTTP_QUERY_AGE = 48 */
161 attr_cache_control
, /* WINHTTP_QUERY_CACHE_CONTROL = 49 */
162 attr_content_base
, /* WINHTTP_QUERY_CONTENT_BASE = 50 */
163 attr_content_location
, /* WINHTTP_QUERY_CONTENT_LOCATION = 51 */
164 attr_content_md5
, /* WINHTTP_QUERY_CONTENT_MD5 = 52 */
165 attr_content_range
, /* WINHTTP_QUERY_CONTENT_RANGE = 53 */
166 attr_etag
, /* WINHTTP_QUERY_ETAG = 54 */
167 attr_host
, /* WINHTTP_QUERY_HOST = 55 */
168 attr_if_match
, /* WINHTTP_QUERY_IF_MATCH = 56 */
169 attr_if_none_match
, /* WINHTTP_QUERY_IF_NONE_MATCH = 57 */
170 attr_if_range
, /* WINHTTP_QUERY_IF_RANGE = 58 */
171 attr_if_unmodified_since
, /* WINHTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
172 attr_max_forwards
, /* WINHTTP_QUERY_MAX_FORWARDS = 60 */
173 attr_proxy_authorization
, /* WINHTTP_QUERY_PROXY_AUTHORIZATION = 61 */
174 attr_range
, /* WINHTTP_QUERY_RANGE = 62 */
175 attr_transfer_encoding
, /* WINHTTP_QUERY_TRANSFER_ENCODING = 63 */
176 attr_upgrade
, /* WINHTTP_QUERY_UPGRADE = 64 */
177 attr_vary
, /* WINHTTP_QUERY_VARY = 65 */
178 attr_via
, /* WINHTTP_QUERY_VIA = 66 */
179 attr_warning
, /* WINHTTP_QUERY_WARNING = 67 */
180 attr_expect
, /* WINHTTP_QUERY_EXPECT = 68 */
181 attr_proxy_connection
, /* WINHTTP_QUERY_PROXY_CONNECTION = 69 */
182 attr_unless_modified_since
, /* WINHTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
183 NULL
, /* WINHTTP_QUERY_PROXY_SUPPORT = 75 */
184 NULL
, /* WINHTTP_QUERY_AUTHENTICATION_INFO = 76 */
185 NULL
, /* WINHTTP_QUERY_PASSPORT_URLS = 77 */
186 NULL
/* WINHTTP_QUERY_PASSPORT_CONFIG = 78 */
189 static task_header_t
*dequeue_task( request_t
*request
)
193 EnterCriticalSection( &request
->task_cs
);
194 TRACE("%u tasks queued\n", list_count( &request
->task_queue
));
195 task
= LIST_ENTRY( list_head( &request
->task_queue
), task_header_t
, entry
);
196 if (task
) list_remove( &task
->entry
);
197 LeaveCriticalSection( &request
->task_cs
);
199 TRACE("returning task %p\n", task
);
203 static DWORD CALLBACK
task_proc( LPVOID param
)
205 request_t
*request
= param
;
208 handles
[0] = request
->task_wait
;
209 handles
[1] = request
->task_cancel
;
212 DWORD err
= WaitForMultipleObjects( 2, handles
, FALSE
, INFINITE
);
218 while ((task
= dequeue_task( request
)))
221 release_object( &task
->request
->hdr
);
226 case WAIT_OBJECT_0
+ 1:
228 CloseHandle( request
->task_cancel
);
229 CloseHandle( request
->task_wait
);
230 request
->task_cs
.DebugInfo
->Spare
[0] = 0;
231 DeleteCriticalSection( &request
->task_cs
);
232 request
->hdr
.vtbl
->destroy( &request
->hdr
);
236 ERR("wait failed %u (%u)\n", err
, GetLastError());
243 static BOOL
queue_task( task_header_t
*task
)
245 request_t
*request
= task
->request
;
247 if (!request
->task_thread
)
249 if (!(request
->task_wait
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
))) return FALSE
;
250 if (!(request
->task_cancel
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
)))
252 CloseHandle( request
->task_wait
);
253 request
->task_wait
= NULL
;
256 if (!(request
->task_thread
= CreateThread( NULL
, 0, task_proc
, request
, 0, NULL
)))
258 CloseHandle( request
->task_wait
);
259 request
->task_wait
= NULL
;
260 CloseHandle( request
->task_cancel
);
261 request
->task_cancel
= NULL
;
264 InitializeCriticalSection( &request
->task_cs
);
265 request
->task_cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": request.task_cs");
268 EnterCriticalSection( &request
->task_cs
);
269 TRACE("queueing task %p\n", task
);
270 list_add_tail( &request
->task_queue
, &task
->entry
);
271 LeaveCriticalSection( &request
->task_cs
);
273 SetEvent( request
->task_wait
);
277 static void free_header( header_t
*header
)
279 heap_free( header
->field
);
280 heap_free( header
->value
);
284 static BOOL
valid_token_char( WCHAR c
)
286 if (c
< 32 || c
== 127) return FALSE
;
293 case '\\': case '\"':
305 static header_t
*parse_header( LPCWSTR string
)
312 if (!(q
= strchrW( p
, ':' )))
314 WARN("no ':' in line %s\n", debugstr_w(string
));
319 WARN("empty field name in line %s\n", debugstr_w(string
));
324 if (!valid_token_char( *p
))
326 WARN("invalid character in field name %s\n", debugstr_w(string
));
332 if (!(header
= heap_alloc_zero( sizeof(header_t
) ))) return NULL
;
333 if (!(header
->field
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) )))
338 memcpy( header
->field
, string
, len
* sizeof(WCHAR
) );
339 header
->field
[len
] = 0;
341 q
++; /* skip past colon */
342 while (*q
== ' ') q
++;
345 if (!(header
->value
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) )))
347 free_header( header
);
350 memcpy( header
->value
, q
, len
* sizeof(WCHAR
) );
351 header
->value
[len
] = 0;
356 static int get_header_index( request_t
*request
, LPCWSTR field
, int requested_index
, BOOL request_only
)
360 TRACE("%s\n", debugstr_w(field
));
362 for (index
= 0; index
< request
->num_headers
; index
++)
364 if (strcmpiW( request
->headers
[index
].field
, field
)) continue;
365 if (request_only
&& !request
->headers
[index
].is_request
) continue;
366 if (!request_only
&& request
->headers
[index
].is_request
) continue;
368 if (!requested_index
) break;
371 if (index
>= request
->num_headers
) index
= -1;
372 TRACE("returning %d\n", index
);
376 static BOOL
insert_header( request_t
*request
, header_t
*header
)
378 DWORD count
= request
->num_headers
+ 1;
381 if (request
->headers
)
382 hdrs
= heap_realloc_zero( request
->headers
, sizeof(header_t
) * count
);
384 hdrs
= heap_alloc_zero( sizeof(header_t
) );
385 if (!hdrs
) return FALSE
;
387 request
->headers
= hdrs
;
388 request
->headers
[count
- 1].field
= strdupW( header
->field
);
389 request
->headers
[count
- 1].value
= strdupW( header
->value
);
390 request
->headers
[count
- 1].is_request
= header
->is_request
;
391 request
->num_headers
= count
;
395 static BOOL
delete_header( request_t
*request
, DWORD index
)
397 if (!request
->num_headers
) return FALSE
;
398 if (index
>= request
->num_headers
) return FALSE
;
399 request
->num_headers
--;
401 heap_free( request
->headers
[index
].field
);
402 heap_free( request
->headers
[index
].value
);
404 memmove( &request
->headers
[index
], &request
->headers
[index
+ 1], (request
->num_headers
- index
) * sizeof(header_t
) );
405 memset( &request
->headers
[request
->num_headers
], 0, sizeof(header_t
) );
409 BOOL
process_header( request_t
*request
, LPCWSTR field
, LPCWSTR value
, DWORD flags
, BOOL request_only
)
414 TRACE("%s: %s 0x%08x\n", debugstr_w(field
), debugstr_w(value
), flags
);
416 if ((index
= get_header_index( request
, field
, 0, request_only
)) >= 0)
418 if (flags
& WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
) return FALSE
;
421 if (flags
& WINHTTP_ADDREQ_FLAG_REPLACE
)
425 delete_header( request
, index
);
426 if (!value
|| !value
[0]) return TRUE
;
428 else if (!(flags
& WINHTTP_ADDREQ_FLAG_ADD
))
430 set_last_error( ERROR_WINHTTP_HEADER_NOT_FOUND
);
434 hdr
.field
= (LPWSTR
)field
;
435 hdr
.value
= (LPWSTR
)value
;
436 hdr
.is_request
= request_only
;
437 return insert_header( request
, &hdr
);
442 if ((flags
& (WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA
| WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON
)) &&
446 int len
, len_orig
, len_value
;
447 header_t
*header
= &request
->headers
[index
];
449 len_orig
= strlenW( header
->value
);
450 len_value
= strlenW( value
);
452 len
= len_orig
+ len_value
+ 2;
453 if (!(tmp
= heap_realloc( header
->value
, (len
+ 1) * sizeof(WCHAR
) ))) return FALSE
;
455 header
->value
[len_orig
++] = (flags
& WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA
) ? ',' : ';';
456 header
->value
[len_orig
++] = ' ';
458 memcpy( &header
->value
[len_orig
], value
, len_value
* sizeof(WCHAR
) );
459 header
->value
[len
] = 0;
464 hdr
.field
= (LPWSTR
)field
;
465 hdr
.value
= (LPWSTR
)value
;
466 hdr
.is_request
= request_only
;
467 return insert_header( request
, &hdr
);
474 BOOL
add_request_headers( request_t
*request
, LPCWSTR headers
, DWORD len
, DWORD flags
)
477 WCHAR
*buffer
, *p
, *q
;
480 if (len
== ~0u) len
= strlenW( headers
);
481 if (!len
) return TRUE
;
482 if (!(buffer
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) return FALSE
;
483 memcpy( buffer
, headers
, len
* sizeof(WCHAR
) );
492 if (q
[0] == '\n' && q
[1] == '\r')
497 if (q
[0] == '\r' && q
[1] == '\n') break;
504 q
+= 2; /* jump over \r\n */
506 if ((header
= parse_header( p
)))
508 ret
= process_header( request
, header
->field
, header
->value
, flags
, TRUE
);
509 free_header( header
);
518 /***********************************************************************
519 * WinHttpAddRequestHeaders (winhttp.@)
521 BOOL WINAPI
WinHttpAddRequestHeaders( HINTERNET hrequest
, LPCWSTR headers
, DWORD len
, DWORD flags
)
526 TRACE("%p, %s, %u, 0x%08x\n", hrequest
, debugstr_wn(headers
, len
), len
, flags
);
528 if (!headers
|| !len
)
530 set_last_error( ERROR_INVALID_PARAMETER
);
533 if (!(request
= (request_t
*)grab_object( hrequest
)))
535 set_last_error( ERROR_INVALID_HANDLE
);
538 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
540 release_object( &request
->hdr
);
541 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
545 ret
= add_request_headers( request
, headers
, len
, flags
);
547 release_object( &request
->hdr
);
548 if (ret
) set_last_error( ERROR_SUCCESS
);
552 static WCHAR
*build_request_path( request_t
*request
)
556 if (strcmpiW( request
->connect
->hostname
, request
->connect
->servername
))
558 static const WCHAR http
[] = { 'h','t','t','p',0 };
559 static const WCHAR https
[] = { 'h','t','t','p','s',0 };
560 static const WCHAR fmt
[] = { '%','s',':','/','/','%','s',0 };
561 LPCWSTR scheme
= (request
->netconn
? request
->netconn
->secure
: (request
->hdr
.flags
& WINHTTP_FLAG_SECURE
)) ? https
: http
;
564 len
= strlenW( scheme
) + strlenW( request
->connect
->hostname
);
565 /* 3 characters for '://', 1 for NUL. */
567 if (request
->connect
->hostport
)
569 /* 1 for ':' between host and port, up to 5 for port */
573 len
+= strlenW( request
->path
);
574 if ((ret
= heap_alloc( len
* sizeof(WCHAR
) )))
576 sprintfW( ret
, fmt
, scheme
, request
->connect
->hostname
);
577 if (request
->connect
->hostport
)
579 static const WCHAR colonFmt
[] = { ':','%','u',0 };
581 sprintfW( ret
+ strlenW( ret
), colonFmt
,
582 request
->connect
->hostport
);
585 strcatW( ret
, request
->path
);
593 static WCHAR
*build_request_string( request_t
*request
)
595 static const WCHAR space
[] = {' ',0};
596 static const WCHAR crlf
[] = {'\r','\n',0};
597 static const WCHAR colon
[] = {':',' ',0};
598 static const WCHAR twocrlf
[] = {'\r','\n','\r','\n',0};
601 const WCHAR
**headers
, **p
;
602 unsigned int len
, i
= 0, j
;
604 /* allocate space for an array of all the string pointers to be added */
605 len
= request
->num_headers
* 4 + 7;
606 if (!(headers
= heap_alloc( len
* sizeof(LPCWSTR
) ))) return NULL
;
608 path
= build_request_path( request
);
609 headers
[i
++] = request
->verb
;
610 headers
[i
++] = space
;
612 headers
[i
++] = space
;
613 headers
[i
++] = request
->version
;
615 for (j
= 0; j
< request
->num_headers
; j
++)
617 if (request
->headers
[j
].is_request
)
620 headers
[i
++] = request
->headers
[j
].field
;
621 headers
[i
++] = colon
;
622 headers
[i
++] = request
->headers
[j
].value
;
624 TRACE("adding header %s (%s)\n", debugstr_w(request
->headers
[j
].field
),
625 debugstr_w(request
->headers
[j
].value
));
628 headers
[i
++] = twocrlf
;
632 for (p
= headers
; *p
; p
++) len
+= strlenW( *p
);
635 if (!(ret
= heap_alloc( len
* sizeof(WCHAR
) )))
638 for (p
= headers
; *p
; p
++) strcatW( ret
, *p
);
641 if (path
!= request
->path
)
643 heap_free( headers
);
647 #define QUERY_MODIFIER_MASK (WINHTTP_QUERY_FLAG_REQUEST_HEADERS | WINHTTP_QUERY_FLAG_SYSTEMTIME | WINHTTP_QUERY_FLAG_NUMBER)
649 static BOOL
query_headers( request_t
*request
, DWORD level
, LPCWSTR name
, LPVOID buffer
, LPDWORD buflen
, LPDWORD index
)
651 header_t
*header
= NULL
;
652 BOOL request_only
, ret
= FALSE
;
653 int requested_index
, header_index
= -1;
656 request_only
= level
& WINHTTP_QUERY_FLAG_REQUEST_HEADERS
;
657 requested_index
= index
? *index
: 0;
659 attr
= level
& ~QUERY_MODIFIER_MASK
;
662 case WINHTTP_QUERY_CUSTOM
:
664 header_index
= get_header_index( request
, name
, requested_index
, request_only
);
667 case WINHTTP_QUERY_RAW_HEADERS
:
669 WCHAR
*headers
, *p
, *q
;
672 headers
= build_request_string( request
);
674 headers
= request
->raw_headers
;
676 if (!(p
= headers
)) return FALSE
;
677 for (len
= 0; *p
; p
++) if (*p
!= '\r') len
++;
679 if (!buffer
|| len
* sizeof(WCHAR
) > *buflen
)
680 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
683 for (p
= headers
, q
= buffer
; *p
; p
++, q
++)
685 if (*p
!= '\r') *q
= *p
;
692 TRACE("returning data: %s\n", debugstr_wn(buffer
, len
));
696 *buflen
= len
* sizeof(WCHAR
);
697 if (request_only
) heap_free( headers
);
700 case WINHTTP_QUERY_RAW_HEADERS_CRLF
:
705 headers
= build_request_string( request
);
707 headers
= request
->raw_headers
;
709 if (!headers
) return FALSE
;
710 len
= strlenW( headers
) * sizeof(WCHAR
);
711 if (!buffer
|| len
+ sizeof(WCHAR
) > *buflen
)
713 len
+= sizeof(WCHAR
);
714 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
718 memcpy( buffer
, headers
, len
+ sizeof(WCHAR
) );
719 TRACE("returning data: %s\n", debugstr_wn(buffer
, len
/ sizeof(WCHAR
)));
723 if (request_only
) heap_free( headers
);
726 case WINHTTP_QUERY_VERSION
:
727 len
= strlenW( request
->version
) * sizeof(WCHAR
);
728 if (!buffer
|| len
+ sizeof(WCHAR
) > *buflen
)
730 len
+= sizeof(WCHAR
);
731 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
735 strcpyW( buffer
, request
->version
);
736 TRACE("returning string: %s\n", debugstr_w(buffer
));
742 case WINHTTP_QUERY_STATUS_TEXT
:
743 len
= strlenW( request
->status_text
) * sizeof(WCHAR
);
744 if (!buffer
|| len
+ sizeof(WCHAR
) > *buflen
)
746 len
+= sizeof(WCHAR
);
747 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
751 strcpyW( buffer
, request
->status_text
);
752 TRACE("returning string: %s\n", debugstr_w(buffer
));
759 if (attr
>= sizeof(attribute_table
)/sizeof(attribute_table
[0]) || !attribute_table
[attr
])
761 FIXME("attribute %u not implemented\n", attr
);
764 TRACE("attribute %s\n", debugstr_w(attribute_table
[attr
]));
765 header_index
= get_header_index( request
, attribute_table
[attr
], requested_index
, request_only
);
769 if (header_index
>= 0)
771 header
= &request
->headers
[header_index
];
773 if (!header
|| (request_only
&& !header
->is_request
))
775 set_last_error( ERROR_WINHTTP_HEADER_NOT_FOUND
);
778 if (index
) *index
+= 1;
779 if (level
& WINHTTP_QUERY_FLAG_NUMBER
)
781 if (!buffer
|| sizeof(int) > *buflen
)
783 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
787 int *number
= buffer
;
788 *number
= atoiW( header
->value
);
789 TRACE("returning number: %d\n", *number
);
792 *buflen
= sizeof(int);
794 else if (level
& WINHTTP_QUERY_FLAG_SYSTEMTIME
)
796 SYSTEMTIME
*st
= buffer
;
797 if (!buffer
|| sizeof(SYSTEMTIME
) > *buflen
)
799 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
801 else if ((ret
= WinHttpTimeToSystemTime( header
->value
, st
)))
803 TRACE("returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
804 st
->wYear
, st
->wMonth
, st
->wDay
, st
->wDayOfWeek
,
805 st
->wHour
, st
->wMinute
, st
->wSecond
, st
->wMilliseconds
);
807 *buflen
= sizeof(SYSTEMTIME
);
809 else if (header
->value
)
811 len
= strlenW( header
->value
) * sizeof(WCHAR
);
812 if (!buffer
|| len
+ sizeof(WCHAR
) > *buflen
)
814 len
+= sizeof(WCHAR
);
815 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
819 strcpyW( buffer
, header
->value
);
820 TRACE("returning string: %s\n", debugstr_w(buffer
));
828 /***********************************************************************
829 * WinHttpQueryHeaders (winhttp.@)
831 BOOL WINAPI
WinHttpQueryHeaders( HINTERNET hrequest
, DWORD level
, LPCWSTR name
, LPVOID buffer
, LPDWORD buflen
, LPDWORD index
)
836 TRACE("%p, 0x%08x, %s, %p, %p, %p\n", hrequest
, level
, debugstr_w(name
), buffer
, buflen
, index
);
838 if (!(request
= (request_t
*)grab_object( hrequest
)))
840 set_last_error( ERROR_INVALID_HANDLE
);
843 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
845 release_object( &request
->hdr
);
846 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
850 ret
= query_headers( request
, level
, name
, buffer
, buflen
, index
);
852 release_object( &request
->hdr
);
853 if (ret
) set_last_error( ERROR_SUCCESS
);
858 #define ARRAYSIZE(array) (sizeof(array) / sizeof((array)[0]))
860 static const WCHAR basicW
[] = {'B','a','s','i','c',0};
861 static const WCHAR ntlmW
[] = {'N','T','L','M',0};
862 static const WCHAR passportW
[] = {'P','a','s','s','p','o','r','t',0};
863 static const WCHAR digestW
[] = {'D','i','g','e','s','t',0};
864 static const WCHAR negotiateW
[] = {'N','e','g','o','t','i','a','t','e',0};
874 { basicW
, ARRAYSIZE(basicW
) - 1, WINHTTP_AUTH_SCHEME_BASIC
},
875 { ntlmW
, ARRAYSIZE(ntlmW
) - 1, WINHTTP_AUTH_SCHEME_NTLM
},
876 { passportW
, ARRAYSIZE(passportW
) - 1, WINHTTP_AUTH_SCHEME_PASSPORT
},
877 { digestW
, ARRAYSIZE(digestW
) - 1, WINHTTP_AUTH_SCHEME_DIGEST
},
878 { negotiateW
, ARRAYSIZE(negotiateW
) - 1, WINHTTP_AUTH_SCHEME_NEGOTIATE
}
880 static const unsigned int num_auth_schemes
= sizeof(auth_schemes
)/sizeof(auth_schemes
[0]);
882 static enum auth_scheme
scheme_from_flag( DWORD flag
)
886 for (i
= 0; i
< num_auth_schemes
; i
++) if (flag
== auth_schemes
[i
].scheme
) return i
;
887 return SCHEME_INVALID
;
890 static DWORD
auth_scheme_from_header( WCHAR
*header
)
894 for (i
= 0; i
< num_auth_schemes
; i
++)
896 if (!strncmpiW( header
, auth_schemes
[i
].str
, auth_schemes
[i
].len
) &&
897 (header
[auth_schemes
[i
].len
] == ' ' || !header
[auth_schemes
[i
].len
])) return auth_schemes
[i
].scheme
;
902 static BOOL
query_auth_schemes( request_t
*request
, DWORD level
, LPDWORD supported
, LPDWORD first
)
904 DWORD index
= 0, supported_schemes
= 0, first_scheme
= 0;
913 query_headers( request
, level
, NULL
, NULL
, &size
, &index
);
914 if (get_last_error() != ERROR_INSUFFICIENT_BUFFER
) break;
917 if (!(buffer
= heap_alloc( size
))) return FALSE
;
918 if (!query_headers( request
, level
, NULL
, buffer
, &size
, &index
))
923 scheme
= auth_scheme_from_header( buffer
);
925 if (!scheme
) continue;
927 if (!first_scheme
) first_scheme
= scheme
;
928 supported_schemes
|= scheme
;
935 *supported
= supported_schemes
;
936 *first
= first_scheme
;
941 /***********************************************************************
942 * WinHttpQueryAuthSchemes (winhttp.@)
944 BOOL WINAPI
WinHttpQueryAuthSchemes( HINTERNET hrequest
, LPDWORD supported
, LPDWORD first
, LPDWORD target
)
949 TRACE("%p, %p, %p, %p\n", hrequest
, supported
, first
, target
);
951 if (!(request
= (request_t
*)grab_object( hrequest
)))
953 set_last_error( ERROR_INVALID_HANDLE
);
956 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
958 release_object( &request
->hdr
);
959 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
962 if (!supported
|| !first
|| !target
)
964 release_object( &request
->hdr
);
965 set_last_error( ERROR_INVALID_PARAMETER
);
970 if (query_auth_schemes( request
, WINHTTP_QUERY_WWW_AUTHENTICATE
, supported
, first
))
972 *target
= WINHTTP_AUTH_TARGET_SERVER
;
975 else if (query_auth_schemes( request
, WINHTTP_QUERY_PROXY_AUTHENTICATE
, supported
, first
))
977 *target
= WINHTTP_AUTH_TARGET_PROXY
;
981 release_object( &request
->hdr
);
982 if (ret
) set_last_error( ERROR_SUCCESS
);
986 static UINT
encode_base64( const char *bin
, unsigned int len
, WCHAR
*base64
)
989 static const char base64enc
[] =
990 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
994 /* first 6 bits, all from bin[0] */
995 base64
[n
++] = base64enc
[(bin
[0] & 0xfc) >> 2];
996 x
= (bin
[0] & 3) << 4;
998 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1001 base64
[n
++] = base64enc
[x
];
1006 base64
[n
++] = base64enc
[x
| ((bin
[1] & 0xf0) >> 4)];
1007 x
= (bin
[1] & 0x0f) << 2;
1009 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1012 base64
[n
++] = base64enc
[x
];
1016 base64
[n
++] = base64enc
[x
| ((bin
[2] & 0xc0) >> 6)];
1018 /* last 6 bits, all from bin [2] */
1019 base64
[n
++] = base64enc
[bin
[2] & 0x3f];
1027 static inline char decode_char( WCHAR c
)
1029 if (c
>= 'A' && c
<= 'Z') return c
- 'A';
1030 if (c
>= 'a' && c
<= 'z') return c
- 'a' + 26;
1031 if (c
>= '0' && c
<= '9') return c
- '0' + 52;
1032 if (c
== '+') return 62;
1033 if (c
== '/') return 63;
1037 static unsigned int decode_base64( const WCHAR
*base64
, unsigned int len
, char *buf
)
1040 char c0
, c1
, c2
, c3
;
1041 const WCHAR
*p
= base64
;
1045 if ((c0
= decode_char( p
[0] )) > 63) return 0;
1046 if ((c1
= decode_char( p
[1] )) > 63) return 0;
1047 if ((c2
= decode_char( p
[2] )) > 63) return 0;
1048 if ((c3
= decode_char( p
[3] )) > 63) return 0;
1052 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
1053 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
1054 buf
[i
+ 2] = (c2
<< 6) | c3
;
1062 if ((c0
= decode_char( p
[0] )) > 63) return 0;
1063 if ((c1
= decode_char( p
[1] )) > 63) return 0;
1065 if (buf
) buf
[i
] = (c0
<< 2) | (c1
>> 4);
1068 else if (p
[3] == '=')
1070 if ((c0
= decode_char( p
[0] )) > 63) return 0;
1071 if ((c1
= decode_char( p
[1] )) > 63) return 0;
1072 if ((c2
= decode_char( p
[2] )) > 63) return 0;
1076 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
1077 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
1083 if ((c0
= decode_char( p
[0] )) > 63) return 0;
1084 if ((c1
= decode_char( p
[1] )) > 63) return 0;
1085 if ((c2
= decode_char( p
[2] )) > 63) return 0;
1086 if ((c3
= decode_char( p
[3] )) > 63) return 0;
1090 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
1091 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
1092 buf
[i
+ 2] = (c2
<< 6) | c3
;
1099 static struct authinfo
*alloc_authinfo(void)
1101 struct authinfo
*ret
;
1103 if (!(ret
= heap_alloc( sizeof(*ret
) ))) return NULL
;
1105 SecInvalidateHandle( &ret
->cred
);
1106 SecInvalidateHandle( &ret
->ctx
);
1107 memset( &ret
->exp
, 0, sizeof(ret
->exp
) );
1113 ret
->finished
= FALSE
;
1117 void destroy_authinfo( struct authinfo
*authinfo
)
1119 if (!authinfo
) return;
1121 if (SecIsValidHandle( &authinfo
->ctx
))
1122 DeleteSecurityContext( &authinfo
->ctx
);
1123 if (SecIsValidHandle( &authinfo
->cred
))
1124 FreeCredentialsHandle( &authinfo
->cred
);
1126 heap_free( authinfo
->data
);
1127 heap_free( authinfo
);
1130 static BOOL
get_authvalue( request_t
*request
, DWORD level
, DWORD scheme
, WCHAR
*buffer
, DWORD len
)
1132 DWORD size
, index
= 0;
1136 if (!query_headers( request
, level
, NULL
, buffer
, &size
, &index
)) return FALSE
;
1137 if (auth_scheme_from_header( buffer
) == scheme
) break;
1142 static BOOL
do_authorization( request_t
*request
, DWORD target
, DWORD scheme_flag
)
1144 struct authinfo
*authinfo
, **auth_ptr
;
1145 enum auth_scheme scheme
= scheme_from_flag( scheme_flag
);
1146 const WCHAR
*auth_target
, *username
, *password
;
1147 WCHAR auth_value
[2048], *auth_reply
;
1148 DWORD len
= sizeof(auth_value
), len_scheme
, flags
;
1149 BOOL ret
, has_auth_value
;
1151 if (scheme
== SCHEME_INVALID
) return FALSE
;
1155 case WINHTTP_AUTH_TARGET_SERVER
:
1156 has_auth_value
= get_authvalue( request
, WINHTTP_QUERY_WWW_AUTHENTICATE
, scheme_flag
, auth_value
, len
);
1157 auth_ptr
= &request
->authinfo
;
1158 auth_target
= attr_authorization
;
1159 if (request
->creds
[TARGET_SERVER
][scheme
].username
)
1161 if (scheme
!= SCHEME_BASIC
&& !has_auth_value
) return FALSE
;
1162 username
= request
->creds
[TARGET_SERVER
][scheme
].username
;
1163 password
= request
->creds
[TARGET_SERVER
][scheme
].password
;
1167 if (!has_auth_value
) return FALSE
;
1168 username
= request
->connect
->username
;
1169 password
= request
->connect
->password
;
1173 case WINHTTP_AUTH_TARGET_PROXY
:
1174 if (!get_authvalue( request
, WINHTTP_QUERY_PROXY_AUTHENTICATE
, scheme_flag
, auth_value
, len
))
1176 auth_ptr
= &request
->proxy_authinfo
;
1177 auth_target
= attr_proxy_authorization
;
1178 if (request
->creds
[TARGET_PROXY
][scheme
].username
)
1180 username
= request
->creds
[TARGET_PROXY
][scheme
].username
;
1181 password
= request
->creds
[TARGET_PROXY
][scheme
].password
;
1185 username
= request
->connect
->session
->proxy_username
;
1186 password
= request
->connect
->session
->proxy_password
;
1191 WARN("unknown target %x\n", target
);
1194 authinfo
= *auth_ptr
;
1200 int userlen
, passlen
;
1202 if (!username
|| !password
) return FALSE
;
1203 if ((!authinfo
&& !(authinfo
= alloc_authinfo())) || authinfo
->finished
) return FALSE
;
1205 userlen
= WideCharToMultiByte( CP_UTF8
, 0, username
, strlenW( username
), NULL
, 0, NULL
, NULL
);
1206 passlen
= WideCharToMultiByte( CP_UTF8
, 0, password
, strlenW( password
), NULL
, 0, NULL
, NULL
);
1208 authinfo
->data_len
= userlen
+ 1 + passlen
;
1209 if (!(authinfo
->data
= heap_alloc( authinfo
->data_len
))) return FALSE
;
1211 WideCharToMultiByte( CP_UTF8
, 0, username
, -1, authinfo
->data
, userlen
, NULL
, NULL
);
1212 authinfo
->data
[userlen
] = ':';
1213 WideCharToMultiByte( CP_UTF8
, 0, password
, -1, authinfo
->data
+ userlen
+ 1, passlen
, NULL
, NULL
);
1215 authinfo
->scheme
= SCHEME_BASIC
;
1216 authinfo
->finished
= TRUE
;
1220 case SCHEME_NEGOTIATE
:
1222 SECURITY_STATUS status
;
1223 SecBufferDesc out_desc
, in_desc
;
1225 ULONG flags
= ISC_REQ_CONNECTION
|ISC_REQ_USE_DCE_STYLE
|ISC_REQ_MUTUAL_AUTH
|ISC_REQ_DELEGATE
;
1232 SEC_WINNT_AUTH_IDENTITY_W id
;
1233 WCHAR
*domain
, *user
;
1235 if (!username
|| !password
|| !(authinfo
= alloc_authinfo())) return FALSE
;
1238 domain
= (WCHAR
*)username
;
1239 user
= strchrW( username
, '\\' );
1244 user
= (WCHAR
*)username
;
1247 id
.Flags
= SEC_WINNT_AUTH_IDENTITY_UNICODE
;
1249 id
.UserLength
= strlenW( user
);
1251 id
.DomainLength
= domain
? user
- domain
- 1 : 0;
1252 id
.Password
= (WCHAR
*)password
;
1253 id
.PasswordLength
= strlenW( password
);
1255 status
= AcquireCredentialsHandleW( NULL
, (SEC_WCHAR
*)auth_schemes
[scheme
].str
,
1256 SECPKG_CRED_OUTBOUND
, NULL
, &id
, NULL
, NULL
,
1257 &authinfo
->cred
, &exp
);
1258 if (status
== SEC_E_OK
)
1261 status
= QuerySecurityPackageInfoW( (SEC_WCHAR
*)auth_schemes
[scheme
].str
, &info
);
1262 if (status
== SEC_E_OK
)
1264 authinfo
->max_token
= info
->cbMaxToken
;
1265 FreeContextBuffer( info
);
1268 if (status
!= SEC_E_OK
)
1270 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
1271 debugstr_w(auth_schemes
[scheme
].str
), status
);
1272 heap_free( authinfo
);
1275 authinfo
->scheme
= scheme
;
1277 else if (authinfo
->finished
) return FALSE
;
1279 if ((strlenW( auth_value
) < auth_schemes
[authinfo
->scheme
].len
||
1280 strncmpiW( auth_value
, auth_schemes
[authinfo
->scheme
].str
, auth_schemes
[authinfo
->scheme
].len
)))
1282 ERR("authentication scheme changed from %s to %s\n",
1283 debugstr_w(auth_schemes
[authinfo
->scheme
].str
), debugstr_w(auth_value
));
1284 destroy_authinfo( authinfo
);
1288 in
.BufferType
= SECBUFFER_TOKEN
;
1292 in_desc
.ulVersion
= 0;
1293 in_desc
.cBuffers
= 1;
1294 in_desc
.pBuffers
= &in
;
1296 p
= auth_value
+ auth_schemes
[scheme
].len
;
1299 int len
= strlenW( ++p
);
1300 in
.cbBuffer
= decode_base64( p
, len
, NULL
);
1301 if (!(in
.pvBuffer
= heap_alloc( in
.cbBuffer
))) {
1302 destroy_authinfo( authinfo
);
1306 decode_base64( p
, len
, in
.pvBuffer
);
1308 out
.BufferType
= SECBUFFER_TOKEN
;
1309 out
.cbBuffer
= authinfo
->max_token
;
1310 if (!(out
.pvBuffer
= heap_alloc( authinfo
->max_token
)))
1312 heap_free( in
.pvBuffer
);
1313 destroy_authinfo( authinfo
);
1317 out_desc
.ulVersion
= 0;
1318 out_desc
.cBuffers
= 1;
1319 out_desc
.pBuffers
= &out
;
1321 status
= InitializeSecurityContextW( first
? &authinfo
->cred
: NULL
, first
? NULL
: &authinfo
->ctx
,
1322 first
? request
->connect
->servername
: NULL
, flags
, 0,
1323 SECURITY_NETWORK_DREP
, in
.pvBuffer
? &in_desc
: NULL
, 0,
1324 &authinfo
->ctx
, &out_desc
, &authinfo
->attr
, &authinfo
->exp
);
1325 heap_free( in
.pvBuffer
);
1326 if (status
== SEC_E_OK
)
1328 heap_free( authinfo
->data
);
1329 authinfo
->data
= out
.pvBuffer
;
1330 authinfo
->data_len
= out
.cbBuffer
;
1331 authinfo
->finished
= TRUE
;
1332 TRACE("sending last auth packet\n");
1334 else if (status
== SEC_I_CONTINUE_NEEDED
)
1336 heap_free( authinfo
->data
);
1337 authinfo
->data
= out
.pvBuffer
;
1338 authinfo
->data_len
= out
.cbBuffer
;
1339 TRACE("sending next auth packet\n");
1343 ERR("InitializeSecurityContextW failed with error 0x%08x\n", status
);
1344 heap_free( out
.pvBuffer
);
1345 destroy_authinfo( authinfo
);
1352 ERR("invalid scheme %u\n", scheme
);
1355 *auth_ptr
= authinfo
;
1357 len_scheme
= auth_schemes
[authinfo
->scheme
].len
;
1358 len
= len_scheme
+ 1 + ((authinfo
->data_len
+ 2) * 4) / 3;
1359 if (!(auth_reply
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) return FALSE
;
1361 memcpy( auth_reply
, auth_schemes
[authinfo
->scheme
].str
, len_scheme
* sizeof(WCHAR
) );
1362 auth_reply
[len_scheme
] = ' ';
1363 encode_base64( authinfo
->data
, authinfo
->data_len
, auth_reply
+ len_scheme
+ 1 );
1365 flags
= WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
;
1366 ret
= process_header( request
, auth_target
, auth_reply
, flags
, TRUE
);
1367 heap_free( auth_reply
);
1371 static LPWSTR
concatenate_string_list( LPCWSTR
*list
, int len
)
1376 for( t
= list
; *t
; t
++ )
1377 len
+= strlenW( *t
);
1380 str
= heap_alloc( len
* sizeof(WCHAR
) );
1381 if (!str
) return NULL
;
1384 for( t
= list
; *t
; t
++ )
1390 static LPWSTR
build_header_request_string( request_t
*request
, LPCWSTR verb
,
1391 LPCWSTR path
, LPCWSTR version
)
1393 static const WCHAR crlf
[] = {'\r','\n',0};
1394 static const WCHAR space
[] = { ' ',0 };
1395 static const WCHAR colon
[] = { ':',' ',0 };
1396 static const WCHAR twocrlf
[] = {'\r','\n','\r','\n', 0};
1397 LPWSTR requestString
;
1403 /* allocate space for an array of all the string pointers to be added */
1404 len
= (request
->num_headers
) * 4 + 10;
1405 req
= heap_alloc( len
* sizeof(LPCWSTR
) );
1406 if (!req
) return NULL
;
1408 /* add the verb, path and HTTP version string */
1416 /* Append custom request headers */
1417 for (i
= 0; i
< request
->num_headers
; i
++)
1419 if (request
->headers
[i
].is_request
)
1422 req
[n
++] = request
->headers
[i
].field
;
1424 req
[n
++] = request
->headers
[i
].value
;
1426 TRACE("Adding custom header %s (%s)\n",
1427 debugstr_w(request
->headers
[i
].field
),
1428 debugstr_w(request
->headers
[i
].value
));
1433 ERR("oops. buffer overrun\n");
1436 requestString
= concatenate_string_list( req
, 4 );
1438 if (!requestString
) return NULL
;
1441 * Set (header) termination string for request
1442 * Make sure there are exactly two new lines at the end of the request
1444 p
= &requestString
[strlenW(requestString
)-1];
1445 while ( (*p
== '\n') || (*p
== '\r') )
1447 strcpyW( p
+1, twocrlf
);
1449 return requestString
;
1452 static BOOL
read_reply( request_t
*request
);
1454 static BOOL
secure_proxy_connect( request_t
*request
)
1456 static const WCHAR verbConnect
[] = {'C','O','N','N','E','C','T',0};
1457 static const WCHAR fmt
[] = {'%','s',':','%','u',0};
1460 connect_t
*connect
= request
->connect
;
1462 path
= heap_alloc( (strlenW( connect
->hostname
) + 13) * sizeof(WCHAR
) );
1465 LPWSTR requestString
;
1467 sprintfW( path
, fmt
, connect
->hostname
, connect
->hostport
);
1468 requestString
= build_header_request_string( request
, verbConnect
,
1473 LPSTR req_ascii
= strdupWA( requestString
);
1475 heap_free( requestString
);
1478 int len
= strlen( req_ascii
), bytes_sent
;
1480 ret
= netconn_send( request
->netconn
, req_ascii
, len
, &bytes_sent
);
1481 heap_free( req_ascii
);
1483 ret
= read_reply( request
);
1490 #ifndef INET6_ADDRSTRLEN
1491 #define INET6_ADDRSTRLEN 46
1494 static WCHAR
*addr_to_str( struct sockaddr_storage
*addr
)
1496 char buf
[INET6_ADDRSTRLEN
];
1499 switch (addr
->ss_family
)
1502 src
= &((struct sockaddr_in
*)addr
)->sin_addr
;
1505 src
= &((struct sockaddr_in6
*)addr
)->sin6_addr
;
1508 WARN("unsupported address family %d\n", addr
->ss_family
);
1511 if (!inet_ntop( addr
->ss_family
, src
, buf
, sizeof(buf
) )) return NULL
;
1512 return strdupAW( buf
);
1515 static CRITICAL_SECTION connection_pool_cs
;
1516 static CRITICAL_SECTION_DEBUG connection_pool_debug
=
1518 0, 0, &connection_pool_cs
,
1519 { &connection_pool_debug
.ProcessLocksList
, &connection_pool_debug
.ProcessLocksList
},
1520 0, 0, { (DWORD_PTR
)(__FILE__
": connection_pool_cs") }
1522 static CRITICAL_SECTION connection_pool_cs
= { &connection_pool_debug
, -1, 0, 0, 0, 0 };
1524 static struct list connection_pool
= LIST_INIT( connection_pool
);
1526 void release_host( hostdata_t
*host
)
1530 EnterCriticalSection( &connection_pool_cs
);
1531 if (!(ref
= --host
->ref
)) list_remove( &host
->entry
);
1532 LeaveCriticalSection( &connection_pool_cs
);
1535 assert( list_empty( &host
->connections
) );
1536 heap_free( host
->hostname
);
1540 static BOOL connection_collector_running
;
1542 static DWORD WINAPI
connection_collector(void *arg
)
1544 unsigned int remaining_connections
;
1545 netconn_t
*netconn
, *next_netconn
;
1546 hostdata_t
*host
, *next_host
;
1551 /* FIXME: Use more sophisticated method */
1553 remaining_connections
= 0;
1554 now
= GetTickCount64();
1556 EnterCriticalSection(&connection_pool_cs
);
1558 LIST_FOR_EACH_ENTRY_SAFE(host
, next_host
, &connection_pool
, hostdata_t
, entry
)
1560 LIST_FOR_EACH_ENTRY_SAFE(netconn
, next_netconn
, &host
->connections
, netconn_t
, entry
)
1562 if (netconn
->keep_until
< now
)
1564 TRACE("freeing %p\n", netconn
);
1565 list_remove(&netconn
->entry
);
1566 netconn_close(netconn
);
1570 remaining_connections
++;
1575 if (!remaining_connections
) connection_collector_running
= FALSE
;
1577 LeaveCriticalSection(&connection_pool_cs
);
1578 } while(remaining_connections
);
1580 FreeLibraryAndExitThread( winhttp_instance
, 0 );
1583 static void cache_connection( netconn_t
*netconn
)
1585 TRACE( "caching connection %p\n", netconn
);
1587 EnterCriticalSection( &connection_pool_cs
);
1589 netconn
->keep_until
= GetTickCount64() + DEFAULT_KEEP_ALIVE_TIMEOUT
;
1590 list_add_head( &netconn
->host
->connections
, &netconn
->entry
);
1592 if (!connection_collector_running
)
1597 GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
, (const WCHAR
*)winhttp_instance
, &module
);
1599 thread
= CreateThread(NULL
, 0, connection_collector
, NULL
, 0, NULL
);
1602 CloseHandle( thread
);
1603 connection_collector_running
= TRUE
;
1607 FreeLibrary( winhttp_instance
);
1611 LeaveCriticalSection( &connection_pool_cs
);
1614 static DWORD
map_secure_protocols( DWORD mask
)
1617 if (mask
& WINHTTP_FLAG_SECURE_PROTOCOL_SSL2
) ret
|= SP_PROT_SSL2_CLIENT
;
1618 if (mask
& WINHTTP_FLAG_SECURE_PROTOCOL_SSL3
) ret
|= SP_PROT_SSL3_CLIENT
;
1619 if (mask
& WINHTTP_FLAG_SECURE_PROTOCOL_TLS1
) ret
|= SP_PROT_TLS1_CLIENT
;
1620 if (mask
& WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1
) ret
|= SP_PROT_TLS1_1_CLIENT
;
1621 if (mask
& WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2
) ret
|= SP_PROT_TLS1_2_CLIENT
;
1625 static BOOL
ensure_cred_handle( session_t
*session
)
1628 SECURITY_STATUS status
;
1630 if (session
->cred_handle_initialized
) return TRUE
;
1632 memset( &cred
, 0, sizeof(cred
) );
1633 cred
.dwVersion
= SCHANNEL_CRED_VERSION
;
1634 cred
.grbitEnabledProtocols
= map_secure_protocols( session
->secure_protocols
);
1635 if ((status
= AcquireCredentialsHandleW( NULL
, (WCHAR
*)UNISP_NAME_W
, SECPKG_CRED_OUTBOUND
, NULL
, &cred
,
1636 NULL
, NULL
, &session
->cred_handle
, NULL
)) != SEC_E_OK
)
1638 WARN( "AcquireCredentialsHandleW failed: 0x%08x\n", status
);
1641 session
->cred_handle_initialized
= TRUE
;
1645 static BOOL
open_connection( request_t
*request
)
1647 BOOL is_secure
= request
->hdr
.flags
& WINHTTP_FLAG_SECURE
;
1648 hostdata_t
*host
= NULL
, *iter
;
1649 netconn_t
*netconn
= NULL
;
1651 WCHAR
*addressW
= NULL
;
1655 if (request
->netconn
) goto done
;
1657 connect
= request
->connect
;
1658 port
= connect
->serverport
? connect
->serverport
: (request
->hdr
.flags
& WINHTTP_FLAG_SECURE
? 443 : 80);
1660 EnterCriticalSection( &connection_pool_cs
);
1662 LIST_FOR_EACH_ENTRY( iter
, &connection_pool
, hostdata_t
, entry
)
1664 if (iter
->port
== port
&& !strcmpW( connect
->servername
, iter
->hostname
) && !is_secure
== !iter
->secure
)
1674 if ((host
= heap_alloc( sizeof(*host
) )))
1677 host
->secure
= is_secure
;
1679 list_init( &host
->connections
);
1680 if ((host
->hostname
= strdupW( connect
->servername
)))
1682 list_add_head( &connection_pool
, &host
->entry
);
1692 LeaveCriticalSection( &connection_pool_cs
);
1694 if (!host
) return FALSE
;
1698 EnterCriticalSection( &connection_pool_cs
);
1699 if (!list_empty( &host
->connections
))
1701 netconn
= LIST_ENTRY( list_head( &host
->connections
), netconn_t
, entry
);
1702 list_remove( &netconn
->entry
);
1704 LeaveCriticalSection( &connection_pool_cs
);
1705 if (!netconn
) break;
1707 if (netconn_is_alive( netconn
)) break;
1708 TRACE("connection %p no longer alive, closing\n", netconn
);
1709 netconn_close( netconn
);
1713 if (!connect
->resolved
&& netconn
)
1715 connect
->sockaddr
= netconn
->sockaddr
;
1716 connect
->resolved
= TRUE
;
1719 if (!connect
->resolved
)
1721 len
= strlenW( host
->hostname
) + 1;
1722 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_RESOLVING_NAME
, host
->hostname
, len
);
1724 if (!netconn_resolve( host
->hostname
, port
, &connect
->sockaddr
, request
->resolve_timeout
))
1726 release_host( host
);
1729 connect
->resolved
= TRUE
;
1731 if (!(addressW
= addr_to_str( &connect
->sockaddr
)))
1733 release_host( host
);
1736 len
= strlenW( addressW
) + 1;
1737 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_NAME_RESOLVED
, addressW
, len
);
1742 if (!addressW
&& !(addressW
= addr_to_str( &connect
->sockaddr
)))
1744 release_host( host
);
1748 TRACE("connecting to %s:%u\n", debugstr_w(addressW
), port
);
1750 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER
, addressW
, 0 );
1752 if (!(netconn
= netconn_create( host
, &connect
->sockaddr
, request
->connect_timeout
)))
1754 heap_free( addressW
);
1755 release_host( host
);
1758 netconn_set_timeout( netconn
, TRUE
, request
->send_timeout
);
1759 netconn_set_timeout( netconn
, FALSE
, request
->recv_timeout
);
1762 if (connect
->session
->proxy_server
&&
1763 strcmpiW( connect
->hostname
, connect
->servername
))
1765 if (!secure_proxy_connect( request
))
1767 heap_free( addressW
);
1768 netconn_close( netconn
);
1772 if (!ensure_cred_handle( connect
->session
) ||
1773 !netconn_secure_connect( netconn
, connect
->hostname
, request
->security_flags
,
1774 &connect
->session
->cred_handle
))
1776 heap_free( addressW
);
1777 netconn_close( netconn
);
1782 request
->netconn
= netconn
;
1783 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER
, addressW
, strlenW(addressW
) + 1 );
1787 TRACE("using connection %p\n", netconn
);
1789 netconn_set_timeout( netconn
, TRUE
, request
->send_timeout
);
1790 netconn_set_timeout( netconn
, FALSE
, request
->recv_timeout
);
1791 request
->netconn
= netconn
;
1795 request
->read_pos
= request
->read_size
= 0;
1796 request
->read_chunked
= FALSE
;
1797 request
->read_chunked_size
= ~0u;
1798 request
->read_chunked_eof
= FALSE
;
1799 heap_free( addressW
);
1803 void close_connection( request_t
*request
)
1805 if (!request
->netconn
) return;
1807 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION
, 0, 0 );
1808 netconn_close( request
->netconn
);
1809 request
->netconn
= NULL
;
1810 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED
, 0, 0 );
1813 static BOOL
add_host_header( request_t
*request
, DWORD modifier
)
1818 static const WCHAR fmt
[] = {'%','s',':','%','u',0};
1819 connect_t
*connect
= request
->connect
;
1822 port
= connect
->hostport
? connect
->hostport
: (request
->hdr
.flags
& WINHTTP_FLAG_SECURE
? 443 : 80);
1824 if (port
== INTERNET_DEFAULT_HTTP_PORT
|| port
== INTERNET_DEFAULT_HTTPS_PORT
)
1826 return process_header( request
, attr_host
, connect
->hostname
, modifier
, TRUE
);
1828 len
= strlenW( connect
->hostname
) + 7; /* sizeof(":65335") */
1829 if (!(host
= heap_alloc( len
* sizeof(WCHAR
) ))) return FALSE
;
1830 sprintfW( host
, fmt
, connect
->hostname
, port
);
1831 ret
= process_header( request
, attr_host
, host
, modifier
, TRUE
);
1836 static void clear_response_headers( request_t
*request
)
1840 for (i
= 0; i
< request
->num_headers
; i
++)
1842 if (!request
->headers
[i
].field
) continue;
1843 if (!request
->headers
[i
].value
) continue;
1844 if (request
->headers
[i
].is_request
) continue;
1845 delete_header( request
, i
);
1850 /* remove some amount of data from the read buffer */
1851 static void remove_data( request_t
*request
, int count
)
1853 if (!(request
->read_size
-= count
)) request
->read_pos
= 0;
1854 else request
->read_pos
+= count
;
1857 /* read some more data into the read buffer */
1858 static BOOL
read_more_data( request_t
*request
, int maxlen
, BOOL notify
)
1863 if (request
->read_chunked_eof
) return FALSE
;
1865 if (request
->read_size
&& request
->read_pos
)
1867 /* move existing data to the start of the buffer */
1868 memmove( request
->read_buf
, request
->read_buf
+ request
->read_pos
, request
->read_size
);
1869 request
->read_pos
= 0;
1871 if (maxlen
== -1) maxlen
= sizeof(request
->read_buf
);
1873 if (notify
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE
, NULL
, 0 );
1875 ret
= netconn_recv( request
->netconn
, request
->read_buf
+ request
->read_size
,
1876 maxlen
- request
->read_size
, 0, &len
);
1878 if (notify
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED
, &len
, sizeof(len
) );
1880 request
->read_size
+= len
;
1884 /* discard data contents until we reach end of line */
1885 static BOOL
discard_eol( request_t
*request
, BOOL notify
)
1889 char *eol
= memchr( request
->read_buf
+ request
->read_pos
, '\n', request
->read_size
);
1892 remove_data( request
, (eol
+ 1) - (request
->read_buf
+ request
->read_pos
) );
1895 request
->read_pos
= request
->read_size
= 0; /* discard everything */
1896 if (!read_more_data( request
, -1, notify
)) return FALSE
;
1897 } while (request
->read_size
);
1901 /* read the size of the next chunk */
1902 static BOOL
start_next_chunk( request_t
*request
, BOOL notify
)
1904 DWORD chunk_size
= 0;
1906 assert(!request
->read_chunked_size
|| request
->read_chunked_size
== ~0u);
1908 if (request
->read_chunked_eof
) return FALSE
;
1910 /* read terminator for the previous chunk */
1911 if (!request
->read_chunked_size
&& !discard_eol( request
, notify
)) return FALSE
;
1915 while (request
->read_size
)
1917 char ch
= request
->read_buf
[request
->read_pos
];
1918 if (ch
>= '0' && ch
<= '9') chunk_size
= chunk_size
* 16 + ch
- '0';
1919 else if (ch
>= 'a' && ch
<= 'f') chunk_size
= chunk_size
* 16 + ch
- 'a' + 10;
1920 else if (ch
>= 'A' && ch
<= 'F') chunk_size
= chunk_size
* 16 + ch
- 'A' + 10;
1921 else if (ch
== ';' || ch
== '\r' || ch
== '\n')
1923 TRACE("reading %u byte chunk\n", chunk_size
);
1925 if (request
->content_length
== ~0u) request
->content_length
= chunk_size
;
1926 else request
->content_length
+= chunk_size
;
1928 request
->read_chunked_size
= chunk_size
;
1929 if (!chunk_size
) request
->read_chunked_eof
= TRUE
;
1931 return discard_eol( request
, notify
);
1933 remove_data( request
, 1 );
1935 if (!read_more_data( request
, -1, notify
)) return FALSE
;
1936 if (!request
->read_size
)
1938 request
->content_length
= request
->content_read
= 0;
1939 request
->read_chunked_size
= 0;
1945 static BOOL
refill_buffer( request_t
*request
, BOOL notify
)
1947 int len
= sizeof(request
->read_buf
);
1949 if (request
->read_chunked
)
1951 if (request
->read_chunked_eof
) return FALSE
;
1952 if (request
->read_chunked_size
== ~0u || !request
->read_chunked_size
)
1954 if (!start_next_chunk( request
, notify
)) return FALSE
;
1956 len
= min( len
, request
->read_chunked_size
);
1958 else if (request
->content_length
!= ~0u)
1960 len
= min( len
, request
->content_length
- request
->content_read
);
1963 if (len
<= request
->read_size
) return TRUE
;
1964 if (!read_more_data( request
, len
, notify
)) return FALSE
;
1965 if (!request
->read_size
) request
->content_length
= request
->content_read
= 0;
1969 static void finished_reading( request_t
*request
)
1971 static const WCHAR closeW
[] = {'c','l','o','s','e',0};
1974 WCHAR connection
[20];
1975 DWORD size
= sizeof(connection
);
1977 if (!request
->netconn
) return;
1979 if (request
->hdr
.disable_flags
& WINHTTP_DISABLE_KEEP_ALIVE
) close
= TRUE
;
1980 else if (query_headers( request
, WINHTTP_QUERY_CONNECTION
, NULL
, connection
, &size
, NULL
) ||
1981 query_headers( request
, WINHTTP_QUERY_PROXY_CONNECTION
, NULL
, connection
, &size
, NULL
))
1983 if (!strcmpiW( connection
, closeW
)) close
= TRUE
;
1985 else if (!strcmpW( request
->version
, http1_0
)) close
= TRUE
;
1988 close_connection( request
);
1992 cache_connection( request
->netconn
);
1993 request
->netconn
= NULL
;
1996 /* return the size of data available to be read immediately */
1997 static DWORD
get_available_data( request_t
*request
)
1999 if (request
->read_chunked
) return min( request
->read_chunked_size
, request
->read_size
);
2000 return request
->read_size
;
2003 /* check if we have reached the end of the data to read */
2004 static BOOL
end_of_read_data( request_t
*request
)
2006 if (!request
->content_length
) return TRUE
;
2007 if (request
->read_chunked
) return request
->read_chunked_eof
;
2008 if (request
->content_length
== ~0u) return FALSE
;
2009 return (request
->content_length
== request
->content_read
);
2012 static BOOL
read_data( request_t
*request
, void *buffer
, DWORD size
, DWORD
*read
, BOOL async
)
2014 int count
, bytes_read
= 0;
2016 if (end_of_read_data( request
)) goto done
;
2020 if (!(count
= get_available_data( request
)))
2022 if (!refill_buffer( request
, async
)) goto done
;
2023 if (!(count
= get_available_data( request
))) goto done
;
2025 count
= min( count
, size
);
2026 memcpy( (char *)buffer
+ bytes_read
, request
->read_buf
+ request
->read_pos
, count
);
2027 remove_data( request
, count
);
2028 if (request
->read_chunked
) request
->read_chunked_size
-= count
;
2030 bytes_read
+= count
;
2031 request
->content_read
+= count
;
2032 if (end_of_read_data( request
)) goto done
;
2034 if (request
->read_chunked
&& !request
->read_chunked_size
) refill_buffer( request
, async
);
2037 TRACE( "retrieved %u bytes (%u/%u)\n", bytes_read
, request
->content_read
, request
->content_length
);
2039 if (async
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_READ_COMPLETE
, buffer
, bytes_read
);
2040 if (read
) *read
= bytes_read
;
2041 if (end_of_read_data( request
)) finished_reading( request
);
2045 /* read any content returned by the server so that the connection can be reused */
2046 static void drain_content( request_t
*request
)
2048 DWORD size
, bytes_read
, bytes_total
= 0, bytes_left
= request
->content_length
- request
->content_read
;
2051 refill_buffer( request
, FALSE
);
2054 if (request
->read_chunked
) size
= sizeof(buffer
);
2057 if (bytes_total
>= bytes_left
) return;
2058 size
= min( sizeof(buffer
), bytes_left
- bytes_total
);
2060 if (!read_data( request
, buffer
, size
, &bytes_read
, FALSE
) || !bytes_read
) return;
2061 bytes_total
+= bytes_read
;
2065 static BOOL
send_request( request_t
*request
, LPCWSTR headers
, DWORD headers_len
, LPVOID optional
,
2066 DWORD optional_len
, DWORD total_len
, DWORD_PTR context
, BOOL async
)
2068 static const WCHAR keep_alive
[] = {'K','e','e','p','-','A','l','i','v','e',0};
2069 static const WCHAR no_cache
[] = {'n','o','-','c','a','c','h','e',0};
2070 static const WCHAR length_fmt
[] = {'%','l','d',0};
2073 connect_t
*connect
= request
->connect
;
2074 session_t
*session
= connect
->session
;
2080 clear_response_headers( request
);
2081 drain_content( request
);
2084 process_header( request
, attr_user_agent
, session
->agent
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
2086 if (connect
->hostname
)
2087 add_host_header( request
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
);
2089 if (request
->creds
[TARGET_SERVER
][SCHEME_BASIC
].username
)
2090 do_authorization( request
, WINHTTP_AUTH_TARGET_SERVER
, WINHTTP_AUTH_SCHEME_BASIC
);
2092 if (total_len
|| (request
->verb
&& !strcmpW( request
->verb
, postW
)))
2094 WCHAR length
[21]; /* decimal long int + null */
2095 sprintfW( length
, length_fmt
, total_len
);
2096 process_header( request
, attr_content_length
, length
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
2098 if (!(request
->hdr
.disable_flags
& WINHTTP_DISABLE_KEEP_ALIVE
))
2100 process_header( request
, attr_connection
, keep_alive
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
2102 if (request
->hdr
.flags
& WINHTTP_FLAG_REFRESH
)
2104 process_header( request
, attr_pragma
, no_cache
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
2105 process_header( request
, attr_cache_control
, no_cache
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
2107 if (headers
&& !add_request_headers( request
, headers
, headers_len
, WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
))
2109 TRACE("failed to add request headers\n");
2112 if (!(request
->hdr
.disable_flags
& WINHTTP_DISABLE_COOKIES
) && !add_cookie_headers( request
))
2114 WARN("failed to add cookie headers\n");
2118 if (context
) request
->hdr
.context
= context
;
2120 if (!(ret
= open_connection( request
))) goto end
;
2121 if (!(req
= build_request_string( request
))) goto end
;
2123 if (!(req_ascii
= strdupWA( req
))) goto end
;
2124 TRACE("full request: %s\n", debugstr_a(req_ascii
));
2125 len
= strlen(req_ascii
);
2127 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_SENDING_REQUEST
, NULL
, 0 );
2129 ret
= netconn_send( request
->netconn
, req_ascii
, len
, &bytes_sent
);
2130 heap_free( req_ascii
);
2135 if (!netconn_send( request
->netconn
, optional
, optional_len
, &bytes_sent
)) goto end
;
2136 request
->optional
= optional
;
2137 request
->optional_len
= optional_len
;
2138 len
+= optional_len
;
2140 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_REQUEST_SENT
, &len
, sizeof(len
) );
2145 if (ret
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE
, NULL
, 0 );
2148 WINHTTP_ASYNC_RESULT result
;
2149 result
.dwResult
= API_SEND_REQUEST
;
2150 result
.dwError
= get_last_error();
2151 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_REQUEST_ERROR
, &result
, sizeof(result
) );
2158 static void task_send_request( task_header_t
*task
)
2160 send_request_t
*s
= (send_request_t
*)task
;
2161 send_request( s
->hdr
.request
, s
->headers
, s
->headers_len
, s
->optional
, s
->optional_len
, s
->total_len
, s
->context
, TRUE
);
2162 heap_free( s
->headers
);
2165 /***********************************************************************
2166 * WinHttpSendRequest (winhttp.@)
2168 BOOL WINAPI
WinHttpSendRequest( HINTERNET hrequest
, LPCWSTR headers
, DWORD headers_len
,
2169 LPVOID optional
, DWORD optional_len
, DWORD total_len
, DWORD_PTR context
)
2174 TRACE("%p, %s, %u, %u, %u, %lx\n", hrequest
, debugstr_wn(headers
, headers_len
), headers_len
, optional_len
,
2175 total_len
, context
);
2177 if (!(request
= (request_t
*)grab_object( hrequest
)))
2179 set_last_error( ERROR_INVALID_HANDLE
);
2182 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
2184 release_object( &request
->hdr
);
2185 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2189 if (headers
&& !headers_len
) headers_len
= strlenW( headers
);
2191 if (request
->connect
->hdr
.flags
& WINHTTP_FLAG_ASYNC
)
2195 if (!(s
= heap_alloc( sizeof(send_request_t
) ))) return FALSE
;
2196 s
->hdr
.request
= request
;
2197 s
->hdr
.proc
= task_send_request
;
2198 s
->headers
= strdupW( headers
);
2199 s
->headers_len
= headers_len
;
2200 s
->optional
= optional
;
2201 s
->optional_len
= optional_len
;
2202 s
->total_len
= total_len
;
2203 s
->context
= context
;
2205 addref_object( &request
->hdr
);
2206 ret
= queue_task( (task_header_t
*)s
);
2209 ret
= send_request( request
, headers
, headers_len
, optional
, optional_len
, total_len
, context
, FALSE
);
2211 release_object( &request
->hdr
);
2212 if (ret
) set_last_error( ERROR_SUCCESS
);
2216 static BOOL
set_credentials( request_t
*request
, DWORD target
, DWORD scheme_flag
, const WCHAR
*username
,
2217 const WCHAR
*password
)
2219 enum auth_scheme scheme
= scheme_from_flag( scheme_flag
);
2221 if (scheme
== SCHEME_INVALID
|| ((scheme
== SCHEME_BASIC
|| scheme
== SCHEME_DIGEST
) && (!username
|| !password
)))
2223 set_last_error( ERROR_INVALID_PARAMETER
);
2228 case WINHTTP_AUTH_TARGET_SERVER
:
2230 heap_free( request
->creds
[TARGET_SERVER
][scheme
].username
);
2231 if (!username
) request
->creds
[TARGET_SERVER
][scheme
].username
= NULL
;
2232 else if (!(request
->creds
[TARGET_SERVER
][scheme
].username
= strdupW( username
))) return FALSE
;
2234 heap_free( request
->creds
[TARGET_SERVER
][scheme
].password
);
2235 if (!password
) request
->creds
[TARGET_SERVER
][scheme
].password
= NULL
;
2236 else if (!(request
->creds
[TARGET_SERVER
][scheme
].password
= strdupW( password
))) return FALSE
;
2239 case WINHTTP_AUTH_TARGET_PROXY
:
2241 heap_free( request
->creds
[TARGET_PROXY
][scheme
].username
);
2242 if (!username
) request
->creds
[TARGET_PROXY
][scheme
].username
= NULL
;
2243 else if (!(request
->creds
[TARGET_PROXY
][scheme
].username
= strdupW( username
))) return FALSE
;
2245 heap_free( request
->creds
[TARGET_PROXY
][scheme
].password
);
2246 if (!password
) request
->creds
[TARGET_PROXY
][scheme
].password
= NULL
;
2247 else if (!(request
->creds
[TARGET_PROXY
][scheme
].password
= strdupW( password
))) return FALSE
;
2251 WARN("unknown target %u\n", target
);
2257 /***********************************************************************
2258 * WinHttpSetCredentials (winhttp.@)
2260 BOOL WINAPI
WinHttpSetCredentials( HINTERNET hrequest
, DWORD target
, DWORD scheme
, LPCWSTR username
,
2261 LPCWSTR password
, LPVOID params
)
2266 TRACE("%p, %x, 0x%08x, %s, %p, %p\n", hrequest
, target
, scheme
, debugstr_w(username
), password
, params
);
2268 if (!(request
= (request_t
*)grab_object( hrequest
)))
2270 set_last_error( ERROR_INVALID_HANDLE
);
2273 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
2275 release_object( &request
->hdr
);
2276 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2280 ret
= set_credentials( request
, target
, scheme
, username
, password
);
2282 release_object( &request
->hdr
);
2283 if (ret
) set_last_error( ERROR_SUCCESS
);
2287 static BOOL
handle_authorization( request_t
*request
, DWORD status
)
2289 DWORD i
, schemes
, first
, level
, target
;
2293 case HTTP_STATUS_DENIED
:
2294 target
= WINHTTP_AUTH_TARGET_SERVER
;
2295 level
= WINHTTP_QUERY_WWW_AUTHENTICATE
;
2298 case HTTP_STATUS_PROXY_AUTH_REQ
:
2299 target
= WINHTTP_AUTH_TARGET_PROXY
;
2300 level
= WINHTTP_QUERY_PROXY_AUTHENTICATE
;
2304 WARN("unhandled status %u\n", status
);
2308 if (!query_auth_schemes( request
, level
, &schemes
, &first
)) return FALSE
;
2309 if (do_authorization( request
, target
, first
)) return TRUE
;
2312 for (i
= 0; i
< num_auth_schemes
; i
++)
2314 if (!(schemes
& auth_schemes
[i
].scheme
)) continue;
2315 if (do_authorization( request
, target
, auth_schemes
[i
].scheme
)) return TRUE
;
2320 /* set the request content length based on the headers */
2321 static DWORD
set_content_length( request_t
*request
, DWORD status
)
2324 DWORD buflen
= sizeof(request
->content_length
);
2326 if (status
== HTTP_STATUS_NO_CONTENT
|| status
== HTTP_STATUS_NOT_MODIFIED
|| !strcmpW( request
->verb
, headW
))
2327 request
->content_length
= 0;
2330 if (!query_headers( request
, WINHTTP_QUERY_CONTENT_LENGTH
|WINHTTP_QUERY_FLAG_NUMBER
,
2331 NULL
, &request
->content_length
, &buflen
, NULL
))
2332 request
->content_length
= ~0u;
2334 buflen
= sizeof(encoding
);
2335 if (query_headers( request
, WINHTTP_QUERY_TRANSFER_ENCODING
, NULL
, encoding
, &buflen
, NULL
) &&
2336 !strcmpiW( encoding
, chunkedW
))
2338 request
->content_length
= ~0u;
2339 request
->read_chunked
= TRUE
;
2340 request
->read_chunked_size
= ~0u;
2341 request
->read_chunked_eof
= FALSE
;
2344 request
->content_read
= 0;
2345 return request
->content_length
;
2348 static BOOL
read_line( request_t
*request
, char *buffer
, DWORD
*len
)
2350 int count
, bytes_read
, pos
= 0;
2354 char *eol
= memchr( request
->read_buf
+ request
->read_pos
, '\n', request
->read_size
);
2357 count
= eol
- (request
->read_buf
+ request
->read_pos
);
2358 bytes_read
= count
+ 1;
2360 else count
= bytes_read
= request
->read_size
;
2362 count
= min( count
, *len
- pos
);
2363 memcpy( buffer
+ pos
, request
->read_buf
+ request
->read_pos
, count
);
2365 remove_data( request
, bytes_read
);
2368 if (!read_more_data( request
, -1, TRUE
)) return FALSE
;
2369 if (!request
->read_size
)
2372 TRACE("returning empty string\n");
2378 if (pos
&& buffer
[pos
- 1] == '\r') pos
--;
2381 buffer
[*len
- 1] = 0;
2382 TRACE("returning %s\n", debugstr_a(buffer
));
2386 #define MAX_REPLY_LEN 1460
2387 #define INITIAL_HEADER_BUFFER_LEN 512
2389 static BOOL
read_reply( request_t
*request
)
2391 static const WCHAR crlf
[] = {'\r','\n',0};
2393 char buffer
[MAX_REPLY_LEN
];
2394 DWORD buflen
, len
, offset
, crlf_len
= 2; /* strlenW(crlf) */
2395 char *status_code
, *status_text
;
2396 WCHAR
*versionW
, *status_textW
, *raw_headers
;
2397 WCHAR status_codeW
[4]; /* sizeof("nnn") */
2399 if (!request
->netconn
) return FALSE
;
2403 buflen
= MAX_REPLY_LEN
;
2404 if (!read_line( request
, buffer
, &buflen
)) return FALSE
;
2406 /* first line should look like 'HTTP/1.x nnn OK' where nnn is the status code */
2407 if (!(status_code
= strchr( buffer
, ' ' ))) return FALSE
;
2409 if (!(status_text
= strchr( status_code
, ' ' ))) return FALSE
;
2410 if ((len
= status_text
- status_code
) != sizeof("nnn") - 1) return FALSE
;
2413 TRACE("version [%s] status code [%s] status text [%s]\n",
2414 debugstr_an(buffer
, status_code
- buffer
- 1),
2415 debugstr_an(status_code
, len
),
2416 debugstr_a(status_text
));
2418 } while (!memcmp( status_code
, "100", len
)); /* ignore "100 Continue" responses */
2420 /* we rely on the fact that the protocol is ascii */
2421 MultiByteToWideChar( CP_ACP
, 0, status_code
, len
, status_codeW
, len
);
2422 status_codeW
[len
] = 0;
2423 if (!(process_header( request
, attr_status
, status_codeW
,
2424 WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
, FALSE
)))
2427 len
= status_code
- buffer
;
2428 if (!(versionW
= heap_alloc( len
* sizeof(WCHAR
) ))) return FALSE
;
2429 MultiByteToWideChar( CP_ACP
, 0, buffer
, len
- 1, versionW
, len
-1 );
2430 versionW
[len
- 1] = 0;
2432 heap_free( request
->version
);
2433 request
->version
= versionW
;
2435 len
= buflen
- (status_text
- buffer
);
2436 if (!(status_textW
= heap_alloc( len
* sizeof(WCHAR
) ))) return FALSE
;
2437 MultiByteToWideChar( CP_ACP
, 0, status_text
, len
, status_textW
, len
);
2439 heap_free( request
->status_text
);
2440 request
->status_text
= status_textW
;
2442 len
= max( buflen
+ crlf_len
, INITIAL_HEADER_BUFFER_LEN
);
2443 if (!(raw_headers
= heap_alloc( len
* sizeof(WCHAR
) ))) return FALSE
;
2444 MultiByteToWideChar( CP_ACP
, 0, buffer
, buflen
, raw_headers
, buflen
);
2445 memcpy( raw_headers
+ buflen
- 1, crlf
, sizeof(crlf
) );
2447 heap_free( request
->raw_headers
);
2448 request
->raw_headers
= raw_headers
;
2450 offset
= buflen
+ crlf_len
- 1;
2455 buflen
= MAX_REPLY_LEN
;
2456 if (!read_line( request
, buffer
, &buflen
)) return TRUE
;
2457 if (!*buffer
) buflen
= 1;
2459 while (len
- offset
< buflen
+ crlf_len
)
2463 if (!(tmp
= heap_realloc( raw_headers
, len
* sizeof(WCHAR
) ))) return FALSE
;
2464 request
->raw_headers
= raw_headers
= tmp
;
2468 memcpy( raw_headers
+ offset
, crlf
, sizeof(crlf
) );
2471 MultiByteToWideChar( CP_ACP
, 0, buffer
, buflen
, raw_headers
+ offset
, buflen
);
2473 if (!(header
= parse_header( raw_headers
+ offset
))) break;
2474 if (!(process_header( request
, header
->field
, header
->value
, WINHTTP_ADDREQ_FLAG_ADD
, FALSE
)))
2476 free_header( header
);
2479 free_header( header
);
2480 memcpy( raw_headers
+ offset
+ buflen
- 1, crlf
, sizeof(crlf
) );
2481 offset
+= buflen
+ crlf_len
- 1;
2484 TRACE("raw headers: %s\n", debugstr_w(raw_headers
));
2488 static void record_cookies( request_t
*request
)
2492 for (i
= 0; i
< request
->num_headers
; i
++)
2494 header_t
*set_cookie
= &request
->headers
[i
];
2495 if (!strcmpiW( set_cookie
->field
, attr_set_cookie
) && !set_cookie
->is_request
)
2497 set_cookies( request
, set_cookie
->value
);
2502 static WCHAR
*get_redirect_url( request_t
*request
, DWORD
*len
)
2507 query_headers( request
, WINHTTP_QUERY_LOCATION
, NULL
, NULL
, &size
, NULL
);
2508 if (get_last_error() != ERROR_INSUFFICIENT_BUFFER
) return FALSE
;
2509 if (!(ret
= heap_alloc( size
))) return NULL
;
2510 *len
= size
/ sizeof(WCHAR
);
2511 if (query_headers( request
, WINHTTP_QUERY_LOCATION
, NULL
, ret
, &size
, NULL
)) return ret
;
2516 static BOOL
handle_redirect( request_t
*request
, DWORD status
)
2521 connect_t
*connect
= request
->connect
;
2523 WCHAR
*hostname
= NULL
, *location
;
2526 if (!(location
= get_redirect_url( request
, &len_url
))) return FALSE
;
2528 memset( &uc
, 0, sizeof(uc
) );
2529 uc
.dwStructSize
= sizeof(uc
);
2530 uc
.dwSchemeLength
= uc
.dwHostNameLength
= uc
.dwUrlPathLength
= uc
.dwExtraInfoLength
= ~0u;
2532 if (!WinHttpCrackUrl( location
, len_url
, 0, &uc
)) /* assume relative redirect */
2536 if (location
[0] == '/')
2538 len
= strlenW( location
);
2539 if (!(path
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto end
;
2540 strcpyW( path
, location
);
2544 if ((p
= strrchrW( request
->path
, '/' ))) *p
= 0;
2545 len
= strlenW( request
->path
) + 1 + strlenW( location
);
2546 if (!(path
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto end
;
2547 strcpyW( path
, request
->path
);
2548 strcatW( path
, slashW
);
2549 strcatW( path
, location
);
2551 heap_free( request
->path
);
2552 request
->path
= path
;
2554 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_REDIRECT
, location
, len_url
+ 1 );
2558 if (uc
.nScheme
== INTERNET_SCHEME_HTTP
&& request
->hdr
.flags
& WINHTTP_FLAG_SECURE
)
2560 if (request
->hdr
.redirect_policy
== WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP
) goto end
;
2561 TRACE("redirect from secure page to non-secure page\n");
2562 request
->hdr
.flags
&= ~WINHTTP_FLAG_SECURE
;
2564 else if (uc
.nScheme
== INTERNET_SCHEME_HTTPS
&& !(request
->hdr
.flags
& WINHTTP_FLAG_SECURE
))
2566 TRACE("redirect from non-secure page to secure page\n");
2567 request
->hdr
.flags
|= WINHTTP_FLAG_SECURE
;
2570 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_REDIRECT
, location
, len_url
+ 1 );
2572 len
= uc
.dwHostNameLength
;
2573 if (!(hostname
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto end
;
2574 memcpy( hostname
, uc
.lpszHostName
, len
* sizeof(WCHAR
) );
2577 port
= uc
.nPort
? uc
.nPort
: (uc
.nScheme
== INTERNET_SCHEME_HTTPS
? 443 : 80);
2578 if (strcmpiW( connect
->hostname
, hostname
) || connect
->serverport
!= port
)
2580 heap_free( connect
->hostname
);
2581 connect
->hostname
= hostname
;
2582 connect
->hostport
= port
;
2583 if (!(ret
= set_server_for_hostname( connect
, hostname
, port
))) goto end
;
2585 netconn_close( request
->netconn
);
2586 request
->netconn
= NULL
;
2587 request
->content_length
= request
->content_read
= 0;
2588 request
->read_pos
= request
->read_size
= 0;
2589 request
->read_chunked
= request
->read_chunked_eof
= FALSE
;
2591 else heap_free( hostname
);
2593 if (!(ret
= add_host_header( request
, WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
))) goto end
;
2594 if (!(ret
= open_connection( request
))) goto end
;
2596 heap_free( request
->path
);
2597 request
->path
= NULL
;
2598 if (uc
.dwUrlPathLength
)
2600 len
= uc
.dwUrlPathLength
+ uc
.dwExtraInfoLength
;
2601 if (!(request
->path
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) goto end
;
2602 strcpyW( request
->path
, uc
.lpszUrlPath
);
2604 else request
->path
= strdupW( slashW
);
2607 /* remove content-type/length headers */
2608 if ((index
= get_header_index( request
, attr_content_type
, 0, TRUE
)) >= 0) delete_header( request
, index
);
2609 if ((index
= get_header_index( request
, attr_content_length
, 0, TRUE
)) >= 0 ) delete_header( request
, index
);
2611 if (status
!= HTTP_STATUS_REDIRECT_KEEP_VERB
&& !strcmpW( request
->verb
, postW
))
2613 heap_free( request
->verb
);
2614 request
->verb
= strdupW( getW
);
2615 request
->optional
= NULL
;
2616 request
->optional_len
= 0;
2621 heap_free( location
);
2625 static BOOL
receive_response( request_t
*request
, BOOL async
)
2628 DWORD size
, query
, status
;
2632 if (!(ret
= read_reply( request
)))
2634 set_last_error( ERROR_WINHTTP_INVALID_SERVER_RESPONSE
);
2637 size
= sizeof(DWORD
);
2638 query
= WINHTTP_QUERY_STATUS_CODE
| WINHTTP_QUERY_FLAG_NUMBER
;
2639 if (!(ret
= query_headers( request
, query
, NULL
, &status
, &size
, NULL
))) break;
2641 set_content_length( request
, status
);
2643 if (!(request
->hdr
.disable_flags
& WINHTTP_DISABLE_COOKIES
)) record_cookies( request
);
2645 if (status
== HTTP_STATUS_MOVED
|| status
== HTTP_STATUS_REDIRECT
|| status
== HTTP_STATUS_REDIRECT_KEEP_VERB
)
2647 if (request
->hdr
.disable_flags
& WINHTTP_DISABLE_REDIRECTS
||
2648 request
->hdr
.redirect_policy
== WINHTTP_OPTION_REDIRECT_POLICY_NEVER
) break;
2650 if (!(ret
= handle_redirect( request
, status
))) break;
2652 /* recurse synchronously */
2653 if ((ret
= send_request( request
, NULL
, 0, request
->optional
, request
->optional_len
, 0, 0, FALSE
))) continue;
2655 else if (status
== HTTP_STATUS_DENIED
|| status
== HTTP_STATUS_PROXY_AUTH_REQ
)
2657 if (request
->hdr
.disable_flags
& WINHTTP_DISABLE_AUTHENTICATION
) break;
2659 if (!handle_authorization( request
, status
)) break;
2661 /* recurse synchronously */
2662 if ((ret
= send_request( request
, NULL
, 0, request
->optional
, request
->optional_len
, 0, 0, FALSE
))) continue;
2667 if (request
->content_length
) refill_buffer( request
, FALSE
);
2671 if (ret
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE
, NULL
, 0 );
2674 WINHTTP_ASYNC_RESULT result
;
2675 result
.dwResult
= API_RECEIVE_RESPONSE
;
2676 result
.dwError
= get_last_error();
2677 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_REQUEST_ERROR
, &result
, sizeof(result
) );
2683 static void task_receive_response( task_header_t
*task
)
2685 receive_response_t
*r
= (receive_response_t
*)task
;
2686 receive_response( r
->hdr
.request
, TRUE
);
2689 /***********************************************************************
2690 * WinHttpReceiveResponse (winhttp.@)
2692 BOOL WINAPI
WinHttpReceiveResponse( HINTERNET hrequest
, LPVOID reserved
)
2697 TRACE("%p, %p\n", hrequest
, reserved
);
2699 if (!(request
= (request_t
*)grab_object( hrequest
)))
2701 set_last_error( ERROR_INVALID_HANDLE
);
2704 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
2706 release_object( &request
->hdr
);
2707 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2711 if (request
->connect
->hdr
.flags
& WINHTTP_FLAG_ASYNC
)
2713 receive_response_t
*r
;
2715 if (!(r
= heap_alloc( sizeof(receive_response_t
) ))) return FALSE
;
2716 r
->hdr
.request
= request
;
2717 r
->hdr
.proc
= task_receive_response
;
2719 addref_object( &request
->hdr
);
2720 ret
= queue_task( (task_header_t
*)r
);
2723 ret
= receive_response( request
, FALSE
);
2725 release_object( &request
->hdr
);
2726 if (ret
) set_last_error( ERROR_SUCCESS
);
2730 static BOOL
query_data_available( request_t
*request
, DWORD
*available
, BOOL async
)
2734 if (end_of_read_data( request
)) goto done
;
2736 count
= get_available_data( request
);
2737 if (!request
->read_chunked
&& request
->netconn
)
2738 count
+= netconn_query_data_available( request
->netconn
);
2741 refill_buffer( request
, async
);
2742 count
= get_available_data( request
);
2743 if (!request
->read_chunked
&& request
->netconn
)
2744 count
+= netconn_query_data_available( request
->netconn
);
2748 if (async
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE
, &count
, sizeof(count
) );
2749 TRACE("%u bytes available\n", count
);
2750 if (available
) *available
= count
;
2754 static void task_query_data_available( task_header_t
*task
)
2756 query_data_t
*q
= (query_data_t
*)task
;
2757 query_data_available( q
->hdr
.request
, q
->available
, TRUE
);
2760 /***********************************************************************
2761 * WinHttpQueryDataAvailable (winhttp.@)
2763 BOOL WINAPI
WinHttpQueryDataAvailable( HINTERNET hrequest
, LPDWORD available
)
2768 TRACE("%p, %p\n", hrequest
, available
);
2770 if (!(request
= (request_t
*)grab_object( hrequest
)))
2772 set_last_error( ERROR_INVALID_HANDLE
);
2775 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
2777 release_object( &request
->hdr
);
2778 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2782 if (request
->connect
->hdr
.flags
& WINHTTP_FLAG_ASYNC
)
2786 if (!(q
= heap_alloc( sizeof(query_data_t
) ))) return FALSE
;
2787 q
->hdr
.request
= request
;
2788 q
->hdr
.proc
= task_query_data_available
;
2789 q
->available
= available
;
2791 addref_object( &request
->hdr
);
2792 ret
= queue_task( (task_header_t
*)q
);
2795 ret
= query_data_available( request
, available
, FALSE
);
2797 release_object( &request
->hdr
);
2798 if (ret
) set_last_error( ERROR_SUCCESS
);
2802 static void task_read_data( task_header_t
*task
)
2804 read_data_t
*r
= (read_data_t
*)task
;
2805 read_data( r
->hdr
.request
, r
->buffer
, r
->to_read
, r
->read
, TRUE
);
2808 /***********************************************************************
2809 * WinHttpReadData (winhttp.@)
2811 BOOL WINAPI
WinHttpReadData( HINTERNET hrequest
, LPVOID buffer
, DWORD to_read
, LPDWORD read
)
2816 TRACE("%p, %p, %d, %p\n", hrequest
, buffer
, to_read
, read
);
2818 if (!(request
= (request_t
*)grab_object( hrequest
)))
2820 set_last_error( ERROR_INVALID_HANDLE
);
2823 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
2825 release_object( &request
->hdr
);
2826 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2830 if (request
->connect
->hdr
.flags
& WINHTTP_FLAG_ASYNC
)
2834 if (!(r
= heap_alloc( sizeof(read_data_t
) ))) return FALSE
;
2835 r
->hdr
.request
= request
;
2836 r
->hdr
.proc
= task_read_data
;
2838 r
->to_read
= to_read
;
2841 addref_object( &request
->hdr
);
2842 ret
= queue_task( (task_header_t
*)r
);
2845 ret
= read_data( request
, buffer
, to_read
, read
, FALSE
);
2847 release_object( &request
->hdr
);
2848 if (ret
) set_last_error( ERROR_SUCCESS
);
2852 static BOOL
write_data( request_t
*request
, LPCVOID buffer
, DWORD to_write
, LPDWORD written
, BOOL async
)
2857 ret
= netconn_send( request
->netconn
, buffer
, to_write
, &num_bytes
);
2861 if (ret
) send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE
, &num_bytes
, sizeof(num_bytes
) );
2864 WINHTTP_ASYNC_RESULT result
;
2865 result
.dwResult
= API_WRITE_DATA
;
2866 result
.dwError
= get_last_error();
2867 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_REQUEST_ERROR
, &result
, sizeof(result
) );
2870 if (ret
&& written
) *written
= num_bytes
;
2874 static void task_write_data( task_header_t
*task
)
2876 write_data_t
*w
= (write_data_t
*)task
;
2877 write_data( w
->hdr
.request
, w
->buffer
, w
->to_write
, w
->written
, TRUE
);
2880 /***********************************************************************
2881 * WinHttpWriteData (winhttp.@)
2883 BOOL WINAPI
WinHttpWriteData( HINTERNET hrequest
, LPCVOID buffer
, DWORD to_write
, LPDWORD written
)
2888 TRACE("%p, %p, %d, %p\n", hrequest
, buffer
, to_write
, written
);
2890 if (!(request
= (request_t
*)grab_object( hrequest
)))
2892 set_last_error( ERROR_INVALID_HANDLE
);
2895 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
2897 release_object( &request
->hdr
);
2898 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
2902 if (request
->connect
->hdr
.flags
& WINHTTP_FLAG_ASYNC
)