2 * Wininet - Http Implementation
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 TransGaming Technologies Inc.
7 * Copyright 2004 Mike McCormack for CodeWeavers
8 * Copyright 2005 Aric Stewart for CodeWeavers
9 * Copyright 2006 Robert Shearman for CodeWeavers
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/port.h"
32 #if defined(__MINGW32__) || defined (_MSC_VER)
36 #include <sys/types.h>
37 #ifdef HAVE_SYS_SOCKET_H
38 # include <sys/socket.h>
40 #ifdef HAVE_ARPA_INET_H
41 # include <arpa/inet.h>
56 #define NO_SHLWAPI_STREAM
57 #define NO_SHLWAPI_REG
58 #define NO_SHLWAPI_STRFCNS
59 #define NO_SHLWAPI_GDI
65 #include "wine/debug.h"
66 #include "wine/exception.h"
67 #include "wine/unicode.h"
69 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
71 static const WCHAR g_szHttp1_0
[] = {'H','T','T','P','/','1','.','0',0};
72 static const WCHAR g_szHttp1_1
[] = {'H','T','T','P','/','1','.','1',0};
73 static const WCHAR hostW
[] = { 'H','o','s','t',0 };
74 static const WCHAR szAuthorization
[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
75 static const WCHAR szProxy_Authorization
[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
76 static const WCHAR szStatus
[] = { 'S','t','a','t','u','s',0 };
77 static const WCHAR szKeepAlive
[] = {'K','e','e','p','-','A','l','i','v','e',0};
78 static const WCHAR szGET
[] = { 'G','E','T', 0 };
79 static const WCHAR szCrLf
[] = {'\r','\n', 0};
81 static const WCHAR szAccept
[] = { 'A','c','c','e','p','t',0 };
82 static const WCHAR szAccept_Charset
[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
83 static const WCHAR szAccept_Encoding
[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
84 static const WCHAR szAccept_Language
[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
85 static const WCHAR szAccept_Ranges
[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
86 static const WCHAR szAge
[] = { 'A','g','e',0 };
87 static const WCHAR szAllow
[] = { 'A','l','l','o','w',0 };
88 static const WCHAR szCache_Control
[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
89 static const WCHAR szConnection
[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
90 static const WCHAR szContent_Base
[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
91 static const WCHAR szContent_Encoding
[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
92 static const WCHAR szContent_ID
[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
93 static const WCHAR szContent_Language
[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
94 static const WCHAR szContent_Length
[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
95 static const WCHAR szContent_Location
[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
96 static const WCHAR szContent_MD5
[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
97 static const WCHAR szContent_Range
[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
98 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 };
99 static const WCHAR szContent_Type
[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
100 static const WCHAR szCookie
[] = { 'C','o','o','k','i','e',0 };
101 static const WCHAR szDate
[] = { 'D','a','t','e',0 };
102 static const WCHAR szFrom
[] = { 'F','r','o','m',0 };
103 static const WCHAR szETag
[] = { 'E','T','a','g',0 };
104 static const WCHAR szExpect
[] = { 'E','x','p','e','c','t',0 };
105 static const WCHAR szExpires
[] = { 'E','x','p','i','r','e','s',0 };
106 static const WCHAR szIf_Match
[] = { 'I','f','-','M','a','t','c','h',0 };
107 static const WCHAR szIf_Modified_Since
[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
108 static const WCHAR szIf_None_Match
[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
109 static const WCHAR szIf_Range
[] = { 'I','f','-','R','a','n','g','e',0 };
110 static const WCHAR szIf_Unmodified_Since
[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
111 static const WCHAR szLast_Modified
[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
112 static const WCHAR szLocation
[] = { 'L','o','c','a','t','i','o','n',0 };
113 static const WCHAR szMax_Forwards
[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
114 static const WCHAR szMime_Version
[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
115 static const WCHAR szPragma
[] = { 'P','r','a','g','m','a',0 };
116 static const WCHAR szProxy_Authenticate
[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
117 static const WCHAR szProxy_Connection
[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
118 static const WCHAR szPublic
[] = { 'P','u','b','l','i','c',0 };
119 static const WCHAR szRange
[] = { 'R','a','n','g','e',0 };
120 static const WCHAR szReferer
[] = { 'R','e','f','e','r','e','r',0 };
121 static const WCHAR szRetry_After
[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
122 static const WCHAR szServer
[] = { 'S','e','r','v','e','r',0 };
123 static const WCHAR szSet_Cookie
[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
124 static const WCHAR szTransfer_Encoding
[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
125 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 };
126 static const WCHAR szUpgrade
[] = { 'U','p','g','r','a','d','e',0 };
127 static const WCHAR szURI
[] = { 'U','R','I',0 };
128 static const WCHAR szUser_Agent
[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
129 static const WCHAR szVary
[] = { 'V','a','r','y',0 };
130 static const WCHAR szVia
[] = { 'V','i','a',0 };
131 static const WCHAR szWarning
[] = { 'W','a','r','n','i','n','g',0 };
132 static const WCHAR szWWW_Authenticate
[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
134 #define MAXHOSTNAME 100
135 #define MAX_FIELD_VALUE_LEN 256
136 #define MAX_FIELD_LEN 256
138 #define HTTP_REFERER szReferer
139 #define HTTP_ACCEPT szAccept
140 #define HTTP_USERAGENT szUser_Agent
142 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
143 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
144 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
145 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
146 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
147 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
148 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
150 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
161 unsigned int auth_data_len
;
162 BOOL finished
; /* finished authenticating */
165 static BOOL
HTTP_OpenConnection(LPWININETHTTPREQW lpwhr
);
166 static BOOL
HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr
, BOOL clear
);
167 static BOOL
HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr
, LPCWSTR field
, LPCWSTR value
, DWORD dwModifier
);
168 static LPWSTR
* HTTP_InterpretHttpHeader(LPCWSTR buffer
);
169 static BOOL
HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr
, LPHTTPHEADERW lpHdr
);
170 static INT
HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszField
, INT index
, BOOL Request
);
171 static BOOL
HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr
, DWORD index
);
172 static LPWSTR
HTTP_build_req( LPCWSTR
*list
, int len
);
173 static BOOL
HTTP_HttpQueryInfoW(LPWININETHTTPREQW
, DWORD
, LPVOID
, LPDWORD
, LPDWORD
);
174 static LPWSTR
HTTP_GetRedirectURL(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszUrl
);
175 static BOOL
HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszUrl
);
176 static UINT
HTTP_DecodeBase64(LPCWSTR base64
, LPSTR bin
);
177 static BOOL
HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr
, LPCWSTR field
);
178 static void HTTP_DrainContent(WININETHTTPREQW
*req
);
179 static BOOL
HTTP_FinishedReading(LPWININETHTTPREQW lpwhr
);
181 static LPHTTPHEADERW
HTTP_GetHeader(LPWININETHTTPREQW req
, LPCWSTR head
)
184 HeaderIndex
= HTTP_GetCustomHeaderIndex(req
, head
, 0, TRUE
);
185 if (HeaderIndex
== -1)
188 return &req
->pCustHeaders
[HeaderIndex
];
191 /* set the request content length based on the headers */
192 static DWORD
set_content_length( LPWININETHTTPREQW lpwhr
)
194 static const WCHAR szChunked
[] = {'c','h','u','n','k','e','d',0};
198 size
= sizeof(lpwhr
->dwContentLength
);
199 if (!HTTP_HttpQueryInfoW(lpwhr
, HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_CONTENT_LENGTH
,
200 &lpwhr
->dwContentLength
, &size
, NULL
))
201 lpwhr
->dwContentLength
= ~0u;
203 size
= sizeof(encoding
);
204 if (HTTP_HttpQueryInfoW(lpwhr
, HTTP_QUERY_TRANSFER_ENCODING
, encoding
, &size
, NULL
) &&
205 !strcmpiW(encoding
, szChunked
))
207 lpwhr
->dwContentLength
= ~0u;
208 lpwhr
->read_chunked
= TRUE
;
211 return lpwhr
->dwContentLength
;
214 /***********************************************************************
215 * HTTP_Tokenize (internal)
217 * Tokenize a string, allocating memory for the tokens.
219 static LPWSTR
* HTTP_Tokenize(LPCWSTR string
, LPCWSTR token_string
)
221 LPWSTR
* token_array
;
228 /* empty string has no tokens */
232 for (i
= 0; string
[i
]; i
++)
234 if (!strncmpW(string
+i
, token_string
, strlenW(token_string
)))
238 /* we want to skip over separators, but not the null terminator */
239 for (j
= 0; j
< strlenW(token_string
) - 1; j
++)
247 /* add 1 for terminating NULL */
248 token_array
= HeapAlloc(GetProcessHeap(), 0, (tokens
+1) * sizeof(*token_array
));
249 token_array
[tokens
] = NULL
;
252 for (i
= 0; i
< tokens
; i
++)
255 next_token
= strstrW(string
, token_string
);
256 if (!next_token
) next_token
= string
+strlenW(string
);
257 len
= next_token
- string
;
258 token_array
[i
] = HeapAlloc(GetProcessHeap(), 0, (len
+1)*sizeof(WCHAR
));
259 memcpy(token_array
[i
], string
, len
*sizeof(WCHAR
));
260 token_array
[i
][len
] = '\0';
261 string
= next_token
+strlenW(token_string
);
266 /***********************************************************************
267 * HTTP_FreeTokens (internal)
269 * Frees memory returned from HTTP_Tokenize.
271 static void HTTP_FreeTokens(LPWSTR
* token_array
)
274 for (i
= 0; token_array
[i
]; i
++)
275 HeapFree(GetProcessHeap(), 0, token_array
[i
]);
276 HeapFree(GetProcessHeap(), 0, token_array
);
279 /* **********************************************************************
281 * Helper functions for the HttpSendRequest(Ex) functions
284 static void AsyncHttpSendRequestProc(WORKREQUEST
*workRequest
)
286 struct WORKREQ_HTTPSENDREQUESTW
const *req
= &workRequest
->u
.HttpSendRequestW
;
287 LPWININETHTTPREQW lpwhr
= (LPWININETHTTPREQW
) workRequest
->hdr
;
289 TRACE("%p\n", lpwhr
);
291 HTTP_HttpSendRequestW(lpwhr
, req
->lpszHeader
,
292 req
->dwHeaderLength
, req
->lpOptional
, req
->dwOptionalLength
,
293 req
->dwContentLength
, req
->bEndRequest
);
295 HeapFree(GetProcessHeap(), 0, req
->lpszHeader
);
298 static void HTTP_FixURL( LPWININETHTTPREQW lpwhr
)
300 static const WCHAR szSlash
[] = { '/',0 };
301 static const WCHAR szHttp
[] = { 'h','t','t','p',':','/','/', 0 };
303 /* If we don't have a path we set it to root */
304 if (NULL
== lpwhr
->lpszPath
)
305 lpwhr
->lpszPath
= WININET_strdupW(szSlash
);
306 else /* remove \r and \n*/
308 int nLen
= strlenW(lpwhr
->lpszPath
);
309 while ((nLen
>0 ) && ((lpwhr
->lpszPath
[nLen
-1] == '\r')||(lpwhr
->lpszPath
[nLen
-1] == '\n')))
312 lpwhr
->lpszPath
[nLen
]='\0';
314 /* Replace '\' with '/' */
317 if (lpwhr
->lpszPath
[nLen
] == '\\') lpwhr
->lpszPath
[nLen
]='/';
321 if(CSTR_EQUAL
!= CompareStringW( LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
,
322 lpwhr
->lpszPath
, strlenW(lpwhr
->lpszPath
), szHttp
, strlenW(szHttp
) )
323 && lpwhr
->lpszPath
[0] != '/') /* not an absolute path ?? --> fix it !! */
325 WCHAR
*fixurl
= HeapAlloc(GetProcessHeap(), 0,
326 (strlenW(lpwhr
->lpszPath
) + 2)*sizeof(WCHAR
));
328 strcpyW(fixurl
+ 1, lpwhr
->lpszPath
);
329 HeapFree( GetProcessHeap(), 0, lpwhr
->lpszPath
);
330 lpwhr
->lpszPath
= fixurl
;
334 static LPWSTR
HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr
, LPCWSTR verb
, LPCWSTR path
, LPCWSTR version
)
336 LPWSTR requestString
;
342 static const WCHAR szSpace
[] = { ' ',0 };
343 static const WCHAR szColon
[] = { ':',' ',0 };
344 static const WCHAR sztwocrlf
[] = {'\r','\n','\r','\n', 0};
346 /* allocate space for an array of all the string pointers to be added */
347 len
= (lpwhr
->nCustHeaders
)*4 + 10;
348 req
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(LPCWSTR
) );
350 /* add the verb, path and HTTP version string */
358 /* Append custom request headers */
359 for (i
= 0; i
< lpwhr
->nCustHeaders
; i
++)
361 if (lpwhr
->pCustHeaders
[i
].wFlags
& HDR_ISREQUEST
)
364 req
[n
++] = lpwhr
->pCustHeaders
[i
].lpszField
;
366 req
[n
++] = lpwhr
->pCustHeaders
[i
].lpszValue
;
368 TRACE("Adding custom header %s (%s)\n",
369 debugstr_w(lpwhr
->pCustHeaders
[i
].lpszField
),
370 debugstr_w(lpwhr
->pCustHeaders
[i
].lpszValue
));
375 ERR("oops. buffer overrun\n");
378 requestString
= HTTP_build_req( req
, 4 );
379 HeapFree( GetProcessHeap(), 0, req
);
382 * Set (header) termination string for request
383 * Make sure there's exactly two new lines at the end of the request
385 p
= &requestString
[strlenW(requestString
)-1];
386 while ( (*p
== '\n') || (*p
== '\r') )
388 strcpyW( p
+1, sztwocrlf
);
390 return requestString
;
393 static void HTTP_ProcessCookies( LPWININETHTTPREQW lpwhr
)
397 LPHTTPHEADERW setCookieHeader
;
399 while((HeaderIndex
= HTTP_GetCustomHeaderIndex(lpwhr
, szSet_Cookie
, numCookies
, FALSE
)) != -1)
401 setCookieHeader
= &lpwhr
->pCustHeaders
[HeaderIndex
];
403 if (!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_COOKIES
) && setCookieHeader
->lpszValue
)
406 static const WCHAR szFmt
[] = { 'h','t','t','p',':','/','/','%','s','%','s',0};
410 Host
= HTTP_GetHeader(lpwhr
, hostW
);
411 len
= lstrlenW(Host
->lpszValue
) + 9 + lstrlenW(lpwhr
->lpszPath
);
412 buf_url
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
413 sprintfW(buf_url
, szFmt
, Host
->lpszValue
, lpwhr
->lpszPath
);
414 InternetSetCookieW(buf_url
, NULL
, setCookieHeader
->lpszValue
);
416 HeapFree(GetProcessHeap(), 0, buf_url
);
422 static inline BOOL
is_basic_auth_value( LPCWSTR pszAuthValue
)
424 static const WCHAR szBasic
[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
425 return !strncmpiW(pszAuthValue
, szBasic
, ARRAYSIZE(szBasic
)) &&
426 ((pszAuthValue
[ARRAYSIZE(szBasic
)] == ' ') || !pszAuthValue
[ARRAYSIZE(szBasic
)]);
429 static BOOL
HTTP_DoAuthorization( LPWININETHTTPREQW lpwhr
, LPCWSTR pszAuthValue
,
430 struct HttpAuthInfo
**ppAuthInfo
,
431 LPWSTR domain_and_username
, LPWSTR password
)
433 SECURITY_STATUS sec_status
;
434 struct HttpAuthInfo
*pAuthInfo
= *ppAuthInfo
;
437 TRACE("%s\n", debugstr_w(pszAuthValue
));
444 pAuthInfo
= HeapAlloc(GetProcessHeap(), 0, sizeof(*pAuthInfo
));
448 SecInvalidateHandle(&pAuthInfo
->cred
);
449 SecInvalidateHandle(&pAuthInfo
->ctx
);
450 memset(&pAuthInfo
->exp
, 0, sizeof(pAuthInfo
->exp
));
452 pAuthInfo
->auth_data
= NULL
;
453 pAuthInfo
->auth_data_len
= 0;
454 pAuthInfo
->finished
= FALSE
;
456 if (is_basic_auth_value(pszAuthValue
))
458 static const WCHAR szBasic
[] = {'B','a','s','i','c',0};
459 pAuthInfo
->scheme
= WININET_strdupW(szBasic
);
460 if (!pAuthInfo
->scheme
)
462 HeapFree(GetProcessHeap(), 0, pAuthInfo
);
469 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity
;
471 pAuthInfo
->scheme
= WININET_strdupW(pszAuthValue
);
472 if (!pAuthInfo
->scheme
)
474 HeapFree(GetProcessHeap(), 0, pAuthInfo
);
478 if (domain_and_username
)
480 WCHAR
*user
= strchrW(domain_and_username
, '\\');
481 WCHAR
*domain
= domain_and_username
;
483 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
485 pAuthData
= &nt_auth_identity
;
490 user
= domain_and_username
;
494 nt_auth_identity
.Flags
= SEC_WINNT_AUTH_IDENTITY_UNICODE
;
495 nt_auth_identity
.User
= user
;
496 nt_auth_identity
.UserLength
= strlenW(nt_auth_identity
.User
);
497 nt_auth_identity
.Domain
= domain
;
498 nt_auth_identity
.DomainLength
= domain
? user
- domain
- 1 : 0;
499 nt_auth_identity
.Password
= password
;
500 nt_auth_identity
.PasswordLength
= strlenW(nt_auth_identity
.Password
);
503 /* use default credentials */
506 sec_status
= AcquireCredentialsHandleW(NULL
, pAuthInfo
->scheme
,
507 SECPKG_CRED_OUTBOUND
, NULL
,
509 NULL
, &pAuthInfo
->cred
,
511 if (sec_status
== SEC_E_OK
)
513 PSecPkgInfoW sec_pkg_info
;
514 sec_status
= QuerySecurityPackageInfoW(pAuthInfo
->scheme
, &sec_pkg_info
);
515 if (sec_status
== SEC_E_OK
)
517 pAuthInfo
->max_token
= sec_pkg_info
->cbMaxToken
;
518 FreeContextBuffer(sec_pkg_info
);
521 if (sec_status
!= SEC_E_OK
)
523 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
524 debugstr_w(pAuthInfo
->scheme
), sec_status
);
525 HeapFree(GetProcessHeap(), 0, pAuthInfo
->scheme
);
526 HeapFree(GetProcessHeap(), 0, pAuthInfo
);
530 *ppAuthInfo
= pAuthInfo
;
532 else if (pAuthInfo
->finished
)
535 if ((strlenW(pszAuthValue
) < strlenW(pAuthInfo
->scheme
)) ||
536 strncmpiW(pszAuthValue
, pAuthInfo
->scheme
, strlenW(pAuthInfo
->scheme
)))
538 ERR("authentication scheme changed from %s to %s\n",
539 debugstr_w(pAuthInfo
->scheme
), debugstr_w(pszAuthValue
));
543 if (is_basic_auth_value(pszAuthValue
))
549 TRACE("basic authentication\n");
551 /* we don't cache credentials for basic authentication, so we can't
552 * retrieve them if the application didn't pass us any credentials */
553 if (!domain_and_username
) return FALSE
;
555 userlen
= WideCharToMultiByte(CP_UTF8
, 0, domain_and_username
, lstrlenW(domain_and_username
), NULL
, 0, NULL
, NULL
);
556 passlen
= WideCharToMultiByte(CP_UTF8
, 0, password
, lstrlenW(password
), NULL
, 0, NULL
, NULL
);
558 /* length includes a nul terminator, which will be re-used for the ':' */
559 auth_data
= HeapAlloc(GetProcessHeap(), 0, userlen
+ 1 + passlen
);
563 WideCharToMultiByte(CP_UTF8
, 0, domain_and_username
, -1, auth_data
, userlen
, NULL
, NULL
);
564 auth_data
[userlen
] = ':';
565 WideCharToMultiByte(CP_UTF8
, 0, password
, -1, &auth_data
[userlen
+1], passlen
, NULL
, NULL
);
567 pAuthInfo
->auth_data
= auth_data
;
568 pAuthInfo
->auth_data_len
= userlen
+ 1 + passlen
;
569 pAuthInfo
->finished
= TRUE
;
576 SecBufferDesc out_desc
, in_desc
;
578 unsigned char *buffer
;
579 ULONG context_req
= ISC_REQ_CONNECTION
| ISC_REQ_USE_DCE_STYLE
|
580 ISC_REQ_MUTUAL_AUTH
| ISC_REQ_DELEGATE
;
582 in
.BufferType
= SECBUFFER_TOKEN
;
586 in_desc
.ulVersion
= 0;
587 in_desc
.cBuffers
= 1;
588 in_desc
.pBuffers
= &in
;
590 pszAuthData
= pszAuthValue
+ strlenW(pAuthInfo
->scheme
);
591 if (*pszAuthData
== ' ')
594 in
.cbBuffer
= HTTP_DecodeBase64(pszAuthData
, NULL
);
595 in
.pvBuffer
= HeapAlloc(GetProcessHeap(), 0, in
.cbBuffer
);
596 HTTP_DecodeBase64(pszAuthData
, in
.pvBuffer
);
599 buffer
= HeapAlloc(GetProcessHeap(), 0, pAuthInfo
->max_token
);
601 out
.BufferType
= SECBUFFER_TOKEN
;
602 out
.cbBuffer
= pAuthInfo
->max_token
;
603 out
.pvBuffer
= buffer
;
605 out_desc
.ulVersion
= 0;
606 out_desc
.cBuffers
= 1;
607 out_desc
.pBuffers
= &out
;
609 sec_status
= InitializeSecurityContextW(first
? &pAuthInfo
->cred
: NULL
,
610 first
? NULL
: &pAuthInfo
->ctx
,
611 first
? lpwhr
->lpHttpSession
->lpszServerName
: NULL
,
612 context_req
, 0, SECURITY_NETWORK_DREP
,
613 in
.pvBuffer
? &in_desc
: NULL
,
614 0, &pAuthInfo
->ctx
, &out_desc
,
615 &pAuthInfo
->attr
, &pAuthInfo
->exp
);
616 if (sec_status
== SEC_E_OK
)
618 pAuthInfo
->finished
= TRUE
;
619 pAuthInfo
->auth_data
= out
.pvBuffer
;
620 pAuthInfo
->auth_data_len
= out
.cbBuffer
;
621 TRACE("sending last auth packet\n");
623 else if (sec_status
== SEC_I_CONTINUE_NEEDED
)
625 pAuthInfo
->auth_data
= out
.pvBuffer
;
626 pAuthInfo
->auth_data_len
= out
.cbBuffer
;
627 TRACE("sending next auth packet\n");
631 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status
);
632 pAuthInfo
->finished
= TRUE
;
633 HeapFree(GetProcessHeap(), 0, out
.pvBuffer
);
641 /***********************************************************************
642 * HTTP_HttpAddRequestHeadersW (internal)
644 static BOOL
HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr
,
645 LPCWSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
650 BOOL bSuccess
= FALSE
;
653 TRACE("copying header: %s\n", debugstr_wn(lpszHeader
, dwHeaderLength
));
655 if( dwHeaderLength
== ~0U )
656 len
= strlenW(lpszHeader
);
658 len
= dwHeaderLength
;
659 buffer
= HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR
)*(len
+1) );
660 lstrcpynW( buffer
, lpszHeader
, len
+ 1);
666 LPWSTR
* pFieldAndValue
;
670 while (*lpszEnd
!= '\0')
672 if (*lpszEnd
== '\r' && *(lpszEnd
+ 1) == '\n')
677 if (*lpszStart
== '\0')
680 if (*lpszEnd
== '\r')
683 lpszEnd
+= 2; /* Jump over \r\n */
685 TRACE("interpreting header %s\n", debugstr_w(lpszStart
));
686 if (*lpszStart
== '\0')
688 /* Skip 0-length headers */
693 pFieldAndValue
= HTTP_InterpretHttpHeader(lpszStart
);
696 bSuccess
= HTTP_VerifyValidHeader(lpwhr
, pFieldAndValue
[0]);
698 bSuccess
= HTTP_ProcessHeader(lpwhr
, pFieldAndValue
[0],
699 pFieldAndValue
[1], dwModifier
| HTTP_ADDHDR_FLAG_REQ
);
700 HTTP_FreeTokens(pFieldAndValue
);
706 HeapFree(GetProcessHeap(), 0, buffer
);
711 /***********************************************************************
712 * HttpAddRequestHeadersW (WININET.@)
714 * Adds one or more HTTP header to the request handler
717 * On Windows if dwHeaderLength includes the trailing '\0', then
718 * HttpAddRequestHeadersW() adds it too. However this results in an
719 * invalid Http header which is rejected by some servers so we probably
720 * don't need to match Windows on that point.
727 BOOL WINAPI
HttpAddRequestHeadersW(HINTERNET hHttpRequest
,
728 LPCWSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
730 BOOL bSuccess
= FALSE
;
731 LPWININETHTTPREQW lpwhr
;
733 TRACE("%p, %s, %i, %i\n", hHttpRequest
, debugstr_wn(lpszHeader
, dwHeaderLength
), dwHeaderLength
, dwModifier
);
738 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hHttpRequest
);
739 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
741 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
744 bSuccess
= HTTP_HttpAddRequestHeadersW( lpwhr
, lpszHeader
, dwHeaderLength
, dwModifier
);
747 WININET_Release( &lpwhr
->hdr
);
752 /***********************************************************************
753 * HttpAddRequestHeadersA (WININET.@)
755 * Adds one or more HTTP header to the request handler
762 BOOL WINAPI
HttpAddRequestHeadersA(HINTERNET hHttpRequest
,
763 LPCSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
769 TRACE("%p, %s, %i, %i\n", hHttpRequest
, debugstr_an(lpszHeader
, dwHeaderLength
), dwHeaderLength
, dwModifier
);
771 len
= MultiByteToWideChar( CP_ACP
, 0, lpszHeader
, dwHeaderLength
, NULL
, 0 );
772 hdr
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
773 MultiByteToWideChar( CP_ACP
, 0, lpszHeader
, dwHeaderLength
, hdr
, len
);
774 if( dwHeaderLength
!= ~0U )
775 dwHeaderLength
= len
;
777 r
= HttpAddRequestHeadersW( hHttpRequest
, hdr
, dwHeaderLength
, dwModifier
);
779 HeapFree( GetProcessHeap(), 0, hdr
);
784 /***********************************************************************
785 * HttpEndRequestA (WININET.@)
787 * Ends an HTTP request that was started by HttpSendRequestEx
794 BOOL WINAPI
HttpEndRequestA(HINTERNET hRequest
,
795 LPINTERNET_BUFFERSA lpBuffersOut
, DWORD dwFlags
, DWORD_PTR dwContext
)
797 TRACE("(%p, %p, %08x, %08lx)\n", hRequest
, lpBuffersOut
, dwFlags
, dwContext
);
801 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
805 return HttpEndRequestW(hRequest
, NULL
, dwFlags
, dwContext
);
808 static BOOL
HTTP_HttpEndRequestW(LPWININETHTTPREQW lpwhr
, DWORD dwFlags
, DWORD_PTR dwContext
)
813 INTERNET_ASYNC_RESULT iar
;
815 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
816 INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
818 responseLen
= HTTP_GetResponseHeaders(lpwhr
, TRUE
);
822 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
823 INTERNET_STATUS_RESPONSE_RECEIVED
, &responseLen
, sizeof(DWORD
));
825 /* process cookies here. Is this right? */
826 HTTP_ProcessCookies(lpwhr
);
828 if (!set_content_length( lpwhr
)) HTTP_FinishedReading(lpwhr
);
830 if (!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTO_REDIRECT
))
832 DWORD dwCode
,dwCodeLength
= sizeof(DWORD
);
833 if (HTTP_HttpQueryInfoW(lpwhr
, HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_STATUS_CODE
, &dwCode
, &dwCodeLength
, NULL
) &&
834 (dwCode
== 302 || dwCode
== 301 || dwCode
== 303))
836 WCHAR
*new_url
, szNewLocation
[INTERNET_MAX_URL_LENGTH
];
837 dwBufferSize
=sizeof(szNewLocation
);
838 if (HTTP_HttpQueryInfoW(lpwhr
, HTTP_QUERY_LOCATION
, szNewLocation
, &dwBufferSize
, NULL
))
840 /* redirects are always GETs */
841 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszVerb
);
842 lpwhr
->lpszVerb
= WININET_strdupW(szGET
);
843 HTTP_DrainContent(lpwhr
);
844 if ((new_url
= HTTP_GetRedirectURL( lpwhr
, szNewLocation
)))
846 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
, INTERNET_STATUS_REDIRECT
,
847 new_url
, (strlenW(new_url
) + 1) * sizeof(WCHAR
));
848 rc
= HTTP_HandleRedirect(lpwhr
, new_url
);
850 rc
= HTTP_HttpSendRequestW(lpwhr
, NULL
, 0, NULL
, 0, 0, TRUE
);
851 HeapFree( GetProcessHeap(), 0, new_url
);
857 iar
.dwResult
= (DWORD_PTR
)lpwhr
->hdr
.hInternet
;
858 iar
.dwError
= rc
? 0 : INTERNET_GetLastError();
860 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
861 INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
862 sizeof(INTERNET_ASYNC_RESULT
));
866 static void AsyncHttpEndRequestProc(WORKREQUEST
*work
)
868 struct WORKREQ_HTTPENDREQUESTW
const *req
= &work
->u
.HttpEndRequestW
;
869 LPWININETHTTPREQW lpwhr
= (LPWININETHTTPREQW
)work
->hdr
;
871 TRACE("%p\n", lpwhr
);
873 HTTP_HttpEndRequestW(lpwhr
, req
->dwFlags
, req
->dwContext
);
876 /***********************************************************************
877 * HttpEndRequestW (WININET.@)
879 * Ends an HTTP request that was started by HttpSendRequestEx
886 BOOL WINAPI
HttpEndRequestW(HINTERNET hRequest
,
887 LPINTERNET_BUFFERSW lpBuffersOut
, DWORD dwFlags
, DWORD_PTR dwContext
)
890 LPWININETHTTPREQW lpwhr
;
896 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
900 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hRequest
);
902 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
904 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
906 WININET_Release( &lpwhr
->hdr
);
909 lpwhr
->hdr
.dwFlags
|= dwFlags
;
911 if (lpwhr
->lpHttpSession
->lpAppInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
914 struct WORKREQ_HTTPENDREQUESTW
*request
;
916 work
.asyncproc
= AsyncHttpEndRequestProc
;
917 work
.hdr
= WININET_AddRef( &lpwhr
->hdr
);
919 request
= &work
.u
.HttpEndRequestW
;
920 request
->dwFlags
= dwFlags
;
921 request
->dwContext
= dwContext
;
923 INTERNET_AsyncCall(&work
);
924 INTERNET_SetLastError(ERROR_IO_PENDING
);
927 rc
= HTTP_HttpEndRequestW(lpwhr
, dwFlags
, dwContext
);
929 WININET_Release( &lpwhr
->hdr
);
930 TRACE("%i <--\n",rc
);
934 /***********************************************************************
935 * HttpOpenRequestW (WININET.@)
937 * Open a HTTP request handle
940 * HINTERNET a HTTP request handle on success
944 HINTERNET WINAPI
HttpOpenRequestW(HINTERNET hHttpSession
,
945 LPCWSTR lpszVerb
, LPCWSTR lpszObjectName
, LPCWSTR lpszVersion
,
946 LPCWSTR lpszReferrer
, LPCWSTR
*lpszAcceptTypes
,
947 DWORD dwFlags
, DWORD_PTR dwContext
)
949 LPWININETHTTPSESSIONW lpwhs
;
950 HINTERNET handle
= NULL
;
952 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession
,
953 debugstr_w(lpszVerb
), debugstr_w(lpszObjectName
),
954 debugstr_w(lpszVersion
), debugstr_w(lpszReferrer
), lpszAcceptTypes
,
956 if(lpszAcceptTypes
!=NULL
)
959 for(i
=0;lpszAcceptTypes
[i
]!=NULL
;i
++)
960 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes
[i
]));
963 lpwhs
= (LPWININETHTTPSESSIONW
) WININET_GetObject( hHttpSession
);
964 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
966 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
971 * My tests seem to show that the windows version does not
972 * become asynchronous until after this point. And anyhow
973 * if this call was asynchronous then how would you get the
974 * necessary HINTERNET pointer returned by this function.
977 handle
= HTTP_HttpOpenRequestW(lpwhs
, lpszVerb
, lpszObjectName
,
978 lpszVersion
, lpszReferrer
, lpszAcceptTypes
,
982 WININET_Release( &lpwhs
->hdr
);
983 TRACE("returning %p\n", handle
);
988 /***********************************************************************
989 * HttpOpenRequestA (WININET.@)
991 * Open a HTTP request handle
994 * HINTERNET a HTTP request handle on success
998 HINTERNET WINAPI
HttpOpenRequestA(HINTERNET hHttpSession
,
999 LPCSTR lpszVerb
, LPCSTR lpszObjectName
, LPCSTR lpszVersion
,
1000 LPCSTR lpszReferrer
, LPCSTR
*lpszAcceptTypes
,
1001 DWORD dwFlags
, DWORD_PTR dwContext
)
1003 LPWSTR szVerb
= NULL
, szObjectName
= NULL
;
1004 LPWSTR szVersion
= NULL
, szReferrer
= NULL
, *szAcceptTypes
= NULL
;
1005 INT len
, acceptTypesCount
;
1006 HINTERNET rc
= FALSE
;
1009 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession
,
1010 debugstr_a(lpszVerb
), debugstr_a(lpszObjectName
),
1011 debugstr_a(lpszVersion
), debugstr_a(lpszReferrer
), lpszAcceptTypes
,
1012 dwFlags
, dwContext
);
1016 len
= MultiByteToWideChar(CP_ACP
, 0, lpszVerb
, -1, NULL
, 0 );
1017 szVerb
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
1020 MultiByteToWideChar(CP_ACP
, 0, lpszVerb
, -1, szVerb
, len
);
1025 len
= MultiByteToWideChar(CP_ACP
, 0, lpszObjectName
, -1, NULL
, 0 );
1026 szObjectName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
1027 if ( !szObjectName
)
1029 MultiByteToWideChar(CP_ACP
, 0, lpszObjectName
, -1, szObjectName
, len
);
1034 len
= MultiByteToWideChar(CP_ACP
, 0, lpszVersion
, -1, NULL
, 0 );
1035 szVersion
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1038 MultiByteToWideChar(CP_ACP
, 0, lpszVersion
, -1, szVersion
, len
);
1043 len
= MultiByteToWideChar(CP_ACP
, 0, lpszReferrer
, -1, NULL
, 0 );
1044 szReferrer
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1047 MultiByteToWideChar(CP_ACP
, 0, lpszReferrer
, -1, szReferrer
, len
);
1050 if (lpszAcceptTypes
)
1052 acceptTypesCount
= 0;
1053 types
= lpszAcceptTypes
;
1058 /* find out how many there are */
1059 if (*types
&& **types
)
1061 TRACE("accept type: %s\n", debugstr_a(*types
));
1067 WARN("invalid accept type pointer\n");
1072 szAcceptTypes
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
*) * (acceptTypesCount
+1));
1073 if (!szAcceptTypes
) goto end
;
1075 acceptTypesCount
= 0;
1076 types
= lpszAcceptTypes
;
1081 if (*types
&& **types
)
1083 len
= MultiByteToWideChar(CP_ACP
, 0, *types
, -1, NULL
, 0 );
1084 szAcceptTypes
[acceptTypesCount
] = HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1086 MultiByteToWideChar(CP_ACP
, 0, *types
, -1, szAcceptTypes
[acceptTypesCount
], len
);
1092 /* ignore invalid pointer */
1097 szAcceptTypes
[acceptTypesCount
] = NULL
;
1100 rc
= HttpOpenRequestW(hHttpSession
, szVerb
, szObjectName
,
1101 szVersion
, szReferrer
,
1102 (LPCWSTR
*)szAcceptTypes
, dwFlags
, dwContext
);
1107 acceptTypesCount
= 0;
1108 while (szAcceptTypes
[acceptTypesCount
])
1110 HeapFree(GetProcessHeap(), 0, szAcceptTypes
[acceptTypesCount
]);
1113 HeapFree(GetProcessHeap(), 0, szAcceptTypes
);
1115 HeapFree(GetProcessHeap(), 0, szReferrer
);
1116 HeapFree(GetProcessHeap(), 0, szVersion
);
1117 HeapFree(GetProcessHeap(), 0, szObjectName
);
1118 HeapFree(GetProcessHeap(), 0, szVerb
);
1123 /***********************************************************************
1126 static UINT
HTTP_EncodeBase64( LPCSTR bin
, unsigned int len
, LPWSTR base64
)
1129 static const CHAR HTTP_Base64Enc
[] =
1130 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1134 /* first 6 bits, all from bin[0] */
1135 base64
[n
++] = HTTP_Base64Enc
[(bin
[0] & 0xfc) >> 2];
1136 x
= (bin
[0] & 3) << 4;
1138 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1141 base64
[n
++] = HTTP_Base64Enc
[x
];
1146 base64
[n
++] = HTTP_Base64Enc
[ x
| ( (bin
[1]&0xf0) >> 4 ) ];
1147 x
= ( bin
[1] & 0x0f ) << 2;
1149 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1152 base64
[n
++] = HTTP_Base64Enc
[x
];
1156 base64
[n
++] = HTTP_Base64Enc
[ x
| ( (bin
[2]&0xc0 ) >> 6 ) ];
1158 /* last 6 bits, all from bin [2] */
1159 base64
[n
++] = HTTP_Base64Enc
[ bin
[2] & 0x3f ];
1167 #define CH(x) (((x) >= 'A' && (x) <= 'Z') ? (x) - 'A' : \
1168 ((x) >= 'a' && (x) <= 'z') ? (x) - 'a' + 26 : \
1169 ((x) >= '0' && (x) <= '9') ? (x) - '0' + 52 : \
1170 ((x) == '+') ? 62 : ((x) == '/') ? 63 : -1)
1171 static const signed char HTTP_Base64Dec
[256] =
1173 CH( 0),CH( 1),CH( 2),CH( 3),CH( 4),CH( 5),CH( 6),CH( 7),CH( 8),CH( 9),
1174 CH(10),CH(11),CH(12),CH(13),CH(14),CH(15),CH(16),CH(17),CH(18),CH(19),
1175 CH(20),CH(21),CH(22),CH(23),CH(24),CH(25),CH(26),CH(27),CH(28),CH(29),
1176 CH(30),CH(31),CH(32),CH(33),CH(34),CH(35),CH(36),CH(37),CH(38),CH(39),
1177 CH(40),CH(41),CH(42),CH(43),CH(44),CH(45),CH(46),CH(47),CH(48),CH(49),
1178 CH(50),CH(51),CH(52),CH(53),CH(54),CH(55),CH(56),CH(57),CH(58),CH(59),
1179 CH(60),CH(61),CH(62),CH(63),CH(64),CH(65),CH(66),CH(67),CH(68),CH(69),
1180 CH(70),CH(71),CH(72),CH(73),CH(74),CH(75),CH(76),CH(77),CH(78),CH(79),
1181 CH(80),CH(81),CH(82),CH(83),CH(84),CH(85),CH(86),CH(87),CH(88),CH(89),
1182 CH(90),CH(91),CH(92),CH(93),CH(94),CH(95),CH(96),CH(97),CH(98),CH(99),
1183 CH(100),CH(101),CH(102),CH(103),CH(104),CH(105),CH(106),CH(107),CH(108),CH(109),
1184 CH(110),CH(111),CH(112),CH(113),CH(114),CH(115),CH(116),CH(117),CH(118),CH(119),
1185 CH(120),CH(121),CH(122),CH(123),CH(124),CH(125),CH(126),CH(127),CH(128),CH(129),
1186 CH(130),CH(131),CH(132),CH(133),CH(134),CH(135),CH(136),CH(137),CH(138),CH(139),
1187 CH(140),CH(141),CH(142),CH(143),CH(144),CH(145),CH(146),CH(147),CH(148),CH(149),
1188 CH(150),CH(151),CH(152),CH(153),CH(154),CH(155),CH(156),CH(157),CH(158),CH(159),
1189 CH(160),CH(161),CH(162),CH(163),CH(164),CH(165),CH(166),CH(167),CH(168),CH(169),
1190 CH(170),CH(171),CH(172),CH(173),CH(174),CH(175),CH(176),CH(177),CH(178),CH(179),
1191 CH(180),CH(181),CH(182),CH(183),CH(184),CH(185),CH(186),CH(187),CH(188),CH(189),
1192 CH(190),CH(191),CH(192),CH(193),CH(194),CH(195),CH(196),CH(197),CH(198),CH(199),
1193 CH(200),CH(201),CH(202),CH(203),CH(204),CH(205),CH(206),CH(207),CH(208),CH(209),
1194 CH(210),CH(211),CH(212),CH(213),CH(214),CH(215),CH(216),CH(217),CH(218),CH(219),
1195 CH(220),CH(221),CH(222),CH(223),CH(224),CH(225),CH(226),CH(227),CH(228),CH(229),
1196 CH(230),CH(231),CH(232),CH(233),CH(234),CH(235),CH(236),CH(237),CH(238),CH(239),
1197 CH(240),CH(241),CH(242),CH(243),CH(244),CH(245),CH(246),CH(247),CH(248), CH(249),
1198 CH(250),CH(251),CH(252),CH(253),CH(254),CH(255),
1202 /***********************************************************************
1205 static UINT
HTTP_DecodeBase64( LPCWSTR base64
, LPSTR bin
)
1213 if (base64
[0] >= ARRAYSIZE(HTTP_Base64Dec
) ||
1214 ((in
[0] = HTTP_Base64Dec
[base64
[0]]) == -1) ||
1215 base64
[1] >= ARRAYSIZE(HTTP_Base64Dec
) ||
1216 ((in
[1] = HTTP_Base64Dec
[base64
[1]]) == -1))
1218 WARN("invalid base64: %s\n", debugstr_w(base64
));
1222 bin
[n
] = (unsigned char) (in
[0] << 2 | in
[1] >> 4);
1225 if ((base64
[2] == '=') && (base64
[3] == '='))
1227 if (base64
[2] > ARRAYSIZE(HTTP_Base64Dec
) ||
1228 ((in
[2] = HTTP_Base64Dec
[base64
[2]]) == -1))
1230 WARN("invalid base64: %s\n", debugstr_w(&base64
[2]));
1234 bin
[n
] = (unsigned char) (in
[1] << 4 | in
[2] >> 2);
1237 if (base64
[3] == '=')
1239 if (base64
[3] > ARRAYSIZE(HTTP_Base64Dec
) ||
1240 ((in
[3] = HTTP_Base64Dec
[base64
[3]]) == -1))
1242 WARN("invalid base64: %s\n", debugstr_w(&base64
[3]));
1246 bin
[n
] = (unsigned char) (((in
[2] << 6) & 0xc0) | in
[3]);
1255 /***********************************************************************
1256 * HTTP_InsertAuthorization
1258 * Insert or delete the authorization field in the request header.
1260 static BOOL
HTTP_InsertAuthorization( LPWININETHTTPREQW lpwhr
, struct HttpAuthInfo
*pAuthInfo
, LPCWSTR header
)
1264 static const WCHAR wszSpace
[] = {' ',0};
1265 static const WCHAR wszBasic
[] = {'B','a','s','i','c',0};
1267 WCHAR
*authorization
= NULL
;
1269 if (pAuthInfo
->auth_data_len
)
1271 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1272 len
= strlenW(pAuthInfo
->scheme
)+1+((pAuthInfo
->auth_data_len
+2)*4)/3;
1273 authorization
= HeapAlloc(GetProcessHeap(), 0, (len
+1)*sizeof(WCHAR
));
1277 strcpyW(authorization
, pAuthInfo
->scheme
);
1278 strcatW(authorization
, wszSpace
);
1279 HTTP_EncodeBase64(pAuthInfo
->auth_data
,
1280 pAuthInfo
->auth_data_len
,
1281 authorization
+strlenW(authorization
));
1283 /* clear the data as it isn't valid now that it has been sent to the
1284 * server, unless it's Basic authentication which doesn't do
1285 * connection tracking */
1286 if (strcmpiW(pAuthInfo
->scheme
, wszBasic
))
1288 HeapFree(GetProcessHeap(), 0, pAuthInfo
->auth_data
);
1289 pAuthInfo
->auth_data
= NULL
;
1290 pAuthInfo
->auth_data_len
= 0;
1294 TRACE("Inserting authorization: %s\n", debugstr_w(authorization
));
1296 HTTP_ProcessHeader(lpwhr
, header
, authorization
, HTTP_ADDHDR_FLAG_REQ
| HTTP_ADDHDR_FLAG_REPLACE
);
1298 HeapFree(GetProcessHeap(), 0, authorization
);
1303 static WCHAR
*HTTP_BuildProxyRequestUrl(WININETHTTPREQW
*req
)
1305 WCHAR new_location
[INTERNET_MAX_URL_LENGTH
], *url
;
1308 size
= sizeof(new_location
);
1309 if (HTTP_HttpQueryInfoW(req
, HTTP_QUERY_LOCATION
, new_location
, &size
, NULL
))
1311 if (!(url
= HeapAlloc( GetProcessHeap(), 0, size
+ sizeof(WCHAR
) ))) return NULL
;
1312 strcpyW( url
, new_location
);
1316 static const WCHAR slash
[] = { '/',0 };
1317 static const WCHAR format
[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
1318 static const WCHAR formatSSL
[] = { 'h','t','t','p','s',':','/','/','%','s',':','%','d',0 };
1319 WININETHTTPSESSIONW
*session
= req
->lpHttpSession
;
1321 size
= 16; /* "https://" + sizeof(port#) + ":/\0" */
1322 size
+= strlenW( session
->lpszHostName
) + strlenW( req
->lpszPath
);
1324 if (!(url
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) ))) return NULL
;
1326 if (req
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
)
1327 sprintfW( url
, formatSSL
, session
->lpszHostName
, session
->nHostPort
);
1329 sprintfW( url
, format
, session
->lpszHostName
, session
->nHostPort
);
1330 if (req
->lpszPath
[0] != '/') strcatW( url
, slash
);
1331 strcatW( url
, req
->lpszPath
);
1333 TRACE("url=%s\n", debugstr_w(url
));
1337 /***********************************************************************
1338 * HTTP_DealWithProxy
1340 static BOOL
HTTP_DealWithProxy( LPWININETAPPINFOW hIC
,
1341 LPWININETHTTPSESSIONW lpwhs
, LPWININETHTTPREQW lpwhr
)
1343 WCHAR buf
[MAXHOSTNAME
];
1344 WCHAR proxy
[MAXHOSTNAME
+ 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
1345 static WCHAR szNul
[] = { 0 };
1346 URL_COMPONENTSW UrlComponents
;
1347 static const WCHAR szHttp
[] = { 'h','t','t','p',':','/','/',0 };
1348 static const WCHAR szFormat
[] = { 'h','t','t','p',':','/','/','%','s',0 };
1350 memset( &UrlComponents
, 0, sizeof UrlComponents
);
1351 UrlComponents
.dwStructSize
= sizeof UrlComponents
;
1352 UrlComponents
.lpszHostName
= buf
;
1353 UrlComponents
.dwHostNameLength
= MAXHOSTNAME
;
1355 if( CSTR_EQUAL
!= CompareStringW(LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
,
1356 hIC
->lpszProxy
,strlenW(szHttp
),szHttp
,strlenW(szHttp
)) )
1357 sprintfW(proxy
, szFormat
, hIC
->lpszProxy
);
1359 strcpyW(proxy
, hIC
->lpszProxy
);
1360 if( !InternetCrackUrlW(proxy
, 0, 0, &UrlComponents
) )
1362 if( UrlComponents
.dwHostNameLength
== 0 )
1365 if( !lpwhr
->lpszPath
)
1366 lpwhr
->lpszPath
= szNul
;
1368 if(UrlComponents
.nPort
== INTERNET_INVALID_PORT_NUMBER
)
1369 UrlComponents
.nPort
= INTERNET_DEFAULT_HTTP_PORT
;
1371 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszServerName
);
1372 lpwhs
->lpszServerName
= WININET_strdupW(UrlComponents
.lpszHostName
);
1373 lpwhs
->nServerPort
= UrlComponents
.nPort
;
1375 TRACE("proxy server=%s port=%d\n", debugstr_w(lpwhs
->lpszServerName
), lpwhs
->nServerPort
);
1379 static BOOL
HTTP_ResolveName(LPWININETHTTPREQW lpwhr
)
1382 LPWININETHTTPSESSIONW lpwhs
= lpwhr
->lpHttpSession
;
1384 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
1385 INTERNET_STATUS_RESOLVING_NAME
,
1386 lpwhs
->lpszServerName
,
1387 strlenW(lpwhs
->lpszServerName
)+1);
1389 if (!GetAddress(lpwhs
->lpszServerName
, lpwhs
->nServerPort
,
1390 &lpwhs
->socketAddress
))
1392 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED
);
1396 inet_ntop(lpwhs
->socketAddress
.sin_family
, &lpwhs
->socketAddress
.sin_addr
,
1397 szaddr
, sizeof(szaddr
));
1398 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
1399 INTERNET_STATUS_NAME_RESOLVED
,
1400 szaddr
, strlen(szaddr
)+1);
1402 TRACE("resolved %s to %s\n", debugstr_w(lpwhs
->lpszServerName
), szaddr
);
1407 /***********************************************************************
1408 * HTTPREQ_Destroy (internal)
1410 * Deallocate request handle
1413 static void HTTPREQ_Destroy(WININETHANDLEHEADER
*hdr
)
1415 LPWININETHTTPREQW lpwhr
= (LPWININETHTTPREQW
) hdr
;
1420 if(lpwhr
->hCacheFile
)
1421 CloseHandle(lpwhr
->hCacheFile
);
1423 if(lpwhr
->lpszCacheFile
) {
1424 DeleteFileW(lpwhr
->lpszCacheFile
); /* FIXME */
1425 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszCacheFile
);
1428 DeleteCriticalSection( &lpwhr
->read_section
);
1429 WININET_Release(&lpwhr
->lpHttpSession
->hdr
);
1431 if (lpwhr
->pAuthInfo
)
1433 if (SecIsValidHandle(&lpwhr
->pAuthInfo
->ctx
))
1434 DeleteSecurityContext(&lpwhr
->pAuthInfo
->ctx
);
1435 if (SecIsValidHandle(&lpwhr
->pAuthInfo
->cred
))
1436 FreeCredentialsHandle(&lpwhr
->pAuthInfo
->cred
);
1438 HeapFree(GetProcessHeap(), 0, lpwhr
->pAuthInfo
->auth_data
);
1439 HeapFree(GetProcessHeap(), 0, lpwhr
->pAuthInfo
->scheme
);
1440 HeapFree(GetProcessHeap(), 0, lpwhr
->pAuthInfo
);
1441 lpwhr
->pAuthInfo
= NULL
;
1444 if (lpwhr
->pProxyAuthInfo
)
1446 if (SecIsValidHandle(&lpwhr
->pProxyAuthInfo
->ctx
))
1447 DeleteSecurityContext(&lpwhr
->pProxyAuthInfo
->ctx
);
1448 if (SecIsValidHandle(&lpwhr
->pProxyAuthInfo
->cred
))
1449 FreeCredentialsHandle(&lpwhr
->pProxyAuthInfo
->cred
);
1451 HeapFree(GetProcessHeap(), 0, lpwhr
->pProxyAuthInfo
->auth_data
);
1452 HeapFree(GetProcessHeap(), 0, lpwhr
->pProxyAuthInfo
->scheme
);
1453 HeapFree(GetProcessHeap(), 0, lpwhr
->pProxyAuthInfo
);
1454 lpwhr
->pProxyAuthInfo
= NULL
;
1457 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszPath
);
1458 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszVerb
);
1459 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszRawHeaders
);
1460 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszVersion
);
1461 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszStatusText
);
1463 for (i
= 0; i
< lpwhr
->nCustHeaders
; i
++)
1465 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
[i
].lpszField
);
1466 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
[i
].lpszValue
);
1469 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
);
1470 HeapFree(GetProcessHeap(), 0, lpwhr
);
1473 static void HTTPREQ_CloseConnection(WININETHANDLEHEADER
*hdr
)
1475 LPWININETHTTPREQW lpwhr
= (LPWININETHTTPREQW
) hdr
;
1477 TRACE("%p\n",lpwhr
);
1479 if (!NETCON_connected(&lpwhr
->netConnection
))
1482 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
1483 INTERNET_STATUS_CLOSING_CONNECTION
, 0, 0);
1485 NETCON_close(&lpwhr
->netConnection
);
1487 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
1488 INTERNET_STATUS_CONNECTION_CLOSED
, 0, 0);
1491 static DWORD
HTTPREQ_QueryOption(WININETHANDLEHEADER
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
1493 WININETHTTPREQW
*req
= (WININETHTTPREQW
*)hdr
;
1496 case INTERNET_OPTION_HANDLE_TYPE
:
1497 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
1499 if (*size
< sizeof(ULONG
))
1500 return ERROR_INSUFFICIENT_BUFFER
;
1502 *size
= sizeof(DWORD
);
1503 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_HTTP_REQUEST
;
1504 return ERROR_SUCCESS
;
1506 case INTERNET_OPTION_URL
: {
1507 WCHAR url
[INTERNET_MAX_URL_LENGTH
];
1512 static const WCHAR httpW
[] = {'h','t','t','p',':','/','/',0};
1514 TRACE("INTERNET_OPTION_URL\n");
1516 host
= HTTP_GetHeader(req
, hostW
);
1517 strcpyW(url
, httpW
);
1518 strcatW(url
, host
->lpszValue
);
1519 if (NULL
!= (pch
= strchrW(url
+ strlenW(httpW
), ':')))
1521 strcatW(url
, req
->lpszPath
);
1523 TRACE("INTERNET_OPTION_URL: %s\n",debugstr_w(url
));
1526 len
= (strlenW(url
)+1) * sizeof(WCHAR
);
1528 return ERROR_INSUFFICIENT_BUFFER
;
1531 strcpyW(buffer
, url
);
1532 return ERROR_SUCCESS
;
1534 len
= WideCharToMultiByte(CP_ACP
, 0, url
, -1, buffer
, *size
, NULL
, NULL
);
1536 return ERROR_INSUFFICIENT_BUFFER
;
1539 return ERROR_SUCCESS
;
1543 case INTERNET_OPTION_DATAFILE_NAME
: {
1546 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
1548 if(!req
->lpszCacheFile
) {
1550 return ERROR_INTERNET_ITEM_NOT_FOUND
;
1554 req_size
= (lstrlenW(req
->lpszCacheFile
)+1) * sizeof(WCHAR
);
1555 if(*size
< req_size
)
1556 return ERROR_INSUFFICIENT_BUFFER
;
1559 memcpy(buffer
, req
->lpszCacheFile
, *size
);
1560 return ERROR_SUCCESS
;
1562 req_size
= WideCharToMultiByte(CP_ACP
, 0, req
->lpszCacheFile
, -1, NULL
, 0, NULL
, NULL
);
1563 if (req_size
> *size
)
1564 return ERROR_INSUFFICIENT_BUFFER
;
1566 *size
= WideCharToMultiByte(CP_ACP
, 0, req
->lpszCacheFile
,
1567 -1, buffer
, *size
, NULL
, NULL
);
1568 return ERROR_SUCCESS
;
1572 case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT
: {
1573 PCCERT_CONTEXT context
;
1575 if(*size
< sizeof(INTERNET_CERTIFICATE_INFOW
)) {
1576 *size
= sizeof(INTERNET_CERTIFICATE_INFOW
);
1577 return ERROR_INSUFFICIENT_BUFFER
;
1580 context
= (PCCERT_CONTEXT
)NETCON_GetCert(&(req
->netConnection
));
1582 INTERNET_CERTIFICATE_INFOW
*info
= (INTERNET_CERTIFICATE_INFOW
*)buffer
;
1585 memset(info
, 0, sizeof(INTERNET_CERTIFICATE_INFOW
));
1586 info
->ftExpiry
= context
->pCertInfo
->NotAfter
;
1587 info
->ftStart
= context
->pCertInfo
->NotBefore
;
1589 len
= CertNameToStrW(context
->dwCertEncodingType
,
1590 &context
->pCertInfo
->Subject
, CERT_SIMPLE_NAME_STR
, NULL
, 0);
1591 info
->lpszSubjectInfo
= LocalAlloc(0, len
*sizeof(WCHAR
));
1592 if(info
->lpszSubjectInfo
)
1593 CertNameToStrW(context
->dwCertEncodingType
,
1594 &context
->pCertInfo
->Subject
, CERT_SIMPLE_NAME_STR
,
1595 info
->lpszSubjectInfo
, len
);
1596 len
= CertNameToStrW(context
->dwCertEncodingType
,
1597 &context
->pCertInfo
->Issuer
, CERT_SIMPLE_NAME_STR
, NULL
, 0);
1598 info
->lpszIssuerInfo
= LocalAlloc(0, len
*sizeof(WCHAR
));
1599 if (info
->lpszIssuerInfo
)
1600 CertNameToStrW(context
->dwCertEncodingType
,
1601 &context
->pCertInfo
->Issuer
, CERT_SIMPLE_NAME_STR
,
1602 info
->lpszIssuerInfo
, len
);
1604 INTERNET_CERTIFICATE_INFOA
*infoA
= (INTERNET_CERTIFICATE_INFOA
*)info
;
1606 len
= CertNameToStrA(context
->dwCertEncodingType
,
1607 &context
->pCertInfo
->Subject
, CERT_SIMPLE_NAME_STR
, NULL
, 0);
1608 infoA
->lpszSubjectInfo
= LocalAlloc(0, len
);
1609 if(infoA
->lpszSubjectInfo
)
1610 CertNameToStrA(context
->dwCertEncodingType
,
1611 &context
->pCertInfo
->Subject
, CERT_SIMPLE_NAME_STR
,
1612 infoA
->lpszSubjectInfo
, len
);
1613 len
= CertNameToStrA(context
->dwCertEncodingType
,
1614 &context
->pCertInfo
->Issuer
, CERT_SIMPLE_NAME_STR
, NULL
, 0);
1615 infoA
->lpszIssuerInfo
= LocalAlloc(0, len
);
1616 if(infoA
->lpszIssuerInfo
)
1617 CertNameToStrA(context
->dwCertEncodingType
,
1618 &context
->pCertInfo
->Issuer
, CERT_SIMPLE_NAME_STR
,
1619 infoA
->lpszIssuerInfo
, len
);
1623 * Contrary to MSDN, these do not appear to be set.
1625 * lpszSignatureAlgName
1626 * lpszEncryptionAlgName
1629 CertFreeCertificateContext(context
);
1630 return ERROR_SUCCESS
;
1635 return INET_QueryOption(option
, buffer
, size
, unicode
);
1638 static DWORD
HTTPREQ_SetOption(WININETHANDLEHEADER
*hdr
, DWORD option
, void *buffer
, DWORD size
)
1640 WININETHTTPREQW
*req
= (WININETHTTPREQW
*)hdr
;
1643 case INTERNET_OPTION_SEND_TIMEOUT
:
1644 case INTERNET_OPTION_RECEIVE_TIMEOUT
:
1645 TRACE("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT\n");
1647 if (size
!= sizeof(DWORD
))
1648 return ERROR_INVALID_PARAMETER
;
1650 return NETCON_set_timeout(&req
->netConnection
, option
== INTERNET_OPTION_SEND_TIMEOUT
,
1653 case INTERNET_OPTION_USERNAME
:
1654 HeapFree(GetProcessHeap(), 0, req
->lpHttpSession
->lpszUserName
);
1655 if (!(req
->lpHttpSession
->lpszUserName
= WININET_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
1656 return ERROR_SUCCESS
;
1658 case INTERNET_OPTION_PASSWORD
:
1659 HeapFree(GetProcessHeap(), 0, req
->lpHttpSession
->lpszPassword
);
1660 if (!(req
->lpHttpSession
->lpszPassword
= WININET_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
1661 return ERROR_SUCCESS
;
1664 return ERROR_INTERNET_INVALID_OPTION
;
1667 /* read some more data into the read buffer (the read section must be held) */
1668 static BOOL
read_more_data( WININETHTTPREQW
*req
, int maxlen
)
1672 if (req
->read_size
&& req
->read_pos
)
1674 /* move existing data to the start of the buffer */
1675 memmove( req
->read_buf
, req
->read_buf
+ req
->read_pos
, req
->read_size
);
1679 if (maxlen
== -1) maxlen
= sizeof(req
->read_buf
);
1680 if (!NETCON_recv( &req
->netConnection
, req
->read_buf
+ req
->read_size
,
1681 maxlen
- req
->read_size
, 0, &len
)) return FALSE
;
1682 req
->read_size
+= len
;
1686 /* remove some amount of data from the read buffer (the read section must be held) */
1687 static void remove_data( WININETHTTPREQW
*req
, int count
)
1689 if (!(req
->read_size
-= count
)) req
->read_pos
= 0;
1690 else req
->read_pos
+= count
;
1693 static BOOL
read_line( WININETHTTPREQW
*req
, LPSTR buffer
, DWORD
*len
)
1695 int count
, bytes_read
, pos
= 0;
1697 EnterCriticalSection( &req
->read_section
);
1700 char *eol
= memchr( req
->read_buf
+ req
->read_pos
, '\n', req
->read_size
);
1704 count
= eol
- (req
->read_buf
+ req
->read_pos
);
1705 bytes_read
= count
+ 1;
1707 else count
= bytes_read
= req
->read_size
;
1709 count
= min( count
, *len
- pos
);
1710 memcpy( buffer
+ pos
, req
->read_buf
+ req
->read_pos
, count
);
1712 remove_data( req
, bytes_read
);
1715 if (!read_more_data( req
, -1 ) || !req
->read_size
)
1718 TRACE( "returning empty string\n" );
1719 LeaveCriticalSection( &req
->read_section
);
1723 LeaveCriticalSection( &req
->read_section
);
1727 if (pos
&& buffer
[pos
- 1] == '\r') pos
--;
1730 buffer
[*len
- 1] = 0;
1731 TRACE( "returning %s\n", debugstr_a(buffer
));
1735 /* discard data contents until we reach end of line (the read section must be held) */
1736 static BOOL
discard_eol( WININETHTTPREQW
*req
)
1740 char *eol
= memchr( req
->read_buf
+ req
->read_pos
, '\n', req
->read_size
);
1743 remove_data( req
, (eol
+ 1) - (req
->read_buf
+ req
->read_pos
) );
1746 req
->read_pos
= req
->read_size
= 0; /* discard everything */
1747 if (!read_more_data( req
, -1 )) return FALSE
;
1748 } while (req
->read_size
);
1752 /* read the size of the next chunk (the read section must be held) */
1753 static BOOL
start_next_chunk( WININETHTTPREQW
*req
)
1755 DWORD chunk_size
= 0;
1757 if (!req
->dwContentLength
) return TRUE
;
1758 if (req
->dwContentLength
== req
->dwContentRead
)
1760 /* read terminator for the previous chunk */
1761 if (!discard_eol( req
)) return FALSE
;
1762 req
->dwContentLength
= ~0u;
1763 req
->dwContentRead
= 0;
1767 while (req
->read_size
)
1769 char ch
= req
->read_buf
[req
->read_pos
];
1770 if (ch
>= '0' && ch
<= '9') chunk_size
= chunk_size
* 16 + ch
- '0';
1771 else if (ch
>= 'a' && ch
<= 'f') chunk_size
= chunk_size
* 16 + ch
- 'a' + 10;
1772 else if (ch
>= 'A' && ch
<= 'F') chunk_size
= chunk_size
* 16 + ch
- 'A' + 10;
1773 else if (ch
== ';' || ch
== '\r' || ch
== '\n')
1775 TRACE( "reading %u byte chunk\n", chunk_size
);
1776 req
->dwContentLength
= chunk_size
;
1777 req
->dwContentRead
= 0;
1778 if (!discard_eol( req
)) return FALSE
;
1781 remove_data( req
, 1 );
1783 if (!read_more_data( req
, -1 )) return FALSE
;
1784 if (!req
->read_size
)
1786 req
->dwContentLength
= req
->dwContentRead
= 0;
1792 /* return the size of data available to be read immediately (the read section must be held) */
1793 static DWORD
get_avail_data( WININETHTTPREQW
*req
)
1795 if (req
->read_chunked
&& (req
->dwContentLength
== ~0u || req
->dwContentLength
== req
->dwContentRead
))
1797 return min( req
->read_size
, req
->dwContentLength
- req
->dwContentRead
);
1800 /* check if we have reached the end of the data to read (the read section must be held) */
1801 static BOOL
end_of_read_data( WININETHTTPREQW
*req
)
1803 if (req
->read_chunked
) return (req
->dwContentLength
== 0);
1804 if (req
->dwContentLength
== ~0u) return FALSE
;
1805 return (req
->dwContentLength
== req
->dwContentRead
);
1808 /* fetch some more data into the read buffer (the read section must be held) */
1809 static BOOL
refill_buffer( WININETHTTPREQW
*req
)
1811 int len
= sizeof(req
->read_buf
);
1813 if (req
->read_chunked
&& (req
->dwContentLength
== ~0u || req
->dwContentLength
== req
->dwContentRead
))
1815 if (!start_next_chunk( req
)) return FALSE
;
1818 if (req
->dwContentLength
!= ~0u) len
= min( len
, req
->dwContentLength
- req
->dwContentRead
);
1819 if (len
<= req
->read_size
) return TRUE
;
1821 if (!read_more_data( req
, len
)) return FALSE
;
1822 if (!req
->read_size
) req
->dwContentLength
= req
->dwContentRead
= 0;
1826 static void HTTP_ReceiveRequestData(WININETHTTPREQW
*req
, BOOL first_notif
)
1828 INTERNET_ASYNC_RESULT iar
;
1832 EnterCriticalSection( &req
->read_section
);
1833 if (refill_buffer( req
)) {
1834 iar
.dwResult
= (DWORD_PTR
)req
->hdr
.hInternet
;
1835 iar
.dwError
= first_notif
? 0 : get_avail_data(req
);
1838 iar
.dwError
= INTERNET_GetLastError();
1840 LeaveCriticalSection( &req
->read_section
);
1842 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
1843 sizeof(INTERNET_ASYNC_RESULT
));
1846 /* read data from the http connection (the read section must be held) */
1847 static DWORD
HTTPREQ_Read(WININETHTTPREQW
*req
, void *buffer
, DWORD size
, DWORD
*read
, BOOL sync
)
1849 int len
, bytes_read
= 0;
1851 EnterCriticalSection( &req
->read_section
);
1852 if (req
->read_chunked
&& (req
->dwContentLength
== ~0u || req
->dwContentLength
== req
->dwContentRead
))
1854 if (!start_next_chunk( req
)) goto done
;
1856 if (req
->dwContentLength
!= ~0u) size
= min( size
, req
->dwContentLength
- req
->dwContentRead
);
1860 bytes_read
= min( req
->read_size
, size
);
1861 memcpy( buffer
, req
->read_buf
+ req
->read_pos
, bytes_read
);
1862 remove_data( req
, bytes_read
);
1865 if (size
> bytes_read
&& (!bytes_read
|| sync
))
1867 if (NETCON_recv( &req
->netConnection
, (char *)buffer
+ bytes_read
, size
- bytes_read
,
1868 sync
? MSG_WAITALL
: 0, &len
))
1870 /* always return success, even if the network layer returns an error */
1873 req
->dwContentRead
+= bytes_read
;
1876 TRACE( "retrieved %u bytes (%u/%u)\n", bytes_read
, req
->dwContentRead
, req
->dwContentLength
);
1877 LeaveCriticalSection( &req
->read_section
);
1879 if(req
->lpszCacheFile
) {
1881 DWORD dwBytesWritten
;
1883 res
= WriteFile(req
->hCacheFile
, buffer
, bytes_read
, &dwBytesWritten
, NULL
);
1885 WARN("WriteFile failed: %u\n", GetLastError());
1888 if(!bytes_read
&& (req
->dwContentRead
== req
->dwContentLength
))
1889 HTTP_FinishedReading(req
);
1891 return ERROR_SUCCESS
;
1895 static DWORD
HTTPREQ_ReadFile(WININETHANDLEHEADER
*hdr
, void *buffer
, DWORD size
, DWORD
*read
)
1897 WININETHTTPREQW
*req
= (WININETHTTPREQW
*)hdr
;
1898 return HTTPREQ_Read(req
, buffer
, size
, read
, TRUE
);
1901 static void HTTPREQ_AsyncReadFileExAProc(WORKREQUEST
*workRequest
)
1903 struct WORKREQ_INTERNETREADFILEEXA
const *data
= &workRequest
->u
.InternetReadFileExA
;
1904 WININETHTTPREQW
*req
= (WININETHTTPREQW
*)workRequest
->hdr
;
1905 INTERNET_ASYNC_RESULT iar
;
1908 TRACE("INTERNETREADFILEEXA %p\n", workRequest
->hdr
);
1910 res
= HTTPREQ_Read(req
, data
->lpBuffersOut
->lpvBuffer
,
1911 data
->lpBuffersOut
->dwBufferLength
, &data
->lpBuffersOut
->dwBufferLength
, TRUE
);
1913 iar
.dwResult
= res
== ERROR_SUCCESS
;
1916 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
,
1917 INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
1918 sizeof(INTERNET_ASYNC_RESULT
));
1921 static DWORD
HTTPREQ_ReadFileExA(WININETHANDLEHEADER
*hdr
, INTERNET_BUFFERSA
*buffers
,
1922 DWORD flags
, DWORD_PTR context
)
1925 WININETHTTPREQW
*req
= (WININETHTTPREQW
*)hdr
;
1928 if (flags
& ~(IRF_ASYNC
|IRF_NO_WAIT
))
1929 FIXME("these dwFlags aren't implemented: 0x%x\n", flags
& ~(IRF_ASYNC
|IRF_NO_WAIT
));
1931 if (buffers
->dwStructSize
!= sizeof(*buffers
))
1932 return ERROR_INVALID_PARAMETER
;
1934 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
1936 if ((hdr
->dwFlags
& INTERNET_FLAG_ASYNC
) && !get_avail_data(req
))
1938 WORKREQUEST workRequest
;
1940 if (TryEnterCriticalSection( &req
->read_section
))
1942 if (get_avail_data(req
))
1944 res
= HTTPREQ_Read(req
, buffers
->lpvBuffer
, buffers
->dwBufferLength
,
1945 &buffers
->dwBufferLength
, FALSE
);
1946 LeaveCriticalSection( &req
->read_section
);
1949 LeaveCriticalSection( &req
->read_section
);
1952 workRequest
.asyncproc
= HTTPREQ_AsyncReadFileExAProc
;
1953 workRequest
.hdr
= WININET_AddRef(&req
->hdr
);
1954 workRequest
.u
.InternetReadFileExA
.lpBuffersOut
= buffers
;
1956 INTERNET_AsyncCall(&workRequest
);
1958 return ERROR_IO_PENDING
;
1961 res
= HTTPREQ_Read(req
, buffers
->lpvBuffer
, buffers
->dwBufferLength
, &buffers
->dwBufferLength
,
1962 !(flags
& IRF_NO_WAIT
));
1965 if (res
== ERROR_SUCCESS
) {
1966 DWORD size
= buffers
->dwBufferLength
;
1967 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
,
1968 &size
, sizeof(size
));
1974 static void HTTPREQ_AsyncReadFileExWProc(WORKREQUEST
*workRequest
)
1976 struct WORKREQ_INTERNETREADFILEEXW
const *data
= &workRequest
->u
.InternetReadFileExW
;
1977 WININETHTTPREQW
*req
= (WININETHTTPREQW
*)workRequest
->hdr
;
1978 INTERNET_ASYNC_RESULT iar
;
1981 TRACE("INTERNETREADFILEEXW %p\n", workRequest
->hdr
);
1983 res
= HTTPREQ_Read(req
, data
->lpBuffersOut
->lpvBuffer
,
1984 data
->lpBuffersOut
->dwBufferLength
, &data
->lpBuffersOut
->dwBufferLength
, TRUE
);
1986 iar
.dwResult
= res
== ERROR_SUCCESS
;
1989 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
,
1990 INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
1991 sizeof(INTERNET_ASYNC_RESULT
));
1994 static DWORD
HTTPREQ_ReadFileExW(WININETHANDLEHEADER
*hdr
, INTERNET_BUFFERSW
*buffers
,
1995 DWORD flags
, DWORD_PTR context
)
1998 WININETHTTPREQW
*req
= (WININETHTTPREQW
*)hdr
;
2001 if (flags
& ~(IRF_ASYNC
|IRF_NO_WAIT
))
2002 FIXME("these dwFlags aren't implemented: 0x%x\n", flags
& ~(IRF_ASYNC
|IRF_NO_WAIT
));
2004 if (buffers
->dwStructSize
!= sizeof(*buffers
))
2005 return ERROR_INVALID_PARAMETER
;
2007 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
2009 if (hdr
->dwFlags
& INTERNET_FLAG_ASYNC
)
2011 WORKREQUEST workRequest
;
2013 if (TryEnterCriticalSection( &req
->read_section
))
2015 if (get_avail_data(req
))
2017 res
= HTTPREQ_Read(req
, buffers
->lpvBuffer
, buffers
->dwBufferLength
,
2018 &buffers
->dwBufferLength
, FALSE
);
2019 LeaveCriticalSection( &req
->read_section
);
2022 LeaveCriticalSection( &req
->read_section
);
2025 workRequest
.asyncproc
= HTTPREQ_AsyncReadFileExWProc
;
2026 workRequest
.hdr
= WININET_AddRef(&req
->hdr
);
2027 workRequest
.u
.InternetReadFileExW
.lpBuffersOut
= buffers
;
2029 INTERNET_AsyncCall(&workRequest
);
2031 return ERROR_IO_PENDING
;
2034 res
= HTTPREQ_Read(req
, buffers
->lpvBuffer
, buffers
->dwBufferLength
, &buffers
->dwBufferLength
,
2035 !(flags
& IRF_NO_WAIT
));
2038 if (res
== ERROR_SUCCESS
) {
2039 DWORD size
= buffers
->dwBufferLength
;
2040 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
,
2041 &size
, sizeof(size
));
2047 static BOOL
HTTPREQ_WriteFile(WININETHANDLEHEADER
*hdr
, const void *buffer
, DWORD size
, DWORD
*written
)
2050 LPWININETHTTPREQW lpwhr
= (LPWININETHTTPREQW
)hdr
;
2052 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
2055 if ((ret
= NETCON_send(&lpwhr
->netConnection
, buffer
, size
, 0, (LPINT
)written
)))
2056 lpwhr
->dwBytesWritten
+= *written
;
2058 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_SENT
, written
, sizeof(DWORD
));
2062 static void HTTPREQ_AsyncQueryDataAvailableProc(WORKREQUEST
*workRequest
)
2064 WININETHTTPREQW
*req
= (WININETHTTPREQW
*)workRequest
->hdr
;
2066 HTTP_ReceiveRequestData(req
, FALSE
);
2069 static DWORD
HTTPREQ_QueryDataAvailable(WININETHANDLEHEADER
*hdr
, DWORD
*available
, DWORD flags
, DWORD_PTR ctx
)
2071 WININETHTTPREQW
*req
= (WININETHTTPREQW
*)hdr
;
2073 TRACE("(%p %p %x %lx)\n", req
, available
, flags
, ctx
);
2075 if (req
->lpHttpSession
->lpAppInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2077 WORKREQUEST workRequest
;
2079 /* never wait, if we can't enter the section we queue an async request right away */
2080 if (TryEnterCriticalSection( &req
->read_section
))
2082 if ((*available
= get_avail_data( req
))) goto done
;
2083 if (end_of_read_data( req
)) goto done
;
2084 LeaveCriticalSection( &req
->read_section
);
2087 workRequest
.asyncproc
= HTTPREQ_AsyncQueryDataAvailableProc
;
2088 workRequest
.hdr
= WININET_AddRef( &req
->hdr
);
2090 INTERNET_AsyncCall(&workRequest
);
2092 return ERROR_IO_PENDING
;
2095 EnterCriticalSection( &req
->read_section
);
2097 if (!(*available
= get_avail_data( req
)) && !end_of_read_data( req
))
2099 refill_buffer( req
);
2100 *available
= get_avail_data( req
);
2104 if (*available
== sizeof(req
->read_buf
)) /* check if we have even more pending in the socket */
2107 if (NETCON_query_data_available(&req
->netConnection
, &extra
))
2108 *available
= min( *available
+ extra
, req
->dwContentLength
- req
->dwContentRead
);
2110 LeaveCriticalSection( &req
->read_section
);
2112 TRACE( "returning %u\n", *available
);
2113 return ERROR_SUCCESS
;
2116 static const HANDLEHEADERVtbl HTTPREQVtbl
= {
2118 HTTPREQ_CloseConnection
,
2119 HTTPREQ_QueryOption
,
2122 HTTPREQ_ReadFileExA
,
2123 HTTPREQ_ReadFileExW
,
2125 HTTPREQ_QueryDataAvailable
,
2129 /***********************************************************************
2130 * HTTP_HttpOpenRequestW (internal)
2132 * Open a HTTP request handle
2135 * HINTERNET a HTTP request handle on success
2139 HINTERNET WINAPI
HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs
,
2140 LPCWSTR lpszVerb
, LPCWSTR lpszObjectName
, LPCWSTR lpszVersion
,
2141 LPCWSTR lpszReferrer
, LPCWSTR
*lpszAcceptTypes
,
2142 DWORD dwFlags
, DWORD_PTR dwContext
)
2144 LPWININETAPPINFOW hIC
= NULL
;
2145 LPWININETHTTPREQW lpwhr
;
2146 LPWSTR lpszHostName
= NULL
;
2147 HINTERNET handle
= NULL
;
2148 static const WCHAR szHostForm
[] = {'%','s',':','%','u',0};
2153 assert( lpwhs
->hdr
.htype
== WH_HHTTPSESSION
);
2154 hIC
= lpwhs
->lpAppInfo
;
2156 lpwhr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WININETHTTPREQW
));
2159 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2162 lpwhr
->hdr
.htype
= WH_HHTTPREQ
;
2163 lpwhr
->hdr
.vtbl
= &HTTPREQVtbl
;
2164 lpwhr
->hdr
.dwFlags
= dwFlags
;
2165 lpwhr
->hdr
.dwContext
= dwContext
;
2166 lpwhr
->hdr
.refs
= 1;
2167 lpwhr
->hdr
.lpfnStatusCB
= lpwhs
->hdr
.lpfnStatusCB
;
2168 lpwhr
->hdr
.dwInternalFlags
= lpwhs
->hdr
.dwInternalFlags
& INET_CALLBACKW
;
2169 lpwhr
->dwContentLength
= ~0u;
2170 InitializeCriticalSection( &lpwhr
->read_section
);
2172 WININET_AddRef( &lpwhs
->hdr
);
2173 lpwhr
->lpHttpSession
= lpwhs
;
2174 list_add_head( &lpwhs
->hdr
.children
, &lpwhr
->hdr
.entry
);
2176 lpszHostName
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) *
2177 (strlenW(lpwhs
->lpszHostName
) + 7 /* length of ":65535" + 1 */));
2178 if (NULL
== lpszHostName
)
2180 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2184 handle
= WININET_AllocHandle( &lpwhr
->hdr
);
2187 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2191 if (!NETCON_init(&lpwhr
->netConnection
, dwFlags
& INTERNET_FLAG_SECURE
))
2193 InternetCloseHandle( handle
);
2198 if (lpszObjectName
&& *lpszObjectName
) {
2202 rc
= UrlEscapeW(lpszObjectName
, NULL
, &len
, URL_ESCAPE_SPACES_ONLY
);
2203 if (rc
!= E_POINTER
)
2204 len
= strlenW(lpszObjectName
)+1;
2205 lpwhr
->lpszPath
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
2206 rc
= UrlEscapeW(lpszObjectName
, lpwhr
->lpszPath
, &len
,
2207 URL_ESCAPE_SPACES_ONLY
);
2210 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName
),rc
);
2211 strcpyW(lpwhr
->lpszPath
,lpszObjectName
);
2214 static const WCHAR slashW
[] = {'/',0};
2216 lpwhr
->lpszPath
= WININET_strdupW(slashW
);
2219 if (lpszReferrer
&& *lpszReferrer
)
2220 HTTP_ProcessHeader(lpwhr
, HTTP_REFERER
, lpszReferrer
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REQ
);
2222 if (lpszAcceptTypes
)
2225 for (i
= 0; lpszAcceptTypes
[i
]; i
++)
2227 if (!*lpszAcceptTypes
[i
]) continue;
2228 HTTP_ProcessHeader(lpwhr
, HTTP_ACCEPT
, lpszAcceptTypes
[i
],
2229 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
|
2230 HTTP_ADDHDR_FLAG_REQ
|
2231 (i
== 0 ? HTTP_ADDHDR_FLAG_REPLACE
: 0));
2235 lpwhr
->lpszVerb
= WININET_strdupW(lpszVerb
&& *lpszVerb
? lpszVerb
: szGET
);
2238 lpwhr
->lpszVersion
= WININET_strdupW(lpszVersion
);
2240 lpwhr
->lpszVersion
= WININET_strdupW(g_szHttp1_1
);
2242 if (lpwhs
->nHostPort
!= INTERNET_INVALID_PORT_NUMBER
&&
2243 lpwhs
->nHostPort
!= INTERNET_DEFAULT_HTTP_PORT
&&
2244 lpwhs
->nHostPort
!= INTERNET_DEFAULT_HTTPS_PORT
)
2246 sprintfW(lpszHostName
, szHostForm
, lpwhs
->lpszHostName
, lpwhs
->nHostPort
);
2247 HTTP_ProcessHeader(lpwhr
, hostW
, lpszHostName
,
2248 HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REQ
);
2251 HTTP_ProcessHeader(lpwhr
, hostW
, lpwhs
->lpszHostName
,
2252 HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REQ
);
2254 if (lpwhs
->nServerPort
== INTERNET_INVALID_PORT_NUMBER
)
2255 lpwhs
->nServerPort
= (dwFlags
& INTERNET_FLAG_SECURE
?
2256 INTERNET_DEFAULT_HTTPS_PORT
:
2257 INTERNET_DEFAULT_HTTP_PORT
);
2259 if (lpwhs
->nHostPort
== INTERNET_INVALID_PORT_NUMBER
)
2260 lpwhs
->nHostPort
= (dwFlags
& INTERNET_FLAG_SECURE
?
2261 INTERNET_DEFAULT_HTTPS_PORT
:
2262 INTERNET_DEFAULT_HTTP_PORT
);
2264 if (NULL
!= hIC
->lpszProxy
&& hIC
->lpszProxy
[0] != 0)
2265 HTTP_DealWithProxy( hIC
, lpwhs
, lpwhr
);
2267 INTERNET_SendCallback(&lpwhs
->hdr
, dwContext
,
2268 INTERNET_STATUS_HANDLE_CREATED
, &handle
,
2272 HeapFree(GetProcessHeap(), 0, lpszHostName
);
2274 WININET_Release( &lpwhr
->hdr
);
2276 TRACE("<-- %p (%p)\n", handle
, lpwhr
);
2280 /* read any content returned by the server so that the connection can be
2282 static void HTTP_DrainContent(WININETHTTPREQW
*req
)
2286 if (!NETCON_connected(&req
->netConnection
)) return;
2288 if (req
->dwContentLength
== -1)
2290 NETCON_close(&req
->netConnection
);
2297 if (HTTPREQ_Read(req
, buffer
, sizeof(buffer
), &bytes_read
, TRUE
) != ERROR_SUCCESS
)
2299 } while (bytes_read
);
2302 static const LPCWSTR header_lookup
[] = {
2303 szMime_Version
, /* HTTP_QUERY_MIME_VERSION = 0 */
2304 szContent_Type
, /* HTTP_QUERY_CONTENT_TYPE = 1 */
2305 szContent_Transfer_Encoding
,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
2306 szContent_ID
, /* HTTP_QUERY_CONTENT_ID = 3 */
2307 NULL
, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
2308 szContent_Length
, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
2309 szContent_Language
, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
2310 szAllow
, /* HTTP_QUERY_ALLOW = 7 */
2311 szPublic
, /* HTTP_QUERY_PUBLIC = 8 */
2312 szDate
, /* HTTP_QUERY_DATE = 9 */
2313 szExpires
, /* HTTP_QUERY_EXPIRES = 10 */
2314 szLast_Modified
, /* HTTP_QUERY_LAST_MODIFIED = 11 */
2315 NULL
, /* HTTP_QUERY_MESSAGE_ID = 12 */
2316 szURI
, /* HTTP_QUERY_URI = 13 */
2317 szFrom
, /* HTTP_QUERY_DERIVED_FROM = 14 */
2318 NULL
, /* HTTP_QUERY_COST = 15 */
2319 NULL
, /* HTTP_QUERY_LINK = 16 */
2320 szPragma
, /* HTTP_QUERY_PRAGMA = 17 */
2321 NULL
, /* HTTP_QUERY_VERSION = 18 */
2322 szStatus
, /* HTTP_QUERY_STATUS_CODE = 19 */
2323 NULL
, /* HTTP_QUERY_STATUS_TEXT = 20 */
2324 NULL
, /* HTTP_QUERY_RAW_HEADERS = 21 */
2325 NULL
, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
2326 szConnection
, /* HTTP_QUERY_CONNECTION = 23 */
2327 szAccept
, /* HTTP_QUERY_ACCEPT = 24 */
2328 szAccept_Charset
, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
2329 szAccept_Encoding
, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
2330 szAccept_Language
, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
2331 szAuthorization
, /* HTTP_QUERY_AUTHORIZATION = 28 */
2332 szContent_Encoding
, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
2333 NULL
, /* HTTP_QUERY_FORWARDED = 30 */
2334 NULL
, /* HTTP_QUERY_FROM = 31 */
2335 szIf_Modified_Since
, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
2336 szLocation
, /* HTTP_QUERY_LOCATION = 33 */
2337 NULL
, /* HTTP_QUERY_ORIG_URI = 34 */
2338 szReferer
, /* HTTP_QUERY_REFERER = 35 */
2339 szRetry_After
, /* HTTP_QUERY_RETRY_AFTER = 36 */
2340 szServer
, /* HTTP_QUERY_SERVER = 37 */
2341 NULL
, /* HTTP_TITLE = 38 */
2342 szUser_Agent
, /* HTTP_QUERY_USER_AGENT = 39 */
2343 szWWW_Authenticate
, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
2344 szProxy_Authenticate
, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
2345 szAccept_Ranges
, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
2346 szSet_Cookie
, /* HTTP_QUERY_SET_COOKIE = 43 */
2347 szCookie
, /* HTTP_QUERY_COOKIE = 44 */
2348 NULL
, /* HTTP_QUERY_REQUEST_METHOD = 45 */
2349 NULL
, /* HTTP_QUERY_REFRESH = 46 */
2350 NULL
, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
2351 szAge
, /* HTTP_QUERY_AGE = 48 */
2352 szCache_Control
, /* HTTP_QUERY_CACHE_CONTROL = 49 */
2353 szContent_Base
, /* HTTP_QUERY_CONTENT_BASE = 50 */
2354 szContent_Location
, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
2355 szContent_MD5
, /* HTTP_QUERY_CONTENT_MD5 = 52 */
2356 szContent_Range
, /* HTTP_QUERY_CONTENT_RANGE = 53 */
2357 szETag
, /* HTTP_QUERY_ETAG = 54 */
2358 hostW
, /* HTTP_QUERY_HOST = 55 */
2359 szIf_Match
, /* HTTP_QUERY_IF_MATCH = 56 */
2360 szIf_None_Match
, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
2361 szIf_Range
, /* HTTP_QUERY_IF_RANGE = 58 */
2362 szIf_Unmodified_Since
, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
2363 szMax_Forwards
, /* HTTP_QUERY_MAX_FORWARDS = 60 */
2364 szProxy_Authorization
, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
2365 szRange
, /* HTTP_QUERY_RANGE = 62 */
2366 szTransfer_Encoding
, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
2367 szUpgrade
, /* HTTP_QUERY_UPGRADE = 64 */
2368 szVary
, /* HTTP_QUERY_VARY = 65 */
2369 szVia
, /* HTTP_QUERY_VIA = 66 */
2370 szWarning
, /* HTTP_QUERY_WARNING = 67 */
2371 szExpect
, /* HTTP_QUERY_EXPECT = 68 */
2372 szProxy_Connection
, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
2373 szUnless_Modified_Since
, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
2376 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
2378 /***********************************************************************
2379 * HTTP_HttpQueryInfoW (internal)
2381 static BOOL
HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr
, DWORD dwInfoLevel
,
2382 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
2384 LPHTTPHEADERW lphttpHdr
= NULL
;
2385 BOOL bSuccess
= FALSE
;
2386 BOOL request_only
= dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
;
2387 INT requested_index
= lpdwIndex
? *lpdwIndex
: 0;
2388 DWORD level
= (dwInfoLevel
& ~HTTP_QUERY_MODIFIER_FLAGS_MASK
);
2391 /* Find requested header structure */
2394 case HTTP_QUERY_CUSTOM
:
2395 if (!lpBuffer
) return FALSE
;
2396 index
= HTTP_GetCustomHeaderIndex(lpwhr
, lpBuffer
, requested_index
, request_only
);
2399 case HTTP_QUERY_RAW_HEADERS_CRLF
:
2406 headers
= HTTP_BuildHeaderRequestString(lpwhr
, lpwhr
->lpszVerb
, lpwhr
->lpszPath
, lpwhr
->lpszVersion
);
2408 headers
= lpwhr
->lpszRawHeaders
;
2411 len
= strlenW(headers
) * sizeof(WCHAR
);
2413 if (len
+ sizeof(WCHAR
) > *lpdwBufferLength
)
2415 len
+= sizeof(WCHAR
);
2416 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2422 memcpy(lpBuffer
, headers
, len
+ sizeof(WCHAR
));
2425 len
= strlenW(szCrLf
) * sizeof(WCHAR
);
2426 memcpy(lpBuffer
, szCrLf
, sizeof(szCrLf
));
2428 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
/ sizeof(WCHAR
)));
2431 *lpdwBufferLength
= len
;
2434 HeapFree(GetProcessHeap(), 0, headers
);
2437 case HTTP_QUERY_RAW_HEADERS
:
2439 LPWSTR
* ppszRawHeaderLines
= HTTP_Tokenize(lpwhr
->lpszRawHeaders
, szCrLf
);
2441 LPWSTR pszString
= lpBuffer
;
2443 for (i
= 0; ppszRawHeaderLines
[i
]; i
++)
2444 size
+= strlenW(ppszRawHeaderLines
[i
]) + 1;
2446 if (size
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
2448 HTTP_FreeTokens(ppszRawHeaderLines
);
2449 *lpdwBufferLength
= (size
+ 1) * sizeof(WCHAR
);
2450 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2455 for (i
= 0; ppszRawHeaderLines
[i
]; i
++)
2457 DWORD len
= strlenW(ppszRawHeaderLines
[i
]);
2458 memcpy(pszString
, ppszRawHeaderLines
[i
], (len
+1)*sizeof(WCHAR
));
2462 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, size
));
2464 *lpdwBufferLength
= size
* sizeof(WCHAR
);
2465 HTTP_FreeTokens(ppszRawHeaderLines
);
2469 case HTTP_QUERY_STATUS_TEXT
:
2470 if (lpwhr
->lpszStatusText
)
2472 DWORD len
= strlenW(lpwhr
->lpszStatusText
);
2473 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
2475 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
2476 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2481 memcpy(lpBuffer
, lpwhr
->lpszStatusText
, (len
+ 1) * sizeof(WCHAR
));
2482 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
));
2484 *lpdwBufferLength
= len
* sizeof(WCHAR
);
2488 case HTTP_QUERY_VERSION
:
2489 if (lpwhr
->lpszVersion
)
2491 DWORD len
= strlenW(lpwhr
->lpszVersion
);
2492 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
2494 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
2495 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2500 memcpy(lpBuffer
, lpwhr
->lpszVersion
, (len
+ 1) * sizeof(WCHAR
));
2501 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
));
2503 *lpdwBufferLength
= len
* sizeof(WCHAR
);
2508 assert (LAST_TABLE_HEADER
== (HTTP_QUERY_UNLESS_MODIFIED_SINCE
+ 1));
2510 if (level
< LAST_TABLE_HEADER
&& header_lookup
[level
])
2511 index
= HTTP_GetCustomHeaderIndex(lpwhr
, header_lookup
[level
],
2512 requested_index
,request_only
);
2516 lphttpHdr
= &lpwhr
->pCustHeaders
[index
];
2518 /* Ensure header satisfies requested attributes */
2520 ((dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
) &&
2521 (~lphttpHdr
->wFlags
& HDR_ISREQUEST
)))
2523 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND
);
2527 if (lpdwIndex
&& level
!= HTTP_QUERY_STATUS_CODE
) (*lpdwIndex
)++;
2529 /* coalesce value to requested type */
2530 if (dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
&& lpBuffer
)
2532 *(int *)lpBuffer
= atoiW(lphttpHdr
->lpszValue
);
2533 TRACE(" returning number: %d\n", *(int *)lpBuffer
);
2536 else if (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
&& lpBuffer
)
2542 tmpTime
= ConvertTimeString(lphttpHdr
->lpszValue
);
2544 tmpTM
= *gmtime(&tmpTime
);
2545 STHook
= (SYSTEMTIME
*)lpBuffer
;
2546 STHook
->wDay
= tmpTM
.tm_mday
;
2547 STHook
->wHour
= tmpTM
.tm_hour
;
2548 STHook
->wMilliseconds
= 0;
2549 STHook
->wMinute
= tmpTM
.tm_min
;
2550 STHook
->wDayOfWeek
= tmpTM
.tm_wday
;
2551 STHook
->wMonth
= tmpTM
.tm_mon
+ 1;
2552 STHook
->wSecond
= tmpTM
.tm_sec
;
2553 STHook
->wYear
= tmpTM
.tm_year
;
2556 TRACE(" returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
2557 STHook
->wYear
, STHook
->wMonth
, STHook
->wDay
, STHook
->wDayOfWeek
,
2558 STHook
->wHour
, STHook
->wMinute
, STHook
->wSecond
, STHook
->wMilliseconds
);
2560 else if (lphttpHdr
->lpszValue
)
2562 DWORD len
= (strlenW(lphttpHdr
->lpszValue
) + 1) * sizeof(WCHAR
);
2564 if (len
> *lpdwBufferLength
)
2566 *lpdwBufferLength
= len
;
2567 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2572 memcpy(lpBuffer
, lphttpHdr
->lpszValue
, len
);
2573 TRACE(" returning string: %s\n", debugstr_w(lpBuffer
));
2575 *lpdwBufferLength
= len
- sizeof(WCHAR
);
2581 /***********************************************************************
2582 * HttpQueryInfoW (WININET.@)
2584 * Queries for information about an HTTP request
2591 BOOL WINAPI
HttpQueryInfoW(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
2592 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
2594 BOOL bSuccess
= FALSE
;
2595 LPWININETHTTPREQW lpwhr
;
2597 if (TRACE_ON(wininet
)) {
2598 #define FE(x) { x, #x }
2599 static const wininet_flag_info query_flags
[] = {
2600 FE(HTTP_QUERY_MIME_VERSION
),
2601 FE(HTTP_QUERY_CONTENT_TYPE
),
2602 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING
),
2603 FE(HTTP_QUERY_CONTENT_ID
),
2604 FE(HTTP_QUERY_CONTENT_DESCRIPTION
),
2605 FE(HTTP_QUERY_CONTENT_LENGTH
),
2606 FE(HTTP_QUERY_CONTENT_LANGUAGE
),
2607 FE(HTTP_QUERY_ALLOW
),
2608 FE(HTTP_QUERY_PUBLIC
),
2609 FE(HTTP_QUERY_DATE
),
2610 FE(HTTP_QUERY_EXPIRES
),
2611 FE(HTTP_QUERY_LAST_MODIFIED
),
2612 FE(HTTP_QUERY_MESSAGE_ID
),
2614 FE(HTTP_QUERY_DERIVED_FROM
),
2615 FE(HTTP_QUERY_COST
),
2616 FE(HTTP_QUERY_LINK
),
2617 FE(HTTP_QUERY_PRAGMA
),
2618 FE(HTTP_QUERY_VERSION
),
2619 FE(HTTP_QUERY_STATUS_CODE
),
2620 FE(HTTP_QUERY_STATUS_TEXT
),
2621 FE(HTTP_QUERY_RAW_HEADERS
),
2622 FE(HTTP_QUERY_RAW_HEADERS_CRLF
),
2623 FE(HTTP_QUERY_CONNECTION
),
2624 FE(HTTP_QUERY_ACCEPT
),
2625 FE(HTTP_QUERY_ACCEPT_CHARSET
),
2626 FE(HTTP_QUERY_ACCEPT_ENCODING
),
2627 FE(HTTP_QUERY_ACCEPT_LANGUAGE
),
2628 FE(HTTP_QUERY_AUTHORIZATION
),
2629 FE(HTTP_QUERY_CONTENT_ENCODING
),
2630 FE(HTTP_QUERY_FORWARDED
),
2631 FE(HTTP_QUERY_FROM
),
2632 FE(HTTP_QUERY_IF_MODIFIED_SINCE
),
2633 FE(HTTP_QUERY_LOCATION
),
2634 FE(HTTP_QUERY_ORIG_URI
),
2635 FE(HTTP_QUERY_REFERER
),
2636 FE(HTTP_QUERY_RETRY_AFTER
),
2637 FE(HTTP_QUERY_SERVER
),
2638 FE(HTTP_QUERY_TITLE
),
2639 FE(HTTP_QUERY_USER_AGENT
),
2640 FE(HTTP_QUERY_WWW_AUTHENTICATE
),
2641 FE(HTTP_QUERY_PROXY_AUTHENTICATE
),
2642 FE(HTTP_QUERY_ACCEPT_RANGES
),
2643 FE(HTTP_QUERY_SET_COOKIE
),
2644 FE(HTTP_QUERY_COOKIE
),
2645 FE(HTTP_QUERY_REQUEST_METHOD
),
2646 FE(HTTP_QUERY_REFRESH
),
2647 FE(HTTP_QUERY_CONTENT_DISPOSITION
),
2649 FE(HTTP_QUERY_CACHE_CONTROL
),
2650 FE(HTTP_QUERY_CONTENT_BASE
),
2651 FE(HTTP_QUERY_CONTENT_LOCATION
),
2652 FE(HTTP_QUERY_CONTENT_MD5
),
2653 FE(HTTP_QUERY_CONTENT_RANGE
),
2654 FE(HTTP_QUERY_ETAG
),
2655 FE(HTTP_QUERY_HOST
),
2656 FE(HTTP_QUERY_IF_MATCH
),
2657 FE(HTTP_QUERY_IF_NONE_MATCH
),
2658 FE(HTTP_QUERY_IF_RANGE
),
2659 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE
),
2660 FE(HTTP_QUERY_MAX_FORWARDS
),
2661 FE(HTTP_QUERY_PROXY_AUTHORIZATION
),
2662 FE(HTTP_QUERY_RANGE
),
2663 FE(HTTP_QUERY_TRANSFER_ENCODING
),
2664 FE(HTTP_QUERY_UPGRADE
),
2665 FE(HTTP_QUERY_VARY
),
2667 FE(HTTP_QUERY_WARNING
),
2668 FE(HTTP_QUERY_CUSTOM
)
2670 static const wininet_flag_info modifier_flags
[] = {
2671 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS
),
2672 FE(HTTP_QUERY_FLAG_SYSTEMTIME
),
2673 FE(HTTP_QUERY_FLAG_NUMBER
),
2674 FE(HTTP_QUERY_FLAG_COALESCE
)
2677 DWORD info_mod
= dwInfoLevel
& HTTP_QUERY_MODIFIER_FLAGS_MASK
;
2678 DWORD info
= dwInfoLevel
& HTTP_QUERY_HEADER_MASK
;
2681 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest
, dwInfoLevel
, dwInfoLevel
);
2682 TRACE(" Attribute:");
2683 for (i
= 0; i
< (sizeof(query_flags
) / sizeof(query_flags
[0])); i
++) {
2684 if (query_flags
[i
].val
== info
) {
2685 TRACE(" %s", query_flags
[i
].name
);
2689 if (i
== (sizeof(query_flags
) / sizeof(query_flags
[0]))) {
2690 TRACE(" Unknown (%08x)", info
);
2693 TRACE(" Modifier:");
2694 for (i
= 0; i
< (sizeof(modifier_flags
) / sizeof(modifier_flags
[0])); i
++) {
2695 if (modifier_flags
[i
].val
& info_mod
) {
2696 TRACE(" %s", modifier_flags
[i
].name
);
2697 info_mod
&= ~ modifier_flags
[i
].val
;
2702 TRACE(" Unknown (%08x)", info_mod
);
2707 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hHttpRequest
);
2708 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
2710 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2714 if (lpBuffer
== NULL
)
2715 *lpdwBufferLength
= 0;
2716 bSuccess
= HTTP_HttpQueryInfoW( lpwhr
, dwInfoLevel
,
2717 lpBuffer
, lpdwBufferLength
, lpdwIndex
);
2721 WININET_Release( &lpwhr
->hdr
);
2723 TRACE("%d <--\n", bSuccess
);
2727 /***********************************************************************
2728 * HttpQueryInfoA (WININET.@)
2730 * Queries for information about an HTTP request
2737 BOOL WINAPI
HttpQueryInfoA(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
2738 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
2744 if((dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
) ||
2745 (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
))
2747 return HttpQueryInfoW( hHttpRequest
, dwInfoLevel
, lpBuffer
,
2748 lpdwBufferLength
, lpdwIndex
);
2754 len
= (*lpdwBufferLength
)*sizeof(WCHAR
);
2755 if ((dwInfoLevel
& HTTP_QUERY_HEADER_MASK
) == HTTP_QUERY_CUSTOM
)
2757 alloclen
= MultiByteToWideChar( CP_ACP
, 0, lpBuffer
, -1, NULL
, 0 ) * sizeof(WCHAR
);
2763 bufferW
= HeapAlloc( GetProcessHeap(), 0, alloclen
);
2764 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
2765 if ((dwInfoLevel
& HTTP_QUERY_HEADER_MASK
) == HTTP_QUERY_CUSTOM
)
2766 MultiByteToWideChar( CP_ACP
, 0, lpBuffer
, -1, bufferW
, alloclen
/ sizeof(WCHAR
) );
2773 result
= HttpQueryInfoW( hHttpRequest
, dwInfoLevel
, bufferW
,
2777 len
= WideCharToMultiByte( CP_ACP
,0, bufferW
, len
/ sizeof(WCHAR
) + 1,
2778 lpBuffer
, *lpdwBufferLength
, NULL
, NULL
);
2779 *lpdwBufferLength
= len
- 1;
2781 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer
));
2784 /* since the strings being returned from HttpQueryInfoW should be
2785 * only ASCII characters, it is reasonable to assume that all of
2786 * the Unicode characters can be reduced to a single byte */
2787 *lpdwBufferLength
= len
/ sizeof(WCHAR
);
2789 HeapFree(GetProcessHeap(), 0, bufferW
);
2794 /***********************************************************************
2795 * HttpSendRequestExA (WININET.@)
2797 * Sends the specified request to the HTTP server and allows chunked
2802 * Failure: FALSE, call GetLastError() for more information.
2804 BOOL WINAPI
HttpSendRequestExA(HINTERNET hRequest
,
2805 LPINTERNET_BUFFERSA lpBuffersIn
,
2806 LPINTERNET_BUFFERSA lpBuffersOut
,
2807 DWORD dwFlags
, DWORD_PTR dwContext
)
2809 INTERNET_BUFFERSW BuffersInW
;
2812 LPWSTR header
= NULL
;
2814 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest
, lpBuffersIn
,
2815 lpBuffersOut
, dwFlags
, dwContext
);
2819 BuffersInW
.dwStructSize
= sizeof(LPINTERNET_BUFFERSW
);
2820 if (lpBuffersIn
->lpcszHeader
)
2822 headerlen
= MultiByteToWideChar(CP_ACP
,0,lpBuffersIn
->lpcszHeader
,
2823 lpBuffersIn
->dwHeadersLength
,0,0);
2824 header
= HeapAlloc(GetProcessHeap(),0,headerlen
*sizeof(WCHAR
));
2825 if (!(BuffersInW
.lpcszHeader
= header
))
2827 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2830 BuffersInW
.dwHeadersLength
= MultiByteToWideChar(CP_ACP
, 0,
2831 lpBuffersIn
->lpcszHeader
, lpBuffersIn
->dwHeadersLength
,
2835 BuffersInW
.lpcszHeader
= NULL
;
2836 BuffersInW
.dwHeadersTotal
= lpBuffersIn
->dwHeadersTotal
;
2837 BuffersInW
.lpvBuffer
= lpBuffersIn
->lpvBuffer
;
2838 BuffersInW
.dwBufferLength
= lpBuffersIn
->dwBufferLength
;
2839 BuffersInW
.dwBufferTotal
= lpBuffersIn
->dwBufferTotal
;
2840 BuffersInW
.Next
= NULL
;
2843 rc
= HttpSendRequestExW(hRequest
, lpBuffersIn
? &BuffersInW
: NULL
, NULL
, dwFlags
, dwContext
);
2845 HeapFree(GetProcessHeap(),0,header
);
2850 /***********************************************************************
2851 * HttpSendRequestExW (WININET.@)
2853 * Sends the specified request to the HTTP server and allows chunked
2858 * Failure: FALSE, call GetLastError() for more information.
2860 BOOL WINAPI
HttpSendRequestExW(HINTERNET hRequest
,
2861 LPINTERNET_BUFFERSW lpBuffersIn
,
2862 LPINTERNET_BUFFERSW lpBuffersOut
,
2863 DWORD dwFlags
, DWORD_PTR dwContext
)
2866 LPWININETHTTPREQW lpwhr
;
2867 LPWININETHTTPSESSIONW lpwhs
;
2868 LPWININETAPPINFOW hIC
;
2870 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest
, lpBuffersIn
,
2871 lpBuffersOut
, dwFlags
, dwContext
);
2873 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hRequest
);
2875 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
2877 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2881 lpwhs
= lpwhr
->lpHttpSession
;
2882 assert(lpwhs
->hdr
.htype
== WH_HHTTPSESSION
);
2883 hIC
= lpwhs
->lpAppInfo
;
2884 assert(hIC
->hdr
.htype
== WH_HINIT
);
2886 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2888 WORKREQUEST workRequest
;
2889 struct WORKREQ_HTTPSENDREQUESTW
*req
;
2891 workRequest
.asyncproc
= AsyncHttpSendRequestProc
;
2892 workRequest
.hdr
= WININET_AddRef( &lpwhr
->hdr
);
2893 req
= &workRequest
.u
.HttpSendRequestW
;
2896 if (lpBuffersIn
->lpcszHeader
)
2897 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
2898 req
->lpszHeader
= WININET_strdupW(lpBuffersIn
->lpcszHeader
);
2900 req
->lpszHeader
= NULL
;
2901 req
->dwHeaderLength
= lpBuffersIn
->dwHeadersLength
;
2902 req
->lpOptional
= lpBuffersIn
->lpvBuffer
;
2903 req
->dwOptionalLength
= lpBuffersIn
->dwBufferLength
;
2904 req
->dwContentLength
= lpBuffersIn
->dwBufferTotal
;
2908 req
->lpszHeader
= NULL
;
2909 req
->dwHeaderLength
= 0;
2910 req
->lpOptional
= NULL
;
2911 req
->dwOptionalLength
= 0;
2912 req
->dwContentLength
= 0;
2915 req
->bEndRequest
= FALSE
;
2917 INTERNET_AsyncCall(&workRequest
);
2919 * This is from windows.
2921 INTERNET_SetLastError(ERROR_IO_PENDING
);
2926 ret
= HTTP_HttpSendRequestW(lpwhr
, lpBuffersIn
->lpcszHeader
, lpBuffersIn
->dwHeadersLength
,
2927 lpBuffersIn
->lpvBuffer
, lpBuffersIn
->dwBufferLength
,
2928 lpBuffersIn
->dwBufferTotal
, FALSE
);
2930 ret
= HTTP_HttpSendRequestW(lpwhr
, NULL
, 0, NULL
, 0, 0, FALSE
);
2935 WININET_Release( &lpwhr
->hdr
);
2941 /***********************************************************************
2942 * HttpSendRequestW (WININET.@)
2944 * Sends the specified request to the HTTP server
2951 BOOL WINAPI
HttpSendRequestW(HINTERNET hHttpRequest
, LPCWSTR lpszHeaders
,
2952 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
2954 LPWININETHTTPREQW lpwhr
;
2955 LPWININETHTTPSESSIONW lpwhs
= NULL
;
2956 LPWININETAPPINFOW hIC
= NULL
;
2959 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest
,
2960 debugstr_wn(lpszHeaders
, dwHeaderLength
), dwHeaderLength
, lpOptional
, dwOptionalLength
);
2962 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hHttpRequest
);
2963 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
2965 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2970 lpwhs
= lpwhr
->lpHttpSession
;
2971 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
2973 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2978 hIC
= lpwhs
->lpAppInfo
;
2979 if (NULL
== hIC
|| hIC
->hdr
.htype
!= WH_HINIT
)
2981 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2986 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2988 WORKREQUEST workRequest
;
2989 struct WORKREQ_HTTPSENDREQUESTW
*req
;
2991 workRequest
.asyncproc
= AsyncHttpSendRequestProc
;
2992 workRequest
.hdr
= WININET_AddRef( &lpwhr
->hdr
);
2993 req
= &workRequest
.u
.HttpSendRequestW
;
2998 if (dwHeaderLength
== ~0u) size
= (strlenW(lpszHeaders
) + 1) * sizeof(WCHAR
);
2999 else size
= dwHeaderLength
* sizeof(WCHAR
);
3001 req
->lpszHeader
= HeapAlloc(GetProcessHeap(), 0, size
);
3002 memcpy(req
->lpszHeader
, lpszHeaders
, size
);
3005 req
->lpszHeader
= 0;
3006 req
->dwHeaderLength
= dwHeaderLength
;
3007 req
->lpOptional
= lpOptional
;
3008 req
->dwOptionalLength
= dwOptionalLength
;
3009 req
->dwContentLength
= dwOptionalLength
;
3010 req
->bEndRequest
= TRUE
;
3012 INTERNET_AsyncCall(&workRequest
);
3014 * This is from windows.
3016 INTERNET_SetLastError(ERROR_IO_PENDING
);
3021 r
= HTTP_HttpSendRequestW(lpwhr
, lpszHeaders
,
3022 dwHeaderLength
, lpOptional
, dwOptionalLength
,
3023 dwOptionalLength
, TRUE
);
3027 WININET_Release( &lpwhr
->hdr
);
3031 /***********************************************************************
3032 * HttpSendRequestA (WININET.@)
3034 * Sends the specified request to the HTTP server
3041 BOOL WINAPI
HttpSendRequestA(HINTERNET hHttpRequest
, LPCSTR lpszHeaders
,
3042 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
3045 LPWSTR szHeaders
=NULL
;
3046 DWORD nLen
=dwHeaderLength
;
3047 if(lpszHeaders
!=NULL
)
3049 nLen
=MultiByteToWideChar(CP_ACP
,0,lpszHeaders
,dwHeaderLength
,NULL
,0);
3050 szHeaders
=HeapAlloc(GetProcessHeap(),0,nLen
*sizeof(WCHAR
));
3051 MultiByteToWideChar(CP_ACP
,0,lpszHeaders
,dwHeaderLength
,szHeaders
,nLen
);
3053 result
=HttpSendRequestW(hHttpRequest
, szHeaders
, nLen
, lpOptional
, dwOptionalLength
);
3054 HeapFree(GetProcessHeap(),0,szHeaders
);
3058 static BOOL
HTTP_GetRequestURL(WININETHTTPREQW
*req
, LPWSTR buf
)
3060 LPHTTPHEADERW host_header
;
3062 static const WCHAR formatW
[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
3064 host_header
= HTTP_GetHeader(req
, hostW
);
3068 sprintfW(buf
, formatW
, host_header
->lpszValue
, req
->lpszPath
); /* FIXME */
3072 /***********************************************************************
3073 * HTTP_GetRedirectURL (internal)
3075 static LPWSTR
HTTP_GetRedirectURL(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszUrl
)
3077 static WCHAR szHttp
[] = {'h','t','t','p',0};
3078 static WCHAR szHttps
[] = {'h','t','t','p','s',0};
3079 LPWININETHTTPSESSIONW lpwhs
= lpwhr
->lpHttpSession
;
3080 URL_COMPONENTSW urlComponents
;
3081 DWORD url_length
= 0;
3083 LPWSTR combined_url
;
3085 if (lpszUrl
[0]=='/') return WININET_strdupW( lpszUrl
);
3087 urlComponents
.dwStructSize
= sizeof(URL_COMPONENTSW
);
3088 urlComponents
.lpszScheme
= (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
) ? szHttps
: szHttp
;
3089 urlComponents
.dwSchemeLength
= 0;
3090 urlComponents
.lpszHostName
= lpwhs
->lpszHostName
;
3091 urlComponents
.dwHostNameLength
= 0;
3092 urlComponents
.nPort
= lpwhs
->nHostPort
;
3093 urlComponents
.lpszUserName
= lpwhs
->lpszUserName
;
3094 urlComponents
.dwUserNameLength
= 0;
3095 urlComponents
.lpszPassword
= NULL
;
3096 urlComponents
.dwPasswordLength
= 0;
3097 urlComponents
.lpszUrlPath
= lpwhr
->lpszPath
;
3098 urlComponents
.dwUrlPathLength
= 0;
3099 urlComponents
.lpszExtraInfo
= NULL
;
3100 urlComponents
.dwExtraInfoLength
= 0;
3102 if (!InternetCreateUrlW(&urlComponents
, 0, NULL
, &url_length
) &&
3103 (GetLastError() != ERROR_INSUFFICIENT_BUFFER
))
3106 orig_url
= HeapAlloc(GetProcessHeap(), 0, url_length
);
3108 /* convert from bytes to characters */
3109 url_length
= url_length
/ sizeof(WCHAR
) - 1;
3110 if (!InternetCreateUrlW(&urlComponents
, 0, orig_url
, &url_length
))
3112 HeapFree(GetProcessHeap(), 0, orig_url
);
3117 if (!InternetCombineUrlW(orig_url
, lpszUrl
, NULL
, &url_length
, ICU_ENCODE_SPACES_ONLY
) &&
3118 (GetLastError() != ERROR_INSUFFICIENT_BUFFER
))
3120 HeapFree(GetProcessHeap(), 0, orig_url
);
3123 combined_url
= HeapAlloc(GetProcessHeap(), 0, url_length
* sizeof(WCHAR
));
3125 if (!InternetCombineUrlW(orig_url
, lpszUrl
, combined_url
, &url_length
, ICU_ENCODE_SPACES_ONLY
))
3127 HeapFree(GetProcessHeap(), 0, orig_url
);
3128 HeapFree(GetProcessHeap(), 0, combined_url
);
3131 HeapFree(GetProcessHeap(), 0, orig_url
);
3132 return combined_url
;
3136 /***********************************************************************
3137 * HTTP_HandleRedirect (internal)
3139 static BOOL
HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszUrl
)
3141 LPWININETHTTPSESSIONW lpwhs
= lpwhr
->lpHttpSession
;
3142 LPWININETAPPINFOW hIC
= lpwhs
->lpAppInfo
;
3143 BOOL using_proxy
= hIC
->lpszProxy
&& hIC
->lpszProxy
[0];
3144 WCHAR path
[INTERNET_MAX_URL_LENGTH
];
3149 /* if it's an absolute path, keep the same session info */
3150 lstrcpynW(path
, lpszUrl
, INTERNET_MAX_URL_LENGTH
);
3154 URL_COMPONENTSW urlComponents
;
3155 WCHAR protocol
[32], hostName
[MAXHOSTNAME
], userName
[1024];
3156 static WCHAR szHttp
[] = {'h','t','t','p',0};
3157 static WCHAR szHttps
[] = {'h','t','t','p','s',0};
3163 urlComponents
.dwStructSize
= sizeof(URL_COMPONENTSW
);
3164 urlComponents
.lpszScheme
= protocol
;
3165 urlComponents
.dwSchemeLength
= 32;
3166 urlComponents
.lpszHostName
= hostName
;
3167 urlComponents
.dwHostNameLength
= MAXHOSTNAME
;
3168 urlComponents
.lpszUserName
= userName
;
3169 urlComponents
.dwUserNameLength
= 1024;
3170 urlComponents
.lpszPassword
= NULL
;
3171 urlComponents
.dwPasswordLength
= 0;
3172 urlComponents
.lpszUrlPath
= path
;
3173 urlComponents
.dwUrlPathLength
= 2048;
3174 urlComponents
.lpszExtraInfo
= NULL
;
3175 urlComponents
.dwExtraInfoLength
= 0;
3176 if(!InternetCrackUrlW(lpszUrl
, strlenW(lpszUrl
), 0, &urlComponents
))
3179 if (!strncmpW(szHttp
, urlComponents
.lpszScheme
, strlenW(szHttp
)) &&
3180 (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
))
3182 TRACE("redirect from secure page to non-secure page\n");
3183 /* FIXME: warn about from secure redirect to non-secure page */
3184 lpwhr
->hdr
.dwFlags
&= ~INTERNET_FLAG_SECURE
;
3186 if (!strncmpW(szHttps
, urlComponents
.lpszScheme
, strlenW(szHttps
)) &&
3187 !(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
))
3189 TRACE("redirect from non-secure page to secure page\n");
3190 /* FIXME: notify about redirect to secure page */
3191 lpwhr
->hdr
.dwFlags
|= INTERNET_FLAG_SECURE
;
3194 if (urlComponents
.nPort
== INTERNET_INVALID_PORT_NUMBER
)
3196 if (lstrlenW(protocol
)>4) /*https*/
3197 urlComponents
.nPort
= INTERNET_DEFAULT_HTTPS_PORT
;
3199 urlComponents
.nPort
= INTERNET_DEFAULT_HTTP_PORT
;
3204 * This upsets redirects to binary files on sourceforge.net
3205 * and gives an html page instead of the target file
3206 * Examination of the HTTP request sent by native wininet.dll
3207 * reveals that it doesn't send a referrer in that case.
3208 * Maybe there's a flag that enables this, or maybe a referrer
3209 * shouldn't be added in case of a redirect.
3212 /* consider the current host as the referrer */
3213 if (lpwhs
->lpszServerName
&& *lpwhs
->lpszServerName
)
3214 HTTP_ProcessHeader(lpwhr
, HTTP_REFERER
, lpwhs
->lpszServerName
,
3215 HTTP_ADDHDR_FLAG_REQ
|HTTP_ADDREQ_FLAG_REPLACE
|
3216 HTTP_ADDHDR_FLAG_ADD_IF_NEW
);
3219 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszHostName
);
3220 if (urlComponents
.nPort
!= INTERNET_DEFAULT_HTTP_PORT
&&
3221 urlComponents
.nPort
!= INTERNET_DEFAULT_HTTPS_PORT
)
3224 static const WCHAR fmt
[] = {'%','s',':','%','i',0};
3225 len
= lstrlenW(hostName
);
3226 len
+= 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
3227 lpwhs
->lpszHostName
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
3228 sprintfW(lpwhs
->lpszHostName
, fmt
, hostName
, urlComponents
.nPort
);
3231 lpwhs
->lpszHostName
= WININET_strdupW(hostName
);
3233 HTTP_ProcessHeader(lpwhr
, hostW
, lpwhs
->lpszHostName
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_REQ
);
3235 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszUserName
);
3236 lpwhs
->lpszUserName
= NULL
;
3238 lpwhs
->lpszUserName
= WININET_strdupW(userName
);
3242 if (strcmpiW(lpwhs
->lpszServerName
, hostName
) || lpwhs
->nServerPort
!= urlComponents
.nPort
)
3244 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszServerName
);
3245 lpwhs
->lpszServerName
= WININET_strdupW(hostName
);
3246 lpwhs
->nServerPort
= urlComponents
.nPort
;
3248 NETCON_close(&lpwhr
->netConnection
);
3249 if (!HTTP_ResolveName(lpwhr
)) return FALSE
;
3250 if (!NETCON_init(&lpwhr
->netConnection
, lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
)) return FALSE
;
3251 lpwhr
->read_pos
= lpwhr
->read_size
= 0;
3252 lpwhr
->read_chunked
= FALSE
;
3256 TRACE("Redirect through proxy\n");
3259 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszPath
);
3260 lpwhr
->lpszPath
=NULL
;
3266 rc
= UrlEscapeW(path
, NULL
, &needed
, URL_ESCAPE_SPACES_ONLY
);
3267 if (rc
!= E_POINTER
)
3268 needed
= strlenW(path
)+1;
3269 lpwhr
->lpszPath
= HeapAlloc(GetProcessHeap(), 0, needed
*sizeof(WCHAR
));
3270 rc
= UrlEscapeW(path
, lpwhr
->lpszPath
, &needed
,
3271 URL_ESCAPE_SPACES_ONLY
);
3274 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path
),rc
);
3275 strcpyW(lpwhr
->lpszPath
,path
);
3279 /* Remove custom content-type/length headers on redirects. */
3280 index
= HTTP_GetCustomHeaderIndex(lpwhr
, szContent_Type
, 0, TRUE
);
3282 HTTP_DeleteCustomHeader(lpwhr
, index
);
3283 index
= HTTP_GetCustomHeaderIndex(lpwhr
, szContent_Length
, 0, TRUE
);
3285 HTTP_DeleteCustomHeader(lpwhr
, index
);
3290 /***********************************************************************
3291 * HTTP_build_req (internal)
3293 * concatenate all the strings in the request together
3295 static LPWSTR
HTTP_build_req( LPCWSTR
*list
, int len
)
3300 for( t
= list
; *t
; t
++ )
3301 len
+= strlenW( *t
);
3304 str
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
3307 for( t
= list
; *t
; t
++ )
3313 static BOOL
HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr
)
3316 LPWSTR requestString
;
3322 static const WCHAR szConnect
[] = {'C','O','N','N','E','C','T',0};
3323 static const WCHAR szFormat
[] = {'%','s',':','%','d',0};
3324 LPWININETHTTPSESSIONW lpwhs
= lpwhr
->lpHttpSession
;
3328 lpszPath
= HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs
->lpszHostName
) + 13)*sizeof(WCHAR
) );
3329 sprintfW( lpszPath
, szFormat
, lpwhs
->lpszHostName
, lpwhs
->nHostPort
);
3330 requestString
= HTTP_BuildHeaderRequestString( lpwhr
, szConnect
, lpszPath
, g_szHttp1_1
);
3331 HeapFree( GetProcessHeap(), 0, lpszPath
);
3333 len
= WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
3334 NULL
, 0, NULL
, NULL
);
3335 len
--; /* the nul terminator isn't needed */
3336 ascii_req
= HeapAlloc( GetProcessHeap(), 0, len
);
3337 WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
3338 ascii_req
, len
, NULL
, NULL
);
3339 HeapFree( GetProcessHeap(), 0, requestString
);
3341 TRACE("full request -> %s\n", debugstr_an( ascii_req
, len
) );
3343 ret
= NETCON_send( &lpwhr
->netConnection
, ascii_req
, len
, 0, &cnt
);
3344 HeapFree( GetProcessHeap(), 0, ascii_req
);
3345 if (!ret
|| cnt
< 0)
3348 responseLen
= HTTP_GetResponseHeaders( lpwhr
, TRUE
);
3355 static void HTTP_InsertCookies(LPWININETHTTPREQW lpwhr
)
3357 static const WCHAR szUrlForm
[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
3358 LPWSTR lpszCookies
, lpszUrl
= NULL
;
3359 DWORD nCookieSize
, size
;
3360 LPHTTPHEADERW Host
= HTTP_GetHeader(lpwhr
, hostW
);
3362 size
= (strlenW(Host
->lpszValue
) + strlenW(szUrlForm
) + strlenW(lpwhr
->lpszPath
)) * sizeof(WCHAR
);
3363 if (!(lpszUrl
= HeapAlloc(GetProcessHeap(), 0, size
))) return;
3364 sprintfW( lpszUrl
, szUrlForm
, Host
->lpszValue
, lpwhr
->lpszPath
);
3366 if (InternetGetCookieW(lpszUrl
, NULL
, NULL
, &nCookieSize
))
3369 static const WCHAR szCookie
[] = {'C','o','o','k','i','e',':',' ',0};
3371 size
= sizeof(szCookie
) + nCookieSize
* sizeof(WCHAR
) + sizeof(szCrLf
);
3372 if ((lpszCookies
= HeapAlloc(GetProcessHeap(), 0, size
)))
3374 cnt
+= sprintfW(lpszCookies
, szCookie
);
3375 InternetGetCookieW(lpszUrl
, NULL
, lpszCookies
+ cnt
, &nCookieSize
);
3376 strcatW(lpszCookies
, szCrLf
);
3378 HTTP_HttpAddRequestHeadersW(lpwhr
, lpszCookies
, strlenW(lpszCookies
), HTTP_ADDREQ_FLAG_REPLACE
);
3379 HeapFree(GetProcessHeap(), 0, lpszCookies
);
3382 HeapFree(GetProcessHeap(), 0, lpszUrl
);
3385 /***********************************************************************
3386 * HTTP_HttpSendRequestW (internal)
3388 * Sends the specified request to the HTTP server
3395 BOOL WINAPI
HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszHeaders
,
3396 DWORD dwHeaderLength
, LPVOID lpOptional
, DWORD dwOptionalLength
,
3397 DWORD dwContentLength
, BOOL bEndRequest
)
3400 BOOL bSuccess
= FALSE
, redirected
= FALSE
;
3401 LPWSTR requestString
= NULL
;
3404 INTERNET_ASYNC_RESULT iar
;
3405 static const WCHAR szPost
[] = { 'P','O','S','T',0 };
3406 static const WCHAR szContentLength
[] =
3407 { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0 };
3408 WCHAR contentLengthStr
[sizeof szContentLength
/2 /* includes \r\n */ + 20 /* int */ ];
3410 TRACE("--> %p\n", lpwhr
);
3412 assert(lpwhr
->hdr
.htype
== WH_HHTTPREQ
);
3414 /* if the verb is NULL default to GET */
3415 if (!lpwhr
->lpszVerb
)
3416 lpwhr
->lpszVerb
= WININET_strdupW(szGET
);
3418 if (dwContentLength
|| strcmpW(lpwhr
->lpszVerb
, szGET
))
3420 sprintfW(contentLengthStr
, szContentLength
, dwContentLength
);
3421 HTTP_HttpAddRequestHeadersW(lpwhr
, contentLengthStr
, -1L, HTTP_ADDREQ_FLAG_REPLACE
);
3422 lpwhr
->dwBytesToWrite
= dwContentLength
;
3424 if (lpwhr
->lpHttpSession
->lpAppInfo
->lpszAgent
)
3426 WCHAR
*agent_header
;
3427 static const WCHAR user_agent
[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0};
3430 len
= strlenW(lpwhr
->lpHttpSession
->lpAppInfo
->lpszAgent
) + strlenW(user_agent
);
3431 agent_header
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
3432 sprintfW(agent_header
, user_agent
, lpwhr
->lpHttpSession
->lpAppInfo
->lpszAgent
);
3434 HTTP_HttpAddRequestHeadersW(lpwhr
, agent_header
, strlenW(agent_header
), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
3435 HeapFree(GetProcessHeap(), 0, agent_header
);
3437 if (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_PRAGMA_NOCACHE
)
3439 static const WCHAR pragma_nocache
[] = {'P','r','a','g','m','a',':',' ','n','o','-','c','a','c','h','e','\r','\n',0};
3440 HTTP_HttpAddRequestHeadersW(lpwhr
, pragma_nocache
, strlenW(pragma_nocache
), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
3442 if ((lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_CACHE_WRITE
) && !strcmpW(lpwhr
->lpszVerb
, szPost
))
3444 static const WCHAR cache_control
[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l',':',
3445 ' ','n','o','-','c','a','c','h','e','\r','\n',0};
3446 HTTP_HttpAddRequestHeadersW(lpwhr
, cache_control
, strlenW(cache_control
), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
3456 /* like native, just in case the caller forgot to call InternetReadFile
3457 * for all the data */
3458 HTTP_DrainContent(lpwhr
);
3459 lpwhr
->dwContentRead
= 0;
3461 if (TRACE_ON(wininet
))
3463 LPHTTPHEADERW Host
= HTTP_GetHeader(lpwhr
, hostW
);
3464 TRACE("Going to url %s %s\n", debugstr_w(Host
->lpszValue
), debugstr_w(lpwhr
->lpszPath
));
3468 if (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_KEEP_CONNECTION
)
3470 HTTP_ProcessHeader(lpwhr
, szConnection
, szKeepAlive
, HTTP_ADDHDR_FLAG_REQ
| HTTP_ADDHDR_FLAG_REPLACE
);
3472 HTTP_InsertAuthorization(lpwhr
, lpwhr
->pAuthInfo
, szAuthorization
);
3473 HTTP_InsertAuthorization(lpwhr
, lpwhr
->pProxyAuthInfo
, szProxy_Authorization
);
3475 if (!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_COOKIES
))
3476 HTTP_InsertCookies(lpwhr
);
3478 /* add the headers the caller supplied */
3479 if( lpszHeaders
&& dwHeaderLength
)
3481 HTTP_HttpAddRequestHeadersW(lpwhr
, lpszHeaders
, dwHeaderLength
,
3482 HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REPLACE
);
3485 if (lpwhr
->lpHttpSession
->lpAppInfo
->lpszProxy
&& lpwhr
->lpHttpSession
->lpAppInfo
->lpszProxy
[0])
3487 WCHAR
*url
= HTTP_BuildProxyRequestUrl(lpwhr
);
3488 requestString
= HTTP_BuildHeaderRequestString(lpwhr
, lpwhr
->lpszVerb
, url
, lpwhr
->lpszVersion
);
3489 HeapFree(GetProcessHeap(), 0, url
);
3492 requestString
= HTTP_BuildHeaderRequestString(lpwhr
, lpwhr
->lpszVerb
, lpwhr
->lpszPath
, lpwhr
->lpszVersion
);
3495 TRACE("Request header -> %s\n", debugstr_w(requestString
) );
3497 /* Send the request and store the results */
3498 if (!HTTP_OpenConnection(lpwhr
))
3501 /* send the request as ASCII, tack on the optional data */
3502 if (!lpOptional
|| redirected
)
3503 dwOptionalLength
= 0;
3504 len
= WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
3505 NULL
, 0, NULL
, NULL
);
3506 ascii_req
= HeapAlloc( GetProcessHeap(), 0, len
+ dwOptionalLength
);
3507 WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
3508 ascii_req
, len
, NULL
, NULL
);
3510 memcpy( &ascii_req
[len
-1], lpOptional
, dwOptionalLength
);
3511 len
= (len
+ dwOptionalLength
- 1);
3513 TRACE("full request -> %s\n", debugstr_a(ascii_req
) );
3515 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3516 INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
3518 NETCON_send(&lpwhr
->netConnection
, ascii_req
, len
, 0, &cnt
);
3519 HeapFree( GetProcessHeap(), 0, ascii_req
);
3521 lpwhr
->dwBytesWritten
= dwOptionalLength
;
3523 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3524 INTERNET_STATUS_REQUEST_SENT
,
3525 &len
, sizeof(DWORD
));
3532 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3533 INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
3538 responseLen
= HTTP_GetResponseHeaders(lpwhr
, TRUE
);
3542 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3543 INTERNET_STATUS_RESPONSE_RECEIVED
, &responseLen
,
3546 HTTP_ProcessCookies(lpwhr
);
3548 if (!set_content_length( lpwhr
)) HTTP_FinishedReading(lpwhr
);
3550 dwBufferSize
= sizeof(dwStatusCode
);
3551 if (!HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_STATUS_CODE
,
3552 &dwStatusCode
,&dwBufferSize
,NULL
))
3555 if (!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTO_REDIRECT
) && bSuccess
)
3557 WCHAR
*new_url
, szNewLocation
[INTERNET_MAX_URL_LENGTH
];
3558 dwBufferSize
=sizeof(szNewLocation
);
3559 if ((dwStatusCode
==HTTP_STATUS_REDIRECT
|| dwStatusCode
==HTTP_STATUS_MOVED
) &&
3560 HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_LOCATION
,szNewLocation
,&dwBufferSize
,NULL
))
3562 /* redirects are always GETs */
3563 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszVerb
);
3564 lpwhr
->lpszVerb
= WININET_strdupW(szGET
);
3566 HTTP_DrainContent(lpwhr
);
3567 if ((new_url
= HTTP_GetRedirectURL( lpwhr
, szNewLocation
)))
3569 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
, INTERNET_STATUS_REDIRECT
,
3570 new_url
, (strlenW(new_url
) + 1) * sizeof(WCHAR
));
3571 bSuccess
= HTTP_HandleRedirect(lpwhr
, new_url
);
3574 HeapFree(GetProcessHeap(), 0, requestString
);
3577 HeapFree( GetProcessHeap(), 0, new_url
);
3582 if (!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTH
) && bSuccess
)
3584 WCHAR szAuthValue
[2048];
3586 if (dwStatusCode
== HTTP_STATUS_DENIED
)
3589 while (HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_WWW_AUTHENTICATE
,szAuthValue
,&dwBufferSize
,&dwIndex
))
3591 if (HTTP_DoAuthorization(lpwhr
, szAuthValue
,
3593 lpwhr
->lpHttpSession
->lpszUserName
,
3594 lpwhr
->lpHttpSession
->lpszPassword
))
3601 if (dwStatusCode
== HTTP_STATUS_PROXY_AUTH_REQ
)
3604 while (HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_PROXY_AUTHENTICATE
,szAuthValue
,&dwBufferSize
,&dwIndex
))
3606 if (HTTP_DoAuthorization(lpwhr
, szAuthValue
,
3607 &lpwhr
->pProxyAuthInfo
,
3608 lpwhr
->lpHttpSession
->lpAppInfo
->lpszProxyUsername
,
3609 lpwhr
->lpHttpSession
->lpAppInfo
->lpszProxyPassword
))
3623 /* FIXME: Better check, when we have to create the cache file */
3624 if(bSuccess
&& (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NEED_FILE
)) {
3625 WCHAR url
[INTERNET_MAX_URL_LENGTH
];
3626 WCHAR cacheFileName
[MAX_PATH
+1];
3629 b
= HTTP_GetRequestURL(lpwhr
, url
);
3631 WARN("Could not get URL\n");
3635 b
= CreateUrlCacheEntryW(url
, lpwhr
->dwContentLength
> 0 ? lpwhr
->dwContentLength
: 0, NULL
, cacheFileName
, 0);
3637 lpwhr
->lpszCacheFile
= WININET_strdupW(cacheFileName
);
3638 lpwhr
->hCacheFile
= CreateFileW(lpwhr
->lpszCacheFile
, GENERIC_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
3639 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3640 if(lpwhr
->hCacheFile
== INVALID_HANDLE_VALUE
) {
3641 WARN("Could not create file: %u\n", GetLastError());
3642 lpwhr
->hCacheFile
= NULL
;
3645 WARN("Could not create cache entry: %08x\n", GetLastError());
3651 HeapFree(GetProcessHeap(), 0, requestString
);
3653 /* TODO: send notification for P3P header */
3655 if (lpwhr
->lpHttpSession
->lpAppInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
3659 if (lpwhr
->dwBytesWritten
== lpwhr
->dwBytesToWrite
) HTTP_ReceiveRequestData(lpwhr
, TRUE
);
3662 iar
.dwResult
= (DWORD_PTR
)lpwhr
->hdr
.hInternet
;
3665 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3666 INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
3667 sizeof(INTERNET_ASYNC_RESULT
));
3672 iar
.dwResult
= (DWORD_PTR
)lpwhr
->hdr
.hInternet
;
3673 iar
.dwError
= INTERNET_GetLastError();
3675 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3676 INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
3677 sizeof(INTERNET_ASYNC_RESULT
));
3682 if (bSuccess
) INTERNET_SetLastError(ERROR_SUCCESS
);
3686 /***********************************************************************
3687 * HTTPSESSION_Destroy (internal)
3689 * Deallocate session handle
3692 static void HTTPSESSION_Destroy(WININETHANDLEHEADER
*hdr
)
3694 LPWININETHTTPSESSIONW lpwhs
= (LPWININETHTTPSESSIONW
) hdr
;
3696 TRACE("%p\n", lpwhs
);
3698 WININET_Release(&lpwhs
->lpAppInfo
->hdr
);
3700 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszHostName
);
3701 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszServerName
);
3702 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszPassword
);
3703 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszUserName
);
3704 HeapFree(GetProcessHeap(), 0, lpwhs
);
3707 static DWORD
HTTPSESSION_QueryOption(WININETHANDLEHEADER
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
3710 case INTERNET_OPTION_HANDLE_TYPE
:
3711 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
3713 if (*size
< sizeof(ULONG
))
3714 return ERROR_INSUFFICIENT_BUFFER
;
3716 *size
= sizeof(DWORD
);
3717 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_CONNECT_HTTP
;
3718 return ERROR_SUCCESS
;
3721 return INET_QueryOption(option
, buffer
, size
, unicode
);
3724 static DWORD
HTTPSESSION_SetOption(WININETHANDLEHEADER
*hdr
, DWORD option
, void *buffer
, DWORD size
)
3726 WININETHTTPSESSIONW
*ses
= (WININETHTTPSESSIONW
*)hdr
;
3729 case INTERNET_OPTION_USERNAME
:
3731 HeapFree(GetProcessHeap(), 0, ses
->lpszUserName
);
3732 if (!(ses
->lpszUserName
= WININET_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
3733 return ERROR_SUCCESS
;
3735 case INTERNET_OPTION_PASSWORD
:
3737 HeapFree(GetProcessHeap(), 0, ses
->lpszPassword
);
3738 if (!(ses
->lpszPassword
= WININET_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
3739 return ERROR_SUCCESS
;
3744 return ERROR_INTERNET_INVALID_OPTION
;
3747 static const HANDLEHEADERVtbl HTTPSESSIONVtbl
= {
3748 HTTPSESSION_Destroy
,
3750 HTTPSESSION_QueryOption
,
3751 HTTPSESSION_SetOption
,
3760 /***********************************************************************
3761 * HTTP_Connect (internal)
3763 * Create http session handle
3766 * HINTERNET a session handle on success
3770 HINTERNET
HTTP_Connect(LPWININETAPPINFOW hIC
, LPCWSTR lpszServerName
,
3771 INTERNET_PORT nServerPort
, LPCWSTR lpszUserName
,
3772 LPCWSTR lpszPassword
, DWORD dwFlags
, DWORD_PTR dwContext
,
3773 DWORD dwInternalFlags
)
3775 LPWININETHTTPSESSIONW lpwhs
= NULL
;
3776 HINTERNET handle
= NULL
;
3780 if (!lpszServerName
|| !lpszServerName
[0])
3782 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
3786 assert( hIC
->hdr
.htype
== WH_HINIT
);
3788 lpwhs
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WININETHTTPSESSIONW
));
3791 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
3796 * According to my tests. The name is not resolved until a request is sent
3799 lpwhs
->hdr
.htype
= WH_HHTTPSESSION
;
3800 lpwhs
->hdr
.vtbl
= &HTTPSESSIONVtbl
;
3801 lpwhs
->hdr
.dwFlags
= dwFlags
;
3802 lpwhs
->hdr
.dwContext
= dwContext
;
3803 lpwhs
->hdr
.dwInternalFlags
= dwInternalFlags
| (hIC
->hdr
.dwInternalFlags
& INET_CALLBACKW
);
3804 lpwhs
->hdr
.refs
= 1;
3805 lpwhs
->hdr
.lpfnStatusCB
= hIC
->hdr
.lpfnStatusCB
;
3807 WININET_AddRef( &hIC
->hdr
);
3808 lpwhs
->lpAppInfo
= hIC
;
3809 list_add_head( &hIC
->hdr
.children
, &lpwhs
->hdr
.entry
);
3811 handle
= WININET_AllocHandle( &lpwhs
->hdr
);
3814 ERR("Failed to alloc handle\n");
3815 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
3819 if(hIC
->lpszProxy
&& hIC
->dwAccessType
== INTERNET_OPEN_TYPE_PROXY
) {
3820 if(strchrW(hIC
->lpszProxy
, ' '))
3821 FIXME("Several proxies not implemented.\n");
3822 if(hIC
->lpszProxyBypass
)
3823 FIXME("Proxy bypass is ignored.\n");
3825 if (lpszServerName
&& lpszServerName
[0])
3827 lpwhs
->lpszServerName
= WININET_strdupW(lpszServerName
);
3828 lpwhs
->lpszHostName
= WININET_strdupW(lpszServerName
);
3830 if (lpszUserName
&& lpszUserName
[0])
3831 lpwhs
->lpszUserName
= WININET_strdupW(lpszUserName
);
3832 if (lpszPassword
&& lpszPassword
[0])
3833 lpwhs
->lpszPassword
= WININET_strdupW(lpszPassword
);
3834 lpwhs
->nServerPort
= nServerPort
;
3835 lpwhs
->nHostPort
= nServerPort
;
3837 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
3838 if (!(lpwhs
->hdr
.dwInternalFlags
& INET_OPENURL
))
3840 INTERNET_SendCallback(&hIC
->hdr
, dwContext
,
3841 INTERNET_STATUS_HANDLE_CREATED
, &handle
,
3847 WININET_Release( &lpwhs
->hdr
);
3850 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
3854 TRACE("%p --> %p (%p)\n", hIC
, handle
, lpwhs
);
3859 /***********************************************************************
3860 * HTTP_OpenConnection (internal)
3862 * Connect to a web server
3869 static BOOL
HTTP_OpenConnection(LPWININETHTTPREQW lpwhr
)
3871 BOOL bSuccess
= FALSE
;
3872 LPWININETHTTPSESSIONW lpwhs
;
3873 LPWININETAPPINFOW hIC
= NULL
;
3879 if (lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
3881 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
3885 if (NETCON_connected(&lpwhr
->netConnection
))
3890 if (!HTTP_ResolveName(lpwhr
)) goto lend
;
3892 lpwhs
= lpwhr
->lpHttpSession
;
3894 hIC
= lpwhs
->lpAppInfo
;
3895 inet_ntop(lpwhs
->socketAddress
.sin_family
, &lpwhs
->socketAddress
.sin_addr
,
3896 szaddr
, sizeof(szaddr
));
3897 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3898 INTERNET_STATUS_CONNECTING_TO_SERVER
,
3902 if (!NETCON_create(&lpwhr
->netConnection
, lpwhs
->socketAddress
.sin_family
,
3905 WARN("Socket creation failed: %u\n", INTERNET_GetLastError());
3909 if (!NETCON_connect(&lpwhr
->netConnection
, (struct sockaddr
*)&lpwhs
->socketAddress
,
3910 sizeof(lpwhs
->socketAddress
)))
3913 if (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
)
3915 /* Note: we differ from Microsoft's WinINet here. they seem to have
3916 * a bug that causes no status callbacks to be sent when starting
3917 * a tunnel to a proxy server using the CONNECT verb. i believe our
3918 * behaviour to be more correct and to not cause any incompatibilities
3919 * because using a secure connection through a proxy server is a rare
3920 * case that would be hard for anyone to depend on */
3921 if (hIC
->lpszProxy
&& !HTTP_SecureProxyConnect(lpwhr
))
3924 if (!NETCON_secure_connect(&lpwhr
->netConnection
, lpwhs
->lpszHostName
))
3926 WARN("Couldn't connect securely to host\n");
3931 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3932 INTERNET_STATUS_CONNECTED_TO_SERVER
,
3933 szaddr
, strlen(szaddr
)+1);
3938 lpwhr
->read_pos
= lpwhr
->read_size
= 0;
3939 lpwhr
->read_chunked
= FALSE
;
3941 TRACE("%d <--\n", bSuccess
);
3946 /***********************************************************************
3947 * HTTP_clear_response_headers (internal)
3949 * clear out any old response headers
3951 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr
)
3955 for( i
=0; i
<lpwhr
->nCustHeaders
; i
++)
3957 if( !lpwhr
->pCustHeaders
[i
].lpszField
)
3959 if( !lpwhr
->pCustHeaders
[i
].lpszValue
)
3961 if ( lpwhr
->pCustHeaders
[i
].wFlags
& HDR_ISREQUEST
)
3963 HTTP_DeleteCustomHeader( lpwhr
, i
);
3968 /***********************************************************************
3969 * HTTP_GetResponseHeaders (internal)
3971 * Read server response
3978 static INT
HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr
, BOOL clear
)
3981 WCHAR buffer
[MAX_REPLY_LEN
];
3982 DWORD buflen
= MAX_REPLY_LEN
;
3983 BOOL bSuccess
= FALSE
;
3985 static const WCHAR szHundred
[] = {'1','0','0',0};
3986 char bufferA
[MAX_REPLY_LEN
];
3987 LPWSTR status_code
, status_text
;
3988 DWORD cchMaxRawHeaders
= 1024;
3989 LPWSTR lpszRawHeaders
= HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders
+1)*sizeof(WCHAR
));
3990 DWORD cchRawHeaders
= 0;
3994 /* clear old response headers (eg. from a redirect response) */
3995 if (clear
) HTTP_clear_response_headers( lpwhr
);
3997 if (!NETCON_connected(&lpwhr
->netConnection
))
4002 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
4004 buflen
= MAX_REPLY_LEN
;
4005 if (!read_line(lpwhr
, bufferA
, &buflen
))
4008 MultiByteToWideChar( CP_ACP
, 0, bufferA
, buflen
, buffer
, MAX_REPLY_LEN
);
4010 /* split the version from the status code */
4011 status_code
= strchrW( buffer
, ' ' );
4016 /* split the status code from the status text */
4017 status_text
= strchrW( status_code
, ' ' );
4022 TRACE("version [%s] status code [%s] status text [%s]\n",
4023 debugstr_w(buffer
), debugstr_w(status_code
), debugstr_w(status_text
) );
4025 } while (!strcmpW(status_code
, szHundred
)); /* ignore "100 Continue" responses */
4027 /* Add status code */
4028 HTTP_ProcessHeader(lpwhr
, szStatus
, status_code
,
4029 HTTP_ADDHDR_FLAG_REPLACE
);
4031 HeapFree(GetProcessHeap(),0,lpwhr
->lpszVersion
);
4032 HeapFree(GetProcessHeap(),0,lpwhr
->lpszStatusText
);
4034 lpwhr
->lpszVersion
= WININET_strdupW(buffer
);
4035 lpwhr
->lpszStatusText
= WININET_strdupW(status_text
);
4037 /* Restore the spaces */
4038 *(status_code
-1) = ' ';
4039 *(status_text
-1) = ' ';
4041 /* regenerate raw headers */
4042 while (cchRawHeaders
+ buflen
+ strlenW(szCrLf
) > cchMaxRawHeaders
)
4044 cchMaxRawHeaders
*= 2;
4045 lpszRawHeaders
= HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders
, (cchMaxRawHeaders
+1)*sizeof(WCHAR
));
4047 memcpy(lpszRawHeaders
+cchRawHeaders
, buffer
, (buflen
-1)*sizeof(WCHAR
));
4048 cchRawHeaders
+= (buflen
-1);
4049 memcpy(lpszRawHeaders
+cchRawHeaders
, szCrLf
, sizeof(szCrLf
));
4050 cchRawHeaders
+= sizeof(szCrLf
)/sizeof(szCrLf
[0])-1;
4051 lpszRawHeaders
[cchRawHeaders
] = '\0';
4053 /* Parse each response line */
4056 buflen
= MAX_REPLY_LEN
;
4057 if (read_line(lpwhr
, bufferA
, &buflen
))
4059 LPWSTR
* pFieldAndValue
;
4061 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA
));
4063 if (!bufferA
[0]) break;
4064 if (!strchr(bufferA
, ':'))
4066 WARN("invalid header\n");
4069 MultiByteToWideChar( CP_ACP
, 0, bufferA
, buflen
, buffer
, MAX_REPLY_LEN
);
4071 while (cchRawHeaders
+ buflen
+ strlenW(szCrLf
) > cchMaxRawHeaders
)
4073 cchMaxRawHeaders
*= 2;
4074 lpszRawHeaders
= HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders
, (cchMaxRawHeaders
+1)*sizeof(WCHAR
));
4076 memcpy(lpszRawHeaders
+cchRawHeaders
, buffer
, (buflen
-1)*sizeof(WCHAR
));
4077 cchRawHeaders
+= (buflen
-1);
4078 memcpy(lpszRawHeaders
+cchRawHeaders
, szCrLf
, sizeof(szCrLf
));
4079 cchRawHeaders
+= sizeof(szCrLf
)/sizeof(szCrLf
[0])-1;
4080 lpszRawHeaders
[cchRawHeaders
] = '\0';
4082 pFieldAndValue
= HTTP_InterpretHttpHeader(buffer
);
4083 if (!pFieldAndValue
)
4086 HTTP_ProcessHeader(lpwhr
, pFieldAndValue
[0], pFieldAndValue
[1],
4087 HTTP_ADDREQ_FLAG_ADD
);
4089 HTTP_FreeTokens(pFieldAndValue
);
4099 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszRawHeaders
);
4100 lpwhr
->lpszRawHeaders
= lpszRawHeaders
;
4101 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders
));
4111 HeapFree(GetProcessHeap(), 0, lpszRawHeaders
);
4117 static void strip_spaces(LPWSTR start
)
4122 while (*str
== ' ' && *str
!= '\0')
4126 memmove(start
, str
, sizeof(WCHAR
) * (strlenW(str
) + 1));
4128 end
= start
+ strlenW(start
) - 1;
4129 while (end
>= start
&& *end
== ' ')
4137 /***********************************************************************
4138 * HTTP_InterpretHttpHeader (internal)
4140 * Parse server response
4144 * Pointer to array of field, value, NULL on success.
4147 static LPWSTR
* HTTP_InterpretHttpHeader(LPCWSTR buffer
)
4149 LPWSTR
* pTokenPair
;
4153 pTokenPair
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*pTokenPair
)*3);
4155 pszColon
= strchrW(buffer
, ':');
4156 /* must have two tokens */
4159 HTTP_FreeTokens(pTokenPair
);
4161 TRACE("No ':' in line: %s\n", debugstr_w(buffer
));
4165 pTokenPair
[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon
- buffer
+ 1) * sizeof(WCHAR
));
4168 HTTP_FreeTokens(pTokenPair
);
4171 memcpy(pTokenPair
[0], buffer
, (pszColon
- buffer
) * sizeof(WCHAR
));
4172 pTokenPair
[0][pszColon
- buffer
] = '\0';
4176 len
= strlenW(pszColon
);
4177 pTokenPair
[1] = HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
4180 HTTP_FreeTokens(pTokenPair
);
4183 memcpy(pTokenPair
[1], pszColon
, (len
+ 1) * sizeof(WCHAR
));
4185 strip_spaces(pTokenPair
[0]);
4186 strip_spaces(pTokenPair
[1]);
4188 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair
[0]), debugstr_w(pTokenPair
[1]));
4192 /***********************************************************************
4193 * HTTP_ProcessHeader (internal)
4195 * Stuff header into header tables according to <dwModifier>
4199 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
4201 static BOOL
HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr
, LPCWSTR field
, LPCWSTR value
, DWORD dwModifier
)
4203 LPHTTPHEADERW lphttpHdr
= NULL
;
4204 BOOL bSuccess
= FALSE
;
4206 BOOL request_only
= dwModifier
& HTTP_ADDHDR_FLAG_REQ
;
4208 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field
), debugstr_w(value
), dwModifier
);
4210 /* REPLACE wins out over ADD */
4211 if (dwModifier
& HTTP_ADDHDR_FLAG_REPLACE
)
4212 dwModifier
&= ~HTTP_ADDHDR_FLAG_ADD
;
4214 if (dwModifier
& HTTP_ADDHDR_FLAG_ADD
)
4217 index
= HTTP_GetCustomHeaderIndex(lpwhr
, field
, 0, request_only
);
4221 if (dwModifier
& HTTP_ADDHDR_FLAG_ADD_IF_NEW
)
4225 lphttpHdr
= &lpwhr
->pCustHeaders
[index
];
4231 hdr
.lpszField
= (LPWSTR
)field
;
4232 hdr
.lpszValue
= (LPWSTR
)value
;
4233 hdr
.wFlags
= hdr
.wCount
= 0;
4235 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
4236 hdr
.wFlags
|= HDR_ISREQUEST
;
4238 return HTTP_InsertCustomHeader(lpwhr
, &hdr
);
4240 /* no value to delete */
4243 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
4244 lphttpHdr
->wFlags
|= HDR_ISREQUEST
;
4246 lphttpHdr
->wFlags
&= ~HDR_ISREQUEST
;
4248 if (dwModifier
& HTTP_ADDHDR_FLAG_REPLACE
)
4250 HTTP_DeleteCustomHeader( lpwhr
, index
);
4256 hdr
.lpszField
= (LPWSTR
)field
;
4257 hdr
.lpszValue
= (LPWSTR
)value
;
4258 hdr
.wFlags
= hdr
.wCount
= 0;
4260 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
4261 hdr
.wFlags
|= HDR_ISREQUEST
;
4263 return HTTP_InsertCustomHeader(lpwhr
, &hdr
);
4268 else if (dwModifier
& COALESCEFLAGS
)
4273 INT origlen
= strlenW(lphttpHdr
->lpszValue
);
4274 INT valuelen
= strlenW(value
);
4276 if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
)
4279 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
4281 else if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON
)
4284 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
4287 len
= origlen
+ valuelen
+ ((ch
> 0) ? 2 : 0);
4289 lpsztmp
= HeapReAlloc(GetProcessHeap(), 0, lphttpHdr
->lpszValue
, (len
+1)*sizeof(WCHAR
));
4292 lphttpHdr
->lpszValue
= lpsztmp
;
4293 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
4296 lphttpHdr
->lpszValue
[origlen
] = ch
;
4298 lphttpHdr
->lpszValue
[origlen
] = ' ';
4302 memcpy(&lphttpHdr
->lpszValue
[origlen
], value
, valuelen
*sizeof(WCHAR
));
4303 lphttpHdr
->lpszValue
[len
] = '\0';
4308 WARN("HeapReAlloc (%d bytes) failed\n",len
+1);
4309 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
4312 TRACE("<-- %d\n",bSuccess
);
4317 /***********************************************************************
4318 * HTTP_FinishedReading (internal)
4320 * Called when all content from server has been read by client.
4323 static BOOL
HTTP_FinishedReading(LPWININETHTTPREQW lpwhr
)
4325 WCHAR szVersion
[10];
4326 WCHAR szConnectionResponse
[20];
4327 DWORD dwBufferSize
= sizeof(szVersion
);
4328 BOOL keepalive
= FALSE
;
4332 /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
4333 * the connection is keep-alive by default */
4334 if (HTTP_HttpQueryInfoW(lpwhr
, HTTP_QUERY_VERSION
, szVersion
,
4335 &dwBufferSize
, NULL
) &&
4336 !strcmpiW(szVersion
, g_szHttp1_1
))
4341 dwBufferSize
= sizeof(szConnectionResponse
);
4342 if (HTTP_HttpQueryInfoW(lpwhr
, HTTP_QUERY_PROXY_CONNECTION
, szConnectionResponse
, &dwBufferSize
, NULL
) ||
4343 HTTP_HttpQueryInfoW(lpwhr
, HTTP_QUERY_CONNECTION
, szConnectionResponse
, &dwBufferSize
, NULL
))
4345 keepalive
= !strcmpiW(szConnectionResponse
, szKeepAlive
);
4350 HTTPREQ_CloseConnection(&lpwhr
->hdr
);
4353 /* FIXME: store data in the URL cache here */
4359 /***********************************************************************
4360 * HTTP_GetCustomHeaderIndex (internal)
4362 * Return index of custom header from header array
4365 static INT
HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszField
,
4366 int requested_index
, BOOL request_only
)
4370 TRACE("%s\n", debugstr_w(lpszField
));
4372 for (index
= 0; index
< lpwhr
->nCustHeaders
; index
++)
4374 if (strcmpiW(lpwhr
->pCustHeaders
[index
].lpszField
, lpszField
))
4377 if (request_only
&& !(lpwhr
->pCustHeaders
[index
].wFlags
& HDR_ISREQUEST
))
4380 if (!request_only
&& (lpwhr
->pCustHeaders
[index
].wFlags
& HDR_ISREQUEST
))
4383 if (requested_index
== 0)
4388 if (index
>= lpwhr
->nCustHeaders
)
4391 TRACE("Return: %d\n", index
);
4396 /***********************************************************************
4397 * HTTP_InsertCustomHeader (internal)
4399 * Insert header into array
4402 static BOOL
HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr
, LPHTTPHEADERW lpHdr
)
4405 LPHTTPHEADERW lph
= NULL
;
4408 TRACE("--> %s: %s\n", debugstr_w(lpHdr
->lpszField
), debugstr_w(lpHdr
->lpszValue
));
4409 count
= lpwhr
->nCustHeaders
+ 1;
4411 lph
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, lpwhr
->pCustHeaders
, sizeof(HTTPHEADERW
) * count
);
4413 lph
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(HTTPHEADERW
) * count
);
4417 lpwhr
->pCustHeaders
= lph
;
4418 lpwhr
->pCustHeaders
[count
-1].lpszField
= WININET_strdupW(lpHdr
->lpszField
);
4419 lpwhr
->pCustHeaders
[count
-1].lpszValue
= WININET_strdupW(lpHdr
->lpszValue
);
4420 lpwhr
->pCustHeaders
[count
-1].wFlags
= lpHdr
->wFlags
;
4421 lpwhr
->pCustHeaders
[count
-1].wCount
= lpHdr
->wCount
;
4422 lpwhr
->nCustHeaders
++;
4427 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
4434 /***********************************************************************
4435 * HTTP_DeleteCustomHeader (internal)
4437 * Delete header from array
4438 * If this function is called, the indexs may change.
4440 static BOOL
HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr
, DWORD index
)
4442 if( lpwhr
->nCustHeaders
<= 0 )
4444 if( index
>= lpwhr
->nCustHeaders
)
4446 lpwhr
->nCustHeaders
--;
4448 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
[index
].lpszField
);
4449 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
[index
].lpszValue
);
4451 memmove( &lpwhr
->pCustHeaders
[index
], &lpwhr
->pCustHeaders
[index
+1],
4452 (lpwhr
->nCustHeaders
- index
)* sizeof(HTTPHEADERW
) );
4453 memset( &lpwhr
->pCustHeaders
[lpwhr
->nCustHeaders
], 0, sizeof(HTTPHEADERW
) );
4459 /***********************************************************************
4460 * HTTP_VerifyValidHeader (internal)
4462 * Verify the given header is not invalid for the given http request
4465 static BOOL
HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr
, LPCWSTR field
)
4467 /* Accept-Encoding is stripped from HTTP/1.0 requests. It is invalid */
4468 if (!strcmpW(lpwhr
->lpszVersion
, g_szHttp1_0
) && !strcmpiW(field
, szAccept_Encoding
))
4474 /***********************************************************************
4475 * IsHostInProxyBypassList (@)
4480 BOOL WINAPI
IsHostInProxyBypassList(DWORD flags
, LPCSTR szHost
, DWORD length
)
4482 FIXME("STUB: flags=%d host=%s length=%d\n",flags
,szHost
,length
);