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 #include <sys/types.h>
33 #ifdef HAVE_SYS_SOCKET_H
34 # include <sys/socket.h>
36 #ifdef HAVE_ARPA_INET_H
37 # include <arpa/inet.h>
52 #define NO_SHLWAPI_STREAM
53 #define NO_SHLWAPI_REG
54 #define NO_SHLWAPI_STRFCNS
55 #define NO_SHLWAPI_GDI
60 #include "wine/debug.h"
61 #include "wine/unicode.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
65 static const WCHAR g_szHttp1_0[] = {' ','H','T','T','P','/','1','.','0',0 };
66 static const WCHAR g_szHttp1_1[] = {' ','H','T','T','P','/','1','.','1',0 };
67 static const WCHAR g_szReferer[] = {'R','e','f','e','r','e','r',0};
68 static const WCHAR g_szAccept[] = {'A','c','c','e','p','t',0};
69 static const WCHAR g_szUserAgent[] = {'U','s','e','r','-','A','g','e','n','t',0};
70 static const WCHAR szHost[] = { 'H','o','s','t',0 };
71 static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
72 static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
73 static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 };
74 static const WCHAR szKeepAlive[] = {'K','e','e','p','-','A','l','i','v','e',0};
76 #define MAXHOSTNAME 100
77 #define MAX_FIELD_VALUE_LEN 256
78 #define MAX_FIELD_LEN 256
80 #define HTTP_REFERER g_szReferer
81 #define HTTP_ACCEPT g_szAccept
82 #define HTTP_USERAGENT g_szUserAgent
84 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
85 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
86 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
87 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
88 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
89 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
90 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
92 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
102 unsigned int auth_data_len;
103 BOOL finished; /* finished authenticating */
106 static void HTTP_CloseConnection(LPWININETHANDLEHEADER hdr);
107 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr);
108 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr);
109 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr);
110 static BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr);
111 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
112 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
113 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr);
114 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField, INT index, BOOL Request);
115 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index);
116 static LPWSTR HTTP_build_req( LPCWSTR *list, int len );
117 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD
118 dwInfoLevel, LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD
120 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl);
121 static UINT HTTP_DecodeBase64(LPCWSTR base64, LPSTR bin);
122 static BOOL HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field);
125 LPHTTPHEADERW HTTP_GetHeader(LPWININETHTTPREQW req, LPCWSTR head)
128 HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
129 if (HeaderIndex == -1)
132 return &req->pCustHeaders[HeaderIndex];
135 /***********************************************************************
136 * HTTP_Tokenize (internal)
138 * Tokenize a string, allocating memory for the tokens.
140 static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
142 LPWSTR * token_array;
147 /* empty string has no tokens */
151 for (i = 0; string[i]; i++)
152 if (!strncmpW(string+i, token_string, strlenW(token_string)))
156 /* we want to skip over separators, but not the null terminator */
157 for (j = 0; j < strlenW(token_string) - 1; j++)
163 /* add 1 for terminating NULL */
164 token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
165 token_array[tokens] = NULL;
168 for (i = 0; i < tokens; i++)
171 next_token = strstrW(string, token_string);
172 if (!next_token) next_token = string+strlenW(string);
173 len = next_token - string;
174 token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
175 memcpy(token_array[i], string, len*sizeof(WCHAR));
176 token_array[i][len] = '\0';
177 string = next_token+strlenW(token_string);
182 /***********************************************************************
183 * HTTP_FreeTokens (internal)
185 * Frees memory returned from HTTP_Tokenize.
187 static void HTTP_FreeTokens(LPWSTR * token_array)
190 for (i = 0; token_array[i]; i++)
191 HeapFree(GetProcessHeap(), 0, token_array[i]);
192 HeapFree(GetProcessHeap(), 0, token_array);
195 /* **********************************************************************
197 * Helper functions for the HttpSendRequest(Ex) functions
200 static void AsyncHttpSendRequestProc(WORKREQUEST *workRequest)
202 struct WORKREQ_HTTPSENDREQUESTW const *req = &workRequest->u.HttpSendRequestW;
203 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) workRequest->hdr;
205 TRACE("%p\n", lpwhr);
207 HTTP_HttpSendRequestW(lpwhr, req->lpszHeader,
208 req->dwHeaderLength, req->lpOptional, req->dwOptionalLength,
209 req->dwContentLength, req->bEndRequest);
211 HeapFree(GetProcessHeap(), 0, req->lpszHeader);
214 static void HTTP_FixVerb( LPWININETHTTPREQW lpwhr )
216 /* if the verb is NULL default to GET */
217 if (NULL == lpwhr->lpszVerb)
219 static const WCHAR szGET[] = { 'G','E','T', 0 };
220 lpwhr->lpszVerb = WININET_strdupW(szGET);
224 static void HTTP_FixURL( LPWININETHTTPREQW lpwhr)
226 static const WCHAR szSlash[] = { '/',0 };
227 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
229 /* If we don't have a path we set it to root */
230 if (NULL == lpwhr->lpszPath)
231 lpwhr->lpszPath = WININET_strdupW(szSlash);
232 else /* remove \r and \n*/
234 int nLen = strlenW(lpwhr->lpszPath);
235 while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
238 lpwhr->lpszPath[nLen]='\0';
240 /* Replace '\' with '/' */
243 if (lpwhr->lpszPath[nLen] == '\\') lpwhr->lpszPath[nLen]='/';
247 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
248 lpwhr->lpszPath, strlenW(lpwhr->lpszPath), szHttp, strlenW(szHttp) )
249 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
251 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
252 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
254 strcpyW(fixurl + 1, lpwhr->lpszPath);
255 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
256 lpwhr->lpszPath = fixurl;
260 static LPWSTR HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr, LPCWSTR verb, LPCWSTR path, BOOL http1_1 )
262 LPWSTR requestString;
268 static const WCHAR szSpace[] = { ' ',0 };
269 static const WCHAR szcrlf[] = {'\r','\n', 0};
270 static const WCHAR szColon[] = { ':',' ',0 };
271 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
273 /* allocate space for an array of all the string pointers to be added */
274 len = (lpwhr->nCustHeaders)*4 + 9;
275 req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
277 /* add the verb, path and HTTP version string */
282 req[n++] = http1_1 ? g_szHttp1_1 : g_szHttp1_0;
284 /* Append custom request headers */
285 for (i = 0; i < lpwhr->nCustHeaders; i++)
287 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
290 req[n++] = lpwhr->pCustHeaders[i].lpszField;
292 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
294 TRACE("Adding custom header %s (%s)\n",
295 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
296 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
301 ERR("oops. buffer overrun\n");
304 requestString = HTTP_build_req( req, 4 );
305 HeapFree( GetProcessHeap(), 0, req );
308 * Set (header) termination string for request
309 * Make sure there's exactly two new lines at the end of the request
311 p = &requestString[strlenW(requestString)-1];
312 while ( (*p == '\n') || (*p == '\r') )
314 strcpyW( p+1, sztwocrlf );
316 return requestString;
319 static void HTTP_ProcessHeaders( LPWININETHTTPREQW lpwhr )
321 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
323 LPHTTPHEADERW setCookieHeader;
325 HeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSet_Cookie, 0, FALSE);
326 if (HeaderIndex == -1)
328 setCookieHeader = &lpwhr->pCustHeaders[HeaderIndex];
330 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && setCookieHeader->lpszValue)
332 int nPosStart = 0, nPosEnd = 0, len;
333 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
335 while (setCookieHeader->lpszValue[nPosEnd] != '\0')
337 LPWSTR buf_cookie, cookie_name, cookie_data;
339 LPWSTR domain = NULL;
343 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
344 setCookieHeader->lpszValue[nPosEnd] != '\0')
348 if (setCookieHeader->lpszValue[nPosEnd] == ';')
350 /* fixme: not case sensitive, strcasestr is gnu only */
351 int nDomainPosEnd = 0;
352 int nDomainPosStart = 0, nDomainLength = 0;
353 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
354 LPWSTR lpszDomain = strstrW(&setCookieHeader->lpszValue[nPosEnd], szDomain);
356 { /* they have specified their own domain, lets use it */
357 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
358 lpszDomain[nDomainPosEnd] != '\0')
362 nDomainPosStart = strlenW(szDomain);
363 nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
364 domain = HeapAlloc(GetProcessHeap(), 0, (nDomainLength + 1)*sizeof(WCHAR));
365 lstrcpynW(domain, &lpszDomain[nDomainPosStart], nDomainLength + 1);
368 if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
369 buf_cookie = HeapAlloc(GetProcessHeap(), 0, ((nPosEnd - nPosStart) + 1)*sizeof(WCHAR));
370 lstrcpynW(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart) + 1);
371 TRACE("%s\n", debugstr_w(buf_cookie));
372 while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
376 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
378 HeapFree(GetProcessHeap(), 0, buf_cookie);
382 cookie_name = HeapAlloc(GetProcessHeap(), 0, (nEqualPos + 1)*sizeof(WCHAR));
383 lstrcpynW(cookie_name, buf_cookie, nEqualPos + 1);
384 cookie_data = &buf_cookie[nEqualPos + 1];
386 Host = HTTP_GetHeader(lpwhr,szHost);
387 len = lstrlenW((domain ? domain : (Host?Host->lpszValue:NULL))) +
388 strlenW(lpwhr->lpszPath) + 9;
389 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
390 sprintfW(buf_url, szFmt, (domain ? domain : (Host?Host->lpszValue:NULL))); /* FIXME PATH!!! */
391 InternetSetCookieW(buf_url, cookie_name, cookie_data);
393 HeapFree(GetProcessHeap(), 0, buf_url);
394 HeapFree(GetProcessHeap(), 0, buf_cookie);
395 HeapFree(GetProcessHeap(), 0, cookie_name);
396 HeapFree(GetProcessHeap(), 0, domain);
402 static inline BOOL is_basic_auth_value( LPCWSTR pszAuthValue )
404 static const WCHAR szBasic[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
405 return !strncmpiW(pszAuthValue, szBasic, ARRAYSIZE(szBasic)) &&
406 ((pszAuthValue[ARRAYSIZE(szBasic)] != ' ') || !pszAuthValue[ARRAYSIZE(szBasic)]);
409 static BOOL HTTP_DoAuthorization( LPWININETHTTPREQW lpwhr, LPCWSTR pszAuthValue,
410 struct HttpAuthInfo **ppAuthInfo,
411 LPWSTR domain_and_username, LPWSTR password )
413 SECURITY_STATUS sec_status;
414 struct HttpAuthInfo *pAuthInfo = *ppAuthInfo;
417 TRACE("%s\n", debugstr_w(pszAuthValue));
419 if (!domain_and_username) return FALSE;
426 pAuthInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*pAuthInfo));
430 SecInvalidateHandle(&pAuthInfo->cred);
431 SecInvalidateHandle(&pAuthInfo->ctx);
432 memset(&pAuthInfo->exp, 0, sizeof(pAuthInfo->exp));
434 pAuthInfo->auth_data = NULL;
435 pAuthInfo->auth_data_len = 0;
436 pAuthInfo->finished = FALSE;
438 if (is_basic_auth_value(pszAuthValue))
440 static const WCHAR szBasic[] = {'B','a','s','i','c',0};
441 pAuthInfo->scheme = WININET_strdupW(szBasic);
442 if (!pAuthInfo->scheme)
444 HeapFree(GetProcessHeap(), 0, pAuthInfo);
450 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity;
451 WCHAR *user = strchrW(domain_and_username, '\\');
452 WCHAR *domain = domain_and_username;
454 pAuthInfo->scheme = WININET_strdupW(pszAuthValue);
455 if (!pAuthInfo->scheme)
457 HeapFree(GetProcessHeap(), 0, pAuthInfo);
464 user = domain_and_username;
467 nt_auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
468 nt_auth_identity.User = user;
469 nt_auth_identity.UserLength = strlenW(nt_auth_identity.User);
470 nt_auth_identity.Domain = domain;
471 nt_auth_identity.DomainLength = domain ? user - domain - 1 : 0;
472 nt_auth_identity.Password = password;
473 nt_auth_identity.PasswordLength = strlenW(nt_auth_identity.Password);
475 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
477 sec_status = AcquireCredentialsHandleW(NULL, pAuthInfo->scheme,
478 SECPKG_CRED_OUTBOUND, NULL,
479 &nt_auth_identity, NULL,
480 NULL, &pAuthInfo->cred,
482 if (sec_status != SEC_E_OK)
484 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
485 debugstr_w(pAuthInfo->scheme), sec_status);
486 HeapFree(GetProcessHeap(), 0, pAuthInfo->scheme);
487 HeapFree(GetProcessHeap(), 0, pAuthInfo);
491 *ppAuthInfo = pAuthInfo;
493 else if (pAuthInfo->finished)
496 if ((strlenW(pszAuthValue) < strlenW(pAuthInfo->scheme)) ||
497 strncmpiW(pszAuthValue, pAuthInfo->scheme, strlenW(pAuthInfo->scheme)))
499 ERR("authentication scheme changed from %s to %s\n",
500 debugstr_w(pAuthInfo->scheme), debugstr_w(pszAuthValue));
504 if (is_basic_auth_value(pszAuthValue))
506 int userlen = WideCharToMultiByte(CP_UTF8, 0, domain_and_username, lstrlenW(domain_and_username), NULL, 0, NULL, NULL);
507 int passlen = WideCharToMultiByte(CP_UTF8, 0, password, lstrlenW(password), NULL, 0, NULL, NULL);
510 TRACE("basic authentication\n");
512 /* length includes a nul terminator, which will be re-used for the ':' */
513 auth_data = HeapAlloc(GetProcessHeap(), 0, userlen + 1 + passlen);
517 WideCharToMultiByte(CP_UTF8, 0, domain_and_username, -1, auth_data, userlen, NULL, NULL);
518 auth_data[userlen] = ':';
519 WideCharToMultiByte(CP_UTF8, 0, password, -1, &auth_data[userlen+1], passlen, NULL, NULL);
521 pAuthInfo->auth_data = auth_data;
522 pAuthInfo->auth_data_len = userlen + 1 + passlen;
523 pAuthInfo->finished = TRUE;
530 SecBufferDesc out_desc, in_desc;
532 unsigned char *buffer;
533 ULONG context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE |
534 ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE;
536 in.BufferType = SECBUFFER_TOKEN;
540 in_desc.ulVersion = 0;
541 in_desc.cBuffers = 1;
542 in_desc.pBuffers = ∈
544 pszAuthData = pszAuthValue + strlenW(pAuthInfo->scheme);
545 if (*pszAuthData == ' ')
548 in.cbBuffer = HTTP_DecodeBase64(pszAuthData, NULL);
549 in.pvBuffer = HeapAlloc(GetProcessHeap(), 0, in.cbBuffer);
550 HTTP_DecodeBase64(pszAuthData, in.pvBuffer);
553 buffer = HeapAlloc(GetProcessHeap(), 0, 0x100);
555 out.BufferType = SECBUFFER_TOKEN;
556 out.cbBuffer = 0x100;
557 out.pvBuffer = buffer;
559 out_desc.ulVersion = 0;
560 out_desc.cBuffers = 1;
561 out_desc.pBuffers = &out;
563 sec_status = InitializeSecurityContextW(first ? &pAuthInfo->cred : NULL,
564 first ? NULL : &pAuthInfo->ctx,
565 first ? lpwhr->lpHttpSession->lpszServerName : NULL,
566 context_req, 0, SECURITY_NETWORK_DREP,
567 in.pvBuffer ? &in_desc : NULL,
568 0, &pAuthInfo->ctx, &out_desc,
569 &pAuthInfo->attr, &pAuthInfo->exp);
570 if (sec_status == SEC_E_OK)
572 pAuthInfo->finished = TRUE;
573 pAuthInfo->auth_data = out.pvBuffer;
574 pAuthInfo->auth_data_len = out.cbBuffer;
575 TRACE("sending last auth packet\n");
577 else if (sec_status == SEC_I_CONTINUE_NEEDED)
579 pAuthInfo->auth_data = out.pvBuffer;
580 pAuthInfo->auth_data_len = out.cbBuffer;
581 TRACE("sending next auth packet\n");
585 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status);
586 HeapFree(GetProcessHeap(), 0, out.pvBuffer);
594 /***********************************************************************
595 * HTTP_HttpAddRequestHeadersW (internal)
597 static BOOL WINAPI HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
598 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
603 BOOL bSuccess = FALSE;
606 TRACE("copying header: %s\n", debugstr_w(lpszHeader));
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 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
642 bSuccess = HTTP_VerifyValidHeader(lpwhr, pFieldAndValue[0]);
644 bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
645 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
646 HTTP_FreeTokens(pFieldAndValue);
652 HeapFree(GetProcessHeap(), 0, buffer);
657 /***********************************************************************
658 * HttpAddRequestHeadersW (WININET.@)
660 * Adds one or more HTTP header to the request handler
667 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
668 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
670 BOOL bSuccess = FALSE;
671 LPWININETHTTPREQW lpwhr;
673 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_w(lpszHeader), dwHeaderLength,
679 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
680 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
682 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
685 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
688 WININET_Release( &lpwhr->hdr );
693 /***********************************************************************
694 * HttpAddRequestHeadersA (WININET.@)
696 * Adds one or more HTTP header to the request handler
703 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
704 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
710 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_a(lpszHeader), dwHeaderLength,
713 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
714 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
715 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
716 if( dwHeaderLength != ~0U )
717 dwHeaderLength = len;
719 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
721 HeapFree( GetProcessHeap(), 0, hdr );
726 /* read any content returned by the server so that the connection can be
728 static void HTTP_DrainContent(LPWININETHTTPREQW lpwhr)
732 if (!NETCON_connected(&lpwhr->netConnection)) return;
734 if (lpwhr->dwContentLength == -1)
735 NETCON_close(&lpwhr->netConnection);
740 if (!INTERNET_ReadFile(&lpwhr->hdr, buffer, sizeof(buffer), &bytes_read,
743 } while (bytes_read);
746 /***********************************************************************
747 * HttpEndRequestA (WININET.@)
749 * Ends an HTTP request that was started by HttpSendRequestEx
756 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
757 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
759 LPINTERNET_BUFFERSA ptr;
760 LPINTERNET_BUFFERSW lpBuffersOutW,ptrW;
763 TRACE("(%p, %p, %08x, %08lx): stub\n", hRequest, lpBuffersOut, dwFlags,
768 lpBuffersOutW = (LPINTERNET_BUFFERSW)HeapAlloc(GetProcessHeap(),
769 HEAP_ZERO_MEMORY, sizeof(INTERNET_BUFFERSW));
771 lpBuffersOutW = NULL;
773 ptrW = lpBuffersOutW;
776 if (ptr->lpvBuffer && ptr->dwBufferLength)
777 ptrW->lpvBuffer = HeapAlloc(GetProcessHeap(),0,ptr->dwBufferLength);
778 ptrW->dwBufferLength = ptr->dwBufferLength;
779 ptrW->dwBufferTotal= ptr->dwBufferTotal;
782 ptrW->Next = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
783 sizeof(INTERNET_BUFFERSW));
789 rc = HttpEndRequestW(hRequest, lpBuffersOutW, dwFlags, dwContext);
793 ptrW = lpBuffersOutW;
796 LPINTERNET_BUFFERSW ptrW2;
798 FIXME("Do we need to translate info out of these buffer?\n");
800 HeapFree(GetProcessHeap(),0,(LPVOID)ptrW->lpvBuffer);
802 HeapFree(GetProcessHeap(),0,ptrW);
810 /***********************************************************************
811 * HttpEndRequestW (WININET.@)
813 * Ends an HTTP request that was started by HttpSendRequestEx
820 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
821 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
824 LPWININETHTTPREQW lpwhr;
829 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
831 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
833 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
835 WININET_Release( &lpwhr->hdr );
839 lpwhr->hdr.dwFlags |= dwFlags;
840 lpwhr->hdr.dwContext = dwContext;
842 /* We appear to do nothing with lpBuffersOut.. is that correct? */
844 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
845 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
847 responseLen = HTTP_GetResponseHeaders(lpwhr);
851 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
852 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
854 /* process headers here. Is this right? */
855 HTTP_ProcessHeaders(lpwhr);
857 dwBufferSize = sizeof(lpwhr->dwContentLength);
858 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
859 &lpwhr->dwContentLength,&dwBufferSize,NULL))
860 lpwhr->dwContentLength = -1;
862 if (lpwhr->dwContentLength == 0)
863 HTTP_FinishedReading(lpwhr);
865 if(!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
867 DWORD dwCode,dwCodeLength=sizeof(DWORD);
868 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,NULL) &&
869 (dwCode==302 || dwCode==301))
871 WCHAR szNewLocation[2048];
872 dwBufferSize=sizeof(szNewLocation);
873 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL))
875 static const WCHAR szGET[] = { 'G','E','T', 0 };
876 /* redirects are always GETs */
877 HeapFree(GetProcessHeap(),0,lpwhr->lpszVerb);
878 lpwhr->lpszVerb = WININET_strdupW(szGET);
879 HTTP_DrainContent(lpwhr);
880 rc = HTTP_HandleRedirect(lpwhr, szNewLocation);
882 rc = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, TRUE);
887 WININET_Release( &lpwhr->hdr );
888 TRACE("%i <--\n",rc);
892 /***********************************************************************
893 * HttpOpenRequestW (WININET.@)
895 * Open a HTTP request handle
898 * HINTERNET a HTTP request handle on success
902 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
903 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
904 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
905 DWORD dwFlags, DWORD_PTR dwContext)
907 LPWININETHTTPSESSIONW lpwhs;
908 HINTERNET handle = NULL;
910 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
911 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
912 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
914 if(lpszAcceptTypes!=NULL)
917 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
918 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
921 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
922 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
924 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
929 * My tests seem to show that the windows version does not
930 * become asynchronous until after this point. And anyhow
931 * if this call was asynchronous then how would you get the
932 * necessary HINTERNET pointer returned by this function.
935 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
936 lpszVersion, lpszReferrer, lpszAcceptTypes,
940 WININET_Release( &lpwhs->hdr );
941 TRACE("returning %p\n", handle);
946 /***********************************************************************
947 * HttpOpenRequestA (WININET.@)
949 * Open a HTTP request handle
952 * HINTERNET a HTTP request handle on success
956 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
957 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
958 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
959 DWORD dwFlags, DWORD_PTR dwContext)
961 LPWSTR szVerb = NULL, szObjectName = NULL;
962 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
964 INT acceptTypesCount;
965 HINTERNET rc = FALSE;
966 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
967 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
968 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
973 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
974 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
977 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
982 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
983 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
986 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
991 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
992 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
995 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
1000 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
1001 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1004 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
1007 acceptTypesCount = 0;
1008 if (lpszAcceptTypes)
1010 /* find out how many there are */
1011 while (lpszAcceptTypes[acceptTypesCount] && *lpszAcceptTypes[acceptTypesCount])
1013 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
1014 acceptTypesCount = 0;
1015 while (lpszAcceptTypes[acceptTypesCount] && *lpszAcceptTypes[acceptTypesCount])
1017 len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
1019 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1020 if (!szAcceptTypes[acceptTypesCount] )
1022 MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
1023 -1, szAcceptTypes[acceptTypesCount], len );
1026 szAcceptTypes[acceptTypesCount] = NULL;
1028 else szAcceptTypes = 0;
1030 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
1031 szVersion, szReferrer,
1032 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
1037 acceptTypesCount = 0;
1038 while (szAcceptTypes[acceptTypesCount])
1040 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
1043 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
1045 HeapFree(GetProcessHeap(), 0, szReferrer);
1046 HeapFree(GetProcessHeap(), 0, szVersion);
1047 HeapFree(GetProcessHeap(), 0, szObjectName);
1048 HeapFree(GetProcessHeap(), 0, szVerb);
1053 /***********************************************************************
1056 static UINT HTTP_EncodeBase64( LPCSTR bin, unsigned int len, LPWSTR base64 )
1059 static const CHAR HTTP_Base64Enc[] =
1060 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1064 /* first 6 bits, all from bin[0] */
1065 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
1066 x = (bin[0] & 3) << 4;
1068 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1071 base64[n++] = HTTP_Base64Enc[x];
1076 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
1077 x = ( bin[1] & 0x0f ) << 2;
1079 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1082 base64[n++] = HTTP_Base64Enc[x];
1086 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
1088 /* last 6 bits, all from bin [2] */
1089 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
1097 #define CH(x) (((x) >= 'A' && (x) <= 'Z') ? (x) - 'A' : \
1098 ((x) >= 'a' && (x) <= 'z') ? (x) - 'a' + 26 : \
1099 ((x) >= '0' && (x) <= '9') ? (x) - '0' + 52 : \
1100 ((x) == '+') ? 62 : ((x) == '/') ? 63 : -1)
1101 static const signed char HTTP_Base64Dec[256] =
1103 CH( 0),CH( 1),CH( 2),CH( 3),CH( 4),CH( 5),CH( 6),CH( 7),CH( 8),CH( 9),
1104 CH(10),CH(11),CH(12),CH(13),CH(14),CH(15),CH(16),CH(17),CH(18),CH(19),
1105 CH(20),CH(21),CH(22),CH(23),CH(24),CH(25),CH(26),CH(27),CH(28),CH(29),
1106 CH(30),CH(31),CH(32),CH(33),CH(34),CH(35),CH(36),CH(37),CH(38),CH(39),
1107 CH(40),CH(41),CH(42),CH(43),CH(44),CH(45),CH(46),CH(47),CH(48),CH(49),
1108 CH(50),CH(51),CH(52),CH(53),CH(54),CH(55),CH(56),CH(57),CH(58),CH(59),
1109 CH(60),CH(61),CH(62),CH(63),CH(64),CH(65),CH(66),CH(67),CH(68),CH(69),
1110 CH(70),CH(71),CH(72),CH(73),CH(74),CH(75),CH(76),CH(77),CH(78),CH(79),
1111 CH(80),CH(81),CH(82),CH(83),CH(84),CH(85),CH(86),CH(87),CH(88),CH(89),
1112 CH(90),CH(91),CH(92),CH(93),CH(94),CH(95),CH(96),CH(97),CH(98),CH(99),
1113 CH(100),CH(101),CH(102),CH(103),CH(104),CH(105),CH(106),CH(107),CH(108),CH(109),
1114 CH(110),CH(111),CH(112),CH(113),CH(114),CH(115),CH(116),CH(117),CH(118),CH(119),
1115 CH(120),CH(121),CH(122),CH(123),CH(124),CH(125),CH(126),CH(127),CH(128),CH(129),
1116 CH(130),CH(131),CH(132),CH(133),CH(134),CH(135),CH(136),CH(137),CH(138),CH(139),
1117 CH(140),CH(141),CH(142),CH(143),CH(144),CH(145),CH(146),CH(147),CH(148),CH(149),
1118 CH(150),CH(151),CH(152),CH(153),CH(154),CH(155),CH(156),CH(157),CH(158),CH(159),
1119 CH(160),CH(161),CH(162),CH(163),CH(164),CH(165),CH(166),CH(167),CH(168),CH(169),
1120 CH(170),CH(171),CH(172),CH(173),CH(174),CH(175),CH(176),CH(177),CH(178),CH(179),
1121 CH(180),CH(181),CH(182),CH(183),CH(184),CH(185),CH(186),CH(187),CH(188),CH(189),
1122 CH(190),CH(191),CH(192),CH(193),CH(194),CH(195),CH(196),CH(197),CH(198),CH(199),
1123 CH(200),CH(201),CH(202),CH(203),CH(204),CH(205),CH(206),CH(207),CH(208),CH(209),
1124 CH(210),CH(211),CH(212),CH(213),CH(214),CH(215),CH(216),CH(217),CH(218),CH(219),
1125 CH(220),CH(221),CH(222),CH(223),CH(224),CH(225),CH(226),CH(227),CH(228),CH(229),
1126 CH(230),CH(231),CH(232),CH(233),CH(234),CH(235),CH(236),CH(237),CH(238),CH(239),
1127 CH(240),CH(241),CH(242),CH(243),CH(244),CH(245),CH(246),CH(247),CH(248), CH(249),
1128 CH(250),CH(251),CH(252),CH(253),CH(254),CH(255),
1132 /***********************************************************************
1135 static UINT HTTP_DecodeBase64( LPCWSTR base64, LPSTR bin )
1143 if (base64[0] > ARRAYSIZE(HTTP_Base64Dec) ||
1144 ((in[0] = HTTP_Base64Dec[base64[0]]) == -1) ||
1145 base64[1] > ARRAYSIZE(HTTP_Base64Dec) ||
1146 ((in[1] = HTTP_Base64Dec[base64[1]]) == -1))
1148 WARN("invalid base64: %s\n", debugstr_w(base64));
1152 bin[n] = (unsigned char) (in[0] << 2 | in[1] >> 4);
1155 if ((base64[2] == '=') && (base64[3] == '='))
1157 if (base64[2] > ARRAYSIZE(HTTP_Base64Dec) ||
1158 ((in[2] = HTTP_Base64Dec[base64[2]]) == -1))
1160 WARN("invalid base64: %s\n", debugstr_w(&base64[2]));
1164 bin[n] = (unsigned char) (in[1] << 4 | in[2] >> 2);
1167 if (base64[3] == '=')
1169 if (base64[3] > ARRAYSIZE(HTTP_Base64Dec) ||
1170 ((in[3] = HTTP_Base64Dec[base64[3]]) == -1))
1172 WARN("invalid base64: %s\n", debugstr_w(&base64[3]));
1176 bin[n] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
1185 /***********************************************************************
1186 * HTTP_InsertAuthorizationForHeader
1188 * Insert or delete the authorization field in the request header.
1190 static BOOL HTTP_InsertAuthorizationForHeader( LPWININETHTTPREQW lpwhr, struct HttpAuthInfo *pAuthInfo, LPCWSTR header )
1192 WCHAR *authorization = NULL;
1194 if (pAuthInfo && pAuthInfo->auth_data_len)
1196 static const WCHAR wszSpace[] = {' ',0};
1197 static const WCHAR wszBasic[] = {'B','a','s','i','c',0};
1200 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1201 len = strlenW(pAuthInfo->scheme)+1+((pAuthInfo->auth_data_len+2)*4)/3;
1202 authorization = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
1206 strcpyW(authorization, pAuthInfo->scheme);
1207 strcatW(authorization, wszSpace);
1208 HTTP_EncodeBase64(pAuthInfo->auth_data,
1209 pAuthInfo->auth_data_len,
1210 authorization+strlenW(authorization));
1212 /* clear the data as it isn't valid now that it has been sent to the
1213 * server, unless it's Basic authentication which doesn't do
1214 * connection tracking */
1215 if (strcmpiW(pAuthInfo->scheme, wszBasic))
1217 HeapFree(GetProcessHeap(), 0, pAuthInfo->auth_data);
1218 pAuthInfo->auth_data = NULL;
1219 pAuthInfo->auth_data_len = 0;
1223 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1225 HTTP_ProcessHeader(lpwhr, header, authorization,
1226 HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1228 HeapFree(GetProcessHeap(), 0, authorization);
1233 /***********************************************************************
1234 * HTTP_InsertAuthorization
1236 * Insert the authorization field in the request header
1238 static BOOL HTTP_InsertAuthorization( LPWININETHTTPREQW lpwhr )
1240 return HTTP_InsertAuthorizationForHeader(lpwhr, lpwhr->pAuthInfo, szAuthorization);
1243 /***********************************************************************
1244 * HTTP_InsertProxyAuthorization
1246 * Insert the proxy authorization field in the request header
1248 static BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr )
1250 return HTTP_InsertAuthorizationForHeader(lpwhr, lpwhr->pProxyAuthInfo, szProxy_Authorization);
1253 /***********************************************************************
1254 * HTTP_DealWithProxy
1256 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
1257 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
1259 WCHAR buf[MAXHOSTNAME];
1260 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
1262 static WCHAR szNul[] = { 0 };
1263 URL_COMPONENTSW UrlComponents;
1264 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }, szSlash[] = { '/',0 } ;
1265 static const WCHAR szFormat1[] = { 'h','t','t','p',':','/','/','%','s',0 };
1266 static const WCHAR szFormat2[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
1269 memset( &UrlComponents, 0, sizeof UrlComponents );
1270 UrlComponents.dwStructSize = sizeof UrlComponents;
1271 UrlComponents.lpszHostName = buf;
1272 UrlComponents.dwHostNameLength = MAXHOSTNAME;
1274 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1275 hIC->lpszProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
1276 sprintfW(proxy, szFormat1, hIC->lpszProxy);
1278 strcpyW(proxy, hIC->lpszProxy);
1279 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
1281 if( UrlComponents.dwHostNameLength == 0 )
1284 if( !lpwhr->lpszPath )
1285 lpwhr->lpszPath = szNul;
1286 TRACE("server=%s path=%s\n",
1287 debugstr_w(lpwhs->lpszHostName), debugstr_w(lpwhr->lpszPath));
1288 /* for constant 15 see above */
1289 len = strlenW(lpwhs->lpszHostName) + strlenW(lpwhr->lpszPath) + 15;
1290 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1292 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1293 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1295 sprintfW(url, szFormat2, lpwhs->lpszHostName, lpwhs->nHostPort);
1297 if( lpwhr->lpszPath[0] != '/' )
1298 strcatW( url, szSlash );
1299 strcatW(url, lpwhr->lpszPath);
1300 if(lpwhr->lpszPath != szNul)
1301 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1302 lpwhr->lpszPath = url;
1304 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1305 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
1306 lpwhs->nServerPort = UrlComponents.nPort;
1311 static BOOL HTTP_ResolveName(LPWININETHTTPREQW lpwhr)
1314 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
1316 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1317 INTERNET_STATUS_RESOLVING_NAME,
1318 lpwhs->lpszServerName,
1319 strlenW(lpwhs->lpszServerName)+1);
1321 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1322 &lpwhs->socketAddress))
1324 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1328 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
1329 szaddr, sizeof(szaddr));
1330 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1331 INTERNET_STATUS_NAME_RESOLVED,
1332 szaddr, strlen(szaddr)+1);
1336 /***********************************************************************
1337 * HTTP_HttpOpenRequestW (internal)
1339 * Open a HTTP request handle
1342 * HINTERNET a HTTP request handle on success
1346 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
1347 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
1348 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
1349 DWORD dwFlags, DWORD_PTR dwContext)
1351 LPWININETAPPINFOW hIC = NULL;
1352 LPWININETHTTPREQW lpwhr;
1354 LPWSTR lpszUrl = NULL;
1356 HINTERNET handle = NULL;
1357 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
1363 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
1364 hIC = lpwhs->lpAppInfo;
1366 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
1369 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1372 lpwhr->hdr.htype = WH_HHTTPREQ;
1373 lpwhr->hdr.dwFlags = dwFlags;
1374 lpwhr->hdr.dwContext = dwContext;
1375 lpwhr->hdr.dwRefCount = 1;
1376 lpwhr->hdr.close_connection = HTTP_CloseConnection;
1377 lpwhr->hdr.destroy = HTTP_CloseHTTPRequestHandle;
1378 lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
1379 lpwhr->hdr.dwInternalFlags = lpwhs->hdr.dwInternalFlags & INET_CALLBACKW;
1381 WININET_AddRef( &lpwhs->hdr );
1382 lpwhr->lpHttpSession = lpwhs;
1383 list_add_head( &lpwhs->hdr.children, &lpwhr->hdr.entry );
1385 handle = WININET_AllocHandle( &lpwhr->hdr );
1388 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1392 if (!NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE))
1394 InternetCloseHandle( handle );
1399 if (NULL != lpszObjectName && strlenW(lpszObjectName)) {
1403 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
1404 if (rc != E_POINTER)
1405 len = strlenW(lpszObjectName)+1;
1406 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1407 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
1408 URL_ESCAPE_SPACES_ONLY);
1411 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc);
1412 strcpyW(lpwhr->lpszPath,lpszObjectName);
1416 if (NULL != lpszReferrer && strlenW(lpszReferrer))
1417 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
1419 if (lpszAcceptTypes)
1422 for (i = 0; lpszAcceptTypes[i]; i++)
1424 if (!*lpszAcceptTypes[i]) continue;
1425 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i],
1426 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA |
1427 HTTP_ADDHDR_FLAG_REQ |
1428 (i == 0 ? HTTP_ADDHDR_FLAG_REPLACE : 0));
1432 if (NULL == lpszVerb)
1434 static const WCHAR szGet[] = {'G','E','T',0};
1435 lpwhr->lpszVerb = WININET_strdupW(szGet);
1437 else if (strlenW(lpszVerb))
1438 lpwhr->lpszVerb = WININET_strdupW(lpszVerb);
1440 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
1442 if (lpwhs->nServerPort == INTERNET_INVALID_PORT_NUMBER)
1443 lpwhs->nServerPort = (dwFlags & INTERNET_FLAG_SECURE ?
1444 INTERNET_DEFAULT_HTTPS_PORT :
1445 INTERNET_DEFAULT_HTTP_PORT);
1446 lpwhs->nHostPort = lpwhs->nServerPort;
1448 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1449 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
1453 WCHAR *agent_header;
1454 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
1456 len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
1457 agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1458 sprintfW(agent_header, user_agent, hIC->lpszAgent );
1460 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
1461 HTTP_ADDREQ_FLAG_ADD);
1462 HeapFree(GetProcessHeap(), 0, agent_header);
1465 Host = HTTP_GetHeader(lpwhr,szHost);
1467 len = lstrlenW(Host->lpszValue) + strlenW(szUrlForm);
1468 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1469 sprintfW( lpszUrl, szUrlForm, Host->lpszValue );
1471 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
1472 InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
1475 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
1476 static const WCHAR szcrlf[] = {'\r','\n',0};
1478 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
1480 cnt += sprintfW(lpszCookies, szCookie);
1481 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
1482 strcatW(lpszCookies, szcrlf);
1484 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
1485 HTTP_ADDREQ_FLAG_ADD);
1486 HeapFree(GetProcessHeap(), 0, lpszCookies);
1488 HeapFree(GetProcessHeap(), 0, lpszUrl);
1491 INTERNET_SendCallback(&lpwhs->hdr, dwContext,
1492 INTERNET_STATUS_HANDLE_CREATED, &handle,
1496 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
1499 if (!HTTP_ResolveName(lpwhr))
1501 InternetCloseHandle( handle );
1507 WININET_Release( &lpwhr->hdr );
1509 TRACE("<-- %p (%p)\n", handle, lpwhr);
1513 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
1514 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
1515 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
1516 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
1517 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
1518 static const WCHAR szAge[] = { 'A','g','e',0 };
1519 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
1520 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
1521 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
1522 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
1523 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
1524 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
1525 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
1526 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
1527 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
1528 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
1529 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
1530 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 };
1531 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
1532 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
1533 static const WCHAR szDate[] = { 'D','a','t','e',0 };
1534 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
1535 static const WCHAR szETag[] = { 'E','T','a','g',0 };
1536 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
1537 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
1538 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
1539 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1540 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
1541 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
1542 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1543 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
1544 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
1545 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
1546 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
1547 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
1548 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1549 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
1550 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
1551 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
1552 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
1553 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
1554 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
1555 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
1556 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
1557 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 };
1558 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
1559 static const WCHAR szURI[] = { 'U','R','I',0 };
1560 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
1561 static const WCHAR szVary[] = { 'V','a','r','y',0 };
1562 static const WCHAR szVia[] = { 'V','i','a',0 };
1563 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
1564 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1566 static const LPCWSTR header_lookup[] = {
1567 szMime_Version, /* HTTP_QUERY_MIME_VERSION = 0 */
1568 szContent_Type, /* HTTP_QUERY_CONTENT_TYPE = 1 */
1569 szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
1570 szContent_ID, /* HTTP_QUERY_CONTENT_ID = 3 */
1571 NULL, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
1572 szContent_Length, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
1573 szContent_Language, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
1574 szAllow, /* HTTP_QUERY_ALLOW = 7 */
1575 szPublic, /* HTTP_QUERY_PUBLIC = 8 */
1576 szDate, /* HTTP_QUERY_DATE = 9 */
1577 szExpires, /* HTTP_QUERY_EXPIRES = 10 */
1578 szLast_Modified, /* HTTP_QUERY_LAST_MODIFIED = 11 */
1579 NULL, /* HTTP_QUERY_MESSAGE_ID = 12 */
1580 szURI, /* HTTP_QUERY_URI = 13 */
1581 szFrom, /* HTTP_QUERY_DERIVED_FROM = 14 */
1582 NULL, /* HTTP_QUERY_COST = 15 */
1583 NULL, /* HTTP_QUERY_LINK = 16 */
1584 szPragma, /* HTTP_QUERY_PRAGMA = 17 */
1585 NULL, /* HTTP_QUERY_VERSION = 18 */
1586 szStatus, /* HTTP_QUERY_STATUS_CODE = 19 */
1587 NULL, /* HTTP_QUERY_STATUS_TEXT = 20 */
1588 NULL, /* HTTP_QUERY_RAW_HEADERS = 21 */
1589 NULL, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
1590 szConnection, /* HTTP_QUERY_CONNECTION = 23 */
1591 szAccept, /* HTTP_QUERY_ACCEPT = 24 */
1592 szAccept_Charset, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
1593 szAccept_Encoding, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
1594 szAccept_Language, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
1595 szAuthorization, /* HTTP_QUERY_AUTHORIZATION = 28 */
1596 szContent_Encoding, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
1597 NULL, /* HTTP_QUERY_FORWARDED = 30 */
1598 NULL, /* HTTP_QUERY_FROM = 31 */
1599 szIf_Modified_Since, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
1600 szLocation, /* HTTP_QUERY_LOCATION = 33 */
1601 NULL, /* HTTP_QUERY_ORIG_URI = 34 */
1602 szReferer, /* HTTP_QUERY_REFERER = 35 */
1603 szRetry_After, /* HTTP_QUERY_RETRY_AFTER = 36 */
1604 szServer, /* HTTP_QUERY_SERVER = 37 */
1605 NULL, /* HTTP_TITLE = 38 */
1606 szUser_Agent, /* HTTP_QUERY_USER_AGENT = 39 */
1607 szWWW_Authenticate, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
1608 szProxy_Authenticate, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
1609 szAccept_Ranges, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
1610 szSet_Cookie, /* HTTP_QUERY_SET_COOKIE = 43 */
1611 szCookie, /* HTTP_QUERY_COOKIE = 44 */
1612 NULL, /* HTTP_QUERY_REQUEST_METHOD = 45 */
1613 NULL, /* HTTP_QUERY_REFRESH = 46 */
1614 NULL, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
1615 szAge, /* HTTP_QUERY_AGE = 48 */
1616 szCache_Control, /* HTTP_QUERY_CACHE_CONTROL = 49 */
1617 szContent_Base, /* HTTP_QUERY_CONTENT_BASE = 50 */
1618 szContent_Location, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
1619 szContent_MD5, /* HTTP_QUERY_CONTENT_MD5 = 52 */
1620 szContent_Range, /* HTTP_QUERY_CONTENT_RANGE = 53 */
1621 szETag, /* HTTP_QUERY_ETAG = 54 */
1622 szHost, /* HTTP_QUERY_HOST = 55 */
1623 szIf_Match, /* HTTP_QUERY_IF_MATCH = 56 */
1624 szIf_None_Match, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
1625 szIf_Range, /* HTTP_QUERY_IF_RANGE = 58 */
1626 szIf_Unmodified_Since, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
1627 szMax_Forwards, /* HTTP_QUERY_MAX_FORWARDS = 60 */
1628 szProxy_Authorization, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
1629 szRange, /* HTTP_QUERY_RANGE = 62 */
1630 szTransfer_Encoding, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
1631 szUpgrade, /* HTTP_QUERY_UPGRADE = 64 */
1632 szVary, /* HTTP_QUERY_VARY = 65 */
1633 szVia, /* HTTP_QUERY_VIA = 66 */
1634 szWarning, /* HTTP_QUERY_WARNING = 67 */
1635 szExpect, /* HTTP_QUERY_EXPECT = 68 */
1636 szProxy_Connection, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
1637 szUnless_Modified_Since, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
1640 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
1642 /***********************************************************************
1643 * HTTP_HttpQueryInfoW (internal)
1645 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
1646 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1648 LPHTTPHEADERW lphttpHdr = NULL;
1649 BOOL bSuccess = FALSE;
1650 BOOL request_only = dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS;
1651 INT requested_index = lpdwIndex ? *lpdwIndex : 0;
1652 INT level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
1655 /* Find requested header structure */
1658 case HTTP_QUERY_CUSTOM:
1659 index = HTTP_GetCustomHeaderIndex(lpwhr, lpBuffer, requested_index, request_only);
1662 case HTTP_QUERY_RAW_HEADERS_CRLF:
1669 headers = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, FALSE);
1671 headers = lpwhr->lpszRawHeaders;
1673 len = strlenW(headers);
1674 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1676 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1677 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1681 memcpy(lpBuffer, headers, (len+1)*sizeof(WCHAR));
1682 *lpdwBufferLength = len * sizeof(WCHAR);
1684 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1689 HeapFree(GetProcessHeap(), 0, headers);
1692 case HTTP_QUERY_RAW_HEADERS:
1694 static const WCHAR szCrLf[] = {'\r','\n',0};
1695 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
1697 LPWSTR pszString = (WCHAR*)lpBuffer;
1699 for (i = 0; ppszRawHeaderLines[i]; i++)
1700 size += strlenW(ppszRawHeaderLines[i]) + 1;
1702 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
1704 HTTP_FreeTokens(ppszRawHeaderLines);
1705 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
1706 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1710 for (i = 0; ppszRawHeaderLines[i]; i++)
1712 DWORD len = strlenW(ppszRawHeaderLines[i]);
1713 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
1718 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
1720 *lpdwBufferLength = size * sizeof(WCHAR);
1721 HTTP_FreeTokens(ppszRawHeaderLines);
1725 case HTTP_QUERY_STATUS_TEXT:
1726 if (lpwhr->lpszStatusText)
1728 DWORD len = strlenW(lpwhr->lpszStatusText);
1729 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1731 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1732 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1735 memcpy(lpBuffer, lpwhr->lpszStatusText, (len+1)*sizeof(WCHAR));
1736 *lpdwBufferLength = len * sizeof(WCHAR);
1738 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1743 case HTTP_QUERY_VERSION:
1744 if (lpwhr->lpszVersion)
1746 DWORD len = strlenW(lpwhr->lpszVersion);
1747 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1749 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1750 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1753 memcpy(lpBuffer, lpwhr->lpszVersion, (len+1)*sizeof(WCHAR));
1754 *lpdwBufferLength = len * sizeof(WCHAR);
1756 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1762 assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
1764 if (level >= 0 && level < LAST_TABLE_HEADER && header_lookup[level])
1765 index = HTTP_GetCustomHeaderIndex(lpwhr, header_lookup[level],
1766 requested_index,request_only);
1770 lphttpHdr = &lpwhr->pCustHeaders[index];
1772 /* Ensure header satisfies requested attributes */
1774 ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
1775 (~lphttpHdr->wFlags & HDR_ISREQUEST)))
1777 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1784 /* coalesce value to requested type */
1785 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
1787 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
1790 TRACE(" returning number : %d\n", *(int *)lpBuffer);
1792 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
1798 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
1800 tmpTM = *gmtime(&tmpTime);
1801 STHook = (SYSTEMTIME *) lpBuffer;
1805 STHook->wDay = tmpTM.tm_mday;
1806 STHook->wHour = tmpTM.tm_hour;
1807 STHook->wMilliseconds = 0;
1808 STHook->wMinute = tmpTM.tm_min;
1809 STHook->wDayOfWeek = tmpTM.tm_wday;
1810 STHook->wMonth = tmpTM.tm_mon + 1;
1811 STHook->wSecond = tmpTM.tm_sec;
1812 STHook->wYear = tmpTM.tm_year;
1816 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
1817 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
1818 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
1820 else if (lphttpHdr->lpszValue)
1822 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
1824 if (len > *lpdwBufferLength)
1826 *lpdwBufferLength = len;
1827 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1831 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
1832 *lpdwBufferLength = len - sizeof(WCHAR);
1835 TRACE(" returning string : %s\n", debugstr_w(lpBuffer));
1840 /***********************************************************************
1841 * HttpQueryInfoW (WININET.@)
1843 * Queries for information about an HTTP request
1850 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1851 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1853 BOOL bSuccess = FALSE;
1854 LPWININETHTTPREQW lpwhr;
1856 if (TRACE_ON(wininet)) {
1857 #define FE(x) { x, #x }
1858 static const wininet_flag_info query_flags[] = {
1859 FE(HTTP_QUERY_MIME_VERSION),
1860 FE(HTTP_QUERY_CONTENT_TYPE),
1861 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
1862 FE(HTTP_QUERY_CONTENT_ID),
1863 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
1864 FE(HTTP_QUERY_CONTENT_LENGTH),
1865 FE(HTTP_QUERY_CONTENT_LANGUAGE),
1866 FE(HTTP_QUERY_ALLOW),
1867 FE(HTTP_QUERY_PUBLIC),
1868 FE(HTTP_QUERY_DATE),
1869 FE(HTTP_QUERY_EXPIRES),
1870 FE(HTTP_QUERY_LAST_MODIFIED),
1871 FE(HTTP_QUERY_MESSAGE_ID),
1873 FE(HTTP_QUERY_DERIVED_FROM),
1874 FE(HTTP_QUERY_COST),
1875 FE(HTTP_QUERY_LINK),
1876 FE(HTTP_QUERY_PRAGMA),
1877 FE(HTTP_QUERY_VERSION),
1878 FE(HTTP_QUERY_STATUS_CODE),
1879 FE(HTTP_QUERY_STATUS_TEXT),
1880 FE(HTTP_QUERY_RAW_HEADERS),
1881 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
1882 FE(HTTP_QUERY_CONNECTION),
1883 FE(HTTP_QUERY_ACCEPT),
1884 FE(HTTP_QUERY_ACCEPT_CHARSET),
1885 FE(HTTP_QUERY_ACCEPT_ENCODING),
1886 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
1887 FE(HTTP_QUERY_AUTHORIZATION),
1888 FE(HTTP_QUERY_CONTENT_ENCODING),
1889 FE(HTTP_QUERY_FORWARDED),
1890 FE(HTTP_QUERY_FROM),
1891 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
1892 FE(HTTP_QUERY_LOCATION),
1893 FE(HTTP_QUERY_ORIG_URI),
1894 FE(HTTP_QUERY_REFERER),
1895 FE(HTTP_QUERY_RETRY_AFTER),
1896 FE(HTTP_QUERY_SERVER),
1897 FE(HTTP_QUERY_TITLE),
1898 FE(HTTP_QUERY_USER_AGENT),
1899 FE(HTTP_QUERY_WWW_AUTHENTICATE),
1900 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
1901 FE(HTTP_QUERY_ACCEPT_RANGES),
1902 FE(HTTP_QUERY_SET_COOKIE),
1903 FE(HTTP_QUERY_COOKIE),
1904 FE(HTTP_QUERY_REQUEST_METHOD),
1905 FE(HTTP_QUERY_REFRESH),
1906 FE(HTTP_QUERY_CONTENT_DISPOSITION),
1908 FE(HTTP_QUERY_CACHE_CONTROL),
1909 FE(HTTP_QUERY_CONTENT_BASE),
1910 FE(HTTP_QUERY_CONTENT_LOCATION),
1911 FE(HTTP_QUERY_CONTENT_MD5),
1912 FE(HTTP_QUERY_CONTENT_RANGE),
1913 FE(HTTP_QUERY_ETAG),
1914 FE(HTTP_QUERY_HOST),
1915 FE(HTTP_QUERY_IF_MATCH),
1916 FE(HTTP_QUERY_IF_NONE_MATCH),
1917 FE(HTTP_QUERY_IF_RANGE),
1918 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
1919 FE(HTTP_QUERY_MAX_FORWARDS),
1920 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
1921 FE(HTTP_QUERY_RANGE),
1922 FE(HTTP_QUERY_TRANSFER_ENCODING),
1923 FE(HTTP_QUERY_UPGRADE),
1924 FE(HTTP_QUERY_VARY),
1926 FE(HTTP_QUERY_WARNING),
1927 FE(HTTP_QUERY_CUSTOM)
1929 static const wininet_flag_info modifier_flags[] = {
1930 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
1931 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
1932 FE(HTTP_QUERY_FLAG_NUMBER),
1933 FE(HTTP_QUERY_FLAG_COALESCE)
1936 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
1937 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
1940 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
1941 TRACE(" Attribute:");
1942 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
1943 if (query_flags[i].val == info) {
1944 TRACE(" %s", query_flags[i].name);
1948 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
1949 TRACE(" Unknown (%08x)", info);
1952 TRACE(" Modifier:");
1953 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
1954 if (modifier_flags[i].val & info_mod) {
1955 TRACE(" %s", modifier_flags[i].name);
1956 info_mod &= ~ modifier_flags[i].val;
1961 TRACE(" Unknown (%08x)", info_mod);
1966 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1967 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1969 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1973 if (lpBuffer == NULL)
1974 *lpdwBufferLength = 0;
1975 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
1976 lpBuffer, lpdwBufferLength, lpdwIndex);
1980 WININET_Release( &lpwhr->hdr );
1982 TRACE("%d <--\n", bSuccess);
1986 /***********************************************************************
1987 * HttpQueryInfoA (WININET.@)
1989 * Queries for information about an HTTP request
1996 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1997 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2003 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
2004 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
2006 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
2007 lpdwBufferLength, lpdwIndex );
2012 len = (*lpdwBufferLength)*sizeof(WCHAR);
2013 bufferW = HeapAlloc( GetProcessHeap(), 0, len );
2014 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
2015 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
2016 MultiByteToWideChar(CP_ACP,0,lpBuffer,-1,bufferW,len);
2023 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
2027 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
2028 lpBuffer, *lpdwBufferLength, NULL, NULL );
2029 *lpdwBufferLength = len - 1;
2031 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
2034 /* since the strings being returned from HttpQueryInfoW should be
2035 * only ASCII characters, it is reasonable to assume that all of
2036 * the Unicode characters can be reduced to a single byte */
2037 *lpdwBufferLength = len / sizeof(WCHAR);
2039 HeapFree(GetProcessHeap(), 0, bufferW );
2044 /***********************************************************************
2045 * HttpSendRequestExA (WININET.@)
2047 * Sends the specified request to the HTTP server and allows chunked
2052 * Failure: FALSE, call GetLastError() for more information.
2054 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
2055 LPINTERNET_BUFFERSA lpBuffersIn,
2056 LPINTERNET_BUFFERSA lpBuffersOut,
2057 DWORD dwFlags, DWORD_PTR dwContext)
2059 INTERNET_BUFFERSW BuffersInW;
2062 LPWSTR header = NULL;
2064 TRACE("(%p, %p, %p, %08x, %08lx): stub\n", hRequest, lpBuffersIn,
2065 lpBuffersOut, dwFlags, dwContext);
2069 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
2070 if (lpBuffersIn->lpcszHeader)
2072 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
2073 lpBuffersIn->dwHeadersLength,0,0);
2074 header = HeapAlloc(GetProcessHeap(),0,headerlen*sizeof(WCHAR));
2075 if (!(BuffersInW.lpcszHeader = header))
2077 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2080 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
2081 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
2085 BuffersInW.lpcszHeader = NULL;
2086 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
2087 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
2088 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
2089 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
2090 BuffersInW.Next = NULL;
2093 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
2095 HeapFree(GetProcessHeap(),0,header);
2100 /***********************************************************************
2101 * HttpSendRequestExW (WININET.@)
2103 * Sends the specified request to the HTTP server and allows chunked
2108 * Failure: FALSE, call GetLastError() for more information.
2110 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
2111 LPINTERNET_BUFFERSW lpBuffersIn,
2112 LPINTERNET_BUFFERSW lpBuffersOut,
2113 DWORD dwFlags, DWORD_PTR dwContext)
2116 LPWININETHTTPREQW lpwhr;
2117 LPWININETHTTPSESSIONW lpwhs;
2118 LPWININETAPPINFOW hIC;
2120 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
2121 lpBuffersOut, dwFlags, dwContext);
2123 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
2125 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2127 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2131 lpwhs = lpwhr->lpHttpSession;
2132 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
2133 hIC = lpwhs->lpAppInfo;
2134 assert(hIC->hdr.htype == WH_HINIT);
2136 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2138 WORKREQUEST workRequest;
2139 struct WORKREQ_HTTPSENDREQUESTW *req;
2141 workRequest.asyncproc = AsyncHttpSendRequestProc;
2142 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
2143 req = &workRequest.u.HttpSendRequestW;
2146 if (lpBuffersIn->lpcszHeader)
2147 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
2148 req->lpszHeader = WININET_strdupW(lpBuffersIn->lpcszHeader);
2150 req->lpszHeader = NULL;
2151 req->dwHeaderLength = lpBuffersIn->dwHeadersLength;
2152 req->lpOptional = lpBuffersIn->lpvBuffer;
2153 req->dwOptionalLength = lpBuffersIn->dwBufferLength;
2154 req->dwContentLength = lpBuffersIn->dwBufferTotal;
2158 req->lpszHeader = NULL;
2159 req->dwHeaderLength = 0;
2160 req->lpOptional = NULL;
2161 req->dwOptionalLength = 0;
2162 req->dwContentLength = 0;
2165 req->bEndRequest = FALSE;
2167 INTERNET_AsyncCall(&workRequest);
2169 * This is from windows.
2171 INTERNET_SetLastError(ERROR_IO_PENDING);
2176 ret = HTTP_HttpSendRequestW(lpwhr, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
2177 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
2178 lpBuffersIn->dwBufferTotal, FALSE);
2180 ret = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, FALSE);
2185 WININET_Release( &lpwhr->hdr );
2191 /***********************************************************************
2192 * HttpSendRequestW (WININET.@)
2194 * Sends the specified request to the HTTP server
2201 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
2202 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
2204 LPWININETHTTPREQW lpwhr;
2205 LPWININETHTTPSESSIONW lpwhs = NULL;
2206 LPWININETAPPINFOW hIC = NULL;
2209 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest,
2210 debugstr_wn(lpszHeaders, dwHeaderLength), dwHeaderLength, lpOptional, dwOptionalLength);
2212 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
2213 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2215 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2220 lpwhs = lpwhr->lpHttpSession;
2221 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
2223 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2228 hIC = lpwhs->lpAppInfo;
2229 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
2231 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2236 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2238 WORKREQUEST workRequest;
2239 struct WORKREQ_HTTPSENDREQUESTW *req;
2241 workRequest.asyncproc = AsyncHttpSendRequestProc;
2242 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
2243 req = &workRequest.u.HttpSendRequestW;
2246 req->lpszHeader = HeapAlloc(GetProcessHeap(), 0, dwHeaderLength * sizeof(WCHAR));
2247 memcpy(req->lpszHeader, lpszHeaders, dwHeaderLength * sizeof(WCHAR));
2250 req->lpszHeader = 0;
2251 req->dwHeaderLength = dwHeaderLength;
2252 req->lpOptional = lpOptional;
2253 req->dwOptionalLength = dwOptionalLength;
2254 req->dwContentLength = dwOptionalLength;
2255 req->bEndRequest = TRUE;
2257 INTERNET_AsyncCall(&workRequest);
2259 * This is from windows.
2261 INTERNET_SetLastError(ERROR_IO_PENDING);
2266 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
2267 dwHeaderLength, lpOptional, dwOptionalLength,
2268 dwOptionalLength, TRUE);
2272 WININET_Release( &lpwhr->hdr );
2276 /***********************************************************************
2277 * HttpSendRequestA (WININET.@)
2279 * Sends the specified request to the HTTP server
2286 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
2287 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
2290 LPWSTR szHeaders=NULL;
2291 DWORD nLen=dwHeaderLength;
2292 if(lpszHeaders!=NULL)
2294 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
2295 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
2296 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
2298 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
2299 HeapFree(GetProcessHeap(),0,szHeaders);
2303 /***********************************************************************
2304 * HTTP_HandleRedirect (internal)
2306 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl)
2308 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
2309 LPWININETAPPINFOW hIC = lpwhs->lpAppInfo;
2314 /* if it's an absolute path, keep the same session info */
2315 lstrcpynW(path, lpszUrl, 2048);
2317 else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
2319 TRACE("Redirect through proxy\n");
2320 lstrcpynW(path, lpszUrl, 2048);
2324 URL_COMPONENTSW urlComponents;
2325 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
2326 static WCHAR szHttp[] = {'h','t','t','p',0};
2327 static WCHAR szHttps[] = {'h','t','t','p','s',0};
2328 DWORD url_length = 0;
2330 LPWSTR combined_url;
2332 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2333 urlComponents.lpszScheme = (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE) ? szHttps : szHttp;
2334 urlComponents.dwSchemeLength = 0;
2335 urlComponents.lpszHostName = lpwhs->lpszHostName;
2336 urlComponents.dwHostNameLength = 0;
2337 urlComponents.nPort = lpwhs->nHostPort;
2338 urlComponents.lpszUserName = lpwhs->lpszUserName;
2339 urlComponents.dwUserNameLength = 0;
2340 urlComponents.lpszPassword = NULL;
2341 urlComponents.dwPasswordLength = 0;
2342 urlComponents.lpszUrlPath = lpwhr->lpszPath;
2343 urlComponents.dwUrlPathLength = 0;
2344 urlComponents.lpszExtraInfo = NULL;
2345 urlComponents.dwExtraInfoLength = 0;
2347 if (!InternetCreateUrlW(&urlComponents, 0, NULL, &url_length) &&
2348 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2351 orig_url = HeapAlloc(GetProcessHeap(), 0, url_length);
2353 /* convert from bytes to characters */
2354 url_length = url_length / sizeof(WCHAR) - 1;
2355 if (!InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length))
2357 HeapFree(GetProcessHeap(), 0, orig_url);
2362 if (!InternetCombineUrlW(orig_url, lpszUrl, NULL, &url_length, ICU_ENCODE_SPACES_ONLY) &&
2363 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2365 HeapFree(GetProcessHeap(), 0, orig_url);
2368 combined_url = HeapAlloc(GetProcessHeap(), 0, url_length * sizeof(WCHAR));
2370 if (!InternetCombineUrlW(orig_url, lpszUrl, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY))
2372 HeapFree(GetProcessHeap(), 0, orig_url);
2373 HeapFree(GetProcessHeap(), 0, combined_url);
2376 HeapFree(GetProcessHeap(), 0, orig_url);
2382 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2383 urlComponents.lpszScheme = protocol;
2384 urlComponents.dwSchemeLength = 32;
2385 urlComponents.lpszHostName = hostName;
2386 urlComponents.dwHostNameLength = MAXHOSTNAME;
2387 urlComponents.lpszUserName = userName;
2388 urlComponents.dwUserNameLength = 1024;
2389 urlComponents.lpszPassword = NULL;
2390 urlComponents.dwPasswordLength = 0;
2391 urlComponents.lpszUrlPath = path;
2392 urlComponents.dwUrlPathLength = 2048;
2393 urlComponents.lpszExtraInfo = NULL;
2394 urlComponents.dwExtraInfoLength = 0;
2395 if(!InternetCrackUrlW(combined_url, strlenW(combined_url), 0, &urlComponents))
2397 HeapFree(GetProcessHeap(), 0, combined_url);
2400 HeapFree(GetProcessHeap(), 0, combined_url);
2402 if (!strncmpW(szHttp, urlComponents.lpszScheme, strlenW(szHttp)) &&
2403 (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2405 TRACE("redirect from secure page to non-secure page\n");
2406 /* FIXME: warn about from secure redirect to non-secure page */
2407 lpwhr->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
2409 if (!strncmpW(szHttps, urlComponents.lpszScheme, strlenW(szHttps)) &&
2410 !(lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2412 TRACE("redirect from non-secure page to secure page\n");
2413 /* FIXME: notify about redirect to secure page */
2414 lpwhr->hdr.dwFlags |= INTERNET_FLAG_SECURE;
2417 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
2419 if (lstrlenW(protocol)>4) /*https*/
2420 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2422 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2427 * This upsets redirects to binary files on sourceforge.net
2428 * and gives an html page instead of the target file
2429 * Examination of the HTTP request sent by native wininet.dll
2430 * reveals that it doesn't send a referrer in that case.
2431 * Maybe there's a flag that enables this, or maybe a referrer
2432 * shouldn't be added in case of a redirect.
2435 /* consider the current host as the referrer */
2436 if (NULL != lpwhs->lpszServerName && strlenW(lpwhs->lpszServerName))
2437 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
2438 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
2439 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
2442 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2443 lpwhs->lpszServerName = WININET_strdupW(hostName);
2444 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
2445 if (urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT &&
2446 urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT)
2449 static const WCHAR fmt[] = {'%','s',':','%','i',0};
2450 len = lstrlenW(hostName);
2451 len += 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
2452 lpwhs->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
2453 sprintfW(lpwhs->lpszHostName, fmt, hostName, urlComponents.nPort);
2456 lpwhs->lpszHostName = WININET_strdupW(hostName);
2458 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
2461 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2462 lpwhs->lpszUserName = NULL;
2464 lpwhs->lpszUserName = WININET_strdupW(userName);
2465 lpwhs->nServerPort = urlComponents.nPort;
2467 if (!HTTP_ResolveName(lpwhr))
2470 NETCON_close(&lpwhr->netConnection);
2472 if (!NETCON_init(&lpwhr->netConnection,lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2476 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2477 lpwhr->lpszPath=NULL;
2483 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
2484 if (rc != E_POINTER)
2485 needed = strlenW(path)+1;
2486 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
2487 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
2488 URL_ESCAPE_SPACES_ONLY);
2491 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
2492 strcpyW(lpwhr->lpszPath,path);
2499 /***********************************************************************
2500 * HTTP_build_req (internal)
2502 * concatenate all the strings in the request together
2504 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
2509 for( t = list; *t ; t++ )
2510 len += strlenW( *t );
2513 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2516 for( t = list; *t ; t++ )
2522 static BOOL HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr)
2525 LPWSTR requestString;
2531 static const WCHAR szConnect[] = {'C','O','N','N','E','C','T',0};
2532 static const WCHAR szFormat[] = {'%','s',':','%','d',0};
2533 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
2537 lpszPath = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs->lpszHostName ) + 13)*sizeof(WCHAR) );
2538 sprintfW( lpszPath, szFormat, lpwhs->lpszHostName, lpwhs->nHostPort );
2539 requestString = HTTP_BuildHeaderRequestString( lpwhr, szConnect, lpszPath, FALSE );
2540 HeapFree( GetProcessHeap(), 0, lpszPath );
2542 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2543 NULL, 0, NULL, NULL );
2544 len--; /* the nul terminator isn't needed */
2545 ascii_req = HeapAlloc( GetProcessHeap(), 0, len );
2546 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2547 ascii_req, len, NULL, NULL );
2548 HeapFree( GetProcessHeap(), 0, requestString );
2550 TRACE("full request -> %s\n", debugstr_an( ascii_req, len ) );
2552 ret = NETCON_send( &lpwhr->netConnection, ascii_req, len, 0, &cnt );
2553 HeapFree( GetProcessHeap(), 0, ascii_req );
2554 if (!ret || cnt < 0)
2557 responseLen = HTTP_GetResponseHeaders( lpwhr );
2564 /***********************************************************************
2565 * HTTP_HttpSendRequestW (internal)
2567 * Sends the specified request to the HTTP server
2574 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
2575 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
2576 DWORD dwContentLength, BOOL bEndRequest)
2579 BOOL bSuccess = FALSE;
2580 LPWSTR requestString = NULL;
2583 INTERNET_ASYNC_RESULT iar;
2584 static const WCHAR szClose[] = { 'C','l','o','s','e',0 };
2585 static const WCHAR szContentLength[] =
2586 { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0 };
2587 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \r\n */ + 20 /* int */ ];
2589 TRACE("--> %p\n", lpwhr);
2591 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
2593 /* Clear any error information */
2594 INTERNET_SetLastError(0);
2596 HTTP_FixVerb(lpwhr);
2598 sprintfW(contentLengthStr, szContentLength, dwContentLength);
2599 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
2608 /* like native, just in case the caller forgot to call InternetReadFile
2609 * for all the data */
2610 HTTP_DrainContent(lpwhr);
2611 lpwhr->dwContentRead = 0;
2613 if (TRACE_ON(wininet))
2615 LPHTTPHEADERW Host = HTTP_GetHeader(lpwhr,szHost);
2616 TRACE("Going to url %s %s\n", debugstr_w(Host->lpszValue), debugstr_w(lpwhr->lpszPath));
2620 HTTP_ProcessHeader(lpwhr, szConnection,
2621 lpwhr->hdr.dwFlags & INTERNET_FLAG_KEEP_CONNECTION ? szKeepAlive : szClose,
2622 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
2624 HTTP_InsertAuthorization(lpwhr);
2625 HTTP_InsertProxyAuthorization(lpwhr);
2627 /* add the headers the caller supplied */
2628 if( lpszHeaders && dwHeaderLength )
2630 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
2631 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
2634 requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, FALSE);
2636 TRACE("Request header -> %s\n", debugstr_w(requestString) );
2638 /* Send the request and store the results */
2639 if (!HTTP_OpenConnection(lpwhr))
2642 /* send the request as ASCII, tack on the optional data */
2644 dwOptionalLength = 0;
2645 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2646 NULL, 0, NULL, NULL );
2647 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
2648 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2649 ascii_req, len, NULL, NULL );
2651 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
2652 len = (len + dwOptionalLength - 1);
2654 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
2656 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2657 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
2659 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
2660 HeapFree( GetProcessHeap(), 0, ascii_req );
2662 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2663 INTERNET_STATUS_REQUEST_SENT,
2664 &len, sizeof(DWORD));
2671 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2672 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2677 responseLen = HTTP_GetResponseHeaders(lpwhr);
2681 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2682 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
2685 HTTP_ProcessHeaders(lpwhr);
2687 dwBufferSize = sizeof(lpwhr->dwContentLength);
2688 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
2689 &lpwhr->dwContentLength,&dwBufferSize,NULL))
2690 lpwhr->dwContentLength = -1;
2692 if (lpwhr->dwContentLength == 0)
2693 HTTP_FinishedReading(lpwhr);
2695 dwBufferSize = sizeof(dwStatusCode);
2696 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,
2697 &dwStatusCode,&dwBufferSize,NULL))
2700 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
2702 WCHAR szNewLocation[2048];
2703 dwBufferSize=sizeof(szNewLocation);
2704 if ((dwStatusCode==HTTP_STATUS_REDIRECT || dwStatusCode==HTTP_STATUS_MOVED) &&
2705 HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL))
2707 HTTP_DrainContent(lpwhr);
2708 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2709 INTERNET_STATUS_REDIRECT, szNewLocation,
2711 bSuccess = HTTP_HandleRedirect(lpwhr, szNewLocation);
2714 HeapFree(GetProcessHeap(), 0, requestString);
2719 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTH) && bSuccess)
2721 WCHAR szAuthValue[2048];
2723 if (dwStatusCode == HTTP_STATUS_DENIED)
2726 while (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_WWW_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex))
2728 if (HTTP_DoAuthorization(lpwhr, szAuthValue,
2730 lpwhr->lpHttpSession->lpszUserName,
2731 lpwhr->lpHttpSession->lpszPassword))
2738 if (dwStatusCode == HTTP_STATUS_PROXY_AUTH_REQ)
2741 while (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_PROXY_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex))
2743 if (HTTP_DoAuthorization(lpwhr, szAuthValue,
2744 &lpwhr->pProxyAuthInfo,
2745 lpwhr->lpHttpSession->lpAppInfo->lpszProxyUsername,
2746 lpwhr->lpHttpSession->lpAppInfo->lpszProxyPassword))
2762 HeapFree(GetProcessHeap(), 0, requestString);
2764 /* TODO: send notification for P3P header */
2766 iar.dwResult = (DWORD)bSuccess;
2767 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
2769 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2770 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2771 sizeof(INTERNET_ASYNC_RESULT));
2777 /***********************************************************************
2778 * HTTP_Connect (internal)
2780 * Create http session handle
2783 * HINTERNET a session handle on success
2787 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
2788 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
2789 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
2790 DWORD dwInternalFlags)
2792 BOOL bSuccess = FALSE;
2793 LPWININETHTTPSESSIONW lpwhs = NULL;
2794 HINTERNET handle = NULL;
2798 if (!lpszServerName || !lpszServerName[0])
2800 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2804 assert( hIC->hdr.htype == WH_HINIT );
2806 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
2809 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2814 * According to my tests. The name is not resolved until a request is sent
2817 lpwhs->hdr.htype = WH_HHTTPSESSION;
2818 lpwhs->hdr.dwFlags = dwFlags;
2819 lpwhs->hdr.dwContext = dwContext;
2820 lpwhs->hdr.dwInternalFlags = dwInternalFlags | (hIC->hdr.dwInternalFlags & INET_CALLBACKW);
2821 lpwhs->hdr.dwRefCount = 1;
2822 lpwhs->hdr.close_connection = NULL;
2823 lpwhs->hdr.destroy = HTTP_CloseHTTPSessionHandle;
2824 lpwhs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
2826 WININET_AddRef( &hIC->hdr );
2827 lpwhs->lpAppInfo = hIC;
2828 list_add_head( &hIC->hdr.children, &lpwhs->hdr.entry );
2830 handle = WININET_AllocHandle( &lpwhs->hdr );
2833 ERR("Failed to alloc handle\n");
2834 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2838 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
2839 if(strchrW(hIC->lpszProxy, ' '))
2840 FIXME("Several proxies not implemented.\n");
2841 if(hIC->lpszProxyBypass)
2842 FIXME("Proxy bypass is ignored.\n");
2844 if (lpszServerName && lpszServerName[0])
2846 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
2847 lpwhs->lpszHostName = WININET_strdupW(lpszServerName);
2849 if (lpszUserName && lpszUserName[0])
2850 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
2851 if (lpszPassword && lpszPassword[0])
2852 lpwhs->lpszPassword = WININET_strdupW(lpszPassword);
2853 lpwhs->nServerPort = nServerPort;
2854 lpwhs->nHostPort = nServerPort;
2856 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2857 if (!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
2859 INTERNET_SendCallback(&hIC->hdr, dwContext,
2860 INTERNET_STATUS_HANDLE_CREATED, &handle,
2868 WININET_Release( &lpwhs->hdr );
2871 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
2875 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
2880 /***********************************************************************
2881 * HTTP_OpenConnection (internal)
2883 * Connect to a web server
2890 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
2892 BOOL bSuccess = FALSE;
2893 LPWININETHTTPSESSIONW lpwhs;
2894 LPWININETAPPINFOW hIC = NULL;
2900 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2902 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2906 if (NETCON_connected(&lpwhr->netConnection))
2912 lpwhs = lpwhr->lpHttpSession;
2914 hIC = lpwhs->lpAppInfo;
2915 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
2916 szaddr, sizeof(szaddr));
2917 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2918 INTERNET_STATUS_CONNECTING_TO_SERVER,
2922 if (!NETCON_create(&lpwhr->netConnection, lpwhs->socketAddress.sin_family,
2925 WARN("Socket creation failed\n");
2929 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
2930 sizeof(lpwhs->socketAddress)))
2933 if (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE)
2935 /* Note: we differ from Microsoft's WinINet here. they seem to have
2936 * a bug that causes no status callbacks to be sent when starting
2937 * a tunnel to a proxy server using the CONNECT verb. i believe our
2938 * behaviour to be more correct and to not cause any incompatibilities
2939 * because using a secure connection through a proxy server is a rare
2940 * case that would be hard for anyone to depend on */
2941 if (hIC->lpszProxy && !HTTP_SecureProxyConnect(lpwhr))
2944 if (!NETCON_secure_connect(&lpwhr->netConnection, lpwhs->lpszHostName))
2946 WARN("Couldn't connect securely to host\n");
2951 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2952 INTERNET_STATUS_CONNECTED_TO_SERVER,
2953 szaddr, strlen(szaddr)+1);
2958 TRACE("%d <--\n", bSuccess);
2963 /***********************************************************************
2964 * HTTP_clear_response_headers (internal)
2966 * clear out any old response headers
2968 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
2972 for( i=0; i<lpwhr->nCustHeaders; i++)
2974 if( !lpwhr->pCustHeaders[i].lpszField )
2976 if( !lpwhr->pCustHeaders[i].lpszValue )
2978 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
2980 HTTP_DeleteCustomHeader( lpwhr, i );
2985 /***********************************************************************
2986 * HTTP_GetResponseHeaders (internal)
2988 * Read server response
2995 static INT HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr)
2998 WCHAR buffer[MAX_REPLY_LEN];
2999 DWORD buflen = MAX_REPLY_LEN;
3000 BOOL bSuccess = FALSE;
3002 static const WCHAR szCrLf[] = {'\r','\n',0};
3003 char bufferA[MAX_REPLY_LEN];
3004 LPWSTR status_code, status_text;
3005 DWORD cchMaxRawHeaders = 1024;
3006 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3007 DWORD cchRawHeaders = 0;
3011 /* clear old response headers (eg. from a redirect response) */
3012 HTTP_clear_response_headers( lpwhr );
3014 if (!NETCON_connected(&lpwhr->netConnection))
3018 * HACK peek at the buffer
3020 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
3023 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
3025 buflen = MAX_REPLY_LEN;
3026 memset(buffer, 0, MAX_REPLY_LEN);
3027 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
3029 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
3031 /* regenerate raw headers */
3032 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
3034 cchMaxRawHeaders *= 2;
3035 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3037 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
3038 cchRawHeaders += (buflen-1);
3039 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
3040 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
3041 lpszRawHeaders[cchRawHeaders] = '\0';
3043 /* split the version from the status code */
3044 status_code = strchrW( buffer, ' ' );
3049 /* split the status code from the status text */
3050 status_text = strchrW( status_code, ' ' );
3055 TRACE("version [%s] status code [%s] status text [%s]\n",
3056 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
3058 HTTP_ProcessHeader(lpwhr, szStatus, status_code,
3059 HTTP_ADDHDR_FLAG_REPLACE);
3061 HeapFree(GetProcessHeap(),0,lpwhr->lpszVersion);
3062 HeapFree(GetProcessHeap(),0,lpwhr->lpszStatusText);
3064 lpwhr->lpszVersion= WININET_strdupW(buffer);
3065 lpwhr->lpszStatusText = WININET_strdupW(status_text);
3067 /* Parse each response line */
3070 buflen = MAX_REPLY_LEN;
3071 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
3073 LPWSTR * pFieldAndValue;
3075 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
3076 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
3078 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
3080 cchMaxRawHeaders *= 2;
3081 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3083 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
3084 cchRawHeaders += (buflen-1);
3085 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
3086 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
3087 lpszRawHeaders[cchRawHeaders] = '\0';
3089 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
3090 if (!pFieldAndValue)
3093 HTTP_ProcessHeader(lpwhr, pFieldAndValue[0], pFieldAndValue[1],
3094 HTTP_ADDREQ_FLAG_ADD );
3096 HTTP_FreeTokens(pFieldAndValue);
3106 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
3107 lpwhr->lpszRawHeaders = lpszRawHeaders;
3108 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
3121 static void strip_spaces(LPWSTR start)
3126 while (*str == ' ' && *str != '\0')
3130 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
3132 end = start + strlenW(start) - 1;
3133 while (end >= start && *end == ' ')
3141 /***********************************************************************
3142 * HTTP_InterpretHttpHeader (internal)
3144 * Parse server response
3148 * Pointer to array of field, value, NULL on success.
3151 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
3153 LPWSTR * pTokenPair;
3157 pTokenPair = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pTokenPair)*3);
3159 pszColon = strchrW(buffer, ':');
3160 /* must have two tokens */
3163 HTTP_FreeTokens(pTokenPair);
3165 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
3169 pTokenPair[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon - buffer + 1) * sizeof(WCHAR));
3172 HTTP_FreeTokens(pTokenPair);
3175 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
3176 pTokenPair[0][pszColon - buffer] = '\0';
3180 len = strlenW(pszColon);
3181 pTokenPair[1] = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
3184 HTTP_FreeTokens(pTokenPair);
3187 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
3189 strip_spaces(pTokenPair[0]);
3190 strip_spaces(pTokenPair[1]);
3192 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
3196 /***********************************************************************
3197 * HTTP_ProcessHeader (internal)
3199 * Stuff header into header tables according to <dwModifier>
3203 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
3205 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
3207 LPHTTPHEADERW lphttpHdr = NULL;
3208 BOOL bSuccess = FALSE;
3210 BOOL request_only = dwModifier & HTTP_ADDHDR_FLAG_REQ;
3212 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), dwModifier);
3214 /* REPLACE wins out over ADD */
3215 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
3216 dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
3218 if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
3221 index = HTTP_GetCustomHeaderIndex(lpwhr, field, 0, request_only);
3225 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
3229 lphttpHdr = &lpwhr->pCustHeaders[index];
3235 hdr.lpszField = (LPWSTR)field;
3236 hdr.lpszValue = (LPWSTR)value;
3237 hdr.wFlags = hdr.wCount = 0;
3239 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3240 hdr.wFlags |= HDR_ISREQUEST;
3242 return HTTP_InsertCustomHeader(lpwhr, &hdr);
3244 /* no value to delete */
3247 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3248 lphttpHdr->wFlags |= HDR_ISREQUEST;
3250 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
3252 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
3254 HTTP_DeleteCustomHeader( lpwhr, index );
3260 hdr.lpszField = (LPWSTR)field;
3261 hdr.lpszValue = (LPWSTR)value;
3262 hdr.wFlags = hdr.wCount = 0;
3264 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3265 hdr.wFlags |= HDR_ISREQUEST;
3267 return HTTP_InsertCustomHeader(lpwhr, &hdr);
3272 else if (dwModifier & COALESCEFLAGS)
3277 INT origlen = strlenW(lphttpHdr->lpszValue);
3278 INT valuelen = strlenW(value);
3280 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
3283 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
3285 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
3288 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
3291 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
3293 lpsztmp = HeapReAlloc(GetProcessHeap(), 0, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
3296 lphttpHdr->lpszValue = lpsztmp;
3297 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
3300 lphttpHdr->lpszValue[origlen] = ch;
3302 lphttpHdr->lpszValue[origlen] = ' ';
3306 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
3307 lphttpHdr->lpszValue[len] = '\0';
3312 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
3313 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3316 TRACE("<-- %d\n",bSuccess);
3321 /***********************************************************************
3322 * HTTP_CloseConnection (internal)
3324 * Close socket connection
3327 static void HTTP_CloseConnection(LPWININETHANDLEHEADER hdr)
3329 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
3330 LPWININETHTTPSESSIONW lpwhs = NULL;
3331 LPWININETAPPINFOW hIC = NULL;
3333 TRACE("%p\n",lpwhr);
3335 if (!NETCON_connected(&lpwhr->netConnection))
3338 if (lpwhr->pAuthInfo)
3340 DeleteSecurityContext(&lpwhr->pAuthInfo->ctx);
3341 FreeCredentialsHandle(&lpwhr->pAuthInfo->cred);
3343 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->auth_data);
3344 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->scheme);
3345 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo);
3346 lpwhr->pAuthInfo = NULL;
3348 if (lpwhr->pProxyAuthInfo)
3350 DeleteSecurityContext(&lpwhr->pProxyAuthInfo->ctx);
3351 FreeCredentialsHandle(&lpwhr->pProxyAuthInfo->cred);
3353 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo->auth_data);
3354 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo->scheme);
3355 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo);
3356 lpwhr->pProxyAuthInfo = NULL;
3359 lpwhs = lpwhr->lpHttpSession;
3360 hIC = lpwhs->lpAppInfo;
3362 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3363 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
3365 NETCON_close(&lpwhr->netConnection);
3367 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3368 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
3372 /***********************************************************************
3373 * HTTP_FinishedReading (internal)
3375 * Called when all content from server has been read by client.
3378 BOOL HTTP_FinishedReading(LPWININETHTTPREQW lpwhr)
3380 WCHAR szConnectionResponse[20];
3381 DWORD dwBufferSize = sizeof(szConnectionResponse);
3385 if (!HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_CONNECTION, szConnectionResponse,
3386 &dwBufferSize, NULL) ||
3387 strcmpiW(szConnectionResponse, szKeepAlive))
3389 HTTP_CloseConnection(&lpwhr->hdr);
3392 /* FIXME: store data in the URL cache here */
3397 /***********************************************************************
3398 * HTTP_CloseHTTPRequestHandle (internal)
3400 * Deallocate request handle
3403 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr)
3406 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
3410 WININET_Release(&lpwhr->lpHttpSession->hdr);
3412 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
3413 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
3414 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
3415 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVersion);
3416 HeapFree(GetProcessHeap(), 0, lpwhr->lpszStatusText);
3418 for (i = 0; i < lpwhr->nCustHeaders; i++)
3420 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
3421 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
3424 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
3425 HeapFree(GetProcessHeap(), 0, lpwhr);
3429 /***********************************************************************
3430 * HTTP_CloseHTTPSessionHandle (internal)
3432 * Deallocate session handle
3435 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr)
3437 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
3439 TRACE("%p\n", lpwhs);
3441 WININET_Release(&lpwhs->lpAppInfo->hdr);
3443 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
3444 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
3445 HeapFree(GetProcessHeap(), 0, lpwhs->lpszPassword);
3446 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
3447 HeapFree(GetProcessHeap(), 0, lpwhs);
3451 /***********************************************************************
3452 * HTTP_GetCustomHeaderIndex (internal)
3454 * Return index of custom header from header array
3457 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField,
3458 int requested_index, BOOL request_only)
3462 TRACE("%s\n", debugstr_w(lpszField));
3464 for (index = 0; index < lpwhr->nCustHeaders; index++)
3466 if (strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
3469 if (request_only && !(lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
3472 if (!request_only && (lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
3475 if (requested_index == 0)
3480 if (index >= lpwhr->nCustHeaders)
3483 TRACE("Return: %d\n", index);
3488 /***********************************************************************
3489 * HTTP_InsertCustomHeader (internal)
3491 * Insert header into array
3494 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
3497 LPHTTPHEADERW lph = NULL;
3500 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
3501 count = lpwhr->nCustHeaders + 1;
3503 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
3505 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
3509 lpwhr->pCustHeaders = lph;
3510 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
3511 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
3512 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
3513 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
3514 lpwhr->nCustHeaders++;
3519 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3526 /***********************************************************************
3527 * HTTP_DeleteCustomHeader (internal)
3529 * Delete header from array
3530 * If this function is called, the indexs may change.
3532 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
3534 if( lpwhr->nCustHeaders <= 0 )
3536 if( index >= lpwhr->nCustHeaders )
3538 lpwhr->nCustHeaders--;
3540 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
3541 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
3542 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
3548 /***********************************************************************
3549 * HTTP_VerifyValidHeader (internal)
3551 * Verify the given header is not invalid for the given http request
3554 static BOOL HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field)
3558 /* Accept-Encoding is stripped from HTTP/1.0 requests. It is invalid */
3559 if (strcmpiW(field,szAccept_Encoding)==0)
3565 /***********************************************************************
3566 * IsHostInProxyBypassList (@)
3571 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
3573 FIXME("STUB: flags=%d host=%s length=%d\n",flags,szHost,length);