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_szHttp[] = {' ','H','T','T','P','/','1','.','0',0 };
61 static const WCHAR g_szReferer[] = {'R','e','f','e','r','e','r',0};
62 static const WCHAR g_szAccept[] = {'A','c','c','e','p','t',0};
63 static const WCHAR g_szUserAgent[] = {'U','s','e','r','-','A','g','e','n','t',0};
64 static const WCHAR g_szHost[] = {'H','o','s','t',0};
67 #define HTTPHEADER g_szHttp
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 );
100 /***********************************************************************
101 * HTTP_Tokenize (internal)
103 * Tokenize a string, allocating memory for the tokens.
105 static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
107 LPWSTR * token_array;
112 /* empty string has no tokens */
116 for (i = 0; string[i]; i++)
117 if (!strncmpW(string+i, token_string, strlenW(token_string)))
121 /* we want to skip over separators, but not the null terminator */
122 for (j = 0; j < strlenW(token_string) - 1; j++)
128 /* add 1 for terminating NULL */
129 token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
130 token_array[tokens] = NULL;
133 for (i = 0; i < tokens; i++)
136 next_token = strstrW(string, token_string);
137 if (!next_token) next_token = string+strlenW(string);
138 len = next_token - string;
139 token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
140 memcpy(token_array[i], string, len*sizeof(WCHAR));
141 token_array[i][len] = '\0';
142 string = next_token+strlenW(token_string);
147 /***********************************************************************
148 * HTTP_FreeTokens (internal)
150 * Frees memory returned from HTTP_Tokenize.
152 static void HTTP_FreeTokens(LPWSTR * token_array)
155 for (i = 0; token_array[i]; i++)
156 HeapFree(GetProcessHeap(), 0, token_array[i]);
157 HeapFree(GetProcessHeap(), 0, token_array);
160 /* **********************************************************************
162 * Helper functions for the HttpSendRequest(Ex) functions
165 static void HTTP_FixVerb( LPWININETHTTPREQW lpwhr )
167 /* if the verb is NULL default to GET */
168 if (NULL == lpwhr->lpszVerb)
170 static const WCHAR szGET[] = { 'G','E','T', 0 };
171 lpwhr->lpszVerb = WININET_strdupW(szGET);
175 static void HTTP_FixURL( LPWININETHTTPREQW lpwhr)
177 static const WCHAR szSlash[] = { '/',0 };
178 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
180 /* If we don't have a path we set it to root */
181 if (NULL == lpwhr->lpszPath)
182 lpwhr->lpszPath = WININET_strdupW(szSlash);
183 else /* remove \r and \n*/
185 int nLen = strlenW(lpwhr->lpszPath);
186 while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
189 lpwhr->lpszPath[nLen]='\0';
191 /* Replace '\' with '/' */
194 if (lpwhr->lpszPath[nLen] == '\\') lpwhr->lpszPath[nLen]='/';
198 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
199 lpwhr->lpszPath, strlenW(szHttp), szHttp, strlenW(szHttp) )
200 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
202 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
203 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
205 strcpyW(fixurl + 1, lpwhr->lpszPath);
206 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
207 lpwhr->lpszPath = fixurl;
211 static LPWSTR HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr )
213 LPWSTR requestString;
219 static const WCHAR szSpace[] = { ' ',0 };
220 static const WCHAR szcrlf[] = {'\r','\n', 0};
221 static const WCHAR szColon[] = { ':',' ',0 };
222 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
224 /* allocate space for an array of all the string pointers to be added */
225 len = (HTTP_QUERY_MAX + lpwhr->nCustHeaders)*4 + 9;
226 req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
228 /* add the verb, path and HTTP/1.0 */
230 req[n++] = lpwhr->lpszVerb;
232 req[n++] = lpwhr->lpszPath;
233 req[n++] = HTTPHEADER;
235 /* Append standard request headers */
236 for (i = 0; i <= HTTP_QUERY_MAX; i++)
238 if (lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST)
241 req[n++] = lpwhr->StdHeaders[i].lpszField;
243 req[n++] = lpwhr->StdHeaders[i].lpszValue;
245 TRACE("Adding header %s (%s)\n",
246 debugstr_w(lpwhr->StdHeaders[i].lpszField),
247 debugstr_w(lpwhr->StdHeaders[i].lpszValue));
251 /* Append custom request heades */
252 for (i = 0; i < lpwhr->nCustHeaders; i++)
254 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
257 req[n++] = lpwhr->pCustHeaders[i].lpszField;
259 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
261 TRACE("Adding custom header %s (%s)\n",
262 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
263 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
268 ERR("oops. buffer overrun\n");
271 requestString = HTTP_build_req( req, 4 );
272 HeapFree( GetProcessHeap(), 0, req );
275 * Set (header) termination string for request
276 * Make sure there's exactly two new lines at the end of the request
278 p = &requestString[strlenW(requestString)-1];
279 while ( (*p == '\n') || (*p == '\r') )
281 strcpyW( p+1, sztwocrlf );
283 return requestString;
286 static void HTTP_ProcessHeaders( LPWININETHTTPREQW lpwhr )
289 static const WCHAR szSetCookie[] = {'S','e','t','-','C','o','o','k','i','e',0 };
291 CustHeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSetCookie);
292 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && (CustHeaderIndex >= 0))
294 LPHTTPHEADERW setCookieHeader;
295 int nPosStart = 0, nPosEnd = 0, len;
296 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
298 setCookieHeader = &lpwhr->pCustHeaders[CustHeaderIndex];
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 = NULL;
368 LPWININETAPPINFOW hIC = NULL;
370 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
371 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
373 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
377 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
378 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
380 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
384 if (hIC && (hIC->lpszProxyUsername || hIC->lpszProxyPassword ))
385 HTTP_InsertProxyAuthorization(lpwhr, hIC->lpszProxyUsername,
386 hIC->lpszProxyPassword);
389 /***********************************************************************
390 * HTTP_HttpAddRequestHeadersW (internal)
392 static BOOL WINAPI HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
393 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
398 BOOL bSuccess = FALSE;
401 TRACE("copying header: %s\n", debugstr_w(lpszHeader));
403 if( dwHeaderLength == ~0U )
404 len = strlenW(lpszHeader);
406 len = dwHeaderLength;
407 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
408 lstrcpynW( buffer, lpszHeader, len + 1);
414 LPWSTR * pFieldAndValue;
418 while (*lpszEnd != '\0')
420 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
425 if (*lpszStart == '\0')
428 if (*lpszEnd == '\r')
431 lpszEnd += 2; /* Jump over \r\n */
433 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
434 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
437 bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
438 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
439 HTTP_FreeTokens(pFieldAndValue);
445 HeapFree(GetProcessHeap(), 0, buffer);
450 /***********************************************************************
451 * HttpAddRequestHeadersW (WININET.@)
453 * Adds one or more HTTP header to the request handler
460 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
461 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
463 BOOL bSuccess = FALSE;
464 LPWININETHTTPREQW lpwhr;
466 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_w(lpszHeader), dwHeaderLength,
472 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
473 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
475 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
478 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
481 WININET_Release( &lpwhr->hdr );
486 /***********************************************************************
487 * HttpAddRequestHeadersA (WININET.@)
489 * Adds one or more HTTP header to the request handler
496 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
497 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
503 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_a(lpszHeader), dwHeaderLength,
506 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
507 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
508 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
509 if( dwHeaderLength != ~0U )
510 dwHeaderLength = len;
512 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
514 HeapFree( GetProcessHeap(), 0, hdr );
519 /***********************************************************************
520 * HttpEndRequestA (WININET.@)
522 * Ends an HTTP request that was started by HttpSendRequestEx
529 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
530 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD dwContext)
532 LPINTERNET_BUFFERSA ptr;
533 LPINTERNET_BUFFERSW lpBuffersOutW,ptrW;
536 TRACE("(%p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersOut, dwFlags,
541 lpBuffersOutW = (LPINTERNET_BUFFERSW)HeapAlloc(GetProcessHeap(),
542 HEAP_ZERO_MEMORY, sizeof(INTERNET_BUFFERSW));
544 lpBuffersOutW = NULL;
546 ptrW = lpBuffersOutW;
549 if (ptr->lpvBuffer && ptr->dwBufferLength)
550 ptrW->lpvBuffer = HeapAlloc(GetProcessHeap(),0,ptr->dwBufferLength);
551 ptrW->dwBufferLength = ptr->dwBufferLength;
552 ptrW->dwBufferTotal= ptr->dwBufferTotal;
555 ptrW->Next = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
556 sizeof(INTERNET_BUFFERSW));
562 rc = HttpEndRequestW(hRequest, lpBuffersOutW, dwFlags, dwContext);
566 ptrW = lpBuffersOutW;
569 LPINTERNET_BUFFERSW ptrW2;
571 FIXME("Do we need to translate info out of these buffer?\n");
573 HeapFree(GetProcessHeap(),0,(LPVOID)ptrW->lpvBuffer);
575 HeapFree(GetProcessHeap(),0,ptrW);
583 /***********************************************************************
584 * HttpEndRequestW (WININET.@)
586 * Ends an HTTP request that was started by HttpSendRequestEx
593 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
594 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD dwContext)
597 LPWININETHTTPREQW lpwhr;
600 static const char nullbuff[] = "\0";
603 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
605 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
607 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
611 lpwhr->hdr.dwFlags |= dwFlags;
612 lpwhr->hdr.dwContext = dwContext;
614 /* End the request by sending a NULL byte */
615 rc = NETCON_send(&lpwhr->netConnection, nullbuff, 1, 0, &cnt);
617 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
618 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
620 responseLen = HTTP_GetResponseHeaders(lpwhr);
624 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
625 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
627 /* process headers here. Is this right? */
628 HTTP_ProcessHeaders(lpwhr);
630 /* We appear to do nothing with the buffer.. is that correct? */
632 TRACE("%i <--\n",rc);
636 /***********************************************************************
637 * HttpOpenRequestW (WININET.@)
639 * Open a HTTP request handle
642 * HINTERNET a HTTP request handle on success
646 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
647 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
648 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
649 DWORD dwFlags, DWORD dwContext)
651 LPWININETHTTPSESSIONW lpwhs;
652 HINTERNET handle = NULL;
654 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
655 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
656 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
658 if(lpszAcceptTypes!=NULL)
661 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
662 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
665 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
666 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
668 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
673 * My tests seem to show that the windows version does not
674 * become asynchronous until after this point. And anyhow
675 * if this call was asynchronous then how would you get the
676 * necessary HINTERNET pointer returned by this function.
679 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
680 lpszVersion, lpszReferrer, lpszAcceptTypes,
684 WININET_Release( &lpwhs->hdr );
685 TRACE("returning %p\n", handle);
690 /***********************************************************************
691 * HttpOpenRequestA (WININET.@)
693 * Open a HTTP request handle
696 * HINTERNET a HTTP request handle on success
700 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
701 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
702 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
703 DWORD dwFlags, DWORD dwContext)
705 LPWSTR szVerb = NULL, szObjectName = NULL;
706 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
708 INT acceptTypesCount;
709 HINTERNET rc = FALSE;
710 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
711 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
712 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
717 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
718 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
721 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
726 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
727 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
730 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
735 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
736 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
739 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
744 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
745 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
748 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
751 acceptTypesCount = 0;
754 /* find out how many there are */
755 while (lpszAcceptTypes[acceptTypesCount])
757 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
758 acceptTypesCount = 0;
759 while (lpszAcceptTypes[acceptTypesCount])
761 len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
763 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
764 if (!szAcceptTypes[acceptTypesCount] )
766 MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
767 -1, szAcceptTypes[acceptTypesCount], len );
770 szAcceptTypes[acceptTypesCount] = NULL;
772 else szAcceptTypes = 0;
774 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
775 szVersion, szReferrer,
776 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
781 acceptTypesCount = 0;
782 while (szAcceptTypes[acceptTypesCount])
784 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
787 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
789 HeapFree(GetProcessHeap(), 0, szReferrer);
790 HeapFree(GetProcessHeap(), 0, szVersion);
791 HeapFree(GetProcessHeap(), 0, szObjectName);
792 HeapFree(GetProcessHeap(), 0, szVerb);
797 /***********************************************************************
800 static UINT HTTP_Base64( LPCWSTR bin, LPWSTR base64 )
803 static LPCSTR HTTP_Base64Enc =
804 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
808 /* first 6 bits, all from bin[0] */
809 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
810 x = (bin[0] & 3) << 4;
812 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
815 base64[n++] = HTTP_Base64Enc[x];
820 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
821 x = ( bin[1] & 0x0f ) << 2;
823 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
826 base64[n++] = HTTP_Base64Enc[x];
830 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
832 /* last 6 bits, all from bin [2] */
833 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
840 /***********************************************************************
841 * HTTP_EncodeBasicAuth
843 * Encode the basic authentication string for HTTP 1.1
845 static LPWSTR HTTP_EncodeBasicAuth( LPCWSTR username, LPCWSTR password)
849 static const WCHAR szBasic[] = {'B','a','s','i','c',' ',0};
850 static const WCHAR szColon[] = {':',0};
852 len = lstrlenW( username ) + 1 + lstrlenW ( password ) + 1;
853 in = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
857 len = lstrlenW(szBasic) +
858 (lstrlenW( username ) + 1 + lstrlenW ( password ))*2 + 1 + 1;
859 out = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
862 lstrcpyW( in, username );
863 lstrcatW( in, szColon );
864 lstrcatW( in, password );
865 lstrcpyW( out, szBasic );
866 HTTP_Base64( in, &out[strlenW(out)] );
868 HeapFree( GetProcessHeap(), 0, in );
873 /***********************************************************************
874 * HTTP_InsertProxyAuthorization
876 * Insert the basic authorization field in the request header
878 static BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
879 LPCWSTR username, LPCWSTR password )
883 static const WCHAR szProxyAuthorization[] = {
884 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
886 hdr.lpszValue = HTTP_EncodeBasicAuth( username, password );
887 hdr.lpszField = (WCHAR *)szProxyAuthorization;
888 hdr.wFlags = HDR_ISREQUEST;
893 TRACE("Inserting %s = %s\n",
894 debugstr_w( hdr.lpszField ), debugstr_w( hdr.lpszValue ) );
896 /* remove the old proxy authorization header */
897 index = HTTP_GetCustomHeaderIndex( lpwhr, hdr.lpszField );
899 HTTP_DeleteCustomHeader( lpwhr, index );
901 HTTP_InsertCustomHeader(lpwhr, &hdr);
902 HeapFree( GetProcessHeap(), 0, hdr.lpszValue );
907 /***********************************************************************
910 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
911 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
913 WCHAR buf[MAXHOSTNAME];
914 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
916 static const WCHAR szNul[] = { 0 };
917 URL_COMPONENTSW UrlComponents;
918 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }, szSlash[] = { '/',0 } ;
919 static const WCHAR szFormat1[] = { 'h','t','t','p',':','/','/','%','s',0 };
920 static const WCHAR szFormat2[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
923 memset( &UrlComponents, 0, sizeof UrlComponents );
924 UrlComponents.dwStructSize = sizeof UrlComponents;
925 UrlComponents.lpszHostName = buf;
926 UrlComponents.dwHostNameLength = MAXHOSTNAME;
928 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
929 buf,strlenW(szHttp),szHttp,strlenW(szHttp)) )
930 sprintfW(proxy, szFormat1, hIC->lpszProxy);
933 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
935 if( UrlComponents.dwHostNameLength == 0 )
938 if( !lpwhr->lpszPath )
939 lpwhr->lpszPath = (LPWSTR)szNul;
940 TRACE("server='%s' path='%s'\n",
941 debugstr_w(lpwhs->lpszServerName), debugstr_w(lpwhr->lpszPath));
942 /* for constant 15 see above */
943 len = strlenW(lpwhs->lpszServerName) + strlenW(lpwhr->lpszPath) + 15;
944 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
946 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
947 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
949 sprintfW(url, szFormat2, lpwhs->lpszServerName, lpwhs->nServerPort);
951 if( lpwhr->lpszPath[0] != '/' )
952 strcatW( url, szSlash );
953 strcatW(url, lpwhr->lpszPath);
954 if(lpwhr->lpszPath != szNul)
955 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
956 lpwhr->lpszPath = url;
957 /* FIXME: Do I have to free lpwhs->lpszServerName here ? */
958 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
959 lpwhs->nServerPort = UrlComponents.nPort;
964 /***********************************************************************
965 * HTTP_HttpOpenRequestW (internal)
967 * Open a HTTP request handle
970 * HINTERNET a HTTP request handle on success
974 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
975 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
976 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
977 DWORD dwFlags, DWORD dwContext)
979 LPWININETAPPINFOW hIC = NULL;
980 LPWININETHTTPREQW lpwhr;
982 LPWSTR lpszUrl = NULL;
984 HINTERNET handle = NULL;
985 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
990 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
991 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
993 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
996 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
999 lpwhr->hdr.htype = WH_HHTTPREQ;
1000 lpwhr->hdr.lpwhparent = WININET_AddRef( &lpwhs->hdr );
1001 lpwhr->hdr.dwFlags = dwFlags;
1002 lpwhr->hdr.dwContext = dwContext;
1003 lpwhr->hdr.dwRefCount = 1;
1004 lpwhr->hdr.destroy = HTTP_CloseHTTPRequestHandle;
1005 lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
1007 handle = WININET_AllocHandle( &lpwhr->hdr );
1010 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1014 NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE);
1016 if (NULL != lpszObjectName && strlenW(lpszObjectName)) {
1020 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
1021 if (rc != E_POINTER)
1022 len = strlenW(lpszObjectName)+1;
1023 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1024 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
1025 URL_ESCAPE_SPACES_ONLY);
1028 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(lpszObjectName),rc);
1029 strcpyW(lpwhr->lpszPath,lpszObjectName);
1033 if (NULL != lpszReferrer && strlenW(lpszReferrer))
1034 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDHDR_FLAG_COALESCE);
1036 if(lpszAcceptTypes!=NULL)
1039 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
1040 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i], HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_REQ|HTTP_ADDHDR_FLAG_ADD_IF_NEW);
1043 if (NULL == lpszVerb)
1045 static const WCHAR szGet[] = {'G','E','T',0};
1046 lpwhr->lpszVerb = WININET_strdupW(szGet);
1048 else if (strlenW(lpszVerb))
1049 lpwhr->lpszVerb = WININET_strdupW(lpszVerb);
1051 if (NULL != lpszReferrer && strlenW(lpszReferrer))
1053 WCHAR buf[MAXHOSTNAME];
1054 URL_COMPONENTSW UrlComponents;
1056 memset( &UrlComponents, 0, sizeof UrlComponents );
1057 UrlComponents.dwStructSize = sizeof UrlComponents;
1058 UrlComponents.lpszHostName = buf;
1059 UrlComponents.dwHostNameLength = MAXHOSTNAME;
1061 InternetCrackUrlW(lpszReferrer, 0, 0, &UrlComponents);
1062 if (strlenW(UrlComponents.lpszHostName))
1063 HTTP_ProcessHeader(lpwhr, g_szHost, UrlComponents.lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1066 HTTP_ProcessHeader(lpwhr, g_szHost, lpwhs->lpszServerName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1068 if (lpwhs->nServerPort == INTERNET_INVALID_PORT_NUMBER)
1069 lpwhs->nServerPort = (dwFlags & INTERNET_FLAG_SECURE ?
1070 INTERNET_DEFAULT_HTTPS_PORT :
1071 INTERNET_DEFAULT_HTTP_PORT);
1073 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1074 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
1078 WCHAR *agent_header;
1079 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
1081 len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
1082 agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1083 sprintfW(agent_header, user_agent, hIC->lpszAgent );
1085 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
1086 HTTP_ADDREQ_FLAG_ADD);
1087 HeapFree(GetProcessHeap(), 0, agent_header);
1090 len = strlenW(lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue) + strlenW(szUrlForm);
1091 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1092 sprintfW( lpszUrl, szUrlForm, lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue );
1094 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
1095 InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
1098 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
1099 static const WCHAR szcrlf[] = {'\r','\n',0};
1101 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
1103 cnt += sprintfW(lpszCookies, szCookie);
1104 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
1105 strcatW(lpszCookies, szcrlf);
1107 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
1108 HTTP_ADDREQ_FLAG_ADD);
1109 HeapFree(GetProcessHeap(), 0, lpszCookies);
1111 HeapFree(GetProcessHeap(), 0, lpszUrl);
1114 SendAsyncCallback(&lpwhs->hdr, dwContext,
1115 INTERNET_STATUS_HANDLE_CREATED, &handle,
1119 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
1123 * According to my tests. The name is not resolved until a request is Opened
1125 SendAsyncCallback(&lpwhr->hdr, dwContext,
1126 INTERNET_STATUS_RESOLVING_NAME,
1127 lpwhs->lpszServerName,
1128 strlenW(lpwhs->lpszServerName)+1);
1130 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1131 &lpwhs->socketAddress))
1133 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1134 InternetCloseHandle( handle );
1139 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1140 INTERNET_STATUS_NAME_RESOLVED,
1141 &(lpwhs->socketAddress),
1142 sizeof(struct sockaddr_in));
1146 WININET_Release( &lpwhr->hdr );
1148 TRACE("<-- %p (%p)\n", handle, lpwhr);
1152 /***********************************************************************
1153 * HTTP_HttpQueryInfoW (internal)
1155 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
1156 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1158 LPHTTPHEADERW lphttpHdr = NULL;
1159 BOOL bSuccess = FALSE;
1161 /* Find requested header structure */
1162 if ((dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK) == HTTP_QUERY_CUSTOM)
1164 INT index = HTTP_GetCustomHeaderIndex(lpwhr, (LPWSTR)lpBuffer);
1169 lphttpHdr = &lpwhr->pCustHeaders[index];
1173 INT index = dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK;
1175 if (index == HTTP_QUERY_RAW_HEADERS_CRLF)
1177 DWORD len = strlenW(lpwhr->lpszRawHeaders);
1178 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1180 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1181 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1184 memcpy(lpBuffer, lpwhr->lpszRawHeaders, (len+1)*sizeof(WCHAR));
1185 *lpdwBufferLength = len * sizeof(WCHAR);
1187 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1191 else if (index == HTTP_QUERY_RAW_HEADERS)
1193 static const WCHAR szCrLf[] = {'\r','\n',0};
1194 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
1196 LPWSTR pszString = (WCHAR*)lpBuffer;
1198 for (i = 0; ppszRawHeaderLines[i]; i++)
1199 size += strlenW(ppszRawHeaderLines[i]) + 1;
1201 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
1203 HTTP_FreeTokens(ppszRawHeaderLines);
1204 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
1205 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1209 for (i = 0; ppszRawHeaderLines[i]; i++)
1211 DWORD len = strlenW(ppszRawHeaderLines[i]);
1212 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
1217 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
1219 *lpdwBufferLength = size * sizeof(WCHAR);
1220 HTTP_FreeTokens(ppszRawHeaderLines);
1224 else if (index >= 0 && index <= HTTP_QUERY_MAX && lpwhr->StdHeaders[index].lpszValue)
1226 lphttpHdr = &lpwhr->StdHeaders[index];
1230 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1235 /* Ensure header satisifies requested attributes */
1236 if ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
1237 (~lphttpHdr->wFlags & HDR_ISREQUEST))
1239 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1243 /* coalesce value to reuqested type */
1244 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
1246 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
1249 TRACE(" returning number : %d\n", *(int *)lpBuffer);
1251 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
1257 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
1259 tmpTM = *gmtime(&tmpTime);
1260 STHook = (SYSTEMTIME *) lpBuffer;
1264 STHook->wDay = tmpTM.tm_mday;
1265 STHook->wHour = tmpTM.tm_hour;
1266 STHook->wMilliseconds = 0;
1267 STHook->wMinute = tmpTM.tm_min;
1268 STHook->wDayOfWeek = tmpTM.tm_wday;
1269 STHook->wMonth = tmpTM.tm_mon + 1;
1270 STHook->wSecond = tmpTM.tm_sec;
1271 STHook->wYear = tmpTM.tm_year;
1275 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
1276 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
1277 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
1279 else if (dwInfoLevel & HTTP_QUERY_FLAG_COALESCE)
1281 if (*lpdwIndex >= lphttpHdr->wCount)
1283 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1287 /* Copy strncpyW(lpBuffer, lphttpHdr[*lpdwIndex], len); */
1293 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
1295 if (len > *lpdwBufferLength)
1297 *lpdwBufferLength = len;
1298 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1302 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
1303 *lpdwBufferLength = len - sizeof(WCHAR);
1306 TRACE(" returning string : '%s'\n", debugstr_w(lpBuffer));
1311 /***********************************************************************
1312 * HttpQueryInfoW (WININET.@)
1314 * Queries for information about an HTTP request
1321 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1322 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1324 BOOL bSuccess = FALSE;
1325 LPWININETHTTPREQW lpwhr;
1327 if (TRACE_ON(wininet)) {
1328 #define FE(x) { x, #x }
1329 static const wininet_flag_info query_flags[] = {
1330 FE(HTTP_QUERY_MIME_VERSION),
1331 FE(HTTP_QUERY_CONTENT_TYPE),
1332 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
1333 FE(HTTP_QUERY_CONTENT_ID),
1334 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
1335 FE(HTTP_QUERY_CONTENT_LENGTH),
1336 FE(HTTP_QUERY_CONTENT_LANGUAGE),
1337 FE(HTTP_QUERY_ALLOW),
1338 FE(HTTP_QUERY_PUBLIC),
1339 FE(HTTP_QUERY_DATE),
1340 FE(HTTP_QUERY_EXPIRES),
1341 FE(HTTP_QUERY_LAST_MODIFIED),
1342 FE(HTTP_QUERY_MESSAGE_ID),
1344 FE(HTTP_QUERY_DERIVED_FROM),
1345 FE(HTTP_QUERY_COST),
1346 FE(HTTP_QUERY_LINK),
1347 FE(HTTP_QUERY_PRAGMA),
1348 FE(HTTP_QUERY_VERSION),
1349 FE(HTTP_QUERY_STATUS_CODE),
1350 FE(HTTP_QUERY_STATUS_TEXT),
1351 FE(HTTP_QUERY_RAW_HEADERS),
1352 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
1353 FE(HTTP_QUERY_CONNECTION),
1354 FE(HTTP_QUERY_ACCEPT),
1355 FE(HTTP_QUERY_ACCEPT_CHARSET),
1356 FE(HTTP_QUERY_ACCEPT_ENCODING),
1357 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
1358 FE(HTTP_QUERY_AUTHORIZATION),
1359 FE(HTTP_QUERY_CONTENT_ENCODING),
1360 FE(HTTP_QUERY_FORWARDED),
1361 FE(HTTP_QUERY_FROM),
1362 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
1363 FE(HTTP_QUERY_LOCATION),
1364 FE(HTTP_QUERY_ORIG_URI),
1365 FE(HTTP_QUERY_REFERER),
1366 FE(HTTP_QUERY_RETRY_AFTER),
1367 FE(HTTP_QUERY_SERVER),
1368 FE(HTTP_QUERY_TITLE),
1369 FE(HTTP_QUERY_USER_AGENT),
1370 FE(HTTP_QUERY_WWW_AUTHENTICATE),
1371 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
1372 FE(HTTP_QUERY_ACCEPT_RANGES),
1373 FE(HTTP_QUERY_SET_COOKIE),
1374 FE(HTTP_QUERY_COOKIE),
1375 FE(HTTP_QUERY_REQUEST_METHOD),
1376 FE(HTTP_QUERY_REFRESH),
1377 FE(HTTP_QUERY_CONTENT_DISPOSITION),
1379 FE(HTTP_QUERY_CACHE_CONTROL),
1380 FE(HTTP_QUERY_CONTENT_BASE),
1381 FE(HTTP_QUERY_CONTENT_LOCATION),
1382 FE(HTTP_QUERY_CONTENT_MD5),
1383 FE(HTTP_QUERY_CONTENT_RANGE),
1384 FE(HTTP_QUERY_ETAG),
1385 FE(HTTP_QUERY_HOST),
1386 FE(HTTP_QUERY_IF_MATCH),
1387 FE(HTTP_QUERY_IF_NONE_MATCH),
1388 FE(HTTP_QUERY_IF_RANGE),
1389 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
1390 FE(HTTP_QUERY_MAX_FORWARDS),
1391 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
1392 FE(HTTP_QUERY_RANGE),
1393 FE(HTTP_QUERY_TRANSFER_ENCODING),
1394 FE(HTTP_QUERY_UPGRADE),
1395 FE(HTTP_QUERY_VARY),
1397 FE(HTTP_QUERY_WARNING),
1398 FE(HTTP_QUERY_CUSTOM)
1400 static const wininet_flag_info modifier_flags[] = {
1401 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
1402 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
1403 FE(HTTP_QUERY_FLAG_NUMBER),
1404 FE(HTTP_QUERY_FLAG_COALESCE)
1407 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
1408 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
1411 TRACE("(%p, 0x%08lx)--> %ld\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
1412 TRACE(" Attribute:");
1413 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
1414 if (query_flags[i].val == info) {
1415 TRACE(" %s", query_flags[i].name);
1419 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
1420 TRACE(" Unknown (%08lx)", info);
1423 TRACE(" Modifier:");
1424 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
1425 if (modifier_flags[i].val & info_mod) {
1426 TRACE(" %s", modifier_flags[i].name);
1427 info_mod &= ~ modifier_flags[i].val;
1432 TRACE(" Unknown (%08lx)", info_mod);
1437 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1438 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1440 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1444 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
1445 lpBuffer, lpdwBufferLength, lpdwIndex);
1449 WININET_Release( &lpwhr->hdr );
1451 TRACE("%d <--\n", bSuccess);
1455 /***********************************************************************
1456 * HttpQueryInfoA (WININET.@)
1458 * Queries for information about an HTTP request
1465 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1466 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1472 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
1473 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
1475 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
1476 lpdwBufferLength, lpdwIndex );
1479 len = (*lpdwBufferLength)*sizeof(WCHAR);
1480 bufferW = HeapAlloc( GetProcessHeap(), 0, len );
1481 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
1485 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
1486 lpBuffer, *lpdwBufferLength, NULL, NULL );
1487 *lpdwBufferLength = len - 1;
1489 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
1492 /* since the strings being returned from HttpQueryInfoW should be
1493 * only ASCII characters, it is reasonable to assume that all of
1494 * the Unicode characters can be reduced to a single byte */
1495 *lpdwBufferLength = len / sizeof(WCHAR);
1497 HeapFree(GetProcessHeap(), 0, bufferW );
1502 /***********************************************************************
1503 * HttpSendRequestExA (WININET.@)
1505 * Sends the specified request to the HTTP server and allows chunked
1508 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
1509 LPINTERNET_BUFFERSA lpBuffersIn,
1510 LPINTERNET_BUFFERSA lpBuffersOut,
1511 DWORD dwFlags, DWORD dwContext)
1513 LPINTERNET_BUFFERSA ptr;
1514 LPINTERNET_BUFFERSW lpBuffersInW,ptrW;
1517 TRACE("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersIn,
1518 lpBuffersOut, dwFlags, dwContext);
1522 lpBuffersInW = (LPINTERNET_BUFFERSW)HeapAlloc(GetProcessHeap(),
1523 HEAP_ZERO_MEMORY, sizeof(INTERNET_BUFFERSW));
1525 lpBuffersInW = NULL;
1527 ptrW = lpBuffersInW;
1531 ptrW->dwStructSize = sizeof(LPINTERNET_BUFFERSW);
1532 if (ptr->lpcszHeader)
1534 headerlen = MultiByteToWideChar(CP_ACP,0,ptr->lpcszHeader,
1535 ptr->dwHeadersLength,0,0);
1537 ptrW->lpcszHeader = HeapAlloc(GetProcessHeap(),0,headerlen*
1539 ptrW->dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
1540 ptr->lpcszHeader, ptr->dwHeadersLength,
1541 (LPWSTR)ptrW->lpcszHeader, headerlen);
1543 ptrW->dwHeadersTotal = ptr->dwHeadersTotal;
1544 ptrW->lpvBuffer = ptr->lpvBuffer;
1545 ptrW->dwBufferLength = ptr->dwBufferLength;
1546 ptrW->dwBufferTotal= ptr->dwBufferTotal;
1549 ptrW->Next = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
1550 sizeof(INTERNET_BUFFERSW));
1556 rc = HttpSendRequestExW(hRequest, lpBuffersInW, NULL, dwFlags, dwContext);
1559 ptrW = lpBuffersInW;
1562 LPINTERNET_BUFFERSW ptrW2;
1563 HeapFree(GetProcessHeap(),0,(LPVOID)ptrW->lpcszHeader);
1565 HeapFree(GetProcessHeap(),0,ptrW);
1572 /***********************************************************************
1573 * HttpSendRequestExW (WININET.@)
1575 * Sends the specified request to the HTTP server and allows chunked
1578 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
1579 LPINTERNET_BUFFERSW lpBuffersIn,
1580 LPINTERNET_BUFFERSW lpBuffersOut,
1581 DWORD dwFlags, DWORD dwContext)
1583 LPINTERNET_BUFFERSW buf_ptr;
1584 DWORD bufferlen = 0;
1586 LPWININETHTTPREQW lpwhr;
1587 LPWSTR requestString = NULL;
1592 TRACE("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersIn,
1593 lpBuffersOut, dwFlags, dwContext);
1595 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
1597 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1599 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1603 HTTP_FixVerb(lpwhr);
1606 buf_ptr = lpBuffersIn;
1609 if (buf_ptr->lpcszHeader)
1611 HTTP_HttpAddRequestHeadersW(lpwhr, buf_ptr->lpcszHeader,
1612 buf_ptr->dwHeadersLength, HTTP_ADDREQ_FLAG_ADD |
1613 HTTP_ADDHDR_FLAG_REPLACE);
1615 bufferlen += buf_ptr->dwBufferTotal;
1616 buf_ptr = buf_ptr->Next;
1619 lpwhr->hdr.dwFlags |= dwFlags;
1620 lpwhr->hdr.dwContext = dwContext;
1624 static const WCHAR szContentLength[] = {
1625 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ',
1626 '%','l','i','\r','\n',0};
1627 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \n\r */ +
1629 sprintfW(contentLengthStr, szContentLength, bufferlen);
1630 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L,
1631 HTTP_ADDREQ_FLAG_ADD);
1636 /* Do Send Request logic */
1638 TRACE("Going to url %s %s\n",
1639 debugstr_w(lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue),
1640 debugstr_w(lpwhr->lpszPath));
1642 HTTP_AddProxyInfo(lpwhr);
1644 requestString = HTTP_BuildHeaderRequestString(lpwhr);
1646 TRACE("Request header -> %s\n", debugstr_w(requestString) );
1648 if (!(rc = HTTP_OpenConnection(lpwhr)))
1651 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1652 INTERNET_STATUS_CONNECTED_TO_SERVER, NULL, 0);
1654 /* send the request as ASCII */
1655 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1, NULL, 0, NULL,
1658 /* we do not want the null because optional data will be sent with following
1659 * InternetWriteFile calls
1662 ascii_req = HeapAlloc( GetProcessHeap(), 0, len);
1663 WideCharToMultiByte( CP_ACP, 0, requestString, -1, ascii_req, len, NULL,
1665 TRACE("full request -> %s\n", debugstr_an(ascii_req,len) );
1667 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1668 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
1670 rc = NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
1671 HeapFree( GetProcessHeap(), 0, ascii_req );
1673 /* This may not be sent here. It may come laster */
1674 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1675 INTERNET_STATUS_REQUEST_SENT, &len,sizeof(DWORD));
1678 HeapFree(GetProcessHeap(), 0, requestString);
1679 WININET_Release(&lpwhr->hdr);
1684 /***********************************************************************
1685 * HttpSendRequestW (WININET.@)
1687 * Sends the specified request to the HTTP server
1694 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
1695 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1697 LPWININETHTTPREQW lpwhr;
1698 LPWININETHTTPSESSIONW lpwhs = NULL;
1699 LPWININETAPPINFOW hIC = NULL;
1702 TRACE("%p, %p (%s), %li, %p, %li)\n", hHttpRequest,
1703 lpszHeaders, debugstr_w(lpszHeaders), dwHeaderLength, lpOptional, dwOptionalLength);
1705 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1706 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1708 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1713 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1714 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1716 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1721 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1722 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1724 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1729 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1731 WORKREQUEST workRequest;
1732 struct WORKREQ_HTTPSENDREQUESTW *req;
1734 workRequest.asyncall = HTTPSENDREQUESTW;
1735 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
1736 req = &workRequest.u.HttpSendRequestW;
1738 req->lpszHeader = WININET_strdupW(lpszHeaders);
1740 req->lpszHeader = 0;
1741 req->dwHeaderLength = dwHeaderLength;
1742 req->lpOptional = lpOptional;
1743 req->dwOptionalLength = dwOptionalLength;
1745 INTERNET_AsyncCall(&workRequest);
1747 * This is from windows.
1749 SetLastError(ERROR_IO_PENDING);
1754 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
1755 dwHeaderLength, lpOptional, dwOptionalLength);
1759 WININET_Release( &lpwhr->hdr );
1763 /***********************************************************************
1764 * HttpSendRequestA (WININET.@)
1766 * Sends the specified request to the HTTP server
1773 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
1774 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1777 LPWSTR szHeaders=NULL;
1778 DWORD nLen=dwHeaderLength;
1779 if(lpszHeaders!=NULL)
1781 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
1782 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
1783 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
1785 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
1786 HeapFree(GetProcessHeap(),0,szHeaders);
1790 /***********************************************************************
1791 * HTTP_HandleRedirect (internal)
1793 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl, LPCWSTR lpszHeaders,
1794 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength)
1796 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1797 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1802 /* if it's an absolute path, keep the same session info */
1803 strcpyW(path,lpszUrl);
1805 else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1807 TRACE("Redirect through proxy\n");
1808 strcpyW(path,lpszUrl);
1812 URL_COMPONENTSW urlComponents;
1813 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
1814 WCHAR password[1024], extra[1024];
1815 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
1816 urlComponents.lpszScheme = protocol;
1817 urlComponents.dwSchemeLength = 32;
1818 urlComponents.lpszHostName = hostName;
1819 urlComponents.dwHostNameLength = MAXHOSTNAME;
1820 urlComponents.lpszUserName = userName;
1821 urlComponents.dwUserNameLength = 1024;
1822 urlComponents.lpszPassword = password;
1823 urlComponents.dwPasswordLength = 1024;
1824 urlComponents.lpszUrlPath = path;
1825 urlComponents.dwUrlPathLength = 2048;
1826 urlComponents.lpszExtraInfo = extra;
1827 urlComponents.dwExtraInfoLength = 1024;
1828 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
1831 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1832 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1836 * This upsets redirects to binary files on sourceforge.net
1837 * and gives an html page instead of the target file
1838 * Examination of the HTTP request sent by native wininet.dll
1839 * reveals that it doesn't send a referrer in that case.
1840 * Maybe there's a flag that enables this, or maybe a referrer
1841 * shouldn't be added in case of a redirect.
1844 /* consider the current host as the referrer */
1845 if (NULL != lpwhs->lpszServerName && strlenW(lpwhs->lpszServerName))
1846 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
1847 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
1848 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
1851 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1852 lpwhs->lpszServerName = WININET_strdupW(hostName);
1853 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
1854 lpwhs->lpszUserName = WININET_strdupW(userName);
1855 lpwhs->nServerPort = urlComponents.nPort;
1857 HTTP_ProcessHeader(lpwhr, g_szHost, hostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1859 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1860 INTERNET_STATUS_RESOLVING_NAME,
1861 lpwhs->lpszServerName,
1862 strlenW(lpwhs->lpszServerName)+1);
1864 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1865 &lpwhs->socketAddress))
1867 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1871 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1872 INTERNET_STATUS_NAME_RESOLVED,
1873 &(lpwhs->socketAddress),
1874 sizeof(struct sockaddr_in));
1878 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1879 lpwhr->lpszPath=NULL;
1885 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
1886 if (rc != E_POINTER)
1887 needed = strlenW(path)+1;
1888 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
1889 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
1890 URL_ESCAPE_SPACES_ONLY);
1893 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(path),rc);
1894 strcpyW(lpwhr->lpszPath,path);
1898 return HTTP_HttpSendRequestW(lpwhr, lpszHeaders, dwHeaderLength, lpOptional, dwOptionalLength);
1901 /***********************************************************************
1902 * HTTP_build_req (internal)
1904 * concatenate all the strings in the request together
1906 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
1911 for( t = list; *t ; t++ )
1912 len += strlenW( *t );
1915 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1918 for( t = list; *t ; t++ )
1924 /***********************************************************************
1925 * HTTP_HttpSendRequestW (internal)
1927 * Sends the specified request to the HTTP server
1934 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
1935 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1938 BOOL bSuccess = FALSE;
1939 LPWSTR requestString = NULL;
1941 BOOL loop_next = FALSE;
1942 INTERNET_ASYNC_RESULT iar;
1944 TRACE("--> %p\n", lpwhr);
1946 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
1948 /* Clear any error information */
1949 INTERNET_SetLastError(0);
1951 HTTP_FixVerb(lpwhr);
1953 /* if we are using optional stuff, we must add the fixed header of that option length */
1954 if (lpOptional && dwOptionalLength)
1956 static const WCHAR szContentLength[] = {
1957 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0};
1958 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \n\r */ + 20 /* int */ ];
1959 sprintfW(contentLengthStr, szContentLength, dwOptionalLength);
1960 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD);
1968 TRACE("Going to url %s %s\n", debugstr_w(lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue), debugstr_w(lpwhr->lpszPath));
1973 /* add the headers the caller supplied */
1974 if( lpszHeaders && dwHeaderLength )
1976 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
1977 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
1980 /* if there's a proxy username and password, add it to the headers */
1981 HTTP_AddProxyInfo(lpwhr);
1983 requestString = HTTP_BuildHeaderRequestString(lpwhr);
1985 TRACE("Request header -> %s\n", debugstr_w(requestString) );
1987 /* Send the request and store the results */
1988 if (!HTTP_OpenConnection(lpwhr))
1991 /* send the request as ASCII, tack on the optional data */
1993 dwOptionalLength = 0;
1994 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
1995 NULL, 0, NULL, NULL );
1996 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
1997 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
1998 ascii_req, len, NULL, NULL );
2000 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
2001 len = (len + dwOptionalLength - 1);
2003 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
2005 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2006 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
2008 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
2009 HeapFree( GetProcessHeap(), 0, ascii_req );
2011 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2012 INTERNET_STATUS_REQUEST_SENT,
2013 &len,sizeof(DWORD));
2015 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2016 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2021 responseLen = HTTP_GetResponseHeaders(lpwhr);
2025 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2026 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
2029 /* process headers here. Is this right? */
2030 HTTP_ProcessHeaders(lpwhr);
2036 HeapFree(GetProcessHeap(), 0, requestString);
2038 /* TODO: send notification for P3P header */
2040 if(!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
2042 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
2043 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
2044 (dwCode==302 || dwCode==301))
2046 WCHAR szNewLocation[2048];
2047 DWORD dwBufferSize=2048;
2049 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
2051 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2052 INTERNET_STATUS_REDIRECT, szNewLocation,
2054 return HTTP_HandleRedirect(lpwhr, szNewLocation, lpszHeaders,
2055 dwHeaderLength, lpOptional, dwOptionalLength);
2061 iar.dwResult = (DWORD)bSuccess;
2062 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
2064 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2065 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2066 sizeof(INTERNET_ASYNC_RESULT));
2073 /***********************************************************************
2074 * HTTP_Connect (internal)
2076 * Create http session handle
2079 * HINTERNET a session handle on success
2083 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
2084 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
2085 LPCWSTR lpszPassword, DWORD dwFlags, DWORD dwContext,
2086 DWORD dwInternalFlags)
2088 BOOL bSuccess = FALSE;
2089 LPWININETHTTPSESSIONW lpwhs = NULL;
2090 HINTERNET handle = NULL;
2094 assert( hIC->hdr.htype == WH_HINIT );
2096 hIC->hdr.dwContext = dwContext;
2098 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
2101 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2106 * According to my tests. The name is not resolved until a request is sent
2109 lpwhs->hdr.htype = WH_HHTTPSESSION;
2110 lpwhs->hdr.lpwhparent = WININET_AddRef( &hIC->hdr );
2111 lpwhs->hdr.dwFlags = dwFlags;
2112 lpwhs->hdr.dwContext = dwContext;
2113 lpwhs->hdr.dwInternalFlags = dwInternalFlags;
2114 lpwhs->hdr.dwRefCount = 1;
2115 lpwhs->hdr.destroy = HTTP_CloseHTTPSessionHandle;
2116 lpwhs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
2118 handle = WININET_AllocHandle( &lpwhs->hdr );
2121 ERR("Failed to alloc handle\n");
2122 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2126 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
2127 if(strchrW(hIC->lpszProxy, ' '))
2128 FIXME("Several proxies not implemented.\n");
2129 if(hIC->lpszProxyBypass)
2130 FIXME("Proxy bypass is ignored.\n");
2132 if (NULL != lpszServerName)
2133 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
2134 if (NULL != lpszUserName)
2135 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
2136 lpwhs->nServerPort = nServerPort;
2138 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2139 if (!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
2141 SendAsyncCallback(&hIC->hdr, dwContext,
2142 INTERNET_STATUS_HANDLE_CREATED, &handle,
2150 WININET_Release( &lpwhs->hdr );
2153 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
2157 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
2162 /***********************************************************************
2163 * HTTP_OpenConnection (internal)
2165 * Connect to a web server
2172 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
2174 BOOL bSuccess = FALSE;
2175 LPWININETHTTPSESSIONW lpwhs;
2176 LPWININETAPPINFOW hIC = NULL;
2181 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2183 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2187 lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
2189 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2190 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2191 INTERNET_STATUS_CONNECTING_TO_SERVER,
2192 &(lpwhs->socketAddress),
2193 sizeof(struct sockaddr_in));
2195 if (!NETCON_create(&lpwhr->netConnection, lpwhs->socketAddress.sin_family,
2198 WARN("Socket creation failed\n");
2202 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
2203 sizeof(lpwhs->socketAddress)))
2205 WARN("Unable to connect to host (%s)\n", strerror(errno));
2209 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2210 INTERNET_STATUS_CONNECTED_TO_SERVER,
2211 &(lpwhs->socketAddress),
2212 sizeof(struct sockaddr_in));
2217 TRACE("%d <--\n", bSuccess);
2222 /***********************************************************************
2223 * HTTP_clear_response_headers (internal)
2225 * clear out any old response headers
2227 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
2231 for( i=0; i<=HTTP_QUERY_MAX; i++ )
2233 if( !lpwhr->StdHeaders[i].lpszField )
2235 if( !lpwhr->StdHeaders[i].lpszValue )
2237 if ( lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST )
2239 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[i], NULL );
2240 HeapFree( GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszField );
2241 lpwhr->StdHeaders[i].lpszField = NULL;
2243 for( i=0; i<lpwhr->nCustHeaders; i++)
2245 if( !lpwhr->pCustHeaders[i].lpszField )
2247 if( !lpwhr->pCustHeaders[i].lpszValue )
2249 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
2251 HTTP_DeleteCustomHeader( lpwhr, i );
2256 /***********************************************************************
2257 * HTTP_GetResponseHeaders (internal)
2259 * Read server response
2266 static BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr)
2269 WCHAR buffer[MAX_REPLY_LEN];
2270 DWORD buflen = MAX_REPLY_LEN;
2271 BOOL bSuccess = FALSE;
2273 static const WCHAR szCrLf[] = {'\r','\n',0};
2274 char bufferA[MAX_REPLY_LEN];
2275 LPWSTR status_code, status_text;
2276 DWORD cchMaxRawHeaders = 1024;
2277 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2278 DWORD cchRawHeaders = 0;
2282 /* clear old response headers (eg. from a redirect response) */
2283 HTTP_clear_response_headers( lpwhr );
2285 if (!NETCON_connected(&lpwhr->netConnection))
2289 * HACK peek at the buffer
2291 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
2294 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
2296 buflen = MAX_REPLY_LEN;
2297 memset(buffer, 0, MAX_REPLY_LEN);
2298 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2300 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2302 /* regenerate raw headers */
2303 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2305 cchMaxRawHeaders *= 2;
2306 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2308 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2309 cchRawHeaders += (buflen-1);
2310 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2311 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2312 lpszRawHeaders[cchRawHeaders] = '\0';
2314 /* split the version from the status code */
2315 status_code = strchrW( buffer, ' ' );
2320 /* split the status code from the status text */
2321 status_text = strchrW( status_code, ' ' );
2326 TRACE("version [%s] status code [%s] status text [%s]\n",
2327 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
2328 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_VERSION], buffer );
2329 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_STATUS_CODE], status_code );
2330 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_STATUS_TEXT], status_text );
2332 /* Parse each response line */
2335 buflen = MAX_REPLY_LEN;
2336 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2338 LPWSTR * pFieldAndValue;
2340 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
2341 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2343 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2345 cchMaxRawHeaders *= 2;
2346 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2348 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2349 cchRawHeaders += (buflen-1);
2350 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2351 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2352 lpszRawHeaders[cchRawHeaders] = '\0';
2354 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
2355 if (!pFieldAndValue)
2358 HTTP_ProcessHeader(lpwhr, pFieldAndValue[0], pFieldAndValue[1],
2359 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE);
2361 HTTP_FreeTokens(pFieldAndValue);
2371 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2372 lpwhr->lpszRawHeaders = lpszRawHeaders;
2373 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
2386 static void strip_spaces(LPWSTR start)
2391 while (*str == ' ' && *str != '\0')
2395 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
2397 end = start + strlenW(start) - 1;
2398 while (end >= start && *end == ' ')
2406 /***********************************************************************
2407 * HTTP_InterpretHttpHeader (internal)
2409 * Parse server response
2413 * Pointer to array of field, value, NULL on success.
2416 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
2418 LPWSTR * pTokenPair;
2422 pTokenPair = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pTokenPair)*3);
2424 pszColon = strchrW(buffer, ':');
2425 /* must have two tokens */
2428 HTTP_FreeTokens(pTokenPair);
2430 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
2434 pTokenPair[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon - buffer + 1) * sizeof(WCHAR));
2437 HTTP_FreeTokens(pTokenPair);
2440 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
2441 pTokenPair[0][pszColon - buffer] = '\0';
2445 len = strlenW(pszColon);
2446 pTokenPair[1] = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
2449 HTTP_FreeTokens(pTokenPair);
2452 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
2454 strip_spaces(pTokenPair[0]);
2455 strip_spaces(pTokenPair[1]);
2457 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
2461 typedef enum {REQUEST_HDR = 1, RESPONSE_HDR = 2, REQ_RESP_HDR = 3} std_hdr_type;
2463 typedef struct std_hdr_data
2465 const WCHAR* hdrStr;
2467 std_hdr_type hdrType;
2470 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
2471 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
2472 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
2473 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
2474 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
2475 static const WCHAR szAge[] = { 'A','g','e',0 };
2476 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
2477 static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
2478 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
2479 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
2480 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
2481 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
2482 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
2483 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
2484 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
2485 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
2486 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
2487 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 };
2488 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
2489 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
2490 static const WCHAR szDate[] = { 'D','a','t','e',0 };
2491 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
2492 static const WCHAR szETag[] = { 'E','T','a','g',0 };
2493 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
2494 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
2495 static const WCHAR szHost[] = { 'H','o','s','t',0 };
2496 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
2497 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
2498 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
2499 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
2500 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
2501 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
2502 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
2503 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
2504 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
2505 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
2506 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
2507 static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
2508 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
2509 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
2510 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
2511 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
2512 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
2513 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
2514 static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 };
2515 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
2516 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 };
2517 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
2518 static const WCHAR szURI[] = { 'U','R','I',0 };
2519 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
2520 static const WCHAR szVary[] = { 'V','a','r','y',0 };
2521 static const WCHAR szVia[] = { 'V','i','a',0 };
2522 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
2523 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
2525 /* Note: Must be kept sorted! */
2526 const std_hdr_data SORTED_STANDARD_HEADERS[] = {
2527 {szAccept_Charset, HTTP_QUERY_ACCEPT_CHARSET, REQUEST_HDR,},
2528 {szAccept_Encoding, HTTP_QUERY_ACCEPT_ENCODING, REQUEST_HDR,},
2529 {szAccept, HTTP_QUERY_ACCEPT, REQUEST_HDR,},
2530 {szAccept_Language, HTTP_QUERY_ACCEPT_LANGUAGE, REQUEST_HDR,},
2531 {szAccept_Ranges, HTTP_QUERY_ACCEPT_RANGES, RESPONSE_HDR,},
2532 {szAge, HTTP_QUERY_AGE, RESPONSE_HDR,},
2533 {szAllow, HTTP_QUERY_ALLOW, REQ_RESP_HDR,},
2534 {szAuthorization, HTTP_QUERY_AUTHORIZATION, REQUEST_HDR,},
2535 {szCache_Control, HTTP_QUERY_CACHE_CONTROL, REQ_RESP_HDR,},
2536 {szConnection, HTTP_QUERY_CONNECTION, REQ_RESP_HDR,},
2537 {szContent_Base, HTTP_QUERY_CONTENT_BASE, REQ_RESP_HDR,},
2538 {szContent_Encoding, HTTP_QUERY_CONTENT_ENCODING, REQ_RESP_HDR,},
2539 {szContent_Language, HTTP_QUERY_CONTENT_LANGUAGE, REQ_RESP_HDR,},
2540 {szContent_Length, HTTP_QUERY_CONTENT_LENGTH, REQ_RESP_HDR,},
2541 {szContent_Location, HTTP_QUERY_CONTENT_LOCATION, REQ_RESP_HDR,},
2542 {szContent_MD5, HTTP_QUERY_CONTENT_MD5, REQ_RESP_HDR,},
2543 {szContent_Range, HTTP_QUERY_CONTENT_RANGE, REQ_RESP_HDR,},
2544 {szContent_Transfer_Encoding,HTTP_QUERY_CONTENT_TRANSFER_ENCODING, REQ_RESP_HDR,},
2545 {szContent_Type, HTTP_QUERY_CONTENT_TYPE, REQ_RESP_HDR,},
2546 {szCookie, HTTP_QUERY_COOKIE, REQUEST_HDR,},
2547 {szDate, HTTP_QUERY_DATE, REQ_RESP_HDR,},
2548 {szETag, HTTP_QUERY_ETAG, REQ_RESP_HDR,},
2549 {szExpect, HTTP_QUERY_EXPECT, REQUEST_HDR,},
2550 {szExpires, HTTP_QUERY_EXPIRES, REQ_RESP_HDR,},
2551 {szFrom, HTTP_QUERY_DERIVED_FROM, REQUEST_HDR,},
2552 {szHost, HTTP_QUERY_HOST, REQUEST_HDR,},
2553 {szIf_Match, HTTP_QUERY_IF_MATCH, REQUEST_HDR,},
2554 {szIf_Modified_Since, HTTP_QUERY_IF_MODIFIED_SINCE, REQUEST_HDR,},
2555 {szIf_None_Match, HTTP_QUERY_IF_NONE_MATCH, REQUEST_HDR,},
2556 {szIf_Range, HTTP_QUERY_IF_RANGE, REQUEST_HDR,},
2557 {szIf_Unmodified_Since, HTTP_QUERY_IF_UNMODIFIED_SINCE, REQUEST_HDR,},
2558 {szLast_Modified, HTTP_QUERY_LAST_MODIFIED, REQ_RESP_HDR,},
2559 {szLocation, HTTP_QUERY_LOCATION, REQ_RESP_HDR,},
2560 {szMax_Forwards, HTTP_QUERY_MAX_FORWARDS, REQUEST_HDR,},
2561 {szMime_Version, HTTP_QUERY_MIME_VERSION, REQ_RESP_HDR,},
2562 {szPragma, HTTP_QUERY_PRAGMA, REQ_RESP_HDR,},
2563 {szProxy_Authenticate, HTTP_QUERY_PROXY_AUTHENTICATE, RESPONSE_HDR,},
2564 {szProxy_Authorization, HTTP_QUERY_PROXY_AUTHORIZATION, REQUEST_HDR,},
2565 {szPublic, HTTP_QUERY_PUBLIC, RESPONSE_HDR,},
2566 {szRange, HTTP_QUERY_RANGE, REQUEST_HDR,},
2567 {szReferer, HTTP_QUERY_REFERER, REQUEST_HDR,},
2568 {szRetry_After, HTTP_QUERY_RETRY_AFTER, RESPONSE_HDR,},
2569 {szServer, HTTP_QUERY_SERVER, RESPONSE_HDR,},
2570 {szSet_Cookie, HTTP_QUERY_SET_COOKIE, RESPONSE_HDR,},
2571 {szStatus, HTTP_QUERY_STATUS_CODE, RESPONSE_HDR,},
2572 {szTransfer_Encoding, HTTP_QUERY_TRANSFER_ENCODING, REQ_RESP_HDR,},
2573 {szUnless_Modified_Since, HTTP_QUERY_UNLESS_MODIFIED_SINCE, REQUEST_HDR,},
2574 {szUpgrade, HTTP_QUERY_UPGRADE, REQ_RESP_HDR,},
2575 {szURI, HTTP_QUERY_URI, REQ_RESP_HDR,},
2576 {szUser_Agent, HTTP_QUERY_USER_AGENT, REQUEST_HDR,},
2577 {szVary, HTTP_QUERY_VARY, RESPONSE_HDR,},
2578 {szVia, HTTP_QUERY_VIA, REQ_RESP_HDR,},
2579 {szWarning, HTTP_QUERY_WARNING, RESPONSE_HDR,},
2580 {szWWW_Authenticate, HTTP_QUERY_WWW_AUTHENTICATE, RESPONSE_HDR},
2583 /***********************************************************************
2584 * HTTP_GetStdHeaderIndex (internal)
2586 * Lookup field index in standard http header array
2588 * FIXME: Add support for HeaderType to avoid inadvertant assignments of
2589 * response headers to requests and looking for request headers
2593 static INT HTTP_GetStdHeaderIndex(LPCWSTR lpszField)
2596 INT hi = sizeof(SORTED_STANDARD_HEADERS) / sizeof(std_hdr_data) -1;
2600 mid = (int) (lo + hi) / 2;
2601 inx = lstrcmpiW(lpszField, SORTED_STANDARD_HEADERS[mid].hdrStr);
2603 return SORTED_STANDARD_HEADERS[mid].hdrIndex;
2609 FIXME("Couldn't find %s in standard header table\n", debugstr_w(lpszField));
2613 /***********************************************************************
2614 * HTTP_ReplaceHeaderValue (internal)
2616 static BOOL HTTP_ReplaceHeaderValue( LPHTTPHEADERW lphttpHdr, LPCWSTR value )
2620 HeapFree( GetProcessHeap(), 0, lphttpHdr->lpszValue );
2621 lphttpHdr->lpszValue = NULL;
2624 len = strlenW(value);
2627 lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0,
2628 (len+1)*sizeof(WCHAR));
2629 strcpyW(lphttpHdr->lpszValue, value);
2634 /***********************************************************************
2635 * HTTP_ProcessHeader (internal)
2637 * Stuff header into header tables according to <dwModifier>
2641 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2643 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
2645 LPHTTPHEADERW lphttpHdr = NULL;
2646 BOOL bSuccess = FALSE;
2649 TRACE("--> %s: %s - 0x%08lx\n", debugstr_w(field), debugstr_w(value), dwModifier);
2651 /* Adjust modifier flags */
2652 if (dwModifier & COALESCEFLASG)
2653 dwModifier |= HTTP_ADDHDR_FLAG_ADD;
2655 /* Try to get index into standard header array */
2656 index = HTTP_GetStdHeaderIndex(field);
2657 /* Don't let applications add Connection header to request */
2658 if ((index == HTTP_QUERY_CONNECTION) && (dwModifier & HTTP_ADDHDR_FLAG_REQ))
2660 else if (index >= 0)
2662 lphttpHdr = &lpwhr->StdHeaders[index];
2664 else /* Find or create new custom header */
2666 index = HTTP_GetCustomHeaderIndex(lpwhr, field);
2669 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
2673 lphttpHdr = &lpwhr->pCustHeaders[index];
2679 hdr.lpszField = (LPWSTR)field;
2680 hdr.lpszValue = (LPWSTR)value;
2681 hdr.wFlags = hdr.wCount = 0;
2683 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2684 hdr.wFlags |= HDR_ISREQUEST;
2686 return HTTP_InsertCustomHeader(lpwhr, &hdr);
2690 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2691 lphttpHdr->wFlags |= HDR_ISREQUEST;
2693 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
2695 if (!lphttpHdr->lpszValue && (dwModifier & (HTTP_ADDHDR_FLAG_ADD|HTTP_ADDHDR_FLAG_ADD_IF_NEW)))
2699 if (!lpwhr->StdHeaders[index].lpszField)
2701 lphttpHdr->lpszField = WININET_strdupW(field);
2703 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2704 lphttpHdr->wFlags |= HDR_ISREQUEST;
2707 slen = strlenW(value) + 1;
2708 lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0, slen*sizeof(WCHAR));
2709 if (lphttpHdr->lpszValue)
2711 strcpyW(lphttpHdr->lpszValue, value);
2716 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2719 else if (lphttpHdr->lpszValue)
2721 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
2722 bSuccess = HTTP_ReplaceHeaderValue( lphttpHdr, value );
2723 else if (dwModifier & COALESCEFLASG)
2728 INT origlen = strlenW(lphttpHdr->lpszValue);
2729 INT valuelen = strlenW(value);
2731 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
2734 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2736 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2739 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2742 len = origlen + valuelen + ((ch > 0) ? 1 : 0);
2744 lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
2747 lphttpHdr->lpszValue = lpsztmp;
2748 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
2751 lphttpHdr->lpszValue[origlen] = ch;
2755 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
2756 lphttpHdr->lpszValue[len] = '\0';
2761 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
2762 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2766 TRACE("<-- %d\n",bSuccess);
2771 /***********************************************************************
2772 * HTTP_CloseConnection (internal)
2774 * Close socket connection
2777 static VOID HTTP_CloseConnection(LPWININETHTTPREQW lpwhr)
2779 LPWININETHTTPSESSIONW lpwhs = NULL;
2780 LPWININETAPPINFOW hIC = NULL;
2782 TRACE("%p\n",lpwhr);
2784 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
2785 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2787 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2788 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2790 if (NETCON_connected(&lpwhr->netConnection))
2792 NETCON_close(&lpwhr->netConnection);
2795 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2796 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2800 /***********************************************************************
2801 * HTTP_CloseHTTPRequestHandle (internal)
2803 * Deallocate request handle
2806 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr)
2809 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
2813 if (NETCON_connected(&lpwhr->netConnection))
2814 HTTP_CloseConnection(lpwhr);
2816 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2817 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
2818 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2820 for (i = 0; i <= HTTP_QUERY_MAX; i++)
2822 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszField);
2823 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszValue);
2826 for (i = 0; i < lpwhr->nCustHeaders; i++)
2828 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
2829 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
2832 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
2833 HeapFree(GetProcessHeap(), 0, lpwhr);
2837 /***********************************************************************
2838 * HTTP_CloseHTTPSessionHandle (internal)
2840 * Deallocate session handle
2843 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr)
2845 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
2847 TRACE("%p\n", lpwhs);
2849 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2850 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2851 HeapFree(GetProcessHeap(), 0, lpwhs);
2855 /***********************************************************************
2856 * HTTP_GetCustomHeaderIndex (internal)
2858 * Return index of custom header from header array
2861 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField)
2865 TRACE("%s\n", debugstr_w(lpszField));
2867 for (index = 0; index < lpwhr->nCustHeaders; index++)
2869 if (!strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
2874 if (index >= lpwhr->nCustHeaders)
2877 TRACE("Return: %ld\n", index);
2882 /***********************************************************************
2883 * HTTP_InsertCustomHeader (internal)
2885 * Insert header into array
2888 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
2891 LPHTTPHEADERW lph = NULL;
2894 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
2895 count = lpwhr->nCustHeaders + 1;
2897 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
2899 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
2903 lpwhr->pCustHeaders = lph;
2904 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
2905 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
2906 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
2907 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
2908 lpwhr->nCustHeaders++;
2913 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2920 /***********************************************************************
2921 * HTTP_DeleteCustomHeader (internal)
2923 * Delete header from array
2924 * If this function is called, the indexs may change.
2926 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
2928 if( lpwhr->nCustHeaders <= 0 )
2930 if( index >= lpwhr->nCustHeaders )
2932 lpwhr->nCustHeaders--;
2934 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
2935 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
2936 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
2941 /***********************************************************************
2942 * IsHostInProxyBypassList (@)
2947 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
2949 FIXME("STUB: flags=%ld host=%s length=%ld\n",flags,szHost,length);