Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1 | /* |
Francois Gouget | fac36c5 | 2012-05-15 10:15:32 +0200 | [diff] [blame] | 2 | * Wininet - HTTP Implementation |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright 1999 Corel Corporation |
Aric Stewart | ff9b9d4 | 2002-06-21 23:59:49 +0000 | [diff] [blame] | 5 | * Copyright 2002 CodeWeavers Inc. |
David Hammerton | 852c7ae | 2003-06-20 23:26:56 +0000 | [diff] [blame] | 6 | * Copyright 2002 TransGaming Technologies Inc. |
Francois Gouget | ad5ff7c | 2004-02-09 22:07:42 +0000 | [diff] [blame] | 7 | * Copyright 2004 Mike McCormack for CodeWeavers |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 8 | * Copyright 2005 Aric Stewart for CodeWeavers |
Rob Shearman | 272954b | 2007-01-04 18:23:17 +0000 | [diff] [blame] | 9 | * Copyright 2006 Robert Shearman for CodeWeavers |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 10 | * Copyright 2011 Jacek Caban for CodeWeavers |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 11 | * |
| 12 | * Ulrich Czekalla |
David Hammerton | 852c7ae | 2003-06-20 23:26:56 +0000 | [diff] [blame] | 13 | * David Hammerton |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 14 | * |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 15 | * This library is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU Lesser General Public |
| 17 | * License as published by the Free Software Foundation; either |
| 18 | * version 2.1 of the License, or (at your option) any later version. |
| 19 | * |
| 20 | * This library is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | * Lesser General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU Lesser General Public |
| 26 | * License along with this library; if not, write to the Free Software |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 27 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 28 | */ |
| 29 | |
Patrik Stridvall | 4710be2 | 2000-06-23 15:47:14 +0000 | [diff] [blame] | 30 | #include "config.h" |
Alexandre Julliard | 754e7aa | 2004-09-03 18:30:28 +0000 | [diff] [blame] | 31 | #include "wine/port.h" |
Patrik Stridvall | 4710be2 | 2000-06-23 15:47:14 +0000 | [diff] [blame] | 32 | |
Alexandre Julliard | 8228061 | 2008-12-09 11:33:25 +0100 | [diff] [blame] | 33 | #if defined(__MINGW32__) || defined (_MSC_VER) |
| 34 | #include <ws2tcpip.h> |
| 35 | #endif |
| 36 | |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 37 | #include <sys/types.h> |
Patrik Stridvall | 4710be2 | 2000-06-23 15:47:14 +0000 | [diff] [blame] | 38 | #ifdef HAVE_SYS_SOCKET_H |
| 39 | # include <sys/socket.h> |
| 40 | #endif |
Jacek Caban | ad02317 | 2006-01-05 14:37:06 +0100 | [diff] [blame] | 41 | #ifdef HAVE_ARPA_INET_H |
| 42 | # include <arpa/inet.h> |
| 43 | #endif |
Alexandre Julliard | e37c6e1 | 2003-09-05 23:08:26 +0000 | [diff] [blame] | 44 | #include <stdarg.h> |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 45 | #include <stdio.h> |
| 46 | #include <stdlib.h> |
Patrik Stridvall | d016f81 | 2002-08-17 00:43:16 +0000 | [diff] [blame] | 47 | #ifdef HAVE_UNISTD_H |
| 48 | # include <unistd.h> |
| 49 | #endif |
Chris Morgan | b9807b4 | 2001-02-15 21:24:07 +0000 | [diff] [blame] | 50 | #include <time.h> |
Mike McCormack | 3a1391b | 2004-07-19 21:49:39 +0000 | [diff] [blame] | 51 | #include <assert.h> |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 52 | #ifdef HAVE_ZLIB |
| 53 | # include <zlib.h> |
| 54 | #endif |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 55 | |
Guy Albertelli | aafec98 | 2001-11-06 22:31:19 +0000 | [diff] [blame] | 56 | #include "windef.h" |
| 57 | #include "winbase.h" |
| 58 | #include "wininet.h" |
Guy Albertelli | aafec98 | 2001-11-06 22:31:19 +0000 | [diff] [blame] | 59 | #include "winerror.h" |
Juan Lang | 2d32343 | 2011-03-03 10:54:07 -0800 | [diff] [blame] | 60 | #include "winternl.h" |
Jon Griffiths | 603f20f | 2001-12-11 00:30:17 +0000 | [diff] [blame] | 61 | #define NO_SHLWAPI_STREAM |
Robert Shearman | dc5f1cb | 2005-11-30 12:01:50 +0100 | [diff] [blame] | 62 | #define NO_SHLWAPI_REG |
| 63 | #define NO_SHLWAPI_STRFCNS |
| 64 | #define NO_SHLWAPI_GDI |
Guy Albertelli | aafec98 | 2001-11-06 22:31:19 +0000 | [diff] [blame] | 65 | #include "shlwapi.h" |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 66 | #include "sspi.h" |
Jacek Caban | 7e63f95 | 2008-03-12 02:24:23 +0100 | [diff] [blame] | 67 | #include "wincrypt.h" |
Juan Lang | 044f645 | 2011-10-31 12:55:15 -0700 | [diff] [blame] | 68 | #include "winuser.h" |
| 69 | #include "cryptuiapi.h" |
Guy Albertelli | aafec98 | 2001-11-06 22:31:19 +0000 | [diff] [blame] | 70 | |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 71 | #include "internet.h" |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 72 | #include "wine/debug.h" |
Hans Leidekker | bd80529 | 2008-10-24 11:08:12 +0200 | [diff] [blame] | 73 | #include "wine/exception.h" |
Alberto Massari | d476a5a | 2002-11-12 02:13:04 +0000 | [diff] [blame] | 74 | #include "wine/unicode.h" |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 75 | |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 76 | WINE_DEFAULT_DEBUG_CHANNEL(wininet); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 77 | |
Hans Leidekker | 3a57711 | 2008-03-24 21:31:53 +0100 | [diff] [blame] | 78 | static const WCHAR g_szHttp1_0[] = {'H','T','T','P','/','1','.','0',0}; |
Hans Leidekker | d0033db | 2008-02-17 20:41:42 +0100 | [diff] [blame] | 79 | static const WCHAR g_szHttp1_1[] = {'H','T','T','P','/','1','.','1',0}; |
Hans Leidekker | 2ed570e | 2010-02-22 12:28:03 +0100 | [diff] [blame] | 80 | static const WCHAR szOK[] = {'O','K',0}; |
| 81 | static const WCHAR szDefaultHeader[] = {'H','T','T','P','/','1','.','0',' ','2','0','0',' ','O','K',0}; |
Jacek Caban | 8317089 | 2009-05-29 23:34:14 +0200 | [diff] [blame] | 82 | static const WCHAR hostW[] = { 'H','o','s','t',0 }; |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 83 | static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 }; |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 84 | static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 }; |
| 85 | static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 }; |
Rob Shearman | 272954b | 2007-01-04 18:23:17 +0000 | [diff] [blame] | 86 | static const WCHAR szKeepAlive[] = {'K','e','e','p','-','A','l','i','v','e',0}; |
Jacek Caban | f979134 | 2008-02-13 13:34:05 +0100 | [diff] [blame] | 87 | static const WCHAR szGET[] = { 'G','E','T', 0 }; |
Hans Leidekker | fa4c25d | 2009-07-22 11:56:33 +0200 | [diff] [blame] | 88 | static const WCHAR szHEAD[] = { 'H','E','A','D', 0 }; |
Lei Zhang | f7e56d1 | 2008-08-27 17:03:13 -0700 | [diff] [blame] | 89 | static const WCHAR szCrLf[] = {'\r','\n', 0}; |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 90 | |
Jacek Caban | 8317089 | 2009-05-29 23:34:14 +0200 | [diff] [blame] | 91 | static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 }; |
| 92 | static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 }; |
| 93 | static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 }; |
| 94 | static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 }; |
| 95 | static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 }; |
| 96 | static const WCHAR szAge[] = { 'A','g','e',0 }; |
| 97 | static const WCHAR szAllow[] = { 'A','l','l','o','w',0 }; |
| 98 | static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 }; |
| 99 | static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 }; |
| 100 | static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 }; |
Alexander Morozov | ef95a79 | 2012-05-23 17:49:24 +0400 | [diff] [blame] | 101 | static const WCHAR szContent_Disposition[] = { 'C','o','n','t','e','n','t','-','D','i','s','p','o','s','i','t','i','o','n',0 }; |
Jacek Caban | 8317089 | 2009-05-29 23:34:14 +0200 | [diff] [blame] | 102 | static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 }; |
| 103 | static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 }; |
| 104 | static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 }; |
| 105 | static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 }; |
| 106 | static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 }; |
| 107 | static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 }; |
| 108 | static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 }; |
| 109 | static const WCHAR szContent_Transfer_Encoding[] = { 'C','o','n','t','e','n','t','-','T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 }; |
| 110 | static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 }; |
| 111 | static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 }; |
| 112 | static const WCHAR szDate[] = { 'D','a','t','e',0 }; |
| 113 | static const WCHAR szFrom[] = { 'F','r','o','m',0 }; |
| 114 | static const WCHAR szETag[] = { 'E','T','a','g',0 }; |
| 115 | static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 }; |
| 116 | static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 }; |
| 117 | static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 }; |
| 118 | static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 }; |
| 119 | static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 }; |
| 120 | static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 }; |
| 121 | static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 }; |
| 122 | static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 }; |
| 123 | static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 }; |
| 124 | static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 }; |
| 125 | static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 }; |
| 126 | static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 }; |
| 127 | static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 }; |
| 128 | static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 }; |
| 129 | static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 }; |
| 130 | static const WCHAR szRange[] = { 'R','a','n','g','e',0 }; |
| 131 | static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 }; |
| 132 | static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 }; |
| 133 | static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 }; |
| 134 | static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 }; |
| 135 | static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 }; |
| 136 | static const WCHAR szUnless_Modified_Since[] = { 'U','n','l','e','s','s','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 }; |
| 137 | static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 }; |
| 138 | static const WCHAR szURI[] = { 'U','R','I',0 }; |
| 139 | static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 }; |
| 140 | static const WCHAR szVary[] = { 'V','a','r','y',0 }; |
| 141 | static const WCHAR szVia[] = { 'V','i','a',0 }; |
| 142 | static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 }; |
| 143 | static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 }; |
| 144 | |
Jacek Caban | 8317089 | 2009-05-29 23:34:14 +0200 | [diff] [blame] | 145 | #define HTTP_REFERER szReferer |
| 146 | #define HTTP_ACCEPT szAccept |
| 147 | #define HTTP_USERAGENT szUser_Agent |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 148 | |
| 149 | #define HTTP_ADDHDR_FLAG_ADD 0x20000000 |
| 150 | #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000 |
| 151 | #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000 |
| 152 | #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000 |
| 153 | #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000 |
| 154 | #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000 |
| 155 | #define HTTP_ADDHDR_FLAG_REQ 0x02000000 |
| 156 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 157 | #define COLLECT_TIME 60000 |
| 158 | |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 159 | #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0])) |
| 160 | |
| 161 | struct HttpAuthInfo |
| 162 | { |
| 163 | LPWSTR scheme; |
| 164 | CredHandle cred; |
| 165 | CtxtHandle ctx; |
| 166 | TimeStamp exp; |
| 167 | ULONG attr; |
Rob Shearman | 0be05ab | 2008-03-10 16:41:44 +0000 | [diff] [blame] | 168 | ULONG max_token; |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 169 | void *auth_data; |
| 170 | unsigned int auth_data_len; |
| 171 | BOOL finished; /* finished authenticating */ |
| 172 | }; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 173 | |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 174 | |
Piotr Caban | c398e6f | 2010-07-17 14:08:44 +0200 | [diff] [blame] | 175 | typedef struct _basicAuthorizationData |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 176 | { |
| 177 | struct list entry; |
| 178 | |
Juan Lang | 2c6ad54 | 2011-03-01 10:30:46 -0800 | [diff] [blame] | 179 | LPWSTR host; |
| 180 | LPWSTR realm; |
| 181 | LPSTR authorization; |
| 182 | UINT authorizationLen; |
Piotr Caban | c398e6f | 2010-07-17 14:08:44 +0200 | [diff] [blame] | 183 | } basicAuthorizationData; |
| 184 | |
| 185 | typedef struct _authorizationData |
| 186 | { |
| 187 | struct list entry; |
| 188 | |
| 189 | LPWSTR host; |
| 190 | LPWSTR scheme; |
| 191 | LPWSTR domain; |
| 192 | UINT domain_len; |
| 193 | LPWSTR user; |
| 194 | UINT user_len; |
| 195 | LPWSTR password; |
| 196 | UINT password_len; |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 197 | } authorizationData; |
| 198 | |
| 199 | static struct list basicAuthorizationCache = LIST_INIT(basicAuthorizationCache); |
Piotr Caban | c398e6f | 2010-07-17 14:08:44 +0200 | [diff] [blame] | 200 | static struct list authorizationCache = LIST_INIT(authorizationCache); |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 201 | |
| 202 | static CRITICAL_SECTION authcache_cs; |
| 203 | static CRITICAL_SECTION_DEBUG critsect_debug = |
| 204 | { |
| 205 | 0, 0, &authcache_cs, |
| 206 | { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList }, |
| 207 | 0, 0, { (DWORD_PTR)(__FILE__ ": authcache_cs") } |
| 208 | }; |
| 209 | static CRITICAL_SECTION authcache_cs = { &critsect_debug, -1, 0, 0, 0, 0 }; |
| 210 | |
Jacek Caban | 34abacd | 2009-07-13 01:41:50 +0200 | [diff] [blame] | 211 | static BOOL HTTP_GetResponseHeaders(http_request_t *req, BOOL clear); |
Jacek Caban | e974965 | 2009-11-30 20:01:00 +0100 | [diff] [blame] | 212 | static DWORD HTTP_ProcessHeader(http_request_t *req, LPCWSTR field, LPCWSTR value, DWORD dwModifier); |
Jacek Caban | 02708c6 | 2005-10-26 10:07:58 +0000 | [diff] [blame] | 213 | static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer); |
Jacek Caban | e974965 | 2009-11-30 20:01:00 +0100 | [diff] [blame] | 214 | static DWORD HTTP_InsertCustomHeader(http_request_t *req, LPHTTPHEADERW lpHdr); |
Jacek Caban | 34abacd | 2009-07-13 01:41:50 +0200 | [diff] [blame] | 215 | static INT HTTP_GetCustomHeaderIndex(http_request_t *req, LPCWSTR lpszField, INT index, BOOL Request); |
| 216 | static BOOL HTTP_DeleteCustomHeader(http_request_t *req, DWORD index); |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 217 | static LPWSTR HTTP_build_req( LPCWSTR *list, int len ); |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 218 | static DWORD HTTP_HttpQueryInfoW(http_request_t*, DWORD, LPVOID, LPDWORD, LPDWORD); |
Jacek Caban | 34abacd | 2009-07-13 01:41:50 +0200 | [diff] [blame] | 219 | static LPWSTR HTTP_GetRedirectURL(http_request_t *req, LPCWSTR lpszUrl); |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 220 | static UINT HTTP_DecodeBase64(LPCWSTR base64, LPSTR bin); |
Jacek Caban | 34abacd | 2009-07-13 01:41:50 +0200 | [diff] [blame] | 221 | static BOOL HTTP_VerifyValidHeader(http_request_t *req, LPCWSTR field); |
Jacek Caban | c400117 | 2012-07-02 17:15:04 +0200 | [diff] [blame] | 222 | static BOOL drain_content(http_request_t*,BOOL); |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 223 | |
| 224 | static CRITICAL_SECTION connection_pool_cs; |
| 225 | static CRITICAL_SECTION_DEBUG connection_pool_debug = |
| 226 | { |
| 227 | 0, 0, &connection_pool_cs, |
| 228 | { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList }, |
| 229 | 0, 0, { (DWORD_PTR)(__FILE__ ": connection_pool_cs") } |
| 230 | }; |
| 231 | static CRITICAL_SECTION connection_pool_cs = { &connection_pool_debug, -1, 0, 0, 0, 0 }; |
| 232 | |
| 233 | static struct list connection_pool = LIST_INIT(connection_pool); |
| 234 | static BOOL collector_running; |
| 235 | |
| 236 | void server_addref(server_t *server) |
| 237 | { |
| 238 | InterlockedIncrement(&server->ref); |
| 239 | } |
| 240 | |
| 241 | void server_release(server_t *server) |
| 242 | { |
| 243 | if(InterlockedDecrement(&server->ref)) |
| 244 | return; |
| 245 | |
Jacek Caban | 95c1b74 | 2012-05-25 16:35:32 +0200 | [diff] [blame] | 246 | list_remove(&server->entry); |
| 247 | |
Jacek Caban | 6c764fb | 2012-06-11 10:22:38 +0200 | [diff] [blame] | 248 | if(server->cert_chain) |
| 249 | CertFreeCertificateChain(server->cert_chain); |
Jacek Caban | 95c1b74 | 2012-05-25 16:35:32 +0200 | [diff] [blame] | 250 | heap_free(server->name); |
| 251 | heap_free(server); |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Jacek Caban | 6c764fb | 2012-06-11 10:22:38 +0200 | [diff] [blame] | 254 | server_t *get_server(const WCHAR *name, INTERNET_PORT port, BOOL do_create) |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 255 | { |
| 256 | server_t *iter, *server = NULL; |
| 257 | |
| 258 | EnterCriticalSection(&connection_pool_cs); |
| 259 | |
| 260 | LIST_FOR_EACH_ENTRY(iter, &connection_pool, server_t, entry) { |
| 261 | if(iter->port == port && !strcmpW(iter->name, name)) { |
| 262 | server = iter; |
| 263 | server_addref(server); |
| 264 | break; |
| 265 | } |
| 266 | } |
| 267 | |
Jacek Caban | 6c764fb | 2012-06-11 10:22:38 +0200 | [diff] [blame] | 268 | if(!server && do_create) { |
| 269 | server = heap_alloc_zero(sizeof(*server)); |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 270 | if(server) { |
Jacek Caban | 95c1b74 | 2012-05-25 16:35:32 +0200 | [diff] [blame] | 271 | server->ref = 2; /* list reference and return */ |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 272 | server->port = port; |
| 273 | list_init(&server->conn_pool); |
| 274 | server->name = heap_strdupW(name); |
| 275 | if(server->name) { |
| 276 | list_add_head(&connection_pool, &server->entry); |
| 277 | }else { |
| 278 | heap_free(server); |
| 279 | server = NULL; |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | LeaveCriticalSection(&connection_pool_cs); |
| 285 | |
| 286 | return server; |
| 287 | } |
| 288 | |
Jacek Caban | f8f2273 | 2012-05-25 16:35:48 +0200 | [diff] [blame] | 289 | BOOL collect_connections(collect_type_t collect_type) |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 290 | { |
| 291 | netconn_t *netconn, *netconn_safe; |
| 292 | server_t *server, *server_safe; |
| 293 | BOOL remaining = FALSE; |
| 294 | DWORD64 now; |
| 295 | |
| 296 | now = GetTickCount64(); |
| 297 | |
| 298 | LIST_FOR_EACH_ENTRY_SAFE(server, server_safe, &connection_pool, server_t, entry) { |
| 299 | LIST_FOR_EACH_ENTRY_SAFE(netconn, netconn_safe, &server->conn_pool, netconn_t, pool_entry) { |
Jacek Caban | f8f2273 | 2012-05-25 16:35:48 +0200 | [diff] [blame] | 300 | if(collect_type > COLLECT_TIMEOUT || netconn->keep_until < now) { |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 301 | TRACE("freeing %p\n", netconn); |
| 302 | list_remove(&netconn->pool_entry); |
| 303 | free_netconn(netconn); |
| 304 | }else { |
| 305 | remaining = TRUE; |
| 306 | } |
| 307 | } |
| 308 | |
Jacek Caban | f8f2273 | 2012-05-25 16:35:48 +0200 | [diff] [blame] | 309 | if(collect_type == COLLECT_CLEANUP) { |
Jacek Caban | 95c1b74 | 2012-05-25 16:35:32 +0200 | [diff] [blame] | 310 | list_remove(&server->entry); |
| 311 | list_init(&server->entry); |
| 312 | server_release(server); |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
| 316 | return remaining; |
| 317 | } |
| 318 | |
| 319 | static DWORD WINAPI collect_connections_proc(void *arg) |
| 320 | { |
| 321 | BOOL remaining_conns; |
| 322 | |
| 323 | do { |
| 324 | /* FIXME: Use more sophisticated method */ |
| 325 | Sleep(5000); |
| 326 | |
| 327 | EnterCriticalSection(&connection_pool_cs); |
| 328 | |
Jacek Caban | f8f2273 | 2012-05-25 16:35:48 +0200 | [diff] [blame] | 329 | remaining_conns = collect_connections(COLLECT_TIMEOUT); |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 330 | if(!remaining_conns) |
| 331 | collector_running = FALSE; |
| 332 | |
| 333 | LeaveCriticalSection(&connection_pool_cs); |
| 334 | }while(remaining_conns); |
| 335 | |
| 336 | FreeLibraryAndExitThread(WININET_hModule, 0); |
| 337 | } |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 338 | |
Jacek Caban | 34abacd | 2009-07-13 01:41:50 +0200 | [diff] [blame] | 339 | static LPHTTPHEADERW HTTP_GetHeader(http_request_t *req, LPCWSTR head) |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 340 | { |
| 341 | int HeaderIndex = 0; |
| 342 | HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE); |
| 343 | if (HeaderIndex == -1) |
| 344 | return NULL; |
| 345 | else |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 346 | return &req->custHeaders[HeaderIndex]; |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 347 | } |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 348 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 349 | typedef enum { |
| 350 | READMODE_SYNC, |
| 351 | READMODE_ASYNC, |
| 352 | READMODE_NOBLOCK |
| 353 | } read_mode_t; |
| 354 | |
| 355 | struct data_stream_vtbl_t { |
| 356 | DWORD (*get_avail_data)(data_stream_t*,http_request_t*); |
| 357 | BOOL (*end_of_data)(data_stream_t*,http_request_t*); |
| 358 | DWORD (*read)(data_stream_t*,http_request_t*,BYTE*,DWORD,DWORD*,read_mode_t); |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 359 | BOOL (*drain_content)(data_stream_t*,http_request_t*); |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 360 | void (*destroy)(data_stream_t*); |
| 361 | }; |
| 362 | |
| 363 | typedef struct { |
| 364 | data_stream_t data_stream; |
| 365 | |
| 366 | BYTE buf[READ_BUFFER_SIZE]; |
| 367 | DWORD buf_size; |
| 368 | DWORD buf_pos; |
| 369 | DWORD chunk_size; |
| 370 | } chunked_stream_t; |
| 371 | |
Michael Stefaniuc | 9bf247d | 2011-04-05 16:18:38 +0200 | [diff] [blame] | 372 | static inline void destroy_data_stream(data_stream_t *stream) |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 373 | { |
| 374 | stream->vtbl->destroy(stream); |
| 375 | } |
| 376 | |
| 377 | static void reset_data_stream(http_request_t *req) |
| 378 | { |
| 379 | destroy_data_stream(req->data_stream); |
| 380 | req->data_stream = &req->netconn_stream.data_stream; |
| 381 | req->read_pos = req->read_size = req->netconn_stream.content_read = 0; |
| 382 | req->read_chunked = req->read_gzip = FALSE; |
| 383 | } |
| 384 | |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 385 | #ifdef HAVE_ZLIB |
| 386 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 387 | typedef struct { |
| 388 | data_stream_t stream; |
| 389 | data_stream_t *parent_stream; |
| 390 | z_stream zstream; |
| 391 | BYTE buf[READ_BUFFER_SIZE]; |
| 392 | DWORD buf_size; |
| 393 | DWORD buf_pos; |
| 394 | BOOL end_of_data; |
| 395 | } gzip_stream_t; |
| 396 | |
| 397 | static DWORD gzip_get_avail_data(data_stream_t *stream, http_request_t *req) |
| 398 | { |
| 399 | /* Allow reading only from read buffer */ |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | static BOOL gzip_end_of_data(data_stream_t *stream, http_request_t *req) |
| 404 | { |
| 405 | gzip_stream_t *gzip_stream = (gzip_stream_t*)stream; |
| 406 | return gzip_stream->end_of_data; |
| 407 | } |
| 408 | |
| 409 | static DWORD gzip_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size, |
| 410 | DWORD *read, read_mode_t read_mode) |
| 411 | { |
| 412 | gzip_stream_t *gzip_stream = (gzip_stream_t*)stream; |
| 413 | z_stream *zstream = &gzip_stream->zstream; |
| 414 | DWORD current_read, ret_read = 0; |
| 415 | BOOL end; |
| 416 | int zres; |
| 417 | DWORD res = ERROR_SUCCESS; |
| 418 | |
| 419 | while(size && !gzip_stream->end_of_data) { |
| 420 | end = gzip_stream->parent_stream->vtbl->end_of_data(gzip_stream->parent_stream, req); |
| 421 | |
| 422 | if(gzip_stream->buf_size <= 64 && !end) { |
| 423 | if(gzip_stream->buf_pos) { |
| 424 | if(gzip_stream->buf_size) |
| 425 | memmove(gzip_stream->buf, gzip_stream->buf+gzip_stream->buf_pos, gzip_stream->buf_size); |
| 426 | gzip_stream->buf_pos = 0; |
| 427 | } |
| 428 | res = gzip_stream->parent_stream->vtbl->read(gzip_stream->parent_stream, req, gzip_stream->buf+gzip_stream->buf_size, |
| 429 | sizeof(gzip_stream->buf)-gzip_stream->buf_size, ¤t_read, read_mode); |
| 430 | gzip_stream->buf_size += current_read; |
| 431 | if(res != ERROR_SUCCESS) |
| 432 | break; |
| 433 | end = gzip_stream->parent_stream->vtbl->end_of_data(gzip_stream->parent_stream, req); |
| 434 | if(!current_read && !end) { |
| 435 | if(read_mode != READMODE_NOBLOCK) { |
| 436 | WARN("unexpected end of data\n"); |
| 437 | gzip_stream->end_of_data = TRUE; |
| 438 | } |
| 439 | break; |
| 440 | } |
| 441 | if(gzip_stream->buf_size <= 64 && !end) |
| 442 | continue; |
| 443 | } |
| 444 | |
| 445 | zstream->next_in = gzip_stream->buf+gzip_stream->buf_pos; |
| 446 | zstream->avail_in = gzip_stream->buf_size-(end ? 0 : 64); |
| 447 | zstream->next_out = buf+ret_read; |
| 448 | zstream->avail_out = size; |
| 449 | zres = inflate(&gzip_stream->zstream, 0); |
| 450 | current_read = size - zstream->avail_out; |
| 451 | size -= current_read; |
| 452 | ret_read += current_read; |
| 453 | gzip_stream->buf_size -= zstream->next_in - (gzip_stream->buf+gzip_stream->buf_pos); |
| 454 | gzip_stream->buf_pos = zstream->next_in-gzip_stream->buf; |
| 455 | if(zres == Z_STREAM_END) { |
| 456 | TRACE("end of data\n"); |
| 457 | gzip_stream->end_of_data = TRUE; |
| 458 | inflateEnd(zstream); |
| 459 | }else if(zres != Z_OK) { |
| 460 | WARN("inflate failed %d: %s\n", zres, debugstr_a(zstream->msg)); |
| 461 | if(!ret_read) |
| 462 | res = ERROR_INTERNET_DECODING_FAILED; |
| 463 | break; |
| 464 | } |
| 465 | |
| 466 | if(ret_read && read_mode == READMODE_ASYNC) |
| 467 | read_mode = READMODE_NOBLOCK; |
| 468 | } |
| 469 | |
| 470 | TRACE("read %u bytes\n", ret_read); |
| 471 | *read = ret_read; |
| 472 | return res; |
| 473 | } |
| 474 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 475 | static BOOL gzip_drain_content(data_stream_t *stream, http_request_t *req) |
| 476 | { |
| 477 | gzip_stream_t *gzip_stream = (gzip_stream_t*)stream; |
| 478 | return gzip_stream->parent_stream->vtbl->drain_content(gzip_stream->parent_stream, req); |
| 479 | } |
| 480 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 481 | static void gzip_destroy(data_stream_t *stream) |
| 482 | { |
| 483 | gzip_stream_t *gzip_stream = (gzip_stream_t*)stream; |
| 484 | |
| 485 | destroy_data_stream(gzip_stream->parent_stream); |
| 486 | |
| 487 | if(!gzip_stream->end_of_data) |
| 488 | inflateEnd(&gzip_stream->zstream); |
| 489 | heap_free(gzip_stream); |
| 490 | } |
| 491 | |
| 492 | static const data_stream_vtbl_t gzip_stream_vtbl = { |
| 493 | gzip_get_avail_data, |
| 494 | gzip_end_of_data, |
| 495 | gzip_read, |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 496 | gzip_drain_content, |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 497 | gzip_destroy |
| 498 | }; |
| 499 | |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 500 | static voidpf wininet_zalloc(voidpf opaque, uInt items, uInt size) |
| 501 | { |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 502 | return heap_alloc(items*size); |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | static void wininet_zfree(voidpf opaque, voidpf address) |
| 506 | { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 507 | heap_free(address); |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 508 | } |
| 509 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 510 | static DWORD init_gzip_stream(http_request_t *req) |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 511 | { |
| 512 | gzip_stream_t *gzip_stream; |
Jacek Caban | 3858e35 | 2009-07-31 21:07:42 +0200 | [diff] [blame] | 513 | int index, zres; |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 514 | |
Jacek Caban | ec96604 | 2011-03-15 00:08:34 +0100 | [diff] [blame] | 515 | gzip_stream = heap_alloc_zero(sizeof(gzip_stream_t)); |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 516 | if(!gzip_stream) |
| 517 | return ERROR_OUTOFMEMORY; |
| 518 | |
| 519 | gzip_stream->stream.vtbl = &gzip_stream_vtbl; |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 520 | gzip_stream->zstream.zalloc = wininet_zalloc; |
| 521 | gzip_stream->zstream.zfree = wininet_zfree; |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 522 | |
| 523 | zres = inflateInit2(&gzip_stream->zstream, 0x1f); |
| 524 | if(zres != Z_OK) { |
| 525 | ERR("inflateInit failed: %d\n", zres); |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 526 | heap_free(gzip_stream); |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 527 | return ERROR_OUTOFMEMORY; |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 528 | } |
| 529 | |
Jacek Caban | 3858e35 | 2009-07-31 21:07:42 +0200 | [diff] [blame] | 530 | index = HTTP_GetCustomHeaderIndex(req, szContent_Length, 0, FALSE); |
| 531 | if(index != -1) |
| 532 | HTTP_DeleteCustomHeader(req, index); |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 533 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 534 | if(req->read_size) { |
| 535 | memcpy(gzip_stream->buf, req->read_buf+req->read_pos, req->read_size); |
| 536 | gzip_stream->buf_size = req->read_size; |
| 537 | req->read_pos = req->read_size = 0; |
| 538 | } |
| 539 | |
| 540 | req->read_gzip = TRUE; |
| 541 | gzip_stream->parent_stream = req->data_stream; |
| 542 | req->data_stream = &gzip_stream->stream; |
| 543 | return ERROR_SUCCESS; |
Jacek Caban | ec96604 | 2011-03-15 00:08:34 +0100 | [diff] [blame] | 544 | } |
| 545 | |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 546 | #else |
| 547 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 548 | static DWORD init_gzip_stream(http_request_t *req) |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 549 | { |
| 550 | ERR("gzip stream not supported, missing zlib.\n"); |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 551 | return ERROR_SUCCESS; |
Jacek Caban | ec96604 | 2011-03-15 00:08:34 +0100 | [diff] [blame] | 552 | } |
| 553 | |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 554 | #endif |
| 555 | |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 556 | /*********************************************************************** |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 557 | * HTTP_Tokenize (internal) |
| 558 | * |
| 559 | * Tokenize a string, allocating memory for the tokens. |
| 560 | */ |
| 561 | static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string) |
| 562 | { |
| 563 | LPWSTR * token_array; |
| 564 | int tokens = 0; |
| 565 | int i; |
| 566 | LPCWSTR next_token; |
| 567 | |
Rob Shearman | 0715d9c | 2008-10-07 15:39:50 +0100 | [diff] [blame] | 568 | if (string) |
| 569 | { |
| 570 | /* empty string has no tokens */ |
| 571 | if (*string) |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 572 | tokens++; |
Rob Shearman | 0715d9c | 2008-10-07 15:39:50 +0100 | [diff] [blame] | 573 | /* count tokens */ |
| 574 | for (i = 0; string[i]; i++) |
| 575 | { |
| 576 | if (!strncmpW(string+i, token_string, strlenW(token_string))) |
| 577 | { |
| 578 | DWORD j; |
| 579 | tokens++; |
| 580 | /* we want to skip over separators, but not the null terminator */ |
| 581 | for (j = 0; j < strlenW(token_string) - 1; j++) |
| 582 | if (!string[i+j]) |
| 583 | break; |
| 584 | i += j; |
| 585 | } |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 586 | } |
Rob Shearman | 0715d9c | 2008-10-07 15:39:50 +0100 | [diff] [blame] | 587 | } |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 588 | |
| 589 | /* add 1 for terminating NULL */ |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 590 | token_array = heap_alloc((tokens+1) * sizeof(*token_array)); |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 591 | token_array[tokens] = NULL; |
| 592 | if (!tokens) |
| 593 | return token_array; |
| 594 | for (i = 0; i < tokens; i++) |
| 595 | { |
| 596 | int len; |
| 597 | next_token = strstrW(string, token_string); |
| 598 | if (!next_token) next_token = string+strlenW(string); |
| 599 | len = next_token - string; |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 600 | token_array[i] = heap_alloc((len+1)*sizeof(WCHAR)); |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 601 | memcpy(token_array[i], string, len*sizeof(WCHAR)); |
| 602 | token_array[i][len] = '\0'; |
| 603 | string = next_token+strlenW(token_string); |
| 604 | } |
| 605 | return token_array; |
| 606 | } |
| 607 | |
| 608 | /*********************************************************************** |
| 609 | * HTTP_FreeTokens (internal) |
| 610 | * |
| 611 | * Frees memory returned from HTTP_Tokenize. |
| 612 | */ |
| 613 | static void HTTP_FreeTokens(LPWSTR * token_array) |
| 614 | { |
| 615 | int i; |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 616 | for (i = 0; token_array[i]; i++) heap_free(token_array[i]); |
| 617 | heap_free(token_array); |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 620 | static void HTTP_FixURL(http_request_t *request) |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 621 | { |
| 622 | static const WCHAR szSlash[] = { '/',0 }; |
| 623 | static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 }; |
| 624 | |
| 625 | /* If we don't have a path we set it to root */ |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 626 | if (NULL == request->path) |
| 627 | request->path = heap_strdupW(szSlash); |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 628 | else /* remove \r and \n*/ |
| 629 | { |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 630 | int nLen = strlenW(request->path); |
| 631 | while ((nLen >0 ) && ((request->path[nLen-1] == '\r')||(request->path[nLen-1] == '\n'))) |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 632 | { |
| 633 | nLen--; |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 634 | request->path[nLen]='\0'; |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 635 | } |
| 636 | /* Replace '\' with '/' */ |
| 637 | while (nLen>0) { |
| 638 | nLen--; |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 639 | if (request->path[nLen] == '\\') request->path[nLen]='/'; |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
| 643 | if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 644 | request->path, strlenW(request->path), szHttp, strlenW(szHttp) ) |
| 645 | && request->path[0] != '/') /* not an absolute path ?? --> fix it !! */ |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 646 | { |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 647 | WCHAR *fixurl = heap_alloc((strlenW(request->path) + 2)*sizeof(WCHAR)); |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 648 | *fixurl = '/'; |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 649 | strcpyW(fixurl + 1, request->path); |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 650 | heap_free( request->path ); |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 651 | request->path = fixurl; |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 655 | static LPWSTR HTTP_BuildHeaderRequestString( http_request_t *request, LPCWSTR verb, LPCWSTR path, LPCWSTR version ) |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 656 | { |
| 657 | LPWSTR requestString; |
| 658 | DWORD len, n; |
| 659 | LPCWSTR *req; |
Gerald Pfeifer | 3f1a20b | 2008-01-12 20:14:04 +0100 | [diff] [blame] | 660 | UINT i; |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 661 | LPWSTR p; |
| 662 | |
| 663 | static const WCHAR szSpace[] = { ' ',0 }; |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 664 | static const WCHAR szColon[] = { ':',' ',0 }; |
| 665 | static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0}; |
| 666 | |
| 667 | /* allocate space for an array of all the string pointers to be added */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 668 | len = (request->nCustHeaders)*4 + 10; |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 669 | req = heap_alloc(len*sizeof(LPCWSTR)); |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 670 | |
Robert Shearman | 0e7c41e | 2005-11-28 11:55:16 +0100 | [diff] [blame] | 671 | /* add the verb, path and HTTP version string */ |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 672 | n = 0; |
Robert Shearman | 0e7c41e | 2005-11-28 11:55:16 +0100 | [diff] [blame] | 673 | req[n++] = verb; |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 674 | req[n++] = szSpace; |
Robert Shearman | 0e7c41e | 2005-11-28 11:55:16 +0100 | [diff] [blame] | 675 | req[n++] = path; |
Hans Leidekker | d0033db | 2008-02-17 20:41:42 +0100 | [diff] [blame] | 676 | req[n++] = szSpace; |
| 677 | req[n++] = version; |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 678 | |
Austin English | 0e4adae | 2008-01-04 13:37:14 -0600 | [diff] [blame] | 679 | /* Append custom request headers */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 680 | for (i = 0; i < request->nCustHeaders; i++) |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 681 | { |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 682 | if (request->custHeaders[i].wFlags & HDR_ISREQUEST) |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 683 | { |
Lei Zhang | f7e56d1 | 2008-08-27 17:03:13 -0700 | [diff] [blame] | 684 | req[n++] = szCrLf; |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 685 | req[n++] = request->custHeaders[i].lpszField; |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 686 | req[n++] = szColon; |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 687 | req[n++] = request->custHeaders[i].lpszValue; |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 688 | |
| 689 | TRACE("Adding custom header %s (%s)\n", |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 690 | debugstr_w(request->custHeaders[i].lpszField), |
| 691 | debugstr_w(request->custHeaders[i].lpszValue)); |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | |
| 695 | if( n >= len ) |
| 696 | ERR("oops. buffer overrun\n"); |
| 697 | |
| 698 | req[n] = NULL; |
| 699 | requestString = HTTP_build_req( req, 4 ); |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 700 | heap_free( req ); |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 701 | |
| 702 | /* |
| 703 | * Set (header) termination string for request |
| 704 | * Make sure there's exactly two new lines at the end of the request |
| 705 | */ |
| 706 | p = &requestString[strlenW(requestString)-1]; |
| 707 | while ( (*p == '\n') || (*p == '\r') ) |
| 708 | p--; |
| 709 | strcpyW( p+1, sztwocrlf ); |
| 710 | |
| 711 | return requestString; |
| 712 | } |
| 713 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 714 | static void HTTP_ProcessCookies( http_request_t *request ) |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 715 | { |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 716 | int HeaderIndex; |
Jan-Peter Nilsson | b261836 | 2008-09-24 22:42:36 +0200 | [diff] [blame] | 717 | int numCookies = 0; |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 718 | LPHTTPHEADERW setCookieHeader; |
| 719 | |
Jacek Caban | 2e2ed52 | 2011-05-19 16:11:45 +0200 | [diff] [blame] | 720 | if(request->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) |
| 721 | return; |
| 722 | |
| 723 | while((HeaderIndex = HTTP_GetCustomHeaderIndex(request, szSet_Cookie, numCookies++, FALSE)) != -1) |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 724 | { |
Jacek Caban | 2e2ed52 | 2011-05-19 16:11:45 +0200 | [diff] [blame] | 725 | HTTPHEADERW *host; |
| 726 | const WCHAR *data; |
| 727 | WCHAR *name; |
| 728 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 729 | setCookieHeader = &request->custHeaders[HeaderIndex]; |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 730 | |
Jacek Caban | 2e2ed52 | 2011-05-19 16:11:45 +0200 | [diff] [blame] | 731 | if (!setCookieHeader->lpszValue) |
| 732 | continue; |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 733 | |
Jacek Caban | 2e2ed52 | 2011-05-19 16:11:45 +0200 | [diff] [blame] | 734 | host = HTTP_GetHeader(request, hostW); |
| 735 | if(!host) |
| 736 | continue; |
Jan-Peter Nilsson | b261836 | 2008-09-24 22:42:36 +0200 | [diff] [blame] | 737 | |
Jacek Caban | 2e2ed52 | 2011-05-19 16:11:45 +0200 | [diff] [blame] | 738 | data = strchrW(setCookieHeader->lpszValue, '='); |
| 739 | if(!data) |
| 740 | continue; |
| 741 | |
| 742 | name = heap_strndupW(setCookieHeader->lpszValue, data-setCookieHeader->lpszValue); |
| 743 | if(!name) |
| 744 | continue; |
| 745 | |
| 746 | data++; |
| 747 | set_cookie(host->lpszValue, request->path, name, data); |
| 748 | heap_free(name); |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 752 | static void strip_spaces(LPWSTR start) |
| 753 | { |
| 754 | LPWSTR str = start; |
| 755 | LPWSTR end; |
| 756 | |
| 757 | while (*str == ' ' && *str != '\0') |
| 758 | str++; |
| 759 | |
| 760 | if (str != start) |
| 761 | memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1)); |
| 762 | |
| 763 | end = start + strlenW(start) - 1; |
| 764 | while (end >= start && *end == ' ') |
| 765 | { |
| 766 | *end = '\0'; |
| 767 | end--; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | static inline BOOL is_basic_auth_value( LPCWSTR pszAuthValue, LPWSTR *pszRealm ) |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 772 | { |
| 773 | static const WCHAR szBasic[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */ |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 774 | static const WCHAR szRealm[] = {'r','e','a','l','m'}; /* Note: not nul-terminated */ |
| 775 | BOOL is_basic; |
| 776 | is_basic = !strncmpiW(pszAuthValue, szBasic, ARRAYSIZE(szBasic)) && |
Rob Shearman | 6021fa0 | 2008-05-15 21:05:21 +0100 | [diff] [blame] | 777 | ((pszAuthValue[ARRAYSIZE(szBasic)] == ' ') || !pszAuthValue[ARRAYSIZE(szBasic)]); |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 778 | if (is_basic && pszRealm) |
| 779 | { |
| 780 | LPCWSTR token; |
| 781 | LPCWSTR ptr = &pszAuthValue[ARRAYSIZE(szBasic)]; |
| 782 | LPCWSTR realm; |
| 783 | ptr++; |
| 784 | *pszRealm=NULL; |
| 785 | token = strchrW(ptr,'='); |
| 786 | if (!token) |
| 787 | return TRUE; |
| 788 | realm = ptr; |
| 789 | while (*realm == ' ' && *realm != '\0') |
| 790 | realm++; |
| 791 | if(!strncmpiW(realm, szRealm, ARRAYSIZE(szRealm)) && |
| 792 | (realm[ARRAYSIZE(szRealm)] == ' ' || realm[ARRAYSIZE(szRealm)] == '=')) |
| 793 | { |
| 794 | token++; |
| 795 | while (*token == ' ' && *token != '\0') |
| 796 | token++; |
| 797 | if (*token == '\0') |
| 798 | return TRUE; |
| 799 | *pszRealm = heap_strdupW(token); |
| 800 | strip_spaces(*pszRealm); |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | return is_basic; |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 805 | } |
| 806 | |
Hans Leidekker | 50fef74 | 2009-09-02 11:44:43 +0200 | [diff] [blame] | 807 | static void destroy_authinfo( struct HttpAuthInfo *authinfo ) |
| 808 | { |
| 809 | if (!authinfo) return; |
| 810 | |
| 811 | if (SecIsValidHandle(&authinfo->ctx)) |
| 812 | DeleteSecurityContext(&authinfo->ctx); |
| 813 | if (SecIsValidHandle(&authinfo->cred)) |
| 814 | FreeCredentialsHandle(&authinfo->cred); |
| 815 | |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 816 | heap_free(authinfo->auth_data); |
| 817 | heap_free(authinfo->scheme); |
| 818 | heap_free(authinfo); |
Hans Leidekker | 50fef74 | 2009-09-02 11:44:43 +0200 | [diff] [blame] | 819 | } |
| 820 | |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 821 | static UINT retrieve_cached_basic_authorization(LPWSTR host, LPWSTR realm, LPSTR *auth_data) |
| 822 | { |
Piotr Caban | c398e6f | 2010-07-17 14:08:44 +0200 | [diff] [blame] | 823 | basicAuthorizationData *ad; |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 824 | UINT rc = 0; |
| 825 | |
| 826 | TRACE("Looking for authorization for %s:%s\n",debugstr_w(host),debugstr_w(realm)); |
| 827 | |
| 828 | EnterCriticalSection(&authcache_cs); |
Piotr Caban | c398e6f | 2010-07-17 14:08:44 +0200 | [diff] [blame] | 829 | LIST_FOR_EACH_ENTRY(ad, &basicAuthorizationCache, basicAuthorizationData, entry) |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 830 | { |
Juan Lang | 2c6ad54 | 2011-03-01 10:30:46 -0800 | [diff] [blame] | 831 | if (!strcmpiW(host,ad->host) && !strcmpW(realm,ad->realm)) |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 832 | { |
| 833 | TRACE("Authorization found in cache\n"); |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 834 | *auth_data = heap_alloc(ad->authorizationLen); |
Juan Lang | 2c6ad54 | 2011-03-01 10:30:46 -0800 | [diff] [blame] | 835 | memcpy(*auth_data,ad->authorization,ad->authorizationLen); |
| 836 | rc = ad->authorizationLen; |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 837 | break; |
| 838 | } |
| 839 | } |
| 840 | LeaveCriticalSection(&authcache_cs); |
| 841 | return rc; |
| 842 | } |
| 843 | |
| 844 | static void cache_basic_authorization(LPWSTR host, LPWSTR realm, LPSTR auth_data, UINT auth_data_len) |
| 845 | { |
| 846 | struct list *cursor; |
Piotr Caban | c398e6f | 2010-07-17 14:08:44 +0200 | [diff] [blame] | 847 | basicAuthorizationData* ad = NULL; |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 848 | |
| 849 | TRACE("caching authorization for %s:%s = %s\n",debugstr_w(host),debugstr_w(realm),debugstr_an(auth_data,auth_data_len)); |
| 850 | |
| 851 | EnterCriticalSection(&authcache_cs); |
| 852 | LIST_FOR_EACH(cursor, &basicAuthorizationCache) |
| 853 | { |
Piotr Caban | c398e6f | 2010-07-17 14:08:44 +0200 | [diff] [blame] | 854 | basicAuthorizationData *check = LIST_ENTRY(cursor,basicAuthorizationData,entry); |
Juan Lang | 2c6ad54 | 2011-03-01 10:30:46 -0800 | [diff] [blame] | 855 | if (!strcmpiW(host,check->host) && !strcmpW(realm,check->realm)) |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 856 | { |
| 857 | ad = check; |
| 858 | break; |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | if (ad) |
| 863 | { |
| 864 | TRACE("Found match in cache, replacing\n"); |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 865 | heap_free(ad->authorization); |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 866 | ad->authorization = heap_alloc(auth_data_len); |
Juan Lang | 2c6ad54 | 2011-03-01 10:30:46 -0800 | [diff] [blame] | 867 | memcpy(ad->authorization, auth_data, auth_data_len); |
| 868 | ad->authorizationLen = auth_data_len; |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 869 | } |
| 870 | else |
| 871 | { |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 872 | ad = heap_alloc(sizeof(basicAuthorizationData)); |
Juan Lang | 2c6ad54 | 2011-03-01 10:30:46 -0800 | [diff] [blame] | 873 | ad->host = heap_strdupW(host); |
Jacek Caban | 0281b7d | 2011-07-28 16:53:57 +0200 | [diff] [blame] | 874 | ad->realm = heap_strdupW(realm); |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 875 | ad->authorization = heap_alloc(auth_data_len); |
Juan Lang | 2c6ad54 | 2011-03-01 10:30:46 -0800 | [diff] [blame] | 876 | memcpy(ad->authorization, auth_data, auth_data_len); |
| 877 | ad->authorizationLen = auth_data_len; |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 878 | list_add_head(&basicAuthorizationCache,&ad->entry); |
| 879 | TRACE("authorization cached\n"); |
| 880 | } |
| 881 | LeaveCriticalSection(&authcache_cs); |
| 882 | } |
| 883 | |
Piotr Caban | c398e6f | 2010-07-17 14:08:44 +0200 | [diff] [blame] | 884 | static BOOL retrieve_cached_authorization(LPWSTR host, LPWSTR scheme, |
| 885 | SEC_WINNT_AUTH_IDENTITY_W *nt_auth_identity) |
| 886 | { |
| 887 | authorizationData *ad; |
| 888 | |
| 889 | TRACE("Looking for authorization for %s:%s\n", debugstr_w(host), debugstr_w(scheme)); |
| 890 | |
| 891 | EnterCriticalSection(&authcache_cs); |
| 892 | LIST_FOR_EACH_ENTRY(ad, &authorizationCache, authorizationData, entry) { |
| 893 | if(!strcmpiW(host, ad->host) && !strcmpiW(scheme, ad->scheme)) { |
| 894 | TRACE("Authorization found in cache\n"); |
| 895 | |
| 896 | nt_auth_identity->User = heap_strdupW(ad->user); |
| 897 | nt_auth_identity->Password = heap_strdupW(ad->password); |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 898 | nt_auth_identity->Domain = heap_alloc(sizeof(WCHAR)*ad->domain_len); |
Piotr Caban | c398e6f | 2010-07-17 14:08:44 +0200 | [diff] [blame] | 899 | if(!nt_auth_identity->User || !nt_auth_identity->Password || |
| 900 | (!nt_auth_identity->Domain && ad->domain_len)) { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 901 | heap_free(nt_auth_identity->User); |
| 902 | heap_free(nt_auth_identity->Password); |
| 903 | heap_free(nt_auth_identity->Domain); |
Piotr Caban | c398e6f | 2010-07-17 14:08:44 +0200 | [diff] [blame] | 904 | break; |
| 905 | } |
| 906 | |
| 907 | nt_auth_identity->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE; |
| 908 | nt_auth_identity->UserLength = ad->user_len; |
| 909 | nt_auth_identity->PasswordLength = ad->password_len; |
| 910 | memcpy(nt_auth_identity->Domain, ad->domain, sizeof(WCHAR)*ad->domain_len); |
| 911 | nt_auth_identity->DomainLength = ad->domain_len; |
| 912 | LeaveCriticalSection(&authcache_cs); |
| 913 | return TRUE; |
| 914 | } |
| 915 | } |
| 916 | LeaveCriticalSection(&authcache_cs); |
| 917 | |
| 918 | return FALSE; |
| 919 | } |
| 920 | |
| 921 | static void cache_authorization(LPWSTR host, LPWSTR scheme, |
| 922 | SEC_WINNT_AUTH_IDENTITY_W *nt_auth_identity) |
| 923 | { |
| 924 | authorizationData *ad; |
| 925 | BOOL found = FALSE; |
| 926 | |
| 927 | TRACE("Caching authorization for %s:%s\n", debugstr_w(host), debugstr_w(scheme)); |
| 928 | |
| 929 | EnterCriticalSection(&authcache_cs); |
| 930 | LIST_FOR_EACH_ENTRY(ad, &authorizationCache, authorizationData, entry) |
| 931 | if(!strcmpiW(host, ad->host) && !strcmpiW(scheme, ad->scheme)) { |
| 932 | found = TRUE; |
| 933 | break; |
| 934 | } |
| 935 | |
| 936 | if(found) { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 937 | heap_free(ad->user); |
| 938 | heap_free(ad->password); |
| 939 | heap_free(ad->domain); |
Piotr Caban | c398e6f | 2010-07-17 14:08:44 +0200 | [diff] [blame] | 940 | } else { |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 941 | ad = heap_alloc(sizeof(authorizationData)); |
Piotr Caban | c398e6f | 2010-07-17 14:08:44 +0200 | [diff] [blame] | 942 | if(!ad) { |
| 943 | LeaveCriticalSection(&authcache_cs); |
| 944 | return; |
| 945 | } |
| 946 | |
| 947 | ad->host = heap_strdupW(host); |
| 948 | ad->scheme = heap_strdupW(scheme); |
| 949 | list_add_head(&authorizationCache, &ad->entry); |
| 950 | } |
| 951 | |
| 952 | ad->user = heap_strndupW(nt_auth_identity->User, nt_auth_identity->UserLength); |
| 953 | ad->password = heap_strndupW(nt_auth_identity->Password, nt_auth_identity->PasswordLength); |
| 954 | ad->domain = heap_strndupW(nt_auth_identity->Domain, nt_auth_identity->DomainLength); |
| 955 | ad->user_len = nt_auth_identity->UserLength; |
| 956 | ad->password_len = nt_auth_identity->PasswordLength; |
| 957 | ad->domain_len = nt_auth_identity->DomainLength; |
| 958 | |
| 959 | if(!ad->host || !ad->scheme || !ad->user || !ad->password |
| 960 | || (nt_auth_identity->Domain && !ad->domain)) { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 961 | heap_free(ad->host); |
| 962 | heap_free(ad->scheme); |
| 963 | heap_free(ad->user); |
| 964 | heap_free(ad->password); |
| 965 | heap_free(ad->domain); |
Piotr Caban | c398e6f | 2010-07-17 14:08:44 +0200 | [diff] [blame] | 966 | list_remove(&ad->entry); |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 967 | heap_free(ad); |
Piotr Caban | c398e6f | 2010-07-17 14:08:44 +0200 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | LeaveCriticalSection(&authcache_cs); |
| 971 | } |
| 972 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 973 | static BOOL HTTP_DoAuthorization( http_request_t *request, LPCWSTR pszAuthValue, |
Rob Shearman | cb28969 | 2007-06-05 19:49:58 +0100 | [diff] [blame] | 974 | struct HttpAuthInfo **ppAuthInfo, |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 975 | LPWSTR domain_and_username, LPWSTR password, |
| 976 | LPWSTR host ) |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 977 | { |
| 978 | SECURITY_STATUS sec_status; |
Rob Shearman | cb28969 | 2007-06-05 19:49:58 +0100 | [diff] [blame] | 979 | struct HttpAuthInfo *pAuthInfo = *ppAuthInfo; |
Rob Shearman | 7b94871 | 2007-05-26 08:49:12 +0100 | [diff] [blame] | 980 | BOOL first = FALSE; |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 981 | LPWSTR szRealm = NULL; |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 982 | |
| 983 | TRACE("%s\n", debugstr_w(pszAuthValue)); |
| 984 | |
| 985 | if (!pAuthInfo) |
| 986 | { |
| 987 | TimeStamp exp; |
| 988 | |
Rob Shearman | 7b94871 | 2007-05-26 08:49:12 +0100 | [diff] [blame] | 989 | first = TRUE; |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 990 | pAuthInfo = heap_alloc(sizeof(*pAuthInfo)); |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 991 | if (!pAuthInfo) |
| 992 | return FALSE; |
| 993 | |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 994 | SecInvalidateHandle(&pAuthInfo->cred); |
| 995 | SecInvalidateHandle(&pAuthInfo->ctx); |
| 996 | memset(&pAuthInfo->exp, 0, sizeof(pAuthInfo->exp)); |
| 997 | pAuthInfo->attr = 0; |
| 998 | pAuthInfo->auth_data = NULL; |
| 999 | pAuthInfo->auth_data_len = 0; |
| 1000 | pAuthInfo->finished = FALSE; |
| 1001 | |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 1002 | if (is_basic_auth_value(pszAuthValue,NULL)) |
Rob Shearman | 4d1b8b1 | 2007-05-25 12:06:11 +0100 | [diff] [blame] | 1003 | { |
| 1004 | static const WCHAR szBasic[] = {'B','a','s','i','c',0}; |
Jacek Caban | f598709 | 2009-07-17 01:10:41 +0200 | [diff] [blame] | 1005 | pAuthInfo->scheme = heap_strdupW(szBasic); |
Rob Shearman | 4d1b8b1 | 2007-05-25 12:06:11 +0100 | [diff] [blame] | 1006 | if (!pAuthInfo->scheme) |
| 1007 | { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 1008 | heap_free(pAuthInfo); |
Rob Shearman | 4d1b8b1 | 2007-05-25 12:06:11 +0100 | [diff] [blame] | 1009 | return FALSE; |
| 1010 | } |
| 1011 | } |
| 1012 | else |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1013 | { |
Rob Shearman | 7631bdf | 2008-03-10 16:40:23 +0000 | [diff] [blame] | 1014 | PVOID pAuthData; |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1015 | SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity; |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1016 | |
Jacek Caban | f598709 | 2009-07-17 01:10:41 +0200 | [diff] [blame] | 1017 | pAuthInfo->scheme = heap_strdupW(pszAuthValue); |
Rob Shearman | 4d1b8b1 | 2007-05-25 12:06:11 +0100 | [diff] [blame] | 1018 | if (!pAuthInfo->scheme) |
| 1019 | { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 1020 | heap_free(pAuthInfo); |
Rob Shearman | 4d1b8b1 | 2007-05-25 12:06:11 +0100 | [diff] [blame] | 1021 | return FALSE; |
| 1022 | } |
| 1023 | |
Rob Shearman | 7631bdf | 2008-03-10 16:40:23 +0000 | [diff] [blame] | 1024 | if (domain_and_username) |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1025 | { |
Rob Shearman | 7631bdf | 2008-03-10 16:40:23 +0000 | [diff] [blame] | 1026 | WCHAR *user = strchrW(domain_and_username, '\\'); |
| 1027 | WCHAR *domain = domain_and_username; |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1028 | |
Rob Shearman | 7631bdf | 2008-03-10 16:40:23 +0000 | [diff] [blame] | 1029 | /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */ |
| 1030 | |
| 1031 | pAuthData = &nt_auth_identity; |
| 1032 | |
| 1033 | if (user) user++; |
| 1034 | else |
| 1035 | { |
| 1036 | user = domain_and_username; |
| 1037 | domain = NULL; |
| 1038 | } |
| 1039 | |
| 1040 | nt_auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE; |
| 1041 | nt_auth_identity.User = user; |
| 1042 | nt_auth_identity.UserLength = strlenW(nt_auth_identity.User); |
| 1043 | nt_auth_identity.Domain = domain; |
| 1044 | nt_auth_identity.DomainLength = domain ? user - domain - 1 : 0; |
| 1045 | nt_auth_identity.Password = password; |
| 1046 | nt_auth_identity.PasswordLength = strlenW(nt_auth_identity.Password); |
Piotr Caban | c398e6f | 2010-07-17 14:08:44 +0200 | [diff] [blame] | 1047 | |
| 1048 | cache_authorization(host, pAuthInfo->scheme, &nt_auth_identity); |
Rob Shearman | 7631bdf | 2008-03-10 16:40:23 +0000 | [diff] [blame] | 1049 | } |
Piotr Caban | c398e6f | 2010-07-17 14:08:44 +0200 | [diff] [blame] | 1050 | else if(retrieve_cached_authorization(host, pAuthInfo->scheme, &nt_auth_identity)) |
| 1051 | pAuthData = &nt_auth_identity; |
Rob Shearman | 7631bdf | 2008-03-10 16:40:23 +0000 | [diff] [blame] | 1052 | else |
| 1053 | /* use default credentials */ |
| 1054 | pAuthData = NULL; |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1055 | |
| 1056 | sec_status = AcquireCredentialsHandleW(NULL, pAuthInfo->scheme, |
| 1057 | SECPKG_CRED_OUTBOUND, NULL, |
Rob Shearman | 7631bdf | 2008-03-10 16:40:23 +0000 | [diff] [blame] | 1058 | pAuthData, NULL, |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1059 | NULL, &pAuthInfo->cred, |
| 1060 | &exp); |
Piotr Caban | c398e6f | 2010-07-17 14:08:44 +0200 | [diff] [blame] | 1061 | |
| 1062 | if(pAuthData && !domain_and_username) { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 1063 | heap_free(nt_auth_identity.User); |
| 1064 | heap_free(nt_auth_identity.Domain); |
| 1065 | heap_free(nt_auth_identity.Password); |
Piotr Caban | c398e6f | 2010-07-17 14:08:44 +0200 | [diff] [blame] | 1066 | } |
| 1067 | |
Rob Shearman | 0be05ab | 2008-03-10 16:41:44 +0000 | [diff] [blame] | 1068 | if (sec_status == SEC_E_OK) |
| 1069 | { |
| 1070 | PSecPkgInfoW sec_pkg_info; |
| 1071 | sec_status = QuerySecurityPackageInfoW(pAuthInfo->scheme, &sec_pkg_info); |
| 1072 | if (sec_status == SEC_E_OK) |
| 1073 | { |
| 1074 | pAuthInfo->max_token = sec_pkg_info->cbMaxToken; |
| 1075 | FreeContextBuffer(sec_pkg_info); |
| 1076 | } |
| 1077 | } |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1078 | if (sec_status != SEC_E_OK) |
| 1079 | { |
| 1080 | WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n", |
| 1081 | debugstr_w(pAuthInfo->scheme), sec_status); |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 1082 | heap_free(pAuthInfo->scheme); |
| 1083 | heap_free(pAuthInfo); |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1084 | return FALSE; |
| 1085 | } |
| 1086 | } |
Rob Shearman | cb28969 | 2007-06-05 19:49:58 +0100 | [diff] [blame] | 1087 | *ppAuthInfo = pAuthInfo; |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1088 | } |
| 1089 | else if (pAuthInfo->finished) |
| 1090 | return FALSE; |
| 1091 | |
| 1092 | if ((strlenW(pszAuthValue) < strlenW(pAuthInfo->scheme)) || |
| 1093 | strncmpiW(pszAuthValue, pAuthInfo->scheme, strlenW(pAuthInfo->scheme))) |
| 1094 | { |
| 1095 | ERR("authentication scheme changed from %s to %s\n", |
| 1096 | debugstr_w(pAuthInfo->scheme), debugstr_w(pszAuthValue)); |
| 1097 | return FALSE; |
| 1098 | } |
| 1099 | |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 1100 | if (is_basic_auth_value(pszAuthValue,&szRealm)) |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1101 | { |
Rob Shearman | 7631bdf | 2008-03-10 16:40:23 +0000 | [diff] [blame] | 1102 | int userlen; |
| 1103 | int passlen; |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 1104 | char *auth_data = NULL; |
| 1105 | UINT auth_data_len = 0; |
Rob Shearman | 847cc51 | 2007-05-21 14:28:02 +0100 | [diff] [blame] | 1106 | |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 1107 | TRACE("basic authentication realm %s\n",debugstr_w(szRealm)); |
Rob Shearman | 847cc51 | 2007-05-21 14:28:02 +0100 | [diff] [blame] | 1108 | |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 1109 | if (!domain_and_username) |
| 1110 | { |
| 1111 | if (host && szRealm) |
| 1112 | auth_data_len = retrieve_cached_basic_authorization(host, szRealm,&auth_data); |
| 1113 | if (auth_data_len == 0) |
| 1114 | { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 1115 | heap_free(szRealm); |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 1116 | return FALSE; |
| 1117 | } |
| 1118 | } |
| 1119 | else |
| 1120 | { |
| 1121 | userlen = WideCharToMultiByte(CP_UTF8, 0, domain_and_username, lstrlenW(domain_and_username), NULL, 0, NULL, NULL); |
| 1122 | passlen = WideCharToMultiByte(CP_UTF8, 0, password, lstrlenW(password), NULL, 0, NULL, NULL); |
Rob Shearman | 7631bdf | 2008-03-10 16:40:23 +0000 | [diff] [blame] | 1123 | |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 1124 | /* length includes a nul terminator, which will be re-used for the ':' */ |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 1125 | auth_data = heap_alloc(userlen + 1 + passlen); |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 1126 | if (!auth_data) |
| 1127 | { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 1128 | heap_free(szRealm); |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 1129 | return FALSE; |
| 1130 | } |
Rob Shearman | 7631bdf | 2008-03-10 16:40:23 +0000 | [diff] [blame] | 1131 | |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 1132 | WideCharToMultiByte(CP_UTF8, 0, domain_and_username, -1, auth_data, userlen, NULL, NULL); |
| 1133 | auth_data[userlen] = ':'; |
| 1134 | WideCharToMultiByte(CP_UTF8, 0, password, -1, &auth_data[userlen+1], passlen, NULL, NULL); |
| 1135 | auth_data_len = userlen + 1 + passlen; |
| 1136 | if (host && szRealm) |
| 1137 | cache_basic_authorization(host, szRealm, auth_data, auth_data_len); |
| 1138 | } |
Rob Shearman | 847cc51 | 2007-05-21 14:28:02 +0100 | [diff] [blame] | 1139 | |
| 1140 | pAuthInfo->auth_data = auth_data; |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 1141 | pAuthInfo->auth_data_len = auth_data_len; |
Rob Shearman | 847cc51 | 2007-05-21 14:28:02 +0100 | [diff] [blame] | 1142 | pAuthInfo->finished = TRUE; |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 1143 | heap_free(szRealm); |
Rob Shearman | 847cc51 | 2007-05-21 14:28:02 +0100 | [diff] [blame] | 1144 | return TRUE; |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1145 | } |
| 1146 | else |
| 1147 | { |
| 1148 | LPCWSTR pszAuthData; |
| 1149 | SecBufferDesc out_desc, in_desc; |
| 1150 | SecBuffer out, in; |
| 1151 | unsigned char *buffer; |
| 1152 | ULONG context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE | |
| 1153 | ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE; |
| 1154 | |
| 1155 | in.BufferType = SECBUFFER_TOKEN; |
| 1156 | in.cbBuffer = 0; |
| 1157 | in.pvBuffer = NULL; |
| 1158 | |
| 1159 | in_desc.ulVersion = 0; |
| 1160 | in_desc.cBuffers = 1; |
| 1161 | in_desc.pBuffers = ∈ |
| 1162 | |
| 1163 | pszAuthData = pszAuthValue + strlenW(pAuthInfo->scheme); |
| 1164 | if (*pszAuthData == ' ') |
| 1165 | { |
| 1166 | pszAuthData++; |
| 1167 | in.cbBuffer = HTTP_DecodeBase64(pszAuthData, NULL); |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 1168 | in.pvBuffer = heap_alloc(in.cbBuffer); |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1169 | HTTP_DecodeBase64(pszAuthData, in.pvBuffer); |
| 1170 | } |
| 1171 | |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 1172 | buffer = heap_alloc(pAuthInfo->max_token); |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1173 | |
| 1174 | out.BufferType = SECBUFFER_TOKEN; |
Rob Shearman | 0be05ab | 2008-03-10 16:41:44 +0000 | [diff] [blame] | 1175 | out.cbBuffer = pAuthInfo->max_token; |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1176 | out.pvBuffer = buffer; |
| 1177 | |
| 1178 | out_desc.ulVersion = 0; |
| 1179 | out_desc.cBuffers = 1; |
| 1180 | out_desc.pBuffers = &out; |
| 1181 | |
Rob Shearman | 7b94871 | 2007-05-26 08:49:12 +0100 | [diff] [blame] | 1182 | sec_status = InitializeSecurityContextW(first ? &pAuthInfo->cred : NULL, |
Rob Shearman | 5edcf3a | 2008-01-24 19:30:11 +0000 | [diff] [blame] | 1183 | first ? NULL : &pAuthInfo->ctx, |
Jacek Caban | 3aeb8eb | 2012-05-25 16:34:24 +0200 | [diff] [blame] | 1184 | first ? request->server->name : NULL, |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1185 | context_req, 0, SECURITY_NETWORK_DREP, |
| 1186 | in.pvBuffer ? &in_desc : NULL, |
| 1187 | 0, &pAuthInfo->ctx, &out_desc, |
| 1188 | &pAuthInfo->attr, &pAuthInfo->exp); |
| 1189 | if (sec_status == SEC_E_OK) |
| 1190 | { |
| 1191 | pAuthInfo->finished = TRUE; |
| 1192 | pAuthInfo->auth_data = out.pvBuffer; |
| 1193 | pAuthInfo->auth_data_len = out.cbBuffer; |
| 1194 | TRACE("sending last auth packet\n"); |
| 1195 | } |
| 1196 | else if (sec_status == SEC_I_CONTINUE_NEEDED) |
| 1197 | { |
| 1198 | pAuthInfo->auth_data = out.pvBuffer; |
| 1199 | pAuthInfo->auth_data_len = out.cbBuffer; |
| 1200 | TRACE("sending next auth packet\n"); |
| 1201 | } |
| 1202 | else |
| 1203 | { |
| 1204 | ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status); |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 1205 | heap_free(out.pvBuffer); |
Hans Leidekker | 50fef74 | 2009-09-02 11:44:43 +0200 | [diff] [blame] | 1206 | destroy_authinfo(pAuthInfo); |
| 1207 | *ppAuthInfo = NULL; |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1208 | return FALSE; |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | return TRUE; |
| 1213 | } |
| 1214 | |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 1215 | /*********************************************************************** |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 1216 | * HTTP_HttpAddRequestHeadersW (internal) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 1217 | */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 1218 | static DWORD HTTP_HttpAddRequestHeadersW(http_request_t *request, |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 1219 | LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 1220 | { |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 1221 | LPWSTR lpszStart; |
| 1222 | LPWSTR lpszEnd; |
| 1223 | LPWSTR buffer; |
Jacek Caban | e974965 | 2009-11-30 20:01:00 +0100 | [diff] [blame] | 1224 | DWORD len, res = ERROR_HTTP_INVALID_HEADER; |
David Hammerton | 852c7ae | 2003-06-20 23:26:56 +0000 | [diff] [blame] | 1225 | |
Francois Gouget | abfa73b | 2008-02-19 00:18:02 +0100 | [diff] [blame] | 1226 | TRACE("copying header: %s\n", debugstr_wn(lpszHeader, dwHeaderLength)); |
Mike McCormack | 08c6c69 | 2004-08-10 23:41:35 +0000 | [diff] [blame] | 1227 | |
Alexandre Julliard | 7c1925a0 | 2005-09-13 15:00:32 +0000 | [diff] [blame] | 1228 | if( dwHeaderLength == ~0U ) |
Mike McCormack | 08c6c69 | 2004-08-10 23:41:35 +0000 | [diff] [blame] | 1229 | len = strlenW(lpszHeader); |
| 1230 | else |
| 1231 | len = dwHeaderLength; |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 1232 | buffer = heap_alloc(sizeof(WCHAR)*(len+1)); |
Peter Berg Larsen | e732fc0 | 2005-03-28 14:17:51 +0000 | [diff] [blame] | 1233 | lstrcpynW( buffer, lpszHeader, len + 1); |
Mike McCormack | 08c6c69 | 2004-08-10 23:41:35 +0000 | [diff] [blame] | 1234 | |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 1235 | lpszStart = buffer; |
| 1236 | |
| 1237 | do |
| 1238 | { |
Robert Shearman | b72a682 | 2004-09-23 22:53:50 +0000 | [diff] [blame] | 1239 | LPWSTR * pFieldAndValue; |
| 1240 | |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 1241 | lpszEnd = lpszStart; |
| 1242 | |
| 1243 | while (*lpszEnd != '\0') |
| 1244 | { |
Mike Kaplinskiy | 6c767c4 | 2009-06-14 18:29:31 -0400 | [diff] [blame] | 1245 | if (*lpszEnd == '\r' || *lpszEnd == '\n') |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 1246 | break; |
| 1247 | lpszEnd++; |
| 1248 | } |
| 1249 | |
Robert Shearman | 4cd38b4 | 2004-07-13 23:34:28 +0000 | [diff] [blame] | 1250 | if (*lpszStart == '\0') |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 1251 | break; |
| 1252 | |
Mike Kaplinskiy | 6c767c4 | 2009-06-14 18:29:31 -0400 | [diff] [blame] | 1253 | if (*lpszEnd == '\r' || *lpszEnd == '\n') |
Robert Shearman | 4cd38b4 | 2004-07-13 23:34:28 +0000 | [diff] [blame] | 1254 | { |
| 1255 | *lpszEnd = '\0'; |
Mike Kaplinskiy | 6c767c4 | 2009-06-14 18:29:31 -0400 | [diff] [blame] | 1256 | lpszEnd++; /* Jump over newline */ |
Robert Shearman | 4cd38b4 | 2004-07-13 23:34:28 +0000 | [diff] [blame] | 1257 | } |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 1258 | TRACE("interpreting header %s\n", debugstr_w(lpszStart)); |
Paul TBBle Hampson | 4d57ee3 | 2009-04-25 02:31:06 +1000 | [diff] [blame] | 1259 | if (*lpszStart == '\0') |
| 1260 | { |
| 1261 | /* Skip 0-length headers */ |
| 1262 | lpszStart = lpszEnd; |
Jacek Caban | e974965 | 2009-11-30 20:01:00 +0100 | [diff] [blame] | 1263 | res = ERROR_SUCCESS; |
Paul TBBle Hampson | 4d57ee3 | 2009-04-25 02:31:06 +1000 | [diff] [blame] | 1264 | continue; |
| 1265 | } |
Robert Shearman | b72a682 | 2004-09-23 22:53:50 +0000 | [diff] [blame] | 1266 | pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart); |
| 1267 | if (pFieldAndValue) |
| 1268 | { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 1269 | res = HTTP_VerifyValidHeader(request, pFieldAndValue[0]); |
Jacek Caban | e974965 | 2009-11-30 20:01:00 +0100 | [diff] [blame] | 1270 | if (res == ERROR_SUCCESS) |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 1271 | res = HTTP_ProcessHeader(request, pFieldAndValue[0], |
Aric Stewart | c8dfc02 | 2007-07-26 08:59:00 -0500 | [diff] [blame] | 1272 | pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ); |
Robert Shearman | b72a682 | 2004-09-23 22:53:50 +0000 | [diff] [blame] | 1273 | HTTP_FreeTokens(pFieldAndValue); |
| 1274 | } |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 1275 | |
Robert Shearman | 4cd38b4 | 2004-07-13 23:34:28 +0000 | [diff] [blame] | 1276 | lpszStart = lpszEnd; |
Jacek Caban | e974965 | 2009-11-30 20:01:00 +0100 | [diff] [blame] | 1277 | } while (res == ERROR_SUCCESS); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 1278 | |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 1279 | heap_free(buffer); |
Jacek Caban | e974965 | 2009-11-30 20:01:00 +0100 | [diff] [blame] | 1280 | return res; |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | /*********************************************************************** |
| 1284 | * HttpAddRequestHeadersW (WININET.@) |
| 1285 | * |
| 1286 | * Adds one or more HTTP header to the request handler |
| 1287 | * |
Francois Gouget | abfa73b | 2008-02-19 00:18:02 +0100 | [diff] [blame] | 1288 | * NOTE |
| 1289 | * On Windows if dwHeaderLength includes the trailing '\0', then |
| 1290 | * HttpAddRequestHeadersW() adds it too. However this results in an |
Francois Gouget | fac36c5 | 2012-05-15 10:15:32 +0200 | [diff] [blame] | 1291 | * invalid HTTP header which is rejected by some servers so we probably |
Francois Gouget | abfa73b | 2008-02-19 00:18:02 +0100 | [diff] [blame] | 1292 | * don't need to match Windows on that point. |
| 1293 | * |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 1294 | * RETURNS |
| 1295 | * TRUE on success |
| 1296 | * FALSE on failure |
| 1297 | * |
| 1298 | */ |
| 1299 | BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest, |
| 1300 | LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier) |
| 1301 | { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 1302 | http_request_t *request; |
Jacek Caban | e974965 | 2009-11-30 20:01:00 +0100 | [diff] [blame] | 1303 | DWORD res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE; |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 1304 | |
Francois Gouget | abfa73b | 2008-02-19 00:18:02 +0100 | [diff] [blame] | 1305 | TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_wn(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier); |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 1306 | |
| 1307 | if (!lpszHeader) |
| 1308 | return TRUE; |
| 1309 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 1310 | request = (http_request_t*) get_handle_object( hHttpRequest ); |
| 1311 | if (request && request->hdr.htype == WH_HHTTPREQ) |
| 1312 | res = HTTP_HttpAddRequestHeadersW( request, lpszHeader, dwHeaderLength, dwModifier ); |
| 1313 | if( request ) |
| 1314 | WININET_Release( &request->hdr ); |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 1315 | |
Jacek Caban | e974965 | 2009-11-30 20:01:00 +0100 | [diff] [blame] | 1316 | if(res != ERROR_SUCCESS) |
| 1317 | SetLastError(res); |
| 1318 | return res == ERROR_SUCCESS; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
Chris Morgan | a8b3216 | 2002-09-27 22:05:23 +0000 | [diff] [blame] | 1321 | /*********************************************************************** |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 1322 | * HttpAddRequestHeadersA (WININET.@) |
| 1323 | * |
| 1324 | * Adds one or more HTTP header to the request handler |
| 1325 | * |
| 1326 | * RETURNS |
| 1327 | * TRUE on success |
| 1328 | * FALSE on failure |
| 1329 | * |
| 1330 | */ |
| 1331 | BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest, |
| 1332 | LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier) |
| 1333 | { |
| 1334 | DWORD len; |
| 1335 | LPWSTR hdr; |
| 1336 | BOOL r; |
| 1337 | |
Francois Gouget | abfa73b | 2008-02-19 00:18:02 +0100 | [diff] [blame] | 1338 | TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_an(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier); |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 1339 | |
| 1340 | len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 ); |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 1341 | hdr = heap_alloc(len*sizeof(WCHAR)); |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 1342 | MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len ); |
Alexandre Julliard | 7c1925a0 | 2005-09-13 15:00:32 +0000 | [diff] [blame] | 1343 | if( dwHeaderLength != ~0U ) |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 1344 | dwHeaderLength = len; |
| 1345 | |
| 1346 | r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier ); |
| 1347 | |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 1348 | heap_free( hdr ); |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 1349 | return r; |
| 1350 | } |
| 1351 | |
Hans Leidekker | 85e9e21 | 2011-09-27 11:40:33 +0200 | [diff] [blame] | 1352 | static void free_accept_types( WCHAR **accept_types ) |
| 1353 | { |
| 1354 | WCHAR *ptr, **types = accept_types; |
| 1355 | |
| 1356 | if (!types) return; |
| 1357 | while ((ptr = *types)) |
| 1358 | { |
| 1359 | heap_free( ptr ); |
| 1360 | types++; |
| 1361 | } |
| 1362 | heap_free( accept_types ); |
| 1363 | } |
| 1364 | |
| 1365 | static WCHAR **convert_accept_types( const char **accept_types ) |
| 1366 | { |
| 1367 | unsigned int count; |
| 1368 | const char **types = accept_types; |
| 1369 | WCHAR **typesW; |
| 1370 | BOOL invalid_pointer = FALSE; |
| 1371 | |
| 1372 | if (!types) return NULL; |
| 1373 | count = 0; |
| 1374 | while (*types) |
| 1375 | { |
| 1376 | __TRY |
| 1377 | { |
| 1378 | /* find out how many there are */ |
| 1379 | if (*types && **types) |
| 1380 | { |
| 1381 | TRACE("accept type: %s\n", debugstr_a(*types)); |
| 1382 | count++; |
| 1383 | } |
| 1384 | } |
| 1385 | __EXCEPT_PAGE_FAULT |
| 1386 | { |
| 1387 | WARN("invalid accept type pointer\n"); |
| 1388 | invalid_pointer = TRUE; |
| 1389 | } |
| 1390 | __ENDTRY; |
| 1391 | types++; |
| 1392 | } |
| 1393 | if (invalid_pointer) return NULL; |
| 1394 | if (!(typesW = heap_alloc( sizeof(WCHAR *) * (count + 1) ))) return NULL; |
| 1395 | count = 0; |
| 1396 | types = accept_types; |
| 1397 | while (*types) |
| 1398 | { |
| 1399 | if (*types && **types) typesW[count++] = heap_strdupAtoW( *types ); |
| 1400 | types++; |
| 1401 | } |
| 1402 | typesW[count] = NULL; |
| 1403 | return typesW; |
| 1404 | } |
| 1405 | |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 1406 | /*********************************************************************** |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 1407 | * HttpOpenRequestA (WININET.@) |
Alberto Massari | d476a5a | 2002-11-12 02:13:04 +0000 | [diff] [blame] | 1408 | * |
| 1409 | * Open a HTTP request handle |
| 1410 | * |
| 1411 | * RETURNS |
| 1412 | * HINTERNET a HTTP request handle on success |
| 1413 | * NULL on failure |
| 1414 | * |
| 1415 | */ |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 1416 | HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession, |
| 1417 | LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion, |
| 1418 | LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes, |
Francois Gouget | d4337f2 | 2007-08-30 16:21:33 +0200 | [diff] [blame] | 1419 | DWORD dwFlags, DWORD_PTR dwContext) |
Alberto Massari | d476a5a | 2002-11-12 02:13:04 +0000 | [diff] [blame] | 1420 | { |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 1421 | LPWSTR szVerb = NULL, szObjectName = NULL; |
| 1422 | LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL; |
David Hammerton | 852c7ae | 2003-06-20 23:26:56 +0000 | [diff] [blame] | 1423 | HINTERNET rc = FALSE; |
Hans Leidekker | 493b691 | 2008-05-07 13:19:37 +0200 | [diff] [blame] | 1424 | |
Francois Gouget | d4337f2 | 2007-08-30 16:21:33 +0200 | [diff] [blame] | 1425 | TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession, |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 1426 | debugstr_a(lpszVerb), debugstr_a(lpszObjectName), |
| 1427 | debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes, |
David Hammerton | 852c7ae | 2003-06-20 23:26:56 +0000 | [diff] [blame] | 1428 | dwFlags, dwContext); |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 1429 | |
David Hammerton | 852c7ae | 2003-06-20 23:26:56 +0000 | [diff] [blame] | 1430 | if (lpszVerb) |
| 1431 | { |
Jacek Caban | 9793625 | 2009-07-17 01:17:13 +0200 | [diff] [blame] | 1432 | szVerb = heap_strdupAtoW(lpszVerb); |
Mike McCormack | 43629c9 | 2003-08-15 03:47:30 +0000 | [diff] [blame] | 1433 | if ( !szVerb ) |
David Hammerton | 852c7ae | 2003-06-20 23:26:56 +0000 | [diff] [blame] | 1434 | goto end; |
David Hammerton | 852c7ae | 2003-06-20 23:26:56 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | if (lpszObjectName) |
| 1438 | { |
Jacek Caban | 9793625 | 2009-07-17 01:17:13 +0200 | [diff] [blame] | 1439 | szObjectName = heap_strdupAtoW(lpszObjectName); |
Mike McCormack | 43629c9 | 2003-08-15 03:47:30 +0000 | [diff] [blame] | 1440 | if ( !szObjectName ) |
David Hammerton | 852c7ae | 2003-06-20 23:26:56 +0000 | [diff] [blame] | 1441 | goto end; |
David Hammerton | 852c7ae | 2003-06-20 23:26:56 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | if (lpszVersion) |
| 1445 | { |
Jacek Caban | 9793625 | 2009-07-17 01:17:13 +0200 | [diff] [blame] | 1446 | szVersion = heap_strdupAtoW(lpszVersion); |
Mike McCormack | 43629c9 | 2003-08-15 03:47:30 +0000 | [diff] [blame] | 1447 | if ( !szVersion ) |
David Hammerton | 852c7ae | 2003-06-20 23:26:56 +0000 | [diff] [blame] | 1448 | goto end; |
David Hammerton | 852c7ae | 2003-06-20 23:26:56 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | if (lpszReferrer) |
| 1452 | { |
Jacek Caban | 9793625 | 2009-07-17 01:17:13 +0200 | [diff] [blame] | 1453 | szReferrer = heap_strdupAtoW(lpszReferrer); |
Mike McCormack | 43629c9 | 2003-08-15 03:47:30 +0000 | [diff] [blame] | 1454 | if ( !szReferrer ) |
David Hammerton | 852c7ae | 2003-06-20 23:26:56 +0000 | [diff] [blame] | 1455 | goto end; |
David Hammerton | 852c7ae | 2003-06-20 23:26:56 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Hans Leidekker | 85e9e21 | 2011-09-27 11:40:33 +0200 | [diff] [blame] | 1458 | szAcceptTypes = convert_accept_types( lpszAcceptTypes ); |
| 1459 | rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName, szVersion, szReferrer, |
| 1460 | (const WCHAR **)szAcceptTypes, dwFlags, dwContext); |
David Hammerton | 852c7ae | 2003-06-20 23:26:56 +0000 | [diff] [blame] | 1461 | |
| 1462 | end: |
Hans Leidekker | 85e9e21 | 2011-09-27 11:40:33 +0200 | [diff] [blame] | 1463 | free_accept_types(szAcceptTypes); |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 1464 | heap_free(szReferrer); |
| 1465 | heap_free(szVersion); |
| 1466 | heap_free(szObjectName); |
| 1467 | heap_free(szVerb); |
David Hammerton | 852c7ae | 2003-06-20 23:26:56 +0000 | [diff] [blame] | 1468 | return rc; |
Alberto Massari | d476a5a | 2002-11-12 02:13:04 +0000 | [diff] [blame] | 1469 | } |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 1470 | |
| 1471 | /*********************************************************************** |
Rob Shearman | a9ebc70 | 2007-01-12 19:17:20 -0600 | [diff] [blame] | 1472 | * HTTP_EncodeBase64 |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 1473 | */ |
Rob Shearman | a9ebc70 | 2007-01-12 19:17:20 -0600 | [diff] [blame] | 1474 | static UINT HTTP_EncodeBase64( LPCSTR bin, unsigned int len, LPWSTR base64 ) |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 1475 | { |
| 1476 | UINT n = 0, x; |
Andrew Talbot | 46fc9c2 | 2007-02-24 21:55:12 +0000 | [diff] [blame] | 1477 | static const CHAR HTTP_Base64Enc[] = |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 1478 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 1479 | |
Rob Shearman | a9ebc70 | 2007-01-12 19:17:20 -0600 | [diff] [blame] | 1480 | while( len > 0 ) |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 1481 | { |
| 1482 | /* first 6 bits, all from bin[0] */ |
| 1483 | base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2]; |
| 1484 | x = (bin[0] & 3) << 4; |
| 1485 | |
| 1486 | /* next 6 bits, 2 from bin[0] and 4 from bin[1] */ |
Rob Shearman | a9ebc70 | 2007-01-12 19:17:20 -0600 | [diff] [blame] | 1487 | if( len == 1 ) |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 1488 | { |
| 1489 | base64[n++] = HTTP_Base64Enc[x]; |
| 1490 | base64[n++] = '='; |
| 1491 | base64[n++] = '='; |
| 1492 | break; |
| 1493 | } |
| 1494 | base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ]; |
| 1495 | x = ( bin[1] & 0x0f ) << 2; |
| 1496 | |
| 1497 | /* next 6 bits 4 from bin[1] and 2 from bin[2] */ |
Rob Shearman | a9ebc70 | 2007-01-12 19:17:20 -0600 | [diff] [blame] | 1498 | if( len == 2 ) |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 1499 | { |
| 1500 | base64[n++] = HTTP_Base64Enc[x]; |
| 1501 | base64[n++] = '='; |
| 1502 | break; |
| 1503 | } |
| 1504 | base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ]; |
| 1505 | |
| 1506 | /* last 6 bits, all from bin [2] */ |
| 1507 | base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ]; |
| 1508 | bin += 3; |
Rob Shearman | a9ebc70 | 2007-01-12 19:17:20 -0600 | [diff] [blame] | 1509 | len -= 3; |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 1510 | } |
| 1511 | base64[n] = 0; |
| 1512 | return n; |
| 1513 | } |
| 1514 | |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1515 | #define CH(x) (((x) >= 'A' && (x) <= 'Z') ? (x) - 'A' : \ |
| 1516 | ((x) >= 'a' && (x) <= 'z') ? (x) - 'a' + 26 : \ |
| 1517 | ((x) >= '0' && (x) <= '9') ? (x) - '0' + 52 : \ |
| 1518 | ((x) == '+') ? 62 : ((x) == '/') ? 63 : -1) |
| 1519 | static const signed char HTTP_Base64Dec[256] = |
| 1520 | { |
| 1521 | CH( 0),CH( 1),CH( 2),CH( 3),CH( 4),CH( 5),CH( 6),CH( 7),CH( 8),CH( 9), |
| 1522 | CH(10),CH(11),CH(12),CH(13),CH(14),CH(15),CH(16),CH(17),CH(18),CH(19), |
| 1523 | CH(20),CH(21),CH(22),CH(23),CH(24),CH(25),CH(26),CH(27),CH(28),CH(29), |
| 1524 | CH(30),CH(31),CH(32),CH(33),CH(34),CH(35),CH(36),CH(37),CH(38),CH(39), |
| 1525 | CH(40),CH(41),CH(42),CH(43),CH(44),CH(45),CH(46),CH(47),CH(48),CH(49), |
| 1526 | CH(50),CH(51),CH(52),CH(53),CH(54),CH(55),CH(56),CH(57),CH(58),CH(59), |
| 1527 | CH(60),CH(61),CH(62),CH(63),CH(64),CH(65),CH(66),CH(67),CH(68),CH(69), |
| 1528 | CH(70),CH(71),CH(72),CH(73),CH(74),CH(75),CH(76),CH(77),CH(78),CH(79), |
| 1529 | CH(80),CH(81),CH(82),CH(83),CH(84),CH(85),CH(86),CH(87),CH(88),CH(89), |
| 1530 | CH(90),CH(91),CH(92),CH(93),CH(94),CH(95),CH(96),CH(97),CH(98),CH(99), |
| 1531 | CH(100),CH(101),CH(102),CH(103),CH(104),CH(105),CH(106),CH(107),CH(108),CH(109), |
| 1532 | CH(110),CH(111),CH(112),CH(113),CH(114),CH(115),CH(116),CH(117),CH(118),CH(119), |
| 1533 | CH(120),CH(121),CH(122),CH(123),CH(124),CH(125),CH(126),CH(127),CH(128),CH(129), |
| 1534 | CH(130),CH(131),CH(132),CH(133),CH(134),CH(135),CH(136),CH(137),CH(138),CH(139), |
| 1535 | CH(140),CH(141),CH(142),CH(143),CH(144),CH(145),CH(146),CH(147),CH(148),CH(149), |
| 1536 | CH(150),CH(151),CH(152),CH(153),CH(154),CH(155),CH(156),CH(157),CH(158),CH(159), |
| 1537 | CH(160),CH(161),CH(162),CH(163),CH(164),CH(165),CH(166),CH(167),CH(168),CH(169), |
| 1538 | CH(170),CH(171),CH(172),CH(173),CH(174),CH(175),CH(176),CH(177),CH(178),CH(179), |
| 1539 | CH(180),CH(181),CH(182),CH(183),CH(184),CH(185),CH(186),CH(187),CH(188),CH(189), |
| 1540 | CH(190),CH(191),CH(192),CH(193),CH(194),CH(195),CH(196),CH(197),CH(198),CH(199), |
| 1541 | CH(200),CH(201),CH(202),CH(203),CH(204),CH(205),CH(206),CH(207),CH(208),CH(209), |
| 1542 | CH(210),CH(211),CH(212),CH(213),CH(214),CH(215),CH(216),CH(217),CH(218),CH(219), |
| 1543 | CH(220),CH(221),CH(222),CH(223),CH(224),CH(225),CH(226),CH(227),CH(228),CH(229), |
| 1544 | CH(230),CH(231),CH(232),CH(233),CH(234),CH(235),CH(236),CH(237),CH(238),CH(239), |
| 1545 | CH(240),CH(241),CH(242),CH(243),CH(244),CH(245),CH(246),CH(247),CH(248), CH(249), |
| 1546 | CH(250),CH(251),CH(252),CH(253),CH(254),CH(255), |
| 1547 | }; |
| 1548 | #undef CH |
| 1549 | |
| 1550 | /*********************************************************************** |
| 1551 | * HTTP_DecodeBase64 |
| 1552 | */ |
| 1553 | static UINT HTTP_DecodeBase64( LPCWSTR base64, LPSTR bin ) |
| 1554 | { |
| 1555 | unsigned int n = 0; |
| 1556 | |
| 1557 | while(*base64) |
| 1558 | { |
| 1559 | signed char in[4]; |
| 1560 | |
Rob Shearman | f8f9dbb | 2008-02-15 10:06:32 +0000 | [diff] [blame] | 1561 | if (base64[0] >= ARRAYSIZE(HTTP_Base64Dec) || |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1562 | ((in[0] = HTTP_Base64Dec[base64[0]]) == -1) || |
Rob Shearman | f8f9dbb | 2008-02-15 10:06:32 +0000 | [diff] [blame] | 1563 | base64[1] >= ARRAYSIZE(HTTP_Base64Dec) || |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1564 | ((in[1] = HTTP_Base64Dec[base64[1]]) == -1)) |
| 1565 | { |
| 1566 | WARN("invalid base64: %s\n", debugstr_w(base64)); |
| 1567 | return 0; |
| 1568 | } |
| 1569 | if (bin) |
| 1570 | bin[n] = (unsigned char) (in[0] << 2 | in[1] >> 4); |
| 1571 | n++; |
| 1572 | |
| 1573 | if ((base64[2] == '=') && (base64[3] == '=')) |
| 1574 | break; |
| 1575 | if (base64[2] > ARRAYSIZE(HTTP_Base64Dec) || |
| 1576 | ((in[2] = HTTP_Base64Dec[base64[2]]) == -1)) |
| 1577 | { |
| 1578 | WARN("invalid base64: %s\n", debugstr_w(&base64[2])); |
| 1579 | return 0; |
| 1580 | } |
| 1581 | if (bin) |
| 1582 | bin[n] = (unsigned char) (in[1] << 4 | in[2] >> 2); |
| 1583 | n++; |
| 1584 | |
| 1585 | if (base64[3] == '=') |
| 1586 | break; |
| 1587 | if (base64[3] > ARRAYSIZE(HTTP_Base64Dec) || |
| 1588 | ((in[3] = HTTP_Base64Dec[base64[3]]) == -1)) |
| 1589 | { |
| 1590 | WARN("invalid base64: %s\n", debugstr_w(&base64[3])); |
| 1591 | return 0; |
| 1592 | } |
| 1593 | if (bin) |
| 1594 | bin[n] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]); |
| 1595 | n++; |
| 1596 | |
| 1597 | base64 += 4; |
| 1598 | } |
| 1599 | |
| 1600 | return n; |
| 1601 | } |
| 1602 | |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 1603 | /*********************************************************************** |
Hans Leidekker | 1c5bc9a | 2008-05-09 15:18:51 +0200 | [diff] [blame] | 1604 | * HTTP_InsertAuthorization |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1605 | * |
| 1606 | * Insert or delete the authorization field in the request header. |
| 1607 | */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 1608 | static BOOL HTTP_InsertAuthorization( http_request_t *request, struct HttpAuthInfo *pAuthInfo, LPCWSTR header ) |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1609 | { |
Rob Shearman | ebaa4d9 | 2008-03-12 13:27:46 +0000 | [diff] [blame] | 1610 | if (pAuthInfo) |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1611 | { |
| 1612 | static const WCHAR wszSpace[] = {' ',0}; |
Rob Shearman | 01826e0 | 2007-11-27 14:19:50 +0000 | [diff] [blame] | 1613 | static const WCHAR wszBasic[] = {'B','a','s','i','c',0}; |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1614 | unsigned int len; |
Rob Shearman | ebaa4d9 | 2008-03-12 13:27:46 +0000 | [diff] [blame] | 1615 | WCHAR *authorization = NULL; |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1616 | |
Rob Shearman | ebaa4d9 | 2008-03-12 13:27:46 +0000 | [diff] [blame] | 1617 | if (pAuthInfo->auth_data_len) |
Rob Shearman | 01826e0 | 2007-11-27 14:19:50 +0000 | [diff] [blame] | 1618 | { |
Rob Shearman | ebaa4d9 | 2008-03-12 13:27:46 +0000 | [diff] [blame] | 1619 | /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */ |
| 1620 | len = strlenW(pAuthInfo->scheme)+1+((pAuthInfo->auth_data_len+2)*4)/3; |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 1621 | authorization = heap_alloc((len+1)*sizeof(WCHAR)); |
Rob Shearman | ebaa4d9 | 2008-03-12 13:27:46 +0000 | [diff] [blame] | 1622 | if (!authorization) |
| 1623 | return FALSE; |
| 1624 | |
| 1625 | strcpyW(authorization, pAuthInfo->scheme); |
| 1626 | strcatW(authorization, wszSpace); |
| 1627 | HTTP_EncodeBase64(pAuthInfo->auth_data, |
| 1628 | pAuthInfo->auth_data_len, |
| 1629 | authorization+strlenW(authorization)); |
| 1630 | |
| 1631 | /* clear the data as it isn't valid now that it has been sent to the |
| 1632 | * server, unless it's Basic authentication which doesn't do |
| 1633 | * connection tracking */ |
| 1634 | if (strcmpiW(pAuthInfo->scheme, wszBasic)) |
| 1635 | { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 1636 | heap_free(pAuthInfo->auth_data); |
Rob Shearman | ebaa4d9 | 2008-03-12 13:27:46 +0000 | [diff] [blame] | 1637 | pAuthInfo->auth_data = NULL; |
| 1638 | pAuthInfo->auth_data_len = 0; |
| 1639 | } |
Rob Shearman | 01826e0 | 2007-11-27 14:19:50 +0000 | [diff] [blame] | 1640 | } |
Rob Shearman | ebaa4d9 | 2008-03-12 13:27:46 +0000 | [diff] [blame] | 1641 | |
| 1642 | TRACE("Inserting authorization: %s\n", debugstr_w(authorization)); |
| 1643 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 1644 | HTTP_ProcessHeader(request, header, authorization, HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE); |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 1645 | heap_free(authorization); |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1646 | } |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1647 | return TRUE; |
| 1648 | } |
| 1649 | |
Jacek Caban | 34abacd | 2009-07-13 01:41:50 +0200 | [diff] [blame] | 1650 | static WCHAR *HTTP_BuildProxyRequestUrl(http_request_t *req) |
Hans Leidekker | 0ffe9f5 | 2008-03-30 19:16:52 +0100 | [diff] [blame] | 1651 | { |
André Hentschel | d8f036e | 2011-08-22 21:31:14 +0200 | [diff] [blame] | 1652 | static const WCHAR slash[] = { '/',0 }; |
| 1653 | static const WCHAR format[] = { 'h','t','t','p',':','/','/','%','s',':','%','u',0 }; |
| 1654 | static const WCHAR formatSSL[] = { 'h','t','t','p','s',':','/','/','%','s',':','%','u',0 }; |
| 1655 | http_session_t *session = req->session; |
Hans Leidekker | 612f3c1 | 2008-03-31 20:26:16 +0200 | [diff] [blame] | 1656 | WCHAR new_location[INTERNET_MAX_URL_LENGTH], *url; |
Hans Leidekker | 0ffe9f5 | 2008-03-30 19:16:52 +0100 | [diff] [blame] | 1657 | DWORD size; |
| 1658 | |
| 1659 | size = sizeof(new_location); |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 1660 | if (HTTP_HttpQueryInfoW(req, HTTP_QUERY_LOCATION, new_location, &size, NULL) == ERROR_SUCCESS) |
Hans Leidekker | 0ffe9f5 | 2008-03-30 19:16:52 +0100 | [diff] [blame] | 1661 | { |
André Hentschel | d8f036e | 2011-08-22 21:31:14 +0200 | [diff] [blame] | 1662 | URL_COMPONENTSW UrlComponents; |
| 1663 | |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 1664 | if (!(url = heap_alloc(size + sizeof(WCHAR)))) return NULL; |
Hans Leidekker | 0ffe9f5 | 2008-03-30 19:16:52 +0100 | [diff] [blame] | 1665 | strcpyW( url, new_location ); |
André Hentschel | d8f036e | 2011-08-22 21:31:14 +0200 | [diff] [blame] | 1666 | |
| 1667 | ZeroMemory(&UrlComponents,sizeof(URL_COMPONENTSW)); |
| 1668 | if(InternetCrackUrlW(url, 0, 0, &UrlComponents)) goto done; |
| 1669 | heap_free(url); |
Hans Leidekker | 0ffe9f5 | 2008-03-30 19:16:52 +0100 | [diff] [blame] | 1670 | } |
André Hentschel | d8f036e | 2011-08-22 21:31:14 +0200 | [diff] [blame] | 1671 | |
| 1672 | size = 16; /* "https://" + sizeof(port#) + ":/\0" */ |
| 1673 | size += strlenW( session->hostName ) + strlenW( req->path ); |
| 1674 | |
| 1675 | if (!(url = heap_alloc(size * sizeof(WCHAR)))) return NULL; |
| 1676 | |
| 1677 | if (req->hdr.dwFlags & INTERNET_FLAG_SECURE) |
| 1678 | sprintfW( url, formatSSL, session->hostName, session->hostPort ); |
Hans Leidekker | 0ffe9f5 | 2008-03-30 19:16:52 +0100 | [diff] [blame] | 1679 | else |
André Hentschel | d8f036e | 2011-08-22 21:31:14 +0200 | [diff] [blame] | 1680 | sprintfW( url, format, session->hostName, session->hostPort ); |
| 1681 | if (req->path[0] != '/') strcatW( url, slash ); |
| 1682 | strcatW( url, req->path ); |
Hans Leidekker | 0ffe9f5 | 2008-03-30 19:16:52 +0100 | [diff] [blame] | 1683 | |
André Hentschel | d8f036e | 2011-08-22 21:31:14 +0200 | [diff] [blame] | 1684 | done: |
Hans Leidekker | 0ffe9f5 | 2008-03-30 19:16:52 +0100 | [diff] [blame] | 1685 | TRACE("url=%s\n", debugstr_w(url)); |
| 1686 | return url; |
| 1687 | } |
| 1688 | |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 1689 | /*********************************************************************** |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 1690 | * HTTP_DealWithProxy |
| 1691 | */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 1692 | static BOOL HTTP_DealWithProxy(appinfo_t *hIC, http_session_t *session, http_request_t *request) |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 1693 | { |
André Hentschel | 0fda135 | 2011-08-22 21:30:55 +0200 | [diff] [blame] | 1694 | WCHAR buf[INTERNET_MAX_HOST_NAME_LENGTH]; |
| 1695 | WCHAR protoProxy[INTERNET_MAX_URL_LENGTH]; |
| 1696 | DWORD protoProxyLen = INTERNET_MAX_URL_LENGTH; |
| 1697 | WCHAR proxy[INTERNET_MAX_URL_LENGTH]; |
Hans Leidekker | 781f3f7 | 2006-10-13 15:43:54 +0200 | [diff] [blame] | 1698 | static WCHAR szNul[] = { 0 }; |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 1699 | URL_COMPONENTSW UrlComponents; |
Jacek Caban | 3aeb8eb | 2012-05-25 16:34:24 +0200 | [diff] [blame] | 1700 | server_t *new_server; |
Juan Lang | de6a0a8 | 2010-03-13 09:36:46 -0800 | [diff] [blame] | 1701 | static const WCHAR protoHttp[] = { 'h','t','t','p',0 }; |
Hans Leidekker | 0ffe9f5 | 2008-03-30 19:16:52 +0100 | [diff] [blame] | 1702 | static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }; |
| 1703 | static const WCHAR szFormat[] = { 'h','t','t','p',':','/','/','%','s',0 }; |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 1704 | |
| 1705 | memset( &UrlComponents, 0, sizeof UrlComponents ); |
| 1706 | UrlComponents.dwStructSize = sizeof UrlComponents; |
| 1707 | UrlComponents.lpszHostName = buf; |
André Hentschel | 0fda135 | 2011-08-22 21:30:55 +0200 | [diff] [blame] | 1708 | UrlComponents.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH; |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 1709 | |
Juan Lang | 7243156 | 2011-03-01 11:00:49 -0800 | [diff] [blame] | 1710 | if (!INTERNET_FindProxyForProtocol(hIC->proxy, protoHttp, protoProxy, &protoProxyLen)) |
Juan Lang | de6a0a8 | 2010-03-13 09:36:46 -0800 | [diff] [blame] | 1711 | return FALSE; |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 1712 | if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, |
Juan Lang | de6a0a8 | 2010-03-13 09:36:46 -0800 | [diff] [blame] | 1713 | protoProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) ) |
| 1714 | sprintfW(proxy, szFormat, protoProxy); |
Uwe Bonnes | 599c452 | 2003-12-15 19:47:31 +0000 | [diff] [blame] | 1715 | else |
Juan Lang | de6a0a8 | 2010-03-13 09:36:46 -0800 | [diff] [blame] | 1716 | strcpyW(proxy, protoProxy); |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 1717 | if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) ) |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 1718 | return FALSE; |
| 1719 | if( UrlComponents.dwHostNameLength == 0 ) |
| 1720 | return FALSE; |
| 1721 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 1722 | if( !request->path ) |
| 1723 | request->path = szNul; |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 1724 | |
| 1725 | if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER) |
| 1726 | UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT; |
| 1727 | |
Jacek Caban | 6c764fb | 2012-06-11 10:22:38 +0200 | [diff] [blame] | 1728 | new_server = get_server(UrlComponents.lpszHostName, UrlComponents.nPort, TRUE); |
Jacek Caban | 3aeb8eb | 2012-05-25 16:34:24 +0200 | [diff] [blame] | 1729 | if(!new_server) |
| 1730 | return FALSE; |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 1731 | |
Jacek Caban | 3aeb8eb | 2012-05-25 16:34:24 +0200 | [diff] [blame] | 1732 | server_release(request->server); |
| 1733 | request->server = new_server; |
| 1734 | |
| 1735 | TRACE("proxy server=%s port=%d\n", debugstr_w(new_server->name), new_server->port); |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 1736 | return TRUE; |
| 1737 | } |
| 1738 | |
Jacek Caban | 3aeb8eb | 2012-05-25 16:34:24 +0200 | [diff] [blame] | 1739 | static DWORD HTTP_ResolveName(http_request_t *request) |
Rob Shearman | 72575a0 | 2006-12-07 00:52:50 +0000 | [diff] [blame] | 1740 | { |
Jacek Caban | 3aeb8eb | 2012-05-25 16:34:24 +0200 | [diff] [blame] | 1741 | server_t *server = request->server; |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1742 | socklen_t addr_len; |
Alexandre Julliard | 3521e1b | 2012-06-15 17:58:23 +0200 | [diff] [blame] | 1743 | void *addr; |
Rob Shearman | 72575a0 | 2006-12-07 00:52:50 +0000 | [diff] [blame] | 1744 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1745 | if(server->addr_len) |
| 1746 | return ERROR_SUCCESS; |
| 1747 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 1748 | INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, |
Rob Shearman | 72575a0 | 2006-12-07 00:52:50 +0000 | [diff] [blame] | 1749 | INTERNET_STATUS_RESOLVING_NAME, |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1750 | server->name, |
| 1751 | (strlenW(server->name)+1) * sizeof(WCHAR)); |
Rob Shearman | 72575a0 | 2006-12-07 00:52:50 +0000 | [diff] [blame] | 1752 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1753 | addr_len = sizeof(server->addr); |
| 1754 | if (!GetAddress(server->name, server->port, (struct sockaddr *)&server->addr, &addr_len)) |
Jacek Caban | 36cb1ef | 2009-11-30 00:14:02 +0100 | [diff] [blame] | 1755 | return ERROR_INTERNET_NAME_NOT_RESOLVED; |
Rob Shearman | 72575a0 | 2006-12-07 00:52:50 +0000 | [diff] [blame] | 1756 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1757 | switch(server->addr.ss_family) { |
Juan Lang | 058e918 | 2009-07-09 11:36:00 -0700 | [diff] [blame] | 1758 | case AF_INET: |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1759 | addr = &((struct sockaddr_in *)&server->addr)->sin_addr; |
Juan Lang | 058e918 | 2009-07-09 11:36:00 -0700 | [diff] [blame] | 1760 | break; |
Juan Lang | 481c9b8 | 2009-07-09 11:42:25 -0700 | [diff] [blame] | 1761 | case AF_INET6: |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1762 | addr = &((struct sockaddr_in6 *)&server->addr)->sin6_addr; |
Juan Lang | 481c9b8 | 2009-07-09 11:42:25 -0700 | [diff] [blame] | 1763 | break; |
Juan Lang | 058e918 | 2009-07-09 11:36:00 -0700 | [diff] [blame] | 1764 | default: |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1765 | WARN("unsupported family %d\n", server->addr.ss_family); |
Jacek Caban | 36cb1ef | 2009-11-30 00:14:02 +0100 | [diff] [blame] | 1766 | return ERROR_INTERNET_NAME_NOT_RESOLVED; |
Juan Lang | 058e918 | 2009-07-09 11:36:00 -0700 | [diff] [blame] | 1767 | } |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1768 | |
| 1769 | server->addr_len = addr_len; |
| 1770 | inet_ntop(server->addr.ss_family, addr, server->addr_str, sizeof(server->addr_str)); |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 1771 | INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, |
Rob Shearman | 72575a0 | 2006-12-07 00:52:50 +0000 | [diff] [blame] | 1772 | INTERNET_STATUS_NAME_RESOLVED, |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1773 | server->addr_str, strlen(server->addr_str)+1); |
Hans Leidekker | 2f99450 | 2008-05-31 21:46:07 +0200 | [diff] [blame] | 1774 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1775 | TRACE("resolved %s to %s\n", debugstr_w(server->name), server->addr_str); |
Jacek Caban | 36cb1ef | 2009-11-30 00:14:02 +0100 | [diff] [blame] | 1776 | return ERROR_SUCCESS; |
Rob Shearman | 72575a0 | 2006-12-07 00:52:50 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
Piotr Caban | 75481bd | 2010-07-24 17:56:41 +0200 | [diff] [blame] | 1779 | static BOOL HTTP_GetRequestURL(http_request_t *req, LPWSTR buf) |
| 1780 | { |
Juan Lang | b9c348a | 2011-03-02 08:28:39 -0800 | [diff] [blame] | 1781 | static const WCHAR http[] = { 'h','t','t','p',':','/','/',0 }; |
| 1782 | static const WCHAR https[] = { 'h','t','t','p','s',':','/','/',0 }; |
| 1783 | static const WCHAR slash[] = { '/',0 }; |
Piotr Caban | 75481bd | 2010-07-24 17:56:41 +0200 | [diff] [blame] | 1784 | LPHTTPHEADERW host_header; |
Juan Lang | b9c348a | 2011-03-02 08:28:39 -0800 | [diff] [blame] | 1785 | LPCWSTR scheme; |
Piotr Caban | 75481bd | 2010-07-24 17:56:41 +0200 | [diff] [blame] | 1786 | |
| 1787 | host_header = HTTP_GetHeader(req, hostW); |
| 1788 | if(!host_header) |
| 1789 | return FALSE; |
| 1790 | |
Juan Lang | b9c348a | 2011-03-02 08:28:39 -0800 | [diff] [blame] | 1791 | if (req->hdr.dwFlags & INTERNET_FLAG_SECURE) |
| 1792 | scheme = https; |
| 1793 | else |
| 1794 | scheme = http; |
| 1795 | strcpyW(buf, scheme); |
| 1796 | strcatW(buf, host_header->lpszValue); |
| 1797 | if (req->path[0] != '/') |
| 1798 | strcatW(buf, slash); |
| 1799 | strcatW(buf, req->path); |
Piotr Caban | 75481bd | 2010-07-24 17:56:41 +0200 | [diff] [blame] | 1800 | return TRUE; |
| 1801 | } |
| 1802 | |
Jacek Caban | 5a535d6 | 2008-02-26 20:20:41 +0100 | [diff] [blame] | 1803 | |
| 1804 | /*********************************************************************** |
| 1805 | * HTTPREQ_Destroy (internal) |
| 1806 | * |
| 1807 | * Deallocate request handle |
| 1808 | * |
| 1809 | */ |
Jacek Caban | 44d633a | 2009-07-07 21:46:09 +0200 | [diff] [blame] | 1810 | static void HTTPREQ_Destroy(object_header_t *hdr) |
Jacek Caban | 5a535d6 | 2008-02-26 20:20:41 +0100 | [diff] [blame] | 1811 | { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 1812 | http_request_t *request = (http_request_t*) hdr; |
Jacek Caban | 5a535d6 | 2008-02-26 20:20:41 +0100 | [diff] [blame] | 1813 | DWORD i; |
| 1814 | |
| 1815 | TRACE("\n"); |
| 1816 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 1817 | if(request->hCacheFile) { |
Piotr Caban | 75481bd | 2010-07-24 17:56:41 +0200 | [diff] [blame] | 1818 | WCHAR url[INTERNET_MAX_URL_LENGTH]; |
Piotr Caban | 75481bd | 2010-07-24 17:56:41 +0200 | [diff] [blame] | 1819 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 1820 | CloseHandle(request->hCacheFile); |
Jacek Caban | 5a535d6 | 2008-02-26 20:20:41 +0100 | [diff] [blame] | 1821 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 1822 | if(HTTP_GetRequestURL(request, url)) { |
Juan Lang | 011b26b | 2011-03-03 15:33:40 -0800 | [diff] [blame] | 1823 | DWORD headersLen; |
| 1824 | |
| 1825 | headersLen = request->rawHeaders ? strlenW(request->rawHeaders) : 0; |
Juan Lang | 28e9229 | 2011-03-04 11:43:54 -0800 | [diff] [blame] | 1826 | CommitUrlCacheEntryW(url, request->cacheFile, request->expires, |
Juan Lang | 011b26b | 2011-03-03 15:33:40 -0800 | [diff] [blame] | 1827 | request->last_modified, NORMAL_CACHE_ENTRY, |
| 1828 | request->rawHeaders, headersLen, NULL, 0); |
Piotr Caban | 75481bd | 2010-07-24 17:56:41 +0200 | [diff] [blame] | 1829 | } |
| 1830 | } |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 1831 | heap_free(request->cacheFile); |
Jacek Caban | 5a535d6 | 2008-02-26 20:20:41 +0100 | [diff] [blame] | 1832 | |
Francois Gouget | 17929b9 | 2011-11-16 16:29:44 +0100 | [diff] [blame] | 1833 | request->read_section.DebugInfo->Spare[0] = 0; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 1834 | DeleteCriticalSection( &request->read_section ); |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 1835 | WININET_Release(&request->session->hdr); |
Jacek Caban | 5a535d6 | 2008-02-26 20:20:41 +0100 | [diff] [blame] | 1836 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 1837 | destroy_authinfo(request->authInfo); |
| 1838 | destroy_authinfo(request->proxyAuthInfo); |
Hans Leidekker | d5dca63 | 2008-10-17 13:46:25 +0200 | [diff] [blame] | 1839 | |
Jacek Caban | 3aeb8eb | 2012-05-25 16:34:24 +0200 | [diff] [blame] | 1840 | if(request->server) |
| 1841 | server_release(request->server); |
| 1842 | |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 1843 | heap_free(request->path); |
| 1844 | heap_free(request->verb); |
| 1845 | heap_free(request->rawHeaders); |
| 1846 | heap_free(request->version); |
| 1847 | heap_free(request->statusText); |
Jacek Caban | 5a535d6 | 2008-02-26 20:20:41 +0100 | [diff] [blame] | 1848 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 1849 | for (i = 0; i < request->nCustHeaders; i++) |
Jacek Caban | 5a535d6 | 2008-02-26 20:20:41 +0100 | [diff] [blame] | 1850 | { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 1851 | heap_free(request->custHeaders[i].lpszField); |
| 1852 | heap_free(request->custHeaders[i].lpszValue); |
Jacek Caban | 5a535d6 | 2008-02-26 20:20:41 +0100 | [diff] [blame] | 1853 | } |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 1854 | destroy_data_stream(request->data_stream); |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 1855 | heap_free(request->custHeaders); |
Jacek Caban | 5a535d6 | 2008-02-26 20:20:41 +0100 | [diff] [blame] | 1856 | } |
| 1857 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1858 | static void http_release_netconn(http_request_t *req, BOOL reuse) |
Jacek Caban | 7dc9bf6 | 2008-02-26 20:21:00 +0100 | [diff] [blame] | 1859 | { |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1860 | TRACE("%p %p\n",req, req->netconn); |
Jacek Caban | 7dc9bf6 | 2008-02-26 20:21:00 +0100 | [diff] [blame] | 1861 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1862 | if(!req->netconn) |
Jacek Caban | 7dc9bf6 | 2008-02-26 20:21:00 +0100 | [diff] [blame] | 1863 | return; |
| 1864 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1865 | if(reuse && req->netconn->keep_alive) { |
| 1866 | BOOL run_collector; |
| 1867 | |
| 1868 | EnterCriticalSection(&connection_pool_cs); |
| 1869 | |
| 1870 | list_add_head(&req->netconn->server->conn_pool, &req->netconn->pool_entry); |
| 1871 | req->netconn->keep_until = GetTickCount64() + COLLECT_TIME; |
| 1872 | req->netconn = NULL; |
| 1873 | |
| 1874 | run_collector = !collector_running; |
| 1875 | collector_running = TRUE; |
| 1876 | |
| 1877 | LeaveCriticalSection(&connection_pool_cs); |
| 1878 | |
| 1879 | if(run_collector) { |
| 1880 | HANDLE thread = NULL; |
| 1881 | HMODULE module; |
| 1882 | |
| 1883 | GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (const WCHAR*)WININET_hModule, &module); |
| 1884 | if(module) |
| 1885 | thread = CreateThread(NULL, 0, collect_connections_proc, NULL, 0, NULL); |
| 1886 | if(!thread) { |
| 1887 | EnterCriticalSection(&connection_pool_cs); |
| 1888 | collector_running = FALSE; |
| 1889 | LeaveCriticalSection(&connection_pool_cs); |
| 1890 | |
| 1891 | if(module) |
| 1892 | FreeLibrary(module); |
| 1893 | } |
Pierre Schweitzer | 85e2b4d | 2011-12-30 21:55:42 +0100 | [diff] [blame] | 1894 | else |
| 1895 | CloseHandle(thread); |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1896 | } |
| 1897 | return; |
| 1898 | } |
| 1899 | |
| 1900 | INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, |
Jacek Caban | 7dc9bf6 | 2008-02-26 20:21:00 +0100 | [diff] [blame] | 1901 | INTERNET_STATUS_CLOSING_CONNECTION, 0, 0); |
| 1902 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1903 | free_netconn(req->netconn); |
| 1904 | req->netconn = NULL; |
Jacek Caban | 7dc9bf6 | 2008-02-26 20:21:00 +0100 | [diff] [blame] | 1905 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1906 | INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, |
Jacek Caban | 7dc9bf6 | 2008-02-26 20:21:00 +0100 | [diff] [blame] | 1907 | INTERNET_STATUS_CONNECTION_CLOSED, 0, 0); |
| 1908 | } |
| 1909 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 1910 | static BOOL HTTP_KeepAlive(http_request_t *request) |
Juan Lang | 6ae6ea9 | 2009-08-10 15:20:45 -0700 | [diff] [blame] | 1911 | { |
| 1912 | WCHAR szVersion[10]; |
| 1913 | WCHAR szConnectionResponse[20]; |
| 1914 | DWORD dwBufferSize = sizeof(szVersion); |
| 1915 | BOOL keepalive = FALSE; |
| 1916 | |
| 1917 | /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that |
| 1918 | * the connection is keep-alive by default */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 1919 | if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_VERSION, szVersion, &dwBufferSize, NULL) == ERROR_SUCCESS |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 1920 | && !strcmpiW(szVersion, g_szHttp1_1)) |
Juan Lang | 6ae6ea9 | 2009-08-10 15:20:45 -0700 | [diff] [blame] | 1921 | { |
| 1922 | keepalive = TRUE; |
| 1923 | } |
| 1924 | |
| 1925 | dwBufferSize = sizeof(szConnectionResponse); |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 1926 | if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_PROXY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) == ERROR_SUCCESS |
| 1927 | || HTTP_HttpQueryInfoW(request, HTTP_QUERY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) == ERROR_SUCCESS) |
Juan Lang | 6ae6ea9 | 2009-08-10 15:20:45 -0700 | [diff] [blame] | 1928 | { |
| 1929 | keepalive = !strcmpiW(szConnectionResponse, szKeepAlive); |
| 1930 | } |
| 1931 | |
| 1932 | return keepalive; |
| 1933 | } |
| 1934 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1935 | static void HTTPREQ_CloseConnection(object_header_t *hdr) |
| 1936 | { |
| 1937 | http_request_t *req = (http_request_t*)hdr; |
| 1938 | |
Jacek Caban | c400117 | 2012-07-02 17:15:04 +0200 | [diff] [blame] | 1939 | http_release_netconn(req, drain_content(req, FALSE)); |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
Jacek Caban | 44d633a | 2009-07-07 21:46:09 +0200 | [diff] [blame] | 1942 | static DWORD HTTPREQ_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode) |
Jacek Caban | e2933c2 | 2008-03-12 02:23:20 +0100 | [diff] [blame] | 1943 | { |
Jacek Caban | 34abacd | 2009-07-13 01:41:50 +0200 | [diff] [blame] | 1944 | http_request_t *req = (http_request_t*)hdr; |
Jacek Caban | 0e010d8 | 2008-03-12 02:23:48 +0100 | [diff] [blame] | 1945 | |
Jacek Caban | e2933c2 | 2008-03-12 02:23:20 +0100 | [diff] [blame] | 1946 | switch(option) { |
Juan Lang | 6ae6ea9 | 2009-08-10 15:20:45 -0700 | [diff] [blame] | 1947 | case INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO: |
| 1948 | { |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 1949 | http_session_t *session = req->session; |
Juan Lang | 6ae6ea9 | 2009-08-10 15:20:45 -0700 | [diff] [blame] | 1950 | INTERNET_DIAGNOSTIC_SOCKET_INFO *info = buffer; |
| 1951 | |
| 1952 | FIXME("INTERNET_DIAGNOSTIC_SOCKET_INFO stub\n"); |
| 1953 | |
| 1954 | if (*size < sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO)) |
| 1955 | return ERROR_INSUFFICIENT_BUFFER; |
| 1956 | *size = sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO); |
| 1957 | /* FIXME: can't get a SOCKET from our connection since we don't use |
| 1958 | * winsock |
| 1959 | */ |
| 1960 | info->Socket = 0; |
| 1961 | /* FIXME: get source port from req->netConnection */ |
| 1962 | info->SourcePort = 0; |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 1963 | info->DestPort = session->hostPort; |
Juan Lang | 6ae6ea9 | 2009-08-10 15:20:45 -0700 | [diff] [blame] | 1964 | info->Flags = 0; |
| 1965 | if (HTTP_KeepAlive(req)) |
| 1966 | info->Flags |= IDSI_FLAG_KEEP_ALIVE; |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 1967 | if (session->appInfo->proxy && session->appInfo->proxy[0] != 0) |
Juan Lang | 6ae6ea9 | 2009-08-10 15:20:45 -0700 | [diff] [blame] | 1968 | info->Flags |= IDSI_FLAG_PROXY; |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 1969 | if (req->netconn->useSSL) |
Juan Lang | 6ae6ea9 | 2009-08-10 15:20:45 -0700 | [diff] [blame] | 1970 | info->Flags |= IDSI_FLAG_SECURE; |
| 1971 | |
| 1972 | return ERROR_SUCCESS; |
| 1973 | } |
| 1974 | |
Jacek Caban | 0133683 | 2012-06-04 18:01:19 +0200 | [diff] [blame] | 1975 | case 98: |
| 1976 | TRACE("Queried undocumented option 98, forwarding to INTERNET_OPTION_SECURITY_FLAGS\n"); |
| 1977 | /* fall through */ |
Aric Stewart | c6ae945 | 2009-06-23 15:29:00 +0900 | [diff] [blame] | 1978 | case INTERNET_OPTION_SECURITY_FLAGS: |
| 1979 | { |
Juan Lang | 948173b | 2010-09-30 13:33:47 -0700 | [diff] [blame] | 1980 | DWORD flags; |
Aric Stewart | c6ae945 | 2009-06-23 15:29:00 +0900 | [diff] [blame] | 1981 | |
| 1982 | if (*size < sizeof(ULONG)) |
| 1983 | return ERROR_INSUFFICIENT_BUFFER; |
| 1984 | |
| 1985 | *size = sizeof(DWORD); |
Jacek Caban | 59a0ab5 | 2012-05-25 16:34:43 +0200 | [diff] [blame] | 1986 | flags = req->netconn ? req->netconn->security_flags : req->security_flags | req->server->security_flags; |
Juan Lang | 948173b | 2010-09-30 13:33:47 -0700 | [diff] [blame] | 1987 | *(DWORD *)buffer = flags; |
Jacek Caban | f3dd75d | 2012-05-25 16:35:12 +0200 | [diff] [blame] | 1988 | |
| 1989 | TRACE("INTERNET_OPTION_SECURITY_FLAGS %x\n", flags); |
Aric Stewart | c6ae945 | 2009-06-23 15:29:00 +0900 | [diff] [blame] | 1990 | return ERROR_SUCCESS; |
| 1991 | } |
| 1992 | |
Jacek Caban | e2933c2 | 2008-03-12 02:23:20 +0100 | [diff] [blame] | 1993 | case INTERNET_OPTION_HANDLE_TYPE: |
| 1994 | TRACE("INTERNET_OPTION_HANDLE_TYPE\n"); |
| 1995 | |
| 1996 | if (*size < sizeof(ULONG)) |
| 1997 | return ERROR_INSUFFICIENT_BUFFER; |
| 1998 | |
| 1999 | *size = sizeof(DWORD); |
| 2000 | *(DWORD*)buffer = INTERNET_HANDLE_TYPE_HTTP_REQUEST; |
| 2001 | return ERROR_SUCCESS; |
Jacek Caban | 0e010d8 | 2008-03-12 02:23:48 +0100 | [diff] [blame] | 2002 | |
| 2003 | case INTERNET_OPTION_URL: { |
| 2004 | WCHAR url[INTERNET_MAX_URL_LENGTH]; |
| 2005 | HTTPHEADERW *host; |
| 2006 | DWORD len; |
Alexander Morozov | 4f51b6c | 2008-07-03 16:34:40 +0400 | [diff] [blame] | 2007 | WCHAR *pch; |
Jacek Caban | 0e010d8 | 2008-03-12 02:23:48 +0100 | [diff] [blame] | 2008 | |
Alexander Morozov | 4f51b6c | 2008-07-03 16:34:40 +0400 | [diff] [blame] | 2009 | static const WCHAR httpW[] = {'h','t','t','p',':','/','/',0}; |
Jacek Caban | 0e010d8 | 2008-03-12 02:23:48 +0100 | [diff] [blame] | 2010 | |
| 2011 | TRACE("INTERNET_OPTION_URL\n"); |
| 2012 | |
| 2013 | host = HTTP_GetHeader(req, hostW); |
Alexander Morozov | 4f51b6c | 2008-07-03 16:34:40 +0400 | [diff] [blame] | 2014 | strcpyW(url, httpW); |
| 2015 | strcatW(url, host->lpszValue); |
| 2016 | if (NULL != (pch = strchrW(url + strlenW(httpW), ':'))) |
| 2017 | *pch = 0; |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 2018 | strcatW(url, req->path); |
Alexander Morozov | 4f51b6c | 2008-07-03 16:34:40 +0400 | [diff] [blame] | 2019 | |
Jacek Caban | 0e010d8 | 2008-03-12 02:23:48 +0100 | [diff] [blame] | 2020 | TRACE("INTERNET_OPTION_URL: %s\n",debugstr_w(url)); |
| 2021 | |
| 2022 | if(unicode) { |
| 2023 | len = (strlenW(url)+1) * sizeof(WCHAR); |
| 2024 | if(*size < len) |
| 2025 | return ERROR_INSUFFICIENT_BUFFER; |
| 2026 | |
| 2027 | *size = len; |
| 2028 | strcpyW(buffer, url); |
| 2029 | return ERROR_SUCCESS; |
| 2030 | }else { |
| 2031 | len = WideCharToMultiByte(CP_ACP, 0, url, -1, buffer, *size, NULL, NULL); |
| 2032 | if(len > *size) |
| 2033 | return ERROR_INSUFFICIENT_BUFFER; |
| 2034 | |
| 2035 | *size = len; |
| 2036 | return ERROR_SUCCESS; |
| 2037 | } |
| 2038 | } |
Jacek Caban | ce6a228 | 2008-03-12 02:24:06 +0100 | [diff] [blame] | 2039 | |
Hans Leidekker | c293285 | 2009-06-16 15:13:52 +0200 | [diff] [blame] | 2040 | case INTERNET_OPTION_CACHE_TIMESTAMPS: { |
| 2041 | INTERNET_CACHE_ENTRY_INFOW *info; |
| 2042 | INTERNET_CACHE_TIMESTAMPS *ts = buffer; |
| 2043 | WCHAR url[INTERNET_MAX_URL_LENGTH]; |
| 2044 | DWORD nbytes, error; |
| 2045 | BOOL ret; |
| 2046 | |
| 2047 | TRACE("INTERNET_OPTION_CACHE_TIMESTAMPS\n"); |
| 2048 | |
| 2049 | if (*size < sizeof(*ts)) |
| 2050 | { |
| 2051 | *size = sizeof(*ts); |
| 2052 | return ERROR_INSUFFICIENT_BUFFER; |
| 2053 | } |
| 2054 | nbytes = 0; |
| 2055 | HTTP_GetRequestURL(req, url); |
| 2056 | ret = GetUrlCacheEntryInfoW(url, NULL, &nbytes); |
| 2057 | error = GetLastError(); |
| 2058 | if (!ret && error == ERROR_INSUFFICIENT_BUFFER) |
| 2059 | { |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 2060 | if (!(info = heap_alloc(nbytes))) |
Hans Leidekker | c293285 | 2009-06-16 15:13:52 +0200 | [diff] [blame] | 2061 | return ERROR_OUTOFMEMORY; |
| 2062 | |
| 2063 | GetUrlCacheEntryInfoW(url, info, &nbytes); |
| 2064 | |
| 2065 | ts->ftExpires = info->ExpireTime; |
| 2066 | ts->ftLastModified = info->LastModifiedTime; |
| 2067 | |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 2068 | heap_free(info); |
Hans Leidekker | c293285 | 2009-06-16 15:13:52 +0200 | [diff] [blame] | 2069 | *size = sizeof(*ts); |
| 2070 | return ERROR_SUCCESS; |
| 2071 | } |
| 2072 | return error; |
| 2073 | } |
| 2074 | |
Jacek Caban | ce6a228 | 2008-03-12 02:24:06 +0100 | [diff] [blame] | 2075 | case INTERNET_OPTION_DATAFILE_NAME: { |
| 2076 | DWORD req_size; |
| 2077 | |
| 2078 | TRACE("INTERNET_OPTION_DATAFILE_NAME\n"); |
| 2079 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 2080 | if(!req->cacheFile) { |
Jacek Caban | ce6a228 | 2008-03-12 02:24:06 +0100 | [diff] [blame] | 2081 | *size = 0; |
| 2082 | return ERROR_INTERNET_ITEM_NOT_FOUND; |
| 2083 | } |
| 2084 | |
| 2085 | if(unicode) { |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 2086 | req_size = (lstrlenW(req->cacheFile)+1) * sizeof(WCHAR); |
Jacek Caban | ce6a228 | 2008-03-12 02:24:06 +0100 | [diff] [blame] | 2087 | if(*size < req_size) |
| 2088 | return ERROR_INSUFFICIENT_BUFFER; |
| 2089 | |
| 2090 | *size = req_size; |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 2091 | memcpy(buffer, req->cacheFile, *size); |
Jacek Caban | ce6a228 | 2008-03-12 02:24:06 +0100 | [diff] [blame] | 2092 | return ERROR_SUCCESS; |
| 2093 | }else { |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 2094 | req_size = WideCharToMultiByte(CP_ACP, 0, req->cacheFile, -1, NULL, 0, NULL, NULL); |
Jacek Caban | ce6a228 | 2008-03-12 02:24:06 +0100 | [diff] [blame] | 2095 | if (req_size > *size) |
| 2096 | return ERROR_INSUFFICIENT_BUFFER; |
| 2097 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 2098 | *size = WideCharToMultiByte(CP_ACP, 0, req->cacheFile, |
Jacek Caban | ce6a228 | 2008-03-12 02:24:06 +0100 | [diff] [blame] | 2099 | -1, buffer, *size, NULL, NULL); |
| 2100 | return ERROR_SUCCESS; |
| 2101 | } |
| 2102 | } |
Jacek Caban | 7e63f95 | 2008-03-12 02:24:23 +0100 | [diff] [blame] | 2103 | |
| 2104 | case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT: { |
| 2105 | PCCERT_CONTEXT context; |
| 2106 | |
Juan Lang | 56ebc04 | 2010-09-30 13:09:04 -0700 | [diff] [blame] | 2107 | if(*size < sizeof(INTERNET_CERTIFICATE_INFOA)) { |
| 2108 | *size = sizeof(INTERNET_CERTIFICATE_INFOA); |
Jacek Caban | 7e63f95 | 2008-03-12 02:24:23 +0100 | [diff] [blame] | 2109 | return ERROR_INSUFFICIENT_BUFFER; |
| 2110 | } |
| 2111 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 2112 | context = (PCCERT_CONTEXT)NETCON_GetCert(req->netconn); |
Jacek Caban | 7e63f95 | 2008-03-12 02:24:23 +0100 | [diff] [blame] | 2113 | if(context) { |
Juan Lang | 56ebc04 | 2010-09-30 13:09:04 -0700 | [diff] [blame] | 2114 | INTERNET_CERTIFICATE_INFOA *info = (INTERNET_CERTIFICATE_INFOA*)buffer; |
Jacek Caban | 7e63f95 | 2008-03-12 02:24:23 +0100 | [diff] [blame] | 2115 | DWORD len; |
| 2116 | |
Thomas Faber | 7d24e59 | 2012-06-17 16:18:36 +0200 | [diff] [blame] | 2117 | memset(info, 0, sizeof(*info)); |
Jacek Caban | 7e63f95 | 2008-03-12 02:24:23 +0100 | [diff] [blame] | 2118 | info->ftExpiry = context->pCertInfo->NotAfter; |
| 2119 | info->ftStart = context->pCertInfo->NotBefore; |
Juan Lang | 56ebc04 | 2010-09-30 13:09:04 -0700 | [diff] [blame] | 2120 | len = CertNameToStrA(context->dwCertEncodingType, |
Jacek Caban | 37e483d | 2012-05-30 12:13:53 +0200 | [diff] [blame] | 2121 | &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG, NULL, 0); |
Juan Lang | 56ebc04 | 2010-09-30 13:09:04 -0700 | [diff] [blame] | 2122 | info->lpszSubjectInfo = LocalAlloc(0, len); |
| 2123 | if(info->lpszSubjectInfo) |
| 2124 | CertNameToStrA(context->dwCertEncodingType, |
Jacek Caban | 37e483d | 2012-05-30 12:13:53 +0200 | [diff] [blame] | 2125 | &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG, |
Juan Lang | 56ebc04 | 2010-09-30 13:09:04 -0700 | [diff] [blame] | 2126 | info->lpszSubjectInfo, len); |
| 2127 | len = CertNameToStrA(context->dwCertEncodingType, |
Jacek Caban | 37e483d | 2012-05-30 12:13:53 +0200 | [diff] [blame] | 2128 | &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG, NULL, 0); |
Juan Lang | 56ebc04 | 2010-09-30 13:09:04 -0700 | [diff] [blame] | 2129 | info->lpszIssuerInfo = LocalAlloc(0, len); |
| 2130 | if(info->lpszIssuerInfo) |
| 2131 | CertNameToStrA(context->dwCertEncodingType, |
Jacek Caban | 37e483d | 2012-05-30 12:13:53 +0200 | [diff] [blame] | 2132 | &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR|CERT_NAME_STR_CRLF_FLAG, |
Juan Lang | 56ebc04 | 2010-09-30 13:09:04 -0700 | [diff] [blame] | 2133 | info->lpszIssuerInfo, len); |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 2134 | info->dwKeySize = NETCON_GetCipherStrength(req->netconn); |
Jacek Caban | 7e63f95 | 2008-03-12 02:24:23 +0100 | [diff] [blame] | 2135 | CertFreeCertificateContext(context); |
| 2136 | return ERROR_SUCCESS; |
| 2137 | } |
Thomas Faber | 0b60b8f | 2012-06-15 11:38:24 +0200 | [diff] [blame] | 2138 | return ERROR_NOT_SUPPORTED; |
Jacek Caban | 7e63f95 | 2008-03-12 02:24:23 +0100 | [diff] [blame] | 2139 | } |
Hans Leidekker | 848cd8a | 2012-01-13 15:15:20 +0100 | [diff] [blame] | 2140 | case INTERNET_OPTION_CONNECT_TIMEOUT: |
| 2141 | if (*size < sizeof(DWORD)) |
| 2142 | return ERROR_INSUFFICIENT_BUFFER; |
| 2143 | |
| 2144 | *size = sizeof(DWORD); |
| 2145 | *(DWORD *)buffer = req->connect_timeout; |
| 2146 | return ERROR_SUCCESS; |
Jacek Caban | 27af819 | 2012-05-24 15:24:43 +0200 | [diff] [blame] | 2147 | case INTERNET_OPTION_REQUEST_FLAGS: { |
| 2148 | DWORD flags = 0; |
Jacek Caban | c040056 | 2012-05-23 18:24:02 +0200 | [diff] [blame] | 2149 | |
| 2150 | if (*size < sizeof(DWORD)) |
| 2151 | return ERROR_INSUFFICIENT_BUFFER; |
| 2152 | |
Jacek Caban | 27af819 | 2012-05-24 15:24:43 +0200 | [diff] [blame] | 2153 | /* FIXME: Add support for: |
| 2154 | * INTERNET_REQFLAG_FROM_CACHE |
| 2155 | * INTERNET_REQFLAG_CACHE_WRITE_DISABLED |
| 2156 | */ |
Jacek Caban | c040056 | 2012-05-23 18:24:02 +0200 | [diff] [blame] | 2157 | |
Jacek Caban | 27af819 | 2012-05-24 15:24:43 +0200 | [diff] [blame] | 2158 | if(req->session->appInfo->proxy) |
| 2159 | flags |= INTERNET_REQFLAG_VIA_PROXY; |
| 2160 | if(!req->rawHeaders) |
| 2161 | flags |= INTERNET_REQFLAG_NO_HEADERS; |
| 2162 | |
| 2163 | TRACE("INTERNET_OPTION_REQUEST_FLAGS returning %x\n", flags); |
| 2164 | |
| 2165 | *size = sizeof(DWORD); |
| 2166 | *(DWORD*)buffer = flags; |
Jacek Caban | c040056 | 2012-05-23 18:24:02 +0200 | [diff] [blame] | 2167 | return ERROR_SUCCESS; |
Jacek Caban | e2933c2 | 2008-03-12 02:23:20 +0100 | [diff] [blame] | 2168 | } |
Jacek Caban | 27af819 | 2012-05-24 15:24:43 +0200 | [diff] [blame] | 2169 | } |
Jacek Caban | e2933c2 | 2008-03-12 02:23:20 +0100 | [diff] [blame] | 2170 | |
Hans Leidekker | 80dd367 | 2010-05-25 12:19:32 +0200 | [diff] [blame] | 2171 | return INET_QueryOption(hdr, option, buffer, size, unicode); |
Jacek Caban | e2933c2 | 2008-03-12 02:23:20 +0100 | [diff] [blame] | 2172 | } |
| 2173 | |
Jacek Caban | 44d633a | 2009-07-07 21:46:09 +0200 | [diff] [blame] | 2174 | static DWORD HTTPREQ_SetOption(object_header_t *hdr, DWORD option, void *buffer, DWORD size) |
Jacek Caban | 0e33eee | 2008-02-26 20:22:02 +0100 | [diff] [blame] | 2175 | { |
Jacek Caban | 34abacd | 2009-07-13 01:41:50 +0200 | [diff] [blame] | 2176 | http_request_t *req = (http_request_t*)hdr; |
Jacek Caban | 0e33eee | 2008-02-26 20:22:02 +0100 | [diff] [blame] | 2177 | |
| 2178 | switch(option) { |
Jacek Caban | d8b5f46 | 2012-06-07 15:40:45 +0200 | [diff] [blame] | 2179 | case 99: /* Undocumented, seems to be INTERNET_OPTION_SECURITY_FLAGS with argument validation */ |
| 2180 | TRACE("Undocumented option 99\n"); |
| 2181 | |
| 2182 | if (!buffer || size != sizeof(DWORD)) |
| 2183 | return ERROR_INVALID_PARAMETER; |
| 2184 | if(*(DWORD*)buffer & ~SECURITY_SET_MASK) |
| 2185 | return ERROR_INTERNET_OPTION_NOT_SETTABLE; |
| 2186 | |
| 2187 | /* fall through */ |
Juan Lang | 77c4ade | 2010-09-28 16:46:41 -0700 | [diff] [blame] | 2188 | case INTERNET_OPTION_SECURITY_FLAGS: |
| 2189 | { |
| 2190 | DWORD flags; |
| 2191 | |
| 2192 | if (!buffer || size != sizeof(DWORD)) |
| 2193 | return ERROR_INVALID_PARAMETER; |
| 2194 | flags = *(DWORD *)buffer; |
Jacek Caban | 8dbc39b | 2012-06-06 17:38:30 +0200 | [diff] [blame] | 2195 | TRACE("INTERNET_OPTION_SECURITY_FLAGS %08x\n", flags); |
| 2196 | flags &= SECURITY_SET_MASK; |
| 2197 | req->security_flags |= flags; |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 2198 | if(req->netconn) |
Jacek Caban | 8dbc39b | 2012-06-06 17:38:30 +0200 | [diff] [blame] | 2199 | req->netconn->security_flags |= flags; |
Juan Lang | 77c4ade | 2010-09-28 16:46:41 -0700 | [diff] [blame] | 2200 | return ERROR_SUCCESS; |
| 2201 | } |
Hans Leidekker | 848cd8a | 2012-01-13 15:15:20 +0100 | [diff] [blame] | 2202 | case INTERNET_OPTION_CONNECT_TIMEOUT: |
Hans Leidekker | 6522393 | 2012-01-13 15:15:48 +0100 | [diff] [blame] | 2203 | if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER; |
Hans Leidekker | 848cd8a | 2012-01-13 15:15:20 +0100 | [diff] [blame] | 2204 | req->connect_timeout = *(DWORD *)buffer; |
| 2205 | return ERROR_SUCCESS; |
| 2206 | |
Jacek Caban | 0e33eee | 2008-02-26 20:22:02 +0100 | [diff] [blame] | 2207 | case INTERNET_OPTION_SEND_TIMEOUT: |
Hans Leidekker | 6522393 | 2012-01-13 15:15:48 +0100 | [diff] [blame] | 2208 | if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER; |
| 2209 | req->send_timeout = *(DWORD *)buffer; |
| 2210 | return ERROR_SUCCESS; |
| 2211 | |
Jacek Caban | 0e33eee | 2008-02-26 20:22:02 +0100 | [diff] [blame] | 2212 | case INTERNET_OPTION_RECEIVE_TIMEOUT: |
Hans Leidekker | 6522393 | 2012-01-13 15:15:48 +0100 | [diff] [blame] | 2213 | if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER; |
| 2214 | req->receive_timeout = *(DWORD *)buffer; |
| 2215 | return ERROR_SUCCESS; |
Hans Leidekker | a57cc6d | 2008-12-02 17:15:07 +0100 | [diff] [blame] | 2216 | |
| 2217 | case INTERNET_OPTION_USERNAME: |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 2218 | heap_free(req->session->userName); |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 2219 | if (!(req->session->userName = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY; |
Hans Leidekker | a57cc6d | 2008-12-02 17:15:07 +0100 | [diff] [blame] | 2220 | return ERROR_SUCCESS; |
| 2221 | |
| 2222 | case INTERNET_OPTION_PASSWORD: |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 2223 | heap_free(req->session->password); |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 2224 | if (!(req->session->password = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY; |
Hans Leidekker | a57cc6d | 2008-12-02 17:15:07 +0100 | [diff] [blame] | 2225 | return ERROR_SUCCESS; |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 2226 | case INTERNET_OPTION_HTTP_DECODING: |
| 2227 | if(size != sizeof(BOOL)) |
| 2228 | return ERROR_INVALID_PARAMETER; |
| 2229 | req->decoding = *(BOOL*)buffer; |
| 2230 | return ERROR_SUCCESS; |
Jacek Caban | 0e33eee | 2008-02-26 20:22:02 +0100 | [diff] [blame] | 2231 | } |
| 2232 | |
Jacek Caban | 4863257 | 2012-03-08 12:19:14 +0100 | [diff] [blame] | 2233 | return INET_SetOption(hdr, option, buffer, size); |
Jacek Caban | 0e33eee | 2008-02-26 20:22:02 +0100 | [diff] [blame] | 2234 | } |
| 2235 | |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 2236 | /* read some more data into the read buffer (the read section must be held) */ |
Jacek Caban | 1d96e20 | 2009-11-30 19:59:56 +0100 | [diff] [blame] | 2237 | static DWORD read_more_data( http_request_t *req, int maxlen ) |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2238 | { |
Jacek Caban | 358e7b7 | 2009-11-30 19:59:40 +0100 | [diff] [blame] | 2239 | DWORD res; |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2240 | int len; |
| 2241 | |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 2242 | if (req->read_pos) |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2243 | { |
| 2244 | /* move existing data to the start of the buffer */ |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 2245 | if(req->read_size) |
| 2246 | memmove( req->read_buf, req->read_buf + req->read_pos, req->read_size ); |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2247 | req->read_pos = 0; |
| 2248 | } |
| 2249 | |
| 2250 | if (maxlen == -1) maxlen = sizeof(req->read_buf); |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 2251 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 2252 | res = NETCON_recv( req->netconn, req->read_buf + req->read_size, |
Jacek Caban | 358e7b7 | 2009-11-30 19:59:40 +0100 | [diff] [blame] | 2253 | maxlen - req->read_size, 0, &len ); |
Jacek Caban | 1d96e20 | 2009-11-30 19:59:56 +0100 | [diff] [blame] | 2254 | if(res == ERROR_SUCCESS) |
| 2255 | req->read_size += len; |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 2256 | |
Jacek Caban | 1d96e20 | 2009-11-30 19:59:56 +0100 | [diff] [blame] | 2257 | return res; |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2258 | } |
| 2259 | |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 2260 | /* remove some amount of data from the read buffer (the read section must be held) */ |
Jacek Caban | 34abacd | 2009-07-13 01:41:50 +0200 | [diff] [blame] | 2261 | static void remove_data( http_request_t *req, int count ) |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2262 | { |
| 2263 | if (!(req->read_size -= count)) req->read_pos = 0; |
| 2264 | else req->read_pos += count; |
| 2265 | } |
| 2266 | |
Jacek Caban | 34abacd | 2009-07-13 01:41:50 +0200 | [diff] [blame] | 2267 | static BOOL read_line( http_request_t *req, LPSTR buffer, DWORD *len ) |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2268 | { |
| 2269 | int count, bytes_read, pos = 0; |
Jacek Caban | 1d96e20 | 2009-11-30 19:59:56 +0100 | [diff] [blame] | 2270 | DWORD res; |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2271 | |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 2272 | EnterCriticalSection( &req->read_section ); |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2273 | for (;;) |
| 2274 | { |
Jacek Caban | 26bbf07 | 2009-05-29 23:34:37 +0200 | [diff] [blame] | 2275 | BYTE *eol = memchr( req->read_buf + req->read_pos, '\n', req->read_size ); |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2276 | |
| 2277 | if (eol) |
| 2278 | { |
| 2279 | count = eol - (req->read_buf + req->read_pos); |
| 2280 | bytes_read = count + 1; |
| 2281 | } |
| 2282 | else count = bytes_read = req->read_size; |
| 2283 | |
| 2284 | count = min( count, *len - pos ); |
| 2285 | memcpy( buffer + pos, req->read_buf + req->read_pos, count ); |
| 2286 | pos += count; |
| 2287 | remove_data( req, bytes_read ); |
| 2288 | if (eol) break; |
| 2289 | |
Jacek Caban | 1d96e20 | 2009-11-30 19:59:56 +0100 | [diff] [blame] | 2290 | if ((res = read_more_data( req, -1 )) != ERROR_SUCCESS || !req->read_size) |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2291 | { |
| 2292 | *len = 0; |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 2293 | TRACE( "returning empty string %u\n", res); |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 2294 | LeaveCriticalSection( &req->read_section ); |
Jacek Caban | 1d96e20 | 2009-11-30 19:59:56 +0100 | [diff] [blame] | 2295 | INTERNET_SetLastError(res); |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2296 | return FALSE; |
| 2297 | } |
| 2298 | } |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 2299 | LeaveCriticalSection( &req->read_section ); |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2300 | |
| 2301 | if (pos < *len) |
| 2302 | { |
| 2303 | if (pos && buffer[pos - 1] == '\r') pos--; |
| 2304 | *len = pos + 1; |
| 2305 | } |
| 2306 | buffer[*len - 1] = 0; |
| 2307 | TRACE( "returning %s\n", debugstr_a(buffer)); |
| 2308 | return TRUE; |
| 2309 | } |
| 2310 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2311 | /* check if we have reached the end of the data to read (the read section must be held) */ |
| 2312 | static BOOL end_of_read_data( http_request_t *req ) |
| 2313 | { |
| 2314 | return !req->read_size && req->data_stream->vtbl->end_of_data(req->data_stream, req); |
| 2315 | } |
| 2316 | |
| 2317 | /* fetch some more data into the read buffer (the read section must be held) */ |
Jacek Caban | 193da88 | 2011-05-23 16:10:09 +0200 | [diff] [blame] | 2318 | static DWORD refill_read_buffer(http_request_t *req, read_mode_t read_mode, DWORD *read_bytes) |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2319 | { |
Dan Kegel | 766eeca | 2012-07-07 17:34:52 -0700 | [diff] [blame] | 2320 | DWORD res, read=0, want; |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2321 | |
| 2322 | if(req->read_size == sizeof(req->read_buf)) |
| 2323 | return ERROR_SUCCESS; |
| 2324 | |
| 2325 | if(req->read_pos) { |
| 2326 | if(req->read_size) |
| 2327 | memmove(req->read_buf, req->read_buf+req->read_pos, req->read_size); |
| 2328 | req->read_pos = 0; |
| 2329 | } |
| 2330 | |
Dan Kegel | 766eeca | 2012-07-07 17:34:52 -0700 | [diff] [blame] | 2331 | want = sizeof(req->read_buf) - req->read_size; |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2332 | res = req->data_stream->vtbl->read(req->data_stream, req, req->read_buf+req->read_size, |
Dan Kegel | 766eeca | 2012-07-07 17:34:52 -0700 | [diff] [blame] | 2333 | want, &read, read_mode); |
| 2334 | assert(read <= want); |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2335 | req->read_size += read; |
| 2336 | |
| 2337 | TRACE("read %u bytes, read_size %u\n", read, req->read_size); |
Jacek Caban | 193da88 | 2011-05-23 16:10:09 +0200 | [diff] [blame] | 2338 | if(read_bytes) |
| 2339 | *read_bytes = read; |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2340 | return res; |
| 2341 | } |
| 2342 | |
| 2343 | /* return the size of data available to be read immediately (the read section must be held) */ |
| 2344 | static DWORD get_avail_data( http_request_t *req ) |
| 2345 | { |
| 2346 | return req->read_size + req->data_stream->vtbl->get_avail_data(req->data_stream, req); |
| 2347 | } |
| 2348 | |
| 2349 | static DWORD netconn_get_avail_data(data_stream_t *stream, http_request_t *req) |
| 2350 | { |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 2351 | netconn_stream_t *netconn_stream = (netconn_stream_t*)stream; |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2352 | DWORD avail = 0; |
| 2353 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 2354 | if(req->netconn) |
| 2355 | NETCON_query_data_available(req->netconn, &avail); |
| 2356 | return netconn_stream->content_length == ~0u |
| 2357 | ? avail |
| 2358 | : min(avail, netconn_stream->content_length-netconn_stream->content_read); |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | static BOOL netconn_end_of_data(data_stream_t *stream, http_request_t *req) |
| 2362 | { |
| 2363 | netconn_stream_t *netconn_stream = (netconn_stream_t*)stream; |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 2364 | return netconn_stream->content_read == netconn_stream->content_length || !req->netconn; |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2365 | } |
| 2366 | |
| 2367 | static DWORD netconn_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size, |
| 2368 | DWORD *read, read_mode_t read_mode) |
| 2369 | { |
| 2370 | netconn_stream_t *netconn_stream = (netconn_stream_t*)stream; |
Jacek Caban | aa612ec | 2011-05-02 11:25:47 +0200 | [diff] [blame] | 2371 | int len = 0; |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2372 | |
| 2373 | size = min(size, netconn_stream->content_length-netconn_stream->content_read); |
| 2374 | |
Dan Kegel | 766eeca | 2012-07-07 17:34:52 -0700 | [diff] [blame] | 2375 | if(read_mode == READMODE_NOBLOCK) { |
| 2376 | DWORD avail = netconn_get_avail_data(stream, req); |
| 2377 | if (size > avail) |
| 2378 | size = avail; |
| 2379 | } |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2380 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 2381 | if(size && req->netconn) { |
| 2382 | if(NETCON_recv(req->netconn, buf, size, read_mode == READMODE_SYNC ? MSG_WAITALL : 0, &len) != ERROR_SUCCESS) |
Jacek Caban | aa612ec | 2011-05-02 11:25:47 +0200 | [diff] [blame] | 2383 | len = 0; |
Jacek Caban | cfdc539 | 2011-06-10 14:47:27 +0200 | [diff] [blame] | 2384 | if(!len) |
| 2385 | netconn_stream->content_length = netconn_stream->content_read; |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2386 | } |
| 2387 | |
Jacek Caban | aa612ec | 2011-05-02 11:25:47 +0200 | [diff] [blame] | 2388 | netconn_stream->content_read += *read = len; |
| 2389 | TRACE("read %u bytes\n", len); |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2390 | return ERROR_SUCCESS; |
| 2391 | } |
| 2392 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 2393 | static BOOL netconn_drain_content(data_stream_t *stream, http_request_t *req) |
| 2394 | { |
| 2395 | netconn_stream_t *netconn_stream = (netconn_stream_t*)stream; |
| 2396 | BYTE buf[1024]; |
| 2397 | DWORD avail; |
| 2398 | int len; |
| 2399 | |
| 2400 | if(netconn_end_of_data(stream, req)) |
| 2401 | return TRUE; |
| 2402 | |
| 2403 | do { |
| 2404 | avail = netconn_get_avail_data(stream, req); |
| 2405 | if(!avail) |
| 2406 | return FALSE; |
| 2407 | |
| 2408 | if(NETCON_recv(req->netconn, buf, min(avail, sizeof(buf)), 0, &len) != ERROR_SUCCESS) |
| 2409 | return FALSE; |
| 2410 | |
| 2411 | netconn_stream->content_read += len; |
| 2412 | }while(netconn_stream->content_read < netconn_stream->content_length); |
| 2413 | |
| 2414 | return TRUE; |
| 2415 | } |
| 2416 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2417 | static void netconn_destroy(data_stream_t *stream) |
| 2418 | { |
| 2419 | } |
| 2420 | |
| 2421 | static const data_stream_vtbl_t netconn_stream_vtbl = { |
| 2422 | netconn_get_avail_data, |
| 2423 | netconn_end_of_data, |
| 2424 | netconn_read, |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 2425 | netconn_drain_content, |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2426 | netconn_destroy |
| 2427 | }; |
| 2428 | |
| 2429 | /* read some more data into the read buffer (the read section must be held) */ |
| 2430 | static DWORD read_more_chunked_data(chunked_stream_t *stream, http_request_t *req, int maxlen) |
| 2431 | { |
| 2432 | DWORD res; |
| 2433 | int len; |
| 2434 | |
| 2435 | if (stream->buf_pos) |
| 2436 | { |
| 2437 | /* move existing data to the start of the buffer */ |
| 2438 | if(stream->buf_size) |
| 2439 | memmove(stream->buf, stream->buf + stream->buf_pos, stream->buf_size); |
| 2440 | stream->buf_pos = 0; |
| 2441 | } |
| 2442 | |
| 2443 | if (maxlen == -1) maxlen = sizeof(stream->buf); |
| 2444 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 2445 | res = NETCON_recv( req->netconn, stream->buf + stream->buf_size, |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2446 | maxlen - stream->buf_size, 0, &len ); |
| 2447 | if(res == ERROR_SUCCESS) |
| 2448 | stream->buf_size += len; |
| 2449 | |
| 2450 | return res; |
| 2451 | } |
| 2452 | |
| 2453 | /* remove some amount of data from the read buffer (the read section must be held) */ |
| 2454 | static void remove_chunked_data(chunked_stream_t *stream, int count) |
| 2455 | { |
| 2456 | if (!(stream->buf_size -= count)) stream->buf_pos = 0; |
| 2457 | else stream->buf_pos += count; |
| 2458 | } |
| 2459 | |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 2460 | /* discard data contents until we reach end of line (the read section must be held) */ |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2461 | static DWORD discard_chunked_eol(chunked_stream_t *stream, http_request_t *req) |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2462 | { |
Jacek Caban | 1d96e20 | 2009-11-30 19:59:56 +0100 | [diff] [blame] | 2463 | DWORD res; |
| 2464 | |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2465 | do |
| 2466 | { |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2467 | BYTE *eol = memchr(stream->buf + stream->buf_pos, '\n', stream->buf_size); |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2468 | if (eol) |
| 2469 | { |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2470 | remove_chunked_data(stream, (eol + 1) - (stream->buf + stream->buf_pos)); |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2471 | break; |
| 2472 | } |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2473 | stream->buf_pos = stream->buf_size = 0; /* discard everything */ |
| 2474 | if ((res = read_more_chunked_data(stream, req, -1)) != ERROR_SUCCESS) return res; |
| 2475 | } while (stream->buf_size); |
Jacek Caban | 1d96e20 | 2009-11-30 19:59:56 +0100 | [diff] [blame] | 2476 | return ERROR_SUCCESS; |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2477 | } |
| 2478 | |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 2479 | /* read the size of the next chunk (the read section must be held) */ |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2480 | static DWORD start_next_chunk(chunked_stream_t *stream, http_request_t *req) |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2481 | { |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2482 | /* TODOO */ |
Jacek Caban | 1d96e20 | 2009-11-30 19:59:56 +0100 | [diff] [blame] | 2483 | DWORD chunk_size = 0, res; |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2484 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2485 | if(stream->chunk_size != ~0u && (res = discard_chunked_eol(stream, req)) != ERROR_SUCCESS) |
| 2486 | return res; |
| 2487 | |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2488 | for (;;) |
| 2489 | { |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2490 | while (stream->buf_size) |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2491 | { |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2492 | char ch = stream->buf[stream->buf_pos]; |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2493 | if (ch >= '0' && ch <= '9') chunk_size = chunk_size * 16 + ch - '0'; |
| 2494 | else if (ch >= 'a' && ch <= 'f') chunk_size = chunk_size * 16 + ch - 'a' + 10; |
| 2495 | else if (ch >= 'A' && ch <= 'F') chunk_size = chunk_size * 16 + ch - 'A' + 10; |
| 2496 | else if (ch == ';' || ch == '\r' || ch == '\n') |
| 2497 | { |
| 2498 | TRACE( "reading %u byte chunk\n", chunk_size ); |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2499 | stream->chunk_size = chunk_size; |
| 2500 | req->contentLength += chunk_size; |
| 2501 | return discard_chunked_eol(stream, req); |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2502 | } |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2503 | remove_chunked_data(stream, 1); |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2504 | } |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2505 | if ((res = read_more_chunked_data(stream, req, -1)) != ERROR_SUCCESS) return res; |
| 2506 | if (!stream->buf_size) |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2507 | { |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2508 | stream->chunk_size = 0; |
Jacek Caban | 1d96e20 | 2009-11-30 19:59:56 +0100 | [diff] [blame] | 2509 | return ERROR_SUCCESS; |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2510 | } |
| 2511 | } |
| 2512 | } |
| 2513 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2514 | static DWORD chunked_get_avail_data(data_stream_t *stream, http_request_t *req) |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2515 | { |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2516 | /* Allow reading only from read buffer */ |
| 2517 | return 0; |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2518 | } |
| 2519 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2520 | static BOOL chunked_end_of_data(data_stream_t *stream, http_request_t *req) |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2521 | { |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2522 | chunked_stream_t *chunked_stream = (chunked_stream_t*)stream; |
| 2523 | return !chunked_stream->chunk_size; |
| 2524 | } |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2525 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2526 | static DWORD chunked_read(data_stream_t *stream, http_request_t *req, BYTE *buf, DWORD size, |
| 2527 | DWORD *read, read_mode_t read_mode) |
| 2528 | { |
| 2529 | chunked_stream_t *chunked_stream = (chunked_stream_t*)stream; |
| 2530 | DWORD read_bytes = 0, ret_read = 0, res = ERROR_SUCCESS; |
| 2531 | |
| 2532 | if(chunked_stream->chunk_size == ~0u) { |
| 2533 | res = start_next_chunk(chunked_stream, req); |
| 2534 | if(res != ERROR_SUCCESS) |
| 2535 | return res; |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2536 | } |
| 2537 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2538 | while(size && chunked_stream->chunk_size) { |
| 2539 | if(chunked_stream->buf_size) { |
| 2540 | read_bytes = min(size, min(chunked_stream->buf_size, chunked_stream->chunk_size)); |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2541 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2542 | /* this could block */ |
| 2543 | if(read_mode == READMODE_NOBLOCK && read_bytes == chunked_stream->chunk_size) |
| 2544 | break; |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2545 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2546 | memcpy(buf+ret_read, chunked_stream->buf+chunked_stream->buf_pos, read_bytes); |
| 2547 | remove_chunked_data(chunked_stream, read_bytes); |
| 2548 | }else { |
| 2549 | read_bytes = min(size, chunked_stream->chunk_size); |
Jacek Caban | a76db21 | 2009-06-07 21:19:06 +0200 | [diff] [blame] | 2550 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2551 | if(read_mode == READMODE_NOBLOCK) { |
| 2552 | DWORD avail; |
Jacek Caban | a76db21 | 2009-06-07 21:19:06 +0200 | [diff] [blame] | 2553 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 2554 | if(!NETCON_query_data_available(req->netconn, &avail) || !avail) |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2555 | break; |
| 2556 | if(read_bytes > avail) |
| 2557 | read_bytes = avail; |
| 2558 | |
| 2559 | /* this could block */ |
| 2560 | if(read_bytes == chunked_stream->chunk_size) |
| 2561 | break; |
| 2562 | } |
| 2563 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 2564 | res = NETCON_recv(req->netconn, (char *)buf+ret_read, read_bytes, 0, (int*)&read_bytes); |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2565 | if(res != ERROR_SUCCESS) |
Jacek Caban | a76db21 | 2009-06-07 21:19:06 +0200 | [diff] [blame] | 2566 | break; |
| 2567 | } |
| 2568 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2569 | chunked_stream->chunk_size -= read_bytes; |
| 2570 | size -= read_bytes; |
| 2571 | ret_read += read_bytes; |
| 2572 | if(!chunked_stream->chunk_size) { |
| 2573 | assert(read_mode != READMODE_NOBLOCK); |
| 2574 | res = start_next_chunk(chunked_stream, req); |
| 2575 | if(res != ERROR_SUCCESS) |
| 2576 | break; |
Jacek Caban | a76db21 | 2009-06-07 21:19:06 +0200 | [diff] [blame] | 2577 | } |
Jacek Caban | a76db21 | 2009-06-07 21:19:06 +0200 | [diff] [blame] | 2578 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2579 | if(read_mode == READMODE_ASYNC) |
| 2580 | read_mode = READMODE_NOBLOCK; |
Jacek Caban | a76db21 | 2009-06-07 21:19:06 +0200 | [diff] [blame] | 2581 | } |
| 2582 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2583 | TRACE("read %u bytes\n", ret_read); |
| 2584 | *read = ret_read; |
Jacek Caban | 2bb3f4f | 2011-03-17 01:01:19 +0100 | [diff] [blame] | 2585 | return res; |
| 2586 | } |
| 2587 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 2588 | static BOOL chunked_drain_content(data_stream_t *stream, http_request_t *req) |
| 2589 | { |
| 2590 | chunked_stream_t *chunked_stream = (chunked_stream_t*)stream; |
| 2591 | |
| 2592 | /* FIXME: we can do better */ |
| 2593 | return !chunked_stream->chunk_size; |
| 2594 | } |
| 2595 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2596 | static void chunked_destroy(data_stream_t *stream) |
Jacek Caban | 2bb3f4f | 2011-03-17 01:01:19 +0100 | [diff] [blame] | 2597 | { |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2598 | chunked_stream_t *chunked_stream = (chunked_stream_t*)stream; |
| 2599 | heap_free(chunked_stream); |
Jacek Caban | a76db21 | 2009-06-07 21:19:06 +0200 | [diff] [blame] | 2600 | } |
| 2601 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2602 | static const data_stream_vtbl_t chunked_stream_vtbl = { |
| 2603 | chunked_get_avail_data, |
| 2604 | chunked_end_of_data, |
| 2605 | chunked_read, |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 2606 | chunked_drain_content, |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2607 | chunked_destroy |
| 2608 | }; |
| 2609 | |
| 2610 | /* set the request content length based on the headers */ |
Jacek Caban | 5240e40 | 2012-05-03 12:19:47 +0200 | [diff] [blame] | 2611 | static DWORD set_content_length(http_request_t *request) |
Jacek Caban | a76db21 | 2009-06-07 21:19:06 +0200 | [diff] [blame] | 2612 | { |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2613 | static const WCHAR szChunked[] = {'c','h','u','n','k','e','d',0}; |
| 2614 | WCHAR encoding[20]; |
| 2615 | DWORD size; |
| 2616 | |
Jacek Caban | 5240e40 | 2012-05-03 12:19:47 +0200 | [diff] [blame] | 2617 | if(request->status_code == HTTP_STATUS_NO_CONTENT) { |
Jacek Caban | a890e3a | 2011-05-13 13:48:15 +0200 | [diff] [blame] | 2618 | request->contentLength = request->netconn_stream.content_length = 0; |
| 2619 | return ERROR_SUCCESS; |
| 2620 | } |
| 2621 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2622 | size = sizeof(request->contentLength); |
| 2623 | if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH, |
| 2624 | &request->contentLength, &size, NULL) != ERROR_SUCCESS) |
| 2625 | request->contentLength = ~0u; |
| 2626 | request->netconn_stream.content_length = request->contentLength; |
| 2627 | request->netconn_stream.content_read = request->read_size; |
| 2628 | |
| 2629 | size = sizeof(encoding); |
| 2630 | if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_TRANSFER_ENCODING, encoding, &size, NULL) == ERROR_SUCCESS && |
| 2631 | !strcmpiW(encoding, szChunked)) |
| 2632 | { |
| 2633 | chunked_stream_t *chunked_stream; |
| 2634 | |
| 2635 | chunked_stream = heap_alloc(sizeof(*chunked_stream)); |
| 2636 | if(!chunked_stream) |
| 2637 | return ERROR_OUTOFMEMORY; |
| 2638 | |
| 2639 | chunked_stream->data_stream.vtbl = &chunked_stream_vtbl; |
| 2640 | chunked_stream->buf_size = chunked_stream->buf_pos = 0; |
| 2641 | chunked_stream->chunk_size = ~0u; |
| 2642 | |
| 2643 | if(request->read_size) { |
| 2644 | memcpy(chunked_stream->buf, request->read_buf+request->read_pos, request->read_size); |
| 2645 | chunked_stream->buf_size = request->read_size; |
| 2646 | request->read_size = request->read_pos = 0; |
| 2647 | } |
| 2648 | |
| 2649 | request->data_stream = &chunked_stream->data_stream; |
| 2650 | request->contentLength = ~0u; |
| 2651 | request->read_chunked = TRUE; |
| 2652 | } |
| 2653 | |
| 2654 | if(request->decoding) { |
| 2655 | int encoding_idx; |
| 2656 | |
| 2657 | static const WCHAR gzipW[] = {'g','z','i','p',0}; |
| 2658 | |
| 2659 | encoding_idx = HTTP_GetCustomHeaderIndex(request, szContent_Encoding, 0, FALSE); |
| 2660 | if(encoding_idx != -1 && !strcmpiW(request->custHeaders[encoding_idx].lpszValue, gzipW)) |
| 2661 | return init_gzip_stream(request); |
| 2662 | } |
| 2663 | |
| 2664 | return ERROR_SUCCESS; |
Jacek Caban | a76db21 | 2009-06-07 21:19:06 +0200 | [diff] [blame] | 2665 | } |
| 2666 | |
Jacek Caban | c0293df | 2011-06-10 14:47:04 +0200 | [diff] [blame] | 2667 | static void send_request_complete(http_request_t *req, DWORD_PTR result, DWORD error) |
Jacek Caban | 1293106 | 2009-01-13 00:28:25 +0100 | [diff] [blame] | 2668 | { |
| 2669 | INTERNET_ASYNC_RESULT iar; |
Jacek Caban | c0293df | 2011-06-10 14:47:04 +0200 | [diff] [blame] | 2670 | |
| 2671 | iar.dwResult = result; |
| 2672 | iar.dwError = error; |
| 2673 | |
| 2674 | INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE, &iar, |
| 2675 | sizeof(INTERNET_ASYNC_RESULT)); |
| 2676 | } |
| 2677 | |
| 2678 | static void HTTP_ReceiveRequestData(http_request_t *req, BOOL first_notif) |
| 2679 | { |
| 2680 | DWORD res, read = 0, avail = 0; |
Jacek Caban | 193da88 | 2011-05-23 16:10:09 +0200 | [diff] [blame] | 2681 | read_mode_t mode; |
Jacek Caban | 1293106 | 2009-01-13 00:28:25 +0100 | [diff] [blame] | 2682 | |
| 2683 | TRACE("%p\n", req); |
| 2684 | |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 2685 | EnterCriticalSection( &req->read_section ); |
Jacek Caban | 193da88 | 2011-05-23 16:10:09 +0200 | [diff] [blame] | 2686 | |
| 2687 | mode = first_notif && req->read_size ? READMODE_NOBLOCK : READMODE_ASYNC; |
| 2688 | res = refill_read_buffer(req, mode, &read); |
Jacek Caban | c0293df | 2011-06-10 14:47:04 +0200 | [diff] [blame] | 2689 | if(res == ERROR_SUCCESS && !first_notif) |
| 2690 | avail = get_avail_data(req); |
Jacek Caban | 193da88 | 2011-05-23 16:10:09 +0200 | [diff] [blame] | 2691 | |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 2692 | LeaveCriticalSection( &req->read_section ); |
Jacek Caban | 1293106 | 2009-01-13 00:28:25 +0100 | [diff] [blame] | 2693 | |
Jacek Caban | 193da88 | 2011-05-23 16:10:09 +0200 | [diff] [blame] | 2694 | if(res != ERROR_SUCCESS || (mode != READMODE_NOBLOCK && !read)) { |
| 2695 | WARN("res %u read %u, closing connection\n", res, read); |
| 2696 | http_release_netconn(req, FALSE); |
| 2697 | } |
| 2698 | |
Jacek Caban | c0293df | 2011-06-10 14:47:04 +0200 | [diff] [blame] | 2699 | if(res == ERROR_SUCCESS) |
Jacek Caban | 8e37ed5 | 2011-06-10 14:47:16 +0200 | [diff] [blame] | 2700 | send_request_complete(req, req->session->hdr.dwInternalFlags & INET_OPENURL ? (DWORD_PTR)req->hdr.hInternet : 1, avail); |
Jacek Caban | c0293df | 2011-06-10 14:47:04 +0200 | [diff] [blame] | 2701 | else |
| 2702 | send_request_complete(req, 0, res); |
Jacek Caban | 1293106 | 2009-01-13 00:28:25 +0100 | [diff] [blame] | 2703 | } |
| 2704 | |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 2705 | /* read data from the http connection (the read section must be held) */ |
Jacek Caban | 34abacd | 2009-07-13 01:41:50 +0200 | [diff] [blame] | 2706 | static DWORD HTTPREQ_Read(http_request_t *req, void *buffer, DWORD size, DWORD *read, BOOL sync) |
Jacek Caban | 3b4ca69 | 2008-03-02 19:35:11 +0100 | [diff] [blame] | 2707 | { |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2708 | DWORD current_read = 0, ret_read = 0; |
| 2709 | read_mode_t read_mode; |
| 2710 | DWORD res = ERROR_SUCCESS; |
| 2711 | |
| 2712 | read_mode = req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC ? READMODE_ASYNC : READMODE_SYNC; |
Jacek Caban | 3b4ca69 | 2008-03-02 19:35:11 +0100 | [diff] [blame] | 2713 | |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 2714 | EnterCriticalSection( &req->read_section ); |
Jacek Caban | a76db21 | 2009-06-07 21:19:06 +0200 | [diff] [blame] | 2715 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2716 | if(req->read_size) { |
| 2717 | ret_read = min(size, req->read_size); |
| 2718 | memcpy(buffer, req->read_buf+req->read_pos, ret_read); |
| 2719 | req->read_size -= ret_read; |
| 2720 | req->read_pos += ret_read; |
| 2721 | if(read_mode == READMODE_ASYNC) |
| 2722 | read_mode = READMODE_NOBLOCK; |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2723 | } |
Jacek Caban | 3b4ca69 | 2008-03-02 19:35:11 +0100 | [diff] [blame] | 2724 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2725 | if(ret_read < size) { |
| 2726 | res = req->data_stream->vtbl->read(req->data_stream, req, (BYTE*)buffer+ret_read, size-ret_read, ¤t_read, read_mode); |
| 2727 | ret_read += current_read; |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2728 | } |
Jacek Caban | 3b4ca69 | 2008-03-02 19:35:11 +0100 | [diff] [blame] | 2729 | |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 2730 | LeaveCriticalSection( &req->read_section ); |
| 2731 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2732 | *read = ret_read; |
| 2733 | TRACE( "retrieved %u bytes (%u)\n", ret_read, req->contentLength ); |
Jacek Caban | 3b4ca69 | 2008-03-02 19:35:11 +0100 | [diff] [blame] | 2734 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2735 | if(req->hCacheFile && res == ERROR_SUCCESS && ret_read) { |
| 2736 | BOOL res; |
| 2737 | DWORD written; |
| 2738 | |
| 2739 | res = WriteFile(req->hCacheFile, buffer, ret_read, &written, NULL); |
Jacek Caban | 3b4ca69 | 2008-03-02 19:35:11 +0100 | [diff] [blame] | 2740 | if(!res) |
| 2741 | WARN("WriteFile failed: %u\n", GetLastError()); |
| 2742 | } |
| 2743 | |
Jacek Caban | 086eb61 | 2011-05-13 13:48:00 +0200 | [diff] [blame] | 2744 | if(size && !ret_read) |
| 2745 | http_release_netconn(req, res == ERROR_SUCCESS); |
Jacek Caban | 3b4ca69 | 2008-03-02 19:35:11 +0100 | [diff] [blame] | 2746 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2747 | return res; |
Jacek Caban | 3b4ca69 | 2008-03-02 19:35:11 +0100 | [diff] [blame] | 2748 | } |
| 2749 | |
Jacek Caban | c400117 | 2012-07-02 17:15:04 +0200 | [diff] [blame] | 2750 | static BOOL drain_content(http_request_t *req, BOOL blocking) |
| 2751 | { |
| 2752 | BOOL ret; |
| 2753 | |
| 2754 | if(!req->netconn || req->contentLength == -1) |
| 2755 | return FALSE; |
| 2756 | |
| 2757 | if(!strcmpW(req->verb, szHEAD)) |
| 2758 | return TRUE; |
| 2759 | |
| 2760 | if(!blocking) |
| 2761 | return req->data_stream->vtbl->drain_content(req->data_stream, req); |
| 2762 | |
| 2763 | EnterCriticalSection( &req->read_section ); |
| 2764 | |
| 2765 | while(1) { |
| 2766 | DWORD bytes_read, res; |
| 2767 | BYTE buf[4096]; |
| 2768 | |
| 2769 | res = HTTPREQ_Read(req, buf, sizeof(buf), &bytes_read, TRUE); |
| 2770 | if(res != ERROR_SUCCESS) { |
| 2771 | ret = FALSE; |
| 2772 | break; |
| 2773 | } |
| 2774 | if(!bytes_read) { |
| 2775 | ret = TRUE; |
| 2776 | break; |
| 2777 | } |
| 2778 | } |
| 2779 | |
| 2780 | LeaveCriticalSection( &req->read_section ); |
| 2781 | return ret; |
| 2782 | } |
Hans Leidekker | 058761f | 2008-03-26 22:22:04 +0100 | [diff] [blame] | 2783 | |
Jacek Caban | 44d633a | 2009-07-07 21:46:09 +0200 | [diff] [blame] | 2784 | static DWORD HTTPREQ_ReadFile(object_header_t *hdr, void *buffer, DWORD size, DWORD *read) |
Hans Leidekker | 058761f | 2008-03-26 22:22:04 +0100 | [diff] [blame] | 2785 | { |
Jacek Caban | 34abacd | 2009-07-13 01:41:50 +0200 | [diff] [blame] | 2786 | http_request_t *req = (http_request_t*)hdr; |
Piotr Caban | 21ced8d | 2010-07-20 00:33:24 +0200 | [diff] [blame] | 2787 | DWORD res; |
| 2788 | |
| 2789 | EnterCriticalSection( &req->read_section ); |
| 2790 | if(hdr->dwError == INTERNET_HANDLE_IN_USE) |
| 2791 | hdr->dwError = ERROR_INTERNET_INTERNAL_ERROR; |
| 2792 | |
| 2793 | res = HTTPREQ_Read(req, buffer, size, read, TRUE); |
| 2794 | if(res == ERROR_SUCCESS) |
| 2795 | res = hdr->dwError; |
| 2796 | LeaveCriticalSection( &req->read_section ); |
| 2797 | |
| 2798 | return res; |
Jacek Caban | 3b4ca69 | 2008-03-02 19:35:11 +0100 | [diff] [blame] | 2799 | } |
| 2800 | |
Hans Leidekker | b013ad1 | 2009-01-15 16:41:42 +0100 | [diff] [blame] | 2801 | static void HTTPREQ_AsyncReadFileExAProc(WORKREQUEST *workRequest) |
Jacek Caban | d597fd1 | 2008-03-03 18:07:20 +0100 | [diff] [blame] | 2802 | { |
| 2803 | struct WORKREQ_INTERNETREADFILEEXA const *data = &workRequest->u.InternetReadFileExA; |
Jacek Caban | 34abacd | 2009-07-13 01:41:50 +0200 | [diff] [blame] | 2804 | http_request_t *req = (http_request_t*)workRequest->hdr; |
Jacek Caban | d597fd1 | 2008-03-03 18:07:20 +0100 | [diff] [blame] | 2805 | DWORD res; |
| 2806 | |
| 2807 | TRACE("INTERNETREADFILEEXA %p\n", workRequest->hdr); |
| 2808 | |
| 2809 | res = HTTPREQ_Read(req, data->lpBuffersOut->lpvBuffer, |
| 2810 | data->lpBuffersOut->dwBufferLength, &data->lpBuffersOut->dwBufferLength, TRUE); |
| 2811 | |
Jacek Caban | c0293df | 2011-06-10 14:47:04 +0200 | [diff] [blame] | 2812 | send_request_complete(req, res == ERROR_SUCCESS, res); |
Jacek Caban | d597fd1 | 2008-03-03 18:07:20 +0100 | [diff] [blame] | 2813 | } |
| 2814 | |
Jacek Caban | 44d633a | 2009-07-07 21:46:09 +0200 | [diff] [blame] | 2815 | static DWORD HTTPREQ_ReadFileExA(object_header_t *hdr, INTERNET_BUFFERSA *buffers, |
Jacek Caban | d597fd1 | 2008-03-03 18:07:20 +0100 | [diff] [blame] | 2816 | DWORD flags, DWORD_PTR context) |
| 2817 | { |
Jacek Caban | 34abacd | 2009-07-13 01:41:50 +0200 | [diff] [blame] | 2818 | http_request_t *req = (http_request_t*)hdr; |
Piotr Caban | 21ced8d | 2010-07-20 00:33:24 +0200 | [diff] [blame] | 2819 | DWORD res, size, read, error = ERROR_SUCCESS; |
Jacek Caban | d597fd1 | 2008-03-03 18:07:20 +0100 | [diff] [blame] | 2820 | |
| 2821 | if (flags & ~(IRF_ASYNC|IRF_NO_WAIT)) |
| 2822 | FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT)); |
| 2823 | |
| 2824 | if (buffers->dwStructSize != sizeof(*buffers)) |
| 2825 | return ERROR_INVALID_PARAMETER; |
| 2826 | |
| 2827 | INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0); |
| 2828 | |
Piotr Caban | 898531d | 2010-07-01 13:10:46 +0200 | [diff] [blame] | 2829 | if (hdr->dwFlags & INTERNET_FLAG_ASYNC) |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2830 | { |
| 2831 | WORKREQUEST workRequest; |
Jacek Caban | d597fd1 | 2008-03-03 18:07:20 +0100 | [diff] [blame] | 2832 | |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 2833 | if (TryEnterCriticalSection( &req->read_section )) |
| 2834 | { |
| 2835 | if (get_avail_data(req)) |
| 2836 | { |
| 2837 | res = HTTPREQ_Read(req, buffers->lpvBuffer, buffers->dwBufferLength, |
| 2838 | &buffers->dwBufferLength, FALSE); |
Piotr Caban | 21ced8d | 2010-07-20 00:33:24 +0200 | [diff] [blame] | 2839 | size = buffers->dwBufferLength; |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 2840 | LeaveCriticalSection( &req->read_section ); |
| 2841 | goto done; |
| 2842 | } |
| 2843 | LeaveCriticalSection( &req->read_section ); |
| 2844 | } |
| 2845 | |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2846 | workRequest.asyncproc = HTTPREQ_AsyncReadFileExAProc; |
| 2847 | workRequest.hdr = WININET_AddRef(&req->hdr); |
| 2848 | workRequest.u.InternetReadFileExA.lpBuffersOut = buffers; |
Jacek Caban | d597fd1 | 2008-03-03 18:07:20 +0100 | [diff] [blame] | 2849 | |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2850 | INTERNET_AsyncCall(&workRequest); |
Jacek Caban | d597fd1 | 2008-03-03 18:07:20 +0100 | [diff] [blame] | 2851 | |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2852 | return ERROR_IO_PENDING; |
Jacek Caban | d597fd1 | 2008-03-03 18:07:20 +0100 | [diff] [blame] | 2853 | } |
| 2854 | |
Piotr Caban | 21ced8d | 2010-07-20 00:33:24 +0200 | [diff] [blame] | 2855 | read = 0; |
| 2856 | size = buffers->dwBufferLength; |
| 2857 | |
| 2858 | EnterCriticalSection( &req->read_section ); |
| 2859 | if(hdr->dwError == ERROR_SUCCESS) |
| 2860 | hdr->dwError = INTERNET_HANDLE_IN_USE; |
| 2861 | else if(hdr->dwError == INTERNET_HANDLE_IN_USE) |
| 2862 | hdr->dwError = ERROR_INTERNET_INTERNAL_ERROR; |
| 2863 | |
| 2864 | while(1) { |
| 2865 | res = HTTPREQ_Read(req, (char*)buffers->lpvBuffer+read, size-read, |
| 2866 | &buffers->dwBufferLength, !(flags & IRF_NO_WAIT)); |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2867 | if(res != ERROR_SUCCESS) |
Piotr Caban | 21ced8d | 2010-07-20 00:33:24 +0200 | [diff] [blame] | 2868 | break; |
| 2869 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2870 | read += buffers->dwBufferLength; |
| 2871 | if(read == size || end_of_read_data(req)) |
Piotr Caban | 21ced8d | 2010-07-20 00:33:24 +0200 | [diff] [blame] | 2872 | break; |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2873 | |
Piotr Caban | 21ced8d | 2010-07-20 00:33:24 +0200 | [diff] [blame] | 2874 | LeaveCriticalSection( &req->read_section ); |
| 2875 | |
| 2876 | INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED, |
| 2877 | &buffers->dwBufferLength, sizeof(buffers->dwBufferLength)); |
| 2878 | INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, |
| 2879 | INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0); |
| 2880 | |
| 2881 | EnterCriticalSection( &req->read_section ); |
| 2882 | } |
| 2883 | |
| 2884 | if(hdr->dwError == INTERNET_HANDLE_IN_USE) |
| 2885 | hdr->dwError = ERROR_SUCCESS; |
| 2886 | else |
| 2887 | error = hdr->dwError; |
| 2888 | |
| 2889 | LeaveCriticalSection( &req->read_section ); |
| 2890 | size = buffers->dwBufferLength; |
| 2891 | buffers->dwBufferLength = read; |
Jacek Caban | d597fd1 | 2008-03-03 18:07:20 +0100 | [diff] [blame] | 2892 | |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 2893 | done: |
Jacek Caban | d597fd1 | 2008-03-03 18:07:20 +0100 | [diff] [blame] | 2894 | if (res == ERROR_SUCCESS) { |
Jacek Caban | d597fd1 | 2008-03-03 18:07:20 +0100 | [diff] [blame] | 2895 | INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED, |
| 2896 | &size, sizeof(size)); |
| 2897 | } |
| 2898 | |
Piotr Caban | 21ced8d | 2010-07-20 00:33:24 +0200 | [diff] [blame] | 2899 | return res==ERROR_SUCCESS ? error : res; |
Jacek Caban | d597fd1 | 2008-03-03 18:07:20 +0100 | [diff] [blame] | 2900 | } |
| 2901 | |
Hans Leidekker | b013ad1 | 2009-01-15 16:41:42 +0100 | [diff] [blame] | 2902 | static void HTTPREQ_AsyncReadFileExWProc(WORKREQUEST *workRequest) |
| 2903 | { |
| 2904 | struct WORKREQ_INTERNETREADFILEEXW const *data = &workRequest->u.InternetReadFileExW; |
Jacek Caban | 34abacd | 2009-07-13 01:41:50 +0200 | [diff] [blame] | 2905 | http_request_t *req = (http_request_t*)workRequest->hdr; |
Hans Leidekker | b013ad1 | 2009-01-15 16:41:42 +0100 | [diff] [blame] | 2906 | DWORD res; |
| 2907 | |
| 2908 | TRACE("INTERNETREADFILEEXW %p\n", workRequest->hdr); |
| 2909 | |
| 2910 | res = HTTPREQ_Read(req, data->lpBuffersOut->lpvBuffer, |
| 2911 | data->lpBuffersOut->dwBufferLength, &data->lpBuffersOut->dwBufferLength, TRUE); |
| 2912 | |
Jacek Caban | c0293df | 2011-06-10 14:47:04 +0200 | [diff] [blame] | 2913 | send_request_complete(req, res == ERROR_SUCCESS, res); |
Hans Leidekker | b013ad1 | 2009-01-15 16:41:42 +0100 | [diff] [blame] | 2914 | } |
| 2915 | |
Jacek Caban | 44d633a | 2009-07-07 21:46:09 +0200 | [diff] [blame] | 2916 | static DWORD HTTPREQ_ReadFileExW(object_header_t *hdr, INTERNET_BUFFERSW *buffers, |
Hans Leidekker | b013ad1 | 2009-01-15 16:41:42 +0100 | [diff] [blame] | 2917 | DWORD flags, DWORD_PTR context) |
| 2918 | { |
| 2919 | |
Jacek Caban | 34abacd | 2009-07-13 01:41:50 +0200 | [diff] [blame] | 2920 | http_request_t *req = (http_request_t*)hdr; |
Piotr Caban | 21ced8d | 2010-07-20 00:33:24 +0200 | [diff] [blame] | 2921 | DWORD res, size, read, error = ERROR_SUCCESS; |
Hans Leidekker | b013ad1 | 2009-01-15 16:41:42 +0100 | [diff] [blame] | 2922 | |
| 2923 | if (flags & ~(IRF_ASYNC|IRF_NO_WAIT)) |
| 2924 | FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT)); |
| 2925 | |
| 2926 | if (buffers->dwStructSize != sizeof(*buffers)) |
| 2927 | return ERROR_INVALID_PARAMETER; |
| 2928 | |
| 2929 | INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0); |
| 2930 | |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 2931 | if (hdr->dwFlags & INTERNET_FLAG_ASYNC) |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2932 | { |
| 2933 | WORKREQUEST workRequest; |
Hans Leidekker | b013ad1 | 2009-01-15 16:41:42 +0100 | [diff] [blame] | 2934 | |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 2935 | if (TryEnterCriticalSection( &req->read_section )) |
| 2936 | { |
| 2937 | if (get_avail_data(req)) |
| 2938 | { |
| 2939 | res = HTTPREQ_Read(req, buffers->lpvBuffer, buffers->dwBufferLength, |
| 2940 | &buffers->dwBufferLength, FALSE); |
Piotr Caban | 21ced8d | 2010-07-20 00:33:24 +0200 | [diff] [blame] | 2941 | size = buffers->dwBufferLength; |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 2942 | LeaveCriticalSection( &req->read_section ); |
| 2943 | goto done; |
| 2944 | } |
| 2945 | LeaveCriticalSection( &req->read_section ); |
| 2946 | } |
| 2947 | |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2948 | workRequest.asyncproc = HTTPREQ_AsyncReadFileExWProc; |
| 2949 | workRequest.hdr = WININET_AddRef(&req->hdr); |
| 2950 | workRequest.u.InternetReadFileExW.lpBuffersOut = buffers; |
Hans Leidekker | b013ad1 | 2009-01-15 16:41:42 +0100 | [diff] [blame] | 2951 | |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2952 | INTERNET_AsyncCall(&workRequest); |
Hans Leidekker | b013ad1 | 2009-01-15 16:41:42 +0100 | [diff] [blame] | 2953 | |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 2954 | return ERROR_IO_PENDING; |
Hans Leidekker | b013ad1 | 2009-01-15 16:41:42 +0100 | [diff] [blame] | 2955 | } |
| 2956 | |
Piotr Caban | 21ced8d | 2010-07-20 00:33:24 +0200 | [diff] [blame] | 2957 | read = 0; |
| 2958 | size = buffers->dwBufferLength; |
| 2959 | |
| 2960 | EnterCriticalSection( &req->read_section ); |
| 2961 | if(hdr->dwError == ERROR_SUCCESS) |
| 2962 | hdr->dwError = INTERNET_HANDLE_IN_USE; |
| 2963 | else if(hdr->dwError == INTERNET_HANDLE_IN_USE) |
| 2964 | hdr->dwError = ERROR_INTERNET_INTERNAL_ERROR; |
| 2965 | |
| 2966 | while(1) { |
| 2967 | res = HTTPREQ_Read(req, (char*)buffers->lpvBuffer+read, size-read, |
| 2968 | &buffers->dwBufferLength, !(flags & IRF_NO_WAIT)); |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2969 | if(res != ERROR_SUCCESS) |
Piotr Caban | 21ced8d | 2010-07-20 00:33:24 +0200 | [diff] [blame] | 2970 | break; |
| 2971 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2972 | read += buffers->dwBufferLength; |
| 2973 | if(read == size || end_of_read_data(req)) |
Piotr Caban | 21ced8d | 2010-07-20 00:33:24 +0200 | [diff] [blame] | 2974 | break; |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 2975 | |
Piotr Caban | 21ced8d | 2010-07-20 00:33:24 +0200 | [diff] [blame] | 2976 | LeaveCriticalSection( &req->read_section ); |
| 2977 | |
| 2978 | INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED, |
| 2979 | &buffers->dwBufferLength, sizeof(buffers->dwBufferLength)); |
| 2980 | INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, |
| 2981 | INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0); |
| 2982 | |
| 2983 | EnterCriticalSection( &req->read_section ); |
| 2984 | } |
| 2985 | |
| 2986 | if(hdr->dwError == INTERNET_HANDLE_IN_USE) |
| 2987 | hdr->dwError = ERROR_SUCCESS; |
| 2988 | else |
| 2989 | error = hdr->dwError; |
| 2990 | |
| 2991 | LeaveCriticalSection( &req->read_section ); |
| 2992 | size = buffers->dwBufferLength; |
| 2993 | buffers->dwBufferLength = read; |
Hans Leidekker | b013ad1 | 2009-01-15 16:41:42 +0100 | [diff] [blame] | 2994 | |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 2995 | done: |
Hans Leidekker | b013ad1 | 2009-01-15 16:41:42 +0100 | [diff] [blame] | 2996 | if (res == ERROR_SUCCESS) { |
Hans Leidekker | b013ad1 | 2009-01-15 16:41:42 +0100 | [diff] [blame] | 2997 | INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED, |
| 2998 | &size, sizeof(size)); |
| 2999 | } |
| 3000 | |
Piotr Caban | 21ced8d | 2010-07-20 00:33:24 +0200 | [diff] [blame] | 3001 | return res==ERROR_SUCCESS ? error : res; |
Hans Leidekker | b013ad1 | 2009-01-15 16:41:42 +0100 | [diff] [blame] | 3002 | } |
| 3003 | |
Jacek Caban | 1ee3ad4 | 2009-11-30 00:13:39 +0100 | [diff] [blame] | 3004 | static DWORD HTTPREQ_WriteFile(object_header_t *hdr, const void *buffer, DWORD size, DWORD *written) |
Jacek Caban | 1ffcfbc | 2008-02-26 20:21:34 +0100 | [diff] [blame] | 3005 | { |
Jacek Caban | b77868c | 2009-11-30 00:13:21 +0100 | [diff] [blame] | 3006 | DWORD res; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3007 | http_request_t *request = (http_request_t*)hdr; |
Jacek Caban | 1ffcfbc | 2008-02-26 20:21:34 +0100 | [diff] [blame] | 3008 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3009 | INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0); |
Hans Leidekker | 85eb438 | 2009-04-08 15:22:05 +0200 | [diff] [blame] | 3010 | |
Hans Leidekker | 0fabf54 | 2009-04-08 15:21:28 +0200 | [diff] [blame] | 3011 | *written = 0; |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 3012 | res = NETCON_send(request->netconn, buffer, size, 0, (LPINT)written); |
Jacek Caban | b77868c | 2009-11-30 00:13:21 +0100 | [diff] [blame] | 3013 | if (res == ERROR_SUCCESS) |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3014 | request->bytesWritten += *written; |
Hans Leidekker | 0fabf54 | 2009-04-08 15:21:28 +0200 | [diff] [blame] | 3015 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3016 | INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REQUEST_SENT, written, sizeof(DWORD)); |
Jacek Caban | 1ee3ad4 | 2009-11-30 00:13:39 +0100 | [diff] [blame] | 3017 | return res; |
Jacek Caban | 1ffcfbc | 2008-02-26 20:21:34 +0100 | [diff] [blame] | 3018 | } |
| 3019 | |
Jacek Caban | 3314184 | 2008-02-29 12:57:57 +0100 | [diff] [blame] | 3020 | static void HTTPREQ_AsyncQueryDataAvailableProc(WORKREQUEST *workRequest) |
| 3021 | { |
Jacek Caban | 34abacd | 2009-07-13 01:41:50 +0200 | [diff] [blame] | 3022 | http_request_t *req = (http_request_t*)workRequest->hdr; |
Jacek Caban | 3314184 | 2008-02-29 12:57:57 +0100 | [diff] [blame] | 3023 | |
Jacek Caban | e13781a | 2009-01-22 13:48:23 +0100 | [diff] [blame] | 3024 | HTTP_ReceiveRequestData(req, FALSE); |
Jacek Caban | 3314184 | 2008-02-29 12:57:57 +0100 | [diff] [blame] | 3025 | } |
| 3026 | |
Jacek Caban | 44d633a | 2009-07-07 21:46:09 +0200 | [diff] [blame] | 3027 | static DWORD HTTPREQ_QueryDataAvailable(object_header_t *hdr, DWORD *available, DWORD flags, DWORD_PTR ctx) |
Jacek Caban | 3314184 | 2008-02-29 12:57:57 +0100 | [diff] [blame] | 3028 | { |
Jacek Caban | 34abacd | 2009-07-13 01:41:50 +0200 | [diff] [blame] | 3029 | http_request_t *req = (http_request_t*)hdr; |
Jacek Caban | 3314184 | 2008-02-29 12:57:57 +0100 | [diff] [blame] | 3030 | |
| 3031 | TRACE("(%p %p %x %lx)\n", req, available, flags, ctx); |
| 3032 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3033 | if (req->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC) |
Jacek Caban | 3314184 | 2008-02-29 12:57:57 +0100 | [diff] [blame] | 3034 | { |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 3035 | WORKREQUEST workRequest; |
Jacek Caban | 3314184 | 2008-02-29 12:57:57 +0100 | [diff] [blame] | 3036 | |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 3037 | /* never wait, if we can't enter the section we queue an async request right away */ |
| 3038 | if (TryEnterCriticalSection( &req->read_section )) |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 3039 | { |
Jacek Caban | 193da88 | 2011-05-23 16:10:09 +0200 | [diff] [blame] | 3040 | refill_read_buffer(req, READMODE_NOBLOCK, NULL); |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 3041 | if ((*available = get_avail_data( req ))) goto done; |
| 3042 | if (end_of_read_data( req )) goto done; |
| 3043 | LeaveCriticalSection( &req->read_section ); |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 3044 | } |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 3045 | |
| 3046 | workRequest.asyncproc = HTTPREQ_AsyncQueryDataAvailableProc; |
| 3047 | workRequest.hdr = WININET_AddRef( &req->hdr ); |
| 3048 | |
| 3049 | INTERNET_AsyncCall(&workRequest); |
| 3050 | |
| 3051 | return ERROR_IO_PENDING; |
Jacek Caban | 3314184 | 2008-02-29 12:57:57 +0100 | [diff] [blame] | 3052 | } |
| 3053 | |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 3054 | EnterCriticalSection( &req->read_section ); |
| 3055 | |
| 3056 | if (!(*available = get_avail_data( req )) && !end_of_read_data( req )) |
| 3057 | { |
Jacek Caban | 193da88 | 2011-05-23 16:10:09 +0200 | [diff] [blame] | 3058 | refill_read_buffer( req, READMODE_ASYNC, NULL ); |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 3059 | *available = get_avail_data( req ); |
| 3060 | } |
| 3061 | |
| 3062 | done: |
Alexandre Julliard | 5c227a9 | 2009-05-28 23:01:28 +0200 | [diff] [blame] | 3063 | LeaveCriticalSection( &req->read_section ); |
Alexandre Julliard | 3d02c42 | 2009-05-14 16:45:38 +0200 | [diff] [blame] | 3064 | |
| 3065 | TRACE( "returning %u\n", *available ); |
Jacek Caban | 3314184 | 2008-02-29 12:57:57 +0100 | [diff] [blame] | 3066 | return ERROR_SUCCESS; |
| 3067 | } |
| 3068 | |
Jacek Caban | 44d633a | 2009-07-07 21:46:09 +0200 | [diff] [blame] | 3069 | static const object_vtbl_t HTTPREQVtbl = { |
Jacek Caban | 7dc9bf6 | 2008-02-26 20:21:00 +0100 | [diff] [blame] | 3070 | HTTPREQ_Destroy, |
Jacek Caban | 1ffcfbc | 2008-02-26 20:21:34 +0100 | [diff] [blame] | 3071 | HTTPREQ_CloseConnection, |
Jacek Caban | e2933c2 | 2008-03-12 02:23:20 +0100 | [diff] [blame] | 3072 | HTTPREQ_QueryOption, |
Jacek Caban | 0e33eee | 2008-02-26 20:22:02 +0100 | [diff] [blame] | 3073 | HTTPREQ_SetOption, |
Jacek Caban | 3b4ca69 | 2008-03-02 19:35:11 +0100 | [diff] [blame] | 3074 | HTTPREQ_ReadFile, |
Jacek Caban | d597fd1 | 2008-03-03 18:07:20 +0100 | [diff] [blame] | 3075 | HTTPREQ_ReadFileExA, |
Hans Leidekker | b013ad1 | 2009-01-15 16:41:42 +0100 | [diff] [blame] | 3076 | HTTPREQ_ReadFileExW, |
Jacek Caban | 8c45eec | 2008-02-27 18:55:09 +0100 | [diff] [blame] | 3077 | HTTPREQ_WriteFile, |
Jacek Caban | 3314184 | 2008-02-29 12:57:57 +0100 | [diff] [blame] | 3078 | HTTPREQ_QueryDataAvailable, |
Jacek Caban | 8c45eec | 2008-02-27 18:55:09 +0100 | [diff] [blame] | 3079 | NULL |
Jacek Caban | 5a535d6 | 2008-02-26 20:20:41 +0100 | [diff] [blame] | 3080 | }; |
| 3081 | |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 3082 | /*********************************************************************** |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 3083 | * HTTP_HttpOpenRequestW (internal) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 3084 | * |
| 3085 | * Open a HTTP request handle |
| 3086 | * |
| 3087 | * RETURNS |
| 3088 | * HINTERNET a HTTP request handle on success |
| 3089 | * NULL on failure |
| 3090 | * |
| 3091 | */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3092 | static DWORD HTTP_HttpOpenRequestW(http_session_t *session, |
Jacek Caban | 85a057e | 2009-11-30 20:00:44 +0100 | [diff] [blame] | 3093 | LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion, |
| 3094 | LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes, |
| 3095 | DWORD dwFlags, DWORD_PTR dwContext, HINTERNET *ret) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 3096 | { |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 3097 | appinfo_t *hIC = session->appInfo; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3098 | http_request_t *request; |
Jacek Caban | 3aeb8eb | 2012-05-25 16:34:24 +0200 | [diff] [blame] | 3099 | INTERNET_PORT port; |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 3100 | DWORD len, res = ERROR_SUCCESS; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 3101 | |
Francois Gouget | 0edbaf7 | 2005-11-10 12:14:56 +0000 | [diff] [blame] | 3102 | TRACE("-->\n"); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 3103 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3104 | request = alloc_object(&session->hdr, &HTTPREQVtbl, sizeof(http_request_t)); |
| 3105 | if(!request) |
Jacek Caban | a073c66 | 2011-02-02 22:51:13 +0100 | [diff] [blame] | 3106 | return ERROR_OUTOFMEMORY; |
| 3107 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3108 | request->hdr.htype = WH_HHTTPREQ; |
| 3109 | request->hdr.dwFlags = dwFlags; |
| 3110 | request->hdr.dwContext = dwContext; |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3111 | request->contentLength = ~0u; |
Jacek Caban | a073c66 | 2011-02-02 22:51:13 +0100 | [diff] [blame] | 3112 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 3113 | request->netconn_stream.data_stream.vtbl = &netconn_stream_vtbl; |
| 3114 | request->data_stream = &request->netconn_stream.data_stream; |
Hans Leidekker | 72273a0 | 2012-01-13 15:15:04 +0100 | [diff] [blame] | 3115 | request->connect_timeout = session->connect_timeout; |
Hans Leidekker | 6522393 | 2012-01-13 15:15:48 +0100 | [diff] [blame] | 3116 | request->send_timeout = session->send_timeout; |
| 3117 | request->receive_timeout = session->receive_timeout; |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 3118 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3119 | InitializeCriticalSection( &request->read_section ); |
Francois Gouget | 17929b9 | 2011-11-16 16:29:44 +0100 | [diff] [blame] | 3120 | request->read_section.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": http_request_t.read_section"); |
Mike McCormack | 3a1391b | 2004-07-19 21:49:39 +0000 | [diff] [blame] | 3121 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3122 | WININET_AddRef( &session->hdr ); |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3123 | request->session = session; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3124 | list_add_head( &session->hdr.children, &request->hdr.entry ); |
Jacek Caban | a9bdc01 | 2006-10-29 18:50:25 +0100 | [diff] [blame] | 3125 | |
Jacek Caban | 2ed97eb | 2012-05-28 13:55:13 +0200 | [diff] [blame] | 3126 | port = session->hostPort; |
Jacek Caban | 3aeb8eb | 2012-05-25 16:34:24 +0200 | [diff] [blame] | 3127 | if(port == INTERNET_INVALID_PORT_NUMBER) |
| 3128 | port = dwFlags & INTERNET_FLAG_SECURE ? INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT; |
| 3129 | |
Jacek Caban | 6c764fb | 2012-06-11 10:22:38 +0200 | [diff] [blame] | 3130 | request->server = get_server(session->hostName, port, TRUE); |
Jacek Caban | 3aeb8eb | 2012-05-25 16:34:24 +0200 | [diff] [blame] | 3131 | if(!request->server) { |
| 3132 | WININET_Release(&request->hdr); |
| 3133 | return ERROR_OUTOFMEMORY; |
| 3134 | } |
| 3135 | |
Juan Lang | 0b8bfd9 | 2011-04-07 08:05:36 -0700 | [diff] [blame] | 3136 | if (dwFlags & INTERNET_FLAG_IGNORE_CERT_CN_INVALID) |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 3137 | request->security_flags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID; |
Juan Lang | 0b8bfd9 | 2011-04-07 08:05:36 -0700 | [diff] [blame] | 3138 | if (dwFlags & INTERNET_FLAG_IGNORE_CERT_DATE_INVALID) |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 3139 | request->security_flags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 3140 | |
Jacek Caban | f979134 | 2008-02-13 13:34:05 +0100 | [diff] [blame] | 3141 | if (lpszObjectName && *lpszObjectName) { |
Aric Stewart | ff9b9d4 | 2002-06-21 23:59:49 +0000 | [diff] [blame] | 3142 | HRESULT rc; |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 3143 | |
| 3144 | len = 0; |
| 3145 | rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY); |
Aric Stewart | ff9b9d4 | 2002-06-21 23:59:49 +0000 | [diff] [blame] | 3146 | if (rc != E_POINTER) |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 3147 | len = strlenW(lpszObjectName)+1; |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 3148 | request->path = heap_alloc(len*sizeof(WCHAR)); |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3149 | rc = UrlEscapeW(lpszObjectName, request->path, &len, |
Huw D M Davies | 0aebee9 | 2001-01-21 21:09:00 +0000 | [diff] [blame] | 3150 | URL_ESCAPE_SPACES_ONLY); |
Rob Shearman | d31ce9a | 2008-10-01 11:20:49 +0100 | [diff] [blame] | 3151 | if (rc != S_OK) |
Aric Stewart | ff9b9d4 | 2002-06-21 23:59:49 +0000 | [diff] [blame] | 3152 | { |
Hans Leidekker | cd2c458 | 2006-10-05 13:18:56 +0200 | [diff] [blame] | 3153 | ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc); |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3154 | strcpyW(request->path,lpszObjectName); |
Aric Stewart | ff9b9d4 | 2002-06-21 23:59:49 +0000 | [diff] [blame] | 3155 | } |
Jacek Caban | d1d1da3 | 2009-05-29 23:34:25 +0200 | [diff] [blame] | 3156 | }else { |
| 3157 | static const WCHAR slashW[] = {'/',0}; |
| 3158 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3159 | request->path = heap_strdupW(slashW); |
Huw D M Davies | 0aebee9 | 2001-01-21 21:09:00 +0000 | [diff] [blame] | 3160 | } |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 3161 | |
Jacek Caban | f979134 | 2008-02-13 13:34:05 +0100 | [diff] [blame] | 3162 | if (lpszReferrer && *lpszReferrer) |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3163 | HTTP_ProcessHeader(request, HTTP_REFERER, lpszReferrer, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ); |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 3164 | |
Hans Leidekker | 2024f68 | 2007-02-12 15:19:17 +0100 | [diff] [blame] | 3165 | if (lpszAcceptTypes) |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 3166 | { |
| 3167 | int i; |
Hans Leidekker | 2024f68 | 2007-02-12 15:19:17 +0100 | [diff] [blame] | 3168 | for (i = 0; lpszAcceptTypes[i]; i++) |
| 3169 | { |
| 3170 | if (!*lpszAcceptTypes[i]) continue; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3171 | HTTP_ProcessHeader(request, HTTP_ACCEPT, lpszAcceptTypes[i], |
Hans Leidekker | 2024f68 | 2007-02-12 15:19:17 +0100 | [diff] [blame] | 3172 | HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA | |
| 3173 | HTTP_ADDHDR_FLAG_REQ | |
| 3174 | (i == 0 ? HTTP_ADDHDR_FLAG_REPLACE : 0)); |
| 3175 | } |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 3176 | } |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 3177 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3178 | request->verb = heap_strdupW(lpszVerb && *lpszVerb ? lpszVerb : szGET); |
| 3179 | request->version = heap_strdupW(lpszVersion ? lpszVersion : g_szHttp1_1); |
Hans Leidekker | d0033db | 2008-02-17 20:41:42 +0100 | [diff] [blame] | 3180 | |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 3181 | if (session->hostPort != INTERNET_INVALID_PORT_NUMBER && |
| 3182 | session->hostPort != INTERNET_DEFAULT_HTTP_PORT && |
| 3183 | session->hostPort != INTERNET_DEFAULT_HTTPS_PORT) |
Alexander Morozov | 4f51b6c | 2008-07-03 16:34:40 +0400 | [diff] [blame] | 3184 | { |
Jacek Caban | 59f2e83 | 2011-05-02 11:26:10 +0200 | [diff] [blame] | 3185 | WCHAR *host_name; |
| 3186 | |
| 3187 | static const WCHAR host_formatW[] = {'%','s',':','%','u',0}; |
| 3188 | |
| 3189 | host_name = heap_alloc((strlenW(session->hostName) + 7 /* length of ":65535" + 1 */) * sizeof(WCHAR)); |
| 3190 | if (!host_name) { |
| 3191 | res = ERROR_OUTOFMEMORY; |
| 3192 | goto lend; |
| 3193 | } |
| 3194 | |
| 3195 | sprintfW(host_name, host_formatW, session->hostName, session->hostPort); |
| 3196 | HTTP_ProcessHeader(request, hostW, host_name, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ); |
| 3197 | heap_free(host_name); |
Alexander Morozov | 4f51b6c | 2008-07-03 16:34:40 +0400 | [diff] [blame] | 3198 | } |
| 3199 | else |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 3200 | HTTP_ProcessHeader(request, hostW, session->hostName, |
Alexander Morozov | 4f51b6c | 2008-07-03 16:34:40 +0400 | [diff] [blame] | 3201 | HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ); |
Robert Shearman | 37f2cc8 | 2004-09-13 19:33:17 +0000 | [diff] [blame] | 3202 | |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 3203 | if (session->hostPort == INTERNET_INVALID_PORT_NUMBER) |
| 3204 | session->hostPort = (dwFlags & INTERNET_FLAG_SECURE ? |
Hans Leidekker | e4c59c2 | 2008-03-30 19:17:31 +0100 | [diff] [blame] | 3205 | INTERNET_DEFAULT_HTTPS_PORT : |
| 3206 | INTERNET_DEFAULT_HTTP_PORT); |
Mike McCormack | 403e58f | 2005-10-19 19:07:08 +0000 | [diff] [blame] | 3207 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 3208 | if (hIC->proxy && hIC->proxy[0]) |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3209 | HTTP_DealWithProxy( hIC, session, request ); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 3210 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3211 | INTERNET_SendCallback(&session->hdr, dwContext, |
| 3212 | INTERNET_STATUS_HANDLE_CREATED, &request->hdr.hInternet, |
Jacek Caban | a073c66 | 2011-02-02 22:51:13 +0100 | [diff] [blame] | 3213 | sizeof(HINTERNET)); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 3214 | |
Mike McCormack | 3a1391b | 2004-07-19 21:49:39 +0000 | [diff] [blame] | 3215 | lend: |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3216 | TRACE("<-- %u (%p)\n", res, request); |
Mike McCormack | 3a1391b | 2004-07-19 21:49:39 +0000 | [diff] [blame] | 3217 | |
Jacek Caban | a073c66 | 2011-02-02 22:51:13 +0100 | [diff] [blame] | 3218 | if(res != ERROR_SUCCESS) { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3219 | WININET_Release( &request->hdr ); |
Jacek Caban | a073c66 | 2011-02-02 22:51:13 +0100 | [diff] [blame] | 3220 | *ret = NULL; |
| 3221 | return res; |
| 3222 | } |
| 3223 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3224 | *ret = request->hdr.hInternet; |
Jacek Caban | a073c66 | 2011-02-02 22:51:13 +0100 | [diff] [blame] | 3225 | return ERROR_SUCCESS; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 3226 | } |
| 3227 | |
Jacek Caban | 47c71fc | 2009-11-30 20:00:28 +0100 | [diff] [blame] | 3228 | /*********************************************************************** |
| 3229 | * HttpOpenRequestW (WININET.@) |
| 3230 | * |
| 3231 | * Open a HTTP request handle |
| 3232 | * |
| 3233 | * RETURNS |
| 3234 | * HINTERNET a HTTP request handle on success |
| 3235 | * NULL on failure |
| 3236 | * |
| 3237 | */ |
| 3238 | HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession, |
| 3239 | LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion, |
| 3240 | LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes, |
| 3241 | DWORD dwFlags, DWORD_PTR dwContext) |
| 3242 | { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3243 | http_session_t *session; |
Jacek Caban | 47c71fc | 2009-11-30 20:00:28 +0100 | [diff] [blame] | 3244 | HINTERNET handle = NULL; |
Jacek Caban | 85a057e | 2009-11-30 20:00:44 +0100 | [diff] [blame] | 3245 | DWORD res; |
Jacek Caban | 47c71fc | 2009-11-30 20:00:28 +0100 | [diff] [blame] | 3246 | |
| 3247 | TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession, |
| 3248 | debugstr_w(lpszVerb), debugstr_w(lpszObjectName), |
| 3249 | debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes, |
| 3250 | dwFlags, dwContext); |
| 3251 | if(lpszAcceptTypes!=NULL) |
| 3252 | { |
| 3253 | int i; |
| 3254 | for(i=0;lpszAcceptTypes[i]!=NULL;i++) |
| 3255 | TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i])); |
| 3256 | } |
| 3257 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3258 | session = (http_session_t*) get_handle_object( hHttpSession ); |
| 3259 | if (NULL == session || session->hdr.htype != WH_HHTTPSESSION) |
Jacek Caban | 47c71fc | 2009-11-30 20:00:28 +0100 | [diff] [blame] | 3260 | { |
Jacek Caban | 85a057e | 2009-11-30 20:00:44 +0100 | [diff] [blame] | 3261 | res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE; |
| 3262 | goto lend; |
Jacek Caban | 47c71fc | 2009-11-30 20:00:28 +0100 | [diff] [blame] | 3263 | } |
| 3264 | |
| 3265 | /* |
| 3266 | * My tests seem to show that the windows version does not |
| 3267 | * become asynchronous until after this point. And anyhow |
| 3268 | * if this call was asynchronous then how would you get the |
| 3269 | * necessary HINTERNET pointer returned by this function. |
| 3270 | * |
| 3271 | */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3272 | res = HTTP_HttpOpenRequestW(session, lpszVerb, lpszObjectName, |
Jacek Caban | 85a057e | 2009-11-30 20:00:44 +0100 | [diff] [blame] | 3273 | lpszVersion, lpszReferrer, lpszAcceptTypes, |
| 3274 | dwFlags, dwContext, &handle); |
Jacek Caban | 47c71fc | 2009-11-30 20:00:28 +0100 | [diff] [blame] | 3275 | lend: |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3276 | if( session ) |
| 3277 | WININET_Release( &session->hdr ); |
Jacek Caban | 47c71fc | 2009-11-30 20:00:28 +0100 | [diff] [blame] | 3278 | TRACE("returning %p\n", handle); |
Jacek Caban | 85a057e | 2009-11-30 20:00:44 +0100 | [diff] [blame] | 3279 | if(res != ERROR_SUCCESS) |
| 3280 | SetLastError(res); |
Jacek Caban | 47c71fc | 2009-11-30 20:00:28 +0100 | [diff] [blame] | 3281 | return handle; |
| 3282 | } |
| 3283 | |
Mike McCormack | 7f5e273 | 2006-03-30 18:02:54 +0900 | [diff] [blame] | 3284 | static const LPCWSTR header_lookup[] = { |
| 3285 | szMime_Version, /* HTTP_QUERY_MIME_VERSION = 0 */ |
| 3286 | szContent_Type, /* HTTP_QUERY_CONTENT_TYPE = 1 */ |
| 3287 | szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */ |
| 3288 | szContent_ID, /* HTTP_QUERY_CONTENT_ID = 3 */ |
| 3289 | NULL, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */ |
| 3290 | szContent_Length, /* HTTP_QUERY_CONTENT_LENGTH = 5 */ |
| 3291 | szContent_Language, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */ |
| 3292 | szAllow, /* HTTP_QUERY_ALLOW = 7 */ |
| 3293 | szPublic, /* HTTP_QUERY_PUBLIC = 8 */ |
| 3294 | szDate, /* HTTP_QUERY_DATE = 9 */ |
| 3295 | szExpires, /* HTTP_QUERY_EXPIRES = 10 */ |
| 3296 | szLast_Modified, /* HTTP_QUERY_LAST_MODIFIED = 11 */ |
| 3297 | NULL, /* HTTP_QUERY_MESSAGE_ID = 12 */ |
| 3298 | szURI, /* HTTP_QUERY_URI = 13 */ |
| 3299 | szFrom, /* HTTP_QUERY_DERIVED_FROM = 14 */ |
| 3300 | NULL, /* HTTP_QUERY_COST = 15 */ |
| 3301 | NULL, /* HTTP_QUERY_LINK = 16 */ |
| 3302 | szPragma, /* HTTP_QUERY_PRAGMA = 17 */ |
| 3303 | NULL, /* HTTP_QUERY_VERSION = 18 */ |
| 3304 | szStatus, /* HTTP_QUERY_STATUS_CODE = 19 */ |
| 3305 | NULL, /* HTTP_QUERY_STATUS_TEXT = 20 */ |
| 3306 | NULL, /* HTTP_QUERY_RAW_HEADERS = 21 */ |
| 3307 | NULL, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */ |
| 3308 | szConnection, /* HTTP_QUERY_CONNECTION = 23 */ |
| 3309 | szAccept, /* HTTP_QUERY_ACCEPT = 24 */ |
| 3310 | szAccept_Charset, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */ |
| 3311 | szAccept_Encoding, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */ |
| 3312 | szAccept_Language, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */ |
| 3313 | szAuthorization, /* HTTP_QUERY_AUTHORIZATION = 28 */ |
| 3314 | szContent_Encoding, /* HTTP_QUERY_CONTENT_ENCODING = 29 */ |
| 3315 | NULL, /* HTTP_QUERY_FORWARDED = 30 */ |
| 3316 | NULL, /* HTTP_QUERY_FROM = 31 */ |
| 3317 | szIf_Modified_Since, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */ |
| 3318 | szLocation, /* HTTP_QUERY_LOCATION = 33 */ |
| 3319 | NULL, /* HTTP_QUERY_ORIG_URI = 34 */ |
| 3320 | szReferer, /* HTTP_QUERY_REFERER = 35 */ |
| 3321 | szRetry_After, /* HTTP_QUERY_RETRY_AFTER = 36 */ |
| 3322 | szServer, /* HTTP_QUERY_SERVER = 37 */ |
| 3323 | NULL, /* HTTP_TITLE = 38 */ |
| 3324 | szUser_Agent, /* HTTP_QUERY_USER_AGENT = 39 */ |
| 3325 | szWWW_Authenticate, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */ |
| 3326 | szProxy_Authenticate, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */ |
| 3327 | szAccept_Ranges, /* HTTP_QUERY_ACCEPT_RANGES = 42 */ |
| 3328 | szSet_Cookie, /* HTTP_QUERY_SET_COOKIE = 43 */ |
| 3329 | szCookie, /* HTTP_QUERY_COOKIE = 44 */ |
| 3330 | NULL, /* HTTP_QUERY_REQUEST_METHOD = 45 */ |
| 3331 | NULL, /* HTTP_QUERY_REFRESH = 46 */ |
Alexander Morozov | ef95a79 | 2012-05-23 17:49:24 +0400 | [diff] [blame] | 3332 | szContent_Disposition, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */ |
Mike McCormack | 7f5e273 | 2006-03-30 18:02:54 +0900 | [diff] [blame] | 3333 | szAge, /* HTTP_QUERY_AGE = 48 */ |
| 3334 | szCache_Control, /* HTTP_QUERY_CACHE_CONTROL = 49 */ |
| 3335 | szContent_Base, /* HTTP_QUERY_CONTENT_BASE = 50 */ |
| 3336 | szContent_Location, /* HTTP_QUERY_CONTENT_LOCATION = 51 */ |
| 3337 | szContent_MD5, /* HTTP_QUERY_CONTENT_MD5 = 52 */ |
| 3338 | szContent_Range, /* HTTP_QUERY_CONTENT_RANGE = 53 */ |
| 3339 | szETag, /* HTTP_QUERY_ETAG = 54 */ |
Jacek Caban | 8317089 | 2009-05-29 23:34:14 +0200 | [diff] [blame] | 3340 | hostW, /* HTTP_QUERY_HOST = 55 */ |
Mike McCormack | 7f5e273 | 2006-03-30 18:02:54 +0900 | [diff] [blame] | 3341 | szIf_Match, /* HTTP_QUERY_IF_MATCH = 56 */ |
| 3342 | szIf_None_Match, /* HTTP_QUERY_IF_NONE_MATCH = 57 */ |
| 3343 | szIf_Range, /* HTTP_QUERY_IF_RANGE = 58 */ |
| 3344 | szIf_Unmodified_Since, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */ |
| 3345 | szMax_Forwards, /* HTTP_QUERY_MAX_FORWARDS = 60 */ |
| 3346 | szProxy_Authorization, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */ |
| 3347 | szRange, /* HTTP_QUERY_RANGE = 62 */ |
| 3348 | szTransfer_Encoding, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */ |
| 3349 | szUpgrade, /* HTTP_QUERY_UPGRADE = 64 */ |
| 3350 | szVary, /* HTTP_QUERY_VARY = 65 */ |
| 3351 | szVia, /* HTTP_QUERY_VIA = 66 */ |
| 3352 | szWarning, /* HTTP_QUERY_WARNING = 67 */ |
| 3353 | szExpect, /* HTTP_QUERY_EXPECT = 68 */ |
| 3354 | szProxy_Connection, /* HTTP_QUERY_PROXY_CONNECTION = 69 */ |
| 3355 | szUnless_Modified_Since, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */ |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 3356 | }; |
| 3357 | |
Mike McCormack | 7f5e273 | 2006-03-30 18:02:54 +0900 | [diff] [blame] | 3358 | #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0])) |
| 3359 | |
Alexandre Julliard | 48243e3 | 2004-07-15 18:57:32 +0000 | [diff] [blame] | 3360 | /*********************************************************************** |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 3361 | * HTTP_HttpQueryInfoW (internal) |
| 3362 | */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3363 | static DWORD HTTP_HttpQueryInfoW(http_request_t *request, DWORD dwInfoLevel, |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3364 | LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex) |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 3365 | { |
| 3366 | LPHTTPHEADERW lphttpHdr = NULL; |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 3367 | BOOL request_only = dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS; |
Mike McCormack | ae30088 | 2006-03-30 18:01:48 +0900 | [diff] [blame] | 3368 | INT requested_index = lpdwIndex ? *lpdwIndex : 0; |
Andrew Talbot | bc8d8e5 | 2008-12-08 16:51:08 +0000 | [diff] [blame] | 3369 | DWORD level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK); |
Mike McCormack | ae30088 | 2006-03-30 18:01:48 +0900 | [diff] [blame] | 3370 | INT index = -1; |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 3371 | |
| 3372 | /* Find requested header structure */ |
Mike McCormack | ae30088 | 2006-03-30 18:01:48 +0900 | [diff] [blame] | 3373 | switch (level) |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 3374 | { |
Mike McCormack | ae30088 | 2006-03-30 18:01:48 +0900 | [diff] [blame] | 3375 | case HTTP_QUERY_CUSTOM: |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3376 | if (!lpBuffer) return ERROR_INVALID_PARAMETER; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3377 | index = HTTP_GetCustomHeaderIndex(request, lpBuffer, requested_index, request_only); |
Mike McCormack | ae30088 | 2006-03-30 18:01:48 +0900 | [diff] [blame] | 3378 | break; |
Mike McCormack | ae30088 | 2006-03-30 18:01:48 +0900 | [diff] [blame] | 3379 | case HTTP_QUERY_RAW_HEADERS_CRLF: |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 3380 | { |
Mikołaj Zalewski | ab7d177 | 2007-08-15 16:29:58 -0700 | [diff] [blame] | 3381 | LPWSTR headers; |
Lei Zhang | bc9e214 | 2008-08-27 16:54:17 -0700 | [diff] [blame] | 3382 | DWORD len = 0; |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3383 | DWORD res = ERROR_INVALID_PARAMETER; |
Mikołaj Zalewski | ab7d177 | 2007-08-15 16:29:58 -0700 | [diff] [blame] | 3384 | |
| 3385 | if (request_only) |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3386 | headers = HTTP_BuildHeaderRequestString(request, request->verb, request->path, request->version); |
Mikołaj Zalewski | ab7d177 | 2007-08-15 16:29:58 -0700 | [diff] [blame] | 3387 | else |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3388 | headers = request->rawHeaders; |
Mikołaj Zalewski | ab7d177 | 2007-08-15 16:29:58 -0700 | [diff] [blame] | 3389 | |
Lei Zhang | bc9e214 | 2008-08-27 16:54:17 -0700 | [diff] [blame] | 3390 | if (headers) |
| 3391 | len = strlenW(headers) * sizeof(WCHAR); |
| 3392 | |
Dan Kegel | 1e7f891 | 2008-07-30 06:49:04 -0700 | [diff] [blame] | 3393 | if (len + sizeof(WCHAR) > *lpdwBufferLength) |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 3394 | { |
Dan Kegel | 1e7f891 | 2008-07-30 06:49:04 -0700 | [diff] [blame] | 3395 | len += sizeof(WCHAR); |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3396 | res = ERROR_INSUFFICIENT_BUFFER; |
Hans Leidekker | 694a092 | 2008-05-18 21:09:50 +0200 | [diff] [blame] | 3397 | } |
| 3398 | else if (lpBuffer) |
Mikołaj Zalewski | ab7d177 | 2007-08-15 16:29:58 -0700 | [diff] [blame] | 3399 | { |
Lei Zhang | bc9e214 | 2008-08-27 16:54:17 -0700 | [diff] [blame] | 3400 | if (headers) |
| 3401 | memcpy(lpBuffer, headers, len + sizeof(WCHAR)); |
| 3402 | else |
| 3403 | { |
Lei Zhang | f7e56d1 | 2008-08-27 17:03:13 -0700 | [diff] [blame] | 3404 | len = strlenW(szCrLf) * sizeof(WCHAR); |
| 3405 | memcpy(lpBuffer, szCrLf, sizeof(szCrLf)); |
Lei Zhang | bc9e214 | 2008-08-27 16:54:17 -0700 | [diff] [blame] | 3406 | } |
Hans Leidekker | 694a092 | 2008-05-18 21:09:50 +0200 | [diff] [blame] | 3407 | TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len / sizeof(WCHAR))); |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3408 | res = ERROR_SUCCESS; |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 3409 | } |
Hans Leidekker | 694a092 | 2008-05-18 21:09:50 +0200 | [diff] [blame] | 3410 | *lpdwBufferLength = len; |
Robert Shearman | 4385d30 | 2004-07-21 21:17:03 +0000 | [diff] [blame] | 3411 | |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 3412 | if (request_only) heap_free(headers); |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3413 | return res; |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 3414 | } |
Mike McCormack | ae30088 | 2006-03-30 18:01:48 +0900 | [diff] [blame] | 3415 | case HTTP_QUERY_RAW_HEADERS: |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 3416 | { |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3417 | LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(request->rawHeaders, szCrLf); |
Mike McCormack | 13b6ce6d | 2004-08-09 18:54:23 +0000 | [diff] [blame] | 3418 | DWORD i, size = 0; |
Hans Leidekker | 128b8a5 | 2008-10-06 15:48:28 +0200 | [diff] [blame] | 3419 | LPWSTR pszString = lpBuffer; |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 3420 | |
| 3421 | for (i = 0; ppszRawHeaderLines[i]; i++) |
| 3422 | size += strlenW(ppszRawHeaderLines[i]) + 1; |
| 3423 | |
| 3424 | if (size + 1 > *lpdwBufferLength/sizeof(WCHAR)) |
| 3425 | { |
| 3426 | HTTP_FreeTokens(ppszRawHeaderLines); |
| 3427 | *lpdwBufferLength = (size + 1) * sizeof(WCHAR); |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3428 | return ERROR_INSUFFICIENT_BUFFER; |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 3429 | } |
Hans Leidekker | 128b8a5 | 2008-10-06 15:48:28 +0200 | [diff] [blame] | 3430 | if (pszString) |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 3431 | { |
Hans Leidekker | 128b8a5 | 2008-10-06 15:48:28 +0200 | [diff] [blame] | 3432 | for (i = 0; ppszRawHeaderLines[i]; i++) |
| 3433 | { |
| 3434 | DWORD len = strlenW(ppszRawHeaderLines[i]); |
| 3435 | memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR)); |
| 3436 | pszString += len+1; |
| 3437 | } |
| 3438 | *pszString = '\0'; |
| 3439 | TRACE("returning data: %s\n", debugstr_wn(lpBuffer, size)); |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 3440 | } |
Robert Shearman | 4385d30 | 2004-07-21 21:17:03 +0000 | [diff] [blame] | 3441 | *lpdwBufferLength = size * sizeof(WCHAR); |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 3442 | HTTP_FreeTokens(ppszRawHeaderLines); |
| 3443 | |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3444 | return ERROR_SUCCESS; |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 3445 | } |
Mike McCormack | ae30088 | 2006-03-30 18:01:48 +0900 | [diff] [blame] | 3446 | case HTTP_QUERY_STATUS_TEXT: |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3447 | if (request->statusText) |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 3448 | { |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3449 | DWORD len = strlenW(request->statusText); |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 3450 | if (len + 1 > *lpdwBufferLength/sizeof(WCHAR)) |
| 3451 | { |
| 3452 | *lpdwBufferLength = (len + 1) * sizeof(WCHAR); |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3453 | return ERROR_INSUFFICIENT_BUFFER; |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 3454 | } |
Hans Leidekker | 128b8a5 | 2008-10-06 15:48:28 +0200 | [diff] [blame] | 3455 | if (lpBuffer) |
| 3456 | { |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3457 | memcpy(lpBuffer, request->statusText, (len + 1) * sizeof(WCHAR)); |
Hans Leidekker | 128b8a5 | 2008-10-06 15:48:28 +0200 | [diff] [blame] | 3458 | TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len)); |
| 3459 | } |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 3460 | *lpdwBufferLength = len * sizeof(WCHAR); |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3461 | return ERROR_SUCCESS; |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 3462 | } |
Mike McCormack | 739cd1e | 2006-05-30 22:55:42 +0900 | [diff] [blame] | 3463 | break; |
Mike McCormack | ae30088 | 2006-03-30 18:01:48 +0900 | [diff] [blame] | 3464 | case HTTP_QUERY_VERSION: |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3465 | if (request->version) |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 3466 | { |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3467 | DWORD len = strlenW(request->version); |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 3468 | if (len + 1 > *lpdwBufferLength/sizeof(WCHAR)) |
| 3469 | { |
| 3470 | *lpdwBufferLength = (len + 1) * sizeof(WCHAR); |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3471 | return ERROR_INSUFFICIENT_BUFFER; |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 3472 | } |
Hans Leidekker | 128b8a5 | 2008-10-06 15:48:28 +0200 | [diff] [blame] | 3473 | if (lpBuffer) |
| 3474 | { |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3475 | memcpy(lpBuffer, request->version, (len + 1) * sizeof(WCHAR)); |
Hans Leidekker | 128b8a5 | 2008-10-06 15:48:28 +0200 | [diff] [blame] | 3476 | TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len)); |
| 3477 | } |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 3478 | *lpdwBufferLength = len * sizeof(WCHAR); |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3479 | return ERROR_SUCCESS; |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 3480 | } |
Mike McCormack | 739cd1e | 2006-05-30 22:55:42 +0900 | [diff] [blame] | 3481 | break; |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 3482 | case HTTP_QUERY_CONTENT_ENCODING: |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 3483 | index = HTTP_GetCustomHeaderIndex(request, header_lookup[request->read_gzip ? HTTP_QUERY_CONTENT_TYPE : level], |
Jacek Caban | 11ca05f | 2009-05-29 23:35:13 +0200 | [diff] [blame] | 3484 | requested_index,request_only); |
| 3485 | break; |
Jacek Caban | 0d76489 | 2012-05-03 12:20:25 +0200 | [diff] [blame] | 3486 | case HTTP_QUERY_STATUS_CODE: { |
| 3487 | DWORD res = ERROR_SUCCESS; |
| 3488 | |
Jacek Caban | 07e5b87 | 2012-05-14 10:41:31 +0200 | [diff] [blame] | 3489 | if(request_only) |
| 3490 | return ERROR_HTTP_INVALID_QUERY_REQUEST; |
| 3491 | |
| 3492 | if(requested_index) |
Jacek Caban | 0d76489 | 2012-05-03 12:20:25 +0200 | [diff] [blame] | 3493 | break; |
| 3494 | |
| 3495 | if(dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) { |
| 3496 | if(*lpdwBufferLength >= sizeof(DWORD)) |
| 3497 | *(DWORD*)lpBuffer = request->status_code; |
| 3498 | else |
| 3499 | res = ERROR_INSUFFICIENT_BUFFER; |
| 3500 | *lpdwBufferLength = sizeof(DWORD); |
| 3501 | }else { |
| 3502 | WCHAR buf[12]; |
| 3503 | DWORD size; |
| 3504 | static const WCHAR formatW[] = {'%','u',0}; |
| 3505 | |
Jacek Caban | 120c413 | 2012-06-15 18:15:20 +0200 | [diff] [blame] | 3506 | size = sprintfW(buf, formatW, request->status_code) * sizeof(WCHAR); |
Jacek Caban | 0d76489 | 2012-05-03 12:20:25 +0200 | [diff] [blame] | 3507 | |
Jacek Caban | 120c413 | 2012-06-15 18:15:20 +0200 | [diff] [blame] | 3508 | if(size <= *lpdwBufferLength) { |
| 3509 | memcpy(lpBuffer, buf, size+sizeof(WCHAR)); |
| 3510 | }else { |
| 3511 | size += sizeof(WCHAR); |
Jacek Caban | 0d76489 | 2012-05-03 12:20:25 +0200 | [diff] [blame] | 3512 | res = ERROR_INSUFFICIENT_BUFFER; |
Jacek Caban | 120c413 | 2012-06-15 18:15:20 +0200 | [diff] [blame] | 3513 | } |
Jacek Caban | 0d76489 | 2012-05-03 12:20:25 +0200 | [diff] [blame] | 3514 | |
| 3515 | *lpdwBufferLength = size; |
| 3516 | } |
| 3517 | return res; |
| 3518 | } |
Mike McCormack | ae30088 | 2006-03-30 18:01:48 +0900 | [diff] [blame] | 3519 | default: |
Mike McCormack | 7f5e273 | 2006-03-30 18:02:54 +0900 | [diff] [blame] | 3520 | assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1)); |
| 3521 | |
Andrew Talbot | bc8d8e5 | 2008-12-08 16:51:08 +0000 | [diff] [blame] | 3522 | if (level < LAST_TABLE_HEADER && header_lookup[level]) |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3523 | index = HTTP_GetCustomHeaderIndex(request, header_lookup[level], |
Mike McCormack | 7f5e273 | 2006-03-30 18:02:54 +0900 | [diff] [blame] | 3524 | requested_index,request_only); |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 3525 | } |
| 3526 | |
Mike McCormack | ae30088 | 2006-03-30 18:01:48 +0900 | [diff] [blame] | 3527 | if (index >= 0) |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3528 | lphttpHdr = &request->custHeaders[index]; |
Mike McCormack | ae30088 | 2006-03-30 18:01:48 +0900 | [diff] [blame] | 3529 | |
Austin English | 0e4adae | 2008-01-04 13:37:14 -0600 | [diff] [blame] | 3530 | /* Ensure header satisfies requested attributes */ |
Mike McCormack | ae30088 | 2006-03-30 18:01:48 +0900 | [diff] [blame] | 3531 | if (!lphttpHdr || |
| 3532 | ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) && |
| 3533 | (~lphttpHdr->wFlags & HDR_ISREQUEST))) |
Alexandre Julliard | 48243e3 | 2004-07-15 18:57:32 +0000 | [diff] [blame] | 3534 | { |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3535 | return ERROR_HTTP_HEADER_NOT_FOUND; |
Alexandre Julliard | 48243e3 | 2004-07-15 18:57:32 +0000 | [diff] [blame] | 3536 | } |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 3537 | |
Jacek Caban | 0d76489 | 2012-05-03 12:20:25 +0200 | [diff] [blame] | 3538 | if (lpdwIndex) (*lpdwIndex)++; |
Mike McCormack | ae30088 | 2006-03-30 18:01:48 +0900 | [diff] [blame] | 3539 | |
Austin English | 0e4adae | 2008-01-04 13:37:14 -0600 | [diff] [blame] | 3540 | /* coalesce value to requested type */ |
Hans Leidekker | 128b8a5 | 2008-10-06 15:48:28 +0200 | [diff] [blame] | 3541 | if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER && lpBuffer) |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 3542 | { |
Hans Leidekker | 128b8a5 | 2008-10-06 15:48:28 +0200 | [diff] [blame] | 3543 | *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue); |
| 3544 | TRACE(" returning number: %d\n", *(int *)lpBuffer); |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3545 | } |
Hans Leidekker | 128b8a5 | 2008-10-06 15:48:28 +0200 | [diff] [blame] | 3546 | else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME && lpBuffer) |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 3547 | { |
| 3548 | time_t tmpTime; |
| 3549 | struct tm tmpTM; |
| 3550 | SYSTEMTIME *STHook; |
| 3551 | |
| 3552 | tmpTime = ConvertTimeString(lphttpHdr->lpszValue); |
| 3553 | |
| 3554 | tmpTM = *gmtime(&tmpTime); |
Hans Leidekker | 128b8a5 | 2008-10-06 15:48:28 +0200 | [diff] [blame] | 3555 | STHook = (SYSTEMTIME *)lpBuffer; |
Hans Leidekker | 128b8a5 | 2008-10-06 15:48:28 +0200 | [diff] [blame] | 3556 | STHook->wDay = tmpTM.tm_mday; |
| 3557 | STHook->wHour = tmpTM.tm_hour; |
| 3558 | STHook->wMilliseconds = 0; |
| 3559 | STHook->wMinute = tmpTM.tm_min; |
| 3560 | STHook->wDayOfWeek = tmpTM.tm_wday; |
| 3561 | STHook->wMonth = tmpTM.tm_mon + 1; |
| 3562 | STHook->wSecond = tmpTM.tm_sec; |
| 3563 | STHook->wYear = tmpTM.tm_year; |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3564 | |
Hans Leidekker | 128b8a5 | 2008-10-06 15:48:28 +0200 | [diff] [blame] | 3565 | TRACE(" returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n", |
| 3566 | STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek, |
| 3567 | STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds); |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 3568 | } |
Aric Stewart | 7bca41a | 2005-12-08 12:44:45 +0100 | [diff] [blame] | 3569 | else if (lphttpHdr->lpszValue) |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 3570 | { |
Mike McCormack | 13b6ce6d | 2004-08-09 18:54:23 +0000 | [diff] [blame] | 3571 | DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR); |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 3572 | |
| 3573 | if (len > *lpdwBufferLength) |
| 3574 | { |
| 3575 | *lpdwBufferLength = len; |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3576 | return ERROR_INSUFFICIENT_BUFFER; |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 3577 | } |
Hans Leidekker | 128b8a5 | 2008-10-06 15:48:28 +0200 | [diff] [blame] | 3578 | if (lpBuffer) |
| 3579 | { |
| 3580 | memcpy(lpBuffer, lphttpHdr->lpszValue, len); |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3581 | TRACE("! returning string: %s\n", debugstr_w(lpBuffer)); |
Hans Leidekker | 128b8a5 | 2008-10-06 15:48:28 +0200 | [diff] [blame] | 3582 | } |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 3583 | *lpdwBufferLength = len - sizeof(WCHAR); |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 3584 | } |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3585 | return ERROR_SUCCESS; |
Mike McCormack | b288f71 | 2004-06-14 17:57:26 +0000 | [diff] [blame] | 3586 | } |
| 3587 | |
| 3588 | /*********************************************************************** |
Mike McCormack | 1baf39f | 2004-03-30 20:37:49 +0000 | [diff] [blame] | 3589 | * HttpQueryInfoW (WININET.@) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 3590 | * |
| 3591 | * Queries for information about an HTTP request |
| 3592 | * |
| 3593 | * RETURNS |
| 3594 | * TRUE on success |
| 3595 | * FALSE on failure |
| 3596 | * |
| 3597 | */ |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 3598 | BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel, |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3599 | LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 3600 | { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3601 | http_request_t *request; |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3602 | DWORD res; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 3603 | |
Lionel Ulmer | 1d5e6b6 | 2004-02-09 22:01:49 +0000 | [diff] [blame] | 3604 | if (TRACE_ON(wininet)) { |
| 3605 | #define FE(x) { x, #x } |
| 3606 | static const wininet_flag_info query_flags[] = { |
| 3607 | FE(HTTP_QUERY_MIME_VERSION), |
| 3608 | FE(HTTP_QUERY_CONTENT_TYPE), |
| 3609 | FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING), |
| 3610 | FE(HTTP_QUERY_CONTENT_ID), |
| 3611 | FE(HTTP_QUERY_CONTENT_DESCRIPTION), |
| 3612 | FE(HTTP_QUERY_CONTENT_LENGTH), |
| 3613 | FE(HTTP_QUERY_CONTENT_LANGUAGE), |
| 3614 | FE(HTTP_QUERY_ALLOW), |
| 3615 | FE(HTTP_QUERY_PUBLIC), |
| 3616 | FE(HTTP_QUERY_DATE), |
| 3617 | FE(HTTP_QUERY_EXPIRES), |
| 3618 | FE(HTTP_QUERY_LAST_MODIFIED), |
| 3619 | FE(HTTP_QUERY_MESSAGE_ID), |
| 3620 | FE(HTTP_QUERY_URI), |
| 3621 | FE(HTTP_QUERY_DERIVED_FROM), |
| 3622 | FE(HTTP_QUERY_COST), |
| 3623 | FE(HTTP_QUERY_LINK), |
| 3624 | FE(HTTP_QUERY_PRAGMA), |
| 3625 | FE(HTTP_QUERY_VERSION), |
| 3626 | FE(HTTP_QUERY_STATUS_CODE), |
| 3627 | FE(HTTP_QUERY_STATUS_TEXT), |
| 3628 | FE(HTTP_QUERY_RAW_HEADERS), |
| 3629 | FE(HTTP_QUERY_RAW_HEADERS_CRLF), |
| 3630 | FE(HTTP_QUERY_CONNECTION), |
| 3631 | FE(HTTP_QUERY_ACCEPT), |
| 3632 | FE(HTTP_QUERY_ACCEPT_CHARSET), |
| 3633 | FE(HTTP_QUERY_ACCEPT_ENCODING), |
| 3634 | FE(HTTP_QUERY_ACCEPT_LANGUAGE), |
| 3635 | FE(HTTP_QUERY_AUTHORIZATION), |
| 3636 | FE(HTTP_QUERY_CONTENT_ENCODING), |
| 3637 | FE(HTTP_QUERY_FORWARDED), |
| 3638 | FE(HTTP_QUERY_FROM), |
| 3639 | FE(HTTP_QUERY_IF_MODIFIED_SINCE), |
| 3640 | FE(HTTP_QUERY_LOCATION), |
| 3641 | FE(HTTP_QUERY_ORIG_URI), |
| 3642 | FE(HTTP_QUERY_REFERER), |
| 3643 | FE(HTTP_QUERY_RETRY_AFTER), |
| 3644 | FE(HTTP_QUERY_SERVER), |
| 3645 | FE(HTTP_QUERY_TITLE), |
| 3646 | FE(HTTP_QUERY_USER_AGENT), |
| 3647 | FE(HTTP_QUERY_WWW_AUTHENTICATE), |
| 3648 | FE(HTTP_QUERY_PROXY_AUTHENTICATE), |
| 3649 | FE(HTTP_QUERY_ACCEPT_RANGES), |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 3650 | FE(HTTP_QUERY_SET_COOKIE), |
| 3651 | FE(HTTP_QUERY_COOKIE), |
Lionel Ulmer | 1d5e6b6 | 2004-02-09 22:01:49 +0000 | [diff] [blame] | 3652 | FE(HTTP_QUERY_REQUEST_METHOD), |
| 3653 | FE(HTTP_QUERY_REFRESH), |
| 3654 | FE(HTTP_QUERY_CONTENT_DISPOSITION), |
| 3655 | FE(HTTP_QUERY_AGE), |
| 3656 | FE(HTTP_QUERY_CACHE_CONTROL), |
| 3657 | FE(HTTP_QUERY_CONTENT_BASE), |
| 3658 | FE(HTTP_QUERY_CONTENT_LOCATION), |
| 3659 | FE(HTTP_QUERY_CONTENT_MD5), |
| 3660 | FE(HTTP_QUERY_CONTENT_RANGE), |
| 3661 | FE(HTTP_QUERY_ETAG), |
| 3662 | FE(HTTP_QUERY_HOST), |
| 3663 | FE(HTTP_QUERY_IF_MATCH), |
| 3664 | FE(HTTP_QUERY_IF_NONE_MATCH), |
| 3665 | FE(HTTP_QUERY_IF_RANGE), |
| 3666 | FE(HTTP_QUERY_IF_UNMODIFIED_SINCE), |
| 3667 | FE(HTTP_QUERY_MAX_FORWARDS), |
| 3668 | FE(HTTP_QUERY_PROXY_AUTHORIZATION), |
| 3669 | FE(HTTP_QUERY_RANGE), |
| 3670 | FE(HTTP_QUERY_TRANSFER_ENCODING), |
| 3671 | FE(HTTP_QUERY_UPGRADE), |
| 3672 | FE(HTTP_QUERY_VARY), |
| 3673 | FE(HTTP_QUERY_VIA), |
| 3674 | FE(HTTP_QUERY_WARNING), |
| 3675 | FE(HTTP_QUERY_CUSTOM) |
| 3676 | }; |
| 3677 | static const wininet_flag_info modifier_flags[] = { |
| 3678 | FE(HTTP_QUERY_FLAG_REQUEST_HEADERS), |
| 3679 | FE(HTTP_QUERY_FLAG_SYSTEMTIME), |
| 3680 | FE(HTTP_QUERY_FLAG_NUMBER), |
| 3681 | FE(HTTP_QUERY_FLAG_COALESCE) |
| 3682 | }; |
| 3683 | #undef FE |
| 3684 | DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK; |
| 3685 | DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK; |
Mike McCormack | 13b6ce6d | 2004-08-09 18:54:23 +0000 | [diff] [blame] | 3686 | DWORD i; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 3687 | |
Juan Lang | 66b4ad2 | 2009-12-04 14:39:37 -0800 | [diff] [blame] | 3688 | TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, info); |
Lionel Ulmer | 1d5e6b6 | 2004-02-09 22:01:49 +0000 | [diff] [blame] | 3689 | TRACE(" Attribute:"); |
| 3690 | for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) { |
| 3691 | if (query_flags[i].val == info) { |
Diego Pettenò | 869a66a | 2005-01-07 17:09:39 +0000 | [diff] [blame] | 3692 | TRACE(" %s", query_flags[i].name); |
Lionel Ulmer | 1d5e6b6 | 2004-02-09 22:01:49 +0000 | [diff] [blame] | 3693 | break; |
| 3694 | } |
| 3695 | } |
| 3696 | if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) { |
Hans Leidekker | cd2c458 | 2006-10-05 13:18:56 +0200 | [diff] [blame] | 3697 | TRACE(" Unknown (%08x)", info); |
Lionel Ulmer | 1d5e6b6 | 2004-02-09 22:01:49 +0000 | [diff] [blame] | 3698 | } |
| 3699 | |
Diego Pettenò | 869a66a | 2005-01-07 17:09:39 +0000 | [diff] [blame] | 3700 | TRACE(" Modifier:"); |
Lionel Ulmer | 1d5e6b6 | 2004-02-09 22:01:49 +0000 | [diff] [blame] | 3701 | for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) { |
| 3702 | if (modifier_flags[i].val & info_mod) { |
Diego Pettenò | 869a66a | 2005-01-07 17:09:39 +0000 | [diff] [blame] | 3703 | TRACE(" %s", modifier_flags[i].name); |
Lionel Ulmer | 1d5e6b6 | 2004-02-09 22:01:49 +0000 | [diff] [blame] | 3704 | info_mod &= ~ modifier_flags[i].val; |
| 3705 | } |
| 3706 | } |
| 3707 | |
| 3708 | if (info_mod) { |
Hans Leidekker | cd2c458 | 2006-10-05 13:18:56 +0200 | [diff] [blame] | 3709 | TRACE(" Unknown (%08x)", info_mod); |
Lionel Ulmer | 1d5e6b6 | 2004-02-09 22:01:49 +0000 | [diff] [blame] | 3710 | } |
Diego Pettenò | 869a66a | 2005-01-07 17:09:39 +0000 | [diff] [blame] | 3711 | TRACE("\n"); |
Lionel Ulmer | 1d5e6b6 | 2004-02-09 22:01:49 +0000 | [diff] [blame] | 3712 | } |
| 3713 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3714 | request = (http_request_t*) get_handle_object( hHttpRequest ); |
| 3715 | if (NULL == request || request->hdr.htype != WH_HHTTPREQ) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 3716 | { |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3717 | res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE; |
| 3718 | goto lend; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 3719 | } |
| 3720 | |
Mikołaj Zalewski | 3fa49f0 | 2007-08-15 16:55:15 -0700 | [diff] [blame] | 3721 | if (lpBuffer == NULL) |
| 3722 | *lpdwBufferLength = 0; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3723 | res = HTTP_HttpQueryInfoW( request, dwInfoLevel, |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3724 | lpBuffer, lpdwBufferLength, lpdwIndex); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 3725 | |
Mike McCormack | 3a1391b | 2004-07-19 21:49:39 +0000 | [diff] [blame] | 3726 | lend: |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3727 | if( request ) |
| 3728 | WININET_Release( &request->hdr ); |
Mike McCormack | 3a1391b | 2004-07-19 21:49:39 +0000 | [diff] [blame] | 3729 | |
Jacek Caban | 9823c23 | 2009-12-14 02:27:29 +0100 | [diff] [blame] | 3730 | TRACE("%u <--\n", res); |
| 3731 | if(res != ERROR_SUCCESS) |
| 3732 | SetLastError(res); |
| 3733 | return res == ERROR_SUCCESS; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 3734 | } |
| 3735 | |
Alberto Massari | d476a5a | 2002-11-12 02:13:04 +0000 | [diff] [blame] | 3736 | /*********************************************************************** |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 3737 | * HttpQueryInfoA (WININET.@) |
Alberto Massari | d476a5a | 2002-11-12 02:13:04 +0000 | [diff] [blame] | 3738 | * |
| 3739 | * Queries for information about an HTTP request |
| 3740 | * |
| 3741 | * RETURNS |
| 3742 | * TRUE on success |
| 3743 | * FALSE on failure |
| 3744 | * |
| 3745 | */ |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 3746 | BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel, |
Alberto Massari | d476a5a | 2002-11-12 02:13:04 +0000 | [diff] [blame] | 3747 | LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex) |
| 3748 | { |
| 3749 | BOOL result; |
Mike McCormack | 1baf39f | 2004-03-30 20:37:49 +0000 | [diff] [blame] | 3750 | DWORD len; |
| 3751 | WCHAR* bufferW; |
| 3752 | |
Alberto Massari | d476a5a | 2002-11-12 02:13:04 +0000 | [diff] [blame] | 3753 | if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) || |
| 3754 | (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)) |
| 3755 | { |
Mike McCormack | 1baf39f | 2004-03-30 20:37:49 +0000 | [diff] [blame] | 3756 | return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer, |
| 3757 | lpdwBufferLength, lpdwIndex ); |
Alberto Massari | d476a5a | 2002-11-12 02:13:04 +0000 | [diff] [blame] | 3758 | } |
Mike McCormack | 1baf39f | 2004-03-30 20:37:49 +0000 | [diff] [blame] | 3759 | |
Mikołaj Zalewski | 3fa49f0 | 2007-08-15 16:55:15 -0700 | [diff] [blame] | 3760 | if (lpBuffer) |
| 3761 | { |
Rob Shearman | 719cd82 | 2008-02-18 19:37:35 +0000 | [diff] [blame] | 3762 | DWORD alloclen; |
Mikołaj Zalewski | 3fa49f0 | 2007-08-15 16:55:15 -0700 | [diff] [blame] | 3763 | len = (*lpdwBufferLength)*sizeof(WCHAR); |
Rob Shearman | 719cd82 | 2008-02-18 19:37:35 +0000 | [diff] [blame] | 3764 | if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM) |
| 3765 | { |
| 3766 | alloclen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 ) * sizeof(WCHAR); |
| 3767 | if (alloclen < len) |
| 3768 | alloclen = len; |
| 3769 | } |
| 3770 | else |
| 3771 | alloclen = len; |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 3772 | bufferW = heap_alloc(alloclen); |
Mikołaj Zalewski | 3fa49f0 | 2007-08-15 16:55:15 -0700 | [diff] [blame] | 3773 | /* buffer is in/out because of HTTP_QUERY_CUSTOM */ |
| 3774 | if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM) |
Rob Shearman | 719cd82 | 2008-02-18 19:37:35 +0000 | [diff] [blame] | 3775 | MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, bufferW, alloclen / sizeof(WCHAR) ); |
Mikołaj Zalewski | 3fa49f0 | 2007-08-15 16:55:15 -0700 | [diff] [blame] | 3776 | } else |
| 3777 | { |
| 3778 | bufferW = NULL; |
| 3779 | len = 0; |
| 3780 | } |
| 3781 | |
Mike McCormack | 1baf39f | 2004-03-30 20:37:49 +0000 | [diff] [blame] | 3782 | result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW, |
| 3783 | &len, lpdwIndex ); |
| 3784 | if( result ) |
Alberto Massari | d476a5a | 2002-11-12 02:13:04 +0000 | [diff] [blame] | 3785 | { |
Robert Shearman | 4385d30 | 2004-07-21 21:17:03 +0000 | [diff] [blame] | 3786 | len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1, |
Mike McCormack | 1baf39f | 2004-03-30 20:37:49 +0000 | [diff] [blame] | 3787 | lpBuffer, *lpdwBufferLength, NULL, NULL ); |
Robert Shearman | 4385d30 | 2004-07-21 21:17:03 +0000 | [diff] [blame] | 3788 | *lpdwBufferLength = len - 1; |
| 3789 | |
| 3790 | TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer)); |
Alberto Massari | d476a5a | 2002-11-12 02:13:04 +0000 | [diff] [blame] | 3791 | } |
Robert Shearman | 907ac44 | 2004-07-20 01:21:08 +0000 | [diff] [blame] | 3792 | else |
| 3793 | /* since the strings being returned from HttpQueryInfoW should be |
| 3794 | * only ASCII characters, it is reasonable to assume that all of |
| 3795 | * the Unicode characters can be reduced to a single byte */ |
| 3796 | *lpdwBufferLength = len / sizeof(WCHAR); |
Robert Shearman | 4385d30 | 2004-07-21 21:17:03 +0000 | [diff] [blame] | 3797 | |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 3798 | heap_free( bufferW ); |
Alberto Massari | d476a5a | 2002-11-12 02:13:04 +0000 | [diff] [blame] | 3799 | return result; |
| 3800 | } |
Huw D M Davies | f9b6d7b | 2000-10-28 00:30:23 +0000 | [diff] [blame] | 3801 | |
| 3802 | /*********************************************************************** |
Alexandre Julliard | 9ee1b06 | 2009-05-04 20:49:15 +0200 | [diff] [blame] | 3803 | * HTTP_GetRedirectURL (internal) |
| 3804 | */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3805 | static LPWSTR HTTP_GetRedirectURL(http_request_t *request, LPCWSTR lpszUrl) |
Alexandre Julliard | 9ee1b06 | 2009-05-04 20:49:15 +0200 | [diff] [blame] | 3806 | { |
| 3807 | static WCHAR szHttp[] = {'h','t','t','p',0}; |
| 3808 | static WCHAR szHttps[] = {'h','t','t','p','s',0}; |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3809 | http_session_t *session = request->session; |
Alexandre Julliard | 9ee1b06 | 2009-05-04 20:49:15 +0200 | [diff] [blame] | 3810 | URL_COMPONENTSW urlComponents; |
| 3811 | DWORD url_length = 0; |
| 3812 | LPWSTR orig_url; |
| 3813 | LPWSTR combined_url; |
| 3814 | |
Alexandre Julliard | 9ee1b06 | 2009-05-04 20:49:15 +0200 | [diff] [blame] | 3815 | urlComponents.dwStructSize = sizeof(URL_COMPONENTSW); |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3816 | urlComponents.lpszScheme = (request->hdr.dwFlags & INTERNET_FLAG_SECURE) ? szHttps : szHttp; |
Alexandre Julliard | 9ee1b06 | 2009-05-04 20:49:15 +0200 | [diff] [blame] | 3817 | urlComponents.dwSchemeLength = 0; |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 3818 | urlComponents.lpszHostName = session->hostName; |
Alexandre Julliard | 9ee1b06 | 2009-05-04 20:49:15 +0200 | [diff] [blame] | 3819 | urlComponents.dwHostNameLength = 0; |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 3820 | urlComponents.nPort = session->hostPort; |
| 3821 | urlComponents.lpszUserName = session->userName; |
Alexandre Julliard | 9ee1b06 | 2009-05-04 20:49:15 +0200 | [diff] [blame] | 3822 | urlComponents.dwUserNameLength = 0; |
| 3823 | urlComponents.lpszPassword = NULL; |
| 3824 | urlComponents.dwPasswordLength = 0; |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3825 | urlComponents.lpszUrlPath = request->path; |
Alexandre Julliard | 9ee1b06 | 2009-05-04 20:49:15 +0200 | [diff] [blame] | 3826 | urlComponents.dwUrlPathLength = 0; |
| 3827 | urlComponents.lpszExtraInfo = NULL; |
| 3828 | urlComponents.dwExtraInfoLength = 0; |
| 3829 | |
| 3830 | if (!InternetCreateUrlW(&urlComponents, 0, NULL, &url_length) && |
| 3831 | (GetLastError() != ERROR_INSUFFICIENT_BUFFER)) |
| 3832 | return NULL; |
| 3833 | |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 3834 | orig_url = heap_alloc(url_length); |
Alexandre Julliard | 9ee1b06 | 2009-05-04 20:49:15 +0200 | [diff] [blame] | 3835 | |
| 3836 | /* convert from bytes to characters */ |
| 3837 | url_length = url_length / sizeof(WCHAR) - 1; |
| 3838 | if (!InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length)) |
| 3839 | { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 3840 | heap_free(orig_url); |
Alexandre Julliard | 9ee1b06 | 2009-05-04 20:49:15 +0200 | [diff] [blame] | 3841 | return NULL; |
| 3842 | } |
| 3843 | |
| 3844 | url_length = 0; |
| 3845 | if (!InternetCombineUrlW(orig_url, lpszUrl, NULL, &url_length, ICU_ENCODE_SPACES_ONLY) && |
| 3846 | (GetLastError() != ERROR_INSUFFICIENT_BUFFER)) |
| 3847 | { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 3848 | heap_free(orig_url); |
Alexandre Julliard | 9ee1b06 | 2009-05-04 20:49:15 +0200 | [diff] [blame] | 3849 | return NULL; |
| 3850 | } |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 3851 | combined_url = heap_alloc(url_length * sizeof(WCHAR)); |
Alexandre Julliard | 9ee1b06 | 2009-05-04 20:49:15 +0200 | [diff] [blame] | 3852 | |
| 3853 | if (!InternetCombineUrlW(orig_url, lpszUrl, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY)) |
| 3854 | { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 3855 | heap_free(orig_url); |
| 3856 | heap_free(combined_url); |
Alexandre Julliard | 9ee1b06 | 2009-05-04 20:49:15 +0200 | [diff] [blame] | 3857 | return NULL; |
| 3858 | } |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 3859 | heap_free(orig_url); |
Alexandre Julliard | 9ee1b06 | 2009-05-04 20:49:15 +0200 | [diff] [blame] | 3860 | return combined_url; |
| 3861 | } |
| 3862 | |
| 3863 | |
| 3864 | /*********************************************************************** |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 3865 | * HTTP_HandleRedirect (internal) |
| 3866 | */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3867 | static DWORD HTTP_HandleRedirect(http_request_t *request, LPCWSTR lpszUrl) |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 3868 | { |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3869 | http_session_t *session = request->session; |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 3870 | appinfo_t *hIC = session->appInfo; |
Juan Lang | 7243156 | 2011-03-01 11:00:49 -0800 | [diff] [blame] | 3871 | BOOL using_proxy = hIC->proxy && hIC->proxy[0]; |
André Hentschel | 0fda135 | 2011-08-22 21:30:55 +0200 | [diff] [blame] | 3872 | WCHAR path[INTERNET_MAX_PATH_LENGTH]; |
Dan Hipschman | a1ebffb | 2008-04-22 15:48:28 -0700 | [diff] [blame] | 3873 | int index; |
Mike McCormack | 7cc70c0 | 2004-02-07 01:03:41 +0000 | [diff] [blame] | 3874 | |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 3875 | if(lpszUrl[0]=='/') |
| 3876 | { |
| 3877 | /* if it's an absolute path, keep the same session info */ |
Hans Leidekker | 612f3c1 | 2008-03-31 20:26:16 +0200 | [diff] [blame] | 3878 | lstrcpynW(path, lpszUrl, INTERNET_MAX_URL_LENGTH); |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 3879 | } |
| 3880 | else |
| 3881 | { |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 3882 | URL_COMPONENTSW urlComponents; |
André Hentschel | 0fda135 | 2011-08-22 21:30:55 +0200 | [diff] [blame] | 3883 | WCHAR protocol[INTERNET_MAX_SCHEME_LENGTH]; |
| 3884 | WCHAR hostName[INTERNET_MAX_HOST_NAME_LENGTH]; |
| 3885 | WCHAR userName[INTERNET_MAX_USER_NAME_LENGTH]; |
Jacek Caban | ac26517 | 2012-01-24 17:31:17 +0100 | [diff] [blame] | 3886 | BOOL custom_port = FALSE; |
| 3887 | |
| 3888 | static WCHAR httpW[] = {'h','t','t','p',0}; |
| 3889 | static WCHAR httpsW[] = {'h','t','t','p','s',0}; |
Robert Shearman | 0590025 | 2006-03-09 15:11:59 +0000 | [diff] [blame] | 3890 | |
Aric Stewart | 1fc760d | 2005-11-28 17:31:02 +0100 | [diff] [blame] | 3891 | userName[0] = 0; |
| 3892 | hostName[0] = 0; |
| 3893 | protocol[0] = 0; |
| 3894 | |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 3895 | urlComponents.dwStructSize = sizeof(URL_COMPONENTSW); |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 3896 | urlComponents.lpszScheme = protocol; |
André Hentschel | 0fda135 | 2011-08-22 21:30:55 +0200 | [diff] [blame] | 3897 | urlComponents.dwSchemeLength = INTERNET_MAX_SCHEME_LENGTH; |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 3898 | urlComponents.lpszHostName = hostName; |
André Hentschel | 0fda135 | 2011-08-22 21:30:55 +0200 | [diff] [blame] | 3899 | urlComponents.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH; |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 3900 | urlComponents.lpszUserName = userName; |
André Hentschel | 0fda135 | 2011-08-22 21:30:55 +0200 | [diff] [blame] | 3901 | urlComponents.dwUserNameLength = INTERNET_MAX_USER_NAME_LENGTH; |
Robert Shearman | dc5f1cb | 2005-11-30 12:01:50 +0100 | [diff] [blame] | 3902 | urlComponents.lpszPassword = NULL; |
| 3903 | urlComponents.dwPasswordLength = 0; |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 3904 | urlComponents.lpszUrlPath = path; |
André Hentschel | 0fda135 | 2011-08-22 21:30:55 +0200 | [diff] [blame] | 3905 | urlComponents.dwUrlPathLength = INTERNET_MAX_PATH_LENGTH; |
Robert Shearman | dc5f1cb | 2005-11-30 12:01:50 +0100 | [diff] [blame] | 3906 | urlComponents.lpszExtraInfo = NULL; |
| 3907 | urlComponents.dwExtraInfoLength = 0; |
Alexandre Julliard | 9ee1b06 | 2009-05-04 20:49:15 +0200 | [diff] [blame] | 3908 | if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents)) |
Jacek Caban | a9ecdc6 | 2009-12-03 14:49:56 +0100 | [diff] [blame] | 3909 | return INTERNET_GetLastError(); |
Robert Shearman | efac01b | 2005-11-29 11:25:31 +0100 | [diff] [blame] | 3910 | |
Jacek Caban | ac26517 | 2012-01-24 17:31:17 +0100 | [diff] [blame] | 3911 | if(!strcmpiW(protocol, httpW)) { |
| 3912 | if(request->hdr.dwFlags & INTERNET_FLAG_SECURE) { |
| 3913 | TRACE("redirect from secure page to non-secure page\n"); |
| 3914 | /* FIXME: warn about from secure redirect to non-secure page */ |
| 3915 | request->hdr.dwFlags &= ~INTERNET_FLAG_SECURE; |
| 3916 | } |
Robert Shearman | efac01b | 2005-11-29 11:25:31 +0100 | [diff] [blame] | 3917 | |
Jacek Caban | ac26517 | 2012-01-24 17:31:17 +0100 | [diff] [blame] | 3918 | if(urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER) |
Aric Stewart | 1fc760d | 2005-11-28 17:31:02 +0100 | [diff] [blame] | 3919 | urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT; |
Jacek Caban | ac26517 | 2012-01-24 17:31:17 +0100 | [diff] [blame] | 3920 | else if(urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT) |
| 3921 | custom_port = TRUE; |
| 3922 | }else if(!strcmpiW(protocol, httpsW)) { |
| 3923 | if(!(request->hdr.dwFlags & INTERNET_FLAG_SECURE)) { |
| 3924 | TRACE("redirect from non-secure page to secure page\n"); |
| 3925 | /* FIXME: notify about redirect to secure page */ |
| 3926 | request->hdr.dwFlags |= INTERNET_FLAG_SECURE; |
| 3927 | } |
| 3928 | |
| 3929 | if(urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER) |
| 3930 | urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT; |
| 3931 | else if(urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT) |
| 3932 | custom_port = TRUE; |
Aric Stewart | 1fc760d | 2005-11-28 17:31:02 +0100 | [diff] [blame] | 3933 | } |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 3934 | |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 3935 | heap_free(session->hostName); |
Jacek Caban | ac26517 | 2012-01-24 17:31:17 +0100 | [diff] [blame] | 3936 | |
| 3937 | if(custom_port) { |
Aric Stewart | 1fc760d | 2005-11-28 17:31:02 +0100 | [diff] [blame] | 3938 | int len; |
André Hentschel | 1a39e29 | 2011-03-27 15:27:46 +0200 | [diff] [blame] | 3939 | static const WCHAR fmt[] = {'%','s',':','%','u',0}; |
Aric Stewart | 1fc760d | 2005-11-28 17:31:02 +0100 | [diff] [blame] | 3940 | len = lstrlenW(hostName); |
Robert Shearman | 8a8ce9c | 2005-11-29 11:35:19 +0100 | [diff] [blame] | 3941 | len += 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */ |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 3942 | session->hostName = heap_alloc(len*sizeof(WCHAR)); |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 3943 | sprintfW(session->hostName, fmt, hostName, urlComponents.nPort); |
Aric Stewart | 1fc760d | 2005-11-28 17:31:02 +0100 | [diff] [blame] | 3944 | } |
| 3945 | else |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 3946 | session->hostName = heap_strdupW(hostName); |
Aric Stewart | 1fc760d | 2005-11-28 17:31:02 +0100 | [diff] [blame] | 3947 | |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 3948 | HTTP_ProcessHeader(request, hostW, session->hostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ); |
Aric Stewart | 1fc760d | 2005-11-28 17:31:02 +0100 | [diff] [blame] | 3949 | |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 3950 | heap_free(session->userName); |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 3951 | session->userName = NULL; |
Robert Shearman | ef20936 | 2006-03-10 12:28:52 +0000 | [diff] [blame] | 3952 | if (userName[0]) |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 3953 | session->userName = heap_strdupW(userName); |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 3954 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 3955 | reset_data_stream(request); |
Jacek Caban | 36cb1ef | 2009-11-30 00:14:02 +0100 | [diff] [blame] | 3956 | |
Jacek Caban | 3aeb8eb | 2012-05-25 16:34:24 +0200 | [diff] [blame] | 3957 | if(!using_proxy && (strcmpiW(request->server->name, hostName) || request->server->port != urlComponents.nPort)) { |
| 3958 | server_t *new_server; |
| 3959 | |
Jacek Caban | 6c764fb | 2012-06-11 10:22:38 +0200 | [diff] [blame] | 3960 | new_server = get_server(hostName, urlComponents.nPort, TRUE); |
Jacek Caban | 3aeb8eb | 2012-05-25 16:34:24 +0200 | [diff] [blame] | 3961 | server_release(request->server); |
| 3962 | request->server = new_server; |
Hans Leidekker | 8210e1b | 2008-03-31 20:25:57 +0200 | [diff] [blame] | 3963 | } |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 3964 | } |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 3965 | heap_free(request->path); |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3966 | request->path=NULL; |
Jacek Caban | f979134 | 2008-02-13 13:34:05 +0100 | [diff] [blame] | 3967 | if (*path) |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 3968 | { |
| 3969 | DWORD needed = 0; |
| 3970 | HRESULT rc; |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 3971 | |
| 3972 | rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY); |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 3973 | if (rc != E_POINTER) |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 3974 | needed = strlenW(path)+1; |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 3975 | request->path = heap_alloc(needed*sizeof(WCHAR)); |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3976 | rc = UrlEscapeW(path, request->path, &needed, |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 3977 | URL_ESCAPE_SPACES_ONLY); |
Rob Shearman | d31ce9a | 2008-10-01 11:20:49 +0100 | [diff] [blame] | 3978 | if (rc != S_OK) |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 3979 | { |
Hans Leidekker | cd2c458 | 2006-10-05 13:18:56 +0200 | [diff] [blame] | 3980 | ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc); |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 3981 | strcpyW(request->path,path); |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 3982 | } |
| 3983 | } |
| 3984 | |
Dan Hipschman | a1ebffb | 2008-04-22 15:48:28 -0700 | [diff] [blame] | 3985 | /* Remove custom content-type/length headers on redirects. */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3986 | index = HTTP_GetCustomHeaderIndex(request, szContent_Type, 0, TRUE); |
Dan Hipschman | a1ebffb | 2008-04-22 15:48:28 -0700 | [diff] [blame] | 3987 | if (0 <= index) |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3988 | HTTP_DeleteCustomHeader(request, index); |
| 3989 | index = HTTP_GetCustomHeaderIndex(request, szContent_Length, 0, TRUE); |
Dan Hipschman | a1ebffb | 2008-04-22 15:48:28 -0700 | [diff] [blame] | 3990 | if (0 <= index) |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 3991 | HTTP_DeleteCustomHeader(request, index); |
Dan Hipschman | a1ebffb | 2008-04-22 15:48:28 -0700 | [diff] [blame] | 3992 | |
Jacek Caban | a9ecdc6 | 2009-12-03 14:49:56 +0100 | [diff] [blame] | 3993 | return ERROR_SUCCESS; |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 3994 | } |
| 3995 | |
| 3996 | /*********************************************************************** |
Mike McCormack | a496906 | 2004-07-04 00:24:47 +0000 | [diff] [blame] | 3997 | * HTTP_build_req (internal) |
| 3998 | * |
| 3999 | * concatenate all the strings in the request together |
| 4000 | */ |
| 4001 | static LPWSTR HTTP_build_req( LPCWSTR *list, int len ) |
| 4002 | { |
| 4003 | LPCWSTR *t; |
| 4004 | LPWSTR str; |
| 4005 | |
| 4006 | for( t = list; *t ; t++ ) |
| 4007 | len += strlenW( *t ); |
| 4008 | len++; |
| 4009 | |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 4010 | str = heap_alloc(len*sizeof(WCHAR)); |
Mike McCormack | a496906 | 2004-07-04 00:24:47 +0000 | [diff] [blame] | 4011 | *str = 0; |
| 4012 | |
| 4013 | for( t = list; *t ; t++ ) |
| 4014 | strcatW( str, *t ); |
| 4015 | |
| 4016 | return str; |
| 4017 | } |
| 4018 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4019 | static DWORD HTTP_SecureProxyConnect(http_request_t *request) |
Robert Shearman | 0e7c41e | 2005-11-28 11:55:16 +0100 | [diff] [blame] | 4020 | { |
| 4021 | LPWSTR lpszPath; |
| 4022 | LPWSTR requestString; |
| 4023 | INT len; |
| 4024 | INT cnt; |
| 4025 | INT responseLen; |
| 4026 | char *ascii_req; |
Jacek Caban | b77868c | 2009-11-30 00:13:21 +0100 | [diff] [blame] | 4027 | DWORD res; |
Robert Shearman | 0e7c41e | 2005-11-28 11:55:16 +0100 | [diff] [blame] | 4028 | static const WCHAR szConnect[] = {'C','O','N','N','E','C','T',0}; |
André Hentschel | 1a39e29 | 2011-03-27 15:27:46 +0200 | [diff] [blame] | 4029 | static const WCHAR szFormat[] = {'%','s',':','%','u',0}; |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 4030 | http_session_t *session = request->session; |
Robert Shearman | 0e7c41e | 2005-11-28 11:55:16 +0100 | [diff] [blame] | 4031 | |
| 4032 | TRACE("\n"); |
| 4033 | |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 4034 | lpszPath = heap_alloc((lstrlenW( session->hostName ) + 13)*sizeof(WCHAR)); |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 4035 | sprintfW( lpszPath, szFormat, session->hostName, session->hostPort ); |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4036 | requestString = HTTP_BuildHeaderRequestString( request, szConnect, lpszPath, g_szHttp1_1 ); |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 4037 | heap_free( lpszPath ); |
Robert Shearman | 0e7c41e | 2005-11-28 11:55:16 +0100 | [diff] [blame] | 4038 | |
| 4039 | len = WideCharToMultiByte( CP_ACP, 0, requestString, -1, |
| 4040 | NULL, 0, NULL, NULL ); |
| 4041 | len--; /* the nul terminator isn't needed */ |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 4042 | ascii_req = heap_alloc(len); |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 4043 | WideCharToMultiByte( CP_ACP, 0, requestString, -1, ascii_req, len, NULL, NULL ); |
| 4044 | heap_free( requestString ); |
Robert Shearman | 0e7c41e | 2005-11-28 11:55:16 +0100 | [diff] [blame] | 4045 | |
| 4046 | TRACE("full request -> %s\n", debugstr_an( ascii_req, len ) ); |
| 4047 | |
Hans Leidekker | 6522393 | 2012-01-13 15:15:48 +0100 | [diff] [blame] | 4048 | NETCON_set_timeout( request->netconn, TRUE, request->send_timeout ); |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 4049 | res = NETCON_send( request->netconn, ascii_req, len, 0, &cnt ); |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 4050 | heap_free( ascii_req ); |
Jacek Caban | 36cb1ef | 2009-11-30 00:14:02 +0100 | [diff] [blame] | 4051 | if (res != ERROR_SUCCESS) |
| 4052 | return res; |
Robert Shearman | 0e7c41e | 2005-11-28 11:55:16 +0100 | [diff] [blame] | 4053 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4054 | responseLen = HTTP_GetResponseHeaders( request, TRUE ); |
Robert Shearman | 0e7c41e | 2005-11-28 11:55:16 +0100 | [diff] [blame] | 4055 | if (!responseLen) |
Jacek Caban | 36cb1ef | 2009-11-30 00:14:02 +0100 | [diff] [blame] | 4056 | return ERROR_HTTP_INVALID_HEADER; |
Robert Shearman | 0e7c41e | 2005-11-28 11:55:16 +0100 | [diff] [blame] | 4057 | |
Jacek Caban | 36cb1ef | 2009-11-30 00:14:02 +0100 | [diff] [blame] | 4058 | return ERROR_SUCCESS; |
Robert Shearman | 0e7c41e | 2005-11-28 11:55:16 +0100 | [diff] [blame] | 4059 | } |
| 4060 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4061 | static void HTTP_InsertCookies(http_request_t *request) |
Hans Leidekker | 572b0ba | 2008-07-19 19:55:39 +0200 | [diff] [blame] | 4062 | { |
Jacek Caban | dce9181 | 2011-05-19 16:11:30 +0200 | [diff] [blame] | 4063 | DWORD cookie_size, size, cnt = 0; |
| 4064 | HTTPHEADERW *host; |
| 4065 | WCHAR *cookies; |
Hans Leidekker | 572b0ba | 2008-07-19 19:55:39 +0200 | [diff] [blame] | 4066 | |
Jacek Caban | dce9181 | 2011-05-19 16:11:30 +0200 | [diff] [blame] | 4067 | static const WCHAR cookieW[] = {'C','o','o','k','i','e',':',' ',0}; |
Hans Leidekker | 572b0ba | 2008-07-19 19:55:39 +0200 | [diff] [blame] | 4068 | |
Jacek Caban | dce9181 | 2011-05-19 16:11:30 +0200 | [diff] [blame] | 4069 | host = HTTP_GetHeader(request, hostW); |
| 4070 | if(!host) |
| 4071 | return; |
Hans Leidekker | 572b0ba | 2008-07-19 19:55:39 +0200 | [diff] [blame] | 4072 | |
Jacek Caban | dce9181 | 2011-05-19 16:11:30 +0200 | [diff] [blame] | 4073 | if(!get_cookie(host->lpszValue, request->path, NULL, &cookie_size)) |
| 4074 | return; |
Hans Leidekker | 572b0ba | 2008-07-19 19:55:39 +0200 | [diff] [blame] | 4075 | |
Jacek Caban | dce9181 | 2011-05-19 16:11:30 +0200 | [diff] [blame] | 4076 | size = sizeof(cookieW) + cookie_size * sizeof(WCHAR) + sizeof(szCrLf); |
| 4077 | if(!(cookies = heap_alloc(size))) |
| 4078 | return; |
| 4079 | |
| 4080 | cnt += sprintfW(cookies, cookieW); |
| 4081 | get_cookie(host->lpszValue, request->path, cookies+cnt, &cookie_size); |
| 4082 | strcatW(cookies, szCrLf); |
| 4083 | |
| 4084 | HTTP_HttpAddRequestHeadersW(request, cookies, strlenW(cookies), HTTP_ADDREQ_FLAG_REPLACE); |
| 4085 | |
| 4086 | heap_free(cookies); |
Hans Leidekker | 572b0ba | 2008-07-19 19:55:39 +0200 | [diff] [blame] | 4087 | } |
| 4088 | |
Juan Lang | aeca2f9 | 2011-10-21 22:55:14 -0700 | [diff] [blame] | 4089 | static WORD HTTP_ParseWkday(LPCWSTR day) |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4090 | { |
Francois Gouget | 4bacb3f | 2011-03-11 18:22:23 +0100 | [diff] [blame] | 4091 | static const WCHAR days[7][4] = {{ 's','u','n',0 }, |
| 4092 | { 'm','o','n',0 }, |
| 4093 | { 't','u','e',0 }, |
| 4094 | { 'w','e','d',0 }, |
| 4095 | { 't','h','u',0 }, |
| 4096 | { 'f','r','i',0 }, |
| 4097 | { 's','a','t',0 }}; |
| 4098 | int i; |
| 4099 | for (i = 0; i < sizeof(days)/sizeof(*days); i++) |
| 4100 | if (!strcmpiW(day, days[i])) |
| 4101 | return i; |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4102 | |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4103 | /* Invalid */ |
| 4104 | return 7; |
| 4105 | } |
| 4106 | |
| 4107 | static WORD HTTP_ParseMonth(LPCWSTR month) |
| 4108 | { |
| 4109 | static const WCHAR jan[] = { 'j','a','n',0 }; |
| 4110 | static const WCHAR feb[] = { 'f','e','b',0 }; |
| 4111 | static const WCHAR mar[] = { 'm','a','r',0 }; |
| 4112 | static const WCHAR apr[] = { 'a','p','r',0 }; |
| 4113 | static const WCHAR may[] = { 'm','a','y',0 }; |
| 4114 | static const WCHAR jun[] = { 'j','u','n',0 }; |
| 4115 | static const WCHAR jul[] = { 'j','u','l',0 }; |
| 4116 | static const WCHAR aug[] = { 'a','u','g',0 }; |
| 4117 | static const WCHAR sep[] = { 's','e','p',0 }; |
| 4118 | static const WCHAR oct[] = { 'o','c','t',0 }; |
| 4119 | static const WCHAR nov[] = { 'n','o','v',0 }; |
| 4120 | static const WCHAR dec[] = { 'd','e','c',0 }; |
| 4121 | |
| 4122 | if (!strcmpiW(month, jan)) return 1; |
| 4123 | if (!strcmpiW(month, feb)) return 2; |
| 4124 | if (!strcmpiW(month, mar)) return 3; |
| 4125 | if (!strcmpiW(month, apr)) return 4; |
| 4126 | if (!strcmpiW(month, may)) return 5; |
| 4127 | if (!strcmpiW(month, jun)) return 6; |
| 4128 | if (!strcmpiW(month, jul)) return 7; |
| 4129 | if (!strcmpiW(month, aug)) return 8; |
| 4130 | if (!strcmpiW(month, sep)) return 9; |
| 4131 | if (!strcmpiW(month, oct)) return 10; |
| 4132 | if (!strcmpiW(month, nov)) return 11; |
| 4133 | if (!strcmpiW(month, dec)) return 12; |
| 4134 | /* Invalid */ |
| 4135 | return 0; |
| 4136 | } |
| 4137 | |
Juan Lang | 28e9229 | 2011-03-04 11:43:54 -0800 | [diff] [blame] | 4138 | /* Parses the string pointed to by *str, assumed to be a 24-hour time HH:MM:SS, |
| 4139 | * optionally preceded by whitespace. |
| 4140 | * Upon success, returns TRUE, sets the wHour, wMinute, and wSecond fields of |
| 4141 | * st, and sets *str to the first character after the time format. |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4142 | */ |
Juan Lang | 28e9229 | 2011-03-04 11:43:54 -0800 | [diff] [blame] | 4143 | static BOOL HTTP_ParseTime(SYSTEMTIME *st, LPCWSTR *str) |
| 4144 | { |
| 4145 | LPCWSTR ptr = *str; |
| 4146 | WCHAR *nextPtr; |
| 4147 | unsigned long num; |
| 4148 | |
| 4149 | while (isspaceW(*ptr)) |
| 4150 | ptr++; |
| 4151 | |
| 4152 | num = strtoulW(ptr, &nextPtr, 10); |
| 4153 | if (!nextPtr || nextPtr <= ptr || *nextPtr != ':') |
| 4154 | { |
| 4155 | ERR("unexpected time format %s\n", debugstr_w(ptr)); |
| 4156 | return FALSE; |
| 4157 | } |
| 4158 | if (num > 23) |
| 4159 | { |
| 4160 | ERR("unexpected hour in time format %s\n", debugstr_w(ptr)); |
| 4161 | return FALSE; |
| 4162 | } |
| 4163 | ptr = nextPtr + 1; |
| 4164 | st->wHour = (WORD)num; |
| 4165 | num = strtoulW(ptr, &nextPtr, 10); |
| 4166 | if (!nextPtr || nextPtr <= ptr || *nextPtr != ':') |
| 4167 | { |
| 4168 | ERR("unexpected time format %s\n", debugstr_w(ptr)); |
| 4169 | return FALSE; |
| 4170 | } |
| 4171 | if (num > 59) |
| 4172 | { |
| 4173 | ERR("unexpected minute in time format %s\n", debugstr_w(ptr)); |
| 4174 | return FALSE; |
| 4175 | } |
| 4176 | ptr = nextPtr + 1; |
| 4177 | st->wMinute = (WORD)num; |
| 4178 | num = strtoulW(ptr, &nextPtr, 10); |
| 4179 | if (!nextPtr || nextPtr <= ptr) |
| 4180 | { |
| 4181 | ERR("unexpected time format %s\n", debugstr_w(ptr)); |
| 4182 | return FALSE; |
| 4183 | } |
| 4184 | if (num > 59) |
| 4185 | { |
| 4186 | ERR("unexpected second in time format %s\n", debugstr_w(ptr)); |
| 4187 | return FALSE; |
| 4188 | } |
| 4189 | ptr = nextPtr + 1; |
| 4190 | *str = ptr; |
| 4191 | st->wSecond = (WORD)num; |
| 4192 | return TRUE; |
| 4193 | } |
| 4194 | |
| 4195 | static BOOL HTTP_ParseDateAsAsctime(LPCWSTR value, FILETIME *ft) |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4196 | { |
| 4197 | static const WCHAR gmt[]= { 'G','M','T',0 }; |
Juan Lang | 28e9229 | 2011-03-04 11:43:54 -0800 | [diff] [blame] | 4198 | WCHAR day[4], *dayPtr, month[4], *monthPtr, *nextPtr; |
| 4199 | LPCWSTR ptr; |
| 4200 | SYSTEMTIME st = { 0 }; |
| 4201 | unsigned long num; |
| 4202 | |
| 4203 | for (ptr = value, dayPtr = day; *ptr && !isspaceW(*ptr) && |
| 4204 | dayPtr - day < sizeof(day) / sizeof(day[0]) - 1; ptr++, dayPtr++) |
| 4205 | *dayPtr = *ptr; |
| 4206 | *dayPtr = 0; |
Juan Lang | aeca2f9 | 2011-10-21 22:55:14 -0700 | [diff] [blame] | 4207 | st.wDayOfWeek = HTTP_ParseWkday(day); |
Juan Lang | 28e9229 | 2011-03-04 11:43:54 -0800 | [diff] [blame] | 4208 | if (st.wDayOfWeek >= 7) |
| 4209 | { |
| 4210 | ERR("unexpected weekday %s\n", debugstr_w(day)); |
| 4211 | return FALSE; |
| 4212 | } |
| 4213 | |
| 4214 | while (isspaceW(*ptr)) |
| 4215 | ptr++; |
| 4216 | |
| 4217 | for (monthPtr = month; !isspace(*ptr) && |
| 4218 | monthPtr - month < sizeof(month) / sizeof(month[0]) - 1; |
| 4219 | monthPtr++, ptr++) |
| 4220 | *monthPtr = *ptr; |
| 4221 | *monthPtr = 0; |
| 4222 | st.wMonth = HTTP_ParseMonth(month); |
| 4223 | if (!st.wMonth || st.wMonth > 12) |
| 4224 | { |
| 4225 | ERR("unexpected month %s\n", debugstr_w(month)); |
| 4226 | return FALSE; |
| 4227 | } |
| 4228 | |
| 4229 | while (isspaceW(*ptr)) |
| 4230 | ptr++; |
| 4231 | |
| 4232 | num = strtoulW(ptr, &nextPtr, 10); |
| 4233 | if (!nextPtr || nextPtr <= ptr || !num || num > 31) |
| 4234 | { |
| 4235 | ERR("unexpected day %s\n", debugstr_w(ptr)); |
| 4236 | return FALSE; |
| 4237 | } |
| 4238 | ptr = nextPtr; |
| 4239 | st.wDay = (WORD)num; |
| 4240 | |
| 4241 | while (isspaceW(*ptr)) |
| 4242 | ptr++; |
| 4243 | |
| 4244 | if (!HTTP_ParseTime(&st, &ptr)) |
| 4245 | return FALSE; |
| 4246 | |
| 4247 | while (isspaceW(*ptr)) |
| 4248 | ptr++; |
| 4249 | |
| 4250 | num = strtoulW(ptr, &nextPtr, 10); |
| 4251 | if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827) |
| 4252 | { |
| 4253 | ERR("unexpected year %s\n", debugstr_w(ptr)); |
| 4254 | return FALSE; |
| 4255 | } |
| 4256 | ptr = nextPtr; |
| 4257 | st.wYear = (WORD)num; |
| 4258 | |
| 4259 | while (isspaceW(*ptr)) |
| 4260 | ptr++; |
| 4261 | |
| 4262 | /* asctime() doesn't report a timezone, but some web servers do, so accept |
| 4263 | * with or without GMT. |
| 4264 | */ |
| 4265 | if (*ptr && strcmpW(ptr, gmt)) |
| 4266 | { |
| 4267 | ERR("unexpected timezone %s\n", debugstr_w(ptr)); |
| 4268 | return FALSE; |
| 4269 | } |
| 4270 | return SystemTimeToFileTime(&st, ft); |
| 4271 | } |
| 4272 | |
| 4273 | static BOOL HTTP_ParseRfc1123Date(LPCWSTR value, FILETIME *ft) |
| 4274 | { |
| 4275 | static const WCHAR gmt[]= { 'G','M','T',0 }; |
| 4276 | WCHAR *nextPtr, day[4], month[4], *monthPtr; |
| 4277 | LPCWSTR ptr; |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4278 | unsigned long num; |
Juan Lang | b9673bc | 2011-03-07 13:13:11 -0800 | [diff] [blame] | 4279 | SYSTEMTIME st = { 0 }; |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4280 | |
| 4281 | ptr = strchrW(value, ','); |
| 4282 | if (!ptr) |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4283 | return FALSE; |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4284 | if (ptr - value != 3) |
| 4285 | { |
Juan Lang | aeca2f9 | 2011-10-21 22:55:14 -0700 | [diff] [blame] | 4286 | WARN("unexpected weekday %s\n", debugstr_wn(value, ptr - value)); |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4287 | return FALSE; |
| 4288 | } |
| 4289 | memcpy(day, value, (ptr - value) * sizeof(WCHAR)); |
| 4290 | day[3] = 0; |
Juan Lang | aeca2f9 | 2011-10-21 22:55:14 -0700 | [diff] [blame] | 4291 | st.wDayOfWeek = HTTP_ParseWkday(day); |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4292 | if (st.wDayOfWeek > 6) |
| 4293 | { |
Juan Lang | aeca2f9 | 2011-10-21 22:55:14 -0700 | [diff] [blame] | 4294 | WARN("unexpected weekday %s\n", debugstr_wn(value, ptr - value)); |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4295 | return FALSE; |
| 4296 | } |
| 4297 | ptr++; |
| 4298 | |
| 4299 | while (isspaceW(*ptr)) |
| 4300 | ptr++; |
| 4301 | |
| 4302 | num = strtoulW(ptr, &nextPtr, 10); |
| 4303 | if (!nextPtr || nextPtr <= ptr || !num || num > 31) |
| 4304 | { |
Juan Lang | aeca2f9 | 2011-10-21 22:55:14 -0700 | [diff] [blame] | 4305 | WARN("unexpected day %s\n", debugstr_w(value)); |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4306 | return FALSE; |
| 4307 | } |
| 4308 | ptr = nextPtr; |
| 4309 | st.wDay = (WORD)num; |
| 4310 | |
| 4311 | while (isspaceW(*ptr)) |
| 4312 | ptr++; |
| 4313 | |
| 4314 | for (monthPtr = month; !isspace(*ptr) && |
| 4315 | monthPtr - month < sizeof(month) / sizeof(month[0]) - 1; |
| 4316 | monthPtr++, ptr++) |
| 4317 | *monthPtr = *ptr; |
| 4318 | *monthPtr = 0; |
| 4319 | st.wMonth = HTTP_ParseMonth(month); |
| 4320 | if (!st.wMonth || st.wMonth > 12) |
| 4321 | { |
Juan Lang | aeca2f9 | 2011-10-21 22:55:14 -0700 | [diff] [blame] | 4322 | WARN("unexpected month %s\n", debugstr_w(month)); |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4323 | return FALSE; |
| 4324 | } |
| 4325 | |
| 4326 | while (isspaceW(*ptr)) |
| 4327 | ptr++; |
| 4328 | |
| 4329 | num = strtoulW(ptr, &nextPtr, 10); |
| 4330 | if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827) |
| 4331 | { |
| 4332 | ERR("unexpected year %s\n", debugstr_w(value)); |
| 4333 | return FALSE; |
| 4334 | } |
| 4335 | ptr = nextPtr; |
| 4336 | st.wYear = (WORD)num; |
| 4337 | |
Juan Lang | 28e9229 | 2011-03-04 11:43:54 -0800 | [diff] [blame] | 4338 | if (!HTTP_ParseTime(&st, &ptr)) |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4339 | return FALSE; |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4340 | |
| 4341 | while (isspaceW(*ptr)) |
| 4342 | ptr++; |
| 4343 | |
| 4344 | if (strcmpW(ptr, gmt)) |
| 4345 | { |
| 4346 | ERR("unexpected time zone %s\n", debugstr_w(ptr)); |
| 4347 | return FALSE; |
| 4348 | } |
| 4349 | return SystemTimeToFileTime(&st, ft); |
| 4350 | } |
| 4351 | |
Juan Lang | aeca2f9 | 2011-10-21 22:55:14 -0700 | [diff] [blame] | 4352 | static WORD HTTP_ParseWeekday(LPCWSTR day) |
| 4353 | { |
| 4354 | static const WCHAR days[7][10] = {{ 's','u','n','d','a','y',0 }, |
| 4355 | { 'm','o','n','d','a','y',0 }, |
| 4356 | { 't','u','e','s','d','a','y',0 }, |
| 4357 | { 'w','e','d','n','e','s','d','a','y',0 }, |
| 4358 | { 't','h','u','r','s','d','a','y',0 }, |
| 4359 | { 'f','r','i','d','a','y',0 }, |
| 4360 | { 's','a','t','u','r','d','a','y',0 }}; |
| 4361 | int i; |
| 4362 | for (i = 0; i < sizeof(days)/sizeof(*days); i++) |
| 4363 | if (!strcmpiW(day, days[i])) |
| 4364 | return i; |
| 4365 | |
| 4366 | /* Invalid */ |
| 4367 | return 7; |
| 4368 | } |
| 4369 | |
| 4370 | static BOOL HTTP_ParseRfc850Date(LPCWSTR value, FILETIME *ft) |
| 4371 | { |
| 4372 | static const WCHAR gmt[]= { 'G','M','T',0 }; |
| 4373 | WCHAR *nextPtr, day[10], month[4], *monthPtr; |
| 4374 | LPCWSTR ptr; |
| 4375 | unsigned long num; |
| 4376 | SYSTEMTIME st = { 0 }; |
| 4377 | |
| 4378 | ptr = strchrW(value, ','); |
| 4379 | if (!ptr) |
| 4380 | return FALSE; |
| 4381 | if (ptr - value == 3) |
| 4382 | { |
| 4383 | memcpy(day, value, (ptr - value) * sizeof(WCHAR)); |
| 4384 | day[3] = 0; |
| 4385 | st.wDayOfWeek = HTTP_ParseWkday(day); |
| 4386 | if (st.wDayOfWeek > 6) |
| 4387 | { |
| 4388 | ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value)); |
| 4389 | return FALSE; |
| 4390 | } |
| 4391 | } |
Juan Lang | ad3e22d | 2011-10-24 13:21:10 -0700 | [diff] [blame] | 4392 | else if (ptr - value < sizeof(day) / sizeof(day[0])) |
Juan Lang | aeca2f9 | 2011-10-21 22:55:14 -0700 | [diff] [blame] | 4393 | { |
| 4394 | memcpy(day, value, (ptr - value) * sizeof(WCHAR)); |
| 4395 | day[ptr - value + 1] = 0; |
| 4396 | st.wDayOfWeek = HTTP_ParseWeekday(day); |
| 4397 | if (st.wDayOfWeek > 6) |
| 4398 | { |
| 4399 | ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value)); |
| 4400 | return FALSE; |
| 4401 | } |
| 4402 | } |
| 4403 | else |
| 4404 | { |
| 4405 | ERR("unexpected weekday %s\n", debugstr_wn(value, ptr - value)); |
| 4406 | return FALSE; |
| 4407 | } |
| 4408 | ptr++; |
| 4409 | |
| 4410 | while (isspaceW(*ptr)) |
| 4411 | ptr++; |
| 4412 | |
| 4413 | num = strtoulW(ptr, &nextPtr, 10); |
| 4414 | if (!nextPtr || nextPtr <= ptr || !num || num > 31) |
| 4415 | { |
| 4416 | ERR("unexpected day %s\n", debugstr_w(value)); |
| 4417 | return FALSE; |
| 4418 | } |
| 4419 | ptr = nextPtr; |
| 4420 | st.wDay = (WORD)num; |
| 4421 | |
| 4422 | if (*ptr != '-') |
| 4423 | { |
| 4424 | ERR("unexpected month format %s\n", debugstr_w(ptr)); |
| 4425 | return FALSE; |
| 4426 | } |
| 4427 | ptr++; |
| 4428 | |
| 4429 | for (monthPtr = month; *ptr != '-' && |
| 4430 | monthPtr - month < sizeof(month) / sizeof(month[0]) - 1; |
| 4431 | monthPtr++, ptr++) |
| 4432 | *monthPtr = *ptr; |
| 4433 | *monthPtr = 0; |
| 4434 | st.wMonth = HTTP_ParseMonth(month); |
| 4435 | if (!st.wMonth || st.wMonth > 12) |
| 4436 | { |
| 4437 | ERR("unexpected month %s\n", debugstr_w(month)); |
| 4438 | return FALSE; |
| 4439 | } |
| 4440 | |
| 4441 | if (*ptr != '-') |
| 4442 | { |
| 4443 | ERR("unexpected year format %s\n", debugstr_w(ptr)); |
| 4444 | return FALSE; |
| 4445 | } |
| 4446 | ptr++; |
| 4447 | |
| 4448 | num = strtoulW(ptr, &nextPtr, 10); |
| 4449 | if (!nextPtr || nextPtr <= ptr || num < 1601 || num > 30827) |
| 4450 | { |
| 4451 | ERR("unexpected year %s\n", debugstr_w(value)); |
| 4452 | return FALSE; |
| 4453 | } |
| 4454 | ptr = nextPtr; |
| 4455 | st.wYear = (WORD)num; |
| 4456 | |
| 4457 | if (!HTTP_ParseTime(&st, &ptr)) |
| 4458 | return FALSE; |
| 4459 | |
| 4460 | while (isspaceW(*ptr)) |
| 4461 | ptr++; |
| 4462 | |
| 4463 | if (strcmpW(ptr, gmt)) |
| 4464 | { |
| 4465 | ERR("unexpected time zone %s\n", debugstr_w(ptr)); |
| 4466 | return FALSE; |
| 4467 | } |
| 4468 | return SystemTimeToFileTime(&st, ft); |
| 4469 | } |
| 4470 | |
Juan Lang | 28e9229 | 2011-03-04 11:43:54 -0800 | [diff] [blame] | 4471 | static BOOL HTTP_ParseDate(LPCWSTR value, FILETIME *ft) |
| 4472 | { |
Juan Lang | d797e5f | 2011-05-13 06:47:49 -0700 | [diff] [blame] | 4473 | static const WCHAR zero[] = { '0',0 }; |
Juan Lang | 28e9229 | 2011-03-04 11:43:54 -0800 | [diff] [blame] | 4474 | BOOL ret; |
| 4475 | |
Juan Lang | d797e5f | 2011-05-13 06:47:49 -0700 | [diff] [blame] | 4476 | if (!strcmpW(value, zero)) |
| 4477 | { |
| 4478 | ft->dwLowDateTime = ft->dwHighDateTime = 0; |
| 4479 | ret = TRUE; |
| 4480 | } |
| 4481 | else if (strchrW(value, ',')) |
Juan Lang | aeca2f9 | 2011-10-21 22:55:14 -0700 | [diff] [blame] | 4482 | { |
Juan Lang | 28e9229 | 2011-03-04 11:43:54 -0800 | [diff] [blame] | 4483 | ret = HTTP_ParseRfc1123Date(value, ft); |
Juan Lang | aeca2f9 | 2011-10-21 22:55:14 -0700 | [diff] [blame] | 4484 | if (!ret) |
| 4485 | { |
| 4486 | ret = HTTP_ParseRfc850Date(value, ft); |
| 4487 | if (!ret) |
| 4488 | ERR("unexpected date format %s\n", debugstr_w(value)); |
| 4489 | } |
| 4490 | } |
Juan Lang | 28e9229 | 2011-03-04 11:43:54 -0800 | [diff] [blame] | 4491 | else |
| 4492 | { |
| 4493 | ret = HTTP_ParseDateAsAsctime(value, ft); |
| 4494 | if (!ret) |
| 4495 | ERR("unexpected date format %s\n", debugstr_w(value)); |
| 4496 | } |
| 4497 | return ret; |
| 4498 | } |
| 4499 | |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4500 | static void HTTP_ProcessExpires(http_request_t *request) |
| 4501 | { |
Juan Lang | ab16c75 | 2011-03-03 10:54:07 -0800 | [diff] [blame] | 4502 | BOOL expirationFound = FALSE; |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4503 | int headerIndex; |
| 4504 | |
Juan Lang | 488c2d0 | 2011-03-03 10:54:47 -0800 | [diff] [blame] | 4505 | /* Look for a Cache-Control header with a max-age directive, as it takes |
| 4506 | * precedence over the Expires header. |
| 4507 | */ |
| 4508 | headerIndex = HTTP_GetCustomHeaderIndex(request, szCache_Control, 0, FALSE); |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4509 | if (headerIndex != -1) |
| 4510 | { |
Juan Lang | 488c2d0 | 2011-03-03 10:54:47 -0800 | [diff] [blame] | 4511 | LPHTTPHEADERW ccHeader = &request->custHeaders[headerIndex]; |
| 4512 | LPWSTR ptr; |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4513 | |
Juan Lang | 488c2d0 | 2011-03-03 10:54:47 -0800 | [diff] [blame] | 4514 | for (ptr = ccHeader->lpszValue; ptr && *ptr; ) |
Juan Lang | ab16c75 | 2011-03-03 10:54:07 -0800 | [diff] [blame] | 4515 | { |
Juan Lang | 488c2d0 | 2011-03-03 10:54:47 -0800 | [diff] [blame] | 4516 | LPWSTR comma = strchrW(ptr, ','), end, equal; |
| 4517 | |
| 4518 | if (comma) |
| 4519 | end = comma; |
| 4520 | else |
| 4521 | end = ptr + strlenW(ptr); |
| 4522 | for (equal = end - 1; equal > ptr && *equal != '='; equal--) |
| 4523 | ; |
| 4524 | if (*equal == '=') |
| 4525 | { |
| 4526 | static const WCHAR max_age[] = { |
| 4527 | 'm','a','x','-','a','g','e',0 }; |
| 4528 | |
| 4529 | if (!strncmpiW(ptr, max_age, equal - ptr - 1)) |
| 4530 | { |
| 4531 | LPWSTR nextPtr; |
| 4532 | unsigned long age; |
| 4533 | |
| 4534 | age = strtoulW(equal + 1, &nextPtr, 10); |
| 4535 | if (nextPtr > equal + 1) |
| 4536 | { |
| 4537 | LARGE_INTEGER ft; |
| 4538 | |
| 4539 | NtQuerySystemTime( &ft ); |
| 4540 | /* Age is in seconds, FILETIME resolution is in |
| 4541 | * 100 nanosecond intervals. |
| 4542 | */ |
| 4543 | ft.QuadPart += age * (ULONGLONG)1000000; |
| 4544 | request->expires.dwLowDateTime = ft.u.LowPart; |
| 4545 | request->expires.dwHighDateTime = ft.u.HighPart; |
| 4546 | expirationFound = TRUE; |
| 4547 | } |
| 4548 | } |
| 4549 | } |
| 4550 | if (comma) |
| 4551 | { |
| 4552 | ptr = comma + 1; |
| 4553 | while (isspaceW(*ptr)) |
| 4554 | ptr++; |
| 4555 | } |
| 4556 | else |
| 4557 | ptr = NULL; |
| 4558 | } |
| 4559 | } |
| 4560 | if (!expirationFound) |
| 4561 | { |
| 4562 | headerIndex = HTTP_GetCustomHeaderIndex(request, szExpires, 0, FALSE); |
| 4563 | if (headerIndex != -1) |
| 4564 | { |
| 4565 | LPHTTPHEADERW expiresHeader = &request->custHeaders[headerIndex]; |
| 4566 | FILETIME ft; |
| 4567 | |
| 4568 | if (HTTP_ParseDate(expiresHeader->lpszValue, &ft)) |
| 4569 | { |
| 4570 | expirationFound = TRUE; |
| 4571 | request->expires = ft; |
| 4572 | } |
Juan Lang | ab16c75 | 2011-03-03 10:54:07 -0800 | [diff] [blame] | 4573 | } |
| 4574 | } |
| 4575 | if (!expirationFound) |
| 4576 | { |
Juan Lang | 2d32343 | 2011-03-03 10:54:07 -0800 | [diff] [blame] | 4577 | LARGE_INTEGER t; |
Juan Lang | ab16c75 | 2011-03-03 10:54:07 -0800 | [diff] [blame] | 4578 | |
| 4579 | /* With no known age, default to 10 minutes until expiration. */ |
Juan Lang | 2d32343 | 2011-03-03 10:54:07 -0800 | [diff] [blame] | 4580 | NtQuerySystemTime( &t ); |
| 4581 | t.QuadPart += 10 * 60 * (ULONGLONG)10000000; |
| 4582 | request->expires.dwLowDateTime = t.u.LowPart; |
| 4583 | request->expires.dwHighDateTime = t.u.HighPart; |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4584 | } |
| 4585 | } |
| 4586 | |
Juan Lang | 28e9229 | 2011-03-04 11:43:54 -0800 | [diff] [blame] | 4587 | static void HTTP_ProcessLastModified(http_request_t *request) |
| 4588 | { |
| 4589 | int headerIndex; |
| 4590 | |
| 4591 | headerIndex = HTTP_GetCustomHeaderIndex(request, szLast_Modified, 0, FALSE); |
| 4592 | if (headerIndex != -1) |
| 4593 | { |
| 4594 | LPHTTPHEADERW expiresHeader = &request->custHeaders[headerIndex]; |
| 4595 | FILETIME ft; |
| 4596 | |
| 4597 | if (HTTP_ParseDate(expiresHeader->lpszValue, &ft)) |
| 4598 | request->last_modified = ft; |
| 4599 | } |
| 4600 | } |
| 4601 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 4602 | static void http_process_keep_alive(http_request_t *req) |
| 4603 | { |
| 4604 | int index; |
| 4605 | |
| 4606 | index = HTTP_GetCustomHeaderIndex(req, szConnection, 0, FALSE); |
| 4607 | if(index != -1) |
| 4608 | req->netconn->keep_alive = !strcmpiW(req->custHeaders[index].lpszValue, szKeepAlive); |
| 4609 | else |
| 4610 | req->netconn->keep_alive = !strcmpiW(req->version, g_szHttp1_1); |
| 4611 | } |
| 4612 | |
Juan Lang | 666353d | 2011-03-02 10:06:37 -0800 | [diff] [blame] | 4613 | static void HTTP_CacheRequest(http_request_t *request) |
| 4614 | { |
| 4615 | WCHAR url[INTERNET_MAX_URL_LENGTH]; |
| 4616 | WCHAR cacheFileName[MAX_PATH+1]; |
| 4617 | BOOL b; |
| 4618 | |
| 4619 | b = HTTP_GetRequestURL(request, url); |
| 4620 | if(!b) { |
| 4621 | WARN("Could not get URL\n"); |
| 4622 | return; |
| 4623 | } |
| 4624 | |
Juan Lang | 7685dad | 2011-03-03 08:55:32 -0800 | [diff] [blame] | 4625 | b = CreateUrlCacheEntryW(url, request->contentLength, NULL, cacheFileName, 0); |
Juan Lang | 666353d | 2011-03-02 10:06:37 -0800 | [diff] [blame] | 4626 | if(b) { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 4627 | heap_free(request->cacheFile); |
Juan Lang | 666353d | 2011-03-02 10:06:37 -0800 | [diff] [blame] | 4628 | CloseHandle(request->hCacheFile); |
| 4629 | |
| 4630 | request->cacheFile = heap_strdupW(cacheFileName); |
| 4631 | request->hCacheFile = CreateFileW(request->cacheFile, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, |
| 4632 | NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 4633 | if(request->hCacheFile == INVALID_HANDLE_VALUE) { |
| 4634 | WARN("Could not create file: %u\n", GetLastError()); |
| 4635 | request->hCacheFile = NULL; |
| 4636 | } |
| 4637 | }else { |
| 4638 | WARN("Could not create cache entry: %08x\n", GetLastError()); |
| 4639 | } |
| 4640 | } |
| 4641 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 4642 | static DWORD open_http_connection(http_request_t *request, BOOL *reusing) |
| 4643 | { |
| 4644 | const BOOL is_https = (request->hdr.dwFlags & INTERNET_FLAG_SECURE) != 0; |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 4645 | netconn_t *netconn = NULL; |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 4646 | DWORD res; |
| 4647 | |
| 4648 | assert(!request->netconn); |
| 4649 | reset_data_stream(request); |
| 4650 | |
Jacek Caban | 3aeb8eb | 2012-05-25 16:34:24 +0200 | [diff] [blame] | 4651 | res = HTTP_ResolveName(request); |
| 4652 | if(res != ERROR_SUCCESS) |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 4653 | return res; |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 4654 | |
| 4655 | EnterCriticalSection(&connection_pool_cs); |
| 4656 | |
Jacek Caban | 3aeb8eb | 2012-05-25 16:34:24 +0200 | [diff] [blame] | 4657 | while(!list_empty(&request->server->conn_pool)) { |
| 4658 | netconn = LIST_ENTRY(list_head(&request->server->conn_pool), netconn_t, pool_entry); |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 4659 | list_remove(&netconn->pool_entry); |
| 4660 | |
| 4661 | if(NETCON_is_alive(netconn)) |
| 4662 | break; |
| 4663 | |
| 4664 | TRACE("connection %p closed during idle\n", netconn); |
| 4665 | free_netconn(netconn); |
| 4666 | netconn = NULL; |
| 4667 | } |
| 4668 | |
| 4669 | LeaveCriticalSection(&connection_pool_cs); |
| 4670 | |
| 4671 | if(netconn) { |
| 4672 | TRACE("<-- reusing %p netconn\n", netconn); |
| 4673 | request->netconn = netconn; |
| 4674 | *reusing = TRUE; |
| 4675 | return ERROR_SUCCESS; |
| 4676 | } |
| 4677 | |
| 4678 | INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, |
| 4679 | INTERNET_STATUS_CONNECTING_TO_SERVER, |
Jacek Caban | 3aeb8eb | 2012-05-25 16:34:24 +0200 | [diff] [blame] | 4680 | request->server->addr_str, |
| 4681 | strlen(request->server->addr_str)+1); |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 4682 | |
Jacek Caban | 868575a | 2012-05-25 16:51:20 +0200 | [diff] [blame] | 4683 | res = create_netconn(is_https, request->server, request->security_flags, |
| 4684 | (request->hdr.ErrorMask & INTERNET_ERROR_MASK_COMBINED_SEC_CERT) != 0, |
| 4685 | request->connect_timeout, &netconn); |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 4686 | if(res != ERROR_SUCCESS) { |
| 4687 | ERR("create_netconn failed: %u\n", res); |
| 4688 | return res; |
| 4689 | } |
| 4690 | |
| 4691 | request->netconn = netconn; |
| 4692 | |
| 4693 | INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, |
| 4694 | INTERNET_STATUS_CONNECTED_TO_SERVER, |
Jacek Caban | 3aeb8eb | 2012-05-25 16:34:24 +0200 | [diff] [blame] | 4695 | request->server->addr_str, strlen(request->server->addr_str)+1); |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 4696 | |
| 4697 | if(is_https) { |
| 4698 | /* Note: we differ from Microsoft's WinINet here. they seem to have |
| 4699 | * a bug that causes no status callbacks to be sent when starting |
| 4700 | * a tunnel to a proxy server using the CONNECT verb. i believe our |
| 4701 | * behaviour to be more correct and to not cause any incompatibilities |
| 4702 | * because using a secure connection through a proxy server is a rare |
| 4703 | * case that would be hard for anyone to depend on */ |
Jacek Caban | 3aeb8eb | 2012-05-25 16:34:24 +0200 | [diff] [blame] | 4704 | if(request->session->appInfo->proxy) |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 4705 | res = HTTP_SecureProxyConnect(request); |
| 4706 | if(res == ERROR_SUCCESS) |
Jacek Caban | 905ede6 | 2012-04-30 14:49:41 +0200 | [diff] [blame] | 4707 | res = NETCON_secure_connect(request->netconn); |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 4708 | } |
| 4709 | |
| 4710 | if(res != ERROR_SUCCESS) { |
| 4711 | http_release_netconn(request, FALSE); |
| 4712 | return res; |
| 4713 | } |
| 4714 | |
| 4715 | *reusing = FALSE; |
Jacek Caban | 3aeb8eb | 2012-05-25 16:34:24 +0200 | [diff] [blame] | 4716 | TRACE("Created connection to %s: %p\n", debugstr_w(request->server->name), netconn); |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 4717 | return ERROR_SUCCESS; |
| 4718 | } |
| 4719 | |
Mike McCormack | a496906 | 2004-07-04 00:24:47 +0000 | [diff] [blame] | 4720 | /*********************************************************************** |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 4721 | * HTTP_HttpSendRequestW (internal) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 4722 | * |
| 4723 | * Sends the specified request to the HTTP server |
| 4724 | * |
| 4725 | * RETURNS |
Juan Lang | b2ed9c5 | 2011-03-02 17:09:45 -0800 | [diff] [blame] | 4726 | * ERROR_SUCCESS on success |
| 4727 | * win32 error code on failure |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 4728 | * |
| 4729 | */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4730 | static DWORD HTTP_HttpSendRequestW(http_request_t *request, LPCWSTR lpszHeaders, |
Robert Shearman | f6252cf | 2005-11-30 12:31:22 +0100 | [diff] [blame] | 4731 | DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength, |
| 4732 | DWORD dwContentLength, BOOL bEndRequest) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 4733 | { |
| 4734 | INT cnt; |
Jacek Caban | c952e81 | 2009-12-03 14:48:54 +0100 | [diff] [blame] | 4735 | BOOL redirected = FALSE; |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 4736 | LPWSTR requestString = NULL; |
Aric Stewart | ff9b9d4 | 2002-06-21 23:59:49 +0000 | [diff] [blame] | 4737 | INT responseLen; |
Rob Shearman | 14fb418 | 2007-01-04 18:21:13 +0000 | [diff] [blame] | 4738 | BOOL loop_next; |
Hans Leidekker | b0912d1 | 2008-02-02 16:08:28 +0100 | [diff] [blame] | 4739 | static const WCHAR szPost[] = { 'P','O','S','T',0 }; |
Hans Leidekker | 64359c2 | 2007-10-28 16:32:46 +0100 | [diff] [blame] | 4740 | static const WCHAR szContentLength[] = |
| 4741 | { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0 }; |
| 4742 | WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \r\n */ + 20 /* int */ ]; |
Jacek Caban | 36cb1ef | 2009-11-30 00:14:02 +0100 | [diff] [blame] | 4743 | DWORD res; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 4744 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4745 | TRACE("--> %p\n", request); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 4746 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4747 | assert(request->hdr.htype == WH_HHTTPREQ); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 4748 | |
Jacek Caban | f979134 | 2008-02-13 13:34:05 +0100 | [diff] [blame] | 4749 | /* if the verb is NULL default to GET */ |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 4750 | if (!request->verb) |
| 4751 | request->verb = heap_strdupW(szGET); |
Jacek Caban | f979134 | 2008-02-13 13:34:05 +0100 | [diff] [blame] | 4752 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 4753 | if (dwContentLength || strcmpW(request->verb, szGET)) |
Hans Leidekker | b0912d1 | 2008-02-02 16:08:28 +0100 | [diff] [blame] | 4754 | { |
| 4755 | sprintfW(contentLengthStr, szContentLength, dwContentLength); |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4756 | HTTP_HttpAddRequestHeadersW(request, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_REPLACE); |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 4757 | request->bytesToWrite = dwContentLength; |
Hans Leidekker | b0912d1 | 2008-02-02 16:08:28 +0100 | [diff] [blame] | 4758 | } |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 4759 | if (request->session->appInfo->agent) |
Hans Leidekker | e2f690a | 2008-05-02 21:59:24 +0200 | [diff] [blame] | 4760 | { |
| 4761 | WCHAR *agent_header; |
| 4762 | static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0}; |
| 4763 | int len; |
| 4764 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 4765 | len = strlenW(request->session->appInfo->agent) + strlenW(user_agent); |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 4766 | agent_header = heap_alloc(len * sizeof(WCHAR)); |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 4767 | sprintfW(agent_header, user_agent, request->session->appInfo->agent); |
Hans Leidekker | e2f690a | 2008-05-02 21:59:24 +0200 | [diff] [blame] | 4768 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4769 | HTTP_HttpAddRequestHeadersW(request, agent_header, strlenW(agent_header), HTTP_ADDREQ_FLAG_ADD_IF_NEW); |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 4770 | heap_free(agent_header); |
Hans Leidekker | e2f690a | 2008-05-02 21:59:24 +0200 | [diff] [blame] | 4771 | } |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4772 | if (request->hdr.dwFlags & INTERNET_FLAG_PRAGMA_NOCACHE) |
Hans Leidekker | 34ff555 | 2008-06-23 20:58:25 +0200 | [diff] [blame] | 4773 | { |
| 4774 | static const WCHAR pragma_nocache[] = {'P','r','a','g','m','a',':',' ','n','o','-','c','a','c','h','e','\r','\n',0}; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4775 | HTTP_HttpAddRequestHeadersW(request, pragma_nocache, strlenW(pragma_nocache), HTTP_ADDREQ_FLAG_ADD_IF_NEW); |
Hans Leidekker | 34ff555 | 2008-06-23 20:58:25 +0200 | [diff] [blame] | 4776 | } |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 4777 | if ((request->hdr.dwFlags & INTERNET_FLAG_NO_CACHE_WRITE) && !strcmpW(request->verb, szPost)) |
Alexander Morozov | ad2f53d | 2008-07-04 14:00:29 +0400 | [diff] [blame] | 4778 | { |
| 4779 | static const WCHAR cache_control[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l',':', |
| 4780 | ' ','n','o','-','c','a','c','h','e','\r','\n',0}; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4781 | HTTP_HttpAddRequestHeadersW(request, cache_control, strlenW(cache_control), HTTP_ADDREQ_FLAG_ADD_IF_NEW); |
Alexander Morozov | ad2f53d | 2008-07-04 14:00:29 +0400 | [diff] [blame] | 4782 | } |
David Hammerton | 6226f3f | 2003-08-05 18:31:02 +0000 | [diff] [blame] | 4783 | |
Jacek Caban | dfa9f4b | 2012-01-24 17:31:38 +0100 | [diff] [blame] | 4784 | /* add the headers the caller supplied */ |
| 4785 | if( lpszHeaders && dwHeaderLength ) |
| 4786 | HTTP_HttpAddRequestHeadersW(request, lpszHeaders, dwHeaderLength, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE); |
| 4787 | |
David Hammerton | 852c7ae | 2003-06-20 23:26:56 +0000 | [diff] [blame] | 4788 | do |
Nikolas Zimmermann | 7659882 | 2001-10-04 18:12:41 +0000 | [diff] [blame] | 4789 | { |
Aric Stewart | be918f4 | 2005-11-21 15:17:55 +0000 | [diff] [blame] | 4790 | DWORD len; |
Piotr Caban | ee68473 | 2010-06-29 12:20:51 +0200 | [diff] [blame] | 4791 | BOOL reusing_connection; |
Mike McCormack | a496906 | 2004-07-04 00:24:47 +0000 | [diff] [blame] | 4792 | char *ascii_req; |
| 4793 | |
Rob Shearman | 14fb418 | 2007-01-04 18:21:13 +0000 | [diff] [blame] | 4794 | loop_next = FALSE; |
Jacek Caban | c400117 | 2012-07-02 17:15:04 +0200 | [diff] [blame] | 4795 | reusing_connection = request->netconn != NULL; |
Rob Shearman | 272954b | 2007-01-04 18:23:17 +0000 | [diff] [blame] | 4796 | |
Piotr Caban | 224af0d | 2010-05-20 02:35:38 +0200 | [diff] [blame] | 4797 | if(redirected) { |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 4798 | request->contentLength = ~0u; |
| 4799 | request->bytesToWrite = 0; |
Piotr Caban | 224af0d | 2010-05-20 02:35:38 +0200 | [diff] [blame] | 4800 | } |
Rob Shearman | 14fb418 | 2007-01-04 18:21:13 +0000 | [diff] [blame] | 4801 | |
Rob Shearman | 4319ec6 | 2006-12-07 00:53:27 +0000 | [diff] [blame] | 4802 | if (TRACE_ON(wininet)) |
| 4803 | { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4804 | LPHTTPHEADERW Host = HTTP_GetHeader(request, hostW); |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 4805 | TRACE("Going to url %s %s\n", debugstr_w(Host->lpszValue), debugstr_w(request->path)); |
Rob Shearman | 4319ec6 | 2006-12-07 00:53:27 +0000 | [diff] [blame] | 4806 | } |
Nikolas Zimmermann | 7659882 | 2001-10-04 18:12:41 +0000 | [diff] [blame] | 4807 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4808 | HTTP_FixURL(request); |
| 4809 | if (request->hdr.dwFlags & INTERNET_FLAG_KEEP_CONNECTION) |
Hans Leidekker | 656a035 | 2008-05-31 21:47:24 +0200 | [diff] [blame] | 4810 | { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4811 | HTTP_ProcessHeader(request, szConnection, szKeepAlive, HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE); |
Hans Leidekker | 656a035 | 2008-05-31 21:47:24 +0200 | [diff] [blame] | 4812 | } |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 4813 | HTTP_InsertAuthorization(request, request->authInfo, szAuthorization); |
| 4814 | HTTP_InsertAuthorization(request, request->proxyAuthInfo, szProxy_Authorization); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 4815 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4816 | if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES)) |
| 4817 | HTTP_InsertCookies(request); |
Hans Leidekker | 572b0ba | 2008-07-19 19:55:39 +0200 | [diff] [blame] | 4818 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 4819 | if (request->session->appInfo->proxy && request->session->appInfo->proxy[0]) |
Hans Leidekker | 0ffe9f5 | 2008-03-30 19:16:52 +0100 | [diff] [blame] | 4820 | { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4821 | WCHAR *url = HTTP_BuildProxyRequestUrl(request); |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 4822 | requestString = HTTP_BuildHeaderRequestString(request, request->verb, url, request->version); |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 4823 | heap_free(url); |
Hans Leidekker | 0ffe9f5 | 2008-03-30 19:16:52 +0100 | [diff] [blame] | 4824 | } |
| 4825 | else |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 4826 | requestString = HTTP_BuildHeaderRequestString(request, request->verb, request->path, request->version); |
Hans Leidekker | 0ffe9f5 | 2008-03-30 19:16:52 +0100 | [diff] [blame] | 4827 | |
Mike McCormack | a496906 | 2004-07-04 00:24:47 +0000 | [diff] [blame] | 4828 | |
| 4829 | TRACE("Request header -> %s\n", debugstr_w(requestString) ); |
Aric Stewart | ff9b9d4 | 2002-06-21 23:59:49 +0000 | [diff] [blame] | 4830 | |
Jacek Caban | c400117 | 2012-07-02 17:15:04 +0200 | [diff] [blame] | 4831 | if (!reusing_connection && (res = open_http_connection(request, &reusing_connection)) != ERROR_SUCCESS) |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 4832 | break; |
Aric Stewart | ff9b9d4 | 2002-06-21 23:59:49 +0000 | [diff] [blame] | 4833 | |
Mike McCormack | a496906 | 2004-07-04 00:24:47 +0000 | [diff] [blame] | 4834 | /* send the request as ASCII, tack on the optional data */ |
Hans Leidekker | 6bb143a | 2009-05-06 15:57:56 +0200 | [diff] [blame] | 4835 | if (!lpOptional || redirected) |
Mike McCormack | a496906 | 2004-07-04 00:24:47 +0000 | [diff] [blame] | 4836 | dwOptionalLength = 0; |
| 4837 | len = WideCharToMultiByte( CP_ACP, 0, requestString, -1, |
| 4838 | NULL, 0, NULL, NULL ); |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 4839 | ascii_req = heap_alloc(len + dwOptionalLength); |
Mike McCormack | a496906 | 2004-07-04 00:24:47 +0000 | [diff] [blame] | 4840 | WideCharToMultiByte( CP_ACP, 0, requestString, -1, |
| 4841 | ascii_req, len, NULL, NULL ); |
| 4842 | if( lpOptional ) |
Mike McCormack | f1d7b14 | 2004-07-21 19:36:34 +0000 | [diff] [blame] | 4843 | memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength ); |
| 4844 | len = (len + dwOptionalLength - 1); |
| 4845 | ascii_req[len] = 0; |
Aric Stewart | 44cbdf2 | 2005-10-19 18:28:35 +0000 | [diff] [blame] | 4846 | TRACE("full request -> %s\n", debugstr_a(ascii_req) ); |
Mike McCormack | f1d7b14 | 2004-07-21 19:36:34 +0000 | [diff] [blame] | 4847 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4848 | INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, |
Robert Shearman | de2666f | 2005-11-29 10:44:05 +0100 | [diff] [blame] | 4849 | INTERNET_STATUS_SENDING_REQUEST, NULL, 0); |
Mike McCormack | f1d7b14 | 2004-07-21 19:36:34 +0000 | [diff] [blame] | 4850 | |
Hans Leidekker | 6522393 | 2012-01-13 15:15:48 +0100 | [diff] [blame] | 4851 | NETCON_set_timeout( request->netconn, TRUE, request->send_timeout ); |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 4852 | res = NETCON_send(request->netconn, ascii_req, len, 0, &cnt); |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 4853 | heap_free( ascii_req ); |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 4854 | if(res != ERROR_SUCCESS) { |
| 4855 | TRACE("send failed: %u\n", res); |
| 4856 | if(!reusing_connection) |
| 4857 | break; |
| 4858 | http_release_netconn(request, FALSE); |
| 4859 | loop_next = TRUE; |
| 4860 | continue; |
| 4861 | } |
David Hammerton | 852c7ae | 2003-06-20 23:26:56 +0000 | [diff] [blame] | 4862 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 4863 | request->bytesWritten = dwOptionalLength; |
Hans Leidekker | 0fabf54 | 2009-04-08 15:21:28 +0200 | [diff] [blame] | 4864 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4865 | INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, |
Robert Shearman | de2666f | 2005-11-29 10:44:05 +0100 | [diff] [blame] | 4866 | INTERNET_STATUS_REQUEST_SENT, |
| 4867 | &len, sizeof(DWORD)); |
David Hammerton | 852c7ae | 2003-06-20 23:26:56 +0000 | [diff] [blame] | 4868 | |
Robert Shearman | f6252cf | 2005-11-30 12:31:22 +0100 | [diff] [blame] | 4869 | if (bEndRequest) |
| 4870 | { |
Rob Shearman | ac1b527 | 2007-01-04 18:21:49 +0000 | [diff] [blame] | 4871 | DWORD dwBufferSize; |
| 4872 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4873 | INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, |
Robert Shearman | f6252cf | 2005-11-30 12:31:22 +0100 | [diff] [blame] | 4874 | INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0); |
| 4875 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4876 | responseLen = HTTP_GetResponseHeaders(request, TRUE); |
Piotr Caban | ee68473 | 2010-06-29 12:20:51 +0200 | [diff] [blame] | 4877 | /* FIXME: We should know that connection is closed before sending |
| 4878 | * headers. Otherwise wrong callbacks are executed */ |
| 4879 | if(!responseLen && reusing_connection) { |
| 4880 | TRACE("Connection closed by server, reconnecting\n"); |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 4881 | http_release_netconn(request, FALSE); |
Piotr Caban | ee68473 | 2010-06-29 12:20:51 +0200 | [diff] [blame] | 4882 | loop_next = TRUE; |
| 4883 | continue; |
| 4884 | } |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 4885 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4886 | INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, |
Robert Shearman | f6252cf | 2005-11-30 12:31:22 +0100 | [diff] [blame] | 4887 | INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, |
| 4888 | sizeof(DWORD)); |
Rob Shearman | 4319ec6 | 2006-12-07 00:53:27 +0000 | [diff] [blame] | 4889 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 4890 | http_process_keep_alive(request); |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4891 | HTTP_ProcessCookies(request); |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 4892 | HTTP_ProcessExpires(request); |
Juan Lang | 28e9229 | 2011-03-04 11:43:54 -0800 | [diff] [blame] | 4893 | HTTP_ProcessLastModified(request); |
Rob Shearman | 4319ec6 | 2006-12-07 00:53:27 +0000 | [diff] [blame] | 4894 | |
Jacek Caban | 5240e40 | 2012-05-03 12:19:47 +0200 | [diff] [blame] | 4895 | res = set_content_length(request); |
Jacek Caban | a890e3a | 2011-05-13 13:48:15 +0200 | [diff] [blame] | 4896 | if(res != ERROR_SUCCESS) |
| 4897 | goto lend; |
| 4898 | if(!request->contentLength) |
| 4899 | http_release_netconn(request, TRUE); |
| 4900 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4901 | if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && responseLen) |
Rob Shearman | 4319ec6 | 2006-12-07 00:53:27 +0000 | [diff] [blame] | 4902 | { |
Alexandre Julliard | 9ee1b06 | 2009-05-04 20:49:15 +0200 | [diff] [blame] | 4903 | WCHAR *new_url, szNewLocation[INTERNET_MAX_URL_LENGTH]; |
Rob Shearman | 9efe083 | 2007-01-12 19:19:18 -0600 | [diff] [blame] | 4904 | dwBufferSize=sizeof(szNewLocation); |
Jacek Caban | 4cf3f78 | 2012-05-03 12:19:58 +0200 | [diff] [blame] | 4905 | switch(request->status_code) { |
| 4906 | case HTTP_STATUS_REDIRECT: |
| 4907 | case HTTP_STATUS_MOVED: |
| 4908 | case HTTP_STATUS_REDIRECT_KEEP_VERB: |
| 4909 | case HTTP_STATUS_REDIRECT_METHOD: |
| 4910 | if(HTTP_HttpQueryInfoW(request,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL) != ERROR_SUCCESS) |
| 4911 | break; |
| 4912 | |
Hans Leidekker | 14d4d19 | 2011-06-01 11:50:32 +0200 | [diff] [blame] | 4913 | if (strcmpW(request->verb, szGET) && strcmpW(request->verb, szHEAD) && |
Jacek Caban | 4cf3f78 | 2012-05-03 12:19:58 +0200 | [diff] [blame] | 4914 | request->status_code != HTTP_STATUS_REDIRECT_KEEP_VERB) |
Hans Leidekker | fa4c25d | 2009-07-22 11:56:33 +0200 | [diff] [blame] | 4915 | { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 4916 | heap_free(request->verb); |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 4917 | request->verb = heap_strdupW(szGET); |
Hans Leidekker | fa4c25d | 2009-07-22 11:56:33 +0200 | [diff] [blame] | 4918 | } |
Jacek Caban | c400117 | 2012-07-02 17:15:04 +0200 | [diff] [blame] | 4919 | http_release_netconn(request, drain_content(request, FALSE)); |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4920 | if ((new_url = HTTP_GetRedirectURL( request, szNewLocation ))) |
Rob Shearman | 4319ec6 | 2006-12-07 00:53:27 +0000 | [diff] [blame] | 4921 | { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4922 | INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REDIRECT, |
Alexandre Julliard | 9ee1b06 | 2009-05-04 20:49:15 +0200 | [diff] [blame] | 4923 | new_url, (strlenW(new_url) + 1) * sizeof(WCHAR)); |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4924 | res = HTTP_HandleRedirect(request, new_url); |
Jacek Caban | a9ecdc6 | 2009-12-03 14:49:56 +0100 | [diff] [blame] | 4925 | if (res == ERROR_SUCCESS) |
Alexandre Julliard | 9ee1b06 | 2009-05-04 20:49:15 +0200 | [diff] [blame] | 4926 | { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 4927 | heap_free(requestString); |
Alexandre Julliard | 9ee1b06 | 2009-05-04 20:49:15 +0200 | [diff] [blame] | 4928 | loop_next = TRUE; |
| 4929 | } |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 4930 | heap_free( new_url ); |
Rob Shearman | 4319ec6 | 2006-12-07 00:53:27 +0000 | [diff] [blame] | 4931 | } |
Hans Leidekker | 6bb143a | 2009-05-06 15:57:56 +0200 | [diff] [blame] | 4932 | redirected = TRUE; |
Rob Shearman | 4319ec6 | 2006-12-07 00:53:27 +0000 | [diff] [blame] | 4933 | } |
| 4934 | } |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4935 | if (!(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTH) && res == ERROR_SUCCESS) |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 4936 | { |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 4937 | WCHAR szAuthValue[2048]; |
| 4938 | dwBufferSize=2048; |
Jacek Caban | 4cf3f78 | 2012-05-03 12:19:58 +0200 | [diff] [blame] | 4939 | if (request->status_code == HTTP_STATUS_DENIED) |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 4940 | { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4941 | LPHTTPHEADERW Host = HTTP_GetHeader(request, hostW); |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 4942 | DWORD dwIndex = 0; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4943 | while (HTTP_HttpQueryInfoW(request,HTTP_QUERY_WWW_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex) == ERROR_SUCCESS) |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 4944 | { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4945 | if (HTTP_DoAuthorization(request, szAuthValue, |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 4946 | &request->authInfo, |
| 4947 | request->session->userName, |
| 4948 | request->session->password, |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 4949 | Host->lpszValue)) |
Rob Shearman | cb28969 | 2007-06-05 19:49:58 +0100 | [diff] [blame] | 4950 | { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 4951 | heap_free(requestString); |
Jacek Caban | c400117 | 2012-07-02 17:15:04 +0200 | [diff] [blame] | 4952 | if(!drain_content(request, TRUE)) { |
| 4953 | FIXME("Could not drain content\n"); |
| 4954 | http_release_netconn(request, FALSE); |
| 4955 | } |
Rob Shearman | cb28969 | 2007-06-05 19:49:58 +0100 | [diff] [blame] | 4956 | loop_next = TRUE; |
| 4957 | break; |
| 4958 | } |
| 4959 | } |
Piotr Caban | 98fb747 | 2010-07-17 14:08:11 +0200 | [diff] [blame] | 4960 | |
| 4961 | if(!loop_next) { |
| 4962 | TRACE("Cleaning wrong authorization data\n"); |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 4963 | destroy_authinfo(request->authInfo); |
| 4964 | request->authInfo = NULL; |
Piotr Caban | 98fb747 | 2010-07-17 14:08:11 +0200 | [diff] [blame] | 4965 | } |
Rob Shearman | cb28969 | 2007-06-05 19:49:58 +0100 | [diff] [blame] | 4966 | } |
Jacek Caban | 4cf3f78 | 2012-05-03 12:19:58 +0200 | [diff] [blame] | 4967 | if (request->status_code == HTTP_STATUS_PROXY_AUTH_REQ) |
Rob Shearman | cb28969 | 2007-06-05 19:49:58 +0100 | [diff] [blame] | 4968 | { |
| 4969 | DWORD dwIndex = 0; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4970 | while (HTTP_HttpQueryInfoW(request,HTTP_QUERY_PROXY_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex) == ERROR_SUCCESS) |
Rob Shearman | cb28969 | 2007-06-05 19:49:58 +0100 | [diff] [blame] | 4971 | { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 4972 | if (HTTP_DoAuthorization(request, szAuthValue, |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 4973 | &request->proxyAuthInfo, |
| 4974 | request->session->appInfo->proxyUsername, |
| 4975 | request->session->appInfo->proxyPassword, |
Aric Stewart | fc50893 | 2009-10-12 14:24:18 -0500 | [diff] [blame] | 4976 | NULL)) |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 4977 | { |
Jacek Caban | c400117 | 2012-07-02 17:15:04 +0200 | [diff] [blame] | 4978 | if(!drain_content(request, TRUE)) { |
| 4979 | FIXME("Could not drain content\n"); |
| 4980 | http_release_netconn(request, FALSE); |
| 4981 | } |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 4982 | loop_next = TRUE; |
| 4983 | break; |
| 4984 | } |
| 4985 | } |
Piotr Caban | 98fb747 | 2010-07-17 14:08:11 +0200 | [diff] [blame] | 4986 | |
| 4987 | if(!loop_next) { |
| 4988 | TRACE("Cleaning wrong proxy authorization data\n"); |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 4989 | destroy_authinfo(request->proxyAuthInfo); |
| 4990 | request->proxyAuthInfo = NULL; |
Piotr Caban | 98fb747 | 2010-07-17 14:08:11 +0200 | [diff] [blame] | 4991 | } |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 4992 | } |
| 4993 | } |
Robert Shearman | f6252cf | 2005-11-30 12:31:22 +0100 | [diff] [blame] | 4994 | } |
| 4995 | else |
Jacek Caban | c952e81 | 2009-12-03 14:48:54 +0100 | [diff] [blame] | 4996 | res = ERROR_SUCCESS; |
David Hammerton | 852c7ae | 2003-06-20 23:26:56 +0000 | [diff] [blame] | 4997 | } |
| 4998 | while (loop_next); |
Aric Stewart | ff9b9d4 | 2002-06-21 23:59:49 +0000 | [diff] [blame] | 4999 | |
Juan Lang | 666353d | 2011-03-02 10:06:37 -0800 | [diff] [blame] | 5000 | if(res == ERROR_SUCCESS) |
| 5001 | HTTP_CacheRequest(request); |
Jacek Caban | d7a49e8 | 2008-02-13 13:32:49 +0100 | [diff] [blame] | 5002 | |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5003 | lend: |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 5004 | heap_free(requestString); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5005 | |
Alberto Massari | bc8bd72 | 2002-12-06 23:20:31 +0000 | [diff] [blame] | 5006 | /* TODO: send notification for P3P header */ |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 5007 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 5008 | if (request->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC) |
Hans Leidekker | 3a711fb | 2009-04-08 15:22:26 +0200 | [diff] [blame] | 5009 | { |
Jacek Caban | 8e37ed5 | 2011-06-10 14:47:16 +0200 | [diff] [blame] | 5010 | if (res == ERROR_SUCCESS) { |
Andy Clayton | da11075 | 2011-11-23 08:45:44 -0600 | [diff] [blame] | 5011 | if(bEndRequest && request->contentLength && request->bytesWritten == request->bytesToWrite) |
Jacek Caban | 8e37ed5 | 2011-06-10 14:47:16 +0200 | [diff] [blame] | 5012 | HTTP_ReceiveRequestData(request, TRUE); |
| 5013 | else |
| 5014 | send_request_complete(request, |
| 5015 | request->session->hdr.dwInternalFlags & INET_OPENURL ? (DWORD_PTR)request->hdr.hInternet : 1, 0); |
| 5016 | }else { |
| 5017 | send_request_complete(request, 0, res); |
| 5018 | } |
Jacek Caban | 1293106 | 2009-01-13 00:28:25 +0100 | [diff] [blame] | 5019 | } |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5020 | |
| 5021 | TRACE("<--\n"); |
Jacek Caban | c952e81 | 2009-12-03 14:48:54 +0100 | [diff] [blame] | 5022 | return res; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5023 | } |
| 5024 | |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5025 | /*********************************************************************** |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5026 | * |
| 5027 | * Helper functions for the HttpSendRequest(Ex) functions |
| 5028 | * |
| 5029 | */ |
| 5030 | static void AsyncHttpSendRequestProc(WORKREQUEST *workRequest) |
| 5031 | { |
| 5032 | struct WORKREQ_HTTPSENDREQUESTW const *req = &workRequest->u.HttpSendRequestW; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5033 | http_request_t *request = (http_request_t*) workRequest->hdr; |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5034 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5035 | TRACE("%p\n", request); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5036 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5037 | HTTP_HttpSendRequestW(request, req->lpszHeader, |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5038 | req->dwHeaderLength, req->lpOptional, req->dwOptionalLength, |
| 5039 | req->dwContentLength, req->bEndRequest); |
| 5040 | |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 5041 | heap_free(req->lpszHeader); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5042 | } |
| 5043 | |
| 5044 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5045 | static DWORD HTTP_HttpEndRequestW(http_request_t *request, DWORD dwFlags, DWORD_PTR dwContext) |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5046 | { |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5047 | DWORD dwBufferSize; |
Jacek Caban | 797a17b | 2012-05-03 12:20:10 +0200 | [diff] [blame] | 5048 | INT responseLen; |
Jacek Caban | 741b661 | 2009-12-03 14:49:29 +0100 | [diff] [blame] | 5049 | DWORD res = ERROR_SUCCESS; |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5050 | |
Jacek Caban | 892d3cd | 2011-12-07 16:40:27 +0100 | [diff] [blame] | 5051 | if(!request->netconn) { |
| 5052 | WARN("Not connected\n"); |
| 5053 | send_request_complete(request, 0, ERROR_INTERNET_OPERATION_CANCELLED); |
| 5054 | return ERROR_INTERNET_OPERATION_CANCELLED; |
| 5055 | } |
| 5056 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5057 | INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5058 | INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0); |
| 5059 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5060 | responseLen = HTTP_GetResponseHeaders(request, TRUE); |
Jacek Caban | 741b661 | 2009-12-03 14:49:29 +0100 | [diff] [blame] | 5061 | if (!responseLen) |
| 5062 | res = ERROR_HTTP_HEADER_NOT_FOUND; |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5063 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5064 | INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5065 | INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD)); |
| 5066 | |
| 5067 | /* process cookies here. Is this right? */ |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 5068 | http_process_keep_alive(request); |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5069 | HTTP_ProcessCookies(request); |
Juan Lang | 0b5ea6f | 2011-03-03 11:20:11 -0800 | [diff] [blame] | 5070 | HTTP_ProcessExpires(request); |
Juan Lang | 28e9229 | 2011-03-04 11:43:54 -0800 | [diff] [blame] | 5071 | HTTP_ProcessLastModified(request); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5072 | |
Jacek Caban | 5240e40 | 2012-05-03 12:19:47 +0200 | [diff] [blame] | 5073 | if ((res = set_content_length(request)) == ERROR_SUCCESS) { |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 5074 | if(!request->contentLength) |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 5075 | http_release_netconn(request, TRUE); |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 5076 | } |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5077 | |
Jacek Caban | ccd11eb | 2011-04-02 15:20:33 +0200 | [diff] [blame] | 5078 | if (res == ERROR_SUCCESS && !(request->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT)) |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5079 | { |
Jacek Caban | 797a17b | 2012-05-03 12:20:10 +0200 | [diff] [blame] | 5080 | switch(request->status_code) { |
| 5081 | case HTTP_STATUS_REDIRECT: |
| 5082 | case HTTP_STATUS_MOVED: |
| 5083 | case HTTP_STATUS_REDIRECT_METHOD: |
| 5084 | case HTTP_STATUS_REDIRECT_KEEP_VERB: { |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5085 | WCHAR *new_url, szNewLocation[INTERNET_MAX_URL_LENGTH]; |
| 5086 | dwBufferSize=sizeof(szNewLocation); |
Jacek Caban | 797a17b | 2012-05-03 12:20:10 +0200 | [diff] [blame] | 5087 | if (HTTP_HttpQueryInfoW(request, HTTP_QUERY_LOCATION, szNewLocation, &dwBufferSize, NULL) != ERROR_SUCCESS) |
| 5088 | break; |
| 5089 | |
| 5090 | if (strcmpW(request->verb, szGET) && strcmpW(request->verb, szHEAD) && |
| 5091 | request->status_code != HTTP_STATUS_REDIRECT_KEEP_VERB) |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5092 | { |
Jacek Caban | 797a17b | 2012-05-03 12:20:10 +0200 | [diff] [blame] | 5093 | heap_free(request->verb); |
| 5094 | request->verb = heap_strdupW(szGET); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5095 | } |
Jacek Caban | c400117 | 2012-07-02 17:15:04 +0200 | [diff] [blame] | 5096 | http_release_netconn(request, drain_content(request, FALSE)); |
Jacek Caban | 797a17b | 2012-05-03 12:20:10 +0200 | [diff] [blame] | 5097 | if ((new_url = HTTP_GetRedirectURL( request, szNewLocation ))) |
| 5098 | { |
| 5099 | INTERNET_SendCallback(&request->hdr, request->hdr.dwContext, INTERNET_STATUS_REDIRECT, |
| 5100 | new_url, (strlenW(new_url) + 1) * sizeof(WCHAR)); |
| 5101 | res = HTTP_HandleRedirect(request, new_url); |
| 5102 | if (res == ERROR_SUCCESS) |
| 5103 | res = HTTP_HttpSendRequestW(request, NULL, 0, NULL, 0, 0, TRUE); |
| 5104 | heap_free( new_url ); |
| 5105 | } |
| 5106 | } |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5107 | } |
| 5108 | } |
| 5109 | |
Jacek Caban | c0293df | 2011-06-10 14:47:04 +0200 | [diff] [blame] | 5110 | if (res == ERROR_SUCCESS && request->contentLength) |
Jacek Caban | 685daf2 | 2011-03-17 01:01:49 +0100 | [diff] [blame] | 5111 | HTTP_ReceiveRequestData(request, TRUE); |
Jacek Caban | c0293df | 2011-06-10 14:47:04 +0200 | [diff] [blame] | 5112 | else |
Jacek Caban | 8e37ed5 | 2011-06-10 14:47:16 +0200 | [diff] [blame] | 5113 | send_request_complete(request, res == ERROR_SUCCESS, res); |
Jacek Caban | 685daf2 | 2011-03-17 01:01:49 +0100 | [diff] [blame] | 5114 | |
Jacek Caban | 741b661 | 2009-12-03 14:49:29 +0100 | [diff] [blame] | 5115 | return res; |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5116 | } |
| 5117 | |
| 5118 | /*********************************************************************** |
| 5119 | * HttpEndRequestA (WININET.@) |
| 5120 | * |
| 5121 | * Ends an HTTP request that was started by HttpSendRequestEx |
| 5122 | * |
| 5123 | * RETURNS |
| 5124 | * TRUE if successful |
| 5125 | * FALSE on failure |
| 5126 | * |
| 5127 | */ |
| 5128 | BOOL WINAPI HttpEndRequestA(HINTERNET hRequest, |
| 5129 | LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext) |
| 5130 | { |
| 5131 | TRACE("(%p, %p, %08x, %08lx)\n", hRequest, lpBuffersOut, dwFlags, dwContext); |
| 5132 | |
| 5133 | if (lpBuffersOut) |
| 5134 | { |
Jacek Caban | e1958a6 | 2009-12-21 13:58:18 +0100 | [diff] [blame] | 5135 | SetLastError(ERROR_INVALID_PARAMETER); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5136 | return FALSE; |
| 5137 | } |
| 5138 | |
| 5139 | return HttpEndRequestW(hRequest, NULL, dwFlags, dwContext); |
| 5140 | } |
| 5141 | |
| 5142 | static void AsyncHttpEndRequestProc(WORKREQUEST *work) |
| 5143 | { |
| 5144 | struct WORKREQ_HTTPENDREQUESTW const *req = &work->u.HttpEndRequestW; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5145 | http_request_t *request = (http_request_t*)work->hdr; |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5146 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5147 | TRACE("%p\n", request); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5148 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5149 | HTTP_HttpEndRequestW(request, req->dwFlags, req->dwContext); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5150 | } |
| 5151 | |
| 5152 | /*********************************************************************** |
| 5153 | * HttpEndRequestW (WININET.@) |
| 5154 | * |
| 5155 | * Ends an HTTP request that was started by HttpSendRequestEx |
| 5156 | * |
| 5157 | * RETURNS |
| 5158 | * TRUE if successful |
| 5159 | * FALSE on failure |
| 5160 | * |
| 5161 | */ |
| 5162 | BOOL WINAPI HttpEndRequestW(HINTERNET hRequest, |
| 5163 | LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext) |
| 5164 | { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5165 | http_request_t *request; |
Jacek Caban | 741b661 | 2009-12-03 14:49:29 +0100 | [diff] [blame] | 5166 | DWORD res; |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5167 | |
Jacek Caban | c0293df | 2011-06-10 14:47:04 +0200 | [diff] [blame] | 5168 | TRACE("%p %p %x %lx -->\n", hRequest, lpBuffersOut, dwFlags, dwContext); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5169 | |
| 5170 | if (lpBuffersOut) |
| 5171 | { |
Jacek Caban | 741b661 | 2009-12-03 14:49:29 +0100 | [diff] [blame] | 5172 | SetLastError(ERROR_INVALID_PARAMETER); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5173 | return FALSE; |
| 5174 | } |
| 5175 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5176 | request = (http_request_t*) get_handle_object( hRequest ); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5177 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5178 | if (NULL == request || request->hdr.htype != WH_HHTTPREQ) |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5179 | { |
Jacek Caban | 741b661 | 2009-12-03 14:49:29 +0100 | [diff] [blame] | 5180 | SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE); |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5181 | if (request) |
| 5182 | WININET_Release( &request->hdr ); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5183 | return FALSE; |
| 5184 | } |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5185 | request->hdr.dwFlags |= dwFlags; |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5186 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 5187 | if (request->session->appInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC) |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5188 | { |
| 5189 | WORKREQUEST work; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5190 | struct WORKREQ_HTTPENDREQUESTW *work_endrequest; |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5191 | |
| 5192 | work.asyncproc = AsyncHttpEndRequestProc; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5193 | work.hdr = WININET_AddRef( &request->hdr ); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5194 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5195 | work_endrequest = &work.u.HttpEndRequestW; |
| 5196 | work_endrequest->dwFlags = dwFlags; |
| 5197 | work_endrequest->dwContext = dwContext; |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5198 | |
| 5199 | INTERNET_AsyncCall(&work); |
Jacek Caban | 741b661 | 2009-12-03 14:49:29 +0100 | [diff] [blame] | 5200 | res = ERROR_IO_PENDING; |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5201 | } |
| 5202 | else |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5203 | res = HTTP_HttpEndRequestW(request, dwFlags, dwContext); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5204 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5205 | WININET_Release( &request->hdr ); |
Jacek Caban | 741b661 | 2009-12-03 14:49:29 +0100 | [diff] [blame] | 5206 | TRACE("%u <--\n", res); |
| 5207 | if(res != ERROR_SUCCESS) |
| 5208 | SetLastError(res); |
| 5209 | return res == ERROR_SUCCESS; |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5210 | } |
| 5211 | |
| 5212 | /*********************************************************************** |
| 5213 | * HttpSendRequestExA (WININET.@) |
| 5214 | * |
| 5215 | * Sends the specified request to the HTTP server and allows chunked |
| 5216 | * transfers. |
| 5217 | * |
| 5218 | * RETURNS |
| 5219 | * Success: TRUE |
| 5220 | * Failure: FALSE, call GetLastError() for more information. |
| 5221 | */ |
| 5222 | BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest, |
| 5223 | LPINTERNET_BUFFERSA lpBuffersIn, |
| 5224 | LPINTERNET_BUFFERSA lpBuffersOut, |
| 5225 | DWORD dwFlags, DWORD_PTR dwContext) |
| 5226 | { |
| 5227 | INTERNET_BUFFERSW BuffersInW; |
| 5228 | BOOL rc = FALSE; |
| 5229 | DWORD headerlen; |
| 5230 | LPWSTR header = NULL; |
| 5231 | |
| 5232 | TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn, |
| 5233 | lpBuffersOut, dwFlags, dwContext); |
| 5234 | |
| 5235 | if (lpBuffersIn) |
| 5236 | { |
| 5237 | BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW); |
| 5238 | if (lpBuffersIn->lpcszHeader) |
| 5239 | { |
| 5240 | headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader, |
| 5241 | lpBuffersIn->dwHeadersLength,0,0); |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 5242 | header = heap_alloc(headerlen*sizeof(WCHAR)); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5243 | if (!(BuffersInW.lpcszHeader = header)) |
| 5244 | { |
Jacek Caban | b73e31c | 2009-12-21 13:58:53 +0100 | [diff] [blame] | 5245 | SetLastError(ERROR_OUTOFMEMORY); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5246 | return FALSE; |
| 5247 | } |
| 5248 | BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0, |
| 5249 | lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength, |
| 5250 | header, headerlen); |
| 5251 | } |
| 5252 | else |
| 5253 | BuffersInW.lpcszHeader = NULL; |
| 5254 | BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal; |
| 5255 | BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer; |
| 5256 | BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength; |
| 5257 | BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal; |
| 5258 | BuffersInW.Next = NULL; |
| 5259 | } |
| 5260 | |
| 5261 | rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext); |
| 5262 | |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 5263 | heap_free(header); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5264 | return rc; |
| 5265 | } |
| 5266 | |
| 5267 | /*********************************************************************** |
| 5268 | * HttpSendRequestExW (WININET.@) |
| 5269 | * |
| 5270 | * Sends the specified request to the HTTP server and allows chunked |
| 5271 | * transfers |
| 5272 | * |
| 5273 | * RETURNS |
| 5274 | * Success: TRUE |
| 5275 | * Failure: FALSE, call GetLastError() for more information. |
| 5276 | */ |
| 5277 | BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest, |
| 5278 | LPINTERNET_BUFFERSW lpBuffersIn, |
| 5279 | LPINTERNET_BUFFERSW lpBuffersOut, |
| 5280 | DWORD dwFlags, DWORD_PTR dwContext) |
| 5281 | { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5282 | http_request_t *request; |
| 5283 | http_session_t *session; |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5284 | appinfo_t *hIC; |
Jacek Caban | c952e81 | 2009-12-03 14:48:54 +0100 | [diff] [blame] | 5285 | DWORD res; |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5286 | |
| 5287 | TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn, |
| 5288 | lpBuffersOut, dwFlags, dwContext); |
| 5289 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5290 | request = (http_request_t*) get_handle_object( hRequest ); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5291 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5292 | if (NULL == request || request->hdr.htype != WH_HHTTPREQ) |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5293 | { |
Jacek Caban | c952e81 | 2009-12-03 14:48:54 +0100 | [diff] [blame] | 5294 | res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE; |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5295 | goto lend; |
| 5296 | } |
| 5297 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 5298 | session = request->session; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5299 | assert(session->hdr.htype == WH_HHTTPSESSION); |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 5300 | hIC = session->appInfo; |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5301 | assert(hIC->hdr.htype == WH_HINIT); |
| 5302 | |
| 5303 | if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) |
| 5304 | { |
| 5305 | WORKREQUEST workRequest; |
| 5306 | struct WORKREQ_HTTPSENDREQUESTW *req; |
| 5307 | |
| 5308 | workRequest.asyncproc = AsyncHttpSendRequestProc; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5309 | workRequest.hdr = WININET_AddRef( &request->hdr ); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5310 | req = &workRequest.u.HttpSendRequestW; |
| 5311 | if (lpBuffersIn) |
| 5312 | { |
| 5313 | DWORD size = 0; |
| 5314 | |
| 5315 | if (lpBuffersIn->lpcszHeader) |
| 5316 | { |
| 5317 | if (lpBuffersIn->dwHeadersLength == ~0u) |
| 5318 | size = (strlenW( lpBuffersIn->lpcszHeader ) + 1) * sizeof(WCHAR); |
| 5319 | else |
| 5320 | size = lpBuffersIn->dwHeadersLength * sizeof(WCHAR); |
| 5321 | |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 5322 | req->lpszHeader = heap_alloc(size); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5323 | memcpy( req->lpszHeader, lpBuffersIn->lpcszHeader, size ); |
| 5324 | } |
| 5325 | else req->lpszHeader = NULL; |
| 5326 | |
| 5327 | req->dwHeaderLength = size / sizeof(WCHAR); |
| 5328 | req->lpOptional = lpBuffersIn->lpvBuffer; |
| 5329 | req->dwOptionalLength = lpBuffersIn->dwBufferLength; |
| 5330 | req->dwContentLength = lpBuffersIn->dwBufferTotal; |
| 5331 | } |
| 5332 | else |
| 5333 | { |
| 5334 | req->lpszHeader = NULL; |
| 5335 | req->dwHeaderLength = 0; |
| 5336 | req->lpOptional = NULL; |
| 5337 | req->dwOptionalLength = 0; |
| 5338 | req->dwContentLength = 0; |
| 5339 | } |
| 5340 | |
| 5341 | req->bEndRequest = FALSE; |
| 5342 | |
| 5343 | INTERNET_AsyncCall(&workRequest); |
| 5344 | /* |
| 5345 | * This is from windows. |
| 5346 | */ |
Jacek Caban | c952e81 | 2009-12-03 14:48:54 +0100 | [diff] [blame] | 5347 | res = ERROR_IO_PENDING; |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5348 | } |
| 5349 | else |
| 5350 | { |
| 5351 | if (lpBuffersIn) |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5352 | res = HTTP_HttpSendRequestW(request, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength, |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5353 | lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength, |
| 5354 | lpBuffersIn->dwBufferTotal, FALSE); |
| 5355 | else |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5356 | res = HTTP_HttpSendRequestW(request, NULL, 0, NULL, 0, 0, FALSE); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5357 | } |
| 5358 | |
| 5359 | lend: |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5360 | if ( request ) |
| 5361 | WININET_Release( &request->hdr ); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5362 | |
| 5363 | TRACE("<---\n"); |
Hans Leidekker | 68453a5 | 2009-12-21 11:13:44 +0100 | [diff] [blame] | 5364 | SetLastError(res); |
Jacek Caban | c952e81 | 2009-12-03 14:48:54 +0100 | [diff] [blame] | 5365 | return res == ERROR_SUCCESS; |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5366 | } |
| 5367 | |
| 5368 | /*********************************************************************** |
| 5369 | * HttpSendRequestW (WININET.@) |
| 5370 | * |
| 5371 | * Sends the specified request to the HTTP server |
| 5372 | * |
| 5373 | * RETURNS |
| 5374 | * TRUE on success |
| 5375 | * FALSE on failure |
| 5376 | * |
| 5377 | */ |
| 5378 | BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders, |
| 5379 | DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength) |
| 5380 | { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5381 | http_request_t *request; |
| 5382 | http_session_t *session = NULL; |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5383 | appinfo_t *hIC = NULL; |
| 5384 | DWORD res = ERROR_SUCCESS; |
| 5385 | |
| 5386 | TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest, |
| 5387 | debugstr_wn(lpszHeaders, dwHeaderLength), dwHeaderLength, lpOptional, dwOptionalLength); |
| 5388 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5389 | request = (http_request_t*) get_handle_object( hHttpRequest ); |
| 5390 | if (NULL == request || request->hdr.htype != WH_HHTTPREQ) |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5391 | { |
| 5392 | res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE; |
| 5393 | goto lend; |
| 5394 | } |
| 5395 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 5396 | session = request->session; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5397 | if (NULL == session || session->hdr.htype != WH_HHTTPSESSION) |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5398 | { |
| 5399 | res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE; |
| 5400 | goto lend; |
| 5401 | } |
| 5402 | |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 5403 | hIC = session->appInfo; |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5404 | if (NULL == hIC || hIC->hdr.htype != WH_HINIT) |
| 5405 | { |
| 5406 | res = ERROR_INTERNET_INCORRECT_HANDLE_TYPE; |
| 5407 | goto lend; |
| 5408 | } |
| 5409 | |
| 5410 | if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC) |
| 5411 | { |
| 5412 | WORKREQUEST workRequest; |
| 5413 | struct WORKREQ_HTTPSENDREQUESTW *req; |
| 5414 | |
| 5415 | workRequest.asyncproc = AsyncHttpSendRequestProc; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5416 | workRequest.hdr = WININET_AddRef( &request->hdr ); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5417 | req = &workRequest.u.HttpSendRequestW; |
| 5418 | if (lpszHeaders) |
| 5419 | { |
| 5420 | DWORD size; |
| 5421 | |
| 5422 | if (dwHeaderLength == ~0u) size = (strlenW(lpszHeaders) + 1) * sizeof(WCHAR); |
| 5423 | else size = dwHeaderLength * sizeof(WCHAR); |
| 5424 | |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 5425 | req->lpszHeader = heap_alloc(size); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5426 | memcpy(req->lpszHeader, lpszHeaders, size); |
| 5427 | } |
| 5428 | else |
| 5429 | req->lpszHeader = 0; |
| 5430 | req->dwHeaderLength = dwHeaderLength; |
| 5431 | req->lpOptional = lpOptional; |
| 5432 | req->dwOptionalLength = dwOptionalLength; |
| 5433 | req->dwContentLength = dwOptionalLength; |
| 5434 | req->bEndRequest = TRUE; |
| 5435 | |
| 5436 | INTERNET_AsyncCall(&workRequest); |
| 5437 | /* |
| 5438 | * This is from windows. |
| 5439 | */ |
| 5440 | res = ERROR_IO_PENDING; |
| 5441 | } |
| 5442 | else |
| 5443 | { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5444 | res = HTTP_HttpSendRequestW(request, lpszHeaders, |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5445 | dwHeaderLength, lpOptional, dwOptionalLength, |
| 5446 | dwOptionalLength, TRUE); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5447 | } |
| 5448 | lend: |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5449 | if( request ) |
| 5450 | WININET_Release( &request->hdr ); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5451 | |
Hans Leidekker | 68453a5 | 2009-12-21 11:13:44 +0100 | [diff] [blame] | 5452 | SetLastError(res); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5453 | return res == ERROR_SUCCESS; |
| 5454 | } |
| 5455 | |
| 5456 | /*********************************************************************** |
| 5457 | * HttpSendRequestA (WININET.@) |
| 5458 | * |
| 5459 | * Sends the specified request to the HTTP server |
| 5460 | * |
| 5461 | * RETURNS |
| 5462 | * TRUE on success |
| 5463 | * FALSE on failure |
| 5464 | * |
| 5465 | */ |
| 5466 | BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders, |
| 5467 | DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength) |
| 5468 | { |
| 5469 | BOOL result; |
| 5470 | LPWSTR szHeaders=NULL; |
| 5471 | DWORD nLen=dwHeaderLength; |
| 5472 | if(lpszHeaders!=NULL) |
| 5473 | { |
| 5474 | nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0); |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 5475 | szHeaders = heap_alloc(nLen*sizeof(WCHAR)); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5476 | MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen); |
| 5477 | } |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 5478 | result = HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength); |
| 5479 | heap_free(szHeaders); |
Jacek Caban | 34fcbb5 | 2009-11-30 20:01:17 +0100 | [diff] [blame] | 5480 | return result; |
| 5481 | } |
| 5482 | |
| 5483 | /*********************************************************************** |
Jacek Caban | 5a535d6 | 2008-02-26 20:20:41 +0100 | [diff] [blame] | 5484 | * HTTPSESSION_Destroy (internal) |
| 5485 | * |
| 5486 | * Deallocate session handle |
| 5487 | * |
| 5488 | */ |
Jacek Caban | 44d633a | 2009-07-07 21:46:09 +0200 | [diff] [blame] | 5489 | static void HTTPSESSION_Destroy(object_header_t *hdr) |
Jacek Caban | 5a535d6 | 2008-02-26 20:20:41 +0100 | [diff] [blame] | 5490 | { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5491 | http_session_t *session = (http_session_t*) hdr; |
Jacek Caban | 5a535d6 | 2008-02-26 20:20:41 +0100 | [diff] [blame] | 5492 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5493 | TRACE("%p\n", session); |
Jacek Caban | 5a535d6 | 2008-02-26 20:20:41 +0100 | [diff] [blame] | 5494 | |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 5495 | WININET_Release(&session->appInfo->hdr); |
Jacek Caban | 5a535d6 | 2008-02-26 20:20:41 +0100 | [diff] [blame] | 5496 | |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 5497 | heap_free(session->hostName); |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 5498 | heap_free(session->password); |
| 5499 | heap_free(session->userName); |
Jacek Caban | 5a535d6 | 2008-02-26 20:20:41 +0100 | [diff] [blame] | 5500 | } |
| 5501 | |
Jacek Caban | 44d633a | 2009-07-07 21:46:09 +0200 | [diff] [blame] | 5502 | static DWORD HTTPSESSION_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode) |
Jacek Caban | e2933c2 | 2008-03-12 02:23:20 +0100 | [diff] [blame] | 5503 | { |
Hans Leidekker | 848cd8a | 2012-01-13 15:15:20 +0100 | [diff] [blame] | 5504 | http_session_t *ses = (http_session_t *)hdr; |
| 5505 | |
Jacek Caban | e2933c2 | 2008-03-12 02:23:20 +0100 | [diff] [blame] | 5506 | switch(option) { |
| 5507 | case INTERNET_OPTION_HANDLE_TYPE: |
| 5508 | TRACE("INTERNET_OPTION_HANDLE_TYPE\n"); |
| 5509 | |
| 5510 | if (*size < sizeof(ULONG)) |
| 5511 | return ERROR_INSUFFICIENT_BUFFER; |
| 5512 | |
| 5513 | *size = sizeof(DWORD); |
| 5514 | *(DWORD*)buffer = INTERNET_HANDLE_TYPE_CONNECT_HTTP; |
| 5515 | return ERROR_SUCCESS; |
Hans Leidekker | 848cd8a | 2012-01-13 15:15:20 +0100 | [diff] [blame] | 5516 | case INTERNET_OPTION_CONNECT_TIMEOUT: |
| 5517 | TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n"); |
| 5518 | |
| 5519 | if (*size < sizeof(DWORD)) |
| 5520 | return ERROR_INSUFFICIENT_BUFFER; |
| 5521 | |
| 5522 | *size = sizeof(DWORD); |
| 5523 | *(DWORD *)buffer = ses->connect_timeout; |
| 5524 | return ERROR_SUCCESS; |
Hans Leidekker | 6522393 | 2012-01-13 15:15:48 +0100 | [diff] [blame] | 5525 | |
| 5526 | case INTERNET_OPTION_SEND_TIMEOUT: |
| 5527 | TRACE("INTERNET_OPTION_SEND_TIMEOUT\n"); |
| 5528 | |
| 5529 | if (*size < sizeof(DWORD)) |
| 5530 | return ERROR_INSUFFICIENT_BUFFER; |
| 5531 | |
| 5532 | *size = sizeof(DWORD); |
| 5533 | *(DWORD *)buffer = ses->send_timeout; |
| 5534 | return ERROR_SUCCESS; |
| 5535 | |
| 5536 | case INTERNET_OPTION_RECEIVE_TIMEOUT: |
| 5537 | TRACE("INTERNET_OPTION_RECEIVE_TIMEOUT\n"); |
| 5538 | |
| 5539 | if (*size < sizeof(DWORD)) |
| 5540 | return ERROR_INSUFFICIENT_BUFFER; |
| 5541 | |
| 5542 | *size = sizeof(DWORD); |
| 5543 | *(DWORD *)buffer = ses->receive_timeout; |
| 5544 | return ERROR_SUCCESS; |
Jacek Caban | e2933c2 | 2008-03-12 02:23:20 +0100 | [diff] [blame] | 5545 | } |
| 5546 | |
Hans Leidekker | 80dd367 | 2010-05-25 12:19:32 +0200 | [diff] [blame] | 5547 | return INET_QueryOption(hdr, option, buffer, size, unicode); |
Jacek Caban | e2933c2 | 2008-03-12 02:23:20 +0100 | [diff] [blame] | 5548 | } |
Jacek Caban | 5a535d6 | 2008-02-26 20:20:41 +0100 | [diff] [blame] | 5549 | |
Jacek Caban | 44d633a | 2009-07-07 21:46:09 +0200 | [diff] [blame] | 5550 | static DWORD HTTPSESSION_SetOption(object_header_t *hdr, DWORD option, void *buffer, DWORD size) |
Hans Leidekker | 8c20124 | 2008-09-24 16:54:37 +0200 | [diff] [blame] | 5551 | { |
Jacek Caban | e9f4a40 | 2009-07-13 01:41:18 +0200 | [diff] [blame] | 5552 | http_session_t *ses = (http_session_t*)hdr; |
Hans Leidekker | 8c20124 | 2008-09-24 16:54:37 +0200 | [diff] [blame] | 5553 | |
| 5554 | switch(option) { |
| 5555 | case INTERNET_OPTION_USERNAME: |
| 5556 | { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 5557 | heap_free(ses->userName); |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 5558 | if (!(ses->userName = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY; |
Hans Leidekker | 8c20124 | 2008-09-24 16:54:37 +0200 | [diff] [blame] | 5559 | return ERROR_SUCCESS; |
| 5560 | } |
| 5561 | case INTERNET_OPTION_PASSWORD: |
| 5562 | { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 5563 | heap_free(ses->password); |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 5564 | if (!(ses->password = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY; |
Hans Leidekker | 8c20124 | 2008-09-24 16:54:37 +0200 | [diff] [blame] | 5565 | return ERROR_SUCCESS; |
| 5566 | } |
Hans Leidekker | 848cd8a | 2012-01-13 15:15:20 +0100 | [diff] [blame] | 5567 | case INTERNET_OPTION_CONNECT_TIMEOUT: |
| 5568 | { |
Hans Leidekker | 6522393 | 2012-01-13 15:15:48 +0100 | [diff] [blame] | 5569 | if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER; |
Hans Leidekker | 848cd8a | 2012-01-13 15:15:20 +0100 | [diff] [blame] | 5570 | ses->connect_timeout = *(DWORD *)buffer; |
| 5571 | return ERROR_SUCCESS; |
| 5572 | } |
Hans Leidekker | 6522393 | 2012-01-13 15:15:48 +0100 | [diff] [blame] | 5573 | case INTERNET_OPTION_SEND_TIMEOUT: |
| 5574 | { |
| 5575 | if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER; |
| 5576 | ses->send_timeout = *(DWORD *)buffer; |
| 5577 | return ERROR_SUCCESS; |
| 5578 | } |
| 5579 | case INTERNET_OPTION_RECEIVE_TIMEOUT: |
| 5580 | { |
| 5581 | if (!buffer || size != sizeof(DWORD)) return ERROR_INVALID_PARAMETER; |
| 5582 | ses->receive_timeout = *(DWORD *)buffer; |
| 5583 | return ERROR_SUCCESS; |
| 5584 | } |
Hans Leidekker | 8c20124 | 2008-09-24 16:54:37 +0200 | [diff] [blame] | 5585 | default: break; |
| 5586 | } |
| 5587 | |
Jacek Caban | 4863257 | 2012-03-08 12:19:14 +0100 | [diff] [blame] | 5588 | return INET_SetOption(hdr, option, buffer, size); |
Hans Leidekker | 8c20124 | 2008-09-24 16:54:37 +0200 | [diff] [blame] | 5589 | } |
| 5590 | |
Jacek Caban | 44d633a | 2009-07-07 21:46:09 +0200 | [diff] [blame] | 5591 | static const object_vtbl_t HTTPSESSIONVtbl = { |
Jacek Caban | 7dc9bf6 | 2008-02-26 20:21:00 +0100 | [diff] [blame] | 5592 | HTTPSESSION_Destroy, |
Jacek Caban | 1ffcfbc | 2008-02-26 20:21:34 +0100 | [diff] [blame] | 5593 | NULL, |
Jacek Caban | e2933c2 | 2008-03-12 02:23:20 +0100 | [diff] [blame] | 5594 | HTTPSESSION_QueryOption, |
Hans Leidekker | 8c20124 | 2008-09-24 16:54:37 +0200 | [diff] [blame] | 5595 | HTTPSESSION_SetOption, |
Jacek Caban | 8c45eec | 2008-02-27 18:55:09 +0100 | [diff] [blame] | 5596 | NULL, |
Jacek Caban | 3314184 | 2008-02-29 12:57:57 +0100 | [diff] [blame] | 5597 | NULL, |
Jacek Caban | 3b4ca69 | 2008-03-02 19:35:11 +0100 | [diff] [blame] | 5598 | NULL, |
Jacek Caban | d597fd1 | 2008-03-03 18:07:20 +0100 | [diff] [blame] | 5599 | NULL, |
Jacek Caban | 7dc9bf6 | 2008-02-26 20:21:00 +0100 | [diff] [blame] | 5600 | NULL |
Jacek Caban | 5a535d6 | 2008-02-26 20:20:41 +0100 | [diff] [blame] | 5601 | }; |
| 5602 | |
| 5603 | |
| 5604 | /*********************************************************************** |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5605 | * HTTP_Connect (internal) |
| 5606 | * |
| 5607 | * Create http session handle |
| 5608 | * |
| 5609 | * RETURNS |
| 5610 | * HINTERNET a session handle on success |
| 5611 | * NULL on failure |
| 5612 | * |
| 5613 | */ |
Jacek Caban | b73e31c | 2009-12-21 13:58:53 +0100 | [diff] [blame] | 5614 | DWORD HTTP_Connect(appinfo_t *hIC, LPCWSTR lpszServerName, |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 5615 | INTERNET_PORT serverPort, LPCWSTR lpszUserName, |
Jacek Caban | b73e31c | 2009-12-21 13:58:53 +0100 | [diff] [blame] | 5616 | LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext, |
| 5617 | DWORD dwInternalFlags, HINTERNET *ret) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5618 | { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5619 | http_session_t *session = NULL; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5620 | |
Aric Stewart | ff9b9d4 | 2002-06-21 23:59:49 +0000 | [diff] [blame] | 5621 | TRACE("-->\n"); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5622 | |
Rob Shearman | 59ab0cf | 2008-01-31 14:47:25 +0000 | [diff] [blame] | 5623 | if (!lpszServerName || !lpszServerName[0]) |
Jacek Caban | b73e31c | 2009-12-21 13:58:53 +0100 | [diff] [blame] | 5624 | return ERROR_INVALID_PARAMETER; |
Rob Shearman | 59ab0cf | 2008-01-31 14:47:25 +0000 | [diff] [blame] | 5625 | |
Mike McCormack | 3a1391b | 2004-07-19 21:49:39 +0000 | [diff] [blame] | 5626 | assert( hIC->hdr.htype == WH_HINIT ); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5627 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5628 | session = alloc_object(&hIC->hdr, &HTTPSESSIONVtbl, sizeof(http_session_t)); |
| 5629 | if (!session) |
Jacek Caban | b73e31c | 2009-12-21 13:58:53 +0100 | [diff] [blame] | 5630 | return ERROR_OUTOFMEMORY; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5631 | |
Aric Stewart | ff9b9d4 | 2002-06-21 23:59:49 +0000 | [diff] [blame] | 5632 | /* |
| 5633 | * According to my tests. The name is not resolved until a request is sent |
| 5634 | */ |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5635 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5636 | session->hdr.htype = WH_HHTTPSESSION; |
| 5637 | session->hdr.dwFlags = dwFlags; |
| 5638 | session->hdr.dwContext = dwContext; |
| 5639 | session->hdr.dwInternalFlags |= dwInternalFlags; |
Mike McCormack | 3a1391b | 2004-07-19 21:49:39 +0000 | [diff] [blame] | 5640 | |
Jacek Caban | c250617 | 2006-10-29 18:48:48 +0100 | [diff] [blame] | 5641 | WININET_AddRef( &hIC->hdr ); |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 5642 | session->appInfo = hIC; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5643 | list_add_head( &hIC->hdr.children, &session->hdr.entry ); |
Jacek Caban | c250617 | 2006-10-29 18:48:48 +0100 | [diff] [blame] | 5644 | |
Juan Lang | 7243156 | 2011-03-01 11:00:49 -0800 | [diff] [blame] | 5645 | if(hIC->proxy && hIC->accessType == INTERNET_OPEN_TYPE_PROXY) { |
| 5646 | if(hIC->proxyBypass) |
Dominik Strasser | 94c02fe | 2003-04-14 21:32:36 +0000 | [diff] [blame] | 5647 | FIXME("Proxy bypass is ignored.\n"); |
| 5648 | } |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 5649 | session->hostName = heap_strdupW(lpszServerName); |
Robert Shearman | ef20936 | 2006-03-10 12:28:52 +0000 | [diff] [blame] | 5650 | if (lpszUserName && lpszUserName[0]) |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 5651 | session->userName = heap_strdupW(lpszUserName); |
Rob Shearman | 4b50768 | 2007-05-21 14:26:26 +0100 | [diff] [blame] | 5652 | if (lpszPassword && lpszPassword[0]) |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 5653 | session->password = heap_strdupW(lpszPassword); |
Juan Lang | 8e05039 | 2011-03-01 11:02:14 -0800 | [diff] [blame] | 5654 | session->hostPort = serverPort; |
Piotr Caban | 0b42100 | 2012-05-14 13:49:29 +0200 | [diff] [blame] | 5655 | session->connect_timeout = hIC->connect_timeout; |
Hans Leidekker | 6522393 | 2012-01-13 15:15:48 +0100 | [diff] [blame] | 5656 | session->send_timeout = INFINITE; |
| 5657 | session->receive_timeout = INFINITE; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5658 | |
Kevin Koltzau | 917df92 | 2004-05-13 05:17:25 +0000 | [diff] [blame] | 5659 | /* Don't send a handle created callback if this handle was created with InternetOpenUrl */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5660 | if (!(session->hdr.dwInternalFlags & INET_OPENURL)) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5661 | { |
Robert Shearman | de2666f | 2005-11-29 10:44:05 +0100 | [diff] [blame] | 5662 | INTERNET_SendCallback(&hIC->hdr, dwContext, |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5663 | INTERNET_STATUS_HANDLE_CREATED, &session->hdr.hInternet, |
Jacek Caban | a073c66 | 2011-02-02 22:51:13 +0100 | [diff] [blame] | 5664 | sizeof(HINTERNET)); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5665 | } |
| 5666 | |
Aric Stewart | ff9b9d4 | 2002-06-21 23:59:49 +0000 | [diff] [blame] | 5667 | /* |
Francois Gouget | 93416cd | 2005-03-23 13:15:18 +0000 | [diff] [blame] | 5668 | * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on |
Aric Stewart | ff9b9d4 | 2002-06-21 23:59:49 +0000 | [diff] [blame] | 5669 | * windows |
| 5670 | */ |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 5671 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5672 | TRACE("%p --> %p\n", hIC, session); |
Jacek Caban | b73e31c | 2009-12-21 13:58:53 +0100 | [diff] [blame] | 5673 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5674 | *ret = session->hdr.hInternet; |
Jacek Caban | a073c66 | 2011-02-02 22:51:13 +0100 | [diff] [blame] | 5675 | return ERROR_SUCCESS; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5676 | } |
| 5677 | |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5678 | /*********************************************************************** |
Alexandre Julliard | 48243e3 | 2004-07-15 18:57:32 +0000 | [diff] [blame] | 5679 | * HTTP_clear_response_headers (internal) |
| 5680 | * |
| 5681 | * clear out any old response headers |
| 5682 | */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5683 | static void HTTP_clear_response_headers( http_request_t *request ) |
Alexandre Julliard | 48243e3 | 2004-07-15 18:57:32 +0000 | [diff] [blame] | 5684 | { |
| 5685 | DWORD i; |
| 5686 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5687 | for( i=0; i<request->nCustHeaders; i++) |
Alexandre Julliard | 48243e3 | 2004-07-15 18:57:32 +0000 | [diff] [blame] | 5688 | { |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 5689 | if( !request->custHeaders[i].lpszField ) |
Alexandre Julliard | 48243e3 | 2004-07-15 18:57:32 +0000 | [diff] [blame] | 5690 | continue; |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 5691 | if( !request->custHeaders[i].lpszValue ) |
Alexandre Julliard | 48243e3 | 2004-07-15 18:57:32 +0000 | [diff] [blame] | 5692 | continue; |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 5693 | if ( request->custHeaders[i].wFlags & HDR_ISREQUEST ) |
Alexandre Julliard | 48243e3 | 2004-07-15 18:57:32 +0000 | [diff] [blame] | 5694 | continue; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5695 | HTTP_DeleteCustomHeader( request, i ); |
Robert Shearman | 7707a76 | 2005-03-10 11:14:24 +0000 | [diff] [blame] | 5696 | i--; |
Alexandre Julliard | 48243e3 | 2004-07-15 18:57:32 +0000 | [diff] [blame] | 5697 | } |
| 5698 | } |
| 5699 | |
| 5700 | /*********************************************************************** |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5701 | * HTTP_GetResponseHeaders (internal) |
| 5702 | * |
| 5703 | * Read server response |
| 5704 | * |
| 5705 | * RETURNS |
| 5706 | * |
| 5707 | * TRUE on success |
| 5708 | * FALSE on error |
| 5709 | */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5710 | static INT HTTP_GetResponseHeaders(http_request_t *request, BOOL clear) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5711 | { |
| 5712 | INT cbreaks = 0; |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 5713 | WCHAR buffer[MAX_REPLY_LEN]; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5714 | DWORD buflen = MAX_REPLY_LEN; |
| 5715 | BOOL bSuccess = FALSE; |
Aric Stewart | ff9b9d4 | 2002-06-21 23:59:49 +0000 | [diff] [blame] | 5716 | INT rc = 0; |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 5717 | char bufferA[MAX_REPLY_LEN]; |
Aric Stewart | b9f2f9d | 2009-06-23 14:30:38 +0900 | [diff] [blame] | 5718 | LPWSTR status_code = NULL, status_text = NULL; |
Mike McCormack | 13b6ce6d | 2004-08-09 18:54:23 +0000 | [diff] [blame] | 5719 | DWORD cchMaxRawHeaders = 1024; |
Hans Leidekker | 911d0df | 2010-02-23 13:03:37 +0100 | [diff] [blame] | 5720 | LPWSTR lpszRawHeaders = NULL; |
Eric van Beurden | 5caf809 | 2009-06-04 10:52:16 -0400 | [diff] [blame] | 5721 | LPWSTR temp; |
Mike McCormack | 13b6ce6d | 2004-08-09 18:54:23 +0000 | [diff] [blame] | 5722 | DWORD cchRawHeaders = 0; |
Aric Stewart | b9f2f9d | 2009-06-23 14:30:38 +0900 | [diff] [blame] | 5723 | BOOL codeHundred = FALSE; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5724 | |
Aric Stewart | ff9b9d4 | 2002-06-21 23:59:49 +0000 | [diff] [blame] | 5725 | TRACE("-->\n"); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5726 | |
Jacek Caban | 8a1df20 | 2011-05-10 09:26:43 +0000 | [diff] [blame] | 5727 | if(!request->netconn) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5728 | goto lend; |
| 5729 | |
Hans Leidekker | 6522393 | 2012-01-13 15:15:48 +0100 | [diff] [blame] | 5730 | NETCON_set_timeout( request->netconn, FALSE, request->receive_timeout ); |
Hans Leidekker | 2617fb6 | 2008-02-17 20:41:56 +0100 | [diff] [blame] | 5731 | do { |
Aric Stewart | b9f2f9d | 2009-06-23 14:30:38 +0900 | [diff] [blame] | 5732 | static const WCHAR szHundred[] = {'1','0','0',0}; |
Hans Leidekker | 2617fb6 | 2008-02-17 20:41:56 +0100 | [diff] [blame] | 5733 | /* |
Hans Leidekker | 2617fb6 | 2008-02-17 20:41:56 +0100 | [diff] [blame] | 5734 | * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code. |
| 5735 | */ |
Hans Leidekker | 96b639d | 2009-03-04 12:42:43 +0100 | [diff] [blame] | 5736 | buflen = MAX_REPLY_LEN; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5737 | if (!read_line(request, bufferA, &buflen)) |
Hans Leidekker | 2617fb6 | 2008-02-17 20:41:56 +0100 | [diff] [blame] | 5738 | goto lend; |
Piotr Caban | ee68473 | 2010-06-29 12:20:51 +0200 | [diff] [blame] | 5739 | |
| 5740 | /* clear old response headers (eg. from a redirect response) */ |
| 5741 | if (clear) { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5742 | HTTP_clear_response_headers( request ); |
Piotr Caban | ee68473 | 2010-06-29 12:20:51 +0200 | [diff] [blame] | 5743 | clear = FALSE; |
| 5744 | } |
| 5745 | |
Hans Leidekker | 96b639d | 2009-03-04 12:42:43 +0100 | [diff] [blame] | 5746 | rc += buflen; |
Hans Leidekker | 2617fb6 | 2008-02-17 20:41:56 +0100 | [diff] [blame] | 5747 | MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN ); |
Aric Stewart | b9f2f9d | 2009-06-23 14:30:38 +0900 | [diff] [blame] | 5748 | /* check is this a status code line? */ |
| 5749 | if (!strncmpW(buffer, g_szHttp1_0, 4)) |
| 5750 | { |
| 5751 | /* split the version from the status code */ |
| 5752 | status_code = strchrW( buffer, ' ' ); |
| 5753 | if( !status_code ) |
| 5754 | goto lend; |
| 5755 | *status_code++=0; |
Hans Leidekker | 2617fb6 | 2008-02-17 20:41:56 +0100 | [diff] [blame] | 5756 | |
Aric Stewart | b9f2f9d | 2009-06-23 14:30:38 +0900 | [diff] [blame] | 5757 | /* split the status code from the status text */ |
| 5758 | status_text = strchrW( status_code, ' ' ); |
| 5759 | if( !status_text ) |
| 5760 | goto lend; |
| 5761 | *status_text++=0; |
Hans Leidekker | 2617fb6 | 2008-02-17 20:41:56 +0100 | [diff] [blame] | 5762 | |
Jacek Caban | 5240e40 | 2012-05-03 12:19:47 +0200 | [diff] [blame] | 5763 | request->status_code = atoiW(status_code); |
| 5764 | |
Aric Stewart | b9f2f9d | 2009-06-23 14:30:38 +0900 | [diff] [blame] | 5765 | TRACE("version [%s] status code [%s] status text [%s]\n", |
| 5766 | debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) ); |
Hans Leidekker | 2617fb6 | 2008-02-17 20:41:56 +0100 | [diff] [blame] | 5767 | |
Aric Stewart | b9f2f9d | 2009-06-23 14:30:38 +0900 | [diff] [blame] | 5768 | codeHundred = (!strcmpW(status_code, szHundred)); |
| 5769 | } |
| 5770 | else if (!codeHundred) |
| 5771 | { |
Hans Leidekker | 2ed570e | 2010-02-22 12:28:03 +0100 | [diff] [blame] | 5772 | WARN("No status line at head of response (%s)\n", debugstr_w(buffer)); |
| 5773 | |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 5774 | heap_free(request->version); |
| 5775 | heap_free(request->statusText); |
Hans Leidekker | 2ed570e | 2010-02-22 12:28:03 +0100 | [diff] [blame] | 5776 | |
Jacek Caban | 2aa3d69 | 2012-05-03 12:20:38 +0200 | [diff] [blame] | 5777 | request->status_code = HTTP_STATUS_OK; |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 5778 | request->version = heap_strdupW(g_szHttp1_0); |
| 5779 | request->statusText = heap_strdupW(szOK); |
Hans Leidekker | 2ed570e | 2010-02-22 12:28:03 +0100 | [diff] [blame] | 5780 | |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 5781 | heap_free(request->rawHeaders); |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 5782 | request->rawHeaders = heap_strdupW(szDefaultHeader); |
Hans Leidekker | 2ed570e | 2010-02-22 12:28:03 +0100 | [diff] [blame] | 5783 | |
| 5784 | bSuccess = TRUE; |
Erik Inge Bolsø | 5af1a49 | 2009-09-29 21:50:12 +0200 | [diff] [blame] | 5785 | goto lend; |
Aric Stewart | b9f2f9d | 2009-06-23 14:30:38 +0900 | [diff] [blame] | 5786 | } |
| 5787 | } while (codeHundred); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5788 | |
Maarten Lankhorst | 5132fb4 | 2008-02-25 11:05:41 -0800 | [diff] [blame] | 5789 | /* Add status code */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5790 | HTTP_ProcessHeader(request, szStatus, status_code, |
Maarten Lankhorst | 5132fb4 | 2008-02-25 11:05:41 -0800 | [diff] [blame] | 5791 | HTTP_ADDHDR_FLAG_REPLACE); |
| 5792 | |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 5793 | heap_free(request->version); |
| 5794 | heap_free(request->statusText); |
Maarten Lankhorst | 5132fb4 | 2008-02-25 11:05:41 -0800 | [diff] [blame] | 5795 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 5796 | request->version = heap_strdupW(buffer); |
| 5797 | request->statusText = heap_strdupW(status_text); |
Maarten Lankhorst | 5132fb4 | 2008-02-25 11:05:41 -0800 | [diff] [blame] | 5798 | |
| 5799 | /* Restore the spaces */ |
| 5800 | *(status_code-1) = ' '; |
| 5801 | *(status_text-1) = ' '; |
| 5802 | |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 5803 | /* regenerate raw headers */ |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 5804 | lpszRawHeaders = heap_alloc((cchMaxRawHeaders + 1) * sizeof(WCHAR)); |
Hans Leidekker | 911d0df | 2010-02-23 13:03:37 +0100 | [diff] [blame] | 5805 | if (!lpszRawHeaders) goto lend; |
| 5806 | |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 5807 | while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders) |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 5808 | cchMaxRawHeaders *= 2; |
Jacek Caban | 55b2722 | 2011-04-22 12:35:05 +0200 | [diff] [blame] | 5809 | temp = heap_realloc(lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR)); |
Eric van Beurden | 5caf809 | 2009-06-04 10:52:16 -0400 | [diff] [blame] | 5810 | if (temp == NULL) goto lend; |
| 5811 | lpszRawHeaders = temp; |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 5812 | memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR)); |
| 5813 | cchRawHeaders += (buflen-1); |
| 5814 | memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf)); |
| 5815 | cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1; |
| 5816 | lpszRawHeaders[cchRawHeaders] = '\0'; |
| 5817 | |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5818 | /* Parse each response line */ |
| 5819 | do |
| 5820 | { |
| 5821 | buflen = MAX_REPLY_LEN; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5822 | if (read_line(request, bufferA, &buflen)) |
Robert Shearman | b72a682 | 2004-09-23 22:53:50 +0000 | [diff] [blame] | 5823 | { |
| 5824 | LPWSTR * pFieldAndValue; |
| 5825 | |
Francois Gouget | da8b3dd | 2005-01-26 21:09:04 +0000 | [diff] [blame] | 5826 | TRACE("got line %s, now interpreting\n", debugstr_a(bufferA)); |
Hans Leidekker | 100ee0a | 2009-03-04 12:44:22 +0100 | [diff] [blame] | 5827 | |
| 5828 | if (!bufferA[0]) break; |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 5829 | MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN ); |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 5830 | |
Robert Shearman | b72a682 | 2004-09-23 22:53:50 +0000 | [diff] [blame] | 5831 | pFieldAndValue = HTTP_InterpretHttpHeader(buffer); |
Eric van Beurden | 59a2178 | 2009-06-04 10:52:59 -0400 | [diff] [blame] | 5832 | if (pFieldAndValue) |
| 5833 | { |
| 5834 | while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders) |
| 5835 | cchMaxRawHeaders *= 2; |
Jacek Caban | 55b2722 | 2011-04-22 12:35:05 +0200 | [diff] [blame] | 5836 | temp = heap_realloc(lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR)); |
Eric van Beurden | 59a2178 | 2009-06-04 10:52:59 -0400 | [diff] [blame] | 5837 | if (temp == NULL) goto lend; |
| 5838 | lpszRawHeaders = temp; |
| 5839 | memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR)); |
| 5840 | cchRawHeaders += (buflen-1); |
| 5841 | memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf)); |
| 5842 | cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1; |
| 5843 | lpszRawHeaders[cchRawHeaders] = '\0'; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5844 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5845 | HTTP_ProcessHeader(request, pFieldAndValue[0], pFieldAndValue[1], |
Eric van Beurden | 59a2178 | 2009-06-04 10:52:59 -0400 | [diff] [blame] | 5846 | HTTP_ADDREQ_FLAG_ADD ); |
Robert Shearman | b72a682 | 2004-09-23 22:53:50 +0000 | [diff] [blame] | 5847 | |
Eric van Beurden | 59a2178 | 2009-06-04 10:52:59 -0400 | [diff] [blame] | 5848 | HTTP_FreeTokens(pFieldAndValue); |
| 5849 | } |
| 5850 | } |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5851 | else |
| 5852 | { |
| 5853 | cbreaks++; |
| 5854 | if (cbreaks >= 2) |
| 5855 | break; |
| 5856 | } |
| 5857 | }while(1); |
| 5858 | |
Eric van Beurden | 42c7dc9 | 2009-06-24 13:28:00 -0400 | [diff] [blame] | 5859 | /* make sure the response header is terminated with an empty line. Some apps really |
| 5860 | truly care about that empty line being there for some reason. Just add it to the |
| 5861 | header. */ |
| 5862 | if (cchRawHeaders + strlenW(szCrLf) > cchMaxRawHeaders) |
| 5863 | { |
| 5864 | cchMaxRawHeaders = cchRawHeaders + strlenW(szCrLf); |
Jacek Caban | 55b2722 | 2011-04-22 12:35:05 +0200 | [diff] [blame] | 5865 | temp = heap_realloc(lpszRawHeaders, (cchMaxRawHeaders + 1) * sizeof(WCHAR)); |
Eric van Beurden | 42c7dc9 | 2009-06-24 13:28:00 -0400 | [diff] [blame] | 5866 | if (temp == NULL) goto lend; |
| 5867 | lpszRawHeaders = temp; |
| 5868 | } |
| 5869 | |
| 5870 | memcpy(&lpszRawHeaders[cchRawHeaders], szCrLf, sizeof(szCrLf)); |
| 5871 | |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 5872 | heap_free(request->rawHeaders); |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 5873 | request->rawHeaders = lpszRawHeaders; |
Robert Shearman | dee8751 | 2004-07-19 20:09:20 +0000 | [diff] [blame] | 5874 | TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders)); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5875 | bSuccess = TRUE; |
| 5876 | |
| 5877 | lend: |
| 5878 | |
Aric Stewart | ff9b9d4 | 2002-06-21 23:59:49 +0000 | [diff] [blame] | 5879 | TRACE("<--\n"); |
| 5880 | if (bSuccess) |
| 5881 | return rc; |
| 5882 | else |
Hans Leidekker | e3e2622 | 2008-07-19 19:52:47 +0200 | [diff] [blame] | 5883 | { |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 5884 | heap_free(lpszRawHeaders); |
Robert Shearman | 0e7c41e | 2005-11-28 11:55:16 +0100 | [diff] [blame] | 5885 | return 0; |
Hans Leidekker | e3e2622 | 2008-07-19 19:52:47 +0200 | [diff] [blame] | 5886 | } |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5887 | } |
| 5888 | |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5889 | /*********************************************************************** |
| 5890 | * HTTP_InterpretHttpHeader (internal) |
| 5891 | * |
| 5892 | * Parse server response |
| 5893 | * |
| 5894 | * RETURNS |
| 5895 | * |
Robert Shearman | b72a682 | 2004-09-23 22:53:50 +0000 | [diff] [blame] | 5896 | * Pointer to array of field, value, NULL on success. |
| 5897 | * NULL on error. |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5898 | */ |
Jacek Caban | 02708c6 | 2005-10-26 10:07:58 +0000 | [diff] [blame] | 5899 | static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5900 | { |
Robert Shearman | b72a682 | 2004-09-23 22:53:50 +0000 | [diff] [blame] | 5901 | LPWSTR * pTokenPair; |
| 5902 | LPWSTR pszColon; |
| 5903 | INT len; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5904 | |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 5905 | pTokenPair = heap_alloc_zero(sizeof(*pTokenPair)*3); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5906 | |
Robert Shearman | b72a682 | 2004-09-23 22:53:50 +0000 | [diff] [blame] | 5907 | pszColon = strchrW(buffer, ':'); |
| 5908 | /* must have two tokens */ |
| 5909 | if (!pszColon) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5910 | { |
Robert Shearman | b72a682 | 2004-09-23 22:53:50 +0000 | [diff] [blame] | 5911 | HTTP_FreeTokens(pTokenPair); |
Robert Shearman | 7707a76 | 2005-03-10 11:14:24 +0000 | [diff] [blame] | 5912 | if (buffer[0]) |
| 5913 | TRACE("No ':' in line: %s\n", debugstr_w(buffer)); |
Robert Shearman | b72a682 | 2004-09-23 22:53:50 +0000 | [diff] [blame] | 5914 | return NULL; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5915 | } |
| 5916 | |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 5917 | pTokenPair[0] = heap_alloc((pszColon - buffer + 1) * sizeof(WCHAR)); |
Robert Shearman | b72a682 | 2004-09-23 22:53:50 +0000 | [diff] [blame] | 5918 | if (!pTokenPair[0]) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5919 | { |
Robert Shearman | b72a682 | 2004-09-23 22:53:50 +0000 | [diff] [blame] | 5920 | HTTP_FreeTokens(pTokenPair); |
| 5921 | return NULL; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5922 | } |
Robert Shearman | b72a682 | 2004-09-23 22:53:50 +0000 | [diff] [blame] | 5923 | memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR)); |
| 5924 | pTokenPair[0][pszColon - buffer] = '\0'; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5925 | |
Robert Shearman | b72a682 | 2004-09-23 22:53:50 +0000 | [diff] [blame] | 5926 | /* skip colon */ |
| 5927 | pszColon++; |
| 5928 | len = strlenW(pszColon); |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 5929 | pTokenPair[1] = heap_alloc((len + 1) * sizeof(WCHAR)); |
Robert Shearman | b72a682 | 2004-09-23 22:53:50 +0000 | [diff] [blame] | 5930 | if (!pTokenPair[1]) |
| 5931 | { |
| 5932 | HTTP_FreeTokens(pTokenPair); |
| 5933 | return NULL; |
| 5934 | } |
| 5935 | memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR)); |
| 5936 | |
| 5937 | strip_spaces(pTokenPair[0]); |
| 5938 | strip_spaces(pTokenPair[1]); |
| 5939 | |
| 5940 | TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1])); |
| 5941 | return pTokenPair; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5942 | } |
| 5943 | |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5944 | /*********************************************************************** |
| 5945 | * HTTP_ProcessHeader (internal) |
| 5946 | * |
| 5947 | * Stuff header into header tables according to <dwModifier> |
| 5948 | * |
| 5949 | */ |
| 5950 | |
Juan Lang | a1ab4a7 | 2007-11-07 14:45:06 -0800 | [diff] [blame] | 5951 | #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5952 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5953 | static DWORD HTTP_ProcessHeader(http_request_t *request, LPCWSTR field, LPCWSTR value, DWORD dwModifier) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5954 | { |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 5955 | LPHTTPHEADERW lphttpHdr = NULL; |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 5956 | INT index = -1; |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 5957 | BOOL request_only = dwModifier & HTTP_ADDHDR_FLAG_REQ; |
Jacek Caban | e974965 | 2009-11-30 20:01:00 +0100 | [diff] [blame] | 5958 | DWORD res = ERROR_HTTP_INVALID_HEADER; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5959 | |
Hans Leidekker | cd2c458 | 2006-10-05 13:18:56 +0200 | [diff] [blame] | 5960 | TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), dwModifier); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5961 | |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 5962 | /* REPLACE wins out over ADD */ |
| 5963 | if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE) |
| 5964 | dwModifier &= ~HTTP_ADDHDR_FLAG_ADD; |
| 5965 | |
| 5966 | if (dwModifier & HTTP_ADDHDR_FLAG_ADD) |
| 5967 | index = -1; |
| 5968 | else |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5969 | index = HTTP_GetCustomHeaderIndex(request, field, 0, request_only); |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 5970 | |
| 5971 | if (index >= 0) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 5972 | { |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 5973 | if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW) |
Jacek Caban | e974965 | 2009-11-30 20:01:00 +0100 | [diff] [blame] | 5974 | return ERROR_HTTP_INVALID_HEADER; |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 5975 | lphttpHdr = &request->custHeaders[index]; |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 5976 | } |
| 5977 | else if (value) |
| 5978 | { |
| 5979 | HTTPHEADERW hdr; |
| 5980 | |
| 5981 | hdr.lpszField = (LPWSTR)field; |
| 5982 | hdr.lpszValue = (LPWSTR)value; |
| 5983 | hdr.wFlags = hdr.wCount = 0; |
| 5984 | |
| 5985 | if (dwModifier & HTTP_ADDHDR_FLAG_REQ) |
| 5986 | hdr.wFlags |= HDR_ISREQUEST; |
| 5987 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 5988 | return HTTP_InsertCustomHeader(request, &hdr); |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 5989 | } |
Rob Shearman | 7b002a3 | 2007-01-12 19:16:49 -0600 | [diff] [blame] | 5990 | /* no value to delete */ |
Jacek Caban | e974965 | 2009-11-30 20:01:00 +0100 | [diff] [blame] | 5991 | else return ERROR_SUCCESS; |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 5992 | |
| 5993 | if (dwModifier & HTTP_ADDHDR_FLAG_REQ) |
| 5994 | lphttpHdr->wFlags |= HDR_ISREQUEST; |
| 5995 | else |
| 5996 | lphttpHdr->wFlags &= ~HDR_ISREQUEST; |
| 5997 | |
| 5998 | if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE) |
| 5999 | { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 6000 | HTTP_DeleteCustomHeader( request, index ); |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 6001 | |
| 6002 | if (value) |
Aric Stewart | ff9b9d4 | 2002-06-21 23:59:49 +0000 | [diff] [blame] | 6003 | { |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 6004 | HTTPHEADERW hdr; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6005 | |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 6006 | hdr.lpszField = (LPWSTR)field; |
| 6007 | hdr.lpszValue = (LPWSTR)value; |
Aric Stewart | ff9b9d4 | 2002-06-21 23:59:49 +0000 | [diff] [blame] | 6008 | hdr.wFlags = hdr.wCount = 0; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6009 | |
| 6010 | if (dwModifier & HTTP_ADDHDR_FLAG_REQ) |
| 6011 | hdr.wFlags |= HDR_ISREQUEST; |
| 6012 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 6013 | return HTTP_InsertCustomHeader(request, &hdr); |
Aric Stewart | ff9b9d4 | 2002-06-21 23:59:49 +0000 | [diff] [blame] | 6014 | } |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 6015 | |
Jacek Caban | e974965 | 2009-11-30 20:01:00 +0100 | [diff] [blame] | 6016 | return ERROR_SUCCESS; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6017 | } |
Juan Lang | a1ab4a7 | 2007-11-07 14:45:06 -0800 | [diff] [blame] | 6018 | else if (dwModifier & COALESCEFLAGS) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6019 | { |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 6020 | LPWSTR lpsztmp; |
| 6021 | WCHAR ch = 0; |
| 6022 | INT len = 0; |
| 6023 | INT origlen = strlenW(lphttpHdr->lpszValue); |
| 6024 | INT valuelen = strlenW(value); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6025 | |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 6026 | if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6027 | { |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 6028 | ch = ','; |
| 6029 | lphttpHdr->wFlags |= HDR_COMMADELIMITED; |
| 6030 | } |
| 6031 | else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON) |
| 6032 | { |
| 6033 | ch = ';'; |
| 6034 | lphttpHdr->wFlags |= HDR_COMMADELIMITED; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6035 | } |
| 6036 | |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 6037 | len = origlen + valuelen + ((ch > 0) ? 2 : 0); |
| 6038 | |
Jacek Caban | 55b2722 | 2011-04-22 12:35:05 +0200 | [diff] [blame] | 6039 | lpsztmp = heap_realloc(lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR)); |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 6040 | if (lpsztmp) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6041 | { |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 6042 | lphttpHdr->lpszValue = lpsztmp; |
| 6043 | /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */ |
| 6044 | if (ch > 0) |
| 6045 | { |
| 6046 | lphttpHdr->lpszValue[origlen] = ch; |
| 6047 | origlen++; |
| 6048 | lphttpHdr->lpszValue[origlen] = ' '; |
| 6049 | origlen++; |
| 6050 | } |
| 6051 | |
| 6052 | memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR)); |
| 6053 | lphttpHdr->lpszValue[len] = '\0'; |
Jacek Caban | e974965 | 2009-11-30 20:01:00 +0100 | [diff] [blame] | 6054 | res = ERROR_SUCCESS; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6055 | } |
| 6056 | else |
| 6057 | { |
Jacek Caban | 55b2722 | 2011-04-22 12:35:05 +0200 | [diff] [blame] | 6058 | WARN("heap_realloc (%d bytes) failed\n",len+1); |
Jacek Caban | e974965 | 2009-11-30 20:01:00 +0100 | [diff] [blame] | 6059 | res = ERROR_OUTOFMEMORY; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6060 | } |
| 6061 | } |
Jacek Caban | e974965 | 2009-11-30 20:01:00 +0100 | [diff] [blame] | 6062 | TRACE("<-- %d\n", res); |
| 6063 | return res; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6064 | } |
| 6065 | |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6066 | /*********************************************************************** |
| 6067 | * HTTP_GetCustomHeaderIndex (internal) |
| 6068 | * |
| 6069 | * Return index of custom header from header array |
| 6070 | * |
| 6071 | */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 6072 | static INT HTTP_GetCustomHeaderIndex(http_request_t *request, LPCWSTR lpszField, |
Mike McCormack | 92ddc1c | 2006-03-30 18:20:04 +0900 | [diff] [blame] | 6073 | int requested_index, BOOL request_only) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6074 | { |
Mike McCormack | 13b6ce6d | 2004-08-09 18:54:23 +0000 | [diff] [blame] | 6075 | DWORD index; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6076 | |
Juan Lang | 3cd5455 | 2009-12-04 14:37:46 -0800 | [diff] [blame] | 6077 | TRACE("%s, %d, %d\n", debugstr_w(lpszField), requested_index, request_only); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6078 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 6079 | for (index = 0; index < request->nCustHeaders; index++) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6080 | { |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 6081 | if (strcmpiW(request->custHeaders[index].lpszField, lpszField)) |
Mike McCormack | 92ddc1c | 2006-03-30 18:20:04 +0900 | [diff] [blame] | 6082 | continue; |
| 6083 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 6084 | if (request_only && !(request->custHeaders[index].wFlags & HDR_ISREQUEST)) |
Mike McCormack | 92ddc1c | 2006-03-30 18:20:04 +0900 | [diff] [blame] | 6085 | continue; |
| 6086 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 6087 | if (!request_only && (request->custHeaders[index].wFlags & HDR_ISREQUEST)) |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 6088 | continue; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6089 | |
Aric Stewart | 1e946d3 | 2005-12-13 17:07:41 +0100 | [diff] [blame] | 6090 | if (requested_index == 0) |
Mike McCormack | 92ddc1c | 2006-03-30 18:20:04 +0900 | [diff] [blame] | 6091 | break; |
| 6092 | requested_index --; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6093 | } |
| 6094 | |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 6095 | if (index >= request->nCustHeaders) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6096 | index = -1; |
| 6097 | |
Hans Leidekker | cd2c458 | 2006-10-05 13:18:56 +0200 | [diff] [blame] | 6098 | TRACE("Return: %d\n", index); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6099 | return index; |
| 6100 | } |
| 6101 | |
| 6102 | |
| 6103 | /*********************************************************************** |
| 6104 | * HTTP_InsertCustomHeader (internal) |
| 6105 | * |
| 6106 | * Insert header into array |
| 6107 | * |
| 6108 | */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 6109 | static DWORD HTTP_InsertCustomHeader(http_request_t *request, LPHTTPHEADERW lpHdr) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6110 | { |
| 6111 | INT count; |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 6112 | LPHTTPHEADERW lph = NULL; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6113 | |
Mike McCormack | a4e902c | 2004-03-30 04:36:09 +0000 | [diff] [blame] | 6114 | TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue)); |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 6115 | count = request->nCustHeaders + 1; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6116 | if (count > 1) |
Jacek Caban | 55b2722 | 2011-04-22 12:35:05 +0200 | [diff] [blame] | 6117 | lph = heap_realloc_zero(request->custHeaders, sizeof(HTTPHEADERW) * count); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6118 | else |
Jacek Caban | 354a74e | 2011-04-21 13:39:03 +0200 | [diff] [blame] | 6119 | lph = heap_alloc_zero(sizeof(HTTPHEADERW) * count); |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6120 | |
Jacek Caban | e974965 | 2009-11-30 20:01:00 +0100 | [diff] [blame] | 6121 | if (!lph) |
| 6122 | return ERROR_OUTOFMEMORY; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6123 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 6124 | request->custHeaders = lph; |
| 6125 | request->custHeaders[count-1].lpszField = heap_strdupW(lpHdr->lpszField); |
| 6126 | request->custHeaders[count-1].lpszValue = heap_strdupW(lpHdr->lpszValue); |
| 6127 | request->custHeaders[count-1].wFlags = lpHdr->wFlags; |
| 6128 | request->custHeaders[count-1].wCount= lpHdr->wCount; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 6129 | request->nCustHeaders++; |
Jacek Caban | e974965 | 2009-11-30 20:01:00 +0100 | [diff] [blame] | 6130 | |
| 6131 | return ERROR_SUCCESS; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6132 | } |
| 6133 | |
| 6134 | |
| 6135 | /*********************************************************************** |
| 6136 | * HTTP_DeleteCustomHeader (internal) |
| 6137 | * |
| 6138 | * Delete header from array |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 6139 | * If this function is called, the indexs may change. |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6140 | */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 6141 | static BOOL HTTP_DeleteCustomHeader(http_request_t *request, DWORD index) |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6142 | { |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 6143 | if( request->nCustHeaders <= 0 ) |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 6144 | return FALSE; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 6145 | if( index >= request->nCustHeaders ) |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 6146 | return FALSE; |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 6147 | request->nCustHeaders--; |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 6148 | |
Hans Leidekker | 9acd1ef | 2011-06-01 11:50:53 +0200 | [diff] [blame] | 6149 | heap_free(request->custHeaders[index].lpszField); |
| 6150 | heap_free(request->custHeaders[index].lpszValue); |
Rob Shearman | 62e0a8c | 2008-06-20 10:14:38 +0100 | [diff] [blame] | 6151 | |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 6152 | memmove( &request->custHeaders[index], &request->custHeaders[index+1], |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 6153 | (request->nCustHeaders - index)* sizeof(HTTPHEADERW) ); |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 6154 | memset( &request->custHeaders[request->nCustHeaders], 0, sizeof(HTTPHEADERW) ); |
Mike McCormack | a1c16d2 | 2003-07-22 03:17:52 +0000 | [diff] [blame] | 6155 | |
| 6156 | return TRUE; |
Ulrich Czekalla | c275724 | 2000-06-11 20:04:44 +0000 | [diff] [blame] | 6157 | } |
Alberto Massari | b09eef2 | 2002-11-13 04:08:26 +0000 | [diff] [blame] | 6158 | |
Aric Stewart | c8dfc02 | 2007-07-26 08:59:00 -0500 | [diff] [blame] | 6159 | |
| 6160 | /*********************************************************************** |
| 6161 | * HTTP_VerifyValidHeader (internal) |
| 6162 | * |
| 6163 | * Verify the given header is not invalid for the given http request |
| 6164 | * |
| 6165 | */ |
Juan Lang | b49b243 | 2011-03-01 10:59:39 -0800 | [diff] [blame] | 6166 | static BOOL HTTP_VerifyValidHeader(http_request_t *request, LPCWSTR field) |
Aric Stewart | c8dfc02 | 2007-07-26 08:59:00 -0500 | [diff] [blame] | 6167 | { |
Aric Stewart | c8dfc02 | 2007-07-26 08:59:00 -0500 | [diff] [blame] | 6168 | /* Accept-Encoding is stripped from HTTP/1.0 requests. It is invalid */ |
Juan Lang | 2098006 | 2011-03-01 11:18:22 -0800 | [diff] [blame] | 6169 | if (!strcmpW(request->version, g_szHttp1_0) && !strcmpiW(field, szAccept_Encoding)) |
Jacek Caban | e974965 | 2009-11-30 20:01:00 +0100 | [diff] [blame] | 6170 | return ERROR_HTTP_INVALID_HEADER; |
Aric Stewart | c8dfc02 | 2007-07-26 08:59:00 -0500 | [diff] [blame] | 6171 | |
Jacek Caban | e974965 | 2009-11-30 20:01:00 +0100 | [diff] [blame] | 6172 | return ERROR_SUCCESS; |
Aric Stewart | c8dfc02 | 2007-07-26 08:59:00 -0500 | [diff] [blame] | 6173 | } |
| 6174 | |
Alberto Massari | b09eef2 | 2002-11-13 04:08:26 +0000 | [diff] [blame] | 6175 | /*********************************************************************** |
| 6176 | * IsHostInProxyBypassList (@) |
| 6177 | * |
| 6178 | * Undocumented |
| 6179 | * |
| 6180 | */ |
| 6181 | BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length) |
| 6182 | { |
Hans Leidekker | cd2c458 | 2006-10-05 13:18:56 +0200 | [diff] [blame] | 6183 | FIXME("STUB: flags=%d host=%s length=%d\n",flags,szHost,length); |
Alberto Massari | b09eef2 | 2002-11-13 04:08:26 +0000 | [diff] [blame] | 6184 | return FALSE; |
| 6185 | } |
Austin English | 1c7d349 | 2010-11-25 06:59:25 -0800 | [diff] [blame] | 6186 | |
| 6187 | /*********************************************************************** |
| 6188 | * InternetShowSecurityInfoByURLA (@) |
| 6189 | */ |
| 6190 | BOOL WINAPI InternetShowSecurityInfoByURLA(LPCSTR url, HWND window) |
| 6191 | { |
| 6192 | FIXME("stub: %s %p\n", url, window); |
| 6193 | return FALSE; |
| 6194 | } |
| 6195 | |
| 6196 | /*********************************************************************** |
| 6197 | * InternetShowSecurityInfoByURLW (@) |
| 6198 | */ |
| 6199 | BOOL WINAPI InternetShowSecurityInfoByURLW(LPCWSTR url, HWND window) |
| 6200 | { |
| 6201 | FIXME("stub: %s %p\n", debugstr_w(url), window); |
| 6202 | return FALSE; |
| 6203 | } |
André Hentschel | adea3c5 | 2011-10-29 15:08:08 +0200 | [diff] [blame] | 6204 | |
| 6205 | /*********************************************************************** |
| 6206 | * ShowX509EncodedCertificate (@) |
| 6207 | */ |
| 6208 | DWORD WINAPI ShowX509EncodedCertificate(HWND parent, LPBYTE cert, DWORD len) |
| 6209 | { |
Juan Lang | 044f645 | 2011-10-31 12:55:15 -0700 | [diff] [blame] | 6210 | PCCERT_CONTEXT certContext = CertCreateCertificateContext(X509_ASN_ENCODING, |
| 6211 | cert, len); |
| 6212 | DWORD ret; |
| 6213 | |
| 6214 | if (certContext) |
| 6215 | { |
| 6216 | CRYPTUI_VIEWCERTIFICATE_STRUCTW view; |
| 6217 | |
| 6218 | memset(&view, 0, sizeof(view)); |
| 6219 | view.hwndParent = parent; |
| 6220 | view.pCertContext = certContext; |
| 6221 | if (CryptUIDlgViewCertificateW(&view, NULL)) |
| 6222 | ret = ERROR_SUCCESS; |
| 6223 | else |
| 6224 | ret = GetLastError(); |
| 6225 | CertFreeCertificateContext(certContext); |
| 6226 | } |
| 6227 | else |
| 6228 | ret = GetLastError(); |
| 6229 | return ret; |
André Hentschel | adea3c5 | 2011-10-29 15:08:08 +0200 | [diff] [blame] | 6230 | } |