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>
35 #ifdef HAVE_ARPA_INET_H
36 # include <arpa/inet.h>
52 #define NO_SHLWAPI_STREAM
53 #define NO_SHLWAPI_REG
54 #define NO_SHLWAPI_STRFCNS
55 #define NO_SHLWAPI_GDI
59 #include "wine/debug.h"
60 #include "wine/unicode.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
64 static const WCHAR g_szHttp1_0[] = {' ','H','T','T','P','/','1','.','0',0 };
65 static const WCHAR g_szHttp1_1[] = {' ','H','T','T','P','/','1','.','1',0 };
66 static const WCHAR g_szReferer[] = {'R','e','f','e','r','e','r',0};
67 static const WCHAR g_szAccept[] = {'A','c','c','e','p','t',0};
68 static const WCHAR g_szUserAgent[] = {'U','s','e','r','-','A','g','e','n','t',0};
69 static const WCHAR szHost[] = { 'H','o','s','t',0 };
70 static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
71 static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 };
73 #define MAXHOSTNAME 100
74 #define MAX_FIELD_VALUE_LEN 256
75 #define MAX_FIELD_LEN 256
77 #define HTTP_REFERER g_szReferer
78 #define HTTP_ACCEPT g_szAccept
79 #define HTTP_USERAGENT g_szUserAgent
81 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
82 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
83 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
84 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
85 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
86 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
87 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
90 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr);
91 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr);
92 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr);
93 static BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr);
94 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
95 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
96 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr);
97 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField, INT index, BOOL Request);
98 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index);
99 static LPWSTR HTTP_build_req( LPCWSTR *list, int len );
100 static BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
101 LPCWSTR username, LPCWSTR password );
102 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD
103 dwInfoLevel, LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD
105 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl,
106 LPCWSTR lpszHeaders, DWORD dwHeaderLength, LPVOID lpOptional, DWORD
107 dwOptionalLength, DWORD dwContentLength);
110 LPHTTPHEADERW HTTP_GetHeader(LPWININETHTTPREQW req, LPCWSTR head)
113 HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
114 if (HeaderIndex == -1)
117 return &req->pCustHeaders[HeaderIndex];
120 /***********************************************************************
121 * HTTP_Tokenize (internal)
123 * Tokenize a string, allocating memory for the tokens.
125 static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
127 LPWSTR * token_array;
132 /* empty string has no tokens */
136 for (i = 0; string[i]; i++)
137 if (!strncmpW(string+i, token_string, strlenW(token_string)))
141 /* we want to skip over separators, but not the null terminator */
142 for (j = 0; j < strlenW(token_string) - 1; j++)
148 /* add 1 for terminating NULL */
149 token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
150 token_array[tokens] = NULL;
153 for (i = 0; i < tokens; i++)
156 next_token = strstrW(string, token_string);
157 if (!next_token) next_token = string+strlenW(string);
158 len = next_token - string;
159 token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
160 memcpy(token_array[i], string, len*sizeof(WCHAR));
161 token_array[i][len] = '\0';
162 string = next_token+strlenW(token_string);
167 /***********************************************************************
168 * HTTP_FreeTokens (internal)
170 * Frees memory returned from HTTP_Tokenize.
172 static void HTTP_FreeTokens(LPWSTR * token_array)
175 for (i = 0; token_array[i]; i++)
176 HeapFree(GetProcessHeap(), 0, token_array[i]);
177 HeapFree(GetProcessHeap(), 0, token_array);
180 /* **********************************************************************
182 * Helper functions for the HttpSendRequest(Ex) functions
185 static void HTTP_FixVerb( LPWININETHTTPREQW lpwhr )
187 /* if the verb is NULL default to GET */
188 if (NULL == lpwhr->lpszVerb)
190 static const WCHAR szGET[] = { 'G','E','T', 0 };
191 lpwhr->lpszVerb = WININET_strdupW(szGET);
195 static void HTTP_FixURL( LPWININETHTTPREQW lpwhr)
197 static const WCHAR szSlash[] = { '/',0 };
198 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
200 /* If we don't have a path we set it to root */
201 if (NULL == lpwhr->lpszPath)
202 lpwhr->lpszPath = WININET_strdupW(szSlash);
203 else /* remove \r and \n*/
205 int nLen = strlenW(lpwhr->lpszPath);
206 while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
209 lpwhr->lpszPath[nLen]='\0';
211 /* Replace '\' with '/' */
214 if (lpwhr->lpszPath[nLen] == '\\') lpwhr->lpszPath[nLen]='/';
218 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
219 lpwhr->lpszPath, strlenW(szHttp), szHttp, strlenW(szHttp) )
220 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
222 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
223 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
225 strcpyW(fixurl + 1, lpwhr->lpszPath);
226 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
227 lpwhr->lpszPath = fixurl;
231 static LPWSTR HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr, LPCWSTR verb, LPCWSTR path, BOOL http1_1 )
233 LPWSTR requestString;
239 static const WCHAR szSpace[] = { ' ',0 };
240 static const WCHAR szcrlf[] = {'\r','\n', 0};
241 static const WCHAR szColon[] = { ':',' ',0 };
242 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
244 /* allocate space for an array of all the string pointers to be added */
245 len = (lpwhr->nCustHeaders)*4 + 9;
246 req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
248 /* add the verb, path and HTTP version string */
253 req[n++] = http1_1 ? g_szHttp1_1 : g_szHttp1_0;
255 /* Append custom request heades */
256 for (i = 0; i < lpwhr->nCustHeaders; i++)
258 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
261 req[n++] = lpwhr->pCustHeaders[i].lpszField;
263 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
265 TRACE("Adding custom header %s (%s)\n",
266 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
267 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
272 ERR("oops. buffer overrun\n");
275 requestString = HTTP_build_req( req, 4 );
276 HeapFree( GetProcessHeap(), 0, req );
279 * Set (header) termination string for request
280 * Make sure there's exactly two new lines at the end of the request
282 p = &requestString[strlenW(requestString)-1];
283 while ( (*p == '\n') || (*p == '\r') )
285 strcpyW( p+1, sztwocrlf );
287 return requestString;
290 static void HTTP_ProcessHeaders( LPWININETHTTPREQW lpwhr )
292 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
294 LPHTTPHEADERW setCookieHeader;
296 HeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSet_Cookie, 0, FALSE);
297 if (HeaderIndex == -1)
299 setCookieHeader = &lpwhr->pCustHeaders[HeaderIndex];
301 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && setCookieHeader->lpszValue)
303 int nPosStart = 0, nPosEnd = 0, len;
304 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
306 while (setCookieHeader->lpszValue[nPosEnd] != '\0')
308 LPWSTR buf_cookie, cookie_name, cookie_data;
310 LPWSTR domain = NULL;
314 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
315 setCookieHeader->lpszValue[nPosEnd] != '\0')
319 if (setCookieHeader->lpszValue[nPosEnd] == ';')
321 /* fixme: not case sensitive, strcasestr is gnu only */
322 int nDomainPosEnd = 0;
323 int nDomainPosStart = 0, nDomainLength = 0;
324 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
325 LPWSTR lpszDomain = strstrW(&setCookieHeader->lpszValue[nPosEnd], szDomain);
327 { /* they have specified their own domain, lets use it */
328 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
329 lpszDomain[nDomainPosEnd] != '\0')
333 nDomainPosStart = strlenW(szDomain);
334 nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
335 domain = HeapAlloc(GetProcessHeap(), 0, (nDomainLength + 1)*sizeof(WCHAR));
336 lstrcpynW(domain, &lpszDomain[nDomainPosStart], nDomainLength + 1);
339 if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
340 buf_cookie = HeapAlloc(GetProcessHeap(), 0, ((nPosEnd - nPosStart) + 1)*sizeof(WCHAR));
341 lstrcpynW(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart) + 1);
342 TRACE("%s\n", debugstr_w(buf_cookie));
343 while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
347 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
349 HeapFree(GetProcessHeap(), 0, buf_cookie);
353 cookie_name = HeapAlloc(GetProcessHeap(), 0, (nEqualPos + 1)*sizeof(WCHAR));
354 lstrcpynW(cookie_name, buf_cookie, nEqualPos + 1);
355 cookie_data = &buf_cookie[nEqualPos + 1];
357 Host = HTTP_GetHeader(lpwhr,szHost);
358 len = lstrlenW((domain ? domain : (Host?Host->lpszValue:NULL))) +
359 strlenW(lpwhr->lpszPath) + 9;
360 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
361 sprintfW(buf_url, szFmt, (domain ? domain : (Host?Host->lpszValue:NULL))); /* FIXME PATH!!! */
362 InternetSetCookieW(buf_url, cookie_name, cookie_data);
364 HeapFree(GetProcessHeap(), 0, buf_url);
365 HeapFree(GetProcessHeap(), 0, buf_cookie);
366 HeapFree(GetProcessHeap(), 0, cookie_name);
367 HeapFree(GetProcessHeap(), 0, domain);
373 static void HTTP_AddProxyInfo( LPWININETHTTPREQW lpwhr )
375 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
376 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW)lpwhs->hdr.lpwhparent;
378 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
379 assert(hIC->hdr.htype == WH_HINIT);
381 if (hIC && (hIC->lpszProxyUsername || hIC->lpszProxyPassword ))
382 HTTP_InsertProxyAuthorization(lpwhr, hIC->lpszProxyUsername,
383 hIC->lpszProxyPassword);
386 /***********************************************************************
387 * HTTP_HttpAddRequestHeadersW (internal)
389 static BOOL WINAPI HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
390 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
395 BOOL bSuccess = FALSE;
398 TRACE("copying header: %s\n", debugstr_w(lpszHeader));
400 if( dwHeaderLength == ~0U )
401 len = strlenW(lpszHeader);
403 len = dwHeaderLength;
404 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
405 lstrcpynW( buffer, lpszHeader, len + 1);
411 LPWSTR * pFieldAndValue;
415 while (*lpszEnd != '\0')
417 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
422 if (*lpszStart == '\0')
425 if (*lpszEnd == '\r')
428 lpszEnd += 2; /* Jump over \r\n */
430 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
431 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
434 bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
435 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
436 HTTP_FreeTokens(pFieldAndValue);
442 HeapFree(GetProcessHeap(), 0, buffer);
447 /***********************************************************************
448 * HttpAddRequestHeadersW (WININET.@)
450 * Adds one or more HTTP header to the request handler
457 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
458 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
460 BOOL bSuccess = FALSE;
461 LPWININETHTTPREQW lpwhr;
463 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_w(lpszHeader), dwHeaderLength,
469 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
470 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
472 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
475 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
478 WININET_Release( &lpwhr->hdr );
483 /***********************************************************************
484 * HttpAddRequestHeadersA (WININET.@)
486 * Adds one or more HTTP header to the request handler
493 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
494 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
500 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_a(lpszHeader), dwHeaderLength,
503 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
504 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
505 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
506 if( dwHeaderLength != ~0U )
507 dwHeaderLength = len;
509 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
511 HeapFree( GetProcessHeap(), 0, hdr );
516 /***********************************************************************
517 * HttpEndRequestA (WININET.@)
519 * Ends an HTTP request that was started by HttpSendRequestEx
526 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
527 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD dwContext)
529 LPINTERNET_BUFFERSA ptr;
530 LPINTERNET_BUFFERSW lpBuffersOutW,ptrW;
533 TRACE("(%p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersOut, dwFlags,
538 lpBuffersOutW = (LPINTERNET_BUFFERSW)HeapAlloc(GetProcessHeap(),
539 HEAP_ZERO_MEMORY, sizeof(INTERNET_BUFFERSW));
541 lpBuffersOutW = NULL;
543 ptrW = lpBuffersOutW;
546 if (ptr->lpvBuffer && ptr->dwBufferLength)
547 ptrW->lpvBuffer = HeapAlloc(GetProcessHeap(),0,ptr->dwBufferLength);
548 ptrW->dwBufferLength = ptr->dwBufferLength;
549 ptrW->dwBufferTotal= ptr->dwBufferTotal;
552 ptrW->Next = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
553 sizeof(INTERNET_BUFFERSW));
559 rc = HttpEndRequestW(hRequest, lpBuffersOutW, dwFlags, dwContext);
563 ptrW = lpBuffersOutW;
566 LPINTERNET_BUFFERSW ptrW2;
568 FIXME("Do we need to translate info out of these buffer?\n");
570 HeapFree(GetProcessHeap(),0,(LPVOID)ptrW->lpvBuffer);
572 HeapFree(GetProcessHeap(),0,ptrW);
580 /***********************************************************************
581 * HttpEndRequestW (WININET.@)
583 * Ends an HTTP request that was started by HttpSendRequestEx
590 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
591 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD dwContext)
594 LPWININETHTTPREQW lpwhr;
598 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
600 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
602 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
606 lpwhr->hdr.dwFlags |= dwFlags;
607 lpwhr->hdr.dwContext = dwContext;
609 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
610 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
612 responseLen = HTTP_GetResponseHeaders(lpwhr);
616 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
617 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
619 /* process headers here. Is this right? */
620 HTTP_ProcessHeaders(lpwhr);
622 /* We appear to do nothing with the buffer.. is that correct? */
624 if(!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
626 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
627 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
628 (dwCode==302 || dwCode==301))
630 WCHAR szNewLocation[2048];
631 DWORD dwBufferSize=2048;
633 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
635 static const WCHAR szGET[] = { 'G','E','T', 0 };
636 /* redirects are always GETs */
637 HeapFree(GetProcessHeap(),0,lpwhr->lpszVerb);
638 lpwhr->lpszVerb = WININET_strdupW(szGET);
639 return HTTP_HandleRedirect(lpwhr, szNewLocation, NULL, 0, NULL, 0, 0);
644 TRACE("%i <--\n",rc);
648 /***********************************************************************
649 * HttpOpenRequestW (WININET.@)
651 * Open a HTTP request handle
654 * HINTERNET a HTTP request handle on success
658 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
659 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
660 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
661 DWORD dwFlags, DWORD dwContext)
663 LPWININETHTTPSESSIONW lpwhs;
664 HINTERNET handle = NULL;
666 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
667 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
668 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
670 if(lpszAcceptTypes!=NULL)
673 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
674 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
677 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
678 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
680 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
685 * My tests seem to show that the windows version does not
686 * become asynchronous until after this point. And anyhow
687 * if this call was asynchronous then how would you get the
688 * necessary HINTERNET pointer returned by this function.
691 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
692 lpszVersion, lpszReferrer, lpszAcceptTypes,
696 WININET_Release( &lpwhs->hdr );
697 TRACE("returning %p\n", handle);
702 /***********************************************************************
703 * HttpOpenRequestA (WININET.@)
705 * Open a HTTP request handle
708 * HINTERNET a HTTP request handle on success
712 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
713 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
714 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
715 DWORD dwFlags, DWORD dwContext)
717 LPWSTR szVerb = NULL, szObjectName = NULL;
718 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
720 INT acceptTypesCount;
721 HINTERNET rc = FALSE;
722 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
723 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
724 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
729 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
730 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
733 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
738 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
739 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
742 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
747 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
748 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
751 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
756 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
757 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
760 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
763 acceptTypesCount = 0;
766 /* find out how many there are */
767 while (lpszAcceptTypes[acceptTypesCount])
769 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
770 acceptTypesCount = 0;
771 while (lpszAcceptTypes[acceptTypesCount])
773 len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
775 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
776 if (!szAcceptTypes[acceptTypesCount] )
778 MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
779 -1, szAcceptTypes[acceptTypesCount], len );
782 szAcceptTypes[acceptTypesCount] = NULL;
784 else szAcceptTypes = 0;
786 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
787 szVersion, szReferrer,
788 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
793 acceptTypesCount = 0;
794 while (szAcceptTypes[acceptTypesCount])
796 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
799 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
801 HeapFree(GetProcessHeap(), 0, szReferrer);
802 HeapFree(GetProcessHeap(), 0, szVersion);
803 HeapFree(GetProcessHeap(), 0, szObjectName);
804 HeapFree(GetProcessHeap(), 0, szVerb);
809 /***********************************************************************
812 static UINT HTTP_Base64( LPCWSTR bin, LPWSTR base64 )
815 static LPCSTR HTTP_Base64Enc =
816 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
820 /* first 6 bits, all from bin[0] */
821 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
822 x = (bin[0] & 3) << 4;
824 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
827 base64[n++] = HTTP_Base64Enc[x];
832 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
833 x = ( bin[1] & 0x0f ) << 2;
835 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
838 base64[n++] = HTTP_Base64Enc[x];
842 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
844 /* last 6 bits, all from bin [2] */
845 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
852 /***********************************************************************
853 * HTTP_EncodeBasicAuth
855 * Encode the basic authentication string for HTTP 1.1
857 static LPWSTR HTTP_EncodeBasicAuth( LPCWSTR username, LPCWSTR password)
861 static const WCHAR szBasic[] = {'B','a','s','i','c',' ',0};
862 static const WCHAR szColon[] = {':',0};
864 len = lstrlenW( username ) + 1 + lstrlenW ( password ) + 1;
865 in = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
869 len = lstrlenW(szBasic) +
870 (lstrlenW( username ) + 1 + lstrlenW ( password ))*2 + 1 + 1;
871 out = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
874 lstrcpyW( in, username );
875 lstrcatW( in, szColon );
876 lstrcatW( in, password );
877 lstrcpyW( out, szBasic );
878 HTTP_Base64( in, &out[strlenW(out)] );
880 HeapFree( GetProcessHeap(), 0, in );
885 /***********************************************************************
886 * HTTP_InsertProxyAuthorization
888 * Insert the basic authorization field in the request header
890 static BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
891 LPCWSTR username, LPCWSTR password )
893 WCHAR *authorization = HTTP_EncodeBasicAuth( username, password );
899 TRACE( "Inserting authorization: %s\n", debugstr_w( authorization ) );
901 HTTP_ProcessHeader(lpwhr, szProxy_Authorization, authorization,
902 HTTP_ADDHDR_FLAG_REPLACE);
904 HeapFree( GetProcessHeap(), 0, authorization );
909 /***********************************************************************
912 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
913 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
915 WCHAR buf[MAXHOSTNAME];
916 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
918 static const WCHAR szNul[] = { 0 };
919 URL_COMPONENTSW UrlComponents;
920 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }, szSlash[] = { '/',0 } ;
921 static const WCHAR szFormat1[] = { 'h','t','t','p',':','/','/','%','s',0 };
922 static const WCHAR szFormat2[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
925 memset( &UrlComponents, 0, sizeof UrlComponents );
926 UrlComponents.dwStructSize = sizeof UrlComponents;
927 UrlComponents.lpszHostName = buf;
928 UrlComponents.dwHostNameLength = MAXHOSTNAME;
930 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
931 hIC->lpszProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
932 sprintfW(proxy, szFormat1, hIC->lpszProxy);
934 strcpyW(proxy, hIC->lpszProxy);
935 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
937 if( UrlComponents.dwHostNameLength == 0 )
940 if( !lpwhr->lpszPath )
941 lpwhr->lpszPath = (LPWSTR)szNul;
942 TRACE("server='%s' path='%s'\n",
943 debugstr_w(lpwhs->lpszHostName), debugstr_w(lpwhr->lpszPath));
944 /* for constant 15 see above */
945 len = strlenW(lpwhs->lpszHostName) + strlenW(lpwhr->lpszPath) + 15;
946 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
948 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
949 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
951 sprintfW(url, szFormat2, lpwhs->lpszHostName, lpwhs->nHostPort);
953 if( lpwhr->lpszPath[0] != '/' )
954 strcatW( url, szSlash );
955 strcatW(url, lpwhr->lpszPath);
956 if(lpwhr->lpszPath != szNul)
957 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
958 lpwhr->lpszPath = url;
960 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
961 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
962 lpwhs->nServerPort = UrlComponents.nPort;
967 /***********************************************************************
968 * HTTP_HttpOpenRequestW (internal)
970 * Open a HTTP request handle
973 * HINTERNET a HTTP request handle on success
977 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
978 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
979 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
980 DWORD dwFlags, DWORD dwContext)
982 LPWININETAPPINFOW hIC = NULL;
983 LPWININETHTTPREQW lpwhr;
985 LPWSTR lpszUrl = NULL;
987 HINTERNET handle = NULL;
988 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
995 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
996 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
998 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
1001 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1004 lpwhr->hdr.htype = WH_HHTTPREQ;
1005 lpwhr->hdr.lpwhparent = WININET_AddRef( &lpwhs->hdr );
1006 lpwhr->hdr.dwFlags = dwFlags;
1007 lpwhr->hdr.dwContext = dwContext;
1008 lpwhr->hdr.dwRefCount = 1;
1009 lpwhr->hdr.destroy = HTTP_CloseHTTPRequestHandle;
1010 lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
1012 handle = WININET_AllocHandle( &lpwhr->hdr );
1015 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1019 if (!NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE))
1021 InternetCloseHandle( handle );
1026 if (NULL != lpszObjectName && strlenW(lpszObjectName)) {
1030 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
1031 if (rc != E_POINTER)
1032 len = strlenW(lpszObjectName)+1;
1033 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1034 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
1035 URL_ESCAPE_SPACES_ONLY);
1038 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(lpszObjectName),rc);
1039 strcpyW(lpwhr->lpszPath,lpszObjectName);
1043 if (NULL != lpszReferrer && strlenW(lpszReferrer))
1044 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDHDR_FLAG_COALESCE);
1046 if(lpszAcceptTypes!=NULL)
1049 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
1050 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i], HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_REQ|HTTP_ADDHDR_FLAG_ADD_IF_NEW);
1053 if (NULL == lpszVerb)
1055 static const WCHAR szGet[] = {'G','E','T',0};
1056 lpwhr->lpszVerb = WININET_strdupW(szGet);
1058 else if (strlenW(lpszVerb))
1059 lpwhr->lpszVerb = WININET_strdupW(lpszVerb);
1061 if (NULL != lpszReferrer && strlenW(lpszReferrer))
1063 WCHAR buf[MAXHOSTNAME];
1064 URL_COMPONENTSW UrlComponents;
1066 memset( &UrlComponents, 0, sizeof UrlComponents );
1067 UrlComponents.dwStructSize = sizeof UrlComponents;
1068 UrlComponents.lpszHostName = buf;
1069 UrlComponents.dwHostNameLength = MAXHOSTNAME;
1071 InternetCrackUrlW(lpszReferrer, 0, 0, &UrlComponents);
1072 if (strlenW(UrlComponents.lpszHostName))
1073 HTTP_ProcessHeader(lpwhr, szHost, UrlComponents.lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1076 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1078 if (lpwhs->nServerPort == INTERNET_INVALID_PORT_NUMBER)
1079 lpwhs->nServerPort = (dwFlags & INTERNET_FLAG_SECURE ?
1080 INTERNET_DEFAULT_HTTPS_PORT :
1081 INTERNET_DEFAULT_HTTP_PORT);
1082 lpwhs->nHostPort = lpwhs->nServerPort;
1084 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1085 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
1089 WCHAR *agent_header;
1090 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
1092 len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
1093 agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1094 sprintfW(agent_header, user_agent, hIC->lpszAgent );
1096 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
1097 HTTP_ADDREQ_FLAG_ADD);
1098 HeapFree(GetProcessHeap(), 0, agent_header);
1101 Host = HTTP_GetHeader(lpwhr,szHost);
1103 len = lstrlenW(Host->lpszValue) + strlenW(szUrlForm);
1104 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1105 sprintfW( lpszUrl, szUrlForm, Host->lpszValue );
1107 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
1108 InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
1111 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
1112 static const WCHAR szcrlf[] = {'\r','\n',0};
1114 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
1116 cnt += sprintfW(lpszCookies, szCookie);
1117 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
1118 strcatW(lpszCookies, szcrlf);
1120 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
1121 HTTP_ADDREQ_FLAG_ADD);
1122 HeapFree(GetProcessHeap(), 0, lpszCookies);
1124 HeapFree(GetProcessHeap(), 0, lpszUrl);
1127 INTERNET_SendCallback(&lpwhs->hdr, dwContext,
1128 INTERNET_STATUS_HANDLE_CREATED, &handle,
1132 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
1136 * According to my tests. The name is not resolved until a request is Opened
1138 INTERNET_SendCallback(&lpwhr->hdr, dwContext,
1139 INTERNET_STATUS_RESOLVING_NAME,
1140 lpwhs->lpszServerName,
1141 strlenW(lpwhs->lpszServerName)+1);
1143 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1144 &lpwhs->socketAddress))
1146 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1147 InternetCloseHandle( handle );
1152 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
1153 szaddr, sizeof(szaddr));
1154 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1155 INTERNET_STATUS_NAME_RESOLVED,
1156 szaddr, strlen(szaddr)+1);
1160 WININET_Release( &lpwhr->hdr );
1162 TRACE("<-- %p (%p)\n", handle, lpwhr);
1166 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
1167 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
1168 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
1169 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
1170 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
1171 static const WCHAR szAge[] = { 'A','g','e',0 };
1172 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
1173 static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
1174 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
1175 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
1176 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
1177 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
1178 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
1179 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
1180 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
1181 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
1182 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
1183 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
1184 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 };
1185 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
1186 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
1187 static const WCHAR szDate[] = { 'D','a','t','e',0 };
1188 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
1189 static const WCHAR szETag[] = { 'E','T','a','g',0 };
1190 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
1191 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
1192 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
1193 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1194 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
1195 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
1196 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1197 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
1198 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
1199 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
1200 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
1201 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
1202 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1203 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
1204 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
1205 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
1206 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
1207 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
1208 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
1209 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
1210 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
1211 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 };
1212 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
1213 static const WCHAR szURI[] = { 'U','R','I',0 };
1214 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
1215 static const WCHAR szVary[] = { 'V','a','r','y',0 };
1216 static const WCHAR szVia[] = { 'V','i','a',0 };
1217 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
1218 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1220 static const LPCWSTR header_lookup[] = {
1221 szMime_Version, /* HTTP_QUERY_MIME_VERSION = 0 */
1222 szContent_Type, /* HTTP_QUERY_CONTENT_TYPE = 1 */
1223 szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
1224 szContent_ID, /* HTTP_QUERY_CONTENT_ID = 3 */
1225 NULL, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
1226 szContent_Length, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
1227 szContent_Language, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
1228 szAllow, /* HTTP_QUERY_ALLOW = 7 */
1229 szPublic, /* HTTP_QUERY_PUBLIC = 8 */
1230 szDate, /* HTTP_QUERY_DATE = 9 */
1231 szExpires, /* HTTP_QUERY_EXPIRES = 10 */
1232 szLast_Modified, /* HTTP_QUERY_LAST_MODIFIED = 11 */
1233 NULL, /* HTTP_QUERY_MESSAGE_ID = 12 */
1234 szURI, /* HTTP_QUERY_URI = 13 */
1235 szFrom, /* HTTP_QUERY_DERIVED_FROM = 14 */
1236 NULL, /* HTTP_QUERY_COST = 15 */
1237 NULL, /* HTTP_QUERY_LINK = 16 */
1238 szPragma, /* HTTP_QUERY_PRAGMA = 17 */
1239 NULL, /* HTTP_QUERY_VERSION = 18 */
1240 szStatus, /* HTTP_QUERY_STATUS_CODE = 19 */
1241 NULL, /* HTTP_QUERY_STATUS_TEXT = 20 */
1242 NULL, /* HTTP_QUERY_RAW_HEADERS = 21 */
1243 NULL, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
1244 szConnection, /* HTTP_QUERY_CONNECTION = 23 */
1245 szAccept, /* HTTP_QUERY_ACCEPT = 24 */
1246 szAccept_Charset, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
1247 szAccept_Encoding, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
1248 szAccept_Language, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
1249 szAuthorization, /* HTTP_QUERY_AUTHORIZATION = 28 */
1250 szContent_Encoding, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
1251 NULL, /* HTTP_QUERY_FORWARDED = 30 */
1252 NULL, /* HTTP_QUERY_FROM = 31 */
1253 szIf_Modified_Since, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
1254 szLocation, /* HTTP_QUERY_LOCATION = 33 */
1255 NULL, /* HTTP_QUERY_ORIG_URI = 34 */
1256 szReferer, /* HTTP_QUERY_REFERER = 35 */
1257 szRetry_After, /* HTTP_QUERY_RETRY_AFTER = 36 */
1258 szServer, /* HTTP_QUERY_SERVER = 37 */
1259 NULL, /* HTTP_TITLE = 38 */
1260 szUser_Agent, /* HTTP_QUERY_USER_AGENT = 39 */
1261 szWWW_Authenticate, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
1262 szProxy_Authenticate, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
1263 szAccept_Ranges, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
1264 szSet_Cookie, /* HTTP_QUERY_SET_COOKIE = 43 */
1265 szCookie, /* HTTP_QUERY_COOKIE = 44 */
1266 NULL, /* HTTP_QUERY_REQUEST_METHOD = 45 */
1267 NULL, /* HTTP_QUERY_REFRESH = 46 */
1268 NULL, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
1269 szAge, /* HTTP_QUERY_AGE = 48 */
1270 szCache_Control, /* HTTP_QUERY_CACHE_CONTROL = 49 */
1271 szContent_Base, /* HTTP_QUERY_CONTENT_BASE = 50 */
1272 szContent_Location, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
1273 szContent_MD5, /* HTTP_QUERY_CONTENT_MD5 = 52 */
1274 szContent_Range, /* HTTP_QUERY_CONTENT_RANGE = 53 */
1275 szETag, /* HTTP_QUERY_ETAG = 54 */
1276 szHost, /* HTTP_QUERY_HOST = 55 */
1277 szIf_Match, /* HTTP_QUERY_IF_MATCH = 56 */
1278 szIf_None_Match, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
1279 szIf_Range, /* HTTP_QUERY_IF_RANGE = 58 */
1280 szIf_Unmodified_Since, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
1281 szMax_Forwards, /* HTTP_QUERY_MAX_FORWARDS = 60 */
1282 szProxy_Authorization, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
1283 szRange, /* HTTP_QUERY_RANGE = 62 */
1284 szTransfer_Encoding, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
1285 szUpgrade, /* HTTP_QUERY_UPGRADE = 64 */
1286 szVary, /* HTTP_QUERY_VARY = 65 */
1287 szVia, /* HTTP_QUERY_VIA = 66 */
1288 szWarning, /* HTTP_QUERY_WARNING = 67 */
1289 szExpect, /* HTTP_QUERY_EXPECT = 68 */
1290 szProxy_Connection, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
1291 szUnless_Modified_Since, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
1294 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
1296 /***********************************************************************
1297 * HTTP_HttpQueryInfoW (internal)
1299 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
1300 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1302 LPHTTPHEADERW lphttpHdr = NULL;
1303 BOOL bSuccess = FALSE;
1304 BOOL request_only = dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS;
1305 INT requested_index = lpdwIndex ? *lpdwIndex : 0;
1306 INT level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
1309 /* Find requested header structure */
1312 case HTTP_QUERY_CUSTOM:
1313 index = HTTP_GetCustomHeaderIndex(lpwhr, lpBuffer, requested_index, request_only);
1316 case HTTP_QUERY_RAW_HEADERS_CRLF:
1318 DWORD len = strlenW(lpwhr->lpszRawHeaders);
1319 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1321 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1322 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1325 memcpy(lpBuffer, lpwhr->lpszRawHeaders, (len+1)*sizeof(WCHAR));
1326 *lpdwBufferLength = len * sizeof(WCHAR);
1328 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1332 case HTTP_QUERY_RAW_HEADERS:
1334 static const WCHAR szCrLf[] = {'\r','\n',0};
1335 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
1337 LPWSTR pszString = (WCHAR*)lpBuffer;
1339 for (i = 0; ppszRawHeaderLines[i]; i++)
1340 size += strlenW(ppszRawHeaderLines[i]) + 1;
1342 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
1344 HTTP_FreeTokens(ppszRawHeaderLines);
1345 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
1346 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1350 for (i = 0; ppszRawHeaderLines[i]; i++)
1352 DWORD len = strlenW(ppszRawHeaderLines[i]);
1353 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
1358 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
1360 *lpdwBufferLength = size * sizeof(WCHAR);
1361 HTTP_FreeTokens(ppszRawHeaderLines);
1365 case HTTP_QUERY_STATUS_TEXT:
1367 DWORD len = strlenW(lpwhr->lpszStatusText);
1368 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1370 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1371 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1374 memcpy(lpBuffer, lpwhr->lpszStatusText, (len+1)*sizeof(WCHAR));
1375 *lpdwBufferLength = len * sizeof(WCHAR);
1377 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1381 case HTTP_QUERY_VERSION:
1383 DWORD len = strlenW(lpwhr->lpszVersion);
1384 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1386 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1387 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1390 memcpy(lpBuffer, lpwhr->lpszVersion, (len+1)*sizeof(WCHAR));
1391 *lpdwBufferLength = len * sizeof(WCHAR);
1393 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1398 assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
1400 if (level >= 0 && level < LAST_TABLE_HEADER && header_lookup[level])
1401 index = HTTP_GetCustomHeaderIndex(lpwhr, header_lookup[level],
1402 requested_index,request_only);
1406 lphttpHdr = &lpwhr->pCustHeaders[index];
1408 /* Ensure header satisifies requested attributes */
1410 ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
1411 (~lphttpHdr->wFlags & HDR_ISREQUEST)))
1413 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1420 /* coalesce value to reuqested type */
1421 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
1423 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
1426 TRACE(" returning number : %d\n", *(int *)lpBuffer);
1428 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
1434 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
1436 tmpTM = *gmtime(&tmpTime);
1437 STHook = (SYSTEMTIME *) lpBuffer;
1441 STHook->wDay = tmpTM.tm_mday;
1442 STHook->wHour = tmpTM.tm_hour;
1443 STHook->wMilliseconds = 0;
1444 STHook->wMinute = tmpTM.tm_min;
1445 STHook->wDayOfWeek = tmpTM.tm_wday;
1446 STHook->wMonth = tmpTM.tm_mon + 1;
1447 STHook->wSecond = tmpTM.tm_sec;
1448 STHook->wYear = tmpTM.tm_year;
1452 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
1453 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
1454 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
1456 else if (dwInfoLevel & HTTP_QUERY_FLAG_COALESCE)
1458 if (*lpdwIndex >= lphttpHdr->wCount)
1460 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1464 /* Copy strncpyW(lpBuffer, lphttpHdr[*lpdwIndex], len); */
1468 else if (lphttpHdr->lpszValue)
1470 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
1472 if (len > *lpdwBufferLength)
1474 *lpdwBufferLength = len;
1475 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1479 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
1480 *lpdwBufferLength = len - sizeof(WCHAR);
1483 TRACE(" returning string : '%s'\n", debugstr_w(lpBuffer));
1488 /***********************************************************************
1489 * HttpQueryInfoW (WININET.@)
1491 * Queries for information about an HTTP request
1498 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1499 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1501 BOOL bSuccess = FALSE;
1502 LPWININETHTTPREQW lpwhr;
1504 if (TRACE_ON(wininet)) {
1505 #define FE(x) { x, #x }
1506 static const wininet_flag_info query_flags[] = {
1507 FE(HTTP_QUERY_MIME_VERSION),
1508 FE(HTTP_QUERY_CONTENT_TYPE),
1509 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
1510 FE(HTTP_QUERY_CONTENT_ID),
1511 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
1512 FE(HTTP_QUERY_CONTENT_LENGTH),
1513 FE(HTTP_QUERY_CONTENT_LANGUAGE),
1514 FE(HTTP_QUERY_ALLOW),
1515 FE(HTTP_QUERY_PUBLIC),
1516 FE(HTTP_QUERY_DATE),
1517 FE(HTTP_QUERY_EXPIRES),
1518 FE(HTTP_QUERY_LAST_MODIFIED),
1519 FE(HTTP_QUERY_MESSAGE_ID),
1521 FE(HTTP_QUERY_DERIVED_FROM),
1522 FE(HTTP_QUERY_COST),
1523 FE(HTTP_QUERY_LINK),
1524 FE(HTTP_QUERY_PRAGMA),
1525 FE(HTTP_QUERY_VERSION),
1526 FE(HTTP_QUERY_STATUS_CODE),
1527 FE(HTTP_QUERY_STATUS_TEXT),
1528 FE(HTTP_QUERY_RAW_HEADERS),
1529 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
1530 FE(HTTP_QUERY_CONNECTION),
1531 FE(HTTP_QUERY_ACCEPT),
1532 FE(HTTP_QUERY_ACCEPT_CHARSET),
1533 FE(HTTP_QUERY_ACCEPT_ENCODING),
1534 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
1535 FE(HTTP_QUERY_AUTHORIZATION),
1536 FE(HTTP_QUERY_CONTENT_ENCODING),
1537 FE(HTTP_QUERY_FORWARDED),
1538 FE(HTTP_QUERY_FROM),
1539 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
1540 FE(HTTP_QUERY_LOCATION),
1541 FE(HTTP_QUERY_ORIG_URI),
1542 FE(HTTP_QUERY_REFERER),
1543 FE(HTTP_QUERY_RETRY_AFTER),
1544 FE(HTTP_QUERY_SERVER),
1545 FE(HTTP_QUERY_TITLE),
1546 FE(HTTP_QUERY_USER_AGENT),
1547 FE(HTTP_QUERY_WWW_AUTHENTICATE),
1548 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
1549 FE(HTTP_QUERY_ACCEPT_RANGES),
1550 FE(HTTP_QUERY_SET_COOKIE),
1551 FE(HTTP_QUERY_COOKIE),
1552 FE(HTTP_QUERY_REQUEST_METHOD),
1553 FE(HTTP_QUERY_REFRESH),
1554 FE(HTTP_QUERY_CONTENT_DISPOSITION),
1556 FE(HTTP_QUERY_CACHE_CONTROL),
1557 FE(HTTP_QUERY_CONTENT_BASE),
1558 FE(HTTP_QUERY_CONTENT_LOCATION),
1559 FE(HTTP_QUERY_CONTENT_MD5),
1560 FE(HTTP_QUERY_CONTENT_RANGE),
1561 FE(HTTP_QUERY_ETAG),
1562 FE(HTTP_QUERY_HOST),
1563 FE(HTTP_QUERY_IF_MATCH),
1564 FE(HTTP_QUERY_IF_NONE_MATCH),
1565 FE(HTTP_QUERY_IF_RANGE),
1566 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
1567 FE(HTTP_QUERY_MAX_FORWARDS),
1568 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
1569 FE(HTTP_QUERY_RANGE),
1570 FE(HTTP_QUERY_TRANSFER_ENCODING),
1571 FE(HTTP_QUERY_UPGRADE),
1572 FE(HTTP_QUERY_VARY),
1574 FE(HTTP_QUERY_WARNING),
1575 FE(HTTP_QUERY_CUSTOM)
1577 static const wininet_flag_info modifier_flags[] = {
1578 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
1579 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
1580 FE(HTTP_QUERY_FLAG_NUMBER),
1581 FE(HTTP_QUERY_FLAG_COALESCE)
1584 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
1585 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
1588 TRACE("(%p, 0x%08lx)--> %ld\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
1589 TRACE(" Attribute:");
1590 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
1591 if (query_flags[i].val == info) {
1592 TRACE(" %s", query_flags[i].name);
1596 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
1597 TRACE(" Unknown (%08lx)", info);
1600 TRACE(" Modifier:");
1601 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
1602 if (modifier_flags[i].val & info_mod) {
1603 TRACE(" %s", modifier_flags[i].name);
1604 info_mod &= ~ modifier_flags[i].val;
1609 TRACE(" Unknown (%08lx)", info_mod);
1614 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1615 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1617 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1621 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
1622 lpBuffer, lpdwBufferLength, lpdwIndex);
1626 WININET_Release( &lpwhr->hdr );
1628 TRACE("%d <--\n", bSuccess);
1632 /***********************************************************************
1633 * HttpQueryInfoA (WININET.@)
1635 * Queries for information about an HTTP request
1642 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1643 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1649 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
1650 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
1652 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
1653 lpdwBufferLength, lpdwIndex );
1656 len = (*lpdwBufferLength)*sizeof(WCHAR);
1657 bufferW = HeapAlloc( GetProcessHeap(), 0, len );
1658 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
1659 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
1660 MultiByteToWideChar(CP_ACP,0,lpBuffer,-1,bufferW,len);
1661 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
1665 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
1666 lpBuffer, *lpdwBufferLength, NULL, NULL );
1667 *lpdwBufferLength = len - 1;
1669 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
1672 /* since the strings being returned from HttpQueryInfoW should be
1673 * only ASCII characters, it is reasonable to assume that all of
1674 * the Unicode characters can be reduced to a single byte */
1675 *lpdwBufferLength = len / sizeof(WCHAR);
1677 HeapFree(GetProcessHeap(), 0, bufferW );
1682 /***********************************************************************
1683 * HttpSendRequestExA (WININET.@)
1685 * Sends the specified request to the HTTP server and allows chunked
1690 * Failure: FALSE, call GetLastError() for more information.
1692 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
1693 LPINTERNET_BUFFERSA lpBuffersIn,
1694 LPINTERNET_BUFFERSA lpBuffersOut,
1695 DWORD dwFlags, DWORD dwContext)
1697 INTERNET_BUFFERSW BuffersInW;
1701 TRACE("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersIn,
1702 lpBuffersOut, dwFlags, dwContext);
1706 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
1707 if (lpBuffersIn->lpcszHeader)
1709 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
1710 lpBuffersIn->dwHeadersLength,0,0);
1711 BuffersInW.lpcszHeader = HeapAlloc(GetProcessHeap(),0,headerlen*
1713 if (!BuffersInW.lpcszHeader)
1715 SetLastError(ERROR_OUTOFMEMORY);
1718 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
1719 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
1720 (LPWSTR)BuffersInW.lpcszHeader, headerlen);
1723 BuffersInW.lpcszHeader = NULL;
1724 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
1725 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
1726 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
1727 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
1728 BuffersInW.Next = NULL;
1731 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
1734 HeapFree(GetProcessHeap(),0,(LPVOID)BuffersInW.lpcszHeader);
1739 /***********************************************************************
1740 * HttpSendRequestExW (WININET.@)
1742 * Sends the specified request to the HTTP server and allows chunked
1747 * Failure: FALSE, call GetLastError() for more information.
1749 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
1750 LPINTERNET_BUFFERSW lpBuffersIn,
1751 LPINTERNET_BUFFERSW lpBuffersOut,
1752 DWORD dwFlags, DWORD dwContext)
1755 LPWININETHTTPREQW lpwhr;
1756 LPWININETHTTPSESSIONW lpwhs;
1757 LPWININETAPPINFOW hIC;
1759 TRACE("(%p, %p, %p, %08lx, %08lx)\n", hRequest, lpBuffersIn,
1760 lpBuffersOut, dwFlags, dwContext);
1762 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
1764 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1766 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1770 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1771 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
1772 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1773 assert(hIC->hdr.htype == WH_HINIT);
1775 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1777 WORKREQUEST workRequest;
1778 struct WORKREQ_HTTPSENDREQUESTW *req;
1780 workRequest.asyncall = HTTPSENDREQUESTW;
1781 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
1782 req = &workRequest.u.HttpSendRequestW;
1785 if (lpBuffersIn->lpcszHeader)
1786 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
1787 req->lpszHeader = WININET_strdupW(lpBuffersIn->lpcszHeader);
1789 req->lpszHeader = NULL;
1790 req->dwHeaderLength = lpBuffersIn->dwHeadersLength;
1791 req->lpOptional = lpBuffersIn->lpvBuffer;
1792 req->dwOptionalLength = lpBuffersIn->dwBufferLength;
1793 req->dwContentLength = lpBuffersIn->dwBufferTotal;
1797 req->lpszHeader = NULL;
1798 req->dwHeaderLength = 0;
1799 req->lpOptional = NULL;
1800 req->dwOptionalLength = 0;
1801 req->dwContentLength = 0;
1804 req->bEndRequest = FALSE;
1806 INTERNET_AsyncCall(&workRequest);
1808 * This is from windows.
1810 SetLastError(ERROR_IO_PENDING);
1815 ret = HTTP_HttpSendRequestW(lpwhr, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
1816 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
1817 lpBuffersIn->dwBufferTotal, FALSE);
1820 WININET_Release(&lpwhr->hdr);
1825 /***********************************************************************
1826 * HttpSendRequestW (WININET.@)
1828 * Sends the specified request to the HTTP server
1835 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
1836 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1838 LPWININETHTTPREQW lpwhr;
1839 LPWININETHTTPSESSIONW lpwhs = NULL;
1840 LPWININETAPPINFOW hIC = NULL;
1843 TRACE("%p, %p (%s), %li, %p, %li)\n", hHttpRequest,
1844 lpszHeaders, debugstr_w(lpszHeaders), dwHeaderLength, lpOptional, dwOptionalLength);
1846 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1847 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1849 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1854 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1855 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1857 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1862 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1863 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1865 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1870 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1872 WORKREQUEST workRequest;
1873 struct WORKREQ_HTTPSENDREQUESTW *req;
1875 workRequest.asyncall = HTTPSENDREQUESTW;
1876 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
1877 req = &workRequest.u.HttpSendRequestW;
1879 req->lpszHeader = WININET_strdupW(lpszHeaders);
1881 req->lpszHeader = 0;
1882 req->dwHeaderLength = dwHeaderLength;
1883 req->lpOptional = lpOptional;
1884 req->dwOptionalLength = dwOptionalLength;
1885 req->dwContentLength = dwOptionalLength;
1886 req->bEndRequest = TRUE;
1888 INTERNET_AsyncCall(&workRequest);
1890 * This is from windows.
1892 SetLastError(ERROR_IO_PENDING);
1897 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
1898 dwHeaderLength, lpOptional, dwOptionalLength,
1899 dwOptionalLength, TRUE);
1903 WININET_Release( &lpwhr->hdr );
1907 /***********************************************************************
1908 * HttpSendRequestA (WININET.@)
1910 * Sends the specified request to the HTTP server
1917 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
1918 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1921 LPWSTR szHeaders=NULL;
1922 DWORD nLen=dwHeaderLength;
1923 if(lpszHeaders!=NULL)
1925 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
1926 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
1927 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
1929 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
1930 HeapFree(GetProcessHeap(),0,szHeaders);
1934 /***********************************************************************
1935 * HTTP_HandleRedirect (internal)
1937 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl, LPCWSTR lpszHeaders,
1938 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
1939 DWORD dwContentLength)
1941 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1942 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1948 /* if it's an absolute path, keep the same session info */
1949 lstrcpynW(path, lpszUrl, 2048);
1951 else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1953 TRACE("Redirect through proxy\n");
1954 lstrcpynW(path, lpszUrl, 2048);
1958 URL_COMPONENTSW urlComponents;
1959 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
1960 static const WCHAR szHttp[] = {'h','t','t','p',0};
1961 static const WCHAR szHttps[] = {'h','t','t','p','s',0};
1962 DWORD url_length = 0;
1964 LPWSTR combined_url;
1966 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
1967 urlComponents.lpszScheme = (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE) ? (LPWSTR)szHttps : (LPWSTR)szHttp;
1968 urlComponents.dwSchemeLength = 0;
1969 urlComponents.lpszHostName = lpwhs->lpszHostName;
1970 urlComponents.dwHostNameLength = 0;
1971 urlComponents.nPort = lpwhs->nHostPort;
1972 urlComponents.lpszUserName = lpwhs->lpszUserName;
1973 urlComponents.dwUserNameLength = 0;
1974 urlComponents.lpszPassword = NULL;
1975 urlComponents.dwPasswordLength = 0;
1976 urlComponents.lpszUrlPath = lpwhr->lpszPath;
1977 urlComponents.dwUrlPathLength = 0;
1978 urlComponents.lpszExtraInfo = NULL;
1979 urlComponents.dwExtraInfoLength = 0;
1981 if (!InternetCreateUrlW(&urlComponents, 0, NULL, &url_length) &&
1982 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
1985 orig_url = HeapAlloc(GetProcessHeap(), 0, url_length);
1987 /* convert from bytes to characters */
1988 url_length = url_length / sizeof(WCHAR) - 1;
1989 if (!InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length))
1991 HeapFree(GetProcessHeap(), 0, orig_url);
1996 if (!InternetCombineUrlW(orig_url, lpszUrl, NULL, &url_length, ICU_ENCODE_SPACES_ONLY) &&
1997 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
1999 HeapFree(GetProcessHeap(), 0, orig_url);
2002 combined_url = HeapAlloc(GetProcessHeap(), 0, url_length * sizeof(WCHAR));
2004 if (!InternetCombineUrlW(orig_url, lpszUrl, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY))
2006 HeapFree(GetProcessHeap(), 0, orig_url);
2007 HeapFree(GetProcessHeap(), 0, combined_url);
2010 HeapFree(GetProcessHeap(), 0, orig_url);
2016 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2017 urlComponents.lpszScheme = protocol;
2018 urlComponents.dwSchemeLength = 32;
2019 urlComponents.lpszHostName = hostName;
2020 urlComponents.dwHostNameLength = MAXHOSTNAME;
2021 urlComponents.lpszUserName = userName;
2022 urlComponents.dwUserNameLength = 1024;
2023 urlComponents.lpszPassword = NULL;
2024 urlComponents.dwPasswordLength = 0;
2025 urlComponents.lpszUrlPath = path;
2026 urlComponents.dwUrlPathLength = 2048;
2027 urlComponents.lpszExtraInfo = NULL;
2028 urlComponents.dwExtraInfoLength = 0;
2029 if(!InternetCrackUrlW(combined_url, strlenW(combined_url), 0, &urlComponents))
2031 HeapFree(GetProcessHeap(), 0, combined_url);
2034 HeapFree(GetProcessHeap(), 0, combined_url);
2036 if (!strncmpW(szHttp, urlComponents.lpszScheme, strlenW(szHttp)) &&
2037 (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2039 TRACE("redirect from secure page to non-secure page\n");
2040 /* FIXME: warn about from secure redirect to non-secure page */
2041 lpwhr->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
2043 if (!strncmpW(szHttps, urlComponents.lpszScheme, strlenW(szHttps)) &&
2044 !(lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2046 TRACE("redirect from non-secure page to secure page\n");
2047 /* FIXME: notify about redirect to secure page */
2048 lpwhr->hdr.dwFlags |= INTERNET_FLAG_SECURE;
2051 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
2053 if (lstrlenW(protocol)>4) /*https*/
2054 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2056 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2061 * This upsets redirects to binary files on sourceforge.net
2062 * and gives an html page instead of the target file
2063 * Examination of the HTTP request sent by native wininet.dll
2064 * reveals that it doesn't send a referrer in that case.
2065 * Maybe there's a flag that enables this, or maybe a referrer
2066 * shouldn't be added in case of a redirect.
2069 /* consider the current host as the referrer */
2070 if (NULL != lpwhs->lpszServerName && strlenW(lpwhs->lpszServerName))
2071 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
2072 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
2073 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
2076 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2077 lpwhs->lpszServerName = WININET_strdupW(hostName);
2078 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
2079 if (urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT &&
2080 urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT)
2083 static WCHAR fmt[] = {'%','s',':','%','i',0};
2084 len = lstrlenW(hostName);
2085 len += 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
2086 lpwhs->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
2087 sprintfW(lpwhs->lpszHostName, fmt, hostName, urlComponents.nPort);
2090 lpwhs->lpszHostName = WININET_strdupW(hostName);
2092 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
2095 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2096 lpwhs->lpszUserName = NULL;
2098 lpwhs->lpszUserName = WININET_strdupW(userName);
2099 lpwhs->nServerPort = urlComponents.nPort;
2101 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2102 INTERNET_STATUS_RESOLVING_NAME,
2103 lpwhs->lpszServerName,
2104 strlenW(lpwhs->lpszServerName)+1);
2106 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
2107 &lpwhs->socketAddress))
2109 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
2113 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
2114 szaddr, sizeof(szaddr));
2115 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2116 INTERNET_STATUS_NAME_RESOLVED,
2117 szaddr, strlen(szaddr)+1);
2119 NETCON_close(&lpwhr->netConnection);
2121 if (!NETCON_init(&lpwhr->netConnection,lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2125 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2126 lpwhr->lpszPath=NULL;
2132 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
2133 if (rc != E_POINTER)
2134 needed = strlenW(path)+1;
2135 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
2136 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
2137 URL_ESCAPE_SPACES_ONLY);
2140 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(path),rc);
2141 strcpyW(lpwhr->lpszPath,path);
2145 return HTTP_HttpSendRequestW(lpwhr, lpszHeaders, dwHeaderLength, lpOptional,
2146 dwOptionalLength, dwContentLength, TRUE);
2149 /***********************************************************************
2150 * HTTP_build_req (internal)
2152 * concatenate all the strings in the request together
2154 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
2159 for( t = list; *t ; t++ )
2160 len += strlenW( *t );
2163 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2166 for( t = list; *t ; t++ )
2172 static BOOL HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr)
2175 LPWSTR requestString;
2181 static const WCHAR szConnect[] = {'C','O','N','N','E','C','T',0};
2182 static const WCHAR szFormat[] = {'%','s',':','%','d',0};
2183 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
2187 lpszPath = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs->lpszHostName ) + 13)*sizeof(WCHAR) );
2188 sprintfW( lpszPath, szFormat, lpwhs->lpszHostName, lpwhs->nHostPort );
2189 requestString = HTTP_BuildHeaderRequestString( lpwhr, szConnect, lpszPath, FALSE );
2190 HeapFree( GetProcessHeap(), 0, lpszPath );
2192 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2193 NULL, 0, NULL, NULL );
2194 len--; /* the nul terminator isn't needed */
2195 ascii_req = HeapAlloc( GetProcessHeap(), 0, len );
2196 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2197 ascii_req, len, NULL, NULL );
2198 HeapFree( GetProcessHeap(), 0, requestString );
2200 TRACE("full request -> %s\n", debugstr_an( ascii_req, len ) );
2202 ret = NETCON_send( &lpwhr->netConnection, ascii_req, len, 0, &cnt );
2203 HeapFree( GetProcessHeap(), 0, ascii_req );
2204 if (!ret || cnt < 0)
2207 responseLen = HTTP_GetResponseHeaders( lpwhr );
2214 /***********************************************************************
2215 * HTTP_HttpSendRequestW (internal)
2217 * Sends the specified request to the HTTP server
2224 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
2225 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
2226 DWORD dwContentLength, BOOL bEndRequest)
2229 BOOL bSuccess = FALSE;
2230 LPWSTR requestString = NULL;
2232 BOOL loop_next = FALSE;
2233 INTERNET_ASYNC_RESULT iar;
2236 TRACE("--> %p\n", lpwhr);
2238 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
2240 /* Clear any error information */
2241 INTERNET_SetLastError(0);
2243 HTTP_FixVerb(lpwhr);
2245 /* if we are using optional stuff, we must add the fixed header of that option length */
2246 if (dwContentLength > 0)
2248 static const WCHAR szContentLength[] = {
2249 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0};
2250 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \n\r */ + 20 /* int */ ];
2251 sprintfW(contentLengthStr, szContentLength, dwContentLength);
2252 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD);
2255 Host = HTTP_GetHeader(lpwhr,szHost);
2261 TRACE("Going to url %s %s\n", debugstr_w(Host->lpszValue), debugstr_w(lpwhr->lpszPath));
2266 /* add the headers the caller supplied */
2267 if( lpszHeaders && dwHeaderLength )
2269 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
2270 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
2273 /* if there's a proxy username and password, add it to the headers */
2274 HTTP_AddProxyInfo(lpwhr);
2276 requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, FALSE);
2278 TRACE("Request header -> %s\n", debugstr_w(requestString) );
2280 /* Send the request and store the results */
2281 if (!HTTP_OpenConnection(lpwhr))
2284 /* send the request as ASCII, tack on the optional data */
2286 dwOptionalLength = 0;
2287 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2288 NULL, 0, NULL, NULL );
2289 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
2290 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2291 ascii_req, len, NULL, NULL );
2293 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
2294 len = (len + dwOptionalLength - 1);
2296 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
2298 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2299 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
2301 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
2302 HeapFree( GetProcessHeap(), 0, ascii_req );
2304 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2305 INTERNET_STATUS_REQUEST_SENT,
2306 &len, sizeof(DWORD));
2310 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2311 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2316 responseLen = HTTP_GetResponseHeaders(lpwhr);
2320 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2321 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
2324 HTTP_ProcessHeaders(lpwhr);
2333 HeapFree(GetProcessHeap(), 0, requestString);
2335 /* TODO: send notification for P3P header */
2337 if(!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess && bEndRequest)
2339 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
2340 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
2341 (dwCode==302 || dwCode==301))
2343 WCHAR szNewLocation[2048];
2344 DWORD dwBufferSize=2048;
2346 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
2348 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2349 INTERNET_STATUS_REDIRECT, szNewLocation,
2351 return HTTP_HandleRedirect(lpwhr, szNewLocation, lpszHeaders,
2352 dwHeaderLength, lpOptional, dwOptionalLength,
2359 iar.dwResult = (DWORD)bSuccess;
2360 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
2362 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2363 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2364 sizeof(INTERNET_ASYNC_RESULT));
2371 /***********************************************************************
2372 * HTTP_Connect (internal)
2374 * Create http session handle
2377 * HINTERNET a session handle on success
2381 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
2382 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
2383 LPCWSTR lpszPassword, DWORD dwFlags, DWORD dwContext,
2384 DWORD dwInternalFlags)
2386 BOOL bSuccess = FALSE;
2387 LPWININETHTTPSESSIONW lpwhs = NULL;
2388 HINTERNET handle = NULL;
2392 assert( hIC->hdr.htype == WH_HINIT );
2394 hIC->hdr.dwContext = dwContext;
2396 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
2399 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2404 * According to my tests. The name is not resolved until a request is sent
2407 lpwhs->hdr.htype = WH_HHTTPSESSION;
2408 lpwhs->hdr.lpwhparent = WININET_AddRef( &hIC->hdr );
2409 lpwhs->hdr.dwFlags = dwFlags;
2410 lpwhs->hdr.dwContext = dwContext;
2411 lpwhs->hdr.dwInternalFlags = dwInternalFlags;
2412 lpwhs->hdr.dwRefCount = 1;
2413 lpwhs->hdr.destroy = HTTP_CloseHTTPSessionHandle;
2414 lpwhs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
2416 handle = WININET_AllocHandle( &lpwhs->hdr );
2419 ERR("Failed to alloc handle\n");
2420 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2424 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
2425 if(strchrW(hIC->lpszProxy, ' '))
2426 FIXME("Several proxies not implemented.\n");
2427 if(hIC->lpszProxyBypass)
2428 FIXME("Proxy bypass is ignored.\n");
2430 if (lpszServerName && lpszServerName[0])
2432 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
2433 lpwhs->lpszHostName = WININET_strdupW(lpszServerName);
2435 if (lpszUserName && lpszUserName[0])
2436 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
2437 lpwhs->nServerPort = nServerPort;
2438 lpwhs->nHostPort = nServerPort;
2440 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2441 if (!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
2443 INTERNET_SendCallback(&hIC->hdr, dwContext,
2444 INTERNET_STATUS_HANDLE_CREATED, &handle,
2452 WININET_Release( &lpwhs->hdr );
2455 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
2459 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
2464 /***********************************************************************
2465 * HTTP_OpenConnection (internal)
2467 * Connect to a web server
2474 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
2476 BOOL bSuccess = FALSE;
2477 LPWININETHTTPSESSIONW lpwhs;
2478 LPWININETAPPINFOW hIC = NULL;
2484 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2486 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2490 lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
2492 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2493 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
2494 szaddr, sizeof(szaddr));
2495 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2496 INTERNET_STATUS_CONNECTING_TO_SERVER,
2500 if (!NETCON_create(&lpwhr->netConnection, lpwhs->socketAddress.sin_family,
2503 WARN("Socket creation failed\n");
2507 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
2508 sizeof(lpwhs->socketAddress)))
2511 if (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE)
2513 /* Note: we differ from Microsoft's WinINet here. they seem to have
2514 * a bug that causes no status callbacks to be sent when starting
2515 * a tunnel to a proxy server using the CONNECT verb. i believe our
2516 * behaviour to be more correct and to not cause any incompatibilities
2517 * because using a secure connection through a proxy server is a rare
2518 * case that would be hard for anyone to depend on */
2519 if (hIC->lpszProxy && !HTTP_SecureProxyConnect(lpwhr))
2522 if (!NETCON_secure_connect(&lpwhr->netConnection, lpwhs->lpszHostName))
2524 WARN("Couldn't connect securely to host\n");
2529 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2530 INTERNET_STATUS_CONNECTED_TO_SERVER,
2531 szaddr, strlen(szaddr)+1);
2536 TRACE("%d <--\n", bSuccess);
2541 /***********************************************************************
2542 * HTTP_clear_response_headers (internal)
2544 * clear out any old response headers
2546 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
2550 for( i=0; i<lpwhr->nCustHeaders; i++)
2552 if( !lpwhr->pCustHeaders[i].lpszField )
2554 if( !lpwhr->pCustHeaders[i].lpszValue )
2556 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
2558 HTTP_DeleteCustomHeader( lpwhr, i );
2563 /***********************************************************************
2564 * HTTP_GetResponseHeaders (internal)
2566 * Read server response
2573 static INT HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr)
2576 WCHAR buffer[MAX_REPLY_LEN];
2577 DWORD buflen = MAX_REPLY_LEN;
2578 BOOL bSuccess = FALSE;
2580 static const WCHAR szCrLf[] = {'\r','\n',0};
2581 char bufferA[MAX_REPLY_LEN];
2582 LPWSTR status_code, status_text;
2583 DWORD cchMaxRawHeaders = 1024;
2584 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2585 DWORD cchRawHeaders = 0;
2589 /* clear old response headers (eg. from a redirect response) */
2590 HTTP_clear_response_headers( lpwhr );
2592 if (!NETCON_connected(&lpwhr->netConnection))
2596 * HACK peek at the buffer
2598 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
2601 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
2603 buflen = MAX_REPLY_LEN;
2604 memset(buffer, 0, MAX_REPLY_LEN);
2605 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2607 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2609 /* regenerate raw headers */
2610 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2612 cchMaxRawHeaders *= 2;
2613 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2615 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2616 cchRawHeaders += (buflen-1);
2617 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2618 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2619 lpszRawHeaders[cchRawHeaders] = '\0';
2621 /* split the version from the status code */
2622 status_code = strchrW( buffer, ' ' );
2627 /* split the status code from the status text */
2628 status_text = strchrW( status_code, ' ' );
2633 TRACE("version [%s] status code [%s] status text [%s]\n",
2634 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
2636 HTTP_ProcessHeader(lpwhr, szStatus, status_code,
2637 HTTP_ADDHDR_FLAG_REPLACE);
2639 HeapFree(GetProcessHeap(),0,lpwhr->lpszVersion);
2640 HeapFree(GetProcessHeap(),0,lpwhr->lpszStatusText);
2642 lpwhr->lpszVersion= WININET_strdupW(buffer);
2643 lpwhr->lpszStatusText = WININET_strdupW(status_text);
2645 /* Parse each response line */
2648 buflen = MAX_REPLY_LEN;
2649 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2651 LPWSTR * pFieldAndValue;
2653 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
2654 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2656 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2658 cchMaxRawHeaders *= 2;
2659 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2661 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2662 cchRawHeaders += (buflen-1);
2663 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2664 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2665 lpszRawHeaders[cchRawHeaders] = '\0';
2667 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
2668 if (!pFieldAndValue)
2671 HTTP_ProcessHeader(lpwhr, pFieldAndValue[0], pFieldAndValue[1],
2672 HTTP_ADDREQ_FLAG_ADD );
2674 HTTP_FreeTokens(pFieldAndValue);
2684 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2685 lpwhr->lpszRawHeaders = lpszRawHeaders;
2686 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
2699 static void strip_spaces(LPWSTR start)
2704 while (*str == ' ' && *str != '\0')
2708 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
2710 end = start + strlenW(start) - 1;
2711 while (end >= start && *end == ' ')
2719 /***********************************************************************
2720 * HTTP_InterpretHttpHeader (internal)
2722 * Parse server response
2726 * Pointer to array of field, value, NULL on success.
2729 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
2731 LPWSTR * pTokenPair;
2735 pTokenPair = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pTokenPair)*3);
2737 pszColon = strchrW(buffer, ':');
2738 /* must have two tokens */
2741 HTTP_FreeTokens(pTokenPair);
2743 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
2747 pTokenPair[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon - buffer + 1) * sizeof(WCHAR));
2750 HTTP_FreeTokens(pTokenPair);
2753 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
2754 pTokenPair[0][pszColon - buffer] = '\0';
2758 len = strlenW(pszColon);
2759 pTokenPair[1] = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
2762 HTTP_FreeTokens(pTokenPair);
2765 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
2767 strip_spaces(pTokenPair[0]);
2768 strip_spaces(pTokenPair[1]);
2770 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
2774 /***********************************************************************
2775 * HTTP_ProcessHeader (internal)
2777 * Stuff header into header tables according to <dwModifier>
2781 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2783 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
2785 LPHTTPHEADERW lphttpHdr = NULL;
2786 BOOL bSuccess = FALSE;
2788 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
2789 BOOL request_only = dwModifier & HTTP_ADDHDR_FLAG_REQ;
2791 TRACE("--> %s: %s - 0x%08lx\n", debugstr_w(field), debugstr_w(value), dwModifier);
2793 /* Don't let applications add Connection header to request */
2794 if (strcmpW(szConnection,field)==0 && (dwModifier & HTTP_ADDHDR_FLAG_REQ))
2799 /* REPLACE wins out over ADD */
2800 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
2801 dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
2803 if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
2806 index = HTTP_GetCustomHeaderIndex(lpwhr, field, 0, request_only);
2810 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
2814 lphttpHdr = &lpwhr->pCustHeaders[index];
2820 hdr.lpszField = (LPWSTR)field;
2821 hdr.lpszValue = (LPWSTR)value;
2822 hdr.wFlags = hdr.wCount = 0;
2824 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2825 hdr.wFlags |= HDR_ISREQUEST;
2827 return HTTP_InsertCustomHeader(lpwhr, &hdr);
2830 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2831 lphttpHdr->wFlags |= HDR_ISREQUEST;
2833 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
2835 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
2837 HTTP_DeleteCustomHeader( lpwhr, index );
2843 hdr.lpszField = (LPWSTR)field;
2844 hdr.lpszValue = (LPWSTR)value;
2845 hdr.wFlags = hdr.wCount = 0;
2847 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2848 hdr.wFlags |= HDR_ISREQUEST;
2850 return HTTP_InsertCustomHeader(lpwhr, &hdr);
2855 else if (dwModifier & COALESCEFLASG)
2860 INT origlen = strlenW(lphttpHdr->lpszValue);
2861 INT valuelen = strlenW(value);
2863 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
2866 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2868 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2871 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2874 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
2876 lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
2879 lphttpHdr->lpszValue = lpsztmp;
2880 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
2883 lphttpHdr->lpszValue[origlen] = ch;
2885 lphttpHdr->lpszValue[origlen] = ' ';
2889 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
2890 lphttpHdr->lpszValue[len] = '\0';
2895 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
2896 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2899 TRACE("<-- %d\n",bSuccess);
2904 /***********************************************************************
2905 * HTTP_CloseConnection (internal)
2907 * Close socket connection
2910 static VOID HTTP_CloseConnection(LPWININETHTTPREQW lpwhr)
2912 LPWININETHTTPSESSIONW lpwhs = NULL;
2913 LPWININETAPPINFOW hIC = NULL;
2915 TRACE("%p\n",lpwhr);
2917 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
2918 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2920 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2921 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2923 if (NETCON_connected(&lpwhr->netConnection))
2925 NETCON_close(&lpwhr->netConnection);
2928 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2929 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2933 /***********************************************************************
2934 * HTTP_CloseHTTPRequestHandle (internal)
2936 * Deallocate request handle
2939 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr)
2942 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
2946 if (NETCON_connected(&lpwhr->netConnection))
2947 HTTP_CloseConnection(lpwhr);
2949 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2950 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
2951 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2952 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVersion);
2953 HeapFree(GetProcessHeap(), 0, lpwhr->lpszStatusText);
2955 for (i = 0; i < lpwhr->nCustHeaders; i++)
2957 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
2958 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
2961 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
2962 HeapFree(GetProcessHeap(), 0, lpwhr);
2966 /***********************************************************************
2967 * HTTP_CloseHTTPSessionHandle (internal)
2969 * Deallocate session handle
2972 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr)
2974 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
2976 TRACE("%p\n", lpwhs);
2978 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
2979 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2980 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2981 HeapFree(GetProcessHeap(), 0, lpwhs);
2985 /***********************************************************************
2986 * HTTP_GetCustomHeaderIndex (internal)
2988 * Return index of custom header from header array
2991 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField,
2992 int requested_index, BOOL request_only)
2996 TRACE("%s\n", debugstr_w(lpszField));
2998 for (index = 0; index < lpwhr->nCustHeaders; index++)
3000 if (strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
3003 if (request_only && !(lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
3006 if (!request_only && (lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
3009 if (requested_index == 0)
3014 if (index >= lpwhr->nCustHeaders)
3017 TRACE("Return: %ld\n", index);
3022 /***********************************************************************
3023 * HTTP_InsertCustomHeader (internal)
3025 * Insert header into array
3028 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
3031 LPHTTPHEADERW lph = NULL;
3034 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
3035 count = lpwhr->nCustHeaders + 1;
3037 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
3039 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
3043 lpwhr->pCustHeaders = lph;
3044 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
3045 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
3046 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
3047 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
3048 lpwhr->nCustHeaders++;
3053 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3060 /***********************************************************************
3061 * HTTP_DeleteCustomHeader (internal)
3063 * Delete header from array
3064 * If this function is called, the indexs may change.
3066 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
3068 if( lpwhr->nCustHeaders <= 0 )
3070 if( index >= lpwhr->nCustHeaders )
3072 lpwhr->nCustHeaders--;
3074 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
3075 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
3076 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
3081 /***********************************************************************
3082 * IsHostInProxyBypassList (@)
3087 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
3089 FIXME("STUB: flags=%ld host=%s length=%ld\n",flags,szHost,length);