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 #include "inet_ntop.c"
71 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
73 static const WCHAR g_szHttp1_0
[] = {'H','T','T','P','/','1','.','0',0};
74 static const WCHAR g_szHttp1_1
[] = {'H','T','T','P','/','1','.','1',0};
75 static const WCHAR g_szReferer
[] = {'R','e','f','e','r','e','r',0};
76 static const WCHAR g_szAccept
[] = {'A','c','c','e','p','t',0};
77 static const WCHAR g_szUserAgent
[] = {'U','s','e','r','-','A','g','e','n','t',0};
78 static const WCHAR szHost
[] = { 'H','o','s','t',0 };
79 static const WCHAR szAuthorization
[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
80 static const WCHAR szProxy_Authorization
[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
81 static const WCHAR szStatus
[] = { 'S','t','a','t','u','s',0 };
82 static const WCHAR szKeepAlive
[] = {'K','e','e','p','-','A','l','i','v','e',0};
83 static const WCHAR szGET
[] = { 'G','E','T', 0 };
84 static const WCHAR szCrLf
[] = {'\r','\n', 0};
86 #define MAXHOSTNAME 100
87 #define MAX_FIELD_VALUE_LEN 256
88 #define MAX_FIELD_LEN 256
90 #define HTTP_REFERER g_szReferer
91 #define HTTP_ACCEPT g_szAccept
92 #define HTTP_USERAGENT g_szUserAgent
94 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
95 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
96 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
97 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
98 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
99 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
100 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
102 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
113 unsigned int auth_data_len
;
114 BOOL finished
; /* finished authenticating */
117 static BOOL
HTTP_OpenConnection(LPWININETHTTPREQW lpwhr
);
118 static BOOL
HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr
, BOOL clear
);
119 static BOOL
HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr
, LPCWSTR field
, LPCWSTR value
, DWORD dwModifier
);
120 static LPWSTR
* HTTP_InterpretHttpHeader(LPCWSTR buffer
);
121 static BOOL
HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr
, LPHTTPHEADERW lpHdr
);
122 static INT
HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszField
, INT index
, BOOL Request
);
123 static BOOL
HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr
, DWORD index
);
124 static LPWSTR
HTTP_build_req( LPCWSTR
*list
, int len
);
125 static BOOL
HTTP_HttpQueryInfoW(LPWININETHTTPREQW
, DWORD
, LPVOID
, LPDWORD
, LPDWORD
);
126 static LPWSTR
HTTP_GetRedirectURL(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszUrl
);
127 static BOOL
HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszUrl
);
128 static UINT
HTTP_DecodeBase64(LPCWSTR base64
, LPSTR bin
);
129 static BOOL
HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr
, LPCWSTR field
);
130 static void HTTP_DrainContent(WININETHTTPREQW
*req
);
131 static BOOL
HTTP_FinishedReading(LPWININETHTTPREQW lpwhr
);
133 static LPHTTPHEADERW
HTTP_GetHeader(LPWININETHTTPREQW req
, LPCWSTR head
)
136 HeaderIndex
= HTTP_GetCustomHeaderIndex(req
, head
, 0, TRUE
);
137 if (HeaderIndex
== -1)
140 return &req
->pCustHeaders
[HeaderIndex
];
143 /* set the request content length based on the headers */
144 static DWORD
set_content_length( LPWININETHTTPREQW lpwhr
)
146 static const WCHAR szChunked
[] = {'c','h','u','n','k','e','d',0};
150 size
= sizeof(lpwhr
->dwContentLength
);
151 if (!HTTP_HttpQueryInfoW(lpwhr
, HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_CONTENT_LENGTH
,
152 &lpwhr
->dwContentLength
, &size
, NULL
))
153 lpwhr
->dwContentLength
= ~0u;
155 size
= sizeof(encoding
);
156 if (HTTP_HttpQueryInfoW(lpwhr
, HTTP_QUERY_TRANSFER_ENCODING
, encoding
, &size
, NULL
) &&
157 !strcmpiW(encoding
, szChunked
))
159 lpwhr
->dwContentLength
= ~0u;
160 lpwhr
->read_chunked
= TRUE
;
163 return lpwhr
->dwContentLength
;
166 /***********************************************************************
167 * HTTP_Tokenize (internal)
169 * Tokenize a string, allocating memory for the tokens.
171 static LPWSTR
* HTTP_Tokenize(LPCWSTR string
, LPCWSTR token_string
)
173 LPWSTR
* token_array
;
180 /* empty string has no tokens */
184 for (i
= 0; string
[i
]; i
++)
186 if (!strncmpW(string
+i
, token_string
, strlenW(token_string
)))
190 /* we want to skip over separators, but not the null terminator */
191 for (j
= 0; j
< strlenW(token_string
) - 1; j
++)
199 /* add 1 for terminating NULL */
200 token_array
= HeapAlloc(GetProcessHeap(), 0, (tokens
+1) * sizeof(*token_array
));
201 token_array
[tokens
] = NULL
;
204 for (i
= 0; i
< tokens
; i
++)
207 next_token
= strstrW(string
, token_string
);
208 if (!next_token
) next_token
= string
+strlenW(string
);
209 len
= next_token
- string
;
210 token_array
[i
] = HeapAlloc(GetProcessHeap(), 0, (len
+1)*sizeof(WCHAR
));
211 memcpy(token_array
[i
], string
, len
*sizeof(WCHAR
));
212 token_array
[i
][len
] = '\0';
213 string
= next_token
+strlenW(token_string
);
218 /***********************************************************************
219 * HTTP_FreeTokens (internal)
221 * Frees memory returned from HTTP_Tokenize.
223 static void HTTP_FreeTokens(LPWSTR
* token_array
)
226 for (i
= 0; token_array
[i
]; i
++)
227 HeapFree(GetProcessHeap(), 0, token_array
[i
]);
228 HeapFree(GetProcessHeap(), 0, token_array
);
231 /* **********************************************************************
233 * Helper functions for the HttpSendRequest(Ex) functions
236 static void AsyncHttpSendRequestProc(WORKREQUEST
*workRequest
)
238 struct WORKREQ_HTTPSENDREQUESTW
const *req
= &workRequest
->u
.HttpSendRequestW
;
239 LPWININETHTTPREQW lpwhr
= (LPWININETHTTPREQW
) workRequest
->hdr
;
241 TRACE("%p\n", lpwhr
);
243 HTTP_HttpSendRequestW(lpwhr
, req
->lpszHeader
,
244 req
->dwHeaderLength
, req
->lpOptional
, req
->dwOptionalLength
,
245 req
->dwContentLength
, req
->bEndRequest
);
247 HeapFree(GetProcessHeap(), 0, req
->lpszHeader
);
250 static void HTTP_FixURL( LPWININETHTTPREQW lpwhr
)
252 static const WCHAR szSlash
[] = { '/',0 };
253 static const WCHAR szHttp
[] = { 'h','t','t','p',':','/','/', 0 };
255 /* If we don't have a path we set it to root */
256 if (NULL
== lpwhr
->lpszPath
)
257 lpwhr
->lpszPath
= WININET_strdupW(szSlash
);
258 else /* remove \r and \n*/
260 int nLen
= strlenW(lpwhr
->lpszPath
);
261 while ((nLen
>0 ) && ((lpwhr
->lpszPath
[nLen
-1] == '\r')||(lpwhr
->lpszPath
[nLen
-1] == '\n')))
264 lpwhr
->lpszPath
[nLen
]='\0';
266 /* Replace '\' with '/' */
269 if (lpwhr
->lpszPath
[nLen
] == '\\') lpwhr
->lpszPath
[nLen
]='/';
273 if(CSTR_EQUAL
!= CompareStringW( LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
,
274 lpwhr
->lpszPath
, strlenW(lpwhr
->lpszPath
), szHttp
, strlenW(szHttp
) )
275 && lpwhr
->lpszPath
[0] != '/') /* not an absolute path ?? --> fix it !! */
277 WCHAR
*fixurl
= HeapAlloc(GetProcessHeap(), 0,
278 (strlenW(lpwhr
->lpszPath
) + 2)*sizeof(WCHAR
));
280 strcpyW(fixurl
+ 1, lpwhr
->lpszPath
);
281 HeapFree( GetProcessHeap(), 0, lpwhr
->lpszPath
);
282 lpwhr
->lpszPath
= fixurl
;
286 static LPWSTR
HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr
, LPCWSTR verb
, LPCWSTR path
, LPCWSTR version
)
288 LPWSTR requestString
;
294 static const WCHAR szSpace
[] = { ' ',0 };
295 static const WCHAR szColon
[] = { ':',' ',0 };
296 static const WCHAR sztwocrlf
[] = {'\r','\n','\r','\n', 0};
298 /* allocate space for an array of all the string pointers to be added */
299 len
= (lpwhr
->nCustHeaders
)*4 + 10;
300 req
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(LPCWSTR
) );
302 /* add the verb, path and HTTP version string */
310 /* Append custom request headers */
311 for (i
= 0; i
< lpwhr
->nCustHeaders
; i
++)
313 if (lpwhr
->pCustHeaders
[i
].wFlags
& HDR_ISREQUEST
)
316 req
[n
++] = lpwhr
->pCustHeaders
[i
].lpszField
;
318 req
[n
++] = lpwhr
->pCustHeaders
[i
].lpszValue
;
320 TRACE("Adding custom header %s (%s)\n",
321 debugstr_w(lpwhr
->pCustHeaders
[i
].lpszField
),
322 debugstr_w(lpwhr
->pCustHeaders
[i
].lpszValue
));
327 ERR("oops. buffer overrun\n");
330 requestString
= HTTP_build_req( req
, 4 );
331 HeapFree( GetProcessHeap(), 0, req
);
334 * Set (header) termination string for request
335 * Make sure there's exactly two new lines at the end of the request
337 p
= &requestString
[strlenW(requestString
)-1];
338 while ( (*p
== '\n') || (*p
== '\r') )
340 strcpyW( p
+1, sztwocrlf
);
342 return requestString
;
345 static void HTTP_ProcessCookies( LPWININETHTTPREQW lpwhr
)
347 static const WCHAR szSet_Cookie
[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
350 LPHTTPHEADERW setCookieHeader
;
352 while((HeaderIndex
= HTTP_GetCustomHeaderIndex(lpwhr
, szSet_Cookie
, numCookies
, FALSE
)) != -1)
354 setCookieHeader
= &lpwhr
->pCustHeaders
[HeaderIndex
];
356 if (!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_COOKIES
) && setCookieHeader
->lpszValue
)
359 static const WCHAR szFmt
[] = { 'h','t','t','p',':','/','/','%','s','%','s',0};
363 Host
= HTTP_GetHeader(lpwhr
,szHost
);
364 len
= lstrlenW(Host
->lpszValue
) + 9 + lstrlenW(lpwhr
->lpszPath
);
365 buf_url
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
366 sprintfW(buf_url
, szFmt
, Host
->lpszValue
, lpwhr
->lpszPath
);
367 InternetSetCookieW(buf_url
, NULL
, setCookieHeader
->lpszValue
);
369 HeapFree(GetProcessHeap(), 0, buf_url
);
375 static inline BOOL
is_basic_auth_value( LPCWSTR pszAuthValue
)
377 static const WCHAR szBasic
[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
378 return !strncmpiW(pszAuthValue
, szBasic
, ARRAYSIZE(szBasic
)) &&
379 ((pszAuthValue
[ARRAYSIZE(szBasic
)] == ' ') || !pszAuthValue
[ARRAYSIZE(szBasic
)]);
382 static BOOL
HTTP_DoAuthorization( LPWININETHTTPREQW lpwhr
, LPCWSTR pszAuthValue
,
383 struct HttpAuthInfo
**ppAuthInfo
,
384 LPWSTR domain_and_username
, LPWSTR password
)
386 SECURITY_STATUS sec_status
;
387 struct HttpAuthInfo
*pAuthInfo
= *ppAuthInfo
;
390 TRACE("%s\n", debugstr_w(pszAuthValue
));
397 pAuthInfo
= HeapAlloc(GetProcessHeap(), 0, sizeof(*pAuthInfo
));
401 SecInvalidateHandle(&pAuthInfo
->cred
);
402 SecInvalidateHandle(&pAuthInfo
->ctx
);
403 memset(&pAuthInfo
->exp
, 0, sizeof(pAuthInfo
->exp
));
405 pAuthInfo
->auth_data
= NULL
;
406 pAuthInfo
->auth_data_len
= 0;
407 pAuthInfo
->finished
= FALSE
;
409 if (is_basic_auth_value(pszAuthValue
))
411 static const WCHAR szBasic
[] = {'B','a','s','i','c',0};
412 pAuthInfo
->scheme
= WININET_strdupW(szBasic
);
413 if (!pAuthInfo
->scheme
)
415 HeapFree(GetProcessHeap(), 0, pAuthInfo
);
422 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity
;
424 pAuthInfo
->scheme
= WININET_strdupW(pszAuthValue
);
425 if (!pAuthInfo
->scheme
)
427 HeapFree(GetProcessHeap(), 0, pAuthInfo
);
431 if (domain_and_username
)
433 WCHAR
*user
= strchrW(domain_and_username
, '\\');
434 WCHAR
*domain
= domain_and_username
;
436 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
438 pAuthData
= &nt_auth_identity
;
443 user
= domain_and_username
;
447 nt_auth_identity
.Flags
= SEC_WINNT_AUTH_IDENTITY_UNICODE
;
448 nt_auth_identity
.User
= user
;
449 nt_auth_identity
.UserLength
= strlenW(nt_auth_identity
.User
);
450 nt_auth_identity
.Domain
= domain
;
451 nt_auth_identity
.DomainLength
= domain
? user
- domain
- 1 : 0;
452 nt_auth_identity
.Password
= password
;
453 nt_auth_identity
.PasswordLength
= strlenW(nt_auth_identity
.Password
);
456 /* use default credentials */
459 sec_status
= AcquireCredentialsHandleW(NULL
, pAuthInfo
->scheme
,
460 SECPKG_CRED_OUTBOUND
, NULL
,
462 NULL
, &pAuthInfo
->cred
,
464 if (sec_status
== SEC_E_OK
)
466 PSecPkgInfoW sec_pkg_info
;
467 sec_status
= QuerySecurityPackageInfoW(pAuthInfo
->scheme
, &sec_pkg_info
);
468 if (sec_status
== SEC_E_OK
)
470 pAuthInfo
->max_token
= sec_pkg_info
->cbMaxToken
;
471 FreeContextBuffer(sec_pkg_info
);
474 if (sec_status
!= SEC_E_OK
)
476 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
477 debugstr_w(pAuthInfo
->scheme
), sec_status
);
478 HeapFree(GetProcessHeap(), 0, pAuthInfo
->scheme
);
479 HeapFree(GetProcessHeap(), 0, pAuthInfo
);
483 *ppAuthInfo
= pAuthInfo
;
485 else if (pAuthInfo
->finished
)
488 if ((strlenW(pszAuthValue
) < strlenW(pAuthInfo
->scheme
)) ||
489 strncmpiW(pszAuthValue
, pAuthInfo
->scheme
, strlenW(pAuthInfo
->scheme
)))
491 ERR("authentication scheme changed from %s to %s\n",
492 debugstr_w(pAuthInfo
->scheme
), debugstr_w(pszAuthValue
));
496 if (is_basic_auth_value(pszAuthValue
))
502 TRACE("basic authentication\n");
504 /* we don't cache credentials for basic authentication, so we can't
505 * retrieve them if the application didn't pass us any credentials */
506 if (!domain_and_username
) return FALSE
;
508 userlen
= WideCharToMultiByte(CP_UTF8
, 0, domain_and_username
, lstrlenW(domain_and_username
), NULL
, 0, NULL
, NULL
);
509 passlen
= WideCharToMultiByte(CP_UTF8
, 0, password
, lstrlenW(password
), NULL
, 0, NULL
, NULL
);
511 /* length includes a nul terminator, which will be re-used for the ':' */
512 auth_data
= HeapAlloc(GetProcessHeap(), 0, userlen
+ 1 + passlen
);
516 WideCharToMultiByte(CP_UTF8
, 0, domain_and_username
, -1, auth_data
, userlen
, NULL
, NULL
);
517 auth_data
[userlen
] = ':';
518 WideCharToMultiByte(CP_UTF8
, 0, password
, -1, &auth_data
[userlen
+1], passlen
, NULL
, NULL
);
520 pAuthInfo
->auth_data
= auth_data
;
521 pAuthInfo
->auth_data_len
= userlen
+ 1 + passlen
;
522 pAuthInfo
->finished
= TRUE
;
529 SecBufferDesc out_desc
, in_desc
;
531 unsigned char *buffer
;
532 ULONG context_req
= ISC_REQ_CONNECTION
| ISC_REQ_USE_DCE_STYLE
|
533 ISC_REQ_MUTUAL_AUTH
| ISC_REQ_DELEGATE
;
535 in
.BufferType
= SECBUFFER_TOKEN
;
539 in_desc
.ulVersion
= 0;
540 in_desc
.cBuffers
= 1;
541 in_desc
.pBuffers
= &in
;
543 pszAuthData
= pszAuthValue
+ strlenW(pAuthInfo
->scheme
);
544 if (*pszAuthData
== ' ')
547 in
.cbBuffer
= HTTP_DecodeBase64(pszAuthData
, NULL
);
548 in
.pvBuffer
= HeapAlloc(GetProcessHeap(), 0, in
.cbBuffer
);
549 HTTP_DecodeBase64(pszAuthData
, in
.pvBuffer
);
552 buffer
= HeapAlloc(GetProcessHeap(), 0, pAuthInfo
->max_token
);
554 out
.BufferType
= SECBUFFER_TOKEN
;
555 out
.cbBuffer
= pAuthInfo
->max_token
;
556 out
.pvBuffer
= buffer
;
558 out_desc
.ulVersion
= 0;
559 out_desc
.cBuffers
= 1;
560 out_desc
.pBuffers
= &out
;
562 sec_status
= InitializeSecurityContextW(first
? &pAuthInfo
->cred
: NULL
,
563 first
? NULL
: &pAuthInfo
->ctx
,
564 first
? lpwhr
->lpHttpSession
->lpszServerName
: NULL
,
565 context_req
, 0, SECURITY_NETWORK_DREP
,
566 in
.pvBuffer
? &in_desc
: NULL
,
567 0, &pAuthInfo
->ctx
, &out_desc
,
568 &pAuthInfo
->attr
, &pAuthInfo
->exp
);
569 if (sec_status
== SEC_E_OK
)
571 pAuthInfo
->finished
= TRUE
;
572 pAuthInfo
->auth_data
= out
.pvBuffer
;
573 pAuthInfo
->auth_data_len
= out
.cbBuffer
;
574 TRACE("sending last auth packet\n");
576 else if (sec_status
== SEC_I_CONTINUE_NEEDED
)
578 pAuthInfo
->auth_data
= out
.pvBuffer
;
579 pAuthInfo
->auth_data_len
= out
.cbBuffer
;
580 TRACE("sending next auth packet\n");
584 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status
);
585 pAuthInfo
->finished
= TRUE
;
586 HeapFree(GetProcessHeap(), 0, out
.pvBuffer
);
594 /***********************************************************************
595 * HTTP_HttpAddRequestHeadersW (internal)
597 static BOOL
HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr
,
598 LPCWSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
603 BOOL bSuccess
= FALSE
;
606 TRACE("copying header: %s\n", debugstr_wn(lpszHeader
, dwHeaderLength
));
608 if( dwHeaderLength
== ~0U )
609 len
= strlenW(lpszHeader
);
611 len
= dwHeaderLength
;
612 buffer
= HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR
)*(len
+1) );
613 lstrcpynW( buffer
, lpszHeader
, len
+ 1);
619 LPWSTR
* pFieldAndValue
;
623 while (*lpszEnd
!= '\0')
625 if (*lpszEnd
== '\r' && *(lpszEnd
+ 1) == '\n')
630 if (*lpszStart
== '\0')
633 if (*lpszEnd
== '\r')
636 lpszEnd
+= 2; /* Jump over \r\n */
638 TRACE("interpreting header %s\n", debugstr_w(lpszStart
));
639 if (*lpszStart
== '\0')
641 /* Skip 0-length headers */
646 pFieldAndValue
= HTTP_InterpretHttpHeader(lpszStart
);
649 bSuccess
= HTTP_VerifyValidHeader(lpwhr
, pFieldAndValue
[0]);
651 bSuccess
= HTTP_ProcessHeader(lpwhr
, pFieldAndValue
[0],
652 pFieldAndValue
[1], dwModifier
| HTTP_ADDHDR_FLAG_REQ
);
653 HTTP_FreeTokens(pFieldAndValue
);
659 HeapFree(GetProcessHeap(), 0, buffer
);
664 /***********************************************************************
665 * HttpAddRequestHeadersW (WININET.@)
667 * Adds one or more HTTP header to the request handler
670 * On Windows if dwHeaderLength includes the trailing '\0', then
671 * HttpAddRequestHeadersW() adds it too. However this results in an
672 * invalid Http header which is rejected by some servers so we probably
673 * don't need to match Windows on that point.
680 BOOL WINAPI
HttpAddRequestHeadersW(HINTERNET hHttpRequest
,
681 LPCWSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
683 BOOL bSuccess
= FALSE
;
684 LPWININETHTTPREQW lpwhr
;
686 TRACE("%p, %s, %i, %i\n", hHttpRequest
, debugstr_wn(lpszHeader
, dwHeaderLength
), dwHeaderLength
, dwModifier
);
691 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hHttpRequest
);
692 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
694 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
697 bSuccess
= HTTP_HttpAddRequestHeadersW( lpwhr
, lpszHeader
, dwHeaderLength
, dwModifier
);
700 WININET_Release( &lpwhr
->hdr
);
705 /***********************************************************************
706 * HttpAddRequestHeadersA (WININET.@)
708 * Adds one or more HTTP header to the request handler
715 BOOL WINAPI
HttpAddRequestHeadersA(HINTERNET hHttpRequest
,
716 LPCSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
722 TRACE("%p, %s, %i, %i\n", hHttpRequest
, debugstr_an(lpszHeader
, dwHeaderLength
), dwHeaderLength
, dwModifier
);
724 len
= MultiByteToWideChar( CP_ACP
, 0, lpszHeader
, dwHeaderLength
, NULL
, 0 );
725 hdr
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
726 MultiByteToWideChar( CP_ACP
, 0, lpszHeader
, dwHeaderLength
, hdr
, len
);
727 if( dwHeaderLength
!= ~0U )
728 dwHeaderLength
= len
;
730 r
= HttpAddRequestHeadersW( hHttpRequest
, hdr
, dwHeaderLength
, dwModifier
);
732 HeapFree( GetProcessHeap(), 0, hdr
);
737 /***********************************************************************
738 * HttpEndRequestA (WININET.@)
740 * Ends an HTTP request that was started by HttpSendRequestEx
747 BOOL WINAPI
HttpEndRequestA(HINTERNET hRequest
,
748 LPINTERNET_BUFFERSA lpBuffersOut
, DWORD dwFlags
, DWORD_PTR dwContext
)
750 TRACE("(%p, %p, %08x, %08lx)\n", hRequest
, lpBuffersOut
, dwFlags
, dwContext
);
754 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
758 return HttpEndRequestW(hRequest
, NULL
, dwFlags
, dwContext
);
761 static BOOL
HTTP_HttpEndRequestW(LPWININETHTTPREQW lpwhr
, DWORD dwFlags
, DWORD_PTR dwContext
)
766 INTERNET_ASYNC_RESULT iar
;
768 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
769 INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
771 responseLen
= HTTP_GetResponseHeaders(lpwhr
, TRUE
);
775 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
776 INTERNET_STATUS_RESPONSE_RECEIVED
, &responseLen
, sizeof(DWORD
));
778 /* process cookies here. Is this right? */
779 HTTP_ProcessCookies(lpwhr
);
781 if (!set_content_length( lpwhr
)) HTTP_FinishedReading(lpwhr
);
783 if (!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTO_REDIRECT
))
785 DWORD dwCode
,dwCodeLength
= sizeof(DWORD
);
786 if (HTTP_HttpQueryInfoW(lpwhr
, HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_STATUS_CODE
, &dwCode
, &dwCodeLength
, NULL
) &&
787 (dwCode
== 302 || dwCode
== 301 || dwCode
== 303))
789 WCHAR
*new_url
, szNewLocation
[INTERNET_MAX_URL_LENGTH
];
790 dwBufferSize
=sizeof(szNewLocation
);
791 if (HTTP_HttpQueryInfoW(lpwhr
, HTTP_QUERY_LOCATION
, szNewLocation
, &dwBufferSize
, NULL
))
793 /* redirects are always GETs */
794 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszVerb
);
795 lpwhr
->lpszVerb
= WININET_strdupW(szGET
);
796 HTTP_DrainContent(lpwhr
);
797 if ((new_url
= HTTP_GetRedirectURL( lpwhr
, szNewLocation
)))
799 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
, INTERNET_STATUS_REDIRECT
,
800 new_url
, (strlenW(new_url
) + 1) * sizeof(WCHAR
));
801 rc
= HTTP_HandleRedirect(lpwhr
, new_url
);
803 rc
= HTTP_HttpSendRequestW(lpwhr
, NULL
, 0, NULL
, 0, 0, TRUE
);
804 HeapFree( GetProcessHeap(), 0, new_url
);
810 iar
.dwResult
= (DWORD_PTR
)lpwhr
->hdr
.hInternet
;
811 iar
.dwError
= rc
? 0 : INTERNET_GetLastError();
813 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
814 INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
815 sizeof(INTERNET_ASYNC_RESULT
));
819 static void AsyncHttpEndRequestProc(WORKREQUEST
*work
)
821 struct WORKREQ_HTTPENDREQUESTW
const *req
= &work
->u
.HttpEndRequestW
;
822 LPWININETHTTPREQW lpwhr
= (LPWININETHTTPREQW
)work
->hdr
;
824 TRACE("%p\n", lpwhr
);
826 HTTP_HttpEndRequestW(lpwhr
, req
->dwFlags
, req
->dwContext
);
829 /***********************************************************************
830 * HttpEndRequestW (WININET.@)
832 * Ends an HTTP request that was started by HttpSendRequestEx
839 BOOL WINAPI
HttpEndRequestW(HINTERNET hRequest
,
840 LPINTERNET_BUFFERSW lpBuffersOut
, DWORD dwFlags
, DWORD_PTR dwContext
)
843 LPWININETHTTPREQW lpwhr
;
849 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
853 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hRequest
);
855 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
857 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
859 WININET_Release( &lpwhr
->hdr
);
862 lpwhr
->hdr
.dwFlags
|= dwFlags
;
864 if (lpwhr
->lpHttpSession
->lpAppInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
867 struct WORKREQ_HTTPENDREQUESTW
*request
;
869 work
.asyncproc
= AsyncHttpEndRequestProc
;
870 work
.hdr
= WININET_AddRef( &lpwhr
->hdr
);
872 request
= &work
.u
.HttpEndRequestW
;
873 request
->dwFlags
= dwFlags
;
874 request
->dwContext
= dwContext
;
876 INTERNET_AsyncCall(&work
);
877 INTERNET_SetLastError(ERROR_IO_PENDING
);
880 rc
= HTTP_HttpEndRequestW(lpwhr
, dwFlags
, dwContext
);
882 WININET_Release( &lpwhr
->hdr
);
883 TRACE("%i <--\n",rc
);
887 /***********************************************************************
888 * HttpOpenRequestW (WININET.@)
890 * Open a HTTP request handle
893 * HINTERNET a HTTP request handle on success
897 HINTERNET WINAPI
HttpOpenRequestW(HINTERNET hHttpSession
,
898 LPCWSTR lpszVerb
, LPCWSTR lpszObjectName
, LPCWSTR lpszVersion
,
899 LPCWSTR lpszReferrer
, LPCWSTR
*lpszAcceptTypes
,
900 DWORD dwFlags
, DWORD_PTR dwContext
)
902 LPWININETHTTPSESSIONW lpwhs
;
903 HINTERNET handle
= NULL
;
905 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession
,
906 debugstr_w(lpszVerb
), debugstr_w(lpszObjectName
),
907 debugstr_w(lpszVersion
), debugstr_w(lpszReferrer
), lpszAcceptTypes
,
909 if(lpszAcceptTypes
!=NULL
)
912 for(i
=0;lpszAcceptTypes
[i
]!=NULL
;i
++)
913 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes
[i
]));
916 lpwhs
= (LPWININETHTTPSESSIONW
) WININET_GetObject( hHttpSession
);
917 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
919 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
924 * My tests seem to show that the windows version does not
925 * become asynchronous until after this point. And anyhow
926 * if this call was asynchronous then how would you get the
927 * necessary HINTERNET pointer returned by this function.
930 handle
= HTTP_HttpOpenRequestW(lpwhs
, lpszVerb
, lpszObjectName
,
931 lpszVersion
, lpszReferrer
, lpszAcceptTypes
,
935 WININET_Release( &lpwhs
->hdr
);
936 TRACE("returning %p\n", handle
);
941 /***********************************************************************
942 * HttpOpenRequestA (WININET.@)
944 * Open a HTTP request handle
947 * HINTERNET a HTTP request handle on success
951 HINTERNET WINAPI
HttpOpenRequestA(HINTERNET hHttpSession
,
952 LPCSTR lpszVerb
, LPCSTR lpszObjectName
, LPCSTR lpszVersion
,
953 LPCSTR lpszReferrer
, LPCSTR
*lpszAcceptTypes
,
954 DWORD dwFlags
, DWORD_PTR dwContext
)
956 LPWSTR szVerb
= NULL
, szObjectName
= NULL
;
957 LPWSTR szVersion
= NULL
, szReferrer
= NULL
, *szAcceptTypes
= NULL
;
958 INT len
, acceptTypesCount
;
959 HINTERNET rc
= FALSE
;
962 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession
,
963 debugstr_a(lpszVerb
), debugstr_a(lpszObjectName
),
964 debugstr_a(lpszVersion
), debugstr_a(lpszReferrer
), lpszAcceptTypes
,
969 len
= MultiByteToWideChar(CP_ACP
, 0, lpszVerb
, -1, NULL
, 0 );
970 szVerb
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
973 MultiByteToWideChar(CP_ACP
, 0, lpszVerb
, -1, szVerb
, len
);
978 len
= MultiByteToWideChar(CP_ACP
, 0, lpszObjectName
, -1, NULL
, 0 );
979 szObjectName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
982 MultiByteToWideChar(CP_ACP
, 0, lpszObjectName
, -1, szObjectName
, len
);
987 len
= MultiByteToWideChar(CP_ACP
, 0, lpszVersion
, -1, NULL
, 0 );
988 szVersion
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
991 MultiByteToWideChar(CP_ACP
, 0, lpszVersion
, -1, szVersion
, len
);
996 len
= MultiByteToWideChar(CP_ACP
, 0, lpszReferrer
, -1, NULL
, 0 );
997 szReferrer
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1000 MultiByteToWideChar(CP_ACP
, 0, lpszReferrer
, -1, szReferrer
, len
);
1003 if (lpszAcceptTypes
)
1005 acceptTypesCount
= 0;
1006 types
= lpszAcceptTypes
;
1011 /* find out how many there are */
1012 if (*types
&& **types
)
1014 TRACE("accept type: %s\n", debugstr_a(*types
));
1020 WARN("invalid accept type pointer\n");
1025 szAcceptTypes
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
*) * (acceptTypesCount
+1));
1026 if (!szAcceptTypes
) goto end
;
1028 acceptTypesCount
= 0;
1029 types
= lpszAcceptTypes
;
1034 if (*types
&& **types
)
1036 len
= MultiByteToWideChar(CP_ACP
, 0, *types
, -1, NULL
, 0 );
1037 szAcceptTypes
[acceptTypesCount
] = HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1039 MultiByteToWideChar(CP_ACP
, 0, *types
, -1, szAcceptTypes
[acceptTypesCount
], len
);
1045 /* ignore invalid pointer */
1050 szAcceptTypes
[acceptTypesCount
] = NULL
;
1053 rc
= HttpOpenRequestW(hHttpSession
, szVerb
, szObjectName
,
1054 szVersion
, szReferrer
,
1055 (LPCWSTR
*)szAcceptTypes
, dwFlags
, dwContext
);
1060 acceptTypesCount
= 0;
1061 while (szAcceptTypes
[acceptTypesCount
])
1063 HeapFree(GetProcessHeap(), 0, szAcceptTypes
[acceptTypesCount
]);
1066 HeapFree(GetProcessHeap(), 0, szAcceptTypes
);
1068 HeapFree(GetProcessHeap(), 0, szReferrer
);
1069 HeapFree(GetProcessHeap(), 0, szVersion
);
1070 HeapFree(GetProcessHeap(), 0, szObjectName
);
1071 HeapFree(GetProcessHeap(), 0, szVerb
);
1076 /***********************************************************************
1079 static UINT
HTTP_EncodeBase64( LPCSTR bin
, unsigned int len
, LPWSTR base64
)
1082 static const CHAR HTTP_Base64Enc
[] =
1083 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1087 /* first 6 bits, all from bin[0] */
1088 base64
[n
++] = HTTP_Base64Enc
[(bin
[0] & 0xfc) >> 2];
1089 x
= (bin
[0] & 3) << 4;
1091 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1094 base64
[n
++] = HTTP_Base64Enc
[x
];
1099 base64
[n
++] = HTTP_Base64Enc
[ x
| ( (bin
[1]&0xf0) >> 4 ) ];
1100 x
= ( bin
[1] & 0x0f ) << 2;
1102 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1105 base64
[n
++] = HTTP_Base64Enc
[x
];
1109 base64
[n
++] = HTTP_Base64Enc
[ x
| ( (bin
[2]&0xc0 ) >> 6 ) ];
1111 /* last 6 bits, all from bin [2] */
1112 base64
[n
++] = HTTP_Base64Enc
[ bin
[2] & 0x3f ];
1120 #define CH(x) (((x) >= 'A' && (x) <= 'Z') ? (x) - 'A' : \
1121 ((x) >= 'a' && (x) <= 'z') ? (x) - 'a' + 26 : \
1122 ((x) >= '0' && (x) <= '9') ? (x) - '0' + 52 : \
1123 ((x) == '+') ? 62 : ((x) == '/') ? 63 : -1)
1124 static const signed char HTTP_Base64Dec
[256] =
1126 CH( 0),CH( 1),CH( 2),CH( 3),CH( 4),CH( 5),CH( 6),CH( 7),CH( 8),CH( 9),
1127 CH(10),CH(11),CH(12),CH(13),CH(14),CH(15),CH(16),CH(17),CH(18),CH(19),
1128 CH(20),CH(21),CH(22),CH(23),CH(24),CH(25),CH(26),CH(27),CH(28),CH(29),
1129 CH(30),CH(31),CH(32),CH(33),CH(34),CH(35),CH(36),CH(37),CH(38),CH(39),
1130 CH(40),CH(41),CH(42),CH(43),CH(44),CH(45),CH(46),CH(47),CH(48),CH(49),
1131 CH(50),CH(51),CH(52),CH(53),CH(54),CH(55),CH(56),CH(57),CH(58),CH(59),
1132 CH(60),CH(61),CH(62),CH(63),CH(64),CH(65),CH(66),CH(67),CH(68),CH(69),
1133 CH(70),CH(71),CH(72),CH(73),CH(74),CH(75),CH(76),CH(77),CH(78),CH(79),
1134 CH(80),CH(81),CH(82),CH(83),CH(84),CH(85),CH(86),CH(87),CH(88),CH(89),
1135 CH(90),CH(91),CH(92),CH(93),CH(94),CH(95),CH(96),CH(97),CH(98),CH(99),
1136 CH(100),CH(101),CH(102),CH(103),CH(104),CH(105),CH(106),CH(107),CH(108),CH(109),
1137 CH(110),CH(111),CH(112),CH(113),CH(114),CH(115),CH(116),CH(117),CH(118),CH(119),
1138 CH(120),CH(121),CH(122),CH(123),CH(124),CH(125),CH(126),CH(127),CH(128),CH(129),
1139 CH(130),CH(131),CH(132),CH(133),CH(134),CH(135),CH(136),CH(137),CH(138),CH(139),
1140 CH(140),CH(141),CH(142),CH(143),CH(144),CH(145),CH(146),CH(147),CH(148),CH(149),
1141 CH(150),CH(151),CH(152),CH(153),CH(154),CH(155),CH(156),CH(157),CH(158),CH(159),
1142 CH(160),CH(161),CH(162),CH(163),CH(164),CH(165),CH(166),CH(167),CH(168),CH(169),
1143 CH(170),CH(171),CH(172),CH(173),CH(174),CH(175),CH(176),CH(177),CH(178),CH(179),
1144 CH(180),CH(181),CH(182),CH(183),CH(184),CH(185),CH(186),CH(187),CH(188),CH(189),
1145 CH(190),CH(191),CH(192),CH(193),CH(194),CH(195),CH(196),CH(197),CH(198),CH(199),
1146 CH(200),CH(201),CH(202),CH(203),CH(204),CH(205),CH(206),CH(207),CH(208),CH(209),
1147 CH(210),CH(211),CH(212),CH(213),CH(214),CH(215),CH(216),CH(217),CH(218),CH(219),
1148 CH(220),CH(221),CH(222),CH(223),CH(224),CH(225),CH(226),CH(227),CH(228),CH(229),
1149 CH(230),CH(231),CH(232),CH(233),CH(234),CH(235),CH(236),CH(237),CH(238),CH(239),
1150 CH(240),CH(241),CH(242),CH(243),CH(244),CH(245),CH(246),CH(247),CH(248), CH(249),
1151 CH(250),CH(251),CH(252),CH(253),CH(254),CH(255),
1155 /***********************************************************************
1158 static UINT
HTTP_DecodeBase64( LPCWSTR base64
, LPSTR bin
)
1166 if (base64
[0] >= ARRAYSIZE(HTTP_Base64Dec
) ||
1167 ((in
[0] = HTTP_Base64Dec
[base64
[0]]) == -1) ||
1168 base64
[1] >= ARRAYSIZE(HTTP_Base64Dec
) ||
1169 ((in
[1] = HTTP_Base64Dec
[base64
[1]]) == -1))
1171 WARN("invalid base64: %s\n", debugstr_w(base64
));
1175 bin
[n
] = (unsigned char) (in
[0] << 2 | in
[1] >> 4);
1178 if ((base64
[2] == '=') && (base64
[3] == '='))
1180 if (base64
[2] > ARRAYSIZE(HTTP_Base64Dec
) ||
1181 ((in
[2] = HTTP_Base64Dec
[base64
[2]]) == -1))
1183 WARN("invalid base64: %s\n", debugstr_w(&base64
[2]));
1187 bin
[n
] = (unsigned char) (in
[1] << 4 | in
[2] >> 2);
1190 if (base64
[3] == '=')
1192 if (base64
[3] > ARRAYSIZE(HTTP_Base64Dec
) ||
1193 ((in
[3] = HTTP_Base64Dec
[base64
[3]]) == -1))
1195 WARN("invalid base64: %s\n", debugstr_w(&base64
[3]));
1199 bin
[n
] = (unsigned char) (((in
[2] << 6) & 0xc0) | in
[3]);
1208 /***********************************************************************
1209 * HTTP_InsertAuthorization
1211 * Insert or delete the authorization field in the request header.
1213 static BOOL
HTTP_InsertAuthorization( LPWININETHTTPREQW lpwhr
, struct HttpAuthInfo
*pAuthInfo
, LPCWSTR header
)
1217 static const WCHAR wszSpace
[] = {' ',0};
1218 static const WCHAR wszBasic
[] = {'B','a','s','i','c',0};
1220 WCHAR
*authorization
= NULL
;
1222 if (pAuthInfo
->auth_data_len
)
1224 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1225 len
= strlenW(pAuthInfo
->scheme
)+1+((pAuthInfo
->auth_data_len
+2)*4)/3;
1226 authorization
= HeapAlloc(GetProcessHeap(), 0, (len
+1)*sizeof(WCHAR
));
1230 strcpyW(authorization
, pAuthInfo
->scheme
);
1231 strcatW(authorization
, wszSpace
);
1232 HTTP_EncodeBase64(pAuthInfo
->auth_data
,
1233 pAuthInfo
->auth_data_len
,
1234 authorization
+strlenW(authorization
));
1236 /* clear the data as it isn't valid now that it has been sent to the
1237 * server, unless it's Basic authentication which doesn't do
1238 * connection tracking */
1239 if (strcmpiW(pAuthInfo
->scheme
, wszBasic
))
1241 HeapFree(GetProcessHeap(), 0, pAuthInfo
->auth_data
);
1242 pAuthInfo
->auth_data
= NULL
;
1243 pAuthInfo
->auth_data_len
= 0;
1247 TRACE("Inserting authorization: %s\n", debugstr_w(authorization
));
1249 HTTP_ProcessHeader(lpwhr
, header
, authorization
, HTTP_ADDHDR_FLAG_REQ
| HTTP_ADDHDR_FLAG_REPLACE
);
1251 HeapFree(GetProcessHeap(), 0, authorization
);
1256 static WCHAR
*HTTP_BuildProxyRequestUrl(WININETHTTPREQW
*req
)
1258 WCHAR new_location
[INTERNET_MAX_URL_LENGTH
], *url
;
1261 size
= sizeof(new_location
);
1262 if (HTTP_HttpQueryInfoW(req
, HTTP_QUERY_LOCATION
, new_location
, &size
, NULL
))
1264 if (!(url
= HeapAlloc( GetProcessHeap(), 0, size
+ sizeof(WCHAR
) ))) return NULL
;
1265 strcpyW( url
, new_location
);
1269 static const WCHAR slash
[] = { '/',0 };
1270 static const WCHAR format
[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
1271 static const WCHAR formatSSL
[] = { 'h','t','t','p','s',':','/','/','%','s',':','%','d',0 };
1272 WININETHTTPSESSIONW
*session
= req
->lpHttpSession
;
1274 size
= 16; /* "https://" + sizeof(port#) + ":/\0" */
1275 size
+= strlenW( session
->lpszHostName
) + strlenW( req
->lpszPath
);
1277 if (!(url
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) ))) return NULL
;
1279 if (req
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
)
1280 sprintfW( url
, formatSSL
, session
->lpszHostName
, session
->nHostPort
);
1282 sprintfW( url
, format
, session
->lpszHostName
, session
->nHostPort
);
1283 if (req
->lpszPath
[0] != '/') strcatW( url
, slash
);
1284 strcatW( url
, req
->lpszPath
);
1286 TRACE("url=%s\n", debugstr_w(url
));
1290 /***********************************************************************
1291 * HTTP_DealWithProxy
1293 static BOOL
HTTP_DealWithProxy( LPWININETAPPINFOW hIC
,
1294 LPWININETHTTPSESSIONW lpwhs
, LPWININETHTTPREQW lpwhr
)
1296 WCHAR buf
[MAXHOSTNAME
];
1297 WCHAR proxy
[MAXHOSTNAME
+ 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
1298 static WCHAR szNul
[] = { 0 };
1299 URL_COMPONENTSW UrlComponents
;
1300 static const WCHAR szHttp
[] = { 'h','t','t','p',':','/','/',0 };
1301 static const WCHAR szFormat
[] = { 'h','t','t','p',':','/','/','%','s',0 };
1303 memset( &UrlComponents
, 0, sizeof UrlComponents
);
1304 UrlComponents
.dwStructSize
= sizeof UrlComponents
;
1305 UrlComponents
.lpszHostName
= buf
;
1306 UrlComponents
.dwHostNameLength
= MAXHOSTNAME
;
1308 if( CSTR_EQUAL
!= CompareStringW(LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
,
1309 hIC
->lpszProxy
,strlenW(szHttp
),szHttp
,strlenW(szHttp
)) )
1310 sprintfW(proxy
, szFormat
, hIC
->lpszProxy
);
1312 strcpyW(proxy
, hIC
->lpszProxy
);
1313 if( !InternetCrackUrlW(proxy
, 0, 0, &UrlComponents
) )
1315 if( UrlComponents
.dwHostNameLength
== 0 )
1318 if( !lpwhr
->lpszPath
)
1319 lpwhr
->lpszPath
= szNul
;
1321 if(UrlComponents
.nPort
== INTERNET_INVALID_PORT_NUMBER
)
1322 UrlComponents
.nPort
= INTERNET_DEFAULT_HTTP_PORT
;
1324 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszServerName
);
1325 lpwhs
->lpszServerName
= WININET_strdupW(UrlComponents
.lpszHostName
);
1326 lpwhs
->nServerPort
= UrlComponents
.nPort
;
1328 TRACE("proxy server=%s port=%d\n", debugstr_w(lpwhs
->lpszServerName
), lpwhs
->nServerPort
);
1332 static BOOL
HTTP_ResolveName(LPWININETHTTPREQW lpwhr
)
1335 LPWININETHTTPSESSIONW lpwhs
= lpwhr
->lpHttpSession
;
1337 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
1338 INTERNET_STATUS_RESOLVING_NAME
,
1339 lpwhs
->lpszServerName
,
1340 strlenW(lpwhs
->lpszServerName
)+1);
1342 if (!GetAddress(lpwhs
->lpszServerName
, lpwhs
->nServerPort
,
1343 &lpwhs
->socketAddress
))
1345 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED
);
1349 inet_ntop(lpwhs
->socketAddress
.sin_family
, &lpwhs
->socketAddress
.sin_addr
,
1350 szaddr
, sizeof(szaddr
));
1351 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
1352 INTERNET_STATUS_NAME_RESOLVED
,
1353 szaddr
, strlen(szaddr
)+1);
1355 TRACE("resolved %s to %s\n", debugstr_w(lpwhs
->lpszServerName
), szaddr
);
1360 /***********************************************************************
1361 * HTTPREQ_Destroy (internal)
1363 * Deallocate request handle
1366 static void HTTPREQ_Destroy(WININETHANDLEHEADER
*hdr
)
1368 LPWININETHTTPREQW lpwhr
= (LPWININETHTTPREQW
) hdr
;
1373 if(lpwhr
->hCacheFile
)
1374 CloseHandle(lpwhr
->hCacheFile
);
1376 if(lpwhr
->lpszCacheFile
) {
1377 DeleteFileW(lpwhr
->lpszCacheFile
); /* FIXME */
1378 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszCacheFile
);
1381 WININET_Release(&lpwhr
->lpHttpSession
->hdr
);
1383 if (lpwhr
->pAuthInfo
)
1385 if (SecIsValidHandle(&lpwhr
->pAuthInfo
->ctx
))
1386 DeleteSecurityContext(&lpwhr
->pAuthInfo
->ctx
);
1387 if (SecIsValidHandle(&lpwhr
->pAuthInfo
->cred
))
1388 FreeCredentialsHandle(&lpwhr
->pAuthInfo
->cred
);
1390 HeapFree(GetProcessHeap(), 0, lpwhr
->pAuthInfo
->auth_data
);
1391 HeapFree(GetProcessHeap(), 0, lpwhr
->pAuthInfo
->scheme
);
1392 HeapFree(GetProcessHeap(), 0, lpwhr
->pAuthInfo
);
1393 lpwhr
->pAuthInfo
= NULL
;
1396 if (lpwhr
->pProxyAuthInfo
)
1398 if (SecIsValidHandle(&lpwhr
->pProxyAuthInfo
->ctx
))
1399 DeleteSecurityContext(&lpwhr
->pProxyAuthInfo
->ctx
);
1400 if (SecIsValidHandle(&lpwhr
->pProxyAuthInfo
->cred
))
1401 FreeCredentialsHandle(&lpwhr
->pProxyAuthInfo
->cred
);
1403 HeapFree(GetProcessHeap(), 0, lpwhr
->pProxyAuthInfo
->auth_data
);
1404 HeapFree(GetProcessHeap(), 0, lpwhr
->pProxyAuthInfo
->scheme
);
1405 HeapFree(GetProcessHeap(), 0, lpwhr
->pProxyAuthInfo
);
1406 lpwhr
->pProxyAuthInfo
= NULL
;
1409 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszPath
);
1410 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszVerb
);
1411 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszRawHeaders
);
1412 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszVersion
);
1413 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszStatusText
);
1415 for (i
= 0; i
< lpwhr
->nCustHeaders
; i
++)
1417 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
[i
].lpszField
);
1418 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
[i
].lpszValue
);
1421 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
);
1422 HeapFree(GetProcessHeap(), 0, lpwhr
);
1425 static void HTTPREQ_CloseConnection(WININETHANDLEHEADER
*hdr
)
1427 LPWININETHTTPREQW lpwhr
= (LPWININETHTTPREQW
) hdr
;
1429 TRACE("%p\n",lpwhr
);
1431 if (!NETCON_connected(&lpwhr
->netConnection
))
1434 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
1435 INTERNET_STATUS_CLOSING_CONNECTION
, 0, 0);
1437 NETCON_close(&lpwhr
->netConnection
);
1439 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
1440 INTERNET_STATUS_CONNECTION_CLOSED
, 0, 0);
1443 static DWORD
HTTPREQ_QueryOption(WININETHANDLEHEADER
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
1445 WININETHTTPREQW
*req
= (WININETHTTPREQW
*)hdr
;
1448 case INTERNET_OPTION_HANDLE_TYPE
:
1449 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
1451 if (*size
< sizeof(ULONG
))
1452 return ERROR_INSUFFICIENT_BUFFER
;
1454 *size
= sizeof(DWORD
);
1455 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_HTTP_REQUEST
;
1456 return ERROR_SUCCESS
;
1458 case INTERNET_OPTION_URL
: {
1459 WCHAR url
[INTERNET_MAX_URL_LENGTH
];
1464 static const WCHAR httpW
[] = {'h','t','t','p',':','/','/',0};
1465 static const WCHAR hostW
[] = {'H','o','s','t',0};
1467 TRACE("INTERNET_OPTION_URL\n");
1469 host
= HTTP_GetHeader(req
, hostW
);
1470 strcpyW(url
, httpW
);
1471 strcatW(url
, host
->lpszValue
);
1472 if (NULL
!= (pch
= strchrW(url
+ strlenW(httpW
), ':')))
1474 strcatW(url
, req
->lpszPath
);
1476 TRACE("INTERNET_OPTION_URL: %s\n",debugstr_w(url
));
1479 len
= (strlenW(url
)+1) * sizeof(WCHAR
);
1481 return ERROR_INSUFFICIENT_BUFFER
;
1484 strcpyW(buffer
, url
);
1485 return ERROR_SUCCESS
;
1487 len
= WideCharToMultiByte(CP_ACP
, 0, url
, -1, buffer
, *size
, NULL
, NULL
);
1489 return ERROR_INSUFFICIENT_BUFFER
;
1492 return ERROR_SUCCESS
;
1496 case INTERNET_OPTION_DATAFILE_NAME
: {
1499 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
1501 if(!req
->lpszCacheFile
) {
1503 return ERROR_INTERNET_ITEM_NOT_FOUND
;
1507 req_size
= (lstrlenW(req
->lpszCacheFile
)+1) * sizeof(WCHAR
);
1508 if(*size
< req_size
)
1509 return ERROR_INSUFFICIENT_BUFFER
;
1512 memcpy(buffer
, req
->lpszCacheFile
, *size
);
1513 return ERROR_SUCCESS
;
1515 req_size
= WideCharToMultiByte(CP_ACP
, 0, req
->lpszCacheFile
, -1, NULL
, 0, NULL
, NULL
);
1516 if (req_size
> *size
)
1517 return ERROR_INSUFFICIENT_BUFFER
;
1519 *size
= WideCharToMultiByte(CP_ACP
, 0, req
->lpszCacheFile
,
1520 -1, buffer
, *size
, NULL
, NULL
);
1521 return ERROR_SUCCESS
;
1525 case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT
: {
1526 PCCERT_CONTEXT context
;
1528 if(*size
< sizeof(INTERNET_CERTIFICATE_INFOW
)) {
1529 *size
= sizeof(INTERNET_CERTIFICATE_INFOW
);
1530 return ERROR_INSUFFICIENT_BUFFER
;
1533 context
= (PCCERT_CONTEXT
)NETCON_GetCert(&(req
->netConnection
));
1535 INTERNET_CERTIFICATE_INFOW
*info
= (INTERNET_CERTIFICATE_INFOW
*)buffer
;
1538 memset(info
, 0, sizeof(INTERNET_CERTIFICATE_INFOW
));
1539 info
->ftExpiry
= context
->pCertInfo
->NotAfter
;
1540 info
->ftStart
= context
->pCertInfo
->NotBefore
;
1542 len
= CertNameToStrW(context
->dwCertEncodingType
,
1543 &context
->pCertInfo
->Subject
, CERT_SIMPLE_NAME_STR
, NULL
, 0);
1544 info
->lpszSubjectInfo
= LocalAlloc(0, len
*sizeof(WCHAR
));
1545 if(info
->lpszSubjectInfo
)
1546 CertNameToStrW(context
->dwCertEncodingType
,
1547 &context
->pCertInfo
->Subject
, CERT_SIMPLE_NAME_STR
,
1548 info
->lpszSubjectInfo
, len
);
1549 len
= CertNameToStrW(context
->dwCertEncodingType
,
1550 &context
->pCertInfo
->Issuer
, CERT_SIMPLE_NAME_STR
, NULL
, 0);
1551 info
->lpszIssuerInfo
= LocalAlloc(0, len
*sizeof(WCHAR
));
1552 if (info
->lpszIssuerInfo
)
1553 CertNameToStrW(context
->dwCertEncodingType
,
1554 &context
->pCertInfo
->Issuer
, CERT_SIMPLE_NAME_STR
,
1555 info
->lpszIssuerInfo
, len
);
1557 INTERNET_CERTIFICATE_INFOA
*infoA
= (INTERNET_CERTIFICATE_INFOA
*)info
;
1559 len
= CertNameToStrA(context
->dwCertEncodingType
,
1560 &context
->pCertInfo
->Subject
, CERT_SIMPLE_NAME_STR
, NULL
, 0);
1561 infoA
->lpszSubjectInfo
= LocalAlloc(0, len
);
1562 if(infoA
->lpszSubjectInfo
)
1563 CertNameToStrA(context
->dwCertEncodingType
,
1564 &context
->pCertInfo
->Subject
, CERT_SIMPLE_NAME_STR
,
1565 infoA
->lpszSubjectInfo
, len
);
1566 len
= CertNameToStrA(context
->dwCertEncodingType
,
1567 &context
->pCertInfo
->Issuer
, CERT_SIMPLE_NAME_STR
, NULL
, 0);
1568 infoA
->lpszIssuerInfo
= LocalAlloc(0, len
);
1569 if(infoA
->lpszIssuerInfo
)
1570 CertNameToStrA(context
->dwCertEncodingType
,
1571 &context
->pCertInfo
->Issuer
, CERT_SIMPLE_NAME_STR
,
1572 infoA
->lpszIssuerInfo
, len
);
1576 * Contrary to MSDN, these do not appear to be set.
1578 * lpszSignatureAlgName
1579 * lpszEncryptionAlgName
1582 CertFreeCertificateContext(context
);
1583 return ERROR_SUCCESS
;
1588 return INET_QueryOption(option
, buffer
, size
, unicode
);
1591 static DWORD
HTTPREQ_SetOption(WININETHANDLEHEADER
*hdr
, DWORD option
, void *buffer
, DWORD size
)
1593 WININETHTTPREQW
*req
= (WININETHTTPREQW
*)hdr
;
1596 case INTERNET_OPTION_SEND_TIMEOUT
:
1597 case INTERNET_OPTION_RECEIVE_TIMEOUT
:
1598 TRACE("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT\n");
1600 if (size
!= sizeof(DWORD
))
1601 return ERROR_INVALID_PARAMETER
;
1603 return NETCON_set_timeout(&req
->netConnection
, option
== INTERNET_OPTION_SEND_TIMEOUT
,
1606 case INTERNET_OPTION_USERNAME
:
1607 HeapFree(GetProcessHeap(), 0, req
->lpHttpSession
->lpszUserName
);
1608 if (!(req
->lpHttpSession
->lpszUserName
= WININET_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
1609 return ERROR_SUCCESS
;
1611 case INTERNET_OPTION_PASSWORD
:
1612 HeapFree(GetProcessHeap(), 0, req
->lpHttpSession
->lpszPassword
);
1613 if (!(req
->lpHttpSession
->lpszPassword
= WININET_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
1614 return ERROR_SUCCESS
;
1617 return ERROR_INTERNET_INVALID_OPTION
;
1620 /* read some more data into the read buffer */
1621 static BOOL
read_more_data( WININETHTTPREQW
*req
, int maxlen
)
1625 if (req
->read_size
&& req
->read_pos
)
1627 /* move existing data to the start of the buffer */
1628 memmove( req
->read_buf
, req
->read_buf
+ req
->read_pos
, req
->read_size
);
1632 if (maxlen
== -1) maxlen
= sizeof(req
->read_buf
);
1633 if (!NETCON_recv( &req
->netConnection
, req
->read_buf
+ req
->read_size
,
1634 maxlen
- req
->read_size
, 0, &len
)) return FALSE
;
1635 req
->read_size
+= len
;
1639 /* remove some amount of data from the read buffer */
1640 static void remove_data( WININETHTTPREQW
*req
, int count
)
1642 if (!(req
->read_size
-= count
)) req
->read_pos
= 0;
1643 else req
->read_pos
+= count
;
1646 static BOOL
read_line( WININETHTTPREQW
*req
, LPSTR buffer
, DWORD
*len
)
1648 int count
, bytes_read
, pos
= 0;
1652 char *eol
= memchr( req
->read_buf
+ req
->read_pos
, '\n', req
->read_size
);
1656 count
= eol
- (req
->read_buf
+ req
->read_pos
);
1657 bytes_read
= count
+ 1;
1659 else count
= bytes_read
= req
->read_size
;
1661 count
= min( count
, *len
- pos
);
1662 memcpy( buffer
+ pos
, req
->read_buf
+ req
->read_pos
, count
);
1664 remove_data( req
, bytes_read
);
1667 if (!read_more_data( req
, -1 )) return FALSE
;
1668 if (!req
->read_size
)
1671 TRACE( "returning empty string\n" );
1678 if (pos
&& buffer
[pos
- 1] == '\r') pos
--;
1681 buffer
[*len
- 1] = 0;
1682 TRACE( "returning %s\n", debugstr_a(buffer
));
1686 /* discard data contents until we reach end of line */
1687 static BOOL
discard_eol( WININETHTTPREQW
*req
)
1691 char *eol
= memchr( req
->read_buf
+ req
->read_pos
, '\n', req
->read_size
);
1694 remove_data( req
, (eol
+ 1) - (req
->read_buf
+ req
->read_pos
) );
1697 req
->read_pos
= req
->read_size
= 0; /* discard everything */
1698 if (!read_more_data( req
, -1 )) return FALSE
;
1699 } while (req
->read_size
);
1703 /* read the size of the next chunk */
1704 static BOOL
start_next_chunk( WININETHTTPREQW
*req
)
1706 DWORD chunk_size
= 0;
1708 if (!req
->dwContentLength
) return TRUE
;
1709 if (req
->dwContentLength
== req
->dwContentRead
)
1711 /* read terminator for the previous chunk */
1712 if (!discard_eol( req
)) return FALSE
;
1713 req
->dwContentLength
= ~0u;
1714 req
->dwContentRead
= 0;
1718 while (req
->read_size
)
1720 char ch
= req
->read_buf
[req
->read_pos
];
1721 if (ch
>= '0' && ch
<= '9') chunk_size
= chunk_size
* 16 + ch
- '0';
1722 else if (ch
>= 'a' && ch
<= 'f') chunk_size
= chunk_size
* 16 + ch
- 'a' + 10;
1723 else if (ch
>= 'A' && ch
<= 'F') chunk_size
= chunk_size
* 16 + ch
- 'A' + 10;
1724 else if (ch
== ';' || ch
== '\r' || ch
== '\n')
1726 TRACE( "reading %u byte chunk\n", chunk_size
);
1727 req
->dwContentLength
= chunk_size
;
1728 req
->dwContentRead
= 0;
1729 if (!discard_eol( req
)) return FALSE
;
1732 remove_data( req
, 1 );
1734 if (!read_more_data( req
, -1 )) return FALSE
;
1735 if (!req
->read_size
)
1737 req
->dwContentLength
= req
->dwContentRead
= 0;
1743 /* return the size of data available to be read immediately */
1744 static DWORD
get_avail_data( WININETHTTPREQW
*req
)
1746 if (req
->read_chunked
&& (req
->dwContentLength
== ~0u || req
->dwContentLength
== req
->dwContentRead
))
1748 return min( req
->read_size
, req
->dwContentLength
- req
->dwContentRead
);
1751 /* check if we have reached the end of the data to read */
1752 static BOOL
end_of_read_data( WININETHTTPREQW
*req
)
1754 if (req
->read_chunked
) return (req
->dwContentLength
== 0);
1755 if (req
->dwContentLength
== ~0u) return FALSE
;
1756 return (req
->dwContentLength
== req
->dwContentRead
);
1759 static BOOL
refill_buffer( WININETHTTPREQW
*req
)
1761 int len
= sizeof(req
->read_buf
);
1763 if (req
->read_chunked
&& (req
->dwContentLength
== ~0u || req
->dwContentLength
== req
->dwContentRead
))
1765 if (!start_next_chunk( req
)) return FALSE
;
1768 if (req
->dwContentLength
!= ~0u) len
= min( len
, req
->dwContentLength
- req
->dwContentRead
);
1769 if (len
<= req
->read_size
) return TRUE
;
1771 if (!read_more_data( req
, len
)) return FALSE
;
1772 if (!req
->read_size
) req
->dwContentLength
= req
->dwContentRead
= 0;
1776 static void HTTP_ReceiveRequestData(WININETHTTPREQW
*req
, BOOL first_notif
)
1778 INTERNET_ASYNC_RESULT iar
;
1782 if (refill_buffer( req
)) {
1783 iar
.dwResult
= (DWORD_PTR
)req
->hdr
.hInternet
;
1784 iar
.dwError
= first_notif
? 0 : get_avail_data(req
);
1787 iar
.dwError
= INTERNET_GetLastError();
1790 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
1791 sizeof(INTERNET_ASYNC_RESULT
));
1794 static DWORD
HTTPREQ_Read(WININETHTTPREQW
*req
, void *buffer
, DWORD size
, DWORD
*read
, BOOL sync
)
1796 int len
, bytes_read
= 0;
1798 if (req
->read_chunked
&& (req
->dwContentLength
== ~0u || req
->dwContentLength
== req
->dwContentRead
))
1800 if (!start_next_chunk( req
)) goto done
;
1802 if (req
->dwContentLength
!= ~0u) size
= min( size
, req
->dwContentLength
- req
->dwContentRead
);
1806 bytes_read
= min( req
->read_size
, size
);
1807 memcpy( buffer
, req
->read_buf
+ req
->read_pos
, bytes_read
);
1808 remove_data( req
, bytes_read
);
1811 if (size
> bytes_read
&& (!bytes_read
|| sync
))
1813 if (NETCON_recv( &req
->netConnection
, (char *)buffer
+ bytes_read
, size
- bytes_read
,
1814 sync
? MSG_WAITALL
: 0, &len
))
1816 /* always return success, even if the network layer returns an error */
1819 req
->dwContentRead
+= bytes_read
;
1822 TRACE( "retrieved %u bytes (%u/%u)\n", bytes_read
, req
->dwContentRead
, req
->dwContentLength
);
1823 if(req
->lpszCacheFile
) {
1825 DWORD dwBytesWritten
;
1827 res
= WriteFile(req
->hCacheFile
, buffer
, bytes_read
, &dwBytesWritten
, NULL
);
1829 WARN("WriteFile failed: %u\n", GetLastError());
1832 if(!bytes_read
&& (req
->dwContentRead
== req
->dwContentLength
))
1833 HTTP_FinishedReading(req
);
1835 return ERROR_SUCCESS
;
1839 static DWORD
HTTPREQ_ReadFile(WININETHANDLEHEADER
*hdr
, void *buffer
, DWORD size
, DWORD
*read
)
1841 WININETHTTPREQW
*req
= (WININETHTTPREQW
*)hdr
;
1842 return HTTPREQ_Read(req
, buffer
, size
, read
, TRUE
);
1845 static void HTTPREQ_AsyncReadFileExAProc(WORKREQUEST
*workRequest
)
1847 struct WORKREQ_INTERNETREADFILEEXA
const *data
= &workRequest
->u
.InternetReadFileExA
;
1848 WININETHTTPREQW
*req
= (WININETHTTPREQW
*)workRequest
->hdr
;
1849 INTERNET_ASYNC_RESULT iar
;
1852 TRACE("INTERNETREADFILEEXA %p\n", workRequest
->hdr
);
1854 res
= HTTPREQ_Read(req
, data
->lpBuffersOut
->lpvBuffer
,
1855 data
->lpBuffersOut
->dwBufferLength
, &data
->lpBuffersOut
->dwBufferLength
, TRUE
);
1857 iar
.dwResult
= res
== ERROR_SUCCESS
;
1860 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
,
1861 INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
1862 sizeof(INTERNET_ASYNC_RESULT
));
1865 static DWORD
HTTPREQ_ReadFileExA(WININETHANDLEHEADER
*hdr
, INTERNET_BUFFERSA
*buffers
,
1866 DWORD flags
, DWORD_PTR context
)
1869 WININETHTTPREQW
*req
= (WININETHTTPREQW
*)hdr
;
1872 if (flags
& ~(IRF_ASYNC
|IRF_NO_WAIT
))
1873 FIXME("these dwFlags aren't implemented: 0x%x\n", flags
& ~(IRF_ASYNC
|IRF_NO_WAIT
));
1875 if (buffers
->dwStructSize
!= sizeof(*buffers
))
1876 return ERROR_INVALID_PARAMETER
;
1878 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
1880 if ((hdr
->dwFlags
& INTERNET_FLAG_ASYNC
) && !get_avail_data(req
))
1882 WORKREQUEST workRequest
;
1884 workRequest
.asyncproc
= HTTPREQ_AsyncReadFileExAProc
;
1885 workRequest
.hdr
= WININET_AddRef(&req
->hdr
);
1886 workRequest
.u
.InternetReadFileExA
.lpBuffersOut
= buffers
;
1888 INTERNET_AsyncCall(&workRequest
);
1890 return ERROR_IO_PENDING
;
1893 res
= HTTPREQ_Read(req
, buffers
->lpvBuffer
, buffers
->dwBufferLength
, &buffers
->dwBufferLength
,
1894 !(flags
& IRF_NO_WAIT
));
1896 if (res
== ERROR_SUCCESS
) {
1897 DWORD size
= buffers
->dwBufferLength
;
1898 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
,
1899 &size
, sizeof(size
));
1905 static void HTTPREQ_AsyncReadFileExWProc(WORKREQUEST
*workRequest
)
1907 struct WORKREQ_INTERNETREADFILEEXW
const *data
= &workRequest
->u
.InternetReadFileExW
;
1908 WININETHTTPREQW
*req
= (WININETHTTPREQW
*)workRequest
->hdr
;
1909 INTERNET_ASYNC_RESULT iar
;
1912 TRACE("INTERNETREADFILEEXW %p\n", workRequest
->hdr
);
1914 res
= HTTPREQ_Read(req
, data
->lpBuffersOut
->lpvBuffer
,
1915 data
->lpBuffersOut
->dwBufferLength
, &data
->lpBuffersOut
->dwBufferLength
, TRUE
);
1917 iar
.dwResult
= res
== ERROR_SUCCESS
;
1920 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
,
1921 INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
1922 sizeof(INTERNET_ASYNC_RESULT
));
1925 static DWORD
HTTPREQ_ReadFileExW(WININETHANDLEHEADER
*hdr
, INTERNET_BUFFERSW
*buffers
,
1926 DWORD flags
, DWORD_PTR context
)
1929 WININETHTTPREQW
*req
= (WININETHTTPREQW
*)hdr
;
1932 if (flags
& ~(IRF_ASYNC
|IRF_NO_WAIT
))
1933 FIXME("these dwFlags aren't implemented: 0x%x\n", flags
& ~(IRF_ASYNC
|IRF_NO_WAIT
));
1935 if (buffers
->dwStructSize
!= sizeof(*buffers
))
1936 return ERROR_INVALID_PARAMETER
;
1938 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
1940 if ((hdr
->dwFlags
& INTERNET_FLAG_ASYNC
) && !get_avail_data(req
))
1942 WORKREQUEST workRequest
;
1944 workRequest
.asyncproc
= HTTPREQ_AsyncReadFileExWProc
;
1945 workRequest
.hdr
= WININET_AddRef(&req
->hdr
);
1946 workRequest
.u
.InternetReadFileExW
.lpBuffersOut
= buffers
;
1948 INTERNET_AsyncCall(&workRequest
);
1950 return ERROR_IO_PENDING
;
1953 res
= HTTPREQ_Read(req
, buffers
->lpvBuffer
, buffers
->dwBufferLength
, &buffers
->dwBufferLength
,
1954 !(flags
& IRF_NO_WAIT
));
1956 if (res
== ERROR_SUCCESS
) {
1957 DWORD size
= buffers
->dwBufferLength
;
1958 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
,
1959 &size
, sizeof(size
));
1965 static BOOL
HTTPREQ_WriteFile(WININETHANDLEHEADER
*hdr
, const void *buffer
, DWORD size
, DWORD
*written
)
1968 LPWININETHTTPREQW lpwhr
= (LPWININETHTTPREQW
)hdr
;
1970 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
1973 if ((ret
= NETCON_send(&lpwhr
->netConnection
, buffer
, size
, 0, (LPINT
)written
)))
1974 lpwhr
->dwBytesWritten
+= *written
;
1976 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_SENT
, written
, sizeof(DWORD
));
1980 static void HTTPREQ_AsyncQueryDataAvailableProc(WORKREQUEST
*workRequest
)
1982 WININETHTTPREQW
*req
= (WININETHTTPREQW
*)workRequest
->hdr
;
1984 HTTP_ReceiveRequestData(req
, FALSE
);
1987 static DWORD
HTTPREQ_QueryDataAvailable(WININETHANDLEHEADER
*hdr
, DWORD
*available
, DWORD flags
, DWORD_PTR ctx
)
1989 WININETHTTPREQW
*req
= (WININETHTTPREQW
*)hdr
;
1991 TRACE("(%p %p %x %lx)\n", req
, available
, flags
, ctx
);
1993 if (!(*available
= get_avail_data( req
)))
1995 if (end_of_read_data( req
)) return ERROR_SUCCESS
;
1997 if (req
->lpHttpSession
->lpAppInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1999 WORKREQUEST workRequest
;
2001 workRequest
.asyncproc
= HTTPREQ_AsyncQueryDataAvailableProc
;
2002 workRequest
.hdr
= WININET_AddRef( &req
->hdr
);
2004 INTERNET_AsyncCall(&workRequest
);
2006 return ERROR_IO_PENDING
;
2010 refill_buffer( req
);
2011 *available
= get_avail_data( req
);
2015 if (*available
== sizeof(req
->read_buf
)) /* check if we have even more pending in the socket */
2018 if (NETCON_query_data_available(&req
->netConnection
, &extra
))
2019 *available
= min( *available
+ extra
, req
->dwContentLength
- req
->dwContentRead
);
2022 TRACE( "returning %u\n", *available
);
2023 return ERROR_SUCCESS
;
2026 static const HANDLEHEADERVtbl HTTPREQVtbl
= {
2028 HTTPREQ_CloseConnection
,
2029 HTTPREQ_QueryOption
,
2032 HTTPREQ_ReadFileExA
,
2033 HTTPREQ_ReadFileExW
,
2035 HTTPREQ_QueryDataAvailable
,
2039 /***********************************************************************
2040 * HTTP_HttpOpenRequestW (internal)
2042 * Open a HTTP request handle
2045 * HINTERNET a HTTP request handle on success
2049 HINTERNET WINAPI
HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs
,
2050 LPCWSTR lpszVerb
, LPCWSTR lpszObjectName
, LPCWSTR lpszVersion
,
2051 LPCWSTR lpszReferrer
, LPCWSTR
*lpszAcceptTypes
,
2052 DWORD dwFlags
, DWORD_PTR dwContext
)
2054 LPWININETAPPINFOW hIC
= NULL
;
2055 LPWININETHTTPREQW lpwhr
;
2056 LPWSTR lpszHostName
= NULL
;
2057 HINTERNET handle
= NULL
;
2058 static const WCHAR szHostForm
[] = {'%','s',':','%','u',0};
2063 assert( lpwhs
->hdr
.htype
== WH_HHTTPSESSION
);
2064 hIC
= lpwhs
->lpAppInfo
;
2066 lpwhr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WININETHTTPREQW
));
2069 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2072 lpwhr
->hdr
.htype
= WH_HHTTPREQ
;
2073 lpwhr
->hdr
.vtbl
= &HTTPREQVtbl
;
2074 lpwhr
->hdr
.dwFlags
= dwFlags
;
2075 lpwhr
->hdr
.dwContext
= dwContext
;
2076 lpwhr
->hdr
.refs
= 1;
2077 lpwhr
->hdr
.lpfnStatusCB
= lpwhs
->hdr
.lpfnStatusCB
;
2078 lpwhr
->hdr
.dwInternalFlags
= lpwhs
->hdr
.dwInternalFlags
& INET_CALLBACKW
;
2079 lpwhr
->dwContentLength
= ~0u;
2081 WININET_AddRef( &lpwhs
->hdr
);
2082 lpwhr
->lpHttpSession
= lpwhs
;
2083 list_add_head( &lpwhs
->hdr
.children
, &lpwhr
->hdr
.entry
);
2085 lpszHostName
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) *
2086 (strlenW(lpwhs
->lpszHostName
) + 7 /* length of ":65535" + 1 */));
2087 if (NULL
== lpszHostName
)
2089 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2093 handle
= WININET_AllocHandle( &lpwhr
->hdr
);
2096 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2100 if (!NETCON_init(&lpwhr
->netConnection
, dwFlags
& INTERNET_FLAG_SECURE
))
2102 InternetCloseHandle( handle
);
2107 if (lpszObjectName
&& *lpszObjectName
) {
2111 rc
= UrlEscapeW(lpszObjectName
, NULL
, &len
, URL_ESCAPE_SPACES_ONLY
);
2112 if (rc
!= E_POINTER
)
2113 len
= strlenW(lpszObjectName
)+1;
2114 lpwhr
->lpszPath
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
2115 rc
= UrlEscapeW(lpszObjectName
, lpwhr
->lpszPath
, &len
,
2116 URL_ESCAPE_SPACES_ONLY
);
2119 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName
),rc
);
2120 strcpyW(lpwhr
->lpszPath
,lpszObjectName
);
2124 if (lpszReferrer
&& *lpszReferrer
)
2125 HTTP_ProcessHeader(lpwhr
, HTTP_REFERER
, lpszReferrer
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REQ
);
2127 if (lpszAcceptTypes
)
2130 for (i
= 0; lpszAcceptTypes
[i
]; i
++)
2132 if (!*lpszAcceptTypes
[i
]) continue;
2133 HTTP_ProcessHeader(lpwhr
, HTTP_ACCEPT
, lpszAcceptTypes
[i
],
2134 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
|
2135 HTTP_ADDHDR_FLAG_REQ
|
2136 (i
== 0 ? HTTP_ADDHDR_FLAG_REPLACE
: 0));
2140 lpwhr
->lpszVerb
= WININET_strdupW(lpszVerb
&& *lpszVerb
? lpszVerb
: szGET
);
2143 lpwhr
->lpszVersion
= WININET_strdupW(lpszVersion
);
2145 lpwhr
->lpszVersion
= WININET_strdupW(g_szHttp1_1
);
2147 if (lpwhs
->nHostPort
!= INTERNET_INVALID_PORT_NUMBER
&&
2148 lpwhs
->nHostPort
!= INTERNET_DEFAULT_HTTP_PORT
&&
2149 lpwhs
->nHostPort
!= INTERNET_DEFAULT_HTTPS_PORT
)
2151 sprintfW(lpszHostName
, szHostForm
, lpwhs
->lpszHostName
, lpwhs
->nHostPort
);
2152 HTTP_ProcessHeader(lpwhr
, szHost
, lpszHostName
,
2153 HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REQ
);
2156 HTTP_ProcessHeader(lpwhr
, szHost
, lpwhs
->lpszHostName
,
2157 HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REQ
);
2159 if (lpwhs
->nServerPort
== INTERNET_INVALID_PORT_NUMBER
)
2160 lpwhs
->nServerPort
= (dwFlags
& INTERNET_FLAG_SECURE
?
2161 INTERNET_DEFAULT_HTTPS_PORT
:
2162 INTERNET_DEFAULT_HTTP_PORT
);
2164 if (lpwhs
->nHostPort
== INTERNET_INVALID_PORT_NUMBER
)
2165 lpwhs
->nHostPort
= (dwFlags
& INTERNET_FLAG_SECURE
?
2166 INTERNET_DEFAULT_HTTPS_PORT
:
2167 INTERNET_DEFAULT_HTTP_PORT
);
2169 if (NULL
!= hIC
->lpszProxy
&& hIC
->lpszProxy
[0] != 0)
2170 HTTP_DealWithProxy( hIC
, lpwhs
, lpwhr
);
2172 INTERNET_SendCallback(&lpwhs
->hdr
, dwContext
,
2173 INTERNET_STATUS_HANDLE_CREATED
, &handle
,
2177 HeapFree(GetProcessHeap(), 0, lpszHostName
);
2179 WININET_Release( &lpwhr
->hdr
);
2181 TRACE("<-- %p (%p)\n", handle
, lpwhr
);
2185 /* read any content returned by the server so that the connection can be
2187 static void HTTP_DrainContent(WININETHTTPREQW
*req
)
2191 if (!NETCON_connected(&req
->netConnection
)) return;
2193 if (req
->dwContentLength
== -1)
2195 NETCON_close(&req
->netConnection
);
2202 if (HTTPREQ_Read(req
, buffer
, sizeof(buffer
), &bytes_read
, TRUE
) != ERROR_SUCCESS
)
2204 } while (bytes_read
);
2207 static const WCHAR szAccept
[] = { 'A','c','c','e','p','t',0 };
2208 static const WCHAR szAccept_Charset
[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
2209 static const WCHAR szAccept_Encoding
[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
2210 static const WCHAR szAccept_Language
[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
2211 static const WCHAR szAccept_Ranges
[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
2212 static const WCHAR szAge
[] = { 'A','g','e',0 };
2213 static const WCHAR szAllow
[] = { 'A','l','l','o','w',0 };
2214 static const WCHAR szCache_Control
[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
2215 static const WCHAR szConnection
[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
2216 static const WCHAR szContent_Base
[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
2217 static const WCHAR szContent_Encoding
[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
2218 static const WCHAR szContent_ID
[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
2219 static const WCHAR szContent_Language
[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
2220 static const WCHAR szContent_Length
[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
2221 static const WCHAR szContent_Location
[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
2222 static const WCHAR szContent_MD5
[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
2223 static const WCHAR szContent_Range
[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
2224 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 };
2225 static const WCHAR szContent_Type
[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
2226 static const WCHAR szCookie
[] = { 'C','o','o','k','i','e',0 };
2227 static const WCHAR szDate
[] = { 'D','a','t','e',0 };
2228 static const WCHAR szFrom
[] = { 'F','r','o','m',0 };
2229 static const WCHAR szETag
[] = { 'E','T','a','g',0 };
2230 static const WCHAR szExpect
[] = { 'E','x','p','e','c','t',0 };
2231 static const WCHAR szExpires
[] = { 'E','x','p','i','r','e','s',0 };
2232 static const WCHAR szIf_Match
[] = { 'I','f','-','M','a','t','c','h',0 };
2233 static const WCHAR szIf_Modified_Since
[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
2234 static const WCHAR szIf_None_Match
[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
2235 static const WCHAR szIf_Range
[] = { 'I','f','-','R','a','n','g','e',0 };
2236 static const WCHAR szIf_Unmodified_Since
[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
2237 static const WCHAR szLast_Modified
[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
2238 static const WCHAR szLocation
[] = { 'L','o','c','a','t','i','o','n',0 };
2239 static const WCHAR szMax_Forwards
[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
2240 static const WCHAR szMime_Version
[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
2241 static const WCHAR szPragma
[] = { 'P','r','a','g','m','a',0 };
2242 static const WCHAR szProxy_Authenticate
[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
2243 static const WCHAR szProxy_Connection
[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
2244 static const WCHAR szPublic
[] = { 'P','u','b','l','i','c',0 };
2245 static const WCHAR szRange
[] = { 'R','a','n','g','e',0 };
2246 static const WCHAR szReferer
[] = { 'R','e','f','e','r','e','r',0 };
2247 static const WCHAR szRetry_After
[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
2248 static const WCHAR szServer
[] = { 'S','e','r','v','e','r',0 };
2249 static const WCHAR szSet_Cookie
[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
2250 static const WCHAR szTransfer_Encoding
[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
2251 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 };
2252 static const WCHAR szUpgrade
[] = { 'U','p','g','r','a','d','e',0 };
2253 static const WCHAR szURI
[] = { 'U','R','I',0 };
2254 static const WCHAR szUser_Agent
[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
2255 static const WCHAR szVary
[] = { 'V','a','r','y',0 };
2256 static const WCHAR szVia
[] = { 'V','i','a',0 };
2257 static const WCHAR szWarning
[] = { 'W','a','r','n','i','n','g',0 };
2258 static const WCHAR szWWW_Authenticate
[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
2260 static const LPCWSTR header_lookup
[] = {
2261 szMime_Version
, /* HTTP_QUERY_MIME_VERSION = 0 */
2262 szContent_Type
, /* HTTP_QUERY_CONTENT_TYPE = 1 */
2263 szContent_Transfer_Encoding
,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
2264 szContent_ID
, /* HTTP_QUERY_CONTENT_ID = 3 */
2265 NULL
, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
2266 szContent_Length
, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
2267 szContent_Language
, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
2268 szAllow
, /* HTTP_QUERY_ALLOW = 7 */
2269 szPublic
, /* HTTP_QUERY_PUBLIC = 8 */
2270 szDate
, /* HTTP_QUERY_DATE = 9 */
2271 szExpires
, /* HTTP_QUERY_EXPIRES = 10 */
2272 szLast_Modified
, /* HTTP_QUERY_LAST_MODIFIED = 11 */
2273 NULL
, /* HTTP_QUERY_MESSAGE_ID = 12 */
2274 szURI
, /* HTTP_QUERY_URI = 13 */
2275 szFrom
, /* HTTP_QUERY_DERIVED_FROM = 14 */
2276 NULL
, /* HTTP_QUERY_COST = 15 */
2277 NULL
, /* HTTP_QUERY_LINK = 16 */
2278 szPragma
, /* HTTP_QUERY_PRAGMA = 17 */
2279 NULL
, /* HTTP_QUERY_VERSION = 18 */
2280 szStatus
, /* HTTP_QUERY_STATUS_CODE = 19 */
2281 NULL
, /* HTTP_QUERY_STATUS_TEXT = 20 */
2282 NULL
, /* HTTP_QUERY_RAW_HEADERS = 21 */
2283 NULL
, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
2284 szConnection
, /* HTTP_QUERY_CONNECTION = 23 */
2285 szAccept
, /* HTTP_QUERY_ACCEPT = 24 */
2286 szAccept_Charset
, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
2287 szAccept_Encoding
, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
2288 szAccept_Language
, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
2289 szAuthorization
, /* HTTP_QUERY_AUTHORIZATION = 28 */
2290 szContent_Encoding
, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
2291 NULL
, /* HTTP_QUERY_FORWARDED = 30 */
2292 NULL
, /* HTTP_QUERY_FROM = 31 */
2293 szIf_Modified_Since
, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
2294 szLocation
, /* HTTP_QUERY_LOCATION = 33 */
2295 NULL
, /* HTTP_QUERY_ORIG_URI = 34 */
2296 szReferer
, /* HTTP_QUERY_REFERER = 35 */
2297 szRetry_After
, /* HTTP_QUERY_RETRY_AFTER = 36 */
2298 szServer
, /* HTTP_QUERY_SERVER = 37 */
2299 NULL
, /* HTTP_TITLE = 38 */
2300 szUser_Agent
, /* HTTP_QUERY_USER_AGENT = 39 */
2301 szWWW_Authenticate
, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
2302 szProxy_Authenticate
, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
2303 szAccept_Ranges
, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
2304 szSet_Cookie
, /* HTTP_QUERY_SET_COOKIE = 43 */
2305 szCookie
, /* HTTP_QUERY_COOKIE = 44 */
2306 NULL
, /* HTTP_QUERY_REQUEST_METHOD = 45 */
2307 NULL
, /* HTTP_QUERY_REFRESH = 46 */
2308 NULL
, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
2309 szAge
, /* HTTP_QUERY_AGE = 48 */
2310 szCache_Control
, /* HTTP_QUERY_CACHE_CONTROL = 49 */
2311 szContent_Base
, /* HTTP_QUERY_CONTENT_BASE = 50 */
2312 szContent_Location
, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
2313 szContent_MD5
, /* HTTP_QUERY_CONTENT_MD5 = 52 */
2314 szContent_Range
, /* HTTP_QUERY_CONTENT_RANGE = 53 */
2315 szETag
, /* HTTP_QUERY_ETAG = 54 */
2316 szHost
, /* HTTP_QUERY_HOST = 55 */
2317 szIf_Match
, /* HTTP_QUERY_IF_MATCH = 56 */
2318 szIf_None_Match
, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
2319 szIf_Range
, /* HTTP_QUERY_IF_RANGE = 58 */
2320 szIf_Unmodified_Since
, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
2321 szMax_Forwards
, /* HTTP_QUERY_MAX_FORWARDS = 60 */
2322 szProxy_Authorization
, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
2323 szRange
, /* HTTP_QUERY_RANGE = 62 */
2324 szTransfer_Encoding
, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
2325 szUpgrade
, /* HTTP_QUERY_UPGRADE = 64 */
2326 szVary
, /* HTTP_QUERY_VARY = 65 */
2327 szVia
, /* HTTP_QUERY_VIA = 66 */
2328 szWarning
, /* HTTP_QUERY_WARNING = 67 */
2329 szExpect
, /* HTTP_QUERY_EXPECT = 68 */
2330 szProxy_Connection
, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
2331 szUnless_Modified_Since
, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
2334 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
2336 /***********************************************************************
2337 * HTTP_HttpQueryInfoW (internal)
2339 static BOOL
HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr
, DWORD dwInfoLevel
,
2340 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
2342 LPHTTPHEADERW lphttpHdr
= NULL
;
2343 BOOL bSuccess
= FALSE
;
2344 BOOL request_only
= dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
;
2345 INT requested_index
= lpdwIndex
? *lpdwIndex
: 0;
2346 DWORD level
= (dwInfoLevel
& ~HTTP_QUERY_MODIFIER_FLAGS_MASK
);
2349 /* Find requested header structure */
2352 case HTTP_QUERY_CUSTOM
:
2353 if (!lpBuffer
) return FALSE
;
2354 index
= HTTP_GetCustomHeaderIndex(lpwhr
, lpBuffer
, requested_index
, request_only
);
2357 case HTTP_QUERY_RAW_HEADERS_CRLF
:
2364 headers
= HTTP_BuildHeaderRequestString(lpwhr
, lpwhr
->lpszVerb
, lpwhr
->lpszPath
, lpwhr
->lpszVersion
);
2366 headers
= lpwhr
->lpszRawHeaders
;
2369 len
= strlenW(headers
) * sizeof(WCHAR
);
2371 if (len
+ sizeof(WCHAR
) > *lpdwBufferLength
)
2373 len
+= sizeof(WCHAR
);
2374 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2380 memcpy(lpBuffer
, headers
, len
+ sizeof(WCHAR
));
2383 len
= strlenW(szCrLf
) * sizeof(WCHAR
);
2384 memcpy(lpBuffer
, szCrLf
, sizeof(szCrLf
));
2386 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
/ sizeof(WCHAR
)));
2389 *lpdwBufferLength
= len
;
2392 HeapFree(GetProcessHeap(), 0, headers
);
2395 case HTTP_QUERY_RAW_HEADERS
:
2397 LPWSTR
* ppszRawHeaderLines
= HTTP_Tokenize(lpwhr
->lpszRawHeaders
, szCrLf
);
2399 LPWSTR pszString
= lpBuffer
;
2401 for (i
= 0; ppszRawHeaderLines
[i
]; i
++)
2402 size
+= strlenW(ppszRawHeaderLines
[i
]) + 1;
2404 if (size
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
2406 HTTP_FreeTokens(ppszRawHeaderLines
);
2407 *lpdwBufferLength
= (size
+ 1) * sizeof(WCHAR
);
2408 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2413 for (i
= 0; ppszRawHeaderLines
[i
]; i
++)
2415 DWORD len
= strlenW(ppszRawHeaderLines
[i
]);
2416 memcpy(pszString
, ppszRawHeaderLines
[i
], (len
+1)*sizeof(WCHAR
));
2420 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, size
));
2422 *lpdwBufferLength
= size
* sizeof(WCHAR
);
2423 HTTP_FreeTokens(ppszRawHeaderLines
);
2427 case HTTP_QUERY_STATUS_TEXT
:
2428 if (lpwhr
->lpszStatusText
)
2430 DWORD len
= strlenW(lpwhr
->lpszStatusText
);
2431 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
2433 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
2434 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2439 memcpy(lpBuffer
, lpwhr
->lpszStatusText
, (len
+ 1) * sizeof(WCHAR
));
2440 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
));
2442 *lpdwBufferLength
= len
* sizeof(WCHAR
);
2446 case HTTP_QUERY_VERSION
:
2447 if (lpwhr
->lpszVersion
)
2449 DWORD len
= strlenW(lpwhr
->lpszVersion
);
2450 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
2452 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
2453 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2458 memcpy(lpBuffer
, lpwhr
->lpszVersion
, (len
+ 1) * sizeof(WCHAR
));
2459 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
));
2461 *lpdwBufferLength
= len
* sizeof(WCHAR
);
2466 assert (LAST_TABLE_HEADER
== (HTTP_QUERY_UNLESS_MODIFIED_SINCE
+ 1));
2468 if (level
< LAST_TABLE_HEADER
&& header_lookup
[level
])
2469 index
= HTTP_GetCustomHeaderIndex(lpwhr
, header_lookup
[level
],
2470 requested_index
,request_only
);
2474 lphttpHdr
= &lpwhr
->pCustHeaders
[index
];
2476 /* Ensure header satisfies requested attributes */
2478 ((dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
) &&
2479 (~lphttpHdr
->wFlags
& HDR_ISREQUEST
)))
2481 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND
);
2485 if (lpdwIndex
&& level
!= HTTP_QUERY_STATUS_CODE
) (*lpdwIndex
)++;
2487 /* coalesce value to requested type */
2488 if (dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
&& lpBuffer
)
2490 *(int *)lpBuffer
= atoiW(lphttpHdr
->lpszValue
);
2491 TRACE(" returning number: %d\n", *(int *)lpBuffer
);
2494 else if (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
&& lpBuffer
)
2500 tmpTime
= ConvertTimeString(lphttpHdr
->lpszValue
);
2502 tmpTM
= *gmtime(&tmpTime
);
2503 STHook
= (SYSTEMTIME
*)lpBuffer
;
2504 STHook
->wDay
= tmpTM
.tm_mday
;
2505 STHook
->wHour
= tmpTM
.tm_hour
;
2506 STHook
->wMilliseconds
= 0;
2507 STHook
->wMinute
= tmpTM
.tm_min
;
2508 STHook
->wDayOfWeek
= tmpTM
.tm_wday
;
2509 STHook
->wMonth
= tmpTM
.tm_mon
+ 1;
2510 STHook
->wSecond
= tmpTM
.tm_sec
;
2511 STHook
->wYear
= tmpTM
.tm_year
;
2514 TRACE(" returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
2515 STHook
->wYear
, STHook
->wMonth
, STHook
->wDay
, STHook
->wDayOfWeek
,
2516 STHook
->wHour
, STHook
->wMinute
, STHook
->wSecond
, STHook
->wMilliseconds
);
2518 else if (lphttpHdr
->lpszValue
)
2520 DWORD len
= (strlenW(lphttpHdr
->lpszValue
) + 1) * sizeof(WCHAR
);
2522 if (len
> *lpdwBufferLength
)
2524 *lpdwBufferLength
= len
;
2525 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2530 memcpy(lpBuffer
, lphttpHdr
->lpszValue
, len
);
2531 TRACE(" returning string: %s\n", debugstr_w(lpBuffer
));
2533 *lpdwBufferLength
= len
- sizeof(WCHAR
);
2539 /***********************************************************************
2540 * HttpQueryInfoW (WININET.@)
2542 * Queries for information about an HTTP request
2549 BOOL WINAPI
HttpQueryInfoW(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
2550 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
2552 BOOL bSuccess
= FALSE
;
2553 LPWININETHTTPREQW lpwhr
;
2555 if (TRACE_ON(wininet
)) {
2556 #define FE(x) { x, #x }
2557 static const wininet_flag_info query_flags
[] = {
2558 FE(HTTP_QUERY_MIME_VERSION
),
2559 FE(HTTP_QUERY_CONTENT_TYPE
),
2560 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING
),
2561 FE(HTTP_QUERY_CONTENT_ID
),
2562 FE(HTTP_QUERY_CONTENT_DESCRIPTION
),
2563 FE(HTTP_QUERY_CONTENT_LENGTH
),
2564 FE(HTTP_QUERY_CONTENT_LANGUAGE
),
2565 FE(HTTP_QUERY_ALLOW
),
2566 FE(HTTP_QUERY_PUBLIC
),
2567 FE(HTTP_QUERY_DATE
),
2568 FE(HTTP_QUERY_EXPIRES
),
2569 FE(HTTP_QUERY_LAST_MODIFIED
),
2570 FE(HTTP_QUERY_MESSAGE_ID
),
2572 FE(HTTP_QUERY_DERIVED_FROM
),
2573 FE(HTTP_QUERY_COST
),
2574 FE(HTTP_QUERY_LINK
),
2575 FE(HTTP_QUERY_PRAGMA
),
2576 FE(HTTP_QUERY_VERSION
),
2577 FE(HTTP_QUERY_STATUS_CODE
),
2578 FE(HTTP_QUERY_STATUS_TEXT
),
2579 FE(HTTP_QUERY_RAW_HEADERS
),
2580 FE(HTTP_QUERY_RAW_HEADERS_CRLF
),
2581 FE(HTTP_QUERY_CONNECTION
),
2582 FE(HTTP_QUERY_ACCEPT
),
2583 FE(HTTP_QUERY_ACCEPT_CHARSET
),
2584 FE(HTTP_QUERY_ACCEPT_ENCODING
),
2585 FE(HTTP_QUERY_ACCEPT_LANGUAGE
),
2586 FE(HTTP_QUERY_AUTHORIZATION
),
2587 FE(HTTP_QUERY_CONTENT_ENCODING
),
2588 FE(HTTP_QUERY_FORWARDED
),
2589 FE(HTTP_QUERY_FROM
),
2590 FE(HTTP_QUERY_IF_MODIFIED_SINCE
),
2591 FE(HTTP_QUERY_LOCATION
),
2592 FE(HTTP_QUERY_ORIG_URI
),
2593 FE(HTTP_QUERY_REFERER
),
2594 FE(HTTP_QUERY_RETRY_AFTER
),
2595 FE(HTTP_QUERY_SERVER
),
2596 FE(HTTP_QUERY_TITLE
),
2597 FE(HTTP_QUERY_USER_AGENT
),
2598 FE(HTTP_QUERY_WWW_AUTHENTICATE
),
2599 FE(HTTP_QUERY_PROXY_AUTHENTICATE
),
2600 FE(HTTP_QUERY_ACCEPT_RANGES
),
2601 FE(HTTP_QUERY_SET_COOKIE
),
2602 FE(HTTP_QUERY_COOKIE
),
2603 FE(HTTP_QUERY_REQUEST_METHOD
),
2604 FE(HTTP_QUERY_REFRESH
),
2605 FE(HTTP_QUERY_CONTENT_DISPOSITION
),
2607 FE(HTTP_QUERY_CACHE_CONTROL
),
2608 FE(HTTP_QUERY_CONTENT_BASE
),
2609 FE(HTTP_QUERY_CONTENT_LOCATION
),
2610 FE(HTTP_QUERY_CONTENT_MD5
),
2611 FE(HTTP_QUERY_CONTENT_RANGE
),
2612 FE(HTTP_QUERY_ETAG
),
2613 FE(HTTP_QUERY_HOST
),
2614 FE(HTTP_QUERY_IF_MATCH
),
2615 FE(HTTP_QUERY_IF_NONE_MATCH
),
2616 FE(HTTP_QUERY_IF_RANGE
),
2617 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE
),
2618 FE(HTTP_QUERY_MAX_FORWARDS
),
2619 FE(HTTP_QUERY_PROXY_AUTHORIZATION
),
2620 FE(HTTP_QUERY_RANGE
),
2621 FE(HTTP_QUERY_TRANSFER_ENCODING
),
2622 FE(HTTP_QUERY_UPGRADE
),
2623 FE(HTTP_QUERY_VARY
),
2625 FE(HTTP_QUERY_WARNING
),
2626 FE(HTTP_QUERY_CUSTOM
)
2628 static const wininet_flag_info modifier_flags
[] = {
2629 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS
),
2630 FE(HTTP_QUERY_FLAG_SYSTEMTIME
),
2631 FE(HTTP_QUERY_FLAG_NUMBER
),
2632 FE(HTTP_QUERY_FLAG_COALESCE
)
2635 DWORD info_mod
= dwInfoLevel
& HTTP_QUERY_MODIFIER_FLAGS_MASK
;
2636 DWORD info
= dwInfoLevel
& HTTP_QUERY_HEADER_MASK
;
2639 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest
, dwInfoLevel
, dwInfoLevel
);
2640 TRACE(" Attribute:");
2641 for (i
= 0; i
< (sizeof(query_flags
) / sizeof(query_flags
[0])); i
++) {
2642 if (query_flags
[i
].val
== info
) {
2643 TRACE(" %s", query_flags
[i
].name
);
2647 if (i
== (sizeof(query_flags
) / sizeof(query_flags
[0]))) {
2648 TRACE(" Unknown (%08x)", info
);
2651 TRACE(" Modifier:");
2652 for (i
= 0; i
< (sizeof(modifier_flags
) / sizeof(modifier_flags
[0])); i
++) {
2653 if (modifier_flags
[i
].val
& info_mod
) {
2654 TRACE(" %s", modifier_flags
[i
].name
);
2655 info_mod
&= ~ modifier_flags
[i
].val
;
2660 TRACE(" Unknown (%08x)", info_mod
);
2665 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hHttpRequest
);
2666 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
2668 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2672 if (lpBuffer
== NULL
)
2673 *lpdwBufferLength
= 0;
2674 bSuccess
= HTTP_HttpQueryInfoW( lpwhr
, dwInfoLevel
,
2675 lpBuffer
, lpdwBufferLength
, lpdwIndex
);
2679 WININET_Release( &lpwhr
->hdr
);
2681 TRACE("%d <--\n", bSuccess
);
2685 /***********************************************************************
2686 * HttpQueryInfoA (WININET.@)
2688 * Queries for information about an HTTP request
2695 BOOL WINAPI
HttpQueryInfoA(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
2696 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
2702 if((dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
) ||
2703 (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
))
2705 return HttpQueryInfoW( hHttpRequest
, dwInfoLevel
, lpBuffer
,
2706 lpdwBufferLength
, lpdwIndex
);
2712 len
= (*lpdwBufferLength
)*sizeof(WCHAR
);
2713 if ((dwInfoLevel
& HTTP_QUERY_HEADER_MASK
) == HTTP_QUERY_CUSTOM
)
2715 alloclen
= MultiByteToWideChar( CP_ACP
, 0, lpBuffer
, -1, NULL
, 0 ) * sizeof(WCHAR
);
2721 bufferW
= HeapAlloc( GetProcessHeap(), 0, alloclen
);
2722 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
2723 if ((dwInfoLevel
& HTTP_QUERY_HEADER_MASK
) == HTTP_QUERY_CUSTOM
)
2724 MultiByteToWideChar( CP_ACP
, 0, lpBuffer
, -1, bufferW
, alloclen
/ sizeof(WCHAR
) );
2731 result
= HttpQueryInfoW( hHttpRequest
, dwInfoLevel
, bufferW
,
2735 len
= WideCharToMultiByte( CP_ACP
,0, bufferW
, len
/ sizeof(WCHAR
) + 1,
2736 lpBuffer
, *lpdwBufferLength
, NULL
, NULL
);
2737 *lpdwBufferLength
= len
- 1;
2739 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer
));
2742 /* since the strings being returned from HttpQueryInfoW should be
2743 * only ASCII characters, it is reasonable to assume that all of
2744 * the Unicode characters can be reduced to a single byte */
2745 *lpdwBufferLength
= len
/ sizeof(WCHAR
);
2747 HeapFree(GetProcessHeap(), 0, bufferW
);
2752 /***********************************************************************
2753 * HttpSendRequestExA (WININET.@)
2755 * Sends the specified request to the HTTP server and allows chunked
2760 * Failure: FALSE, call GetLastError() for more information.
2762 BOOL WINAPI
HttpSendRequestExA(HINTERNET hRequest
,
2763 LPINTERNET_BUFFERSA lpBuffersIn
,
2764 LPINTERNET_BUFFERSA lpBuffersOut
,
2765 DWORD dwFlags
, DWORD_PTR dwContext
)
2767 INTERNET_BUFFERSW BuffersInW
;
2770 LPWSTR header
= NULL
;
2772 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest
, lpBuffersIn
,
2773 lpBuffersOut
, dwFlags
, dwContext
);
2777 BuffersInW
.dwStructSize
= sizeof(LPINTERNET_BUFFERSW
);
2778 if (lpBuffersIn
->lpcszHeader
)
2780 headerlen
= MultiByteToWideChar(CP_ACP
,0,lpBuffersIn
->lpcszHeader
,
2781 lpBuffersIn
->dwHeadersLength
,0,0);
2782 header
= HeapAlloc(GetProcessHeap(),0,headerlen
*sizeof(WCHAR
));
2783 if (!(BuffersInW
.lpcszHeader
= header
))
2785 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2788 BuffersInW
.dwHeadersLength
= MultiByteToWideChar(CP_ACP
, 0,
2789 lpBuffersIn
->lpcszHeader
, lpBuffersIn
->dwHeadersLength
,
2793 BuffersInW
.lpcszHeader
= NULL
;
2794 BuffersInW
.dwHeadersTotal
= lpBuffersIn
->dwHeadersTotal
;
2795 BuffersInW
.lpvBuffer
= lpBuffersIn
->lpvBuffer
;
2796 BuffersInW
.dwBufferLength
= lpBuffersIn
->dwBufferLength
;
2797 BuffersInW
.dwBufferTotal
= lpBuffersIn
->dwBufferTotal
;
2798 BuffersInW
.Next
= NULL
;
2801 rc
= HttpSendRequestExW(hRequest
, lpBuffersIn
? &BuffersInW
: NULL
, NULL
, dwFlags
, dwContext
);
2803 HeapFree(GetProcessHeap(),0,header
);
2808 /***********************************************************************
2809 * HttpSendRequestExW (WININET.@)
2811 * Sends the specified request to the HTTP server and allows chunked
2816 * Failure: FALSE, call GetLastError() for more information.
2818 BOOL WINAPI
HttpSendRequestExW(HINTERNET hRequest
,
2819 LPINTERNET_BUFFERSW lpBuffersIn
,
2820 LPINTERNET_BUFFERSW lpBuffersOut
,
2821 DWORD dwFlags
, DWORD_PTR dwContext
)
2824 LPWININETHTTPREQW lpwhr
;
2825 LPWININETHTTPSESSIONW lpwhs
;
2826 LPWININETAPPINFOW hIC
;
2828 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest
, lpBuffersIn
,
2829 lpBuffersOut
, dwFlags
, dwContext
);
2831 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hRequest
);
2833 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
2835 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2839 lpwhs
= lpwhr
->lpHttpSession
;
2840 assert(lpwhs
->hdr
.htype
== WH_HHTTPSESSION
);
2841 hIC
= lpwhs
->lpAppInfo
;
2842 assert(hIC
->hdr
.htype
== WH_HINIT
);
2844 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2846 WORKREQUEST workRequest
;
2847 struct WORKREQ_HTTPSENDREQUESTW
*req
;
2849 workRequest
.asyncproc
= AsyncHttpSendRequestProc
;
2850 workRequest
.hdr
= WININET_AddRef( &lpwhr
->hdr
);
2851 req
= &workRequest
.u
.HttpSendRequestW
;
2854 if (lpBuffersIn
->lpcszHeader
)
2855 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
2856 req
->lpszHeader
= WININET_strdupW(lpBuffersIn
->lpcszHeader
);
2858 req
->lpszHeader
= NULL
;
2859 req
->dwHeaderLength
= lpBuffersIn
->dwHeadersLength
;
2860 req
->lpOptional
= lpBuffersIn
->lpvBuffer
;
2861 req
->dwOptionalLength
= lpBuffersIn
->dwBufferLength
;
2862 req
->dwContentLength
= lpBuffersIn
->dwBufferTotal
;
2866 req
->lpszHeader
= NULL
;
2867 req
->dwHeaderLength
= 0;
2868 req
->lpOptional
= NULL
;
2869 req
->dwOptionalLength
= 0;
2870 req
->dwContentLength
= 0;
2873 req
->bEndRequest
= FALSE
;
2875 INTERNET_AsyncCall(&workRequest
);
2877 * This is from windows.
2879 INTERNET_SetLastError(ERROR_IO_PENDING
);
2884 ret
= HTTP_HttpSendRequestW(lpwhr
, lpBuffersIn
->lpcszHeader
, lpBuffersIn
->dwHeadersLength
,
2885 lpBuffersIn
->lpvBuffer
, lpBuffersIn
->dwBufferLength
,
2886 lpBuffersIn
->dwBufferTotal
, FALSE
);
2888 ret
= HTTP_HttpSendRequestW(lpwhr
, NULL
, 0, NULL
, 0, 0, FALSE
);
2893 WININET_Release( &lpwhr
->hdr
);
2899 /***********************************************************************
2900 * HttpSendRequestW (WININET.@)
2902 * Sends the specified request to the HTTP server
2909 BOOL WINAPI
HttpSendRequestW(HINTERNET hHttpRequest
, LPCWSTR lpszHeaders
,
2910 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
2912 LPWININETHTTPREQW lpwhr
;
2913 LPWININETHTTPSESSIONW lpwhs
= NULL
;
2914 LPWININETAPPINFOW hIC
= NULL
;
2917 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest
,
2918 debugstr_wn(lpszHeaders
, dwHeaderLength
), dwHeaderLength
, lpOptional
, dwOptionalLength
);
2920 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hHttpRequest
);
2921 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
2923 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2928 lpwhs
= lpwhr
->lpHttpSession
;
2929 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
2931 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2936 hIC
= lpwhs
->lpAppInfo
;
2937 if (NULL
== hIC
|| hIC
->hdr
.htype
!= WH_HINIT
)
2939 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2944 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2946 WORKREQUEST workRequest
;
2947 struct WORKREQ_HTTPSENDREQUESTW
*req
;
2949 workRequest
.asyncproc
= AsyncHttpSendRequestProc
;
2950 workRequest
.hdr
= WININET_AddRef( &lpwhr
->hdr
);
2951 req
= &workRequest
.u
.HttpSendRequestW
;
2956 if (dwHeaderLength
== ~0u) size
= (strlenW(lpszHeaders
) + 1) * sizeof(WCHAR
);
2957 else size
= dwHeaderLength
* sizeof(WCHAR
);
2959 req
->lpszHeader
= HeapAlloc(GetProcessHeap(), 0, size
);
2960 memcpy(req
->lpszHeader
, lpszHeaders
, size
);
2963 req
->lpszHeader
= 0;
2964 req
->dwHeaderLength
= dwHeaderLength
;
2965 req
->lpOptional
= lpOptional
;
2966 req
->dwOptionalLength
= dwOptionalLength
;
2967 req
->dwContentLength
= dwOptionalLength
;
2968 req
->bEndRequest
= TRUE
;
2970 INTERNET_AsyncCall(&workRequest
);
2972 * This is from windows.
2974 INTERNET_SetLastError(ERROR_IO_PENDING
);
2979 r
= HTTP_HttpSendRequestW(lpwhr
, lpszHeaders
,
2980 dwHeaderLength
, lpOptional
, dwOptionalLength
,
2981 dwOptionalLength
, TRUE
);
2985 WININET_Release( &lpwhr
->hdr
);
2989 /***********************************************************************
2990 * HttpSendRequestA (WININET.@)
2992 * Sends the specified request to the HTTP server
2999 BOOL WINAPI
HttpSendRequestA(HINTERNET hHttpRequest
, LPCSTR lpszHeaders
,
3000 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
3003 LPWSTR szHeaders
=NULL
;
3004 DWORD nLen
=dwHeaderLength
;
3005 if(lpszHeaders
!=NULL
)
3007 nLen
=MultiByteToWideChar(CP_ACP
,0,lpszHeaders
,dwHeaderLength
,NULL
,0);
3008 szHeaders
=HeapAlloc(GetProcessHeap(),0,nLen
*sizeof(WCHAR
));
3009 MultiByteToWideChar(CP_ACP
,0,lpszHeaders
,dwHeaderLength
,szHeaders
,nLen
);
3011 result
=HttpSendRequestW(hHttpRequest
, szHeaders
, nLen
, lpOptional
, dwOptionalLength
);
3012 HeapFree(GetProcessHeap(),0,szHeaders
);
3016 static BOOL
HTTP_GetRequestURL(WININETHTTPREQW
*req
, LPWSTR buf
)
3018 LPHTTPHEADERW host_header
;
3020 static const WCHAR formatW
[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
3022 host_header
= HTTP_GetHeader(req
, szHost
);
3026 sprintfW(buf
, formatW
, host_header
->lpszValue
, req
->lpszPath
); /* FIXME */
3030 /***********************************************************************
3031 * HTTP_GetRedirectURL (internal)
3033 static LPWSTR
HTTP_GetRedirectURL(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszUrl
)
3035 static WCHAR szHttp
[] = {'h','t','t','p',0};
3036 static WCHAR szHttps
[] = {'h','t','t','p','s',0};
3037 LPWININETHTTPSESSIONW lpwhs
= lpwhr
->lpHttpSession
;
3038 URL_COMPONENTSW urlComponents
;
3039 DWORD url_length
= 0;
3041 LPWSTR combined_url
;
3043 if (lpszUrl
[0]=='/') return WININET_strdupW( lpszUrl
);
3045 urlComponents
.dwStructSize
= sizeof(URL_COMPONENTSW
);
3046 urlComponents
.lpszScheme
= (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
) ? szHttps
: szHttp
;
3047 urlComponents
.dwSchemeLength
= 0;
3048 urlComponents
.lpszHostName
= lpwhs
->lpszHostName
;
3049 urlComponents
.dwHostNameLength
= 0;
3050 urlComponents
.nPort
= lpwhs
->nHostPort
;
3051 urlComponents
.lpszUserName
= lpwhs
->lpszUserName
;
3052 urlComponents
.dwUserNameLength
= 0;
3053 urlComponents
.lpszPassword
= NULL
;
3054 urlComponents
.dwPasswordLength
= 0;
3055 urlComponents
.lpszUrlPath
= lpwhr
->lpszPath
;
3056 urlComponents
.dwUrlPathLength
= 0;
3057 urlComponents
.lpszExtraInfo
= NULL
;
3058 urlComponents
.dwExtraInfoLength
= 0;
3060 if (!InternetCreateUrlW(&urlComponents
, 0, NULL
, &url_length
) &&
3061 (GetLastError() != ERROR_INSUFFICIENT_BUFFER
))
3064 orig_url
= HeapAlloc(GetProcessHeap(), 0, url_length
);
3066 /* convert from bytes to characters */
3067 url_length
= url_length
/ sizeof(WCHAR
) - 1;
3068 if (!InternetCreateUrlW(&urlComponents
, 0, orig_url
, &url_length
))
3070 HeapFree(GetProcessHeap(), 0, orig_url
);
3075 if (!InternetCombineUrlW(orig_url
, lpszUrl
, NULL
, &url_length
, ICU_ENCODE_SPACES_ONLY
) &&
3076 (GetLastError() != ERROR_INSUFFICIENT_BUFFER
))
3078 HeapFree(GetProcessHeap(), 0, orig_url
);
3081 combined_url
= HeapAlloc(GetProcessHeap(), 0, url_length
* sizeof(WCHAR
));
3083 if (!InternetCombineUrlW(orig_url
, lpszUrl
, combined_url
, &url_length
, ICU_ENCODE_SPACES_ONLY
))
3085 HeapFree(GetProcessHeap(), 0, orig_url
);
3086 HeapFree(GetProcessHeap(), 0, combined_url
);
3089 HeapFree(GetProcessHeap(), 0, orig_url
);
3090 return combined_url
;
3094 /***********************************************************************
3095 * HTTP_HandleRedirect (internal)
3097 static BOOL
HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszUrl
)
3099 LPWININETHTTPSESSIONW lpwhs
= lpwhr
->lpHttpSession
;
3100 LPWININETAPPINFOW hIC
= lpwhs
->lpAppInfo
;
3101 BOOL using_proxy
= hIC
->lpszProxy
&& hIC
->lpszProxy
[0];
3102 WCHAR path
[INTERNET_MAX_URL_LENGTH
];
3107 /* if it's an absolute path, keep the same session info */
3108 lstrcpynW(path
, lpszUrl
, INTERNET_MAX_URL_LENGTH
);
3112 URL_COMPONENTSW urlComponents
;
3113 WCHAR protocol
[32], hostName
[MAXHOSTNAME
], userName
[1024];
3114 static WCHAR szHttp
[] = {'h','t','t','p',0};
3115 static WCHAR szHttps
[] = {'h','t','t','p','s',0};
3121 urlComponents
.dwStructSize
= sizeof(URL_COMPONENTSW
);
3122 urlComponents
.lpszScheme
= protocol
;
3123 urlComponents
.dwSchemeLength
= 32;
3124 urlComponents
.lpszHostName
= hostName
;
3125 urlComponents
.dwHostNameLength
= MAXHOSTNAME
;
3126 urlComponents
.lpszUserName
= userName
;
3127 urlComponents
.dwUserNameLength
= 1024;
3128 urlComponents
.lpszPassword
= NULL
;
3129 urlComponents
.dwPasswordLength
= 0;
3130 urlComponents
.lpszUrlPath
= path
;
3131 urlComponents
.dwUrlPathLength
= 2048;
3132 urlComponents
.lpszExtraInfo
= NULL
;
3133 urlComponents
.dwExtraInfoLength
= 0;
3134 if(!InternetCrackUrlW(lpszUrl
, strlenW(lpszUrl
), 0, &urlComponents
))
3137 if (!strncmpW(szHttp
, urlComponents
.lpszScheme
, strlenW(szHttp
)) &&
3138 (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
))
3140 TRACE("redirect from secure page to non-secure page\n");
3141 /* FIXME: warn about from secure redirect to non-secure page */
3142 lpwhr
->hdr
.dwFlags
&= ~INTERNET_FLAG_SECURE
;
3144 if (!strncmpW(szHttps
, urlComponents
.lpszScheme
, strlenW(szHttps
)) &&
3145 !(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
))
3147 TRACE("redirect from non-secure page to secure page\n");
3148 /* FIXME: notify about redirect to secure page */
3149 lpwhr
->hdr
.dwFlags
|= INTERNET_FLAG_SECURE
;
3152 if (urlComponents
.nPort
== INTERNET_INVALID_PORT_NUMBER
)
3154 if (lstrlenW(protocol
)>4) /*https*/
3155 urlComponents
.nPort
= INTERNET_DEFAULT_HTTPS_PORT
;
3157 urlComponents
.nPort
= INTERNET_DEFAULT_HTTP_PORT
;
3162 * This upsets redirects to binary files on sourceforge.net
3163 * and gives an html page instead of the target file
3164 * Examination of the HTTP request sent by native wininet.dll
3165 * reveals that it doesn't send a referrer in that case.
3166 * Maybe there's a flag that enables this, or maybe a referrer
3167 * shouldn't be added in case of a redirect.
3170 /* consider the current host as the referrer */
3171 if (lpwhs
->lpszServerName
&& *lpwhs
->lpszServerName
)
3172 HTTP_ProcessHeader(lpwhr
, HTTP_REFERER
, lpwhs
->lpszServerName
,
3173 HTTP_ADDHDR_FLAG_REQ
|HTTP_ADDREQ_FLAG_REPLACE
|
3174 HTTP_ADDHDR_FLAG_ADD_IF_NEW
);
3177 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszHostName
);
3178 if (urlComponents
.nPort
!= INTERNET_DEFAULT_HTTP_PORT
&&
3179 urlComponents
.nPort
!= INTERNET_DEFAULT_HTTPS_PORT
)
3182 static const WCHAR fmt
[] = {'%','s',':','%','i',0};
3183 len
= lstrlenW(hostName
);
3184 len
+= 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
3185 lpwhs
->lpszHostName
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
3186 sprintfW(lpwhs
->lpszHostName
, fmt
, hostName
, urlComponents
.nPort
);
3189 lpwhs
->lpszHostName
= WININET_strdupW(hostName
);
3191 HTTP_ProcessHeader(lpwhr
, szHost
, lpwhs
->lpszHostName
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_REQ
);
3193 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszUserName
);
3194 lpwhs
->lpszUserName
= NULL
;
3196 lpwhs
->lpszUserName
= WININET_strdupW(userName
);
3200 if (strcmpiW(lpwhs
->lpszServerName
, hostName
) || lpwhs
->nServerPort
!= urlComponents
.nPort
)
3202 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszServerName
);
3203 lpwhs
->lpszServerName
= WININET_strdupW(hostName
);
3204 lpwhs
->nServerPort
= urlComponents
.nPort
;
3206 NETCON_close(&lpwhr
->netConnection
);
3207 if (!HTTP_ResolveName(lpwhr
)) return FALSE
;
3208 if (!NETCON_init(&lpwhr
->netConnection
, lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
)) return FALSE
;
3209 lpwhr
->read_pos
= lpwhr
->read_size
= 0;
3210 lpwhr
->read_chunked
= FALSE
;
3214 TRACE("Redirect through proxy\n");
3217 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszPath
);
3218 lpwhr
->lpszPath
=NULL
;
3224 rc
= UrlEscapeW(path
, NULL
, &needed
, URL_ESCAPE_SPACES_ONLY
);
3225 if (rc
!= E_POINTER
)
3226 needed
= strlenW(path
)+1;
3227 lpwhr
->lpszPath
= HeapAlloc(GetProcessHeap(), 0, needed
*sizeof(WCHAR
));
3228 rc
= UrlEscapeW(path
, lpwhr
->lpszPath
, &needed
,
3229 URL_ESCAPE_SPACES_ONLY
);
3232 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path
),rc
);
3233 strcpyW(lpwhr
->lpszPath
,path
);
3237 /* Remove custom content-type/length headers on redirects. */
3238 index
= HTTP_GetCustomHeaderIndex(lpwhr
, szContent_Type
, 0, TRUE
);
3240 HTTP_DeleteCustomHeader(lpwhr
, index
);
3241 index
= HTTP_GetCustomHeaderIndex(lpwhr
, szContent_Length
, 0, TRUE
);
3243 HTTP_DeleteCustomHeader(lpwhr
, index
);
3248 /***********************************************************************
3249 * HTTP_build_req (internal)
3251 * concatenate all the strings in the request together
3253 static LPWSTR
HTTP_build_req( LPCWSTR
*list
, int len
)
3258 for( t
= list
; *t
; t
++ )
3259 len
+= strlenW( *t
);
3262 str
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
3265 for( t
= list
; *t
; t
++ )
3271 static BOOL
HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr
)
3274 LPWSTR requestString
;
3280 static const WCHAR szConnect
[] = {'C','O','N','N','E','C','T',0};
3281 static const WCHAR szFormat
[] = {'%','s',':','%','d',0};
3282 LPWININETHTTPSESSIONW lpwhs
= lpwhr
->lpHttpSession
;
3286 lpszPath
= HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs
->lpszHostName
) + 13)*sizeof(WCHAR
) );
3287 sprintfW( lpszPath
, szFormat
, lpwhs
->lpszHostName
, lpwhs
->nHostPort
);
3288 requestString
= HTTP_BuildHeaderRequestString( lpwhr
, szConnect
, lpszPath
, g_szHttp1_1
);
3289 HeapFree( GetProcessHeap(), 0, lpszPath
);
3291 len
= WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
3292 NULL
, 0, NULL
, NULL
);
3293 len
--; /* the nul terminator isn't needed */
3294 ascii_req
= HeapAlloc( GetProcessHeap(), 0, len
);
3295 WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
3296 ascii_req
, len
, NULL
, NULL
);
3297 HeapFree( GetProcessHeap(), 0, requestString
);
3299 TRACE("full request -> %s\n", debugstr_an( ascii_req
, len
) );
3301 ret
= NETCON_send( &lpwhr
->netConnection
, ascii_req
, len
, 0, &cnt
);
3302 HeapFree( GetProcessHeap(), 0, ascii_req
);
3303 if (!ret
|| cnt
< 0)
3306 responseLen
= HTTP_GetResponseHeaders( lpwhr
, TRUE
);
3313 static void HTTP_InsertCookies(LPWININETHTTPREQW lpwhr
)
3315 static const WCHAR szUrlForm
[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
3316 LPWSTR lpszCookies
, lpszUrl
= NULL
;
3317 DWORD nCookieSize
, size
;
3318 LPHTTPHEADERW Host
= HTTP_GetHeader(lpwhr
,szHost
);
3320 size
= (strlenW(Host
->lpszValue
) + strlenW(szUrlForm
) + strlenW(lpwhr
->lpszPath
)) * sizeof(WCHAR
);
3321 if (!(lpszUrl
= HeapAlloc(GetProcessHeap(), 0, size
))) return;
3322 sprintfW( lpszUrl
, szUrlForm
, Host
->lpszValue
, lpwhr
->lpszPath
);
3324 if (InternetGetCookieW(lpszUrl
, NULL
, NULL
, &nCookieSize
))
3327 static const WCHAR szCookie
[] = {'C','o','o','k','i','e',':',' ',0};
3329 size
= sizeof(szCookie
) + nCookieSize
* sizeof(WCHAR
) + sizeof(szCrLf
);
3330 if ((lpszCookies
= HeapAlloc(GetProcessHeap(), 0, size
)))
3332 cnt
+= sprintfW(lpszCookies
, szCookie
);
3333 InternetGetCookieW(lpszUrl
, NULL
, lpszCookies
+ cnt
, &nCookieSize
);
3334 strcatW(lpszCookies
, szCrLf
);
3336 HTTP_HttpAddRequestHeadersW(lpwhr
, lpszCookies
, strlenW(lpszCookies
), HTTP_ADDREQ_FLAG_REPLACE
);
3337 HeapFree(GetProcessHeap(), 0, lpszCookies
);
3340 HeapFree(GetProcessHeap(), 0, lpszUrl
);
3343 /***********************************************************************
3344 * HTTP_HttpSendRequestW (internal)
3346 * Sends the specified request to the HTTP server
3353 BOOL WINAPI
HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszHeaders
,
3354 DWORD dwHeaderLength
, LPVOID lpOptional
, DWORD dwOptionalLength
,
3355 DWORD dwContentLength
, BOOL bEndRequest
)
3358 BOOL bSuccess
= FALSE
, redirected
= FALSE
;
3359 LPWSTR requestString
= NULL
;
3362 INTERNET_ASYNC_RESULT iar
;
3363 static const WCHAR szPost
[] = { 'P','O','S','T',0 };
3364 static const WCHAR szContentLength
[] =
3365 { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0 };
3366 WCHAR contentLengthStr
[sizeof szContentLength
/2 /* includes \r\n */ + 20 /* int */ ];
3368 TRACE("--> %p\n", lpwhr
);
3370 assert(lpwhr
->hdr
.htype
== WH_HHTTPREQ
);
3372 /* if the verb is NULL default to GET */
3373 if (!lpwhr
->lpszVerb
)
3374 lpwhr
->lpszVerb
= WININET_strdupW(szGET
);
3376 if (dwContentLength
|| strcmpW(lpwhr
->lpszVerb
, szGET
))
3378 sprintfW(contentLengthStr
, szContentLength
, dwContentLength
);
3379 HTTP_HttpAddRequestHeadersW(lpwhr
, contentLengthStr
, -1L, HTTP_ADDREQ_FLAG_REPLACE
);
3380 lpwhr
->dwBytesToWrite
= dwContentLength
;
3382 if (lpwhr
->lpHttpSession
->lpAppInfo
->lpszAgent
)
3384 WCHAR
*agent_header
;
3385 static const WCHAR user_agent
[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0};
3388 len
= strlenW(lpwhr
->lpHttpSession
->lpAppInfo
->lpszAgent
) + strlenW(user_agent
);
3389 agent_header
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
3390 sprintfW(agent_header
, user_agent
, lpwhr
->lpHttpSession
->lpAppInfo
->lpszAgent
);
3392 HTTP_HttpAddRequestHeadersW(lpwhr
, agent_header
, strlenW(agent_header
), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
3393 HeapFree(GetProcessHeap(), 0, agent_header
);
3395 if (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_PRAGMA_NOCACHE
)
3397 static const WCHAR pragma_nocache
[] = {'P','r','a','g','m','a',':',' ','n','o','-','c','a','c','h','e','\r','\n',0};
3398 HTTP_HttpAddRequestHeadersW(lpwhr
, pragma_nocache
, strlenW(pragma_nocache
), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
3400 if ((lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_CACHE_WRITE
) && !strcmpW(lpwhr
->lpszVerb
, szPost
))
3402 static const WCHAR cache_control
[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l',':',
3403 ' ','n','o','-','c','a','c','h','e','\r','\n',0};
3404 HTTP_HttpAddRequestHeadersW(lpwhr
, cache_control
, strlenW(cache_control
), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
3414 /* like native, just in case the caller forgot to call InternetReadFile
3415 * for all the data */
3416 HTTP_DrainContent(lpwhr
);
3417 lpwhr
->dwContentRead
= 0;
3419 if (TRACE_ON(wininet
))
3421 LPHTTPHEADERW Host
= HTTP_GetHeader(lpwhr
,szHost
);
3422 TRACE("Going to url %s %s\n", debugstr_w(Host
->lpszValue
), debugstr_w(lpwhr
->lpszPath
));
3426 if (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_KEEP_CONNECTION
)
3428 HTTP_ProcessHeader(lpwhr
, szConnection
, szKeepAlive
, HTTP_ADDHDR_FLAG_REQ
| HTTP_ADDHDR_FLAG_REPLACE
);
3430 HTTP_InsertAuthorization(lpwhr
, lpwhr
->pAuthInfo
, szAuthorization
);
3431 HTTP_InsertAuthorization(lpwhr
, lpwhr
->pProxyAuthInfo
, szProxy_Authorization
);
3433 if (!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_COOKIES
))
3434 HTTP_InsertCookies(lpwhr
);
3436 /* add the headers the caller supplied */
3437 if( lpszHeaders
&& dwHeaderLength
)
3439 HTTP_HttpAddRequestHeadersW(lpwhr
, lpszHeaders
, dwHeaderLength
,
3440 HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REPLACE
);
3443 if (lpwhr
->lpHttpSession
->lpAppInfo
->lpszProxy
&& lpwhr
->lpHttpSession
->lpAppInfo
->lpszProxy
[0])
3445 WCHAR
*url
= HTTP_BuildProxyRequestUrl(lpwhr
);
3446 requestString
= HTTP_BuildHeaderRequestString(lpwhr
, lpwhr
->lpszVerb
, url
, lpwhr
->lpszVersion
);
3447 HeapFree(GetProcessHeap(), 0, url
);
3450 requestString
= HTTP_BuildHeaderRequestString(lpwhr
, lpwhr
->lpszVerb
, lpwhr
->lpszPath
, lpwhr
->lpszVersion
);
3453 TRACE("Request header -> %s\n", debugstr_w(requestString
) );
3455 /* Send the request and store the results */
3456 if (!HTTP_OpenConnection(lpwhr
))
3459 /* send the request as ASCII, tack on the optional data */
3460 if (!lpOptional
|| redirected
)
3461 dwOptionalLength
= 0;
3462 len
= WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
3463 NULL
, 0, NULL
, NULL
);
3464 ascii_req
= HeapAlloc( GetProcessHeap(), 0, len
+ dwOptionalLength
);
3465 WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
3466 ascii_req
, len
, NULL
, NULL
);
3468 memcpy( &ascii_req
[len
-1], lpOptional
, dwOptionalLength
);
3469 len
= (len
+ dwOptionalLength
- 1);
3471 TRACE("full request -> %s\n", debugstr_a(ascii_req
) );
3473 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3474 INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
3476 NETCON_send(&lpwhr
->netConnection
, ascii_req
, len
, 0, &cnt
);
3477 HeapFree( GetProcessHeap(), 0, ascii_req
);
3479 lpwhr
->dwBytesWritten
= dwOptionalLength
;
3481 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3482 INTERNET_STATUS_REQUEST_SENT
,
3483 &len
, sizeof(DWORD
));
3490 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3491 INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
3496 responseLen
= HTTP_GetResponseHeaders(lpwhr
, TRUE
);
3500 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3501 INTERNET_STATUS_RESPONSE_RECEIVED
, &responseLen
,
3504 HTTP_ProcessCookies(lpwhr
);
3506 if (!set_content_length( lpwhr
)) HTTP_FinishedReading(lpwhr
);
3508 dwBufferSize
= sizeof(dwStatusCode
);
3509 if (!HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_STATUS_CODE
,
3510 &dwStatusCode
,&dwBufferSize
,NULL
))
3513 if (!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTO_REDIRECT
) && bSuccess
)
3515 WCHAR
*new_url
, szNewLocation
[INTERNET_MAX_URL_LENGTH
];
3516 dwBufferSize
=sizeof(szNewLocation
);
3517 if ((dwStatusCode
==HTTP_STATUS_REDIRECT
|| dwStatusCode
==HTTP_STATUS_MOVED
) &&
3518 HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_LOCATION
,szNewLocation
,&dwBufferSize
,NULL
))
3520 /* redirects are always GETs */
3521 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszVerb
);
3522 lpwhr
->lpszVerb
= WININET_strdupW(szGET
);
3524 HTTP_DrainContent(lpwhr
);
3525 if ((new_url
= HTTP_GetRedirectURL( lpwhr
, szNewLocation
)))
3527 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
, INTERNET_STATUS_REDIRECT
,
3528 new_url
, (strlenW(new_url
) + 1) * sizeof(WCHAR
));
3529 bSuccess
= HTTP_HandleRedirect(lpwhr
, new_url
);
3532 HeapFree(GetProcessHeap(), 0, requestString
);
3535 HeapFree( GetProcessHeap(), 0, new_url
);
3540 if (!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTH
) && bSuccess
)
3542 WCHAR szAuthValue
[2048];
3544 if (dwStatusCode
== HTTP_STATUS_DENIED
)
3547 while (HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_WWW_AUTHENTICATE
,szAuthValue
,&dwBufferSize
,&dwIndex
))
3549 if (HTTP_DoAuthorization(lpwhr
, szAuthValue
,
3551 lpwhr
->lpHttpSession
->lpszUserName
,
3552 lpwhr
->lpHttpSession
->lpszPassword
))
3559 if (dwStatusCode
== HTTP_STATUS_PROXY_AUTH_REQ
)
3562 while (HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_PROXY_AUTHENTICATE
,szAuthValue
,&dwBufferSize
,&dwIndex
))
3564 if (HTTP_DoAuthorization(lpwhr
, szAuthValue
,
3565 &lpwhr
->pProxyAuthInfo
,
3566 lpwhr
->lpHttpSession
->lpAppInfo
->lpszProxyUsername
,
3567 lpwhr
->lpHttpSession
->lpAppInfo
->lpszProxyPassword
))
3581 /* FIXME: Better check, when we have to create the cache file */
3582 if(bSuccess
&& (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NEED_FILE
)) {
3583 WCHAR url
[INTERNET_MAX_URL_LENGTH
];
3584 WCHAR cacheFileName
[MAX_PATH
+1];
3587 b
= HTTP_GetRequestURL(lpwhr
, url
);
3589 WARN("Could not get URL\n");
3593 b
= CreateUrlCacheEntryW(url
, lpwhr
->dwContentLength
> 0 ? lpwhr
->dwContentLength
: 0, NULL
, cacheFileName
, 0);
3595 lpwhr
->lpszCacheFile
= WININET_strdupW(cacheFileName
);
3596 lpwhr
->hCacheFile
= CreateFileW(lpwhr
->lpszCacheFile
, GENERIC_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
3597 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
3598 if(lpwhr
->hCacheFile
== INVALID_HANDLE_VALUE
) {
3599 WARN("Could not create file: %u\n", GetLastError());
3600 lpwhr
->hCacheFile
= NULL
;
3603 WARN("Could not create cache entry: %08x\n", GetLastError());
3609 HeapFree(GetProcessHeap(), 0, requestString
);
3611 /* TODO: send notification for P3P header */
3613 if (lpwhr
->lpHttpSession
->lpAppInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
3617 if (lpwhr
->dwBytesWritten
== lpwhr
->dwBytesToWrite
) HTTP_ReceiveRequestData(lpwhr
, TRUE
);
3620 iar
.dwResult
= (DWORD_PTR
)lpwhr
->hdr
.hInternet
;
3623 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3624 INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
3625 sizeof(INTERNET_ASYNC_RESULT
));
3630 iar
.dwResult
= (DWORD_PTR
)lpwhr
->hdr
.hInternet
;
3631 iar
.dwError
= INTERNET_GetLastError();
3633 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3634 INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
3635 sizeof(INTERNET_ASYNC_RESULT
));
3640 if (bSuccess
) INTERNET_SetLastError(ERROR_SUCCESS
);
3644 /***********************************************************************
3645 * HTTPSESSION_Destroy (internal)
3647 * Deallocate session handle
3650 static void HTTPSESSION_Destroy(WININETHANDLEHEADER
*hdr
)
3652 LPWININETHTTPSESSIONW lpwhs
= (LPWININETHTTPSESSIONW
) hdr
;
3654 TRACE("%p\n", lpwhs
);
3656 WININET_Release(&lpwhs
->lpAppInfo
->hdr
);
3658 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszHostName
);
3659 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszServerName
);
3660 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszPassword
);
3661 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszUserName
);
3662 HeapFree(GetProcessHeap(), 0, lpwhs
);
3665 static DWORD
HTTPSESSION_QueryOption(WININETHANDLEHEADER
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
3668 case INTERNET_OPTION_HANDLE_TYPE
:
3669 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
3671 if (*size
< sizeof(ULONG
))
3672 return ERROR_INSUFFICIENT_BUFFER
;
3674 *size
= sizeof(DWORD
);
3675 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_CONNECT_HTTP
;
3676 return ERROR_SUCCESS
;
3679 return INET_QueryOption(option
, buffer
, size
, unicode
);
3682 static DWORD
HTTPSESSION_SetOption(WININETHANDLEHEADER
*hdr
, DWORD option
, void *buffer
, DWORD size
)
3684 WININETHTTPSESSIONW
*ses
= (WININETHTTPSESSIONW
*)hdr
;
3687 case INTERNET_OPTION_USERNAME
:
3689 HeapFree(GetProcessHeap(), 0, ses
->lpszUserName
);
3690 if (!(ses
->lpszUserName
= WININET_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
3691 return ERROR_SUCCESS
;
3693 case INTERNET_OPTION_PASSWORD
:
3695 HeapFree(GetProcessHeap(), 0, ses
->lpszPassword
);
3696 if (!(ses
->lpszPassword
= WININET_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
3697 return ERROR_SUCCESS
;
3702 return ERROR_INTERNET_INVALID_OPTION
;
3705 static const HANDLEHEADERVtbl HTTPSESSIONVtbl
= {
3706 HTTPSESSION_Destroy
,
3708 HTTPSESSION_QueryOption
,
3709 HTTPSESSION_SetOption
,
3718 /***********************************************************************
3719 * HTTP_Connect (internal)
3721 * Create http session handle
3724 * HINTERNET a session handle on success
3728 HINTERNET
HTTP_Connect(LPWININETAPPINFOW hIC
, LPCWSTR lpszServerName
,
3729 INTERNET_PORT nServerPort
, LPCWSTR lpszUserName
,
3730 LPCWSTR lpszPassword
, DWORD dwFlags
, DWORD_PTR dwContext
,
3731 DWORD dwInternalFlags
)
3733 LPWININETHTTPSESSIONW lpwhs
= NULL
;
3734 HINTERNET handle
= NULL
;
3738 if (!lpszServerName
|| !lpszServerName
[0])
3740 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
3744 assert( hIC
->hdr
.htype
== WH_HINIT
);
3746 lpwhs
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WININETHTTPSESSIONW
));
3749 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
3754 * According to my tests. The name is not resolved until a request is sent
3757 lpwhs
->hdr
.htype
= WH_HHTTPSESSION
;
3758 lpwhs
->hdr
.vtbl
= &HTTPSESSIONVtbl
;
3759 lpwhs
->hdr
.dwFlags
= dwFlags
;
3760 lpwhs
->hdr
.dwContext
= dwContext
;
3761 lpwhs
->hdr
.dwInternalFlags
= dwInternalFlags
| (hIC
->hdr
.dwInternalFlags
& INET_CALLBACKW
);
3762 lpwhs
->hdr
.refs
= 1;
3763 lpwhs
->hdr
.lpfnStatusCB
= hIC
->hdr
.lpfnStatusCB
;
3765 WININET_AddRef( &hIC
->hdr
);
3766 lpwhs
->lpAppInfo
= hIC
;
3767 list_add_head( &hIC
->hdr
.children
, &lpwhs
->hdr
.entry
);
3769 handle
= WININET_AllocHandle( &lpwhs
->hdr
);
3772 ERR("Failed to alloc handle\n");
3773 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
3777 if(hIC
->lpszProxy
&& hIC
->dwAccessType
== INTERNET_OPEN_TYPE_PROXY
) {
3778 if(strchrW(hIC
->lpszProxy
, ' '))
3779 FIXME("Several proxies not implemented.\n");
3780 if(hIC
->lpszProxyBypass
)
3781 FIXME("Proxy bypass is ignored.\n");
3783 if (lpszServerName
&& lpszServerName
[0])
3785 lpwhs
->lpszServerName
= WININET_strdupW(lpszServerName
);
3786 lpwhs
->lpszHostName
= WININET_strdupW(lpszServerName
);
3788 if (lpszUserName
&& lpszUserName
[0])
3789 lpwhs
->lpszUserName
= WININET_strdupW(lpszUserName
);
3790 if (lpszPassword
&& lpszPassword
[0])
3791 lpwhs
->lpszPassword
= WININET_strdupW(lpszPassword
);
3792 lpwhs
->nServerPort
= nServerPort
;
3793 lpwhs
->nHostPort
= nServerPort
;
3795 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
3796 if (!(lpwhs
->hdr
.dwInternalFlags
& INET_OPENURL
))
3798 INTERNET_SendCallback(&hIC
->hdr
, dwContext
,
3799 INTERNET_STATUS_HANDLE_CREATED
, &handle
,
3805 WININET_Release( &lpwhs
->hdr
);
3808 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
3812 TRACE("%p --> %p (%p)\n", hIC
, handle
, lpwhs
);
3817 /***********************************************************************
3818 * HTTP_OpenConnection (internal)
3820 * Connect to a web server
3827 static BOOL
HTTP_OpenConnection(LPWININETHTTPREQW lpwhr
)
3829 BOOL bSuccess
= FALSE
;
3830 LPWININETHTTPSESSIONW lpwhs
;
3831 LPWININETAPPINFOW hIC
= NULL
;
3837 if (lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
3839 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
3843 if (NETCON_connected(&lpwhr
->netConnection
))
3848 if (!HTTP_ResolveName(lpwhr
)) goto lend
;
3850 lpwhs
= lpwhr
->lpHttpSession
;
3852 hIC
= lpwhs
->lpAppInfo
;
3853 inet_ntop(lpwhs
->socketAddress
.sin_family
, &lpwhs
->socketAddress
.sin_addr
,
3854 szaddr
, sizeof(szaddr
));
3855 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3856 INTERNET_STATUS_CONNECTING_TO_SERVER
,
3860 if (!NETCON_create(&lpwhr
->netConnection
, lpwhs
->socketAddress
.sin_family
,
3863 WARN("Socket creation failed: %u\n", INTERNET_GetLastError());
3867 if (!NETCON_connect(&lpwhr
->netConnection
, (struct sockaddr
*)&lpwhs
->socketAddress
,
3868 sizeof(lpwhs
->socketAddress
)))
3871 if (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
)
3873 /* Note: we differ from Microsoft's WinINet here. they seem to have
3874 * a bug that causes no status callbacks to be sent when starting
3875 * a tunnel to a proxy server using the CONNECT verb. i believe our
3876 * behaviour to be more correct and to not cause any incompatibilities
3877 * because using a secure connection through a proxy server is a rare
3878 * case that would be hard for anyone to depend on */
3879 if (hIC
->lpszProxy
&& !HTTP_SecureProxyConnect(lpwhr
))
3882 if (!NETCON_secure_connect(&lpwhr
->netConnection
, lpwhs
->lpszHostName
))
3884 WARN("Couldn't connect securely to host\n");
3889 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
3890 INTERNET_STATUS_CONNECTED_TO_SERVER
,
3891 szaddr
, strlen(szaddr
)+1);
3896 lpwhr
->read_pos
= lpwhr
->read_size
= 0;
3897 lpwhr
->read_chunked
= FALSE
;
3899 TRACE("%d <--\n", bSuccess
);
3904 /***********************************************************************
3905 * HTTP_clear_response_headers (internal)
3907 * clear out any old response headers
3909 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr
)
3913 for( i
=0; i
<lpwhr
->nCustHeaders
; i
++)
3915 if( !lpwhr
->pCustHeaders
[i
].lpszField
)
3917 if( !lpwhr
->pCustHeaders
[i
].lpszValue
)
3919 if ( lpwhr
->pCustHeaders
[i
].wFlags
& HDR_ISREQUEST
)
3921 HTTP_DeleteCustomHeader( lpwhr
, i
);
3926 /***********************************************************************
3927 * HTTP_GetResponseHeaders (internal)
3929 * Read server response
3936 static INT
HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr
, BOOL clear
)
3939 WCHAR buffer
[MAX_REPLY_LEN
];
3940 DWORD buflen
= MAX_REPLY_LEN
;
3941 BOOL bSuccess
= FALSE
;
3943 static const WCHAR szHundred
[] = {'1','0','0',0};
3944 char bufferA
[MAX_REPLY_LEN
];
3945 LPWSTR status_code
, status_text
;
3946 DWORD cchMaxRawHeaders
= 1024;
3947 LPWSTR lpszRawHeaders
= HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders
+1)*sizeof(WCHAR
));
3948 DWORD cchRawHeaders
= 0;
3952 /* clear old response headers (eg. from a redirect response) */
3953 if (clear
) HTTP_clear_response_headers( lpwhr
);
3955 if (!NETCON_connected(&lpwhr
->netConnection
))
3960 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
3962 buflen
= MAX_REPLY_LEN
;
3963 if (!read_line(lpwhr
, bufferA
, &buflen
))
3966 MultiByteToWideChar( CP_ACP
, 0, bufferA
, buflen
, buffer
, MAX_REPLY_LEN
);
3968 /* split the version from the status code */
3969 status_code
= strchrW( buffer
, ' ' );
3974 /* split the status code from the status text */
3975 status_text
= strchrW( status_code
, ' ' );
3980 TRACE("version [%s] status code [%s] status text [%s]\n",
3981 debugstr_w(buffer
), debugstr_w(status_code
), debugstr_w(status_text
) );
3983 } while (!strcmpW(status_code
, szHundred
)); /* ignore "100 Continue" responses */
3985 /* Add status code */
3986 HTTP_ProcessHeader(lpwhr
, szStatus
, status_code
,
3987 HTTP_ADDHDR_FLAG_REPLACE
);
3989 HeapFree(GetProcessHeap(),0,lpwhr
->lpszVersion
);
3990 HeapFree(GetProcessHeap(),0,lpwhr
->lpszStatusText
);
3992 lpwhr
->lpszVersion
= WININET_strdupW(buffer
);
3993 lpwhr
->lpszStatusText
= WININET_strdupW(status_text
);
3995 /* Restore the spaces */
3996 *(status_code
-1) = ' ';
3997 *(status_text
-1) = ' ';
3999 /* regenerate raw headers */
4000 while (cchRawHeaders
+ buflen
+ strlenW(szCrLf
) > cchMaxRawHeaders
)
4002 cchMaxRawHeaders
*= 2;
4003 lpszRawHeaders
= HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders
, (cchMaxRawHeaders
+1)*sizeof(WCHAR
));
4005 memcpy(lpszRawHeaders
+cchRawHeaders
, buffer
, (buflen
-1)*sizeof(WCHAR
));
4006 cchRawHeaders
+= (buflen
-1);
4007 memcpy(lpszRawHeaders
+cchRawHeaders
, szCrLf
, sizeof(szCrLf
));
4008 cchRawHeaders
+= sizeof(szCrLf
)/sizeof(szCrLf
[0])-1;
4009 lpszRawHeaders
[cchRawHeaders
] = '\0';
4011 /* Parse each response line */
4014 buflen
= MAX_REPLY_LEN
;
4015 if (read_line(lpwhr
, bufferA
, &buflen
))
4017 LPWSTR
* pFieldAndValue
;
4019 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA
));
4021 if (!bufferA
[0]) break;
4022 if (!strchr(bufferA
, ':'))
4024 WARN("invalid header\n");
4027 MultiByteToWideChar( CP_ACP
, 0, bufferA
, buflen
, buffer
, MAX_REPLY_LEN
);
4029 while (cchRawHeaders
+ buflen
+ strlenW(szCrLf
) > cchMaxRawHeaders
)
4031 cchMaxRawHeaders
*= 2;
4032 lpszRawHeaders
= HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders
, (cchMaxRawHeaders
+1)*sizeof(WCHAR
));
4034 memcpy(lpszRawHeaders
+cchRawHeaders
, buffer
, (buflen
-1)*sizeof(WCHAR
));
4035 cchRawHeaders
+= (buflen
-1);
4036 memcpy(lpszRawHeaders
+cchRawHeaders
, szCrLf
, sizeof(szCrLf
));
4037 cchRawHeaders
+= sizeof(szCrLf
)/sizeof(szCrLf
[0])-1;
4038 lpszRawHeaders
[cchRawHeaders
] = '\0';
4040 pFieldAndValue
= HTTP_InterpretHttpHeader(buffer
);
4041 if (!pFieldAndValue
)
4044 HTTP_ProcessHeader(lpwhr
, pFieldAndValue
[0], pFieldAndValue
[1],
4045 HTTP_ADDREQ_FLAG_ADD
);
4047 HTTP_FreeTokens(pFieldAndValue
);
4057 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszRawHeaders
);
4058 lpwhr
->lpszRawHeaders
= lpszRawHeaders
;
4059 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders
));
4069 HeapFree(GetProcessHeap(), 0, lpszRawHeaders
);
4075 static void strip_spaces(LPWSTR start
)
4080 while (*str
== ' ' && *str
!= '\0')
4084 memmove(start
, str
, sizeof(WCHAR
) * (strlenW(str
) + 1));
4086 end
= start
+ strlenW(start
) - 1;
4087 while (end
>= start
&& *end
== ' ')
4095 /***********************************************************************
4096 * HTTP_InterpretHttpHeader (internal)
4098 * Parse server response
4102 * Pointer to array of field, value, NULL on success.
4105 static LPWSTR
* HTTP_InterpretHttpHeader(LPCWSTR buffer
)
4107 LPWSTR
* pTokenPair
;
4111 pTokenPair
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*pTokenPair
)*3);
4113 pszColon
= strchrW(buffer
, ':');
4114 /* must have two tokens */
4117 HTTP_FreeTokens(pTokenPair
);
4119 TRACE("No ':' in line: %s\n", debugstr_w(buffer
));
4123 pTokenPair
[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon
- buffer
+ 1) * sizeof(WCHAR
));
4126 HTTP_FreeTokens(pTokenPair
);
4129 memcpy(pTokenPair
[0], buffer
, (pszColon
- buffer
) * sizeof(WCHAR
));
4130 pTokenPair
[0][pszColon
- buffer
] = '\0';
4134 len
= strlenW(pszColon
);
4135 pTokenPair
[1] = HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
4138 HTTP_FreeTokens(pTokenPair
);
4141 memcpy(pTokenPair
[1], pszColon
, (len
+ 1) * sizeof(WCHAR
));
4143 strip_spaces(pTokenPair
[0]);
4144 strip_spaces(pTokenPair
[1]);
4146 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair
[0]), debugstr_w(pTokenPair
[1]));
4150 /***********************************************************************
4151 * HTTP_ProcessHeader (internal)
4153 * Stuff header into header tables according to <dwModifier>
4157 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
4159 static BOOL
HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr
, LPCWSTR field
, LPCWSTR value
, DWORD dwModifier
)
4161 LPHTTPHEADERW lphttpHdr
= NULL
;
4162 BOOL bSuccess
= FALSE
;
4164 BOOL request_only
= dwModifier
& HTTP_ADDHDR_FLAG_REQ
;
4166 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field
), debugstr_w(value
), dwModifier
);
4168 /* REPLACE wins out over ADD */
4169 if (dwModifier
& HTTP_ADDHDR_FLAG_REPLACE
)
4170 dwModifier
&= ~HTTP_ADDHDR_FLAG_ADD
;
4172 if (dwModifier
& HTTP_ADDHDR_FLAG_ADD
)
4175 index
= HTTP_GetCustomHeaderIndex(lpwhr
, field
, 0, request_only
);
4179 if (dwModifier
& HTTP_ADDHDR_FLAG_ADD_IF_NEW
)
4183 lphttpHdr
= &lpwhr
->pCustHeaders
[index
];
4189 hdr
.lpszField
= (LPWSTR
)field
;
4190 hdr
.lpszValue
= (LPWSTR
)value
;
4191 hdr
.wFlags
= hdr
.wCount
= 0;
4193 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
4194 hdr
.wFlags
|= HDR_ISREQUEST
;
4196 return HTTP_InsertCustomHeader(lpwhr
, &hdr
);
4198 /* no value to delete */
4201 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
4202 lphttpHdr
->wFlags
|= HDR_ISREQUEST
;
4204 lphttpHdr
->wFlags
&= ~HDR_ISREQUEST
;
4206 if (dwModifier
& HTTP_ADDHDR_FLAG_REPLACE
)
4208 HTTP_DeleteCustomHeader( lpwhr
, index
);
4214 hdr
.lpszField
= (LPWSTR
)field
;
4215 hdr
.lpszValue
= (LPWSTR
)value
;
4216 hdr
.wFlags
= hdr
.wCount
= 0;
4218 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
4219 hdr
.wFlags
|= HDR_ISREQUEST
;
4221 return HTTP_InsertCustomHeader(lpwhr
, &hdr
);
4226 else if (dwModifier
& COALESCEFLAGS
)
4231 INT origlen
= strlenW(lphttpHdr
->lpszValue
);
4232 INT valuelen
= strlenW(value
);
4234 if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
)
4237 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
4239 else if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON
)
4242 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
4245 len
= origlen
+ valuelen
+ ((ch
> 0) ? 2 : 0);
4247 lpsztmp
= HeapReAlloc(GetProcessHeap(), 0, lphttpHdr
->lpszValue
, (len
+1)*sizeof(WCHAR
));
4250 lphttpHdr
->lpszValue
= lpsztmp
;
4251 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
4254 lphttpHdr
->lpszValue
[origlen
] = ch
;
4256 lphttpHdr
->lpszValue
[origlen
] = ' ';
4260 memcpy(&lphttpHdr
->lpszValue
[origlen
], value
, valuelen
*sizeof(WCHAR
));
4261 lphttpHdr
->lpszValue
[len
] = '\0';
4266 WARN("HeapReAlloc (%d bytes) failed\n",len
+1);
4267 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
4270 TRACE("<-- %d\n",bSuccess
);
4275 /***********************************************************************
4276 * HTTP_FinishedReading (internal)
4278 * Called when all content from server has been read by client.
4281 static BOOL
HTTP_FinishedReading(LPWININETHTTPREQW lpwhr
)
4283 WCHAR szVersion
[10];
4284 WCHAR szConnectionResponse
[20];
4285 DWORD dwBufferSize
= sizeof(szVersion
);
4286 BOOL keepalive
= FALSE
;
4290 /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
4291 * the connection is keep-alive by default */
4292 if (HTTP_HttpQueryInfoW(lpwhr
, HTTP_QUERY_VERSION
, szVersion
,
4293 &dwBufferSize
, NULL
) &&
4294 !strcmpiW(szVersion
, g_szHttp1_1
))
4299 dwBufferSize
= sizeof(szConnectionResponse
);
4300 if (HTTP_HttpQueryInfoW(lpwhr
, HTTP_QUERY_PROXY_CONNECTION
, szConnectionResponse
, &dwBufferSize
, NULL
) ||
4301 HTTP_HttpQueryInfoW(lpwhr
, HTTP_QUERY_CONNECTION
, szConnectionResponse
, &dwBufferSize
, NULL
))
4303 keepalive
= !strcmpiW(szConnectionResponse
, szKeepAlive
);
4308 HTTPREQ_CloseConnection(&lpwhr
->hdr
);
4311 /* FIXME: store data in the URL cache here */
4317 /***********************************************************************
4318 * HTTP_GetCustomHeaderIndex (internal)
4320 * Return index of custom header from header array
4323 static INT
HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszField
,
4324 int requested_index
, BOOL request_only
)
4328 TRACE("%s\n", debugstr_w(lpszField
));
4330 for (index
= 0; index
< lpwhr
->nCustHeaders
; index
++)
4332 if (strcmpiW(lpwhr
->pCustHeaders
[index
].lpszField
, lpszField
))
4335 if (request_only
&& !(lpwhr
->pCustHeaders
[index
].wFlags
& HDR_ISREQUEST
))
4338 if (!request_only
&& (lpwhr
->pCustHeaders
[index
].wFlags
& HDR_ISREQUEST
))
4341 if (requested_index
== 0)
4346 if (index
>= lpwhr
->nCustHeaders
)
4349 TRACE("Return: %d\n", index
);
4354 /***********************************************************************
4355 * HTTP_InsertCustomHeader (internal)
4357 * Insert header into array
4360 static BOOL
HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr
, LPHTTPHEADERW lpHdr
)
4363 LPHTTPHEADERW lph
= NULL
;
4366 TRACE("--> %s: %s\n", debugstr_w(lpHdr
->lpszField
), debugstr_w(lpHdr
->lpszValue
));
4367 count
= lpwhr
->nCustHeaders
+ 1;
4369 lph
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, lpwhr
->pCustHeaders
, sizeof(HTTPHEADERW
) * count
);
4371 lph
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(HTTPHEADERW
) * count
);
4375 lpwhr
->pCustHeaders
= lph
;
4376 lpwhr
->pCustHeaders
[count
-1].lpszField
= WININET_strdupW(lpHdr
->lpszField
);
4377 lpwhr
->pCustHeaders
[count
-1].lpszValue
= WININET_strdupW(lpHdr
->lpszValue
);
4378 lpwhr
->pCustHeaders
[count
-1].wFlags
= lpHdr
->wFlags
;
4379 lpwhr
->pCustHeaders
[count
-1].wCount
= lpHdr
->wCount
;
4380 lpwhr
->nCustHeaders
++;
4385 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
4392 /***********************************************************************
4393 * HTTP_DeleteCustomHeader (internal)
4395 * Delete header from array
4396 * If this function is called, the indexs may change.
4398 static BOOL
HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr
, DWORD index
)
4400 if( lpwhr
->nCustHeaders
<= 0 )
4402 if( index
>= lpwhr
->nCustHeaders
)
4404 lpwhr
->nCustHeaders
--;
4406 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
[index
].lpszField
);
4407 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
[index
].lpszValue
);
4409 memmove( &lpwhr
->pCustHeaders
[index
], &lpwhr
->pCustHeaders
[index
+1],
4410 (lpwhr
->nCustHeaders
- index
)* sizeof(HTTPHEADERW
) );
4411 memset( &lpwhr
->pCustHeaders
[lpwhr
->nCustHeaders
], 0, sizeof(HTTPHEADERW
) );
4417 /***********************************************************************
4418 * HTTP_VerifyValidHeader (internal)
4420 * Verify the given header is not invalid for the given http request
4423 static BOOL
HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr
, LPCWSTR field
)
4425 /* Accept-Encoding is stripped from HTTP/1.0 requests. It is invalid */
4426 if (!strcmpW(lpwhr
->lpszVersion
, g_szHttp1_0
) && !strcmpiW(field
, szAccept_Encoding
))
4432 /***********************************************************************
4433 * IsHostInProxyBypassList (@)
4438 BOOL WINAPI
IsHostInProxyBypassList(DWORD flags
, LPCSTR szHost
, DWORD length
)
4440 FIXME("STUB: flags=%d host=%s length=%d\n",flags
,szHost
,length
);