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
8 * Copyright 2005 Aric Stewart for CodeWeavers
9 * Copyright 2006 Robert Shearman for CodeWeavers
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/port.h"
32 #include <sys/types.h>
33 #ifdef HAVE_SYS_SOCKET_H
34 # include <sys/socket.h>
36 #ifdef HAVE_ARPA_INET_H
37 # include <arpa/inet.h>
53 #define NO_SHLWAPI_STREAM
54 #define NO_SHLWAPI_REG
55 #define NO_SHLWAPI_STRFCNS
56 #define NO_SHLWAPI_GDI
60 #include "wine/debug.h"
61 #include "wine/unicode.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
65 static const WCHAR g_szHttp1_0
[] = {' ','H','T','T','P','/','1','.','0',0 };
66 static const WCHAR g_szHttp1_1
[] = {' ','H','T','T','P','/','1','.','1',0 };
67 static const WCHAR g_szReferer
[] = {'R','e','f','e','r','e','r',0};
68 static const WCHAR g_szAccept
[] = {'A','c','c','e','p','t',0};
69 static const WCHAR g_szUserAgent
[] = {'U','s','e','r','-','A','g','e','n','t',0};
70 static const WCHAR szHost
[] = { 'H','o','s','t',0 };
71 static const WCHAR szProxy_Authorization
[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
72 static const WCHAR szStatus
[] = { 'S','t','a','t','u','s',0 };
73 static const WCHAR szKeepAlive
[] = {'K','e','e','p','-','A','l','i','v','e',0};
75 #define MAXHOSTNAME 100
76 #define MAX_FIELD_VALUE_LEN 256
77 #define MAX_FIELD_LEN 256
79 #define HTTP_REFERER g_szReferer
80 #define HTTP_ACCEPT g_szAccept
81 #define HTTP_USERAGENT g_szUserAgent
83 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
84 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
85 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
86 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
87 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
88 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
89 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
92 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr
);
93 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr
);
94 static BOOL
HTTP_OpenConnection(LPWININETHTTPREQW lpwhr
);
95 static BOOL
HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr
);
96 static BOOL
HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr
, LPCWSTR field
, LPCWSTR value
, DWORD dwModifier
);
97 static LPWSTR
* HTTP_InterpretHttpHeader(LPCWSTR buffer
);
98 static BOOL
HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr
, LPHTTPHEADERW lpHdr
);
99 static INT
HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszField
, INT index
, BOOL Request
);
100 static BOOL
HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr
, DWORD index
);
101 static LPWSTR
HTTP_build_req( LPCWSTR
*list
, int len
);
102 static BOOL
HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr
,
103 LPCWSTR username
, LPCWSTR password
);
104 static BOOL WINAPI
HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr
, DWORD
105 dwInfoLevel
, LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD
107 static BOOL
HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszUrl
);
110 LPHTTPHEADERW
HTTP_GetHeader(LPWININETHTTPREQW req
, LPCWSTR head
)
113 HeaderIndex
= HTTP_GetCustomHeaderIndex(req
, head
, 0, TRUE
);
114 if (HeaderIndex
== -1)
117 return &req
->pCustHeaders
[HeaderIndex
];
120 /***********************************************************************
121 * HTTP_Tokenize (internal)
123 * Tokenize a string, allocating memory for the tokens.
125 static LPWSTR
* HTTP_Tokenize(LPCWSTR string
, LPCWSTR token_string
)
127 LPWSTR
* token_array
;
132 /* empty string has no tokens */
136 for (i
= 0; string
[i
]; i
++)
137 if (!strncmpW(string
+i
, token_string
, strlenW(token_string
)))
141 /* we want to skip over separators, but not the null terminator */
142 for (j
= 0; j
< strlenW(token_string
) - 1; j
++)
148 /* add 1 for terminating NULL */
149 token_array
= HeapAlloc(GetProcessHeap(), 0, (tokens
+1) * sizeof(*token_array
));
150 token_array
[tokens
] = NULL
;
153 for (i
= 0; i
< tokens
; i
++)
156 next_token
= strstrW(string
, token_string
);
157 if (!next_token
) next_token
= string
+strlenW(string
);
158 len
= next_token
- string
;
159 token_array
[i
] = HeapAlloc(GetProcessHeap(), 0, (len
+1)*sizeof(WCHAR
));
160 memcpy(token_array
[i
], string
, len
*sizeof(WCHAR
));
161 token_array
[i
][len
] = '\0';
162 string
= next_token
+strlenW(token_string
);
167 /***********************************************************************
168 * HTTP_FreeTokens (internal)
170 * Frees memory returned from HTTP_Tokenize.
172 static void HTTP_FreeTokens(LPWSTR
* token_array
)
175 for (i
= 0; token_array
[i
]; i
++)
176 HeapFree(GetProcessHeap(), 0, token_array
[i
]);
177 HeapFree(GetProcessHeap(), 0, token_array
);
180 /* **********************************************************************
182 * Helper functions for the HttpSendRequest(Ex) functions
185 static void AsyncHttpSendRequestProc(WORKREQUEST
*workRequest
)
187 struct WORKREQ_HTTPSENDREQUESTW
const *req
= &workRequest
->u
.HttpSendRequestW
;
188 LPWININETHTTPREQW lpwhr
= (LPWININETHTTPREQW
) workRequest
->hdr
;
190 TRACE("%p\n", lpwhr
);
192 HTTP_HttpSendRequestW(lpwhr
, req
->lpszHeader
,
193 req
->dwHeaderLength
, req
->lpOptional
, req
->dwOptionalLength
,
194 req
->dwContentLength
, req
->bEndRequest
);
196 HeapFree(GetProcessHeap(), 0, req
->lpszHeader
);
199 static void HTTP_FixVerb( LPWININETHTTPREQW lpwhr
)
201 /* if the verb is NULL default to GET */
202 if (NULL
== lpwhr
->lpszVerb
)
204 static const WCHAR szGET
[] = { 'G','E','T', 0 };
205 lpwhr
->lpszVerb
= WININET_strdupW(szGET
);
209 static void HTTP_FixURL( LPWININETHTTPREQW lpwhr
)
211 static const WCHAR szSlash
[] = { '/',0 };
212 static const WCHAR szHttp
[] = { 'h','t','t','p',':','/','/', 0 };
214 /* If we don't have a path we set it to root */
215 if (NULL
== lpwhr
->lpszPath
)
216 lpwhr
->lpszPath
= WININET_strdupW(szSlash
);
217 else /* remove \r and \n*/
219 int nLen
= strlenW(lpwhr
->lpszPath
);
220 while ((nLen
>0 ) && ((lpwhr
->lpszPath
[nLen
-1] == '\r')||(lpwhr
->lpszPath
[nLen
-1] == '\n')))
223 lpwhr
->lpszPath
[nLen
]='\0';
225 /* Replace '\' with '/' */
228 if (lpwhr
->lpszPath
[nLen
] == '\\') lpwhr
->lpszPath
[nLen
]='/';
232 if(CSTR_EQUAL
!= CompareStringW( LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
,
233 lpwhr
->lpszPath
, strlenW(szHttp
), szHttp
, strlenW(szHttp
) )
234 && lpwhr
->lpszPath
[0] != '/') /* not an absolute path ?? --> fix it !! */
236 WCHAR
*fixurl
= HeapAlloc(GetProcessHeap(), 0,
237 (strlenW(lpwhr
->lpszPath
) + 2)*sizeof(WCHAR
));
239 strcpyW(fixurl
+ 1, lpwhr
->lpszPath
);
240 HeapFree( GetProcessHeap(), 0, lpwhr
->lpszPath
);
241 lpwhr
->lpszPath
= fixurl
;
245 static LPWSTR
HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr
, LPCWSTR verb
, LPCWSTR path
, BOOL http1_1
)
247 LPWSTR requestString
;
253 static const WCHAR szSpace
[] = { ' ',0 };
254 static const WCHAR szcrlf
[] = {'\r','\n', 0};
255 static const WCHAR szColon
[] = { ':',' ',0 };
256 static const WCHAR sztwocrlf
[] = {'\r','\n','\r','\n', 0};
258 /* allocate space for an array of all the string pointers to be added */
259 len
= (lpwhr
->nCustHeaders
)*4 + 9;
260 req
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(LPCWSTR
) );
262 /* add the verb, path and HTTP version string */
267 req
[n
++] = http1_1
? g_szHttp1_1
: g_szHttp1_0
;
269 /* Append custom request heades */
270 for (i
= 0; i
< lpwhr
->nCustHeaders
; i
++)
272 if (lpwhr
->pCustHeaders
[i
].wFlags
& HDR_ISREQUEST
)
275 req
[n
++] = lpwhr
->pCustHeaders
[i
].lpszField
;
277 req
[n
++] = lpwhr
->pCustHeaders
[i
].lpszValue
;
279 TRACE("Adding custom header %s (%s)\n",
280 debugstr_w(lpwhr
->pCustHeaders
[i
].lpszField
),
281 debugstr_w(lpwhr
->pCustHeaders
[i
].lpszValue
));
286 ERR("oops. buffer overrun\n");
289 requestString
= HTTP_build_req( req
, 4 );
290 HeapFree( GetProcessHeap(), 0, req
);
293 * Set (header) termination string for request
294 * Make sure there's exactly two new lines at the end of the request
296 p
= &requestString
[strlenW(requestString
)-1];
297 while ( (*p
== '\n') || (*p
== '\r') )
299 strcpyW( p
+1, sztwocrlf
);
301 return requestString
;
304 static void HTTP_ProcessHeaders( LPWININETHTTPREQW lpwhr
)
306 static const WCHAR szSet_Cookie
[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
308 LPHTTPHEADERW setCookieHeader
;
310 HeaderIndex
= HTTP_GetCustomHeaderIndex(lpwhr
, szSet_Cookie
, 0, FALSE
);
311 if (HeaderIndex
== -1)
313 setCookieHeader
= &lpwhr
->pCustHeaders
[HeaderIndex
];
315 if (!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_COOKIES
) && setCookieHeader
->lpszValue
)
317 int nPosStart
= 0, nPosEnd
= 0, len
;
318 static const WCHAR szFmt
[] = { 'h','t','t','p',':','/','/','%','s','/',0};
320 while (setCookieHeader
->lpszValue
[nPosEnd
] != '\0')
322 LPWSTR buf_cookie
, cookie_name
, cookie_data
;
324 LPWSTR domain
= NULL
;
328 while (setCookieHeader
->lpszValue
[nPosEnd
] != ';' && setCookieHeader
->lpszValue
[nPosEnd
] != ',' &&
329 setCookieHeader
->lpszValue
[nPosEnd
] != '\0')
333 if (setCookieHeader
->lpszValue
[nPosEnd
] == ';')
335 /* fixme: not case sensitive, strcasestr is gnu only */
336 int nDomainPosEnd
= 0;
337 int nDomainPosStart
= 0, nDomainLength
= 0;
338 static const WCHAR szDomain
[] = {'d','o','m','a','i','n','=',0};
339 LPWSTR lpszDomain
= strstrW(&setCookieHeader
->lpszValue
[nPosEnd
], szDomain
);
341 { /* they have specified their own domain, lets use it */
342 while (lpszDomain
[nDomainPosEnd
] != ';' && lpszDomain
[nDomainPosEnd
] != ',' &&
343 lpszDomain
[nDomainPosEnd
] != '\0')
347 nDomainPosStart
= strlenW(szDomain
);
348 nDomainLength
= (nDomainPosEnd
- nDomainPosStart
) + 1;
349 domain
= HeapAlloc(GetProcessHeap(), 0, (nDomainLength
+ 1)*sizeof(WCHAR
));
350 lstrcpynW(domain
, &lpszDomain
[nDomainPosStart
], nDomainLength
+ 1);
353 if (setCookieHeader
->lpszValue
[nPosEnd
] == '\0') break;
354 buf_cookie
= HeapAlloc(GetProcessHeap(), 0, ((nPosEnd
- nPosStart
) + 1)*sizeof(WCHAR
));
355 lstrcpynW(buf_cookie
, &setCookieHeader
->lpszValue
[nPosStart
], (nPosEnd
- nPosStart
) + 1);
356 TRACE("%s\n", debugstr_w(buf_cookie
));
357 while (buf_cookie
[nEqualPos
] != '=' && buf_cookie
[nEqualPos
] != '\0')
361 if (buf_cookie
[nEqualPos
] == '\0' || buf_cookie
[nEqualPos
+ 1] == '\0')
363 HeapFree(GetProcessHeap(), 0, buf_cookie
);
367 cookie_name
= HeapAlloc(GetProcessHeap(), 0, (nEqualPos
+ 1)*sizeof(WCHAR
));
368 lstrcpynW(cookie_name
, buf_cookie
, nEqualPos
+ 1);
369 cookie_data
= &buf_cookie
[nEqualPos
+ 1];
371 Host
= HTTP_GetHeader(lpwhr
,szHost
);
372 len
= lstrlenW((domain
? domain
: (Host
?Host
->lpszValue
:NULL
))) +
373 strlenW(lpwhr
->lpszPath
) + 9;
374 buf_url
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
375 sprintfW(buf_url
, szFmt
, (domain
? domain
: (Host
?Host
->lpszValue
:NULL
))); /* FIXME PATH!!! */
376 InternetSetCookieW(buf_url
, cookie_name
, cookie_data
);
378 HeapFree(GetProcessHeap(), 0, buf_url
);
379 HeapFree(GetProcessHeap(), 0, buf_cookie
);
380 HeapFree(GetProcessHeap(), 0, cookie_name
);
381 HeapFree(GetProcessHeap(), 0, domain
);
387 static void HTTP_AddProxyInfo( LPWININETHTTPREQW lpwhr
)
389 LPWININETHTTPSESSIONW lpwhs
= lpwhr
->lpHttpSession
;
390 LPWININETAPPINFOW hIC
= lpwhs
->lpAppInfo
;
392 assert(lpwhs
->hdr
.htype
== WH_HHTTPSESSION
);
393 assert(hIC
->hdr
.htype
== WH_HINIT
);
395 if (hIC
&& (hIC
->lpszProxyUsername
|| hIC
->lpszProxyPassword
))
396 HTTP_InsertProxyAuthorization(lpwhr
, hIC
->lpszProxyUsername
,
397 hIC
->lpszProxyPassword
);
400 /***********************************************************************
401 * HTTP_HttpAddRequestHeadersW (internal)
403 static BOOL WINAPI
HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr
,
404 LPCWSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
409 BOOL bSuccess
= FALSE
;
412 TRACE("copying header: %s\n", debugstr_w(lpszHeader
));
414 if( dwHeaderLength
== ~0U )
415 len
= strlenW(lpszHeader
);
417 len
= dwHeaderLength
;
418 buffer
= HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR
)*(len
+1) );
419 lstrcpynW( buffer
, lpszHeader
, len
+ 1);
425 LPWSTR
* pFieldAndValue
;
429 while (*lpszEnd
!= '\0')
431 if (*lpszEnd
== '\r' && *(lpszEnd
+ 1) == '\n')
436 if (*lpszStart
== '\0')
439 if (*lpszEnd
== '\r')
442 lpszEnd
+= 2; /* Jump over \r\n */
444 TRACE("interpreting header %s\n", debugstr_w(lpszStart
));
445 pFieldAndValue
= HTTP_InterpretHttpHeader(lpszStart
);
448 bSuccess
= HTTP_ProcessHeader(lpwhr
, pFieldAndValue
[0],
449 pFieldAndValue
[1], dwModifier
| HTTP_ADDHDR_FLAG_REQ
);
450 HTTP_FreeTokens(pFieldAndValue
);
456 HeapFree(GetProcessHeap(), 0, buffer
);
461 /***********************************************************************
462 * HttpAddRequestHeadersW (WININET.@)
464 * Adds one or more HTTP header to the request handler
471 BOOL WINAPI
HttpAddRequestHeadersW(HINTERNET hHttpRequest
,
472 LPCWSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
474 BOOL bSuccess
= FALSE
;
475 LPWININETHTTPREQW lpwhr
;
477 TRACE("%p, %s, %i, %i\n", hHttpRequest
, debugstr_w(lpszHeader
), dwHeaderLength
,
483 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hHttpRequest
);
484 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
486 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
489 bSuccess
= HTTP_HttpAddRequestHeadersW( lpwhr
, lpszHeader
, dwHeaderLength
, dwModifier
);
492 WININET_Release( &lpwhr
->hdr
);
497 /***********************************************************************
498 * HttpAddRequestHeadersA (WININET.@)
500 * Adds one or more HTTP header to the request handler
507 BOOL WINAPI
HttpAddRequestHeadersA(HINTERNET hHttpRequest
,
508 LPCSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
514 TRACE("%p, %s, %i, %i\n", hHttpRequest
, debugstr_a(lpszHeader
), dwHeaderLength
,
517 len
= MultiByteToWideChar( CP_ACP
, 0, lpszHeader
, dwHeaderLength
, NULL
, 0 );
518 hdr
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
519 MultiByteToWideChar( CP_ACP
, 0, lpszHeader
, dwHeaderLength
, hdr
, len
);
520 if( dwHeaderLength
!= ~0U )
521 dwHeaderLength
= len
;
523 r
= HttpAddRequestHeadersW( hHttpRequest
, hdr
, dwHeaderLength
, dwModifier
);
525 HeapFree( GetProcessHeap(), 0, hdr
);
530 /* read any content returned by the server so that the connection can be
532 static void HTTP_DrainContent(LPWININETHTTPREQW lpwhr
)
538 if (!INTERNET_ReadFile(&lpwhr
->hdr
, buffer
, sizeof(buffer
), &bytes_read
,
541 } while (bytes_read
);
544 /***********************************************************************
545 * HttpEndRequestA (WININET.@)
547 * Ends an HTTP request that was started by HttpSendRequestEx
554 BOOL WINAPI
HttpEndRequestA(HINTERNET hRequest
,
555 LPINTERNET_BUFFERSA lpBuffersOut
, DWORD dwFlags
, DWORD dwContext
)
557 LPINTERNET_BUFFERSA ptr
;
558 LPINTERNET_BUFFERSW lpBuffersOutW
,ptrW
;
561 TRACE("(%p, %p, %08x, %08x): stub\n", hRequest
, lpBuffersOut
, dwFlags
,
566 lpBuffersOutW
= (LPINTERNET_BUFFERSW
)HeapAlloc(GetProcessHeap(),
567 HEAP_ZERO_MEMORY
, sizeof(INTERNET_BUFFERSW
));
569 lpBuffersOutW
= NULL
;
571 ptrW
= lpBuffersOutW
;
574 if (ptr
->lpvBuffer
&& ptr
->dwBufferLength
)
575 ptrW
->lpvBuffer
= HeapAlloc(GetProcessHeap(),0,ptr
->dwBufferLength
);
576 ptrW
->dwBufferLength
= ptr
->dwBufferLength
;
577 ptrW
->dwBufferTotal
= ptr
->dwBufferTotal
;
580 ptrW
->Next
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,
581 sizeof(INTERNET_BUFFERSW
));
587 rc
= HttpEndRequestW(hRequest
, lpBuffersOutW
, dwFlags
, dwContext
);
591 ptrW
= lpBuffersOutW
;
594 LPINTERNET_BUFFERSW ptrW2
;
596 FIXME("Do we need to translate info out of these buffer?\n");
598 HeapFree(GetProcessHeap(),0,(LPVOID
)ptrW
->lpvBuffer
);
600 HeapFree(GetProcessHeap(),0,ptrW
);
608 /***********************************************************************
609 * HttpEndRequestW (WININET.@)
611 * Ends an HTTP request that was started by HttpSendRequestEx
618 BOOL WINAPI
HttpEndRequestW(HINTERNET hRequest
,
619 LPINTERNET_BUFFERSW lpBuffersOut
, DWORD dwFlags
, DWORD dwContext
)
622 LPWININETHTTPREQW lpwhr
;
627 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hRequest
);
629 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
631 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
635 lpwhr
->hdr
.dwFlags
|= dwFlags
;
636 lpwhr
->hdr
.dwContext
= dwContext
;
638 /* We appear to do nothing with lpBuffersOut.. is that correct? */
640 SendAsyncCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
641 INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
643 responseLen
= HTTP_GetResponseHeaders(lpwhr
);
647 SendAsyncCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
648 INTERNET_STATUS_RESPONSE_RECEIVED
, &responseLen
, sizeof(DWORD
));
650 /* process headers here. Is this right? */
651 HTTP_ProcessHeaders(lpwhr
);
653 dwBufferSize
= sizeof(lpwhr
->dwContentLength
);
654 if (!HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_CONTENT_LENGTH
,
655 &lpwhr
->dwContentLength
,&dwBufferSize
,NULL
))
656 lpwhr
->dwContentLength
= -1;
658 if (lpwhr
->dwContentLength
== 0)
659 HTTP_FinishedReading(lpwhr
);
661 if(!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTO_REDIRECT
))
663 DWORD dwCode
,dwCodeLength
=sizeof(DWORD
);
664 if(HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_STATUS_CODE
,&dwCode
,&dwCodeLength
,NULL
) &&
665 (dwCode
==302 || dwCode
==301))
667 WCHAR szNewLocation
[2048];
668 dwBufferSize
=sizeof(szNewLocation
);
669 if(HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_LOCATION
,szNewLocation
,&dwBufferSize
,NULL
))
671 static const WCHAR szGET
[] = { 'G','E','T', 0 };
672 /* redirects are always GETs */
673 HeapFree(GetProcessHeap(),0,lpwhr
->lpszVerb
);
674 lpwhr
->lpszVerb
= WININET_strdupW(szGET
);
675 HTTP_DrainContent(lpwhr
);
676 rc
= HTTP_HandleRedirect(lpwhr
, szNewLocation
);
678 rc
= HTTP_HttpSendRequestW(lpwhr
, NULL
, 0, NULL
, 0, 0, TRUE
);
683 TRACE("%i <--\n",rc
);
687 /***********************************************************************
688 * HttpOpenRequestW (WININET.@)
690 * Open a HTTP request handle
693 * HINTERNET a HTTP request handle on success
697 HINTERNET WINAPI
HttpOpenRequestW(HINTERNET hHttpSession
,
698 LPCWSTR lpszVerb
, LPCWSTR lpszObjectName
, LPCWSTR lpszVersion
,
699 LPCWSTR lpszReferrer
, LPCWSTR
*lpszAcceptTypes
,
700 DWORD dwFlags
, DWORD dwContext
)
702 LPWININETHTTPSESSIONW lpwhs
;
703 HINTERNET handle
= NULL
;
705 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08x)\n", hHttpSession
,
706 debugstr_w(lpszVerb
), debugstr_w(lpszObjectName
),
707 debugstr_w(lpszVersion
), debugstr_w(lpszReferrer
), lpszAcceptTypes
,
709 if(lpszAcceptTypes
!=NULL
)
712 for(i
=0;lpszAcceptTypes
[i
]!=NULL
;i
++)
713 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes
[i
]));
716 lpwhs
= (LPWININETHTTPSESSIONW
) WININET_GetObject( hHttpSession
);
717 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
719 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
724 * My tests seem to show that the windows version does not
725 * become asynchronous until after this point. And anyhow
726 * if this call was asynchronous then how would you get the
727 * necessary HINTERNET pointer returned by this function.
730 handle
= HTTP_HttpOpenRequestW(lpwhs
, lpszVerb
, lpszObjectName
,
731 lpszVersion
, lpszReferrer
, lpszAcceptTypes
,
735 WININET_Release( &lpwhs
->hdr
);
736 TRACE("returning %p\n", handle
);
741 /***********************************************************************
742 * HttpOpenRequestA (WININET.@)
744 * Open a HTTP request handle
747 * HINTERNET a HTTP request handle on success
751 HINTERNET WINAPI
HttpOpenRequestA(HINTERNET hHttpSession
,
752 LPCSTR lpszVerb
, LPCSTR lpszObjectName
, LPCSTR lpszVersion
,
753 LPCSTR lpszReferrer
, LPCSTR
*lpszAcceptTypes
,
754 DWORD dwFlags
, DWORD dwContext
)
756 LPWSTR szVerb
= NULL
, szObjectName
= NULL
;
757 LPWSTR szVersion
= NULL
, szReferrer
= NULL
, *szAcceptTypes
= NULL
;
759 INT acceptTypesCount
;
760 HINTERNET rc
= FALSE
;
761 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08x)\n", hHttpSession
,
762 debugstr_a(lpszVerb
), debugstr_a(lpszObjectName
),
763 debugstr_a(lpszVersion
), debugstr_a(lpszReferrer
), lpszAcceptTypes
,
768 len
= MultiByteToWideChar(CP_ACP
, 0, lpszVerb
, -1, NULL
, 0 );
769 szVerb
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
772 MultiByteToWideChar(CP_ACP
, 0, lpszVerb
, -1, szVerb
, len
);
777 len
= MultiByteToWideChar(CP_ACP
, 0, lpszObjectName
, -1, NULL
, 0 );
778 szObjectName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
781 MultiByteToWideChar(CP_ACP
, 0, lpszObjectName
, -1, szObjectName
, len
);
786 len
= MultiByteToWideChar(CP_ACP
, 0, lpszVersion
, -1, NULL
, 0 );
787 szVersion
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
790 MultiByteToWideChar(CP_ACP
, 0, lpszVersion
, -1, szVersion
, len
);
795 len
= MultiByteToWideChar(CP_ACP
, 0, lpszReferrer
, -1, NULL
, 0 );
796 szReferrer
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
799 MultiByteToWideChar(CP_ACP
, 0, lpszReferrer
, -1, szReferrer
, len
);
802 acceptTypesCount
= 0;
805 /* find out how many there are */
806 while (lpszAcceptTypes
[acceptTypesCount
])
808 szAcceptTypes
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
*) * (acceptTypesCount
+1));
809 acceptTypesCount
= 0;
810 while (lpszAcceptTypes
[acceptTypesCount
])
812 len
= MultiByteToWideChar(CP_ACP
, 0, lpszAcceptTypes
[acceptTypesCount
],
814 szAcceptTypes
[acceptTypesCount
] = HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
815 if (!szAcceptTypes
[acceptTypesCount
] )
817 MultiByteToWideChar(CP_ACP
, 0, lpszAcceptTypes
[acceptTypesCount
],
818 -1, szAcceptTypes
[acceptTypesCount
], len
);
821 szAcceptTypes
[acceptTypesCount
] = NULL
;
823 else szAcceptTypes
= 0;
825 rc
= HttpOpenRequestW(hHttpSession
, szVerb
, szObjectName
,
826 szVersion
, szReferrer
,
827 (LPCWSTR
*)szAcceptTypes
, dwFlags
, dwContext
);
832 acceptTypesCount
= 0;
833 while (szAcceptTypes
[acceptTypesCount
])
835 HeapFree(GetProcessHeap(), 0, szAcceptTypes
[acceptTypesCount
]);
838 HeapFree(GetProcessHeap(), 0, szAcceptTypes
);
840 HeapFree(GetProcessHeap(), 0, szReferrer
);
841 HeapFree(GetProcessHeap(), 0, szVersion
);
842 HeapFree(GetProcessHeap(), 0, szObjectName
);
843 HeapFree(GetProcessHeap(), 0, szVerb
);
848 /***********************************************************************
851 static UINT
HTTP_EncodeBase64( LPCWSTR bin
, LPWSTR base64
)
854 static LPCSTR HTTP_Base64Enc
=
855 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
859 /* first 6 bits, all from bin[0] */
860 base64
[n
++] = HTTP_Base64Enc
[(bin
[0] & 0xfc) >> 2];
861 x
= (bin
[0] & 3) << 4;
863 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
866 base64
[n
++] = HTTP_Base64Enc
[x
];
871 base64
[n
++] = HTTP_Base64Enc
[ x
| ( (bin
[1]&0xf0) >> 4 ) ];
872 x
= ( bin
[1] & 0x0f ) << 2;
874 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
877 base64
[n
++] = HTTP_Base64Enc
[x
];
881 base64
[n
++] = HTTP_Base64Enc
[ x
| ( (bin
[2]&0xc0 ) >> 6 ) ];
883 /* last 6 bits, all from bin [2] */
884 base64
[n
++] = HTTP_Base64Enc
[ bin
[2] & 0x3f ];
891 /***********************************************************************
892 * HTTP_EncodeBasicAuth
894 * Encode the basic authentication string for HTTP 1.1
896 static LPWSTR
HTTP_EncodeBasicAuth( LPCWSTR username
, LPCWSTR password
)
900 static const WCHAR szBasic
[] = {'B','a','s','i','c',' ',0};
901 static const WCHAR szColon
[] = {':',0};
903 len
= lstrlenW( username
) + 1 + lstrlenW ( password
) + 1;
904 in
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
908 len
= lstrlenW(szBasic
) +
909 (lstrlenW( username
) + 1 + lstrlenW ( password
))*2 + 1 + 1;
910 out
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
913 lstrcpyW( in
, username
);
914 lstrcatW( in
, szColon
);
915 lstrcatW( in
, password
);
916 lstrcpyW( out
, szBasic
);
917 HTTP_EncodeBase64( in
, &out
[strlenW(out
)] );
919 HeapFree( GetProcessHeap(), 0, in
);
924 /***********************************************************************
925 * HTTP_InsertProxyAuthorization
927 * Insert the basic authorization field in the request header
929 static BOOL
HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr
,
930 LPCWSTR username
, LPCWSTR password
)
932 WCHAR
*authorization
= HTTP_EncodeBasicAuth( username
, password
);
938 TRACE( "Inserting authorization: %s\n", debugstr_w( authorization
) );
940 HTTP_ProcessHeader(lpwhr
, szProxy_Authorization
, authorization
,
941 HTTP_ADDHDR_FLAG_REPLACE
);
943 HeapFree( GetProcessHeap(), 0, authorization
);
948 /***********************************************************************
951 static BOOL
HTTP_DealWithProxy( LPWININETAPPINFOW hIC
,
952 LPWININETHTTPSESSIONW lpwhs
, LPWININETHTTPREQW lpwhr
)
954 WCHAR buf
[MAXHOSTNAME
];
955 WCHAR proxy
[MAXHOSTNAME
+ 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
957 static WCHAR szNul
[] = { 0 };
958 URL_COMPONENTSW UrlComponents
;
959 static const WCHAR szHttp
[] = { 'h','t','t','p',':','/','/',0 }, szSlash
[] = { '/',0 } ;
960 static const WCHAR szFormat1
[] = { 'h','t','t','p',':','/','/','%','s',0 };
961 static const WCHAR szFormat2
[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
964 memset( &UrlComponents
, 0, sizeof UrlComponents
);
965 UrlComponents
.dwStructSize
= sizeof UrlComponents
;
966 UrlComponents
.lpszHostName
= buf
;
967 UrlComponents
.dwHostNameLength
= MAXHOSTNAME
;
969 if( CSTR_EQUAL
!= CompareStringW(LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
,
970 hIC
->lpszProxy
,strlenW(szHttp
),szHttp
,strlenW(szHttp
)) )
971 sprintfW(proxy
, szFormat1
, hIC
->lpszProxy
);
973 strcpyW(proxy
, hIC
->lpszProxy
);
974 if( !InternetCrackUrlW(proxy
, 0, 0, &UrlComponents
) )
976 if( UrlComponents
.dwHostNameLength
== 0 )
979 if( !lpwhr
->lpszPath
)
980 lpwhr
->lpszPath
= szNul
;
981 TRACE("server='%s' path='%s'\n",
982 debugstr_w(lpwhs
->lpszHostName
), debugstr_w(lpwhr
->lpszPath
));
983 /* for constant 15 see above */
984 len
= strlenW(lpwhs
->lpszHostName
) + strlenW(lpwhr
->lpszPath
) + 15;
985 url
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
987 if(UrlComponents
.nPort
== INTERNET_INVALID_PORT_NUMBER
)
988 UrlComponents
.nPort
= INTERNET_DEFAULT_HTTP_PORT
;
990 sprintfW(url
, szFormat2
, lpwhs
->lpszHostName
, lpwhs
->nHostPort
);
992 if( lpwhr
->lpszPath
[0] != '/' )
993 strcatW( url
, szSlash
);
994 strcatW(url
, lpwhr
->lpszPath
);
995 if(lpwhr
->lpszPath
!= szNul
)
996 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszPath
);
997 lpwhr
->lpszPath
= url
;
999 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszServerName
);
1000 lpwhs
->lpszServerName
= WININET_strdupW(UrlComponents
.lpszHostName
);
1001 lpwhs
->nServerPort
= UrlComponents
.nPort
;
1006 static BOOL
HTTP_ResolveName(LPWININETHTTPREQW lpwhr
)
1009 LPWININETHTTPSESSIONW lpwhs
= lpwhr
->lpHttpSession
;
1011 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
1012 INTERNET_STATUS_RESOLVING_NAME
,
1013 lpwhs
->lpszServerName
,
1014 strlenW(lpwhs
->lpszServerName
)+1);
1016 if (!GetAddress(lpwhs
->lpszServerName
, lpwhs
->nServerPort
,
1017 &lpwhs
->socketAddress
))
1019 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED
);
1023 inet_ntop(lpwhs
->socketAddress
.sin_family
, &lpwhs
->socketAddress
.sin_addr
,
1024 szaddr
, sizeof(szaddr
));
1025 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
1026 INTERNET_STATUS_NAME_RESOLVED
,
1027 szaddr
, strlen(szaddr
)+1);
1031 /***********************************************************************
1032 * HTTP_HttpOpenRequestW (internal)
1034 * Open a HTTP request handle
1037 * HINTERNET a HTTP request handle on success
1041 HINTERNET WINAPI
HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs
,
1042 LPCWSTR lpszVerb
, LPCWSTR lpszObjectName
, LPCWSTR lpszVersion
,
1043 LPCWSTR lpszReferrer
, LPCWSTR
*lpszAcceptTypes
,
1044 DWORD dwFlags
, DWORD dwContext
)
1046 LPWININETAPPINFOW hIC
= NULL
;
1047 LPWININETHTTPREQW lpwhr
;
1049 LPWSTR lpszUrl
= NULL
;
1051 HINTERNET handle
= NULL
;
1052 static const WCHAR szUrlForm
[] = {'h','t','t','p',':','/','/','%','s',0};
1058 assert( lpwhs
->hdr
.htype
== WH_HHTTPSESSION
);
1059 hIC
= lpwhs
->lpAppInfo
;
1061 lpwhr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WININETHTTPREQW
));
1064 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
1067 lpwhr
->hdr
.htype
= WH_HHTTPREQ
;
1068 lpwhr
->hdr
.dwFlags
= dwFlags
;
1069 lpwhr
->hdr
.dwContext
= dwContext
;
1070 lpwhr
->hdr
.dwRefCount
= 1;
1071 lpwhr
->hdr
.destroy
= HTTP_CloseHTTPRequestHandle
;
1072 lpwhr
->hdr
.lpfnStatusCB
= lpwhs
->hdr
.lpfnStatusCB
;
1073 lpwhr
->hdr
.dwInternalFlags
= lpwhs
->hdr
.dwInternalFlags
& INET_CALLBACKW
;
1075 WININET_AddRef( &lpwhs
->hdr
);
1076 lpwhr
->lpHttpSession
= lpwhs
;
1078 handle
= WININET_AllocHandle( &lpwhr
->hdr
);
1081 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
1085 if (!NETCON_init(&lpwhr
->netConnection
, dwFlags
& INTERNET_FLAG_SECURE
))
1087 InternetCloseHandle( handle
);
1092 if (NULL
!= lpszObjectName
&& strlenW(lpszObjectName
)) {
1096 rc
= UrlEscapeW(lpszObjectName
, NULL
, &len
, URL_ESCAPE_SPACES_ONLY
);
1097 if (rc
!= E_POINTER
)
1098 len
= strlenW(lpszObjectName
)+1;
1099 lpwhr
->lpszPath
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
1100 rc
= UrlEscapeW(lpszObjectName
, lpwhr
->lpszPath
, &len
,
1101 URL_ESCAPE_SPACES_ONLY
);
1104 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName
),rc
);
1105 strcpyW(lpwhr
->lpszPath
,lpszObjectName
);
1109 if (NULL
!= lpszReferrer
&& strlenW(lpszReferrer
))
1110 HTTP_ProcessHeader(lpwhr
, HTTP_REFERER
, lpszReferrer
, HTTP_ADDHDR_FLAG_COALESCE
);
1112 if(lpszAcceptTypes
!=NULL
)
1115 for(i
=0;lpszAcceptTypes
[i
]!=NULL
;i
++)
1116 HTTP_ProcessHeader(lpwhr
, HTTP_ACCEPT
, lpszAcceptTypes
[i
],
1117 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
|HTTP_ADDHDR_FLAG_REQ
|(i
== 0 ? HTTP_ADDHDR_FLAG_REPLACE
: 0));
1120 if (NULL
== lpszVerb
)
1122 static const WCHAR szGet
[] = {'G','E','T',0};
1123 lpwhr
->lpszVerb
= WININET_strdupW(szGet
);
1125 else if (strlenW(lpszVerb
))
1126 lpwhr
->lpszVerb
= WININET_strdupW(lpszVerb
);
1128 if (NULL
!= lpszReferrer
&& strlenW(lpszReferrer
))
1130 WCHAR buf
[MAXHOSTNAME
];
1131 URL_COMPONENTSW UrlComponents
;
1133 memset( &UrlComponents
, 0, sizeof UrlComponents
);
1134 UrlComponents
.dwStructSize
= sizeof UrlComponents
;
1135 UrlComponents
.lpszHostName
= buf
;
1136 UrlComponents
.dwHostNameLength
= MAXHOSTNAME
;
1138 InternetCrackUrlW(lpszReferrer
, 0, 0, &UrlComponents
);
1139 if (strlenW(UrlComponents
.lpszHostName
))
1140 HTTP_ProcessHeader(lpwhr
, szHost
, UrlComponents
.lpszHostName
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_REQ
);
1143 HTTP_ProcessHeader(lpwhr
, szHost
, lpwhs
->lpszHostName
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_REQ
);
1145 if (lpwhs
->nServerPort
== INTERNET_INVALID_PORT_NUMBER
)
1146 lpwhs
->nServerPort
= (dwFlags
& INTERNET_FLAG_SECURE
?
1147 INTERNET_DEFAULT_HTTPS_PORT
:
1148 INTERNET_DEFAULT_HTTP_PORT
);
1149 lpwhs
->nHostPort
= lpwhs
->nServerPort
;
1151 if (NULL
!= hIC
->lpszProxy
&& hIC
->lpszProxy
[0] != 0)
1152 HTTP_DealWithProxy( hIC
, lpwhs
, lpwhr
);
1156 WCHAR
*agent_header
;
1157 static const WCHAR user_agent
[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
1159 len
= strlenW(hIC
->lpszAgent
) + strlenW(user_agent
);
1160 agent_header
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
1161 sprintfW(agent_header
, user_agent
, hIC
->lpszAgent
);
1163 HTTP_HttpAddRequestHeadersW(lpwhr
, agent_header
, strlenW(agent_header
),
1164 HTTP_ADDREQ_FLAG_ADD
);
1165 HeapFree(GetProcessHeap(), 0, agent_header
);
1168 Host
= HTTP_GetHeader(lpwhr
,szHost
);
1170 len
= lstrlenW(Host
->lpszValue
) + strlenW(szUrlForm
);
1171 lpszUrl
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
1172 sprintfW( lpszUrl
, szUrlForm
, Host
->lpszValue
);
1174 if (!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_COOKIES
) &&
1175 InternetGetCookieW(lpszUrl
, NULL
, NULL
, &nCookieSize
))
1178 static const WCHAR szCookie
[] = {'C','o','o','k','i','e',':',' ',0};
1179 static const WCHAR szcrlf
[] = {'\r','\n',0};
1181 lpszCookies
= HeapAlloc(GetProcessHeap(), 0, (nCookieSize
+ 1 + 8)*sizeof(WCHAR
));
1183 cnt
+= sprintfW(lpszCookies
, szCookie
);
1184 InternetGetCookieW(lpszUrl
, NULL
, lpszCookies
+ cnt
, &nCookieSize
);
1185 strcatW(lpszCookies
, szcrlf
);
1187 HTTP_HttpAddRequestHeadersW(lpwhr
, lpszCookies
, strlenW(lpszCookies
),
1188 HTTP_ADDREQ_FLAG_ADD
);
1189 HeapFree(GetProcessHeap(), 0, lpszCookies
);
1191 HeapFree(GetProcessHeap(), 0, lpszUrl
);
1194 INTERNET_SendCallback(&lpwhs
->hdr
, dwContext
,
1195 INTERNET_STATUS_HANDLE_CREATED
, &handle
,
1199 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
1202 if (!HTTP_ResolveName(lpwhr
))
1204 InternetCloseHandle( handle
);
1210 WININET_Release( &lpwhr
->hdr
);
1212 TRACE("<-- %p (%p)\n", handle
, lpwhr
);
1216 static const WCHAR szAccept
[] = { 'A','c','c','e','p','t',0 };
1217 static const WCHAR szAccept_Charset
[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
1218 static const WCHAR szAccept_Encoding
[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
1219 static const WCHAR szAccept_Language
[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
1220 static const WCHAR szAccept_Ranges
[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
1221 static const WCHAR szAge
[] = { 'A','g','e',0 };
1222 static const WCHAR szAllow
[] = { 'A','l','l','o','w',0 };
1223 static const WCHAR szAuthorization
[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
1224 static const WCHAR szCache_Control
[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
1225 static const WCHAR szConnection
[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
1226 static const WCHAR szContent_Base
[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
1227 static const WCHAR szContent_Encoding
[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
1228 static const WCHAR szContent_ID
[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
1229 static const WCHAR szContent_Language
[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
1230 static const WCHAR szContent_Length
[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
1231 static const WCHAR szContent_Location
[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
1232 static const WCHAR szContent_MD5
[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
1233 static const WCHAR szContent_Range
[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
1234 static const WCHAR szContent_Transfer_Encoding
[] = { 'C','o','n','t','e','n','t','-','T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
1235 static const WCHAR szContent_Type
[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
1236 static const WCHAR szCookie
[] = { 'C','o','o','k','i','e',0 };
1237 static const WCHAR szDate
[] = { 'D','a','t','e',0 };
1238 static const WCHAR szFrom
[] = { 'F','r','o','m',0 };
1239 static const WCHAR szETag
[] = { 'E','T','a','g',0 };
1240 static const WCHAR szExpect
[] = { 'E','x','p','e','c','t',0 };
1241 static const WCHAR szExpires
[] = { 'E','x','p','i','r','e','s',0 };
1242 static const WCHAR szIf_Match
[] = { 'I','f','-','M','a','t','c','h',0 };
1243 static const WCHAR szIf_Modified_Since
[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1244 static const WCHAR szIf_None_Match
[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
1245 static const WCHAR szIf_Range
[] = { 'I','f','-','R','a','n','g','e',0 };
1246 static const WCHAR szIf_Unmodified_Since
[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1247 static const WCHAR szLast_Modified
[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
1248 static const WCHAR szLocation
[] = { 'L','o','c','a','t','i','o','n',0 };
1249 static const WCHAR szMax_Forwards
[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
1250 static const WCHAR szMime_Version
[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
1251 static const WCHAR szPragma
[] = { 'P','r','a','g','m','a',0 };
1252 static const WCHAR szProxy_Authenticate
[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1253 static const WCHAR szProxy_Connection
[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
1254 static const WCHAR szPublic
[] = { 'P','u','b','l','i','c',0 };
1255 static const WCHAR szRange
[] = { 'R','a','n','g','e',0 };
1256 static const WCHAR szReferer
[] = { 'R','e','f','e','r','e','r',0 };
1257 static const WCHAR szRetry_After
[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
1258 static const WCHAR szServer
[] = { 'S','e','r','v','e','r',0 };
1259 static const WCHAR szSet_Cookie
[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
1260 static const WCHAR szTransfer_Encoding
[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
1261 static const WCHAR szUnless_Modified_Since
[] = { 'U','n','l','e','s','s','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1262 static const WCHAR szUpgrade
[] = { 'U','p','g','r','a','d','e',0 };
1263 static const WCHAR szURI
[] = { 'U','R','I',0 };
1264 static const WCHAR szUser_Agent
[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
1265 static const WCHAR szVary
[] = { 'V','a','r','y',0 };
1266 static const WCHAR szVia
[] = { 'V','i','a',0 };
1267 static const WCHAR szWarning
[] = { 'W','a','r','n','i','n','g',0 };
1268 static const WCHAR szWWW_Authenticate
[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1270 static const LPCWSTR header_lookup
[] = {
1271 szMime_Version
, /* HTTP_QUERY_MIME_VERSION = 0 */
1272 szContent_Type
, /* HTTP_QUERY_CONTENT_TYPE = 1 */
1273 szContent_Transfer_Encoding
,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
1274 szContent_ID
, /* HTTP_QUERY_CONTENT_ID = 3 */
1275 NULL
, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
1276 szContent_Length
, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
1277 szContent_Language
, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
1278 szAllow
, /* HTTP_QUERY_ALLOW = 7 */
1279 szPublic
, /* HTTP_QUERY_PUBLIC = 8 */
1280 szDate
, /* HTTP_QUERY_DATE = 9 */
1281 szExpires
, /* HTTP_QUERY_EXPIRES = 10 */
1282 szLast_Modified
, /* HTTP_QUERY_LAST_MODIFIED = 11 */
1283 NULL
, /* HTTP_QUERY_MESSAGE_ID = 12 */
1284 szURI
, /* HTTP_QUERY_URI = 13 */
1285 szFrom
, /* HTTP_QUERY_DERIVED_FROM = 14 */
1286 NULL
, /* HTTP_QUERY_COST = 15 */
1287 NULL
, /* HTTP_QUERY_LINK = 16 */
1288 szPragma
, /* HTTP_QUERY_PRAGMA = 17 */
1289 NULL
, /* HTTP_QUERY_VERSION = 18 */
1290 szStatus
, /* HTTP_QUERY_STATUS_CODE = 19 */
1291 NULL
, /* HTTP_QUERY_STATUS_TEXT = 20 */
1292 NULL
, /* HTTP_QUERY_RAW_HEADERS = 21 */
1293 NULL
, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
1294 szConnection
, /* HTTP_QUERY_CONNECTION = 23 */
1295 szAccept
, /* HTTP_QUERY_ACCEPT = 24 */
1296 szAccept_Charset
, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
1297 szAccept_Encoding
, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
1298 szAccept_Language
, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
1299 szAuthorization
, /* HTTP_QUERY_AUTHORIZATION = 28 */
1300 szContent_Encoding
, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
1301 NULL
, /* HTTP_QUERY_FORWARDED = 30 */
1302 NULL
, /* HTTP_QUERY_FROM = 31 */
1303 szIf_Modified_Since
, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
1304 szLocation
, /* HTTP_QUERY_LOCATION = 33 */
1305 NULL
, /* HTTP_QUERY_ORIG_URI = 34 */
1306 szReferer
, /* HTTP_QUERY_REFERER = 35 */
1307 szRetry_After
, /* HTTP_QUERY_RETRY_AFTER = 36 */
1308 szServer
, /* HTTP_QUERY_SERVER = 37 */
1309 NULL
, /* HTTP_TITLE = 38 */
1310 szUser_Agent
, /* HTTP_QUERY_USER_AGENT = 39 */
1311 szWWW_Authenticate
, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
1312 szProxy_Authenticate
, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
1313 szAccept_Ranges
, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
1314 szSet_Cookie
, /* HTTP_QUERY_SET_COOKIE = 43 */
1315 szCookie
, /* HTTP_QUERY_COOKIE = 44 */
1316 NULL
, /* HTTP_QUERY_REQUEST_METHOD = 45 */
1317 NULL
, /* HTTP_QUERY_REFRESH = 46 */
1318 NULL
, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
1319 szAge
, /* HTTP_QUERY_AGE = 48 */
1320 szCache_Control
, /* HTTP_QUERY_CACHE_CONTROL = 49 */
1321 szContent_Base
, /* HTTP_QUERY_CONTENT_BASE = 50 */
1322 szContent_Location
, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
1323 szContent_MD5
, /* HTTP_QUERY_CONTENT_MD5 = 52 */
1324 szContent_Range
, /* HTTP_QUERY_CONTENT_RANGE = 53 */
1325 szETag
, /* HTTP_QUERY_ETAG = 54 */
1326 szHost
, /* HTTP_QUERY_HOST = 55 */
1327 szIf_Match
, /* HTTP_QUERY_IF_MATCH = 56 */
1328 szIf_None_Match
, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
1329 szIf_Range
, /* HTTP_QUERY_IF_RANGE = 58 */
1330 szIf_Unmodified_Since
, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
1331 szMax_Forwards
, /* HTTP_QUERY_MAX_FORWARDS = 60 */
1332 szProxy_Authorization
, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
1333 szRange
, /* HTTP_QUERY_RANGE = 62 */
1334 szTransfer_Encoding
, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
1335 szUpgrade
, /* HTTP_QUERY_UPGRADE = 64 */
1336 szVary
, /* HTTP_QUERY_VARY = 65 */
1337 szVia
, /* HTTP_QUERY_VIA = 66 */
1338 szWarning
, /* HTTP_QUERY_WARNING = 67 */
1339 szExpect
, /* HTTP_QUERY_EXPECT = 68 */
1340 szProxy_Connection
, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
1341 szUnless_Modified_Since
, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
1344 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
1346 /***********************************************************************
1347 * HTTP_HttpQueryInfoW (internal)
1349 static BOOL WINAPI
HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr
, DWORD dwInfoLevel
,
1350 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
1352 LPHTTPHEADERW lphttpHdr
= NULL
;
1353 BOOL bSuccess
= FALSE
;
1354 BOOL request_only
= dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
;
1355 INT requested_index
= lpdwIndex
? *lpdwIndex
: 0;
1356 INT level
= (dwInfoLevel
& ~HTTP_QUERY_MODIFIER_FLAGS_MASK
);
1359 /* Find requested header structure */
1362 case HTTP_QUERY_CUSTOM
:
1363 index
= HTTP_GetCustomHeaderIndex(lpwhr
, lpBuffer
, requested_index
, request_only
);
1366 case HTTP_QUERY_RAW_HEADERS_CRLF
:
1368 DWORD len
= strlenW(lpwhr
->lpszRawHeaders
);
1369 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
1371 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
1372 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1375 memcpy(lpBuffer
, lpwhr
->lpszRawHeaders
, (len
+1)*sizeof(WCHAR
));
1376 *lpdwBufferLength
= len
* sizeof(WCHAR
);
1378 TRACE("returning data: %s\n", debugstr_wn((WCHAR
*)lpBuffer
, len
));
1382 case HTTP_QUERY_RAW_HEADERS
:
1384 static const WCHAR szCrLf
[] = {'\r','\n',0};
1385 LPWSTR
* ppszRawHeaderLines
= HTTP_Tokenize(lpwhr
->lpszRawHeaders
, szCrLf
);
1387 LPWSTR pszString
= (WCHAR
*)lpBuffer
;
1389 for (i
= 0; ppszRawHeaderLines
[i
]; i
++)
1390 size
+= strlenW(ppszRawHeaderLines
[i
]) + 1;
1392 if (size
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
1394 HTTP_FreeTokens(ppszRawHeaderLines
);
1395 *lpdwBufferLength
= (size
+ 1) * sizeof(WCHAR
);
1396 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1400 for (i
= 0; ppszRawHeaderLines
[i
]; i
++)
1402 DWORD len
= strlenW(ppszRawHeaderLines
[i
]);
1403 memcpy(pszString
, ppszRawHeaderLines
[i
], (len
+1)*sizeof(WCHAR
));
1408 TRACE("returning data: %s\n", debugstr_wn((WCHAR
*)lpBuffer
, size
));
1410 *lpdwBufferLength
= size
* sizeof(WCHAR
);
1411 HTTP_FreeTokens(ppszRawHeaderLines
);
1415 case HTTP_QUERY_STATUS_TEXT
:
1416 if (lpwhr
->lpszStatusText
)
1418 DWORD len
= strlenW(lpwhr
->lpszStatusText
);
1419 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
1421 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
1422 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1425 memcpy(lpBuffer
, lpwhr
->lpszStatusText
, (len
+1)*sizeof(WCHAR
));
1426 *lpdwBufferLength
= len
* sizeof(WCHAR
);
1428 TRACE("returning data: %s\n", debugstr_wn((WCHAR
*)lpBuffer
, len
));
1433 case HTTP_QUERY_VERSION
:
1434 if (lpwhr
->lpszVersion
)
1436 DWORD len
= strlenW(lpwhr
->lpszVersion
);
1437 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
1439 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
1440 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1443 memcpy(lpBuffer
, lpwhr
->lpszVersion
, (len
+1)*sizeof(WCHAR
));
1444 *lpdwBufferLength
= len
* sizeof(WCHAR
);
1446 TRACE("returning data: %s\n", debugstr_wn((WCHAR
*)lpBuffer
, len
));
1452 assert (LAST_TABLE_HEADER
== (HTTP_QUERY_UNLESS_MODIFIED_SINCE
+ 1));
1454 if (level
>= 0 && level
< LAST_TABLE_HEADER
&& header_lookup
[level
])
1455 index
= HTTP_GetCustomHeaderIndex(lpwhr
, header_lookup
[level
],
1456 requested_index
,request_only
);
1460 lphttpHdr
= &lpwhr
->pCustHeaders
[index
];
1462 /* Ensure header satisifies requested attributes */
1464 ((dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
) &&
1465 (~lphttpHdr
->wFlags
& HDR_ISREQUEST
)))
1467 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND
);
1474 /* coalesce value to reuqested type */
1475 if (dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
)
1477 *(int *)lpBuffer
= atoiW(lphttpHdr
->lpszValue
);
1480 TRACE(" returning number : %d\n", *(int *)lpBuffer
);
1482 else if (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
)
1488 tmpTime
= ConvertTimeString(lphttpHdr
->lpszValue
);
1490 tmpTM
= *gmtime(&tmpTime
);
1491 STHook
= (SYSTEMTIME
*) lpBuffer
;
1495 STHook
->wDay
= tmpTM
.tm_mday
;
1496 STHook
->wHour
= tmpTM
.tm_hour
;
1497 STHook
->wMilliseconds
= 0;
1498 STHook
->wMinute
= tmpTM
.tm_min
;
1499 STHook
->wDayOfWeek
= tmpTM
.tm_wday
;
1500 STHook
->wMonth
= tmpTM
.tm_mon
+ 1;
1501 STHook
->wSecond
= tmpTM
.tm_sec
;
1502 STHook
->wYear
= tmpTM
.tm_year
;
1506 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
1507 STHook
->wYear
, STHook
->wMonth
, STHook
->wDay
, STHook
->wDayOfWeek
,
1508 STHook
->wHour
, STHook
->wMinute
, STHook
->wSecond
, STHook
->wMilliseconds
);
1510 else if (lphttpHdr
->lpszValue
)
1512 DWORD len
= (strlenW(lphttpHdr
->lpszValue
) + 1) * sizeof(WCHAR
);
1514 if (len
> *lpdwBufferLength
)
1516 *lpdwBufferLength
= len
;
1517 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1521 memcpy(lpBuffer
, lphttpHdr
->lpszValue
, len
);
1522 *lpdwBufferLength
= len
- sizeof(WCHAR
);
1525 TRACE(" returning string : '%s'\n", debugstr_w(lpBuffer
));
1530 /***********************************************************************
1531 * HttpQueryInfoW (WININET.@)
1533 * Queries for information about an HTTP request
1540 BOOL WINAPI
HttpQueryInfoW(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
1541 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
1543 BOOL bSuccess
= FALSE
;
1544 LPWININETHTTPREQW lpwhr
;
1546 if (TRACE_ON(wininet
)) {
1547 #define FE(x) { x, #x }
1548 static const wininet_flag_info query_flags
[] = {
1549 FE(HTTP_QUERY_MIME_VERSION
),
1550 FE(HTTP_QUERY_CONTENT_TYPE
),
1551 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING
),
1552 FE(HTTP_QUERY_CONTENT_ID
),
1553 FE(HTTP_QUERY_CONTENT_DESCRIPTION
),
1554 FE(HTTP_QUERY_CONTENT_LENGTH
),
1555 FE(HTTP_QUERY_CONTENT_LANGUAGE
),
1556 FE(HTTP_QUERY_ALLOW
),
1557 FE(HTTP_QUERY_PUBLIC
),
1558 FE(HTTP_QUERY_DATE
),
1559 FE(HTTP_QUERY_EXPIRES
),
1560 FE(HTTP_QUERY_LAST_MODIFIED
),
1561 FE(HTTP_QUERY_MESSAGE_ID
),
1563 FE(HTTP_QUERY_DERIVED_FROM
),
1564 FE(HTTP_QUERY_COST
),
1565 FE(HTTP_QUERY_LINK
),
1566 FE(HTTP_QUERY_PRAGMA
),
1567 FE(HTTP_QUERY_VERSION
),
1568 FE(HTTP_QUERY_STATUS_CODE
),
1569 FE(HTTP_QUERY_STATUS_TEXT
),
1570 FE(HTTP_QUERY_RAW_HEADERS
),
1571 FE(HTTP_QUERY_RAW_HEADERS_CRLF
),
1572 FE(HTTP_QUERY_CONNECTION
),
1573 FE(HTTP_QUERY_ACCEPT
),
1574 FE(HTTP_QUERY_ACCEPT_CHARSET
),
1575 FE(HTTP_QUERY_ACCEPT_ENCODING
),
1576 FE(HTTP_QUERY_ACCEPT_LANGUAGE
),
1577 FE(HTTP_QUERY_AUTHORIZATION
),
1578 FE(HTTP_QUERY_CONTENT_ENCODING
),
1579 FE(HTTP_QUERY_FORWARDED
),
1580 FE(HTTP_QUERY_FROM
),
1581 FE(HTTP_QUERY_IF_MODIFIED_SINCE
),
1582 FE(HTTP_QUERY_LOCATION
),
1583 FE(HTTP_QUERY_ORIG_URI
),
1584 FE(HTTP_QUERY_REFERER
),
1585 FE(HTTP_QUERY_RETRY_AFTER
),
1586 FE(HTTP_QUERY_SERVER
),
1587 FE(HTTP_QUERY_TITLE
),
1588 FE(HTTP_QUERY_USER_AGENT
),
1589 FE(HTTP_QUERY_WWW_AUTHENTICATE
),
1590 FE(HTTP_QUERY_PROXY_AUTHENTICATE
),
1591 FE(HTTP_QUERY_ACCEPT_RANGES
),
1592 FE(HTTP_QUERY_SET_COOKIE
),
1593 FE(HTTP_QUERY_COOKIE
),
1594 FE(HTTP_QUERY_REQUEST_METHOD
),
1595 FE(HTTP_QUERY_REFRESH
),
1596 FE(HTTP_QUERY_CONTENT_DISPOSITION
),
1598 FE(HTTP_QUERY_CACHE_CONTROL
),
1599 FE(HTTP_QUERY_CONTENT_BASE
),
1600 FE(HTTP_QUERY_CONTENT_LOCATION
),
1601 FE(HTTP_QUERY_CONTENT_MD5
),
1602 FE(HTTP_QUERY_CONTENT_RANGE
),
1603 FE(HTTP_QUERY_ETAG
),
1604 FE(HTTP_QUERY_HOST
),
1605 FE(HTTP_QUERY_IF_MATCH
),
1606 FE(HTTP_QUERY_IF_NONE_MATCH
),
1607 FE(HTTP_QUERY_IF_RANGE
),
1608 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE
),
1609 FE(HTTP_QUERY_MAX_FORWARDS
),
1610 FE(HTTP_QUERY_PROXY_AUTHORIZATION
),
1611 FE(HTTP_QUERY_RANGE
),
1612 FE(HTTP_QUERY_TRANSFER_ENCODING
),
1613 FE(HTTP_QUERY_UPGRADE
),
1614 FE(HTTP_QUERY_VARY
),
1616 FE(HTTP_QUERY_WARNING
),
1617 FE(HTTP_QUERY_CUSTOM
)
1619 static const wininet_flag_info modifier_flags
[] = {
1620 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS
),
1621 FE(HTTP_QUERY_FLAG_SYSTEMTIME
),
1622 FE(HTTP_QUERY_FLAG_NUMBER
),
1623 FE(HTTP_QUERY_FLAG_COALESCE
)
1626 DWORD info_mod
= dwInfoLevel
& HTTP_QUERY_MODIFIER_FLAGS_MASK
;
1627 DWORD info
= dwInfoLevel
& HTTP_QUERY_HEADER_MASK
;
1630 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest
, dwInfoLevel
, dwInfoLevel
);
1631 TRACE(" Attribute:");
1632 for (i
= 0; i
< (sizeof(query_flags
) / sizeof(query_flags
[0])); i
++) {
1633 if (query_flags
[i
].val
== info
) {
1634 TRACE(" %s", query_flags
[i
].name
);
1638 if (i
== (sizeof(query_flags
) / sizeof(query_flags
[0]))) {
1639 TRACE(" Unknown (%08x)", info
);
1642 TRACE(" Modifier:");
1643 for (i
= 0; i
< (sizeof(modifier_flags
) / sizeof(modifier_flags
[0])); i
++) {
1644 if (modifier_flags
[i
].val
& info_mod
) {
1645 TRACE(" %s", modifier_flags
[i
].name
);
1646 info_mod
&= ~ modifier_flags
[i
].val
;
1651 TRACE(" Unknown (%08x)", info_mod
);
1656 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hHttpRequest
);
1657 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
1659 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1663 bSuccess
= HTTP_HttpQueryInfoW( lpwhr
, dwInfoLevel
,
1664 lpBuffer
, lpdwBufferLength
, lpdwIndex
);
1668 WININET_Release( &lpwhr
->hdr
);
1670 TRACE("%d <--\n", bSuccess
);
1674 /***********************************************************************
1675 * HttpQueryInfoA (WININET.@)
1677 * Queries for information about an HTTP request
1684 BOOL WINAPI
HttpQueryInfoA(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
1685 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
1691 if((dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
) ||
1692 (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
))
1694 return HttpQueryInfoW( hHttpRequest
, dwInfoLevel
, lpBuffer
,
1695 lpdwBufferLength
, lpdwIndex
);
1698 len
= (*lpdwBufferLength
)*sizeof(WCHAR
);
1699 bufferW
= HeapAlloc( GetProcessHeap(), 0, len
);
1700 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
1701 if ((dwInfoLevel
& HTTP_QUERY_HEADER_MASK
) == HTTP_QUERY_CUSTOM
)
1702 MultiByteToWideChar(CP_ACP
,0,lpBuffer
,-1,bufferW
,len
);
1703 result
= HttpQueryInfoW( hHttpRequest
, dwInfoLevel
, bufferW
,
1707 len
= WideCharToMultiByte( CP_ACP
,0, bufferW
, len
/ sizeof(WCHAR
) + 1,
1708 lpBuffer
, *lpdwBufferLength
, NULL
, NULL
);
1709 *lpdwBufferLength
= len
- 1;
1711 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer
));
1714 /* since the strings being returned from HttpQueryInfoW should be
1715 * only ASCII characters, it is reasonable to assume that all of
1716 * the Unicode characters can be reduced to a single byte */
1717 *lpdwBufferLength
= len
/ sizeof(WCHAR
);
1719 HeapFree(GetProcessHeap(), 0, bufferW
);
1724 /***********************************************************************
1725 * HttpSendRequestExA (WININET.@)
1727 * Sends the specified request to the HTTP server and allows chunked
1732 * Failure: FALSE, call GetLastError() for more information.
1734 BOOL WINAPI
HttpSendRequestExA(HINTERNET hRequest
,
1735 LPINTERNET_BUFFERSA lpBuffersIn
,
1736 LPINTERNET_BUFFERSA lpBuffersOut
,
1737 DWORD dwFlags
, DWORD dwContext
)
1739 INTERNET_BUFFERSW BuffersInW
;
1742 LPWSTR header
= NULL
;
1744 TRACE("(%p, %p, %p, %08x, %08x): stub\n", hRequest
, lpBuffersIn
,
1745 lpBuffersOut
, dwFlags
, dwContext
);
1749 BuffersInW
.dwStructSize
= sizeof(LPINTERNET_BUFFERSW
);
1750 if (lpBuffersIn
->lpcszHeader
)
1752 headerlen
= MultiByteToWideChar(CP_ACP
,0,lpBuffersIn
->lpcszHeader
,
1753 lpBuffersIn
->dwHeadersLength
,0,0);
1754 header
= HeapAlloc(GetProcessHeap(),0,headerlen
*sizeof(WCHAR
));
1755 if (!(BuffersInW
.lpcszHeader
= header
))
1757 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
1760 BuffersInW
.dwHeadersLength
= MultiByteToWideChar(CP_ACP
, 0,
1761 lpBuffersIn
->lpcszHeader
, lpBuffersIn
->dwHeadersLength
,
1765 BuffersInW
.lpcszHeader
= NULL
;
1766 BuffersInW
.dwHeadersTotal
= lpBuffersIn
->dwHeadersTotal
;
1767 BuffersInW
.lpvBuffer
= lpBuffersIn
->lpvBuffer
;
1768 BuffersInW
.dwBufferLength
= lpBuffersIn
->dwBufferLength
;
1769 BuffersInW
.dwBufferTotal
= lpBuffersIn
->dwBufferTotal
;
1770 BuffersInW
.Next
= NULL
;
1773 rc
= HttpSendRequestExW(hRequest
, lpBuffersIn
? &BuffersInW
: NULL
, NULL
, dwFlags
, dwContext
);
1775 HeapFree(GetProcessHeap(),0,header
);
1780 /***********************************************************************
1781 * HttpSendRequestExW (WININET.@)
1783 * Sends the specified request to the HTTP server and allows chunked
1788 * Failure: FALSE, call GetLastError() for more information.
1790 BOOL WINAPI
HttpSendRequestExW(HINTERNET hRequest
,
1791 LPINTERNET_BUFFERSW lpBuffersIn
,
1792 LPINTERNET_BUFFERSW lpBuffersOut
,
1793 DWORD dwFlags
, DWORD dwContext
)
1796 LPWININETHTTPREQW lpwhr
;
1797 LPWININETHTTPSESSIONW lpwhs
;
1798 LPWININETAPPINFOW hIC
;
1800 TRACE("(%p, %p, %p, %08x, %08x)\n", hRequest
, lpBuffersIn
,
1801 lpBuffersOut
, dwFlags
, dwContext
);
1803 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hRequest
);
1805 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
1807 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1811 lpwhs
= lpwhr
->lpHttpSession
;
1812 assert(lpwhs
->hdr
.htype
== WH_HHTTPSESSION
);
1813 hIC
= lpwhs
->lpAppInfo
;
1814 assert(hIC
->hdr
.htype
== WH_HINIT
);
1816 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1818 WORKREQUEST workRequest
;
1819 struct WORKREQ_HTTPSENDREQUESTW
*req
;
1821 workRequest
.asyncproc
= AsyncHttpSendRequestProc
;
1822 workRequest
.hdr
= WININET_AddRef( &lpwhr
->hdr
);
1823 req
= &workRequest
.u
.HttpSendRequestW
;
1826 if (lpBuffersIn
->lpcszHeader
)
1827 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
1828 req
->lpszHeader
= WININET_strdupW(lpBuffersIn
->lpcszHeader
);
1830 req
->lpszHeader
= NULL
;
1831 req
->dwHeaderLength
= lpBuffersIn
->dwHeadersLength
;
1832 req
->lpOptional
= lpBuffersIn
->lpvBuffer
;
1833 req
->dwOptionalLength
= lpBuffersIn
->dwBufferLength
;
1834 req
->dwContentLength
= lpBuffersIn
->dwBufferTotal
;
1838 req
->lpszHeader
= NULL
;
1839 req
->dwHeaderLength
= 0;
1840 req
->lpOptional
= NULL
;
1841 req
->dwOptionalLength
= 0;
1842 req
->dwContentLength
= 0;
1845 req
->bEndRequest
= FALSE
;
1847 INTERNET_AsyncCall(&workRequest
);
1849 * This is from windows.
1851 INTERNET_SetLastError(ERROR_IO_PENDING
);
1856 ret
= HTTP_HttpSendRequestW(lpwhr
, lpBuffersIn
->lpcszHeader
, lpBuffersIn
->dwHeadersLength
,
1857 lpBuffersIn
->lpvBuffer
, lpBuffersIn
->dwBufferLength
,
1858 lpBuffersIn
->dwBufferTotal
, FALSE
);
1861 WININET_Release(&lpwhr
->hdr
);
1866 /***********************************************************************
1867 * HttpSendRequestW (WININET.@)
1869 * Sends the specified request to the HTTP server
1876 BOOL WINAPI
HttpSendRequestW(HINTERNET hHttpRequest
, LPCWSTR lpszHeaders
,
1877 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
1879 LPWININETHTTPREQW lpwhr
;
1880 LPWININETHTTPSESSIONW lpwhs
= NULL
;
1881 LPWININETAPPINFOW hIC
= NULL
;
1884 TRACE("%p, %p (%s), %i, %p, %i)\n", hHttpRequest
,
1885 lpszHeaders
, debugstr_w(lpszHeaders
), dwHeaderLength
, lpOptional
, dwOptionalLength
);
1887 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hHttpRequest
);
1888 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
1890 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1895 lpwhs
= lpwhr
->lpHttpSession
;
1896 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
1898 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1903 hIC
= lpwhs
->lpAppInfo
;
1904 if (NULL
== hIC
|| hIC
->hdr
.htype
!= WH_HINIT
)
1906 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1911 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1913 WORKREQUEST workRequest
;
1914 struct WORKREQ_HTTPSENDREQUESTW
*req
;
1916 workRequest
.asyncproc
= AsyncHttpSendRequestProc
;
1917 workRequest
.hdr
= WININET_AddRef( &lpwhr
->hdr
);
1918 req
= &workRequest
.u
.HttpSendRequestW
;
1920 req
->lpszHeader
= WININET_strdupW(lpszHeaders
);
1922 req
->lpszHeader
= 0;
1923 req
->dwHeaderLength
= dwHeaderLength
;
1924 req
->lpOptional
= lpOptional
;
1925 req
->dwOptionalLength
= dwOptionalLength
;
1926 req
->dwContentLength
= dwOptionalLength
;
1927 req
->bEndRequest
= TRUE
;
1929 INTERNET_AsyncCall(&workRequest
);
1931 * This is from windows.
1933 INTERNET_SetLastError(ERROR_IO_PENDING
);
1938 r
= HTTP_HttpSendRequestW(lpwhr
, lpszHeaders
,
1939 dwHeaderLength
, lpOptional
, dwOptionalLength
,
1940 dwOptionalLength
, TRUE
);
1944 WININET_Release( &lpwhr
->hdr
);
1948 /***********************************************************************
1949 * HttpSendRequestA (WININET.@)
1951 * Sends the specified request to the HTTP server
1958 BOOL WINAPI
HttpSendRequestA(HINTERNET hHttpRequest
, LPCSTR lpszHeaders
,
1959 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
1962 LPWSTR szHeaders
=NULL
;
1963 DWORD nLen
=dwHeaderLength
;
1964 if(lpszHeaders
!=NULL
)
1966 nLen
=MultiByteToWideChar(CP_ACP
,0,lpszHeaders
,dwHeaderLength
,NULL
,0);
1967 szHeaders
=HeapAlloc(GetProcessHeap(),0,nLen
*sizeof(WCHAR
));
1968 MultiByteToWideChar(CP_ACP
,0,lpszHeaders
,dwHeaderLength
,szHeaders
,nLen
);
1970 result
=HttpSendRequestW(hHttpRequest
, szHeaders
, nLen
, lpOptional
, dwOptionalLength
);
1971 HeapFree(GetProcessHeap(),0,szHeaders
);
1975 /***********************************************************************
1976 * HTTP_HandleRedirect (internal)
1978 static BOOL
HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszUrl
)
1980 LPWININETHTTPSESSIONW lpwhs
= lpwhr
->lpHttpSession
;
1981 LPWININETAPPINFOW hIC
= lpwhs
->lpAppInfo
;
1986 /* if it's an absolute path, keep the same session info */
1987 lstrcpynW(path
, lpszUrl
, 2048);
1989 else if (NULL
!= hIC
->lpszProxy
&& hIC
->lpszProxy
[0] != 0)
1991 TRACE("Redirect through proxy\n");
1992 lstrcpynW(path
, lpszUrl
, 2048);
1996 URL_COMPONENTSW urlComponents
;
1997 WCHAR protocol
[32], hostName
[MAXHOSTNAME
], userName
[1024];
1998 static WCHAR szHttp
[] = {'h','t','t','p',0};
1999 static WCHAR szHttps
[] = {'h','t','t','p','s',0};
2000 DWORD url_length
= 0;
2002 LPWSTR combined_url
;
2004 urlComponents
.dwStructSize
= sizeof(URL_COMPONENTSW
);
2005 urlComponents
.lpszScheme
= (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
) ? szHttps
: szHttp
;
2006 urlComponents
.dwSchemeLength
= 0;
2007 urlComponents
.lpszHostName
= lpwhs
->lpszHostName
;
2008 urlComponents
.dwHostNameLength
= 0;
2009 urlComponents
.nPort
= lpwhs
->nHostPort
;
2010 urlComponents
.lpszUserName
= lpwhs
->lpszUserName
;
2011 urlComponents
.dwUserNameLength
= 0;
2012 urlComponents
.lpszPassword
= NULL
;
2013 urlComponents
.dwPasswordLength
= 0;
2014 urlComponents
.lpszUrlPath
= lpwhr
->lpszPath
;
2015 urlComponents
.dwUrlPathLength
= 0;
2016 urlComponents
.lpszExtraInfo
= NULL
;
2017 urlComponents
.dwExtraInfoLength
= 0;
2019 if (!InternetCreateUrlW(&urlComponents
, 0, NULL
, &url_length
) &&
2020 (GetLastError() != ERROR_INSUFFICIENT_BUFFER
))
2023 orig_url
= HeapAlloc(GetProcessHeap(), 0, url_length
);
2025 /* convert from bytes to characters */
2026 url_length
= url_length
/ sizeof(WCHAR
) - 1;
2027 if (!InternetCreateUrlW(&urlComponents
, 0, orig_url
, &url_length
))
2029 HeapFree(GetProcessHeap(), 0, orig_url
);
2034 if (!InternetCombineUrlW(orig_url
, lpszUrl
, NULL
, &url_length
, ICU_ENCODE_SPACES_ONLY
) &&
2035 (GetLastError() != ERROR_INSUFFICIENT_BUFFER
))
2037 HeapFree(GetProcessHeap(), 0, orig_url
);
2040 combined_url
= HeapAlloc(GetProcessHeap(), 0, url_length
* sizeof(WCHAR
));
2042 if (!InternetCombineUrlW(orig_url
, lpszUrl
, combined_url
, &url_length
, ICU_ENCODE_SPACES_ONLY
))
2044 HeapFree(GetProcessHeap(), 0, orig_url
);
2045 HeapFree(GetProcessHeap(), 0, combined_url
);
2048 HeapFree(GetProcessHeap(), 0, orig_url
);
2054 urlComponents
.dwStructSize
= sizeof(URL_COMPONENTSW
);
2055 urlComponents
.lpszScheme
= protocol
;
2056 urlComponents
.dwSchemeLength
= 32;
2057 urlComponents
.lpszHostName
= hostName
;
2058 urlComponents
.dwHostNameLength
= MAXHOSTNAME
;
2059 urlComponents
.lpszUserName
= userName
;
2060 urlComponents
.dwUserNameLength
= 1024;
2061 urlComponents
.lpszPassword
= NULL
;
2062 urlComponents
.dwPasswordLength
= 0;
2063 urlComponents
.lpszUrlPath
= path
;
2064 urlComponents
.dwUrlPathLength
= 2048;
2065 urlComponents
.lpszExtraInfo
= NULL
;
2066 urlComponents
.dwExtraInfoLength
= 0;
2067 if(!InternetCrackUrlW(combined_url
, strlenW(combined_url
), 0, &urlComponents
))
2069 HeapFree(GetProcessHeap(), 0, combined_url
);
2072 HeapFree(GetProcessHeap(), 0, combined_url
);
2074 if (!strncmpW(szHttp
, urlComponents
.lpszScheme
, strlenW(szHttp
)) &&
2075 (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
))
2077 TRACE("redirect from secure page to non-secure page\n");
2078 /* FIXME: warn about from secure redirect to non-secure page */
2079 lpwhr
->hdr
.dwFlags
&= ~INTERNET_FLAG_SECURE
;
2081 if (!strncmpW(szHttps
, urlComponents
.lpszScheme
, strlenW(szHttps
)) &&
2082 !(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
))
2084 TRACE("redirect from non-secure page to secure page\n");
2085 /* FIXME: notify about redirect to secure page */
2086 lpwhr
->hdr
.dwFlags
|= INTERNET_FLAG_SECURE
;
2089 if (urlComponents
.nPort
== INTERNET_INVALID_PORT_NUMBER
)
2091 if (lstrlenW(protocol
)>4) /*https*/
2092 urlComponents
.nPort
= INTERNET_DEFAULT_HTTPS_PORT
;
2094 urlComponents
.nPort
= INTERNET_DEFAULT_HTTP_PORT
;
2099 * This upsets redirects to binary files on sourceforge.net
2100 * and gives an html page instead of the target file
2101 * Examination of the HTTP request sent by native wininet.dll
2102 * reveals that it doesn't send a referrer in that case.
2103 * Maybe there's a flag that enables this, or maybe a referrer
2104 * shouldn't be added in case of a redirect.
2107 /* consider the current host as the referrer */
2108 if (NULL
!= lpwhs
->lpszServerName
&& strlenW(lpwhs
->lpszServerName
))
2109 HTTP_ProcessHeader(lpwhr
, HTTP_REFERER
, lpwhs
->lpszServerName
,
2110 HTTP_ADDHDR_FLAG_REQ
|HTTP_ADDREQ_FLAG_REPLACE
|
2111 HTTP_ADDHDR_FLAG_ADD_IF_NEW
);
2114 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszServerName
);
2115 lpwhs
->lpszServerName
= WININET_strdupW(hostName
);
2116 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszHostName
);
2117 if (urlComponents
.nPort
!= INTERNET_DEFAULT_HTTP_PORT
&&
2118 urlComponents
.nPort
!= INTERNET_DEFAULT_HTTPS_PORT
)
2121 static const WCHAR fmt
[] = {'%','s',':','%','i',0};
2122 len
= lstrlenW(hostName
);
2123 len
+= 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
2124 lpwhs
->lpszHostName
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
2125 sprintfW(lpwhs
->lpszHostName
, fmt
, hostName
, urlComponents
.nPort
);
2128 lpwhs
->lpszHostName
= WININET_strdupW(hostName
);
2130 HTTP_ProcessHeader(lpwhr
, szHost
, lpwhs
->lpszHostName
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_REQ
);
2133 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszUserName
);
2134 lpwhs
->lpszUserName
= NULL
;
2136 lpwhs
->lpszUserName
= WININET_strdupW(userName
);
2137 lpwhs
->nServerPort
= urlComponents
.nPort
;
2139 if (!HTTP_ResolveName(lpwhr
))
2142 NETCON_close(&lpwhr
->netConnection
);
2144 if (!NETCON_init(&lpwhr
->netConnection
,lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
))
2148 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszPath
);
2149 lpwhr
->lpszPath
=NULL
;
2155 rc
= UrlEscapeW(path
, NULL
, &needed
, URL_ESCAPE_SPACES_ONLY
);
2156 if (rc
!= E_POINTER
)
2157 needed
= strlenW(path
)+1;
2158 lpwhr
->lpszPath
= HeapAlloc(GetProcessHeap(), 0, needed
*sizeof(WCHAR
));
2159 rc
= UrlEscapeW(path
, lpwhr
->lpszPath
, &needed
,
2160 URL_ESCAPE_SPACES_ONLY
);
2163 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path
),rc
);
2164 strcpyW(lpwhr
->lpszPath
,path
);
2171 /***********************************************************************
2172 * HTTP_build_req (internal)
2174 * concatenate all the strings in the request together
2176 static LPWSTR
HTTP_build_req( LPCWSTR
*list
, int len
)
2181 for( t
= list
; *t
; t
++ )
2182 len
+= strlenW( *t
);
2185 str
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
2188 for( t
= list
; *t
; t
++ )
2194 static BOOL
HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr
)
2197 LPWSTR requestString
;
2203 static const WCHAR szConnect
[] = {'C','O','N','N','E','C','T',0};
2204 static const WCHAR szFormat
[] = {'%','s',':','%','d',0};
2205 LPWININETHTTPSESSIONW lpwhs
= lpwhr
->lpHttpSession
;
2209 lpszPath
= HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs
->lpszHostName
) + 13)*sizeof(WCHAR
) );
2210 sprintfW( lpszPath
, szFormat
, lpwhs
->lpszHostName
, lpwhs
->nHostPort
);
2211 requestString
= HTTP_BuildHeaderRequestString( lpwhr
, szConnect
, lpszPath
, FALSE
);
2212 HeapFree( GetProcessHeap(), 0, lpszPath
);
2214 len
= WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
2215 NULL
, 0, NULL
, NULL
);
2216 len
--; /* the nul terminator isn't needed */
2217 ascii_req
= HeapAlloc( GetProcessHeap(), 0, len
);
2218 WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
2219 ascii_req
, len
, NULL
, NULL
);
2220 HeapFree( GetProcessHeap(), 0, requestString
);
2222 TRACE("full request -> %s\n", debugstr_an( ascii_req
, len
) );
2224 ret
= NETCON_send( &lpwhr
->netConnection
, ascii_req
, len
, 0, &cnt
);
2225 HeapFree( GetProcessHeap(), 0, ascii_req
);
2226 if (!ret
|| cnt
< 0)
2229 responseLen
= HTTP_GetResponseHeaders( lpwhr
);
2236 /***********************************************************************
2237 * HTTP_HttpSendRequestW (internal)
2239 * Sends the specified request to the HTTP server
2246 BOOL WINAPI
HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszHeaders
,
2247 DWORD dwHeaderLength
, LPVOID lpOptional
, DWORD dwOptionalLength
,
2248 DWORD dwContentLength
, BOOL bEndRequest
)
2251 BOOL bSuccess
= FALSE
;
2252 LPWSTR requestString
= NULL
;
2255 INTERNET_ASYNC_RESULT iar
;
2256 static const WCHAR szClose
[] = { 'C','l','o','s','e',0 };
2258 TRACE("--> %p\n", lpwhr
);
2260 assert(lpwhr
->hdr
.htype
== WH_HHTTPREQ
);
2262 /* Clear any error information */
2263 INTERNET_SetLastError(0);
2265 HTTP_FixVerb(lpwhr
);
2267 /* if we are using optional stuff, we must add the fixed header of that option length */
2268 if (dwContentLength
> 0)
2270 static const WCHAR szContentLength
[] = {
2271 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0};
2272 WCHAR contentLengthStr
[sizeof szContentLength
/2 /* includes \n\r */ + 20 /* int */ ];
2273 sprintfW(contentLengthStr
, szContentLength
, dwContentLength
);
2274 HTTP_HttpAddRequestHeadersW(lpwhr
, contentLengthStr
, -1L,
2275 HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REPLACE
);
2285 /* like native, just in case the caller forgot to call InternetReadFile
2286 * for all the data */
2287 HTTP_DrainContent(lpwhr
);
2288 lpwhr
->dwContentRead
= 0;
2290 if (TRACE_ON(wininet
))
2292 LPHTTPHEADERW Host
= HTTP_GetHeader(lpwhr
,szHost
);
2293 TRACE("Going to url %s %s\n", debugstr_w(Host
->lpszValue
), debugstr_w(lpwhr
->lpszPath
));
2298 /* add the headers the caller supplied */
2299 if( lpszHeaders
&& dwHeaderLength
)
2301 HTTP_HttpAddRequestHeadersW(lpwhr
, lpszHeaders
, dwHeaderLength
,
2302 HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REPLACE
);
2305 HTTP_ProcessHeader(lpwhr
, szConnection
,
2306 lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_KEEP_CONNECTION
? szKeepAlive
: szClose
,
2307 HTTP_ADDHDR_FLAG_REQ
| HTTP_ADDHDR_FLAG_REPLACE
);
2309 /* if there's a proxy username and password, add it to the headers */
2310 HTTP_AddProxyInfo(lpwhr
);
2312 requestString
= HTTP_BuildHeaderRequestString(lpwhr
, lpwhr
->lpszVerb
, lpwhr
->lpszPath
, FALSE
);
2314 TRACE("Request header -> %s\n", debugstr_w(requestString
) );
2316 /* Send the request and store the results */
2317 if (!HTTP_OpenConnection(lpwhr
))
2320 /* send the request as ASCII, tack on the optional data */
2322 dwOptionalLength
= 0;
2323 len
= WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
2324 NULL
, 0, NULL
, NULL
);
2325 ascii_req
= HeapAlloc( GetProcessHeap(), 0, len
+ dwOptionalLength
);
2326 WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
2327 ascii_req
, len
, NULL
, NULL
);
2329 memcpy( &ascii_req
[len
-1], lpOptional
, dwOptionalLength
);
2330 len
= (len
+ dwOptionalLength
- 1);
2332 TRACE("full request -> %s\n", debugstr_a(ascii_req
) );
2334 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2335 INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
2337 NETCON_send(&lpwhr
->netConnection
, ascii_req
, len
, 0, &cnt
);
2338 HeapFree( GetProcessHeap(), 0, ascii_req
);
2340 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2341 INTERNET_STATUS_REQUEST_SENT
,
2342 &len
, sizeof(DWORD
));
2348 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2349 INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
2354 responseLen
= HTTP_GetResponseHeaders(lpwhr
);
2358 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2359 INTERNET_STATUS_RESPONSE_RECEIVED
, &responseLen
,
2362 HTTP_ProcessHeaders(lpwhr
);
2364 dwBufferSize
= sizeof(lpwhr
->dwContentLength
);
2365 if (!HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_CONTENT_LENGTH
,
2366 &lpwhr
->dwContentLength
,&dwBufferSize
,NULL
))
2367 lpwhr
->dwContentLength
= -1;
2369 if (lpwhr
->dwContentLength
== 0)
2370 HTTP_FinishedReading(lpwhr
);
2372 if (!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTO_REDIRECT
) && bSuccess
)
2374 DWORD dwCode
,dwCodeLength
=sizeof(DWORD
);
2375 WCHAR szNewLocation
[2048];
2376 dwBufferSize
=sizeof(szNewLocation
);
2377 if (HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_STATUS_CODE
,&dwCode
,&dwCodeLength
,NULL
) &&
2378 (dwCode
==HTTP_STATUS_REDIRECT
|| dwCode
==HTTP_STATUS_MOVED
) &&
2379 HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_LOCATION
,szNewLocation
,&dwBufferSize
,NULL
))
2381 HTTP_DrainContent(lpwhr
);
2382 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2383 INTERNET_STATUS_REDIRECT
, szNewLocation
,
2385 bSuccess
= HTTP_HandleRedirect(lpwhr
, szNewLocation
);
2388 HeapFree(GetProcessHeap(), 0, requestString
);
2402 HeapFree(GetProcessHeap(), 0, requestString
);
2404 /* TODO: send notification for P3P header */
2406 iar
.dwResult
= (DWORD
)bSuccess
;
2407 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
2409 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2410 INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
2411 sizeof(INTERNET_ASYNC_RESULT
));
2417 /***********************************************************************
2418 * HTTP_Connect (internal)
2420 * Create http session handle
2423 * HINTERNET a session handle on success
2427 HINTERNET
HTTP_Connect(LPWININETAPPINFOW hIC
, LPCWSTR lpszServerName
,
2428 INTERNET_PORT nServerPort
, LPCWSTR lpszUserName
,
2429 LPCWSTR lpszPassword
, DWORD dwFlags
, DWORD dwContext
,
2430 DWORD dwInternalFlags
)
2432 BOOL bSuccess
= FALSE
;
2433 LPWININETHTTPSESSIONW lpwhs
= NULL
;
2434 HINTERNET handle
= NULL
;
2438 assert( hIC
->hdr
.htype
== WH_HINIT
);
2440 hIC
->hdr
.dwContext
= dwContext
;
2442 lpwhs
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WININETHTTPSESSIONW
));
2445 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2450 * According to my tests. The name is not resolved until a request is sent
2453 lpwhs
->hdr
.htype
= WH_HHTTPSESSION
;
2454 lpwhs
->hdr
.dwFlags
= dwFlags
;
2455 lpwhs
->hdr
.dwContext
= dwContext
;
2456 lpwhs
->hdr
.dwInternalFlags
= dwInternalFlags
| (hIC
->hdr
.dwInternalFlags
& INET_CALLBACKW
);
2457 lpwhs
->hdr
.dwRefCount
= 1;
2458 lpwhs
->hdr
.destroy
= HTTP_CloseHTTPSessionHandle
;
2459 lpwhs
->hdr
.lpfnStatusCB
= hIC
->hdr
.lpfnStatusCB
;
2461 WININET_AddRef( &hIC
->hdr
);
2462 lpwhs
->lpAppInfo
= hIC
;
2464 handle
= WININET_AllocHandle( &lpwhs
->hdr
);
2467 ERR("Failed to alloc handle\n");
2468 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2472 if(hIC
->lpszProxy
&& hIC
->dwAccessType
== INTERNET_OPEN_TYPE_PROXY
) {
2473 if(strchrW(hIC
->lpszProxy
, ' '))
2474 FIXME("Several proxies not implemented.\n");
2475 if(hIC
->lpszProxyBypass
)
2476 FIXME("Proxy bypass is ignored.\n");
2478 if (lpszServerName
&& lpszServerName
[0])
2480 lpwhs
->lpszServerName
= WININET_strdupW(lpszServerName
);
2481 lpwhs
->lpszHostName
= WININET_strdupW(lpszServerName
);
2483 if (lpszUserName
&& lpszUserName
[0])
2484 lpwhs
->lpszUserName
= WININET_strdupW(lpszUserName
);
2485 lpwhs
->nServerPort
= nServerPort
;
2486 lpwhs
->nHostPort
= nServerPort
;
2488 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2489 if (!(lpwhs
->hdr
.dwInternalFlags
& INET_OPENURL
))
2491 INTERNET_SendCallback(&hIC
->hdr
, dwContext
,
2492 INTERNET_STATUS_HANDLE_CREATED
, &handle
,
2500 WININET_Release( &lpwhs
->hdr
);
2503 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
2507 TRACE("%p --> %p (%p)\n", hIC
, handle
, lpwhs
);
2512 /***********************************************************************
2513 * HTTP_OpenConnection (internal)
2515 * Connect to a web server
2522 static BOOL
HTTP_OpenConnection(LPWININETHTTPREQW lpwhr
)
2524 BOOL bSuccess
= FALSE
;
2525 LPWININETHTTPSESSIONW lpwhs
;
2526 LPWININETAPPINFOW hIC
= NULL
;
2532 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
2534 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2538 if (NETCON_connected(&lpwhr
->netConnection
))
2544 lpwhs
= lpwhr
->lpHttpSession
;
2546 hIC
= lpwhs
->lpAppInfo
;
2547 inet_ntop(lpwhs
->socketAddress
.sin_family
, &lpwhs
->socketAddress
.sin_addr
,
2548 szaddr
, sizeof(szaddr
));
2549 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2550 INTERNET_STATUS_CONNECTING_TO_SERVER
,
2554 if (!NETCON_create(&lpwhr
->netConnection
, lpwhs
->socketAddress
.sin_family
,
2557 WARN("Socket creation failed\n");
2561 if (!NETCON_connect(&lpwhr
->netConnection
, (struct sockaddr
*)&lpwhs
->socketAddress
,
2562 sizeof(lpwhs
->socketAddress
)))
2565 if (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
)
2567 /* Note: we differ from Microsoft's WinINet here. they seem to have
2568 * a bug that causes no status callbacks to be sent when starting
2569 * a tunnel to a proxy server using the CONNECT verb. i believe our
2570 * behaviour to be more correct and to not cause any incompatibilities
2571 * because using a secure connection through a proxy server is a rare
2572 * case that would be hard for anyone to depend on */
2573 if (hIC
->lpszProxy
&& !HTTP_SecureProxyConnect(lpwhr
))
2576 if (!NETCON_secure_connect(&lpwhr
->netConnection
, lpwhs
->lpszHostName
))
2578 WARN("Couldn't connect securely to host\n");
2583 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2584 INTERNET_STATUS_CONNECTED_TO_SERVER
,
2585 szaddr
, strlen(szaddr
)+1);
2590 TRACE("%d <--\n", bSuccess
);
2595 /***********************************************************************
2596 * HTTP_clear_response_headers (internal)
2598 * clear out any old response headers
2600 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr
)
2604 for( i
=0; i
<lpwhr
->nCustHeaders
; i
++)
2606 if( !lpwhr
->pCustHeaders
[i
].lpszField
)
2608 if( !lpwhr
->pCustHeaders
[i
].lpszValue
)
2610 if ( lpwhr
->pCustHeaders
[i
].wFlags
& HDR_ISREQUEST
)
2612 HTTP_DeleteCustomHeader( lpwhr
, i
);
2617 /***********************************************************************
2618 * HTTP_GetResponseHeaders (internal)
2620 * Read server response
2627 static INT
HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr
)
2630 WCHAR buffer
[MAX_REPLY_LEN
];
2631 DWORD buflen
= MAX_REPLY_LEN
;
2632 BOOL bSuccess
= FALSE
;
2634 static const WCHAR szCrLf
[] = {'\r','\n',0};
2635 char bufferA
[MAX_REPLY_LEN
];
2636 LPWSTR status_code
, status_text
;
2637 DWORD cchMaxRawHeaders
= 1024;
2638 LPWSTR lpszRawHeaders
= HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders
+1)*sizeof(WCHAR
));
2639 DWORD cchRawHeaders
= 0;
2643 /* clear old response headers (eg. from a redirect response) */
2644 HTTP_clear_response_headers( lpwhr
);
2646 if (!NETCON_connected(&lpwhr
->netConnection
))
2650 * HACK peek at the buffer
2652 NETCON_recv(&lpwhr
->netConnection
, buffer
, buflen
, MSG_PEEK
, &rc
);
2655 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
2657 buflen
= MAX_REPLY_LEN
;
2658 memset(buffer
, 0, MAX_REPLY_LEN
);
2659 if (!NETCON_getNextLine(&lpwhr
->netConnection
, bufferA
, &buflen
))
2661 MultiByteToWideChar( CP_ACP
, 0, bufferA
, buflen
, buffer
, MAX_REPLY_LEN
);
2663 /* regenerate raw headers */
2664 while (cchRawHeaders
+ buflen
+ strlenW(szCrLf
) > cchMaxRawHeaders
)
2666 cchMaxRawHeaders
*= 2;
2667 lpszRawHeaders
= HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders
, (cchMaxRawHeaders
+1)*sizeof(WCHAR
));
2669 memcpy(lpszRawHeaders
+cchRawHeaders
, buffer
, (buflen
-1)*sizeof(WCHAR
));
2670 cchRawHeaders
+= (buflen
-1);
2671 memcpy(lpszRawHeaders
+cchRawHeaders
, szCrLf
, sizeof(szCrLf
));
2672 cchRawHeaders
+= sizeof(szCrLf
)/sizeof(szCrLf
[0])-1;
2673 lpszRawHeaders
[cchRawHeaders
] = '\0';
2675 /* split the version from the status code */
2676 status_code
= strchrW( buffer
, ' ' );
2681 /* split the status code from the status text */
2682 status_text
= strchrW( status_code
, ' ' );
2687 TRACE("version [%s] status code [%s] status text [%s]\n",
2688 debugstr_w(buffer
), debugstr_w(status_code
), debugstr_w(status_text
) );
2690 HTTP_ProcessHeader(lpwhr
, szStatus
, status_code
,
2691 HTTP_ADDHDR_FLAG_REPLACE
);
2693 HeapFree(GetProcessHeap(),0,lpwhr
->lpszVersion
);
2694 HeapFree(GetProcessHeap(),0,lpwhr
->lpszStatusText
);
2696 lpwhr
->lpszVersion
= WININET_strdupW(buffer
);
2697 lpwhr
->lpszStatusText
= WININET_strdupW(status_text
);
2699 /* Parse each response line */
2702 buflen
= MAX_REPLY_LEN
;
2703 if (NETCON_getNextLine(&lpwhr
->netConnection
, bufferA
, &buflen
))
2705 LPWSTR
* pFieldAndValue
;
2707 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA
));
2708 MultiByteToWideChar( CP_ACP
, 0, bufferA
, buflen
, buffer
, MAX_REPLY_LEN
);
2710 while (cchRawHeaders
+ buflen
+ strlenW(szCrLf
) > cchMaxRawHeaders
)
2712 cchMaxRawHeaders
*= 2;
2713 lpszRawHeaders
= HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders
, (cchMaxRawHeaders
+1)*sizeof(WCHAR
));
2715 memcpy(lpszRawHeaders
+cchRawHeaders
, buffer
, (buflen
-1)*sizeof(WCHAR
));
2716 cchRawHeaders
+= (buflen
-1);
2717 memcpy(lpszRawHeaders
+cchRawHeaders
, szCrLf
, sizeof(szCrLf
));
2718 cchRawHeaders
+= sizeof(szCrLf
)/sizeof(szCrLf
[0])-1;
2719 lpszRawHeaders
[cchRawHeaders
] = '\0';
2721 pFieldAndValue
= HTTP_InterpretHttpHeader(buffer
);
2722 if (!pFieldAndValue
)
2725 HTTP_ProcessHeader(lpwhr
, pFieldAndValue
[0], pFieldAndValue
[1],
2726 HTTP_ADDREQ_FLAG_ADD
);
2728 HTTP_FreeTokens(pFieldAndValue
);
2738 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszRawHeaders
);
2739 lpwhr
->lpszRawHeaders
= lpszRawHeaders
;
2740 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders
));
2753 static void strip_spaces(LPWSTR start
)
2758 while (*str
== ' ' && *str
!= '\0')
2762 memmove(start
, str
, sizeof(WCHAR
) * (strlenW(str
) + 1));
2764 end
= start
+ strlenW(start
) - 1;
2765 while (end
>= start
&& *end
== ' ')
2773 /***********************************************************************
2774 * HTTP_InterpretHttpHeader (internal)
2776 * Parse server response
2780 * Pointer to array of field, value, NULL on success.
2783 static LPWSTR
* HTTP_InterpretHttpHeader(LPCWSTR buffer
)
2785 LPWSTR
* pTokenPair
;
2789 pTokenPair
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*pTokenPair
)*3);
2791 pszColon
= strchrW(buffer
, ':');
2792 /* must have two tokens */
2795 HTTP_FreeTokens(pTokenPair
);
2797 TRACE("No ':' in line: %s\n", debugstr_w(buffer
));
2801 pTokenPair
[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon
- buffer
+ 1) * sizeof(WCHAR
));
2804 HTTP_FreeTokens(pTokenPair
);
2807 memcpy(pTokenPair
[0], buffer
, (pszColon
- buffer
) * sizeof(WCHAR
));
2808 pTokenPair
[0][pszColon
- buffer
] = '\0';
2812 len
= strlenW(pszColon
);
2813 pTokenPair
[1] = HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
2816 HTTP_FreeTokens(pTokenPair
);
2819 memcpy(pTokenPair
[1], pszColon
, (len
+ 1) * sizeof(WCHAR
));
2821 strip_spaces(pTokenPair
[0]);
2822 strip_spaces(pTokenPair
[1]);
2824 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair
[0]), debugstr_w(pTokenPair
[1]));
2828 /***********************************************************************
2829 * HTTP_ProcessHeader (internal)
2831 * Stuff header into header tables according to <dwModifier>
2835 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2837 static BOOL
HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr
, LPCWSTR field
, LPCWSTR value
, DWORD dwModifier
)
2839 LPHTTPHEADERW lphttpHdr
= NULL
;
2840 BOOL bSuccess
= FALSE
;
2842 BOOL request_only
= dwModifier
& HTTP_ADDHDR_FLAG_REQ
;
2844 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field
), debugstr_w(value
), dwModifier
);
2846 /* REPLACE wins out over ADD */
2847 if (dwModifier
& HTTP_ADDHDR_FLAG_REPLACE
)
2848 dwModifier
&= ~HTTP_ADDHDR_FLAG_ADD
;
2850 if (dwModifier
& HTTP_ADDHDR_FLAG_ADD
)
2853 index
= HTTP_GetCustomHeaderIndex(lpwhr
, field
, 0, request_only
);
2857 if (dwModifier
& HTTP_ADDHDR_FLAG_ADD_IF_NEW
)
2861 lphttpHdr
= &lpwhr
->pCustHeaders
[index
];
2867 hdr
.lpszField
= (LPWSTR
)field
;
2868 hdr
.lpszValue
= (LPWSTR
)value
;
2869 hdr
.wFlags
= hdr
.wCount
= 0;
2871 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
2872 hdr
.wFlags
|= HDR_ISREQUEST
;
2874 return HTTP_InsertCustomHeader(lpwhr
, &hdr
);
2876 /* no value to delete */
2879 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
2880 lphttpHdr
->wFlags
|= HDR_ISREQUEST
;
2882 lphttpHdr
->wFlags
&= ~HDR_ISREQUEST
;
2884 if (dwModifier
& HTTP_ADDHDR_FLAG_REPLACE
)
2886 HTTP_DeleteCustomHeader( lpwhr
, index
);
2892 hdr
.lpszField
= (LPWSTR
)field
;
2893 hdr
.lpszValue
= (LPWSTR
)value
;
2894 hdr
.wFlags
= hdr
.wCount
= 0;
2896 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
2897 hdr
.wFlags
|= HDR_ISREQUEST
;
2899 return HTTP_InsertCustomHeader(lpwhr
, &hdr
);
2904 else if (dwModifier
& COALESCEFLASG
)
2909 INT origlen
= strlenW(lphttpHdr
->lpszValue
);
2910 INT valuelen
= strlenW(value
);
2912 if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
)
2915 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
2917 else if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON
)
2920 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
2923 len
= origlen
+ valuelen
+ ((ch
> 0) ? 2 : 0);
2925 lpsztmp
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, lphttpHdr
->lpszValue
, (len
+1)*sizeof(WCHAR
));
2928 lphttpHdr
->lpszValue
= lpsztmp
;
2929 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
2932 lphttpHdr
->lpszValue
[origlen
] = ch
;
2934 lphttpHdr
->lpszValue
[origlen
] = ' ';
2938 memcpy(&lphttpHdr
->lpszValue
[origlen
], value
, valuelen
*sizeof(WCHAR
));
2939 lphttpHdr
->lpszValue
[len
] = '\0';
2944 WARN("HeapReAlloc (%d bytes) failed\n",len
+1);
2945 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2948 TRACE("<-- %d\n",bSuccess
);
2953 /***********************************************************************
2954 * HTTP_CloseConnection (internal)
2956 * Close socket connection
2959 static VOID
HTTP_CloseConnection(LPWININETHTTPREQW lpwhr
)
2961 LPWININETHTTPSESSIONW lpwhs
= NULL
;
2962 LPWININETAPPINFOW hIC
= NULL
;
2964 TRACE("%p\n",lpwhr
);
2966 if (!NETCON_connected(&lpwhr
->netConnection
))
2969 lpwhs
= lpwhr
->lpHttpSession
;
2970 hIC
= lpwhs
->lpAppInfo
;
2972 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2973 INTERNET_STATUS_CLOSING_CONNECTION
, 0, 0);
2975 NETCON_close(&lpwhr
->netConnection
);
2977 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2978 INTERNET_STATUS_CONNECTION_CLOSED
, 0, 0);
2982 /***********************************************************************
2983 * HTTP_FinishedReading (internal)
2985 * Called when all content from server has been read by client.
2988 BOOL
HTTP_FinishedReading(LPWININETHTTPREQW lpwhr
)
2990 WCHAR szConnectionResponse
[20];
2991 DWORD dwBufferSize
= sizeof(szConnectionResponse
);
2995 if (!HTTP_HttpQueryInfoW(lpwhr
, HTTP_QUERY_CONNECTION
, szConnectionResponse
,
2996 &dwBufferSize
, NULL
) ||
2997 strcmpiW(szConnectionResponse
, szKeepAlive
))
2999 HTTP_CloseConnection(lpwhr
);
3002 /* FIXME: store data in the URL cache here */
3007 /***********************************************************************
3008 * HTTP_CloseHTTPRequestHandle (internal)
3010 * Deallocate request handle
3013 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr
)
3016 LPWININETHTTPREQW lpwhr
= (LPWININETHTTPREQW
) hdr
;
3020 WININET_Release(&lpwhr
->lpHttpSession
->hdr
);
3022 HTTP_CloseConnection(lpwhr
);
3024 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszPath
);
3025 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszVerb
);
3026 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszRawHeaders
);
3027 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszVersion
);
3028 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszStatusText
);
3030 for (i
= 0; i
< lpwhr
->nCustHeaders
; i
++)
3032 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
[i
].lpszField
);
3033 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
[i
].lpszValue
);
3036 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
);
3037 HeapFree(GetProcessHeap(), 0, lpwhr
);
3041 /***********************************************************************
3042 * HTTP_CloseHTTPSessionHandle (internal)
3044 * Deallocate session handle
3047 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr
)
3049 LPWININETHTTPSESSIONW lpwhs
= (LPWININETHTTPSESSIONW
) hdr
;
3051 TRACE("%p\n", lpwhs
);
3053 WININET_Release(&lpwhs
->lpAppInfo
->hdr
);
3055 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszHostName
);
3056 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszServerName
);
3057 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszUserName
);
3058 HeapFree(GetProcessHeap(), 0, lpwhs
);
3062 /***********************************************************************
3063 * HTTP_GetCustomHeaderIndex (internal)
3065 * Return index of custom header from header array
3068 static INT
HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszField
,
3069 int requested_index
, BOOL request_only
)
3073 TRACE("%s\n", debugstr_w(lpszField
));
3075 for (index
= 0; index
< lpwhr
->nCustHeaders
; index
++)
3077 if (strcmpiW(lpwhr
->pCustHeaders
[index
].lpszField
, lpszField
))
3080 if (request_only
&& !(lpwhr
->pCustHeaders
[index
].wFlags
& HDR_ISREQUEST
))
3083 if (!request_only
&& (lpwhr
->pCustHeaders
[index
].wFlags
& HDR_ISREQUEST
))
3086 if (requested_index
== 0)
3091 if (index
>= lpwhr
->nCustHeaders
)
3094 TRACE("Return: %d\n", index
);
3099 /***********************************************************************
3100 * HTTP_InsertCustomHeader (internal)
3102 * Insert header into array
3105 static BOOL
HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr
, LPHTTPHEADERW lpHdr
)
3108 LPHTTPHEADERW lph
= NULL
;
3111 TRACE("--> %s: %s\n", debugstr_w(lpHdr
->lpszField
), debugstr_w(lpHdr
->lpszValue
));
3112 count
= lpwhr
->nCustHeaders
+ 1;
3114 lph
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, lpwhr
->pCustHeaders
, sizeof(HTTPHEADERW
) * count
);
3116 lph
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(HTTPHEADERW
) * count
);
3120 lpwhr
->pCustHeaders
= lph
;
3121 lpwhr
->pCustHeaders
[count
-1].lpszField
= WININET_strdupW(lpHdr
->lpszField
);
3122 lpwhr
->pCustHeaders
[count
-1].lpszValue
= WININET_strdupW(lpHdr
->lpszValue
);
3123 lpwhr
->pCustHeaders
[count
-1].wFlags
= lpHdr
->wFlags
;
3124 lpwhr
->pCustHeaders
[count
-1].wCount
= lpHdr
->wCount
;
3125 lpwhr
->nCustHeaders
++;
3130 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
3137 /***********************************************************************
3138 * HTTP_DeleteCustomHeader (internal)
3140 * Delete header from array
3141 * If this function is called, the indexs may change.
3143 static BOOL
HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr
, DWORD index
)
3145 if( lpwhr
->nCustHeaders
<= 0 )
3147 if( index
>= lpwhr
->nCustHeaders
)
3149 lpwhr
->nCustHeaders
--;
3151 memmove( &lpwhr
->pCustHeaders
[index
], &lpwhr
->pCustHeaders
[index
+1],
3152 (lpwhr
->nCustHeaders
- index
)* sizeof(HTTPHEADERW
) );
3153 memset( &lpwhr
->pCustHeaders
[lpwhr
->nCustHeaders
], 0, sizeof(HTTPHEADERW
) );
3158 /***********************************************************************
3159 * IsHostInProxyBypassList (@)
3164 BOOL WINAPI
IsHostInProxyBypassList(DWORD flags
, LPCSTR szHost
, DWORD length
)
3166 FIXME("STUB: flags=%d host=%s length=%d\n",flags
,szHost
,length
);