[WINHTTP_WINETEST] Sync with Wine Staging 1.7.55. CORE-10536
[reactos.git] / reactos / dll / win32 / wininet / http.c
1 /*
2 * Wininet - HTTP Implementation
3 *
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
11 *
12 * Ulrich Czekalla
13 * David Hammerton
14 *
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.
19 *
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.
24 *
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
28 */
29
30 #include "internet.h"
31
32 #ifdef HAVE_ZLIB
33 # include <zlib.h>
34 #endif
35
36 #include <winternl.h>
37
38 #include <wine/exception.h>
39
40 static const WCHAR g_szHttp1_0[] = {'H','T','T','P','/','1','.','0',0};
41 static const WCHAR g_szHttp1_1[] = {'H','T','T','P','/','1','.','1',0};
42 static const WCHAR szOK[] = {'O','K',0};
43 static const WCHAR hostW[] = { 'H','o','s','t',0 };
44 static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
45 static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
46 static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 };
47 static const WCHAR szKeepAlive[] = {'K','e','e','p','-','A','l','i','v','e',0};
48 static const WCHAR szGET[] = { 'G','E','T', 0 };
49 static const WCHAR szHEAD[] = { 'H','E','A','D', 0 };
50
51 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
52 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
53 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
54 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
55 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
56 static const WCHAR szAge[] = { 'A','g','e',0 };
57 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
58 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
59 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
60 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
61 static const WCHAR szContent_Disposition[] = { 'C','o','n','t','e','n','t','-','D','i','s','p','o','s','i','t','i','o','n',0 };
62 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
63 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
64 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
65 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
66 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
67 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
68 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
69 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 };
70 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
71 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
72 static const WCHAR szDate[] = { 'D','a','t','e',0 };
73 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
74 static const WCHAR szETag[] = { 'E','T','a','g',0 };
75 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
76 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
77 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
78 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
79 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
80 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
81 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
82 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
83 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
84 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
85 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
86 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
87 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
88 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
89 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
90 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
91 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
92 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
93 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
94 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
95 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
96 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 };
97 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
98 static const WCHAR szURI[] = { 'U','R','I',0 };
99 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
100 static const WCHAR szVary[] = { 'V','a','r','y',0 };
101 static const WCHAR szVia[] = { 'V','i','a',0 };
102 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
103 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
104
105 static const WCHAR emptyW[] = {0};
106
107 #define HTTP_REFERER szReferer
108 #define HTTP_ACCEPT szAccept
109 #define HTTP_USERAGENT szUser_Agent
110
111 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
112 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
113 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
114 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
115 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
116 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
117 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
118
119 #define COLLECT_TIME 60000
120
121 #undef ARRAYSIZE
122 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
123
124 struct HttpAuthInfo
125 {
126 LPWSTR scheme;
127 CredHandle cred;
128 CtxtHandle ctx;
129 TimeStamp exp;
130 ULONG attr;
131 ULONG max_token;
132 void *auth_data;
133 unsigned int auth_data_len;
134 BOOL finished; /* finished authenticating */
135 };
136
137
138 typedef struct _basicAuthorizationData
139 {
140 struct list entry;
141
142 LPWSTR host;
143 LPWSTR realm;
144 LPSTR authorization;
145 UINT authorizationLen;
146 } basicAuthorizationData;
147
148 typedef struct _authorizationData
149 {
150 struct list entry;
151
152 LPWSTR host;
153 LPWSTR scheme;
154 LPWSTR domain;
155 UINT domain_len;
156 LPWSTR user;
157 UINT user_len;
158 LPWSTR password;
159 UINT password_len;
160 } authorizationData;
161
162 static struct list basicAuthorizationCache = LIST_INIT(basicAuthorizationCache);
163 static struct list authorizationCache = LIST_INIT(authorizationCache);
164
165 static CRITICAL_SECTION authcache_cs;
166 static CRITICAL_SECTION_DEBUG critsect_debug =
167 {
168 0, 0, &authcache_cs,
169 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
170 0, 0, { (DWORD_PTR)(__FILE__ ": authcache_cs") }
171 };
172 static CRITICAL_SECTION authcache_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
173
174 static DWORD HTTP_GetResponseHeaders(http_request_t *req, INT *len);
175 static DWORD HTTP_ProcessHeader(http_request_t *req, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
176 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
177 static DWORD HTTP_InsertCustomHeader(http_request_t *req, LPHTTPHEADERW lpHdr);
178 static INT HTTP_GetCustomHeaderIndex(http_request_t *req, LPCWSTR lpszField, INT index, BOOL Request);
179 static BOOL HTTP_DeleteCustomHeader(http_request_t *req, DWORD index);
180 static LPWSTR HTTP_build_req( LPCWSTR *list, int len );
181 static DWORD HTTP_HttpQueryInfoW(http_request_t*, DWORD, LPVOID, LPDWORD, LPDWORD);
182 static LPWSTR HTTP_GetRedirectURL(http_request_t *req, LPCWSTR lpszUrl);
183 static UINT HTTP_DecodeBase64(LPCWSTR base64, LPSTR bin);
184 static BOOL drain_content(http_request_t*,BOOL);
185
186 static CRITICAL_SECTION connection_pool_cs;
187 static CRITICAL_SECTION_DEBUG connection_pool_debug =
188 {
189 0, 0, &connection_pool_cs,
190 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
191 0, 0, { (DWORD_PTR)(__FILE__ ": connection_pool_cs") }
192 };
193 static CRITICAL_SECTION connection_pool_cs = { &connection_pool_debug, -1, 0, 0, 0, 0 };
194
195 static struct list connection_pool = LIST_INIT(connection_pool);
196 static BOOL collector_running;
197
198 void server_addref(server_t *server)
199 {
200 InterlockedIncrement(&server->ref);
201 }
202
203 void server_release(server_t *server)
204 {
205 if(InterlockedDecrement(&server->ref))
206 return;
207
208 #ifdef __REACTOS__
209 EnterCriticalSection(&connection_pool_cs);
210 #endif
211 list_remove(&server->entry);
212 #ifdef __REACTOS__
213 LeaveCriticalSection(&connection_pool_cs);
214 #endif
215
216 if(server->cert_chain)
217 CertFreeCertificateChain(server->cert_chain);
218 heap_free(server->name);
219 heap_free(server->scheme_host_port);
220 heap_free(server);
221 }
222
223 static BOOL process_host_port(server_t *server)
224 {
225 BOOL default_port;
226 size_t name_len;
227 WCHAR *buf;
228
229 static const WCHAR httpW[] = {'h','t','t','p',0};
230 static const WCHAR httpsW[] = {'h','t','t','p','s',0};
231 static const WCHAR formatW[] = {'%','s',':','/','/','%','s',':','%','u',0};
232
233 name_len = strlenW(server->name);
234 buf = heap_alloc((name_len + 10 /* strlen("://:<port>") */)*sizeof(WCHAR) + sizeof(httpsW));
235 if(!buf)
236 return FALSE;
237
238 sprintfW(buf, formatW, server->is_https ? httpsW : httpW, server->name, server->port);
239 server->scheme_host_port = buf;
240
241 server->host_port = server->scheme_host_port + 7 /* strlen("http://") */;
242 if(server->is_https)
243 server->host_port++;
244
245 default_port = server->port == (server->is_https ? INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT);
246 server->canon_host_port = default_port ? server->name : server->host_port;
247 return TRUE;
248 }
249
250 server_t *get_server(const WCHAR *name, INTERNET_PORT port, BOOL is_https, BOOL do_create)
251 {
252 server_t *iter, *server = NULL;
253
254 if(port == INTERNET_INVALID_PORT_NUMBER)
255 port = INTERNET_DEFAULT_HTTP_PORT;
256
257 EnterCriticalSection(&connection_pool_cs);
258
259 LIST_FOR_EACH_ENTRY(iter, &connection_pool, server_t, entry) {
260 if(iter->port == port && !strcmpW(iter->name, name) && iter->is_https == is_https) {
261 server = iter;
262 server_addref(server);
263 break;
264 }
265 }
266
267 if(!server && do_create) {
268 server = heap_alloc_zero(sizeof(*server));
269 if(server) {
270 server->ref = 2; /* list reference and return */
271 server->port = port;
272 server->is_https = is_https;
273 list_init(&server->conn_pool);
274 server->name = heap_strdupW(name);
275 if(server->name && process_host_port(server)) {
276 list_add_head(&connection_pool, &server->entry);
277 }else {
278 heap_free(server);
279 server = NULL;
280 }
281 }
282 }
283
284 LeaveCriticalSection(&connection_pool_cs);
285
286 return server;
287 }
288
289 BOOL collect_connections(collect_type_t collect_type)
290 {
291 netconn_t *netconn, *netconn_safe;
292 server_t *server, *server_safe;
293 BOOL remaining = FALSE;
294 DWORD64 now;
295
296 #ifdef __REACTOS__
297 now = GetTickCount();
298 #else
299 now = GetTickCount64();
300 #endif
301
302 LIST_FOR_EACH_ENTRY_SAFE(server, server_safe, &connection_pool, server_t, entry) {
303 LIST_FOR_EACH_ENTRY_SAFE(netconn, netconn_safe, &server->conn_pool, netconn_t, pool_entry) {
304 if(collect_type > COLLECT_TIMEOUT || netconn->keep_until < now) {
305 TRACE("freeing %p\n", netconn);
306 list_remove(&netconn->pool_entry);
307 free_netconn(netconn);
308 }else {
309 remaining = TRUE;
310 }
311 }
312
313 if(collect_type == COLLECT_CLEANUP) {
314 list_remove(&server->entry);
315 list_init(&server->entry);
316 server_release(server);
317 }
318 }
319
320 return remaining;
321 }
322
323 static DWORD WINAPI collect_connections_proc(void *arg)
324 {
325 BOOL remaining_conns;
326
327 do {
328 /* FIXME: Use more sophisticated method */
329 Sleep(5000);
330
331 EnterCriticalSection(&connection_pool_cs);
332
333 remaining_conns = collect_connections(COLLECT_TIMEOUT);
334 if(!remaining_conns)
335 collector_running = FALSE;
336
337 LeaveCriticalSection(&connection_pool_cs);
338 }while(remaining_conns);
339
340 FreeLibraryAndExitThread(WININET_hModule, 0);
341 }
342
343 /***********************************************************************
344 * HTTP_GetHeader (internal)
345 *
346 * Headers section must be held
347 */
348 static LPHTTPHEADERW HTTP_GetHeader(http_request_t *req, LPCWSTR head)
349 {
350 int HeaderIndex = 0;
351 HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
352 if (HeaderIndex == -1)
353 return NULL;
354 else
355 return &req->custHeaders[HeaderIndex];
356 }
357
358 static WCHAR *get_host_header( http_request_t *req )
359 {
360 HTTPHEADERW *header;
361 WCHAR *ret = NULL;
362
363 EnterCriticalSection( &req->headers_section );
364 if ((header = HTTP_GetHeader( req, hostW ))) ret = heap_strdupW( header->lpszValue );
365 else ret = heap_strdupW( req->server->canon_host_port );
366 LeaveCriticalSection( &req->headers_section );
367 return ret;
368 }
369
370 typedef enum {
371 BLOCKING_ALLOW,
372 BLOCKING_DISALLOW,
373 BLOCKING_WAITALL
374 } blocking_mode_t;
375
376 struct data_stream_vtbl_t {
377 DWORD (*get_avail_data)(data_stream_t*,http_request_t*);
378 BOOL (*end_of_data)(data_stream_t*,http_request_t*);
379 DWORD (*read)(data_stream_t*,http_request_t*,BYTE*,DWORD,DWORD*,blocking_mode_t);
380 BOOL (*drain_content)(data_stream_t*,http_request_t*);
381 void (*destroy)(data_stream_t*);
382 };
383
384 typedef struct {
385 data_stream_t data_stream;
386
387 BYTE buf[READ_BUFFER_SIZE];
388 DWORD buf_size;
389 DWORD buf_pos;
390 DWORD chunk_size;
391 BOOL end_of_data;
392 } chunked_stream_t;
393
394 static inline void destroy_data_stream(data_stream_t *stream)
395 {
396 stream->vtbl->destroy(stream);
397 }
398
399 static void reset_data_stream(http_request_t *req)
400 {
401 destroy_data_stream(req->data_stream);
402 req->data_stream = &req->netconn_stream.data_stream;
403 req->read_pos = req->read_size = req->netconn_stream.content_read = 0;
404 req->read_gzip = FALSE;
405 }
406
407 static void remove_header( http_request_t *request, const WCHAR *str, BOOL from_request )
408 {
409 int index;
410 EnterCriticalSection( &request->headers_section );
411 index = HTTP_GetCustomHeaderIndex( request, str, 0, from_request );
412 if (index != -1) HTTP_DeleteCustomHeader( request, index );
413 LeaveCriticalSection( &request->headers_section );
414 }
415
416 #ifdef HAVE_ZLIB
417
418 typedef struct {
419 data_stream_t stream;
420 data_stream_t *parent_stream;
421 z_stream zstream;
422 BYTE buf[READ_BUFFER_SIZE];
423 DWORD buf_size;
424 DWORD buf_pos;
425 BOOL end_of_data;
426 } gzip_stream_t;
427
428 static DWORD gzip_get_avail_data(data_stream_t *stream, http_request_t *req)
429 {
430 /* Allow reading only from read buffer */
431 return 0;
432 }
433
434 static BOOL gzip_end_of_data(data_stream_t *stream, http_request_t *req)
435 {
436 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
437 return gzip_stream->end_of_data
438 || (!gzip_stream->buf_size && gzip_stream->parent_stream->vtbl->end_of_data(gzip_stream->parent_stream, req));
439 }
440
441 static DWORD gzip_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
442 DWORD *read, blocking_mode_t blocking_mode)
443 {
444 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
445 z_stream *zstream = &gzip_stream->zstream;
446 DWORD current_read, ret_read = 0;
447 int zres;
448 DWORD res = ERROR_SUCCESS;
449
450 TRACE("(%d %d)\n", size, blocking_mode);
451
452 while(size && !gzip_stream->end_of_data) {
453 if(!gzip_stream->buf_size) {
454 if(gzip_stream->buf_pos) {
455 if(gzip_stream->buf_size)
456 memmove(gzip_stream->buf, gzip_stream->buf+gzip_stream->buf_pos, gzip_stream->buf_size);
457 gzip_stream->buf_pos = 0;
458 }
459 res = gzip_stream->parent_stream->vtbl->read(gzip_stream->parent_stream, req, gzip_stream->buf+gzip_stream->buf_size,
460 sizeof(gzip_stream->buf)-gzip_stream->buf_size, &current_read, blocking_mode);
461 gzip_stream->buf_size += current_read;
462 if(res != ERROR_SUCCESS)
463 break;
464
465 if(!current_read) {
466 if(blocking_mode != BLOCKING_DISALLOW) {
467 WARN("unexpected end of data\n");
468 gzip_stream->end_of_data = TRUE;
469 }
470 break;
471 }
472 }
473
474 zstream->next_in = gzip_stream->buf+gzip_stream->buf_pos;
475 zstream->avail_in = gzip_stream->buf_size;
476 zstream->next_out = buf+ret_read;
477 zstream->avail_out = size;
478 zres = inflate(&gzip_stream->zstream, 0);
479 current_read = size - zstream->avail_out;
480 size -= current_read;
481 ret_read += current_read;
482 gzip_stream->buf_size -= zstream->next_in - (gzip_stream->buf+gzip_stream->buf_pos);
483 gzip_stream->buf_pos = zstream->next_in-gzip_stream->buf;
484 if(zres == Z_STREAM_END) {
485 TRACE("end of data\n");
486 gzip_stream->end_of_data = TRUE;
487 inflateEnd(zstream);
488 }else if(zres != Z_OK) {
489 WARN("inflate failed %d: %s\n", zres, debugstr_a(zstream->msg));
490 if(!ret_read)
491 res = ERROR_INTERNET_DECODING_FAILED;
492 break;
493 }
494
495 if(ret_read && blocking_mode == BLOCKING_ALLOW)
496 blocking_mode = BLOCKING_DISALLOW;
497 }
498
499 TRACE("read %u bytes\n", ret_read);
500 *read = ret_read;
501 return res;
502 }
503
504 static BOOL gzip_drain_content(data_stream_t *stream, http_request_t *req)
505 {
506 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
507 return gzip_stream->parent_stream->vtbl->drain_content(gzip_stream->parent_stream, req);
508 }
509
510 static void gzip_destroy(data_stream_t *stream)
511 {
512 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
513
514 destroy_data_stream(gzip_stream->parent_stream);
515
516 if(!gzip_stream->end_of_data)
517 inflateEnd(&gzip_stream->zstream);
518 heap_free(gzip_stream);
519 }
520
521 static const data_stream_vtbl_t gzip_stream_vtbl = {
522 gzip_get_avail_data,
523 gzip_end_of_data,
524 gzip_read,
525 gzip_drain_content,
526 gzip_destroy
527 };
528
529 static voidpf wininet_zalloc(voidpf opaque, uInt items, uInt size)
530 {
531 return heap_alloc(items*size);
532 }
533
534 static void wininet_zfree(voidpf opaque, voidpf address)
535 {
536 heap_free(address);
537 }
538
539 static DWORD init_gzip_stream(http_request_t *req, BOOL is_gzip)
540 {
541 gzip_stream_t *gzip_stream;
542 int zres;
543
544 gzip_stream = heap_alloc_zero(sizeof(gzip_stream_t));
545 if(!gzip_stream)
546 return ERROR_OUTOFMEMORY;
547
548 gzip_stream->stream.vtbl = &gzip_stream_vtbl;
549 gzip_stream->zstream.zalloc = wininet_zalloc;
550 gzip_stream->zstream.zfree = wininet_zfree;
551
552 zres = inflateInit2(&gzip_stream->zstream, is_gzip ? 0x1f : -15);
553 if(zres != Z_OK) {
554 ERR("inflateInit failed: %d\n", zres);
555 heap_free(gzip_stream);
556 return ERROR_OUTOFMEMORY;
557 }
558
559 remove_header(req, szContent_Length, FALSE);
560
561 if(req->read_size) {
562 memcpy(gzip_stream->buf, req->read_buf+req->read_pos, req->read_size);
563 gzip_stream->buf_size = req->read_size;
564 req->read_pos = req->read_size = 0;
565 }
566
567 req->read_gzip = TRUE;
568 gzip_stream->parent_stream = req->data_stream;
569 req->data_stream = &gzip_stream->stream;
570 return ERROR_SUCCESS;
571 }
572
573 #else
574
575 static DWORD init_gzip_stream(http_request_t *req, BOOL is_gzip)
576 {
577 ERR("gzip stream not supported, missing zlib.\n");
578 return ERROR_SUCCESS;
579 }
580
581 #endif
582
583 /***********************************************************************
584 * HTTP_FreeTokens (internal)
585 *
586 * Frees table of pointers.
587 */
588 static void HTTP_FreeTokens(LPWSTR * token_array)
589 {
590 int i;
591 for (i = 0; token_array[i]; i++) heap_free(token_array[i]);
592 heap_free(token_array);
593 }
594
595 static void HTTP_FixURL(http_request_t *request)
596 {
597 static const WCHAR szSlash[] = { '/',0 };
598 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
599
600 /* If we don't have a path we set it to root */
601 if (NULL == request->path)
602 request->path = heap_strdupW(szSlash);
603 else /* remove \r and \n*/
604 {
605 int nLen = strlenW(request->path);
606 while ((nLen >0 ) && ((request->path[nLen-1] == '\r')||(request->path[nLen-1] == '\n')))
607 {
608 nLen--;
609 request->path[nLen]='\0';
610 }
611 /* Replace '\' with '/' */
612 while (nLen>0) {
613 nLen--;
614 if (request->path[nLen] == '\\') request->path[nLen]='/';
615 }
616 }
617
618 if(CSTR_EQUAL != CompareStringW( LOCALE_INVARIANT, NORM_IGNORECASE,
619 request->path, strlenW(request->path), szHttp, strlenW(szHttp) )
620 && request->path[0] != '/') /* not an absolute path ?? --> fix it !! */
621 {
622 WCHAR *fixurl = heap_alloc((strlenW(request->path) + 2)*sizeof(WCHAR));
623 *fixurl = '/';
624 strcpyW(fixurl + 1, request->path);
625 heap_free( request->path );
626 request->path = fixurl;
627 }
628 }
629
630 static WCHAR* build_request_header(http_request_t *request, const WCHAR *verb,
631 const WCHAR *path, const WCHAR *version, BOOL use_cr)
632 {
633 static const WCHAR szSpace[] = {' ',0};
634 static const WCHAR szColon[] = {':',' ',0};
635 static const WCHAR szCr[] = {'\r',0};
636 static const WCHAR szLf[] = {'\n',0};
637 LPWSTR requestString;
638 DWORD len, n;
639 LPCWSTR *req;
640 UINT i;
641
642 EnterCriticalSection( &request->headers_section );
643
644 /* allocate space for an array of all the string pointers to be added */
645 len = request->nCustHeaders * 5 + 10;
646 if (!(req = heap_alloc( len * sizeof(const WCHAR *) )))
647 {
648 LeaveCriticalSection( &request->headers_section );
649 return NULL;
650 }
651
652 /* add the verb, path and HTTP version string */
653 n = 0;
654 req[n++] = verb;
655 req[n++] = szSpace;
656 req[n++] = path;
657 req[n++] = szSpace;
658 req[n++] = version;
659 if (use_cr)
660 req[n++] = szCr;
661 req[n++] = szLf;
662
663 /* Append custom request headers */
664 for (i = 0; i < request->nCustHeaders; i++)
665 {
666 if (request->custHeaders[i].wFlags & HDR_ISREQUEST)
667 {
668 req[n++] = request->custHeaders[i].lpszField;
669 req[n++] = szColon;
670 req[n++] = request->custHeaders[i].lpszValue;
671 if (use_cr)
672 req[n++] = szCr;
673 req[n++] = szLf;
674
675 TRACE("Adding custom header %s (%s)\n",
676 debugstr_w(request->custHeaders[i].lpszField),
677 debugstr_w(request->custHeaders[i].lpszValue));
678 }
679 }
680 if (use_cr)
681 req[n++] = szCr;
682 req[n++] = szLf;
683 req[n] = NULL;
684
685 requestString = HTTP_build_req( req, 4 );
686 heap_free( req );
687 LeaveCriticalSection( &request->headers_section );
688 return requestString;
689 }
690
691 static WCHAR* build_response_header(http_request_t *request, BOOL use_cr)
692 {
693 static const WCHAR colonW[] = { ':',' ',0 };
694 static const WCHAR crW[] = { '\r',0 };
695 static const WCHAR lfW[] = { '\n',0 };
696 static const WCHAR status_fmt[] = { ' ','%','u',' ',0 };
697 const WCHAR **req;
698 WCHAR *ret, buf[14];
699 DWORD i, n = 0;
700
701 EnterCriticalSection( &request->headers_section );
702
703 if (!(req = heap_alloc( (request->nCustHeaders * 5 + 8) * sizeof(WCHAR *) )))
704 {
705 LeaveCriticalSection( &request->headers_section );
706 return NULL;
707 }
708
709 if (request->status_code)
710 {
711 req[n++] = request->version;
712 sprintfW(buf, status_fmt, request->status_code);
713 req[n++] = buf;
714 req[n++] = request->statusText;
715 if (use_cr)
716 req[n++] = crW;
717 req[n++] = lfW;
718 }
719
720 for(i = 0; i < request->nCustHeaders; i++)
721 {
722 if(!(request->custHeaders[i].wFlags & HDR_ISREQUEST)
723 && strcmpW(request->custHeaders[i].lpszField, szStatus))
724 {
725 req[n++] = request->custHeaders[i].lpszField;
726 req[n++] = colonW;
727 req[n++] = request->custHeaders[i].lpszValue;
728 if(use_cr)
729 req[n++] = crW;
730 req[n++] = lfW;
731
732 TRACE("Adding custom header %s (%s)\n",
733 debugstr_w(request->custHeaders[i].lpszField),
734 debugstr_w(request->custHeaders[i].lpszValue));
735 }
736 }
737 if(use_cr)
738 req[n++] = crW;
739 req[n++] = lfW;
740 req[n] = NULL;
741
742 ret = HTTP_build_req(req, 0);
743 heap_free(req);
744 LeaveCriticalSection( &request->headers_section );
745 return ret;
746 }
747
748 static void HTTP_ProcessCookies( http_request_t *request )
749 {
750 int HeaderIndex;
751 int numCookies = 0;
752 LPHTTPHEADERW setCookieHeader;
753 WCHAR *path, *tmp;
754
755 if(request->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES)
756 return;
757
758 path = heap_strdupW(request->path);
759 if (!path)
760 return;
761
762 tmp = strrchrW(path, '/');
763 if (tmp && tmp[1]) tmp[1] = 0;
764
765 EnterCriticalSection( &request->headers_section );
766
767 while((HeaderIndex = HTTP_GetCustomHeaderIndex(request, szSet_Cookie, numCookies++, FALSE)) != -1)
768 {
769 const WCHAR *data;
770 WCHAR *name;
771
772 setCookieHeader = &request->custHeaders[HeaderIndex];
773
774 if (!setCookieHeader->lpszValue)
775 continue;
776
777 data = strchrW(setCookieHeader->lpszValue, '=');
778 if(!data)
779 continue;
780
781 name = heap_strndupW(setCookieHeader->lpszValue, data-setCookieHeader->lpszValue);
782 if(!name)
783 continue;
784
785 data++;
786 set_cookie(request->server->name, path, name, data, INTERNET_COOKIE_HTTPONLY);
787 heap_free(name);
788 }
789
790 LeaveCriticalSection( &request->headers_section );
791 heap_free(path);
792 }
793
794 static void strip_spaces(LPWSTR start)
795 {
796 LPWSTR str = start;
797 LPWSTR end;
798
799 while (*str == ' ')
800 str++;
801
802 if (str != start)
803 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
804
805 end = start + strlenW(start) - 1;
806 while (end >= start && *end == ' ')
807 {
808 *end = '\0';
809 end--;
810 }
811 }
812
813 static inline BOOL is_basic_auth_value( LPCWSTR pszAuthValue, LPWSTR *pszRealm )
814 {
815 static const WCHAR szBasic[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
816 static const WCHAR szRealm[] = {'r','e','a','l','m'}; /* Note: not nul-terminated */
817 BOOL is_basic;
818 is_basic = !strncmpiW(pszAuthValue, szBasic, ARRAYSIZE(szBasic)) &&
819 ((pszAuthValue[ARRAYSIZE(szBasic)] == ' ') || !pszAuthValue[ARRAYSIZE(szBasic)]);
820 if (is_basic && pszRealm)
821 {
822 LPCWSTR token;
823 LPCWSTR ptr = &pszAuthValue[ARRAYSIZE(szBasic)];
824 LPCWSTR realm;
825 ptr++;
826 *pszRealm=NULL;
827 token = strchrW(ptr,'=');
828 if (!token)
829 return TRUE;
830 realm = ptr;
831 while (*realm == ' ')
832 realm++;
833 if(!strncmpiW(realm, szRealm, ARRAYSIZE(szRealm)) &&
834 (realm[ARRAYSIZE(szRealm)] == ' ' || realm[ARRAYSIZE(szRealm)] == '='))
835 {
836 token++;
837 while (*token == ' ')
838 token++;
839 if (*token == '\0')
840 return TRUE;
841 *pszRealm = heap_strdupW(token);
842 strip_spaces(*pszRealm);
843 }
844 }
845
846 return is_basic;
847 }
848
849 static void destroy_authinfo( struct HttpAuthInfo *authinfo )
850 {
851 if (!authinfo) return;
852
853 if (SecIsValidHandle(&authinfo->ctx))
854 DeleteSecurityContext(&authinfo->ctx);
855 if (SecIsValidHandle(&authinfo->cred))
856 FreeCredentialsHandle(&authinfo->cred);
857
858 heap_free(authinfo->auth_data);
859 heap_free(authinfo->scheme);
860 heap_free(authinfo);
861 }
862
863 static UINT retrieve_cached_basic_authorization(const WCHAR *host, const WCHAR *realm, char **auth_data)
864 {
865 basicAuthorizationData *ad;
866 UINT rc = 0;
867
868 TRACE("Looking for authorization for %s:%s\n",debugstr_w(host),debugstr_w(realm));
869
870 EnterCriticalSection(&authcache_cs);
871 LIST_FOR_EACH_ENTRY(ad, &basicAuthorizationCache, basicAuthorizationData, entry)
872 {
873 if (!strcmpiW(host, ad->host) && (!realm || !strcmpW(realm, ad->realm)))
874 {
875 TRACE("Authorization found in cache\n");
876 *auth_data = heap_alloc(ad->authorizationLen);
877 memcpy(*auth_data,ad->authorization,ad->authorizationLen);
878 rc = ad->authorizationLen;
879 break;
880 }
881 }
882 LeaveCriticalSection(&authcache_cs);
883 return rc;
884 }
885
886 static void cache_basic_authorization(LPWSTR host, LPWSTR realm, LPSTR auth_data, UINT auth_data_len)
887 {
888 struct list *cursor;
889 basicAuthorizationData* ad = NULL;
890
891 TRACE("caching authorization for %s:%s = %s\n",debugstr_w(host),debugstr_w(realm),debugstr_an(auth_data,auth_data_len));
892
893 EnterCriticalSection(&authcache_cs);
894 LIST_FOR_EACH(cursor, &basicAuthorizationCache)
895 {
896 basicAuthorizationData *check = LIST_ENTRY(cursor,basicAuthorizationData,entry);
897 if (!strcmpiW(host,check->host) && !strcmpW(realm,check->realm))
898 {
899 ad = check;
900 break;
901 }
902 }
903
904 if (ad)
905 {
906 TRACE("Found match in cache, replacing\n");
907 heap_free(ad->authorization);
908 ad->authorization = heap_alloc(auth_data_len);
909 memcpy(ad->authorization, auth_data, auth_data_len);
910 ad->authorizationLen = auth_data_len;
911 }
912 else
913 {
914 ad = heap_alloc(sizeof(basicAuthorizationData));
915 ad->host = heap_strdupW(host);
916 ad->realm = heap_strdupW(realm);
917 ad->authorization = heap_alloc(auth_data_len);
918 memcpy(ad->authorization, auth_data, auth_data_len);
919 ad->authorizationLen = auth_data_len;
920 list_add_head(&basicAuthorizationCache,&ad->entry);
921 TRACE("authorization cached\n");
922 }
923 LeaveCriticalSection(&authcache_cs);
924 }
925
926 static BOOL retrieve_cached_authorization(LPWSTR host, LPWSTR scheme,
927 SEC_WINNT_AUTH_IDENTITY_W *nt_auth_identity)
928 {
929 authorizationData *ad;
930
931 TRACE("Looking for authorization for %s:%s\n", debugstr_w(host), debugstr_w(scheme));
932
933 EnterCriticalSection(&authcache_cs);
934 LIST_FOR_EACH_ENTRY(ad, &authorizationCache, authorizationData, entry) {
935 if(!strcmpiW(host, ad->host) && !strcmpiW(scheme, ad->scheme)) {
936 TRACE("Authorization found in cache\n");
937
938 nt_auth_identity->User = heap_strdupW(ad->user);
939 nt_auth_identity->Password = heap_strdupW(ad->password);
940 nt_auth_identity->Domain = heap_alloc(sizeof(WCHAR)*ad->domain_len);
941 if(!nt_auth_identity->User || !nt_auth_identity->Password ||
942 (!nt_auth_identity->Domain && ad->domain_len)) {
943 heap_free(nt_auth_identity->User);
944 heap_free(nt_auth_identity->Password);
945 heap_free(nt_auth_identity->Domain);
946 break;
947 }
948
949 nt_auth_identity->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
950 nt_auth_identity->UserLength = ad->user_len;
951 nt_auth_identity->PasswordLength = ad->password_len;
952 memcpy(nt_auth_identity->Domain, ad->domain, sizeof(WCHAR)*ad->domain_len);
953 nt_auth_identity->DomainLength = ad->domain_len;
954 LeaveCriticalSection(&authcache_cs);
955 return TRUE;
956 }
957 }
958 LeaveCriticalSection(&authcache_cs);
959
960 return FALSE;
961 }
962
963 static void cache_authorization(LPWSTR host, LPWSTR scheme,
964 SEC_WINNT_AUTH_IDENTITY_W *nt_auth_identity)
965 {
966 authorizationData *ad;
967 BOOL found = FALSE;
968
969 TRACE("Caching authorization for %s:%s\n", debugstr_w(host), debugstr_w(scheme));
970
971 EnterCriticalSection(&authcache_cs);
972 LIST_FOR_EACH_ENTRY(ad, &authorizationCache, authorizationData, entry)
973 if(!strcmpiW(host, ad->host) && !strcmpiW(scheme, ad->scheme)) {
974 found = TRUE;
975 break;
976 }
977
978 if(found) {
979 heap_free(ad->user);
980 heap_free(ad->password);
981 heap_free(ad->domain);
982 } else {
983 ad = heap_alloc(sizeof(authorizationData));
984 if(!ad) {
985 LeaveCriticalSection(&authcache_cs);
986 return;
987 }
988
989 ad->host = heap_strdupW(host);
990 ad->scheme = heap_strdupW(scheme);
991 list_add_head(&authorizationCache, &ad->entry);
992 }
993
994 ad->user = heap_strndupW(nt_auth_identity->User, nt_auth_identity->UserLength);
995 ad->password = heap_strndupW(nt_auth_identity->Password, nt_auth_identity->PasswordLength);
996 ad->domain = heap_strndupW(nt_auth_identity->Domain, nt_auth_identity->DomainLength);
997 ad->user_len = nt_auth_identity->UserLength;
998 ad->password_len = nt_auth_identity->PasswordLength;
999 ad->domain_len = nt_auth_identity->DomainLength;
1000
1001 if(!ad->host || !ad->scheme || !ad->user || !ad->password
1002 || (nt_auth_identity->Domain && !ad->domain)) {
1003 heap_free(ad->host);
1004 heap_free(ad->scheme);
1005 heap_free(ad->user);
1006 heap_free(ad->password);
1007 heap_free(ad->domain);
1008 list_remove(&ad->entry);
1009 heap_free(ad);
1010 }
1011
1012 LeaveCriticalSection(&authcache_cs);
1013 }
1014
1015 static BOOL HTTP_DoAuthorization( http_request_t *request, LPCWSTR pszAuthValue,
1016 struct HttpAuthInfo **ppAuthInfo,
1017 LPWSTR domain_and_username, LPWSTR password,
1018 LPWSTR host )
1019 {
1020 SECURITY_STATUS sec_status;
1021 struct HttpAuthInfo *pAuthInfo = *ppAuthInfo;
1022 BOOL first = FALSE;
1023 LPWSTR szRealm = NULL;
1024
1025 TRACE("%s\n", debugstr_w(pszAuthValue));
1026
1027 if (!pAuthInfo)
1028 {
1029 TimeStamp exp;
1030
1031 first = TRUE;
1032 pAuthInfo = heap_alloc(sizeof(*pAuthInfo));
1033 if (!pAuthInfo)
1034 return FALSE;
1035
1036 SecInvalidateHandle(&pAuthInfo->cred);
1037 SecInvalidateHandle(&pAuthInfo->ctx);
1038 memset(&pAuthInfo->exp, 0, sizeof(pAuthInfo->exp));
1039 pAuthInfo->attr = 0;
1040 pAuthInfo->auth_data = NULL;
1041 pAuthInfo->auth_data_len = 0;
1042 pAuthInfo->finished = FALSE;
1043
1044 if (is_basic_auth_value(pszAuthValue,NULL))
1045 {
1046 static const WCHAR szBasic[] = {'B','a','s','i','c',0};
1047 pAuthInfo->scheme = heap_strdupW(szBasic);
1048 if (!pAuthInfo->scheme)
1049 {
1050 heap_free(pAuthInfo);
1051 return FALSE;
1052 }
1053 }
1054 else
1055 {
1056 PVOID pAuthData;
1057 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity;
1058
1059 pAuthInfo->scheme = heap_strdupW(pszAuthValue);
1060 if (!pAuthInfo->scheme)
1061 {
1062 heap_free(pAuthInfo);
1063 return FALSE;
1064 }
1065
1066 if (domain_and_username)
1067 {
1068 WCHAR *user = strchrW(domain_and_username, '\\');
1069 WCHAR *domain = domain_and_username;
1070
1071 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
1072
1073 pAuthData = &nt_auth_identity;
1074
1075 if (user) user++;
1076 else
1077 {
1078 user = domain_and_username;
1079 domain = NULL;
1080 }
1081
1082 nt_auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
1083 nt_auth_identity.User = user;
1084 nt_auth_identity.UserLength = strlenW(nt_auth_identity.User);
1085 nt_auth_identity.Domain = domain;
1086 nt_auth_identity.DomainLength = domain ? user - domain - 1 : 0;
1087 nt_auth_identity.Password = password;
1088 nt_auth_identity.PasswordLength = strlenW(nt_auth_identity.Password);
1089
1090 cache_authorization(host, pAuthInfo->scheme, &nt_auth_identity);
1091 }
1092 else if(retrieve_cached_authorization(host, pAuthInfo->scheme, &nt_auth_identity))
1093 pAuthData = &nt_auth_identity;
1094 else
1095 /* use default credentials */
1096 pAuthData = NULL;
1097
1098 sec_status = AcquireCredentialsHandleW(NULL, pAuthInfo->scheme,
1099 SECPKG_CRED_OUTBOUND, NULL,
1100 pAuthData, NULL,
1101 NULL, &pAuthInfo->cred,
1102 &exp);
1103
1104 if(pAuthData && !domain_and_username) {
1105 heap_free(nt_auth_identity.User);
1106 heap_free(nt_auth_identity.Domain);
1107 heap_free(nt_auth_identity.Password);
1108 }
1109
1110 if (sec_status == SEC_E_OK)
1111 {
1112 PSecPkgInfoW sec_pkg_info;
1113 sec_status = QuerySecurityPackageInfoW(pAuthInfo->scheme, &sec_pkg_info);
1114 if (sec_status == SEC_E_OK)
1115 {
1116 pAuthInfo->max_token = sec_pkg_info->cbMaxToken;
1117 FreeContextBuffer(sec_pkg_info);
1118 }
1119 }
1120 if (sec_status != SEC_E_OK)
1121 {
1122 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
1123 debugstr_w(pAuthInfo->scheme), sec_status);
1124 heap_free(pAuthInfo->scheme);
1125 heap_free(pAuthInfo);
1126 return FALSE;
1127 }
1128 }
1129 *ppAuthInfo = pAuthInfo;
1130 }
1131 else if (pAuthInfo->finished)
1132 return FALSE;
1133
1134 if ((strlenW(pszAuthValue) < strlenW(pAuthInfo->scheme)) ||
1135 strncmpiW(pszAuthValue, pAuthInfo->scheme, strlenW(pAuthInfo->scheme)))
1136 {
1137 ERR("authentication scheme changed from %s to %s\n",
1138 debugstr_w(pAuthInfo->scheme), debugstr_w(pszAuthValue));
1139 return FALSE;
1140 }
1141
1142 if (is_basic_auth_value(pszAuthValue,&szRealm))
1143 {
1144 int userlen;
1145 int passlen;
1146 char *auth_data = NULL;
1147 UINT auth_data_len = 0;
1148
1149 TRACE("basic authentication realm %s\n",debugstr_w(szRealm));
1150
1151 if (!domain_and_username)
1152 {
1153 if (host && szRealm)
1154 auth_data_len = retrieve_cached_basic_authorization(host, szRealm,&auth_data);
1155 if (auth_data_len == 0)
1156 {
1157 heap_free(szRealm);
1158 return FALSE;
1159 }
1160 }
1161 else
1162 {
1163 userlen = WideCharToMultiByte(CP_UTF8, 0, domain_and_username, lstrlenW(domain_and_username), NULL, 0, NULL, NULL);
1164 passlen = WideCharToMultiByte(CP_UTF8, 0, password, lstrlenW(password), NULL, 0, NULL, NULL);
1165
1166 /* length includes a nul terminator, which will be re-used for the ':' */
1167 auth_data = heap_alloc(userlen + 1 + passlen);
1168 if (!auth_data)
1169 {
1170 heap_free(szRealm);
1171 return FALSE;
1172 }
1173
1174 WideCharToMultiByte(CP_UTF8, 0, domain_and_username, -1, auth_data, userlen, NULL, NULL);
1175 auth_data[userlen] = ':';
1176 WideCharToMultiByte(CP_UTF8, 0, password, -1, &auth_data[userlen+1], passlen, NULL, NULL);
1177 auth_data_len = userlen + 1 + passlen;
1178 if (host && szRealm)
1179 cache_basic_authorization(host, szRealm, auth_data, auth_data_len);
1180 }
1181
1182 pAuthInfo->auth_data = auth_data;
1183 pAuthInfo->auth_data_len = auth_data_len;
1184 pAuthInfo->finished = TRUE;
1185 heap_free(szRealm);
1186 return TRUE;
1187 }
1188 else
1189 {
1190 LPCWSTR pszAuthData;
1191 SecBufferDesc out_desc, in_desc;
1192 SecBuffer out, in;
1193 unsigned char *buffer;
1194 ULONG context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE |
1195 ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE;
1196
1197 in.BufferType = SECBUFFER_TOKEN;
1198 in.cbBuffer = 0;
1199 in.pvBuffer = NULL;
1200
1201 in_desc.ulVersion = 0;
1202 in_desc.cBuffers = 1;
1203 in_desc.pBuffers = &in;
1204
1205 pszAuthData = pszAuthValue + strlenW(pAuthInfo->scheme);
1206 if (*pszAuthData == ' ')
1207 {
1208 pszAuthData++;
1209 in.cbBuffer = HTTP_DecodeBase64(pszAuthData, NULL);
1210 in.pvBuffer = heap_alloc(in.cbBuffer);
1211 HTTP_DecodeBase64(pszAuthData, in.pvBuffer);
1212 }
1213
1214 buffer = heap_alloc(pAuthInfo->max_token);
1215
1216 out.BufferType = SECBUFFER_TOKEN;
1217 out.cbBuffer = pAuthInfo->max_token;
1218 out.pvBuffer = buffer;
1219
1220 out_desc.ulVersion = 0;
1221 out_desc.cBuffers = 1;
1222 out_desc.pBuffers = &out;
1223
1224 sec_status = InitializeSecurityContextW(first ? &pAuthInfo->cred : NULL,
1225 first ? NULL : &pAuthInfo->ctx,
1226 first ? request->server->name : NULL,
1227 context_req, 0, SECURITY_NETWORK_DREP,
1228 in.pvBuffer ? &in_desc : NULL,
1229 0, &pAuthInfo->ctx, &out_desc,
1230 &pAuthInfo->attr, &pAuthInfo->exp);
1231 if (sec_status == SEC_E_OK)
1232 {
1233 pAuthInfo->finished = TRUE;
1234 pAuthInfo->auth_data = out.pvBuffer;
1235 pAuthInfo->auth_data_len = out.cbBuffer;
1236 TRACE("sending last auth packet\n");
1237 }
1238 else if (sec_status == SEC_I_CONTINUE_NEEDED)
1239 {
1240 pAuthInfo->auth_data = out.pvBuffer;
1241 pAuthInfo->auth_data_len = out.cbBuffer;
1242 TRACE("sending next auth packet\n");
1243 }
1244 else
1245 {
1246 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status);
1247 heap_free(out.pvBuffer);
1248 destroy_authinfo(pAuthInfo);
1249 *ppAuthInfo = NULL;
1250 return FALSE;
1251 }
1252 }
1253
1254 return TRUE;
1255 }
1256
1257 /***********************************************************************
1258 * HTTP_HttpAddRequestHeadersW (internal)
1259 */
1260 static DWORD HTTP_HttpAddRequestHeadersW(http_request_t *request,
1261 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1262 {
1263 LPWSTR lpszStart;
1264 LPWSTR lpszEnd;
1265 LPWSTR buffer;
1266 DWORD len, res = ERROR_HTTP_INVALID_HEADER;
1267
1268 TRACE("copying header: %s\n", debugstr_wn(lpszHeader, dwHeaderLength));
1269
1270 if( dwHeaderLength == ~0U )
1271 len = strlenW(lpszHeader);
1272 else
1273 len = dwHeaderLength;
1274 buffer = heap_alloc(sizeof(WCHAR)*(len+1));
1275 lstrcpynW( buffer, lpszHeader, len + 1);
1276
1277 lpszStart = buffer;
1278
1279 do
1280 {
1281 LPWSTR * pFieldAndValue;
1282
1283 lpszEnd = lpszStart;
1284
1285 while (*lpszEnd != '\0')
1286 {
1287 if (*lpszEnd == '\r' || *lpszEnd == '\n')
1288 break;
1289 lpszEnd++;
1290 }
1291
1292 if (*lpszStart == '\0')
1293 break;
1294
1295 if (*lpszEnd == '\r' || *lpszEnd == '\n')
1296 {
1297 *lpszEnd = '\0';
1298 lpszEnd++; /* Jump over newline */
1299 }
1300 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
1301 if (*lpszStart == '\0')
1302 {
1303 /* Skip 0-length headers */
1304 lpszStart = lpszEnd;
1305 res = ERROR_SUCCESS;
1306 continue;
1307 }
1308 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
1309 if (pFieldAndValue)
1310 {
1311 res = HTTP_ProcessHeader(request, pFieldAndValue[0],
1312 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
1313 HTTP_FreeTokens(pFieldAndValue);
1314 }
1315
1316 lpszStart = lpszEnd;
1317 } while (res == ERROR_SUCCESS);
1318
1319 heap_free(buffer);
1320 return res;
1321 }
1322
1323 /***********************************************************************
1324 * HttpAddRequestHeadersW (WININET.@)
1325 *
1326 * Adds one or more HTTP header to the request handler
1327 *
1328 * NOTE
1329 * On Windows if dwHeaderLength includes the trailing '\0', then
1330 * HttpAddRequestHeadersW() adds it too. However this results in an
1331 * invalid HTTP header which is rejected by some servers so we probably
1332 * don't need to match Windows on that point.
1333 *
1334 * RETURNS
1335 * TRUE on success
1336 * FALSE on failure
1337 *
1338 */
1339 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
1340 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1341 {
1342 http_request_t *request;
1343 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1344
1345 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_wn(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
1346
1347 if (!lpszHeader)
1348 return TRUE;
1349
1350 request = (http_request_t*) get_handle_object( hHttpRequest );
1351 if (request && request->hdr.htype == WH_HHTTPREQ)
1352 res = HTTP_HttpAddRequestHeadersW( request, lpszHeader, dwHeaderLength, dwModifier );
1353 if( request )
1354 WININET_Release( &request->hdr );
1355
1356 if(res != ERROR_SUCCESS)
1357 SetLastError(res);
1358 return res == ERROR_SUCCESS;
1359 }
1360
1361 /***********************************************************************
1362 * HttpAddRequestHeadersA (WININET.@)
1363 *
1364 * Adds one or more HTTP header to the request handler
1365 *
1366 * RETURNS
1367 * TRUE on success
1368 * FALSE on failure
1369 *
1370 */
1371 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
1372 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1373 {
1374 DWORD len;
1375 LPWSTR hdr;
1376 BOOL r;
1377
1378 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_an(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
1379
1380 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
1381 hdr = heap_alloc(len*sizeof(WCHAR));
1382 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
1383 if( dwHeaderLength != ~0U )
1384 dwHeaderLength = len;
1385
1386 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
1387
1388 heap_free( hdr );
1389 return r;
1390 }
1391
1392 static void free_accept_types( WCHAR **accept_types )
1393 {
1394 WCHAR *ptr, **types = accept_types;
1395
1396 if (!types) return;
1397 while ((ptr = *types))
1398 {
1399 heap_free( ptr );
1400 types++;
1401 }
1402 heap_free( accept_types );
1403 }
1404
1405 static WCHAR **convert_accept_types( const char **accept_types )
1406 {
1407 unsigned int count;
1408 const char **types = accept_types;
1409 WCHAR **typesW;
1410 BOOL invalid_pointer = FALSE;
1411
1412 if (!types) return NULL;
1413 count = 0;
1414 while (*types)
1415 {
1416 __TRY
1417 {
1418 /* find out how many there are */
1419 if (*types && **types)
1420 {
1421 TRACE("accept type: %s\n", debugstr_a(*types));
1422 count++;
1423 }
1424 }
1425 __EXCEPT_PAGE_FAULT
1426 {
1427 WARN("invalid accept type pointer\n");
1428 invalid_pointer = TRUE;
1429 }
1430 __ENDTRY;
1431 types++;
1432 }
1433 if (invalid_pointer) return NULL;
1434 if (!(typesW = heap_alloc( sizeof(WCHAR *) * (count + 1) ))) return NULL;
1435 count = 0;
1436 types = accept_types;
1437 while (*types)
1438 {
1439 if (*types && **types) typesW[count++] = heap_strdupAtoW( *types );
1440 types++;
1441 }
1442 typesW[count] = NULL;
1443 return typesW;
1444 }
1445
1446 /***********************************************************************
1447 * HttpOpenRequestA (WININET.@)
1448 *
1449 * Open a HTTP request handle
1450 *
1451 * RETURNS
1452 * HINTERNET a HTTP request handle on success
1453 * NULL on failure
1454 *
1455 */
1456 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
1457 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
1458 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
1459 DWORD dwFlags, DWORD_PTR dwContext)
1460 {
1461 LPWSTR szVerb = NULL, szObjectName = NULL;
1462 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
1463 HINTERNET rc = NULL;
1464
1465 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
1466 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
1467 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
1468 dwFlags, dwContext);
1469
1470 if (lpszVerb)
1471 {
1472 szVerb = heap_strdupAtoW(lpszVerb);
1473 if ( !szVerb )
1474 goto end;
1475 }
1476
1477 if (lpszObjectName)
1478 {
1479 szObjectName = heap_strdupAtoW(lpszObjectName);
1480 if ( !szObjectName )
1481 goto end;
1482 }
1483
1484 if (lpszVersion)
1485 {
1486 szVersion = heap_strdupAtoW(lpszVersion);
1487 if ( !szVersion )
1488 goto end;
1489 }
1490
1491 if (lpszReferrer)
1492 {
1493 szReferrer = heap_strdupAtoW(lpszReferrer);
1494 if ( !szReferrer )
1495 goto end;
1496 }
1497
1498 szAcceptTypes = convert_accept_types( lpszAcceptTypes );
1499 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName, szVersion, szReferrer,
1500 (const WCHAR **)szAcceptTypes, dwFlags, dwContext);
1501
1502 end:
1503 free_accept_types(szAcceptTypes);
1504 heap_free(szReferrer);
1505 heap_free(szVersion);
1506 heap_free(szObjectName);
1507 heap_free(szVerb);
1508 return rc;
1509 }
1510
1511 /***********************************************************************
1512 * HTTP_EncodeBase64
1513 */
1514 static UINT HTTP_EncodeBase64( LPCSTR bin, unsigned int len, LPWSTR base64 )
1515 {
1516 UINT n = 0, x;
1517 static const CHAR HTTP_Base64Enc[] =
1518 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1519
1520 while( len > 0 )
1521 {
1522 /* first 6 bits, all from bin[0] */
1523 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
1524 x = (bin[0] & 3) << 4;
1525
1526 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1527 if( len == 1 )
1528 {
1529 base64[n++] = HTTP_Base64Enc[x];
1530 base64[n++] = '=';
1531 base64[n++] = '=';
1532 break;
1533 }
1534 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
1535 x = ( bin[1] & 0x0f ) << 2;
1536
1537 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1538 if( len == 2 )
1539 {
1540 base64[n++] = HTTP_Base64Enc[x];
1541 base64[n++] = '=';
1542 break;
1543 }
1544 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
1545
1546 /* last 6 bits, all from bin [2] */
1547 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
1548 bin += 3;
1549 len -= 3;
1550 }
1551 base64[n] = 0;
1552 return n;
1553 }
1554
1555 static const signed char HTTP_Base64Dec[] =
1556 {
1557 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00 */
1558 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10 */
1559 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20 */
1560 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 0x30 */
1561 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40 */
1562 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50 */
1563 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60 */
1564 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 0x70 */
1565 };
1566
1567 /***********************************************************************
1568 * HTTP_DecodeBase64
1569 */
1570 static UINT HTTP_DecodeBase64( LPCWSTR base64, LPSTR bin )
1571 {
1572 unsigned int n = 0;
1573
1574 while(*base64)
1575 {
1576 signed char in[4];
1577
1578 if (base64[0] >= ARRAYSIZE(HTTP_Base64Dec) ||
1579 ((in[0] = HTTP_Base64Dec[base64[0]]) == -1) ||
1580 base64[1] >= ARRAYSIZE(HTTP_Base64Dec) ||
1581 ((in[1] = HTTP_Base64Dec[base64[1]]) == -1))
1582 {
1583 WARN("invalid base64: %s\n", debugstr_w(base64));
1584 return 0;
1585 }
1586 if (bin)
1587 bin[n] = (unsigned char) (in[0] << 2 | in[1] >> 4);
1588 n++;
1589
1590 if ((base64[2] == '=') && (base64[3] == '='))
1591 break;
1592 if (base64[2] > ARRAYSIZE(HTTP_Base64Dec) ||
1593 ((in[2] = HTTP_Base64Dec[base64[2]]) == -1))
1594 {
1595 WARN("invalid base64: %s\n", debugstr_w(&base64[2]));
1596 return 0;
1597 }
1598 if (bin)
1599 bin[n] = (unsigned char) (in[1] << 4 | in[2] >> 2);
1600 n++;
1601
1602 if (base64[3] == '=')
1603 break;
1604 if (base64[3] > ARRAYSIZE(HTTP_Base64Dec) ||
1605 ((in[3] = HTTP_Base64Dec[base64[3]]) == -1))
1606 {
1607 WARN("invalid base64: %s\n", debugstr_w(&base64[3]));
1608 return 0;
1609 }
1610 if (bin)
1611 bin[n] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
1612 n++;
1613
1614 base64 += 4;
1615 }
1616
1617 return n;
1618 }
1619
1620 static WCHAR *encode_auth_data( const WCHAR *scheme, const char *data, UINT data_len )
1621 {
1622 WCHAR *ret;
1623 UINT len, scheme_len = strlenW( scheme );
1624
1625 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1626 len = scheme_len + 1 + ((data_len + 2) * 4) / 3;
1627 if (!(ret = heap_alloc( (len + 1) * sizeof(WCHAR) ))) return NULL;
1628 memcpy( ret, scheme, scheme_len * sizeof(WCHAR) );
1629 ret[scheme_len] = ' ';
1630 HTTP_EncodeBase64( data, data_len, ret + scheme_len + 1 );
1631 return ret;
1632 }
1633
1634
1635 /***********************************************************************
1636 * HTTP_InsertAuthorization
1637 *
1638 * Insert or delete the authorization field in the request header.
1639 */
1640 static BOOL HTTP_InsertAuthorization( http_request_t *request, struct HttpAuthInfo *pAuthInfo, LPCWSTR header )
1641 {
1642 static const WCHAR wszBasic[] = {'B','a','s','i','c',0};
1643 WCHAR *host, *authorization = NULL;
1644
1645 if (pAuthInfo)
1646 {
1647 if (pAuthInfo->auth_data_len)
1648 {
1649 if (!(authorization = encode_auth_data(pAuthInfo->scheme, pAuthInfo->auth_data, pAuthInfo->auth_data_len)))
1650 return FALSE;
1651
1652 /* clear the data as it isn't valid now that it has been sent to the
1653 * server, unless it's Basic authentication which doesn't do
1654 * connection tracking */
1655 if (strcmpiW(pAuthInfo->scheme, wszBasic))
1656 {
1657 heap_free(pAuthInfo->auth_data);
1658 pAuthInfo->auth_data = NULL;
1659 pAuthInfo->auth_data_len = 0;
1660 }
1661 }
1662
1663 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1664
1665 HTTP_ProcessHeader(request, header, authorization,
1666 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
1667 heap_free(authorization);
1668 }
1669 else if (!strcmpW(header, szAuthorization) && (host = get_host_header(request)))
1670 {
1671 UINT data_len;
1672 char *data;
1673
1674 if ((data_len = retrieve_cached_basic_authorization(host, NULL, &data)))
1675 {
1676 TRACE("Found cached basic authorization for %s\n", debugstr_w(host));
1677
1678 if (!(authorization = encode_auth_data(wszBasic, data, data_len)))
1679 {
1680 heap_free(data);
1681 heap_free(host);
1682 return FALSE;
1683 }
1684
1685 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1686
1687 HTTP_ProcessHeader(request, header, authorization,
1688 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_ADD);
1689 heap_free(data);
1690 heap_free(authorization);
1691 }
1692 heap_free(host);
1693 }
1694 return TRUE;
1695 }
1696
1697 static WCHAR *build_proxy_path_url(http_request_t *req)
1698 {
1699 DWORD size, len;
1700 WCHAR *url;
1701
1702 len = strlenW(req->server->scheme_host_port);
1703 size = len + strlenW(req->path) + 1;
1704 if(*req->path != '/')
1705 size++;
1706 url = heap_alloc(size * sizeof(WCHAR));
1707 if(!url)
1708 return NULL;
1709
1710 memcpy(url, req->server->scheme_host_port, len*sizeof(WCHAR));
1711 if(*req->path != '/')
1712 url[len++] = '/';
1713
1714 strcpyW(url+len, req->path);
1715
1716 TRACE("url=%s\n", debugstr_w(url));
1717 return url;
1718 }
1719
1720 static BOOL HTTP_DomainMatches(LPCWSTR server, LPCWSTR domain)
1721 {
1722 static const WCHAR localW[] = { '<','l','o','c','a','l','>',0 };
1723 BOOL ret = FALSE;
1724
1725 if (!strcmpiW( domain, localW ) && !strchrW( server, '.' ))
1726 ret = TRUE;
1727 else if (*domain == '*')
1728 {
1729 if (domain[1] == '.')
1730 {
1731 LPCWSTR dot;
1732
1733 /* For a hostname to match a wildcard, the last domain must match
1734 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
1735 * hostname is www.foo.a.b, it matches, but a.b does not.
1736 */
1737 dot = strchrW( server, '.' );
1738 if (dot)
1739 {
1740 int len = strlenW( dot + 1 );
1741
1742 if (len > strlenW( domain + 2 ))
1743 {
1744 LPCWSTR ptr;
1745
1746 /* The server's domain is longer than the wildcard, so it
1747 * could be a subdomain. Compare the last portion of the
1748 * server's domain.
1749 */
1750 ptr = dot + len + 1 - strlenW( domain + 2 );
1751 if (!strcmpiW( ptr, domain + 2 ))
1752 {
1753 /* This is only a match if the preceding character is
1754 * a '.', i.e. that it is a matching domain. E.g.
1755 * if domain is '*.b.c' and server is 'www.ab.c' they
1756 * do not match.
1757 */
1758 ret = *(ptr - 1) == '.';
1759 }
1760 }
1761 else
1762 ret = !strcmpiW( dot + 1, domain + 2 );
1763 }
1764 }
1765 }
1766 else
1767 ret = !strcmpiW( server, domain );
1768 return ret;
1769 }
1770
1771 static BOOL HTTP_ShouldBypassProxy(appinfo_t *lpwai, LPCWSTR server)
1772 {
1773 LPCWSTR ptr;
1774 BOOL ret = FALSE;
1775
1776 if (!lpwai->proxyBypass) return FALSE;
1777 ptr = lpwai->proxyBypass;
1778 do {
1779 LPCWSTR tmp = ptr;
1780
1781 ptr = strchrW( ptr, ';' );
1782 if (!ptr)
1783 ptr = strchrW( tmp, ' ' );
1784 if (ptr)
1785 {
1786 if (ptr - tmp < INTERNET_MAX_HOST_NAME_LENGTH)
1787 {
1788 WCHAR domain[INTERNET_MAX_HOST_NAME_LENGTH];
1789
1790 memcpy( domain, tmp, (ptr - tmp) * sizeof(WCHAR) );
1791 domain[ptr - tmp] = 0;
1792 ret = HTTP_DomainMatches( server, domain );
1793 }
1794 ptr += 1;
1795 }
1796 else if (*tmp)
1797 ret = HTTP_DomainMatches( server, tmp );
1798 } while (ptr && !ret);
1799 return ret;
1800 }
1801
1802 /***********************************************************************
1803 * HTTP_DealWithProxy
1804 */
1805 static BOOL HTTP_DealWithProxy(appinfo_t *hIC, http_session_t *session, http_request_t *request)
1806 {
1807 static const WCHAR protoHttp[] = { 'h','t','t','p',0 };
1808 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 };
1809 static const WCHAR szFormat[] = { 'h','t','t','p',':','/','/','%','s',0 };
1810 WCHAR buf[INTERNET_MAX_HOST_NAME_LENGTH];
1811 WCHAR protoProxy[INTERNET_MAX_URL_LENGTH];
1812 DWORD protoProxyLen = INTERNET_MAX_URL_LENGTH;
1813 WCHAR proxy[INTERNET_MAX_URL_LENGTH];
1814 static WCHAR szNul[] = { 0 };
1815 URL_COMPONENTSW UrlComponents;
1816 server_t *new_server;
1817 BOOL is_https;
1818
1819 memset( &UrlComponents, 0, sizeof UrlComponents );
1820 UrlComponents.dwStructSize = sizeof UrlComponents;
1821 UrlComponents.lpszHostName = buf;
1822 UrlComponents.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH;
1823
1824 if (!INTERNET_FindProxyForProtocol(hIC->proxy, protoHttp, protoProxy, &protoProxyLen))
1825 return FALSE;
1826 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1827 protoProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
1828 sprintfW(proxy, szFormat, protoProxy);
1829 else
1830 strcpyW(proxy, protoProxy);
1831 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
1832 return FALSE;
1833 if( UrlComponents.dwHostNameLength == 0 )
1834 return FALSE;
1835
1836 if( !request->path )
1837 request->path = szNul;
1838
1839 is_https = (UrlComponents.nScheme == INTERNET_SCHEME_HTTPS);
1840 if (is_https && UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1841 UrlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
1842
1843 new_server = get_server(UrlComponents.lpszHostName, UrlComponents.nPort, is_https, TRUE);
1844 if(!new_server)
1845 return FALSE;
1846
1847 request->proxy = new_server;
1848
1849 TRACE("proxy server=%s port=%d\n", debugstr_w(new_server->name), new_server->port);
1850 return TRUE;
1851 }
1852
1853 static DWORD HTTP_ResolveName(http_request_t *request)
1854 {
1855 server_t *server = request->proxy ? request->proxy : request->server;
1856 int addr_len;
1857
1858 if(server->addr_len)
1859 return ERROR_SUCCESS;
1860
1861 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
1862 INTERNET_STATUS_RESOLVING_NAME,
1863 server->name,
1864 (strlenW(server->name)+1) * sizeof(WCHAR));
1865
1866 addr_len = sizeof(server->addr);
1867 if (!GetAddress(server->name, server->port, (SOCKADDR*)&server->addr, &addr_len, server->addr_str))
1868 return ERROR_INTERNET_NAME_NOT_RESOLVED;
1869
1870 server->addr_len = addr_len;
1871 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
1872 INTERNET_STATUS_NAME_RESOLVED,
1873 server->addr_str, strlen(server->addr_str)+1);
1874
1875 TRACE("resolved %s to %s\n", debugstr_w(server->name), server->addr_str);
1876 return ERROR_SUCCESS;
1877 }
1878
1879 static BOOL HTTP_GetRequestURL(http_request_t *req, LPWSTR buf)
1880 {
1881 static const WCHAR http[] = { 'h','t','t','p',':','/','/',0 };
1882 static const WCHAR https[] = { 'h','t','t','p','s',':','/','/',0 };
1883 static const WCHAR slash[] = { '/',0 };
1884 LPHTTPHEADERW host_header;
1885 const WCHAR *host;
1886 LPCWSTR scheme;
1887
1888 EnterCriticalSection( &req->headers_section );
1889
1890 host_header = HTTP_GetHeader(req, hostW);
1891 if (host_header) host = host_header->lpszValue;
1892 else host = req->server->canon_host_port;
1893
1894 if (req->hdr.dwFlags & INTERNET_FLAG_SECURE)
1895 scheme = https;
1896 else
1897 scheme = http;
1898 strcpyW(buf, scheme);
1899 strcatW(buf, host);
1900 if (req->path[0] != '/')
1901 strcatW(buf, slash);
1902 strcatW(buf, req->path);
1903
1904 LeaveCriticalSection( &req->headers_section );
1905 return TRUE;
1906 }
1907
1908
1909 /***********************************************************************
1910 * HTTPREQ_Destroy (internal)
1911 *
1912 * Deallocate request handle
1913 *
1914 */
1915 static void HTTPREQ_Destroy(object_header_t *hdr)
1916 {
1917 http_request_t *request = (http_request_t*) hdr;
1918 DWORD i;
1919
1920 TRACE("\n");
1921
1922 if(request->hCacheFile)
1923 CloseHandle(request->hCacheFile);
1924 if(request->req_file)
1925 req_file_release(request->req_file);
1926
1927 request->headers_section.DebugInfo->Spare[0] = 0;
1928 DeleteCriticalSection( &request->headers_section );
1929 request->read_section.DebugInfo->Spare[0] = 0;
1930 DeleteCriticalSection( &request->read_section );
1931 WININET_Release(&request->session->hdr);
1932
1933 destroy_authinfo(request->authInfo);
1934 destroy_authinfo(request->proxyAuthInfo);
1935
1936 if(request->server)
1937 server_release(request->server);
1938 if(request->proxy)
1939 server_release(request->proxy);
1940
1941 heap_free(request->path);
1942 heap_free(request->verb);
1943 heap_free(request->version);
1944 heap_free(request->statusText);
1945
1946 for (i = 0; i < request->nCustHeaders; i++)
1947 {
1948 heap_free(request->custHeaders[i].lpszField);
1949 heap_free(request->custHeaders[i].lpszValue);
1950 }
1951 destroy_data_stream(request->data_stream);
1952 heap_free(request->custHeaders);
1953 }
1954
1955 static void http_release_netconn(http_request_t *req, BOOL reuse)
1956 {
1957 TRACE("%p %p %x\n",req, req->netconn, reuse);
1958
1959 if(!is_valid_netconn(req->netconn))
1960 return;
1961
1962 #ifndef __REACTOS__
1963 if(reuse && req->netconn->keep_alive) {
1964 BOOL run_collector;
1965
1966 EnterCriticalSection(&connection_pool_cs);
1967
1968 list_add_head(&req->netconn->server->conn_pool, &req->netconn->pool_entry);
1969 req->netconn->keep_until = (DWORD64)GetTickCount() + COLLECT_TIME;
1970 req->netconn = NULL;
1971
1972 run_collector = !collector_running;
1973 collector_running = TRUE;
1974
1975 LeaveCriticalSection(&connection_pool_cs);
1976
1977 if(run_collector) {
1978 HANDLE thread = NULL;
1979 HMODULE module;
1980
1981 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (const WCHAR*)WININET_hModule, &module);
1982 if(module)
1983 thread = CreateThread(NULL, 0, collect_connections_proc, NULL, 0, NULL);
1984 if(!thread) {
1985 EnterCriticalSection(&connection_pool_cs);
1986 collector_running = FALSE;
1987 LeaveCriticalSection(&connection_pool_cs);
1988
1989 if(module)
1990 FreeLibrary(module);
1991 }
1992 else
1993 CloseHandle(thread);
1994 }
1995 return;
1996 }
1997 #else
1998 /* Silence unused function warning */
1999 (void)collect_connections_proc;
2000 #endif
2001
2002 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
2003 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2004
2005 close_netconn(req->netconn);
2006
2007 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
2008 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2009 }
2010
2011 static BOOL HTTP_KeepAlive(http_request_t *request)
2012 {
2013 WCHAR szVersion[10];
2014 WCHAR szConnectionResponse[20];
2015 DWORD dwBufferSize = sizeof(szVersion);
2016 BOOL keepalive = FALSE;
2017
2018 /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
2019 * the connection is keep-alive by default */
2020 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_VERSION, szVersion, &dwBufferSize, NULL) == ERROR_SUCCESS
2021 && !strcmpiW(szVersion, g_szHttp1_1))
2022 {
2023 keepalive = TRUE;
2024 }
2025
2026 dwBufferSize = sizeof(szConnectionResponse);
2027 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_PROXY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) == ERROR_SUCCESS
2028 || HTTP_HttpQueryInfoW(request, HTTP_QUERY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) == ERROR_SUCCESS)
2029 {
2030 keepalive = !strcmpiW(szConnectionResponse, szKeepAlive);
2031 }
2032
2033 return keepalive;
2034 }
2035
2036 static void HTTPREQ_CloseConnection(object_header_t *hdr)
2037 {
2038 http_request_t *req = (http_request_t*)hdr;
2039
2040 http_release_netconn(req, drain_content(req, FALSE));
2041 }
2042
2043 static DWORD str_to_buffer(const WCHAR *str, void *buffer, DWORD *size, BOOL unicode)
2044 {
2045 int len;
2046 if (unicode)
2047 {
2048 WCHAR *buf = buffer;
2049
2050 if (str) len = strlenW(str);
2051 else len = 0;
2052 if (*size < (len + 1) * sizeof(WCHAR))
2053 {
2054 *size = (len + 1) * sizeof(WCHAR);
2055 return ERROR_INSUFFICIENT_BUFFER;
2056 }
2057 if (str) strcpyW(buf, str);
2058 else buf[0] = 0;
2059
2060 *size = len;
2061 return ERROR_SUCCESS;
2062 }
2063 else
2064 {
2065 char *buf = buffer;
2066
2067 if (str) len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
2068 else len = 1;
2069 if (*size < len)
2070 {
2071 *size = len;
2072 return ERROR_INSUFFICIENT_BUFFER;
2073 }
2074 if (str) WideCharToMultiByte(CP_ACP, 0, str, -1, buf, *size, NULL, NULL);
2075 else buf[0] = 0;
2076
2077 *size = len - 1;
2078 return ERROR_SUCCESS;
2079 }
2080 }
2081
2082 static DWORD HTTPREQ_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2083 {
2084 http_request_t *req = (http_request_t*)hdr;
2085
2086 switch(option) {
2087 case INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO:
2088 {
2089 INTERNET_DIAGNOSTIC_SOCKET_INFO *info = buffer;
2090
2091 FIXME("INTERNET_DIAGNOSTIC_SOCKET_INFO stub\n");
2092
2093 if (*size < sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO))
2094 return ERROR_INSUFFICIENT_BUFFER;
2095 *size = sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO);
2096 /* FIXME: can't get a SOCKET from our connection since we don't use
2097 * winsock
2098 */
2099 info->Socket = 0;
2100 /* FIXME: get source port from req->netConnection */
2101 info->SourcePort = 0;
2102 info->DestPort = req->server->port;
2103 info->Flags = 0;
2104 if (HTTP_KeepAlive(req))
2105 info->Flags |= IDSI_FLAG_KEEP_ALIVE;
2106 if (req->proxy)
2107 info->Flags |= IDSI_FLAG_PROXY;
2108 if (is_valid_netconn(req->netconn) && req->netconn->secure)
2109 info->Flags |= IDSI_FLAG_SECURE;
2110
2111 return ERROR_SUCCESS;
2112 }
2113
2114 case 98:
2115 TRACE("Queried undocumented option 98, forwarding to INTERNET_OPTION_SECURITY_FLAGS\n");
2116 /* fall through */
2117 case INTERNET_OPTION_SECURITY_FLAGS:
2118 {
2119 DWORD flags;
2120
2121 if (*size < sizeof(ULONG))
2122 return ERROR_INSUFFICIENT_BUFFER;
2123
2124 *size = sizeof(DWORD);
2125 flags = is_valid_netconn(req->netconn) ? req->netconn->security_flags : req->security_flags | req->server->security_flags;
2126 *(DWORD *)buffer = flags;
2127
2128 TRACE("INTERNET_OPTION_SECURITY_FLAGS %x\n", flags);
2129 return ERROR_SUCCESS;
2130 }
2131
2132 case INTERNET_OPTION_HANDLE_TYPE:
2133 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
2134
2135 if (*size < sizeof(ULONG))
2136 return ERROR_INSUFFICIENT_BUFFER;
2137
2138 *size = sizeof(DWORD);
2139 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_HTTP_REQUEST;
2140 return ERROR_SUCCESS;
2141
2142 case INTERNET_OPTION_URL: {
2143 static const WCHAR httpW[] = {'h','t','t','p',':','/','/',0};
2144 WCHAR url[INTERNET_MAX_URL_LENGTH];
2145
2146 TRACE("INTERNET_OPTION_URL\n");
2147
2148 strcpyW(url, httpW);
2149 strcatW(url, req->server->canon_host_port);
2150 strcatW(url, req->path);
2151
2152 TRACE("INTERNET_OPTION_URL: %s\n",debugstr_w(url));
2153 return str_to_buffer(url, buffer, size, unicode);
2154 }
2155 case INTERNET_OPTION_USER_AGENT:
2156 return str_to_buffer(req->session->appInfo->agent, buffer, size, unicode);
2157 case INTERNET_OPTION_USERNAME:
2158 return str_to_buffer(req->session->userName, buffer, size, unicode);
2159 case INTERNET_OPTION_PASSWORD:
2160 return str_to_buffer(req->session->password, buffer, size, unicode);
2161 case INTERNET_OPTION_PROXY_USERNAME:
2162 return str_to_buffer(req->session->appInfo->proxyUsername, buffer, size, unicode);
2163 case INTERNET_OPTION_PROXY_PASSWORD:
2164 return str_to_buffer(req->session->appInfo->proxyPassword, buffer, size, unicode);
2165
2166 case INTERNET_OPTION_CACHE_TIMESTAMPS: {
2167 INTERNET_CACHE_ENTRY_INFOW *info;
2168 INTERNET_CACHE_TIMESTAMPS *ts = buffer;
2169 WCHAR url[INTERNET_MAX_URL_LENGTH];
2170 DWORD nbytes, error;
2171 BOOL ret;
2172
2173 TRACE("INTERNET_OPTION_CACHE_TIMESTAMPS\n");
2174
2175 if (*size < sizeof(*ts))
2176 {
2177 *size = sizeof(*ts);
2178 return ERROR_INSUFFICIENT_BUFFER;
2179 }
2180 nbytes = 0;
2181 HTTP_GetRequestURL(req, url);
2182 ret = GetUrlCacheEntryInfoW(url, NULL, &nbytes);
2183 error = GetLastError();
2184 if (!ret && error == ERROR_INSUFFICIENT_BUFFER)
2185 {
2186 if (!(info = heap_alloc(nbytes)))
2187 return ERROR_OUTOFMEMORY;
2188
2189 GetUrlCacheEntryInfoW(url, info, &nbytes);
2190
2191 ts->ftExpires = info->ExpireTime;
2192 ts->ftLastModified = info->LastModifiedTime;
2193
2194 heap_free(info);
2195 *size = sizeof(*ts);
2196 return ERROR_SUCCESS;
2197 }
2198 return error;
2199 }
2200
2201 case INTERNET_OPTION_DATAFILE_NAME: {
2202 DWORD req_size;
2203
2204 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
2205
2206 if(!req->req_file) {
2207 *size = 0;
2208 return ERROR_INTERNET_ITEM_NOT_FOUND;
2209 }
2210
2211 if(unicode) {
2212 req_size = (lstrlenW(req->req_file->file_name)+1) * sizeof(WCHAR);
2213 if(*size < req_size)
2214 return ERROR_INSUFFICIENT_BUFFER;
2215
2216 *size = req_size;
2217 memcpy(buffer, req->req_file->file_name, *size);
2218 return ERROR_SUCCESS;
2219 }else {
2220 req_size = WideCharToMultiByte(CP_ACP, 0, req->req_file->file_name, -1, NULL, 0, NULL, NULL);
2221 if (req_size > *size)
2222 return ERROR_INSUFFICIENT_BUFFER;
2223
2224 *size = WideCharToMultiByte(CP_ACP, 0, req->req_file->file_name,
2225 -1, buffer, *size, NULL, NULL);
2226 return ERROR_SUCCESS;
2227 }
2228 }
2229
2230 case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT: {
2231 PCCERT_CONTEXT context;
2232
2233 if(!req->netconn)
2234 return ERROR_INTERNET_INVALID_OPERATION;
2235
2236 if(*size < sizeof(INTERNET_CERTIFICATE_INFOA)) {
2237 *size = sizeof(INTERNET_CERTIFICATE_INFOA);
2238 return ERROR_INSUFFICIENT_BUFFER;
2239 }
2240
2241 context = (PCCERT_CONTEXT)NETCON_GetCert(req->netconn);
2242 if(context) {
2243 INTERNET_CERTIFICATE_INFOA *info = (INTERNET_CERTIFICATE_INFOA*)buffer;
2244 DWORD len;
2245
2246 memset(info, 0, sizeof(*info));
2247 info->ftExpiry = context->pCertInfo->NotAfter;
2248 info->ftStart = context->pCertInfo->NotBefore;
2249 len = CertNameToStrA(context->dwCertEncodingType,
2250 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG, NULL, 0);
2251 info->lpszSubjectInfo = LocalAlloc(0, len);
2252 if(info->lpszSubjectInfo)
2253 CertNameToStrA(context->dwCertEncodingType,
2254 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG,
2255 info->lpszSubjectInfo, len);
2256 len = CertNameToStrA(context->dwCertEncodingType,
2257 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG, NULL, 0);
2258 info->lpszIssuerInfo = LocalAlloc(0, len);
2259 if(info->lpszIssuerInfo)
2260 CertNameToStrA(context->dwCertEncodingType,
2261 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG,
2262 info->lpszIssuerInfo, len);
2263 info->dwKeySize = NETCON_GetCipherStrength(req->netconn);
2264 CertFreeCertificateContext(context);
2265 return ERROR_SUCCESS;
2266 }
2267 return ERROR_NOT_SUPPORTED;
2268 }
2269 case INTERNET_OPTION_CONNECT_TIMEOUT:
2270 if (*size < sizeof(DWORD))
2271 return ERROR_INSUFFICIENT_BUFFER;
2272
2273 *size = sizeof(DWORD);
2274 *(DWORD *)buffer = req->connect_timeout;
2275 return ERROR_SUCCESS;
2276 case INTERNET_OPTION_REQUEST_FLAGS: {
2277 DWORD flags = 0;
2278
2279 if (*size < sizeof(DWORD))
2280 return ERROR_INSUFFICIENT_BUFFER;
2281
2282 /* FIXME: Add support for:
2283 * INTERNET_REQFLAG_FROM_CACHE
2284 * INTERNET_REQFLAG_CACHE_WRITE_DISABLED
2285 */
2286
2287 if(req->proxy)
2288 flags |= INTERNET_REQFLAG_VIA_PROXY;
2289 if(!req->status_code)
2290 flags |= INTERNET_REQFLAG_NO_HEADERS;
2291
2292 TRACE("INTERNET_OPTION_REQUEST_FLAGS returning %x\n", flags);
2293
2294 *size = sizeof(DWORD);
2295 *(DWORD*)buffer = flags;
2296 return ERROR_SUCCESS;
2297 }
2298 }
2299
2300 return INET_QueryOption(hdr, option, buffer, size, unicode);
2301 }
2302
2303 static DWORD HTTPREQ_SetOption(object_header_t *hdr, DWORD option, void *buffer, DWORD size)
2304 {
2305 http_request_t *req = (http_request_t*)hdr;
2306
2307 switch(option) {
2308 case 99: /* Undocumented, seems to be INTERNET_OPTION_SECURITY_FLAGS with argument validation */
2309 TRACE("Undocumented option 99\n");
2310
2311 if (!buffer || size != sizeof(DWORD))
2312 return ERROR_INVALID_PARAMETER;
2313 if(*(DWORD*)buffer & ~SECURITY_SET_MASK)
2314 return ERROR_INTERNET_OPTION_NOT_SETTABLE;
2315
2316 /* fall through */
2317 case INTERNET_OPTION_SECURITY_FLAGS:
2318 {
2319 DWORD flags;
2320
2321 if (!buffer || size != sizeof(DWORD))
2322 return ERROR_INVALID_PARAMETER;
2323 flags = *(DWORD *)buffer;
2324 TRACE("INTERNET_OPTION_SECURITY_FLAGS %08x\n", flags);
2325 flags &= SECURITY_SET_MASK;
2326 req->security_flags |= flags;
2327 if(is_valid_netconn(req->netconn))
2328 req->netconn->security_flags |= flags;
2329 return ERROR_SUCCESS;
2330 }
2331 case INTERNET_OPTION_CONNECT_TIMEOUT:
2332 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2333 req->connect_timeout = *(DWORD *)buffer;
2334 return ERROR_SUCCESS;
2335
2336 case INTERNET_OPTION_SEND_TIMEOUT:
2337 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2338 req->send_timeout = *(DWORD *)buffer;
2339 return ERROR_SUCCESS;
2340
2341 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2342 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2343 req->receive_timeout = *(DWORD *)buffer;
2344 return ERROR_SUCCESS;
2345
2346 case INTERNET_OPTION_USERNAME:
2347 heap_free(req->session->userName);
2348 if (!(req->session->userName = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2349 return ERROR_SUCCESS;
2350
2351 case INTERNET_OPTION_PASSWORD:
2352 heap_free(req->session->password);
2353 if (!(req->session->password = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2354 return ERROR_SUCCESS;
2355
2356 case INTERNET_OPTION_PROXY_USERNAME:
2357 heap_free(req->session->appInfo->proxyUsername);
2358 if (!(req->session->appInfo->proxyUsername = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2359 return ERROR_SUCCESS;
2360
2361 case INTERNET_OPTION_PROXY_PASSWORD:
2362 heap_free(req->session->appInfo->proxyPassword);
2363 if (!(req->session->appInfo->proxyPassword = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2364 return ERROR_SUCCESS;
2365
2366 case INTERNET_OPTION_HTTP_DECODING:
2367 if(size != sizeof(BOOL))
2368 return ERROR_INVALID_PARAMETER;
2369 req->decoding = *(BOOL*)buffer;
2370 return ERROR_SUCCESS;
2371 }
2372
2373 return INET_SetOption(hdr, option, buffer, size);
2374 }
2375
2376 static void commit_cache_entry(http_request_t *req)
2377 {
2378 WCHAR url[INTERNET_MAX_URL_LENGTH];
2379
2380 TRACE("%p\n", req);
2381
2382 CloseHandle(req->hCacheFile);
2383 req->hCacheFile = NULL;
2384
2385 if(HTTP_GetRequestURL(req, url)) {
2386 WCHAR *header;
2387 DWORD header_len;
2388 BOOL res;
2389
2390 header = build_response_header(req, TRUE);
2391 header_len = (header ? strlenW(header) : 0);
2392 res = CommitUrlCacheEntryW(url, req->req_file->file_name, req->expires,
2393 req->last_modified, NORMAL_CACHE_ENTRY,
2394 header, header_len, NULL, 0);
2395 if(res)
2396 req->req_file->is_committed = TRUE;
2397 else
2398 WARN("CommitUrlCacheEntry failed: %u\n", GetLastError());
2399 heap_free(header);
2400 }
2401 }
2402
2403 static void create_cache_entry(http_request_t *req)
2404 {
2405 static const WCHAR no_cacheW[] = {'n','o','-','c','a','c','h','e',0};
2406 static const WCHAR no_storeW[] = {'n','o','-','s','t','o','r','e',0};
2407
2408 WCHAR url[INTERNET_MAX_URL_LENGTH];
2409 WCHAR file_name[MAX_PATH+1];
2410 BOOL b = TRUE;
2411
2412 /* FIXME: We should free previous cache file earlier */
2413 if(req->req_file) {
2414 req_file_release(req->req_file);
2415 req->req_file = NULL;
2416 }
2417 if(req->hCacheFile) {
2418 CloseHandle(req->hCacheFile);
2419 req->hCacheFile = NULL;
2420 }
2421
2422 if(req->hdr.dwFlags & INTERNET_FLAG_NO_CACHE_WRITE)
2423 b = FALSE;
2424
2425 if(b) {
2426 int header_idx;
2427
2428 EnterCriticalSection( &req->headers_section );
2429
2430 header_idx = HTTP_GetCustomHeaderIndex(req, szCache_Control, 0, FALSE);
2431 if(header_idx != -1) {
2432 WCHAR *ptr;
2433
2434 for(ptr=req->custHeaders[header_idx].lpszValue; *ptr; ) {
2435 WCHAR *end;
2436
2437 while(*ptr==' ' || *ptr=='\t')
2438 ptr++;
2439
2440 end = strchrW(ptr, ',');
2441 if(!end)
2442 end = ptr + strlenW(ptr);
2443
2444 if(!strncmpiW(ptr, no_cacheW, sizeof(no_cacheW)/sizeof(*no_cacheW)-1)
2445 || !strncmpiW(ptr, no_storeW, sizeof(no_storeW)/sizeof(*no_storeW)-1)) {
2446 b = FALSE;
2447 break;
2448 }
2449
2450 ptr = end;
2451 if(*ptr == ',')
2452 ptr++;
2453 }
2454 }
2455
2456 LeaveCriticalSection( &req->headers_section );
2457 }
2458
2459 if(!b) {
2460 if(!(req->hdr.dwFlags & INTERNET_FLAG_NEED_FILE))
2461 return;
2462
2463 FIXME("INTERNET_FLAG_NEED_FILE is not supported correctly\n");
2464 }
2465
2466 b = HTTP_GetRequestURL(req, url);
2467 if(!b) {
2468 WARN("Could not get URL\n");
2469 return;
2470 }
2471
2472 b = CreateUrlCacheEntryW(url, req->contentLength == ~0u ? 0 : req->contentLength, NULL, file_name, 0);
2473 if(!b) {
2474 WARN("Could not create cache entry: %08x\n", GetLastError());
2475 return;
2476 }
2477
2478 create_req_file(file_name, &req->req_file);
2479
2480 req->hCacheFile = CreateFileW(file_name, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
2481 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2482 if(req->hCacheFile == INVALID_HANDLE_VALUE) {
2483 WARN("Could not create file: %u\n", GetLastError());
2484 req->hCacheFile = NULL;
2485 return;
2486 }
2487
2488 if(req->read_size) {
2489 DWORD written;
2490
2491 b = WriteFile(req->hCacheFile, req->read_buf+req->read_pos, req->read_size, &written, NULL);
2492 if(!b)
2493 FIXME("WriteFile failed: %u\n", GetLastError());
2494
2495 if(req->data_stream->vtbl->end_of_data(req->data_stream, req))
2496 commit_cache_entry(req);
2497 }
2498 }
2499
2500 /* read some more data into the read buffer (the read section must be held) */
2501 static DWORD read_more_data( http_request_t *req, int maxlen )
2502 {
2503 DWORD res;
2504 int len;
2505
2506 if (req->read_pos)
2507 {
2508 /* move existing data to the start of the buffer */
2509 if(req->read_size)
2510 memmove( req->read_buf, req->read_buf + req->read_pos, req->read_size );
2511 req->read_pos = 0;
2512 }
2513
2514 if (maxlen == -1) maxlen = sizeof(req->read_buf);
2515
2516 res = NETCON_recv( req->netconn, req->read_buf + req->read_size,
2517 maxlen - req->read_size, TRUE, &len );
2518 if(res == ERROR_SUCCESS)
2519 req->read_size += len;
2520
2521 return res;
2522 }
2523
2524 /* remove some amount of data from the read buffer (the read section must be held) */
2525 static void remove_data( http_request_t *req, int count )
2526 {
2527 if (!(req->read_size -= count)) req->read_pos = 0;
2528 else req->read_pos += count;
2529 }
2530
2531 static DWORD read_line( http_request_t *req, LPSTR buffer, DWORD *len )
2532 {
2533 int count, bytes_read, pos = 0;
2534 DWORD res;
2535
2536 EnterCriticalSection( &req->read_section );
2537 for (;;)
2538 {
2539 BYTE *eol = memchr( req->read_buf + req->read_pos, '\n', req->read_size );
2540
2541 if (eol)
2542 {
2543 count = eol - (req->read_buf + req->read_pos);
2544 bytes_read = count + 1;
2545 }
2546 else count = bytes_read = req->read_size;
2547
2548 count = min( count, *len - pos );
2549 memcpy( buffer + pos, req->read_buf + req->read_pos, count );
2550 pos += count;
2551 remove_data( req, bytes_read );
2552 if (eol) break;
2553
2554 if ((res = read_more_data( req, -1 )))
2555 {
2556 WARN( "read failed %u\n", res );
2557 LeaveCriticalSection( &req->read_section );
2558 return res;
2559 }
2560 if (!req->read_size)
2561 {
2562 *len = 0;
2563 TRACE( "returning empty string\n" );
2564 LeaveCriticalSection( &req->read_section );
2565 return ERROR_SUCCESS;
2566 }
2567 }
2568 LeaveCriticalSection( &req->read_section );
2569
2570 if (pos < *len)
2571 {
2572 if (pos && buffer[pos - 1] == '\r') pos--;
2573 *len = pos + 1;
2574 }
2575 buffer[*len - 1] = 0;
2576 TRACE( "returning %s\n", debugstr_a(buffer));
2577 return ERROR_SUCCESS;
2578 }
2579
2580 /* check if we have reached the end of the data to read (the read section must be held) */
2581 static BOOL end_of_read_data( http_request_t *req )
2582 {
2583 return !req->read_size && req->data_stream->vtbl->end_of_data(req->data_stream, req);
2584 }
2585
2586 static DWORD read_http_stream(http_request_t *req, BYTE *buf, DWORD size, DWORD *read, blocking_mode_t blocking_mode)
2587 {
2588 DWORD res;
2589
2590 res = req->data_stream->vtbl->read(req->data_stream, req, buf, size, read, blocking_mode);
2591 assert(*read <= size);
2592
2593 if(req->hCacheFile) {
2594 if(*read) {
2595 BOOL bres;
2596 DWORD written;
2597
2598 bres = WriteFile(req->hCacheFile, buf, *read, &written, NULL);
2599 if(!bres)
2600 FIXME("WriteFile failed: %u\n", GetLastError());
2601 }
2602
2603 if(req->data_stream->vtbl->end_of_data(req->data_stream, req))
2604 commit_cache_entry(req);
2605 }
2606
2607 return res;
2608 }
2609
2610 /* fetch some more data into the read buffer (the read section must be held) */
2611 static DWORD refill_read_buffer(http_request_t *req, blocking_mode_t blocking_mode, DWORD *read_bytes)
2612 {
2613 DWORD res, read=0;
2614
2615 if(req->read_size == sizeof(req->read_buf))
2616 return ERROR_SUCCESS;
2617
2618 if(req->read_pos) {
2619 if(req->read_size)
2620 memmove(req->read_buf, req->read_buf+req->read_pos, req->read_size);
2621 req->read_pos = 0;
2622 }
2623
2624 res = read_http_stream(req, req->read_buf+req->read_size, sizeof(req->read_buf) - req->read_size,
2625 &read, blocking_mode);
2626 req->read_size += read;
2627
2628 TRACE("read %u bytes, read_size %u\n", read, req->read_size);
2629 if(read_bytes)
2630 *read_bytes = read;
2631 return res;
2632 }
2633
2634 /* return the size of data available to be read immediately (the read section must be held) */
2635 static DWORD get_avail_data( http_request_t *req )
2636 {
2637 return req->read_size + req->data_stream->vtbl->get_avail_data(req->data_stream, req);
2638 }
2639
2640 static DWORD netconn_get_avail_data(data_stream_t *stream, http_request_t *req)
2641 {
2642 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2643 DWORD avail = 0;
2644
2645 if(is_valid_netconn(req->netconn))
2646 NETCON_query_data_available(req->netconn, &avail);
2647 return netconn_stream->content_length == ~0u
2648 ? avail
2649 : min(avail, netconn_stream->content_length-netconn_stream->content_read);
2650 }
2651
2652 static BOOL netconn_end_of_data(data_stream_t *stream, http_request_t *req)
2653 {
2654 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2655 return netconn_stream->content_read == netconn_stream->content_length || !is_valid_netconn(req->netconn);
2656 }
2657
2658 static DWORD netconn_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
2659 DWORD *read, blocking_mode_t blocking_mode)
2660 {
2661 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2662 DWORD res = ERROR_SUCCESS;
2663 int len = 0, ret = 0;
2664
2665 size = min(size, netconn_stream->content_length-netconn_stream->content_read);
2666
2667 if(size && is_valid_netconn(req->netconn)) {
2668 while((res = NETCON_recv(req->netconn, buf+ret, size-ret, blocking_mode != BLOCKING_DISALLOW, &len)) == ERROR_SUCCESS) {
2669 if(!len) {
2670 netconn_stream->content_length = netconn_stream->content_read;
2671 break;
2672 }
2673 ret += len;
2674 netconn_stream->content_read += len;
2675 if(blocking_mode != BLOCKING_WAITALL || size == ret)
2676 break;
2677 }
2678
2679 if(ret || (blocking_mode == BLOCKING_DISALLOW && res == WSAEWOULDBLOCK))
2680 res = ERROR_SUCCESS;
2681 }
2682
2683 TRACE("read %u bytes\n", ret);
2684 *read = ret;
2685 return res;
2686 }
2687
2688 static BOOL netconn_drain_content(data_stream_t *stream, http_request_t *req)
2689 {
2690 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2691 BYTE buf[1024];
2692 int len;
2693
2694 if(netconn_end_of_data(stream, req))
2695 return TRUE;
2696
2697 do {
2698 if(NETCON_recv(req->netconn, buf, sizeof(buf), FALSE, &len) != ERROR_SUCCESS)
2699 return FALSE;
2700
2701 netconn_stream->content_read += len;
2702 }while(netconn_stream->content_read < netconn_stream->content_length);
2703
2704 return TRUE;
2705 }
2706
2707 static void netconn_destroy(data_stream_t *stream)
2708 {
2709 }
2710
2711 static const data_stream_vtbl_t netconn_stream_vtbl = {
2712 netconn_get_avail_data,
2713 netconn_end_of_data,
2714 netconn_read,
2715 netconn_drain_content,
2716 netconn_destroy
2717 };
2718
2719 /* read some more data into the read buffer (the read section must be held) */
2720 static DWORD read_more_chunked_data(chunked_stream_t *stream, http_request_t *req, int maxlen)
2721 {
2722 DWORD res;
2723 int len;
2724
2725 assert(!stream->end_of_data);
2726
2727 if (stream->buf_pos)
2728 {
2729 /* move existing data to the start of the buffer */
2730 if(stream->buf_size)
2731 memmove(stream->buf, stream->buf + stream->buf_pos, stream->buf_size);
2732 stream->buf_pos = 0;
2733 }
2734
2735 if (maxlen == -1) maxlen = sizeof(stream->buf);
2736
2737 res = NETCON_recv( req->netconn, stream->buf + stream->buf_size,
2738 maxlen - stream->buf_size, TRUE, &len );
2739 if(res == ERROR_SUCCESS)
2740 stream->buf_size += len;
2741
2742 return res;
2743 }
2744
2745 /* remove some amount of data from the read buffer (the read section must be held) */
2746 static void remove_chunked_data(chunked_stream_t *stream, int count)
2747 {
2748 if (!(stream->buf_size -= count)) stream->buf_pos = 0;
2749 else stream->buf_pos += count;
2750 }
2751
2752 /* discard data contents until we reach end of line (the read section must be held) */
2753 static DWORD discard_chunked_eol(chunked_stream_t *stream, http_request_t *req)
2754 {
2755 DWORD res;
2756
2757 do
2758 {
2759 BYTE *eol = memchr(stream->buf + stream->buf_pos, '\n', stream->buf_size);
2760 if (eol)
2761 {
2762 remove_chunked_data(stream, (eol + 1) - (stream->buf + stream->buf_pos));
2763 break;
2764 }
2765 stream->buf_pos = stream->buf_size = 0; /* discard everything */
2766 if ((res = read_more_chunked_data(stream, req, -1)) != ERROR_SUCCESS) return res;
2767 } while (stream->buf_size);
2768 return ERROR_SUCCESS;
2769 }
2770
2771 /* read the size of the next chunk (the read section must be held) */
2772 static DWORD start_next_chunk(chunked_stream_t *stream, http_request_t *req)
2773 {
2774 DWORD chunk_size = 0, res;
2775
2776 assert(!stream->chunk_size || stream->chunk_size == ~0u);
2777
2778 if (stream->end_of_data) return ERROR_SUCCESS;
2779
2780 /* read terminator for the previous chunk */
2781 if(!stream->chunk_size && (res = discard_chunked_eol(stream, req)) != ERROR_SUCCESS)
2782 return res;
2783
2784 for (;;)
2785 {
2786 while (stream->buf_size)
2787 {
2788 char ch = stream->buf[stream->buf_pos];
2789 if (ch >= '0' && ch <= '9') chunk_size = chunk_size * 16 + ch - '0';
2790 else if (ch >= 'a' && ch <= 'f') chunk_size = chunk_size * 16 + ch - 'a' + 10;
2791 else if (ch >= 'A' && ch <= 'F') chunk_size = chunk_size * 16 + ch - 'A' + 10;
2792 else if (ch == ';' || ch == '\r' || ch == '\n')
2793 {
2794 TRACE( "reading %u byte chunk\n", chunk_size );
2795 stream->chunk_size = chunk_size;
2796 if (req->contentLength == ~0u) req->contentLength = chunk_size;
2797 else req->contentLength += chunk_size;
2798
2799 /* eat the rest of this line */
2800 if ((res = discard_chunked_eol(stream, req)) != ERROR_SUCCESS)
2801 return res;
2802
2803 /* if there's chunk data, return now */
2804 if (chunk_size) return ERROR_SUCCESS;
2805
2806 /* otherwise, eat the terminator for this chunk */
2807 if ((res = discard_chunked_eol(stream, req)) != ERROR_SUCCESS)
2808 return res;
2809
2810 stream->end_of_data = TRUE;
2811 return ERROR_SUCCESS;
2812 }
2813 remove_chunked_data(stream, 1);
2814 }
2815 if ((res = read_more_chunked_data(stream, req, -1)) != ERROR_SUCCESS) return res;
2816 if (!stream->buf_size)
2817 {
2818 stream->chunk_size = 0;
2819 return ERROR_SUCCESS;
2820 }
2821 }
2822 }
2823
2824 static DWORD chunked_get_avail_data(data_stream_t *stream, http_request_t *req)
2825 {
2826 /* Allow reading only from read buffer */
2827 return 0;
2828 }
2829
2830 static BOOL chunked_end_of_data(data_stream_t *stream, http_request_t *req)
2831 {
2832 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2833 return chunked_stream->end_of_data;
2834 }
2835
2836 static DWORD chunked_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
2837 DWORD *read, blocking_mode_t blocking_mode)
2838 {
2839 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2840 DWORD read_bytes = 0, ret_read = 0, res = ERROR_SUCCESS;
2841
2842 if(!chunked_stream->chunk_size || chunked_stream->chunk_size == ~0u) {
2843 res = start_next_chunk(chunked_stream, req);
2844 if(res != ERROR_SUCCESS)
2845 return res;
2846 }
2847
2848 while(size && chunked_stream->chunk_size && !chunked_stream->end_of_data) {
2849 if(chunked_stream->buf_size) {
2850 read_bytes = min(size, min(chunked_stream->buf_size, chunked_stream->chunk_size));
2851
2852 /* this could block */
2853 if(blocking_mode == BLOCKING_DISALLOW && read_bytes == chunked_stream->chunk_size)
2854 break;
2855
2856 memcpy(buf+ret_read, chunked_stream->buf+chunked_stream->buf_pos, read_bytes);
2857 remove_chunked_data(chunked_stream, read_bytes);
2858 }else {
2859 read_bytes = min(size, chunked_stream->chunk_size);
2860
2861 if(blocking_mode == BLOCKING_DISALLOW) {
2862 DWORD avail;
2863
2864 if(!is_valid_netconn(req->netconn) || !NETCON_query_data_available(req->netconn, &avail) || !avail)
2865 break;
2866 if(read_bytes > avail)
2867 read_bytes = avail;
2868
2869 /* this could block */
2870 if(read_bytes == chunked_stream->chunk_size)
2871 break;
2872 }
2873
2874 res = NETCON_recv(req->netconn, (char *)buf+ret_read, read_bytes, TRUE, (int*)&read_bytes);
2875 if(res != ERROR_SUCCESS)
2876 break;
2877 }
2878
2879 chunked_stream->chunk_size -= read_bytes;
2880 size -= read_bytes;
2881 ret_read += read_bytes;
2882 if(size && !chunked_stream->chunk_size) {
2883 assert(blocking_mode != BLOCKING_DISALLOW);
2884 res = start_next_chunk(chunked_stream, req);
2885 if(res != ERROR_SUCCESS)
2886 break;
2887 }
2888
2889 if(blocking_mode == BLOCKING_ALLOW)
2890 blocking_mode = BLOCKING_DISALLOW;
2891 }
2892
2893 TRACE("read %u bytes\n", ret_read);
2894 *read = ret_read;
2895 return res;
2896 }
2897
2898 static BOOL chunked_drain_content(data_stream_t *stream, http_request_t *req)
2899 {
2900 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2901
2902 remove_chunked_data(chunked_stream, chunked_stream->buf_size);
2903 return chunked_stream->end_of_data;
2904 }
2905
2906 static void chunked_destroy(data_stream_t *stream)
2907 {
2908 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2909 heap_free(chunked_stream);
2910 }
2911
2912 static const data_stream_vtbl_t chunked_stream_vtbl = {
2913 chunked_get_avail_data,
2914 chunked_end_of_data,
2915 chunked_read,
2916 chunked_drain_content,
2917 chunked_destroy
2918 };
2919
2920 /* set the request content length based on the headers */
2921 static DWORD set_content_length(http_request_t *request)
2922 {
2923 static const WCHAR szChunked[] = {'c','h','u','n','k','e','d',0};
2924 static const WCHAR headW[] = {'H','E','A','D',0};
2925 WCHAR encoding[20];
2926 DWORD size;
2927
2928 if(request->status_code == HTTP_STATUS_NO_CONTENT || !strcmpW(request->verb, headW)) {
2929 request->contentLength = request->netconn_stream.content_length = 0;
2930 return ERROR_SUCCESS;
2931 }
2932
2933 size = sizeof(request->contentLength);
2934 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
2935 &request->contentLength, &size, NULL) != ERROR_SUCCESS)
2936 request->contentLength = ~0u;
2937 request->netconn_stream.content_length = request->contentLength;
2938 request->netconn_stream.content_read = request->read_size;
2939
2940 size = sizeof(encoding);
2941 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_TRANSFER_ENCODING, encoding, &size, NULL) == ERROR_SUCCESS &&
2942 !strcmpiW(encoding, szChunked))
2943 {
2944 chunked_stream_t *chunked_stream;
2945
2946 chunked_stream = heap_alloc(sizeof(*chunked_stream));
2947 if(!chunked_stream)
2948 return ERROR_OUTOFMEMORY;
2949
2950 chunked_stream->data_stream.vtbl = &chunked_stream_vtbl;
2951 chunked_stream->buf_size = chunked_stream->buf_pos = 0;
2952 chunked_stream->chunk_size = ~0u;
2953 chunked_stream->end_of_data = FALSE;
2954
2955 if(request->read_size) {
2956 memcpy(chunked_stream->buf, request->read_buf+request->read_pos, request->read_size);
2957 chunked_stream->buf_size = request->read_size;
2958 request->read_size = request->read_pos = 0;
2959 }
2960
2961 request->data_stream = &chunked_stream->data_stream;
2962 request->contentLength = ~0u;
2963 }
2964
2965 if(request->decoding) {
2966 int encoding_idx;
2967
2968 static const WCHAR deflateW[] = {'d','e','f','l','a','t','e',0};
2969 static const WCHAR gzipW[] = {'g','z','i','p',0};
2970
2971 EnterCriticalSection( &request->headers_section );
2972
2973 encoding_idx = HTTP_GetCustomHeaderIndex(request, szContent_Encoding, 0, FALSE);
2974 if(encoding_idx != -1) {
2975 if(!strcmpiW(request->custHeaders[encoding_idx].lpszValue, gzipW)) {
2976 HTTP_DeleteCustomHeader(request, encoding_idx);
2977 LeaveCriticalSection( &request->headers_section );
2978 return init_gzip_stream(request, TRUE);
2979 }
2980 if(!strcmpiW(request->custHeaders[encoding_idx].lpszValue, deflateW)) {
2981 HTTP_DeleteCustomHeader(request, encoding_idx);
2982 LeaveCriticalSection( &request->headers_section );
2983 return init_gzip_stream(request, FALSE);
2984 }
2985 }
2986
2987 LeaveCriticalSection( &request->headers_section );
2988 }
2989
2990 return ERROR_SUCCESS;
2991 }
2992
2993 static void send_request_complete(http_request_t *req, DWORD_PTR result, DWORD error)
2994 {
2995 INTERNET_ASYNC_RESULT iar;
2996
2997 iar.dwResult = result;
2998 iar.dwError = error;
2999
3000 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE, &iar,
3001 sizeof(INTERNET_ASYNC_RESULT));
3002 }
3003
3004 static void HTTP_ReceiveRequestData(http_request_t *req, BOOL first_notif, DWORD *ret_size)
3005 {
3006 DWORD res, read = 0, avail = 0;
3007 blocking_mode_t mode;
3008
3009 TRACE("%p\n", req);
3010
3011 EnterCriticalSection( &req->read_section );
3012
3013 mode = first_notif && req->read_size ? BLOCKING_DISALLOW : BLOCKING_ALLOW;
3014 res = refill_read_buffer(req, mode, &read);
3015 if(res == ERROR_SUCCESS)
3016 avail = get_avail_data(req);
3017
3018 LeaveCriticalSection( &req->read_section );
3019
3020 if(res != ERROR_SUCCESS || (mode != BLOCKING_DISALLOW && !read)) {
3021 WARN("res %u read %u, closing connection\n", res, read);
3022 http_release_netconn(req, FALSE);
3023 }
3024
3025 if(res != ERROR_SUCCESS) {
3026 send_request_complete(req, 0, res);
3027 return;
3028 }
3029
3030 if(ret_size)
3031 *ret_size = avail;
3032 if(first_notif)
3033 avail = 0;
3034
3035 send_request_complete(req, req->session->hdr.dwInternalFlags & INET_OPENURL ? (DWORD_PTR)req->hdr.hInternet : 1, avail);
3036 }
3037
3038 /* read data from the http connection (the read section must be held) */
3039 static DWORD HTTPREQ_Read(http_request_t *req, void *buffer, DWORD size, DWORD *read, BOOL sync)
3040 {
3041 DWORD current_read = 0, ret_read = 0;
3042 blocking_mode_t blocking_mode;
3043 DWORD res = ERROR_SUCCESS;
3044
3045 blocking_mode = req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC ? BLOCKING_ALLOW : BLOCKING_WAITALL;
3046
3047 EnterCriticalSection( &req->read_section );
3048
3049 if(req->read_size) {
3050 ret_read = min(size, req->read_size);
3051 memcpy(buffer, req->read_buf+req->read_pos, ret_read);
3052 req->read_size -= ret_read;
3053 req->read_pos += ret_read;
3054 if(blocking_mode == BLOCKING_ALLOW)
3055 blocking_mode = BLOCKING_DISALLOW;
3056 }
3057
3058 if(ret_read < size) {
3059 res = read_http_stream(req, (BYTE*)buffer+ret_read, size-ret_read, &current_read, blocking_mode);
3060 ret_read += current_read;
3061 }
3062
3063 LeaveCriticalSection( &req->read_section );
3064
3065 *read = ret_read;
3066 TRACE( "retrieved %u bytes (%u)\n", ret_read, req->contentLength );
3067
3068 if(size && !ret_read)
3069 http_release_netconn(req, res == ERROR_SUCCESS);
3070
3071 return res;
3072 }
3073
3074 static BOOL drain_content(http_request_t *req, BOOL blocking)
3075 {
3076 BOOL ret;
3077
3078 if(!is_valid_netconn(req->netconn) || req->contentLength == -1)
3079 return FALSE;
3080
3081 if(!strcmpW(req->verb, szHEAD))
3082 return TRUE;
3083
3084 if(!blocking)
3085 return req->data_stream->vtbl->drain_content(req->data_stream, req);
3086
3087 EnterCriticalSection( &req->read_section );
3088
3089 while(1) {
3090 DWORD bytes_read, res;
3091 BYTE buf[4096];
3092
3093 res = HTTPREQ_Read(req, buf, sizeof(buf), &bytes_read, TRUE);
3094 if(res != ERROR_SUCCESS) {
3095 ret = FALSE;
3096 break;
3097 }
3098 if(!bytes_read) {
3099 ret = TRUE;
3100 break;
3101 }
3102 }
3103
3104 LeaveCriticalSection( &req->read_section );
3105 return ret;
3106 }
3107
3108 static DWORD HTTPREQ_ReadFile(object_header_t *hdr, void *buffer, DWORD size, DWORD *read)
3109 {
3110 http_request_t *req = (http_request_t*)hdr;
3111 DWORD res;
3112
3113 EnterCriticalSection( &req->read_section );
3114 if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3115 hdr->dwError = ERROR_INTERNET_INTERNAL_ERROR;
3116
3117 res = HTTPREQ_Read(req, buffer, size, read, TRUE);
3118 if(res == ERROR_SUCCESS)
3119 res = hdr->dwError;
3120 LeaveCriticalSection( &req->read_section );
3121
3122 return res;
3123 }
3124
3125 typedef struct {
3126 task_header_t hdr;
3127 void *buf;
3128 DWORD size;
3129 DWORD *ret_read;
3130 } read_file_ex_task_t;
3131
3132 static void AsyncReadFileExProc(task_header_t *hdr)
3133 {
3134 read_file_ex_task_t *task = (read_file_ex_task_t*)hdr;
3135 http_request_t *req = (http_request_t*)task->hdr.hdr;
3136 DWORD res;
3137
3138 TRACE("INTERNETREADFILEEXW %p\n", task->hdr.hdr);
3139
3140 res = HTTPREQ_Read(req, task->buf, task->size, task->ret_read, TRUE);
3141 send_request_complete(req, res == ERROR_SUCCESS, res);
3142 }
3143
3144 static DWORD HTTPREQ_ReadFileEx(object_header_t *hdr, void *buf, DWORD size, DWORD *ret_read,
3145 DWORD flags, DWORD_PTR context)
3146 {
3147
3148 http_request_t *req = (http_request_t*)hdr;
3149 DWORD res, read, cread, error = ERROR_SUCCESS;
3150
3151 if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
3152 FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
3153
3154 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3155
3156 if (hdr->dwFlags & INTERNET_FLAG_ASYNC)
3157 {
3158 read_file_ex_task_t *task;
3159
3160 if (TryEnterCriticalSection( &req->read_section ))
3161 {
3162 if (get_avail_data(req))
3163 {
3164 res = HTTPREQ_Read(req, buf, size, &read, FALSE);
3165 LeaveCriticalSection( &req->read_section );
3166 goto done;
3167 }
3168 LeaveCriticalSection( &req->read_section );
3169 }
3170
3171 task = alloc_async_task(&req->hdr, AsyncReadFileExProc, sizeof(*task));
3172 task->buf = buf;
3173 task->size = size;
3174 task->ret_read = ret_read;
3175
3176 INTERNET_AsyncCall(&task->hdr);
3177
3178 return ERROR_IO_PENDING;
3179 }
3180
3181 read = 0;
3182
3183 EnterCriticalSection( &req->read_section );
3184 if(hdr->dwError == ERROR_SUCCESS)
3185 hdr->dwError = INTERNET_HANDLE_IN_USE;
3186 else if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3187 hdr->dwError = ERROR_INTERNET_INTERNAL_ERROR;
3188
3189 while(1) {
3190 res = HTTPREQ_Read(req, (char*)buf+read, size-read, &cread, !(flags & IRF_NO_WAIT));
3191 if(res != ERROR_SUCCESS)
3192 break;
3193
3194 read += cread;
3195 if(read == size || end_of_read_data(req))
3196 break;
3197
3198 LeaveCriticalSection( &req->read_section );
3199
3200 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
3201 &cread, sizeof(cread));
3202 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
3203 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3204
3205 EnterCriticalSection( &req->read_section );
3206 }
3207
3208 if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3209 hdr->dwError = ERROR_SUCCESS;
3210 else
3211 error = hdr->dwError;
3212
3213 LeaveCriticalSection( &req->read_section );
3214
3215 done:
3216 *ret_read = read;
3217 if (res == ERROR_SUCCESS) {
3218 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
3219 &read, sizeof(read));
3220 }
3221
3222 return res==ERROR_SUCCESS ? error : res;
3223 }
3224
3225 static DWORD HTTPREQ_WriteFile(object_header_t *hdr, const void *buffer, DWORD size, DWORD *written)
3226 {
3227 DWORD res;
3228 http_request_t *request = (http_request_t*)hdr;
3229
3230 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
3231
3232 *written = 0;
3233 res = NETCON_send(request->netconn, buffer, size, 0, (LPINT)written);
3234 if (res == ERROR_SUCCESS)
3235 request->bytesWritten += *written;
3236
3237 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REQUEST_SENT, written, sizeof(DWORD));
3238 return res;
3239 }
3240
3241 typedef struct {
3242 task_header_t hdr;
3243 DWORD *ret_size;
3244 } http_data_available_task_t;
3245
3246 static void AsyncQueryDataAvailableProc(task_header_t *hdr)
3247 {
3248 http_data_available_task_t *task = (http_data_available_task_t*)hdr;
3249
3250 HTTP_ReceiveRequestData((http_request_t*)task->hdr.hdr, FALSE, task->ret_size);
3251 }
3252
3253 static DWORD HTTPREQ_QueryDataAvailable(object_header_t *hdr, DWORD *available, DWORD flags, DWORD_PTR ctx)
3254 {
3255 http_request_t *req = (http_request_t*)hdr;
3256
3257 TRACE("(%p %p %x %lx)\n", req, available, flags, ctx);
3258
3259 if (req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3260 {
3261 http_data_available_task_t *task;
3262
3263 /* never wait, if we can't enter the section we queue an async request right away */
3264 if (TryEnterCriticalSection( &req->read_section ))
3265 {
3266 refill_read_buffer(req, BLOCKING_DISALLOW, NULL);
3267 if ((*available = get_avail_data( req ))) goto done;
3268 if (end_of_read_data( req )) goto done;
3269 LeaveCriticalSection( &req->read_section );
3270 }
3271
3272 task = alloc_async_task(&req->hdr, AsyncQueryDataAvailableProc, sizeof(*task));
3273 task->ret_size = available;
3274 INTERNET_AsyncCall(&task->hdr);
3275 return ERROR_IO_PENDING;
3276 }
3277
3278 EnterCriticalSection( &req->read_section );
3279
3280 if (!(*available = get_avail_data( req )) && !end_of_read_data( req ))
3281 {
3282 refill_read_buffer( req, BLOCKING_ALLOW, NULL );
3283 *available = get_avail_data( req );
3284 }
3285
3286 done:
3287 LeaveCriticalSection( &req->read_section );
3288
3289 TRACE( "returning %u\n", *available );
3290 return ERROR_SUCCESS;
3291 }
3292
3293 static DWORD HTTPREQ_LockRequestFile(object_header_t *hdr, req_file_t **ret)
3294 {
3295 http_request_t *req = (http_request_t*)hdr;
3296
3297 TRACE("(%p)\n", req);
3298
3299 if(!req->req_file) {
3300 WARN("No cache file name available\n");
3301 return ERROR_FILE_NOT_FOUND;
3302 }
3303
3304 *ret = req_file_addref(req->req_file);
3305 return ERROR_SUCCESS;
3306 }
3307
3308 static const object_vtbl_t HTTPREQVtbl = {
3309 HTTPREQ_Destroy,
3310 HTTPREQ_CloseConnection,
3311 HTTPREQ_QueryOption,
3312 HTTPREQ_SetOption,
3313 HTTPREQ_ReadFile,
3314 HTTPREQ_ReadFileEx,
3315 HTTPREQ_WriteFile,
3316 HTTPREQ_QueryDataAvailable,
3317 NULL,
3318 HTTPREQ_LockRequestFile
3319 };
3320
3321 /***********************************************************************
3322 * HTTP_HttpOpenRequestW (internal)
3323 *
3324 * Open a HTTP request handle
3325 *
3326 * RETURNS
3327 * HINTERNET a HTTP request handle on success
3328 * NULL on failure
3329 *
3330 */
3331 static DWORD HTTP_HttpOpenRequestW(http_session_t *session,
3332 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
3333 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
3334 DWORD dwFlags, DWORD_PTR dwContext, HINTERNET *ret)
3335 {
3336 appinfo_t *hIC = session->appInfo;
3337 http_request_t *request;
3338 DWORD len;
3339
3340 TRACE("-->\n");
3341
3342 request = alloc_object(&session->hdr, &HTTPREQVtbl, sizeof(http_request_t));
3343 if(!request)
3344 return ERROR_OUTOFMEMORY;
3345
3346 request->hdr.htype = WH_HHTTPREQ;
3347 request->hdr.dwFlags = dwFlags;
3348 request->hdr.dwContext = dwContext;
3349 request->contentLength = ~0u;
3350
3351 request->netconn_stream.data_stream.vtbl = &netconn_stream_vtbl;
3352 request->data_stream = &request->netconn_stream.data_stream;
3353 request->connect_timeout = session->connect_timeout;
3354 request->send_timeout = session->send_timeout;
3355 request->receive_timeout = session->receive_timeout;
3356
3357 InitializeCriticalSection( &request->headers_section );
3358 request->headers_section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": http_request_t.headers_section");
3359
3360 InitializeCriticalSection( &request->read_section );
3361 request->read_section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": http_request_t.read_section");
3362
3363 WININET_AddRef( &session->hdr );
3364 request->session = session;
3365 list_add_head( &session->hdr.children, &request->hdr.entry );
3366
3367 request->server = get_server(session->hostName, session->hostPort, (dwFlags & INTERNET_FLAG_SECURE) != 0, TRUE);
3368 if(!request->server) {
3369 WININET_Release(&request->hdr);
3370 return ERROR_OUTOFMEMORY;
3371 }
3372
3373 if (dwFlags & INTERNET_FLAG_IGNORE_CERT_CN_INVALID)
3374 request->security_flags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
3375 if (dwFlags & INTERNET_FLAG_IGNORE_CERT_DATE_INVALID)
3376 request->security_flags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
3377
3378 if (lpszObjectName && *lpszObjectName) {
3379 HRESULT rc;
3380
3381 len = 0;
3382 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
3383 if (rc != E_POINTER)
3384 len = strlenW(lpszObjectName)+1;
3385 request->path = heap_alloc(len*sizeof(WCHAR));
3386 rc = UrlEscapeW(lpszObjectName, request->path, &len,
3387 URL_ESCAPE_SPACES_ONLY);
3388 if (rc != S_OK)
3389 {
3390 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc);
3391 strcpyW(request->path,lpszObjectName);
3392 }
3393 }else {
3394 static const WCHAR slashW[] = {'/',0};
3395
3396 request->path = heap_strdupW(slashW);
3397 }
3398
3399 if (lpszReferrer && *lpszReferrer)
3400 HTTP_ProcessHeader(request, HTTP_REFERER, lpszReferrer, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
3401
3402 if (lpszAcceptTypes)
3403 {
3404 int i;
3405 for (i = 0; lpszAcceptTypes[i]; i++)
3406 {
3407 if (!*lpszAcceptTypes[i]) continue;
3408 HTTP_ProcessHeader(request, HTTP_ACCEPT, lpszAcceptTypes[i],
3409 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA |
3410 HTTP_ADDHDR_FLAG_REQ |
3411 (i == 0 ? (HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_ADD) : 0));
3412 }
3413 }
3414
3415 request->verb = heap_strdupW(lpszVerb && *lpszVerb ? lpszVerb : szGET);
3416 request->version = heap_strdupW(lpszVersion && *lpszVersion ? lpszVersion : g_szHttp1_1);
3417
3418 if (hIC->proxy && hIC->proxy[0] && !HTTP_ShouldBypassProxy(hIC, session->hostName))
3419 HTTP_DealWithProxy( hIC, session, request );
3420
3421 INTERNET_SendCallback(&session->hdr, dwContext,
3422 INTERNET_STATUS_HANDLE_CREATED, &request->hdr.hInternet,
3423 sizeof(HINTERNET));
3424
3425 TRACE("<-- (%p)\n", request);
3426
3427 *ret = request->hdr.hInternet;
3428 return ERROR_SUCCESS;
3429 }
3430
3431 /***********************************************************************
3432 * HttpOpenRequestW (WININET.@)
3433 *
3434 * Open a HTTP request handle
3435 *
3436 * RETURNS
3437 * HINTERNET a HTTP request handle on success
3438 * NULL on failure
3439 *
3440 */
3441 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
3442 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
3443 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
3444 DWORD dwFlags, DWORD_PTR dwContext)
3445 {
3446 http_session_t *session;
3447 HINTERNET handle = NULL;
3448 DWORD res;
3449
3450 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
3451 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
3452 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
3453 dwFlags, dwContext);
3454 if(lpszAcceptTypes!=NULL)
3455 {
3456 int i;
3457 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
3458 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
3459 }
3460
3461 session = (http_session_t*) get_handle_object( hHttpSession );
3462 if (NULL == session || session->hdr.htype != WH_HHTTPSESSION)
3463 {
3464 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3465 goto lend;
3466 }
3467
3468 /*
3469 * My tests seem to show that the windows version does not
3470 * become asynchronous until after this point. And anyhow
3471 * if this call was asynchronous then how would you get the
3472 * necessary HINTERNET pointer returned by this function.
3473 *
3474 */
3475 res = HTTP_HttpOpenRequestW(session, lpszVerb, lpszObjectName,
3476 lpszVersion, lpszReferrer, lpszAcceptTypes,
3477 dwFlags, dwContext, &handle);
3478 lend:
3479 if( session )
3480 WININET_Release( &session->hdr );
3481 TRACE("returning %p\n", handle);
3482 if(res != ERROR_SUCCESS)
3483 SetLastError(res);
3484 return handle;
3485 }
3486
3487 static const LPCWSTR header_lookup[] = {
3488 szMime_Version, /* HTTP_QUERY_MIME_VERSION = 0 */
3489 szContent_Type, /* HTTP_QUERY_CONTENT_TYPE = 1 */
3490 szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
3491 szContent_ID, /* HTTP_QUERY_CONTENT_ID = 3 */
3492 NULL, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
3493 szContent_Length, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
3494 szContent_Language, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
3495 szAllow, /* HTTP_QUERY_ALLOW = 7 */
3496 szPublic, /* HTTP_QUERY_PUBLIC = 8 */
3497 szDate, /* HTTP_QUERY_DATE = 9 */
3498 szExpires, /* HTTP_QUERY_EXPIRES = 10 */
3499 szLast_Modified, /* HTTP_QUERY_LAST_MODIFIED = 11 */
3500 NULL, /* HTTP_QUERY_MESSAGE_ID = 12 */
3501 szURI, /* HTTP_QUERY_URI = 13 */
3502 szFrom, /* HTTP_QUERY_DERIVED_FROM = 14 */
3503 NULL, /* HTTP_QUERY_COST = 15 */
3504 NULL, /* HTTP_QUERY_LINK = 16 */
3505 szPragma, /* HTTP_QUERY_PRAGMA = 17 */
3506 NULL, /* HTTP_QUERY_VERSION = 18 */
3507 szStatus, /* HTTP_QUERY_STATUS_CODE = 19 */
3508 NULL, /* HTTP_QUERY_STATUS_TEXT = 20 */
3509 NULL, /* HTTP_QUERY_RAW_HEADERS = 21 */
3510 NULL, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
3511 szConnection, /* HTTP_QUERY_CONNECTION = 23 */
3512 szAccept, /* HTTP_QUERY_ACCEPT = 24 */
3513 szAccept_Charset, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
3514 szAccept_Encoding, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
3515 szAccept_Language, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
3516 szAuthorization, /* HTTP_QUERY_AUTHORIZATION = 28 */
3517 szContent_Encoding, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
3518 NULL, /* HTTP_QUERY_FORWARDED = 30 */
3519 NULL, /* HTTP_QUERY_FROM = 31 */
3520 szIf_Modified_Since, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
3521 szLocation, /* HTTP_QUERY_LOCATION = 33 */
3522 NULL, /* HTTP_QUERY_ORIG_URI = 34 */
3523 szReferer, /* HTTP_QUERY_REFERER = 35 */
3524 szRetry_After, /* HTTP_QUERY_RETRY_AFTER = 36 */
3525 szServer, /* HTTP_QUERY_SERVER = 37 */
3526 NULL, /* HTTP_TITLE = 38 */
3527 szUser_Agent, /* HTTP_QUERY_USER_AGENT = 39 */
3528 szWWW_Authenticate, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
3529 szProxy_Authenticate, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
3530 szAccept_Ranges, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
3531 szSet_Cookie, /* HTTP_QUERY_SET_COOKIE = 43 */
3532 szCookie, /* HTTP_QUERY_COOKIE = 44 */
3533 NULL, /* HTTP_QUERY_REQUEST_METHOD = 45 */
3534 NULL, /* HTTP_QUERY_REFRESH = 46 */
3535 szContent_Disposition, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
3536 szAge, /* HTTP_QUERY_AGE = 48 */
3537 szCache_Control, /* HTTP_QUERY_CACHE_CONTROL = 49 */
3538 szContent_Base, /* HTTP_QUERY_CONTENT_BASE = 50 */
3539 szContent_Location, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
3540 szContent_MD5, /* HTTP_QUERY_CONTENT_MD5 = 52 */
3541 szContent_Range, /* HTTP_QUERY_CONTENT_RANGE = 53 */
3542 szETag, /* HTTP_QUERY_ETAG = 54 */
3543 hostW, /* HTTP_QUERY_HOST = 55 */
3544 szIf_Match, /* HTTP_QUERY_IF_MATCH = 56 */
3545 szIf_None_Match, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
3546 szIf_Range, /* HTTP_QUERY_IF_RANGE = 58 */
3547 szIf_Unmodified_Since, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
3548 szMax_Forwards, /* HTTP_QUERY_MAX_FORWARDS = 60 */
3549 szProxy_Authorization, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
3550 szRange, /* HTTP_QUERY_RANGE = 62 */
3551 szTransfer_Encoding, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
3552 szUpgrade, /* HTTP_QUERY_UPGRADE = 64 */
3553 szVary, /* HTTP_QUERY_VARY = 65 */
3554 szVia, /* HTTP_QUERY_VIA = 66 */
3555 szWarning, /* HTTP_QUERY_WARNING = 67 */
3556 szExpect, /* HTTP_QUERY_EXPECT = 68 */
3557 szProxy_Connection, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
3558 szUnless_Modified_Since, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
3559 };
3560
3561 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
3562
3563 /***********************************************************************
3564 * HTTP_HttpQueryInfoW (internal)
3565 */
3566 static DWORD HTTP_HttpQueryInfoW(http_request_t *request, DWORD dwInfoLevel,
3567 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
3568 {
3569 LPHTTPHEADERW lphttpHdr = NULL;
3570 BOOL request_only = !!(dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS);
3571 INT requested_index = lpdwIndex ? *lpdwIndex : 0;
3572 DWORD level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
3573 INT index = -1;
3574
3575 EnterCriticalSection( &request->headers_section );
3576
3577 /* Find requested header structure */
3578 switch (level)
3579 {
3580 case HTTP_QUERY_CUSTOM:
3581 if (!lpBuffer)
3582 {
3583 LeaveCriticalSection( &request->headers_section );
3584 return ERROR_INVALID_PARAMETER;
3585 }
3586 index = HTTP_GetCustomHeaderIndex(request, lpBuffer, requested_index, request_only);
3587 break;
3588 case HTTP_QUERY_RAW_HEADERS_CRLF:
3589 {
3590 LPWSTR headers;
3591 DWORD len = 0;
3592 DWORD res = ERROR_INVALID_PARAMETER;
3593
3594 if (request_only)
3595 headers = build_request_header(request, request->verb, request->path, request->version, TRUE);
3596 else
3597 headers = build_response_header(request, TRUE);
3598 if (!headers)
3599 {
3600 LeaveCriticalSection( &request->headers_section );
3601 return ERROR_OUTOFMEMORY;
3602 }
3603
3604 len = strlenW(headers) * sizeof(WCHAR);
3605 if (len + sizeof(WCHAR) > *lpdwBufferLength)
3606 {
3607 len += sizeof(WCHAR);
3608 res = ERROR_INSUFFICIENT_BUFFER;
3609 }
3610 else if (lpBuffer)
3611 {
3612 memcpy(lpBuffer, headers, len + sizeof(WCHAR));
3613 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len / sizeof(WCHAR)));
3614 res = ERROR_SUCCESS;
3615 }
3616 *lpdwBufferLength = len;
3617
3618 heap_free(headers);
3619 LeaveCriticalSection( &request->headers_section );
3620 return res;
3621 }
3622 case HTTP_QUERY_RAW_HEADERS:
3623 {
3624 LPWSTR headers;
3625 DWORD len;
3626
3627 if (request_only)
3628 headers = build_request_header(request, request->verb, request->path, request->version, FALSE);
3629 else
3630 headers = build_response_header(request, FALSE);
3631
3632 if (!headers)
3633 {
3634 LeaveCriticalSection( &request->headers_section );
3635 return ERROR_OUTOFMEMORY;
3636 }
3637
3638 len = strlenW(headers) * sizeof(WCHAR);
3639 if (len > *lpdwBufferLength)
3640 {
3641 *lpdwBufferLength = len;
3642 heap_free(headers);
3643 LeaveCriticalSection( &request->headers_section );
3644 return ERROR_INSUFFICIENT_BUFFER;
3645 }
3646
3647 if (lpBuffer)
3648 {
3649 DWORD i;
3650
3651 TRACE("returning data: %s\n", debugstr_wn(headers, len / sizeof(WCHAR)));
3652
3653 for (i = 0; i < len / sizeof(WCHAR); i++)
3654 {
3655 if (headers[i] == '\n')
3656 headers[i] = 0;
3657 }
3658 memcpy(lpBuffer, headers, len);
3659 }
3660 *lpdwBufferLength = len - sizeof(WCHAR);
3661
3662 heap_free(headers);
3663 LeaveCriticalSection( &request->headers_section );
3664 return ERROR_SUCCESS;
3665 }
3666 case HTTP_QUERY_STATUS_TEXT:
3667 if (request->statusText)
3668 {
3669 DWORD len = strlenW(request->statusText);
3670 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
3671 {
3672 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
3673 LeaveCriticalSection( &request->headers_section );
3674 return ERROR_INSUFFICIENT_BUFFER;
3675 }
3676 if (lpBuffer)
3677 {
3678 memcpy(lpBuffer, request->statusText, (len + 1) * sizeof(WCHAR));
3679 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
3680 }
3681 *lpdwBufferLength = len * sizeof(WCHAR);
3682 LeaveCriticalSection( &request->headers_section );
3683 return ERROR_SUCCESS;
3684 }
3685 break;
3686 case HTTP_QUERY_VERSION:
3687 if (request->version)
3688 {
3689 DWORD len = strlenW(request->version);
3690 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
3691 {
3692 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
3693 LeaveCriticalSection( &request->headers_section );
3694 return ERROR_INSUFFICIENT_BUFFER;
3695 }
3696 if (lpBuffer)
3697 {
3698 memcpy(lpBuffer, request->version, (len + 1) * sizeof(WCHAR));
3699 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
3700 }
3701 *lpdwBufferLength = len * sizeof(WCHAR);
3702 LeaveCriticalSection( &request->headers_section );
3703 return ERROR_SUCCESS;
3704 }
3705 break;
3706 case HTTP_QUERY_CONTENT_ENCODING:
3707 index = HTTP_GetCustomHeaderIndex(request, header_lookup[request->read_gzip ? HTTP_QUERY_CONTENT_TYPE : level],
3708 requested_index,request_only);
3709 break;
3710 case HTTP_QUERY_STATUS_CODE: {
3711 DWORD res = ERROR_SUCCESS;
3712
3713 if(request_only)
3714 {
3715 LeaveCriticalSection( &request->headers_section );
3716 return ERROR_HTTP_INVALID_QUERY_REQUEST;
3717 }
3718
3719 if(requested_index)
3720 break;
3721
3722 if(dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) {
3723 if(*lpdwBufferLength >= sizeof(DWORD))
3724 *(DWORD*)lpBuffer = request->status_code;
3725 else
3726 res = ERROR_INSUFFICIENT_BUFFER;
3727 *lpdwBufferLength = sizeof(DWORD);
3728 }else {
3729 WCHAR buf[12];
3730 DWORD size;
3731 static const WCHAR formatW[] = {'%','u',0};
3732
3733 size = sprintfW(buf, formatW, request->status_code) * sizeof(WCHAR);
3734
3735 if(size <= *lpdwBufferLength) {
3736 memcpy(lpBuffer, buf, size+sizeof(WCHAR));
3737 }else {
3738 size += sizeof(WCHAR);
3739 res = ERROR_INSUFFICIENT_BUFFER;
3740 }
3741
3742 *lpdwBufferLength = size;
3743 }
3744 LeaveCriticalSection( &request->headers_section );
3745 return res;
3746 }
3747 default:
3748 assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
3749
3750 if (level < LAST_TABLE_HEADER && header_lookup[level])
3751 index = HTTP_GetCustomHeaderIndex(request, header_lookup[level],
3752 requested_index,request_only);
3753 }
3754
3755 if (index >= 0)
3756 lphttpHdr = &request->custHeaders[index];
3757
3758 /* Ensure header satisfies requested attributes */
3759 if (!lphttpHdr ||
3760 ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
3761 (~lphttpHdr->wFlags & HDR_ISREQUEST)))
3762 {
3763 LeaveCriticalSection( &request->headers_section );
3764 return ERROR_HTTP_HEADER_NOT_FOUND;
3765 }
3766
3767 /* coalesce value to requested type */
3768 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER && lpBuffer)
3769 {
3770 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
3771 TRACE(" returning number: %d\n", *(int *)lpBuffer);
3772 }
3773 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME && lpBuffer)
3774 {
3775 time_t tmpTime;
3776 struct tm tmpTM;
3777 SYSTEMTIME *STHook;
3778
3779 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
3780
3781 tmpTM = *gmtime(&tmpTime);
3782 STHook = (SYSTEMTIME *)lpBuffer;
3783 STHook->wDay = tmpTM.tm_mday;
3784 STHook->wHour = tmpTM.tm_hour;
3785 STHook->wMilliseconds = 0;
3786 STHook->wMinute = tmpTM.tm_min;
3787 STHook->wDayOfWeek = tmpTM.tm_wday;
3788 STHook->wMonth = tmpTM.tm_mon + 1;
3789 STHook->wSecond = tmpTM.tm_sec;
3790 STHook->wYear = 1900+tmpTM.tm_year;
3791
3792 TRACE(" returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
3793 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
3794 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
3795 }
3796 else if (lphttpHdr->lpszValue)
3797 {
3798 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
3799
3800 if (len > *lpdwBufferLength)
3801 {
3802 *lpdwBufferLength = len;
3803 LeaveCriticalSection( &request->headers_section );
3804 return ERROR_INSUFFICIENT_BUFFER;
3805 }
3806 if (lpBuffer)
3807 {
3808 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
3809 TRACE("! returning string: %s\n", debugstr_w(lpBuffer));
3810 }
3811 *lpdwBufferLength = len - sizeof(WCHAR);
3812 }
3813 if (lpdwIndex) (*lpdwIndex)++;
3814
3815 LeaveCriticalSection( &request->headers_section );
3816 return ERROR_SUCCESS;
3817 }
3818
3819 /***********************************************************************
3820 * HttpQueryInfoW (WININET.@)
3821 *
3822 * Queries for information about an HTTP request
3823 *
3824 * RETURNS
3825 * TRUE on success
3826 * FALSE on failure
3827 *
3828 */
3829 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
3830 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
3831 {
3832 http_request_t *request;
3833 DWORD res;
3834
3835 if (TRACE_ON(wininet)) {
3836 #define FE(x) { x, #x }
3837 static const wininet_flag_info query_flags[] = {
3838 FE(HTTP_QUERY_MIME_VERSION),
3839 FE(HTTP_QUERY_CONTENT_TYPE),
3840 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
3841 FE(HTTP_QUERY_CONTENT_ID),
3842 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
3843 FE(HTTP_QUERY_CONTENT_LENGTH),
3844 FE(HTTP_QUERY_CONTENT_LANGUAGE),
3845 FE(HTTP_QUERY_ALLOW),
3846 FE(HTTP_QUERY_PUBLIC),
3847 FE(HTTP_QUERY_DATE),
3848 FE(HTTP_QUERY_EXPIRES),
3849 FE(HTTP_QUERY_LAST_MODIFIED),
3850 FE(HTTP_QUERY_MESSAGE_ID),
3851 FE(HTTP_QUERY_URI),
3852 FE(HTTP_QUERY_DERIVED_FROM),
3853 FE(HTTP_QUERY_COST),
3854 FE(HTTP_QUERY_LINK),
3855 FE(HTTP_QUERY_PRAGMA),
3856 FE(HTTP_QUERY_VERSION),
3857 FE(HTTP_QUERY_STATUS_CODE),
3858 FE(HTTP_QUERY_STATUS_TEXT),
3859 FE(HTTP_QUERY_RAW_HEADERS),
3860 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
3861 FE(HTTP_QUERY_CONNECTION),
3862 FE(HTTP_QUERY_ACCEPT),
3863 FE(HTTP_QUERY_ACCEPT_CHARSET),
3864 FE(HTTP_QUERY_ACCEPT_ENCODING),
3865 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
3866 FE(HTTP_QUERY_AUTHORIZATION),
3867 FE(HTTP_QUERY_CONTENT_ENCODING),
3868 FE(HTTP_QUERY_FORWARDED),
3869 FE(HTTP_QUERY_FROM),
3870 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
3871 FE(HTTP_QUERY_LOCATION),
3872 FE(HTTP_QUERY_ORIG_URI),
3873 FE(HTTP_QUERY_REFERER),
3874 FE(HTTP_QUERY_RETRY_AFTER),
3875 FE(HTTP_QUERY_SERVER),
3876 FE(HTTP_QUERY_TITLE),
3877 FE(HTTP_QUERY_USER_AGENT),
3878 FE(HTTP_QUERY_WWW_AUTHENTICATE),
3879 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
3880 FE(HTTP_QUERY_ACCEPT_RANGES),
3881 FE(HTTP_QUERY_SET_COOKIE),
3882 FE(HTTP_QUERY_COOKIE),
3883 FE(HTTP_QUERY_REQUEST_METHOD),
3884 FE(HTTP_QUERY_REFRESH),
3885 FE(HTTP_QUERY_CONTENT_DISPOSITION),
3886 FE(HTTP_QUERY_AGE),
3887 FE(HTTP_QUERY_CACHE_CONTROL),
3888 FE(HTTP_QUERY_CONTENT_BASE),
3889 FE(HTTP_QUERY_CONTENT_LOCATION),
3890 FE(HTTP_QUERY_CONTENT_MD5),
3891 FE(HTTP_QUERY_CONTENT_RANGE),
3892 FE(HTTP_QUERY_ETAG),
3893 FE(HTTP_QUERY_HOST),
3894 FE(HTTP_QUERY_IF_MATCH),
3895 FE(HTTP_QUERY_IF_NONE_MATCH),
3896 FE(HTTP_QUERY_IF_RANGE),
3897 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
3898 FE(HTTP_QUERY_MAX_FORWARDS),
3899 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
3900 FE(HTTP_QUERY_RANGE),
3901 FE(HTTP_QUERY_TRANSFER_ENCODING),
3902 FE(HTTP_QUERY_UPGRADE),
3903 FE(HTTP_QUERY_VARY),
3904 FE(HTTP_QUERY_VIA),
3905 FE(HTTP_QUERY_WARNING),
3906 FE(HTTP_QUERY_CUSTOM)
3907 };
3908 static const wininet_flag_info modifier_flags[] = {
3909 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
3910 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
3911 FE(HTTP_QUERY_FLAG_NUMBER),
3912 FE(HTTP_QUERY_FLAG_COALESCE)
3913 };
3914 #undef FE
3915 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
3916 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
3917 DWORD i;
3918
3919 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, info);
3920 TRACE(" Attribute:");
3921 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
3922 if (query_flags[i].val == info) {
3923 TRACE(" %s", query_flags[i].name);
3924 break;
3925 }
3926 }
3927 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
3928 TRACE(" Unknown (%08x)", info);
3929 }
3930
3931 TRACE(" Modifier:");
3932 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
3933 if (modifier_flags[i].val & info_mod) {
3934 TRACE(" %s", modifier_flags[i].name);
3935 info_mod &= ~ modifier_flags[i].val;
3936 }
3937 }
3938
3939 if (info_mod) {
3940 TRACE(" Unknown (%08x)", info_mod);
3941 }
3942 TRACE("\n");
3943 }
3944
3945 request = (http_request_t*) get_handle_object( hHttpRequest );
3946 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
3947 {
3948 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3949 goto lend;
3950 }
3951
3952 if (lpBuffer == NULL)
3953 *lpdwBufferLength = 0;
3954 res = HTTP_HttpQueryInfoW( request, dwInfoLevel,
3955 lpBuffer, lpdwBufferLength, lpdwIndex);
3956
3957 lend:
3958 if( request )
3959 WININET_Release( &request->hdr );
3960
3961 TRACE("%u <--\n", res);
3962 if(res != ERROR_SUCCESS)
3963 SetLastError(res);
3964 return res == ERROR_SUCCESS;
3965 }
3966
3967 /***********************************************************************
3968 * HttpQueryInfoA (WININET.@)
3969 *
3970 * Queries for information about an HTTP request
3971 *
3972 * RETURNS
3973 * TRUE on success
3974 * FALSE on failure
3975 *
3976 */
3977 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
3978 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
3979 {
3980 BOOL result;
3981 DWORD len;
3982 WCHAR* bufferW;
3983
3984 TRACE("%p %x\n", hHttpRequest, dwInfoLevel);
3985
3986 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
3987 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
3988 {
3989 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
3990 lpdwBufferLength, lpdwIndex );
3991 }
3992
3993 if (lpBuffer)
3994 {
3995 DWORD alloclen;
3996 len = (*lpdwBufferLength)*sizeof(WCHAR);
3997 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
3998 {
3999 alloclen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 ) * sizeof(WCHAR);
4000 if (alloclen < len)
4001 alloclen = len;
4002 }
4003 else
4004 alloclen = len;
4005 bufferW = heap_alloc(alloclen);
4006 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
4007 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
4008 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, bufferW, alloclen / sizeof(WCHAR) );
4009 } else
4010 {
4011 bufferW = NULL;
4012 len = 0;
4013 }
4014
4015 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
4016 &len, lpdwIndex );
4017 if( result )
4018 {
4019 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
4020 lpBuffer, *lpdwBufferLength, NULL, NULL );
4021 *lpdwBufferLength = len - 1;
4022
4023 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
4024 }
4025 else
4026 /* since the strings being returned from HttpQueryInfoW should be
4027 * only ASCII characters, it is reasonable to assume that all of
4028 * the Unicode characters can be reduced to a single byte */
4029 *lpdwBufferLength = len / sizeof(WCHAR);
4030
4031 heap_free( bufferW );
4032 return result;
4033 }
4034
4035 /***********************************************************************
4036 * HTTP_GetRedirectURL (internal)
4037 */
4038 static LPWSTR HTTP_GetRedirectURL(http_request_t *request, LPCWSTR lpszUrl)
4039 {
4040 static WCHAR szHttp[] = {'h','t','t','p',0};
4041 static WCHAR szHttps[] = {'h','t','t','p','s',0};
4042 http_session_t *session = request->session;
4043 URL_COMPONENTSW urlComponents;
4044 DWORD url_length = 0;
4045 LPWSTR orig_url;
4046 LPWSTR combined_url;
4047
4048 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
4049 urlComponents.lpszScheme = (request->hdr.dwFlags & INTERNET_FLAG_SECURE) ? szHttps : szHttp;
4050 urlComponents.dwSchemeLength = 0;
4051 urlComponents.lpszHostName = request->server->name;
4052 urlComponents.dwHostNameLength = 0;
4053 urlComponents.nPort = request->server->port;
4054 urlComponents.lpszUserName = session->userName;
4055 urlComponents.dwUserNameLength = 0;
4056 urlComponents.lpszPassword = NULL;
4057 urlComponents.dwPasswordLength = 0;
4058 urlComponents.lpszUrlPath = request->path;
4059 urlComponents.dwUrlPathLength = 0;
4060 urlComponents.lpszExtraInfo = NULL;
4061 urlComponents.dwExtraInfoLength = 0;
4062
4063 if (!InternetCreateUrlW(&urlComponents, 0, NULL, &url_length) &&
4064 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
4065 return NULL;
4066
4067 orig_url = heap_alloc(url_length);
4068
4069 /* convert from bytes to characters */
4070 url_length = url_length / sizeof(WCHAR) - 1;
4071 if (!InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length))
4072 {
4073 heap_free(orig_url);
4074 return NULL;
4075 }
4076
4077 url_length = 0;
4078 if (!InternetCombineUrlW(orig_url, lpszUrl, NULL, &url_length, ICU_ENCODE_SPACES_ONLY) &&
4079 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
4080 {
4081 heap_free(orig_url);
4082 return NULL;
4083 }
4084 combined_url = heap_alloc(url_length * sizeof(WCHAR));
4085
4086 if (!InternetCombineUrlW(orig_url, lpszUrl, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY))
4087 {
4088 heap_free(orig_url);
4089 heap_free(combined_url);
4090 return NULL;
4091 }
4092 heap_free(orig_url);
4093 return combined_url;
4094 }
4095
4096
4097 /***********************************************************************
4098 * HTTP_HandleRedirect (internal)
4099 */
4100 static DWORD HTTP_HandleRedirect(http_request_t *request, LPCWSTR lpszUrl)
4101 {
4102 http_session_t *session = request->session;
4103 WCHAR path[INTERNET_MAX_PATH_LENGTH];
4104
4105 if(lpszUrl[0]=='/')
4106 {
4107 /* if it's an absolute path, keep the same session info */
4108 lstrcpynW(path, lpszUrl, INTERNET_MAX_URL_LENGTH);
4109 }
4110 else
4111 {
4112 URL_COMPONENTSW urlComponents;
4113 WCHAR protocol[INTERNET_MAX_SCHEME_LENGTH];
4114 WCHAR hostName[INTERNET_MAX_HOST_NAME_LENGTH];
4115 WCHAR userName[INTERNET_MAX_USER_NAME_LENGTH];
4116 BOOL custom_port = FALSE;
4117
4118 static const WCHAR httpW[] = {'h','t','t','p',0};
4119 static const WCHAR httpsW[] = {'h','t','t','p','s',0};
4120
4121 userName[0] = 0;
4122 hostName[0] = 0;
4123 protocol[0] = 0;
4124
4125 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
4126 urlComponents.lpszScheme = protocol;
4127 urlComponents.dwSchemeLength = INTERNET_MAX_SCHEME_LENGTH;
4128 urlComponents.lpszHostName = hostName;
4129 urlComponents.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH;
4130 urlComponents.lpszUserName = userName;
4131 urlComponents.dwUserNameLength = INTERNET_MAX_USER_NAME_LENGTH;
4132 urlComponents.lpszPassword = NULL;
4133 urlComponents.dwPasswordLength = 0;
4134 urlComponents.lpszUrlPath = path;
4135 urlComponents.dwUrlPathLength = INTERNET_MAX_PATH_LENGTH;
4136 urlComponents.lpszExtraInfo = NULL;
4137 urlComponents.dwExtraInfoLength = 0;
4138 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
4139 return INTERNET_GetLastError();
4140
4141 if(!strcmpiW(protocol, httpW)) {
4142 if(request->hdr.dwFlags & INTERNET_FLAG_SECURE) {
4143 TRACE("redirect from secure page to non-secure page\n");
4144 /* FIXME: warn about from secure redirect to non-secure page */
4145 request->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
4146 }
4147
4148 if(urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
4149 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
4150 else if(urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT)
4151 custom_port = TRUE;
4152 }else if(!strcmpiW(protocol, httpsW)) {
4153 if(!(request->hdr.dwFlags & INTERNET_FLAG_SECURE)) {
4154 TRACE("redirect from non-secure page to secure page\n");
4155 /* FIXME: notify about redirect to secure page */
4156 request->hdr.dwFlags |= INTERNET_FLAG_SECURE;
4157 }
4158
4159 if(urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
4160 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
4161 else if(urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT)
4162 custom_port = TRUE;
4163 }
4164
4165 heap_free(session->hostName);
4166
4167 session->hostName = heap_strdupW(hostName);
4168 session->hostPort = urlComponents.nPort;
4169
4170 heap_free(session->userName);
4171 session->userName = NULL;
4172 if (userName[0])
4173 session->userName = heap_strdupW(userName);
4174
4175 reset_data_stream(request);
4176
4177 if(strcmpiW(request->server->name, hostName) || request->server->port != urlComponents.nPort) {
4178 server_t *new_server;
4179
4180 new_server = get_server(hostName, urlComponents.nPort, urlComponents.nScheme == INTERNET_SCHEME_HTTPS, TRUE);
4181 server_release(request->server);
4182 request->server = new_server;
4183 }
4184
4185 if (custom_port)
4186 HTTP_ProcessHeader(request, hostW, request->server->host_port, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
4187 else
4188 HTTP_ProcessHeader(request, hostW, request->server->name, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
4189 }
4190 heap_free(request->path);
4191 request->path=NULL;
4192 if (*path)
4193 {
4194 DWORD needed = 0;
4195 HRESULT rc;
4196
4197 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
4198 if (rc != E_POINTER)
4199 needed = strlenW(path)+1;
4200 request->path = heap_alloc(needed*sizeof(WCHAR));
4201 rc = UrlEscapeW(path, request->path, &needed,
4202 URL_ESCAPE_SPACES_ONLY);
4203 if (rc != S_OK)
4204 {
4205 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
4206 strcpyW(request->path,path);
4207 }
4208 }
4209
4210 /* Remove custom content-type/length headers on redirects. */
4211 remove_header(request, szContent_Type, TRUE);
4212 remove_header(request, szContent_Length, TRUE);
4213
4214 return ERROR_SUCCESS;
4215 }
4216
4217 /***********************************************************************
4218 * HTTP_build_req (internal)
4219 *
4220 * concatenate all the strings in the request together
4221 */
4222 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
4223 {
4224 LPCWSTR *t;
4225 LPWSTR str;
4226
4227 for( t = list; *t ; t++ )
4228 len += strlenW( *t );
4229 len++;
4230
4231 str = heap_alloc(len*sizeof(WCHAR));
4232 *str = 0;
4233
4234 for( t = list; *t ; t++ )
4235 strcatW( str, *t );
4236
4237 return str;
4238 }
4239
4240 static void HTTP_InsertCookies(http_request_t *request)
4241 {
4242 WCHAR *cookies;
4243 DWORD res;
4244
4245 res = get_cookie_header(request->server->name, request->path, &cookies);
4246 if(res != ERROR_SUCCESS || !cookies)
4247 return;
4248
4249 HTTP_HttpAddRequestHeadersW(request, cookies, strlenW(cookies),
4250 HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
4251 heap_free(cookies);
4252 }
4253
4254 static WORD HTTP_ParseWkday(LPCWSTR day)
4255 {
4256 static const WCHAR days[7][4] = {{ 's','u','n',0 },
4257 { 'm','o','n',0 },
4258 { 't','u','e',0 },
4259 { 'w','e','d',0 },
4260 { 't','h','u',0 },
4261 { 'f','r','i',0 },
4262 { 's','a','t',0 }};
4263 unsigned int i;
4264 for (i = 0; i < sizeof(days)/sizeof(*days); i++)
4265 if (!strcmpiW(day, days[i]))
4266 return i;
4267
4268 /* Invalid */
4269 return 7;
4270 }
4271
4272 static WORD HTTP_ParseMonth(LPCWSTR month)
4273 {
4274 static const WCHAR jan[] = { 'j','a','n',0 };
4275 static const WCHAR feb[] = { 'f','e','b',0 };
4276 static const WCHAR mar[] = { 'm','a','r',0 };
4277 static const WCHAR apr[] = { 'a','p','r',0 };
4278 static const WCHAR may[] = { 'm','a','y',0 };
4279 static const WCHAR jun[] = { 'j','u','n',0 };
4280 static const WCHAR jul[] = { 'j','u','l',0 };
4281 static const WCHAR aug[] = { 'a','u','g',0 };
4282 static const WCHAR sep[] = { 's','e','p',0 };
4283 static const WCHAR oct[] = { 'o','c','t',0 };
4284 static const WCHAR nov[] = { 'n','o','v',0 };
4285 static const WCHAR dec[] = { 'd','e','c',0 };
4286
4287 if (!strcmpiW(month, jan)) return 1;
4288 if (!strcmpiW(month, feb)) return 2;
4289 if (!strcmpiW(month, mar)) return 3;
4290 if (!strcmpiW(month, apr)) return 4;
4291 if (!strcmpiW(month, may)) return 5;
4292 if (!strcmpiW(month, jun)) return 6;
4293 if (!strcmpiW(month, jul)) return 7;
4294 if (!strcmpiW(month, aug)) return 8;
4295 if (!strcmpiW(month, sep)) return 9;
4296 if (!strcmpiW(month, oct)) return 10;
4297 if (!strcmpiW(month, nov)) return 11;
4298 if (!strcmpiW(month, dec)) return 12;
4299 /* Invalid */
4300 return 0;
4301 }
4302
4303 /* Parses the string pointed to by *str, assumed to be a 24-hour time HH:MM:SS,
4304 * optionally preceded by whitespace.
4305 * Upon success, returns TRUE, sets the wHour, wMinute, and wSecond fields of
4306 * st, and sets *str to the first character after the time format.
4307 */
4308 static BOOL HTTP_ParseTime(SYSTEMTIME *st, LPCWSTR *str)
4309 {
4310 LPCWSTR ptr = *str;
4311 WCHAR *nextPtr;
4312 unsigned long num;
4313
4314 while (isspaceW(*ptr))
4315 ptr++;
4316
4317 num = strtoulW(ptr, &nextPtr, 10);
4318 if (!nextPtr || nextPtr <= ptr || *nextPtr != ':')
4319 {
4320 ERR("unexpected time format %s\n", debugstr_w(ptr));
4321 return FALSE;
4322 }
4323 if (num > 23)
4324 {
4325 ERR("unexpected hour in time format %s\n", debugstr_w(ptr));
4326 return FALSE;
4327 }
4328 ptr = nextPtr + 1;
4329 st->wHour = (WORD)num;
4330 num = strtoulW(ptr, &nextPtr, 10);
4331 if (!nextPtr || nextPtr <= ptr || *nextPtr != ':')
4332 {
4333 ERR("unexpected time format %s\n", debugstr_w(ptr));
4334 return FALSE;
4335 }
4336 if (num > 59)
4337 {
4338 ERR("unexpected minute in time format %s\n", debugstr_w(ptr));
4339 return FALSE;
4340 }
4341 ptr = nextPtr + 1;
4342 st->wMinute = (WORD)num;
4343 num = strtoulW(ptr, &nextPtr, 10);
4344 if (!nextPtr || nextPtr <= ptr)
4345 {
4346 ERR("unexpected time format %s\n", debugstr_w(ptr));
4347 return FALSE;
4348 }
4349 if (num > 59)
4350 {
4351 ERR("unexpected second in time format %s\n", debugstr_w(ptr));
4352 return FALSE;
4353 }
4354 ptr = nextPtr + 1;
4355 *str = ptr;
4356 st->wSecond = (WORD)num;
4357 return TRUE;
4358 }
4359
4360 static BOOL HTTP_ParseDateAsAsctime(LPCWSTR value, FILETIME *ft)
4361 {
4362 static const WCHAR gmt[]= { 'G','M','T',0 };
4363 WCHAR day[4], *dayPtr, month[4], *monthPtr, *nextPtr;
4364 LPCWSTR ptr;
4365 SYSTEMTIME st = { 0 };
4366 unsigned long num;
4367
4368 for (ptr = value, dayPtr = day; *ptr && !isspaceW(*ptr) &&
4369 dayPtr - day < sizeof(day) / sizeof(day[0]) - 1; ptr++, dayPtr++)
4370 *dayPtr = *ptr;
4371 *dayPtr = 0;
4372 st.wDayOfWeek = HTTP_ParseWkday(day);
4373 if (st.wDayOfWeek >= 7)
4374 {
4375 ERR("unexpected weekday %s\n", debugstr_w(day));
4376 return FALSE;
4377 }
4378
4379 while (isspaceW(*ptr))
4380 ptr++;
4381
4382 for (monthPtr = month; !isspace(*ptr) &&
4383 monthPtr - month < sizeof(month) / sizeof(month[0]) - 1;
4384 monthPtr++, ptr++)
4385 *monthPtr = *ptr;
4386 *monthPtr = 0;
4387 st.wMonth = HTTP_ParseMonth(month);
4388 if (!st.wMonth || st.wMonth > 12)
4389 {
4390 ERR("unexpected month %s\n", debugstr_w(month));
4391 return FALSE;
4392 }
4393
4394 while (isspaceW(*ptr))
4395 ptr++;
4396
4397 num = strtoulW(ptr, &nextPtr, 10);
4398 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4399 {
4400 ERR("unexpected day %s\n", debugstr_w(ptr));
4401 return FALSE;
4402 }
4403 ptr = nextPtr;
4404 st.wDay = (WORD)num;
4405
4406 while (isspaceW(*ptr))
4407 ptr++;
4408
4409 if (!HTTP_ParseTime(&st, &ptr))
4410 return FALSE;
4411
4412 while (isspaceW(*ptr))
4413 ptr++;
4414
4415 num = strtoulW(ptr, &nextPtr, 10);
4416 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4417 {
4418 ERR("unexpected year %s\n", debugstr_w(ptr));
4419 return FALSE;
4420 }
4421 ptr = nextPtr;
4422 st.wYear = (WORD)num;
4423
4424 while (isspaceW(*ptr))
4425 ptr++;
4426
4427 /* asctime() doesn't report a timezone, but some web servers do, so accept
4428 * with or without GMT.
4429 */
4430 if (*ptr && strcmpW(ptr, gmt))
4431 {
4432 ERR("unexpected timezone %s\n", debugstr_w(ptr));
4433 return FALSE;
4434 }
4435 return SystemTimeToFileTime(&st, ft);
4436 }
4437
4438 static BOOL HTTP_ParseRfc1123Date(LPCWSTR value, FILETIME *ft)
4439 {
4440 static const WCHAR gmt[]= { 'G','M','T',0 };
4441 WCHAR *nextPtr, day[4], month[4], *monthPtr;
4442 LPCWSTR ptr;
4443 unsigned long num;
4444 SYSTEMTIME st = { 0 };
4445
4446 ptr = strchrW(value, ',');
4447 if (!ptr)
4448 return FALSE;
4449 if (ptr - value != 3)
4450 {
4451 WARN("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4452 return FALSE;
4453 }
4454 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4455 day[3] = 0;
4456 st.wDayOfWeek = HTTP_ParseWkday(day);
4457 if (st.wDayOfWeek > 6)
4458 {
4459 WARN("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4460 return FALSE;
4461 }
4462 ptr++;
4463
4464 while (isspaceW(*ptr))
4465 ptr++;
4466
4467 num = strtoulW(ptr, &nextPtr, 10);
4468 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4469 {
4470 WARN("unexpected day %s\n", debugstr_w(value));
4471 return FALSE;
4472 }
4473 ptr = nextPtr;
4474 st.wDay = (WORD)num;
4475
4476 while (isspaceW(*ptr))
4477 ptr++;
4478
4479 for (monthPtr = month; !isspace(*ptr) &&
4480 monthPtr - month < sizeof(month) / sizeof(month[0]) - 1;
4481 monthPtr++, ptr++)
4482 *monthPtr = *ptr;
4483 *monthPtr = 0;
4484 st.wMonth = HTTP_ParseMonth(month);
4485 if (!st.wMonth || st.wMonth > 12)
4486 {
4487 WARN("unexpected month %s\n", debugstr_w(month));
4488 return FALSE;
4489 }
4490
4491 while (isspaceW(*ptr))
4492 ptr++;
4493
4494 num = strtoulW(ptr, &nextPtr, 10);
4495 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4496 {
4497 ERR("unexpected year %s\n", debugstr_w(value));
4498 return FALSE;
4499 }
4500 ptr = nextPtr;
4501 st.wYear = (WORD)num;
4502
4503 if (!HTTP_ParseTime(&st, &ptr))
4504 return FALSE;
4505
4506 while (isspaceW(*ptr))
4507 ptr++;
4508
4509 if (strcmpW(ptr, gmt))
4510 {
4511 ERR("unexpected time zone %s\n", debugstr_w(ptr));
4512 return FALSE;
4513 }
4514 return SystemTimeToFileTime(&st, ft);
4515 }
4516
4517 static WORD HTTP_ParseWeekday(LPCWSTR day)
4518 {
4519 static const WCHAR days[7][10] = {{ 's','u','n','d','a','y',0 },
4520 { 'm','o','n','d','a','y',0 },
4521 { 't','u','e','s','d','a','y',0 },
4522 { 'w','e','d','n','e','s','d','a','y',0 },
4523 { 't','h','u','r','s','d','a','y',0 },
4524 { 'f','r','i','d','a','y',0 },
4525 { 's','a','t','u','r','d','a','y',0 }};
4526 unsigned int i;
4527 for (i = 0; i < sizeof(days)/sizeof(*days); i++)
4528 if (!strcmpiW(day, days[i]))
4529 return i;
4530
4531 /* Invalid */
4532 return 7;
4533 }
4534
4535 static BOOL HTTP_ParseRfc850Date(LPCWSTR value, FILETIME *ft)
4536 {
4537 static const WCHAR gmt[]= { 'G','M','T',0 };
4538 WCHAR *nextPtr, day[10], month[4], *monthPtr;
4539 LPCWSTR ptr;
4540 unsigned long num;
4541 SYSTEMTIME st = { 0 };
4542
4543 ptr = strchrW(value, ',');
4544 if (!ptr)
4545 return FALSE;
4546 if (ptr - value == 3)
4547 {
4548 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4549 day[3] = 0;
4550 st.wDayOfWeek = HTTP_ParseWkday(day);
4551 if (st.wDayOfWeek > 6)
4552 {
4553 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4554 return FALSE;
4555 }
4556 }
4557 else if (ptr - value < sizeof(day) / sizeof(day[0]))
4558 {
4559 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4560 day[ptr - value + 1] = 0;
4561 st.wDayOfWeek = HTTP_ParseWeekday(day);
4562 if (st.wDayOfWeek > 6)
4563 {
4564 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4565 return FALSE;
4566 }
4567 }
4568 else
4569 {
4570 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4571 return FALSE;
4572 }
4573 ptr++;
4574
4575 while (isspaceW(*ptr))
4576 ptr++;
4577
4578 num = strtoulW(ptr, &nextPtr, 10);
4579 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4580 {
4581 ERR("unexpected day %s\n", debugstr_w(value));
4582 return FALSE;
4583 }
4584 ptr = nextPtr;
4585 st.wDay = (WORD)num;
4586
4587 if (*ptr != '-')
4588 {
4589 ERR("unexpected month format %s\n", debugstr_w(ptr));
4590 return FALSE;
4591 }
4592 ptr++;
4593
4594 for (monthPtr = month; *ptr != '-' &&
4595 monthPtr - month < sizeof(month) / sizeof(month[0]) - 1;
4596 monthPtr++, ptr++)
4597 *monthPtr = *ptr;
4598 *monthPtr = 0;
4599 st.wMonth = HTTP_ParseMonth(month);
4600 if (!st.wMonth || st.wMonth > 12)
4601 {
4602 ERR("unexpected month %s\n", debugstr_w(month));
4603 return FALSE;
4604 }
4605
4606 if (*ptr != '-')
4607 {
4608 ERR("unexpected year format %s\n", debugstr_w(ptr));
4609 return FALSE;
4610 }
4611 ptr++;
4612
4613 num = strtoulW(ptr, &nextPtr, 10);
4614 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4615 {
4616 ERR("unexpected year %s\n", debugstr_w(value));
4617 return FALSE;
4618 }
4619 ptr = nextPtr;
4620 st.wYear = (WORD)num;
4621
4622 if (!HTTP_ParseTime(&st, &ptr))
4623 return FALSE;
4624
4625 while (isspaceW(*ptr))
4626 ptr++;
4627
4628 if (strcmpW(ptr, gmt))
4629 {
4630 ERR("unexpected time zone %s\n", debugstr_w(ptr));
4631 return FALSE;
4632 }
4633 return SystemTimeToFileTime(&st, ft);
4634 }
4635
4636 static BOOL HTTP_ParseDate(LPCWSTR value, FILETIME *ft)
4637 {
4638 static const WCHAR zero[] = { '0',0 };
4639 BOOL ret;
4640
4641 if (!strcmpW(value, zero))
4642 {
4643 ft->dwLowDateTime = ft->dwHighDateTime = 0;
4644 ret = TRUE;
4645 }
4646 else if (strchrW(value, ','))
4647 {
4648 ret = HTTP_ParseRfc1123Date(value, ft);
4649 if (!ret)
4650 {
4651 ret = HTTP_ParseRfc850Date(value, ft);
4652 if (!ret)
4653 ERR("unexpected date format %s\n", debugstr_w(value));
4654 }
4655 }
4656 else
4657 {
4658 ret = HTTP_ParseDateAsAsctime(value, ft);
4659 if (!ret)
4660 ERR("unexpected date format %s\n", debugstr_w(value));
4661 }
4662 return ret;
4663 }
4664
4665 static void HTTP_ProcessExpires(http_request_t *request)
4666 {
4667 BOOL expirationFound = FALSE;
4668 int headerIndex;
4669
4670 EnterCriticalSection( &request->headers_section );
4671
4672 /* Look for a Cache-Control header with a max-age directive, as it takes
4673 * precedence over the Expires header.
4674 */
4675 headerIndex = HTTP_GetCustomHeaderIndex(request, szCache_Control, 0, FALSE);
4676 if (headerIndex != -1)
4677 {
4678 LPHTTPHEADERW ccHeader = &request->custHeaders[headerIndex];
4679 LPWSTR ptr;
4680
4681 for (ptr = ccHeader->lpszValue; ptr && *ptr; )
4682 {
4683 LPWSTR comma = strchrW(ptr, ','), end, equal;
4684
4685 if (comma)
4686 end = comma;
4687 else
4688 end = ptr + strlenW(ptr);
4689 for (equal = end - 1; equal > ptr && *equal != '='; equal--)
4690 ;
4691 if (*equal == '=')
4692 {
4693 static const WCHAR max_age[] = {
4694 'm','a','x','-','a','g','e',0 };
4695
4696 if (!strncmpiW(ptr, max_age, equal - ptr - 1))
4697 {
4698 LPWSTR nextPtr;
4699 unsigned long age;
4700
4701 age = strtoulW(equal + 1, &nextPtr, 10);
4702 if (nextPtr > equal + 1)
4703 {
4704 LARGE_INTEGER ft;
4705
4706 NtQuerySystemTime( &ft );
4707 /* Age is in seconds, FILETIME resolution is in
4708 * 100 nanosecond intervals.
4709 */
4710 ft.QuadPart += age * (ULONGLONG)1000000;
4711 request->expires.dwLowDateTime = ft.u.LowPart;
4712 request->expires.dwHighDateTime = ft.u.HighPart;
4713 expirationFound = TRUE;
4714 }
4715 }
4716 }
4717 if (comma)
4718 {
4719 ptr = comma + 1;
4720 while (isspaceW(*ptr))
4721 ptr++;
4722 }
4723 else
4724 ptr = NULL;
4725 }
4726 }
4727 if (!expirationFound)
4728 {
4729 headerIndex = HTTP_GetCustomHeaderIndex(request, szExpires, 0, FALSE);
4730 if (headerIndex != -1)
4731 {
4732 LPHTTPHEADERW expiresHeader = &request->custHeaders[headerIndex];
4733 FILETIME ft;
4734
4735 if (HTTP_ParseDate(expiresHeader->lpszValue, &ft))
4736 {
4737 expirationFound = TRUE;
4738 request->expires = ft;
4739 }
4740 }
4741 }
4742 if (!expirationFound)
4743 {
4744 LARGE_INTEGER t;
4745
4746 /* With no known age, default to 10 minutes until expiration. */
4747 NtQuerySystemTime( &t );
4748 t.QuadPart += 10 * 60 * (ULONGLONG)10000000;
4749 request->expires.dwLowDateTime = t.u.LowPart;
4750 request->expires.dwHighDateTime = t.u.HighPart;
4751 }
4752
4753 LeaveCriticalSection( &request->headers_section );
4754 }
4755
4756 static void HTTP_ProcessLastModified(http_request_t *request)
4757 {
4758 int headerIndex;
4759
4760 EnterCriticalSection( &request->headers_section );
4761
4762 headerIndex = HTTP_GetCustomHeaderIndex(request, szLast_Modified, 0, FALSE);
4763 if (headerIndex != -1)
4764 {
4765 LPHTTPHEADERW expiresHeader = &request->custHeaders[headerIndex];
4766 FILETIME ft;
4767
4768 if (HTTP_ParseDate(expiresHeader->lpszValue, &ft))
4769 request->last_modified = ft;
4770 }
4771
4772 LeaveCriticalSection( &request->headers_section );
4773 }
4774
4775 static void http_process_keep_alive(http_request_t *req)
4776 {
4777 int index;
4778
4779 EnterCriticalSection( &req->headers_section );
4780
4781 if ((index = HTTP_GetCustomHeaderIndex(req, szConnection, 0, FALSE)) != -1)
4782 req->netconn->keep_alive = !strcmpiW(req->custHeaders[index].lpszValue, szKeepAlive);
4783 else if ((index = HTTP_GetCustomHeaderIndex(req, szProxy_Connection, 0, FALSE)) != -1)
4784 req->netconn->keep_alive = !strcmpiW(req->custHeaders[index].lpszValue, szKeepAlive);
4785 else
4786 req->netconn->keep_alive = !strcmpiW(req->version, g_szHttp1_1);
4787
4788 LeaveCriticalSection( &req->headers_section );
4789 }
4790
4791 static DWORD open_http_connection(http_request_t *request, BOOL *reusing)
4792 {
4793 const BOOL is_https = (request->hdr.dwFlags & INTERNET_FLAG_SECURE) != 0;
4794 netconn_t *netconn = NULL;
4795 DWORD res;
4796
4797 reset_data_stream(request);
4798
4799 if (request->netconn)
4800 {
4801 if (is_valid_netconn(request->netconn) && NETCON_is_alive(request->netconn))
4802 {
4803 *reusing = TRUE;
4804 return ERROR_SUCCESS;
4805 }
4806 else
4807 {
4808 free_netconn(request->netconn);
4809 request->netconn = NULL;
4810 }
4811 }
4812
4813 res = HTTP_ResolveName(request);
4814 if(res != ERROR_SUCCESS)
4815 return res;
4816
4817 EnterCriticalSection(&connection_pool_cs);
4818
4819 while(!list_empty(&request->server->conn_pool)) {
4820 netconn = LIST_ENTRY(list_head(&request->server->conn_pool), netconn_t, pool_entry);
4821 list_remove(&netconn->pool_entry);
4822
4823 if(is_valid_netconn(netconn) && NETCON_is_alive(netconn))
4824 break;
4825
4826 TRACE("connection %p closed during idle\n", netconn);
4827 free_netconn(netconn);
4828 netconn = NULL;
4829 }
4830
4831 LeaveCriticalSection(&connection_pool_cs);
4832
4833 if(netconn) {
4834 TRACE("<-- reusing %p netconn\n", netconn);
4835 request->netconn = netconn;
4836 *reusing = TRUE;
4837 return ERROR_SUCCESS;
4838 }
4839
4840 TRACE("connecting to %s, proxy %s\n", debugstr_w(request->server->name),
4841 request->proxy ? debugstr_w(request->proxy->name) : "(null)");
4842
4843 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
4844 INTERNET_STATUS_CONNECTING_TO_SERVER,
4845 request->server->addr_str,
4846 strlen(request->server->addr_str)+1);
4847
4848 res = create_netconn(is_https, request->proxy ? request->proxy : request->server, request->security_flags,
4849 (request->hdr.ErrorMask & INTERNET_ERROR_MASK_COMBINED_SEC_CERT) != 0,
4850 request->connect_timeout, &netconn);
4851 if(res != ERROR_SUCCESS) {
4852 ERR("create_netconn failed: %u\n", res);
4853 return res;
4854 }
4855
4856 request->netconn = netconn;
4857
4858 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
4859 INTERNET_STATUS_CONNECTED_TO_SERVER,
4860 request->server->addr_str, strlen(request->server->addr_str)+1);
4861
4862 *reusing = FALSE;
4863 TRACE("Created connection to %s: %p\n", debugstr_w(request->server->name), netconn);
4864 return ERROR_SUCCESS;
4865 }
4866
4867 static char *build_ascii_request( const WCHAR *str, void *data, DWORD data_len, DWORD *out_len )
4868 {
4869 int len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
4870 char *ret;
4871
4872 if (!(ret = heap_alloc( len + data_len ))) return NULL;
4873 WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
4874 if (data_len) memcpy( ret + len - 1, data, data_len );
4875 *out_len = len + data_len - 1;
4876 ret[*out_len] = 0;
4877 return ret;
4878 }
4879
4880 /***********************************************************************
4881 * HTTP_HttpSendRequestW (internal)
4882 *
4883 * Sends the specified request to the HTTP server
4884 *
4885 * RETURNS
4886 * ERROR_SUCCESS on success
4887 * win32 error code on failure
4888 *
4889 */
4890 static DWORD HTTP_HttpSendRequestW(http_request_t *request, LPCWSTR lpszHeaders,
4891 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
4892 DWORD dwContentLength, BOOL bEndRequest)
4893 {
4894 static const WCHAR szContentLength[] =
4895 { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0 };
4896 BOOL redirected = FALSE, secure_proxy_connect = FALSE, loop_next;
4897 LPWSTR requestString = NULL;
4898 INT responseLen, cnt;
4899 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \r\n */ + 20 /* int */ ];
4900 DWORD res;
4901
4902 TRACE("--> %p\n", request);
4903
4904 assert(request->hdr.htype == WH_HHTTPREQ);
4905
4906 /* if the verb is NULL default to GET */
4907 if (!request->verb)
4908 request->verb = heap_strdupW(szGET);
4909
4910 HTTP_ProcessHeader(request, hostW, request->server->canon_host_port,
4911 HTTP_ADDREQ_FLAG_ADD_IF_NEW | HTTP_ADDHDR_FLAG_REQ);
4912
4913 if (dwContentLength || strcmpW(request->verb, szGET))
4914 {
4915 sprintfW(contentLengthStr, szContentLength, dwContentLength);
4916 HTTP_HttpAddRequestHeadersW(request, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4917 request->bytesToWrite = dwContentLength;
4918 }
4919 if (request->session->appInfo->agent)
4920 {
4921 WCHAR *agent_header;
4922 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0};
4923 int len;
4924
4925 len = strlenW(request->session->appInfo->agent) + strlenW(user_agent);
4926 agent_header = heap_alloc(len * sizeof(WCHAR));
4927 sprintfW(agent_header, user_agent, request->session->appInfo->agent);
4928
4929 HTTP_HttpAddRequestHeadersW(request, agent_header, strlenW(agent_header), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4930 heap_free(agent_header);
4931 }
4932 if (request->hdr.dwFlags & INTERNET_FLAG_PRAGMA_NOCACHE)
4933 {
4934 static const WCHAR pragma_nocache[] = {'P','r','a','g','m','a',':',' ','n','o','-','c','a','c','h','e','\r','\n',0};
4935 HTTP_HttpAddRequestHeadersW(request, pragma_nocache, strlenW(pragma_nocache), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4936 }
4937 if ((request->hdr.dwFlags & INTERNET_FLAG_NO_CACHE_WRITE) && strcmpW(request->verb, szGET))
4938 {
4939 static const WCHAR cache_control[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l',':',
4940 ' ','n','o','-','c','a','c','h','e','\r','\n',0};
4941 HTTP_HttpAddRequestHeadersW(request, cache_control, strlenW(cache_control), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4942 }
4943
4944 /* add the headers the caller supplied */
4945 if( lpszHeaders && dwHeaderLength )
4946 HTTP_HttpAddRequestHeadersW(request, lpszHeaders, dwHeaderLength, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
4947
4948 do
4949 {
4950 DWORD len, data_len = dwOptionalLength;
4951 BOOL reusing_connection;
4952 char *ascii_req;
4953
4954 loop_next = FALSE;
4955
4956 if(redirected) {
4957 request->contentLength = ~0u;
4958 request->bytesToWrite = 0;
4959 }
4960
4961 if (TRACE_ON(wininet))
4962 {
4963 HTTPHEADERW *host;
4964
4965 EnterCriticalSection( &request->headers_section );
4966 host = HTTP_GetHeader( request, hostW );
4967 TRACE("Going to url %s %s\n", debugstr_w(host->lpszValue), debugstr_w(request->path));
4968 LeaveCriticalSection( &request->headers_section );
4969 }
4970
4971 HTTP_FixURL(request);
4972 if (request->hdr.dwFlags & INTERNET_FLAG_KEEP_CONNECTION)
4973 {
4974 HTTP_ProcessHeader(request, szConnection, szKeepAlive,
4975 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_ADD);
4976 }
4977 HTTP_InsertAuthorization(request, request->authInfo, szAuthorization);
4978 HTTP_InsertAuthorization(request, request->proxyAuthInfo, szProxy_Authorization);
4979
4980 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES))
4981 HTTP_InsertCookies(request);
4982
4983 res = open_http_connection(request, &reusing_connection);
4984 if (res != ERROR_SUCCESS)
4985 break;
4986
4987 if (!reusing_connection && (request->hdr.dwFlags & INTERNET_FLAG_SECURE))
4988 {
4989 if (request->proxy) secure_proxy_connect = TRUE;
4990 else
4991 {
4992 res = NETCON_secure_connect(request->netconn, request->server);
4993 if (res != ERROR_SUCCESS)
4994 {
4995 WARN("failed to upgrade to secure connection\n");
4996 http_release_netconn(request, FALSE);
4997 break;
4998 }
4999 }
5000 }
5001 if (secure_proxy_connect)
5002 {
5003 static const WCHAR connectW[] = {'C','O','N','N','E','C','T',0};
5004 const WCHAR *target = request->server->host_port;
5005 requestString = build_request_header(request, connectW, target, g_szHttp1_1, TRUE);
5006 }
5007 else if (request->proxy && !(request->hdr.dwFlags & INTERNET_FLAG_SECURE))
5008 {
5009 WCHAR *url = build_proxy_path_url(request);
5010 requestString = build_request_header(request, request->verb, url, request->version, TRUE);
5011 heap_free(url);
5012 }
5013 else
5014 requestString = build_request_header(request, request->verb, request->path, request->version, TRUE);
5015
5016 TRACE("Request header -> %s\n", debugstr_w(requestString) );
5017
5018 /* send the request as ASCII, tack on the optional data */
5019 if (!lpOptional || redirected || secure_proxy_connect)
5020 data_len = 0;
5021
5022 ascii_req = build_ascii_request( requestString, lpOptional, data_len, &len );
5023 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
5024
5025 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5026 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
5027
5028 NETCON_set_timeout( request->netconn, TRUE, request->send_timeout );
5029 res = NETCON_send(request->netconn, ascii_req, len, 0, &cnt);
5030 heap_free( ascii_req );
5031 if(res != ERROR_SUCCESS) {
5032 TRACE("send failed: %u\n", res);
5033 if(!reusing_connection)
5034 break;
5035 http_release_netconn(request, FALSE);
5036 loop_next = TRUE;
5037 continue;
5038 }
5039
5040 request->bytesWritten = data_len;
5041
5042 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5043 INTERNET_STATUS_REQUEST_SENT,
5044 &len, sizeof(DWORD));
5045
5046 if (bEndRequest)
5047 {
5048 DWORD dwBufferSize;
5049
5050 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5051 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
5052
5053 if (HTTP_GetResponseHeaders(request, &responseLen))
5054 {
5055 http_release_netconn(request, FALSE);
5056 res = ERROR_INTERNET_CONNECTION_ABORTED;
5057 goto lend;
5058 }
5059 /* FIXME: We should know that connection is closed before sending
5060 * headers. Otherwise wrong callbacks are executed */
5061 if(!responseLen && reusing_connection) {
5062 TRACE("Connection closed by server, reconnecting\n");
5063 http_release_netconn(request, FALSE);
5064 loop_next = TRUE;
5065 continue;
5066 }
5067
5068 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5069 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
5070 sizeof(DWORD));
5071
5072 http_process_keep_alive(request);
5073 HTTP_ProcessCookies(request);
5074 HTTP_ProcessExpires(request);
5075 HTTP_ProcessLastModified(request);
5076
5077 res = set_content_length(request);
5078 if(res != ERROR_SUCCESS)
5079 goto lend;
5080 if(!request->contentLength)
5081 http_release_netconn(request, TRUE);
5082
5083 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && responseLen)
5084 {
5085 WCHAR *new_url, szNewLocation[INTERNET_MAX_URL_LENGTH];
5086 dwBufferSize=sizeof(szNewLocation);
5087 switch(request->status_code) {
5088 case HTTP_STATUS_REDIRECT:
5089 case HTTP_STATUS_MOVED:
5090 case HTTP_STATUS_REDIRECT_KEEP_VERB:
5091 case HTTP_STATUS_REDIRECT_METHOD:
5092 if(HTTP_HttpQueryInfoW(request,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL) != ERROR_SUCCESS)
5093 break;
5094
5095 if (strcmpW(request->verb, szGET) && strcmpW(request->verb, szHEAD) &&
5096 request->status_code != HTTP_STATUS_REDIRECT_KEEP_VERB)
5097 {
5098 heap_free(request->verb);
5099 request->verb = heap_strdupW(szGET);
5100 }
5101 http_release_netconn(request, drain_content(request, FALSE));
5102 if ((new_url = HTTP_GetRedirectURL( request, szNewLocation )))
5103 {
5104 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REDIRECT,
5105 new_url, (strlenW(new_url) + 1) * sizeof(WCHAR));
5106 res = HTTP_HandleRedirect(request, new_url);
5107 if (res == ERROR_SUCCESS)
5108 {
5109 heap_free(requestString);
5110 loop_next = TRUE;
5111 }
5112 heap_free( new_url );
5113 }
5114 redirected = TRUE;
5115 }
5116 }
5117 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTH) && res == ERROR_SUCCESS)
5118 {
5119 WCHAR szAuthValue[2048];
5120 dwBufferSize=2048;
5121 if (request->status_code == HTTP_STATUS_DENIED)
5122 {
5123 WCHAR *host = heap_strdupW( request->server->canon_host_port );
5124 DWORD dwIndex = 0;
5125 while (HTTP_HttpQueryInfoW(request,HTTP_QUERY_WWW_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex) == ERROR_SUCCESS)
5126 {
5127 if (HTTP_DoAuthorization(request, szAuthValue,
5128 &request->authInfo,
5129 request->session->userName,
5130 request->session->password, host))
5131 {
5132 heap_free(requestString);
5133 if(!drain_content(request, TRUE)) {
5134 FIXME("Could not drain content\n");
5135 http_release_netconn(request, FALSE);
5136 }
5137 loop_next = TRUE;
5138 break;
5139 }
5140 }
5141 heap_free( host );
5142
5143 if(!loop_next) {
5144 TRACE("Cleaning wrong authorization data\n");
5145 destroy_authinfo(request->authInfo);
5146 request->authInfo = NULL;
5147 }
5148 }
5149 if (request->status_code == HTTP_STATUS_PROXY_AUTH_REQ)
5150 {
5151 DWORD dwIndex = 0;
5152 while (HTTP_HttpQueryInfoW(request,HTTP_QUERY_PROXY_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex) == ERROR_SUCCESS)
5153 {
5154 if (HTTP_DoAuthorization(request, szAuthValue,
5155 &request->proxyAuthInfo,
5156 request->session->appInfo->proxyUsername,
5157 request->session->appInfo->proxyPassword,
5158 NULL))
5159 {
5160 heap_free(requestString);
5161 if(!drain_content(request, TRUE)) {
5162 FIXME("Could not drain content\n");
5163 http_release_netconn(request, FALSE);
5164 }
5165 loop_next = TRUE;
5166 break;
5167 }
5168 }
5169
5170 if(!loop_next) {
5171 TRACE("Cleaning wrong proxy authorization data\n");
5172 destroy_authinfo(request->proxyAuthInfo);
5173 request->proxyAuthInfo = NULL;
5174 }
5175 }
5176 }
5177 if (secure_proxy_connect && request->status_code == HTTP_STATUS_OK)
5178 {
5179 res = NETCON_secure_connect(request->netconn, request->server);
5180 if (res != ERROR_SUCCESS)
5181 {
5182 WARN("failed to upgrade to secure proxy connection\n");
5183 http_release_netconn( request, FALSE );
5184 break;
5185 }
5186 remove_header(request, szProxy_Authorization, TRUE);
5187 destroy_authinfo(request->proxyAuthInfo);
5188 request->proxyAuthInfo = NULL;
5189
5190 secure_proxy_connect = FALSE;
5191 loop_next = TRUE;
5192 }
5193 }
5194 else
5195 res = ERROR_SUCCESS;
5196 }
5197 while (loop_next);
5198
5199 lend:
5200 heap_free(requestString);
5201
5202 /* TODO: send notification for P3P header */
5203
5204 if(res == ERROR_SUCCESS)
5205 create_cache_entry(request);
5206
5207 if (request->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5208 {
5209 if (res == ERROR_SUCCESS) {
5210 if(bEndRequest && request->contentLength && request->bytesWritten == request->bytesToWrite)
5211 HTTP_ReceiveRequestData(request, TRUE, NULL);
5212 else
5213 send_request_complete(request,
5214 request->session->hdr.dwInternalFlags & INET_OPENURL ? (DWORD_PTR)request->hdr.hInternet : 1, 0);
5215 }else {
5216 send_request_complete(request, 0, res);
5217 }
5218 }
5219
5220 TRACE("<--\n");
5221 return res;
5222 }
5223
5224 typedef struct {
5225 task_header_t hdr;
5226 WCHAR *headers;
5227 DWORD headers_len;
5228 void *optional;
5229 DWORD optional_len;
5230 DWORD content_len;
5231 BOOL end_request;
5232 } send_request_task_t;
5233
5234 /***********************************************************************
5235 *
5236 * Helper functions for the HttpSendRequest(Ex) functions
5237 *
5238 */
5239 static void AsyncHttpSendRequestProc(task_header_t *hdr)
5240 {
5241 send_request_task_t *task = (send_request_task_t*)hdr;
5242 http_request_t *request = (http_request_t*)task->hdr.hdr;
5243
5244 TRACE("%p\n", request);
5245
5246 HTTP_HttpSendRequestW(request, task->headers, task->headers_len, task->optional,
5247 task->optional_len, task->content_len, task->end_request);
5248
5249 heap_free(task->headers);
5250 }
5251
5252
5253 static DWORD HTTP_HttpEndRequestW(http_request_t *request, DWORD dwFlags, DWORD_PTR dwContext)
5254 {
5255 DWORD dwBufferSize;
5256 INT responseLen;
5257 DWORD res = ERROR_SUCCESS;
5258
5259 if(!is_valid_netconn(request->netconn)) {
5260 WARN("Not connected\n");
5261 send_request_complete(request, 0, ERROR_INTERNET_OPERATION_CANCELLED);
5262 return ERROR_INTERNET_OPERATION_CANCELLED;
5263 }
5264
5265 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5266 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
5267
5268 if (HTTP_GetResponseHeaders(request, &responseLen) || !responseLen)
5269 res = ERROR_HTTP_HEADER_NOT_FOUND;
5270
5271 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5272 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
5273
5274 /* process cookies here. Is this right? */
5275 http_process_keep_alive(request);
5276 HTTP_ProcessCookies(request);
5277 HTTP_ProcessExpires(request);
5278 HTTP_ProcessLastModified(request);
5279
5280 if ((res = set_content_length(request)) == ERROR_SUCCESS) {
5281 if(!request->contentLength)
5282 http_release_netconn(request, TRUE);
5283 }
5284
5285 if (res == ERROR_SUCCESS && !(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
5286 {
5287 switch(request->status_code) {
5288 case HTTP_STATUS_REDIRECT:
5289 case HTTP_STATUS_MOVED:
5290 case HTTP_STATUS_REDIRECT_METHOD:
5291 case HTTP_STATUS_REDIRECT_KEEP_VERB: {
5292 WCHAR *new_url, szNewLocation[INTERNET_MAX_URL_LENGTH];
5293 dwBufferSize=sizeof(szNewLocation);
5294 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_LOCATION, szNewLocation, &dwBufferSize, NULL) != ERROR_SUCCESS)
5295 break;
5296
5297 if (strcmpW(request->verb, szGET) && strcmpW(request->verb, szHEAD) &&
5298 request->status_code != HTTP_STATUS_REDIRECT_KEEP_VERB)
5299 {
5300 heap_free(request->verb);
5301 request->verb = heap_strdupW(szGET);
5302 }
5303 http_release_netconn(request, drain_content(request, FALSE));
5304 if ((new_url = HTTP_GetRedirectURL( request, szNewLocation )))
5305 {
5306 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REDIRECT,
5307 new_url, (strlenW(new_url) + 1) * sizeof(WCHAR));
5308 res = HTTP_HandleRedirect(request, new_url);
5309 if (res == ERROR_SUCCESS)
5310 res = HTTP_HttpSendRequestW(request, NULL, 0, NULL, 0, 0, TRUE);
5311 heap_free( new_url );
5312 }
5313 }
5314 }
5315 }
5316
5317 if(res == ERROR_SUCCESS)
5318 create_cache_entry(request);
5319
5320 if (res == ERROR_SUCCESS && request->contentLength)
5321 HTTP_ReceiveRequestData(request, TRUE, NULL);
5322 else
5323 send_request_complete(request, res == ERROR_SUCCESS, res);
5324
5325 return res;
5326 }
5327
5328 /***********************************************************************
5329 * HttpEndRequestA (WININET.@)
5330 *
5331 * Ends an HTTP request that was started by HttpSendRequestEx
5332 *
5333 * RETURNS
5334 * TRUE if successful
5335 * FALSE on failure
5336 *
5337 */
5338 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
5339 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
5340 {
5341 TRACE("(%p, %p, %08x, %08lx)\n", hRequest, lpBuffersOut, dwFlags, dwContext);
5342
5343 if (lpBuffersOut)
5344 {
5345 SetLastError(ERROR_INVALID_PARAMETER);
5346 return FALSE;
5347 }
5348
5349 return HttpEndRequestW(hRequest, NULL, dwFlags, dwContext);
5350 }
5351
5352 typedef struct {
5353 task_header_t hdr;
5354 DWORD flags;
5355 DWORD context;
5356 } end_request_task_t;
5357
5358 static void AsyncHttpEndRequestProc(task_header_t *hdr)
5359 {
5360 end_request_task_t *task = (end_request_task_t*)hdr;
5361 http_request_t *req = (http_request_t*)task->hdr.hdr;
5362
5363 TRACE("%p\n", req);
5364
5365 HTTP_HttpEndRequestW(req, task->flags, task->context);
5366 }
5367
5368 /***********************************************************************
5369 * HttpEndRequestW (WININET.@)
5370 *
5371 * Ends an HTTP request that was started by HttpSendRequestEx
5372 *
5373 * RETURNS
5374 * TRUE if successful
5375 * FALSE on failure
5376 *
5377 */
5378 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
5379 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
5380 {
5381 http_request_t *request;
5382 DWORD res;
5383
5384 TRACE("%p %p %x %lx -->\n", hRequest, lpBuffersOut, dwFlags, dwContext);
5385
5386 if (lpBuffersOut)
5387 {
5388 SetLastError(ERROR_INVALID_PARAMETER);
5389 return FALSE;
5390 }
5391
5392 request = (http_request_t*) get_handle_object( hRequest );
5393
5394 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5395 {
5396 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
5397 if (request)
5398 WININET_Release( &request->hdr );
5399 return FALSE;
5400 }
5401 request->hdr.dwFlags |= dwFlags;
5402
5403 if (request->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5404 {
5405 end_request_task_t *task;
5406
5407 task = alloc_async_task(&request->hdr, AsyncHttpEndRequestProc, sizeof(*task));
5408 task->flags = dwFlags;
5409 task->context = dwContext;
5410
5411 INTERNET_AsyncCall(&task->hdr);
5412 res = ERROR_IO_PENDING;
5413 }
5414 else
5415 res = HTTP_HttpEndRequestW(request, dwFlags, dwContext);
5416
5417 WININET_Release( &request->hdr );
5418 TRACE("%u <--\n", res);
5419 if(res != ERROR_SUCCESS)
5420 SetLastError(res);
5421 return res == ERROR_SUCCESS;
5422 }
5423
5424 /***********************************************************************
5425 * HttpSendRequestExA (WININET.@)
5426 *
5427 * Sends the specified request to the HTTP server and allows chunked
5428 * transfers.
5429 *
5430 * RETURNS
5431 * Success: TRUE
5432 * Failure: FALSE, call GetLastError() for more information.
5433 */
5434 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
5435 LPINTERNET_BUFFERSA lpBuffersIn,
5436 LPINTERNET_BUFFERSA lpBuffersOut,
5437 DWORD dwFlags, DWORD_PTR dwContext)
5438 {
5439 INTERNET_BUFFERSW BuffersInW;
5440 BOOL rc = FALSE;
5441 DWORD headerlen;
5442 LPWSTR header = NULL;
5443
5444 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
5445 lpBuffersOut, dwFlags, dwContext);
5446
5447 if (lpBuffersIn)
5448 {
5449 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
5450 if (lpBuffersIn->lpcszHeader)
5451 {
5452 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
5453 lpBuffersIn->dwHeadersLength,0,0);
5454 header = heap_alloc(headerlen*sizeof(WCHAR));
5455 if (!(BuffersInW.lpcszHeader = header))
5456 {
5457 SetLastError(ERROR_OUTOFMEMORY);
5458 return FALSE;
5459 }
5460 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
5461 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
5462 header, headerlen);
5463 }
5464 else
5465 BuffersInW.lpcszHeader = NULL;
5466 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
5467 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
5468 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
5469 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
5470 BuffersInW.Next = NULL;
5471 }
5472
5473 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
5474
5475 heap_free(header);
5476 return rc;
5477 }
5478
5479 /***********************************************************************
5480 * HttpSendRequestExW (WININET.@)
5481 *
5482 * Sends the specified request to the HTTP server and allows chunked
5483 * transfers
5484 *
5485 * RETURNS
5486 * Success: TRUE
5487 * Failure: FALSE, call GetLastError() for more information.
5488 */
5489 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
5490 LPINTERNET_BUFFERSW lpBuffersIn,
5491 LPINTERNET_BUFFERSW lpBuffersOut,
5492 DWORD dwFlags, DWORD_PTR dwContext)
5493 {
5494 http_request_t *request;
5495 http_session_t *session;
5496 appinfo_t *hIC;
5497 DWORD res;
5498
5499 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
5500 lpBuffersOut, dwFlags, dwContext);
5501
5502 request = (http_request_t*) get_handle_object( hRequest );
5503
5504 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5505 {
5506 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5507 goto lend;
5508 }
5509
5510 session = request->session;
5511 assert(session->hdr.htype == WH_HHTTPSESSION);
5512 hIC = session->appInfo;
5513 assert(hIC->hdr.htype == WH_HINIT);
5514
5515 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5516 {
5517 send_request_task_t *task;
5518
5519 task = alloc_async_task(&request->hdr, AsyncHttpSendRequestProc, sizeof(*task));
5520 if (lpBuffersIn)
5521 {
5522 DWORD size = 0;
5523
5524 if (lpBuffersIn->lpcszHeader)
5525 {
5526 if (lpBuffersIn->dwHeadersLength == ~0u)
5527 size = (strlenW( lpBuffersIn->lpcszHeader ) + 1) * sizeof(WCHAR);
5528 else
5529 size = lpBuffersIn->dwHeadersLength * sizeof(WCHAR);
5530
5531 task->headers = heap_alloc(size);
5532 memcpy(task->headers, lpBuffersIn->lpcszHeader, size);
5533 }
5534 else task->headers = NULL;
5535
5536 task->headers_len = size / sizeof(WCHAR);
5537 task->optional = lpBuffersIn->lpvBuffer;
5538 task->optional_len = lpBuffersIn->dwBufferLength;
5539 task->content_len = lpBuffersIn->dwBufferTotal;
5540 }
5541 else
5542 {
5543 task->headers = NULL;
5544 task->headers_len = 0;
5545 task->optional = NULL;
5546 task->optional_len = 0;
5547 task->content_len = 0;
5548 }
5549
5550 task->end_request = FALSE;
5551
5552 INTERNET_AsyncCall(&task->hdr);
5553 res = ERROR_IO_PENDING;
5554 }
5555 else
5556 {
5557 if (lpBuffersIn)
5558 res = HTTP_HttpSendRequestW(request, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
5559 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
5560 lpBuffersIn->dwBufferTotal, FALSE);
5561 else
5562 res = HTTP_HttpSendRequestW(request, NULL, 0, NULL, 0, 0, FALSE);
5563 }
5564
5565 lend:
5566 if ( request )
5567 WININET_Release( &request->hdr );
5568
5569 TRACE("<---\n");
5570 SetLastError(res);
5571 return res == ERROR_SUCCESS;
5572 }
5573
5574 /***********************************************************************
5575 * HttpSendRequestW (WININET.@)
5576 *
5577 * Sends the specified request to the HTTP server
5578 *
5579 * RETURNS
5580 * TRUE on success
5581 * FALSE on failure
5582 *
5583 */
5584 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
5585 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
5586 {
5587 http_request_t *request;
5588 http_session_t *session = NULL;
5589 appinfo_t *hIC = NULL;
5590 DWORD res = ERROR_SUCCESS;
5591
5592 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest,
5593 debugstr_wn(lpszHeaders, dwHeaderLength), dwHeaderLength, lpOptional, dwOptionalLength);
5594
5595 request = (http_request_t*) get_handle_object( hHttpRequest );
5596 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5597 {
5598 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5599 goto lend;
5600 }
5601
5602 session = request->session;
5603 if (NULL == session || session->hdr.htype != WH_HHTTPSESSION)
5604 {
5605 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5606 goto lend;
5607 }
5608
5609 hIC = session->appInfo;
5610 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
5611 {
5612 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5613 goto lend;
5614 }
5615
5616 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5617 {
5618 send_request_task_t *task;
5619
5620 task = alloc_async_task(&request->hdr, AsyncHttpSendRequestProc, sizeof(*task));
5621 if (lpszHeaders)
5622 {
5623 DWORD size;
5624
5625 if (dwHeaderLength == ~0u) size = (strlenW(lpszHeaders) + 1) * sizeof(WCHAR);
5626 else size = dwHeaderLength * sizeof(WCHAR);
5627
5628 task->headers = heap_alloc(size);
5629 memcpy(task->headers, lpszHeaders, size);
5630 }
5631 else
5632 task->headers = NULL;
5633 task->headers_len = dwHeaderLength;
5634 task->optional = lpOptional;
5635 task->optional_len = dwOptionalLength;
5636 task->content_len = dwOptionalLength;
5637 task->end_request = TRUE;
5638
5639 INTERNET_AsyncCall(&task->hdr);
5640 res = ERROR_IO_PENDING;
5641 }
5642 else
5643 {
5644 res = HTTP_HttpSendRequestW(request, lpszHeaders,
5645 dwHeaderLength, lpOptional, dwOptionalLength,
5646 dwOptionalLength, TRUE);
5647 }
5648 lend:
5649 if( request )
5650 WININET_Release( &request->hdr );
5651
5652 SetLastError(res);
5653 return res == ERROR_SUCCESS;
5654 }
5655
5656 /***********************************************************************
5657 * HttpSendRequestA (WININET.@)
5658 *
5659 * Sends the specified request to the HTTP server
5660 *
5661 * RETURNS
5662 * TRUE on success
5663 * FALSE on failure
5664 *
5665 */
5666 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
5667 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
5668 {
5669 BOOL result;
5670 LPWSTR szHeaders=NULL;
5671 DWORD nLen=dwHeaderLength;
5672 if(lpszHeaders!=NULL)
5673 {
5674 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
5675 szHeaders = heap_alloc(nLen*sizeof(WCHAR));
5676 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
5677 }
5678 result = HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
5679 heap_free(szHeaders);
5680 return result;
5681 }
5682
5683 /***********************************************************************
5684 * HTTPSESSION_Destroy (internal)
5685 *
5686 * Deallocate session handle
5687 *
5688 */
5689 static void HTTPSESSION_Destroy(object_header_t *hdr)
5690 {
5691 http_session_t *session = (http_session_t*) hdr;
5692
5693 TRACE("%p\n", session);
5694
5695 WININET_Release(&session->appInfo->hdr);
5696
5697 heap_free(session->hostName);
5698 heap_free(session->password);
5699 heap_free(session->userName);
5700 }
5701
5702 static DWORD HTTPSESSION_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
5703 {
5704 http_session_t *ses = (http_session_t *)hdr;
5705
5706 switch(option) {
5707 case INTERNET_OPTION_HANDLE_TYPE:
5708 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
5709
5710 if (*size < sizeof(ULONG))
5711 return ERROR_INSUFFICIENT_BUFFER;
5712
5713 *size = sizeof(DWORD);
5714 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_CONNECT_HTTP;
5715 return ERROR_SUCCESS;
5716 case INTERNET_OPTION_CONNECT_TIMEOUT:
5717 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
5718
5719 if (*size < sizeof(DWORD))
5720 return ERROR_INSUFFICIENT_BUFFER;
5721
5722 *size = sizeof(DWORD);
5723 *(DWORD *)buffer = ses->connect_timeout;
5724 return ERROR_SUCCESS;
5725
5726 case INTERNET_OPTION_SEND_TIMEOUT:
5727 TRACE("INTERNET_OPTION_SEND_TIMEOUT\n");
5728
5729 if (*size < sizeof(DWORD))
5730 return ERROR_INSUFFICIENT_BUFFER;
5731
5732 *size = sizeof(DWORD);
5733 *(DWORD *)buffer = ses->send_timeout;
5734 return ERROR_SUCCESS;
5735
5736 case INTERNET_OPTION_RECEIVE_TIMEOUT:
5737 TRACE("INTERNET_OPTION_RECEIVE_TIMEOUT\n");
5738
5739 if (*size < sizeof(DWORD))
5740 return ERROR_INSUFFICIENT_BUFFER;
5741
5742 *size = sizeof(DWORD);
5743 *(DWORD *)buffer = ses->receive_timeout;
5744 return ERROR_SUCCESS;
5745 }
5746
5747 return INET_QueryOption(hdr, option, buffer, size, unicode);
5748 }
5749
5750 static DWORD HTTPSESSION_SetOption(object_header_t *hdr, DWORD option, void *buffer, DWORD size)
5751 {
5752 http_session_t *ses = (http_session_t*)hdr;
5753
5754 switch(option) {
5755 case INTERNET_OPTION_USERNAME:
5756 {
5757 heap_free(ses->userName);
5758 if (!(ses->userName = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5759 return ERROR_SUCCESS;
5760 }
5761 case INTERNET_OPTION_PASSWORD:
5762 {
5763 heap_free(ses->password);
5764 if (!(ses->password = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5765 return ERROR_SUCCESS;
5766 }
5767 case INTERNET_OPTION_PROXY_USERNAME:
5768 {
5769 heap_free(ses->appInfo->proxyUsername);
5770 if (!(ses->appInfo->proxyUsername = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5771 return ERROR_SUCCESS;
5772 }
5773 case INTERNET_OPTION_PROXY_PASSWORD:
5774 {
5775 heap_free(ses->appInfo->proxyPassword);
5776 if (!(ses->appInfo->proxyPassword = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5777 return ERROR_SUCCESS;
5778 }
5779 case INTERNET_OPTION_CONNECT_TIMEOUT:
5780 {
5781 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5782 ses->connect_timeout = *(DWORD *)buffer;
5783 return ERROR_SUCCESS;
5784 }
5785 case INTERNET_OPTION_SEND_TIMEOUT:
5786 {
5787 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5788 ses->send_timeout = *(DWORD *)buffer;
5789 return ERROR_SUCCESS;
5790 }
5791 case INTERNET_OPTION_RECEIVE_TIMEOUT:
5792 {
5793 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5794 ses->receive_timeout = *(DWORD *)buffer;
5795 return ERROR_SUCCESS;
5796 }
5797 default: break;
5798 }
5799
5800 return INET_SetOption(hdr, option, buffer, size);
5801 }
5802
5803 static const object_vtbl_t HTTPSESSIONVtbl = {
5804 HTTPSESSION_Destroy,
5805 NULL,
5806 HTTPSESSION_QueryOption,
5807 HTTPSESSION_SetOption,
5808 NULL,
5809 NULL,
5810 NULL,
5811 NULL,
5812 NULL
5813 };
5814
5815
5816 /***********************************************************************
5817 * HTTP_Connect (internal)
5818 *
5819 * Create http session handle
5820 *
5821 * RETURNS
5822 * HINTERNET a session handle on success
5823 * NULL on failure
5824 *
5825 */
5826 DWORD HTTP_Connect(appinfo_t *hIC, LPCWSTR lpszServerName,
5827 INTERNET_PORT serverPort, LPCWSTR lpszUserName,
5828 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
5829 DWORD dwInternalFlags, HINTERNET *ret)
5830 {
5831 http_session_t *session = NULL;
5832
5833 TRACE("-->\n");
5834
5835 if (!lpszServerName || !lpszServerName[0])
5836 return ERROR_INVALID_PARAMETER;
5837
5838 assert( hIC->hdr.htype == WH_HINIT );
5839
5840 session = alloc_object(&hIC->hdr, &HTTPSESSIONVtbl, sizeof(http_session_t));
5841 if (!session)
5842 return ERROR_OUTOFMEMORY;
5843
5844 /*
5845 * According to my tests. The name is not resolved until a request is sent
5846 */
5847
5848 session->hdr.htype = WH_HHTTPSESSION;
5849 session->hdr.dwFlags = dwFlags;
5850 session->hdr.dwContext = dwContext;
5851 session->hdr.dwInternalFlags |= dwInternalFlags;
5852
5853 WININET_AddRef( &hIC->hdr );
5854 session->appInfo = hIC;
5855 list_add_head( &hIC->hdr.children, &session->hdr.entry );
5856
5857 session->hostName = heap_strdupW(lpszServerName);
5858 if (lpszUserName && lpszUserName[0])
5859 session->userName = heap_strdupW(lpszUserName);
5860 if (lpszPassword && lpszPassword[0])
5861 session->password = heap_strdupW(lpszPassword);
5862 session->hostPort = serverPort;
5863 session->connect_timeout = hIC->connect_timeout;
5864 session->send_timeout = 0;
5865 session->receive_timeout = 0;
5866
5867 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
5868 if (!(session->hdr.dwInternalFlags & INET_OPENURL))
5869 {
5870 INTERNET_SendCallback(&hIC->hdr, dwContext,
5871 INTERNET_STATUS_HANDLE_CREATED, &session->hdr.hInternet,
5872 sizeof(HINTERNET));
5873 }
5874
5875 /*
5876 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
5877 * windows
5878 */
5879
5880 TRACE("%p --> %p\n", hIC, session);
5881
5882 *ret = session->hdr.hInternet;
5883 return ERROR_SUCCESS;
5884 }
5885
5886 /***********************************************************************
5887 * HTTP_clear_response_headers (internal)
5888 *
5889 * clear out any old response headers
5890 */
5891 static void HTTP_clear_response_headers( http_request_t *request )
5892 {
5893 DWORD i;
5894
5895 EnterCriticalSection( &request->headers_section );
5896
5897 for( i=0; i<request->nCustHeaders; i++)
5898 {
5899 if( !request->custHeaders[i].lpszField )
5900 continue;
5901 if( !request->custHeaders[i].lpszValue )
5902 continue;
5903 if ( request->custHeaders[i].wFlags & HDR_ISREQUEST )
5904 continue;
5905 HTTP_DeleteCustomHeader( request, i );
5906 i--;
5907 }
5908
5909 LeaveCriticalSection( &request->headers_section );
5910 }
5911
5912 /***********************************************************************
5913 * HTTP_GetResponseHeaders (internal)
5914 *
5915 * Read server response
5916 *
5917 * RETURNS
5918 *
5919 * TRUE on success
5920 * FALSE on error
5921 */
5922 static DWORD HTTP_GetResponseHeaders(http_request_t *request, INT *len)
5923 {
5924 INT cbreaks = 0;
5925 WCHAR buffer[MAX_REPLY_LEN];
5926 DWORD buflen = MAX_REPLY_LEN;
5927 INT rc = 0;
5928 char bufferA[MAX_REPLY_LEN];
5929 LPWSTR status_code = NULL, status_text = NULL;
5930 DWORD res = ERROR_HTTP_INVALID_SERVER_RESPONSE;
5931 BOOL codeHundred = FALSE;
5932
5933 TRACE("-->\n");
5934
5935 if(!is_valid_netconn(request->netconn))
5936 goto lend;
5937
5938 /* clear old response headers (eg. from a redirect response) */
5939 HTTP_clear_response_headers( request );
5940
5941 NETCON_set_timeout( request->netconn, FALSE, request->receive_timeout );
5942 do {
5943 /*
5944 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
5945 */
5946 buflen = MAX_REPLY_LEN;
5947 if ((res = read_line(request, bufferA, &buflen)))
5948 goto lend;
5949
5950 if (!buflen) goto lend;
5951
5952 rc += buflen;
5953 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
5954 /* check is this a status code line? */
5955 if (!strncmpW(buffer, g_szHttp1_0, 4))
5956 {
5957 /* split the version from the status code */
5958 status_code = strchrW( buffer, ' ' );
5959 if( !status_code )
5960 goto lend;
5961 *status_code++=0;
5962
5963 /* split the status code from the status text */
5964 status_text = strchrW( status_code, ' ' );
5965 if( status_text )
5966 *status_text++=0;
5967
5968 request->status_code = atoiW(status_code);
5969
5970 TRACE("version [%s] status code [%s] status text [%s]\n",
5971 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
5972
5973 codeHundred = request->status_code == HTTP_STATUS_CONTINUE;
5974 }
5975 else if (!codeHundred)
5976 {
5977 WARN("No status line at head of response (%s)\n", debugstr_w(buffer));
5978
5979 heap_free(request->version);
5980 heap_free(request->statusText);
5981
5982 request->status_code = HTTP_STATUS_OK;
5983 request->version = heap_strdupW(g_szHttp1_0);
5984 request->statusText = heap_strdupW(szOK);
5985
5986 goto lend;
5987 }
5988 } while (codeHundred);
5989
5990 /* Add status code */
5991 HTTP_ProcessHeader(request, szStatus, status_code,
5992 HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_ADD);
5993
5994 heap_free(request->version);
5995 heap_free(request->statusText);
5996
5997 request->version = heap_strdupW(buffer);
5998 request->statusText = heap_strdupW(status_text ? status_text : emptyW);
5999
6000 /* Restore the spaces */
6001 *(status_code-1) = ' ';
6002 if (status_text)
6003 *(status_text-1) = ' ';
6004
6005 /* Parse each response line */
6006 do
6007 {
6008 buflen = MAX_REPLY_LEN;
6009 if (!read_line(request, bufferA, &buflen) && buflen)
6010 {
6011 LPWSTR * pFieldAndValue;
6012
6013 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
6014
6015 if (!bufferA[0]) break;
6016 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
6017
6018 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
6019 if (pFieldAndValue)
6020 {
6021 HTTP_ProcessHeader(request, pFieldAndValue[0], pFieldAndValue[1],
6022 HTTP_ADDREQ_FLAG_ADD );
6023 HTTP_FreeTokens(pFieldAndValue);
6024 }
6025 }
6026 else
6027 {
6028 cbreaks++;
6029 if (cbreaks >= 2)
6030 break;
6031 }
6032 }while(1);
6033
6034 res = ERROR_SUCCESS;
6035
6036 lend:
6037
6038 *len = rc;
6039 TRACE("<--\n");
6040 return res;
6041 }
6042
6043 /***********************************************************************
6044 * HTTP_InterpretHttpHeader (internal)
6045 *
6046 * Parse server response
6047 *
6048 * RETURNS
6049 *
6050 * Pointer to array of field, value, NULL on success.
6051 * NULL on error.
6052 */
6053 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
6054 {
6055 LPWSTR * pTokenPair;
6056 LPWSTR pszColon;
6057 INT len;
6058
6059 pTokenPair = heap_alloc_zero(sizeof(*pTokenPair)*3);
6060
6061 pszColon = strchrW(buffer, ':');
6062 /* must have two tokens */
6063 if (!pszColon)
6064 {
6065 HTTP_FreeTokens(pTokenPair);
6066 if (buffer[0])
6067 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
6068 return NULL;
6069 }
6070
6071 pTokenPair[0] = heap_alloc((pszColon - buffer + 1) * sizeof(WCHAR));
6072 if (!pTokenPair[0])
6073 {
6074 HTTP_FreeTokens(pTokenPair);
6075 return NULL;
6076 }
6077 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
6078 pTokenPair[0][pszColon - buffer] = '\0';
6079
6080 /* skip colon */
6081 pszColon++;
6082 len = strlenW(pszColon);
6083 pTokenPair[1] = heap_alloc((len + 1) * sizeof(WCHAR));
6084 if (!pTokenPair[1])
6085 {
6086 HTTP_FreeTokens(pTokenPair);
6087 return NULL;
6088 }
6089 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
6090
6091 strip_spaces(pTokenPair[0]);
6092 strip_spaces(pTokenPair[1]);
6093
6094 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
6095 return pTokenPair;
6096 }
6097
6098 /***********************************************************************
6099 * HTTP_ProcessHeader (internal)
6100 *
6101 * Stuff header into header tables according to <dwModifier>
6102 *
6103 */
6104
6105 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
6106
6107 static DWORD HTTP_ProcessHeader(http_request_t *request, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
6108 {
6109 LPHTTPHEADERW lphttpHdr;
6110 INT index;
6111 BOOL request_only = !!(dwModifier & HTTP_ADDHDR_FLAG_REQ);
6112 DWORD res = ERROR_SUCCESS;
6113
6114 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), dwModifier);
6115
6116 EnterCriticalSection( &request->headers_section );
6117
6118 index = HTTP_GetCustomHeaderIndex(request, field, 0, request_only);
6119 if (index >= 0)
6120 {
6121 lphttpHdr = &request->custHeaders[index];
6122
6123 /* replace existing header if FLAG_REPLACE is given */
6124 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
6125 {
6126 HTTP_DeleteCustomHeader( request, index );
6127
6128 if (value && value[0])
6129 {
6130 HTTPHEADERW hdr;
6131
6132 hdr.lpszField = (LPWSTR)field;
6133 hdr.lpszValue = (LPWSTR)value;
6134 hdr.wFlags = hdr.wCount = 0;
6135
6136 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6137 hdr.wFlags |= HDR_ISREQUEST;
6138
6139 res = HTTP_InsertCustomHeader( request, &hdr );
6140 }
6141
6142 goto out;
6143 }
6144
6145 /* do not add new header if FLAG_ADD_IF_NEW is set */
6146 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
6147 {
6148 res = ERROR_HTTP_INVALID_HEADER; /* FIXME */
6149 goto out;
6150 }
6151
6152 /* handle appending to existing header */
6153 if (dwModifier & COALESCEFLAGS)
6154 {
6155 LPWSTR lpsztmp;
6156 WCHAR ch = 0;
6157 INT len = 0;
6158 INT origlen = strlenW(lphttpHdr->lpszValue);
6159 INT valuelen = strlenW(value);
6160
6161 /* FIXME: Should it really clear HDR_ISREQUEST? */
6162 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6163 lphttpHdr->wFlags |= HDR_ISREQUEST;
6164 else
6165 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
6166
6167 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
6168 {
6169 ch = ',';
6170 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
6171 }
6172 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
6173 {
6174 ch = ';';
6175 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
6176 }
6177
6178 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
6179
6180 lpsztmp = heap_realloc(lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
6181 if (lpsztmp)
6182 {
6183 lphttpHdr->lpszValue = lpsztmp;
6184 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
6185 if (ch > 0)
6186 {
6187 lphttpHdr->lpszValue[origlen] = ch;
6188 origlen++;
6189 lphttpHdr->lpszValue[origlen] = ' ';
6190 origlen++;
6191 }
6192
6193 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
6194 lphttpHdr->lpszValue[len] = '\0';
6195 }
6196 else
6197 {
6198 WARN("heap_realloc (%d bytes) failed\n",len+1);
6199 res = ERROR_OUTOFMEMORY;
6200 }
6201
6202 goto out;
6203 }
6204 }
6205
6206 /* FIXME: What about other combinations? */
6207 if ((dwModifier & ~HTTP_ADDHDR_FLAG_REQ) == HTTP_ADDHDR_FLAG_REPLACE)
6208 {
6209 res = ERROR_HTTP_HEADER_NOT_FOUND;
6210 goto out;
6211 }
6212
6213 /* FIXME: What if value == ""? */
6214 if (value)
6215 {
6216 HTTPHEADERW hdr;
6217
6218 hdr.lpszField = (LPWSTR)field;
6219 hdr.lpszValue = (LPWSTR)value;
6220 hdr.wFlags = hdr.wCount = 0;
6221
6222 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6223 hdr.wFlags |= HDR_ISREQUEST;
6224
6225 res = HTTP_InsertCustomHeader( request, &hdr );
6226 goto out;
6227 }
6228
6229 /* FIXME: What if value == NULL? */
6230 out:
6231 TRACE("<-- %d\n", res);
6232 LeaveCriticalSection( &request->headers_section );
6233 return res;
6234 }
6235
6236 /***********************************************************************
6237 * HTTP_GetCustomHeaderIndex (internal)
6238 *
6239 * Return index of custom header from header array
6240 * Headers section must be held
6241 */
6242 static INT HTTP_GetCustomHeaderIndex(http_request_t *request, LPCWSTR lpszField,
6243 int requested_index, BOOL request_only)
6244 {
6245 DWORD index;
6246
6247 TRACE("%s, %d, %d\n", debugstr_w(lpszField), requested_index, request_only);
6248
6249 for (index = 0; index < request->nCustHeaders; index++)
6250 {
6251 if (strcmpiW(request->custHeaders[index].lpszField, lpszField))
6252 continue;
6253
6254 if (request_only && !(request->custHeaders[index].wFlags & HDR_ISREQUEST))
6255 continue;
6256
6257 if (!request_only && (request->custHeaders[index].wFlags & HDR_ISREQUEST))
6258 continue;
6259
6260 if (requested_index == 0)
6261 break;
6262 requested_index --;
6263 }
6264
6265 if (index >= request->nCustHeaders)
6266 index = -1;
6267
6268 TRACE("Return: %d\n", index);
6269 return index;
6270 }
6271
6272
6273 /***********************************************************************
6274 * HTTP_InsertCustomHeader (internal)
6275 *
6276 * Insert header into array
6277 * Headers section must be held
6278 */
6279 static DWORD HTTP_InsertCustomHeader(http_request_t *request, LPHTTPHEADERW lpHdr)
6280 {
6281 INT count;
6282 LPHTTPHEADERW lph = NULL;
6283
6284 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
6285 count = request->nCustHeaders + 1;
6286 if (count > 1)
6287 lph = heap_realloc_zero(request->custHeaders, sizeof(HTTPHEADERW) * count);
6288 else
6289 lph = heap_alloc_zero(sizeof(HTTPHEADERW) * count);
6290
6291 if (!lph)
6292 return ERROR_OUTOFMEMORY;
6293
6294 request->custHeaders = lph;
6295 request->custHeaders[count-1].lpszField = heap_strdupW(lpHdr->lpszField);
6296 request->custHeaders[count-1].lpszValue = heap_strdupW(lpHdr->lpszValue);
6297 request->custHeaders[count-1].wFlags = lpHdr->wFlags;
6298 request->custHeaders[count-1].wCount= lpHdr->wCount;
6299 request->nCustHeaders++;
6300
6301 return ERROR_SUCCESS;
6302 }
6303
6304
6305 /***********************************************************************
6306 * HTTP_DeleteCustomHeader (internal)
6307 *
6308 * Delete header from array
6309 * If this function is called, the index may change.
6310 * Headers section must be held
6311 */
6312 static BOOL HTTP_DeleteCustomHeader(http_request_t *request, DWORD index)
6313 {
6314 if( request->nCustHeaders <= 0 )
6315 return FALSE;
6316 if( index >= request->nCustHeaders )
6317 return FALSE;
6318 request->nCustHeaders--;
6319
6320 heap_free(request->custHeaders[index].lpszField);
6321 heap_free(request->custHeaders[index].lpszValue);
6322
6323 memmove( &request->custHeaders[index], &request->custHeaders[index+1],
6324 (request->nCustHeaders - index)* sizeof(HTTPHEADERW) );
6325 memset( &request->custHeaders[request->nCustHeaders], 0, sizeof(HTTPHEADERW) );
6326
6327 return TRUE;
6328 }
6329
6330
6331 /***********************************************************************
6332 * IsHostInProxyBypassList (@)
6333 *
6334 * Undocumented
6335 *
6336 */
6337 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
6338 {
6339 FIXME("STUB: flags=%d host=%s length=%d\n",flags,szHost,length);
6340 return FALSE;
6341 }