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
30 #include <sys/types.h>
31 #ifdef HAVE_SYS_SOCKET_H
32 # include <sys/socket.h>
50 #define NO_SHLWAPI_STREAM
54 #include "wine/debug.h"
55 #include "wine/unicode.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
59 static const WCHAR g_szHttp[] = {' ','H','T','T','P','/','1','.','0',0 };
60 static const WCHAR g_szHost[] = {'\r','\n','H','o','s','t',':',' ',0 };
61 static const WCHAR g_szReferer[] = {'R','e','f','e','r','e','r',0};
62 static const WCHAR g_szAccept[] = {'A','c','c','e','p','t',0};
63 static const WCHAR g_szUserAgent[] = {'U','s','e','r','-','A','g','e','n','t',0};
66 #define HTTPHEADER g_szHttp
67 #define HTTPHOSTHEADER g_szHost
68 #define MAXHOSTNAME 100
69 #define MAX_FIELD_VALUE_LEN 256
70 #define MAX_FIELD_LEN 256
72 #define HTTP_REFERER g_szReferer
73 #define HTTP_ACCEPT g_szAccept
74 #define HTTP_USERAGENT g_szUserAgent
76 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
77 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
78 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
79 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
80 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
81 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
82 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
85 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr);
86 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr);
87 BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr);
88 int HTTP_WriteDataToStream(LPWININETHTTPREQW lpwhr,
89 void *Buffer, int BytesToWrite);
90 int HTTP_ReadDataFromStream(LPWININETHTTPREQW lpwhr,
91 void *Buffer, int BytesToRead);
92 BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr);
93 BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
94 BOOL HTTP_ReplaceHeaderValue( LPHTTPHEADERW lphttpHdr, LPCWSTR lpsztmp );
95 void HTTP_CloseConnection(LPWININETHTTPREQW lpwhr);
96 BOOL HTTP_InterpretHttpHeader(LPWSTR buffer, LPWSTR field, INT fieldlen, LPWSTR value, INT valuelen);
97 INT HTTP_GetStdHeaderIndex(LPCWSTR lpszField);
98 BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr);
99 INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField);
100 BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, INT index);
102 /***********************************************************************
103 * HTTP_Tokenize (internal)
105 * Tokenize a string, allocating memory for the tokens.
107 static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
109 LPWSTR * token_array;
114 /* empty string has no tokens */
118 for (i = 0; string[i]; i++)
119 if (!strncmpW(string+i, token_string, strlenW(token_string)))
123 /* we want to skip over separators, but not the null terminator */
124 for (j = 0; j < strlenW(token_string) - 1; j++)
130 /* add 1 for terminating NULL */
131 token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
132 token_array[tokens] = NULL;
135 for (i = 0; i < tokens; i++)
138 next_token = strstrW(string, token_string);
139 if (!next_token) next_token = string+strlenW(string);
140 len = next_token - string;
141 token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
142 memcpy(token_array[i], string, len*sizeof(WCHAR));
143 token_array[i][len] = '\0';
144 string = next_token+strlenW(token_string);
149 /***********************************************************************
150 * HTTP_FreeTokens (internal)
152 * Frees memory returned from HTTP_Tokenize.
154 static void HTTP_FreeTokens(LPWSTR * token_array)
157 for (i = 0; token_array[i]; i++)
158 HeapFree(GetProcessHeap(), 0, token_array[i]);
159 HeapFree(GetProcessHeap(), 0, token_array);
162 /***********************************************************************
163 * HTTP_HttpAddRequestHeadersW (internal)
165 static BOOL WINAPI HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
166 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
171 WCHAR value[MAX_FIELD_VALUE_LEN], field[MAX_FIELD_LEN];
172 BOOL bSuccess = FALSE;
174 TRACE("copying header: %s\n", debugstr_w(lpszHeader));
175 buffer = WININET_strdupW(lpszHeader);
182 while (*lpszEnd != '\0')
184 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
189 if (*lpszStart == '\0')
192 if (*lpszEnd == '\r')
195 lpszEnd += 2; /* Jump over \r\n */
197 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
198 if (HTTP_InterpretHttpHeader(lpszStart, field, MAX_FIELD_LEN, value, MAX_FIELD_VALUE_LEN))
199 bSuccess = HTTP_ProcessHeader(lpwhr, field, value, dwModifier | HTTP_ADDHDR_FLAG_REQ);
205 HeapFree(GetProcessHeap(), 0, buffer);
210 /***********************************************************************
211 * HttpAddRequestHeadersW (WININET.@)
213 * Adds one or more HTTP header to the request handler
220 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
221 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
223 BOOL bSuccess = FALSE;
224 LPWININETHTTPREQW lpwhr;
226 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_w(lpszHeader), dwHeaderLength,
232 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
233 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
235 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
238 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
241 WININET_Release( &lpwhr->hdr );
246 /***********************************************************************
247 * HttpAddRequestHeadersA (WININET.@)
249 * Adds one or more HTTP header to the request handler
256 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
257 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
263 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_a(lpszHeader), dwHeaderLength,
266 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
267 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
268 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
269 if( dwHeaderLength != -1 )
270 dwHeaderLength = len;
272 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
274 HeapFree( GetProcessHeap(), 0, hdr );
279 /***********************************************************************
280 * HttpEndRequestA (WININET.@)
282 * Ends an HTTP request that was started by HttpSendRequestEx
289 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest, LPINTERNET_BUFFERSA lpBuffersOut,
290 DWORD dwFlags, DWORD dwContext)
296 /***********************************************************************
297 * HttpEndRequestW (WININET.@)
299 * Ends an HTTP request that was started by HttpSendRequestEx
306 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest, LPINTERNET_BUFFERSW lpBuffersOut,
307 DWORD dwFlags, DWORD dwContext)
313 /***********************************************************************
314 * HttpOpenRequestW (WININET.@)
316 * Open a HTTP request handle
319 * HINTERNET a HTTP request handle on success
323 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
324 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
325 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
326 DWORD dwFlags, DWORD dwContext)
328 LPWININETHTTPSESSIONW lpwhs;
329 LPWININETAPPINFOW hIC = NULL;
330 HINTERNET handle = NULL;
332 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
333 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
334 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
336 if(lpszAcceptTypes!=NULL)
339 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
340 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
343 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
344 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
346 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
349 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
352 * My tests seem to show that the windows version does not
353 * become asynchronous until after this point. And anyhow
354 * if this call was asynchronous then how would you get the
355 * necessary HINTERNET pointer returned by this function.
358 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
359 lpszVersion, lpszReferrer, lpszAcceptTypes,
363 WININET_Release( &lpwhs->hdr );
364 TRACE("returning %p\n", handle);
369 /***********************************************************************
370 * HttpOpenRequestA (WININET.@)
372 * Open a HTTP request handle
375 * HINTERNET a HTTP request handle on success
379 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
380 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
381 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
382 DWORD dwFlags, DWORD dwContext)
384 LPWSTR szVerb = NULL, szObjectName = NULL;
385 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
387 INT acceptTypesCount;
388 HINTERNET rc = FALSE;
389 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
390 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
391 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
396 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
397 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
400 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
405 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
406 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
409 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
414 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
415 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
418 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
423 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
424 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
427 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
430 acceptTypesCount = 0;
433 /* find out how many there are */
434 while (lpszAcceptTypes[acceptTypesCount])
436 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
437 acceptTypesCount = 0;
438 while (lpszAcceptTypes[acceptTypesCount])
440 len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
442 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
443 if (!szAcceptTypes[acceptTypesCount] )
445 MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
446 -1, szAcceptTypes[acceptTypesCount], len );
449 szAcceptTypes[acceptTypesCount] = NULL;
451 else szAcceptTypes = 0;
453 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
454 szVersion, szReferrer,
455 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
460 acceptTypesCount = 0;
461 while (szAcceptTypes[acceptTypesCount])
463 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
466 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
468 if (szReferrer) HeapFree(GetProcessHeap(), 0, szReferrer);
469 if (szVersion) HeapFree(GetProcessHeap(), 0, szVersion);
470 if (szObjectName) HeapFree(GetProcessHeap(), 0, szObjectName);
471 if (szVerb) HeapFree(GetProcessHeap(), 0, szVerb);
476 /***********************************************************************
479 static UINT HTTP_Base64( LPCWSTR bin, LPWSTR base64 )
482 static LPSTR HTTP_Base64Enc =
483 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
487 /* first 6 bits, all from bin[0] */
488 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
489 x = (bin[0] & 3) << 4;
491 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
494 base64[n++] = HTTP_Base64Enc[x];
499 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
500 x = ( bin[1] & 0x0f ) << 2;
502 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
505 base64[n++] = HTTP_Base64Enc[x];
509 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
511 /* last 6 bits, all from bin [2] */
512 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
519 /***********************************************************************
520 * HTTP_EncodeBasicAuth
522 * Encode the basic authentication string for HTTP 1.1
524 static LPWSTR HTTP_EncodeBasicAuth( LPCWSTR username, LPCWSTR password)
528 static const WCHAR szBasic[] = {'B','a','s','i','c',' ',0};
529 static const WCHAR szColon[] = {':',0};
531 len = lstrlenW( username ) + 1 + lstrlenW ( password ) + 1;
532 in = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
536 len = lstrlenW(szBasic) +
537 (lstrlenW( username ) + 1 + lstrlenW ( password ))*2 + 1 + 1;
538 out = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
541 lstrcpyW( in, username );
542 lstrcatW( in, szColon );
543 lstrcatW( in, password );
544 lstrcpyW( out, szBasic );
545 HTTP_Base64( in, &out[strlenW(out)] );
547 HeapFree( GetProcessHeap(), 0, in );
552 /***********************************************************************
553 * HTTP_InsertProxyAuthorization
555 * Insert the basic authorization field in the request header
557 BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
558 LPCWSTR username, LPCWSTR password )
562 static const WCHAR szProxyAuthorization[] = {
563 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
565 hdr.lpszValue = HTTP_EncodeBasicAuth( username, password );
566 hdr.lpszField = (WCHAR *)szProxyAuthorization;
567 hdr.wFlags = HDR_ISREQUEST;
572 TRACE("Inserting %s = %s\n",
573 debugstr_w( hdr.lpszField ), debugstr_w( hdr.lpszValue ) );
575 /* remove the old proxy authorization header */
576 index = HTTP_GetCustomHeaderIndex( lpwhr, hdr.lpszField );
578 HTTP_DeleteCustomHeader( lpwhr, index );
580 HTTP_InsertCustomHeader(lpwhr, &hdr);
581 HeapFree( GetProcessHeap(), 0, hdr.lpszValue );
586 /***********************************************************************
589 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
590 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
592 WCHAR buf[MAXHOSTNAME];
593 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
595 static const WCHAR szNul[] = { 0 };
596 URL_COMPONENTSW UrlComponents;
597 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }, szSlash[] = { '/',0 } ;
598 static const WCHAR szFormat1[] = { 'h','t','t','p',':','/','/','%','s',0 };
599 static const WCHAR szFormat2[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
602 memset( &UrlComponents, 0, sizeof UrlComponents );
603 UrlComponents.dwStructSize = sizeof UrlComponents;
604 UrlComponents.lpszHostName = buf;
605 UrlComponents.dwHostNameLength = MAXHOSTNAME;
607 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
608 buf,strlenW(szHttp),szHttp,strlenW(szHttp)) )
609 sprintfW(proxy, szFormat1, hIC->lpszProxy);
612 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
614 if( UrlComponents.dwHostNameLength == 0 )
617 if( !lpwhr->lpszPath )
618 lpwhr->lpszPath = (LPWSTR)szNul;
619 TRACE("server='%s' path='%s'\n",
620 debugstr_w(lpwhs->lpszServerName), debugstr_w(lpwhr->lpszPath));
621 /* for constant 15 see above */
622 len = strlenW(lpwhs->lpszServerName) + strlenW(lpwhr->lpszPath) + 15;
623 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
625 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
626 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
628 sprintfW(url, szFormat2, lpwhs->lpszServerName, lpwhs->nServerPort);
630 if( lpwhr->lpszPath[0] != '/' )
631 strcatW( url, szSlash );
632 strcatW(url, lpwhr->lpszPath);
633 if(lpwhr->lpszPath != szNul)
634 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
635 lpwhr->lpszPath = url;
636 /* FIXME: Do I have to free lpwhs->lpszServerName here ? */
637 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
638 lpwhs->nServerPort = UrlComponents.nPort;
643 /***********************************************************************
644 * HTTP_HttpOpenRequestW (internal)
646 * Open a HTTP request handle
649 * HINTERNET a HTTP request handle on success
653 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
654 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
655 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
656 DWORD dwFlags, DWORD dwContext)
658 LPWININETAPPINFOW hIC = NULL;
659 LPWININETHTTPREQW lpwhr;
661 LPWSTR lpszUrl = NULL;
663 HINTERNET handle = NULL;
664 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
669 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
670 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
672 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
675 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
678 lpwhr->hdr.htype = WH_HHTTPREQ;
679 lpwhr->hdr.lpwhparent = WININET_AddRef( &lpwhs->hdr );
680 lpwhr->hdr.dwFlags = dwFlags;
681 lpwhr->hdr.dwContext = dwContext;
682 lpwhr->hdr.dwRefCount = 1;
683 lpwhr->hdr.destroy = HTTP_CloseHTTPRequestHandle;
685 handle = WININET_AllocHandle( &lpwhr->hdr );
688 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
692 NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE);
694 if (NULL != lpszObjectName && strlenW(lpszObjectName)) {
698 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
700 len = strlenW(lpszObjectName)+1;
701 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
702 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
703 URL_ESCAPE_SPACES_ONLY);
706 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(lpszObjectName),rc);
707 strcpyW(lpwhr->lpszPath,lpszObjectName);
711 if (NULL != lpszReferrer && strlenW(lpszReferrer))
712 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDHDR_FLAG_COALESCE);
714 if(lpszAcceptTypes!=NULL)
717 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
718 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i], HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_REQ|HTTP_ADDHDR_FLAG_ADD_IF_NEW);
721 if (NULL == lpszVerb)
723 static const WCHAR szGet[] = {'G','E','T',0};
724 lpwhr->lpszVerb = WININET_strdupW(szGet);
726 else if (strlenW(lpszVerb))
727 lpwhr->lpszVerb = WININET_strdupW(lpszVerb);
729 if (NULL != lpszReferrer && strlenW(lpszReferrer))
731 WCHAR buf[MAXHOSTNAME];
732 URL_COMPONENTSW UrlComponents;
734 memset( &UrlComponents, 0, sizeof UrlComponents );
735 UrlComponents.dwStructSize = sizeof UrlComponents;
736 UrlComponents.lpszHostName = buf;
737 UrlComponents.dwHostNameLength = MAXHOSTNAME;
739 InternetCrackUrlW(lpszReferrer, 0, 0, &UrlComponents);
740 if (strlenW(UrlComponents.lpszHostName))
741 lpwhr->lpszHostName = WININET_strdupW(UrlComponents.lpszHostName);
743 lpwhr->lpszHostName = WININET_strdupW(lpwhs->lpszServerName);
745 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
746 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
751 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
753 len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
754 agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
755 sprintfW(agent_header, user_agent, hIC->lpszAgent );
757 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
758 HTTP_ADDREQ_FLAG_ADD);
759 HeapFree(GetProcessHeap(), 0, agent_header);
762 len = strlenW(lpwhr->lpszHostName) + strlenW(szUrlForm);
763 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
764 sprintfW( lpszUrl, szUrlForm, lpwhr->lpszHostName );
766 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
767 InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
770 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
771 static const WCHAR szcrlf[] = {'\r','\n',0};
773 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
775 cnt += sprintfW(lpszCookies, szCookie);
776 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
777 strcatW(lpszCookies, szcrlf);
779 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
780 HTTP_ADDREQ_FLAG_ADD);
781 HeapFree(GetProcessHeap(), 0, lpszCookies);
783 HeapFree(GetProcessHeap(), 0, lpszUrl);
787 if (hIC->lpfnStatusCB)
789 INTERNET_ASYNC_RESULT iar;
791 iar.dwResult = (DWORD)handle;
792 iar.dwError = ERROR_SUCCESS;
794 SendAsyncCallback(hIC, &lpwhs->hdr, dwContext,
795 INTERNET_STATUS_HANDLE_CREATED, &iar,
796 sizeof(INTERNET_ASYNC_RESULT));
800 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
804 * According to my tests. The name is not resolved until a request is Opened
806 SendAsyncCallback(hIC, &lpwhs->hdr, dwContext,
807 INTERNET_STATUS_RESOLVING_NAME,
808 lpwhs->lpszServerName,
809 strlenW(lpwhs->lpszServerName)+1);
810 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
811 &lpwhs->phostent, &lpwhs->socketAddress))
813 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
814 InternetCloseHandle( handle );
819 SendAsyncCallback(hIC, &lpwhs->hdr, lpwhr->hdr.dwContext,
820 INTERNET_STATUS_NAME_RESOLVED,
821 &(lpwhs->socketAddress),
822 sizeof(struct sockaddr_in));
826 WININET_Release( &lpwhr->hdr );
828 TRACE("<-- %p (%p)\n", handle, lpwhr);
832 /***********************************************************************
833 * HTTP_HttpQueryInfoW (internal)
835 BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
836 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
838 LPHTTPHEADERW lphttpHdr = NULL;
839 BOOL bSuccess = FALSE;
841 /* Find requested header structure */
842 if ((dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK) == HTTP_QUERY_CUSTOM)
844 INT index = HTTP_GetCustomHeaderIndex(lpwhr, (LPWSTR)lpBuffer);
849 lphttpHdr = &lpwhr->pCustHeaders[index];
853 INT index = dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK;
855 if (index == HTTP_QUERY_RAW_HEADERS_CRLF)
857 int len = strlenW(lpwhr->lpszRawHeaders);
858 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
860 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
861 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
864 memcpy(lpBuffer, lpwhr->lpszRawHeaders, (len+1)*sizeof(WCHAR));
865 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
868 else if (index == HTTP_QUERY_RAW_HEADERS)
870 static const WCHAR szCrLf[] = {'\r','\n',0};
871 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
874 LPWSTR pszString = (WCHAR*)lpBuffer;
876 for (i = 0; ppszRawHeaderLines[i]; i++)
877 size += strlenW(ppszRawHeaderLines[i]) + 1;
879 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
881 HTTP_FreeTokens(ppszRawHeaderLines);
882 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
883 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
887 for (i = 0; ppszRawHeaderLines[i]; i++)
889 int len = strlenW(ppszRawHeaderLines[i]);
890 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
895 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
897 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
898 HTTP_FreeTokens(ppszRawHeaderLines);
902 else if (index >= 0 && index <= HTTP_QUERY_MAX && lpwhr->StdHeaders[index].lpszValue)
904 lphttpHdr = &lpwhr->StdHeaders[index];
908 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
913 /* Ensure header satisifies requested attributes */
914 if ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
915 (~lphttpHdr->wFlags & HDR_ISREQUEST))
917 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
921 /* coalesce value to reuqested type */
922 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
924 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
927 TRACE(" returning number : %d\n", *(int *)lpBuffer);
929 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
935 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
937 tmpTM = *gmtime(&tmpTime);
938 STHook = (SYSTEMTIME *) lpBuffer;
942 STHook->wDay = tmpTM.tm_mday;
943 STHook->wHour = tmpTM.tm_hour;
944 STHook->wMilliseconds = 0;
945 STHook->wMinute = tmpTM.tm_min;
946 STHook->wDayOfWeek = tmpTM.tm_wday;
947 STHook->wMonth = tmpTM.tm_mon + 1;
948 STHook->wSecond = tmpTM.tm_sec;
949 STHook->wYear = tmpTM.tm_year;
953 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
954 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
955 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
957 else if (dwInfoLevel & HTTP_QUERY_FLAG_COALESCE)
959 if (*lpdwIndex >= lphttpHdr->wCount)
961 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
965 /* Copy strncpyW(lpBuffer, lphttpHdr[*lpdwIndex], len); */
971 INT len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
973 if (len > *lpdwBufferLength)
975 *lpdwBufferLength = len;
976 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
980 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
981 *lpdwBufferLength = len - sizeof(WCHAR);
984 TRACE(" returning string : '%s'\n", debugstr_w(lpBuffer));
989 /***********************************************************************
990 * HttpQueryInfoW (WININET.@)
992 * Queries for information about an HTTP request
999 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1000 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1002 BOOL bSuccess = FALSE;
1003 LPWININETHTTPREQW lpwhr;
1005 if (TRACE_ON(wininet)) {
1006 #define FE(x) { x, #x }
1007 static const wininet_flag_info query_flags[] = {
1008 FE(HTTP_QUERY_MIME_VERSION),
1009 FE(HTTP_QUERY_CONTENT_TYPE),
1010 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
1011 FE(HTTP_QUERY_CONTENT_ID),
1012 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
1013 FE(HTTP_QUERY_CONTENT_LENGTH),
1014 FE(HTTP_QUERY_CONTENT_LANGUAGE),
1015 FE(HTTP_QUERY_ALLOW),
1016 FE(HTTP_QUERY_PUBLIC),
1017 FE(HTTP_QUERY_DATE),
1018 FE(HTTP_QUERY_EXPIRES),
1019 FE(HTTP_QUERY_LAST_MODIFIED),
1020 FE(HTTP_QUERY_MESSAGE_ID),
1022 FE(HTTP_QUERY_DERIVED_FROM),
1023 FE(HTTP_QUERY_COST),
1024 FE(HTTP_QUERY_LINK),
1025 FE(HTTP_QUERY_PRAGMA),
1026 FE(HTTP_QUERY_VERSION),
1027 FE(HTTP_QUERY_STATUS_CODE),
1028 FE(HTTP_QUERY_STATUS_TEXT),
1029 FE(HTTP_QUERY_RAW_HEADERS),
1030 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
1031 FE(HTTP_QUERY_CONNECTION),
1032 FE(HTTP_QUERY_ACCEPT),
1033 FE(HTTP_QUERY_ACCEPT_CHARSET),
1034 FE(HTTP_QUERY_ACCEPT_ENCODING),
1035 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
1036 FE(HTTP_QUERY_AUTHORIZATION),
1037 FE(HTTP_QUERY_CONTENT_ENCODING),
1038 FE(HTTP_QUERY_FORWARDED),
1039 FE(HTTP_QUERY_FROM),
1040 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
1041 FE(HTTP_QUERY_LOCATION),
1042 FE(HTTP_QUERY_ORIG_URI),
1043 FE(HTTP_QUERY_REFERER),
1044 FE(HTTP_QUERY_RETRY_AFTER),
1045 FE(HTTP_QUERY_SERVER),
1046 FE(HTTP_QUERY_TITLE),
1047 FE(HTTP_QUERY_USER_AGENT),
1048 FE(HTTP_QUERY_WWW_AUTHENTICATE),
1049 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
1050 FE(HTTP_QUERY_ACCEPT_RANGES),
1051 FE(HTTP_QUERY_SET_COOKIE),
1052 FE(HTTP_QUERY_COOKIE),
1053 FE(HTTP_QUERY_REQUEST_METHOD),
1054 FE(HTTP_QUERY_REFRESH),
1055 FE(HTTP_QUERY_CONTENT_DISPOSITION),
1057 FE(HTTP_QUERY_CACHE_CONTROL),
1058 FE(HTTP_QUERY_CONTENT_BASE),
1059 FE(HTTP_QUERY_CONTENT_LOCATION),
1060 FE(HTTP_QUERY_CONTENT_MD5),
1061 FE(HTTP_QUERY_CONTENT_RANGE),
1062 FE(HTTP_QUERY_ETAG),
1063 FE(HTTP_QUERY_HOST),
1064 FE(HTTP_QUERY_IF_MATCH),
1065 FE(HTTP_QUERY_IF_NONE_MATCH),
1066 FE(HTTP_QUERY_IF_RANGE),
1067 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
1068 FE(HTTP_QUERY_MAX_FORWARDS),
1069 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
1070 FE(HTTP_QUERY_RANGE),
1071 FE(HTTP_QUERY_TRANSFER_ENCODING),
1072 FE(HTTP_QUERY_UPGRADE),
1073 FE(HTTP_QUERY_VARY),
1075 FE(HTTP_QUERY_WARNING),
1076 FE(HTTP_QUERY_CUSTOM)
1078 static const wininet_flag_info modifier_flags[] = {
1079 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
1080 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
1081 FE(HTTP_QUERY_FLAG_NUMBER),
1082 FE(HTTP_QUERY_FLAG_COALESCE)
1085 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
1086 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
1089 TRACE("(%p, 0x%08lx)--> %ld\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
1090 TRACE(" Attribute:");
1091 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
1092 if (query_flags[i].val == info) {
1093 DPRINTF(" %s", query_flags[i].name);
1097 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
1098 DPRINTF(" Unknown (%08lx)", info);
1101 DPRINTF(" Modifier:");
1102 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
1103 if (modifier_flags[i].val & info_mod) {
1104 DPRINTF(" %s", modifier_flags[i].name);
1105 info_mod &= ~ modifier_flags[i].val;
1110 DPRINTF(" Unknown (%08lx)", info_mod);
1115 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1116 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1118 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1122 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
1123 lpBuffer, lpdwBufferLength, lpdwIndex);
1127 WININET_Release( &lpwhr->hdr );
1129 TRACE("%d <--\n", bSuccess);
1133 /***********************************************************************
1134 * HttpQueryInfoA (WININET.@)
1136 * Queries for information about an HTTP request
1143 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1144 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1150 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
1151 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
1153 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
1154 lpdwBufferLength, lpdwIndex );
1157 len = (*lpdwBufferLength)*sizeof(WCHAR);
1158 bufferW = HeapAlloc( GetProcessHeap(), 0, len );
1159 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
1163 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR),
1164 lpBuffer, *lpdwBufferLength, NULL, NULL );
1165 *lpdwBufferLength = len * sizeof(WCHAR);
1168 /* since the strings being returned from HttpQueryInfoW should be
1169 * only ASCII characters, it is reasonable to assume that all of
1170 * the Unicode characters can be reduced to a single byte */
1171 *lpdwBufferLength = len / sizeof(WCHAR);
1173 HeapFree(GetProcessHeap(), 0, bufferW );
1178 /***********************************************************************
1179 * HttpSendRequestExA (WININET.@)
1181 * Sends the specified request to the HTTP server and allows chunked
1184 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
1185 LPINTERNET_BUFFERSA lpBuffersIn,
1186 LPINTERNET_BUFFERSA lpBuffersOut,
1187 DWORD dwFlags, DWORD dwContext)
1189 FIXME("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersIn,
1190 lpBuffersOut, dwFlags, dwContext);
1194 /***********************************************************************
1195 * HttpSendRequestW (WININET.@)
1197 * Sends the specified request to the HTTP server
1204 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
1205 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1207 LPWININETHTTPREQW lpwhr;
1208 LPWININETHTTPSESSIONW lpwhs = NULL;
1209 LPWININETAPPINFOW hIC = NULL;
1212 TRACE("%p, %p (%s), %li, %p, %li)\n", hHttpRequest,
1213 lpszHeaders, debugstr_w(lpszHeaders), dwHeaderLength, lpOptional, dwOptionalLength);
1215 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1216 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1218 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1223 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1224 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1226 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1231 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1232 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1234 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1239 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1241 WORKREQUEST workRequest;
1242 struct WORKREQ_HTTPSENDREQUESTW *req;
1244 workRequest.asyncall = HTTPSENDREQUESTW;
1245 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
1246 req = &workRequest.u.HttpSendRequestW;
1248 req->lpszHeader = WININET_strdupW(lpszHeaders);
1250 req->lpszHeader = 0;
1251 req->dwHeaderLength = dwHeaderLength;
1252 req->lpOptional = lpOptional;
1253 req->dwOptionalLength = dwOptionalLength;
1255 INTERNET_AsyncCall(&workRequest);
1257 * This is from windows.
1259 SetLastError(ERROR_IO_PENDING);
1264 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
1265 dwHeaderLength, lpOptional, dwOptionalLength);
1269 WININET_Release( &lpwhr->hdr );
1273 /***********************************************************************
1274 * HttpSendRequestA (WININET.@)
1276 * Sends the specified request to the HTTP server
1283 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
1284 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1287 LPWSTR szHeaders=NULL;
1288 DWORD nLen=dwHeaderLength;
1289 if(lpszHeaders!=NULL)
1291 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
1292 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
1293 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
1295 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
1297 HeapFree(GetProcessHeap(),0,szHeaders);
1301 /***********************************************************************
1302 * HTTP_HandleRedirect (internal)
1304 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl, LPCWSTR lpszHeaders,
1305 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength)
1307 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1308 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1313 /* if it's an absolute path, keep the same session info */
1314 strcpyW(path,lpszUrl);
1316 else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1318 TRACE("Redirect through proxy\n");
1319 strcpyW(path,lpszUrl);
1323 URL_COMPONENTSW urlComponents;
1324 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
1325 WCHAR password[1024], extra[1024];
1326 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
1327 urlComponents.lpszScheme = protocol;
1328 urlComponents.dwSchemeLength = 32;
1329 urlComponents.lpszHostName = hostName;
1330 urlComponents.dwHostNameLength = MAXHOSTNAME;
1331 urlComponents.lpszUserName = userName;
1332 urlComponents.dwUserNameLength = 1024;
1333 urlComponents.lpszPassword = password;
1334 urlComponents.dwPasswordLength = 1024;
1335 urlComponents.lpszUrlPath = path;
1336 urlComponents.dwUrlPathLength = 2048;
1337 urlComponents.lpszExtraInfo = extra;
1338 urlComponents.dwExtraInfoLength = 1024;
1339 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
1342 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1343 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1347 * This upsets redirects to binary files on sourceforge.net
1348 * and gives an html page instead of the target file
1349 * Examination of the HTTP request sent by native wininet.dll
1350 * reveals that it doesn't send a referrer in that case.
1351 * Maybe there's a flag that enables this, or maybe a referrer
1352 * shouldn't be added in case of a redirect.
1355 /* consider the current host as the referrer */
1356 if (NULL != lpwhs->lpszServerName && strlenW(lpwhs->lpszServerName))
1357 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
1358 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
1359 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
1362 if (NULL != lpwhs->lpszServerName)
1363 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1364 lpwhs->lpszServerName = WININET_strdupW(hostName);
1365 if (NULL != lpwhs->lpszUserName)
1366 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
1367 lpwhs->lpszUserName = WININET_strdupW(userName);
1368 lpwhs->nServerPort = urlComponents.nPort;
1370 if (NULL != lpwhr->lpszHostName)
1371 HeapFree(GetProcessHeap(), 0, lpwhr->lpszHostName);
1372 lpwhr->lpszHostName=WININET_strdupW(hostName);
1374 SendAsyncCallback(hIC, &lpwhs->hdr, lpwhr->hdr.dwContext,
1375 INTERNET_STATUS_RESOLVING_NAME,
1376 lpwhs->lpszServerName,
1377 strlenW(lpwhs->lpszServerName)+1);
1379 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1380 &lpwhs->phostent, &lpwhs->socketAddress))
1382 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1386 SendAsyncCallback(hIC, &lpwhs->hdr, lpwhr->hdr.dwContext,
1387 INTERNET_STATUS_NAME_RESOLVED,
1388 &(lpwhs->socketAddress),
1389 sizeof(struct sockaddr_in));
1394 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1395 lpwhr->lpszPath=NULL;
1401 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
1402 if (rc != E_POINTER)
1403 needed = strlenW(path)+1;
1404 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
1405 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
1406 URL_ESCAPE_SPACES_ONLY);
1409 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(path),rc);
1410 strcpyW(lpwhr->lpszPath,path);
1414 return HTTP_HttpSendRequestW(lpwhr, lpszHeaders, dwHeaderLength, lpOptional, dwOptionalLength);
1417 /***********************************************************************
1418 * HTTP_build_req (internal)
1420 * concatenate all the strings in the request together
1422 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
1427 for( t = list; *t ; t++ )
1428 len += strlenW( *t );
1431 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1434 for( t = list; *t ; t++ )
1440 /***********************************************************************
1441 * HTTP_HttpSendRequestW (internal)
1443 * Sends the specified request to the HTTP server
1450 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
1451 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1455 BOOL bSuccess = FALSE;
1456 LPWSTR requestString = NULL;
1458 LPWININETHTTPSESSIONW lpwhs = NULL;
1459 LPWININETAPPINFOW hIC = NULL;
1460 BOOL loop_next = FALSE;
1461 int CustHeaderIndex;
1463 TRACE("--> %p\n", lpwhr);
1465 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
1467 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1468 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1470 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1474 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1475 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1477 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1481 /* Clear any error information */
1482 INTERNET_SetLastError(0);
1485 /* We must have a verb */
1486 if (NULL == lpwhr->lpszVerb)
1491 /* if we are using optional stuff, we must add the fixed header of that option length */
1492 if (lpOptional && dwOptionalLength)
1494 static const WCHAR szContentLength[] = {
1495 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0};
1496 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \n\r */ + 20 /* int */ ];
1497 sprintfW(contentLengthStr, szContentLength, dwOptionalLength);
1498 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD);
1503 static const WCHAR szSlash[] = { '/',0 };
1504 static const WCHAR szSpace[] = { ' ',0 };
1505 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
1506 static const WCHAR szcrlf[] = {'\r','\n', 0};
1507 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
1508 static const WCHAR szSetCookie[] = {'S','e','t','-','C','o','o','k','i','e',0 };
1509 static const WCHAR szColon[] = { ':',' ',0 };
1516 TRACE("Going to url %s %s\n", debugstr_w(lpwhr->lpszHostName), debugstr_w(lpwhr->lpszPath));
1519 /* If we don't have a path we set it to root */
1520 if (NULL == lpwhr->lpszPath)
1521 lpwhr->lpszPath = WININET_strdupW(szSlash);
1523 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1524 lpwhr->lpszPath, strlenW(szHttp), szHttp, strlenW(szHttp) )
1525 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
1527 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
1528 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
1530 strcpyW(fixurl + 1, lpwhr->lpszPath);
1531 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
1532 lpwhr->lpszPath = fixurl;
1535 /* allocate space for an array of all the string pointers to be added */
1536 len = (2 + HTTP_QUERY_MAX + lpwhr->nCustHeaders)*4 + 3;
1537 req = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, len*sizeof(LPCWSTR) );
1539 /* add the verb, path and HTTP/1.0 */
1541 req[n++] = lpwhr->lpszVerb;
1543 req[n++] = lpwhr->lpszPath;
1544 req[n++] = HTTPHEADER;
1546 /* Append standard request headers */
1547 for (i = 0; i <= HTTP_QUERY_MAX; i++)
1549 if (lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST)
1552 req[n++] = lpwhr->StdHeaders[i].lpszField;
1554 req[n++] = lpwhr->StdHeaders[i].lpszValue;
1556 TRACE("Adding header %s (%s)\n",
1557 debugstr_w(lpwhr->StdHeaders[i].lpszField),
1558 debugstr_w(lpwhr->StdHeaders[i].lpszValue));
1562 /* Append custom request heades */
1563 for (i = 0; i < lpwhr->nCustHeaders; i++)
1565 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
1568 req[n++] = lpwhr->pCustHeaders[i].lpszField;
1570 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
1572 TRACE("Adding custom header %s (%s)\n",
1573 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
1574 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
1578 if (lpwhr->lpszHostName)
1580 req[n++] = HTTPHOSTHEADER;
1581 req[n++] = lpwhr->lpszHostName;
1585 ERR("oops. buffer overrun\n");
1587 requestString = HTTP_build_req( req, 4 );
1588 HeapFree( GetProcessHeap(), 0, req );
1591 * Set (header) termination string for request
1592 * Make sure there's exactly two new lines at the end of the request
1594 p = &requestString[strlenW(requestString)-1];
1595 while ( (*p == '\n') || (*p == '\r') )
1597 strcpyW( p+1, sztwocrlf );
1599 TRACE("Request header -> %s\n", debugstr_w(requestString) );
1601 /* Send the request and store the results */
1602 if (!HTTP_OpenConnection(lpwhr))
1605 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1606 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
1608 /* send the request as ASCII, tack on the optional data */
1610 dwOptionalLength = 0;
1611 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
1612 NULL, 0, NULL, NULL );
1613 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
1614 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
1615 ascii_req, len, NULL, NULL );
1617 memcpy( &ascii_req[len], lpOptional, dwOptionalLength );
1618 len += dwOptionalLength;
1619 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
1620 HeapFree( GetProcessHeap(), 0, ascii_req );
1622 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1623 INTERNET_STATUS_REQUEST_SENT,
1624 &len,sizeof(DWORD));
1626 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1627 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
1632 responseLen = HTTP_GetResponseHeaders(lpwhr);
1636 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1637 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
1640 /* process headers here. Is this right? */
1641 CustHeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSetCookie);
1642 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && (CustHeaderIndex >= 0))
1644 LPHTTPHEADERW setCookieHeader;
1645 int nPosStart = 0, nPosEnd = 0, len;
1646 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
1648 setCookieHeader = &lpwhr->pCustHeaders[CustHeaderIndex];
1650 while (setCookieHeader->lpszValue[nPosEnd] != '\0')
1652 LPWSTR buf_cookie, cookie_name, cookie_data;
1654 LPWSTR domain = NULL;
1656 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
1657 setCookieHeader->lpszValue[nPosEnd] != '\0')
1661 if (setCookieHeader->lpszValue[nPosEnd] == ';')
1663 /* fixme: not case sensitive, strcasestr is gnu only */
1664 int nDomainPosEnd = 0;
1665 int nDomainPosStart = 0, nDomainLength = 0;
1666 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
1667 LPWSTR lpszDomain = strstrW(&setCookieHeader->lpszValue[nPosEnd], szDomain);
1669 { /* they have specified their own domain, lets use it */
1670 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
1671 lpszDomain[nDomainPosEnd] != '\0')
1675 nDomainPosStart = strlenW(szDomain);
1676 nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
1677 domain = HeapAlloc(GetProcessHeap(), 0, (nDomainLength + 1)*sizeof(WCHAR));
1678 strncpyW(domain, &lpszDomain[nDomainPosStart], nDomainLength);
1679 domain[nDomainLength] = '\0';
1682 if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
1683 buf_cookie = HeapAlloc(GetProcessHeap(), 0, ((nPosEnd - nPosStart) + 1)*sizeof(WCHAR));
1684 strncpyW(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart));
1685 buf_cookie[(nPosEnd - nPosStart)] = '\0';
1686 TRACE("%s\n", debugstr_w(buf_cookie));
1687 while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
1691 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
1693 HeapFree(GetProcessHeap(), 0, buf_cookie);
1697 cookie_name = HeapAlloc(GetProcessHeap(), 0, (nEqualPos + 1)*sizeof(WCHAR));
1698 strncpyW(cookie_name, buf_cookie, nEqualPos);
1699 cookie_name[nEqualPos] = '\0';
1700 cookie_data = &buf_cookie[nEqualPos + 1];
1703 len = strlenW((domain ? domain : lpwhr->lpszHostName)) + strlenW(lpwhr->lpszPath) + 9;
1704 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1705 sprintfW(buf_url, szFmt, (domain ? domain : lpwhr->lpszHostName)); /* FIXME PATH!!! */
1706 InternetSetCookieW(buf_url, cookie_name, cookie_data);
1708 HeapFree(GetProcessHeap(), 0, buf_url);
1709 HeapFree(GetProcessHeap(), 0, buf_cookie);
1710 HeapFree(GetProcessHeap(), 0, cookie_name);
1711 if (domain) HeapFree(GetProcessHeap(), 0, domain);
1712 nPosStart = nPosEnd;
1721 HeapFree(GetProcessHeap(), 0, requestString);
1723 /* TODO: send notification for P3P header */
1725 if(!(hIC->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
1727 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
1728 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
1729 (dwCode==302 || dwCode==301))
1731 WCHAR szNewLocation[2048];
1732 DWORD dwBufferSize=2048;
1734 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
1736 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1737 INTERNET_STATUS_REDIRECT, szNewLocation,
1739 return HTTP_HandleRedirect(lpwhr, szNewLocation, lpszHeaders,
1740 dwHeaderLength, lpOptional, dwOptionalLength);
1745 if (hIC->lpfnStatusCB)
1747 INTERNET_ASYNC_RESULT iar;
1749 iar.dwResult = (DWORD)bSuccess;
1750 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1752 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1753 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1754 sizeof(INTERNET_ASYNC_RESULT));
1762 /***********************************************************************
1763 * HTTP_Connect (internal)
1765 * Create http session handle
1768 * HINTERNET a session handle on success
1772 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
1773 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
1774 LPCWSTR lpszPassword, DWORD dwFlags, DWORD dwContext,
1775 DWORD dwInternalFlags)
1777 BOOL bSuccess = FALSE;
1778 LPWININETHTTPSESSIONW lpwhs = NULL;
1779 HINTERNET handle = NULL;
1783 assert( hIC->hdr.htype == WH_HINIT );
1785 hIC->hdr.dwContext = dwContext;
1787 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
1790 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1795 * According to my tests. The name is not resolved until a request is sent
1798 if (nServerPort == INTERNET_INVALID_PORT_NUMBER)
1799 nServerPort = INTERNET_DEFAULT_HTTP_PORT;
1801 lpwhs->hdr.htype = WH_HHTTPSESSION;
1802 lpwhs->hdr.lpwhparent = WININET_AddRef( &hIC->hdr );
1803 lpwhs->hdr.dwFlags = dwFlags;
1804 lpwhs->hdr.dwContext = dwContext;
1805 lpwhs->hdr.dwInternalFlags = dwInternalFlags;
1806 lpwhs->hdr.dwRefCount = 1;
1807 lpwhs->hdr.destroy = HTTP_CloseHTTPSessionHandle;
1809 handle = WININET_AllocHandle( &lpwhs->hdr );
1812 ERR("Failed to alloc handle\n");
1813 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1817 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
1818 if(strchrW(hIC->lpszProxy, ' '))
1819 FIXME("Several proxies not implemented.\n");
1820 if(hIC->lpszProxyBypass)
1821 FIXME("Proxy bypass is ignored.\n");
1823 if (NULL != lpszServerName)
1824 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
1825 if (NULL != lpszUserName)
1826 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
1827 lpwhs->nServerPort = nServerPort;
1829 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
1830 if (hIC->lpfnStatusCB && !(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
1832 INTERNET_ASYNC_RESULT iar;
1834 iar.dwResult = (DWORD)handle;
1835 iar.dwError = ERROR_SUCCESS;
1837 SendAsyncCallback(hIC, &hIC->hdr, dwContext,
1838 INTERNET_STATUS_HANDLE_CREATED, &iar,
1839 sizeof(INTERNET_ASYNC_RESULT));
1846 WININET_Release( &lpwhs->hdr );
1849 * a INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
1853 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
1858 /***********************************************************************
1859 * HTTP_OpenConnection (internal)
1861 * Connect to a web server
1868 BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
1870 BOOL bSuccess = FALSE;
1871 LPWININETHTTPSESSIONW lpwhs;
1872 LPWININETAPPINFOW hIC = NULL;
1877 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1879 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1883 lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
1885 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1886 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1887 INTERNET_STATUS_CONNECTING_TO_SERVER,
1888 &(lpwhs->socketAddress),
1889 sizeof(struct sockaddr_in));
1891 if (!NETCON_create(&lpwhr->netConnection, lpwhs->phostent->h_addrtype,
1894 WARN("Socket creation failed\n");
1898 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
1899 sizeof(lpwhs->socketAddress)))
1901 WARN("Unable to connect to host (%s)\n", strerror(errno));
1905 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1906 INTERNET_STATUS_CONNECTED_TO_SERVER,
1907 &(lpwhs->socketAddress),
1908 sizeof(struct sockaddr_in));
1913 TRACE("%d <--\n", bSuccess);
1918 /***********************************************************************
1919 * HTTP_clear_response_headers (internal)
1921 * clear out any old response headers
1923 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
1927 for( i=0; i<=HTTP_QUERY_MAX; i++ )
1929 if( !lpwhr->StdHeaders[i].lpszField )
1931 if( !lpwhr->StdHeaders[i].lpszValue )
1933 if ( lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST )
1935 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[i], NULL );
1937 for( i=0; i<lpwhr->nCustHeaders; i++)
1939 if( !lpwhr->pCustHeaders[i].lpszField )
1941 if( !lpwhr->pCustHeaders[i].lpszValue )
1943 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
1945 HTTP_ReplaceHeaderValue( &lpwhr->pCustHeaders[i], NULL );
1949 /***********************************************************************
1950 * HTTP_GetResponseHeaders (internal)
1952 * Read server response
1959 BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr)
1962 WCHAR buffer[MAX_REPLY_LEN];
1963 DWORD buflen = MAX_REPLY_LEN;
1964 BOOL bSuccess = FALSE;
1966 WCHAR value[MAX_FIELD_VALUE_LEN], field[MAX_FIELD_LEN];
1967 static const WCHAR szHttp[] = { 'H','T','T','P',0 };
1968 static const WCHAR szCrLf[] = {'\r','\n',0};
1969 char bufferA[MAX_REPLY_LEN];
1970 LPWSTR status_code, status_text;
1971 int cchMaxRawHeaders = 1024;
1972 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
1973 int cchRawHeaders = 0;
1977 /* clear old response headers (eg. from a redirect response) */
1978 HTTP_clear_response_headers( lpwhr );
1980 if (!NETCON_connected(&lpwhr->netConnection))
1984 * HACK peek at the buffer
1986 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
1989 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
1991 buflen = MAX_REPLY_LEN;
1992 memset(buffer, 0, MAX_REPLY_LEN);
1993 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
1995 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
1997 if (strncmpW(buffer, szHttp, 4) != 0)
2000 /* regenerate raw headers */
2001 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2003 cchMaxRawHeaders *= 2;
2004 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2006 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2007 cchRawHeaders += (buflen-1);
2008 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2009 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2010 lpszRawHeaders[cchRawHeaders] = '\0';
2012 /* split the version from the status code */
2013 status_code = strchrW( buffer, ' ' );
2018 /* split the status code from the status text */
2019 status_text = strchrW( status_code, ' ' );
2024 TRACE("version [%s] status code [%s] status text [%s]\n",
2025 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
2026 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_VERSION], buffer );
2027 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_STATUS_CODE], status_code );
2028 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_STATUS_TEXT], status_text );
2030 /* Parse each response line */
2033 buflen = MAX_REPLY_LEN;
2034 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2036 TRACE("got line %s, now interpretting\n", debugstr_a(bufferA));
2037 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2039 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2041 cchMaxRawHeaders *= 2;
2042 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2044 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2045 cchRawHeaders += (buflen-1);
2046 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2047 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2048 lpszRawHeaders[cchRawHeaders] = '\0';
2050 if (!HTTP_InterpretHttpHeader(buffer, field, MAX_FIELD_LEN, value, MAX_FIELD_VALUE_LEN))
2053 HTTP_ProcessHeader(lpwhr, field, value, (HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE));
2063 if (lpwhr->lpszRawHeaders) HeapFree(GetProcessHeap(), 0, lpszRawHeaders);
2064 lpwhr->lpszRawHeaders = lpszRawHeaders;
2065 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
2078 /***********************************************************************
2079 * HTTP_InterpretHttpHeader (internal)
2081 * Parse server response
2088 static INT stripSpaces(LPCWSTR lpszSrc, LPWSTR lpszStart, INT *len)
2095 while (*lpszSrc == ' ' && *lpszSrc != '\0')
2099 while(*lpsztmp != '\0')
2101 if (*lpsztmp != ' ')
2102 srclen = lpsztmp - lpszSrc + 1;
2107 *len = min(*len, srclen);
2108 strncpyW(lpszStart, lpszSrc, *len);
2109 lpszStart[*len] = '\0';
2115 BOOL HTTP_InterpretHttpHeader(LPWSTR buffer, LPWSTR field, INT fieldlen, LPWSTR value, INT valuelen)
2118 BOOL bSuccess = FALSE;
2125 pd = strchrW(buffer, ':');
2129 if (stripSpaces(buffer, field, &fieldlen) > 0)
2131 if (stripSpaces(pd+1, value, &valuelen) > 0)
2136 TRACE("%d: field(%s) Value(%s)\n", bSuccess, debugstr_w(field), debugstr_w(value));
2141 /***********************************************************************
2142 * HTTP_GetStdHeaderIndex (internal)
2144 * Lookup field index in standard http header array
2146 * FIXME: This should be stuffed into a hash table
2148 INT HTTP_GetStdHeaderIndex(LPCWSTR lpszField)
2151 static const WCHAR szContentLength[] = {
2152 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
2153 static const WCHAR szContentType[] = {
2154 'C','o','n','t','e','n','t','-','T','y','p','e',0};
2155 static const WCHAR szLastModified[] = {
2156 'L','a','s','t','-','M','o','d','i','f','i','e','d',0};
2157 static const WCHAR szLocation[] = {'L','o','c','a','t','i','o','n',0};
2158 static const WCHAR szAccept[] = {'A','c','c','e','p','t',0};
2159 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0};
2160 static const WCHAR szContentTrans[] = { 'C','o','n','t','e','n','t','-',
2161 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0};
2162 static const WCHAR szDate[] = { 'D','a','t','e',0};
2163 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0};
2164 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0};
2165 static const WCHAR szETag[] = { 'E','T','a','g',0};
2166 static const WCHAR szAcceptRanges[] = {
2167 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
2168 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
2169 static const WCHAR szMimeVersion[] = {
2170 'M','i','m','e','-','V','e','r','s','i','o','n', 0};
2171 static const WCHAR szPragma[] = { 'P','r','a','g','m','a', 0};
2172 static const WCHAR szCacheControl[] = {
2173 'C','a','c','h','e','-','C','o','n','t','r','o','l',0};
2174 static const WCHAR szUserAgent[] = { 'U','s','e','r','-','A','g','e','n','t',0};
2175 static const WCHAR szProxyAuth[] = {
2176 'P','r','o','x','y','-',
2177 'A','u','t','h','e','n','t','i','c','a','t','e', 0};
2178 static const WCHAR szContentEncoding[] = {
2179 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0};
2180 static const WCHAR szCookie[] = {'C','o','o','k','i','e',0};
2181 static const WCHAR szVary[] = {'V','a','r','y',0};
2182 static const WCHAR szVia[] = {'V','i','a',0};
2184 if (!strcmpiW(lpszField, szContentLength))
2185 index = HTTP_QUERY_CONTENT_LENGTH;
2186 else if (!strcmpiW(lpszField,szContentType))
2187 index = HTTP_QUERY_CONTENT_TYPE;
2188 else if (!strcmpiW(lpszField,szLastModified))
2189 index = HTTP_QUERY_LAST_MODIFIED;
2190 else if (!strcmpiW(lpszField,szLocation))
2191 index = HTTP_QUERY_LOCATION;
2192 else if (!strcmpiW(lpszField,szAccept))
2193 index = HTTP_QUERY_ACCEPT;
2194 else if (!strcmpiW(lpszField,szReferer))
2195 index = HTTP_QUERY_REFERER;
2196 else if (!strcmpiW(lpszField,szContentTrans))
2197 index = HTTP_QUERY_CONTENT_TRANSFER_ENCODING;
2198 else if (!strcmpiW(lpszField,szDate))
2199 index = HTTP_QUERY_DATE;
2200 else if (!strcmpiW(lpszField,szServer))
2201 index = HTTP_QUERY_SERVER;
2202 else if (!strcmpiW(lpszField,szConnection))
2203 index = HTTP_QUERY_CONNECTION;
2204 else if (!strcmpiW(lpszField,szETag))
2205 index = HTTP_QUERY_ETAG;
2206 else if (!strcmpiW(lpszField,szAcceptRanges))
2207 index = HTTP_QUERY_ACCEPT_RANGES;
2208 else if (!strcmpiW(lpszField,szExpires))
2209 index = HTTP_QUERY_EXPIRES;
2210 else if (!strcmpiW(lpszField,szMimeVersion))
2211 index = HTTP_QUERY_MIME_VERSION;
2212 else if (!strcmpiW(lpszField,szPragma))
2213 index = HTTP_QUERY_PRAGMA;
2214 else if (!strcmpiW(lpszField,szCacheControl))
2215 index = HTTP_QUERY_CACHE_CONTROL;
2216 else if (!strcmpiW(lpszField,szUserAgent))
2217 index = HTTP_QUERY_USER_AGENT;
2218 else if (!strcmpiW(lpszField,szProxyAuth))
2219 index = HTTP_QUERY_PROXY_AUTHENTICATE;
2220 else if (!strcmpiW(lpszField,szContentEncoding))
2221 index = HTTP_QUERY_CONTENT_ENCODING;
2222 else if (!strcmpiW(lpszField,szCookie))
2223 index = HTTP_QUERY_COOKIE;
2224 else if (!strcmpiW(lpszField,szVary))
2225 index = HTTP_QUERY_VARY;
2226 else if (!strcmpiW(lpszField,szVia))
2227 index = HTTP_QUERY_VIA;
2230 TRACE("Couldn't find %s in standard header table\n", debugstr_w(lpszField));
2236 /***********************************************************************
2237 * HTTP_ReplaceHeaderValue (internal)
2239 BOOL HTTP_ReplaceHeaderValue( LPHTTPHEADERW lphttpHdr, LPCWSTR value )
2243 if( lphttpHdr->lpszValue )
2244 HeapFree( GetProcessHeap(), 0, lphttpHdr->lpszValue );
2245 lphttpHdr->lpszValue = NULL;
2248 len = strlenW(value);
2251 lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0,
2252 (len+1)*sizeof(WCHAR));
2253 strcpyW(lphttpHdr->lpszValue, value);
2258 /***********************************************************************
2259 * HTTP_ProcessHeader (internal)
2261 * Stuff header into header tables according to <dwModifier>
2265 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2267 BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
2269 LPHTTPHEADERW lphttpHdr = NULL;
2270 BOOL bSuccess = FALSE;
2273 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), (unsigned int)dwModifier);
2275 /* Adjust modifier flags */
2276 if (dwModifier & COALESCEFLASG)
2277 dwModifier |= HTTP_ADDHDR_FLAG_ADD;
2279 /* Try to get index into standard header array */
2280 index = HTTP_GetStdHeaderIndex(field);
2283 lphttpHdr = &lpwhr->StdHeaders[index];
2285 else /* Find or create new custom header */
2287 index = HTTP_GetCustomHeaderIndex(lpwhr, field);
2290 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
2294 lphttpHdr = &lpwhr->pCustHeaders[index];
2300 hdr.lpszField = (LPWSTR)field;
2301 hdr.lpszValue = (LPWSTR)value;
2302 hdr.wFlags = hdr.wCount = 0;
2304 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2305 hdr.wFlags |= HDR_ISREQUEST;
2307 return HTTP_InsertCustomHeader(lpwhr, &hdr);
2311 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2312 lphttpHdr->wFlags |= HDR_ISREQUEST;
2314 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
2316 if (!lphttpHdr->lpszValue && (dwModifier & (HTTP_ADDHDR_FLAG_ADD|HTTP_ADDHDR_FLAG_ADD_IF_NEW)))
2320 if (!lpwhr->StdHeaders[index].lpszField)
2322 lphttpHdr->lpszField = WININET_strdupW(field);
2324 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2325 lphttpHdr->wFlags |= HDR_ISREQUEST;
2328 slen = strlenW(value) + 1;
2329 lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0, slen*sizeof(WCHAR));
2330 if (lphttpHdr->lpszValue)
2332 strcpyW(lphttpHdr->lpszValue, value);
2337 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2340 else if (lphttpHdr->lpszValue)
2342 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
2343 bSuccess = HTTP_ReplaceHeaderValue( lphttpHdr, value );
2344 else if (dwModifier & COALESCEFLASG)
2349 INT origlen = strlenW(lphttpHdr->lpszValue);
2350 INT valuelen = strlenW(value);
2352 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
2355 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2357 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2360 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2363 len = origlen + valuelen + ((ch > 0) ? 1 : 0);
2365 lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
2368 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
2371 lphttpHdr->lpszValue[origlen] = ch;
2375 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
2376 lphttpHdr->lpszValue[len] = '\0';
2381 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
2382 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2386 TRACE("<-- %d\n",bSuccess);
2391 /***********************************************************************
2392 * HTTP_CloseConnection (internal)
2394 * Close socket connection
2397 VOID HTTP_CloseConnection(LPWININETHTTPREQW lpwhr)
2399 LPWININETHTTPSESSIONW lpwhs = NULL;
2400 LPWININETAPPINFOW hIC = NULL;
2402 TRACE("%p\n",lpwhr);
2404 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
2405 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2407 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
2408 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2410 if (NETCON_connected(&lpwhr->netConnection))
2412 NETCON_close(&lpwhr->netConnection);
2415 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
2416 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2420 /***********************************************************************
2421 * HTTP_CloseHTTPRequestHandle (internal)
2423 * Deallocate request handle
2426 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr)
2429 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
2433 if (NETCON_connected(&lpwhr->netConnection))
2434 HTTP_CloseConnection(lpwhr);
2436 if (lpwhr->lpszPath)
2437 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2438 if (lpwhr->lpszVerb)
2439 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
2440 if (lpwhr->lpszHostName)
2441 HeapFree(GetProcessHeap(), 0, lpwhr->lpszHostName);
2442 if (lpwhr->lpszRawHeaders)
2443 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2445 for (i = 0; i <= HTTP_QUERY_MAX; i++)
2447 if (lpwhr->StdHeaders[i].lpszField)
2448 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszField);
2449 if (lpwhr->StdHeaders[i].lpszValue)
2450 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszValue);
2453 for (i = 0; i < lpwhr->nCustHeaders; i++)
2455 if (lpwhr->pCustHeaders[i].lpszField)
2456 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
2457 if (lpwhr->pCustHeaders[i].lpszValue)
2458 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
2461 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
2462 HeapFree(GetProcessHeap(), 0, lpwhr);
2466 /***********************************************************************
2467 * HTTP_CloseHTTPSessionHandle (internal)
2469 * Deallocate session handle
2472 void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr)
2474 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
2476 TRACE("%p\n", lpwhs);
2478 if (lpwhs->lpszServerName)
2479 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2480 if (lpwhs->lpszUserName)
2481 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2482 HeapFree(GetProcessHeap(), 0, lpwhs);
2486 /***********************************************************************
2487 * HTTP_GetCustomHeaderIndex (internal)
2489 * Return index of custom header from header array
2492 INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField)
2496 TRACE("%s\n", debugstr_w(lpszField));
2498 for (index = 0; index < lpwhr->nCustHeaders; index++)
2500 if (!strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
2505 if (index >= lpwhr->nCustHeaders)
2508 TRACE("Return: %d\n", index);
2513 /***********************************************************************
2514 * HTTP_InsertCustomHeader (internal)
2516 * Insert header into array
2519 BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
2522 LPHTTPHEADERW lph = NULL;
2525 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
2526 count = lpwhr->nCustHeaders + 1;
2528 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
2530 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
2534 lpwhr->pCustHeaders = lph;
2535 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
2536 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
2537 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
2538 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
2539 lpwhr->nCustHeaders++;
2544 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2551 /***********************************************************************
2552 * HTTP_DeleteCustomHeader (internal)
2554 * Delete header from array
2555 * If this function is called, the indexs may change.
2557 BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, INT index)
2559 if( lpwhr->nCustHeaders <= 0 )
2561 if( lpwhr->nCustHeaders >= index )
2563 lpwhr->nCustHeaders--;
2565 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
2566 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
2567 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
2572 /***********************************************************************
2573 * IsHostInProxyBypassList (@)
2578 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
2580 FIXME("STUB: flags=%ld host=%s length=%ld\n",flags,szHost,length);