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 )
288 LPHTTPHEADERW setCookieHeader = &lpwhr->StdHeaders[HTTP_QUERY_SET_COOKIE];
290 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && setCookieHeader->lpszValue)
292 int nPosStart = 0, nPosEnd = 0, len;
293 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
295 while (setCookieHeader->lpszValue[nPosEnd] != '\0')
297 LPWSTR buf_cookie, cookie_name, cookie_data;
299 LPWSTR domain = NULL;
301 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
302 setCookieHeader->lpszValue[nPosEnd] != '\0')
306 if (setCookieHeader->lpszValue[nPosEnd] == ';')
308 /* fixme: not case sensitive, strcasestr is gnu only */
309 int nDomainPosEnd = 0;
310 int nDomainPosStart = 0, nDomainLength = 0;
311 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
312 LPWSTR lpszDomain = strstrW(&setCookieHeader->lpszValue[nPosEnd], szDomain);
314 { /* they have specified their own domain, lets use it */
315 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
316 lpszDomain[nDomainPosEnd] != '\0')
320 nDomainPosStart = strlenW(szDomain);
321 nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
322 domain = HeapAlloc(GetProcessHeap(), 0, (nDomainLength + 1)*sizeof(WCHAR));
323 lstrcpynW(domain, &lpszDomain[nDomainPosStart], nDomainLength + 1);
326 if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
327 buf_cookie = HeapAlloc(GetProcessHeap(), 0, ((nPosEnd - nPosStart) + 1)*sizeof(WCHAR));
328 lstrcpynW(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart) + 1);
329 TRACE("%s\n", debugstr_w(buf_cookie));
330 while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
334 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
336 HeapFree(GetProcessHeap(), 0, buf_cookie);
340 cookie_name = HeapAlloc(GetProcessHeap(), 0, (nEqualPos + 1)*sizeof(WCHAR));
341 lstrcpynW(cookie_name, buf_cookie, nEqualPos + 1);
342 cookie_data = &buf_cookie[nEqualPos + 1];
345 len = strlenW((domain ? domain : lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue)) +
346 strlenW(lpwhr->lpszPath) + 9;
347 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
348 sprintfW(buf_url, szFmt, (domain ? domain : lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue)); /* FIXME PATH!!! */
349 InternetSetCookieW(buf_url, cookie_name, cookie_data);
351 HeapFree(GetProcessHeap(), 0, buf_url);
352 HeapFree(GetProcessHeap(), 0, buf_cookie);
353 HeapFree(GetProcessHeap(), 0, cookie_name);
354 HeapFree(GetProcessHeap(), 0, domain);
360 static void HTTP_AddProxyInfo( LPWININETHTTPREQW lpwhr )
362 LPWININETHTTPSESSIONW lpwhs = NULL;
363 LPWININETAPPINFOW hIC = NULL;
365 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
366 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
368 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
369 assert(hIC->hdr.htype == WH_HINIT);
371 if (hIC && (hIC->lpszProxyUsername || hIC->lpszProxyPassword ))
372 HTTP_InsertProxyAuthorization(lpwhr, hIC->lpszProxyUsername,
373 hIC->lpszProxyPassword);
376 /***********************************************************************
377 * HTTP_HttpAddRequestHeadersW (internal)
379 static BOOL WINAPI HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
380 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
385 BOOL bSuccess = FALSE;
388 TRACE("copying header: %s\n", debugstr_w(lpszHeader));
390 if( dwHeaderLength == ~0U )
391 len = strlenW(lpszHeader);
393 len = dwHeaderLength;
394 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
395 lstrcpynW( buffer, lpszHeader, len + 1);
401 LPWSTR * pFieldAndValue;
405 while (*lpszEnd != '\0')
407 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
412 if (*lpszStart == '\0')
415 if (*lpszEnd == '\r')
418 lpszEnd += 2; /* Jump over \r\n */
420 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
421 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
424 bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
425 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
426 HTTP_FreeTokens(pFieldAndValue);
432 HeapFree(GetProcessHeap(), 0, buffer);
437 /***********************************************************************
438 * HttpAddRequestHeadersW (WININET.@)
440 * Adds one or more HTTP header to the request handler
447 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
448 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
450 BOOL bSuccess = FALSE;
451 LPWININETHTTPREQW lpwhr;
453 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_w(lpszHeader), dwHeaderLength,
459 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
460 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
462 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
465 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
468 WININET_Release( &lpwhr->hdr );
473 /***********************************************************************
474 * HttpAddRequestHeadersA (WININET.@)
476 * Adds one or more HTTP header to the request handler
483 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
484 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
490 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_a(lpszHeader), dwHeaderLength,
493 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
494 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
495 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
496 if( dwHeaderLength != ~0U )
497 dwHeaderLength = len;
499 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
501 HeapFree( GetProcessHeap(), 0, hdr );
506 /***********************************************************************
507 * HttpEndRequestA (WININET.@)
509 * Ends an HTTP request that was started by HttpSendRequestEx
516 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
517 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD dwContext)
519 LPINTERNET_BUFFERSA ptr;
520 LPINTERNET_BUFFERSW lpBuffersOutW,ptrW;
523 TRACE("(%p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersOut, dwFlags,
528 lpBuffersOutW = (LPINTERNET_BUFFERSW)HeapAlloc(GetProcessHeap(),
529 HEAP_ZERO_MEMORY, sizeof(INTERNET_BUFFERSW));
531 lpBuffersOutW = NULL;
533 ptrW = lpBuffersOutW;
536 if (ptr->lpvBuffer && ptr->dwBufferLength)
537 ptrW->lpvBuffer = HeapAlloc(GetProcessHeap(),0,ptr->dwBufferLength);
538 ptrW->dwBufferLength = ptr->dwBufferLength;
539 ptrW->dwBufferTotal= ptr->dwBufferTotal;
542 ptrW->Next = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
543 sizeof(INTERNET_BUFFERSW));
549 rc = HttpEndRequestW(hRequest, lpBuffersOutW, dwFlags, dwContext);
553 ptrW = lpBuffersOutW;
556 LPINTERNET_BUFFERSW ptrW2;
558 FIXME("Do we need to translate info out of these buffer?\n");
560 HeapFree(GetProcessHeap(),0,(LPVOID)ptrW->lpvBuffer);
562 HeapFree(GetProcessHeap(),0,ptrW);
570 /***********************************************************************
571 * HttpEndRequestW (WININET.@)
573 * Ends an HTTP request that was started by HttpSendRequestEx
580 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
581 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD dwContext)
584 LPWININETHTTPREQW lpwhr;
588 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
590 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
592 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
596 lpwhr->hdr.dwFlags |= dwFlags;
597 lpwhr->hdr.dwContext = dwContext;
599 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
600 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
602 responseLen = HTTP_GetResponseHeaders(lpwhr);
606 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
607 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
609 /* process headers here. Is this right? */
610 HTTP_ProcessHeaders(lpwhr);
612 /* We appear to do nothing with the buffer.. is that correct? */
614 TRACE("%i <--\n",rc);
618 /***********************************************************************
619 * HttpOpenRequestW (WININET.@)
621 * Open a HTTP request handle
624 * HINTERNET a HTTP request handle on success
628 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
629 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
630 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
631 DWORD dwFlags, DWORD dwContext)
633 LPWININETHTTPSESSIONW lpwhs;
634 HINTERNET handle = NULL;
636 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
637 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
638 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
640 if(lpszAcceptTypes!=NULL)
643 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
644 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
647 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
648 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
650 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
655 * My tests seem to show that the windows version does not
656 * become asynchronous until after this point. And anyhow
657 * if this call was asynchronous then how would you get the
658 * necessary HINTERNET pointer returned by this function.
661 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
662 lpszVersion, lpszReferrer, lpszAcceptTypes,
666 WININET_Release( &lpwhs->hdr );
667 TRACE("returning %p\n", handle);
672 /***********************************************************************
673 * HttpOpenRequestA (WININET.@)
675 * Open a HTTP request handle
678 * HINTERNET a HTTP request handle on success
682 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
683 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
684 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
685 DWORD dwFlags, DWORD dwContext)
687 LPWSTR szVerb = NULL, szObjectName = NULL;
688 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
690 INT acceptTypesCount;
691 HINTERNET rc = FALSE;
692 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
693 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
694 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
699 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
700 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
703 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
708 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
709 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
712 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
717 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
718 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
721 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
726 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
727 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
730 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
733 acceptTypesCount = 0;
736 /* find out how many there are */
737 while (lpszAcceptTypes[acceptTypesCount])
739 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
740 acceptTypesCount = 0;
741 while (lpszAcceptTypes[acceptTypesCount])
743 len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
745 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
746 if (!szAcceptTypes[acceptTypesCount] )
748 MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
749 -1, szAcceptTypes[acceptTypesCount], len );
752 szAcceptTypes[acceptTypesCount] = NULL;
754 else szAcceptTypes = 0;
756 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
757 szVersion, szReferrer,
758 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
763 acceptTypesCount = 0;
764 while (szAcceptTypes[acceptTypesCount])
766 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
769 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
771 HeapFree(GetProcessHeap(), 0, szReferrer);
772 HeapFree(GetProcessHeap(), 0, szVersion);
773 HeapFree(GetProcessHeap(), 0, szObjectName);
774 HeapFree(GetProcessHeap(), 0, szVerb);
779 /***********************************************************************
782 static UINT HTTP_Base64( LPCWSTR bin, LPWSTR base64 )
785 static LPCSTR HTTP_Base64Enc =
786 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
790 /* first 6 bits, all from bin[0] */
791 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
792 x = (bin[0] & 3) << 4;
794 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
797 base64[n++] = HTTP_Base64Enc[x];
802 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
803 x = ( bin[1] & 0x0f ) << 2;
805 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
808 base64[n++] = HTTP_Base64Enc[x];
812 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
814 /* last 6 bits, all from bin [2] */
815 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
822 /***********************************************************************
823 * HTTP_EncodeBasicAuth
825 * Encode the basic authentication string for HTTP 1.1
827 static LPWSTR HTTP_EncodeBasicAuth( LPCWSTR username, LPCWSTR password)
831 static const WCHAR szBasic[] = {'B','a','s','i','c',' ',0};
832 static const WCHAR szColon[] = {':',0};
834 len = lstrlenW( username ) + 1 + lstrlenW ( password ) + 1;
835 in = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
839 len = lstrlenW(szBasic) +
840 (lstrlenW( username ) + 1 + lstrlenW ( password ))*2 + 1 + 1;
841 out = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
844 lstrcpyW( in, username );
845 lstrcatW( in, szColon );
846 lstrcatW( in, password );
847 lstrcpyW( out, szBasic );
848 HTTP_Base64( in, &out[strlenW(out)] );
850 HeapFree( GetProcessHeap(), 0, in );
855 /***********************************************************************
856 * HTTP_InsertProxyAuthorization
858 * Insert the basic authorization field in the request header
860 static BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
861 LPCWSTR username, LPCWSTR password )
863 WCHAR *authorization = HTTP_EncodeBasicAuth( username, password );
869 TRACE( "Inserting authorization: %s\n", debugstr_w( authorization ) );
871 ret = HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_PROXY_AUTHORIZATION],
874 HeapFree( GetProcessHeap(), 0, authorization );
879 /***********************************************************************
882 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
883 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
885 WCHAR buf[MAXHOSTNAME];
886 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
888 static const WCHAR szNul[] = { 0 };
889 URL_COMPONENTSW UrlComponents;
890 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }, szSlash[] = { '/',0 } ;
891 static const WCHAR szFormat1[] = { 'h','t','t','p',':','/','/','%','s',0 };
892 static const WCHAR szFormat2[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
895 memset( &UrlComponents, 0, sizeof UrlComponents );
896 UrlComponents.dwStructSize = sizeof UrlComponents;
897 UrlComponents.lpszHostName = buf;
898 UrlComponents.dwHostNameLength = MAXHOSTNAME;
900 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
901 hIC->lpszProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
902 sprintfW(proxy, szFormat1, hIC->lpszProxy);
904 strcpyW(proxy, hIC->lpszProxy);
905 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
907 if( UrlComponents.dwHostNameLength == 0 )
910 if( !lpwhr->lpszPath )
911 lpwhr->lpszPath = (LPWSTR)szNul;
912 TRACE("server='%s' path='%s'\n",
913 debugstr_w(lpwhs->lpszHostName), debugstr_w(lpwhr->lpszPath));
914 /* for constant 15 see above */
915 len = strlenW(lpwhs->lpszHostName) + strlenW(lpwhr->lpszPath) + 15;
916 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
918 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
919 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
921 sprintfW(url, szFormat2, lpwhs->lpszHostName, lpwhs->nServerPort);
923 if( lpwhr->lpszPath[0] != '/' )
924 strcatW( url, szSlash );
925 strcatW(url, lpwhr->lpszPath);
926 if(lpwhr->lpszPath != szNul)
927 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
928 lpwhr->lpszPath = url;
930 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
931 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
932 lpwhs->nServerPort = UrlComponents.nPort;
937 /***********************************************************************
938 * HTTP_HttpOpenRequestW (internal)
940 * Open a HTTP request handle
943 * HINTERNET a HTTP request handle on success
947 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
948 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
949 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
950 DWORD dwFlags, DWORD dwContext)
952 LPWININETAPPINFOW hIC = NULL;
953 LPWININETHTTPREQW lpwhr;
955 LPWSTR lpszUrl = NULL;
957 HINTERNET handle = NULL;
958 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
963 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
964 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
966 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
969 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
972 lpwhr->hdr.htype = WH_HHTTPREQ;
973 lpwhr->hdr.lpwhparent = WININET_AddRef( &lpwhs->hdr );
974 lpwhr->hdr.dwFlags = dwFlags;
975 lpwhr->hdr.dwContext = dwContext;
976 lpwhr->hdr.dwRefCount = 1;
977 lpwhr->hdr.destroy = HTTP_CloseHTTPRequestHandle;
978 lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
980 handle = WININET_AllocHandle( &lpwhr->hdr );
983 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
987 NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE);
989 if (NULL != lpszObjectName && strlenW(lpszObjectName)) {
993 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
995 len = strlenW(lpszObjectName)+1;
996 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
997 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
998 URL_ESCAPE_SPACES_ONLY);
1001 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(lpszObjectName),rc);
1002 strcpyW(lpwhr->lpszPath,lpszObjectName);
1006 if (NULL != lpszReferrer && strlenW(lpszReferrer))
1007 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDHDR_FLAG_COALESCE);
1009 if(lpszAcceptTypes!=NULL)
1012 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
1013 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i], HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_REQ|HTTP_ADDHDR_FLAG_ADD_IF_NEW);
1016 if (NULL == lpszVerb)
1018 static const WCHAR szGet[] = {'G','E','T',0};
1019 lpwhr->lpszVerb = WININET_strdupW(szGet);
1021 else if (strlenW(lpszVerb))
1022 lpwhr->lpszVerb = WININET_strdupW(lpszVerb);
1024 if (NULL != lpszReferrer && strlenW(lpszReferrer))
1026 WCHAR buf[MAXHOSTNAME];
1027 URL_COMPONENTSW UrlComponents;
1029 memset( &UrlComponents, 0, sizeof UrlComponents );
1030 UrlComponents.dwStructSize = sizeof UrlComponents;
1031 UrlComponents.lpszHostName = buf;
1032 UrlComponents.dwHostNameLength = MAXHOSTNAME;
1034 InternetCrackUrlW(lpszReferrer, 0, 0, &UrlComponents);
1035 if (strlenW(UrlComponents.lpszHostName))
1036 HTTP_ProcessHeader(lpwhr, g_szHost, UrlComponents.lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1039 HTTP_ProcessHeader(lpwhr, g_szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1041 if (lpwhs->nServerPort == INTERNET_INVALID_PORT_NUMBER)
1042 lpwhs->nServerPort = (dwFlags & INTERNET_FLAG_SECURE ?
1043 INTERNET_DEFAULT_HTTPS_PORT :
1044 INTERNET_DEFAULT_HTTP_PORT);
1046 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1047 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
1051 WCHAR *agent_header;
1052 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
1054 len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
1055 agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1056 sprintfW(agent_header, user_agent, hIC->lpszAgent );
1058 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
1059 HTTP_ADDREQ_FLAG_ADD);
1060 HeapFree(GetProcessHeap(), 0, agent_header);
1063 len = strlenW(lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue) + strlenW(szUrlForm);
1064 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1065 sprintfW( lpszUrl, szUrlForm, lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue );
1067 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
1068 InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
1071 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
1072 static const WCHAR szcrlf[] = {'\r','\n',0};
1074 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
1076 cnt += sprintfW(lpszCookies, szCookie);
1077 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
1078 strcatW(lpszCookies, szcrlf);
1080 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
1081 HTTP_ADDREQ_FLAG_ADD);
1082 HeapFree(GetProcessHeap(), 0, lpszCookies);
1084 HeapFree(GetProcessHeap(), 0, lpszUrl);
1087 SendAsyncCallback(&lpwhs->hdr, dwContext,
1088 INTERNET_STATUS_HANDLE_CREATED, &handle,
1092 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
1096 * According to my tests. The name is not resolved until a request is Opened
1098 SendAsyncCallback(&lpwhr->hdr, dwContext,
1099 INTERNET_STATUS_RESOLVING_NAME,
1100 lpwhs->lpszServerName,
1101 strlenW(lpwhs->lpszServerName)+1);
1103 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1104 &lpwhs->socketAddress))
1106 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1107 InternetCloseHandle( handle );
1112 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1113 INTERNET_STATUS_NAME_RESOLVED,
1114 &(lpwhs->socketAddress),
1115 sizeof(struct sockaddr_in));
1119 WININET_Release( &lpwhr->hdr );
1121 TRACE("<-- %p (%p)\n", handle, lpwhr);
1125 /***********************************************************************
1126 * HTTP_HttpQueryInfoW (internal)
1128 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
1129 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1131 LPHTTPHEADERW lphttpHdr = NULL;
1132 BOOL bSuccess = FALSE;
1134 /* Find requested header structure */
1135 if ((dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK) == HTTP_QUERY_CUSTOM)
1137 INT index = HTTP_GetCustomHeaderIndex(lpwhr, (LPWSTR)lpBuffer);
1142 lphttpHdr = &lpwhr->pCustHeaders[index];
1146 INT index = dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK;
1148 if (index == HTTP_QUERY_RAW_HEADERS_CRLF)
1150 DWORD len = strlenW(lpwhr->lpszRawHeaders);
1151 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1153 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1154 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1157 memcpy(lpBuffer, lpwhr->lpszRawHeaders, (len+1)*sizeof(WCHAR));
1158 *lpdwBufferLength = len * sizeof(WCHAR);
1160 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1164 else if (index == HTTP_QUERY_RAW_HEADERS)
1166 static const WCHAR szCrLf[] = {'\r','\n',0};
1167 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
1169 LPWSTR pszString = (WCHAR*)lpBuffer;
1171 for (i = 0; ppszRawHeaderLines[i]; i++)
1172 size += strlenW(ppszRawHeaderLines[i]) + 1;
1174 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
1176 HTTP_FreeTokens(ppszRawHeaderLines);
1177 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
1178 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1182 for (i = 0; ppszRawHeaderLines[i]; i++)
1184 DWORD len = strlenW(ppszRawHeaderLines[i]);
1185 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
1190 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
1192 *lpdwBufferLength = size * sizeof(WCHAR);
1193 HTTP_FreeTokens(ppszRawHeaderLines);
1197 else if (index >= 0 && index <= HTTP_QUERY_MAX && lpwhr->StdHeaders[index].lpszValue)
1199 lphttpHdr = &lpwhr->StdHeaders[index];
1203 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1208 /* Ensure header satisifies requested attributes */
1209 if ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
1210 (~lphttpHdr->wFlags & HDR_ISREQUEST))
1212 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1216 /* coalesce value to reuqested type */
1217 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
1219 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
1222 TRACE(" returning number : %d\n", *(int *)lpBuffer);
1224 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
1230 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
1232 tmpTM = *gmtime(&tmpTime);
1233 STHook = (SYSTEMTIME *) lpBuffer;
1237 STHook->wDay = tmpTM.tm_mday;
1238 STHook->wHour = tmpTM.tm_hour;
1239 STHook->wMilliseconds = 0;
1240 STHook->wMinute = tmpTM.tm_min;
1241 STHook->wDayOfWeek = tmpTM.tm_wday;
1242 STHook->wMonth = tmpTM.tm_mon + 1;
1243 STHook->wSecond = tmpTM.tm_sec;
1244 STHook->wYear = tmpTM.tm_year;
1248 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
1249 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
1250 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
1252 else if (dwInfoLevel & HTTP_QUERY_FLAG_COALESCE)
1254 if (*lpdwIndex >= lphttpHdr->wCount)
1256 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1260 /* Copy strncpyW(lpBuffer, lphttpHdr[*lpdwIndex], len); */
1266 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
1268 if (len > *lpdwBufferLength)
1270 *lpdwBufferLength = len;
1271 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1275 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
1276 *lpdwBufferLength = len - sizeof(WCHAR);
1279 TRACE(" returning string : '%s'\n", debugstr_w(lpBuffer));
1284 /***********************************************************************
1285 * HttpQueryInfoW (WININET.@)
1287 * Queries for information about an HTTP request
1294 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1295 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1297 BOOL bSuccess = FALSE;
1298 LPWININETHTTPREQW lpwhr;
1300 if (TRACE_ON(wininet)) {
1301 #define FE(x) { x, #x }
1302 static const wininet_flag_info query_flags[] = {
1303 FE(HTTP_QUERY_MIME_VERSION),
1304 FE(HTTP_QUERY_CONTENT_TYPE),
1305 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
1306 FE(HTTP_QUERY_CONTENT_ID),
1307 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
1308 FE(HTTP_QUERY_CONTENT_LENGTH),
1309 FE(HTTP_QUERY_CONTENT_LANGUAGE),
1310 FE(HTTP_QUERY_ALLOW),
1311 FE(HTTP_QUERY_PUBLIC),
1312 FE(HTTP_QUERY_DATE),
1313 FE(HTTP_QUERY_EXPIRES),
1314 FE(HTTP_QUERY_LAST_MODIFIED),
1315 FE(HTTP_QUERY_MESSAGE_ID),
1317 FE(HTTP_QUERY_DERIVED_FROM),
1318 FE(HTTP_QUERY_COST),
1319 FE(HTTP_QUERY_LINK),
1320 FE(HTTP_QUERY_PRAGMA),
1321 FE(HTTP_QUERY_VERSION),
1322 FE(HTTP_QUERY_STATUS_CODE),
1323 FE(HTTP_QUERY_STATUS_TEXT),
1324 FE(HTTP_QUERY_RAW_HEADERS),
1325 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
1326 FE(HTTP_QUERY_CONNECTION),
1327 FE(HTTP_QUERY_ACCEPT),
1328 FE(HTTP_QUERY_ACCEPT_CHARSET),
1329 FE(HTTP_QUERY_ACCEPT_ENCODING),
1330 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
1331 FE(HTTP_QUERY_AUTHORIZATION),
1332 FE(HTTP_QUERY_CONTENT_ENCODING),
1333 FE(HTTP_QUERY_FORWARDED),
1334 FE(HTTP_QUERY_FROM),
1335 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
1336 FE(HTTP_QUERY_LOCATION),
1337 FE(HTTP_QUERY_ORIG_URI),
1338 FE(HTTP_QUERY_REFERER),
1339 FE(HTTP_QUERY_RETRY_AFTER),
1340 FE(HTTP_QUERY_SERVER),
1341 FE(HTTP_QUERY_TITLE),
1342 FE(HTTP_QUERY_USER_AGENT),
1343 FE(HTTP_QUERY_WWW_AUTHENTICATE),
1344 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
1345 FE(HTTP_QUERY_ACCEPT_RANGES),
1346 FE(HTTP_QUERY_SET_COOKIE),
1347 FE(HTTP_QUERY_COOKIE),
1348 FE(HTTP_QUERY_REQUEST_METHOD),
1349 FE(HTTP_QUERY_REFRESH),
1350 FE(HTTP_QUERY_CONTENT_DISPOSITION),
1352 FE(HTTP_QUERY_CACHE_CONTROL),
1353 FE(HTTP_QUERY_CONTENT_BASE),
1354 FE(HTTP_QUERY_CONTENT_LOCATION),
1355 FE(HTTP_QUERY_CONTENT_MD5),
1356 FE(HTTP_QUERY_CONTENT_RANGE),
1357 FE(HTTP_QUERY_ETAG),
1358 FE(HTTP_QUERY_HOST),
1359 FE(HTTP_QUERY_IF_MATCH),
1360 FE(HTTP_QUERY_IF_NONE_MATCH),
1361 FE(HTTP_QUERY_IF_RANGE),
1362 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
1363 FE(HTTP_QUERY_MAX_FORWARDS),
1364 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
1365 FE(HTTP_QUERY_RANGE),
1366 FE(HTTP_QUERY_TRANSFER_ENCODING),
1367 FE(HTTP_QUERY_UPGRADE),
1368 FE(HTTP_QUERY_VARY),
1370 FE(HTTP_QUERY_WARNING),
1371 FE(HTTP_QUERY_CUSTOM)
1373 static const wininet_flag_info modifier_flags[] = {
1374 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
1375 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
1376 FE(HTTP_QUERY_FLAG_NUMBER),
1377 FE(HTTP_QUERY_FLAG_COALESCE)
1380 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
1381 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
1384 TRACE("(%p, 0x%08lx)--> %ld\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
1385 TRACE(" Attribute:");
1386 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
1387 if (query_flags[i].val == info) {
1388 TRACE(" %s", query_flags[i].name);
1392 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
1393 TRACE(" Unknown (%08lx)", info);
1396 TRACE(" Modifier:");
1397 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
1398 if (modifier_flags[i].val & info_mod) {
1399 TRACE(" %s", modifier_flags[i].name);
1400 info_mod &= ~ modifier_flags[i].val;
1405 TRACE(" Unknown (%08lx)", info_mod);
1410 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1411 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1413 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1417 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
1418 lpBuffer, lpdwBufferLength, lpdwIndex);
1422 WININET_Release( &lpwhr->hdr );
1424 TRACE("%d <--\n", bSuccess);
1428 /***********************************************************************
1429 * HttpQueryInfoA (WININET.@)
1431 * Queries for information about an HTTP request
1438 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1439 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1445 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
1446 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
1448 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
1449 lpdwBufferLength, lpdwIndex );
1452 len = (*lpdwBufferLength)*sizeof(WCHAR);
1453 bufferW = HeapAlloc( GetProcessHeap(), 0, len );
1454 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
1458 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
1459 lpBuffer, *lpdwBufferLength, NULL, NULL );
1460 *lpdwBufferLength = len - 1;
1462 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
1465 /* since the strings being returned from HttpQueryInfoW should be
1466 * only ASCII characters, it is reasonable to assume that all of
1467 * the Unicode characters can be reduced to a single byte */
1468 *lpdwBufferLength = len / sizeof(WCHAR);
1470 HeapFree(GetProcessHeap(), 0, bufferW );
1475 /***********************************************************************
1476 * HttpSendRequestExA (WININET.@)
1478 * Sends the specified request to the HTTP server and allows chunked
1481 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
1482 LPINTERNET_BUFFERSA lpBuffersIn,
1483 LPINTERNET_BUFFERSA lpBuffersOut,
1484 DWORD dwFlags, DWORD dwContext)
1486 LPINTERNET_BUFFERSA ptr;
1487 LPINTERNET_BUFFERSW lpBuffersInW,ptrW;
1490 TRACE("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersIn,
1491 lpBuffersOut, dwFlags, dwContext);
1495 lpBuffersInW = (LPINTERNET_BUFFERSW)HeapAlloc(GetProcessHeap(),
1496 HEAP_ZERO_MEMORY, sizeof(INTERNET_BUFFERSW));
1498 lpBuffersInW = NULL;
1500 ptrW = lpBuffersInW;
1504 ptrW->dwStructSize = sizeof(LPINTERNET_BUFFERSW);
1505 if (ptr->lpcszHeader)
1507 headerlen = MultiByteToWideChar(CP_ACP,0,ptr->lpcszHeader,
1508 ptr->dwHeadersLength,0,0);
1510 ptrW->lpcszHeader = HeapAlloc(GetProcessHeap(),0,headerlen*
1512 ptrW->dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
1513 ptr->lpcszHeader, ptr->dwHeadersLength,
1514 (LPWSTR)ptrW->lpcszHeader, headerlen);
1516 ptrW->dwHeadersTotal = ptr->dwHeadersTotal;
1517 ptrW->lpvBuffer = ptr->lpvBuffer;
1518 ptrW->dwBufferLength = ptr->dwBufferLength;
1519 ptrW->dwBufferTotal= ptr->dwBufferTotal;
1522 ptrW->Next = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
1523 sizeof(INTERNET_BUFFERSW));
1529 rc = HttpSendRequestExW(hRequest, lpBuffersInW, NULL, dwFlags, dwContext);
1532 ptrW = lpBuffersInW;
1535 LPINTERNET_BUFFERSW ptrW2;
1536 HeapFree(GetProcessHeap(),0,(LPVOID)ptrW->lpcszHeader);
1538 HeapFree(GetProcessHeap(),0,ptrW);
1545 /***********************************************************************
1546 * HttpSendRequestExW (WININET.@)
1548 * Sends the specified request to the HTTP server and allows chunked
1551 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
1552 LPINTERNET_BUFFERSW lpBuffersIn,
1553 LPINTERNET_BUFFERSW lpBuffersOut,
1554 DWORD dwFlags, DWORD dwContext)
1556 LPINTERNET_BUFFERSW buf_ptr;
1557 DWORD bufferlen = 0;
1559 LPWININETHTTPREQW lpwhr;
1560 LPWSTR requestString = NULL;
1565 TRACE("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersIn,
1566 lpBuffersOut, dwFlags, dwContext);
1568 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
1570 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1572 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1576 HTTP_FixVerb(lpwhr);
1579 buf_ptr = lpBuffersIn;
1582 if (buf_ptr->lpcszHeader)
1584 HTTP_HttpAddRequestHeadersW(lpwhr, buf_ptr->lpcszHeader,
1585 buf_ptr->dwHeadersLength, HTTP_ADDREQ_FLAG_ADD |
1586 HTTP_ADDHDR_FLAG_REPLACE);
1588 bufferlen += buf_ptr->dwBufferTotal;
1589 buf_ptr = buf_ptr->Next;
1592 lpwhr->hdr.dwFlags |= dwFlags;
1593 lpwhr->hdr.dwContext = dwContext;
1597 static const WCHAR szContentLength[] = {
1598 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ',
1599 '%','l','i','\r','\n',0};
1600 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \n\r */ +
1602 sprintfW(contentLengthStr, szContentLength, bufferlen);
1603 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L,
1604 HTTP_ADDREQ_FLAG_ADD);
1609 /* Do Send Request logic */
1611 TRACE("Going to url %s %s\n",
1612 debugstr_w(lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue),
1613 debugstr_w(lpwhr->lpszPath));
1615 HTTP_AddProxyInfo(lpwhr);
1617 requestString = HTTP_BuildHeaderRequestString(lpwhr);
1619 TRACE("Request header -> %s\n", debugstr_w(requestString) );
1621 if (!(rc = HTTP_OpenConnection(lpwhr)))
1624 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1625 INTERNET_STATUS_CONNECTED_TO_SERVER, NULL, 0);
1627 /* send the request as ASCII */
1628 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1, NULL, 0, NULL,
1631 /* we do not want the null because optional data will be sent with following
1632 * InternetWriteFile calls
1635 ascii_req = HeapAlloc( GetProcessHeap(), 0, len);
1636 WideCharToMultiByte( CP_ACP, 0, requestString, -1, ascii_req, len, NULL,
1638 TRACE("full request -> %s\n", debugstr_an(ascii_req,len) );
1640 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1641 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
1643 rc = NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
1644 HeapFree( GetProcessHeap(), 0, ascii_req );
1646 /* This may not be sent here. It may come laster */
1647 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1648 INTERNET_STATUS_REQUEST_SENT, &len,sizeof(DWORD));
1651 HeapFree(GetProcessHeap(), 0, requestString);
1652 WININET_Release(&lpwhr->hdr);
1657 /***********************************************************************
1658 * HttpSendRequestW (WININET.@)
1660 * Sends the specified request to the HTTP server
1667 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
1668 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1670 LPWININETHTTPREQW lpwhr;
1671 LPWININETHTTPSESSIONW lpwhs = NULL;
1672 LPWININETAPPINFOW hIC = NULL;
1675 TRACE("%p, %p (%s), %li, %p, %li)\n", hHttpRequest,
1676 lpszHeaders, debugstr_w(lpszHeaders), dwHeaderLength, lpOptional, dwOptionalLength);
1678 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1679 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1681 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1686 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1687 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1689 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1694 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1695 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1697 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1702 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1704 WORKREQUEST workRequest;
1705 struct WORKREQ_HTTPSENDREQUESTW *req;
1707 workRequest.asyncall = HTTPSENDREQUESTW;
1708 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
1709 req = &workRequest.u.HttpSendRequestW;
1711 req->lpszHeader = WININET_strdupW(lpszHeaders);
1713 req->lpszHeader = 0;
1714 req->dwHeaderLength = dwHeaderLength;
1715 req->lpOptional = lpOptional;
1716 req->dwOptionalLength = dwOptionalLength;
1718 INTERNET_AsyncCall(&workRequest);
1720 * This is from windows.
1722 SetLastError(ERROR_IO_PENDING);
1727 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
1728 dwHeaderLength, lpOptional, dwOptionalLength);
1732 WININET_Release( &lpwhr->hdr );
1736 /***********************************************************************
1737 * HttpSendRequestA (WININET.@)
1739 * Sends the specified request to the HTTP server
1746 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
1747 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1750 LPWSTR szHeaders=NULL;
1751 DWORD nLen=dwHeaderLength;
1752 if(lpszHeaders!=NULL)
1754 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
1755 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
1756 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
1758 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
1759 HeapFree(GetProcessHeap(),0,szHeaders);
1763 /***********************************************************************
1764 * HTTP_HandleRedirect (internal)
1766 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl, LPCWSTR lpszHeaders,
1767 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength)
1769 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1770 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1775 /* if it's an absolute path, keep the same session info */
1776 strcpyW(path,lpszUrl);
1778 else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1780 TRACE("Redirect through proxy\n");
1781 strcpyW(path,lpszUrl);
1785 URL_COMPONENTSW urlComponents;
1786 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
1787 WCHAR password[1024], extra[1024];
1788 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
1789 urlComponents.lpszScheme = protocol;
1790 urlComponents.dwSchemeLength = 32;
1791 urlComponents.lpszHostName = hostName;
1792 urlComponents.dwHostNameLength = MAXHOSTNAME;
1793 urlComponents.lpszUserName = userName;
1794 urlComponents.dwUserNameLength = 1024;
1795 urlComponents.lpszPassword = password;
1796 urlComponents.dwPasswordLength = 1024;
1797 urlComponents.lpszUrlPath = path;
1798 urlComponents.dwUrlPathLength = 2048;
1799 urlComponents.lpszExtraInfo = extra;
1800 urlComponents.dwExtraInfoLength = 1024;
1801 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
1804 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1805 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1809 * This upsets redirects to binary files on sourceforge.net
1810 * and gives an html page instead of the target file
1811 * Examination of the HTTP request sent by native wininet.dll
1812 * reveals that it doesn't send a referrer in that case.
1813 * Maybe there's a flag that enables this, or maybe a referrer
1814 * shouldn't be added in case of a redirect.
1817 /* consider the current host as the referrer */
1818 if (NULL != lpwhs->lpszServerName && strlenW(lpwhs->lpszServerName))
1819 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
1820 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
1821 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
1824 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
1825 lpwhs->lpszHostName = WININET_strdupW(hostName);
1826 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1827 lpwhs->lpszServerName = WININET_strdupW(hostName);
1828 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
1829 lpwhs->lpszUserName = WININET_strdupW(userName);
1830 lpwhs->nServerPort = urlComponents.nPort;
1832 HTTP_ProcessHeader(lpwhr, g_szHost, hostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1834 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1835 INTERNET_STATUS_RESOLVING_NAME,
1836 lpwhs->lpszServerName,
1837 strlenW(lpwhs->lpszServerName)+1);
1839 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1840 &lpwhs->socketAddress))
1842 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1846 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1847 INTERNET_STATUS_NAME_RESOLVED,
1848 &(lpwhs->socketAddress),
1849 sizeof(struct sockaddr_in));
1853 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1854 lpwhr->lpszPath=NULL;
1860 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
1861 if (rc != E_POINTER)
1862 needed = strlenW(path)+1;
1863 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
1864 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
1865 URL_ESCAPE_SPACES_ONLY);
1868 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(path),rc);
1869 strcpyW(lpwhr->lpszPath,path);
1873 return HTTP_HttpSendRequestW(lpwhr, lpszHeaders, dwHeaderLength, lpOptional, dwOptionalLength);
1876 /***********************************************************************
1877 * HTTP_build_req (internal)
1879 * concatenate all the strings in the request together
1881 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
1886 for( t = list; *t ; t++ )
1887 len += strlenW( *t );
1890 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1893 for( t = list; *t ; t++ )
1899 /***********************************************************************
1900 * HTTP_HttpSendRequestW (internal)
1902 * Sends the specified request to the HTTP server
1909 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
1910 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1913 BOOL bSuccess = FALSE;
1914 LPWSTR requestString = NULL;
1916 BOOL loop_next = FALSE;
1917 INTERNET_ASYNC_RESULT iar;
1919 TRACE("--> %p\n", lpwhr);
1921 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
1923 /* Clear any error information */
1924 INTERNET_SetLastError(0);
1926 HTTP_FixVerb(lpwhr);
1928 /* if we are using optional stuff, we must add the fixed header of that option length */
1929 if (lpOptional && dwOptionalLength)
1931 static const WCHAR szContentLength[] = {
1932 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0};
1933 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \n\r */ + 20 /* int */ ];
1934 sprintfW(contentLengthStr, szContentLength, dwOptionalLength);
1935 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD);
1943 TRACE("Going to url %s %s\n", debugstr_w(lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue), debugstr_w(lpwhr->lpszPath));
1948 /* add the headers the caller supplied */
1949 if( lpszHeaders && dwHeaderLength )
1951 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
1952 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
1955 /* if there's a proxy username and password, add it to the headers */
1956 HTTP_AddProxyInfo(lpwhr);
1958 requestString = HTTP_BuildHeaderRequestString(lpwhr);
1960 TRACE("Request header -> %s\n", debugstr_w(requestString) );
1962 /* Send the request and store the results */
1963 if (!HTTP_OpenConnection(lpwhr))
1966 /* send the request as ASCII, tack on the optional data */
1968 dwOptionalLength = 0;
1969 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
1970 NULL, 0, NULL, NULL );
1971 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
1972 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
1973 ascii_req, len, NULL, NULL );
1975 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
1976 len = (len + dwOptionalLength - 1);
1978 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
1980 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1981 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
1983 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
1984 HeapFree( GetProcessHeap(), 0, ascii_req );
1986 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1987 INTERNET_STATUS_REQUEST_SENT,
1988 &len,sizeof(DWORD));
1990 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1991 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
1996 responseLen = HTTP_GetResponseHeaders(lpwhr);
2000 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2001 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
2004 /* process headers here. Is this right? */
2005 HTTP_ProcessHeaders(lpwhr);
2011 HeapFree(GetProcessHeap(), 0, requestString);
2013 /* TODO: send notification for P3P header */
2015 if(!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
2017 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
2018 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
2019 (dwCode==302 || dwCode==301))
2021 WCHAR szNewLocation[2048];
2022 DWORD dwBufferSize=2048;
2024 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
2026 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2027 INTERNET_STATUS_REDIRECT, szNewLocation,
2029 return HTTP_HandleRedirect(lpwhr, szNewLocation, lpszHeaders,
2030 dwHeaderLength, lpOptional, dwOptionalLength);
2036 iar.dwResult = (DWORD)bSuccess;
2037 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
2039 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2040 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2041 sizeof(INTERNET_ASYNC_RESULT));
2048 /***********************************************************************
2049 * HTTP_Connect (internal)
2051 * Create http session handle
2054 * HINTERNET a session handle on success
2058 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
2059 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
2060 LPCWSTR lpszPassword, DWORD dwFlags, DWORD dwContext,
2061 DWORD dwInternalFlags)
2063 BOOL bSuccess = FALSE;
2064 LPWININETHTTPSESSIONW lpwhs = NULL;
2065 HINTERNET handle = NULL;
2069 assert( hIC->hdr.htype == WH_HINIT );
2071 hIC->hdr.dwContext = dwContext;
2073 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
2076 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2081 * According to my tests. The name is not resolved until a request is sent
2084 lpwhs->hdr.htype = WH_HHTTPSESSION;
2085 lpwhs->hdr.lpwhparent = WININET_AddRef( &hIC->hdr );
2086 lpwhs->hdr.dwFlags = dwFlags;
2087 lpwhs->hdr.dwContext = dwContext;
2088 lpwhs->hdr.dwInternalFlags = dwInternalFlags;
2089 lpwhs->hdr.dwRefCount = 1;
2090 lpwhs->hdr.destroy = HTTP_CloseHTTPSessionHandle;
2091 lpwhs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
2093 handle = WININET_AllocHandle( &lpwhs->hdr );
2096 ERR("Failed to alloc handle\n");
2097 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2101 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
2102 if(strchrW(hIC->lpszProxy, ' '))
2103 FIXME("Several proxies not implemented.\n");
2104 if(hIC->lpszProxyBypass)
2105 FIXME("Proxy bypass is ignored.\n");
2107 if (NULL != lpszServerName)
2109 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
2110 lpwhs->lpszHostName = WININET_strdupW(lpszServerName);
2112 if (NULL != lpszUserName)
2113 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
2114 lpwhs->nServerPort = nServerPort;
2116 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2117 if (!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
2119 SendAsyncCallback(&hIC->hdr, dwContext,
2120 INTERNET_STATUS_HANDLE_CREATED, &handle,
2128 WININET_Release( &lpwhs->hdr );
2131 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
2135 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
2140 /***********************************************************************
2141 * HTTP_OpenConnection (internal)
2143 * Connect to a web server
2150 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
2152 BOOL bSuccess = FALSE;
2153 LPWININETHTTPSESSIONW lpwhs;
2154 LPWININETAPPINFOW hIC = NULL;
2159 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2161 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2165 lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
2167 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2168 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2169 INTERNET_STATUS_CONNECTING_TO_SERVER,
2170 &(lpwhs->socketAddress),
2171 sizeof(struct sockaddr_in));
2173 if (!NETCON_create(&lpwhr->netConnection, lpwhs->socketAddress.sin_family,
2176 WARN("Socket creation failed\n");
2180 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
2181 sizeof(lpwhs->socketAddress)))
2183 WARN("Unable to connect to host (%s)\n", strerror(errno));
2187 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2188 INTERNET_STATUS_CONNECTED_TO_SERVER,
2189 &(lpwhs->socketAddress),
2190 sizeof(struct sockaddr_in));
2195 TRACE("%d <--\n", bSuccess);
2200 /***********************************************************************
2201 * HTTP_clear_response_headers (internal)
2203 * clear out any old response headers
2205 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
2209 for( i=0; i<=HTTP_QUERY_MAX; i++ )
2211 if( !lpwhr->StdHeaders[i].lpszField )
2213 if( !lpwhr->StdHeaders[i].lpszValue )
2215 if ( lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST )
2217 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[i], NULL );
2218 HeapFree( GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszField );
2219 lpwhr->StdHeaders[i].lpszField = NULL;
2221 for( i=0; i<lpwhr->nCustHeaders; i++)
2223 if( !lpwhr->pCustHeaders[i].lpszField )
2225 if( !lpwhr->pCustHeaders[i].lpszValue )
2227 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
2229 HTTP_DeleteCustomHeader( lpwhr, i );
2234 /***********************************************************************
2235 * HTTP_GetResponseHeaders (internal)
2237 * Read server response
2244 static BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr)
2247 WCHAR buffer[MAX_REPLY_LEN];
2248 DWORD buflen = MAX_REPLY_LEN;
2249 BOOL bSuccess = FALSE;
2251 static const WCHAR szCrLf[] = {'\r','\n',0};
2252 char bufferA[MAX_REPLY_LEN];
2253 LPWSTR status_code, status_text;
2254 DWORD cchMaxRawHeaders = 1024;
2255 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2256 DWORD cchRawHeaders = 0;
2260 /* clear old response headers (eg. from a redirect response) */
2261 HTTP_clear_response_headers( lpwhr );
2263 if (!NETCON_connected(&lpwhr->netConnection))
2267 * HACK peek at the buffer
2269 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
2272 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
2274 buflen = MAX_REPLY_LEN;
2275 memset(buffer, 0, MAX_REPLY_LEN);
2276 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2278 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2280 /* regenerate raw headers */
2281 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2283 cchMaxRawHeaders *= 2;
2284 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2286 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2287 cchRawHeaders += (buflen-1);
2288 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2289 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2290 lpszRawHeaders[cchRawHeaders] = '\0';
2292 /* split the version from the status code */
2293 status_code = strchrW( buffer, ' ' );
2298 /* split the status code from the status text */
2299 status_text = strchrW( status_code, ' ' );
2304 TRACE("version [%s] status code [%s] status text [%s]\n",
2305 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
2306 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_VERSION], buffer );
2307 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_STATUS_CODE], status_code );
2308 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_STATUS_TEXT], status_text );
2310 /* Parse each response line */
2313 buflen = MAX_REPLY_LEN;
2314 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2316 LPWSTR * pFieldAndValue;
2318 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
2319 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2321 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2323 cchMaxRawHeaders *= 2;
2324 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2326 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2327 cchRawHeaders += (buflen-1);
2328 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2329 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2330 lpszRawHeaders[cchRawHeaders] = '\0';
2332 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
2333 if (!pFieldAndValue)
2336 HTTP_ProcessHeader(lpwhr, pFieldAndValue[0], pFieldAndValue[1],
2337 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE);
2339 HTTP_FreeTokens(pFieldAndValue);
2349 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2350 lpwhr->lpszRawHeaders = lpszRawHeaders;
2351 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
2364 static void strip_spaces(LPWSTR start)
2369 while (*str == ' ' && *str != '\0')
2373 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
2375 end = start + strlenW(start) - 1;
2376 while (end >= start && *end == ' ')
2384 /***********************************************************************
2385 * HTTP_InterpretHttpHeader (internal)
2387 * Parse server response
2391 * Pointer to array of field, value, NULL on success.
2394 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
2396 LPWSTR * pTokenPair;
2400 pTokenPair = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pTokenPair)*3);
2402 pszColon = strchrW(buffer, ':');
2403 /* must have two tokens */
2406 HTTP_FreeTokens(pTokenPair);
2408 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
2412 pTokenPair[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon - buffer + 1) * sizeof(WCHAR));
2415 HTTP_FreeTokens(pTokenPair);
2418 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
2419 pTokenPair[0][pszColon - buffer] = '\0';
2423 len = strlenW(pszColon);
2424 pTokenPair[1] = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
2427 HTTP_FreeTokens(pTokenPair);
2430 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
2432 strip_spaces(pTokenPair[0]);
2433 strip_spaces(pTokenPair[1]);
2435 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
2439 typedef enum {REQUEST_HDR = 1, RESPONSE_HDR = 2, REQ_RESP_HDR = 3} std_hdr_type;
2441 typedef struct std_hdr_data
2443 const WCHAR* hdrStr;
2445 std_hdr_type hdrType;
2448 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
2449 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
2450 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
2451 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
2452 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
2453 static const WCHAR szAge[] = { 'A','g','e',0 };
2454 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
2455 static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
2456 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
2457 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
2458 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
2459 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
2460 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
2461 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
2462 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
2463 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
2464 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
2465 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
2466 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 };
2467 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
2468 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
2469 static const WCHAR szDate[] = { 'D','a','t','e',0 };
2470 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
2471 static const WCHAR szETag[] = { 'E','T','a','g',0 };
2472 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
2473 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
2474 static const WCHAR szHost[] = { 'H','o','s','t',0 };
2475 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
2476 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
2477 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
2478 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
2479 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
2480 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
2481 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
2482 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
2483 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
2484 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
2485 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
2486 static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
2487 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
2488 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
2489 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
2490 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
2491 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
2492 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
2493 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
2494 static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 };
2495 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
2496 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 };
2497 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
2498 static const WCHAR szURI[] = { 'U','R','I',0 };
2499 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
2500 static const WCHAR szVary[] = { 'V','a','r','y',0 };
2501 static const WCHAR szVia[] = { 'V','i','a',0 };
2502 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
2503 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
2505 /* Note: Must be kept sorted! */
2506 const std_hdr_data SORTED_STANDARD_HEADERS[] = {
2507 {szAccept, HTTP_QUERY_ACCEPT, REQUEST_HDR,},
2508 {szAccept_Charset, HTTP_QUERY_ACCEPT_CHARSET, REQUEST_HDR,},
2509 {szAccept_Encoding, HTTP_QUERY_ACCEPT_ENCODING, REQUEST_HDR,},
2510 {szAccept_Language, HTTP_QUERY_ACCEPT_LANGUAGE, REQUEST_HDR,},
2511 {szAccept_Ranges, HTTP_QUERY_ACCEPT_RANGES, RESPONSE_HDR,},
2512 {szAge, HTTP_QUERY_AGE, RESPONSE_HDR,},
2513 {szAllow, HTTP_QUERY_ALLOW, REQ_RESP_HDR,},
2514 {szAuthorization, HTTP_QUERY_AUTHORIZATION, REQUEST_HDR,},
2515 {szCache_Control, HTTP_QUERY_CACHE_CONTROL, REQ_RESP_HDR,},
2516 {szConnection, HTTP_QUERY_CONNECTION, REQ_RESP_HDR,},
2517 {szContent_Base, HTTP_QUERY_CONTENT_BASE, REQ_RESP_HDR,},
2518 {szContent_Encoding, HTTP_QUERY_CONTENT_ENCODING, REQ_RESP_HDR,},
2519 {szContent_ID, HTTP_QUERY_CONTENT_ID, REQ_RESP_HDR,},
2520 {szContent_Language, HTTP_QUERY_CONTENT_LANGUAGE, REQ_RESP_HDR,},
2521 {szContent_Length, HTTP_QUERY_CONTENT_LENGTH, REQ_RESP_HDR,},
2522 {szContent_Location, HTTP_QUERY_CONTENT_LOCATION, REQ_RESP_HDR,},
2523 {szContent_MD5, HTTP_QUERY_CONTENT_MD5, REQ_RESP_HDR,},
2524 {szContent_Range, HTTP_QUERY_CONTENT_RANGE, REQ_RESP_HDR,},
2525 {szContent_Transfer_Encoding,HTTP_QUERY_CONTENT_TRANSFER_ENCODING, REQ_RESP_HDR,},
2526 {szContent_Type, HTTP_QUERY_CONTENT_TYPE, REQ_RESP_HDR,},
2527 {szCookie, HTTP_QUERY_COOKIE, REQUEST_HDR,},
2528 {szDate, HTTP_QUERY_DATE, REQ_RESP_HDR,},
2529 {szETag, HTTP_QUERY_ETAG, REQ_RESP_HDR,},
2530 {szExpect, HTTP_QUERY_EXPECT, REQUEST_HDR,},
2531 {szExpires, HTTP_QUERY_EXPIRES, REQ_RESP_HDR,},
2532 {szFrom, HTTP_QUERY_DERIVED_FROM, REQUEST_HDR,},
2533 {szHost, HTTP_QUERY_HOST, REQUEST_HDR,},
2534 {szIf_Match, HTTP_QUERY_IF_MATCH, REQUEST_HDR,},
2535 {szIf_Modified_Since, HTTP_QUERY_IF_MODIFIED_SINCE, REQUEST_HDR,},
2536 {szIf_None_Match, HTTP_QUERY_IF_NONE_MATCH, REQUEST_HDR,},
2537 {szIf_Range, HTTP_QUERY_IF_RANGE, REQUEST_HDR,},
2538 {szIf_Unmodified_Since, HTTP_QUERY_IF_UNMODIFIED_SINCE, REQUEST_HDR,},
2539 {szLast_Modified, HTTP_QUERY_LAST_MODIFIED, REQ_RESP_HDR,},
2540 {szLocation, HTTP_QUERY_LOCATION, REQ_RESP_HDR,},
2541 {szMax_Forwards, HTTP_QUERY_MAX_FORWARDS, REQUEST_HDR,},
2542 {szMime_Version, HTTP_QUERY_MIME_VERSION, REQ_RESP_HDR,},
2543 {szPragma, HTTP_QUERY_PRAGMA, REQ_RESP_HDR,},
2544 {szProxy_Authenticate, HTTP_QUERY_PROXY_AUTHENTICATE, RESPONSE_HDR,},
2545 {szProxy_Authorization, HTTP_QUERY_PROXY_AUTHORIZATION, REQUEST_HDR,},
2546 {szProxy_Connection, HTTP_QUERY_PROXY_CONNECTION, REQ_RESP_HDR,},
2547 {szPublic, HTTP_QUERY_PUBLIC, RESPONSE_HDR,},
2548 {szRange, HTTP_QUERY_RANGE, REQUEST_HDR,},
2549 {szReferer, HTTP_QUERY_REFERER, REQUEST_HDR,},
2550 {szRetry_After, HTTP_QUERY_RETRY_AFTER, RESPONSE_HDR,},
2551 {szServer, HTTP_QUERY_SERVER, RESPONSE_HDR,},
2552 {szSet_Cookie, HTTP_QUERY_SET_COOKIE, RESPONSE_HDR,},
2553 {szStatus, HTTP_QUERY_STATUS_CODE, RESPONSE_HDR,},
2554 {szTransfer_Encoding, HTTP_QUERY_TRANSFER_ENCODING, REQ_RESP_HDR,},
2555 {szUnless_Modified_Since, HTTP_QUERY_UNLESS_MODIFIED_SINCE, REQUEST_HDR,},
2556 {szUpgrade, HTTP_QUERY_UPGRADE, REQ_RESP_HDR,},
2557 {szURI, HTTP_QUERY_URI, REQ_RESP_HDR,},
2558 {szUser_Agent, HTTP_QUERY_USER_AGENT, REQUEST_HDR,},
2559 {szVary, HTTP_QUERY_VARY, RESPONSE_HDR,},
2560 {szVia, HTTP_QUERY_VIA, REQ_RESP_HDR,},
2561 {szWarning, HTTP_QUERY_WARNING, RESPONSE_HDR,},
2562 {szWWW_Authenticate, HTTP_QUERY_WWW_AUTHENTICATE, RESPONSE_HDR},
2565 /***********************************************************************
2566 * HTTP_GetStdHeaderIndex (internal)
2568 * Lookup field index in standard http header array
2570 * FIXME: Add support for HeaderType to avoid inadvertant assignments of
2571 * response headers to requests and looking for request headers
2575 static INT HTTP_GetStdHeaderIndex(LPCWSTR lpszField)
2578 INT hi = sizeof(SORTED_STANDARD_HEADERS) / sizeof(std_hdr_data) -1;
2583 for (i = 0; i < sizeof(SORTED_STANDARD_HEADERS) / sizeof(std_hdr_data) -1; i++)
2584 if (lstrcmpiW(SORTED_STANDARD_HEADERS[i].hdrStr, SORTED_STANDARD_HEADERS[i+1].hdrStr) > 0)
2585 ERR("%s should be after %s\n", debugstr_w(SORTED_STANDARD_HEADERS[i].hdrStr), debugstr_w(SORTED_STANDARD_HEADERS[i+1].hdrStr));
2589 mid = (int) (lo + hi) / 2;
2590 inx = lstrcmpiW(lpszField, SORTED_STANDARD_HEADERS[mid].hdrStr);
2592 return SORTED_STANDARD_HEADERS[mid].hdrIndex;
2598 WARN("Couldn't find %s in standard header table\n", debugstr_w(lpszField));
2602 /***********************************************************************
2603 * HTTP_ReplaceHeaderValue (internal)
2605 static BOOL HTTP_ReplaceHeaderValue( LPHTTPHEADERW lphttpHdr, LPCWSTR value )
2609 HeapFree( GetProcessHeap(), 0, lphttpHdr->lpszValue );
2610 lphttpHdr->lpszValue = NULL;
2613 len = strlenW(value);
2616 lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0,
2617 (len+1)*sizeof(WCHAR));
2618 strcpyW(lphttpHdr->lpszValue, value);
2623 /***********************************************************************
2624 * HTTP_ProcessHeader (internal)
2626 * Stuff header into header tables according to <dwModifier>
2630 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2632 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
2634 LPHTTPHEADERW lphttpHdr = NULL;
2635 BOOL bSuccess = FALSE;
2638 TRACE("--> %s: %s - 0x%08lx\n", debugstr_w(field), debugstr_w(value), dwModifier);
2640 /* Adjust modifier flags */
2641 if (dwModifier & COALESCEFLASG)
2642 dwModifier |= HTTP_ADDHDR_FLAG_ADD;
2644 /* Try to get index into standard header array */
2645 index = HTTP_GetStdHeaderIndex(field);
2646 /* Don't let applications add Connection header to request */
2647 if ((index == HTTP_QUERY_CONNECTION) && (dwModifier & HTTP_ADDHDR_FLAG_REQ))
2649 else if (index >= 0)
2651 lphttpHdr = &lpwhr->StdHeaders[index];
2653 else /* Find or create new custom header */
2655 index = HTTP_GetCustomHeaderIndex(lpwhr, field);
2658 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
2662 lphttpHdr = &lpwhr->pCustHeaders[index];
2668 hdr.lpszField = (LPWSTR)field;
2669 hdr.lpszValue = (LPWSTR)value;
2670 hdr.wFlags = hdr.wCount = 0;
2672 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2673 hdr.wFlags |= HDR_ISREQUEST;
2675 return HTTP_InsertCustomHeader(lpwhr, &hdr);
2679 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2680 lphttpHdr->wFlags |= HDR_ISREQUEST;
2682 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
2684 if (!lphttpHdr->lpszValue && (dwModifier & HTTP_ADDHDR_FLAG_ADD ||
2685 dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW))
2689 if (!lpwhr->StdHeaders[index].lpszField)
2691 lphttpHdr->lpszField = WININET_strdupW(field);
2693 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2694 lphttpHdr->wFlags |= HDR_ISREQUEST;
2697 slen = strlenW(value) + 1;
2698 lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0, slen*sizeof(WCHAR));
2699 if (lphttpHdr->lpszValue)
2701 strcpyW(lphttpHdr->lpszValue, value);
2706 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2709 else if (lphttpHdr->lpszValue)
2711 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE ||
2712 dwModifier & HTTP_ADDHDR_FLAG_ADD)
2713 bSuccess = HTTP_ReplaceHeaderValue( lphttpHdr, value );
2714 else if (dwModifier & COALESCEFLASG)
2719 INT origlen = strlenW(lphttpHdr->lpszValue);
2720 INT valuelen = strlenW(value);
2722 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
2725 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2727 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2730 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2733 len = origlen + valuelen + ((ch > 0) ? 1 : 0);
2735 lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
2738 lphttpHdr->lpszValue = lpsztmp;
2739 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
2742 lphttpHdr->lpszValue[origlen] = ch;
2746 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
2747 lphttpHdr->lpszValue[len] = '\0';
2752 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
2753 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2757 TRACE("<-- %d\n",bSuccess);
2762 /***********************************************************************
2763 * HTTP_CloseConnection (internal)
2765 * Close socket connection
2768 static VOID HTTP_CloseConnection(LPWININETHTTPREQW lpwhr)
2770 LPWININETHTTPSESSIONW lpwhs = NULL;
2771 LPWININETAPPINFOW hIC = NULL;
2773 TRACE("%p\n",lpwhr);
2775 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
2776 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2778 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2779 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2781 if (NETCON_connected(&lpwhr->netConnection))
2783 NETCON_close(&lpwhr->netConnection);
2786 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2787 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2791 /***********************************************************************
2792 * HTTP_CloseHTTPRequestHandle (internal)
2794 * Deallocate request handle
2797 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr)
2800 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
2804 if (NETCON_connected(&lpwhr->netConnection))
2805 HTTP_CloseConnection(lpwhr);
2807 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2808 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
2809 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2811 for (i = 0; i <= HTTP_QUERY_MAX; i++)
2813 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszField);
2814 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszValue);
2817 for (i = 0; i < lpwhr->nCustHeaders; i++)
2819 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
2820 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
2823 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
2824 HeapFree(GetProcessHeap(), 0, lpwhr);
2828 /***********************************************************************
2829 * HTTP_CloseHTTPSessionHandle (internal)
2831 * Deallocate session handle
2834 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr)
2836 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
2838 TRACE("%p\n", lpwhs);
2840 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
2841 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2842 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2843 HeapFree(GetProcessHeap(), 0, lpwhs);
2847 /***********************************************************************
2848 * HTTP_GetCustomHeaderIndex (internal)
2850 * Return index of custom header from header array
2853 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField)
2857 TRACE("%s\n", debugstr_w(lpszField));
2859 for (index = 0; index < lpwhr->nCustHeaders; index++)
2861 if (!strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
2866 if (index >= lpwhr->nCustHeaders)
2869 TRACE("Return: %ld\n", index);
2874 /***********************************************************************
2875 * HTTP_InsertCustomHeader (internal)
2877 * Insert header into array
2880 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
2883 LPHTTPHEADERW lph = NULL;
2886 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
2887 count = lpwhr->nCustHeaders + 1;
2889 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
2891 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
2895 lpwhr->pCustHeaders = lph;
2896 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
2897 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
2898 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
2899 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
2900 lpwhr->nCustHeaders++;
2905 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2912 /***********************************************************************
2913 * HTTP_DeleteCustomHeader (internal)
2915 * Delete header from array
2916 * If this function is called, the indexs may change.
2918 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
2920 if( lpwhr->nCustHeaders <= 0 )
2922 if( index >= lpwhr->nCustHeaders )
2924 lpwhr->nCustHeaders--;
2926 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
2927 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
2928 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
2933 /***********************************************************************
2934 * IsHostInProxyBypassList (@)
2939 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
2941 FIXME("STUB: flags=%ld host=%s length=%ld\n",flags,szHost,length);