wininet: Canonicalize URL in HttpOpenRequest.
[wine.git] / dlls / wininet / http.c
blobb3c8e6659e3d21c64d144d53554eee7d1961f8d1
1 /*
2 * Wininet - HTTP Implementation
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 TransGaming Technologies Inc.
7 * Copyright 2004 Mike McCormack for CodeWeavers
8 * Copyright 2005 Aric Stewart for CodeWeavers
9 * Copyright 2006 Robert Shearman for CodeWeavers
10 * Copyright 2011 Jacek Caban for CodeWeavers
12 * Ulrich Czekalla
13 * David Hammerton
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "config.h"
32 #include <stdlib.h>
34 #ifdef HAVE_ZLIB
35 # include <zlib.h>
36 #endif
38 #include "winsock2.h"
39 #include "ws2ipdef.h"
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <time.h>
44 #include <assert.h>
46 #include "windef.h"
47 #include "winbase.h"
48 #include "wininet.h"
49 #include "winerror.h"
50 #include "winternl.h"
51 #define NO_SHLWAPI_STREAM
52 #define NO_SHLWAPI_REG
53 #define NO_SHLWAPI_STRFCNS
54 #define NO_SHLWAPI_GDI
55 #include "shlwapi.h"
56 #include "sspi.h"
57 #include "wincrypt.h"
58 #include "winuser.h"
60 #include "internet.h"
61 #include "wine/debug.h"
62 #include "wine/exception.h"
63 #include "wine/unicode.h"
65 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
67 static const WCHAR g_szHttp1_0[] = {'H','T','T','P','/','1','.','0',0};
68 static const WCHAR g_szHttp1_1[] = {'H','T','T','P','/','1','.','1',0};
69 static const WCHAR szOK[] = {'O','K',0};
70 static const WCHAR hostW[] = { 'H','o','s','t',0 };
71 static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
72 static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
73 static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 };
74 static const WCHAR szKeepAlive[] = {'K','e','e','p','-','A','l','i','v','e',0};
75 static const WCHAR szGET[] = { 'G','E','T', 0 };
76 static const WCHAR szHEAD[] = { 'H','E','A','D', 0 };
78 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
79 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
80 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
81 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
82 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
83 static const WCHAR szAge[] = { 'A','g','e',0 };
84 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
85 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
86 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
87 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
88 static const WCHAR szContent_Disposition[] = { 'C','o','n','t','e','n','t','-','D','i','s','p','o','s','i','t','i','o','n',0 };
89 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
90 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
91 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
92 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
93 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
94 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
95 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
96 static const WCHAR szContent_Transfer_Encoding[] = { 'C','o','n','t','e','n','t','-','T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
97 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
98 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
99 static const WCHAR szDate[] = { 'D','a','t','e',0 };
100 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
101 static const WCHAR szETag[] = { 'E','T','a','g',0 };
102 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
103 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
104 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
105 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
106 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
107 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
108 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
109 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
110 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
111 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
112 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
113 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
114 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
115 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
116 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
117 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
118 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
119 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
120 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
121 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
122 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
123 static const WCHAR szUnless_Modified_Since[] = { 'U','n','l','e','s','s','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
124 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
125 static const WCHAR szURI[] = { 'U','R','I',0 };
126 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
127 static const WCHAR szVary[] = { 'V','a','r','y',0 };
128 static const WCHAR szVia[] = { 'V','i','a',0 };
129 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
130 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
132 static const WCHAR emptyW[] = {0};
134 #define HTTP_REFERER szReferer
135 #define HTTP_ACCEPT szAccept
136 #define HTTP_USERAGENT szUser_Agent
138 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
139 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
140 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
141 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
142 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
143 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
144 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
146 #define COLLECT_TIME 60000
148 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
150 struct HttpAuthInfo
152 LPWSTR scheme;
153 CredHandle cred;
154 CtxtHandle ctx;
155 TimeStamp exp;
156 ULONG attr;
157 ULONG max_token;
158 void *auth_data;
159 unsigned int auth_data_len;
160 BOOL finished; /* finished authenticating */
164 typedef struct _basicAuthorizationData
166 struct list entry;
168 LPWSTR host;
169 LPWSTR realm;
170 LPSTR authorization;
171 UINT authorizationLen;
172 } basicAuthorizationData;
174 typedef struct _authorizationData
176 struct list entry;
178 LPWSTR host;
179 LPWSTR scheme;
180 LPWSTR domain;
181 UINT domain_len;
182 LPWSTR user;
183 UINT user_len;
184 LPWSTR password;
185 UINT password_len;
186 } authorizationData;
188 static struct list basicAuthorizationCache = LIST_INIT(basicAuthorizationCache);
189 static struct list authorizationCache = LIST_INIT(authorizationCache);
191 static CRITICAL_SECTION authcache_cs;
192 static CRITICAL_SECTION_DEBUG critsect_debug =
194 0, 0, &authcache_cs,
195 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
196 0, 0, { (DWORD_PTR)(__FILE__ ": authcache_cs") }
198 static CRITICAL_SECTION authcache_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
200 static DWORD HTTP_GetResponseHeaders(http_request_t *req, INT *len);
201 static DWORD HTTP_ProcessHeader(http_request_t *req, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
202 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
203 static DWORD HTTP_InsertCustomHeader(http_request_t *req, LPHTTPHEADERW lpHdr);
204 static INT HTTP_GetCustomHeaderIndex(http_request_t *req, LPCWSTR lpszField, INT index, BOOL Request);
205 static BOOL HTTP_DeleteCustomHeader(http_request_t *req, DWORD index);
206 static LPWSTR HTTP_build_req( LPCWSTR *list, int len );
207 static DWORD HTTP_HttpQueryInfoW(http_request_t*, DWORD, LPVOID, LPDWORD, LPDWORD);
208 static UINT HTTP_DecodeBase64(LPCWSTR base64, LPSTR bin);
209 static BOOL drain_content(http_request_t*,BOOL);
211 static CRITICAL_SECTION connection_pool_cs;
212 static CRITICAL_SECTION_DEBUG connection_pool_debug =
214 0, 0, &connection_pool_cs,
215 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
216 0, 0, { (DWORD_PTR)(__FILE__ ": connection_pool_cs") }
218 static CRITICAL_SECTION connection_pool_cs = { &connection_pool_debug, -1, 0, 0, 0, 0 };
220 static struct list connection_pool = LIST_INIT(connection_pool);
221 static BOOL collector_running;
223 void server_addref(server_t *server)
225 InterlockedIncrement(&server->ref);
228 void server_release(server_t *server)
230 if(InterlockedDecrement(&server->ref))
231 return;
233 list_remove(&server->entry);
235 if(server->cert_chain)
236 CertFreeCertificateChain(server->cert_chain);
237 heap_free(server->name);
238 heap_free(server->scheme_host_port);
239 heap_free(server);
242 static BOOL process_host_port(server_t *server)
244 BOOL default_port;
245 size_t name_len;
246 WCHAR *buf;
248 static const WCHAR httpW[] = {'h','t','t','p',0};
249 static const WCHAR httpsW[] = {'h','t','t','p','s',0};
250 static const WCHAR formatW[] = {'%','s',':','/','/','%','s',':','%','u',0};
252 name_len = strlenW(server->name);
253 buf = heap_alloc((name_len + 10 /* strlen("://:<port>") */)*sizeof(WCHAR) + sizeof(httpsW));
254 if(!buf)
255 return FALSE;
257 sprintfW(buf, formatW, server->is_https ? httpsW : httpW, server->name, server->port);
258 server->scheme_host_port = buf;
260 server->host_port = server->scheme_host_port + 7 /* strlen("http://") */;
261 if(server->is_https)
262 server->host_port++;
264 default_port = server->port == (server->is_https ? INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT);
265 server->canon_host_port = default_port ? server->name : server->host_port;
266 return TRUE;
269 server_t *get_server(substr_t name, INTERNET_PORT port, BOOL is_https, BOOL do_create)
271 server_t *iter, *server = NULL;
273 if(port == INTERNET_INVALID_PORT_NUMBER)
274 port = INTERNET_DEFAULT_HTTP_PORT;
276 EnterCriticalSection(&connection_pool_cs);
278 LIST_FOR_EACH_ENTRY(iter, &connection_pool, server_t, entry) {
279 if(iter->port == port && name.len == strlenW(iter->name) && !strncmpW(iter->name, name.str, name.len)
280 && iter->is_https == is_https) {
281 server = iter;
282 server_addref(server);
283 break;
287 if(!server && do_create) {
288 server = heap_alloc_zero(sizeof(*server));
289 if(server) {
290 server->ref = 2; /* list reference and return */
291 server->port = port;
292 server->is_https = is_https;
293 list_init(&server->conn_pool);
294 server->name = heap_strndupW(name.str, name.len);
295 if(server->name && process_host_port(server)) {
296 list_add_head(&connection_pool, &server->entry);
297 }else {
298 heap_free(server);
299 server = NULL;
304 LeaveCriticalSection(&connection_pool_cs);
306 return server;
309 BOOL collect_connections(collect_type_t collect_type)
311 netconn_t *netconn, *netconn_safe;
312 server_t *server, *server_safe;
313 BOOL remaining = FALSE;
314 DWORD64 now;
316 now = GetTickCount64();
318 LIST_FOR_EACH_ENTRY_SAFE(server, server_safe, &connection_pool, server_t, entry) {
319 LIST_FOR_EACH_ENTRY_SAFE(netconn, netconn_safe, &server->conn_pool, netconn_t, pool_entry) {
320 if(collect_type > COLLECT_TIMEOUT || netconn->keep_until < now) {
321 TRACE("freeing %p\n", netconn);
322 list_remove(&netconn->pool_entry);
323 free_netconn(netconn);
324 }else {
325 remaining = TRUE;
329 if(collect_type == COLLECT_CLEANUP) {
330 list_remove(&server->entry);
331 list_init(&server->entry);
332 server_release(server);
336 return remaining;
339 static DWORD WINAPI collect_connections_proc(void *arg)
341 BOOL remaining_conns;
343 do {
344 /* FIXME: Use more sophisticated method */
345 Sleep(5000);
347 EnterCriticalSection(&connection_pool_cs);
349 remaining_conns = collect_connections(COLLECT_TIMEOUT);
350 if(!remaining_conns)
351 collector_running = FALSE;
353 LeaveCriticalSection(&connection_pool_cs);
354 }while(remaining_conns);
356 FreeLibraryAndExitThread(WININET_hModule, 0);
359 /***********************************************************************
360 * HTTP_GetHeader (internal)
362 * Headers section must be held
364 static LPHTTPHEADERW HTTP_GetHeader(http_request_t *req, LPCWSTR head)
366 int HeaderIndex = 0;
367 HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
368 if (HeaderIndex == -1)
369 return NULL;
370 else
371 return &req->custHeaders[HeaderIndex];
374 static WCHAR *get_host_header( http_request_t *req )
376 HTTPHEADERW *header;
377 WCHAR *ret = NULL;
379 EnterCriticalSection( &req->headers_section );
380 if ((header = HTTP_GetHeader( req, hostW ))) ret = heap_strdupW( header->lpszValue );
381 else ret = heap_strdupW( req->server->canon_host_port );
382 LeaveCriticalSection( &req->headers_section );
383 return ret;
386 typedef enum {
387 BLOCKING_ALLOW,
388 BLOCKING_DISALLOW,
389 BLOCKING_WAITALL
390 } blocking_mode_t;
392 struct data_stream_vtbl_t {
393 DWORD (*get_avail_data)(data_stream_t*,http_request_t*);
394 BOOL (*end_of_data)(data_stream_t*,http_request_t*);
395 DWORD (*read)(data_stream_t*,http_request_t*,BYTE*,DWORD,DWORD*,blocking_mode_t);
396 BOOL (*drain_content)(data_stream_t*,http_request_t*);
397 void (*destroy)(data_stream_t*);
400 typedef struct {
401 data_stream_t data_stream;
403 BYTE buf[READ_BUFFER_SIZE];
404 DWORD buf_size;
405 DWORD buf_pos;
406 DWORD chunk_size;
408 enum {
409 CHUNKED_STREAM_STATE_READING_CHUNK_SIZE,
410 CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE,
411 CHUNKED_STREAM_STATE_READING_CHUNK,
412 CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA,
413 CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END,
414 CHUNKED_STREAM_STATE_END_OF_STREAM
415 } state;
416 } chunked_stream_t;
418 static inline void destroy_data_stream(data_stream_t *stream)
420 stream->vtbl->destroy(stream);
423 static void reset_data_stream(http_request_t *req)
425 destroy_data_stream(req->data_stream);
426 req->data_stream = &req->netconn_stream.data_stream;
427 req->read_pos = req->read_size = req->netconn_stream.content_read = 0;
428 req->read_gzip = FALSE;
431 static void remove_header( http_request_t *request, const WCHAR *str, BOOL from_request )
433 int index;
434 EnterCriticalSection( &request->headers_section );
435 index = HTTP_GetCustomHeaderIndex( request, str, 0, from_request );
436 if (index != -1) HTTP_DeleteCustomHeader( request, index );
437 LeaveCriticalSection( &request->headers_section );
440 #ifdef HAVE_ZLIB
442 typedef struct {
443 data_stream_t stream;
444 data_stream_t *parent_stream;
445 z_stream zstream;
446 BYTE buf[READ_BUFFER_SIZE];
447 DWORD buf_size;
448 DWORD buf_pos;
449 BOOL end_of_data;
450 } gzip_stream_t;
452 static DWORD gzip_get_avail_data(data_stream_t *stream, http_request_t *req)
454 /* Allow reading only from read buffer */
455 return 0;
458 static BOOL gzip_end_of_data(data_stream_t *stream, http_request_t *req)
460 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
461 return gzip_stream->end_of_data
462 || (!gzip_stream->buf_size && gzip_stream->parent_stream->vtbl->end_of_data(gzip_stream->parent_stream, req));
465 static DWORD gzip_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
466 DWORD *read, blocking_mode_t blocking_mode)
468 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
469 z_stream *zstream = &gzip_stream->zstream;
470 DWORD current_read, ret_read = 0;
471 int zres;
472 DWORD res = ERROR_SUCCESS;
474 TRACE("(%d %d)\n", size, blocking_mode);
476 while(size && !gzip_stream->end_of_data) {
477 if(!gzip_stream->buf_size) {
478 if(gzip_stream->buf_pos) {
479 if(gzip_stream->buf_size)
480 memmove(gzip_stream->buf, gzip_stream->buf+gzip_stream->buf_pos, gzip_stream->buf_size);
481 gzip_stream->buf_pos = 0;
483 res = gzip_stream->parent_stream->vtbl->read(gzip_stream->parent_stream, req, gzip_stream->buf+gzip_stream->buf_size,
484 sizeof(gzip_stream->buf)-gzip_stream->buf_size, &current_read, blocking_mode);
485 gzip_stream->buf_size += current_read;
486 if(res != ERROR_SUCCESS)
487 break;
489 if(!current_read) {
490 if(blocking_mode != BLOCKING_DISALLOW) {
491 WARN("unexpected end of data\n");
492 gzip_stream->end_of_data = TRUE;
494 break;
498 zstream->next_in = gzip_stream->buf+gzip_stream->buf_pos;
499 zstream->avail_in = gzip_stream->buf_size;
500 zstream->next_out = buf+ret_read;
501 zstream->avail_out = size;
502 zres = inflate(&gzip_stream->zstream, 0);
503 current_read = size - zstream->avail_out;
504 size -= current_read;
505 ret_read += current_read;
506 gzip_stream->buf_size -= zstream->next_in - (gzip_stream->buf+gzip_stream->buf_pos);
507 gzip_stream->buf_pos = zstream->next_in-gzip_stream->buf;
508 if(zres == Z_STREAM_END) {
509 TRACE("end of data\n");
510 gzip_stream->end_of_data = TRUE;
511 inflateEnd(zstream);
512 }else if(zres != Z_OK) {
513 WARN("inflate failed %d: %s\n", zres, debugstr_a(zstream->msg));
514 if(!ret_read)
515 res = ERROR_INTERNET_DECODING_FAILED;
516 break;
519 if(ret_read && blocking_mode == BLOCKING_ALLOW)
520 blocking_mode = BLOCKING_DISALLOW;
523 TRACE("read %u bytes\n", ret_read);
524 *read = ret_read;
525 return res;
528 static BOOL gzip_drain_content(data_stream_t *stream, http_request_t *req)
530 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
531 return gzip_stream->parent_stream->vtbl->drain_content(gzip_stream->parent_stream, req);
534 static void gzip_destroy(data_stream_t *stream)
536 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
538 destroy_data_stream(gzip_stream->parent_stream);
540 if(!gzip_stream->end_of_data)
541 inflateEnd(&gzip_stream->zstream);
542 heap_free(gzip_stream);
545 static const data_stream_vtbl_t gzip_stream_vtbl = {
546 gzip_get_avail_data,
547 gzip_end_of_data,
548 gzip_read,
549 gzip_drain_content,
550 gzip_destroy
553 static voidpf wininet_zalloc(voidpf opaque, uInt items, uInt size)
555 return heap_alloc(items*size);
558 static void wininet_zfree(voidpf opaque, voidpf address)
560 heap_free(address);
563 static DWORD init_gzip_stream(http_request_t *req, BOOL is_gzip)
565 gzip_stream_t *gzip_stream;
566 int zres;
568 gzip_stream = heap_alloc_zero(sizeof(gzip_stream_t));
569 if(!gzip_stream)
570 return ERROR_OUTOFMEMORY;
572 gzip_stream->stream.vtbl = &gzip_stream_vtbl;
573 gzip_stream->zstream.zalloc = wininet_zalloc;
574 gzip_stream->zstream.zfree = wininet_zfree;
576 zres = inflateInit2(&gzip_stream->zstream, is_gzip ? 0x1f : -15);
577 if(zres != Z_OK) {
578 ERR("inflateInit failed: %d\n", zres);
579 heap_free(gzip_stream);
580 return ERROR_OUTOFMEMORY;
583 remove_header(req, szContent_Length, FALSE);
585 if(req->read_size) {
586 memcpy(gzip_stream->buf, req->read_buf+req->read_pos, req->read_size);
587 gzip_stream->buf_size = req->read_size;
588 req->read_pos = req->read_size = 0;
591 req->read_gzip = TRUE;
592 gzip_stream->parent_stream = req->data_stream;
593 req->data_stream = &gzip_stream->stream;
594 return ERROR_SUCCESS;
597 #else
599 static DWORD init_gzip_stream(http_request_t *req, BOOL is_gzip)
601 ERR("gzip stream not supported, missing zlib.\n");
602 return ERROR_SUCCESS;
605 #endif
607 /***********************************************************************
608 * HTTP_FreeTokens (internal)
610 * Frees table of pointers.
612 static void HTTP_FreeTokens(LPWSTR * token_array)
614 int i;
615 for (i = 0; token_array[i]; i++) heap_free(token_array[i]);
616 heap_free(token_array);
619 static void HTTP_FixURL(http_request_t *request)
621 static const WCHAR szSlash[] = { '/',0 };
622 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
624 /* If we don't have a path we set it to root */
625 if (NULL == request->path)
626 request->path = heap_strdupW(szSlash);
627 else /* remove \r and \n*/
629 int nLen = strlenW(request->path);
630 while ((nLen >0 ) && ((request->path[nLen-1] == '\r')||(request->path[nLen-1] == '\n')))
632 nLen--;
633 request->path[nLen]='\0';
635 /* Replace '\' with '/' */
636 while (nLen>0) {
637 nLen--;
638 if (request->path[nLen] == '\\') request->path[nLen]='/';
642 if(CSTR_EQUAL != CompareStringW( LOCALE_INVARIANT, NORM_IGNORECASE,
643 request->path, strlenW(request->path), szHttp, strlenW(szHttp) )
644 && request->path[0] != '/') /* not an absolute path ?? --> fix it !! */
646 WCHAR *fixurl = heap_alloc((strlenW(request->path) + 2)*sizeof(WCHAR));
647 *fixurl = '/';
648 strcpyW(fixurl + 1, request->path);
649 heap_free( request->path );
650 request->path = fixurl;
654 static WCHAR* build_request_header(http_request_t *request, const WCHAR *verb,
655 const WCHAR *path, const WCHAR *version, BOOL use_cr)
657 static const WCHAR szSpace[] = {' ',0};
658 static const WCHAR szColon[] = {':',' ',0};
659 static const WCHAR szCr[] = {'\r',0};
660 static const WCHAR szLf[] = {'\n',0};
661 LPWSTR requestString;
662 DWORD len, n;
663 LPCWSTR *req;
664 UINT i;
666 EnterCriticalSection( &request->headers_section );
668 /* allocate space for an array of all the string pointers to be added */
669 len = request->nCustHeaders * 5 + 10;
670 if (!(req = heap_alloc( len * sizeof(const WCHAR *) )))
672 LeaveCriticalSection( &request->headers_section );
673 return NULL;
676 /* add the verb, path and HTTP version string */
677 n = 0;
678 req[n++] = verb;
679 req[n++] = szSpace;
680 req[n++] = path;
681 req[n++] = szSpace;
682 req[n++] = version;
683 if (use_cr)
684 req[n++] = szCr;
685 req[n++] = szLf;
687 /* Append custom request headers */
688 for (i = 0; i < request->nCustHeaders; i++)
690 if (request->custHeaders[i].wFlags & HDR_ISREQUEST)
692 req[n++] = request->custHeaders[i].lpszField;
693 req[n++] = szColon;
694 req[n++] = request->custHeaders[i].lpszValue;
695 if (use_cr)
696 req[n++] = szCr;
697 req[n++] = szLf;
699 TRACE("Adding custom header %s (%s)\n",
700 debugstr_w(request->custHeaders[i].lpszField),
701 debugstr_w(request->custHeaders[i].lpszValue));
704 if (use_cr)
705 req[n++] = szCr;
706 req[n++] = szLf;
707 req[n] = NULL;
709 requestString = HTTP_build_req( req, 4 );
710 heap_free( req );
711 LeaveCriticalSection( &request->headers_section );
712 return requestString;
715 static WCHAR* build_response_header(http_request_t *request, BOOL use_cr)
717 static const WCHAR colonW[] = { ':',' ',0 };
718 static const WCHAR crW[] = { '\r',0 };
719 static const WCHAR lfW[] = { '\n',0 };
720 static const WCHAR status_fmt[] = { ' ','%','u',' ',0 };
721 const WCHAR **req;
722 WCHAR *ret, buf[14];
723 DWORD i, n = 0;
725 EnterCriticalSection( &request->headers_section );
727 if (!(req = heap_alloc( (request->nCustHeaders * 5 + 8) * sizeof(WCHAR *) )))
729 LeaveCriticalSection( &request->headers_section );
730 return NULL;
733 if (request->status_code)
735 req[n++] = request->version;
736 sprintfW(buf, status_fmt, request->status_code);
737 req[n++] = buf;
738 req[n++] = request->statusText;
739 if (use_cr)
740 req[n++] = crW;
741 req[n++] = lfW;
744 for(i = 0; i < request->nCustHeaders; i++)
746 if(!(request->custHeaders[i].wFlags & HDR_ISREQUEST)
747 && strcmpW(request->custHeaders[i].lpszField, szStatus))
749 req[n++] = request->custHeaders[i].lpszField;
750 req[n++] = colonW;
751 req[n++] = request->custHeaders[i].lpszValue;
752 if(use_cr)
753 req[n++] = crW;
754 req[n++] = lfW;
756 TRACE("Adding custom header %s (%s)\n",
757 debugstr_w(request->custHeaders[i].lpszField),
758 debugstr_w(request->custHeaders[i].lpszValue));
761 if(use_cr)
762 req[n++] = crW;
763 req[n++] = lfW;
764 req[n] = NULL;
766 ret = HTTP_build_req(req, 0);
767 heap_free(req);
768 LeaveCriticalSection( &request->headers_section );
769 return ret;
772 static void HTTP_ProcessCookies( http_request_t *request )
774 int HeaderIndex;
775 int numCookies = 0;
776 LPHTTPHEADERW setCookieHeader;
778 if(request->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES)
779 return;
781 EnterCriticalSection( &request->headers_section );
783 while((HeaderIndex = HTTP_GetCustomHeaderIndex(request, szSet_Cookie, numCookies++, FALSE)) != -1)
785 const WCHAR *data;
786 substr_t name;
788 setCookieHeader = &request->custHeaders[HeaderIndex];
790 if (!setCookieHeader->lpszValue)
791 continue;
793 data = strchrW(setCookieHeader->lpszValue, '=');
794 if(!data)
795 continue;
797 name = substr(setCookieHeader->lpszValue, data - setCookieHeader->lpszValue);
798 data++;
799 set_cookie(substrz(request->server->name), substrz(request->path), name, substrz(data), INTERNET_COOKIE_HTTPONLY);
802 LeaveCriticalSection( &request->headers_section );
805 static void strip_spaces(LPWSTR start)
807 LPWSTR str = start;
808 LPWSTR end;
810 while (*str == ' ')
811 str++;
813 if (str != start)
814 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
816 end = start + strlenW(start) - 1;
817 while (end >= start && *end == ' ')
819 *end = '\0';
820 end--;
824 static inline BOOL is_basic_auth_value( LPCWSTR pszAuthValue, LPWSTR *pszRealm )
826 static const WCHAR szBasic[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
827 static const WCHAR szRealm[] = {'r','e','a','l','m'}; /* Note: not nul-terminated */
828 BOOL is_basic;
829 is_basic = !strncmpiW(pszAuthValue, szBasic, ARRAYSIZE(szBasic)) &&
830 ((pszAuthValue[ARRAYSIZE(szBasic)] == ' ') || !pszAuthValue[ARRAYSIZE(szBasic)]);
831 if (is_basic && pszRealm)
833 LPCWSTR token;
834 LPCWSTR ptr = &pszAuthValue[ARRAYSIZE(szBasic)];
835 LPCWSTR realm;
836 ptr++;
837 *pszRealm=NULL;
838 token = strchrW(ptr,'=');
839 if (!token)
840 return TRUE;
841 realm = ptr;
842 while (*realm == ' ')
843 realm++;
844 if(!strncmpiW(realm, szRealm, ARRAYSIZE(szRealm)) &&
845 (realm[ARRAYSIZE(szRealm)] == ' ' || realm[ARRAYSIZE(szRealm)] == '='))
847 token++;
848 while (*token == ' ')
849 token++;
850 if (*token == '\0')
851 return TRUE;
852 *pszRealm = heap_strdupW(token);
853 strip_spaces(*pszRealm);
857 return is_basic;
860 static void destroy_authinfo( struct HttpAuthInfo *authinfo )
862 if (!authinfo) return;
864 if (SecIsValidHandle(&authinfo->ctx))
865 DeleteSecurityContext(&authinfo->ctx);
866 if (SecIsValidHandle(&authinfo->cred))
867 FreeCredentialsHandle(&authinfo->cred);
869 heap_free(authinfo->auth_data);
870 heap_free(authinfo->scheme);
871 heap_free(authinfo);
874 static UINT retrieve_cached_basic_authorization(const WCHAR *host, const WCHAR *realm, char **auth_data)
876 basicAuthorizationData *ad;
877 UINT rc = 0;
879 TRACE("Looking for authorization for %s:%s\n",debugstr_w(host),debugstr_w(realm));
881 EnterCriticalSection(&authcache_cs);
882 LIST_FOR_EACH_ENTRY(ad, &basicAuthorizationCache, basicAuthorizationData, entry)
884 if (!strcmpiW(host, ad->host) && (!realm || !strcmpW(realm, ad->realm)))
886 TRACE("Authorization found in cache\n");
887 *auth_data = heap_alloc(ad->authorizationLen);
888 memcpy(*auth_data,ad->authorization,ad->authorizationLen);
889 rc = ad->authorizationLen;
890 break;
893 LeaveCriticalSection(&authcache_cs);
894 return rc;
897 static void cache_basic_authorization(LPWSTR host, LPWSTR realm, LPSTR auth_data, UINT auth_data_len)
899 struct list *cursor;
900 basicAuthorizationData* ad = NULL;
902 TRACE("caching authorization for %s:%s = %s\n",debugstr_w(host),debugstr_w(realm),debugstr_an(auth_data,auth_data_len));
904 EnterCriticalSection(&authcache_cs);
905 LIST_FOR_EACH(cursor, &basicAuthorizationCache)
907 basicAuthorizationData *check = LIST_ENTRY(cursor,basicAuthorizationData,entry);
908 if (!strcmpiW(host,check->host) && !strcmpW(realm,check->realm))
910 ad = check;
911 break;
915 if (ad)
917 TRACE("Found match in cache, replacing\n");
918 heap_free(ad->authorization);
919 ad->authorization = heap_alloc(auth_data_len);
920 memcpy(ad->authorization, auth_data, auth_data_len);
921 ad->authorizationLen = auth_data_len;
923 else
925 ad = heap_alloc(sizeof(basicAuthorizationData));
926 ad->host = heap_strdupW(host);
927 ad->realm = heap_strdupW(realm);
928 ad->authorization = heap_alloc(auth_data_len);
929 memcpy(ad->authorization, auth_data, auth_data_len);
930 ad->authorizationLen = auth_data_len;
931 list_add_head(&basicAuthorizationCache,&ad->entry);
932 TRACE("authorization cached\n");
934 LeaveCriticalSection(&authcache_cs);
937 static BOOL retrieve_cached_authorization(LPWSTR host, LPWSTR scheme,
938 SEC_WINNT_AUTH_IDENTITY_W *nt_auth_identity)
940 authorizationData *ad;
942 TRACE("Looking for authorization for %s:%s\n", debugstr_w(host), debugstr_w(scheme));
944 EnterCriticalSection(&authcache_cs);
945 LIST_FOR_EACH_ENTRY(ad, &authorizationCache, authorizationData, entry) {
946 if(!strcmpiW(host, ad->host) && !strcmpiW(scheme, ad->scheme)) {
947 TRACE("Authorization found in cache\n");
949 nt_auth_identity->User = heap_strdupW(ad->user);
950 nt_auth_identity->Password = heap_strdupW(ad->password);
951 nt_auth_identity->Domain = heap_alloc(sizeof(WCHAR)*ad->domain_len);
952 if(!nt_auth_identity->User || !nt_auth_identity->Password ||
953 (!nt_auth_identity->Domain && ad->domain_len)) {
954 heap_free(nt_auth_identity->User);
955 heap_free(nt_auth_identity->Password);
956 heap_free(nt_auth_identity->Domain);
957 break;
960 nt_auth_identity->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
961 nt_auth_identity->UserLength = ad->user_len;
962 nt_auth_identity->PasswordLength = ad->password_len;
963 memcpy(nt_auth_identity->Domain, ad->domain, sizeof(WCHAR)*ad->domain_len);
964 nt_auth_identity->DomainLength = ad->domain_len;
965 LeaveCriticalSection(&authcache_cs);
966 return TRUE;
969 LeaveCriticalSection(&authcache_cs);
971 return FALSE;
974 static void cache_authorization(LPWSTR host, LPWSTR scheme,
975 SEC_WINNT_AUTH_IDENTITY_W *nt_auth_identity)
977 authorizationData *ad;
978 BOOL found = FALSE;
980 TRACE("Caching authorization for %s:%s\n", debugstr_w(host), debugstr_w(scheme));
982 EnterCriticalSection(&authcache_cs);
983 LIST_FOR_EACH_ENTRY(ad, &authorizationCache, authorizationData, entry)
984 if(!strcmpiW(host, ad->host) && !strcmpiW(scheme, ad->scheme)) {
985 found = TRUE;
986 break;
989 if(found) {
990 heap_free(ad->user);
991 heap_free(ad->password);
992 heap_free(ad->domain);
993 } else {
994 ad = heap_alloc(sizeof(authorizationData));
995 if(!ad) {
996 LeaveCriticalSection(&authcache_cs);
997 return;
1000 ad->host = heap_strdupW(host);
1001 ad->scheme = heap_strdupW(scheme);
1002 list_add_head(&authorizationCache, &ad->entry);
1005 ad->user = heap_strndupW(nt_auth_identity->User, nt_auth_identity->UserLength);
1006 ad->password = heap_strndupW(nt_auth_identity->Password, nt_auth_identity->PasswordLength);
1007 ad->domain = heap_strndupW(nt_auth_identity->Domain, nt_auth_identity->DomainLength);
1008 ad->user_len = nt_auth_identity->UserLength;
1009 ad->password_len = nt_auth_identity->PasswordLength;
1010 ad->domain_len = nt_auth_identity->DomainLength;
1012 if(!ad->host || !ad->scheme || !ad->user || !ad->password
1013 || (nt_auth_identity->Domain && !ad->domain)) {
1014 heap_free(ad->host);
1015 heap_free(ad->scheme);
1016 heap_free(ad->user);
1017 heap_free(ad->password);
1018 heap_free(ad->domain);
1019 list_remove(&ad->entry);
1020 heap_free(ad);
1023 LeaveCriticalSection(&authcache_cs);
1026 static BOOL HTTP_DoAuthorization( http_request_t *request, LPCWSTR pszAuthValue,
1027 struct HttpAuthInfo **ppAuthInfo,
1028 LPWSTR domain_and_username, LPWSTR password,
1029 LPWSTR host )
1031 SECURITY_STATUS sec_status;
1032 struct HttpAuthInfo *pAuthInfo = *ppAuthInfo;
1033 BOOL first = FALSE;
1034 LPWSTR szRealm = NULL;
1036 TRACE("%s\n", debugstr_w(pszAuthValue));
1038 if (!pAuthInfo)
1040 TimeStamp exp;
1042 first = TRUE;
1043 pAuthInfo = heap_alloc(sizeof(*pAuthInfo));
1044 if (!pAuthInfo)
1045 return FALSE;
1047 SecInvalidateHandle(&pAuthInfo->cred);
1048 SecInvalidateHandle(&pAuthInfo->ctx);
1049 memset(&pAuthInfo->exp, 0, sizeof(pAuthInfo->exp));
1050 pAuthInfo->attr = 0;
1051 pAuthInfo->auth_data = NULL;
1052 pAuthInfo->auth_data_len = 0;
1053 pAuthInfo->finished = FALSE;
1055 if (is_basic_auth_value(pszAuthValue,NULL))
1057 static const WCHAR szBasic[] = {'B','a','s','i','c',0};
1058 pAuthInfo->scheme = heap_strdupW(szBasic);
1059 if (!pAuthInfo->scheme)
1061 heap_free(pAuthInfo);
1062 return FALSE;
1065 else
1067 PVOID pAuthData;
1068 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity;
1070 pAuthInfo->scheme = heap_strdupW(pszAuthValue);
1071 if (!pAuthInfo->scheme)
1073 heap_free(pAuthInfo);
1074 return FALSE;
1077 if (domain_and_username)
1079 WCHAR *user = strchrW(domain_and_username, '\\');
1080 WCHAR *domain = domain_and_username;
1082 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
1084 pAuthData = &nt_auth_identity;
1086 if (user) user++;
1087 else
1089 user = domain_and_username;
1090 domain = NULL;
1093 nt_auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
1094 nt_auth_identity.User = user;
1095 nt_auth_identity.UserLength = strlenW(nt_auth_identity.User);
1096 nt_auth_identity.Domain = domain;
1097 nt_auth_identity.DomainLength = domain ? user - domain - 1 : 0;
1098 nt_auth_identity.Password = password;
1099 nt_auth_identity.PasswordLength = strlenW(nt_auth_identity.Password);
1101 cache_authorization(host, pAuthInfo->scheme, &nt_auth_identity);
1103 else if(retrieve_cached_authorization(host, pAuthInfo->scheme, &nt_auth_identity))
1104 pAuthData = &nt_auth_identity;
1105 else
1106 /* use default credentials */
1107 pAuthData = NULL;
1109 sec_status = AcquireCredentialsHandleW(NULL, pAuthInfo->scheme,
1110 SECPKG_CRED_OUTBOUND, NULL,
1111 pAuthData, NULL,
1112 NULL, &pAuthInfo->cred,
1113 &exp);
1115 if(pAuthData && !domain_and_username) {
1116 heap_free(nt_auth_identity.User);
1117 heap_free(nt_auth_identity.Domain);
1118 heap_free(nt_auth_identity.Password);
1121 if (sec_status == SEC_E_OK)
1123 PSecPkgInfoW sec_pkg_info;
1124 sec_status = QuerySecurityPackageInfoW(pAuthInfo->scheme, &sec_pkg_info);
1125 if (sec_status == SEC_E_OK)
1127 pAuthInfo->max_token = sec_pkg_info->cbMaxToken;
1128 FreeContextBuffer(sec_pkg_info);
1131 if (sec_status != SEC_E_OK)
1133 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
1134 debugstr_w(pAuthInfo->scheme), sec_status);
1135 heap_free(pAuthInfo->scheme);
1136 heap_free(pAuthInfo);
1137 return FALSE;
1140 *ppAuthInfo = pAuthInfo;
1142 else if (pAuthInfo->finished)
1143 return FALSE;
1145 if ((strlenW(pszAuthValue) < strlenW(pAuthInfo->scheme)) ||
1146 strncmpiW(pszAuthValue, pAuthInfo->scheme, strlenW(pAuthInfo->scheme)))
1148 ERR("authentication scheme changed from %s to %s\n",
1149 debugstr_w(pAuthInfo->scheme), debugstr_w(pszAuthValue));
1150 return FALSE;
1153 if (is_basic_auth_value(pszAuthValue,&szRealm))
1155 int userlen;
1156 int passlen;
1157 char *auth_data = NULL;
1158 UINT auth_data_len = 0;
1160 TRACE("basic authentication realm %s\n",debugstr_w(szRealm));
1162 if (!domain_and_username)
1164 if (host && szRealm)
1165 auth_data_len = retrieve_cached_basic_authorization(host, szRealm,&auth_data);
1166 if (auth_data_len == 0)
1168 heap_free(szRealm);
1169 return FALSE;
1172 else
1174 userlen = WideCharToMultiByte(CP_UTF8, 0, domain_and_username, lstrlenW(domain_and_username), NULL, 0, NULL, NULL);
1175 passlen = WideCharToMultiByte(CP_UTF8, 0, password, lstrlenW(password), NULL, 0, NULL, NULL);
1177 /* length includes a nul terminator, which will be re-used for the ':' */
1178 auth_data = heap_alloc(userlen + 1 + passlen);
1179 if (!auth_data)
1181 heap_free(szRealm);
1182 return FALSE;
1185 WideCharToMultiByte(CP_UTF8, 0, domain_and_username, -1, auth_data, userlen, NULL, NULL);
1186 auth_data[userlen] = ':';
1187 WideCharToMultiByte(CP_UTF8, 0, password, -1, &auth_data[userlen+1], passlen, NULL, NULL);
1188 auth_data_len = userlen + 1 + passlen;
1189 if (host && szRealm)
1190 cache_basic_authorization(host, szRealm, auth_data, auth_data_len);
1193 pAuthInfo->auth_data = auth_data;
1194 pAuthInfo->auth_data_len = auth_data_len;
1195 pAuthInfo->finished = TRUE;
1196 heap_free(szRealm);
1197 return TRUE;
1199 else
1201 LPCWSTR pszAuthData;
1202 SecBufferDesc out_desc, in_desc;
1203 SecBuffer out, in;
1204 unsigned char *buffer;
1205 ULONG context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE |
1206 ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE;
1208 in.BufferType = SECBUFFER_TOKEN;
1209 in.cbBuffer = 0;
1210 in.pvBuffer = NULL;
1212 in_desc.ulVersion = 0;
1213 in_desc.cBuffers = 1;
1214 in_desc.pBuffers = &in;
1216 pszAuthData = pszAuthValue + strlenW(pAuthInfo->scheme);
1217 if (*pszAuthData == ' ')
1219 pszAuthData++;
1220 in.cbBuffer = HTTP_DecodeBase64(pszAuthData, NULL);
1221 in.pvBuffer = heap_alloc(in.cbBuffer);
1222 HTTP_DecodeBase64(pszAuthData, in.pvBuffer);
1225 buffer = heap_alloc(pAuthInfo->max_token);
1227 out.BufferType = SECBUFFER_TOKEN;
1228 out.cbBuffer = pAuthInfo->max_token;
1229 out.pvBuffer = buffer;
1231 out_desc.ulVersion = 0;
1232 out_desc.cBuffers = 1;
1233 out_desc.pBuffers = &out;
1235 sec_status = InitializeSecurityContextW(first ? &pAuthInfo->cred : NULL,
1236 first ? NULL : &pAuthInfo->ctx,
1237 first ? request->server->name : NULL,
1238 context_req, 0, SECURITY_NETWORK_DREP,
1239 in.pvBuffer ? &in_desc : NULL,
1240 0, &pAuthInfo->ctx, &out_desc,
1241 &pAuthInfo->attr, &pAuthInfo->exp);
1242 if (sec_status == SEC_E_OK)
1244 pAuthInfo->finished = TRUE;
1245 pAuthInfo->auth_data = out.pvBuffer;
1246 pAuthInfo->auth_data_len = out.cbBuffer;
1247 TRACE("sending last auth packet\n");
1249 else if (sec_status == SEC_I_CONTINUE_NEEDED)
1251 pAuthInfo->auth_data = out.pvBuffer;
1252 pAuthInfo->auth_data_len = out.cbBuffer;
1253 TRACE("sending next auth packet\n");
1255 else
1257 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status);
1258 heap_free(out.pvBuffer);
1259 destroy_authinfo(pAuthInfo);
1260 *ppAuthInfo = NULL;
1261 return FALSE;
1265 return TRUE;
1268 /***********************************************************************
1269 * HTTP_HttpAddRequestHeadersW (internal)
1271 static DWORD HTTP_HttpAddRequestHeadersW(http_request_t *request,
1272 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1274 LPWSTR lpszStart;
1275 LPWSTR lpszEnd;
1276 LPWSTR buffer;
1277 DWORD len, res = ERROR_HTTP_INVALID_HEADER;
1279 TRACE("copying header: %s\n", debugstr_wn(lpszHeader, dwHeaderLength));
1281 if( dwHeaderLength == ~0U )
1282 len = strlenW(lpszHeader);
1283 else
1284 len = dwHeaderLength;
1285 buffer = heap_alloc(sizeof(WCHAR)*(len+1));
1286 lstrcpynW( buffer, lpszHeader, len + 1);
1288 lpszStart = buffer;
1292 LPWSTR * pFieldAndValue;
1294 lpszEnd = lpszStart;
1296 while (*lpszEnd != '\0')
1298 if (*lpszEnd == '\r' || *lpszEnd == '\n')
1299 break;
1300 lpszEnd++;
1303 if (*lpszStart == '\0')
1304 break;
1306 if (*lpszEnd == '\r' || *lpszEnd == '\n')
1308 *lpszEnd = '\0';
1309 lpszEnd++; /* Jump over newline */
1311 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
1312 if (*lpszStart == '\0')
1314 /* Skip 0-length headers */
1315 lpszStart = lpszEnd;
1316 res = ERROR_SUCCESS;
1317 continue;
1319 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
1320 if (pFieldAndValue)
1322 res = HTTP_ProcessHeader(request, pFieldAndValue[0],
1323 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
1324 HTTP_FreeTokens(pFieldAndValue);
1327 lpszStart = lpszEnd;
1328 } while (res == ERROR_SUCCESS);
1330 heap_free(buffer);
1331 return res;
1334 /***********************************************************************
1335 * HttpAddRequestHeadersW (WININET.@)
1337 * Adds one or more HTTP header to the request handler
1339 * NOTE
1340 * On Windows if dwHeaderLength includes the trailing '\0', then
1341 * HttpAddRequestHeadersW() adds it too. However this results in an
1342 * invalid HTTP header which is rejected by some servers so we probably
1343 * don't need to match Windows on that point.
1345 * RETURNS
1346 * TRUE on success
1347 * FALSE on failure
1350 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
1351 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1353 http_request_t *request;
1354 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1356 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_wn(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
1358 if (!lpszHeader)
1359 return TRUE;
1361 request = (http_request_t*) get_handle_object( hHttpRequest );
1362 if (request && request->hdr.htype == WH_HHTTPREQ)
1363 res = HTTP_HttpAddRequestHeadersW( request, lpszHeader, dwHeaderLength, dwModifier );
1364 if( request )
1365 WININET_Release( &request->hdr );
1367 if(res != ERROR_SUCCESS)
1368 SetLastError(res);
1369 return res == ERROR_SUCCESS;
1372 /***********************************************************************
1373 * HttpAddRequestHeadersA (WININET.@)
1375 * Adds one or more HTTP header to the request handler
1377 * RETURNS
1378 * TRUE on success
1379 * FALSE on failure
1382 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
1383 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1385 WCHAR *headers = NULL;
1386 BOOL r;
1388 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_an(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
1390 if(lpszHeader)
1391 headers = heap_strndupAtoW(lpszHeader, dwHeaderLength, &dwHeaderLength);
1393 r = HttpAddRequestHeadersW(hHttpRequest, headers, dwHeaderLength, dwModifier);
1395 heap_free(headers);
1396 return r;
1399 static void free_accept_types( WCHAR **accept_types )
1401 WCHAR *ptr, **types = accept_types;
1403 if (!types) return;
1404 while ((ptr = *types))
1406 heap_free( ptr );
1407 types++;
1409 heap_free( accept_types );
1412 static WCHAR **convert_accept_types( const char **accept_types )
1414 unsigned int count;
1415 const char **types = accept_types;
1416 WCHAR **typesW;
1417 BOOL invalid_pointer = FALSE;
1419 if (!types) return NULL;
1420 count = 0;
1421 while (*types)
1423 __TRY
1425 /* find out how many there are */
1426 if (*types && **types)
1428 TRACE("accept type: %s\n", debugstr_a(*types));
1429 count++;
1432 __EXCEPT_PAGE_FAULT
1434 WARN("invalid accept type pointer\n");
1435 invalid_pointer = TRUE;
1437 __ENDTRY;
1438 types++;
1440 if (invalid_pointer) return NULL;
1441 if (!(typesW = heap_alloc( sizeof(WCHAR *) * (count + 1) ))) return NULL;
1442 count = 0;
1443 types = accept_types;
1444 while (*types)
1446 if (*types && **types) typesW[count++] = heap_strdupAtoW( *types );
1447 types++;
1449 typesW[count] = NULL;
1450 return typesW;
1453 /***********************************************************************
1454 * HttpOpenRequestA (WININET.@)
1456 * Open a HTTP request handle
1458 * RETURNS
1459 * HINTERNET a HTTP request handle on success
1460 * NULL on failure
1463 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
1464 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
1465 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
1466 DWORD dwFlags, DWORD_PTR dwContext)
1468 LPWSTR szVerb = NULL, szObjectName = NULL;
1469 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
1470 HINTERNET rc = NULL;
1472 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
1473 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
1474 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
1475 dwFlags, dwContext);
1477 if (lpszVerb)
1479 szVerb = heap_strdupAtoW(lpszVerb);
1480 if ( !szVerb )
1481 goto end;
1484 if (lpszObjectName)
1486 szObjectName = heap_strdupAtoW(lpszObjectName);
1487 if ( !szObjectName )
1488 goto end;
1491 if (lpszVersion)
1493 szVersion = heap_strdupAtoW(lpszVersion);
1494 if ( !szVersion )
1495 goto end;
1498 if (lpszReferrer)
1500 szReferrer = heap_strdupAtoW(lpszReferrer);
1501 if ( !szReferrer )
1502 goto end;
1505 szAcceptTypes = convert_accept_types( lpszAcceptTypes );
1506 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName, szVersion, szReferrer,
1507 (const WCHAR **)szAcceptTypes, dwFlags, dwContext);
1509 end:
1510 free_accept_types(szAcceptTypes);
1511 heap_free(szReferrer);
1512 heap_free(szVersion);
1513 heap_free(szObjectName);
1514 heap_free(szVerb);
1515 return rc;
1518 /***********************************************************************
1519 * HTTP_EncodeBase64
1521 static UINT HTTP_EncodeBase64( LPCSTR bin, unsigned int len, LPWSTR base64 )
1523 UINT n = 0, x;
1524 static const CHAR HTTP_Base64Enc[] =
1525 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1527 while( len > 0 )
1529 /* first 6 bits, all from bin[0] */
1530 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
1531 x = (bin[0] & 3) << 4;
1533 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1534 if( len == 1 )
1536 base64[n++] = HTTP_Base64Enc[x];
1537 base64[n++] = '=';
1538 base64[n++] = '=';
1539 break;
1541 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
1542 x = ( bin[1] & 0x0f ) << 2;
1544 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1545 if( len == 2 )
1547 base64[n++] = HTTP_Base64Enc[x];
1548 base64[n++] = '=';
1549 break;
1551 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
1553 /* last 6 bits, all from bin [2] */
1554 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
1555 bin += 3;
1556 len -= 3;
1558 base64[n] = 0;
1559 return n;
1562 static const signed char HTTP_Base64Dec[] =
1564 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00 */
1565 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10 */
1566 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20 */
1567 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 0x30 */
1568 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40 */
1569 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50 */
1570 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60 */
1571 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 0x70 */
1574 /***********************************************************************
1575 * HTTP_DecodeBase64
1577 static UINT HTTP_DecodeBase64( LPCWSTR base64, LPSTR bin )
1579 unsigned int n = 0;
1581 while(*base64)
1583 signed char in[4];
1585 if (base64[0] >= ARRAYSIZE(HTTP_Base64Dec) ||
1586 ((in[0] = HTTP_Base64Dec[base64[0]]) == -1) ||
1587 base64[1] >= ARRAYSIZE(HTTP_Base64Dec) ||
1588 ((in[1] = HTTP_Base64Dec[base64[1]]) == -1))
1590 WARN("invalid base64: %s\n", debugstr_w(base64));
1591 return 0;
1593 if (bin)
1594 bin[n] = (unsigned char) (in[0] << 2 | in[1] >> 4);
1595 n++;
1597 if ((base64[2] == '=') && (base64[3] == '='))
1598 break;
1599 if (base64[2] > ARRAYSIZE(HTTP_Base64Dec) ||
1600 ((in[2] = HTTP_Base64Dec[base64[2]]) == -1))
1602 WARN("invalid base64: %s\n", debugstr_w(&base64[2]));
1603 return 0;
1605 if (bin)
1606 bin[n] = (unsigned char) (in[1] << 4 | in[2] >> 2);
1607 n++;
1609 if (base64[3] == '=')
1610 break;
1611 if (base64[3] > ARRAYSIZE(HTTP_Base64Dec) ||
1612 ((in[3] = HTTP_Base64Dec[base64[3]]) == -1))
1614 WARN("invalid base64: %s\n", debugstr_w(&base64[3]));
1615 return 0;
1617 if (bin)
1618 bin[n] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
1619 n++;
1621 base64 += 4;
1624 return n;
1627 static WCHAR *encode_auth_data( const WCHAR *scheme, const char *data, UINT data_len )
1629 WCHAR *ret;
1630 UINT len, scheme_len = strlenW( scheme );
1632 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1633 len = scheme_len + 1 + ((data_len + 2) * 4) / 3;
1634 if (!(ret = heap_alloc( (len + 1) * sizeof(WCHAR) ))) return NULL;
1635 memcpy( ret, scheme, scheme_len * sizeof(WCHAR) );
1636 ret[scheme_len] = ' ';
1637 HTTP_EncodeBase64( data, data_len, ret + scheme_len + 1 );
1638 return ret;
1642 /***********************************************************************
1643 * HTTP_InsertAuthorization
1645 * Insert or delete the authorization field in the request header.
1647 static BOOL HTTP_InsertAuthorization( http_request_t *request, struct HttpAuthInfo *pAuthInfo, LPCWSTR header )
1649 static const WCHAR wszBasic[] = {'B','a','s','i','c',0};
1650 WCHAR *host, *authorization = NULL;
1652 if (pAuthInfo)
1654 if (pAuthInfo->auth_data_len)
1656 if (!(authorization = encode_auth_data(pAuthInfo->scheme, pAuthInfo->auth_data, pAuthInfo->auth_data_len)))
1657 return FALSE;
1659 /* clear the data as it isn't valid now that it has been sent to the
1660 * server, unless it's Basic authentication which doesn't do
1661 * connection tracking */
1662 if (strcmpiW(pAuthInfo->scheme, wszBasic))
1664 heap_free(pAuthInfo->auth_data);
1665 pAuthInfo->auth_data = NULL;
1666 pAuthInfo->auth_data_len = 0;
1670 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1672 HTTP_ProcessHeader(request, header, authorization,
1673 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
1674 heap_free(authorization);
1676 else if (!strcmpW(header, szAuthorization) && (host = get_host_header(request)))
1678 UINT data_len;
1679 char *data;
1681 if ((data_len = retrieve_cached_basic_authorization(host, NULL, &data)))
1683 TRACE("Found cached basic authorization for %s\n", debugstr_w(host));
1685 if (!(authorization = encode_auth_data(wszBasic, data, data_len)))
1687 heap_free(data);
1688 heap_free(host);
1689 return FALSE;
1692 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1694 HTTP_ProcessHeader(request, header, authorization,
1695 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_ADD);
1696 heap_free(data);
1697 heap_free(authorization);
1699 heap_free(host);
1701 return TRUE;
1704 static WCHAR *build_proxy_path_url(http_request_t *req)
1706 DWORD size, len;
1707 WCHAR *url;
1709 len = strlenW(req->server->scheme_host_port);
1710 size = len + strlenW(req->path) + 1;
1711 if(*req->path != '/')
1712 size++;
1713 url = heap_alloc(size * sizeof(WCHAR));
1714 if(!url)
1715 return NULL;
1717 memcpy(url, req->server->scheme_host_port, len*sizeof(WCHAR));
1718 if(*req->path != '/')
1719 url[len++] = '/';
1721 strcpyW(url+len, req->path);
1723 TRACE("url=%s\n", debugstr_w(url));
1724 return url;
1727 static BOOL HTTP_DomainMatches(LPCWSTR server, substr_t domain)
1729 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
1730 const WCHAR *dot, *ptr;
1731 int len;
1733 if(domain.len == sizeof(localW)/sizeof(WCHAR)-1 && !strncmpiW(domain.str, localW, domain.len) && !strchrW(server, '.' ))
1734 return TRUE;
1736 if(domain.len && *domain.str != '*')
1737 return domain.len == strlenW(server) && !strncmpiW(server, domain.str, domain.len);
1739 if(domain.len < 2 || domain.str[1] != '.')
1740 return FALSE;
1742 /* For a hostname to match a wildcard, the last domain must match
1743 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
1744 * hostname is www.foo.a.b, it matches, but a.b does not.
1746 dot = strchrW(server, '.');
1747 if(!dot)
1748 return FALSE;
1750 len = strlenW(dot + 1);
1751 if(len <= domain.len - 2)
1752 return FALSE;
1754 /* The server's domain is longer than the wildcard, so it
1755 * could be a subdomain. Compare the last portion of the
1756 * server's domain.
1758 ptr = dot + 1 + len - domain.len + 2;
1759 if(!strncmpiW(ptr, domain.str+2, domain.len-2))
1760 /* This is only a match if the preceding character is
1761 * a '.', i.e. that it is a matching domain. E.g.
1762 * if domain is '*.b.c' and server is 'www.ab.c' they
1763 * do not match.
1765 return *(ptr - 1) == '.';
1767 return len == domain.len-2 && !strncmpiW(dot + 1, domain.str + 2, len);
1770 static BOOL HTTP_ShouldBypassProxy(appinfo_t *lpwai, LPCWSTR server)
1772 LPCWSTR ptr;
1773 BOOL ret = FALSE;
1775 if (!lpwai->proxyBypass) return FALSE;
1776 ptr = lpwai->proxyBypass;
1777 while(1) {
1778 LPCWSTR tmp = ptr;
1780 ptr = strchrW( ptr, ';' );
1781 if (!ptr)
1782 ptr = strchrW( tmp, ' ' );
1783 if (!ptr)
1784 ptr = tmp + strlenW(tmp);
1785 ret = HTTP_DomainMatches( server, substr(tmp, ptr-tmp) );
1786 if (ret || !*ptr)
1787 break;
1788 ptr++;
1790 return ret;
1793 /***********************************************************************
1794 * HTTP_DealWithProxy
1796 static BOOL HTTP_DealWithProxy(appinfo_t *hIC, http_session_t *session, http_request_t *request)
1798 static const WCHAR protoHttp[] = { 'h','t','t','p',0 };
1799 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 };
1800 static WCHAR szNul[] = { 0 };
1801 URL_COMPONENTSW UrlComponents = { sizeof(UrlComponents) };
1802 server_t *new_server = NULL;
1803 WCHAR *proxy;
1805 proxy = INTERNET_FindProxyForProtocol(hIC->proxy, protoHttp);
1806 if(!proxy)
1807 return FALSE;
1808 if(CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1809 proxy, strlenW(szHttp), szHttp, strlenW(szHttp))) {
1810 WCHAR *proxy_url = heap_alloc(strlenW(proxy)*sizeof(WCHAR) + sizeof(szHttp));
1811 if(!proxy_url) {
1812 heap_free(proxy);
1813 return FALSE;
1815 strcpyW(proxy_url, szHttp);
1816 strcatW(proxy_url, proxy);
1817 heap_free(proxy);
1818 proxy = proxy_url;
1821 UrlComponents.dwHostNameLength = 1;
1822 if(InternetCrackUrlW(proxy, 0, 0, &UrlComponents) && UrlComponents.dwHostNameLength) {
1823 if( !request->path )
1824 request->path = szNul;
1826 new_server = get_server(substr(UrlComponents.lpszHostName, UrlComponents.dwHostNameLength),
1827 UrlComponents.nPort, UrlComponents.nScheme == INTERNET_SCHEME_HTTPS, TRUE);
1829 heap_free(proxy);
1830 if(!new_server)
1831 return FALSE;
1833 request->proxy = new_server;
1835 TRACE("proxy server=%s port=%d\n", debugstr_w(new_server->name), new_server->port);
1836 return TRUE;
1839 static DWORD HTTP_ResolveName(http_request_t *request)
1841 server_t *server = request->proxy ? request->proxy : request->server;
1842 int addr_len;
1844 if(server->addr_len)
1845 return ERROR_SUCCESS;
1847 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
1848 INTERNET_STATUS_RESOLVING_NAME,
1849 server->name,
1850 (strlenW(server->name)+1) * sizeof(WCHAR));
1852 addr_len = sizeof(server->addr);
1853 if (!GetAddress(server->name, server->port, (SOCKADDR*)&server->addr, &addr_len, server->addr_str))
1854 return ERROR_INTERNET_NAME_NOT_RESOLVED;
1856 server->addr_len = addr_len;
1857 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
1858 INTERNET_STATUS_NAME_RESOLVED,
1859 server->addr_str, strlen(server->addr_str)+1);
1861 TRACE("resolved %s to %s\n", debugstr_w(server->name), server->addr_str);
1862 return ERROR_SUCCESS;
1865 static WCHAR *compose_request_url(http_request_t *req)
1867 static const WCHAR http[] = { 'h','t','t','p',':','/','/',0 };
1868 static const WCHAR https[] = { 'h','t','t','p','s',':','/','/',0 };
1869 const WCHAR *host, *scheme;
1870 WCHAR *buf, *ptr;
1871 size_t len;
1873 host = req->server->canon_host_port;
1875 if (req->server->is_https)
1876 scheme = https;
1877 else
1878 scheme = http;
1880 len = strlenW(scheme) + strlenW(host) + (req->path[0] != '/' ? 1 : 0) + strlenW(req->path);
1881 ptr = buf = heap_alloc((len+1) * sizeof(WCHAR));
1882 if(buf) {
1883 strcpyW(ptr, scheme);
1884 ptr += strlenW(ptr);
1886 strcpyW(ptr, host);
1887 ptr += strlenW(ptr);
1889 if(req->path[0] != '/')
1890 *ptr++ = '/';
1892 strcpyW(ptr, req->path);
1893 ptr += strlenW(ptr);
1894 *ptr = 0;
1897 return buf;
1901 /***********************************************************************
1902 * HTTPREQ_Destroy (internal)
1904 * Deallocate request handle
1907 static void HTTPREQ_Destroy(object_header_t *hdr)
1909 http_request_t *request = (http_request_t*) hdr;
1910 DWORD i;
1912 TRACE("\n");
1914 if(request->hCacheFile)
1915 CloseHandle(request->hCacheFile);
1916 if(request->req_file)
1917 req_file_release(request->req_file);
1919 request->headers_section.DebugInfo->Spare[0] = 0;
1920 DeleteCriticalSection( &request->headers_section );
1921 request->read_section.DebugInfo->Spare[0] = 0;
1922 DeleteCriticalSection( &request->read_section );
1923 WININET_Release(&request->session->hdr);
1925 destroy_authinfo(request->authInfo);
1926 destroy_authinfo(request->proxyAuthInfo);
1928 if(request->server)
1929 server_release(request->server);
1930 if(request->proxy)
1931 server_release(request->proxy);
1933 heap_free(request->path);
1934 heap_free(request->verb);
1935 heap_free(request->version);
1936 heap_free(request->statusText);
1938 for (i = 0; i < request->nCustHeaders; i++)
1940 heap_free(request->custHeaders[i].lpszField);
1941 heap_free(request->custHeaders[i].lpszValue);
1943 destroy_data_stream(request->data_stream);
1944 heap_free(request->custHeaders);
1947 static void http_release_netconn(http_request_t *req, BOOL reuse)
1949 TRACE("%p %p %x\n",req, req->netconn, reuse);
1951 if(!is_valid_netconn(req->netconn))
1952 return;
1954 if(reuse && req->netconn->keep_alive) {
1955 BOOL run_collector;
1957 EnterCriticalSection(&connection_pool_cs);
1959 list_add_head(&req->netconn->server->conn_pool, &req->netconn->pool_entry);
1960 req->netconn->keep_until = GetTickCount64() + COLLECT_TIME;
1961 req->netconn = NULL;
1963 run_collector = !collector_running;
1964 collector_running = TRUE;
1966 LeaveCriticalSection(&connection_pool_cs);
1968 if(run_collector) {
1969 HANDLE thread = NULL;
1970 HMODULE module;
1972 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (const WCHAR*)WININET_hModule, &module);
1973 if(module)
1974 thread = CreateThread(NULL, 0, collect_connections_proc, NULL, 0, NULL);
1975 if(!thread) {
1976 EnterCriticalSection(&connection_pool_cs);
1977 collector_running = FALSE;
1978 LeaveCriticalSection(&connection_pool_cs);
1980 if(module)
1981 FreeLibrary(module);
1983 else
1984 CloseHandle(thread);
1986 return;
1989 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
1990 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
1992 close_netconn(req->netconn);
1994 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
1995 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
1998 static BOOL HTTP_KeepAlive(http_request_t *request)
2000 WCHAR szVersion[10];
2001 WCHAR szConnectionResponse[20];
2002 DWORD dwBufferSize = sizeof(szVersion);
2003 BOOL keepalive = FALSE;
2005 /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
2006 * the connection is keep-alive by default */
2007 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_VERSION, szVersion, &dwBufferSize, NULL) == ERROR_SUCCESS
2008 && !strcmpiW(szVersion, g_szHttp1_1))
2010 keepalive = TRUE;
2013 dwBufferSize = sizeof(szConnectionResponse);
2014 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_PROXY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) == ERROR_SUCCESS
2015 || HTTP_HttpQueryInfoW(request, HTTP_QUERY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) == ERROR_SUCCESS)
2017 keepalive = !strcmpiW(szConnectionResponse, szKeepAlive);
2020 return keepalive;
2023 static void HTTPREQ_CloseConnection(object_header_t *hdr)
2025 http_request_t *req = (http_request_t*)hdr;
2027 http_release_netconn(req, drain_content(req, FALSE));
2030 static DWORD str_to_buffer(const WCHAR *str, void *buffer, DWORD *size, BOOL unicode)
2032 int len;
2033 if (unicode)
2035 WCHAR *buf = buffer;
2037 if (str) len = strlenW(str);
2038 else len = 0;
2039 if (*size < (len + 1) * sizeof(WCHAR))
2041 *size = (len + 1) * sizeof(WCHAR);
2042 return ERROR_INSUFFICIENT_BUFFER;
2044 if (str) strcpyW(buf, str);
2045 else buf[0] = 0;
2047 *size = len;
2048 return ERROR_SUCCESS;
2050 else
2052 char *buf = buffer;
2054 if (str) len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
2055 else len = 1;
2056 if (*size < len)
2058 *size = len;
2059 return ERROR_INSUFFICIENT_BUFFER;
2061 if (str) WideCharToMultiByte(CP_ACP, 0, str, -1, buf, *size, NULL, NULL);
2062 else buf[0] = 0;
2064 *size = len - 1;
2065 return ERROR_SUCCESS;
2069 static DWORD HTTPREQ_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2071 http_request_t *req = (http_request_t*)hdr;
2073 switch(option) {
2074 case INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO:
2076 INTERNET_DIAGNOSTIC_SOCKET_INFO *info = buffer;
2078 FIXME("INTERNET_DIAGNOSTIC_SOCKET_INFO stub\n");
2080 if (*size < sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO))
2081 return ERROR_INSUFFICIENT_BUFFER;
2082 *size = sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO);
2083 /* FIXME: can't get a SOCKET from our connection since we don't use
2084 * winsock
2086 info->Socket = 0;
2087 /* FIXME: get source port from req->netConnection */
2088 info->SourcePort = 0;
2089 info->DestPort = req->server->port;
2090 info->Flags = 0;
2091 if (HTTP_KeepAlive(req))
2092 info->Flags |= IDSI_FLAG_KEEP_ALIVE;
2093 if (req->proxy)
2094 info->Flags |= IDSI_FLAG_PROXY;
2095 if (is_valid_netconn(req->netconn) && req->netconn->secure)
2096 info->Flags |= IDSI_FLAG_SECURE;
2098 return ERROR_SUCCESS;
2101 case 98:
2102 TRACE("Queried undocumented option 98, forwarding to INTERNET_OPTION_SECURITY_FLAGS\n");
2103 /* fall through */
2104 case INTERNET_OPTION_SECURITY_FLAGS:
2106 DWORD flags;
2108 if (*size < sizeof(ULONG))
2109 return ERROR_INSUFFICIENT_BUFFER;
2111 *size = sizeof(DWORD);
2112 flags = is_valid_netconn(req->netconn) ? req->netconn->security_flags : req->security_flags | req->server->security_flags;
2113 *(DWORD *)buffer = flags;
2115 TRACE("INTERNET_OPTION_SECURITY_FLAGS %x\n", flags);
2116 return ERROR_SUCCESS;
2119 case INTERNET_OPTION_HANDLE_TYPE:
2120 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
2122 if (*size < sizeof(ULONG))
2123 return ERROR_INSUFFICIENT_BUFFER;
2125 *size = sizeof(DWORD);
2126 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_HTTP_REQUEST;
2127 return ERROR_SUCCESS;
2129 case INTERNET_OPTION_URL: {
2130 WCHAR *url;
2131 DWORD res;
2133 TRACE("INTERNET_OPTION_URL\n");
2135 url = compose_request_url(req);
2136 if(!url)
2137 return ERROR_OUTOFMEMORY;
2139 res = str_to_buffer(url, buffer, size, unicode);
2140 heap_free(url);
2141 return res;
2143 case INTERNET_OPTION_USER_AGENT:
2144 return str_to_buffer(req->session->appInfo->agent, buffer, size, unicode);
2145 case INTERNET_OPTION_USERNAME:
2146 return str_to_buffer(req->session->userName, buffer, size, unicode);
2147 case INTERNET_OPTION_PASSWORD:
2148 return str_to_buffer(req->session->password, buffer, size, unicode);
2149 case INTERNET_OPTION_PROXY_USERNAME:
2150 return str_to_buffer(req->session->appInfo->proxyUsername, buffer, size, unicode);
2151 case INTERNET_OPTION_PROXY_PASSWORD:
2152 return str_to_buffer(req->session->appInfo->proxyPassword, buffer, size, unicode);
2154 case INTERNET_OPTION_CACHE_TIMESTAMPS: {
2155 INTERNET_CACHE_ENTRY_INFOW *info;
2156 INTERNET_CACHE_TIMESTAMPS *ts = buffer;
2157 DWORD nbytes, error;
2158 BOOL ret;
2160 TRACE("INTERNET_OPTION_CACHE_TIMESTAMPS\n");
2162 if(!req->req_file)
2163 return ERROR_FILE_NOT_FOUND;
2165 if (*size < sizeof(*ts))
2167 *size = sizeof(*ts);
2168 return ERROR_INSUFFICIENT_BUFFER;
2171 nbytes = 0;
2172 ret = GetUrlCacheEntryInfoW(req->req_file->url, NULL, &nbytes);
2173 error = GetLastError();
2174 if (!ret && error == ERROR_INSUFFICIENT_BUFFER)
2176 if (!(info = heap_alloc(nbytes)))
2177 return ERROR_OUTOFMEMORY;
2179 GetUrlCacheEntryInfoW(req->req_file->url, info, &nbytes);
2181 ts->ftExpires = info->ExpireTime;
2182 ts->ftLastModified = info->LastModifiedTime;
2184 heap_free(info);
2185 *size = sizeof(*ts);
2186 return ERROR_SUCCESS;
2188 return error;
2191 case INTERNET_OPTION_DATAFILE_NAME: {
2192 DWORD req_size;
2194 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
2196 if(!req->req_file) {
2197 *size = 0;
2198 return ERROR_INTERNET_ITEM_NOT_FOUND;
2201 if(unicode) {
2202 req_size = (lstrlenW(req->req_file->file_name)+1) * sizeof(WCHAR);
2203 if(*size < req_size)
2204 return ERROR_INSUFFICIENT_BUFFER;
2206 *size = req_size;
2207 memcpy(buffer, req->req_file->file_name, *size);
2208 return ERROR_SUCCESS;
2209 }else {
2210 req_size = WideCharToMultiByte(CP_ACP, 0, req->req_file->file_name, -1, NULL, 0, NULL, NULL);
2211 if (req_size > *size)
2212 return ERROR_INSUFFICIENT_BUFFER;
2214 *size = WideCharToMultiByte(CP_ACP, 0, req->req_file->file_name,
2215 -1, buffer, *size, NULL, NULL);
2216 return ERROR_SUCCESS;
2220 case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT: {
2221 PCCERT_CONTEXT context;
2223 if(!req->netconn)
2224 return ERROR_INTERNET_INVALID_OPERATION;
2226 if(*size < sizeof(INTERNET_CERTIFICATE_INFOA)) {
2227 *size = sizeof(INTERNET_CERTIFICATE_INFOA);
2228 return ERROR_INSUFFICIENT_BUFFER;
2231 context = (PCCERT_CONTEXT)NETCON_GetCert(req->netconn);
2232 if(context) {
2233 INTERNET_CERTIFICATE_INFOA *info = (INTERNET_CERTIFICATE_INFOA*)buffer;
2234 DWORD len;
2236 memset(info, 0, sizeof(*info));
2237 info->ftExpiry = context->pCertInfo->NotAfter;
2238 info->ftStart = context->pCertInfo->NotBefore;
2239 len = CertNameToStrA(context->dwCertEncodingType,
2240 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG, NULL, 0);
2241 info->lpszSubjectInfo = LocalAlloc(0, len);
2242 if(info->lpszSubjectInfo)
2243 CertNameToStrA(context->dwCertEncodingType,
2244 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG,
2245 info->lpszSubjectInfo, len);
2246 len = CertNameToStrA(context->dwCertEncodingType,
2247 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG, NULL, 0);
2248 info->lpszIssuerInfo = LocalAlloc(0, len);
2249 if(info->lpszIssuerInfo)
2250 CertNameToStrA(context->dwCertEncodingType,
2251 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG,
2252 info->lpszIssuerInfo, len);
2253 info->dwKeySize = NETCON_GetCipherStrength(req->netconn);
2254 CertFreeCertificateContext(context);
2255 return ERROR_SUCCESS;
2257 return ERROR_NOT_SUPPORTED;
2259 case INTERNET_OPTION_CONNECT_TIMEOUT:
2260 if (*size < sizeof(DWORD))
2261 return ERROR_INSUFFICIENT_BUFFER;
2263 *size = sizeof(DWORD);
2264 *(DWORD *)buffer = req->connect_timeout;
2265 return ERROR_SUCCESS;
2266 case INTERNET_OPTION_REQUEST_FLAGS: {
2267 DWORD flags = 0;
2269 if (*size < sizeof(DWORD))
2270 return ERROR_INSUFFICIENT_BUFFER;
2272 /* FIXME: Add support for:
2273 * INTERNET_REQFLAG_FROM_CACHE
2274 * INTERNET_REQFLAG_CACHE_WRITE_DISABLED
2277 if(req->proxy)
2278 flags |= INTERNET_REQFLAG_VIA_PROXY;
2279 if(!req->status_code)
2280 flags |= INTERNET_REQFLAG_NO_HEADERS;
2282 TRACE("INTERNET_OPTION_REQUEST_FLAGS returning %x\n", flags);
2284 *size = sizeof(DWORD);
2285 *(DWORD*)buffer = flags;
2286 return ERROR_SUCCESS;
2290 return INET_QueryOption(hdr, option, buffer, size, unicode);
2293 static DWORD HTTPREQ_SetOption(object_header_t *hdr, DWORD option, void *buffer, DWORD size)
2295 http_request_t *req = (http_request_t*)hdr;
2297 switch(option) {
2298 case 99: /* Undocumented, seems to be INTERNET_OPTION_SECURITY_FLAGS with argument validation */
2299 TRACE("Undocumented option 99\n");
2301 if (!buffer || size != sizeof(DWORD))
2302 return ERROR_INVALID_PARAMETER;
2303 if(*(DWORD*)buffer & ~SECURITY_SET_MASK)
2304 return ERROR_INTERNET_OPTION_NOT_SETTABLE;
2306 /* fall through */
2307 case INTERNET_OPTION_SECURITY_FLAGS:
2309 DWORD flags;
2311 if (!buffer || size != sizeof(DWORD))
2312 return ERROR_INVALID_PARAMETER;
2313 flags = *(DWORD *)buffer;
2314 TRACE("INTERNET_OPTION_SECURITY_FLAGS %08x\n", flags);
2315 flags &= SECURITY_SET_MASK;
2316 req->security_flags |= flags;
2317 if(is_valid_netconn(req->netconn))
2318 req->netconn->security_flags |= flags;
2319 return ERROR_SUCCESS;
2321 case INTERNET_OPTION_CONNECT_TIMEOUT:
2322 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2323 req->connect_timeout = *(DWORD *)buffer;
2324 return ERROR_SUCCESS;
2326 case INTERNET_OPTION_SEND_TIMEOUT:
2327 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2328 req->send_timeout = *(DWORD *)buffer;
2329 return ERROR_SUCCESS;
2331 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2332 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2333 req->receive_timeout = *(DWORD *)buffer;
2334 return ERROR_SUCCESS;
2336 case INTERNET_OPTION_USERNAME:
2337 heap_free(req->session->userName);
2338 if (!(req->session->userName = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2339 return ERROR_SUCCESS;
2341 case INTERNET_OPTION_PASSWORD:
2342 heap_free(req->session->password);
2343 if (!(req->session->password = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2344 return ERROR_SUCCESS;
2346 case INTERNET_OPTION_PROXY_USERNAME:
2347 heap_free(req->session->appInfo->proxyUsername);
2348 if (!(req->session->appInfo->proxyUsername = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2349 return ERROR_SUCCESS;
2351 case INTERNET_OPTION_PROXY_PASSWORD:
2352 heap_free(req->session->appInfo->proxyPassword);
2353 if (!(req->session->appInfo->proxyPassword = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2354 return ERROR_SUCCESS;
2356 case INTERNET_OPTION_HTTP_DECODING:
2357 if(size != sizeof(BOOL))
2358 return ERROR_INVALID_PARAMETER;
2359 req->decoding = *(BOOL*)buffer;
2360 return ERROR_SUCCESS;
2363 return INET_SetOption(hdr, option, buffer, size);
2366 static void commit_cache_entry(http_request_t *req)
2368 WCHAR *header;
2369 DWORD header_len;
2370 BOOL res;
2372 TRACE("%p\n", req);
2374 CloseHandle(req->hCacheFile);
2375 req->hCacheFile = NULL;
2377 header = build_response_header(req, TRUE);
2378 header_len = (header ? strlenW(header) : 0);
2379 res = CommitUrlCacheEntryW(req->req_file->url, req->req_file->file_name, req->expires,
2380 req->last_modified, NORMAL_CACHE_ENTRY,
2381 header, header_len, NULL, 0);
2382 if(res)
2383 req->req_file->is_committed = TRUE;
2384 else
2385 WARN("CommitUrlCacheEntry failed: %u\n", GetLastError());
2386 heap_free(header);
2389 static void create_cache_entry(http_request_t *req)
2391 static const WCHAR no_cacheW[] = {'n','o','-','c','a','c','h','e',0};
2392 static const WCHAR no_storeW[] = {'n','o','-','s','t','o','r','e',0};
2394 WCHAR file_name[MAX_PATH+1];
2395 WCHAR *url;
2396 BOOL b = TRUE;
2398 /* FIXME: We should free previous cache file earlier */
2399 if(req->req_file) {
2400 req_file_release(req->req_file);
2401 req->req_file = NULL;
2403 if(req->hCacheFile) {
2404 CloseHandle(req->hCacheFile);
2405 req->hCacheFile = NULL;
2408 if(req->hdr.dwFlags & INTERNET_FLAG_NO_CACHE_WRITE)
2409 b = FALSE;
2411 if(b) {
2412 int header_idx;
2414 EnterCriticalSection( &req->headers_section );
2416 header_idx = HTTP_GetCustomHeaderIndex(req, szCache_Control, 0, FALSE);
2417 if(header_idx != -1) {
2418 WCHAR *ptr;
2420 for(ptr=req->custHeaders[header_idx].lpszValue; *ptr; ) {
2421 WCHAR *end;
2423 while(*ptr==' ' || *ptr=='\t')
2424 ptr++;
2426 end = strchrW(ptr, ',');
2427 if(!end)
2428 end = ptr + strlenW(ptr);
2430 if(!strncmpiW(ptr, no_cacheW, sizeof(no_cacheW)/sizeof(*no_cacheW)-1)
2431 || !strncmpiW(ptr, no_storeW, sizeof(no_storeW)/sizeof(*no_storeW)-1)) {
2432 b = FALSE;
2433 break;
2436 ptr = end;
2437 if(*ptr == ',')
2438 ptr++;
2442 LeaveCriticalSection( &req->headers_section );
2445 if(!b) {
2446 if(!(req->hdr.dwFlags & INTERNET_FLAG_NEED_FILE))
2447 return;
2449 FIXME("INTERNET_FLAG_NEED_FILE is not supported correctly\n");
2452 url = compose_request_url(req);
2453 if(!url) {
2454 WARN("Could not get URL\n");
2455 return;
2458 b = CreateUrlCacheEntryW(url, req->contentLength == ~0u ? 0 : req->contentLength, NULL, file_name, 0);
2459 if(!b) {
2460 WARN("Could not create cache entry: %08x\n", GetLastError());
2461 return;
2464 create_req_file(file_name, &req->req_file);
2465 req->req_file->url = url;
2467 req->hCacheFile = CreateFileW(file_name, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
2468 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2469 if(req->hCacheFile == INVALID_HANDLE_VALUE) {
2470 WARN("Could not create file: %u\n", GetLastError());
2471 req->hCacheFile = NULL;
2472 return;
2475 if(req->read_size) {
2476 DWORD written;
2478 b = WriteFile(req->hCacheFile, req->read_buf+req->read_pos, req->read_size, &written, NULL);
2479 if(!b)
2480 FIXME("WriteFile failed: %u\n", GetLastError());
2482 if(req->data_stream->vtbl->end_of_data(req->data_stream, req))
2483 commit_cache_entry(req);
2487 /* read some more data into the read buffer (the read section must be held) */
2488 static DWORD read_more_data( http_request_t *req, int maxlen )
2490 DWORD res;
2491 int len;
2493 if (req->read_pos)
2495 /* move existing data to the start of the buffer */
2496 if(req->read_size)
2497 memmove( req->read_buf, req->read_buf + req->read_pos, req->read_size );
2498 req->read_pos = 0;
2501 if (maxlen == -1) maxlen = sizeof(req->read_buf);
2503 res = NETCON_recv( req->netconn, req->read_buf + req->read_size,
2504 maxlen - req->read_size, TRUE, &len );
2505 if(res == ERROR_SUCCESS)
2506 req->read_size += len;
2508 return res;
2511 /* remove some amount of data from the read buffer (the read section must be held) */
2512 static void remove_data( http_request_t *req, int count )
2514 if (!(req->read_size -= count)) req->read_pos = 0;
2515 else req->read_pos += count;
2518 static DWORD read_line( http_request_t *req, LPSTR buffer, DWORD *len )
2520 int count, bytes_read, pos = 0;
2521 DWORD res;
2523 EnterCriticalSection( &req->read_section );
2524 for (;;)
2526 BYTE *eol = memchr( req->read_buf + req->read_pos, '\n', req->read_size );
2528 if (eol)
2530 count = eol - (req->read_buf + req->read_pos);
2531 bytes_read = count + 1;
2533 else count = bytes_read = req->read_size;
2535 count = min( count, *len - pos );
2536 memcpy( buffer + pos, req->read_buf + req->read_pos, count );
2537 pos += count;
2538 remove_data( req, bytes_read );
2539 if (eol) break;
2541 if ((res = read_more_data( req, -1 )))
2543 WARN( "read failed %u\n", res );
2544 LeaveCriticalSection( &req->read_section );
2545 return res;
2547 if (!req->read_size)
2549 *len = 0;
2550 TRACE( "returning empty string\n" );
2551 LeaveCriticalSection( &req->read_section );
2552 return ERROR_SUCCESS;
2555 LeaveCriticalSection( &req->read_section );
2557 if (pos < *len)
2559 if (pos && buffer[pos - 1] == '\r') pos--;
2560 *len = pos + 1;
2562 buffer[*len - 1] = 0;
2563 TRACE( "returning %s\n", debugstr_a(buffer));
2564 return ERROR_SUCCESS;
2567 /* check if we have reached the end of the data to read (the read section must be held) */
2568 static BOOL end_of_read_data( http_request_t *req )
2570 return !req->read_size && req->data_stream->vtbl->end_of_data(req->data_stream, req);
2573 static DWORD read_http_stream(http_request_t *req, BYTE *buf, DWORD size, DWORD *read, blocking_mode_t blocking_mode)
2575 DWORD res;
2577 res = req->data_stream->vtbl->read(req->data_stream, req, buf, size, read, blocking_mode);
2578 assert(*read <= size);
2580 if(req->hCacheFile) {
2581 if(*read) {
2582 BOOL bres;
2583 DWORD written;
2585 bres = WriteFile(req->hCacheFile, buf, *read, &written, NULL);
2586 if(!bres)
2587 FIXME("WriteFile failed: %u\n", GetLastError());
2590 if(req->data_stream->vtbl->end_of_data(req->data_stream, req))
2591 commit_cache_entry(req);
2594 return res;
2597 /* fetch some more data into the read buffer (the read section must be held) */
2598 static DWORD refill_read_buffer(http_request_t *req, blocking_mode_t blocking_mode, DWORD *read_bytes)
2600 DWORD res, read=0;
2602 if(req->read_size == sizeof(req->read_buf))
2603 return ERROR_SUCCESS;
2605 if(req->read_pos) {
2606 if(req->read_size)
2607 memmove(req->read_buf, req->read_buf+req->read_pos, req->read_size);
2608 req->read_pos = 0;
2611 res = read_http_stream(req, req->read_buf+req->read_size, sizeof(req->read_buf) - req->read_size,
2612 &read, blocking_mode);
2613 req->read_size += read;
2615 TRACE("read %u bytes, read_size %u\n", read, req->read_size);
2616 if(read_bytes)
2617 *read_bytes = read;
2618 return res;
2621 /* return the size of data available to be read immediately (the read section must be held) */
2622 static DWORD get_avail_data( http_request_t *req )
2624 DWORD avail = req->read_size;
2627 * Different Windows versions have different limits of returned data, but all
2628 * of them return no more than centrain amount. We use READ_BUFFER_SIZE as a limit.
2630 if(avail < READ_BUFFER_SIZE)
2631 avail += req->data_stream->vtbl->get_avail_data(req->data_stream, req);
2633 return min(avail, READ_BUFFER_SIZE);
2636 static DWORD netconn_get_avail_data(data_stream_t *stream, http_request_t *req)
2638 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2639 DWORD avail = 0;
2641 if(is_valid_netconn(req->netconn))
2642 NETCON_query_data_available(req->netconn, &avail);
2643 return netconn_stream->content_length == ~0u
2644 ? avail
2645 : min(avail, netconn_stream->content_length-netconn_stream->content_read);
2648 static BOOL netconn_end_of_data(data_stream_t *stream, http_request_t *req)
2650 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2651 return netconn_stream->content_read == netconn_stream->content_length || !is_valid_netconn(req->netconn);
2654 static DWORD netconn_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
2655 DWORD *read, blocking_mode_t blocking_mode)
2657 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2658 DWORD res = ERROR_SUCCESS;
2659 int len = 0, ret = 0;
2661 size = min(size, netconn_stream->content_length-netconn_stream->content_read);
2663 if(size && is_valid_netconn(req->netconn)) {
2664 while((res = NETCON_recv(req->netconn, buf+ret, size-ret, blocking_mode != BLOCKING_DISALLOW, &len)) == ERROR_SUCCESS) {
2665 if(!len) {
2666 netconn_stream->content_length = netconn_stream->content_read;
2667 break;
2669 ret += len;
2670 netconn_stream->content_read += len;
2671 if(blocking_mode != BLOCKING_WAITALL || size == ret)
2672 break;
2675 if(ret || (blocking_mode == BLOCKING_DISALLOW && res == WSAEWOULDBLOCK))
2676 res = ERROR_SUCCESS;
2679 TRACE("read %u bytes\n", ret);
2680 *read = ret;
2681 return res;
2684 static BOOL netconn_drain_content(data_stream_t *stream, http_request_t *req)
2686 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2687 BYTE buf[1024];
2688 int len;
2690 if(netconn_end_of_data(stream, req))
2691 return TRUE;
2693 do {
2694 if(NETCON_recv(req->netconn, buf, sizeof(buf), FALSE, &len) != ERROR_SUCCESS)
2695 return FALSE;
2697 netconn_stream->content_read += len;
2698 }while(netconn_stream->content_read < netconn_stream->content_length);
2700 return TRUE;
2703 static void netconn_destroy(data_stream_t *stream)
2707 static const data_stream_vtbl_t netconn_stream_vtbl = {
2708 netconn_get_avail_data,
2709 netconn_end_of_data,
2710 netconn_read,
2711 netconn_drain_content,
2712 netconn_destroy
2715 static char next_chunked_data_char(chunked_stream_t *stream)
2717 assert(stream->buf_size);
2719 stream->buf_size--;
2720 return stream->buf[stream->buf_pos++];
2723 static BOOL chunked_end_of_data(data_stream_t *stream, http_request_t *req)
2725 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2726 return chunked_stream->state == CHUNKED_STREAM_STATE_END_OF_STREAM;
2729 static DWORD chunked_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
2730 DWORD *read, blocking_mode_t blocking_mode)
2732 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2733 DWORD ret_read = 0, res = ERROR_SUCCESS;
2734 BOOL continue_read = TRUE;
2735 int read_bytes;
2736 char ch;
2738 do {
2739 TRACE("state %d\n", chunked_stream->state);
2741 /* Ensure that we have data in the buffer for states that need it. */
2742 if(!chunked_stream->buf_size) {
2743 switch(chunked_stream->state) {
2744 case CHUNKED_STREAM_STATE_READING_CHUNK_SIZE:
2745 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE:
2746 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA:
2747 case CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END:
2748 chunked_stream->buf_pos = 0;
2749 res = NETCON_recv(req->netconn, chunked_stream->buf, sizeof(chunked_stream->buf), blocking_mode != BLOCKING_DISALLOW, &read_bytes);
2750 if(res == ERROR_SUCCESS && read_bytes) {
2751 chunked_stream->buf_size += read_bytes;
2752 }else if(res == WSAEWOULDBLOCK) {
2753 continue_read = FALSE;
2754 continue;
2755 }else {
2756 chunked_stream->state = CHUNKED_STREAM_STATE_END_OF_STREAM;
2758 break;
2759 default:
2760 break;
2764 switch(chunked_stream->state) {
2765 case CHUNKED_STREAM_STATE_READING_CHUNK_SIZE:
2766 ch = next_chunked_data_char(chunked_stream);
2768 if(ch >= '0' && ch <= '9') {
2769 chunked_stream->chunk_size = chunked_stream->chunk_size * 16 + ch - '0';
2770 }else if(ch >= 'a' && ch <= 'f') {
2771 chunked_stream->chunk_size = chunked_stream->chunk_size * 16 + ch - 'a' + 10;
2772 }else if (ch >= 'A' && ch <= 'F') {
2773 chunked_stream->chunk_size = chunked_stream->chunk_size * 16 + ch - 'A' + 10;
2774 }else if (ch == ';' || ch == '\r' || ch == '\n') {
2775 TRACE("reading %u byte chunk\n", chunked_stream->chunk_size);
2776 chunked_stream->buf_size++;
2777 chunked_stream->buf_pos--;
2778 if(req->contentLength == ~0u) req->contentLength = chunked_stream->chunk_size;
2779 else req->contentLength += chunked_stream->chunk_size;
2780 chunked_stream->state = CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE;
2782 break;
2784 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE:
2785 ch = next_chunked_data_char(chunked_stream);
2786 if(ch == '\n')
2787 chunked_stream->state = chunked_stream->chunk_size
2788 ? CHUNKED_STREAM_STATE_READING_CHUNK
2789 : CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END;
2790 else if(ch != '\r')
2791 WARN("unexpected char '%c'\n", ch);
2792 break;
2794 case CHUNKED_STREAM_STATE_READING_CHUNK:
2795 assert(chunked_stream->chunk_size);
2796 if(!size) {
2797 continue_read = FALSE;
2798 break;
2800 read_bytes = min(size, chunked_stream->chunk_size);
2802 if(chunked_stream->buf_size) {
2803 if(read_bytes > chunked_stream->buf_size)
2804 read_bytes = chunked_stream->buf_size;
2806 memcpy(buf+ret_read, chunked_stream->buf+chunked_stream->buf_pos, read_bytes);
2807 chunked_stream->buf_pos += read_bytes;
2808 chunked_stream->buf_size -= read_bytes;
2809 }else {
2810 res = NETCON_recv(req->netconn, (char*)buf+ret_read, read_bytes,
2811 blocking_mode != BLOCKING_DISALLOW, (int*)&read_bytes);
2812 if(res != ERROR_SUCCESS) {
2813 continue_read = FALSE;
2814 break;
2817 if(!read_bytes) {
2818 chunked_stream->state = CHUNKED_STREAM_STATE_END_OF_STREAM;
2819 continue;
2823 chunked_stream->chunk_size -= read_bytes;
2824 size -= read_bytes;
2825 ret_read += read_bytes;
2826 if(!chunked_stream->chunk_size)
2827 chunked_stream->state = CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA;
2828 if(blocking_mode == BLOCKING_ALLOW)
2829 blocking_mode = BLOCKING_DISALLOW;
2830 break;
2832 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA:
2833 ch = next_chunked_data_char(chunked_stream);
2834 if(ch == '\n')
2835 chunked_stream->state = CHUNKED_STREAM_STATE_READING_CHUNK_SIZE;
2836 else if(ch != '\r')
2837 WARN("unexpected char '%c'\n", ch);
2838 break;
2840 case CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END:
2841 ch = next_chunked_data_char(chunked_stream);
2842 if(ch == '\n')
2843 chunked_stream->state = CHUNKED_STREAM_STATE_END_OF_STREAM;
2844 else if(ch != '\r')
2845 WARN("unexpected char '%c'\n", ch);
2846 break;
2848 case CHUNKED_STREAM_STATE_END_OF_STREAM:
2849 continue_read = FALSE;
2850 break;
2852 } while(continue_read);
2854 if(ret_read)
2855 res = ERROR_SUCCESS;
2856 if(res != ERROR_SUCCESS && res != WSAEWOULDBLOCK)
2857 return res;
2859 TRACE("read %d bytes\n", ret_read);
2860 *read = ret_read;
2861 return ERROR_SUCCESS;
2864 static DWORD chunked_get_avail_data(data_stream_t *stream, http_request_t *req)
2866 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2867 DWORD avail = 0;
2869 if(chunked_stream->state != CHUNKED_STREAM_STATE_READING_CHUNK) {
2870 DWORD res, read;
2872 /* try to process to the next chunk */
2873 res = chunked_read(stream, req, NULL, 0, &read, BLOCKING_DISALLOW);
2874 if(res != ERROR_SUCCESS || chunked_stream->state != CHUNKED_STREAM_STATE_READING_CHUNK)
2875 return 0;
2878 if(is_valid_netconn(req->netconn) && chunked_stream->buf_size < chunked_stream->chunk_size)
2879 NETCON_query_data_available(req->netconn, &avail);
2881 return min(avail + chunked_stream->buf_size, chunked_stream->chunk_size);
2884 static BOOL chunked_drain_content(data_stream_t *stream, http_request_t *req)
2886 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2887 return chunked_stream->state == CHUNKED_STREAM_STATE_END_OF_STREAM;
2890 static void chunked_destroy(data_stream_t *stream)
2892 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2893 heap_free(chunked_stream);
2896 static const data_stream_vtbl_t chunked_stream_vtbl = {
2897 chunked_get_avail_data,
2898 chunked_end_of_data,
2899 chunked_read,
2900 chunked_drain_content,
2901 chunked_destroy
2904 /* set the request content length based on the headers */
2905 static DWORD set_content_length(http_request_t *request)
2907 static const WCHAR szChunked[] = {'c','h','u','n','k','e','d',0};
2908 static const WCHAR headW[] = {'H','E','A','D',0};
2909 WCHAR encoding[20];
2910 DWORD size;
2912 if(request->status_code == HTTP_STATUS_NO_CONTENT || !strcmpW(request->verb, headW)) {
2913 request->contentLength = request->netconn_stream.content_length = 0;
2914 return ERROR_SUCCESS;
2917 size = sizeof(request->contentLength);
2918 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
2919 &request->contentLength, &size, NULL) != ERROR_SUCCESS)
2920 request->contentLength = ~0u;
2921 request->netconn_stream.content_length = request->contentLength;
2922 request->netconn_stream.content_read = request->read_size;
2924 size = sizeof(encoding);
2925 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_TRANSFER_ENCODING, encoding, &size, NULL) == ERROR_SUCCESS &&
2926 !strcmpiW(encoding, szChunked))
2928 chunked_stream_t *chunked_stream;
2930 chunked_stream = heap_alloc(sizeof(*chunked_stream));
2931 if(!chunked_stream)
2932 return ERROR_OUTOFMEMORY;
2934 chunked_stream->data_stream.vtbl = &chunked_stream_vtbl;
2935 chunked_stream->buf_size = chunked_stream->buf_pos = 0;
2936 chunked_stream->chunk_size = 0;
2937 chunked_stream->state = CHUNKED_STREAM_STATE_READING_CHUNK_SIZE;
2939 if(request->read_size) {
2940 memcpy(chunked_stream->buf, request->read_buf+request->read_pos, request->read_size);
2941 chunked_stream->buf_size = request->read_size;
2942 request->read_size = request->read_pos = 0;
2945 request->data_stream = &chunked_stream->data_stream;
2946 request->contentLength = ~0u;
2949 if(request->decoding) {
2950 int encoding_idx;
2952 static const WCHAR deflateW[] = {'d','e','f','l','a','t','e',0};
2953 static const WCHAR gzipW[] = {'g','z','i','p',0};
2955 EnterCriticalSection( &request->headers_section );
2957 encoding_idx = HTTP_GetCustomHeaderIndex(request, szContent_Encoding, 0, FALSE);
2958 if(encoding_idx != -1) {
2959 if(!strcmpiW(request->custHeaders[encoding_idx].lpszValue, gzipW)) {
2960 HTTP_DeleteCustomHeader(request, encoding_idx);
2961 LeaveCriticalSection( &request->headers_section );
2962 return init_gzip_stream(request, TRUE);
2964 if(!strcmpiW(request->custHeaders[encoding_idx].lpszValue, deflateW)) {
2965 HTTP_DeleteCustomHeader(request, encoding_idx);
2966 LeaveCriticalSection( &request->headers_section );
2967 return init_gzip_stream(request, FALSE);
2971 LeaveCriticalSection( &request->headers_section );
2974 return ERROR_SUCCESS;
2977 static void send_request_complete(http_request_t *req, DWORD_PTR result, DWORD error)
2979 INTERNET_ASYNC_RESULT iar;
2981 iar.dwResult = result;
2982 iar.dwError = error;
2984 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2985 sizeof(INTERNET_ASYNC_RESULT));
2988 static void HTTP_ReceiveRequestData(http_request_t *req, BOOL first_notif, DWORD *ret_size)
2990 DWORD res, read = 0, avail = 0;
2991 blocking_mode_t mode;
2993 TRACE("%p\n", req);
2995 EnterCriticalSection( &req->read_section );
2997 mode = first_notif && req->read_size ? BLOCKING_DISALLOW : BLOCKING_ALLOW;
2998 res = refill_read_buffer(req, mode, &read);
2999 if(res == ERROR_SUCCESS)
3000 avail = get_avail_data(req);
3002 LeaveCriticalSection( &req->read_section );
3004 if(res != ERROR_SUCCESS || (mode != BLOCKING_DISALLOW && !read)) {
3005 WARN("res %u read %u, closing connection\n", res, read);
3006 http_release_netconn(req, FALSE);
3009 if(res != ERROR_SUCCESS) {
3010 send_request_complete(req, 0, res);
3011 return;
3014 if(ret_size)
3015 *ret_size = avail;
3016 if(first_notif)
3017 avail = 0;
3019 send_request_complete(req, req->session->hdr.dwInternalFlags & INET_OPENURL ? (DWORD_PTR)req->hdr.hInternet : 1, avail);
3022 /* read data from the http connection (the read section must be held) */
3023 static DWORD HTTPREQ_Read(http_request_t *req, void *buffer, DWORD size, DWORD *read)
3025 DWORD current_read = 0, ret_read = 0;
3026 blocking_mode_t blocking_mode;
3027 DWORD res = ERROR_SUCCESS;
3029 blocking_mode = req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC ? BLOCKING_ALLOW : BLOCKING_WAITALL;
3031 EnterCriticalSection( &req->read_section );
3033 if(req->read_size) {
3034 ret_read = min(size, req->read_size);
3035 memcpy(buffer, req->read_buf+req->read_pos, ret_read);
3036 req->read_size -= ret_read;
3037 req->read_pos += ret_read;
3038 if(blocking_mode == BLOCKING_ALLOW)
3039 blocking_mode = BLOCKING_DISALLOW;
3042 if(ret_read < size) {
3043 res = read_http_stream(req, (BYTE*)buffer+ret_read, size-ret_read, &current_read, blocking_mode);
3044 ret_read += current_read;
3047 LeaveCriticalSection( &req->read_section );
3049 *read = ret_read;
3050 TRACE( "retrieved %u bytes (%u)\n", ret_read, req->contentLength );
3052 if(size && !ret_read)
3053 http_release_netconn(req, res == ERROR_SUCCESS);
3055 return res;
3058 static BOOL drain_content(http_request_t *req, BOOL blocking)
3060 BOOL ret;
3062 if(!is_valid_netconn(req->netconn) || req->contentLength == -1)
3063 return FALSE;
3065 if(!strcmpW(req->verb, szHEAD))
3066 return TRUE;
3068 if(!blocking)
3069 return req->data_stream->vtbl->drain_content(req->data_stream, req);
3071 EnterCriticalSection( &req->read_section );
3073 while(1) {
3074 DWORD bytes_read, res;
3075 BYTE buf[4096];
3077 res = HTTPREQ_Read(req, buf, sizeof(buf), &bytes_read);
3078 if(res != ERROR_SUCCESS) {
3079 ret = FALSE;
3080 break;
3082 if(!bytes_read) {
3083 ret = TRUE;
3084 break;
3088 LeaveCriticalSection( &req->read_section );
3089 return ret;
3092 typedef struct {
3093 task_header_t hdr;
3094 void *buf;
3095 DWORD size;
3096 DWORD *ret_read;
3097 } read_file_ex_task_t;
3099 static void AsyncReadFileExProc(task_header_t *hdr)
3101 read_file_ex_task_t *task = (read_file_ex_task_t*)hdr;
3102 http_request_t *req = (http_request_t*)task->hdr.hdr;
3103 DWORD res = ERROR_SUCCESS, read = 0, buffered = 0;
3105 TRACE("%p\n", req);
3107 if(task->ret_read)
3108 res = HTTPREQ_Read(req, task->buf, task->size, &read);
3109 if(res == ERROR_SUCCESS)
3110 res = refill_read_buffer(req, task->ret_read ? BLOCKING_DISALLOW : BLOCKING_ALLOW, &buffered);
3111 if (res == ERROR_SUCCESS)
3113 if(task->ret_read)
3114 *task->ret_read = read;
3115 read += buffered;
3116 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
3117 &read, sizeof(read));
3120 send_request_complete(req, res == ERROR_SUCCESS, res);
3123 static DWORD HTTPREQ_ReadFileEx(object_header_t *hdr, void *buf, DWORD size, DWORD *ret_read,
3124 DWORD flags, DWORD_PTR context)
3127 http_request_t *req = (http_request_t*)hdr;
3128 DWORD res, read, cread, error = ERROR_SUCCESS;
3130 TRACE("(%p %p %u %x)\n", req, buf, size, flags);
3132 if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
3133 FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
3135 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3137 if (req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3139 read_file_ex_task_t *task;
3141 if (TryEnterCriticalSection( &req->read_section ))
3143 if (get_avail_data(req) || end_of_read_data(req))
3145 res = HTTPREQ_Read(req, buf, size, &read);
3146 LeaveCriticalSection( &req->read_section );
3147 goto done;
3149 LeaveCriticalSection( &req->read_section );
3152 task = alloc_async_task(&req->hdr, AsyncReadFileExProc, sizeof(*task));
3153 task->buf = buf;
3154 task->size = size;
3155 task->ret_read = (flags & IRF_NO_WAIT) ? NULL : ret_read;
3157 INTERNET_AsyncCall(&task->hdr);
3159 return ERROR_IO_PENDING;
3162 read = 0;
3164 EnterCriticalSection( &req->read_section );
3165 if(hdr->dwError == ERROR_SUCCESS)
3166 hdr->dwError = INTERNET_HANDLE_IN_USE;
3167 else if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3168 hdr->dwError = ERROR_INTERNET_INTERNAL_ERROR;
3170 while(1) {
3171 res = HTTPREQ_Read(req, (char*)buf+read, size-read, &cread);
3172 if(res != ERROR_SUCCESS)
3173 break;
3175 read += cread;
3176 if(read == size || end_of_read_data(req))
3177 break;
3179 LeaveCriticalSection( &req->read_section );
3181 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
3182 &cread, sizeof(cread));
3183 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
3184 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3186 EnterCriticalSection( &req->read_section );
3189 if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3190 hdr->dwError = ERROR_SUCCESS;
3191 else
3192 error = hdr->dwError;
3194 LeaveCriticalSection( &req->read_section );
3196 done:
3197 *ret_read = read;
3198 if (res == ERROR_SUCCESS) {
3199 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
3200 &read, sizeof(read));
3203 return res==ERROR_SUCCESS ? error : res;
3206 static DWORD HTTPREQ_WriteFile(object_header_t *hdr, const void *buffer, DWORD size, DWORD *written)
3208 DWORD res;
3209 http_request_t *request = (http_request_t*)hdr;
3211 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
3213 *written = 0;
3214 res = NETCON_send(request->netconn, buffer, size, 0, (LPINT)written);
3215 if (res == ERROR_SUCCESS)
3216 request->bytesWritten += *written;
3218 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REQUEST_SENT, written, sizeof(DWORD));
3219 return res;
3222 static DWORD HTTPREQ_ReadFile(object_header_t *hdr, void *buffer, DWORD size, DWORD *read)
3224 http_request_t *req = (http_request_t*)hdr;
3225 DWORD res;
3227 if (req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3229 read_file_ex_task_t *task;
3231 if (TryEnterCriticalSection( &req->read_section ))
3233 if (get_avail_data(req) || end_of_read_data(req))
3235 res = HTTPREQ_Read(req, buffer, size, read);
3236 LeaveCriticalSection( &req->read_section );
3237 return res;
3239 LeaveCriticalSection( &req->read_section );
3242 task = alloc_async_task(&req->hdr, AsyncReadFileExProc, sizeof(*task));
3243 task->buf = buffer;
3244 task->size = size;
3245 task->ret_read = read;
3247 *read = 0;
3248 INTERNET_AsyncCall(&task->hdr);
3250 return ERROR_IO_PENDING;
3253 EnterCriticalSection( &req->read_section );
3254 if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3255 hdr->dwError = ERROR_INTERNET_INTERNAL_ERROR;
3257 res = HTTPREQ_Read(req, buffer, size, read);
3258 if(res == ERROR_SUCCESS)
3259 res = hdr->dwError;
3260 LeaveCriticalSection( &req->read_section );
3262 return res;
3265 typedef struct {
3266 task_header_t hdr;
3267 DWORD *ret_size;
3268 } http_data_available_task_t;
3270 static void AsyncQueryDataAvailableProc(task_header_t *hdr)
3272 http_data_available_task_t *task = (http_data_available_task_t*)hdr;
3274 HTTP_ReceiveRequestData((http_request_t*)task->hdr.hdr, FALSE, task->ret_size);
3277 static DWORD HTTPREQ_QueryDataAvailable(object_header_t *hdr, DWORD *available, DWORD flags, DWORD_PTR ctx)
3279 http_request_t *req = (http_request_t*)hdr;
3281 TRACE("(%p %p %x %lx)\n", req, available, flags, ctx);
3283 if (req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3285 http_data_available_task_t *task;
3287 /* never wait, if we can't enter the section we queue an async request right away */
3288 if (TryEnterCriticalSection( &req->read_section ))
3290 refill_read_buffer(req, BLOCKING_DISALLOW, NULL);
3291 if ((*available = get_avail_data( req ))) goto done;
3292 if (end_of_read_data( req )) goto done;
3293 LeaveCriticalSection( &req->read_section );
3296 task = alloc_async_task(&req->hdr, AsyncQueryDataAvailableProc, sizeof(*task));
3297 task->ret_size = available;
3298 INTERNET_AsyncCall(&task->hdr);
3299 return ERROR_IO_PENDING;
3302 EnterCriticalSection( &req->read_section );
3304 if (!(*available = get_avail_data( req )) && !end_of_read_data( req ))
3306 refill_read_buffer( req, BLOCKING_ALLOW, NULL );
3307 *available = get_avail_data( req );
3310 done:
3311 LeaveCriticalSection( &req->read_section );
3313 TRACE( "returning %u\n", *available );
3314 return ERROR_SUCCESS;
3317 static DWORD HTTPREQ_LockRequestFile(object_header_t *hdr, req_file_t **ret)
3319 http_request_t *req = (http_request_t*)hdr;
3321 TRACE("(%p)\n", req);
3323 if(!req->req_file) {
3324 WARN("No cache file name available\n");
3325 return ERROR_FILE_NOT_FOUND;
3328 *ret = req_file_addref(req->req_file);
3329 return ERROR_SUCCESS;
3332 static const object_vtbl_t HTTPREQVtbl = {
3333 HTTPREQ_Destroy,
3334 HTTPREQ_CloseConnection,
3335 HTTPREQ_QueryOption,
3336 HTTPREQ_SetOption,
3337 HTTPREQ_ReadFile,
3338 HTTPREQ_ReadFileEx,
3339 HTTPREQ_WriteFile,
3340 HTTPREQ_QueryDataAvailable,
3341 NULL,
3342 HTTPREQ_LockRequestFile
3345 /***********************************************************************
3346 * HTTP_HttpOpenRequestW (internal)
3348 * Open a HTTP request handle
3350 * RETURNS
3351 * HINTERNET a HTTP request handle on success
3352 * NULL on failure
3355 static DWORD HTTP_HttpOpenRequestW(http_session_t *session,
3356 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
3357 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
3358 DWORD dwFlags, DWORD_PTR dwContext, HINTERNET *ret)
3360 appinfo_t *hIC = session->appInfo;
3361 http_request_t *request;
3362 DWORD len;
3364 TRACE("-->\n");
3366 request = alloc_object(&session->hdr, &HTTPREQVtbl, sizeof(http_request_t));
3367 if(!request)
3368 return ERROR_OUTOFMEMORY;
3370 request->hdr.htype = WH_HHTTPREQ;
3371 request->hdr.dwFlags = dwFlags;
3372 request->hdr.dwContext = dwContext;
3373 request->contentLength = ~0u;
3375 request->netconn_stream.data_stream.vtbl = &netconn_stream_vtbl;
3376 request->data_stream = &request->netconn_stream.data_stream;
3377 request->connect_timeout = session->connect_timeout;
3378 request->send_timeout = session->send_timeout;
3379 request->receive_timeout = session->receive_timeout;
3381 InitializeCriticalSection( &request->headers_section );
3382 request->headers_section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": http_request_t.headers_section");
3384 InitializeCriticalSection( &request->read_section );
3385 request->read_section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": http_request_t.read_section");
3387 WININET_AddRef( &session->hdr );
3388 request->session = session;
3389 list_add_head( &session->hdr.children, &request->hdr.entry );
3391 request->server = get_server(substrz(session->hostName), session->hostPort, (dwFlags & INTERNET_FLAG_SECURE) != 0, TRUE);
3392 if(!request->server) {
3393 WININET_Release(&request->hdr);
3394 return ERROR_OUTOFMEMORY;
3397 if (dwFlags & INTERNET_FLAG_IGNORE_CERT_CN_INVALID)
3398 request->security_flags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
3399 if (dwFlags & INTERNET_FLAG_IGNORE_CERT_DATE_INVALID)
3400 request->security_flags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
3402 if (lpszObjectName && *lpszObjectName) {
3403 HRESULT rc;
3404 WCHAR dummy;
3406 len = 1;
3407 rc = UrlCanonicalizeW(lpszObjectName, &dummy, &len, URL_ESCAPE_SPACES_ONLY);
3408 if (rc != E_POINTER)
3409 len = strlenW(lpszObjectName)+1;
3410 request->path = heap_alloc(len*sizeof(WCHAR));
3411 rc = UrlCanonicalizeW(lpszObjectName, request->path, &len,
3412 URL_ESCAPE_SPACES_ONLY);
3413 if (rc != S_OK)
3415 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc);
3416 strcpyW(request->path,lpszObjectName);
3418 }else {
3419 static const WCHAR slashW[] = {'/',0};
3421 request->path = heap_strdupW(slashW);
3424 if (lpszReferrer && *lpszReferrer)
3425 HTTP_ProcessHeader(request, HTTP_REFERER, lpszReferrer, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
3427 if (lpszAcceptTypes)
3429 int i;
3430 for (i = 0; lpszAcceptTypes[i]; i++)
3432 if (!*lpszAcceptTypes[i]) continue;
3433 HTTP_ProcessHeader(request, HTTP_ACCEPT, lpszAcceptTypes[i],
3434 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA |
3435 HTTP_ADDHDR_FLAG_REQ |
3436 (i == 0 ? (HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_ADD) : 0));
3440 request->verb = heap_strdupW(lpszVerb && *lpszVerb ? lpszVerb : szGET);
3441 request->version = heap_strdupW(lpszVersion && *lpszVersion ? lpszVersion : g_szHttp1_1);
3443 if (hIC->proxy && hIC->proxy[0] && !HTTP_ShouldBypassProxy(hIC, session->hostName))
3444 HTTP_DealWithProxy( hIC, session, request );
3446 INTERNET_SendCallback(&session->hdr, dwContext,
3447 INTERNET_STATUS_HANDLE_CREATED, &request->hdr.hInternet,
3448 sizeof(HINTERNET));
3450 TRACE("<-- (%p)\n", request);
3452 *ret = request->hdr.hInternet;
3453 return ERROR_SUCCESS;
3456 /***********************************************************************
3457 * HttpOpenRequestW (WININET.@)
3459 * Open a HTTP request handle
3461 * RETURNS
3462 * HINTERNET a HTTP request handle on success
3463 * NULL on failure
3466 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
3467 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
3468 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
3469 DWORD dwFlags, DWORD_PTR dwContext)
3471 http_session_t *session;
3472 HINTERNET handle = NULL;
3473 DWORD res;
3475 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
3476 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
3477 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
3478 dwFlags, dwContext);
3479 if(lpszAcceptTypes!=NULL)
3481 int i;
3482 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
3483 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
3486 session = (http_session_t*) get_handle_object( hHttpSession );
3487 if (NULL == session || session->hdr.htype != WH_HHTTPSESSION)
3489 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3490 goto lend;
3494 * My tests seem to show that the windows version does not
3495 * become asynchronous until after this point. And anyhow
3496 * if this call was asynchronous then how would you get the
3497 * necessary HINTERNET pointer returned by this function.
3500 res = HTTP_HttpOpenRequestW(session, lpszVerb, lpszObjectName,
3501 lpszVersion, lpszReferrer, lpszAcceptTypes,
3502 dwFlags, dwContext, &handle);
3503 lend:
3504 if( session )
3505 WININET_Release( &session->hdr );
3506 TRACE("returning %p\n", handle);
3507 if(res != ERROR_SUCCESS)
3508 SetLastError(res);
3509 return handle;
3512 static const LPCWSTR header_lookup[] = {
3513 szMime_Version, /* HTTP_QUERY_MIME_VERSION = 0 */
3514 szContent_Type, /* HTTP_QUERY_CONTENT_TYPE = 1 */
3515 szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
3516 szContent_ID, /* HTTP_QUERY_CONTENT_ID = 3 */
3517 NULL, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
3518 szContent_Length, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
3519 szContent_Language, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
3520 szAllow, /* HTTP_QUERY_ALLOW = 7 */
3521 szPublic, /* HTTP_QUERY_PUBLIC = 8 */
3522 szDate, /* HTTP_QUERY_DATE = 9 */
3523 szExpires, /* HTTP_QUERY_EXPIRES = 10 */
3524 szLast_Modified, /* HTTP_QUERY_LAST_MODIFIED = 11 */
3525 NULL, /* HTTP_QUERY_MESSAGE_ID = 12 */
3526 szURI, /* HTTP_QUERY_URI = 13 */
3527 szFrom, /* HTTP_QUERY_DERIVED_FROM = 14 */
3528 NULL, /* HTTP_QUERY_COST = 15 */
3529 NULL, /* HTTP_QUERY_LINK = 16 */
3530 szPragma, /* HTTP_QUERY_PRAGMA = 17 */
3531 NULL, /* HTTP_QUERY_VERSION = 18 */
3532 szStatus, /* HTTP_QUERY_STATUS_CODE = 19 */
3533 NULL, /* HTTP_QUERY_STATUS_TEXT = 20 */
3534 NULL, /* HTTP_QUERY_RAW_HEADERS = 21 */
3535 NULL, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
3536 szConnection, /* HTTP_QUERY_CONNECTION = 23 */
3537 szAccept, /* HTTP_QUERY_ACCEPT = 24 */
3538 szAccept_Charset, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
3539 szAccept_Encoding, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
3540 szAccept_Language, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
3541 szAuthorization, /* HTTP_QUERY_AUTHORIZATION = 28 */
3542 szContent_Encoding, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
3543 NULL, /* HTTP_QUERY_FORWARDED = 30 */
3544 NULL, /* HTTP_QUERY_FROM = 31 */
3545 szIf_Modified_Since, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
3546 szLocation, /* HTTP_QUERY_LOCATION = 33 */
3547 NULL, /* HTTP_QUERY_ORIG_URI = 34 */
3548 szReferer, /* HTTP_QUERY_REFERER = 35 */
3549 szRetry_After, /* HTTP_QUERY_RETRY_AFTER = 36 */
3550 szServer, /* HTTP_QUERY_SERVER = 37 */
3551 NULL, /* HTTP_TITLE = 38 */
3552 szUser_Agent, /* HTTP_QUERY_USER_AGENT = 39 */
3553 szWWW_Authenticate, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
3554 szProxy_Authenticate, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
3555 szAccept_Ranges, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
3556 szSet_Cookie, /* HTTP_QUERY_SET_COOKIE = 43 */
3557 szCookie, /* HTTP_QUERY_COOKIE = 44 */
3558 NULL, /* HTTP_QUERY_REQUEST_METHOD = 45 */
3559 NULL, /* HTTP_QUERY_REFRESH = 46 */
3560 szContent_Disposition, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
3561 szAge, /* HTTP_QUERY_AGE = 48 */
3562 szCache_Control, /* HTTP_QUERY_CACHE_CONTROL = 49 */
3563 szContent_Base, /* HTTP_QUERY_CONTENT_BASE = 50 */
3564 szContent_Location, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
3565 szContent_MD5, /* HTTP_QUERY_CONTENT_MD5 = 52 */
3566 szContent_Range, /* HTTP_QUERY_CONTENT_RANGE = 53 */
3567 szETag, /* HTTP_QUERY_ETAG = 54 */
3568 hostW, /* HTTP_QUERY_HOST = 55 */
3569 szIf_Match, /* HTTP_QUERY_IF_MATCH = 56 */
3570 szIf_None_Match, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
3571 szIf_Range, /* HTTP_QUERY_IF_RANGE = 58 */
3572 szIf_Unmodified_Since, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
3573 szMax_Forwards, /* HTTP_QUERY_MAX_FORWARDS = 60 */
3574 szProxy_Authorization, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
3575 szRange, /* HTTP_QUERY_RANGE = 62 */
3576 szTransfer_Encoding, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
3577 szUpgrade, /* HTTP_QUERY_UPGRADE = 64 */
3578 szVary, /* HTTP_QUERY_VARY = 65 */
3579 szVia, /* HTTP_QUERY_VIA = 66 */
3580 szWarning, /* HTTP_QUERY_WARNING = 67 */
3581 szExpect, /* HTTP_QUERY_EXPECT = 68 */
3582 szProxy_Connection, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
3583 szUnless_Modified_Since, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
3586 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
3588 /***********************************************************************
3589 * HTTP_HttpQueryInfoW (internal)
3591 static DWORD HTTP_HttpQueryInfoW(http_request_t *request, DWORD dwInfoLevel,
3592 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
3594 LPHTTPHEADERW lphttpHdr = NULL;
3595 BOOL request_only = !!(dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS);
3596 INT requested_index = lpdwIndex ? *lpdwIndex : 0;
3597 DWORD level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
3598 INT index = -1;
3600 EnterCriticalSection( &request->headers_section );
3602 /* Find requested header structure */
3603 switch (level)
3605 case HTTP_QUERY_CUSTOM:
3606 if (!lpBuffer)
3608 LeaveCriticalSection( &request->headers_section );
3609 return ERROR_INVALID_PARAMETER;
3611 index = HTTP_GetCustomHeaderIndex(request, lpBuffer, requested_index, request_only);
3612 break;
3613 case HTTP_QUERY_RAW_HEADERS_CRLF:
3615 LPWSTR headers;
3616 DWORD len = 0;
3617 DWORD res = ERROR_INVALID_PARAMETER;
3619 if (request_only)
3620 headers = build_request_header(request, request->verb, request->path, request->version, TRUE);
3621 else
3622 headers = build_response_header(request, TRUE);
3623 if (!headers)
3625 LeaveCriticalSection( &request->headers_section );
3626 return ERROR_OUTOFMEMORY;
3629 len = strlenW(headers) * sizeof(WCHAR);
3630 if (len + sizeof(WCHAR) > *lpdwBufferLength)
3632 len += sizeof(WCHAR);
3633 res = ERROR_INSUFFICIENT_BUFFER;
3635 else if (lpBuffer)
3637 memcpy(lpBuffer, headers, len + sizeof(WCHAR));
3638 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len / sizeof(WCHAR)));
3639 res = ERROR_SUCCESS;
3641 *lpdwBufferLength = len;
3643 heap_free(headers);
3644 LeaveCriticalSection( &request->headers_section );
3645 return res;
3647 case HTTP_QUERY_RAW_HEADERS:
3649 LPWSTR headers;
3650 DWORD len;
3652 if (request_only)
3653 headers = build_request_header(request, request->verb, request->path, request->version, FALSE);
3654 else
3655 headers = build_response_header(request, FALSE);
3657 if (!headers)
3659 LeaveCriticalSection( &request->headers_section );
3660 return ERROR_OUTOFMEMORY;
3663 len = strlenW(headers) * sizeof(WCHAR);
3664 if (len > *lpdwBufferLength)
3666 *lpdwBufferLength = len;
3667 heap_free(headers);
3668 LeaveCriticalSection( &request->headers_section );
3669 return ERROR_INSUFFICIENT_BUFFER;
3672 if (lpBuffer)
3674 DWORD i;
3676 TRACE("returning data: %s\n", debugstr_wn(headers, len / sizeof(WCHAR)));
3678 for (i = 0; i < len / sizeof(WCHAR); i++)
3680 if (headers[i] == '\n')
3681 headers[i] = 0;
3683 memcpy(lpBuffer, headers, len);
3685 *lpdwBufferLength = len - sizeof(WCHAR);
3687 heap_free(headers);
3688 LeaveCriticalSection( &request->headers_section );
3689 return ERROR_SUCCESS;
3691 case HTTP_QUERY_STATUS_TEXT:
3692 if (request->statusText)
3694 DWORD len = strlenW(request->statusText);
3695 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
3697 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
3698 LeaveCriticalSection( &request->headers_section );
3699 return ERROR_INSUFFICIENT_BUFFER;
3701 if (lpBuffer)
3703 memcpy(lpBuffer, request->statusText, (len + 1) * sizeof(WCHAR));
3704 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
3706 *lpdwBufferLength = len * sizeof(WCHAR);
3707 LeaveCriticalSection( &request->headers_section );
3708 return ERROR_SUCCESS;
3710 break;
3711 case HTTP_QUERY_VERSION:
3712 if (request->version)
3714 DWORD len = strlenW(request->version);
3715 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
3717 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
3718 LeaveCriticalSection( &request->headers_section );
3719 return ERROR_INSUFFICIENT_BUFFER;
3721 if (lpBuffer)
3723 memcpy(lpBuffer, request->version, (len + 1) * sizeof(WCHAR));
3724 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
3726 *lpdwBufferLength = len * sizeof(WCHAR);
3727 LeaveCriticalSection( &request->headers_section );
3728 return ERROR_SUCCESS;
3730 break;
3731 case HTTP_QUERY_CONTENT_ENCODING:
3732 index = HTTP_GetCustomHeaderIndex(request, header_lookup[request->read_gzip ? HTTP_QUERY_CONTENT_TYPE : level],
3733 requested_index,request_only);
3734 break;
3735 case HTTP_QUERY_STATUS_CODE: {
3736 DWORD res = ERROR_SUCCESS;
3738 if(request_only)
3740 LeaveCriticalSection( &request->headers_section );
3741 return ERROR_HTTP_INVALID_QUERY_REQUEST;
3744 if(requested_index)
3745 break;
3747 if(dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) {
3748 if(*lpdwBufferLength >= sizeof(DWORD))
3749 *(DWORD*)lpBuffer = request->status_code;
3750 else
3751 res = ERROR_INSUFFICIENT_BUFFER;
3752 *lpdwBufferLength = sizeof(DWORD);
3753 }else {
3754 WCHAR buf[12];
3755 DWORD size;
3756 static const WCHAR formatW[] = {'%','u',0};
3758 size = sprintfW(buf, formatW, request->status_code) * sizeof(WCHAR);
3760 if(size <= *lpdwBufferLength) {
3761 memcpy(lpBuffer, buf, size+sizeof(WCHAR));
3762 }else {
3763 size += sizeof(WCHAR);
3764 res = ERROR_INSUFFICIENT_BUFFER;
3767 *lpdwBufferLength = size;
3769 LeaveCriticalSection( &request->headers_section );
3770 return res;
3772 default:
3773 assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
3775 if (level < LAST_TABLE_HEADER && header_lookup[level])
3776 index = HTTP_GetCustomHeaderIndex(request, header_lookup[level],
3777 requested_index,request_only);
3780 if (index >= 0)
3781 lphttpHdr = &request->custHeaders[index];
3783 /* Ensure header satisfies requested attributes */
3784 if (!lphttpHdr ||
3785 ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
3786 (~lphttpHdr->wFlags & HDR_ISREQUEST)))
3788 LeaveCriticalSection( &request->headers_section );
3789 return ERROR_HTTP_HEADER_NOT_FOUND;
3792 /* coalesce value to requested type */
3793 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER && lpBuffer)
3795 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
3796 TRACE(" returning number: %d\n", *(int *)lpBuffer);
3798 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME && lpBuffer)
3800 time_t tmpTime;
3801 struct tm tmpTM;
3802 SYSTEMTIME *STHook;
3804 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
3806 tmpTM = *gmtime(&tmpTime);
3807 STHook = (SYSTEMTIME *)lpBuffer;
3808 STHook->wDay = tmpTM.tm_mday;
3809 STHook->wHour = tmpTM.tm_hour;
3810 STHook->wMilliseconds = 0;
3811 STHook->wMinute = tmpTM.tm_min;
3812 STHook->wDayOfWeek = tmpTM.tm_wday;
3813 STHook->wMonth = tmpTM.tm_mon + 1;
3814 STHook->wSecond = tmpTM.tm_sec;
3815 STHook->wYear = 1900+tmpTM.tm_year;
3817 TRACE(" returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
3818 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
3819 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
3821 else if (lphttpHdr->lpszValue)
3823 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
3825 if (len > *lpdwBufferLength)
3827 *lpdwBufferLength = len;
3828 LeaveCriticalSection( &request->headers_section );
3829 return ERROR_INSUFFICIENT_BUFFER;
3831 if (lpBuffer)
3833 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
3834 TRACE("! returning string: %s\n", debugstr_w(lpBuffer));
3836 *lpdwBufferLength = len - sizeof(WCHAR);
3838 if (lpdwIndex) (*lpdwIndex)++;
3840 LeaveCriticalSection( &request->headers_section );
3841 return ERROR_SUCCESS;
3844 /***********************************************************************
3845 * HttpQueryInfoW (WININET.@)
3847 * Queries for information about an HTTP request
3849 * RETURNS
3850 * TRUE on success
3851 * FALSE on failure
3854 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
3855 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
3857 http_request_t *request;
3858 DWORD res;
3860 if (TRACE_ON(wininet)) {
3861 #define FE(x) { x, #x }
3862 static const wininet_flag_info query_flags[] = {
3863 FE(HTTP_QUERY_MIME_VERSION),
3864 FE(HTTP_QUERY_CONTENT_TYPE),
3865 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
3866 FE(HTTP_QUERY_CONTENT_ID),
3867 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
3868 FE(HTTP_QUERY_CONTENT_LENGTH),
3869 FE(HTTP_QUERY_CONTENT_LANGUAGE),
3870 FE(HTTP_QUERY_ALLOW),
3871 FE(HTTP_QUERY_PUBLIC),
3872 FE(HTTP_QUERY_DATE),
3873 FE(HTTP_QUERY_EXPIRES),
3874 FE(HTTP_QUERY_LAST_MODIFIED),
3875 FE(HTTP_QUERY_MESSAGE_ID),
3876 FE(HTTP_QUERY_URI),
3877 FE(HTTP_QUERY_DERIVED_FROM),
3878 FE(HTTP_QUERY_COST),
3879 FE(HTTP_QUERY_LINK),
3880 FE(HTTP_QUERY_PRAGMA),
3881 FE(HTTP_QUERY_VERSION),
3882 FE(HTTP_QUERY_STATUS_CODE),
3883 FE(HTTP_QUERY_STATUS_TEXT),
3884 FE(HTTP_QUERY_RAW_HEADERS),
3885 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
3886 FE(HTTP_QUERY_CONNECTION),
3887 FE(HTTP_QUERY_ACCEPT),
3888 FE(HTTP_QUERY_ACCEPT_CHARSET),
3889 FE(HTTP_QUERY_ACCEPT_ENCODING),
3890 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
3891 FE(HTTP_QUERY_AUTHORIZATION),
3892 FE(HTTP_QUERY_CONTENT_ENCODING),
3893 FE(HTTP_QUERY_FORWARDED),
3894 FE(HTTP_QUERY_FROM),
3895 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
3896 FE(HTTP_QUERY_LOCATION),
3897 FE(HTTP_QUERY_ORIG_URI),
3898 FE(HTTP_QUERY_REFERER),
3899 FE(HTTP_QUERY_RETRY_AFTER),
3900 FE(HTTP_QUERY_SERVER),
3901 FE(HTTP_QUERY_TITLE),
3902 FE(HTTP_QUERY_USER_AGENT),
3903 FE(HTTP_QUERY_WWW_AUTHENTICATE),
3904 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
3905 FE(HTTP_QUERY_ACCEPT_RANGES),
3906 FE(HTTP_QUERY_SET_COOKIE),
3907 FE(HTTP_QUERY_COOKIE),
3908 FE(HTTP_QUERY_REQUEST_METHOD),
3909 FE(HTTP_QUERY_REFRESH),
3910 FE(HTTP_QUERY_CONTENT_DISPOSITION),
3911 FE(HTTP_QUERY_AGE),
3912 FE(HTTP_QUERY_CACHE_CONTROL),
3913 FE(HTTP_QUERY_CONTENT_BASE),
3914 FE(HTTP_QUERY_CONTENT_LOCATION),
3915 FE(HTTP_QUERY_CONTENT_MD5),
3916 FE(HTTP_QUERY_CONTENT_RANGE),
3917 FE(HTTP_QUERY_ETAG),
3918 FE(HTTP_QUERY_HOST),
3919 FE(HTTP_QUERY_IF_MATCH),
3920 FE(HTTP_QUERY_IF_NONE_MATCH),
3921 FE(HTTP_QUERY_IF_RANGE),
3922 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
3923 FE(HTTP_QUERY_MAX_FORWARDS),
3924 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
3925 FE(HTTP_QUERY_RANGE),
3926 FE(HTTP_QUERY_TRANSFER_ENCODING),
3927 FE(HTTP_QUERY_UPGRADE),
3928 FE(HTTP_QUERY_VARY),
3929 FE(HTTP_QUERY_VIA),
3930 FE(HTTP_QUERY_WARNING),
3931 FE(HTTP_QUERY_CUSTOM)
3933 static const wininet_flag_info modifier_flags[] = {
3934 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
3935 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
3936 FE(HTTP_QUERY_FLAG_NUMBER),
3937 FE(HTTP_QUERY_FLAG_COALESCE)
3939 #undef FE
3940 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
3941 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
3942 DWORD i;
3944 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, info);
3945 TRACE(" Attribute:");
3946 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
3947 if (query_flags[i].val == info) {
3948 TRACE(" %s", query_flags[i].name);
3949 break;
3952 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
3953 TRACE(" Unknown (%08x)", info);
3956 TRACE(" Modifier:");
3957 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
3958 if (modifier_flags[i].val & info_mod) {
3959 TRACE(" %s", modifier_flags[i].name);
3960 info_mod &= ~ modifier_flags[i].val;
3964 if (info_mod) {
3965 TRACE(" Unknown (%08x)", info_mod);
3967 TRACE("\n");
3970 request = (http_request_t*) get_handle_object( hHttpRequest );
3971 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
3973 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3974 goto lend;
3977 if (lpBuffer == NULL)
3978 *lpdwBufferLength = 0;
3979 res = HTTP_HttpQueryInfoW( request, dwInfoLevel,
3980 lpBuffer, lpdwBufferLength, lpdwIndex);
3982 lend:
3983 if( request )
3984 WININET_Release( &request->hdr );
3986 TRACE("%u <--\n", res);
3987 if(res != ERROR_SUCCESS)
3988 SetLastError(res);
3989 return res == ERROR_SUCCESS;
3992 /***********************************************************************
3993 * HttpQueryInfoA (WININET.@)
3995 * Queries for information about an HTTP request
3997 * RETURNS
3998 * TRUE on success
3999 * FALSE on failure
4002 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
4003 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
4005 BOOL result;
4006 DWORD len;
4007 WCHAR* bufferW;
4009 TRACE("%p %x\n", hHttpRequest, dwInfoLevel);
4011 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
4012 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
4014 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
4015 lpdwBufferLength, lpdwIndex );
4018 if (lpBuffer)
4020 DWORD alloclen;
4021 len = (*lpdwBufferLength)*sizeof(WCHAR);
4022 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
4024 alloclen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 ) * sizeof(WCHAR);
4025 if (alloclen < len)
4026 alloclen = len;
4028 else
4029 alloclen = len;
4030 bufferW = heap_alloc(alloclen);
4031 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
4032 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
4033 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, bufferW, alloclen / sizeof(WCHAR) );
4034 } else
4036 bufferW = NULL;
4037 len = 0;
4040 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
4041 &len, lpdwIndex );
4042 if( result )
4044 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
4045 lpBuffer, *lpdwBufferLength, NULL, NULL );
4046 *lpdwBufferLength = len - 1;
4048 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
4050 else
4051 /* since the strings being returned from HttpQueryInfoW should be
4052 * only ASCII characters, it is reasonable to assume that all of
4053 * the Unicode characters can be reduced to a single byte */
4054 *lpdwBufferLength = len / sizeof(WCHAR);
4056 heap_free( bufferW );
4057 return result;
4060 static WCHAR *get_redirect_url(http_request_t *request)
4062 static WCHAR szHttp[] = {'h','t','t','p',0};
4063 static WCHAR szHttps[] = {'h','t','t','p','s',0};
4064 http_session_t *session = request->session;
4065 URL_COMPONENTSW urlComponents = { sizeof(urlComponents) };
4066 WCHAR *orig_url = NULL, *redirect_url = NULL, *combined_url = NULL;
4067 DWORD url_length = 0, res;
4068 BOOL b;
4070 url_length = 0;
4071 res = HTTP_HttpQueryInfoW(request, HTTP_QUERY_LOCATION, redirect_url, &url_length, NULL);
4072 if(res == ERROR_INSUFFICIENT_BUFFER) {
4073 redirect_url = heap_alloc(url_length);
4074 res = HTTP_HttpQueryInfoW(request, HTTP_QUERY_LOCATION, redirect_url, &url_length, NULL);
4076 if(res != ERROR_SUCCESS) {
4077 heap_free(redirect_url);
4078 return NULL;
4081 urlComponents.lpszScheme = (request->hdr.dwFlags & INTERNET_FLAG_SECURE) ? szHttps : szHttp;
4082 urlComponents.lpszHostName = request->server->name;
4083 urlComponents.nPort = request->server->port;
4084 urlComponents.lpszUserName = session->userName;
4085 urlComponents.lpszUrlPath = request->path;
4087 b = InternetCreateUrlW(&urlComponents, 0, NULL, &url_length);
4088 if(!b && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
4089 orig_url = heap_alloc(url_length);
4091 /* convert from bytes to characters */
4092 url_length = url_length / sizeof(WCHAR) - 1;
4093 b = InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length);
4096 if(b) {
4097 url_length = 0;
4098 b = InternetCombineUrlW(orig_url, redirect_url, NULL, &url_length, ICU_ENCODE_SPACES_ONLY);
4099 if(!b && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
4100 combined_url = heap_alloc(url_length * sizeof(WCHAR));
4101 b = InternetCombineUrlW(orig_url, redirect_url, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY);
4102 if(!b) {
4103 heap_free(combined_url);
4104 combined_url = NULL;
4109 heap_free(orig_url);
4110 heap_free(redirect_url);
4111 return combined_url;
4115 /***********************************************************************
4116 * HTTP_HandleRedirect (internal)
4118 static DWORD HTTP_HandleRedirect(http_request_t *request, LPCWSTR lpszUrl)
4120 http_session_t *session = request->session;
4121 WCHAR *path;
4123 if(lpszUrl[0]=='/')
4125 /* if it's an absolute path, keep the same session info */
4126 path = heap_strdupW(lpszUrl);
4128 else
4130 URL_COMPONENTSW urlComponents = { sizeof(urlComponents) };
4131 BOOL custom_port = FALSE;
4132 substr_t host;
4134 urlComponents.dwHostNameLength = 1;
4135 urlComponents.dwUserNameLength = 1;
4136 urlComponents.dwUrlPathLength = 1;
4137 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
4138 return INTERNET_GetLastError();
4140 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP) {
4141 if(request->hdr.dwFlags & INTERNET_FLAG_SECURE) {
4142 TRACE("redirect from secure page to non-secure page\n");
4143 /* FIXME: warn about from secure redirect to non-secure page */
4144 request->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
4147 custom_port = urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT;
4148 }else if(urlComponents.nScheme == INTERNET_SCHEME_HTTPS) {
4149 if(!(request->hdr.dwFlags & INTERNET_FLAG_SECURE)) {
4150 TRACE("redirect from non-secure page to secure page\n");
4151 /* FIXME: notify about redirect to secure page */
4152 request->hdr.dwFlags |= INTERNET_FLAG_SECURE;
4155 custom_port = urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT;
4158 heap_free(session->hostName);
4160 session->hostName = heap_strndupW(urlComponents.lpszHostName, urlComponents.dwHostNameLength);
4161 session->hostPort = urlComponents.nPort;
4163 heap_free(session->userName);
4164 session->userName = NULL;
4165 if (urlComponents.dwUserNameLength)
4166 session->userName = heap_strndupW(urlComponents.lpszUserName, urlComponents.dwUserNameLength);
4168 reset_data_stream(request);
4170 host = substr(urlComponents.lpszHostName, urlComponents.dwHostNameLength);
4172 if(host.len != strlenW(request->server->name) || strncmpiW(request->server->name, host.str, host.len)
4173 || request->server->port != urlComponents.nPort) {
4174 server_t *new_server;
4176 new_server = get_server(host, urlComponents.nPort, urlComponents.nScheme == INTERNET_SCHEME_HTTPS, TRUE);
4177 server_release(request->server);
4178 request->server = new_server;
4181 if (custom_port)
4182 HTTP_ProcessHeader(request, hostW, request->server->host_port, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
4183 else
4184 HTTP_ProcessHeader(request, hostW, request->server->name, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
4186 path = heap_strndupW(urlComponents.lpszUrlPath, urlComponents.dwUrlPathLength);
4188 heap_free(request->path);
4189 request->path = NULL;
4190 if (*path)
4192 DWORD needed = 0;
4193 HRESULT rc;
4195 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
4196 if (rc == E_POINTER)
4197 needed = strlenW(path)+1;
4198 request->path = heap_alloc(needed*sizeof(WCHAR));
4199 rc = UrlEscapeW(path, request->path, &needed,
4200 URL_ESCAPE_SPACES_ONLY);
4201 if (rc != S_OK)
4203 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
4204 strcpyW(request->path, path);
4208 heap_free(path);
4210 /* Remove custom content-type/length headers on redirects. */
4211 remove_header(request, szContent_Type, TRUE);
4212 remove_header(request, szContent_Length, TRUE);
4214 return ERROR_SUCCESS;
4217 /***********************************************************************
4218 * HTTP_build_req (internal)
4220 * concatenate all the strings in the request together
4222 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
4224 LPCWSTR *t;
4225 LPWSTR str;
4227 for( t = list; *t ; t++ )
4228 len += strlenW( *t );
4229 len++;
4231 str = heap_alloc(len*sizeof(WCHAR));
4232 *str = 0;
4234 for( t = list; *t ; t++ )
4235 strcatW( str, *t );
4237 return str;
4240 static void HTTP_InsertCookies(http_request_t *request)
4242 WCHAR *cookies;
4243 DWORD res;
4245 res = get_cookie_header(request->server->name, request->path, &cookies);
4246 if(res != ERROR_SUCCESS || !cookies)
4247 return;
4249 HTTP_HttpAddRequestHeadersW(request, cookies, strlenW(cookies),
4250 HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
4251 heap_free(cookies);
4254 static WORD HTTP_ParseWkday(LPCWSTR day)
4256 static const WCHAR days[7][4] = {{ 's','u','n',0 },
4257 { 'm','o','n',0 },
4258 { 't','u','e',0 },
4259 { 'w','e','d',0 },
4260 { 't','h','u',0 },
4261 { 'f','r','i',0 },
4262 { 's','a','t',0 }};
4263 unsigned int i;
4264 for (i = 0; i < sizeof(days)/sizeof(*days); i++)
4265 if (!strcmpiW(day, days[i]))
4266 return i;
4268 /* Invalid */
4269 return 7;
4272 static WORD HTTP_ParseMonth(LPCWSTR month)
4274 static const WCHAR jan[] = { 'j','a','n',0 };
4275 static const WCHAR feb[] = { 'f','e','b',0 };
4276 static const WCHAR mar[] = { 'm','a','r',0 };
4277 static const WCHAR apr[] = { 'a','p','r',0 };
4278 static const WCHAR may[] = { 'm','a','y',0 };
4279 static const WCHAR jun[] = { 'j','u','n',0 };
4280 static const WCHAR jul[] = { 'j','u','l',0 };
4281 static const WCHAR aug[] = { 'a','u','g',0 };
4282 static const WCHAR sep[] = { 's','e','p',0 };
4283 static const WCHAR oct[] = { 'o','c','t',0 };
4284 static const WCHAR nov[] = { 'n','o','v',0 };
4285 static const WCHAR dec[] = { 'd','e','c',0 };
4287 if (!strcmpiW(month, jan)) return 1;
4288 if (!strcmpiW(month, feb)) return 2;
4289 if (!strcmpiW(month, mar)) return 3;
4290 if (!strcmpiW(month, apr)) return 4;
4291 if (!strcmpiW(month, may)) return 5;
4292 if (!strcmpiW(month, jun)) return 6;
4293 if (!strcmpiW(month, jul)) return 7;
4294 if (!strcmpiW(month, aug)) return 8;
4295 if (!strcmpiW(month, sep)) return 9;
4296 if (!strcmpiW(month, oct)) return 10;
4297 if (!strcmpiW(month, nov)) return 11;
4298 if (!strcmpiW(month, dec)) return 12;
4299 /* Invalid */
4300 return 0;
4303 /* Parses the string pointed to by *str, assumed to be a 24-hour time HH:MM:SS,
4304 * optionally preceded by whitespace.
4305 * Upon success, returns TRUE, sets the wHour, wMinute, and wSecond fields of
4306 * st, and sets *str to the first character after the time format.
4308 static BOOL HTTP_ParseTime(SYSTEMTIME *st, LPCWSTR *str)
4310 LPCWSTR ptr = *str;
4311 WCHAR *nextPtr;
4312 unsigned long num;
4314 while (isspaceW(*ptr))
4315 ptr++;
4317 num = strtoulW(ptr, &nextPtr, 10);
4318 if (!nextPtr || nextPtr <= ptr || *nextPtr != ':')
4320 ERR("unexpected time format %s\n", debugstr_w(ptr));
4321 return FALSE;
4323 if (num > 23)
4325 ERR("unexpected hour in time format %s\n", debugstr_w(ptr));
4326 return FALSE;
4328 ptr = nextPtr + 1;
4329 st->wHour = (WORD)num;
4330 num = strtoulW(ptr, &nextPtr, 10);
4331 if (!nextPtr || nextPtr <= ptr || *nextPtr != ':')
4333 ERR("unexpected time format %s\n", debugstr_w(ptr));
4334 return FALSE;
4336 if (num > 59)
4338 ERR("unexpected minute in time format %s\n", debugstr_w(ptr));
4339 return FALSE;
4341 ptr = nextPtr + 1;
4342 st->wMinute = (WORD)num;
4343 num = strtoulW(ptr, &nextPtr, 10);
4344 if (!nextPtr || nextPtr <= ptr)
4346 ERR("unexpected time format %s\n", debugstr_w(ptr));
4347 return FALSE;
4349 if (num > 59)
4351 ERR("unexpected second in time format %s\n", debugstr_w(ptr));
4352 return FALSE;
4354 ptr = nextPtr + 1;
4355 *str = ptr;
4356 st->wSecond = (WORD)num;
4357 return TRUE;
4360 static BOOL HTTP_ParseDateAsAsctime(LPCWSTR value, FILETIME *ft)
4362 static const WCHAR gmt[]= { 'G','M','T',0 };
4363 WCHAR day[4], *dayPtr, month[4], *monthPtr, *nextPtr;
4364 LPCWSTR ptr;
4365 SYSTEMTIME st = { 0 };
4366 unsigned long num;
4368 for (ptr = value, dayPtr = day; *ptr && !isspaceW(*ptr) &&
4369 dayPtr - day < sizeof(day) / sizeof(day[0]) - 1; ptr++, dayPtr++)
4370 *dayPtr = *ptr;
4371 *dayPtr = 0;
4372 st.wDayOfWeek = HTTP_ParseWkday(day);
4373 if (st.wDayOfWeek >= 7)
4375 ERR("unexpected weekday %s\n", debugstr_w(day));
4376 return FALSE;
4379 while (isspaceW(*ptr))
4380 ptr++;
4382 for (monthPtr = month; !isspace(*ptr) &&
4383 monthPtr - month < sizeof(month) / sizeof(month[0]) - 1;
4384 monthPtr++, ptr++)
4385 *monthPtr = *ptr;
4386 *monthPtr = 0;
4387 st.wMonth = HTTP_ParseMonth(month);
4388 if (!st.wMonth || st.wMonth > 12)
4390 ERR("unexpected month %s\n", debugstr_w(month));
4391 return FALSE;
4394 while (isspaceW(*ptr))
4395 ptr++;
4397 num = strtoulW(ptr, &nextPtr, 10);
4398 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4400 ERR("unexpected day %s\n", debugstr_w(ptr));
4401 return FALSE;
4403 ptr = nextPtr;
4404 st.wDay = (WORD)num;
4406 while (isspaceW(*ptr))
4407 ptr++;
4409 if (!HTTP_ParseTime(&st, &ptr))
4410 return FALSE;
4412 while (isspaceW(*ptr))
4413 ptr++;
4415 num = strtoulW(ptr, &nextPtr, 10);
4416 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4418 ERR("unexpected year %s\n", debugstr_w(ptr));
4419 return FALSE;
4421 ptr = nextPtr;
4422 st.wYear = (WORD)num;
4424 while (isspaceW(*ptr))
4425 ptr++;
4427 /* asctime() doesn't report a timezone, but some web servers do, so accept
4428 * with or without GMT.
4430 if (*ptr && strcmpW(ptr, gmt))
4432 ERR("unexpected timezone %s\n", debugstr_w(ptr));
4433 return FALSE;
4435 return SystemTimeToFileTime(&st, ft);
4438 static BOOL HTTP_ParseRfc1123Date(LPCWSTR value, FILETIME *ft)
4440 static const WCHAR gmt[]= { 'G','M','T',0 };
4441 WCHAR *nextPtr, day[4], month[4], *monthPtr;
4442 LPCWSTR ptr;
4443 unsigned long num;
4444 SYSTEMTIME st = { 0 };
4446 ptr = strchrW(value, ',');
4447 if (!ptr)
4448 return FALSE;
4449 if (ptr - value != 3)
4451 WARN("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4452 return FALSE;
4454 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4455 day[3] = 0;
4456 st.wDayOfWeek = HTTP_ParseWkday(day);
4457 if (st.wDayOfWeek > 6)
4459 WARN("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4460 return FALSE;
4462 ptr++;
4464 while (isspaceW(*ptr))
4465 ptr++;
4467 num = strtoulW(ptr, &nextPtr, 10);
4468 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4470 WARN("unexpected day %s\n", debugstr_w(value));
4471 return FALSE;
4473 ptr = nextPtr;
4474 st.wDay = (WORD)num;
4476 while (isspaceW(*ptr))
4477 ptr++;
4479 for (monthPtr = month; !isspace(*ptr) &&
4480 monthPtr - month < sizeof(month) / sizeof(month[0]) - 1;
4481 monthPtr++, ptr++)
4482 *monthPtr = *ptr;
4483 *monthPtr = 0;
4484 st.wMonth = HTTP_ParseMonth(month);
4485 if (!st.wMonth || st.wMonth > 12)
4487 WARN("unexpected month %s\n", debugstr_w(month));
4488 return FALSE;
4491 while (isspaceW(*ptr))
4492 ptr++;
4494 num = strtoulW(ptr, &nextPtr, 10);
4495 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4497 ERR("unexpected year %s\n", debugstr_w(value));
4498 return FALSE;
4500 ptr = nextPtr;
4501 st.wYear = (WORD)num;
4503 if (!HTTP_ParseTime(&st, &ptr))
4504 return FALSE;
4506 while (isspaceW(*ptr))
4507 ptr++;
4509 if (strcmpW(ptr, gmt))
4511 ERR("unexpected time zone %s\n", debugstr_w(ptr));
4512 return FALSE;
4514 return SystemTimeToFileTime(&st, ft);
4517 static WORD HTTP_ParseWeekday(LPCWSTR day)
4519 static const WCHAR days[7][10] = {{ 's','u','n','d','a','y',0 },
4520 { 'm','o','n','d','a','y',0 },
4521 { 't','u','e','s','d','a','y',0 },
4522 { 'w','e','d','n','e','s','d','a','y',0 },
4523 { 't','h','u','r','s','d','a','y',0 },
4524 { 'f','r','i','d','a','y',0 },
4525 { 's','a','t','u','r','d','a','y',0 }};
4526 unsigned int i;
4527 for (i = 0; i < sizeof(days)/sizeof(*days); i++)
4528 if (!strcmpiW(day, days[i]))
4529 return i;
4531 /* Invalid */
4532 return 7;
4535 static BOOL HTTP_ParseRfc850Date(LPCWSTR value, FILETIME *ft)
4537 static const WCHAR gmt[]= { 'G','M','T',0 };
4538 WCHAR *nextPtr, day[10], month[4], *monthPtr;
4539 LPCWSTR ptr;
4540 unsigned long num;
4541 SYSTEMTIME st = { 0 };
4543 ptr = strchrW(value, ',');
4544 if (!ptr)
4545 return FALSE;
4546 if (ptr - value == 3)
4548 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4549 day[3] = 0;
4550 st.wDayOfWeek = HTTP_ParseWkday(day);
4551 if (st.wDayOfWeek > 6)
4553 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4554 return FALSE;
4557 else if (ptr - value < sizeof(day) / sizeof(day[0]))
4559 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4560 day[ptr - value + 1] = 0;
4561 st.wDayOfWeek = HTTP_ParseWeekday(day);
4562 if (st.wDayOfWeek > 6)
4564 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4565 return FALSE;
4568 else
4570 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4571 return FALSE;
4573 ptr++;
4575 while (isspaceW(*ptr))
4576 ptr++;
4578 num = strtoulW(ptr, &nextPtr, 10);
4579 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4581 ERR("unexpected day %s\n", debugstr_w(value));
4582 return FALSE;
4584 ptr = nextPtr;
4585 st.wDay = (WORD)num;
4587 if (*ptr != '-')
4589 ERR("unexpected month format %s\n", debugstr_w(ptr));
4590 return FALSE;
4592 ptr++;
4594 for (monthPtr = month; *ptr != '-' &&
4595 monthPtr - month < sizeof(month) / sizeof(month[0]) - 1;
4596 monthPtr++, ptr++)
4597 *monthPtr = *ptr;
4598 *monthPtr = 0;
4599 st.wMonth = HTTP_ParseMonth(month);
4600 if (!st.wMonth || st.wMonth > 12)
4602 ERR("unexpected month %s\n", debugstr_w(month));
4603 return FALSE;
4606 if (*ptr != '-')
4608 ERR("unexpected year format %s\n", debugstr_w(ptr));
4609 return FALSE;
4611 ptr++;
4613 num = strtoulW(ptr, &nextPtr, 10);
4614 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4616 ERR("unexpected year %s\n", debugstr_w(value));
4617 return FALSE;
4619 ptr = nextPtr;
4620 st.wYear = (WORD)num;
4622 if (!HTTP_ParseTime(&st, &ptr))
4623 return FALSE;
4625 while (isspaceW(*ptr))
4626 ptr++;
4628 if (strcmpW(ptr, gmt))
4630 ERR("unexpected time zone %s\n", debugstr_w(ptr));
4631 return FALSE;
4633 return SystemTimeToFileTime(&st, ft);
4636 static BOOL HTTP_ParseDate(LPCWSTR value, FILETIME *ft)
4638 static const WCHAR zero[] = { '0',0 };
4639 BOOL ret;
4641 if (!strcmpW(value, zero))
4643 ft->dwLowDateTime = ft->dwHighDateTime = 0;
4644 ret = TRUE;
4646 else if (strchrW(value, ','))
4648 ret = HTTP_ParseRfc1123Date(value, ft);
4649 if (!ret)
4651 ret = HTTP_ParseRfc850Date(value, ft);
4652 if (!ret)
4653 ERR("unexpected date format %s\n", debugstr_w(value));
4656 else
4658 ret = HTTP_ParseDateAsAsctime(value, ft);
4659 if (!ret)
4660 ERR("unexpected date format %s\n", debugstr_w(value));
4662 return ret;
4665 static void HTTP_ProcessExpires(http_request_t *request)
4667 BOOL expirationFound = FALSE;
4668 int headerIndex;
4670 EnterCriticalSection( &request->headers_section );
4672 /* Look for a Cache-Control header with a max-age directive, as it takes
4673 * precedence over the Expires header.
4675 headerIndex = HTTP_GetCustomHeaderIndex(request, szCache_Control, 0, FALSE);
4676 if (headerIndex != -1)
4678 LPHTTPHEADERW ccHeader = &request->custHeaders[headerIndex];
4679 LPWSTR ptr;
4681 for (ptr = ccHeader->lpszValue; ptr && *ptr; )
4683 LPWSTR comma = strchrW(ptr, ','), end, equal;
4685 if (comma)
4686 end = comma;
4687 else
4688 end = ptr + strlenW(ptr);
4689 for (equal = end - 1; equal > ptr && *equal != '='; equal--)
4691 if (*equal == '=')
4693 static const WCHAR max_age[] = {
4694 'm','a','x','-','a','g','e',0 };
4696 if (!strncmpiW(ptr, max_age, equal - ptr - 1))
4698 LPWSTR nextPtr;
4699 unsigned long age;
4701 age = strtoulW(equal + 1, &nextPtr, 10);
4702 if (nextPtr > equal + 1)
4704 LARGE_INTEGER ft;
4706 NtQuerySystemTime( &ft );
4707 /* Age is in seconds, FILETIME resolution is in
4708 * 100 nanosecond intervals.
4710 ft.QuadPart += age * (ULONGLONG)1000000;
4711 request->expires.dwLowDateTime = ft.u.LowPart;
4712 request->expires.dwHighDateTime = ft.u.HighPart;
4713 expirationFound = TRUE;
4717 if (comma)
4719 ptr = comma + 1;
4720 while (isspaceW(*ptr))
4721 ptr++;
4723 else
4724 ptr = NULL;
4727 if (!expirationFound)
4729 headerIndex = HTTP_GetCustomHeaderIndex(request, szExpires, 0, FALSE);
4730 if (headerIndex != -1)
4732 LPHTTPHEADERW expiresHeader = &request->custHeaders[headerIndex];
4733 FILETIME ft;
4735 if (HTTP_ParseDate(expiresHeader->lpszValue, &ft))
4737 expirationFound = TRUE;
4738 request->expires = ft;
4742 if (!expirationFound)
4744 LARGE_INTEGER t;
4746 /* With no known age, default to 10 minutes until expiration. */
4747 NtQuerySystemTime( &t );
4748 t.QuadPart += 10 * 60 * (ULONGLONG)10000000;
4749 request->expires.dwLowDateTime = t.u.LowPart;
4750 request->expires.dwHighDateTime = t.u.HighPart;
4753 LeaveCriticalSection( &request->headers_section );
4756 static void HTTP_ProcessLastModified(http_request_t *request)
4758 int headerIndex;
4760 EnterCriticalSection( &request->headers_section );
4762 headerIndex = HTTP_GetCustomHeaderIndex(request, szLast_Modified, 0, FALSE);
4763 if (headerIndex != -1)
4765 LPHTTPHEADERW expiresHeader = &request->custHeaders[headerIndex];
4766 FILETIME ft;
4768 if (HTTP_ParseDate(expiresHeader->lpszValue, &ft))
4769 request->last_modified = ft;
4772 LeaveCriticalSection( &request->headers_section );
4775 static void http_process_keep_alive(http_request_t *req)
4777 int index;
4779 EnterCriticalSection( &req->headers_section );
4781 if ((index = HTTP_GetCustomHeaderIndex(req, szConnection, 0, FALSE)) != -1)
4782 req->netconn->keep_alive = !strcmpiW(req->custHeaders[index].lpszValue, szKeepAlive);
4783 else if ((index = HTTP_GetCustomHeaderIndex(req, szProxy_Connection, 0, FALSE)) != -1)
4784 req->netconn->keep_alive = !strcmpiW(req->custHeaders[index].lpszValue, szKeepAlive);
4785 else
4786 req->netconn->keep_alive = !strcmpiW(req->version, g_szHttp1_1);
4788 LeaveCriticalSection( &req->headers_section );
4791 static DWORD open_http_connection(http_request_t *request, BOOL *reusing)
4793 const BOOL is_https = (request->hdr.dwFlags & INTERNET_FLAG_SECURE) != 0;
4794 netconn_t *netconn = NULL;
4795 DWORD res;
4797 reset_data_stream(request);
4799 if (request->netconn)
4801 if (is_valid_netconn(request->netconn) && NETCON_is_alive(request->netconn))
4803 *reusing = TRUE;
4804 return ERROR_SUCCESS;
4806 else
4808 free_netconn(request->netconn);
4809 request->netconn = NULL;
4813 res = HTTP_ResolveName(request);
4814 if(res != ERROR_SUCCESS)
4815 return res;
4817 EnterCriticalSection(&connection_pool_cs);
4819 while(!list_empty(&request->server->conn_pool)) {
4820 netconn = LIST_ENTRY(list_head(&request->server->conn_pool), netconn_t, pool_entry);
4821 list_remove(&netconn->pool_entry);
4823 if(is_valid_netconn(netconn) && NETCON_is_alive(netconn))
4824 break;
4826 TRACE("connection %p closed during idle\n", netconn);
4827 free_netconn(netconn);
4828 netconn = NULL;
4831 LeaveCriticalSection(&connection_pool_cs);
4833 if(netconn) {
4834 TRACE("<-- reusing %p netconn\n", netconn);
4835 request->netconn = netconn;
4836 *reusing = TRUE;
4837 return ERROR_SUCCESS;
4840 TRACE("connecting to %s, proxy %s\n", debugstr_w(request->server->name),
4841 request->proxy ? debugstr_w(request->proxy->name) : "(null)");
4843 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
4844 INTERNET_STATUS_CONNECTING_TO_SERVER,
4845 request->server->addr_str,
4846 strlen(request->server->addr_str)+1);
4848 res = create_netconn(is_https, request->proxy ? request->proxy : request->server, request->security_flags,
4849 (request->hdr.ErrorMask & INTERNET_ERROR_MASK_COMBINED_SEC_CERT) != 0,
4850 request->connect_timeout, &netconn);
4851 if(res != ERROR_SUCCESS) {
4852 ERR("create_netconn failed: %u\n", res);
4853 return res;
4856 request->netconn = netconn;
4858 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
4859 INTERNET_STATUS_CONNECTED_TO_SERVER,
4860 request->server->addr_str, strlen(request->server->addr_str)+1);
4862 *reusing = FALSE;
4863 TRACE("Created connection to %s: %p\n", debugstr_w(request->server->name), netconn);
4864 return ERROR_SUCCESS;
4867 static char *build_ascii_request( const WCHAR *str, void *data, DWORD data_len, DWORD *out_len )
4869 int len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
4870 char *ret;
4872 if (!(ret = heap_alloc( len + data_len ))) return NULL;
4873 WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
4874 if (data_len) memcpy( ret + len - 1, data, data_len );
4875 *out_len = len + data_len - 1;
4876 ret[*out_len] = 0;
4877 return ret;
4880 static void set_content_length_header( http_request_t *request, DWORD len, DWORD flags )
4882 static const WCHAR fmtW[] =
4883 {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','u','\r','\n',0};
4884 WCHAR buf[sizeof(fmtW)/sizeof(fmtW[0]) + 10];
4886 sprintfW( buf, fmtW, len );
4887 HTTP_HttpAddRequestHeadersW( request, buf, ~0u, flags );
4890 /***********************************************************************
4891 * HTTP_HttpSendRequestW (internal)
4893 * Sends the specified request to the HTTP server
4895 * RETURNS
4896 * ERROR_SUCCESS on success
4897 * win32 error code on failure
4900 static DWORD HTTP_HttpSendRequestW(http_request_t *request, LPCWSTR lpszHeaders,
4901 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
4902 DWORD dwContentLength, BOOL bEndRequest)
4904 BOOL redirected = FALSE, secure_proxy_connect = FALSE, loop_next;
4905 LPWSTR requestString = NULL;
4906 INT responseLen, cnt;
4907 DWORD res;
4909 TRACE("--> %p\n", request);
4911 assert(request->hdr.htype == WH_HHTTPREQ);
4913 /* if the verb is NULL default to GET */
4914 if (!request->verb)
4915 request->verb = heap_strdupW(szGET);
4917 HTTP_ProcessHeader(request, hostW, request->server->canon_host_port,
4918 HTTP_ADDREQ_FLAG_ADD_IF_NEW | HTTP_ADDHDR_FLAG_REQ);
4920 if (dwContentLength || strcmpW(request->verb, szGET))
4922 set_content_length_header(request, dwContentLength, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4923 request->bytesToWrite = dwContentLength;
4925 if (request->session->appInfo->agent)
4927 WCHAR *agent_header;
4928 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0};
4929 int len;
4931 len = strlenW(request->session->appInfo->agent) + strlenW(user_agent);
4932 agent_header = heap_alloc(len * sizeof(WCHAR));
4933 sprintfW(agent_header, user_agent, request->session->appInfo->agent);
4935 HTTP_HttpAddRequestHeadersW(request, agent_header, strlenW(agent_header), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4936 heap_free(agent_header);
4938 if (request->hdr.dwFlags & INTERNET_FLAG_PRAGMA_NOCACHE)
4940 static const WCHAR pragma_nocache[] = {'P','r','a','g','m','a',':',' ','n','o','-','c','a','c','h','e','\r','\n',0};
4941 HTTP_HttpAddRequestHeadersW(request, pragma_nocache, strlenW(pragma_nocache), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4943 if ((request->hdr.dwFlags & INTERNET_FLAG_NO_CACHE_WRITE) && strcmpW(request->verb, szGET))
4945 static const WCHAR cache_control[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l',':',
4946 ' ','n','o','-','c','a','c','h','e','\r','\n',0};
4947 HTTP_HttpAddRequestHeadersW(request, cache_control, strlenW(cache_control), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4950 /* add the headers the caller supplied */
4951 if( lpszHeaders && dwHeaderLength )
4952 HTTP_HttpAddRequestHeadersW(request, lpszHeaders, dwHeaderLength, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
4956 DWORD len, data_len = dwOptionalLength;
4957 BOOL reusing_connection;
4958 char *ascii_req;
4960 loop_next = FALSE;
4962 if(redirected) {
4963 request->contentLength = ~0u;
4964 request->bytesToWrite = 0;
4967 if (TRACE_ON(wininet))
4969 HTTPHEADERW *host;
4971 EnterCriticalSection( &request->headers_section );
4972 host = HTTP_GetHeader( request, hostW );
4973 TRACE("Going to url %s %s\n", debugstr_w(host->lpszValue), debugstr_w(request->path));
4974 LeaveCriticalSection( &request->headers_section );
4977 HTTP_FixURL(request);
4978 if (request->hdr.dwFlags & INTERNET_FLAG_KEEP_CONNECTION)
4980 HTTP_ProcessHeader(request, szConnection, szKeepAlive,
4981 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_ADD);
4983 HTTP_InsertAuthorization(request, request->authInfo, szAuthorization);
4984 HTTP_InsertAuthorization(request, request->proxyAuthInfo, szProxy_Authorization);
4986 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES))
4987 HTTP_InsertCookies(request);
4989 res = open_http_connection(request, &reusing_connection);
4990 if (res != ERROR_SUCCESS)
4991 break;
4993 if (!reusing_connection && (request->hdr.dwFlags & INTERNET_FLAG_SECURE))
4995 if (request->proxy) secure_proxy_connect = TRUE;
4996 else
4998 res = NETCON_secure_connect(request->netconn, request->server);
4999 if (res != ERROR_SUCCESS)
5001 WARN("failed to upgrade to secure connection\n");
5002 http_release_netconn(request, FALSE);
5003 break;
5007 if (secure_proxy_connect)
5009 static const WCHAR connectW[] = {'C','O','N','N','E','C','T',0};
5010 const WCHAR *target = request->server->host_port;
5012 if (HTTP_GetCustomHeaderIndex(request, szContent_Length, 0, TRUE) >= 0)
5013 set_content_length_header(request, 0, HTTP_ADDREQ_FLAG_REPLACE);
5015 requestString = build_request_header(request, connectW, target, g_szHttp1_1, TRUE);
5017 else if (request->proxy && !(request->hdr.dwFlags & INTERNET_FLAG_SECURE))
5019 WCHAR *url = build_proxy_path_url(request);
5020 requestString = build_request_header(request, request->verb, url, request->version, TRUE);
5021 heap_free(url);
5023 else
5025 if (request->proxy && HTTP_GetCustomHeaderIndex(request, szContent_Length, 0, TRUE) >= 0)
5026 set_content_length_header(request, dwContentLength, HTTP_ADDREQ_FLAG_REPLACE);
5028 requestString = build_request_header(request, request->verb, request->path, request->version, TRUE);
5031 TRACE("Request header -> %s\n", debugstr_w(requestString) );
5033 /* send the request as ASCII, tack on the optional data */
5034 if (!lpOptional || redirected || secure_proxy_connect)
5035 data_len = 0;
5037 ascii_req = build_ascii_request( requestString, lpOptional, data_len, &len );
5038 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
5040 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5041 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
5043 NETCON_set_timeout( request->netconn, TRUE, request->send_timeout );
5044 res = NETCON_send(request->netconn, ascii_req, len, 0, &cnt);
5045 heap_free( ascii_req );
5046 if(res != ERROR_SUCCESS) {
5047 TRACE("send failed: %u\n", res);
5048 if(!reusing_connection)
5049 break;
5050 http_release_netconn(request, FALSE);
5051 loop_next = TRUE;
5052 continue;
5055 request->bytesWritten = data_len;
5057 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5058 INTERNET_STATUS_REQUEST_SENT,
5059 &len, sizeof(DWORD));
5061 if (bEndRequest)
5063 DWORD dwBufferSize;
5065 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5066 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
5068 if (HTTP_GetResponseHeaders(request, &responseLen))
5070 http_release_netconn(request, FALSE);
5071 res = ERROR_INTERNET_CONNECTION_ABORTED;
5072 goto lend;
5074 /* FIXME: We should know that connection is closed before sending
5075 * headers. Otherwise wrong callbacks are executed */
5076 if(!responseLen && reusing_connection) {
5077 TRACE("Connection closed by server, reconnecting\n");
5078 http_release_netconn(request, FALSE);
5079 loop_next = TRUE;
5080 continue;
5083 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5084 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
5085 sizeof(DWORD));
5087 http_process_keep_alive(request);
5088 HTTP_ProcessCookies(request);
5089 HTTP_ProcessExpires(request);
5090 HTTP_ProcessLastModified(request);
5092 res = set_content_length(request);
5093 if(res != ERROR_SUCCESS)
5094 goto lend;
5095 if(!request->contentLength)
5096 http_release_netconn(request, TRUE);
5098 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && responseLen)
5100 WCHAR *new_url;
5102 switch(request->status_code) {
5103 case HTTP_STATUS_REDIRECT:
5104 case HTTP_STATUS_MOVED:
5105 case HTTP_STATUS_REDIRECT_KEEP_VERB:
5106 case HTTP_STATUS_REDIRECT_METHOD:
5107 new_url = get_redirect_url(request);
5108 if(!new_url)
5109 break;
5111 if (strcmpW(request->verb, szGET) && strcmpW(request->verb, szHEAD) &&
5112 request->status_code != HTTP_STATUS_REDIRECT_KEEP_VERB)
5114 heap_free(request->verb);
5115 request->verb = heap_strdupW(szGET);
5117 http_release_netconn(request, drain_content(request, FALSE));
5118 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REDIRECT,
5119 new_url, (strlenW(new_url) + 1) * sizeof(WCHAR));
5120 res = HTTP_HandleRedirect(request, new_url);
5121 heap_free(new_url);
5122 if (res == ERROR_SUCCESS) {
5123 heap_free(requestString);
5124 loop_next = TRUE;
5126 redirected = TRUE;
5129 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTH) && res == ERROR_SUCCESS)
5131 WCHAR szAuthValue[2048];
5132 dwBufferSize=2048;
5133 if (request->status_code == HTTP_STATUS_DENIED)
5135 WCHAR *host = heap_strdupW( request->server->canon_host_port );
5136 DWORD dwIndex = 0;
5137 while (HTTP_HttpQueryInfoW(request,HTTP_QUERY_WWW_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex) == ERROR_SUCCESS)
5139 if (HTTP_DoAuthorization(request, szAuthValue,
5140 &request->authInfo,
5141 request->session->userName,
5142 request->session->password, host))
5144 heap_free(requestString);
5145 if(!drain_content(request, TRUE)) {
5146 FIXME("Could not drain content\n");
5147 http_release_netconn(request, FALSE);
5149 loop_next = TRUE;
5150 break;
5153 heap_free( host );
5155 if(!loop_next) {
5156 TRACE("Cleaning wrong authorization data\n");
5157 destroy_authinfo(request->authInfo);
5158 request->authInfo = NULL;
5161 if (request->status_code == HTTP_STATUS_PROXY_AUTH_REQ)
5163 DWORD dwIndex = 0;
5164 while (HTTP_HttpQueryInfoW(request,HTTP_QUERY_PROXY_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex) == ERROR_SUCCESS)
5166 if (HTTP_DoAuthorization(request, szAuthValue,
5167 &request->proxyAuthInfo,
5168 request->session->appInfo->proxyUsername,
5169 request->session->appInfo->proxyPassword,
5170 NULL))
5172 heap_free(requestString);
5173 if(!drain_content(request, TRUE)) {
5174 FIXME("Could not drain content\n");
5175 http_release_netconn(request, FALSE);
5177 loop_next = TRUE;
5178 break;
5182 if(!loop_next) {
5183 TRACE("Cleaning wrong proxy authorization data\n");
5184 destroy_authinfo(request->proxyAuthInfo);
5185 request->proxyAuthInfo = NULL;
5189 if (secure_proxy_connect && request->status_code == HTTP_STATUS_OK)
5191 res = NETCON_secure_connect(request->netconn, request->server);
5192 if (res != ERROR_SUCCESS)
5194 WARN("failed to upgrade to secure proxy connection\n");
5195 http_release_netconn( request, FALSE );
5196 break;
5198 remove_header(request, szProxy_Authorization, TRUE);
5199 destroy_authinfo(request->proxyAuthInfo);
5200 request->proxyAuthInfo = NULL;
5202 secure_proxy_connect = FALSE;
5203 loop_next = TRUE;
5206 else
5207 res = ERROR_SUCCESS;
5209 while (loop_next);
5211 lend:
5212 heap_free(requestString);
5214 /* TODO: send notification for P3P header */
5216 if(res == ERROR_SUCCESS)
5217 create_cache_entry(request);
5219 if (request->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5221 if (res == ERROR_SUCCESS) {
5222 if(bEndRequest && request->contentLength && request->bytesWritten == request->bytesToWrite)
5223 HTTP_ReceiveRequestData(request, TRUE, NULL);
5224 else
5225 send_request_complete(request,
5226 request->session->hdr.dwInternalFlags & INET_OPENURL ? (DWORD_PTR)request->hdr.hInternet : 1, 0);
5227 }else {
5228 send_request_complete(request, 0, res);
5232 TRACE("<--\n");
5233 return res;
5236 typedef struct {
5237 task_header_t hdr;
5238 WCHAR *headers;
5239 DWORD headers_len;
5240 void *optional;
5241 DWORD optional_len;
5242 DWORD content_len;
5243 BOOL end_request;
5244 } send_request_task_t;
5246 /***********************************************************************
5248 * Helper functions for the HttpSendRequest(Ex) functions
5251 static void AsyncHttpSendRequestProc(task_header_t *hdr)
5253 send_request_task_t *task = (send_request_task_t*)hdr;
5254 http_request_t *request = (http_request_t*)task->hdr.hdr;
5256 TRACE("%p\n", request);
5258 HTTP_HttpSendRequestW(request, task->headers, task->headers_len, task->optional,
5259 task->optional_len, task->content_len, task->end_request);
5261 heap_free(task->headers);
5265 static DWORD HTTP_HttpEndRequestW(http_request_t *request, DWORD dwFlags, DWORD_PTR dwContext)
5267 INT responseLen;
5268 DWORD res = ERROR_SUCCESS;
5270 if(!is_valid_netconn(request->netconn)) {
5271 WARN("Not connected\n");
5272 send_request_complete(request, 0, ERROR_INTERNET_OPERATION_CANCELLED);
5273 return ERROR_INTERNET_OPERATION_CANCELLED;
5276 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5277 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
5279 if (HTTP_GetResponseHeaders(request, &responseLen) || !responseLen)
5280 res = ERROR_HTTP_HEADER_NOT_FOUND;
5282 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5283 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
5285 /* process cookies here. Is this right? */
5286 http_process_keep_alive(request);
5287 HTTP_ProcessCookies(request);
5288 HTTP_ProcessExpires(request);
5289 HTTP_ProcessLastModified(request);
5291 if ((res = set_content_length(request)) == ERROR_SUCCESS) {
5292 if(!request->contentLength)
5293 http_release_netconn(request, TRUE);
5296 if (res == ERROR_SUCCESS && !(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
5298 switch(request->status_code) {
5299 case HTTP_STATUS_REDIRECT:
5300 case HTTP_STATUS_MOVED:
5301 case HTTP_STATUS_REDIRECT_METHOD:
5302 case HTTP_STATUS_REDIRECT_KEEP_VERB: {
5303 WCHAR *new_url;
5305 new_url = get_redirect_url(request);
5306 if(!new_url)
5307 break;
5309 if (strcmpW(request->verb, szGET) && strcmpW(request->verb, szHEAD) &&
5310 request->status_code != HTTP_STATUS_REDIRECT_KEEP_VERB)
5312 heap_free(request->verb);
5313 request->verb = heap_strdupW(szGET);
5315 http_release_netconn(request, drain_content(request, FALSE));
5316 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REDIRECT,
5317 new_url, (strlenW(new_url) + 1) * sizeof(WCHAR));
5318 res = HTTP_HandleRedirect(request, new_url);
5319 heap_free(new_url);
5320 if (res == ERROR_SUCCESS)
5321 res = HTTP_HttpSendRequestW(request, NULL, 0, NULL, 0, 0, TRUE);
5326 if(res == ERROR_SUCCESS)
5327 create_cache_entry(request);
5329 if (res == ERROR_SUCCESS && request->contentLength)
5330 HTTP_ReceiveRequestData(request, TRUE, NULL);
5331 else
5332 send_request_complete(request, res == ERROR_SUCCESS, res);
5334 return res;
5337 /***********************************************************************
5338 * HttpEndRequestA (WININET.@)
5340 * Ends an HTTP request that was started by HttpSendRequestEx
5342 * RETURNS
5343 * TRUE if successful
5344 * FALSE on failure
5347 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
5348 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
5350 TRACE("(%p, %p, %08x, %08lx)\n", hRequest, lpBuffersOut, dwFlags, dwContext);
5352 if (lpBuffersOut)
5354 SetLastError(ERROR_INVALID_PARAMETER);
5355 return FALSE;
5358 return HttpEndRequestW(hRequest, NULL, dwFlags, dwContext);
5361 typedef struct {
5362 task_header_t hdr;
5363 DWORD flags;
5364 DWORD context;
5365 } end_request_task_t;
5367 static void AsyncHttpEndRequestProc(task_header_t *hdr)
5369 end_request_task_t *task = (end_request_task_t*)hdr;
5370 http_request_t *req = (http_request_t*)task->hdr.hdr;
5372 TRACE("%p\n", req);
5374 HTTP_HttpEndRequestW(req, task->flags, task->context);
5377 /***********************************************************************
5378 * HttpEndRequestW (WININET.@)
5380 * Ends an HTTP request that was started by HttpSendRequestEx
5382 * RETURNS
5383 * TRUE if successful
5384 * FALSE on failure
5387 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
5388 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
5390 http_request_t *request;
5391 DWORD res;
5393 TRACE("%p %p %x %lx -->\n", hRequest, lpBuffersOut, dwFlags, dwContext);
5395 if (lpBuffersOut)
5397 SetLastError(ERROR_INVALID_PARAMETER);
5398 return FALSE;
5401 request = (http_request_t*) get_handle_object( hRequest );
5403 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5405 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
5406 if (request)
5407 WININET_Release( &request->hdr );
5408 return FALSE;
5410 request->hdr.dwFlags |= dwFlags;
5412 if (request->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5414 end_request_task_t *task;
5416 task = alloc_async_task(&request->hdr, AsyncHttpEndRequestProc, sizeof(*task));
5417 task->flags = dwFlags;
5418 task->context = dwContext;
5420 INTERNET_AsyncCall(&task->hdr);
5421 res = ERROR_IO_PENDING;
5423 else
5424 res = HTTP_HttpEndRequestW(request, dwFlags, dwContext);
5426 WININET_Release( &request->hdr );
5427 TRACE("%u <--\n", res);
5428 if(res != ERROR_SUCCESS)
5429 SetLastError(res);
5430 return res == ERROR_SUCCESS;
5433 /***********************************************************************
5434 * HttpSendRequestExA (WININET.@)
5436 * Sends the specified request to the HTTP server and allows chunked
5437 * transfers.
5439 * RETURNS
5440 * Success: TRUE
5441 * Failure: FALSE, call GetLastError() for more information.
5443 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
5444 LPINTERNET_BUFFERSA lpBuffersIn,
5445 LPINTERNET_BUFFERSA lpBuffersOut,
5446 DWORD dwFlags, DWORD_PTR dwContext)
5448 INTERNET_BUFFERSW BuffersInW;
5449 BOOL rc = FALSE;
5450 DWORD headerlen;
5451 LPWSTR header = NULL;
5453 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
5454 lpBuffersOut, dwFlags, dwContext);
5456 if (lpBuffersIn)
5458 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
5459 if (lpBuffersIn->lpcszHeader)
5461 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
5462 lpBuffersIn->dwHeadersLength,0,0);
5463 header = heap_alloc(headerlen*sizeof(WCHAR));
5464 if (!(BuffersInW.lpcszHeader = header))
5466 SetLastError(ERROR_OUTOFMEMORY);
5467 return FALSE;
5469 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
5470 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
5471 header, headerlen);
5473 else
5474 BuffersInW.lpcszHeader = NULL;
5475 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
5476 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
5477 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
5478 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
5479 BuffersInW.Next = NULL;
5482 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
5484 heap_free(header);
5485 return rc;
5488 /***********************************************************************
5489 * HttpSendRequestExW (WININET.@)
5491 * Sends the specified request to the HTTP server and allows chunked
5492 * transfers
5494 * RETURNS
5495 * Success: TRUE
5496 * Failure: FALSE, call GetLastError() for more information.
5498 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
5499 LPINTERNET_BUFFERSW lpBuffersIn,
5500 LPINTERNET_BUFFERSW lpBuffersOut,
5501 DWORD dwFlags, DWORD_PTR dwContext)
5503 http_request_t *request;
5504 http_session_t *session;
5505 appinfo_t *hIC;
5506 DWORD res;
5508 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
5509 lpBuffersOut, dwFlags, dwContext);
5511 request = (http_request_t*) get_handle_object( hRequest );
5513 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5515 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5516 goto lend;
5519 session = request->session;
5520 assert(session->hdr.htype == WH_HHTTPSESSION);
5521 hIC = session->appInfo;
5522 assert(hIC->hdr.htype == WH_HINIT);
5524 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5526 send_request_task_t *task;
5528 task = alloc_async_task(&request->hdr, AsyncHttpSendRequestProc, sizeof(*task));
5529 if (lpBuffersIn)
5531 DWORD size = 0;
5533 if (lpBuffersIn->lpcszHeader)
5535 if (lpBuffersIn->dwHeadersLength == ~0u)
5536 size = (strlenW( lpBuffersIn->lpcszHeader ) + 1) * sizeof(WCHAR);
5537 else
5538 size = lpBuffersIn->dwHeadersLength * sizeof(WCHAR);
5540 task->headers = heap_alloc(size);
5541 memcpy(task->headers, lpBuffersIn->lpcszHeader, size);
5543 else task->headers = NULL;
5545 task->headers_len = size / sizeof(WCHAR);
5546 task->optional = lpBuffersIn->lpvBuffer;
5547 task->optional_len = lpBuffersIn->dwBufferLength;
5548 task->content_len = lpBuffersIn->dwBufferTotal;
5550 else
5552 task->headers = NULL;
5553 task->headers_len = 0;
5554 task->optional = NULL;
5555 task->optional_len = 0;
5556 task->content_len = 0;
5559 task->end_request = FALSE;
5561 INTERNET_AsyncCall(&task->hdr);
5562 res = ERROR_IO_PENDING;
5564 else
5566 if (lpBuffersIn)
5567 res = HTTP_HttpSendRequestW(request, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
5568 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
5569 lpBuffersIn->dwBufferTotal, FALSE);
5570 else
5571 res = HTTP_HttpSendRequestW(request, NULL, 0, NULL, 0, 0, FALSE);
5574 lend:
5575 if ( request )
5576 WININET_Release( &request->hdr );
5578 TRACE("<---\n");
5579 SetLastError(res);
5580 return res == ERROR_SUCCESS;
5583 /***********************************************************************
5584 * HttpSendRequestW (WININET.@)
5586 * Sends the specified request to the HTTP server
5588 * RETURNS
5589 * TRUE on success
5590 * FALSE on failure
5593 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
5594 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
5596 http_request_t *request;
5597 http_session_t *session = NULL;
5598 appinfo_t *hIC = NULL;
5599 DWORD res = ERROR_SUCCESS;
5601 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest,
5602 debugstr_wn(lpszHeaders, dwHeaderLength), dwHeaderLength, lpOptional, dwOptionalLength);
5604 request = (http_request_t*) get_handle_object( hHttpRequest );
5605 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5607 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5608 goto lend;
5611 session = request->session;
5612 if (NULL == session || session->hdr.htype != WH_HHTTPSESSION)
5614 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5615 goto lend;
5618 hIC = session->appInfo;
5619 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
5621 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5622 goto lend;
5625 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5627 send_request_task_t *task;
5629 task = alloc_async_task(&request->hdr, AsyncHttpSendRequestProc, sizeof(*task));
5630 if (lpszHeaders)
5632 DWORD size;
5634 if (dwHeaderLength == ~0u) size = (strlenW(lpszHeaders) + 1) * sizeof(WCHAR);
5635 else size = dwHeaderLength * sizeof(WCHAR);
5637 task->headers = heap_alloc(size);
5638 memcpy(task->headers, lpszHeaders, size);
5640 else
5641 task->headers = NULL;
5642 task->headers_len = dwHeaderLength;
5643 task->optional = lpOptional;
5644 task->optional_len = dwOptionalLength;
5645 task->content_len = dwOptionalLength;
5646 task->end_request = TRUE;
5648 INTERNET_AsyncCall(&task->hdr);
5649 res = ERROR_IO_PENDING;
5651 else
5653 res = HTTP_HttpSendRequestW(request, lpszHeaders,
5654 dwHeaderLength, lpOptional, dwOptionalLength,
5655 dwOptionalLength, TRUE);
5657 lend:
5658 if( request )
5659 WININET_Release( &request->hdr );
5661 SetLastError(res);
5662 return res == ERROR_SUCCESS;
5665 /***********************************************************************
5666 * HttpSendRequestA (WININET.@)
5668 * Sends the specified request to the HTTP server
5670 * RETURNS
5671 * TRUE on success
5672 * FALSE on failure
5675 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
5676 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
5678 BOOL result;
5679 LPWSTR szHeaders=NULL;
5680 DWORD nLen=dwHeaderLength;
5681 if(lpszHeaders!=NULL)
5683 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
5684 szHeaders = heap_alloc(nLen*sizeof(WCHAR));
5685 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
5687 result = HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
5688 heap_free(szHeaders);
5689 return result;
5692 /***********************************************************************
5693 * HTTPSESSION_Destroy (internal)
5695 * Deallocate session handle
5698 static void HTTPSESSION_Destroy(object_header_t *hdr)
5700 http_session_t *session = (http_session_t*) hdr;
5702 TRACE("%p\n", session);
5704 WININET_Release(&session->appInfo->hdr);
5706 heap_free(session->hostName);
5707 heap_free(session->password);
5708 heap_free(session->userName);
5711 static DWORD HTTPSESSION_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
5713 http_session_t *ses = (http_session_t *)hdr;
5715 switch(option) {
5716 case INTERNET_OPTION_HANDLE_TYPE:
5717 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
5719 if (*size < sizeof(ULONG))
5720 return ERROR_INSUFFICIENT_BUFFER;
5722 *size = sizeof(DWORD);
5723 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_CONNECT_HTTP;
5724 return ERROR_SUCCESS;
5725 case INTERNET_OPTION_CONNECT_TIMEOUT:
5726 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
5728 if (*size < sizeof(DWORD))
5729 return ERROR_INSUFFICIENT_BUFFER;
5731 *size = sizeof(DWORD);
5732 *(DWORD *)buffer = ses->connect_timeout;
5733 return ERROR_SUCCESS;
5735 case INTERNET_OPTION_SEND_TIMEOUT:
5736 TRACE("INTERNET_OPTION_SEND_TIMEOUT\n");
5738 if (*size < sizeof(DWORD))
5739 return ERROR_INSUFFICIENT_BUFFER;
5741 *size = sizeof(DWORD);
5742 *(DWORD *)buffer = ses->send_timeout;
5743 return ERROR_SUCCESS;
5745 case INTERNET_OPTION_RECEIVE_TIMEOUT:
5746 TRACE("INTERNET_OPTION_RECEIVE_TIMEOUT\n");
5748 if (*size < sizeof(DWORD))
5749 return ERROR_INSUFFICIENT_BUFFER;
5751 *size = sizeof(DWORD);
5752 *(DWORD *)buffer = ses->receive_timeout;
5753 return ERROR_SUCCESS;
5756 return INET_QueryOption(hdr, option, buffer, size, unicode);
5759 static DWORD HTTPSESSION_SetOption(object_header_t *hdr, DWORD option, void *buffer, DWORD size)
5761 http_session_t *ses = (http_session_t*)hdr;
5763 switch(option) {
5764 case INTERNET_OPTION_USERNAME:
5766 heap_free(ses->userName);
5767 if (!(ses->userName = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5768 return ERROR_SUCCESS;
5770 case INTERNET_OPTION_PASSWORD:
5772 heap_free(ses->password);
5773 if (!(ses->password = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5774 return ERROR_SUCCESS;
5776 case INTERNET_OPTION_PROXY_USERNAME:
5778 heap_free(ses->appInfo->proxyUsername);
5779 if (!(ses->appInfo->proxyUsername = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5780 return ERROR_SUCCESS;
5782 case INTERNET_OPTION_PROXY_PASSWORD:
5784 heap_free(ses->appInfo->proxyPassword);
5785 if (!(ses->appInfo->proxyPassword = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5786 return ERROR_SUCCESS;
5788 case INTERNET_OPTION_CONNECT_TIMEOUT:
5790 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5791 ses->connect_timeout = *(DWORD *)buffer;
5792 return ERROR_SUCCESS;
5794 case INTERNET_OPTION_SEND_TIMEOUT:
5796 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5797 ses->send_timeout = *(DWORD *)buffer;
5798 return ERROR_SUCCESS;
5800 case INTERNET_OPTION_RECEIVE_TIMEOUT:
5802 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5803 ses->receive_timeout = *(DWORD *)buffer;
5804 return ERROR_SUCCESS;
5806 default: break;
5809 return INET_SetOption(hdr, option, buffer, size);
5812 static const object_vtbl_t HTTPSESSIONVtbl = {
5813 HTTPSESSION_Destroy,
5814 NULL,
5815 HTTPSESSION_QueryOption,
5816 HTTPSESSION_SetOption,
5817 NULL,
5818 NULL,
5819 NULL,
5820 NULL,
5821 NULL
5825 /***********************************************************************
5826 * HTTP_Connect (internal)
5828 * Create http session handle
5830 * RETURNS
5831 * HINTERNET a session handle on success
5832 * NULL on failure
5835 DWORD HTTP_Connect(appinfo_t *hIC, LPCWSTR lpszServerName,
5836 INTERNET_PORT serverPort, LPCWSTR lpszUserName,
5837 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
5838 DWORD dwInternalFlags, HINTERNET *ret)
5840 http_session_t *session = NULL;
5842 TRACE("-->\n");
5844 if (!lpszServerName || !lpszServerName[0])
5845 return ERROR_INVALID_PARAMETER;
5847 assert( hIC->hdr.htype == WH_HINIT );
5849 session = alloc_object(&hIC->hdr, &HTTPSESSIONVtbl, sizeof(http_session_t));
5850 if (!session)
5851 return ERROR_OUTOFMEMORY;
5854 * According to my tests. The name is not resolved until a request is sent
5857 session->hdr.htype = WH_HHTTPSESSION;
5858 session->hdr.dwFlags = dwFlags;
5859 session->hdr.dwContext = dwContext;
5860 session->hdr.dwInternalFlags |= dwInternalFlags;
5862 WININET_AddRef( &hIC->hdr );
5863 session->appInfo = hIC;
5864 list_add_head( &hIC->hdr.children, &session->hdr.entry );
5866 session->hostName = heap_strdupW(lpszServerName);
5867 if (lpszUserName && lpszUserName[0])
5868 session->userName = heap_strdupW(lpszUserName);
5869 if (lpszPassword && lpszPassword[0])
5870 session->password = heap_strdupW(lpszPassword);
5871 session->hostPort = serverPort;
5872 session->connect_timeout = hIC->connect_timeout;
5873 session->send_timeout = 0;
5874 session->receive_timeout = 0;
5876 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
5877 if (!(session->hdr.dwInternalFlags & INET_OPENURL))
5879 INTERNET_SendCallback(&hIC->hdr, dwContext,
5880 INTERNET_STATUS_HANDLE_CREATED, &session->hdr.hInternet,
5881 sizeof(HINTERNET));
5885 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
5886 * windows
5889 TRACE("%p --> %p\n", hIC, session);
5891 *ret = session->hdr.hInternet;
5892 return ERROR_SUCCESS;
5895 /***********************************************************************
5896 * HTTP_clear_response_headers (internal)
5898 * clear out any old response headers
5900 static void HTTP_clear_response_headers( http_request_t *request )
5902 DWORD i;
5904 EnterCriticalSection( &request->headers_section );
5906 for( i=0; i<request->nCustHeaders; i++)
5908 if( !request->custHeaders[i].lpszField )
5909 continue;
5910 if( !request->custHeaders[i].lpszValue )
5911 continue;
5912 if ( request->custHeaders[i].wFlags & HDR_ISREQUEST )
5913 continue;
5914 HTTP_DeleteCustomHeader( request, i );
5915 i--;
5918 LeaveCriticalSection( &request->headers_section );
5921 /***********************************************************************
5922 * HTTP_GetResponseHeaders (internal)
5924 * Read server response
5926 * RETURNS
5928 * TRUE on success
5929 * FALSE on error
5931 static DWORD HTTP_GetResponseHeaders(http_request_t *request, INT *len)
5933 INT cbreaks = 0;
5934 WCHAR buffer[MAX_REPLY_LEN];
5935 DWORD buflen = MAX_REPLY_LEN;
5936 INT rc = 0;
5937 char bufferA[MAX_REPLY_LEN];
5938 LPWSTR status_code = NULL, status_text = NULL;
5939 DWORD res = ERROR_HTTP_INVALID_SERVER_RESPONSE;
5940 BOOL codeHundred = FALSE;
5942 TRACE("-->\n");
5944 if(!is_valid_netconn(request->netconn))
5945 goto lend;
5947 /* clear old response headers (eg. from a redirect response) */
5948 HTTP_clear_response_headers( request );
5950 NETCON_set_timeout( request->netconn, FALSE, request->receive_timeout );
5951 do {
5953 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
5955 buflen = MAX_REPLY_LEN;
5956 if ((res = read_line(request, bufferA, &buflen)))
5957 goto lend;
5959 if (!buflen) goto lend;
5961 rc += buflen;
5962 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
5963 /* check is this a status code line? */
5964 if (!strncmpW(buffer, g_szHttp1_0, 4))
5966 /* split the version from the status code */
5967 status_code = strchrW( buffer, ' ' );
5968 if( !status_code )
5969 goto lend;
5970 *status_code++=0;
5972 /* split the status code from the status text */
5973 status_text = strchrW( status_code, ' ' );
5974 if( status_text )
5975 *status_text++=0;
5977 request->status_code = atoiW(status_code);
5979 TRACE("version [%s] status code [%s] status text [%s]\n",
5980 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
5982 codeHundred = request->status_code == HTTP_STATUS_CONTINUE;
5984 else if (!codeHundred)
5986 WARN("No status line at head of response (%s)\n", debugstr_w(buffer));
5988 heap_free(request->version);
5989 heap_free(request->statusText);
5991 request->status_code = HTTP_STATUS_OK;
5992 request->version = heap_strdupW(g_szHttp1_0);
5993 request->statusText = heap_strdupW(szOK);
5995 goto lend;
5997 } while (codeHundred);
5999 /* Add status code */
6000 HTTP_ProcessHeader(request, szStatus, status_code,
6001 HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_ADD);
6003 heap_free(request->version);
6004 heap_free(request->statusText);
6006 request->version = heap_strdupW(buffer);
6007 request->statusText = heap_strdupW(status_text ? status_text : emptyW);
6009 /* Restore the spaces */
6010 *(status_code-1) = ' ';
6011 if (status_text)
6012 *(status_text-1) = ' ';
6014 /* Parse each response line */
6017 buflen = MAX_REPLY_LEN;
6018 if (!read_line(request, bufferA, &buflen) && buflen)
6020 LPWSTR * pFieldAndValue;
6022 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
6024 if (!bufferA[0]) break;
6025 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
6027 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
6028 if (pFieldAndValue)
6030 HTTP_ProcessHeader(request, pFieldAndValue[0], pFieldAndValue[1],
6031 HTTP_ADDREQ_FLAG_ADD );
6032 HTTP_FreeTokens(pFieldAndValue);
6035 else
6037 cbreaks++;
6038 if (cbreaks >= 2)
6039 break;
6041 }while(1);
6043 res = ERROR_SUCCESS;
6045 lend:
6047 *len = rc;
6048 TRACE("<--\n");
6049 return res;
6052 /***********************************************************************
6053 * HTTP_InterpretHttpHeader (internal)
6055 * Parse server response
6057 * RETURNS
6059 * Pointer to array of field, value, NULL on success.
6060 * NULL on error.
6062 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
6064 LPWSTR * pTokenPair;
6065 LPWSTR pszColon;
6066 INT len;
6068 pTokenPair = heap_alloc_zero(sizeof(*pTokenPair)*3);
6070 pszColon = strchrW(buffer, ':');
6071 /* must have two tokens */
6072 if (!pszColon)
6074 HTTP_FreeTokens(pTokenPair);
6075 if (buffer[0])
6076 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
6077 return NULL;
6080 pTokenPair[0] = heap_alloc((pszColon - buffer + 1) * sizeof(WCHAR));
6081 if (!pTokenPair[0])
6083 HTTP_FreeTokens(pTokenPair);
6084 return NULL;
6086 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
6087 pTokenPair[0][pszColon - buffer] = '\0';
6089 /* skip colon */
6090 pszColon++;
6091 len = strlenW(pszColon);
6092 pTokenPair[1] = heap_alloc((len + 1) * sizeof(WCHAR));
6093 if (!pTokenPair[1])
6095 HTTP_FreeTokens(pTokenPair);
6096 return NULL;
6098 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
6100 strip_spaces(pTokenPair[0]);
6101 strip_spaces(pTokenPair[1]);
6103 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
6104 return pTokenPair;
6107 /***********************************************************************
6108 * HTTP_ProcessHeader (internal)
6110 * Stuff header into header tables according to <dwModifier>
6114 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
6116 static DWORD HTTP_ProcessHeader(http_request_t *request, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
6118 LPHTTPHEADERW lphttpHdr = NULL;
6119 INT index;
6120 BOOL request_only = !!(dwModifier & HTTP_ADDHDR_FLAG_REQ);
6121 DWORD res = ERROR_HTTP_INVALID_HEADER;
6123 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), dwModifier);
6125 EnterCriticalSection( &request->headers_section );
6127 /* REPLACE wins out over ADD */
6128 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
6129 dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
6131 if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
6132 index = -1;
6133 else
6134 index = HTTP_GetCustomHeaderIndex(request, field, 0, request_only);
6136 if (index >= 0)
6138 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
6140 LeaveCriticalSection( &request->headers_section );
6141 return ERROR_HTTP_INVALID_HEADER;
6143 lphttpHdr = &request->custHeaders[index];
6145 else if (value)
6147 HTTPHEADERW hdr;
6149 hdr.lpszField = (LPWSTR)field;
6150 hdr.lpszValue = (LPWSTR)value;
6151 hdr.wFlags = hdr.wCount = 0;
6153 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6154 hdr.wFlags |= HDR_ISREQUEST;
6156 res = HTTP_InsertCustomHeader(request, &hdr);
6157 LeaveCriticalSection( &request->headers_section );
6158 return res;
6160 /* no value to delete */
6161 else
6163 LeaveCriticalSection( &request->headers_section );
6164 return ERROR_SUCCESS;
6167 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6168 lphttpHdr->wFlags |= HDR_ISREQUEST;
6169 else
6170 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
6172 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
6174 HTTP_DeleteCustomHeader( request, index );
6176 if (value && value[0])
6178 HTTPHEADERW hdr;
6180 hdr.lpszField = (LPWSTR)field;
6181 hdr.lpszValue = (LPWSTR)value;
6182 hdr.wFlags = hdr.wCount = 0;
6184 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6185 hdr.wFlags |= HDR_ISREQUEST;
6187 res = HTTP_InsertCustomHeader(request, &hdr);
6188 LeaveCriticalSection( &request->headers_section );
6189 return res;
6192 LeaveCriticalSection( &request->headers_section );
6193 return ERROR_SUCCESS;
6195 else if (dwModifier & COALESCEFLAGS)
6197 LPWSTR lpsztmp;
6198 WCHAR ch = 0;
6199 INT len = 0;
6200 INT origlen = strlenW(lphttpHdr->lpszValue);
6201 INT valuelen = strlenW(value);
6203 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
6205 ch = ',';
6206 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
6208 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
6210 ch = ';';
6211 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
6214 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
6216 lpsztmp = heap_realloc(lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
6217 if (lpsztmp)
6219 lphttpHdr->lpszValue = lpsztmp;
6220 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
6221 if (ch > 0)
6223 lphttpHdr->lpszValue[origlen] = ch;
6224 origlen++;
6225 lphttpHdr->lpszValue[origlen] = ' ';
6226 origlen++;
6229 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
6230 lphttpHdr->lpszValue[len] = '\0';
6231 res = ERROR_SUCCESS;
6233 else
6235 WARN("heap_realloc (%d bytes) failed\n",len+1);
6236 res = ERROR_OUTOFMEMORY;
6239 TRACE("<-- %d\n", res);
6240 LeaveCriticalSection( &request->headers_section );
6241 return res;
6244 /***********************************************************************
6245 * HTTP_GetCustomHeaderIndex (internal)
6247 * Return index of custom header from header array
6248 * Headers section must be held
6250 static INT HTTP_GetCustomHeaderIndex(http_request_t *request, LPCWSTR lpszField,
6251 int requested_index, BOOL request_only)
6253 DWORD index;
6255 TRACE("%s, %d, %d\n", debugstr_w(lpszField), requested_index, request_only);
6257 for (index = 0; index < request->nCustHeaders; index++)
6259 if (strcmpiW(request->custHeaders[index].lpszField, lpszField))
6260 continue;
6262 if (request_only && !(request->custHeaders[index].wFlags & HDR_ISREQUEST))
6263 continue;
6265 if (!request_only && (request->custHeaders[index].wFlags & HDR_ISREQUEST))
6266 continue;
6268 if (requested_index == 0)
6269 break;
6270 requested_index --;
6273 if (index >= request->nCustHeaders)
6274 index = -1;
6276 TRACE("Return: %d\n", index);
6277 return index;
6281 /***********************************************************************
6282 * HTTP_InsertCustomHeader (internal)
6284 * Insert header into array
6285 * Headers section must be held
6287 static DWORD HTTP_InsertCustomHeader(http_request_t *request, LPHTTPHEADERW lpHdr)
6289 INT count;
6290 LPHTTPHEADERW lph = NULL;
6292 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
6293 count = request->nCustHeaders + 1;
6294 if (count > 1)
6295 lph = heap_realloc_zero(request->custHeaders, sizeof(HTTPHEADERW) * count);
6296 else
6297 lph = heap_alloc_zero(sizeof(HTTPHEADERW) * count);
6299 if (!lph)
6300 return ERROR_OUTOFMEMORY;
6302 request->custHeaders = lph;
6303 request->custHeaders[count-1].lpszField = heap_strdupW(lpHdr->lpszField);
6304 request->custHeaders[count-1].lpszValue = heap_strdupW(lpHdr->lpszValue);
6305 request->custHeaders[count-1].wFlags = lpHdr->wFlags;
6306 request->custHeaders[count-1].wCount= lpHdr->wCount;
6307 request->nCustHeaders++;
6309 return ERROR_SUCCESS;
6313 /***********************************************************************
6314 * HTTP_DeleteCustomHeader (internal)
6316 * Delete header from array
6317 * If this function is called, the index may change.
6318 * Headers section must be held
6320 static BOOL HTTP_DeleteCustomHeader(http_request_t *request, DWORD index)
6322 if( request->nCustHeaders <= 0 )
6323 return FALSE;
6324 if( index >= request->nCustHeaders )
6325 return FALSE;
6326 request->nCustHeaders--;
6328 heap_free(request->custHeaders[index].lpszField);
6329 heap_free(request->custHeaders[index].lpszValue);
6331 memmove( &request->custHeaders[index], &request->custHeaders[index+1],
6332 (request->nCustHeaders - index)* sizeof(HTTPHEADERW) );
6333 memset( &request->custHeaders[request->nCustHeaders], 0, sizeof(HTTPHEADERW) );
6335 return TRUE;
6339 /***********************************************************************
6340 * IsHostInProxyBypassList (@)
6342 * Undocumented
6345 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
6347 FIXME("STUB: flags=%d host=%s length=%d\n",flags,szHost,length);
6348 return FALSE;