wininet: Pass blocking mode to HTTPREQ_Read.
[wine.git] / dlls / wininet / http.c
blobfcd8d50594f4397197a97593d8b610667ffc01c4
1 /*
2 * Wininet - HTTP Implementation
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 TransGaming Technologies Inc.
7 * Copyright 2004 Mike McCormack for CodeWeavers
8 * Copyright 2005 Aric Stewart for CodeWeavers
9 * Copyright 2006 Robert Shearman for CodeWeavers
10 * Copyright 2011 Jacek Caban for CodeWeavers
12 * Ulrich Czekalla
13 * David Hammerton
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "config.h"
32 #include <stdlib.h>
34 #ifdef HAVE_ZLIB
35 # include <zlib.h>
36 #endif
38 #include "winsock2.h"
39 #include "ws2ipdef.h"
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <time.h>
44 #include <assert.h>
46 #include "windef.h"
47 #include "winbase.h"
48 #include "wininet.h"
49 #include "winerror.h"
50 #include "winternl.h"
51 #define NO_SHLWAPI_STREAM
52 #define NO_SHLWAPI_REG
53 #define NO_SHLWAPI_STRFCNS
54 #define NO_SHLWAPI_GDI
55 #include "shlwapi.h"
56 #include "sspi.h"
57 #include "wincrypt.h"
58 #include "winuser.h"
60 #include "internet.h"
61 #include "wine/debug.h"
62 #include "wine/exception.h"
63 #include "wine/unicode.h"
65 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
67 static const WCHAR g_szHttp1_0[] = {'H','T','T','P','/','1','.','0',0};
68 static const WCHAR g_szHttp1_1[] = {'H','T','T','P','/','1','.','1',0};
69 static const WCHAR szOK[] = {'O','K',0};
70 static const WCHAR hostW[] = { 'H','o','s','t',0 };
71 static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
72 static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
73 static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 };
74 static const WCHAR szKeepAlive[] = {'K','e','e','p','-','A','l','i','v','e',0};
75 static const WCHAR szGET[] = { 'G','E','T', 0 };
76 static const WCHAR szHEAD[] = { 'H','E','A','D', 0 };
78 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
79 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
80 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
81 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
82 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
83 static const WCHAR szAge[] = { 'A','g','e',0 };
84 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
85 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
86 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
87 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
88 static const WCHAR szContent_Disposition[] = { 'C','o','n','t','e','n','t','-','D','i','s','p','o','s','i','t','i','o','n',0 };
89 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
90 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
91 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
92 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
93 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
94 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
95 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
96 static const WCHAR szContent_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 };
97 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
98 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
99 static const WCHAR szDate[] = { 'D','a','t','e',0 };
100 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
101 static const WCHAR szETag[] = { 'E','T','a','g',0 };
102 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
103 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
104 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
105 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
106 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
107 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
108 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
109 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
110 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
111 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
112 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
113 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
114 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
115 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
116 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
117 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
118 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
119 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
120 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
121 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
122 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
123 static const WCHAR szUnless_Modified_Since[] = { 'U','n','l','e','s','s','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
124 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
125 static const WCHAR szURI[] = { 'U','R','I',0 };
126 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
127 static const WCHAR szVary[] = { 'V','a','r','y',0 };
128 static const WCHAR szVia[] = { 'V','i','a',0 };
129 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
130 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
132 static const WCHAR emptyW[] = {0};
134 #define HTTP_REFERER szReferer
135 #define HTTP_ACCEPT szAccept
136 #define HTTP_USERAGENT szUser_Agent
138 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
139 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
140 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
141 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
142 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
143 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
144 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
146 #define COLLECT_TIME 60000
148 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
150 struct HttpAuthInfo
152 LPWSTR scheme;
153 CredHandle cred;
154 CtxtHandle ctx;
155 TimeStamp exp;
156 ULONG attr;
157 ULONG max_token;
158 void *auth_data;
159 unsigned int auth_data_len;
160 BOOL finished; /* finished authenticating */
164 typedef struct _basicAuthorizationData
166 struct list entry;
168 LPWSTR host;
169 LPWSTR realm;
170 LPSTR authorization;
171 UINT authorizationLen;
172 } basicAuthorizationData;
174 typedef struct _authorizationData
176 struct list entry;
178 LPWSTR host;
179 LPWSTR scheme;
180 LPWSTR domain;
181 UINT domain_len;
182 LPWSTR user;
183 UINT user_len;
184 LPWSTR password;
185 UINT password_len;
186 } authorizationData;
188 static struct list basicAuthorizationCache = LIST_INIT(basicAuthorizationCache);
189 static struct list authorizationCache = LIST_INIT(authorizationCache);
191 static CRITICAL_SECTION authcache_cs;
192 static CRITICAL_SECTION_DEBUG critsect_debug =
194 0, 0, &authcache_cs,
195 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
196 0, 0, { (DWORD_PTR)(__FILE__ ": authcache_cs") }
198 static CRITICAL_SECTION authcache_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
200 static DWORD HTTP_GetResponseHeaders(http_request_t *req, INT *len);
201 static DWORD HTTP_ProcessHeader(http_request_t *req, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
202 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
203 static DWORD HTTP_InsertCustomHeader(http_request_t *req, LPHTTPHEADERW lpHdr);
204 static INT HTTP_GetCustomHeaderIndex(http_request_t *req, LPCWSTR lpszField, INT index, BOOL Request);
205 static BOOL HTTP_DeleteCustomHeader(http_request_t *req, DWORD index);
206 static LPWSTR HTTP_build_req( LPCWSTR *list, int len );
207 static DWORD HTTP_HttpQueryInfoW(http_request_t*, DWORD, LPVOID, LPDWORD, LPDWORD);
208 static UINT HTTP_DecodeBase64(LPCWSTR base64, LPSTR bin);
209 static BOOL drain_content(http_request_t*,BOOL);
211 static CRITICAL_SECTION connection_pool_cs;
212 static CRITICAL_SECTION_DEBUG connection_pool_debug =
214 0, 0, &connection_pool_cs,
215 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
216 0, 0, { (DWORD_PTR)(__FILE__ ": connection_pool_cs") }
218 static CRITICAL_SECTION connection_pool_cs = { &connection_pool_debug, -1, 0, 0, 0, 0 };
220 static struct list connection_pool = LIST_INIT(connection_pool);
221 static BOOL collector_running;
223 void server_addref(server_t *server)
225 InterlockedIncrement(&server->ref);
228 void server_release(server_t *server)
230 if(InterlockedDecrement(&server->ref))
231 return;
233 list_remove(&server->entry);
235 if(server->cert_chain)
236 CertFreeCertificateChain(server->cert_chain);
237 heap_free(server->name);
238 heap_free(server->scheme_host_port);
239 heap_free(server);
242 static BOOL process_host_port(server_t *server)
244 BOOL default_port;
245 size_t name_len;
246 WCHAR *buf;
248 static const WCHAR httpW[] = {'h','t','t','p',0};
249 static const WCHAR httpsW[] = {'h','t','t','p','s',0};
250 static const WCHAR formatW[] = {'%','s',':','/','/','%','s',':','%','u',0};
252 name_len = strlenW(server->name);
253 buf = heap_alloc((name_len + 10 /* strlen("://:<port>") */)*sizeof(WCHAR) + sizeof(httpsW));
254 if(!buf)
255 return FALSE;
257 sprintfW(buf, formatW, server->is_https ? httpsW : httpW, server->name, server->port);
258 server->scheme_host_port = buf;
260 server->host_port = server->scheme_host_port + 7 /* strlen("http://") */;
261 if(server->is_https)
262 server->host_port++;
264 default_port = server->port == (server->is_https ? INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT);
265 server->canon_host_port = default_port ? server->name : server->host_port;
266 return TRUE;
269 server_t *get_server(substr_t name, INTERNET_PORT port, BOOL is_https, BOOL do_create)
271 server_t *iter, *server = NULL;
273 EnterCriticalSection(&connection_pool_cs);
275 LIST_FOR_EACH_ENTRY(iter, &connection_pool, server_t, entry) {
276 if(iter->port == port && name.len == strlenW(iter->name) && !strncmpW(iter->name, name.str, name.len)
277 && iter->is_https == is_https) {
278 server = iter;
279 server_addref(server);
280 break;
284 if(!server && do_create) {
285 server = heap_alloc_zero(sizeof(*server));
286 if(server) {
287 server->ref = 2; /* list reference and return */
288 server->port = port;
289 server->is_https = is_https;
290 list_init(&server->conn_pool);
291 server->name = heap_strndupW(name.str, name.len);
292 if(server->name && process_host_port(server)) {
293 list_add_head(&connection_pool, &server->entry);
294 }else {
295 heap_free(server);
296 server = NULL;
301 LeaveCriticalSection(&connection_pool_cs);
303 return server;
306 BOOL collect_connections(collect_type_t collect_type)
308 netconn_t *netconn, *netconn_safe;
309 server_t *server, *server_safe;
310 BOOL remaining = FALSE;
311 DWORD64 now;
313 now = GetTickCount64();
315 LIST_FOR_EACH_ENTRY_SAFE(server, server_safe, &connection_pool, server_t, entry) {
316 LIST_FOR_EACH_ENTRY_SAFE(netconn, netconn_safe, &server->conn_pool, netconn_t, pool_entry) {
317 if(collect_type > COLLECT_TIMEOUT || netconn->keep_until < now) {
318 TRACE("freeing %p\n", netconn);
319 list_remove(&netconn->pool_entry);
320 free_netconn(netconn);
321 }else {
322 remaining = TRUE;
326 if(collect_type == COLLECT_CLEANUP) {
327 list_remove(&server->entry);
328 list_init(&server->entry);
329 server_release(server);
333 return remaining;
336 static DWORD WINAPI collect_connections_proc(void *arg)
338 BOOL remaining_conns;
340 do {
341 /* FIXME: Use more sophisticated method */
342 Sleep(5000);
344 EnterCriticalSection(&connection_pool_cs);
346 remaining_conns = collect_connections(COLLECT_TIMEOUT);
347 if(!remaining_conns)
348 collector_running = FALSE;
350 LeaveCriticalSection(&connection_pool_cs);
351 }while(remaining_conns);
353 FreeLibraryAndExitThread(WININET_hModule, 0);
356 /***********************************************************************
357 * HTTP_GetHeader (internal)
359 * Headers section must be held
361 static LPHTTPHEADERW HTTP_GetHeader(http_request_t *req, LPCWSTR head)
363 int HeaderIndex = 0;
364 HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
365 if (HeaderIndex == -1)
366 return NULL;
367 else
368 return &req->custHeaders[HeaderIndex];
371 static WCHAR *get_host_header( http_request_t *req )
373 HTTPHEADERW *header;
374 WCHAR *ret = NULL;
376 EnterCriticalSection( &req->headers_section );
377 if ((header = HTTP_GetHeader( req, hostW ))) ret = heap_strdupW( header->lpszValue );
378 else ret = heap_strdupW( req->server->canon_host_port );
379 LeaveCriticalSection( &req->headers_section );
380 return ret;
383 typedef enum {
384 BLOCKING_ALLOW,
385 BLOCKING_DISALLOW,
386 BLOCKING_WAITALL
387 } blocking_mode_t;
389 struct data_stream_vtbl_t {
390 DWORD (*get_avail_data)(data_stream_t*,http_request_t*);
391 BOOL (*end_of_data)(data_stream_t*,http_request_t*);
392 DWORD (*read)(data_stream_t*,http_request_t*,BYTE*,DWORD,DWORD*,blocking_mode_t);
393 BOOL (*drain_content)(data_stream_t*,http_request_t*);
394 void (*destroy)(data_stream_t*);
397 typedef struct {
398 data_stream_t data_stream;
400 BYTE buf[READ_BUFFER_SIZE];
401 DWORD buf_size;
402 DWORD buf_pos;
403 DWORD chunk_size;
405 enum {
406 CHUNKED_STREAM_STATE_READING_CHUNK_SIZE,
407 CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE,
408 CHUNKED_STREAM_STATE_READING_CHUNK,
409 CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA,
410 CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END,
411 CHUNKED_STREAM_STATE_END_OF_STREAM
412 } state;
413 } chunked_stream_t;
415 static inline void destroy_data_stream(data_stream_t *stream)
417 stream->vtbl->destroy(stream);
420 static void reset_data_stream(http_request_t *req)
422 destroy_data_stream(req->data_stream);
423 req->data_stream = &req->netconn_stream.data_stream;
424 req->read_pos = req->read_size = req->netconn_stream.content_read = 0;
425 req->read_gzip = FALSE;
428 static void remove_header( http_request_t *request, const WCHAR *str, BOOL from_request )
430 int index;
431 EnterCriticalSection( &request->headers_section );
432 index = HTTP_GetCustomHeaderIndex( request, str, 0, from_request );
433 if (index != -1) HTTP_DeleteCustomHeader( request, index );
434 LeaveCriticalSection( &request->headers_section );
437 #ifdef HAVE_ZLIB
439 typedef struct {
440 data_stream_t stream;
441 data_stream_t *parent_stream;
442 z_stream zstream;
443 BYTE buf[READ_BUFFER_SIZE];
444 DWORD buf_size;
445 DWORD buf_pos;
446 BOOL end_of_data;
447 } gzip_stream_t;
449 static DWORD gzip_get_avail_data(data_stream_t *stream, http_request_t *req)
451 /* Allow reading only from read buffer */
452 return 0;
455 static BOOL gzip_end_of_data(data_stream_t *stream, http_request_t *req)
457 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
458 return gzip_stream->end_of_data
459 || (!gzip_stream->buf_size && gzip_stream->parent_stream->vtbl->end_of_data(gzip_stream->parent_stream, req));
462 static DWORD gzip_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
463 DWORD *read, blocking_mode_t blocking_mode)
465 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
466 z_stream *zstream = &gzip_stream->zstream;
467 DWORD current_read, ret_read = 0;
468 int zres;
469 DWORD res = ERROR_SUCCESS;
471 TRACE("(%d %d)\n", size, blocking_mode);
473 while(size && !gzip_stream->end_of_data) {
474 if(!gzip_stream->buf_size) {
475 if(gzip_stream->buf_pos) {
476 if(gzip_stream->buf_size)
477 memmove(gzip_stream->buf, gzip_stream->buf+gzip_stream->buf_pos, gzip_stream->buf_size);
478 gzip_stream->buf_pos = 0;
480 res = gzip_stream->parent_stream->vtbl->read(gzip_stream->parent_stream, req, gzip_stream->buf+gzip_stream->buf_size,
481 sizeof(gzip_stream->buf)-gzip_stream->buf_size, &current_read, blocking_mode);
482 gzip_stream->buf_size += current_read;
483 if(res != ERROR_SUCCESS)
484 break;
486 if(!current_read) {
487 WARN("unexpected end of data\n");
488 gzip_stream->end_of_data = TRUE;
489 break;
493 zstream->next_in = gzip_stream->buf+gzip_stream->buf_pos;
494 zstream->avail_in = gzip_stream->buf_size;
495 zstream->next_out = buf+ret_read;
496 zstream->avail_out = size;
497 zres = inflate(&gzip_stream->zstream, 0);
498 current_read = size - zstream->avail_out;
499 size -= current_read;
500 ret_read += current_read;
501 gzip_stream->buf_size -= zstream->next_in - (gzip_stream->buf+gzip_stream->buf_pos);
502 gzip_stream->buf_pos = zstream->next_in-gzip_stream->buf;
503 if(zres == Z_STREAM_END) {
504 TRACE("end of data\n");
505 gzip_stream->end_of_data = TRUE;
506 inflateEnd(zstream);
507 }else if(zres != Z_OK) {
508 WARN("inflate failed %d: %s\n", zres, debugstr_a(zstream->msg));
509 if(!ret_read)
510 res = ERROR_INTERNET_DECODING_FAILED;
511 break;
514 if(ret_read && blocking_mode == BLOCKING_ALLOW)
515 blocking_mode = BLOCKING_DISALLOW;
518 TRACE("read %u bytes\n", ret_read);
519 if(ret_read)
520 res = ERROR_SUCCESS;
521 *read = ret_read;
522 return res;
525 static BOOL gzip_drain_content(data_stream_t *stream, http_request_t *req)
527 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
528 return gzip_stream->parent_stream->vtbl->drain_content(gzip_stream->parent_stream, req);
531 static void gzip_destroy(data_stream_t *stream)
533 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
535 destroy_data_stream(gzip_stream->parent_stream);
537 if(!gzip_stream->end_of_data)
538 inflateEnd(&gzip_stream->zstream);
539 heap_free(gzip_stream);
542 static const data_stream_vtbl_t gzip_stream_vtbl = {
543 gzip_get_avail_data,
544 gzip_end_of_data,
545 gzip_read,
546 gzip_drain_content,
547 gzip_destroy
550 static voidpf wininet_zalloc(voidpf opaque, uInt items, uInt size)
552 return heap_alloc(items*size);
555 static void wininet_zfree(voidpf opaque, voidpf address)
557 heap_free(address);
560 static DWORD init_gzip_stream(http_request_t *req, BOOL is_gzip)
562 gzip_stream_t *gzip_stream;
563 int zres;
565 gzip_stream = heap_alloc_zero(sizeof(gzip_stream_t));
566 if(!gzip_stream)
567 return ERROR_OUTOFMEMORY;
569 gzip_stream->stream.vtbl = &gzip_stream_vtbl;
570 gzip_stream->zstream.zalloc = wininet_zalloc;
571 gzip_stream->zstream.zfree = wininet_zfree;
573 zres = inflateInit2(&gzip_stream->zstream, is_gzip ? 0x1f : -15);
574 if(zres != Z_OK) {
575 ERR("inflateInit failed: %d\n", zres);
576 heap_free(gzip_stream);
577 return ERROR_OUTOFMEMORY;
580 remove_header(req, szContent_Length, FALSE);
582 if(req->read_size) {
583 memcpy(gzip_stream->buf, req->read_buf+req->read_pos, req->read_size);
584 gzip_stream->buf_size = req->read_size;
585 req->read_pos = req->read_size = 0;
588 req->read_gzip = TRUE;
589 gzip_stream->parent_stream = req->data_stream;
590 req->data_stream = &gzip_stream->stream;
591 return ERROR_SUCCESS;
594 #else
596 static DWORD init_gzip_stream(http_request_t *req, BOOL is_gzip)
598 ERR("gzip stream not supported, missing zlib.\n");
599 return ERROR_SUCCESS;
602 #endif
604 /***********************************************************************
605 * HTTP_FreeTokens (internal)
607 * Frees table of pointers.
609 static void HTTP_FreeTokens(LPWSTR * token_array)
611 int i;
612 for (i = 0; token_array[i]; i++) heap_free(token_array[i]);
613 heap_free(token_array);
616 static void HTTP_FixURL(http_request_t *request)
618 static const WCHAR szSlash[] = { '/',0 };
619 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
621 /* If we don't have a path we set it to root */
622 if (NULL == request->path)
623 request->path = heap_strdupW(szSlash);
624 else /* remove \r and \n*/
626 int nLen = strlenW(request->path);
627 while ((nLen >0 ) && ((request->path[nLen-1] == '\r')||(request->path[nLen-1] == '\n')))
629 nLen--;
630 request->path[nLen]='\0';
632 /* Replace '\' with '/' */
633 while (nLen>0) {
634 nLen--;
635 if (request->path[nLen] == '\\') request->path[nLen]='/';
639 if(CSTR_EQUAL != CompareStringW( LOCALE_INVARIANT, NORM_IGNORECASE,
640 request->path, strlenW(request->path), szHttp, strlenW(szHttp) )
641 && request->path[0] != '/') /* not an absolute path ?? --> fix it !! */
643 WCHAR *fixurl = heap_alloc((strlenW(request->path) + 2)*sizeof(WCHAR));
644 *fixurl = '/';
645 strcpyW(fixurl + 1, request->path);
646 heap_free( request->path );
647 request->path = fixurl;
651 static WCHAR* build_request_header(http_request_t *request, const WCHAR *verb,
652 const WCHAR *path, const WCHAR *version, BOOL use_cr)
654 static const WCHAR szSpace[] = {' ',0};
655 static const WCHAR szColon[] = {':',' ',0};
656 static const WCHAR szCr[] = {'\r',0};
657 static const WCHAR szLf[] = {'\n',0};
658 LPWSTR requestString;
659 DWORD len, n;
660 LPCWSTR *req;
661 UINT i;
663 EnterCriticalSection( &request->headers_section );
665 /* allocate space for an array of all the string pointers to be added */
666 len = request->nCustHeaders * 5 + 10;
667 if (!(req = heap_alloc( len * sizeof(const WCHAR *) )))
669 LeaveCriticalSection( &request->headers_section );
670 return NULL;
673 /* add the verb, path and HTTP version string */
674 n = 0;
675 req[n++] = verb;
676 req[n++] = szSpace;
677 req[n++] = path;
678 req[n++] = szSpace;
679 req[n++] = version;
680 if (use_cr)
681 req[n++] = szCr;
682 req[n++] = szLf;
684 /* Append custom request headers */
685 for (i = 0; i < request->nCustHeaders; i++)
687 if (request->custHeaders[i].wFlags & HDR_ISREQUEST)
689 req[n++] = request->custHeaders[i].lpszField;
690 req[n++] = szColon;
691 req[n++] = request->custHeaders[i].lpszValue;
692 if (use_cr)
693 req[n++] = szCr;
694 req[n++] = szLf;
696 TRACE("Adding custom header %s (%s)\n",
697 debugstr_w(request->custHeaders[i].lpszField),
698 debugstr_w(request->custHeaders[i].lpszValue));
701 if (use_cr)
702 req[n++] = szCr;
703 req[n++] = szLf;
704 req[n] = NULL;
706 requestString = HTTP_build_req( req, 4 );
707 heap_free( req );
708 LeaveCriticalSection( &request->headers_section );
709 return requestString;
712 static WCHAR* build_response_header(http_request_t *request, BOOL use_cr)
714 static const WCHAR colonW[] = { ':',' ',0 };
715 static const WCHAR crW[] = { '\r',0 };
716 static const WCHAR lfW[] = { '\n',0 };
717 static const WCHAR status_fmt[] = { ' ','%','u',' ',0 };
718 const WCHAR **req;
719 WCHAR *ret, buf[14];
720 DWORD i, n = 0;
722 EnterCriticalSection( &request->headers_section );
724 if (!(req = heap_alloc( (request->nCustHeaders * 5 + 8) * sizeof(WCHAR *) )))
726 LeaveCriticalSection( &request->headers_section );
727 return NULL;
730 if (request->status_code)
732 req[n++] = request->version;
733 sprintfW(buf, status_fmt, request->status_code);
734 req[n++] = buf;
735 req[n++] = request->statusText;
736 if (use_cr)
737 req[n++] = crW;
738 req[n++] = lfW;
741 for(i = 0; i < request->nCustHeaders; i++)
743 if(!(request->custHeaders[i].wFlags & HDR_ISREQUEST)
744 && strcmpW(request->custHeaders[i].lpszField, szStatus))
746 req[n++] = request->custHeaders[i].lpszField;
747 req[n++] = colonW;
748 req[n++] = request->custHeaders[i].lpszValue;
749 if(use_cr)
750 req[n++] = crW;
751 req[n++] = lfW;
753 TRACE("Adding custom header %s (%s)\n",
754 debugstr_w(request->custHeaders[i].lpszField),
755 debugstr_w(request->custHeaders[i].lpszValue));
758 if(use_cr)
759 req[n++] = crW;
760 req[n++] = lfW;
761 req[n] = NULL;
763 ret = HTTP_build_req(req, 0);
764 heap_free(req);
765 LeaveCriticalSection( &request->headers_section );
766 return ret;
769 static void HTTP_ProcessCookies( http_request_t *request )
771 int HeaderIndex;
772 int numCookies = 0;
773 LPHTTPHEADERW setCookieHeader;
775 if(request->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES)
776 return;
778 EnterCriticalSection( &request->headers_section );
780 while((HeaderIndex = HTTP_GetCustomHeaderIndex(request, szSet_Cookie, numCookies++, FALSE)) != -1)
782 const WCHAR *data;
783 substr_t name;
785 setCookieHeader = &request->custHeaders[HeaderIndex];
787 if (!setCookieHeader->lpszValue)
788 continue;
790 data = strchrW(setCookieHeader->lpszValue, '=');
791 if(!data)
792 continue;
794 name = substr(setCookieHeader->lpszValue, data - setCookieHeader->lpszValue);
795 data++;
796 set_cookie(substrz(request->server->name), substrz(request->path), name, substrz(data), INTERNET_COOKIE_HTTPONLY);
799 LeaveCriticalSection( &request->headers_section );
802 static void strip_spaces(LPWSTR start)
804 LPWSTR str = start;
805 LPWSTR end;
807 while (*str == ' ')
808 str++;
810 if (str != start)
811 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
813 end = start + strlenW(start) - 1;
814 while (end >= start && *end == ' ')
816 *end = '\0';
817 end--;
821 static inline BOOL is_basic_auth_value( LPCWSTR pszAuthValue, LPWSTR *pszRealm )
823 static const WCHAR szBasic[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
824 static const WCHAR szRealm[] = {'r','e','a','l','m'}; /* Note: not nul-terminated */
825 BOOL is_basic;
826 is_basic = !strncmpiW(pszAuthValue, szBasic, ARRAYSIZE(szBasic)) &&
827 ((pszAuthValue[ARRAYSIZE(szBasic)] == ' ') || !pszAuthValue[ARRAYSIZE(szBasic)]);
828 if (is_basic && pszRealm)
830 LPCWSTR token;
831 LPCWSTR ptr = &pszAuthValue[ARRAYSIZE(szBasic)];
832 LPCWSTR realm;
833 ptr++;
834 *pszRealm=NULL;
835 token = strchrW(ptr,'=');
836 if (!token)
837 return TRUE;
838 realm = ptr;
839 while (*realm == ' ')
840 realm++;
841 if(!strncmpiW(realm, szRealm, ARRAYSIZE(szRealm)) &&
842 (realm[ARRAYSIZE(szRealm)] == ' ' || realm[ARRAYSIZE(szRealm)] == '='))
844 token++;
845 while (*token == ' ')
846 token++;
847 if (*token == '\0')
848 return TRUE;
849 *pszRealm = heap_strdupW(token);
850 strip_spaces(*pszRealm);
854 return is_basic;
857 static void destroy_authinfo( struct HttpAuthInfo *authinfo )
859 if (!authinfo) return;
861 if (SecIsValidHandle(&authinfo->ctx))
862 DeleteSecurityContext(&authinfo->ctx);
863 if (SecIsValidHandle(&authinfo->cred))
864 FreeCredentialsHandle(&authinfo->cred);
866 heap_free(authinfo->auth_data);
867 heap_free(authinfo->scheme);
868 heap_free(authinfo);
871 static UINT retrieve_cached_basic_authorization(const WCHAR *host, const WCHAR *realm, char **auth_data)
873 basicAuthorizationData *ad;
874 UINT rc = 0;
876 TRACE("Looking for authorization for %s:%s\n",debugstr_w(host),debugstr_w(realm));
878 EnterCriticalSection(&authcache_cs);
879 LIST_FOR_EACH_ENTRY(ad, &basicAuthorizationCache, basicAuthorizationData, entry)
881 if (!strcmpiW(host, ad->host) && (!realm || !strcmpW(realm, ad->realm)))
883 TRACE("Authorization found in cache\n");
884 *auth_data = heap_alloc(ad->authorizationLen);
885 memcpy(*auth_data,ad->authorization,ad->authorizationLen);
886 rc = ad->authorizationLen;
887 break;
890 LeaveCriticalSection(&authcache_cs);
891 return rc;
894 static void cache_basic_authorization(LPWSTR host, LPWSTR realm, LPSTR auth_data, UINT auth_data_len)
896 struct list *cursor;
897 basicAuthorizationData* ad = NULL;
899 TRACE("caching authorization for %s:%s = %s\n",debugstr_w(host),debugstr_w(realm),debugstr_an(auth_data,auth_data_len));
901 EnterCriticalSection(&authcache_cs);
902 LIST_FOR_EACH(cursor, &basicAuthorizationCache)
904 basicAuthorizationData *check = LIST_ENTRY(cursor,basicAuthorizationData,entry);
905 if (!strcmpiW(host,check->host) && !strcmpW(realm,check->realm))
907 ad = check;
908 break;
912 if (ad)
914 TRACE("Found match in cache, replacing\n");
915 heap_free(ad->authorization);
916 ad->authorization = heap_alloc(auth_data_len);
917 memcpy(ad->authorization, auth_data, auth_data_len);
918 ad->authorizationLen = auth_data_len;
920 else
922 ad = heap_alloc(sizeof(basicAuthorizationData));
923 ad->host = heap_strdupW(host);
924 ad->realm = heap_strdupW(realm);
925 ad->authorization = heap_alloc(auth_data_len);
926 memcpy(ad->authorization, auth_data, auth_data_len);
927 ad->authorizationLen = auth_data_len;
928 list_add_head(&basicAuthorizationCache,&ad->entry);
929 TRACE("authorization cached\n");
931 LeaveCriticalSection(&authcache_cs);
934 static BOOL retrieve_cached_authorization(LPWSTR host, LPWSTR scheme,
935 SEC_WINNT_AUTH_IDENTITY_W *nt_auth_identity)
937 authorizationData *ad;
939 TRACE("Looking for authorization for %s:%s\n", debugstr_w(host), debugstr_w(scheme));
941 EnterCriticalSection(&authcache_cs);
942 LIST_FOR_EACH_ENTRY(ad, &authorizationCache, authorizationData, entry) {
943 if(!strcmpiW(host, ad->host) && !strcmpiW(scheme, ad->scheme)) {
944 TRACE("Authorization found in cache\n");
946 nt_auth_identity->User = heap_strdupW(ad->user);
947 nt_auth_identity->Password = heap_strdupW(ad->password);
948 nt_auth_identity->Domain = heap_alloc(sizeof(WCHAR)*ad->domain_len);
949 if(!nt_auth_identity->User || !nt_auth_identity->Password ||
950 (!nt_auth_identity->Domain && ad->domain_len)) {
951 heap_free(nt_auth_identity->User);
952 heap_free(nt_auth_identity->Password);
953 heap_free(nt_auth_identity->Domain);
954 break;
957 nt_auth_identity->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
958 nt_auth_identity->UserLength = ad->user_len;
959 nt_auth_identity->PasswordLength = ad->password_len;
960 memcpy(nt_auth_identity->Domain, ad->domain, sizeof(WCHAR)*ad->domain_len);
961 nt_auth_identity->DomainLength = ad->domain_len;
962 LeaveCriticalSection(&authcache_cs);
963 return TRUE;
966 LeaveCriticalSection(&authcache_cs);
968 return FALSE;
971 static void cache_authorization(LPWSTR host, LPWSTR scheme,
972 SEC_WINNT_AUTH_IDENTITY_W *nt_auth_identity)
974 authorizationData *ad;
975 BOOL found = FALSE;
977 TRACE("Caching authorization for %s:%s\n", debugstr_w(host), debugstr_w(scheme));
979 EnterCriticalSection(&authcache_cs);
980 LIST_FOR_EACH_ENTRY(ad, &authorizationCache, authorizationData, entry)
981 if(!strcmpiW(host, ad->host) && !strcmpiW(scheme, ad->scheme)) {
982 found = TRUE;
983 break;
986 if(found) {
987 heap_free(ad->user);
988 heap_free(ad->password);
989 heap_free(ad->domain);
990 } else {
991 ad = heap_alloc(sizeof(authorizationData));
992 if(!ad) {
993 LeaveCriticalSection(&authcache_cs);
994 return;
997 ad->host = heap_strdupW(host);
998 ad->scheme = heap_strdupW(scheme);
999 list_add_head(&authorizationCache, &ad->entry);
1002 ad->user = heap_strndupW(nt_auth_identity->User, nt_auth_identity->UserLength);
1003 ad->password = heap_strndupW(nt_auth_identity->Password, nt_auth_identity->PasswordLength);
1004 ad->domain = heap_strndupW(nt_auth_identity->Domain, nt_auth_identity->DomainLength);
1005 ad->user_len = nt_auth_identity->UserLength;
1006 ad->password_len = nt_auth_identity->PasswordLength;
1007 ad->domain_len = nt_auth_identity->DomainLength;
1009 if(!ad->host || !ad->scheme || !ad->user || !ad->password
1010 || (nt_auth_identity->Domain && !ad->domain)) {
1011 heap_free(ad->host);
1012 heap_free(ad->scheme);
1013 heap_free(ad->user);
1014 heap_free(ad->password);
1015 heap_free(ad->domain);
1016 list_remove(&ad->entry);
1017 heap_free(ad);
1020 LeaveCriticalSection(&authcache_cs);
1023 static BOOL HTTP_DoAuthorization( http_request_t *request, LPCWSTR pszAuthValue,
1024 struct HttpAuthInfo **ppAuthInfo,
1025 LPWSTR domain_and_username, LPWSTR password,
1026 LPWSTR host )
1028 SECURITY_STATUS sec_status;
1029 struct HttpAuthInfo *pAuthInfo = *ppAuthInfo;
1030 BOOL first = FALSE;
1031 LPWSTR szRealm = NULL;
1033 TRACE("%s\n", debugstr_w(pszAuthValue));
1035 if (!pAuthInfo)
1037 TimeStamp exp;
1039 first = TRUE;
1040 pAuthInfo = heap_alloc(sizeof(*pAuthInfo));
1041 if (!pAuthInfo)
1042 return FALSE;
1044 SecInvalidateHandle(&pAuthInfo->cred);
1045 SecInvalidateHandle(&pAuthInfo->ctx);
1046 memset(&pAuthInfo->exp, 0, sizeof(pAuthInfo->exp));
1047 pAuthInfo->attr = 0;
1048 pAuthInfo->auth_data = NULL;
1049 pAuthInfo->auth_data_len = 0;
1050 pAuthInfo->finished = FALSE;
1052 if (is_basic_auth_value(pszAuthValue,NULL))
1054 static const WCHAR szBasic[] = {'B','a','s','i','c',0};
1055 pAuthInfo->scheme = heap_strdupW(szBasic);
1056 if (!pAuthInfo->scheme)
1058 heap_free(pAuthInfo);
1059 return FALSE;
1062 else
1064 PVOID pAuthData;
1065 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity;
1067 pAuthInfo->scheme = heap_strdupW(pszAuthValue);
1068 if (!pAuthInfo->scheme)
1070 heap_free(pAuthInfo);
1071 return FALSE;
1074 if (domain_and_username)
1076 WCHAR *user = strchrW(domain_and_username, '\\');
1077 WCHAR *domain = domain_and_username;
1079 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
1081 pAuthData = &nt_auth_identity;
1083 if (user) user++;
1084 else
1086 user = domain_and_username;
1087 domain = NULL;
1090 nt_auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
1091 nt_auth_identity.User = user;
1092 nt_auth_identity.UserLength = strlenW(nt_auth_identity.User);
1093 nt_auth_identity.Domain = domain;
1094 nt_auth_identity.DomainLength = domain ? user - domain - 1 : 0;
1095 nt_auth_identity.Password = password;
1096 nt_auth_identity.PasswordLength = strlenW(nt_auth_identity.Password);
1098 cache_authorization(host, pAuthInfo->scheme, &nt_auth_identity);
1100 else if(retrieve_cached_authorization(host, pAuthInfo->scheme, &nt_auth_identity))
1101 pAuthData = &nt_auth_identity;
1102 else
1103 /* use default credentials */
1104 pAuthData = NULL;
1106 sec_status = AcquireCredentialsHandleW(NULL, pAuthInfo->scheme,
1107 SECPKG_CRED_OUTBOUND, NULL,
1108 pAuthData, NULL,
1109 NULL, &pAuthInfo->cred,
1110 &exp);
1112 if(pAuthData && !domain_and_username) {
1113 heap_free(nt_auth_identity.User);
1114 heap_free(nt_auth_identity.Domain);
1115 heap_free(nt_auth_identity.Password);
1118 if (sec_status == SEC_E_OK)
1120 PSecPkgInfoW sec_pkg_info;
1121 sec_status = QuerySecurityPackageInfoW(pAuthInfo->scheme, &sec_pkg_info);
1122 if (sec_status == SEC_E_OK)
1124 pAuthInfo->max_token = sec_pkg_info->cbMaxToken;
1125 FreeContextBuffer(sec_pkg_info);
1128 if (sec_status != SEC_E_OK)
1130 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
1131 debugstr_w(pAuthInfo->scheme), sec_status);
1132 heap_free(pAuthInfo->scheme);
1133 heap_free(pAuthInfo);
1134 return FALSE;
1137 *ppAuthInfo = pAuthInfo;
1139 else if (pAuthInfo->finished)
1140 return FALSE;
1142 if ((strlenW(pszAuthValue) < strlenW(pAuthInfo->scheme)) ||
1143 strncmpiW(pszAuthValue, pAuthInfo->scheme, strlenW(pAuthInfo->scheme)))
1145 ERR("authentication scheme changed from %s to %s\n",
1146 debugstr_w(pAuthInfo->scheme), debugstr_w(pszAuthValue));
1147 return FALSE;
1150 if (is_basic_auth_value(pszAuthValue,&szRealm))
1152 int userlen;
1153 int passlen;
1154 char *auth_data = NULL;
1155 UINT auth_data_len = 0;
1157 TRACE("basic authentication realm %s\n",debugstr_w(szRealm));
1159 if (!domain_and_username)
1161 if (host && szRealm)
1162 auth_data_len = retrieve_cached_basic_authorization(host, szRealm,&auth_data);
1163 if (auth_data_len == 0)
1165 heap_free(szRealm);
1166 return FALSE;
1169 else
1171 userlen = WideCharToMultiByte(CP_UTF8, 0, domain_and_username, lstrlenW(domain_and_username), NULL, 0, NULL, NULL);
1172 passlen = WideCharToMultiByte(CP_UTF8, 0, password, lstrlenW(password), NULL, 0, NULL, NULL);
1174 /* length includes a nul terminator, which will be re-used for the ':' */
1175 auth_data = heap_alloc(userlen + 1 + passlen);
1176 if (!auth_data)
1178 heap_free(szRealm);
1179 return FALSE;
1182 WideCharToMultiByte(CP_UTF8, 0, domain_and_username, -1, auth_data, userlen, NULL, NULL);
1183 auth_data[userlen] = ':';
1184 WideCharToMultiByte(CP_UTF8, 0, password, -1, &auth_data[userlen+1], passlen, NULL, NULL);
1185 auth_data_len = userlen + 1 + passlen;
1186 if (host && szRealm)
1187 cache_basic_authorization(host, szRealm, auth_data, auth_data_len);
1190 pAuthInfo->auth_data = auth_data;
1191 pAuthInfo->auth_data_len = auth_data_len;
1192 pAuthInfo->finished = TRUE;
1193 heap_free(szRealm);
1194 return TRUE;
1196 else
1198 LPCWSTR pszAuthData;
1199 SecBufferDesc out_desc, in_desc;
1200 SecBuffer out, in;
1201 unsigned char *buffer;
1202 ULONG context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE |
1203 ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE;
1205 in.BufferType = SECBUFFER_TOKEN;
1206 in.cbBuffer = 0;
1207 in.pvBuffer = NULL;
1209 in_desc.ulVersion = 0;
1210 in_desc.cBuffers = 1;
1211 in_desc.pBuffers = &in;
1213 pszAuthData = pszAuthValue + strlenW(pAuthInfo->scheme);
1214 if (*pszAuthData == ' ')
1216 pszAuthData++;
1217 in.cbBuffer = HTTP_DecodeBase64(pszAuthData, NULL);
1218 in.pvBuffer = heap_alloc(in.cbBuffer);
1219 HTTP_DecodeBase64(pszAuthData, in.pvBuffer);
1222 buffer = heap_alloc(pAuthInfo->max_token);
1224 out.BufferType = SECBUFFER_TOKEN;
1225 out.cbBuffer = pAuthInfo->max_token;
1226 out.pvBuffer = buffer;
1228 out_desc.ulVersion = 0;
1229 out_desc.cBuffers = 1;
1230 out_desc.pBuffers = &out;
1232 sec_status = InitializeSecurityContextW(first ? &pAuthInfo->cred : NULL,
1233 first ? NULL : &pAuthInfo->ctx,
1234 first ? request->server->name : NULL,
1235 context_req, 0, SECURITY_NETWORK_DREP,
1236 in.pvBuffer ? &in_desc : NULL,
1237 0, &pAuthInfo->ctx, &out_desc,
1238 &pAuthInfo->attr, &pAuthInfo->exp);
1239 if (sec_status == SEC_E_OK)
1241 pAuthInfo->finished = TRUE;
1242 pAuthInfo->auth_data = out.pvBuffer;
1243 pAuthInfo->auth_data_len = out.cbBuffer;
1244 TRACE("sending last auth packet\n");
1246 else if (sec_status == SEC_I_CONTINUE_NEEDED)
1248 pAuthInfo->auth_data = out.pvBuffer;
1249 pAuthInfo->auth_data_len = out.cbBuffer;
1250 TRACE("sending next auth packet\n");
1252 else
1254 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status);
1255 heap_free(out.pvBuffer);
1256 destroy_authinfo(pAuthInfo);
1257 *ppAuthInfo = NULL;
1258 return FALSE;
1262 return TRUE;
1265 /***********************************************************************
1266 * HTTP_HttpAddRequestHeadersW (internal)
1268 static DWORD HTTP_HttpAddRequestHeadersW(http_request_t *request,
1269 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1271 LPWSTR lpszStart;
1272 LPWSTR lpszEnd;
1273 LPWSTR buffer;
1274 DWORD len, res = ERROR_HTTP_INVALID_HEADER;
1276 TRACE("copying header: %s\n", debugstr_wn(lpszHeader, dwHeaderLength));
1278 if( dwHeaderLength == ~0U )
1279 len = strlenW(lpszHeader);
1280 else
1281 len = dwHeaderLength;
1282 buffer = heap_alloc(sizeof(WCHAR)*(len+1));
1283 lstrcpynW( buffer, lpszHeader, len + 1);
1285 lpszStart = buffer;
1289 LPWSTR * pFieldAndValue;
1291 lpszEnd = lpszStart;
1293 while (*lpszEnd != '\0')
1295 if (*lpszEnd == '\r' || *lpszEnd == '\n')
1296 break;
1297 lpszEnd++;
1300 if (*lpszStart == '\0')
1301 break;
1303 if (*lpszEnd == '\r' || *lpszEnd == '\n')
1305 *lpszEnd = '\0';
1306 lpszEnd++; /* Jump over newline */
1308 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
1309 if (*lpszStart == '\0')
1311 /* Skip 0-length headers */
1312 lpszStart = lpszEnd;
1313 res = ERROR_SUCCESS;
1314 continue;
1316 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
1317 if (pFieldAndValue)
1319 res = HTTP_ProcessHeader(request, pFieldAndValue[0],
1320 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
1321 HTTP_FreeTokens(pFieldAndValue);
1324 lpszStart = lpszEnd;
1325 } while (res == ERROR_SUCCESS);
1327 heap_free(buffer);
1328 return res;
1331 /***********************************************************************
1332 * HttpAddRequestHeadersW (WININET.@)
1334 * Adds one or more HTTP header to the request handler
1336 * NOTE
1337 * On Windows if dwHeaderLength includes the trailing '\0', then
1338 * HttpAddRequestHeadersW() adds it too. However this results in an
1339 * invalid HTTP header which is rejected by some servers so we probably
1340 * don't need to match Windows on that point.
1342 * RETURNS
1343 * TRUE on success
1344 * FALSE on failure
1347 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
1348 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1350 http_request_t *request;
1351 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1353 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_wn(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
1355 if (!lpszHeader)
1356 return TRUE;
1358 request = (http_request_t*) get_handle_object( hHttpRequest );
1359 if (request && request->hdr.htype == WH_HHTTPREQ)
1360 res = HTTP_HttpAddRequestHeadersW( request, lpszHeader, dwHeaderLength, dwModifier );
1361 if( request )
1362 WININET_Release( &request->hdr );
1364 if(res != ERROR_SUCCESS)
1365 SetLastError(res);
1366 return res == ERROR_SUCCESS;
1369 /***********************************************************************
1370 * HttpAddRequestHeadersA (WININET.@)
1372 * Adds one or more HTTP header to the request handler
1374 * RETURNS
1375 * TRUE on success
1376 * FALSE on failure
1379 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
1380 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1382 WCHAR *headers = NULL;
1383 BOOL r;
1385 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_an(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
1387 if(lpszHeader)
1388 headers = heap_strndupAtoW(lpszHeader, dwHeaderLength, &dwHeaderLength);
1390 r = HttpAddRequestHeadersW(hHttpRequest, headers, dwHeaderLength, dwModifier);
1392 heap_free(headers);
1393 return r;
1396 static void free_accept_types( WCHAR **accept_types )
1398 WCHAR *ptr, **types = accept_types;
1400 if (!types) return;
1401 while ((ptr = *types))
1403 heap_free( ptr );
1404 types++;
1406 heap_free( accept_types );
1409 static WCHAR **convert_accept_types( const char **accept_types )
1411 unsigned int count;
1412 const char **types = accept_types;
1413 WCHAR **typesW;
1414 BOOL invalid_pointer = FALSE;
1416 if (!types) return NULL;
1417 count = 0;
1418 while (*types)
1420 __TRY
1422 /* find out how many there are */
1423 if (*types && **types)
1425 TRACE("accept type: %s\n", debugstr_a(*types));
1426 count++;
1429 __EXCEPT_PAGE_FAULT
1431 WARN("invalid accept type pointer\n");
1432 invalid_pointer = TRUE;
1434 __ENDTRY;
1435 types++;
1437 if (invalid_pointer) return NULL;
1438 if (!(typesW = heap_alloc( sizeof(WCHAR *) * (count + 1) ))) return NULL;
1439 count = 0;
1440 types = accept_types;
1441 while (*types)
1443 if (*types && **types) typesW[count++] = heap_strdupAtoW( *types );
1444 types++;
1446 typesW[count] = NULL;
1447 return typesW;
1450 /***********************************************************************
1451 * HttpOpenRequestA (WININET.@)
1453 * Open a HTTP request handle
1455 * RETURNS
1456 * HINTERNET a HTTP request handle on success
1457 * NULL on failure
1460 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
1461 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
1462 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
1463 DWORD dwFlags, DWORD_PTR dwContext)
1465 LPWSTR szVerb = NULL, szObjectName = NULL;
1466 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
1467 HINTERNET rc = NULL;
1469 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
1470 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
1471 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
1472 dwFlags, dwContext);
1474 if (lpszVerb)
1476 szVerb = heap_strdupAtoW(lpszVerb);
1477 if ( !szVerb )
1478 goto end;
1481 if (lpszObjectName)
1483 szObjectName = heap_strdupAtoW(lpszObjectName);
1484 if ( !szObjectName )
1485 goto end;
1488 if (lpszVersion)
1490 szVersion = heap_strdupAtoW(lpszVersion);
1491 if ( !szVersion )
1492 goto end;
1495 if (lpszReferrer)
1497 szReferrer = heap_strdupAtoW(lpszReferrer);
1498 if ( !szReferrer )
1499 goto end;
1502 szAcceptTypes = convert_accept_types( lpszAcceptTypes );
1503 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName, szVersion, szReferrer,
1504 (const WCHAR **)szAcceptTypes, dwFlags, dwContext);
1506 end:
1507 free_accept_types(szAcceptTypes);
1508 heap_free(szReferrer);
1509 heap_free(szVersion);
1510 heap_free(szObjectName);
1511 heap_free(szVerb);
1512 return rc;
1515 /***********************************************************************
1516 * HTTP_EncodeBase64
1518 static UINT HTTP_EncodeBase64( LPCSTR bin, unsigned int len, LPWSTR base64 )
1520 UINT n = 0, x;
1521 static const CHAR HTTP_Base64Enc[] =
1522 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1524 while( len > 0 )
1526 /* first 6 bits, all from bin[0] */
1527 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
1528 x = (bin[0] & 3) << 4;
1530 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1531 if( len == 1 )
1533 base64[n++] = HTTP_Base64Enc[x];
1534 base64[n++] = '=';
1535 base64[n++] = '=';
1536 break;
1538 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
1539 x = ( bin[1] & 0x0f ) << 2;
1541 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1542 if( len == 2 )
1544 base64[n++] = HTTP_Base64Enc[x];
1545 base64[n++] = '=';
1546 break;
1548 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
1550 /* last 6 bits, all from bin [2] */
1551 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
1552 bin += 3;
1553 len -= 3;
1555 base64[n] = 0;
1556 return n;
1559 static const signed char HTTP_Base64Dec[] =
1561 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00 */
1562 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10 */
1563 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20 */
1564 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 0x30 */
1565 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40 */
1566 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50 */
1567 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60 */
1568 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 0x70 */
1571 /***********************************************************************
1572 * HTTP_DecodeBase64
1574 static UINT HTTP_DecodeBase64( LPCWSTR base64, LPSTR bin )
1576 unsigned int n = 0;
1578 while(*base64)
1580 signed char in[4];
1582 if (base64[0] >= ARRAYSIZE(HTTP_Base64Dec) ||
1583 ((in[0] = HTTP_Base64Dec[base64[0]]) == -1) ||
1584 base64[1] >= ARRAYSIZE(HTTP_Base64Dec) ||
1585 ((in[1] = HTTP_Base64Dec[base64[1]]) == -1))
1587 WARN("invalid base64: %s\n", debugstr_w(base64));
1588 return 0;
1590 if (bin)
1591 bin[n] = (unsigned char) (in[0] << 2 | in[1] >> 4);
1592 n++;
1594 if ((base64[2] == '=') && (base64[3] == '='))
1595 break;
1596 if (base64[2] > ARRAYSIZE(HTTP_Base64Dec) ||
1597 ((in[2] = HTTP_Base64Dec[base64[2]]) == -1))
1599 WARN("invalid base64: %s\n", debugstr_w(&base64[2]));
1600 return 0;
1602 if (bin)
1603 bin[n] = (unsigned char) (in[1] << 4 | in[2] >> 2);
1604 n++;
1606 if (base64[3] == '=')
1607 break;
1608 if (base64[3] > ARRAYSIZE(HTTP_Base64Dec) ||
1609 ((in[3] = HTTP_Base64Dec[base64[3]]) == -1))
1611 WARN("invalid base64: %s\n", debugstr_w(&base64[3]));
1612 return 0;
1614 if (bin)
1615 bin[n] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
1616 n++;
1618 base64 += 4;
1621 return n;
1624 static WCHAR *encode_auth_data( const WCHAR *scheme, const char *data, UINT data_len )
1626 WCHAR *ret;
1627 UINT len, scheme_len = strlenW( scheme );
1629 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1630 len = scheme_len + 1 + ((data_len + 2) * 4) / 3;
1631 if (!(ret = heap_alloc( (len + 1) * sizeof(WCHAR) ))) return NULL;
1632 memcpy( ret, scheme, scheme_len * sizeof(WCHAR) );
1633 ret[scheme_len] = ' ';
1634 HTTP_EncodeBase64( data, data_len, ret + scheme_len + 1 );
1635 return ret;
1639 /***********************************************************************
1640 * HTTP_InsertAuthorization
1642 * Insert or delete the authorization field in the request header.
1644 static BOOL HTTP_InsertAuthorization( http_request_t *request, struct HttpAuthInfo *pAuthInfo, LPCWSTR header )
1646 static const WCHAR wszBasic[] = {'B','a','s','i','c',0};
1647 WCHAR *host, *authorization = NULL;
1649 if (pAuthInfo)
1651 if (pAuthInfo->auth_data_len)
1653 if (!(authorization = encode_auth_data(pAuthInfo->scheme, pAuthInfo->auth_data, pAuthInfo->auth_data_len)))
1654 return FALSE;
1656 /* clear the data as it isn't valid now that it has been sent to the
1657 * server, unless it's Basic authentication which doesn't do
1658 * connection tracking */
1659 if (strcmpiW(pAuthInfo->scheme, wszBasic))
1661 heap_free(pAuthInfo->auth_data);
1662 pAuthInfo->auth_data = NULL;
1663 pAuthInfo->auth_data_len = 0;
1667 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1669 HTTP_ProcessHeader(request, header, authorization,
1670 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
1671 heap_free(authorization);
1673 else if (!strcmpW(header, szAuthorization) && (host = get_host_header(request)))
1675 UINT data_len;
1676 char *data;
1678 if ((data_len = retrieve_cached_basic_authorization(host, NULL, &data)))
1680 TRACE("Found cached basic authorization for %s\n", debugstr_w(host));
1682 if (!(authorization = encode_auth_data(wszBasic, data, data_len)))
1684 heap_free(data);
1685 heap_free(host);
1686 return FALSE;
1689 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1691 HTTP_ProcessHeader(request, header, authorization,
1692 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_ADD);
1693 heap_free(data);
1694 heap_free(authorization);
1696 heap_free(host);
1698 return TRUE;
1701 static WCHAR *build_proxy_path_url(http_request_t *req)
1703 DWORD size, len;
1704 WCHAR *url;
1706 len = strlenW(req->server->scheme_host_port);
1707 size = len + strlenW(req->path) + 1;
1708 if(*req->path != '/')
1709 size++;
1710 url = heap_alloc(size * sizeof(WCHAR));
1711 if(!url)
1712 return NULL;
1714 memcpy(url, req->server->scheme_host_port, len*sizeof(WCHAR));
1715 if(*req->path != '/')
1716 url[len++] = '/';
1718 strcpyW(url+len, req->path);
1720 TRACE("url=%s\n", debugstr_w(url));
1721 return url;
1724 static BOOL HTTP_DomainMatches(LPCWSTR server, substr_t domain)
1726 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
1727 const WCHAR *dot, *ptr;
1728 int len;
1730 if(domain.len == sizeof(localW)/sizeof(WCHAR)-1 && !strncmpiW(domain.str, localW, domain.len) && !strchrW(server, '.' ))
1731 return TRUE;
1733 if(domain.len && *domain.str != '*')
1734 return domain.len == strlenW(server) && !strncmpiW(server, domain.str, domain.len);
1736 if(domain.len < 2 || domain.str[1] != '.')
1737 return FALSE;
1739 /* For a hostname to match a wildcard, the last domain must match
1740 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
1741 * hostname is www.foo.a.b, it matches, but a.b does not.
1743 dot = strchrW(server, '.');
1744 if(!dot)
1745 return FALSE;
1747 len = strlenW(dot + 1);
1748 if(len <= domain.len - 2)
1749 return FALSE;
1751 /* The server's domain is longer than the wildcard, so it
1752 * could be a subdomain. Compare the last portion of the
1753 * server's domain.
1755 ptr = dot + 1 + len - domain.len + 2;
1756 if(!strncmpiW(ptr, domain.str+2, domain.len-2))
1757 /* This is only a match if the preceding character is
1758 * a '.', i.e. that it is a matching domain. E.g.
1759 * if domain is '*.b.c' and server is 'www.ab.c' they
1760 * do not match.
1762 return *(ptr - 1) == '.';
1764 return len == domain.len-2 && !strncmpiW(dot + 1, domain.str + 2, len);
1767 static BOOL HTTP_ShouldBypassProxy(appinfo_t *lpwai, LPCWSTR server)
1769 LPCWSTR ptr;
1770 BOOL ret = FALSE;
1772 if (!lpwai->proxyBypass) return FALSE;
1773 ptr = lpwai->proxyBypass;
1774 while(1) {
1775 LPCWSTR tmp = ptr;
1777 ptr = strchrW( ptr, ';' );
1778 if (!ptr)
1779 ptr = strchrW( tmp, ' ' );
1780 if (!ptr)
1781 ptr = tmp + strlenW(tmp);
1782 ret = HTTP_DomainMatches( server, substr(tmp, ptr-tmp) );
1783 if (ret || !*ptr)
1784 break;
1785 ptr++;
1787 return ret;
1790 /***********************************************************************
1791 * HTTP_DealWithProxy
1793 static BOOL HTTP_DealWithProxy(appinfo_t *hIC, http_session_t *session, http_request_t *request)
1795 static const WCHAR protoHttp[] = { 'h','t','t','p',0 };
1796 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 };
1797 static WCHAR szNul[] = { 0 };
1798 URL_COMPONENTSW UrlComponents = { sizeof(UrlComponents) };
1799 server_t *new_server = NULL;
1800 WCHAR *proxy;
1802 proxy = INTERNET_FindProxyForProtocol(hIC->proxy, protoHttp);
1803 if(!proxy)
1804 return FALSE;
1805 if(CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1806 proxy, strlenW(szHttp), szHttp, strlenW(szHttp))) {
1807 WCHAR *proxy_url = heap_alloc(strlenW(proxy)*sizeof(WCHAR) + sizeof(szHttp));
1808 if(!proxy_url) {
1809 heap_free(proxy);
1810 return FALSE;
1812 strcpyW(proxy_url, szHttp);
1813 strcatW(proxy_url, proxy);
1814 heap_free(proxy);
1815 proxy = proxy_url;
1818 UrlComponents.dwHostNameLength = 1;
1819 if(InternetCrackUrlW(proxy, 0, 0, &UrlComponents) && UrlComponents.dwHostNameLength) {
1820 if( !request->path )
1821 request->path = szNul;
1823 new_server = get_server(substr(UrlComponents.lpszHostName, UrlComponents.dwHostNameLength),
1824 UrlComponents.nPort, UrlComponents.nScheme == INTERNET_SCHEME_HTTPS, TRUE);
1826 heap_free(proxy);
1827 if(!new_server)
1828 return FALSE;
1830 request->proxy = new_server;
1832 TRACE("proxy server=%s port=%d\n", debugstr_w(new_server->name), new_server->port);
1833 return TRUE;
1836 static DWORD HTTP_ResolveName(http_request_t *request)
1838 server_t *server = request->proxy ? request->proxy : request->server;
1839 int addr_len;
1841 if(server->addr_len)
1842 return ERROR_SUCCESS;
1844 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
1845 INTERNET_STATUS_RESOLVING_NAME,
1846 server->name,
1847 (strlenW(server->name)+1) * sizeof(WCHAR));
1849 addr_len = sizeof(server->addr);
1850 if (!GetAddress(server->name, server->port, (SOCKADDR*)&server->addr, &addr_len, server->addr_str))
1851 return ERROR_INTERNET_NAME_NOT_RESOLVED;
1853 server->addr_len = addr_len;
1854 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
1855 INTERNET_STATUS_NAME_RESOLVED,
1856 server->addr_str, strlen(server->addr_str)+1);
1858 TRACE("resolved %s to %s\n", debugstr_w(server->name), server->addr_str);
1859 return ERROR_SUCCESS;
1862 static WCHAR *compose_request_url(http_request_t *req)
1864 static const WCHAR http[] = { 'h','t','t','p',':','/','/',0 };
1865 static const WCHAR https[] = { 'h','t','t','p','s',':','/','/',0 };
1866 const WCHAR *host, *scheme;
1867 WCHAR *buf, *ptr;
1868 size_t len;
1870 host = req->server->canon_host_port;
1872 if (req->server->is_https)
1873 scheme = https;
1874 else
1875 scheme = http;
1877 len = strlenW(scheme) + strlenW(host) + (req->path[0] != '/' ? 1 : 0) + strlenW(req->path);
1878 ptr = buf = heap_alloc((len+1) * sizeof(WCHAR));
1879 if(buf) {
1880 strcpyW(ptr, scheme);
1881 ptr += strlenW(ptr);
1883 strcpyW(ptr, host);
1884 ptr += strlenW(ptr);
1886 if(req->path[0] != '/')
1887 *ptr++ = '/';
1889 strcpyW(ptr, req->path);
1890 ptr += strlenW(ptr);
1891 *ptr = 0;
1894 return buf;
1898 /***********************************************************************
1899 * HTTPREQ_Destroy (internal)
1901 * Deallocate request handle
1904 static void HTTPREQ_Destroy(object_header_t *hdr)
1906 http_request_t *request = (http_request_t*) hdr;
1907 DWORD i;
1909 TRACE("\n");
1911 if(request->hCacheFile)
1912 CloseHandle(request->hCacheFile);
1913 if(request->req_file)
1914 req_file_release(request->req_file);
1916 request->headers_section.DebugInfo->Spare[0] = 0;
1917 DeleteCriticalSection( &request->headers_section );
1918 request->read_section.DebugInfo->Spare[0] = 0;
1919 DeleteCriticalSection( &request->read_section );
1920 WININET_Release(&request->session->hdr);
1922 destroy_authinfo(request->authInfo);
1923 destroy_authinfo(request->proxyAuthInfo);
1925 if(request->server)
1926 server_release(request->server);
1927 if(request->proxy)
1928 server_release(request->proxy);
1930 heap_free(request->path);
1931 heap_free(request->verb);
1932 heap_free(request->version);
1933 heap_free(request->statusText);
1935 for (i = 0; i < request->nCustHeaders; i++)
1937 heap_free(request->custHeaders[i].lpszField);
1938 heap_free(request->custHeaders[i].lpszValue);
1940 destroy_data_stream(request->data_stream);
1941 heap_free(request->custHeaders);
1944 static void http_release_netconn(http_request_t *req, BOOL reuse)
1946 TRACE("%p %p %x\n",req, req->netconn, reuse);
1948 if(!is_valid_netconn(req->netconn))
1949 return;
1951 if(reuse && req->netconn->keep_alive) {
1952 BOOL run_collector;
1954 EnterCriticalSection(&connection_pool_cs);
1956 list_add_head(&req->netconn->server->conn_pool, &req->netconn->pool_entry);
1957 req->netconn->keep_until = GetTickCount64() + COLLECT_TIME;
1958 req->netconn = NULL;
1960 run_collector = !collector_running;
1961 collector_running = TRUE;
1963 LeaveCriticalSection(&connection_pool_cs);
1965 if(run_collector) {
1966 HANDLE thread = NULL;
1967 HMODULE module;
1969 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (const WCHAR*)WININET_hModule, &module);
1970 if(module)
1971 thread = CreateThread(NULL, 0, collect_connections_proc, NULL, 0, NULL);
1972 if(!thread) {
1973 EnterCriticalSection(&connection_pool_cs);
1974 collector_running = FALSE;
1975 LeaveCriticalSection(&connection_pool_cs);
1977 if(module)
1978 FreeLibrary(module);
1980 else
1981 CloseHandle(thread);
1983 return;
1986 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
1987 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
1989 close_netconn(req->netconn);
1991 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
1992 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
1995 static BOOL HTTP_KeepAlive(http_request_t *request)
1997 WCHAR szVersion[10];
1998 WCHAR szConnectionResponse[20];
1999 DWORD dwBufferSize = sizeof(szVersion);
2000 BOOL keepalive = FALSE;
2002 /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
2003 * the connection is keep-alive by default */
2004 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_VERSION, szVersion, &dwBufferSize, NULL) == ERROR_SUCCESS
2005 && !strcmpiW(szVersion, g_szHttp1_1))
2007 keepalive = TRUE;
2010 dwBufferSize = sizeof(szConnectionResponse);
2011 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_PROXY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) == ERROR_SUCCESS
2012 || HTTP_HttpQueryInfoW(request, HTTP_QUERY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) == ERROR_SUCCESS)
2014 keepalive = !strcmpiW(szConnectionResponse, szKeepAlive);
2017 return keepalive;
2020 static void HTTPREQ_CloseConnection(object_header_t *hdr)
2022 http_request_t *req = (http_request_t*)hdr;
2024 http_release_netconn(req, drain_content(req, FALSE));
2027 static DWORD str_to_buffer(const WCHAR *str, void *buffer, DWORD *size, BOOL unicode)
2029 int len;
2030 if (unicode)
2032 WCHAR *buf = buffer;
2034 if (str) len = strlenW(str);
2035 else len = 0;
2036 if (*size < (len + 1) * sizeof(WCHAR))
2038 *size = (len + 1) * sizeof(WCHAR);
2039 return ERROR_INSUFFICIENT_BUFFER;
2041 if (str) strcpyW(buf, str);
2042 else buf[0] = 0;
2044 *size = len;
2045 return ERROR_SUCCESS;
2047 else
2049 char *buf = buffer;
2051 if (str) len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
2052 else len = 1;
2053 if (*size < len)
2055 *size = len;
2056 return ERROR_INSUFFICIENT_BUFFER;
2058 if (str) WideCharToMultiByte(CP_ACP, 0, str, -1, buf, *size, NULL, NULL);
2059 else buf[0] = 0;
2061 *size = len - 1;
2062 return ERROR_SUCCESS;
2066 static DWORD HTTPREQ_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2068 http_request_t *req = (http_request_t*)hdr;
2070 switch(option) {
2071 case INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO:
2073 INTERNET_DIAGNOSTIC_SOCKET_INFO *info = buffer;
2075 FIXME("INTERNET_DIAGNOSTIC_SOCKET_INFO stub\n");
2077 if (*size < sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO))
2078 return ERROR_INSUFFICIENT_BUFFER;
2079 *size = sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO);
2080 /* FIXME: can't get a SOCKET from our connection since we don't use
2081 * winsock
2083 info->Socket = 0;
2084 /* FIXME: get source port from req->netConnection */
2085 info->SourcePort = 0;
2086 info->DestPort = req->server->port;
2087 info->Flags = 0;
2088 if (HTTP_KeepAlive(req))
2089 info->Flags |= IDSI_FLAG_KEEP_ALIVE;
2090 if (req->proxy)
2091 info->Flags |= IDSI_FLAG_PROXY;
2092 if (is_valid_netconn(req->netconn) && req->netconn->secure)
2093 info->Flags |= IDSI_FLAG_SECURE;
2095 return ERROR_SUCCESS;
2098 case 98:
2099 TRACE("Queried undocumented option 98, forwarding to INTERNET_OPTION_SECURITY_FLAGS\n");
2100 /* fall through */
2101 case INTERNET_OPTION_SECURITY_FLAGS:
2103 DWORD flags;
2105 if (*size < sizeof(ULONG))
2106 return ERROR_INSUFFICIENT_BUFFER;
2108 *size = sizeof(DWORD);
2109 flags = is_valid_netconn(req->netconn) ? req->netconn->security_flags : req->security_flags | req->server->security_flags;
2110 *(DWORD *)buffer = flags;
2112 TRACE("INTERNET_OPTION_SECURITY_FLAGS %x\n", flags);
2113 return ERROR_SUCCESS;
2116 case INTERNET_OPTION_HANDLE_TYPE:
2117 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
2119 if (*size < sizeof(ULONG))
2120 return ERROR_INSUFFICIENT_BUFFER;
2122 *size = sizeof(DWORD);
2123 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_HTTP_REQUEST;
2124 return ERROR_SUCCESS;
2126 case INTERNET_OPTION_URL: {
2127 WCHAR *url;
2128 DWORD res;
2130 TRACE("INTERNET_OPTION_URL\n");
2132 url = compose_request_url(req);
2133 if(!url)
2134 return ERROR_OUTOFMEMORY;
2136 res = str_to_buffer(url, buffer, size, unicode);
2137 heap_free(url);
2138 return res;
2140 case INTERNET_OPTION_USER_AGENT:
2141 return str_to_buffer(req->session->appInfo->agent, buffer, size, unicode);
2142 case INTERNET_OPTION_USERNAME:
2143 return str_to_buffer(req->session->userName, buffer, size, unicode);
2144 case INTERNET_OPTION_PASSWORD:
2145 return str_to_buffer(req->session->password, buffer, size, unicode);
2146 case INTERNET_OPTION_PROXY_USERNAME:
2147 return str_to_buffer(req->session->appInfo->proxyUsername, buffer, size, unicode);
2148 case INTERNET_OPTION_PROXY_PASSWORD:
2149 return str_to_buffer(req->session->appInfo->proxyPassword, buffer, size, unicode);
2151 case INTERNET_OPTION_CACHE_TIMESTAMPS: {
2152 INTERNET_CACHE_ENTRY_INFOW *info;
2153 INTERNET_CACHE_TIMESTAMPS *ts = buffer;
2154 DWORD nbytes, error;
2155 BOOL ret;
2157 TRACE("INTERNET_OPTION_CACHE_TIMESTAMPS\n");
2159 if(!req->req_file)
2160 return ERROR_FILE_NOT_FOUND;
2162 if (*size < sizeof(*ts))
2164 *size = sizeof(*ts);
2165 return ERROR_INSUFFICIENT_BUFFER;
2168 nbytes = 0;
2169 ret = GetUrlCacheEntryInfoW(req->req_file->url, NULL, &nbytes);
2170 error = GetLastError();
2171 if (!ret && error == ERROR_INSUFFICIENT_BUFFER)
2173 if (!(info = heap_alloc(nbytes)))
2174 return ERROR_OUTOFMEMORY;
2176 GetUrlCacheEntryInfoW(req->req_file->url, info, &nbytes);
2178 ts->ftExpires = info->ExpireTime;
2179 ts->ftLastModified = info->LastModifiedTime;
2181 heap_free(info);
2182 *size = sizeof(*ts);
2183 return ERROR_SUCCESS;
2185 return error;
2188 case INTERNET_OPTION_DATAFILE_NAME: {
2189 DWORD req_size;
2191 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
2193 if(!req->req_file) {
2194 *size = 0;
2195 return ERROR_INTERNET_ITEM_NOT_FOUND;
2198 if(unicode) {
2199 req_size = (lstrlenW(req->req_file->file_name)+1) * sizeof(WCHAR);
2200 if(*size < req_size)
2201 return ERROR_INSUFFICIENT_BUFFER;
2203 *size = req_size;
2204 memcpy(buffer, req->req_file->file_name, *size);
2205 return ERROR_SUCCESS;
2206 }else {
2207 req_size = WideCharToMultiByte(CP_ACP, 0, req->req_file->file_name, -1, NULL, 0, NULL, NULL);
2208 if (req_size > *size)
2209 return ERROR_INSUFFICIENT_BUFFER;
2211 *size = WideCharToMultiByte(CP_ACP, 0, req->req_file->file_name,
2212 -1, buffer, *size, NULL, NULL);
2213 return ERROR_SUCCESS;
2217 case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT: {
2218 PCCERT_CONTEXT context;
2220 if(!req->netconn)
2221 return ERROR_INTERNET_INVALID_OPERATION;
2223 if(*size < sizeof(INTERNET_CERTIFICATE_INFOA)) {
2224 *size = sizeof(INTERNET_CERTIFICATE_INFOA);
2225 return ERROR_INSUFFICIENT_BUFFER;
2228 context = (PCCERT_CONTEXT)NETCON_GetCert(req->netconn);
2229 if(context) {
2230 INTERNET_CERTIFICATE_INFOA *info = (INTERNET_CERTIFICATE_INFOA*)buffer;
2231 DWORD len;
2233 memset(info, 0, sizeof(*info));
2234 info->ftExpiry = context->pCertInfo->NotAfter;
2235 info->ftStart = context->pCertInfo->NotBefore;
2236 len = CertNameToStrA(context->dwCertEncodingType,
2237 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG, NULL, 0);
2238 info->lpszSubjectInfo = LocalAlloc(0, len);
2239 if(info->lpszSubjectInfo)
2240 CertNameToStrA(context->dwCertEncodingType,
2241 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG,
2242 info->lpszSubjectInfo, len);
2243 len = CertNameToStrA(context->dwCertEncodingType,
2244 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG, NULL, 0);
2245 info->lpszIssuerInfo = LocalAlloc(0, len);
2246 if(info->lpszIssuerInfo)
2247 CertNameToStrA(context->dwCertEncodingType,
2248 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG,
2249 info->lpszIssuerInfo, len);
2250 info->dwKeySize = NETCON_GetCipherStrength(req->netconn);
2251 CertFreeCertificateContext(context);
2252 return ERROR_SUCCESS;
2254 return ERROR_NOT_SUPPORTED;
2256 case INTERNET_OPTION_CONNECT_TIMEOUT:
2257 if (*size < sizeof(DWORD))
2258 return ERROR_INSUFFICIENT_BUFFER;
2260 *size = sizeof(DWORD);
2261 *(DWORD *)buffer = req->connect_timeout;
2262 return ERROR_SUCCESS;
2263 case INTERNET_OPTION_REQUEST_FLAGS: {
2264 DWORD flags = 0;
2266 if (*size < sizeof(DWORD))
2267 return ERROR_INSUFFICIENT_BUFFER;
2269 /* FIXME: Add support for:
2270 * INTERNET_REQFLAG_FROM_CACHE
2271 * INTERNET_REQFLAG_CACHE_WRITE_DISABLED
2274 if(req->proxy)
2275 flags |= INTERNET_REQFLAG_VIA_PROXY;
2276 if(!req->status_code)
2277 flags |= INTERNET_REQFLAG_NO_HEADERS;
2279 TRACE("INTERNET_OPTION_REQUEST_FLAGS returning %x\n", flags);
2281 *size = sizeof(DWORD);
2282 *(DWORD*)buffer = flags;
2283 return ERROR_SUCCESS;
2287 return INET_QueryOption(hdr, option, buffer, size, unicode);
2290 static DWORD HTTPREQ_SetOption(object_header_t *hdr, DWORD option, void *buffer, DWORD size)
2292 http_request_t *req = (http_request_t*)hdr;
2294 switch(option) {
2295 case 99: /* Undocumented, seems to be INTERNET_OPTION_SECURITY_FLAGS with argument validation */
2296 TRACE("Undocumented option 99\n");
2298 if (!buffer || size != sizeof(DWORD))
2299 return ERROR_INVALID_PARAMETER;
2300 if(*(DWORD*)buffer & ~SECURITY_SET_MASK)
2301 return ERROR_INTERNET_OPTION_NOT_SETTABLE;
2303 /* fall through */
2304 case INTERNET_OPTION_SECURITY_FLAGS:
2306 DWORD flags;
2308 if (!buffer || size != sizeof(DWORD))
2309 return ERROR_INVALID_PARAMETER;
2310 flags = *(DWORD *)buffer;
2311 TRACE("INTERNET_OPTION_SECURITY_FLAGS %08x\n", flags);
2312 flags &= SECURITY_SET_MASK;
2313 req->security_flags |= flags;
2314 if(is_valid_netconn(req->netconn))
2315 req->netconn->security_flags |= flags;
2316 return ERROR_SUCCESS;
2318 case INTERNET_OPTION_CONNECT_TIMEOUT:
2319 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2320 req->connect_timeout = *(DWORD *)buffer;
2321 return ERROR_SUCCESS;
2323 case INTERNET_OPTION_SEND_TIMEOUT:
2324 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2325 req->send_timeout = *(DWORD *)buffer;
2326 return ERROR_SUCCESS;
2328 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2329 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2330 req->receive_timeout = *(DWORD *)buffer;
2331 return ERROR_SUCCESS;
2333 case INTERNET_OPTION_USERNAME:
2334 heap_free(req->session->userName);
2335 if (!(req->session->userName = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2336 return ERROR_SUCCESS;
2338 case INTERNET_OPTION_PASSWORD:
2339 heap_free(req->session->password);
2340 if (!(req->session->password = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2341 return ERROR_SUCCESS;
2343 case INTERNET_OPTION_PROXY_USERNAME:
2344 heap_free(req->session->appInfo->proxyUsername);
2345 if (!(req->session->appInfo->proxyUsername = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2346 return ERROR_SUCCESS;
2348 case INTERNET_OPTION_PROXY_PASSWORD:
2349 heap_free(req->session->appInfo->proxyPassword);
2350 if (!(req->session->appInfo->proxyPassword = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2351 return ERROR_SUCCESS;
2353 case INTERNET_OPTION_HTTP_DECODING:
2354 if(size != sizeof(BOOL))
2355 return ERROR_INVALID_PARAMETER;
2356 req->decoding = *(BOOL*)buffer;
2357 return ERROR_SUCCESS;
2360 return INET_SetOption(hdr, option, buffer, size);
2363 static void commit_cache_entry(http_request_t *req)
2365 WCHAR *header;
2366 DWORD header_len;
2367 BOOL res;
2369 TRACE("%p\n", req);
2371 CloseHandle(req->hCacheFile);
2372 req->hCacheFile = NULL;
2374 header = build_response_header(req, TRUE);
2375 header_len = (header ? strlenW(header) : 0);
2376 res = CommitUrlCacheEntryW(req->req_file->url, req->req_file->file_name, req->expires,
2377 req->last_modified, NORMAL_CACHE_ENTRY,
2378 header, header_len, NULL, 0);
2379 if(res)
2380 req->req_file->is_committed = TRUE;
2381 else
2382 WARN("CommitUrlCacheEntry failed: %u\n", GetLastError());
2383 heap_free(header);
2386 static void create_cache_entry(http_request_t *req)
2388 static const WCHAR no_cacheW[] = {'n','o','-','c','a','c','h','e',0};
2389 static const WCHAR no_storeW[] = {'n','o','-','s','t','o','r','e',0};
2391 WCHAR file_name[MAX_PATH+1];
2392 WCHAR *url;
2393 BOOL b = TRUE;
2395 /* FIXME: We should free previous cache file earlier */
2396 if(req->req_file) {
2397 req_file_release(req->req_file);
2398 req->req_file = NULL;
2400 if(req->hCacheFile) {
2401 CloseHandle(req->hCacheFile);
2402 req->hCacheFile = NULL;
2405 if(req->hdr.dwFlags & INTERNET_FLAG_NO_CACHE_WRITE)
2406 b = FALSE;
2408 if(b) {
2409 int header_idx;
2411 EnterCriticalSection( &req->headers_section );
2413 header_idx = HTTP_GetCustomHeaderIndex(req, szCache_Control, 0, FALSE);
2414 if(header_idx != -1) {
2415 WCHAR *ptr;
2417 for(ptr=req->custHeaders[header_idx].lpszValue; *ptr; ) {
2418 WCHAR *end;
2420 while(*ptr==' ' || *ptr=='\t')
2421 ptr++;
2423 end = strchrW(ptr, ',');
2424 if(!end)
2425 end = ptr + strlenW(ptr);
2427 if(!strncmpiW(ptr, no_cacheW, sizeof(no_cacheW)/sizeof(*no_cacheW)-1)
2428 || !strncmpiW(ptr, no_storeW, sizeof(no_storeW)/sizeof(*no_storeW)-1)) {
2429 b = FALSE;
2430 break;
2433 ptr = end;
2434 if(*ptr == ',')
2435 ptr++;
2439 LeaveCriticalSection( &req->headers_section );
2442 if(!b) {
2443 if(!(req->hdr.dwFlags & INTERNET_FLAG_NEED_FILE))
2444 return;
2446 FIXME("INTERNET_FLAG_NEED_FILE is not supported correctly\n");
2449 url = compose_request_url(req);
2450 if(!url) {
2451 WARN("Could not get URL\n");
2452 return;
2455 b = CreateUrlCacheEntryW(url, req->contentLength == ~0u ? 0 : req->contentLength, NULL, file_name, 0);
2456 if(!b) {
2457 WARN("Could not create cache entry: %08x\n", GetLastError());
2458 return;
2461 create_req_file(file_name, &req->req_file);
2462 req->req_file->url = url;
2464 req->hCacheFile = CreateFileW(file_name, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
2465 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2466 if(req->hCacheFile == INVALID_HANDLE_VALUE) {
2467 WARN("Could not create file: %u\n", GetLastError());
2468 req->hCacheFile = NULL;
2469 return;
2472 if(req->read_size) {
2473 DWORD written;
2475 b = WriteFile(req->hCacheFile, req->read_buf+req->read_pos, req->read_size, &written, NULL);
2476 if(!b)
2477 FIXME("WriteFile failed: %u\n", GetLastError());
2479 if(req->data_stream->vtbl->end_of_data(req->data_stream, req))
2480 commit_cache_entry(req);
2484 /* read some more data into the read buffer (the read section must be held) */
2485 static DWORD read_more_data( http_request_t *req, int maxlen )
2487 DWORD res;
2488 int len;
2490 if (req->read_pos)
2492 /* move existing data to the start of the buffer */
2493 if(req->read_size)
2494 memmove( req->read_buf, req->read_buf + req->read_pos, req->read_size );
2495 req->read_pos = 0;
2498 if (maxlen == -1) maxlen = sizeof(req->read_buf);
2500 res = NETCON_recv( req->netconn, req->read_buf + req->read_size,
2501 maxlen - req->read_size, TRUE, &len );
2502 if(res == ERROR_SUCCESS)
2503 req->read_size += len;
2505 return res;
2508 /* remove some amount of data from the read buffer (the read section must be held) */
2509 static void remove_data( http_request_t *req, int count )
2511 if (!(req->read_size -= count)) req->read_pos = 0;
2512 else req->read_pos += count;
2515 static DWORD read_line( http_request_t *req, LPSTR buffer, DWORD *len )
2517 int count, bytes_read, pos = 0;
2518 DWORD res;
2520 EnterCriticalSection( &req->read_section );
2521 for (;;)
2523 BYTE *eol = memchr( req->read_buf + req->read_pos, '\n', req->read_size );
2525 if (eol)
2527 count = eol - (req->read_buf + req->read_pos);
2528 bytes_read = count + 1;
2530 else count = bytes_read = req->read_size;
2532 count = min( count, *len - pos );
2533 memcpy( buffer + pos, req->read_buf + req->read_pos, count );
2534 pos += count;
2535 remove_data( req, bytes_read );
2536 if (eol) break;
2538 if ((res = read_more_data( req, -1 )))
2540 WARN( "read failed %u\n", res );
2541 LeaveCriticalSection( &req->read_section );
2542 return res;
2544 if (!req->read_size)
2546 *len = 0;
2547 TRACE( "returning empty string\n" );
2548 LeaveCriticalSection( &req->read_section );
2549 return ERROR_SUCCESS;
2552 LeaveCriticalSection( &req->read_section );
2554 if (pos < *len)
2556 if (pos && buffer[pos - 1] == '\r') pos--;
2557 *len = pos + 1;
2559 buffer[*len - 1] = 0;
2560 TRACE( "returning %s\n", debugstr_a(buffer));
2561 return ERROR_SUCCESS;
2564 /* check if we have reached the end of the data to read (the read section must be held) */
2565 static BOOL end_of_read_data( http_request_t *req )
2567 return !req->read_size && req->data_stream->vtbl->end_of_data(req->data_stream, req);
2570 static DWORD read_http_stream(http_request_t *req, BYTE *buf, DWORD size, DWORD *read, blocking_mode_t blocking_mode)
2572 DWORD res;
2574 res = req->data_stream->vtbl->read(req->data_stream, req, buf, size, read, blocking_mode);
2575 if(res != ERROR_SUCCESS) {
2576 if(res != WSAEWOULDBLOCK)
2577 return res;
2578 *read = 0;
2579 return ERROR_SUCCESS;
2581 assert(*read <= size);
2583 if(req->hCacheFile) {
2584 if(*read) {
2585 BOOL bres;
2586 DWORD written;
2588 bres = WriteFile(req->hCacheFile, buf, *read, &written, NULL);
2589 if(!bres)
2590 FIXME("WriteFile failed: %u\n", GetLastError());
2593 if(!*read || req->data_stream->vtbl->end_of_data(req->data_stream, req))
2594 commit_cache_entry(req);
2597 return res;
2600 /* fetch some more data into the read buffer (the read section must be held) */
2601 static DWORD refill_read_buffer(http_request_t *req, blocking_mode_t blocking_mode, DWORD *read_bytes)
2603 DWORD res, read=0;
2605 if(req->read_size == sizeof(req->read_buf))
2606 return ERROR_SUCCESS;
2608 if(req->read_pos) {
2609 if(req->read_size)
2610 memmove(req->read_buf, req->read_buf+req->read_pos, req->read_size);
2611 req->read_pos = 0;
2614 res = read_http_stream(req, req->read_buf+req->read_size, sizeof(req->read_buf) - req->read_size,
2615 &read, blocking_mode);
2616 req->read_size += read;
2618 TRACE("read %u bytes, read_size %u\n", read, req->read_size);
2619 if(read_bytes)
2620 *read_bytes = read;
2621 return res;
2624 /* return the size of data available to be read immediately (the read section must be held) */
2625 static DWORD get_avail_data( http_request_t *req )
2627 DWORD avail = req->read_size;
2630 * Different Windows versions have different limits of returned data, but all
2631 * of them return no more than centrain amount. We use READ_BUFFER_SIZE as a limit.
2633 if(avail < READ_BUFFER_SIZE)
2634 avail += req->data_stream->vtbl->get_avail_data(req->data_stream, req);
2636 return min(avail, READ_BUFFER_SIZE);
2639 static DWORD netconn_get_avail_data(data_stream_t *stream, http_request_t *req)
2641 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2642 DWORD avail = 0;
2644 if(is_valid_netconn(req->netconn))
2645 NETCON_query_data_available(req->netconn, &avail);
2646 return netconn_stream->content_length == ~0u
2647 ? avail
2648 : min(avail, netconn_stream->content_length-netconn_stream->content_read);
2651 static BOOL netconn_end_of_data(data_stream_t *stream, http_request_t *req)
2653 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2654 return netconn_stream->content_read == netconn_stream->content_length || !is_valid_netconn(req->netconn);
2657 static DWORD netconn_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
2658 DWORD *read, blocking_mode_t blocking_mode)
2660 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2661 DWORD res = ERROR_SUCCESS;
2662 int len = 0, ret = 0;
2664 size = min(size, netconn_stream->content_length-netconn_stream->content_read);
2666 if(size && is_valid_netconn(req->netconn)) {
2667 while((res = NETCON_recv(req->netconn, buf+ret, size-ret, blocking_mode != BLOCKING_DISALLOW, &len)) == ERROR_SUCCESS) {
2668 if(!len) {
2669 netconn_stream->content_length = netconn_stream->content_read;
2670 break;
2672 ret += len;
2673 netconn_stream->content_read += len;
2674 if(blocking_mode != BLOCKING_WAITALL || size == ret)
2675 break;
2678 if(res == WSAEWOULDBLOCK && ret)
2679 res = ERROR_SUCCESS;
2682 TRACE("read %u bytes\n", ret);
2683 *read = ret;
2684 return res;
2687 static BOOL netconn_drain_content(data_stream_t *stream, http_request_t *req)
2689 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2690 BYTE buf[1024];
2691 int len, res;
2692 size_t size;
2694 if(netconn_stream->content_length == ~0u)
2695 return FALSE;
2697 while(netconn_stream->content_read < netconn_stream->content_length) {
2698 size = min(sizeof(buf), netconn_stream->content_length-netconn_stream->content_read);
2699 res = NETCON_recv(req->netconn, buf, size, FALSE, &len);
2700 if(res || !len)
2701 return FALSE;
2703 netconn_stream->content_read += len;
2706 return TRUE;
2709 static void netconn_destroy(data_stream_t *stream)
2713 static const data_stream_vtbl_t netconn_stream_vtbl = {
2714 netconn_get_avail_data,
2715 netconn_end_of_data,
2716 netconn_read,
2717 netconn_drain_content,
2718 netconn_destroy
2721 static char next_chunked_data_char(chunked_stream_t *stream)
2723 assert(stream->buf_size);
2725 stream->buf_size--;
2726 return stream->buf[stream->buf_pos++];
2729 static BOOL chunked_end_of_data(data_stream_t *stream, http_request_t *req)
2731 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2732 return chunked_stream->state == CHUNKED_STREAM_STATE_END_OF_STREAM;
2735 static DWORD chunked_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
2736 DWORD *read, blocking_mode_t blocking_mode)
2738 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2739 DWORD ret_read = 0, res = ERROR_SUCCESS;
2740 BOOL continue_read = TRUE;
2741 int read_bytes;
2742 char ch;
2744 do {
2745 TRACE("state %d\n", chunked_stream->state);
2747 /* Ensure that we have data in the buffer for states that need it. */
2748 if(!chunked_stream->buf_size) {
2749 switch(chunked_stream->state) {
2750 case CHUNKED_STREAM_STATE_READING_CHUNK_SIZE:
2751 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE:
2752 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA:
2753 case CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END:
2754 chunked_stream->buf_pos = 0;
2755 res = NETCON_recv(req->netconn, chunked_stream->buf, sizeof(chunked_stream->buf), blocking_mode != BLOCKING_DISALLOW, &read_bytes);
2756 if(res == ERROR_SUCCESS && read_bytes) {
2757 chunked_stream->buf_size += read_bytes;
2758 }else if(res == WSAEWOULDBLOCK) {
2759 if(ret_read)
2760 res = ERROR_SUCCESS;
2761 continue_read = FALSE;
2762 continue;
2763 }else {
2764 chunked_stream->state = CHUNKED_STREAM_STATE_END_OF_STREAM;
2766 break;
2767 default:
2768 break;
2772 switch(chunked_stream->state) {
2773 case CHUNKED_STREAM_STATE_READING_CHUNK_SIZE:
2774 ch = next_chunked_data_char(chunked_stream);
2776 if(ch >= '0' && ch <= '9') {
2777 chunked_stream->chunk_size = chunked_stream->chunk_size * 16 + ch - '0';
2778 }else if(ch >= 'a' && ch <= 'f') {
2779 chunked_stream->chunk_size = chunked_stream->chunk_size * 16 + ch - 'a' + 10;
2780 }else if (ch >= 'A' && ch <= 'F') {
2781 chunked_stream->chunk_size = chunked_stream->chunk_size * 16 + ch - 'A' + 10;
2782 }else if (ch == ';' || ch == '\r' || ch == '\n') {
2783 TRACE("reading %u byte chunk\n", chunked_stream->chunk_size);
2784 chunked_stream->buf_size++;
2785 chunked_stream->buf_pos--;
2786 if(req->contentLength == ~0u) req->contentLength = chunked_stream->chunk_size;
2787 else req->contentLength += chunked_stream->chunk_size;
2788 chunked_stream->state = CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE;
2790 break;
2792 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE:
2793 ch = next_chunked_data_char(chunked_stream);
2794 if(ch == '\n')
2795 chunked_stream->state = chunked_stream->chunk_size
2796 ? CHUNKED_STREAM_STATE_READING_CHUNK
2797 : CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END;
2798 else if(ch != '\r')
2799 WARN("unexpected char '%c'\n", ch);
2800 break;
2802 case CHUNKED_STREAM_STATE_READING_CHUNK:
2803 assert(chunked_stream->chunk_size);
2804 if(!size) {
2805 continue_read = FALSE;
2806 break;
2808 read_bytes = min(size, chunked_stream->chunk_size);
2810 if(chunked_stream->buf_size) {
2811 if(read_bytes > chunked_stream->buf_size)
2812 read_bytes = chunked_stream->buf_size;
2814 memcpy(buf+ret_read, chunked_stream->buf+chunked_stream->buf_pos, read_bytes);
2815 chunked_stream->buf_pos += read_bytes;
2816 chunked_stream->buf_size -= read_bytes;
2817 }else {
2818 res = NETCON_recv(req->netconn, (char*)buf+ret_read, read_bytes,
2819 blocking_mode != BLOCKING_DISALLOW, (int*)&read_bytes);
2820 if(res != ERROR_SUCCESS) {
2821 continue_read = FALSE;
2822 break;
2825 if(!read_bytes) {
2826 chunked_stream->state = CHUNKED_STREAM_STATE_END_OF_STREAM;
2827 continue;
2831 chunked_stream->chunk_size -= read_bytes;
2832 size -= read_bytes;
2833 ret_read += read_bytes;
2834 if(!chunked_stream->chunk_size)
2835 chunked_stream->state = CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA;
2836 if(blocking_mode == BLOCKING_ALLOW)
2837 blocking_mode = BLOCKING_DISALLOW;
2838 break;
2840 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA:
2841 ch = next_chunked_data_char(chunked_stream);
2842 if(ch == '\n')
2843 chunked_stream->state = CHUNKED_STREAM_STATE_READING_CHUNK_SIZE;
2844 else if(ch != '\r')
2845 WARN("unexpected char '%c'\n", ch);
2846 break;
2848 case CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END:
2849 ch = next_chunked_data_char(chunked_stream);
2850 if(ch == '\n')
2851 chunked_stream->state = CHUNKED_STREAM_STATE_END_OF_STREAM;
2852 else if(ch != '\r')
2853 WARN("unexpected char '%c'\n", ch);
2854 break;
2856 case CHUNKED_STREAM_STATE_END_OF_STREAM:
2857 continue_read = FALSE;
2858 break;
2860 } while(continue_read);
2862 if(ret_read)
2863 res = ERROR_SUCCESS;
2864 if(res != ERROR_SUCCESS)
2865 return res;
2867 TRACE("read %d bytes\n", ret_read);
2868 *read = ret_read;
2869 return ERROR_SUCCESS;
2872 static DWORD chunked_get_avail_data(data_stream_t *stream, http_request_t *req)
2874 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2875 DWORD avail = 0;
2877 if(chunked_stream->state != CHUNKED_STREAM_STATE_READING_CHUNK) {
2878 DWORD res, read;
2880 /* try to process to the next chunk */
2881 res = chunked_read(stream, req, NULL, 0, &read, BLOCKING_DISALLOW);
2882 if(res != ERROR_SUCCESS || chunked_stream->state != CHUNKED_STREAM_STATE_READING_CHUNK)
2883 return 0;
2886 if(is_valid_netconn(req->netconn) && chunked_stream->buf_size < chunked_stream->chunk_size)
2887 NETCON_query_data_available(req->netconn, &avail);
2889 return min(avail + chunked_stream->buf_size, chunked_stream->chunk_size);
2892 static BOOL chunked_drain_content(data_stream_t *stream, http_request_t *req)
2894 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2895 return chunked_stream->state == CHUNKED_STREAM_STATE_END_OF_STREAM;
2898 static void chunked_destroy(data_stream_t *stream)
2900 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2901 heap_free(chunked_stream);
2904 static const data_stream_vtbl_t chunked_stream_vtbl = {
2905 chunked_get_avail_data,
2906 chunked_end_of_data,
2907 chunked_read,
2908 chunked_drain_content,
2909 chunked_destroy
2912 /* set the request content length based on the headers */
2913 static DWORD set_content_length(http_request_t *request)
2915 static const WCHAR szChunked[] = {'c','h','u','n','k','e','d',0};
2916 static const WCHAR headW[] = {'H','E','A','D',0};
2917 WCHAR encoding[20];
2918 DWORD size;
2920 if(request->status_code == HTTP_STATUS_NO_CONTENT || !strcmpW(request->verb, headW)) {
2921 request->contentLength = request->netconn_stream.content_length = 0;
2922 return ERROR_SUCCESS;
2925 size = sizeof(request->contentLength);
2926 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
2927 &request->contentLength, &size, NULL) != ERROR_SUCCESS)
2928 request->contentLength = ~0u;
2929 request->netconn_stream.content_length = request->contentLength;
2930 request->netconn_stream.content_read = request->read_size;
2932 size = sizeof(encoding);
2933 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_TRANSFER_ENCODING, encoding, &size, NULL) == ERROR_SUCCESS &&
2934 !strcmpiW(encoding, szChunked))
2936 chunked_stream_t *chunked_stream;
2938 chunked_stream = heap_alloc(sizeof(*chunked_stream));
2939 if(!chunked_stream)
2940 return ERROR_OUTOFMEMORY;
2942 chunked_stream->data_stream.vtbl = &chunked_stream_vtbl;
2943 chunked_stream->buf_size = chunked_stream->buf_pos = 0;
2944 chunked_stream->chunk_size = 0;
2945 chunked_stream->state = CHUNKED_STREAM_STATE_READING_CHUNK_SIZE;
2947 if(request->read_size) {
2948 memcpy(chunked_stream->buf, request->read_buf+request->read_pos, request->read_size);
2949 chunked_stream->buf_size = request->read_size;
2950 request->read_size = request->read_pos = 0;
2953 request->data_stream = &chunked_stream->data_stream;
2954 request->contentLength = ~0u;
2957 if(request->decoding) {
2958 int encoding_idx;
2960 static const WCHAR deflateW[] = {'d','e','f','l','a','t','e',0};
2961 static const WCHAR gzipW[] = {'g','z','i','p',0};
2963 EnterCriticalSection( &request->headers_section );
2965 encoding_idx = HTTP_GetCustomHeaderIndex(request, szContent_Encoding, 0, FALSE);
2966 if(encoding_idx != -1) {
2967 if(!strcmpiW(request->custHeaders[encoding_idx].lpszValue, gzipW)) {
2968 HTTP_DeleteCustomHeader(request, encoding_idx);
2969 LeaveCriticalSection( &request->headers_section );
2970 return init_gzip_stream(request, TRUE);
2972 if(!strcmpiW(request->custHeaders[encoding_idx].lpszValue, deflateW)) {
2973 HTTP_DeleteCustomHeader(request, encoding_idx);
2974 LeaveCriticalSection( &request->headers_section );
2975 return init_gzip_stream(request, FALSE);
2979 LeaveCriticalSection( &request->headers_section );
2982 return ERROR_SUCCESS;
2985 static void send_request_complete(http_request_t *req, DWORD_PTR result, DWORD error)
2987 INTERNET_ASYNC_RESULT iar;
2989 iar.dwResult = result;
2990 iar.dwError = error;
2992 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2993 sizeof(INTERNET_ASYNC_RESULT));
2996 static void HTTP_ReceiveRequestData(http_request_t *req, BOOL first_notif, DWORD *ret_size)
2998 DWORD res, read = 0, avail = 0;
2999 blocking_mode_t mode;
3001 TRACE("%p\n", req);
3003 EnterCriticalSection( &req->read_section );
3005 mode = first_notif && req->read_size ? BLOCKING_DISALLOW : BLOCKING_ALLOW;
3006 res = refill_read_buffer(req, mode, &read);
3007 if(res == ERROR_SUCCESS)
3008 avail = get_avail_data(req);
3010 LeaveCriticalSection( &req->read_section );
3012 if(res != ERROR_SUCCESS || (mode != BLOCKING_DISALLOW && !read)) {
3013 WARN("res %u read %u, closing connection\n", res, read);
3014 http_release_netconn(req, FALSE);
3017 if(res != ERROR_SUCCESS) {
3018 send_request_complete(req, 0, res);
3019 return;
3022 if(ret_size)
3023 *ret_size = avail;
3024 if(first_notif)
3025 avail = 0;
3027 send_request_complete(req, req->session->hdr.dwInternalFlags & INET_OPENURL ? (DWORD_PTR)req->hdr.hInternet : 1, avail);
3030 /* read data from the http connection (the read section must be held) */
3031 static DWORD HTTPREQ_Read(http_request_t *req, void *buffer, DWORD size, DWORD *read, blocking_mode_t blocking_mode)
3033 DWORD current_read = 0, ret_read = 0;
3034 DWORD res = ERROR_SUCCESS;
3036 EnterCriticalSection( &req->read_section );
3038 if(req->read_size) {
3039 ret_read = min(size, req->read_size);
3040 memcpy(buffer, req->read_buf+req->read_pos, ret_read);
3041 req->read_size -= ret_read;
3042 req->read_pos += ret_read;
3043 if(blocking_mode == BLOCKING_ALLOW)
3044 blocking_mode = BLOCKING_DISALLOW;
3047 if(ret_read < size) {
3048 res = read_http_stream(req, (BYTE*)buffer+ret_read, size-ret_read, &current_read, blocking_mode);
3049 ret_read += current_read;
3052 LeaveCriticalSection( &req->read_section );
3054 *read = ret_read;
3055 TRACE( "retrieved %u bytes (%u)\n", ret_read, req->contentLength );
3057 if(size && !ret_read)
3058 http_release_netconn(req, res == ERROR_SUCCESS);
3060 return res;
3063 static BOOL drain_content(http_request_t *req, BOOL blocking)
3065 BOOL ret;
3067 if(!is_valid_netconn(req->netconn) || req->contentLength == -1)
3068 return FALSE;
3070 if(!strcmpW(req->verb, szHEAD))
3071 return TRUE;
3073 if(!blocking)
3074 return req->data_stream->vtbl->drain_content(req->data_stream, req);
3076 EnterCriticalSection( &req->read_section );
3078 while(1) {
3079 DWORD bytes_read, res;
3080 BYTE buf[4096];
3082 res = HTTPREQ_Read(req, buf, sizeof(buf), &bytes_read, BLOCKING_ALLOW);
3083 if(res != ERROR_SUCCESS) {
3084 ret = FALSE;
3085 break;
3087 if(!bytes_read) {
3088 ret = TRUE;
3089 break;
3093 LeaveCriticalSection( &req->read_section );
3094 return ret;
3097 typedef struct {
3098 task_header_t hdr;
3099 void *buf;
3100 DWORD size;
3101 DWORD *ret_read;
3102 } read_file_ex_task_t;
3104 static void AsyncReadFileExProc(task_header_t *hdr)
3106 read_file_ex_task_t *task = (read_file_ex_task_t*)hdr;
3107 http_request_t *req = (http_request_t*)task->hdr.hdr;
3108 DWORD res = ERROR_SUCCESS, read = 0, buffered = 0;
3110 TRACE("%p\n", req);
3112 if(task->ret_read)
3113 res = HTTPREQ_Read(req, task->buf, task->size, &read, BLOCKING_ALLOW);
3114 if(res == ERROR_SUCCESS)
3115 res = refill_read_buffer(req, task->ret_read ? BLOCKING_DISALLOW : BLOCKING_ALLOW, &buffered);
3116 if (res == ERROR_SUCCESS)
3118 if(task->ret_read)
3119 *task->ret_read = read;
3120 read += buffered;
3121 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
3122 &read, sizeof(read));
3125 send_request_complete(req, res == ERROR_SUCCESS, res);
3128 static DWORD HTTPREQ_ReadFileEx(object_header_t *hdr, void *buf, DWORD size, DWORD *ret_read,
3129 DWORD flags, DWORD_PTR context)
3132 http_request_t *req = (http_request_t*)hdr;
3133 DWORD res, read, cread, error = ERROR_SUCCESS;
3135 TRACE("(%p %p %u %x)\n", req, buf, size, flags);
3137 if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
3138 FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
3140 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3142 if (req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3144 read_file_ex_task_t *task;
3146 if (TryEnterCriticalSection( &req->read_section ))
3148 if (get_avail_data(req) || end_of_read_data(req))
3150 res = HTTPREQ_Read(req, buf, size, &read, BLOCKING_DISALLOW);
3151 LeaveCriticalSection( &req->read_section );
3152 goto done;
3154 LeaveCriticalSection( &req->read_section );
3157 task = alloc_async_task(&req->hdr, AsyncReadFileExProc, sizeof(*task));
3158 task->buf = buf;
3159 task->size = size;
3160 task->ret_read = (flags & IRF_NO_WAIT) ? NULL : ret_read;
3162 INTERNET_AsyncCall(&task->hdr);
3164 return ERROR_IO_PENDING;
3167 read = 0;
3169 EnterCriticalSection( &req->read_section );
3170 if(hdr->dwError == ERROR_SUCCESS)
3171 hdr->dwError = INTERNET_HANDLE_IN_USE;
3172 else if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3173 hdr->dwError = ERROR_INTERNET_INTERNAL_ERROR;
3175 while(1) {
3176 res = HTTPREQ_Read(req, (char*)buf+read, size-read, &cread, BLOCKING_ALLOW);
3177 if(res != ERROR_SUCCESS)
3178 break;
3180 read += cread;
3181 if(read == size || end_of_read_data(req))
3182 break;
3184 LeaveCriticalSection( &req->read_section );
3186 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
3187 &cread, sizeof(cread));
3188 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
3189 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3191 EnterCriticalSection( &req->read_section );
3194 if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3195 hdr->dwError = ERROR_SUCCESS;
3196 else
3197 error = hdr->dwError;
3199 LeaveCriticalSection( &req->read_section );
3201 done:
3202 *ret_read = read;
3203 if (res == ERROR_SUCCESS) {
3204 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
3205 &read, sizeof(read));
3208 return res==ERROR_SUCCESS ? error : res;
3211 static DWORD HTTPREQ_WriteFile(object_header_t *hdr, const void *buffer, DWORD size, DWORD *written)
3213 DWORD res;
3214 http_request_t *request = (http_request_t*)hdr;
3216 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
3218 *written = 0;
3219 res = NETCON_send(request->netconn, buffer, size, 0, (LPINT)written);
3220 if (res == ERROR_SUCCESS)
3221 request->bytesWritten += *written;
3223 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REQUEST_SENT, written, sizeof(DWORD));
3224 return res;
3227 static DWORD HTTPREQ_ReadFile(object_header_t *hdr, void *buffer, DWORD size, DWORD *read)
3229 http_request_t *req = (http_request_t*)hdr;
3230 DWORD res;
3232 if (req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3234 read_file_ex_task_t *task;
3236 if (TryEnterCriticalSection( &req->read_section ))
3238 if (get_avail_data(req) || end_of_read_data(req))
3240 res = HTTPREQ_Read(req, buffer, size, read, BLOCKING_DISALLOW);
3241 LeaveCriticalSection( &req->read_section );
3242 return res;
3244 LeaveCriticalSection( &req->read_section );
3247 task = alloc_async_task(&req->hdr, AsyncReadFileExProc, sizeof(*task));
3248 task->buf = buffer;
3249 task->size = size;
3250 task->ret_read = read;
3252 *read = 0;
3253 INTERNET_AsyncCall(&task->hdr);
3255 return ERROR_IO_PENDING;
3258 EnterCriticalSection( &req->read_section );
3259 if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3260 hdr->dwError = ERROR_INTERNET_INTERNAL_ERROR;
3262 res = HTTPREQ_Read(req, buffer, size, read, BLOCKING_WAITALL);
3263 if(res == ERROR_SUCCESS)
3264 res = hdr->dwError;
3265 LeaveCriticalSection( &req->read_section );
3267 return res;
3270 typedef struct {
3271 task_header_t hdr;
3272 DWORD *ret_size;
3273 } http_data_available_task_t;
3275 static void AsyncQueryDataAvailableProc(task_header_t *hdr)
3277 http_data_available_task_t *task = (http_data_available_task_t*)hdr;
3279 HTTP_ReceiveRequestData((http_request_t*)task->hdr.hdr, FALSE, task->ret_size);
3282 static DWORD HTTPREQ_QueryDataAvailable(object_header_t *hdr, DWORD *available, DWORD flags, DWORD_PTR ctx)
3284 http_request_t *req = (http_request_t*)hdr;
3286 TRACE("(%p %p %x %lx)\n", req, available, flags, ctx);
3288 if (req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3290 http_data_available_task_t *task;
3292 /* never wait, if we can't enter the section we queue an async request right away */
3293 if (TryEnterCriticalSection( &req->read_section ))
3295 refill_read_buffer(req, BLOCKING_DISALLOW, NULL);
3296 if ((*available = get_avail_data( req ))) goto done;
3297 if (end_of_read_data( req )) goto done;
3298 LeaveCriticalSection( &req->read_section );
3301 task = alloc_async_task(&req->hdr, AsyncQueryDataAvailableProc, sizeof(*task));
3302 task->ret_size = available;
3303 INTERNET_AsyncCall(&task->hdr);
3304 return ERROR_IO_PENDING;
3307 EnterCriticalSection( &req->read_section );
3309 if (!(*available = get_avail_data( req )) && !end_of_read_data( req ))
3311 refill_read_buffer( req, BLOCKING_ALLOW, NULL );
3312 *available = get_avail_data( req );
3315 done:
3316 LeaveCriticalSection( &req->read_section );
3318 TRACE( "returning %u\n", *available );
3319 return ERROR_SUCCESS;
3322 static DWORD HTTPREQ_LockRequestFile(object_header_t *hdr, req_file_t **ret)
3324 http_request_t *req = (http_request_t*)hdr;
3326 TRACE("(%p)\n", req);
3328 if(!req->req_file) {
3329 WARN("No cache file name available\n");
3330 return ERROR_FILE_NOT_FOUND;
3333 *ret = req_file_addref(req->req_file);
3334 return ERROR_SUCCESS;
3337 static const object_vtbl_t HTTPREQVtbl = {
3338 HTTPREQ_Destroy,
3339 HTTPREQ_CloseConnection,
3340 HTTPREQ_QueryOption,
3341 HTTPREQ_SetOption,
3342 HTTPREQ_ReadFile,
3343 HTTPREQ_ReadFileEx,
3344 HTTPREQ_WriteFile,
3345 HTTPREQ_QueryDataAvailable,
3346 NULL,
3347 HTTPREQ_LockRequestFile
3350 /***********************************************************************
3351 * HTTP_HttpOpenRequestW (internal)
3353 * Open a HTTP request handle
3355 * RETURNS
3356 * HINTERNET a HTTP request handle on success
3357 * NULL on failure
3360 static DWORD HTTP_HttpOpenRequestW(http_session_t *session,
3361 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
3362 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
3363 DWORD dwFlags, DWORD_PTR dwContext, HINTERNET *ret)
3365 appinfo_t *hIC = session->appInfo;
3366 http_request_t *request;
3367 DWORD port, len;
3369 TRACE("-->\n");
3371 request = alloc_object(&session->hdr, &HTTPREQVtbl, sizeof(http_request_t));
3372 if(!request)
3373 return ERROR_OUTOFMEMORY;
3375 request->hdr.htype = WH_HHTTPREQ;
3376 request->hdr.dwFlags = dwFlags;
3377 request->hdr.dwContext = dwContext;
3378 request->contentLength = ~0u;
3380 request->netconn_stream.data_stream.vtbl = &netconn_stream_vtbl;
3381 request->data_stream = &request->netconn_stream.data_stream;
3382 request->connect_timeout = session->connect_timeout;
3383 request->send_timeout = session->send_timeout;
3384 request->receive_timeout = session->receive_timeout;
3386 InitializeCriticalSection( &request->headers_section );
3387 request->headers_section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": http_request_t.headers_section");
3389 InitializeCriticalSection( &request->read_section );
3390 request->read_section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": http_request_t.read_section");
3392 WININET_AddRef( &session->hdr );
3393 request->session = session;
3394 list_add_head( &session->hdr.children, &request->hdr.entry );
3396 port = session->hostPort;
3397 if (port == INTERNET_INVALID_PORT_NUMBER)
3398 port = (session->hdr.dwFlags & INTERNET_FLAG_SECURE) ?
3399 INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT;
3401 request->server = get_server(substrz(session->hostName), port, (dwFlags & INTERNET_FLAG_SECURE) != 0, TRUE);
3402 if(!request->server) {
3403 WININET_Release(&request->hdr);
3404 return ERROR_OUTOFMEMORY;
3407 if (dwFlags & INTERNET_FLAG_IGNORE_CERT_CN_INVALID)
3408 request->security_flags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
3409 if (dwFlags & INTERNET_FLAG_IGNORE_CERT_DATE_INVALID)
3410 request->security_flags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
3412 if (lpszObjectName && *lpszObjectName) {
3413 HRESULT rc;
3414 WCHAR dummy;
3416 len = 1;
3417 rc = UrlCanonicalizeW(lpszObjectName, &dummy, &len, URL_ESCAPE_SPACES_ONLY);
3418 if (rc != E_POINTER)
3419 len = strlenW(lpszObjectName)+1;
3420 request->path = heap_alloc(len*sizeof(WCHAR));
3421 rc = UrlCanonicalizeW(lpszObjectName, request->path, &len,
3422 URL_ESCAPE_SPACES_ONLY);
3423 if (rc != S_OK)
3425 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc);
3426 strcpyW(request->path,lpszObjectName);
3428 }else {
3429 static const WCHAR slashW[] = {'/',0};
3431 request->path = heap_strdupW(slashW);
3434 if (lpszReferrer && *lpszReferrer)
3435 HTTP_ProcessHeader(request, HTTP_REFERER, lpszReferrer, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
3437 if (lpszAcceptTypes)
3439 int i;
3440 for (i = 0; lpszAcceptTypes[i]; i++)
3442 if (!*lpszAcceptTypes[i]) continue;
3443 HTTP_ProcessHeader(request, HTTP_ACCEPT, lpszAcceptTypes[i],
3444 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA |
3445 HTTP_ADDHDR_FLAG_REQ |
3446 (i == 0 ? (HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_ADD) : 0));
3450 request->verb = heap_strdupW(lpszVerb && *lpszVerb ? lpszVerb : szGET);
3451 request->version = heap_strdupW(lpszVersion && *lpszVersion ? lpszVersion : g_szHttp1_1);
3453 if (hIC->proxy && hIC->proxy[0] && !HTTP_ShouldBypassProxy(hIC, session->hostName))
3454 HTTP_DealWithProxy( hIC, session, request );
3456 INTERNET_SendCallback(&session->hdr, dwContext,
3457 INTERNET_STATUS_HANDLE_CREATED, &request->hdr.hInternet,
3458 sizeof(HINTERNET));
3460 TRACE("<-- (%p)\n", request);
3462 *ret = request->hdr.hInternet;
3463 return ERROR_SUCCESS;
3466 /***********************************************************************
3467 * HttpOpenRequestW (WININET.@)
3469 * Open a HTTP request handle
3471 * RETURNS
3472 * HINTERNET a HTTP request handle on success
3473 * NULL on failure
3476 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
3477 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
3478 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
3479 DWORD dwFlags, DWORD_PTR dwContext)
3481 http_session_t *session;
3482 HINTERNET handle = NULL;
3483 DWORD res;
3485 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
3486 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
3487 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
3488 dwFlags, dwContext);
3489 if(lpszAcceptTypes!=NULL)
3491 int i;
3492 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
3493 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
3496 session = (http_session_t*) get_handle_object( hHttpSession );
3497 if (NULL == session || session->hdr.htype != WH_HHTTPSESSION)
3499 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3500 goto lend;
3504 * My tests seem to show that the windows version does not
3505 * become asynchronous until after this point. And anyhow
3506 * if this call was asynchronous then how would you get the
3507 * necessary HINTERNET pointer returned by this function.
3510 res = HTTP_HttpOpenRequestW(session, lpszVerb, lpszObjectName,
3511 lpszVersion, lpszReferrer, lpszAcceptTypes,
3512 dwFlags, dwContext, &handle);
3513 lend:
3514 if( session )
3515 WININET_Release( &session->hdr );
3516 TRACE("returning %p\n", handle);
3517 if(res != ERROR_SUCCESS)
3518 SetLastError(res);
3519 return handle;
3522 static const LPCWSTR header_lookup[] = {
3523 szMime_Version, /* HTTP_QUERY_MIME_VERSION = 0 */
3524 szContent_Type, /* HTTP_QUERY_CONTENT_TYPE = 1 */
3525 szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
3526 szContent_ID, /* HTTP_QUERY_CONTENT_ID = 3 */
3527 NULL, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
3528 szContent_Length, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
3529 szContent_Language, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
3530 szAllow, /* HTTP_QUERY_ALLOW = 7 */
3531 szPublic, /* HTTP_QUERY_PUBLIC = 8 */
3532 szDate, /* HTTP_QUERY_DATE = 9 */
3533 szExpires, /* HTTP_QUERY_EXPIRES = 10 */
3534 szLast_Modified, /* HTTP_QUERY_LAST_MODIFIED = 11 */
3535 NULL, /* HTTP_QUERY_MESSAGE_ID = 12 */
3536 szURI, /* HTTP_QUERY_URI = 13 */
3537 szFrom, /* HTTP_QUERY_DERIVED_FROM = 14 */
3538 NULL, /* HTTP_QUERY_COST = 15 */
3539 NULL, /* HTTP_QUERY_LINK = 16 */
3540 szPragma, /* HTTP_QUERY_PRAGMA = 17 */
3541 NULL, /* HTTP_QUERY_VERSION = 18 */
3542 szStatus, /* HTTP_QUERY_STATUS_CODE = 19 */
3543 NULL, /* HTTP_QUERY_STATUS_TEXT = 20 */
3544 NULL, /* HTTP_QUERY_RAW_HEADERS = 21 */
3545 NULL, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
3546 szConnection, /* HTTP_QUERY_CONNECTION = 23 */
3547 szAccept, /* HTTP_QUERY_ACCEPT = 24 */
3548 szAccept_Charset, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
3549 szAccept_Encoding, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
3550 szAccept_Language, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
3551 szAuthorization, /* HTTP_QUERY_AUTHORIZATION = 28 */
3552 szContent_Encoding, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
3553 NULL, /* HTTP_QUERY_FORWARDED = 30 */
3554 NULL, /* HTTP_QUERY_FROM = 31 */
3555 szIf_Modified_Since, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
3556 szLocation, /* HTTP_QUERY_LOCATION = 33 */
3557 NULL, /* HTTP_QUERY_ORIG_URI = 34 */
3558 szReferer, /* HTTP_QUERY_REFERER = 35 */
3559 szRetry_After, /* HTTP_QUERY_RETRY_AFTER = 36 */
3560 szServer, /* HTTP_QUERY_SERVER = 37 */
3561 NULL, /* HTTP_TITLE = 38 */
3562 szUser_Agent, /* HTTP_QUERY_USER_AGENT = 39 */
3563 szWWW_Authenticate, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
3564 szProxy_Authenticate, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
3565 szAccept_Ranges, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
3566 szSet_Cookie, /* HTTP_QUERY_SET_COOKIE = 43 */
3567 szCookie, /* HTTP_QUERY_COOKIE = 44 */
3568 NULL, /* HTTP_QUERY_REQUEST_METHOD = 45 */
3569 NULL, /* HTTP_QUERY_REFRESH = 46 */
3570 szContent_Disposition, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
3571 szAge, /* HTTP_QUERY_AGE = 48 */
3572 szCache_Control, /* HTTP_QUERY_CACHE_CONTROL = 49 */
3573 szContent_Base, /* HTTP_QUERY_CONTENT_BASE = 50 */
3574 szContent_Location, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
3575 szContent_MD5, /* HTTP_QUERY_CONTENT_MD5 = 52 */
3576 szContent_Range, /* HTTP_QUERY_CONTENT_RANGE = 53 */
3577 szETag, /* HTTP_QUERY_ETAG = 54 */
3578 hostW, /* HTTP_QUERY_HOST = 55 */
3579 szIf_Match, /* HTTP_QUERY_IF_MATCH = 56 */
3580 szIf_None_Match, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
3581 szIf_Range, /* HTTP_QUERY_IF_RANGE = 58 */
3582 szIf_Unmodified_Since, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
3583 szMax_Forwards, /* HTTP_QUERY_MAX_FORWARDS = 60 */
3584 szProxy_Authorization, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
3585 szRange, /* HTTP_QUERY_RANGE = 62 */
3586 szTransfer_Encoding, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
3587 szUpgrade, /* HTTP_QUERY_UPGRADE = 64 */
3588 szVary, /* HTTP_QUERY_VARY = 65 */
3589 szVia, /* HTTP_QUERY_VIA = 66 */
3590 szWarning, /* HTTP_QUERY_WARNING = 67 */
3591 szExpect, /* HTTP_QUERY_EXPECT = 68 */
3592 szProxy_Connection, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
3593 szUnless_Modified_Since, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
3596 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
3598 /***********************************************************************
3599 * HTTP_HttpQueryInfoW (internal)
3601 static DWORD HTTP_HttpQueryInfoW(http_request_t *request, DWORD dwInfoLevel,
3602 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
3604 LPHTTPHEADERW lphttpHdr = NULL;
3605 BOOL request_only = !!(dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS);
3606 INT requested_index = lpdwIndex ? *lpdwIndex : 0;
3607 DWORD level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
3608 INT index = -1;
3610 EnterCriticalSection( &request->headers_section );
3612 /* Find requested header structure */
3613 switch (level)
3615 case HTTP_QUERY_CUSTOM:
3616 if (!lpBuffer)
3618 LeaveCriticalSection( &request->headers_section );
3619 return ERROR_INVALID_PARAMETER;
3621 index = HTTP_GetCustomHeaderIndex(request, lpBuffer, requested_index, request_only);
3622 break;
3623 case HTTP_QUERY_RAW_HEADERS_CRLF:
3625 LPWSTR headers;
3626 DWORD len = 0;
3627 DWORD res = ERROR_INVALID_PARAMETER;
3629 if (request_only)
3630 headers = build_request_header(request, request->verb, request->path, request->version, TRUE);
3631 else
3632 headers = build_response_header(request, TRUE);
3633 if (!headers)
3635 LeaveCriticalSection( &request->headers_section );
3636 return ERROR_OUTOFMEMORY;
3639 len = strlenW(headers) * sizeof(WCHAR);
3640 if (len + sizeof(WCHAR) > *lpdwBufferLength)
3642 len += sizeof(WCHAR);
3643 res = ERROR_INSUFFICIENT_BUFFER;
3645 else if (lpBuffer)
3647 memcpy(lpBuffer, headers, len + sizeof(WCHAR));
3648 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len / sizeof(WCHAR)));
3649 res = ERROR_SUCCESS;
3651 *lpdwBufferLength = len;
3653 heap_free(headers);
3654 LeaveCriticalSection( &request->headers_section );
3655 return res;
3657 case HTTP_QUERY_RAW_HEADERS:
3659 LPWSTR headers;
3660 DWORD len;
3662 if (request_only)
3663 headers = build_request_header(request, request->verb, request->path, request->version, FALSE);
3664 else
3665 headers = build_response_header(request, FALSE);
3667 if (!headers)
3669 LeaveCriticalSection( &request->headers_section );
3670 return ERROR_OUTOFMEMORY;
3673 len = strlenW(headers) * sizeof(WCHAR);
3674 if (len > *lpdwBufferLength)
3676 *lpdwBufferLength = len;
3677 heap_free(headers);
3678 LeaveCriticalSection( &request->headers_section );
3679 return ERROR_INSUFFICIENT_BUFFER;
3682 if (lpBuffer)
3684 DWORD i;
3686 TRACE("returning data: %s\n", debugstr_wn(headers, len / sizeof(WCHAR)));
3688 for (i = 0; i < len / sizeof(WCHAR); i++)
3690 if (headers[i] == '\n')
3691 headers[i] = 0;
3693 memcpy(lpBuffer, headers, len);
3695 *lpdwBufferLength = len - sizeof(WCHAR);
3697 heap_free(headers);
3698 LeaveCriticalSection( &request->headers_section );
3699 return ERROR_SUCCESS;
3701 case HTTP_QUERY_STATUS_TEXT:
3702 if (request->statusText)
3704 DWORD len = strlenW(request->statusText);
3705 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
3707 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
3708 LeaveCriticalSection( &request->headers_section );
3709 return ERROR_INSUFFICIENT_BUFFER;
3711 if (lpBuffer)
3713 memcpy(lpBuffer, request->statusText, (len + 1) * sizeof(WCHAR));
3714 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
3716 *lpdwBufferLength = len * sizeof(WCHAR);
3717 LeaveCriticalSection( &request->headers_section );
3718 return ERROR_SUCCESS;
3720 break;
3721 case HTTP_QUERY_VERSION:
3722 if (request->version)
3724 DWORD len = strlenW(request->version);
3725 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
3727 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
3728 LeaveCriticalSection( &request->headers_section );
3729 return ERROR_INSUFFICIENT_BUFFER;
3731 if (lpBuffer)
3733 memcpy(lpBuffer, request->version, (len + 1) * sizeof(WCHAR));
3734 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
3736 *lpdwBufferLength = len * sizeof(WCHAR);
3737 LeaveCriticalSection( &request->headers_section );
3738 return ERROR_SUCCESS;
3740 break;
3741 case HTTP_QUERY_CONTENT_ENCODING:
3742 index = HTTP_GetCustomHeaderIndex(request, header_lookup[request->read_gzip ? HTTP_QUERY_CONTENT_TYPE : level],
3743 requested_index,request_only);
3744 break;
3745 case HTTP_QUERY_STATUS_CODE: {
3746 DWORD res = ERROR_SUCCESS;
3748 if(request_only)
3750 LeaveCriticalSection( &request->headers_section );
3751 return ERROR_HTTP_INVALID_QUERY_REQUEST;
3754 if(requested_index)
3755 break;
3757 if(dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) {
3758 if(*lpdwBufferLength >= sizeof(DWORD))
3759 *(DWORD*)lpBuffer = request->status_code;
3760 else
3761 res = ERROR_INSUFFICIENT_BUFFER;
3762 *lpdwBufferLength = sizeof(DWORD);
3763 }else {
3764 WCHAR buf[12];
3765 DWORD size;
3766 static const WCHAR formatW[] = {'%','u',0};
3768 size = sprintfW(buf, formatW, request->status_code) * sizeof(WCHAR);
3770 if(size <= *lpdwBufferLength) {
3771 memcpy(lpBuffer, buf, size+sizeof(WCHAR));
3772 }else {
3773 size += sizeof(WCHAR);
3774 res = ERROR_INSUFFICIENT_BUFFER;
3777 *lpdwBufferLength = size;
3779 LeaveCriticalSection( &request->headers_section );
3780 return res;
3782 default:
3783 assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
3785 if (level < LAST_TABLE_HEADER && header_lookup[level])
3786 index = HTTP_GetCustomHeaderIndex(request, header_lookup[level],
3787 requested_index,request_only);
3790 if (index >= 0)
3791 lphttpHdr = &request->custHeaders[index];
3793 /* Ensure header satisfies requested attributes */
3794 if (!lphttpHdr ||
3795 ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
3796 (~lphttpHdr->wFlags & HDR_ISREQUEST)))
3798 LeaveCriticalSection( &request->headers_section );
3799 return ERROR_HTTP_HEADER_NOT_FOUND;
3802 /* coalesce value to requested type */
3803 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER && lpBuffer)
3805 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
3806 TRACE(" returning number: %d\n", *(int *)lpBuffer);
3808 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME && lpBuffer)
3810 time_t tmpTime;
3811 struct tm tmpTM;
3812 SYSTEMTIME *STHook;
3814 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
3816 tmpTM = *gmtime(&tmpTime);
3817 STHook = (SYSTEMTIME *)lpBuffer;
3818 STHook->wDay = tmpTM.tm_mday;
3819 STHook->wHour = tmpTM.tm_hour;
3820 STHook->wMilliseconds = 0;
3821 STHook->wMinute = tmpTM.tm_min;
3822 STHook->wDayOfWeek = tmpTM.tm_wday;
3823 STHook->wMonth = tmpTM.tm_mon + 1;
3824 STHook->wSecond = tmpTM.tm_sec;
3825 STHook->wYear = 1900+tmpTM.tm_year;
3827 TRACE(" returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
3828 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
3829 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
3831 else if (lphttpHdr->lpszValue)
3833 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
3835 if (len > *lpdwBufferLength)
3837 *lpdwBufferLength = len;
3838 LeaveCriticalSection( &request->headers_section );
3839 return ERROR_INSUFFICIENT_BUFFER;
3841 if (lpBuffer)
3843 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
3844 TRACE("! returning string: %s\n", debugstr_w(lpBuffer));
3846 *lpdwBufferLength = len - sizeof(WCHAR);
3848 if (lpdwIndex) (*lpdwIndex)++;
3850 LeaveCriticalSection( &request->headers_section );
3851 return ERROR_SUCCESS;
3854 /***********************************************************************
3855 * HttpQueryInfoW (WININET.@)
3857 * Queries for information about an HTTP request
3859 * RETURNS
3860 * TRUE on success
3861 * FALSE on failure
3864 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
3865 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
3867 http_request_t *request;
3868 DWORD res;
3870 if (TRACE_ON(wininet)) {
3871 #define FE(x) { x, #x }
3872 static const wininet_flag_info query_flags[] = {
3873 FE(HTTP_QUERY_MIME_VERSION),
3874 FE(HTTP_QUERY_CONTENT_TYPE),
3875 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
3876 FE(HTTP_QUERY_CONTENT_ID),
3877 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
3878 FE(HTTP_QUERY_CONTENT_LENGTH),
3879 FE(HTTP_QUERY_CONTENT_LANGUAGE),
3880 FE(HTTP_QUERY_ALLOW),
3881 FE(HTTP_QUERY_PUBLIC),
3882 FE(HTTP_QUERY_DATE),
3883 FE(HTTP_QUERY_EXPIRES),
3884 FE(HTTP_QUERY_LAST_MODIFIED),
3885 FE(HTTP_QUERY_MESSAGE_ID),
3886 FE(HTTP_QUERY_URI),
3887 FE(HTTP_QUERY_DERIVED_FROM),
3888 FE(HTTP_QUERY_COST),
3889 FE(HTTP_QUERY_LINK),
3890 FE(HTTP_QUERY_PRAGMA),
3891 FE(HTTP_QUERY_VERSION),
3892 FE(HTTP_QUERY_STATUS_CODE),
3893 FE(HTTP_QUERY_STATUS_TEXT),
3894 FE(HTTP_QUERY_RAW_HEADERS),
3895 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
3896 FE(HTTP_QUERY_CONNECTION),
3897 FE(HTTP_QUERY_ACCEPT),
3898 FE(HTTP_QUERY_ACCEPT_CHARSET),
3899 FE(HTTP_QUERY_ACCEPT_ENCODING),
3900 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
3901 FE(HTTP_QUERY_AUTHORIZATION),
3902 FE(HTTP_QUERY_CONTENT_ENCODING),
3903 FE(HTTP_QUERY_FORWARDED),
3904 FE(HTTP_QUERY_FROM),
3905 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
3906 FE(HTTP_QUERY_LOCATION),
3907 FE(HTTP_QUERY_ORIG_URI),
3908 FE(HTTP_QUERY_REFERER),
3909 FE(HTTP_QUERY_RETRY_AFTER),
3910 FE(HTTP_QUERY_SERVER),
3911 FE(HTTP_QUERY_TITLE),
3912 FE(HTTP_QUERY_USER_AGENT),
3913 FE(HTTP_QUERY_WWW_AUTHENTICATE),
3914 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
3915 FE(HTTP_QUERY_ACCEPT_RANGES),
3916 FE(HTTP_QUERY_SET_COOKIE),
3917 FE(HTTP_QUERY_COOKIE),
3918 FE(HTTP_QUERY_REQUEST_METHOD),
3919 FE(HTTP_QUERY_REFRESH),
3920 FE(HTTP_QUERY_CONTENT_DISPOSITION),
3921 FE(HTTP_QUERY_AGE),
3922 FE(HTTP_QUERY_CACHE_CONTROL),
3923 FE(HTTP_QUERY_CONTENT_BASE),
3924 FE(HTTP_QUERY_CONTENT_LOCATION),
3925 FE(HTTP_QUERY_CONTENT_MD5),
3926 FE(HTTP_QUERY_CONTENT_RANGE),
3927 FE(HTTP_QUERY_ETAG),
3928 FE(HTTP_QUERY_HOST),
3929 FE(HTTP_QUERY_IF_MATCH),
3930 FE(HTTP_QUERY_IF_NONE_MATCH),
3931 FE(HTTP_QUERY_IF_RANGE),
3932 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
3933 FE(HTTP_QUERY_MAX_FORWARDS),
3934 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
3935 FE(HTTP_QUERY_RANGE),
3936 FE(HTTP_QUERY_TRANSFER_ENCODING),
3937 FE(HTTP_QUERY_UPGRADE),
3938 FE(HTTP_QUERY_VARY),
3939 FE(HTTP_QUERY_VIA),
3940 FE(HTTP_QUERY_WARNING),
3941 FE(HTTP_QUERY_CUSTOM)
3943 static const wininet_flag_info modifier_flags[] = {
3944 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
3945 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
3946 FE(HTTP_QUERY_FLAG_NUMBER),
3947 FE(HTTP_QUERY_FLAG_COALESCE)
3949 #undef FE
3950 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
3951 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
3952 DWORD i;
3954 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, info);
3955 TRACE(" Attribute:");
3956 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
3957 if (query_flags[i].val == info) {
3958 TRACE(" %s", query_flags[i].name);
3959 break;
3962 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
3963 TRACE(" Unknown (%08x)", info);
3966 TRACE(" Modifier:");
3967 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
3968 if (modifier_flags[i].val & info_mod) {
3969 TRACE(" %s", modifier_flags[i].name);
3970 info_mod &= ~ modifier_flags[i].val;
3974 if (info_mod) {
3975 TRACE(" Unknown (%08x)", info_mod);
3977 TRACE("\n");
3980 request = (http_request_t*) get_handle_object( hHttpRequest );
3981 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
3983 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3984 goto lend;
3987 if (lpBuffer == NULL)
3988 *lpdwBufferLength = 0;
3989 res = HTTP_HttpQueryInfoW( request, dwInfoLevel,
3990 lpBuffer, lpdwBufferLength, lpdwIndex);
3992 lend:
3993 if( request )
3994 WININET_Release( &request->hdr );
3996 TRACE("%u <--\n", res);
3997 if(res != ERROR_SUCCESS)
3998 SetLastError(res);
3999 return res == ERROR_SUCCESS;
4002 /***********************************************************************
4003 * HttpQueryInfoA (WININET.@)
4005 * Queries for information about an HTTP request
4007 * RETURNS
4008 * TRUE on success
4009 * FALSE on failure
4012 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
4013 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
4015 BOOL result;
4016 DWORD len;
4017 WCHAR* bufferW;
4019 TRACE("%p %x\n", hHttpRequest, dwInfoLevel);
4021 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
4022 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
4024 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
4025 lpdwBufferLength, lpdwIndex );
4028 if (lpBuffer)
4030 DWORD alloclen;
4031 len = (*lpdwBufferLength)*sizeof(WCHAR);
4032 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
4034 alloclen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 ) * sizeof(WCHAR);
4035 if (alloclen < len)
4036 alloclen = len;
4038 else
4039 alloclen = len;
4040 bufferW = heap_alloc(alloclen);
4041 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
4042 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
4043 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, bufferW, alloclen / sizeof(WCHAR) );
4044 } else
4046 bufferW = NULL;
4047 len = 0;
4050 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
4051 &len, lpdwIndex );
4052 if( result )
4054 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
4055 lpBuffer, *lpdwBufferLength, NULL, NULL );
4056 *lpdwBufferLength = len - 1;
4058 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
4060 else
4061 /* since the strings being returned from HttpQueryInfoW should be
4062 * only ASCII characters, it is reasonable to assume that all of
4063 * the Unicode characters can be reduced to a single byte */
4064 *lpdwBufferLength = len / sizeof(WCHAR);
4066 heap_free( bufferW );
4067 return result;
4070 static WCHAR *get_redirect_url(http_request_t *request)
4072 static WCHAR szHttp[] = {'h','t','t','p',0};
4073 static WCHAR szHttps[] = {'h','t','t','p','s',0};
4074 http_session_t *session = request->session;
4075 URL_COMPONENTSW urlComponents = { sizeof(urlComponents) };
4076 WCHAR *orig_url = NULL, *redirect_url = NULL, *combined_url = NULL;
4077 DWORD url_length = 0, res;
4078 BOOL b;
4080 url_length = 0;
4081 res = HTTP_HttpQueryInfoW(request, HTTP_QUERY_LOCATION, redirect_url, &url_length, NULL);
4082 if(res == ERROR_INSUFFICIENT_BUFFER) {
4083 redirect_url = heap_alloc(url_length);
4084 res = HTTP_HttpQueryInfoW(request, HTTP_QUERY_LOCATION, redirect_url, &url_length, NULL);
4086 if(res != ERROR_SUCCESS) {
4087 heap_free(redirect_url);
4088 return NULL;
4091 urlComponents.lpszScheme = (request->hdr.dwFlags & INTERNET_FLAG_SECURE) ? szHttps : szHttp;
4092 urlComponents.lpszHostName = request->server->name;
4093 urlComponents.nPort = request->server->port;
4094 urlComponents.lpszUserName = session->userName;
4095 urlComponents.lpszUrlPath = request->path;
4097 b = InternetCreateUrlW(&urlComponents, 0, NULL, &url_length);
4098 if(!b && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
4099 orig_url = heap_alloc(url_length);
4101 /* convert from bytes to characters */
4102 url_length = url_length / sizeof(WCHAR) - 1;
4103 b = InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length);
4106 if(b) {
4107 url_length = 0;
4108 b = InternetCombineUrlW(orig_url, redirect_url, NULL, &url_length, ICU_ENCODE_SPACES_ONLY);
4109 if(!b && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
4110 combined_url = heap_alloc(url_length * sizeof(WCHAR));
4111 b = InternetCombineUrlW(orig_url, redirect_url, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY);
4112 if(!b) {
4113 heap_free(combined_url);
4114 combined_url = NULL;
4119 heap_free(orig_url);
4120 heap_free(redirect_url);
4121 return combined_url;
4125 /***********************************************************************
4126 * HTTP_HandleRedirect (internal)
4128 static DWORD HTTP_HandleRedirect(http_request_t *request, LPCWSTR lpszUrl)
4130 http_session_t *session = request->session;
4131 WCHAR *path;
4133 if(lpszUrl[0]=='/')
4135 /* if it's an absolute path, keep the same session info */
4136 path = heap_strdupW(lpszUrl);
4138 else
4140 URL_COMPONENTSW urlComponents = { sizeof(urlComponents) };
4141 BOOL custom_port = FALSE;
4142 substr_t host;
4144 urlComponents.dwHostNameLength = 1;
4145 urlComponents.dwUserNameLength = 1;
4146 urlComponents.dwUrlPathLength = 1;
4147 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
4148 return INTERNET_GetLastError();
4150 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP) {
4151 if(request->hdr.dwFlags & INTERNET_FLAG_SECURE) {
4152 TRACE("redirect from secure page to non-secure page\n");
4153 /* FIXME: warn about from secure redirect to non-secure page */
4154 request->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
4157 custom_port = urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT;
4158 }else if(urlComponents.nScheme == INTERNET_SCHEME_HTTPS) {
4159 if(!(request->hdr.dwFlags & INTERNET_FLAG_SECURE)) {
4160 TRACE("redirect from non-secure page to secure page\n");
4161 /* FIXME: notify about redirect to secure page */
4162 request->hdr.dwFlags |= INTERNET_FLAG_SECURE;
4165 custom_port = urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT;
4168 heap_free(session->hostName);
4170 session->hostName = heap_strndupW(urlComponents.lpszHostName, urlComponents.dwHostNameLength);
4171 session->hostPort = urlComponents.nPort;
4173 heap_free(session->userName);
4174 session->userName = NULL;
4175 if (urlComponents.dwUserNameLength)
4176 session->userName = heap_strndupW(urlComponents.lpszUserName, urlComponents.dwUserNameLength);
4178 reset_data_stream(request);
4180 host = substr(urlComponents.lpszHostName, urlComponents.dwHostNameLength);
4182 if(host.len != strlenW(request->server->name) || strncmpiW(request->server->name, host.str, host.len)
4183 || request->server->port != urlComponents.nPort) {
4184 server_t *new_server;
4186 new_server = get_server(host, urlComponents.nPort, urlComponents.nScheme == INTERNET_SCHEME_HTTPS, TRUE);
4187 server_release(request->server);
4188 request->server = new_server;
4191 if (custom_port)
4192 HTTP_ProcessHeader(request, hostW, request->server->host_port, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
4193 else
4194 HTTP_ProcessHeader(request, hostW, request->server->name, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
4196 path = heap_strndupW(urlComponents.lpszUrlPath, urlComponents.dwUrlPathLength);
4198 heap_free(request->path);
4199 request->path = NULL;
4200 if (*path)
4202 DWORD needed = 1;
4203 HRESULT rc;
4204 WCHAR dummy = 0;
4206 rc = UrlEscapeW(path, &dummy, &needed, URL_ESCAPE_SPACES_ONLY);
4207 if (rc != E_POINTER)
4208 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
4209 request->path = heap_alloc(needed*sizeof(WCHAR));
4210 rc = UrlEscapeW(path, request->path, &needed,
4211 URL_ESCAPE_SPACES_ONLY);
4212 if (rc != S_OK)
4214 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
4215 strcpyW(request->path, path);
4219 heap_free(path);
4221 /* Remove custom content-type/length headers on redirects. */
4222 remove_header(request, szContent_Type, TRUE);
4223 remove_header(request, szContent_Length, TRUE);
4225 return ERROR_SUCCESS;
4228 /***********************************************************************
4229 * HTTP_build_req (internal)
4231 * concatenate all the strings in the request together
4233 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
4235 LPCWSTR *t;
4236 LPWSTR str;
4238 for( t = list; *t ; t++ )
4239 len += strlenW( *t );
4240 len++;
4242 str = heap_alloc(len*sizeof(WCHAR));
4243 *str = 0;
4245 for( t = list; *t ; t++ )
4246 strcatW( str, *t );
4248 return str;
4251 static void HTTP_InsertCookies(http_request_t *request)
4253 WCHAR *cookies;
4254 DWORD res;
4256 res = get_cookie_header(request->server->name, request->path, &cookies);
4257 if(res != ERROR_SUCCESS || !cookies)
4258 return;
4260 HTTP_HttpAddRequestHeadersW(request, cookies, strlenW(cookies),
4261 HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
4262 heap_free(cookies);
4265 static WORD HTTP_ParseWkday(LPCWSTR day)
4267 static const WCHAR days[7][4] = {{ 's','u','n',0 },
4268 { 'm','o','n',0 },
4269 { 't','u','e',0 },
4270 { 'w','e','d',0 },
4271 { 't','h','u',0 },
4272 { 'f','r','i',0 },
4273 { 's','a','t',0 }};
4274 unsigned int i;
4275 for (i = 0; i < sizeof(days)/sizeof(*days); i++)
4276 if (!strcmpiW(day, days[i]))
4277 return i;
4279 /* Invalid */
4280 return 7;
4283 static WORD HTTP_ParseMonth(LPCWSTR month)
4285 static const WCHAR jan[] = { 'j','a','n',0 };
4286 static const WCHAR feb[] = { 'f','e','b',0 };
4287 static const WCHAR mar[] = { 'm','a','r',0 };
4288 static const WCHAR apr[] = { 'a','p','r',0 };
4289 static const WCHAR may[] = { 'm','a','y',0 };
4290 static const WCHAR jun[] = { 'j','u','n',0 };
4291 static const WCHAR jul[] = { 'j','u','l',0 };
4292 static const WCHAR aug[] = { 'a','u','g',0 };
4293 static const WCHAR sep[] = { 's','e','p',0 };
4294 static const WCHAR oct[] = { 'o','c','t',0 };
4295 static const WCHAR nov[] = { 'n','o','v',0 };
4296 static const WCHAR dec[] = { 'd','e','c',0 };
4298 if (!strcmpiW(month, jan)) return 1;
4299 if (!strcmpiW(month, feb)) return 2;
4300 if (!strcmpiW(month, mar)) return 3;
4301 if (!strcmpiW(month, apr)) return 4;
4302 if (!strcmpiW(month, may)) return 5;
4303 if (!strcmpiW(month, jun)) return 6;
4304 if (!strcmpiW(month, jul)) return 7;
4305 if (!strcmpiW(month, aug)) return 8;
4306 if (!strcmpiW(month, sep)) return 9;
4307 if (!strcmpiW(month, oct)) return 10;
4308 if (!strcmpiW(month, nov)) return 11;
4309 if (!strcmpiW(month, dec)) return 12;
4310 /* Invalid */
4311 return 0;
4314 /* Parses the string pointed to by *str, assumed to be a 24-hour time HH:MM:SS,
4315 * optionally preceded by whitespace.
4316 * Upon success, returns TRUE, sets the wHour, wMinute, and wSecond fields of
4317 * st, and sets *str to the first character after the time format.
4319 static BOOL HTTP_ParseTime(SYSTEMTIME *st, LPCWSTR *str)
4321 LPCWSTR ptr = *str;
4322 WCHAR *nextPtr;
4323 unsigned long num;
4325 while (isspaceW(*ptr))
4326 ptr++;
4328 num = strtoulW(ptr, &nextPtr, 10);
4329 if (!nextPtr || nextPtr <= ptr || *nextPtr != ':')
4331 ERR("unexpected time format %s\n", debugstr_w(ptr));
4332 return FALSE;
4334 if (num > 23)
4336 ERR("unexpected hour in time format %s\n", debugstr_w(ptr));
4337 return FALSE;
4339 ptr = nextPtr + 1;
4340 st->wHour = (WORD)num;
4341 num = strtoulW(ptr, &nextPtr, 10);
4342 if (!nextPtr || nextPtr <= ptr || *nextPtr != ':')
4344 ERR("unexpected time format %s\n", debugstr_w(ptr));
4345 return FALSE;
4347 if (num > 59)
4349 ERR("unexpected minute in time format %s\n", debugstr_w(ptr));
4350 return FALSE;
4352 ptr = nextPtr + 1;
4353 st->wMinute = (WORD)num;
4354 num = strtoulW(ptr, &nextPtr, 10);
4355 if (!nextPtr || nextPtr <= ptr)
4357 ERR("unexpected time format %s\n", debugstr_w(ptr));
4358 return FALSE;
4360 if (num > 59)
4362 ERR("unexpected second in time format %s\n", debugstr_w(ptr));
4363 return FALSE;
4365 ptr = nextPtr + 1;
4366 *str = ptr;
4367 st->wSecond = (WORD)num;
4368 return TRUE;
4371 static BOOL HTTP_ParseDateAsAsctime(LPCWSTR value, FILETIME *ft)
4373 static const WCHAR gmt[]= { 'G','M','T',0 };
4374 WCHAR day[4], *dayPtr, month[4], *monthPtr, *nextPtr;
4375 LPCWSTR ptr;
4376 SYSTEMTIME st = { 0 };
4377 unsigned long num;
4379 for (ptr = value, dayPtr = day; *ptr && !isspaceW(*ptr) &&
4380 dayPtr - day < sizeof(day) / sizeof(day[0]) - 1; ptr++, dayPtr++)
4381 *dayPtr = *ptr;
4382 *dayPtr = 0;
4383 st.wDayOfWeek = HTTP_ParseWkday(day);
4384 if (st.wDayOfWeek >= 7)
4386 ERR("unexpected weekday %s\n", debugstr_w(day));
4387 return FALSE;
4390 while (isspaceW(*ptr))
4391 ptr++;
4393 for (monthPtr = month; !isspace(*ptr) &&
4394 monthPtr - month < sizeof(month) / sizeof(month[0]) - 1;
4395 monthPtr++, ptr++)
4396 *monthPtr = *ptr;
4397 *monthPtr = 0;
4398 st.wMonth = HTTP_ParseMonth(month);
4399 if (!st.wMonth || st.wMonth > 12)
4401 ERR("unexpected month %s\n", debugstr_w(month));
4402 return FALSE;
4405 while (isspaceW(*ptr))
4406 ptr++;
4408 num = strtoulW(ptr, &nextPtr, 10);
4409 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4411 ERR("unexpected day %s\n", debugstr_w(ptr));
4412 return FALSE;
4414 ptr = nextPtr;
4415 st.wDay = (WORD)num;
4417 while (isspaceW(*ptr))
4418 ptr++;
4420 if (!HTTP_ParseTime(&st, &ptr))
4421 return FALSE;
4423 while (isspaceW(*ptr))
4424 ptr++;
4426 num = strtoulW(ptr, &nextPtr, 10);
4427 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4429 ERR("unexpected year %s\n", debugstr_w(ptr));
4430 return FALSE;
4432 ptr = nextPtr;
4433 st.wYear = (WORD)num;
4435 while (isspaceW(*ptr))
4436 ptr++;
4438 /* asctime() doesn't report a timezone, but some web servers do, so accept
4439 * with or without GMT.
4441 if (*ptr && strcmpW(ptr, gmt))
4443 ERR("unexpected timezone %s\n", debugstr_w(ptr));
4444 return FALSE;
4446 return SystemTimeToFileTime(&st, ft);
4449 static BOOL HTTP_ParseRfc1123Date(LPCWSTR value, FILETIME *ft)
4451 static const WCHAR gmt[]= { 'G','M','T',0 };
4452 WCHAR *nextPtr, day[4], month[4], *monthPtr;
4453 LPCWSTR ptr;
4454 unsigned long num;
4455 SYSTEMTIME st = { 0 };
4457 ptr = strchrW(value, ',');
4458 if (!ptr)
4459 return FALSE;
4460 if (ptr - value != 3)
4462 WARN("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4463 return FALSE;
4465 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4466 day[3] = 0;
4467 st.wDayOfWeek = HTTP_ParseWkday(day);
4468 if (st.wDayOfWeek > 6)
4470 WARN("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4471 return FALSE;
4473 ptr++;
4475 while (isspaceW(*ptr))
4476 ptr++;
4478 num = strtoulW(ptr, &nextPtr, 10);
4479 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4481 WARN("unexpected day %s\n", debugstr_w(value));
4482 return FALSE;
4484 ptr = nextPtr;
4485 st.wDay = (WORD)num;
4487 while (isspaceW(*ptr))
4488 ptr++;
4490 for (monthPtr = month; !isspace(*ptr) &&
4491 monthPtr - month < sizeof(month) / sizeof(month[0]) - 1;
4492 monthPtr++, ptr++)
4493 *monthPtr = *ptr;
4494 *monthPtr = 0;
4495 st.wMonth = HTTP_ParseMonth(month);
4496 if (!st.wMonth || st.wMonth > 12)
4498 WARN("unexpected month %s\n", debugstr_w(month));
4499 return FALSE;
4502 while (isspaceW(*ptr))
4503 ptr++;
4505 num = strtoulW(ptr, &nextPtr, 10);
4506 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4508 ERR("unexpected year %s\n", debugstr_w(value));
4509 return FALSE;
4511 ptr = nextPtr;
4512 st.wYear = (WORD)num;
4514 if (!HTTP_ParseTime(&st, &ptr))
4515 return FALSE;
4517 while (isspaceW(*ptr))
4518 ptr++;
4520 if (strcmpW(ptr, gmt))
4522 ERR("unexpected time zone %s\n", debugstr_w(ptr));
4523 return FALSE;
4525 return SystemTimeToFileTime(&st, ft);
4528 static WORD HTTP_ParseWeekday(LPCWSTR day)
4530 static const WCHAR days[7][10] = {{ 's','u','n','d','a','y',0 },
4531 { 'm','o','n','d','a','y',0 },
4532 { 't','u','e','s','d','a','y',0 },
4533 { 'w','e','d','n','e','s','d','a','y',0 },
4534 { 't','h','u','r','s','d','a','y',0 },
4535 { 'f','r','i','d','a','y',0 },
4536 { 's','a','t','u','r','d','a','y',0 }};
4537 unsigned int i;
4538 for (i = 0; i < sizeof(days)/sizeof(*days); i++)
4539 if (!strcmpiW(day, days[i]))
4540 return i;
4542 /* Invalid */
4543 return 7;
4546 static BOOL HTTP_ParseRfc850Date(LPCWSTR value, FILETIME *ft)
4548 static const WCHAR gmt[]= { 'G','M','T',0 };
4549 WCHAR *nextPtr, day[10], month[4], *monthPtr;
4550 LPCWSTR ptr;
4551 unsigned long num;
4552 SYSTEMTIME st = { 0 };
4554 ptr = strchrW(value, ',');
4555 if (!ptr)
4556 return FALSE;
4557 if (ptr - value == 3)
4559 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4560 day[3] = 0;
4561 st.wDayOfWeek = HTTP_ParseWkday(day);
4562 if (st.wDayOfWeek > 6)
4564 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4565 return FALSE;
4568 else if (ptr - value < sizeof(day) / sizeof(day[0]))
4570 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4571 day[ptr - value + 1] = 0;
4572 st.wDayOfWeek = HTTP_ParseWeekday(day);
4573 if (st.wDayOfWeek > 6)
4575 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4576 return FALSE;
4579 else
4581 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4582 return FALSE;
4584 ptr++;
4586 while (isspaceW(*ptr))
4587 ptr++;
4589 num = strtoulW(ptr, &nextPtr, 10);
4590 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4592 ERR("unexpected day %s\n", debugstr_w(value));
4593 return FALSE;
4595 ptr = nextPtr;
4596 st.wDay = (WORD)num;
4598 if (*ptr != '-')
4600 ERR("unexpected month format %s\n", debugstr_w(ptr));
4601 return FALSE;
4603 ptr++;
4605 for (monthPtr = month; *ptr != '-' &&
4606 monthPtr - month < sizeof(month) / sizeof(month[0]) - 1;
4607 monthPtr++, ptr++)
4608 *monthPtr = *ptr;
4609 *monthPtr = 0;
4610 st.wMonth = HTTP_ParseMonth(month);
4611 if (!st.wMonth || st.wMonth > 12)
4613 ERR("unexpected month %s\n", debugstr_w(month));
4614 return FALSE;
4617 if (*ptr != '-')
4619 ERR("unexpected year format %s\n", debugstr_w(ptr));
4620 return FALSE;
4622 ptr++;
4624 num = strtoulW(ptr, &nextPtr, 10);
4625 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4627 ERR("unexpected year %s\n", debugstr_w(value));
4628 return FALSE;
4630 ptr = nextPtr;
4631 st.wYear = (WORD)num;
4633 if (!HTTP_ParseTime(&st, &ptr))
4634 return FALSE;
4636 while (isspaceW(*ptr))
4637 ptr++;
4639 if (strcmpW(ptr, gmt))
4641 ERR("unexpected time zone %s\n", debugstr_w(ptr));
4642 return FALSE;
4644 return SystemTimeToFileTime(&st, ft);
4647 static BOOL HTTP_ParseDate(LPCWSTR value, FILETIME *ft)
4649 static const WCHAR zero[] = { '0',0 };
4650 BOOL ret;
4652 if (!strcmpW(value, zero))
4654 ft->dwLowDateTime = ft->dwHighDateTime = 0;
4655 ret = TRUE;
4657 else if (strchrW(value, ','))
4659 ret = HTTP_ParseRfc1123Date(value, ft);
4660 if (!ret)
4662 ret = HTTP_ParseRfc850Date(value, ft);
4663 if (!ret)
4664 ERR("unexpected date format %s\n", debugstr_w(value));
4667 else
4669 ret = HTTP_ParseDateAsAsctime(value, ft);
4670 if (!ret)
4671 ERR("unexpected date format %s\n", debugstr_w(value));
4673 return ret;
4676 static void HTTP_ProcessExpires(http_request_t *request)
4678 BOOL expirationFound = FALSE;
4679 int headerIndex;
4681 EnterCriticalSection( &request->headers_section );
4683 /* Look for a Cache-Control header with a max-age directive, as it takes
4684 * precedence over the Expires header.
4686 headerIndex = HTTP_GetCustomHeaderIndex(request, szCache_Control, 0, FALSE);
4687 if (headerIndex != -1)
4689 LPHTTPHEADERW ccHeader = &request->custHeaders[headerIndex];
4690 LPWSTR ptr;
4692 for (ptr = ccHeader->lpszValue; ptr && *ptr; )
4694 LPWSTR comma = strchrW(ptr, ','), end, equal;
4696 if (comma)
4697 end = comma;
4698 else
4699 end = ptr + strlenW(ptr);
4700 for (equal = end - 1; equal > ptr && *equal != '='; equal--)
4702 if (*equal == '=')
4704 static const WCHAR max_age[] = {
4705 'm','a','x','-','a','g','e',0 };
4707 if (!strncmpiW(ptr, max_age, equal - ptr - 1))
4709 LPWSTR nextPtr;
4710 unsigned long age;
4712 age = strtoulW(equal + 1, &nextPtr, 10);
4713 if (nextPtr > equal + 1)
4715 LARGE_INTEGER ft;
4717 NtQuerySystemTime( &ft );
4718 /* Age is in seconds, FILETIME resolution is in
4719 * 100 nanosecond intervals.
4721 ft.QuadPart += age * (ULONGLONG)1000000;
4722 request->expires.dwLowDateTime = ft.u.LowPart;
4723 request->expires.dwHighDateTime = ft.u.HighPart;
4724 expirationFound = TRUE;
4728 if (comma)
4730 ptr = comma + 1;
4731 while (isspaceW(*ptr))
4732 ptr++;
4734 else
4735 ptr = NULL;
4738 if (!expirationFound)
4740 headerIndex = HTTP_GetCustomHeaderIndex(request, szExpires, 0, FALSE);
4741 if (headerIndex != -1)
4743 LPHTTPHEADERW expiresHeader = &request->custHeaders[headerIndex];
4744 FILETIME ft;
4746 if (HTTP_ParseDate(expiresHeader->lpszValue, &ft))
4748 expirationFound = TRUE;
4749 request->expires = ft;
4753 if (!expirationFound)
4755 LARGE_INTEGER t;
4757 /* With no known age, default to 10 minutes until expiration. */
4758 NtQuerySystemTime( &t );
4759 t.QuadPart += 10 * 60 * (ULONGLONG)10000000;
4760 request->expires.dwLowDateTime = t.u.LowPart;
4761 request->expires.dwHighDateTime = t.u.HighPart;
4764 LeaveCriticalSection( &request->headers_section );
4767 static void HTTP_ProcessLastModified(http_request_t *request)
4769 int headerIndex;
4771 EnterCriticalSection( &request->headers_section );
4773 headerIndex = HTTP_GetCustomHeaderIndex(request, szLast_Modified, 0, FALSE);
4774 if (headerIndex != -1)
4776 LPHTTPHEADERW expiresHeader = &request->custHeaders[headerIndex];
4777 FILETIME ft;
4779 if (HTTP_ParseDate(expiresHeader->lpszValue, &ft))
4780 request->last_modified = ft;
4783 LeaveCriticalSection( &request->headers_section );
4786 static void http_process_keep_alive(http_request_t *req)
4788 int index;
4790 EnterCriticalSection( &req->headers_section );
4792 if ((index = HTTP_GetCustomHeaderIndex(req, szConnection, 0, FALSE)) != -1)
4793 req->netconn->keep_alive = !strcmpiW(req->custHeaders[index].lpszValue, szKeepAlive);
4794 else if ((index = HTTP_GetCustomHeaderIndex(req, szProxy_Connection, 0, FALSE)) != -1)
4795 req->netconn->keep_alive = !strcmpiW(req->custHeaders[index].lpszValue, szKeepAlive);
4796 else
4797 req->netconn->keep_alive = !strcmpiW(req->version, g_szHttp1_1);
4799 LeaveCriticalSection( &req->headers_section );
4802 static DWORD open_http_connection(http_request_t *request, BOOL *reusing)
4804 const BOOL is_https = (request->hdr.dwFlags & INTERNET_FLAG_SECURE) != 0;
4805 netconn_t *netconn = NULL;
4806 DWORD res;
4808 reset_data_stream(request);
4810 if (request->netconn)
4812 if (is_valid_netconn(request->netconn) && NETCON_is_alive(request->netconn))
4814 *reusing = TRUE;
4815 return ERROR_SUCCESS;
4817 else
4819 free_netconn(request->netconn);
4820 request->netconn = NULL;
4824 res = HTTP_ResolveName(request);
4825 if(res != ERROR_SUCCESS)
4826 return res;
4828 EnterCriticalSection(&connection_pool_cs);
4830 while(!list_empty(&request->server->conn_pool)) {
4831 netconn = LIST_ENTRY(list_head(&request->server->conn_pool), netconn_t, pool_entry);
4832 list_remove(&netconn->pool_entry);
4834 if(is_valid_netconn(netconn) && NETCON_is_alive(netconn))
4835 break;
4837 TRACE("connection %p closed during idle\n", netconn);
4838 free_netconn(netconn);
4839 netconn = NULL;
4842 LeaveCriticalSection(&connection_pool_cs);
4844 if(netconn) {
4845 TRACE("<-- reusing %p netconn\n", netconn);
4846 request->netconn = netconn;
4847 *reusing = TRUE;
4848 return ERROR_SUCCESS;
4851 TRACE("connecting to %s, proxy %s\n", debugstr_w(request->server->name),
4852 request->proxy ? debugstr_w(request->proxy->name) : "(null)");
4854 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
4855 INTERNET_STATUS_CONNECTING_TO_SERVER,
4856 request->server->addr_str,
4857 strlen(request->server->addr_str)+1);
4859 res = create_netconn(is_https, request->proxy ? request->proxy : request->server, request->security_flags,
4860 (request->hdr.ErrorMask & INTERNET_ERROR_MASK_COMBINED_SEC_CERT) != 0,
4861 request->connect_timeout, &netconn);
4862 if(res != ERROR_SUCCESS) {
4863 ERR("create_netconn failed: %u\n", res);
4864 return res;
4867 request->netconn = netconn;
4869 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
4870 INTERNET_STATUS_CONNECTED_TO_SERVER,
4871 request->server->addr_str, strlen(request->server->addr_str)+1);
4873 *reusing = FALSE;
4874 TRACE("Created connection to %s: %p\n", debugstr_w(request->server->name), netconn);
4875 return ERROR_SUCCESS;
4878 static char *build_ascii_request( const WCHAR *str, void *data, DWORD data_len, DWORD *out_len )
4880 int len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
4881 char *ret;
4883 if (!(ret = heap_alloc( len + data_len ))) return NULL;
4884 WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
4885 if (data_len) memcpy( ret + len - 1, data, data_len );
4886 *out_len = len + data_len - 1;
4887 ret[*out_len] = 0;
4888 return ret;
4891 static void set_content_length_header( http_request_t *request, DWORD len, DWORD flags )
4893 static const WCHAR fmtW[] =
4894 {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','u','\r','\n',0};
4895 WCHAR buf[sizeof(fmtW)/sizeof(fmtW[0]) + 10];
4897 sprintfW( buf, fmtW, len );
4898 HTTP_HttpAddRequestHeadersW( request, buf, ~0u, flags );
4901 /***********************************************************************
4902 * HTTP_HttpSendRequestW (internal)
4904 * Sends the specified request to the HTTP server
4906 * RETURNS
4907 * ERROR_SUCCESS on success
4908 * win32 error code on failure
4911 static DWORD HTTP_HttpSendRequestW(http_request_t *request, LPCWSTR lpszHeaders,
4912 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
4913 DWORD dwContentLength, BOOL bEndRequest)
4915 BOOL redirected = FALSE, secure_proxy_connect = FALSE, loop_next;
4916 LPWSTR requestString = NULL;
4917 INT responseLen, cnt;
4918 DWORD res;
4920 TRACE("--> %p\n", request);
4922 assert(request->hdr.htype == WH_HHTTPREQ);
4924 /* if the verb is NULL default to GET */
4925 if (!request->verb)
4926 request->verb = heap_strdupW(szGET);
4928 HTTP_ProcessHeader(request, hostW, request->server->canon_host_port,
4929 HTTP_ADDREQ_FLAG_ADD_IF_NEW | HTTP_ADDHDR_FLAG_REQ);
4931 if (dwContentLength || strcmpW(request->verb, szGET))
4933 set_content_length_header(request, dwContentLength, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4934 request->bytesToWrite = dwContentLength;
4936 if (request->session->appInfo->agent)
4938 WCHAR *agent_header;
4939 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0};
4940 int len;
4942 len = strlenW(request->session->appInfo->agent) + strlenW(user_agent);
4943 agent_header = heap_alloc(len * sizeof(WCHAR));
4944 sprintfW(agent_header, user_agent, request->session->appInfo->agent);
4946 HTTP_HttpAddRequestHeadersW(request, agent_header, strlenW(agent_header), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4947 heap_free(agent_header);
4949 if (request->hdr.dwFlags & INTERNET_FLAG_PRAGMA_NOCACHE)
4951 static const WCHAR pragma_nocache[] = {'P','r','a','g','m','a',':',' ','n','o','-','c','a','c','h','e','\r','\n',0};
4952 HTTP_HttpAddRequestHeadersW(request, pragma_nocache, strlenW(pragma_nocache), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4954 if ((request->hdr.dwFlags & INTERNET_FLAG_NO_CACHE_WRITE) && strcmpW(request->verb, szGET))
4956 static const WCHAR cache_control[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l',':',
4957 ' ','n','o','-','c','a','c','h','e','\r','\n',0};
4958 HTTP_HttpAddRequestHeadersW(request, cache_control, strlenW(cache_control), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4961 /* add the headers the caller supplied */
4962 if( lpszHeaders && dwHeaderLength )
4963 HTTP_HttpAddRequestHeadersW(request, lpszHeaders, dwHeaderLength, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
4967 DWORD len, data_len = dwOptionalLength;
4968 BOOL reusing_connection;
4969 char *ascii_req;
4971 loop_next = FALSE;
4973 if(redirected) {
4974 request->contentLength = ~0u;
4975 request->bytesToWrite = 0;
4978 if (TRACE_ON(wininet))
4980 HTTPHEADERW *host;
4982 EnterCriticalSection( &request->headers_section );
4983 host = HTTP_GetHeader( request, hostW );
4984 TRACE("Going to url %s %s\n", debugstr_w(host->lpszValue), debugstr_w(request->path));
4985 LeaveCriticalSection( &request->headers_section );
4988 HTTP_FixURL(request);
4989 if (request->hdr.dwFlags & INTERNET_FLAG_KEEP_CONNECTION)
4991 HTTP_ProcessHeader(request, szConnection, szKeepAlive,
4992 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_ADD);
4994 HTTP_InsertAuthorization(request, request->authInfo, szAuthorization);
4995 HTTP_InsertAuthorization(request, request->proxyAuthInfo, szProxy_Authorization);
4997 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES))
4998 HTTP_InsertCookies(request);
5000 res = open_http_connection(request, &reusing_connection);
5001 if (res != ERROR_SUCCESS)
5002 break;
5004 if (!reusing_connection && (request->hdr.dwFlags & INTERNET_FLAG_SECURE))
5006 if (request->proxy) secure_proxy_connect = TRUE;
5007 else
5009 res = NETCON_secure_connect(request->netconn, request->server);
5010 if (res != ERROR_SUCCESS)
5012 WARN("failed to upgrade to secure connection\n");
5013 http_release_netconn(request, FALSE);
5014 break;
5018 if (secure_proxy_connect)
5020 static const WCHAR connectW[] = {'C','O','N','N','E','C','T',0};
5021 const WCHAR *target = request->server->host_port;
5023 if (HTTP_GetCustomHeaderIndex(request, szContent_Length, 0, TRUE) >= 0)
5024 set_content_length_header(request, 0, HTTP_ADDREQ_FLAG_REPLACE);
5026 requestString = build_request_header(request, connectW, target, g_szHttp1_1, TRUE);
5028 else if (request->proxy && !(request->hdr.dwFlags & INTERNET_FLAG_SECURE))
5030 WCHAR *url = build_proxy_path_url(request);
5031 requestString = build_request_header(request, request->verb, url, request->version, TRUE);
5032 heap_free(url);
5034 else
5036 if (request->proxy && HTTP_GetCustomHeaderIndex(request, szContent_Length, 0, TRUE) >= 0)
5037 set_content_length_header(request, dwContentLength, HTTP_ADDREQ_FLAG_REPLACE);
5039 requestString = build_request_header(request, request->verb, request->path, request->version, TRUE);
5042 TRACE("Request header -> %s\n", debugstr_w(requestString) );
5044 /* send the request as ASCII, tack on the optional data */
5045 if (!lpOptional || redirected || secure_proxy_connect)
5046 data_len = 0;
5048 ascii_req = build_ascii_request( requestString, lpOptional, data_len, &len );
5049 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
5051 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5052 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
5054 NETCON_set_timeout( request->netconn, TRUE, request->send_timeout );
5055 res = NETCON_send(request->netconn, ascii_req, len, 0, &cnt);
5056 heap_free( ascii_req );
5057 if(res != ERROR_SUCCESS) {
5058 TRACE("send failed: %u\n", res);
5059 if(!reusing_connection)
5060 break;
5061 http_release_netconn(request, FALSE);
5062 loop_next = TRUE;
5063 continue;
5066 request->bytesWritten = data_len;
5068 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5069 INTERNET_STATUS_REQUEST_SENT,
5070 &len, sizeof(DWORD));
5072 if (bEndRequest)
5074 DWORD dwBufferSize;
5076 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5077 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
5079 if (HTTP_GetResponseHeaders(request, &responseLen))
5081 http_release_netconn(request, FALSE);
5082 res = ERROR_INTERNET_CONNECTION_ABORTED;
5083 goto lend;
5085 /* FIXME: We should know that connection is closed before sending
5086 * headers. Otherwise wrong callbacks are executed */
5087 if(!responseLen && reusing_connection) {
5088 TRACE("Connection closed by server, reconnecting\n");
5089 http_release_netconn(request, FALSE);
5090 loop_next = TRUE;
5091 continue;
5094 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5095 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
5096 sizeof(DWORD));
5098 http_process_keep_alive(request);
5099 HTTP_ProcessCookies(request);
5100 HTTP_ProcessExpires(request);
5101 HTTP_ProcessLastModified(request);
5103 res = set_content_length(request);
5104 if(res != ERROR_SUCCESS)
5105 goto lend;
5106 if(!request->contentLength)
5107 http_release_netconn(request, TRUE);
5109 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && responseLen)
5111 WCHAR *new_url;
5113 switch(request->status_code) {
5114 case HTTP_STATUS_REDIRECT:
5115 case HTTP_STATUS_MOVED:
5116 case HTTP_STATUS_REDIRECT_KEEP_VERB:
5117 case HTTP_STATUS_REDIRECT_METHOD:
5118 new_url = get_redirect_url(request);
5119 if(!new_url)
5120 break;
5122 if (strcmpW(request->verb, szGET) && strcmpW(request->verb, szHEAD) &&
5123 request->status_code != HTTP_STATUS_REDIRECT_KEEP_VERB)
5125 heap_free(request->verb);
5126 request->verb = heap_strdupW(szGET);
5128 http_release_netconn(request, drain_content(request, FALSE));
5129 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REDIRECT,
5130 new_url, (strlenW(new_url) + 1) * sizeof(WCHAR));
5131 res = HTTP_HandleRedirect(request, new_url);
5132 heap_free(new_url);
5133 if (res == ERROR_SUCCESS) {
5134 heap_free(requestString);
5135 loop_next = TRUE;
5137 redirected = TRUE;
5140 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTH) && res == ERROR_SUCCESS)
5142 WCHAR szAuthValue[2048];
5143 dwBufferSize=2048;
5144 if (request->status_code == HTTP_STATUS_DENIED)
5146 WCHAR *host = heap_strdupW( request->server->canon_host_port );
5147 DWORD dwIndex = 0;
5148 while (HTTP_HttpQueryInfoW(request,HTTP_QUERY_WWW_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex) == ERROR_SUCCESS)
5150 if (HTTP_DoAuthorization(request, szAuthValue,
5151 &request->authInfo,
5152 request->session->userName,
5153 request->session->password, host))
5155 heap_free(requestString);
5156 if(!drain_content(request, TRUE)) {
5157 FIXME("Could not drain content\n");
5158 http_release_netconn(request, FALSE);
5160 loop_next = TRUE;
5161 break;
5164 heap_free( host );
5166 if(!loop_next) {
5167 TRACE("Cleaning wrong authorization data\n");
5168 destroy_authinfo(request->authInfo);
5169 request->authInfo = NULL;
5172 if (request->status_code == HTTP_STATUS_PROXY_AUTH_REQ)
5174 DWORD dwIndex = 0;
5175 while (HTTP_HttpQueryInfoW(request,HTTP_QUERY_PROXY_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex) == ERROR_SUCCESS)
5177 if (HTTP_DoAuthorization(request, szAuthValue,
5178 &request->proxyAuthInfo,
5179 request->session->appInfo->proxyUsername,
5180 request->session->appInfo->proxyPassword,
5181 NULL))
5183 heap_free(requestString);
5184 if(!drain_content(request, TRUE)) {
5185 FIXME("Could not drain content\n");
5186 http_release_netconn(request, FALSE);
5188 loop_next = TRUE;
5189 break;
5193 if(!loop_next) {
5194 TRACE("Cleaning wrong proxy authorization data\n");
5195 destroy_authinfo(request->proxyAuthInfo);
5196 request->proxyAuthInfo = NULL;
5200 if (secure_proxy_connect && request->status_code == HTTP_STATUS_OK)
5202 res = NETCON_secure_connect(request->netconn, request->server);
5203 if (res != ERROR_SUCCESS)
5205 WARN("failed to upgrade to secure proxy connection\n");
5206 http_release_netconn( request, FALSE );
5207 break;
5209 remove_header(request, szProxy_Authorization, TRUE);
5210 destroy_authinfo(request->proxyAuthInfo);
5211 request->proxyAuthInfo = NULL;
5213 secure_proxy_connect = FALSE;
5214 loop_next = TRUE;
5217 else
5218 res = ERROR_SUCCESS;
5220 while (loop_next);
5222 lend:
5223 heap_free(requestString);
5225 /* TODO: send notification for P3P header */
5227 if(res == ERROR_SUCCESS)
5228 create_cache_entry(request);
5230 if (request->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5232 if (res == ERROR_SUCCESS) {
5233 if(bEndRequest && request->contentLength && request->bytesWritten == request->bytesToWrite)
5234 HTTP_ReceiveRequestData(request, TRUE, NULL);
5235 else
5236 send_request_complete(request,
5237 request->session->hdr.dwInternalFlags & INET_OPENURL ? (DWORD_PTR)request->hdr.hInternet : 1, 0);
5238 }else {
5239 send_request_complete(request, 0, res);
5243 TRACE("<--\n");
5244 return res;
5247 typedef struct {
5248 task_header_t hdr;
5249 WCHAR *headers;
5250 DWORD headers_len;
5251 void *optional;
5252 DWORD optional_len;
5253 DWORD content_len;
5254 BOOL end_request;
5255 } send_request_task_t;
5257 /***********************************************************************
5259 * Helper functions for the HttpSendRequest(Ex) functions
5262 static void AsyncHttpSendRequestProc(task_header_t *hdr)
5264 send_request_task_t *task = (send_request_task_t*)hdr;
5265 http_request_t *request = (http_request_t*)task->hdr.hdr;
5267 TRACE("%p\n", request);
5269 HTTP_HttpSendRequestW(request, task->headers, task->headers_len, task->optional,
5270 task->optional_len, task->content_len, task->end_request);
5272 heap_free(task->headers);
5276 static DWORD HTTP_HttpEndRequestW(http_request_t *request, DWORD dwFlags, DWORD_PTR dwContext)
5278 INT responseLen;
5279 DWORD res = ERROR_SUCCESS;
5281 if(!is_valid_netconn(request->netconn)) {
5282 WARN("Not connected\n");
5283 send_request_complete(request, 0, ERROR_INTERNET_OPERATION_CANCELLED);
5284 return ERROR_INTERNET_OPERATION_CANCELLED;
5287 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5288 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
5290 if (HTTP_GetResponseHeaders(request, &responseLen) || !responseLen)
5291 res = ERROR_HTTP_HEADER_NOT_FOUND;
5293 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5294 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
5296 /* process cookies here. Is this right? */
5297 http_process_keep_alive(request);
5298 HTTP_ProcessCookies(request);
5299 HTTP_ProcessExpires(request);
5300 HTTP_ProcessLastModified(request);
5302 if ((res = set_content_length(request)) == ERROR_SUCCESS) {
5303 if(!request->contentLength)
5304 http_release_netconn(request, TRUE);
5307 if (res == ERROR_SUCCESS && !(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
5309 switch(request->status_code) {
5310 case HTTP_STATUS_REDIRECT:
5311 case HTTP_STATUS_MOVED:
5312 case HTTP_STATUS_REDIRECT_METHOD:
5313 case HTTP_STATUS_REDIRECT_KEEP_VERB: {
5314 WCHAR *new_url;
5316 new_url = get_redirect_url(request);
5317 if(!new_url)
5318 break;
5320 if (strcmpW(request->verb, szGET) && strcmpW(request->verb, szHEAD) &&
5321 request->status_code != HTTP_STATUS_REDIRECT_KEEP_VERB)
5323 heap_free(request->verb);
5324 request->verb = heap_strdupW(szGET);
5326 http_release_netconn(request, drain_content(request, FALSE));
5327 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REDIRECT,
5328 new_url, (strlenW(new_url) + 1) * sizeof(WCHAR));
5329 res = HTTP_HandleRedirect(request, new_url);
5330 heap_free(new_url);
5331 if (res == ERROR_SUCCESS)
5332 res = HTTP_HttpSendRequestW(request, NULL, 0, NULL, 0, 0, TRUE);
5337 if(res == ERROR_SUCCESS)
5338 create_cache_entry(request);
5340 if (res == ERROR_SUCCESS && request->contentLength)
5341 HTTP_ReceiveRequestData(request, TRUE, NULL);
5342 else
5343 send_request_complete(request, res == ERROR_SUCCESS, res);
5345 return res;
5348 /***********************************************************************
5349 * HttpEndRequestA (WININET.@)
5351 * Ends an HTTP request that was started by HttpSendRequestEx
5353 * RETURNS
5354 * TRUE if successful
5355 * FALSE on failure
5358 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
5359 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
5361 TRACE("(%p, %p, %08x, %08lx)\n", hRequest, lpBuffersOut, dwFlags, dwContext);
5363 if (lpBuffersOut)
5365 SetLastError(ERROR_INVALID_PARAMETER);
5366 return FALSE;
5369 return HttpEndRequestW(hRequest, NULL, dwFlags, dwContext);
5372 typedef struct {
5373 task_header_t hdr;
5374 DWORD flags;
5375 DWORD context;
5376 } end_request_task_t;
5378 static void AsyncHttpEndRequestProc(task_header_t *hdr)
5380 end_request_task_t *task = (end_request_task_t*)hdr;
5381 http_request_t *req = (http_request_t*)task->hdr.hdr;
5383 TRACE("%p\n", req);
5385 HTTP_HttpEndRequestW(req, task->flags, task->context);
5388 /***********************************************************************
5389 * HttpEndRequestW (WININET.@)
5391 * Ends an HTTP request that was started by HttpSendRequestEx
5393 * RETURNS
5394 * TRUE if successful
5395 * FALSE on failure
5398 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
5399 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
5401 http_request_t *request;
5402 DWORD res;
5404 TRACE("%p %p %x %lx -->\n", hRequest, lpBuffersOut, dwFlags, dwContext);
5406 if (lpBuffersOut)
5408 SetLastError(ERROR_INVALID_PARAMETER);
5409 return FALSE;
5412 request = (http_request_t*) get_handle_object( hRequest );
5414 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5416 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
5417 if (request)
5418 WININET_Release( &request->hdr );
5419 return FALSE;
5421 request->hdr.dwFlags |= dwFlags;
5423 if (request->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5425 end_request_task_t *task;
5427 task = alloc_async_task(&request->hdr, AsyncHttpEndRequestProc, sizeof(*task));
5428 task->flags = dwFlags;
5429 task->context = dwContext;
5431 INTERNET_AsyncCall(&task->hdr);
5432 res = ERROR_IO_PENDING;
5434 else
5435 res = HTTP_HttpEndRequestW(request, dwFlags, dwContext);
5437 WININET_Release( &request->hdr );
5438 TRACE("%u <--\n", res);
5439 if(res != ERROR_SUCCESS)
5440 SetLastError(res);
5441 return res == ERROR_SUCCESS;
5444 /***********************************************************************
5445 * HttpSendRequestExA (WININET.@)
5447 * Sends the specified request to the HTTP server and allows chunked
5448 * transfers.
5450 * RETURNS
5451 * Success: TRUE
5452 * Failure: FALSE, call GetLastError() for more information.
5454 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
5455 LPINTERNET_BUFFERSA lpBuffersIn,
5456 LPINTERNET_BUFFERSA lpBuffersOut,
5457 DWORD dwFlags, DWORD_PTR dwContext)
5459 INTERNET_BUFFERSW BuffersInW;
5460 BOOL rc = FALSE;
5461 DWORD headerlen;
5462 LPWSTR header = NULL;
5464 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
5465 lpBuffersOut, dwFlags, dwContext);
5467 if (lpBuffersIn)
5469 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
5470 if (lpBuffersIn->lpcszHeader)
5472 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
5473 lpBuffersIn->dwHeadersLength,0,0);
5474 header = heap_alloc(headerlen*sizeof(WCHAR));
5475 if (!(BuffersInW.lpcszHeader = header))
5477 SetLastError(ERROR_OUTOFMEMORY);
5478 return FALSE;
5480 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
5481 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
5482 header, headerlen);
5484 else
5485 BuffersInW.lpcszHeader = NULL;
5486 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
5487 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
5488 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
5489 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
5490 BuffersInW.Next = NULL;
5493 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
5495 heap_free(header);
5496 return rc;
5499 /***********************************************************************
5500 * HttpSendRequestExW (WININET.@)
5502 * Sends the specified request to the HTTP server and allows chunked
5503 * transfers
5505 * RETURNS
5506 * Success: TRUE
5507 * Failure: FALSE, call GetLastError() for more information.
5509 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
5510 LPINTERNET_BUFFERSW lpBuffersIn,
5511 LPINTERNET_BUFFERSW lpBuffersOut,
5512 DWORD dwFlags, DWORD_PTR dwContext)
5514 http_request_t *request;
5515 http_session_t *session;
5516 appinfo_t *hIC;
5517 DWORD res;
5519 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
5520 lpBuffersOut, dwFlags, dwContext);
5522 request = (http_request_t*) get_handle_object( hRequest );
5524 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5526 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5527 goto lend;
5530 session = request->session;
5531 assert(session->hdr.htype == WH_HHTTPSESSION);
5532 hIC = session->appInfo;
5533 assert(hIC->hdr.htype == WH_HINIT);
5535 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5537 send_request_task_t *task;
5539 task = alloc_async_task(&request->hdr, AsyncHttpSendRequestProc, sizeof(*task));
5540 if (lpBuffersIn)
5542 DWORD size = 0;
5544 if (lpBuffersIn->lpcszHeader)
5546 if (lpBuffersIn->dwHeadersLength == ~0u)
5547 size = (strlenW( lpBuffersIn->lpcszHeader ) + 1) * sizeof(WCHAR);
5548 else
5549 size = lpBuffersIn->dwHeadersLength * sizeof(WCHAR);
5551 task->headers = heap_alloc(size);
5552 memcpy(task->headers, lpBuffersIn->lpcszHeader, size);
5554 else task->headers = NULL;
5556 task->headers_len = size / sizeof(WCHAR);
5557 task->optional = lpBuffersIn->lpvBuffer;
5558 task->optional_len = lpBuffersIn->dwBufferLength;
5559 task->content_len = lpBuffersIn->dwBufferTotal;
5561 else
5563 task->headers = NULL;
5564 task->headers_len = 0;
5565 task->optional = NULL;
5566 task->optional_len = 0;
5567 task->content_len = 0;
5570 task->end_request = FALSE;
5572 INTERNET_AsyncCall(&task->hdr);
5573 res = ERROR_IO_PENDING;
5575 else
5577 if (lpBuffersIn)
5578 res = HTTP_HttpSendRequestW(request, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
5579 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
5580 lpBuffersIn->dwBufferTotal, FALSE);
5581 else
5582 res = HTTP_HttpSendRequestW(request, NULL, 0, NULL, 0, 0, FALSE);
5585 lend:
5586 if ( request )
5587 WININET_Release( &request->hdr );
5589 TRACE("<---\n");
5590 SetLastError(res);
5591 return res == ERROR_SUCCESS;
5594 /***********************************************************************
5595 * HttpSendRequestW (WININET.@)
5597 * Sends the specified request to the HTTP server
5599 * RETURNS
5600 * TRUE on success
5601 * FALSE on failure
5604 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
5605 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
5607 http_request_t *request;
5608 http_session_t *session = NULL;
5609 appinfo_t *hIC = NULL;
5610 DWORD res = ERROR_SUCCESS;
5612 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest,
5613 debugstr_wn(lpszHeaders, dwHeaderLength), dwHeaderLength, lpOptional, dwOptionalLength);
5615 request = (http_request_t*) get_handle_object( hHttpRequest );
5616 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5618 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5619 goto lend;
5622 session = request->session;
5623 if (NULL == session || session->hdr.htype != WH_HHTTPSESSION)
5625 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5626 goto lend;
5629 hIC = session->appInfo;
5630 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
5632 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5633 goto lend;
5636 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5638 send_request_task_t *task;
5640 task = alloc_async_task(&request->hdr, AsyncHttpSendRequestProc, sizeof(*task));
5641 if (lpszHeaders)
5643 DWORD size;
5645 if (dwHeaderLength == ~0u) size = (strlenW(lpszHeaders) + 1) * sizeof(WCHAR);
5646 else size = dwHeaderLength * sizeof(WCHAR);
5648 task->headers = heap_alloc(size);
5649 memcpy(task->headers, lpszHeaders, size);
5651 else
5652 task->headers = NULL;
5653 task->headers_len = dwHeaderLength;
5654 task->optional = lpOptional;
5655 task->optional_len = dwOptionalLength;
5656 task->content_len = dwOptionalLength;
5657 task->end_request = TRUE;
5659 INTERNET_AsyncCall(&task->hdr);
5660 res = ERROR_IO_PENDING;
5662 else
5664 res = HTTP_HttpSendRequestW(request, lpszHeaders,
5665 dwHeaderLength, lpOptional, dwOptionalLength,
5666 dwOptionalLength, TRUE);
5668 lend:
5669 if( request )
5670 WININET_Release( &request->hdr );
5672 SetLastError(res);
5673 return res == ERROR_SUCCESS;
5676 /***********************************************************************
5677 * HttpSendRequestA (WININET.@)
5679 * Sends the specified request to the HTTP server
5681 * RETURNS
5682 * TRUE on success
5683 * FALSE on failure
5686 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
5687 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
5689 BOOL result;
5690 LPWSTR szHeaders=NULL;
5691 DWORD nLen=dwHeaderLength;
5692 if(lpszHeaders!=NULL)
5694 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
5695 szHeaders = heap_alloc(nLen*sizeof(WCHAR));
5696 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
5698 result = HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
5699 heap_free(szHeaders);
5700 return result;
5703 /***********************************************************************
5704 * HTTPSESSION_Destroy (internal)
5706 * Deallocate session handle
5709 static void HTTPSESSION_Destroy(object_header_t *hdr)
5711 http_session_t *session = (http_session_t*) hdr;
5713 TRACE("%p\n", session);
5715 WININET_Release(&session->appInfo->hdr);
5717 heap_free(session->hostName);
5718 heap_free(session->password);
5719 heap_free(session->userName);
5722 static DWORD HTTPSESSION_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
5724 http_session_t *ses = (http_session_t *)hdr;
5726 switch(option) {
5727 case INTERNET_OPTION_HANDLE_TYPE:
5728 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
5730 if (*size < sizeof(ULONG))
5731 return ERROR_INSUFFICIENT_BUFFER;
5733 *size = sizeof(DWORD);
5734 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_CONNECT_HTTP;
5735 return ERROR_SUCCESS;
5736 case INTERNET_OPTION_CONNECT_TIMEOUT:
5737 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
5739 if (*size < sizeof(DWORD))
5740 return ERROR_INSUFFICIENT_BUFFER;
5742 *size = sizeof(DWORD);
5743 *(DWORD *)buffer = ses->connect_timeout;
5744 return ERROR_SUCCESS;
5746 case INTERNET_OPTION_SEND_TIMEOUT:
5747 TRACE("INTERNET_OPTION_SEND_TIMEOUT\n");
5749 if (*size < sizeof(DWORD))
5750 return ERROR_INSUFFICIENT_BUFFER;
5752 *size = sizeof(DWORD);
5753 *(DWORD *)buffer = ses->send_timeout;
5754 return ERROR_SUCCESS;
5756 case INTERNET_OPTION_RECEIVE_TIMEOUT:
5757 TRACE("INTERNET_OPTION_RECEIVE_TIMEOUT\n");
5759 if (*size < sizeof(DWORD))
5760 return ERROR_INSUFFICIENT_BUFFER;
5762 *size = sizeof(DWORD);
5763 *(DWORD *)buffer = ses->receive_timeout;
5764 return ERROR_SUCCESS;
5767 return INET_QueryOption(hdr, option, buffer, size, unicode);
5770 static DWORD HTTPSESSION_SetOption(object_header_t *hdr, DWORD option, void *buffer, DWORD size)
5772 http_session_t *ses = (http_session_t*)hdr;
5774 switch(option) {
5775 case INTERNET_OPTION_USERNAME:
5777 heap_free(ses->userName);
5778 if (!(ses->userName = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5779 return ERROR_SUCCESS;
5781 case INTERNET_OPTION_PASSWORD:
5783 heap_free(ses->password);
5784 if (!(ses->password = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5785 return ERROR_SUCCESS;
5787 case INTERNET_OPTION_PROXY_USERNAME:
5789 heap_free(ses->appInfo->proxyUsername);
5790 if (!(ses->appInfo->proxyUsername = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5791 return ERROR_SUCCESS;
5793 case INTERNET_OPTION_PROXY_PASSWORD:
5795 heap_free(ses->appInfo->proxyPassword);
5796 if (!(ses->appInfo->proxyPassword = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5797 return ERROR_SUCCESS;
5799 case INTERNET_OPTION_CONNECT_TIMEOUT:
5801 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5802 ses->connect_timeout = *(DWORD *)buffer;
5803 return ERROR_SUCCESS;
5805 case INTERNET_OPTION_SEND_TIMEOUT:
5807 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5808 ses->send_timeout = *(DWORD *)buffer;
5809 return ERROR_SUCCESS;
5811 case INTERNET_OPTION_RECEIVE_TIMEOUT:
5813 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5814 ses->receive_timeout = *(DWORD *)buffer;
5815 return ERROR_SUCCESS;
5817 default: break;
5820 return INET_SetOption(hdr, option, buffer, size);
5823 static const object_vtbl_t HTTPSESSIONVtbl = {
5824 HTTPSESSION_Destroy,
5825 NULL,
5826 HTTPSESSION_QueryOption,
5827 HTTPSESSION_SetOption,
5828 NULL,
5829 NULL,
5830 NULL,
5831 NULL,
5832 NULL
5836 /***********************************************************************
5837 * HTTP_Connect (internal)
5839 * Create http session handle
5841 * RETURNS
5842 * HINTERNET a session handle on success
5843 * NULL on failure
5846 DWORD HTTP_Connect(appinfo_t *hIC, LPCWSTR lpszServerName,
5847 INTERNET_PORT serverPort, LPCWSTR lpszUserName,
5848 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
5849 DWORD dwInternalFlags, HINTERNET *ret)
5851 http_session_t *session = NULL;
5853 TRACE("-->\n");
5855 if (!lpszServerName || !lpszServerName[0])
5856 return ERROR_INVALID_PARAMETER;
5858 assert( hIC->hdr.htype == WH_HINIT );
5860 session = alloc_object(&hIC->hdr, &HTTPSESSIONVtbl, sizeof(http_session_t));
5861 if (!session)
5862 return ERROR_OUTOFMEMORY;
5865 * According to my tests. The name is not resolved until a request is sent
5868 session->hdr.htype = WH_HHTTPSESSION;
5869 session->hdr.dwFlags = dwFlags;
5870 session->hdr.dwContext = dwContext;
5871 session->hdr.dwInternalFlags |= dwInternalFlags;
5873 WININET_AddRef( &hIC->hdr );
5874 session->appInfo = hIC;
5875 list_add_head( &hIC->hdr.children, &session->hdr.entry );
5877 session->hostName = heap_strdupW(lpszServerName);
5878 if (lpszUserName && lpszUserName[0])
5879 session->userName = heap_strdupW(lpszUserName);
5880 if (lpszPassword && lpszPassword[0])
5881 session->password = heap_strdupW(lpszPassword);
5882 session->hostPort = serverPort;
5883 session->connect_timeout = hIC->connect_timeout;
5884 session->send_timeout = 0;
5885 session->receive_timeout = 0;
5887 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
5888 if (!(session->hdr.dwInternalFlags & INET_OPENURL))
5890 INTERNET_SendCallback(&hIC->hdr, dwContext,
5891 INTERNET_STATUS_HANDLE_CREATED, &session->hdr.hInternet,
5892 sizeof(HINTERNET));
5896 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
5897 * windows
5900 TRACE("%p --> %p\n", hIC, session);
5902 *ret = session->hdr.hInternet;
5903 return ERROR_SUCCESS;
5906 /***********************************************************************
5907 * HTTP_clear_response_headers (internal)
5909 * clear out any old response headers
5911 static void HTTP_clear_response_headers( http_request_t *request )
5913 DWORD i;
5915 EnterCriticalSection( &request->headers_section );
5917 for( i=0; i<request->nCustHeaders; i++)
5919 if( !request->custHeaders[i].lpszField )
5920 continue;
5921 if( !request->custHeaders[i].lpszValue )
5922 continue;
5923 if ( request->custHeaders[i].wFlags & HDR_ISREQUEST )
5924 continue;
5925 HTTP_DeleteCustomHeader( request, i );
5926 i--;
5929 LeaveCriticalSection( &request->headers_section );
5932 /***********************************************************************
5933 * HTTP_GetResponseHeaders (internal)
5935 * Read server response
5937 * RETURNS
5939 * TRUE on success
5940 * FALSE on error
5942 static DWORD HTTP_GetResponseHeaders(http_request_t *request, INT *len)
5944 INT cbreaks = 0;
5945 WCHAR buffer[MAX_REPLY_LEN];
5946 DWORD buflen = MAX_REPLY_LEN;
5947 INT rc = 0;
5948 char bufferA[MAX_REPLY_LEN];
5949 LPWSTR status_code = NULL, status_text = NULL;
5950 DWORD res = ERROR_HTTP_INVALID_SERVER_RESPONSE;
5951 BOOL codeHundred = FALSE;
5953 TRACE("-->\n");
5955 if(!is_valid_netconn(request->netconn))
5956 goto lend;
5958 /* clear old response headers (eg. from a redirect response) */
5959 HTTP_clear_response_headers( request );
5961 NETCON_set_timeout( request->netconn, FALSE, request->receive_timeout );
5962 do {
5964 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
5966 buflen = MAX_REPLY_LEN;
5967 if ((res = read_line(request, bufferA, &buflen)))
5968 goto lend;
5970 if (!buflen) goto lend;
5972 rc += buflen;
5973 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
5974 /* check is this a status code line? */
5975 if (!strncmpW(buffer, g_szHttp1_0, 4))
5977 /* split the version from the status code */
5978 status_code = strchrW( buffer, ' ' );
5979 if( !status_code )
5980 goto lend;
5981 *status_code++=0;
5983 /* split the status code from the status text */
5984 status_text = strchrW( status_code, ' ' );
5985 if( status_text )
5986 *status_text++=0;
5988 request->status_code = atoiW(status_code);
5990 TRACE("version [%s] status code [%s] status text [%s]\n",
5991 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
5993 codeHundred = request->status_code == HTTP_STATUS_CONTINUE;
5995 else if (!codeHundred)
5997 WARN("No status line at head of response (%s)\n", debugstr_w(buffer));
5999 heap_free(request->version);
6000 heap_free(request->statusText);
6002 request->status_code = HTTP_STATUS_OK;
6003 request->version = heap_strdupW(g_szHttp1_0);
6004 request->statusText = heap_strdupW(szOK);
6006 goto lend;
6008 } while (codeHundred);
6010 /* Add status code */
6011 HTTP_ProcessHeader(request, szStatus, status_code,
6012 HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_ADD);
6014 heap_free(request->version);
6015 heap_free(request->statusText);
6017 request->version = heap_strdupW(buffer);
6018 request->statusText = heap_strdupW(status_text ? status_text : emptyW);
6020 /* Restore the spaces */
6021 *(status_code-1) = ' ';
6022 if (status_text)
6023 *(status_text-1) = ' ';
6025 /* Parse each response line */
6028 buflen = MAX_REPLY_LEN;
6029 if (!read_line(request, bufferA, &buflen) && buflen)
6031 LPWSTR * pFieldAndValue;
6033 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
6035 if (!bufferA[0]) break;
6036 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
6038 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
6039 if (pFieldAndValue)
6041 HTTP_ProcessHeader(request, pFieldAndValue[0], pFieldAndValue[1],
6042 HTTP_ADDREQ_FLAG_ADD );
6043 HTTP_FreeTokens(pFieldAndValue);
6046 else
6048 cbreaks++;
6049 if (cbreaks >= 2)
6050 break;
6052 }while(1);
6054 res = ERROR_SUCCESS;
6056 lend:
6058 *len = rc;
6059 TRACE("<--\n");
6060 return res;
6063 /***********************************************************************
6064 * HTTP_InterpretHttpHeader (internal)
6066 * Parse server response
6068 * RETURNS
6070 * Pointer to array of field, value, NULL on success.
6071 * NULL on error.
6073 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
6075 LPWSTR * pTokenPair;
6076 LPWSTR pszColon;
6077 INT len;
6079 pTokenPair = heap_alloc_zero(sizeof(*pTokenPair)*3);
6081 pszColon = strchrW(buffer, ':');
6082 /* must have two tokens */
6083 if (!pszColon)
6085 HTTP_FreeTokens(pTokenPair);
6086 if (buffer[0])
6087 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
6088 return NULL;
6091 pTokenPair[0] = heap_alloc((pszColon - buffer + 1) * sizeof(WCHAR));
6092 if (!pTokenPair[0])
6094 HTTP_FreeTokens(pTokenPair);
6095 return NULL;
6097 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
6098 pTokenPair[0][pszColon - buffer] = '\0';
6100 /* skip colon */
6101 pszColon++;
6102 len = strlenW(pszColon);
6103 pTokenPair[1] = heap_alloc((len + 1) * sizeof(WCHAR));
6104 if (!pTokenPair[1])
6106 HTTP_FreeTokens(pTokenPair);
6107 return NULL;
6109 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
6111 strip_spaces(pTokenPair[0]);
6112 strip_spaces(pTokenPair[1]);
6114 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
6115 return pTokenPair;
6118 /***********************************************************************
6119 * HTTP_ProcessHeader (internal)
6121 * Stuff header into header tables according to <dwModifier>
6125 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
6127 static DWORD HTTP_ProcessHeader(http_request_t *request, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
6129 LPHTTPHEADERW lphttpHdr = NULL;
6130 INT index;
6131 BOOL request_only = !!(dwModifier & HTTP_ADDHDR_FLAG_REQ);
6132 DWORD res = ERROR_HTTP_INVALID_HEADER;
6134 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), dwModifier);
6136 EnterCriticalSection( &request->headers_section );
6138 /* REPLACE wins out over ADD */
6139 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
6140 dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
6142 if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
6143 index = -1;
6144 else
6145 index = HTTP_GetCustomHeaderIndex(request, field, 0, request_only);
6147 if (index >= 0)
6149 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
6151 LeaveCriticalSection( &request->headers_section );
6152 return ERROR_HTTP_INVALID_HEADER;
6154 lphttpHdr = &request->custHeaders[index];
6156 else if (value)
6158 HTTPHEADERW hdr;
6160 hdr.lpszField = (LPWSTR)field;
6161 hdr.lpszValue = (LPWSTR)value;
6162 hdr.wFlags = hdr.wCount = 0;
6164 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6165 hdr.wFlags |= HDR_ISREQUEST;
6167 res = HTTP_InsertCustomHeader(request, &hdr);
6168 LeaveCriticalSection( &request->headers_section );
6169 return res;
6171 /* no value to delete */
6172 else
6174 LeaveCriticalSection( &request->headers_section );
6175 return ERROR_SUCCESS;
6178 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6179 lphttpHdr->wFlags |= HDR_ISREQUEST;
6180 else
6181 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
6183 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
6185 HTTP_DeleteCustomHeader( request, index );
6187 if (value && value[0])
6189 HTTPHEADERW hdr;
6191 hdr.lpszField = (LPWSTR)field;
6192 hdr.lpszValue = (LPWSTR)value;
6193 hdr.wFlags = hdr.wCount = 0;
6195 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6196 hdr.wFlags |= HDR_ISREQUEST;
6198 res = HTTP_InsertCustomHeader(request, &hdr);
6199 LeaveCriticalSection( &request->headers_section );
6200 return res;
6203 LeaveCriticalSection( &request->headers_section );
6204 return ERROR_SUCCESS;
6206 else if (dwModifier & COALESCEFLAGS)
6208 LPWSTR lpsztmp;
6209 WCHAR ch = 0;
6210 INT len = 0;
6211 INT origlen = strlenW(lphttpHdr->lpszValue);
6212 INT valuelen = strlenW(value);
6214 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
6216 ch = ',';
6217 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
6219 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
6221 ch = ';';
6222 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
6225 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
6227 lpsztmp = heap_realloc(lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
6228 if (lpsztmp)
6230 lphttpHdr->lpszValue = lpsztmp;
6231 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
6232 if (ch > 0)
6234 lphttpHdr->lpszValue[origlen] = ch;
6235 origlen++;
6236 lphttpHdr->lpszValue[origlen] = ' ';
6237 origlen++;
6240 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
6241 lphttpHdr->lpszValue[len] = '\0';
6242 res = ERROR_SUCCESS;
6244 else
6246 WARN("heap_realloc (%d bytes) failed\n",len+1);
6247 res = ERROR_OUTOFMEMORY;
6250 TRACE("<-- %d\n", res);
6251 LeaveCriticalSection( &request->headers_section );
6252 return res;
6255 /***********************************************************************
6256 * HTTP_GetCustomHeaderIndex (internal)
6258 * Return index of custom header from header array
6259 * Headers section must be held
6261 static INT HTTP_GetCustomHeaderIndex(http_request_t *request, LPCWSTR lpszField,
6262 int requested_index, BOOL request_only)
6264 DWORD index;
6266 TRACE("%s, %d, %d\n", debugstr_w(lpszField), requested_index, request_only);
6268 for (index = 0; index < request->nCustHeaders; index++)
6270 if (strcmpiW(request->custHeaders[index].lpszField, lpszField))
6271 continue;
6273 if (request_only && !(request->custHeaders[index].wFlags & HDR_ISREQUEST))
6274 continue;
6276 if (!request_only && (request->custHeaders[index].wFlags & HDR_ISREQUEST))
6277 continue;
6279 if (requested_index == 0)
6280 break;
6281 requested_index --;
6284 if (index >= request->nCustHeaders)
6285 index = -1;
6287 TRACE("Return: %d\n", index);
6288 return index;
6292 /***********************************************************************
6293 * HTTP_InsertCustomHeader (internal)
6295 * Insert header into array
6296 * Headers section must be held
6298 static DWORD HTTP_InsertCustomHeader(http_request_t *request, LPHTTPHEADERW lpHdr)
6300 INT count;
6301 LPHTTPHEADERW lph = NULL;
6303 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
6304 count = request->nCustHeaders + 1;
6305 if (count > 1)
6306 lph = heap_realloc_zero(request->custHeaders, sizeof(HTTPHEADERW) * count);
6307 else
6308 lph = heap_alloc_zero(sizeof(HTTPHEADERW) * count);
6310 if (!lph)
6311 return ERROR_OUTOFMEMORY;
6313 request->custHeaders = lph;
6314 request->custHeaders[count-1].lpszField = heap_strdupW(lpHdr->lpszField);
6315 request->custHeaders[count-1].lpszValue = heap_strdupW(lpHdr->lpszValue);
6316 request->custHeaders[count-1].wFlags = lpHdr->wFlags;
6317 request->custHeaders[count-1].wCount= lpHdr->wCount;
6318 request->nCustHeaders++;
6320 return ERROR_SUCCESS;
6324 /***********************************************************************
6325 * HTTP_DeleteCustomHeader (internal)
6327 * Delete header from array
6328 * If this function is called, the index may change.
6329 * Headers section must be held
6331 static BOOL HTTP_DeleteCustomHeader(http_request_t *request, DWORD index)
6333 if( request->nCustHeaders <= 0 )
6334 return FALSE;
6335 if( index >= request->nCustHeaders )
6336 return FALSE;
6337 request->nCustHeaders--;
6339 heap_free(request->custHeaders[index].lpszField);
6340 heap_free(request->custHeaders[index].lpszValue);
6342 memmove( &request->custHeaders[index], &request->custHeaders[index+1],
6343 (request->nCustHeaders - index)* sizeof(HTTPHEADERW) );
6344 memset( &request->custHeaders[request->nCustHeaders], 0, sizeof(HTTPHEADERW) );
6346 return TRUE;
6350 /***********************************************************************
6351 * IsHostInProxyBypassList (@)
6353 * Undocumented
6356 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
6358 FIXME("STUB: flags=%d host=%s length=%d\n",flags,szHost,length);
6359 return FALSE;