wininet: Don't use blocking_mode_t in netconn.c.
[wine.git] / dlls / wininet / http.c
blob3be88b7403d508341bd248c1169b0dc50c2df543
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 typedef enum {
384 BLOCKING_ALLOW,
385 BLOCKING_DISALLOW,
386 BLOCKING_WAITALL
387 } blocking_mode_t;
389 struct data_stream_vtbl_t {
390 DWORD (*get_avail_data)(data_stream_t*,http_request_t*);
391 BOOL (*end_of_data)(data_stream_t*,http_request_t*);
392 DWORD (*read)(data_stream_t*,http_request_t*,BYTE*,DWORD,DWORD*,blocking_mode_t);
393 BOOL (*drain_content)(data_stream_t*,http_request_t*);
394 void (*destroy)(data_stream_t*);
397 typedef struct {
398 data_stream_t data_stream;
400 BYTE buf[READ_BUFFER_SIZE];
401 DWORD buf_size;
402 DWORD buf_pos;
403 DWORD chunk_size;
404 BOOL end_of_data;
405 } chunked_stream_t;
407 static inline void destroy_data_stream(data_stream_t *stream)
409 stream->vtbl->destroy(stream);
412 static void reset_data_stream(http_request_t *req)
414 destroy_data_stream(req->data_stream);
415 req->data_stream = &req->netconn_stream.data_stream;
416 req->read_pos = req->read_size = req->netconn_stream.content_read = 0;
417 req->read_gzip = FALSE;
420 static void remove_header( http_request_t *request, const WCHAR *str, BOOL from_request )
422 int index;
423 EnterCriticalSection( &request->headers_section );
424 index = HTTP_GetCustomHeaderIndex( request, str, 0, from_request );
425 if (index != -1) HTTP_DeleteCustomHeader( request, index );
426 LeaveCriticalSection( &request->headers_section );
429 #ifdef HAVE_ZLIB
431 typedef struct {
432 data_stream_t stream;
433 data_stream_t *parent_stream;
434 z_stream zstream;
435 BYTE buf[READ_BUFFER_SIZE];
436 DWORD buf_size;
437 DWORD buf_pos;
438 BOOL end_of_data;
439 } gzip_stream_t;
441 static DWORD gzip_get_avail_data(data_stream_t *stream, http_request_t *req)
443 /* Allow reading only from read buffer */
444 return 0;
447 static BOOL gzip_end_of_data(data_stream_t *stream, http_request_t *req)
449 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
450 return gzip_stream->end_of_data
451 || (!gzip_stream->buf_size && gzip_stream->parent_stream->vtbl->end_of_data(gzip_stream->parent_stream, req));
454 static DWORD gzip_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
455 DWORD *read, blocking_mode_t blocking_mode)
457 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
458 z_stream *zstream = &gzip_stream->zstream;
459 DWORD current_read, ret_read = 0;
460 int zres;
461 DWORD res = ERROR_SUCCESS;
463 TRACE("(%d %d)\n", size, blocking_mode);
465 while(size && !gzip_stream->end_of_data) {
466 if(!gzip_stream->buf_size) {
467 if(gzip_stream->buf_pos) {
468 if(gzip_stream->buf_size)
469 memmove(gzip_stream->buf, gzip_stream->buf+gzip_stream->buf_pos, gzip_stream->buf_size);
470 gzip_stream->buf_pos = 0;
472 res = gzip_stream->parent_stream->vtbl->read(gzip_stream->parent_stream, req, gzip_stream->buf+gzip_stream->buf_size,
473 sizeof(gzip_stream->buf)-gzip_stream->buf_size, &current_read, blocking_mode);
474 gzip_stream->buf_size += current_read;
475 if(res != ERROR_SUCCESS)
476 break;
478 if(!current_read) {
479 if(blocking_mode != BLOCKING_DISALLOW) {
480 WARN("unexpected end of data\n");
481 gzip_stream->end_of_data = TRUE;
483 break;
487 zstream->next_in = gzip_stream->buf+gzip_stream->buf_pos;
488 zstream->avail_in = gzip_stream->buf_size;
489 zstream->next_out = buf+ret_read;
490 zstream->avail_out = size;
491 zres = inflate(&gzip_stream->zstream, 0);
492 current_read = size - zstream->avail_out;
493 size -= current_read;
494 ret_read += current_read;
495 gzip_stream->buf_size -= zstream->next_in - (gzip_stream->buf+gzip_stream->buf_pos);
496 gzip_stream->buf_pos = zstream->next_in-gzip_stream->buf;
497 if(zres == Z_STREAM_END) {
498 TRACE("end of data\n");
499 gzip_stream->end_of_data = TRUE;
500 inflateEnd(zstream);
501 }else if(zres != Z_OK) {
502 WARN("inflate failed %d: %s\n", zres, debugstr_a(zstream->msg));
503 if(!ret_read)
504 res = ERROR_INTERNET_DECODING_FAILED;
505 break;
508 if(ret_read && blocking_mode == BLOCKING_ALLOW)
509 blocking_mode = BLOCKING_DISALLOW;
512 TRACE("read %u bytes\n", ret_read);
513 *read = ret_read;
514 return res;
517 static BOOL gzip_drain_content(data_stream_t *stream, http_request_t *req)
519 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
520 return gzip_stream->parent_stream->vtbl->drain_content(gzip_stream->parent_stream, req);
523 static void gzip_destroy(data_stream_t *stream)
525 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
527 destroy_data_stream(gzip_stream->parent_stream);
529 if(!gzip_stream->end_of_data)
530 inflateEnd(&gzip_stream->zstream);
531 heap_free(gzip_stream);
534 static const data_stream_vtbl_t gzip_stream_vtbl = {
535 gzip_get_avail_data,
536 gzip_end_of_data,
537 gzip_read,
538 gzip_drain_content,
539 gzip_destroy
542 static voidpf wininet_zalloc(voidpf opaque, uInt items, uInt size)
544 return heap_alloc(items*size);
547 static void wininet_zfree(voidpf opaque, voidpf address)
549 heap_free(address);
552 static DWORD init_gzip_stream(http_request_t *req, BOOL is_gzip)
554 gzip_stream_t *gzip_stream;
555 int zres;
557 gzip_stream = heap_alloc_zero(sizeof(gzip_stream_t));
558 if(!gzip_stream)
559 return ERROR_OUTOFMEMORY;
561 gzip_stream->stream.vtbl = &gzip_stream_vtbl;
562 gzip_stream->zstream.zalloc = wininet_zalloc;
563 gzip_stream->zstream.zfree = wininet_zfree;
565 zres = inflateInit2(&gzip_stream->zstream, is_gzip ? 0x1f : -15);
566 if(zres != Z_OK) {
567 ERR("inflateInit failed: %d\n", zres);
568 heap_free(gzip_stream);
569 return ERROR_OUTOFMEMORY;
572 remove_header(req, szContent_Length, FALSE);
574 if(req->read_size) {
575 memcpy(gzip_stream->buf, req->read_buf+req->read_pos, req->read_size);
576 gzip_stream->buf_size = req->read_size;
577 req->read_pos = req->read_size = 0;
580 req->read_gzip = TRUE;
581 gzip_stream->parent_stream = req->data_stream;
582 req->data_stream = &gzip_stream->stream;
583 return ERROR_SUCCESS;
586 #else
588 static DWORD init_gzip_stream(http_request_t *req, BOOL is_gzip)
590 ERR("gzip stream not supported, missing zlib.\n");
591 return ERROR_SUCCESS;
594 #endif
596 /***********************************************************************
597 * HTTP_FreeTokens (internal)
599 * Frees table of pointers.
601 static void HTTP_FreeTokens(LPWSTR * token_array)
603 int i;
604 for (i = 0; token_array[i]; i++) heap_free(token_array[i]);
605 heap_free(token_array);
608 static void HTTP_FixURL(http_request_t *request)
610 static const WCHAR szSlash[] = { '/',0 };
611 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
613 /* If we don't have a path we set it to root */
614 if (NULL == request->path)
615 request->path = heap_strdupW(szSlash);
616 else /* remove \r and \n*/
618 int nLen = strlenW(request->path);
619 while ((nLen >0 ) && ((request->path[nLen-1] == '\r')||(request->path[nLen-1] == '\n')))
621 nLen--;
622 request->path[nLen]='\0';
624 /* Replace '\' with '/' */
625 while (nLen>0) {
626 nLen--;
627 if (request->path[nLen] == '\\') request->path[nLen]='/';
631 if(CSTR_EQUAL != CompareStringW( LOCALE_INVARIANT, NORM_IGNORECASE,
632 request->path, strlenW(request->path), szHttp, strlenW(szHttp) )
633 && request->path[0] != '/') /* not an absolute path ?? --> fix it !! */
635 WCHAR *fixurl = heap_alloc((strlenW(request->path) + 2)*sizeof(WCHAR));
636 *fixurl = '/';
637 strcpyW(fixurl + 1, request->path);
638 heap_free( request->path );
639 request->path = fixurl;
643 static WCHAR* build_request_header(http_request_t *request, const WCHAR *verb,
644 const WCHAR *path, const WCHAR *version, BOOL use_cr)
646 static const WCHAR szSpace[] = {' ',0};
647 static const WCHAR szColon[] = {':',' ',0};
648 static const WCHAR szCr[] = {'\r',0};
649 static const WCHAR szLf[] = {'\n',0};
650 LPWSTR requestString;
651 DWORD len, n;
652 LPCWSTR *req;
653 UINT i;
655 EnterCriticalSection( &request->headers_section );
657 /* allocate space for an array of all the string pointers to be added */
658 len = request->nCustHeaders * 5 + 10;
659 if (!(req = heap_alloc( len * sizeof(const WCHAR *) )))
661 LeaveCriticalSection( &request->headers_section );
662 return NULL;
665 /* add the verb, path and HTTP version string */
666 n = 0;
667 req[n++] = verb;
668 req[n++] = szSpace;
669 req[n++] = path;
670 req[n++] = szSpace;
671 req[n++] = version;
672 if (use_cr)
673 req[n++] = szCr;
674 req[n++] = szLf;
676 /* Append custom request headers */
677 for (i = 0; i < request->nCustHeaders; i++)
679 if (request->custHeaders[i].wFlags & HDR_ISREQUEST)
681 req[n++] = request->custHeaders[i].lpszField;
682 req[n++] = szColon;
683 req[n++] = request->custHeaders[i].lpszValue;
684 if (use_cr)
685 req[n++] = szCr;
686 req[n++] = szLf;
688 TRACE("Adding custom header %s (%s)\n",
689 debugstr_w(request->custHeaders[i].lpszField),
690 debugstr_w(request->custHeaders[i].lpszValue));
693 if (use_cr)
694 req[n++] = szCr;
695 req[n++] = szLf;
696 req[n] = NULL;
698 requestString = HTTP_build_req( req, 4 );
699 heap_free( req );
700 LeaveCriticalSection( &request->headers_section );
701 return requestString;
704 static WCHAR* build_response_header(http_request_t *request, BOOL use_cr)
706 static const WCHAR colonW[] = { ':',' ',0 };
707 static const WCHAR crW[] = { '\r',0 };
708 static const WCHAR lfW[] = { '\n',0 };
709 static const WCHAR status_fmt[] = { ' ','%','u',' ',0 };
710 const WCHAR **req;
711 WCHAR *ret, buf[14];
712 DWORD i, n = 0;
714 EnterCriticalSection( &request->headers_section );
716 if (!(req = heap_alloc( (request->nCustHeaders * 5 + 8) * sizeof(WCHAR *) )))
718 LeaveCriticalSection( &request->headers_section );
719 return NULL;
722 if (request->status_code)
724 req[n++] = request->version;
725 sprintfW(buf, status_fmt, request->status_code);
726 req[n++] = buf;
727 req[n++] = request->statusText;
728 if (use_cr)
729 req[n++] = crW;
730 req[n++] = lfW;
733 for(i = 0; i < request->nCustHeaders; i++)
735 if(!(request->custHeaders[i].wFlags & HDR_ISREQUEST)
736 && strcmpW(request->custHeaders[i].lpszField, szStatus))
738 req[n++] = request->custHeaders[i].lpszField;
739 req[n++] = colonW;
740 req[n++] = request->custHeaders[i].lpszValue;
741 if(use_cr)
742 req[n++] = crW;
743 req[n++] = lfW;
745 TRACE("Adding custom header %s (%s)\n",
746 debugstr_w(request->custHeaders[i].lpszField),
747 debugstr_w(request->custHeaders[i].lpszValue));
750 if(use_cr)
751 req[n++] = crW;
752 req[n++] = lfW;
753 req[n] = NULL;
755 ret = HTTP_build_req(req, 0);
756 heap_free(req);
757 LeaveCriticalSection( &request->headers_section );
758 return ret;
761 static void HTTP_ProcessCookies( http_request_t *request )
763 int HeaderIndex;
764 int numCookies = 0;
765 LPHTTPHEADERW setCookieHeader;
767 if(request->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES)
768 return;
770 EnterCriticalSection( &request->headers_section );
772 while((HeaderIndex = HTTP_GetCustomHeaderIndex(request, szSet_Cookie, numCookies++, FALSE)) != -1)
774 HTTPHEADERW *host;
775 const WCHAR *data;
776 WCHAR *name;
778 setCookieHeader = &request->custHeaders[HeaderIndex];
780 if (!setCookieHeader->lpszValue)
781 continue;
783 host = HTTP_GetHeader(request, hostW);
784 if(!host)
785 continue;
787 data = strchrW(setCookieHeader->lpszValue, '=');
788 if(!data)
789 continue;
791 name = heap_strndupW(setCookieHeader->lpszValue, data-setCookieHeader->lpszValue);
792 if(!name)
793 continue;
795 data++;
796 set_cookie(host->lpszValue, request->path, name, data, INTERNET_COOKIE_HTTPONLY);
797 heap_free(name);
800 LeaveCriticalSection( &request->headers_section );
803 static void strip_spaces(LPWSTR start)
805 LPWSTR str = start;
806 LPWSTR end;
808 while (*str == ' ')
809 str++;
811 if (str != start)
812 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
814 end = start + strlenW(start) - 1;
815 while (end >= start && *end == ' ')
817 *end = '\0';
818 end--;
822 static inline BOOL is_basic_auth_value( LPCWSTR pszAuthValue, LPWSTR *pszRealm )
824 static const WCHAR szBasic[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
825 static const WCHAR szRealm[] = {'r','e','a','l','m'}; /* Note: not nul-terminated */
826 BOOL is_basic;
827 is_basic = !strncmpiW(pszAuthValue, szBasic, ARRAYSIZE(szBasic)) &&
828 ((pszAuthValue[ARRAYSIZE(szBasic)] == ' ') || !pszAuthValue[ARRAYSIZE(szBasic)]);
829 if (is_basic && pszRealm)
831 LPCWSTR token;
832 LPCWSTR ptr = &pszAuthValue[ARRAYSIZE(szBasic)];
833 LPCWSTR realm;
834 ptr++;
835 *pszRealm=NULL;
836 token = strchrW(ptr,'=');
837 if (!token)
838 return TRUE;
839 realm = ptr;
840 while (*realm == ' ')
841 realm++;
842 if(!strncmpiW(realm, szRealm, ARRAYSIZE(szRealm)) &&
843 (realm[ARRAYSIZE(szRealm)] == ' ' || realm[ARRAYSIZE(szRealm)] == '='))
845 token++;
846 while (*token == ' ')
847 token++;
848 if (*token == '\0')
849 return TRUE;
850 *pszRealm = heap_strdupW(token);
851 strip_spaces(*pszRealm);
855 return is_basic;
858 static void destroy_authinfo( struct HttpAuthInfo *authinfo )
860 if (!authinfo) return;
862 if (SecIsValidHandle(&authinfo->ctx))
863 DeleteSecurityContext(&authinfo->ctx);
864 if (SecIsValidHandle(&authinfo->cred))
865 FreeCredentialsHandle(&authinfo->cred);
867 heap_free(authinfo->auth_data);
868 heap_free(authinfo->scheme);
869 heap_free(authinfo);
872 static UINT retrieve_cached_basic_authorization(const WCHAR *host, const WCHAR *realm, char **auth_data)
874 basicAuthorizationData *ad;
875 UINT rc = 0;
877 TRACE("Looking for authorization for %s:%s\n",debugstr_w(host),debugstr_w(realm));
879 EnterCriticalSection(&authcache_cs);
880 LIST_FOR_EACH_ENTRY(ad, &basicAuthorizationCache, basicAuthorizationData, entry)
882 if (!strcmpiW(host, ad->host) && (!realm || !strcmpW(realm, ad->realm)))
884 TRACE("Authorization found in cache\n");
885 *auth_data = heap_alloc(ad->authorizationLen);
886 memcpy(*auth_data,ad->authorization,ad->authorizationLen);
887 rc = ad->authorizationLen;
888 break;
891 LeaveCriticalSection(&authcache_cs);
892 return rc;
895 static void cache_basic_authorization(LPWSTR host, LPWSTR realm, LPSTR auth_data, UINT auth_data_len)
897 struct list *cursor;
898 basicAuthorizationData* ad = NULL;
900 TRACE("caching authorization for %s:%s = %s\n",debugstr_w(host),debugstr_w(realm),debugstr_an(auth_data,auth_data_len));
902 EnterCriticalSection(&authcache_cs);
903 LIST_FOR_EACH(cursor, &basicAuthorizationCache)
905 basicAuthorizationData *check = LIST_ENTRY(cursor,basicAuthorizationData,entry);
906 if (!strcmpiW(host,check->host) && !strcmpW(realm,check->realm))
908 ad = check;
909 break;
913 if (ad)
915 TRACE("Found match in cache, replacing\n");
916 heap_free(ad->authorization);
917 ad->authorization = heap_alloc(auth_data_len);
918 memcpy(ad->authorization, auth_data, auth_data_len);
919 ad->authorizationLen = auth_data_len;
921 else
923 ad = heap_alloc(sizeof(basicAuthorizationData));
924 ad->host = heap_strdupW(host);
925 ad->realm = heap_strdupW(realm);
926 ad->authorization = heap_alloc(auth_data_len);
927 memcpy(ad->authorization, auth_data, auth_data_len);
928 ad->authorizationLen = auth_data_len;
929 list_add_head(&basicAuthorizationCache,&ad->entry);
930 TRACE("authorization cached\n");
932 LeaveCriticalSection(&authcache_cs);
935 static BOOL retrieve_cached_authorization(LPWSTR host, LPWSTR scheme,
936 SEC_WINNT_AUTH_IDENTITY_W *nt_auth_identity)
938 authorizationData *ad;
940 TRACE("Looking for authorization for %s:%s\n", debugstr_w(host), debugstr_w(scheme));
942 EnterCriticalSection(&authcache_cs);
943 LIST_FOR_EACH_ENTRY(ad, &authorizationCache, authorizationData, entry) {
944 if(!strcmpiW(host, ad->host) && !strcmpiW(scheme, ad->scheme)) {
945 TRACE("Authorization found in cache\n");
947 nt_auth_identity->User = heap_strdupW(ad->user);
948 nt_auth_identity->Password = heap_strdupW(ad->password);
949 nt_auth_identity->Domain = heap_alloc(sizeof(WCHAR)*ad->domain_len);
950 if(!nt_auth_identity->User || !nt_auth_identity->Password ||
951 (!nt_auth_identity->Domain && ad->domain_len)) {
952 heap_free(nt_auth_identity->User);
953 heap_free(nt_auth_identity->Password);
954 heap_free(nt_auth_identity->Domain);
955 break;
958 nt_auth_identity->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
959 nt_auth_identity->UserLength = ad->user_len;
960 nt_auth_identity->PasswordLength = ad->password_len;
961 memcpy(nt_auth_identity->Domain, ad->domain, sizeof(WCHAR)*ad->domain_len);
962 nt_auth_identity->DomainLength = ad->domain_len;
963 LeaveCriticalSection(&authcache_cs);
964 return TRUE;
967 LeaveCriticalSection(&authcache_cs);
969 return FALSE;
972 static void cache_authorization(LPWSTR host, LPWSTR scheme,
973 SEC_WINNT_AUTH_IDENTITY_W *nt_auth_identity)
975 authorizationData *ad;
976 BOOL found = FALSE;
978 TRACE("Caching authorization for %s:%s\n", debugstr_w(host), debugstr_w(scheme));
980 EnterCriticalSection(&authcache_cs);
981 LIST_FOR_EACH_ENTRY(ad, &authorizationCache, authorizationData, entry)
982 if(!strcmpiW(host, ad->host) && !strcmpiW(scheme, ad->scheme)) {
983 found = TRUE;
984 break;
987 if(found) {
988 heap_free(ad->user);
989 heap_free(ad->password);
990 heap_free(ad->domain);
991 } else {
992 ad = heap_alloc(sizeof(authorizationData));
993 if(!ad) {
994 LeaveCriticalSection(&authcache_cs);
995 return;
998 ad->host = heap_strdupW(host);
999 ad->scheme = heap_strdupW(scheme);
1000 list_add_head(&authorizationCache, &ad->entry);
1003 ad->user = heap_strndupW(nt_auth_identity->User, nt_auth_identity->UserLength);
1004 ad->password = heap_strndupW(nt_auth_identity->Password, nt_auth_identity->PasswordLength);
1005 ad->domain = heap_strndupW(nt_auth_identity->Domain, nt_auth_identity->DomainLength);
1006 ad->user_len = nt_auth_identity->UserLength;
1007 ad->password_len = nt_auth_identity->PasswordLength;
1008 ad->domain_len = nt_auth_identity->DomainLength;
1010 if(!ad->host || !ad->scheme || !ad->user || !ad->password
1011 || (nt_auth_identity->Domain && !ad->domain)) {
1012 heap_free(ad->host);
1013 heap_free(ad->scheme);
1014 heap_free(ad->user);
1015 heap_free(ad->password);
1016 heap_free(ad->domain);
1017 list_remove(&ad->entry);
1018 heap_free(ad);
1021 LeaveCriticalSection(&authcache_cs);
1024 static BOOL HTTP_DoAuthorization( http_request_t *request, LPCWSTR pszAuthValue,
1025 struct HttpAuthInfo **ppAuthInfo,
1026 LPWSTR domain_and_username, LPWSTR password,
1027 LPWSTR host )
1029 SECURITY_STATUS sec_status;
1030 struct HttpAuthInfo *pAuthInfo = *ppAuthInfo;
1031 BOOL first = FALSE;
1032 LPWSTR szRealm = NULL;
1034 TRACE("%s\n", debugstr_w(pszAuthValue));
1036 if (!pAuthInfo)
1038 TimeStamp exp;
1040 first = TRUE;
1041 pAuthInfo = heap_alloc(sizeof(*pAuthInfo));
1042 if (!pAuthInfo)
1043 return FALSE;
1045 SecInvalidateHandle(&pAuthInfo->cred);
1046 SecInvalidateHandle(&pAuthInfo->ctx);
1047 memset(&pAuthInfo->exp, 0, sizeof(pAuthInfo->exp));
1048 pAuthInfo->attr = 0;
1049 pAuthInfo->auth_data = NULL;
1050 pAuthInfo->auth_data_len = 0;
1051 pAuthInfo->finished = FALSE;
1053 if (is_basic_auth_value(pszAuthValue,NULL))
1055 static const WCHAR szBasic[] = {'B','a','s','i','c',0};
1056 pAuthInfo->scheme = heap_strdupW(szBasic);
1057 if (!pAuthInfo->scheme)
1059 heap_free(pAuthInfo);
1060 return FALSE;
1063 else
1065 PVOID pAuthData;
1066 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity;
1068 pAuthInfo->scheme = heap_strdupW(pszAuthValue);
1069 if (!pAuthInfo->scheme)
1071 heap_free(pAuthInfo);
1072 return FALSE;
1075 if (domain_and_username)
1077 WCHAR *user = strchrW(domain_and_username, '\\');
1078 WCHAR *domain = domain_and_username;
1080 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
1082 pAuthData = &nt_auth_identity;
1084 if (user) user++;
1085 else
1087 user = domain_and_username;
1088 domain = NULL;
1091 nt_auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
1092 nt_auth_identity.User = user;
1093 nt_auth_identity.UserLength = strlenW(nt_auth_identity.User);
1094 nt_auth_identity.Domain = domain;
1095 nt_auth_identity.DomainLength = domain ? user - domain - 1 : 0;
1096 nt_auth_identity.Password = password;
1097 nt_auth_identity.PasswordLength = strlenW(nt_auth_identity.Password);
1099 cache_authorization(host, pAuthInfo->scheme, &nt_auth_identity);
1101 else if(retrieve_cached_authorization(host, pAuthInfo->scheme, &nt_auth_identity))
1102 pAuthData = &nt_auth_identity;
1103 else
1104 /* use default credentials */
1105 pAuthData = NULL;
1107 sec_status = AcquireCredentialsHandleW(NULL, pAuthInfo->scheme,
1108 SECPKG_CRED_OUTBOUND, NULL,
1109 pAuthData, NULL,
1110 NULL, &pAuthInfo->cred,
1111 &exp);
1113 if(pAuthData && !domain_and_username) {
1114 heap_free(nt_auth_identity.User);
1115 heap_free(nt_auth_identity.Domain);
1116 heap_free(nt_auth_identity.Password);
1119 if (sec_status == SEC_E_OK)
1121 PSecPkgInfoW sec_pkg_info;
1122 sec_status = QuerySecurityPackageInfoW(pAuthInfo->scheme, &sec_pkg_info);
1123 if (sec_status == SEC_E_OK)
1125 pAuthInfo->max_token = sec_pkg_info->cbMaxToken;
1126 FreeContextBuffer(sec_pkg_info);
1129 if (sec_status != SEC_E_OK)
1131 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
1132 debugstr_w(pAuthInfo->scheme), sec_status);
1133 heap_free(pAuthInfo->scheme);
1134 heap_free(pAuthInfo);
1135 return FALSE;
1138 *ppAuthInfo = pAuthInfo;
1140 else if (pAuthInfo->finished)
1141 return FALSE;
1143 if ((strlenW(pszAuthValue) < strlenW(pAuthInfo->scheme)) ||
1144 strncmpiW(pszAuthValue, pAuthInfo->scheme, strlenW(pAuthInfo->scheme)))
1146 ERR("authentication scheme changed from %s to %s\n",
1147 debugstr_w(pAuthInfo->scheme), debugstr_w(pszAuthValue));
1148 return FALSE;
1151 if (is_basic_auth_value(pszAuthValue,&szRealm))
1153 int userlen;
1154 int passlen;
1155 char *auth_data = NULL;
1156 UINT auth_data_len = 0;
1158 TRACE("basic authentication realm %s\n",debugstr_w(szRealm));
1160 if (!domain_and_username)
1162 if (host && szRealm)
1163 auth_data_len = retrieve_cached_basic_authorization(host, szRealm,&auth_data);
1164 if (auth_data_len == 0)
1166 heap_free(szRealm);
1167 return FALSE;
1170 else
1172 userlen = WideCharToMultiByte(CP_UTF8, 0, domain_and_username, lstrlenW(domain_and_username), NULL, 0, NULL, NULL);
1173 passlen = WideCharToMultiByte(CP_UTF8, 0, password, lstrlenW(password), NULL, 0, NULL, NULL);
1175 /* length includes a nul terminator, which will be re-used for the ':' */
1176 auth_data = heap_alloc(userlen + 1 + passlen);
1177 if (!auth_data)
1179 heap_free(szRealm);
1180 return FALSE;
1183 WideCharToMultiByte(CP_UTF8, 0, domain_and_username, -1, auth_data, userlen, NULL, NULL);
1184 auth_data[userlen] = ':';
1185 WideCharToMultiByte(CP_UTF8, 0, password, -1, &auth_data[userlen+1], passlen, NULL, NULL);
1186 auth_data_len = userlen + 1 + passlen;
1187 if (host && szRealm)
1188 cache_basic_authorization(host, szRealm, auth_data, auth_data_len);
1191 pAuthInfo->auth_data = auth_data;
1192 pAuthInfo->auth_data_len = auth_data_len;
1193 pAuthInfo->finished = TRUE;
1194 heap_free(szRealm);
1195 return TRUE;
1197 else
1199 LPCWSTR pszAuthData;
1200 SecBufferDesc out_desc, in_desc;
1201 SecBuffer out, in;
1202 unsigned char *buffer;
1203 ULONG context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE |
1204 ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE;
1206 in.BufferType = SECBUFFER_TOKEN;
1207 in.cbBuffer = 0;
1208 in.pvBuffer = NULL;
1210 in_desc.ulVersion = 0;
1211 in_desc.cBuffers = 1;
1212 in_desc.pBuffers = &in;
1214 pszAuthData = pszAuthValue + strlenW(pAuthInfo->scheme);
1215 if (*pszAuthData == ' ')
1217 pszAuthData++;
1218 in.cbBuffer = HTTP_DecodeBase64(pszAuthData, NULL);
1219 in.pvBuffer = heap_alloc(in.cbBuffer);
1220 HTTP_DecodeBase64(pszAuthData, in.pvBuffer);
1223 buffer = heap_alloc(pAuthInfo->max_token);
1225 out.BufferType = SECBUFFER_TOKEN;
1226 out.cbBuffer = pAuthInfo->max_token;
1227 out.pvBuffer = buffer;
1229 out_desc.ulVersion = 0;
1230 out_desc.cBuffers = 1;
1231 out_desc.pBuffers = &out;
1233 sec_status = InitializeSecurityContextW(first ? &pAuthInfo->cred : NULL,
1234 first ? NULL : &pAuthInfo->ctx,
1235 first ? request->server->name : NULL,
1236 context_req, 0, SECURITY_NETWORK_DREP,
1237 in.pvBuffer ? &in_desc : NULL,
1238 0, &pAuthInfo->ctx, &out_desc,
1239 &pAuthInfo->attr, &pAuthInfo->exp);
1240 if (sec_status == SEC_E_OK)
1242 pAuthInfo->finished = TRUE;
1243 pAuthInfo->auth_data = out.pvBuffer;
1244 pAuthInfo->auth_data_len = out.cbBuffer;
1245 TRACE("sending last auth packet\n");
1247 else if (sec_status == SEC_I_CONTINUE_NEEDED)
1249 pAuthInfo->auth_data = out.pvBuffer;
1250 pAuthInfo->auth_data_len = out.cbBuffer;
1251 TRACE("sending next auth packet\n");
1253 else
1255 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status);
1256 heap_free(out.pvBuffer);
1257 destroy_authinfo(pAuthInfo);
1258 *ppAuthInfo = NULL;
1259 return FALSE;
1263 return TRUE;
1266 /***********************************************************************
1267 * HTTP_HttpAddRequestHeadersW (internal)
1269 static DWORD HTTP_HttpAddRequestHeadersW(http_request_t *request,
1270 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1272 LPWSTR lpszStart;
1273 LPWSTR lpszEnd;
1274 LPWSTR buffer;
1275 DWORD len, res = ERROR_HTTP_INVALID_HEADER;
1277 TRACE("copying header: %s\n", debugstr_wn(lpszHeader, dwHeaderLength));
1279 if( dwHeaderLength == ~0U )
1280 len = strlenW(lpszHeader);
1281 else
1282 len = dwHeaderLength;
1283 buffer = heap_alloc(sizeof(WCHAR)*(len+1));
1284 lstrcpynW( buffer, lpszHeader, len + 1);
1286 lpszStart = buffer;
1290 LPWSTR * pFieldAndValue;
1292 lpszEnd = lpszStart;
1294 while (*lpszEnd != '\0')
1296 if (*lpszEnd == '\r' || *lpszEnd == '\n')
1297 break;
1298 lpszEnd++;
1301 if (*lpszStart == '\0')
1302 break;
1304 if (*lpszEnd == '\r' || *lpszEnd == '\n')
1306 *lpszEnd = '\0';
1307 lpszEnd++; /* Jump over newline */
1309 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
1310 if (*lpszStart == '\0')
1312 /* Skip 0-length headers */
1313 lpszStart = lpszEnd;
1314 res = ERROR_SUCCESS;
1315 continue;
1317 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
1318 if (pFieldAndValue)
1320 res = HTTP_ProcessHeader(request, pFieldAndValue[0],
1321 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
1322 HTTP_FreeTokens(pFieldAndValue);
1325 lpszStart = lpszEnd;
1326 } while (res == ERROR_SUCCESS);
1328 heap_free(buffer);
1329 return res;
1332 /***********************************************************************
1333 * HttpAddRequestHeadersW (WININET.@)
1335 * Adds one or more HTTP header to the request handler
1337 * NOTE
1338 * On Windows if dwHeaderLength includes the trailing '\0', then
1339 * HttpAddRequestHeadersW() adds it too. However this results in an
1340 * invalid HTTP header which is rejected by some servers so we probably
1341 * don't need to match Windows on that point.
1343 * RETURNS
1344 * TRUE on success
1345 * FALSE on failure
1348 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
1349 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1351 http_request_t *request;
1352 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1354 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_wn(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
1356 if (!lpszHeader)
1357 return TRUE;
1359 request = (http_request_t*) get_handle_object( hHttpRequest );
1360 if (request && request->hdr.htype == WH_HHTTPREQ)
1361 res = HTTP_HttpAddRequestHeadersW( request, lpszHeader, dwHeaderLength, dwModifier );
1362 if( request )
1363 WININET_Release( &request->hdr );
1365 if(res != ERROR_SUCCESS)
1366 SetLastError(res);
1367 return res == ERROR_SUCCESS;
1370 /***********************************************************************
1371 * HttpAddRequestHeadersA (WININET.@)
1373 * Adds one or more HTTP header to the request handler
1375 * RETURNS
1376 * TRUE on success
1377 * FALSE on failure
1380 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
1381 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1383 DWORD len;
1384 LPWSTR hdr;
1385 BOOL r;
1387 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_an(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
1389 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
1390 hdr = heap_alloc(len*sizeof(WCHAR));
1391 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
1392 if( dwHeaderLength != ~0U )
1393 dwHeaderLength = len;
1395 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
1397 heap_free( hdr );
1398 return r;
1401 static void free_accept_types( WCHAR **accept_types )
1403 WCHAR *ptr, **types = accept_types;
1405 if (!types) return;
1406 while ((ptr = *types))
1408 heap_free( ptr );
1409 types++;
1411 heap_free( accept_types );
1414 static WCHAR **convert_accept_types( const char **accept_types )
1416 unsigned int count;
1417 const char **types = accept_types;
1418 WCHAR **typesW;
1419 BOOL invalid_pointer = FALSE;
1421 if (!types) return NULL;
1422 count = 0;
1423 while (*types)
1425 __TRY
1427 /* find out how many there are */
1428 if (*types && **types)
1430 TRACE("accept type: %s\n", debugstr_a(*types));
1431 count++;
1434 __EXCEPT_PAGE_FAULT
1436 WARN("invalid accept type pointer\n");
1437 invalid_pointer = TRUE;
1439 __ENDTRY;
1440 types++;
1442 if (invalid_pointer) return NULL;
1443 if (!(typesW = heap_alloc( sizeof(WCHAR *) * (count + 1) ))) return NULL;
1444 count = 0;
1445 types = accept_types;
1446 while (*types)
1448 if (*types && **types) typesW[count++] = heap_strdupAtoW( *types );
1449 types++;
1451 typesW[count] = NULL;
1452 return typesW;
1455 /***********************************************************************
1456 * HttpOpenRequestA (WININET.@)
1458 * Open a HTTP request handle
1460 * RETURNS
1461 * HINTERNET a HTTP request handle on success
1462 * NULL on failure
1465 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
1466 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
1467 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
1468 DWORD dwFlags, DWORD_PTR dwContext)
1470 LPWSTR szVerb = NULL, szObjectName = NULL;
1471 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
1472 HINTERNET rc = NULL;
1474 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
1475 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
1476 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
1477 dwFlags, dwContext);
1479 if (lpszVerb)
1481 szVerb = heap_strdupAtoW(lpszVerb);
1482 if ( !szVerb )
1483 goto end;
1486 if (lpszObjectName)
1488 szObjectName = heap_strdupAtoW(lpszObjectName);
1489 if ( !szObjectName )
1490 goto end;
1493 if (lpszVersion)
1495 szVersion = heap_strdupAtoW(lpszVersion);
1496 if ( !szVersion )
1497 goto end;
1500 if (lpszReferrer)
1502 szReferrer = heap_strdupAtoW(lpszReferrer);
1503 if ( !szReferrer )
1504 goto end;
1507 szAcceptTypes = convert_accept_types( lpszAcceptTypes );
1508 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName, szVersion, szReferrer,
1509 (const WCHAR **)szAcceptTypes, dwFlags, dwContext);
1511 end:
1512 free_accept_types(szAcceptTypes);
1513 heap_free(szReferrer);
1514 heap_free(szVersion);
1515 heap_free(szObjectName);
1516 heap_free(szVerb);
1517 return rc;
1520 /***********************************************************************
1521 * HTTP_EncodeBase64
1523 static UINT HTTP_EncodeBase64( LPCSTR bin, unsigned int len, LPWSTR base64 )
1525 UINT n = 0, x;
1526 static const CHAR HTTP_Base64Enc[] =
1527 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1529 while( len > 0 )
1531 /* first 6 bits, all from bin[0] */
1532 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
1533 x = (bin[0] & 3) << 4;
1535 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1536 if( len == 1 )
1538 base64[n++] = HTTP_Base64Enc[x];
1539 base64[n++] = '=';
1540 base64[n++] = '=';
1541 break;
1543 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
1544 x = ( bin[1] & 0x0f ) << 2;
1546 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1547 if( len == 2 )
1549 base64[n++] = HTTP_Base64Enc[x];
1550 base64[n++] = '=';
1551 break;
1553 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
1555 /* last 6 bits, all from bin [2] */
1556 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
1557 bin += 3;
1558 len -= 3;
1560 base64[n] = 0;
1561 return n;
1564 static const signed char HTTP_Base64Dec[] =
1566 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00 */
1567 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10 */
1568 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20 */
1569 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 0x30 */
1570 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40 */
1571 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50 */
1572 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60 */
1573 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 0x70 */
1576 /***********************************************************************
1577 * HTTP_DecodeBase64
1579 static UINT HTTP_DecodeBase64( LPCWSTR base64, LPSTR bin )
1581 unsigned int n = 0;
1583 while(*base64)
1585 signed char in[4];
1587 if (base64[0] >= ARRAYSIZE(HTTP_Base64Dec) ||
1588 ((in[0] = HTTP_Base64Dec[base64[0]]) == -1) ||
1589 base64[1] >= ARRAYSIZE(HTTP_Base64Dec) ||
1590 ((in[1] = HTTP_Base64Dec[base64[1]]) == -1))
1592 WARN("invalid base64: %s\n", debugstr_w(base64));
1593 return 0;
1595 if (bin)
1596 bin[n] = (unsigned char) (in[0] << 2 | in[1] >> 4);
1597 n++;
1599 if ((base64[2] == '=') && (base64[3] == '='))
1600 break;
1601 if (base64[2] > ARRAYSIZE(HTTP_Base64Dec) ||
1602 ((in[2] = HTTP_Base64Dec[base64[2]]) == -1))
1604 WARN("invalid base64: %s\n", debugstr_w(&base64[2]));
1605 return 0;
1607 if (bin)
1608 bin[n] = (unsigned char) (in[1] << 4 | in[2] >> 2);
1609 n++;
1611 if (base64[3] == '=')
1612 break;
1613 if (base64[3] > ARRAYSIZE(HTTP_Base64Dec) ||
1614 ((in[3] = HTTP_Base64Dec[base64[3]]) == -1))
1616 WARN("invalid base64: %s\n", debugstr_w(&base64[3]));
1617 return 0;
1619 if (bin)
1620 bin[n] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
1621 n++;
1623 base64 += 4;
1626 return n;
1629 static WCHAR *encode_auth_data( const WCHAR *scheme, const char *data, UINT data_len )
1631 WCHAR *ret;
1632 UINT len, scheme_len = strlenW( scheme );
1634 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1635 len = scheme_len + 1 + ((data_len + 2) * 4) / 3;
1636 if (!(ret = heap_alloc( (len + 1) * sizeof(WCHAR) ))) return NULL;
1637 memcpy( ret, scheme, scheme_len * sizeof(WCHAR) );
1638 ret[scheme_len] = ' ';
1639 HTTP_EncodeBase64( data, data_len, ret + scheme_len + 1 );
1640 return ret;
1644 /***********************************************************************
1645 * HTTP_InsertAuthorization
1647 * Insert or delete the authorization field in the request header.
1649 static BOOL HTTP_InsertAuthorization( http_request_t *request, struct HttpAuthInfo *pAuthInfo, LPCWSTR header )
1651 static const WCHAR wszBasic[] = {'B','a','s','i','c',0};
1652 WCHAR *host, *authorization = NULL;
1654 if (pAuthInfo)
1656 if (pAuthInfo->auth_data_len)
1658 if (!(authorization = encode_auth_data(pAuthInfo->scheme, pAuthInfo->auth_data, pAuthInfo->auth_data_len)))
1659 return FALSE;
1661 /* clear the data as it isn't valid now that it has been sent to the
1662 * server, unless it's Basic authentication which doesn't do
1663 * connection tracking */
1664 if (strcmpiW(pAuthInfo->scheme, wszBasic))
1666 heap_free(pAuthInfo->auth_data);
1667 pAuthInfo->auth_data = NULL;
1668 pAuthInfo->auth_data_len = 0;
1672 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1674 HTTP_ProcessHeader(request, header, authorization, HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
1675 heap_free(authorization);
1677 else if (!strcmpW(header, szAuthorization) && (host = get_host_header(request)))
1679 UINT data_len;
1680 char *data;
1682 if ((data_len = retrieve_cached_basic_authorization(host, NULL, &data)))
1684 TRACE("Found cached basic authorization for %s\n", debugstr_w(host));
1686 if (!(authorization = encode_auth_data(wszBasic, data, data_len)))
1688 heap_free(data);
1689 heap_free(host);
1690 return FALSE;
1693 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1695 HTTP_ProcessHeader(request, header, authorization, HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
1696 heap_free(data);
1697 heap_free(authorization);
1699 heap_free(host);
1701 return TRUE;
1704 static WCHAR *build_proxy_path_url(http_request_t *req)
1706 DWORD size, len;
1707 WCHAR *url;
1709 len = strlenW(req->server->scheme_host_port);
1710 size = len + strlenW(req->path) + 1;
1711 if(*req->path != '/')
1712 size++;
1713 url = heap_alloc(size * sizeof(WCHAR));
1714 if(!url)
1715 return NULL;
1717 memcpy(url, req->server->scheme_host_port, len*sizeof(WCHAR));
1718 if(*req->path != '/')
1719 url[len++] = '/';
1721 strcpyW(url+len, req->path);
1723 TRACE("url=%s\n", debugstr_w(url));
1724 return url;
1727 static BOOL HTTP_DomainMatches(LPCWSTR server, LPCWSTR domain)
1729 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
1730 BOOL ret = FALSE;
1732 if (!strcmpiW( domain, localW ) && !strchrW( server, '.' ))
1733 ret = TRUE;
1734 else if (*domain == '*')
1736 if (domain[1] == '.')
1738 LPCWSTR dot;
1740 /* For a hostname to match a wildcard, the last domain must match
1741 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
1742 * hostname is www.foo.a.b, it matches, but a.b does not.
1744 dot = strchrW( server, '.' );
1745 if (dot)
1747 int len = strlenW( dot + 1 );
1749 if (len > strlenW( domain + 2 ))
1751 LPCWSTR ptr;
1753 /* The server's domain is longer than the wildcard, so it
1754 * could be a subdomain. Compare the last portion of the
1755 * server's domain.
1757 ptr = dot + len + 1 - strlenW( domain + 2 );
1758 if (!strcmpiW( ptr, domain + 2 ))
1760 /* This is only a match if the preceding character is
1761 * a '.', i.e. that it is a matching domain. E.g.
1762 * if domain is '*.b.c' and server is 'www.ab.c' they
1763 * do not match.
1765 ret = *(ptr - 1) == '.';
1768 else
1769 ret = !strcmpiW( dot + 1, domain + 2 );
1773 else
1774 ret = !strcmpiW( server, domain );
1775 return ret;
1778 static BOOL HTTP_ShouldBypassProxy(appinfo_t *lpwai, LPCWSTR server)
1780 LPCWSTR ptr;
1781 BOOL ret = FALSE;
1783 if (!lpwai->proxyBypass) return FALSE;
1784 ptr = lpwai->proxyBypass;
1785 do {
1786 LPCWSTR tmp = ptr;
1788 ptr = strchrW( ptr, ';' );
1789 if (!ptr)
1790 ptr = strchrW( tmp, ' ' );
1791 if (ptr)
1793 if (ptr - tmp < INTERNET_MAX_HOST_NAME_LENGTH)
1795 WCHAR domain[INTERNET_MAX_HOST_NAME_LENGTH];
1797 memcpy( domain, tmp, (ptr - tmp) * sizeof(WCHAR) );
1798 domain[ptr - tmp] = 0;
1799 ret = HTTP_DomainMatches( server, domain );
1801 ptr += 1;
1803 else if (*tmp)
1804 ret = HTTP_DomainMatches( server, tmp );
1805 } while (ptr && !ret);
1806 return ret;
1809 /***********************************************************************
1810 * HTTP_DealWithProxy
1812 static BOOL HTTP_DealWithProxy(appinfo_t *hIC, http_session_t *session, http_request_t *request)
1814 static const WCHAR protoHttp[] = { 'h','t','t','p',0 };
1815 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 };
1816 static const WCHAR szFormat[] = { 'h','t','t','p',':','/','/','%','s',0 };
1817 WCHAR buf[INTERNET_MAX_HOST_NAME_LENGTH];
1818 WCHAR protoProxy[INTERNET_MAX_URL_LENGTH];
1819 DWORD protoProxyLen = INTERNET_MAX_URL_LENGTH;
1820 WCHAR proxy[INTERNET_MAX_URL_LENGTH];
1821 static WCHAR szNul[] = { 0 };
1822 URL_COMPONENTSW UrlComponents;
1823 server_t *new_server;
1824 BOOL is_https;
1826 memset( &UrlComponents, 0, sizeof UrlComponents );
1827 UrlComponents.dwStructSize = sizeof UrlComponents;
1828 UrlComponents.lpszHostName = buf;
1829 UrlComponents.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH;
1831 if (!INTERNET_FindProxyForProtocol(hIC->proxy, protoHttp, protoProxy, &protoProxyLen))
1832 return FALSE;
1833 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1834 protoProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
1835 sprintfW(proxy, szFormat, protoProxy);
1836 else
1837 strcpyW(proxy, protoProxy);
1838 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
1839 return FALSE;
1840 if( UrlComponents.dwHostNameLength == 0 )
1841 return FALSE;
1843 if( !request->path )
1844 request->path = szNul;
1846 is_https = (UrlComponents.nScheme == INTERNET_SCHEME_HTTPS);
1847 if (is_https && UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1848 UrlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
1850 new_server = get_server(UrlComponents.lpszHostName, UrlComponents.nPort, is_https, TRUE);
1851 if(!new_server)
1852 return FALSE;
1854 request->proxy = new_server;
1856 TRACE("proxy server=%s port=%d\n", debugstr_w(new_server->name), new_server->port);
1857 return TRUE;
1860 static DWORD HTTP_ResolveName(http_request_t *request)
1862 server_t *server = request->proxy ? request->proxy : request->server;
1863 int addr_len;
1865 if(server->addr_len)
1866 return ERROR_SUCCESS;
1868 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
1869 INTERNET_STATUS_RESOLVING_NAME,
1870 server->name,
1871 (strlenW(server->name)+1) * sizeof(WCHAR));
1873 addr_len = sizeof(server->addr);
1874 if (!GetAddress(server->name, server->port, (SOCKADDR*)&server->addr, &addr_len, server->addr_str))
1875 return ERROR_INTERNET_NAME_NOT_RESOLVED;
1877 server->addr_len = addr_len;
1878 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
1879 INTERNET_STATUS_NAME_RESOLVED,
1880 server->addr_str, strlen(server->addr_str)+1);
1882 TRACE("resolved %s to %s\n", debugstr_w(server->name), server->addr_str);
1883 return ERROR_SUCCESS;
1886 static BOOL HTTP_GetRequestURL(http_request_t *req, LPWSTR buf)
1888 static const WCHAR http[] = { 'h','t','t','p',':','/','/',0 };
1889 static const WCHAR https[] = { 'h','t','t','p','s',':','/','/',0 };
1890 static const WCHAR slash[] = { '/',0 };
1891 LPHTTPHEADERW host_header;
1892 LPCWSTR scheme;
1894 host_header = HTTP_GetHeader(req, hostW);
1895 if(!host_header)
1896 return FALSE;
1898 EnterCriticalSection( &req->headers_section );
1900 if (req->hdr.dwFlags & INTERNET_FLAG_SECURE)
1901 scheme = https;
1902 else
1903 scheme = http;
1904 strcpyW(buf, scheme);
1905 strcatW(buf, host_header->lpszValue);
1906 if (req->path[0] != '/')
1907 strcatW(buf, slash);
1908 strcatW(buf, req->path);
1910 LeaveCriticalSection( &req->headers_section );
1911 return TRUE;
1915 /***********************************************************************
1916 * HTTPREQ_Destroy (internal)
1918 * Deallocate request handle
1921 static void HTTPREQ_Destroy(object_header_t *hdr)
1923 http_request_t *request = (http_request_t*) hdr;
1924 DWORD i;
1926 TRACE("\n");
1928 if(request->hCacheFile)
1929 CloseHandle(request->hCacheFile);
1930 if(request->req_file)
1931 req_file_release(request->req_file);
1933 request->headers_section.DebugInfo->Spare[0] = 0;
1934 DeleteCriticalSection( &request->headers_section );
1935 request->read_section.DebugInfo->Spare[0] = 0;
1936 DeleteCriticalSection( &request->read_section );
1937 WININET_Release(&request->session->hdr);
1939 destroy_authinfo(request->authInfo);
1940 destroy_authinfo(request->proxyAuthInfo);
1942 if(request->server)
1943 server_release(request->server);
1944 if(request->proxy)
1945 server_release(request->proxy);
1947 heap_free(request->path);
1948 heap_free(request->verb);
1949 heap_free(request->version);
1950 heap_free(request->statusText);
1952 for (i = 0; i < request->nCustHeaders; i++)
1954 heap_free(request->custHeaders[i].lpszField);
1955 heap_free(request->custHeaders[i].lpszValue);
1957 destroy_data_stream(request->data_stream);
1958 heap_free(request->custHeaders);
1961 static void http_release_netconn(http_request_t *req, BOOL reuse)
1963 TRACE("%p %p %x\n",req, req->netconn, reuse);
1965 if(!is_valid_netconn(req->netconn))
1966 return;
1968 if(reuse && req->netconn->keep_alive) {
1969 BOOL run_collector;
1971 EnterCriticalSection(&connection_pool_cs);
1973 list_add_head(&req->netconn->server->conn_pool, &req->netconn->pool_entry);
1974 req->netconn->keep_until = GetTickCount64() + COLLECT_TIME;
1975 req->netconn = NULL;
1977 run_collector = !collector_running;
1978 collector_running = TRUE;
1980 LeaveCriticalSection(&connection_pool_cs);
1982 if(run_collector) {
1983 HANDLE thread = NULL;
1984 HMODULE module;
1986 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (const WCHAR*)WININET_hModule, &module);
1987 if(module)
1988 thread = CreateThread(NULL, 0, collect_connections_proc, NULL, 0, NULL);
1989 if(!thread) {
1990 EnterCriticalSection(&connection_pool_cs);
1991 collector_running = FALSE;
1992 LeaveCriticalSection(&connection_pool_cs);
1994 if(module)
1995 FreeLibrary(module);
1997 else
1998 CloseHandle(thread);
2000 return;
2003 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
2004 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2006 close_netconn(req->netconn);
2008 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
2009 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2012 static BOOL HTTP_KeepAlive(http_request_t *request)
2014 WCHAR szVersion[10];
2015 WCHAR szConnectionResponse[20];
2016 DWORD dwBufferSize = sizeof(szVersion);
2017 BOOL keepalive = FALSE;
2019 /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
2020 * the connection is keep-alive by default */
2021 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_VERSION, szVersion, &dwBufferSize, NULL) == ERROR_SUCCESS
2022 && !strcmpiW(szVersion, g_szHttp1_1))
2024 keepalive = TRUE;
2027 dwBufferSize = sizeof(szConnectionResponse);
2028 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_PROXY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) == ERROR_SUCCESS
2029 || HTTP_HttpQueryInfoW(request, HTTP_QUERY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) == ERROR_SUCCESS)
2031 keepalive = !strcmpiW(szConnectionResponse, szKeepAlive);
2034 return keepalive;
2037 static void HTTPREQ_CloseConnection(object_header_t *hdr)
2039 http_request_t *req = (http_request_t*)hdr;
2041 http_release_netconn(req, drain_content(req, FALSE));
2044 static DWORD str_to_buffer(const WCHAR *str, void *buffer, DWORD *size, BOOL unicode)
2046 int len;
2047 if (unicode)
2049 WCHAR *buf = buffer;
2051 if (str) len = strlenW(str);
2052 else len = 0;
2053 if (*size < (len + 1) * sizeof(WCHAR))
2055 *size = (len + 1) * sizeof(WCHAR);
2056 return ERROR_INSUFFICIENT_BUFFER;
2058 if (str) strcpyW(buf, str);
2059 else buf[0] = 0;
2061 *size = len;
2062 return ERROR_SUCCESS;
2064 else
2066 char *buf = buffer;
2068 if (str) len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
2069 else len = 1;
2070 if (*size < len)
2072 *size = len;
2073 return ERROR_INSUFFICIENT_BUFFER;
2075 if (str) WideCharToMultiByte(CP_ACP, 0, str, -1, buf, *size, NULL, NULL);
2076 else buf[0] = 0;
2078 *size = len - 1;
2079 return ERROR_SUCCESS;
2083 static DWORD HTTPREQ_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2085 http_request_t *req = (http_request_t*)hdr;
2087 switch(option) {
2088 case INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO:
2090 INTERNET_DIAGNOSTIC_SOCKET_INFO *info = buffer;
2092 FIXME("INTERNET_DIAGNOSTIC_SOCKET_INFO stub\n");
2094 if (*size < sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO))
2095 return ERROR_INSUFFICIENT_BUFFER;
2096 *size = sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO);
2097 /* FIXME: can't get a SOCKET from our connection since we don't use
2098 * winsock
2100 info->Socket = 0;
2101 /* FIXME: get source port from req->netConnection */
2102 info->SourcePort = 0;
2103 info->DestPort = req->server->port;
2104 info->Flags = 0;
2105 if (HTTP_KeepAlive(req))
2106 info->Flags |= IDSI_FLAG_KEEP_ALIVE;
2107 if (req->proxy)
2108 info->Flags |= IDSI_FLAG_PROXY;
2109 if (is_valid_netconn(req->netconn) && req->netconn->secure)
2110 info->Flags |= IDSI_FLAG_SECURE;
2112 return ERROR_SUCCESS;
2115 case 98:
2116 TRACE("Queried undocumented option 98, forwarding to INTERNET_OPTION_SECURITY_FLAGS\n");
2117 /* fall through */
2118 case INTERNET_OPTION_SECURITY_FLAGS:
2120 DWORD flags;
2122 if (*size < sizeof(ULONG))
2123 return ERROR_INSUFFICIENT_BUFFER;
2125 *size = sizeof(DWORD);
2126 flags = is_valid_netconn(req->netconn) ? req->netconn->security_flags : req->security_flags | req->server->security_flags;
2127 *(DWORD *)buffer = flags;
2129 TRACE("INTERNET_OPTION_SECURITY_FLAGS %x\n", flags);
2130 return ERROR_SUCCESS;
2133 case INTERNET_OPTION_HANDLE_TYPE:
2134 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
2136 if (*size < sizeof(ULONG))
2137 return ERROR_INSUFFICIENT_BUFFER;
2139 *size = sizeof(DWORD);
2140 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_HTTP_REQUEST;
2141 return ERROR_SUCCESS;
2143 case INTERNET_OPTION_URL: {
2144 static const WCHAR httpW[] = {'h','t','t','p',':','/','/',0};
2145 WCHAR url[INTERNET_MAX_URL_LENGTH];
2146 HTTPHEADERW *host;
2148 TRACE("INTERNET_OPTION_URL\n");
2150 EnterCriticalSection( &req->headers_section );
2151 host = HTTP_GetHeader(req, hostW);
2152 strcpyW(url, httpW);
2153 strcatW(url, host->lpszValue);
2154 strcatW(url, req->path);
2155 LeaveCriticalSection( &req->headers_section );
2157 TRACE("INTERNET_OPTION_URL: %s\n",debugstr_w(url));
2158 return str_to_buffer(url, buffer, size, unicode);
2160 case INTERNET_OPTION_USER_AGENT:
2161 return str_to_buffer(req->session->appInfo->agent, buffer, size, unicode);
2162 case INTERNET_OPTION_USERNAME:
2163 return str_to_buffer(req->session->userName, buffer, size, unicode);
2164 case INTERNET_OPTION_PASSWORD:
2165 return str_to_buffer(req->session->password, buffer, size, unicode);
2166 case INTERNET_OPTION_PROXY_USERNAME:
2167 return str_to_buffer(req->session->appInfo->proxyUsername, buffer, size, unicode);
2168 case INTERNET_OPTION_PROXY_PASSWORD:
2169 return str_to_buffer(req->session->appInfo->proxyPassword, buffer, size, unicode);
2171 case INTERNET_OPTION_CACHE_TIMESTAMPS: {
2172 INTERNET_CACHE_ENTRY_INFOW *info;
2173 INTERNET_CACHE_TIMESTAMPS *ts = buffer;
2174 WCHAR url[INTERNET_MAX_URL_LENGTH];
2175 DWORD nbytes, error;
2176 BOOL ret;
2178 TRACE("INTERNET_OPTION_CACHE_TIMESTAMPS\n");
2180 if (*size < sizeof(*ts))
2182 *size = sizeof(*ts);
2183 return ERROR_INSUFFICIENT_BUFFER;
2185 nbytes = 0;
2186 HTTP_GetRequestURL(req, url);
2187 ret = GetUrlCacheEntryInfoW(url, NULL, &nbytes);
2188 error = GetLastError();
2189 if (!ret && error == ERROR_INSUFFICIENT_BUFFER)
2191 if (!(info = heap_alloc(nbytes)))
2192 return ERROR_OUTOFMEMORY;
2194 GetUrlCacheEntryInfoW(url, info, &nbytes);
2196 ts->ftExpires = info->ExpireTime;
2197 ts->ftLastModified = info->LastModifiedTime;
2199 heap_free(info);
2200 *size = sizeof(*ts);
2201 return ERROR_SUCCESS;
2203 return error;
2206 case INTERNET_OPTION_DATAFILE_NAME: {
2207 DWORD req_size;
2209 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
2211 if(!req->req_file) {
2212 *size = 0;
2213 return ERROR_INTERNET_ITEM_NOT_FOUND;
2216 if(unicode) {
2217 req_size = (lstrlenW(req->req_file->file_name)+1) * sizeof(WCHAR);
2218 if(*size < req_size)
2219 return ERROR_INSUFFICIENT_BUFFER;
2221 *size = req_size;
2222 memcpy(buffer, req->req_file->file_name, *size);
2223 return ERROR_SUCCESS;
2224 }else {
2225 req_size = WideCharToMultiByte(CP_ACP, 0, req->req_file->file_name, -1, NULL, 0, NULL, NULL);
2226 if (req_size > *size)
2227 return ERROR_INSUFFICIENT_BUFFER;
2229 *size = WideCharToMultiByte(CP_ACP, 0, req->req_file->file_name,
2230 -1, buffer, *size, NULL, NULL);
2231 return ERROR_SUCCESS;
2235 case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT: {
2236 PCCERT_CONTEXT context;
2238 if(!req->netconn)
2239 return ERROR_INTERNET_INVALID_OPERATION;
2241 if(*size < sizeof(INTERNET_CERTIFICATE_INFOA)) {
2242 *size = sizeof(INTERNET_CERTIFICATE_INFOA);
2243 return ERROR_INSUFFICIENT_BUFFER;
2246 context = (PCCERT_CONTEXT)NETCON_GetCert(req->netconn);
2247 if(context) {
2248 INTERNET_CERTIFICATE_INFOA *info = (INTERNET_CERTIFICATE_INFOA*)buffer;
2249 DWORD len;
2251 memset(info, 0, sizeof(*info));
2252 info->ftExpiry = context->pCertInfo->NotAfter;
2253 info->ftStart = context->pCertInfo->NotBefore;
2254 len = CertNameToStrA(context->dwCertEncodingType,
2255 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG, NULL, 0);
2256 info->lpszSubjectInfo = LocalAlloc(0, len);
2257 if(info->lpszSubjectInfo)
2258 CertNameToStrA(context->dwCertEncodingType,
2259 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG,
2260 info->lpszSubjectInfo, len);
2261 len = CertNameToStrA(context->dwCertEncodingType,
2262 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG, NULL, 0);
2263 info->lpszIssuerInfo = LocalAlloc(0, len);
2264 if(info->lpszIssuerInfo)
2265 CertNameToStrA(context->dwCertEncodingType,
2266 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG,
2267 info->lpszIssuerInfo, len);
2268 info->dwKeySize = NETCON_GetCipherStrength(req->netconn);
2269 CertFreeCertificateContext(context);
2270 return ERROR_SUCCESS;
2272 return ERROR_NOT_SUPPORTED;
2274 case INTERNET_OPTION_CONNECT_TIMEOUT:
2275 if (*size < sizeof(DWORD))
2276 return ERROR_INSUFFICIENT_BUFFER;
2278 *size = sizeof(DWORD);
2279 *(DWORD *)buffer = req->connect_timeout;
2280 return ERROR_SUCCESS;
2281 case INTERNET_OPTION_REQUEST_FLAGS: {
2282 DWORD flags = 0;
2284 if (*size < sizeof(DWORD))
2285 return ERROR_INSUFFICIENT_BUFFER;
2287 /* FIXME: Add support for:
2288 * INTERNET_REQFLAG_FROM_CACHE
2289 * INTERNET_REQFLAG_CACHE_WRITE_DISABLED
2292 if(req->proxy)
2293 flags |= INTERNET_REQFLAG_VIA_PROXY;
2294 if(!req->status_code)
2295 flags |= INTERNET_REQFLAG_NO_HEADERS;
2297 TRACE("INTERNET_OPTION_REQUEST_FLAGS returning %x\n", flags);
2299 *size = sizeof(DWORD);
2300 *(DWORD*)buffer = flags;
2301 return ERROR_SUCCESS;
2305 return INET_QueryOption(hdr, option, buffer, size, unicode);
2308 static DWORD HTTPREQ_SetOption(object_header_t *hdr, DWORD option, void *buffer, DWORD size)
2310 http_request_t *req = (http_request_t*)hdr;
2312 switch(option) {
2313 case 99: /* Undocumented, seems to be INTERNET_OPTION_SECURITY_FLAGS with argument validation */
2314 TRACE("Undocumented option 99\n");
2316 if (!buffer || size != sizeof(DWORD))
2317 return ERROR_INVALID_PARAMETER;
2318 if(*(DWORD*)buffer & ~SECURITY_SET_MASK)
2319 return ERROR_INTERNET_OPTION_NOT_SETTABLE;
2321 /* fall through */
2322 case INTERNET_OPTION_SECURITY_FLAGS:
2324 DWORD flags;
2326 if (!buffer || size != sizeof(DWORD))
2327 return ERROR_INVALID_PARAMETER;
2328 flags = *(DWORD *)buffer;
2329 TRACE("INTERNET_OPTION_SECURITY_FLAGS %08x\n", flags);
2330 flags &= SECURITY_SET_MASK;
2331 req->security_flags |= flags;
2332 if(is_valid_netconn(req->netconn))
2333 req->netconn->security_flags |= flags;
2334 return ERROR_SUCCESS;
2336 case INTERNET_OPTION_CONNECT_TIMEOUT:
2337 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2338 req->connect_timeout = *(DWORD *)buffer;
2339 return ERROR_SUCCESS;
2341 case INTERNET_OPTION_SEND_TIMEOUT:
2342 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2343 req->send_timeout = *(DWORD *)buffer;
2344 return ERROR_SUCCESS;
2346 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2347 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2348 req->receive_timeout = *(DWORD *)buffer;
2349 return ERROR_SUCCESS;
2351 case INTERNET_OPTION_USERNAME:
2352 heap_free(req->session->userName);
2353 if (!(req->session->userName = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2354 return ERROR_SUCCESS;
2356 case INTERNET_OPTION_PASSWORD:
2357 heap_free(req->session->password);
2358 if (!(req->session->password = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2359 return ERROR_SUCCESS;
2361 case INTERNET_OPTION_PROXY_USERNAME:
2362 heap_free(req->session->appInfo->proxyUsername);
2363 if (!(req->session->appInfo->proxyUsername = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2364 return ERROR_SUCCESS;
2366 case INTERNET_OPTION_PROXY_PASSWORD:
2367 heap_free(req->session->appInfo->proxyPassword);
2368 if (!(req->session->appInfo->proxyPassword = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2369 return ERROR_SUCCESS;
2371 case INTERNET_OPTION_HTTP_DECODING:
2372 if(size != sizeof(BOOL))
2373 return ERROR_INVALID_PARAMETER;
2374 req->decoding = *(BOOL*)buffer;
2375 return ERROR_SUCCESS;
2378 return INET_SetOption(hdr, option, buffer, size);
2381 static void commit_cache_entry(http_request_t *req)
2383 WCHAR url[INTERNET_MAX_URL_LENGTH];
2385 TRACE("%p\n", req);
2387 CloseHandle(req->hCacheFile);
2388 req->hCacheFile = NULL;
2390 if(HTTP_GetRequestURL(req, url)) {
2391 WCHAR *header;
2392 DWORD header_len;
2393 BOOL res;
2395 header = build_response_header(req, TRUE);
2396 header_len = (header ? strlenW(header) : 0);
2397 res = CommitUrlCacheEntryW(url, req->req_file->file_name, req->expires,
2398 req->last_modified, NORMAL_CACHE_ENTRY,
2399 header, header_len, NULL, 0);
2400 if(res)
2401 req->req_file->is_committed = TRUE;
2402 else
2403 WARN("CommitUrlCacheEntry failed: %u\n", GetLastError());
2404 heap_free(header);
2408 static void create_cache_entry(http_request_t *req)
2410 static const WCHAR no_cacheW[] = {'n','o','-','c','a','c','h','e',0};
2411 static const WCHAR no_storeW[] = {'n','o','-','s','t','o','r','e',0};
2413 WCHAR url[INTERNET_MAX_URL_LENGTH];
2414 WCHAR file_name[MAX_PATH+1];
2415 BOOL b = TRUE;
2417 /* FIXME: We should free previous cache file earlier */
2418 if(req->req_file) {
2419 req_file_release(req->req_file);
2420 req->req_file = NULL;
2422 if(req->hCacheFile) {
2423 CloseHandle(req->hCacheFile);
2424 req->hCacheFile = NULL;
2427 if(req->hdr.dwFlags & INTERNET_FLAG_NO_CACHE_WRITE)
2428 b = FALSE;
2430 if(b) {
2431 int header_idx;
2433 EnterCriticalSection( &req->headers_section );
2435 header_idx = HTTP_GetCustomHeaderIndex(req, szCache_Control, 0, FALSE);
2436 if(header_idx != -1) {
2437 WCHAR *ptr;
2439 for(ptr=req->custHeaders[header_idx].lpszValue; *ptr; ) {
2440 WCHAR *end;
2442 while(*ptr==' ' || *ptr=='\t')
2443 ptr++;
2445 end = strchrW(ptr, ',');
2446 if(!end)
2447 end = ptr + strlenW(ptr);
2449 if(!strncmpiW(ptr, no_cacheW, sizeof(no_cacheW)/sizeof(*no_cacheW)-1)
2450 || !strncmpiW(ptr, no_storeW, sizeof(no_storeW)/sizeof(*no_storeW)-1)) {
2451 b = FALSE;
2452 break;
2455 ptr = end;
2456 if(*ptr == ',')
2457 ptr++;
2461 LeaveCriticalSection( &req->headers_section );
2464 if(!b) {
2465 if(!(req->hdr.dwFlags & INTERNET_FLAG_NEED_FILE))
2466 return;
2468 FIXME("INTERNET_FLAG_NEED_FILE is not supported correctly\n");
2471 b = HTTP_GetRequestURL(req, url);
2472 if(!b) {
2473 WARN("Could not get URL\n");
2474 return;
2477 b = CreateUrlCacheEntryW(url, req->contentLength == ~0u ? 0 : req->contentLength, NULL, file_name, 0);
2478 if(!b) {
2479 WARN("Could not create cache entry: %08x\n", GetLastError());
2480 return;
2483 create_req_file(file_name, &req->req_file);
2485 req->hCacheFile = CreateFileW(file_name, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
2486 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2487 if(req->hCacheFile == INVALID_HANDLE_VALUE) {
2488 WARN("Could not create file: %u\n", GetLastError());
2489 req->hCacheFile = NULL;
2490 return;
2493 if(req->read_size) {
2494 DWORD written;
2496 b = WriteFile(req->hCacheFile, req->read_buf+req->read_pos, req->read_size, &written, NULL);
2497 if(!b)
2498 FIXME("WriteFile failed: %u\n", GetLastError());
2500 if(req->data_stream->vtbl->end_of_data(req->data_stream, req))
2501 commit_cache_entry(req);
2505 /* read some more data into the read buffer (the read section must be held) */
2506 static DWORD read_more_data( http_request_t *req, int maxlen )
2508 DWORD res;
2509 int len;
2511 if (req->read_pos)
2513 /* move existing data to the start of the buffer */
2514 if(req->read_size)
2515 memmove( req->read_buf, req->read_buf + req->read_pos, req->read_size );
2516 req->read_pos = 0;
2519 if (maxlen == -1) maxlen = sizeof(req->read_buf);
2521 res = NETCON_recv( req->netconn, req->read_buf + req->read_size,
2522 maxlen - req->read_size, TRUE, &len );
2523 if(res == ERROR_SUCCESS)
2524 req->read_size += len;
2526 return res;
2529 /* remove some amount of data from the read buffer (the read section must be held) */
2530 static void remove_data( http_request_t *req, int count )
2532 if (!(req->read_size -= count)) req->read_pos = 0;
2533 else req->read_pos += count;
2536 static DWORD read_line( http_request_t *req, LPSTR buffer, DWORD *len )
2538 int count, bytes_read, pos = 0;
2539 DWORD res;
2541 EnterCriticalSection( &req->read_section );
2542 for (;;)
2544 BYTE *eol = memchr( req->read_buf + req->read_pos, '\n', req->read_size );
2546 if (eol)
2548 count = eol - (req->read_buf + req->read_pos);
2549 bytes_read = count + 1;
2551 else count = bytes_read = req->read_size;
2553 count = min( count, *len - pos );
2554 memcpy( buffer + pos, req->read_buf + req->read_pos, count );
2555 pos += count;
2556 remove_data( req, bytes_read );
2557 if (eol) break;
2559 if ((res = read_more_data( req, -1 )))
2561 WARN( "read failed %u\n", res );
2562 LeaveCriticalSection( &req->read_section );
2563 return res;
2565 if (!req->read_size)
2567 *len = 0;
2568 TRACE( "returning empty string\n" );
2569 LeaveCriticalSection( &req->read_section );
2570 return ERROR_SUCCESS;
2573 LeaveCriticalSection( &req->read_section );
2575 if (pos < *len)
2577 if (pos && buffer[pos - 1] == '\r') pos--;
2578 *len = pos + 1;
2580 buffer[*len - 1] = 0;
2581 TRACE( "returning %s\n", debugstr_a(buffer));
2582 return ERROR_SUCCESS;
2585 /* check if we have reached the end of the data to read (the read section must be held) */
2586 static BOOL end_of_read_data( http_request_t *req )
2588 return !req->read_size && req->data_stream->vtbl->end_of_data(req->data_stream, req);
2591 static DWORD read_http_stream(http_request_t *req, BYTE *buf, DWORD size, DWORD *read, blocking_mode_t blocking_mode)
2593 DWORD res;
2595 res = req->data_stream->vtbl->read(req->data_stream, req, buf, size, read, blocking_mode);
2596 assert(*read <= size);
2598 if(req->hCacheFile) {
2599 if(*read) {
2600 BOOL bres;
2601 DWORD written;
2603 bres = WriteFile(req->hCacheFile, buf, *read, &written, NULL);
2604 if(!bres)
2605 FIXME("WriteFile failed: %u\n", GetLastError());
2608 if(req->data_stream->vtbl->end_of_data(req->data_stream, req))
2609 commit_cache_entry(req);
2612 return res;
2615 /* fetch some more data into the read buffer (the read section must be held) */
2616 static DWORD refill_read_buffer(http_request_t *req, blocking_mode_t blocking_mode, DWORD *read_bytes)
2618 DWORD res, read=0;
2620 if(req->read_size == sizeof(req->read_buf))
2621 return ERROR_SUCCESS;
2623 if(req->read_pos) {
2624 if(req->read_size)
2625 memmove(req->read_buf, req->read_buf+req->read_pos, req->read_size);
2626 req->read_pos = 0;
2629 res = read_http_stream(req, req->read_buf+req->read_size, sizeof(req->read_buf) - req->read_size,
2630 &read, blocking_mode);
2631 req->read_size += read;
2633 TRACE("read %u bytes, read_size %u\n", read, req->read_size);
2634 if(read_bytes)
2635 *read_bytes = read;
2636 return res;
2639 /* return the size of data available to be read immediately (the read section must be held) */
2640 static DWORD get_avail_data( http_request_t *req )
2642 return req->read_size + req->data_stream->vtbl->get_avail_data(req->data_stream, req);
2645 static DWORD netconn_get_avail_data(data_stream_t *stream, http_request_t *req)
2647 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2648 DWORD avail = 0;
2650 if(is_valid_netconn(req->netconn))
2651 NETCON_query_data_available(req->netconn, &avail);
2652 return netconn_stream->content_length == ~0u
2653 ? avail
2654 : min(avail, netconn_stream->content_length-netconn_stream->content_read);
2657 static BOOL netconn_end_of_data(data_stream_t *stream, http_request_t *req)
2659 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2660 return netconn_stream->content_read == netconn_stream->content_length || !is_valid_netconn(req->netconn);
2663 static DWORD netconn_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
2664 DWORD *read, blocking_mode_t blocking_mode)
2666 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2667 DWORD res = ERROR_SUCCESS;
2668 int len = 0, ret = 0;
2670 size = min(size, netconn_stream->content_length-netconn_stream->content_read);
2672 if(size && is_valid_netconn(req->netconn)) {
2673 while((res = NETCON_recv(req->netconn, buf+ret, size-ret, blocking_mode != BLOCKING_DISALLOW, &len)) == ERROR_SUCCESS) {
2674 if(!len) {
2675 netconn_stream->content_length = netconn_stream->content_read;
2676 break;
2678 ret += len;
2679 netconn_stream->content_read += len;
2680 if(blocking_mode != BLOCKING_WAITALL || size == ret)
2681 break;
2684 if(ret || (blocking_mode == BLOCKING_DISALLOW && res == WSAEWOULDBLOCK))
2685 res = ERROR_SUCCESS;
2688 TRACE("read %u bytes\n", ret);
2689 *read = ret;
2690 return res;
2693 static BOOL netconn_drain_content(data_stream_t *stream, http_request_t *req)
2695 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2696 BYTE buf[1024];
2697 int len;
2699 if(netconn_end_of_data(stream, req))
2700 return TRUE;
2702 do {
2703 if(NETCON_recv(req->netconn, buf, sizeof(buf), FALSE, &len) != ERROR_SUCCESS)
2704 return FALSE;
2706 netconn_stream->content_read += len;
2707 }while(netconn_stream->content_read < netconn_stream->content_length);
2709 return TRUE;
2712 static void netconn_destroy(data_stream_t *stream)
2716 static const data_stream_vtbl_t netconn_stream_vtbl = {
2717 netconn_get_avail_data,
2718 netconn_end_of_data,
2719 netconn_read,
2720 netconn_drain_content,
2721 netconn_destroy
2724 /* read some more data into the read buffer (the read section must be held) */
2725 static DWORD read_more_chunked_data(chunked_stream_t *stream, http_request_t *req, int maxlen)
2727 DWORD res;
2728 int len;
2730 assert(!stream->end_of_data);
2732 if (stream->buf_pos)
2734 /* move existing data to the start of the buffer */
2735 if(stream->buf_size)
2736 memmove(stream->buf, stream->buf + stream->buf_pos, stream->buf_size);
2737 stream->buf_pos = 0;
2740 if (maxlen == -1) maxlen = sizeof(stream->buf);
2742 res = NETCON_recv( req->netconn, stream->buf + stream->buf_size,
2743 maxlen - stream->buf_size, TRUE, &len );
2744 if(res == ERROR_SUCCESS)
2745 stream->buf_size += len;
2747 return res;
2750 /* remove some amount of data from the read buffer (the read section must be held) */
2751 static void remove_chunked_data(chunked_stream_t *stream, int count)
2753 if (!(stream->buf_size -= count)) stream->buf_pos = 0;
2754 else stream->buf_pos += count;
2757 /* discard data contents until we reach end of line (the read section must be held) */
2758 static DWORD discard_chunked_eol(chunked_stream_t *stream, http_request_t *req)
2760 DWORD res;
2764 BYTE *eol = memchr(stream->buf + stream->buf_pos, '\n', stream->buf_size);
2765 if (eol)
2767 remove_chunked_data(stream, (eol + 1) - (stream->buf + stream->buf_pos));
2768 break;
2770 stream->buf_pos = stream->buf_size = 0; /* discard everything */
2771 if ((res = read_more_chunked_data(stream, req, -1)) != ERROR_SUCCESS) return res;
2772 } while (stream->buf_size);
2773 return ERROR_SUCCESS;
2776 /* read the size of the next chunk (the read section must be held) */
2777 static DWORD start_next_chunk(chunked_stream_t *stream, http_request_t *req)
2779 DWORD chunk_size = 0, res;
2781 assert(!stream->chunk_size || stream->chunk_size == ~0u);
2783 if (stream->end_of_data) return ERROR_SUCCESS;
2785 /* read terminator for the previous chunk */
2786 if(!stream->chunk_size && (res = discard_chunked_eol(stream, req)) != ERROR_SUCCESS)
2787 return res;
2789 for (;;)
2791 while (stream->buf_size)
2793 char ch = stream->buf[stream->buf_pos];
2794 if (ch >= '0' && ch <= '9') chunk_size = chunk_size * 16 + ch - '0';
2795 else if (ch >= 'a' && ch <= 'f') chunk_size = chunk_size * 16 + ch - 'a' + 10;
2796 else if (ch >= 'A' && ch <= 'F') chunk_size = chunk_size * 16 + ch - 'A' + 10;
2797 else if (ch == ';' || ch == '\r' || ch == '\n')
2799 TRACE( "reading %u byte chunk\n", chunk_size );
2800 stream->chunk_size = chunk_size;
2801 if (req->contentLength == ~0u) req->contentLength = chunk_size;
2802 else req->contentLength += chunk_size;
2804 /* eat the rest of this line */
2805 if ((res = discard_chunked_eol(stream, req)) != ERROR_SUCCESS)
2806 return res;
2808 /* if there's chunk data, return now */
2809 if (chunk_size) return ERROR_SUCCESS;
2811 /* otherwise, eat the terminator for this chunk */
2812 if ((res = discard_chunked_eol(stream, req)) != ERROR_SUCCESS)
2813 return res;
2815 stream->end_of_data = TRUE;
2816 return ERROR_SUCCESS;
2818 remove_chunked_data(stream, 1);
2820 if ((res = read_more_chunked_data(stream, req, -1)) != ERROR_SUCCESS) return res;
2821 if (!stream->buf_size)
2823 stream->chunk_size = 0;
2824 return ERROR_SUCCESS;
2829 static DWORD chunked_get_avail_data(data_stream_t *stream, http_request_t *req)
2831 /* Allow reading only from read buffer */
2832 return 0;
2835 static BOOL chunked_end_of_data(data_stream_t *stream, http_request_t *req)
2837 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2838 return chunked_stream->end_of_data;
2841 static DWORD chunked_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
2842 DWORD *read, blocking_mode_t blocking_mode)
2844 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2845 DWORD read_bytes = 0, ret_read = 0, res = ERROR_SUCCESS;
2847 if(!chunked_stream->chunk_size || chunked_stream->chunk_size == ~0u) {
2848 res = start_next_chunk(chunked_stream, req);
2849 if(res != ERROR_SUCCESS)
2850 return res;
2853 while(size && chunked_stream->chunk_size && !chunked_stream->end_of_data) {
2854 if(chunked_stream->buf_size) {
2855 read_bytes = min(size, min(chunked_stream->buf_size, chunked_stream->chunk_size));
2857 /* this could block */
2858 if(blocking_mode == BLOCKING_DISALLOW && read_bytes == chunked_stream->chunk_size)
2859 break;
2861 memcpy(buf+ret_read, chunked_stream->buf+chunked_stream->buf_pos, read_bytes);
2862 remove_chunked_data(chunked_stream, read_bytes);
2863 }else {
2864 read_bytes = min(size, chunked_stream->chunk_size);
2866 if(blocking_mode == BLOCKING_DISALLOW) {
2867 DWORD avail;
2869 if(!is_valid_netconn(req->netconn) || !NETCON_query_data_available(req->netconn, &avail) || !avail)
2870 break;
2871 if(read_bytes > avail)
2872 read_bytes = avail;
2874 /* this could block */
2875 if(read_bytes == chunked_stream->chunk_size)
2876 break;
2879 res = NETCON_recv(req->netconn, (char *)buf+ret_read, read_bytes, TRUE, (int*)&read_bytes);
2880 if(res != ERROR_SUCCESS)
2881 break;
2884 chunked_stream->chunk_size -= read_bytes;
2885 size -= read_bytes;
2886 ret_read += read_bytes;
2887 if(size && !chunked_stream->chunk_size) {
2888 assert(blocking_mode != BLOCKING_DISALLOW);
2889 res = start_next_chunk(chunked_stream, req);
2890 if(res != ERROR_SUCCESS)
2891 break;
2894 if(blocking_mode == BLOCKING_ALLOW)
2895 blocking_mode = BLOCKING_DISALLOW;
2898 TRACE("read %u bytes\n", ret_read);
2899 *read = ret_read;
2900 return res;
2903 static BOOL chunked_drain_content(data_stream_t *stream, http_request_t *req)
2905 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2907 remove_chunked_data(chunked_stream, chunked_stream->buf_size);
2908 return chunked_stream->end_of_data;
2911 static void chunked_destroy(data_stream_t *stream)
2913 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2914 heap_free(chunked_stream);
2917 static const data_stream_vtbl_t chunked_stream_vtbl = {
2918 chunked_get_avail_data,
2919 chunked_end_of_data,
2920 chunked_read,
2921 chunked_drain_content,
2922 chunked_destroy
2925 /* set the request content length based on the headers */
2926 static DWORD set_content_length(http_request_t *request)
2928 static const WCHAR szChunked[] = {'c','h','u','n','k','e','d',0};
2929 static const WCHAR headW[] = {'H','E','A','D',0};
2930 WCHAR encoding[20];
2931 DWORD size;
2933 if(request->status_code == HTTP_STATUS_NO_CONTENT || !strcmpW(request->verb, headW)) {
2934 request->contentLength = request->netconn_stream.content_length = 0;
2935 return ERROR_SUCCESS;
2938 size = sizeof(request->contentLength);
2939 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
2940 &request->contentLength, &size, NULL) != ERROR_SUCCESS)
2941 request->contentLength = ~0u;
2942 request->netconn_stream.content_length = request->contentLength;
2943 request->netconn_stream.content_read = request->read_size;
2945 size = sizeof(encoding);
2946 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_TRANSFER_ENCODING, encoding, &size, NULL) == ERROR_SUCCESS &&
2947 !strcmpiW(encoding, szChunked))
2949 chunked_stream_t *chunked_stream;
2951 chunked_stream = heap_alloc(sizeof(*chunked_stream));
2952 if(!chunked_stream)
2953 return ERROR_OUTOFMEMORY;
2955 chunked_stream->data_stream.vtbl = &chunked_stream_vtbl;
2956 chunked_stream->buf_size = chunked_stream->buf_pos = 0;
2957 chunked_stream->chunk_size = ~0u;
2958 chunked_stream->end_of_data = FALSE;
2960 if(request->read_size) {
2961 memcpy(chunked_stream->buf, request->read_buf+request->read_pos, request->read_size);
2962 chunked_stream->buf_size = request->read_size;
2963 request->read_size = request->read_pos = 0;
2966 request->data_stream = &chunked_stream->data_stream;
2967 request->contentLength = ~0u;
2970 if(request->decoding) {
2971 int encoding_idx;
2973 static const WCHAR deflateW[] = {'d','e','f','l','a','t','e',0};
2974 static const WCHAR gzipW[] = {'g','z','i','p',0};
2976 EnterCriticalSection( &request->headers_section );
2978 encoding_idx = HTTP_GetCustomHeaderIndex(request, szContent_Encoding, 0, FALSE);
2979 if(encoding_idx != -1) {
2980 if(!strcmpiW(request->custHeaders[encoding_idx].lpszValue, gzipW)) {
2981 HTTP_DeleteCustomHeader(request, encoding_idx);
2982 LeaveCriticalSection( &request->headers_section );
2983 return init_gzip_stream(request, TRUE);
2985 if(!strcmpiW(request->custHeaders[encoding_idx].lpszValue, deflateW)) {
2986 HTTP_DeleteCustomHeader(request, encoding_idx);
2987 LeaveCriticalSection( &request->headers_section );
2988 return init_gzip_stream(request, FALSE);
2992 LeaveCriticalSection( &request->headers_section );
2995 return ERROR_SUCCESS;
2998 static void send_request_complete(http_request_t *req, DWORD_PTR result, DWORD error)
3000 INTERNET_ASYNC_RESULT iar;
3002 iar.dwResult = result;
3003 iar.dwError = error;
3005 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE, &iar,
3006 sizeof(INTERNET_ASYNC_RESULT));
3009 static void HTTP_ReceiveRequestData(http_request_t *req, BOOL first_notif, DWORD *ret_size)
3011 DWORD res, read = 0, avail = 0;
3012 blocking_mode_t mode;
3014 TRACE("%p\n", req);
3016 EnterCriticalSection( &req->read_section );
3018 mode = first_notif && req->read_size ? BLOCKING_DISALLOW : BLOCKING_ALLOW;
3019 res = refill_read_buffer(req, mode, &read);
3020 if(res == ERROR_SUCCESS)
3021 avail = get_avail_data(req);
3023 LeaveCriticalSection( &req->read_section );
3025 if(res != ERROR_SUCCESS || (mode != BLOCKING_DISALLOW && !read)) {
3026 WARN("res %u read %u, closing connection\n", res, read);
3027 http_release_netconn(req, FALSE);
3030 if(res != ERROR_SUCCESS) {
3031 send_request_complete(req, 0, res);
3032 return;
3035 if(ret_size)
3036 *ret_size = avail;
3037 if(first_notif)
3038 avail = 0;
3040 send_request_complete(req, req->session->hdr.dwInternalFlags & INET_OPENURL ? (DWORD_PTR)req->hdr.hInternet : 1, avail);
3043 /* read data from the http connection (the read section must be held) */
3044 static DWORD HTTPREQ_Read(http_request_t *req, void *buffer, DWORD size, DWORD *read, BOOL sync)
3046 DWORD current_read = 0, ret_read = 0;
3047 blocking_mode_t blocking_mode;
3048 DWORD res = ERROR_SUCCESS;
3050 blocking_mode = req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC ? BLOCKING_ALLOW : BLOCKING_WAITALL;
3052 EnterCriticalSection( &req->read_section );
3054 if(req->read_size) {
3055 ret_read = min(size, req->read_size);
3056 memcpy(buffer, req->read_buf+req->read_pos, ret_read);
3057 req->read_size -= ret_read;
3058 req->read_pos += ret_read;
3059 if(blocking_mode == BLOCKING_ALLOW)
3060 blocking_mode = BLOCKING_DISALLOW;
3063 if(ret_read < size) {
3064 res = read_http_stream(req, (BYTE*)buffer+ret_read, size-ret_read, &current_read, blocking_mode);
3065 ret_read += current_read;
3068 LeaveCriticalSection( &req->read_section );
3070 *read = ret_read;
3071 TRACE( "retrieved %u bytes (%u)\n", ret_read, req->contentLength );
3073 if(size && !ret_read)
3074 http_release_netconn(req, res == ERROR_SUCCESS);
3076 return res;
3079 static BOOL drain_content(http_request_t *req, BOOL blocking)
3081 BOOL ret;
3083 if(!is_valid_netconn(req->netconn) || req->contentLength == -1)
3084 return FALSE;
3086 if(!strcmpW(req->verb, szHEAD))
3087 return TRUE;
3089 if(!blocking)
3090 return req->data_stream->vtbl->drain_content(req->data_stream, req);
3092 EnterCriticalSection( &req->read_section );
3094 while(1) {
3095 DWORD bytes_read, res;
3096 BYTE buf[4096];
3098 res = HTTPREQ_Read(req, buf, sizeof(buf), &bytes_read, TRUE);
3099 if(res != ERROR_SUCCESS) {
3100 ret = FALSE;
3101 break;
3103 if(!bytes_read) {
3104 ret = TRUE;
3105 break;
3109 LeaveCriticalSection( &req->read_section );
3110 return ret;
3113 static DWORD HTTPREQ_ReadFile(object_header_t *hdr, void *buffer, DWORD size, DWORD *read)
3115 http_request_t *req = (http_request_t*)hdr;
3116 DWORD res;
3118 EnterCriticalSection( &req->read_section );
3119 if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3120 hdr->dwError = ERROR_INTERNET_INTERNAL_ERROR;
3122 res = HTTPREQ_Read(req, buffer, size, read, TRUE);
3123 if(res == ERROR_SUCCESS)
3124 res = hdr->dwError;
3125 LeaveCriticalSection( &req->read_section );
3127 return res;
3130 typedef struct {
3131 task_header_t hdr;
3132 void *buf;
3133 DWORD size;
3134 DWORD *ret_read;
3135 } read_file_ex_task_t;
3137 static void AsyncReadFileExProc(task_header_t *hdr)
3139 read_file_ex_task_t *task = (read_file_ex_task_t*)hdr;
3140 http_request_t *req = (http_request_t*)task->hdr.hdr;
3141 DWORD res;
3143 TRACE("INTERNETREADFILEEXW %p\n", task->hdr.hdr);
3145 res = HTTPREQ_Read(req, task->buf, task->size, task->ret_read, TRUE);
3146 send_request_complete(req, res == ERROR_SUCCESS, res);
3149 static DWORD HTTPREQ_ReadFileEx(object_header_t *hdr, void *buf, DWORD size, DWORD *ret_read,
3150 DWORD flags, DWORD_PTR context)
3153 http_request_t *req = (http_request_t*)hdr;
3154 DWORD res, read, cread, error = ERROR_SUCCESS;
3156 if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
3157 FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
3159 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3161 if (hdr->dwFlags & INTERNET_FLAG_ASYNC)
3163 read_file_ex_task_t *task;
3165 if (TryEnterCriticalSection( &req->read_section ))
3167 if (get_avail_data(req))
3169 res = HTTPREQ_Read(req, buf, size, &read, FALSE);
3170 LeaveCriticalSection( &req->read_section );
3171 goto done;
3173 LeaveCriticalSection( &req->read_section );
3176 task = alloc_async_task(&req->hdr, AsyncReadFileExProc, sizeof(*task));
3177 task->buf = buf;
3178 task->size = size;
3179 task->ret_read = ret_read;
3181 INTERNET_AsyncCall(&task->hdr);
3183 return ERROR_IO_PENDING;
3186 read = 0;
3188 EnterCriticalSection( &req->read_section );
3189 if(hdr->dwError == ERROR_SUCCESS)
3190 hdr->dwError = INTERNET_HANDLE_IN_USE;
3191 else if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3192 hdr->dwError = ERROR_INTERNET_INTERNAL_ERROR;
3194 while(1) {
3195 res = HTTPREQ_Read(req, (char*)buf+read, size-read, &cread, !(flags & IRF_NO_WAIT));
3196 if(res != ERROR_SUCCESS)
3197 break;
3199 read += cread;
3200 if(read == size || end_of_read_data(req))
3201 break;
3203 LeaveCriticalSection( &req->read_section );
3205 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
3206 &cread, sizeof(cread));
3207 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
3208 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3210 EnterCriticalSection( &req->read_section );
3213 if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3214 hdr->dwError = ERROR_SUCCESS;
3215 else
3216 error = hdr->dwError;
3218 LeaveCriticalSection( &req->read_section );
3220 done:
3221 *ret_read = read;
3222 if (res == ERROR_SUCCESS) {
3223 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
3224 &read, sizeof(read));
3227 return res==ERROR_SUCCESS ? error : res;
3230 static DWORD HTTPREQ_WriteFile(object_header_t *hdr, const void *buffer, DWORD size, DWORD *written)
3232 DWORD res;
3233 http_request_t *request = (http_request_t*)hdr;
3235 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
3237 *written = 0;
3238 res = NETCON_send(request->netconn, buffer, size, 0, (LPINT)written);
3239 if (res == ERROR_SUCCESS)
3240 request->bytesWritten += *written;
3242 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REQUEST_SENT, written, sizeof(DWORD));
3243 return res;
3246 typedef struct {
3247 task_header_t hdr;
3248 DWORD *ret_size;
3249 } http_data_available_task_t;
3251 static void AsyncQueryDataAvailableProc(task_header_t *hdr)
3253 http_data_available_task_t *task = (http_data_available_task_t*)hdr;
3255 HTTP_ReceiveRequestData((http_request_t*)task->hdr.hdr, FALSE, task->ret_size);
3258 static DWORD HTTPREQ_QueryDataAvailable(object_header_t *hdr, DWORD *available, DWORD flags, DWORD_PTR ctx)
3260 http_request_t *req = (http_request_t*)hdr;
3262 TRACE("(%p %p %x %lx)\n", req, available, flags, ctx);
3264 if (req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3266 http_data_available_task_t *task;
3268 /* never wait, if we can't enter the section we queue an async request right away */
3269 if (TryEnterCriticalSection( &req->read_section ))
3271 refill_read_buffer(req, BLOCKING_DISALLOW, NULL);
3272 if ((*available = get_avail_data( req ))) goto done;
3273 if (end_of_read_data( req )) goto done;
3274 LeaveCriticalSection( &req->read_section );
3277 task = alloc_async_task(&req->hdr, AsyncQueryDataAvailableProc, sizeof(*task));
3278 task->ret_size = available;
3279 INTERNET_AsyncCall(&task->hdr);
3280 return ERROR_IO_PENDING;
3283 EnterCriticalSection( &req->read_section );
3285 if (!(*available = get_avail_data( req )) && !end_of_read_data( req ))
3287 refill_read_buffer( req, BLOCKING_ALLOW, NULL );
3288 *available = get_avail_data( req );
3291 done:
3292 LeaveCriticalSection( &req->read_section );
3294 TRACE( "returning %u\n", *available );
3295 return ERROR_SUCCESS;
3298 static DWORD HTTPREQ_LockRequestFile(object_header_t *hdr, req_file_t **ret)
3300 http_request_t *req = (http_request_t*)hdr;
3302 TRACE("(%p)\n", req);
3304 if(!req->req_file) {
3305 WARN("No cache file name available\n");
3306 return ERROR_FILE_NOT_FOUND;
3309 *ret = req_file_addref(req->req_file);
3310 return ERROR_SUCCESS;
3313 static const object_vtbl_t HTTPREQVtbl = {
3314 HTTPREQ_Destroy,
3315 HTTPREQ_CloseConnection,
3316 HTTPREQ_QueryOption,
3317 HTTPREQ_SetOption,
3318 HTTPREQ_ReadFile,
3319 HTTPREQ_ReadFileEx,
3320 HTTPREQ_WriteFile,
3321 HTTPREQ_QueryDataAvailable,
3322 NULL,
3323 HTTPREQ_LockRequestFile
3326 /***********************************************************************
3327 * HTTP_HttpOpenRequestW (internal)
3329 * Open a HTTP request handle
3331 * RETURNS
3332 * HINTERNET a HTTP request handle on success
3333 * NULL on failure
3336 static DWORD HTTP_HttpOpenRequestW(http_session_t *session,
3337 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
3338 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
3339 DWORD dwFlags, DWORD_PTR dwContext, HINTERNET *ret)
3341 appinfo_t *hIC = session->appInfo;
3342 http_request_t *request;
3343 DWORD len;
3345 TRACE("-->\n");
3347 request = alloc_object(&session->hdr, &HTTPREQVtbl, sizeof(http_request_t));
3348 if(!request)
3349 return ERROR_OUTOFMEMORY;
3351 request->hdr.htype = WH_HHTTPREQ;
3352 request->hdr.dwFlags = dwFlags;
3353 request->hdr.dwContext = dwContext;
3354 request->contentLength = ~0u;
3356 request->netconn_stream.data_stream.vtbl = &netconn_stream_vtbl;
3357 request->data_stream = &request->netconn_stream.data_stream;
3358 request->connect_timeout = session->connect_timeout;
3359 request->send_timeout = session->send_timeout;
3360 request->receive_timeout = session->receive_timeout;
3362 InitializeCriticalSection( &request->headers_section );
3363 request->headers_section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": http_request_t.headers_section");
3365 InitializeCriticalSection( &request->read_section );
3366 request->read_section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": http_request_t.read_section");
3368 WININET_AddRef( &session->hdr );
3369 request->session = session;
3370 list_add_head( &session->hdr.children, &request->hdr.entry );
3372 request->server = get_server(session->hostName, session->hostPort, (dwFlags & INTERNET_FLAG_SECURE) != 0, TRUE);
3373 if(!request->server) {
3374 WININET_Release(&request->hdr);
3375 return ERROR_OUTOFMEMORY;
3378 if (dwFlags & INTERNET_FLAG_IGNORE_CERT_CN_INVALID)
3379 request->security_flags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
3380 if (dwFlags & INTERNET_FLAG_IGNORE_CERT_DATE_INVALID)
3381 request->security_flags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
3383 if (lpszObjectName && *lpszObjectName) {
3384 HRESULT rc;
3386 len = 0;
3387 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
3388 if (rc != E_POINTER)
3389 len = strlenW(lpszObjectName)+1;
3390 request->path = heap_alloc(len*sizeof(WCHAR));
3391 rc = UrlEscapeW(lpszObjectName, request->path, &len,
3392 URL_ESCAPE_SPACES_ONLY);
3393 if (rc != S_OK)
3395 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc);
3396 strcpyW(request->path,lpszObjectName);
3398 }else {
3399 static const WCHAR slashW[] = {'/',0};
3401 request->path = heap_strdupW(slashW);
3404 if (lpszReferrer && *lpszReferrer)
3405 HTTP_ProcessHeader(request, HTTP_REFERER, lpszReferrer, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
3407 if (lpszAcceptTypes)
3409 int i;
3410 for (i = 0; lpszAcceptTypes[i]; i++)
3412 if (!*lpszAcceptTypes[i]) continue;
3413 HTTP_ProcessHeader(request, HTTP_ACCEPT, lpszAcceptTypes[i],
3414 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA |
3415 HTTP_ADDHDR_FLAG_REQ |
3416 (i == 0 ? HTTP_ADDHDR_FLAG_REPLACE : 0));
3420 request->verb = heap_strdupW(lpszVerb && *lpszVerb ? lpszVerb : szGET);
3421 request->version = heap_strdupW(lpszVersion && *lpszVersion ? lpszVersion : g_szHttp1_1);
3423 HTTP_ProcessHeader(request, hostW, request->server->canon_host_port, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
3425 if (hIC->proxy && hIC->proxy[0] && !HTTP_ShouldBypassProxy(hIC, session->hostName))
3426 HTTP_DealWithProxy( hIC, session, request );
3428 INTERNET_SendCallback(&session->hdr, dwContext,
3429 INTERNET_STATUS_HANDLE_CREATED, &request->hdr.hInternet,
3430 sizeof(HINTERNET));
3432 TRACE("<-- (%p)\n", request);
3434 *ret = request->hdr.hInternet;
3435 return ERROR_SUCCESS;
3438 /***********************************************************************
3439 * HttpOpenRequestW (WININET.@)
3441 * Open a HTTP request handle
3443 * RETURNS
3444 * HINTERNET a HTTP request handle on success
3445 * NULL on failure
3448 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
3449 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
3450 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
3451 DWORD dwFlags, DWORD_PTR dwContext)
3453 http_session_t *session;
3454 HINTERNET handle = NULL;
3455 DWORD res;
3457 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
3458 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
3459 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
3460 dwFlags, dwContext);
3461 if(lpszAcceptTypes!=NULL)
3463 int i;
3464 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
3465 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
3468 session = (http_session_t*) get_handle_object( hHttpSession );
3469 if (NULL == session || session->hdr.htype != WH_HHTTPSESSION)
3471 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3472 goto lend;
3476 * My tests seem to show that the windows version does not
3477 * become asynchronous until after this point. And anyhow
3478 * if this call was asynchronous then how would you get the
3479 * necessary HINTERNET pointer returned by this function.
3482 res = HTTP_HttpOpenRequestW(session, lpszVerb, lpszObjectName,
3483 lpszVersion, lpszReferrer, lpszAcceptTypes,
3484 dwFlags, dwContext, &handle);
3485 lend:
3486 if( session )
3487 WININET_Release( &session->hdr );
3488 TRACE("returning %p\n", handle);
3489 if(res != ERROR_SUCCESS)
3490 SetLastError(res);
3491 return handle;
3494 static const LPCWSTR header_lookup[] = {
3495 szMime_Version, /* HTTP_QUERY_MIME_VERSION = 0 */
3496 szContent_Type, /* HTTP_QUERY_CONTENT_TYPE = 1 */
3497 szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
3498 szContent_ID, /* HTTP_QUERY_CONTENT_ID = 3 */
3499 NULL, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
3500 szContent_Length, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
3501 szContent_Language, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
3502 szAllow, /* HTTP_QUERY_ALLOW = 7 */
3503 szPublic, /* HTTP_QUERY_PUBLIC = 8 */
3504 szDate, /* HTTP_QUERY_DATE = 9 */
3505 szExpires, /* HTTP_QUERY_EXPIRES = 10 */
3506 szLast_Modified, /* HTTP_QUERY_LAST_MODIFIED = 11 */
3507 NULL, /* HTTP_QUERY_MESSAGE_ID = 12 */
3508 szURI, /* HTTP_QUERY_URI = 13 */
3509 szFrom, /* HTTP_QUERY_DERIVED_FROM = 14 */
3510 NULL, /* HTTP_QUERY_COST = 15 */
3511 NULL, /* HTTP_QUERY_LINK = 16 */
3512 szPragma, /* HTTP_QUERY_PRAGMA = 17 */
3513 NULL, /* HTTP_QUERY_VERSION = 18 */
3514 szStatus, /* HTTP_QUERY_STATUS_CODE = 19 */
3515 NULL, /* HTTP_QUERY_STATUS_TEXT = 20 */
3516 NULL, /* HTTP_QUERY_RAW_HEADERS = 21 */
3517 NULL, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
3518 szConnection, /* HTTP_QUERY_CONNECTION = 23 */
3519 szAccept, /* HTTP_QUERY_ACCEPT = 24 */
3520 szAccept_Charset, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
3521 szAccept_Encoding, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
3522 szAccept_Language, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
3523 szAuthorization, /* HTTP_QUERY_AUTHORIZATION = 28 */
3524 szContent_Encoding, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
3525 NULL, /* HTTP_QUERY_FORWARDED = 30 */
3526 NULL, /* HTTP_QUERY_FROM = 31 */
3527 szIf_Modified_Since, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
3528 szLocation, /* HTTP_QUERY_LOCATION = 33 */
3529 NULL, /* HTTP_QUERY_ORIG_URI = 34 */
3530 szReferer, /* HTTP_QUERY_REFERER = 35 */
3531 szRetry_After, /* HTTP_QUERY_RETRY_AFTER = 36 */
3532 szServer, /* HTTP_QUERY_SERVER = 37 */
3533 NULL, /* HTTP_TITLE = 38 */
3534 szUser_Agent, /* HTTP_QUERY_USER_AGENT = 39 */
3535 szWWW_Authenticate, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
3536 szProxy_Authenticate, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
3537 szAccept_Ranges, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
3538 szSet_Cookie, /* HTTP_QUERY_SET_COOKIE = 43 */
3539 szCookie, /* HTTP_QUERY_COOKIE = 44 */
3540 NULL, /* HTTP_QUERY_REQUEST_METHOD = 45 */
3541 NULL, /* HTTP_QUERY_REFRESH = 46 */
3542 szContent_Disposition, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
3543 szAge, /* HTTP_QUERY_AGE = 48 */
3544 szCache_Control, /* HTTP_QUERY_CACHE_CONTROL = 49 */
3545 szContent_Base, /* HTTP_QUERY_CONTENT_BASE = 50 */
3546 szContent_Location, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
3547 szContent_MD5, /* HTTP_QUERY_CONTENT_MD5 = 52 */
3548 szContent_Range, /* HTTP_QUERY_CONTENT_RANGE = 53 */
3549 szETag, /* HTTP_QUERY_ETAG = 54 */
3550 hostW, /* HTTP_QUERY_HOST = 55 */
3551 szIf_Match, /* HTTP_QUERY_IF_MATCH = 56 */
3552 szIf_None_Match, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
3553 szIf_Range, /* HTTP_QUERY_IF_RANGE = 58 */
3554 szIf_Unmodified_Since, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
3555 szMax_Forwards, /* HTTP_QUERY_MAX_FORWARDS = 60 */
3556 szProxy_Authorization, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
3557 szRange, /* HTTP_QUERY_RANGE = 62 */
3558 szTransfer_Encoding, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
3559 szUpgrade, /* HTTP_QUERY_UPGRADE = 64 */
3560 szVary, /* HTTP_QUERY_VARY = 65 */
3561 szVia, /* HTTP_QUERY_VIA = 66 */
3562 szWarning, /* HTTP_QUERY_WARNING = 67 */
3563 szExpect, /* HTTP_QUERY_EXPECT = 68 */
3564 szProxy_Connection, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
3565 szUnless_Modified_Since, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
3568 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
3570 /***********************************************************************
3571 * HTTP_HttpQueryInfoW (internal)
3573 static DWORD HTTP_HttpQueryInfoW(http_request_t *request, DWORD dwInfoLevel,
3574 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
3576 LPHTTPHEADERW lphttpHdr = NULL;
3577 BOOL request_only = !!(dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS);
3578 INT requested_index = lpdwIndex ? *lpdwIndex : 0;
3579 DWORD level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
3580 INT index = -1;
3582 EnterCriticalSection( &request->headers_section );
3584 /* Find requested header structure */
3585 switch (level)
3587 case HTTP_QUERY_CUSTOM:
3588 if (!lpBuffer)
3590 LeaveCriticalSection( &request->headers_section );
3591 return ERROR_INVALID_PARAMETER;
3593 index = HTTP_GetCustomHeaderIndex(request, lpBuffer, requested_index, request_only);
3594 break;
3595 case HTTP_QUERY_RAW_HEADERS_CRLF:
3597 LPWSTR headers;
3598 DWORD len = 0;
3599 DWORD res = ERROR_INVALID_PARAMETER;
3601 if (request_only)
3602 headers = build_request_header(request, request->verb, request->path, request->version, TRUE);
3603 else
3604 headers = build_response_header(request, TRUE);
3605 if (!headers)
3607 LeaveCriticalSection( &request->headers_section );
3608 return ERROR_OUTOFMEMORY;
3611 len = strlenW(headers) * sizeof(WCHAR);
3612 if (len + sizeof(WCHAR) > *lpdwBufferLength)
3614 len += sizeof(WCHAR);
3615 res = ERROR_INSUFFICIENT_BUFFER;
3617 else if (lpBuffer)
3619 memcpy(lpBuffer, headers, len + sizeof(WCHAR));
3620 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len / sizeof(WCHAR)));
3621 res = ERROR_SUCCESS;
3623 *lpdwBufferLength = len;
3625 heap_free(headers);
3626 LeaveCriticalSection( &request->headers_section );
3627 return res;
3629 case HTTP_QUERY_RAW_HEADERS:
3631 LPWSTR headers;
3632 DWORD len;
3634 if (request_only)
3635 headers = build_request_header(request, request->verb, request->path, request->version, FALSE);
3636 else
3637 headers = build_response_header(request, FALSE);
3639 if (!headers)
3641 LeaveCriticalSection( &request->headers_section );
3642 return ERROR_OUTOFMEMORY;
3645 len = strlenW(headers) * sizeof(WCHAR);
3646 if (len > *lpdwBufferLength)
3648 *lpdwBufferLength = len;
3649 heap_free(headers);
3650 LeaveCriticalSection( &request->headers_section );
3651 return ERROR_INSUFFICIENT_BUFFER;
3654 if (lpBuffer)
3656 DWORD i;
3658 TRACE("returning data: %s\n", debugstr_wn(headers, len / sizeof(WCHAR)));
3660 for (i = 0; i < len / sizeof(WCHAR); i++)
3662 if (headers[i] == '\n')
3663 headers[i] = 0;
3665 memcpy(lpBuffer, headers, len);
3667 *lpdwBufferLength = len - sizeof(WCHAR);
3669 heap_free(headers);
3670 LeaveCriticalSection( &request->headers_section );
3671 return ERROR_SUCCESS;
3673 case HTTP_QUERY_STATUS_TEXT:
3674 if (request->statusText)
3676 DWORD len = strlenW(request->statusText);
3677 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
3679 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
3680 LeaveCriticalSection( &request->headers_section );
3681 return ERROR_INSUFFICIENT_BUFFER;
3683 if (lpBuffer)
3685 memcpy(lpBuffer, request->statusText, (len + 1) * sizeof(WCHAR));
3686 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
3688 *lpdwBufferLength = len * sizeof(WCHAR);
3689 LeaveCriticalSection( &request->headers_section );
3690 return ERROR_SUCCESS;
3692 break;
3693 case HTTP_QUERY_VERSION:
3694 if (request->version)
3696 DWORD len = strlenW(request->version);
3697 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
3699 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
3700 LeaveCriticalSection( &request->headers_section );
3701 return ERROR_INSUFFICIENT_BUFFER;
3703 if (lpBuffer)
3705 memcpy(lpBuffer, request->version, (len + 1) * sizeof(WCHAR));
3706 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
3708 *lpdwBufferLength = len * sizeof(WCHAR);
3709 LeaveCriticalSection( &request->headers_section );
3710 return ERROR_SUCCESS;
3712 break;
3713 case HTTP_QUERY_CONTENT_ENCODING:
3714 index = HTTP_GetCustomHeaderIndex(request, header_lookup[request->read_gzip ? HTTP_QUERY_CONTENT_TYPE : level],
3715 requested_index,request_only);
3716 break;
3717 case HTTP_QUERY_STATUS_CODE: {
3718 DWORD res = ERROR_SUCCESS;
3720 if(request_only)
3722 LeaveCriticalSection( &request->headers_section );
3723 return ERROR_HTTP_INVALID_QUERY_REQUEST;
3726 if(requested_index)
3727 break;
3729 if(dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) {
3730 if(*lpdwBufferLength >= sizeof(DWORD))
3731 *(DWORD*)lpBuffer = request->status_code;
3732 else
3733 res = ERROR_INSUFFICIENT_BUFFER;
3734 *lpdwBufferLength = sizeof(DWORD);
3735 }else {
3736 WCHAR buf[12];
3737 DWORD size;
3738 static const WCHAR formatW[] = {'%','u',0};
3740 size = sprintfW(buf, formatW, request->status_code) * sizeof(WCHAR);
3742 if(size <= *lpdwBufferLength) {
3743 memcpy(lpBuffer, buf, size+sizeof(WCHAR));
3744 }else {
3745 size += sizeof(WCHAR);
3746 res = ERROR_INSUFFICIENT_BUFFER;
3749 *lpdwBufferLength = size;
3751 LeaveCriticalSection( &request->headers_section );
3752 return res;
3754 default:
3755 assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
3757 if (level < LAST_TABLE_HEADER && header_lookup[level])
3758 index = HTTP_GetCustomHeaderIndex(request, header_lookup[level],
3759 requested_index,request_only);
3762 if (index >= 0)
3763 lphttpHdr = &request->custHeaders[index];
3765 /* Ensure header satisfies requested attributes */
3766 if (!lphttpHdr ||
3767 ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
3768 (~lphttpHdr->wFlags & HDR_ISREQUEST)))
3770 LeaveCriticalSection( &request->headers_section );
3771 return ERROR_HTTP_HEADER_NOT_FOUND;
3774 /* coalesce value to requested type */
3775 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER && lpBuffer)
3777 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
3778 TRACE(" returning number: %d\n", *(int *)lpBuffer);
3780 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME && lpBuffer)
3782 time_t tmpTime;
3783 struct tm tmpTM;
3784 SYSTEMTIME *STHook;
3786 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
3788 tmpTM = *gmtime(&tmpTime);
3789 STHook = (SYSTEMTIME *)lpBuffer;
3790 STHook->wDay = tmpTM.tm_mday;
3791 STHook->wHour = tmpTM.tm_hour;
3792 STHook->wMilliseconds = 0;
3793 STHook->wMinute = tmpTM.tm_min;
3794 STHook->wDayOfWeek = tmpTM.tm_wday;
3795 STHook->wMonth = tmpTM.tm_mon + 1;
3796 STHook->wSecond = tmpTM.tm_sec;
3797 STHook->wYear = 1900+tmpTM.tm_year;
3799 TRACE(" returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
3800 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
3801 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
3803 else if (lphttpHdr->lpszValue)
3805 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
3807 if (len > *lpdwBufferLength)
3809 *lpdwBufferLength = len;
3810 LeaveCriticalSection( &request->headers_section );
3811 return ERROR_INSUFFICIENT_BUFFER;
3813 if (lpBuffer)
3815 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
3816 TRACE("! returning string: %s\n", debugstr_w(lpBuffer));
3818 *lpdwBufferLength = len - sizeof(WCHAR);
3820 if (lpdwIndex) (*lpdwIndex)++;
3822 LeaveCriticalSection( &request->headers_section );
3823 return ERROR_SUCCESS;
3826 /***********************************************************************
3827 * HttpQueryInfoW (WININET.@)
3829 * Queries for information about an HTTP request
3831 * RETURNS
3832 * TRUE on success
3833 * FALSE on failure
3836 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
3837 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
3839 http_request_t *request;
3840 DWORD res;
3842 if (TRACE_ON(wininet)) {
3843 #define FE(x) { x, #x }
3844 static const wininet_flag_info query_flags[] = {
3845 FE(HTTP_QUERY_MIME_VERSION),
3846 FE(HTTP_QUERY_CONTENT_TYPE),
3847 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
3848 FE(HTTP_QUERY_CONTENT_ID),
3849 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
3850 FE(HTTP_QUERY_CONTENT_LENGTH),
3851 FE(HTTP_QUERY_CONTENT_LANGUAGE),
3852 FE(HTTP_QUERY_ALLOW),
3853 FE(HTTP_QUERY_PUBLIC),
3854 FE(HTTP_QUERY_DATE),
3855 FE(HTTP_QUERY_EXPIRES),
3856 FE(HTTP_QUERY_LAST_MODIFIED),
3857 FE(HTTP_QUERY_MESSAGE_ID),
3858 FE(HTTP_QUERY_URI),
3859 FE(HTTP_QUERY_DERIVED_FROM),
3860 FE(HTTP_QUERY_COST),
3861 FE(HTTP_QUERY_LINK),
3862 FE(HTTP_QUERY_PRAGMA),
3863 FE(HTTP_QUERY_VERSION),
3864 FE(HTTP_QUERY_STATUS_CODE),
3865 FE(HTTP_QUERY_STATUS_TEXT),
3866 FE(HTTP_QUERY_RAW_HEADERS),
3867 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
3868 FE(HTTP_QUERY_CONNECTION),
3869 FE(HTTP_QUERY_ACCEPT),
3870 FE(HTTP_QUERY_ACCEPT_CHARSET),
3871 FE(HTTP_QUERY_ACCEPT_ENCODING),
3872 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
3873 FE(HTTP_QUERY_AUTHORIZATION),
3874 FE(HTTP_QUERY_CONTENT_ENCODING),
3875 FE(HTTP_QUERY_FORWARDED),
3876 FE(HTTP_QUERY_FROM),
3877 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
3878 FE(HTTP_QUERY_LOCATION),
3879 FE(HTTP_QUERY_ORIG_URI),
3880 FE(HTTP_QUERY_REFERER),
3881 FE(HTTP_QUERY_RETRY_AFTER),
3882 FE(HTTP_QUERY_SERVER),
3883 FE(HTTP_QUERY_TITLE),
3884 FE(HTTP_QUERY_USER_AGENT),
3885 FE(HTTP_QUERY_WWW_AUTHENTICATE),
3886 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
3887 FE(HTTP_QUERY_ACCEPT_RANGES),
3888 FE(HTTP_QUERY_SET_COOKIE),
3889 FE(HTTP_QUERY_COOKIE),
3890 FE(HTTP_QUERY_REQUEST_METHOD),
3891 FE(HTTP_QUERY_REFRESH),
3892 FE(HTTP_QUERY_CONTENT_DISPOSITION),
3893 FE(HTTP_QUERY_AGE),
3894 FE(HTTP_QUERY_CACHE_CONTROL),
3895 FE(HTTP_QUERY_CONTENT_BASE),
3896 FE(HTTP_QUERY_CONTENT_LOCATION),
3897 FE(HTTP_QUERY_CONTENT_MD5),
3898 FE(HTTP_QUERY_CONTENT_RANGE),
3899 FE(HTTP_QUERY_ETAG),
3900 FE(HTTP_QUERY_HOST),
3901 FE(HTTP_QUERY_IF_MATCH),
3902 FE(HTTP_QUERY_IF_NONE_MATCH),
3903 FE(HTTP_QUERY_IF_RANGE),
3904 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
3905 FE(HTTP_QUERY_MAX_FORWARDS),
3906 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
3907 FE(HTTP_QUERY_RANGE),
3908 FE(HTTP_QUERY_TRANSFER_ENCODING),
3909 FE(HTTP_QUERY_UPGRADE),
3910 FE(HTTP_QUERY_VARY),
3911 FE(HTTP_QUERY_VIA),
3912 FE(HTTP_QUERY_WARNING),
3913 FE(HTTP_QUERY_CUSTOM)
3915 static const wininet_flag_info modifier_flags[] = {
3916 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
3917 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
3918 FE(HTTP_QUERY_FLAG_NUMBER),
3919 FE(HTTP_QUERY_FLAG_COALESCE)
3921 #undef FE
3922 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
3923 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
3924 DWORD i;
3926 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, info);
3927 TRACE(" Attribute:");
3928 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
3929 if (query_flags[i].val == info) {
3930 TRACE(" %s", query_flags[i].name);
3931 break;
3934 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
3935 TRACE(" Unknown (%08x)", info);
3938 TRACE(" Modifier:");
3939 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
3940 if (modifier_flags[i].val & info_mod) {
3941 TRACE(" %s", modifier_flags[i].name);
3942 info_mod &= ~ modifier_flags[i].val;
3946 if (info_mod) {
3947 TRACE(" Unknown (%08x)", info_mod);
3949 TRACE("\n");
3952 request = (http_request_t*) get_handle_object( hHttpRequest );
3953 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
3955 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3956 goto lend;
3959 if (lpBuffer == NULL)
3960 *lpdwBufferLength = 0;
3961 res = HTTP_HttpQueryInfoW( request, dwInfoLevel,
3962 lpBuffer, lpdwBufferLength, lpdwIndex);
3964 lend:
3965 if( request )
3966 WININET_Release( &request->hdr );
3968 TRACE("%u <--\n", res);
3969 if(res != ERROR_SUCCESS)
3970 SetLastError(res);
3971 return res == ERROR_SUCCESS;
3974 /***********************************************************************
3975 * HttpQueryInfoA (WININET.@)
3977 * Queries for information about an HTTP request
3979 * RETURNS
3980 * TRUE on success
3981 * FALSE on failure
3984 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
3985 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
3987 BOOL result;
3988 DWORD len;
3989 WCHAR* bufferW;
3991 TRACE("%p %x\n", hHttpRequest, dwInfoLevel);
3993 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
3994 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
3996 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
3997 lpdwBufferLength, lpdwIndex );
4000 if (lpBuffer)
4002 DWORD alloclen;
4003 len = (*lpdwBufferLength)*sizeof(WCHAR);
4004 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
4006 alloclen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 ) * sizeof(WCHAR);
4007 if (alloclen < len)
4008 alloclen = len;
4010 else
4011 alloclen = len;
4012 bufferW = heap_alloc(alloclen);
4013 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
4014 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
4015 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, bufferW, alloclen / sizeof(WCHAR) );
4016 } else
4018 bufferW = NULL;
4019 len = 0;
4022 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
4023 &len, lpdwIndex );
4024 if( result )
4026 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
4027 lpBuffer, *lpdwBufferLength, NULL, NULL );
4028 *lpdwBufferLength = len - 1;
4030 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
4032 else
4033 /* since the strings being returned from HttpQueryInfoW should be
4034 * only ASCII characters, it is reasonable to assume that all of
4035 * the Unicode characters can be reduced to a single byte */
4036 *lpdwBufferLength = len / sizeof(WCHAR);
4038 heap_free( bufferW );
4039 return result;
4042 /***********************************************************************
4043 * HTTP_GetRedirectURL (internal)
4045 static LPWSTR HTTP_GetRedirectURL(http_request_t *request, LPCWSTR lpszUrl)
4047 static WCHAR szHttp[] = {'h','t','t','p',0};
4048 static WCHAR szHttps[] = {'h','t','t','p','s',0};
4049 http_session_t *session = request->session;
4050 URL_COMPONENTSW urlComponents;
4051 DWORD url_length = 0;
4052 LPWSTR orig_url;
4053 LPWSTR combined_url;
4055 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
4056 urlComponents.lpszScheme = (request->hdr.dwFlags & INTERNET_FLAG_SECURE) ? szHttps : szHttp;
4057 urlComponents.dwSchemeLength = 0;
4058 urlComponents.lpszHostName = request->server->name;
4059 urlComponents.dwHostNameLength = 0;
4060 urlComponents.nPort = request->server->port;
4061 urlComponents.lpszUserName = session->userName;
4062 urlComponents.dwUserNameLength = 0;
4063 urlComponents.lpszPassword = NULL;
4064 urlComponents.dwPasswordLength = 0;
4065 urlComponents.lpszUrlPath = request->path;
4066 urlComponents.dwUrlPathLength = 0;
4067 urlComponents.lpszExtraInfo = NULL;
4068 urlComponents.dwExtraInfoLength = 0;
4070 if (!InternetCreateUrlW(&urlComponents, 0, NULL, &url_length) &&
4071 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
4072 return NULL;
4074 orig_url = heap_alloc(url_length);
4076 /* convert from bytes to characters */
4077 url_length = url_length / sizeof(WCHAR) - 1;
4078 if (!InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length))
4080 heap_free(orig_url);
4081 return NULL;
4084 url_length = 0;
4085 if (!InternetCombineUrlW(orig_url, lpszUrl, NULL, &url_length, ICU_ENCODE_SPACES_ONLY) &&
4086 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
4088 heap_free(orig_url);
4089 return NULL;
4091 combined_url = heap_alloc(url_length * sizeof(WCHAR));
4093 if (!InternetCombineUrlW(orig_url, lpszUrl, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY))
4095 heap_free(orig_url);
4096 heap_free(combined_url);
4097 return NULL;
4099 heap_free(orig_url);
4100 return combined_url;
4104 /***********************************************************************
4105 * HTTP_HandleRedirect (internal)
4107 static DWORD HTTP_HandleRedirect(http_request_t *request, LPCWSTR lpszUrl)
4109 http_session_t *session = request->session;
4110 WCHAR path[INTERNET_MAX_PATH_LENGTH];
4112 if(lpszUrl[0]=='/')
4114 /* if it's an absolute path, keep the same session info */
4115 lstrcpynW(path, lpszUrl, INTERNET_MAX_URL_LENGTH);
4117 else
4119 URL_COMPONENTSW urlComponents;
4120 WCHAR protocol[INTERNET_MAX_SCHEME_LENGTH];
4121 WCHAR hostName[INTERNET_MAX_HOST_NAME_LENGTH];
4122 WCHAR userName[INTERNET_MAX_USER_NAME_LENGTH];
4123 BOOL custom_port = FALSE;
4125 static const WCHAR httpW[] = {'h','t','t','p',0};
4126 static const WCHAR httpsW[] = {'h','t','t','p','s',0};
4128 userName[0] = 0;
4129 hostName[0] = 0;
4130 protocol[0] = 0;
4132 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
4133 urlComponents.lpszScheme = protocol;
4134 urlComponents.dwSchemeLength = INTERNET_MAX_SCHEME_LENGTH;
4135 urlComponents.lpszHostName = hostName;
4136 urlComponents.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH;
4137 urlComponents.lpszUserName = userName;
4138 urlComponents.dwUserNameLength = INTERNET_MAX_USER_NAME_LENGTH;
4139 urlComponents.lpszPassword = NULL;
4140 urlComponents.dwPasswordLength = 0;
4141 urlComponents.lpszUrlPath = path;
4142 urlComponents.dwUrlPathLength = INTERNET_MAX_PATH_LENGTH;
4143 urlComponents.lpszExtraInfo = NULL;
4144 urlComponents.dwExtraInfoLength = 0;
4145 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
4146 return INTERNET_GetLastError();
4148 if(!strcmpiW(protocol, httpW)) {
4149 if(request->hdr.dwFlags & INTERNET_FLAG_SECURE) {
4150 TRACE("redirect from secure page to non-secure page\n");
4151 /* FIXME: warn about from secure redirect to non-secure page */
4152 request->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
4155 if(urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
4156 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
4157 else if(urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT)
4158 custom_port = TRUE;
4159 }else if(!strcmpiW(protocol, httpsW)) {
4160 if(!(request->hdr.dwFlags & INTERNET_FLAG_SECURE)) {
4161 TRACE("redirect from non-secure page to secure page\n");
4162 /* FIXME: notify about redirect to secure page */
4163 request->hdr.dwFlags |= INTERNET_FLAG_SECURE;
4166 if(urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
4167 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
4168 else if(urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT)
4169 custom_port = TRUE;
4172 heap_free(session->hostName);
4174 session->hostName = heap_strdupW(hostName);
4175 session->hostPort = urlComponents.nPort;
4177 heap_free(session->userName);
4178 session->userName = NULL;
4179 if (userName[0])
4180 session->userName = heap_strdupW(userName);
4182 reset_data_stream(request);
4184 if(strcmpiW(request->server->name, hostName) || request->server->port != urlComponents.nPort) {
4185 server_t *new_server;
4187 new_server = get_server(hostName, urlComponents.nPort, urlComponents.nScheme == INTERNET_SCHEME_HTTPS, TRUE);
4188 server_release(request->server);
4189 request->server = new_server;
4192 if (custom_port)
4193 HTTP_ProcessHeader(request, hostW, request->server->host_port, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
4194 else
4195 HTTP_ProcessHeader(request, hostW, request->server->name, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
4197 heap_free(request->path);
4198 request->path=NULL;
4199 if (*path)
4201 DWORD needed = 0;
4202 HRESULT rc;
4204 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
4205 if (rc != E_POINTER)
4206 needed = strlenW(path)+1;
4207 request->path = heap_alloc(needed*sizeof(WCHAR));
4208 rc = UrlEscapeW(path, request->path, &needed,
4209 URL_ESCAPE_SPACES_ONLY);
4210 if (rc != S_OK)
4212 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
4213 strcpyW(request->path,path);
4217 /* Remove custom content-type/length headers on redirects. */
4218 remove_header(request, szContent_Type, TRUE);
4219 remove_header(request, szContent_Length, TRUE);
4221 return ERROR_SUCCESS;
4224 /***********************************************************************
4225 * HTTP_build_req (internal)
4227 * concatenate all the strings in the request together
4229 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
4231 LPCWSTR *t;
4232 LPWSTR str;
4234 for( t = list; *t ; t++ )
4235 len += strlenW( *t );
4236 len++;
4238 str = heap_alloc(len*sizeof(WCHAR));
4239 *str = 0;
4241 for( t = list; *t ; t++ )
4242 strcatW( str, *t );
4244 return str;
4247 static void HTTP_InsertCookies(http_request_t *request)
4249 WCHAR *cookies;
4250 DWORD res;
4252 res = get_cookie_header(request->server->name, request->path, &cookies);
4253 if(res != ERROR_SUCCESS || !cookies)
4254 return;
4256 HTTP_HttpAddRequestHeadersW(request, cookies, strlenW(cookies), HTTP_ADDREQ_FLAG_REPLACE);
4257 heap_free(cookies);
4260 static WORD HTTP_ParseWkday(LPCWSTR day)
4262 static const WCHAR days[7][4] = {{ 's','u','n',0 },
4263 { 'm','o','n',0 },
4264 { 't','u','e',0 },
4265 { 'w','e','d',0 },
4266 { 't','h','u',0 },
4267 { 'f','r','i',0 },
4268 { 's','a','t',0 }};
4269 unsigned int i;
4270 for (i = 0; i < sizeof(days)/sizeof(*days); i++)
4271 if (!strcmpiW(day, days[i]))
4272 return i;
4274 /* Invalid */
4275 return 7;
4278 static WORD HTTP_ParseMonth(LPCWSTR month)
4280 static const WCHAR jan[] = { 'j','a','n',0 };
4281 static const WCHAR feb[] = { 'f','e','b',0 };
4282 static const WCHAR mar[] = { 'm','a','r',0 };
4283 static const WCHAR apr[] = { 'a','p','r',0 };
4284 static const WCHAR may[] = { 'm','a','y',0 };
4285 static const WCHAR jun[] = { 'j','u','n',0 };
4286 static const WCHAR jul[] = { 'j','u','l',0 };
4287 static const WCHAR aug[] = { 'a','u','g',0 };
4288 static const WCHAR sep[] = { 's','e','p',0 };
4289 static const WCHAR oct[] = { 'o','c','t',0 };
4290 static const WCHAR nov[] = { 'n','o','v',0 };
4291 static const WCHAR dec[] = { 'd','e','c',0 };
4293 if (!strcmpiW(month, jan)) return 1;
4294 if (!strcmpiW(month, feb)) return 2;
4295 if (!strcmpiW(month, mar)) return 3;
4296 if (!strcmpiW(month, apr)) return 4;
4297 if (!strcmpiW(month, may)) return 5;
4298 if (!strcmpiW(month, jun)) return 6;
4299 if (!strcmpiW(month, jul)) return 7;
4300 if (!strcmpiW(month, aug)) return 8;
4301 if (!strcmpiW(month, sep)) return 9;
4302 if (!strcmpiW(month, oct)) return 10;
4303 if (!strcmpiW(month, nov)) return 11;
4304 if (!strcmpiW(month, dec)) return 12;
4305 /* Invalid */
4306 return 0;
4309 /* Parses the string pointed to by *str, assumed to be a 24-hour time HH:MM:SS,
4310 * optionally preceded by whitespace.
4311 * Upon success, returns TRUE, sets the wHour, wMinute, and wSecond fields of
4312 * st, and sets *str to the first character after the time format.
4314 static BOOL HTTP_ParseTime(SYSTEMTIME *st, LPCWSTR *str)
4316 LPCWSTR ptr = *str;
4317 WCHAR *nextPtr;
4318 unsigned long num;
4320 while (isspaceW(*ptr))
4321 ptr++;
4323 num = strtoulW(ptr, &nextPtr, 10);
4324 if (!nextPtr || nextPtr <= ptr || *nextPtr != ':')
4326 ERR("unexpected time format %s\n", debugstr_w(ptr));
4327 return FALSE;
4329 if (num > 23)
4331 ERR("unexpected hour in time format %s\n", debugstr_w(ptr));
4332 return FALSE;
4334 ptr = nextPtr + 1;
4335 st->wHour = (WORD)num;
4336 num = strtoulW(ptr, &nextPtr, 10);
4337 if (!nextPtr || nextPtr <= ptr || *nextPtr != ':')
4339 ERR("unexpected time format %s\n", debugstr_w(ptr));
4340 return FALSE;
4342 if (num > 59)
4344 ERR("unexpected minute in time format %s\n", debugstr_w(ptr));
4345 return FALSE;
4347 ptr = nextPtr + 1;
4348 st->wMinute = (WORD)num;
4349 num = strtoulW(ptr, &nextPtr, 10);
4350 if (!nextPtr || nextPtr <= ptr)
4352 ERR("unexpected time format %s\n", debugstr_w(ptr));
4353 return FALSE;
4355 if (num > 59)
4357 ERR("unexpected second in time format %s\n", debugstr_w(ptr));
4358 return FALSE;
4360 ptr = nextPtr + 1;
4361 *str = ptr;
4362 st->wSecond = (WORD)num;
4363 return TRUE;
4366 static BOOL HTTP_ParseDateAsAsctime(LPCWSTR value, FILETIME *ft)
4368 static const WCHAR gmt[]= { 'G','M','T',0 };
4369 WCHAR day[4], *dayPtr, month[4], *monthPtr, *nextPtr;
4370 LPCWSTR ptr;
4371 SYSTEMTIME st = { 0 };
4372 unsigned long num;
4374 for (ptr = value, dayPtr = day; *ptr && !isspaceW(*ptr) &&
4375 dayPtr - day < sizeof(day) / sizeof(day[0]) - 1; ptr++, dayPtr++)
4376 *dayPtr = *ptr;
4377 *dayPtr = 0;
4378 st.wDayOfWeek = HTTP_ParseWkday(day);
4379 if (st.wDayOfWeek >= 7)
4381 ERR("unexpected weekday %s\n", debugstr_w(day));
4382 return FALSE;
4385 while (isspaceW(*ptr))
4386 ptr++;
4388 for (monthPtr = month; !isspace(*ptr) &&
4389 monthPtr - month < sizeof(month) / sizeof(month[0]) - 1;
4390 monthPtr++, ptr++)
4391 *monthPtr = *ptr;
4392 *monthPtr = 0;
4393 st.wMonth = HTTP_ParseMonth(month);
4394 if (!st.wMonth || st.wMonth > 12)
4396 ERR("unexpected month %s\n", debugstr_w(month));
4397 return FALSE;
4400 while (isspaceW(*ptr))
4401 ptr++;
4403 num = strtoulW(ptr, &nextPtr, 10);
4404 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4406 ERR("unexpected day %s\n", debugstr_w(ptr));
4407 return FALSE;
4409 ptr = nextPtr;
4410 st.wDay = (WORD)num;
4412 while (isspaceW(*ptr))
4413 ptr++;
4415 if (!HTTP_ParseTime(&st, &ptr))
4416 return FALSE;
4418 while (isspaceW(*ptr))
4419 ptr++;
4421 num = strtoulW(ptr, &nextPtr, 10);
4422 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4424 ERR("unexpected year %s\n", debugstr_w(ptr));
4425 return FALSE;
4427 ptr = nextPtr;
4428 st.wYear = (WORD)num;
4430 while (isspaceW(*ptr))
4431 ptr++;
4433 /* asctime() doesn't report a timezone, but some web servers do, so accept
4434 * with or without GMT.
4436 if (*ptr && strcmpW(ptr, gmt))
4438 ERR("unexpected timezone %s\n", debugstr_w(ptr));
4439 return FALSE;
4441 return SystemTimeToFileTime(&st, ft);
4444 static BOOL HTTP_ParseRfc1123Date(LPCWSTR value, FILETIME *ft)
4446 static const WCHAR gmt[]= { 'G','M','T',0 };
4447 WCHAR *nextPtr, day[4], month[4], *monthPtr;
4448 LPCWSTR ptr;
4449 unsigned long num;
4450 SYSTEMTIME st = { 0 };
4452 ptr = strchrW(value, ',');
4453 if (!ptr)
4454 return FALSE;
4455 if (ptr - value != 3)
4457 WARN("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4458 return FALSE;
4460 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4461 day[3] = 0;
4462 st.wDayOfWeek = HTTP_ParseWkday(day);
4463 if (st.wDayOfWeek > 6)
4465 WARN("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4466 return FALSE;
4468 ptr++;
4470 while (isspaceW(*ptr))
4471 ptr++;
4473 num = strtoulW(ptr, &nextPtr, 10);
4474 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4476 WARN("unexpected day %s\n", debugstr_w(value));
4477 return FALSE;
4479 ptr = nextPtr;
4480 st.wDay = (WORD)num;
4482 while (isspaceW(*ptr))
4483 ptr++;
4485 for (monthPtr = month; !isspace(*ptr) &&
4486 monthPtr - month < sizeof(month) / sizeof(month[0]) - 1;
4487 monthPtr++, ptr++)
4488 *monthPtr = *ptr;
4489 *monthPtr = 0;
4490 st.wMonth = HTTP_ParseMonth(month);
4491 if (!st.wMonth || st.wMonth > 12)
4493 WARN("unexpected month %s\n", debugstr_w(month));
4494 return FALSE;
4497 while (isspaceW(*ptr))
4498 ptr++;
4500 num = strtoulW(ptr, &nextPtr, 10);
4501 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4503 ERR("unexpected year %s\n", debugstr_w(value));
4504 return FALSE;
4506 ptr = nextPtr;
4507 st.wYear = (WORD)num;
4509 if (!HTTP_ParseTime(&st, &ptr))
4510 return FALSE;
4512 while (isspaceW(*ptr))
4513 ptr++;
4515 if (strcmpW(ptr, gmt))
4517 ERR("unexpected time zone %s\n", debugstr_w(ptr));
4518 return FALSE;
4520 return SystemTimeToFileTime(&st, ft);
4523 static WORD HTTP_ParseWeekday(LPCWSTR day)
4525 static const WCHAR days[7][10] = {{ 's','u','n','d','a','y',0 },
4526 { 'm','o','n','d','a','y',0 },
4527 { 't','u','e','s','d','a','y',0 },
4528 { 'w','e','d','n','e','s','d','a','y',0 },
4529 { 't','h','u','r','s','d','a','y',0 },
4530 { 'f','r','i','d','a','y',0 },
4531 { 's','a','t','u','r','d','a','y',0 }};
4532 unsigned int i;
4533 for (i = 0; i < sizeof(days)/sizeof(*days); i++)
4534 if (!strcmpiW(day, days[i]))
4535 return i;
4537 /* Invalid */
4538 return 7;
4541 static BOOL HTTP_ParseRfc850Date(LPCWSTR value, FILETIME *ft)
4543 static const WCHAR gmt[]= { 'G','M','T',0 };
4544 WCHAR *nextPtr, day[10], month[4], *monthPtr;
4545 LPCWSTR ptr;
4546 unsigned long num;
4547 SYSTEMTIME st = { 0 };
4549 ptr = strchrW(value, ',');
4550 if (!ptr)
4551 return FALSE;
4552 if (ptr - value == 3)
4554 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4555 day[3] = 0;
4556 st.wDayOfWeek = HTTP_ParseWkday(day);
4557 if (st.wDayOfWeek > 6)
4559 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4560 return FALSE;
4563 else if (ptr - value < sizeof(day) / sizeof(day[0]))
4565 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4566 day[ptr - value + 1] = 0;
4567 st.wDayOfWeek = HTTP_ParseWeekday(day);
4568 if (st.wDayOfWeek > 6)
4570 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4571 return FALSE;
4574 else
4576 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4577 return FALSE;
4579 ptr++;
4581 while (isspaceW(*ptr))
4582 ptr++;
4584 num = strtoulW(ptr, &nextPtr, 10);
4585 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4587 ERR("unexpected day %s\n", debugstr_w(value));
4588 return FALSE;
4590 ptr = nextPtr;
4591 st.wDay = (WORD)num;
4593 if (*ptr != '-')
4595 ERR("unexpected month format %s\n", debugstr_w(ptr));
4596 return FALSE;
4598 ptr++;
4600 for (monthPtr = month; *ptr != '-' &&
4601 monthPtr - month < sizeof(month) / sizeof(month[0]) - 1;
4602 monthPtr++, ptr++)
4603 *monthPtr = *ptr;
4604 *monthPtr = 0;
4605 st.wMonth = HTTP_ParseMonth(month);
4606 if (!st.wMonth || st.wMonth > 12)
4608 ERR("unexpected month %s\n", debugstr_w(month));
4609 return FALSE;
4612 if (*ptr != '-')
4614 ERR("unexpected year format %s\n", debugstr_w(ptr));
4615 return FALSE;
4617 ptr++;
4619 num = strtoulW(ptr, &nextPtr, 10);
4620 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4622 ERR("unexpected year %s\n", debugstr_w(value));
4623 return FALSE;
4625 ptr = nextPtr;
4626 st.wYear = (WORD)num;
4628 if (!HTTP_ParseTime(&st, &ptr))
4629 return FALSE;
4631 while (isspaceW(*ptr))
4632 ptr++;
4634 if (strcmpW(ptr, gmt))
4636 ERR("unexpected time zone %s\n", debugstr_w(ptr));
4637 return FALSE;
4639 return SystemTimeToFileTime(&st, ft);
4642 static BOOL HTTP_ParseDate(LPCWSTR value, FILETIME *ft)
4644 static const WCHAR zero[] = { '0',0 };
4645 BOOL ret;
4647 if (!strcmpW(value, zero))
4649 ft->dwLowDateTime = ft->dwHighDateTime = 0;
4650 ret = TRUE;
4652 else if (strchrW(value, ','))
4654 ret = HTTP_ParseRfc1123Date(value, ft);
4655 if (!ret)
4657 ret = HTTP_ParseRfc850Date(value, ft);
4658 if (!ret)
4659 ERR("unexpected date format %s\n", debugstr_w(value));
4662 else
4664 ret = HTTP_ParseDateAsAsctime(value, ft);
4665 if (!ret)
4666 ERR("unexpected date format %s\n", debugstr_w(value));
4668 return ret;
4671 static void HTTP_ProcessExpires(http_request_t *request)
4673 BOOL expirationFound = FALSE;
4674 int headerIndex;
4676 EnterCriticalSection( &request->headers_section );
4678 /* Look for a Cache-Control header with a max-age directive, as it takes
4679 * precedence over the Expires header.
4681 headerIndex = HTTP_GetCustomHeaderIndex(request, szCache_Control, 0, FALSE);
4682 if (headerIndex != -1)
4684 LPHTTPHEADERW ccHeader = &request->custHeaders[headerIndex];
4685 LPWSTR ptr;
4687 for (ptr = ccHeader->lpszValue; ptr && *ptr; )
4689 LPWSTR comma = strchrW(ptr, ','), end, equal;
4691 if (comma)
4692 end = comma;
4693 else
4694 end = ptr + strlenW(ptr);
4695 for (equal = end - 1; equal > ptr && *equal != '='; equal--)
4697 if (*equal == '=')
4699 static const WCHAR max_age[] = {
4700 'm','a','x','-','a','g','e',0 };
4702 if (!strncmpiW(ptr, max_age, equal - ptr - 1))
4704 LPWSTR nextPtr;
4705 unsigned long age;
4707 age = strtoulW(equal + 1, &nextPtr, 10);
4708 if (nextPtr > equal + 1)
4710 LARGE_INTEGER ft;
4712 NtQuerySystemTime( &ft );
4713 /* Age is in seconds, FILETIME resolution is in
4714 * 100 nanosecond intervals.
4716 ft.QuadPart += age * (ULONGLONG)1000000;
4717 request->expires.dwLowDateTime = ft.u.LowPart;
4718 request->expires.dwHighDateTime = ft.u.HighPart;
4719 expirationFound = TRUE;
4723 if (comma)
4725 ptr = comma + 1;
4726 while (isspaceW(*ptr))
4727 ptr++;
4729 else
4730 ptr = NULL;
4733 if (!expirationFound)
4735 headerIndex = HTTP_GetCustomHeaderIndex(request, szExpires, 0, FALSE);
4736 if (headerIndex != -1)
4738 LPHTTPHEADERW expiresHeader = &request->custHeaders[headerIndex];
4739 FILETIME ft;
4741 if (HTTP_ParseDate(expiresHeader->lpszValue, &ft))
4743 expirationFound = TRUE;
4744 request->expires = ft;
4748 if (!expirationFound)
4750 LARGE_INTEGER t;
4752 /* With no known age, default to 10 minutes until expiration. */
4753 NtQuerySystemTime( &t );
4754 t.QuadPart += 10 * 60 * (ULONGLONG)10000000;
4755 request->expires.dwLowDateTime = t.u.LowPart;
4756 request->expires.dwHighDateTime = t.u.HighPart;
4759 LeaveCriticalSection( &request->headers_section );
4762 static void HTTP_ProcessLastModified(http_request_t *request)
4764 int headerIndex;
4766 EnterCriticalSection( &request->headers_section );
4768 headerIndex = HTTP_GetCustomHeaderIndex(request, szLast_Modified, 0, FALSE);
4769 if (headerIndex != -1)
4771 LPHTTPHEADERW expiresHeader = &request->custHeaders[headerIndex];
4772 FILETIME ft;
4774 if (HTTP_ParseDate(expiresHeader->lpszValue, &ft))
4775 request->last_modified = ft;
4778 LeaveCriticalSection( &request->headers_section );
4781 static void http_process_keep_alive(http_request_t *req)
4783 int index;
4785 EnterCriticalSection( &req->headers_section );
4787 if ((index = HTTP_GetCustomHeaderIndex(req, szConnection, 0, FALSE)) != -1)
4788 req->netconn->keep_alive = !strcmpiW(req->custHeaders[index].lpszValue, szKeepAlive);
4789 else if ((index = HTTP_GetCustomHeaderIndex(req, szProxy_Connection, 0, FALSE)) != -1)
4790 req->netconn->keep_alive = !strcmpiW(req->custHeaders[index].lpszValue, szKeepAlive);
4791 else
4792 req->netconn->keep_alive = !strcmpiW(req->version, g_szHttp1_1);
4794 LeaveCriticalSection( &req->headers_section );
4797 static DWORD open_http_connection(http_request_t *request, BOOL *reusing)
4799 const BOOL is_https = (request->hdr.dwFlags & INTERNET_FLAG_SECURE) != 0;
4800 netconn_t *netconn = NULL;
4801 DWORD res;
4803 reset_data_stream(request);
4805 if (request->netconn)
4807 if (is_valid_netconn(request->netconn) && NETCON_is_alive(request->netconn))
4809 *reusing = TRUE;
4810 return ERROR_SUCCESS;
4812 else
4814 free_netconn(request->netconn);
4815 request->netconn = NULL;
4819 res = HTTP_ResolveName(request);
4820 if(res != ERROR_SUCCESS)
4821 return res;
4823 EnterCriticalSection(&connection_pool_cs);
4825 while(!list_empty(&request->server->conn_pool)) {
4826 netconn = LIST_ENTRY(list_head(&request->server->conn_pool), netconn_t, pool_entry);
4827 list_remove(&netconn->pool_entry);
4829 if(is_valid_netconn(netconn) && NETCON_is_alive(netconn))
4830 break;
4832 TRACE("connection %p closed during idle\n", netconn);
4833 free_netconn(netconn);
4834 netconn = NULL;
4837 LeaveCriticalSection(&connection_pool_cs);
4839 if(netconn) {
4840 TRACE("<-- reusing %p netconn\n", netconn);
4841 request->netconn = netconn;
4842 *reusing = TRUE;
4843 return ERROR_SUCCESS;
4846 TRACE("connecting to %s, proxy %s\n", debugstr_w(request->server->name),
4847 request->proxy ? debugstr_w(request->proxy->name) : "(null)");
4849 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
4850 INTERNET_STATUS_CONNECTING_TO_SERVER,
4851 request->server->addr_str,
4852 strlen(request->server->addr_str)+1);
4854 res = create_netconn(is_https, request->proxy ? request->proxy : request->server, request->security_flags,
4855 (request->hdr.ErrorMask & INTERNET_ERROR_MASK_COMBINED_SEC_CERT) != 0,
4856 request->connect_timeout, &netconn);
4857 if(res != ERROR_SUCCESS) {
4858 ERR("create_netconn failed: %u\n", res);
4859 return res;
4862 request->netconn = netconn;
4864 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
4865 INTERNET_STATUS_CONNECTED_TO_SERVER,
4866 request->server->addr_str, strlen(request->server->addr_str)+1);
4868 *reusing = FALSE;
4869 TRACE("Created connection to %s: %p\n", debugstr_w(request->server->name), netconn);
4870 return ERROR_SUCCESS;
4873 static char *build_ascii_request( const WCHAR *str, void *data, DWORD data_len, DWORD *out_len )
4875 int len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
4876 char *ret;
4878 if (!(ret = heap_alloc( len + data_len ))) return NULL;
4879 WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
4880 if (data_len) memcpy( ret + len - 1, data, data_len );
4881 *out_len = len + data_len - 1;
4882 ret[*out_len] = 0;
4883 return ret;
4886 /***********************************************************************
4887 * HTTP_HttpSendRequestW (internal)
4889 * Sends the specified request to the HTTP server
4891 * RETURNS
4892 * ERROR_SUCCESS on success
4893 * win32 error code on failure
4896 static DWORD HTTP_HttpSendRequestW(http_request_t *request, LPCWSTR lpszHeaders,
4897 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
4898 DWORD dwContentLength, BOOL bEndRequest)
4900 static const WCHAR szContentLength[] =
4901 { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0 };
4902 BOOL redirected = FALSE, secure_proxy_connect = FALSE, loop_next;
4903 LPWSTR requestString = NULL;
4904 INT responseLen, cnt;
4905 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \r\n */ + 20 /* int */ ];
4906 DWORD res;
4908 TRACE("--> %p\n", request);
4910 assert(request->hdr.htype == WH_HHTTPREQ);
4912 /* if the verb is NULL default to GET */
4913 if (!request->verb)
4914 request->verb = heap_strdupW(szGET);
4916 if (dwContentLength || strcmpW(request->verb, szGET))
4918 sprintfW(contentLengthStr, szContentLength, dwContentLength);
4919 HTTP_HttpAddRequestHeadersW(request, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4920 request->bytesToWrite = dwContentLength;
4922 if (request->session->appInfo->agent)
4924 WCHAR *agent_header;
4925 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0};
4926 int len;
4928 len = strlenW(request->session->appInfo->agent) + strlenW(user_agent);
4929 agent_header = heap_alloc(len * sizeof(WCHAR));
4930 sprintfW(agent_header, user_agent, request->session->appInfo->agent);
4932 HTTP_HttpAddRequestHeadersW(request, agent_header, strlenW(agent_header), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4933 heap_free(agent_header);
4935 if (request->hdr.dwFlags & INTERNET_FLAG_PRAGMA_NOCACHE)
4937 static const WCHAR pragma_nocache[] = {'P','r','a','g','m','a',':',' ','n','o','-','c','a','c','h','e','\r','\n',0};
4938 HTTP_HttpAddRequestHeadersW(request, pragma_nocache, strlenW(pragma_nocache), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4940 if ((request->hdr.dwFlags & INTERNET_FLAG_NO_CACHE_WRITE) && strcmpW(request->verb, szGET))
4942 static const WCHAR cache_control[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l',':',
4943 ' ','n','o','-','c','a','c','h','e','\r','\n',0};
4944 HTTP_HttpAddRequestHeadersW(request, cache_control, strlenW(cache_control), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4947 /* add the headers the caller supplied */
4948 if( lpszHeaders && dwHeaderLength )
4949 HTTP_HttpAddRequestHeadersW(request, lpszHeaders, dwHeaderLength, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
4953 DWORD len, data_len = dwOptionalLength;
4954 BOOL reusing_connection;
4955 char *ascii_req;
4957 loop_next = FALSE;
4959 if(redirected) {
4960 request->contentLength = ~0u;
4961 request->bytesToWrite = 0;
4964 if (TRACE_ON(wininet))
4966 HTTPHEADERW *host;
4968 EnterCriticalSection( &request->headers_section );
4969 host = HTTP_GetHeader( request, hostW );
4970 TRACE("Going to url %s %s\n", debugstr_w(host->lpszValue), debugstr_w(request->path));
4971 LeaveCriticalSection( &request->headers_section );
4974 HTTP_FixURL(request);
4975 if (request->hdr.dwFlags & INTERNET_FLAG_KEEP_CONNECTION)
4977 HTTP_ProcessHeader(request, szConnection, szKeepAlive, HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
4979 HTTP_InsertAuthorization(request, request->authInfo, szAuthorization);
4980 HTTP_InsertAuthorization(request, request->proxyAuthInfo, szProxy_Authorization);
4982 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES))
4983 HTTP_InsertCookies(request);
4985 res = open_http_connection(request, &reusing_connection);
4986 if (res != ERROR_SUCCESS)
4987 break;
4989 if (!reusing_connection && (request->hdr.dwFlags & INTERNET_FLAG_SECURE))
4991 if (request->proxy) secure_proxy_connect = TRUE;
4992 else
4994 res = NETCON_secure_connect(request->netconn, request->server);
4995 if (res != ERROR_SUCCESS)
4997 WARN("failed to upgrade to secure connection\n");
4998 http_release_netconn(request, FALSE);
4999 break;
5003 if (secure_proxy_connect)
5005 static const WCHAR connectW[] = {'C','O','N','N','E','C','T',0};
5006 const WCHAR *target = request->server->host_port;
5007 requestString = build_request_header(request, connectW, target, g_szHttp1_1, TRUE);
5009 else if (request->proxy && !(request->hdr.dwFlags & INTERNET_FLAG_SECURE))
5011 WCHAR *url = build_proxy_path_url(request);
5012 requestString = build_request_header(request, request->verb, url, request->version, TRUE);
5013 heap_free(url);
5015 else
5016 requestString = build_request_header(request, request->verb, request->path, request->version, TRUE);
5018 TRACE("Request header -> %s\n", debugstr_w(requestString) );
5020 /* send the request as ASCII, tack on the optional data */
5021 if (!lpOptional || redirected || secure_proxy_connect)
5022 data_len = 0;
5024 ascii_req = build_ascii_request( requestString, lpOptional, data_len, &len );
5025 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
5027 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5028 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
5030 NETCON_set_timeout( request->netconn, TRUE, request->send_timeout );
5031 res = NETCON_send(request->netconn, ascii_req, len, 0, &cnt);
5032 heap_free( ascii_req );
5033 if(res != ERROR_SUCCESS) {
5034 TRACE("send failed: %u\n", res);
5035 if(!reusing_connection)
5036 break;
5037 http_release_netconn(request, FALSE);
5038 loop_next = TRUE;
5039 continue;
5042 request->bytesWritten = data_len;
5044 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5045 INTERNET_STATUS_REQUEST_SENT,
5046 &len, sizeof(DWORD));
5048 if (bEndRequest)
5050 DWORD dwBufferSize;
5052 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5053 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
5055 if (HTTP_GetResponseHeaders(request, &responseLen))
5057 http_release_netconn(request, FALSE);
5058 res = ERROR_INTERNET_CONNECTION_ABORTED;
5059 goto lend;
5061 /* FIXME: We should know that connection is closed before sending
5062 * headers. Otherwise wrong callbacks are executed */
5063 if(!responseLen && reusing_connection) {
5064 TRACE("Connection closed by server, reconnecting\n");
5065 http_release_netconn(request, FALSE);
5066 loop_next = TRUE;
5067 continue;
5070 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5071 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
5072 sizeof(DWORD));
5074 http_process_keep_alive(request);
5075 HTTP_ProcessCookies(request);
5076 HTTP_ProcessExpires(request);
5077 HTTP_ProcessLastModified(request);
5079 res = set_content_length(request);
5080 if(res != ERROR_SUCCESS)
5081 goto lend;
5082 if(!request->contentLength)
5083 http_release_netconn(request, TRUE);
5085 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && responseLen)
5087 WCHAR *new_url, szNewLocation[INTERNET_MAX_URL_LENGTH];
5088 dwBufferSize=sizeof(szNewLocation);
5089 switch(request->status_code) {
5090 case HTTP_STATUS_REDIRECT:
5091 case HTTP_STATUS_MOVED:
5092 case HTTP_STATUS_REDIRECT_KEEP_VERB:
5093 case HTTP_STATUS_REDIRECT_METHOD:
5094 if(HTTP_HttpQueryInfoW(request,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL) != ERROR_SUCCESS)
5095 break;
5097 if (strcmpW(request->verb, szGET) && strcmpW(request->verb, szHEAD) &&
5098 request->status_code != HTTP_STATUS_REDIRECT_KEEP_VERB)
5100 heap_free(request->verb);
5101 request->verb = heap_strdupW(szGET);
5103 http_release_netconn(request, drain_content(request, FALSE));
5104 if ((new_url = HTTP_GetRedirectURL( request, szNewLocation )))
5106 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REDIRECT,
5107 new_url, (strlenW(new_url) + 1) * sizeof(WCHAR));
5108 res = HTTP_HandleRedirect(request, new_url);
5109 if (res == ERROR_SUCCESS)
5111 heap_free(requestString);
5112 loop_next = TRUE;
5114 heap_free( new_url );
5116 redirected = TRUE;
5119 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTH) && res == ERROR_SUCCESS)
5121 WCHAR szAuthValue[2048];
5122 dwBufferSize=2048;
5123 if (request->status_code == HTTP_STATUS_DENIED)
5125 WCHAR *host = get_host_header( request );
5126 DWORD dwIndex = 0;
5127 while (HTTP_HttpQueryInfoW(request,HTTP_QUERY_WWW_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex) == ERROR_SUCCESS)
5129 if (HTTP_DoAuthorization(request, szAuthValue,
5130 &request->authInfo,
5131 request->session->userName,
5132 request->session->password, host))
5134 heap_free(requestString);
5135 if(!drain_content(request, TRUE)) {
5136 FIXME("Could not drain content\n");
5137 http_release_netconn(request, FALSE);
5139 loop_next = TRUE;
5140 break;
5143 heap_free( host );
5145 if(!loop_next) {
5146 TRACE("Cleaning wrong authorization data\n");
5147 destroy_authinfo(request->authInfo);
5148 request->authInfo = NULL;
5151 if (request->status_code == HTTP_STATUS_PROXY_AUTH_REQ)
5153 DWORD dwIndex = 0;
5154 while (HTTP_HttpQueryInfoW(request,HTTP_QUERY_PROXY_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex) == ERROR_SUCCESS)
5156 if (HTTP_DoAuthorization(request, szAuthValue,
5157 &request->proxyAuthInfo,
5158 request->session->appInfo->proxyUsername,
5159 request->session->appInfo->proxyPassword,
5160 NULL))
5162 heap_free(requestString);
5163 if(!drain_content(request, TRUE)) {
5164 FIXME("Could not drain content\n");
5165 http_release_netconn(request, FALSE);
5167 loop_next = TRUE;
5168 break;
5172 if(!loop_next) {
5173 TRACE("Cleaning wrong proxy authorization data\n");
5174 destroy_authinfo(request->proxyAuthInfo);
5175 request->proxyAuthInfo = NULL;
5179 if (secure_proxy_connect && request->status_code == HTTP_STATUS_OK)
5181 res = NETCON_secure_connect(request->netconn, request->server);
5182 if (res != ERROR_SUCCESS)
5184 WARN("failed to upgrade to secure proxy connection\n");
5185 http_release_netconn( request, FALSE );
5186 break;
5188 remove_header(request, szProxy_Authorization, TRUE);
5189 destroy_authinfo(request->proxyAuthInfo);
5190 request->proxyAuthInfo = NULL;
5192 secure_proxy_connect = FALSE;
5193 loop_next = TRUE;
5196 else
5197 res = ERROR_SUCCESS;
5199 while (loop_next);
5201 lend:
5202 heap_free(requestString);
5204 /* TODO: send notification for P3P header */
5206 if(res == ERROR_SUCCESS)
5207 create_cache_entry(request);
5209 if (request->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5211 if (res == ERROR_SUCCESS) {
5212 if(bEndRequest && request->contentLength && request->bytesWritten == request->bytesToWrite)
5213 HTTP_ReceiveRequestData(request, TRUE, NULL);
5214 else
5215 send_request_complete(request,
5216 request->session->hdr.dwInternalFlags & INET_OPENURL ? (DWORD_PTR)request->hdr.hInternet : 1, 0);
5217 }else {
5218 send_request_complete(request, 0, res);
5222 TRACE("<--\n");
5223 return res;
5226 typedef struct {
5227 task_header_t hdr;
5228 WCHAR *headers;
5229 DWORD headers_len;
5230 void *optional;
5231 DWORD optional_len;
5232 DWORD content_len;
5233 BOOL end_request;
5234 } send_request_task_t;
5236 /***********************************************************************
5238 * Helper functions for the HttpSendRequest(Ex) functions
5241 static void AsyncHttpSendRequestProc(task_header_t *hdr)
5243 send_request_task_t *task = (send_request_task_t*)hdr;
5244 http_request_t *request = (http_request_t*)task->hdr.hdr;
5246 TRACE("%p\n", request);
5248 HTTP_HttpSendRequestW(request, task->headers, task->headers_len, task->optional,
5249 task->optional_len, task->content_len, task->end_request);
5251 heap_free(task->headers);
5255 static DWORD HTTP_HttpEndRequestW(http_request_t *request, DWORD dwFlags, DWORD_PTR dwContext)
5257 DWORD dwBufferSize;
5258 INT responseLen;
5259 DWORD res = ERROR_SUCCESS;
5261 if(!is_valid_netconn(request->netconn)) {
5262 WARN("Not connected\n");
5263 send_request_complete(request, 0, ERROR_INTERNET_OPERATION_CANCELLED);
5264 return ERROR_INTERNET_OPERATION_CANCELLED;
5267 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5268 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
5270 if (HTTP_GetResponseHeaders(request, &responseLen) || !responseLen)
5271 res = ERROR_HTTP_HEADER_NOT_FOUND;
5273 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5274 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
5276 /* process cookies here. Is this right? */
5277 http_process_keep_alive(request);
5278 HTTP_ProcessCookies(request);
5279 HTTP_ProcessExpires(request);
5280 HTTP_ProcessLastModified(request);
5282 if ((res = set_content_length(request)) == ERROR_SUCCESS) {
5283 if(!request->contentLength)
5284 http_release_netconn(request, TRUE);
5287 if (res == ERROR_SUCCESS && !(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
5289 switch(request->status_code) {
5290 case HTTP_STATUS_REDIRECT:
5291 case HTTP_STATUS_MOVED:
5292 case HTTP_STATUS_REDIRECT_METHOD:
5293 case HTTP_STATUS_REDIRECT_KEEP_VERB: {
5294 WCHAR *new_url, szNewLocation[INTERNET_MAX_URL_LENGTH];
5295 dwBufferSize=sizeof(szNewLocation);
5296 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_LOCATION, szNewLocation, &dwBufferSize, NULL) != ERROR_SUCCESS)
5297 break;
5299 if (strcmpW(request->verb, szGET) && strcmpW(request->verb, szHEAD) &&
5300 request->status_code != HTTP_STATUS_REDIRECT_KEEP_VERB)
5302 heap_free(request->verb);
5303 request->verb = heap_strdupW(szGET);
5305 http_release_netconn(request, drain_content(request, FALSE));
5306 if ((new_url = HTTP_GetRedirectURL( request, szNewLocation )))
5308 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REDIRECT,
5309 new_url, (strlenW(new_url) + 1) * sizeof(WCHAR));
5310 res = HTTP_HandleRedirect(request, new_url);
5311 if (res == ERROR_SUCCESS)
5312 res = HTTP_HttpSendRequestW(request, NULL, 0, NULL, 0, 0, TRUE);
5313 heap_free( new_url );
5319 if(res == ERROR_SUCCESS)
5320 create_cache_entry(request);
5322 if (res == ERROR_SUCCESS && request->contentLength)
5323 HTTP_ReceiveRequestData(request, TRUE, NULL);
5324 else
5325 send_request_complete(request, res == ERROR_SUCCESS, res);
5327 return res;
5330 /***********************************************************************
5331 * HttpEndRequestA (WININET.@)
5333 * Ends an HTTP request that was started by HttpSendRequestEx
5335 * RETURNS
5336 * TRUE if successful
5337 * FALSE on failure
5340 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
5341 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
5343 TRACE("(%p, %p, %08x, %08lx)\n", hRequest, lpBuffersOut, dwFlags, dwContext);
5345 if (lpBuffersOut)
5347 SetLastError(ERROR_INVALID_PARAMETER);
5348 return FALSE;
5351 return HttpEndRequestW(hRequest, NULL, dwFlags, dwContext);
5354 typedef struct {
5355 task_header_t hdr;
5356 DWORD flags;
5357 DWORD context;
5358 } end_request_task_t;
5360 static void AsyncHttpEndRequestProc(task_header_t *hdr)
5362 end_request_task_t *task = (end_request_task_t*)hdr;
5363 http_request_t *req = (http_request_t*)task->hdr.hdr;
5365 TRACE("%p\n", req);
5367 HTTP_HttpEndRequestW(req, task->flags, task->context);
5370 /***********************************************************************
5371 * HttpEndRequestW (WININET.@)
5373 * Ends an HTTP request that was started by HttpSendRequestEx
5375 * RETURNS
5376 * TRUE if successful
5377 * FALSE on failure
5380 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
5381 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
5383 http_request_t *request;
5384 DWORD res;
5386 TRACE("%p %p %x %lx -->\n", hRequest, lpBuffersOut, dwFlags, dwContext);
5388 if (lpBuffersOut)
5390 SetLastError(ERROR_INVALID_PARAMETER);
5391 return FALSE;
5394 request = (http_request_t*) get_handle_object( hRequest );
5396 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5398 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
5399 if (request)
5400 WININET_Release( &request->hdr );
5401 return FALSE;
5403 request->hdr.dwFlags |= dwFlags;
5405 if (request->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5407 end_request_task_t *task;
5409 task = alloc_async_task(&request->hdr, AsyncHttpEndRequestProc, sizeof(*task));
5410 task->flags = dwFlags;
5411 task->context = dwContext;
5413 INTERNET_AsyncCall(&task->hdr);
5414 res = ERROR_IO_PENDING;
5416 else
5417 res = HTTP_HttpEndRequestW(request, dwFlags, dwContext);
5419 WININET_Release( &request->hdr );
5420 TRACE("%u <--\n", res);
5421 if(res != ERROR_SUCCESS)
5422 SetLastError(res);
5423 return res == ERROR_SUCCESS;
5426 /***********************************************************************
5427 * HttpSendRequestExA (WININET.@)
5429 * Sends the specified request to the HTTP server and allows chunked
5430 * transfers.
5432 * RETURNS
5433 * Success: TRUE
5434 * Failure: FALSE, call GetLastError() for more information.
5436 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
5437 LPINTERNET_BUFFERSA lpBuffersIn,
5438 LPINTERNET_BUFFERSA lpBuffersOut,
5439 DWORD dwFlags, DWORD_PTR dwContext)
5441 INTERNET_BUFFERSW BuffersInW;
5442 BOOL rc = FALSE;
5443 DWORD headerlen;
5444 LPWSTR header = NULL;
5446 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
5447 lpBuffersOut, dwFlags, dwContext);
5449 if (lpBuffersIn)
5451 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
5452 if (lpBuffersIn->lpcszHeader)
5454 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
5455 lpBuffersIn->dwHeadersLength,0,0);
5456 header = heap_alloc(headerlen*sizeof(WCHAR));
5457 if (!(BuffersInW.lpcszHeader = header))
5459 SetLastError(ERROR_OUTOFMEMORY);
5460 return FALSE;
5462 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
5463 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
5464 header, headerlen);
5466 else
5467 BuffersInW.lpcszHeader = NULL;
5468 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
5469 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
5470 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
5471 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
5472 BuffersInW.Next = NULL;
5475 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
5477 heap_free(header);
5478 return rc;
5481 /***********************************************************************
5482 * HttpSendRequestExW (WININET.@)
5484 * Sends the specified request to the HTTP server and allows chunked
5485 * transfers
5487 * RETURNS
5488 * Success: TRUE
5489 * Failure: FALSE, call GetLastError() for more information.
5491 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
5492 LPINTERNET_BUFFERSW lpBuffersIn,
5493 LPINTERNET_BUFFERSW lpBuffersOut,
5494 DWORD dwFlags, DWORD_PTR dwContext)
5496 http_request_t *request;
5497 http_session_t *session;
5498 appinfo_t *hIC;
5499 DWORD res;
5501 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
5502 lpBuffersOut, dwFlags, dwContext);
5504 request = (http_request_t*) get_handle_object( hRequest );
5506 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5508 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5509 goto lend;
5512 session = request->session;
5513 assert(session->hdr.htype == WH_HHTTPSESSION);
5514 hIC = session->appInfo;
5515 assert(hIC->hdr.htype == WH_HINIT);
5517 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5519 send_request_task_t *task;
5521 task = alloc_async_task(&request->hdr, AsyncHttpSendRequestProc, sizeof(*task));
5522 if (lpBuffersIn)
5524 DWORD size = 0;
5526 if (lpBuffersIn->lpcszHeader)
5528 if (lpBuffersIn->dwHeadersLength == ~0u)
5529 size = (strlenW( lpBuffersIn->lpcszHeader ) + 1) * sizeof(WCHAR);
5530 else
5531 size = lpBuffersIn->dwHeadersLength * sizeof(WCHAR);
5533 task->headers = heap_alloc(size);
5534 memcpy(task->headers, lpBuffersIn->lpcszHeader, size);
5536 else task->headers = NULL;
5538 task->headers_len = size / sizeof(WCHAR);
5539 task->optional = lpBuffersIn->lpvBuffer;
5540 task->optional_len = lpBuffersIn->dwBufferLength;
5541 task->content_len = lpBuffersIn->dwBufferTotal;
5543 else
5545 task->headers = NULL;
5546 task->headers_len = 0;
5547 task->optional = NULL;
5548 task->optional_len = 0;
5549 task->content_len = 0;
5552 task->end_request = FALSE;
5554 INTERNET_AsyncCall(&task->hdr);
5555 res = ERROR_IO_PENDING;
5557 else
5559 if (lpBuffersIn)
5560 res = HTTP_HttpSendRequestW(request, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
5561 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
5562 lpBuffersIn->dwBufferTotal, FALSE);
5563 else
5564 res = HTTP_HttpSendRequestW(request, NULL, 0, NULL, 0, 0, FALSE);
5567 lend:
5568 if ( request )
5569 WININET_Release( &request->hdr );
5571 TRACE("<---\n");
5572 SetLastError(res);
5573 return res == ERROR_SUCCESS;
5576 /***********************************************************************
5577 * HttpSendRequestW (WININET.@)
5579 * Sends the specified request to the HTTP server
5581 * RETURNS
5582 * TRUE on success
5583 * FALSE on failure
5586 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
5587 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
5589 http_request_t *request;
5590 http_session_t *session = NULL;
5591 appinfo_t *hIC = NULL;
5592 DWORD res = ERROR_SUCCESS;
5594 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest,
5595 debugstr_wn(lpszHeaders, dwHeaderLength), dwHeaderLength, lpOptional, dwOptionalLength);
5597 request = (http_request_t*) get_handle_object( hHttpRequest );
5598 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5600 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5601 goto lend;
5604 session = request->session;
5605 if (NULL == session || session->hdr.htype != WH_HHTTPSESSION)
5607 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5608 goto lend;
5611 hIC = session->appInfo;
5612 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
5614 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5615 goto lend;
5618 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5620 send_request_task_t *task;
5622 task = alloc_async_task(&request->hdr, AsyncHttpSendRequestProc, sizeof(*task));
5623 if (lpszHeaders)
5625 DWORD size;
5627 if (dwHeaderLength == ~0u) size = (strlenW(lpszHeaders) + 1) * sizeof(WCHAR);
5628 else size = dwHeaderLength * sizeof(WCHAR);
5630 task->headers = heap_alloc(size);
5631 memcpy(task->headers, lpszHeaders, size);
5633 else
5634 task->headers = NULL;
5635 task->headers_len = dwHeaderLength;
5636 task->optional = lpOptional;
5637 task->optional_len = dwOptionalLength;
5638 task->content_len = dwOptionalLength;
5639 task->end_request = TRUE;
5641 INTERNET_AsyncCall(&task->hdr);
5642 res = ERROR_IO_PENDING;
5644 else
5646 res = HTTP_HttpSendRequestW(request, lpszHeaders,
5647 dwHeaderLength, lpOptional, dwOptionalLength,
5648 dwOptionalLength, TRUE);
5650 lend:
5651 if( request )
5652 WININET_Release( &request->hdr );
5654 SetLastError(res);
5655 return res == ERROR_SUCCESS;
5658 /***********************************************************************
5659 * HttpSendRequestA (WININET.@)
5661 * Sends the specified request to the HTTP server
5663 * RETURNS
5664 * TRUE on success
5665 * FALSE on failure
5668 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
5669 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
5671 BOOL result;
5672 LPWSTR szHeaders=NULL;
5673 DWORD nLen=dwHeaderLength;
5674 if(lpszHeaders!=NULL)
5676 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
5677 szHeaders = heap_alloc(nLen*sizeof(WCHAR));
5678 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
5680 result = HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
5681 heap_free(szHeaders);
5682 return result;
5685 /***********************************************************************
5686 * HTTPSESSION_Destroy (internal)
5688 * Deallocate session handle
5691 static void HTTPSESSION_Destroy(object_header_t *hdr)
5693 http_session_t *session = (http_session_t*) hdr;
5695 TRACE("%p\n", session);
5697 WININET_Release(&session->appInfo->hdr);
5699 heap_free(session->hostName);
5700 heap_free(session->password);
5701 heap_free(session->userName);
5704 static DWORD HTTPSESSION_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
5706 http_session_t *ses = (http_session_t *)hdr;
5708 switch(option) {
5709 case INTERNET_OPTION_HANDLE_TYPE:
5710 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
5712 if (*size < sizeof(ULONG))
5713 return ERROR_INSUFFICIENT_BUFFER;
5715 *size = sizeof(DWORD);
5716 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_CONNECT_HTTP;
5717 return ERROR_SUCCESS;
5718 case INTERNET_OPTION_CONNECT_TIMEOUT:
5719 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
5721 if (*size < sizeof(DWORD))
5722 return ERROR_INSUFFICIENT_BUFFER;
5724 *size = sizeof(DWORD);
5725 *(DWORD *)buffer = ses->connect_timeout;
5726 return ERROR_SUCCESS;
5728 case INTERNET_OPTION_SEND_TIMEOUT:
5729 TRACE("INTERNET_OPTION_SEND_TIMEOUT\n");
5731 if (*size < sizeof(DWORD))
5732 return ERROR_INSUFFICIENT_BUFFER;
5734 *size = sizeof(DWORD);
5735 *(DWORD *)buffer = ses->send_timeout;
5736 return ERROR_SUCCESS;
5738 case INTERNET_OPTION_RECEIVE_TIMEOUT:
5739 TRACE("INTERNET_OPTION_RECEIVE_TIMEOUT\n");
5741 if (*size < sizeof(DWORD))
5742 return ERROR_INSUFFICIENT_BUFFER;
5744 *size = sizeof(DWORD);
5745 *(DWORD *)buffer = ses->receive_timeout;
5746 return ERROR_SUCCESS;
5749 return INET_QueryOption(hdr, option, buffer, size, unicode);
5752 static DWORD HTTPSESSION_SetOption(object_header_t *hdr, DWORD option, void *buffer, DWORD size)
5754 http_session_t *ses = (http_session_t*)hdr;
5756 switch(option) {
5757 case INTERNET_OPTION_USERNAME:
5759 heap_free(ses->userName);
5760 if (!(ses->userName = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5761 return ERROR_SUCCESS;
5763 case INTERNET_OPTION_PASSWORD:
5765 heap_free(ses->password);
5766 if (!(ses->password = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5767 return ERROR_SUCCESS;
5769 case INTERNET_OPTION_PROXY_USERNAME:
5771 heap_free(ses->appInfo->proxyUsername);
5772 if (!(ses->appInfo->proxyUsername = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5773 return ERROR_SUCCESS;
5775 case INTERNET_OPTION_PROXY_PASSWORD:
5777 heap_free(ses->appInfo->proxyPassword);
5778 if (!(ses->appInfo->proxyPassword = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5779 return ERROR_SUCCESS;
5781 case INTERNET_OPTION_CONNECT_TIMEOUT:
5783 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5784 ses->connect_timeout = *(DWORD *)buffer;
5785 return ERROR_SUCCESS;
5787 case INTERNET_OPTION_SEND_TIMEOUT:
5789 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5790 ses->send_timeout = *(DWORD *)buffer;
5791 return ERROR_SUCCESS;
5793 case INTERNET_OPTION_RECEIVE_TIMEOUT:
5795 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5796 ses->receive_timeout = *(DWORD *)buffer;
5797 return ERROR_SUCCESS;
5799 default: break;
5802 return INET_SetOption(hdr, option, buffer, size);
5805 static const object_vtbl_t HTTPSESSIONVtbl = {
5806 HTTPSESSION_Destroy,
5807 NULL,
5808 HTTPSESSION_QueryOption,
5809 HTTPSESSION_SetOption,
5810 NULL,
5811 NULL,
5812 NULL,
5813 NULL,
5814 NULL
5818 /***********************************************************************
5819 * HTTP_Connect (internal)
5821 * Create http session handle
5823 * RETURNS
5824 * HINTERNET a session handle on success
5825 * NULL on failure
5828 DWORD HTTP_Connect(appinfo_t *hIC, LPCWSTR lpszServerName,
5829 INTERNET_PORT serverPort, LPCWSTR lpszUserName,
5830 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
5831 DWORD dwInternalFlags, HINTERNET *ret)
5833 http_session_t *session = NULL;
5835 TRACE("-->\n");
5837 if (!lpszServerName || !lpszServerName[0])
5838 return ERROR_INVALID_PARAMETER;
5840 assert( hIC->hdr.htype == WH_HINIT );
5842 session = alloc_object(&hIC->hdr, &HTTPSESSIONVtbl, sizeof(http_session_t));
5843 if (!session)
5844 return ERROR_OUTOFMEMORY;
5847 * According to my tests. The name is not resolved until a request is sent
5850 session->hdr.htype = WH_HHTTPSESSION;
5851 session->hdr.dwFlags = dwFlags;
5852 session->hdr.dwContext = dwContext;
5853 session->hdr.dwInternalFlags |= dwInternalFlags;
5855 WININET_AddRef( &hIC->hdr );
5856 session->appInfo = hIC;
5857 list_add_head( &hIC->hdr.children, &session->hdr.entry );
5859 session->hostName = heap_strdupW(lpszServerName);
5860 if (lpszUserName && lpszUserName[0])
5861 session->userName = heap_strdupW(lpszUserName);
5862 if (lpszPassword && lpszPassword[0])
5863 session->password = heap_strdupW(lpszPassword);
5864 session->hostPort = serverPort;
5865 session->connect_timeout = hIC->connect_timeout;
5866 session->send_timeout = 0;
5867 session->receive_timeout = 0;
5869 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
5870 if (!(session->hdr.dwInternalFlags & INET_OPENURL))
5872 INTERNET_SendCallback(&hIC->hdr, dwContext,
5873 INTERNET_STATUS_HANDLE_CREATED, &session->hdr.hInternet,
5874 sizeof(HINTERNET));
5878 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
5879 * windows
5882 TRACE("%p --> %p\n", hIC, session);
5884 *ret = session->hdr.hInternet;
5885 return ERROR_SUCCESS;
5888 /***********************************************************************
5889 * HTTP_clear_response_headers (internal)
5891 * clear out any old response headers
5893 static void HTTP_clear_response_headers( http_request_t *request )
5895 DWORD i;
5897 EnterCriticalSection( &request->headers_section );
5899 for( i=0; i<request->nCustHeaders; i++)
5901 if( !request->custHeaders[i].lpszField )
5902 continue;
5903 if( !request->custHeaders[i].lpszValue )
5904 continue;
5905 if ( request->custHeaders[i].wFlags & HDR_ISREQUEST )
5906 continue;
5907 HTTP_DeleteCustomHeader( request, i );
5908 i--;
5911 LeaveCriticalSection( &request->headers_section );
5914 /***********************************************************************
5915 * HTTP_GetResponseHeaders (internal)
5917 * Read server response
5919 * RETURNS
5921 * TRUE on success
5922 * FALSE on error
5924 static DWORD HTTP_GetResponseHeaders(http_request_t *request, INT *len)
5926 INT cbreaks = 0;
5927 WCHAR buffer[MAX_REPLY_LEN];
5928 DWORD buflen = MAX_REPLY_LEN;
5929 INT rc = 0;
5930 char bufferA[MAX_REPLY_LEN];
5931 LPWSTR status_code = NULL, status_text = NULL;
5932 DWORD res = ERROR_HTTP_INVALID_SERVER_RESPONSE;
5933 BOOL codeHundred = FALSE;
5935 TRACE("-->\n");
5937 if(!is_valid_netconn(request->netconn))
5938 goto lend;
5940 /* clear old response headers (eg. from a redirect response) */
5941 HTTP_clear_response_headers( request );
5943 NETCON_set_timeout( request->netconn, FALSE, request->receive_timeout );
5944 do {
5946 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
5948 buflen = MAX_REPLY_LEN;
5949 if ((res = read_line(request, bufferA, &buflen)))
5950 goto lend;
5952 if (!buflen) goto lend;
5954 rc += buflen;
5955 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
5956 /* check is this a status code line? */
5957 if (!strncmpW(buffer, g_szHttp1_0, 4))
5959 /* split the version from the status code */
5960 status_code = strchrW( buffer, ' ' );
5961 if( !status_code )
5962 goto lend;
5963 *status_code++=0;
5965 /* split the status code from the status text */
5966 status_text = strchrW( status_code, ' ' );
5967 if( status_text )
5968 *status_text++=0;
5970 request->status_code = atoiW(status_code);
5972 TRACE("version [%s] status code [%s] status text [%s]\n",
5973 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
5975 codeHundred = request->status_code == HTTP_STATUS_CONTINUE;
5977 else if (!codeHundred)
5979 WARN("No status line at head of response (%s)\n", debugstr_w(buffer));
5981 heap_free(request->version);
5982 heap_free(request->statusText);
5984 request->status_code = HTTP_STATUS_OK;
5985 request->version = heap_strdupW(g_szHttp1_0);
5986 request->statusText = heap_strdupW(szOK);
5988 goto lend;
5990 } while (codeHundred);
5992 /* Add status code */
5993 HTTP_ProcessHeader(request, szStatus, status_code,
5994 HTTP_ADDHDR_FLAG_REPLACE);
5996 heap_free(request->version);
5997 heap_free(request->statusText);
5999 request->version = heap_strdupW(buffer);
6000 request->statusText = heap_strdupW(status_text ? status_text : emptyW);
6002 /* Restore the spaces */
6003 *(status_code-1) = ' ';
6004 if (status_text)
6005 *(status_text-1) = ' ';
6007 /* Parse each response line */
6010 buflen = MAX_REPLY_LEN;
6011 if (!read_line(request, bufferA, &buflen) && buflen)
6013 LPWSTR * pFieldAndValue;
6015 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
6017 if (!bufferA[0]) break;
6018 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
6020 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
6021 if (pFieldAndValue)
6023 HTTP_ProcessHeader(request, pFieldAndValue[0], pFieldAndValue[1],
6024 HTTP_ADDREQ_FLAG_ADD );
6025 HTTP_FreeTokens(pFieldAndValue);
6028 else
6030 cbreaks++;
6031 if (cbreaks >= 2)
6032 break;
6034 }while(1);
6036 res = ERROR_SUCCESS;
6038 lend:
6040 *len = rc;
6041 TRACE("<--\n");
6042 return res;
6045 /***********************************************************************
6046 * HTTP_InterpretHttpHeader (internal)
6048 * Parse server response
6050 * RETURNS
6052 * Pointer to array of field, value, NULL on success.
6053 * NULL on error.
6055 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
6057 LPWSTR * pTokenPair;
6058 LPWSTR pszColon;
6059 INT len;
6061 pTokenPair = heap_alloc_zero(sizeof(*pTokenPair)*3);
6063 pszColon = strchrW(buffer, ':');
6064 /* must have two tokens */
6065 if (!pszColon)
6067 HTTP_FreeTokens(pTokenPair);
6068 if (buffer[0])
6069 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
6070 return NULL;
6073 pTokenPair[0] = heap_alloc((pszColon - buffer + 1) * sizeof(WCHAR));
6074 if (!pTokenPair[0])
6076 HTTP_FreeTokens(pTokenPair);
6077 return NULL;
6079 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
6080 pTokenPair[0][pszColon - buffer] = '\0';
6082 /* skip colon */
6083 pszColon++;
6084 len = strlenW(pszColon);
6085 pTokenPair[1] = heap_alloc((len + 1) * sizeof(WCHAR));
6086 if (!pTokenPair[1])
6088 HTTP_FreeTokens(pTokenPair);
6089 return NULL;
6091 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
6093 strip_spaces(pTokenPair[0]);
6094 strip_spaces(pTokenPair[1]);
6096 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
6097 return pTokenPair;
6100 /***********************************************************************
6101 * HTTP_ProcessHeader (internal)
6103 * Stuff header into header tables according to <dwModifier>
6107 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
6109 static DWORD HTTP_ProcessHeader(http_request_t *request, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
6111 LPHTTPHEADERW lphttpHdr = NULL;
6112 INT index;
6113 BOOL request_only = !!(dwModifier & HTTP_ADDHDR_FLAG_REQ);
6114 DWORD res = ERROR_HTTP_INVALID_HEADER;
6116 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), dwModifier);
6118 EnterCriticalSection( &request->headers_section );
6120 /* REPLACE wins out over ADD */
6121 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
6122 dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
6124 if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
6125 index = -1;
6126 else
6127 index = HTTP_GetCustomHeaderIndex(request, field, 0, request_only);
6129 if (index >= 0)
6131 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
6133 LeaveCriticalSection( &request->headers_section );
6134 return ERROR_HTTP_INVALID_HEADER;
6136 lphttpHdr = &request->custHeaders[index];
6138 else if (value)
6140 HTTPHEADERW hdr;
6142 hdr.lpszField = (LPWSTR)field;
6143 hdr.lpszValue = (LPWSTR)value;
6144 hdr.wFlags = hdr.wCount = 0;
6146 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6147 hdr.wFlags |= HDR_ISREQUEST;
6149 res = HTTP_InsertCustomHeader(request, &hdr);
6150 LeaveCriticalSection( &request->headers_section );
6151 return res;
6153 /* no value to delete */
6154 else
6156 LeaveCriticalSection( &request->headers_section );
6157 return ERROR_SUCCESS;
6160 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6161 lphttpHdr->wFlags |= HDR_ISREQUEST;
6162 else
6163 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
6165 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
6167 HTTP_DeleteCustomHeader( request, index );
6169 if (value && value[0])
6171 HTTPHEADERW hdr;
6173 hdr.lpszField = (LPWSTR)field;
6174 hdr.lpszValue = (LPWSTR)value;
6175 hdr.wFlags = hdr.wCount = 0;
6177 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6178 hdr.wFlags |= HDR_ISREQUEST;
6180 res = HTTP_InsertCustomHeader(request, &hdr);
6181 LeaveCriticalSection( &request->headers_section );
6182 return res;
6185 LeaveCriticalSection( &request->headers_section );
6186 return ERROR_SUCCESS;
6188 else if (dwModifier & COALESCEFLAGS)
6190 LPWSTR lpsztmp;
6191 WCHAR ch = 0;
6192 INT len = 0;
6193 INT origlen = strlenW(lphttpHdr->lpszValue);
6194 INT valuelen = strlenW(value);
6196 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
6198 ch = ',';
6199 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
6201 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
6203 ch = ';';
6204 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
6207 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
6209 lpsztmp = heap_realloc(lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
6210 if (lpsztmp)
6212 lphttpHdr->lpszValue = lpsztmp;
6213 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
6214 if (ch > 0)
6216 lphttpHdr->lpszValue[origlen] = ch;
6217 origlen++;
6218 lphttpHdr->lpszValue[origlen] = ' ';
6219 origlen++;
6222 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
6223 lphttpHdr->lpszValue[len] = '\0';
6224 res = ERROR_SUCCESS;
6226 else
6228 WARN("heap_realloc (%d bytes) failed\n",len+1);
6229 res = ERROR_OUTOFMEMORY;
6232 TRACE("<-- %d\n", res);
6233 LeaveCriticalSection( &request->headers_section );
6234 return res;
6237 /***********************************************************************
6238 * HTTP_GetCustomHeaderIndex (internal)
6240 * Return index of custom header from header array
6241 * Headers section must be held
6243 static INT HTTP_GetCustomHeaderIndex(http_request_t *request, LPCWSTR lpszField,
6244 int requested_index, BOOL request_only)
6246 DWORD index;
6248 TRACE("%s, %d, %d\n", debugstr_w(lpszField), requested_index, request_only);
6250 for (index = 0; index < request->nCustHeaders; index++)
6252 if (strcmpiW(request->custHeaders[index].lpszField, lpszField))
6253 continue;
6255 if (request_only && !(request->custHeaders[index].wFlags & HDR_ISREQUEST))
6256 continue;
6258 if (!request_only && (request->custHeaders[index].wFlags & HDR_ISREQUEST))
6259 continue;
6261 if (requested_index == 0)
6262 break;
6263 requested_index --;
6266 if (index >= request->nCustHeaders)
6267 index = -1;
6269 TRACE("Return: %d\n", index);
6270 return index;
6274 /***********************************************************************
6275 * HTTP_InsertCustomHeader (internal)
6277 * Insert header into array
6278 * Headers section must be held
6280 static DWORD HTTP_InsertCustomHeader(http_request_t *request, LPHTTPHEADERW lpHdr)
6282 INT count;
6283 LPHTTPHEADERW lph = NULL;
6285 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
6286 count = request->nCustHeaders + 1;
6287 if (count > 1)
6288 lph = heap_realloc_zero(request->custHeaders, sizeof(HTTPHEADERW) * count);
6289 else
6290 lph = heap_alloc_zero(sizeof(HTTPHEADERW) * count);
6292 if (!lph)
6293 return ERROR_OUTOFMEMORY;
6295 request->custHeaders = lph;
6296 request->custHeaders[count-1].lpszField = heap_strdupW(lpHdr->lpszField);
6297 request->custHeaders[count-1].lpszValue = heap_strdupW(lpHdr->lpszValue);
6298 request->custHeaders[count-1].wFlags = lpHdr->wFlags;
6299 request->custHeaders[count-1].wCount= lpHdr->wCount;
6300 request->nCustHeaders++;
6302 return ERROR_SUCCESS;
6306 /***********************************************************************
6307 * HTTP_DeleteCustomHeader (internal)
6309 * Delete header from array
6310 * If this function is called, the index may change.
6311 * Headers section must be held
6313 static BOOL HTTP_DeleteCustomHeader(http_request_t *request, DWORD index)
6315 if( request->nCustHeaders <= 0 )
6316 return FALSE;
6317 if( index >= request->nCustHeaders )
6318 return FALSE;
6319 request->nCustHeaders--;
6321 heap_free(request->custHeaders[index].lpszField);
6322 heap_free(request->custHeaders[index].lpszValue);
6324 memmove( &request->custHeaders[index], &request->custHeaders[index+1],
6325 (request->nCustHeaders - index)* sizeof(HTTPHEADERW) );
6326 memset( &request->custHeaders[request->nCustHeaders], 0, sizeof(HTTPHEADERW) );
6328 return TRUE;
6332 /***********************************************************************
6333 * IsHostInProxyBypassList (@)
6335 * Undocumented
6338 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
6340 FIXME("STUB: flags=%d host=%s length=%d\n",flags,szHost,length);
6341 return FALSE;