Store HTTP host header in the same way as most other headers.
[wine/multimedia.git] / dlls / wininet / http.c
blobb4bd5316fd52863f13daa823c3719a811ddc1355
1 /*
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
9 * Ulrich Czekalla
10 * Aric Stewart
11 * David Hammerton
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
28 #include "config.h"
29 #include "wine/port.h"
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_SOCKET_H
33 # include <sys/socket.h>
34 #endif
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
41 #include <errno.h>
42 #include <string.h>
43 #include <time.h>
44 #include <assert.h>
46 #include "windef.h"
47 #include "winbase.h"
48 #include "wininet.h"
49 #include "winreg.h"
50 #include "winerror.h"
51 #define NO_SHLWAPI_STREAM
52 #include "shlwapi.h"
54 #include "internet.h"
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 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, 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;
110 int tokens = 0;
111 int i;
112 LPCWSTR next_token;
114 /* empty string has no tokens */
115 if (*string)
116 tokens++;
117 /* count tokens */
118 for (i = 0; string[i]; i++)
119 if (!strncmpW(string+i, token_string, strlenW(token_string)))
121 DWORD j;
122 tokens++;
123 /* we want to skip over separators, but not the null terminator */
124 for (j = 0; j < strlenW(token_string) - 1; j++)
125 if (!string[i+j])
126 break;
127 i += j;
130 /* add 1 for terminating NULL */
131 token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
132 token_array[tokens] = NULL;
133 if (!tokens)
134 return token_array;
135 for (i = 0; i < tokens; i++)
137 int len;
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);
146 return token_array;
149 /***********************************************************************
150 * HTTP_FreeTokens (internal)
152 * Frees memory returned from HTTP_Tokenize.
154 static void HTTP_FreeTokens(LPWSTR * token_array)
156 int i;
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)
168 LPWSTR lpszStart;
169 LPWSTR lpszEnd;
170 LPWSTR buffer;
171 WCHAR value[MAX_FIELD_VALUE_LEN], field[MAX_FIELD_LEN];
172 BOOL bSuccess = FALSE;
173 DWORD len;
175 TRACE("copying header: %s\n", debugstr_w(lpszHeader));
177 if( dwHeaderLength == ~0UL )
178 len = strlenW(lpszHeader);
179 else
180 len = dwHeaderLength;
181 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
182 strncpyW( buffer, lpszHeader, len );
183 buffer[len]=0;
185 lpszStart = buffer;
189 lpszEnd = lpszStart;
191 while (*lpszEnd != '\0')
193 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
194 break;
195 lpszEnd++;
198 if (*lpszStart == '\0')
199 break;
201 if (*lpszEnd == '\r')
203 *lpszEnd = '\0';
204 lpszEnd += 2; /* Jump over \r\n */
206 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
207 if (HTTP_InterpretHttpHeader(lpszStart, field, MAX_FIELD_LEN, value, MAX_FIELD_VALUE_LEN))
208 bSuccess = HTTP_ProcessHeader(lpwhr, field, value, dwModifier | HTTP_ADDHDR_FLAG_REQ);
210 lpszStart = lpszEnd;
212 } while (bSuccess);
214 HeapFree(GetProcessHeap(), 0, buffer);
216 return bSuccess;
219 /***********************************************************************
220 * HttpAddRequestHeadersW (WININET.@)
222 * Adds one or more HTTP header to the request handler
224 * RETURNS
225 * TRUE on success
226 * FALSE on failure
229 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
230 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
232 BOOL bSuccess = FALSE;
233 LPWININETHTTPREQW lpwhr;
235 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_w(lpszHeader), dwHeaderLength,
236 dwModifier);
238 if (!lpszHeader)
239 return TRUE;
241 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
242 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
244 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
245 goto lend;
247 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
248 lend:
249 if( lpwhr )
250 WININET_Release( &lpwhr->hdr );
252 return bSuccess;
255 /***********************************************************************
256 * HttpAddRequestHeadersA (WININET.@)
258 * Adds one or more HTTP header to the request handler
260 * RETURNS
261 * TRUE on success
262 * FALSE on failure
265 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
266 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
268 DWORD len;
269 LPWSTR hdr;
270 BOOL r;
272 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_a(lpszHeader), dwHeaderLength,
273 dwModifier);
275 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
276 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
277 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
278 if( dwHeaderLength != ~0UL )
279 dwHeaderLength = len;
281 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
283 HeapFree( GetProcessHeap(), 0, hdr );
285 return r;
288 /***********************************************************************
289 * HttpEndRequestA (WININET.@)
291 * Ends an HTTP request that was started by HttpSendRequestEx
293 * RETURNS
294 * TRUE if successful
295 * FALSE on failure
298 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest, LPINTERNET_BUFFERSA lpBuffersOut,
299 DWORD dwFlags, DWORD dwContext)
301 FIXME("stub\n");
302 return FALSE;
305 /***********************************************************************
306 * HttpEndRequestW (WININET.@)
308 * Ends an HTTP request that was started by HttpSendRequestEx
310 * RETURNS
311 * TRUE if successful
312 * FALSE on failure
315 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest, LPINTERNET_BUFFERSW lpBuffersOut,
316 DWORD dwFlags, DWORD dwContext)
318 FIXME("stub\n");
319 return FALSE;
322 /***********************************************************************
323 * HttpOpenRequestW (WININET.@)
325 * Open a HTTP request handle
327 * RETURNS
328 * HINTERNET a HTTP request handle on success
329 * NULL on failure
332 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
333 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
334 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
335 DWORD dwFlags, DWORD dwContext)
337 LPWININETHTTPSESSIONW lpwhs;
338 HINTERNET handle = NULL;
340 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
341 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
342 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
343 dwFlags, dwContext);
344 if(lpszAcceptTypes!=NULL)
346 int i;
347 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
348 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
351 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
352 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
354 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
355 goto lend;
359 * My tests seem to show that the windows version does not
360 * become asynchronous until after this point. And anyhow
361 * if this call was asynchronous then how would you get the
362 * necessary HINTERNET pointer returned by this function.
365 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
366 lpszVersion, lpszReferrer, lpszAcceptTypes,
367 dwFlags, dwContext);
368 lend:
369 if( lpwhs )
370 WININET_Release( &lpwhs->hdr );
371 TRACE("returning %p\n", handle);
372 return handle;
376 /***********************************************************************
377 * HttpOpenRequestA (WININET.@)
379 * Open a HTTP request handle
381 * RETURNS
382 * HINTERNET a HTTP request handle on success
383 * NULL on failure
386 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
387 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
388 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
389 DWORD dwFlags, DWORD dwContext)
391 LPWSTR szVerb = NULL, szObjectName = NULL;
392 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
393 INT len;
394 INT acceptTypesCount;
395 HINTERNET rc = FALSE;
396 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
397 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
398 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
399 dwFlags, dwContext);
401 if (lpszVerb)
403 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
404 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
405 if ( !szVerb )
406 goto end;
407 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
410 if (lpszObjectName)
412 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
413 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
414 if ( !szObjectName )
415 goto end;
416 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
419 if (lpszVersion)
421 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
422 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
423 if ( !szVersion )
424 goto end;
425 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
428 if (lpszReferrer)
430 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
431 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
432 if ( !szReferrer )
433 goto end;
434 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
437 acceptTypesCount = 0;
438 if (lpszAcceptTypes)
440 /* find out how many there are */
441 while (lpszAcceptTypes[acceptTypesCount])
442 acceptTypesCount++;
443 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
444 acceptTypesCount = 0;
445 while (lpszAcceptTypes[acceptTypesCount])
447 len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
448 -1, NULL, 0 );
449 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
450 if (!szAcceptTypes[acceptTypesCount] )
451 goto end;
452 MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
453 -1, szAcceptTypes[acceptTypesCount], len );
454 acceptTypesCount++;
456 szAcceptTypes[acceptTypesCount] = NULL;
458 else szAcceptTypes = 0;
460 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
461 szVersion, szReferrer,
462 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
464 end:
465 if (szAcceptTypes)
467 acceptTypesCount = 0;
468 while (szAcceptTypes[acceptTypesCount])
470 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
471 acceptTypesCount++;
473 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
475 if (szReferrer) HeapFree(GetProcessHeap(), 0, szReferrer);
476 if (szVersion) HeapFree(GetProcessHeap(), 0, szVersion);
477 if (szObjectName) HeapFree(GetProcessHeap(), 0, szObjectName);
478 if (szVerb) HeapFree(GetProcessHeap(), 0, szVerb);
480 return rc;
483 /***********************************************************************
484 * HTTP_Base64
486 static UINT HTTP_Base64( LPCWSTR bin, LPWSTR base64 )
488 UINT n = 0, x;
489 static LPSTR HTTP_Base64Enc =
490 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
492 while( bin[0] )
494 /* first 6 bits, all from bin[0] */
495 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
496 x = (bin[0] & 3) << 4;
498 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
499 if( !bin[1] )
501 base64[n++] = HTTP_Base64Enc[x];
502 base64[n++] = '=';
503 base64[n++] = '=';
504 break;
506 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
507 x = ( bin[1] & 0x0f ) << 2;
509 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
510 if( !bin[2] )
512 base64[n++] = HTTP_Base64Enc[x];
513 base64[n++] = '=';
514 break;
516 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
518 /* last 6 bits, all from bin [2] */
519 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
520 bin += 3;
522 base64[n] = 0;
523 return n;
526 /***********************************************************************
527 * HTTP_EncodeBasicAuth
529 * Encode the basic authentication string for HTTP 1.1
531 static LPWSTR HTTP_EncodeBasicAuth( LPCWSTR username, LPCWSTR password)
533 UINT len;
534 LPWSTR in, out;
535 static const WCHAR szBasic[] = {'B','a','s','i','c',' ',0};
536 static const WCHAR szColon[] = {':',0};
538 len = lstrlenW( username ) + 1 + lstrlenW ( password ) + 1;
539 in = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
540 if( !in )
541 return NULL;
543 len = lstrlenW(szBasic) +
544 (lstrlenW( username ) + 1 + lstrlenW ( password ))*2 + 1 + 1;
545 out = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
546 if( out )
548 lstrcpyW( in, username );
549 lstrcatW( in, szColon );
550 lstrcatW( in, password );
551 lstrcpyW( out, szBasic );
552 HTTP_Base64( in, &out[strlenW(out)] );
554 HeapFree( GetProcessHeap(), 0, in );
556 return out;
559 /***********************************************************************
560 * HTTP_InsertProxyAuthorization
562 * Insert the basic authorization field in the request header
564 BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
565 LPCWSTR username, LPCWSTR password )
567 HTTPHEADERW hdr;
568 INT index;
569 static const WCHAR szProxyAuthorization[] = {
570 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
572 hdr.lpszValue = HTTP_EncodeBasicAuth( username, password );
573 hdr.lpszField = (WCHAR *)szProxyAuthorization;
574 hdr.wFlags = HDR_ISREQUEST;
575 hdr.wCount = 0;
576 if( !hdr.lpszValue )
577 return FALSE;
579 TRACE("Inserting %s = %s\n",
580 debugstr_w( hdr.lpszField ), debugstr_w( hdr.lpszValue ) );
582 /* remove the old proxy authorization header */
583 index = HTTP_GetCustomHeaderIndex( lpwhr, hdr.lpszField );
584 if( index >=0 )
585 HTTP_DeleteCustomHeader( lpwhr, index );
587 HTTP_InsertCustomHeader(lpwhr, &hdr);
588 HeapFree( GetProcessHeap(), 0, hdr.lpszValue );
590 return TRUE;
593 /***********************************************************************
594 * HTTP_DealWithProxy
596 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
597 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
599 WCHAR buf[MAXHOSTNAME];
600 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
601 WCHAR* url;
602 static const WCHAR szNul[] = { 0 };
603 URL_COMPONENTSW UrlComponents;
604 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }, szSlash[] = { '/',0 } ;
605 static const WCHAR szFormat1[] = { 'h','t','t','p',':','/','/','%','s',0 };
606 static const WCHAR szFormat2[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
607 int len;
609 memset( &UrlComponents, 0, sizeof UrlComponents );
610 UrlComponents.dwStructSize = sizeof UrlComponents;
611 UrlComponents.lpszHostName = buf;
612 UrlComponents.dwHostNameLength = MAXHOSTNAME;
614 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
615 buf,strlenW(szHttp),szHttp,strlenW(szHttp)) )
616 sprintfW(proxy, szFormat1, hIC->lpszProxy);
617 else
618 strcpyW(proxy,buf);
619 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
620 return FALSE;
621 if( UrlComponents.dwHostNameLength == 0 )
622 return FALSE;
624 if( !lpwhr->lpszPath )
625 lpwhr->lpszPath = (LPWSTR)szNul;
626 TRACE("server='%s' path='%s'\n",
627 debugstr_w(lpwhs->lpszServerName), debugstr_w(lpwhr->lpszPath));
628 /* for constant 15 see above */
629 len = strlenW(lpwhs->lpszServerName) + strlenW(lpwhr->lpszPath) + 15;
630 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
632 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
633 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
635 sprintfW(url, szFormat2, lpwhs->lpszServerName, lpwhs->nServerPort);
637 if( lpwhr->lpszPath[0] != '/' )
638 strcatW( url, szSlash );
639 strcatW(url, lpwhr->lpszPath);
640 if(lpwhr->lpszPath != szNul)
641 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
642 lpwhr->lpszPath = url;
643 /* FIXME: Do I have to free lpwhs->lpszServerName here ? */
644 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
645 lpwhs->nServerPort = UrlComponents.nPort;
647 return TRUE;
650 /***********************************************************************
651 * HTTP_HttpOpenRequestW (internal)
653 * Open a HTTP request handle
655 * RETURNS
656 * HINTERNET a HTTP request handle on success
657 * NULL on failure
660 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
661 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
662 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
663 DWORD dwFlags, DWORD dwContext)
665 LPWININETAPPINFOW hIC = NULL;
666 LPWININETHTTPREQW lpwhr;
667 LPWSTR lpszCookies;
668 LPWSTR lpszUrl = NULL;
669 DWORD nCookieSize;
670 HINTERNET handle = NULL;
671 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
672 DWORD len;
674 TRACE("--> \n");
676 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
677 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
679 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
680 if (NULL == lpwhr)
682 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
683 goto lend;
685 lpwhr->hdr.htype = WH_HHTTPREQ;
686 lpwhr->hdr.lpwhparent = WININET_AddRef( &lpwhs->hdr );
687 lpwhr->hdr.dwFlags = dwFlags;
688 lpwhr->hdr.dwContext = dwContext;
689 lpwhr->hdr.dwRefCount = 1;
690 lpwhr->hdr.destroy = HTTP_CloseHTTPRequestHandle;
692 handle = WININET_AllocHandle( &lpwhr->hdr );
693 if (NULL == handle)
695 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
696 goto lend;
699 NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE);
701 if (NULL != lpszObjectName && strlenW(lpszObjectName)) {
702 HRESULT rc;
704 len = 0;
705 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
706 if (rc != E_POINTER)
707 len = strlenW(lpszObjectName)+1;
708 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
709 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
710 URL_ESCAPE_SPACES_ONLY);
711 if (rc)
713 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(lpszObjectName),rc);
714 strcpyW(lpwhr->lpszPath,lpszObjectName);
718 if (NULL != lpszReferrer && strlenW(lpszReferrer))
719 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDHDR_FLAG_COALESCE);
721 if(lpszAcceptTypes!=NULL)
723 int i;
724 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
725 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i], HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_REQ|HTTP_ADDHDR_FLAG_ADD_IF_NEW);
728 if (NULL == lpszVerb)
730 static const WCHAR szGet[] = {'G','E','T',0};
731 lpwhr->lpszVerb = WININET_strdupW(szGet);
733 else if (strlenW(lpszVerb))
734 lpwhr->lpszVerb = WININET_strdupW(lpszVerb);
736 if (NULL != lpszReferrer && strlenW(lpszReferrer))
738 WCHAR buf[MAXHOSTNAME];
739 URL_COMPONENTSW UrlComponents;
741 memset( &UrlComponents, 0, sizeof UrlComponents );
742 UrlComponents.dwStructSize = sizeof UrlComponents;
743 UrlComponents.lpszHostName = buf;
744 UrlComponents.dwHostNameLength = MAXHOSTNAME;
746 InternetCrackUrlW(lpszReferrer, 0, 0, &UrlComponents);
747 if (strlenW(UrlComponents.lpszHostName))
748 HTTP_ProcessHeader(lpwhr, g_szHost, UrlComponents.lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
750 else
751 HTTP_ProcessHeader(lpwhr, g_szHost, lpwhs->lpszServerName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
753 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
754 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
756 if (hIC->lpszAgent)
758 WCHAR *agent_header;
759 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
761 len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
762 agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
763 sprintfW(agent_header, user_agent, hIC->lpszAgent );
765 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
766 HTTP_ADDREQ_FLAG_ADD);
767 HeapFree(GetProcessHeap(), 0, agent_header);
770 len = strlenW(lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue) + strlenW(szUrlForm);
771 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
772 sprintfW( lpszUrl, szUrlForm, lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue );
774 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
775 InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
777 int cnt = 0;
778 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
779 static const WCHAR szcrlf[] = {'\r','\n',0};
781 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
783 cnt += sprintfW(lpszCookies, szCookie);
784 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
785 strcatW(lpszCookies, szcrlf);
787 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
788 HTTP_ADDREQ_FLAG_ADD);
789 HeapFree(GetProcessHeap(), 0, lpszCookies);
791 HeapFree(GetProcessHeap(), 0, lpszUrl);
795 if (hIC->lpfnStatusCB)
797 INTERNET_ASYNC_RESULT iar;
799 iar.dwResult = (DWORD)handle;
800 iar.dwError = ERROR_SUCCESS;
802 SendAsyncCallback(hIC, &lpwhs->hdr, dwContext,
803 INTERNET_STATUS_HANDLE_CREATED, &iar,
804 sizeof(INTERNET_ASYNC_RESULT));
808 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
812 * According to my tests. The name is not resolved until a request is Opened
814 SendAsyncCallback(hIC, &lpwhs->hdr, dwContext,
815 INTERNET_STATUS_RESOLVING_NAME,
816 lpwhs->lpszServerName,
817 strlenW(lpwhs->lpszServerName)+1);
818 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
819 &lpwhs->phostent, &lpwhs->socketAddress))
821 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
822 InternetCloseHandle( handle );
823 handle = NULL;
824 goto lend;
827 SendAsyncCallback(hIC, &lpwhs->hdr, lpwhr->hdr.dwContext,
828 INTERNET_STATUS_NAME_RESOLVED,
829 &(lpwhs->socketAddress),
830 sizeof(struct sockaddr_in));
832 lend:
833 if( lpwhr )
834 WININET_Release( &lpwhr->hdr );
836 TRACE("<-- %p (%p)\n", handle, lpwhr);
837 return handle;
840 /***********************************************************************
841 * HTTP_HttpQueryInfoW (internal)
843 BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
844 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
846 LPHTTPHEADERW lphttpHdr = NULL;
847 BOOL bSuccess = FALSE;
849 /* Find requested header structure */
850 if ((dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK) == HTTP_QUERY_CUSTOM)
852 INT index = HTTP_GetCustomHeaderIndex(lpwhr, (LPWSTR)lpBuffer);
854 if (index < 0)
855 return bSuccess;
857 lphttpHdr = &lpwhr->pCustHeaders[index];
859 else
861 INT index = dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK;
863 if (index == HTTP_QUERY_RAW_HEADERS_CRLF)
865 DWORD len = strlenW(lpwhr->lpszRawHeaders);
866 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
868 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
869 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
870 return FALSE;
872 memcpy(lpBuffer, lpwhr->lpszRawHeaders, (len+1)*sizeof(WCHAR));
873 *lpdwBufferLength = len * sizeof(WCHAR);
875 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
877 return TRUE;
879 else if (index == HTTP_QUERY_RAW_HEADERS)
881 static const WCHAR szCrLf[] = {'\r','\n',0};
882 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
883 DWORD i, size = 0;
884 LPWSTR pszString = (WCHAR*)lpBuffer;
886 for (i = 0; ppszRawHeaderLines[i]; i++)
887 size += strlenW(ppszRawHeaderLines[i]) + 1;
889 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
891 HTTP_FreeTokens(ppszRawHeaderLines);
892 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
893 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
894 return FALSE;
897 for (i = 0; ppszRawHeaderLines[i]; i++)
899 DWORD len = strlenW(ppszRawHeaderLines[i]);
900 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
901 pszString += len+1;
903 *pszString = '\0';
905 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
907 *lpdwBufferLength = size * sizeof(WCHAR);
908 HTTP_FreeTokens(ppszRawHeaderLines);
910 return TRUE;
912 else if (index >= 0 && index <= HTTP_QUERY_MAX && lpwhr->StdHeaders[index].lpszValue)
914 lphttpHdr = &lpwhr->StdHeaders[index];
916 else
918 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
919 return bSuccess;
923 /* Ensure header satisifies requested attributes */
924 if ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
925 (~lphttpHdr->wFlags & HDR_ISREQUEST))
927 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
928 return bSuccess;
931 /* coalesce value to reuqested type */
932 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
934 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
935 bSuccess = TRUE;
937 TRACE(" returning number : %d\n", *(int *)lpBuffer);
939 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
941 time_t tmpTime;
942 struct tm tmpTM;
943 SYSTEMTIME *STHook;
945 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
947 tmpTM = *gmtime(&tmpTime);
948 STHook = (SYSTEMTIME *) lpBuffer;
949 if(STHook==NULL)
950 return bSuccess;
952 STHook->wDay = tmpTM.tm_mday;
953 STHook->wHour = tmpTM.tm_hour;
954 STHook->wMilliseconds = 0;
955 STHook->wMinute = tmpTM.tm_min;
956 STHook->wDayOfWeek = tmpTM.tm_wday;
957 STHook->wMonth = tmpTM.tm_mon + 1;
958 STHook->wSecond = tmpTM.tm_sec;
959 STHook->wYear = tmpTM.tm_year;
961 bSuccess = TRUE;
963 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
964 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
965 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
967 else if (dwInfoLevel & HTTP_QUERY_FLAG_COALESCE)
969 if (*lpdwIndex >= lphttpHdr->wCount)
971 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
973 else
975 /* Copy strncpyW(lpBuffer, lphttpHdr[*lpdwIndex], len); */
976 (*lpdwIndex)++;
979 else
981 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
983 if (len > *lpdwBufferLength)
985 *lpdwBufferLength = len;
986 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
987 return bSuccess;
990 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
991 *lpdwBufferLength = len - sizeof(WCHAR);
992 bSuccess = TRUE;
994 TRACE(" returning string : '%s'\n", debugstr_w(lpBuffer));
996 return bSuccess;
999 /***********************************************************************
1000 * HttpQueryInfoW (WININET.@)
1002 * Queries for information about an HTTP request
1004 * RETURNS
1005 * TRUE on success
1006 * FALSE on failure
1009 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1010 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1012 BOOL bSuccess = FALSE;
1013 LPWININETHTTPREQW lpwhr;
1015 if (TRACE_ON(wininet)) {
1016 #define FE(x) { x, #x }
1017 static const wininet_flag_info query_flags[] = {
1018 FE(HTTP_QUERY_MIME_VERSION),
1019 FE(HTTP_QUERY_CONTENT_TYPE),
1020 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
1021 FE(HTTP_QUERY_CONTENT_ID),
1022 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
1023 FE(HTTP_QUERY_CONTENT_LENGTH),
1024 FE(HTTP_QUERY_CONTENT_LANGUAGE),
1025 FE(HTTP_QUERY_ALLOW),
1026 FE(HTTP_QUERY_PUBLIC),
1027 FE(HTTP_QUERY_DATE),
1028 FE(HTTP_QUERY_EXPIRES),
1029 FE(HTTP_QUERY_LAST_MODIFIED),
1030 FE(HTTP_QUERY_MESSAGE_ID),
1031 FE(HTTP_QUERY_URI),
1032 FE(HTTP_QUERY_DERIVED_FROM),
1033 FE(HTTP_QUERY_COST),
1034 FE(HTTP_QUERY_LINK),
1035 FE(HTTP_QUERY_PRAGMA),
1036 FE(HTTP_QUERY_VERSION),
1037 FE(HTTP_QUERY_STATUS_CODE),
1038 FE(HTTP_QUERY_STATUS_TEXT),
1039 FE(HTTP_QUERY_RAW_HEADERS),
1040 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
1041 FE(HTTP_QUERY_CONNECTION),
1042 FE(HTTP_QUERY_ACCEPT),
1043 FE(HTTP_QUERY_ACCEPT_CHARSET),
1044 FE(HTTP_QUERY_ACCEPT_ENCODING),
1045 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
1046 FE(HTTP_QUERY_AUTHORIZATION),
1047 FE(HTTP_QUERY_CONTENT_ENCODING),
1048 FE(HTTP_QUERY_FORWARDED),
1049 FE(HTTP_QUERY_FROM),
1050 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
1051 FE(HTTP_QUERY_LOCATION),
1052 FE(HTTP_QUERY_ORIG_URI),
1053 FE(HTTP_QUERY_REFERER),
1054 FE(HTTP_QUERY_RETRY_AFTER),
1055 FE(HTTP_QUERY_SERVER),
1056 FE(HTTP_QUERY_TITLE),
1057 FE(HTTP_QUERY_USER_AGENT),
1058 FE(HTTP_QUERY_WWW_AUTHENTICATE),
1059 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
1060 FE(HTTP_QUERY_ACCEPT_RANGES),
1061 FE(HTTP_QUERY_SET_COOKIE),
1062 FE(HTTP_QUERY_COOKIE),
1063 FE(HTTP_QUERY_REQUEST_METHOD),
1064 FE(HTTP_QUERY_REFRESH),
1065 FE(HTTP_QUERY_CONTENT_DISPOSITION),
1066 FE(HTTP_QUERY_AGE),
1067 FE(HTTP_QUERY_CACHE_CONTROL),
1068 FE(HTTP_QUERY_CONTENT_BASE),
1069 FE(HTTP_QUERY_CONTENT_LOCATION),
1070 FE(HTTP_QUERY_CONTENT_MD5),
1071 FE(HTTP_QUERY_CONTENT_RANGE),
1072 FE(HTTP_QUERY_ETAG),
1073 FE(HTTP_QUERY_HOST),
1074 FE(HTTP_QUERY_IF_MATCH),
1075 FE(HTTP_QUERY_IF_NONE_MATCH),
1076 FE(HTTP_QUERY_IF_RANGE),
1077 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
1078 FE(HTTP_QUERY_MAX_FORWARDS),
1079 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
1080 FE(HTTP_QUERY_RANGE),
1081 FE(HTTP_QUERY_TRANSFER_ENCODING),
1082 FE(HTTP_QUERY_UPGRADE),
1083 FE(HTTP_QUERY_VARY),
1084 FE(HTTP_QUERY_VIA),
1085 FE(HTTP_QUERY_WARNING),
1086 FE(HTTP_QUERY_CUSTOM)
1088 static const wininet_flag_info modifier_flags[] = {
1089 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
1090 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
1091 FE(HTTP_QUERY_FLAG_NUMBER),
1092 FE(HTTP_QUERY_FLAG_COALESCE)
1094 #undef FE
1095 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
1096 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
1097 DWORD i;
1099 TRACE("(%p, 0x%08lx)--> %ld\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
1100 TRACE(" Attribute:");
1101 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
1102 if (query_flags[i].val == info) {
1103 DPRINTF(" %s", query_flags[i].name);
1104 break;
1107 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
1108 DPRINTF(" Unknown (%08lx)", info);
1111 DPRINTF(" Modifier:");
1112 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
1113 if (modifier_flags[i].val & info_mod) {
1114 DPRINTF(" %s", modifier_flags[i].name);
1115 info_mod &= ~ modifier_flags[i].val;
1119 if (info_mod) {
1120 DPRINTF(" Unknown (%08lx)", info_mod);
1122 DPRINTF("\n");
1125 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1126 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1128 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1129 goto lend;
1132 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
1133 lpBuffer, lpdwBufferLength, lpdwIndex);
1135 lend:
1136 if( lpwhr )
1137 WININET_Release( &lpwhr->hdr );
1139 TRACE("%d <--\n", bSuccess);
1140 return bSuccess;
1143 /***********************************************************************
1144 * HttpQueryInfoA (WININET.@)
1146 * Queries for information about an HTTP request
1148 * RETURNS
1149 * TRUE on success
1150 * FALSE on failure
1153 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1154 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1156 BOOL result;
1157 DWORD len;
1158 WCHAR* bufferW;
1160 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
1161 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
1163 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
1164 lpdwBufferLength, lpdwIndex );
1167 len = (*lpdwBufferLength)*sizeof(WCHAR);
1168 bufferW = HeapAlloc( GetProcessHeap(), 0, len );
1169 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
1170 &len, lpdwIndex );
1171 if( result )
1173 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
1174 lpBuffer, *lpdwBufferLength, NULL, NULL );
1175 *lpdwBufferLength = len - 1;
1177 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
1179 else
1180 /* since the strings being returned from HttpQueryInfoW should be
1181 * only ASCII characters, it is reasonable to assume that all of
1182 * the Unicode characters can be reduced to a single byte */
1183 *lpdwBufferLength = len / sizeof(WCHAR);
1185 HeapFree(GetProcessHeap(), 0, bufferW );
1187 return result;
1190 /***********************************************************************
1191 * HttpSendRequestExA (WININET.@)
1193 * Sends the specified request to the HTTP server and allows chunked
1194 * transfers
1196 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
1197 LPINTERNET_BUFFERSA lpBuffersIn,
1198 LPINTERNET_BUFFERSA lpBuffersOut,
1199 DWORD dwFlags, DWORD dwContext)
1201 FIXME("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersIn,
1202 lpBuffersOut, dwFlags, dwContext);
1203 return FALSE;
1206 /***********************************************************************
1207 * HttpSendRequestW (WININET.@)
1209 * Sends the specified request to the HTTP server
1211 * RETURNS
1212 * TRUE on success
1213 * FALSE on failure
1216 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
1217 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1219 LPWININETHTTPREQW lpwhr;
1220 LPWININETHTTPSESSIONW lpwhs = NULL;
1221 LPWININETAPPINFOW hIC = NULL;
1222 BOOL r;
1224 TRACE("%p, %p (%s), %li, %p, %li)\n", hHttpRequest,
1225 lpszHeaders, debugstr_w(lpszHeaders), dwHeaderLength, lpOptional, dwOptionalLength);
1227 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1228 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1230 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1231 r = FALSE;
1232 goto lend;
1235 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1236 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1238 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1239 r = FALSE;
1240 goto lend;
1243 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1244 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1246 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1247 r = FALSE;
1248 goto lend;
1251 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1253 WORKREQUEST workRequest;
1254 struct WORKREQ_HTTPSENDREQUESTW *req;
1256 workRequest.asyncall = HTTPSENDREQUESTW;
1257 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
1258 req = &workRequest.u.HttpSendRequestW;
1259 if (lpszHeaders)
1260 req->lpszHeader = WININET_strdupW(lpszHeaders);
1261 else
1262 req->lpszHeader = 0;
1263 req->dwHeaderLength = dwHeaderLength;
1264 req->lpOptional = lpOptional;
1265 req->dwOptionalLength = dwOptionalLength;
1267 INTERNET_AsyncCall(&workRequest);
1269 * This is from windows.
1271 SetLastError(ERROR_IO_PENDING);
1272 r = FALSE;
1274 else
1276 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
1277 dwHeaderLength, lpOptional, dwOptionalLength);
1279 lend:
1280 if( lpwhr )
1281 WININET_Release( &lpwhr->hdr );
1282 return r;
1285 /***********************************************************************
1286 * HttpSendRequestA (WININET.@)
1288 * Sends the specified request to the HTTP server
1290 * RETURNS
1291 * TRUE on success
1292 * FALSE on failure
1295 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
1296 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1298 BOOL result;
1299 LPWSTR szHeaders=NULL;
1300 DWORD nLen=dwHeaderLength;
1301 if(lpszHeaders!=NULL)
1303 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
1304 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
1305 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
1307 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
1308 if(szHeaders!=NULL)
1309 HeapFree(GetProcessHeap(),0,szHeaders);
1310 return result;
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;
1321 WCHAR path[2048];
1323 if(lpszUrl[0]=='/')
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);
1333 else
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))
1352 return FALSE;
1354 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1355 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1357 #if 0
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);
1372 #endif
1374 if (NULL != lpwhs->lpszServerName)
1375 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1376 lpwhs->lpszServerName = WININET_strdupW(hostName);
1377 if (NULL != lpwhs->lpszUserName)
1378 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
1379 lpwhs->lpszUserName = WININET_strdupW(userName);
1380 lpwhs->nServerPort = urlComponents.nPort;
1382 HTTP_ProcessHeader(lpwhr, g_szHost, hostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
1384 SendAsyncCallback(hIC, &lpwhs->hdr, lpwhr->hdr.dwContext,
1385 INTERNET_STATUS_RESOLVING_NAME,
1386 lpwhs->lpszServerName,
1387 strlenW(lpwhs->lpszServerName)+1);
1389 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1390 &lpwhs->phostent, &lpwhs->socketAddress))
1392 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1393 return FALSE;
1396 SendAsyncCallback(hIC, &lpwhs->hdr, lpwhr->hdr.dwContext,
1397 INTERNET_STATUS_NAME_RESOLVED,
1398 &(lpwhs->socketAddress),
1399 sizeof(struct sockaddr_in));
1403 if(lpwhr->lpszPath)
1404 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1405 lpwhr->lpszPath=NULL;
1406 if (strlenW(path))
1408 DWORD needed = 0;
1409 HRESULT rc;
1411 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
1412 if (rc != E_POINTER)
1413 needed = strlenW(path)+1;
1414 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
1415 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
1416 URL_ESCAPE_SPACES_ONLY);
1417 if (rc)
1419 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(path),rc);
1420 strcpyW(lpwhr->lpszPath,path);
1424 return HTTP_HttpSendRequestW(lpwhr, lpszHeaders, dwHeaderLength, lpOptional, dwOptionalLength);
1427 /***********************************************************************
1428 * HTTP_build_req (internal)
1430 * concatenate all the strings in the request together
1432 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
1434 LPCWSTR *t;
1435 LPWSTR str;
1437 for( t = list; *t ; t++ )
1438 len += strlenW( *t );
1439 len++;
1441 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1442 *str = 0;
1444 for( t = list; *t ; t++ )
1445 strcatW( str, *t );
1447 return str;
1450 /***********************************************************************
1451 * HTTP_HttpSendRequestW (internal)
1453 * Sends the specified request to the HTTP server
1455 * RETURNS
1456 * TRUE on success
1457 * FALSE on failure
1460 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
1461 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1463 INT cnt;
1464 DWORD i;
1465 BOOL bSuccess = FALSE;
1466 LPWSTR requestString = NULL;
1467 INT responseLen;
1468 LPWININETHTTPSESSIONW lpwhs = NULL;
1469 LPWININETAPPINFOW hIC = NULL;
1470 BOOL loop_next = FALSE;
1471 int CustHeaderIndex;
1473 TRACE("--> %p\n", lpwhr);
1475 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
1477 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1478 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1480 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1481 return FALSE;
1484 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1485 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1487 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1488 return FALSE;
1491 /* Clear any error information */
1492 INTERNET_SetLastError(0);
1495 /* We must have a verb */
1496 if (NULL == lpwhr->lpszVerb)
1498 goto lend;
1501 /* if we are using optional stuff, we must add the fixed header of that option length */
1502 if (lpOptional && dwOptionalLength)
1504 static const WCHAR szContentLength[] = {
1505 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0};
1506 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \n\r */ + 20 /* int */ ];
1507 sprintfW(contentLengthStr, szContentLength, dwOptionalLength);
1508 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD);
1513 static const WCHAR szSlash[] = { '/',0 };
1514 static const WCHAR szSpace[] = { ' ',0 };
1515 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
1516 static const WCHAR szcrlf[] = {'\r','\n', 0};
1517 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
1518 static const WCHAR szSetCookie[] = {'S','e','t','-','C','o','o','k','i','e',0 };
1519 static const WCHAR szColon[] = { ':',' ',0 };
1520 LPCWSTR *req;
1521 LPWSTR p;
1522 DWORD len, n;
1523 char *ascii_req;
1525 TRACE("Going to url %s %s\n", debugstr_w(lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue), debugstr_w(lpwhr->lpszPath));
1526 loop_next = FALSE;
1528 /* If we don't have a path we set it to root */
1529 if (NULL == lpwhr->lpszPath)
1530 lpwhr->lpszPath = WININET_strdupW(szSlash);
1531 else /* remove \r and \n*/
1533 int nLen = strlenW(lpwhr->lpszPath);
1534 while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
1536 nLen--;
1537 lpwhr->lpszPath[nLen]='\0';
1541 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1542 lpwhr->lpszPath, strlenW(szHttp), szHttp, strlenW(szHttp) )
1543 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
1545 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
1546 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
1547 *fixurl = '/';
1548 strcpyW(fixurl + 1, lpwhr->lpszPath);
1549 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
1550 lpwhr->lpszPath = fixurl;
1553 /* add the headers the caller supplied */
1554 if( lpszHeaders && dwHeaderLength )
1556 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
1557 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
1560 /* allocate space for an array of all the string pointers to be added */
1561 len = (HTTP_QUERY_MAX + lpwhr->nCustHeaders)*4 + 9;
1562 req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
1564 /* add the verb, path and HTTP/1.0 */
1565 n = 0;
1566 req[n++] = lpwhr->lpszVerb;
1567 req[n++] = szSpace;
1568 req[n++] = lpwhr->lpszPath;
1569 req[n++] = HTTPHEADER;
1571 /* Append standard request headers */
1572 for (i = 0; i <= HTTP_QUERY_MAX; i++)
1574 if (lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST)
1576 req[n++] = szcrlf;
1577 req[n++] = lpwhr->StdHeaders[i].lpszField;
1578 req[n++] = szColon;
1579 req[n++] = lpwhr->StdHeaders[i].lpszValue;
1581 TRACE("Adding header %s (%s)\n",
1582 debugstr_w(lpwhr->StdHeaders[i].lpszField),
1583 debugstr_w(lpwhr->StdHeaders[i].lpszValue));
1587 /* Append custom request heades */
1588 for (i = 0; i < lpwhr->nCustHeaders; i++)
1590 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
1592 req[n++] = szcrlf;
1593 req[n++] = lpwhr->pCustHeaders[i].lpszField;
1594 req[n++] = szColon;
1595 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
1597 TRACE("Adding custom header %s (%s)\n",
1598 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
1599 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
1603 if( n >= len )
1604 ERR("oops. buffer overrun\n");
1606 req[n] = NULL;
1607 requestString = HTTP_build_req( req, 4 );
1608 HeapFree( GetProcessHeap(), 0, req );
1611 * Set (header) termination string for request
1612 * Make sure there's exactly two new lines at the end of the request
1614 p = &requestString[strlenW(requestString)-1];
1615 while ( (*p == '\n') || (*p == '\r') )
1616 p--;
1617 strcpyW( p+1, sztwocrlf );
1619 TRACE("Request header -> %s\n", debugstr_w(requestString) );
1621 /* Send the request and store the results */
1622 if (!HTTP_OpenConnection(lpwhr))
1623 goto lend;
1625 /* send the request as ASCII, tack on the optional data */
1626 if( !lpOptional )
1627 dwOptionalLength = 0;
1628 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
1629 NULL, 0, NULL, NULL );
1630 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
1631 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
1632 ascii_req, len, NULL, NULL );
1633 if( lpOptional )
1634 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
1635 len = (len + dwOptionalLength - 1);
1636 ascii_req[len] = 0;
1637 TRACE("full request -> %s\n", ascii_req );
1639 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1640 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
1642 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
1643 HeapFree( GetProcessHeap(), 0, ascii_req );
1645 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1646 INTERNET_STATUS_REQUEST_SENT,
1647 &len,sizeof(DWORD));
1649 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1650 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
1652 if (cnt < 0)
1653 goto lend;
1655 responseLen = HTTP_GetResponseHeaders(lpwhr);
1656 if (responseLen)
1657 bSuccess = TRUE;
1659 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1660 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
1661 sizeof(DWORD));
1663 /* process headers here. Is this right? */
1664 CustHeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSetCookie);
1665 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && (CustHeaderIndex >= 0))
1667 LPHTTPHEADERW setCookieHeader;
1668 int nPosStart = 0, nPosEnd = 0, len;
1669 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
1671 setCookieHeader = &lpwhr->pCustHeaders[CustHeaderIndex];
1673 while (setCookieHeader->lpszValue[nPosEnd] != '\0')
1675 LPWSTR buf_cookie, cookie_name, cookie_data;
1676 LPWSTR buf_url;
1677 LPWSTR domain = NULL;
1678 int nEqualPos = 0;
1679 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
1680 setCookieHeader->lpszValue[nPosEnd] != '\0')
1682 nPosEnd++;
1684 if (setCookieHeader->lpszValue[nPosEnd] == ';')
1686 /* fixme: not case sensitive, strcasestr is gnu only */
1687 int nDomainPosEnd = 0;
1688 int nDomainPosStart = 0, nDomainLength = 0;
1689 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
1690 LPWSTR lpszDomain = strstrW(&setCookieHeader->lpszValue[nPosEnd], szDomain);
1691 if (lpszDomain)
1692 { /* they have specified their own domain, lets use it */
1693 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
1694 lpszDomain[nDomainPosEnd] != '\0')
1696 nDomainPosEnd++;
1698 nDomainPosStart = strlenW(szDomain);
1699 nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
1700 domain = HeapAlloc(GetProcessHeap(), 0, (nDomainLength + 1)*sizeof(WCHAR));
1701 strncpyW(domain, &lpszDomain[nDomainPosStart], nDomainLength);
1702 domain[nDomainLength] = '\0';
1705 if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
1706 buf_cookie = HeapAlloc(GetProcessHeap(), 0, ((nPosEnd - nPosStart) + 1)*sizeof(WCHAR));
1707 strncpyW(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart));
1708 buf_cookie[(nPosEnd - nPosStart)] = '\0';
1709 TRACE("%s\n", debugstr_w(buf_cookie));
1710 while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
1712 nEqualPos++;
1714 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
1716 HeapFree(GetProcessHeap(), 0, buf_cookie);
1717 break;
1720 cookie_name = HeapAlloc(GetProcessHeap(), 0, (nEqualPos + 1)*sizeof(WCHAR));
1721 strncpyW(cookie_name, buf_cookie, nEqualPos);
1722 cookie_name[nEqualPos] = '\0';
1723 cookie_data = &buf_cookie[nEqualPos + 1];
1726 len = strlenW((domain ? domain : lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue)) +
1727 strlenW(lpwhr->lpszPath) + 9;
1728 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1729 sprintfW(buf_url, szFmt, (domain ? domain : lpwhr->StdHeaders[HTTP_QUERY_HOST].lpszValue)); /* FIXME PATH!!! */
1730 InternetSetCookieW(buf_url, cookie_name, cookie_data);
1732 HeapFree(GetProcessHeap(), 0, buf_url);
1733 HeapFree(GetProcessHeap(), 0, buf_cookie);
1734 HeapFree(GetProcessHeap(), 0, cookie_name);
1735 if (domain) HeapFree(GetProcessHeap(), 0, domain);
1736 nPosStart = nPosEnd;
1740 while (loop_next);
1742 lend:
1744 if (requestString)
1745 HeapFree(GetProcessHeap(), 0, requestString);
1747 /* TODO: send notification for P3P header */
1749 if(!(hIC->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
1751 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
1752 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
1753 (dwCode==302 || dwCode==301))
1755 WCHAR szNewLocation[2048];
1756 DWORD dwBufferSize=2048;
1757 dwIndex=0;
1758 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
1760 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1761 INTERNET_STATUS_REDIRECT, szNewLocation,
1762 dwBufferSize);
1763 return HTTP_HandleRedirect(lpwhr, szNewLocation, lpszHeaders,
1764 dwHeaderLength, lpOptional, dwOptionalLength);
1769 if (hIC->lpfnStatusCB)
1771 INTERNET_ASYNC_RESULT iar;
1773 iar.dwResult = (DWORD)bSuccess;
1774 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1776 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1777 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1778 sizeof(INTERNET_ASYNC_RESULT));
1781 TRACE("<--\n");
1782 return bSuccess;
1786 /***********************************************************************
1787 * HTTP_Connect (internal)
1789 * Create http session handle
1791 * RETURNS
1792 * HINTERNET a session handle on success
1793 * NULL on failure
1796 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
1797 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
1798 LPCWSTR lpszPassword, DWORD dwFlags, DWORD dwContext,
1799 DWORD dwInternalFlags)
1801 BOOL bSuccess = FALSE;
1802 LPWININETHTTPSESSIONW lpwhs = NULL;
1803 HINTERNET handle = NULL;
1805 TRACE("-->\n");
1807 assert( hIC->hdr.htype == WH_HINIT );
1809 hIC->hdr.dwContext = dwContext;
1811 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
1812 if (NULL == lpwhs)
1814 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1815 goto lerror;
1819 * According to my tests. The name is not resolved until a request is sent
1822 if (nServerPort == INTERNET_INVALID_PORT_NUMBER)
1823 nServerPort = INTERNET_DEFAULT_HTTP_PORT;
1825 lpwhs->hdr.htype = WH_HHTTPSESSION;
1826 lpwhs->hdr.lpwhparent = WININET_AddRef( &hIC->hdr );
1827 lpwhs->hdr.dwFlags = dwFlags;
1828 lpwhs->hdr.dwContext = dwContext;
1829 lpwhs->hdr.dwInternalFlags = dwInternalFlags;
1830 lpwhs->hdr.dwRefCount = 1;
1831 lpwhs->hdr.destroy = HTTP_CloseHTTPSessionHandle;
1833 handle = WININET_AllocHandle( &lpwhs->hdr );
1834 if (NULL == handle)
1836 ERR("Failed to alloc handle\n");
1837 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1838 goto lerror;
1841 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
1842 if(strchrW(hIC->lpszProxy, ' '))
1843 FIXME("Several proxies not implemented.\n");
1844 if(hIC->lpszProxyBypass)
1845 FIXME("Proxy bypass is ignored.\n");
1847 if (NULL != lpszServerName)
1848 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
1849 if (NULL != lpszUserName)
1850 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
1851 lpwhs->nServerPort = nServerPort;
1853 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
1854 if (hIC->lpfnStatusCB && !(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
1856 INTERNET_ASYNC_RESULT iar;
1858 iar.dwResult = (DWORD)handle;
1859 iar.dwError = ERROR_SUCCESS;
1861 SendAsyncCallback(hIC, &hIC->hdr, dwContext,
1862 INTERNET_STATUS_HANDLE_CREATED, &iar,
1863 sizeof(INTERNET_ASYNC_RESULT));
1866 bSuccess = TRUE;
1868 lerror:
1869 if( lpwhs )
1870 WININET_Release( &lpwhs->hdr );
1873 * a INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
1874 * windows
1877 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
1878 return handle;
1882 /***********************************************************************
1883 * HTTP_OpenConnection (internal)
1885 * Connect to a web server
1887 * RETURNS
1889 * TRUE on success
1890 * FALSE on failure
1892 BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
1894 BOOL bSuccess = FALSE;
1895 LPWININETHTTPSESSIONW lpwhs;
1896 LPWININETAPPINFOW hIC = NULL;
1898 TRACE("-->\n");
1901 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1903 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1904 goto lend;
1907 lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
1909 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1910 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1911 INTERNET_STATUS_CONNECTING_TO_SERVER,
1912 &(lpwhs->socketAddress),
1913 sizeof(struct sockaddr_in));
1915 if (!NETCON_create(&lpwhr->netConnection, lpwhs->phostent->h_addrtype,
1916 SOCK_STREAM, 0))
1918 WARN("Socket creation failed\n");
1919 goto lend;
1922 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
1923 sizeof(lpwhs->socketAddress)))
1925 WARN("Unable to connect to host (%s)\n", strerror(errno));
1926 goto lend;
1929 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1930 INTERNET_STATUS_CONNECTED_TO_SERVER,
1931 &(lpwhs->socketAddress),
1932 sizeof(struct sockaddr_in));
1934 bSuccess = TRUE;
1936 lend:
1937 TRACE("%d <--\n", bSuccess);
1938 return bSuccess;
1942 /***********************************************************************
1943 * HTTP_clear_response_headers (internal)
1945 * clear out any old response headers
1947 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
1949 DWORD i;
1951 for( i=0; i<=HTTP_QUERY_MAX; i++ )
1953 if( !lpwhr->StdHeaders[i].lpszField )
1954 continue;
1955 if( !lpwhr->StdHeaders[i].lpszValue )
1956 continue;
1957 if ( lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST )
1958 continue;
1959 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[i], NULL );
1961 for( i=0; i<lpwhr->nCustHeaders; i++)
1963 if( !lpwhr->pCustHeaders[i].lpszField )
1964 continue;
1965 if( !lpwhr->pCustHeaders[i].lpszValue )
1966 continue;
1967 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
1968 continue;
1969 HTTP_ReplaceHeaderValue( &lpwhr->pCustHeaders[i], NULL );
1973 /***********************************************************************
1974 * HTTP_GetResponseHeaders (internal)
1976 * Read server response
1978 * RETURNS
1980 * TRUE on success
1981 * FALSE on error
1983 BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr)
1985 INT cbreaks = 0;
1986 WCHAR buffer[MAX_REPLY_LEN];
1987 DWORD buflen = MAX_REPLY_LEN;
1988 BOOL bSuccess = FALSE;
1989 INT rc = 0;
1990 WCHAR value[MAX_FIELD_VALUE_LEN], field[MAX_FIELD_LEN];
1991 static const WCHAR szCrLf[] = {'\r','\n',0};
1992 char bufferA[MAX_REPLY_LEN];
1993 LPWSTR status_code, status_text;
1994 DWORD cchMaxRawHeaders = 1024;
1995 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
1996 DWORD cchRawHeaders = 0;
1998 TRACE("-->\n");
2000 /* clear old response headers (eg. from a redirect response) */
2001 HTTP_clear_response_headers( lpwhr );
2003 if (!NETCON_connected(&lpwhr->netConnection))
2004 goto lend;
2007 * HACK peek at the buffer
2009 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
2012 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
2014 buflen = MAX_REPLY_LEN;
2015 memset(buffer, 0, MAX_REPLY_LEN);
2016 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2017 goto lend;
2018 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2020 /* regenerate raw headers */
2021 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2023 cchMaxRawHeaders *= 2;
2024 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2026 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2027 cchRawHeaders += (buflen-1);
2028 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2029 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2030 lpszRawHeaders[cchRawHeaders] = '\0';
2032 /* split the version from the status code */
2033 status_code = strchrW( buffer, ' ' );
2034 if( !status_code )
2035 goto lend;
2036 *status_code++=0;
2038 /* split the status code from the status text */
2039 status_text = strchrW( status_code, ' ' );
2040 if( !status_text )
2041 goto lend;
2042 *status_text++=0;
2044 TRACE("version [%s] status code [%s] status text [%s]\n",
2045 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
2046 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_VERSION], buffer );
2047 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_STATUS_CODE], status_code );
2048 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_STATUS_TEXT], status_text );
2050 /* Parse each response line */
2053 buflen = MAX_REPLY_LEN;
2054 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2056 TRACE("got line %s, now interpretting\n", debugstr_a(bufferA));
2057 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2059 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2061 cchMaxRawHeaders *= 2;
2062 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2064 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2065 cchRawHeaders += (buflen-1);
2066 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2067 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2068 lpszRawHeaders[cchRawHeaders] = '\0';
2070 if (!HTTP_InterpretHttpHeader(buffer, field, MAX_FIELD_LEN, value, MAX_FIELD_VALUE_LEN))
2071 break;
2073 HTTP_ProcessHeader(lpwhr, field, value, (HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE));
2075 else
2077 cbreaks++;
2078 if (cbreaks >= 2)
2079 break;
2081 }while(1);
2083 if (lpwhr->lpszRawHeaders) HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2084 lpwhr->lpszRawHeaders = lpszRawHeaders;
2085 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
2086 bSuccess = TRUE;
2088 lend:
2090 TRACE("<--\n");
2091 if (bSuccess)
2092 return rc;
2093 else
2094 return FALSE;
2098 /***********************************************************************
2099 * HTTP_InterpretHttpHeader (internal)
2101 * Parse server response
2103 * RETURNS
2105 * TRUE on success
2106 * FALSE on error
2108 static INT stripSpaces(LPCWSTR lpszSrc, LPWSTR lpszStart, INT *len)
2110 LPCWSTR lpsztmp;
2111 INT srclen;
2113 srclen = 0;
2115 while (*lpszSrc == ' ' && *lpszSrc != '\0')
2116 lpszSrc++;
2118 lpsztmp = lpszSrc;
2119 while(*lpsztmp != '\0')
2121 if (*lpsztmp != ' ')
2122 srclen = lpsztmp - lpszSrc + 1;
2124 lpsztmp++;
2127 *len = min(*len, srclen);
2128 strncpyW(lpszStart, lpszSrc, *len);
2129 lpszStart[*len] = '\0';
2131 return *len;
2135 BOOL HTTP_InterpretHttpHeader(LPWSTR buffer, LPWSTR field, INT fieldlen, LPWSTR value, INT valuelen)
2137 WCHAR *pd;
2138 BOOL bSuccess = FALSE;
2140 TRACE("\n");
2142 *field = '\0';
2143 *value = '\0';
2145 pd = strchrW(buffer, ':');
2146 if (pd)
2148 *pd = '\0';
2149 if (stripSpaces(buffer, field, &fieldlen) > 0)
2151 if (stripSpaces(pd+1, value, &valuelen) > 0)
2152 bSuccess = TRUE;
2156 TRACE("%d: field(%s) Value(%s)\n", bSuccess, debugstr_w(field), debugstr_w(value));
2157 return bSuccess;
2161 /***********************************************************************
2162 * HTTP_GetStdHeaderIndex (internal)
2164 * Lookup field index in standard http header array
2166 * FIXME: This should be stuffed into a hash table
2168 INT HTTP_GetStdHeaderIndex(LPCWSTR lpszField)
2170 INT index = -1;
2171 static const WCHAR szContentLength[] = {
2172 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
2173 static const WCHAR szQueryRange[] = {
2174 'R','a','n','g','e',0};
2175 static const WCHAR szContentRange[] = {
2176 'C','o','n','t','e','n','t','-','R','a','n','g','e',0};
2177 static const WCHAR szContentType[] = {
2178 'C','o','n','t','e','n','t','-','T','y','p','e',0};
2179 static const WCHAR szLastModified[] = {
2180 'L','a','s','t','-','M','o','d','i','f','i','e','d',0};
2181 static const WCHAR szLocation[] = {'L','o','c','a','t','i','o','n',0};
2182 static const WCHAR szAccept[] = {'A','c','c','e','p','t',0};
2183 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0};
2184 static const WCHAR szContentTrans[] = { 'C','o','n','t','e','n','t','-',
2185 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0};
2186 static const WCHAR szDate[] = { 'D','a','t','e',0};
2187 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0};
2188 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0};
2189 static const WCHAR szETag[] = { 'E','T','a','g',0};
2190 static const WCHAR szAcceptRanges[] = {
2191 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
2192 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
2193 static const WCHAR szMimeVersion[] = {
2194 'M','i','m','e','-','V','e','r','s','i','o','n', 0};
2195 static const WCHAR szPragma[] = { 'P','r','a','g','m','a', 0};
2196 static const WCHAR szCacheControl[] = {
2197 'C','a','c','h','e','-','C','o','n','t','r','o','l',0};
2198 static const WCHAR szUserAgent[] = { 'U','s','e','r','-','A','g','e','n','t',0};
2199 static const WCHAR szProxyAuth[] = {
2200 'P','r','o','x','y','-',
2201 'A','u','t','h','e','n','t','i','c','a','t','e', 0};
2202 static const WCHAR szContentEncoding[] = {
2203 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0};
2204 static const WCHAR szCookie[] = {'C','o','o','k','i','e',0};
2205 static const WCHAR szVary[] = {'V','a','r','y',0};
2206 static const WCHAR szVia[] = {'V','i','a',0};
2208 if (!strcmpiW(lpszField, szContentLength))
2209 index = HTTP_QUERY_CONTENT_LENGTH;
2210 else if (!strcmpiW(lpszField,szQueryRange))
2211 index = HTTP_QUERY_RANGE;
2212 else if (!strcmpiW(lpszField,szContentRange))
2213 index = HTTP_QUERY_CONTENT_RANGE;
2214 else if (!strcmpiW(lpszField,szContentType))
2215 index = HTTP_QUERY_CONTENT_TYPE;
2216 else if (!strcmpiW(lpszField,szLastModified))
2217 index = HTTP_QUERY_LAST_MODIFIED;
2218 else if (!strcmpiW(lpszField,szLocation))
2219 index = HTTP_QUERY_LOCATION;
2220 else if (!strcmpiW(lpszField,szAccept))
2221 index = HTTP_QUERY_ACCEPT;
2222 else if (!strcmpiW(lpszField,szReferer))
2223 index = HTTP_QUERY_REFERER;
2224 else if (!strcmpiW(lpszField,szContentTrans))
2225 index = HTTP_QUERY_CONTENT_TRANSFER_ENCODING;
2226 else if (!strcmpiW(lpszField,szDate))
2227 index = HTTP_QUERY_DATE;
2228 else if (!strcmpiW(lpszField,szServer))
2229 index = HTTP_QUERY_SERVER;
2230 else if (!strcmpiW(lpszField,szConnection))
2231 index = HTTP_QUERY_CONNECTION;
2232 else if (!strcmpiW(lpszField,szETag))
2233 index = HTTP_QUERY_ETAG;
2234 else if (!strcmpiW(lpszField,szAcceptRanges))
2235 index = HTTP_QUERY_ACCEPT_RANGES;
2236 else if (!strcmpiW(lpszField,szExpires))
2237 index = HTTP_QUERY_EXPIRES;
2238 else if (!strcmpiW(lpszField,szMimeVersion))
2239 index = HTTP_QUERY_MIME_VERSION;
2240 else if (!strcmpiW(lpszField,szPragma))
2241 index = HTTP_QUERY_PRAGMA;
2242 else if (!strcmpiW(lpszField,szCacheControl))
2243 index = HTTP_QUERY_CACHE_CONTROL;
2244 else if (!strcmpiW(lpszField,szUserAgent))
2245 index = HTTP_QUERY_USER_AGENT;
2246 else if (!strcmpiW(lpszField,szProxyAuth))
2247 index = HTTP_QUERY_PROXY_AUTHENTICATE;
2248 else if (!strcmpiW(lpszField,szContentEncoding))
2249 index = HTTP_QUERY_CONTENT_ENCODING;
2250 else if (!strcmpiW(lpszField,szCookie))
2251 index = HTTP_QUERY_COOKIE;
2252 else if (!strcmpiW(lpszField,szVary))
2253 index = HTTP_QUERY_VARY;
2254 else if (!strcmpiW(lpszField,szVia))
2255 index = HTTP_QUERY_VIA;
2256 else if (!strcmpiW(lpszField,g_szHost))
2257 index = HTTP_QUERY_HOST;
2258 else
2260 TRACE("Couldn't find %s in standard header table\n", debugstr_w(lpszField));
2263 return index;
2266 /***********************************************************************
2267 * HTTP_ReplaceHeaderValue (internal)
2269 BOOL HTTP_ReplaceHeaderValue( LPHTTPHEADERW lphttpHdr, LPCWSTR value )
2271 INT len = 0;
2273 if( lphttpHdr->lpszValue )
2274 HeapFree( GetProcessHeap(), 0, lphttpHdr->lpszValue );
2275 lphttpHdr->lpszValue = NULL;
2277 if( value )
2278 len = strlenW(value);
2279 if (len)
2281 lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0,
2282 (len+1)*sizeof(WCHAR));
2283 strcpyW(lphttpHdr->lpszValue, value);
2285 return TRUE;
2288 /***********************************************************************
2289 * HTTP_ProcessHeader (internal)
2291 * Stuff header into header tables according to <dwModifier>
2295 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2297 BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
2299 LPHTTPHEADERW lphttpHdr = NULL;
2300 BOOL bSuccess = FALSE;
2301 INT index;
2303 TRACE("--> %s: %s - 0x%08lx\n", debugstr_w(field), debugstr_w(value), dwModifier);
2305 /* Adjust modifier flags */
2306 if (dwModifier & COALESCEFLASG)
2307 dwModifier |= HTTP_ADDHDR_FLAG_ADD;
2309 /* Try to get index into standard header array */
2310 index = HTTP_GetStdHeaderIndex(field);
2311 if (index >= 0)
2313 lphttpHdr = &lpwhr->StdHeaders[index];
2315 else /* Find or create new custom header */
2317 index = HTTP_GetCustomHeaderIndex(lpwhr, field);
2318 if (index >= 0)
2320 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
2322 return FALSE;
2324 lphttpHdr = &lpwhr->pCustHeaders[index];
2326 else
2328 HTTPHEADERW hdr;
2330 hdr.lpszField = (LPWSTR)field;
2331 hdr.lpszValue = (LPWSTR)value;
2332 hdr.wFlags = hdr.wCount = 0;
2334 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2335 hdr.wFlags |= HDR_ISREQUEST;
2337 return HTTP_InsertCustomHeader(lpwhr, &hdr);
2341 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2342 lphttpHdr->wFlags |= HDR_ISREQUEST;
2343 else
2344 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
2346 if (!lphttpHdr->lpszValue && (dwModifier & (HTTP_ADDHDR_FLAG_ADD|HTTP_ADDHDR_FLAG_ADD_IF_NEW)))
2348 INT slen;
2350 if (!lpwhr->StdHeaders[index].lpszField)
2352 lphttpHdr->lpszField = WININET_strdupW(field);
2354 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2355 lphttpHdr->wFlags |= HDR_ISREQUEST;
2358 slen = strlenW(value) + 1;
2359 lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0, slen*sizeof(WCHAR));
2360 if (lphttpHdr->lpszValue)
2362 strcpyW(lphttpHdr->lpszValue, value);
2363 bSuccess = TRUE;
2365 else
2367 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2370 else if (lphttpHdr->lpszValue)
2372 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
2373 bSuccess = HTTP_ReplaceHeaderValue( lphttpHdr, value );
2374 else if (dwModifier & COALESCEFLASG)
2376 LPWSTR lpsztmp;
2377 WCHAR ch = 0;
2378 INT len = 0;
2379 INT origlen = strlenW(lphttpHdr->lpszValue);
2380 INT valuelen = strlenW(value);
2382 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
2384 ch = ',';
2385 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2387 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2389 ch = ';';
2390 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2393 len = origlen + valuelen + ((ch > 0) ? 1 : 0);
2395 lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
2396 if (lpsztmp)
2398 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
2399 if (ch > 0)
2401 lphttpHdr->lpszValue[origlen] = ch;
2402 origlen++;
2405 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
2406 lphttpHdr->lpszValue[len] = '\0';
2407 bSuccess = TRUE;
2409 else
2411 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
2412 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2416 TRACE("<-- %d\n",bSuccess);
2417 return bSuccess;
2421 /***********************************************************************
2422 * HTTP_CloseConnection (internal)
2424 * Close socket connection
2427 VOID HTTP_CloseConnection(LPWININETHTTPREQW lpwhr)
2429 LPWININETHTTPSESSIONW lpwhs = NULL;
2430 LPWININETAPPINFOW hIC = NULL;
2432 TRACE("%p\n",lpwhr);
2434 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
2435 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2437 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
2438 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2440 if (NETCON_connected(&lpwhr->netConnection))
2442 NETCON_close(&lpwhr->netConnection);
2445 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
2446 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2450 /***********************************************************************
2451 * HTTP_CloseHTTPRequestHandle (internal)
2453 * Deallocate request handle
2456 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr)
2458 DWORD i;
2459 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
2461 TRACE("\n");
2463 if (NETCON_connected(&lpwhr->netConnection))
2464 HTTP_CloseConnection(lpwhr);
2466 if (lpwhr->lpszPath)
2467 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2468 if (lpwhr->lpszVerb)
2469 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
2470 if (lpwhr->lpszRawHeaders)
2471 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2473 for (i = 0; i <= HTTP_QUERY_MAX; i++)
2475 if (lpwhr->StdHeaders[i].lpszField)
2476 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszField);
2477 if (lpwhr->StdHeaders[i].lpszValue)
2478 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszValue);
2481 for (i = 0; i < lpwhr->nCustHeaders; i++)
2483 if (lpwhr->pCustHeaders[i].lpszField)
2484 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
2485 if (lpwhr->pCustHeaders[i].lpszValue)
2486 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
2489 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
2490 HeapFree(GetProcessHeap(), 0, lpwhr);
2494 /***********************************************************************
2495 * HTTP_CloseHTTPSessionHandle (internal)
2497 * Deallocate session handle
2500 void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr)
2502 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
2504 TRACE("%p\n", lpwhs);
2506 if (lpwhs->lpszServerName)
2507 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2508 if (lpwhs->lpszUserName)
2509 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2510 HeapFree(GetProcessHeap(), 0, lpwhs);
2514 /***********************************************************************
2515 * HTTP_GetCustomHeaderIndex (internal)
2517 * Return index of custom header from header array
2520 INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField)
2522 DWORD index;
2524 TRACE("%s\n", debugstr_w(lpszField));
2526 for (index = 0; index < lpwhr->nCustHeaders; index++)
2528 if (!strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
2529 break;
2533 if (index >= lpwhr->nCustHeaders)
2534 index = -1;
2536 TRACE("Return: %lu\n", index);
2537 return index;
2541 /***********************************************************************
2542 * HTTP_InsertCustomHeader (internal)
2544 * Insert header into array
2547 BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
2549 INT count;
2550 LPHTTPHEADERW lph = NULL;
2551 BOOL r = FALSE;
2553 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
2554 count = lpwhr->nCustHeaders + 1;
2555 if (count > 1)
2556 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
2557 else
2558 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
2560 if (NULL != lph)
2562 lpwhr->pCustHeaders = lph;
2563 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
2564 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
2565 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
2566 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
2567 lpwhr->nCustHeaders++;
2568 r = TRUE;
2570 else
2572 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2575 return r;
2579 /***********************************************************************
2580 * HTTP_DeleteCustomHeader (internal)
2582 * Delete header from array
2583 * If this function is called, the indexs may change.
2585 BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
2587 if( lpwhr->nCustHeaders <= 0 )
2588 return FALSE;
2589 if( lpwhr->nCustHeaders >= index )
2590 return FALSE;
2591 lpwhr->nCustHeaders--;
2593 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
2594 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
2595 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
2597 return TRUE;
2600 /***********************************************************************
2601 * IsHostInProxyBypassList (@)
2603 * Undocumented
2606 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
2608 FIXME("STUB: flags=%ld host=%s length=%ld\n",flags,szHost,length);
2609 return FALSE;