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