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
);
2215 if (lpszReferrer
&& *lpszReferrer
)
2216 HTTP_ProcessHeader(lpwhr
, HTTP_REFERER
, lpszReferrer
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REQ
);
2218 if (lpszAcceptTypes
)
2221 for (i
= 0; lpszAcceptTypes
[i
]; i
++)
2223 if (!*lpszAcceptTypes
[i
]) continue;
2224 HTTP_ProcessHeader(lpwhr
, HTTP_ACCEPT
, lpszAcceptTypes
[i
],
2225 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
|
2226 HTTP_ADDHDR_FLAG_REQ
|
2227 (i
== 0 ? HTTP_ADDHDR_FLAG_REPLACE
: 0));
2231 lpwhr
->lpszVerb
= WININET_strdupW(lpszVerb
&& *lpszVerb
? lpszVerb
: szGET
);
2234 lpwhr
->lpszVersion
= WININET_strdupW(lpszVersion
);
2236 lpwhr
->lpszVersion
= WININET_strdupW(g_szHttp1_1
);
2238 if (lpwhs
->nHostPort
!= INTERNET_INVALID_PORT_NUMBER
&&
2239 lpwhs
->nHostPort
!= INTERNET_DEFAULT_HTTP_PORT
&&
2240 lpwhs
->nHostPort
!= INTERNET_DEFAULT_HTTPS_PORT
)
2242 sprintfW(lpszHostName
, szHostForm
, lpwhs
->lpszHostName
, lpwhs
->nHostPort
);
2243 HTTP_ProcessHeader(lpwhr
, hostW
, lpszHostName
,
2244 HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REQ
);
2247 HTTP_ProcessHeader(lpwhr
, hostW
, lpwhs
->lpszHostName
,
2248 HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REQ
);
2250 if (lpwhs
->nServerPort
== INTERNET_INVALID_PORT_NUMBER
)
2251 lpwhs
->nServerPort
= (dwFlags
& INTERNET_FLAG_SECURE
?
2252 INTERNET_DEFAULT_HTTPS_PORT
:
2253 INTERNET_DEFAULT_HTTP_PORT
);
2255 if (lpwhs
->nHostPort
== INTERNET_INVALID_PORT_NUMBER
)
2256 lpwhs
->nHostPort
= (dwFlags
& INTERNET_FLAG_SECURE
?
2257 INTERNET_DEFAULT_HTTPS_PORT
:
2258 INTERNET_DEFAULT_HTTP_PORT
);
2260 if (NULL
!= hIC
->lpszProxy
&& hIC
->lpszProxy
[0] != 0)
2261 HTTP_DealWithProxy( hIC
, lpwhs
, lpwhr
);
2263 INTERNET_SendCallback(&lpwhs
->hdr
, dwContext
,
2264 INTERNET_STATUS_HANDLE_CREATED
, &handle
,
2268 HeapFree(GetProcessHeap(), 0, lpszHostName
);
2270 WININET_Release( &lpwhr
->hdr
);
2272 TRACE("<-- %p (%p)\n", handle
, lpwhr
);
2276 /* read any content returned by the server so that the connection can be
2278 static void HTTP_DrainContent(WININETHTTPREQW
*req
)
2282 if (!NETCON_connected(&req
->netConnection
)) return;
2284 if (req
->dwContentLength
== -1)
2286 NETCON_close(&req
->netConnection
);
2293 if (HTTPREQ_Read(req
, buffer
, sizeof(buffer
), &bytes_read
, TRUE
) != ERROR_SUCCESS
)
2295 } while (bytes_read
);
2298 static const LPCWSTR header_lookup
[] = {
2299 szMime_Version
, /* HTTP_QUERY_MIME_VERSION = 0 */
2300 szContent_Type
, /* HTTP_QUERY_CONTENT_TYPE = 1 */
2301 szContent_Transfer_Encoding
,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
2302 szContent_ID
, /* HTTP_QUERY_CONTENT_ID = 3 */
2303 NULL
, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
2304 szContent_Length
, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
2305 szContent_Language
, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
2306 szAllow
, /* HTTP_QUERY_ALLOW = 7 */
2307 szPublic
, /* HTTP_QUERY_PUBLIC = 8 */
2308 szDate
, /* HTTP_QUERY_DATE = 9 */
2309 szExpires
, /* HTTP_QUERY_EXPIRES = 10 */
2310 szLast_Modified
, /* HTTP_QUERY_LAST_MODIFIED = 11 */
2311 NULL
, /* HTTP_QUERY_MESSAGE_ID = 12 */
2312 szURI
, /* HTTP_QUERY_URI = 13 */
2313 szFrom
, /* HTTP_QUERY_DERIVED_FROM = 14 */
2314 NULL
, /* HTTP_QUERY_COST = 15 */
2315 NULL
, /* HTTP_QUERY_LINK = 16 */
2316 szPragma
, /* HTTP_QUERY_PRAGMA = 17 */
2317 NULL
, /* HTTP_QUERY_VERSION = 18 */
2318 szStatus
, /* HTTP_QUERY_STATUS_CODE = 19 */
2319 NULL
, /* HTTP_QUERY_STATUS_TEXT = 20 */
2320 NULL
, /* HTTP_QUERY_RAW_HEADERS = 21 */
2321 NULL
, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
2322 szConnection
, /* HTTP_QUERY_CONNECTION = 23 */
2323 szAccept
, /* HTTP_QUERY_ACCEPT = 24 */
2324 szAccept_Charset
, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
2325 szAccept_Encoding
, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
2326 szAccept_Language
, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
2327 szAuthorization
, /* HTTP_QUERY_AUTHORIZATION = 28 */
2328 szContent_Encoding
, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
2329 NULL
, /* HTTP_QUERY_FORWARDED = 30 */
2330 NULL
, /* HTTP_QUERY_FROM = 31 */
2331 szIf_Modified_Since
, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
2332 szLocation
, /* HTTP_QUERY_LOCATION = 33 */
2333 NULL
, /* HTTP_QUERY_ORIG_URI = 34 */
2334 szReferer
, /* HTTP_QUERY_REFERER = 35 */
2335 szRetry_After
, /* HTTP_QUERY_RETRY_AFTER = 36 */
2336 szServer
, /* HTTP_QUERY_SERVER = 37 */
2337 NULL
, /* HTTP_TITLE = 38 */
2338 szUser_Agent
, /* HTTP_QUERY_USER_AGENT = 39 */
2339 szWWW_Authenticate
, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
2340 szProxy_Authenticate
, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
2341 szAccept_Ranges
, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
2342 szSet_Cookie
, /* HTTP_QUERY_SET_COOKIE = 43 */
2343 szCookie
, /* HTTP_QUERY_COOKIE = 44 */
2344 NULL
, /* HTTP_QUERY_REQUEST_METHOD = 45 */
2345 NULL
, /* HTTP_QUERY_REFRESH = 46 */
2346 NULL
, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
2347 szAge
, /* HTTP_QUERY_AGE = 48 */
2348 szCache_Control
, /* HTTP_QUERY_CACHE_CONTROL = 49 */
2349 szContent_Base
, /* HTTP_QUERY_CONTENT_BASE = 50 */
2350 szContent_Location
, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
2351 szContent_MD5
, /* HTTP_QUERY_CONTENT_MD5 = 52 */
2352 szContent_Range
, /* HTTP_QUERY_CONTENT_RANGE = 53 */
2353 szETag
, /* HTTP_QUERY_ETAG = 54 */
2354 hostW
, /* HTTP_QUERY_HOST = 55 */
2355 szIf_Match
, /* HTTP_QUERY_IF_MATCH = 56 */
2356 szIf_None_Match
, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
2357 szIf_Range
, /* HTTP_QUERY_IF_RANGE = 58 */
2358 szIf_Unmodified_Since
, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
2359 szMax_Forwards
, /* HTTP_QUERY_MAX_FORWARDS = 60 */
2360 szProxy_Authorization
, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
2361 szRange
, /* HTTP_QUERY_RANGE = 62 */
2362 szTransfer_Encoding
, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
2363 szUpgrade
, /* HTTP_QUERY_UPGRADE = 64 */
2364 szVary
, /* HTTP_QUERY_VARY = 65 */
2365 szVia
, /* HTTP_QUERY_VIA = 66 */
2366 szWarning
, /* HTTP_QUERY_WARNING = 67 */
2367 szExpect
, /* HTTP_QUERY_EXPECT = 68 */
2368 szProxy_Connection
, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
2369 szUnless_Modified_Since
, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
2372 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
2374 /***********************************************************************
2375 * HTTP_HttpQueryInfoW (internal)
2377 static BOOL
HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr
, DWORD dwInfoLevel
,
2378 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
2380 LPHTTPHEADERW lphttpHdr
= NULL
;
2381 BOOL bSuccess
= FALSE
;
2382 BOOL request_only
= dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
;
2383 INT requested_index
= lpdwIndex
? *lpdwIndex
: 0;
2384 DWORD level
= (dwInfoLevel
& ~HTTP_QUERY_MODIFIER_FLAGS_MASK
);
2387 /* Find requested header structure */
2390 case HTTP_QUERY_CUSTOM
:
2391 if (!lpBuffer
) return FALSE
;
2392 index
= HTTP_GetCustomHeaderIndex(lpwhr
, lpBuffer
, requested_index
, request_only
);
2395 case HTTP_QUERY_RAW_HEADERS_CRLF
:
2402 headers
= HTTP_BuildHeaderRequestString(lpwhr
, lpwhr
->lpszVerb
, lpwhr
->lpszPath
, lpwhr
->lpszVersion
);
2404 headers
= lpwhr
->lpszRawHeaders
;
2407 len
= strlenW(headers
) * sizeof(WCHAR
);
2409 if (len
+ sizeof(WCHAR
) > *lpdwBufferLength
)
2411 len
+= sizeof(WCHAR
);
2412 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2418 memcpy(lpBuffer
, headers
, len
+ sizeof(WCHAR
));
2421 len
= strlenW(szCrLf
) * sizeof(WCHAR
);
2422 memcpy(lpBuffer
, szCrLf
, sizeof(szCrLf
));
2424 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
/ sizeof(WCHAR
)));
2427 *lpdwBufferLength
= len
;
2430 HeapFree(GetProcessHeap(), 0, headers
);
2433 case HTTP_QUERY_RAW_HEADERS
:
2435 LPWSTR
* ppszRawHeaderLines
= HTTP_Tokenize(lpwhr
->lpszRawHeaders
, szCrLf
);
2437 LPWSTR pszString
= lpBuffer
;
2439 for (i
= 0; ppszRawHeaderLines
[i
]; i
++)
2440 size
+= strlenW(ppszRawHeaderLines
[i
]) + 1;
2442 if (size
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
2444 HTTP_FreeTokens(ppszRawHeaderLines
);
2445 *lpdwBufferLength
= (size
+ 1) * sizeof(WCHAR
);
2446 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2451 for (i
= 0; ppszRawHeaderLines
[i
]; i
++)
2453 DWORD len
= strlenW(ppszRawHeaderLines
[i
]);
2454 memcpy(pszString
, ppszRawHeaderLines
[i
], (len
+1)*sizeof(WCHAR
));
2458 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, size
));
2460 *lpdwBufferLength
= size
* sizeof(WCHAR
);
2461 HTTP_FreeTokens(ppszRawHeaderLines
);
2465 case HTTP_QUERY_STATUS_TEXT
:
2466 if (lpwhr
->lpszStatusText
)
2468 DWORD len
= strlenW(lpwhr
->lpszStatusText
);
2469 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
2471 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
2472 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2477 memcpy(lpBuffer
, lpwhr
->lpszStatusText
, (len
+ 1) * sizeof(WCHAR
));
2478 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
));
2480 *lpdwBufferLength
= len
* sizeof(WCHAR
);
2484 case HTTP_QUERY_VERSION
:
2485 if (lpwhr
->lpszVersion
)
2487 DWORD len
= strlenW(lpwhr
->lpszVersion
);
2488 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
2490 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
2491 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2496 memcpy(lpBuffer
, lpwhr
->lpszVersion
, (len
+ 1) * sizeof(WCHAR
));
2497 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
));
2499 *lpdwBufferLength
= len
* sizeof(WCHAR
);
2504 assert (LAST_TABLE_HEADER
== (HTTP_QUERY_UNLESS_MODIFIED_SINCE
+ 1));
2506 if (level
< LAST_TABLE_HEADER
&& header_lookup
[level
])
2507 index
= HTTP_GetCustomHeaderIndex(lpwhr
, header_lookup
[level
],
2508 requested_index
,request_only
);
2512 lphttpHdr
= &lpwhr
->pCustHeaders
[index
];
2514 /* Ensure header satisfies requested attributes */
2516 ((dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
) &&
2517 (~lphttpHdr
->wFlags
& HDR_ISREQUEST
)))
2519 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND
);
2523 if (lpdwIndex
&& level
!= HTTP_QUERY_STATUS_CODE
) (*lpdwIndex
)++;
2525 /* coalesce value to requested type */
2526 if (dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
&& lpBuffer
)
2528 *(int *)lpBuffer
= atoiW(lphttpHdr
->lpszValue
);
2529 TRACE(" returning number: %d\n", *(int *)lpBuffer
);
2532 else if (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
&& lpBuffer
)
2538 tmpTime
= ConvertTimeString(lphttpHdr
->lpszValue
);
2540 tmpTM
= *gmtime(&tmpTime
);
2541 STHook
= (SYSTEMTIME
*)lpBuffer
;
2542 STHook
->wDay
= tmpTM
.tm_mday
;
2543 STHook
->wHour
= tmpTM
.tm_hour
;
2544 STHook
->wMilliseconds
= 0;
2545 STHook
->wMinute
= tmpTM
.tm_min
;
2546 STHook
->wDayOfWeek
= tmpTM
.tm_wday
;
2547 STHook
->wMonth
= tmpTM
.tm_mon
+ 1;
2548 STHook
->wSecond
= tmpTM
.tm_sec
;
2549 STHook
->wYear
= tmpTM
.tm_year
;
2552 TRACE(" returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
2553 STHook
->wYear
, STHook
->wMonth
, STHook
->wDay
, STHook
->wDayOfWeek
,
2554 STHook
->wHour
, STHook
->wMinute
, STHook
->wSecond
, STHook
->wMilliseconds
);
2556 else if (lphttpHdr
->lpszValue
)
2558 DWORD len
= (strlenW(lphttpHdr
->lpszValue
) + 1) * sizeof(WCHAR
);
2560 if (len
> *lpdwBufferLength
)
2562 *lpdwBufferLength
= len
;
2563 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2568 memcpy(lpBuffer
, lphttpHdr
->lpszValue
, len
);
2569 TRACE(" returning string: %s\n", debugstr_w(lpBuffer
));
2571 *lpdwBufferLength
= len
- sizeof(WCHAR
);
2577 /***********************************************************************
2578 * HttpQueryInfoW (WININET.@)
2580 * Queries for information about an HTTP request
2587 BOOL WINAPI
HttpQueryInfoW(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
2588 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
2590 BOOL bSuccess
= FALSE
;
2591 LPWININETHTTPREQW lpwhr
;
2593 if (TRACE_ON(wininet
)) {
2594 #define FE(x) { x, #x }
2595 static const wininet_flag_info query_flags
[] = {
2596 FE(HTTP_QUERY_MIME_VERSION
),
2597 FE(HTTP_QUERY_CONTENT_TYPE
),
2598 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING
),
2599 FE(HTTP_QUERY_CONTENT_ID
),
2600 FE(HTTP_QUERY_CONTENT_DESCRIPTION
),
2601 FE(HTTP_QUERY_CONTENT_LENGTH
),
2602 FE(HTTP_QUERY_CONTENT_LANGUAGE
),
2603 FE(HTTP_QUERY_ALLOW
),
2604 FE(HTTP_QUERY_PUBLIC
),
2605 FE(HTTP_QUERY_DATE
),
2606 FE(HTTP_QUERY_EXPIRES
),
2607 FE(HTTP_QUERY_LAST_MODIFIED
),
2608 FE(HTTP_QUERY_MESSAGE_ID
),
2610 FE(HTTP_QUERY_DERIVED_FROM
),
2611 FE(HTTP_QUERY_COST
),
2612 FE(HTTP_QUERY_LINK
),
2613 FE(HTTP_QUERY_PRAGMA
),
2614 FE(HTTP_QUERY_VERSION
),
2615 FE(HTTP_QUERY_STATUS_CODE
),
2616 FE(HTTP_QUERY_STATUS_TEXT
),
2617 FE(HTTP_QUERY_RAW_HEADERS
),
2618 FE(HTTP_QUERY_RAW_HEADERS_CRLF
),
2619 FE(HTTP_QUERY_CONNECTION
),
2620 FE(HTTP_QUERY_ACCEPT
),
2621 FE(HTTP_QUERY_ACCEPT_CHARSET
),
2622 FE(HTTP_QUERY_ACCEPT_ENCODING
),
2623 FE(HTTP_QUERY_ACCEPT_LANGUAGE
),
2624 FE(HTTP_QUERY_AUTHORIZATION
),
2625 FE(HTTP_QUERY_CONTENT_ENCODING
),
2626 FE(HTTP_QUERY_FORWARDED
),
2627 FE(HTTP_QUERY_FROM
),
2628 FE(HTTP_QUERY_IF_MODIFIED_SINCE
),
2629 FE(HTTP_QUERY_LOCATION
),
2630 FE(HTTP_QUERY_ORIG_URI
),
2631 FE(HTTP_QUERY_REFERER
),
2632 FE(HTTP_QUERY_RETRY_AFTER
),
2633 FE(HTTP_QUERY_SERVER
),
2634 FE(HTTP_QUERY_TITLE
),
2635 FE(HTTP_QUERY_USER_AGENT
),
2636 FE(HTTP_QUERY_WWW_AUTHENTICATE
),
2637 FE(HTTP_QUERY_PROXY_AUTHENTICATE
),
2638 FE(HTTP_QUERY_ACCEPT_RANGES
),
2639 FE(HTTP_QUERY_SET_COOKIE
),
2640 FE(HTTP_QUERY_COOKIE
),
2641 FE(HTTP_QUERY_REQUEST_METHOD
),
2642 FE(HTTP_QUERY_REFRESH
),
2643 FE(HTTP_QUERY_CONTENT_DISPOSITION
),
2645 FE(HTTP_QUERY_CACHE_CONTROL
),
2646 FE(HTTP_QUERY_CONTENT_BASE
),
2647 FE(HTTP_QUERY_CONTENT_LOCATION
),
2648 FE(HTTP_QUERY_CONTENT_MD5
),
2649 FE(HTTP_QUERY_CONTENT_RANGE
),
2650 FE(HTTP_QUERY_ETAG
),
2651 FE(HTTP_QUERY_HOST
),
2652 FE(HTTP_QUERY_IF_MATCH
),
2653 FE(HTTP_QUERY_IF_NONE_MATCH
),
2654 FE(HTTP_QUERY_IF_RANGE
),
2655 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE
),
2656 FE(HTTP_QUERY_MAX_FORWARDS
),
2657 FE(HTTP_QUERY_PROXY_AUTHORIZATION
),
2658 FE(HTTP_QUERY_RANGE
),
2659 FE(HTTP_QUERY_TRANSFER_ENCODING
),
2660 FE(HTTP_QUERY_UPGRADE
),
2661 FE(HTTP_QUERY_VARY
),
2663 FE(HTTP_QUERY_WARNING
),
2664 FE(HTTP_QUERY_CUSTOM
)
2666 static const wininet_flag_info modifier_flags
[] = {
2667 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS
),
2668 FE(HTTP_QUERY_FLAG_SYSTEMTIME
),
2669 FE(HTTP_QUERY_FLAG_NUMBER
),
2670 FE(HTTP_QUERY_FLAG_COALESCE
)
2673 DWORD info_mod
= dwInfoLevel
& HTTP_QUERY_MODIFIER_FLAGS_MASK
;
2674 DWORD info
= dwInfoLevel
& HTTP_QUERY_HEADER_MASK
;
2677 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest
, dwInfoLevel
, dwInfoLevel
);
2678 TRACE(" Attribute:");
2679 for (i
= 0; i
< (sizeof(query_flags
) / sizeof(query_flags
[0])); i
++) {
2680 if (query_flags
[i
].val
== info
) {
2681 TRACE(" %s", query_flags
[i
].name
);
2685 if (i
== (sizeof(query_flags
) / sizeof(query_flags
[0]))) {
2686 TRACE(" Unknown (%08x)", info
);
2689 TRACE(" Modifier:");
2690 for (i
= 0; i
< (sizeof(modifier_flags
) / sizeof(modifier_flags
[0])); i
++) {
2691 if (modifier_flags
[i
].val
& info_mod
) {
2692 TRACE(" %s", modifier_flags
[i
].name
);
2693 info_mod
&= ~ modifier_flags
[i
].val
;
2698 TRACE(" Unknown (%08x)", info_mod
);
2703 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hHttpRequest
);
2704 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
2706 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2710 if (lpBuffer
== NULL
)
2711 *lpdwBufferLength
= 0;
2712 bSuccess
= HTTP_HttpQueryInfoW( lpwhr
, dwInfoLevel
,
2713 lpBuffer
, lpdwBufferLength
, lpdwIndex
);
2717 WININET_Release( &lpwhr
->hdr
);
2719 TRACE("%d <--\n", bSuccess
);
2723 /***********************************************************************
2724 * HttpQueryInfoA (WININET.@)
2726 * Queries for information about an HTTP request
2733 BOOL WINAPI
HttpQueryInfoA(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
2734 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
2740 if((dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
) ||
2741 (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
))
2743 return HttpQueryInfoW( hHttpRequest
, dwInfoLevel
, lpBuffer
,
2744 lpdwBufferLength
, lpdwIndex
);
2750 len
= (*lpdwBufferLength
)*sizeof(WCHAR
);
2751 if ((dwInfoLevel
& HTTP_QUERY_HEADER_MASK
) == HTTP_QUERY_CUSTOM
)
2753 alloclen
= MultiByteToWideChar( CP_ACP
, 0, lpBuffer
, -1, NULL
, 0 ) * sizeof(WCHAR
);
2759 bufferW
= HeapAlloc( GetProcessHeap(), 0, alloclen
);
2760 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
2761 if ((dwInfoLevel
& HTTP_QUERY_HEADER_MASK
) == HTTP_QUERY_CUSTOM
)
2762 MultiByteToWideChar( CP_ACP
, 0, lpBuffer
, -1, bufferW
, alloclen
/ sizeof(WCHAR
) );
2769 result
= HttpQueryInfoW( hHttpRequest
, dwInfoLevel
, bufferW
,
2773 len
= WideCharToMultiByte( CP_ACP
,0, bufferW
, len
/ sizeof(WCHAR
) + 1,
2774 lpBuffer
, *lpdwBufferLength
, NULL
, NULL
);
2775 *lpdwBufferLength
= len
- 1;
2777 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer
));
2780 /* since the strings being returned from HttpQueryInfoW should be
2781 * only ASCII characters, it is reasonable to assume that all of
2782 * the Unicode characters can be reduced to a single byte */
2783 *lpdwBufferLength
= len
/ sizeof(WCHAR
);
2785 HeapFree(GetProcessHeap(), 0, bufferW
);
2790 /***********************************************************************
2791 * HttpSendRequestExA (WININET.@)
2793 * Sends the specified request to the HTTP server and allows chunked
2798 * Failure: FALSE, call GetLastError() for more information.
2800 BOOL WINAPI
HttpSendRequestExA(HINTERNET hRequest
,
2801 LPINTERNET_BUFFERSA lpBuffersIn
,
2802 LPINTERNET_BUFFERSA lpBuffersOut
,
2803 DWORD dwFlags
, DWORD_PTR dwContext
)
2805 INTERNET_BUFFERSW BuffersInW
;
2808 LPWSTR header
= NULL
;
2810 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest
, lpBuffersIn
,
2811 lpBuffersOut
, dwFlags
, dwContext
);
2815 BuffersInW
.dwStructSize
= sizeof(LPINTERNET_BUFFERSW
);
2816 if (lpBuffersIn
->lpcszHeader
)
2818 headerlen
= MultiByteToWideChar(CP_ACP
,0,lpBuffersIn
->lpcszHeader
,
2819 lpBuffersIn
->dwHeadersLength
,0,0);
2820 header
= HeapAlloc(GetProcessHeap(),0,headerlen
*sizeof(WCHAR
));
2821 if (!(BuffersInW
.lpcszHeader
= header
))
2823 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2826 BuffersInW
.dwHeadersLength
= MultiByteToWideChar(CP_ACP
, 0,
2827 lpBuffersIn
->lpcszHeader
, lpBuffersIn
->dwHeadersLength
,
2831 BuffersInW
.lpcszHeader
= NULL
;
2832 BuffersInW
.dwHeadersTotal
= lpBuffersIn
->dwHeadersTotal
;
2833 BuffersInW
.lpvBuffer
= lpBuffersIn
->lpvBuffer
;
2834 BuffersInW
.dwBufferLength
= lpBuffersIn
->dwBufferLength
;
2835 BuffersInW
.dwBufferTotal
= lpBuffersIn
->dwBufferTotal
;
2836 BuffersInW
.Next
= NULL
;
2839 rc
= HttpSendRequestExW(hRequest
, lpBuffersIn
? &BuffersInW
: NULL
, NULL
, dwFlags
, dwContext
);
2841 HeapFree(GetProcessHeap(),0,header
);
2846 /***********************************************************************
2847 * HttpSendRequestExW (WININET.@)
2849 * Sends the specified request to the HTTP server and allows chunked
2854 * Failure: FALSE, call GetLastError() for more information.
2856 BOOL WINAPI
HttpSendRequestExW(HINTERNET hRequest
,
2857 LPINTERNET_BUFFERSW lpBuffersIn
,
2858 LPINTERNET_BUFFERSW lpBuffersOut
,
2859 DWORD dwFlags
, DWORD_PTR dwContext
)
2862 LPWININETHTTPREQW lpwhr
;
2863 LPWININETHTTPSESSIONW lpwhs
;
2864 LPWININETAPPINFOW hIC
;
2866 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest
, lpBuffersIn
,
2867 lpBuffersOut
, dwFlags
, dwContext
);
2869 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hRequest
);
2871 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
2873 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2877 lpwhs
= lpwhr
->lpHttpSession
;
2878 assert(lpwhs
->hdr
.htype
== WH_HHTTPSESSION
);
2879 hIC
= lpwhs
->lpAppInfo
;
2880 assert(hIC
->hdr
.htype
== WH_HINIT
);
2882 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2884 WORKREQUEST workRequest
;
2885 struct WORKREQ_HTTPSENDREQUESTW
*req
;
2887 workRequest
.asyncproc
= AsyncHttpSendRequestProc
;
2888 workRequest
.hdr
= WININET_AddRef( &lpwhr
->hdr
);
2889 req
= &workRequest
.u
.HttpSendRequestW
;
2892 if (lpBuffersIn
->lpcszHeader
)
2893 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
2894 req
->lpszHeader
= WININET_strdupW(lpBuffersIn
->lpcszHeader
);
2896 req
->lpszHeader
= NULL
;
2897 req
->dwHeaderLength
= lpBuffersIn
->dwHeadersLength
;
2898 req
->lpOptional
= lpBuffersIn
->lpvBuffer
;
2899 req
->dwOptionalLength
= lpBuffersIn
->dwBufferLength
;
2900 req
->dwContentLength
= lpBuffersIn
->dwBufferTotal
;
2904 req
->lpszHeader
= NULL
;
2905 req
->dwHeaderLength
= 0;
2906 req
->lpOptional
= NULL
;
2907 req
->dwOptionalLength
= 0;
2908 req
->dwContentLength
= 0;
2911 req
->bEndRequest
= FALSE
;
2913 INTERNET_AsyncCall(&workRequest
);
2915 * This is from windows.
2917 INTERNET_SetLastError(ERROR_IO_PENDING
);
2922 ret
= HTTP_HttpSendRequestW(lpwhr
, lpBuffersIn
->lpcszHeader
, lpBuffersIn
->dwHeadersLength
,
2923 lpBuffersIn
->lpvBuffer
, lpBuffersIn
->dwBufferLength
,
2924 lpBuffersIn
->dwBufferTotal
, FALSE
);
2926 ret
= HTTP_HttpSendRequestW(lpwhr
, NULL
, 0, NULL
, 0, 0, FALSE
);
2931 WININET_Release( &lpwhr
->hdr
);
2937 /***********************************************************************
2938 * HttpSendRequestW (WININET.@)
2940 * Sends the specified request to the HTTP server
2947 BOOL WINAPI
HttpSendRequestW(HINTERNET hHttpRequest
, LPCWSTR lpszHeaders
,
2948 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
2950 LPWININETHTTPREQW lpwhr
;
2951 LPWININETHTTPSESSIONW lpwhs
= NULL
;
2952 LPWININETAPPINFOW hIC
= NULL
;
2955 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest
,
2956 debugstr_wn(lpszHeaders
, dwHeaderLength
), dwHeaderLength
, lpOptional
, dwOptionalLength
);
2958 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hHttpRequest
);
2959 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
2961 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2966 lpwhs
= lpwhr
->lpHttpSession
;
2967 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
2969 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2974 hIC
= lpwhs
->lpAppInfo
;
2975 if (NULL
== hIC
|| hIC
->hdr
.htype
!= WH_HINIT
)
2977 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2982 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2984 WORKREQUEST workRequest
;
2985 struct WORKREQ_HTTPSENDREQUESTW
*req
;
2987 workRequest
.asyncproc
= AsyncHttpSendRequestProc
;
2988 workRequest
.hdr
= WININET_AddRef( &lpwhr
->hdr
);
2989 req
= &workRequest
.u
.HttpSendRequestW
;
2994 if (dwHeaderLength
== ~0u) size
= (strlenW(lpszHeaders
) + 1) * sizeof(WCHAR
);
2995 else size
= dwHeaderLength
* sizeof(WCHAR
);
2997 req
->lpszHeader
= HeapAlloc(GetProcessHeap(), 0, size
);
2998 memcpy(req
->lpszHeader
, lpszHeaders
, size
);
3001 req
->lpszHeader
= 0;
3002 req
->dwHeaderLength
= dwHeaderLength
;
3003 req
->lpOptional
= lpOptional
;
3004 req
->dwOptionalLength
= dwOptionalLength
;
3005 req
->dwContentLength
= dwOptionalLength
;
3006 req
->bEndRequest
= TRUE
;
3008 INTERNET_AsyncCall(&workRequest
);
3010 * This is from windows.
3012 INTERNET_SetLastError(ERROR_IO_PENDING
);
3017 r
= HTTP_HttpSendRequestW(lpwhr
, lpszHeaders
,
3018 dwHeaderLength
, lpOptional
, dwOptionalLength
,
3019 dwOptionalLength
, TRUE
);
3023 WININET_Release( &lpwhr
->hdr
);
3027 /***********************************************************************
3028 * HttpSendRequestA (WININET.@)
3030 * Sends the specified request to the HTTP server
3037 BOOL WINAPI
HttpSendRequestA(HINTERNET hHttpRequest
, LPCSTR lpszHeaders
,
3038 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
3041 LPWSTR szHeaders
=NULL
;
3042 DWORD nLen
=dwHeaderLength
;
3043 if(lpszHeaders
!=NULL
)
3045 nLen
=MultiByteToWideChar(CP_ACP
,0,lpszHeaders
,dwHeaderLength
,NULL
,0);
3046 szHeaders
=HeapAlloc(GetProcessHeap(),0,nLen
*sizeof(WCHAR
));
3047 MultiByteToWideChar(CP_ACP
,0,lpszHeaders
,dwHeaderLength
,szHeaders
,nLen
);
3049 result
=HttpSendRequestW(hHttpRequest
, szHeaders
, nLen
, lpOptional
, dwOptionalLength
);
3050 HeapFree(GetProcessHeap(),0,szHeaders
);
3054 static BOOL
HTTP_GetRequestURL(WININETHTTPREQW
*req
, LPWSTR buf
)
3056 LPHTTPHEADERW host_header
;
3058 static const WCHAR formatW
[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
3060 host_header
= HTTP_GetHeader(req
, hostW
);
3064 sprintfW(buf
, formatW
, host_header
->lpszValue
, req
->lpszPath
); /* FIXME */
3068 /***********************************************************************
3069 * HTTP_GetRedirectURL (internal)
3071 static LPWSTR
HTTP_GetRedirectURL(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszUrl
)
3073 static WCHAR szHttp
[] = {'h','t','t','p',0};
3074 static WCHAR szHttps
[] = {'h','t','t','p','s',0};
3075 LPWININETHTTPSESSIONW lpwhs
= lpwhr
->lpHttpSession
;
3076 URL_COMPONENTSW urlComponents
;
3077 DWORD url_length
= 0;
3079 LPWSTR combined_url
;
3081 if (lpszUrl
[0]=='/') return WININET_strdupW( lpszUrl
);
3083 urlComponents
.dwStructSize
= sizeof(URL_COMPONENTSW
);
3084 urlComponents
.lpszScheme
= (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
) ? szHttps
: szHttp
;
3085 urlComponents
.dwSchemeLength
= 0;
3086 urlComponents
.lpszHostName
= lpwhs
->lpszHostName
;
3087 urlComponents
.dwHostNameLength
= 0;
3088 urlComponents
.nPort
= lpwhs
->nHostPort
;
3089 urlComponents
.lpszUserName
= lpwhs
->lpszUserName
;
3090 urlComponents
.dwUserNameLength
= 0;
3091 urlComponents
.lpszPassword
= NULL
;
3092 urlComponents
.dwPasswordLength
= 0;
3093 urlComponents
.lpszUrlPath
= lpwhr
->lpszPath
;
3094 urlComponents
.dwUrlPathLength
= 0;
3095 urlComponents
.lpszExtraInfo
= NULL
;
3096 urlComponents
.dwExtraInfoLength
= 0;
3098 if (!InternetCreateUrlW(&urlComponents
, 0, NULL
, &url_length
) &&
3099 (GetLastError() != ERROR_INSUFFICIENT_BUFFER
))
3102 orig_url
= HeapAlloc(GetProcessHeap(), 0, url_length
);
3104 /* convert from bytes to characters */
3105 url_length
= url_length
/ sizeof(WCHAR
) - 1;
3106 if (!InternetCreateUrlW(&urlComponents
, 0, orig_url
, &url_length
))
3108 HeapFree(GetProcessHeap(), 0, orig_url
);
3113 if (!InternetCombineUrlW(orig_url
, lpszUrl
, NULL
, &url_length
, ICU_ENCODE_SPACES_ONLY
) &&
3114 (GetLastError() != ERROR_INSUFFICIENT_BUFFER
))
3116 HeapFree(GetProcessHeap(), 0, orig_url
);
3119 combined_url
= HeapAlloc(GetProcessHeap(), 0, url_length
* sizeof(WCHAR
));
3121 if (!InternetCombineUrlW(orig_url
, lpszUrl
, combined_url
, &url_length
, ICU_ENCODE_SPACES_ONLY
))
3123 HeapFree(GetProcessHeap(), 0, orig_url
);
3124 HeapFree(GetProcessHeap(), 0, combined_url
);
3127 HeapFree(GetProcessHeap(), 0, orig_url
);
3128 return combined_url
;
3132 /***********************************************************************
3133 * HTTP_HandleRedirect (internal)
3135 static BOOL
HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszUrl
)
3137 LPWININETHTTPSESSIONW lpwhs
= lpwhr
->lpHttpSession
;
3138 LPWININETAPPINFOW hIC
= lpwhs
->lpAppInfo
;
3139 BOOL using_proxy
= hIC
->lpszProxy
&& hIC
->lpszProxy
[0];
3140 WCHAR path
[INTERNET_MAX_URL_LENGTH
];
3145 /* if it's an absolute path, keep the same session info */
3146 lstrcpynW(path
, lpszUrl
, INTERNET_MAX_URL_LENGTH
);
3150 URL_COMPONENTSW urlComponents
;
3151 WCHAR protocol
[32], hostName
[MAXHOSTNAME
], userName
[1024];
3152 static WCHAR szHttp
[] = {'h','t','t','p',0};
3153 static WCHAR szHttps
[] = {'h','t','t','p','s',0};
3159 urlComponents
.dwStructSize
= sizeof(URL_COMPONENTSW
);
3160 urlComponents
.lpszScheme
= protocol
;
3161 urlComponents
.dwSchemeLength
= 32;
3162 urlComponents
.lpszHostName
= hostName
;
3163 urlComponents
.dwHostNameLength
= MAXHOSTNAME
;
3164 urlComponents
.lpszUserName
= userName
;
3165 urlComponents
.dwUserNameLength
= 1024;
3166 urlComponents
.lpszPassword
= NULL
;
3167 urlComponents
.dwPasswordLength
= 0;
3168 urlComponents
.lpszUrlPath
= path
;
3169 urlComponents
.dwUrlPathLength
= 2048;
3170 urlComponents
.lpszExtraInfo
= NULL
;
3171 urlComponents
.dwExtraInfoLength
= 0;
3172 if(!InternetCrackUrlW(lpszUrl
, strlenW(lpszUrl
), 0, &urlComponents
))
3175 if (!strncmpW(szHttp
, urlComponents
.lpszScheme
, strlenW(szHttp
)) &&
3176 (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
))
3178 TRACE("redirect from secure page to non-secure page\n");
3179 /* FIXME: warn about from secure redirect to non-secure page */
3180 lpwhr
->hdr
.dwFlags
&= ~INTERNET_FLAG_SECURE
;
3182 if (!strncmpW(szHttps
, urlComponents
.lpszScheme
, strlenW(szHttps
)) &&
3183 !(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
))
3185 TRACE("redirect from non-secure page to secure page\n");
3186 /* FIXME: notify about redirect to secure page */
3187 lpwhr
->hdr
.dwFlags
|= INTERNET_FLAG_SECURE
;
3190 if (urlComponents
.nPort
== INTERNET_INVALID_PORT_NUMBER
)
3192 if (lstrlenW(protocol
)>4) /*https*/
3193 urlComponents
.nPort
= INTERNET_DEFAULT_HTTPS_PORT
;
3195 urlComponents
.nPort
= INTERNET_DEFAULT_HTTP_PORT
;
3200 * This upsets redirects to binary files on sourceforge.net
3201 * and gives an html page instead of the target file
3202 * Examination of the HTTP request sent by native wininet.dll
3203 * reveals that it doesn't send a referrer in that case.
3204 * Maybe there's a flag that enables this, or maybe a referrer
3205 * shouldn't be added in case of a redirect.
3208 /* consider the current host as the referrer */
3209 if (lpwhs
->lpszServerName
&& *lpwhs
->lpszServerName
)
3210 HTTP_ProcessHeader(lpwhr
, HTTP_REFERER
, lpwhs
->lpszServerName
,
3211 HTTP_ADDHDR_FLAG_REQ
|HTTP_ADDREQ_FLAG_REPLACE
|
3212 HTTP_ADDHDR_FLAG_ADD_IF_NEW
);
3215 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszHostName
);
3216 if (urlComponents
.nPort
!= INTERNET_DEFAULT_HTTP_PORT
&&
3217 urlComponents
.nPort
!= INTERNET_DEFAULT_HTTPS_PORT
)
3220 static const WCHAR fmt
[] = {'%','s',':','%','i',0};
3221 len
= lstrlenW(hostName
);
3222 len
+= 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
3223 lpwhs
->lpszHostName
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
3224 sprintfW(lpwhs
->lpszHostName
, fmt
, hostName
, urlComponents
.nPort
);
3227 lpwhs
->lpszHostName
= WININET_strdupW(hostName
);
3229 HTTP_ProcessHeader(lpwhr
, hostW
, lpwhs
->lpszHostName
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_REQ
);
3231 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszUserName
);
3232 lpwhs
->lpszUserName
= NULL
;
3234 lpwhs
->lpszUserName
= WININET_strdupW(userName
);
3238 if (strcmpiW(lpwhs
->lpszServerName
, hostName
) || lpwhs
->nServerPort
!= urlComponents
.nPort
)
3240 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszServerName
);
3241 lpwhs
->lpszServerName
= WININET_strdupW(hostName
);
3242 lpwhs
->nServerPort
= urlComponents
.nPort
;
3244 NETCON_close(&lpwhr
->netConnection
);
3245 if (!HTTP_ResolveName(lpwhr
)) return FALSE
;
3246 if (!NETCON_init(&lpwhr
->netConnection
, lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
)) return FALSE
;
3247 lpwhr
->read_pos
= lpwhr
->read_size
= 0;
3248 lpwhr
->read_chunked
= FALSE
;
3252 TRACE("Redirect through proxy\n");
3255 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszPath
);
3256 lpwhr
->lpszPath
=NULL
;
3262 rc
= UrlEscapeW(path
, NULL
, &needed
, URL_ESCAPE_SPACES_ONLY
);
3263 if (rc
!= E_POINTER
)
3264 needed
= strlenW(path
)+1;
3265 lpwhr
->lpszPath
= HeapAlloc(GetProcessHeap(), 0, needed
*sizeof(WCHAR
));
3266 rc
= UrlEscapeW(path
, lpwhr
->lpszPath
, &needed
,
3267 URL_ESCAPE_SPACES_ONLY
);
3270 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path
),rc
);
3271 strcpyW(lpwhr
->lpszPath
,path
);
3275 /* Remove custom content-type/length headers on redirects. */
3276 index
= HTTP_GetCustomHeaderIndex(lpwhr
, szContent_Type
, 0, TRUE
);
3278 HTTP_DeleteCustomHeader(lpwhr
, index
);
3279 index
= HTTP_GetCustomHeaderIndex(lpwhr
, szContent_Length
, 0, TRUE
);
3281 HTTP_DeleteCustomHeader(lpwhr
, index
);
3286 /***********************************************************************
3287 * HTTP_build_req (internal)
3289 * concatenate all the strings in the request together
3291 static LPWSTR
HTTP_build_req( LPCWSTR
*list
, int len
)
3296 for( t
= list
; *t
; t
++ )
3297 len
+= strlenW( *t
);
3300 str
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
3303 for( t
= list
; *t
; t
++ )
3309 static BOOL
HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr
)
3312 LPWSTR requestString
;
3318 static const WCHAR szConnect
[] = {'C','O','N','N','E','C','T',0};
3319 static const WCHAR szFormat
[] = {'%','s',':','%','d',0};
3320 LPWININETHTTPSESSIONW lpwhs
= lpwhr
->lpHttpSession
;
3324 lpszPath
= HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs
->lpszHostName
) + 13)*sizeof(WCHAR
) );
3325 sprintfW( lpszPath
, szFormat
, lpwhs
->lpszHostName
, lpwhs
->nHostPort
);
3326 requestString
= HTTP_BuildHeaderRequestString( lpwhr
, szConnect
, lpszPath
, g_szHttp1_1
);
3327 HeapFree( GetProcessHeap(), 0, lpszPath
);
3329 len
= WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
3330 NULL
, 0, NULL
, NULL
);
3331 len
--; /* the nul terminator isn't needed */
3332 ascii_req
= HeapAlloc( GetProcessHeap(), 0, len
);
3333 WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
3334 ascii_req
, len
, NULL
, NULL
);
3335 HeapFree( GetProcessHeap(), 0, requestString
);
3337 TRACE("full request -> %s\n", debugstr_an( ascii_req
, len
) );
3339 ret
= NETCON_send( &lpwhr
->netConnection
, ascii_req
, len
, 0, &cnt
);
3340 HeapFree( GetProcessHeap(), 0, ascii_req
);
3341 if (!ret
|| cnt
< 0)
3344 responseLen
= HTTP_GetResponseHeaders( lpwhr
, TRUE
);
3351 static void HTTP_InsertCookies(LPWININETHTTPREQW lpwhr
)
3353 static const WCHAR szUrlForm
[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
3354 LPWSTR lpszCookies
, lpszUrl
= NULL
;
3355 DWORD nCookieSize
, size
;
3356 LPHTTPHEADERW Host
= HTTP_GetHeader(lpwhr
, hostW
);
3358 size
= (strlenW(Host
->lpszValue
) + strlenW(szUrlForm
) + strlenW(lpwhr
->lpszPath
)) * sizeof(WCHAR
);
3359 if (!(lpszUrl
= HeapAlloc(GetProcessHeap(), 0, size
))) return;
3360 sprintfW( lpszUrl
, szUrlForm
, Host
->lpszValue
, lpwhr
->lpszPath
);
3362 if (InternetGetCookieW(lpszUrl
, NULL
, NULL
, &nCookieSize
))
3365 static const WCHAR szCookie
[] = {'C','o','o','k','i','e',':',' ',0};
3367 size
= sizeof(szCookie
) + nCookieSize
* sizeof(WCHAR
) + sizeof(szCrLf
);
3368 if ((lpszCookies
= HeapAlloc(GetProcessHeap(), 0, size
)))
3370 cnt
+= sprintfW(lpszCookies
, szCookie
);
3371 InternetGetCookieW(lpszUrl
, NULL
, lpszCookies
+ cnt
, &nCookieSize
);
3372 strcatW(lpszCookies
, szCrLf
);
3374 HTTP_HttpAddRequestHeadersW(lpwhr
, lpszCookies
, strlenW(lpszCookies
), HTTP_ADDREQ_FLAG_REPLACE
);
3375 HeapFree(GetProcessHeap(), 0, lpszCookies
);
3378 HeapFree(GetProcessHeap(), 0, lpszUrl
);
3381 /***********************************************************************
3382 * HTTP_HttpSendRequestW (internal)
3384 * Sends the specified request to the HTTP server
3391 BOOL WINAPI
HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszHeaders
,
3392 DWORD dwHeaderLength
, LPVOID lpOptional
, DWORD dwOptionalLength
,
3393 DWORD dwContentLength
, BOOL bEndRequest
)
3396 BOOL bSuccess
= FALSE
, redirected
= FALSE
;
3397 LPWSTR requestString
= NULL
;
3400 INTERNET_ASYNC_RESULT iar
;
3401 static const WCHAR szPost
[] = { 'P','O','S','T',0 };
3402 static const WCHAR szContentLength
[] =
3403 { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0 };
3404 WCHAR contentLengthStr
[sizeof szContentLength
/2 /* includes \r\n */ + 20 /* int */ ];
3406 TRACE("--> %p\n", lpwhr
);
3408 assert(lpwhr
->hdr
.htype
== WH_HHTTPREQ
);
3410 /* if the verb is NULL default to GET */
3411 if (!lpwhr
->lpszVerb
)
3412 lpwhr
->lpszVerb
= WININET_strdupW(szGET
);
3414 if (dwContentLength
|| strcmpW(lpwhr
->lpszVerb
, szGET
))
3416 sprintfW(contentLengthStr
, szContentLength
, dwContentLength
);
3417 HTTP_HttpAddRequestHeadersW(lpwhr
, contentLengthStr
, -1L, HTTP_ADDREQ_FLAG_REPLACE
);
3418 lpwhr
->dwBytesToWrite
= dwContentLength
;
3420 if (lpwhr
->lpHttpSession
->lpAppInfo
->lpszAgent
)
3422 WCHAR
*agent_header
;
3423 static const WCHAR user_agent
[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0};
3426 len
= strlenW(lpwhr
->lpHttpSession
->lpAppInfo
->lpszAgent
) + strlenW(user_agent
);
3427 agent_header
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
3428 sprintfW(agent_header
, user_agent
, lpwhr
->lpHttpSession
->lpAppInfo
->lpszAgent
);
3430 HTTP_HttpAddRequestHeadersW(lpwhr
, agent_header
, strlenW(agent_header
), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
3431 HeapFree(GetProcessHeap(), 0, agent_header
);
3433 if (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_PRAGMA_NOCACHE
)
3435 static const WCHAR pragma_nocache
[] = {'P','r','a','g','m','a',':',' ','n','o','-','c','a','c','h','e','\r','\n',0};
3436 HTTP_HttpAddRequestHeadersW(lpwhr
, pragma_nocache
, strlenW(pragma_nocache
), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
3438 if ((lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_CACHE_WRITE
) && !strcmpW(lpwhr
->lpszVerb
, szPost
))
3440 static const WCHAR cache_control
[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l',':',
3441 ' ','n','o','-','c','a','c','h','e','\r','\n',0};
3442 HTTP_HttpAddRequestHeadersW(lpwhr
, cache_control
, strlenW(cache_control
), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
3452 /* like native, just in case the caller forgot to call InternetReadFile
3453 * for all the data */
3454 HTTP_DrainContent(lpwhr
);
3455 lpwhr
->dwContentRead
= 0;
3457 if (TRACE_ON(wininet
))
3459 LPHTTPHEADERW Host
= HTTP_GetHeader(lpwhr
, hostW
);
3460 TRACE("Going to url %s %s\n", debugstr_w(Host
->lpszValue
), debugstr_w(lpwhr
->lpszPath
));
3464 if (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_KEEP_CONNECTION
)
3466 HTTP_ProcessHeader(lpwhr
, szConnection
, szKeepAlive
, HTTP_ADDHDR_FLAG_REQ
| HTTP_ADDHDR_FLAG_REPLACE
);
3468 HTTP_InsertAuthorization(lpwhr
, lpwhr
->pAuthInfo
, szAuthorization
);
3469 HTTP_InsertAuthorization(lpwhr
, lpwhr
->pProxyAuthInfo
, szProxy_Authorization
);
3471 if (!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_COOKIES
))
3472 HTTP_InsertCookies(lpwhr
);
3474 /* add the headers the caller supplied */
3475 if( lpszHeaders
&& dwHeaderLength
)
3477 HTTP_HttpAddRequestHeadersW(lpwhr
, lpszHeaders
, dwHeaderLength
,
3478 HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REPLACE
);
3481 if (lpwhr
->lpHttpSession
->lpAppInfo
->lpszProxy
&& lpwhr
->lpHttpSession
->lpAppInfo
->lpszProxy
[0])
3483 WCHAR
*url
= HTTP_BuildProxyRequestUrl(lpwhr
);
3484 requestString
= HTTP_BuildHeaderRequestString(lpwhr
, lpwhr
->lpszVerb
, url
, lpwhr
->lpszVersion
);
3485 HeapFree(GetProcessHeap(), 0, url
);
3488 requestString
= HTTP_BuildHeaderRequestString(lpwhr
, lpwhr
->lpszVerb
, lpwhr
->lpszPath
, lpwhr
->lpszVersion
);
3491 TRACE("Request header -> %s\n", debugstr_w(requestString
) );
3493 /* Send the request and store the results */
3494 if (!HTTP_OpenConnection(lpwhr
))
3497 /* send the request as ASCII, tack on the optional data */
3498 if (!lpOptional
|| redirected
)
3499 dwOptionalLength
= 0;
3500 len
= WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
3501 NULL
, 0, NULL
, NULL
);
3502 ascii_req
= HeapAlloc( GetProcessHeap(), 0, len
+ dwOptionalLength
);
3503 WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
3504 ascii_req
, len
, NULL
, NULL
);
3506 memcpy( &ascii_req
[len
-1], lpOptional
, dwOptionalLength
);
3507 len
= (len
+ dwOptionalLength
- 1);
3509 TRACE("full request -> %s\n", debugstr_a(ascii_req
) );
3511 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3512 INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
3514 NETCON_send(&lpwhr
->netConnection
, ascii_req
, len
, 0, &cnt
);
3515 HeapFree( GetProcessHeap(), 0, ascii_req
);
3517 lpwhr
->dwBytesWritten
= dwOptionalLength
;
3519 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3520 INTERNET_STATUS_REQUEST_SENT
,
3521 &len
, sizeof(DWORD
));
3528 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3529 INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
3534 responseLen
= HTTP_GetResponseHeaders(lpwhr
, TRUE
);
3538 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3539 INTERNET_STATUS_RESPONSE_RECEIVED
, &responseLen
,
3542 HTTP_ProcessCookies(lpwhr
);
3544 if (!set_content_length( lpwhr
)) HTTP_FinishedReading(lpwhr
);
3546 dwBufferSize
= sizeof(dwStatusCode
);
3547 if (!HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_STATUS_CODE
,
3548 &dwStatusCode
,&dwBufferSize
,NULL
))
3551 if (!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTO_REDIRECT
) && bSuccess
)
3553 WCHAR
*new_url
, szNewLocation
[INTERNET_MAX_URL_LENGTH
];
3554 dwBufferSize
=sizeof(szNewLocation
);
3555 if ((dwStatusCode
==HTTP_STATUS_REDIRECT
|| dwStatusCode
==HTTP_STATUS_MOVED
) &&
3556 HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_LOCATION
,szNewLocation
,&dwBufferSize
,NULL
))
3558 /* redirects are always GETs */
3559 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszVerb
);
3560 lpwhr
->lpszVerb
= WININET_strdupW(szGET
);
3562 HTTP_DrainContent(lpwhr
);
3563 if ((new_url
= HTTP_GetRedirectURL( lpwhr
, szNewLocation
)))
3565 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
, INTERNET_STATUS_REDIRECT
,
3566 new_url
, (strlenW(new_url
) + 1) * sizeof(WCHAR
));
3567 bSuccess
= HTTP_HandleRedirect(lpwhr
, new_url
);
3570 HeapFree(GetProcessHeap(), 0, requestString
);
3573 HeapFree( GetProcessHeap(), 0, new_url
);
3578 if (!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTH
) && bSuccess
)
3580 WCHAR szAuthValue
[2048];
3582 if (dwStatusCode
== HTTP_STATUS_DENIED
)
3585 while (HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_WWW_AUTHENTICATE
,szAuthValue
,&dwBufferSize
,&dwIndex
))
3587 if (HTTP_DoAuthorization(lpwhr
, szAuthValue
,
3589 lpwhr
->lpHttpSession
->lpszUserName
,
3590 lpwhr
->lpHttpSession
->lpszPassword
))
3597 if (dwStatusCode
== HTTP_STATUS_PROXY_AUTH_REQ
)
3600 while (HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_PROXY_AUTHENTICATE
,szAuthValue
,&dwBufferSize
,&dwIndex
))
3602 if (HTTP_DoAuthorization(lpwhr
, szAuthValue
,
3603 &lpwhr
->pProxyAuthInfo
,
3604 lpwhr
->lpHttpSession
->lpAppInfo
->lpszProxyUsername
,
3605 lpwhr
->lpHttpSession
->lpAppInfo
->lpszProxyPassword
))
3619 /* FIXME: Better check, when we have to create the cache file */
3620 if(bSuccess
&& (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NEED_FILE
)) {
3621 WCHAR url
[INTERNET_MAX_URL_LENGTH
];
3622 WCHAR cacheFileName
[MAX_PATH
+1];
3625 b
= HTTP_GetRequestURL(lpwhr
, url
);
3627 WARN("Could not get URL\n");
3631 b
= CreateUrlCacheEntryW(url
, lpwhr
->dwContentLength
> 0 ? lpwhr
->dwContentLength
: 0, NULL
, cacheFileName
, 0);
3633 lpwhr
->lpszCacheFile
= WININET_strdupW(cacheFileName
);
3634 lpwhr
->hCacheFile
= CreateFileW(lpwhr
->lpszCacheFile
, GENERIC_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
3635 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3636 if(lpwhr
->hCacheFile
== INVALID_HANDLE_VALUE
) {
3637 WARN("Could not create file: %u\n", GetLastError());
3638 lpwhr
->hCacheFile
= NULL
;
3641 WARN("Could not create cache entry: %08x\n", GetLastError());
3647 HeapFree(GetProcessHeap(), 0, requestString
);
3649 /* TODO: send notification for P3P header */
3651 if (lpwhr
->lpHttpSession
->lpAppInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
3655 if (lpwhr
->dwBytesWritten
== lpwhr
->dwBytesToWrite
) HTTP_ReceiveRequestData(lpwhr
, TRUE
);
3658 iar
.dwResult
= (DWORD_PTR
)lpwhr
->hdr
.hInternet
;
3661 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3662 INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
3663 sizeof(INTERNET_ASYNC_RESULT
));
3668 iar
.dwResult
= (DWORD_PTR
)lpwhr
->hdr
.hInternet
;
3669 iar
.dwError
= INTERNET_GetLastError();
3671 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3672 INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
3673 sizeof(INTERNET_ASYNC_RESULT
));
3678 if (bSuccess
) INTERNET_SetLastError(ERROR_SUCCESS
);
3682 /***********************************************************************
3683 * HTTPSESSION_Destroy (internal)
3685 * Deallocate session handle
3688 static void HTTPSESSION_Destroy(WININETHANDLEHEADER
*hdr
)
3690 LPWININETHTTPSESSIONW lpwhs
= (LPWININETHTTPSESSIONW
) hdr
;
3692 TRACE("%p\n", lpwhs
);
3694 WININET_Release(&lpwhs
->lpAppInfo
->hdr
);
3696 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszHostName
);
3697 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszServerName
);
3698 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszPassword
);
3699 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszUserName
);
3700 HeapFree(GetProcessHeap(), 0, lpwhs
);
3703 static DWORD
HTTPSESSION_QueryOption(WININETHANDLEHEADER
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
3706 case INTERNET_OPTION_HANDLE_TYPE
:
3707 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
3709 if (*size
< sizeof(ULONG
))
3710 return ERROR_INSUFFICIENT_BUFFER
;
3712 *size
= sizeof(DWORD
);
3713 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_CONNECT_HTTP
;
3714 return ERROR_SUCCESS
;
3717 return INET_QueryOption(option
, buffer
, size
, unicode
);
3720 static DWORD
HTTPSESSION_SetOption(WININETHANDLEHEADER
*hdr
, DWORD option
, void *buffer
, DWORD size
)
3722 WININETHTTPSESSIONW
*ses
= (WININETHTTPSESSIONW
*)hdr
;
3725 case INTERNET_OPTION_USERNAME
:
3727 HeapFree(GetProcessHeap(), 0, ses
->lpszUserName
);
3728 if (!(ses
->lpszUserName
= WININET_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
3729 return ERROR_SUCCESS
;
3731 case INTERNET_OPTION_PASSWORD
:
3733 HeapFree(GetProcessHeap(), 0, ses
->lpszPassword
);
3734 if (!(ses
->lpszPassword
= WININET_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
3735 return ERROR_SUCCESS
;
3740 return ERROR_INTERNET_INVALID_OPTION
;
3743 static const HANDLEHEADERVtbl HTTPSESSIONVtbl
= {
3744 HTTPSESSION_Destroy
,
3746 HTTPSESSION_QueryOption
,
3747 HTTPSESSION_SetOption
,
3756 /***********************************************************************
3757 * HTTP_Connect (internal)
3759 * Create http session handle
3762 * HINTERNET a session handle on success
3766 HINTERNET
HTTP_Connect(LPWININETAPPINFOW hIC
, LPCWSTR lpszServerName
,
3767 INTERNET_PORT nServerPort
, LPCWSTR lpszUserName
,
3768 LPCWSTR lpszPassword
, DWORD dwFlags
, DWORD_PTR dwContext
,
3769 DWORD dwInternalFlags
)
3771 LPWININETHTTPSESSIONW lpwhs
= NULL
;
3772 HINTERNET handle
= NULL
;
3776 if (!lpszServerName
|| !lpszServerName
[0])
3778 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
3782 assert( hIC
->hdr
.htype
== WH_HINIT
);
3784 lpwhs
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WININETHTTPSESSIONW
));
3787 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
3792 * According to my tests. The name is not resolved until a request is sent
3795 lpwhs
->hdr
.htype
= WH_HHTTPSESSION
;
3796 lpwhs
->hdr
.vtbl
= &HTTPSESSIONVtbl
;
3797 lpwhs
->hdr
.dwFlags
= dwFlags
;
3798 lpwhs
->hdr
.dwContext
= dwContext
;
3799 lpwhs
->hdr
.dwInternalFlags
= dwInternalFlags
| (hIC
->hdr
.dwInternalFlags
& INET_CALLBACKW
);
3800 lpwhs
->hdr
.refs
= 1;
3801 lpwhs
->hdr
.lpfnStatusCB
= hIC
->hdr
.lpfnStatusCB
;
3803 WININET_AddRef( &hIC
->hdr
);
3804 lpwhs
->lpAppInfo
= hIC
;
3805 list_add_head( &hIC
->hdr
.children
, &lpwhs
->hdr
.entry
);
3807 handle
= WININET_AllocHandle( &lpwhs
->hdr
);
3810 ERR("Failed to alloc handle\n");
3811 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
3815 if(hIC
->lpszProxy
&& hIC
->dwAccessType
== INTERNET_OPEN_TYPE_PROXY
) {
3816 if(strchrW(hIC
->lpszProxy
, ' '))
3817 FIXME("Several proxies not implemented.\n");
3818 if(hIC
->lpszProxyBypass
)
3819 FIXME("Proxy bypass is ignored.\n");
3821 if (lpszServerName
&& lpszServerName
[0])
3823 lpwhs
->lpszServerName
= WININET_strdupW(lpszServerName
);
3824 lpwhs
->lpszHostName
= WININET_strdupW(lpszServerName
);
3826 if (lpszUserName
&& lpszUserName
[0])
3827 lpwhs
->lpszUserName
= WININET_strdupW(lpszUserName
);
3828 if (lpszPassword
&& lpszPassword
[0])
3829 lpwhs
->lpszPassword
= WININET_strdupW(lpszPassword
);
3830 lpwhs
->nServerPort
= nServerPort
;
3831 lpwhs
->nHostPort
= nServerPort
;
3833 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
3834 if (!(lpwhs
->hdr
.dwInternalFlags
& INET_OPENURL
))
3836 INTERNET_SendCallback(&hIC
->hdr
, dwContext
,
3837 INTERNET_STATUS_HANDLE_CREATED
, &handle
,
3843 WININET_Release( &lpwhs
->hdr
);
3846 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
3850 TRACE("%p --> %p (%p)\n", hIC
, handle
, lpwhs
);
3855 /***********************************************************************
3856 * HTTP_OpenConnection (internal)
3858 * Connect to a web server
3865 static BOOL
HTTP_OpenConnection(LPWININETHTTPREQW lpwhr
)
3867 BOOL bSuccess
= FALSE
;
3868 LPWININETHTTPSESSIONW lpwhs
;
3869 LPWININETAPPINFOW hIC
= NULL
;
3875 if (lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
3877 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
3881 if (NETCON_connected(&lpwhr
->netConnection
))
3886 if (!HTTP_ResolveName(lpwhr
)) goto lend
;
3888 lpwhs
= lpwhr
->lpHttpSession
;
3890 hIC
= lpwhs
->lpAppInfo
;
3891 inet_ntop(lpwhs
->socketAddress
.sin_family
, &lpwhs
->socketAddress
.sin_addr
,
3892 szaddr
, sizeof(szaddr
));
3893 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3894 INTERNET_STATUS_CONNECTING_TO_SERVER
,
3898 if (!NETCON_create(&lpwhr
->netConnection
, lpwhs
->socketAddress
.sin_family
,
3901 WARN("Socket creation failed: %u\n", INTERNET_GetLastError());
3905 if (!NETCON_connect(&lpwhr
->netConnection
, (struct sockaddr
*)&lpwhs
->socketAddress
,
3906 sizeof(lpwhs
->socketAddress
)))
3909 if (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
)
3911 /* Note: we differ from Microsoft's WinINet here. they seem to have
3912 * a bug that causes no status callbacks to be sent when starting
3913 * a tunnel to a proxy server using the CONNECT verb. i believe our
3914 * behaviour to be more correct and to not cause any incompatibilities
3915 * because using a secure connection through a proxy server is a rare
3916 * case that would be hard for anyone to depend on */
3917 if (hIC
->lpszProxy
&& !HTTP_SecureProxyConnect(lpwhr
))
3920 if (!NETCON_secure_connect(&lpwhr
->netConnection
, lpwhs
->lpszHostName
))
3922 WARN("Couldn't connect securely to host\n");
3927 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3928 INTERNET_STATUS_CONNECTED_TO_SERVER
,
3929 szaddr
, strlen(szaddr
)+1);
3934 lpwhr
->read_pos
= lpwhr
->read_size
= 0;
3935 lpwhr
->read_chunked
= FALSE
;
3937 TRACE("%d <--\n", bSuccess
);
3942 /***********************************************************************
3943 * HTTP_clear_response_headers (internal)
3945 * clear out any old response headers
3947 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr
)
3951 for( i
=0; i
<lpwhr
->nCustHeaders
; i
++)
3953 if( !lpwhr
->pCustHeaders
[i
].lpszField
)
3955 if( !lpwhr
->pCustHeaders
[i
].lpszValue
)
3957 if ( lpwhr
->pCustHeaders
[i
].wFlags
& HDR_ISREQUEST
)
3959 HTTP_DeleteCustomHeader( lpwhr
, i
);
3964 /***********************************************************************
3965 * HTTP_GetResponseHeaders (internal)
3967 * Read server response
3974 static INT
HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr
, BOOL clear
)
3977 WCHAR buffer
[MAX_REPLY_LEN
];
3978 DWORD buflen
= MAX_REPLY_LEN
;
3979 BOOL bSuccess
= FALSE
;
3981 static const WCHAR szHundred
[] = {'1','0','0',0};
3982 char bufferA
[MAX_REPLY_LEN
];
3983 LPWSTR status_code
, status_text
;
3984 DWORD cchMaxRawHeaders
= 1024;
3985 LPWSTR lpszRawHeaders
= HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders
+1)*sizeof(WCHAR
));
3986 DWORD cchRawHeaders
= 0;
3990 /* clear old response headers (eg. from a redirect response) */
3991 if (clear
) HTTP_clear_response_headers( lpwhr
);
3993 if (!NETCON_connected(&lpwhr
->netConnection
))
3998 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
4000 buflen
= MAX_REPLY_LEN
;
4001 if (!read_line(lpwhr
, bufferA
, &buflen
))
4004 MultiByteToWideChar( CP_ACP
, 0, bufferA
, buflen
, buffer
, MAX_REPLY_LEN
);
4006 /* split the version from the status code */
4007 status_code
= strchrW( buffer
, ' ' );
4012 /* split the status code from the status text */
4013 status_text
= strchrW( status_code
, ' ' );
4018 TRACE("version [%s] status code [%s] status text [%s]\n",
4019 debugstr_w(buffer
), debugstr_w(status_code
), debugstr_w(status_text
) );
4021 } while (!strcmpW(status_code
, szHundred
)); /* ignore "100 Continue" responses */
4023 /* Add status code */
4024 HTTP_ProcessHeader(lpwhr
, szStatus
, status_code
,
4025 HTTP_ADDHDR_FLAG_REPLACE
);
4027 HeapFree(GetProcessHeap(),0,lpwhr
->lpszVersion
);
4028 HeapFree(GetProcessHeap(),0,lpwhr
->lpszStatusText
);
4030 lpwhr
->lpszVersion
= WININET_strdupW(buffer
);
4031 lpwhr
->lpszStatusText
= WININET_strdupW(status_text
);
4033 /* Restore the spaces */
4034 *(status_code
-1) = ' ';
4035 *(status_text
-1) = ' ';
4037 /* regenerate raw headers */
4038 while (cchRawHeaders
+ buflen
+ strlenW(szCrLf
) > cchMaxRawHeaders
)
4040 cchMaxRawHeaders
*= 2;
4041 lpszRawHeaders
= HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders
, (cchMaxRawHeaders
+1)*sizeof(WCHAR
));
4043 memcpy(lpszRawHeaders
+cchRawHeaders
, buffer
, (buflen
-1)*sizeof(WCHAR
));
4044 cchRawHeaders
+= (buflen
-1);
4045 memcpy(lpszRawHeaders
+cchRawHeaders
, szCrLf
, sizeof(szCrLf
));
4046 cchRawHeaders
+= sizeof(szCrLf
)/sizeof(szCrLf
[0])-1;
4047 lpszRawHeaders
[cchRawHeaders
] = '\0';
4049 /* Parse each response line */
4052 buflen
= MAX_REPLY_LEN
;
4053 if (read_line(lpwhr
, bufferA
, &buflen
))
4055 LPWSTR
* pFieldAndValue
;
4057 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA
));
4059 if (!bufferA
[0]) break;
4060 if (!strchr(bufferA
, ':'))
4062 WARN("invalid header\n");
4065 MultiByteToWideChar( CP_ACP
, 0, bufferA
, buflen
, buffer
, MAX_REPLY_LEN
);
4067 while (cchRawHeaders
+ buflen
+ strlenW(szCrLf
) > cchMaxRawHeaders
)
4069 cchMaxRawHeaders
*= 2;
4070 lpszRawHeaders
= HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders
, (cchMaxRawHeaders
+1)*sizeof(WCHAR
));
4072 memcpy(lpszRawHeaders
+cchRawHeaders
, buffer
, (buflen
-1)*sizeof(WCHAR
));
4073 cchRawHeaders
+= (buflen
-1);
4074 memcpy(lpszRawHeaders
+cchRawHeaders
, szCrLf
, sizeof(szCrLf
));
4075 cchRawHeaders
+= sizeof(szCrLf
)/sizeof(szCrLf
[0])-1;
4076 lpszRawHeaders
[cchRawHeaders
] = '\0';
4078 pFieldAndValue
= HTTP_InterpretHttpHeader(buffer
);
4079 if (!pFieldAndValue
)
4082 HTTP_ProcessHeader(lpwhr
, pFieldAndValue
[0], pFieldAndValue
[1],
4083 HTTP_ADDREQ_FLAG_ADD
);
4085 HTTP_FreeTokens(pFieldAndValue
);
4095 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszRawHeaders
);
4096 lpwhr
->lpszRawHeaders
= lpszRawHeaders
;
4097 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders
));
4107 HeapFree(GetProcessHeap(), 0, lpszRawHeaders
);
4113 static void strip_spaces(LPWSTR start
)
4118 while (*str
== ' ' && *str
!= '\0')
4122 memmove(start
, str
, sizeof(WCHAR
) * (strlenW(str
) + 1));
4124 end
= start
+ strlenW(start
) - 1;
4125 while (end
>= start
&& *end
== ' ')
4133 /***********************************************************************
4134 * HTTP_InterpretHttpHeader (internal)
4136 * Parse server response
4140 * Pointer to array of field, value, NULL on success.
4143 static LPWSTR
* HTTP_InterpretHttpHeader(LPCWSTR buffer
)
4145 LPWSTR
* pTokenPair
;
4149 pTokenPair
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*pTokenPair
)*3);
4151 pszColon
= strchrW(buffer
, ':');
4152 /* must have two tokens */
4155 HTTP_FreeTokens(pTokenPair
);
4157 TRACE("No ':' in line: %s\n", debugstr_w(buffer
));
4161 pTokenPair
[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon
- buffer
+ 1) * sizeof(WCHAR
));
4164 HTTP_FreeTokens(pTokenPair
);
4167 memcpy(pTokenPair
[0], buffer
, (pszColon
- buffer
) * sizeof(WCHAR
));
4168 pTokenPair
[0][pszColon
- buffer
] = '\0';
4172 len
= strlenW(pszColon
);
4173 pTokenPair
[1] = HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
4176 HTTP_FreeTokens(pTokenPair
);
4179 memcpy(pTokenPair
[1], pszColon
, (len
+ 1) * sizeof(WCHAR
));
4181 strip_spaces(pTokenPair
[0]);
4182 strip_spaces(pTokenPair
[1]);
4184 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair
[0]), debugstr_w(pTokenPair
[1]));
4188 /***********************************************************************
4189 * HTTP_ProcessHeader (internal)
4191 * Stuff header into header tables according to <dwModifier>
4195 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
4197 static BOOL
HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr
, LPCWSTR field
, LPCWSTR value
, DWORD dwModifier
)
4199 LPHTTPHEADERW lphttpHdr
= NULL
;
4200 BOOL bSuccess
= FALSE
;
4202 BOOL request_only
= dwModifier
& HTTP_ADDHDR_FLAG_REQ
;
4204 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field
), debugstr_w(value
), dwModifier
);
4206 /* REPLACE wins out over ADD */
4207 if (dwModifier
& HTTP_ADDHDR_FLAG_REPLACE
)
4208 dwModifier
&= ~HTTP_ADDHDR_FLAG_ADD
;
4210 if (dwModifier
& HTTP_ADDHDR_FLAG_ADD
)
4213 index
= HTTP_GetCustomHeaderIndex(lpwhr
, field
, 0, request_only
);
4217 if (dwModifier
& HTTP_ADDHDR_FLAG_ADD_IF_NEW
)
4221 lphttpHdr
= &lpwhr
->pCustHeaders
[index
];
4227 hdr
.lpszField
= (LPWSTR
)field
;
4228 hdr
.lpszValue
= (LPWSTR
)value
;
4229 hdr
.wFlags
= hdr
.wCount
= 0;
4231 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
4232 hdr
.wFlags
|= HDR_ISREQUEST
;
4234 return HTTP_InsertCustomHeader(lpwhr
, &hdr
);
4236 /* no value to delete */
4239 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
4240 lphttpHdr
->wFlags
|= HDR_ISREQUEST
;
4242 lphttpHdr
->wFlags
&= ~HDR_ISREQUEST
;
4244 if (dwModifier
& HTTP_ADDHDR_FLAG_REPLACE
)
4246 HTTP_DeleteCustomHeader( lpwhr
, index
);
4252 hdr
.lpszField
= (LPWSTR
)field
;
4253 hdr
.lpszValue
= (LPWSTR
)value
;
4254 hdr
.wFlags
= hdr
.wCount
= 0;
4256 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
4257 hdr
.wFlags
|= HDR_ISREQUEST
;
4259 return HTTP_InsertCustomHeader(lpwhr
, &hdr
);
4264 else if (dwModifier
& COALESCEFLAGS
)
4269 INT origlen
= strlenW(lphttpHdr
->lpszValue
);
4270 INT valuelen
= strlenW(value
);
4272 if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
)
4275 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
4277 else if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON
)
4280 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
4283 len
= origlen
+ valuelen
+ ((ch
> 0) ? 2 : 0);
4285 lpsztmp
= HeapReAlloc(GetProcessHeap(), 0, lphttpHdr
->lpszValue
, (len
+1)*sizeof(WCHAR
));
4288 lphttpHdr
->lpszValue
= lpsztmp
;
4289 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
4292 lphttpHdr
->lpszValue
[origlen
] = ch
;
4294 lphttpHdr
->lpszValue
[origlen
] = ' ';
4298 memcpy(&lphttpHdr
->lpszValue
[origlen
], value
, valuelen
*sizeof(WCHAR
));
4299 lphttpHdr
->lpszValue
[len
] = '\0';
4304 WARN("HeapReAlloc (%d bytes) failed\n",len
+1);
4305 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
4308 TRACE("<-- %d\n",bSuccess
);
4313 /***********************************************************************
4314 * HTTP_FinishedReading (internal)
4316 * Called when all content from server has been read by client.
4319 static BOOL
HTTP_FinishedReading(LPWININETHTTPREQW lpwhr
)
4321 WCHAR szVersion
[10];
4322 WCHAR szConnectionResponse
[20];
4323 DWORD dwBufferSize
= sizeof(szVersion
);
4324 BOOL keepalive
= FALSE
;
4328 /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
4329 * the connection is keep-alive by default */
4330 if (HTTP_HttpQueryInfoW(lpwhr
, HTTP_QUERY_VERSION
, szVersion
,
4331 &dwBufferSize
, NULL
) &&
4332 !strcmpiW(szVersion
, g_szHttp1_1
))
4337 dwBufferSize
= sizeof(szConnectionResponse
);
4338 if (HTTP_HttpQueryInfoW(lpwhr
, HTTP_QUERY_PROXY_CONNECTION
, szConnectionResponse
, &dwBufferSize
, NULL
) ||
4339 HTTP_HttpQueryInfoW(lpwhr
, HTTP_QUERY_CONNECTION
, szConnectionResponse
, &dwBufferSize
, NULL
))
4341 keepalive
= !strcmpiW(szConnectionResponse
, szKeepAlive
);
4346 HTTPREQ_CloseConnection(&lpwhr
->hdr
);
4349 /* FIXME: store data in the URL cache here */
4355 /***********************************************************************
4356 * HTTP_GetCustomHeaderIndex (internal)
4358 * Return index of custom header from header array
4361 static INT
HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszField
,
4362 int requested_index
, BOOL request_only
)
4366 TRACE("%s\n", debugstr_w(lpszField
));
4368 for (index
= 0; index
< lpwhr
->nCustHeaders
; index
++)
4370 if (strcmpiW(lpwhr
->pCustHeaders
[index
].lpszField
, lpszField
))
4373 if (request_only
&& !(lpwhr
->pCustHeaders
[index
].wFlags
& HDR_ISREQUEST
))
4376 if (!request_only
&& (lpwhr
->pCustHeaders
[index
].wFlags
& HDR_ISREQUEST
))
4379 if (requested_index
== 0)
4384 if (index
>= lpwhr
->nCustHeaders
)
4387 TRACE("Return: %d\n", index
);
4392 /***********************************************************************
4393 * HTTP_InsertCustomHeader (internal)
4395 * Insert header into array
4398 static BOOL
HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr
, LPHTTPHEADERW lpHdr
)
4401 LPHTTPHEADERW lph
= NULL
;
4404 TRACE("--> %s: %s\n", debugstr_w(lpHdr
->lpszField
), debugstr_w(lpHdr
->lpszValue
));
4405 count
= lpwhr
->nCustHeaders
+ 1;
4407 lph
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, lpwhr
->pCustHeaders
, sizeof(HTTPHEADERW
) * count
);
4409 lph
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(HTTPHEADERW
) * count
);
4413 lpwhr
->pCustHeaders
= lph
;
4414 lpwhr
->pCustHeaders
[count
-1].lpszField
= WININET_strdupW(lpHdr
->lpszField
);
4415 lpwhr
->pCustHeaders
[count
-1].lpszValue
= WININET_strdupW(lpHdr
->lpszValue
);
4416 lpwhr
->pCustHeaders
[count
-1].wFlags
= lpHdr
->wFlags
;
4417 lpwhr
->pCustHeaders
[count
-1].wCount
= lpHdr
->wCount
;
4418 lpwhr
->nCustHeaders
++;
4423 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
4430 /***********************************************************************
4431 * HTTP_DeleteCustomHeader (internal)
4433 * Delete header from array
4434 * If this function is called, the indexs may change.
4436 static BOOL
HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr
, DWORD index
)
4438 if( lpwhr
->nCustHeaders
<= 0 )
4440 if( index
>= lpwhr
->nCustHeaders
)
4442 lpwhr
->nCustHeaders
--;
4444 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
[index
].lpszField
);
4445 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
[index
].lpszValue
);
4447 memmove( &lpwhr
->pCustHeaders
[index
], &lpwhr
->pCustHeaders
[index
+1],
4448 (lpwhr
->nCustHeaders
- index
)* sizeof(HTTPHEADERW
) );
4449 memset( &lpwhr
->pCustHeaders
[lpwhr
->nCustHeaders
], 0, sizeof(HTTPHEADERW
) );
4455 /***********************************************************************
4456 * HTTP_VerifyValidHeader (internal)
4458 * Verify the given header is not invalid for the given http request
4461 static BOOL
HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr
, LPCWSTR field
)
4463 /* Accept-Encoding is stripped from HTTP/1.0 requests. It is invalid */
4464 if (!strcmpW(lpwhr
->lpszVersion
, g_szHttp1_0
) && !strcmpiW(field
, szAccept_Encoding
))
4470 /***********************************************************************
4471 * IsHostInProxyBypassList (@)
4476 BOOL WINAPI
IsHostInProxyBypassList(DWORD flags
, LPCSTR szHost
, DWORD length
)
4478 FIXME("STUB: flags=%d host=%s length=%d\n",flags
,szHost
,length
);