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