winex11: Store the process name at startup to avoid grabbing the loader lock again.
[wine.git] / dlls / wininet / http.c
blob078a5cb95c6ce3484b3bd6924dff56f775774b72
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 { &connection_pool_debug.ProcessLocksList, &connection_pool_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 struct data_stream_vtbl_t {
384 BOOL (*end_of_data)(data_stream_t*,http_request_t*);
385 DWORD (*read)(data_stream_t*,http_request_t*,BYTE*,DWORD,DWORD*,BOOL);
386 BOOL (*drain_content)(data_stream_t*,http_request_t*);
387 void (*destroy)(data_stream_t*);
390 typedef struct {
391 data_stream_t data_stream;
393 BYTE buf[READ_BUFFER_SIZE];
394 DWORD buf_size;
395 DWORD buf_pos;
396 DWORD chunk_size;
398 enum {
399 CHUNKED_STREAM_STATE_READING_CHUNK_SIZE,
400 CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE,
401 CHUNKED_STREAM_STATE_READING_CHUNK,
402 CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA,
403 CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END,
404 CHUNKED_STREAM_STATE_END_OF_STREAM
405 } state;
406 } chunked_stream_t;
408 static inline void destroy_data_stream(data_stream_t *stream)
410 stream->vtbl->destroy(stream);
413 static void reset_data_stream(http_request_t *req)
415 destroy_data_stream(req->data_stream);
416 req->data_stream = &req->netconn_stream.data_stream;
417 req->read_pos = req->read_size = req->netconn_stream.content_read = 0;
418 req->read_gzip = FALSE;
421 static void remove_header( http_request_t *request, const WCHAR *str, BOOL from_request )
423 int index;
424 EnterCriticalSection( &request->headers_section );
425 index = HTTP_GetCustomHeaderIndex( request, str, 0, from_request );
426 if (index != -1) HTTP_DeleteCustomHeader( request, index );
427 LeaveCriticalSection( &request->headers_section );
430 #ifdef HAVE_ZLIB
432 typedef struct {
433 data_stream_t stream;
434 data_stream_t *parent_stream;
435 z_stream zstream;
436 BYTE buf[READ_BUFFER_SIZE];
437 DWORD buf_size;
438 DWORD buf_pos;
439 BOOL end_of_data;
440 } gzip_stream_t;
442 static BOOL gzip_end_of_data(data_stream_t *stream, http_request_t *req)
444 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
445 return gzip_stream->end_of_data
446 || (!gzip_stream->buf_size && gzip_stream->parent_stream->vtbl->end_of_data(gzip_stream->parent_stream, req));
449 static DWORD gzip_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
450 DWORD *read, BOOL allow_blocking)
452 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
453 z_stream *zstream = &gzip_stream->zstream;
454 DWORD current_read, ret_read = 0;
455 int zres;
456 DWORD res = ERROR_SUCCESS;
458 TRACE("(%d %x)\n", size, allow_blocking);
460 while(size && !gzip_stream->end_of_data) {
461 if(!gzip_stream->buf_size) {
462 if(gzip_stream->buf_pos) {
463 if(gzip_stream->buf_size)
464 memmove(gzip_stream->buf, gzip_stream->buf+gzip_stream->buf_pos, gzip_stream->buf_size);
465 gzip_stream->buf_pos = 0;
467 res = gzip_stream->parent_stream->vtbl->read(gzip_stream->parent_stream, req, gzip_stream->buf+gzip_stream->buf_size,
468 sizeof(gzip_stream->buf)-gzip_stream->buf_size, &current_read, allow_blocking);
469 if(res != ERROR_SUCCESS)
470 break;
472 gzip_stream->buf_size += current_read;
473 if(!current_read) {
474 WARN("unexpected end of data\n");
475 gzip_stream->end_of_data = TRUE;
476 break;
480 zstream->next_in = gzip_stream->buf+gzip_stream->buf_pos;
481 zstream->avail_in = gzip_stream->buf_size;
482 zstream->next_out = buf+ret_read;
483 zstream->avail_out = size;
484 zres = inflate(&gzip_stream->zstream, 0);
485 current_read = size - zstream->avail_out;
486 size -= current_read;
487 ret_read += current_read;
488 gzip_stream->buf_size -= zstream->next_in - (gzip_stream->buf+gzip_stream->buf_pos);
489 gzip_stream->buf_pos = zstream->next_in-gzip_stream->buf;
490 if(zres == Z_STREAM_END) {
491 TRACE("end of data\n");
492 gzip_stream->end_of_data = TRUE;
493 inflateEnd(zstream);
494 }else if(zres != Z_OK) {
495 WARN("inflate failed %d: %s\n", zres, debugstr_a(zstream->msg));
496 if(!ret_read)
497 res = ERROR_INTERNET_DECODING_FAILED;
498 break;
501 if(ret_read)
502 allow_blocking = FALSE;
505 TRACE("read %u bytes\n", ret_read);
506 if(ret_read)
507 res = ERROR_SUCCESS;
508 *read = ret_read;
509 return res;
512 static BOOL gzip_drain_content(data_stream_t *stream, http_request_t *req)
514 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
515 return gzip_stream->parent_stream->vtbl->drain_content(gzip_stream->parent_stream, req);
518 static void gzip_destroy(data_stream_t *stream)
520 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
522 destroy_data_stream(gzip_stream->parent_stream);
524 if(!gzip_stream->end_of_data)
525 inflateEnd(&gzip_stream->zstream);
526 heap_free(gzip_stream);
529 static const data_stream_vtbl_t gzip_stream_vtbl = {
530 gzip_end_of_data,
531 gzip_read,
532 gzip_drain_content,
533 gzip_destroy
536 static voidpf wininet_zalloc(voidpf opaque, uInt items, uInt size)
538 return heap_alloc(items*size);
541 static void wininet_zfree(voidpf opaque, voidpf address)
543 heap_free(address);
546 static DWORD init_gzip_stream(http_request_t *req, BOOL is_gzip)
548 gzip_stream_t *gzip_stream;
549 int zres;
551 gzip_stream = heap_alloc_zero(sizeof(gzip_stream_t));
552 if(!gzip_stream)
553 return ERROR_OUTOFMEMORY;
555 gzip_stream->stream.vtbl = &gzip_stream_vtbl;
556 gzip_stream->zstream.zalloc = wininet_zalloc;
557 gzip_stream->zstream.zfree = wininet_zfree;
559 zres = inflateInit2(&gzip_stream->zstream, is_gzip ? 0x1f : -15);
560 if(zres != Z_OK) {
561 ERR("inflateInit failed: %d\n", zres);
562 heap_free(gzip_stream);
563 return ERROR_OUTOFMEMORY;
566 remove_header(req, szContent_Length, FALSE);
568 if(req->read_size) {
569 memcpy(gzip_stream->buf, req->read_buf+req->read_pos, req->read_size);
570 gzip_stream->buf_size = req->read_size;
571 req->read_pos = req->read_size = 0;
574 req->read_gzip = TRUE;
575 gzip_stream->parent_stream = req->data_stream;
576 req->data_stream = &gzip_stream->stream;
577 return ERROR_SUCCESS;
580 #else
582 static DWORD init_gzip_stream(http_request_t *req, BOOL is_gzip)
584 ERR("gzip stream not supported, missing zlib.\n");
585 return ERROR_SUCCESS;
588 #endif
590 /***********************************************************************
591 * HTTP_FreeTokens (internal)
593 * Frees table of pointers.
595 static void HTTP_FreeTokens(LPWSTR * token_array)
597 int i;
598 for (i = 0; token_array[i]; i++) heap_free(token_array[i]);
599 heap_free(token_array);
602 static void HTTP_FixURL(http_request_t *request)
604 static const WCHAR szSlash[] = { '/',0 };
605 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
607 /* If we don't have a path we set it to root */
608 if (NULL == request->path)
609 request->path = heap_strdupW(szSlash);
610 else /* remove \r and \n*/
612 int nLen = strlenW(request->path);
613 while ((nLen >0 ) && ((request->path[nLen-1] == '\r')||(request->path[nLen-1] == '\n')))
615 nLen--;
616 request->path[nLen]='\0';
618 /* Replace '\' with '/' */
619 while (nLen>0) {
620 nLen--;
621 if (request->path[nLen] == '\\') request->path[nLen]='/';
625 if(CSTR_EQUAL != CompareStringW( LOCALE_INVARIANT, NORM_IGNORECASE,
626 request->path, strlenW(request->path), szHttp, strlenW(szHttp) )
627 && request->path[0] != '/') /* not an absolute path ?? --> fix it !! */
629 WCHAR *fixurl = heap_alloc((strlenW(request->path) + 2)*sizeof(WCHAR));
630 *fixurl = '/';
631 strcpyW(fixurl + 1, request->path);
632 heap_free( request->path );
633 request->path = fixurl;
637 static WCHAR* build_request_header(http_request_t *request, const WCHAR *verb,
638 const WCHAR *path, const WCHAR *version, BOOL use_cr)
640 static const WCHAR szSpace[] = {' ',0};
641 static const WCHAR szColon[] = {':',' ',0};
642 static const WCHAR szCr[] = {'\r',0};
643 static const WCHAR szLf[] = {'\n',0};
644 LPWSTR requestString;
645 DWORD len, n;
646 LPCWSTR *req;
647 UINT i;
649 EnterCriticalSection( &request->headers_section );
651 /* allocate space for an array of all the string pointers to be added */
652 len = request->nCustHeaders * 5 + 10;
653 if (!(req = heap_alloc( len * sizeof(const WCHAR *) )))
655 LeaveCriticalSection( &request->headers_section );
656 return NULL;
659 /* add the verb, path and HTTP version string */
660 n = 0;
661 req[n++] = verb;
662 req[n++] = szSpace;
663 req[n++] = path;
664 req[n++] = szSpace;
665 req[n++] = version;
666 if (use_cr)
667 req[n++] = szCr;
668 req[n++] = szLf;
670 /* Append custom request headers */
671 for (i = 0; i < request->nCustHeaders; i++)
673 if (request->custHeaders[i].wFlags & HDR_ISREQUEST)
675 req[n++] = request->custHeaders[i].lpszField;
676 req[n++] = szColon;
677 req[n++] = request->custHeaders[i].lpszValue;
678 if (use_cr)
679 req[n++] = szCr;
680 req[n++] = szLf;
682 TRACE("Adding custom header %s (%s)\n",
683 debugstr_w(request->custHeaders[i].lpszField),
684 debugstr_w(request->custHeaders[i].lpszValue));
687 if (use_cr)
688 req[n++] = szCr;
689 req[n++] = szLf;
690 req[n] = NULL;
692 requestString = HTTP_build_req( req, 4 );
693 heap_free( req );
694 LeaveCriticalSection( &request->headers_section );
695 return requestString;
698 static WCHAR* build_response_header(http_request_t *request, BOOL use_cr)
700 static const WCHAR colonW[] = { ':',' ',0 };
701 static const WCHAR crW[] = { '\r',0 };
702 static const WCHAR lfW[] = { '\n',0 };
703 static const WCHAR status_fmt[] = { ' ','%','u',' ',0 };
704 const WCHAR **req;
705 WCHAR *ret, buf[14];
706 DWORD i, n = 0;
708 EnterCriticalSection( &request->headers_section );
710 if (!(req = heap_alloc( (request->nCustHeaders * 5 + 8) * sizeof(WCHAR *) )))
712 LeaveCriticalSection( &request->headers_section );
713 return NULL;
716 if (request->status_code)
718 req[n++] = request->version;
719 sprintfW(buf, status_fmt, request->status_code);
720 req[n++] = buf;
721 req[n++] = request->statusText;
722 if (use_cr)
723 req[n++] = crW;
724 req[n++] = lfW;
727 for(i = 0; i < request->nCustHeaders; i++)
729 if(!(request->custHeaders[i].wFlags & HDR_ISREQUEST)
730 && strcmpW(request->custHeaders[i].lpszField, szStatus))
732 req[n++] = request->custHeaders[i].lpszField;
733 req[n++] = colonW;
734 req[n++] = request->custHeaders[i].lpszValue;
735 if(use_cr)
736 req[n++] = crW;
737 req[n++] = lfW;
739 TRACE("Adding custom header %s (%s)\n",
740 debugstr_w(request->custHeaders[i].lpszField),
741 debugstr_w(request->custHeaders[i].lpszValue));
744 if(use_cr)
745 req[n++] = crW;
746 req[n++] = lfW;
747 req[n] = NULL;
749 ret = HTTP_build_req(req, 0);
750 heap_free(req);
751 LeaveCriticalSection( &request->headers_section );
752 return ret;
755 static void HTTP_ProcessCookies( http_request_t *request )
757 int HeaderIndex;
758 int numCookies = 0;
759 LPHTTPHEADERW setCookieHeader;
761 if(request->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES)
762 return;
764 EnterCriticalSection( &request->headers_section );
766 while((HeaderIndex = HTTP_GetCustomHeaderIndex(request, szSet_Cookie, numCookies++, FALSE)) != -1)
768 const WCHAR *data;
769 substr_t name;
771 setCookieHeader = &request->custHeaders[HeaderIndex];
773 if (!setCookieHeader->lpszValue)
774 continue;
776 data = strchrW(setCookieHeader->lpszValue, '=');
777 if(!data)
778 continue;
780 name = substr(setCookieHeader->lpszValue, data - setCookieHeader->lpszValue);
781 data++;
782 set_cookie(substrz(request->server->name), substrz(request->path), name, substrz(data), INTERNET_COOKIE_HTTPONLY);
785 LeaveCriticalSection( &request->headers_section );
788 static void strip_spaces(LPWSTR start)
790 LPWSTR str = start;
791 LPWSTR end;
793 while (*str == ' ')
794 str++;
796 if (str != start)
797 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
799 end = start + strlenW(start) - 1;
800 while (end >= start && *end == ' ')
802 *end = '\0';
803 end--;
807 static inline BOOL is_basic_auth_value( LPCWSTR pszAuthValue, LPWSTR *pszRealm )
809 static const WCHAR szBasic[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
810 static const WCHAR szRealm[] = {'r','e','a','l','m'}; /* Note: not nul-terminated */
811 BOOL is_basic;
812 is_basic = !strncmpiW(pszAuthValue, szBasic, ARRAYSIZE(szBasic)) &&
813 ((pszAuthValue[ARRAYSIZE(szBasic)] == ' ') || !pszAuthValue[ARRAYSIZE(szBasic)]);
814 if (is_basic && pszRealm)
816 LPCWSTR token;
817 LPCWSTR ptr = &pszAuthValue[ARRAYSIZE(szBasic)];
818 LPCWSTR realm;
819 ptr++;
820 *pszRealm=NULL;
821 token = strchrW(ptr,'=');
822 if (!token)
823 return TRUE;
824 realm = ptr;
825 while (*realm == ' ')
826 realm++;
827 if(!strncmpiW(realm, szRealm, ARRAYSIZE(szRealm)) &&
828 (realm[ARRAYSIZE(szRealm)] == ' ' || realm[ARRAYSIZE(szRealm)] == '='))
830 token++;
831 while (*token == ' ')
832 token++;
833 if (*token == '\0')
834 return TRUE;
835 *pszRealm = heap_strdupW(token);
836 strip_spaces(*pszRealm);
840 return is_basic;
843 static void destroy_authinfo( struct HttpAuthInfo *authinfo )
845 if (!authinfo) return;
847 if (SecIsValidHandle(&authinfo->ctx))
848 DeleteSecurityContext(&authinfo->ctx);
849 if (SecIsValidHandle(&authinfo->cred))
850 FreeCredentialsHandle(&authinfo->cred);
852 heap_free(authinfo->auth_data);
853 heap_free(authinfo->scheme);
854 heap_free(authinfo);
857 static UINT retrieve_cached_basic_authorization(const WCHAR *host, const WCHAR *realm, char **auth_data)
859 basicAuthorizationData *ad;
860 UINT rc = 0;
862 TRACE("Looking for authorization for %s:%s\n",debugstr_w(host),debugstr_w(realm));
864 EnterCriticalSection(&authcache_cs);
865 LIST_FOR_EACH_ENTRY(ad, &basicAuthorizationCache, basicAuthorizationData, entry)
867 if (!strcmpiW(host, ad->host) && (!realm || !strcmpW(realm, ad->realm)))
869 TRACE("Authorization found in cache\n");
870 *auth_data = heap_alloc(ad->authorizationLen);
871 memcpy(*auth_data,ad->authorization,ad->authorizationLen);
872 rc = ad->authorizationLen;
873 break;
876 LeaveCriticalSection(&authcache_cs);
877 return rc;
880 static void cache_basic_authorization(LPWSTR host, LPWSTR realm, LPSTR auth_data, UINT auth_data_len)
882 struct list *cursor;
883 basicAuthorizationData* ad = NULL;
885 TRACE("caching authorization for %s:%s = %s\n",debugstr_w(host),debugstr_w(realm),debugstr_an(auth_data,auth_data_len));
887 EnterCriticalSection(&authcache_cs);
888 LIST_FOR_EACH(cursor, &basicAuthorizationCache)
890 basicAuthorizationData *check = LIST_ENTRY(cursor,basicAuthorizationData,entry);
891 if (!strcmpiW(host,check->host) && !strcmpW(realm,check->realm))
893 ad = check;
894 break;
898 if (ad)
900 TRACE("Found match in cache, replacing\n");
901 heap_free(ad->authorization);
902 ad->authorization = heap_alloc(auth_data_len);
903 memcpy(ad->authorization, auth_data, auth_data_len);
904 ad->authorizationLen = auth_data_len;
906 else
908 ad = heap_alloc(sizeof(basicAuthorizationData));
909 ad->host = heap_strdupW(host);
910 ad->realm = heap_strdupW(realm);
911 ad->authorization = heap_alloc(auth_data_len);
912 memcpy(ad->authorization, auth_data, auth_data_len);
913 ad->authorizationLen = auth_data_len;
914 list_add_head(&basicAuthorizationCache,&ad->entry);
915 TRACE("authorization cached\n");
917 LeaveCriticalSection(&authcache_cs);
920 static BOOL retrieve_cached_authorization(LPWSTR host, LPWSTR scheme,
921 SEC_WINNT_AUTH_IDENTITY_W *nt_auth_identity)
923 authorizationData *ad;
925 TRACE("Looking for authorization for %s:%s\n", debugstr_w(host), debugstr_w(scheme));
927 EnterCriticalSection(&authcache_cs);
928 LIST_FOR_EACH_ENTRY(ad, &authorizationCache, authorizationData, entry) {
929 if(!strcmpiW(host, ad->host) && !strcmpiW(scheme, ad->scheme)) {
930 TRACE("Authorization found in cache\n");
932 nt_auth_identity->User = heap_strdupW(ad->user);
933 nt_auth_identity->Password = heap_strdupW(ad->password);
934 nt_auth_identity->Domain = heap_alloc(sizeof(WCHAR)*ad->domain_len);
935 if(!nt_auth_identity->User || !nt_auth_identity->Password ||
936 (!nt_auth_identity->Domain && ad->domain_len)) {
937 heap_free(nt_auth_identity->User);
938 heap_free(nt_auth_identity->Password);
939 heap_free(nt_auth_identity->Domain);
940 break;
943 nt_auth_identity->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
944 nt_auth_identity->UserLength = ad->user_len;
945 nt_auth_identity->PasswordLength = ad->password_len;
946 memcpy(nt_auth_identity->Domain, ad->domain, sizeof(WCHAR)*ad->domain_len);
947 nt_auth_identity->DomainLength = ad->domain_len;
948 LeaveCriticalSection(&authcache_cs);
949 return TRUE;
952 LeaveCriticalSection(&authcache_cs);
954 return FALSE;
957 static void cache_authorization(LPWSTR host, LPWSTR scheme,
958 SEC_WINNT_AUTH_IDENTITY_W *nt_auth_identity)
960 authorizationData *ad;
961 BOOL found = FALSE;
963 TRACE("Caching authorization for %s:%s\n", debugstr_w(host), debugstr_w(scheme));
965 EnterCriticalSection(&authcache_cs);
966 LIST_FOR_EACH_ENTRY(ad, &authorizationCache, authorizationData, entry)
967 if(!strcmpiW(host, ad->host) && !strcmpiW(scheme, ad->scheme)) {
968 found = TRUE;
969 break;
972 if(found) {
973 heap_free(ad->user);
974 heap_free(ad->password);
975 heap_free(ad->domain);
976 } else {
977 ad = heap_alloc(sizeof(authorizationData));
978 if(!ad) {
979 LeaveCriticalSection(&authcache_cs);
980 return;
983 ad->host = heap_strdupW(host);
984 ad->scheme = heap_strdupW(scheme);
985 list_add_head(&authorizationCache, &ad->entry);
988 ad->user = heap_strndupW(nt_auth_identity->User, nt_auth_identity->UserLength);
989 ad->password = heap_strndupW(nt_auth_identity->Password, nt_auth_identity->PasswordLength);
990 ad->domain = heap_strndupW(nt_auth_identity->Domain, nt_auth_identity->DomainLength);
991 ad->user_len = nt_auth_identity->UserLength;
992 ad->password_len = nt_auth_identity->PasswordLength;
993 ad->domain_len = nt_auth_identity->DomainLength;
995 if(!ad->host || !ad->scheme || !ad->user || !ad->password
996 || (nt_auth_identity->Domain && !ad->domain)) {
997 heap_free(ad->host);
998 heap_free(ad->scheme);
999 heap_free(ad->user);
1000 heap_free(ad->password);
1001 heap_free(ad->domain);
1002 list_remove(&ad->entry);
1003 heap_free(ad);
1006 LeaveCriticalSection(&authcache_cs);
1009 static BOOL HTTP_DoAuthorization( http_request_t *request, LPCWSTR pszAuthValue,
1010 struct HttpAuthInfo **ppAuthInfo,
1011 LPWSTR domain_and_username, LPWSTR password,
1012 LPWSTR host )
1014 SECURITY_STATUS sec_status;
1015 struct HttpAuthInfo *pAuthInfo = *ppAuthInfo;
1016 BOOL first = FALSE;
1017 LPWSTR szRealm = NULL;
1019 TRACE("%s\n", debugstr_w(pszAuthValue));
1021 if (!pAuthInfo)
1023 TimeStamp exp;
1025 first = TRUE;
1026 pAuthInfo = heap_alloc(sizeof(*pAuthInfo));
1027 if (!pAuthInfo)
1028 return FALSE;
1030 SecInvalidateHandle(&pAuthInfo->cred);
1031 SecInvalidateHandle(&pAuthInfo->ctx);
1032 memset(&pAuthInfo->exp, 0, sizeof(pAuthInfo->exp));
1033 pAuthInfo->attr = 0;
1034 pAuthInfo->auth_data = NULL;
1035 pAuthInfo->auth_data_len = 0;
1036 pAuthInfo->finished = FALSE;
1038 if (is_basic_auth_value(pszAuthValue,NULL))
1040 static const WCHAR szBasic[] = {'B','a','s','i','c',0};
1041 pAuthInfo->scheme = heap_strdupW(szBasic);
1042 if (!pAuthInfo->scheme)
1044 heap_free(pAuthInfo);
1045 return FALSE;
1048 else
1050 PVOID pAuthData;
1051 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity;
1053 pAuthInfo->scheme = heap_strdupW(pszAuthValue);
1054 if (!pAuthInfo->scheme)
1056 heap_free(pAuthInfo);
1057 return FALSE;
1060 if (domain_and_username)
1062 WCHAR *user = strchrW(domain_and_username, '\\');
1063 WCHAR *domain = domain_and_username;
1065 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
1067 pAuthData = &nt_auth_identity;
1069 if (user) user++;
1070 else
1072 user = domain_and_username;
1073 domain = NULL;
1076 nt_auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
1077 nt_auth_identity.User = user;
1078 nt_auth_identity.UserLength = strlenW(nt_auth_identity.User);
1079 nt_auth_identity.Domain = domain;
1080 nt_auth_identity.DomainLength = domain ? user - domain - 1 : 0;
1081 nt_auth_identity.Password = password;
1082 nt_auth_identity.PasswordLength = strlenW(nt_auth_identity.Password);
1084 cache_authorization(host, pAuthInfo->scheme, &nt_auth_identity);
1086 else if(retrieve_cached_authorization(host, pAuthInfo->scheme, &nt_auth_identity))
1087 pAuthData = &nt_auth_identity;
1088 else
1089 /* use default credentials */
1090 pAuthData = NULL;
1092 sec_status = AcquireCredentialsHandleW(NULL, pAuthInfo->scheme,
1093 SECPKG_CRED_OUTBOUND, NULL,
1094 pAuthData, NULL,
1095 NULL, &pAuthInfo->cred,
1096 &exp);
1098 if(pAuthData && !domain_and_username) {
1099 heap_free(nt_auth_identity.User);
1100 heap_free(nt_auth_identity.Domain);
1101 heap_free(nt_auth_identity.Password);
1104 if (sec_status == SEC_E_OK)
1106 PSecPkgInfoW sec_pkg_info;
1107 sec_status = QuerySecurityPackageInfoW(pAuthInfo->scheme, &sec_pkg_info);
1108 if (sec_status == SEC_E_OK)
1110 pAuthInfo->max_token = sec_pkg_info->cbMaxToken;
1111 FreeContextBuffer(sec_pkg_info);
1114 if (sec_status != SEC_E_OK)
1116 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
1117 debugstr_w(pAuthInfo->scheme), sec_status);
1118 heap_free(pAuthInfo->scheme);
1119 heap_free(pAuthInfo);
1120 return FALSE;
1123 *ppAuthInfo = pAuthInfo;
1125 else if (pAuthInfo->finished)
1126 return FALSE;
1128 if ((strlenW(pszAuthValue) < strlenW(pAuthInfo->scheme)) ||
1129 strncmpiW(pszAuthValue, pAuthInfo->scheme, strlenW(pAuthInfo->scheme)))
1131 ERR("authentication scheme changed from %s to %s\n",
1132 debugstr_w(pAuthInfo->scheme), debugstr_w(pszAuthValue));
1133 return FALSE;
1136 if (is_basic_auth_value(pszAuthValue,&szRealm))
1138 int userlen;
1139 int passlen;
1140 char *auth_data = NULL;
1141 UINT auth_data_len = 0;
1143 TRACE("basic authentication realm %s\n",debugstr_w(szRealm));
1145 if (!domain_and_username)
1147 if (host && szRealm)
1148 auth_data_len = retrieve_cached_basic_authorization(host, szRealm,&auth_data);
1149 if (auth_data_len == 0)
1151 heap_free(szRealm);
1152 return FALSE;
1155 else
1157 userlen = WideCharToMultiByte(CP_UTF8, 0, domain_and_username, lstrlenW(domain_and_username), NULL, 0, NULL, NULL);
1158 passlen = WideCharToMultiByte(CP_UTF8, 0, password, lstrlenW(password), NULL, 0, NULL, NULL);
1160 /* length includes a nul terminator, which will be re-used for the ':' */
1161 auth_data = heap_alloc(userlen + 1 + passlen);
1162 if (!auth_data)
1164 heap_free(szRealm);
1165 return FALSE;
1168 WideCharToMultiByte(CP_UTF8, 0, domain_and_username, -1, auth_data, userlen, NULL, NULL);
1169 auth_data[userlen] = ':';
1170 WideCharToMultiByte(CP_UTF8, 0, password, -1, &auth_data[userlen+1], passlen, NULL, NULL);
1171 auth_data_len = userlen + 1 + passlen;
1172 if (host && szRealm)
1173 cache_basic_authorization(host, szRealm, auth_data, auth_data_len);
1176 pAuthInfo->auth_data = auth_data;
1177 pAuthInfo->auth_data_len = auth_data_len;
1178 pAuthInfo->finished = TRUE;
1179 heap_free(szRealm);
1180 return TRUE;
1182 else
1184 LPCWSTR pszAuthData;
1185 SecBufferDesc out_desc, in_desc;
1186 SecBuffer out, in;
1187 unsigned char *buffer;
1188 ULONG context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE |
1189 ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE;
1191 in.BufferType = SECBUFFER_TOKEN;
1192 in.cbBuffer = 0;
1193 in.pvBuffer = NULL;
1195 in_desc.ulVersion = 0;
1196 in_desc.cBuffers = 1;
1197 in_desc.pBuffers = &in;
1199 pszAuthData = pszAuthValue + strlenW(pAuthInfo->scheme);
1200 if (*pszAuthData == ' ')
1202 pszAuthData++;
1203 in.cbBuffer = HTTP_DecodeBase64(pszAuthData, NULL);
1204 in.pvBuffer = heap_alloc(in.cbBuffer);
1205 HTTP_DecodeBase64(pszAuthData, in.pvBuffer);
1208 buffer = heap_alloc(pAuthInfo->max_token);
1210 out.BufferType = SECBUFFER_TOKEN;
1211 out.cbBuffer = pAuthInfo->max_token;
1212 out.pvBuffer = buffer;
1214 out_desc.ulVersion = 0;
1215 out_desc.cBuffers = 1;
1216 out_desc.pBuffers = &out;
1218 sec_status = InitializeSecurityContextW(first ? &pAuthInfo->cred : NULL,
1219 first ? NULL : &pAuthInfo->ctx,
1220 first ? request->server->name : NULL,
1221 context_req, 0, SECURITY_NETWORK_DREP,
1222 in.pvBuffer ? &in_desc : NULL,
1223 0, &pAuthInfo->ctx, &out_desc,
1224 &pAuthInfo->attr, &pAuthInfo->exp);
1225 if (sec_status == SEC_E_OK)
1227 pAuthInfo->finished = TRUE;
1228 pAuthInfo->auth_data = out.pvBuffer;
1229 pAuthInfo->auth_data_len = out.cbBuffer;
1230 TRACE("sending last auth packet\n");
1232 else if (sec_status == SEC_I_CONTINUE_NEEDED)
1234 pAuthInfo->auth_data = out.pvBuffer;
1235 pAuthInfo->auth_data_len = out.cbBuffer;
1236 TRACE("sending next auth packet\n");
1238 else
1240 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status);
1241 heap_free(out.pvBuffer);
1242 destroy_authinfo(pAuthInfo);
1243 *ppAuthInfo = NULL;
1244 return FALSE;
1248 return TRUE;
1251 /***********************************************************************
1252 * HTTP_HttpAddRequestHeadersW (internal)
1254 static DWORD HTTP_HttpAddRequestHeadersW(http_request_t *request,
1255 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1257 LPWSTR lpszStart;
1258 LPWSTR lpszEnd;
1259 LPWSTR buffer;
1260 DWORD len, res = ERROR_HTTP_INVALID_HEADER;
1262 TRACE("copying header: %s\n", debugstr_wn(lpszHeader, dwHeaderLength));
1264 if( dwHeaderLength == ~0U )
1265 len = strlenW(lpszHeader);
1266 else
1267 len = dwHeaderLength;
1268 buffer = heap_alloc(sizeof(WCHAR)*(len+1));
1269 lstrcpynW( buffer, lpszHeader, len + 1);
1271 lpszStart = buffer;
1275 LPWSTR * pFieldAndValue;
1277 lpszEnd = lpszStart;
1279 while (*lpszEnd != '\0')
1281 if (*lpszEnd == '\r' || *lpszEnd == '\n')
1282 break;
1283 lpszEnd++;
1286 if (*lpszStart == '\0')
1287 break;
1289 if (*lpszEnd == '\r' || *lpszEnd == '\n')
1291 *lpszEnd = '\0';
1292 lpszEnd++; /* Jump over newline */
1294 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
1295 if (*lpszStart == '\0')
1297 /* Skip 0-length headers */
1298 lpszStart = lpszEnd;
1299 res = ERROR_SUCCESS;
1300 continue;
1302 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
1303 if (pFieldAndValue)
1305 res = HTTP_ProcessHeader(request, pFieldAndValue[0],
1306 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
1307 HTTP_FreeTokens(pFieldAndValue);
1310 lpszStart = lpszEnd;
1311 } while (res == ERROR_SUCCESS);
1313 heap_free(buffer);
1314 return res;
1317 /***********************************************************************
1318 * HttpAddRequestHeadersW (WININET.@)
1320 * Adds one or more HTTP header to the request handler
1322 * NOTE
1323 * On Windows if dwHeaderLength includes the trailing '\0', then
1324 * HttpAddRequestHeadersW() adds it too. However this results in an
1325 * invalid HTTP header which is rejected by some servers so we probably
1326 * don't need to match Windows on that point.
1328 * RETURNS
1329 * TRUE on success
1330 * FALSE on failure
1333 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
1334 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1336 http_request_t *request;
1337 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1339 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_wn(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
1341 if (!lpszHeader)
1342 return TRUE;
1344 request = (http_request_t*) get_handle_object( hHttpRequest );
1345 if (request && request->hdr.htype == WH_HHTTPREQ)
1346 res = HTTP_HttpAddRequestHeadersW( request, lpszHeader, dwHeaderLength, dwModifier );
1347 if( request )
1348 WININET_Release( &request->hdr );
1350 if(res != ERROR_SUCCESS)
1351 SetLastError(res);
1352 return res == ERROR_SUCCESS;
1355 /***********************************************************************
1356 * HttpAddRequestHeadersA (WININET.@)
1358 * Adds one or more HTTP header to the request handler
1360 * RETURNS
1361 * TRUE on success
1362 * FALSE on failure
1365 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
1366 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1368 WCHAR *headers = NULL;
1369 BOOL r;
1371 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_an(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
1373 if(lpszHeader)
1374 headers = heap_strndupAtoW(lpszHeader, dwHeaderLength, &dwHeaderLength);
1376 r = HttpAddRequestHeadersW(hHttpRequest, headers, dwHeaderLength, dwModifier);
1378 heap_free(headers);
1379 return r;
1382 static void free_accept_types( WCHAR **accept_types )
1384 WCHAR *ptr, **types = accept_types;
1386 if (!types) return;
1387 while ((ptr = *types))
1389 heap_free( ptr );
1390 types++;
1392 heap_free( accept_types );
1395 static WCHAR **convert_accept_types( const char **accept_types )
1397 unsigned int count;
1398 const char **types = accept_types;
1399 WCHAR **typesW;
1400 BOOL invalid_pointer = FALSE;
1402 if (!types) return NULL;
1403 count = 0;
1404 while (*types)
1406 __TRY
1408 /* find out how many there are */
1409 if (*types && **types)
1411 TRACE("accept type: %s\n", debugstr_a(*types));
1412 count++;
1415 __EXCEPT_PAGE_FAULT
1417 WARN("invalid accept type pointer\n");
1418 invalid_pointer = TRUE;
1420 __ENDTRY;
1421 types++;
1423 if (invalid_pointer) return NULL;
1424 if (!(typesW = heap_alloc( sizeof(WCHAR *) * (count + 1) ))) return NULL;
1425 count = 0;
1426 types = accept_types;
1427 while (*types)
1429 if (*types && **types) typesW[count++] = heap_strdupAtoW( *types );
1430 types++;
1432 typesW[count] = NULL;
1433 return typesW;
1436 /***********************************************************************
1437 * HttpOpenRequestA (WININET.@)
1439 * Open a HTTP request handle
1441 * RETURNS
1442 * HINTERNET a HTTP request handle on success
1443 * NULL on failure
1446 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
1447 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
1448 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
1449 DWORD dwFlags, DWORD_PTR dwContext)
1451 LPWSTR szVerb = NULL, szObjectName = NULL;
1452 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
1453 HINTERNET rc = NULL;
1455 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
1456 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
1457 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
1458 dwFlags, dwContext);
1460 if (lpszVerb)
1462 szVerb = heap_strdupAtoW(lpszVerb);
1463 if ( !szVerb )
1464 goto end;
1467 if (lpszObjectName)
1469 szObjectName = heap_strdupAtoW(lpszObjectName);
1470 if ( !szObjectName )
1471 goto end;
1474 if (lpszVersion)
1476 szVersion = heap_strdupAtoW(lpszVersion);
1477 if ( !szVersion )
1478 goto end;
1481 if (lpszReferrer)
1483 szReferrer = heap_strdupAtoW(lpszReferrer);
1484 if ( !szReferrer )
1485 goto end;
1488 szAcceptTypes = convert_accept_types( lpszAcceptTypes );
1489 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName, szVersion, szReferrer,
1490 (const WCHAR **)szAcceptTypes, dwFlags, dwContext);
1492 end:
1493 free_accept_types(szAcceptTypes);
1494 heap_free(szReferrer);
1495 heap_free(szVersion);
1496 heap_free(szObjectName);
1497 heap_free(szVerb);
1498 return rc;
1501 /***********************************************************************
1502 * HTTP_EncodeBase64
1504 static UINT HTTP_EncodeBase64( LPCSTR bin, unsigned int len, LPWSTR base64 )
1506 UINT n = 0, x;
1507 static const CHAR HTTP_Base64Enc[] =
1508 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1510 while( len > 0 )
1512 /* first 6 bits, all from bin[0] */
1513 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
1514 x = (bin[0] & 3) << 4;
1516 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1517 if( len == 1 )
1519 base64[n++] = HTTP_Base64Enc[x];
1520 base64[n++] = '=';
1521 base64[n++] = '=';
1522 break;
1524 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
1525 x = ( bin[1] & 0x0f ) << 2;
1527 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1528 if( len == 2 )
1530 base64[n++] = HTTP_Base64Enc[x];
1531 base64[n++] = '=';
1532 break;
1534 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
1536 /* last 6 bits, all from bin [2] */
1537 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
1538 bin += 3;
1539 len -= 3;
1541 base64[n] = 0;
1542 return n;
1545 static const signed char HTTP_Base64Dec[] =
1547 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00 */
1548 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10 */
1549 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20 */
1550 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 0x30 */
1551 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40 */
1552 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50 */
1553 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60 */
1554 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 0x70 */
1557 /***********************************************************************
1558 * HTTP_DecodeBase64
1560 static UINT HTTP_DecodeBase64( LPCWSTR base64, LPSTR bin )
1562 unsigned int n = 0;
1564 while(*base64)
1566 signed char in[4];
1568 if (base64[0] >= ARRAYSIZE(HTTP_Base64Dec) ||
1569 ((in[0] = HTTP_Base64Dec[base64[0]]) == -1) ||
1570 base64[1] >= ARRAYSIZE(HTTP_Base64Dec) ||
1571 ((in[1] = HTTP_Base64Dec[base64[1]]) == -1))
1573 WARN("invalid base64: %s\n", debugstr_w(base64));
1574 return 0;
1576 if (bin)
1577 bin[n] = (unsigned char) (in[0] << 2 | in[1] >> 4);
1578 n++;
1580 if ((base64[2] == '=') && (base64[3] == '='))
1581 break;
1582 if (base64[2] > ARRAYSIZE(HTTP_Base64Dec) ||
1583 ((in[2] = HTTP_Base64Dec[base64[2]]) == -1))
1585 WARN("invalid base64: %s\n", debugstr_w(&base64[2]));
1586 return 0;
1588 if (bin)
1589 bin[n] = (unsigned char) (in[1] << 4 | in[2] >> 2);
1590 n++;
1592 if (base64[3] == '=')
1593 break;
1594 if (base64[3] > ARRAYSIZE(HTTP_Base64Dec) ||
1595 ((in[3] = HTTP_Base64Dec[base64[3]]) == -1))
1597 WARN("invalid base64: %s\n", debugstr_w(&base64[3]));
1598 return 0;
1600 if (bin)
1601 bin[n] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
1602 n++;
1604 base64 += 4;
1607 return n;
1610 static WCHAR *encode_auth_data( const WCHAR *scheme, const char *data, UINT data_len )
1612 WCHAR *ret;
1613 UINT len, scheme_len = strlenW( scheme );
1615 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1616 len = scheme_len + 1 + ((data_len + 2) * 4) / 3;
1617 if (!(ret = heap_alloc( (len + 1) * sizeof(WCHAR) ))) return NULL;
1618 memcpy( ret, scheme, scheme_len * sizeof(WCHAR) );
1619 ret[scheme_len] = ' ';
1620 HTTP_EncodeBase64( data, data_len, ret + scheme_len + 1 );
1621 return ret;
1625 /***********************************************************************
1626 * HTTP_InsertAuthorization
1628 * Insert or delete the authorization field in the request header.
1630 static BOOL HTTP_InsertAuthorization( http_request_t *request, struct HttpAuthInfo *pAuthInfo, LPCWSTR header )
1632 static const WCHAR wszBasic[] = {'B','a','s','i','c',0};
1633 WCHAR *host, *authorization = NULL;
1635 if (pAuthInfo)
1637 if (pAuthInfo->auth_data_len)
1639 if (!(authorization = encode_auth_data(pAuthInfo->scheme, pAuthInfo->auth_data, pAuthInfo->auth_data_len)))
1640 return FALSE;
1642 /* clear the data as it isn't valid now that it has been sent to the
1643 * server, unless it's Basic authentication which doesn't do
1644 * connection tracking */
1645 if (strcmpiW(pAuthInfo->scheme, wszBasic))
1647 heap_free(pAuthInfo->auth_data);
1648 pAuthInfo->auth_data = NULL;
1649 pAuthInfo->auth_data_len = 0;
1653 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1655 HTTP_ProcessHeader(request, header, authorization,
1656 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
1657 heap_free(authorization);
1659 else if (!strcmpW(header, szAuthorization) && (host = get_host_header(request)))
1661 UINT data_len;
1662 char *data;
1664 if ((data_len = retrieve_cached_basic_authorization(host, NULL, &data)))
1666 TRACE("Found cached basic authorization for %s\n", debugstr_w(host));
1668 if (!(authorization = encode_auth_data(wszBasic, data, data_len)))
1670 heap_free(data);
1671 heap_free(host);
1672 return FALSE;
1675 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1677 HTTP_ProcessHeader(request, header, authorization,
1678 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_ADD);
1679 heap_free(data);
1680 heap_free(authorization);
1682 heap_free(host);
1684 return TRUE;
1687 static WCHAR *build_proxy_path_url(http_request_t *req)
1689 DWORD size, len;
1690 WCHAR *url;
1692 len = strlenW(req->server->scheme_host_port);
1693 size = len + strlenW(req->path) + 1;
1694 if(*req->path != '/')
1695 size++;
1696 url = heap_alloc(size * sizeof(WCHAR));
1697 if(!url)
1698 return NULL;
1700 memcpy(url, req->server->scheme_host_port, len*sizeof(WCHAR));
1701 if(*req->path != '/')
1702 url[len++] = '/';
1704 strcpyW(url+len, req->path);
1706 TRACE("url=%s\n", debugstr_w(url));
1707 return url;
1710 static BOOL HTTP_DomainMatches(LPCWSTR server, substr_t domain)
1712 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
1713 const WCHAR *dot, *ptr;
1714 int len;
1716 if(domain.len == sizeof(localW)/sizeof(WCHAR)-1 && !strncmpiW(domain.str, localW, domain.len) && !strchrW(server, '.' ))
1717 return TRUE;
1719 if(domain.len && *domain.str != '*')
1720 return domain.len == strlenW(server) && !strncmpiW(server, domain.str, domain.len);
1722 if(domain.len < 2 || domain.str[1] != '.')
1723 return FALSE;
1725 /* For a hostname to match a wildcard, the last domain must match
1726 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
1727 * hostname is www.foo.a.b, it matches, but a.b does not.
1729 dot = strchrW(server, '.');
1730 if(!dot)
1731 return FALSE;
1733 len = strlenW(dot + 1);
1734 if(len <= domain.len - 2)
1735 return FALSE;
1737 /* The server's domain is longer than the wildcard, so it
1738 * could be a subdomain. Compare the last portion of the
1739 * server's domain.
1741 ptr = dot + 1 + len - domain.len + 2;
1742 if(!strncmpiW(ptr, domain.str+2, domain.len-2))
1743 /* This is only a match if the preceding character is
1744 * a '.', i.e. that it is a matching domain. E.g.
1745 * if domain is '*.b.c' and server is 'www.ab.c' they
1746 * do not match.
1748 return *(ptr - 1) == '.';
1750 return len == domain.len-2 && !strncmpiW(dot + 1, domain.str + 2, len);
1753 static BOOL HTTP_ShouldBypassProxy(appinfo_t *lpwai, LPCWSTR server)
1755 LPCWSTR ptr;
1756 BOOL ret = FALSE;
1758 if (!lpwai->proxyBypass) return FALSE;
1759 ptr = lpwai->proxyBypass;
1760 while(1) {
1761 LPCWSTR tmp = ptr;
1763 ptr = strchrW( ptr, ';' );
1764 if (!ptr)
1765 ptr = strchrW( tmp, ' ' );
1766 if (!ptr)
1767 ptr = tmp + strlenW(tmp);
1768 ret = HTTP_DomainMatches( server, substr(tmp, ptr-tmp) );
1769 if (ret || !*ptr)
1770 break;
1771 ptr++;
1773 return ret;
1776 /***********************************************************************
1777 * HTTP_DealWithProxy
1779 static BOOL HTTP_DealWithProxy(appinfo_t *hIC, http_session_t *session, http_request_t *request)
1781 static const WCHAR protoHttp[] = { 'h','t','t','p',0 };
1782 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 };
1783 static WCHAR szNul[] = { 0 };
1784 URL_COMPONENTSW UrlComponents = { sizeof(UrlComponents) };
1785 server_t *new_server = NULL;
1786 WCHAR *proxy;
1788 proxy = INTERNET_FindProxyForProtocol(hIC->proxy, protoHttp);
1789 if(!proxy)
1790 return FALSE;
1791 if(CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1792 proxy, strlenW(szHttp), szHttp, strlenW(szHttp))) {
1793 WCHAR *proxy_url = heap_alloc(strlenW(proxy)*sizeof(WCHAR) + sizeof(szHttp));
1794 if(!proxy_url) {
1795 heap_free(proxy);
1796 return FALSE;
1798 strcpyW(proxy_url, szHttp);
1799 strcatW(proxy_url, proxy);
1800 heap_free(proxy);
1801 proxy = proxy_url;
1804 UrlComponents.dwHostNameLength = 1;
1805 if(InternetCrackUrlW(proxy, 0, 0, &UrlComponents) && UrlComponents.dwHostNameLength) {
1806 if( !request->path )
1807 request->path = szNul;
1809 new_server = get_server(substr(UrlComponents.lpszHostName, UrlComponents.dwHostNameLength),
1810 UrlComponents.nPort, UrlComponents.nScheme == INTERNET_SCHEME_HTTPS, TRUE);
1812 heap_free(proxy);
1813 if(!new_server)
1814 return FALSE;
1816 request->proxy = new_server;
1818 TRACE("proxy server=%s port=%d\n", debugstr_w(new_server->name), new_server->port);
1819 return TRUE;
1822 static DWORD HTTP_ResolveName(http_request_t *request)
1824 server_t *server = request->proxy ? request->proxy : request->server;
1825 int addr_len;
1827 if(server->addr_len)
1828 return ERROR_SUCCESS;
1830 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
1831 INTERNET_STATUS_RESOLVING_NAME,
1832 server->name,
1833 (strlenW(server->name)+1) * sizeof(WCHAR));
1835 addr_len = sizeof(server->addr);
1836 if (!GetAddress(server->name, server->port, (SOCKADDR*)&server->addr, &addr_len, server->addr_str))
1837 return ERROR_INTERNET_NAME_NOT_RESOLVED;
1839 server->addr_len = addr_len;
1840 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
1841 INTERNET_STATUS_NAME_RESOLVED,
1842 server->addr_str, strlen(server->addr_str)+1);
1844 TRACE("resolved %s to %s\n", debugstr_w(server->name), server->addr_str);
1845 return ERROR_SUCCESS;
1848 static WCHAR *compose_request_url(http_request_t *req)
1850 static const WCHAR http[] = { 'h','t','t','p',':','/','/',0 };
1851 static const WCHAR https[] = { 'h','t','t','p','s',':','/','/',0 };
1852 const WCHAR *host, *scheme;
1853 WCHAR *buf, *ptr;
1854 size_t len;
1856 host = req->server->canon_host_port;
1858 if (req->server->is_https)
1859 scheme = https;
1860 else
1861 scheme = http;
1863 len = strlenW(scheme) + strlenW(host) + (req->path[0] != '/' ? 1 : 0) + strlenW(req->path);
1864 ptr = buf = heap_alloc((len+1) * sizeof(WCHAR));
1865 if(buf) {
1866 strcpyW(ptr, scheme);
1867 ptr += strlenW(ptr);
1869 strcpyW(ptr, host);
1870 ptr += strlenW(ptr);
1872 if(req->path[0] != '/')
1873 *ptr++ = '/';
1875 strcpyW(ptr, req->path);
1876 ptr += strlenW(ptr);
1877 *ptr = 0;
1880 return buf;
1884 /***********************************************************************
1885 * HTTPREQ_Destroy (internal)
1887 * Deallocate request handle
1890 static void HTTPREQ_Destroy(object_header_t *hdr)
1892 http_request_t *request = (http_request_t*) hdr;
1893 DWORD i;
1895 TRACE("\n");
1897 if(request->hCacheFile)
1898 CloseHandle(request->hCacheFile);
1899 if(request->req_file)
1900 req_file_release(request->req_file);
1902 request->headers_section.DebugInfo->Spare[0] = 0;
1903 DeleteCriticalSection( &request->headers_section );
1904 request->read_section.DebugInfo->Spare[0] = 0;
1905 DeleteCriticalSection( &request->read_section );
1906 WININET_Release(&request->session->hdr);
1908 destroy_authinfo(request->authInfo);
1909 destroy_authinfo(request->proxyAuthInfo);
1911 if(request->server)
1912 server_release(request->server);
1913 if(request->proxy)
1914 server_release(request->proxy);
1916 heap_free(request->path);
1917 heap_free(request->verb);
1918 heap_free(request->version);
1919 heap_free(request->statusText);
1921 for (i = 0; i < request->nCustHeaders; i++)
1923 heap_free(request->custHeaders[i].lpszField);
1924 heap_free(request->custHeaders[i].lpszValue);
1926 destroy_data_stream(request->data_stream);
1927 heap_free(request->custHeaders);
1930 static void http_release_netconn(http_request_t *req, BOOL reuse)
1932 TRACE("%p %p %x\n",req, req->netconn, reuse);
1934 if(!is_valid_netconn(req->netconn))
1935 return;
1937 if(reuse && req->netconn->keep_alive) {
1938 BOOL run_collector;
1940 EnterCriticalSection(&connection_pool_cs);
1942 list_add_head(&req->netconn->server->conn_pool, &req->netconn->pool_entry);
1943 req->netconn->keep_until = GetTickCount64() + COLLECT_TIME;
1944 req->netconn = NULL;
1946 run_collector = !collector_running;
1947 collector_running = TRUE;
1949 LeaveCriticalSection(&connection_pool_cs);
1951 if(run_collector) {
1952 HANDLE thread = NULL;
1953 HMODULE module;
1955 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (const WCHAR*)WININET_hModule, &module);
1956 if(module)
1957 thread = CreateThread(NULL, 0, collect_connections_proc, NULL, 0, NULL);
1958 if(!thread) {
1959 EnterCriticalSection(&connection_pool_cs);
1960 collector_running = FALSE;
1961 LeaveCriticalSection(&connection_pool_cs);
1963 if(module)
1964 FreeLibrary(module);
1966 else
1967 CloseHandle(thread);
1969 return;
1972 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
1973 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
1975 close_netconn(req->netconn);
1977 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
1978 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
1981 static BOOL HTTP_KeepAlive(http_request_t *request)
1983 WCHAR szVersion[10];
1984 WCHAR szConnectionResponse[20];
1985 DWORD dwBufferSize = sizeof(szVersion);
1986 BOOL keepalive = FALSE;
1988 /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
1989 * the connection is keep-alive by default */
1990 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_VERSION, szVersion, &dwBufferSize, NULL) == ERROR_SUCCESS
1991 && !strcmpiW(szVersion, g_szHttp1_1))
1993 keepalive = TRUE;
1996 dwBufferSize = sizeof(szConnectionResponse);
1997 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_PROXY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) == ERROR_SUCCESS
1998 || HTTP_HttpQueryInfoW(request, HTTP_QUERY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) == ERROR_SUCCESS)
2000 keepalive = !strcmpiW(szConnectionResponse, szKeepAlive);
2003 return keepalive;
2006 static void HTTPREQ_CloseConnection(object_header_t *hdr)
2008 http_request_t *req = (http_request_t*)hdr;
2010 http_release_netconn(req, drain_content(req, FALSE));
2013 static DWORD str_to_buffer(const WCHAR *str, void *buffer, DWORD *size, BOOL unicode)
2015 int len;
2016 if (unicode)
2018 WCHAR *buf = buffer;
2020 if (str) len = strlenW(str);
2021 else len = 0;
2022 if (*size < (len + 1) * sizeof(WCHAR))
2024 *size = (len + 1) * sizeof(WCHAR);
2025 return ERROR_INSUFFICIENT_BUFFER;
2027 if (str) strcpyW(buf, str);
2028 else buf[0] = 0;
2030 *size = len;
2031 return ERROR_SUCCESS;
2033 else
2035 char *buf = buffer;
2037 if (str) len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
2038 else len = 1;
2039 if (*size < len)
2041 *size = len;
2042 return ERROR_INSUFFICIENT_BUFFER;
2044 if (str) WideCharToMultiByte(CP_ACP, 0, str, -1, buf, *size, NULL, NULL);
2045 else buf[0] = 0;
2047 *size = len - 1;
2048 return ERROR_SUCCESS;
2052 static DWORD HTTPREQ_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2054 http_request_t *req = (http_request_t*)hdr;
2056 switch(option) {
2057 case INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO:
2059 INTERNET_DIAGNOSTIC_SOCKET_INFO *info = buffer;
2061 FIXME("INTERNET_DIAGNOSTIC_SOCKET_INFO stub\n");
2063 if (*size < sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO))
2064 return ERROR_INSUFFICIENT_BUFFER;
2065 *size = sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO);
2066 /* FIXME: can't get a SOCKET from our connection since we don't use
2067 * winsock
2069 info->Socket = 0;
2070 /* FIXME: get source port from req->netConnection */
2071 info->SourcePort = 0;
2072 info->DestPort = req->server->port;
2073 info->Flags = 0;
2074 if (HTTP_KeepAlive(req))
2075 info->Flags |= IDSI_FLAG_KEEP_ALIVE;
2076 if (req->proxy)
2077 info->Flags |= IDSI_FLAG_PROXY;
2078 if (is_valid_netconn(req->netconn) && req->netconn->secure)
2079 info->Flags |= IDSI_FLAG_SECURE;
2081 return ERROR_SUCCESS;
2084 case 98:
2085 TRACE("Queried undocumented option 98, forwarding to INTERNET_OPTION_SECURITY_FLAGS\n");
2086 /* fall through */
2087 case INTERNET_OPTION_SECURITY_FLAGS:
2089 DWORD flags;
2091 if (*size < sizeof(ULONG))
2092 return ERROR_INSUFFICIENT_BUFFER;
2094 *size = sizeof(DWORD);
2095 flags = is_valid_netconn(req->netconn) ? req->netconn->security_flags : req->security_flags | req->server->security_flags;
2096 *(DWORD *)buffer = flags;
2098 TRACE("INTERNET_OPTION_SECURITY_FLAGS %x\n", flags);
2099 return ERROR_SUCCESS;
2102 case INTERNET_OPTION_HANDLE_TYPE:
2103 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
2105 if (*size < sizeof(ULONG))
2106 return ERROR_INSUFFICIENT_BUFFER;
2108 *size = sizeof(DWORD);
2109 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_HTTP_REQUEST;
2110 return ERROR_SUCCESS;
2112 case INTERNET_OPTION_URL: {
2113 WCHAR *url;
2114 DWORD res;
2116 TRACE("INTERNET_OPTION_URL\n");
2118 url = compose_request_url(req);
2119 if(!url)
2120 return ERROR_OUTOFMEMORY;
2122 res = str_to_buffer(url, buffer, size, unicode);
2123 heap_free(url);
2124 return res;
2126 case INTERNET_OPTION_USER_AGENT:
2127 return str_to_buffer(req->session->appInfo->agent, buffer, size, unicode);
2128 case INTERNET_OPTION_USERNAME:
2129 return str_to_buffer(req->session->userName, buffer, size, unicode);
2130 case INTERNET_OPTION_PASSWORD:
2131 return str_to_buffer(req->session->password, buffer, size, unicode);
2132 case INTERNET_OPTION_PROXY_USERNAME:
2133 return str_to_buffer(req->session->appInfo->proxyUsername, buffer, size, unicode);
2134 case INTERNET_OPTION_PROXY_PASSWORD:
2135 return str_to_buffer(req->session->appInfo->proxyPassword, buffer, size, unicode);
2137 case INTERNET_OPTION_CACHE_TIMESTAMPS: {
2138 INTERNET_CACHE_ENTRY_INFOW *info;
2139 INTERNET_CACHE_TIMESTAMPS *ts = buffer;
2140 DWORD nbytes, error;
2141 BOOL ret;
2143 TRACE("INTERNET_OPTION_CACHE_TIMESTAMPS\n");
2145 if(!req->req_file)
2146 return ERROR_FILE_NOT_FOUND;
2148 if (*size < sizeof(*ts))
2150 *size = sizeof(*ts);
2151 return ERROR_INSUFFICIENT_BUFFER;
2154 nbytes = 0;
2155 ret = GetUrlCacheEntryInfoW(req->req_file->url, NULL, &nbytes);
2156 error = GetLastError();
2157 if (!ret && error == ERROR_INSUFFICIENT_BUFFER)
2159 if (!(info = heap_alloc(nbytes)))
2160 return ERROR_OUTOFMEMORY;
2162 GetUrlCacheEntryInfoW(req->req_file->url, info, &nbytes);
2164 ts->ftExpires = info->ExpireTime;
2165 ts->ftLastModified = info->LastModifiedTime;
2167 heap_free(info);
2168 *size = sizeof(*ts);
2169 return ERROR_SUCCESS;
2171 return error;
2174 case INTERNET_OPTION_DATAFILE_NAME: {
2175 DWORD req_size;
2177 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
2179 if(!req->req_file) {
2180 *size = 0;
2181 return ERROR_INTERNET_ITEM_NOT_FOUND;
2184 if(unicode) {
2185 req_size = (lstrlenW(req->req_file->file_name)+1) * sizeof(WCHAR);
2186 if(*size < req_size)
2187 return ERROR_INSUFFICIENT_BUFFER;
2189 *size = req_size;
2190 memcpy(buffer, req->req_file->file_name, *size);
2191 return ERROR_SUCCESS;
2192 }else {
2193 req_size = WideCharToMultiByte(CP_ACP, 0, req->req_file->file_name, -1, NULL, 0, NULL, NULL);
2194 if (req_size > *size)
2195 return ERROR_INSUFFICIENT_BUFFER;
2197 *size = WideCharToMultiByte(CP_ACP, 0, req->req_file->file_name,
2198 -1, buffer, *size, NULL, NULL);
2199 return ERROR_SUCCESS;
2203 case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT: {
2204 PCCERT_CONTEXT context;
2206 if(!req->netconn)
2207 return ERROR_INTERNET_INVALID_OPERATION;
2209 if(*size < sizeof(INTERNET_CERTIFICATE_INFOA)) {
2210 *size = sizeof(INTERNET_CERTIFICATE_INFOA);
2211 return ERROR_INSUFFICIENT_BUFFER;
2214 context = (PCCERT_CONTEXT)NETCON_GetCert(req->netconn);
2215 if(context) {
2216 INTERNET_CERTIFICATE_INFOA *info = (INTERNET_CERTIFICATE_INFOA*)buffer;
2217 DWORD len;
2219 memset(info, 0, sizeof(*info));
2220 info->ftExpiry = context->pCertInfo->NotAfter;
2221 info->ftStart = context->pCertInfo->NotBefore;
2222 len = CertNameToStrA(context->dwCertEncodingType,
2223 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG, NULL, 0);
2224 info->lpszSubjectInfo = LocalAlloc(0, len);
2225 if(info->lpszSubjectInfo)
2226 CertNameToStrA(context->dwCertEncodingType,
2227 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG,
2228 info->lpszSubjectInfo, len);
2229 len = CertNameToStrA(context->dwCertEncodingType,
2230 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG, NULL, 0);
2231 info->lpszIssuerInfo = LocalAlloc(0, len);
2232 if(info->lpszIssuerInfo)
2233 CertNameToStrA(context->dwCertEncodingType,
2234 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG,
2235 info->lpszIssuerInfo, len);
2236 info->dwKeySize = NETCON_GetCipherStrength(req->netconn);
2237 CertFreeCertificateContext(context);
2238 return ERROR_SUCCESS;
2240 return ERROR_NOT_SUPPORTED;
2242 case INTERNET_OPTION_CONNECT_TIMEOUT:
2243 if (*size < sizeof(DWORD))
2244 return ERROR_INSUFFICIENT_BUFFER;
2246 *size = sizeof(DWORD);
2247 *(DWORD *)buffer = req->connect_timeout;
2248 return ERROR_SUCCESS;
2249 case INTERNET_OPTION_REQUEST_FLAGS: {
2250 DWORD flags = 0;
2252 if (*size < sizeof(DWORD))
2253 return ERROR_INSUFFICIENT_BUFFER;
2255 /* FIXME: Add support for:
2256 * INTERNET_REQFLAG_FROM_CACHE
2257 * INTERNET_REQFLAG_CACHE_WRITE_DISABLED
2260 if(req->proxy)
2261 flags |= INTERNET_REQFLAG_VIA_PROXY;
2262 if(!req->status_code)
2263 flags |= INTERNET_REQFLAG_NO_HEADERS;
2265 TRACE("INTERNET_OPTION_REQUEST_FLAGS returning %x\n", flags);
2267 *size = sizeof(DWORD);
2268 *(DWORD*)buffer = flags;
2269 return ERROR_SUCCESS;
2273 return INET_QueryOption(hdr, option, buffer, size, unicode);
2276 static DWORD HTTPREQ_SetOption(object_header_t *hdr, DWORD option, void *buffer, DWORD size)
2278 http_request_t *req = (http_request_t*)hdr;
2280 switch(option) {
2281 case 99: /* Undocumented, seems to be INTERNET_OPTION_SECURITY_FLAGS with argument validation */
2282 TRACE("Undocumented option 99\n");
2284 if (!buffer || size != sizeof(DWORD))
2285 return ERROR_INVALID_PARAMETER;
2286 if(*(DWORD*)buffer & ~SECURITY_SET_MASK)
2287 return ERROR_INTERNET_OPTION_NOT_SETTABLE;
2289 /* fall through */
2290 case INTERNET_OPTION_SECURITY_FLAGS:
2292 DWORD flags;
2294 if (!buffer || size != sizeof(DWORD))
2295 return ERROR_INVALID_PARAMETER;
2296 flags = *(DWORD *)buffer;
2297 TRACE("INTERNET_OPTION_SECURITY_FLAGS %08x\n", flags);
2298 flags &= SECURITY_SET_MASK;
2299 req->security_flags |= flags;
2300 if(is_valid_netconn(req->netconn))
2301 req->netconn->security_flags |= flags;
2302 return ERROR_SUCCESS;
2304 case INTERNET_OPTION_CONNECT_TIMEOUT:
2305 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2306 req->connect_timeout = *(DWORD *)buffer;
2307 return ERROR_SUCCESS;
2309 case INTERNET_OPTION_SEND_TIMEOUT:
2310 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2311 req->send_timeout = *(DWORD *)buffer;
2312 return ERROR_SUCCESS;
2314 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2315 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2316 req->receive_timeout = *(DWORD *)buffer;
2317 return ERROR_SUCCESS;
2319 case INTERNET_OPTION_USERNAME:
2320 heap_free(req->session->userName);
2321 if (!(req->session->userName = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2322 return ERROR_SUCCESS;
2324 case INTERNET_OPTION_PASSWORD:
2325 heap_free(req->session->password);
2326 if (!(req->session->password = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2327 return ERROR_SUCCESS;
2329 case INTERNET_OPTION_PROXY_USERNAME:
2330 heap_free(req->session->appInfo->proxyUsername);
2331 if (!(req->session->appInfo->proxyUsername = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2332 return ERROR_SUCCESS;
2334 case INTERNET_OPTION_PROXY_PASSWORD:
2335 heap_free(req->session->appInfo->proxyPassword);
2336 if (!(req->session->appInfo->proxyPassword = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2337 return ERROR_SUCCESS;
2339 case INTERNET_OPTION_HTTP_DECODING:
2340 if(size != sizeof(BOOL))
2341 return ERROR_INVALID_PARAMETER;
2342 req->decoding = *(BOOL*)buffer;
2343 return ERROR_SUCCESS;
2346 return INET_SetOption(hdr, option, buffer, size);
2349 static void commit_cache_entry(http_request_t *req)
2351 WCHAR *header;
2352 DWORD header_len;
2353 BOOL res;
2355 TRACE("%p\n", req);
2357 CloseHandle(req->hCacheFile);
2358 req->hCacheFile = NULL;
2360 header = build_response_header(req, TRUE);
2361 header_len = (header ? strlenW(header) : 0);
2362 res = CommitUrlCacheEntryW(req->req_file->url, req->req_file->file_name, req->expires,
2363 req->last_modified, NORMAL_CACHE_ENTRY,
2364 header, header_len, NULL, 0);
2365 if(res)
2366 req->req_file->is_committed = TRUE;
2367 else
2368 WARN("CommitUrlCacheEntry failed: %u\n", GetLastError());
2369 heap_free(header);
2372 static void create_cache_entry(http_request_t *req)
2374 static const WCHAR no_cacheW[] = {'n','o','-','c','a','c','h','e',0};
2375 static const WCHAR no_storeW[] = {'n','o','-','s','t','o','r','e',0};
2377 WCHAR file_name[MAX_PATH+1];
2378 WCHAR *url;
2379 BOOL b = TRUE;
2381 /* FIXME: We should free previous cache file earlier */
2382 if(req->req_file) {
2383 req_file_release(req->req_file);
2384 req->req_file = NULL;
2386 if(req->hCacheFile) {
2387 CloseHandle(req->hCacheFile);
2388 req->hCacheFile = NULL;
2391 if(req->hdr.dwFlags & INTERNET_FLAG_NO_CACHE_WRITE)
2392 b = FALSE;
2394 if(b) {
2395 int header_idx;
2397 EnterCriticalSection( &req->headers_section );
2399 header_idx = HTTP_GetCustomHeaderIndex(req, szCache_Control, 0, FALSE);
2400 if(header_idx != -1) {
2401 WCHAR *ptr;
2403 for(ptr=req->custHeaders[header_idx].lpszValue; *ptr; ) {
2404 WCHAR *end;
2406 while(*ptr==' ' || *ptr=='\t')
2407 ptr++;
2409 end = strchrW(ptr, ',');
2410 if(!end)
2411 end = ptr + strlenW(ptr);
2413 if(!strncmpiW(ptr, no_cacheW, sizeof(no_cacheW)/sizeof(*no_cacheW)-1)
2414 || !strncmpiW(ptr, no_storeW, sizeof(no_storeW)/sizeof(*no_storeW)-1)) {
2415 b = FALSE;
2416 break;
2419 ptr = end;
2420 if(*ptr == ',')
2421 ptr++;
2425 LeaveCriticalSection( &req->headers_section );
2428 if(!b) {
2429 if(!(req->hdr.dwFlags & INTERNET_FLAG_NEED_FILE))
2430 return;
2432 FIXME("INTERNET_FLAG_NEED_FILE is not supported correctly\n");
2435 url = compose_request_url(req);
2436 if(!url) {
2437 WARN("Could not get URL\n");
2438 return;
2441 b = CreateUrlCacheEntryW(url, req->contentLength == ~0u ? 0 : req->contentLength, NULL, file_name, 0);
2442 if(!b) {
2443 WARN("Could not create cache entry: %08x\n", GetLastError());
2444 return;
2447 create_req_file(file_name, &req->req_file);
2448 req->req_file->url = url;
2450 req->hCacheFile = CreateFileW(file_name, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
2451 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2452 if(req->hCacheFile == INVALID_HANDLE_VALUE) {
2453 WARN("Could not create file: %u\n", GetLastError());
2454 req->hCacheFile = NULL;
2455 return;
2458 if(req->read_size) {
2459 DWORD written;
2461 b = WriteFile(req->hCacheFile, req->read_buf+req->read_pos, req->read_size, &written, NULL);
2462 if(!b)
2463 FIXME("WriteFile failed: %u\n", GetLastError());
2465 if(req->data_stream->vtbl->end_of_data(req->data_stream, req))
2466 commit_cache_entry(req);
2470 /* read some more data into the read buffer (the read section must be held) */
2471 static DWORD read_more_data( http_request_t *req, int maxlen )
2473 DWORD res;
2474 int len;
2476 if (req->read_pos)
2478 /* move existing data to the start of the buffer */
2479 if(req->read_size)
2480 memmove( req->read_buf, req->read_buf + req->read_pos, req->read_size );
2481 req->read_pos = 0;
2484 if (maxlen == -1) maxlen = sizeof(req->read_buf);
2486 res = NETCON_recv( req->netconn, req->read_buf + req->read_size,
2487 maxlen - req->read_size, TRUE, &len );
2488 if(res == ERROR_SUCCESS)
2489 req->read_size += len;
2491 return res;
2494 /* remove some amount of data from the read buffer (the read section must be held) */
2495 static void remove_data( http_request_t *req, int count )
2497 if (!(req->read_size -= count)) req->read_pos = 0;
2498 else req->read_pos += count;
2501 static DWORD read_line( http_request_t *req, LPSTR buffer, DWORD *len )
2503 int count, bytes_read, pos = 0;
2504 DWORD res;
2506 EnterCriticalSection( &req->read_section );
2507 for (;;)
2509 BYTE *eol = memchr( req->read_buf + req->read_pos, '\n', req->read_size );
2511 if (eol)
2513 count = eol - (req->read_buf + req->read_pos);
2514 bytes_read = count + 1;
2516 else count = bytes_read = req->read_size;
2518 count = min( count, *len - pos );
2519 memcpy( buffer + pos, req->read_buf + req->read_pos, count );
2520 pos += count;
2521 remove_data( req, bytes_read );
2522 if (eol) break;
2524 if ((res = read_more_data( req, -1 )))
2526 WARN( "read failed %u\n", res );
2527 LeaveCriticalSection( &req->read_section );
2528 return res;
2530 if (!req->read_size)
2532 *len = 0;
2533 TRACE( "returning empty string\n" );
2534 LeaveCriticalSection( &req->read_section );
2535 return ERROR_SUCCESS;
2538 LeaveCriticalSection( &req->read_section );
2540 if (pos < *len)
2542 if (pos && buffer[pos - 1] == '\r') pos--;
2543 *len = pos + 1;
2545 buffer[*len - 1] = 0;
2546 TRACE( "returning %s\n", debugstr_a(buffer));
2547 return ERROR_SUCCESS;
2550 /* check if we have reached the end of the data to read (the read section must be held) */
2551 static BOOL end_of_read_data( http_request_t *req )
2553 return !req->read_size && req->data_stream->vtbl->end_of_data(req->data_stream, req);
2556 static DWORD read_http_stream(http_request_t *req, BYTE *buf, DWORD size, DWORD *read, BOOL allow_blocking)
2558 DWORD res;
2560 res = req->data_stream->vtbl->read(req->data_stream, req, buf, size, read, allow_blocking);
2561 if(res != ERROR_SUCCESS)
2562 *read = 0;
2563 assert(*read <= size);
2565 if(req->hCacheFile) {
2566 if(*read) {
2567 BOOL bres;
2568 DWORD written;
2570 bres = WriteFile(req->hCacheFile, buf, *read, &written, NULL);
2571 if(!bres)
2572 FIXME("WriteFile failed: %u\n", GetLastError());
2575 if((res == ERROR_SUCCESS && !*read) || req->data_stream->vtbl->end_of_data(req->data_stream, req))
2576 commit_cache_entry(req);
2579 return res;
2582 /* fetch some more data into the read buffer (the read section must be held) */
2583 static DWORD refill_read_buffer(http_request_t *req, BOOL allow_blocking, DWORD *read_bytes)
2585 DWORD res, read=0;
2587 if(req->read_size == sizeof(req->read_buf))
2588 return ERROR_SUCCESS;
2590 if(req->read_pos) {
2591 if(req->read_size)
2592 memmove(req->read_buf, req->read_buf+req->read_pos, req->read_size);
2593 req->read_pos = 0;
2596 res = read_http_stream(req, req->read_buf+req->read_size, sizeof(req->read_buf) - req->read_size,
2597 &read, allow_blocking);
2598 if(res != ERROR_SUCCESS)
2599 return res;
2601 req->read_size += read;
2603 TRACE("read %u bytes, read_size %u\n", read, req->read_size);
2604 if(read_bytes)
2605 *read_bytes = read;
2606 return res;
2609 static BOOL netconn_end_of_data(data_stream_t *stream, http_request_t *req)
2611 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2612 return netconn_stream->content_read == netconn_stream->content_length || !is_valid_netconn(req->netconn);
2615 static DWORD netconn_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
2616 DWORD *read, BOOL allow_blocking)
2618 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2619 DWORD res = ERROR_SUCCESS;
2620 int ret = 0;
2622 size = min(size, netconn_stream->content_length-netconn_stream->content_read);
2624 if(size && is_valid_netconn(req->netconn)) {
2625 res = NETCON_recv(req->netconn, buf, size, allow_blocking, &ret);
2626 if(res == ERROR_SUCCESS) {
2627 if(!ret)
2628 netconn_stream->content_length = netconn_stream->content_read;
2629 netconn_stream->content_read += ret;
2633 TRACE("res %u read %u bytes\n", res, ret);
2634 *read = ret;
2635 return res;
2638 static BOOL netconn_drain_content(data_stream_t *stream, http_request_t *req)
2640 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2641 BYTE buf[1024];
2642 int len, res;
2643 size_t size;
2645 if(netconn_stream->content_length == ~0u)
2646 return FALSE;
2648 while(netconn_stream->content_read < netconn_stream->content_length) {
2649 size = min(sizeof(buf), netconn_stream->content_length-netconn_stream->content_read);
2650 res = NETCON_recv(req->netconn, buf, size, FALSE, &len);
2651 if(res || !len)
2652 return FALSE;
2654 netconn_stream->content_read += len;
2657 return TRUE;
2660 static void netconn_destroy(data_stream_t *stream)
2664 static const data_stream_vtbl_t netconn_stream_vtbl = {
2665 netconn_end_of_data,
2666 netconn_read,
2667 netconn_drain_content,
2668 netconn_destroy
2671 static char next_chunked_data_char(chunked_stream_t *stream)
2673 assert(stream->buf_size);
2675 stream->buf_size--;
2676 return stream->buf[stream->buf_pos++];
2679 static BOOL chunked_end_of_data(data_stream_t *stream, http_request_t *req)
2681 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2682 return chunked_stream->state == CHUNKED_STREAM_STATE_END_OF_STREAM;
2685 static DWORD chunked_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
2686 DWORD *read, BOOL allow_blocking)
2688 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2689 DWORD ret_read = 0, res = ERROR_SUCCESS;
2690 BOOL continue_read = TRUE;
2691 int read_bytes;
2692 char ch;
2694 do {
2695 TRACE("state %d\n", chunked_stream->state);
2697 /* Ensure that we have data in the buffer for states that need it. */
2698 if(!chunked_stream->buf_size) {
2699 switch(chunked_stream->state) {
2700 case CHUNKED_STREAM_STATE_READING_CHUNK_SIZE:
2701 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE:
2702 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA:
2703 case CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END:
2704 chunked_stream->buf_pos = 0;
2705 res = NETCON_recv(req->netconn, chunked_stream->buf, sizeof(chunked_stream->buf), allow_blocking, &read_bytes);
2706 if(res == ERROR_SUCCESS && read_bytes) {
2707 chunked_stream->buf_size += read_bytes;
2708 }else if(res == WSAEWOULDBLOCK) {
2709 if(ret_read)
2710 res = ERROR_SUCCESS;
2711 continue_read = FALSE;
2712 continue;
2713 }else {
2714 chunked_stream->state = CHUNKED_STREAM_STATE_END_OF_STREAM;
2716 break;
2717 default:
2718 break;
2722 switch(chunked_stream->state) {
2723 case CHUNKED_STREAM_STATE_READING_CHUNK_SIZE:
2724 ch = next_chunked_data_char(chunked_stream);
2726 if(ch >= '0' && ch <= '9') {
2727 chunked_stream->chunk_size = chunked_stream->chunk_size * 16 + ch - '0';
2728 }else if(ch >= 'a' && ch <= 'f') {
2729 chunked_stream->chunk_size = chunked_stream->chunk_size * 16 + ch - 'a' + 10;
2730 }else if (ch >= 'A' && ch <= 'F') {
2731 chunked_stream->chunk_size = chunked_stream->chunk_size * 16 + ch - 'A' + 10;
2732 }else if (ch == ';' || ch == '\r' || ch == '\n') {
2733 TRACE("reading %u byte chunk\n", chunked_stream->chunk_size);
2734 chunked_stream->buf_size++;
2735 chunked_stream->buf_pos--;
2736 if(req->contentLength == ~0u) req->contentLength = chunked_stream->chunk_size;
2737 else req->contentLength += chunked_stream->chunk_size;
2738 chunked_stream->state = CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE;
2740 break;
2742 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE:
2743 ch = next_chunked_data_char(chunked_stream);
2744 if(ch == '\n')
2745 chunked_stream->state = chunked_stream->chunk_size
2746 ? CHUNKED_STREAM_STATE_READING_CHUNK
2747 : CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END;
2748 else if(ch != '\r')
2749 WARN("unexpected char '%c'\n", ch);
2750 break;
2752 case CHUNKED_STREAM_STATE_READING_CHUNK:
2753 assert(chunked_stream->chunk_size);
2754 if(!size) {
2755 continue_read = FALSE;
2756 break;
2758 read_bytes = min(size, chunked_stream->chunk_size);
2760 if(chunked_stream->buf_size) {
2761 if(read_bytes > chunked_stream->buf_size)
2762 read_bytes = chunked_stream->buf_size;
2764 memcpy(buf+ret_read, chunked_stream->buf+chunked_stream->buf_pos, read_bytes);
2765 chunked_stream->buf_pos += read_bytes;
2766 chunked_stream->buf_size -= read_bytes;
2767 }else {
2768 res = NETCON_recv(req->netconn, (char*)buf+ret_read, read_bytes,
2769 allow_blocking, (int*)&read_bytes);
2770 if(res != ERROR_SUCCESS) {
2771 continue_read = FALSE;
2772 break;
2775 if(!read_bytes) {
2776 chunked_stream->state = CHUNKED_STREAM_STATE_END_OF_STREAM;
2777 continue;
2781 chunked_stream->chunk_size -= read_bytes;
2782 size -= read_bytes;
2783 ret_read += read_bytes;
2784 if(!chunked_stream->chunk_size)
2785 chunked_stream->state = CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA;
2786 allow_blocking = FALSE;
2787 break;
2789 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA:
2790 ch = next_chunked_data_char(chunked_stream);
2791 if(ch == '\n')
2792 chunked_stream->state = CHUNKED_STREAM_STATE_READING_CHUNK_SIZE;
2793 else if(ch != '\r')
2794 WARN("unexpected char '%c'\n", ch);
2795 break;
2797 case CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END:
2798 ch = next_chunked_data_char(chunked_stream);
2799 if(ch == '\n')
2800 chunked_stream->state = CHUNKED_STREAM_STATE_END_OF_STREAM;
2801 else if(ch != '\r')
2802 WARN("unexpected char '%c'\n", ch);
2803 break;
2805 case CHUNKED_STREAM_STATE_END_OF_STREAM:
2806 continue_read = FALSE;
2807 break;
2809 } while(continue_read);
2811 if(ret_read)
2812 res = ERROR_SUCCESS;
2813 if(res != ERROR_SUCCESS)
2814 return res;
2816 TRACE("read %d bytes\n", ret_read);
2817 *read = ret_read;
2818 return ERROR_SUCCESS;
2821 static BOOL chunked_drain_content(data_stream_t *stream, http_request_t *req)
2823 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2824 return chunked_stream->state == CHUNKED_STREAM_STATE_END_OF_STREAM;
2827 static void chunked_destroy(data_stream_t *stream)
2829 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2830 heap_free(chunked_stream);
2833 static const data_stream_vtbl_t chunked_stream_vtbl = {
2834 chunked_end_of_data,
2835 chunked_read,
2836 chunked_drain_content,
2837 chunked_destroy
2840 /* set the request content length based on the headers */
2841 static DWORD set_content_length(http_request_t *request)
2843 static const WCHAR szChunked[] = {'c','h','u','n','k','e','d',0};
2844 static const WCHAR headW[] = {'H','E','A','D',0};
2845 WCHAR encoding[20];
2846 DWORD size;
2848 if(request->status_code == HTTP_STATUS_NO_CONTENT || !strcmpW(request->verb, headW)) {
2849 request->contentLength = request->netconn_stream.content_length = 0;
2850 return ERROR_SUCCESS;
2853 size = sizeof(request->contentLength);
2854 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
2855 &request->contentLength, &size, NULL) != ERROR_SUCCESS)
2856 request->contentLength = ~0u;
2857 request->netconn_stream.content_length = request->contentLength;
2858 request->netconn_stream.content_read = request->read_size;
2860 size = sizeof(encoding);
2861 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_TRANSFER_ENCODING, encoding, &size, NULL) == ERROR_SUCCESS &&
2862 !strcmpiW(encoding, szChunked))
2864 chunked_stream_t *chunked_stream;
2866 chunked_stream = heap_alloc(sizeof(*chunked_stream));
2867 if(!chunked_stream)
2868 return ERROR_OUTOFMEMORY;
2870 chunked_stream->data_stream.vtbl = &chunked_stream_vtbl;
2871 chunked_stream->buf_size = chunked_stream->buf_pos = 0;
2872 chunked_stream->chunk_size = 0;
2873 chunked_stream->state = CHUNKED_STREAM_STATE_READING_CHUNK_SIZE;
2875 if(request->read_size) {
2876 memcpy(chunked_stream->buf, request->read_buf+request->read_pos, request->read_size);
2877 chunked_stream->buf_size = request->read_size;
2878 request->read_size = request->read_pos = 0;
2881 request->data_stream = &chunked_stream->data_stream;
2882 request->contentLength = ~0u;
2885 if(request->decoding) {
2886 int encoding_idx;
2888 static const WCHAR deflateW[] = {'d','e','f','l','a','t','e',0};
2889 static const WCHAR gzipW[] = {'g','z','i','p',0};
2891 EnterCriticalSection( &request->headers_section );
2893 encoding_idx = HTTP_GetCustomHeaderIndex(request, szContent_Encoding, 0, FALSE);
2894 if(encoding_idx != -1) {
2895 if(!strcmpiW(request->custHeaders[encoding_idx].lpszValue, gzipW)) {
2896 HTTP_DeleteCustomHeader(request, encoding_idx);
2897 LeaveCriticalSection( &request->headers_section );
2898 return init_gzip_stream(request, TRUE);
2900 if(!strcmpiW(request->custHeaders[encoding_idx].lpszValue, deflateW)) {
2901 HTTP_DeleteCustomHeader(request, encoding_idx);
2902 LeaveCriticalSection( &request->headers_section );
2903 return init_gzip_stream(request, FALSE);
2907 LeaveCriticalSection( &request->headers_section );
2910 return ERROR_SUCCESS;
2913 static void send_request_complete(http_request_t *req, DWORD_PTR result, DWORD error)
2915 INTERNET_ASYNC_RESULT iar;
2917 iar.dwResult = result;
2918 iar.dwError = error;
2920 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2921 sizeof(INTERNET_ASYNC_RESULT));
2924 static void HTTP_ReceiveRequestData(http_request_t *req)
2926 DWORD res, read = 0;
2928 TRACE("%p\n", req);
2930 EnterCriticalSection( &req->read_section );
2932 res = refill_read_buffer(req, FALSE, &read);
2933 if(res == ERROR_SUCCESS)
2934 read += req->read_size;
2936 LeaveCriticalSection( &req->read_section );
2938 if(res != WSAEWOULDBLOCK && (res != ERROR_SUCCESS || !read)) {
2939 WARN("res %u read %u, closing connection\n", res, read);
2940 http_release_netconn(req, FALSE);
2943 if(res != ERROR_SUCCESS && res != WSAEWOULDBLOCK) {
2944 send_request_complete(req, 0, res);
2945 return;
2948 send_request_complete(req, req->session->hdr.dwInternalFlags & INET_OPENURL ? (DWORD_PTR)req->hdr.hInternet : 1, 0);
2951 /* read data from the http connection (the read section must be held) */
2952 static DWORD HTTPREQ_Read(http_request_t *req, void *buffer, DWORD size, DWORD *read, BOOL allow_blocking)
2954 DWORD current_read = 0, ret_read = 0;
2955 DWORD res = ERROR_SUCCESS;
2957 EnterCriticalSection( &req->read_section );
2959 if(req->read_size) {
2960 ret_read = min(size, req->read_size);
2961 memcpy(buffer, req->read_buf+req->read_pos, ret_read);
2962 req->read_size -= ret_read;
2963 req->read_pos += ret_read;
2964 allow_blocking = FALSE;
2967 if(ret_read < size) {
2968 res = read_http_stream(req, (BYTE*)buffer+ret_read, size-ret_read, &current_read, allow_blocking);
2969 if(res == ERROR_SUCCESS)
2970 ret_read += current_read;
2971 else if(res == WSAEWOULDBLOCK && ret_read)
2972 res = ERROR_SUCCESS;
2975 LeaveCriticalSection( &req->read_section );
2977 *read = ret_read;
2978 TRACE( "retrieved %u bytes (%u)\n", ret_read, req->contentLength );
2980 if(res != WSAEWOULDBLOCK && (!ret_read || res != ERROR_SUCCESS))
2981 http_release_netconn(req, res == ERROR_SUCCESS);
2983 return res;
2986 static BOOL drain_content(http_request_t *req, BOOL blocking)
2988 BOOL ret;
2990 if(!is_valid_netconn(req->netconn) || req->contentLength == -1)
2991 return FALSE;
2993 if(!strcmpW(req->verb, szHEAD))
2994 return TRUE;
2996 if(!blocking)
2997 return req->data_stream->vtbl->drain_content(req->data_stream, req);
2999 EnterCriticalSection( &req->read_section );
3001 while(1) {
3002 DWORD bytes_read, res;
3003 BYTE buf[4096];
3005 res = HTTPREQ_Read(req, buf, sizeof(buf), &bytes_read, TRUE);
3006 if(res != ERROR_SUCCESS) {
3007 ret = FALSE;
3008 break;
3010 if(!bytes_read) {
3011 ret = TRUE;
3012 break;
3016 LeaveCriticalSection( &req->read_section );
3017 return ret;
3020 typedef struct {
3021 task_header_t hdr;
3022 void *buf;
3023 DWORD size;
3024 DWORD read_pos;
3025 DWORD *ret_read;
3026 } read_file_task_t;
3028 static void async_read_file_proc(task_header_t *hdr)
3030 read_file_task_t *task = (read_file_task_t*)hdr;
3031 http_request_t *req = (http_request_t*)task->hdr.hdr;
3032 DWORD res = ERROR_SUCCESS, read = task->read_pos, complete_arg = 0;
3034 TRACE("req %p buf %p size %u read_pos %u ret_read %p\n", req, task->buf, task->size, task->read_pos, task->ret_read);
3036 if(task->buf) {
3037 DWORD read_bytes;
3038 while (read < task->size) {
3039 res = HTTPREQ_Read(req, (char*)task->buf + read, task->size - read, &read_bytes, TRUE);
3040 if (res != ERROR_SUCCESS || !read_bytes)
3041 break;
3042 read += read_bytes;
3044 }else {
3045 EnterCriticalSection(&req->read_section);
3046 res = refill_read_buffer(req, TRUE, &read);
3047 LeaveCriticalSection(&req->read_section);
3049 if(task->ret_read)
3050 complete_arg = read; /* QueryDataAvailable reports read bytes in request complete notification */
3051 if(res != ERROR_SUCCESS || !read)
3052 http_release_netconn(req, drain_content(req, FALSE));
3055 TRACE("res %u read %u\n", res, read);
3057 if(task->ret_read)
3058 *task->ret_read = read;
3060 /* FIXME: We should report bytes transferred before decoding content. */
3061 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED, &read, sizeof(read));
3063 if(res != ERROR_SUCCESS)
3064 complete_arg = res;
3065 send_request_complete(req, res == ERROR_SUCCESS, complete_arg);
3068 static DWORD async_read(http_request_t *req, void *buf, DWORD size, DWORD read_pos, DWORD *ret_read)
3070 read_file_task_t *task;
3072 task = alloc_async_task(&req->hdr, async_read_file_proc, sizeof(*task));
3073 if(!task)
3074 return ERROR_OUTOFMEMORY;
3076 task->buf = buf;
3077 task->size = size;
3078 task->read_pos = read_pos;
3079 task->ret_read = ret_read;
3081 INTERNET_AsyncCall(&task->hdr);
3082 return ERROR_IO_PENDING;
3085 static DWORD HTTPREQ_ReadFile(object_header_t *hdr, void *buf, DWORD size, DWORD *ret_read,
3086 DWORD flags, DWORD_PTR context)
3088 http_request_t *req = (http_request_t*)hdr;
3089 DWORD res = ERROR_SUCCESS, read = 0, cread, error = ERROR_SUCCESS;
3090 BOOL allow_blocking, notify_received = FALSE;
3092 TRACE("(%p %p %u %x)\n", req, buf, size, flags);
3094 if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
3095 FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
3097 allow_blocking = !(req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC);
3099 if(allow_blocking || TryEnterCriticalSection(&req->read_section)) {
3100 if(allow_blocking)
3101 EnterCriticalSection(&req->read_section);
3102 if(hdr->dwError == ERROR_SUCCESS)
3103 hdr->dwError = INTERNET_HANDLE_IN_USE;
3104 else if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3105 hdr->dwError = ERROR_INTERNET_INTERNAL_ERROR;
3107 if(req->read_size) {
3108 read = min(size, req->read_size);
3109 memcpy(buf, req->read_buf + req->read_pos, read);
3110 req->read_size -= read;
3111 req->read_pos += read;
3114 if(read < size && (!read || !(flags & IRF_NO_WAIT)) && !end_of_read_data(req)) {
3115 LeaveCriticalSection(&req->read_section);
3116 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3117 EnterCriticalSection( &req->read_section );
3118 notify_received = TRUE;
3120 while(read < size) {
3121 res = HTTPREQ_Read(req, (char*)buf+read, size-read, &cread, allow_blocking);
3122 read += cread;
3123 if (res != ERROR_SUCCESS || !cread)
3124 break;
3128 if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3129 hdr->dwError = ERROR_SUCCESS;
3130 else
3131 error = hdr->dwError;
3133 LeaveCriticalSection( &req->read_section );
3134 }else {
3135 res = WSAEWOULDBLOCK;
3138 if(res == WSAEWOULDBLOCK) {
3139 if(!(flags & IRF_NO_WAIT))
3140 return async_read(req, buf, size, read, ret_read);
3141 if(!read)
3142 return async_read(req, NULL, 0, 0, NULL);
3143 res = ERROR_SUCCESS;
3146 *ret_read = read;
3147 if (res != ERROR_SUCCESS)
3148 return res;
3150 if(notify_received)
3151 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
3152 &read, sizeof(read));
3153 return error;
3156 static DWORD HTTPREQ_WriteFile(object_header_t *hdr, const void *buffer, DWORD size, DWORD *written)
3158 DWORD res;
3159 http_request_t *request = (http_request_t*)hdr;
3161 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
3163 *written = 0;
3164 res = NETCON_send(request->netconn, buffer, size, 0, (LPINT)written);
3165 if (res == ERROR_SUCCESS)
3166 request->bytesWritten += *written;
3168 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REQUEST_SENT, written, sizeof(DWORD));
3169 return res;
3172 static DWORD HTTPREQ_QueryDataAvailable(object_header_t *hdr, DWORD *available, DWORD flags, DWORD_PTR ctx)
3174 http_request_t *req = (http_request_t*)hdr;
3175 DWORD res = ERROR_SUCCESS, avail = 0, error = ERROR_SUCCESS;
3176 BOOL allow_blocking, notify_received = FALSE;
3178 TRACE("(%p %p %x %lx)\n", req, available, flags, ctx);
3180 if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
3181 FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
3183 *available = 0;
3184 allow_blocking = !(req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC);
3186 if(allow_blocking || TryEnterCriticalSection(&req->read_section)) {
3187 if(allow_blocking)
3188 EnterCriticalSection(&req->read_section);
3189 if(hdr->dwError == ERROR_SUCCESS)
3190 hdr->dwError = INTERNET_HANDLE_IN_USE;
3191 else if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3192 hdr->dwError = ERROR_INTERNET_INTERNAL_ERROR;
3194 avail = req->read_size;
3196 if(!avail && !end_of_read_data(req)) {
3197 LeaveCriticalSection(&req->read_section);
3198 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3199 EnterCriticalSection( &req->read_section );
3200 notify_received = TRUE;
3202 res = refill_read_buffer(req, allow_blocking, &avail);
3205 if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3206 hdr->dwError = ERROR_SUCCESS;
3207 else
3208 error = hdr->dwError;
3210 LeaveCriticalSection( &req->read_section );
3211 }else {
3212 res = WSAEWOULDBLOCK;
3215 if(res == WSAEWOULDBLOCK)
3216 return async_read(req, NULL, 0, 0, available);
3218 if (res != ERROR_SUCCESS)
3219 return res;
3221 *available = avail;
3222 if(notify_received)
3223 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
3224 &avail, sizeof(avail));
3225 return error;
3228 static DWORD HTTPREQ_LockRequestFile(object_header_t *hdr, req_file_t **ret)
3230 http_request_t *req = (http_request_t*)hdr;
3232 TRACE("(%p)\n", req);
3234 if(!req->req_file) {
3235 WARN("No cache file name available\n");
3236 return ERROR_FILE_NOT_FOUND;
3239 *ret = req_file_addref(req->req_file);
3240 return ERROR_SUCCESS;
3243 static const object_vtbl_t HTTPREQVtbl = {
3244 HTTPREQ_Destroy,
3245 HTTPREQ_CloseConnection,
3246 HTTPREQ_QueryOption,
3247 HTTPREQ_SetOption,
3248 HTTPREQ_ReadFile,
3249 HTTPREQ_WriteFile,
3250 HTTPREQ_QueryDataAvailable,
3251 NULL,
3252 HTTPREQ_LockRequestFile
3255 /***********************************************************************
3256 * HTTP_HttpOpenRequestW (internal)
3258 * Open a HTTP request handle
3260 * RETURNS
3261 * HINTERNET a HTTP request handle on success
3262 * NULL on failure
3265 static DWORD HTTP_HttpOpenRequestW(http_session_t *session,
3266 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
3267 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
3268 DWORD dwFlags, DWORD_PTR dwContext, HINTERNET *ret)
3270 appinfo_t *hIC = session->appInfo;
3271 http_request_t *request;
3272 DWORD port, len;
3274 TRACE("-->\n");
3276 request = alloc_object(&session->hdr, &HTTPREQVtbl, sizeof(http_request_t));
3277 if(!request)
3278 return ERROR_OUTOFMEMORY;
3280 request->hdr.htype = WH_HHTTPREQ;
3281 request->hdr.dwFlags = dwFlags;
3282 request->hdr.dwContext = dwContext;
3283 request->contentLength = ~0u;
3285 request->netconn_stream.data_stream.vtbl = &netconn_stream_vtbl;
3286 request->data_stream = &request->netconn_stream.data_stream;
3287 request->connect_timeout = session->connect_timeout;
3288 request->send_timeout = session->send_timeout;
3289 request->receive_timeout = session->receive_timeout;
3291 InitializeCriticalSection( &request->headers_section );
3292 request->headers_section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": http_request_t.headers_section");
3294 InitializeCriticalSection( &request->read_section );
3295 request->read_section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": http_request_t.read_section");
3297 WININET_AddRef( &session->hdr );
3298 request->session = session;
3299 list_add_head( &session->hdr.children, &request->hdr.entry );
3301 port = session->hostPort;
3302 if (port == INTERNET_INVALID_PORT_NUMBER)
3303 port = (session->hdr.dwFlags & INTERNET_FLAG_SECURE) ?
3304 INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT;
3306 request->server = get_server(substrz(session->hostName), port, (dwFlags & INTERNET_FLAG_SECURE) != 0, TRUE);
3307 if(!request->server) {
3308 WININET_Release(&request->hdr);
3309 return ERROR_OUTOFMEMORY;
3312 if (dwFlags & INTERNET_FLAG_IGNORE_CERT_CN_INVALID)
3313 request->security_flags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
3314 if (dwFlags & INTERNET_FLAG_IGNORE_CERT_DATE_INVALID)
3315 request->security_flags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
3317 if (lpszObjectName && *lpszObjectName) {
3318 HRESULT rc;
3319 WCHAR dummy;
3321 len = 1;
3322 rc = UrlCanonicalizeW(lpszObjectName, &dummy, &len, URL_ESCAPE_SPACES_ONLY);
3323 if (rc != E_POINTER)
3324 len = strlenW(lpszObjectName)+1;
3325 request->path = heap_alloc(len*sizeof(WCHAR));
3326 rc = UrlCanonicalizeW(lpszObjectName, request->path, &len,
3327 URL_ESCAPE_SPACES_ONLY);
3328 if (rc != S_OK)
3330 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc);
3331 strcpyW(request->path,lpszObjectName);
3333 }else {
3334 static const WCHAR slashW[] = {'/',0};
3336 request->path = heap_strdupW(slashW);
3339 if (lpszReferrer && *lpszReferrer)
3340 HTTP_ProcessHeader(request, HTTP_REFERER, lpszReferrer, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
3342 if (lpszAcceptTypes)
3344 int i;
3345 for (i = 0; lpszAcceptTypes[i]; i++)
3347 if (!*lpszAcceptTypes[i]) continue;
3348 HTTP_ProcessHeader(request, HTTP_ACCEPT, lpszAcceptTypes[i],
3349 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA |
3350 HTTP_ADDHDR_FLAG_REQ |
3351 (i == 0 ? (HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_ADD) : 0));
3355 request->verb = heap_strdupW(lpszVerb && *lpszVerb ? lpszVerb : szGET);
3356 request->version = heap_strdupW(lpszVersion && *lpszVersion ? lpszVersion : g_szHttp1_1);
3358 if (hIC->proxy && hIC->proxy[0] && !HTTP_ShouldBypassProxy(hIC, session->hostName))
3359 HTTP_DealWithProxy( hIC, session, request );
3361 INTERNET_SendCallback(&session->hdr, dwContext,
3362 INTERNET_STATUS_HANDLE_CREATED, &request->hdr.hInternet,
3363 sizeof(HINTERNET));
3365 TRACE("<-- (%p)\n", request);
3367 *ret = request->hdr.hInternet;
3368 return ERROR_SUCCESS;
3371 /***********************************************************************
3372 * HttpOpenRequestW (WININET.@)
3374 * Open a HTTP request handle
3376 * RETURNS
3377 * HINTERNET a HTTP request handle on success
3378 * NULL on failure
3381 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
3382 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
3383 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
3384 DWORD dwFlags, DWORD_PTR dwContext)
3386 http_session_t *session;
3387 HINTERNET handle = NULL;
3388 DWORD res;
3390 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
3391 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
3392 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
3393 dwFlags, dwContext);
3394 if(lpszAcceptTypes!=NULL)
3396 int i;
3397 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
3398 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
3401 session = (http_session_t*) get_handle_object( hHttpSession );
3402 if (NULL == session || session->hdr.htype != WH_HHTTPSESSION)
3404 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3405 goto lend;
3409 * My tests seem to show that the windows version does not
3410 * become asynchronous until after this point. And anyhow
3411 * if this call was asynchronous then how would you get the
3412 * necessary HINTERNET pointer returned by this function.
3415 res = HTTP_HttpOpenRequestW(session, lpszVerb, lpszObjectName,
3416 lpszVersion, lpszReferrer, lpszAcceptTypes,
3417 dwFlags, dwContext, &handle);
3418 lend:
3419 if( session )
3420 WININET_Release( &session->hdr );
3421 TRACE("returning %p\n", handle);
3422 if(res != ERROR_SUCCESS)
3423 SetLastError(res);
3424 return handle;
3427 static const LPCWSTR header_lookup[] = {
3428 szMime_Version, /* HTTP_QUERY_MIME_VERSION = 0 */
3429 szContent_Type, /* HTTP_QUERY_CONTENT_TYPE = 1 */
3430 szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
3431 szContent_ID, /* HTTP_QUERY_CONTENT_ID = 3 */
3432 NULL, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
3433 szContent_Length, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
3434 szContent_Language, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
3435 szAllow, /* HTTP_QUERY_ALLOW = 7 */
3436 szPublic, /* HTTP_QUERY_PUBLIC = 8 */
3437 szDate, /* HTTP_QUERY_DATE = 9 */
3438 szExpires, /* HTTP_QUERY_EXPIRES = 10 */
3439 szLast_Modified, /* HTTP_QUERY_LAST_MODIFIED = 11 */
3440 NULL, /* HTTP_QUERY_MESSAGE_ID = 12 */
3441 szURI, /* HTTP_QUERY_URI = 13 */
3442 szFrom, /* HTTP_QUERY_DERIVED_FROM = 14 */
3443 NULL, /* HTTP_QUERY_COST = 15 */
3444 NULL, /* HTTP_QUERY_LINK = 16 */
3445 szPragma, /* HTTP_QUERY_PRAGMA = 17 */
3446 NULL, /* HTTP_QUERY_VERSION = 18 */
3447 szStatus, /* HTTP_QUERY_STATUS_CODE = 19 */
3448 NULL, /* HTTP_QUERY_STATUS_TEXT = 20 */
3449 NULL, /* HTTP_QUERY_RAW_HEADERS = 21 */
3450 NULL, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
3451 szConnection, /* HTTP_QUERY_CONNECTION = 23 */
3452 szAccept, /* HTTP_QUERY_ACCEPT = 24 */
3453 szAccept_Charset, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
3454 szAccept_Encoding, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
3455 szAccept_Language, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
3456 szAuthorization, /* HTTP_QUERY_AUTHORIZATION = 28 */
3457 szContent_Encoding, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
3458 NULL, /* HTTP_QUERY_FORWARDED = 30 */
3459 NULL, /* HTTP_QUERY_FROM = 31 */
3460 szIf_Modified_Since, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
3461 szLocation, /* HTTP_QUERY_LOCATION = 33 */
3462 NULL, /* HTTP_QUERY_ORIG_URI = 34 */
3463 szReferer, /* HTTP_QUERY_REFERER = 35 */
3464 szRetry_After, /* HTTP_QUERY_RETRY_AFTER = 36 */
3465 szServer, /* HTTP_QUERY_SERVER = 37 */
3466 NULL, /* HTTP_TITLE = 38 */
3467 szUser_Agent, /* HTTP_QUERY_USER_AGENT = 39 */
3468 szWWW_Authenticate, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
3469 szProxy_Authenticate, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
3470 szAccept_Ranges, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
3471 szSet_Cookie, /* HTTP_QUERY_SET_COOKIE = 43 */
3472 szCookie, /* HTTP_QUERY_COOKIE = 44 */
3473 NULL, /* HTTP_QUERY_REQUEST_METHOD = 45 */
3474 NULL, /* HTTP_QUERY_REFRESH = 46 */
3475 szContent_Disposition, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
3476 szAge, /* HTTP_QUERY_AGE = 48 */
3477 szCache_Control, /* HTTP_QUERY_CACHE_CONTROL = 49 */
3478 szContent_Base, /* HTTP_QUERY_CONTENT_BASE = 50 */
3479 szContent_Location, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
3480 szContent_MD5, /* HTTP_QUERY_CONTENT_MD5 = 52 */
3481 szContent_Range, /* HTTP_QUERY_CONTENT_RANGE = 53 */
3482 szETag, /* HTTP_QUERY_ETAG = 54 */
3483 hostW, /* HTTP_QUERY_HOST = 55 */
3484 szIf_Match, /* HTTP_QUERY_IF_MATCH = 56 */
3485 szIf_None_Match, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
3486 szIf_Range, /* HTTP_QUERY_IF_RANGE = 58 */
3487 szIf_Unmodified_Since, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
3488 szMax_Forwards, /* HTTP_QUERY_MAX_FORWARDS = 60 */
3489 szProxy_Authorization, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
3490 szRange, /* HTTP_QUERY_RANGE = 62 */
3491 szTransfer_Encoding, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
3492 szUpgrade, /* HTTP_QUERY_UPGRADE = 64 */
3493 szVary, /* HTTP_QUERY_VARY = 65 */
3494 szVia, /* HTTP_QUERY_VIA = 66 */
3495 szWarning, /* HTTP_QUERY_WARNING = 67 */
3496 szExpect, /* HTTP_QUERY_EXPECT = 68 */
3497 szProxy_Connection, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
3498 szUnless_Modified_Since, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
3501 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
3503 /***********************************************************************
3504 * HTTP_HttpQueryInfoW (internal)
3506 static DWORD HTTP_HttpQueryInfoW(http_request_t *request, DWORD dwInfoLevel,
3507 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
3509 LPHTTPHEADERW lphttpHdr = NULL;
3510 BOOL request_only = !!(dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS);
3511 INT requested_index = lpdwIndex ? *lpdwIndex : 0;
3512 DWORD level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
3513 INT index = -1;
3515 EnterCriticalSection( &request->headers_section );
3517 /* Find requested header structure */
3518 switch (level)
3520 case HTTP_QUERY_CUSTOM:
3521 if (!lpBuffer)
3523 LeaveCriticalSection( &request->headers_section );
3524 return ERROR_INVALID_PARAMETER;
3526 index = HTTP_GetCustomHeaderIndex(request, lpBuffer, requested_index, request_only);
3527 break;
3528 case HTTP_QUERY_RAW_HEADERS_CRLF:
3530 LPWSTR headers;
3531 DWORD len = 0;
3532 DWORD res = ERROR_INVALID_PARAMETER;
3534 if (request_only)
3535 headers = build_request_header(request, request->verb, request->path, request->version, TRUE);
3536 else
3537 headers = build_response_header(request, TRUE);
3538 if (!headers)
3540 LeaveCriticalSection( &request->headers_section );
3541 return ERROR_OUTOFMEMORY;
3544 len = strlenW(headers) * sizeof(WCHAR);
3545 if (len + sizeof(WCHAR) > *lpdwBufferLength)
3547 len += sizeof(WCHAR);
3548 res = ERROR_INSUFFICIENT_BUFFER;
3550 else if (lpBuffer)
3552 memcpy(lpBuffer, headers, len + sizeof(WCHAR));
3553 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len / sizeof(WCHAR)));
3554 res = ERROR_SUCCESS;
3556 *lpdwBufferLength = len;
3558 heap_free(headers);
3559 LeaveCriticalSection( &request->headers_section );
3560 return res;
3562 case HTTP_QUERY_RAW_HEADERS:
3564 LPWSTR headers;
3565 DWORD len;
3567 if (request_only)
3568 headers = build_request_header(request, request->verb, request->path, request->version, FALSE);
3569 else
3570 headers = build_response_header(request, FALSE);
3572 if (!headers)
3574 LeaveCriticalSection( &request->headers_section );
3575 return ERROR_OUTOFMEMORY;
3578 len = strlenW(headers) * sizeof(WCHAR);
3579 if (len > *lpdwBufferLength)
3581 *lpdwBufferLength = len;
3582 heap_free(headers);
3583 LeaveCriticalSection( &request->headers_section );
3584 return ERROR_INSUFFICIENT_BUFFER;
3587 if (lpBuffer)
3589 DWORD i;
3591 TRACE("returning data: %s\n", debugstr_wn(headers, len / sizeof(WCHAR)));
3593 for (i = 0; i < len / sizeof(WCHAR); i++)
3595 if (headers[i] == '\n')
3596 headers[i] = 0;
3598 memcpy(lpBuffer, headers, len);
3600 *lpdwBufferLength = len - sizeof(WCHAR);
3602 heap_free(headers);
3603 LeaveCriticalSection( &request->headers_section );
3604 return ERROR_SUCCESS;
3606 case HTTP_QUERY_STATUS_TEXT:
3607 if (request->statusText)
3609 DWORD len = strlenW(request->statusText);
3610 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
3612 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
3613 LeaveCriticalSection( &request->headers_section );
3614 return ERROR_INSUFFICIENT_BUFFER;
3616 if (lpBuffer)
3618 memcpy(lpBuffer, request->statusText, (len + 1) * sizeof(WCHAR));
3619 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
3621 *lpdwBufferLength = len * sizeof(WCHAR);
3622 LeaveCriticalSection( &request->headers_section );
3623 return ERROR_SUCCESS;
3625 break;
3626 case HTTP_QUERY_VERSION:
3627 if (request->version)
3629 DWORD len = strlenW(request->version);
3630 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
3632 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
3633 LeaveCriticalSection( &request->headers_section );
3634 return ERROR_INSUFFICIENT_BUFFER;
3636 if (lpBuffer)
3638 memcpy(lpBuffer, request->version, (len + 1) * sizeof(WCHAR));
3639 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
3641 *lpdwBufferLength = len * sizeof(WCHAR);
3642 LeaveCriticalSection( &request->headers_section );
3643 return ERROR_SUCCESS;
3645 break;
3646 case HTTP_QUERY_CONTENT_ENCODING:
3647 index = HTTP_GetCustomHeaderIndex(request, header_lookup[request->read_gzip ? HTTP_QUERY_CONTENT_TYPE : level],
3648 requested_index,request_only);
3649 break;
3650 case HTTP_QUERY_STATUS_CODE: {
3651 DWORD res = ERROR_SUCCESS;
3653 if(request_only)
3655 LeaveCriticalSection( &request->headers_section );
3656 return ERROR_HTTP_INVALID_QUERY_REQUEST;
3659 if(requested_index)
3660 break;
3662 if(dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) {
3663 if(*lpdwBufferLength >= sizeof(DWORD))
3664 *(DWORD*)lpBuffer = request->status_code;
3665 else
3666 res = ERROR_INSUFFICIENT_BUFFER;
3667 *lpdwBufferLength = sizeof(DWORD);
3668 }else {
3669 WCHAR buf[12];
3670 DWORD size;
3671 static const WCHAR formatW[] = {'%','u',0};
3673 size = sprintfW(buf, formatW, request->status_code) * sizeof(WCHAR);
3675 if(size <= *lpdwBufferLength) {
3676 memcpy(lpBuffer, buf, size+sizeof(WCHAR));
3677 }else {
3678 size += sizeof(WCHAR);
3679 res = ERROR_INSUFFICIENT_BUFFER;
3682 *lpdwBufferLength = size;
3684 LeaveCriticalSection( &request->headers_section );
3685 return res;
3687 default:
3688 assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
3690 if (level < LAST_TABLE_HEADER && header_lookup[level])
3691 index = HTTP_GetCustomHeaderIndex(request, header_lookup[level],
3692 requested_index,request_only);
3695 if (index >= 0)
3696 lphttpHdr = &request->custHeaders[index];
3698 /* Ensure header satisfies requested attributes */
3699 if (!lphttpHdr ||
3700 ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
3701 (~lphttpHdr->wFlags & HDR_ISREQUEST)))
3703 LeaveCriticalSection( &request->headers_section );
3704 return ERROR_HTTP_HEADER_NOT_FOUND;
3707 /* coalesce value to requested type */
3708 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER && lpBuffer)
3710 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
3711 TRACE(" returning number: %d\n", *(int *)lpBuffer);
3713 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME && lpBuffer)
3715 time_t tmpTime;
3716 struct tm tmpTM;
3717 SYSTEMTIME *STHook;
3719 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
3721 tmpTM = *gmtime(&tmpTime);
3722 STHook = (SYSTEMTIME *)lpBuffer;
3723 STHook->wDay = tmpTM.tm_mday;
3724 STHook->wHour = tmpTM.tm_hour;
3725 STHook->wMilliseconds = 0;
3726 STHook->wMinute = tmpTM.tm_min;
3727 STHook->wDayOfWeek = tmpTM.tm_wday;
3728 STHook->wMonth = tmpTM.tm_mon + 1;
3729 STHook->wSecond = tmpTM.tm_sec;
3730 STHook->wYear = 1900+tmpTM.tm_year;
3732 TRACE(" returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
3733 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
3734 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
3736 else if (lphttpHdr->lpszValue)
3738 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
3740 if (len > *lpdwBufferLength)
3742 *lpdwBufferLength = len;
3743 LeaveCriticalSection( &request->headers_section );
3744 return ERROR_INSUFFICIENT_BUFFER;
3746 if (lpBuffer)
3748 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
3749 TRACE("! returning string: %s\n", debugstr_w(lpBuffer));
3751 *lpdwBufferLength = len - sizeof(WCHAR);
3753 if (lpdwIndex) (*lpdwIndex)++;
3755 LeaveCriticalSection( &request->headers_section );
3756 return ERROR_SUCCESS;
3759 /***********************************************************************
3760 * HttpQueryInfoW (WININET.@)
3762 * Queries for information about an HTTP request
3764 * RETURNS
3765 * TRUE on success
3766 * FALSE on failure
3769 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
3770 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
3772 http_request_t *request;
3773 DWORD res;
3775 if (TRACE_ON(wininet)) {
3776 #define FE(x) { x, #x }
3777 static const wininet_flag_info query_flags[] = {
3778 FE(HTTP_QUERY_MIME_VERSION),
3779 FE(HTTP_QUERY_CONTENT_TYPE),
3780 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
3781 FE(HTTP_QUERY_CONTENT_ID),
3782 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
3783 FE(HTTP_QUERY_CONTENT_LENGTH),
3784 FE(HTTP_QUERY_CONTENT_LANGUAGE),
3785 FE(HTTP_QUERY_ALLOW),
3786 FE(HTTP_QUERY_PUBLIC),
3787 FE(HTTP_QUERY_DATE),
3788 FE(HTTP_QUERY_EXPIRES),
3789 FE(HTTP_QUERY_LAST_MODIFIED),
3790 FE(HTTP_QUERY_MESSAGE_ID),
3791 FE(HTTP_QUERY_URI),
3792 FE(HTTP_QUERY_DERIVED_FROM),
3793 FE(HTTP_QUERY_COST),
3794 FE(HTTP_QUERY_LINK),
3795 FE(HTTP_QUERY_PRAGMA),
3796 FE(HTTP_QUERY_VERSION),
3797 FE(HTTP_QUERY_STATUS_CODE),
3798 FE(HTTP_QUERY_STATUS_TEXT),
3799 FE(HTTP_QUERY_RAW_HEADERS),
3800 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
3801 FE(HTTP_QUERY_CONNECTION),
3802 FE(HTTP_QUERY_ACCEPT),
3803 FE(HTTP_QUERY_ACCEPT_CHARSET),
3804 FE(HTTP_QUERY_ACCEPT_ENCODING),
3805 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
3806 FE(HTTP_QUERY_AUTHORIZATION),
3807 FE(HTTP_QUERY_CONTENT_ENCODING),
3808 FE(HTTP_QUERY_FORWARDED),
3809 FE(HTTP_QUERY_FROM),
3810 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
3811 FE(HTTP_QUERY_LOCATION),
3812 FE(HTTP_QUERY_ORIG_URI),
3813 FE(HTTP_QUERY_REFERER),
3814 FE(HTTP_QUERY_RETRY_AFTER),
3815 FE(HTTP_QUERY_SERVER),
3816 FE(HTTP_QUERY_TITLE),
3817 FE(HTTP_QUERY_USER_AGENT),
3818 FE(HTTP_QUERY_WWW_AUTHENTICATE),
3819 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
3820 FE(HTTP_QUERY_ACCEPT_RANGES),
3821 FE(HTTP_QUERY_SET_COOKIE),
3822 FE(HTTP_QUERY_COOKIE),
3823 FE(HTTP_QUERY_REQUEST_METHOD),
3824 FE(HTTP_QUERY_REFRESH),
3825 FE(HTTP_QUERY_CONTENT_DISPOSITION),
3826 FE(HTTP_QUERY_AGE),
3827 FE(HTTP_QUERY_CACHE_CONTROL),
3828 FE(HTTP_QUERY_CONTENT_BASE),
3829 FE(HTTP_QUERY_CONTENT_LOCATION),
3830 FE(HTTP_QUERY_CONTENT_MD5),
3831 FE(HTTP_QUERY_CONTENT_RANGE),
3832 FE(HTTP_QUERY_ETAG),
3833 FE(HTTP_QUERY_HOST),
3834 FE(HTTP_QUERY_IF_MATCH),
3835 FE(HTTP_QUERY_IF_NONE_MATCH),
3836 FE(HTTP_QUERY_IF_RANGE),
3837 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
3838 FE(HTTP_QUERY_MAX_FORWARDS),
3839 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
3840 FE(HTTP_QUERY_RANGE),
3841 FE(HTTP_QUERY_TRANSFER_ENCODING),
3842 FE(HTTP_QUERY_UPGRADE),
3843 FE(HTTP_QUERY_VARY),
3844 FE(HTTP_QUERY_VIA),
3845 FE(HTTP_QUERY_WARNING),
3846 FE(HTTP_QUERY_CUSTOM)
3848 static const wininet_flag_info modifier_flags[] = {
3849 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
3850 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
3851 FE(HTTP_QUERY_FLAG_NUMBER),
3852 FE(HTTP_QUERY_FLAG_COALESCE)
3854 #undef FE
3855 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
3856 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
3857 DWORD i;
3859 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, info);
3860 TRACE(" Attribute:");
3861 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
3862 if (query_flags[i].val == info) {
3863 TRACE(" %s", query_flags[i].name);
3864 break;
3867 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
3868 TRACE(" Unknown (%08x)", info);
3871 TRACE(" Modifier:");
3872 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
3873 if (modifier_flags[i].val & info_mod) {
3874 TRACE(" %s", modifier_flags[i].name);
3875 info_mod &= ~ modifier_flags[i].val;
3879 if (info_mod) {
3880 TRACE(" Unknown (%08x)", info_mod);
3882 TRACE("\n");
3885 request = (http_request_t*) get_handle_object( hHttpRequest );
3886 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
3888 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3889 goto lend;
3892 if (lpBuffer == NULL)
3893 *lpdwBufferLength = 0;
3894 res = HTTP_HttpQueryInfoW( request, dwInfoLevel,
3895 lpBuffer, lpdwBufferLength, lpdwIndex);
3897 lend:
3898 if( request )
3899 WININET_Release( &request->hdr );
3901 TRACE("%u <--\n", res);
3902 if(res != ERROR_SUCCESS)
3903 SetLastError(res);
3904 return res == ERROR_SUCCESS;
3907 /***********************************************************************
3908 * HttpQueryInfoA (WININET.@)
3910 * Queries for information about an HTTP request
3912 * RETURNS
3913 * TRUE on success
3914 * FALSE on failure
3917 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
3918 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
3920 BOOL result;
3921 DWORD len;
3922 WCHAR* bufferW;
3924 TRACE("%p %x\n", hHttpRequest, dwInfoLevel);
3926 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
3927 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
3929 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
3930 lpdwBufferLength, lpdwIndex );
3933 if (lpBuffer)
3935 DWORD alloclen;
3936 len = (*lpdwBufferLength)*sizeof(WCHAR);
3937 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
3939 alloclen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 ) * sizeof(WCHAR);
3940 if (alloclen < len)
3941 alloclen = len;
3943 else
3944 alloclen = len;
3945 bufferW = heap_alloc(alloclen);
3946 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
3947 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
3948 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, bufferW, alloclen / sizeof(WCHAR) );
3949 } else
3951 bufferW = NULL;
3952 len = 0;
3955 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
3956 &len, lpdwIndex );
3957 if( result )
3959 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
3960 lpBuffer, *lpdwBufferLength, NULL, NULL );
3961 *lpdwBufferLength = len - 1;
3963 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
3965 else
3966 /* since the strings being returned from HttpQueryInfoW should be
3967 * only ASCII characters, it is reasonable to assume that all of
3968 * the Unicode characters can be reduced to a single byte */
3969 *lpdwBufferLength = len / sizeof(WCHAR);
3971 heap_free( bufferW );
3972 return result;
3975 static WCHAR *get_redirect_url(http_request_t *request)
3977 static WCHAR szHttp[] = {'h','t','t','p',0};
3978 static WCHAR szHttps[] = {'h','t','t','p','s',0};
3979 http_session_t *session = request->session;
3980 URL_COMPONENTSW urlComponents = { sizeof(urlComponents) };
3981 WCHAR *orig_url = NULL, *redirect_url = NULL, *combined_url = NULL;
3982 DWORD url_length = 0, res;
3983 BOOL b;
3985 url_length = 0;
3986 res = HTTP_HttpQueryInfoW(request, HTTP_QUERY_LOCATION, redirect_url, &url_length, NULL);
3987 if(res == ERROR_INSUFFICIENT_BUFFER) {
3988 redirect_url = heap_alloc(url_length);
3989 res = HTTP_HttpQueryInfoW(request, HTTP_QUERY_LOCATION, redirect_url, &url_length, NULL);
3991 if(res != ERROR_SUCCESS) {
3992 heap_free(redirect_url);
3993 return NULL;
3996 urlComponents.dwSchemeLength = 1;
3997 b = InternetCrackUrlW(redirect_url, url_length, 0, &urlComponents);
3998 if(b && urlComponents.dwSchemeLength &&
3999 urlComponents.nScheme != INTERNET_SCHEME_HTTP && urlComponents.nScheme != INTERNET_SCHEME_HTTPS) {
4000 TRACE("redirect to non-http URL\n");
4001 return NULL;
4004 urlComponents.lpszScheme = (request->hdr.dwFlags & INTERNET_FLAG_SECURE) ? szHttps : szHttp;
4005 urlComponents.dwSchemeLength = 0;
4006 urlComponents.lpszHostName = request->server->name;
4007 urlComponents.nPort = request->server->port;
4008 urlComponents.lpszUserName = session->userName;
4009 urlComponents.lpszUrlPath = request->path;
4011 b = InternetCreateUrlW(&urlComponents, 0, NULL, &url_length);
4012 if(!b && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
4013 orig_url = heap_alloc(url_length);
4015 /* convert from bytes to characters */
4016 url_length = url_length / sizeof(WCHAR) - 1;
4017 b = InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length);
4020 if(b) {
4021 url_length = 0;
4022 b = InternetCombineUrlW(orig_url, redirect_url, NULL, &url_length, ICU_ENCODE_SPACES_ONLY);
4023 if(!b && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
4024 combined_url = heap_alloc(url_length * sizeof(WCHAR));
4025 b = InternetCombineUrlW(orig_url, redirect_url, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY);
4026 if(!b) {
4027 heap_free(combined_url);
4028 combined_url = NULL;
4033 heap_free(orig_url);
4034 heap_free(redirect_url);
4035 return combined_url;
4039 /***********************************************************************
4040 * HTTP_HandleRedirect (internal)
4042 static DWORD HTTP_HandleRedirect(http_request_t *request, WCHAR *url)
4044 URL_COMPONENTSW urlComponents = { sizeof(urlComponents) };
4045 http_session_t *session = request->session;
4046 size_t url_len = strlenW(url);
4048 if(url[0] == '/')
4050 /* if it's an absolute path, keep the same session info */
4051 urlComponents.lpszUrlPath = url;
4052 urlComponents.dwUrlPathLength = url_len;
4054 else
4056 urlComponents.dwHostNameLength = 1;
4057 urlComponents.dwUserNameLength = 1;
4058 urlComponents.dwUrlPathLength = 1;
4059 if(!InternetCrackUrlW(url, url_len, 0, &urlComponents))
4060 return INTERNET_GetLastError();
4062 if(!urlComponents.dwHostNameLength)
4063 return ERROR_INTERNET_INVALID_URL;
4066 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REDIRECT,
4067 url, (url_len + 1) * sizeof(WCHAR));
4069 if(urlComponents.dwHostNameLength) {
4070 BOOL custom_port = FALSE;
4071 substr_t host;
4073 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP) {
4074 if(request->hdr.dwFlags & INTERNET_FLAG_SECURE) {
4075 TRACE("redirect from secure page to non-secure page\n");
4076 /* FIXME: warn about from secure redirect to non-secure page */
4077 request->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
4080 custom_port = urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT;
4081 }else if(urlComponents.nScheme == INTERNET_SCHEME_HTTPS) {
4082 if(!(request->hdr.dwFlags & INTERNET_FLAG_SECURE)) {
4083 TRACE("redirect from non-secure page to secure page\n");
4084 /* FIXME: notify about redirect to secure page */
4085 request->hdr.dwFlags |= INTERNET_FLAG_SECURE;
4088 custom_port = urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT;
4091 heap_free(session->hostName);
4093 session->hostName = heap_strndupW(urlComponents.lpszHostName, urlComponents.dwHostNameLength);
4094 session->hostPort = urlComponents.nPort;
4096 heap_free(session->userName);
4097 session->userName = NULL;
4098 if (urlComponents.dwUserNameLength)
4099 session->userName = heap_strndupW(urlComponents.lpszUserName, urlComponents.dwUserNameLength);
4101 reset_data_stream(request);
4103 host = substr(urlComponents.lpszHostName, urlComponents.dwHostNameLength);
4105 if(host.len != strlenW(request->server->name) || strncmpiW(request->server->name, host.str, host.len)
4106 || request->server->port != urlComponents.nPort) {
4107 server_t *new_server;
4109 new_server = get_server(host, urlComponents.nPort, urlComponents.nScheme == INTERNET_SCHEME_HTTPS, TRUE);
4110 server_release(request->server);
4111 request->server = new_server;
4114 if (custom_port)
4115 HTTP_ProcessHeader(request, hostW, request->server->host_port, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
4116 else
4117 HTTP_ProcessHeader(request, hostW, request->server->name, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
4120 heap_free(request->path);
4121 request->path = NULL;
4122 if(urlComponents.dwUrlPathLength)
4124 DWORD needed = 1;
4125 HRESULT rc;
4126 WCHAR dummy = 0;
4127 WCHAR *path;
4129 path = heap_strndupW(urlComponents.lpszUrlPath, urlComponents.dwUrlPathLength);
4130 rc = UrlEscapeW(path, &dummy, &needed, URL_ESCAPE_SPACES_ONLY);
4131 if (rc != E_POINTER)
4132 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
4133 request->path = heap_alloc(needed*sizeof(WCHAR));
4134 rc = UrlEscapeW(path, request->path, &needed,
4135 URL_ESCAPE_SPACES_ONLY);
4136 if (rc != S_OK)
4138 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
4139 strcpyW(request->path, path);
4141 heap_free(path);
4144 /* Remove custom content-type/length headers on redirects. */
4145 remove_header(request, szContent_Type, TRUE);
4146 remove_header(request, szContent_Length, TRUE);
4148 return ERROR_SUCCESS;
4151 /***********************************************************************
4152 * HTTP_build_req (internal)
4154 * concatenate all the strings in the request together
4156 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
4158 LPCWSTR *t;
4159 LPWSTR str;
4161 for( t = list; *t ; t++ )
4162 len += strlenW( *t );
4163 len++;
4165 str = heap_alloc(len*sizeof(WCHAR));
4166 *str = 0;
4168 for( t = list; *t ; t++ )
4169 strcatW( str, *t );
4171 return str;
4174 static void HTTP_InsertCookies(http_request_t *request)
4176 WCHAR *cookies;
4177 DWORD res;
4179 res = get_cookie_header(request->server->name, request->path, &cookies);
4180 if(res != ERROR_SUCCESS || !cookies)
4181 return;
4183 HTTP_HttpAddRequestHeadersW(request, cookies, strlenW(cookies),
4184 HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
4185 heap_free(cookies);
4188 static WORD HTTP_ParseWkday(LPCWSTR day)
4190 static const WCHAR days[7][4] = {{ 's','u','n',0 },
4191 { 'm','o','n',0 },
4192 { 't','u','e',0 },
4193 { 'w','e','d',0 },
4194 { 't','h','u',0 },
4195 { 'f','r','i',0 },
4196 { 's','a','t',0 }};
4197 unsigned int i;
4198 for (i = 0; i < sizeof(days)/sizeof(*days); i++)
4199 if (!strcmpiW(day, days[i]))
4200 return i;
4202 /* Invalid */
4203 return 7;
4206 static WORD HTTP_ParseMonth(LPCWSTR month)
4208 static const WCHAR jan[] = { 'j','a','n',0 };
4209 static const WCHAR feb[] = { 'f','e','b',0 };
4210 static const WCHAR mar[] = { 'm','a','r',0 };
4211 static const WCHAR apr[] = { 'a','p','r',0 };
4212 static const WCHAR may[] = { 'm','a','y',0 };
4213 static const WCHAR jun[] = { 'j','u','n',0 };
4214 static const WCHAR jul[] = { 'j','u','l',0 };
4215 static const WCHAR aug[] = { 'a','u','g',0 };
4216 static const WCHAR sep[] = { 's','e','p',0 };
4217 static const WCHAR oct[] = { 'o','c','t',0 };
4218 static const WCHAR nov[] = { 'n','o','v',0 };
4219 static const WCHAR dec[] = { 'd','e','c',0 };
4221 if (!strcmpiW(month, jan)) return 1;
4222 if (!strcmpiW(month, feb)) return 2;
4223 if (!strcmpiW(month, mar)) return 3;
4224 if (!strcmpiW(month, apr)) return 4;
4225 if (!strcmpiW(month, may)) return 5;
4226 if (!strcmpiW(month, jun)) return 6;
4227 if (!strcmpiW(month, jul)) return 7;
4228 if (!strcmpiW(month, aug)) return 8;
4229 if (!strcmpiW(month, sep)) return 9;
4230 if (!strcmpiW(month, oct)) return 10;
4231 if (!strcmpiW(month, nov)) return 11;
4232 if (!strcmpiW(month, dec)) return 12;
4233 /* Invalid */
4234 return 0;
4237 /* Parses the string pointed to by *str, assumed to be a 24-hour time HH:MM:SS,
4238 * optionally preceded by whitespace.
4239 * Upon success, returns TRUE, sets the wHour, wMinute, and wSecond fields of
4240 * st, and sets *str to the first character after the time format.
4242 static BOOL HTTP_ParseTime(SYSTEMTIME *st, LPCWSTR *str)
4244 LPCWSTR ptr = *str;
4245 WCHAR *nextPtr;
4246 unsigned long num;
4248 while (isspaceW(*ptr))
4249 ptr++;
4251 num = strtoulW(ptr, &nextPtr, 10);
4252 if (!nextPtr || nextPtr <= ptr || *nextPtr != ':')
4254 ERR("unexpected time format %s\n", debugstr_w(ptr));
4255 return FALSE;
4257 if (num > 23)
4259 ERR("unexpected hour in time format %s\n", debugstr_w(ptr));
4260 return FALSE;
4262 ptr = nextPtr + 1;
4263 st->wHour = (WORD)num;
4264 num = strtoulW(ptr, &nextPtr, 10);
4265 if (!nextPtr || nextPtr <= ptr || *nextPtr != ':')
4267 ERR("unexpected time format %s\n", debugstr_w(ptr));
4268 return FALSE;
4270 if (num > 59)
4272 ERR("unexpected minute in time format %s\n", debugstr_w(ptr));
4273 return FALSE;
4275 ptr = nextPtr + 1;
4276 st->wMinute = (WORD)num;
4277 num = strtoulW(ptr, &nextPtr, 10);
4278 if (!nextPtr || nextPtr <= ptr)
4280 ERR("unexpected time format %s\n", debugstr_w(ptr));
4281 return FALSE;
4283 if (num > 59)
4285 ERR("unexpected second in time format %s\n", debugstr_w(ptr));
4286 return FALSE;
4288 ptr = nextPtr + 1;
4289 *str = ptr;
4290 st->wSecond = (WORD)num;
4291 return TRUE;
4294 static BOOL HTTP_ParseDateAsAsctime(LPCWSTR value, FILETIME *ft)
4296 static const WCHAR gmt[]= { 'G','M','T',0 };
4297 WCHAR day[4], *dayPtr, month[4], *monthPtr, *nextPtr;
4298 LPCWSTR ptr;
4299 SYSTEMTIME st = { 0 };
4300 unsigned long num;
4302 for (ptr = value, dayPtr = day; *ptr && !isspaceW(*ptr) &&
4303 dayPtr - day < sizeof(day) / sizeof(day[0]) - 1; ptr++, dayPtr++)
4304 *dayPtr = *ptr;
4305 *dayPtr = 0;
4306 st.wDayOfWeek = HTTP_ParseWkday(day);
4307 if (st.wDayOfWeek >= 7)
4309 ERR("unexpected weekday %s\n", debugstr_w(day));
4310 return FALSE;
4313 while (isspaceW(*ptr))
4314 ptr++;
4316 for (monthPtr = month; !isspaceW(*ptr) &&
4317 monthPtr - month < sizeof(month) / sizeof(month[0]) - 1;
4318 monthPtr++, ptr++)
4319 *monthPtr = *ptr;
4320 *monthPtr = 0;
4321 st.wMonth = HTTP_ParseMonth(month);
4322 if (!st.wMonth || st.wMonth > 12)
4324 ERR("unexpected month %s\n", debugstr_w(month));
4325 return FALSE;
4328 while (isspaceW(*ptr))
4329 ptr++;
4331 num = strtoulW(ptr, &nextPtr, 10);
4332 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4334 ERR("unexpected day %s\n", debugstr_w(ptr));
4335 return FALSE;
4337 ptr = nextPtr;
4338 st.wDay = (WORD)num;
4340 while (isspaceW(*ptr))
4341 ptr++;
4343 if (!HTTP_ParseTime(&st, &ptr))
4344 return FALSE;
4346 while (isspaceW(*ptr))
4347 ptr++;
4349 num = strtoulW(ptr, &nextPtr, 10);
4350 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4352 ERR("unexpected year %s\n", debugstr_w(ptr));
4353 return FALSE;
4355 ptr = nextPtr;
4356 st.wYear = (WORD)num;
4358 while (isspaceW(*ptr))
4359 ptr++;
4361 /* asctime() doesn't report a timezone, but some web servers do, so accept
4362 * with or without GMT.
4364 if (*ptr && strcmpW(ptr, gmt))
4366 ERR("unexpected timezone %s\n", debugstr_w(ptr));
4367 return FALSE;
4369 return SystemTimeToFileTime(&st, ft);
4372 static BOOL HTTP_ParseRfc1123Date(LPCWSTR value, FILETIME *ft)
4374 static const WCHAR gmt[]= { 'G','M','T',0 };
4375 WCHAR *nextPtr, day[4], month[4], *monthPtr;
4376 LPCWSTR ptr;
4377 unsigned long num;
4378 SYSTEMTIME st = { 0 };
4380 ptr = strchrW(value, ',');
4381 if (!ptr)
4382 return FALSE;
4383 if (ptr - value != 3)
4385 WARN("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4386 return FALSE;
4388 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4389 day[3] = 0;
4390 st.wDayOfWeek = HTTP_ParseWkday(day);
4391 if (st.wDayOfWeek > 6)
4393 WARN("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4394 return FALSE;
4396 ptr++;
4398 while (isspaceW(*ptr))
4399 ptr++;
4401 num = strtoulW(ptr, &nextPtr, 10);
4402 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4404 WARN("unexpected day %s\n", debugstr_w(value));
4405 return FALSE;
4407 ptr = nextPtr;
4408 st.wDay = (WORD)num;
4410 while (isspaceW(*ptr))
4411 ptr++;
4413 for (monthPtr = month; !isspaceW(*ptr) &&
4414 monthPtr - month < sizeof(month) / sizeof(month[0]) - 1;
4415 monthPtr++, ptr++)
4416 *monthPtr = *ptr;
4417 *monthPtr = 0;
4418 st.wMonth = HTTP_ParseMonth(month);
4419 if (!st.wMonth || st.wMonth > 12)
4421 WARN("unexpected month %s\n", debugstr_w(month));
4422 return FALSE;
4425 while (isspaceW(*ptr))
4426 ptr++;
4428 num = strtoulW(ptr, &nextPtr, 10);
4429 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4431 ERR("unexpected year %s\n", debugstr_w(value));
4432 return FALSE;
4434 ptr = nextPtr;
4435 st.wYear = (WORD)num;
4437 if (!HTTP_ParseTime(&st, &ptr))
4438 return FALSE;
4440 while (isspaceW(*ptr))
4441 ptr++;
4443 if (strcmpW(ptr, gmt))
4445 ERR("unexpected time zone %s\n", debugstr_w(ptr));
4446 return FALSE;
4448 return SystemTimeToFileTime(&st, ft);
4451 static WORD HTTP_ParseWeekday(LPCWSTR day)
4453 static const WCHAR days[7][10] = {{ 's','u','n','d','a','y',0 },
4454 { 'm','o','n','d','a','y',0 },
4455 { 't','u','e','s','d','a','y',0 },
4456 { 'w','e','d','n','e','s','d','a','y',0 },
4457 { 't','h','u','r','s','d','a','y',0 },
4458 { 'f','r','i','d','a','y',0 },
4459 { 's','a','t','u','r','d','a','y',0 }};
4460 unsigned int i;
4461 for (i = 0; i < sizeof(days)/sizeof(*days); i++)
4462 if (!strcmpiW(day, days[i]))
4463 return i;
4465 /* Invalid */
4466 return 7;
4469 static BOOL HTTP_ParseRfc850Date(LPCWSTR value, FILETIME *ft)
4471 static const WCHAR gmt[]= { 'G','M','T',0 };
4472 WCHAR *nextPtr, day[10], month[4], *monthPtr;
4473 LPCWSTR ptr;
4474 unsigned long num;
4475 SYSTEMTIME st = { 0 };
4477 ptr = strchrW(value, ',');
4478 if (!ptr)
4479 return FALSE;
4480 if (ptr - value == 3)
4482 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4483 day[3] = 0;
4484 st.wDayOfWeek = HTTP_ParseWkday(day);
4485 if (st.wDayOfWeek > 6)
4487 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4488 return FALSE;
4491 else if (ptr - value < sizeof(day) / sizeof(day[0]))
4493 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4494 day[ptr - value + 1] = 0;
4495 st.wDayOfWeek = HTTP_ParseWeekday(day);
4496 if (st.wDayOfWeek > 6)
4498 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4499 return FALSE;
4502 else
4504 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4505 return FALSE;
4507 ptr++;
4509 while (isspaceW(*ptr))
4510 ptr++;
4512 num = strtoulW(ptr, &nextPtr, 10);
4513 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4515 ERR("unexpected day %s\n", debugstr_w(value));
4516 return FALSE;
4518 ptr = nextPtr;
4519 st.wDay = (WORD)num;
4521 if (*ptr != '-')
4523 ERR("unexpected month format %s\n", debugstr_w(ptr));
4524 return FALSE;
4526 ptr++;
4528 for (monthPtr = month; *ptr != '-' &&
4529 monthPtr - month < sizeof(month) / sizeof(month[0]) - 1;
4530 monthPtr++, ptr++)
4531 *monthPtr = *ptr;
4532 *monthPtr = 0;
4533 st.wMonth = HTTP_ParseMonth(month);
4534 if (!st.wMonth || st.wMonth > 12)
4536 ERR("unexpected month %s\n", debugstr_w(month));
4537 return FALSE;
4540 if (*ptr != '-')
4542 ERR("unexpected year format %s\n", debugstr_w(ptr));
4543 return FALSE;
4545 ptr++;
4547 num = strtoulW(ptr, &nextPtr, 10);
4548 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4550 ERR("unexpected year %s\n", debugstr_w(value));
4551 return FALSE;
4553 ptr = nextPtr;
4554 st.wYear = (WORD)num;
4556 if (!HTTP_ParseTime(&st, &ptr))
4557 return FALSE;
4559 while (isspaceW(*ptr))
4560 ptr++;
4562 if (strcmpW(ptr, gmt))
4564 ERR("unexpected time zone %s\n", debugstr_w(ptr));
4565 return FALSE;
4567 return SystemTimeToFileTime(&st, ft);
4570 static BOOL HTTP_ParseDate(LPCWSTR value, FILETIME *ft)
4572 static const WCHAR zero[] = { '0',0 };
4573 BOOL ret;
4575 if (!strcmpW(value, zero))
4577 ft->dwLowDateTime = ft->dwHighDateTime = 0;
4578 ret = TRUE;
4580 else if (strchrW(value, ','))
4582 ret = HTTP_ParseRfc1123Date(value, ft);
4583 if (!ret)
4585 ret = HTTP_ParseRfc850Date(value, ft);
4586 if (!ret)
4587 ERR("unexpected date format %s\n", debugstr_w(value));
4590 else
4592 ret = HTTP_ParseDateAsAsctime(value, ft);
4593 if (!ret)
4594 ERR("unexpected date format %s\n", debugstr_w(value));
4596 return ret;
4599 static void HTTP_ProcessExpires(http_request_t *request)
4601 BOOL expirationFound = FALSE;
4602 int headerIndex;
4604 EnterCriticalSection( &request->headers_section );
4606 /* Look for a Cache-Control header with a max-age directive, as it takes
4607 * precedence over the Expires header.
4609 headerIndex = HTTP_GetCustomHeaderIndex(request, szCache_Control, 0, FALSE);
4610 if (headerIndex != -1)
4612 LPHTTPHEADERW ccHeader = &request->custHeaders[headerIndex];
4613 LPWSTR ptr;
4615 for (ptr = ccHeader->lpszValue; ptr && *ptr; )
4617 LPWSTR comma = strchrW(ptr, ','), end, equal;
4619 if (comma)
4620 end = comma;
4621 else
4622 end = ptr + strlenW(ptr);
4623 for (equal = end - 1; equal > ptr && *equal != '='; equal--)
4625 if (*equal == '=')
4627 static const WCHAR max_age[] = {
4628 'm','a','x','-','a','g','e',0 };
4630 if (!strncmpiW(ptr, max_age, equal - ptr - 1))
4632 LPWSTR nextPtr;
4633 unsigned long age;
4635 age = strtoulW(equal + 1, &nextPtr, 10);
4636 if (nextPtr > equal + 1)
4638 LARGE_INTEGER ft;
4640 NtQuerySystemTime( &ft );
4641 /* Age is in seconds, FILETIME resolution is in
4642 * 100 nanosecond intervals.
4644 ft.QuadPart += age * (ULONGLONG)1000000;
4645 request->expires.dwLowDateTime = ft.u.LowPart;
4646 request->expires.dwHighDateTime = ft.u.HighPart;
4647 expirationFound = TRUE;
4651 if (comma)
4653 ptr = comma + 1;
4654 while (isspaceW(*ptr))
4655 ptr++;
4657 else
4658 ptr = NULL;
4661 if (!expirationFound)
4663 headerIndex = HTTP_GetCustomHeaderIndex(request, szExpires, 0, FALSE);
4664 if (headerIndex != -1)
4666 LPHTTPHEADERW expiresHeader = &request->custHeaders[headerIndex];
4667 FILETIME ft;
4669 if (HTTP_ParseDate(expiresHeader->lpszValue, &ft))
4671 expirationFound = TRUE;
4672 request->expires = ft;
4676 if (!expirationFound)
4678 LARGE_INTEGER t;
4680 /* With no known age, default to 10 minutes until expiration. */
4681 NtQuerySystemTime( &t );
4682 t.QuadPart += 10 * 60 * (ULONGLONG)10000000;
4683 request->expires.dwLowDateTime = t.u.LowPart;
4684 request->expires.dwHighDateTime = t.u.HighPart;
4687 LeaveCriticalSection( &request->headers_section );
4690 static void HTTP_ProcessLastModified(http_request_t *request)
4692 int headerIndex;
4694 EnterCriticalSection( &request->headers_section );
4696 headerIndex = HTTP_GetCustomHeaderIndex(request, szLast_Modified, 0, FALSE);
4697 if (headerIndex != -1)
4699 LPHTTPHEADERW expiresHeader = &request->custHeaders[headerIndex];
4700 FILETIME ft;
4702 if (HTTP_ParseDate(expiresHeader->lpszValue, &ft))
4703 request->last_modified = ft;
4706 LeaveCriticalSection( &request->headers_section );
4709 static void http_process_keep_alive(http_request_t *req)
4711 int index;
4713 EnterCriticalSection( &req->headers_section );
4715 if ((index = HTTP_GetCustomHeaderIndex(req, szConnection, 0, FALSE)) != -1)
4716 req->netconn->keep_alive = !strcmpiW(req->custHeaders[index].lpszValue, szKeepAlive);
4717 else if ((index = HTTP_GetCustomHeaderIndex(req, szProxy_Connection, 0, FALSE)) != -1)
4718 req->netconn->keep_alive = !strcmpiW(req->custHeaders[index].lpszValue, szKeepAlive);
4719 else
4720 req->netconn->keep_alive = !strcmpiW(req->version, g_szHttp1_1);
4722 LeaveCriticalSection( &req->headers_section );
4725 static DWORD open_http_connection(http_request_t *request, BOOL *reusing)
4727 const BOOL is_https = (request->hdr.dwFlags & INTERNET_FLAG_SECURE) != 0;
4728 netconn_t *netconn = NULL;
4729 DWORD res;
4731 reset_data_stream(request);
4733 if (request->netconn)
4735 if (is_valid_netconn(request->netconn) && NETCON_is_alive(request->netconn))
4737 *reusing = TRUE;
4738 return ERROR_SUCCESS;
4740 else
4742 free_netconn(request->netconn);
4743 request->netconn = NULL;
4747 res = HTTP_ResolveName(request);
4748 if(res != ERROR_SUCCESS)
4749 return res;
4751 EnterCriticalSection(&connection_pool_cs);
4753 while(!list_empty(&request->server->conn_pool)) {
4754 netconn = LIST_ENTRY(list_head(&request->server->conn_pool), netconn_t, pool_entry);
4755 list_remove(&netconn->pool_entry);
4757 if(is_valid_netconn(netconn) && NETCON_is_alive(netconn))
4758 break;
4760 TRACE("connection %p closed during idle\n", netconn);
4761 free_netconn(netconn);
4762 netconn = NULL;
4765 LeaveCriticalSection(&connection_pool_cs);
4767 if(netconn) {
4768 TRACE("<-- reusing %p netconn\n", netconn);
4769 request->netconn = netconn;
4770 *reusing = TRUE;
4771 return ERROR_SUCCESS;
4774 TRACE("connecting to %s, proxy %s\n", debugstr_w(request->server->name),
4775 request->proxy ? debugstr_w(request->proxy->name) : "(null)");
4777 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
4778 INTERNET_STATUS_CONNECTING_TO_SERVER,
4779 request->server->addr_str,
4780 strlen(request->server->addr_str)+1);
4782 res = create_netconn(is_https, request->proxy ? request->proxy : request->server, request->security_flags,
4783 (request->hdr.ErrorMask & INTERNET_ERROR_MASK_COMBINED_SEC_CERT) != 0,
4784 request->connect_timeout, &netconn);
4785 if(res != ERROR_SUCCESS) {
4786 ERR("create_netconn failed: %u\n", res);
4787 return res;
4790 request->netconn = netconn;
4792 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
4793 INTERNET_STATUS_CONNECTED_TO_SERVER,
4794 request->server->addr_str, strlen(request->server->addr_str)+1);
4796 *reusing = FALSE;
4797 TRACE("Created connection to %s: %p\n", debugstr_w(request->server->name), netconn);
4798 return ERROR_SUCCESS;
4801 static char *build_ascii_request( const WCHAR *str, void *data, DWORD data_len, DWORD *out_len )
4803 int len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
4804 char *ret;
4806 if (!(ret = heap_alloc( len + data_len ))) return NULL;
4807 WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
4808 if (data_len) memcpy( ret + len - 1, data, data_len );
4809 *out_len = len + data_len - 1;
4810 ret[*out_len] = 0;
4811 return ret;
4814 static void set_content_length_header( http_request_t *request, DWORD len, DWORD flags )
4816 static const WCHAR fmtW[] =
4817 {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','u','\r','\n',0};
4818 WCHAR buf[sizeof(fmtW)/sizeof(fmtW[0]) + 10];
4820 sprintfW( buf, fmtW, len );
4821 HTTP_HttpAddRequestHeadersW( request, buf, ~0u, flags );
4824 /***********************************************************************
4825 * HTTP_HttpSendRequestW (internal)
4827 * Sends the specified request to the HTTP server
4829 * RETURNS
4830 * ERROR_SUCCESS on success
4831 * win32 error code on failure
4834 static DWORD HTTP_HttpSendRequestW(http_request_t *request, LPCWSTR lpszHeaders,
4835 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
4836 DWORD dwContentLength, BOOL bEndRequest)
4838 BOOL redirected = FALSE, secure_proxy_connect = FALSE, loop_next;
4839 LPWSTR requestString = NULL;
4840 INT responseLen, cnt;
4841 DWORD res;
4843 TRACE("--> %p\n", request);
4845 assert(request->hdr.htype == WH_HHTTPREQ);
4847 /* if the verb is NULL default to GET */
4848 if (!request->verb)
4849 request->verb = heap_strdupW(szGET);
4851 HTTP_ProcessHeader(request, hostW, request->server->canon_host_port,
4852 HTTP_ADDREQ_FLAG_ADD_IF_NEW | HTTP_ADDHDR_FLAG_REQ);
4854 if (dwContentLength || strcmpW(request->verb, szGET))
4856 set_content_length_header(request, dwContentLength, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4857 request->bytesToWrite = dwContentLength;
4859 if (request->session->appInfo->agent)
4861 WCHAR *agent_header;
4862 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0};
4863 int len;
4865 len = strlenW(request->session->appInfo->agent) + strlenW(user_agent);
4866 agent_header = heap_alloc(len * sizeof(WCHAR));
4867 sprintfW(agent_header, user_agent, request->session->appInfo->agent);
4869 HTTP_HttpAddRequestHeadersW(request, agent_header, strlenW(agent_header), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4870 heap_free(agent_header);
4872 if (request->hdr.dwFlags & INTERNET_FLAG_PRAGMA_NOCACHE)
4874 static const WCHAR pragma_nocache[] = {'P','r','a','g','m','a',':',' ','n','o','-','c','a','c','h','e','\r','\n',0};
4875 HTTP_HttpAddRequestHeadersW(request, pragma_nocache, strlenW(pragma_nocache), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4877 if ((request->hdr.dwFlags & INTERNET_FLAG_NO_CACHE_WRITE) && strcmpW(request->verb, szGET))
4879 static const WCHAR cache_control[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l',':',
4880 ' ','n','o','-','c','a','c','h','e','\r','\n',0};
4881 HTTP_HttpAddRequestHeadersW(request, cache_control, strlenW(cache_control), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4884 /* add the headers the caller supplied */
4885 if( lpszHeaders && dwHeaderLength )
4886 HTTP_HttpAddRequestHeadersW(request, lpszHeaders, dwHeaderLength, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
4890 DWORD len, data_len = dwOptionalLength;
4891 BOOL reusing_connection;
4892 char *ascii_req;
4894 loop_next = FALSE;
4896 if(redirected) {
4897 request->contentLength = ~0u;
4898 request->bytesToWrite = 0;
4901 if (TRACE_ON(wininet))
4903 HTTPHEADERW *host;
4905 EnterCriticalSection( &request->headers_section );
4906 host = HTTP_GetHeader( request, hostW );
4907 TRACE("Going to url %s %s\n", debugstr_w(host->lpszValue), debugstr_w(request->path));
4908 LeaveCriticalSection( &request->headers_section );
4911 HTTP_FixURL(request);
4912 if (request->hdr.dwFlags & INTERNET_FLAG_KEEP_CONNECTION)
4914 HTTP_ProcessHeader(request, szConnection, szKeepAlive,
4915 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_ADD);
4917 HTTP_InsertAuthorization(request, request->authInfo, szAuthorization);
4918 HTTP_InsertAuthorization(request, request->proxyAuthInfo, szProxy_Authorization);
4920 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES))
4921 HTTP_InsertCookies(request);
4923 res = open_http_connection(request, &reusing_connection);
4924 if (res != ERROR_SUCCESS)
4925 break;
4927 if (!reusing_connection && (request->hdr.dwFlags & INTERNET_FLAG_SECURE))
4929 if (request->proxy) secure_proxy_connect = TRUE;
4930 else
4932 res = NETCON_secure_connect(request->netconn, request->server);
4933 if (res != ERROR_SUCCESS)
4935 WARN("failed to upgrade to secure connection\n");
4936 http_release_netconn(request, FALSE);
4937 break;
4941 if (secure_proxy_connect)
4943 static const WCHAR connectW[] = {'C','O','N','N','E','C','T',0};
4944 const WCHAR *target = request->server->host_port;
4946 if (HTTP_GetCustomHeaderIndex(request, szContent_Length, 0, TRUE) >= 0)
4947 set_content_length_header(request, 0, HTTP_ADDREQ_FLAG_REPLACE);
4949 requestString = build_request_header(request, connectW, target, g_szHttp1_1, TRUE);
4951 else if (request->proxy && !(request->hdr.dwFlags & INTERNET_FLAG_SECURE))
4953 WCHAR *url = build_proxy_path_url(request);
4954 requestString = build_request_header(request, request->verb, url, request->version, TRUE);
4955 heap_free(url);
4957 else
4959 if (request->proxy && HTTP_GetCustomHeaderIndex(request, szContent_Length, 0, TRUE) >= 0)
4960 set_content_length_header(request, dwContentLength, HTTP_ADDREQ_FLAG_REPLACE);
4962 requestString = build_request_header(request, request->verb, request->path, request->version, TRUE);
4965 TRACE("Request header -> %s\n", debugstr_w(requestString) );
4967 /* send the request as ASCII, tack on the optional data */
4968 if (!lpOptional || redirected || secure_proxy_connect)
4969 data_len = 0;
4971 ascii_req = build_ascii_request( requestString, lpOptional, data_len, &len );
4972 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
4974 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
4975 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
4977 NETCON_set_timeout( request->netconn, TRUE, request->send_timeout );
4978 res = NETCON_send(request->netconn, ascii_req, len, 0, &cnt);
4979 heap_free( ascii_req );
4980 if(res != ERROR_SUCCESS) {
4981 TRACE("send failed: %u\n", res);
4982 if(!reusing_connection)
4983 break;
4984 http_release_netconn(request, FALSE);
4985 loop_next = TRUE;
4986 continue;
4989 request->bytesWritten = data_len;
4991 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
4992 INTERNET_STATUS_REQUEST_SENT,
4993 &len, sizeof(DWORD));
4995 if (bEndRequest)
4997 DWORD dwBufferSize;
4999 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5000 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
5002 if (HTTP_GetResponseHeaders(request, &responseLen))
5004 http_release_netconn(request, FALSE);
5005 res = ERROR_INTERNET_CONNECTION_ABORTED;
5006 goto lend;
5008 /* FIXME: We should know that connection is closed before sending
5009 * headers. Otherwise wrong callbacks are executed */
5010 if(!responseLen && reusing_connection) {
5011 TRACE("Connection closed by server, reconnecting\n");
5012 http_release_netconn(request, FALSE);
5013 loop_next = TRUE;
5014 continue;
5017 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5018 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
5019 sizeof(DWORD));
5021 http_process_keep_alive(request);
5022 HTTP_ProcessCookies(request);
5023 HTTP_ProcessExpires(request);
5024 HTTP_ProcessLastModified(request);
5026 res = set_content_length(request);
5027 if(res != ERROR_SUCCESS)
5028 goto lend;
5029 if(!request->contentLength)
5030 http_release_netconn(request, TRUE);
5032 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && responseLen)
5034 WCHAR *new_url;
5036 switch(request->status_code) {
5037 case HTTP_STATUS_REDIRECT:
5038 case HTTP_STATUS_MOVED:
5039 case HTTP_STATUS_REDIRECT_KEEP_VERB:
5040 case HTTP_STATUS_REDIRECT_METHOD:
5041 new_url = get_redirect_url(request);
5042 if(!new_url)
5043 break;
5045 if (strcmpW(request->verb, szGET) && strcmpW(request->verb, szHEAD) &&
5046 request->status_code != HTTP_STATUS_REDIRECT_KEEP_VERB)
5048 heap_free(request->verb);
5049 request->verb = heap_strdupW(szGET);
5051 http_release_netconn(request, drain_content(request, FALSE));
5052 res = HTTP_HandleRedirect(request, new_url);
5053 heap_free(new_url);
5054 if (res == ERROR_SUCCESS) {
5055 heap_free(requestString);
5056 loop_next = TRUE;
5058 redirected = TRUE;
5061 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTH) && res == ERROR_SUCCESS)
5063 WCHAR szAuthValue[2048];
5064 dwBufferSize=2048;
5065 if (request->status_code == HTTP_STATUS_DENIED)
5067 WCHAR *host = heap_strdupW( request->server->canon_host_port );
5068 DWORD dwIndex = 0;
5069 while (HTTP_HttpQueryInfoW(request,HTTP_QUERY_WWW_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex) == ERROR_SUCCESS)
5071 if (HTTP_DoAuthorization(request, szAuthValue,
5072 &request->authInfo,
5073 request->session->userName,
5074 request->session->password, host))
5076 heap_free(requestString);
5077 if(!drain_content(request, TRUE)) {
5078 FIXME("Could not drain content\n");
5079 http_release_netconn(request, FALSE);
5081 loop_next = TRUE;
5082 break;
5085 heap_free( host );
5087 if(!loop_next) {
5088 TRACE("Cleaning wrong authorization data\n");
5089 destroy_authinfo(request->authInfo);
5090 request->authInfo = NULL;
5093 if (request->status_code == HTTP_STATUS_PROXY_AUTH_REQ)
5095 DWORD dwIndex = 0;
5096 while (HTTP_HttpQueryInfoW(request,HTTP_QUERY_PROXY_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex) == ERROR_SUCCESS)
5098 if (HTTP_DoAuthorization(request, szAuthValue,
5099 &request->proxyAuthInfo,
5100 request->session->appInfo->proxyUsername,
5101 request->session->appInfo->proxyPassword,
5102 NULL))
5104 heap_free(requestString);
5105 if(!drain_content(request, TRUE)) {
5106 FIXME("Could not drain content\n");
5107 http_release_netconn(request, FALSE);
5109 loop_next = TRUE;
5110 break;
5114 if(!loop_next) {
5115 TRACE("Cleaning wrong proxy authorization data\n");
5116 destroy_authinfo(request->proxyAuthInfo);
5117 request->proxyAuthInfo = NULL;
5121 if (secure_proxy_connect && request->status_code == HTTP_STATUS_OK)
5123 res = NETCON_secure_connect(request->netconn, request->server);
5124 if (res != ERROR_SUCCESS)
5126 WARN("failed to upgrade to secure proxy connection\n");
5127 http_release_netconn( request, FALSE );
5128 break;
5130 remove_header(request, szProxy_Authorization, TRUE);
5131 destroy_authinfo(request->proxyAuthInfo);
5132 request->proxyAuthInfo = NULL;
5134 secure_proxy_connect = FALSE;
5135 loop_next = TRUE;
5138 else
5139 res = ERROR_SUCCESS;
5141 while (loop_next);
5143 lend:
5144 heap_free(requestString);
5146 /* TODO: send notification for P3P header */
5148 if(res == ERROR_SUCCESS)
5149 create_cache_entry(request);
5151 if (request->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5153 if (res == ERROR_SUCCESS) {
5154 if(bEndRequest && request->contentLength && request->bytesWritten == request->bytesToWrite)
5155 HTTP_ReceiveRequestData(request);
5156 else
5157 send_request_complete(request,
5158 request->session->hdr.dwInternalFlags & INET_OPENURL ? (DWORD_PTR)request->hdr.hInternet : 1, 0);
5159 }else {
5160 send_request_complete(request, 0, res);
5164 TRACE("<--\n");
5165 return res;
5168 typedef struct {
5169 task_header_t hdr;
5170 WCHAR *headers;
5171 DWORD headers_len;
5172 void *optional;
5173 DWORD optional_len;
5174 DWORD content_len;
5175 BOOL end_request;
5176 } send_request_task_t;
5178 /***********************************************************************
5180 * Helper functions for the HttpSendRequest(Ex) functions
5183 static void AsyncHttpSendRequestProc(task_header_t *hdr)
5185 send_request_task_t *task = (send_request_task_t*)hdr;
5186 http_request_t *request = (http_request_t*)task->hdr.hdr;
5188 TRACE("%p\n", request);
5190 HTTP_HttpSendRequestW(request, task->headers, task->headers_len, task->optional,
5191 task->optional_len, task->content_len, task->end_request);
5193 heap_free(task->headers);
5197 static DWORD HTTP_HttpEndRequestW(http_request_t *request, DWORD dwFlags, DWORD_PTR dwContext)
5199 INT responseLen;
5200 DWORD res = ERROR_SUCCESS;
5202 if(!is_valid_netconn(request->netconn)) {
5203 WARN("Not connected\n");
5204 send_request_complete(request, 0, ERROR_INTERNET_OPERATION_CANCELLED);
5205 return ERROR_INTERNET_OPERATION_CANCELLED;
5208 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5209 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
5211 if (HTTP_GetResponseHeaders(request, &responseLen) || !responseLen)
5212 res = ERROR_HTTP_HEADER_NOT_FOUND;
5214 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5215 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
5217 /* process cookies here. Is this right? */
5218 http_process_keep_alive(request);
5219 HTTP_ProcessCookies(request);
5220 HTTP_ProcessExpires(request);
5221 HTTP_ProcessLastModified(request);
5223 if ((res = set_content_length(request)) == ERROR_SUCCESS) {
5224 if(!request->contentLength)
5225 http_release_netconn(request, TRUE);
5228 if (res == ERROR_SUCCESS && !(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
5230 switch(request->status_code) {
5231 case HTTP_STATUS_REDIRECT:
5232 case HTTP_STATUS_MOVED:
5233 case HTTP_STATUS_REDIRECT_METHOD:
5234 case HTTP_STATUS_REDIRECT_KEEP_VERB: {
5235 WCHAR *new_url;
5237 new_url = get_redirect_url(request);
5238 if(!new_url)
5239 break;
5241 if (strcmpW(request->verb, szGET) && strcmpW(request->verb, szHEAD) &&
5242 request->status_code != HTTP_STATUS_REDIRECT_KEEP_VERB)
5244 heap_free(request->verb);
5245 request->verb = heap_strdupW(szGET);
5247 http_release_netconn(request, drain_content(request, FALSE));
5248 res = HTTP_HandleRedirect(request, new_url);
5249 heap_free(new_url);
5250 if (res == ERROR_SUCCESS)
5251 res = HTTP_HttpSendRequestW(request, NULL, 0, NULL, 0, 0, TRUE);
5256 if(res == ERROR_SUCCESS)
5257 create_cache_entry(request);
5259 if (res == ERROR_SUCCESS && request->contentLength)
5260 HTTP_ReceiveRequestData(request);
5261 else
5262 send_request_complete(request, res == ERROR_SUCCESS, res);
5264 return res;
5267 /***********************************************************************
5268 * HttpEndRequestA (WININET.@)
5270 * Ends an HTTP request that was started by HttpSendRequestEx
5272 * RETURNS
5273 * TRUE if successful
5274 * FALSE on failure
5277 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
5278 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
5280 TRACE("(%p, %p, %08x, %08lx)\n", hRequest, lpBuffersOut, dwFlags, dwContext);
5282 if (lpBuffersOut)
5284 SetLastError(ERROR_INVALID_PARAMETER);
5285 return FALSE;
5288 return HttpEndRequestW(hRequest, NULL, dwFlags, dwContext);
5291 typedef struct {
5292 task_header_t hdr;
5293 DWORD flags;
5294 DWORD context;
5295 } end_request_task_t;
5297 static void AsyncHttpEndRequestProc(task_header_t *hdr)
5299 end_request_task_t *task = (end_request_task_t*)hdr;
5300 http_request_t *req = (http_request_t*)task->hdr.hdr;
5302 TRACE("%p\n", req);
5304 HTTP_HttpEndRequestW(req, task->flags, task->context);
5307 /***********************************************************************
5308 * HttpEndRequestW (WININET.@)
5310 * Ends an HTTP request that was started by HttpSendRequestEx
5312 * RETURNS
5313 * TRUE if successful
5314 * FALSE on failure
5317 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
5318 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
5320 http_request_t *request;
5321 DWORD res;
5323 TRACE("%p %p %x %lx -->\n", hRequest, lpBuffersOut, dwFlags, dwContext);
5325 if (lpBuffersOut)
5327 SetLastError(ERROR_INVALID_PARAMETER);
5328 return FALSE;
5331 request = (http_request_t*) get_handle_object( hRequest );
5333 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5335 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
5336 if (request)
5337 WININET_Release( &request->hdr );
5338 return FALSE;
5340 request->hdr.dwFlags |= dwFlags;
5342 if (request->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5344 end_request_task_t *task;
5346 task = alloc_async_task(&request->hdr, AsyncHttpEndRequestProc, sizeof(*task));
5347 task->flags = dwFlags;
5348 task->context = dwContext;
5350 INTERNET_AsyncCall(&task->hdr);
5351 res = ERROR_IO_PENDING;
5353 else
5354 res = HTTP_HttpEndRequestW(request, dwFlags, dwContext);
5356 WININET_Release( &request->hdr );
5357 TRACE("%u <--\n", res);
5358 if(res != ERROR_SUCCESS)
5359 SetLastError(res);
5360 return res == ERROR_SUCCESS;
5363 /***********************************************************************
5364 * HttpSendRequestExA (WININET.@)
5366 * Sends the specified request to the HTTP server and allows chunked
5367 * transfers.
5369 * RETURNS
5370 * Success: TRUE
5371 * Failure: FALSE, call GetLastError() for more information.
5373 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
5374 LPINTERNET_BUFFERSA lpBuffersIn,
5375 LPINTERNET_BUFFERSA lpBuffersOut,
5376 DWORD dwFlags, DWORD_PTR dwContext)
5378 INTERNET_BUFFERSW BuffersInW;
5379 BOOL rc = FALSE;
5380 DWORD headerlen;
5381 LPWSTR header = NULL;
5383 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
5384 lpBuffersOut, dwFlags, dwContext);
5386 if (lpBuffersIn)
5388 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
5389 if (lpBuffersIn->lpcszHeader)
5391 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
5392 lpBuffersIn->dwHeadersLength,0,0);
5393 header = heap_alloc(headerlen*sizeof(WCHAR));
5394 if (!(BuffersInW.lpcszHeader = header))
5396 SetLastError(ERROR_OUTOFMEMORY);
5397 return FALSE;
5399 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
5400 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
5401 header, headerlen);
5403 else
5404 BuffersInW.lpcszHeader = NULL;
5405 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
5406 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
5407 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
5408 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
5409 BuffersInW.Next = NULL;
5412 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
5414 heap_free(header);
5415 return rc;
5418 /***********************************************************************
5419 * HttpSendRequestExW (WININET.@)
5421 * Sends the specified request to the HTTP server and allows chunked
5422 * transfers
5424 * RETURNS
5425 * Success: TRUE
5426 * Failure: FALSE, call GetLastError() for more information.
5428 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
5429 LPINTERNET_BUFFERSW lpBuffersIn,
5430 LPINTERNET_BUFFERSW lpBuffersOut,
5431 DWORD dwFlags, DWORD_PTR dwContext)
5433 http_request_t *request;
5434 http_session_t *session;
5435 appinfo_t *hIC;
5436 DWORD res;
5438 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
5439 lpBuffersOut, dwFlags, dwContext);
5441 request = (http_request_t*) get_handle_object( hRequest );
5443 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5445 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5446 goto lend;
5449 session = request->session;
5450 assert(session->hdr.htype == WH_HHTTPSESSION);
5451 hIC = session->appInfo;
5452 assert(hIC->hdr.htype == WH_HINIT);
5454 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5456 send_request_task_t *task;
5458 task = alloc_async_task(&request->hdr, AsyncHttpSendRequestProc, sizeof(*task));
5459 if (lpBuffersIn)
5461 DWORD size = 0;
5463 if (lpBuffersIn->lpcszHeader)
5465 if (lpBuffersIn->dwHeadersLength == ~0u)
5466 size = (strlenW( lpBuffersIn->lpcszHeader ) + 1) * sizeof(WCHAR);
5467 else
5468 size = lpBuffersIn->dwHeadersLength * sizeof(WCHAR);
5470 task->headers = heap_alloc(size);
5471 memcpy(task->headers, lpBuffersIn->lpcszHeader, size);
5473 else task->headers = NULL;
5475 task->headers_len = size / sizeof(WCHAR);
5476 task->optional = lpBuffersIn->lpvBuffer;
5477 task->optional_len = lpBuffersIn->dwBufferLength;
5478 task->content_len = lpBuffersIn->dwBufferTotal;
5480 else
5482 task->headers = NULL;
5483 task->headers_len = 0;
5484 task->optional = NULL;
5485 task->optional_len = 0;
5486 task->content_len = 0;
5489 task->end_request = FALSE;
5491 INTERNET_AsyncCall(&task->hdr);
5492 res = ERROR_IO_PENDING;
5494 else
5496 if (lpBuffersIn)
5497 res = HTTP_HttpSendRequestW(request, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
5498 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
5499 lpBuffersIn->dwBufferTotal, FALSE);
5500 else
5501 res = HTTP_HttpSendRequestW(request, NULL, 0, NULL, 0, 0, FALSE);
5504 lend:
5505 if ( request )
5506 WININET_Release( &request->hdr );
5508 TRACE("<---\n");
5509 SetLastError(res);
5510 return res == ERROR_SUCCESS;
5513 /***********************************************************************
5514 * HttpSendRequestW (WININET.@)
5516 * Sends the specified request to the HTTP server
5518 * RETURNS
5519 * TRUE on success
5520 * FALSE on failure
5523 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
5524 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
5526 http_request_t *request;
5527 http_session_t *session = NULL;
5528 appinfo_t *hIC = NULL;
5529 DWORD res = ERROR_SUCCESS;
5531 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest,
5532 debugstr_wn(lpszHeaders, dwHeaderLength), dwHeaderLength, lpOptional, dwOptionalLength);
5534 request = (http_request_t*) get_handle_object( hHttpRequest );
5535 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5537 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5538 goto lend;
5541 session = request->session;
5542 if (NULL == session || session->hdr.htype != WH_HHTTPSESSION)
5544 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5545 goto lend;
5548 hIC = session->appInfo;
5549 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
5551 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5552 goto lend;
5555 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5557 send_request_task_t *task;
5559 task = alloc_async_task(&request->hdr, AsyncHttpSendRequestProc, sizeof(*task));
5560 if (lpszHeaders)
5562 DWORD size;
5564 if (dwHeaderLength == ~0u) size = (strlenW(lpszHeaders) + 1) * sizeof(WCHAR);
5565 else size = dwHeaderLength * sizeof(WCHAR);
5567 task->headers = heap_alloc(size);
5568 memcpy(task->headers, lpszHeaders, size);
5570 else
5571 task->headers = NULL;
5572 task->headers_len = dwHeaderLength;
5573 task->optional = lpOptional;
5574 task->optional_len = dwOptionalLength;
5575 task->content_len = dwOptionalLength;
5576 task->end_request = TRUE;
5578 INTERNET_AsyncCall(&task->hdr);
5579 res = ERROR_IO_PENDING;
5581 else
5583 res = HTTP_HttpSendRequestW(request, lpszHeaders,
5584 dwHeaderLength, lpOptional, dwOptionalLength,
5585 dwOptionalLength, TRUE);
5587 lend:
5588 if( request )
5589 WININET_Release( &request->hdr );
5591 SetLastError(res);
5592 return res == ERROR_SUCCESS;
5595 /***********************************************************************
5596 * HttpSendRequestA (WININET.@)
5598 * Sends the specified request to the HTTP server
5600 * RETURNS
5601 * TRUE on success
5602 * FALSE on failure
5605 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
5606 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
5608 BOOL result;
5609 LPWSTR szHeaders=NULL;
5610 DWORD nLen=dwHeaderLength;
5611 if(lpszHeaders!=NULL)
5613 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
5614 szHeaders = heap_alloc(nLen*sizeof(WCHAR));
5615 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
5617 result = HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
5618 heap_free(szHeaders);
5619 return result;
5622 /***********************************************************************
5623 * HTTPSESSION_Destroy (internal)
5625 * Deallocate session handle
5628 static void HTTPSESSION_Destroy(object_header_t *hdr)
5630 http_session_t *session = (http_session_t*) hdr;
5632 TRACE("%p\n", session);
5634 WININET_Release(&session->appInfo->hdr);
5636 heap_free(session->hostName);
5637 heap_free(session->password);
5638 heap_free(session->userName);
5641 static DWORD HTTPSESSION_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
5643 http_session_t *ses = (http_session_t *)hdr;
5645 switch(option) {
5646 case INTERNET_OPTION_HANDLE_TYPE:
5647 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
5649 if (*size < sizeof(ULONG))
5650 return ERROR_INSUFFICIENT_BUFFER;
5652 *size = sizeof(DWORD);
5653 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_CONNECT_HTTP;
5654 return ERROR_SUCCESS;
5655 case INTERNET_OPTION_CONNECT_TIMEOUT:
5656 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
5658 if (*size < sizeof(DWORD))
5659 return ERROR_INSUFFICIENT_BUFFER;
5661 *size = sizeof(DWORD);
5662 *(DWORD *)buffer = ses->connect_timeout;
5663 return ERROR_SUCCESS;
5665 case INTERNET_OPTION_SEND_TIMEOUT:
5666 TRACE("INTERNET_OPTION_SEND_TIMEOUT\n");
5668 if (*size < sizeof(DWORD))
5669 return ERROR_INSUFFICIENT_BUFFER;
5671 *size = sizeof(DWORD);
5672 *(DWORD *)buffer = ses->send_timeout;
5673 return ERROR_SUCCESS;
5675 case INTERNET_OPTION_RECEIVE_TIMEOUT:
5676 TRACE("INTERNET_OPTION_RECEIVE_TIMEOUT\n");
5678 if (*size < sizeof(DWORD))
5679 return ERROR_INSUFFICIENT_BUFFER;
5681 *size = sizeof(DWORD);
5682 *(DWORD *)buffer = ses->receive_timeout;
5683 return ERROR_SUCCESS;
5686 return INET_QueryOption(hdr, option, buffer, size, unicode);
5689 static DWORD HTTPSESSION_SetOption(object_header_t *hdr, DWORD option, void *buffer, DWORD size)
5691 http_session_t *ses = (http_session_t*)hdr;
5693 switch(option) {
5694 case INTERNET_OPTION_USERNAME:
5696 heap_free(ses->userName);
5697 if (!(ses->userName = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5698 return ERROR_SUCCESS;
5700 case INTERNET_OPTION_PASSWORD:
5702 heap_free(ses->password);
5703 if (!(ses->password = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5704 return ERROR_SUCCESS;
5706 case INTERNET_OPTION_PROXY_USERNAME:
5708 heap_free(ses->appInfo->proxyUsername);
5709 if (!(ses->appInfo->proxyUsername = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5710 return ERROR_SUCCESS;
5712 case INTERNET_OPTION_PROXY_PASSWORD:
5714 heap_free(ses->appInfo->proxyPassword);
5715 if (!(ses->appInfo->proxyPassword = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5716 return ERROR_SUCCESS;
5718 case INTERNET_OPTION_CONNECT_TIMEOUT:
5720 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5721 ses->connect_timeout = *(DWORD *)buffer;
5722 return ERROR_SUCCESS;
5724 case INTERNET_OPTION_SEND_TIMEOUT:
5726 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5727 ses->send_timeout = *(DWORD *)buffer;
5728 return ERROR_SUCCESS;
5730 case INTERNET_OPTION_RECEIVE_TIMEOUT:
5732 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5733 ses->receive_timeout = *(DWORD *)buffer;
5734 return ERROR_SUCCESS;
5736 default: break;
5739 return INET_SetOption(hdr, option, buffer, size);
5742 static const object_vtbl_t HTTPSESSIONVtbl = {
5743 HTTPSESSION_Destroy,
5744 NULL,
5745 HTTPSESSION_QueryOption,
5746 HTTPSESSION_SetOption,
5747 NULL,
5748 NULL,
5749 NULL,
5750 NULL
5754 /***********************************************************************
5755 * HTTP_Connect (internal)
5757 * Create http session handle
5759 * RETURNS
5760 * HINTERNET a session handle on success
5761 * NULL on failure
5764 DWORD HTTP_Connect(appinfo_t *hIC, LPCWSTR lpszServerName,
5765 INTERNET_PORT serverPort, LPCWSTR lpszUserName,
5766 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
5767 DWORD dwInternalFlags, HINTERNET *ret)
5769 http_session_t *session = NULL;
5771 TRACE("-->\n");
5773 if (!lpszServerName || !lpszServerName[0])
5774 return ERROR_INVALID_PARAMETER;
5776 assert( hIC->hdr.htype == WH_HINIT );
5778 session = alloc_object(&hIC->hdr, &HTTPSESSIONVtbl, sizeof(http_session_t));
5779 if (!session)
5780 return ERROR_OUTOFMEMORY;
5783 * According to my tests. The name is not resolved until a request is sent
5786 session->hdr.htype = WH_HHTTPSESSION;
5787 session->hdr.dwFlags = dwFlags;
5788 session->hdr.dwContext = dwContext;
5789 session->hdr.dwInternalFlags |= dwInternalFlags;
5791 WININET_AddRef( &hIC->hdr );
5792 session->appInfo = hIC;
5793 list_add_head( &hIC->hdr.children, &session->hdr.entry );
5795 session->hostName = heap_strdupW(lpszServerName);
5796 if (lpszUserName && lpszUserName[0])
5797 session->userName = heap_strdupW(lpszUserName);
5798 if (lpszPassword && lpszPassword[0])
5799 session->password = heap_strdupW(lpszPassword);
5800 session->hostPort = serverPort;
5801 session->connect_timeout = hIC->connect_timeout;
5802 session->send_timeout = 0;
5803 session->receive_timeout = 0;
5805 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
5806 if (!(session->hdr.dwInternalFlags & INET_OPENURL))
5808 INTERNET_SendCallback(&hIC->hdr, dwContext,
5809 INTERNET_STATUS_HANDLE_CREATED, &session->hdr.hInternet,
5810 sizeof(HINTERNET));
5814 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
5815 * windows
5818 TRACE("%p --> %p\n", hIC, session);
5820 *ret = session->hdr.hInternet;
5821 return ERROR_SUCCESS;
5824 /***********************************************************************
5825 * HTTP_clear_response_headers (internal)
5827 * clear out any old response headers
5829 static void HTTP_clear_response_headers( http_request_t *request )
5831 DWORD i;
5833 EnterCriticalSection( &request->headers_section );
5835 for( i=0; i<request->nCustHeaders; i++)
5837 if( !request->custHeaders[i].lpszField )
5838 continue;
5839 if( !request->custHeaders[i].lpszValue )
5840 continue;
5841 if ( request->custHeaders[i].wFlags & HDR_ISREQUEST )
5842 continue;
5843 HTTP_DeleteCustomHeader( request, i );
5844 i--;
5847 LeaveCriticalSection( &request->headers_section );
5850 /***********************************************************************
5851 * HTTP_GetResponseHeaders (internal)
5853 * Read server response
5855 * RETURNS
5857 * TRUE on success
5858 * FALSE on error
5860 static DWORD HTTP_GetResponseHeaders(http_request_t *request, INT *len)
5862 INT cbreaks = 0;
5863 WCHAR buffer[MAX_REPLY_LEN];
5864 DWORD buflen = MAX_REPLY_LEN;
5865 INT rc = 0;
5866 char bufferA[MAX_REPLY_LEN];
5867 LPWSTR status_code = NULL, status_text = NULL;
5868 DWORD res = ERROR_HTTP_INVALID_SERVER_RESPONSE;
5869 BOOL codeHundred = FALSE;
5871 TRACE("-->\n");
5873 if(!is_valid_netconn(request->netconn))
5874 goto lend;
5876 /* clear old response headers (eg. from a redirect response) */
5877 HTTP_clear_response_headers( request );
5879 NETCON_set_timeout( request->netconn, FALSE, request->receive_timeout );
5880 do {
5882 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
5884 buflen = MAX_REPLY_LEN;
5885 if ((res = read_line(request, bufferA, &buflen)))
5886 goto lend;
5888 if (!buflen) goto lend;
5890 rc += buflen;
5891 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
5892 /* check is this a status code line? */
5893 if (!strncmpW(buffer, g_szHttp1_0, 4))
5895 /* split the version from the status code */
5896 status_code = strchrW( buffer, ' ' );
5897 if( !status_code )
5898 goto lend;
5899 *status_code++=0;
5901 /* split the status code from the status text */
5902 status_text = strchrW( status_code, ' ' );
5903 if( status_text )
5904 *status_text++=0;
5906 request->status_code = atoiW(status_code);
5908 TRACE("version [%s] status code [%s] status text [%s]\n",
5909 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
5911 codeHundred = request->status_code == HTTP_STATUS_CONTINUE;
5913 else if (!codeHundred)
5915 WARN("No status line at head of response (%s)\n", debugstr_w(buffer));
5917 heap_free(request->version);
5918 heap_free(request->statusText);
5920 request->status_code = HTTP_STATUS_OK;
5921 request->version = heap_strdupW(g_szHttp1_0);
5922 request->statusText = heap_strdupW(szOK);
5924 goto lend;
5926 } while (codeHundred);
5928 /* Add status code */
5929 HTTP_ProcessHeader(request, szStatus, status_code,
5930 HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_ADD);
5932 heap_free(request->version);
5933 heap_free(request->statusText);
5935 request->version = heap_strdupW(buffer);
5936 request->statusText = heap_strdupW(status_text ? status_text : emptyW);
5938 /* Restore the spaces */
5939 *(status_code-1) = ' ';
5940 if (status_text)
5941 *(status_text-1) = ' ';
5943 /* Parse each response line */
5946 buflen = MAX_REPLY_LEN;
5947 if (!read_line(request, bufferA, &buflen) && buflen)
5949 LPWSTR * pFieldAndValue;
5951 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
5953 if (!bufferA[0]) break;
5954 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
5956 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
5957 if (pFieldAndValue)
5959 HTTP_ProcessHeader(request, pFieldAndValue[0], pFieldAndValue[1],
5960 HTTP_ADDREQ_FLAG_ADD );
5961 HTTP_FreeTokens(pFieldAndValue);
5964 else
5966 cbreaks++;
5967 if (cbreaks >= 2)
5968 break;
5970 }while(1);
5972 res = ERROR_SUCCESS;
5974 lend:
5976 *len = rc;
5977 TRACE("<--\n");
5978 return res;
5981 /***********************************************************************
5982 * HTTP_InterpretHttpHeader (internal)
5984 * Parse server response
5986 * RETURNS
5988 * Pointer to array of field, value, NULL on success.
5989 * NULL on error.
5991 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
5993 LPWSTR * pTokenPair;
5994 LPWSTR pszColon;
5995 INT len;
5997 pTokenPair = heap_alloc_zero(sizeof(*pTokenPair)*3);
5999 pszColon = strchrW(buffer, ':');
6000 /* must have two tokens */
6001 if (!pszColon)
6003 HTTP_FreeTokens(pTokenPair);
6004 if (buffer[0])
6005 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
6006 return NULL;
6009 pTokenPair[0] = heap_alloc((pszColon - buffer + 1) * sizeof(WCHAR));
6010 if (!pTokenPair[0])
6012 HTTP_FreeTokens(pTokenPair);
6013 return NULL;
6015 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
6016 pTokenPair[0][pszColon - buffer] = '\0';
6018 /* skip colon */
6019 pszColon++;
6020 len = strlenW(pszColon);
6021 pTokenPair[1] = heap_alloc((len + 1) * sizeof(WCHAR));
6022 if (!pTokenPair[1])
6024 HTTP_FreeTokens(pTokenPair);
6025 return NULL;
6027 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
6029 strip_spaces(pTokenPair[0]);
6030 strip_spaces(pTokenPair[1]);
6032 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
6033 return pTokenPair;
6036 /***********************************************************************
6037 * HTTP_ProcessHeader (internal)
6039 * Stuff header into header tables according to <dwModifier>
6043 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
6045 static DWORD HTTP_ProcessHeader(http_request_t *request, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
6047 LPHTTPHEADERW lphttpHdr = NULL;
6048 INT index;
6049 BOOL request_only = !!(dwModifier & HTTP_ADDHDR_FLAG_REQ);
6050 DWORD res = ERROR_HTTP_INVALID_HEADER;
6052 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), dwModifier);
6054 EnterCriticalSection( &request->headers_section );
6056 /* REPLACE wins out over ADD */
6057 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
6058 dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
6060 if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
6061 index = -1;
6062 else
6063 index = HTTP_GetCustomHeaderIndex(request, field, 0, request_only);
6065 if (index >= 0)
6067 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
6069 LeaveCriticalSection( &request->headers_section );
6070 return ERROR_HTTP_INVALID_HEADER;
6072 lphttpHdr = &request->custHeaders[index];
6074 else if (value)
6076 HTTPHEADERW hdr;
6078 hdr.lpszField = (LPWSTR)field;
6079 hdr.lpszValue = (LPWSTR)value;
6080 hdr.wFlags = hdr.wCount = 0;
6082 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6083 hdr.wFlags |= HDR_ISREQUEST;
6085 res = HTTP_InsertCustomHeader(request, &hdr);
6086 LeaveCriticalSection( &request->headers_section );
6087 return res;
6089 /* no value to delete */
6090 else
6092 LeaveCriticalSection( &request->headers_section );
6093 return ERROR_SUCCESS;
6096 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6097 lphttpHdr->wFlags |= HDR_ISREQUEST;
6098 else
6099 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
6101 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
6103 HTTP_DeleteCustomHeader( request, index );
6105 if (value && value[0])
6107 HTTPHEADERW hdr;
6109 hdr.lpszField = (LPWSTR)field;
6110 hdr.lpszValue = (LPWSTR)value;
6111 hdr.wFlags = hdr.wCount = 0;
6113 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6114 hdr.wFlags |= HDR_ISREQUEST;
6116 res = HTTP_InsertCustomHeader(request, &hdr);
6117 LeaveCriticalSection( &request->headers_section );
6118 return res;
6121 LeaveCriticalSection( &request->headers_section );
6122 return ERROR_SUCCESS;
6124 else if (dwModifier & COALESCEFLAGS)
6126 LPWSTR lpsztmp;
6127 WCHAR ch = 0;
6128 INT len = 0;
6129 INT origlen = strlenW(lphttpHdr->lpszValue);
6130 INT valuelen = strlenW(value);
6132 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
6134 ch = ',';
6135 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
6137 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
6139 ch = ';';
6140 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
6143 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
6145 lpsztmp = heap_realloc(lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
6146 if (lpsztmp)
6148 lphttpHdr->lpszValue = lpsztmp;
6149 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
6150 if (ch > 0)
6152 lphttpHdr->lpszValue[origlen] = ch;
6153 origlen++;
6154 lphttpHdr->lpszValue[origlen] = ' ';
6155 origlen++;
6158 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
6159 lphttpHdr->lpszValue[len] = '\0';
6160 res = ERROR_SUCCESS;
6162 else
6164 WARN("heap_realloc (%d bytes) failed\n",len+1);
6165 res = ERROR_OUTOFMEMORY;
6168 TRACE("<-- %d\n", res);
6169 LeaveCriticalSection( &request->headers_section );
6170 return res;
6173 /***********************************************************************
6174 * HTTP_GetCustomHeaderIndex (internal)
6176 * Return index of custom header from header array
6177 * Headers section must be held
6179 static INT HTTP_GetCustomHeaderIndex(http_request_t *request, LPCWSTR lpszField,
6180 int requested_index, BOOL request_only)
6182 DWORD index;
6184 TRACE("%s, %d, %d\n", debugstr_w(lpszField), requested_index, request_only);
6186 for (index = 0; index < request->nCustHeaders; index++)
6188 if (strcmpiW(request->custHeaders[index].lpszField, lpszField))
6189 continue;
6191 if (request_only && !(request->custHeaders[index].wFlags & HDR_ISREQUEST))
6192 continue;
6194 if (!request_only && (request->custHeaders[index].wFlags & HDR_ISREQUEST))
6195 continue;
6197 if (requested_index == 0)
6198 break;
6199 requested_index --;
6202 if (index >= request->nCustHeaders)
6203 index = -1;
6205 TRACE("Return: %d\n", index);
6206 return index;
6210 /***********************************************************************
6211 * HTTP_InsertCustomHeader (internal)
6213 * Insert header into array
6214 * Headers section must be held
6216 static DWORD HTTP_InsertCustomHeader(http_request_t *request, LPHTTPHEADERW lpHdr)
6218 INT count;
6219 LPHTTPHEADERW lph = NULL;
6221 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
6222 count = request->nCustHeaders + 1;
6223 if (count > 1)
6224 lph = heap_realloc_zero(request->custHeaders, sizeof(HTTPHEADERW) * count);
6225 else
6226 lph = heap_alloc_zero(sizeof(HTTPHEADERW) * count);
6228 if (!lph)
6229 return ERROR_OUTOFMEMORY;
6231 request->custHeaders = lph;
6232 request->custHeaders[count-1].lpszField = heap_strdupW(lpHdr->lpszField);
6233 request->custHeaders[count-1].lpszValue = heap_strdupW(lpHdr->lpszValue);
6234 request->custHeaders[count-1].wFlags = lpHdr->wFlags;
6235 request->custHeaders[count-1].wCount= lpHdr->wCount;
6236 request->nCustHeaders++;
6238 return ERROR_SUCCESS;
6242 /***********************************************************************
6243 * HTTP_DeleteCustomHeader (internal)
6245 * Delete header from array
6246 * If this function is called, the index may change.
6247 * Headers section must be held
6249 static BOOL HTTP_DeleteCustomHeader(http_request_t *request, DWORD index)
6251 if( request->nCustHeaders <= 0 )
6252 return FALSE;
6253 if( index >= request->nCustHeaders )
6254 return FALSE;
6255 request->nCustHeaders--;
6257 heap_free(request->custHeaders[index].lpszField);
6258 heap_free(request->custHeaders[index].lpszValue);
6260 memmove( &request->custHeaders[index], &request->custHeaders[index+1],
6261 (request->nCustHeaders - index)* sizeof(HTTPHEADERW) );
6262 memset( &request->custHeaders[request->nCustHeaders], 0, sizeof(HTTPHEADERW) );
6264 return TRUE;
6268 /***********************************************************************
6269 * IsHostInProxyBypassList (@)
6271 * Undocumented
6274 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
6276 FIXME("STUB: flags=%d host=%s length=%d\n",flags,szHost,length);
6277 return FALSE;