wininet: Don't pass BLOCKING_WAITALL to NETCON_recv in netconn_read.
[wine.git] / dlls / wininet / http.c
blobf4f67cb71b04384eb17b8b0341e3aa21c885bbc3
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 #ifdef HAVE_ZLIB
33 # include <zlib.h>
34 #endif
36 #include "winsock2.h"
37 #include "ws2ipdef.h"
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <time.h>
43 #include <assert.h>
44 #include "windef.h"
45 #include "winbase.h"
46 #include "wininet.h"
47 #include "winerror.h"
48 #include "winternl.h"
49 #define NO_SHLWAPI_STREAM
50 #define NO_SHLWAPI_REG
51 #define NO_SHLWAPI_STRFCNS
52 #define NO_SHLWAPI_GDI
53 #include "shlwapi.h"
54 #include "sspi.h"
55 #include "wincrypt.h"
56 #include "winuser.h"
58 #include "internet.h"
59 #include "wine/debug.h"
60 #include "wine/exception.h"
61 #include "wine/unicode.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
65 static const WCHAR g_szHttp1_0[] = {'H','T','T','P','/','1','.','0',0};
66 static const WCHAR g_szHttp1_1[] = {'H','T','T','P','/','1','.','1',0};
67 static const WCHAR szOK[] = {'O','K',0};
68 static const WCHAR hostW[] = { 'H','o','s','t',0 };
69 static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
70 static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
71 static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 };
72 static const WCHAR szKeepAlive[] = {'K','e','e','p','-','A','l','i','v','e',0};
73 static const WCHAR szGET[] = { 'G','E','T', 0 };
74 static const WCHAR szHEAD[] = { 'H','E','A','D', 0 };
76 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
77 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
78 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
79 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
80 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
81 static const WCHAR szAge[] = { 'A','g','e',0 };
82 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
83 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
84 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
85 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
86 static const WCHAR szContent_Disposition[] = { 'C','o','n','t','e','n','t','-','D','i','s','p','o','s','i','t','i','o','n',0 };
87 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
88 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
89 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
90 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
91 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
92 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
93 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
94 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 };
95 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
96 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
97 static const WCHAR szDate[] = { 'D','a','t','e',0 };
98 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
99 static const WCHAR szETag[] = { 'E','T','a','g',0 };
100 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
101 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
102 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
103 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
104 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
105 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
106 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
107 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
108 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
109 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
110 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
111 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
112 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
113 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
114 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
115 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
116 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
117 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
118 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
119 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
120 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
121 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 };
122 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
123 static const WCHAR szURI[] = { 'U','R','I',0 };
124 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
125 static const WCHAR szVary[] = { 'V','a','r','y',0 };
126 static const WCHAR szVia[] = { 'V','i','a',0 };
127 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
128 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
130 static const WCHAR emptyW[] = {0};
132 #define HTTP_REFERER szReferer
133 #define HTTP_ACCEPT szAccept
134 #define HTTP_USERAGENT szUser_Agent
136 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
137 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
138 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
139 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
140 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
141 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
142 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
144 #define COLLECT_TIME 60000
146 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
148 struct HttpAuthInfo
150 LPWSTR scheme;
151 CredHandle cred;
152 CtxtHandle ctx;
153 TimeStamp exp;
154 ULONG attr;
155 ULONG max_token;
156 void *auth_data;
157 unsigned int auth_data_len;
158 BOOL finished; /* finished authenticating */
162 typedef struct _basicAuthorizationData
164 struct list entry;
166 LPWSTR host;
167 LPWSTR realm;
168 LPSTR authorization;
169 UINT authorizationLen;
170 } basicAuthorizationData;
172 typedef struct _authorizationData
174 struct list entry;
176 LPWSTR host;
177 LPWSTR scheme;
178 LPWSTR domain;
179 UINT domain_len;
180 LPWSTR user;
181 UINT user_len;
182 LPWSTR password;
183 UINT password_len;
184 } authorizationData;
186 static struct list basicAuthorizationCache = LIST_INIT(basicAuthorizationCache);
187 static struct list authorizationCache = LIST_INIT(authorizationCache);
189 static CRITICAL_SECTION authcache_cs;
190 static CRITICAL_SECTION_DEBUG critsect_debug =
192 0, 0, &authcache_cs,
193 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
194 0, 0, { (DWORD_PTR)(__FILE__ ": authcache_cs") }
196 static CRITICAL_SECTION authcache_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
198 static DWORD HTTP_GetResponseHeaders(http_request_t *req, INT *len);
199 static DWORD HTTP_ProcessHeader(http_request_t *req, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
200 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
201 static DWORD HTTP_InsertCustomHeader(http_request_t *req, LPHTTPHEADERW lpHdr);
202 static INT HTTP_GetCustomHeaderIndex(http_request_t *req, LPCWSTR lpszField, INT index, BOOL Request);
203 static BOOL HTTP_DeleteCustomHeader(http_request_t *req, DWORD index);
204 static LPWSTR HTTP_build_req( LPCWSTR *list, int len );
205 static DWORD HTTP_HttpQueryInfoW(http_request_t*, DWORD, LPVOID, LPDWORD, LPDWORD);
206 static LPWSTR HTTP_GetRedirectURL(http_request_t *req, LPCWSTR lpszUrl);
207 static UINT HTTP_DecodeBase64(LPCWSTR base64, LPSTR bin);
208 static BOOL drain_content(http_request_t*,BOOL);
210 static CRITICAL_SECTION connection_pool_cs;
211 static CRITICAL_SECTION_DEBUG connection_pool_debug =
213 0, 0, &connection_pool_cs,
214 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
215 0, 0, { (DWORD_PTR)(__FILE__ ": connection_pool_cs") }
217 static CRITICAL_SECTION connection_pool_cs = { &connection_pool_debug, -1, 0, 0, 0, 0 };
219 static struct list connection_pool = LIST_INIT(connection_pool);
220 static BOOL collector_running;
222 void server_addref(server_t *server)
224 InterlockedIncrement(&server->ref);
227 void server_release(server_t *server)
229 if(InterlockedDecrement(&server->ref))
230 return;
232 list_remove(&server->entry);
234 if(server->cert_chain)
235 CertFreeCertificateChain(server->cert_chain);
236 heap_free(server->name);
237 heap_free(server->scheme_host_port);
238 heap_free(server);
241 static BOOL process_host_port(server_t *server)
243 BOOL default_port;
244 size_t name_len;
245 WCHAR *buf;
247 static const WCHAR httpW[] = {'h','t','t','p',0};
248 static const WCHAR httpsW[] = {'h','t','t','p','s',0};
249 static const WCHAR formatW[] = {'%','s',':','/','/','%','s',':','%','u',0};
251 name_len = strlenW(server->name);
252 buf = heap_alloc((name_len + 10 /* strlen("://:<port>") */)*sizeof(WCHAR) + sizeof(httpsW));
253 if(!buf)
254 return FALSE;
256 sprintfW(buf, formatW, server->is_https ? httpsW : httpW, server->name, server->port);
257 server->scheme_host_port = buf;
259 server->host_port = server->scheme_host_port + 7 /* strlen("http://") */;
260 if(server->is_https)
261 server->host_port++;
263 default_port = server->port == (server->is_https ? INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT);
264 server->canon_host_port = default_port ? server->name : server->host_port;
265 return TRUE;
268 server_t *get_server(const WCHAR *name, INTERNET_PORT port, BOOL is_https, BOOL do_create)
270 server_t *iter, *server = NULL;
272 if(port == INTERNET_INVALID_PORT_NUMBER)
273 port = INTERNET_DEFAULT_HTTP_PORT;
275 EnterCriticalSection(&connection_pool_cs);
277 LIST_FOR_EACH_ENTRY(iter, &connection_pool, server_t, entry) {
278 if(iter->port == port && !strcmpW(iter->name, name) && iter->is_https == is_https) {
279 server = iter;
280 server_addref(server);
281 break;
285 if(!server && do_create) {
286 server = heap_alloc_zero(sizeof(*server));
287 if(server) {
288 server->ref = 2; /* list reference and return */
289 server->port = port;
290 server->is_https = is_https;
291 list_init(&server->conn_pool);
292 server->name = heap_strdupW(name);
293 if(server->name && process_host_port(server)) {
294 list_add_head(&connection_pool, &server->entry);
295 }else {
296 heap_free(server);
297 server = NULL;
302 LeaveCriticalSection(&connection_pool_cs);
304 return server;
307 BOOL collect_connections(collect_type_t collect_type)
309 netconn_t *netconn, *netconn_safe;
310 server_t *server, *server_safe;
311 BOOL remaining = FALSE;
312 DWORD64 now;
314 now = GetTickCount64();
316 LIST_FOR_EACH_ENTRY_SAFE(server, server_safe, &connection_pool, server_t, entry) {
317 LIST_FOR_EACH_ENTRY_SAFE(netconn, netconn_safe, &server->conn_pool, netconn_t, pool_entry) {
318 if(collect_type > COLLECT_TIMEOUT || netconn->keep_until < now) {
319 TRACE("freeing %p\n", netconn);
320 list_remove(&netconn->pool_entry);
321 free_netconn(netconn);
322 }else {
323 remaining = TRUE;
327 if(collect_type == COLLECT_CLEANUP) {
328 list_remove(&server->entry);
329 list_init(&server->entry);
330 server_release(server);
334 return remaining;
337 static DWORD WINAPI collect_connections_proc(void *arg)
339 BOOL remaining_conns;
341 do {
342 /* FIXME: Use more sophisticated method */
343 Sleep(5000);
345 EnterCriticalSection(&connection_pool_cs);
347 remaining_conns = collect_connections(COLLECT_TIMEOUT);
348 if(!remaining_conns)
349 collector_running = FALSE;
351 LeaveCriticalSection(&connection_pool_cs);
352 }while(remaining_conns);
354 FreeLibraryAndExitThread(WININET_hModule, 0);
357 /***********************************************************************
358 * HTTP_GetHeader (internal)
360 * Headers section must be held
362 static LPHTTPHEADERW HTTP_GetHeader(http_request_t *req, LPCWSTR head)
364 int HeaderIndex = 0;
365 HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
366 if (HeaderIndex == -1)
367 return NULL;
368 else
369 return &req->custHeaders[HeaderIndex];
372 static WCHAR *get_host_header( http_request_t *req )
374 HTTPHEADERW *header;
375 WCHAR *ret = NULL;
377 EnterCriticalSection( &req->headers_section );
378 if ((header = HTTP_GetHeader( req, hostW ))) ret = heap_strdupW( header->lpszValue );
379 LeaveCriticalSection( &req->headers_section );
380 return ret;
383 struct data_stream_vtbl_t {
384 DWORD (*get_avail_data)(data_stream_t*,http_request_t*);
385 BOOL (*end_of_data)(data_stream_t*,http_request_t*);
386 DWORD (*read)(data_stream_t*,http_request_t*,BYTE*,DWORD,DWORD*,blocking_mode_t);
387 BOOL (*drain_content)(data_stream_t*,http_request_t*);
388 void (*destroy)(data_stream_t*);
391 typedef struct {
392 data_stream_t data_stream;
394 BYTE buf[READ_BUFFER_SIZE];
395 DWORD buf_size;
396 DWORD buf_pos;
397 DWORD chunk_size;
398 BOOL end_of_data;
399 } chunked_stream_t;
401 static inline void destroy_data_stream(data_stream_t *stream)
403 stream->vtbl->destroy(stream);
406 static void reset_data_stream(http_request_t *req)
408 destroy_data_stream(req->data_stream);
409 req->data_stream = &req->netconn_stream.data_stream;
410 req->read_pos = req->read_size = req->netconn_stream.content_read = 0;
411 req->read_gzip = FALSE;
414 static void remove_header( http_request_t *request, const WCHAR *str, BOOL from_request )
416 int index;
417 EnterCriticalSection( &request->headers_section );
418 index = HTTP_GetCustomHeaderIndex( request, str, 0, from_request );
419 if (index != -1) HTTP_DeleteCustomHeader( request, index );
420 LeaveCriticalSection( &request->headers_section );
423 #ifdef HAVE_ZLIB
425 typedef struct {
426 data_stream_t stream;
427 data_stream_t *parent_stream;
428 z_stream zstream;
429 BYTE buf[READ_BUFFER_SIZE];
430 DWORD buf_size;
431 DWORD buf_pos;
432 BOOL end_of_data;
433 } gzip_stream_t;
435 static DWORD gzip_get_avail_data(data_stream_t *stream, http_request_t *req)
437 /* Allow reading only from read buffer */
438 return 0;
441 static BOOL gzip_end_of_data(data_stream_t *stream, http_request_t *req)
443 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
444 return gzip_stream->end_of_data
445 || (!gzip_stream->buf_size && gzip_stream->parent_stream->vtbl->end_of_data(gzip_stream->parent_stream, req));
448 static DWORD gzip_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
449 DWORD *read, blocking_mode_t blocking_mode)
451 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
452 z_stream *zstream = &gzip_stream->zstream;
453 DWORD current_read, ret_read = 0;
454 int zres;
455 DWORD res = ERROR_SUCCESS;
457 TRACE("(%d %d)\n", size, blocking_mode);
459 while(size && !gzip_stream->end_of_data) {
460 if(!gzip_stream->buf_size) {
461 if(gzip_stream->buf_pos) {
462 if(gzip_stream->buf_size)
463 memmove(gzip_stream->buf, gzip_stream->buf+gzip_stream->buf_pos, gzip_stream->buf_size);
464 gzip_stream->buf_pos = 0;
466 res = gzip_stream->parent_stream->vtbl->read(gzip_stream->parent_stream, req, gzip_stream->buf+gzip_stream->buf_size,
467 sizeof(gzip_stream->buf)-gzip_stream->buf_size, &current_read, blocking_mode);
468 gzip_stream->buf_size += current_read;
469 if(res != ERROR_SUCCESS)
470 break;
472 if(!current_read) {
473 if(blocking_mode != BLOCKING_DISALLOW) {
474 WARN("unexpected end of data\n");
475 gzip_stream->end_of_data = TRUE;
477 break;
481 zstream->next_in = gzip_stream->buf+gzip_stream->buf_pos;
482 zstream->avail_in = gzip_stream->buf_size;
483 zstream->next_out = buf+ret_read;
484 zstream->avail_out = size;
485 zres = inflate(&gzip_stream->zstream, 0);
486 current_read = size - zstream->avail_out;
487 size -= current_read;
488 ret_read += current_read;
489 gzip_stream->buf_size -= zstream->next_in - (gzip_stream->buf+gzip_stream->buf_pos);
490 gzip_stream->buf_pos = zstream->next_in-gzip_stream->buf;
491 if(zres == Z_STREAM_END) {
492 TRACE("end of data\n");
493 gzip_stream->end_of_data = TRUE;
494 inflateEnd(zstream);
495 }else if(zres != Z_OK) {
496 WARN("inflate failed %d: %s\n", zres, debugstr_a(zstream->msg));
497 if(!ret_read)
498 res = ERROR_INTERNET_DECODING_FAILED;
499 break;
502 if(ret_read && blocking_mode == BLOCKING_ALLOW)
503 blocking_mode = BLOCKING_DISALLOW;
506 TRACE("read %u bytes\n", ret_read);
507 *read = ret_read;
508 return res;
511 static BOOL gzip_drain_content(data_stream_t *stream, http_request_t *req)
513 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
514 return gzip_stream->parent_stream->vtbl->drain_content(gzip_stream->parent_stream, req);
517 static void gzip_destroy(data_stream_t *stream)
519 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
521 destroy_data_stream(gzip_stream->parent_stream);
523 if(!gzip_stream->end_of_data)
524 inflateEnd(&gzip_stream->zstream);
525 heap_free(gzip_stream);
528 static const data_stream_vtbl_t gzip_stream_vtbl = {
529 gzip_get_avail_data,
530 gzip_end_of_data,
531 gzip_read,
532 gzip_drain_content,
533 gzip_destroy
536 static voidpf wininet_zalloc(voidpf opaque, uInt items, uInt size)
538 return heap_alloc(items*size);
541 static void wininet_zfree(voidpf opaque, voidpf address)
543 heap_free(address);
546 static DWORD init_gzip_stream(http_request_t *req, BOOL is_gzip)
548 gzip_stream_t *gzip_stream;
549 int zres;
551 gzip_stream = heap_alloc_zero(sizeof(gzip_stream_t));
552 if(!gzip_stream)
553 return ERROR_OUTOFMEMORY;
555 gzip_stream->stream.vtbl = &gzip_stream_vtbl;
556 gzip_stream->zstream.zalloc = wininet_zalloc;
557 gzip_stream->zstream.zfree = wininet_zfree;
559 zres = inflateInit2(&gzip_stream->zstream, is_gzip ? 0x1f : -15);
560 if(zres != Z_OK) {
561 ERR("inflateInit failed: %d\n", zres);
562 heap_free(gzip_stream);
563 return ERROR_OUTOFMEMORY;
566 remove_header(req, szContent_Length, FALSE);
568 if(req->read_size) {
569 memcpy(gzip_stream->buf, req->read_buf+req->read_pos, req->read_size);
570 gzip_stream->buf_size = req->read_size;
571 req->read_pos = req->read_size = 0;
574 req->read_gzip = TRUE;
575 gzip_stream->parent_stream = req->data_stream;
576 req->data_stream = &gzip_stream->stream;
577 return ERROR_SUCCESS;
580 #else
582 static DWORD init_gzip_stream(http_request_t *req, BOOL is_gzip)
584 ERR("gzip stream not supported, missing zlib.\n");
585 return ERROR_SUCCESS;
588 #endif
590 /***********************************************************************
591 * HTTP_FreeTokens (internal)
593 * Frees table of pointers.
595 static void HTTP_FreeTokens(LPWSTR * token_array)
597 int i;
598 for (i = 0; token_array[i]; i++) heap_free(token_array[i]);
599 heap_free(token_array);
602 static void HTTP_FixURL(http_request_t *request)
604 static const WCHAR szSlash[] = { '/',0 };
605 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
607 /* If we don't have a path we set it to root */
608 if (NULL == request->path)
609 request->path = heap_strdupW(szSlash);
610 else /* remove \r and \n*/
612 int nLen = strlenW(request->path);
613 while ((nLen >0 ) && ((request->path[nLen-1] == '\r')||(request->path[nLen-1] == '\n')))
615 nLen--;
616 request->path[nLen]='\0';
618 /* Replace '\' with '/' */
619 while (nLen>0) {
620 nLen--;
621 if (request->path[nLen] == '\\') request->path[nLen]='/';
625 if(CSTR_EQUAL != CompareStringW( LOCALE_INVARIANT, NORM_IGNORECASE,
626 request->path, strlenW(request->path), szHttp, strlenW(szHttp) )
627 && request->path[0] != '/') /* not an absolute path ?? --> fix it !! */
629 WCHAR *fixurl = heap_alloc((strlenW(request->path) + 2)*sizeof(WCHAR));
630 *fixurl = '/';
631 strcpyW(fixurl + 1, request->path);
632 heap_free( request->path );
633 request->path = fixurl;
637 static WCHAR* build_request_header(http_request_t *request, const WCHAR *verb,
638 const WCHAR *path, const WCHAR *version, BOOL use_cr)
640 static const WCHAR szSpace[] = {' ',0};
641 static const WCHAR szColon[] = {':',' ',0};
642 static const WCHAR szCr[] = {'\r',0};
643 static const WCHAR szLf[] = {'\n',0};
644 LPWSTR requestString;
645 DWORD len, n;
646 LPCWSTR *req;
647 UINT i;
649 EnterCriticalSection( &request->headers_section );
651 /* allocate space for an array of all the string pointers to be added */
652 len = request->nCustHeaders * 5 + 10;
653 if (!(req = heap_alloc( len * sizeof(const WCHAR *) )))
655 LeaveCriticalSection( &request->headers_section );
656 return NULL;
659 /* add the verb, path and HTTP version string */
660 n = 0;
661 req[n++] = verb;
662 req[n++] = szSpace;
663 req[n++] = path;
664 req[n++] = szSpace;
665 req[n++] = version;
666 if (use_cr)
667 req[n++] = szCr;
668 req[n++] = szLf;
670 /* Append custom request headers */
671 for (i = 0; i < request->nCustHeaders; i++)
673 if (request->custHeaders[i].wFlags & HDR_ISREQUEST)
675 req[n++] = request->custHeaders[i].lpszField;
676 req[n++] = szColon;
677 req[n++] = request->custHeaders[i].lpszValue;
678 if (use_cr)
679 req[n++] = szCr;
680 req[n++] = szLf;
682 TRACE("Adding custom header %s (%s)\n",
683 debugstr_w(request->custHeaders[i].lpszField),
684 debugstr_w(request->custHeaders[i].lpszValue));
687 if (use_cr)
688 req[n++] = szCr;
689 req[n++] = szLf;
690 req[n] = NULL;
692 requestString = HTTP_build_req( req, 4 );
693 heap_free( req );
694 LeaveCriticalSection( &request->headers_section );
695 return requestString;
698 static WCHAR* build_response_header(http_request_t *request, BOOL use_cr)
700 static const WCHAR colonW[] = { ':',' ',0 };
701 static const WCHAR crW[] = { '\r',0 };
702 static const WCHAR lfW[] = { '\n',0 };
703 static const WCHAR status_fmt[] = { ' ','%','u',' ',0 };
704 const WCHAR **req;
705 WCHAR *ret, buf[14];
706 DWORD i, n = 0;
708 EnterCriticalSection( &request->headers_section );
710 if (!(req = heap_alloc( (request->nCustHeaders * 5 + 8) * sizeof(WCHAR *) )))
712 LeaveCriticalSection( &request->headers_section );
713 return NULL;
716 if (request->status_code)
718 req[n++] = request->version;
719 sprintfW(buf, status_fmt, request->status_code);
720 req[n++] = buf;
721 req[n++] = request->statusText;
722 if (use_cr)
723 req[n++] = crW;
724 req[n++] = lfW;
727 for(i = 0; i < request->nCustHeaders; i++)
729 if(!(request->custHeaders[i].wFlags & HDR_ISREQUEST)
730 && strcmpW(request->custHeaders[i].lpszField, szStatus))
732 req[n++] = request->custHeaders[i].lpszField;
733 req[n++] = colonW;
734 req[n++] = request->custHeaders[i].lpszValue;
735 if(use_cr)
736 req[n++] = crW;
737 req[n++] = lfW;
739 TRACE("Adding custom header %s (%s)\n",
740 debugstr_w(request->custHeaders[i].lpszField),
741 debugstr_w(request->custHeaders[i].lpszValue));
744 if(use_cr)
745 req[n++] = crW;
746 req[n++] = lfW;
747 req[n] = NULL;
749 ret = HTTP_build_req(req, 0);
750 heap_free(req);
751 LeaveCriticalSection( &request->headers_section );
752 return ret;
755 static void HTTP_ProcessCookies( http_request_t *request )
757 int HeaderIndex;
758 int numCookies = 0;
759 LPHTTPHEADERW setCookieHeader;
761 if(request->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES)
762 return;
764 EnterCriticalSection( &request->headers_section );
766 while((HeaderIndex = HTTP_GetCustomHeaderIndex(request, szSet_Cookie, numCookies++, FALSE)) != -1)
768 HTTPHEADERW *host;
769 const WCHAR *data;
770 WCHAR *name;
772 setCookieHeader = &request->custHeaders[HeaderIndex];
774 if (!setCookieHeader->lpszValue)
775 continue;
777 host = HTTP_GetHeader(request, hostW);
778 if(!host)
779 continue;
781 data = strchrW(setCookieHeader->lpszValue, '=');
782 if(!data)
783 continue;
785 name = heap_strndupW(setCookieHeader->lpszValue, data-setCookieHeader->lpszValue);
786 if(!name)
787 continue;
789 data++;
790 set_cookie(host->lpszValue, request->path, name, data, INTERNET_COOKIE_HTTPONLY);
791 heap_free(name);
794 LeaveCriticalSection( &request->headers_section );
797 static void strip_spaces(LPWSTR start)
799 LPWSTR str = start;
800 LPWSTR end;
802 while (*str == ' ')
803 str++;
805 if (str != start)
806 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
808 end = start + strlenW(start) - 1;
809 while (end >= start && *end == ' ')
811 *end = '\0';
812 end--;
816 static inline BOOL is_basic_auth_value( LPCWSTR pszAuthValue, LPWSTR *pszRealm )
818 static const WCHAR szBasic[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
819 static const WCHAR szRealm[] = {'r','e','a','l','m'}; /* Note: not nul-terminated */
820 BOOL is_basic;
821 is_basic = !strncmpiW(pszAuthValue, szBasic, ARRAYSIZE(szBasic)) &&
822 ((pszAuthValue[ARRAYSIZE(szBasic)] == ' ') || !pszAuthValue[ARRAYSIZE(szBasic)]);
823 if (is_basic && pszRealm)
825 LPCWSTR token;
826 LPCWSTR ptr = &pszAuthValue[ARRAYSIZE(szBasic)];
827 LPCWSTR realm;
828 ptr++;
829 *pszRealm=NULL;
830 token = strchrW(ptr,'=');
831 if (!token)
832 return TRUE;
833 realm = ptr;
834 while (*realm == ' ')
835 realm++;
836 if(!strncmpiW(realm, szRealm, ARRAYSIZE(szRealm)) &&
837 (realm[ARRAYSIZE(szRealm)] == ' ' || realm[ARRAYSIZE(szRealm)] == '='))
839 token++;
840 while (*token == ' ')
841 token++;
842 if (*token == '\0')
843 return TRUE;
844 *pszRealm = heap_strdupW(token);
845 strip_spaces(*pszRealm);
849 return is_basic;
852 static void destroy_authinfo( struct HttpAuthInfo *authinfo )
854 if (!authinfo) return;
856 if (SecIsValidHandle(&authinfo->ctx))
857 DeleteSecurityContext(&authinfo->ctx);
858 if (SecIsValidHandle(&authinfo->cred))
859 FreeCredentialsHandle(&authinfo->cred);
861 heap_free(authinfo->auth_data);
862 heap_free(authinfo->scheme);
863 heap_free(authinfo);
866 static UINT retrieve_cached_basic_authorization(const WCHAR *host, const WCHAR *realm, char **auth_data)
868 basicAuthorizationData *ad;
869 UINT rc = 0;
871 TRACE("Looking for authorization for %s:%s\n",debugstr_w(host),debugstr_w(realm));
873 EnterCriticalSection(&authcache_cs);
874 LIST_FOR_EACH_ENTRY(ad, &basicAuthorizationCache, basicAuthorizationData, entry)
876 if (!strcmpiW(host, ad->host) && (!realm || !strcmpW(realm, ad->realm)))
878 TRACE("Authorization found in cache\n");
879 *auth_data = heap_alloc(ad->authorizationLen);
880 memcpy(*auth_data,ad->authorization,ad->authorizationLen);
881 rc = ad->authorizationLen;
882 break;
885 LeaveCriticalSection(&authcache_cs);
886 return rc;
889 static void cache_basic_authorization(LPWSTR host, LPWSTR realm, LPSTR auth_data, UINT auth_data_len)
891 struct list *cursor;
892 basicAuthorizationData* ad = NULL;
894 TRACE("caching authorization for %s:%s = %s\n",debugstr_w(host),debugstr_w(realm),debugstr_an(auth_data,auth_data_len));
896 EnterCriticalSection(&authcache_cs);
897 LIST_FOR_EACH(cursor, &basicAuthorizationCache)
899 basicAuthorizationData *check = LIST_ENTRY(cursor,basicAuthorizationData,entry);
900 if (!strcmpiW(host,check->host) && !strcmpW(realm,check->realm))
902 ad = check;
903 break;
907 if (ad)
909 TRACE("Found match in cache, replacing\n");
910 heap_free(ad->authorization);
911 ad->authorization = heap_alloc(auth_data_len);
912 memcpy(ad->authorization, auth_data, auth_data_len);
913 ad->authorizationLen = auth_data_len;
915 else
917 ad = heap_alloc(sizeof(basicAuthorizationData));
918 ad->host = heap_strdupW(host);
919 ad->realm = heap_strdupW(realm);
920 ad->authorization = heap_alloc(auth_data_len);
921 memcpy(ad->authorization, auth_data, auth_data_len);
922 ad->authorizationLen = auth_data_len;
923 list_add_head(&basicAuthorizationCache,&ad->entry);
924 TRACE("authorization cached\n");
926 LeaveCriticalSection(&authcache_cs);
929 static BOOL retrieve_cached_authorization(LPWSTR host, LPWSTR scheme,
930 SEC_WINNT_AUTH_IDENTITY_W *nt_auth_identity)
932 authorizationData *ad;
934 TRACE("Looking for authorization for %s:%s\n", debugstr_w(host), debugstr_w(scheme));
936 EnterCriticalSection(&authcache_cs);
937 LIST_FOR_EACH_ENTRY(ad, &authorizationCache, authorizationData, entry) {
938 if(!strcmpiW(host, ad->host) && !strcmpiW(scheme, ad->scheme)) {
939 TRACE("Authorization found in cache\n");
941 nt_auth_identity->User = heap_strdupW(ad->user);
942 nt_auth_identity->Password = heap_strdupW(ad->password);
943 nt_auth_identity->Domain = heap_alloc(sizeof(WCHAR)*ad->domain_len);
944 if(!nt_auth_identity->User || !nt_auth_identity->Password ||
945 (!nt_auth_identity->Domain && ad->domain_len)) {
946 heap_free(nt_auth_identity->User);
947 heap_free(nt_auth_identity->Password);
948 heap_free(nt_auth_identity->Domain);
949 break;
952 nt_auth_identity->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
953 nt_auth_identity->UserLength = ad->user_len;
954 nt_auth_identity->PasswordLength = ad->password_len;
955 memcpy(nt_auth_identity->Domain, ad->domain, sizeof(WCHAR)*ad->domain_len);
956 nt_auth_identity->DomainLength = ad->domain_len;
957 LeaveCriticalSection(&authcache_cs);
958 return TRUE;
961 LeaveCriticalSection(&authcache_cs);
963 return FALSE;
966 static void cache_authorization(LPWSTR host, LPWSTR scheme,
967 SEC_WINNT_AUTH_IDENTITY_W *nt_auth_identity)
969 authorizationData *ad;
970 BOOL found = FALSE;
972 TRACE("Caching authorization for %s:%s\n", debugstr_w(host), debugstr_w(scheme));
974 EnterCriticalSection(&authcache_cs);
975 LIST_FOR_EACH_ENTRY(ad, &authorizationCache, authorizationData, entry)
976 if(!strcmpiW(host, ad->host) && !strcmpiW(scheme, ad->scheme)) {
977 found = TRUE;
978 break;
981 if(found) {
982 heap_free(ad->user);
983 heap_free(ad->password);
984 heap_free(ad->domain);
985 } else {
986 ad = heap_alloc(sizeof(authorizationData));
987 if(!ad) {
988 LeaveCriticalSection(&authcache_cs);
989 return;
992 ad->host = heap_strdupW(host);
993 ad->scheme = heap_strdupW(scheme);
994 list_add_head(&authorizationCache, &ad->entry);
997 ad->user = heap_strndupW(nt_auth_identity->User, nt_auth_identity->UserLength);
998 ad->password = heap_strndupW(nt_auth_identity->Password, nt_auth_identity->PasswordLength);
999 ad->domain = heap_strndupW(nt_auth_identity->Domain, nt_auth_identity->DomainLength);
1000 ad->user_len = nt_auth_identity->UserLength;
1001 ad->password_len = nt_auth_identity->PasswordLength;
1002 ad->domain_len = nt_auth_identity->DomainLength;
1004 if(!ad->host || !ad->scheme || !ad->user || !ad->password
1005 || (nt_auth_identity->Domain && !ad->domain)) {
1006 heap_free(ad->host);
1007 heap_free(ad->scheme);
1008 heap_free(ad->user);
1009 heap_free(ad->password);
1010 heap_free(ad->domain);
1011 list_remove(&ad->entry);
1012 heap_free(ad);
1015 LeaveCriticalSection(&authcache_cs);
1018 static BOOL HTTP_DoAuthorization( http_request_t *request, LPCWSTR pszAuthValue,
1019 struct HttpAuthInfo **ppAuthInfo,
1020 LPWSTR domain_and_username, LPWSTR password,
1021 LPWSTR host )
1023 SECURITY_STATUS sec_status;
1024 struct HttpAuthInfo *pAuthInfo = *ppAuthInfo;
1025 BOOL first = FALSE;
1026 LPWSTR szRealm = NULL;
1028 TRACE("%s\n", debugstr_w(pszAuthValue));
1030 if (!pAuthInfo)
1032 TimeStamp exp;
1034 first = TRUE;
1035 pAuthInfo = heap_alloc(sizeof(*pAuthInfo));
1036 if (!pAuthInfo)
1037 return FALSE;
1039 SecInvalidateHandle(&pAuthInfo->cred);
1040 SecInvalidateHandle(&pAuthInfo->ctx);
1041 memset(&pAuthInfo->exp, 0, sizeof(pAuthInfo->exp));
1042 pAuthInfo->attr = 0;
1043 pAuthInfo->auth_data = NULL;
1044 pAuthInfo->auth_data_len = 0;
1045 pAuthInfo->finished = FALSE;
1047 if (is_basic_auth_value(pszAuthValue,NULL))
1049 static const WCHAR szBasic[] = {'B','a','s','i','c',0};
1050 pAuthInfo->scheme = heap_strdupW(szBasic);
1051 if (!pAuthInfo->scheme)
1053 heap_free(pAuthInfo);
1054 return FALSE;
1057 else
1059 PVOID pAuthData;
1060 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity;
1062 pAuthInfo->scheme = heap_strdupW(pszAuthValue);
1063 if (!pAuthInfo->scheme)
1065 heap_free(pAuthInfo);
1066 return FALSE;
1069 if (domain_and_username)
1071 WCHAR *user = strchrW(domain_and_username, '\\');
1072 WCHAR *domain = domain_and_username;
1074 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
1076 pAuthData = &nt_auth_identity;
1078 if (user) user++;
1079 else
1081 user = domain_and_username;
1082 domain = NULL;
1085 nt_auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
1086 nt_auth_identity.User = user;
1087 nt_auth_identity.UserLength = strlenW(nt_auth_identity.User);
1088 nt_auth_identity.Domain = domain;
1089 nt_auth_identity.DomainLength = domain ? user - domain - 1 : 0;
1090 nt_auth_identity.Password = password;
1091 nt_auth_identity.PasswordLength = strlenW(nt_auth_identity.Password);
1093 cache_authorization(host, pAuthInfo->scheme, &nt_auth_identity);
1095 else if(retrieve_cached_authorization(host, pAuthInfo->scheme, &nt_auth_identity))
1096 pAuthData = &nt_auth_identity;
1097 else
1098 /* use default credentials */
1099 pAuthData = NULL;
1101 sec_status = AcquireCredentialsHandleW(NULL, pAuthInfo->scheme,
1102 SECPKG_CRED_OUTBOUND, NULL,
1103 pAuthData, NULL,
1104 NULL, &pAuthInfo->cred,
1105 &exp);
1107 if(pAuthData && !domain_and_username) {
1108 heap_free(nt_auth_identity.User);
1109 heap_free(nt_auth_identity.Domain);
1110 heap_free(nt_auth_identity.Password);
1113 if (sec_status == SEC_E_OK)
1115 PSecPkgInfoW sec_pkg_info;
1116 sec_status = QuerySecurityPackageInfoW(pAuthInfo->scheme, &sec_pkg_info);
1117 if (sec_status == SEC_E_OK)
1119 pAuthInfo->max_token = sec_pkg_info->cbMaxToken;
1120 FreeContextBuffer(sec_pkg_info);
1123 if (sec_status != SEC_E_OK)
1125 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
1126 debugstr_w(pAuthInfo->scheme), sec_status);
1127 heap_free(pAuthInfo->scheme);
1128 heap_free(pAuthInfo);
1129 return FALSE;
1132 *ppAuthInfo = pAuthInfo;
1134 else if (pAuthInfo->finished)
1135 return FALSE;
1137 if ((strlenW(pszAuthValue) < strlenW(pAuthInfo->scheme)) ||
1138 strncmpiW(pszAuthValue, pAuthInfo->scheme, strlenW(pAuthInfo->scheme)))
1140 ERR("authentication scheme changed from %s to %s\n",
1141 debugstr_w(pAuthInfo->scheme), debugstr_w(pszAuthValue));
1142 return FALSE;
1145 if (is_basic_auth_value(pszAuthValue,&szRealm))
1147 int userlen;
1148 int passlen;
1149 char *auth_data = NULL;
1150 UINT auth_data_len = 0;
1152 TRACE("basic authentication realm %s\n",debugstr_w(szRealm));
1154 if (!domain_and_username)
1156 if (host && szRealm)
1157 auth_data_len = retrieve_cached_basic_authorization(host, szRealm,&auth_data);
1158 if (auth_data_len == 0)
1160 heap_free(szRealm);
1161 return FALSE;
1164 else
1166 userlen = WideCharToMultiByte(CP_UTF8, 0, domain_and_username, lstrlenW(domain_and_username), NULL, 0, NULL, NULL);
1167 passlen = WideCharToMultiByte(CP_UTF8, 0, password, lstrlenW(password), NULL, 0, NULL, NULL);
1169 /* length includes a nul terminator, which will be re-used for the ':' */
1170 auth_data = heap_alloc(userlen + 1 + passlen);
1171 if (!auth_data)
1173 heap_free(szRealm);
1174 return FALSE;
1177 WideCharToMultiByte(CP_UTF8, 0, domain_and_username, -1, auth_data, userlen, NULL, NULL);
1178 auth_data[userlen] = ':';
1179 WideCharToMultiByte(CP_UTF8, 0, password, -1, &auth_data[userlen+1], passlen, NULL, NULL);
1180 auth_data_len = userlen + 1 + passlen;
1181 if (host && szRealm)
1182 cache_basic_authorization(host, szRealm, auth_data, auth_data_len);
1185 pAuthInfo->auth_data = auth_data;
1186 pAuthInfo->auth_data_len = auth_data_len;
1187 pAuthInfo->finished = TRUE;
1188 heap_free(szRealm);
1189 return TRUE;
1191 else
1193 LPCWSTR pszAuthData;
1194 SecBufferDesc out_desc, in_desc;
1195 SecBuffer out, in;
1196 unsigned char *buffer;
1197 ULONG context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE |
1198 ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE;
1200 in.BufferType = SECBUFFER_TOKEN;
1201 in.cbBuffer = 0;
1202 in.pvBuffer = NULL;
1204 in_desc.ulVersion = 0;
1205 in_desc.cBuffers = 1;
1206 in_desc.pBuffers = &in;
1208 pszAuthData = pszAuthValue + strlenW(pAuthInfo->scheme);
1209 if (*pszAuthData == ' ')
1211 pszAuthData++;
1212 in.cbBuffer = HTTP_DecodeBase64(pszAuthData, NULL);
1213 in.pvBuffer = heap_alloc(in.cbBuffer);
1214 HTTP_DecodeBase64(pszAuthData, in.pvBuffer);
1217 buffer = heap_alloc(pAuthInfo->max_token);
1219 out.BufferType = SECBUFFER_TOKEN;
1220 out.cbBuffer = pAuthInfo->max_token;
1221 out.pvBuffer = buffer;
1223 out_desc.ulVersion = 0;
1224 out_desc.cBuffers = 1;
1225 out_desc.pBuffers = &out;
1227 sec_status = InitializeSecurityContextW(first ? &pAuthInfo->cred : NULL,
1228 first ? NULL : &pAuthInfo->ctx,
1229 first ? request->server->name : NULL,
1230 context_req, 0, SECURITY_NETWORK_DREP,
1231 in.pvBuffer ? &in_desc : NULL,
1232 0, &pAuthInfo->ctx, &out_desc,
1233 &pAuthInfo->attr, &pAuthInfo->exp);
1234 if (sec_status == SEC_E_OK)
1236 pAuthInfo->finished = TRUE;
1237 pAuthInfo->auth_data = out.pvBuffer;
1238 pAuthInfo->auth_data_len = out.cbBuffer;
1239 TRACE("sending last auth packet\n");
1241 else if (sec_status == SEC_I_CONTINUE_NEEDED)
1243 pAuthInfo->auth_data = out.pvBuffer;
1244 pAuthInfo->auth_data_len = out.cbBuffer;
1245 TRACE("sending next auth packet\n");
1247 else
1249 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status);
1250 heap_free(out.pvBuffer);
1251 destroy_authinfo(pAuthInfo);
1252 *ppAuthInfo = NULL;
1253 return FALSE;
1257 return TRUE;
1260 /***********************************************************************
1261 * HTTP_HttpAddRequestHeadersW (internal)
1263 static DWORD HTTP_HttpAddRequestHeadersW(http_request_t *request,
1264 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1266 LPWSTR lpszStart;
1267 LPWSTR lpszEnd;
1268 LPWSTR buffer;
1269 DWORD len, res = ERROR_HTTP_INVALID_HEADER;
1271 TRACE("copying header: %s\n", debugstr_wn(lpszHeader, dwHeaderLength));
1273 if( dwHeaderLength == ~0U )
1274 len = strlenW(lpszHeader);
1275 else
1276 len = dwHeaderLength;
1277 buffer = heap_alloc(sizeof(WCHAR)*(len+1));
1278 lstrcpynW( buffer, lpszHeader, len + 1);
1280 lpszStart = buffer;
1284 LPWSTR * pFieldAndValue;
1286 lpszEnd = lpszStart;
1288 while (*lpszEnd != '\0')
1290 if (*lpszEnd == '\r' || *lpszEnd == '\n')
1291 break;
1292 lpszEnd++;
1295 if (*lpszStart == '\0')
1296 break;
1298 if (*lpszEnd == '\r' || *lpszEnd == '\n')
1300 *lpszEnd = '\0';
1301 lpszEnd++; /* Jump over newline */
1303 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
1304 if (*lpszStart == '\0')
1306 /* Skip 0-length headers */
1307 lpszStart = lpszEnd;
1308 res = ERROR_SUCCESS;
1309 continue;
1311 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
1312 if (pFieldAndValue)
1314 res = HTTP_ProcessHeader(request, pFieldAndValue[0],
1315 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
1316 HTTP_FreeTokens(pFieldAndValue);
1319 lpszStart = lpszEnd;
1320 } while (res == ERROR_SUCCESS);
1322 heap_free(buffer);
1323 return res;
1326 /***********************************************************************
1327 * HttpAddRequestHeadersW (WININET.@)
1329 * Adds one or more HTTP header to the request handler
1331 * NOTE
1332 * On Windows if dwHeaderLength includes the trailing '\0', then
1333 * HttpAddRequestHeadersW() adds it too. However this results in an
1334 * invalid HTTP header which is rejected by some servers so we probably
1335 * don't need to match Windows on that point.
1337 * RETURNS
1338 * TRUE on success
1339 * FALSE on failure
1342 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
1343 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1345 http_request_t *request;
1346 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1348 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_wn(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
1350 if (!lpszHeader)
1351 return TRUE;
1353 request = (http_request_t*) get_handle_object( hHttpRequest );
1354 if (request && request->hdr.htype == WH_HHTTPREQ)
1355 res = HTTP_HttpAddRequestHeadersW( request, lpszHeader, dwHeaderLength, dwModifier );
1356 if( request )
1357 WININET_Release( &request->hdr );
1359 if(res != ERROR_SUCCESS)
1360 SetLastError(res);
1361 return res == ERROR_SUCCESS;
1364 /***********************************************************************
1365 * HttpAddRequestHeadersA (WININET.@)
1367 * Adds one or more HTTP header to the request handler
1369 * RETURNS
1370 * TRUE on success
1371 * FALSE on failure
1374 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
1375 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1377 DWORD len;
1378 LPWSTR hdr;
1379 BOOL r;
1381 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_an(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
1383 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
1384 hdr = heap_alloc(len*sizeof(WCHAR));
1385 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
1386 if( dwHeaderLength != ~0U )
1387 dwHeaderLength = len;
1389 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
1391 heap_free( hdr );
1392 return r;
1395 static void free_accept_types( WCHAR **accept_types )
1397 WCHAR *ptr, **types = accept_types;
1399 if (!types) return;
1400 while ((ptr = *types))
1402 heap_free( ptr );
1403 types++;
1405 heap_free( accept_types );
1408 static WCHAR **convert_accept_types( const char **accept_types )
1410 unsigned int count;
1411 const char **types = accept_types;
1412 WCHAR **typesW;
1413 BOOL invalid_pointer = FALSE;
1415 if (!types) return NULL;
1416 count = 0;
1417 while (*types)
1419 __TRY
1421 /* find out how many there are */
1422 if (*types && **types)
1424 TRACE("accept type: %s\n", debugstr_a(*types));
1425 count++;
1428 __EXCEPT_PAGE_FAULT
1430 WARN("invalid accept type pointer\n");
1431 invalid_pointer = TRUE;
1433 __ENDTRY;
1434 types++;
1436 if (invalid_pointer) return NULL;
1437 if (!(typesW = heap_alloc( sizeof(WCHAR *) * (count + 1) ))) return NULL;
1438 count = 0;
1439 types = accept_types;
1440 while (*types)
1442 if (*types && **types) typesW[count++] = heap_strdupAtoW( *types );
1443 types++;
1445 typesW[count] = NULL;
1446 return typesW;
1449 /***********************************************************************
1450 * HttpOpenRequestA (WININET.@)
1452 * Open a HTTP request handle
1454 * RETURNS
1455 * HINTERNET a HTTP request handle on success
1456 * NULL on failure
1459 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
1460 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
1461 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
1462 DWORD dwFlags, DWORD_PTR dwContext)
1464 LPWSTR szVerb = NULL, szObjectName = NULL;
1465 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
1466 HINTERNET rc = NULL;
1468 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
1469 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
1470 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
1471 dwFlags, dwContext);
1473 if (lpszVerb)
1475 szVerb = heap_strdupAtoW(lpszVerb);
1476 if ( !szVerb )
1477 goto end;
1480 if (lpszObjectName)
1482 szObjectName = heap_strdupAtoW(lpszObjectName);
1483 if ( !szObjectName )
1484 goto end;
1487 if (lpszVersion)
1489 szVersion = heap_strdupAtoW(lpszVersion);
1490 if ( !szVersion )
1491 goto end;
1494 if (lpszReferrer)
1496 szReferrer = heap_strdupAtoW(lpszReferrer);
1497 if ( !szReferrer )
1498 goto end;
1501 szAcceptTypes = convert_accept_types( lpszAcceptTypes );
1502 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName, szVersion, szReferrer,
1503 (const WCHAR **)szAcceptTypes, dwFlags, dwContext);
1505 end:
1506 free_accept_types(szAcceptTypes);
1507 heap_free(szReferrer);
1508 heap_free(szVersion);
1509 heap_free(szObjectName);
1510 heap_free(szVerb);
1511 return rc;
1514 /***********************************************************************
1515 * HTTP_EncodeBase64
1517 static UINT HTTP_EncodeBase64( LPCSTR bin, unsigned int len, LPWSTR base64 )
1519 UINT n = 0, x;
1520 static const CHAR HTTP_Base64Enc[] =
1521 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1523 while( len > 0 )
1525 /* first 6 bits, all from bin[0] */
1526 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
1527 x = (bin[0] & 3) << 4;
1529 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1530 if( len == 1 )
1532 base64[n++] = HTTP_Base64Enc[x];
1533 base64[n++] = '=';
1534 base64[n++] = '=';
1535 break;
1537 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
1538 x = ( bin[1] & 0x0f ) << 2;
1540 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1541 if( len == 2 )
1543 base64[n++] = HTTP_Base64Enc[x];
1544 base64[n++] = '=';
1545 break;
1547 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
1549 /* last 6 bits, all from bin [2] */
1550 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
1551 bin += 3;
1552 len -= 3;
1554 base64[n] = 0;
1555 return n;
1558 static const signed char HTTP_Base64Dec[] =
1560 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00 */
1561 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10 */
1562 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20 */
1563 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 0x30 */
1564 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40 */
1565 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50 */
1566 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60 */
1567 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 0x70 */
1570 /***********************************************************************
1571 * HTTP_DecodeBase64
1573 static UINT HTTP_DecodeBase64( LPCWSTR base64, LPSTR bin )
1575 unsigned int n = 0;
1577 while(*base64)
1579 signed char in[4];
1581 if (base64[0] >= ARRAYSIZE(HTTP_Base64Dec) ||
1582 ((in[0] = HTTP_Base64Dec[base64[0]]) == -1) ||
1583 base64[1] >= ARRAYSIZE(HTTP_Base64Dec) ||
1584 ((in[1] = HTTP_Base64Dec[base64[1]]) == -1))
1586 WARN("invalid base64: %s\n", debugstr_w(base64));
1587 return 0;
1589 if (bin)
1590 bin[n] = (unsigned char) (in[0] << 2 | in[1] >> 4);
1591 n++;
1593 if ((base64[2] == '=') && (base64[3] == '='))
1594 break;
1595 if (base64[2] > ARRAYSIZE(HTTP_Base64Dec) ||
1596 ((in[2] = HTTP_Base64Dec[base64[2]]) == -1))
1598 WARN("invalid base64: %s\n", debugstr_w(&base64[2]));
1599 return 0;
1601 if (bin)
1602 bin[n] = (unsigned char) (in[1] << 4 | in[2] >> 2);
1603 n++;
1605 if (base64[3] == '=')
1606 break;
1607 if (base64[3] > ARRAYSIZE(HTTP_Base64Dec) ||
1608 ((in[3] = HTTP_Base64Dec[base64[3]]) == -1))
1610 WARN("invalid base64: %s\n", debugstr_w(&base64[3]));
1611 return 0;
1613 if (bin)
1614 bin[n] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
1615 n++;
1617 base64 += 4;
1620 return n;
1623 static WCHAR *encode_auth_data( const WCHAR *scheme, const char *data, UINT data_len )
1625 WCHAR *ret;
1626 UINT len, scheme_len = strlenW( scheme );
1628 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1629 len = scheme_len + 1 + ((data_len + 2) * 4) / 3;
1630 if (!(ret = heap_alloc( (len + 1) * sizeof(WCHAR) ))) return NULL;
1631 memcpy( ret, scheme, scheme_len * sizeof(WCHAR) );
1632 ret[scheme_len] = ' ';
1633 HTTP_EncodeBase64( data, data_len, ret + scheme_len + 1 );
1634 return ret;
1638 /***********************************************************************
1639 * HTTP_InsertAuthorization
1641 * Insert or delete the authorization field in the request header.
1643 static BOOL HTTP_InsertAuthorization( http_request_t *request, struct HttpAuthInfo *pAuthInfo, LPCWSTR header )
1645 static const WCHAR wszBasic[] = {'B','a','s','i','c',0};
1646 WCHAR *host, *authorization = NULL;
1648 if (pAuthInfo)
1650 if (pAuthInfo->auth_data_len)
1652 if (!(authorization = encode_auth_data(pAuthInfo->scheme, pAuthInfo->auth_data, pAuthInfo->auth_data_len)))
1653 return FALSE;
1655 /* clear the data as it isn't valid now that it has been sent to the
1656 * server, unless it's Basic authentication which doesn't do
1657 * connection tracking */
1658 if (strcmpiW(pAuthInfo->scheme, wszBasic))
1660 heap_free(pAuthInfo->auth_data);
1661 pAuthInfo->auth_data = NULL;
1662 pAuthInfo->auth_data_len = 0;
1666 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1668 HTTP_ProcessHeader(request, header, authorization, HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
1669 heap_free(authorization);
1671 else if (!strcmpW(header, szAuthorization) && (host = get_host_header(request)))
1673 UINT data_len;
1674 char *data;
1676 if ((data_len = retrieve_cached_basic_authorization(host, NULL, &data)))
1678 TRACE("Found cached basic authorization for %s\n", debugstr_w(host));
1680 if (!(authorization = encode_auth_data(wszBasic, data, data_len)))
1682 heap_free(data);
1683 heap_free(host);
1684 return FALSE;
1687 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1689 HTTP_ProcessHeader(request, header, authorization, HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
1690 heap_free(data);
1691 heap_free(authorization);
1693 heap_free(host);
1695 return TRUE;
1698 static WCHAR *build_proxy_path_url(http_request_t *req)
1700 DWORD size, len;
1701 WCHAR *url;
1703 len = strlenW(req->server->scheme_host_port);
1704 size = len + strlenW(req->path) + 1;
1705 if(*req->path != '/')
1706 size++;
1707 url = heap_alloc(size * sizeof(WCHAR));
1708 if(!url)
1709 return NULL;
1711 memcpy(url, req->server->scheme_host_port, len*sizeof(WCHAR));
1712 if(*req->path != '/')
1713 url[len++] = '/';
1715 strcpyW(url+len, req->path);
1717 TRACE("url=%s\n", debugstr_w(url));
1718 return url;
1721 static BOOL HTTP_DomainMatches(LPCWSTR server, LPCWSTR domain)
1723 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
1724 BOOL ret = FALSE;
1726 if (!strcmpiW( domain, localW ) && !strchrW( server, '.' ))
1727 ret = TRUE;
1728 else if (*domain == '*')
1730 if (domain[1] == '.')
1732 LPCWSTR dot;
1734 /* For a hostname to match a wildcard, the last domain must match
1735 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
1736 * hostname is www.foo.a.b, it matches, but a.b does not.
1738 dot = strchrW( server, '.' );
1739 if (dot)
1741 int len = strlenW( dot + 1 );
1743 if (len > strlenW( domain + 2 ))
1745 LPCWSTR ptr;
1747 /* The server's domain is longer than the wildcard, so it
1748 * could be a subdomain. Compare the last portion of the
1749 * server's domain.
1751 ptr = dot + len + 1 - strlenW( domain + 2 );
1752 if (!strcmpiW( ptr, domain + 2 ))
1754 /* This is only a match if the preceding character is
1755 * a '.', i.e. that it is a matching domain. E.g.
1756 * if domain is '*.b.c' and server is 'www.ab.c' they
1757 * do not match.
1759 ret = *(ptr - 1) == '.';
1762 else
1763 ret = !strcmpiW( dot + 1, domain + 2 );
1767 else
1768 ret = !strcmpiW( server, domain );
1769 return ret;
1772 static BOOL HTTP_ShouldBypassProxy(appinfo_t *lpwai, LPCWSTR server)
1774 LPCWSTR ptr;
1775 BOOL ret = FALSE;
1777 if (!lpwai->proxyBypass) return FALSE;
1778 ptr = lpwai->proxyBypass;
1779 do {
1780 LPCWSTR tmp = ptr;
1782 ptr = strchrW( ptr, ';' );
1783 if (!ptr)
1784 ptr = strchrW( tmp, ' ' );
1785 if (ptr)
1787 if (ptr - tmp < INTERNET_MAX_HOST_NAME_LENGTH)
1789 WCHAR domain[INTERNET_MAX_HOST_NAME_LENGTH];
1791 memcpy( domain, tmp, (ptr - tmp) * sizeof(WCHAR) );
1792 domain[ptr - tmp] = 0;
1793 ret = HTTP_DomainMatches( server, domain );
1795 ptr += 1;
1797 else if (*tmp)
1798 ret = HTTP_DomainMatches( server, tmp );
1799 } while (ptr && !ret);
1800 return ret;
1803 /***********************************************************************
1804 * HTTP_DealWithProxy
1806 static BOOL HTTP_DealWithProxy(appinfo_t *hIC, http_session_t *session, http_request_t *request)
1808 static const WCHAR protoHttp[] = { 'h','t','t','p',0 };
1809 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 };
1810 static const WCHAR szFormat[] = { 'h','t','t','p',':','/','/','%','s',0 };
1811 WCHAR buf[INTERNET_MAX_HOST_NAME_LENGTH];
1812 WCHAR protoProxy[INTERNET_MAX_URL_LENGTH];
1813 DWORD protoProxyLen = INTERNET_MAX_URL_LENGTH;
1814 WCHAR proxy[INTERNET_MAX_URL_LENGTH];
1815 static WCHAR szNul[] = { 0 };
1816 URL_COMPONENTSW UrlComponents;
1817 server_t *new_server;
1818 BOOL is_https;
1820 memset( &UrlComponents, 0, sizeof UrlComponents );
1821 UrlComponents.dwStructSize = sizeof UrlComponents;
1822 UrlComponents.lpszHostName = buf;
1823 UrlComponents.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH;
1825 if (!INTERNET_FindProxyForProtocol(hIC->proxy, protoHttp, protoProxy, &protoProxyLen))
1826 return FALSE;
1827 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1828 protoProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
1829 sprintfW(proxy, szFormat, protoProxy);
1830 else
1831 strcpyW(proxy, protoProxy);
1832 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
1833 return FALSE;
1834 if( UrlComponents.dwHostNameLength == 0 )
1835 return FALSE;
1837 if( !request->path )
1838 request->path = szNul;
1840 is_https = (UrlComponents.nScheme == INTERNET_SCHEME_HTTPS);
1841 if (is_https && UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1842 UrlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
1844 new_server = get_server(UrlComponents.lpszHostName, UrlComponents.nPort, is_https, TRUE);
1845 if(!new_server)
1846 return FALSE;
1848 request->proxy = new_server;
1850 TRACE("proxy server=%s port=%d\n", debugstr_w(new_server->name), new_server->port);
1851 return TRUE;
1854 static DWORD HTTP_ResolveName(http_request_t *request)
1856 server_t *server = request->proxy ? request->proxy : request->server;
1857 int addr_len;
1859 if(server->addr_len)
1860 return ERROR_SUCCESS;
1862 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
1863 INTERNET_STATUS_RESOLVING_NAME,
1864 server->name,
1865 (strlenW(server->name)+1) * sizeof(WCHAR));
1867 addr_len = sizeof(server->addr);
1868 if (!GetAddress(server->name, server->port, (SOCKADDR*)&server->addr, &addr_len, server->addr_str))
1869 return ERROR_INTERNET_NAME_NOT_RESOLVED;
1871 server->addr_len = addr_len;
1872 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
1873 INTERNET_STATUS_NAME_RESOLVED,
1874 server->addr_str, strlen(server->addr_str)+1);
1876 TRACE("resolved %s to %s\n", debugstr_w(server->name), server->addr_str);
1877 return ERROR_SUCCESS;
1880 static BOOL HTTP_GetRequestURL(http_request_t *req, LPWSTR buf)
1882 static const WCHAR http[] = { 'h','t','t','p',':','/','/',0 };
1883 static const WCHAR https[] = { 'h','t','t','p','s',':','/','/',0 };
1884 static const WCHAR slash[] = { '/',0 };
1885 LPHTTPHEADERW host_header;
1886 LPCWSTR scheme;
1888 host_header = HTTP_GetHeader(req, hostW);
1889 if(!host_header)
1890 return FALSE;
1892 EnterCriticalSection( &req->headers_section );
1894 if (req->hdr.dwFlags & INTERNET_FLAG_SECURE)
1895 scheme = https;
1896 else
1897 scheme = http;
1898 strcpyW(buf, scheme);
1899 strcatW(buf, host_header->lpszValue);
1900 if (req->path[0] != '/')
1901 strcatW(buf, slash);
1902 strcatW(buf, req->path);
1904 LeaveCriticalSection( &req->headers_section );
1905 return TRUE;
1909 /***********************************************************************
1910 * HTTPREQ_Destroy (internal)
1912 * Deallocate request handle
1915 static void HTTPREQ_Destroy(object_header_t *hdr)
1917 http_request_t *request = (http_request_t*) hdr;
1918 DWORD i;
1920 TRACE("\n");
1922 if(request->hCacheFile)
1923 CloseHandle(request->hCacheFile);
1924 if(request->req_file)
1925 req_file_release(request->req_file);
1927 request->headers_section.DebugInfo->Spare[0] = 0;
1928 DeleteCriticalSection( &request->headers_section );
1929 request->read_section.DebugInfo->Spare[0] = 0;
1930 DeleteCriticalSection( &request->read_section );
1931 WININET_Release(&request->session->hdr);
1933 destroy_authinfo(request->authInfo);
1934 destroy_authinfo(request->proxyAuthInfo);
1936 if(request->server)
1937 server_release(request->server);
1938 if(request->proxy)
1939 server_release(request->proxy);
1941 heap_free(request->path);
1942 heap_free(request->verb);
1943 heap_free(request->version);
1944 heap_free(request->statusText);
1946 for (i = 0; i < request->nCustHeaders; i++)
1948 heap_free(request->custHeaders[i].lpszField);
1949 heap_free(request->custHeaders[i].lpszValue);
1951 destroy_data_stream(request->data_stream);
1952 heap_free(request->custHeaders);
1955 static void http_release_netconn(http_request_t *req, BOOL reuse)
1957 TRACE("%p %p %x\n",req, req->netconn, reuse);
1959 if(!is_valid_netconn(req->netconn))
1960 return;
1962 if(reuse && req->netconn->keep_alive) {
1963 BOOL run_collector;
1965 EnterCriticalSection(&connection_pool_cs);
1967 list_add_head(&req->netconn->server->conn_pool, &req->netconn->pool_entry);
1968 req->netconn->keep_until = GetTickCount64() + COLLECT_TIME;
1969 req->netconn = NULL;
1971 run_collector = !collector_running;
1972 collector_running = TRUE;
1974 LeaveCriticalSection(&connection_pool_cs);
1976 if(run_collector) {
1977 HANDLE thread = NULL;
1978 HMODULE module;
1980 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (const WCHAR*)WININET_hModule, &module);
1981 if(module)
1982 thread = CreateThread(NULL, 0, collect_connections_proc, NULL, 0, NULL);
1983 if(!thread) {
1984 EnterCriticalSection(&connection_pool_cs);
1985 collector_running = FALSE;
1986 LeaveCriticalSection(&connection_pool_cs);
1988 if(module)
1989 FreeLibrary(module);
1991 else
1992 CloseHandle(thread);
1994 return;
1997 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
1998 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2000 close_netconn(req->netconn);
2002 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
2003 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2006 static BOOL HTTP_KeepAlive(http_request_t *request)
2008 WCHAR szVersion[10];
2009 WCHAR szConnectionResponse[20];
2010 DWORD dwBufferSize = sizeof(szVersion);
2011 BOOL keepalive = FALSE;
2013 /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
2014 * the connection is keep-alive by default */
2015 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_VERSION, szVersion, &dwBufferSize, NULL) == ERROR_SUCCESS
2016 && !strcmpiW(szVersion, g_szHttp1_1))
2018 keepalive = TRUE;
2021 dwBufferSize = sizeof(szConnectionResponse);
2022 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_PROXY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) == ERROR_SUCCESS
2023 || HTTP_HttpQueryInfoW(request, HTTP_QUERY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) == ERROR_SUCCESS)
2025 keepalive = !strcmpiW(szConnectionResponse, szKeepAlive);
2028 return keepalive;
2031 static void HTTPREQ_CloseConnection(object_header_t *hdr)
2033 http_request_t *req = (http_request_t*)hdr;
2035 http_release_netconn(req, drain_content(req, FALSE));
2038 static DWORD str_to_buffer(const WCHAR *str, void *buffer, DWORD *size, BOOL unicode)
2040 int len;
2041 if (unicode)
2043 WCHAR *buf = buffer;
2045 if (str) len = strlenW(str);
2046 else len = 0;
2047 if (*size < (len + 1) * sizeof(WCHAR))
2049 *size = (len + 1) * sizeof(WCHAR);
2050 return ERROR_INSUFFICIENT_BUFFER;
2052 if (str) strcpyW(buf, str);
2053 else buf[0] = 0;
2055 *size = len;
2056 return ERROR_SUCCESS;
2058 else
2060 char *buf = buffer;
2062 if (str) len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
2063 else len = 1;
2064 if (*size < len)
2066 *size = len;
2067 return ERROR_INSUFFICIENT_BUFFER;
2069 if (str) WideCharToMultiByte(CP_ACP, 0, str, -1, buf, *size, NULL, NULL);
2070 else buf[0] = 0;
2072 *size = len - 1;
2073 return ERROR_SUCCESS;
2077 static DWORD HTTPREQ_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2079 http_request_t *req = (http_request_t*)hdr;
2081 switch(option) {
2082 case INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO:
2084 INTERNET_DIAGNOSTIC_SOCKET_INFO *info = buffer;
2086 FIXME("INTERNET_DIAGNOSTIC_SOCKET_INFO stub\n");
2088 if (*size < sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO))
2089 return ERROR_INSUFFICIENT_BUFFER;
2090 *size = sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO);
2091 /* FIXME: can't get a SOCKET from our connection since we don't use
2092 * winsock
2094 info->Socket = 0;
2095 /* FIXME: get source port from req->netConnection */
2096 info->SourcePort = 0;
2097 info->DestPort = req->server->port;
2098 info->Flags = 0;
2099 if (HTTP_KeepAlive(req))
2100 info->Flags |= IDSI_FLAG_KEEP_ALIVE;
2101 if (req->proxy)
2102 info->Flags |= IDSI_FLAG_PROXY;
2103 if (is_valid_netconn(req->netconn) && req->netconn->secure)
2104 info->Flags |= IDSI_FLAG_SECURE;
2106 return ERROR_SUCCESS;
2109 case 98:
2110 TRACE("Queried undocumented option 98, forwarding to INTERNET_OPTION_SECURITY_FLAGS\n");
2111 /* fall through */
2112 case INTERNET_OPTION_SECURITY_FLAGS:
2114 DWORD flags;
2116 if (*size < sizeof(ULONG))
2117 return ERROR_INSUFFICIENT_BUFFER;
2119 *size = sizeof(DWORD);
2120 flags = is_valid_netconn(req->netconn) ? req->netconn->security_flags : req->security_flags | req->server->security_flags;
2121 *(DWORD *)buffer = flags;
2123 TRACE("INTERNET_OPTION_SECURITY_FLAGS %x\n", flags);
2124 return ERROR_SUCCESS;
2127 case INTERNET_OPTION_HANDLE_TYPE:
2128 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
2130 if (*size < sizeof(ULONG))
2131 return ERROR_INSUFFICIENT_BUFFER;
2133 *size = sizeof(DWORD);
2134 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_HTTP_REQUEST;
2135 return ERROR_SUCCESS;
2137 case INTERNET_OPTION_URL: {
2138 static const WCHAR httpW[] = {'h','t','t','p',':','/','/',0};
2139 WCHAR url[INTERNET_MAX_URL_LENGTH];
2140 HTTPHEADERW *host;
2142 TRACE("INTERNET_OPTION_URL\n");
2144 EnterCriticalSection( &req->headers_section );
2145 host = HTTP_GetHeader(req, hostW);
2146 strcpyW(url, httpW);
2147 strcatW(url, host->lpszValue);
2148 strcatW(url, req->path);
2149 LeaveCriticalSection( &req->headers_section );
2151 TRACE("INTERNET_OPTION_URL: %s\n",debugstr_w(url));
2152 return str_to_buffer(url, buffer, size, unicode);
2154 case INTERNET_OPTION_USER_AGENT:
2155 return str_to_buffer(req->session->appInfo->agent, buffer, size, unicode);
2156 case INTERNET_OPTION_USERNAME:
2157 return str_to_buffer(req->session->userName, buffer, size, unicode);
2158 case INTERNET_OPTION_PASSWORD:
2159 return str_to_buffer(req->session->password, buffer, size, unicode);
2160 case INTERNET_OPTION_PROXY_USERNAME:
2161 return str_to_buffer(req->session->appInfo->proxyUsername, buffer, size, unicode);
2162 case INTERNET_OPTION_PROXY_PASSWORD:
2163 return str_to_buffer(req->session->appInfo->proxyPassword, buffer, size, unicode);
2165 case INTERNET_OPTION_CACHE_TIMESTAMPS: {
2166 INTERNET_CACHE_ENTRY_INFOW *info;
2167 INTERNET_CACHE_TIMESTAMPS *ts = buffer;
2168 WCHAR url[INTERNET_MAX_URL_LENGTH];
2169 DWORD nbytes, error;
2170 BOOL ret;
2172 TRACE("INTERNET_OPTION_CACHE_TIMESTAMPS\n");
2174 if (*size < sizeof(*ts))
2176 *size = sizeof(*ts);
2177 return ERROR_INSUFFICIENT_BUFFER;
2179 nbytes = 0;
2180 HTTP_GetRequestURL(req, url);
2181 ret = GetUrlCacheEntryInfoW(url, NULL, &nbytes);
2182 error = GetLastError();
2183 if (!ret && error == ERROR_INSUFFICIENT_BUFFER)
2185 if (!(info = heap_alloc(nbytes)))
2186 return ERROR_OUTOFMEMORY;
2188 GetUrlCacheEntryInfoW(url, info, &nbytes);
2190 ts->ftExpires = info->ExpireTime;
2191 ts->ftLastModified = info->LastModifiedTime;
2193 heap_free(info);
2194 *size = sizeof(*ts);
2195 return ERROR_SUCCESS;
2197 return error;
2200 case INTERNET_OPTION_DATAFILE_NAME: {
2201 DWORD req_size;
2203 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
2205 if(!req->req_file) {
2206 *size = 0;
2207 return ERROR_INTERNET_ITEM_NOT_FOUND;
2210 if(unicode) {
2211 req_size = (lstrlenW(req->req_file->file_name)+1) * sizeof(WCHAR);
2212 if(*size < req_size)
2213 return ERROR_INSUFFICIENT_BUFFER;
2215 *size = req_size;
2216 memcpy(buffer, req->req_file->file_name, *size);
2217 return ERROR_SUCCESS;
2218 }else {
2219 req_size = WideCharToMultiByte(CP_ACP, 0, req->req_file->file_name, -1, NULL, 0, NULL, NULL);
2220 if (req_size > *size)
2221 return ERROR_INSUFFICIENT_BUFFER;
2223 *size = WideCharToMultiByte(CP_ACP, 0, req->req_file->file_name,
2224 -1, buffer, *size, NULL, NULL);
2225 return ERROR_SUCCESS;
2229 case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT: {
2230 PCCERT_CONTEXT context;
2232 if(!req->netconn)
2233 return ERROR_INTERNET_INVALID_OPERATION;
2235 if(*size < sizeof(INTERNET_CERTIFICATE_INFOA)) {
2236 *size = sizeof(INTERNET_CERTIFICATE_INFOA);
2237 return ERROR_INSUFFICIENT_BUFFER;
2240 context = (PCCERT_CONTEXT)NETCON_GetCert(req->netconn);
2241 if(context) {
2242 INTERNET_CERTIFICATE_INFOA *info = (INTERNET_CERTIFICATE_INFOA*)buffer;
2243 DWORD len;
2245 memset(info, 0, sizeof(*info));
2246 info->ftExpiry = context->pCertInfo->NotAfter;
2247 info->ftStart = context->pCertInfo->NotBefore;
2248 len = CertNameToStrA(context->dwCertEncodingType,
2249 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG, NULL, 0);
2250 info->lpszSubjectInfo = LocalAlloc(0, len);
2251 if(info->lpszSubjectInfo)
2252 CertNameToStrA(context->dwCertEncodingType,
2253 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG,
2254 info->lpszSubjectInfo, len);
2255 len = CertNameToStrA(context->dwCertEncodingType,
2256 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG, NULL, 0);
2257 info->lpszIssuerInfo = LocalAlloc(0, len);
2258 if(info->lpszIssuerInfo)
2259 CertNameToStrA(context->dwCertEncodingType,
2260 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG,
2261 info->lpszIssuerInfo, len);
2262 info->dwKeySize = NETCON_GetCipherStrength(req->netconn);
2263 CertFreeCertificateContext(context);
2264 return ERROR_SUCCESS;
2266 return ERROR_NOT_SUPPORTED;
2268 case INTERNET_OPTION_CONNECT_TIMEOUT:
2269 if (*size < sizeof(DWORD))
2270 return ERROR_INSUFFICIENT_BUFFER;
2272 *size = sizeof(DWORD);
2273 *(DWORD *)buffer = req->connect_timeout;
2274 return ERROR_SUCCESS;
2275 case INTERNET_OPTION_REQUEST_FLAGS: {
2276 DWORD flags = 0;
2278 if (*size < sizeof(DWORD))
2279 return ERROR_INSUFFICIENT_BUFFER;
2281 /* FIXME: Add support for:
2282 * INTERNET_REQFLAG_FROM_CACHE
2283 * INTERNET_REQFLAG_CACHE_WRITE_DISABLED
2286 if(req->proxy)
2287 flags |= INTERNET_REQFLAG_VIA_PROXY;
2288 if(!req->status_code)
2289 flags |= INTERNET_REQFLAG_NO_HEADERS;
2291 TRACE("INTERNET_OPTION_REQUEST_FLAGS returning %x\n", flags);
2293 *size = sizeof(DWORD);
2294 *(DWORD*)buffer = flags;
2295 return ERROR_SUCCESS;
2299 return INET_QueryOption(hdr, option, buffer, size, unicode);
2302 static DWORD HTTPREQ_SetOption(object_header_t *hdr, DWORD option, void *buffer, DWORD size)
2304 http_request_t *req = (http_request_t*)hdr;
2306 switch(option) {
2307 case 99: /* Undocumented, seems to be INTERNET_OPTION_SECURITY_FLAGS with argument validation */
2308 TRACE("Undocumented option 99\n");
2310 if (!buffer || size != sizeof(DWORD))
2311 return ERROR_INVALID_PARAMETER;
2312 if(*(DWORD*)buffer & ~SECURITY_SET_MASK)
2313 return ERROR_INTERNET_OPTION_NOT_SETTABLE;
2315 /* fall through */
2316 case INTERNET_OPTION_SECURITY_FLAGS:
2318 DWORD flags;
2320 if (!buffer || size != sizeof(DWORD))
2321 return ERROR_INVALID_PARAMETER;
2322 flags = *(DWORD *)buffer;
2323 TRACE("INTERNET_OPTION_SECURITY_FLAGS %08x\n", flags);
2324 flags &= SECURITY_SET_MASK;
2325 req->security_flags |= flags;
2326 if(is_valid_netconn(req->netconn))
2327 req->netconn->security_flags |= flags;
2328 return ERROR_SUCCESS;
2330 case INTERNET_OPTION_CONNECT_TIMEOUT:
2331 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2332 req->connect_timeout = *(DWORD *)buffer;
2333 return ERROR_SUCCESS;
2335 case INTERNET_OPTION_SEND_TIMEOUT:
2336 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2337 req->send_timeout = *(DWORD *)buffer;
2338 return ERROR_SUCCESS;
2340 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2341 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2342 req->receive_timeout = *(DWORD *)buffer;
2343 return ERROR_SUCCESS;
2345 case INTERNET_OPTION_USERNAME:
2346 heap_free(req->session->userName);
2347 if (!(req->session->userName = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2348 return ERROR_SUCCESS;
2350 case INTERNET_OPTION_PASSWORD:
2351 heap_free(req->session->password);
2352 if (!(req->session->password = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2353 return ERROR_SUCCESS;
2355 case INTERNET_OPTION_PROXY_USERNAME:
2356 heap_free(req->session->appInfo->proxyUsername);
2357 if (!(req->session->appInfo->proxyUsername = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2358 return ERROR_SUCCESS;
2360 case INTERNET_OPTION_PROXY_PASSWORD:
2361 heap_free(req->session->appInfo->proxyPassword);
2362 if (!(req->session->appInfo->proxyPassword = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2363 return ERROR_SUCCESS;
2365 case INTERNET_OPTION_HTTP_DECODING:
2366 if(size != sizeof(BOOL))
2367 return ERROR_INVALID_PARAMETER;
2368 req->decoding = *(BOOL*)buffer;
2369 return ERROR_SUCCESS;
2372 return INET_SetOption(hdr, option, buffer, size);
2375 static void commit_cache_entry(http_request_t *req)
2377 WCHAR url[INTERNET_MAX_URL_LENGTH];
2379 TRACE("%p\n", req);
2381 CloseHandle(req->hCacheFile);
2382 req->hCacheFile = NULL;
2384 if(HTTP_GetRequestURL(req, url)) {
2385 WCHAR *header;
2386 DWORD header_len;
2387 BOOL res;
2389 header = build_response_header(req, TRUE);
2390 header_len = (header ? strlenW(header) : 0);
2391 res = CommitUrlCacheEntryW(url, req->req_file->file_name, req->expires,
2392 req->last_modified, NORMAL_CACHE_ENTRY,
2393 header, header_len, NULL, 0);
2394 if(res)
2395 req->req_file->is_committed = TRUE;
2396 else
2397 WARN("CommitUrlCacheEntry failed: %u\n", GetLastError());
2398 heap_free(header);
2402 static void create_cache_entry(http_request_t *req)
2404 static const WCHAR no_cacheW[] = {'n','o','-','c','a','c','h','e',0};
2405 static const WCHAR no_storeW[] = {'n','o','-','s','t','o','r','e',0};
2407 WCHAR url[INTERNET_MAX_URL_LENGTH];
2408 WCHAR file_name[MAX_PATH+1];
2409 BOOL b = TRUE;
2411 /* FIXME: We should free previous cache file earlier */
2412 if(req->req_file) {
2413 req_file_release(req->req_file);
2414 req->req_file = NULL;
2416 if(req->hCacheFile) {
2417 CloseHandle(req->hCacheFile);
2418 req->hCacheFile = NULL;
2421 if(req->hdr.dwFlags & INTERNET_FLAG_NO_CACHE_WRITE)
2422 b = FALSE;
2424 if(b) {
2425 int header_idx;
2427 EnterCriticalSection( &req->headers_section );
2429 header_idx = HTTP_GetCustomHeaderIndex(req, szCache_Control, 0, FALSE);
2430 if(header_idx != -1) {
2431 WCHAR *ptr;
2433 for(ptr=req->custHeaders[header_idx].lpszValue; *ptr; ) {
2434 WCHAR *end;
2436 while(*ptr==' ' || *ptr=='\t')
2437 ptr++;
2439 end = strchrW(ptr, ',');
2440 if(!end)
2441 end = ptr + strlenW(ptr);
2443 if(!strncmpiW(ptr, no_cacheW, sizeof(no_cacheW)/sizeof(*no_cacheW)-1)
2444 || !strncmpiW(ptr, no_storeW, sizeof(no_storeW)/sizeof(*no_storeW)-1)) {
2445 b = FALSE;
2446 break;
2449 ptr = end;
2450 if(*ptr == ',')
2451 ptr++;
2455 LeaveCriticalSection( &req->headers_section );
2458 if(!b) {
2459 if(!(req->hdr.dwFlags & INTERNET_FLAG_NEED_FILE))
2460 return;
2462 FIXME("INTERNET_FLAG_NEED_FILE is not supported correctly\n");
2465 b = HTTP_GetRequestURL(req, url);
2466 if(!b) {
2467 WARN("Could not get URL\n");
2468 return;
2471 b = CreateUrlCacheEntryW(url, req->contentLength == ~0u ? 0 : req->contentLength, NULL, file_name, 0);
2472 if(!b) {
2473 WARN("Could not create cache entry: %08x\n", GetLastError());
2474 return;
2477 create_req_file(file_name, &req->req_file);
2479 req->hCacheFile = CreateFileW(file_name, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
2480 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2481 if(req->hCacheFile == INVALID_HANDLE_VALUE) {
2482 WARN("Could not create file: %u\n", GetLastError());
2483 req->hCacheFile = NULL;
2484 return;
2487 if(req->read_size) {
2488 DWORD written;
2490 b = WriteFile(req->hCacheFile, req->read_buf+req->read_pos, req->read_size, &written, NULL);
2491 if(!b)
2492 FIXME("WriteFile failed: %u\n", GetLastError());
2494 if(req->data_stream->vtbl->end_of_data(req->data_stream, req))
2495 commit_cache_entry(req);
2499 /* read some more data into the read buffer (the read section must be held) */
2500 static DWORD read_more_data( http_request_t *req, int maxlen )
2502 DWORD res;
2503 int len;
2505 if (req->read_pos)
2507 /* move existing data to the start of the buffer */
2508 if(req->read_size)
2509 memmove( req->read_buf, req->read_buf + req->read_pos, req->read_size );
2510 req->read_pos = 0;
2513 if (maxlen == -1) maxlen = sizeof(req->read_buf);
2515 res = NETCON_recv( req->netconn, req->read_buf + req->read_size,
2516 maxlen - req->read_size, BLOCKING_ALLOW, &len );
2517 if(res == ERROR_SUCCESS)
2518 req->read_size += len;
2520 return res;
2523 /* remove some amount of data from the read buffer (the read section must be held) */
2524 static void remove_data( http_request_t *req, int count )
2526 if (!(req->read_size -= count)) req->read_pos = 0;
2527 else req->read_pos += count;
2530 static DWORD read_line( http_request_t *req, LPSTR buffer, DWORD *len )
2532 int count, bytes_read, pos = 0;
2533 DWORD res;
2535 EnterCriticalSection( &req->read_section );
2536 for (;;)
2538 BYTE *eol = memchr( req->read_buf + req->read_pos, '\n', req->read_size );
2540 if (eol)
2542 count = eol - (req->read_buf + req->read_pos);
2543 bytes_read = count + 1;
2545 else count = bytes_read = req->read_size;
2547 count = min( count, *len - pos );
2548 memcpy( buffer + pos, req->read_buf + req->read_pos, count );
2549 pos += count;
2550 remove_data( req, bytes_read );
2551 if (eol) break;
2553 if ((res = read_more_data( req, -1 )))
2555 WARN( "read failed %u\n", res );
2556 LeaveCriticalSection( &req->read_section );
2557 return res;
2559 if (!req->read_size)
2561 *len = 0;
2562 TRACE( "returning empty string\n" );
2563 LeaveCriticalSection( &req->read_section );
2564 return ERROR_SUCCESS;
2567 LeaveCriticalSection( &req->read_section );
2569 if (pos < *len)
2571 if (pos && buffer[pos - 1] == '\r') pos--;
2572 *len = pos + 1;
2574 buffer[*len - 1] = 0;
2575 TRACE( "returning %s\n", debugstr_a(buffer));
2576 return ERROR_SUCCESS;
2579 /* check if we have reached the end of the data to read (the read section must be held) */
2580 static BOOL end_of_read_data( http_request_t *req )
2582 return !req->read_size && req->data_stream->vtbl->end_of_data(req->data_stream, req);
2585 static DWORD read_http_stream(http_request_t *req, BYTE *buf, DWORD size, DWORD *read, blocking_mode_t blocking_mode)
2587 DWORD res;
2589 res = req->data_stream->vtbl->read(req->data_stream, req, buf, size, read, blocking_mode);
2590 assert(*read <= size);
2592 if(req->hCacheFile) {
2593 if(*read) {
2594 BOOL bres;
2595 DWORD written;
2597 bres = WriteFile(req->hCacheFile, buf, *read, &written, NULL);
2598 if(!bres)
2599 FIXME("WriteFile failed: %u\n", GetLastError());
2602 if(req->data_stream->vtbl->end_of_data(req->data_stream, req))
2603 commit_cache_entry(req);
2606 return res;
2609 /* fetch some more data into the read buffer (the read section must be held) */
2610 static DWORD refill_read_buffer(http_request_t *req, blocking_mode_t blocking_mode, DWORD *read_bytes)
2612 DWORD res, read=0;
2614 if(req->read_size == sizeof(req->read_buf))
2615 return ERROR_SUCCESS;
2617 if(req->read_pos) {
2618 if(req->read_size)
2619 memmove(req->read_buf, req->read_buf+req->read_pos, req->read_size);
2620 req->read_pos = 0;
2623 res = read_http_stream(req, req->read_buf+req->read_size, sizeof(req->read_buf) - req->read_size,
2624 &read, blocking_mode);
2625 req->read_size += read;
2627 TRACE("read %u bytes, read_size %u\n", read, req->read_size);
2628 if(read_bytes)
2629 *read_bytes = read;
2630 return res;
2633 /* return the size of data available to be read immediately (the read section must be held) */
2634 static DWORD get_avail_data( http_request_t *req )
2636 return req->read_size + req->data_stream->vtbl->get_avail_data(req->data_stream, req);
2639 static DWORD netconn_get_avail_data(data_stream_t *stream, http_request_t *req)
2641 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2642 DWORD avail = 0;
2644 if(is_valid_netconn(req->netconn))
2645 NETCON_query_data_available(req->netconn, &avail);
2646 return netconn_stream->content_length == ~0u
2647 ? avail
2648 : min(avail, netconn_stream->content_length-netconn_stream->content_read);
2651 static BOOL netconn_end_of_data(data_stream_t *stream, http_request_t *req)
2653 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2654 return netconn_stream->content_read == netconn_stream->content_length || !is_valid_netconn(req->netconn);
2657 static DWORD netconn_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
2658 DWORD *read, blocking_mode_t blocking_mode)
2660 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2661 DWORD res = ERROR_SUCCESS;
2662 int len = 0, ret = 0;
2664 size = min(size, netconn_stream->content_length-netconn_stream->content_read);
2666 if(size && is_valid_netconn(req->netconn)) {
2667 while((res = NETCON_recv(req->netconn, buf+ret, size-ret,
2668 blocking_mode == BLOCKING_WAITALL ? BLOCKING_ALLOW : blocking_mode, &len)) == ERROR_SUCCESS) {
2669 if(!len) {
2670 netconn_stream->content_length = netconn_stream->content_read;
2671 break;
2673 ret += len;
2674 netconn_stream->content_read += len;
2675 if(blocking_mode != BLOCKING_WAITALL || size == ret)
2676 break;
2679 if(ret || (blocking_mode == BLOCKING_DISALLOW && res == WSAEWOULDBLOCK))
2680 res = ERROR_SUCCESS;
2683 TRACE("read %u bytes\n", ret);
2684 *read = ret;
2685 return res;
2688 static BOOL netconn_drain_content(data_stream_t *stream, http_request_t *req)
2690 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2691 BYTE buf[1024];
2692 int len;
2694 if(netconn_end_of_data(stream, req))
2695 return TRUE;
2697 do {
2698 if(NETCON_recv(req->netconn, buf, sizeof(buf), BLOCKING_DISALLOW, &len) != ERROR_SUCCESS)
2699 return FALSE;
2701 netconn_stream->content_read += len;
2702 }while(netconn_stream->content_read < netconn_stream->content_length);
2704 return TRUE;
2707 static void netconn_destroy(data_stream_t *stream)
2711 static const data_stream_vtbl_t netconn_stream_vtbl = {
2712 netconn_get_avail_data,
2713 netconn_end_of_data,
2714 netconn_read,
2715 netconn_drain_content,
2716 netconn_destroy
2719 /* read some more data into the read buffer (the read section must be held) */
2720 static DWORD read_more_chunked_data(chunked_stream_t *stream, http_request_t *req, int maxlen)
2722 DWORD res;
2723 int len;
2725 assert(!stream->end_of_data);
2727 if (stream->buf_pos)
2729 /* move existing data to the start of the buffer */
2730 if(stream->buf_size)
2731 memmove(stream->buf, stream->buf + stream->buf_pos, stream->buf_size);
2732 stream->buf_pos = 0;
2735 if (maxlen == -1) maxlen = sizeof(stream->buf);
2737 res = NETCON_recv( req->netconn, stream->buf + stream->buf_size,
2738 maxlen - stream->buf_size, BLOCKING_ALLOW, &len );
2739 if(res == ERROR_SUCCESS)
2740 stream->buf_size += len;
2742 return res;
2745 /* remove some amount of data from the read buffer (the read section must be held) */
2746 static void remove_chunked_data(chunked_stream_t *stream, int count)
2748 if (!(stream->buf_size -= count)) stream->buf_pos = 0;
2749 else stream->buf_pos += count;
2752 /* discard data contents until we reach end of line (the read section must be held) */
2753 static DWORD discard_chunked_eol(chunked_stream_t *stream, http_request_t *req)
2755 DWORD res;
2759 BYTE *eol = memchr(stream->buf + stream->buf_pos, '\n', stream->buf_size);
2760 if (eol)
2762 remove_chunked_data(stream, (eol + 1) - (stream->buf + stream->buf_pos));
2763 break;
2765 stream->buf_pos = stream->buf_size = 0; /* discard everything */
2766 if ((res = read_more_chunked_data(stream, req, -1)) != ERROR_SUCCESS) return res;
2767 } while (stream->buf_size);
2768 return ERROR_SUCCESS;
2771 /* read the size of the next chunk (the read section must be held) */
2772 static DWORD start_next_chunk(chunked_stream_t *stream, http_request_t *req)
2774 DWORD chunk_size = 0, res;
2776 assert(!stream->chunk_size || stream->chunk_size == ~0u);
2778 if (stream->end_of_data) return ERROR_SUCCESS;
2780 /* read terminator for the previous chunk */
2781 if(!stream->chunk_size && (res = discard_chunked_eol(stream, req)) != ERROR_SUCCESS)
2782 return res;
2784 for (;;)
2786 while (stream->buf_size)
2788 char ch = stream->buf[stream->buf_pos];
2789 if (ch >= '0' && ch <= '9') chunk_size = chunk_size * 16 + ch - '0';
2790 else if (ch >= 'a' && ch <= 'f') chunk_size = chunk_size * 16 + ch - 'a' + 10;
2791 else if (ch >= 'A' && ch <= 'F') chunk_size = chunk_size * 16 + ch - 'A' + 10;
2792 else if (ch == ';' || ch == '\r' || ch == '\n')
2794 TRACE( "reading %u byte chunk\n", chunk_size );
2795 stream->chunk_size = chunk_size;
2796 if (req->contentLength == ~0u) req->contentLength = chunk_size;
2797 else req->contentLength += chunk_size;
2799 /* eat the rest of this line */
2800 if ((res = discard_chunked_eol(stream, req)) != ERROR_SUCCESS)
2801 return res;
2803 /* if there's chunk data, return now */
2804 if (chunk_size) return ERROR_SUCCESS;
2806 /* otherwise, eat the terminator for this chunk */
2807 if ((res = discard_chunked_eol(stream, req)) != ERROR_SUCCESS)
2808 return res;
2810 stream->end_of_data = TRUE;
2811 return ERROR_SUCCESS;
2813 remove_chunked_data(stream, 1);
2815 if ((res = read_more_chunked_data(stream, req, -1)) != ERROR_SUCCESS) return res;
2816 if (!stream->buf_size)
2818 stream->chunk_size = 0;
2819 return ERROR_SUCCESS;
2824 static DWORD chunked_get_avail_data(data_stream_t *stream, http_request_t *req)
2826 /* Allow reading only from read buffer */
2827 return 0;
2830 static BOOL chunked_end_of_data(data_stream_t *stream, http_request_t *req)
2832 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2833 return chunked_stream->end_of_data;
2836 static DWORD chunked_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
2837 DWORD *read, blocking_mode_t blocking_mode)
2839 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2840 DWORD read_bytes = 0, ret_read = 0, res = ERROR_SUCCESS;
2842 if(!chunked_stream->chunk_size || chunked_stream->chunk_size == ~0u) {
2843 res = start_next_chunk(chunked_stream, req);
2844 if(res != ERROR_SUCCESS)
2845 return res;
2848 while(size && chunked_stream->chunk_size && !chunked_stream->end_of_data) {
2849 if(chunked_stream->buf_size) {
2850 read_bytes = min(size, min(chunked_stream->buf_size, chunked_stream->chunk_size));
2852 /* this could block */
2853 if(blocking_mode == BLOCKING_DISALLOW && read_bytes == chunked_stream->chunk_size)
2854 break;
2856 memcpy(buf+ret_read, chunked_stream->buf+chunked_stream->buf_pos, read_bytes);
2857 remove_chunked_data(chunked_stream, read_bytes);
2858 }else {
2859 read_bytes = min(size, chunked_stream->chunk_size);
2861 if(blocking_mode == BLOCKING_DISALLOW) {
2862 DWORD avail;
2864 if(!is_valid_netconn(req->netconn) || !NETCON_query_data_available(req->netconn, &avail) || !avail)
2865 break;
2866 if(read_bytes > avail)
2867 read_bytes = avail;
2869 /* this could block */
2870 if(read_bytes == chunked_stream->chunk_size)
2871 break;
2874 res = NETCON_recv(req->netconn, (char *)buf+ret_read, read_bytes, BLOCKING_ALLOW, (int*)&read_bytes);
2875 if(res != ERROR_SUCCESS)
2876 break;
2879 chunked_stream->chunk_size -= read_bytes;
2880 size -= read_bytes;
2881 ret_read += read_bytes;
2882 if(size && !chunked_stream->chunk_size) {
2883 assert(blocking_mode != BLOCKING_DISALLOW);
2884 res = start_next_chunk(chunked_stream, req);
2885 if(res != ERROR_SUCCESS)
2886 break;
2889 if(blocking_mode == BLOCKING_ALLOW)
2890 blocking_mode = BLOCKING_DISALLOW;
2893 TRACE("read %u bytes\n", ret_read);
2894 *read = ret_read;
2895 return res;
2898 static BOOL chunked_drain_content(data_stream_t *stream, http_request_t *req)
2900 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2902 remove_chunked_data(chunked_stream, chunked_stream->buf_size);
2903 return chunked_stream->end_of_data;
2906 static void chunked_destroy(data_stream_t *stream)
2908 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2909 heap_free(chunked_stream);
2912 static const data_stream_vtbl_t chunked_stream_vtbl = {
2913 chunked_get_avail_data,
2914 chunked_end_of_data,
2915 chunked_read,
2916 chunked_drain_content,
2917 chunked_destroy
2920 /* set the request content length based on the headers */
2921 static DWORD set_content_length(http_request_t *request)
2923 static const WCHAR szChunked[] = {'c','h','u','n','k','e','d',0};
2924 static const WCHAR headW[] = {'H','E','A','D',0};
2925 WCHAR encoding[20];
2926 DWORD size;
2928 if(request->status_code == HTTP_STATUS_NO_CONTENT || !strcmpW(request->verb, headW)) {
2929 request->contentLength = request->netconn_stream.content_length = 0;
2930 return ERROR_SUCCESS;
2933 size = sizeof(request->contentLength);
2934 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
2935 &request->contentLength, &size, NULL) != ERROR_SUCCESS)
2936 request->contentLength = ~0u;
2937 request->netconn_stream.content_length = request->contentLength;
2938 request->netconn_stream.content_read = request->read_size;
2940 size = sizeof(encoding);
2941 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_TRANSFER_ENCODING, encoding, &size, NULL) == ERROR_SUCCESS &&
2942 !strcmpiW(encoding, szChunked))
2944 chunked_stream_t *chunked_stream;
2946 chunked_stream = heap_alloc(sizeof(*chunked_stream));
2947 if(!chunked_stream)
2948 return ERROR_OUTOFMEMORY;
2950 chunked_stream->data_stream.vtbl = &chunked_stream_vtbl;
2951 chunked_stream->buf_size = chunked_stream->buf_pos = 0;
2952 chunked_stream->chunk_size = ~0u;
2953 chunked_stream->end_of_data = FALSE;
2955 if(request->read_size) {
2956 memcpy(chunked_stream->buf, request->read_buf+request->read_pos, request->read_size);
2957 chunked_stream->buf_size = request->read_size;
2958 request->read_size = request->read_pos = 0;
2961 request->data_stream = &chunked_stream->data_stream;
2962 request->contentLength = ~0u;
2965 if(request->decoding) {
2966 int encoding_idx;
2968 static const WCHAR deflateW[] = {'d','e','f','l','a','t','e',0};
2969 static const WCHAR gzipW[] = {'g','z','i','p',0};
2971 EnterCriticalSection( &request->headers_section );
2973 encoding_idx = HTTP_GetCustomHeaderIndex(request, szContent_Encoding, 0, FALSE);
2974 if(encoding_idx != -1) {
2975 if(!strcmpiW(request->custHeaders[encoding_idx].lpszValue, gzipW)) {
2976 HTTP_DeleteCustomHeader(request, encoding_idx);
2977 LeaveCriticalSection( &request->headers_section );
2978 return init_gzip_stream(request, TRUE);
2980 if(!strcmpiW(request->custHeaders[encoding_idx].lpszValue, deflateW)) {
2981 HTTP_DeleteCustomHeader(request, encoding_idx);
2982 LeaveCriticalSection( &request->headers_section );
2983 return init_gzip_stream(request, FALSE);
2987 LeaveCriticalSection( &request->headers_section );
2990 return ERROR_SUCCESS;
2993 static void send_request_complete(http_request_t *req, DWORD_PTR result, DWORD error)
2995 INTERNET_ASYNC_RESULT iar;
2997 iar.dwResult = result;
2998 iar.dwError = error;
3000 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE, &iar,
3001 sizeof(INTERNET_ASYNC_RESULT));
3004 static void HTTP_ReceiveRequestData(http_request_t *req, BOOL first_notif, DWORD *ret_size)
3006 DWORD res, read = 0, avail = 0;
3007 blocking_mode_t mode;
3009 TRACE("%p\n", req);
3011 EnterCriticalSection( &req->read_section );
3013 mode = first_notif && req->read_size ? BLOCKING_DISALLOW : BLOCKING_ALLOW;
3014 res = refill_read_buffer(req, mode, &read);
3015 if(res == ERROR_SUCCESS)
3016 avail = get_avail_data(req);
3018 LeaveCriticalSection( &req->read_section );
3020 if(res != ERROR_SUCCESS || (mode != BLOCKING_DISALLOW && !read)) {
3021 WARN("res %u read %u, closing connection\n", res, read);
3022 http_release_netconn(req, FALSE);
3025 if(res != ERROR_SUCCESS) {
3026 send_request_complete(req, 0, res);
3027 return;
3030 if(ret_size)
3031 *ret_size = avail;
3032 if(first_notif)
3033 avail = 0;
3035 send_request_complete(req, req->session->hdr.dwInternalFlags & INET_OPENURL ? (DWORD_PTR)req->hdr.hInternet : 1, avail);
3038 /* read data from the http connection (the read section must be held) */
3039 static DWORD HTTPREQ_Read(http_request_t *req, void *buffer, DWORD size, DWORD *read, BOOL sync)
3041 DWORD current_read = 0, ret_read = 0;
3042 blocking_mode_t blocking_mode;
3043 DWORD res = ERROR_SUCCESS;
3045 blocking_mode = req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC ? BLOCKING_ALLOW : BLOCKING_WAITALL;
3047 EnterCriticalSection( &req->read_section );
3049 if(req->read_size) {
3050 ret_read = min(size, req->read_size);
3051 memcpy(buffer, req->read_buf+req->read_pos, ret_read);
3052 req->read_size -= ret_read;
3053 req->read_pos += ret_read;
3054 if(blocking_mode == BLOCKING_ALLOW)
3055 blocking_mode = BLOCKING_DISALLOW;
3058 if(ret_read < size) {
3059 res = read_http_stream(req, (BYTE*)buffer+ret_read, size-ret_read, &current_read, blocking_mode);
3060 ret_read += current_read;
3063 LeaveCriticalSection( &req->read_section );
3065 *read = ret_read;
3066 TRACE( "retrieved %u bytes (%u)\n", ret_read, req->contentLength );
3068 if(size && !ret_read)
3069 http_release_netconn(req, res == ERROR_SUCCESS);
3071 return res;
3074 static BOOL drain_content(http_request_t *req, BOOL blocking)
3076 BOOL ret;
3078 if(!is_valid_netconn(req->netconn) || req->contentLength == -1)
3079 return FALSE;
3081 if(!strcmpW(req->verb, szHEAD))
3082 return TRUE;
3084 if(!blocking)
3085 return req->data_stream->vtbl->drain_content(req->data_stream, req);
3087 EnterCriticalSection( &req->read_section );
3089 while(1) {
3090 DWORD bytes_read, res;
3091 BYTE buf[4096];
3093 res = HTTPREQ_Read(req, buf, sizeof(buf), &bytes_read, TRUE);
3094 if(res != ERROR_SUCCESS) {
3095 ret = FALSE;
3096 break;
3098 if(!bytes_read) {
3099 ret = TRUE;
3100 break;
3104 LeaveCriticalSection( &req->read_section );
3105 return ret;
3108 static DWORD HTTPREQ_ReadFile(object_header_t *hdr, void *buffer, DWORD size, DWORD *read)
3110 http_request_t *req = (http_request_t*)hdr;
3111 DWORD res;
3113 EnterCriticalSection( &req->read_section );
3114 if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3115 hdr->dwError = ERROR_INTERNET_INTERNAL_ERROR;
3117 res = HTTPREQ_Read(req, buffer, size, read, TRUE);
3118 if(res == ERROR_SUCCESS)
3119 res = hdr->dwError;
3120 LeaveCriticalSection( &req->read_section );
3122 return res;
3125 typedef struct {
3126 task_header_t hdr;
3127 void *buf;
3128 DWORD size;
3129 DWORD *ret_read;
3130 } read_file_ex_task_t;
3132 static void AsyncReadFileExProc(task_header_t *hdr)
3134 read_file_ex_task_t *task = (read_file_ex_task_t*)hdr;
3135 http_request_t *req = (http_request_t*)task->hdr.hdr;
3136 DWORD res;
3138 TRACE("INTERNETREADFILEEXW %p\n", task->hdr.hdr);
3140 res = HTTPREQ_Read(req, task->buf, task->size, task->ret_read, TRUE);
3141 send_request_complete(req, res == ERROR_SUCCESS, res);
3144 static DWORD HTTPREQ_ReadFileEx(object_header_t *hdr, void *buf, DWORD size, DWORD *ret_read,
3145 DWORD flags, DWORD_PTR context)
3148 http_request_t *req = (http_request_t*)hdr;
3149 DWORD res, read, cread, error = ERROR_SUCCESS;
3151 if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
3152 FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
3154 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3156 if (hdr->dwFlags & INTERNET_FLAG_ASYNC)
3158 read_file_ex_task_t *task;
3160 if (TryEnterCriticalSection( &req->read_section ))
3162 if (get_avail_data(req))
3164 res = HTTPREQ_Read(req, buf, size, &read, FALSE);
3165 LeaveCriticalSection( &req->read_section );
3166 goto done;
3168 LeaveCriticalSection( &req->read_section );
3171 task = alloc_async_task(&req->hdr, AsyncReadFileExProc, sizeof(*task));
3172 task->buf = buf;
3173 task->size = size;
3174 task->ret_read = ret_read;
3176 INTERNET_AsyncCall(&task->hdr);
3178 return ERROR_IO_PENDING;
3181 read = 0;
3183 EnterCriticalSection( &req->read_section );
3184 if(hdr->dwError == ERROR_SUCCESS)
3185 hdr->dwError = INTERNET_HANDLE_IN_USE;
3186 else if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3187 hdr->dwError = ERROR_INTERNET_INTERNAL_ERROR;
3189 while(1) {
3190 res = HTTPREQ_Read(req, (char*)buf+read, size-read, &cread, !(flags & IRF_NO_WAIT));
3191 if(res != ERROR_SUCCESS)
3192 break;
3194 read += cread;
3195 if(read == size || end_of_read_data(req))
3196 break;
3198 LeaveCriticalSection( &req->read_section );
3200 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
3201 &cread, sizeof(cread));
3202 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
3203 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3205 EnterCriticalSection( &req->read_section );
3208 if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3209 hdr->dwError = ERROR_SUCCESS;
3210 else
3211 error = hdr->dwError;
3213 LeaveCriticalSection( &req->read_section );
3215 done:
3216 *ret_read = read;
3217 if (res == ERROR_SUCCESS) {
3218 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
3219 &read, sizeof(read));
3222 return res==ERROR_SUCCESS ? error : res;
3225 static DWORD HTTPREQ_WriteFile(object_header_t *hdr, const void *buffer, DWORD size, DWORD *written)
3227 DWORD res;
3228 http_request_t *request = (http_request_t*)hdr;
3230 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
3232 *written = 0;
3233 res = NETCON_send(request->netconn, buffer, size, 0, (LPINT)written);
3234 if (res == ERROR_SUCCESS)
3235 request->bytesWritten += *written;
3237 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REQUEST_SENT, written, sizeof(DWORD));
3238 return res;
3241 typedef struct {
3242 task_header_t hdr;
3243 DWORD *ret_size;
3244 } http_data_available_task_t;
3246 static void AsyncQueryDataAvailableProc(task_header_t *hdr)
3248 http_data_available_task_t *task = (http_data_available_task_t*)hdr;
3250 HTTP_ReceiveRequestData((http_request_t*)task->hdr.hdr, FALSE, task->ret_size);
3253 static DWORD HTTPREQ_QueryDataAvailable(object_header_t *hdr, DWORD *available, DWORD flags, DWORD_PTR ctx)
3255 http_request_t *req = (http_request_t*)hdr;
3257 TRACE("(%p %p %x %lx)\n", req, available, flags, ctx);
3259 if (req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3261 http_data_available_task_t *task;
3263 /* never wait, if we can't enter the section we queue an async request right away */
3264 if (TryEnterCriticalSection( &req->read_section ))
3266 refill_read_buffer(req, BLOCKING_DISALLOW, NULL);
3267 if ((*available = get_avail_data( req ))) goto done;
3268 if (end_of_read_data( req )) goto done;
3269 LeaveCriticalSection( &req->read_section );
3272 task = alloc_async_task(&req->hdr, AsyncQueryDataAvailableProc, sizeof(*task));
3273 task->ret_size = available;
3274 INTERNET_AsyncCall(&task->hdr);
3275 return ERROR_IO_PENDING;
3278 EnterCriticalSection( &req->read_section );
3280 if (!(*available = get_avail_data( req )) && !end_of_read_data( req ))
3282 refill_read_buffer( req, BLOCKING_ALLOW, NULL );
3283 *available = get_avail_data( req );
3286 done:
3287 LeaveCriticalSection( &req->read_section );
3289 TRACE( "returning %u\n", *available );
3290 return ERROR_SUCCESS;
3293 static DWORD HTTPREQ_LockRequestFile(object_header_t *hdr, req_file_t **ret)
3295 http_request_t *req = (http_request_t*)hdr;
3297 TRACE("(%p)\n", req);
3299 if(!req->req_file) {
3300 WARN("No cache file name available\n");
3301 return ERROR_FILE_NOT_FOUND;
3304 *ret = req_file_addref(req->req_file);
3305 return ERROR_SUCCESS;
3308 static const object_vtbl_t HTTPREQVtbl = {
3309 HTTPREQ_Destroy,
3310 HTTPREQ_CloseConnection,
3311 HTTPREQ_QueryOption,
3312 HTTPREQ_SetOption,
3313 HTTPREQ_ReadFile,
3314 HTTPREQ_ReadFileEx,
3315 HTTPREQ_WriteFile,
3316 HTTPREQ_QueryDataAvailable,
3317 NULL,
3318 HTTPREQ_LockRequestFile
3321 /***********************************************************************
3322 * HTTP_HttpOpenRequestW (internal)
3324 * Open a HTTP request handle
3326 * RETURNS
3327 * HINTERNET a HTTP request handle on success
3328 * NULL on failure
3331 static DWORD HTTP_HttpOpenRequestW(http_session_t *session,
3332 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
3333 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
3334 DWORD dwFlags, DWORD_PTR dwContext, HINTERNET *ret)
3336 appinfo_t *hIC = session->appInfo;
3337 http_request_t *request;
3338 DWORD len;
3340 TRACE("-->\n");
3342 request = alloc_object(&session->hdr, &HTTPREQVtbl, sizeof(http_request_t));
3343 if(!request)
3344 return ERROR_OUTOFMEMORY;
3346 request->hdr.htype = WH_HHTTPREQ;
3347 request->hdr.dwFlags = dwFlags;
3348 request->hdr.dwContext = dwContext;
3349 request->contentLength = ~0u;
3351 request->netconn_stream.data_stream.vtbl = &netconn_stream_vtbl;
3352 request->data_stream = &request->netconn_stream.data_stream;
3353 request->connect_timeout = session->connect_timeout;
3354 request->send_timeout = session->send_timeout;
3355 request->receive_timeout = session->receive_timeout;
3357 InitializeCriticalSection( &request->headers_section );
3358 request->headers_section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": http_request_t.headers_section");
3360 InitializeCriticalSection( &request->read_section );
3361 request->read_section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": http_request_t.read_section");
3363 WININET_AddRef( &session->hdr );
3364 request->session = session;
3365 list_add_head( &session->hdr.children, &request->hdr.entry );
3367 request->server = get_server(session->hostName, session->hostPort, (dwFlags & INTERNET_FLAG_SECURE) != 0, TRUE);
3368 if(!request->server) {
3369 WININET_Release(&request->hdr);
3370 return ERROR_OUTOFMEMORY;
3373 if (dwFlags & INTERNET_FLAG_IGNORE_CERT_CN_INVALID)
3374 request->security_flags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
3375 if (dwFlags & INTERNET_FLAG_IGNORE_CERT_DATE_INVALID)
3376 request->security_flags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
3378 if (lpszObjectName && *lpszObjectName) {
3379 HRESULT rc;
3381 len = 0;
3382 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
3383 if (rc != E_POINTER)
3384 len = strlenW(lpszObjectName)+1;
3385 request->path = heap_alloc(len*sizeof(WCHAR));
3386 rc = UrlEscapeW(lpszObjectName, request->path, &len,
3387 URL_ESCAPE_SPACES_ONLY);
3388 if (rc != S_OK)
3390 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc);
3391 strcpyW(request->path,lpszObjectName);
3393 }else {
3394 static const WCHAR slashW[] = {'/',0};
3396 request->path = heap_strdupW(slashW);
3399 if (lpszReferrer && *lpszReferrer)
3400 HTTP_ProcessHeader(request, HTTP_REFERER, lpszReferrer, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
3402 if (lpszAcceptTypes)
3404 int i;
3405 for (i = 0; lpszAcceptTypes[i]; i++)
3407 if (!*lpszAcceptTypes[i]) continue;
3408 HTTP_ProcessHeader(request, HTTP_ACCEPT, lpszAcceptTypes[i],
3409 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA |
3410 HTTP_ADDHDR_FLAG_REQ |
3411 (i == 0 ? HTTP_ADDHDR_FLAG_REPLACE : 0));
3415 request->verb = heap_strdupW(lpszVerb && *lpszVerb ? lpszVerb : szGET);
3416 request->version = heap_strdupW(lpszVersion && *lpszVersion ? lpszVersion : g_szHttp1_1);
3418 HTTP_ProcessHeader(request, hostW, request->server->canon_host_port, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
3420 if (hIC->proxy && hIC->proxy[0] && !HTTP_ShouldBypassProxy(hIC, session->hostName))
3421 HTTP_DealWithProxy( hIC, session, request );
3423 INTERNET_SendCallback(&session->hdr, dwContext,
3424 INTERNET_STATUS_HANDLE_CREATED, &request->hdr.hInternet,
3425 sizeof(HINTERNET));
3427 TRACE("<-- (%p)\n", request);
3429 *ret = request->hdr.hInternet;
3430 return ERROR_SUCCESS;
3433 /***********************************************************************
3434 * HttpOpenRequestW (WININET.@)
3436 * Open a HTTP request handle
3438 * RETURNS
3439 * HINTERNET a HTTP request handle on success
3440 * NULL on failure
3443 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
3444 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
3445 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
3446 DWORD dwFlags, DWORD_PTR dwContext)
3448 http_session_t *session;
3449 HINTERNET handle = NULL;
3450 DWORD res;
3452 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
3453 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
3454 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
3455 dwFlags, dwContext);
3456 if(lpszAcceptTypes!=NULL)
3458 int i;
3459 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
3460 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
3463 session = (http_session_t*) get_handle_object( hHttpSession );
3464 if (NULL == session || session->hdr.htype != WH_HHTTPSESSION)
3466 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3467 goto lend;
3471 * My tests seem to show that the windows version does not
3472 * become asynchronous until after this point. And anyhow
3473 * if this call was asynchronous then how would you get the
3474 * necessary HINTERNET pointer returned by this function.
3477 res = HTTP_HttpOpenRequestW(session, lpszVerb, lpszObjectName,
3478 lpszVersion, lpszReferrer, lpszAcceptTypes,
3479 dwFlags, dwContext, &handle);
3480 lend:
3481 if( session )
3482 WININET_Release( &session->hdr );
3483 TRACE("returning %p\n", handle);
3484 if(res != ERROR_SUCCESS)
3485 SetLastError(res);
3486 return handle;
3489 static const LPCWSTR header_lookup[] = {
3490 szMime_Version, /* HTTP_QUERY_MIME_VERSION = 0 */
3491 szContent_Type, /* HTTP_QUERY_CONTENT_TYPE = 1 */
3492 szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
3493 szContent_ID, /* HTTP_QUERY_CONTENT_ID = 3 */
3494 NULL, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
3495 szContent_Length, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
3496 szContent_Language, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
3497 szAllow, /* HTTP_QUERY_ALLOW = 7 */
3498 szPublic, /* HTTP_QUERY_PUBLIC = 8 */
3499 szDate, /* HTTP_QUERY_DATE = 9 */
3500 szExpires, /* HTTP_QUERY_EXPIRES = 10 */
3501 szLast_Modified, /* HTTP_QUERY_LAST_MODIFIED = 11 */
3502 NULL, /* HTTP_QUERY_MESSAGE_ID = 12 */
3503 szURI, /* HTTP_QUERY_URI = 13 */
3504 szFrom, /* HTTP_QUERY_DERIVED_FROM = 14 */
3505 NULL, /* HTTP_QUERY_COST = 15 */
3506 NULL, /* HTTP_QUERY_LINK = 16 */
3507 szPragma, /* HTTP_QUERY_PRAGMA = 17 */
3508 NULL, /* HTTP_QUERY_VERSION = 18 */
3509 szStatus, /* HTTP_QUERY_STATUS_CODE = 19 */
3510 NULL, /* HTTP_QUERY_STATUS_TEXT = 20 */
3511 NULL, /* HTTP_QUERY_RAW_HEADERS = 21 */
3512 NULL, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
3513 szConnection, /* HTTP_QUERY_CONNECTION = 23 */
3514 szAccept, /* HTTP_QUERY_ACCEPT = 24 */
3515 szAccept_Charset, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
3516 szAccept_Encoding, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
3517 szAccept_Language, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
3518 szAuthorization, /* HTTP_QUERY_AUTHORIZATION = 28 */
3519 szContent_Encoding, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
3520 NULL, /* HTTP_QUERY_FORWARDED = 30 */
3521 NULL, /* HTTP_QUERY_FROM = 31 */
3522 szIf_Modified_Since, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
3523 szLocation, /* HTTP_QUERY_LOCATION = 33 */
3524 NULL, /* HTTP_QUERY_ORIG_URI = 34 */
3525 szReferer, /* HTTP_QUERY_REFERER = 35 */
3526 szRetry_After, /* HTTP_QUERY_RETRY_AFTER = 36 */
3527 szServer, /* HTTP_QUERY_SERVER = 37 */
3528 NULL, /* HTTP_TITLE = 38 */
3529 szUser_Agent, /* HTTP_QUERY_USER_AGENT = 39 */
3530 szWWW_Authenticate, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
3531 szProxy_Authenticate, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
3532 szAccept_Ranges, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
3533 szSet_Cookie, /* HTTP_QUERY_SET_COOKIE = 43 */
3534 szCookie, /* HTTP_QUERY_COOKIE = 44 */
3535 NULL, /* HTTP_QUERY_REQUEST_METHOD = 45 */
3536 NULL, /* HTTP_QUERY_REFRESH = 46 */
3537 szContent_Disposition, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
3538 szAge, /* HTTP_QUERY_AGE = 48 */
3539 szCache_Control, /* HTTP_QUERY_CACHE_CONTROL = 49 */
3540 szContent_Base, /* HTTP_QUERY_CONTENT_BASE = 50 */
3541 szContent_Location, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
3542 szContent_MD5, /* HTTP_QUERY_CONTENT_MD5 = 52 */
3543 szContent_Range, /* HTTP_QUERY_CONTENT_RANGE = 53 */
3544 szETag, /* HTTP_QUERY_ETAG = 54 */
3545 hostW, /* HTTP_QUERY_HOST = 55 */
3546 szIf_Match, /* HTTP_QUERY_IF_MATCH = 56 */
3547 szIf_None_Match, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
3548 szIf_Range, /* HTTP_QUERY_IF_RANGE = 58 */
3549 szIf_Unmodified_Since, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
3550 szMax_Forwards, /* HTTP_QUERY_MAX_FORWARDS = 60 */
3551 szProxy_Authorization, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
3552 szRange, /* HTTP_QUERY_RANGE = 62 */
3553 szTransfer_Encoding, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
3554 szUpgrade, /* HTTP_QUERY_UPGRADE = 64 */
3555 szVary, /* HTTP_QUERY_VARY = 65 */
3556 szVia, /* HTTP_QUERY_VIA = 66 */
3557 szWarning, /* HTTP_QUERY_WARNING = 67 */
3558 szExpect, /* HTTP_QUERY_EXPECT = 68 */
3559 szProxy_Connection, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
3560 szUnless_Modified_Since, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
3563 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
3565 /***********************************************************************
3566 * HTTP_HttpQueryInfoW (internal)
3568 static DWORD HTTP_HttpQueryInfoW(http_request_t *request, DWORD dwInfoLevel,
3569 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
3571 LPHTTPHEADERW lphttpHdr = NULL;
3572 BOOL request_only = !!(dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS);
3573 INT requested_index = lpdwIndex ? *lpdwIndex : 0;
3574 DWORD level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
3575 INT index = -1;
3577 EnterCriticalSection( &request->headers_section );
3579 /* Find requested header structure */
3580 switch (level)
3582 case HTTP_QUERY_CUSTOM:
3583 if (!lpBuffer)
3585 LeaveCriticalSection( &request->headers_section );
3586 return ERROR_INVALID_PARAMETER;
3588 index = HTTP_GetCustomHeaderIndex(request, lpBuffer, requested_index, request_only);
3589 break;
3590 case HTTP_QUERY_RAW_HEADERS_CRLF:
3592 LPWSTR headers;
3593 DWORD len = 0;
3594 DWORD res = ERROR_INVALID_PARAMETER;
3596 if (request_only)
3597 headers = build_request_header(request, request->verb, request->path, request->version, TRUE);
3598 else
3599 headers = build_response_header(request, TRUE);
3600 if (!headers)
3602 LeaveCriticalSection( &request->headers_section );
3603 return ERROR_OUTOFMEMORY;
3606 len = strlenW(headers) * sizeof(WCHAR);
3607 if (len + sizeof(WCHAR) > *lpdwBufferLength)
3609 len += sizeof(WCHAR);
3610 res = ERROR_INSUFFICIENT_BUFFER;
3612 else if (lpBuffer)
3614 memcpy(lpBuffer, headers, len + sizeof(WCHAR));
3615 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len / sizeof(WCHAR)));
3616 res = ERROR_SUCCESS;
3618 *lpdwBufferLength = len;
3620 heap_free(headers);
3621 LeaveCriticalSection( &request->headers_section );
3622 return res;
3624 case HTTP_QUERY_RAW_HEADERS:
3626 LPWSTR headers;
3627 DWORD len;
3629 if (request_only)
3630 headers = build_request_header(request, request->verb, request->path, request->version, FALSE);
3631 else
3632 headers = build_response_header(request, FALSE);
3634 if (!headers)
3636 LeaveCriticalSection( &request->headers_section );
3637 return ERROR_OUTOFMEMORY;
3640 len = strlenW(headers) * sizeof(WCHAR);
3641 if (len > *lpdwBufferLength)
3643 *lpdwBufferLength = len;
3644 heap_free(headers);
3645 LeaveCriticalSection( &request->headers_section );
3646 return ERROR_INSUFFICIENT_BUFFER;
3649 if (lpBuffer)
3651 DWORD i;
3653 TRACE("returning data: %s\n", debugstr_wn(headers, len / sizeof(WCHAR)));
3655 for (i = 0; i < len / sizeof(WCHAR); i++)
3657 if (headers[i] == '\n')
3658 headers[i] = 0;
3660 memcpy(lpBuffer, headers, len);
3662 *lpdwBufferLength = len - sizeof(WCHAR);
3664 heap_free(headers);
3665 LeaveCriticalSection( &request->headers_section );
3666 return ERROR_SUCCESS;
3668 case HTTP_QUERY_STATUS_TEXT:
3669 if (request->statusText)
3671 DWORD len = strlenW(request->statusText);
3672 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
3674 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
3675 LeaveCriticalSection( &request->headers_section );
3676 return ERROR_INSUFFICIENT_BUFFER;
3678 if (lpBuffer)
3680 memcpy(lpBuffer, request->statusText, (len + 1) * sizeof(WCHAR));
3681 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
3683 *lpdwBufferLength = len * sizeof(WCHAR);
3684 LeaveCriticalSection( &request->headers_section );
3685 return ERROR_SUCCESS;
3687 break;
3688 case HTTP_QUERY_VERSION:
3689 if (request->version)
3691 DWORD len = strlenW(request->version);
3692 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
3694 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
3695 LeaveCriticalSection( &request->headers_section );
3696 return ERROR_INSUFFICIENT_BUFFER;
3698 if (lpBuffer)
3700 memcpy(lpBuffer, request->version, (len + 1) * sizeof(WCHAR));
3701 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
3703 *lpdwBufferLength = len * sizeof(WCHAR);
3704 LeaveCriticalSection( &request->headers_section );
3705 return ERROR_SUCCESS;
3707 break;
3708 case HTTP_QUERY_CONTENT_ENCODING:
3709 index = HTTP_GetCustomHeaderIndex(request, header_lookup[request->read_gzip ? HTTP_QUERY_CONTENT_TYPE : level],
3710 requested_index,request_only);
3711 break;
3712 case HTTP_QUERY_STATUS_CODE: {
3713 DWORD res = ERROR_SUCCESS;
3715 if(request_only)
3717 LeaveCriticalSection( &request->headers_section );
3718 return ERROR_HTTP_INVALID_QUERY_REQUEST;
3721 if(requested_index)
3722 break;
3724 if(dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) {
3725 if(*lpdwBufferLength >= sizeof(DWORD))
3726 *(DWORD*)lpBuffer = request->status_code;
3727 else
3728 res = ERROR_INSUFFICIENT_BUFFER;
3729 *lpdwBufferLength = sizeof(DWORD);
3730 }else {
3731 WCHAR buf[12];
3732 DWORD size;
3733 static const WCHAR formatW[] = {'%','u',0};
3735 size = sprintfW(buf, formatW, request->status_code) * sizeof(WCHAR);
3737 if(size <= *lpdwBufferLength) {
3738 memcpy(lpBuffer, buf, size+sizeof(WCHAR));
3739 }else {
3740 size += sizeof(WCHAR);
3741 res = ERROR_INSUFFICIENT_BUFFER;
3744 *lpdwBufferLength = size;
3746 LeaveCriticalSection( &request->headers_section );
3747 return res;
3749 default:
3750 assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
3752 if (level < LAST_TABLE_HEADER && header_lookup[level])
3753 index = HTTP_GetCustomHeaderIndex(request, header_lookup[level],
3754 requested_index,request_only);
3757 if (index >= 0)
3758 lphttpHdr = &request->custHeaders[index];
3760 /* Ensure header satisfies requested attributes */
3761 if (!lphttpHdr ||
3762 ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
3763 (~lphttpHdr->wFlags & HDR_ISREQUEST)))
3765 LeaveCriticalSection( &request->headers_section );
3766 return ERROR_HTTP_HEADER_NOT_FOUND;
3769 /* coalesce value to requested type */
3770 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER && lpBuffer)
3772 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
3773 TRACE(" returning number: %d\n", *(int *)lpBuffer);
3775 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME && lpBuffer)
3777 time_t tmpTime;
3778 struct tm tmpTM;
3779 SYSTEMTIME *STHook;
3781 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
3783 tmpTM = *gmtime(&tmpTime);
3784 STHook = (SYSTEMTIME *)lpBuffer;
3785 STHook->wDay = tmpTM.tm_mday;
3786 STHook->wHour = tmpTM.tm_hour;
3787 STHook->wMilliseconds = 0;
3788 STHook->wMinute = tmpTM.tm_min;
3789 STHook->wDayOfWeek = tmpTM.tm_wday;
3790 STHook->wMonth = tmpTM.tm_mon + 1;
3791 STHook->wSecond = tmpTM.tm_sec;
3792 STHook->wYear = 1900+tmpTM.tm_year;
3794 TRACE(" returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
3795 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
3796 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
3798 else if (lphttpHdr->lpszValue)
3800 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
3802 if (len > *lpdwBufferLength)
3804 *lpdwBufferLength = len;
3805 LeaveCriticalSection( &request->headers_section );
3806 return ERROR_INSUFFICIENT_BUFFER;
3808 if (lpBuffer)
3810 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
3811 TRACE("! returning string: %s\n", debugstr_w(lpBuffer));
3813 *lpdwBufferLength = len - sizeof(WCHAR);
3815 if (lpdwIndex) (*lpdwIndex)++;
3817 LeaveCriticalSection( &request->headers_section );
3818 return ERROR_SUCCESS;
3821 /***********************************************************************
3822 * HttpQueryInfoW (WININET.@)
3824 * Queries for information about an HTTP request
3826 * RETURNS
3827 * TRUE on success
3828 * FALSE on failure
3831 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
3832 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
3834 http_request_t *request;
3835 DWORD res;
3837 if (TRACE_ON(wininet)) {
3838 #define FE(x) { x, #x }
3839 static const wininet_flag_info query_flags[] = {
3840 FE(HTTP_QUERY_MIME_VERSION),
3841 FE(HTTP_QUERY_CONTENT_TYPE),
3842 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
3843 FE(HTTP_QUERY_CONTENT_ID),
3844 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
3845 FE(HTTP_QUERY_CONTENT_LENGTH),
3846 FE(HTTP_QUERY_CONTENT_LANGUAGE),
3847 FE(HTTP_QUERY_ALLOW),
3848 FE(HTTP_QUERY_PUBLIC),
3849 FE(HTTP_QUERY_DATE),
3850 FE(HTTP_QUERY_EXPIRES),
3851 FE(HTTP_QUERY_LAST_MODIFIED),
3852 FE(HTTP_QUERY_MESSAGE_ID),
3853 FE(HTTP_QUERY_URI),
3854 FE(HTTP_QUERY_DERIVED_FROM),
3855 FE(HTTP_QUERY_COST),
3856 FE(HTTP_QUERY_LINK),
3857 FE(HTTP_QUERY_PRAGMA),
3858 FE(HTTP_QUERY_VERSION),
3859 FE(HTTP_QUERY_STATUS_CODE),
3860 FE(HTTP_QUERY_STATUS_TEXT),
3861 FE(HTTP_QUERY_RAW_HEADERS),
3862 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
3863 FE(HTTP_QUERY_CONNECTION),
3864 FE(HTTP_QUERY_ACCEPT),
3865 FE(HTTP_QUERY_ACCEPT_CHARSET),
3866 FE(HTTP_QUERY_ACCEPT_ENCODING),
3867 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
3868 FE(HTTP_QUERY_AUTHORIZATION),
3869 FE(HTTP_QUERY_CONTENT_ENCODING),
3870 FE(HTTP_QUERY_FORWARDED),
3871 FE(HTTP_QUERY_FROM),
3872 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
3873 FE(HTTP_QUERY_LOCATION),
3874 FE(HTTP_QUERY_ORIG_URI),
3875 FE(HTTP_QUERY_REFERER),
3876 FE(HTTP_QUERY_RETRY_AFTER),
3877 FE(HTTP_QUERY_SERVER),
3878 FE(HTTP_QUERY_TITLE),
3879 FE(HTTP_QUERY_USER_AGENT),
3880 FE(HTTP_QUERY_WWW_AUTHENTICATE),
3881 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
3882 FE(HTTP_QUERY_ACCEPT_RANGES),
3883 FE(HTTP_QUERY_SET_COOKIE),
3884 FE(HTTP_QUERY_COOKIE),
3885 FE(HTTP_QUERY_REQUEST_METHOD),
3886 FE(HTTP_QUERY_REFRESH),
3887 FE(HTTP_QUERY_CONTENT_DISPOSITION),
3888 FE(HTTP_QUERY_AGE),
3889 FE(HTTP_QUERY_CACHE_CONTROL),
3890 FE(HTTP_QUERY_CONTENT_BASE),
3891 FE(HTTP_QUERY_CONTENT_LOCATION),
3892 FE(HTTP_QUERY_CONTENT_MD5),
3893 FE(HTTP_QUERY_CONTENT_RANGE),
3894 FE(HTTP_QUERY_ETAG),
3895 FE(HTTP_QUERY_HOST),
3896 FE(HTTP_QUERY_IF_MATCH),
3897 FE(HTTP_QUERY_IF_NONE_MATCH),
3898 FE(HTTP_QUERY_IF_RANGE),
3899 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
3900 FE(HTTP_QUERY_MAX_FORWARDS),
3901 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
3902 FE(HTTP_QUERY_RANGE),
3903 FE(HTTP_QUERY_TRANSFER_ENCODING),
3904 FE(HTTP_QUERY_UPGRADE),
3905 FE(HTTP_QUERY_VARY),
3906 FE(HTTP_QUERY_VIA),
3907 FE(HTTP_QUERY_WARNING),
3908 FE(HTTP_QUERY_CUSTOM)
3910 static const wininet_flag_info modifier_flags[] = {
3911 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
3912 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
3913 FE(HTTP_QUERY_FLAG_NUMBER),
3914 FE(HTTP_QUERY_FLAG_COALESCE)
3916 #undef FE
3917 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
3918 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
3919 DWORD i;
3921 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, info);
3922 TRACE(" Attribute:");
3923 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
3924 if (query_flags[i].val == info) {
3925 TRACE(" %s", query_flags[i].name);
3926 break;
3929 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
3930 TRACE(" Unknown (%08x)", info);
3933 TRACE(" Modifier:");
3934 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
3935 if (modifier_flags[i].val & info_mod) {
3936 TRACE(" %s", modifier_flags[i].name);
3937 info_mod &= ~ modifier_flags[i].val;
3941 if (info_mod) {
3942 TRACE(" Unknown (%08x)", info_mod);
3944 TRACE("\n");
3947 request = (http_request_t*) get_handle_object( hHttpRequest );
3948 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
3950 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3951 goto lend;
3954 if (lpBuffer == NULL)
3955 *lpdwBufferLength = 0;
3956 res = HTTP_HttpQueryInfoW( request, dwInfoLevel,
3957 lpBuffer, lpdwBufferLength, lpdwIndex);
3959 lend:
3960 if( request )
3961 WININET_Release( &request->hdr );
3963 TRACE("%u <--\n", res);
3964 if(res != ERROR_SUCCESS)
3965 SetLastError(res);
3966 return res == ERROR_SUCCESS;
3969 /***********************************************************************
3970 * HttpQueryInfoA (WININET.@)
3972 * Queries for information about an HTTP request
3974 * RETURNS
3975 * TRUE on success
3976 * FALSE on failure
3979 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
3980 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
3982 BOOL result;
3983 DWORD len;
3984 WCHAR* bufferW;
3986 TRACE("%p %x\n", hHttpRequest, dwInfoLevel);
3988 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
3989 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
3991 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
3992 lpdwBufferLength, lpdwIndex );
3995 if (lpBuffer)
3997 DWORD alloclen;
3998 len = (*lpdwBufferLength)*sizeof(WCHAR);
3999 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
4001 alloclen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 ) * sizeof(WCHAR);
4002 if (alloclen < len)
4003 alloclen = len;
4005 else
4006 alloclen = len;
4007 bufferW = heap_alloc(alloclen);
4008 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
4009 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
4010 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, bufferW, alloclen / sizeof(WCHAR) );
4011 } else
4013 bufferW = NULL;
4014 len = 0;
4017 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
4018 &len, lpdwIndex );
4019 if( result )
4021 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
4022 lpBuffer, *lpdwBufferLength, NULL, NULL );
4023 *lpdwBufferLength = len - 1;
4025 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
4027 else
4028 /* since the strings being returned from HttpQueryInfoW should be
4029 * only ASCII characters, it is reasonable to assume that all of
4030 * the Unicode characters can be reduced to a single byte */
4031 *lpdwBufferLength = len / sizeof(WCHAR);
4033 heap_free( bufferW );
4034 return result;
4037 /***********************************************************************
4038 * HTTP_GetRedirectURL (internal)
4040 static LPWSTR HTTP_GetRedirectURL(http_request_t *request, LPCWSTR lpszUrl)
4042 static WCHAR szHttp[] = {'h','t','t','p',0};
4043 static WCHAR szHttps[] = {'h','t','t','p','s',0};
4044 http_session_t *session = request->session;
4045 URL_COMPONENTSW urlComponents;
4046 DWORD url_length = 0;
4047 LPWSTR orig_url;
4048 LPWSTR combined_url;
4050 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
4051 urlComponents.lpszScheme = (request->hdr.dwFlags & INTERNET_FLAG_SECURE) ? szHttps : szHttp;
4052 urlComponents.dwSchemeLength = 0;
4053 urlComponents.lpszHostName = request->server->name;
4054 urlComponents.dwHostNameLength = 0;
4055 urlComponents.nPort = request->server->port;
4056 urlComponents.lpszUserName = session->userName;
4057 urlComponents.dwUserNameLength = 0;
4058 urlComponents.lpszPassword = NULL;
4059 urlComponents.dwPasswordLength = 0;
4060 urlComponents.lpszUrlPath = request->path;
4061 urlComponents.dwUrlPathLength = 0;
4062 urlComponents.lpszExtraInfo = NULL;
4063 urlComponents.dwExtraInfoLength = 0;
4065 if (!InternetCreateUrlW(&urlComponents, 0, NULL, &url_length) &&
4066 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
4067 return NULL;
4069 orig_url = heap_alloc(url_length);
4071 /* convert from bytes to characters */
4072 url_length = url_length / sizeof(WCHAR) - 1;
4073 if (!InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length))
4075 heap_free(orig_url);
4076 return NULL;
4079 url_length = 0;
4080 if (!InternetCombineUrlW(orig_url, lpszUrl, NULL, &url_length, ICU_ENCODE_SPACES_ONLY) &&
4081 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
4083 heap_free(orig_url);
4084 return NULL;
4086 combined_url = heap_alloc(url_length * sizeof(WCHAR));
4088 if (!InternetCombineUrlW(orig_url, lpszUrl, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY))
4090 heap_free(orig_url);
4091 heap_free(combined_url);
4092 return NULL;
4094 heap_free(orig_url);
4095 return combined_url;
4099 /***********************************************************************
4100 * HTTP_HandleRedirect (internal)
4102 static DWORD HTTP_HandleRedirect(http_request_t *request, LPCWSTR lpszUrl)
4104 http_session_t *session = request->session;
4105 WCHAR path[INTERNET_MAX_PATH_LENGTH];
4107 if(lpszUrl[0]=='/')
4109 /* if it's an absolute path, keep the same session info */
4110 lstrcpynW(path, lpszUrl, INTERNET_MAX_URL_LENGTH);
4112 else
4114 URL_COMPONENTSW urlComponents;
4115 WCHAR protocol[INTERNET_MAX_SCHEME_LENGTH];
4116 WCHAR hostName[INTERNET_MAX_HOST_NAME_LENGTH];
4117 WCHAR userName[INTERNET_MAX_USER_NAME_LENGTH];
4118 BOOL custom_port = FALSE;
4120 static const WCHAR httpW[] = {'h','t','t','p',0};
4121 static const WCHAR httpsW[] = {'h','t','t','p','s',0};
4123 userName[0] = 0;
4124 hostName[0] = 0;
4125 protocol[0] = 0;
4127 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
4128 urlComponents.lpszScheme = protocol;
4129 urlComponents.dwSchemeLength = INTERNET_MAX_SCHEME_LENGTH;
4130 urlComponents.lpszHostName = hostName;
4131 urlComponents.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH;
4132 urlComponents.lpszUserName = userName;
4133 urlComponents.dwUserNameLength = INTERNET_MAX_USER_NAME_LENGTH;
4134 urlComponents.lpszPassword = NULL;
4135 urlComponents.dwPasswordLength = 0;
4136 urlComponents.lpszUrlPath = path;
4137 urlComponents.dwUrlPathLength = INTERNET_MAX_PATH_LENGTH;
4138 urlComponents.lpszExtraInfo = NULL;
4139 urlComponents.dwExtraInfoLength = 0;
4140 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
4141 return INTERNET_GetLastError();
4143 if(!strcmpiW(protocol, httpW)) {
4144 if(request->hdr.dwFlags & INTERNET_FLAG_SECURE) {
4145 TRACE("redirect from secure page to non-secure page\n");
4146 /* FIXME: warn about from secure redirect to non-secure page */
4147 request->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
4150 if(urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
4151 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
4152 else if(urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT)
4153 custom_port = TRUE;
4154 }else if(!strcmpiW(protocol, httpsW)) {
4155 if(!(request->hdr.dwFlags & INTERNET_FLAG_SECURE)) {
4156 TRACE("redirect from non-secure page to secure page\n");
4157 /* FIXME: notify about redirect to secure page */
4158 request->hdr.dwFlags |= INTERNET_FLAG_SECURE;
4161 if(urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
4162 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
4163 else if(urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT)
4164 custom_port = TRUE;
4167 heap_free(session->hostName);
4169 session->hostName = heap_strdupW(hostName);
4170 session->hostPort = urlComponents.nPort;
4172 heap_free(session->userName);
4173 session->userName = NULL;
4174 if (userName[0])
4175 session->userName = heap_strdupW(userName);
4177 reset_data_stream(request);
4179 if(strcmpiW(request->server->name, hostName) || request->server->port != urlComponents.nPort) {
4180 server_t *new_server;
4182 new_server = get_server(hostName, urlComponents.nPort, urlComponents.nScheme == INTERNET_SCHEME_HTTPS, TRUE);
4183 server_release(request->server);
4184 request->server = new_server;
4187 if (custom_port)
4188 HTTP_ProcessHeader(request, hostW, request->server->host_port, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
4189 else
4190 HTTP_ProcessHeader(request, hostW, request->server->name, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
4192 heap_free(request->path);
4193 request->path=NULL;
4194 if (*path)
4196 DWORD needed = 0;
4197 HRESULT rc;
4199 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
4200 if (rc != E_POINTER)
4201 needed = strlenW(path)+1;
4202 request->path = heap_alloc(needed*sizeof(WCHAR));
4203 rc = UrlEscapeW(path, request->path, &needed,
4204 URL_ESCAPE_SPACES_ONLY);
4205 if (rc != S_OK)
4207 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
4208 strcpyW(request->path,path);
4212 /* Remove custom content-type/length headers on redirects. */
4213 remove_header(request, szContent_Type, TRUE);
4214 remove_header(request, szContent_Length, TRUE);
4216 return ERROR_SUCCESS;
4219 /***********************************************************************
4220 * HTTP_build_req (internal)
4222 * concatenate all the strings in the request together
4224 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
4226 LPCWSTR *t;
4227 LPWSTR str;
4229 for( t = list; *t ; t++ )
4230 len += strlenW( *t );
4231 len++;
4233 str = heap_alloc(len*sizeof(WCHAR));
4234 *str = 0;
4236 for( t = list; *t ; t++ )
4237 strcatW( str, *t );
4239 return str;
4242 static void HTTP_InsertCookies(http_request_t *request)
4244 WCHAR *cookies;
4245 DWORD res;
4247 res = get_cookie_header(request->server->name, request->path, &cookies);
4248 if(res != ERROR_SUCCESS || !cookies)
4249 return;
4251 HTTP_HttpAddRequestHeadersW(request, cookies, strlenW(cookies), HTTP_ADDREQ_FLAG_REPLACE);
4252 heap_free(cookies);
4255 static WORD HTTP_ParseWkday(LPCWSTR day)
4257 static const WCHAR days[7][4] = {{ 's','u','n',0 },
4258 { 'm','o','n',0 },
4259 { 't','u','e',0 },
4260 { 'w','e','d',0 },
4261 { 't','h','u',0 },
4262 { 'f','r','i',0 },
4263 { 's','a','t',0 }};
4264 unsigned int i;
4265 for (i = 0; i < sizeof(days)/sizeof(*days); i++)
4266 if (!strcmpiW(day, days[i]))
4267 return i;
4269 /* Invalid */
4270 return 7;
4273 static WORD HTTP_ParseMonth(LPCWSTR month)
4275 static const WCHAR jan[] = { 'j','a','n',0 };
4276 static const WCHAR feb[] = { 'f','e','b',0 };
4277 static const WCHAR mar[] = { 'm','a','r',0 };
4278 static const WCHAR apr[] = { 'a','p','r',0 };
4279 static const WCHAR may[] = { 'm','a','y',0 };
4280 static const WCHAR jun[] = { 'j','u','n',0 };
4281 static const WCHAR jul[] = { 'j','u','l',0 };
4282 static const WCHAR aug[] = { 'a','u','g',0 };
4283 static const WCHAR sep[] = { 's','e','p',0 };
4284 static const WCHAR oct[] = { 'o','c','t',0 };
4285 static const WCHAR nov[] = { 'n','o','v',0 };
4286 static const WCHAR dec[] = { 'd','e','c',0 };
4288 if (!strcmpiW(month, jan)) return 1;
4289 if (!strcmpiW(month, feb)) return 2;
4290 if (!strcmpiW(month, mar)) return 3;
4291 if (!strcmpiW(month, apr)) return 4;
4292 if (!strcmpiW(month, may)) return 5;
4293 if (!strcmpiW(month, jun)) return 6;
4294 if (!strcmpiW(month, jul)) return 7;
4295 if (!strcmpiW(month, aug)) return 8;
4296 if (!strcmpiW(month, sep)) return 9;
4297 if (!strcmpiW(month, oct)) return 10;
4298 if (!strcmpiW(month, nov)) return 11;
4299 if (!strcmpiW(month, dec)) return 12;
4300 /* Invalid */
4301 return 0;
4304 /* Parses the string pointed to by *str, assumed to be a 24-hour time HH:MM:SS,
4305 * optionally preceded by whitespace.
4306 * Upon success, returns TRUE, sets the wHour, wMinute, and wSecond fields of
4307 * st, and sets *str to the first character after the time format.
4309 static BOOL HTTP_ParseTime(SYSTEMTIME *st, LPCWSTR *str)
4311 LPCWSTR ptr = *str;
4312 WCHAR *nextPtr;
4313 unsigned long num;
4315 while (isspaceW(*ptr))
4316 ptr++;
4318 num = strtoulW(ptr, &nextPtr, 10);
4319 if (!nextPtr || nextPtr <= ptr || *nextPtr != ':')
4321 ERR("unexpected time format %s\n", debugstr_w(ptr));
4322 return FALSE;
4324 if (num > 23)
4326 ERR("unexpected hour in time format %s\n", debugstr_w(ptr));
4327 return FALSE;
4329 ptr = nextPtr + 1;
4330 st->wHour = (WORD)num;
4331 num = strtoulW(ptr, &nextPtr, 10);
4332 if (!nextPtr || nextPtr <= ptr || *nextPtr != ':')
4334 ERR("unexpected time format %s\n", debugstr_w(ptr));
4335 return FALSE;
4337 if (num > 59)
4339 ERR("unexpected minute in time format %s\n", debugstr_w(ptr));
4340 return FALSE;
4342 ptr = nextPtr + 1;
4343 st->wMinute = (WORD)num;
4344 num = strtoulW(ptr, &nextPtr, 10);
4345 if (!nextPtr || nextPtr <= ptr)
4347 ERR("unexpected time format %s\n", debugstr_w(ptr));
4348 return FALSE;
4350 if (num > 59)
4352 ERR("unexpected second in time format %s\n", debugstr_w(ptr));
4353 return FALSE;
4355 ptr = nextPtr + 1;
4356 *str = ptr;
4357 st->wSecond = (WORD)num;
4358 return TRUE;
4361 static BOOL HTTP_ParseDateAsAsctime(LPCWSTR value, FILETIME *ft)
4363 static const WCHAR gmt[]= { 'G','M','T',0 };
4364 WCHAR day[4], *dayPtr, month[4], *monthPtr, *nextPtr;
4365 LPCWSTR ptr;
4366 SYSTEMTIME st = { 0 };
4367 unsigned long num;
4369 for (ptr = value, dayPtr = day; *ptr && !isspaceW(*ptr) &&
4370 dayPtr - day < sizeof(day) / sizeof(day[0]) - 1; ptr++, dayPtr++)
4371 *dayPtr = *ptr;
4372 *dayPtr = 0;
4373 st.wDayOfWeek = HTTP_ParseWkday(day);
4374 if (st.wDayOfWeek >= 7)
4376 ERR("unexpected weekday %s\n", debugstr_w(day));
4377 return FALSE;
4380 while (isspaceW(*ptr))
4381 ptr++;
4383 for (monthPtr = month; !isspace(*ptr) &&
4384 monthPtr - month < sizeof(month) / sizeof(month[0]) - 1;
4385 monthPtr++, ptr++)
4386 *monthPtr = *ptr;
4387 *monthPtr = 0;
4388 st.wMonth = HTTP_ParseMonth(month);
4389 if (!st.wMonth || st.wMonth > 12)
4391 ERR("unexpected month %s\n", debugstr_w(month));
4392 return FALSE;
4395 while (isspaceW(*ptr))
4396 ptr++;
4398 num = strtoulW(ptr, &nextPtr, 10);
4399 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4401 ERR("unexpected day %s\n", debugstr_w(ptr));
4402 return FALSE;
4404 ptr = nextPtr;
4405 st.wDay = (WORD)num;
4407 while (isspaceW(*ptr))
4408 ptr++;
4410 if (!HTTP_ParseTime(&st, &ptr))
4411 return FALSE;
4413 while (isspaceW(*ptr))
4414 ptr++;
4416 num = strtoulW(ptr, &nextPtr, 10);
4417 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4419 ERR("unexpected year %s\n", debugstr_w(ptr));
4420 return FALSE;
4422 ptr = nextPtr;
4423 st.wYear = (WORD)num;
4425 while (isspaceW(*ptr))
4426 ptr++;
4428 /* asctime() doesn't report a timezone, but some web servers do, so accept
4429 * with or without GMT.
4431 if (*ptr && strcmpW(ptr, gmt))
4433 ERR("unexpected timezone %s\n", debugstr_w(ptr));
4434 return FALSE;
4436 return SystemTimeToFileTime(&st, ft);
4439 static BOOL HTTP_ParseRfc1123Date(LPCWSTR value, FILETIME *ft)
4441 static const WCHAR gmt[]= { 'G','M','T',0 };
4442 WCHAR *nextPtr, day[4], month[4], *monthPtr;
4443 LPCWSTR ptr;
4444 unsigned long num;
4445 SYSTEMTIME st = { 0 };
4447 ptr = strchrW(value, ',');
4448 if (!ptr)
4449 return FALSE;
4450 if (ptr - value != 3)
4452 WARN("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4453 return FALSE;
4455 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4456 day[3] = 0;
4457 st.wDayOfWeek = HTTP_ParseWkday(day);
4458 if (st.wDayOfWeek > 6)
4460 WARN("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4461 return FALSE;
4463 ptr++;
4465 while (isspaceW(*ptr))
4466 ptr++;
4468 num = strtoulW(ptr, &nextPtr, 10);
4469 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4471 WARN("unexpected day %s\n", debugstr_w(value));
4472 return FALSE;
4474 ptr = nextPtr;
4475 st.wDay = (WORD)num;
4477 while (isspaceW(*ptr))
4478 ptr++;
4480 for (monthPtr = month; !isspace(*ptr) &&
4481 monthPtr - month < sizeof(month) / sizeof(month[0]) - 1;
4482 monthPtr++, ptr++)
4483 *monthPtr = *ptr;
4484 *monthPtr = 0;
4485 st.wMonth = HTTP_ParseMonth(month);
4486 if (!st.wMonth || st.wMonth > 12)
4488 WARN("unexpected month %s\n", debugstr_w(month));
4489 return FALSE;
4492 while (isspaceW(*ptr))
4493 ptr++;
4495 num = strtoulW(ptr, &nextPtr, 10);
4496 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4498 ERR("unexpected year %s\n", debugstr_w(value));
4499 return FALSE;
4501 ptr = nextPtr;
4502 st.wYear = (WORD)num;
4504 if (!HTTP_ParseTime(&st, &ptr))
4505 return FALSE;
4507 while (isspaceW(*ptr))
4508 ptr++;
4510 if (strcmpW(ptr, gmt))
4512 ERR("unexpected time zone %s\n", debugstr_w(ptr));
4513 return FALSE;
4515 return SystemTimeToFileTime(&st, ft);
4518 static WORD HTTP_ParseWeekday(LPCWSTR day)
4520 static const WCHAR days[7][10] = {{ 's','u','n','d','a','y',0 },
4521 { 'm','o','n','d','a','y',0 },
4522 { 't','u','e','s','d','a','y',0 },
4523 { 'w','e','d','n','e','s','d','a','y',0 },
4524 { 't','h','u','r','s','d','a','y',0 },
4525 { 'f','r','i','d','a','y',0 },
4526 { 's','a','t','u','r','d','a','y',0 }};
4527 unsigned int i;
4528 for (i = 0; i < sizeof(days)/sizeof(*days); i++)
4529 if (!strcmpiW(day, days[i]))
4530 return i;
4532 /* Invalid */
4533 return 7;
4536 static BOOL HTTP_ParseRfc850Date(LPCWSTR value, FILETIME *ft)
4538 static const WCHAR gmt[]= { 'G','M','T',0 };
4539 WCHAR *nextPtr, day[10], month[4], *monthPtr;
4540 LPCWSTR ptr;
4541 unsigned long num;
4542 SYSTEMTIME st = { 0 };
4544 ptr = strchrW(value, ',');
4545 if (!ptr)
4546 return FALSE;
4547 if (ptr - value == 3)
4549 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4550 day[3] = 0;
4551 st.wDayOfWeek = HTTP_ParseWkday(day);
4552 if (st.wDayOfWeek > 6)
4554 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4555 return FALSE;
4558 else if (ptr - value < sizeof(day) / sizeof(day[0]))
4560 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4561 day[ptr - value + 1] = 0;
4562 st.wDayOfWeek = HTTP_ParseWeekday(day);
4563 if (st.wDayOfWeek > 6)
4565 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4566 return FALSE;
4569 else
4571 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4572 return FALSE;
4574 ptr++;
4576 while (isspaceW(*ptr))
4577 ptr++;
4579 num = strtoulW(ptr, &nextPtr, 10);
4580 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4582 ERR("unexpected day %s\n", debugstr_w(value));
4583 return FALSE;
4585 ptr = nextPtr;
4586 st.wDay = (WORD)num;
4588 if (*ptr != '-')
4590 ERR("unexpected month format %s\n", debugstr_w(ptr));
4591 return FALSE;
4593 ptr++;
4595 for (monthPtr = month; *ptr != '-' &&
4596 monthPtr - month < sizeof(month) / sizeof(month[0]) - 1;
4597 monthPtr++, ptr++)
4598 *monthPtr = *ptr;
4599 *monthPtr = 0;
4600 st.wMonth = HTTP_ParseMonth(month);
4601 if (!st.wMonth || st.wMonth > 12)
4603 ERR("unexpected month %s\n", debugstr_w(month));
4604 return FALSE;
4607 if (*ptr != '-')
4609 ERR("unexpected year format %s\n", debugstr_w(ptr));
4610 return FALSE;
4612 ptr++;
4614 num = strtoulW(ptr, &nextPtr, 10);
4615 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4617 ERR("unexpected year %s\n", debugstr_w(value));
4618 return FALSE;
4620 ptr = nextPtr;
4621 st.wYear = (WORD)num;
4623 if (!HTTP_ParseTime(&st, &ptr))
4624 return FALSE;
4626 while (isspaceW(*ptr))
4627 ptr++;
4629 if (strcmpW(ptr, gmt))
4631 ERR("unexpected time zone %s\n", debugstr_w(ptr));
4632 return FALSE;
4634 return SystemTimeToFileTime(&st, ft);
4637 static BOOL HTTP_ParseDate(LPCWSTR value, FILETIME *ft)
4639 static const WCHAR zero[] = { '0',0 };
4640 BOOL ret;
4642 if (!strcmpW(value, zero))
4644 ft->dwLowDateTime = ft->dwHighDateTime = 0;
4645 ret = TRUE;
4647 else if (strchrW(value, ','))
4649 ret = HTTP_ParseRfc1123Date(value, ft);
4650 if (!ret)
4652 ret = HTTP_ParseRfc850Date(value, ft);
4653 if (!ret)
4654 ERR("unexpected date format %s\n", debugstr_w(value));
4657 else
4659 ret = HTTP_ParseDateAsAsctime(value, ft);
4660 if (!ret)
4661 ERR("unexpected date format %s\n", debugstr_w(value));
4663 return ret;
4666 static void HTTP_ProcessExpires(http_request_t *request)
4668 BOOL expirationFound = FALSE;
4669 int headerIndex;
4671 EnterCriticalSection( &request->headers_section );
4673 /* Look for a Cache-Control header with a max-age directive, as it takes
4674 * precedence over the Expires header.
4676 headerIndex = HTTP_GetCustomHeaderIndex(request, szCache_Control, 0, FALSE);
4677 if (headerIndex != -1)
4679 LPHTTPHEADERW ccHeader = &request->custHeaders[headerIndex];
4680 LPWSTR ptr;
4682 for (ptr = ccHeader->lpszValue; ptr && *ptr; )
4684 LPWSTR comma = strchrW(ptr, ','), end, equal;
4686 if (comma)
4687 end = comma;
4688 else
4689 end = ptr + strlenW(ptr);
4690 for (equal = end - 1; equal > ptr && *equal != '='; equal--)
4692 if (*equal == '=')
4694 static const WCHAR max_age[] = {
4695 'm','a','x','-','a','g','e',0 };
4697 if (!strncmpiW(ptr, max_age, equal - ptr - 1))
4699 LPWSTR nextPtr;
4700 unsigned long age;
4702 age = strtoulW(equal + 1, &nextPtr, 10);
4703 if (nextPtr > equal + 1)
4705 LARGE_INTEGER ft;
4707 NtQuerySystemTime( &ft );
4708 /* Age is in seconds, FILETIME resolution is in
4709 * 100 nanosecond intervals.
4711 ft.QuadPart += age * (ULONGLONG)1000000;
4712 request->expires.dwLowDateTime = ft.u.LowPart;
4713 request->expires.dwHighDateTime = ft.u.HighPart;
4714 expirationFound = TRUE;
4718 if (comma)
4720 ptr = comma + 1;
4721 while (isspaceW(*ptr))
4722 ptr++;
4724 else
4725 ptr = NULL;
4728 if (!expirationFound)
4730 headerIndex = HTTP_GetCustomHeaderIndex(request, szExpires, 0, FALSE);
4731 if (headerIndex != -1)
4733 LPHTTPHEADERW expiresHeader = &request->custHeaders[headerIndex];
4734 FILETIME ft;
4736 if (HTTP_ParseDate(expiresHeader->lpszValue, &ft))
4738 expirationFound = TRUE;
4739 request->expires = ft;
4743 if (!expirationFound)
4745 LARGE_INTEGER t;
4747 /* With no known age, default to 10 minutes until expiration. */
4748 NtQuerySystemTime( &t );
4749 t.QuadPart += 10 * 60 * (ULONGLONG)10000000;
4750 request->expires.dwLowDateTime = t.u.LowPart;
4751 request->expires.dwHighDateTime = t.u.HighPart;
4754 LeaveCriticalSection( &request->headers_section );
4757 static void HTTP_ProcessLastModified(http_request_t *request)
4759 int headerIndex;
4761 EnterCriticalSection( &request->headers_section );
4763 headerIndex = HTTP_GetCustomHeaderIndex(request, szLast_Modified, 0, FALSE);
4764 if (headerIndex != -1)
4766 LPHTTPHEADERW expiresHeader = &request->custHeaders[headerIndex];
4767 FILETIME ft;
4769 if (HTTP_ParseDate(expiresHeader->lpszValue, &ft))
4770 request->last_modified = ft;
4773 LeaveCriticalSection( &request->headers_section );
4776 static void http_process_keep_alive(http_request_t *req)
4778 int index;
4780 EnterCriticalSection( &req->headers_section );
4782 if ((index = HTTP_GetCustomHeaderIndex(req, szConnection, 0, FALSE)) != -1)
4783 req->netconn->keep_alive = !strcmpiW(req->custHeaders[index].lpszValue, szKeepAlive);
4784 else if ((index = HTTP_GetCustomHeaderIndex(req, szProxy_Connection, 0, FALSE)) != -1)
4785 req->netconn->keep_alive = !strcmpiW(req->custHeaders[index].lpszValue, szKeepAlive);
4786 else
4787 req->netconn->keep_alive = !strcmpiW(req->version, g_szHttp1_1);
4789 LeaveCriticalSection( &req->headers_section );
4792 static DWORD open_http_connection(http_request_t *request, BOOL *reusing)
4794 const BOOL is_https = (request->hdr.dwFlags & INTERNET_FLAG_SECURE) != 0;
4795 netconn_t *netconn = NULL;
4796 DWORD res;
4798 reset_data_stream(request);
4800 if (request->netconn)
4802 if (is_valid_netconn(request->netconn) && NETCON_is_alive(request->netconn))
4804 *reusing = TRUE;
4805 return ERROR_SUCCESS;
4807 else
4809 free_netconn(request->netconn);
4810 request->netconn = NULL;
4814 res = HTTP_ResolveName(request);
4815 if(res != ERROR_SUCCESS)
4816 return res;
4818 EnterCriticalSection(&connection_pool_cs);
4820 while(!list_empty(&request->server->conn_pool)) {
4821 netconn = LIST_ENTRY(list_head(&request->server->conn_pool), netconn_t, pool_entry);
4822 list_remove(&netconn->pool_entry);
4824 if(is_valid_netconn(netconn) && NETCON_is_alive(netconn))
4825 break;
4827 TRACE("connection %p closed during idle\n", netconn);
4828 free_netconn(netconn);
4829 netconn = NULL;
4832 LeaveCriticalSection(&connection_pool_cs);
4834 if(netconn) {
4835 TRACE("<-- reusing %p netconn\n", netconn);
4836 request->netconn = netconn;
4837 *reusing = TRUE;
4838 return ERROR_SUCCESS;
4841 TRACE("connecting to %s, proxy %s\n", debugstr_w(request->server->name),
4842 request->proxy ? debugstr_w(request->proxy->name) : "(null)");
4844 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
4845 INTERNET_STATUS_CONNECTING_TO_SERVER,
4846 request->server->addr_str,
4847 strlen(request->server->addr_str)+1);
4849 res = create_netconn(is_https, request->proxy ? request->proxy : request->server, request->security_flags,
4850 (request->hdr.ErrorMask & INTERNET_ERROR_MASK_COMBINED_SEC_CERT) != 0,
4851 request->connect_timeout, &netconn);
4852 if(res != ERROR_SUCCESS) {
4853 ERR("create_netconn failed: %u\n", res);
4854 return res;
4857 request->netconn = netconn;
4859 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
4860 INTERNET_STATUS_CONNECTED_TO_SERVER,
4861 request->server->addr_str, strlen(request->server->addr_str)+1);
4863 *reusing = FALSE;
4864 TRACE("Created connection to %s: %p\n", debugstr_w(request->server->name), netconn);
4865 return ERROR_SUCCESS;
4868 static char *build_ascii_request( const WCHAR *str, void *data, DWORD data_len, DWORD *out_len )
4870 int len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
4871 char *ret;
4873 if (!(ret = heap_alloc( len + data_len ))) return NULL;
4874 WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
4875 if (data_len) memcpy( ret + len - 1, data, data_len );
4876 *out_len = len + data_len - 1;
4877 ret[*out_len] = 0;
4878 return ret;
4881 /***********************************************************************
4882 * HTTP_HttpSendRequestW (internal)
4884 * Sends the specified request to the HTTP server
4886 * RETURNS
4887 * ERROR_SUCCESS on success
4888 * win32 error code on failure
4891 static DWORD HTTP_HttpSendRequestW(http_request_t *request, LPCWSTR lpszHeaders,
4892 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
4893 DWORD dwContentLength, BOOL bEndRequest)
4895 static const WCHAR szContentLength[] =
4896 { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0 };
4897 BOOL redirected = FALSE, secure_proxy_connect = FALSE, loop_next;
4898 LPWSTR requestString = NULL;
4899 INT responseLen, cnt;
4900 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \r\n */ + 20 /* int */ ];
4901 DWORD res;
4903 TRACE("--> %p\n", request);
4905 assert(request->hdr.htype == WH_HHTTPREQ);
4907 /* if the verb is NULL default to GET */
4908 if (!request->verb)
4909 request->verb = heap_strdupW(szGET);
4911 if (dwContentLength || strcmpW(request->verb, szGET))
4913 sprintfW(contentLengthStr, szContentLength, dwContentLength);
4914 HTTP_HttpAddRequestHeadersW(request, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4915 request->bytesToWrite = dwContentLength;
4917 if (request->session->appInfo->agent)
4919 WCHAR *agent_header;
4920 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0};
4921 int len;
4923 len = strlenW(request->session->appInfo->agent) + strlenW(user_agent);
4924 agent_header = heap_alloc(len * sizeof(WCHAR));
4925 sprintfW(agent_header, user_agent, request->session->appInfo->agent);
4927 HTTP_HttpAddRequestHeadersW(request, agent_header, strlenW(agent_header), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4928 heap_free(agent_header);
4930 if (request->hdr.dwFlags & INTERNET_FLAG_PRAGMA_NOCACHE)
4932 static const WCHAR pragma_nocache[] = {'P','r','a','g','m','a',':',' ','n','o','-','c','a','c','h','e','\r','\n',0};
4933 HTTP_HttpAddRequestHeadersW(request, pragma_nocache, strlenW(pragma_nocache), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4935 if ((request->hdr.dwFlags & INTERNET_FLAG_NO_CACHE_WRITE) && strcmpW(request->verb, szGET))
4937 static const WCHAR cache_control[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l',':',
4938 ' ','n','o','-','c','a','c','h','e','\r','\n',0};
4939 HTTP_HttpAddRequestHeadersW(request, cache_control, strlenW(cache_control), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4942 /* add the headers the caller supplied */
4943 if( lpszHeaders && dwHeaderLength )
4944 HTTP_HttpAddRequestHeadersW(request, lpszHeaders, dwHeaderLength, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
4948 DWORD len, data_len = dwOptionalLength;
4949 BOOL reusing_connection;
4950 char *ascii_req;
4952 loop_next = FALSE;
4954 if(redirected) {
4955 request->contentLength = ~0u;
4956 request->bytesToWrite = 0;
4959 if (TRACE_ON(wininet))
4961 HTTPHEADERW *host;
4963 EnterCriticalSection( &request->headers_section );
4964 host = HTTP_GetHeader( request, hostW );
4965 TRACE("Going to url %s %s\n", debugstr_w(host->lpszValue), debugstr_w(request->path));
4966 LeaveCriticalSection( &request->headers_section );
4969 HTTP_FixURL(request);
4970 if (request->hdr.dwFlags & INTERNET_FLAG_KEEP_CONNECTION)
4972 HTTP_ProcessHeader(request, szConnection, szKeepAlive, HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
4974 HTTP_InsertAuthorization(request, request->authInfo, szAuthorization);
4975 HTTP_InsertAuthorization(request, request->proxyAuthInfo, szProxy_Authorization);
4977 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES))
4978 HTTP_InsertCookies(request);
4980 res = open_http_connection(request, &reusing_connection);
4981 if (res != ERROR_SUCCESS)
4982 break;
4984 if (!reusing_connection && (request->hdr.dwFlags & INTERNET_FLAG_SECURE))
4986 if (request->proxy) secure_proxy_connect = TRUE;
4987 else
4989 res = NETCON_secure_connect(request->netconn, request->server);
4990 if (res != ERROR_SUCCESS)
4992 WARN("failed to upgrade to secure connection\n");
4993 http_release_netconn(request, FALSE);
4994 break;
4998 if (secure_proxy_connect)
5000 static const WCHAR connectW[] = {'C','O','N','N','E','C','T',0};
5001 const WCHAR *target = request->server->host_port;
5002 requestString = build_request_header(request, connectW, target, g_szHttp1_1, TRUE);
5004 else if (request->proxy && !(request->hdr.dwFlags & INTERNET_FLAG_SECURE))
5006 WCHAR *url = build_proxy_path_url(request);
5007 requestString = build_request_header(request, request->verb, url, request->version, TRUE);
5008 heap_free(url);
5010 else
5011 requestString = build_request_header(request, request->verb, request->path, request->version, TRUE);
5013 TRACE("Request header -> %s\n", debugstr_w(requestString) );
5015 /* send the request as ASCII, tack on the optional data */
5016 if (!lpOptional || redirected || secure_proxy_connect)
5017 data_len = 0;
5019 ascii_req = build_ascii_request( requestString, lpOptional, data_len, &len );
5020 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
5022 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5023 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
5025 NETCON_set_timeout( request->netconn, TRUE, request->send_timeout );
5026 res = NETCON_send(request->netconn, ascii_req, len, 0, &cnt);
5027 heap_free( ascii_req );
5028 if(res != ERROR_SUCCESS) {
5029 TRACE("send failed: %u\n", res);
5030 if(!reusing_connection)
5031 break;
5032 http_release_netconn(request, FALSE);
5033 loop_next = TRUE;
5034 continue;
5037 request->bytesWritten = data_len;
5039 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5040 INTERNET_STATUS_REQUEST_SENT,
5041 &len, sizeof(DWORD));
5043 if (bEndRequest)
5045 DWORD dwBufferSize;
5047 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5048 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
5050 if (HTTP_GetResponseHeaders(request, &responseLen))
5052 http_release_netconn(request, FALSE);
5053 res = ERROR_INTERNET_CONNECTION_ABORTED;
5054 goto lend;
5056 /* FIXME: We should know that connection is closed before sending
5057 * headers. Otherwise wrong callbacks are executed */
5058 if(!responseLen && reusing_connection) {
5059 TRACE("Connection closed by server, reconnecting\n");
5060 http_release_netconn(request, FALSE);
5061 loop_next = TRUE;
5062 continue;
5065 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5066 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
5067 sizeof(DWORD));
5069 http_process_keep_alive(request);
5070 HTTP_ProcessCookies(request);
5071 HTTP_ProcessExpires(request);
5072 HTTP_ProcessLastModified(request);
5074 res = set_content_length(request);
5075 if(res != ERROR_SUCCESS)
5076 goto lend;
5077 if(!request->contentLength)
5078 http_release_netconn(request, TRUE);
5080 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && responseLen)
5082 WCHAR *new_url, szNewLocation[INTERNET_MAX_URL_LENGTH];
5083 dwBufferSize=sizeof(szNewLocation);
5084 switch(request->status_code) {
5085 case HTTP_STATUS_REDIRECT:
5086 case HTTP_STATUS_MOVED:
5087 case HTTP_STATUS_REDIRECT_KEEP_VERB:
5088 case HTTP_STATUS_REDIRECT_METHOD:
5089 if(HTTP_HttpQueryInfoW(request,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL) != ERROR_SUCCESS)
5090 break;
5092 if (strcmpW(request->verb, szGET) && strcmpW(request->verb, szHEAD) &&
5093 request->status_code != HTTP_STATUS_REDIRECT_KEEP_VERB)
5095 heap_free(request->verb);
5096 request->verb = heap_strdupW(szGET);
5098 http_release_netconn(request, drain_content(request, FALSE));
5099 if ((new_url = HTTP_GetRedirectURL( request, szNewLocation )))
5101 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REDIRECT,
5102 new_url, (strlenW(new_url) + 1) * sizeof(WCHAR));
5103 res = HTTP_HandleRedirect(request, new_url);
5104 if (res == ERROR_SUCCESS)
5106 heap_free(requestString);
5107 loop_next = TRUE;
5109 heap_free( new_url );
5111 redirected = TRUE;
5114 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTH) && res == ERROR_SUCCESS)
5116 WCHAR szAuthValue[2048];
5117 dwBufferSize=2048;
5118 if (request->status_code == HTTP_STATUS_DENIED)
5120 WCHAR *host = get_host_header( request );
5121 DWORD dwIndex = 0;
5122 while (HTTP_HttpQueryInfoW(request,HTTP_QUERY_WWW_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex) == ERROR_SUCCESS)
5124 if (HTTP_DoAuthorization(request, szAuthValue,
5125 &request->authInfo,
5126 request->session->userName,
5127 request->session->password, host))
5129 heap_free(requestString);
5130 if(!drain_content(request, TRUE)) {
5131 FIXME("Could not drain content\n");
5132 http_release_netconn(request, FALSE);
5134 loop_next = TRUE;
5135 break;
5138 heap_free( host );
5140 if(!loop_next) {
5141 TRACE("Cleaning wrong authorization data\n");
5142 destroy_authinfo(request->authInfo);
5143 request->authInfo = NULL;
5146 if (request->status_code == HTTP_STATUS_PROXY_AUTH_REQ)
5148 DWORD dwIndex = 0;
5149 while (HTTP_HttpQueryInfoW(request,HTTP_QUERY_PROXY_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex) == ERROR_SUCCESS)
5151 if (HTTP_DoAuthorization(request, szAuthValue,
5152 &request->proxyAuthInfo,
5153 request->session->appInfo->proxyUsername,
5154 request->session->appInfo->proxyPassword,
5155 NULL))
5157 heap_free(requestString);
5158 if(!drain_content(request, TRUE)) {
5159 FIXME("Could not drain content\n");
5160 http_release_netconn(request, FALSE);
5162 loop_next = TRUE;
5163 break;
5167 if(!loop_next) {
5168 TRACE("Cleaning wrong proxy authorization data\n");
5169 destroy_authinfo(request->proxyAuthInfo);
5170 request->proxyAuthInfo = NULL;
5174 if (secure_proxy_connect && request->status_code == HTTP_STATUS_OK)
5176 res = NETCON_secure_connect(request->netconn, request->server);
5177 if (res != ERROR_SUCCESS)
5179 WARN("failed to upgrade to secure proxy connection\n");
5180 http_release_netconn( request, FALSE );
5181 break;
5183 remove_header(request, szProxy_Authorization, TRUE);
5184 destroy_authinfo(request->proxyAuthInfo);
5185 request->proxyAuthInfo = NULL;
5187 secure_proxy_connect = FALSE;
5188 loop_next = TRUE;
5191 else
5192 res = ERROR_SUCCESS;
5194 while (loop_next);
5196 lend:
5197 heap_free(requestString);
5199 /* TODO: send notification for P3P header */
5201 if(res == ERROR_SUCCESS)
5202 create_cache_entry(request);
5204 if (request->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5206 if (res == ERROR_SUCCESS) {
5207 if(bEndRequest && request->contentLength && request->bytesWritten == request->bytesToWrite)
5208 HTTP_ReceiveRequestData(request, TRUE, NULL);
5209 else
5210 send_request_complete(request,
5211 request->session->hdr.dwInternalFlags & INET_OPENURL ? (DWORD_PTR)request->hdr.hInternet : 1, 0);
5212 }else {
5213 send_request_complete(request, 0, res);
5217 TRACE("<--\n");
5218 return res;
5221 typedef struct {
5222 task_header_t hdr;
5223 WCHAR *headers;
5224 DWORD headers_len;
5225 void *optional;
5226 DWORD optional_len;
5227 DWORD content_len;
5228 BOOL end_request;
5229 } send_request_task_t;
5231 /***********************************************************************
5233 * Helper functions for the HttpSendRequest(Ex) functions
5236 static void AsyncHttpSendRequestProc(task_header_t *hdr)
5238 send_request_task_t *task = (send_request_task_t*)hdr;
5239 http_request_t *request = (http_request_t*)task->hdr.hdr;
5241 TRACE("%p\n", request);
5243 HTTP_HttpSendRequestW(request, task->headers, task->headers_len, task->optional,
5244 task->optional_len, task->content_len, task->end_request);
5246 heap_free(task->headers);
5250 static DWORD HTTP_HttpEndRequestW(http_request_t *request, DWORD dwFlags, DWORD_PTR dwContext)
5252 DWORD dwBufferSize;
5253 INT responseLen;
5254 DWORD res = ERROR_SUCCESS;
5256 if(!is_valid_netconn(request->netconn)) {
5257 WARN("Not connected\n");
5258 send_request_complete(request, 0, ERROR_INTERNET_OPERATION_CANCELLED);
5259 return ERROR_INTERNET_OPERATION_CANCELLED;
5262 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5263 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
5265 if (HTTP_GetResponseHeaders(request, &responseLen) || !responseLen)
5266 res = ERROR_HTTP_HEADER_NOT_FOUND;
5268 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5269 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
5271 /* process cookies here. Is this right? */
5272 http_process_keep_alive(request);
5273 HTTP_ProcessCookies(request);
5274 HTTP_ProcessExpires(request);
5275 HTTP_ProcessLastModified(request);
5277 if ((res = set_content_length(request)) == ERROR_SUCCESS) {
5278 if(!request->contentLength)
5279 http_release_netconn(request, TRUE);
5282 if (res == ERROR_SUCCESS && !(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
5284 switch(request->status_code) {
5285 case HTTP_STATUS_REDIRECT:
5286 case HTTP_STATUS_MOVED:
5287 case HTTP_STATUS_REDIRECT_METHOD:
5288 case HTTP_STATUS_REDIRECT_KEEP_VERB: {
5289 WCHAR *new_url, szNewLocation[INTERNET_MAX_URL_LENGTH];
5290 dwBufferSize=sizeof(szNewLocation);
5291 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_LOCATION, szNewLocation, &dwBufferSize, NULL) != ERROR_SUCCESS)
5292 break;
5294 if (strcmpW(request->verb, szGET) && strcmpW(request->verb, szHEAD) &&
5295 request->status_code != HTTP_STATUS_REDIRECT_KEEP_VERB)
5297 heap_free(request->verb);
5298 request->verb = heap_strdupW(szGET);
5300 http_release_netconn(request, drain_content(request, FALSE));
5301 if ((new_url = HTTP_GetRedirectURL( request, szNewLocation )))
5303 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REDIRECT,
5304 new_url, (strlenW(new_url) + 1) * sizeof(WCHAR));
5305 res = HTTP_HandleRedirect(request, new_url);
5306 if (res == ERROR_SUCCESS)
5307 res = HTTP_HttpSendRequestW(request, NULL, 0, NULL, 0, 0, TRUE);
5308 heap_free( new_url );
5314 if(res == ERROR_SUCCESS)
5315 create_cache_entry(request);
5317 if (res == ERROR_SUCCESS && request->contentLength)
5318 HTTP_ReceiveRequestData(request, TRUE, NULL);
5319 else
5320 send_request_complete(request, res == ERROR_SUCCESS, res);
5322 return res;
5325 /***********************************************************************
5326 * HttpEndRequestA (WININET.@)
5328 * Ends an HTTP request that was started by HttpSendRequestEx
5330 * RETURNS
5331 * TRUE if successful
5332 * FALSE on failure
5335 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
5336 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
5338 TRACE("(%p, %p, %08x, %08lx)\n", hRequest, lpBuffersOut, dwFlags, dwContext);
5340 if (lpBuffersOut)
5342 SetLastError(ERROR_INVALID_PARAMETER);
5343 return FALSE;
5346 return HttpEndRequestW(hRequest, NULL, dwFlags, dwContext);
5349 typedef struct {
5350 task_header_t hdr;
5351 DWORD flags;
5352 DWORD context;
5353 } end_request_task_t;
5355 static void AsyncHttpEndRequestProc(task_header_t *hdr)
5357 end_request_task_t *task = (end_request_task_t*)hdr;
5358 http_request_t *req = (http_request_t*)task->hdr.hdr;
5360 TRACE("%p\n", req);
5362 HTTP_HttpEndRequestW(req, task->flags, task->context);
5365 /***********************************************************************
5366 * HttpEndRequestW (WININET.@)
5368 * Ends an HTTP request that was started by HttpSendRequestEx
5370 * RETURNS
5371 * TRUE if successful
5372 * FALSE on failure
5375 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
5376 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
5378 http_request_t *request;
5379 DWORD res;
5381 TRACE("%p %p %x %lx -->\n", hRequest, lpBuffersOut, dwFlags, dwContext);
5383 if (lpBuffersOut)
5385 SetLastError(ERROR_INVALID_PARAMETER);
5386 return FALSE;
5389 request = (http_request_t*) get_handle_object( hRequest );
5391 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5393 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
5394 if (request)
5395 WININET_Release( &request->hdr );
5396 return FALSE;
5398 request->hdr.dwFlags |= dwFlags;
5400 if (request->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5402 end_request_task_t *task;
5404 task = alloc_async_task(&request->hdr, AsyncHttpEndRequestProc, sizeof(*task));
5405 task->flags = dwFlags;
5406 task->context = dwContext;
5408 INTERNET_AsyncCall(&task->hdr);
5409 res = ERROR_IO_PENDING;
5411 else
5412 res = HTTP_HttpEndRequestW(request, dwFlags, dwContext);
5414 WININET_Release( &request->hdr );
5415 TRACE("%u <--\n", res);
5416 if(res != ERROR_SUCCESS)
5417 SetLastError(res);
5418 return res == ERROR_SUCCESS;
5421 /***********************************************************************
5422 * HttpSendRequestExA (WININET.@)
5424 * Sends the specified request to the HTTP server and allows chunked
5425 * transfers.
5427 * RETURNS
5428 * Success: TRUE
5429 * Failure: FALSE, call GetLastError() for more information.
5431 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
5432 LPINTERNET_BUFFERSA lpBuffersIn,
5433 LPINTERNET_BUFFERSA lpBuffersOut,
5434 DWORD dwFlags, DWORD_PTR dwContext)
5436 INTERNET_BUFFERSW BuffersInW;
5437 BOOL rc = FALSE;
5438 DWORD headerlen;
5439 LPWSTR header = NULL;
5441 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
5442 lpBuffersOut, dwFlags, dwContext);
5444 if (lpBuffersIn)
5446 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
5447 if (lpBuffersIn->lpcszHeader)
5449 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
5450 lpBuffersIn->dwHeadersLength,0,0);
5451 header = heap_alloc(headerlen*sizeof(WCHAR));
5452 if (!(BuffersInW.lpcszHeader = header))
5454 SetLastError(ERROR_OUTOFMEMORY);
5455 return FALSE;
5457 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
5458 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
5459 header, headerlen);
5461 else
5462 BuffersInW.lpcszHeader = NULL;
5463 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
5464 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
5465 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
5466 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
5467 BuffersInW.Next = NULL;
5470 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
5472 heap_free(header);
5473 return rc;
5476 /***********************************************************************
5477 * HttpSendRequestExW (WININET.@)
5479 * Sends the specified request to the HTTP server and allows chunked
5480 * transfers
5482 * RETURNS
5483 * Success: TRUE
5484 * Failure: FALSE, call GetLastError() for more information.
5486 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
5487 LPINTERNET_BUFFERSW lpBuffersIn,
5488 LPINTERNET_BUFFERSW lpBuffersOut,
5489 DWORD dwFlags, DWORD_PTR dwContext)
5491 http_request_t *request;
5492 http_session_t *session;
5493 appinfo_t *hIC;
5494 DWORD res;
5496 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
5497 lpBuffersOut, dwFlags, dwContext);
5499 request = (http_request_t*) get_handle_object( hRequest );
5501 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5503 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5504 goto lend;
5507 session = request->session;
5508 assert(session->hdr.htype == WH_HHTTPSESSION);
5509 hIC = session->appInfo;
5510 assert(hIC->hdr.htype == WH_HINIT);
5512 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5514 send_request_task_t *task;
5516 task = alloc_async_task(&request->hdr, AsyncHttpSendRequestProc, sizeof(*task));
5517 if (lpBuffersIn)
5519 DWORD size = 0;
5521 if (lpBuffersIn->lpcszHeader)
5523 if (lpBuffersIn->dwHeadersLength == ~0u)
5524 size = (strlenW( lpBuffersIn->lpcszHeader ) + 1) * sizeof(WCHAR);
5525 else
5526 size = lpBuffersIn->dwHeadersLength * sizeof(WCHAR);
5528 task->headers = heap_alloc(size);
5529 memcpy(task->headers, lpBuffersIn->lpcszHeader, size);
5531 else task->headers = NULL;
5533 task->headers_len = size / sizeof(WCHAR);
5534 task->optional = lpBuffersIn->lpvBuffer;
5535 task->optional_len = lpBuffersIn->dwBufferLength;
5536 task->content_len = lpBuffersIn->dwBufferTotal;
5538 else
5540 task->headers = NULL;
5541 task->headers_len = 0;
5542 task->optional = NULL;
5543 task->optional_len = 0;
5544 task->content_len = 0;
5547 task->end_request = FALSE;
5549 INTERNET_AsyncCall(&task->hdr);
5550 res = ERROR_IO_PENDING;
5552 else
5554 if (lpBuffersIn)
5555 res = HTTP_HttpSendRequestW(request, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
5556 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
5557 lpBuffersIn->dwBufferTotal, FALSE);
5558 else
5559 res = HTTP_HttpSendRequestW(request, NULL, 0, NULL, 0, 0, FALSE);
5562 lend:
5563 if ( request )
5564 WININET_Release( &request->hdr );
5566 TRACE("<---\n");
5567 SetLastError(res);
5568 return res == ERROR_SUCCESS;
5571 /***********************************************************************
5572 * HttpSendRequestW (WININET.@)
5574 * Sends the specified request to the HTTP server
5576 * RETURNS
5577 * TRUE on success
5578 * FALSE on failure
5581 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
5582 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
5584 http_request_t *request;
5585 http_session_t *session = NULL;
5586 appinfo_t *hIC = NULL;
5587 DWORD res = ERROR_SUCCESS;
5589 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest,
5590 debugstr_wn(lpszHeaders, dwHeaderLength), dwHeaderLength, lpOptional, dwOptionalLength);
5592 request = (http_request_t*) get_handle_object( hHttpRequest );
5593 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5595 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5596 goto lend;
5599 session = request->session;
5600 if (NULL == session || session->hdr.htype != WH_HHTTPSESSION)
5602 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5603 goto lend;
5606 hIC = session->appInfo;
5607 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
5609 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5610 goto lend;
5613 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5615 send_request_task_t *task;
5617 task = alloc_async_task(&request->hdr, AsyncHttpSendRequestProc, sizeof(*task));
5618 if (lpszHeaders)
5620 DWORD size;
5622 if (dwHeaderLength == ~0u) size = (strlenW(lpszHeaders) + 1) * sizeof(WCHAR);
5623 else size = dwHeaderLength * sizeof(WCHAR);
5625 task->headers = heap_alloc(size);
5626 memcpy(task->headers, lpszHeaders, size);
5628 else
5629 task->headers = NULL;
5630 task->headers_len = dwHeaderLength;
5631 task->optional = lpOptional;
5632 task->optional_len = dwOptionalLength;
5633 task->content_len = dwOptionalLength;
5634 task->end_request = TRUE;
5636 INTERNET_AsyncCall(&task->hdr);
5637 res = ERROR_IO_PENDING;
5639 else
5641 res = HTTP_HttpSendRequestW(request, lpszHeaders,
5642 dwHeaderLength, lpOptional, dwOptionalLength,
5643 dwOptionalLength, TRUE);
5645 lend:
5646 if( request )
5647 WININET_Release( &request->hdr );
5649 SetLastError(res);
5650 return res == ERROR_SUCCESS;
5653 /***********************************************************************
5654 * HttpSendRequestA (WININET.@)
5656 * Sends the specified request to the HTTP server
5658 * RETURNS
5659 * TRUE on success
5660 * FALSE on failure
5663 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
5664 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
5666 BOOL result;
5667 LPWSTR szHeaders=NULL;
5668 DWORD nLen=dwHeaderLength;
5669 if(lpszHeaders!=NULL)
5671 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
5672 szHeaders = heap_alloc(nLen*sizeof(WCHAR));
5673 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
5675 result = HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
5676 heap_free(szHeaders);
5677 return result;
5680 /***********************************************************************
5681 * HTTPSESSION_Destroy (internal)
5683 * Deallocate session handle
5686 static void HTTPSESSION_Destroy(object_header_t *hdr)
5688 http_session_t *session = (http_session_t*) hdr;
5690 TRACE("%p\n", session);
5692 WININET_Release(&session->appInfo->hdr);
5694 heap_free(session->hostName);
5695 heap_free(session->password);
5696 heap_free(session->userName);
5699 static DWORD HTTPSESSION_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
5701 http_session_t *ses = (http_session_t *)hdr;
5703 switch(option) {
5704 case INTERNET_OPTION_HANDLE_TYPE:
5705 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
5707 if (*size < sizeof(ULONG))
5708 return ERROR_INSUFFICIENT_BUFFER;
5710 *size = sizeof(DWORD);
5711 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_CONNECT_HTTP;
5712 return ERROR_SUCCESS;
5713 case INTERNET_OPTION_CONNECT_TIMEOUT:
5714 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
5716 if (*size < sizeof(DWORD))
5717 return ERROR_INSUFFICIENT_BUFFER;
5719 *size = sizeof(DWORD);
5720 *(DWORD *)buffer = ses->connect_timeout;
5721 return ERROR_SUCCESS;
5723 case INTERNET_OPTION_SEND_TIMEOUT:
5724 TRACE("INTERNET_OPTION_SEND_TIMEOUT\n");
5726 if (*size < sizeof(DWORD))
5727 return ERROR_INSUFFICIENT_BUFFER;
5729 *size = sizeof(DWORD);
5730 *(DWORD *)buffer = ses->send_timeout;
5731 return ERROR_SUCCESS;
5733 case INTERNET_OPTION_RECEIVE_TIMEOUT:
5734 TRACE("INTERNET_OPTION_RECEIVE_TIMEOUT\n");
5736 if (*size < sizeof(DWORD))
5737 return ERROR_INSUFFICIENT_BUFFER;
5739 *size = sizeof(DWORD);
5740 *(DWORD *)buffer = ses->receive_timeout;
5741 return ERROR_SUCCESS;
5744 return INET_QueryOption(hdr, option, buffer, size, unicode);
5747 static DWORD HTTPSESSION_SetOption(object_header_t *hdr, DWORD option, void *buffer, DWORD size)
5749 http_session_t *ses = (http_session_t*)hdr;
5751 switch(option) {
5752 case INTERNET_OPTION_USERNAME:
5754 heap_free(ses->userName);
5755 if (!(ses->userName = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5756 return ERROR_SUCCESS;
5758 case INTERNET_OPTION_PASSWORD:
5760 heap_free(ses->password);
5761 if (!(ses->password = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5762 return ERROR_SUCCESS;
5764 case INTERNET_OPTION_PROXY_USERNAME:
5766 heap_free(ses->appInfo->proxyUsername);
5767 if (!(ses->appInfo->proxyUsername = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5768 return ERROR_SUCCESS;
5770 case INTERNET_OPTION_PROXY_PASSWORD:
5772 heap_free(ses->appInfo->proxyPassword);
5773 if (!(ses->appInfo->proxyPassword = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5774 return ERROR_SUCCESS;
5776 case INTERNET_OPTION_CONNECT_TIMEOUT:
5778 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5779 ses->connect_timeout = *(DWORD *)buffer;
5780 return ERROR_SUCCESS;
5782 case INTERNET_OPTION_SEND_TIMEOUT:
5784 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5785 ses->send_timeout = *(DWORD *)buffer;
5786 return ERROR_SUCCESS;
5788 case INTERNET_OPTION_RECEIVE_TIMEOUT:
5790 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5791 ses->receive_timeout = *(DWORD *)buffer;
5792 return ERROR_SUCCESS;
5794 default: break;
5797 return INET_SetOption(hdr, option, buffer, size);
5800 static const object_vtbl_t HTTPSESSIONVtbl = {
5801 HTTPSESSION_Destroy,
5802 NULL,
5803 HTTPSESSION_QueryOption,
5804 HTTPSESSION_SetOption,
5805 NULL,
5806 NULL,
5807 NULL,
5808 NULL,
5809 NULL
5813 /***********************************************************************
5814 * HTTP_Connect (internal)
5816 * Create http session handle
5818 * RETURNS
5819 * HINTERNET a session handle on success
5820 * NULL on failure
5823 DWORD HTTP_Connect(appinfo_t *hIC, LPCWSTR lpszServerName,
5824 INTERNET_PORT serverPort, LPCWSTR lpszUserName,
5825 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
5826 DWORD dwInternalFlags, HINTERNET *ret)
5828 http_session_t *session = NULL;
5830 TRACE("-->\n");
5832 if (!lpszServerName || !lpszServerName[0])
5833 return ERROR_INVALID_PARAMETER;
5835 assert( hIC->hdr.htype == WH_HINIT );
5837 session = alloc_object(&hIC->hdr, &HTTPSESSIONVtbl, sizeof(http_session_t));
5838 if (!session)
5839 return ERROR_OUTOFMEMORY;
5842 * According to my tests. The name is not resolved until a request is sent
5845 session->hdr.htype = WH_HHTTPSESSION;
5846 session->hdr.dwFlags = dwFlags;
5847 session->hdr.dwContext = dwContext;
5848 session->hdr.dwInternalFlags |= dwInternalFlags;
5850 WININET_AddRef( &hIC->hdr );
5851 session->appInfo = hIC;
5852 list_add_head( &hIC->hdr.children, &session->hdr.entry );
5854 session->hostName = heap_strdupW(lpszServerName);
5855 if (lpszUserName && lpszUserName[0])
5856 session->userName = heap_strdupW(lpszUserName);
5857 if (lpszPassword && lpszPassword[0])
5858 session->password = heap_strdupW(lpszPassword);
5859 session->hostPort = serverPort;
5860 session->connect_timeout = hIC->connect_timeout;
5861 session->send_timeout = 0;
5862 session->receive_timeout = 0;
5864 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
5865 if (!(session->hdr.dwInternalFlags & INET_OPENURL))
5867 INTERNET_SendCallback(&hIC->hdr, dwContext,
5868 INTERNET_STATUS_HANDLE_CREATED, &session->hdr.hInternet,
5869 sizeof(HINTERNET));
5873 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
5874 * windows
5877 TRACE("%p --> %p\n", hIC, session);
5879 *ret = session->hdr.hInternet;
5880 return ERROR_SUCCESS;
5883 /***********************************************************************
5884 * HTTP_clear_response_headers (internal)
5886 * clear out any old response headers
5888 static void HTTP_clear_response_headers( http_request_t *request )
5890 DWORD i;
5892 EnterCriticalSection( &request->headers_section );
5894 for( i=0; i<request->nCustHeaders; i++)
5896 if( !request->custHeaders[i].lpszField )
5897 continue;
5898 if( !request->custHeaders[i].lpszValue )
5899 continue;
5900 if ( request->custHeaders[i].wFlags & HDR_ISREQUEST )
5901 continue;
5902 HTTP_DeleteCustomHeader( request, i );
5903 i--;
5906 LeaveCriticalSection( &request->headers_section );
5909 /***********************************************************************
5910 * HTTP_GetResponseHeaders (internal)
5912 * Read server response
5914 * RETURNS
5916 * TRUE on success
5917 * FALSE on error
5919 static DWORD HTTP_GetResponseHeaders(http_request_t *request, INT *len)
5921 INT cbreaks = 0;
5922 WCHAR buffer[MAX_REPLY_LEN];
5923 DWORD buflen = MAX_REPLY_LEN;
5924 INT rc = 0;
5925 char bufferA[MAX_REPLY_LEN];
5926 LPWSTR status_code = NULL, status_text = NULL;
5927 DWORD res = ERROR_HTTP_INVALID_SERVER_RESPONSE;
5928 BOOL codeHundred = FALSE;
5930 TRACE("-->\n");
5932 if(!is_valid_netconn(request->netconn))
5933 goto lend;
5935 /* clear old response headers (eg. from a redirect response) */
5936 HTTP_clear_response_headers( request );
5938 NETCON_set_timeout( request->netconn, FALSE, request->receive_timeout );
5939 do {
5941 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
5943 buflen = MAX_REPLY_LEN;
5944 if ((res = read_line(request, bufferA, &buflen)))
5945 goto lend;
5947 if (!buflen) goto lend;
5949 rc += buflen;
5950 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
5951 /* check is this a status code line? */
5952 if (!strncmpW(buffer, g_szHttp1_0, 4))
5954 /* split the version from the status code */
5955 status_code = strchrW( buffer, ' ' );
5956 if( !status_code )
5957 goto lend;
5958 *status_code++=0;
5960 /* split the status code from the status text */
5961 status_text = strchrW( status_code, ' ' );
5962 if( status_text )
5963 *status_text++=0;
5965 request->status_code = atoiW(status_code);
5967 TRACE("version [%s] status code [%s] status text [%s]\n",
5968 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
5970 codeHundred = request->status_code == HTTP_STATUS_CONTINUE;
5972 else if (!codeHundred)
5974 WARN("No status line at head of response (%s)\n", debugstr_w(buffer));
5976 heap_free(request->version);
5977 heap_free(request->statusText);
5979 request->status_code = HTTP_STATUS_OK;
5980 request->version = heap_strdupW(g_szHttp1_0);
5981 request->statusText = heap_strdupW(szOK);
5983 goto lend;
5985 } while (codeHundred);
5987 /* Add status code */
5988 HTTP_ProcessHeader(request, szStatus, status_code,
5989 HTTP_ADDHDR_FLAG_REPLACE);
5991 heap_free(request->version);
5992 heap_free(request->statusText);
5994 request->version = heap_strdupW(buffer);
5995 request->statusText = heap_strdupW(status_text ? status_text : emptyW);
5997 /* Restore the spaces */
5998 *(status_code-1) = ' ';
5999 if (status_text)
6000 *(status_text-1) = ' ';
6002 /* Parse each response line */
6005 buflen = MAX_REPLY_LEN;
6006 if (!read_line(request, bufferA, &buflen) && buflen)
6008 LPWSTR * pFieldAndValue;
6010 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
6012 if (!bufferA[0]) break;
6013 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
6015 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
6016 if (pFieldAndValue)
6018 HTTP_ProcessHeader(request, pFieldAndValue[0], pFieldAndValue[1],
6019 HTTP_ADDREQ_FLAG_ADD );
6020 HTTP_FreeTokens(pFieldAndValue);
6023 else
6025 cbreaks++;
6026 if (cbreaks >= 2)
6027 break;
6029 }while(1);
6031 res = ERROR_SUCCESS;
6033 lend:
6035 *len = rc;
6036 TRACE("<--\n");
6037 return res;
6040 /***********************************************************************
6041 * HTTP_InterpretHttpHeader (internal)
6043 * Parse server response
6045 * RETURNS
6047 * Pointer to array of field, value, NULL on success.
6048 * NULL on error.
6050 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
6052 LPWSTR * pTokenPair;
6053 LPWSTR pszColon;
6054 INT len;
6056 pTokenPair = heap_alloc_zero(sizeof(*pTokenPair)*3);
6058 pszColon = strchrW(buffer, ':');
6059 /* must have two tokens */
6060 if (!pszColon)
6062 HTTP_FreeTokens(pTokenPair);
6063 if (buffer[0])
6064 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
6065 return NULL;
6068 pTokenPair[0] = heap_alloc((pszColon - buffer + 1) * sizeof(WCHAR));
6069 if (!pTokenPair[0])
6071 HTTP_FreeTokens(pTokenPair);
6072 return NULL;
6074 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
6075 pTokenPair[0][pszColon - buffer] = '\0';
6077 /* skip colon */
6078 pszColon++;
6079 len = strlenW(pszColon);
6080 pTokenPair[1] = heap_alloc((len + 1) * sizeof(WCHAR));
6081 if (!pTokenPair[1])
6083 HTTP_FreeTokens(pTokenPair);
6084 return NULL;
6086 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
6088 strip_spaces(pTokenPair[0]);
6089 strip_spaces(pTokenPair[1]);
6091 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
6092 return pTokenPair;
6095 /***********************************************************************
6096 * HTTP_ProcessHeader (internal)
6098 * Stuff header into header tables according to <dwModifier>
6102 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
6104 static DWORD HTTP_ProcessHeader(http_request_t *request, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
6106 LPHTTPHEADERW lphttpHdr = NULL;
6107 INT index;
6108 BOOL request_only = !!(dwModifier & HTTP_ADDHDR_FLAG_REQ);
6109 DWORD res = ERROR_HTTP_INVALID_HEADER;
6111 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), dwModifier);
6113 EnterCriticalSection( &request->headers_section );
6115 /* REPLACE wins out over ADD */
6116 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
6117 dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
6119 if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
6120 index = -1;
6121 else
6122 index = HTTP_GetCustomHeaderIndex(request, field, 0, request_only);
6124 if (index >= 0)
6126 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
6128 LeaveCriticalSection( &request->headers_section );
6129 return ERROR_HTTP_INVALID_HEADER;
6131 lphttpHdr = &request->custHeaders[index];
6133 else if (value)
6135 HTTPHEADERW hdr;
6137 hdr.lpszField = (LPWSTR)field;
6138 hdr.lpszValue = (LPWSTR)value;
6139 hdr.wFlags = hdr.wCount = 0;
6141 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6142 hdr.wFlags |= HDR_ISREQUEST;
6144 res = HTTP_InsertCustomHeader(request, &hdr);
6145 LeaveCriticalSection( &request->headers_section );
6146 return res;
6148 /* no value to delete */
6149 else
6151 LeaveCriticalSection( &request->headers_section );
6152 return ERROR_SUCCESS;
6155 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6156 lphttpHdr->wFlags |= HDR_ISREQUEST;
6157 else
6158 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
6160 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
6162 HTTP_DeleteCustomHeader( request, index );
6164 if (value && value[0])
6166 HTTPHEADERW hdr;
6168 hdr.lpszField = (LPWSTR)field;
6169 hdr.lpszValue = (LPWSTR)value;
6170 hdr.wFlags = hdr.wCount = 0;
6172 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6173 hdr.wFlags |= HDR_ISREQUEST;
6175 res = HTTP_InsertCustomHeader(request, &hdr);
6176 LeaveCriticalSection( &request->headers_section );
6177 return res;
6180 LeaveCriticalSection( &request->headers_section );
6181 return ERROR_SUCCESS;
6183 else if (dwModifier & COALESCEFLAGS)
6185 LPWSTR lpsztmp;
6186 WCHAR ch = 0;
6187 INT len = 0;
6188 INT origlen = strlenW(lphttpHdr->lpszValue);
6189 INT valuelen = strlenW(value);
6191 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
6193 ch = ',';
6194 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
6196 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
6198 ch = ';';
6199 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
6202 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
6204 lpsztmp = heap_realloc(lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
6205 if (lpsztmp)
6207 lphttpHdr->lpszValue = lpsztmp;
6208 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
6209 if (ch > 0)
6211 lphttpHdr->lpszValue[origlen] = ch;
6212 origlen++;
6213 lphttpHdr->lpszValue[origlen] = ' ';
6214 origlen++;
6217 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
6218 lphttpHdr->lpszValue[len] = '\0';
6219 res = ERROR_SUCCESS;
6221 else
6223 WARN("heap_realloc (%d bytes) failed\n",len+1);
6224 res = ERROR_OUTOFMEMORY;
6227 TRACE("<-- %d\n", res);
6228 LeaveCriticalSection( &request->headers_section );
6229 return res;
6232 /***********************************************************************
6233 * HTTP_GetCustomHeaderIndex (internal)
6235 * Return index of custom header from header array
6236 * Headers section must be held
6238 static INT HTTP_GetCustomHeaderIndex(http_request_t *request, LPCWSTR lpszField,
6239 int requested_index, BOOL request_only)
6241 DWORD index;
6243 TRACE("%s, %d, %d\n", debugstr_w(lpszField), requested_index, request_only);
6245 for (index = 0; index < request->nCustHeaders; index++)
6247 if (strcmpiW(request->custHeaders[index].lpszField, lpszField))
6248 continue;
6250 if (request_only && !(request->custHeaders[index].wFlags & HDR_ISREQUEST))
6251 continue;
6253 if (!request_only && (request->custHeaders[index].wFlags & HDR_ISREQUEST))
6254 continue;
6256 if (requested_index == 0)
6257 break;
6258 requested_index --;
6261 if (index >= request->nCustHeaders)
6262 index = -1;
6264 TRACE("Return: %d\n", index);
6265 return index;
6269 /***********************************************************************
6270 * HTTP_InsertCustomHeader (internal)
6272 * Insert header into array
6273 * Headers section must be held
6275 static DWORD HTTP_InsertCustomHeader(http_request_t *request, LPHTTPHEADERW lpHdr)
6277 INT count;
6278 LPHTTPHEADERW lph = NULL;
6280 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
6281 count = request->nCustHeaders + 1;
6282 if (count > 1)
6283 lph = heap_realloc_zero(request->custHeaders, sizeof(HTTPHEADERW) * count);
6284 else
6285 lph = heap_alloc_zero(sizeof(HTTPHEADERW) * count);
6287 if (!lph)
6288 return ERROR_OUTOFMEMORY;
6290 request->custHeaders = lph;
6291 request->custHeaders[count-1].lpszField = heap_strdupW(lpHdr->lpszField);
6292 request->custHeaders[count-1].lpszValue = heap_strdupW(lpHdr->lpszValue);
6293 request->custHeaders[count-1].wFlags = lpHdr->wFlags;
6294 request->custHeaders[count-1].wCount= lpHdr->wCount;
6295 request->nCustHeaders++;
6297 return ERROR_SUCCESS;
6301 /***********************************************************************
6302 * HTTP_DeleteCustomHeader (internal)
6304 * Delete header from array
6305 * If this function is called, the index may change.
6306 * Headers section must be held
6308 static BOOL HTTP_DeleteCustomHeader(http_request_t *request, DWORD index)
6310 if( request->nCustHeaders <= 0 )
6311 return FALSE;
6312 if( index >= request->nCustHeaders )
6313 return FALSE;
6314 request->nCustHeaders--;
6316 heap_free(request->custHeaders[index].lpszField);
6317 heap_free(request->custHeaders[index].lpszValue);
6319 memmove( &request->custHeaders[index], &request->custHeaders[index+1],
6320 (request->nCustHeaders - index)* sizeof(HTTPHEADERW) );
6321 memset( &request->custHeaders[request->nCustHeaders], 0, sizeof(HTTPHEADERW) );
6323 return TRUE;
6327 /***********************************************************************
6328 * IsHostInProxyBypassList (@)
6330 * Undocumented
6333 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
6335 FIXME("STUB: flags=%d host=%s length=%d\n",flags,szHost,length);
6336 return FALSE;