Strip dangling \r\n from HTTP_HttpSendRequest.
[wine/multimedia.git] / dlls / wininet / http.c
blobf1dab8d808f7b47addcb2c5b43f67003bdfa1c44
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_szHost[] = {'\r','\n','H','o','s','t',':',' ',0 };
62 static const WCHAR g_szReferer[] = {'R','e','f','e','r','e','r',0};
63 static const WCHAR g_szAccept[] = {'A','c','c','e','p','t',0};
64 static const WCHAR g_szUserAgent[] = {'U','s','e','r','-','A','g','e','n','t',0};
67 #define HTTPHEADER g_szHttp
68 #define HTTPHOSTHEADER g_szHost
69 #define MAXHOSTNAME 100
70 #define MAX_FIELD_VALUE_LEN 256
71 #define MAX_FIELD_LEN 256
73 #define HTTP_REFERER g_szReferer
74 #define HTTP_ACCEPT g_szAccept
75 #define HTTP_USERAGENT g_szUserAgent
77 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
78 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
79 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
80 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
81 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
82 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
83 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
86 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr);
87 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr);
88 BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr);
89 int HTTP_WriteDataToStream(LPWININETHTTPREQW lpwhr,
90 void *Buffer, int BytesToWrite);
91 int HTTP_ReadDataFromStream(LPWININETHTTPREQW lpwhr,
92 void *Buffer, int BytesToRead);
93 BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr);
94 BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
95 BOOL HTTP_ReplaceHeaderValue( LPHTTPHEADERW lphttpHdr, LPCWSTR lpsztmp );
96 void HTTP_CloseConnection(LPWININETHTTPREQW lpwhr);
97 BOOL HTTP_InterpretHttpHeader(LPWSTR buffer, LPWSTR field, INT fieldlen, LPWSTR value, INT valuelen);
98 INT HTTP_GetStdHeaderIndex(LPCWSTR lpszField);
99 BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr);
100 INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField);
101 BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index);
103 /***********************************************************************
104 * HTTP_Tokenize (internal)
106 * Tokenize a string, allocating memory for the tokens.
108 static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
110 LPWSTR * token_array;
111 int tokens = 0;
112 int i;
113 LPCWSTR next_token;
115 /* empty string has no tokens */
116 if (*string)
117 tokens++;
118 /* count tokens */
119 for (i = 0; string[i]; i++)
120 if (!strncmpW(string+i, token_string, strlenW(token_string)))
122 DWORD j;
123 tokens++;
124 /* we want to skip over separators, but not the null terminator */
125 for (j = 0; j < strlenW(token_string) - 1; j++)
126 if (!string[i+j])
127 break;
128 i += j;
131 /* add 1 for terminating NULL */
132 token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
133 token_array[tokens] = NULL;
134 if (!tokens)
135 return token_array;
136 for (i = 0; i < tokens; i++)
138 int len;
139 next_token = strstrW(string, token_string);
140 if (!next_token) next_token = string+strlenW(string);
141 len = next_token - string;
142 token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
143 memcpy(token_array[i], string, len*sizeof(WCHAR));
144 token_array[i][len] = '\0';
145 string = next_token+strlenW(token_string);
147 return token_array;
150 /***********************************************************************
151 * HTTP_FreeTokens (internal)
153 * Frees memory returned from HTTP_Tokenize.
155 static void HTTP_FreeTokens(LPWSTR * token_array)
157 int i;
158 for (i = 0; token_array[i]; i++)
159 HeapFree(GetProcessHeap(), 0, token_array[i]);
160 HeapFree(GetProcessHeap(), 0, token_array);
163 /***********************************************************************
164 * HTTP_HttpAddRequestHeadersW (internal)
166 static BOOL WINAPI HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
167 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
169 LPWSTR lpszStart;
170 LPWSTR lpszEnd;
171 LPWSTR buffer;
172 WCHAR value[MAX_FIELD_VALUE_LEN], field[MAX_FIELD_LEN];
173 BOOL bSuccess = FALSE;
174 DWORD len;
176 TRACE("copying header: %s\n", debugstr_w(lpszHeader));
178 if( dwHeaderLength == ~0UL )
179 len = strlenW(lpszHeader);
180 else
181 len = dwHeaderLength;
182 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
183 strncpyW( buffer, lpszHeader, len );
184 buffer[len]=0;
186 lpszStart = buffer;
190 lpszEnd = lpszStart;
192 while (*lpszEnd != '\0')
194 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
195 break;
196 lpszEnd++;
199 if (*lpszStart == '\0')
200 break;
202 if (*lpszEnd == '\r')
204 *lpszEnd = '\0';
205 lpszEnd += 2; /* Jump over \r\n */
207 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
208 if (HTTP_InterpretHttpHeader(lpszStart, field, MAX_FIELD_LEN, value, MAX_FIELD_VALUE_LEN))
209 bSuccess = HTTP_ProcessHeader(lpwhr, field, value, dwModifier | HTTP_ADDHDR_FLAG_REQ);
211 lpszStart = lpszEnd;
213 } while (bSuccess);
215 HeapFree(GetProcessHeap(), 0, buffer);
217 return bSuccess;
220 /***********************************************************************
221 * HttpAddRequestHeadersW (WININET.@)
223 * Adds one or more HTTP header to the request handler
225 * RETURNS
226 * TRUE on success
227 * FALSE on failure
230 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
231 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
233 BOOL bSuccess = FALSE;
234 LPWININETHTTPREQW lpwhr;
236 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_w(lpszHeader), dwHeaderLength,
237 dwModifier);
239 if (!lpszHeader)
240 return TRUE;
242 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
243 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
245 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
246 goto lend;
248 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
249 lend:
250 if( lpwhr )
251 WININET_Release( &lpwhr->hdr );
253 return bSuccess;
256 /***********************************************************************
257 * HttpAddRequestHeadersA (WININET.@)
259 * Adds one or more HTTP header to the request handler
261 * RETURNS
262 * TRUE on success
263 * FALSE on failure
266 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
267 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
269 DWORD len;
270 LPWSTR hdr;
271 BOOL r;
273 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_a(lpszHeader), dwHeaderLength,
274 dwModifier);
276 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
277 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
278 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
279 if( dwHeaderLength != ~0UL )
280 dwHeaderLength = len;
282 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
284 HeapFree( GetProcessHeap(), 0, hdr );
286 return r;
289 /***********************************************************************
290 * HttpEndRequestA (WININET.@)
292 * Ends an HTTP request that was started by HttpSendRequestEx
294 * RETURNS
295 * TRUE if successful
296 * FALSE on failure
299 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest, LPINTERNET_BUFFERSA lpBuffersOut,
300 DWORD dwFlags, DWORD dwContext)
302 FIXME("stub\n");
303 return FALSE;
306 /***********************************************************************
307 * HttpEndRequestW (WININET.@)
309 * Ends an HTTP request that was started by HttpSendRequestEx
311 * RETURNS
312 * TRUE if successful
313 * FALSE on failure
316 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest, LPINTERNET_BUFFERSW lpBuffersOut,
317 DWORD dwFlags, DWORD dwContext)
319 FIXME("stub\n");
320 return FALSE;
323 /***********************************************************************
324 * HttpOpenRequestW (WININET.@)
326 * Open a HTTP request handle
328 * RETURNS
329 * HINTERNET a HTTP request handle on success
330 * NULL on failure
333 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
334 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
335 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
336 DWORD dwFlags, DWORD dwContext)
338 LPWININETHTTPSESSIONW lpwhs;
339 LPWININETAPPINFOW hIC = NULL;
340 HINTERNET handle = NULL;
342 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
343 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
344 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
345 dwFlags, dwContext);
346 if(lpszAcceptTypes!=NULL)
348 int i;
349 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
350 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
353 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
354 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
356 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
357 goto lend;
359 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
362 * My tests seem to show that the windows version does not
363 * become asynchronous until after this point. And anyhow
364 * if this call was asynchronous then how would you get the
365 * necessary HINTERNET pointer returned by this function.
368 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
369 lpszVersion, lpszReferrer, lpszAcceptTypes,
370 dwFlags, dwContext);
371 lend:
372 if( lpwhs )
373 WININET_Release( &lpwhs->hdr );
374 TRACE("returning %p\n", handle);
375 return handle;
379 /***********************************************************************
380 * HttpOpenRequestA (WININET.@)
382 * Open a HTTP request handle
384 * RETURNS
385 * HINTERNET a HTTP request handle on success
386 * NULL on failure
389 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
390 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
391 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
392 DWORD dwFlags, DWORD dwContext)
394 LPWSTR szVerb = NULL, szObjectName = NULL;
395 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
396 INT len;
397 INT acceptTypesCount;
398 HINTERNET rc = FALSE;
399 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
400 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
401 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
402 dwFlags, dwContext);
404 if (lpszVerb)
406 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
407 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
408 if ( !szVerb )
409 goto end;
410 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
413 if (lpszObjectName)
415 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
416 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
417 if ( !szObjectName )
418 goto end;
419 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
422 if (lpszVersion)
424 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
425 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
426 if ( !szVersion )
427 goto end;
428 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
431 if (lpszReferrer)
433 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
434 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
435 if ( !szReferrer )
436 goto end;
437 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
440 acceptTypesCount = 0;
441 if (lpszAcceptTypes)
443 /* find out how many there are */
444 while (lpszAcceptTypes[acceptTypesCount])
445 acceptTypesCount++;
446 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
447 acceptTypesCount = 0;
448 while (lpszAcceptTypes[acceptTypesCount])
450 len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
451 -1, NULL, 0 );
452 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
453 if (!szAcceptTypes[acceptTypesCount] )
454 goto end;
455 MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
456 -1, szAcceptTypes[acceptTypesCount], len );
457 acceptTypesCount++;
459 szAcceptTypes[acceptTypesCount] = NULL;
461 else szAcceptTypes = 0;
463 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
464 szVersion, szReferrer,
465 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
467 end:
468 if (szAcceptTypes)
470 acceptTypesCount = 0;
471 while (szAcceptTypes[acceptTypesCount])
473 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
474 acceptTypesCount++;
476 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
478 if (szReferrer) HeapFree(GetProcessHeap(), 0, szReferrer);
479 if (szVersion) HeapFree(GetProcessHeap(), 0, szVersion);
480 if (szObjectName) HeapFree(GetProcessHeap(), 0, szObjectName);
481 if (szVerb) HeapFree(GetProcessHeap(), 0, szVerb);
483 return rc;
486 /***********************************************************************
487 * HTTP_Base64
489 static UINT HTTP_Base64( LPCWSTR bin, LPWSTR base64 )
491 UINT n = 0, x;
492 static LPSTR HTTP_Base64Enc =
493 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
495 while( bin[0] )
497 /* first 6 bits, all from bin[0] */
498 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
499 x = (bin[0] & 3) << 4;
501 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
502 if( !bin[1] )
504 base64[n++] = HTTP_Base64Enc[x];
505 base64[n++] = '=';
506 base64[n++] = '=';
507 break;
509 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
510 x = ( bin[1] & 0x0f ) << 2;
512 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
513 if( !bin[2] )
515 base64[n++] = HTTP_Base64Enc[x];
516 base64[n++] = '=';
517 break;
519 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
521 /* last 6 bits, all from bin [2] */
522 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
523 bin += 3;
525 base64[n] = 0;
526 return n;
529 /***********************************************************************
530 * HTTP_EncodeBasicAuth
532 * Encode the basic authentication string for HTTP 1.1
534 static LPWSTR HTTP_EncodeBasicAuth( LPCWSTR username, LPCWSTR password)
536 UINT len;
537 LPWSTR in, out;
538 static const WCHAR szBasic[] = {'B','a','s','i','c',' ',0};
539 static const WCHAR szColon[] = {':',0};
541 len = lstrlenW( username ) + 1 + lstrlenW ( password ) + 1;
542 in = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
543 if( !in )
544 return NULL;
546 len = lstrlenW(szBasic) +
547 (lstrlenW( username ) + 1 + lstrlenW ( password ))*2 + 1 + 1;
548 out = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
549 if( out )
551 lstrcpyW( in, username );
552 lstrcatW( in, szColon );
553 lstrcatW( in, password );
554 lstrcpyW( out, szBasic );
555 HTTP_Base64( in, &out[strlenW(out)] );
557 HeapFree( GetProcessHeap(), 0, in );
559 return out;
562 /***********************************************************************
563 * HTTP_InsertProxyAuthorization
565 * Insert the basic authorization field in the request header
567 BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
568 LPCWSTR username, LPCWSTR password )
570 HTTPHEADERW hdr;
571 INT index;
572 static const WCHAR szProxyAuthorization[] = {
573 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
575 hdr.lpszValue = HTTP_EncodeBasicAuth( username, password );
576 hdr.lpszField = (WCHAR *)szProxyAuthorization;
577 hdr.wFlags = HDR_ISREQUEST;
578 hdr.wCount = 0;
579 if( !hdr.lpszValue )
580 return FALSE;
582 TRACE("Inserting %s = %s\n",
583 debugstr_w( hdr.lpszField ), debugstr_w( hdr.lpszValue ) );
585 /* remove the old proxy authorization header */
586 index = HTTP_GetCustomHeaderIndex( lpwhr, hdr.lpszField );
587 if( index >=0 )
588 HTTP_DeleteCustomHeader( lpwhr, index );
590 HTTP_InsertCustomHeader(lpwhr, &hdr);
591 HeapFree( GetProcessHeap(), 0, hdr.lpszValue );
593 return TRUE;
596 /***********************************************************************
597 * HTTP_DealWithProxy
599 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
600 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
602 WCHAR buf[MAXHOSTNAME];
603 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
604 WCHAR* url;
605 static const WCHAR szNul[] = { 0 };
606 URL_COMPONENTSW UrlComponents;
607 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }, szSlash[] = { '/',0 } ;
608 static const WCHAR szFormat1[] = { 'h','t','t','p',':','/','/','%','s',0 };
609 static const WCHAR szFormat2[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
610 int len;
612 memset( &UrlComponents, 0, sizeof UrlComponents );
613 UrlComponents.dwStructSize = sizeof UrlComponents;
614 UrlComponents.lpszHostName = buf;
615 UrlComponents.dwHostNameLength = MAXHOSTNAME;
617 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
618 buf,strlenW(szHttp),szHttp,strlenW(szHttp)) )
619 sprintfW(proxy, szFormat1, hIC->lpszProxy);
620 else
621 strcpyW(proxy,buf);
622 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
623 return FALSE;
624 if( UrlComponents.dwHostNameLength == 0 )
625 return FALSE;
627 if( !lpwhr->lpszPath )
628 lpwhr->lpszPath = (LPWSTR)szNul;
629 TRACE("server='%s' path='%s'\n",
630 debugstr_w(lpwhs->lpszServerName), debugstr_w(lpwhr->lpszPath));
631 /* for constant 15 see above */
632 len = strlenW(lpwhs->lpszServerName) + strlenW(lpwhr->lpszPath) + 15;
633 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
635 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
636 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
638 sprintfW(url, szFormat2, lpwhs->lpszServerName, lpwhs->nServerPort);
640 if( lpwhr->lpszPath[0] != '/' )
641 strcatW( url, szSlash );
642 strcatW(url, lpwhr->lpszPath);
643 if(lpwhr->lpszPath != szNul)
644 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
645 lpwhr->lpszPath = url;
646 /* FIXME: Do I have to free lpwhs->lpszServerName here ? */
647 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
648 lpwhs->nServerPort = UrlComponents.nPort;
650 return TRUE;
653 /***********************************************************************
654 * HTTP_HttpOpenRequestW (internal)
656 * Open a HTTP request handle
658 * RETURNS
659 * HINTERNET a HTTP request handle on success
660 * NULL on failure
663 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
664 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
665 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
666 DWORD dwFlags, DWORD dwContext)
668 LPWININETAPPINFOW hIC = NULL;
669 LPWININETHTTPREQW lpwhr;
670 LPWSTR lpszCookies;
671 LPWSTR lpszUrl = NULL;
672 DWORD nCookieSize;
673 HINTERNET handle = NULL;
674 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
675 DWORD len;
677 TRACE("--> \n");
679 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
680 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
682 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
683 if (NULL == lpwhr)
685 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
686 goto lend;
688 lpwhr->hdr.htype = WH_HHTTPREQ;
689 lpwhr->hdr.lpwhparent = WININET_AddRef( &lpwhs->hdr );
690 lpwhr->hdr.dwFlags = dwFlags;
691 lpwhr->hdr.dwContext = dwContext;
692 lpwhr->hdr.dwRefCount = 1;
693 lpwhr->hdr.destroy = HTTP_CloseHTTPRequestHandle;
695 handle = WININET_AllocHandle( &lpwhr->hdr );
696 if (NULL == handle)
698 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
699 goto lend;
702 NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE);
704 if (NULL != lpszObjectName && strlenW(lpszObjectName)) {
705 HRESULT rc;
707 len = 0;
708 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
709 if (rc != E_POINTER)
710 len = strlenW(lpszObjectName)+1;
711 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
712 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
713 URL_ESCAPE_SPACES_ONLY);
714 if (rc)
716 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(lpszObjectName),rc);
717 strcpyW(lpwhr->lpszPath,lpszObjectName);
721 if (NULL != lpszReferrer && strlenW(lpszReferrer))
722 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDHDR_FLAG_COALESCE);
724 if(lpszAcceptTypes!=NULL)
726 int i;
727 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
728 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i], HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_REQ|HTTP_ADDHDR_FLAG_ADD_IF_NEW);
731 if (NULL == lpszVerb)
733 static const WCHAR szGet[] = {'G','E','T',0};
734 lpwhr->lpszVerb = WININET_strdupW(szGet);
736 else if (strlenW(lpszVerb))
737 lpwhr->lpszVerb = WININET_strdupW(lpszVerb);
739 if (NULL != lpszReferrer && strlenW(lpszReferrer))
741 WCHAR buf[MAXHOSTNAME];
742 URL_COMPONENTSW UrlComponents;
744 memset( &UrlComponents, 0, sizeof UrlComponents );
745 UrlComponents.dwStructSize = sizeof UrlComponents;
746 UrlComponents.lpszHostName = buf;
747 UrlComponents.dwHostNameLength = MAXHOSTNAME;
749 InternetCrackUrlW(lpszReferrer, 0, 0, &UrlComponents);
750 if (strlenW(UrlComponents.lpszHostName))
751 lpwhr->lpszHostName = WININET_strdupW(UrlComponents.lpszHostName);
752 } else {
753 lpwhr->lpszHostName = WININET_strdupW(lpwhs->lpszServerName);
755 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
756 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
758 if (hIC->lpszAgent)
760 WCHAR *agent_header;
761 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
763 len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
764 agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
765 sprintfW(agent_header, user_agent, hIC->lpszAgent );
767 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
768 HTTP_ADDREQ_FLAG_ADD);
769 HeapFree(GetProcessHeap(), 0, agent_header);
772 len = strlenW(lpwhr->lpszHostName) + strlenW(szUrlForm);
773 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
774 sprintfW( lpszUrl, szUrlForm, lpwhr->lpszHostName );
776 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
777 InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
779 int cnt = 0;
780 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
781 static const WCHAR szcrlf[] = {'\r','\n',0};
783 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
785 cnt += sprintfW(lpszCookies, szCookie);
786 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
787 strcatW(lpszCookies, szcrlf);
789 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
790 HTTP_ADDREQ_FLAG_ADD);
791 HeapFree(GetProcessHeap(), 0, lpszCookies);
793 HeapFree(GetProcessHeap(), 0, lpszUrl);
797 if (hIC->lpfnStatusCB)
799 INTERNET_ASYNC_RESULT iar;
801 iar.dwResult = (DWORD)handle;
802 iar.dwError = ERROR_SUCCESS;
804 SendAsyncCallback(hIC, &lpwhs->hdr, dwContext,
805 INTERNET_STATUS_HANDLE_CREATED, &iar,
806 sizeof(INTERNET_ASYNC_RESULT));
810 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
814 * According to my tests. The name is not resolved until a request is Opened
816 SendAsyncCallback(hIC, &lpwhs->hdr, dwContext,
817 INTERNET_STATUS_RESOLVING_NAME,
818 lpwhs->lpszServerName,
819 strlenW(lpwhs->lpszServerName)+1);
820 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
821 &lpwhs->phostent, &lpwhs->socketAddress))
823 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
824 InternetCloseHandle( handle );
825 handle = NULL;
826 goto lend;
829 SendAsyncCallback(hIC, &lpwhs->hdr, lpwhr->hdr.dwContext,
830 INTERNET_STATUS_NAME_RESOLVED,
831 &(lpwhs->socketAddress),
832 sizeof(struct sockaddr_in));
834 lend:
835 if( lpwhr )
836 WININET_Release( &lpwhr->hdr );
838 TRACE("<-- %p (%p)\n", handle, lpwhr);
839 return handle;
842 /***********************************************************************
843 * HTTP_HttpQueryInfoW (internal)
845 BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
846 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
848 LPHTTPHEADERW lphttpHdr = NULL;
849 BOOL bSuccess = FALSE;
851 /* Find requested header structure */
852 if ((dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK) == HTTP_QUERY_CUSTOM)
854 INT index = HTTP_GetCustomHeaderIndex(lpwhr, (LPWSTR)lpBuffer);
856 if (index < 0)
857 return bSuccess;
859 lphttpHdr = &lpwhr->pCustHeaders[index];
861 else
863 INT index = dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK;
865 if (index == HTTP_QUERY_RAW_HEADERS_CRLF)
867 DWORD len = strlenW(lpwhr->lpszRawHeaders);
868 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
870 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
871 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
872 return FALSE;
874 memcpy(lpBuffer, lpwhr->lpszRawHeaders, (len+1)*sizeof(WCHAR));
875 *lpdwBufferLength = len * sizeof(WCHAR);
877 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
879 return TRUE;
881 else if (index == HTTP_QUERY_RAW_HEADERS)
883 static const WCHAR szCrLf[] = {'\r','\n',0};
884 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
885 DWORD i, size = 0;
886 LPWSTR pszString = (WCHAR*)lpBuffer;
888 for (i = 0; ppszRawHeaderLines[i]; i++)
889 size += strlenW(ppszRawHeaderLines[i]) + 1;
891 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
893 HTTP_FreeTokens(ppszRawHeaderLines);
894 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
895 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
896 return FALSE;
899 for (i = 0; ppszRawHeaderLines[i]; i++)
901 DWORD len = strlenW(ppszRawHeaderLines[i]);
902 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
903 pszString += len+1;
905 *pszString = '\0';
907 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
909 *lpdwBufferLength = size * sizeof(WCHAR);
910 HTTP_FreeTokens(ppszRawHeaderLines);
912 return TRUE;
914 else if (index >= 0 && index <= HTTP_QUERY_MAX && lpwhr->StdHeaders[index].lpszValue)
916 lphttpHdr = &lpwhr->StdHeaders[index];
918 else
920 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
921 return bSuccess;
925 /* Ensure header satisifies requested attributes */
926 if ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
927 (~lphttpHdr->wFlags & HDR_ISREQUEST))
929 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
930 return bSuccess;
933 /* coalesce value to reuqested type */
934 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
936 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
937 bSuccess = TRUE;
939 TRACE(" returning number : %d\n", *(int *)lpBuffer);
941 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
943 time_t tmpTime;
944 struct tm tmpTM;
945 SYSTEMTIME *STHook;
947 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
949 tmpTM = *gmtime(&tmpTime);
950 STHook = (SYSTEMTIME *) lpBuffer;
951 if(STHook==NULL)
952 return bSuccess;
954 STHook->wDay = tmpTM.tm_mday;
955 STHook->wHour = tmpTM.tm_hour;
956 STHook->wMilliseconds = 0;
957 STHook->wMinute = tmpTM.tm_min;
958 STHook->wDayOfWeek = tmpTM.tm_wday;
959 STHook->wMonth = tmpTM.tm_mon + 1;
960 STHook->wSecond = tmpTM.tm_sec;
961 STHook->wYear = tmpTM.tm_year;
963 bSuccess = TRUE;
965 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
966 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
967 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
969 else if (dwInfoLevel & HTTP_QUERY_FLAG_COALESCE)
971 if (*lpdwIndex >= lphttpHdr->wCount)
973 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
975 else
977 /* Copy strncpyW(lpBuffer, lphttpHdr[*lpdwIndex], len); */
978 (*lpdwIndex)++;
981 else
983 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
985 if (len > *lpdwBufferLength)
987 *lpdwBufferLength = len;
988 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
989 return bSuccess;
992 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
993 *lpdwBufferLength = len - sizeof(WCHAR);
994 bSuccess = TRUE;
996 TRACE(" returning string : '%s'\n", debugstr_w(lpBuffer));
998 return bSuccess;
1001 /***********************************************************************
1002 * HttpQueryInfoW (WININET.@)
1004 * Queries for information about an HTTP request
1006 * RETURNS
1007 * TRUE on success
1008 * FALSE on failure
1011 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1012 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1014 BOOL bSuccess = FALSE;
1015 LPWININETHTTPREQW lpwhr;
1017 if (TRACE_ON(wininet)) {
1018 #define FE(x) { x, #x }
1019 static const wininet_flag_info query_flags[] = {
1020 FE(HTTP_QUERY_MIME_VERSION),
1021 FE(HTTP_QUERY_CONTENT_TYPE),
1022 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
1023 FE(HTTP_QUERY_CONTENT_ID),
1024 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
1025 FE(HTTP_QUERY_CONTENT_LENGTH),
1026 FE(HTTP_QUERY_CONTENT_LANGUAGE),
1027 FE(HTTP_QUERY_ALLOW),
1028 FE(HTTP_QUERY_PUBLIC),
1029 FE(HTTP_QUERY_DATE),
1030 FE(HTTP_QUERY_EXPIRES),
1031 FE(HTTP_QUERY_LAST_MODIFIED),
1032 FE(HTTP_QUERY_MESSAGE_ID),
1033 FE(HTTP_QUERY_URI),
1034 FE(HTTP_QUERY_DERIVED_FROM),
1035 FE(HTTP_QUERY_COST),
1036 FE(HTTP_QUERY_LINK),
1037 FE(HTTP_QUERY_PRAGMA),
1038 FE(HTTP_QUERY_VERSION),
1039 FE(HTTP_QUERY_STATUS_CODE),
1040 FE(HTTP_QUERY_STATUS_TEXT),
1041 FE(HTTP_QUERY_RAW_HEADERS),
1042 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
1043 FE(HTTP_QUERY_CONNECTION),
1044 FE(HTTP_QUERY_ACCEPT),
1045 FE(HTTP_QUERY_ACCEPT_CHARSET),
1046 FE(HTTP_QUERY_ACCEPT_ENCODING),
1047 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
1048 FE(HTTP_QUERY_AUTHORIZATION),
1049 FE(HTTP_QUERY_CONTENT_ENCODING),
1050 FE(HTTP_QUERY_FORWARDED),
1051 FE(HTTP_QUERY_FROM),
1052 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
1053 FE(HTTP_QUERY_LOCATION),
1054 FE(HTTP_QUERY_ORIG_URI),
1055 FE(HTTP_QUERY_REFERER),
1056 FE(HTTP_QUERY_RETRY_AFTER),
1057 FE(HTTP_QUERY_SERVER),
1058 FE(HTTP_QUERY_TITLE),
1059 FE(HTTP_QUERY_USER_AGENT),
1060 FE(HTTP_QUERY_WWW_AUTHENTICATE),
1061 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
1062 FE(HTTP_QUERY_ACCEPT_RANGES),
1063 FE(HTTP_QUERY_SET_COOKIE),
1064 FE(HTTP_QUERY_COOKIE),
1065 FE(HTTP_QUERY_REQUEST_METHOD),
1066 FE(HTTP_QUERY_REFRESH),
1067 FE(HTTP_QUERY_CONTENT_DISPOSITION),
1068 FE(HTTP_QUERY_AGE),
1069 FE(HTTP_QUERY_CACHE_CONTROL),
1070 FE(HTTP_QUERY_CONTENT_BASE),
1071 FE(HTTP_QUERY_CONTENT_LOCATION),
1072 FE(HTTP_QUERY_CONTENT_MD5),
1073 FE(HTTP_QUERY_CONTENT_RANGE),
1074 FE(HTTP_QUERY_ETAG),
1075 FE(HTTP_QUERY_HOST),
1076 FE(HTTP_QUERY_IF_MATCH),
1077 FE(HTTP_QUERY_IF_NONE_MATCH),
1078 FE(HTTP_QUERY_IF_RANGE),
1079 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
1080 FE(HTTP_QUERY_MAX_FORWARDS),
1081 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
1082 FE(HTTP_QUERY_RANGE),
1083 FE(HTTP_QUERY_TRANSFER_ENCODING),
1084 FE(HTTP_QUERY_UPGRADE),
1085 FE(HTTP_QUERY_VARY),
1086 FE(HTTP_QUERY_VIA),
1087 FE(HTTP_QUERY_WARNING),
1088 FE(HTTP_QUERY_CUSTOM)
1090 static const wininet_flag_info modifier_flags[] = {
1091 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
1092 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
1093 FE(HTTP_QUERY_FLAG_NUMBER),
1094 FE(HTTP_QUERY_FLAG_COALESCE)
1096 #undef FE
1097 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
1098 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
1099 DWORD i;
1101 TRACE("(%p, 0x%08lx)--> %ld\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
1102 TRACE(" Attribute:");
1103 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
1104 if (query_flags[i].val == info) {
1105 DPRINTF(" %s", query_flags[i].name);
1106 break;
1109 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
1110 DPRINTF(" Unknown (%08lx)", info);
1113 DPRINTF(" Modifier:");
1114 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
1115 if (modifier_flags[i].val & info_mod) {
1116 DPRINTF(" %s", modifier_flags[i].name);
1117 info_mod &= ~ modifier_flags[i].val;
1121 if (info_mod) {
1122 DPRINTF(" Unknown (%08lx)", info_mod);
1124 DPRINTF("\n");
1127 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1128 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1130 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1131 goto lend;
1134 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
1135 lpBuffer, lpdwBufferLength, lpdwIndex);
1137 lend:
1138 if( lpwhr )
1139 WININET_Release( &lpwhr->hdr );
1141 TRACE("%d <--\n", bSuccess);
1142 return bSuccess;
1145 /***********************************************************************
1146 * HttpQueryInfoA (WININET.@)
1148 * Queries for information about an HTTP request
1150 * RETURNS
1151 * TRUE on success
1152 * FALSE on failure
1155 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1156 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1158 BOOL result;
1159 DWORD len;
1160 WCHAR* bufferW;
1162 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
1163 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
1165 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
1166 lpdwBufferLength, lpdwIndex );
1169 len = (*lpdwBufferLength)*sizeof(WCHAR);
1170 bufferW = HeapAlloc( GetProcessHeap(), 0, len );
1171 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
1172 &len, lpdwIndex );
1173 if( result )
1175 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
1176 lpBuffer, *lpdwBufferLength, NULL, NULL );
1177 *lpdwBufferLength = len - 1;
1179 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
1181 else
1182 /* since the strings being returned from HttpQueryInfoW should be
1183 * only ASCII characters, it is reasonable to assume that all of
1184 * the Unicode characters can be reduced to a single byte */
1185 *lpdwBufferLength = len / sizeof(WCHAR);
1187 HeapFree(GetProcessHeap(), 0, bufferW );
1189 return result;
1192 /***********************************************************************
1193 * HttpSendRequestExA (WININET.@)
1195 * Sends the specified request to the HTTP server and allows chunked
1196 * transfers
1198 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
1199 LPINTERNET_BUFFERSA lpBuffersIn,
1200 LPINTERNET_BUFFERSA lpBuffersOut,
1201 DWORD dwFlags, DWORD dwContext)
1203 FIXME("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersIn,
1204 lpBuffersOut, dwFlags, dwContext);
1205 return FALSE;
1208 /***********************************************************************
1209 * HttpSendRequestW (WININET.@)
1211 * Sends the specified request to the HTTP server
1213 * RETURNS
1214 * TRUE on success
1215 * FALSE on failure
1218 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
1219 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1221 LPWININETHTTPREQW lpwhr;
1222 LPWININETHTTPSESSIONW lpwhs = NULL;
1223 LPWININETAPPINFOW hIC = NULL;
1224 BOOL r;
1226 TRACE("%p, %p (%s), %li, %p, %li)\n", hHttpRequest,
1227 lpszHeaders, debugstr_w(lpszHeaders), dwHeaderLength, lpOptional, dwOptionalLength);
1229 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1230 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1232 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1233 r = FALSE;
1234 goto lend;
1237 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1238 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1240 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1241 r = FALSE;
1242 goto lend;
1245 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1246 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1248 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1249 r = FALSE;
1250 goto lend;
1253 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1255 WORKREQUEST workRequest;
1256 struct WORKREQ_HTTPSENDREQUESTW *req;
1258 workRequest.asyncall = HTTPSENDREQUESTW;
1259 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
1260 req = &workRequest.u.HttpSendRequestW;
1261 if (lpszHeaders)
1262 req->lpszHeader = WININET_strdupW(lpszHeaders);
1263 else
1264 req->lpszHeader = 0;
1265 req->dwHeaderLength = dwHeaderLength;
1266 req->lpOptional = lpOptional;
1267 req->dwOptionalLength = dwOptionalLength;
1269 INTERNET_AsyncCall(&workRequest);
1271 * This is from windows.
1273 SetLastError(ERROR_IO_PENDING);
1274 r = FALSE;
1276 else
1278 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
1279 dwHeaderLength, lpOptional, dwOptionalLength);
1281 lend:
1282 if( lpwhr )
1283 WININET_Release( &lpwhr->hdr );
1284 return r;
1287 /***********************************************************************
1288 * HttpSendRequestA (WININET.@)
1290 * Sends the specified request to the HTTP server
1292 * RETURNS
1293 * TRUE on success
1294 * FALSE on failure
1297 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
1298 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1300 BOOL result;
1301 LPWSTR szHeaders=NULL;
1302 DWORD nLen=dwHeaderLength;
1303 if(lpszHeaders!=NULL)
1305 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
1306 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
1307 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
1309 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
1310 if(szHeaders!=NULL)
1311 HeapFree(GetProcessHeap(),0,szHeaders);
1312 return result;
1315 /***********************************************************************
1316 * HTTP_HandleRedirect (internal)
1318 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl, LPCWSTR lpszHeaders,
1319 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength)
1321 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1322 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1323 WCHAR path[2048];
1325 if(lpszUrl[0]=='/')
1327 /* if it's an absolute path, keep the same session info */
1328 strcpyW(path,lpszUrl);
1330 else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1332 TRACE("Redirect through proxy\n");
1333 strcpyW(path,lpszUrl);
1335 else
1337 URL_COMPONENTSW urlComponents;
1338 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
1339 WCHAR password[1024], extra[1024];
1340 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
1341 urlComponents.lpszScheme = protocol;
1342 urlComponents.dwSchemeLength = 32;
1343 urlComponents.lpszHostName = hostName;
1344 urlComponents.dwHostNameLength = MAXHOSTNAME;
1345 urlComponents.lpszUserName = userName;
1346 urlComponents.dwUserNameLength = 1024;
1347 urlComponents.lpszPassword = password;
1348 urlComponents.dwPasswordLength = 1024;
1349 urlComponents.lpszUrlPath = path;
1350 urlComponents.dwUrlPathLength = 2048;
1351 urlComponents.lpszExtraInfo = extra;
1352 urlComponents.dwExtraInfoLength = 1024;
1353 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
1354 return FALSE;
1356 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1357 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1359 #if 0
1361 * This upsets redirects to binary files on sourceforge.net
1362 * and gives an html page instead of the target file
1363 * Examination of the HTTP request sent by native wininet.dll
1364 * reveals that it doesn't send a referrer in that case.
1365 * Maybe there's a flag that enables this, or maybe a referrer
1366 * shouldn't be added in case of a redirect.
1369 /* consider the current host as the referrer */
1370 if (NULL != lpwhs->lpszServerName && strlenW(lpwhs->lpszServerName))
1371 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
1372 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
1373 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
1374 #endif
1376 if (NULL != lpwhs->lpszServerName)
1377 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1378 lpwhs->lpszServerName = WININET_strdupW(hostName);
1379 if (NULL != lpwhs->lpszUserName)
1380 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
1381 lpwhs->lpszUserName = WININET_strdupW(userName);
1382 lpwhs->nServerPort = urlComponents.nPort;
1384 if (NULL != lpwhr->lpszHostName)
1385 HeapFree(GetProcessHeap(), 0, lpwhr->lpszHostName);
1386 lpwhr->lpszHostName=WININET_strdupW(hostName);
1388 SendAsyncCallback(hIC, &lpwhs->hdr, lpwhr->hdr.dwContext,
1389 INTERNET_STATUS_RESOLVING_NAME,
1390 lpwhs->lpszServerName,
1391 strlenW(lpwhs->lpszServerName)+1);
1393 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1394 &lpwhs->phostent, &lpwhs->socketAddress))
1396 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1397 return FALSE;
1400 SendAsyncCallback(hIC, &lpwhs->hdr, lpwhr->hdr.dwContext,
1401 INTERNET_STATUS_NAME_RESOLVED,
1402 &(lpwhs->socketAddress),
1403 sizeof(struct sockaddr_in));
1407 if(lpwhr->lpszPath)
1408 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1409 lpwhr->lpszPath=NULL;
1410 if (strlenW(path))
1412 DWORD needed = 0;
1413 HRESULT rc;
1415 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
1416 if (rc != E_POINTER)
1417 needed = strlenW(path)+1;
1418 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
1419 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
1420 URL_ESCAPE_SPACES_ONLY);
1421 if (rc)
1423 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(path),rc);
1424 strcpyW(lpwhr->lpszPath,path);
1428 return HTTP_HttpSendRequestW(lpwhr, lpszHeaders, dwHeaderLength, lpOptional, dwOptionalLength);
1431 /***********************************************************************
1432 * HTTP_build_req (internal)
1434 * concatenate all the strings in the request together
1436 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
1438 LPCWSTR *t;
1439 LPWSTR str;
1441 for( t = list; *t ; t++ )
1442 len += strlenW( *t );
1443 len++;
1445 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1446 *str = 0;
1448 for( t = list; *t ; t++ )
1449 strcatW( str, *t );
1451 return str;
1454 /***********************************************************************
1455 * HTTP_HttpSendRequestW (internal)
1457 * Sends the specified request to the HTTP server
1459 * RETURNS
1460 * TRUE on success
1461 * FALSE on failure
1464 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
1465 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1467 INT cnt;
1468 DWORD i;
1469 BOOL bSuccess = FALSE;
1470 LPWSTR requestString = NULL;
1471 INT responseLen;
1472 LPWININETHTTPSESSIONW lpwhs = NULL;
1473 LPWININETAPPINFOW hIC = NULL;
1474 BOOL loop_next = FALSE;
1475 int CustHeaderIndex;
1477 TRACE("--> %p\n", lpwhr);
1479 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
1481 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1482 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1484 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1485 return FALSE;
1488 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1489 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1491 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1492 return FALSE;
1495 /* Clear any error information */
1496 INTERNET_SetLastError(0);
1499 /* We must have a verb */
1500 if (NULL == lpwhr->lpszVerb)
1502 goto lend;
1505 /* if we are using optional stuff, we must add the fixed header of that option length */
1506 if (lpOptional && dwOptionalLength)
1508 static const WCHAR szContentLength[] = {
1509 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0};
1510 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \n\r */ + 20 /* int */ ];
1511 sprintfW(contentLengthStr, szContentLength, dwOptionalLength);
1512 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD);
1517 static const WCHAR szSlash[] = { '/',0 };
1518 static const WCHAR szSpace[] = { ' ',0 };
1519 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
1520 static const WCHAR szcrlf[] = {'\r','\n', 0};
1521 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
1522 static const WCHAR szSetCookie[] = {'S','e','t','-','C','o','o','k','i','e',0 };
1523 static const WCHAR szColon[] = { ':',' ',0 };
1524 LPCWSTR *req;
1525 LPWSTR p;
1526 DWORD len, n;
1527 char *ascii_req;
1529 TRACE("Going to url %s %s\n", debugstr_w(lpwhr->lpszHostName), debugstr_w(lpwhr->lpszPath));
1530 loop_next = FALSE;
1532 /* If we don't have a path we set it to root */
1533 if (NULL == lpwhr->lpszPath)
1534 lpwhr->lpszPath = WININET_strdupW(szSlash);
1535 else /* remove \r and \n*/
1537 int nLen = strlenW(lpwhr->lpszPath);
1538 while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
1540 nLen--;
1541 lpwhr->lpszPath[nLen]='\0';
1545 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1546 lpwhr->lpszPath, strlenW(szHttp), szHttp, strlenW(szHttp) )
1547 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
1549 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
1550 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
1551 *fixurl = '/';
1552 strcpyW(fixurl + 1, lpwhr->lpszPath);
1553 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
1554 lpwhr->lpszPath = fixurl;
1557 /* add the headers the caller supplied */
1558 if( lpszHeaders && dwHeaderLength )
1560 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
1561 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
1564 /* allocate space for an array of all the string pointers to be added */
1565 len = (HTTP_QUERY_MAX + lpwhr->nCustHeaders)*4 + 9;
1566 req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
1568 /* add the verb, path and HTTP/1.0 */
1569 n = 0;
1570 req[n++] = lpwhr->lpszVerb;
1571 req[n++] = szSpace;
1572 req[n++] = lpwhr->lpszPath;
1573 req[n++] = HTTPHEADER;
1575 /* Append standard request headers */
1576 for (i = 0; i <= HTTP_QUERY_MAX; i++)
1578 if (lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST)
1580 req[n++] = szcrlf;
1581 req[n++] = lpwhr->StdHeaders[i].lpszField;
1582 req[n++] = szColon;
1583 req[n++] = lpwhr->StdHeaders[i].lpszValue;
1585 TRACE("Adding header %s (%s)\n",
1586 debugstr_w(lpwhr->StdHeaders[i].lpszField),
1587 debugstr_w(lpwhr->StdHeaders[i].lpszValue));
1591 /* Append custom request heades */
1592 for (i = 0; i < lpwhr->nCustHeaders; i++)
1594 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
1596 req[n++] = szcrlf;
1597 req[n++] = lpwhr->pCustHeaders[i].lpszField;
1598 req[n++] = szColon;
1599 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
1601 TRACE("Adding custom header %s (%s)\n",
1602 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
1603 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
1607 if (lpwhr->lpszHostName)
1609 req[n++] = HTTPHOSTHEADER;
1610 req[n++] = lpwhr->lpszHostName;
1613 if( n >= len )
1614 ERR("oops. buffer overrun\n");
1616 req[n] = NULL;
1617 requestString = HTTP_build_req( req, 4 );
1618 HeapFree( GetProcessHeap(), 0, req );
1621 * Set (header) termination string for request
1622 * Make sure there's exactly two new lines at the end of the request
1624 p = &requestString[strlenW(requestString)-1];
1625 while ( (*p == '\n') || (*p == '\r') )
1626 p--;
1627 strcpyW( p+1, sztwocrlf );
1629 TRACE("Request header -> %s\n", debugstr_w(requestString) );
1631 /* Send the request and store the results */
1632 if (!HTTP_OpenConnection(lpwhr))
1633 goto lend;
1635 /* send the request as ASCII, tack on the optional data */
1636 if( !lpOptional )
1637 dwOptionalLength = 0;
1638 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
1639 NULL, 0, NULL, NULL );
1640 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
1641 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
1642 ascii_req, len, NULL, NULL );
1643 if( lpOptional )
1644 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
1645 len = (len + dwOptionalLength - 1);
1646 ascii_req[len] = 0;
1647 TRACE("full request -> %s\n", ascii_req );
1649 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1650 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
1652 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
1653 HeapFree( GetProcessHeap(), 0, ascii_req );
1655 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1656 INTERNET_STATUS_REQUEST_SENT,
1657 &len,sizeof(DWORD));
1659 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1660 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
1662 if (cnt < 0)
1663 goto lend;
1665 responseLen = HTTP_GetResponseHeaders(lpwhr);
1666 if (responseLen)
1667 bSuccess = TRUE;
1669 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1670 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
1671 sizeof(DWORD));
1673 /* process headers here. Is this right? */
1674 CustHeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSetCookie);
1675 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && (CustHeaderIndex >= 0))
1677 LPHTTPHEADERW setCookieHeader;
1678 int nPosStart = 0, nPosEnd = 0, len;
1679 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
1681 setCookieHeader = &lpwhr->pCustHeaders[CustHeaderIndex];
1683 while (setCookieHeader->lpszValue[nPosEnd] != '\0')
1685 LPWSTR buf_cookie, cookie_name, cookie_data;
1686 LPWSTR buf_url;
1687 LPWSTR domain = NULL;
1688 int nEqualPos = 0;
1689 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
1690 setCookieHeader->lpszValue[nPosEnd] != '\0')
1692 nPosEnd++;
1694 if (setCookieHeader->lpszValue[nPosEnd] == ';')
1696 /* fixme: not case sensitive, strcasestr is gnu only */
1697 int nDomainPosEnd = 0;
1698 int nDomainPosStart = 0, nDomainLength = 0;
1699 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
1700 LPWSTR lpszDomain = strstrW(&setCookieHeader->lpszValue[nPosEnd], szDomain);
1701 if (lpszDomain)
1702 { /* they have specified their own domain, lets use it */
1703 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
1704 lpszDomain[nDomainPosEnd] != '\0')
1706 nDomainPosEnd++;
1708 nDomainPosStart = strlenW(szDomain);
1709 nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
1710 domain = HeapAlloc(GetProcessHeap(), 0, (nDomainLength + 1)*sizeof(WCHAR));
1711 strncpyW(domain, &lpszDomain[nDomainPosStart], nDomainLength);
1712 domain[nDomainLength] = '\0';
1715 if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
1716 buf_cookie = HeapAlloc(GetProcessHeap(), 0, ((nPosEnd - nPosStart) + 1)*sizeof(WCHAR));
1717 strncpyW(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart));
1718 buf_cookie[(nPosEnd - nPosStart)] = '\0';
1719 TRACE("%s\n", debugstr_w(buf_cookie));
1720 while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
1722 nEqualPos++;
1724 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
1726 HeapFree(GetProcessHeap(), 0, buf_cookie);
1727 break;
1730 cookie_name = HeapAlloc(GetProcessHeap(), 0, (nEqualPos + 1)*sizeof(WCHAR));
1731 strncpyW(cookie_name, buf_cookie, nEqualPos);
1732 cookie_name[nEqualPos] = '\0';
1733 cookie_data = &buf_cookie[nEqualPos + 1];
1736 len = strlenW((domain ? domain : lpwhr->lpszHostName)) + strlenW(lpwhr->lpszPath) + 9;
1737 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1738 sprintfW(buf_url, szFmt, (domain ? domain : lpwhr->lpszHostName)); /* FIXME PATH!!! */
1739 InternetSetCookieW(buf_url, cookie_name, cookie_data);
1741 HeapFree(GetProcessHeap(), 0, buf_url);
1742 HeapFree(GetProcessHeap(), 0, buf_cookie);
1743 HeapFree(GetProcessHeap(), 0, cookie_name);
1744 if (domain) HeapFree(GetProcessHeap(), 0, domain);
1745 nPosStart = nPosEnd;
1749 while (loop_next);
1751 lend:
1753 if (requestString)
1754 HeapFree(GetProcessHeap(), 0, requestString);
1756 /* TODO: send notification for P3P header */
1758 if(!(hIC->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
1760 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
1761 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
1762 (dwCode==302 || dwCode==301))
1764 WCHAR szNewLocation[2048];
1765 DWORD dwBufferSize=2048;
1766 dwIndex=0;
1767 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
1769 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1770 INTERNET_STATUS_REDIRECT, szNewLocation,
1771 dwBufferSize);
1772 return HTTP_HandleRedirect(lpwhr, szNewLocation, lpszHeaders,
1773 dwHeaderLength, lpOptional, dwOptionalLength);
1778 if (hIC->lpfnStatusCB)
1780 INTERNET_ASYNC_RESULT iar;
1782 iar.dwResult = (DWORD)bSuccess;
1783 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1785 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1786 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1787 sizeof(INTERNET_ASYNC_RESULT));
1790 TRACE("<--\n");
1791 return bSuccess;
1795 /***********************************************************************
1796 * HTTP_Connect (internal)
1798 * Create http session handle
1800 * RETURNS
1801 * HINTERNET a session handle on success
1802 * NULL on failure
1805 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
1806 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
1807 LPCWSTR lpszPassword, DWORD dwFlags, DWORD dwContext,
1808 DWORD dwInternalFlags)
1810 BOOL bSuccess = FALSE;
1811 LPWININETHTTPSESSIONW lpwhs = NULL;
1812 HINTERNET handle = NULL;
1814 TRACE("-->\n");
1816 assert( hIC->hdr.htype == WH_HINIT );
1818 hIC->hdr.dwContext = dwContext;
1820 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
1821 if (NULL == lpwhs)
1823 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1824 goto lerror;
1828 * According to my tests. The name is not resolved until a request is sent
1831 if (nServerPort == INTERNET_INVALID_PORT_NUMBER)
1832 nServerPort = INTERNET_DEFAULT_HTTP_PORT;
1834 lpwhs->hdr.htype = WH_HHTTPSESSION;
1835 lpwhs->hdr.lpwhparent = WININET_AddRef( &hIC->hdr );
1836 lpwhs->hdr.dwFlags = dwFlags;
1837 lpwhs->hdr.dwContext = dwContext;
1838 lpwhs->hdr.dwInternalFlags = dwInternalFlags;
1839 lpwhs->hdr.dwRefCount = 1;
1840 lpwhs->hdr.destroy = HTTP_CloseHTTPSessionHandle;
1842 handle = WININET_AllocHandle( &lpwhs->hdr );
1843 if (NULL == handle)
1845 ERR("Failed to alloc handle\n");
1846 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1847 goto lerror;
1850 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
1851 if(strchrW(hIC->lpszProxy, ' '))
1852 FIXME("Several proxies not implemented.\n");
1853 if(hIC->lpszProxyBypass)
1854 FIXME("Proxy bypass is ignored.\n");
1856 if (NULL != lpszServerName)
1857 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
1858 if (NULL != lpszUserName)
1859 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
1860 lpwhs->nServerPort = nServerPort;
1862 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
1863 if (hIC->lpfnStatusCB && !(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
1865 INTERNET_ASYNC_RESULT iar;
1867 iar.dwResult = (DWORD)handle;
1868 iar.dwError = ERROR_SUCCESS;
1870 SendAsyncCallback(hIC, &hIC->hdr, dwContext,
1871 INTERNET_STATUS_HANDLE_CREATED, &iar,
1872 sizeof(INTERNET_ASYNC_RESULT));
1875 bSuccess = TRUE;
1877 lerror:
1878 if( lpwhs )
1879 WININET_Release( &lpwhs->hdr );
1882 * a INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
1883 * windows
1886 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
1887 return handle;
1891 /***********************************************************************
1892 * HTTP_OpenConnection (internal)
1894 * Connect to a web server
1896 * RETURNS
1898 * TRUE on success
1899 * FALSE on failure
1901 BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
1903 BOOL bSuccess = FALSE;
1904 LPWININETHTTPSESSIONW lpwhs;
1905 LPWININETAPPINFOW hIC = NULL;
1907 TRACE("-->\n");
1910 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1912 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1913 goto lend;
1916 lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
1918 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1919 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1920 INTERNET_STATUS_CONNECTING_TO_SERVER,
1921 &(lpwhs->socketAddress),
1922 sizeof(struct sockaddr_in));
1924 if (!NETCON_create(&lpwhr->netConnection, lpwhs->phostent->h_addrtype,
1925 SOCK_STREAM, 0))
1927 WARN("Socket creation failed\n");
1928 goto lend;
1931 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
1932 sizeof(lpwhs->socketAddress)))
1934 WARN("Unable to connect to host (%s)\n", strerror(errno));
1935 goto lend;
1938 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
1939 INTERNET_STATUS_CONNECTED_TO_SERVER,
1940 &(lpwhs->socketAddress),
1941 sizeof(struct sockaddr_in));
1943 bSuccess = TRUE;
1945 lend:
1946 TRACE("%d <--\n", bSuccess);
1947 return bSuccess;
1951 /***********************************************************************
1952 * HTTP_clear_response_headers (internal)
1954 * clear out any old response headers
1956 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
1958 DWORD i;
1960 for( i=0; i<=HTTP_QUERY_MAX; i++ )
1962 if( !lpwhr->StdHeaders[i].lpszField )
1963 continue;
1964 if( !lpwhr->StdHeaders[i].lpszValue )
1965 continue;
1966 if ( lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST )
1967 continue;
1968 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[i], NULL );
1970 for( i=0; i<lpwhr->nCustHeaders; i++)
1972 if( !lpwhr->pCustHeaders[i].lpszField )
1973 continue;
1974 if( !lpwhr->pCustHeaders[i].lpszValue )
1975 continue;
1976 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
1977 continue;
1978 HTTP_ReplaceHeaderValue( &lpwhr->pCustHeaders[i], NULL );
1982 /***********************************************************************
1983 * HTTP_GetResponseHeaders (internal)
1985 * Read server response
1987 * RETURNS
1989 * TRUE on success
1990 * FALSE on error
1992 BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr)
1994 INT cbreaks = 0;
1995 WCHAR buffer[MAX_REPLY_LEN];
1996 DWORD buflen = MAX_REPLY_LEN;
1997 BOOL bSuccess = FALSE;
1998 INT rc = 0;
1999 WCHAR value[MAX_FIELD_VALUE_LEN], field[MAX_FIELD_LEN];
2000 static const WCHAR szHttp[] = { 'H','T','T','P',0 };
2001 static const WCHAR szCrLf[] = {'\r','\n',0};
2002 char bufferA[MAX_REPLY_LEN];
2003 LPWSTR status_code, status_text;
2004 DWORD cchMaxRawHeaders = 1024;
2005 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2006 DWORD cchRawHeaders = 0;
2008 TRACE("-->\n");
2010 /* clear old response headers (eg. from a redirect response) */
2011 HTTP_clear_response_headers( lpwhr );
2013 if (!NETCON_connected(&lpwhr->netConnection))
2014 goto lend;
2017 * HACK peek at the buffer
2019 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
2022 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
2024 buflen = MAX_REPLY_LEN;
2025 memset(buffer, 0, MAX_REPLY_LEN);
2026 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2027 goto lend;
2028 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2030 if (strncmpW(buffer, szHttp, 4) != 0)
2031 goto lend;
2033 /* regenerate raw headers */
2034 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2036 cchMaxRawHeaders *= 2;
2037 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2039 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2040 cchRawHeaders += (buflen-1);
2041 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2042 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2043 lpszRawHeaders[cchRawHeaders] = '\0';
2045 /* split the version from the status code */
2046 status_code = strchrW( buffer, ' ' );
2047 if( !status_code )
2048 goto lend;
2049 *status_code++=0;
2051 /* split the status code from the status text */
2052 status_text = strchrW( status_code, ' ' );
2053 if( !status_text )
2054 goto lend;
2055 *status_text++=0;
2057 TRACE("version [%s] status code [%s] status text [%s]\n",
2058 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
2059 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_VERSION], buffer );
2060 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_STATUS_CODE], status_code );
2061 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_STATUS_TEXT], status_text );
2063 /* Parse each response line */
2066 buflen = MAX_REPLY_LEN;
2067 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2069 TRACE("got line %s, now interpretting\n", debugstr_a(bufferA));
2070 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2072 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2074 cchMaxRawHeaders *= 2;
2075 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2077 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2078 cchRawHeaders += (buflen-1);
2079 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2080 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2081 lpszRawHeaders[cchRawHeaders] = '\0';
2083 if (!HTTP_InterpretHttpHeader(buffer, field, MAX_FIELD_LEN, value, MAX_FIELD_VALUE_LEN))
2084 break;
2086 HTTP_ProcessHeader(lpwhr, field, value, (HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE));
2088 else
2090 cbreaks++;
2091 if (cbreaks >= 2)
2092 break;
2094 }while(1);
2096 if (lpwhr->lpszRawHeaders) HeapFree(GetProcessHeap(), 0, lpszRawHeaders);
2097 lpwhr->lpszRawHeaders = lpszRawHeaders;
2098 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
2099 bSuccess = TRUE;
2101 lend:
2103 TRACE("<--\n");
2104 if (bSuccess)
2105 return rc;
2106 else
2107 return FALSE;
2111 /***********************************************************************
2112 * HTTP_InterpretHttpHeader (internal)
2114 * Parse server response
2116 * RETURNS
2118 * TRUE on success
2119 * FALSE on error
2121 static INT stripSpaces(LPCWSTR lpszSrc, LPWSTR lpszStart, INT *len)
2123 LPCWSTR lpsztmp;
2124 INT srclen;
2126 srclen = 0;
2128 while (*lpszSrc == ' ' && *lpszSrc != '\0')
2129 lpszSrc++;
2131 lpsztmp = lpszSrc;
2132 while(*lpsztmp != '\0')
2134 if (*lpsztmp != ' ')
2135 srclen = lpsztmp - lpszSrc + 1;
2137 lpsztmp++;
2140 *len = min(*len, srclen);
2141 strncpyW(lpszStart, lpszSrc, *len);
2142 lpszStart[*len] = '\0';
2144 return *len;
2148 BOOL HTTP_InterpretHttpHeader(LPWSTR buffer, LPWSTR field, INT fieldlen, LPWSTR value, INT valuelen)
2150 WCHAR *pd;
2151 BOOL bSuccess = FALSE;
2153 TRACE("\n");
2155 *field = '\0';
2156 *value = '\0';
2158 pd = strchrW(buffer, ':');
2159 if (pd)
2161 *pd = '\0';
2162 if (stripSpaces(buffer, field, &fieldlen) > 0)
2164 if (stripSpaces(pd+1, value, &valuelen) > 0)
2165 bSuccess = TRUE;
2169 TRACE("%d: field(%s) Value(%s)\n", bSuccess, debugstr_w(field), debugstr_w(value));
2170 return bSuccess;
2174 /***********************************************************************
2175 * HTTP_GetStdHeaderIndex (internal)
2177 * Lookup field index in standard http header array
2179 * FIXME: This should be stuffed into a hash table
2181 INT HTTP_GetStdHeaderIndex(LPCWSTR lpszField)
2183 INT index = -1;
2184 static const WCHAR szContentLength[] = {
2185 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
2186 static const WCHAR szQueryRange[] = {
2187 'R','a','n','g','e',0};
2188 static const WCHAR szContentRange[] = {
2189 'C','o','n','t','e','n','t','-','R','a','n','g','e',0};
2190 static const WCHAR szContentType[] = {
2191 'C','o','n','t','e','n','t','-','T','y','p','e',0};
2192 static const WCHAR szLastModified[] = {
2193 'L','a','s','t','-','M','o','d','i','f','i','e','d',0};
2194 static const WCHAR szLocation[] = {'L','o','c','a','t','i','o','n',0};
2195 static const WCHAR szAccept[] = {'A','c','c','e','p','t',0};
2196 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0};
2197 static const WCHAR szContentTrans[] = { 'C','o','n','t','e','n','t','-',
2198 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0};
2199 static const WCHAR szDate[] = { 'D','a','t','e',0};
2200 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0};
2201 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0};
2202 static const WCHAR szETag[] = { 'E','T','a','g',0};
2203 static const WCHAR szAcceptRanges[] = {
2204 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
2205 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
2206 static const WCHAR szMimeVersion[] = {
2207 'M','i','m','e','-','V','e','r','s','i','o','n', 0};
2208 static const WCHAR szPragma[] = { 'P','r','a','g','m','a', 0};
2209 static const WCHAR szCacheControl[] = {
2210 'C','a','c','h','e','-','C','o','n','t','r','o','l',0};
2211 static const WCHAR szUserAgent[] = { 'U','s','e','r','-','A','g','e','n','t',0};
2212 static const WCHAR szProxyAuth[] = {
2213 'P','r','o','x','y','-',
2214 'A','u','t','h','e','n','t','i','c','a','t','e', 0};
2215 static const WCHAR szContentEncoding[] = {
2216 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0};
2217 static const WCHAR szCookie[] = {'C','o','o','k','i','e',0};
2218 static const WCHAR szVary[] = {'V','a','r','y',0};
2219 static const WCHAR szVia[] = {'V','i','a',0};
2221 if (!strcmpiW(lpszField, szContentLength))
2222 index = HTTP_QUERY_CONTENT_LENGTH;
2223 else if (!strcmpiW(lpszField,szQueryRange))
2224 index = HTTP_QUERY_RANGE;
2225 else if (!strcmpiW(lpszField,szContentRange))
2226 index = HTTP_QUERY_CONTENT_RANGE;
2227 else if (!strcmpiW(lpszField,szContentType))
2228 index = HTTP_QUERY_CONTENT_TYPE;
2229 else if (!strcmpiW(lpszField,szLastModified))
2230 index = HTTP_QUERY_LAST_MODIFIED;
2231 else if (!strcmpiW(lpszField,szLocation))
2232 index = HTTP_QUERY_LOCATION;
2233 else if (!strcmpiW(lpszField,szAccept))
2234 index = HTTP_QUERY_ACCEPT;
2235 else if (!strcmpiW(lpszField,szReferer))
2236 index = HTTP_QUERY_REFERER;
2237 else if (!strcmpiW(lpszField,szContentTrans))
2238 index = HTTP_QUERY_CONTENT_TRANSFER_ENCODING;
2239 else if (!strcmpiW(lpszField,szDate))
2240 index = HTTP_QUERY_DATE;
2241 else if (!strcmpiW(lpszField,szServer))
2242 index = HTTP_QUERY_SERVER;
2243 else if (!strcmpiW(lpszField,szConnection))
2244 index = HTTP_QUERY_CONNECTION;
2245 else if (!strcmpiW(lpszField,szETag))
2246 index = HTTP_QUERY_ETAG;
2247 else if (!strcmpiW(lpszField,szAcceptRanges))
2248 index = HTTP_QUERY_ACCEPT_RANGES;
2249 else if (!strcmpiW(lpszField,szExpires))
2250 index = HTTP_QUERY_EXPIRES;
2251 else if (!strcmpiW(lpszField,szMimeVersion))
2252 index = HTTP_QUERY_MIME_VERSION;
2253 else if (!strcmpiW(lpszField,szPragma))
2254 index = HTTP_QUERY_PRAGMA;
2255 else if (!strcmpiW(lpszField,szCacheControl))
2256 index = HTTP_QUERY_CACHE_CONTROL;
2257 else if (!strcmpiW(lpszField,szUserAgent))
2258 index = HTTP_QUERY_USER_AGENT;
2259 else if (!strcmpiW(lpszField,szProxyAuth))
2260 index = HTTP_QUERY_PROXY_AUTHENTICATE;
2261 else if (!strcmpiW(lpszField,szContentEncoding))
2262 index = HTTP_QUERY_CONTENT_ENCODING;
2263 else if (!strcmpiW(lpszField,szCookie))
2264 index = HTTP_QUERY_COOKIE;
2265 else if (!strcmpiW(lpszField,szVary))
2266 index = HTTP_QUERY_VARY;
2267 else if (!strcmpiW(lpszField,szVia))
2268 index = HTTP_QUERY_VIA;
2269 else
2271 TRACE("Couldn't find %s in standard header table\n", debugstr_w(lpszField));
2274 return index;
2277 /***********************************************************************
2278 * HTTP_ReplaceHeaderValue (internal)
2280 BOOL HTTP_ReplaceHeaderValue( LPHTTPHEADERW lphttpHdr, LPCWSTR value )
2282 INT len = 0;
2284 if( lphttpHdr->lpszValue )
2285 HeapFree( GetProcessHeap(), 0, lphttpHdr->lpszValue );
2286 lphttpHdr->lpszValue = NULL;
2288 if( value )
2289 len = strlenW(value);
2290 if (len)
2292 lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0,
2293 (len+1)*sizeof(WCHAR));
2294 strcpyW(lphttpHdr->lpszValue, value);
2296 return TRUE;
2299 /***********************************************************************
2300 * HTTP_ProcessHeader (internal)
2302 * Stuff header into header tables according to <dwModifier>
2306 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2308 BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
2310 LPHTTPHEADERW lphttpHdr = NULL;
2311 BOOL bSuccess = FALSE;
2312 INT index;
2314 TRACE("--> %s: %s - 0x%08lx\n", debugstr_w(field), debugstr_w(value), dwModifier);
2316 /* Adjust modifier flags */
2317 if (dwModifier & COALESCEFLASG)
2318 dwModifier |= HTTP_ADDHDR_FLAG_ADD;
2320 /* Try to get index into standard header array */
2321 index = HTTP_GetStdHeaderIndex(field);
2322 if (index >= 0)
2324 lphttpHdr = &lpwhr->StdHeaders[index];
2326 else /* Find or create new custom header */
2328 index = HTTP_GetCustomHeaderIndex(lpwhr, field);
2329 if (index >= 0)
2331 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
2333 return FALSE;
2335 lphttpHdr = &lpwhr->pCustHeaders[index];
2337 else
2339 HTTPHEADERW hdr;
2341 hdr.lpszField = (LPWSTR)field;
2342 hdr.lpszValue = (LPWSTR)value;
2343 hdr.wFlags = hdr.wCount = 0;
2345 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2346 hdr.wFlags |= HDR_ISREQUEST;
2348 return HTTP_InsertCustomHeader(lpwhr, &hdr);
2352 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2353 lphttpHdr->wFlags |= HDR_ISREQUEST;
2354 else
2355 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
2357 if (!lphttpHdr->lpszValue && (dwModifier & (HTTP_ADDHDR_FLAG_ADD|HTTP_ADDHDR_FLAG_ADD_IF_NEW)))
2359 INT slen;
2361 if (!lpwhr->StdHeaders[index].lpszField)
2363 lphttpHdr->lpszField = WININET_strdupW(field);
2365 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2366 lphttpHdr->wFlags |= HDR_ISREQUEST;
2369 slen = strlenW(value) + 1;
2370 lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0, slen*sizeof(WCHAR));
2371 if (lphttpHdr->lpszValue)
2373 strcpyW(lphttpHdr->lpszValue, value);
2374 bSuccess = TRUE;
2376 else
2378 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2381 else if (lphttpHdr->lpszValue)
2383 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
2384 bSuccess = HTTP_ReplaceHeaderValue( lphttpHdr, value );
2385 else if (dwModifier & COALESCEFLASG)
2387 LPWSTR lpsztmp;
2388 WCHAR ch = 0;
2389 INT len = 0;
2390 INT origlen = strlenW(lphttpHdr->lpszValue);
2391 INT valuelen = strlenW(value);
2393 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
2395 ch = ',';
2396 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2398 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2400 ch = ';';
2401 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2404 len = origlen + valuelen + ((ch > 0) ? 1 : 0);
2406 lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
2407 if (lpsztmp)
2409 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
2410 if (ch > 0)
2412 lphttpHdr->lpszValue[origlen] = ch;
2413 origlen++;
2416 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
2417 lphttpHdr->lpszValue[len] = '\0';
2418 bSuccess = TRUE;
2420 else
2422 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
2423 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2427 TRACE("<-- %d\n",bSuccess);
2428 return bSuccess;
2432 /***********************************************************************
2433 * HTTP_CloseConnection (internal)
2435 * Close socket connection
2438 VOID HTTP_CloseConnection(LPWININETHTTPREQW lpwhr)
2440 LPWININETHTTPSESSIONW lpwhs = NULL;
2441 LPWININETAPPINFOW hIC = NULL;
2443 TRACE("%p\n",lpwhr);
2445 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
2446 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2448 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
2449 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2451 if (NETCON_connected(&lpwhr->netConnection))
2453 NETCON_close(&lpwhr->netConnection);
2456 SendAsyncCallback(hIC, &lpwhr->hdr, lpwhr->hdr.dwContext,
2457 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2461 /***********************************************************************
2462 * HTTP_CloseHTTPRequestHandle (internal)
2464 * Deallocate request handle
2467 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr)
2469 DWORD i;
2470 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
2472 TRACE("\n");
2474 if (NETCON_connected(&lpwhr->netConnection))
2475 HTTP_CloseConnection(lpwhr);
2477 if (lpwhr->lpszPath)
2478 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2479 if (lpwhr->lpszVerb)
2480 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
2481 if (lpwhr->lpszHostName)
2482 HeapFree(GetProcessHeap(), 0, lpwhr->lpszHostName);
2483 if (lpwhr->lpszRawHeaders)
2484 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2486 for (i = 0; i <= HTTP_QUERY_MAX; i++)
2488 if (lpwhr->StdHeaders[i].lpszField)
2489 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszField);
2490 if (lpwhr->StdHeaders[i].lpszValue)
2491 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszValue);
2494 for (i = 0; i < lpwhr->nCustHeaders; i++)
2496 if (lpwhr->pCustHeaders[i].lpszField)
2497 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
2498 if (lpwhr->pCustHeaders[i].lpszValue)
2499 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
2502 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
2503 HeapFree(GetProcessHeap(), 0, lpwhr);
2507 /***********************************************************************
2508 * HTTP_CloseHTTPSessionHandle (internal)
2510 * Deallocate session handle
2513 void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr)
2515 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
2517 TRACE("%p\n", lpwhs);
2519 if (lpwhs->lpszServerName)
2520 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2521 if (lpwhs->lpszUserName)
2522 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2523 HeapFree(GetProcessHeap(), 0, lpwhs);
2527 /***********************************************************************
2528 * HTTP_GetCustomHeaderIndex (internal)
2530 * Return index of custom header from header array
2533 INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField)
2535 DWORD index;
2537 TRACE("%s\n", debugstr_w(lpszField));
2539 for (index = 0; index < lpwhr->nCustHeaders; index++)
2541 if (!strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
2542 break;
2546 if (index >= lpwhr->nCustHeaders)
2547 index = -1;
2549 TRACE("Return: %lu\n", index);
2550 return index;
2554 /***********************************************************************
2555 * HTTP_InsertCustomHeader (internal)
2557 * Insert header into array
2560 BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
2562 INT count;
2563 LPHTTPHEADERW lph = NULL;
2564 BOOL r = FALSE;
2566 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
2567 count = lpwhr->nCustHeaders + 1;
2568 if (count > 1)
2569 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
2570 else
2571 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
2573 if (NULL != lph)
2575 lpwhr->pCustHeaders = lph;
2576 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
2577 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
2578 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
2579 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
2580 lpwhr->nCustHeaders++;
2581 r = TRUE;
2583 else
2585 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2588 return r;
2592 /***********************************************************************
2593 * HTTP_DeleteCustomHeader (internal)
2595 * Delete header from array
2596 * If this function is called, the indexs may change.
2598 BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
2600 if( lpwhr->nCustHeaders <= 0 )
2601 return FALSE;
2602 if( lpwhr->nCustHeaders >= index )
2603 return FALSE;
2604 lpwhr->nCustHeaders--;
2606 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
2607 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
2608 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
2610 return TRUE;
2613 /***********************************************************************
2614 * IsHostInProxyBypassList (@)
2616 * Undocumented
2619 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
2621 FIXME("STUB: flags=%ld host=%s length=%ld\n",flags,szHost,length);
2622 return FALSE;