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_szReferer[] = {'R','e','f','e','r','e','r',0};
62 static const WCHAR g_szAccept[] = {'A','c','c','e','p','t',0};
63 static const WCHAR g_szUserAgent[] = {'U','s','e','r','-','A','g','e','n','t',0};
64 static const WCHAR g_szHost[] = {'H','o','s','t',0};
67 #define HTTPHEADER g_szHttp
68 #define MAXHOSTNAME 100
69 #define MAX_FIELD_VALUE_LEN 256
70 #define MAX_FIELD_LEN 256
72 #define HTTP_REFERER g_szReferer
73 #define HTTP_ACCEPT g_szAccept
74 #define HTTP_USERAGENT g_szUserAgent
76 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
77 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
78 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
79 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
80 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
81 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
82 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
85 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr);
86 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr);
87 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 LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
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, DWORD 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 BOOL bSuccess = FALSE;
174 TRACE("copying header: %s\n", debugstr_w(lpszHeader));
176 if( dwHeaderLength == ~0UL )
177 len = strlenW(lpszHeader);
179 len = dwHeaderLength;
180 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
181 strncpyW( buffer, lpszHeader, len );
188 LPWSTR * pFieldAndValue;
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 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
211 bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
212 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
213 HTTP_FreeTokens(pFieldAndValue);
219 HeapFree(GetProcessHeap(), 0, buffer);
224 /***********************************************************************
225 * HttpAddRequestHeadersW (WININET.@)
227 * Adds one or more HTTP header to the request handler
234 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
235 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
237 BOOL bSuccess = FALSE;
238 LPWININETHTTPREQW lpwhr;
240 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_w(lpszHeader), dwHeaderLength,
246 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
247 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
249 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
252 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
255 WININET_Release( &lpwhr->hdr );
260 /***********************************************************************
261 * HttpAddRequestHeadersA (WININET.@)
263 * Adds one or more HTTP header to the request handler
270 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
271 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
277 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_a(lpszHeader), dwHeaderLength,
280 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
281 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
282 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
283 if( dwHeaderLength != ~0UL )
284 dwHeaderLength = len;
286 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
288 HeapFree( GetProcessHeap(), 0, hdr );
293 /***********************************************************************
294 * HttpEndRequestA (WININET.@)
296 * Ends an HTTP request that was started by HttpSendRequestEx
303 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest, LPINTERNET_BUFFERSA lpBuffersOut,
304 DWORD dwFlags, DWORD dwContext)
310 /***********************************************************************
311 * HttpEndRequestW (WININET.@)
313 * Ends an HTTP request that was started by HttpSendRequestEx
320 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest, LPINTERNET_BUFFERSW lpBuffersOut,
321 DWORD dwFlags, DWORD dwContext)
327 /***********************************************************************
328 * HttpOpenRequestW (WININET.@)
330 * Open a HTTP request handle
333 * HINTERNET a HTTP request handle on success
337 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
338 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
339 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
340 DWORD dwFlags, DWORD dwContext)
342 LPWININETHTTPSESSIONW lpwhs;
343 HINTERNET handle = NULL;
345 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
346 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
347 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
349 if(lpszAcceptTypes!=NULL)
352 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
353 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
356 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
357 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
359 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
364 * My tests seem to show that the windows version does not
365 * become asynchronous until after this point. And anyhow
366 * if this call was asynchronous then how would you get the
367 * necessary HINTERNET pointer returned by this function.
370 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
371 lpszVersion, lpszReferrer, lpszAcceptTypes,
375 WININET_Release( &lpwhs->hdr );
376 TRACE("returning %p\n", handle);
381 /***********************************************************************
382 * HttpOpenRequestA (WININET.@)
384 * Open a HTTP request handle
387 * HINTERNET a HTTP request handle on success
391 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
392 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
393 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
394 DWORD dwFlags, DWORD dwContext)
396 LPWSTR szVerb = NULL, szObjectName = NULL;
397 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
399 INT acceptTypesCount;
400 HINTERNET rc = FALSE;
401 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
402 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
403 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
408 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
409 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
412 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
417 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
418 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
421 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
426 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
427 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
430 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
435 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
436 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
439 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
442 acceptTypesCount = 0;
445 /* find out how many there are */
446 while (lpszAcceptTypes[acceptTypesCount])
448 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
449 acceptTypesCount = 0;
450 while (lpszAcceptTypes[acceptTypesCount])
452 len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
454 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
455 if (!szAcceptTypes[acceptTypesCount] )
457 MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
458 -1, szAcceptTypes[acceptTypesCount], len );
461 szAcceptTypes[acceptTypesCount] = NULL;
463 else szAcceptTypes = 0;
465 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
466 szVersion, szReferrer,
467 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
472 acceptTypesCount = 0;
473 while (szAcceptTypes[acceptTypesCount])
475 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
478 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
480 HeapFree(GetProcessHeap(), 0, szReferrer);
481 HeapFree(GetProcessHeap(), 0, szVersion);
482 HeapFree(GetProcessHeap(), 0, szObjectName);
483 HeapFree(GetProcessHeap(), 0, szVerb);
488 /***********************************************************************
491 static UINT HTTP_Base64( LPCWSTR bin, LPWSTR base64 )
494 static LPSTR HTTP_Base64Enc =
495 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
499 /* first 6 bits, all from bin[0] */
500 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
501 x = (bin[0] & 3) << 4;
503 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
506 base64[n++] = HTTP_Base64Enc[x];
511 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
512 x = ( bin[1] & 0x0f ) << 2;
514 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
517 base64[n++] = HTTP_Base64Enc[x];
521 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
523 /* last 6 bits, all from bin [2] */
524 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
531 /***********************************************************************
532 * HTTP_EncodeBasicAuth
534 * Encode the basic authentication string for HTTP 1.1
536 static LPWSTR HTTP_EncodeBasicAuth( LPCWSTR username, LPCWSTR password)
540 static const WCHAR szBasic[] = {'B','a','s','i','c',' ',0};
541 static const WCHAR szColon[] = {':',0};
543 len = lstrlenW( username ) + 1 + lstrlenW ( password ) + 1;
544 in = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
548 len = lstrlenW(szBasic) +
549 (lstrlenW( username ) + 1 + lstrlenW ( password ))*2 + 1 + 1;
550 out = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
553 lstrcpyW( in, username );
554 lstrcatW( in, szColon );
555 lstrcatW( in, password );
556 lstrcpyW( out, szBasic );
557 HTTP_Base64( in, &out[strlenW(out)] );
559 HeapFree( GetProcessHeap(), 0, in );
564 /***********************************************************************
565 * HTTP_InsertProxyAuthorization
567 * Insert the basic authorization field in the request header
569 BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
570 LPCWSTR username, LPCWSTR password )
574 static const WCHAR szProxyAuthorization[] = {
575 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
577 hdr.lpszValue = HTTP_EncodeBasicAuth( username, password );
578 hdr.lpszField = (WCHAR *)szProxyAuthorization;
579 hdr.wFlags = HDR_ISREQUEST;
584 TRACE("Inserting %s = %s\n",
585 debugstr_w( hdr.lpszField ), debugstr_w( hdr.lpszValue ) );
587 /* remove the old proxy authorization header */
588 index = HTTP_GetCustomHeaderIndex( lpwhr, hdr.lpszField );
590 HTTP_DeleteCustomHeader( lpwhr, index );
592 HTTP_InsertCustomHeader(lpwhr, &hdr);
593 HeapFree( GetProcessHeap(), 0, hdr.lpszValue );
598 /***********************************************************************
601 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
602 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
604 WCHAR buf[MAXHOSTNAME];
605 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
607 static const WCHAR szNul[] = { 0 };
608 URL_COMPONENTSW UrlComponents;
609 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }, szSlash[] = { '/',0 } ;
610 static const WCHAR szFormat1[] = { 'h','t','t','p',':','/','/','%','s',0 };
611 static const WCHAR szFormat2[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
614 memset( &UrlComponents, 0, sizeof UrlComponents );
615 UrlComponents.dwStructSize = sizeof UrlComponents;
616 UrlComponents.lpszHostName = buf;
617 UrlComponents.dwHostNameLength = MAXHOSTNAME;
619 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
620 buf,strlenW(szHttp),szHttp,strlenW(szHttp)) )
621 sprintfW(proxy, szFormat1, hIC->lpszProxy);
624 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
626 if( UrlComponents.dwHostNameLength == 0 )
629 if( !lpwhr->lpszPath )
630 lpwhr->lpszPath = (LPWSTR)szNul;
631 TRACE("server='%s' path='%s'\n",
632 debugstr_w(lpwhs->lpszServerName), debugstr_w(lpwhr->lpszPath));
633 /* for constant 15 see above */
634 len = strlenW(lpwhs->lpszServerName) + strlenW(lpwhr->lpszPath) + 15;
635 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
637 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
638 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
640 sprintfW(url, szFormat2, lpwhs->lpszServerName, lpwhs->nServerPort);
642 if( lpwhr->lpszPath[0] != '/' )
643 strcatW( url, szSlash );
644 strcatW(url, lpwhr->lpszPath);
645 if(lpwhr->lpszPath != szNul)
646 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
647 lpwhr->lpszPath = url;
648 /* FIXME: Do I have to free lpwhs->lpszServerName here ? */
649 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
650 lpwhs->nServerPort = UrlComponents.nPort;
655 /***********************************************************************
656 * HTTP_HttpOpenRequestW (internal)
658 * Open a HTTP request handle
661 * HINTERNET a HTTP request handle on success
665 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
666 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
667 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
668 DWORD dwFlags, DWORD dwContext)
670 LPWININETAPPINFOW hIC = NULL;
671 LPWININETHTTPREQW lpwhr;
673 LPWSTR lpszUrl = NULL;
675 HINTERNET handle = NULL;
676 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
678 INTERNET_ASYNC_RESULT iar;
682 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
683 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
685 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
688 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
691 lpwhr->hdr.htype = WH_HHTTPREQ;
692 lpwhr->hdr.lpwhparent = WININET_AddRef( &lpwhs->hdr );
693 lpwhr->hdr.dwFlags = dwFlags;
694 lpwhr->hdr.dwContext = dwContext;
695 lpwhr->hdr.dwRefCount = 1;
696 lpwhr->hdr.destroy = HTTP_CloseHTTPRequestHandle;
697 lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
699 handle = WININET_AllocHandle( &lpwhr->hdr );
702 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
706 NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE);
708 if (NULL != lpszObjectName && strlenW(lpszObjectName)) {
712 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
714 len = strlenW(lpszObjectName)+1;
715 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
716 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
717 URL_ESCAPE_SPACES_ONLY);
720 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(lpszObjectName),rc);
721 strcpyW(lpwhr->lpszPath,lpszObjectName);
725 if (NULL != lpszReferrer && strlenW(lpszReferrer))
726 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDHDR_FLAG_COALESCE);
728 if(lpszAcceptTypes!=NULL)
731 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
732 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i], HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_REQ|HTTP_ADDHDR_FLAG_ADD_IF_NEW);
735 if (NULL == lpszVerb)
737 static const WCHAR szGet[] = {'G','E','T',0};
738 lpwhr->lpszVerb = WININET_strdupW(szGet);
740 else if (strlenW(lpszVerb))
741 lpwhr->lpszVerb = WININET_strdupW(lpszVerb);
743 if (NULL != lpszReferrer && strlenW(lpszReferrer))
745 WCHAR buf[MAXHOSTNAME];
746 URL_COMPONENTSW UrlComponents;
748 memset( &UrlComponents, 0, sizeof UrlComponents );
749 UrlComponents.dwStructSize = sizeof UrlComponents;
750 UrlComponents.lpszHostName = buf;
751 UrlComponents.dwHostNameLength = MAXHOSTNAME;
753 InternetCrackUrlW(lpszReferrer, 0, 0, &UrlComponents);
754 if (strlenW(UrlComponents.lpszHostName))
755 HTTP_ProcessHeader(lpwhr, g_szHost, UrlComponents.lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
758 HTTP_ProcessHeader(lpwhr, g_szHost, lpwhs->lpszServerName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
760 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
761 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
766 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
768 len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
769 agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
770 sprintfW(agent_header, user_agent, hIC->lpszAgent );
772 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
773 HTTP_ADDREQ_FLAG_ADD);
774 HeapFree(GetProcessHeap(), 0, agent_header);
777 len = strlenW(lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue) + strlenW(szUrlForm);
778 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
779 sprintfW( lpszUrl, szUrlForm, lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue );
781 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
782 InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
785 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
786 static const WCHAR szcrlf[] = {'\r','\n',0};
788 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
790 cnt += sprintfW(lpszCookies, szCookie);
791 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
792 strcatW(lpszCookies, szcrlf);
794 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
795 HTTP_ADDREQ_FLAG_ADD);
796 HeapFree(GetProcessHeap(), 0, lpszCookies);
798 HeapFree(GetProcessHeap(), 0, lpszUrl);
801 iar.dwResult = (DWORD_PTR)handle;
802 iar.dwError = ERROR_SUCCESS;
804 SendAsyncCallback(&lpwhs->hdr, dwContext,
805 INTERNET_STATUS_HANDLE_CREATED, &iar,
806 sizeof(INTERNET_ASYNC_RESULT));
809 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
813 * According to my tests. The name is not resolved until a request is Opened
815 SendAsyncCallback(&lpwhr->hdr, dwContext,
816 INTERNET_STATUS_RESOLVING_NAME,
817 lpwhs->lpszServerName,
818 strlenW(lpwhs->lpszServerName)+1);
819 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
820 &lpwhs->phostent, &lpwhs->socketAddress))
822 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
823 InternetCloseHandle( handle );
828 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
829 INTERNET_STATUS_NAME_RESOLVED,
830 &(lpwhs->socketAddress),
831 sizeof(struct sockaddr_in));
835 WININET_Release( &lpwhr->hdr );
837 TRACE("<-- %p (%p)\n", handle, lpwhr);
841 /***********************************************************************
842 * HTTP_HttpQueryInfoW (internal)
844 BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
845 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
847 LPHTTPHEADERW lphttpHdr = NULL;
848 BOOL bSuccess = FALSE;
850 /* Find requested header structure */
851 if ((dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK) == HTTP_QUERY_CUSTOM)
853 INT index = HTTP_GetCustomHeaderIndex(lpwhr, (LPWSTR)lpBuffer);
858 lphttpHdr = &lpwhr->pCustHeaders[index];
862 INT index = dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK;
864 if (index == HTTP_QUERY_RAW_HEADERS_CRLF)
866 DWORD len = strlenW(lpwhr->lpszRawHeaders);
867 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
869 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
870 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
873 memcpy(lpBuffer, lpwhr->lpszRawHeaders, (len+1)*sizeof(WCHAR));
874 *lpdwBufferLength = len * sizeof(WCHAR);
876 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
880 else if (index == HTTP_QUERY_RAW_HEADERS)
882 static const WCHAR szCrLf[] = {'\r','\n',0};
883 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
885 LPWSTR pszString = (WCHAR*)lpBuffer;
887 for (i = 0; ppszRawHeaderLines[i]; i++)
888 size += strlenW(ppszRawHeaderLines[i]) + 1;
890 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
892 HTTP_FreeTokens(ppszRawHeaderLines);
893 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
894 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
898 for (i = 0; ppszRawHeaderLines[i]; i++)
900 DWORD len = strlenW(ppszRawHeaderLines[i]);
901 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
906 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
908 *lpdwBufferLength = size * sizeof(WCHAR);
909 HTTP_FreeTokens(ppszRawHeaderLines);
913 else if (index >= 0 && index <= HTTP_QUERY_MAX && lpwhr->StdHeaders[index].lpszValue)
915 lphttpHdr = &lpwhr->StdHeaders[index];
919 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
924 /* Ensure header satisifies requested attributes */
925 if ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
926 (~lphttpHdr->wFlags & HDR_ISREQUEST))
928 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
932 /* coalesce value to reuqested type */
933 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
935 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
938 TRACE(" returning number : %d\n", *(int *)lpBuffer);
940 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
946 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
948 tmpTM = *gmtime(&tmpTime);
949 STHook = (SYSTEMTIME *) lpBuffer;
953 STHook->wDay = tmpTM.tm_mday;
954 STHook->wHour = tmpTM.tm_hour;
955 STHook->wMilliseconds = 0;
956 STHook->wMinute = tmpTM.tm_min;
957 STHook->wDayOfWeek = tmpTM.tm_wday;
958 STHook->wMonth = tmpTM.tm_mon + 1;
959 STHook->wSecond = tmpTM.tm_sec;
960 STHook->wYear = tmpTM.tm_year;
964 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
965 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
966 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
968 else if (dwInfoLevel & HTTP_QUERY_FLAG_COALESCE)
970 if (*lpdwIndex >= lphttpHdr->wCount)
972 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
976 /* Copy strncpyW(lpBuffer, lphttpHdr[*lpdwIndex], len); */
982 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
984 if (len > *lpdwBufferLength)
986 *lpdwBufferLength = len;
987 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
991 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
992 *lpdwBufferLength = len - sizeof(WCHAR);
995 TRACE(" returning string : '%s'\n", debugstr_w(lpBuffer));
1000 /***********************************************************************
1001 * HttpQueryInfoW (WININET.@)
1003 * Queries for information about an HTTP request
1010 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1011 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1013 BOOL bSuccess = FALSE;
1014 LPWININETHTTPREQW lpwhr;
1016 if (TRACE_ON(wininet)) {
1017 #define FE(x) { x, #x }
1018 static const wininet_flag_info query_flags[] = {
1019 FE(HTTP_QUERY_MIME_VERSION),
1020 FE(HTTP_QUERY_CONTENT_TYPE),
1021 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
1022 FE(HTTP_QUERY_CONTENT_ID),
1023 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
1024 FE(HTTP_QUERY_CONTENT_LENGTH),
1025 FE(HTTP_QUERY_CONTENT_LANGUAGE),
1026 FE(HTTP_QUERY_ALLOW),
1027 FE(HTTP_QUERY_PUBLIC),
1028 FE(HTTP_QUERY_DATE),
1029 FE(HTTP_QUERY_EXPIRES),
1030 FE(HTTP_QUERY_LAST_MODIFIED),
1031 FE(HTTP_QUERY_MESSAGE_ID),
1033 FE(HTTP_QUERY_DERIVED_FROM),
1034 FE(HTTP_QUERY_COST),
1035 FE(HTTP_QUERY_LINK),
1036 FE(HTTP_QUERY_PRAGMA),
1037 FE(HTTP_QUERY_VERSION),
1038 FE(HTTP_QUERY_STATUS_CODE),
1039 FE(HTTP_QUERY_STATUS_TEXT),
1040 FE(HTTP_QUERY_RAW_HEADERS),
1041 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
1042 FE(HTTP_QUERY_CONNECTION),
1043 FE(HTTP_QUERY_ACCEPT),
1044 FE(HTTP_QUERY_ACCEPT_CHARSET),
1045 FE(HTTP_QUERY_ACCEPT_ENCODING),
1046 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
1047 FE(HTTP_QUERY_AUTHORIZATION),
1048 FE(HTTP_QUERY_CONTENT_ENCODING),
1049 FE(HTTP_QUERY_FORWARDED),
1050 FE(HTTP_QUERY_FROM),
1051 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
1052 FE(HTTP_QUERY_LOCATION),
1053 FE(HTTP_QUERY_ORIG_URI),
1054 FE(HTTP_QUERY_REFERER),
1055 FE(HTTP_QUERY_RETRY_AFTER),
1056 FE(HTTP_QUERY_SERVER),
1057 FE(HTTP_QUERY_TITLE),
1058 FE(HTTP_QUERY_USER_AGENT),
1059 FE(HTTP_QUERY_WWW_AUTHENTICATE),
1060 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
1061 FE(HTTP_QUERY_ACCEPT_RANGES),
1062 FE(HTTP_QUERY_SET_COOKIE),
1063 FE(HTTP_QUERY_COOKIE),
1064 FE(HTTP_QUERY_REQUEST_METHOD),
1065 FE(HTTP_QUERY_REFRESH),
1066 FE(HTTP_QUERY_CONTENT_DISPOSITION),
1068 FE(HTTP_QUERY_CACHE_CONTROL),
1069 FE(HTTP_QUERY_CONTENT_BASE),
1070 FE(HTTP_QUERY_CONTENT_LOCATION),
1071 FE(HTTP_QUERY_CONTENT_MD5),
1072 FE(HTTP_QUERY_CONTENT_RANGE),
1073 FE(HTTP_QUERY_ETAG),
1074 FE(HTTP_QUERY_HOST),
1075 FE(HTTP_QUERY_IF_MATCH),
1076 FE(HTTP_QUERY_IF_NONE_MATCH),
1077 FE(HTTP_QUERY_IF_RANGE),
1078 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
1079 FE(HTTP_QUERY_MAX_FORWARDS),
1080 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
1081 FE(HTTP_QUERY_RANGE),
1082 FE(HTTP_QUERY_TRANSFER_ENCODING),
1083 FE(HTTP_QUERY_UPGRADE),
1084 FE(HTTP_QUERY_VARY),
1086 FE(HTTP_QUERY_WARNING),
1087 FE(HTTP_QUERY_CUSTOM)
1089 static const wininet_flag_info modifier_flags[] = {
1090 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
1091 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
1092 FE(HTTP_QUERY_FLAG_NUMBER),
1093 FE(HTTP_QUERY_FLAG_COALESCE)
1096 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
1097 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
1100 TRACE("(%p, 0x%08lx)--> %ld\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
1101 TRACE(" Attribute:");
1102 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
1103 if (query_flags[i].val == info) {
1104 DPRINTF(" %s", query_flags[i].name);
1108 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
1109 DPRINTF(" Unknown (%08lx)", info);
1112 DPRINTF(" Modifier:");
1113 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
1114 if (modifier_flags[i].val & info_mod) {
1115 DPRINTF(" %s", modifier_flags[i].name);
1116 info_mod &= ~ modifier_flags[i].val;
1121 DPRINTF(" Unknown (%08lx)", info_mod);
1126 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1127 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1129 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1133 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
1134 lpBuffer, lpdwBufferLength, lpdwIndex);
1138 WININET_Release( &lpwhr->hdr );
1140 TRACE("%d <--\n", bSuccess);
1144 /***********************************************************************
1145 * HttpQueryInfoA (WININET.@)
1147 * Queries for information about an HTTP request
1154 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1155 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1161 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
1162 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
1164 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
1165 lpdwBufferLength, lpdwIndex );
1168 len = (*lpdwBufferLength)*sizeof(WCHAR);
1169 bufferW = HeapAlloc( GetProcessHeap(), 0, len );
1170 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
1174 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
1175 lpBuffer, *lpdwBufferLength, NULL, NULL );
1176 *lpdwBufferLength = len - 1;
1178 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
1181 /* since the strings being returned from HttpQueryInfoW should be
1182 * only ASCII characters, it is reasonable to assume that all of
1183 * the Unicode characters can be reduced to a single byte */
1184 *lpdwBufferLength = len / sizeof(WCHAR);
1186 HeapFree(GetProcessHeap(), 0, bufferW );
1191 /***********************************************************************
1192 * HttpSendRequestExA (WININET.@)
1194 * Sends the specified request to the HTTP server and allows chunked
1197 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
1198 LPINTERNET_BUFFERSA lpBuffersIn,
1199 LPINTERNET_BUFFERSA lpBuffersOut,
1200 DWORD dwFlags, DWORD dwContext)
1202 FIXME("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersIn,
1203 lpBuffersOut, dwFlags, dwContext);
1207 /***********************************************************************
1208 * HttpSendRequestW (WININET.@)
1210 * Sends the specified request to the HTTP server
1217 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
1218 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1220 LPWININETHTTPREQW lpwhr;
1221 LPWININETHTTPSESSIONW lpwhs = NULL;
1222 LPWININETAPPINFOW hIC = NULL;
1225 TRACE("%p, %p (%s), %li, %p, %li)\n", hHttpRequest,
1226 lpszHeaders, debugstr_w(lpszHeaders), dwHeaderLength, lpOptional, dwOptionalLength);
1228 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1229 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1231 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1236 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1237 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1239 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1244 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1245 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1247 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1252 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1254 WORKREQUEST workRequest;
1255 struct WORKREQ_HTTPSENDREQUESTW *req;
1257 workRequest.asyncall = HTTPSENDREQUESTW;
1258 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
1259 req = &workRequest.u.HttpSendRequestW;
1261 req->lpszHeader = WININET_strdupW(lpszHeaders);
1263 req->lpszHeader = 0;
1264 req->dwHeaderLength = dwHeaderLength;
1265 req->lpOptional = lpOptional;
1266 req->dwOptionalLength = dwOptionalLength;
1268 INTERNET_AsyncCall(&workRequest);
1270 * This is from windows.
1272 SetLastError(ERROR_IO_PENDING);
1277 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
1278 dwHeaderLength, lpOptional, dwOptionalLength);
1282 WININET_Release( &lpwhr->hdr );
1286 /***********************************************************************
1287 * HttpSendRequestA (WININET.@)
1289 * Sends the specified request to the HTTP server
1296 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
1297 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1300 LPWSTR szHeaders=NULL;
1301 DWORD nLen=dwHeaderLength;
1302 if(lpszHeaders!=NULL)
1304 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
1305 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
1306 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
1308 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
1309 HeapFree(GetProcessHeap(),0,szHeaders);
1313 /***********************************************************************
1314 * HTTP_HandleRedirect (internal)
1316 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl, LPCWSTR lpszHeaders,
1317 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength)
1319 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1320 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1325 /* if it's an absolute path, keep the same session info */
1326 strcpyW(path,lpszUrl);
1328 else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1330 TRACE("Redirect through proxy\n");
1331 strcpyW(path,lpszUrl);
1335 URL_COMPONENTSW urlComponents;
1336 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
1337 WCHAR password[1024], extra[1024];
1338 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
1339 urlComponents.lpszScheme = protocol;
1340 urlComponents.dwSchemeLength = 32;
1341 urlComponents.lpszHostName = hostName;
1342 urlComponents.dwHostNameLength = MAXHOSTNAME;
1343 urlComponents.lpszUserName = userName;
1344 urlComponents.dwUserNameLength = 1024;
1345 urlComponents.lpszPassword = password;
1346 urlComponents.dwPasswordLength = 1024;
1347 urlComponents.lpszUrlPath = path;
1348 urlComponents.dwUrlPathLength = 2048;
1349 urlComponents.lpszExtraInfo = extra;
1350 urlComponents.dwExtraInfoLength = 1024;
1351 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
1354 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1355 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1359 * This upsets redirects to binary files on sourceforge.net
1360 * and gives an html page instead of the target file
1361 * Examination of the HTTP request sent by native wininet.dll
1362 * reveals that it doesn't send a referrer in that case.
1363 * Maybe there's a flag that enables this, or maybe a referrer
1364 * shouldn't be added in case of a redirect.
1367 /* consider the current host as the referrer */
1368 if (NULL != lpwhs->lpszServerName && strlenW(lpwhs->lpszServerName))
1369 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
1370 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
1371 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
1374 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1375 lpwhs->lpszServerName = WININET_strdupW(hostName);
1376 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
1377 lpwhs->lpszUserName = WININET_strdupW(userName);
1378 lpwhs->nServerPort = urlComponents.nPort;
1380 HTTP_ProcessHeader(lpwhr, g_szHost, hostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1382 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1383 INTERNET_STATUS_RESOLVING_NAME,
1384 lpwhs->lpszServerName,
1385 strlenW(lpwhs->lpszServerName)+1);
1387 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1388 &lpwhs->phostent, &lpwhs->socketAddress))
1390 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1394 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1395 INTERNET_STATUS_NAME_RESOLVED,
1396 &(lpwhs->socketAddress),
1397 sizeof(struct sockaddr_in));
1401 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1402 lpwhr->lpszPath=NULL;
1408 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
1409 if (rc != E_POINTER)
1410 needed = strlenW(path)+1;
1411 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
1412 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
1413 URL_ESCAPE_SPACES_ONLY);
1416 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(path),rc);
1417 strcpyW(lpwhr->lpszPath,path);
1421 return HTTP_HttpSendRequestW(lpwhr, lpszHeaders, dwHeaderLength, lpOptional, dwOptionalLength);
1424 /***********************************************************************
1425 * HTTP_build_req (internal)
1427 * concatenate all the strings in the request together
1429 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
1434 for( t = list; *t ; t++ )
1435 len += strlenW( *t );
1438 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1441 for( t = list; *t ; t++ )
1447 /***********************************************************************
1448 * HTTP_HttpSendRequestW (internal)
1450 * Sends the specified request to the HTTP server
1457 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
1458 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1462 BOOL bSuccess = FALSE;
1463 LPWSTR requestString = NULL;
1465 LPWININETHTTPSESSIONW lpwhs = NULL;
1466 LPWININETAPPINFOW hIC = NULL;
1467 BOOL loop_next = FALSE;
1468 int CustHeaderIndex;
1469 INTERNET_ASYNC_RESULT iar;
1471 TRACE("--> %p\n", lpwhr);
1473 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
1475 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1476 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1478 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1482 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1483 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1485 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1489 /* Clear any error information */
1490 INTERNET_SetLastError(0);
1493 /* We must have a verb */
1494 if (NULL == lpwhr->lpszVerb)
1499 /* if we are using optional stuff, we must add the fixed header of that option length */
1500 if (lpOptional && dwOptionalLength)
1502 static const WCHAR szContentLength[] = {
1503 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0};
1504 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \n\r */ + 20 /* int */ ];
1505 sprintfW(contentLengthStr, szContentLength, dwOptionalLength);
1506 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD);
1511 static const WCHAR szSlash[] = { '/',0 };
1512 static const WCHAR szSpace[] = { ' ',0 };
1513 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
1514 static const WCHAR szcrlf[] = {'\r','\n', 0};
1515 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
1516 static const WCHAR szSetCookie[] = {'S','e','t','-','C','o','o','k','i','e',0 };
1517 static const WCHAR szColon[] = { ':',' ',0 };
1523 TRACE("Going to url %s %s\n", debugstr_w(lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue), debugstr_w(lpwhr->lpszPath));
1526 /* If we don't have a path we set it to root */
1527 if (NULL == lpwhr->lpszPath)
1528 lpwhr->lpszPath = WININET_strdupW(szSlash);
1529 else /* remove \r and \n*/
1531 int nLen = strlenW(lpwhr->lpszPath);
1532 while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
1535 lpwhr->lpszPath[nLen]='\0';
1539 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1540 lpwhr->lpszPath, strlenW(szHttp), szHttp, strlenW(szHttp) )
1541 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
1543 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
1544 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
1546 strcpyW(fixurl + 1, lpwhr->lpszPath);
1547 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
1548 lpwhr->lpszPath = fixurl;
1551 /* add the headers the caller supplied */
1552 if( lpszHeaders && dwHeaderLength )
1554 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
1555 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
1558 /* if there's a proxy username and password, add it to the headers */
1559 if (hIC && (hIC->lpszProxyUsername || hIC->lpszProxyPassword ))
1560 HTTP_InsertProxyAuthorization(lpwhr, hIC->lpszProxyUsername, hIC->lpszProxyPassword);
1562 /* allocate space for an array of all the string pointers to be added */
1563 len = (HTTP_QUERY_MAX + lpwhr->nCustHeaders)*4 + 9;
1564 req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
1566 /* add the verb, path and HTTP/1.0 */
1568 req[n++] = lpwhr->lpszVerb;
1570 req[n++] = lpwhr->lpszPath;
1571 req[n++] = HTTPHEADER;
1573 /* Append standard request headers */
1574 for (i = 0; i <= HTTP_QUERY_MAX; i++)
1576 if (lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST)
1579 req[n++] = lpwhr->StdHeaders[i].lpszField;
1581 req[n++] = lpwhr->StdHeaders[i].lpszValue;
1583 TRACE("Adding header %s (%s)\n",
1584 debugstr_w(lpwhr->StdHeaders[i].lpszField),
1585 debugstr_w(lpwhr->StdHeaders[i].lpszValue));
1589 /* Append custom request heades */
1590 for (i = 0; i < lpwhr->nCustHeaders; i++)
1592 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
1595 req[n++] = lpwhr->pCustHeaders[i].lpszField;
1597 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
1599 TRACE("Adding custom header %s (%s)\n",
1600 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
1601 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
1606 ERR("oops. buffer overrun\n");
1609 requestString = HTTP_build_req( req, 4 );
1610 HeapFree( GetProcessHeap(), 0, req );
1613 * Set (header) termination string for request
1614 * Make sure there's exactly two new lines at the end of the request
1616 p = &requestString[strlenW(requestString)-1];
1617 while ( (*p == '\n') || (*p == '\r') )
1619 strcpyW( p+1, sztwocrlf );
1621 TRACE("Request header -> %s\n", debugstr_w(requestString) );
1623 /* Send the request and store the results */
1624 if (!HTTP_OpenConnection(lpwhr))
1627 /* send the request as ASCII, tack on the optional data */
1629 dwOptionalLength = 0;
1630 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
1631 NULL, 0, NULL, NULL );
1632 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
1633 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
1634 ascii_req, len, NULL, NULL );
1636 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
1637 len = (len + dwOptionalLength - 1);
1639 TRACE("full request -> %s\n", ascii_req );
1641 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1642 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
1644 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
1645 HeapFree( GetProcessHeap(), 0, ascii_req );
1647 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1648 INTERNET_STATUS_REQUEST_SENT,
1649 &len,sizeof(DWORD));
1651 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1652 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
1657 responseLen = HTTP_GetResponseHeaders(lpwhr);
1661 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1662 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
1665 /* process headers here. Is this right? */
1666 CustHeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSetCookie);
1667 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && (CustHeaderIndex >= 0))
1669 LPHTTPHEADERW setCookieHeader;
1670 int nPosStart = 0, nPosEnd = 0, len;
1671 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
1673 setCookieHeader = &lpwhr->pCustHeaders[CustHeaderIndex];
1675 while (setCookieHeader->lpszValue[nPosEnd] != '\0')
1677 LPWSTR buf_cookie, cookie_name, cookie_data;
1679 LPWSTR domain = NULL;
1681 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
1682 setCookieHeader->lpszValue[nPosEnd] != '\0')
1686 if (setCookieHeader->lpszValue[nPosEnd] == ';')
1688 /* fixme: not case sensitive, strcasestr is gnu only */
1689 int nDomainPosEnd = 0;
1690 int nDomainPosStart = 0, nDomainLength = 0;
1691 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
1692 LPWSTR lpszDomain = strstrW(&setCookieHeader->lpszValue[nPosEnd], szDomain);
1694 { /* they have specified their own domain, lets use it */
1695 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
1696 lpszDomain[nDomainPosEnd] != '\0')
1700 nDomainPosStart = strlenW(szDomain);
1701 nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
1702 domain = HeapAlloc(GetProcessHeap(), 0, (nDomainLength + 1)*sizeof(WCHAR));
1703 strncpyW(domain, &lpszDomain[nDomainPosStart], nDomainLength);
1704 domain[nDomainLength] = '\0';
1707 if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
1708 buf_cookie = HeapAlloc(GetProcessHeap(), 0, ((nPosEnd - nPosStart) + 1)*sizeof(WCHAR));
1709 strncpyW(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart));
1710 buf_cookie[(nPosEnd - nPosStart)] = '\0';
1711 TRACE("%s\n", debugstr_w(buf_cookie));
1712 while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
1716 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
1718 HeapFree(GetProcessHeap(), 0, buf_cookie);
1722 cookie_name = HeapAlloc(GetProcessHeap(), 0, (nEqualPos + 1)*sizeof(WCHAR));
1723 strncpyW(cookie_name, buf_cookie, nEqualPos);
1724 cookie_name[nEqualPos] = '\0';
1725 cookie_data = &buf_cookie[nEqualPos + 1];
1728 len = strlenW((domain ? domain : lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue)) +
1729 strlenW(lpwhr->lpszPath) + 9;
1730 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1731 sprintfW(buf_url, szFmt, (domain ? domain : lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue)); /* FIXME PATH!!! */
1732 InternetSetCookieW(buf_url, cookie_name, cookie_data);
1734 HeapFree(GetProcessHeap(), 0, buf_url);
1735 HeapFree(GetProcessHeap(), 0, buf_cookie);
1736 HeapFree(GetProcessHeap(), 0, cookie_name);
1737 HeapFree(GetProcessHeap(), 0, domain);
1738 nPosStart = nPosEnd;
1746 HeapFree(GetProcessHeap(), 0, requestString);
1748 /* TODO: send notification for P3P header */
1750 if(!(hIC->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
1752 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
1753 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
1754 (dwCode==302 || dwCode==301))
1756 WCHAR szNewLocation[2048];
1757 DWORD dwBufferSize=2048;
1759 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
1761 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1762 INTERNET_STATUS_REDIRECT, szNewLocation,
1764 return HTTP_HandleRedirect(lpwhr, szNewLocation, lpszHeaders,
1765 dwHeaderLength, lpOptional, dwOptionalLength);
1771 iar.dwResult = (DWORD)bSuccess;
1772 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1774 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1775 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1776 sizeof(INTERNET_ASYNC_RESULT));
1783 /***********************************************************************
1784 * HTTP_Connect (internal)
1786 * Create http session handle
1789 * HINTERNET a session handle on success
1793 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
1794 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
1795 LPCWSTR lpszPassword, DWORD dwFlags, DWORD dwContext,
1796 DWORD dwInternalFlags)
1798 BOOL bSuccess = FALSE;
1799 LPWININETHTTPSESSIONW lpwhs = NULL;
1800 HINTERNET handle = NULL;
1804 assert( hIC->hdr.htype == WH_HINIT );
1806 hIC->hdr.dwContext = dwContext;
1808 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
1811 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1816 * According to my tests. The name is not resolved until a request is sent
1819 if (nServerPort == INTERNET_INVALID_PORT_NUMBER)
1820 nServerPort = INTERNET_DEFAULT_HTTP_PORT;
1822 lpwhs->hdr.htype = WH_HHTTPSESSION;
1823 lpwhs->hdr.lpwhparent = WININET_AddRef( &hIC->hdr );
1824 lpwhs->hdr.dwFlags = dwFlags;
1825 lpwhs->hdr.dwContext = dwContext;
1826 lpwhs->hdr.dwInternalFlags = dwInternalFlags;
1827 lpwhs->hdr.dwRefCount = 1;
1828 lpwhs->hdr.destroy = HTTP_CloseHTTPSessionHandle;
1829 lpwhs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
1831 handle = WININET_AllocHandle( &lpwhs->hdr );
1834 ERR("Failed to alloc handle\n");
1835 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1839 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
1840 if(strchrW(hIC->lpszProxy, ' '))
1841 FIXME("Several proxies not implemented.\n");
1842 if(hIC->lpszProxyBypass)
1843 FIXME("Proxy bypass is ignored.\n");
1845 if (NULL != lpszServerName)
1846 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
1847 if (NULL != lpszUserName)
1848 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
1849 lpwhs->nServerPort = nServerPort;
1851 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
1852 if (!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
1854 INTERNET_ASYNC_RESULT iar;
1856 iar.dwResult = (DWORD_PTR)handle;
1857 iar.dwError = ERROR_SUCCESS;
1859 SendAsyncCallback(&lpwhs->hdr, dwContext,
1860 INTERNET_STATUS_HANDLE_CREATED, &iar,
1861 sizeof(INTERNET_ASYNC_RESULT));
1868 WININET_Release( &lpwhs->hdr );
1871 * a INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
1875 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
1880 /***********************************************************************
1881 * HTTP_OpenConnection (internal)
1883 * Connect to a web server
1890 BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
1892 BOOL bSuccess = FALSE;
1893 LPWININETHTTPSESSIONW lpwhs;
1894 LPWININETAPPINFOW hIC = NULL;
1899 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1901 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1905 lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
1907 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1908 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1909 INTERNET_STATUS_CONNECTING_TO_SERVER,
1910 &(lpwhs->socketAddress),
1911 sizeof(struct sockaddr_in));
1913 if (!NETCON_create(&lpwhr->netConnection, lpwhs->phostent->h_addrtype,
1916 WARN("Socket creation failed\n");
1920 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
1921 sizeof(lpwhs->socketAddress)))
1923 WARN("Unable to connect to host (%s)\n", strerror(errno));
1927 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1928 INTERNET_STATUS_CONNECTED_TO_SERVER,
1929 &(lpwhs->socketAddress),
1930 sizeof(struct sockaddr_in));
1935 TRACE("%d <--\n", bSuccess);
1940 /***********************************************************************
1941 * HTTP_clear_response_headers (internal)
1943 * clear out any old response headers
1945 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
1949 for( i=0; i<=HTTP_QUERY_MAX; i++ )
1951 if( !lpwhr->StdHeaders[i].lpszField )
1953 if( !lpwhr->StdHeaders[i].lpszValue )
1955 if ( lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST )
1957 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[i], NULL );
1959 for( i=0; i<lpwhr->nCustHeaders; i++)
1961 if( !lpwhr->pCustHeaders[i].lpszField )
1963 if( !lpwhr->pCustHeaders[i].lpszValue )
1965 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
1967 HTTP_ReplaceHeaderValue( &lpwhr->pCustHeaders[i], NULL );
1971 /***********************************************************************
1972 * HTTP_GetResponseHeaders (internal)
1974 * Read server response
1981 BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr)
1984 WCHAR buffer[MAX_REPLY_LEN];
1985 DWORD buflen = MAX_REPLY_LEN;
1986 BOOL bSuccess = FALSE;
1988 static const WCHAR szCrLf[] = {'\r','\n',0};
1989 char bufferA[MAX_REPLY_LEN];
1990 LPWSTR status_code, status_text;
1991 DWORD cchMaxRawHeaders = 1024;
1992 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
1993 DWORD cchRawHeaders = 0;
1997 /* clear old response headers (eg. from a redirect response) */
1998 HTTP_clear_response_headers( lpwhr );
2000 if (!NETCON_connected(&lpwhr->netConnection))
2004 * HACK peek at the buffer
2006 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
2009 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
2011 buflen = MAX_REPLY_LEN;
2012 memset(buffer, 0, MAX_REPLY_LEN);
2013 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2015 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2017 /* regenerate raw headers */
2018 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2020 cchMaxRawHeaders *= 2;
2021 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2023 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2024 cchRawHeaders += (buflen-1);
2025 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2026 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2027 lpszRawHeaders[cchRawHeaders] = '\0';
2029 /* split the version from the status code */
2030 status_code = strchrW( buffer, ' ' );
2035 /* split the status code from the status text */
2036 status_text = strchrW( status_code, ' ' );
2041 TRACE("version [%s] status code [%s] status text [%s]\n",
2042 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
2043 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_VERSION], buffer );
2044 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_STATUS_CODE], status_code );
2045 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_STATUS_TEXT], status_text );
2047 /* Parse each response line */
2050 buflen = MAX_REPLY_LEN;
2051 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2053 LPWSTR * pFieldAndValue;
2055 TRACE("got line %s, now interpretting\n", debugstr_a(bufferA));
2056 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2058 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2060 cchMaxRawHeaders *= 2;
2061 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2063 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2064 cchRawHeaders += (buflen-1);
2065 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2066 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2067 lpszRawHeaders[cchRawHeaders] = '\0';
2069 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
2070 if (!pFieldAndValue)
2073 HTTP_ProcessHeader(lpwhr, pFieldAndValue[0], pFieldAndValue[1],
2074 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE);
2076 HTTP_FreeTokens(pFieldAndValue);
2086 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2087 lpwhr->lpszRawHeaders = lpszRawHeaders;
2088 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
2101 static void strip_spaces(LPWSTR start)
2106 while (*str == ' ' && *str != '\0')
2110 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
2112 end = start + strlenW(start) - 1;
2113 while (end >= start && *end == ' ')
2121 /***********************************************************************
2122 * HTTP_InterpretHttpHeader (internal)
2124 * Parse server response
2128 * Pointer to array of field, value, NULL on success.
2131 LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
2133 LPWSTR * pTokenPair;
2137 pTokenPair = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pTokenPair)*3);
2139 pszColon = strchrW(buffer, ':');
2140 /* must have two tokens */
2143 HTTP_FreeTokens(pTokenPair);
2147 pTokenPair[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon - buffer + 1) * sizeof(WCHAR));
2150 HTTP_FreeTokens(pTokenPair);
2153 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
2154 pTokenPair[0][pszColon - buffer] = '\0';
2158 len = strlenW(pszColon);
2159 pTokenPair[1] = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
2162 HTTP_FreeTokens(pTokenPair);
2165 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
2167 strip_spaces(pTokenPair[0]);
2168 strip_spaces(pTokenPair[1]);
2170 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
2175 /***********************************************************************
2176 * HTTP_GetStdHeaderIndex (internal)
2178 * Lookup field index in standard http header array
2180 * FIXME: This should be stuffed into a hash table
2182 INT HTTP_GetStdHeaderIndex(LPCWSTR lpszField)
2185 static const WCHAR szContentLength[] = {
2186 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
2187 static const WCHAR szQueryRange[] = {
2188 'R','a','n','g','e',0};
2189 static const WCHAR szContentRange[] = {
2190 'C','o','n','t','e','n','t','-','R','a','n','g','e',0};
2191 static const WCHAR szContentType[] = {
2192 'C','o','n','t','e','n','t','-','T','y','p','e',0};
2193 static const WCHAR szLastModified[] = {
2194 'L','a','s','t','-','M','o','d','i','f','i','e','d',0};
2195 static const WCHAR szLocation[] = {'L','o','c','a','t','i','o','n',0};
2196 static const WCHAR szAccept[] = {'A','c','c','e','p','t',0};
2197 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0};
2198 static const WCHAR szContentTrans[] = { 'C','o','n','t','e','n','t','-',
2199 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0};
2200 static const WCHAR szDate[] = { 'D','a','t','e',0};
2201 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0};
2202 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0};
2203 static const WCHAR szETag[] = { 'E','T','a','g',0};
2204 static const WCHAR szAcceptRanges[] = {
2205 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
2206 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
2207 static const WCHAR szMimeVersion[] = {
2208 'M','i','m','e','-','V','e','r','s','i','o','n', 0};
2209 static const WCHAR szPragma[] = { 'P','r','a','g','m','a', 0};
2210 static const WCHAR szCacheControl[] = {
2211 'C','a','c','h','e','-','C','o','n','t','r','o','l',0};
2212 static const WCHAR szUserAgent[] = { 'U','s','e','r','-','A','g','e','n','t',0};
2213 static const WCHAR szProxyAuth[] = {
2214 'P','r','o','x','y','-',
2215 'A','u','t','h','e','n','t','i','c','a','t','e', 0};
2216 static const WCHAR szContentEncoding[] = {
2217 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0};
2218 static const WCHAR szCookie[] = {'C','o','o','k','i','e',0};
2219 static const WCHAR szVary[] = {'V','a','r','y',0};
2220 static const WCHAR szVia[] = {'V','i','a',0};
2222 if (!strcmpiW(lpszField, szContentLength))
2223 index = HTTP_QUERY_CONTENT_LENGTH;
2224 else if (!strcmpiW(lpszField,szQueryRange))
2225 index = HTTP_QUERY_RANGE;
2226 else if (!strcmpiW(lpszField,szContentRange))
2227 index = HTTP_QUERY_CONTENT_RANGE;
2228 else if (!strcmpiW(lpszField,szContentType))
2229 index = HTTP_QUERY_CONTENT_TYPE;
2230 else if (!strcmpiW(lpszField,szLastModified))
2231 index = HTTP_QUERY_LAST_MODIFIED;
2232 else if (!strcmpiW(lpszField,szLocation))
2233 index = HTTP_QUERY_LOCATION;
2234 else if (!strcmpiW(lpszField,szAccept))
2235 index = HTTP_QUERY_ACCEPT;
2236 else if (!strcmpiW(lpszField,szReferer))
2237 index = HTTP_QUERY_REFERER;
2238 else if (!strcmpiW(lpszField,szContentTrans))
2239 index = HTTP_QUERY_CONTENT_TRANSFER_ENCODING;
2240 else if (!strcmpiW(lpszField,szDate))
2241 index = HTTP_QUERY_DATE;
2242 else if (!strcmpiW(lpszField,szServer))
2243 index = HTTP_QUERY_SERVER;
2244 else if (!strcmpiW(lpszField,szConnection))
2245 index = HTTP_QUERY_CONNECTION;
2246 else if (!strcmpiW(lpszField,szETag))
2247 index = HTTP_QUERY_ETAG;
2248 else if (!strcmpiW(lpszField,szAcceptRanges))
2249 index = HTTP_QUERY_ACCEPT_RANGES;
2250 else if (!strcmpiW(lpszField,szExpires))
2251 index = HTTP_QUERY_EXPIRES;
2252 else if (!strcmpiW(lpszField,szMimeVersion))
2253 index = HTTP_QUERY_MIME_VERSION;
2254 else if (!strcmpiW(lpszField,szPragma))
2255 index = HTTP_QUERY_PRAGMA;
2256 else if (!strcmpiW(lpszField,szCacheControl))
2257 index = HTTP_QUERY_CACHE_CONTROL;
2258 else if (!strcmpiW(lpszField,szUserAgent))
2259 index = HTTP_QUERY_USER_AGENT;
2260 else if (!strcmpiW(lpszField,szProxyAuth))
2261 index = HTTP_QUERY_PROXY_AUTHENTICATE;
2262 else if (!strcmpiW(lpszField,szContentEncoding))
2263 index = HTTP_QUERY_CONTENT_ENCODING;
2264 else if (!strcmpiW(lpszField,szCookie))
2265 index = HTTP_QUERY_COOKIE;
2266 else if (!strcmpiW(lpszField,szVary))
2267 index = HTTP_QUERY_VARY;
2268 else if (!strcmpiW(lpszField,szVia))
2269 index = HTTP_QUERY_VIA;
2270 else if (!strcmpiW(lpszField,g_szHost))
2271 index = HTTP_QUERY_HOST;
2274 TRACE("Couldn't find %s in standard header table\n", debugstr_w(lpszField));
2280 /***********************************************************************
2281 * HTTP_ReplaceHeaderValue (internal)
2283 BOOL HTTP_ReplaceHeaderValue( LPHTTPHEADERW lphttpHdr, LPCWSTR value )
2287 HeapFree( GetProcessHeap(), 0, lphttpHdr->lpszValue );
2288 lphttpHdr->lpszValue = NULL;
2291 len = strlenW(value);
2294 lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0,
2295 (len+1)*sizeof(WCHAR));
2296 strcpyW(lphttpHdr->lpszValue, value);
2301 /***********************************************************************
2302 * HTTP_ProcessHeader (internal)
2304 * Stuff header into header tables according to <dwModifier>
2308 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2310 BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
2312 LPHTTPHEADERW lphttpHdr = NULL;
2313 BOOL bSuccess = FALSE;
2316 TRACE("--> %s: %s - 0x%08lx\n", debugstr_w(field), debugstr_w(value), dwModifier);
2318 /* Adjust modifier flags */
2319 if (dwModifier & COALESCEFLASG)
2320 dwModifier |= HTTP_ADDHDR_FLAG_ADD;
2322 /* Try to get index into standard header array */
2323 index = HTTP_GetStdHeaderIndex(field);
2324 /* Don't let applications add Connection header to request */
2325 if ((index == HTTP_QUERY_CONNECTION) && (dwModifier & HTTP_ADDHDR_FLAG_REQ))
2327 else if (index >= 0)
2329 lphttpHdr = &lpwhr->StdHeaders[index];
2331 else /* Find or create new custom header */
2333 index = HTTP_GetCustomHeaderIndex(lpwhr, field);
2336 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
2340 lphttpHdr = &lpwhr->pCustHeaders[index];
2346 hdr.lpszField = (LPWSTR)field;
2347 hdr.lpszValue = (LPWSTR)value;
2348 hdr.wFlags = hdr.wCount = 0;
2350 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2351 hdr.wFlags |= HDR_ISREQUEST;
2353 return HTTP_InsertCustomHeader(lpwhr, &hdr);
2357 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2358 lphttpHdr->wFlags |= HDR_ISREQUEST;
2360 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
2362 if (!lphttpHdr->lpszValue && (dwModifier & (HTTP_ADDHDR_FLAG_ADD|HTTP_ADDHDR_FLAG_ADD_IF_NEW)))
2366 if (!lpwhr->StdHeaders[index].lpszField)
2368 lphttpHdr->lpszField = WININET_strdupW(field);
2370 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2371 lphttpHdr->wFlags |= HDR_ISREQUEST;
2374 slen = strlenW(value) + 1;
2375 lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0, slen*sizeof(WCHAR));
2376 if (lphttpHdr->lpszValue)
2378 strcpyW(lphttpHdr->lpszValue, value);
2383 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2386 else if (lphttpHdr->lpszValue)
2388 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
2389 bSuccess = HTTP_ReplaceHeaderValue( lphttpHdr, value );
2390 else if (dwModifier & COALESCEFLASG)
2395 INT origlen = strlenW(lphttpHdr->lpszValue);
2396 INT valuelen = strlenW(value);
2398 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
2401 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2403 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2406 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2409 len = origlen + valuelen + ((ch > 0) ? 1 : 0);
2411 lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
2414 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
2417 lphttpHdr->lpszValue[origlen] = ch;
2421 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
2422 lphttpHdr->lpszValue[len] = '\0';
2427 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
2428 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2432 TRACE("<-- %d\n",bSuccess);
2437 /***********************************************************************
2438 * HTTP_CloseConnection (internal)
2440 * Close socket connection
2443 VOID HTTP_CloseConnection(LPWININETHTTPREQW lpwhr)
2445 LPWININETHTTPSESSIONW lpwhs = NULL;
2446 LPWININETAPPINFOW hIC = NULL;
2448 TRACE("%p\n",lpwhr);
2450 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
2451 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2453 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2454 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2456 if (NETCON_connected(&lpwhr->netConnection))
2458 NETCON_close(&lpwhr->netConnection);
2461 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2462 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2466 /***********************************************************************
2467 * HTTP_CloseHTTPRequestHandle (internal)
2469 * Deallocate request handle
2472 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr)
2475 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
2479 if (NETCON_connected(&lpwhr->netConnection))
2480 HTTP_CloseConnection(lpwhr);
2482 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2483 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
2484 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2486 for (i = 0; i <= HTTP_QUERY_MAX; i++)
2488 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszField);
2489 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszValue);
2492 for (i = 0; i < lpwhr->nCustHeaders; i++)
2494 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
2495 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
2498 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
2499 HeapFree(GetProcessHeap(), 0, lpwhr);
2503 /***********************************************************************
2504 * HTTP_CloseHTTPSessionHandle (internal)
2506 * Deallocate session handle
2509 void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr)
2511 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
2513 TRACE("%p\n", lpwhs);
2515 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2516 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2517 HeapFree(GetProcessHeap(), 0, lpwhs);
2521 /***********************************************************************
2522 * HTTP_GetCustomHeaderIndex (internal)
2524 * Return index of custom header from header array
2527 INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField)
2531 TRACE("%s\n", debugstr_w(lpszField));
2533 for (index = 0; index < lpwhr->nCustHeaders; index++)
2535 if (!strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
2540 if (index >= lpwhr->nCustHeaders)
2543 TRACE("Return: %ld\n", index);
2548 /***********************************************************************
2549 * HTTP_InsertCustomHeader (internal)
2551 * Insert header into array
2554 BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
2557 LPHTTPHEADERW lph = NULL;
2560 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
2561 count = lpwhr->nCustHeaders + 1;
2563 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
2565 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
2569 lpwhr->pCustHeaders = lph;
2570 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
2571 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
2572 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
2573 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
2574 lpwhr->nCustHeaders++;
2579 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2586 /***********************************************************************
2587 * HTTP_DeleteCustomHeader (internal)
2589 * Delete header from array
2590 * If this function is called, the indexs may change.
2592 BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
2594 if( lpwhr->nCustHeaders <= 0 )
2596 if( lpwhr->nCustHeaders >= index )
2598 lpwhr->nCustHeaders--;
2600 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
2601 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
2602 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
2607 /***********************************************************************
2608 * IsHostInProxyBypassList (@)
2613 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
2615 FIXME("STUB: flags=%ld host=%s length=%ld\n",flags,szHost,length);