wined3d: Use wined3d_uint32_compare() in compare_sig().
[wine.git] / dlls / wininet / http.c
blobc493137896a3f75895c9be5d007938c2b16cccaf
1 /*
2 * Wininet - HTTP Implementation
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
12 * Ulrich Czekalla
13 * David Hammerton
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.
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.
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
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <time.h>
34 #include <assert.h>
35 #include <errno.h>
36 #include <limits.h>
37 #include <zlib.h>
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wininet.h"
42 #include "winerror.h"
43 #include "winternl.h"
44 #include "winsock2.h"
45 #include "ws2ipdef.h"
46 #define NO_SHLWAPI_STREAM
47 #define NO_SHLWAPI_REG
48 #define NO_SHLWAPI_GDI
49 #include "shlwapi.h"
50 #include "sspi.h"
51 #include "wincrypt.h"
52 #include "winuser.h"
54 #include "internet.h"
55 #include "resource.h"
56 #include "wine/debug.h"
57 #include "wine/exception.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
61 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
62 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
63 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
64 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
65 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
66 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
68 #define COLLECT_TIME 60000
70 struct HttpAuthInfo
72 LPWSTR scheme;
73 CredHandle cred;
74 CtxtHandle ctx;
75 TimeStamp exp;
76 ULONG attr;
77 ULONG max_token;
78 void *auth_data;
79 unsigned int auth_data_len;
80 BOOL finished; /* finished authenticating */
84 typedef struct _basicAuthorizationData
86 struct list entry;
88 LPWSTR host;
89 LPWSTR realm;
90 LPSTR authorization;
91 UINT authorizationLen;
92 } basicAuthorizationData;
94 typedef struct _authorizationData
96 struct list entry;
98 LPWSTR host;
99 LPWSTR scheme;
100 LPWSTR domain;
101 UINT domain_len;
102 LPWSTR user;
103 UINT user_len;
104 LPWSTR password;
105 UINT password_len;
106 } authorizationData;
108 static struct list basicAuthorizationCache = LIST_INIT(basicAuthorizationCache);
109 static struct list authorizationCache = LIST_INIT(authorizationCache);
111 static CRITICAL_SECTION authcache_cs;
112 static CRITICAL_SECTION_DEBUG critsect_debug =
114 0, 0, &authcache_cs,
115 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
116 0, 0, { (DWORD_PTR)(__FILE__ ": authcache_cs") }
118 static CRITICAL_SECTION authcache_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
120 static DWORD HTTP_GetResponseHeaders(http_request_t *req, INT *len);
121 static DWORD HTTP_ProcessHeader(http_request_t *req, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
122 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
123 static DWORD HTTP_InsertCustomHeader(http_request_t *req, LPHTTPHEADERW lpHdr);
124 static INT HTTP_GetCustomHeaderIndex(http_request_t *req, LPCWSTR lpszField, INT index, BOOL Request);
125 static BOOL HTTP_DeleteCustomHeader(http_request_t *req, DWORD index);
126 static LPWSTR HTTP_build_req( LPCWSTR *list, int len );
127 static DWORD HTTP_HttpQueryInfoW(http_request_t*, DWORD, LPVOID, LPDWORD, LPDWORD);
128 static UINT HTTP_DecodeBase64(LPCWSTR base64, LPSTR bin);
129 static DWORD drain_content(http_request_t*,BOOL);
131 static CRITICAL_SECTION connection_pool_cs;
132 static CRITICAL_SECTION_DEBUG connection_pool_debug =
134 0, 0, &connection_pool_cs,
135 { &connection_pool_debug.ProcessLocksList, &connection_pool_debug.ProcessLocksList },
136 0, 0, { (DWORD_PTR)(__FILE__ ": connection_pool_cs") }
138 static CRITICAL_SECTION connection_pool_cs = { &connection_pool_debug, -1, 0, 0, 0, 0 };
140 static struct list connection_pool = LIST_INIT(connection_pool);
141 static BOOL collector_running;
143 void server_addref(server_t *server)
145 InterlockedIncrement(&server->ref);
148 void server_release(server_t *server)
150 if(InterlockedDecrement(&server->ref))
151 return;
153 list_remove(&server->entry);
155 if(server->cert_chain)
156 CertFreeCertificateChain(server->cert_chain);
157 heap_free(server->name);
158 heap_free(server->scheme_host_port);
159 heap_free(server);
162 static BOOL process_host_port(server_t *server)
164 BOOL default_port;
165 size_t name_len, len;
166 WCHAR *buf;
168 name_len = lstrlenW(server->name);
169 len = name_len + 10 /* strlen("://:<port>") */ + ARRAY_SIZE(L"https");
170 buf = heap_alloc( len * sizeof(WCHAR) );
171 if(!buf)
172 return FALSE;
174 swprintf(buf, len, L"%s://%s:%u", server->is_https ? L"https" : L"http", server->name, server->port);
175 server->scheme_host_port = buf;
177 server->host_port = server->scheme_host_port + 7 /* strlen("http://") */;
178 if(server->is_https)
179 server->host_port++;
181 default_port = server->port == (server->is_https ? INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT);
182 server->canon_host_port = default_port ? server->name : server->host_port;
183 return TRUE;
186 server_t *get_server(substr_t name, INTERNET_PORT port, BOOL is_https, BOOL do_create)
188 server_t *iter, *server = NULL;
190 EnterCriticalSection(&connection_pool_cs);
192 LIST_FOR_EACH_ENTRY(iter, &connection_pool, server_t, entry) {
193 if(iter->port == port && name.len == lstrlenW(iter->name) && !wcsnicmp(iter->name, name.str, name.len)
194 && iter->is_https == is_https) {
195 server = iter;
196 server_addref(server);
197 break;
201 if(!server && do_create) {
202 server = heap_alloc_zero(sizeof(*server));
203 if(server) {
204 server->ref = 2; /* list reference and return */
205 server->port = port;
206 server->is_https = is_https;
207 list_init(&server->conn_pool);
208 server->name = heap_strndupW(name.str, name.len);
209 if(server->name && process_host_port(server)) {
210 list_add_head(&connection_pool, &server->entry);
211 }else {
212 heap_free(server);
213 server = NULL;
218 LeaveCriticalSection(&connection_pool_cs);
220 return server;
223 BOOL collect_connections(collect_type_t collect_type)
225 netconn_t *netconn, *netconn_safe;
226 server_t *server, *server_safe;
227 BOOL remaining = FALSE;
228 DWORD64 now;
230 now = GetTickCount64();
232 LIST_FOR_EACH_ENTRY_SAFE(server, server_safe, &connection_pool, server_t, entry) {
233 LIST_FOR_EACH_ENTRY_SAFE(netconn, netconn_safe, &server->conn_pool, netconn_t, pool_entry) {
234 if(collect_type > COLLECT_TIMEOUT || netconn->keep_until < now) {
235 TRACE("freeing %p\n", netconn);
236 list_remove(&netconn->pool_entry);
237 free_netconn(netconn);
238 }else {
239 remaining = TRUE;
243 if(collect_type == COLLECT_CLEANUP) {
244 list_remove(&server->entry);
245 list_init(&server->entry);
246 server_release(server);
250 return remaining;
253 static DWORD WINAPI collect_connections_proc(void *arg)
255 BOOL remaining_conns;
257 do {
258 /* FIXME: Use more sophisticated method */
259 Sleep(5000);
261 EnterCriticalSection(&connection_pool_cs);
263 remaining_conns = collect_connections(COLLECT_TIMEOUT);
264 if(!remaining_conns)
265 collector_running = FALSE;
267 LeaveCriticalSection(&connection_pool_cs);
268 }while(remaining_conns);
270 FreeLibraryAndExitThread(WININET_hModule, 0);
273 /***********************************************************************
274 * HTTP_GetHeader (internal)
276 * Headers section must be held
278 static LPHTTPHEADERW HTTP_GetHeader(http_request_t *req, LPCWSTR head)
280 int HeaderIndex = 0;
281 HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
282 if (HeaderIndex == -1)
283 return NULL;
284 else
285 return &req->custHeaders[HeaderIndex];
288 static WCHAR *get_host_header( http_request_t *req )
290 HTTPHEADERW *header;
291 WCHAR *ret = NULL;
293 EnterCriticalSection( &req->headers_section );
294 if ((header = HTTP_GetHeader( req, L"Host" ))) ret = heap_strdupW( header->lpszValue );
295 else ret = heap_strdupW( req->server->canon_host_port );
296 LeaveCriticalSection( &req->headers_section );
297 return ret;
300 struct data_stream_vtbl_t {
301 BOOL (*end_of_data)(data_stream_t*,http_request_t*);
302 DWORD (*read)(data_stream_t*,http_request_t*,BYTE*,DWORD,DWORD*,BOOL);
303 DWORD (*drain_content)(data_stream_t*,http_request_t*,BOOL);
304 void (*destroy)(data_stream_t*);
307 typedef struct {
308 data_stream_t data_stream;
310 BYTE buf[READ_BUFFER_SIZE];
311 DWORD buf_size;
312 DWORD buf_pos;
313 DWORD chunk_size;
315 enum {
316 CHUNKED_STREAM_STATE_READING_CHUNK_SIZE,
317 CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE,
318 CHUNKED_STREAM_STATE_READING_CHUNK,
319 CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA,
320 CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END,
321 CHUNKED_STREAM_STATE_END_OF_STREAM,
322 CHUNKED_STREAM_STATE_ERROR
323 } state;
324 } chunked_stream_t;
326 static inline void destroy_data_stream(data_stream_t *stream)
328 stream->vtbl->destroy(stream);
331 static void reset_data_stream(http_request_t *req)
333 destroy_data_stream(req->data_stream);
334 req->data_stream = &req->netconn_stream.data_stream;
335 req->read_pos = req->read_size = req->netconn_stream.content_read = 0;
336 req->read_gzip = FALSE;
339 static void remove_header( http_request_t *request, const WCHAR *str, BOOL from_request )
341 int index;
342 EnterCriticalSection( &request->headers_section );
343 index = HTTP_GetCustomHeaderIndex( request, str, 0, from_request );
344 if (index != -1) HTTP_DeleteCustomHeader( request, index );
345 LeaveCriticalSection( &request->headers_section );
348 typedef struct {
349 data_stream_t stream;
350 data_stream_t *parent_stream;
351 z_stream zstream;
352 BYTE buf[READ_BUFFER_SIZE];
353 DWORD buf_size;
354 DWORD buf_pos;
355 BOOL end_of_data;
356 } gzip_stream_t;
358 static BOOL gzip_end_of_data(data_stream_t *stream, http_request_t *req)
360 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
361 return gzip_stream->end_of_data
362 || (!gzip_stream->buf_size && gzip_stream->parent_stream->vtbl->end_of_data(gzip_stream->parent_stream, req));
365 static DWORD gzip_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
366 DWORD *read, BOOL allow_blocking)
368 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
369 z_stream *zstream = &gzip_stream->zstream;
370 DWORD current_read, ret_read = 0;
371 int zres;
372 DWORD res = ERROR_SUCCESS;
374 TRACE("(%d %x)\n", size, allow_blocking);
376 while(size && !gzip_stream->end_of_data) {
377 if(!gzip_stream->buf_size) {
378 if(gzip_stream->buf_pos) {
379 if(gzip_stream->buf_size)
380 memmove(gzip_stream->buf, gzip_stream->buf+gzip_stream->buf_pos, gzip_stream->buf_size);
381 gzip_stream->buf_pos = 0;
383 res = gzip_stream->parent_stream->vtbl->read(gzip_stream->parent_stream, req, gzip_stream->buf+gzip_stream->buf_size,
384 sizeof(gzip_stream->buf)-gzip_stream->buf_size, &current_read, allow_blocking);
385 if(res != ERROR_SUCCESS)
386 break;
388 gzip_stream->buf_size += current_read;
389 if(!current_read) {
390 WARN("unexpected end of data\n");
391 gzip_stream->end_of_data = TRUE;
392 break;
396 zstream->next_in = gzip_stream->buf+gzip_stream->buf_pos;
397 zstream->avail_in = gzip_stream->buf_size;
398 zstream->next_out = buf+ret_read;
399 zstream->avail_out = size;
400 zres = inflate(&gzip_stream->zstream, 0);
401 current_read = size - zstream->avail_out;
402 size -= current_read;
403 ret_read += current_read;
404 gzip_stream->buf_size -= zstream->next_in - (gzip_stream->buf+gzip_stream->buf_pos);
405 gzip_stream->buf_pos = zstream->next_in-gzip_stream->buf;
406 if(zres == Z_STREAM_END) {
407 TRACE("end of data\n");
408 gzip_stream->end_of_data = TRUE;
409 inflateEnd(zstream);
410 }else if(zres != Z_OK) {
411 WARN("inflate failed %d: %s\n", zres, debugstr_a(zstream->msg));
412 if(!ret_read)
413 res = ERROR_INTERNET_DECODING_FAILED;
414 break;
417 if(ret_read)
418 allow_blocking = FALSE;
421 TRACE("read %u bytes\n", ret_read);
422 if(ret_read)
423 res = ERROR_SUCCESS;
424 *read = ret_read;
425 return res;
428 static DWORD gzip_drain_content(data_stream_t *stream, http_request_t *req, BOOL allow_blocking)
430 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
431 return gzip_stream->parent_stream->vtbl->drain_content(gzip_stream->parent_stream, req, allow_blocking);
434 static void gzip_destroy(data_stream_t *stream)
436 gzip_stream_t *gzip_stream = (gzip_stream_t*)stream;
438 destroy_data_stream(gzip_stream->parent_stream);
440 if(!gzip_stream->end_of_data)
441 inflateEnd(&gzip_stream->zstream);
442 heap_free(gzip_stream);
445 static const data_stream_vtbl_t gzip_stream_vtbl = {
446 gzip_end_of_data,
447 gzip_read,
448 gzip_drain_content,
449 gzip_destroy
452 static voidpf wininet_zalloc(voidpf opaque, uInt items, uInt size)
454 return heap_alloc(items*size);
457 static void wininet_zfree(voidpf opaque, voidpf address)
459 heap_free(address);
462 static DWORD init_gzip_stream(http_request_t *req, BOOL is_gzip)
464 gzip_stream_t *gzip_stream;
465 int zres;
467 gzip_stream = heap_alloc_zero(sizeof(gzip_stream_t));
468 if(!gzip_stream)
469 return ERROR_OUTOFMEMORY;
471 gzip_stream->stream.vtbl = &gzip_stream_vtbl;
472 gzip_stream->zstream.zalloc = wininet_zalloc;
473 gzip_stream->zstream.zfree = wininet_zfree;
475 zres = inflateInit2(&gzip_stream->zstream, is_gzip ? 0x1f : -15);
476 if(zres != Z_OK) {
477 ERR("inflateInit failed: %d\n", zres);
478 heap_free(gzip_stream);
479 return ERROR_OUTOFMEMORY;
482 remove_header(req, L"Content-Length", FALSE);
484 if(req->read_size) {
485 memcpy(gzip_stream->buf, req->read_buf+req->read_pos, req->read_size);
486 gzip_stream->buf_size = req->read_size;
487 req->read_pos = req->read_size = 0;
490 req->read_gzip = TRUE;
491 gzip_stream->parent_stream = req->data_stream;
492 req->data_stream = &gzip_stream->stream;
493 return ERROR_SUCCESS;
496 /***********************************************************************
497 * HTTP_FreeTokens (internal)
499 * Frees table of pointers.
501 static void HTTP_FreeTokens(LPWSTR * token_array)
503 int i;
504 for (i = 0; token_array[i]; i++) heap_free(token_array[i]);
505 heap_free(token_array);
508 static void HTTP_FixURL(http_request_t *request)
510 /* If we don't have a path we set it to root */
511 if (NULL == request->path)
512 request->path = heap_strdupW(L"/");
513 else /* remove \r and \n*/
515 int nLen = lstrlenW(request->path);
516 while ((nLen >0 ) && ((request->path[nLen-1] == '\r')||(request->path[nLen-1] == '\n')))
518 nLen--;
519 request->path[nLen]='\0';
521 /* Replace '\' with '/' */
522 while (nLen>0) {
523 nLen--;
524 if (request->path[nLen] == '\\') request->path[nLen]='/';
528 if(CSTR_EQUAL != CompareStringW( LOCALE_INVARIANT, NORM_IGNORECASE,
529 request->path, lstrlenW(request->path), L"http://", lstrlenW(L"http://") )
530 && request->path[0] != '/') /* not an absolute path ?? --> fix it !! */
532 WCHAR *fixurl = heap_alloc((lstrlenW(request->path) + 2)*sizeof(WCHAR));
533 *fixurl = '/';
534 lstrcpyW(fixurl + 1, request->path);
535 heap_free( request->path );
536 request->path = fixurl;
540 static WCHAR* build_request_header(http_request_t *request, const WCHAR *verb,
541 const WCHAR *path, const WCHAR *version, BOOL use_cr)
543 LPWSTR requestString;
544 DWORD len, n;
545 LPCWSTR *req;
546 UINT i;
548 EnterCriticalSection( &request->headers_section );
550 /* allocate space for an array of all the string pointers to be added */
551 len = request->nCustHeaders * 5 + 10;
552 if (!(req = heap_alloc( len * sizeof(const WCHAR *) )))
554 LeaveCriticalSection( &request->headers_section );
555 return NULL;
558 /* add the verb, path and HTTP version string */
559 n = 0;
560 req[n++] = verb;
561 req[n++] = L" ";
562 req[n++] = path;
563 req[n++] = L" ";
564 req[n++] = version;
565 if (use_cr)
566 req[n++] = L"\r";
567 req[n++] = L"\n";
569 /* Append custom request headers */
570 for (i = 0; i < request->nCustHeaders; i++)
572 if (request->custHeaders[i].wFlags & HDR_ISREQUEST)
574 req[n++] = request->custHeaders[i].lpszField;
575 req[n++] = L": ";
576 req[n++] = request->custHeaders[i].lpszValue;
577 if (use_cr)
578 req[n++] = L"\r";
579 req[n++] = L"\n";
581 TRACE("Adding custom header %s (%s)\n",
582 debugstr_w(request->custHeaders[i].lpszField),
583 debugstr_w(request->custHeaders[i].lpszValue));
586 if (use_cr)
587 req[n++] = L"\r";
588 req[n++] = L"\n";
589 req[n] = NULL;
591 requestString = HTTP_build_req( req, 4 );
592 heap_free( req );
593 LeaveCriticalSection( &request->headers_section );
594 return requestString;
597 static WCHAR* build_response_header(http_request_t *request, BOOL use_cr)
599 const WCHAR **req;
600 WCHAR *ret, buf[14];
601 DWORD i, n = 0;
603 EnterCriticalSection( &request->headers_section );
605 if (!(req = heap_alloc( (request->nCustHeaders * 5 + 8) * sizeof(WCHAR *) )))
607 LeaveCriticalSection( &request->headers_section );
608 return NULL;
611 if (request->status_code)
613 req[n++] = request->version;
614 swprintf(buf, ARRAY_SIZE(buf), L" %u ", request->status_code);
615 req[n++] = buf;
616 req[n++] = request->statusText;
617 if (use_cr)
618 req[n++] = L"\r";
619 req[n++] = L"\n";
622 for(i = 0; i < request->nCustHeaders; i++)
624 if(!(request->custHeaders[i].wFlags & HDR_ISREQUEST)
625 && wcscmp(request->custHeaders[i].lpszField, L"Status"))
627 req[n++] = request->custHeaders[i].lpszField;
628 req[n++] = L": ";
629 req[n++] = request->custHeaders[i].lpszValue;
630 if(use_cr)
631 req[n++] = L"\r";
632 req[n++] = L"\n";
634 TRACE("Adding custom header %s (%s)\n",
635 debugstr_w(request->custHeaders[i].lpszField),
636 debugstr_w(request->custHeaders[i].lpszValue));
639 if(use_cr)
640 req[n++] = L"\r";
641 req[n++] = L"\n";
642 req[n] = NULL;
644 ret = HTTP_build_req(req, 0);
645 heap_free(req);
646 LeaveCriticalSection( &request->headers_section );
647 return ret;
650 static void HTTP_ProcessCookies( http_request_t *request )
652 int HeaderIndex;
653 int numCookies = 0;
654 LPHTTPHEADERW setCookieHeader;
656 if(request->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES)
657 return;
659 EnterCriticalSection( &request->headers_section );
661 while((HeaderIndex = HTTP_GetCustomHeaderIndex(request, L"Set-Cookie", numCookies++, FALSE)) != -1)
663 const WCHAR *data;
664 substr_t name;
666 setCookieHeader = &request->custHeaders[HeaderIndex];
668 if (!setCookieHeader->lpszValue)
669 continue;
671 data = wcschr(setCookieHeader->lpszValue, '=');
672 if(!data)
673 continue;
675 name = substr(setCookieHeader->lpszValue, data - setCookieHeader->lpszValue);
676 data++;
677 set_cookie(substrz(request->server->name), substrz(request->path), name, substrz(data), INTERNET_COOKIE_HTTPONLY);
680 LeaveCriticalSection( &request->headers_section );
683 static void strip_spaces(LPWSTR start)
685 LPWSTR str = start;
686 LPWSTR end;
688 while (*str == ' ')
689 str++;
691 if (str != start)
692 memmove(start, str, sizeof(WCHAR) * (lstrlenW(str) + 1));
694 end = start + lstrlenW(start) - 1;
695 while (end >= start && *end == ' ')
697 *end = '\0';
698 end--;
702 static inline BOOL is_basic_auth_value( LPCWSTR pszAuthValue, LPWSTR *pszRealm )
704 static const WCHAR szBasic[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
705 static const WCHAR szRealm[] = {'r','e','a','l','m'}; /* Note: not nul-terminated */
706 BOOL is_basic;
707 is_basic = !wcsnicmp(pszAuthValue, szBasic, ARRAY_SIZE(szBasic)) &&
708 ((pszAuthValue[ARRAY_SIZE(szBasic)] == ' ') || !pszAuthValue[ARRAY_SIZE(szBasic)]);
709 if (is_basic && pszRealm)
711 LPCWSTR token;
712 LPCWSTR ptr = &pszAuthValue[ARRAY_SIZE(szBasic)];
713 LPCWSTR realm;
714 ptr++;
715 *pszRealm=NULL;
716 token = wcschr(ptr,'=');
717 if (!token)
718 return TRUE;
719 realm = ptr;
720 while (*realm == ' ')
721 realm++;
722 if(!wcsnicmp(realm, szRealm, ARRAY_SIZE(szRealm)) &&
723 (realm[ARRAY_SIZE(szRealm)] == ' ' || realm[ARRAY_SIZE(szRealm)] == '='))
725 token++;
726 while (*token == ' ')
727 token++;
728 if (*token == '\0')
729 return TRUE;
730 *pszRealm = heap_strdupW(token);
731 strip_spaces(*pszRealm);
735 return is_basic;
738 static void destroy_authinfo( struct HttpAuthInfo *authinfo )
740 if (!authinfo) return;
742 if (SecIsValidHandle(&authinfo->ctx))
743 DeleteSecurityContext(&authinfo->ctx);
744 if (SecIsValidHandle(&authinfo->cred))
745 FreeCredentialsHandle(&authinfo->cred);
747 heap_free(authinfo->auth_data);
748 heap_free(authinfo->scheme);
749 heap_free(authinfo);
752 static UINT retrieve_cached_basic_authorization(http_request_t *req, const WCHAR *host, const WCHAR *realm, char **auth_data)
754 basicAuthorizationData *ad;
755 UINT rc = 0;
757 TRACE("Looking for authorization for %s:%s\n",debugstr_w(host),debugstr_w(realm));
759 EnterCriticalSection(&authcache_cs);
760 LIST_FOR_EACH_ENTRY(ad, &basicAuthorizationCache, basicAuthorizationData, entry)
762 if (!wcsicmp(host, ad->host) && (!realm || !wcscmp(realm, ad->realm)))
764 char *colon;
765 DWORD length;
767 TRACE("Authorization found in cache\n");
768 *auth_data = heap_alloc(ad->authorizationLen);
769 memcpy(*auth_data,ad->authorization,ad->authorizationLen);
770 rc = ad->authorizationLen;
772 /* update session username and password to reflect current credentials */
773 colon = strchr(ad->authorization, ':');
774 length = colon - ad->authorization;
776 heap_free(req->session->userName);
777 heap_free(req->session->password);
779 req->session->userName = heap_strndupAtoW(ad->authorization, length, &length);
780 length++;
781 req->session->password = heap_strndupAtoW(&ad->authorization[length], ad->authorizationLen - length, &length);
782 break;
785 LeaveCriticalSection(&authcache_cs);
786 return rc;
789 static void cache_basic_authorization(LPWSTR host, LPWSTR realm, LPSTR auth_data, UINT auth_data_len)
791 struct list *cursor;
792 basicAuthorizationData* ad = NULL;
794 TRACE("caching authorization for %s:%s = %s\n",debugstr_w(host),debugstr_w(realm),debugstr_an(auth_data,auth_data_len));
796 EnterCriticalSection(&authcache_cs);
797 LIST_FOR_EACH(cursor, &basicAuthorizationCache)
799 basicAuthorizationData *check = LIST_ENTRY(cursor,basicAuthorizationData,entry);
800 if (!wcsicmp(host,check->host) && !wcscmp(realm,check->realm))
802 ad = check;
803 break;
807 if (ad)
809 TRACE("Found match in cache, replacing\n");
810 heap_free(ad->authorization);
811 ad->authorization = heap_alloc(auth_data_len);
812 memcpy(ad->authorization, auth_data, auth_data_len);
813 ad->authorizationLen = auth_data_len;
815 else
817 ad = heap_alloc(sizeof(basicAuthorizationData));
818 ad->host = heap_strdupW(host);
819 ad->realm = heap_strdupW(realm);
820 ad->authorization = heap_alloc(auth_data_len);
821 memcpy(ad->authorization, auth_data, auth_data_len);
822 ad->authorizationLen = auth_data_len;
823 list_add_head(&basicAuthorizationCache,&ad->entry);
824 TRACE("authorization cached\n");
826 LeaveCriticalSection(&authcache_cs);
829 static BOOL retrieve_cached_authorization(LPWSTR host, LPWSTR scheme,
830 SEC_WINNT_AUTH_IDENTITY_W *nt_auth_identity)
832 authorizationData *ad;
834 TRACE("Looking for authorization for %s:%s\n", debugstr_w(host), debugstr_w(scheme));
836 EnterCriticalSection(&authcache_cs);
837 LIST_FOR_EACH_ENTRY(ad, &authorizationCache, authorizationData, entry) {
838 if(!wcsicmp(host, ad->host) && !wcsicmp(scheme, ad->scheme)) {
839 TRACE("Authorization found in cache\n");
841 nt_auth_identity->User = heap_strdupW(ad->user);
842 nt_auth_identity->Password = heap_strdupW(ad->password);
843 nt_auth_identity->Domain = heap_alloc(sizeof(WCHAR)*ad->domain_len);
844 if(!nt_auth_identity->User || !nt_auth_identity->Password ||
845 (!nt_auth_identity->Domain && ad->domain_len)) {
846 heap_free(nt_auth_identity->User);
847 heap_free(nt_auth_identity->Password);
848 heap_free(nt_auth_identity->Domain);
849 break;
852 nt_auth_identity->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
853 nt_auth_identity->UserLength = ad->user_len;
854 nt_auth_identity->PasswordLength = ad->password_len;
855 memcpy(nt_auth_identity->Domain, ad->domain, sizeof(WCHAR)*ad->domain_len);
856 nt_auth_identity->DomainLength = ad->domain_len;
857 LeaveCriticalSection(&authcache_cs);
858 return TRUE;
861 LeaveCriticalSection(&authcache_cs);
863 return FALSE;
866 static void cache_authorization(LPWSTR host, LPWSTR scheme,
867 SEC_WINNT_AUTH_IDENTITY_W *nt_auth_identity)
869 authorizationData *ad;
870 BOOL found = FALSE;
872 TRACE("Caching authorization for %s:%s\n", debugstr_w(host), debugstr_w(scheme));
874 EnterCriticalSection(&authcache_cs);
875 LIST_FOR_EACH_ENTRY(ad, &authorizationCache, authorizationData, entry)
876 if(!wcsicmp(host, ad->host) && !wcsicmp(scheme, ad->scheme)) {
877 found = TRUE;
878 break;
881 if(found) {
882 heap_free(ad->user);
883 heap_free(ad->password);
884 heap_free(ad->domain);
885 } else {
886 ad = heap_alloc(sizeof(authorizationData));
887 if(!ad) {
888 LeaveCriticalSection(&authcache_cs);
889 return;
892 ad->host = heap_strdupW(host);
893 ad->scheme = heap_strdupW(scheme);
894 list_add_head(&authorizationCache, &ad->entry);
897 ad->user = heap_strndupW(nt_auth_identity->User, nt_auth_identity->UserLength);
898 ad->password = heap_strndupW(nt_auth_identity->Password, nt_auth_identity->PasswordLength);
899 ad->domain = heap_strndupW(nt_auth_identity->Domain, nt_auth_identity->DomainLength);
900 ad->user_len = nt_auth_identity->UserLength;
901 ad->password_len = nt_auth_identity->PasswordLength;
902 ad->domain_len = nt_auth_identity->DomainLength;
904 if(!ad->host || !ad->scheme || !ad->user || !ad->password
905 || (nt_auth_identity->Domain && !ad->domain)) {
906 heap_free(ad->host);
907 heap_free(ad->scheme);
908 heap_free(ad->user);
909 heap_free(ad->password);
910 heap_free(ad->domain);
911 list_remove(&ad->entry);
912 heap_free(ad);
915 LeaveCriticalSection(&authcache_cs);
918 void free_authorization_cache(void)
920 authorizationData *ad, *sa_safe;
921 basicAuthorizationData *basic, *basic_safe;
923 EnterCriticalSection(&authcache_cs);
925 LIST_FOR_EACH_ENTRY_SAFE(basic, basic_safe, &basicAuthorizationCache, basicAuthorizationData, entry)
927 heap_free(basic->host);
928 heap_free(basic->realm);
929 heap_free(basic->authorization);
931 list_remove(&basic->entry);
932 heap_free(basic);
935 LIST_FOR_EACH_ENTRY_SAFE(ad, sa_safe, &authorizationCache, authorizationData, entry)
937 heap_free(ad->host);
938 heap_free(ad->scheme);
939 heap_free(ad->user);
940 heap_free(ad->password);
941 heap_free(ad->domain);
942 list_remove(&ad->entry);
943 heap_free(ad);
946 LeaveCriticalSection(&authcache_cs);
949 static BOOL HTTP_DoAuthorization( http_request_t *request, LPCWSTR pszAuthValue,
950 struct HttpAuthInfo **ppAuthInfo,
951 LPWSTR domain_and_username, LPWSTR password,
952 LPWSTR host )
954 SECURITY_STATUS sec_status;
955 struct HttpAuthInfo *pAuthInfo = *ppAuthInfo;
956 BOOL first = FALSE;
957 LPWSTR szRealm = NULL;
959 TRACE("%s\n", debugstr_w(pszAuthValue));
961 if (!pAuthInfo)
963 TimeStamp exp;
965 first = TRUE;
966 pAuthInfo = heap_alloc(sizeof(*pAuthInfo));
967 if (!pAuthInfo)
968 return FALSE;
970 SecInvalidateHandle(&pAuthInfo->cred);
971 SecInvalidateHandle(&pAuthInfo->ctx);
972 memset(&pAuthInfo->exp, 0, sizeof(pAuthInfo->exp));
973 pAuthInfo->attr = 0;
974 pAuthInfo->auth_data = NULL;
975 pAuthInfo->auth_data_len = 0;
976 pAuthInfo->finished = FALSE;
978 if (is_basic_auth_value(pszAuthValue,NULL))
980 pAuthInfo->scheme = heap_strdupW(L"Basic");
981 if (!pAuthInfo->scheme)
983 heap_free(pAuthInfo);
984 return FALSE;
987 else
989 PVOID pAuthData;
990 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity;
992 pAuthInfo->scheme = heap_strdupW(pszAuthValue);
993 if (!pAuthInfo->scheme)
995 heap_free(pAuthInfo);
996 return FALSE;
999 if (domain_and_username)
1001 WCHAR *user = wcschr(domain_and_username, '\\');
1002 WCHAR *domain = domain_and_username;
1004 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
1006 pAuthData = &nt_auth_identity;
1008 if (user) user++;
1009 else
1011 user = domain_and_username;
1012 domain = NULL;
1015 nt_auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
1016 nt_auth_identity.User = user;
1017 nt_auth_identity.UserLength = lstrlenW(nt_auth_identity.User);
1018 nt_auth_identity.Domain = domain;
1019 nt_auth_identity.DomainLength = domain ? user - domain - 1 : 0;
1020 nt_auth_identity.Password = password;
1021 nt_auth_identity.PasswordLength = lstrlenW(nt_auth_identity.Password);
1023 cache_authorization(host, pAuthInfo->scheme, &nt_auth_identity);
1025 else if(retrieve_cached_authorization(host, pAuthInfo->scheme, &nt_auth_identity))
1026 pAuthData = &nt_auth_identity;
1027 else
1028 /* use default credentials */
1029 pAuthData = NULL;
1031 sec_status = AcquireCredentialsHandleW(NULL, pAuthInfo->scheme,
1032 SECPKG_CRED_OUTBOUND, NULL,
1033 pAuthData, NULL,
1034 NULL, &pAuthInfo->cred,
1035 &exp);
1037 if(pAuthData && !domain_and_username) {
1038 heap_free(nt_auth_identity.User);
1039 heap_free(nt_auth_identity.Domain);
1040 heap_free(nt_auth_identity.Password);
1043 if (sec_status == SEC_E_OK)
1045 PSecPkgInfoW sec_pkg_info;
1046 sec_status = QuerySecurityPackageInfoW(pAuthInfo->scheme, &sec_pkg_info);
1047 if (sec_status == SEC_E_OK)
1049 pAuthInfo->max_token = sec_pkg_info->cbMaxToken;
1050 FreeContextBuffer(sec_pkg_info);
1053 if (sec_status != SEC_E_OK)
1055 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
1056 debugstr_w(pAuthInfo->scheme), sec_status);
1057 heap_free(pAuthInfo->scheme);
1058 heap_free(pAuthInfo);
1059 return FALSE;
1062 *ppAuthInfo = pAuthInfo;
1064 else if (pAuthInfo->finished)
1065 return FALSE;
1067 if ((lstrlenW(pszAuthValue) < lstrlenW(pAuthInfo->scheme)) ||
1068 wcsnicmp(pszAuthValue, pAuthInfo->scheme, lstrlenW(pAuthInfo->scheme)))
1070 ERR("authentication scheme changed from %s to %s\n",
1071 debugstr_w(pAuthInfo->scheme), debugstr_w(pszAuthValue));
1072 return FALSE;
1075 if (is_basic_auth_value(pszAuthValue,&szRealm))
1077 int userlen;
1078 int passlen;
1079 char *auth_data = NULL;
1080 UINT auth_data_len = 0;
1082 TRACE("basic authentication realm %s\n",debugstr_w(szRealm));
1084 if (!domain_and_username)
1086 if (host && szRealm)
1087 auth_data_len = retrieve_cached_basic_authorization(request, host, szRealm,&auth_data);
1088 if (auth_data_len == 0)
1090 heap_free(szRealm);
1091 return FALSE;
1094 else
1096 userlen = WideCharToMultiByte(CP_UTF8, 0, domain_and_username, lstrlenW(domain_and_username), NULL, 0, NULL, NULL);
1097 passlen = WideCharToMultiByte(CP_UTF8, 0, password, lstrlenW(password), NULL, 0, NULL, NULL);
1099 /* length includes a nul terminator, which will be re-used for the ':' */
1100 auth_data = heap_alloc(userlen + 1 + passlen);
1101 if (!auth_data)
1103 heap_free(szRealm);
1104 return FALSE;
1107 WideCharToMultiByte(CP_UTF8, 0, domain_and_username, -1, auth_data, userlen, NULL, NULL);
1108 auth_data[userlen] = ':';
1109 WideCharToMultiByte(CP_UTF8, 0, password, -1, &auth_data[userlen+1], passlen, NULL, NULL);
1110 auth_data_len = userlen + 1 + passlen;
1111 if (host && szRealm)
1112 cache_basic_authorization(host, szRealm, auth_data, auth_data_len);
1115 pAuthInfo->auth_data = auth_data;
1116 pAuthInfo->auth_data_len = auth_data_len;
1117 pAuthInfo->finished = TRUE;
1118 heap_free(szRealm);
1119 return TRUE;
1121 else
1123 LPCWSTR pszAuthData;
1124 SecBufferDesc out_desc, in_desc;
1125 SecBuffer out, in;
1126 unsigned char *buffer;
1127 ULONG context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE |
1128 ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE;
1130 in.BufferType = SECBUFFER_TOKEN;
1131 in.cbBuffer = 0;
1132 in.pvBuffer = NULL;
1134 in_desc.ulVersion = 0;
1135 in_desc.cBuffers = 1;
1136 in_desc.pBuffers = &in;
1138 pszAuthData = pszAuthValue + lstrlenW(pAuthInfo->scheme);
1139 if (*pszAuthData == ' ')
1141 pszAuthData++;
1142 in.cbBuffer = HTTP_DecodeBase64(pszAuthData, NULL);
1143 in.pvBuffer = heap_alloc(in.cbBuffer);
1144 HTTP_DecodeBase64(pszAuthData, in.pvBuffer);
1147 buffer = heap_alloc(pAuthInfo->max_token);
1149 out.BufferType = SECBUFFER_TOKEN;
1150 out.cbBuffer = pAuthInfo->max_token;
1151 out.pvBuffer = buffer;
1153 out_desc.ulVersion = 0;
1154 out_desc.cBuffers = 1;
1155 out_desc.pBuffers = &out;
1157 sec_status = InitializeSecurityContextW(first ? &pAuthInfo->cred : NULL,
1158 first ? NULL : &pAuthInfo->ctx,
1159 first ? request->server->name : NULL,
1160 context_req, 0, SECURITY_NETWORK_DREP,
1161 in.pvBuffer ? &in_desc : NULL,
1162 0, &pAuthInfo->ctx, &out_desc,
1163 &pAuthInfo->attr, &pAuthInfo->exp);
1164 if (sec_status == SEC_E_OK)
1166 pAuthInfo->finished = TRUE;
1167 pAuthInfo->auth_data = out.pvBuffer;
1168 pAuthInfo->auth_data_len = out.cbBuffer;
1169 TRACE("sending last auth packet\n");
1171 else if (sec_status == SEC_I_CONTINUE_NEEDED)
1173 pAuthInfo->auth_data = out.pvBuffer;
1174 pAuthInfo->auth_data_len = out.cbBuffer;
1175 TRACE("sending next auth packet\n");
1177 else
1179 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status);
1180 heap_free(out.pvBuffer);
1181 destroy_authinfo(pAuthInfo);
1182 *ppAuthInfo = NULL;
1183 return FALSE;
1187 return TRUE;
1190 /***********************************************************************
1191 * HTTP_HttpAddRequestHeadersW (internal)
1193 static DWORD HTTP_HttpAddRequestHeadersW(http_request_t *request,
1194 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1196 LPWSTR lpszStart;
1197 LPWSTR lpszEnd;
1198 LPWSTR buffer;
1199 DWORD len, res = ERROR_HTTP_INVALID_HEADER;
1201 TRACE("copying header: %s\n", debugstr_wn(lpszHeader, dwHeaderLength));
1203 if( dwHeaderLength == ~0U )
1204 len = lstrlenW(lpszHeader);
1205 else
1206 len = dwHeaderLength;
1207 buffer = heap_alloc(sizeof(WCHAR)*(len+1));
1208 lstrcpynW( buffer, lpszHeader, len + 1);
1210 lpszStart = buffer;
1214 LPWSTR * pFieldAndValue;
1216 lpszEnd = lpszStart;
1218 while (*lpszEnd != '\0')
1220 if (*lpszEnd == '\r' || *lpszEnd == '\n')
1221 break;
1222 lpszEnd++;
1225 if (*lpszStart == '\0')
1226 break;
1228 if (*lpszEnd == '\r' || *lpszEnd == '\n')
1230 *lpszEnd = '\0';
1231 lpszEnd++; /* Jump over newline */
1233 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
1234 if (*lpszStart == '\0')
1236 /* Skip 0-length headers */
1237 lpszStart = lpszEnd;
1238 res = ERROR_SUCCESS;
1239 continue;
1241 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
1242 if (pFieldAndValue)
1244 res = HTTP_ProcessHeader(request, pFieldAndValue[0],
1245 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
1246 HTTP_FreeTokens(pFieldAndValue);
1249 lpszStart = lpszEnd;
1250 } while (res == ERROR_SUCCESS);
1252 heap_free(buffer);
1253 return res;
1256 /***********************************************************************
1257 * HttpAddRequestHeadersW (WININET.@)
1259 * Adds one or more HTTP header to the request handler
1261 * NOTE
1262 * On Windows if dwHeaderLength includes the trailing '\0', then
1263 * HttpAddRequestHeadersW() adds it too. However this results in an
1264 * invalid HTTP header which is rejected by some servers so we probably
1265 * don't need to match Windows on that point.
1267 * RETURNS
1268 * TRUE on success
1269 * FALSE on failure
1272 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
1273 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1275 http_request_t *request;
1276 DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
1278 TRACE("%p, %s, %u, %08x\n", hHttpRequest, debugstr_wn(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
1280 if (!lpszHeader)
1281 return TRUE;
1283 request = (http_request_t*) get_handle_object( hHttpRequest );
1284 if (request && request->hdr.htype == WH_HHTTPREQ)
1285 res = HTTP_HttpAddRequestHeadersW( request, lpszHeader, dwHeaderLength, dwModifier );
1286 if( request )
1287 WININET_Release( &request->hdr );
1289 if(res != ERROR_SUCCESS)
1290 SetLastError(res);
1291 return res == ERROR_SUCCESS;
1294 /***********************************************************************
1295 * HttpAddRequestHeadersA (WININET.@)
1297 * Adds one or more HTTP header to the request handler
1299 * RETURNS
1300 * TRUE on success
1301 * FALSE on failure
1304 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
1305 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
1307 WCHAR *headers = NULL;
1308 BOOL r;
1310 TRACE("%p, %s, %u, %08x\n", hHttpRequest, debugstr_an(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
1312 if(lpszHeader)
1313 headers = heap_strndupAtoW(lpszHeader, dwHeaderLength, &dwHeaderLength);
1315 r = HttpAddRequestHeadersW(hHttpRequest, headers, dwHeaderLength, dwModifier);
1317 heap_free(headers);
1318 return r;
1321 static void free_accept_types( WCHAR **accept_types )
1323 WCHAR *ptr, **types = accept_types;
1325 if (!types) return;
1326 while ((ptr = *types))
1328 heap_free( ptr );
1329 types++;
1331 heap_free( accept_types );
1334 static WCHAR **convert_accept_types( const char **accept_types )
1336 unsigned int count;
1337 const char **types = accept_types;
1338 WCHAR **typesW;
1339 BOOL invalid_pointer = FALSE;
1341 if (!types) return NULL;
1342 count = 0;
1343 while (*types)
1345 __TRY
1347 /* find out how many there are */
1348 if (*types && **types)
1350 TRACE("accept type: %s\n", debugstr_a(*types));
1351 count++;
1354 __EXCEPT_PAGE_FAULT
1356 WARN("invalid accept type pointer\n");
1357 invalid_pointer = TRUE;
1359 __ENDTRY;
1360 types++;
1362 if (invalid_pointer) return NULL;
1363 if (!(typesW = heap_alloc( sizeof(WCHAR *) * (count + 1) ))) return NULL;
1364 count = 0;
1365 types = accept_types;
1366 while (*types)
1368 if (*types && **types) typesW[count++] = heap_strdupAtoW( *types );
1369 types++;
1371 typesW[count] = NULL;
1372 return typesW;
1375 /***********************************************************************
1376 * HttpOpenRequestA (WININET.@)
1378 * Open a HTTP request handle
1380 * RETURNS
1381 * HINTERNET a HTTP request handle on success
1382 * NULL on failure
1385 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
1386 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
1387 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
1388 DWORD dwFlags, DWORD_PTR dwContext)
1390 LPWSTR szVerb = NULL, szObjectName = NULL;
1391 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
1392 HINTERNET rc = NULL;
1394 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
1395 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
1396 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
1397 dwFlags, dwContext);
1399 if (lpszVerb)
1401 szVerb = heap_strdupAtoW(lpszVerb);
1402 if ( !szVerb )
1403 goto end;
1406 if (lpszObjectName)
1408 szObjectName = heap_strdupAtoW(lpszObjectName);
1409 if ( !szObjectName )
1410 goto end;
1413 if (lpszVersion)
1415 szVersion = heap_strdupAtoW(lpszVersion);
1416 if ( !szVersion )
1417 goto end;
1420 if (lpszReferrer)
1422 szReferrer = heap_strdupAtoW(lpszReferrer);
1423 if ( !szReferrer )
1424 goto end;
1427 szAcceptTypes = convert_accept_types( lpszAcceptTypes );
1428 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName, szVersion, szReferrer,
1429 (const WCHAR **)szAcceptTypes, dwFlags, dwContext);
1431 end:
1432 free_accept_types(szAcceptTypes);
1433 heap_free(szReferrer);
1434 heap_free(szVersion);
1435 heap_free(szObjectName);
1436 heap_free(szVerb);
1437 return rc;
1440 /***********************************************************************
1441 * HTTP_EncodeBase64
1443 static UINT HTTP_EncodeBase64( LPCSTR bin, unsigned int len, LPWSTR base64 )
1445 UINT n = 0, x;
1446 static const CHAR HTTP_Base64Enc[] =
1447 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1449 while( len > 0 )
1451 /* first 6 bits, all from bin[0] */
1452 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
1453 x = (bin[0] & 3) << 4;
1455 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1456 if( len == 1 )
1458 base64[n++] = HTTP_Base64Enc[x];
1459 base64[n++] = '=';
1460 base64[n++] = '=';
1461 break;
1463 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
1464 x = ( bin[1] & 0x0f ) << 2;
1466 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1467 if( len == 2 )
1469 base64[n++] = HTTP_Base64Enc[x];
1470 base64[n++] = '=';
1471 break;
1473 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
1475 /* last 6 bits, all from bin [2] */
1476 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
1477 bin += 3;
1478 len -= 3;
1480 base64[n] = 0;
1481 return n;
1484 static const signed char HTTP_Base64Dec[] =
1486 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00 */
1487 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10 */
1488 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20 */
1489 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 0x30 */
1490 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40 */
1491 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50 */
1492 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60 */
1493 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 0x70 */
1496 /***********************************************************************
1497 * HTTP_DecodeBase64
1499 static UINT HTTP_DecodeBase64( LPCWSTR base64, LPSTR bin )
1501 unsigned int n = 0;
1503 while(*base64)
1505 signed char in[4];
1507 if (base64[0] >= ARRAY_SIZE(HTTP_Base64Dec) ||
1508 ((in[0] = HTTP_Base64Dec[base64[0]]) == -1) ||
1509 base64[1] >= ARRAY_SIZE(HTTP_Base64Dec) ||
1510 ((in[1] = HTTP_Base64Dec[base64[1]]) == -1))
1512 WARN("invalid base64: %s\n", debugstr_w(base64));
1513 return 0;
1515 if (bin)
1516 bin[n] = (unsigned char) (in[0] << 2 | in[1] >> 4);
1517 n++;
1519 if ((base64[2] == '=') && (base64[3] == '='))
1520 break;
1521 if (base64[2] > ARRAY_SIZE(HTTP_Base64Dec) ||
1522 ((in[2] = HTTP_Base64Dec[base64[2]]) == -1))
1524 WARN("invalid base64: %s\n", debugstr_w(&base64[2]));
1525 return 0;
1527 if (bin)
1528 bin[n] = (unsigned char) (in[1] << 4 | in[2] >> 2);
1529 n++;
1531 if (base64[3] == '=')
1532 break;
1533 if (base64[3] > ARRAY_SIZE(HTTP_Base64Dec) ||
1534 ((in[3] = HTTP_Base64Dec[base64[3]]) == -1))
1536 WARN("invalid base64: %s\n", debugstr_w(&base64[3]));
1537 return 0;
1539 if (bin)
1540 bin[n] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
1541 n++;
1543 base64 += 4;
1546 return n;
1549 static WCHAR *encode_auth_data( const WCHAR *scheme, const char *data, UINT data_len )
1551 WCHAR *ret;
1552 UINT len, scheme_len = lstrlenW( scheme );
1554 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1555 len = scheme_len + 1 + ((data_len + 2) * 4) / 3;
1556 if (!(ret = heap_alloc( (len + 1) * sizeof(WCHAR) ))) return NULL;
1557 memcpy( ret, scheme, scheme_len * sizeof(WCHAR) );
1558 ret[scheme_len] = ' ';
1559 HTTP_EncodeBase64( data, data_len, ret + scheme_len + 1 );
1560 return ret;
1564 /***********************************************************************
1565 * HTTP_InsertAuthorization
1567 * Insert or delete the authorization field in the request header.
1569 static BOOL HTTP_InsertAuthorization( http_request_t *request, struct HttpAuthInfo *pAuthInfo, LPCWSTR header )
1571 WCHAR *host, *authorization = NULL;
1573 if (pAuthInfo)
1575 if (pAuthInfo->auth_data_len)
1577 if (!(authorization = encode_auth_data(pAuthInfo->scheme, pAuthInfo->auth_data, pAuthInfo->auth_data_len)))
1578 return FALSE;
1580 /* clear the data as it isn't valid now that it has been sent to the
1581 * server, unless it's Basic authentication which doesn't do
1582 * connection tracking */
1583 if (wcsicmp(pAuthInfo->scheme, L"Basic"))
1585 heap_free(pAuthInfo->auth_data);
1586 pAuthInfo->auth_data = NULL;
1587 pAuthInfo->auth_data_len = 0;
1591 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1593 HTTP_ProcessHeader(request, header, authorization,
1594 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
1595 heap_free(authorization);
1597 else
1599 UINT data_len;
1600 char *data;
1602 /* Don't use cached credentials when a username or Authorization was specified */
1603 if ((request->session->userName && request->session->userName[0]) || wcscmp(header, L"Authorization"))
1604 return TRUE;
1606 if (!(host = get_host_header(request)))
1607 return TRUE;
1609 if ((data_len = retrieve_cached_basic_authorization(request, host, NULL, &data)))
1611 TRACE("Found cached basic authorization for %s\n", debugstr_w(host));
1613 if (!(authorization = encode_auth_data(L"Basic", data, data_len)))
1615 heap_free(data);
1616 heap_free(host);
1617 return FALSE;
1620 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1622 HTTP_ProcessHeader(request, header, authorization,
1623 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_ADD);
1624 heap_free(data);
1625 heap_free(authorization);
1627 heap_free(host);
1629 return TRUE;
1632 static WCHAR *build_proxy_path_url(http_request_t *req)
1634 DWORD size, len;
1635 WCHAR *url;
1637 len = lstrlenW(req->server->scheme_host_port);
1638 size = len + lstrlenW(req->path) + 1;
1639 if(*req->path != '/')
1640 size++;
1641 url = heap_alloc(size * sizeof(WCHAR));
1642 if(!url)
1643 return NULL;
1645 memcpy(url, req->server->scheme_host_port, len*sizeof(WCHAR));
1646 if(*req->path != '/')
1647 url[len++] = '/';
1649 lstrcpyW(url+len, req->path);
1651 TRACE("url=%s\n", debugstr_w(url));
1652 return url;
1655 static BOOL HTTP_DomainMatches(LPCWSTR server, substr_t domain)
1657 const WCHAR *dot, *ptr;
1658 int len;
1660 if(domain.len == ARRAY_SIZE(L"<local>")-1 && !wcsnicmp(domain.str, L"<local>", domain.len) && !wcschr(server, '.' ))
1661 return TRUE;
1663 if(domain.len && *domain.str != '*')
1664 return domain.len == lstrlenW(server) && !wcsnicmp(server, domain.str, domain.len);
1666 if(domain.len < 2 || domain.str[1] != '.')
1667 return FALSE;
1669 /* For a hostname to match a wildcard, the last domain must match
1670 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
1671 * hostname is www.foo.a.b, it matches, but a.b does not.
1673 dot = wcschr(server, '.');
1674 if(!dot)
1675 return FALSE;
1677 len = lstrlenW(dot + 1);
1678 if(len < domain.len - 2)
1679 return FALSE;
1681 /* The server's domain is longer than the wildcard, so it
1682 * could be a subdomain. Compare the last portion of the
1683 * server's domain.
1685 ptr = dot + 1 + len - domain.len + 2;
1686 if(!wcsnicmp(ptr, domain.str+2, domain.len-2))
1687 /* This is only a match if the preceding character is
1688 * a '.', i.e. that it is a matching domain. E.g.
1689 * if domain is '*.b.c' and server is 'www.ab.c' they
1690 * do not match.
1692 return *(ptr - 1) == '.';
1694 return len == domain.len-2 && !wcsnicmp(dot + 1, domain.str + 2, len);
1697 static BOOL HTTP_ShouldBypassProxy(appinfo_t *lpwai, LPCWSTR server)
1699 LPCWSTR ptr;
1700 BOOL ret = FALSE;
1702 if (!lpwai->proxyBypass) return FALSE;
1703 ptr = lpwai->proxyBypass;
1704 while(1) {
1705 LPCWSTR tmp = ptr;
1707 ptr = wcschr( ptr, ';' );
1708 if (!ptr)
1709 ptr = wcschr( tmp, ' ' );
1710 if (!ptr)
1711 ptr = tmp + lstrlenW(tmp);
1712 ret = HTTP_DomainMatches( server, substr(tmp, ptr-tmp) );
1713 if (ret || !*ptr)
1714 break;
1715 ptr++;
1717 return ret;
1720 /***********************************************************************
1721 * HTTP_DealWithProxy
1723 static BOOL HTTP_DealWithProxy(appinfo_t *hIC, http_session_t *session, http_request_t *request)
1725 static WCHAR szNul[] = L"";
1726 URL_COMPONENTSW UrlComponents = { sizeof(UrlComponents) };
1727 server_t *new_server = NULL;
1728 WCHAR *proxy;
1730 proxy = INTERNET_FindProxyForProtocol(hIC->proxy, L"http");
1731 if(!proxy)
1732 return FALSE;
1733 if(CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1734 proxy, lstrlenW(L"http://"), L"http://", lstrlenW(L"http://"))) {
1735 WCHAR *proxy_url = heap_alloc(lstrlenW(proxy)*sizeof(WCHAR) + sizeof(L"http://"));
1736 if(!proxy_url) {
1737 heap_free(proxy);
1738 return FALSE;
1740 lstrcpyW(proxy_url, L"http://");
1741 lstrcatW(proxy_url, proxy);
1742 heap_free(proxy);
1743 proxy = proxy_url;
1746 UrlComponents.dwHostNameLength = 1;
1747 if(InternetCrackUrlW(proxy, 0, 0, &UrlComponents) && UrlComponents.dwHostNameLength) {
1748 if( !request->path )
1749 request->path = szNul;
1751 new_server = get_server(substr(UrlComponents.lpszHostName, UrlComponents.dwHostNameLength),
1752 UrlComponents.nPort, UrlComponents.nScheme == INTERNET_SCHEME_HTTPS, TRUE);
1754 heap_free(proxy);
1755 if(!new_server)
1756 return FALSE;
1758 request->proxy = new_server;
1760 TRACE("proxy server=%s port=%d\n", debugstr_w(new_server->name), new_server->port);
1761 return TRUE;
1764 static DWORD HTTP_ResolveName(http_request_t *request)
1766 server_t *server = request->proxy ? request->proxy : request->server;
1767 int addr_len;
1769 if(server->addr_len)
1770 return ERROR_SUCCESS;
1772 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
1773 INTERNET_STATUS_RESOLVING_NAME,
1774 server->name,
1775 (lstrlenW(server->name)+1) * sizeof(WCHAR));
1777 addr_len = sizeof(server->addr);
1778 if (!GetAddress(server->name, server->port, (SOCKADDR*)&server->addr, &addr_len, server->addr_str))
1779 return ERROR_INTERNET_NAME_NOT_RESOLVED;
1781 server->addr_len = addr_len;
1782 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
1783 INTERNET_STATUS_NAME_RESOLVED,
1784 server->addr_str, strlen(server->addr_str)+1);
1786 TRACE("resolved %s to %s\n", debugstr_w(server->name), server->addr_str);
1787 return ERROR_SUCCESS;
1790 static WCHAR *compose_request_url(http_request_t *req)
1792 const WCHAR *host, *scheme;
1793 WCHAR *buf, *ptr;
1794 size_t len;
1796 host = req->server->canon_host_port;
1798 if (req->server->is_https)
1799 scheme = L"https://";
1800 else
1801 scheme = L"http://";
1803 len = lstrlenW(scheme) + lstrlenW(host) + (req->path[0] != '/' ? 1 : 0) + lstrlenW(req->path);
1804 ptr = buf = heap_alloc((len+1) * sizeof(WCHAR));
1805 if(buf) {
1806 lstrcpyW(ptr, scheme);
1807 ptr += lstrlenW(ptr);
1809 lstrcpyW(ptr, host);
1810 ptr += lstrlenW(ptr);
1812 if(req->path[0] != '/')
1813 *ptr++ = '/';
1815 lstrcpyW(ptr, req->path);
1816 ptr += lstrlenW(ptr);
1817 *ptr = 0;
1820 return buf;
1824 /***********************************************************************
1825 * HTTPREQ_Destroy (internal)
1827 * Deallocate request handle
1830 static void HTTPREQ_Destroy(object_header_t *hdr)
1832 http_request_t *request = (http_request_t*) hdr;
1833 DWORD i;
1835 TRACE("\n");
1837 if(request->hCacheFile)
1838 CloseHandle(request->hCacheFile);
1839 if(request->req_file)
1840 req_file_release(request->req_file);
1842 request->headers_section.DebugInfo->Spare[0] = 0;
1843 DeleteCriticalSection( &request->headers_section );
1844 request->read_section.DebugInfo->Spare[0] = 0;
1845 DeleteCriticalSection( &request->read_section );
1846 WININET_Release(&request->session->hdr);
1848 destroy_authinfo(request->authInfo);
1849 destroy_authinfo(request->proxyAuthInfo);
1851 if(request->server)
1852 server_release(request->server);
1853 if(request->proxy)
1854 server_release(request->proxy);
1856 heap_free(request->path);
1857 heap_free(request->verb);
1858 heap_free(request->version);
1859 heap_free(request->statusText);
1861 for (i = 0; i < request->nCustHeaders; i++)
1863 heap_free(request->custHeaders[i].lpszField);
1864 heap_free(request->custHeaders[i].lpszValue);
1866 destroy_data_stream(request->data_stream);
1867 heap_free(request->custHeaders);
1870 static void http_release_netconn(http_request_t *req, BOOL reuse)
1872 TRACE("%p %p %x\n",req, req->netconn, reuse);
1874 if(!is_valid_netconn(req->netconn))
1875 return;
1877 if(reuse && req->netconn->keep_alive) {
1878 BOOL run_collector;
1880 EnterCriticalSection(&connection_pool_cs);
1882 list_add_head(&req->netconn->server->conn_pool, &req->netconn->pool_entry);
1883 req->netconn->keep_until = GetTickCount64() + COLLECT_TIME;
1884 req->netconn = NULL;
1886 run_collector = !collector_running;
1887 collector_running = TRUE;
1889 LeaveCriticalSection(&connection_pool_cs);
1891 if(run_collector) {
1892 HANDLE thread = NULL;
1893 HMODULE module;
1895 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (const WCHAR*)WININET_hModule, &module);
1896 if(module)
1897 thread = CreateThread(NULL, 0, collect_connections_proc, NULL, 0, NULL);
1898 if(!thread) {
1899 EnterCriticalSection(&connection_pool_cs);
1900 collector_running = FALSE;
1901 LeaveCriticalSection(&connection_pool_cs);
1903 if(module)
1904 FreeLibrary(module);
1906 else
1907 CloseHandle(thread);
1909 return;
1912 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
1913 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
1915 close_netconn(req->netconn);
1917 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
1918 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
1921 static BOOL HTTP_KeepAlive(http_request_t *request)
1923 WCHAR szVersion[10];
1924 WCHAR szConnectionResponse[20];
1925 DWORD dwBufferSize = sizeof(szVersion);
1926 BOOL keepalive = FALSE;
1928 /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
1929 * the connection is keep-alive by default */
1930 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_VERSION, szVersion, &dwBufferSize, NULL) == ERROR_SUCCESS
1931 && !wcsicmp(szVersion, L"HTTP/1.1"))
1933 keepalive = TRUE;
1936 dwBufferSize = sizeof(szConnectionResponse);
1937 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_PROXY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) == ERROR_SUCCESS
1938 || HTTP_HttpQueryInfoW(request, HTTP_QUERY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) == ERROR_SUCCESS)
1940 keepalive = !wcsicmp(szConnectionResponse, L"Keep-Alive");
1943 return keepalive;
1946 static void HTTPREQ_CloseConnection(object_header_t *hdr)
1948 http_request_t *req = (http_request_t*)hdr;
1950 http_release_netconn(req, drain_content(req, FALSE) == ERROR_SUCCESS);
1953 static DWORD str_to_buffer(const WCHAR *str, void *buffer, DWORD *size, BOOL unicode)
1955 int len;
1956 if (unicode)
1958 WCHAR *buf = buffer;
1960 if (str) len = lstrlenW(str);
1961 else len = 0;
1962 if (*size < (len + 1) * sizeof(WCHAR))
1964 *size = (len + 1) * sizeof(WCHAR);
1965 return ERROR_INSUFFICIENT_BUFFER;
1967 if (str) lstrcpyW(buf, str);
1968 else buf[0] = 0;
1970 *size = len;
1971 return ERROR_SUCCESS;
1973 else
1975 char *buf = buffer;
1977 if (str) len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
1978 else len = 1;
1979 if (*size < len)
1981 *size = len;
1982 return ERROR_INSUFFICIENT_BUFFER;
1984 if (str) WideCharToMultiByte(CP_ACP, 0, str, -1, buf, *size, NULL, NULL);
1985 else buf[0] = 0;
1987 *size = len - 1;
1988 return ERROR_SUCCESS;
1992 static DWORD get_security_cert_struct(http_request_t *req, INTERNET_CERTIFICATE_INFOA *info)
1994 PCCERT_CONTEXT context;
1995 DWORD len;
1997 context = (PCCERT_CONTEXT)NETCON_GetCert(req->netconn);
1998 if(!context)
1999 return ERROR_NOT_SUPPORTED;
2001 memset(info, 0, sizeof(*info));
2002 info->ftExpiry = context->pCertInfo->NotAfter;
2003 info->ftStart = context->pCertInfo->NotBefore;
2004 len = CertNameToStrA(context->dwCertEncodingType,
2005 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG, NULL, 0);
2006 info->lpszSubjectInfo = LocalAlloc(0, len);
2007 if(info->lpszSubjectInfo)
2008 CertNameToStrA(context->dwCertEncodingType,
2009 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG,
2010 info->lpszSubjectInfo, len);
2011 len = CertNameToStrA(context->dwCertEncodingType,
2012 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG, NULL, 0);
2013 info->lpszIssuerInfo = LocalAlloc(0, len);
2014 if(info->lpszIssuerInfo)
2015 CertNameToStrA(context->dwCertEncodingType,
2016 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG,
2017 info->lpszIssuerInfo, len);
2018 info->dwKeySize = NETCON_GetCipherStrength(req->netconn);
2020 CertFreeCertificateContext(context);
2021 return ERROR_SUCCESS;
2024 static DWORD HTTPREQ_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2026 http_request_t *req = (http_request_t*)hdr;
2028 switch(option) {
2029 case INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO:
2031 INTERNET_DIAGNOSTIC_SOCKET_INFO *info = buffer;
2033 FIXME("INTERNET_DIAGNOSTIC_SOCKET_INFO stub\n");
2035 if (*size < sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO))
2036 return ERROR_INSUFFICIENT_BUFFER;
2037 *size = sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO);
2038 /* FIXME: can't get a SOCKET from our connection since we don't use
2039 * winsock
2041 info->Socket = 0;
2042 /* FIXME: get source port from req->netConnection */
2043 info->SourcePort = 0;
2044 info->DestPort = req->server->port;
2045 info->Flags = 0;
2046 if (HTTP_KeepAlive(req))
2047 info->Flags |= IDSI_FLAG_KEEP_ALIVE;
2048 if (req->proxy)
2049 info->Flags |= IDSI_FLAG_PROXY;
2050 if (is_valid_netconn(req->netconn) && req->netconn->secure)
2051 info->Flags |= IDSI_FLAG_SECURE;
2053 return ERROR_SUCCESS;
2056 case 98:
2057 TRACE("Queried undocumented option 98, forwarding to INTERNET_OPTION_SECURITY_FLAGS\n");
2058 /* fall through */
2059 case INTERNET_OPTION_SECURITY_FLAGS:
2061 DWORD flags;
2063 if (*size < sizeof(ULONG))
2064 return ERROR_INSUFFICIENT_BUFFER;
2066 *size = sizeof(DWORD);
2067 flags = is_valid_netconn(req->netconn) ? req->netconn->security_flags : req->security_flags | req->server->security_flags;
2068 *(DWORD *)buffer = flags;
2070 TRACE("INTERNET_OPTION_SECURITY_FLAGS %x\n", flags);
2071 return ERROR_SUCCESS;
2074 case INTERNET_OPTION_HANDLE_TYPE:
2075 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
2077 if (*size < sizeof(ULONG))
2078 return ERROR_INSUFFICIENT_BUFFER;
2080 *size = sizeof(DWORD);
2081 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_HTTP_REQUEST;
2082 return ERROR_SUCCESS;
2084 case INTERNET_OPTION_URL: {
2085 WCHAR *url;
2086 DWORD res;
2088 TRACE("INTERNET_OPTION_URL\n");
2090 url = compose_request_url(req);
2091 if(!url)
2092 return ERROR_OUTOFMEMORY;
2094 res = str_to_buffer(url, buffer, size, unicode);
2095 heap_free(url);
2096 return res;
2098 case INTERNET_OPTION_USER_AGENT:
2099 return str_to_buffer(req->session->appInfo->agent, buffer, size, unicode);
2100 case INTERNET_OPTION_USERNAME:
2101 return str_to_buffer(req->session->userName, buffer, size, unicode);
2102 case INTERNET_OPTION_PASSWORD:
2103 return str_to_buffer(req->session->password, buffer, size, unicode);
2104 case INTERNET_OPTION_PROXY_USERNAME:
2105 return str_to_buffer(req->session->appInfo->proxyUsername, buffer, size, unicode);
2106 case INTERNET_OPTION_PROXY_PASSWORD:
2107 return str_to_buffer(req->session->appInfo->proxyPassword, buffer, size, unicode);
2109 case INTERNET_OPTION_CACHE_TIMESTAMPS: {
2110 INTERNET_CACHE_ENTRY_INFOW *info;
2111 INTERNET_CACHE_TIMESTAMPS *ts = buffer;
2112 DWORD nbytes, error;
2113 BOOL ret;
2115 TRACE("INTERNET_OPTION_CACHE_TIMESTAMPS\n");
2117 if(!req->req_file)
2118 return ERROR_FILE_NOT_FOUND;
2120 if (*size < sizeof(*ts))
2122 *size = sizeof(*ts);
2123 return ERROR_INSUFFICIENT_BUFFER;
2126 nbytes = 0;
2127 ret = GetUrlCacheEntryInfoW(req->req_file->url, NULL, &nbytes);
2128 error = GetLastError();
2129 if (!ret && error == ERROR_INSUFFICIENT_BUFFER)
2131 if (!(info = heap_alloc(nbytes)))
2132 return ERROR_OUTOFMEMORY;
2134 GetUrlCacheEntryInfoW(req->req_file->url, info, &nbytes);
2136 ts->ftExpires = info->ExpireTime;
2137 ts->ftLastModified = info->LastModifiedTime;
2139 heap_free(info);
2140 *size = sizeof(*ts);
2141 return ERROR_SUCCESS;
2143 return error;
2146 case INTERNET_OPTION_DATAFILE_NAME: {
2147 DWORD req_size;
2149 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
2151 if(!req->req_file) {
2152 *size = 0;
2153 return ERROR_INTERNET_ITEM_NOT_FOUND;
2156 if(unicode) {
2157 req_size = (lstrlenW(req->req_file->file_name)+1) * sizeof(WCHAR);
2158 if(*size < req_size)
2159 return ERROR_INSUFFICIENT_BUFFER;
2161 *size = req_size;
2162 memcpy(buffer, req->req_file->file_name, *size);
2163 return ERROR_SUCCESS;
2164 }else {
2165 req_size = WideCharToMultiByte(CP_ACP, 0, req->req_file->file_name, -1, NULL, 0, NULL, NULL);
2166 if (req_size > *size)
2167 return ERROR_INSUFFICIENT_BUFFER;
2169 *size = WideCharToMultiByte(CP_ACP, 0, req->req_file->file_name,
2170 -1, buffer, *size, NULL, NULL);
2171 return ERROR_SUCCESS;
2175 case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT: {
2176 if(!req->netconn)
2177 return ERROR_INTERNET_INVALID_OPERATION;
2179 if(*size < sizeof(INTERNET_CERTIFICATE_INFOA)) {
2180 *size = sizeof(INTERNET_CERTIFICATE_INFOA);
2181 return ERROR_INSUFFICIENT_BUFFER;
2184 return get_security_cert_struct(req, (INTERNET_CERTIFICATE_INFOA*)buffer);
2186 case INTERNET_OPTION_SECURITY_CERTIFICATE: {
2187 DWORD err;
2188 int needed;
2189 char subject[64];
2190 char issuer[64];
2191 char effective[64];
2192 char expiration[64];
2193 char protocol[64];
2194 char signature[64];
2195 char encryption[64];
2196 char privacy[64];
2197 char bits[16];
2198 char strength[16];
2199 char start_date[32];
2200 char start_time[32];
2201 char expiry_date[32];
2202 char expiry_time[32];
2203 SYSTEMTIME start, expiry;
2204 INTERNET_CERTIFICATE_INFOA info;
2206 if(!size)
2207 return ERROR_INVALID_PARAMETER;
2209 if(!req->netconn) {
2210 *size = 0;
2211 return ERROR_INTERNET_INVALID_OPERATION;
2214 if(!buffer) {
2215 *size = 1;
2216 return ERROR_INSUFFICIENT_BUFFER;
2219 if((err = get_security_cert_struct(req, &info)))
2220 return err;
2222 LoadStringA(WININET_hModule, IDS_CERT_SUBJECT, subject, sizeof(subject));
2223 LoadStringA(WININET_hModule, IDS_CERT_ISSUER, issuer, sizeof(issuer));
2224 LoadStringA(WININET_hModule, IDS_CERT_EFFECTIVE, effective, sizeof(effective));
2225 LoadStringA(WININET_hModule, IDS_CERT_EXPIRATION, expiration, sizeof(expiration));
2226 LoadStringA(WININET_hModule, IDS_CERT_PROTOCOL, protocol, sizeof(protocol));
2227 LoadStringA(WININET_hModule, IDS_CERT_SIGNATURE, signature, sizeof(signature));
2228 LoadStringA(WININET_hModule, IDS_CERT_ENCRYPTION, encryption, sizeof(encryption));
2229 LoadStringA(WININET_hModule, IDS_CERT_PRIVACY, privacy, sizeof(privacy));
2230 LoadStringA(WININET_hModule, info.dwKeySize >= 128 ? IDS_CERT_HIGH : IDS_CERT_LOW,
2231 strength, sizeof(strength));
2232 LoadStringA(WININET_hModule, IDS_CERT_BITS, bits, sizeof(bits));
2234 FileTimeToSystemTime(&info.ftStart, &start);
2235 FileTimeToSystemTime(&info.ftExpiry, &expiry);
2236 GetDateFormatA(LOCALE_USER_DEFAULT, 0, &start, NULL, start_date, sizeof(start_date));
2237 GetTimeFormatA(LOCALE_USER_DEFAULT, 0, &start, NULL, start_time, sizeof(start_time));
2238 GetDateFormatA(LOCALE_USER_DEFAULT, 0, &expiry, NULL, expiry_date, sizeof(expiry_date));
2239 GetTimeFormatA(LOCALE_USER_DEFAULT, 0, &expiry, NULL, expiry_time, sizeof(expiry_time));
2241 needed = _scprintf("%s:\r\n%s\r\n"
2242 "%s:\r\n%s\r\n"
2243 "%s:\t%s %s\r\n"
2244 "%s:\t%s %s\r\n"
2245 "%s:\t(null)\r\n"
2246 "%s:\t(null)\r\n"
2247 "%s:\t(null)\r\n"
2248 "%s:\t%s (%u %s)",
2249 subject, info.lpszSubjectInfo,
2250 issuer, info.lpszIssuerInfo,
2251 effective, start_date, start_time,
2252 expiration, expiry_date, expiry_time,
2253 protocol, signature, encryption,
2254 privacy, strength, info.dwKeySize, bits);
2256 if(needed < *size) {
2257 err = ERROR_SUCCESS;
2258 *size = snprintf(buffer, *size,
2259 "%s:\r\n%s\r\n"
2260 "%s:\r\n%s\r\n"
2261 "%s:\t%s %s\r\n"
2262 "%s:\t%s %s\r\n"
2263 "%s:\t(null)\r\n"
2264 "%s:\t(null)\r\n"
2265 "%s:\t(null)\r\n"
2266 "%s:\t%s (%u %s)",
2267 subject, info.lpszSubjectInfo,
2268 issuer, info.lpszIssuerInfo,
2269 effective, start_date, start_time,
2270 expiration, expiry_date, expiry_time,
2271 protocol, signature, encryption,
2272 privacy, strength, info.dwKeySize, bits);
2273 }else {
2274 err = ERROR_INSUFFICIENT_BUFFER;
2275 *size = 1;
2278 LocalFree(info.lpszSubjectInfo);
2279 LocalFree(info.lpszIssuerInfo);
2280 LocalFree(info.lpszProtocolName);
2281 LocalFree(info.lpszSignatureAlgName);
2282 LocalFree(info.lpszEncryptionAlgName);
2283 return err;
2285 case INTERNET_OPTION_CONNECT_TIMEOUT:
2286 if (*size < sizeof(DWORD))
2287 return ERROR_INSUFFICIENT_BUFFER;
2289 *size = sizeof(DWORD);
2290 *(DWORD *)buffer = req->connect_timeout;
2291 return ERROR_SUCCESS;
2292 case INTERNET_OPTION_REQUEST_FLAGS: {
2293 DWORD flags = 0;
2295 if (*size < sizeof(DWORD))
2296 return ERROR_INSUFFICIENT_BUFFER;
2298 /* FIXME: Add support for:
2299 * INTERNET_REQFLAG_FROM_CACHE
2300 * INTERNET_REQFLAG_CACHE_WRITE_DISABLED
2303 if(req->proxy)
2304 flags |= INTERNET_REQFLAG_VIA_PROXY;
2305 if(!req->status_code)
2306 flags |= INTERNET_REQFLAG_NO_HEADERS;
2308 TRACE("INTERNET_OPTION_REQUEST_FLAGS returning %x\n", flags);
2310 *size = sizeof(DWORD);
2311 *(DWORD*)buffer = flags;
2312 return ERROR_SUCCESS;
2314 case INTERNET_OPTION_ERROR_MASK:
2315 TRACE("INTERNET_OPTION_ERROR_MASK\n");
2317 if (*size < sizeof(ULONG))
2318 return ERROR_INSUFFICIENT_BUFFER;
2320 *(ULONG*)buffer = hdr->ErrorMask;
2321 *size = sizeof(ULONG);
2322 return ERROR_SUCCESS;
2323 case INTERNET_OPTION_SERVER_CERT_CHAIN_CONTEXT:
2324 TRACE("INTERNET_OPTION_SERVER_CERT_CHAIN_CONTEXT\n");
2326 if (*size < sizeof(PCCERT_CHAIN_CONTEXT))
2327 return ERROR_INSUFFICIENT_BUFFER;
2329 if (!req->server->cert_chain)
2330 return ERROR_INTERNET_INCORRECT_HANDLE_STATE;
2332 *(PCCERT_CHAIN_CONTEXT *)buffer = CertDuplicateCertificateChain(req->server->cert_chain);
2333 *size = sizeof(PCCERT_CHAIN_CONTEXT);
2335 return ERROR_SUCCESS;
2338 return INET_QueryOption(hdr, option, buffer, size, unicode);
2341 static DWORD HTTPREQ_SetOption(object_header_t *hdr, DWORD option, void *buffer, DWORD size)
2343 http_request_t *req = (http_request_t*)hdr;
2345 switch(option) {
2346 case 99: /* Undocumented, seems to be INTERNET_OPTION_SECURITY_FLAGS with argument validation */
2347 TRACE("Undocumented option 99\n");
2349 if (!buffer || size != sizeof(DWORD))
2350 return ERROR_INVALID_PARAMETER;
2351 if(*(DWORD*)buffer & ~SECURITY_SET_MASK)
2352 return ERROR_INTERNET_OPTION_NOT_SETTABLE;
2354 /* fall through */
2355 case INTERNET_OPTION_SECURITY_FLAGS:
2357 DWORD flags;
2359 if (!buffer || size != sizeof(DWORD))
2360 return ERROR_INVALID_PARAMETER;
2361 flags = *(DWORD *)buffer;
2362 TRACE("INTERNET_OPTION_SECURITY_FLAGS %08x\n", flags);
2363 flags &= SECURITY_SET_MASK;
2364 req->security_flags |= flags;
2365 if(is_valid_netconn(req->netconn))
2366 req->netconn->security_flags |= flags;
2367 return ERROR_SUCCESS;
2369 case INTERNET_OPTION_CONNECT_TIMEOUT:
2370 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2371 req->connect_timeout = *(DWORD *)buffer;
2372 return ERROR_SUCCESS;
2374 case INTERNET_OPTION_SEND_TIMEOUT:
2375 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2376 req->send_timeout = *(DWORD *)buffer;
2377 return ERROR_SUCCESS;
2379 case INTERNET_OPTION_RECEIVE_TIMEOUT:
2380 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
2381 req->receive_timeout = *(DWORD *)buffer;
2382 return ERROR_SUCCESS;
2384 case INTERNET_OPTION_USERNAME:
2385 heap_free(req->session->userName);
2386 if (!(req->session->userName = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2387 return ERROR_SUCCESS;
2389 case INTERNET_OPTION_PASSWORD:
2390 heap_free(req->session->password);
2391 if (!(req->session->password = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2392 return ERROR_SUCCESS;
2394 case INTERNET_OPTION_PROXY_USERNAME:
2395 heap_free(req->session->appInfo->proxyUsername);
2396 if (!(req->session->appInfo->proxyUsername = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2397 return ERROR_SUCCESS;
2399 case INTERNET_OPTION_PROXY_PASSWORD:
2400 heap_free(req->session->appInfo->proxyPassword);
2401 if (!(req->session->appInfo->proxyPassword = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
2402 return ERROR_SUCCESS;
2406 return INET_SetOption(hdr, option, buffer, size);
2409 static void commit_cache_entry(http_request_t *req)
2411 WCHAR *header;
2412 DWORD header_len;
2413 BOOL res;
2415 TRACE("%p\n", req);
2417 CloseHandle(req->hCacheFile);
2418 req->hCacheFile = NULL;
2420 header = build_response_header(req, TRUE);
2421 header_len = (header ? lstrlenW(header) : 0);
2422 res = CommitUrlCacheEntryW(req->req_file->url, req->req_file->file_name, req->expires,
2423 req->last_modified, NORMAL_CACHE_ENTRY,
2424 header, header_len, NULL, 0);
2425 if(res)
2426 req->req_file->is_committed = TRUE;
2427 else
2428 WARN("CommitUrlCacheEntry failed: %u\n", GetLastError());
2429 heap_free(header);
2432 static void create_cache_entry(http_request_t *req)
2434 WCHAR file_name[MAX_PATH+1];
2435 WCHAR *url;
2436 BOOL b = TRUE;
2438 /* FIXME: We should free previous cache file earlier */
2439 if(req->req_file) {
2440 req_file_release(req->req_file);
2441 req->req_file = NULL;
2443 if(req->hCacheFile) {
2444 CloseHandle(req->hCacheFile);
2445 req->hCacheFile = NULL;
2448 if(req->hdr.dwFlags & INTERNET_FLAG_NO_CACHE_WRITE)
2449 b = FALSE;
2451 if(b) {
2452 int header_idx;
2454 EnterCriticalSection( &req->headers_section );
2456 header_idx = HTTP_GetCustomHeaderIndex(req, L"Cache-Control", 0, FALSE);
2457 if(header_idx != -1) {
2458 WCHAR *ptr;
2460 for(ptr=req->custHeaders[header_idx].lpszValue; *ptr; ) {
2461 WCHAR *end;
2463 while(*ptr==' ' || *ptr=='\t')
2464 ptr++;
2466 end = wcschr(ptr, ',');
2467 if(!end)
2468 end = ptr + lstrlenW(ptr);
2470 if(!wcsnicmp(ptr, L"no-cache", ARRAY_SIZE(L"no-cache")-1)
2471 || !wcsnicmp(ptr, L"no-store", ARRAY_SIZE(L"no-store")-1)) {
2472 b = FALSE;
2473 break;
2476 ptr = end;
2477 if(*ptr == ',')
2478 ptr++;
2482 LeaveCriticalSection( &req->headers_section );
2485 if(!b) {
2486 if(!(req->hdr.dwFlags & INTERNET_FLAG_NEED_FILE))
2487 return;
2489 FIXME("INTERNET_FLAG_NEED_FILE is not supported correctly\n");
2492 url = compose_request_url(req);
2493 if(!url) {
2494 WARN("Could not get URL\n");
2495 return;
2498 b = CreateUrlCacheEntryW(url, req->contentLength == ~0 ? 0 : req->contentLength, NULL, file_name, 0);
2499 if(!b) {
2500 WARN("Could not create cache entry: %08x\n", GetLastError());
2501 return;
2504 create_req_file(file_name, &req->req_file);
2505 req->req_file->url = url;
2507 req->hCacheFile = CreateFileW(file_name, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
2508 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2509 if(req->hCacheFile == INVALID_HANDLE_VALUE) {
2510 WARN("Could not create file: %u\n", GetLastError());
2511 req->hCacheFile = NULL;
2512 return;
2515 if(req->read_size) {
2516 DWORD written;
2518 b = WriteFile(req->hCacheFile, req->read_buf+req->read_pos, req->read_size, &written, NULL);
2519 if(!b)
2520 FIXME("WriteFile failed: %u\n", GetLastError());
2522 if(req->data_stream->vtbl->end_of_data(req->data_stream, req))
2523 commit_cache_entry(req);
2527 /* read some more data into the read buffer (the read section must be held) */
2528 static DWORD read_more_data( http_request_t *req, int maxlen )
2530 DWORD res;
2531 int len;
2533 if (req->read_pos)
2535 /* move existing data to the start of the buffer */
2536 if(req->read_size)
2537 memmove( req->read_buf, req->read_buf + req->read_pos, req->read_size );
2538 req->read_pos = 0;
2541 if (maxlen == -1) maxlen = sizeof(req->read_buf);
2543 res = NETCON_recv( req->netconn, req->read_buf + req->read_size,
2544 maxlen - req->read_size, TRUE, &len );
2545 if(res == ERROR_SUCCESS)
2546 req->read_size += len;
2548 return res;
2551 /* remove some amount of data from the read buffer (the read section must be held) */
2552 static void remove_data( http_request_t *req, int count )
2554 if (!(req->read_size -= count)) req->read_pos = 0;
2555 else req->read_pos += count;
2558 static DWORD read_line( http_request_t *req, LPSTR buffer, DWORD *len )
2560 int count, bytes_read, pos = 0;
2561 DWORD res;
2563 EnterCriticalSection( &req->read_section );
2564 for (;;)
2566 BYTE *eol = memchr( req->read_buf + req->read_pos, '\n', req->read_size );
2568 if (eol)
2570 count = eol - (req->read_buf + req->read_pos);
2571 bytes_read = count + 1;
2573 else count = bytes_read = req->read_size;
2575 count = min( count, *len - pos );
2576 memcpy( buffer + pos, req->read_buf + req->read_pos, count );
2577 pos += count;
2578 remove_data( req, bytes_read );
2579 if (eol) break;
2581 if ((res = read_more_data( req, -1 )))
2583 WARN( "read failed %u\n", res );
2584 LeaveCriticalSection( &req->read_section );
2585 return res;
2587 if (!req->read_size)
2589 *len = 0;
2590 TRACE( "returning empty string\n" );
2591 LeaveCriticalSection( &req->read_section );
2592 return ERROR_SUCCESS;
2595 LeaveCriticalSection( &req->read_section );
2597 if (pos < *len)
2599 if (pos && buffer[pos - 1] == '\r') pos--;
2600 *len = pos + 1;
2602 buffer[*len - 1] = 0;
2603 TRACE( "returning %s\n", debugstr_a(buffer));
2604 return ERROR_SUCCESS;
2607 /* check if we have reached the end of the data to read (the read section must be held) */
2608 static BOOL end_of_read_data( http_request_t *req )
2610 return !req->read_size && req->data_stream->vtbl->end_of_data(req->data_stream, req);
2613 static DWORD read_http_stream(http_request_t *req, BYTE *buf, DWORD size, DWORD *read, BOOL allow_blocking)
2615 DWORD res;
2617 res = req->data_stream->vtbl->read(req->data_stream, req, buf, size, read, allow_blocking);
2618 if(res != ERROR_SUCCESS)
2619 *read = 0;
2620 assert(*read <= size);
2622 if(req->hCacheFile) {
2623 if(*read) {
2624 BOOL bres;
2625 DWORD written;
2627 bres = WriteFile(req->hCacheFile, buf, *read, &written, NULL);
2628 if(!bres)
2629 FIXME("WriteFile failed: %u\n", GetLastError());
2632 if((res == ERROR_SUCCESS && !*read) || req->data_stream->vtbl->end_of_data(req->data_stream, req))
2633 commit_cache_entry(req);
2636 return res;
2639 /* fetch some more data into the read buffer (the read section must be held) */
2640 static DWORD refill_read_buffer(http_request_t *req, BOOL allow_blocking, DWORD *read_bytes)
2642 DWORD res, read=0;
2644 if(req->read_size == sizeof(req->read_buf))
2645 return ERROR_SUCCESS;
2647 if(req->read_pos) {
2648 if(req->read_size)
2649 memmove(req->read_buf, req->read_buf+req->read_pos, req->read_size);
2650 req->read_pos = 0;
2653 res = read_http_stream(req, req->read_buf+req->read_size, sizeof(req->read_buf) - req->read_size,
2654 &read, allow_blocking);
2655 if(res != ERROR_SUCCESS)
2656 return res;
2658 req->read_size += read;
2660 TRACE("read %u bytes, read_size %u\n", read, req->read_size);
2661 if(read_bytes)
2662 *read_bytes = read;
2663 return res;
2666 static BOOL netconn_end_of_data(data_stream_t *stream, http_request_t *req)
2668 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2669 return netconn_stream->content_read == netconn_stream->content_length || !is_valid_netconn(req->netconn);
2672 static DWORD netconn_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
2673 DWORD *read, BOOL allow_blocking)
2675 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2676 DWORD res = ERROR_SUCCESS;
2677 int ret = 0;
2679 size = min(size, netconn_stream->content_length-netconn_stream->content_read);
2681 if(size && is_valid_netconn(req->netconn)) {
2682 res = NETCON_recv(req->netconn, buf, size, allow_blocking, &ret);
2683 if(res == ERROR_SUCCESS) {
2684 if(!ret)
2685 netconn_stream->content_length = netconn_stream->content_read;
2686 netconn_stream->content_read += ret;
2690 TRACE("res %u read %u bytes\n", res, ret);
2691 *read = ret;
2692 return res;
2695 static DWORD netconn_drain_content(data_stream_t *stream, http_request_t *req, BOOL allow_blocking)
2697 netconn_stream_t *netconn_stream = (netconn_stream_t*)stream;
2698 BYTE buf[1024];
2699 int len, res;
2700 size_t size;
2702 if(netconn_stream->content_length == ~0)
2703 return WSAEISCONN;
2705 while(netconn_stream->content_read < netconn_stream->content_length) {
2706 size = min(sizeof(buf), netconn_stream->content_length-netconn_stream->content_read);
2707 res = NETCON_recv(req->netconn, buf, size, allow_blocking, &len);
2708 if(res)
2709 return res;
2710 if(!len)
2711 return WSAECONNABORTED;
2713 netconn_stream->content_read += len;
2716 return ERROR_SUCCESS;
2719 static void netconn_destroy(data_stream_t *stream)
2723 static const data_stream_vtbl_t netconn_stream_vtbl = {
2724 netconn_end_of_data,
2725 netconn_read,
2726 netconn_drain_content,
2727 netconn_destroy
2730 static char next_chunked_data_char(chunked_stream_t *stream)
2732 assert(stream->buf_size);
2734 stream->buf_size--;
2735 return stream->buf[stream->buf_pos++];
2738 static BOOL chunked_end_of_data(data_stream_t *stream, http_request_t *req)
2740 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2741 switch(chunked_stream->state) {
2742 case CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END:
2743 case CHUNKED_STREAM_STATE_END_OF_STREAM:
2744 case CHUNKED_STREAM_STATE_ERROR:
2745 return TRUE;
2746 default:
2747 return FALSE;
2751 static DWORD chunked_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size,
2752 DWORD *read, BOOL allow_blocking)
2754 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2755 DWORD ret_read = 0, res = ERROR_SUCCESS;
2756 BOOL continue_read = TRUE;
2757 int read_bytes;
2758 char ch;
2760 do {
2761 TRACE("state %d\n", chunked_stream->state);
2763 /* Ensure that we have data in the buffer for states that need it. */
2764 if(!chunked_stream->buf_size) {
2765 BOOL blocking_read = allow_blocking;
2767 switch(chunked_stream->state) {
2768 case CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END:
2769 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE:
2770 /* never allow blocking after 0 chunk size */
2771 if(!chunked_stream->chunk_size)
2772 blocking_read = FALSE;
2773 /* fall through */
2774 case CHUNKED_STREAM_STATE_READING_CHUNK_SIZE:
2775 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA:
2776 chunked_stream->buf_pos = 0;
2777 res = NETCON_recv(req->netconn, chunked_stream->buf, sizeof(chunked_stream->buf), blocking_read, &read_bytes);
2778 if(res == ERROR_SUCCESS && read_bytes) {
2779 chunked_stream->buf_size += read_bytes;
2780 }else if(res == WSAEWOULDBLOCK) {
2781 if(ret_read || allow_blocking)
2782 res = ERROR_SUCCESS;
2783 continue_read = FALSE;
2784 continue;
2785 }else {
2786 chunked_stream->state = CHUNKED_STREAM_STATE_ERROR;
2788 break;
2789 default:
2790 break;
2794 switch(chunked_stream->state) {
2795 case CHUNKED_STREAM_STATE_READING_CHUNK_SIZE:
2796 ch = next_chunked_data_char(chunked_stream);
2798 if(ch >= '0' && ch <= '9') {
2799 chunked_stream->chunk_size = chunked_stream->chunk_size * 16 + ch - '0';
2800 }else if(ch >= 'a' && ch <= 'f') {
2801 chunked_stream->chunk_size = chunked_stream->chunk_size * 16 + ch - 'a' + 10;
2802 }else if (ch >= 'A' && ch <= 'F') {
2803 chunked_stream->chunk_size = chunked_stream->chunk_size * 16 + ch - 'A' + 10;
2804 }else if (ch == ';' || ch == '\r' || ch == '\n') {
2805 TRACE("reading %u byte chunk\n", chunked_stream->chunk_size);
2806 chunked_stream->buf_size++;
2807 chunked_stream->buf_pos--;
2808 if(req->contentLength == ~0) req->contentLength = chunked_stream->chunk_size;
2809 else req->contentLength += chunked_stream->chunk_size;
2810 chunked_stream->state = CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE;
2812 break;
2814 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE:
2815 ch = next_chunked_data_char(chunked_stream);
2816 if(ch == '\n')
2817 chunked_stream->state = chunked_stream->chunk_size
2818 ? CHUNKED_STREAM_STATE_READING_CHUNK
2819 : CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END;
2820 else if(ch != '\r')
2821 WARN("unexpected char '%c'\n", ch);
2822 break;
2824 case CHUNKED_STREAM_STATE_READING_CHUNK:
2825 assert(chunked_stream->chunk_size);
2826 if(!size) {
2827 continue_read = FALSE;
2828 break;
2830 read_bytes = min(size, chunked_stream->chunk_size);
2832 if(chunked_stream->buf_size) {
2833 if(read_bytes > chunked_stream->buf_size)
2834 read_bytes = chunked_stream->buf_size;
2836 memcpy(buf+ret_read, chunked_stream->buf+chunked_stream->buf_pos, read_bytes);
2837 chunked_stream->buf_pos += read_bytes;
2838 chunked_stream->buf_size -= read_bytes;
2839 }else {
2840 res = NETCON_recv(req->netconn, (char*)buf+ret_read, read_bytes,
2841 allow_blocking, (int*)&read_bytes);
2842 if(res != ERROR_SUCCESS) {
2843 continue_read = FALSE;
2844 break;
2847 if(!read_bytes) {
2848 chunked_stream->state = CHUNKED_STREAM_STATE_ERROR;
2849 continue;
2853 chunked_stream->chunk_size -= read_bytes;
2854 size -= read_bytes;
2855 ret_read += read_bytes;
2856 if(!chunked_stream->chunk_size)
2857 chunked_stream->state = CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA;
2858 allow_blocking = FALSE;
2859 break;
2861 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA:
2862 ch = next_chunked_data_char(chunked_stream);
2863 if(ch == '\n')
2864 chunked_stream->state = CHUNKED_STREAM_STATE_READING_CHUNK_SIZE;
2865 else if(ch != '\r')
2866 WARN("unexpected char '%c'\n", ch);
2867 break;
2869 case CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END:
2870 ch = next_chunked_data_char(chunked_stream);
2871 if(ch == '\n')
2872 chunked_stream->state = CHUNKED_STREAM_STATE_END_OF_STREAM;
2873 else if(ch != '\r')
2874 WARN("unexpected char '%c'\n", ch);
2875 break;
2877 case CHUNKED_STREAM_STATE_END_OF_STREAM:
2878 case CHUNKED_STREAM_STATE_ERROR:
2879 continue_read = FALSE;
2880 break;
2882 } while(continue_read);
2884 if(ret_read)
2885 res = ERROR_SUCCESS;
2886 if(res != ERROR_SUCCESS)
2887 return res;
2889 TRACE("read %d bytes\n", ret_read);
2890 *read = ret_read;
2891 return ERROR_SUCCESS;
2894 static DWORD chunked_drain_content(data_stream_t *stream, http_request_t *req, BOOL allow_blocking)
2896 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2897 BYTE buf[1024];
2898 DWORD size, res;
2900 while(chunked_stream->state != CHUNKED_STREAM_STATE_END_OF_STREAM
2901 && chunked_stream->state != CHUNKED_STREAM_STATE_ERROR) {
2902 res = chunked_read(stream, req, buf, sizeof(buf), &size, allow_blocking);
2903 if(res != ERROR_SUCCESS)
2904 return res;
2907 if(chunked_stream->state != CHUNKED_STREAM_STATE_END_OF_STREAM)
2908 return ERROR_NO_DATA;
2909 return ERROR_SUCCESS;
2912 static void chunked_destroy(data_stream_t *stream)
2914 chunked_stream_t *chunked_stream = (chunked_stream_t*)stream;
2915 heap_free(chunked_stream);
2918 static const data_stream_vtbl_t chunked_stream_vtbl = {
2919 chunked_end_of_data,
2920 chunked_read,
2921 chunked_drain_content,
2922 chunked_destroy
2925 /* set the request content length based on the headers */
2926 static DWORD set_content_length(http_request_t *request)
2928 WCHAR contentLength[32];
2929 WCHAR encoding[20];
2930 DWORD size;
2932 if(request->status_code == HTTP_STATUS_NO_CONTENT || request->status_code == HTTP_STATUS_NOT_MODIFIED ||
2933 !wcscmp(request->verb, L"HEAD"))
2935 if (HTTP_GetCustomHeaderIndex(request, L"Content-Length", 0, FALSE) == -1 ||
2936 !wcscmp(request->verb, L"HEAD"))
2937 request->read_size = 0;
2938 request->contentLength = request->netconn_stream.content_length = 0;
2939 return ERROR_SUCCESS;
2942 size = sizeof(contentLength);
2943 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_CONTENT_LENGTH,
2944 contentLength, &size, NULL) != ERROR_SUCCESS ||
2945 !StrToInt64ExW(contentLength, STIF_DEFAULT, (LONGLONG*)&request->contentLength)) {
2946 request->contentLength = ~0;
2949 request->netconn_stream.content_length = request->contentLength;
2950 request->netconn_stream.content_read = request->read_size;
2952 size = sizeof(encoding);
2953 if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_TRANSFER_ENCODING, encoding, &size, NULL) == ERROR_SUCCESS &&
2954 !wcsicmp(encoding, L"chunked"))
2956 chunked_stream_t *chunked_stream;
2958 chunked_stream = heap_alloc(sizeof(*chunked_stream));
2959 if(!chunked_stream)
2960 return ERROR_OUTOFMEMORY;
2962 chunked_stream->data_stream.vtbl = &chunked_stream_vtbl;
2963 chunked_stream->buf_size = chunked_stream->buf_pos = 0;
2964 chunked_stream->chunk_size = 0;
2965 chunked_stream->state = CHUNKED_STREAM_STATE_READING_CHUNK_SIZE;
2967 if(request->read_size) {
2968 memcpy(chunked_stream->buf, request->read_buf+request->read_pos, request->read_size);
2969 chunked_stream->buf_size = request->read_size;
2970 request->read_size = request->read_pos = 0;
2973 request->data_stream = &chunked_stream->data_stream;
2974 request->contentLength = ~0;
2977 if(request->hdr.decoding) {
2978 int encoding_idx;
2980 EnterCriticalSection( &request->headers_section );
2982 encoding_idx = HTTP_GetCustomHeaderIndex(request, L"Content-Encoding", 0, FALSE);
2983 if(encoding_idx != -1) {
2984 if(!wcsicmp(request->custHeaders[encoding_idx].lpszValue, L"gzip")) {
2985 HTTP_DeleteCustomHeader(request, encoding_idx);
2986 LeaveCriticalSection( &request->headers_section );
2987 return init_gzip_stream(request, TRUE);
2989 if(!wcsicmp(request->custHeaders[encoding_idx].lpszValue, L"deflate")) {
2990 HTTP_DeleteCustomHeader(request, encoding_idx);
2991 LeaveCriticalSection( &request->headers_section );
2992 return init_gzip_stream(request, FALSE);
2996 LeaveCriticalSection( &request->headers_section );
2999 return ERROR_SUCCESS;
3002 static void send_request_complete(http_request_t *req, DWORD_PTR result, DWORD error)
3004 INTERNET_ASYNC_RESULT iar;
3006 iar.dwResult = result;
3007 iar.dwError = error;
3009 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE, &iar,
3010 sizeof(INTERNET_ASYNC_RESULT));
3013 static void HTTP_ReceiveRequestData(http_request_t *req)
3015 DWORD res, read = 0;
3017 TRACE("%p\n", req);
3019 EnterCriticalSection( &req->read_section );
3021 res = refill_read_buffer(req, FALSE, &read);
3022 if(res == ERROR_SUCCESS)
3023 read += req->read_size;
3025 LeaveCriticalSection( &req->read_section );
3027 if(res != WSAEWOULDBLOCK && (res != ERROR_SUCCESS || !read)) {
3028 WARN("res %u read %u, closing connection\n", res, read);
3029 http_release_netconn(req, FALSE);
3032 if(res != ERROR_SUCCESS && res != WSAEWOULDBLOCK) {
3033 send_request_complete(req, 0, res);
3034 return;
3037 send_request_complete(req, req->session->hdr.dwInternalFlags & INET_OPENURL ? (DWORD_PTR)req->hdr.hInternet : 1, 0);
3040 /* read data from the http connection (the read section must be held) */
3041 static DWORD HTTPREQ_Read(http_request_t *req, void *buffer, DWORD size, DWORD *read, BOOL allow_blocking)
3043 DWORD current_read = 0, ret_read = 0;
3044 DWORD res = ERROR_SUCCESS;
3046 EnterCriticalSection( &req->read_section );
3048 if(req->read_size) {
3049 ret_read = min(size, req->read_size);
3050 memcpy(buffer, req->read_buf+req->read_pos, ret_read);
3051 req->read_size -= ret_read;
3052 req->read_pos += ret_read;
3053 allow_blocking = FALSE;
3056 if(ret_read < size) {
3057 res = read_http_stream(req, (BYTE*)buffer+ret_read, size-ret_read, &current_read, allow_blocking);
3058 if(res == ERROR_SUCCESS)
3059 ret_read += current_read;
3060 else if(res == WSAEWOULDBLOCK && ret_read)
3061 res = ERROR_SUCCESS;
3064 LeaveCriticalSection( &req->read_section );
3066 *read = ret_read;
3067 TRACE( "retrieved %u bytes (res %u)\n", ret_read, res );
3069 if(res != WSAEWOULDBLOCK) {
3070 if(res != ERROR_SUCCESS)
3071 http_release_netconn(req, FALSE);
3072 else if(!ret_read && drain_content(req, FALSE) == ERROR_SUCCESS)
3073 http_release_netconn(req, TRUE);
3076 return res;
3079 static DWORD drain_content(http_request_t *req, BOOL blocking)
3081 DWORD res;
3083 TRACE("%p\n", req->netconn);
3085 if(!is_valid_netconn(req->netconn))
3086 return ERROR_NO_DATA;
3088 if(!wcscmp(req->verb, L"HEAD"))
3089 return ERROR_SUCCESS;
3091 EnterCriticalSection( &req->read_section );
3092 res = req->data_stream->vtbl->drain_content(req->data_stream, req, blocking);
3093 LeaveCriticalSection( &req->read_section );
3094 return res;
3097 typedef struct {
3098 task_header_t hdr;
3099 void *buf;
3100 DWORD size;
3101 DWORD read_pos;
3102 DWORD *ret_read;
3103 } read_file_task_t;
3105 static void async_read_file_proc(task_header_t *hdr)
3107 read_file_task_t *task = (read_file_task_t*)hdr;
3108 http_request_t *req = (http_request_t*)task->hdr.hdr;
3109 DWORD res = ERROR_SUCCESS, read = task->read_pos, complete_arg = 0;
3111 TRACE("req %p buf %p size %u read_pos %u ret_read %p\n", req, task->buf, task->size, task->read_pos, task->ret_read);
3113 if(task->buf) {
3114 DWORD read_bytes;
3115 while (read < task->size) {
3116 res = HTTPREQ_Read(req, (char*)task->buf + read, task->size - read, &read_bytes, TRUE);
3117 if (res != ERROR_SUCCESS || !read_bytes)
3118 break;
3119 read += read_bytes;
3121 }else {
3122 EnterCriticalSection(&req->read_section);
3123 res = refill_read_buffer(req, TRUE, &read);
3124 LeaveCriticalSection(&req->read_section);
3126 if(task->ret_read)
3127 complete_arg = read; /* QueryDataAvailable reports read bytes in request complete notification */
3128 if(res != ERROR_SUCCESS || !read)
3129 http_release_netconn(req, drain_content(req, FALSE) == ERROR_SUCCESS);
3132 TRACE("res %u read %u\n", res, read);
3134 if(task->ret_read)
3135 *task->ret_read = read;
3137 /* FIXME: We should report bytes transferred before decoding content. */
3138 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED, &read, sizeof(read));
3140 if(res != ERROR_SUCCESS)
3141 complete_arg = res;
3142 send_request_complete(req, res == ERROR_SUCCESS, complete_arg);
3145 static DWORD async_read(http_request_t *req, void *buf, DWORD size, DWORD read_pos, DWORD *ret_read)
3147 read_file_task_t *task;
3149 task = alloc_async_task(&req->hdr, async_read_file_proc, sizeof(*task));
3150 if(!task)
3151 return ERROR_OUTOFMEMORY;
3153 task->buf = buf;
3154 task->size = size;
3155 task->read_pos = read_pos;
3156 task->ret_read = ret_read;
3158 INTERNET_AsyncCall(&task->hdr);
3159 return ERROR_IO_PENDING;
3162 static DWORD HTTPREQ_ReadFile(object_header_t *hdr, void *buf, DWORD size, DWORD *ret_read,
3163 DWORD flags, DWORD_PTR context)
3165 http_request_t *req = (http_request_t*)hdr;
3166 DWORD res = ERROR_SUCCESS, read = 0, cread, error = ERROR_SUCCESS;
3167 BOOL allow_blocking, notify_received = FALSE;
3169 TRACE("(%p %p %u %x)\n", req, buf, size, flags);
3171 if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
3172 FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
3174 allow_blocking = !(req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC);
3176 if(allow_blocking || TryEnterCriticalSection(&req->read_section)) {
3177 if(allow_blocking)
3178 EnterCriticalSection(&req->read_section);
3179 if(hdr->dwError == ERROR_SUCCESS)
3180 hdr->dwError = INTERNET_HANDLE_IN_USE;
3181 else if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3182 hdr->dwError = ERROR_INTERNET_INTERNAL_ERROR;
3184 if(req->read_size) {
3185 read = min(size, req->read_size);
3186 memcpy(buf, req->read_buf + req->read_pos, read);
3187 req->read_size -= read;
3188 req->read_pos += read;
3191 if(read < size && (!read || !(flags & IRF_NO_WAIT)) && !end_of_read_data(req)) {
3192 LeaveCriticalSection(&req->read_section);
3193 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3194 EnterCriticalSection( &req->read_section );
3195 notify_received = TRUE;
3197 while(read < size) {
3198 res = HTTPREQ_Read(req, (char*)buf+read, size-read, &cread, allow_blocking);
3199 read += cread;
3200 if (res != ERROR_SUCCESS || !cread)
3201 break;
3205 if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3206 hdr->dwError = ERROR_SUCCESS;
3207 else
3208 error = hdr->dwError;
3210 LeaveCriticalSection( &req->read_section );
3211 }else {
3212 res = WSAEWOULDBLOCK;
3215 if(res == WSAEWOULDBLOCK) {
3216 if(!(flags & IRF_NO_WAIT))
3217 return async_read(req, buf, size, read, ret_read);
3218 if(!read)
3219 return async_read(req, NULL, 0, 0, NULL);
3220 res = ERROR_SUCCESS;
3223 *ret_read = read;
3224 if (res != ERROR_SUCCESS)
3225 return res;
3227 if(notify_received)
3228 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
3229 &read, sizeof(read));
3230 return error;
3233 static DWORD HTTPREQ_WriteFile(object_header_t *hdr, const void *buffer, DWORD size, DWORD *written)
3235 DWORD res;
3236 http_request_t *request = (http_request_t*)hdr;
3238 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
3240 *written = 0;
3241 res = NETCON_send(request->netconn, buffer, size, 0, (LPINT)written);
3242 if (res == ERROR_SUCCESS)
3243 request->bytesWritten += *written;
3245 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REQUEST_SENT, written, sizeof(DWORD));
3246 return res;
3249 static DWORD HTTPREQ_QueryDataAvailable(object_header_t *hdr, DWORD *available, DWORD flags, DWORD_PTR ctx)
3251 http_request_t *req = (http_request_t*)hdr;
3252 DWORD res = ERROR_SUCCESS, avail = 0, error = ERROR_SUCCESS;
3253 BOOL allow_blocking, notify_received = FALSE;
3255 TRACE("(%p %p %x %lx)\n", req, available, flags, ctx);
3257 if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
3258 FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
3260 *available = 0;
3261 allow_blocking = !(req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC);
3263 if(allow_blocking || TryEnterCriticalSection(&req->read_section)) {
3264 if(allow_blocking)
3265 EnterCriticalSection(&req->read_section);
3266 if(hdr->dwError == ERROR_SUCCESS)
3267 hdr->dwError = INTERNET_HANDLE_IN_USE;
3268 else if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3269 hdr->dwError = ERROR_INTERNET_INTERNAL_ERROR;
3271 avail = req->read_size;
3273 if(!avail && !end_of_read_data(req)) {
3274 LeaveCriticalSection(&req->read_section);
3275 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3276 EnterCriticalSection( &req->read_section );
3277 notify_received = TRUE;
3279 res = refill_read_buffer(req, allow_blocking, &avail);
3282 if(hdr->dwError == INTERNET_HANDLE_IN_USE)
3283 hdr->dwError = ERROR_SUCCESS;
3284 else
3285 error = hdr->dwError;
3287 LeaveCriticalSection( &req->read_section );
3288 }else {
3289 res = WSAEWOULDBLOCK;
3292 if(res == WSAEWOULDBLOCK)
3293 return async_read(req, NULL, 0, 0, available);
3295 if (res != ERROR_SUCCESS)
3296 return res;
3298 *available = avail;
3299 if(notify_received)
3300 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
3301 &avail, sizeof(avail));
3302 return error;
3305 static DWORD HTTPREQ_LockRequestFile(object_header_t *hdr, req_file_t **ret)
3307 http_request_t *req = (http_request_t*)hdr;
3309 TRACE("(%p)\n", req);
3311 if(!req->req_file) {
3312 WARN("No cache file name available\n");
3313 return ERROR_FILE_NOT_FOUND;
3316 *ret = req_file_addref(req->req_file);
3317 return ERROR_SUCCESS;
3320 static const object_vtbl_t HTTPREQVtbl = {
3321 HTTPREQ_Destroy,
3322 HTTPREQ_CloseConnection,
3323 HTTPREQ_QueryOption,
3324 HTTPREQ_SetOption,
3325 HTTPREQ_ReadFile,
3326 HTTPREQ_WriteFile,
3327 HTTPREQ_QueryDataAvailable,
3328 NULL,
3329 HTTPREQ_LockRequestFile
3332 /***********************************************************************
3333 * HTTP_HttpOpenRequestW (internal)
3335 * Open a HTTP request handle
3337 * RETURNS
3338 * HINTERNET a HTTP request handle on success
3339 * NULL on failure
3342 static DWORD HTTP_HttpOpenRequestW(http_session_t *session,
3343 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
3344 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
3345 DWORD dwFlags, DWORD_PTR dwContext, HINTERNET *ret)
3347 appinfo_t *hIC = session->appInfo;
3348 http_request_t *request;
3349 DWORD port, len;
3351 TRACE("-->\n");
3353 request = alloc_object(&session->hdr, &HTTPREQVtbl, sizeof(http_request_t));
3354 if(!request)
3355 return ERROR_OUTOFMEMORY;
3357 request->hdr.htype = WH_HHTTPREQ;
3358 request->hdr.dwFlags = dwFlags;
3359 request->hdr.dwContext = dwContext;
3360 request->hdr.decoding = session->hdr.decoding;
3361 request->contentLength = ~0;
3363 request->netconn_stream.data_stream.vtbl = &netconn_stream_vtbl;
3364 request->data_stream = &request->netconn_stream.data_stream;
3365 request->connect_timeout = session->connect_timeout;
3366 request->send_timeout = session->send_timeout;
3367 request->receive_timeout = session->receive_timeout;
3369 InitializeCriticalSection( &request->headers_section );
3370 request->headers_section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": http_request_t.headers_section");
3372 InitializeCriticalSection( &request->read_section );
3373 request->read_section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": http_request_t.read_section");
3375 WININET_AddRef( &session->hdr );
3376 request->session = session;
3377 list_add_head( &session->hdr.children, &request->hdr.entry );
3379 port = session->hostPort;
3380 if (port == INTERNET_INVALID_PORT_NUMBER)
3381 port = (session->hdr.dwFlags & INTERNET_FLAG_SECURE) ?
3382 INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT;
3384 request->server = get_server(substrz(session->hostName), port, (dwFlags & INTERNET_FLAG_SECURE) != 0, TRUE);
3385 if(!request->server) {
3386 WININET_Release(&request->hdr);
3387 return ERROR_OUTOFMEMORY;
3390 if (dwFlags & INTERNET_FLAG_IGNORE_CERT_CN_INVALID)
3391 request->security_flags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
3392 if (dwFlags & INTERNET_FLAG_IGNORE_CERT_DATE_INVALID)
3393 request->security_flags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
3395 if (lpszObjectName && *lpszObjectName) {
3396 HRESULT rc;
3397 WCHAR dummy;
3399 len = 1;
3400 rc = UrlCanonicalizeW(lpszObjectName, &dummy, &len, URL_ESCAPE_SPACES_ONLY);
3401 if (rc != E_POINTER)
3402 len = lstrlenW(lpszObjectName)+1;
3403 request->path = heap_alloc(len*sizeof(WCHAR));
3404 rc = UrlCanonicalizeW(lpszObjectName, request->path, &len,
3405 URL_ESCAPE_SPACES_ONLY);
3406 if (rc != S_OK)
3408 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc);
3409 lstrcpyW(request->path,lpszObjectName);
3411 }else {
3412 request->path = heap_strdupW(L"/");
3415 if (lpszReferrer && *lpszReferrer)
3416 HTTP_ProcessHeader(request, L"Referer", lpszReferrer, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
3418 if (lpszAcceptTypes)
3420 int i;
3421 for (i = 0; lpszAcceptTypes[i]; i++)
3423 if (!*lpszAcceptTypes[i]) continue;
3424 HTTP_ProcessHeader(request, L"Accept", lpszAcceptTypes[i],
3425 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA |
3426 HTTP_ADDHDR_FLAG_REQ |
3427 (i == 0 ? (HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_ADD) : 0));
3431 request->verb = heap_strdupW(lpszVerb && *lpszVerb ? lpszVerb : L"GET");
3432 request->version = heap_strdupW(lpszVersion && *lpszVersion ? lpszVersion : L"HTTP/1.1");
3434 if (hIC->proxy && hIC->proxy[0] && !HTTP_ShouldBypassProxy(hIC, session->hostName))
3435 HTTP_DealWithProxy( hIC, session, request );
3437 INTERNET_SendCallback(&session->hdr, dwContext,
3438 INTERNET_STATUS_HANDLE_CREATED, &request->hdr.hInternet,
3439 sizeof(HINTERNET));
3441 TRACE("<-- (%p)\n", request);
3443 *ret = request->hdr.hInternet;
3444 return ERROR_SUCCESS;
3447 /***********************************************************************
3448 * HttpOpenRequestW (WININET.@)
3450 * Open a HTTP request handle
3452 * RETURNS
3453 * HINTERNET a HTTP request handle on success
3454 * NULL on failure
3457 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
3458 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
3459 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
3460 DWORD dwFlags, DWORD_PTR dwContext)
3462 http_session_t *session;
3463 HINTERNET handle = NULL;
3464 DWORD res;
3466 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
3467 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
3468 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
3469 dwFlags, dwContext);
3470 if(lpszAcceptTypes!=NULL)
3472 int i;
3473 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
3474 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
3477 session = (http_session_t*) get_handle_object( hHttpSession );
3478 if (NULL == session || session->hdr.htype != WH_HHTTPSESSION)
3480 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3481 goto lend;
3485 * My tests seem to show that the windows version does not
3486 * become asynchronous until after this point. And anyhow
3487 * if this call was asynchronous then how would you get the
3488 * necessary HINTERNET pointer returned by this function.
3491 res = HTTP_HttpOpenRequestW(session, lpszVerb, lpszObjectName,
3492 lpszVersion, lpszReferrer, lpszAcceptTypes,
3493 dwFlags, dwContext, &handle);
3494 lend:
3495 if( session )
3496 WININET_Release( &session->hdr );
3497 TRACE("returning %p\n", handle);
3498 if(res != ERROR_SUCCESS)
3499 SetLastError(res);
3500 return handle;
3503 static const LPCWSTR header_lookup[] = {
3504 L"Mime-Version", /* HTTP_QUERY_MIME_VERSION = 0 */
3505 L"Content-Type", /* HTTP_QUERY_CONTENT_TYPE = 1 */
3506 L"Content-Transfer-Encoding", /* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
3507 L"Content-ID", /* HTTP_QUERY_CONTENT_ID = 3 */
3508 NULL, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
3509 L"Content-Length", /* HTTP_QUERY_CONTENT_LENGTH = 5 */
3510 L"Content-Language", /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
3511 L"Allow", /* HTTP_QUERY_ALLOW = 7 */
3512 L"Public", /* HTTP_QUERY_PUBLIC = 8 */
3513 L"Date", /* HTTP_QUERY_DATE = 9 */
3514 L"Expires", /* HTTP_QUERY_EXPIRES = 10 */
3515 L"Last-Modified", /* HTTP_QUERY_LAST_MODIFIED = 11 */
3516 NULL, /* HTTP_QUERY_MESSAGE_ID = 12 */
3517 L"URI", /* HTTP_QUERY_URI = 13 */
3518 L"From", /* HTTP_QUERY_DERIVED_FROM = 14 */
3519 NULL, /* HTTP_QUERY_COST = 15 */
3520 NULL, /* HTTP_QUERY_LINK = 16 */
3521 L"Pragma", /* HTTP_QUERY_PRAGMA = 17 */
3522 NULL, /* HTTP_QUERY_VERSION = 18 */
3523 L"Status", /* HTTP_QUERY_STATUS_CODE = 19 */
3524 NULL, /* HTTP_QUERY_STATUS_TEXT = 20 */
3525 NULL, /* HTTP_QUERY_RAW_HEADERS = 21 */
3526 NULL, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
3527 L"Connection", /* HTTP_QUERY_CONNECTION = 23 */
3528 L"Accept", /* HTTP_QUERY_ACCEPT = 24 */
3529 L"Accept-Charset", /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
3530 L"Accept-Encoding", /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
3531 L"Accept-Language", /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
3532 L"Authorization", /* HTTP_QUERY_AUTHORIZATION = 28 */
3533 L"Content-Encoding", /* HTTP_QUERY_CONTENT_ENCODING = 29 */
3534 NULL, /* HTTP_QUERY_FORWARDED = 30 */
3535 NULL, /* HTTP_QUERY_FROM = 31 */
3536 L"If-Modified-Since", /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
3537 L"Location", /* HTTP_QUERY_LOCATION = 33 */
3538 NULL, /* HTTP_QUERY_ORIG_URI = 34 */
3539 L"Referer", /* HTTP_QUERY_REFERER = 35 */
3540 L"Retry-After", /* HTTP_QUERY_RETRY_AFTER = 36 */
3541 L"Server", /* HTTP_QUERY_SERVER = 37 */
3542 NULL, /* HTTP_TITLE = 38 */
3543 L"User-Agent", /* HTTP_QUERY_USER_AGENT = 39 */
3544 L"WWW-Authenticate", /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
3545 L"Proxy-Authenticate", /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
3546 L"Accept-Ranges", /* HTTP_QUERY_ACCEPT_RANGES = 42 */
3547 L"Set-Cookie", /* HTTP_QUERY_SET_COOKIE = 43 */
3548 L"Cookie", /* HTTP_QUERY_COOKIE = 44 */
3549 NULL, /* HTTP_QUERY_REQUEST_METHOD = 45 */
3550 NULL, /* HTTP_QUERY_REFRESH = 46 */
3551 L"Content-Disposition", /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
3552 L"Age", /* HTTP_QUERY_AGE = 48 */
3553 L"Cache-Control", /* HTTP_QUERY_CACHE_CONTROL = 49 */
3554 L"Content-Base", /* HTTP_QUERY_CONTENT_BASE = 50 */
3555 L"Content-Location", /* HTTP_QUERY_CONTENT_LOCATION = 51 */
3556 L"Content-MD5", /* HTTP_QUERY_CONTENT_MD5 = 52 */
3557 L"Content-Range", /* HTTP_QUERY_CONTENT_RANGE = 53 */
3558 L"ETag", /* HTTP_QUERY_ETAG = 54 */
3559 L"Host", /* HTTP_QUERY_HOST = 55 */
3560 L"If-Match", /* HTTP_QUERY_IF_MATCH = 56 */
3561 L"If-None-Match", /* HTTP_QUERY_IF_NONE_MATCH = 57 */
3562 L"If-Range", /* HTTP_QUERY_IF_RANGE = 58 */
3563 L"If-Unmodified-Since", /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
3564 L"Max-Forwards", /* HTTP_QUERY_MAX_FORWARDS = 60 */
3565 L"Proxy-Authorization", /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
3566 L"Range", /* HTTP_QUERY_RANGE = 62 */
3567 L"Transfer-Encoding", /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
3568 L"Upgrade", /* HTTP_QUERY_UPGRADE = 64 */
3569 L"Vary", /* HTTP_QUERY_VARY = 65 */
3570 L"Via", /* HTTP_QUERY_VIA = 66 */
3571 L"Warning", /* HTTP_QUERY_WARNING = 67 */
3572 L"Expect", /* HTTP_QUERY_EXPECT = 68 */
3573 L"Proxy-Connection", /* HTTP_QUERY_PROXY_CONNECTION = 69 */
3574 L"Unless-Modified-Since", /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
3577 /***********************************************************************
3578 * HTTP_HttpQueryInfoW (internal)
3580 static DWORD HTTP_HttpQueryInfoW(http_request_t *request, DWORD dwInfoLevel,
3581 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
3583 LPHTTPHEADERW lphttpHdr = NULL;
3584 BOOL request_only = !!(dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS);
3585 INT requested_index = lpdwIndex ? *lpdwIndex : 0;
3586 DWORD level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
3587 INT index = -1;
3589 EnterCriticalSection( &request->headers_section );
3591 /* Find requested header structure */
3592 switch (level)
3594 case HTTP_QUERY_CUSTOM:
3595 if (!lpBuffer)
3597 LeaveCriticalSection( &request->headers_section );
3598 return ERROR_INVALID_PARAMETER;
3600 index = HTTP_GetCustomHeaderIndex(request, lpBuffer, requested_index, request_only);
3601 break;
3602 case HTTP_QUERY_RAW_HEADERS_CRLF:
3604 LPWSTR headers;
3605 DWORD len = 0;
3606 DWORD res = ERROR_INVALID_PARAMETER;
3608 if (request_only)
3609 headers = build_request_header(request, request->verb, request->path, request->version, TRUE);
3610 else
3611 headers = build_response_header(request, TRUE);
3612 if (!headers)
3614 LeaveCriticalSection( &request->headers_section );
3615 return ERROR_OUTOFMEMORY;
3618 len = lstrlenW(headers) * sizeof(WCHAR);
3619 if (len + sizeof(WCHAR) > *lpdwBufferLength)
3621 len += sizeof(WCHAR);
3622 res = ERROR_INSUFFICIENT_BUFFER;
3624 else if (lpBuffer)
3626 memcpy(lpBuffer, headers, len + sizeof(WCHAR));
3627 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len / sizeof(WCHAR)));
3628 res = ERROR_SUCCESS;
3630 *lpdwBufferLength = len;
3632 heap_free(headers);
3633 LeaveCriticalSection( &request->headers_section );
3634 return res;
3636 case HTTP_QUERY_RAW_HEADERS:
3638 LPWSTR headers;
3639 DWORD len;
3641 if (request_only)
3642 headers = build_request_header(request, request->verb, request->path, request->version, FALSE);
3643 else
3644 headers = build_response_header(request, FALSE);
3646 if (!headers)
3648 LeaveCriticalSection( &request->headers_section );
3649 return ERROR_OUTOFMEMORY;
3652 len = lstrlenW(headers) * sizeof(WCHAR);
3653 if (len > *lpdwBufferLength)
3655 *lpdwBufferLength = len;
3656 heap_free(headers);
3657 LeaveCriticalSection( &request->headers_section );
3658 return ERROR_INSUFFICIENT_BUFFER;
3661 if (lpBuffer)
3663 DWORD i;
3665 TRACE("returning data: %s\n", debugstr_wn(headers, len / sizeof(WCHAR)));
3667 for (i = 0; i < len / sizeof(WCHAR); i++)
3669 if (headers[i] == '\n')
3670 headers[i] = 0;
3672 memcpy(lpBuffer, headers, len);
3674 *lpdwBufferLength = len - sizeof(WCHAR);
3676 heap_free(headers);
3677 LeaveCriticalSection( &request->headers_section );
3678 return ERROR_SUCCESS;
3680 case HTTP_QUERY_STATUS_TEXT:
3681 if (request->statusText)
3683 DWORD len = lstrlenW(request->statusText);
3684 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
3686 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
3687 LeaveCriticalSection( &request->headers_section );
3688 return ERROR_INSUFFICIENT_BUFFER;
3690 if (lpBuffer)
3692 memcpy(lpBuffer, request->statusText, (len + 1) * sizeof(WCHAR));
3693 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
3695 *lpdwBufferLength = len * sizeof(WCHAR);
3696 LeaveCriticalSection( &request->headers_section );
3697 return ERROR_SUCCESS;
3699 break;
3700 case HTTP_QUERY_VERSION:
3701 if (request->version)
3703 DWORD len = lstrlenW(request->version);
3704 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
3706 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
3707 LeaveCriticalSection( &request->headers_section );
3708 return ERROR_INSUFFICIENT_BUFFER;
3710 if (lpBuffer)
3712 memcpy(lpBuffer, request->version, (len + 1) * sizeof(WCHAR));
3713 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
3715 *lpdwBufferLength = len * sizeof(WCHAR);
3716 LeaveCriticalSection( &request->headers_section );
3717 return ERROR_SUCCESS;
3719 break;
3720 case HTTP_QUERY_CONTENT_ENCODING:
3721 index = HTTP_GetCustomHeaderIndex(request, header_lookup[request->read_gzip ? HTTP_QUERY_CONTENT_TYPE : level],
3722 requested_index,request_only);
3723 break;
3724 case HTTP_QUERY_STATUS_CODE: {
3725 DWORD res = ERROR_SUCCESS;
3727 if(request_only)
3729 LeaveCriticalSection( &request->headers_section );
3730 return ERROR_HTTP_INVALID_QUERY_REQUEST;
3733 if(requested_index)
3734 break;
3736 if(dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) {
3737 if(*lpdwBufferLength >= sizeof(DWORD))
3738 *(DWORD*)lpBuffer = request->status_code;
3739 else
3740 res = ERROR_INSUFFICIENT_BUFFER;
3741 *lpdwBufferLength = sizeof(DWORD);
3742 }else {
3743 WCHAR buf[12];
3744 DWORD size;
3746 size = swprintf(buf, ARRAY_SIZE(buf), L"%u", request->status_code) * sizeof(WCHAR);
3748 if(size <= *lpdwBufferLength) {
3749 memcpy(lpBuffer, buf, size+sizeof(WCHAR));
3750 }else {
3751 size += sizeof(WCHAR);
3752 res = ERROR_INSUFFICIENT_BUFFER;
3755 *lpdwBufferLength = size;
3757 LeaveCriticalSection( &request->headers_section );
3758 return res;
3760 default:
3761 assert (ARRAY_SIZE(header_lookup) == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
3763 if (level < ARRAY_SIZE(header_lookup) && header_lookup[level])
3764 index = HTTP_GetCustomHeaderIndex(request, header_lookup[level],
3765 requested_index,request_only);
3768 if (index >= 0)
3769 lphttpHdr = &request->custHeaders[index];
3771 /* Ensure header satisfies requested attributes */
3772 if (!lphttpHdr ||
3773 ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
3774 (~lphttpHdr->wFlags & HDR_ISREQUEST)))
3776 LeaveCriticalSection( &request->headers_section );
3777 return ERROR_HTTP_HEADER_NOT_FOUND;
3780 /* coalesce value to requested type */
3781 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER && lpBuffer)
3783 unsigned long value;
3785 if (*lpdwBufferLength != sizeof(DWORD))
3787 LeaveCriticalSection( &request->headers_section );
3788 return ERROR_HTTP_INVALID_HEADER;
3791 errno = 0;
3792 value = wcstoul( lphttpHdr->lpszValue, NULL, 10 );
3793 if (value > UINT_MAX || (value == ULONG_MAX && errno == ERANGE))
3795 LeaveCriticalSection( &request->headers_section );
3796 return ERROR_HTTP_INVALID_HEADER;
3799 *(DWORD *)lpBuffer = value;
3800 TRACE(" returning number: %u\n", *(DWORD *)lpBuffer);
3802 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME && lpBuffer)
3804 time_t tmpTime;
3805 struct tm tmpTM;
3806 SYSTEMTIME *STHook;
3808 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
3810 tmpTM = *gmtime(&tmpTime);
3811 STHook = (SYSTEMTIME *)lpBuffer;
3812 STHook->wDay = tmpTM.tm_mday;
3813 STHook->wHour = tmpTM.tm_hour;
3814 STHook->wMilliseconds = 0;
3815 STHook->wMinute = tmpTM.tm_min;
3816 STHook->wDayOfWeek = tmpTM.tm_wday;
3817 STHook->wMonth = tmpTM.tm_mon + 1;
3818 STHook->wSecond = tmpTM.tm_sec;
3819 STHook->wYear = 1900+tmpTM.tm_year;
3821 TRACE(" returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
3822 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
3823 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
3825 else if (lphttpHdr->lpszValue)
3827 DWORD len = (lstrlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
3829 if (len > *lpdwBufferLength)
3831 *lpdwBufferLength = len;
3832 LeaveCriticalSection( &request->headers_section );
3833 return ERROR_INSUFFICIENT_BUFFER;
3835 if (lpBuffer)
3837 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
3838 TRACE("! returning string: %s\n", debugstr_w(lpBuffer));
3840 *lpdwBufferLength = len - sizeof(WCHAR);
3842 if (lpdwIndex) (*lpdwIndex)++;
3844 LeaveCriticalSection( &request->headers_section );
3845 return ERROR_SUCCESS;
3848 /***********************************************************************
3849 * HttpQueryInfoW (WININET.@)
3851 * Queries for information about an HTTP request
3853 * RETURNS
3854 * TRUE on success
3855 * FALSE on failure
3858 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
3859 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
3861 http_request_t *request;
3862 DWORD res;
3864 if (TRACE_ON(wininet)) {
3865 #define FE(x) { x, #x }
3866 static const wininet_flag_info query_flags[] = {
3867 FE(HTTP_QUERY_MIME_VERSION),
3868 FE(HTTP_QUERY_CONTENT_TYPE),
3869 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
3870 FE(HTTP_QUERY_CONTENT_ID),
3871 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
3872 FE(HTTP_QUERY_CONTENT_LENGTH),
3873 FE(HTTP_QUERY_CONTENT_LANGUAGE),
3874 FE(HTTP_QUERY_ALLOW),
3875 FE(HTTP_QUERY_PUBLIC),
3876 FE(HTTP_QUERY_DATE),
3877 FE(HTTP_QUERY_EXPIRES),
3878 FE(HTTP_QUERY_LAST_MODIFIED),
3879 FE(HTTP_QUERY_MESSAGE_ID),
3880 FE(HTTP_QUERY_URI),
3881 FE(HTTP_QUERY_DERIVED_FROM),
3882 FE(HTTP_QUERY_COST),
3883 FE(HTTP_QUERY_LINK),
3884 FE(HTTP_QUERY_PRAGMA),
3885 FE(HTTP_QUERY_VERSION),
3886 FE(HTTP_QUERY_STATUS_CODE),
3887 FE(HTTP_QUERY_STATUS_TEXT),
3888 FE(HTTP_QUERY_RAW_HEADERS),
3889 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
3890 FE(HTTP_QUERY_CONNECTION),
3891 FE(HTTP_QUERY_ACCEPT),
3892 FE(HTTP_QUERY_ACCEPT_CHARSET),
3893 FE(HTTP_QUERY_ACCEPT_ENCODING),
3894 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
3895 FE(HTTP_QUERY_AUTHORIZATION),
3896 FE(HTTP_QUERY_CONTENT_ENCODING),
3897 FE(HTTP_QUERY_FORWARDED),
3898 FE(HTTP_QUERY_FROM),
3899 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
3900 FE(HTTP_QUERY_LOCATION),
3901 FE(HTTP_QUERY_ORIG_URI),
3902 FE(HTTP_QUERY_REFERER),
3903 FE(HTTP_QUERY_RETRY_AFTER),
3904 FE(HTTP_QUERY_SERVER),
3905 FE(HTTP_QUERY_TITLE),
3906 FE(HTTP_QUERY_USER_AGENT),
3907 FE(HTTP_QUERY_WWW_AUTHENTICATE),
3908 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
3909 FE(HTTP_QUERY_ACCEPT_RANGES),
3910 FE(HTTP_QUERY_SET_COOKIE),
3911 FE(HTTP_QUERY_COOKIE),
3912 FE(HTTP_QUERY_REQUEST_METHOD),
3913 FE(HTTP_QUERY_REFRESH),
3914 FE(HTTP_QUERY_CONTENT_DISPOSITION),
3915 FE(HTTP_QUERY_AGE),
3916 FE(HTTP_QUERY_CACHE_CONTROL),
3917 FE(HTTP_QUERY_CONTENT_BASE),
3918 FE(HTTP_QUERY_CONTENT_LOCATION),
3919 FE(HTTP_QUERY_CONTENT_MD5),
3920 FE(HTTP_QUERY_CONTENT_RANGE),
3921 FE(HTTP_QUERY_ETAG),
3922 FE(HTTP_QUERY_HOST),
3923 FE(HTTP_QUERY_IF_MATCH),
3924 FE(HTTP_QUERY_IF_NONE_MATCH),
3925 FE(HTTP_QUERY_IF_RANGE),
3926 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
3927 FE(HTTP_QUERY_MAX_FORWARDS),
3928 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
3929 FE(HTTP_QUERY_RANGE),
3930 FE(HTTP_QUERY_TRANSFER_ENCODING),
3931 FE(HTTP_QUERY_UPGRADE),
3932 FE(HTTP_QUERY_VARY),
3933 FE(HTTP_QUERY_VIA),
3934 FE(HTTP_QUERY_WARNING),
3935 FE(HTTP_QUERY_CUSTOM)
3937 static const wininet_flag_info modifier_flags[] = {
3938 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
3939 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
3940 FE(HTTP_QUERY_FLAG_NUMBER),
3941 FE(HTTP_QUERY_FLAG_COALESCE)
3943 #undef FE
3944 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
3945 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
3946 DWORD i;
3948 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, info);
3949 TRACE(" Attribute:");
3950 for (i = 0; i < ARRAY_SIZE(query_flags); i++) {
3951 if (query_flags[i].val == info) {
3952 TRACE(" %s", query_flags[i].name);
3953 break;
3956 if (i == ARRAY_SIZE(query_flags)) {
3957 TRACE(" Unknown (%08x)", info);
3960 TRACE(" Modifier:");
3961 for (i = 0; i < ARRAY_SIZE(modifier_flags); i++) {
3962 if (modifier_flags[i].val & info_mod) {
3963 TRACE(" %s", modifier_flags[i].name);
3964 info_mod &= ~ modifier_flags[i].val;
3968 if (info_mod) {
3969 TRACE(" Unknown (%08x)", info_mod);
3971 TRACE("\n");
3974 request = (http_request_t*) get_handle_object( hHttpRequest );
3975 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
3977 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
3978 goto lend;
3981 if (lpBuffer == NULL)
3982 *lpdwBufferLength = 0;
3983 res = HTTP_HttpQueryInfoW( request, dwInfoLevel,
3984 lpBuffer, lpdwBufferLength, lpdwIndex);
3986 lend:
3987 if( request )
3988 WININET_Release( &request->hdr );
3990 TRACE("%u <--\n", res);
3992 SetLastError(res);
3993 return res == ERROR_SUCCESS;
3996 /***********************************************************************
3997 * HttpQueryInfoA (WININET.@)
3999 * Queries for information about an HTTP request
4001 * RETURNS
4002 * TRUE on success
4003 * FALSE on failure
4006 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
4007 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
4009 BOOL result;
4010 DWORD len;
4011 WCHAR* bufferW;
4013 TRACE("%p %x\n", hHttpRequest, dwInfoLevel);
4015 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
4016 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
4018 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
4019 lpdwBufferLength, lpdwIndex );
4022 if (lpBuffer)
4024 DWORD alloclen;
4025 len = (*lpdwBufferLength)*sizeof(WCHAR);
4026 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
4028 alloclen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 ) * sizeof(WCHAR);
4029 if (alloclen < len)
4030 alloclen = len;
4032 else
4033 alloclen = len;
4034 bufferW = heap_alloc(alloclen);
4035 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
4036 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
4037 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, bufferW, alloclen / sizeof(WCHAR) );
4038 } else
4040 bufferW = NULL;
4041 len = 0;
4044 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
4045 &len, lpdwIndex );
4046 if( result )
4048 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
4049 lpBuffer, *lpdwBufferLength, NULL, NULL );
4050 *lpdwBufferLength = len - 1;
4052 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
4054 else
4055 /* since the strings being returned from HttpQueryInfoW should be
4056 * only ASCII characters, it is reasonable to assume that all of
4057 * the Unicode characters can be reduced to a single byte */
4058 *lpdwBufferLength = len / sizeof(WCHAR);
4060 heap_free( bufferW );
4061 return result;
4064 static WCHAR *get_redirect_url(http_request_t *request)
4066 static WCHAR szHttp[] = L"http";
4067 static WCHAR szHttps[] = L"https";
4068 http_session_t *session = request->session;
4069 URL_COMPONENTSW urlComponents = { sizeof(urlComponents) };
4070 WCHAR *orig_url = NULL, *redirect_url = NULL, *combined_url = NULL;
4071 DWORD url_length = 0, res;
4072 BOOL b;
4074 url_length = 0;
4075 res = HTTP_HttpQueryInfoW(request, HTTP_QUERY_LOCATION, redirect_url, &url_length, NULL);
4076 if(res == ERROR_INSUFFICIENT_BUFFER) {
4077 redirect_url = heap_alloc(url_length);
4078 res = HTTP_HttpQueryInfoW(request, HTTP_QUERY_LOCATION, redirect_url, &url_length, NULL);
4080 if(res != ERROR_SUCCESS) {
4081 heap_free(redirect_url);
4082 return NULL;
4085 urlComponents.dwSchemeLength = 1;
4086 b = InternetCrackUrlW(redirect_url, url_length / sizeof(WCHAR), 0, &urlComponents);
4087 if(b && urlComponents.dwSchemeLength &&
4088 urlComponents.nScheme != INTERNET_SCHEME_HTTP && urlComponents.nScheme != INTERNET_SCHEME_HTTPS) {
4089 TRACE("redirect to non-http URL\n");
4090 return NULL;
4093 urlComponents.lpszScheme = (request->hdr.dwFlags & INTERNET_FLAG_SECURE) ? szHttps : szHttp;
4094 urlComponents.dwSchemeLength = 0;
4095 urlComponents.lpszHostName = request->server->name;
4096 urlComponents.nPort = request->server->port;
4097 urlComponents.lpszUserName = session->userName;
4098 urlComponents.lpszUrlPath = request->path;
4100 b = InternetCreateUrlW(&urlComponents, 0, NULL, &url_length);
4101 if(!b && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
4102 orig_url = heap_alloc(url_length);
4104 /* convert from bytes to characters */
4105 url_length = url_length / sizeof(WCHAR) - 1;
4106 b = InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length);
4109 if(b) {
4110 url_length = 0;
4111 b = InternetCombineUrlW(orig_url, redirect_url, NULL, &url_length, ICU_ENCODE_SPACES_ONLY);
4112 if(!b && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
4113 combined_url = heap_alloc(url_length * sizeof(WCHAR));
4114 b = InternetCombineUrlW(orig_url, redirect_url, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY);
4115 if(!b) {
4116 heap_free(combined_url);
4117 combined_url = NULL;
4122 heap_free(orig_url);
4123 heap_free(redirect_url);
4124 return combined_url;
4128 /***********************************************************************
4129 * HTTP_HandleRedirect (internal)
4131 static DWORD HTTP_HandleRedirect(http_request_t *request, WCHAR *url)
4133 URL_COMPONENTSW urlComponents = { sizeof(urlComponents) };
4134 http_session_t *session = request->session;
4135 size_t url_len = lstrlenW(url);
4137 if(url[0] == '/')
4139 /* if it's an absolute path, keep the same session info */
4140 urlComponents.lpszUrlPath = url;
4141 urlComponents.dwUrlPathLength = url_len;
4143 else
4145 urlComponents.dwHostNameLength = 1;
4146 urlComponents.dwUserNameLength = 1;
4147 urlComponents.dwUrlPathLength = 1;
4148 if(!InternetCrackUrlW(url, url_len, 0, &urlComponents))
4149 return INTERNET_GetLastError();
4151 if(!urlComponents.dwHostNameLength)
4152 return ERROR_INTERNET_INVALID_URL;
4155 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REDIRECT,
4156 url, (url_len + 1) * sizeof(WCHAR));
4158 if(urlComponents.dwHostNameLength) {
4159 BOOL custom_port = FALSE;
4160 substr_t host;
4162 if(urlComponents.nScheme == INTERNET_SCHEME_HTTP) {
4163 if(request->hdr.dwFlags & INTERNET_FLAG_SECURE) {
4164 TRACE("redirect from secure page to non-secure page\n");
4165 /* FIXME: warn about from secure redirect to non-secure page */
4166 request->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
4169 custom_port = urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT;
4170 }else if(urlComponents.nScheme == INTERNET_SCHEME_HTTPS) {
4171 if(!(request->hdr.dwFlags & INTERNET_FLAG_SECURE)) {
4172 TRACE("redirect from non-secure page to secure page\n");
4173 /* FIXME: notify about redirect to secure page */
4174 request->hdr.dwFlags |= INTERNET_FLAG_SECURE;
4177 custom_port = urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT;
4180 heap_free(session->hostName);
4182 session->hostName = heap_strndupW(urlComponents.lpszHostName, urlComponents.dwHostNameLength);
4183 session->hostPort = urlComponents.nPort;
4185 heap_free(session->userName);
4186 session->userName = NULL;
4187 if (urlComponents.dwUserNameLength)
4188 session->userName = heap_strndupW(urlComponents.lpszUserName, urlComponents.dwUserNameLength);
4190 reset_data_stream(request);
4192 host = substr(urlComponents.lpszHostName, urlComponents.dwHostNameLength);
4194 if(host.len != lstrlenW(request->server->name) || wcsnicmp(request->server->name, host.str, host.len)
4195 || request->server->port != urlComponents.nPort) {
4196 server_t *new_server;
4198 new_server = get_server(host, urlComponents.nPort, urlComponents.nScheme == INTERNET_SCHEME_HTTPS, TRUE);
4199 server_release(request->server);
4200 request->server = new_server;
4203 if (custom_port)
4204 HTTP_ProcessHeader(request, L"Host", request->server->host_port,
4205 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
4206 else
4207 HTTP_ProcessHeader(request, L"Host", request->server->name,
4208 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
4211 heap_free(request->path);
4212 request->path = NULL;
4213 if(urlComponents.dwUrlPathLength)
4215 DWORD needed = 1;
4216 HRESULT rc;
4217 WCHAR dummy[] = L"";
4218 WCHAR *path;
4220 path = heap_strndupW(urlComponents.lpszUrlPath, urlComponents.dwUrlPathLength);
4221 rc = UrlEscapeW(path, dummy, &needed, URL_ESCAPE_SPACES_ONLY);
4222 if (rc != E_POINTER)
4223 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
4224 request->path = heap_alloc(needed*sizeof(WCHAR));
4225 rc = UrlEscapeW(path, request->path, &needed,
4226 URL_ESCAPE_SPACES_ONLY);
4227 if (rc != S_OK)
4229 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
4230 lstrcpyW(request->path, path);
4232 heap_free(path);
4235 /* Remove custom content-type/length headers on redirects. */
4236 remove_header(request, L"Content-Type", TRUE);
4237 remove_header(request, L"Content-Length", TRUE);
4239 return ERROR_SUCCESS;
4242 /***********************************************************************
4243 * HTTP_build_req (internal)
4245 * concatenate all the strings in the request together
4247 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
4249 LPCWSTR *t;
4250 LPWSTR str;
4252 for( t = list; *t ; t++ )
4253 len += lstrlenW( *t );
4254 len++;
4256 str = heap_alloc(len*sizeof(WCHAR));
4257 *str = 0;
4259 for( t = list; *t ; t++ )
4260 lstrcatW( str, *t );
4262 return str;
4265 static void HTTP_InsertCookies(http_request_t *request)
4267 WCHAR *cookies;
4268 DWORD res;
4270 res = get_cookie_header(request->server->name, request->path, &cookies);
4271 if(res != ERROR_SUCCESS || !cookies)
4272 return;
4274 HTTP_HttpAddRequestHeadersW(request, cookies, lstrlenW(cookies),
4275 HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
4276 heap_free(cookies);
4279 static WORD HTTP_ParseWkday(LPCWSTR day)
4281 static const WCHAR days[7][4] = {L"sun",
4282 L"mon",
4283 L"tue",
4284 L"wed",
4285 L"thu",
4286 L"fri",
4287 L"sat"};
4288 unsigned int i;
4289 for (i = 0; i < ARRAY_SIZE(days); i++)
4290 if (!wcsicmp(day, days[i]))
4291 return i;
4293 /* Invalid */
4294 return 7;
4297 static WORD HTTP_ParseMonth(LPCWSTR month)
4299 if (!wcsicmp(month, L"jan")) return 1;
4300 if (!wcsicmp(month, L"feb")) return 2;
4301 if (!wcsicmp(month, L"mar")) return 3;
4302 if (!wcsicmp(month, L"apr")) return 4;
4303 if (!wcsicmp(month, L"may")) return 5;
4304 if (!wcsicmp(month, L"jun")) return 6;
4305 if (!wcsicmp(month, L"jul")) return 7;
4306 if (!wcsicmp(month, L"aug")) return 8;
4307 if (!wcsicmp(month, L"sep")) return 9;
4308 if (!wcsicmp(month, L"oct")) return 10;
4309 if (!wcsicmp(month, L"nov")) return 11;
4310 if (!wcsicmp(month, L"dec")) return 12;
4311 /* Invalid */
4312 return 0;
4315 /* Parses the string pointed to by *str, assumed to be a 24-hour time HH:MM:SS,
4316 * optionally preceded by whitespace.
4317 * Upon success, returns TRUE, sets the wHour, wMinute, and wSecond fields of
4318 * st, and sets *str to the first character after the time format.
4320 static BOOL HTTP_ParseTime(SYSTEMTIME *st, LPCWSTR *str)
4322 LPCWSTR ptr = *str;
4323 WCHAR *nextPtr;
4324 unsigned long num;
4326 while (iswspace(*ptr))
4327 ptr++;
4329 num = wcstoul(ptr, &nextPtr, 10);
4330 if (!nextPtr || nextPtr <= ptr || *nextPtr != ':')
4332 ERR("unexpected time format %s\n", debugstr_w(ptr));
4333 return FALSE;
4335 if (num > 23)
4337 ERR("unexpected hour in time format %s\n", debugstr_w(ptr));
4338 return FALSE;
4340 ptr = nextPtr + 1;
4341 st->wHour = (WORD)num;
4342 num = wcstoul(ptr, &nextPtr, 10);
4343 if (!nextPtr || nextPtr <= ptr || *nextPtr != ':')
4345 ERR("unexpected time format %s\n", debugstr_w(ptr));
4346 return FALSE;
4348 if (num > 59)
4350 ERR("unexpected minute in time format %s\n", debugstr_w(ptr));
4351 return FALSE;
4353 ptr = nextPtr + 1;
4354 st->wMinute = (WORD)num;
4355 num = wcstoul(ptr, &nextPtr, 10);
4356 if (!nextPtr || nextPtr <= ptr)
4358 ERR("unexpected time format %s\n", debugstr_w(ptr));
4359 return FALSE;
4361 if (num > 59)
4363 ERR("unexpected second in time format %s\n", debugstr_w(ptr));
4364 return FALSE;
4366 *str = nextPtr;
4367 st->wSecond = (WORD)num;
4368 return TRUE;
4371 static BOOL HTTP_ParseDateAsAsctime(LPCWSTR value, FILETIME *ft)
4373 WCHAR day[4], *dayPtr, month[4], *monthPtr, *nextPtr;
4374 LPCWSTR ptr;
4375 SYSTEMTIME st = { 0 };
4376 unsigned long num;
4378 for (ptr = value, dayPtr = day; *ptr && !iswspace(*ptr) &&
4379 dayPtr - day < ARRAY_SIZE(day) - 1; ptr++, dayPtr++)
4380 *dayPtr = *ptr;
4381 *dayPtr = 0;
4382 st.wDayOfWeek = HTTP_ParseWkday(day);
4383 if (st.wDayOfWeek >= 7)
4385 ERR("unexpected weekday %s\n", debugstr_w(day));
4386 return FALSE;
4389 while (iswspace(*ptr))
4390 ptr++;
4392 for (monthPtr = month; !iswspace(*ptr) && monthPtr - month < ARRAY_SIZE(month) - 1;
4393 monthPtr++, ptr++)
4394 *monthPtr = *ptr;
4395 *monthPtr = 0;
4396 st.wMonth = HTTP_ParseMonth(month);
4397 if (!st.wMonth || st.wMonth > 12)
4399 ERR("unexpected month %s\n", debugstr_w(month));
4400 return FALSE;
4403 while (iswspace(*ptr))
4404 ptr++;
4406 num = wcstoul(ptr, &nextPtr, 10);
4407 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4409 ERR("unexpected day %s\n", debugstr_w(ptr));
4410 return FALSE;
4412 ptr = nextPtr;
4413 st.wDay = (WORD)num;
4415 while (iswspace(*ptr))
4416 ptr++;
4418 if (!HTTP_ParseTime(&st, &ptr))
4419 return FALSE;
4421 while (iswspace(*ptr))
4422 ptr++;
4424 num = wcstoul(ptr, &nextPtr, 10);
4425 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4427 ERR("unexpected year %s\n", debugstr_w(ptr));
4428 return FALSE;
4430 ptr = nextPtr;
4431 st.wYear = (WORD)num;
4433 while (iswspace(*ptr))
4434 ptr++;
4436 /* asctime() doesn't report a timezone, but some web servers do, so accept
4437 * with or without GMT.
4439 if (*ptr && wcscmp(ptr, L"GMT"))
4441 ERR("unexpected timezone %s\n", debugstr_w(ptr));
4442 return FALSE;
4444 return SystemTimeToFileTime(&st, ft);
4447 static BOOL HTTP_ParseRfc1123Date(LPCWSTR value, FILETIME *ft)
4449 WCHAR *nextPtr, day[4], month[4], *monthPtr;
4450 LPCWSTR ptr;
4451 unsigned long num;
4452 SYSTEMTIME st = { 0 };
4454 ptr = wcschr(value, ',');
4455 if (!ptr)
4456 return FALSE;
4457 if (ptr - value != 3)
4459 WARN("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4460 return FALSE;
4462 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4463 day[3] = 0;
4464 st.wDayOfWeek = HTTP_ParseWkday(day);
4465 if (st.wDayOfWeek > 6)
4467 WARN("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4468 return FALSE;
4470 ptr++;
4472 while (iswspace(*ptr))
4473 ptr++;
4475 num = wcstoul(ptr, &nextPtr, 10);
4476 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4478 WARN("unexpected day %s\n", debugstr_w(value));
4479 return FALSE;
4481 ptr = nextPtr;
4482 st.wDay = (WORD)num;
4484 while (iswspace(*ptr))
4485 ptr++;
4487 for (monthPtr = month; !iswspace(*ptr) && monthPtr - month < ARRAY_SIZE(month) - 1;
4488 monthPtr++, ptr++)
4489 *monthPtr = *ptr;
4490 *monthPtr = 0;
4491 st.wMonth = HTTP_ParseMonth(month);
4492 if (!st.wMonth || st.wMonth > 12)
4494 WARN("unexpected month %s\n", debugstr_w(month));
4495 return FALSE;
4498 while (iswspace(*ptr))
4499 ptr++;
4501 num = wcstoul(ptr, &nextPtr, 10);
4502 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4504 ERR("unexpected year %s\n", debugstr_w(value));
4505 return FALSE;
4507 ptr = nextPtr;
4508 st.wYear = (WORD)num;
4510 if (!HTTP_ParseTime(&st, &ptr))
4511 return FALSE;
4513 while (iswspace(*ptr))
4514 ptr++;
4516 if (wcscmp(ptr, L"GMT"))
4518 ERR("unexpected time zone %s\n", debugstr_w(ptr));
4519 return FALSE;
4521 return SystemTimeToFileTime(&st, ft);
4524 static WORD HTTP_ParseWeekday(LPCWSTR day)
4526 static const WCHAR days[7][10] = {L"sunday",
4527 L"monday",
4528 L"tuesday",
4529 L"wednesday",
4530 L"thursday",
4531 L"friday",
4532 L"saturday"};
4533 unsigned int i;
4534 for (i = 0; i < ARRAY_SIZE(days); i++)
4535 if (!wcsicmp(day, days[i]))
4536 return i;
4538 /* Invalid */
4539 return 7;
4542 static BOOL HTTP_ParseRfc850Date(LPCWSTR value, FILETIME *ft)
4544 WCHAR *nextPtr, day[10], month[4], *monthPtr;
4545 LPCWSTR ptr;
4546 unsigned long num;
4547 SYSTEMTIME st = { 0 };
4549 ptr = wcschr(value, ',');
4550 if (!ptr)
4551 return FALSE;
4552 if (ptr - value == 3)
4554 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4555 day[3] = 0;
4556 st.wDayOfWeek = HTTP_ParseWkday(day);
4557 if (st.wDayOfWeek > 6)
4559 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4560 return FALSE;
4563 else if (ptr - value < ARRAY_SIZE(day))
4565 memcpy(day, value, (ptr - value) * sizeof(WCHAR));
4566 day[ptr - value + 1] = 0;
4567 st.wDayOfWeek = HTTP_ParseWeekday(day);
4568 if (st.wDayOfWeek > 6)
4570 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4571 return FALSE;
4574 else
4576 ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value));
4577 return FALSE;
4579 ptr++;
4581 while (iswspace(*ptr))
4582 ptr++;
4584 num = wcstoul(ptr, &nextPtr, 10);
4585 if (!nextPtr || nextPtr <= ptr || !num || num > 31)
4587 ERR("unexpected day %s\n", debugstr_w(value));
4588 return FALSE;
4590 ptr = nextPtr;
4591 st.wDay = (WORD)num;
4593 if (*ptr != '-')
4595 ERR("unexpected month format %s\n", debugstr_w(ptr));
4596 return FALSE;
4598 ptr++;
4600 for (monthPtr = month; *ptr != '-' && monthPtr - month < ARRAY_SIZE(month) - 1;
4601 monthPtr++, ptr++)
4602 *monthPtr = *ptr;
4603 *monthPtr = 0;
4604 st.wMonth = HTTP_ParseMonth(month);
4605 if (!st.wMonth || st.wMonth > 12)
4607 ERR("unexpected month %s\n", debugstr_w(month));
4608 return FALSE;
4611 if (*ptr != '-')
4613 ERR("unexpected year format %s\n", debugstr_w(ptr));
4614 return FALSE;
4616 ptr++;
4618 num = wcstoul(ptr, &nextPtr, 10);
4619 if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827)
4621 ERR("unexpected year %s\n", debugstr_w(value));
4622 return FALSE;
4624 ptr = nextPtr;
4625 st.wYear = (WORD)num;
4627 if (!HTTP_ParseTime(&st, &ptr))
4628 return FALSE;
4630 while (iswspace(*ptr))
4631 ptr++;
4633 if (wcscmp(ptr, L"GMT"))
4635 ERR("unexpected time zone %s\n", debugstr_w(ptr));
4636 return FALSE;
4638 return SystemTimeToFileTime(&st, ft);
4641 static BOOL HTTP_ParseDate(LPCWSTR value, FILETIME *ft)
4643 BOOL ret;
4645 if (!wcscmp(value, L"0"))
4647 ft->dwLowDateTime = ft->dwHighDateTime = 0;
4648 ret = TRUE;
4650 else if (wcschr(value, ','))
4652 ret = HTTP_ParseRfc1123Date(value, ft);
4653 if (!ret)
4655 ret = HTTP_ParseRfc850Date(value, ft);
4656 if (!ret)
4657 ERR("unexpected date format %s\n", debugstr_w(value));
4660 else
4662 ret = HTTP_ParseDateAsAsctime(value, ft);
4663 if (!ret)
4664 ERR("unexpected date format %s\n", debugstr_w(value));
4666 return ret;
4669 static void HTTP_ProcessExpires(http_request_t *request)
4671 BOOL expirationFound = FALSE;
4672 int headerIndex;
4674 EnterCriticalSection( &request->headers_section );
4676 /* Look for a Cache-Control header with a max-age directive, as it takes
4677 * precedence over the Expires header.
4679 headerIndex = HTTP_GetCustomHeaderIndex(request, L"Cache-Control", 0, FALSE);
4680 if (headerIndex != -1)
4682 LPHTTPHEADERW ccHeader = &request->custHeaders[headerIndex];
4683 LPWSTR ptr;
4685 for (ptr = ccHeader->lpszValue; ptr && *ptr; )
4687 LPWSTR comma = wcschr(ptr, ','), end, equal;
4689 if (comma)
4690 end = comma;
4691 else
4692 end = ptr + lstrlenW(ptr);
4693 for (equal = end - 1; equal > ptr && *equal != '='; equal--)
4695 if (*equal == '=')
4697 if (!wcsnicmp(ptr, L"max-age", equal - ptr - 1))
4699 LPWSTR nextPtr;
4700 unsigned long age;
4702 age = wcstoul(equal + 1, &nextPtr, 10);
4703 if (nextPtr > equal + 1)
4705 LARGE_INTEGER ft;
4707 NtQuerySystemTime( &ft );
4708 /* Age is in seconds, FILETIME resolution is in
4709 * 100 nanosecond intervals.
4711 ft.QuadPart += age * (ULONGLONG)1000000;
4712 request->expires.dwLowDateTime = ft.u.LowPart;
4713 request->expires.dwHighDateTime = ft.u.HighPart;
4714 expirationFound = TRUE;
4718 if (comma)
4720 ptr = comma + 1;
4721 while (iswspace(*ptr))
4722 ptr++;
4724 else
4725 ptr = NULL;
4728 if (!expirationFound)
4730 headerIndex = HTTP_GetCustomHeaderIndex(request, L"Expires", 0, FALSE);
4731 if (headerIndex != -1)
4733 LPHTTPHEADERW expiresHeader = &request->custHeaders[headerIndex];
4734 FILETIME ft;
4736 if (HTTP_ParseDate(expiresHeader->lpszValue, &ft))
4738 expirationFound = TRUE;
4739 request->expires = ft;
4743 if (!expirationFound)
4745 LARGE_INTEGER t;
4747 /* With no known age, default to 10 minutes until expiration. */
4748 NtQuerySystemTime( &t );
4749 t.QuadPart += 10 * 60 * (ULONGLONG)10000000;
4750 request->expires.dwLowDateTime = t.u.LowPart;
4751 request->expires.dwHighDateTime = t.u.HighPart;
4754 LeaveCriticalSection( &request->headers_section );
4757 static void HTTP_ProcessLastModified(http_request_t *request)
4759 int headerIndex;
4761 EnterCriticalSection( &request->headers_section );
4763 headerIndex = HTTP_GetCustomHeaderIndex(request, L"Last-Modified", 0, FALSE);
4764 if (headerIndex != -1)
4766 LPHTTPHEADERW expiresHeader = &request->custHeaders[headerIndex];
4767 FILETIME ft;
4769 if (HTTP_ParseDate(expiresHeader->lpszValue, &ft))
4770 request->last_modified = ft;
4773 LeaveCriticalSection( &request->headers_section );
4776 static void http_process_keep_alive(http_request_t *req)
4778 int index;
4780 EnterCriticalSection( &req->headers_section );
4782 if ((index = HTTP_GetCustomHeaderIndex(req, L"Connection", 0, FALSE)) != -1)
4783 req->netconn->keep_alive = !wcsicmp(req->custHeaders[index].lpszValue, L"Keep-Alive");
4784 else if ((index = HTTP_GetCustomHeaderIndex(req, L"Proxy-Connection", 0, FALSE)) != -1)
4785 req->netconn->keep_alive = !wcsicmp(req->custHeaders[index].lpszValue, L"Keep-Alive");
4786 else
4787 req->netconn->keep_alive = !wcsicmp(req->version, L"HTTP/1.1");
4789 LeaveCriticalSection( &req->headers_section );
4792 static DWORD open_http_connection(http_request_t *request, BOOL *reusing)
4794 netconn_t *netconn = NULL;
4795 DWORD res;
4797 if (request->netconn)
4799 if (NETCON_is_alive(request->netconn) && drain_content(request, TRUE) == ERROR_SUCCESS)
4801 reset_data_stream(request);
4802 *reusing = TRUE;
4803 return ERROR_SUCCESS;
4806 TRACE("freeing netconn\n");
4807 free_netconn(request->netconn);
4808 request->netconn = NULL;
4811 reset_data_stream(request);
4813 res = HTTP_ResolveName(request);
4814 if(res != ERROR_SUCCESS)
4815 return res;
4817 EnterCriticalSection(&connection_pool_cs);
4819 while(!list_empty(&request->server->conn_pool)) {
4820 netconn = LIST_ENTRY(list_head(&request->server->conn_pool), netconn_t, pool_entry);
4821 list_remove(&netconn->pool_entry);
4823 if(is_valid_netconn(netconn) && NETCON_is_alive(netconn))
4824 break;
4826 TRACE("connection %p closed during idle\n", netconn);
4827 free_netconn(netconn);
4828 netconn = NULL;
4831 LeaveCriticalSection(&connection_pool_cs);
4833 if(netconn) {
4834 TRACE("<-- reusing %p netconn\n", netconn);
4835 request->netconn = netconn;
4836 *reusing = TRUE;
4837 return ERROR_SUCCESS;
4840 TRACE("connecting to %s, proxy %s\n", debugstr_w(request->server->name),
4841 request->proxy ? debugstr_w(request->proxy->name) : "(null)");
4843 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
4844 INTERNET_STATUS_CONNECTING_TO_SERVER,
4845 request->server->addr_str,
4846 strlen(request->server->addr_str)+1);
4848 res = create_netconn(request->proxy ? request->proxy : request->server, request->security_flags,
4849 (request->hdr.ErrorMask & INTERNET_ERROR_MASK_COMBINED_SEC_CERT) != 0,
4850 request->connect_timeout, &netconn);
4851 if(res != ERROR_SUCCESS) {
4852 ERR("create_netconn failed: %u\n", res);
4853 return res;
4856 request->netconn = netconn;
4858 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
4859 INTERNET_STATUS_CONNECTED_TO_SERVER,
4860 request->server->addr_str, strlen(request->server->addr_str)+1);
4862 *reusing = FALSE;
4863 TRACE("Created connection to %s: %p\n", debugstr_w(request->server->name), netconn);
4864 return ERROR_SUCCESS;
4867 static char *build_ascii_request( const WCHAR *str, void *data, DWORD data_len, DWORD *out_len )
4869 int len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
4870 char *ret;
4872 if (!(ret = heap_alloc( len + data_len ))) return NULL;
4873 WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
4874 if (data_len) memcpy( ret + len - 1, data, data_len );
4875 *out_len = len + data_len - 1;
4876 ret[*out_len] = 0;
4877 return ret;
4880 static void set_content_length_header( http_request_t *request, DWORD len, DWORD flags )
4882 WCHAR buf[ARRAY_SIZE(L"Content-Length: %u\r\n") + 10];
4884 swprintf( buf, ARRAY_SIZE(buf), L"Content-Length: %u\r\n", len );
4885 HTTP_HttpAddRequestHeadersW( request, buf, ~0u, flags );
4888 /***********************************************************************
4889 * HTTP_HttpSendRequestW (internal)
4891 * Sends the specified request to the HTTP server
4893 * RETURNS
4894 * ERROR_SUCCESS on success
4895 * win32 error code on failure
4898 static DWORD HTTP_HttpSendRequestW(http_request_t *request, LPCWSTR lpszHeaders,
4899 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
4900 DWORD dwContentLength, BOOL bEndRequest)
4902 BOOL redirected = FALSE, secure_proxy_connect = FALSE, loop_next;
4903 WCHAR *request_header = NULL;
4904 INT responseLen, cnt;
4905 DWORD res;
4907 TRACE("--> %p\n", request);
4909 assert(request->hdr.htype == WH_HHTTPREQ);
4911 /* if the verb is NULL default to GET */
4912 if (!request->verb)
4913 request->verb = heap_strdupW(L"GET");
4915 HTTP_ProcessHeader(request, L"Host", request->server->canon_host_port,
4916 HTTP_ADDREQ_FLAG_ADD_IF_NEW | HTTP_ADDHDR_FLAG_REQ);
4918 if (dwContentLength || wcscmp(request->verb, L"GET"))
4920 set_content_length_header(request, dwContentLength, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4921 request->bytesToWrite = dwContentLength;
4923 if (request->session->appInfo->agent)
4925 WCHAR *agent_header;
4926 int len;
4928 len = lstrlenW(request->session->appInfo->agent) + lstrlenW(L"User-Agent: %s\r\n");
4929 agent_header = heap_alloc(len * sizeof(WCHAR));
4930 swprintf(agent_header, len, L"User-Agent: %s\r\n", request->session->appInfo->agent);
4932 HTTP_HttpAddRequestHeadersW(request, agent_header, lstrlenW(agent_header), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4933 heap_free(agent_header);
4935 if (request->hdr.dwFlags & INTERNET_FLAG_PRAGMA_NOCACHE)
4937 HTTP_HttpAddRequestHeadersW(request, L"Pragma: no-cache\r\n",
4938 lstrlenW(L"Pragma: no-cache\r\n"), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4940 if ((request->hdr.dwFlags & INTERNET_FLAG_NO_CACHE_WRITE) && wcscmp(request->verb, L"GET"))
4942 HTTP_HttpAddRequestHeadersW(request, L"Cache-Control: no-cache\r\n",
4943 lstrlenW(L"Cache-Control: no-cache\r\n"), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
4946 /* add the headers the caller supplied */
4947 if( lpszHeaders && dwHeaderLength )
4948 HTTP_HttpAddRequestHeadersW(request, lpszHeaders, dwHeaderLength, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
4952 DWORD len, data_len = dwOptionalLength;
4953 BOOL reusing_connection;
4954 char *ascii_req;
4956 loop_next = FALSE;
4958 if(redirected) {
4959 request->contentLength = ~0;
4960 request->bytesToWrite = 0;
4963 if (TRACE_ON(wininet))
4965 HTTPHEADERW *host;
4967 EnterCriticalSection( &request->headers_section );
4968 host = HTTP_GetHeader(request, L"Host");
4969 TRACE("Going to url %s %s\n", debugstr_w(host->lpszValue), debugstr_w(request->path));
4970 LeaveCriticalSection( &request->headers_section );
4973 HTTP_FixURL(request);
4974 if (request->hdr.dwFlags & INTERNET_FLAG_KEEP_CONNECTION)
4976 HTTP_ProcessHeader(request, L"Connection", L"Keep-Alive",
4977 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_ADD);
4979 HTTP_InsertAuthorization(request, request->authInfo, L"Authorization");
4980 HTTP_InsertAuthorization(request, request->proxyAuthInfo, L"Proxy-Authorization");
4982 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES))
4983 HTTP_InsertCookies(request);
4985 res = open_http_connection(request, &reusing_connection);
4986 if (res != ERROR_SUCCESS)
4987 break;
4989 if (!reusing_connection && (request->hdr.dwFlags & INTERNET_FLAG_SECURE))
4991 if (request->proxy) secure_proxy_connect = TRUE;
4992 else
4994 res = NETCON_secure_connect(request->netconn, request->server);
4995 if (res != ERROR_SUCCESS)
4997 WARN("failed to upgrade to secure connection\n");
4998 http_release_netconn(request, FALSE);
4999 break;
5003 if (secure_proxy_connect)
5005 const WCHAR *target = request->server->host_port;
5007 if (HTTP_GetCustomHeaderIndex(request, L"Content-Length", 0, TRUE) >= 0)
5008 set_content_length_header(request, 0, HTTP_ADDREQ_FLAG_REPLACE);
5010 request_header = build_request_header(request, L"CONNECT", target, L"HTTP/1.1", TRUE);
5012 else if (request->proxy && !(request->hdr.dwFlags & INTERNET_FLAG_SECURE))
5014 WCHAR *url = build_proxy_path_url(request);
5015 request_header = build_request_header(request, request->verb, url, request->version, TRUE);
5016 heap_free(url);
5018 else
5020 if (request->proxy && HTTP_GetCustomHeaderIndex(request, L"Content-Length", 0, TRUE) >= 0)
5021 set_content_length_header(request, dwContentLength, HTTP_ADDREQ_FLAG_REPLACE);
5023 request_header = build_request_header(request, request->verb, request->path, request->version, TRUE);
5026 TRACE("Request header -> %s\n", debugstr_w(request_header) );
5028 /* send the request as ASCII, tack on the optional data */
5029 if (!lpOptional || redirected || secure_proxy_connect)
5030 data_len = 0;
5032 ascii_req = build_ascii_request(request_header, lpOptional, data_len, &len);
5033 heap_free(request_header);
5034 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
5036 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5037 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
5039 NETCON_set_timeout( request->netconn, TRUE, request->send_timeout );
5040 res = NETCON_send(request->netconn, ascii_req, len, 0, &cnt);
5041 heap_free( ascii_req );
5042 if(res != ERROR_SUCCESS) {
5043 TRACE("send failed: %u\n", res);
5044 if(!reusing_connection)
5045 break;
5046 http_release_netconn(request, FALSE);
5047 loop_next = TRUE;
5048 continue;
5051 request->bytesWritten = data_len;
5053 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5054 INTERNET_STATUS_REQUEST_SENT,
5055 &len, sizeof(DWORD));
5057 if (bEndRequest)
5059 DWORD dwBufferSize;
5061 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5062 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
5064 if (HTTP_GetResponseHeaders(request, &responseLen))
5066 http_release_netconn(request, FALSE);
5067 res = ERROR_INTERNET_CONNECTION_ABORTED;
5068 goto lend;
5070 /* FIXME: We should know that connection is closed before sending
5071 * headers. Otherwise wrong callbacks are executed */
5072 if(!responseLen && reusing_connection) {
5073 TRACE("Connection closed by server, reconnecting\n");
5074 http_release_netconn(request, FALSE);
5075 loop_next = TRUE;
5076 continue;
5079 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5080 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
5081 sizeof(DWORD));
5083 http_process_keep_alive(request);
5084 HTTP_ProcessCookies(request);
5085 HTTP_ProcessExpires(request);
5086 HTTP_ProcessLastModified(request);
5088 res = set_content_length(request);
5089 if(res != ERROR_SUCCESS)
5090 goto lend;
5091 if(!request->contentLength && !secure_proxy_connect)
5092 http_release_netconn(request, TRUE);
5094 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && responseLen)
5096 WCHAR *new_url;
5098 switch(request->status_code) {
5099 case HTTP_STATUS_REDIRECT:
5100 case HTTP_STATUS_MOVED:
5101 case HTTP_STATUS_REDIRECT_KEEP_VERB:
5102 case HTTP_STATUS_REDIRECT_METHOD:
5103 new_url = get_redirect_url(request);
5104 if(!new_url)
5105 break;
5107 if (wcscmp(request->verb, L"GET") && wcscmp(request->verb, L"HEAD") &&
5108 request->status_code != HTTP_STATUS_REDIRECT_KEEP_VERB)
5110 heap_free(request->verb);
5111 request->verb = heap_strdupW(L"GET");
5113 http_release_netconn(request, drain_content(request, FALSE) == ERROR_SUCCESS);
5114 res = HTTP_HandleRedirect(request, new_url);
5115 heap_free(new_url);
5116 if (res == ERROR_SUCCESS)
5117 loop_next = TRUE;
5118 redirected = TRUE;
5121 if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTH) && res == ERROR_SUCCESS)
5123 WCHAR szAuthValue[2048];
5124 dwBufferSize=2048;
5125 if (request->status_code == HTTP_STATUS_DENIED)
5127 WCHAR *host = heap_strdupW( request->server->canon_host_port );
5128 DWORD dwIndex = 0;
5129 while (HTTP_HttpQueryInfoW(request,HTTP_QUERY_WWW_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex) == ERROR_SUCCESS)
5131 if (HTTP_DoAuthorization(request, szAuthValue,
5132 &request->authInfo,
5133 request->session->userName,
5134 request->session->password, host))
5136 if (drain_content(request, TRUE) != ERROR_SUCCESS)
5138 FIXME("Could not drain content\n");
5139 http_release_netconn(request, FALSE);
5141 loop_next = TRUE;
5142 break;
5145 dwBufferSize = 2048;
5147 heap_free( host );
5149 if(!loop_next) {
5150 TRACE("Cleaning wrong authorization data\n");
5151 destroy_authinfo(request->authInfo);
5152 request->authInfo = NULL;
5155 if (request->status_code == HTTP_STATUS_PROXY_AUTH_REQ)
5157 DWORD dwIndex = 0;
5158 while (HTTP_HttpQueryInfoW(request,HTTP_QUERY_PROXY_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex) == ERROR_SUCCESS)
5160 if (HTTP_DoAuthorization(request, szAuthValue,
5161 &request->proxyAuthInfo,
5162 request->session->appInfo->proxyUsername,
5163 request->session->appInfo->proxyPassword,
5164 NULL))
5166 if (drain_content(request, TRUE) != ERROR_SUCCESS)
5168 FIXME("Could not drain content\n");
5169 http_release_netconn(request, FALSE);
5171 loop_next = TRUE;
5172 break;
5175 dwBufferSize = 2048;
5178 if(!loop_next) {
5179 TRACE("Cleaning wrong proxy authorization data\n");
5180 destroy_authinfo(request->proxyAuthInfo);
5181 request->proxyAuthInfo = NULL;
5185 if (secure_proxy_connect && request->status_code == HTTP_STATUS_OK)
5187 res = NETCON_secure_connect(request->netconn, request->server);
5188 if (res != ERROR_SUCCESS)
5190 WARN("failed to upgrade to secure proxy connection\n");
5191 http_release_netconn( request, FALSE );
5192 break;
5194 remove_header(request, L"Proxy-Authorization", TRUE);
5195 destroy_authinfo(request->proxyAuthInfo);
5196 request->proxyAuthInfo = NULL;
5197 request->contentLength = 0;
5198 request->netconn_stream.content_length = 0;
5200 secure_proxy_connect = FALSE;
5201 loop_next = TRUE;
5204 else
5205 res = ERROR_SUCCESS;
5207 while (loop_next);
5209 lend:
5210 /* TODO: send notification for P3P header */
5212 if(res == ERROR_SUCCESS)
5213 create_cache_entry(request);
5215 if (request->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5217 if (res == ERROR_SUCCESS) {
5218 if(bEndRequest && request->contentLength && request->bytesWritten == request->bytesToWrite)
5219 HTTP_ReceiveRequestData(request);
5220 else
5221 send_request_complete(request,
5222 request->session->hdr.dwInternalFlags & INET_OPENURL ? (DWORD_PTR)request->hdr.hInternet : 1, 0);
5223 }else {
5224 send_request_complete(request, 0, res);
5228 TRACE("<--\n");
5229 return res;
5232 typedef struct {
5233 task_header_t hdr;
5234 WCHAR *headers;
5235 DWORD headers_len;
5236 void *optional;
5237 DWORD optional_len;
5238 DWORD content_len;
5239 BOOL end_request;
5240 } send_request_task_t;
5242 /***********************************************************************
5244 * Helper functions for the HttpSendRequest(Ex) functions
5247 static void AsyncHttpSendRequestProc(task_header_t *hdr)
5249 send_request_task_t *task = (send_request_task_t*)hdr;
5250 http_request_t *request = (http_request_t*)task->hdr.hdr;
5252 TRACE("%p\n", request);
5254 HTTP_HttpSendRequestW(request, task->headers, task->headers_len, task->optional,
5255 task->optional_len, task->content_len, task->end_request);
5257 heap_free(task->headers);
5261 static DWORD HTTP_HttpEndRequestW(http_request_t *request, DWORD dwFlags, DWORD_PTR dwContext)
5263 INT responseLen;
5264 DWORD res = ERROR_SUCCESS;
5266 if(!is_valid_netconn(request->netconn)) {
5267 WARN("Not connected\n");
5268 send_request_complete(request, 0, ERROR_INTERNET_OPERATION_CANCELLED);
5269 return ERROR_INTERNET_OPERATION_CANCELLED;
5272 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5273 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
5275 if (HTTP_GetResponseHeaders(request, &responseLen) || !responseLen)
5276 res = ERROR_HTTP_HEADER_NOT_FOUND;
5278 INTERNET_SendCallback(&request->hdr, request->hdr.dwContext,
5279 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
5281 /* process cookies here. Is this right? */
5282 http_process_keep_alive(request);
5283 HTTP_ProcessCookies(request);
5284 HTTP_ProcessExpires(request);
5285 HTTP_ProcessLastModified(request);
5287 if ((res = set_content_length(request)) == ERROR_SUCCESS) {
5288 if(!request->contentLength)
5289 http_release_netconn(request, TRUE);
5292 if (res == ERROR_SUCCESS && !(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
5294 switch(request->status_code) {
5295 case HTTP_STATUS_REDIRECT:
5296 case HTTP_STATUS_MOVED:
5297 case HTTP_STATUS_REDIRECT_METHOD:
5298 case HTTP_STATUS_REDIRECT_KEEP_VERB: {
5299 WCHAR *new_url;
5301 new_url = get_redirect_url(request);
5302 if(!new_url)
5303 break;
5305 if (wcscmp(request->verb, L"GET") && wcscmp(request->verb, L"HEAD") &&
5306 request->status_code != HTTP_STATUS_REDIRECT_KEEP_VERB)
5308 heap_free(request->verb);
5309 request->verb = heap_strdupW(L"GET");
5311 http_release_netconn(request, drain_content(request, FALSE) == ERROR_SUCCESS);
5312 res = HTTP_HandleRedirect(request, new_url);
5313 heap_free(new_url);
5314 if (res == ERROR_SUCCESS)
5315 res = HTTP_HttpSendRequestW(request, NULL, 0, NULL, 0, 0, TRUE);
5320 if(res == ERROR_SUCCESS)
5321 create_cache_entry(request);
5323 if (res == ERROR_SUCCESS && request->contentLength)
5324 HTTP_ReceiveRequestData(request);
5325 else
5326 send_request_complete(request, res == ERROR_SUCCESS, res);
5328 return res;
5331 /***********************************************************************
5332 * HttpEndRequestA (WININET.@)
5334 * Ends an HTTP request that was started by HttpSendRequestEx
5336 * RETURNS
5337 * TRUE if successful
5338 * FALSE on failure
5341 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
5342 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
5344 TRACE("(%p, %p, %08x, %08lx)\n", hRequest, lpBuffersOut, dwFlags, dwContext);
5346 if (lpBuffersOut)
5348 SetLastError(ERROR_INVALID_PARAMETER);
5349 return FALSE;
5352 return HttpEndRequestW(hRequest, NULL, dwFlags, dwContext);
5355 typedef struct {
5356 task_header_t hdr;
5357 DWORD flags;
5358 DWORD context;
5359 } end_request_task_t;
5361 static void AsyncHttpEndRequestProc(task_header_t *hdr)
5363 end_request_task_t *task = (end_request_task_t*)hdr;
5364 http_request_t *req = (http_request_t*)task->hdr.hdr;
5366 TRACE("%p\n", req);
5368 HTTP_HttpEndRequestW(req, task->flags, task->context);
5371 /***********************************************************************
5372 * HttpEndRequestW (WININET.@)
5374 * Ends an HTTP request that was started by HttpSendRequestEx
5376 * RETURNS
5377 * TRUE if successful
5378 * FALSE on failure
5381 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
5382 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
5384 http_request_t *request;
5385 DWORD res;
5387 TRACE("%p %p %x %lx -->\n", hRequest, lpBuffersOut, dwFlags, dwContext);
5389 if (lpBuffersOut)
5391 SetLastError(ERROR_INVALID_PARAMETER);
5392 return FALSE;
5395 request = (http_request_t*) get_handle_object( hRequest );
5397 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5399 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
5400 if (request)
5401 WININET_Release( &request->hdr );
5402 return FALSE;
5404 request->hdr.dwFlags |= dwFlags;
5406 if (request->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5408 end_request_task_t *task;
5410 task = alloc_async_task(&request->hdr, AsyncHttpEndRequestProc, sizeof(*task));
5411 task->flags = dwFlags;
5412 task->context = dwContext;
5414 INTERNET_AsyncCall(&task->hdr);
5415 res = ERROR_IO_PENDING;
5417 else
5418 res = HTTP_HttpEndRequestW(request, dwFlags, dwContext);
5420 WININET_Release( &request->hdr );
5421 TRACE("%u <--\n", res);
5422 if(res != ERROR_SUCCESS)
5423 SetLastError(res);
5424 return res == ERROR_SUCCESS;
5427 /***********************************************************************
5428 * HttpSendRequestExA (WININET.@)
5430 * Sends the specified request to the HTTP server and allows chunked
5431 * transfers.
5433 * RETURNS
5434 * Success: TRUE
5435 * Failure: FALSE, call GetLastError() for more information.
5437 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
5438 LPINTERNET_BUFFERSA lpBuffersIn,
5439 LPINTERNET_BUFFERSA lpBuffersOut,
5440 DWORD dwFlags, DWORD_PTR dwContext)
5442 INTERNET_BUFFERSW BuffersInW;
5443 BOOL rc = FALSE;
5444 DWORD headerlen;
5445 LPWSTR header = NULL;
5447 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
5448 lpBuffersOut, dwFlags, dwContext);
5450 if (lpBuffersIn)
5452 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
5453 if (lpBuffersIn->lpcszHeader)
5455 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
5456 lpBuffersIn->dwHeadersLength,0,0);
5457 header = heap_alloc(headerlen*sizeof(WCHAR));
5458 if (!(BuffersInW.lpcszHeader = header))
5460 SetLastError(ERROR_OUTOFMEMORY);
5461 return FALSE;
5463 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
5464 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
5465 header, headerlen);
5467 else
5468 BuffersInW.lpcszHeader = NULL;
5469 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
5470 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
5471 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
5472 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
5473 BuffersInW.Next = NULL;
5476 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
5478 heap_free(header);
5479 return rc;
5482 /***********************************************************************
5483 * HttpSendRequestExW (WININET.@)
5485 * Sends the specified request to the HTTP server and allows chunked
5486 * transfers
5488 * RETURNS
5489 * Success: TRUE
5490 * Failure: FALSE, call GetLastError() for more information.
5492 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
5493 LPINTERNET_BUFFERSW lpBuffersIn,
5494 LPINTERNET_BUFFERSW lpBuffersOut,
5495 DWORD dwFlags, DWORD_PTR dwContext)
5497 http_request_t *request;
5498 http_session_t *session;
5499 appinfo_t *hIC;
5500 DWORD res;
5502 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
5503 lpBuffersOut, dwFlags, dwContext);
5505 request = (http_request_t*) get_handle_object( hRequest );
5507 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5509 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5510 goto lend;
5513 session = request->session;
5514 assert(session->hdr.htype == WH_HHTTPSESSION);
5515 hIC = session->appInfo;
5516 assert(hIC->hdr.htype == WH_HINIT);
5518 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5520 send_request_task_t *task;
5522 task = alloc_async_task(&request->hdr, AsyncHttpSendRequestProc, sizeof(*task));
5523 if (lpBuffersIn)
5525 DWORD size = 0;
5527 if (lpBuffersIn->lpcszHeader)
5529 if (lpBuffersIn->dwHeadersLength == ~0u)
5530 size = (lstrlenW( lpBuffersIn->lpcszHeader ) + 1) * sizeof(WCHAR);
5531 else
5532 size = lpBuffersIn->dwHeadersLength * sizeof(WCHAR);
5534 task->headers = heap_alloc(size);
5535 memcpy(task->headers, lpBuffersIn->lpcszHeader, size);
5537 else task->headers = NULL;
5539 task->headers_len = size / sizeof(WCHAR);
5540 task->optional = lpBuffersIn->lpvBuffer;
5541 task->optional_len = lpBuffersIn->dwBufferLength;
5542 task->content_len = lpBuffersIn->dwBufferTotal;
5544 else
5546 task->headers = NULL;
5547 task->headers_len = 0;
5548 task->optional = NULL;
5549 task->optional_len = 0;
5550 task->content_len = 0;
5553 task->end_request = FALSE;
5555 INTERNET_AsyncCall(&task->hdr);
5556 res = ERROR_IO_PENDING;
5558 else
5560 if (lpBuffersIn)
5561 res = HTTP_HttpSendRequestW(request, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
5562 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
5563 lpBuffersIn->dwBufferTotal, FALSE);
5564 else
5565 res = HTTP_HttpSendRequestW(request, NULL, 0, NULL, 0, 0, FALSE);
5568 lend:
5569 if ( request )
5570 WININET_Release( &request->hdr );
5572 TRACE("<---\n");
5573 SetLastError(res);
5574 return res == ERROR_SUCCESS;
5577 /***********************************************************************
5578 * HttpSendRequestW (WININET.@)
5580 * Sends the specified request to the HTTP server
5582 * RETURNS
5583 * TRUE on success
5584 * FALSE on failure
5587 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
5588 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
5590 http_request_t *request;
5591 http_session_t *session = NULL;
5592 appinfo_t *hIC = NULL;
5593 DWORD res = ERROR_SUCCESS;
5595 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest,
5596 debugstr_wn(lpszHeaders, dwHeaderLength), dwHeaderLength, lpOptional, dwOptionalLength);
5598 request = (http_request_t*) get_handle_object( hHttpRequest );
5599 if (NULL == request || request->hdr.htype != WH_HHTTPREQ)
5601 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5602 goto lend;
5605 session = request->session;
5606 if (NULL == session || session->hdr.htype != WH_HHTTPSESSION)
5608 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5609 goto lend;
5612 hIC = session->appInfo;
5613 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
5615 res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE;
5616 goto lend;
5619 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
5621 send_request_task_t *task;
5623 task = alloc_async_task(&request->hdr, AsyncHttpSendRequestProc, sizeof(*task));
5624 if (lpszHeaders)
5626 DWORD size;
5628 if (dwHeaderLength == ~0u) size = (lstrlenW(lpszHeaders) + 1) * sizeof(WCHAR);
5629 else size = dwHeaderLength * sizeof(WCHAR);
5631 task->headers = heap_alloc(size);
5632 memcpy(task->headers, lpszHeaders, size);
5634 else
5635 task->headers = NULL;
5636 task->headers_len = dwHeaderLength;
5637 task->optional = lpOptional;
5638 task->optional_len = dwOptionalLength;
5639 task->content_len = dwOptionalLength;
5640 task->end_request = TRUE;
5642 INTERNET_AsyncCall(&task->hdr);
5643 res = ERROR_IO_PENDING;
5645 else
5647 res = HTTP_HttpSendRequestW(request, lpszHeaders,
5648 dwHeaderLength, lpOptional, dwOptionalLength,
5649 dwOptionalLength, TRUE);
5651 lend:
5652 if( request )
5653 WININET_Release( &request->hdr );
5655 SetLastError(res);
5656 return res == ERROR_SUCCESS;
5659 /***********************************************************************
5660 * HttpSendRequestA (WININET.@)
5662 * Sends the specified request to the HTTP server
5664 * RETURNS
5665 * TRUE on success
5666 * FALSE on failure
5669 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
5670 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
5672 BOOL result;
5673 LPWSTR szHeaders=NULL;
5674 DWORD nLen=dwHeaderLength;
5675 if(lpszHeaders!=NULL)
5677 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
5678 szHeaders = heap_alloc(nLen*sizeof(WCHAR));
5679 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
5681 result = HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
5682 heap_free(szHeaders);
5683 return result;
5686 /***********************************************************************
5687 * HTTPSESSION_Destroy (internal)
5689 * Deallocate session handle
5692 static void HTTPSESSION_Destroy(object_header_t *hdr)
5694 http_session_t *session = (http_session_t*) hdr;
5696 TRACE("%p\n", session);
5698 WININET_Release(&session->appInfo->hdr);
5700 heap_free(session->hostName);
5701 heap_free(session->password);
5702 heap_free(session->userName);
5705 static DWORD HTTPSESSION_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
5707 http_session_t *ses = (http_session_t *)hdr;
5709 switch(option) {
5710 case INTERNET_OPTION_HANDLE_TYPE:
5711 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
5713 if (*size < sizeof(ULONG))
5714 return ERROR_INSUFFICIENT_BUFFER;
5716 *size = sizeof(DWORD);
5717 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_CONNECT_HTTP;
5718 return ERROR_SUCCESS;
5719 case INTERNET_OPTION_CONNECT_TIMEOUT:
5720 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
5722 if (*size < sizeof(DWORD))
5723 return ERROR_INSUFFICIENT_BUFFER;
5725 *size = sizeof(DWORD);
5726 *(DWORD *)buffer = ses->connect_timeout;
5727 return ERROR_SUCCESS;
5729 case INTERNET_OPTION_SEND_TIMEOUT:
5730 TRACE("INTERNET_OPTION_SEND_TIMEOUT\n");
5732 if (*size < sizeof(DWORD))
5733 return ERROR_INSUFFICIENT_BUFFER;
5735 *size = sizeof(DWORD);
5736 *(DWORD *)buffer = ses->send_timeout;
5737 return ERROR_SUCCESS;
5739 case INTERNET_OPTION_RECEIVE_TIMEOUT:
5740 TRACE("INTERNET_OPTION_RECEIVE_TIMEOUT\n");
5742 if (*size < sizeof(DWORD))
5743 return ERROR_INSUFFICIENT_BUFFER;
5745 *size = sizeof(DWORD);
5746 *(DWORD *)buffer = ses->receive_timeout;
5747 return ERROR_SUCCESS;
5750 return INET_QueryOption(hdr, option, buffer, size, unicode);
5753 static DWORD HTTPSESSION_SetOption(object_header_t *hdr, DWORD option, void *buffer, DWORD size)
5755 http_session_t *ses = (http_session_t*)hdr;
5757 switch(option) {
5758 case INTERNET_OPTION_USERNAME:
5760 heap_free(ses->userName);
5761 if (!(ses->userName = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5762 return ERROR_SUCCESS;
5764 case INTERNET_OPTION_PASSWORD:
5766 heap_free(ses->password);
5767 if (!(ses->password = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5768 return ERROR_SUCCESS;
5770 case INTERNET_OPTION_PROXY_USERNAME:
5772 heap_free(ses->appInfo->proxyUsername);
5773 if (!(ses->appInfo->proxyUsername = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5774 return ERROR_SUCCESS;
5776 case INTERNET_OPTION_PROXY_PASSWORD:
5778 heap_free(ses->appInfo->proxyPassword);
5779 if (!(ses->appInfo->proxyPassword = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
5780 return ERROR_SUCCESS;
5782 case INTERNET_OPTION_CONNECT_TIMEOUT:
5784 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5785 ses->connect_timeout = *(DWORD *)buffer;
5786 return ERROR_SUCCESS;
5788 case INTERNET_OPTION_SEND_TIMEOUT:
5790 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5791 ses->send_timeout = *(DWORD *)buffer;
5792 return ERROR_SUCCESS;
5794 case INTERNET_OPTION_RECEIVE_TIMEOUT:
5796 if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER;
5797 ses->receive_timeout = *(DWORD *)buffer;
5798 return ERROR_SUCCESS;
5800 default: break;
5803 return INET_SetOption(hdr, option, buffer, size);
5806 static const object_vtbl_t HTTPSESSIONVtbl = {
5807 HTTPSESSION_Destroy,
5808 NULL,
5809 HTTPSESSION_QueryOption,
5810 HTTPSESSION_SetOption,
5811 NULL,
5812 NULL,
5813 NULL,
5814 NULL
5818 /***********************************************************************
5819 * HTTP_Connect (internal)
5821 * Create http session handle
5823 * RETURNS
5824 * HINTERNET a session handle on success
5825 * NULL on failure
5828 DWORD HTTP_Connect(appinfo_t *hIC, LPCWSTR lpszServerName,
5829 INTERNET_PORT serverPort, LPCWSTR lpszUserName,
5830 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
5831 DWORD dwInternalFlags, HINTERNET *ret)
5833 http_session_t *session = NULL;
5835 TRACE("-->\n");
5837 if (!lpszServerName || !lpszServerName[0])
5838 return ERROR_INVALID_PARAMETER;
5840 assert( hIC->hdr.htype == WH_HINIT );
5842 session = alloc_object(&hIC->hdr, &HTTPSESSIONVtbl, sizeof(http_session_t));
5843 if (!session)
5844 return ERROR_OUTOFMEMORY;
5847 * According to my tests. The name is not resolved until a request is sent
5850 session->hdr.htype = WH_HHTTPSESSION;
5851 session->hdr.dwFlags = dwFlags;
5852 session->hdr.dwContext = dwContext;
5853 session->hdr.dwInternalFlags |= dwInternalFlags;
5854 session->hdr.decoding = hIC->hdr.decoding;
5856 WININET_AddRef( &hIC->hdr );
5857 session->appInfo = hIC;
5858 list_add_head( &hIC->hdr.children, &session->hdr.entry );
5860 session->hostName = heap_strdupW(lpszServerName);
5861 if (lpszUserName && lpszUserName[0])
5862 session->userName = heap_strdupW(lpszUserName);
5863 session->password = heap_strdupW(lpszPassword);
5864 session->hostPort = serverPort;
5865 session->connect_timeout = hIC->connect_timeout;
5866 session->send_timeout = 0;
5867 session->receive_timeout = 0;
5869 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
5870 if (!(session->hdr.dwInternalFlags & INET_OPENURL))
5872 INTERNET_SendCallback(&hIC->hdr, dwContext,
5873 INTERNET_STATUS_HANDLE_CREATED, &session->hdr.hInternet,
5874 sizeof(HINTERNET));
5878 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
5879 * windows
5882 TRACE("%p --> %p\n", hIC, session);
5884 *ret = session->hdr.hInternet;
5885 return ERROR_SUCCESS;
5888 /***********************************************************************
5889 * HTTP_clear_response_headers (internal)
5891 * clear out any old response headers
5893 static void HTTP_clear_response_headers( http_request_t *request )
5895 DWORD i;
5897 EnterCriticalSection( &request->headers_section );
5899 for( i=0; i<request->nCustHeaders; i++)
5901 if( !request->custHeaders[i].lpszField )
5902 continue;
5903 if( !request->custHeaders[i].lpszValue )
5904 continue;
5905 if ( request->custHeaders[i].wFlags & HDR_ISREQUEST )
5906 continue;
5907 HTTP_DeleteCustomHeader( request, i );
5908 i--;
5911 LeaveCriticalSection( &request->headers_section );
5914 /***********************************************************************
5915 * HTTP_GetResponseHeaders (internal)
5917 * Read server response
5919 * RETURNS
5921 * TRUE on success
5922 * FALSE on error
5924 static DWORD HTTP_GetResponseHeaders(http_request_t *request, INT *len)
5926 INT cbreaks = 0;
5927 WCHAR buffer[MAX_REPLY_LEN];
5928 DWORD buflen = MAX_REPLY_LEN;
5929 INT rc = 0;
5930 char bufferA[MAX_REPLY_LEN];
5931 LPWSTR status_code = NULL, status_text = NULL;
5932 DWORD res = ERROR_HTTP_INVALID_SERVER_RESPONSE;
5933 BOOL codeHundred = FALSE;
5935 TRACE("-->\n");
5937 if(!is_valid_netconn(request->netconn))
5938 goto lend;
5940 /* clear old response headers (eg. from a redirect response) */
5941 HTTP_clear_response_headers( request );
5943 NETCON_set_timeout( request->netconn, FALSE, request->receive_timeout );
5944 do {
5946 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
5948 buflen = MAX_REPLY_LEN;
5949 if ((res = read_line(request, bufferA, &buflen)))
5950 goto lend;
5952 if (!buflen) goto lend;
5954 rc += buflen;
5955 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
5956 /* check is this a status code line? */
5957 if (!wcsncmp(buffer, L"HTTP/1.0", 4))
5959 /* split the version from the status code */
5960 status_code = wcschr( buffer, ' ' );
5961 if( !status_code )
5962 goto lend;
5963 *status_code++=0;
5965 /* split the status code from the status text */
5966 status_text = wcschr( status_code, ' ' );
5967 if( status_text )
5968 *status_text++=0;
5970 request->status_code = wcstol(status_code, NULL, 10);
5972 TRACE("version [%s] status code [%s] status text [%s]\n",
5973 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
5975 codeHundred = request->status_code == HTTP_STATUS_CONTINUE;
5977 else if (!codeHundred)
5979 WARN("No status line at head of response (%s)\n", debugstr_w(buffer));
5981 heap_free(request->version);
5982 heap_free(request->statusText);
5984 request->status_code = HTTP_STATUS_OK;
5985 request->version = heap_strdupW(L"HTTP/1.0");
5986 request->statusText = heap_strdupW(L"OK");
5988 goto lend;
5990 } while (codeHundred);
5992 /* Add status code */
5993 HTTP_ProcessHeader(request, L"Status", status_code,
5994 HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_ADD);
5996 heap_free(request->version);
5997 heap_free(request->statusText);
5999 request->version = heap_strdupW(buffer);
6000 request->statusText = heap_strdupW(status_text ? status_text : L"");
6002 /* Restore the spaces */
6003 *(status_code-1) = ' ';
6004 if (status_text)
6005 *(status_text-1) = ' ';
6007 /* Parse each response line */
6010 buflen = MAX_REPLY_LEN;
6011 if (!read_line(request, bufferA, &buflen) && buflen)
6013 LPWSTR * pFieldAndValue;
6015 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
6017 if (!bufferA[0]) break;
6018 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
6020 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
6021 if (pFieldAndValue)
6023 HTTP_ProcessHeader(request, pFieldAndValue[0], pFieldAndValue[1],
6024 HTTP_ADDREQ_FLAG_ADD );
6025 HTTP_FreeTokens(pFieldAndValue);
6028 else
6030 cbreaks++;
6031 if (cbreaks >= 2)
6032 break;
6034 }while(1);
6036 res = ERROR_SUCCESS;
6038 lend:
6040 *len = rc;
6041 TRACE("<--\n");
6042 return res;
6045 /***********************************************************************
6046 * HTTP_InterpretHttpHeader (internal)
6048 * Parse server response
6050 * RETURNS
6052 * Pointer to array of field, value, NULL on success.
6053 * NULL on error.
6055 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
6057 LPWSTR * pTokenPair;
6058 LPWSTR pszColon;
6059 INT len;
6061 pTokenPair = heap_alloc_zero(sizeof(*pTokenPair)*3);
6063 pszColon = wcschr(buffer, ':');
6064 /* must have two tokens */
6065 if (!pszColon)
6067 HTTP_FreeTokens(pTokenPair);
6068 if (buffer[0])
6069 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
6070 return NULL;
6073 pTokenPair[0] = heap_alloc((pszColon - buffer + 1) * sizeof(WCHAR));
6074 if (!pTokenPair[0])
6076 HTTP_FreeTokens(pTokenPair);
6077 return NULL;
6079 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
6080 pTokenPair[0][pszColon - buffer] = '\0';
6082 /* skip colon */
6083 pszColon++;
6084 len = lstrlenW(pszColon);
6085 pTokenPair[1] = heap_alloc((len + 1) * sizeof(WCHAR));
6086 if (!pTokenPair[1])
6088 HTTP_FreeTokens(pTokenPair);
6089 return NULL;
6091 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
6093 strip_spaces(pTokenPair[0]);
6094 strip_spaces(pTokenPair[1]);
6096 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
6097 return pTokenPair;
6100 /***********************************************************************
6101 * HTTP_ProcessHeader (internal)
6103 * Stuff header into header tables according to <dwModifier>
6107 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
6109 static DWORD HTTP_ProcessHeader(http_request_t *request, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
6111 LPHTTPHEADERW lphttpHdr = NULL;
6112 INT index;
6113 BOOL request_only = !!(dwModifier & HTTP_ADDHDR_FLAG_REQ);
6114 DWORD res = ERROR_HTTP_INVALID_HEADER;
6116 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), dwModifier);
6118 EnterCriticalSection( &request->headers_section );
6120 /* REPLACE wins out over ADD */
6121 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
6122 dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
6124 if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
6125 index = -1;
6126 else
6127 index = HTTP_GetCustomHeaderIndex(request, field, 0, request_only);
6129 if (index >= 0)
6131 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
6133 LeaveCriticalSection( &request->headers_section );
6134 return ERROR_HTTP_INVALID_HEADER;
6136 lphttpHdr = &request->custHeaders[index];
6138 else if (value)
6140 HTTPHEADERW hdr;
6142 hdr.lpszField = (LPWSTR)field;
6143 hdr.lpszValue = (LPWSTR)value;
6144 hdr.wFlags = hdr.wCount = 0;
6146 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6147 hdr.wFlags |= HDR_ISREQUEST;
6149 res = HTTP_InsertCustomHeader(request, &hdr);
6150 LeaveCriticalSection( &request->headers_section );
6151 return res;
6153 /* no value to delete */
6154 else
6156 LeaveCriticalSection( &request->headers_section );
6157 return ERROR_SUCCESS;
6160 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6161 lphttpHdr->wFlags |= HDR_ISREQUEST;
6162 else
6163 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
6165 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
6167 HTTP_DeleteCustomHeader( request, index );
6169 if (value && value[0])
6171 HTTPHEADERW hdr;
6173 hdr.lpszField = (LPWSTR)field;
6174 hdr.lpszValue = (LPWSTR)value;
6175 hdr.wFlags = hdr.wCount = 0;
6177 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
6178 hdr.wFlags |= HDR_ISREQUEST;
6180 res = HTTP_InsertCustomHeader(request, &hdr);
6181 LeaveCriticalSection( &request->headers_section );
6182 return res;
6185 LeaveCriticalSection( &request->headers_section );
6186 return ERROR_SUCCESS;
6188 else if (dwModifier & COALESCEFLAGS)
6190 LPWSTR lpsztmp;
6191 WCHAR ch = 0;
6192 INT len = 0;
6193 INT origlen = lstrlenW(lphttpHdr->lpszValue);
6194 INT valuelen = lstrlenW(value);
6196 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
6198 ch = ',';
6199 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
6201 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
6203 ch = ';';
6204 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
6207 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
6209 lpsztmp = heap_realloc(lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
6210 if (lpsztmp)
6212 lphttpHdr->lpszValue = lpsztmp;
6213 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
6214 if (ch > 0)
6216 lphttpHdr->lpszValue[origlen] = ch;
6217 origlen++;
6218 lphttpHdr->lpszValue[origlen] = ' ';
6219 origlen++;
6222 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
6223 lphttpHdr->lpszValue[len] = '\0';
6224 res = ERROR_SUCCESS;
6226 else
6228 WARN("heap_realloc (%d bytes) failed\n",len+1);
6229 res = ERROR_OUTOFMEMORY;
6232 TRACE("<-- %d\n", res);
6233 LeaveCriticalSection( &request->headers_section );
6234 return res;
6237 /***********************************************************************
6238 * HTTP_GetCustomHeaderIndex (internal)
6240 * Return index of custom header from header array
6241 * Headers section must be held
6243 static INT HTTP_GetCustomHeaderIndex(http_request_t *request, LPCWSTR lpszField,
6244 int requested_index, BOOL request_only)
6246 DWORD index;
6248 TRACE("%s, %d, %d\n", debugstr_w(lpszField), requested_index, request_only);
6250 for (index = 0; index < request->nCustHeaders; index++)
6252 if (wcsicmp(request->custHeaders[index].lpszField, lpszField))
6253 continue;
6255 if (request_only && !(request->custHeaders[index].wFlags & HDR_ISREQUEST))
6256 continue;
6258 if (!request_only && (request->custHeaders[index].wFlags & HDR_ISREQUEST))
6259 continue;
6261 if (requested_index == 0)
6262 break;
6263 requested_index --;
6266 if (index >= request->nCustHeaders)
6267 index = -1;
6269 TRACE("Return: %d\n", index);
6270 return index;
6274 /***********************************************************************
6275 * HTTP_InsertCustomHeader (internal)
6277 * Insert header into array
6278 * Headers section must be held
6280 static DWORD HTTP_InsertCustomHeader(http_request_t *request, LPHTTPHEADERW lpHdr)
6282 INT count;
6283 LPHTTPHEADERW lph = NULL;
6285 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
6286 count = request->nCustHeaders + 1;
6287 if (count > 1)
6288 lph = heap_realloc_zero(request->custHeaders, sizeof(HTTPHEADERW) * count);
6289 else
6290 lph = heap_alloc_zero(sizeof(HTTPHEADERW) * count);
6292 if (!lph)
6293 return ERROR_OUTOFMEMORY;
6295 request->custHeaders = lph;
6296 request->custHeaders[count-1].lpszField = heap_strdupW(lpHdr->lpszField);
6297 request->custHeaders[count-1].lpszValue = heap_strdupW(lpHdr->lpszValue);
6298 request->custHeaders[count-1].wFlags = lpHdr->wFlags;
6299 request->custHeaders[count-1].wCount= lpHdr->wCount;
6300 request->nCustHeaders++;
6302 return ERROR_SUCCESS;
6306 /***********************************************************************
6307 * HTTP_DeleteCustomHeader (internal)
6309 * Delete header from array
6310 * If this function is called, the index may change.
6311 * Headers section must be held
6313 static BOOL HTTP_DeleteCustomHeader(http_request_t *request, DWORD index)
6315 if( request->nCustHeaders <= 0 )
6316 return FALSE;
6317 if( index >= request->nCustHeaders )
6318 return FALSE;
6319 request->nCustHeaders--;
6321 heap_free(request->custHeaders[index].lpszField);
6322 heap_free(request->custHeaders[index].lpszValue);
6324 memmove( &request->custHeaders[index], &request->custHeaders[index+1],
6325 (request->nCustHeaders - index)* sizeof(HTTPHEADERW) );
6326 memset( &request->custHeaders[request->nCustHeaders], 0, sizeof(HTTPHEADERW) );
6328 return TRUE;
6332 /***********************************************************************
6333 * IsHostInProxyBypassList (WININET.@)
6335 BOOL WINAPI IsHostInProxyBypassList(INTERNET_SCHEME scheme, LPCSTR szHost, DWORD length)
6337 FIXME("STUB: scheme=%d host=%s length=%d\n", scheme, szHost, length);
6338 return FALSE;