2 * Wininet - Http Implementation
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 TransGaming Technologies Inc.
7 * Copyright 2004 Mike McCormack for CodeWeavers
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "wine/port.h"
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_SOCKET_H
33 # include <sys/socket.h>
51 #define NO_SHLWAPI_STREAM
55 #include "wine/debug.h"
56 #include "wine/unicode.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
60 static const WCHAR g_szHttp
[] = {' ','H','T','T','P','/','1','.','0',0 };
61 static const WCHAR g_szHost
[] = {'\r','\n','H','o','s','t',':',' ',0 };
62 static const WCHAR g_szReferer
[] = {'R','e','f','e','r','e','r',0};
63 static const WCHAR g_szAccept
[] = {'A','c','c','e','p','t',0};
64 static const WCHAR g_szUserAgent
[] = {'U','s','e','r','-','A','g','e','n','t',0};
67 #define HTTPHEADER g_szHttp
68 #define HTTPHOSTHEADER g_szHost
69 #define MAXHOSTNAME 100
70 #define MAX_FIELD_VALUE_LEN 256
71 #define MAX_FIELD_LEN 256
73 #define HTTP_REFERER g_szReferer
74 #define HTTP_ACCEPT g_szAccept
75 #define HTTP_USERAGENT g_szUserAgent
77 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
78 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
79 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
80 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
81 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
82 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
83 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
86 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr
);
87 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr
);
88 BOOL
HTTP_OpenConnection(LPWININETHTTPREQW lpwhr
);
89 int HTTP_WriteDataToStream(LPWININETHTTPREQW lpwhr
,
90 void *Buffer
, int BytesToWrite
);
91 int HTTP_ReadDataFromStream(LPWININETHTTPREQW lpwhr
,
92 void *Buffer
, int BytesToRead
);
93 BOOL
HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr
);
94 BOOL
HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr
, LPCWSTR field
, LPCWSTR value
, DWORD dwModifier
);
95 BOOL
HTTP_ReplaceHeaderValue( LPHTTPHEADERW lphttpHdr
, LPCWSTR lpsztmp
);
96 void HTTP_CloseConnection(LPWININETHTTPREQW lpwhr
);
97 BOOL
HTTP_InterpretHttpHeader(LPWSTR buffer
, LPWSTR field
, INT fieldlen
, LPWSTR value
, INT valuelen
);
98 INT
HTTP_GetStdHeaderIndex(LPCWSTR lpszField
);
99 BOOL
HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr
, LPHTTPHEADERW lpHdr
);
100 INT
HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszField
);
101 BOOL
HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr
, DWORD index
);
103 /***********************************************************************
104 * HTTP_Tokenize (internal)
106 * Tokenize a string, allocating memory for the tokens.
108 static LPWSTR
* HTTP_Tokenize(LPCWSTR string
, LPCWSTR token_string
)
110 LPWSTR
* token_array
;
115 /* empty string has no tokens */
119 for (i
= 0; string
[i
]; i
++)
120 if (!strncmpW(string
+i
, token_string
, strlenW(token_string
)))
124 /* we want to skip over separators, but not the null terminator */
125 for (j
= 0; j
< strlenW(token_string
) - 1; j
++)
131 /* add 1 for terminating NULL */
132 token_array
= HeapAlloc(GetProcessHeap(), 0, (tokens
+1) * sizeof(*token_array
));
133 token_array
[tokens
] = NULL
;
136 for (i
= 0; i
< tokens
; i
++)
139 next_token
= strstrW(string
, token_string
);
140 if (!next_token
) next_token
= string
+strlenW(string
);
141 len
= next_token
- string
;
142 token_array
[i
] = HeapAlloc(GetProcessHeap(), 0, (len
+1)*sizeof(WCHAR
));
143 memcpy(token_array
[i
], string
, len
*sizeof(WCHAR
));
144 token_array
[i
][len
] = '\0';
145 string
= next_token
+strlenW(token_string
);
150 /***********************************************************************
151 * HTTP_FreeTokens (internal)
153 * Frees memory returned from HTTP_Tokenize.
155 static void HTTP_FreeTokens(LPWSTR
* token_array
)
158 for (i
= 0; token_array
[i
]; i
++)
159 HeapFree(GetProcessHeap(), 0, token_array
[i
]);
160 HeapFree(GetProcessHeap(), 0, token_array
);
163 /***********************************************************************
164 * HTTP_HttpAddRequestHeadersW (internal)
166 static BOOL WINAPI
HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr
,
167 LPCWSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
172 WCHAR value
[MAX_FIELD_VALUE_LEN
], field
[MAX_FIELD_LEN
];
173 BOOL bSuccess
= FALSE
;
176 TRACE("copying header: %s\n", debugstr_w(lpszHeader
));
178 if( dwHeaderLength
== ~0UL )
179 len
= strlenW(lpszHeader
);
181 len
= dwHeaderLength
;
182 buffer
= HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR
)*(len
+1) );
183 strncpyW( buffer
, lpszHeader
, len
);
192 while (*lpszEnd
!= '\0')
194 if (*lpszEnd
== '\r' && *(lpszEnd
+ 1) == '\n')
199 if (*lpszStart
== '\0')
202 if (*lpszEnd
== '\r')
205 lpszEnd
+= 2; /* Jump over \r\n */
207 TRACE("interpreting header %s\n", debugstr_w(lpszStart
));
208 if (HTTP_InterpretHttpHeader(lpszStart
, field
, MAX_FIELD_LEN
, value
, MAX_FIELD_VALUE_LEN
))
209 bSuccess
= HTTP_ProcessHeader(lpwhr
, field
, value
, dwModifier
| HTTP_ADDHDR_FLAG_REQ
);
215 HeapFree(GetProcessHeap(), 0, buffer
);
220 /***********************************************************************
221 * HttpAddRequestHeadersW (WININET.@)
223 * Adds one or more HTTP header to the request handler
230 BOOL WINAPI
HttpAddRequestHeadersW(HINTERNET hHttpRequest
,
231 LPCWSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
233 BOOL bSuccess
= FALSE
;
234 LPWININETHTTPREQW lpwhr
;
236 TRACE("%p, %s, %li, %li\n", hHttpRequest
, debugstr_w(lpszHeader
), dwHeaderLength
,
242 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hHttpRequest
);
243 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
245 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
248 bSuccess
= HTTP_HttpAddRequestHeadersW( lpwhr
, lpszHeader
, dwHeaderLength
, dwModifier
);
251 WININET_Release( &lpwhr
->hdr
);
256 /***********************************************************************
257 * HttpAddRequestHeadersA (WININET.@)
259 * Adds one or more HTTP header to the request handler
266 BOOL WINAPI
HttpAddRequestHeadersA(HINTERNET hHttpRequest
,
267 LPCSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
273 TRACE("%p, %s, %li, %li\n", hHttpRequest
, debugstr_a(lpszHeader
), dwHeaderLength
,
276 len
= MultiByteToWideChar( CP_ACP
, 0, lpszHeader
, dwHeaderLength
, NULL
, 0 );
277 hdr
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
278 MultiByteToWideChar( CP_ACP
, 0, lpszHeader
, dwHeaderLength
, hdr
, len
);
279 if( dwHeaderLength
!= ~0UL )
280 dwHeaderLength
= len
;
282 r
= HttpAddRequestHeadersW( hHttpRequest
, hdr
, dwHeaderLength
, dwModifier
);
284 HeapFree( GetProcessHeap(), 0, hdr
);
289 /***********************************************************************
290 * HttpEndRequestA (WININET.@)
292 * Ends an HTTP request that was started by HttpSendRequestEx
299 BOOL WINAPI
HttpEndRequestA(HINTERNET hRequest
, LPINTERNET_BUFFERSA lpBuffersOut
,
300 DWORD dwFlags
, DWORD dwContext
)
306 /***********************************************************************
307 * HttpEndRequestW (WININET.@)
309 * Ends an HTTP request that was started by HttpSendRequestEx
316 BOOL WINAPI
HttpEndRequestW(HINTERNET hRequest
, LPINTERNET_BUFFERSW lpBuffersOut
,
317 DWORD dwFlags
, DWORD dwContext
)
323 /***********************************************************************
324 * HttpOpenRequestW (WININET.@)
326 * Open a HTTP request handle
329 * HINTERNET a HTTP request handle on success
333 HINTERNET WINAPI
HttpOpenRequestW(HINTERNET hHttpSession
,
334 LPCWSTR lpszVerb
, LPCWSTR lpszObjectName
, LPCWSTR lpszVersion
,
335 LPCWSTR lpszReferrer
, LPCWSTR
*lpszAcceptTypes
,
336 DWORD dwFlags
, DWORD dwContext
)
338 LPWININETHTTPSESSIONW lpwhs
;
339 LPWININETAPPINFOW hIC
= NULL
;
340 HINTERNET handle
= NULL
;
342 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession
,
343 debugstr_w(lpszVerb
), debugstr_w(lpszObjectName
),
344 debugstr_w(lpszVersion
), debugstr_w(lpszReferrer
), lpszAcceptTypes
,
346 if(lpszAcceptTypes
!=NULL
)
349 for(i
=0;lpszAcceptTypes
[i
]!=NULL
;i
++)
350 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes
[i
]));
353 lpwhs
= (LPWININETHTTPSESSIONW
) WININET_GetObject( hHttpSession
);
354 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
356 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
359 hIC
= (LPWININETAPPINFOW
) lpwhs
->hdr
.lpwhparent
;
362 * My tests seem to show that the windows version does not
363 * become asynchronous until after this point. And anyhow
364 * if this call was asynchronous then how would you get the
365 * necessary HINTERNET pointer returned by this function.
368 handle
= HTTP_HttpOpenRequestW(lpwhs
, lpszVerb
, lpszObjectName
,
369 lpszVersion
, lpszReferrer
, lpszAcceptTypes
,
373 WININET_Release( &lpwhs
->hdr
);
374 TRACE("returning %p\n", handle
);
379 /***********************************************************************
380 * HttpOpenRequestA (WININET.@)
382 * Open a HTTP request handle
385 * HINTERNET a HTTP request handle on success
389 HINTERNET WINAPI
HttpOpenRequestA(HINTERNET hHttpSession
,
390 LPCSTR lpszVerb
, LPCSTR lpszObjectName
, LPCSTR lpszVersion
,
391 LPCSTR lpszReferrer
, LPCSTR
*lpszAcceptTypes
,
392 DWORD dwFlags
, DWORD dwContext
)
394 LPWSTR szVerb
= NULL
, szObjectName
= NULL
;
395 LPWSTR szVersion
= NULL
, szReferrer
= NULL
, *szAcceptTypes
= NULL
;
397 INT acceptTypesCount
;
398 HINTERNET rc
= FALSE
;
399 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession
,
400 debugstr_a(lpszVerb
), debugstr_a(lpszObjectName
),
401 debugstr_a(lpszVersion
), debugstr_a(lpszReferrer
), lpszAcceptTypes
,
406 len
= MultiByteToWideChar(CP_ACP
, 0, lpszVerb
, -1, NULL
, 0 );
407 szVerb
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
410 MultiByteToWideChar(CP_ACP
, 0, lpszVerb
, -1, szVerb
, len
);
415 len
= MultiByteToWideChar(CP_ACP
, 0, lpszObjectName
, -1, NULL
, 0 );
416 szObjectName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
419 MultiByteToWideChar(CP_ACP
, 0, lpszObjectName
, -1, szObjectName
, len
);
424 len
= MultiByteToWideChar(CP_ACP
, 0, lpszVersion
, -1, NULL
, 0 );
425 szVersion
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
428 MultiByteToWideChar(CP_ACP
, 0, lpszVersion
, -1, szVersion
, len
);
433 len
= MultiByteToWideChar(CP_ACP
, 0, lpszReferrer
, -1, NULL
, 0 );
434 szReferrer
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
437 MultiByteToWideChar(CP_ACP
, 0, lpszReferrer
, -1, szReferrer
, len
);
440 acceptTypesCount
= 0;
443 /* find out how many there are */
444 while (lpszAcceptTypes
[acceptTypesCount
])
446 szAcceptTypes
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
*) * (acceptTypesCount
+1));
447 acceptTypesCount
= 0;
448 while (lpszAcceptTypes
[acceptTypesCount
])
450 len
= MultiByteToWideChar(CP_ACP
, 0, lpszAcceptTypes
[acceptTypesCount
],
452 szAcceptTypes
[acceptTypesCount
] = HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
453 if (!szAcceptTypes
[acceptTypesCount
] )
455 MultiByteToWideChar(CP_ACP
, 0, lpszAcceptTypes
[acceptTypesCount
],
456 -1, szAcceptTypes
[acceptTypesCount
], len
);
459 szAcceptTypes
[acceptTypesCount
] = NULL
;
461 else szAcceptTypes
= 0;
463 rc
= HttpOpenRequestW(hHttpSession
, szVerb
, szObjectName
,
464 szVersion
, szReferrer
,
465 (LPCWSTR
*)szAcceptTypes
, dwFlags
, dwContext
);
470 acceptTypesCount
= 0;
471 while (szAcceptTypes
[acceptTypesCount
])
473 HeapFree(GetProcessHeap(), 0, szAcceptTypes
[acceptTypesCount
]);
476 HeapFree(GetProcessHeap(), 0, szAcceptTypes
);
478 if (szReferrer
) HeapFree(GetProcessHeap(), 0, szReferrer
);
479 if (szVersion
) HeapFree(GetProcessHeap(), 0, szVersion
);
480 if (szObjectName
) HeapFree(GetProcessHeap(), 0, szObjectName
);
481 if (szVerb
) HeapFree(GetProcessHeap(), 0, szVerb
);
486 /***********************************************************************
489 static UINT
HTTP_Base64( LPCWSTR bin
, LPWSTR base64
)
492 static LPSTR HTTP_Base64Enc
=
493 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
497 /* first 6 bits, all from bin[0] */
498 base64
[n
++] = HTTP_Base64Enc
[(bin
[0] & 0xfc) >> 2];
499 x
= (bin
[0] & 3) << 4;
501 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
504 base64
[n
++] = HTTP_Base64Enc
[x
];
509 base64
[n
++] = HTTP_Base64Enc
[ x
| ( (bin
[1]&0xf0) >> 4 ) ];
510 x
= ( bin
[1] & 0x0f ) << 2;
512 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
515 base64
[n
++] = HTTP_Base64Enc
[x
];
519 base64
[n
++] = HTTP_Base64Enc
[ x
| ( (bin
[2]&0xc0 ) >> 6 ) ];
521 /* last 6 bits, all from bin [2] */
522 base64
[n
++] = HTTP_Base64Enc
[ bin
[2] & 0x3f ];
529 /***********************************************************************
530 * HTTP_EncodeBasicAuth
532 * Encode the basic authentication string for HTTP 1.1
534 static LPWSTR
HTTP_EncodeBasicAuth( LPCWSTR username
, LPCWSTR password
)
538 static const WCHAR szBasic
[] = {'B','a','s','i','c',' ',0};
539 static const WCHAR szColon
[] = {':',0};
541 len
= lstrlenW( username
) + 1 + lstrlenW ( password
) + 1;
542 in
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
546 len
= lstrlenW(szBasic
) +
547 (lstrlenW( username
) + 1 + lstrlenW ( password
))*2 + 1 + 1;
548 out
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
551 lstrcpyW( in
, username
);
552 lstrcatW( in
, szColon
);
553 lstrcatW( in
, password
);
554 lstrcpyW( out
, szBasic
);
555 HTTP_Base64( in
, &out
[strlenW(out
)] );
557 HeapFree( GetProcessHeap(), 0, in
);
562 /***********************************************************************
563 * HTTP_InsertProxyAuthorization
565 * Insert the basic authorization field in the request header
567 BOOL
HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr
,
568 LPCWSTR username
, LPCWSTR password
)
572 static const WCHAR szProxyAuthorization
[] = {
573 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
575 hdr
.lpszValue
= HTTP_EncodeBasicAuth( username
, password
);
576 hdr
.lpszField
= (WCHAR
*)szProxyAuthorization
;
577 hdr
.wFlags
= HDR_ISREQUEST
;
582 TRACE("Inserting %s = %s\n",
583 debugstr_w( hdr
.lpszField
), debugstr_w( hdr
.lpszValue
) );
585 /* remove the old proxy authorization header */
586 index
= HTTP_GetCustomHeaderIndex( lpwhr
, hdr
.lpszField
);
588 HTTP_DeleteCustomHeader( lpwhr
, index
);
590 HTTP_InsertCustomHeader(lpwhr
, &hdr
);
591 HeapFree( GetProcessHeap(), 0, hdr
.lpszValue
);
596 /***********************************************************************
599 static BOOL
HTTP_DealWithProxy( LPWININETAPPINFOW hIC
,
600 LPWININETHTTPSESSIONW lpwhs
, LPWININETHTTPREQW lpwhr
)
602 WCHAR buf
[MAXHOSTNAME
];
603 WCHAR proxy
[MAXHOSTNAME
+ 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
605 static const WCHAR szNul
[] = { 0 };
606 URL_COMPONENTSW UrlComponents
;
607 static const WCHAR szHttp
[] = { 'h','t','t','p',':','/','/',0 }, szSlash
[] = { '/',0 } ;
608 static const WCHAR szFormat1
[] = { 'h','t','t','p',':','/','/','%','s',0 };
609 static const WCHAR szFormat2
[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
612 memset( &UrlComponents
, 0, sizeof UrlComponents
);
613 UrlComponents
.dwStructSize
= sizeof UrlComponents
;
614 UrlComponents
.lpszHostName
= buf
;
615 UrlComponents
.dwHostNameLength
= MAXHOSTNAME
;
617 if( CSTR_EQUAL
!= CompareStringW(LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
,
618 buf
,strlenW(szHttp
),szHttp
,strlenW(szHttp
)) )
619 sprintfW(proxy
, szFormat1
, hIC
->lpszProxy
);
622 if( !InternetCrackUrlW(proxy
, 0, 0, &UrlComponents
) )
624 if( UrlComponents
.dwHostNameLength
== 0 )
627 if( !lpwhr
->lpszPath
)
628 lpwhr
->lpszPath
= (LPWSTR
)szNul
;
629 TRACE("server='%s' path='%s'\n",
630 debugstr_w(lpwhs
->lpszServerName
), debugstr_w(lpwhr
->lpszPath
));
631 /* for constant 15 see above */
632 len
= strlenW(lpwhs
->lpszServerName
) + strlenW(lpwhr
->lpszPath
) + 15;
633 url
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
635 if(UrlComponents
.nPort
== INTERNET_INVALID_PORT_NUMBER
)
636 UrlComponents
.nPort
= INTERNET_DEFAULT_HTTP_PORT
;
638 sprintfW(url
, szFormat2
, lpwhs
->lpszServerName
, lpwhs
->nServerPort
);
640 if( lpwhr
->lpszPath
[0] != '/' )
641 strcatW( url
, szSlash
);
642 strcatW(url
, lpwhr
->lpszPath
);
643 if(lpwhr
->lpszPath
!= szNul
)
644 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszPath
);
645 lpwhr
->lpszPath
= url
;
646 /* FIXME: Do I have to free lpwhs->lpszServerName here ? */
647 lpwhs
->lpszServerName
= WININET_strdupW(UrlComponents
.lpszHostName
);
648 lpwhs
->nServerPort
= UrlComponents
.nPort
;
653 /***********************************************************************
654 * HTTP_HttpOpenRequestW (internal)
656 * Open a HTTP request handle
659 * HINTERNET a HTTP request handle on success
663 HINTERNET WINAPI
HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs
,
664 LPCWSTR lpszVerb
, LPCWSTR lpszObjectName
, LPCWSTR lpszVersion
,
665 LPCWSTR lpszReferrer
, LPCWSTR
*lpszAcceptTypes
,
666 DWORD dwFlags
, DWORD dwContext
)
668 LPWININETAPPINFOW hIC
= NULL
;
669 LPWININETHTTPREQW lpwhr
;
671 LPWSTR lpszUrl
= NULL
;
673 HINTERNET handle
= NULL
;
674 static const WCHAR szUrlForm
[] = {'h','t','t','p',':','/','/','%','s',0};
679 assert( lpwhs
->hdr
.htype
== WH_HHTTPSESSION
);
680 hIC
= (LPWININETAPPINFOW
) lpwhs
->hdr
.lpwhparent
;
682 lpwhr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WININETHTTPREQW
));
685 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
688 lpwhr
->hdr
.htype
= WH_HHTTPREQ
;
689 lpwhr
->hdr
.lpwhparent
= WININET_AddRef( &lpwhs
->hdr
);
690 lpwhr
->hdr
.dwFlags
= dwFlags
;
691 lpwhr
->hdr
.dwContext
= dwContext
;
692 lpwhr
->hdr
.dwRefCount
= 1;
693 lpwhr
->hdr
.destroy
= HTTP_CloseHTTPRequestHandle
;
695 handle
= WININET_AllocHandle( &lpwhr
->hdr
);
698 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
702 NETCON_init(&lpwhr
->netConnection
, dwFlags
& INTERNET_FLAG_SECURE
);
704 if (NULL
!= lpszObjectName
&& strlenW(lpszObjectName
)) {
708 rc
= UrlEscapeW(lpszObjectName
, NULL
, &len
, URL_ESCAPE_SPACES_ONLY
);
710 len
= strlenW(lpszObjectName
)+1;
711 lpwhr
->lpszPath
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
712 rc
= UrlEscapeW(lpszObjectName
, lpwhr
->lpszPath
, &len
,
713 URL_ESCAPE_SPACES_ONLY
);
716 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(lpszObjectName
),rc
);
717 strcpyW(lpwhr
->lpszPath
,lpszObjectName
);
721 if (NULL
!= lpszReferrer
&& strlenW(lpszReferrer
))
722 HTTP_ProcessHeader(lpwhr
, HTTP_REFERER
, lpszReferrer
, HTTP_ADDHDR_FLAG_COALESCE
);
724 if(lpszAcceptTypes
!=NULL
)
727 for(i
=0;lpszAcceptTypes
[i
]!=NULL
;i
++)
728 HTTP_ProcessHeader(lpwhr
, HTTP_ACCEPT
, lpszAcceptTypes
[i
], HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
|HTTP_ADDHDR_FLAG_REQ
|HTTP_ADDHDR_FLAG_ADD_IF_NEW
);
731 if (NULL
== lpszVerb
)
733 static const WCHAR szGet
[] = {'G','E','T',0};
734 lpwhr
->lpszVerb
= WININET_strdupW(szGet
);
736 else if (strlenW(lpszVerb
))
737 lpwhr
->lpszVerb
= WININET_strdupW(lpszVerb
);
739 if (NULL
!= lpszReferrer
&& strlenW(lpszReferrer
))
741 WCHAR buf
[MAXHOSTNAME
];
742 URL_COMPONENTSW UrlComponents
;
744 memset( &UrlComponents
, 0, sizeof UrlComponents
);
745 UrlComponents
.dwStructSize
= sizeof UrlComponents
;
746 UrlComponents
.lpszHostName
= buf
;
747 UrlComponents
.dwHostNameLength
= MAXHOSTNAME
;
749 InternetCrackUrlW(lpszReferrer
, 0, 0, &UrlComponents
);
750 if (strlenW(UrlComponents
.lpszHostName
))
751 lpwhr
->lpszHostName
= WININET_strdupW(UrlComponents
.lpszHostName
);
753 lpwhr
->lpszHostName
= WININET_strdupW(lpwhs
->lpszServerName
);
755 if (NULL
!= hIC
->lpszProxy
&& hIC
->lpszProxy
[0] != 0)
756 HTTP_DealWithProxy( hIC
, lpwhs
, lpwhr
);
761 static const WCHAR user_agent
[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
763 len
= strlenW(hIC
->lpszAgent
) + strlenW(user_agent
);
764 agent_header
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
765 sprintfW(agent_header
, user_agent
, hIC
->lpszAgent
);
767 HTTP_HttpAddRequestHeadersW(lpwhr
, agent_header
, strlenW(agent_header
),
768 HTTP_ADDREQ_FLAG_ADD
);
769 HeapFree(GetProcessHeap(), 0, agent_header
);
772 len
= strlenW(lpwhr
->lpszHostName
) + strlenW(szUrlForm
);
773 lpszUrl
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
774 sprintfW( lpszUrl
, szUrlForm
, lpwhr
->lpszHostName
);
776 if (!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_COOKIES
) &&
777 InternetGetCookieW(lpszUrl
, NULL
, NULL
, &nCookieSize
))
780 static const WCHAR szCookie
[] = {'C','o','o','k','i','e',':',' ',0};
781 static const WCHAR szcrlf
[] = {'\r','\n',0};
783 lpszCookies
= HeapAlloc(GetProcessHeap(), 0, (nCookieSize
+ 1 + 8)*sizeof(WCHAR
));
785 cnt
+= sprintfW(lpszCookies
, szCookie
);
786 InternetGetCookieW(lpszUrl
, NULL
, lpszCookies
+ cnt
, &nCookieSize
);
787 strcatW(lpszCookies
, szcrlf
);
789 HTTP_HttpAddRequestHeadersW(lpwhr
, lpszCookies
, strlenW(lpszCookies
),
790 HTTP_ADDREQ_FLAG_ADD
);
791 HeapFree(GetProcessHeap(), 0, lpszCookies
);
793 HeapFree(GetProcessHeap(), 0, lpszUrl
);
797 if (hIC
->lpfnStatusCB
)
799 INTERNET_ASYNC_RESULT iar
;
801 iar
.dwResult
= (DWORD
)handle
;
802 iar
.dwError
= ERROR_SUCCESS
;
804 SendAsyncCallback(hIC
, &lpwhs
->hdr
, dwContext
,
805 INTERNET_STATUS_HANDLE_CREATED
, &iar
,
806 sizeof(INTERNET_ASYNC_RESULT
));
810 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
814 * According to my tests. The name is not resolved until a request is Opened
816 SendAsyncCallback(hIC
, &lpwhs
->hdr
, dwContext
,
817 INTERNET_STATUS_RESOLVING_NAME
,
818 lpwhs
->lpszServerName
,
819 strlenW(lpwhs
->lpszServerName
)+1);
820 if (!GetAddress(lpwhs
->lpszServerName
, lpwhs
->nServerPort
,
821 &lpwhs
->phostent
, &lpwhs
->socketAddress
))
823 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED
);
824 InternetCloseHandle( handle
);
829 SendAsyncCallback(hIC
, &lpwhs
->hdr
, lpwhr
->hdr
.dwContext
,
830 INTERNET_STATUS_NAME_RESOLVED
,
831 &(lpwhs
->socketAddress
),
832 sizeof(struct sockaddr_in
));
836 WININET_Release( &lpwhr
->hdr
);
838 TRACE("<-- %p (%p)\n", handle
, lpwhr
);
842 /***********************************************************************
843 * HTTP_HttpQueryInfoW (internal)
845 BOOL WINAPI
HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr
, DWORD dwInfoLevel
,
846 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
848 LPHTTPHEADERW lphttpHdr
= NULL
;
849 BOOL bSuccess
= FALSE
;
851 /* Find requested header structure */
852 if ((dwInfoLevel
& ~HTTP_QUERY_MODIFIER_FLAGS_MASK
) == HTTP_QUERY_CUSTOM
)
854 INT index
= HTTP_GetCustomHeaderIndex(lpwhr
, (LPWSTR
)lpBuffer
);
859 lphttpHdr
= &lpwhr
->pCustHeaders
[index
];
863 INT index
= dwInfoLevel
& ~HTTP_QUERY_MODIFIER_FLAGS_MASK
;
865 if (index
== HTTP_QUERY_RAW_HEADERS_CRLF
)
867 DWORD len
= strlenW(lpwhr
->lpszRawHeaders
);
868 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
870 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
871 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
874 memcpy(lpBuffer
, lpwhr
->lpszRawHeaders
, (len
+1)*sizeof(WCHAR
));
875 *lpdwBufferLength
= len
* sizeof(WCHAR
);
877 TRACE("returning data: %s\n", debugstr_wn((WCHAR
*)lpBuffer
, len
));
881 else if (index
== HTTP_QUERY_RAW_HEADERS
)
883 static const WCHAR szCrLf
[] = {'\r','\n',0};
884 LPWSTR
* ppszRawHeaderLines
= HTTP_Tokenize(lpwhr
->lpszRawHeaders
, szCrLf
);
886 LPWSTR pszString
= (WCHAR
*)lpBuffer
;
888 for (i
= 0; ppszRawHeaderLines
[i
]; i
++)
889 size
+= strlenW(ppszRawHeaderLines
[i
]) + 1;
891 if (size
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
893 HTTP_FreeTokens(ppszRawHeaderLines
);
894 *lpdwBufferLength
= (size
+ 1) * sizeof(WCHAR
);
895 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
899 for (i
= 0; ppszRawHeaderLines
[i
]; i
++)
901 DWORD len
= strlenW(ppszRawHeaderLines
[i
]);
902 memcpy(pszString
, ppszRawHeaderLines
[i
], (len
+1)*sizeof(WCHAR
));
907 TRACE("returning data: %s\n", debugstr_wn((WCHAR
*)lpBuffer
, size
));
909 *lpdwBufferLength
= size
* sizeof(WCHAR
);
910 HTTP_FreeTokens(ppszRawHeaderLines
);
914 else if (index
>= 0 && index
<= HTTP_QUERY_MAX
&& lpwhr
->StdHeaders
[index
].lpszValue
)
916 lphttpHdr
= &lpwhr
->StdHeaders
[index
];
920 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND
);
925 /* Ensure header satisifies requested attributes */
926 if ((dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
) &&
927 (~lphttpHdr
->wFlags
& HDR_ISREQUEST
))
929 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND
);
933 /* coalesce value to reuqested type */
934 if (dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
)
936 *(int *)lpBuffer
= atoiW(lphttpHdr
->lpszValue
);
939 TRACE(" returning number : %d\n", *(int *)lpBuffer
);
941 else if (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
)
947 tmpTime
= ConvertTimeString(lphttpHdr
->lpszValue
);
949 tmpTM
= *gmtime(&tmpTime
);
950 STHook
= (SYSTEMTIME
*) lpBuffer
;
954 STHook
->wDay
= tmpTM
.tm_mday
;
955 STHook
->wHour
= tmpTM
.tm_hour
;
956 STHook
->wMilliseconds
= 0;
957 STHook
->wMinute
= tmpTM
.tm_min
;
958 STHook
->wDayOfWeek
= tmpTM
.tm_wday
;
959 STHook
->wMonth
= tmpTM
.tm_mon
+ 1;
960 STHook
->wSecond
= tmpTM
.tm_sec
;
961 STHook
->wYear
= tmpTM
.tm_year
;
965 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
966 STHook
->wYear
, STHook
->wMonth
, STHook
->wDay
, STHook
->wDayOfWeek
,
967 STHook
->wHour
, STHook
->wMinute
, STHook
->wSecond
, STHook
->wMilliseconds
);
969 else if (dwInfoLevel
& HTTP_QUERY_FLAG_COALESCE
)
971 if (*lpdwIndex
>= lphttpHdr
->wCount
)
973 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND
);
977 /* Copy strncpyW(lpBuffer, lphttpHdr[*lpdwIndex], len); */
983 DWORD len
= (strlenW(lphttpHdr
->lpszValue
) + 1) * sizeof(WCHAR
);
985 if (len
> *lpdwBufferLength
)
987 *lpdwBufferLength
= len
;
988 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
992 memcpy(lpBuffer
, lphttpHdr
->lpszValue
, len
);
993 *lpdwBufferLength
= len
- sizeof(WCHAR
);
996 TRACE(" returning string : '%s'\n", debugstr_w(lpBuffer
));
1001 /***********************************************************************
1002 * HttpQueryInfoW (WININET.@)
1004 * Queries for information about an HTTP request
1011 BOOL WINAPI
HttpQueryInfoW(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
1012 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
1014 BOOL bSuccess
= FALSE
;
1015 LPWININETHTTPREQW lpwhr
;
1017 if (TRACE_ON(wininet
)) {
1018 #define FE(x) { x, #x }
1019 static const wininet_flag_info query_flags
[] = {
1020 FE(HTTP_QUERY_MIME_VERSION
),
1021 FE(HTTP_QUERY_CONTENT_TYPE
),
1022 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING
),
1023 FE(HTTP_QUERY_CONTENT_ID
),
1024 FE(HTTP_QUERY_CONTENT_DESCRIPTION
),
1025 FE(HTTP_QUERY_CONTENT_LENGTH
),
1026 FE(HTTP_QUERY_CONTENT_LANGUAGE
),
1027 FE(HTTP_QUERY_ALLOW
),
1028 FE(HTTP_QUERY_PUBLIC
),
1029 FE(HTTP_QUERY_DATE
),
1030 FE(HTTP_QUERY_EXPIRES
),
1031 FE(HTTP_QUERY_LAST_MODIFIED
),
1032 FE(HTTP_QUERY_MESSAGE_ID
),
1034 FE(HTTP_QUERY_DERIVED_FROM
),
1035 FE(HTTP_QUERY_COST
),
1036 FE(HTTP_QUERY_LINK
),
1037 FE(HTTP_QUERY_PRAGMA
),
1038 FE(HTTP_QUERY_VERSION
),
1039 FE(HTTP_QUERY_STATUS_CODE
),
1040 FE(HTTP_QUERY_STATUS_TEXT
),
1041 FE(HTTP_QUERY_RAW_HEADERS
),
1042 FE(HTTP_QUERY_RAW_HEADERS_CRLF
),
1043 FE(HTTP_QUERY_CONNECTION
),
1044 FE(HTTP_QUERY_ACCEPT
),
1045 FE(HTTP_QUERY_ACCEPT_CHARSET
),
1046 FE(HTTP_QUERY_ACCEPT_ENCODING
),
1047 FE(HTTP_QUERY_ACCEPT_LANGUAGE
),
1048 FE(HTTP_QUERY_AUTHORIZATION
),
1049 FE(HTTP_QUERY_CONTENT_ENCODING
),
1050 FE(HTTP_QUERY_FORWARDED
),
1051 FE(HTTP_QUERY_FROM
),
1052 FE(HTTP_QUERY_IF_MODIFIED_SINCE
),
1053 FE(HTTP_QUERY_LOCATION
),
1054 FE(HTTP_QUERY_ORIG_URI
),
1055 FE(HTTP_QUERY_REFERER
),
1056 FE(HTTP_QUERY_RETRY_AFTER
),
1057 FE(HTTP_QUERY_SERVER
),
1058 FE(HTTP_QUERY_TITLE
),
1059 FE(HTTP_QUERY_USER_AGENT
),
1060 FE(HTTP_QUERY_WWW_AUTHENTICATE
),
1061 FE(HTTP_QUERY_PROXY_AUTHENTICATE
),
1062 FE(HTTP_QUERY_ACCEPT_RANGES
),
1063 FE(HTTP_QUERY_SET_COOKIE
),
1064 FE(HTTP_QUERY_COOKIE
),
1065 FE(HTTP_QUERY_REQUEST_METHOD
),
1066 FE(HTTP_QUERY_REFRESH
),
1067 FE(HTTP_QUERY_CONTENT_DISPOSITION
),
1069 FE(HTTP_QUERY_CACHE_CONTROL
),
1070 FE(HTTP_QUERY_CONTENT_BASE
),
1071 FE(HTTP_QUERY_CONTENT_LOCATION
),
1072 FE(HTTP_QUERY_CONTENT_MD5
),
1073 FE(HTTP_QUERY_CONTENT_RANGE
),
1074 FE(HTTP_QUERY_ETAG
),
1075 FE(HTTP_QUERY_HOST
),
1076 FE(HTTP_QUERY_IF_MATCH
),
1077 FE(HTTP_QUERY_IF_NONE_MATCH
),
1078 FE(HTTP_QUERY_IF_RANGE
),
1079 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE
),
1080 FE(HTTP_QUERY_MAX_FORWARDS
),
1081 FE(HTTP_QUERY_PROXY_AUTHORIZATION
),
1082 FE(HTTP_QUERY_RANGE
),
1083 FE(HTTP_QUERY_TRANSFER_ENCODING
),
1084 FE(HTTP_QUERY_UPGRADE
),
1085 FE(HTTP_QUERY_VARY
),
1087 FE(HTTP_QUERY_WARNING
),
1088 FE(HTTP_QUERY_CUSTOM
)
1090 static const wininet_flag_info modifier_flags
[] = {
1091 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS
),
1092 FE(HTTP_QUERY_FLAG_SYSTEMTIME
),
1093 FE(HTTP_QUERY_FLAG_NUMBER
),
1094 FE(HTTP_QUERY_FLAG_COALESCE
)
1097 DWORD info_mod
= dwInfoLevel
& HTTP_QUERY_MODIFIER_FLAGS_MASK
;
1098 DWORD info
= dwInfoLevel
& HTTP_QUERY_HEADER_MASK
;
1101 TRACE("(%p, 0x%08lx)--> %ld\n", hHttpRequest
, dwInfoLevel
, dwInfoLevel
);
1102 TRACE(" Attribute:");
1103 for (i
= 0; i
< (sizeof(query_flags
) / sizeof(query_flags
[0])); i
++) {
1104 if (query_flags
[i
].val
== info
) {
1105 DPRINTF(" %s", query_flags
[i
].name
);
1109 if (i
== (sizeof(query_flags
) / sizeof(query_flags
[0]))) {
1110 DPRINTF(" Unknown (%08lx)", info
);
1113 DPRINTF(" Modifier:");
1114 for (i
= 0; i
< (sizeof(modifier_flags
) / sizeof(modifier_flags
[0])); i
++) {
1115 if (modifier_flags
[i
].val
& info_mod
) {
1116 DPRINTF(" %s", modifier_flags
[i
].name
);
1117 info_mod
&= ~ modifier_flags
[i
].val
;
1122 DPRINTF(" Unknown (%08lx)", info_mod
);
1127 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hHttpRequest
);
1128 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
1130 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1134 bSuccess
= HTTP_HttpQueryInfoW( lpwhr
, dwInfoLevel
,
1135 lpBuffer
, lpdwBufferLength
, lpdwIndex
);
1139 WININET_Release( &lpwhr
->hdr
);
1141 TRACE("%d <--\n", bSuccess
);
1145 /***********************************************************************
1146 * HttpQueryInfoA (WININET.@)
1148 * Queries for information about an HTTP request
1155 BOOL WINAPI
HttpQueryInfoA(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
1156 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
1162 if((dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
) ||
1163 (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
))
1165 return HttpQueryInfoW( hHttpRequest
, dwInfoLevel
, lpBuffer
,
1166 lpdwBufferLength
, lpdwIndex
);
1169 len
= (*lpdwBufferLength
)*sizeof(WCHAR
);
1170 bufferW
= HeapAlloc( GetProcessHeap(), 0, len
);
1171 result
= HttpQueryInfoW( hHttpRequest
, dwInfoLevel
, bufferW
,
1175 len
= WideCharToMultiByte( CP_ACP
,0, bufferW
, len
/ sizeof(WCHAR
) + 1,
1176 lpBuffer
, *lpdwBufferLength
, NULL
, NULL
);
1177 *lpdwBufferLength
= len
- 1;
1179 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer
));
1182 /* since the strings being returned from HttpQueryInfoW should be
1183 * only ASCII characters, it is reasonable to assume that all of
1184 * the Unicode characters can be reduced to a single byte */
1185 *lpdwBufferLength
= len
/ sizeof(WCHAR
);
1187 HeapFree(GetProcessHeap(), 0, bufferW
);
1192 /***********************************************************************
1193 * HttpSendRequestExA (WININET.@)
1195 * Sends the specified request to the HTTP server and allows chunked
1198 BOOL WINAPI
HttpSendRequestExA(HINTERNET hRequest
,
1199 LPINTERNET_BUFFERSA lpBuffersIn
,
1200 LPINTERNET_BUFFERSA lpBuffersOut
,
1201 DWORD dwFlags
, DWORD dwContext
)
1203 FIXME("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest
, lpBuffersIn
,
1204 lpBuffersOut
, dwFlags
, dwContext
);
1208 /***********************************************************************
1209 * HttpSendRequestW (WININET.@)
1211 * Sends the specified request to the HTTP server
1218 BOOL WINAPI
HttpSendRequestW(HINTERNET hHttpRequest
, LPCWSTR lpszHeaders
,
1219 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
1221 LPWININETHTTPREQW lpwhr
;
1222 LPWININETHTTPSESSIONW lpwhs
= NULL
;
1223 LPWININETAPPINFOW hIC
= NULL
;
1226 TRACE("%p, %p (%s), %li, %p, %li)\n", hHttpRequest
,
1227 lpszHeaders
, debugstr_w(lpszHeaders
), dwHeaderLength
, lpOptional
, dwOptionalLength
);
1229 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hHttpRequest
);
1230 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
1232 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1237 lpwhs
= (LPWININETHTTPSESSIONW
) lpwhr
->hdr
.lpwhparent
;
1238 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
1240 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1245 hIC
= (LPWININETAPPINFOW
) lpwhs
->hdr
.lpwhparent
;
1246 if (NULL
== hIC
|| hIC
->hdr
.htype
!= WH_HINIT
)
1248 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1253 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1255 WORKREQUEST workRequest
;
1256 struct WORKREQ_HTTPSENDREQUESTW
*req
;
1258 workRequest
.asyncall
= HTTPSENDREQUESTW
;
1259 workRequest
.hdr
= WININET_AddRef( &lpwhr
->hdr
);
1260 req
= &workRequest
.u
.HttpSendRequestW
;
1262 req
->lpszHeader
= WININET_strdupW(lpszHeaders
);
1264 req
->lpszHeader
= 0;
1265 req
->dwHeaderLength
= dwHeaderLength
;
1266 req
->lpOptional
= lpOptional
;
1267 req
->dwOptionalLength
= dwOptionalLength
;
1269 INTERNET_AsyncCall(&workRequest
);
1271 * This is from windows.
1273 SetLastError(ERROR_IO_PENDING
);
1278 r
= HTTP_HttpSendRequestW(lpwhr
, lpszHeaders
,
1279 dwHeaderLength
, lpOptional
, dwOptionalLength
);
1283 WININET_Release( &lpwhr
->hdr
);
1287 /***********************************************************************
1288 * HttpSendRequestA (WININET.@)
1290 * Sends the specified request to the HTTP server
1297 BOOL WINAPI
HttpSendRequestA(HINTERNET hHttpRequest
, LPCSTR lpszHeaders
,
1298 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
1301 LPWSTR szHeaders
=NULL
;
1302 DWORD nLen
=dwHeaderLength
;
1303 if(lpszHeaders
!=NULL
)
1305 nLen
=MultiByteToWideChar(CP_ACP
,0,lpszHeaders
,dwHeaderLength
,NULL
,0);
1306 szHeaders
=HeapAlloc(GetProcessHeap(),0,nLen
*sizeof(WCHAR
));
1307 MultiByteToWideChar(CP_ACP
,0,lpszHeaders
,dwHeaderLength
,szHeaders
,nLen
);
1309 result
=HttpSendRequestW(hHttpRequest
, szHeaders
, nLen
, lpOptional
, dwOptionalLength
);
1311 HeapFree(GetProcessHeap(),0,szHeaders
);
1315 /***********************************************************************
1316 * HTTP_HandleRedirect (internal)
1318 static BOOL
HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszUrl
, LPCWSTR lpszHeaders
,
1319 DWORD dwHeaderLength
, LPVOID lpOptional
, DWORD dwOptionalLength
)
1321 LPWININETHTTPSESSIONW lpwhs
= (LPWININETHTTPSESSIONW
) lpwhr
->hdr
.lpwhparent
;
1322 LPWININETAPPINFOW hIC
= (LPWININETAPPINFOW
) lpwhs
->hdr
.lpwhparent
;
1327 /* if it's an absolute path, keep the same session info */
1328 strcpyW(path
,lpszUrl
);
1330 else if (NULL
!= hIC
->lpszProxy
&& hIC
->lpszProxy
[0] != 0)
1332 TRACE("Redirect through proxy\n");
1333 strcpyW(path
,lpszUrl
);
1337 URL_COMPONENTSW urlComponents
;
1338 WCHAR protocol
[32], hostName
[MAXHOSTNAME
], userName
[1024];
1339 WCHAR password
[1024], extra
[1024];
1340 urlComponents
.dwStructSize
= sizeof(URL_COMPONENTSW
);
1341 urlComponents
.lpszScheme
= protocol
;
1342 urlComponents
.dwSchemeLength
= 32;
1343 urlComponents
.lpszHostName
= hostName
;
1344 urlComponents
.dwHostNameLength
= MAXHOSTNAME
;
1345 urlComponents
.lpszUserName
= userName
;
1346 urlComponents
.dwUserNameLength
= 1024;
1347 urlComponents
.lpszPassword
= password
;
1348 urlComponents
.dwPasswordLength
= 1024;
1349 urlComponents
.lpszUrlPath
= path
;
1350 urlComponents
.dwUrlPathLength
= 2048;
1351 urlComponents
.lpszExtraInfo
= extra
;
1352 urlComponents
.dwExtraInfoLength
= 1024;
1353 if(!InternetCrackUrlW(lpszUrl
, strlenW(lpszUrl
), 0, &urlComponents
))
1356 if (urlComponents
.nPort
== INTERNET_INVALID_PORT_NUMBER
)
1357 urlComponents
.nPort
= INTERNET_DEFAULT_HTTP_PORT
;
1361 * This upsets redirects to binary files on sourceforge.net
1362 * and gives an html page instead of the target file
1363 * Examination of the HTTP request sent by native wininet.dll
1364 * reveals that it doesn't send a referrer in that case.
1365 * Maybe there's a flag that enables this, or maybe a referrer
1366 * shouldn't be added in case of a redirect.
1369 /* consider the current host as the referrer */
1370 if (NULL
!= lpwhs
->lpszServerName
&& strlenW(lpwhs
->lpszServerName
))
1371 HTTP_ProcessHeader(lpwhr
, HTTP_REFERER
, lpwhs
->lpszServerName
,
1372 HTTP_ADDHDR_FLAG_REQ
|HTTP_ADDREQ_FLAG_REPLACE
|
1373 HTTP_ADDHDR_FLAG_ADD_IF_NEW
);
1376 if (NULL
!= lpwhs
->lpszServerName
)
1377 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszServerName
);
1378 lpwhs
->lpszServerName
= WININET_strdupW(hostName
);
1379 if (NULL
!= lpwhs
->lpszUserName
)
1380 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszUserName
);
1381 lpwhs
->lpszUserName
= WININET_strdupW(userName
);
1382 lpwhs
->nServerPort
= urlComponents
.nPort
;
1384 if (NULL
!= lpwhr
->lpszHostName
)
1385 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszHostName
);
1386 lpwhr
->lpszHostName
=WININET_strdupW(hostName
);
1388 SendAsyncCallback(hIC
, &lpwhs
->hdr
, lpwhr
->hdr
.dwContext
,
1389 INTERNET_STATUS_RESOLVING_NAME
,
1390 lpwhs
->lpszServerName
,
1391 strlenW(lpwhs
->lpszServerName
)+1);
1393 if (!GetAddress(lpwhs
->lpszServerName
, lpwhs
->nServerPort
,
1394 &lpwhs
->phostent
, &lpwhs
->socketAddress
))
1396 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED
);
1400 SendAsyncCallback(hIC
, &lpwhs
->hdr
, lpwhr
->hdr
.dwContext
,
1401 INTERNET_STATUS_NAME_RESOLVED
,
1402 &(lpwhs
->socketAddress
),
1403 sizeof(struct sockaddr_in
));
1408 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszPath
);
1409 lpwhr
->lpszPath
=NULL
;
1415 rc
= UrlEscapeW(path
, NULL
, &needed
, URL_ESCAPE_SPACES_ONLY
);
1416 if (rc
!= E_POINTER
)
1417 needed
= strlenW(path
)+1;
1418 lpwhr
->lpszPath
= HeapAlloc(GetProcessHeap(), 0, needed
*sizeof(WCHAR
));
1419 rc
= UrlEscapeW(path
, lpwhr
->lpszPath
, &needed
,
1420 URL_ESCAPE_SPACES_ONLY
);
1423 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(path
),rc
);
1424 strcpyW(lpwhr
->lpszPath
,path
);
1428 return HTTP_HttpSendRequestW(lpwhr
, lpszHeaders
, dwHeaderLength
, lpOptional
, dwOptionalLength
);
1431 /***********************************************************************
1432 * HTTP_build_req (internal)
1434 * concatenate all the strings in the request together
1436 static LPWSTR
HTTP_build_req( LPCWSTR
*list
, int len
)
1441 for( t
= list
; *t
; t
++ )
1442 len
+= strlenW( *t
);
1445 str
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
1448 for( t
= list
; *t
; t
++ )
1454 /***********************************************************************
1455 * HTTP_HttpSendRequestW (internal)
1457 * Sends the specified request to the HTTP server
1464 BOOL WINAPI
HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszHeaders
,
1465 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
1469 BOOL bSuccess
= FALSE
;
1470 LPWSTR requestString
= NULL
;
1472 LPWININETHTTPSESSIONW lpwhs
= NULL
;
1473 LPWININETAPPINFOW hIC
= NULL
;
1474 BOOL loop_next
= FALSE
;
1475 int CustHeaderIndex
;
1477 TRACE("--> %p\n", lpwhr
);
1479 assert(lpwhr
->hdr
.htype
== WH_HHTTPREQ
);
1481 lpwhs
= (LPWININETHTTPSESSIONW
) lpwhr
->hdr
.lpwhparent
;
1482 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
1484 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1488 hIC
= (LPWININETAPPINFOW
) lpwhs
->hdr
.lpwhparent
;
1489 if (NULL
== hIC
|| hIC
->hdr
.htype
!= WH_HINIT
)
1491 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1495 /* Clear any error information */
1496 INTERNET_SetLastError(0);
1499 /* We must have a verb */
1500 if (NULL
== lpwhr
->lpszVerb
)
1505 /* if we are using optional stuff, we must add the fixed header of that option length */
1506 if (lpOptional
&& dwOptionalLength
)
1508 static const WCHAR szContentLength
[] = {
1509 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0};
1510 WCHAR contentLengthStr
[sizeof szContentLength
/2 /* includes \n\r */ + 20 /* int */ ];
1511 sprintfW(contentLengthStr
, szContentLength
, dwOptionalLength
);
1512 HTTP_HttpAddRequestHeadersW(lpwhr
, contentLengthStr
, -1L, HTTP_ADDREQ_FLAG_ADD
);
1517 static const WCHAR szSlash
[] = { '/',0 };
1518 static const WCHAR szSpace
[] = { ' ',0 };
1519 static const WCHAR szHttp
[] = { 'h','t','t','p',':','/','/', 0 };
1520 static const WCHAR szcrlf
[] = {'\r','\n', 0};
1521 static const WCHAR sztwocrlf
[] = {'\r','\n','\r','\n', 0};
1522 static const WCHAR szSetCookie
[] = {'S','e','t','-','C','o','o','k','i','e',0 };
1523 static const WCHAR szColon
[] = { ':',' ',0 };
1529 TRACE("Going to url %s %s\n", debugstr_w(lpwhr
->lpszHostName
), debugstr_w(lpwhr
->lpszPath
));
1532 /* If we don't have a path we set it to root */
1533 if (NULL
== lpwhr
->lpszPath
)
1534 lpwhr
->lpszPath
= WININET_strdupW(szSlash
);
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')))
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
));
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 */
1570 req
[n
++] = lpwhr
->lpszVerb
;
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
)
1581 req
[n
++] = lpwhr
->StdHeaders
[i
].lpszField
;
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
)
1597 req
[n
++] = lpwhr
->pCustHeaders
[i
].lpszField
;
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
;
1614 ERR("oops. buffer overrun\n");
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') )
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
))
1635 /* send the request as ASCII, tack on the optional data */
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
);
1644 memcpy( &ascii_req
[len
-1], lpOptional
, dwOptionalLength
);
1645 len
= (len
+ dwOptionalLength
- 1);
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);
1665 responseLen
= HTTP_GetResponseHeaders(lpwhr
);
1669 SendAsyncCallback(hIC
, &lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
1670 INTERNET_STATUS_RESPONSE_RECEIVED
, &responseLen
,
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
;
1687 LPWSTR domain
= NULL
;
1689 while (setCookieHeader
->lpszValue
[nPosEnd
] != ';' && setCookieHeader
->lpszValue
[nPosEnd
] != ',' &&
1690 setCookieHeader
->lpszValue
[nPosEnd
] != '\0')
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
);
1702 { /* they have specified their own domain, lets use it */
1703 while (lpszDomain
[nDomainPosEnd
] != ';' && lpszDomain
[nDomainPosEnd
] != ',' &&
1704 lpszDomain
[nDomainPosEnd
] != '\0')
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')
1724 if (buf_cookie
[nEqualPos
] == '\0' || buf_cookie
[nEqualPos
+ 1] == '\0')
1726 HeapFree(GetProcessHeap(), 0, buf_cookie
);
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
;
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;
1767 if(HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_LOCATION
,szNewLocation
,&dwBufferSize
,&dwIndex
))
1769 SendAsyncCallback(hIC
, &lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
1770 INTERNET_STATUS_REDIRECT
, szNewLocation
,
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
));
1795 /***********************************************************************
1796 * HTTP_Connect (internal)
1798 * Create http session handle
1801 * HINTERNET a session handle on success
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
;
1816 assert( hIC
->hdr
.htype
== WH_HINIT
);
1818 hIC
->hdr
.dwContext
= dwContext
;
1820 lpwhs
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WININETHTTPSESSIONW
));
1823 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
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
);
1845 ERR("Failed to alloc handle\n");
1846 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
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
));
1879 WININET_Release( &lpwhs
->hdr
);
1882 * a INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
1886 TRACE("%p --> %p (%p)\n", hIC
, handle
, lpwhs
);
1891 /***********************************************************************
1892 * HTTP_OpenConnection (internal)
1894 * Connect to a web server
1901 BOOL
HTTP_OpenConnection(LPWININETHTTPREQW lpwhr
)
1903 BOOL bSuccess
= FALSE
;
1904 LPWININETHTTPSESSIONW lpwhs
;
1905 LPWININETAPPINFOW hIC
= NULL
;
1910 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
1912 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
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
,
1927 WARN("Socket creation failed\n");
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
));
1938 SendAsyncCallback(hIC
, &lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
1939 INTERNET_STATUS_CONNECTED_TO_SERVER
,
1940 &(lpwhs
->socketAddress
),
1941 sizeof(struct sockaddr_in
));
1946 TRACE("%d <--\n", bSuccess
);
1951 /***********************************************************************
1952 * HTTP_clear_response_headers (internal)
1954 * clear out any old response headers
1956 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr
)
1960 for( i
=0; i
<=HTTP_QUERY_MAX
; i
++ )
1962 if( !lpwhr
->StdHeaders
[i
].lpszField
)
1964 if( !lpwhr
->StdHeaders
[i
].lpszValue
)
1966 if ( lpwhr
->StdHeaders
[i
].wFlags
& HDR_ISREQUEST
)
1968 HTTP_ReplaceHeaderValue( &lpwhr
->StdHeaders
[i
], NULL
);
1970 for( i
=0; i
<lpwhr
->nCustHeaders
; i
++)
1972 if( !lpwhr
->pCustHeaders
[i
].lpszField
)
1974 if( !lpwhr
->pCustHeaders
[i
].lpszValue
)
1976 if ( lpwhr
->pCustHeaders
[i
].wFlags
& HDR_ISREQUEST
)
1978 HTTP_ReplaceHeaderValue( &lpwhr
->pCustHeaders
[i
], NULL
);
1982 /***********************************************************************
1983 * HTTP_GetResponseHeaders (internal)
1985 * Read server response
1992 BOOL
HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr
)
1995 WCHAR buffer
[MAX_REPLY_LEN
];
1996 DWORD buflen
= MAX_REPLY_LEN
;
1997 BOOL bSuccess
= FALSE
;
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;
2010 /* clear old response headers (eg. from a redirect response) */
2011 HTTP_clear_response_headers( lpwhr
);
2013 if (!NETCON_connected(&lpwhr
->netConnection
))
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
))
2028 MultiByteToWideChar( CP_ACP
, 0, bufferA
, buflen
, buffer
, MAX_REPLY_LEN
);
2030 if (strncmpW(buffer
, szHttp
, 4) != 0)
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
, ' ' );
2051 /* split the status code from the status text */
2052 status_text
= strchrW( status_code
, ' ' );
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
))
2086 HTTP_ProcessHeader(lpwhr
, field
, value
, (HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
));
2096 if (lpwhr
->lpszRawHeaders
) HeapFree(GetProcessHeap(), 0, lpszRawHeaders
);
2097 lpwhr
->lpszRawHeaders
= lpszRawHeaders
;
2098 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders
));
2111 /***********************************************************************
2112 * HTTP_InterpretHttpHeader (internal)
2114 * Parse server response
2121 static INT
stripSpaces(LPCWSTR lpszSrc
, LPWSTR lpszStart
, INT
*len
)
2128 while (*lpszSrc
== ' ' && *lpszSrc
!= '\0')
2132 while(*lpsztmp
!= '\0')
2134 if (*lpsztmp
!= ' ')
2135 srclen
= lpsztmp
- lpszSrc
+ 1;
2140 *len
= min(*len
, srclen
);
2141 strncpyW(lpszStart
, lpszSrc
, *len
);
2142 lpszStart
[*len
] = '\0';
2148 BOOL
HTTP_InterpretHttpHeader(LPWSTR buffer
, LPWSTR field
, INT fieldlen
, LPWSTR value
, INT valuelen
)
2151 BOOL bSuccess
= FALSE
;
2158 pd
= strchrW(buffer
, ':');
2162 if (stripSpaces(buffer
, field
, &fieldlen
) > 0)
2164 if (stripSpaces(pd
+1, value
, &valuelen
) > 0)
2169 TRACE("%d: field(%s) Value(%s)\n", bSuccess
, debugstr_w(field
), debugstr_w(value
));
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
)
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
;
2271 TRACE("Couldn't find %s in standard header table\n", debugstr_w(lpszField
));
2277 /***********************************************************************
2278 * HTTP_ReplaceHeaderValue (internal)
2280 BOOL
HTTP_ReplaceHeaderValue( LPHTTPHEADERW lphttpHdr
, LPCWSTR value
)
2284 if( lphttpHdr
->lpszValue
)
2285 HeapFree( GetProcessHeap(), 0, lphttpHdr
->lpszValue
);
2286 lphttpHdr
->lpszValue
= NULL
;
2289 len
= strlenW(value
);
2292 lphttpHdr
->lpszValue
= HeapAlloc(GetProcessHeap(), 0,
2293 (len
+1)*sizeof(WCHAR
));
2294 strcpyW(lphttpHdr
->lpszValue
, value
);
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
;
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
);
2324 lphttpHdr
= &lpwhr
->StdHeaders
[index
];
2326 else /* Find or create new custom header */
2328 index
= HTTP_GetCustomHeaderIndex(lpwhr
, field
);
2331 if (dwModifier
& HTTP_ADDHDR_FLAG_ADD_IF_NEW
)
2335 lphttpHdr
= &lpwhr
->pCustHeaders
[index
];
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
;
2355 lphttpHdr
->wFlags
&= ~HDR_ISREQUEST
;
2357 if (!lphttpHdr
->lpszValue
&& (dwModifier
& (HTTP_ADDHDR_FLAG_ADD
|HTTP_ADDHDR_FLAG_ADD_IF_NEW
)))
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
);
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
)
2390 INT origlen
= strlenW(lphttpHdr
->lpszValue
);
2391 INT valuelen
= strlenW(value
);
2393 if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
)
2396 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
2398 else if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON
)
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
));
2409 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
2412 lphttpHdr
->lpszValue
[origlen
] = ch
;
2416 memcpy(&lphttpHdr
->lpszValue
[origlen
], value
, valuelen
*sizeof(WCHAR
));
2417 lphttpHdr
->lpszValue
[len
] = '\0';
2422 WARN("HeapReAlloc (%d bytes) failed\n",len
+1);
2423 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2427 TRACE("<-- %d\n",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
)
2470 LPWININETHTTPREQW lpwhr
= (LPWININETHTTPREQW
) hdr
;
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
)
2537 TRACE("%s\n", debugstr_w(lpszField
));
2539 for (index
= 0; index
< lpwhr
->nCustHeaders
; index
++)
2541 if (!strcmpiW(lpwhr
->pCustHeaders
[index
].lpszField
, lpszField
))
2546 if (index
>= lpwhr
->nCustHeaders
)
2549 TRACE("Return: %lu\n", index
);
2554 /***********************************************************************
2555 * HTTP_InsertCustomHeader (internal)
2557 * Insert header into array
2560 BOOL
HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr
, LPHTTPHEADERW lpHdr
)
2563 LPHTTPHEADERW lph
= NULL
;
2566 TRACE("--> %s: %s\n", debugstr_w(lpHdr
->lpszField
), debugstr_w(lpHdr
->lpszValue
));
2567 count
= lpwhr
->nCustHeaders
+ 1;
2569 lph
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, lpwhr
->pCustHeaders
, sizeof(HTTPHEADERW
) * count
);
2571 lph
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(HTTPHEADERW
) * count
);
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
++;
2585 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
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 )
2602 if( lpwhr
->nCustHeaders
>= index
)
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
) );
2613 /***********************************************************************
2614 * IsHostInProxyBypassList (@)
2619 BOOL WINAPI
IsHostInProxyBypassList(DWORD flags
, LPCSTR szHost
, DWORD length
)
2621 FIXME("STUB: flags=%ld host=%s length=%ld\n",flags
,szHost
,length
);