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