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
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "wine/port.h"
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_SOCKET_H
33 # include <sys/socket.h>
51 #define NO_SHLWAPI_STREAM
55 #include "wine/debug.h"
56 #include "wine/unicode.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
60 static const WCHAR g_szHttp1_0[] = {' ','H','T','T','P','/','1','.','0',0 };
61 static const WCHAR g_szHttp1_1[] = {' ','H','T','T','P','/','1','.','1',0 };
62 static const WCHAR g_szReferer[] = {'R','e','f','e','r','e','r',0};
63 static const WCHAR g_szAccept[] = {'A','c','c','e','p','t',0};
64 static const WCHAR g_szUserAgent[] = {'U','s','e','r','-','A','g','e','n','t',0};
65 static const WCHAR g_szHost[] = {'H','o','s','t',0};
68 #define MAXHOSTNAME 100
69 #define MAX_FIELD_VALUE_LEN 256
70 #define MAX_FIELD_LEN 256
72 #define HTTP_REFERER g_szReferer
73 #define HTTP_ACCEPT g_szAccept
74 #define HTTP_USERAGENT g_szUserAgent
76 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
77 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
78 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
79 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
80 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
81 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
82 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
85 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr);
86 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr);
87 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr);
88 static BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr);
89 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
90 static BOOL HTTP_ReplaceHeaderValue( LPHTTPHEADERW lphttpHdr, LPCWSTR lpsztmp );
91 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
92 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr);
93 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField);
94 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index);
95 static LPWSTR HTTP_build_req( LPCWSTR *list, int len );
96 static BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
97 LPCWSTR username, LPCWSTR password );
98 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD
99 dwInfoLevel, LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD
101 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl,
102 LPCWSTR lpszHeaders, DWORD dwHeaderLength, LPVOID lpOptional, DWORD
105 /***********************************************************************
106 * HTTP_Tokenize (internal)
108 * Tokenize a string, allocating memory for the tokens.
110 static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
112 LPWSTR * token_array;
117 /* empty string has no tokens */
121 for (i = 0; string[i]; i++)
122 if (!strncmpW(string+i, token_string, strlenW(token_string)))
126 /* we want to skip over separators, but not the null terminator */
127 for (j = 0; j < strlenW(token_string) - 1; j++)
133 /* add 1 for terminating NULL */
134 token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
135 token_array[tokens] = NULL;
138 for (i = 0; i < tokens; i++)
141 next_token = strstrW(string, token_string);
142 if (!next_token) next_token = string+strlenW(string);
143 len = next_token - string;
144 token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
145 memcpy(token_array[i], string, len*sizeof(WCHAR));
146 token_array[i][len] = '\0';
147 string = next_token+strlenW(token_string);
152 /***********************************************************************
153 * HTTP_FreeTokens (internal)
155 * Frees memory returned from HTTP_Tokenize.
157 static void HTTP_FreeTokens(LPWSTR * token_array)
160 for (i = 0; token_array[i]; i++)
161 HeapFree(GetProcessHeap(), 0, token_array[i]);
162 HeapFree(GetProcessHeap(), 0, token_array);
165 /* **********************************************************************
167 * Helper functions for the HttpSendRequest(Ex) functions
170 static void HTTP_FixVerb( LPWININETHTTPREQW lpwhr )
172 /* if the verb is NULL default to GET */
173 if (NULL == lpwhr->lpszVerb)
175 static const WCHAR szGET[] = { 'G','E','T', 0 };
176 lpwhr->lpszVerb = WININET_strdupW(szGET);
180 static void HTTP_FixURL( LPWININETHTTPREQW lpwhr)
182 static const WCHAR szSlash[] = { '/',0 };
183 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
185 /* If we don't have a path we set it to root */
186 if (NULL == lpwhr->lpszPath)
187 lpwhr->lpszPath = WININET_strdupW(szSlash);
188 else /* remove \r and \n*/
190 int nLen = strlenW(lpwhr->lpszPath);
191 while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
194 lpwhr->lpszPath[nLen]='\0';
196 /* Replace '\' with '/' */
199 if (lpwhr->lpszPath[nLen] == '\\') lpwhr->lpszPath[nLen]='/';
203 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
204 lpwhr->lpszPath, strlenW(szHttp), szHttp, strlenW(szHttp) )
205 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
207 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
208 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
210 strcpyW(fixurl + 1, lpwhr->lpszPath);
211 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
212 lpwhr->lpszPath = fixurl;
216 static LPWSTR HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr, LPCWSTR verb, LPCWSTR path, BOOL http1_1 )
218 LPWSTR requestString;
224 static const WCHAR szSpace[] = { ' ',0 };
225 static const WCHAR szcrlf[] = {'\r','\n', 0};
226 static const WCHAR szColon[] = { ':',' ',0 };
227 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
229 /* allocate space for an array of all the string pointers to be added */
230 len = (HTTP_QUERY_MAX + lpwhr->nCustHeaders)*4 + 9;
231 req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
233 /* add the verb, path and HTTP version string */
238 req[n++] = http1_1 ? g_szHttp1_1 : g_szHttp1_0;
240 /* Append standard request headers */
241 for (i = 0; i <= HTTP_QUERY_MAX; i++)
243 if (lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST)
246 req[n++] = lpwhr->StdHeaders[i].lpszField;
248 req[n++] = lpwhr->StdHeaders[i].lpszValue;
250 TRACE("Adding header %s (%s)\n",
251 debugstr_w(lpwhr->StdHeaders[i].lpszField),
252 debugstr_w(lpwhr->StdHeaders[i].lpszValue));
256 /* Append custom request heades */
257 for (i = 0; i < lpwhr->nCustHeaders; i++)
259 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
262 req[n++] = lpwhr->pCustHeaders[i].lpszField;
264 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
266 TRACE("Adding custom header %s (%s)\n",
267 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
268 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
273 ERR("oops. buffer overrun\n");
276 requestString = HTTP_build_req( req, 4 );
277 HeapFree( GetProcessHeap(), 0, req );
280 * Set (header) termination string for request
281 * Make sure there's exactly two new lines at the end of the request
283 p = &requestString[strlenW(requestString)-1];
284 while ( (*p == '\n') || (*p == '\r') )
286 strcpyW( p+1, sztwocrlf );
288 return requestString;
291 static void HTTP_ProcessHeaders( LPWININETHTTPREQW lpwhr )
293 LPHTTPHEADERW setCookieHeader = &lpwhr->StdHeaders[HTTP_QUERY_SET_COOKIE];
295 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && setCookieHeader->lpszValue)
297 int nPosStart = 0, nPosEnd = 0, len;
298 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
300 while (setCookieHeader->lpszValue[nPosEnd] != '\0')
302 LPWSTR buf_cookie, cookie_name, cookie_data;
304 LPWSTR domain = NULL;
306 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
307 setCookieHeader->lpszValue[nPosEnd] != '\0')
311 if (setCookieHeader->lpszValue[nPosEnd] == ';')
313 /* fixme: not case sensitive, strcasestr is gnu only */
314 int nDomainPosEnd = 0;
315 int nDomainPosStart = 0, nDomainLength = 0;
316 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
317 LPWSTR lpszDomain = strstrW(&setCookieHeader->lpszValue[nPosEnd], szDomain);
319 { /* they have specified their own domain, lets use it */
320 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
321 lpszDomain[nDomainPosEnd] != '\0')
325 nDomainPosStart = strlenW(szDomain);
326 nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
327 domain = HeapAlloc(GetProcessHeap(), 0, (nDomainLength + 1)*sizeof(WCHAR));
328 lstrcpynW(domain, &lpszDomain[nDomainPosStart], nDomainLength + 1);
331 if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
332 buf_cookie = HeapAlloc(GetProcessHeap(), 0, ((nPosEnd - nPosStart) + 1)*sizeof(WCHAR));
333 lstrcpynW(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart) + 1);
334 TRACE("%s\n", debugstr_w(buf_cookie));
335 while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
339 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
341 HeapFree(GetProcessHeap(), 0, buf_cookie);
345 cookie_name = HeapAlloc(GetProcessHeap(), 0, (nEqualPos + 1)*sizeof(WCHAR));
346 lstrcpynW(cookie_name, buf_cookie, nEqualPos + 1);
347 cookie_data = &buf_cookie[nEqualPos + 1];
350 len = strlenW((domain ? domain : lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue)) +
351 strlenW(lpwhr->lpszPath) + 9;
352 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
353 sprintfW(buf_url, szFmt, (domain ? domain : lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue)); /* FIXME PATH!!! */
354 InternetSetCookieW(buf_url, cookie_name, cookie_data);
356 HeapFree(GetProcessHeap(), 0, buf_url);
357 HeapFree(GetProcessHeap(), 0, buf_cookie);
358 HeapFree(GetProcessHeap(), 0, cookie_name);
359 HeapFree(GetProcessHeap(), 0, domain);
365 static void HTTP_AddProxyInfo( LPWININETHTTPREQW lpwhr )
367 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
368 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW)lpwhs->hdr.lpwhparent;
370 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
371 assert(hIC->hdr.htype == WH_HINIT);
373 if (hIC && (hIC->lpszProxyUsername || hIC->lpszProxyPassword ))
374 HTTP_InsertProxyAuthorization(lpwhr, hIC->lpszProxyUsername,
375 hIC->lpszProxyPassword);
378 /***********************************************************************
379 * HTTP_HttpAddRequestHeadersW (internal)
381 static BOOL WINAPI HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
382 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
387 BOOL bSuccess = FALSE;
390 TRACE("copying header: %s\n", debugstr_w(lpszHeader));
392 if( dwHeaderLength == ~0U )
393 len = strlenW(lpszHeader);
395 len = dwHeaderLength;
396 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
397 lstrcpynW( buffer, lpszHeader, len + 1);
403 LPWSTR * pFieldAndValue;
407 while (*lpszEnd != '\0')
409 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
414 if (*lpszStart == '\0')
417 if (*lpszEnd == '\r')
420 lpszEnd += 2; /* Jump over \r\n */
422 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
423 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
426 bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
427 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
428 HTTP_FreeTokens(pFieldAndValue);
434 HeapFree(GetProcessHeap(), 0, buffer);
439 /***********************************************************************
440 * HttpAddRequestHeadersW (WININET.@)
442 * Adds one or more HTTP header to the request handler
449 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
450 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
452 BOOL bSuccess = FALSE;
453 LPWININETHTTPREQW lpwhr;
455 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_w(lpszHeader), dwHeaderLength,
461 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
462 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
464 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
467 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
470 WININET_Release( &lpwhr->hdr );
475 /***********************************************************************
476 * HttpAddRequestHeadersA (WININET.@)
478 * Adds one or more HTTP header to the request handler
485 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
486 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
492 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_a(lpszHeader), dwHeaderLength,
495 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
496 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
497 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
498 if( dwHeaderLength != ~0U )
499 dwHeaderLength = len;
501 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
503 HeapFree( GetProcessHeap(), 0, hdr );
508 /***********************************************************************
509 * HttpEndRequestA (WININET.@)
511 * Ends an HTTP request that was started by HttpSendRequestEx
518 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
519 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD dwContext)
521 LPINTERNET_BUFFERSA ptr;
522 LPINTERNET_BUFFERSW lpBuffersOutW,ptrW;
525 TRACE("(%p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersOut, dwFlags,
530 lpBuffersOutW = (LPINTERNET_BUFFERSW)HeapAlloc(GetProcessHeap(),
531 HEAP_ZERO_MEMORY, sizeof(INTERNET_BUFFERSW));
533 lpBuffersOutW = NULL;
535 ptrW = lpBuffersOutW;
538 if (ptr->lpvBuffer && ptr->dwBufferLength)
539 ptrW->lpvBuffer = HeapAlloc(GetProcessHeap(),0,ptr->dwBufferLength);
540 ptrW->dwBufferLength = ptr->dwBufferLength;
541 ptrW->dwBufferTotal= ptr->dwBufferTotal;
544 ptrW->Next = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
545 sizeof(INTERNET_BUFFERSW));
551 rc = HttpEndRequestW(hRequest, lpBuffersOutW, dwFlags, dwContext);
555 ptrW = lpBuffersOutW;
558 LPINTERNET_BUFFERSW ptrW2;
560 FIXME("Do we need to translate info out of these buffer?\n");
562 HeapFree(GetProcessHeap(),0,(LPVOID)ptrW->lpvBuffer);
564 HeapFree(GetProcessHeap(),0,ptrW);
572 /***********************************************************************
573 * HttpEndRequestW (WININET.@)
575 * Ends an HTTP request that was started by HttpSendRequestEx
582 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
583 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD dwContext)
586 LPWININETHTTPREQW lpwhr;
590 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
592 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
594 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
598 lpwhr->hdr.dwFlags |= dwFlags;
599 lpwhr->hdr.dwContext = dwContext;
601 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
602 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
604 responseLen = HTTP_GetResponseHeaders(lpwhr);
608 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
609 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
611 /* process headers here. Is this right? */
612 HTTP_ProcessHeaders(lpwhr);
614 /* We appear to do nothing with the buffer.. is that correct? */
616 if(!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
618 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
619 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
620 (dwCode==302 || dwCode==301))
622 WCHAR szNewLocation[2048];
623 DWORD dwBufferSize=2048;
625 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
627 static const WCHAR szGET[] = { 'G','E','T', 0 };
628 /* redirects are always GETs */
629 HeapFree(GetProcessHeap(),0,lpwhr->lpszVerb);
630 lpwhr->lpszVerb = WININET_strdupW(szGET);
631 return HTTP_HandleRedirect(lpwhr, szNewLocation, NULL, 0, NULL, 0);
636 TRACE("%i <--\n",rc);
640 /***********************************************************************
641 * HttpOpenRequestW (WININET.@)
643 * Open a HTTP request handle
646 * HINTERNET a HTTP request handle on success
650 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
651 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
652 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
653 DWORD dwFlags, DWORD dwContext)
655 LPWININETHTTPSESSIONW lpwhs;
656 HINTERNET handle = NULL;
658 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
659 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
660 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
662 if(lpszAcceptTypes!=NULL)
665 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
666 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
669 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
670 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
672 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
677 * My tests seem to show that the windows version does not
678 * become asynchronous until after this point. And anyhow
679 * if this call was asynchronous then how would you get the
680 * necessary HINTERNET pointer returned by this function.
683 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
684 lpszVersion, lpszReferrer, lpszAcceptTypes,
688 WININET_Release( &lpwhs->hdr );
689 TRACE("returning %p\n", handle);
694 /***********************************************************************
695 * HttpOpenRequestA (WININET.@)
697 * Open a HTTP request handle
700 * HINTERNET a HTTP request handle on success
704 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
705 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
706 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
707 DWORD dwFlags, DWORD dwContext)
709 LPWSTR szVerb = NULL, szObjectName = NULL;
710 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
712 INT acceptTypesCount;
713 HINTERNET rc = FALSE;
714 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
715 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
716 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
721 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
722 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
725 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
730 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
731 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
734 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
739 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
740 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
743 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
748 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
749 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
752 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
755 acceptTypesCount = 0;
758 /* find out how many there are */
759 while (lpszAcceptTypes[acceptTypesCount])
761 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
762 acceptTypesCount = 0;
763 while (lpszAcceptTypes[acceptTypesCount])
765 len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
767 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
768 if (!szAcceptTypes[acceptTypesCount] )
770 MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
771 -1, szAcceptTypes[acceptTypesCount], len );
774 szAcceptTypes[acceptTypesCount] = NULL;
776 else szAcceptTypes = 0;
778 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
779 szVersion, szReferrer,
780 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
785 acceptTypesCount = 0;
786 while (szAcceptTypes[acceptTypesCount])
788 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
791 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
793 HeapFree(GetProcessHeap(), 0, szReferrer);
794 HeapFree(GetProcessHeap(), 0, szVersion);
795 HeapFree(GetProcessHeap(), 0, szObjectName);
796 HeapFree(GetProcessHeap(), 0, szVerb);
801 /***********************************************************************
804 static UINT HTTP_Base64( LPCWSTR bin, LPWSTR base64 )
807 static LPCSTR HTTP_Base64Enc =
808 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
812 /* first 6 bits, all from bin[0] */
813 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
814 x = (bin[0] & 3) << 4;
816 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
819 base64[n++] = HTTP_Base64Enc[x];
824 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
825 x = ( bin[1] & 0x0f ) << 2;
827 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
830 base64[n++] = HTTP_Base64Enc[x];
834 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
836 /* last 6 bits, all from bin [2] */
837 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
844 /***********************************************************************
845 * HTTP_EncodeBasicAuth
847 * Encode the basic authentication string for HTTP 1.1
849 static LPWSTR HTTP_EncodeBasicAuth( LPCWSTR username, LPCWSTR password)
853 static const WCHAR szBasic[] = {'B','a','s','i','c',' ',0};
854 static const WCHAR szColon[] = {':',0};
856 len = lstrlenW( username ) + 1 + lstrlenW ( password ) + 1;
857 in = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
861 len = lstrlenW(szBasic) +
862 (lstrlenW( username ) + 1 + lstrlenW ( password ))*2 + 1 + 1;
863 out = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
866 lstrcpyW( in, username );
867 lstrcatW( in, szColon );
868 lstrcatW( in, password );
869 lstrcpyW( out, szBasic );
870 HTTP_Base64( in, &out[strlenW(out)] );
872 HeapFree( GetProcessHeap(), 0, in );
877 /***********************************************************************
878 * HTTP_InsertProxyAuthorization
880 * Insert the basic authorization field in the request header
882 static BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
883 LPCWSTR username, LPCWSTR password )
885 WCHAR *authorization = HTTP_EncodeBasicAuth( username, password );
891 TRACE( "Inserting authorization: %s\n", debugstr_w( authorization ) );
893 ret = HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_PROXY_AUTHORIZATION],
896 HeapFree( GetProcessHeap(), 0, authorization );
901 /***********************************************************************
904 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
905 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
907 WCHAR buf[MAXHOSTNAME];
908 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
910 static const WCHAR szNul[] = { 0 };
911 URL_COMPONENTSW UrlComponents;
912 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }, szSlash[] = { '/',0 } ;
913 static const WCHAR szFormat1[] = { 'h','t','t','p',':','/','/','%','s',0 };
914 static const WCHAR szFormat2[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
917 memset( &UrlComponents, 0, sizeof UrlComponents );
918 UrlComponents.dwStructSize = sizeof UrlComponents;
919 UrlComponents.lpszHostName = buf;
920 UrlComponents.dwHostNameLength = MAXHOSTNAME;
922 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
923 hIC->lpszProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
924 sprintfW(proxy, szFormat1, hIC->lpszProxy);
926 strcpyW(proxy, hIC->lpszProxy);
927 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
929 if( UrlComponents.dwHostNameLength == 0 )
932 if( !lpwhr->lpszPath )
933 lpwhr->lpszPath = (LPWSTR)szNul;
934 TRACE("server='%s' path='%s'\n",
935 debugstr_w(lpwhs->lpszHostName), debugstr_w(lpwhr->lpszPath));
936 /* for constant 15 see above */
937 len = strlenW(lpwhs->lpszHostName) + strlenW(lpwhr->lpszPath) + 15;
938 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
940 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
941 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
943 sprintfW(url, szFormat2, lpwhs->lpszHostName, lpwhs->nHostPort);
945 if( lpwhr->lpszPath[0] != '/' )
946 strcatW( url, szSlash );
947 strcatW(url, lpwhr->lpszPath);
948 if(lpwhr->lpszPath != szNul)
949 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
950 lpwhr->lpszPath = url;
952 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
953 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
954 lpwhs->nServerPort = UrlComponents.nPort;
959 /***********************************************************************
960 * HTTP_HttpOpenRequestW (internal)
962 * Open a HTTP request handle
965 * HINTERNET a HTTP request handle on success
969 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
970 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
971 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
972 DWORD dwFlags, DWORD dwContext)
974 LPWININETAPPINFOW hIC = NULL;
975 LPWININETHTTPREQW lpwhr;
977 LPWSTR lpszUrl = NULL;
979 HINTERNET handle = NULL;
980 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
985 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
986 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
988 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
991 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
994 lpwhr->hdr.htype = WH_HHTTPREQ;
995 lpwhr->hdr.lpwhparent = WININET_AddRef( &lpwhs->hdr );
996 lpwhr->hdr.dwFlags = dwFlags;
997 lpwhr->hdr.dwContext = dwContext;
998 lpwhr->hdr.dwRefCount = 1;
999 lpwhr->hdr.destroy = HTTP_CloseHTTPRequestHandle;
1000 lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
1002 handle = WININET_AllocHandle( &lpwhr->hdr );
1005 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1009 NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE);
1011 if (NULL != lpszObjectName && strlenW(lpszObjectName)) {
1015 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
1016 if (rc != E_POINTER)
1017 len = strlenW(lpszObjectName)+1;
1018 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1019 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
1020 URL_ESCAPE_SPACES_ONLY);
1023 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(lpszObjectName),rc);
1024 strcpyW(lpwhr->lpszPath,lpszObjectName);
1028 if (NULL != lpszReferrer && strlenW(lpszReferrer))
1029 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDHDR_FLAG_COALESCE);
1031 if(lpszAcceptTypes!=NULL)
1034 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
1035 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i], HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_REQ|HTTP_ADDHDR_FLAG_ADD_IF_NEW);
1038 if (NULL == lpszVerb)
1040 static const WCHAR szGet[] = {'G','E','T',0};
1041 lpwhr->lpszVerb = WININET_strdupW(szGet);
1043 else if (strlenW(lpszVerb))
1044 lpwhr->lpszVerb = WININET_strdupW(lpszVerb);
1046 if (NULL != lpszReferrer && strlenW(lpszReferrer))
1048 WCHAR buf[MAXHOSTNAME];
1049 URL_COMPONENTSW UrlComponents;
1051 memset( &UrlComponents, 0, sizeof UrlComponents );
1052 UrlComponents.dwStructSize = sizeof UrlComponents;
1053 UrlComponents.lpszHostName = buf;
1054 UrlComponents.dwHostNameLength = MAXHOSTNAME;
1056 InternetCrackUrlW(lpszReferrer, 0, 0, &UrlComponents);
1057 if (strlenW(UrlComponents.lpszHostName))
1058 HTTP_ProcessHeader(lpwhr, g_szHost, UrlComponents.lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1061 HTTP_ProcessHeader(lpwhr, g_szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1063 if (lpwhs->nServerPort == INTERNET_INVALID_PORT_NUMBER)
1064 lpwhs->nServerPort = (dwFlags & INTERNET_FLAG_SECURE ?
1065 INTERNET_DEFAULT_HTTPS_PORT :
1066 INTERNET_DEFAULT_HTTP_PORT);
1067 lpwhs->nHostPort = lpwhs->nServerPort;
1069 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1070 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
1074 WCHAR *agent_header;
1075 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
1077 len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
1078 agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1079 sprintfW(agent_header, user_agent, hIC->lpszAgent );
1081 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
1082 HTTP_ADDREQ_FLAG_ADD);
1083 HeapFree(GetProcessHeap(), 0, agent_header);
1086 len = strlenW(lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue) + strlenW(szUrlForm);
1087 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1088 sprintfW( lpszUrl, szUrlForm, lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue );
1090 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
1091 InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
1094 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
1095 static const WCHAR szcrlf[] = {'\r','\n',0};
1097 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
1099 cnt += sprintfW(lpszCookies, szCookie);
1100 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
1101 strcatW(lpszCookies, szcrlf);
1103 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
1104 HTTP_ADDREQ_FLAG_ADD);
1105 HeapFree(GetProcessHeap(), 0, lpszCookies);
1107 HeapFree(GetProcessHeap(), 0, lpszUrl);
1110 SendAsyncCallback(&lpwhs->hdr, dwContext,
1111 INTERNET_STATUS_HANDLE_CREATED, &handle,
1115 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
1119 * According to my tests. The name is not resolved until a request is Opened
1121 SendAsyncCallback(&lpwhr->hdr, dwContext,
1122 INTERNET_STATUS_RESOLVING_NAME,
1123 lpwhs->lpszServerName,
1124 strlenW(lpwhs->lpszServerName)+1);
1126 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1127 &lpwhs->socketAddress))
1129 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1130 InternetCloseHandle( handle );
1135 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1136 INTERNET_STATUS_NAME_RESOLVED,
1137 &(lpwhs->socketAddress),
1138 sizeof(struct sockaddr_in));
1142 WININET_Release( &lpwhr->hdr );
1144 TRACE("<-- %p (%p)\n", handle, lpwhr);
1148 /***********************************************************************
1149 * HTTP_HttpQueryInfoW (internal)
1151 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
1152 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1154 LPHTTPHEADERW lphttpHdr = NULL;
1155 BOOL bSuccess = FALSE;
1157 /* Find requested header structure */
1158 if ((dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK) == HTTP_QUERY_CUSTOM)
1160 INT index = HTTP_GetCustomHeaderIndex(lpwhr, (LPWSTR)lpBuffer);
1165 lphttpHdr = &lpwhr->pCustHeaders[index];
1169 INT index = dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK;
1171 if (index == HTTP_QUERY_RAW_HEADERS_CRLF)
1173 DWORD len = strlenW(lpwhr->lpszRawHeaders);
1174 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1176 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1177 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1180 memcpy(lpBuffer, lpwhr->lpszRawHeaders, (len+1)*sizeof(WCHAR));
1181 *lpdwBufferLength = len * sizeof(WCHAR);
1183 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1187 else if (index == HTTP_QUERY_RAW_HEADERS)
1189 static const WCHAR szCrLf[] = {'\r','\n',0};
1190 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
1192 LPWSTR pszString = (WCHAR*)lpBuffer;
1194 for (i = 0; ppszRawHeaderLines[i]; i++)
1195 size += strlenW(ppszRawHeaderLines[i]) + 1;
1197 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
1199 HTTP_FreeTokens(ppszRawHeaderLines);
1200 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
1201 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1205 for (i = 0; ppszRawHeaderLines[i]; i++)
1207 DWORD len = strlenW(ppszRawHeaderLines[i]);
1208 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
1213 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
1215 *lpdwBufferLength = size * sizeof(WCHAR);
1216 HTTP_FreeTokens(ppszRawHeaderLines);
1220 else if (index >= 0 && index <= HTTP_QUERY_MAX && lpwhr->StdHeaders[index].lpszValue)
1222 lphttpHdr = &lpwhr->StdHeaders[index];
1226 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1231 /* Ensure header satisifies requested attributes */
1232 if ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
1233 (~lphttpHdr->wFlags & HDR_ISREQUEST))
1235 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1239 /* coalesce value to reuqested type */
1240 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
1242 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
1245 TRACE(" returning number : %d\n", *(int *)lpBuffer);
1247 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
1253 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
1255 tmpTM = *gmtime(&tmpTime);
1256 STHook = (SYSTEMTIME *) lpBuffer;
1260 STHook->wDay = tmpTM.tm_mday;
1261 STHook->wHour = tmpTM.tm_hour;
1262 STHook->wMilliseconds = 0;
1263 STHook->wMinute = tmpTM.tm_min;
1264 STHook->wDayOfWeek = tmpTM.tm_wday;
1265 STHook->wMonth = tmpTM.tm_mon + 1;
1266 STHook->wSecond = tmpTM.tm_sec;
1267 STHook->wYear = tmpTM.tm_year;
1271 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
1272 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
1273 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
1275 else if (dwInfoLevel & HTTP_QUERY_FLAG_COALESCE)
1277 if (*lpdwIndex >= lphttpHdr->wCount)
1279 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1283 /* Copy strncpyW(lpBuffer, lphttpHdr[*lpdwIndex], len); */
1289 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
1291 if (len > *lpdwBufferLength)
1293 *lpdwBufferLength = len;
1294 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1298 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
1299 *lpdwBufferLength = len - sizeof(WCHAR);
1302 TRACE(" returning string : '%s'\n", debugstr_w(lpBuffer));
1307 /***********************************************************************
1308 * HttpQueryInfoW (WININET.@)
1310 * Queries for information about an HTTP request
1317 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1318 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1320 BOOL bSuccess = FALSE;
1321 LPWININETHTTPREQW lpwhr;
1323 if (TRACE_ON(wininet)) {
1324 #define FE(x) { x, #x }
1325 static const wininet_flag_info query_flags[] = {
1326 FE(HTTP_QUERY_MIME_VERSION),
1327 FE(HTTP_QUERY_CONTENT_TYPE),
1328 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
1329 FE(HTTP_QUERY_CONTENT_ID),
1330 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
1331 FE(HTTP_QUERY_CONTENT_LENGTH),
1332 FE(HTTP_QUERY_CONTENT_LANGUAGE),
1333 FE(HTTP_QUERY_ALLOW),
1334 FE(HTTP_QUERY_PUBLIC),
1335 FE(HTTP_QUERY_DATE),
1336 FE(HTTP_QUERY_EXPIRES),
1337 FE(HTTP_QUERY_LAST_MODIFIED),
1338 FE(HTTP_QUERY_MESSAGE_ID),
1340 FE(HTTP_QUERY_DERIVED_FROM),
1341 FE(HTTP_QUERY_COST),
1342 FE(HTTP_QUERY_LINK),
1343 FE(HTTP_QUERY_PRAGMA),
1344 FE(HTTP_QUERY_VERSION),
1345 FE(HTTP_QUERY_STATUS_CODE),
1346 FE(HTTP_QUERY_STATUS_TEXT),
1347 FE(HTTP_QUERY_RAW_HEADERS),
1348 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
1349 FE(HTTP_QUERY_CONNECTION),
1350 FE(HTTP_QUERY_ACCEPT),
1351 FE(HTTP_QUERY_ACCEPT_CHARSET),
1352 FE(HTTP_QUERY_ACCEPT_ENCODING),
1353 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
1354 FE(HTTP_QUERY_AUTHORIZATION),
1355 FE(HTTP_QUERY_CONTENT_ENCODING),
1356 FE(HTTP_QUERY_FORWARDED),
1357 FE(HTTP_QUERY_FROM),
1358 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
1359 FE(HTTP_QUERY_LOCATION),
1360 FE(HTTP_QUERY_ORIG_URI),
1361 FE(HTTP_QUERY_REFERER),
1362 FE(HTTP_QUERY_RETRY_AFTER),
1363 FE(HTTP_QUERY_SERVER),
1364 FE(HTTP_QUERY_TITLE),
1365 FE(HTTP_QUERY_USER_AGENT),
1366 FE(HTTP_QUERY_WWW_AUTHENTICATE),
1367 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
1368 FE(HTTP_QUERY_ACCEPT_RANGES),
1369 FE(HTTP_QUERY_SET_COOKIE),
1370 FE(HTTP_QUERY_COOKIE),
1371 FE(HTTP_QUERY_REQUEST_METHOD),
1372 FE(HTTP_QUERY_REFRESH),
1373 FE(HTTP_QUERY_CONTENT_DISPOSITION),
1375 FE(HTTP_QUERY_CACHE_CONTROL),
1376 FE(HTTP_QUERY_CONTENT_BASE),
1377 FE(HTTP_QUERY_CONTENT_LOCATION),
1378 FE(HTTP_QUERY_CONTENT_MD5),
1379 FE(HTTP_QUERY_CONTENT_RANGE),
1380 FE(HTTP_QUERY_ETAG),
1381 FE(HTTP_QUERY_HOST),
1382 FE(HTTP_QUERY_IF_MATCH),
1383 FE(HTTP_QUERY_IF_NONE_MATCH),
1384 FE(HTTP_QUERY_IF_RANGE),
1385 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
1386 FE(HTTP_QUERY_MAX_FORWARDS),
1387 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
1388 FE(HTTP_QUERY_RANGE),
1389 FE(HTTP_QUERY_TRANSFER_ENCODING),
1390 FE(HTTP_QUERY_UPGRADE),
1391 FE(HTTP_QUERY_VARY),
1393 FE(HTTP_QUERY_WARNING),
1394 FE(HTTP_QUERY_CUSTOM)
1396 static const wininet_flag_info modifier_flags[] = {
1397 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
1398 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
1399 FE(HTTP_QUERY_FLAG_NUMBER),
1400 FE(HTTP_QUERY_FLAG_COALESCE)
1403 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
1404 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
1407 TRACE("(%p, 0x%08lx)--> %ld\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
1408 TRACE(" Attribute:");
1409 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
1410 if (query_flags[i].val == info) {
1411 TRACE(" %s", query_flags[i].name);
1415 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
1416 TRACE(" Unknown (%08lx)", info);
1419 TRACE(" Modifier:");
1420 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
1421 if (modifier_flags[i].val & info_mod) {
1422 TRACE(" %s", modifier_flags[i].name);
1423 info_mod &= ~ modifier_flags[i].val;
1428 TRACE(" Unknown (%08lx)", info_mod);
1433 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1434 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1436 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1440 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
1441 lpBuffer, lpdwBufferLength, lpdwIndex);
1445 WININET_Release( &lpwhr->hdr );
1447 TRACE("%d <--\n", bSuccess);
1451 /***********************************************************************
1452 * HttpQueryInfoA (WININET.@)
1454 * Queries for information about an HTTP request
1461 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1462 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1468 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
1469 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
1471 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
1472 lpdwBufferLength, lpdwIndex );
1475 len = (*lpdwBufferLength)*sizeof(WCHAR);
1476 bufferW = HeapAlloc( GetProcessHeap(), 0, len );
1477 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
1481 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
1482 lpBuffer, *lpdwBufferLength, NULL, NULL );
1483 *lpdwBufferLength = len - 1;
1485 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
1488 /* since the strings being returned from HttpQueryInfoW should be
1489 * only ASCII characters, it is reasonable to assume that all of
1490 * the Unicode characters can be reduced to a single byte */
1491 *lpdwBufferLength = len / sizeof(WCHAR);
1493 HeapFree(GetProcessHeap(), 0, bufferW );
1498 /***********************************************************************
1499 * HttpSendRequestExA (WININET.@)
1501 * Sends the specified request to the HTTP server and allows chunked
1504 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
1505 LPINTERNET_BUFFERSA lpBuffersIn,
1506 LPINTERNET_BUFFERSA lpBuffersOut,
1507 DWORD dwFlags, DWORD dwContext)
1509 LPINTERNET_BUFFERSA ptr;
1510 LPINTERNET_BUFFERSW lpBuffersInW,ptrW;
1513 TRACE("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersIn,
1514 lpBuffersOut, dwFlags, dwContext);
1518 lpBuffersInW = (LPINTERNET_BUFFERSW)HeapAlloc(GetProcessHeap(),
1519 HEAP_ZERO_MEMORY, sizeof(INTERNET_BUFFERSW));
1521 lpBuffersInW = NULL;
1523 ptrW = lpBuffersInW;
1527 ptrW->dwStructSize = sizeof(LPINTERNET_BUFFERSW);
1528 if (ptr->lpcszHeader)
1530 headerlen = MultiByteToWideChar(CP_ACP,0,ptr->lpcszHeader,
1531 ptr->dwHeadersLength,0,0);
1533 ptrW->lpcszHeader = HeapAlloc(GetProcessHeap(),0,headerlen*
1535 ptrW->dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
1536 ptr->lpcszHeader, ptr->dwHeadersLength,
1537 (LPWSTR)ptrW->lpcszHeader, headerlen);
1539 ptrW->dwHeadersTotal = ptr->dwHeadersTotal;
1540 ptrW->lpvBuffer = ptr->lpvBuffer;
1541 ptrW->dwBufferLength = ptr->dwBufferLength;
1542 ptrW->dwBufferTotal= ptr->dwBufferTotal;
1545 ptrW->Next = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
1546 sizeof(INTERNET_BUFFERSW));
1552 rc = HttpSendRequestExW(hRequest, lpBuffersInW, NULL, dwFlags, dwContext);
1555 ptrW = lpBuffersInW;
1558 LPINTERNET_BUFFERSW ptrW2;
1559 HeapFree(GetProcessHeap(),0,(LPVOID)ptrW->lpcszHeader);
1561 HeapFree(GetProcessHeap(),0,ptrW);
1568 /***********************************************************************
1569 * HttpSendRequestExW (WININET.@)
1571 * Sends the specified request to the HTTP server and allows chunked
1574 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
1575 LPINTERNET_BUFFERSW lpBuffersIn,
1576 LPINTERNET_BUFFERSW lpBuffersOut,
1577 DWORD dwFlags, DWORD dwContext)
1579 LPINTERNET_BUFFERSW buf_ptr;
1580 DWORD bufferlen = 0;
1582 LPWININETHTTPREQW lpwhr;
1583 LPWSTR requestString = NULL;
1588 TRACE("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersIn,
1589 lpBuffersOut, dwFlags, dwContext);
1591 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
1593 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1595 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1599 HTTP_FixVerb(lpwhr);
1602 buf_ptr = lpBuffersIn;
1605 if (buf_ptr->lpcszHeader)
1607 HTTP_HttpAddRequestHeadersW(lpwhr, buf_ptr->lpcszHeader,
1608 buf_ptr->dwHeadersLength, HTTP_ADDREQ_FLAG_ADD |
1609 HTTP_ADDHDR_FLAG_REPLACE);
1611 bufferlen += buf_ptr->dwBufferTotal;
1612 buf_ptr = buf_ptr->Next;
1615 lpwhr->hdr.dwFlags |= dwFlags;
1616 lpwhr->hdr.dwContext = dwContext;
1620 static const WCHAR szContentLength[] = {
1621 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ',
1622 '%','l','i','\r','\n',0};
1623 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \n\r */ +
1625 sprintfW(contentLengthStr, szContentLength, bufferlen);
1626 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L,
1627 HTTP_ADDREQ_FLAG_ADD);
1632 /* Do Send Request logic */
1634 TRACE("Going to url %s %s\n",
1635 debugstr_w(lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue),
1636 debugstr_w(lpwhr->lpszPath));
1638 HTTP_AddProxyInfo(lpwhr);
1640 requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, FALSE);
1642 TRACE("Request header -> %s\n", debugstr_w(requestString) );
1644 if (!(rc = HTTP_OpenConnection(lpwhr)))
1647 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1648 INTERNET_STATUS_CONNECTED_TO_SERVER, NULL, 0);
1650 /* send the request as ASCII */
1651 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1, NULL, 0, NULL,
1654 /* we do not want the null because optional data will be sent with following
1655 * InternetWriteFile calls
1658 ascii_req = HeapAlloc( GetProcessHeap(), 0, len);
1659 WideCharToMultiByte( CP_ACP, 0, requestString, -1, ascii_req, len, NULL,
1661 TRACE("full request -> %s\n", debugstr_an(ascii_req,len) );
1663 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1664 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
1666 rc = NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
1667 HeapFree( GetProcessHeap(), 0, ascii_req );
1669 /* This may not be sent here. It may come laster */
1670 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1671 INTERNET_STATUS_REQUEST_SENT, &len,sizeof(DWORD));
1674 HeapFree(GetProcessHeap(), 0, requestString);
1675 WININET_Release(&lpwhr->hdr);
1680 /***********************************************************************
1681 * HttpSendRequestW (WININET.@)
1683 * Sends the specified request to the HTTP server
1690 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
1691 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1693 LPWININETHTTPREQW lpwhr;
1694 LPWININETHTTPSESSIONW lpwhs = NULL;
1695 LPWININETAPPINFOW hIC = NULL;
1698 TRACE("%p, %p (%s), %li, %p, %li)\n", hHttpRequest,
1699 lpszHeaders, debugstr_w(lpszHeaders), dwHeaderLength, lpOptional, dwOptionalLength);
1701 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1702 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1704 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1709 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1710 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1712 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1717 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1718 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1720 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1725 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1727 WORKREQUEST workRequest;
1728 struct WORKREQ_HTTPSENDREQUESTW *req;
1730 workRequest.asyncall = HTTPSENDREQUESTW;
1731 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
1732 req = &workRequest.u.HttpSendRequestW;
1734 req->lpszHeader = WININET_strdupW(lpszHeaders);
1736 req->lpszHeader = 0;
1737 req->dwHeaderLength = dwHeaderLength;
1738 req->lpOptional = lpOptional;
1739 req->dwOptionalLength = dwOptionalLength;
1741 INTERNET_AsyncCall(&workRequest);
1743 * This is from windows.
1745 SetLastError(ERROR_IO_PENDING);
1750 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
1751 dwHeaderLength, lpOptional, dwOptionalLength);
1755 WININET_Release( &lpwhr->hdr );
1759 /***********************************************************************
1760 * HttpSendRequestA (WININET.@)
1762 * Sends the specified request to the HTTP server
1769 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
1770 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1773 LPWSTR szHeaders=NULL;
1774 DWORD nLen=dwHeaderLength;
1775 if(lpszHeaders!=NULL)
1777 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
1778 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
1779 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
1781 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
1782 HeapFree(GetProcessHeap(),0,szHeaders);
1786 /***********************************************************************
1787 * HTTP_HandleRedirect (internal)
1789 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl, LPCWSTR lpszHeaders,
1790 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength)
1792 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1793 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1798 /* if it's an absolute path, keep the same session info */
1799 strcpyW(path,lpszUrl);
1801 else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1803 TRACE("Redirect through proxy\n");
1804 strcpyW(path,lpszUrl);
1808 URL_COMPONENTSW urlComponents;
1809 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
1810 WCHAR password[1024], extra[1024];
1817 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
1818 urlComponents.lpszScheme = protocol;
1819 urlComponents.dwSchemeLength = 32;
1820 urlComponents.lpszHostName = hostName;
1821 urlComponents.dwHostNameLength = MAXHOSTNAME;
1822 urlComponents.lpszUserName = userName;
1823 urlComponents.dwUserNameLength = 1024;
1824 urlComponents.lpszPassword = password;
1825 urlComponents.dwPasswordLength = 1024;
1826 urlComponents.lpszUrlPath = path;
1827 urlComponents.dwUrlPathLength = 2048;
1828 urlComponents.lpszExtraInfo = extra;
1829 urlComponents.dwExtraInfoLength = 1024;
1830 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
1833 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1835 if (lstrlenW(protocol)>4) /*https*/
1836 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
1838 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1843 * This upsets redirects to binary files on sourceforge.net
1844 * and gives an html page instead of the target file
1845 * Examination of the HTTP request sent by native wininet.dll
1846 * reveals that it doesn't send a referrer in that case.
1847 * Maybe there's a flag that enables this, or maybe a referrer
1848 * shouldn't be added in case of a redirect.
1851 /* consider the current host as the referrer */
1852 if (NULL != lpwhs->lpszServerName && strlenW(lpwhs->lpszServerName))
1853 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
1854 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
1855 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
1858 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
1859 lpwhs->lpszHostName = WININET_strdupW(hostName);
1860 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1861 if (urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT &&
1862 urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT)
1865 static WCHAR fmt[] = {'%','s',':','%','i',0};
1866 len = lstrlenW(hostName);
1868 lpwhs->lpszServerName = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
1869 sprintfW(lpwhs->lpszServerName,fmt,hostName,urlComponents.nPort);
1872 lpwhs->lpszServerName = WININET_strdupW(hostName);
1874 HTTP_ProcessHeader(lpwhr, g_szHost, lpwhs->lpszServerName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1877 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
1878 lpwhs->lpszUserName = WININET_strdupW(userName);
1879 lpwhs->nServerPort = urlComponents.nPort;
1881 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1882 INTERNET_STATUS_RESOLVING_NAME,
1883 lpwhs->lpszServerName,
1884 strlenW(lpwhs->lpszServerName)+1);
1886 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1887 &lpwhs->socketAddress))
1889 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1893 StrCatW(path,extra);
1895 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1896 INTERNET_STATUS_NAME_RESOLVED,
1897 &(lpwhs->socketAddress),
1898 sizeof(struct sockaddr_in));
1902 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1903 lpwhr->lpszPath=NULL;
1909 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
1910 if (rc != E_POINTER)
1911 needed = strlenW(path)+1;
1912 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
1913 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
1914 URL_ESCAPE_SPACES_ONLY);
1917 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(path),rc);
1918 strcpyW(lpwhr->lpszPath,path);
1922 return HTTP_HttpSendRequestW(lpwhr, lpszHeaders, dwHeaderLength, lpOptional, dwOptionalLength);
1925 /***********************************************************************
1926 * HTTP_build_req (internal)
1928 * concatenate all the strings in the request together
1930 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
1935 for( t = list; *t ; t++ )
1936 len += strlenW( *t );
1939 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1942 for( t = list; *t ; t++ )
1948 static BOOL HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr)
1951 LPWSTR requestString;
1957 static const WCHAR szConnect[] = {'C','O','N','N','E','C','T',0};
1958 static const WCHAR szFormat[] = {'%','s',':','%','d',0};
1959 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
1963 lpszPath = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs->lpszHostName ) + 13)*sizeof(WCHAR) );
1964 sprintfW( lpszPath, szFormat, lpwhs->lpszHostName, lpwhs->nHostPort );
1965 requestString = HTTP_BuildHeaderRequestString( lpwhr, szConnect, lpszPath, FALSE );
1966 HeapFree( GetProcessHeap(), 0, lpszPath );
1968 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
1969 NULL, 0, NULL, NULL );
1970 len--; /* the nul terminator isn't needed */
1971 ascii_req = HeapAlloc( GetProcessHeap(), 0, len );
1972 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
1973 ascii_req, len, NULL, NULL );
1974 HeapFree( GetProcessHeap(), 0, requestString );
1976 TRACE("full request -> %s\n", debugstr_an( ascii_req, len ) );
1978 ret = NETCON_send( &lpwhr->netConnection, ascii_req, len, 0, &cnt );
1979 HeapFree( GetProcessHeap(), 0, ascii_req );
1980 if (!ret || cnt < 0)
1983 responseLen = HTTP_GetResponseHeaders( lpwhr );
1990 /***********************************************************************
1991 * HTTP_HttpSendRequestW (internal)
1993 * Sends the specified request to the HTTP server
2000 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
2001 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
2004 BOOL bSuccess = FALSE;
2005 LPWSTR requestString = NULL;
2007 BOOL loop_next = FALSE;
2008 INTERNET_ASYNC_RESULT iar;
2010 TRACE("--> %p\n", lpwhr);
2012 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
2014 /* Clear any error information */
2015 INTERNET_SetLastError(0);
2017 HTTP_FixVerb(lpwhr);
2019 /* if we are using optional stuff, we must add the fixed header of that option length */
2020 if (lpOptional && dwOptionalLength)
2022 static const WCHAR szContentLength[] = {
2023 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0};
2024 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \n\r */ + 20 /* int */ ];
2025 sprintfW(contentLengthStr, szContentLength, dwOptionalLength);
2026 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD);
2034 TRACE("Going to url %s %s\n", debugstr_w(lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue), debugstr_w(lpwhr->lpszPath));
2039 /* add the headers the caller supplied */
2040 if( lpszHeaders && dwHeaderLength )
2042 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
2043 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
2046 /* if there's a proxy username and password, add it to the headers */
2047 HTTP_AddProxyInfo(lpwhr);
2049 requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, FALSE);
2051 TRACE("Request header -> %s\n", debugstr_w(requestString) );
2053 /* Send the request and store the results */
2054 if (!HTTP_OpenConnection(lpwhr))
2057 /* send the request as ASCII, tack on the optional data */
2059 dwOptionalLength = 0;
2060 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2061 NULL, 0, NULL, NULL );
2062 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
2063 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2064 ascii_req, len, NULL, NULL );
2066 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
2067 len = (len + dwOptionalLength - 1);
2069 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
2071 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2072 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
2074 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
2075 HeapFree( GetProcessHeap(), 0, ascii_req );
2077 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2078 INTERNET_STATUS_REQUEST_SENT,
2079 &len,sizeof(DWORD));
2081 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2082 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2087 responseLen = HTTP_GetResponseHeaders(lpwhr);
2091 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2092 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
2095 /* process headers here. Is this right? */
2096 HTTP_ProcessHeaders(lpwhr);
2102 HeapFree(GetProcessHeap(), 0, requestString);
2104 /* TODO: send notification for P3P header */
2106 if(!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
2108 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
2109 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
2110 (dwCode==302 || dwCode==301))
2112 WCHAR szNewLocation[2048];
2113 DWORD dwBufferSize=2048;
2115 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
2117 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2118 INTERNET_STATUS_REDIRECT, szNewLocation,
2120 return HTTP_HandleRedirect(lpwhr, szNewLocation, lpszHeaders,
2121 dwHeaderLength, lpOptional, dwOptionalLength);
2127 iar.dwResult = (DWORD)bSuccess;
2128 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
2130 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2131 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2132 sizeof(INTERNET_ASYNC_RESULT));
2139 /***********************************************************************
2140 * HTTP_Connect (internal)
2142 * Create http session handle
2145 * HINTERNET a session handle on success
2149 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
2150 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
2151 LPCWSTR lpszPassword, DWORD dwFlags, DWORD dwContext,
2152 DWORD dwInternalFlags)
2154 BOOL bSuccess = FALSE;
2155 LPWININETHTTPSESSIONW lpwhs = NULL;
2156 HINTERNET handle = NULL;
2160 assert( hIC->hdr.htype == WH_HINIT );
2162 hIC->hdr.dwContext = dwContext;
2164 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
2167 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2172 * According to my tests. The name is not resolved until a request is sent
2175 lpwhs->hdr.htype = WH_HHTTPSESSION;
2176 lpwhs->hdr.lpwhparent = WININET_AddRef( &hIC->hdr );
2177 lpwhs->hdr.dwFlags = dwFlags;
2178 lpwhs->hdr.dwContext = dwContext;
2179 lpwhs->hdr.dwInternalFlags = dwInternalFlags;
2180 lpwhs->hdr.dwRefCount = 1;
2181 lpwhs->hdr.destroy = HTTP_CloseHTTPSessionHandle;
2182 lpwhs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
2184 handle = WININET_AllocHandle( &lpwhs->hdr );
2187 ERR("Failed to alloc handle\n");
2188 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2192 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
2193 if(strchrW(hIC->lpszProxy, ' '))
2194 FIXME("Several proxies not implemented.\n");
2195 if(hIC->lpszProxyBypass)
2196 FIXME("Proxy bypass is ignored.\n");
2198 if (NULL != lpszServerName)
2200 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
2201 lpwhs->lpszHostName = WININET_strdupW(lpszServerName);
2203 if (NULL != lpszUserName)
2204 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
2205 lpwhs->nServerPort = nServerPort;
2206 lpwhs->nHostPort = nServerPort;
2208 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2209 if (!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
2211 SendAsyncCallback(&hIC->hdr, dwContext,
2212 INTERNET_STATUS_HANDLE_CREATED, &handle,
2220 WININET_Release( &lpwhs->hdr );
2223 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
2227 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
2232 /***********************************************************************
2233 * HTTP_OpenConnection (internal)
2235 * Connect to a web server
2242 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
2244 BOOL bSuccess = FALSE;
2245 LPWININETHTTPSESSIONW lpwhs;
2246 LPWININETAPPINFOW hIC = NULL;
2251 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2253 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2257 lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
2259 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2260 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2261 INTERNET_STATUS_CONNECTING_TO_SERVER,
2262 &(lpwhs->socketAddress),
2263 sizeof(struct sockaddr_in));
2265 if (!NETCON_create(&lpwhr->netConnection, lpwhs->socketAddress.sin_family,
2268 WARN("Socket creation failed\n");
2272 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
2273 sizeof(lpwhs->socketAddress)))
2275 WARN("Unable to connect to host (%s)\n", strerror(errno));
2279 if (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE)
2281 /* Note: we differ from Microsoft's WinINet here. they seem to have
2282 * a bug that causes no status callbacks to be sent when starting
2283 * a tunnel to a proxy server using the CONNECT verb. i believe our
2284 * behaviour to be more correct and to not cause any incompatibilities
2285 * because using a secure connection through a proxy server is a rare
2286 * case that would be hard for anyone to depend on */
2287 if (hIC->lpszProxy && !HTTP_SecureProxyConnect(lpwhr))
2290 if (!NETCON_secure_connect(&lpwhr->netConnection, lpwhs->lpszHostName))
2292 WARN("Couldn't connect securely to host\n");
2297 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2298 INTERNET_STATUS_CONNECTED_TO_SERVER,
2299 &(lpwhs->socketAddress),
2300 sizeof(struct sockaddr_in));
2305 TRACE("%d <--\n", bSuccess);
2310 /***********************************************************************
2311 * HTTP_clear_response_headers (internal)
2313 * clear out any old response headers
2315 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
2319 for( i=0; i<=HTTP_QUERY_MAX; i++ )
2321 if( !lpwhr->StdHeaders[i].lpszField )
2323 if( !lpwhr->StdHeaders[i].lpszValue )
2325 if ( lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST )
2327 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[i], NULL );
2328 HeapFree( GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszField );
2329 lpwhr->StdHeaders[i].lpszField = NULL;
2331 for( i=0; i<lpwhr->nCustHeaders; i++)
2333 if( !lpwhr->pCustHeaders[i].lpszField )
2335 if( !lpwhr->pCustHeaders[i].lpszValue )
2337 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
2339 HTTP_DeleteCustomHeader( lpwhr, i );
2344 /***********************************************************************
2345 * HTTP_GetResponseHeaders (internal)
2347 * Read server response
2354 static INT HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr)
2357 WCHAR buffer[MAX_REPLY_LEN];
2358 DWORD buflen = MAX_REPLY_LEN;
2359 BOOL bSuccess = FALSE;
2361 static const WCHAR szCrLf[] = {'\r','\n',0};
2362 char bufferA[MAX_REPLY_LEN];
2363 LPWSTR status_code, status_text;
2364 DWORD cchMaxRawHeaders = 1024;
2365 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2366 DWORD cchRawHeaders = 0;
2370 /* clear old response headers (eg. from a redirect response) */
2371 HTTP_clear_response_headers( lpwhr );
2373 if (!NETCON_connected(&lpwhr->netConnection))
2377 * HACK peek at the buffer
2379 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
2382 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
2384 buflen = MAX_REPLY_LEN;
2385 memset(buffer, 0, MAX_REPLY_LEN);
2386 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2388 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2390 /* regenerate raw headers */
2391 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2393 cchMaxRawHeaders *= 2;
2394 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2396 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2397 cchRawHeaders += (buflen-1);
2398 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2399 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2400 lpszRawHeaders[cchRawHeaders] = '\0';
2402 /* split the version from the status code */
2403 status_code = strchrW( buffer, ' ' );
2408 /* split the status code from the status text */
2409 status_text = strchrW( status_code, ' ' );
2414 TRACE("version [%s] status code [%s] status text [%s]\n",
2415 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
2416 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_VERSION], buffer );
2417 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_STATUS_CODE], status_code );
2418 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_STATUS_TEXT], status_text );
2420 /* Parse each response line */
2423 buflen = MAX_REPLY_LEN;
2424 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2426 LPWSTR * pFieldAndValue;
2428 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
2429 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2431 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2433 cchMaxRawHeaders *= 2;
2434 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2436 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2437 cchRawHeaders += (buflen-1);
2438 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2439 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2440 lpszRawHeaders[cchRawHeaders] = '\0';
2442 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
2443 if (!pFieldAndValue)
2446 HTTP_ProcessHeader(lpwhr, pFieldAndValue[0], pFieldAndValue[1],
2447 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE);
2449 HTTP_FreeTokens(pFieldAndValue);
2459 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2460 lpwhr->lpszRawHeaders = lpszRawHeaders;
2461 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
2474 static void strip_spaces(LPWSTR start)
2479 while (*str == ' ' && *str != '\0')
2483 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
2485 end = start + strlenW(start) - 1;
2486 while (end >= start && *end == ' ')
2494 /***********************************************************************
2495 * HTTP_InterpretHttpHeader (internal)
2497 * Parse server response
2501 * Pointer to array of field, value, NULL on success.
2504 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
2506 LPWSTR * pTokenPair;
2510 pTokenPair = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pTokenPair)*3);
2512 pszColon = strchrW(buffer, ':');
2513 /* must have two tokens */
2516 HTTP_FreeTokens(pTokenPair);
2518 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
2522 pTokenPair[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon - buffer + 1) * sizeof(WCHAR));
2525 HTTP_FreeTokens(pTokenPair);
2528 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
2529 pTokenPair[0][pszColon - buffer] = '\0';
2533 len = strlenW(pszColon);
2534 pTokenPair[1] = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
2537 HTTP_FreeTokens(pTokenPair);
2540 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
2542 strip_spaces(pTokenPair[0]);
2543 strip_spaces(pTokenPair[1]);
2545 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
2549 typedef enum {REQUEST_HDR = 1, RESPONSE_HDR = 2, REQ_RESP_HDR = 3} std_hdr_type;
2551 typedef struct std_hdr_data
2553 const WCHAR* hdrStr;
2555 std_hdr_type hdrType;
2558 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
2559 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
2560 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
2561 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
2562 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
2563 static const WCHAR szAge[] = { 'A','g','e',0 };
2564 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
2565 static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
2566 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
2567 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
2568 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
2569 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
2570 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
2571 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
2572 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
2573 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
2574 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
2575 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
2576 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 };
2577 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
2578 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
2579 static const WCHAR szDate[] = { 'D','a','t','e',0 };
2580 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
2581 static const WCHAR szETag[] = { 'E','T','a','g',0 };
2582 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
2583 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
2584 static const WCHAR szHost[] = { 'H','o','s','t',0 };
2585 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
2586 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
2587 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
2588 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
2589 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
2590 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
2591 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
2592 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
2593 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
2594 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
2595 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
2596 static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
2597 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
2598 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
2599 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
2600 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
2601 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
2602 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
2603 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
2604 static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 };
2605 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
2606 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 };
2607 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
2608 static const WCHAR szURI[] = { 'U','R','I',0 };
2609 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
2610 static const WCHAR szVary[] = { 'V','a','r','y',0 };
2611 static const WCHAR szVia[] = { 'V','i','a',0 };
2612 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
2613 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
2615 /* Note: Must be kept sorted! */
2616 const std_hdr_data SORTED_STANDARD_HEADERS[] = {
2617 {szAccept, HTTP_QUERY_ACCEPT, REQUEST_HDR,},
2618 {szAccept_Charset, HTTP_QUERY_ACCEPT_CHARSET, REQUEST_HDR,},
2619 {szAccept_Encoding, HTTP_QUERY_ACCEPT_ENCODING, REQUEST_HDR,},
2620 {szAccept_Language, HTTP_QUERY_ACCEPT_LANGUAGE, REQUEST_HDR,},
2621 {szAccept_Ranges, HTTP_QUERY_ACCEPT_RANGES, RESPONSE_HDR,},
2622 {szAge, HTTP_QUERY_AGE, RESPONSE_HDR,},
2623 {szAllow, HTTP_QUERY_ALLOW, REQ_RESP_HDR,},
2624 {szAuthorization, HTTP_QUERY_AUTHORIZATION, REQUEST_HDR,},
2625 {szCache_Control, HTTP_QUERY_CACHE_CONTROL, REQ_RESP_HDR,},
2626 {szConnection, HTTP_QUERY_CONNECTION, REQ_RESP_HDR,},
2627 {szContent_Base, HTTP_QUERY_CONTENT_BASE, REQ_RESP_HDR,},
2628 {szContent_Encoding, HTTP_QUERY_CONTENT_ENCODING, REQ_RESP_HDR,},
2629 {szContent_ID, HTTP_QUERY_CONTENT_ID, REQ_RESP_HDR,},
2630 {szContent_Language, HTTP_QUERY_CONTENT_LANGUAGE, REQ_RESP_HDR,},
2631 {szContent_Length, HTTP_QUERY_CONTENT_LENGTH, REQ_RESP_HDR,},
2632 {szContent_Location, HTTP_QUERY_CONTENT_LOCATION, REQ_RESP_HDR,},
2633 {szContent_MD5, HTTP_QUERY_CONTENT_MD5, REQ_RESP_HDR,},
2634 {szContent_Range, HTTP_QUERY_CONTENT_RANGE, REQ_RESP_HDR,},
2635 {szContent_Transfer_Encoding,HTTP_QUERY_CONTENT_TRANSFER_ENCODING, REQ_RESP_HDR,},
2636 {szContent_Type, HTTP_QUERY_CONTENT_TYPE, REQ_RESP_HDR,},
2637 {szCookie, HTTP_QUERY_COOKIE, REQUEST_HDR,},
2638 {szDate, HTTP_QUERY_DATE, REQ_RESP_HDR,},
2639 {szETag, HTTP_QUERY_ETAG, REQ_RESP_HDR,},
2640 {szExpect, HTTP_QUERY_EXPECT, REQUEST_HDR,},
2641 {szExpires, HTTP_QUERY_EXPIRES, REQ_RESP_HDR,},
2642 {szFrom, HTTP_QUERY_DERIVED_FROM, REQUEST_HDR,},
2643 {szHost, HTTP_QUERY_HOST, REQUEST_HDR,},
2644 {szIf_Match, HTTP_QUERY_IF_MATCH, REQUEST_HDR,},
2645 {szIf_Modified_Since, HTTP_QUERY_IF_MODIFIED_SINCE, REQUEST_HDR,},
2646 {szIf_None_Match, HTTP_QUERY_IF_NONE_MATCH, REQUEST_HDR,},
2647 {szIf_Range, HTTP_QUERY_IF_RANGE, REQUEST_HDR,},
2648 {szIf_Unmodified_Since, HTTP_QUERY_IF_UNMODIFIED_SINCE, REQUEST_HDR,},
2649 {szLast_Modified, HTTP_QUERY_LAST_MODIFIED, REQ_RESP_HDR,},
2650 {szLocation, HTTP_QUERY_LOCATION, REQ_RESP_HDR,},
2651 {szMax_Forwards, HTTP_QUERY_MAX_FORWARDS, REQUEST_HDR,},
2652 {szMime_Version, HTTP_QUERY_MIME_VERSION, REQ_RESP_HDR,},
2653 {szPragma, HTTP_QUERY_PRAGMA, REQ_RESP_HDR,},
2654 {szProxy_Authenticate, HTTP_QUERY_PROXY_AUTHENTICATE, RESPONSE_HDR,},
2655 {szProxy_Authorization, HTTP_QUERY_PROXY_AUTHORIZATION, REQUEST_HDR,},
2656 {szProxy_Connection, HTTP_QUERY_PROXY_CONNECTION, REQ_RESP_HDR,},
2657 {szPublic, HTTP_QUERY_PUBLIC, RESPONSE_HDR,},
2658 {szRange, HTTP_QUERY_RANGE, REQUEST_HDR,},
2659 {szReferer, HTTP_QUERY_REFERER, REQUEST_HDR,},
2660 {szRetry_After, HTTP_QUERY_RETRY_AFTER, RESPONSE_HDR,},
2661 {szServer, HTTP_QUERY_SERVER, RESPONSE_HDR,},
2662 {szSet_Cookie, HTTP_QUERY_SET_COOKIE, RESPONSE_HDR,},
2663 {szStatus, HTTP_QUERY_STATUS_CODE, RESPONSE_HDR,},
2664 {szTransfer_Encoding, HTTP_QUERY_TRANSFER_ENCODING, REQ_RESP_HDR,},
2665 {szUnless_Modified_Since, HTTP_QUERY_UNLESS_MODIFIED_SINCE, REQUEST_HDR,},
2666 {szUpgrade, HTTP_QUERY_UPGRADE, REQ_RESP_HDR,},
2667 {szURI, HTTP_QUERY_URI, REQ_RESP_HDR,},
2668 {szUser_Agent, HTTP_QUERY_USER_AGENT, REQUEST_HDR,},
2669 {szVary, HTTP_QUERY_VARY, RESPONSE_HDR,},
2670 {szVia, HTTP_QUERY_VIA, REQ_RESP_HDR,},
2671 {szWarning, HTTP_QUERY_WARNING, RESPONSE_HDR,},
2672 {szWWW_Authenticate, HTTP_QUERY_WWW_AUTHENTICATE, RESPONSE_HDR},
2675 /***********************************************************************
2676 * HTTP_GetStdHeaderIndex (internal)
2678 * Lookup field index in standard http header array
2680 * FIXME: Add support for HeaderType to avoid inadvertant assignments of
2681 * response headers to requests and looking for request headers
2685 static INT HTTP_GetStdHeaderIndex(LPCWSTR lpszField)
2688 INT hi = sizeof(SORTED_STANDARD_HEADERS) / sizeof(std_hdr_data) -1;
2693 for (i = 0; i < sizeof(SORTED_STANDARD_HEADERS) / sizeof(std_hdr_data) -1; i++)
2694 if (lstrcmpiW(SORTED_STANDARD_HEADERS[i].hdrStr, SORTED_STANDARD_HEADERS[i+1].hdrStr) > 0)
2695 ERR("%s should be after %s\n", debugstr_w(SORTED_STANDARD_HEADERS[i].hdrStr), debugstr_w(SORTED_STANDARD_HEADERS[i+1].hdrStr));
2699 mid = (int) (lo + hi) / 2;
2700 inx = lstrcmpiW(lpszField, SORTED_STANDARD_HEADERS[mid].hdrStr);
2702 return SORTED_STANDARD_HEADERS[mid].hdrIndex;
2708 WARN("Couldn't find %s in standard header table\n", debugstr_w(lpszField));
2712 /***********************************************************************
2713 * HTTP_ReplaceHeaderValue (internal)
2715 static BOOL HTTP_ReplaceHeaderValue( LPHTTPHEADERW lphttpHdr, LPCWSTR value )
2719 HeapFree( GetProcessHeap(), 0, lphttpHdr->lpszValue );
2720 lphttpHdr->lpszValue = NULL;
2723 len = strlenW(value);
2726 lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0,
2727 (len+1)*sizeof(WCHAR));
2728 strcpyW(lphttpHdr->lpszValue, value);
2733 /***********************************************************************
2734 * HTTP_ProcessHeader (internal)
2736 * Stuff header into header tables according to <dwModifier>
2740 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2742 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
2744 LPHTTPHEADERW lphttpHdr = NULL;
2745 BOOL bSuccess = FALSE;
2748 TRACE("--> %s: %s - 0x%08lx\n", debugstr_w(field), debugstr_w(value), dwModifier);
2750 /* Adjust modifier flags */
2751 if (dwModifier & COALESCEFLASG)
2752 dwModifier |= HTTP_ADDHDR_FLAG_ADD;
2754 /* Try to get index into standard header array */
2755 index = HTTP_GetStdHeaderIndex(field);
2756 /* Don't let applications add Connection header to request */
2757 if ((index == HTTP_QUERY_CONNECTION) && (dwModifier & HTTP_ADDHDR_FLAG_REQ))
2759 else if (index >= 0)
2761 lphttpHdr = &lpwhr->StdHeaders[index];
2763 else /* Find or create new custom header */
2765 index = HTTP_GetCustomHeaderIndex(lpwhr, field);
2768 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
2772 lphttpHdr = &lpwhr->pCustHeaders[index];
2778 hdr.lpszField = (LPWSTR)field;
2779 hdr.lpszValue = (LPWSTR)value;
2780 hdr.wFlags = hdr.wCount = 0;
2782 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2783 hdr.wFlags |= HDR_ISREQUEST;
2785 return HTTP_InsertCustomHeader(lpwhr, &hdr);
2789 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2790 lphttpHdr->wFlags |= HDR_ISREQUEST;
2792 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
2794 if (!lphttpHdr->lpszValue && (dwModifier & HTTP_ADDHDR_FLAG_ADD ||
2795 dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW))
2799 if (!lpwhr->StdHeaders[index].lpszField)
2801 lphttpHdr->lpszField = WININET_strdupW(field);
2803 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2804 lphttpHdr->wFlags |= HDR_ISREQUEST;
2807 slen = strlenW(value) + 1;
2808 lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0, slen*sizeof(WCHAR));
2809 if (lphttpHdr->lpszValue)
2811 strcpyW(lphttpHdr->lpszValue, value);
2816 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2819 else if (lphttpHdr->lpszValue)
2821 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE ||
2822 dwModifier & HTTP_ADDHDR_FLAG_ADD)
2823 bSuccess = HTTP_ReplaceHeaderValue( lphttpHdr, value );
2824 else if (dwModifier & COALESCEFLASG)
2829 INT origlen = strlenW(lphttpHdr->lpszValue);
2830 INT valuelen = strlenW(value);
2832 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
2835 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2837 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2840 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2843 len = origlen + valuelen + ((ch > 0) ? 1 : 0);
2845 lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
2848 lphttpHdr->lpszValue = lpsztmp;
2849 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
2852 lphttpHdr->lpszValue[origlen] = ch;
2856 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
2857 lphttpHdr->lpszValue[len] = '\0';
2862 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
2863 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2867 TRACE("<-- %d\n",bSuccess);
2872 /***********************************************************************
2873 * HTTP_CloseConnection (internal)
2875 * Close socket connection
2878 static VOID HTTP_CloseConnection(LPWININETHTTPREQW lpwhr)
2880 LPWININETHTTPSESSIONW lpwhs = NULL;
2881 LPWININETAPPINFOW hIC = NULL;
2883 TRACE("%p\n",lpwhr);
2885 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
2886 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2888 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2889 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2891 if (NETCON_connected(&lpwhr->netConnection))
2893 NETCON_close(&lpwhr->netConnection);
2896 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2897 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2901 /***********************************************************************
2902 * HTTP_CloseHTTPRequestHandle (internal)
2904 * Deallocate request handle
2907 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr)
2910 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
2914 if (NETCON_connected(&lpwhr->netConnection))
2915 HTTP_CloseConnection(lpwhr);
2917 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2918 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
2919 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2921 for (i = 0; i <= HTTP_QUERY_MAX; i++)
2923 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszField);
2924 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszValue);
2927 for (i = 0; i < lpwhr->nCustHeaders; i++)
2929 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
2930 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
2933 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
2934 HeapFree(GetProcessHeap(), 0, lpwhr);
2938 /***********************************************************************
2939 * HTTP_CloseHTTPSessionHandle (internal)
2941 * Deallocate session handle
2944 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr)
2946 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
2948 TRACE("%p\n", lpwhs);
2950 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
2951 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2952 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2953 HeapFree(GetProcessHeap(), 0, lpwhs);
2957 /***********************************************************************
2958 * HTTP_GetCustomHeaderIndex (internal)
2960 * Return index of custom header from header array
2963 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField)
2967 TRACE("%s\n", debugstr_w(lpszField));
2969 for (index = 0; index < lpwhr->nCustHeaders; index++)
2971 if (!strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
2976 if (index >= lpwhr->nCustHeaders)
2979 TRACE("Return: %ld\n", index);
2984 /***********************************************************************
2985 * HTTP_InsertCustomHeader (internal)
2987 * Insert header into array
2990 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
2993 LPHTTPHEADERW lph = NULL;
2996 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
2997 count = lpwhr->nCustHeaders + 1;
2999 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
3001 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
3005 lpwhr->pCustHeaders = lph;
3006 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
3007 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
3008 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
3009 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
3010 lpwhr->nCustHeaders++;
3015 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3022 /***********************************************************************
3023 * HTTP_DeleteCustomHeader (internal)
3025 * Delete header from array
3026 * If this function is called, the indexs may change.
3028 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
3030 if( lpwhr->nCustHeaders <= 0 )
3032 if( index >= lpwhr->nCustHeaders )
3034 lpwhr->nCustHeaders--;
3036 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
3037 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
3038 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
3043 /***********************************************************************
3044 * IsHostInProxyBypassList (@)
3049 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
3051 FIXME("STUB: flags=%ld host=%s length=%ld\n",flags,szHost,length);