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
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_szHost[] = {'\r','\n','H','o','s','t',':',' ',0 };
62 static const WCHAR g_szReferer[] = {'R','e','f','e','r','e','r',0};
63 static const WCHAR g_szAccept[] = {'A','c','c','e','p','t',0};
64 static const WCHAR g_szUserAgent[] = {'U','s','e','r','-','A','g','e','n','t',0};
67 #define HTTPHEADER g_szHttp
68 #define HTTPHOSTHEADER g_szHost
69 #define MAXHOSTNAME 100
70 #define MAX_FIELD_VALUE_LEN 256
71 #define MAX_FIELD_LEN 256
73 #define HTTP_REFERER g_szReferer
74 #define HTTP_ACCEPT g_szAccept
75 #define HTTP_USERAGENT g_szUserAgent
77 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
78 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
79 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
80 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
81 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
82 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
83 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
86 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr);
87 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr);
88 BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr);
89 int HTTP_WriteDataToStream(LPWININETHTTPREQW lpwhr,
90 void *Buffer, int BytesToWrite);
91 int HTTP_ReadDataFromStream(LPWININETHTTPREQW lpwhr,
92 void *Buffer, int BytesToRead);
93 BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr);
94 BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
95 BOOL HTTP_ReplaceHeaderValue( LPHTTPHEADERW lphttpHdr, LPCWSTR lpsztmp );
96 void HTTP_CloseConnection(LPWININETHTTPREQW lpwhr);
97 BOOL HTTP_InterpretHttpHeader(LPWSTR buffer, LPWSTR field, INT fieldlen, LPWSTR value, INT valuelen);
98 INT HTTP_GetStdHeaderIndex(LPCWSTR lpszField);
99 BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr);
100 INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField);
101 BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index);
103 /***********************************************************************
104 * HTTP_Tokenize (internal)
106 * Tokenize a string, allocating memory for the tokens.
108 static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
110 LPWSTR * token_array;
115 /* empty string has no tokens */
119 for (i = 0; string[i]; i++)
120 if (!strncmpW(string+i, token_string, strlenW(token_string)))
124 /* we want to skip over separators, but not the null terminator */
125 for (j = 0; j < strlenW(token_string) - 1; j++)
131 /* add 1 for terminating NULL */
132 token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
133 token_array[tokens] = NULL;
136 for (i = 0; i < tokens; i++)
139 next_token = strstrW(string, token_string);
140 if (!next_token) next_token = string+strlenW(string);
141 len = next_token - string;
142 token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
143 memcpy(token_array[i], string, len*sizeof(WCHAR));
144 token_array[i][len] = '\0';
145 string = next_token+strlenW(token_string);
150 /***********************************************************************
151 * HTTP_FreeTokens (internal)
153 * Frees memory returned from HTTP_Tokenize.
155 static void HTTP_FreeTokens(LPWSTR * token_array)
158 for (i = 0; token_array[i]; i++)
159 HeapFree(GetProcessHeap(), 0, token_array[i]);
160 HeapFree(GetProcessHeap(), 0, token_array);
163 /***********************************************************************
164 * HTTP_HttpAddRequestHeadersW (internal)
166 static BOOL WINAPI HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
167 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
172 WCHAR value[MAX_FIELD_VALUE_LEN], field[MAX_FIELD_LEN];
173 BOOL bSuccess = FALSE;
176 TRACE("copying header: %s\n", debugstr_w(lpszHeader));
178 if( dwHeaderLength == ~0UL )
179 len = strlenW(lpszHeader);
181 len = dwHeaderLength;
182 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
183 strncpyW( buffer, lpszHeader, len );
192 while (*lpszEnd != '\0')
194 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
199 if (*lpszStart == '\0')
202 if (*lpszEnd == '\r')
205 lpszEnd += 2; /* Jump over \r\n */
207 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
208 if (HTTP_InterpretHttpHeader(lpszStart, field, MAX_FIELD_LEN, value, MAX_FIELD_VALUE_LEN))
209 bSuccess = HTTP_ProcessHeader(lpwhr, field, value, dwModifier | HTTP_ADDHDR_FLAG_REQ);
215 HeapFree(GetProcessHeap(), 0, buffer);
220 /***********************************************************************
221 * HttpAddRequestHeadersW (WININET.@)
223 * Adds one or more HTTP header to the request handler
230 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
231 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
233 BOOL bSuccess = FALSE;
234 LPWININETHTTPREQW lpwhr;
236 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_w(lpszHeader), dwHeaderLength,
242 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
243 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
245 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
248 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
251 WININET_Release( &lpwhr->hdr );
256 /***********************************************************************
257 * HttpAddRequestHeadersA (WININET.@)
259 * Adds one or more HTTP header to the request handler
266 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
267 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
273 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_a(lpszHeader), dwHeaderLength,
276 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
277 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
278 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
279 if( dwHeaderLength != ~0UL )
280 dwHeaderLength = len;
282 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
284 HeapFree( GetProcessHeap(), 0, hdr );
289 /***********************************************************************
290 * HttpEndRequestA (WININET.@)
292 * Ends an HTTP request that was started by HttpSendRequestEx
299 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest, LPINTERNET_BUFFERSA lpBuffersOut,
300 DWORD dwFlags, DWORD dwContext)
306 /***********************************************************************
307 * HttpEndRequestW (WININET.@)
309 * Ends an HTTP request that was started by HttpSendRequestEx
316 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest, LPINTERNET_BUFFERSW lpBuffersOut,
317 DWORD dwFlags, DWORD dwContext)
323 /***********************************************************************
324 * HttpOpenRequestW (WININET.@)
326 * Open a HTTP request handle
329 * HINTERNET a HTTP request handle on success
333 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
334 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
335 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
336 DWORD dwFlags, DWORD dwContext)
338 LPWININETHTTPSESSIONW lpwhs;
339 LPWININETAPPINFOW hIC = NULL;
340 HINTERNET handle = NULL;
342 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
343 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
344 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
346 if(lpszAcceptTypes!=NULL)
349 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
350 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
353 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
354 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
356 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
359 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
362 * My tests seem to show that the windows version does not
363 * become asynchronous until after this point. And anyhow
364 * if this call was asynchronous then how would you get the
365 * necessary HINTERNET pointer returned by this function.
368 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
369 lpszVersion, lpszReferrer, lpszAcceptTypes,
373 WININET_Release( &lpwhs->hdr );
374 TRACE("returning %p\n", handle);
379 /***********************************************************************
380 * HttpOpenRequestA (WININET.@)
382 * Open a HTTP request handle
385 * HINTERNET a HTTP request handle on success
389 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
390 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
391 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
392 DWORD dwFlags, DWORD dwContext)
394 LPWSTR szVerb = NULL, szObjectName = NULL;
395 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
397 INT acceptTypesCount;
398 HINTERNET rc = FALSE;
399 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
400 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
401 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
406 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
407 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
410 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
415 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
416 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
419 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
424 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
425 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
428 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
433 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
434 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
437 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
440 acceptTypesCount = 0;
443 /* find out how many there are */
444 while (lpszAcceptTypes[acceptTypesCount])
446 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
447 acceptTypesCount = 0;
448 while (lpszAcceptTypes[acceptTypesCount])
450 len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
452 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
453 if (!szAcceptTypes[acceptTypesCount] )
455 MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
456 -1, szAcceptTypes[acceptTypesCount], len );
459 szAcceptTypes[acceptTypesCount] = NULL;
461 else szAcceptTypes = 0;
463 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
464 szVersion, szReferrer,
465 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
470 acceptTypesCount = 0;
471 while (szAcceptTypes[acceptTypesCount])
473 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
476 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
478 if (szReferrer) HeapFree(GetProcessHeap(), 0, szReferrer);
479 if (szVersion) HeapFree(GetProcessHeap(), 0, szVersion);
480 if (szObjectName) HeapFree(GetProcessHeap(), 0, szObjectName);
481 if (szVerb) HeapFree(GetProcessHeap(), 0, szVerb);
486 /***********************************************************************
489 static UINT HTTP_Base64( LPCWSTR bin, LPWSTR base64 )
492 static LPSTR HTTP_Base64Enc =
493 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
497 /* first 6 bits, all from bin[0] */
498 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
499 x = (bin[0] & 3) << 4;
501 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
504 base64[n++] = HTTP_Base64Enc[x];
509 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
510 x = ( bin[1] & 0x0f ) << 2;
512 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
515 base64[n++] = HTTP_Base64Enc[x];
519 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
521 /* last 6 bits, all from bin [2] */
522 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
529 /***********************************************************************
530 * HTTP_EncodeBasicAuth
532 * Encode the basic authentication string for HTTP 1.1
534 static LPWSTR HTTP_EncodeBasicAuth( LPCWSTR username, LPCWSTR password)
538 static const WCHAR szBasic[] = {'B','a','s','i','c',' ',0};
539 static const WCHAR szColon[] = {':',0};
541 len = lstrlenW( username ) + 1 + lstrlenW ( password ) + 1;
542 in = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
546 len = lstrlenW(szBasic) +
547 (lstrlenW( username ) + 1 + lstrlenW ( password ))*2 + 1 + 1;
548 out = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
551 lstrcpyW( in, username );
552 lstrcatW( in, szColon );
553 lstrcatW( in, password );
554 lstrcpyW( out, szBasic );
555 HTTP_Base64( in, &out[strlenW(out)] );
557 HeapFree( GetProcessHeap(), 0, in );
562 /***********************************************************************
563 * HTTP_InsertProxyAuthorization
565 * Insert the basic authorization field in the request header
567 BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
568 LPCWSTR username, LPCWSTR password )
572 static const WCHAR szProxyAuthorization[] = {
573 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
575 hdr.lpszValue = HTTP_EncodeBasicAuth( username, password );
576 hdr.lpszField = (WCHAR *)szProxyAuthorization;
577 hdr.wFlags = HDR_ISREQUEST;
582 TRACE("Inserting %s = %s\n",
583 debugstr_w( hdr.lpszField ), debugstr_w( hdr.lpszValue ) );
585 /* remove the old proxy authorization header */
586 index = HTTP_GetCustomHeaderIndex( lpwhr, hdr.lpszField );
588 HTTP_DeleteCustomHeader( lpwhr, index );
590 HTTP_InsertCustomHeader(lpwhr, &hdr);
591 HeapFree( GetProcessHeap(), 0, hdr.lpszValue );
596 /***********************************************************************
599 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
600 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
602 WCHAR buf[MAXHOSTNAME];
603 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
605 static const WCHAR szNul[] = { 0 };
606 URL_COMPONENTSW UrlComponents;
607 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }, szSlash[] = { '/',0 } ;
608 static const WCHAR szFormat1[] = { 'h','t','t','p',':','/','/','%','s',0 };
609 static const WCHAR szFormat2[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
612 memset( &UrlComponents, 0, sizeof UrlComponents );
613 UrlComponents.dwStructSize = sizeof UrlComponents;
614 UrlComponents.lpszHostName = buf;
615 UrlComponents.dwHostNameLength = MAXHOSTNAME;
617 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
618 buf,strlenW(szHttp),szHttp,strlenW(szHttp)) )
619 sprintfW(proxy, szFormat1, hIC->lpszProxy);
622 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
624 if( UrlComponents.dwHostNameLength == 0 )
627 if( !lpwhr->lpszPath )
628 lpwhr->lpszPath = (LPWSTR)szNul;
629 TRACE("server='%s' path='%s'\n",
630 debugstr_w(lpwhs->lpszServerName), debugstr_w(lpwhr->lpszPath));
631 /* for constant 15 see above */
632 len = strlenW(lpwhs->lpszServerName) + strlenW(lpwhr->lpszPath) + 15;
633 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
635 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
636 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
638 sprintfW(url, szFormat2, lpwhs->lpszServerName, lpwhs->nServerPort);
640 if( lpwhr->lpszPath[0] != '/' )
641 strcatW( url, szSlash );
642 strcatW(url, lpwhr->lpszPath);
643 if(lpwhr->lpszPath != szNul)
644 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
645 lpwhr->lpszPath = url;
646 /* FIXME: Do I have to free lpwhs->lpszServerName here ? */
647 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
648 lpwhs->nServerPort = UrlComponents.nPort;
653 /***********************************************************************
654 * HTTP_HttpOpenRequestW (internal)
656 * Open a HTTP request handle
659 * HINTERNET a HTTP request handle on success
663 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
664 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
665 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
666 DWORD dwFlags, DWORD dwContext)
668 LPWININETAPPINFOW hIC = NULL;
669 LPWININETHTTPREQW lpwhr;
671 LPWSTR lpszUrl = NULL;
673 HINTERNET handle = NULL;
674 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
679 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
680 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
682 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
685 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
688 lpwhr->hdr.htype = WH_HHTTPREQ;
689 lpwhr->hdr.lpwhparent = WININET_AddRef( &lpwhs->hdr );
690 lpwhr->hdr.dwFlags = dwFlags;
691 lpwhr->hdr.dwContext = dwContext;
692 lpwhr->hdr.dwRefCount = 1;
693 lpwhr->hdr.destroy = HTTP_CloseHTTPRequestHandle;
695 handle = WININET_AllocHandle( &lpwhr->hdr );
698 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
702 NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE);
704 if (NULL != lpszObjectName && strlenW(lpszObjectName)) {
708 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
710 len = strlenW(lpszObjectName)+1;
711 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
712 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
713 URL_ESCAPE_SPACES_ONLY);
716 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(lpszObjectName),rc);
717 strcpyW(lpwhr->lpszPath,lpszObjectName);
721 if (NULL != lpszReferrer && strlenW(lpszReferrer))
722 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDHDR_FLAG_COALESCE);
724 if(lpszAcceptTypes!=NULL)
727 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
728 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i], HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_REQ|HTTP_ADDHDR_FLAG_ADD_IF_NEW);
731 if (NULL == lpszVerb)
733 static const WCHAR szGet[] = {'G','E','T',0};
734 lpwhr->lpszVerb = WININET_strdupW(szGet);
736 else if (strlenW(lpszVerb))
737 lpwhr->lpszVerb = WININET_strdupW(lpszVerb);
739 if (NULL != lpszReferrer && strlenW(lpszReferrer))
741 WCHAR buf[MAXHOSTNAME];
742 URL_COMPONENTSW UrlComponents;
744 memset( &UrlComponents, 0, sizeof UrlComponents );
745 UrlComponents.dwStructSize = sizeof UrlComponents;
746 UrlComponents.lpszHostName = buf;
747 UrlComponents.dwHostNameLength = MAXHOSTNAME;
749 InternetCrackUrlW(lpszReferrer, 0, 0, &UrlComponents);
750 if (strlenW(UrlComponents.lpszHostName))
751 lpwhr->lpszHostName = WININET_strdupW(UrlComponents.lpszHostName);
753 lpwhr->lpszHostName = WININET_strdupW(lpwhs->lpszServerName);
755 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
756 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
761 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
763 len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
764 agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
765 sprintfW(agent_header, user_agent, hIC->lpszAgent );
767 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
768 HTTP_ADDREQ_FLAG_ADD);
769 HeapFree(GetProcessHeap(), 0, agent_header);
772 len = strlenW(lpwhr->lpszHostName) + strlenW(szUrlForm);
773 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
774 sprintfW( lpszUrl, szUrlForm, lpwhr->lpszHostName );
776 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
777 InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
780 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
781 static const WCHAR szcrlf[] = {'\r','\n',0};
783 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
785 cnt += sprintfW(lpszCookies, szCookie);
786 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
787 strcatW(lpszCookies, szcrlf);
789 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
790 HTTP_ADDREQ_FLAG_ADD);
791 HeapFree(GetProcessHeap(), 0, lpszCookies);
793 HeapFree(GetProcessHeap(), 0, lpszUrl);
797 if (hIC->lpfnStatusCB)
799 INTERNET_ASYNC_RESULT iar;
801 iar.dwResult = (DWORD)handle;
802 iar.dwError = ERROR_SUCCESS;
804 SendAsyncCallback(hIC, &lpwhs->hdr, dwContext,
805 INTERNET_STATUS_HANDLE_CREATED, &iar,
806 sizeof(INTERNET_ASYNC_RESULT));
810 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
814 * According to my tests. The name is not resolved until a request is Opened
816 SendAsyncCallback(hIC, &lpwhs->hdr, dwContext,
817 INTERNET_STATUS_RESOLVING_NAME,
818 lpwhs->lpszServerName,
819 strlenW(lpwhs->lpszServerName)+1);
820 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
821 &lpwhs->phostent, &lpwhs->socketAddress))
823 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
824 InternetCloseHandle( handle );
829 SendAsyncCallback(hIC, &lpwhs->hdr, lpwhr->hdr.dwContext,
830 INTERNET_STATUS_NAME_RESOLVED,
831 &(lpwhs->socketAddress),
832 sizeof(struct sockaddr_in));
836 WININET_Release( &lpwhr->hdr );
838 TRACE("<-- %p (%p)\n", handle, lpwhr);
842 /***********************************************************************
843 * HTTP_HttpQueryInfoW (internal)
845 BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
846 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
848 LPHTTPHEADERW lphttpHdr = NULL;
849 BOOL bSuccess = FALSE;
851 /* Find requested header structure */
852 if ((dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK) == HTTP_QUERY_CUSTOM)
854 INT index = HTTP_GetCustomHeaderIndex(lpwhr, (LPWSTR)lpBuffer);
859 lphttpHdr = &lpwhr->pCustHeaders[index];
863 INT index = dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK;
865 if (index == HTTP_QUERY_RAW_HEADERS_CRLF)
867 DWORD len = strlenW(lpwhr->lpszRawHeaders);
868 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
870 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
871 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
874 memcpy(lpBuffer, lpwhr->lpszRawHeaders, (len+1)*sizeof(WCHAR));
875 *lpdwBufferLength = len * sizeof(WCHAR);
877 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
881 else if (index == HTTP_QUERY_RAW_HEADERS)
883 static const WCHAR szCrLf[] = {'\r','\n',0};
884 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
886 LPWSTR pszString = (WCHAR*)lpBuffer;
888 for (i = 0; ppszRawHeaderLines[i]; i++)
889 size += strlenW(ppszRawHeaderLines[i]) + 1;
891 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
893 HTTP_FreeTokens(ppszRawHeaderLines);
894 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
895 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
899 for (i = 0; ppszRawHeaderLines[i]; i++)
901 DWORD len = strlenW(ppszRawHeaderLines[i]);
902 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
907 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
909 *lpdwBufferLength = size * sizeof(WCHAR);
910 HTTP_FreeTokens(ppszRawHeaderLines);
914 else if (index >= 0 && index <= HTTP_QUERY_MAX && lpwhr->StdHeaders[index].lpszValue)
916 lphttpHdr = &lpwhr->StdHeaders[index];
920 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
925 /* Ensure header satisifies requested attributes */
926 if ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
927 (~lphttpHdr->wFlags & HDR_ISREQUEST))
929 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
933 /* coalesce value to reuqested type */
934 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
936 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
939 TRACE(" returning number : %d\n", *(int *)lpBuffer);
941 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
947 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
949 tmpTM = *gmtime(&tmpTime);
950 STHook = (SYSTEMTIME *) lpBuffer;
954 STHook->wDay = tmpTM.tm_mday;
955 STHook->wHour = tmpTM.tm_hour;
956 STHook->wMilliseconds = 0;
957 STHook->wMinute = tmpTM.tm_min;
958 STHook->wDayOfWeek = tmpTM.tm_wday;
959 STHook->wMonth = tmpTM.tm_mon + 1;
960 STHook->wSecond = tmpTM.tm_sec;
961 STHook->wYear = tmpTM.tm_year;
965 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
966 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
967 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
969 else if (dwInfoLevel & HTTP_QUERY_FLAG_COALESCE)
971 if (*lpdwIndex >= lphttpHdr->wCount)
973 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
977 /* Copy strncpyW(lpBuffer, lphttpHdr[*lpdwIndex], len); */
983 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
985 if (len > *lpdwBufferLength)
987 *lpdwBufferLength = len;
988 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
992 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
993 *lpdwBufferLength = len - sizeof(WCHAR);
996 TRACE(" returning string : '%s'\n", debugstr_w(lpBuffer));
1001 /***********************************************************************
1002 * HttpQueryInfoW (WININET.@)
1004 * Queries for information about an HTTP request
1011 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1012 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1014 BOOL bSuccess = FALSE;
1015 LPWININETHTTPREQW lpwhr;
1017 if (TRACE_ON(wininet)) {
1018 #define FE(x) { x, #x }
1019 static const wininet_flag_info query_flags[] = {
1020 FE(HTTP_QUERY_MIME_VERSION),
1021 FE(HTTP_QUERY_CONTENT_TYPE),
1022 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
1023 FE(HTTP_QUERY_CONTENT_ID),
1024 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
1025 FE(HTTP_QUERY_CONTENT_LENGTH),
1026 FE(HTTP_QUERY_CONTENT_LANGUAGE),
1027 FE(HTTP_QUERY_ALLOW),
1028 FE(HTTP_QUERY_PUBLIC),
1029 FE(HTTP_QUERY_DATE),
1030 FE(HTTP_QUERY_EXPIRES),
1031 FE(HTTP_QUERY_LAST_MODIFIED),
1032 FE(HTTP_QUERY_MESSAGE_ID),
1034 FE(HTTP_QUERY_DERIVED_FROM),
1035 FE(HTTP_QUERY_COST),
1036 FE(HTTP_QUERY_LINK),
1037 FE(HTTP_QUERY_PRAGMA),
1038 FE(HTTP_QUERY_VERSION),
1039 FE(HTTP_QUERY_STATUS_CODE),
1040 FE(HTTP_QUERY_STATUS_TEXT),
1041 FE(HTTP_QUERY_RAW_HEADERS),
1042 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
1043 FE(HTTP_QUERY_CONNECTION),
1044 FE(HTTP_QUERY_ACCEPT),
1045 FE(HTTP_QUERY_ACCEPT_CHARSET),
1046 FE(HTTP_QUERY_ACCEPT_ENCODING),
1047 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
1048 FE(HTTP_QUERY_AUTHORIZATION),
1049 FE(HTTP_QUERY_CONTENT_ENCODING),
1050 FE(HTTP_QUERY_FORWARDED),
1051 FE(HTTP_QUERY_FROM),
1052 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
1053 FE(HTTP_QUERY_LOCATION),
1054 FE(HTTP_QUERY_ORIG_URI),
1055 FE(HTTP_QUERY_REFERER),
1056 FE(HTTP_QUERY_RETRY_AFTER),
1057 FE(HTTP_QUERY_SERVER),
1058 FE(HTTP_QUERY_TITLE),
1059 FE(HTTP_QUERY_USER_AGENT),
1060 FE(HTTP_QUERY_WWW_AUTHENTICATE),
1061 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
1062 FE(HTTP_QUERY_ACCEPT_RANGES),
1063 FE(HTTP_QUERY_SET_COOKIE),
1064 FE(HTTP_QUERY_COOKIE),
1065 FE(HTTP_QUERY_REQUEST_METHOD),
1066 FE(HTTP_QUERY_REFRESH),
1067 FE(HTTP_QUERY_CONTENT_DISPOSITION),
1069 FE(HTTP_QUERY_CACHE_CONTROL),
1070 FE(HTTP_QUERY_CONTENT_BASE),
1071 FE(HTTP_QUERY_CONTENT_LOCATION),
1072 FE(HTTP_QUERY_CONTENT_MD5),
1073 FE(HTTP_QUERY_CONTENT_RANGE),
1074 FE(HTTP_QUERY_ETAG),
1075 FE(HTTP_QUERY_HOST),
1076 FE(HTTP_QUERY_IF_MATCH),
1077 FE(HTTP_QUERY_IF_NONE_MATCH),
1078 FE(HTTP_QUERY_IF_RANGE),
1079 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
1080 FE(HTTP_QUERY_MAX_FORWARDS),
1081 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
1082 FE(HTTP_QUERY_RANGE),
1083 FE(HTTP_QUERY_TRANSFER_ENCODING),
1084 FE(HTTP_QUERY_UPGRADE),
1085 FE(HTTP_QUERY_VARY),
1087 FE(HTTP_QUERY_WARNING),
1088 FE(HTTP_QUERY_CUSTOM)
1090 static const wininet_flag_info modifier_flags[] = {
1091 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
1092 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
1093 FE(HTTP_QUERY_FLAG_NUMBER),
1094 FE(HTTP_QUERY_FLAG_COALESCE)
1097 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
1098 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
1101 TRACE("(%p, 0x%08lx)--> %ld\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
1102 TRACE(" Attribute:");
1103 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
1104 if (query_flags[i].val == info) {
1105 DPRINTF(" %s", query_flags[i].name);
1109 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
1110 DPRINTF(" Unknown (%08lx)", info);
1113 DPRINTF(" Modifier:");
1114 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
1115 if (modifier_flags[i].val & info_mod) {
1116 DPRINTF(" %s", modifier_flags[i].name);
1117 info_mod &= ~ modifier_flags[i].val;
1122 DPRINTF(" Unknown (%08lx)", info_mod);
1127 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1128 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1130 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1134 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
1135 lpBuffer, lpdwBufferLength, lpdwIndex);
1139 WININET_Release( &lpwhr->hdr );
1141 TRACE("%d <--\n", bSuccess);
1145 /***********************************************************************
1146 * HttpQueryInfoA (WININET.@)
1148 * Queries for information about an HTTP request
1155 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1156 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1162 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
1163 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
1165 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
1166 lpdwBufferLength, lpdwIndex );
1169 len = (*lpdwBufferLength)*sizeof(WCHAR);
1170 bufferW = HeapAlloc( GetProcessHeap(), 0, len );
1171 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
1175 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
1176 lpBuffer, *lpdwBufferLength, NULL, NULL );
1177 *lpdwBufferLength = len - 1;
1179 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
1182 /* since the strings being returned from HttpQueryInfoW should be
1183 * only ASCII characters, it is reasonable to assume that all of
1184 * the Unicode characters can be reduced to a single byte */
1185 *lpdwBufferLength = len / sizeof(WCHAR);
1187 HeapFree(GetProcessHeap(), 0, bufferW );
1192 /***********************************************************************
1193 * HttpSendRequestExA (WININET.@)
1195 * Sends the specified request to the HTTP server and allows chunked
1198 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
1199 LPINTERNET_BUFFERSA lpBuffersIn,
1200 LPINTERNET_BUFFERSA lpBuffersOut,
1201 DWORD dwFlags, DWORD dwContext)
1203 FIXME("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersIn,
1204 lpBuffersOut, dwFlags, dwContext);
1208 /***********************************************************************
1209 * HttpSendRequestW (WININET.@)
1211 * Sends the specified request to the HTTP server
1218 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
1219 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1221 LPWININETHTTPREQW lpwhr;
1222 LPWININETHTTPSESSIONW lpwhs = NULL;
1223 LPWININETAPPINFOW hIC = NULL;
1226 TRACE("%p, %p (%s), %li, %p, %li)\n", hHttpRequest,
1227 lpszHeaders, debugstr_w(lpszHeaders), dwHeaderLength, lpOptional, dwOptionalLength);
1229 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1230 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1232 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1237 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1238 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1240 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1245 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1246 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1248 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1253 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1255 WORKREQUEST workRequest;
1256 struct WORKREQ_HTTPSENDREQUESTW *req;
1258 workRequest.asyncall = HTTPSENDREQUESTW;
1259 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
1260 req = &workRequest.u.HttpSendRequestW;
1262 req->lpszHeader = WININET_strdupW(lpszHeaders);
1264 req->lpszHeader = 0;
1265 req->dwHeaderLength = dwHeaderLength;
1266 req->lpOptional = lpOptional;
1267 req->dwOptionalLength = dwOptionalLength;
1269 INTERNET_AsyncCall(&workRequest);
1271 * This is from windows.
1273 SetLastError(ERROR_IO_PENDING);
1278 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
1279 dwHeaderLength, lpOptional, dwOptionalLength);
1283 WININET_Release( &lpwhr->hdr );
1287 /***********************************************************************
1288 * HttpSendRequestA (WININET.@)
1290 * Sends the specified request to the HTTP server
1297 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
1298 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1301 LPWSTR szHeaders=NULL;
1302 DWORD nLen=dwHeaderLength;
1303 if(lpszHeaders!=NULL)
1305 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
1306 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
1307 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
1309 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
1311 HeapFree(GetProcessHeap(),0,szHeaders);
1315 /***********************************************************************
1316 * HTTP_HandleRedirect (internal)
1318 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl, LPCWSTR lpszHeaders,
1319 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength)
1321 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1322 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1327 /* if it's an absolute path, keep the same session info */
1328 strcpyW(path,lpszUrl);
1330 else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1332 TRACE("Redirect through proxy\n");
1333 strcpyW(path,lpszUrl);
1337 URL_COMPONENTSW urlComponents;
1338 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
1339 WCHAR password[1024], extra[1024];
1340 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
1341 urlComponents.lpszScheme = protocol;
1342 urlComponents.dwSchemeLength = 32;
1343 urlComponents.lpszHostName = hostName;
1344 urlComponents.dwHostNameLength = MAXHOSTNAME;
1345 urlComponents.lpszUserName = userName;
1346 urlComponents.dwUserNameLength = 1024;
1347 urlComponents.lpszPassword = password;
1348 urlComponents.dwPasswordLength = 1024;
1349 urlComponents.lpszUrlPath = path;
1350 urlComponents.dwUrlPathLength = 2048;
1351 urlComponents.lpszExtraInfo = extra;
1352 urlComponents.dwExtraInfoLength = 1024;
1353 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
1356 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1357 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1361 * This upsets redirects to binary files on sourceforge.net
1362 * and gives an html page instead of the target file
1363 * Examination of the HTTP request sent by native wininet.dll
1364 * reveals that it doesn't send a referrer in that case.
1365 * Maybe there's a flag that enables this, or maybe a referrer
1366 * shouldn't be added in case of a redirect.
1369 /* consider the current host as the referrer */
1370 if (NULL != lpwhs->lpszServerName && strlenW(lpwhs->lpszServerName))
1371 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
1372 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
1373 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
1376 if (NULL != lpwhs->lpszServerName)
1377 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1378 lpwhs->lpszServerName = WININET_strdupW(hostName);
1379 if (NULL != lpwhs->lpszUserName)
1380 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
1381 lpwhs->lpszUserName = WININET_strdupW(userName);
1382 lpwhs->nServerPort = urlComponents.nPort;
1384 if (NULL != lpwhr->lpszHostName)
1385 HeapFree(GetProcessHeap(), 0, lpwhr->lpszHostName);
1386 lpwhr->lpszHostName=WININET_strdupW(hostName);
1388 SendAsyncCallback(hIC, &lpwhs->hdr, lpwhr->hdr.dwContext,
1389 INTERNET_STATUS_RESOLVING_NAME,
1390 lpwhs->lpszServerName,
1391 strlenW(lpwhs->lpszServerName)+1);
1393 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1394 &lpwhs->phostent, &lpwhs->socketAddress))
1396 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1400 SendAsyncCallback(hIC, &lpwhs->hdr, lpwhr->hdr.dwContext,
1401 INTERNET_STATUS_NAME_RESOLVED,
1402 &(lpwhs->socketAddress),
1403 sizeof(struct sockaddr_in));
1408 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1409 lpwhr->lpszPath=NULL;
1415 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
1416 if (rc != E_POINTER)
1417 needed = strlenW(path)+1;
1418 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
1419 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
1420 URL_ESCAPE_SPACES_ONLY);
1423 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(path),rc);
1424 strcpyW(lpwhr->lpszPath,path);
1428 return HTTP_HttpSendRequestW(lpwhr, lpszHeaders, dwHeaderLength, lpOptional, dwOptionalLength);
1431 /***********************************************************************
1432 * HTTP_build_req (internal)
1434 * concatenate all the strings in the request together
1436 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
1441 for( t = list; *t ; t++ )
1442 len += strlenW( *t );
1445 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1448 for( t = list; *t ; t++ )
1454 /***********************************************************************
1455 * HTTP_HttpSendRequestW (internal)
1457 * Sends the specified request to the HTTP server
1464 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
1465 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1469 BOOL bSuccess = FALSE;
1470 LPWSTR requestString = NULL;
1472 LPWININETHTTPSESSIONW lpwhs = NULL;
1473 LPWININETAPPINFOW hIC = NULL;
1474 BOOL loop_next = FALSE;
1475 int CustHeaderIndex;
1477 TRACE("--> %p\n", lpwhr);
1479 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
1481 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1482 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1484 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1488 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1489 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1491 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1495 /* Clear any error information */
1496 INTERNET_SetLastError(0);
1499 /* We must have a verb */
1500 if (NULL == lpwhr->lpszVerb)
1505 /* if we are using optional stuff, we must add the fixed header of that option length */
1506 if (lpOptional && dwOptionalLength)
1508 static const WCHAR szContentLength[] = {
1509 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0};
1510 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \n\r */ + 20 /* int */ ];
1511 sprintfW(contentLengthStr, szContentLength, dwOptionalLength);
1512 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD);
1517 static const WCHAR szSlash[] = { '/',0 };
1518 static const WCHAR szSpace[] = { ' ',0 };
1519 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
1520 static const WCHAR szcrlf[] = {'\r','\n', 0};
1521 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
1522 static const WCHAR szSetCookie[] = {'S','e','t','-','C','o','o','k','i','e',0 };
1523 static const WCHAR szColon[] = { ':',' ',0 };
1529 TRACE("Going to url %s %s\n", debugstr_w(lpwhr->lpszHostName), debugstr_w(lpwhr->lpszPath));
1532 /* If we don't have a path we set it to root */
1533 if (NULL == lpwhr->lpszPath)
1534 lpwhr->lpszPath = WININET_strdupW(szSlash);
1536 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1537 lpwhr->lpszPath, strlenW(szHttp), szHttp, strlenW(szHttp) )
1538 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
1540 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
1541 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
1543 strcpyW(fixurl + 1, lpwhr->lpszPath);
1544 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
1545 lpwhr->lpszPath = fixurl;
1548 /* add the headers the caller supplied */
1549 if( lpszHeaders && dwHeaderLength )
1551 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
1552 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
1555 /* allocate space for an array of all the string pointers to be added */
1556 len = (HTTP_QUERY_MAX + lpwhr->nCustHeaders)*4 + 9;
1557 req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
1559 /* add the verb, path and HTTP/1.0 */
1561 req[n++] = lpwhr->lpszVerb;
1563 req[n++] = lpwhr->lpszPath;
1564 req[n++] = HTTPHEADER;
1566 /* Append standard request headers */
1567 for (i = 0; i <= HTTP_QUERY_MAX; i++)
1569 if (lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST)
1572 req[n++] = lpwhr->StdHeaders[i].lpszField;
1574 req[n++] = lpwhr->StdHeaders[i].lpszValue;
1576 TRACE("Adding header %s (%s)\n",
1577 debugstr_w(lpwhr->StdHeaders[i].lpszField),
1578 debugstr_w(lpwhr->StdHeaders[i].lpszValue));
1582 /* Append custom request heades */
1583 for (i = 0; i < lpwhr->nCustHeaders; i++)
1585 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
1588 req[n++] = lpwhr->pCustHeaders[i].lpszField;
1590 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
1592 TRACE("Adding custom header %s (%s)\n",
1593 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
1594 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
1598 if (lpwhr->lpszHostName)
1600 req[n++] = HTTPHOSTHEADER;
1601 req[n++] = lpwhr->lpszHostName;
1605 ERR("oops. buffer overrun\n");
1608 requestString = HTTP_build_req( req, 4 );
1609 HeapFree( GetProcessHeap(), 0, req );
1612 * Set (header) termination string for request
1613 * Make sure there's exactly two new lines at the end of the request
1615 p = &requestString[strlenW(requestString)-1];
1616 while ( (*p == '\n') || (*p == '\r') )
1618 strcpyW( p+1, sztwocrlf );
1620 TRACE("Request header -> %s\n", debugstr_w(requestString) );
1622 /* Send the request and store the results */
1623 if (!HTTP_OpenConnection(lpwhr))
1626 /* send the request as ASCII, tack on the optional data */
1628 dwOptionalLength = 0;
1629 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
1630 NULL, 0, NULL, NULL );
1631 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
1632 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
1633 ascii_req, len, NULL, NULL );
1635 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
1636 len = (len + dwOptionalLength - 1);
1638 TRACE("full request -> %s\n", ascii_req );
1640 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1641 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
1643 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
1644 HeapFree( GetProcessHeap(), 0, ascii_req );
1646 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1647 INTERNET_STATUS_REQUEST_SENT,
1648 &len,sizeof(DWORD));
1650 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1651 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
1656 responseLen = HTTP_GetResponseHeaders(lpwhr);
1660 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1661 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
1664 /* process headers here. Is this right? */
1665 CustHeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSetCookie);
1666 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && (CustHeaderIndex >= 0))
1668 LPHTTPHEADERW setCookieHeader;
1669 int nPosStart = 0, nPosEnd = 0, len;
1670 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
1672 setCookieHeader = &lpwhr->pCustHeaders[CustHeaderIndex];
1674 while (setCookieHeader->lpszValue[nPosEnd] != '\0')
1676 LPWSTR buf_cookie, cookie_name, cookie_data;
1678 LPWSTR domain = NULL;
1680 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
1681 setCookieHeader->lpszValue[nPosEnd] != '\0')
1685 if (setCookieHeader->lpszValue[nPosEnd] == ';')
1687 /* fixme: not case sensitive, strcasestr is gnu only */
1688 int nDomainPosEnd = 0;
1689 int nDomainPosStart = 0, nDomainLength = 0;
1690 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
1691 LPWSTR lpszDomain = strstrW(&setCookieHeader->lpszValue[nPosEnd], szDomain);
1693 { /* they have specified their own domain, lets use it */
1694 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
1695 lpszDomain[nDomainPosEnd] != '\0')
1699 nDomainPosStart = strlenW(szDomain);
1700 nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
1701 domain = HeapAlloc(GetProcessHeap(), 0, (nDomainLength + 1)*sizeof(WCHAR));
1702 strncpyW(domain, &lpszDomain[nDomainPosStart], nDomainLength);
1703 domain[nDomainLength] = '\0';
1706 if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
1707 buf_cookie = HeapAlloc(GetProcessHeap(), 0, ((nPosEnd - nPosStart) + 1)*sizeof(WCHAR));
1708 strncpyW(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart));
1709 buf_cookie[(nPosEnd - nPosStart)] = '\0';
1710 TRACE("%s\n", debugstr_w(buf_cookie));
1711 while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
1715 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
1717 HeapFree(GetProcessHeap(), 0, buf_cookie);
1721 cookie_name = HeapAlloc(GetProcessHeap(), 0, (nEqualPos + 1)*sizeof(WCHAR));
1722 strncpyW(cookie_name, buf_cookie, nEqualPos);
1723 cookie_name[nEqualPos] = '\0';
1724 cookie_data = &buf_cookie[nEqualPos + 1];
1727 len = strlenW((domain ? domain : lpwhr->lpszHostName)) + strlenW(lpwhr->lpszPath) + 9;
1728 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1729 sprintfW(buf_url, szFmt, (domain ? domain : lpwhr->lpszHostName)); /* FIXME PATH!!! */
1730 InternetSetCookieW(buf_url, cookie_name, cookie_data);
1732 HeapFree(GetProcessHeap(), 0, buf_url);
1733 HeapFree(GetProcessHeap(), 0, buf_cookie);
1734 HeapFree(GetProcessHeap(), 0, cookie_name);
1735 if (domain) HeapFree(GetProcessHeap(), 0, domain);
1736 nPosStart = nPosEnd;
1745 HeapFree(GetProcessHeap(), 0, requestString);
1747 /* TODO: send notification for P3P header */
1749 if(!(hIC->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
1751 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
1752 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
1753 (dwCode==302 || dwCode==301))
1755 WCHAR szNewLocation[2048];
1756 DWORD dwBufferSize=2048;
1758 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
1760 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1761 INTERNET_STATUS_REDIRECT, szNewLocation,
1763 return HTTP_HandleRedirect(lpwhr, szNewLocation, lpszHeaders,
1764 dwHeaderLength, lpOptional, dwOptionalLength);
1769 if (hIC->lpfnStatusCB)
1771 INTERNET_ASYNC_RESULT iar;
1773 iar.dwResult = (DWORD)bSuccess;
1774 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1776 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1777 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1778 sizeof(INTERNET_ASYNC_RESULT));
1786 /***********************************************************************
1787 * HTTP_Connect (internal)
1789 * Create http session handle
1792 * HINTERNET a session handle on success
1796 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
1797 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
1798 LPCWSTR lpszPassword, DWORD dwFlags, DWORD dwContext,
1799 DWORD dwInternalFlags)
1801 BOOL bSuccess = FALSE;
1802 LPWININETHTTPSESSIONW lpwhs = NULL;
1803 HINTERNET handle = NULL;
1807 assert( hIC->hdr.htype == WH_HINIT );
1809 hIC->hdr.dwContext = dwContext;
1811 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
1814 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1819 * According to my tests. The name is not resolved until a request is sent
1822 if (nServerPort == INTERNET_INVALID_PORT_NUMBER)
1823 nServerPort = INTERNET_DEFAULT_HTTP_PORT;
1825 lpwhs->hdr.htype = WH_HHTTPSESSION;
1826 lpwhs->hdr.lpwhparent = WININET_AddRef( &hIC->hdr );
1827 lpwhs->hdr.dwFlags = dwFlags;
1828 lpwhs->hdr.dwContext = dwContext;
1829 lpwhs->hdr.dwInternalFlags = dwInternalFlags;
1830 lpwhs->hdr.dwRefCount = 1;
1831 lpwhs->hdr.destroy = HTTP_CloseHTTPSessionHandle;
1833 handle = WININET_AllocHandle( &lpwhs->hdr );
1836 ERR("Failed to alloc handle\n");
1837 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1841 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
1842 if(strchrW(hIC->lpszProxy, ' '))
1843 FIXME("Several proxies not implemented.\n");
1844 if(hIC->lpszProxyBypass)
1845 FIXME("Proxy bypass is ignored.\n");
1847 if (NULL != lpszServerName)
1848 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
1849 if (NULL != lpszUserName)
1850 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
1851 lpwhs->nServerPort = nServerPort;
1853 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
1854 if (hIC->lpfnStatusCB && !(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
1856 INTERNET_ASYNC_RESULT iar;
1858 iar.dwResult = (DWORD)handle;
1859 iar.dwError = ERROR_SUCCESS;
1861 SendAsyncCallback(hIC, &hIC->hdr, dwContext,
1862 INTERNET_STATUS_HANDLE_CREATED, &iar,
1863 sizeof(INTERNET_ASYNC_RESULT));
1870 WININET_Release( &lpwhs->hdr );
1873 * a INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
1877 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
1882 /***********************************************************************
1883 * HTTP_OpenConnection (internal)
1885 * Connect to a web server
1892 BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
1894 BOOL bSuccess = FALSE;
1895 LPWININETHTTPSESSIONW lpwhs;
1896 LPWININETAPPINFOW hIC = NULL;
1901 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1903 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1907 lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
1909 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1910 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1911 INTERNET_STATUS_CONNECTING_TO_SERVER,
1912 &(lpwhs->socketAddress),
1913 sizeof(struct sockaddr_in));
1915 if (!NETCON_create(&lpwhr->netConnection, lpwhs->phostent->h_addrtype,
1918 WARN("Socket creation failed\n");
1922 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
1923 sizeof(lpwhs->socketAddress)))
1925 WARN("Unable to connect to host (%s)\n", strerror(errno));
1929 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1930 INTERNET_STATUS_CONNECTED_TO_SERVER,
1931 &(lpwhs->socketAddress),
1932 sizeof(struct sockaddr_in));
1937 TRACE("%d <--\n", bSuccess);
1942 /***********************************************************************
1943 * HTTP_clear_response_headers (internal)
1945 * clear out any old response headers
1947 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
1951 for( i=0; i<=HTTP_QUERY_MAX; i++ )
1953 if( !lpwhr->StdHeaders[i].lpszField )
1955 if( !lpwhr->StdHeaders[i].lpszValue )
1957 if ( lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST )
1959 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[i], NULL );
1961 for( i=0; i<lpwhr->nCustHeaders; i++)
1963 if( !lpwhr->pCustHeaders[i].lpszField )
1965 if( !lpwhr->pCustHeaders[i].lpszValue )
1967 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
1969 HTTP_ReplaceHeaderValue( &lpwhr->pCustHeaders[i], NULL );
1973 /***********************************************************************
1974 * HTTP_GetResponseHeaders (internal)
1976 * Read server response
1983 BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr)
1986 WCHAR buffer[MAX_REPLY_LEN];
1987 DWORD buflen = MAX_REPLY_LEN;
1988 BOOL bSuccess = FALSE;
1990 WCHAR value[MAX_FIELD_VALUE_LEN], field[MAX_FIELD_LEN];
1991 static const WCHAR szHttp[] = { 'H','T','T','P',0 };
1992 static const WCHAR szCrLf[] = {'\r','\n',0};
1993 char bufferA[MAX_REPLY_LEN];
1994 LPWSTR status_code, status_text;
1995 DWORD cchMaxRawHeaders = 1024;
1996 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
1997 DWORD cchRawHeaders = 0;
2001 /* clear old response headers (eg. from a redirect response) */
2002 HTTP_clear_response_headers( lpwhr );
2004 if (!NETCON_connected(&lpwhr->netConnection))
2008 * HACK peek at the buffer
2010 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
2013 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
2015 buflen = MAX_REPLY_LEN;
2016 memset(buffer, 0, MAX_REPLY_LEN);
2017 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2019 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2021 if (strncmpW(buffer, szHttp, 4) != 0)
2024 /* regenerate raw headers */
2025 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2027 cchMaxRawHeaders *= 2;
2028 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2030 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2031 cchRawHeaders += (buflen-1);
2032 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2033 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2034 lpszRawHeaders[cchRawHeaders] = '\0';
2036 /* split the version from the status code */
2037 status_code = strchrW( buffer, ' ' );
2042 /* split the status code from the status text */
2043 status_text = strchrW( status_code, ' ' );
2048 TRACE("version [%s] status code [%s] status text [%s]\n",
2049 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
2050 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_VERSION], buffer );
2051 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_STATUS_CODE], status_code );
2052 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_STATUS_TEXT], status_text );
2054 /* Parse each response line */
2057 buflen = MAX_REPLY_LEN;
2058 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2060 TRACE("got line %s, now interpretting\n", debugstr_a(bufferA));
2061 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2063 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2065 cchMaxRawHeaders *= 2;
2066 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2068 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2069 cchRawHeaders += (buflen-1);
2070 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2071 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2072 lpszRawHeaders[cchRawHeaders] = '\0';
2074 if (!HTTP_InterpretHttpHeader(buffer, field, MAX_FIELD_LEN, value, MAX_FIELD_VALUE_LEN))
2077 HTTP_ProcessHeader(lpwhr, field, value, (HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE));
2087 if (lpwhr->lpszRawHeaders) HeapFree(GetProcessHeap(), 0, lpszRawHeaders);
2088 lpwhr->lpszRawHeaders = lpszRawHeaders;
2089 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
2102 /***********************************************************************
2103 * HTTP_InterpretHttpHeader (internal)
2105 * Parse server response
2112 static INT stripSpaces(LPCWSTR lpszSrc, LPWSTR lpszStart, INT *len)
2119 while (*lpszSrc == ' ' && *lpszSrc != '\0')
2123 while(*lpsztmp != '\0')
2125 if (*lpsztmp != ' ')
2126 srclen = lpsztmp - lpszSrc + 1;
2131 *len = min(*len, srclen);
2132 strncpyW(lpszStart, lpszSrc, *len);
2133 lpszStart[*len] = '\0';
2139 BOOL HTTP_InterpretHttpHeader(LPWSTR buffer, LPWSTR field, INT fieldlen, LPWSTR value, INT valuelen)
2142 BOOL bSuccess = FALSE;
2149 pd = strchrW(buffer, ':');
2153 if (stripSpaces(buffer, field, &fieldlen) > 0)
2155 if (stripSpaces(pd+1, value, &valuelen) > 0)
2160 TRACE("%d: field(%s) Value(%s)\n", bSuccess, debugstr_w(field), debugstr_w(value));
2165 /***********************************************************************
2166 * HTTP_GetStdHeaderIndex (internal)
2168 * Lookup field index in standard http header array
2170 * FIXME: This should be stuffed into a hash table
2172 INT HTTP_GetStdHeaderIndex(LPCWSTR lpszField)
2175 static const WCHAR szContentLength[] = {
2176 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
2177 static const WCHAR szContentType[] = {
2178 'C','o','n','t','e','n','t','-','T','y','p','e',0};
2179 static const WCHAR szLastModified[] = {
2180 'L','a','s','t','-','M','o','d','i','f','i','e','d',0};
2181 static const WCHAR szLocation[] = {'L','o','c','a','t','i','o','n',0};
2182 static const WCHAR szAccept[] = {'A','c','c','e','p','t',0};
2183 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0};
2184 static const WCHAR szContentTrans[] = { 'C','o','n','t','e','n','t','-',
2185 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0};
2186 static const WCHAR szDate[] = { 'D','a','t','e',0};
2187 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0};
2188 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0};
2189 static const WCHAR szETag[] = { 'E','T','a','g',0};
2190 static const WCHAR szAcceptRanges[] = {
2191 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
2192 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
2193 static const WCHAR szMimeVersion[] = {
2194 'M','i','m','e','-','V','e','r','s','i','o','n', 0};
2195 static const WCHAR szPragma[] = { 'P','r','a','g','m','a', 0};
2196 static const WCHAR szCacheControl[] = {
2197 'C','a','c','h','e','-','C','o','n','t','r','o','l',0};
2198 static const WCHAR szUserAgent[] = { 'U','s','e','r','-','A','g','e','n','t',0};
2199 static const WCHAR szProxyAuth[] = {
2200 'P','r','o','x','y','-',
2201 'A','u','t','h','e','n','t','i','c','a','t','e', 0};
2202 static const WCHAR szContentEncoding[] = {
2203 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0};
2204 static const WCHAR szCookie[] = {'C','o','o','k','i','e',0};
2205 static const WCHAR szVary[] = {'V','a','r','y',0};
2206 static const WCHAR szVia[] = {'V','i','a',0};
2208 if (!strcmpiW(lpszField, szContentLength))
2209 index = HTTP_QUERY_CONTENT_LENGTH;
2210 else if (!strcmpiW(lpszField,szContentType))
2211 index = HTTP_QUERY_CONTENT_TYPE;
2212 else if (!strcmpiW(lpszField,szLastModified))
2213 index = HTTP_QUERY_LAST_MODIFIED;
2214 else if (!strcmpiW(lpszField,szLocation))
2215 index = HTTP_QUERY_LOCATION;
2216 else if (!strcmpiW(lpszField,szAccept))
2217 index = HTTP_QUERY_ACCEPT;
2218 else if (!strcmpiW(lpszField,szReferer))
2219 index = HTTP_QUERY_REFERER;
2220 else if (!strcmpiW(lpszField,szContentTrans))
2221 index = HTTP_QUERY_CONTENT_TRANSFER_ENCODING;
2222 else if (!strcmpiW(lpszField,szDate))
2223 index = HTTP_QUERY_DATE;
2224 else if (!strcmpiW(lpszField,szServer))
2225 index = HTTP_QUERY_SERVER;
2226 else if (!strcmpiW(lpszField,szConnection))
2227 index = HTTP_QUERY_CONNECTION;
2228 else if (!strcmpiW(lpszField,szETag))
2229 index = HTTP_QUERY_ETAG;
2230 else if (!strcmpiW(lpszField,szAcceptRanges))
2231 index = HTTP_QUERY_ACCEPT_RANGES;
2232 else if (!strcmpiW(lpszField,szExpires))
2233 index = HTTP_QUERY_EXPIRES;
2234 else if (!strcmpiW(lpszField,szMimeVersion))
2235 index = HTTP_QUERY_MIME_VERSION;
2236 else if (!strcmpiW(lpszField,szPragma))
2237 index = HTTP_QUERY_PRAGMA;
2238 else if (!strcmpiW(lpszField,szCacheControl))
2239 index = HTTP_QUERY_CACHE_CONTROL;
2240 else if (!strcmpiW(lpszField,szUserAgent))
2241 index = HTTP_QUERY_USER_AGENT;
2242 else if (!strcmpiW(lpszField,szProxyAuth))
2243 index = HTTP_QUERY_PROXY_AUTHENTICATE;
2244 else if (!strcmpiW(lpszField,szContentEncoding))
2245 index = HTTP_QUERY_CONTENT_ENCODING;
2246 else if (!strcmpiW(lpszField,szCookie))
2247 index = HTTP_QUERY_COOKIE;
2248 else if (!strcmpiW(lpszField,szVary))
2249 index = HTTP_QUERY_VARY;
2250 else if (!strcmpiW(lpszField,szVia))
2251 index = HTTP_QUERY_VIA;
2254 TRACE("Couldn't find %s in standard header table\n", debugstr_w(lpszField));
2260 /***********************************************************************
2261 * HTTP_ReplaceHeaderValue (internal)
2263 BOOL HTTP_ReplaceHeaderValue( LPHTTPHEADERW lphttpHdr, LPCWSTR value )
2267 if( lphttpHdr->lpszValue )
2268 HeapFree( GetProcessHeap(), 0, lphttpHdr->lpszValue );
2269 lphttpHdr->lpszValue = NULL;
2272 len = strlenW(value);
2275 lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0,
2276 (len+1)*sizeof(WCHAR));
2277 strcpyW(lphttpHdr->lpszValue, value);
2282 /***********************************************************************
2283 * HTTP_ProcessHeader (internal)
2285 * Stuff header into header tables according to <dwModifier>
2289 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2291 BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
2293 LPHTTPHEADERW lphttpHdr = NULL;
2294 BOOL bSuccess = FALSE;
2297 TRACE("--> %s: %s - 0x%08lx\n", debugstr_w(field), debugstr_w(value), dwModifier);
2299 /* Adjust modifier flags */
2300 if (dwModifier & COALESCEFLASG)
2301 dwModifier |= HTTP_ADDHDR_FLAG_ADD;
2303 /* Try to get index into standard header array */
2304 index = HTTP_GetStdHeaderIndex(field);
2307 lphttpHdr = &lpwhr->StdHeaders[index];
2309 else /* Find or create new custom header */
2311 index = HTTP_GetCustomHeaderIndex(lpwhr, field);
2314 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
2318 lphttpHdr = &lpwhr->pCustHeaders[index];
2324 hdr.lpszField = (LPWSTR)field;
2325 hdr.lpszValue = (LPWSTR)value;
2326 hdr.wFlags = hdr.wCount = 0;
2328 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2329 hdr.wFlags |= HDR_ISREQUEST;
2331 return HTTP_InsertCustomHeader(lpwhr, &hdr);
2335 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2336 lphttpHdr->wFlags |= HDR_ISREQUEST;
2338 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
2340 if (!lphttpHdr->lpszValue && (dwModifier & (HTTP_ADDHDR_FLAG_ADD|HTTP_ADDHDR_FLAG_ADD_IF_NEW)))
2344 if (!lpwhr->StdHeaders[index].lpszField)
2346 lphttpHdr->lpszField = WININET_strdupW(field);
2348 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2349 lphttpHdr->wFlags |= HDR_ISREQUEST;
2352 slen = strlenW(value) + 1;
2353 lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0, slen*sizeof(WCHAR));
2354 if (lphttpHdr->lpszValue)
2356 strcpyW(lphttpHdr->lpszValue, value);
2361 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2364 else if (lphttpHdr->lpszValue)
2366 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
2367 bSuccess = HTTP_ReplaceHeaderValue( lphttpHdr, value );
2368 else if (dwModifier & COALESCEFLASG)
2373 INT origlen = strlenW(lphttpHdr->lpszValue);
2374 INT valuelen = strlenW(value);
2376 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
2379 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2381 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2384 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2387 len = origlen + valuelen + ((ch > 0) ? 1 : 0);
2389 lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
2392 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
2395 lphttpHdr->lpszValue[origlen] = ch;
2399 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
2400 lphttpHdr->lpszValue[len] = '\0';
2405 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
2406 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2410 TRACE("<-- %d\n",bSuccess);
2415 /***********************************************************************
2416 * HTTP_CloseConnection (internal)
2418 * Close socket connection
2421 VOID HTTP_CloseConnection(LPWININETHTTPREQW lpwhr)
2423 LPWININETHTTPSESSIONW lpwhs = NULL;
2424 LPWININETAPPINFOW hIC = NULL;
2426 TRACE("%p\n",lpwhr);
2428 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
2429 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2431 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
2432 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2434 if (NETCON_connected(&lpwhr->netConnection))
2436 NETCON_close(&lpwhr->netConnection);
2439 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
2440 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2444 /***********************************************************************
2445 * HTTP_CloseHTTPRequestHandle (internal)
2447 * Deallocate request handle
2450 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr)
2453 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
2457 if (NETCON_connected(&lpwhr->netConnection))
2458 HTTP_CloseConnection(lpwhr);
2460 if (lpwhr->lpszPath)
2461 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2462 if (lpwhr->lpszVerb)
2463 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
2464 if (lpwhr->lpszHostName)
2465 HeapFree(GetProcessHeap(), 0, lpwhr->lpszHostName);
2466 if (lpwhr->lpszRawHeaders)
2467 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2469 for (i = 0; i <= HTTP_QUERY_MAX; i++)
2471 if (lpwhr->StdHeaders[i].lpszField)
2472 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszField);
2473 if (lpwhr->StdHeaders[i].lpszValue)
2474 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszValue);
2477 for (i = 0; i < lpwhr->nCustHeaders; i++)
2479 if (lpwhr->pCustHeaders[i].lpszField)
2480 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
2481 if (lpwhr->pCustHeaders[i].lpszValue)
2482 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
2485 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
2486 HeapFree(GetProcessHeap(), 0, lpwhr);
2490 /***********************************************************************
2491 * HTTP_CloseHTTPSessionHandle (internal)
2493 * Deallocate session handle
2496 void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr)
2498 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
2500 TRACE("%p\n", lpwhs);
2502 if (lpwhs->lpszServerName)
2503 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2504 if (lpwhs->lpszUserName)
2505 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2506 HeapFree(GetProcessHeap(), 0, lpwhs);
2510 /***********************************************************************
2511 * HTTP_GetCustomHeaderIndex (internal)
2513 * Return index of custom header from header array
2516 INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField)
2520 TRACE("%s\n", debugstr_w(lpszField));
2522 for (index = 0; index < lpwhr->nCustHeaders; index++)
2524 if (!strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
2529 if (index >= lpwhr->nCustHeaders)
2532 TRACE("Return: %lu\n", index);
2537 /***********************************************************************
2538 * HTTP_InsertCustomHeader (internal)
2540 * Insert header into array
2543 BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
2546 LPHTTPHEADERW lph = NULL;
2549 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
2550 count = lpwhr->nCustHeaders + 1;
2552 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
2554 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
2558 lpwhr->pCustHeaders = lph;
2559 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
2560 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
2561 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
2562 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
2563 lpwhr->nCustHeaders++;
2568 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2575 /***********************************************************************
2576 * HTTP_DeleteCustomHeader (internal)
2578 * Delete header from array
2579 * If this function is called, the indexs may change.
2581 BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
2583 if( lpwhr->nCustHeaders <= 0 )
2585 if( lpwhr->nCustHeaders >= index )
2587 lpwhr->nCustHeaders--;
2589 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
2590 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
2591 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
2596 /***********************************************************************
2597 * IsHostInProxyBypassList (@)
2602 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
2604 FIXME("STUB: flags=%ld host=%s length=%ld\n",flags,szHost,length);