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