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
10 * Copyright 2011 Jacek Caban for CodeWeavers
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
49 #define NO_SHLWAPI_STREAM
50 #define NO_SHLWAPI_REG
51 #define NO_SHLWAPI_STRFCNS
52 #define NO_SHLWAPI_GDI
59 #include "wine/debug.h"
60 #include "wine/exception.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 szOK
[] = {'O','K',0};
68 static const WCHAR hostW
[] = { 'H','o','s','t',0 };
69 static const WCHAR szAuthorization
[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
70 static const WCHAR szProxy_Authorization
[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
71 static const WCHAR szStatus
[] = { 'S','t','a','t','u','s',0 };
72 static const WCHAR szKeepAlive
[] = {'K','e','e','p','-','A','l','i','v','e',0};
73 static const WCHAR szGET
[] = { 'G','E','T', 0 };
74 static const WCHAR szHEAD
[] = { 'H','E','A','D', 0 };
76 static const WCHAR szAccept
[] = { 'A','c','c','e','p','t',0 };
77 static const WCHAR szAccept_Charset
[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
78 static const WCHAR szAccept_Encoding
[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
79 static const WCHAR szAccept_Language
[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
80 static const WCHAR szAccept_Ranges
[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
81 static const WCHAR szAge
[] = { 'A','g','e',0 };
82 static const WCHAR szAllow
[] = { 'A','l','l','o','w',0 };
83 static const WCHAR szCache_Control
[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
84 static const WCHAR szConnection
[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
85 static const WCHAR szContent_Base
[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
86 static const WCHAR szContent_Disposition
[] = { 'C','o','n','t','e','n','t','-','D','i','s','p','o','s','i','t','i','o','n',0 };
87 static const WCHAR szContent_Encoding
[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
88 static const WCHAR szContent_ID
[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
89 static const WCHAR szContent_Language
[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
90 static const WCHAR szContent_Length
[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
91 static const WCHAR szContent_Location
[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
92 static const WCHAR szContent_MD5
[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
93 static const WCHAR szContent_Range
[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
94 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 };
95 static const WCHAR szContent_Type
[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
96 static const WCHAR szCookie
[] = { 'C','o','o','k','i','e',0 };
97 static const WCHAR szDate
[] = { 'D','a','t','e',0 };
98 static const WCHAR szFrom
[] = { 'F','r','o','m',0 };
99 static const WCHAR szETag
[] = { 'E','T','a','g',0 };
100 static const WCHAR szExpect
[] = { 'E','x','p','e','c','t',0 };
101 static const WCHAR szExpires
[] = { 'E','x','p','i','r','e','s',0 };
102 static const WCHAR szIf_Match
[] = { 'I','f','-','M','a','t','c','h',0 };
103 static const WCHAR szIf_Modified_Since
[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
104 static const WCHAR szIf_None_Match
[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
105 static const WCHAR szIf_Range
[] = { 'I','f','-','R','a','n','g','e',0 };
106 static const WCHAR szIf_Unmodified_Since
[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
107 static const WCHAR szLast_Modified
[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
108 static const WCHAR szLocation
[] = { 'L','o','c','a','t','i','o','n',0 };
109 static const WCHAR szMax_Forwards
[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
110 static const WCHAR szMime_Version
[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
111 static const WCHAR szPragma
[] = { 'P','r','a','g','m','a',0 };
112 static const WCHAR szProxy_Authenticate
[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
113 static const WCHAR szProxy_Connection
[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
114 static const WCHAR szPublic
[] = { 'P','u','b','l','i','c',0 };
115 static const WCHAR szRange
[] = { 'R','a','n','g','e',0 };
116 static const WCHAR szReferer
[] = { 'R','e','f','e','r','e','r',0 };
117 static const WCHAR szRetry_After
[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
118 static const WCHAR szServer
[] = { 'S','e','r','v','e','r',0 };
119 static const WCHAR szSet_Cookie
[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
120 static const WCHAR szTransfer_Encoding
[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
121 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 };
122 static const WCHAR szUpgrade
[] = { 'U','p','g','r','a','d','e',0 };
123 static const WCHAR szURI
[] = { 'U','R','I',0 };
124 static const WCHAR szUser_Agent
[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
125 static const WCHAR szVary
[] = { 'V','a','r','y',0 };
126 static const WCHAR szVia
[] = { 'V','i','a',0 };
127 static const WCHAR szWarning
[] = { 'W','a','r','n','i','n','g',0 };
128 static const WCHAR szWWW_Authenticate
[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
130 static const WCHAR emptyW
[] = {0};
132 #define HTTP_REFERER szReferer
133 #define HTTP_ACCEPT szAccept
134 #define HTTP_USERAGENT szUser_Agent
136 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
137 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
138 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
139 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
140 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
141 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
142 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
144 #define COLLECT_TIME 60000
146 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
157 unsigned int auth_data_len
;
158 BOOL finished
; /* finished authenticating */
162 typedef struct _basicAuthorizationData
169 UINT authorizationLen
;
170 } basicAuthorizationData
;
172 typedef struct _authorizationData
186 static struct list basicAuthorizationCache
= LIST_INIT(basicAuthorizationCache
);
187 static struct list authorizationCache
= LIST_INIT(authorizationCache
);
189 static CRITICAL_SECTION authcache_cs
;
190 static CRITICAL_SECTION_DEBUG critsect_debug
=
193 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
194 0, 0, { (DWORD_PTR
)(__FILE__
": authcache_cs") }
196 static CRITICAL_SECTION authcache_cs
= { &critsect_debug
, -1, 0, 0, 0, 0 };
198 static DWORD
HTTP_GetResponseHeaders(http_request_t
*req
, INT
*len
);
199 static DWORD
HTTP_ProcessHeader(http_request_t
*req
, LPCWSTR field
, LPCWSTR value
, DWORD dwModifier
);
200 static LPWSTR
* HTTP_InterpretHttpHeader(LPCWSTR buffer
);
201 static DWORD
HTTP_InsertCustomHeader(http_request_t
*req
, LPHTTPHEADERW lpHdr
);
202 static INT
HTTP_GetCustomHeaderIndex(http_request_t
*req
, LPCWSTR lpszField
, INT index
, BOOL Request
);
203 static BOOL
HTTP_DeleteCustomHeader(http_request_t
*req
, DWORD index
);
204 static LPWSTR
HTTP_build_req( LPCWSTR
*list
, int len
);
205 static DWORD
HTTP_HttpQueryInfoW(http_request_t
*, DWORD
, LPVOID
, LPDWORD
, LPDWORD
);
206 static LPWSTR
HTTP_GetRedirectURL(http_request_t
*req
, LPCWSTR lpszUrl
);
207 static UINT
HTTP_DecodeBase64(LPCWSTR base64
, LPSTR bin
);
208 static BOOL
drain_content(http_request_t
*,BOOL
);
210 static CRITICAL_SECTION connection_pool_cs
;
211 static CRITICAL_SECTION_DEBUG connection_pool_debug
=
213 0, 0, &connection_pool_cs
,
214 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
215 0, 0, { (DWORD_PTR
)(__FILE__
": connection_pool_cs") }
217 static CRITICAL_SECTION connection_pool_cs
= { &connection_pool_debug
, -1, 0, 0, 0, 0 };
219 static struct list connection_pool
= LIST_INIT(connection_pool
);
220 static BOOL collector_running
;
222 void server_addref(server_t
*server
)
224 InterlockedIncrement(&server
->ref
);
227 void server_release(server_t
*server
)
229 if(InterlockedDecrement(&server
->ref
))
232 list_remove(&server
->entry
);
234 if(server
->cert_chain
)
235 CertFreeCertificateChain(server
->cert_chain
);
236 heap_free(server
->name
);
237 heap_free(server
->scheme_host_port
);
241 static BOOL
process_host_port(server_t
*server
)
247 static const WCHAR httpW
[] = {'h','t','t','p',0};
248 static const WCHAR httpsW
[] = {'h','t','t','p','s',0};
249 static const WCHAR formatW
[] = {'%','s',':','/','/','%','s',':','%','u',0};
251 name_len
= strlenW(server
->name
);
252 buf
= heap_alloc((name_len
+ 10 /* strlen("://:<port>") */)*sizeof(WCHAR
) + sizeof(httpsW
));
256 sprintfW(buf
, formatW
, server
->is_https
? httpsW
: httpW
, server
->name
, server
->port
);
257 server
->scheme_host_port
= buf
;
259 server
->host_port
= server
->scheme_host_port
+ 7 /* strlen("http://") */;
263 default_port
= server
->port
== (server
->is_https
? INTERNET_DEFAULT_HTTPS_PORT
: INTERNET_DEFAULT_HTTP_PORT
);
264 server
->canon_host_port
= default_port
? server
->name
: server
->host_port
;
268 server_t
*get_server(const WCHAR
*name
, INTERNET_PORT port
, BOOL is_https
, BOOL do_create
)
270 server_t
*iter
, *server
= NULL
;
272 if(port
== INTERNET_INVALID_PORT_NUMBER
)
273 port
= INTERNET_DEFAULT_HTTP_PORT
;
275 EnterCriticalSection(&connection_pool_cs
);
277 LIST_FOR_EACH_ENTRY(iter
, &connection_pool
, server_t
, entry
) {
278 if(iter
->port
== port
&& !strcmpW(iter
->name
, name
) && iter
->is_https
== is_https
) {
280 server_addref(server
);
285 if(!server
&& do_create
) {
286 server
= heap_alloc_zero(sizeof(*server
));
288 server
->ref
= 2; /* list reference and return */
290 server
->is_https
= is_https
;
291 list_init(&server
->conn_pool
);
292 server
->name
= heap_strdupW(name
);
293 if(server
->name
&& process_host_port(server
)) {
294 list_add_head(&connection_pool
, &server
->entry
);
302 LeaveCriticalSection(&connection_pool_cs
);
307 BOOL
collect_connections(collect_type_t collect_type
)
309 netconn_t
*netconn
, *netconn_safe
;
310 server_t
*server
, *server_safe
;
311 BOOL remaining
= FALSE
;
314 now
= GetTickCount64();
316 LIST_FOR_EACH_ENTRY_SAFE(server
, server_safe
, &connection_pool
, server_t
, entry
) {
317 LIST_FOR_EACH_ENTRY_SAFE(netconn
, netconn_safe
, &server
->conn_pool
, netconn_t
, pool_entry
) {
318 if(collect_type
> COLLECT_TIMEOUT
|| netconn
->keep_until
< now
) {
319 TRACE("freeing %p\n", netconn
);
320 list_remove(&netconn
->pool_entry
);
321 free_netconn(netconn
);
327 if(collect_type
== COLLECT_CLEANUP
) {
328 list_remove(&server
->entry
);
329 list_init(&server
->entry
);
330 server_release(server
);
337 static DWORD WINAPI
collect_connections_proc(void *arg
)
339 BOOL remaining_conns
;
342 /* FIXME: Use more sophisticated method */
345 EnterCriticalSection(&connection_pool_cs
);
347 remaining_conns
= collect_connections(COLLECT_TIMEOUT
);
349 collector_running
= FALSE
;
351 LeaveCriticalSection(&connection_pool_cs
);
352 }while(remaining_conns
);
354 FreeLibraryAndExitThread(WININET_hModule
, 0);
357 /***********************************************************************
358 * HTTP_GetHeader (internal)
360 * Headers section must be held
362 static LPHTTPHEADERW
HTTP_GetHeader(http_request_t
*req
, LPCWSTR head
)
365 HeaderIndex
= HTTP_GetCustomHeaderIndex(req
, head
, 0, TRUE
);
366 if (HeaderIndex
== -1)
369 return &req
->custHeaders
[HeaderIndex
];
372 static WCHAR
*get_host_header( http_request_t
*req
)
377 EnterCriticalSection( &req
->headers_section
);
378 if ((header
= HTTP_GetHeader( req
, hostW
))) ret
= heap_strdupW( header
->lpszValue
);
379 LeaveCriticalSection( &req
->headers_section
);
389 struct data_stream_vtbl_t
{
390 DWORD (*get_avail_data
)(data_stream_t
*,http_request_t
*);
391 BOOL (*end_of_data
)(data_stream_t
*,http_request_t
*);
392 DWORD (*read
)(data_stream_t
*,http_request_t
*,BYTE
*,DWORD
,DWORD
*,blocking_mode_t
);
393 BOOL (*drain_content
)(data_stream_t
*,http_request_t
*);
394 void (*destroy
)(data_stream_t
*);
398 data_stream_t data_stream
;
400 BYTE buf
[READ_BUFFER_SIZE
];
407 static inline void destroy_data_stream(data_stream_t
*stream
)
409 stream
->vtbl
->destroy(stream
);
412 static void reset_data_stream(http_request_t
*req
)
414 destroy_data_stream(req
->data_stream
);
415 req
->data_stream
= &req
->netconn_stream
.data_stream
;
416 req
->read_pos
= req
->read_size
= req
->netconn_stream
.content_read
= 0;
417 req
->read_gzip
= FALSE
;
420 static void remove_header( http_request_t
*request
, const WCHAR
*str
, BOOL from_request
)
423 EnterCriticalSection( &request
->headers_section
);
424 index
= HTTP_GetCustomHeaderIndex( request
, str
, 0, from_request
);
425 if (index
!= -1) HTTP_DeleteCustomHeader( request
, index
);
426 LeaveCriticalSection( &request
->headers_section
);
432 data_stream_t stream
;
433 data_stream_t
*parent_stream
;
435 BYTE buf
[READ_BUFFER_SIZE
];
441 static DWORD
gzip_get_avail_data(data_stream_t
*stream
, http_request_t
*req
)
443 /* Allow reading only from read buffer */
447 static BOOL
gzip_end_of_data(data_stream_t
*stream
, http_request_t
*req
)
449 gzip_stream_t
*gzip_stream
= (gzip_stream_t
*)stream
;
450 return gzip_stream
->end_of_data
451 || (!gzip_stream
->buf_size
&& gzip_stream
->parent_stream
->vtbl
->end_of_data(gzip_stream
->parent_stream
, req
));
454 static DWORD
gzip_read(data_stream_t
*stream
, http_request_t
*req
, BYTE
*buf
, DWORD size
,
455 DWORD
*read
, blocking_mode_t blocking_mode
)
457 gzip_stream_t
*gzip_stream
= (gzip_stream_t
*)stream
;
458 z_stream
*zstream
= &gzip_stream
->zstream
;
459 DWORD current_read
, ret_read
= 0;
461 DWORD res
= ERROR_SUCCESS
;
463 TRACE("(%d %d)\n", size
, blocking_mode
);
465 while(size
&& !gzip_stream
->end_of_data
) {
466 if(!gzip_stream
->buf_size
) {
467 if(gzip_stream
->buf_pos
) {
468 if(gzip_stream
->buf_size
)
469 memmove(gzip_stream
->buf
, gzip_stream
->buf
+gzip_stream
->buf_pos
, gzip_stream
->buf_size
);
470 gzip_stream
->buf_pos
= 0;
472 res
= gzip_stream
->parent_stream
->vtbl
->read(gzip_stream
->parent_stream
, req
, gzip_stream
->buf
+gzip_stream
->buf_size
,
473 sizeof(gzip_stream
->buf
)-gzip_stream
->buf_size
, ¤t_read
, blocking_mode
);
474 gzip_stream
->buf_size
+= current_read
;
475 if(res
!= ERROR_SUCCESS
)
479 if(blocking_mode
!= BLOCKING_DISALLOW
) {
480 WARN("unexpected end of data\n");
481 gzip_stream
->end_of_data
= TRUE
;
487 zstream
->next_in
= gzip_stream
->buf
+gzip_stream
->buf_pos
;
488 zstream
->avail_in
= gzip_stream
->buf_size
;
489 zstream
->next_out
= buf
+ret_read
;
490 zstream
->avail_out
= size
;
491 zres
= inflate(&gzip_stream
->zstream
, 0);
492 current_read
= size
- zstream
->avail_out
;
493 size
-= current_read
;
494 ret_read
+= current_read
;
495 gzip_stream
->buf_size
-= zstream
->next_in
- (gzip_stream
->buf
+gzip_stream
->buf_pos
);
496 gzip_stream
->buf_pos
= zstream
->next_in
-gzip_stream
->buf
;
497 if(zres
== Z_STREAM_END
) {
498 TRACE("end of data\n");
499 gzip_stream
->end_of_data
= TRUE
;
501 }else if(zres
!= Z_OK
) {
502 WARN("inflate failed %d: %s\n", zres
, debugstr_a(zstream
->msg
));
504 res
= ERROR_INTERNET_DECODING_FAILED
;
508 if(ret_read
&& blocking_mode
== BLOCKING_ALLOW
)
509 blocking_mode
= BLOCKING_DISALLOW
;
512 TRACE("read %u bytes\n", ret_read
);
517 static BOOL
gzip_drain_content(data_stream_t
*stream
, http_request_t
*req
)
519 gzip_stream_t
*gzip_stream
= (gzip_stream_t
*)stream
;
520 return gzip_stream
->parent_stream
->vtbl
->drain_content(gzip_stream
->parent_stream
, req
);
523 static void gzip_destroy(data_stream_t
*stream
)
525 gzip_stream_t
*gzip_stream
= (gzip_stream_t
*)stream
;
527 destroy_data_stream(gzip_stream
->parent_stream
);
529 if(!gzip_stream
->end_of_data
)
530 inflateEnd(&gzip_stream
->zstream
);
531 heap_free(gzip_stream
);
534 static const data_stream_vtbl_t gzip_stream_vtbl
= {
542 static voidpf
wininet_zalloc(voidpf opaque
, uInt items
, uInt size
)
544 return heap_alloc(items
*size
);
547 static void wininet_zfree(voidpf opaque
, voidpf address
)
552 static DWORD
init_gzip_stream(http_request_t
*req
, BOOL is_gzip
)
554 gzip_stream_t
*gzip_stream
;
557 gzip_stream
= heap_alloc_zero(sizeof(gzip_stream_t
));
559 return ERROR_OUTOFMEMORY
;
561 gzip_stream
->stream
.vtbl
= &gzip_stream_vtbl
;
562 gzip_stream
->zstream
.zalloc
= wininet_zalloc
;
563 gzip_stream
->zstream
.zfree
= wininet_zfree
;
565 zres
= inflateInit2(&gzip_stream
->zstream
, is_gzip
? 0x1f : -15);
567 ERR("inflateInit failed: %d\n", zres
);
568 heap_free(gzip_stream
);
569 return ERROR_OUTOFMEMORY
;
572 remove_header(req
, szContent_Length
, FALSE
);
575 memcpy(gzip_stream
->buf
, req
->read_buf
+req
->read_pos
, req
->read_size
);
576 gzip_stream
->buf_size
= req
->read_size
;
577 req
->read_pos
= req
->read_size
= 0;
580 req
->read_gzip
= TRUE
;
581 gzip_stream
->parent_stream
= req
->data_stream
;
582 req
->data_stream
= &gzip_stream
->stream
;
583 return ERROR_SUCCESS
;
588 static DWORD
init_gzip_stream(http_request_t
*req
, BOOL is_gzip
)
590 ERR("gzip stream not supported, missing zlib.\n");
591 return ERROR_SUCCESS
;
596 /***********************************************************************
597 * HTTP_FreeTokens (internal)
599 * Frees table of pointers.
601 static void HTTP_FreeTokens(LPWSTR
* token_array
)
604 for (i
= 0; token_array
[i
]; i
++) heap_free(token_array
[i
]);
605 heap_free(token_array
);
608 static void HTTP_FixURL(http_request_t
*request
)
610 static const WCHAR szSlash
[] = { '/',0 };
611 static const WCHAR szHttp
[] = { 'h','t','t','p',':','/','/', 0 };
613 /* If we don't have a path we set it to root */
614 if (NULL
== request
->path
)
615 request
->path
= heap_strdupW(szSlash
);
616 else /* remove \r and \n*/
618 int nLen
= strlenW(request
->path
);
619 while ((nLen
>0 ) && ((request
->path
[nLen
-1] == '\r')||(request
->path
[nLen
-1] == '\n')))
622 request
->path
[nLen
]='\0';
624 /* Replace '\' with '/' */
627 if (request
->path
[nLen
] == '\\') request
->path
[nLen
]='/';
631 if(CSTR_EQUAL
!= CompareStringW( LOCALE_INVARIANT
, NORM_IGNORECASE
,
632 request
->path
, strlenW(request
->path
), szHttp
, strlenW(szHttp
) )
633 && request
->path
[0] != '/') /* not an absolute path ?? --> fix it !! */
635 WCHAR
*fixurl
= heap_alloc((strlenW(request
->path
) + 2)*sizeof(WCHAR
));
637 strcpyW(fixurl
+ 1, request
->path
);
638 heap_free( request
->path
);
639 request
->path
= fixurl
;
643 static WCHAR
* build_request_header(http_request_t
*request
, const WCHAR
*verb
,
644 const WCHAR
*path
, const WCHAR
*version
, BOOL use_cr
)
646 static const WCHAR szSpace
[] = {' ',0};
647 static const WCHAR szColon
[] = {':',' ',0};
648 static const WCHAR szCr
[] = {'\r',0};
649 static const WCHAR szLf
[] = {'\n',0};
650 LPWSTR requestString
;
655 EnterCriticalSection( &request
->headers_section
);
657 /* allocate space for an array of all the string pointers to be added */
658 len
= request
->nCustHeaders
* 5 + 10;
659 if (!(req
= heap_alloc( len
* sizeof(const WCHAR
*) )))
661 LeaveCriticalSection( &request
->headers_section
);
665 /* add the verb, path and HTTP version string */
676 /* Append custom request headers */
677 for (i
= 0; i
< request
->nCustHeaders
; i
++)
679 if (request
->custHeaders
[i
].wFlags
& HDR_ISREQUEST
)
681 req
[n
++] = request
->custHeaders
[i
].lpszField
;
683 req
[n
++] = request
->custHeaders
[i
].lpszValue
;
688 TRACE("Adding custom header %s (%s)\n",
689 debugstr_w(request
->custHeaders
[i
].lpszField
),
690 debugstr_w(request
->custHeaders
[i
].lpszValue
));
698 requestString
= HTTP_build_req( req
, 4 );
700 LeaveCriticalSection( &request
->headers_section
);
701 return requestString
;
704 static WCHAR
* build_response_header(http_request_t
*request
, BOOL use_cr
)
706 static const WCHAR colonW
[] = { ':',' ',0 };
707 static const WCHAR crW
[] = { '\r',0 };
708 static const WCHAR lfW
[] = { '\n',0 };
709 static const WCHAR status_fmt
[] = { ' ','%','u',' ',0 };
714 EnterCriticalSection( &request
->headers_section
);
716 if (!(req
= heap_alloc( (request
->nCustHeaders
* 5 + 8) * sizeof(WCHAR
*) )))
718 LeaveCriticalSection( &request
->headers_section
);
722 if (request
->status_code
)
724 req
[n
++] = request
->version
;
725 sprintfW(buf
, status_fmt
, request
->status_code
);
727 req
[n
++] = request
->statusText
;
733 for(i
= 0; i
< request
->nCustHeaders
; i
++)
735 if(!(request
->custHeaders
[i
].wFlags
& HDR_ISREQUEST
)
736 && strcmpW(request
->custHeaders
[i
].lpszField
, szStatus
))
738 req
[n
++] = request
->custHeaders
[i
].lpszField
;
740 req
[n
++] = request
->custHeaders
[i
].lpszValue
;
745 TRACE("Adding custom header %s (%s)\n",
746 debugstr_w(request
->custHeaders
[i
].lpszField
),
747 debugstr_w(request
->custHeaders
[i
].lpszValue
));
755 ret
= HTTP_build_req(req
, 0);
757 LeaveCriticalSection( &request
->headers_section
);
761 static void HTTP_ProcessCookies( http_request_t
*request
)
765 LPHTTPHEADERW setCookieHeader
;
767 if(request
->hdr
.dwFlags
& INTERNET_FLAG_NO_COOKIES
)
770 EnterCriticalSection( &request
->headers_section
);
772 while((HeaderIndex
= HTTP_GetCustomHeaderIndex(request
, szSet_Cookie
, numCookies
++, FALSE
)) != -1)
778 setCookieHeader
= &request
->custHeaders
[HeaderIndex
];
780 if (!setCookieHeader
->lpszValue
)
783 host
= HTTP_GetHeader(request
, hostW
);
787 data
= strchrW(setCookieHeader
->lpszValue
, '=');
791 name
= heap_strndupW(setCookieHeader
->lpszValue
, data
-setCookieHeader
->lpszValue
);
796 set_cookie(host
->lpszValue
, request
->path
, name
, data
, INTERNET_COOKIE_HTTPONLY
);
800 LeaveCriticalSection( &request
->headers_section
);
803 static void strip_spaces(LPWSTR start
)
812 memmove(start
, str
, sizeof(WCHAR
) * (strlenW(str
) + 1));
814 end
= start
+ strlenW(start
) - 1;
815 while (end
>= start
&& *end
== ' ')
822 static inline BOOL
is_basic_auth_value( LPCWSTR pszAuthValue
, LPWSTR
*pszRealm
)
824 static const WCHAR szBasic
[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
825 static const WCHAR szRealm
[] = {'r','e','a','l','m'}; /* Note: not nul-terminated */
827 is_basic
= !strncmpiW(pszAuthValue
, szBasic
, ARRAYSIZE(szBasic
)) &&
828 ((pszAuthValue
[ARRAYSIZE(szBasic
)] == ' ') || !pszAuthValue
[ARRAYSIZE(szBasic
)]);
829 if (is_basic
&& pszRealm
)
832 LPCWSTR ptr
= &pszAuthValue
[ARRAYSIZE(szBasic
)];
836 token
= strchrW(ptr
,'=');
840 while (*realm
== ' ')
842 if(!strncmpiW(realm
, szRealm
, ARRAYSIZE(szRealm
)) &&
843 (realm
[ARRAYSIZE(szRealm
)] == ' ' || realm
[ARRAYSIZE(szRealm
)] == '='))
846 while (*token
== ' ')
850 *pszRealm
= heap_strdupW(token
);
851 strip_spaces(*pszRealm
);
858 static void destroy_authinfo( struct HttpAuthInfo
*authinfo
)
860 if (!authinfo
) return;
862 if (SecIsValidHandle(&authinfo
->ctx
))
863 DeleteSecurityContext(&authinfo
->ctx
);
864 if (SecIsValidHandle(&authinfo
->cred
))
865 FreeCredentialsHandle(&authinfo
->cred
);
867 heap_free(authinfo
->auth_data
);
868 heap_free(authinfo
->scheme
);
872 static UINT
retrieve_cached_basic_authorization(const WCHAR
*host
, const WCHAR
*realm
, char **auth_data
)
874 basicAuthorizationData
*ad
;
877 TRACE("Looking for authorization for %s:%s\n",debugstr_w(host
),debugstr_w(realm
));
879 EnterCriticalSection(&authcache_cs
);
880 LIST_FOR_EACH_ENTRY(ad
, &basicAuthorizationCache
, basicAuthorizationData
, entry
)
882 if (!strcmpiW(host
, ad
->host
) && (!realm
|| !strcmpW(realm
, ad
->realm
)))
884 TRACE("Authorization found in cache\n");
885 *auth_data
= heap_alloc(ad
->authorizationLen
);
886 memcpy(*auth_data
,ad
->authorization
,ad
->authorizationLen
);
887 rc
= ad
->authorizationLen
;
891 LeaveCriticalSection(&authcache_cs
);
895 static void cache_basic_authorization(LPWSTR host
, LPWSTR realm
, LPSTR auth_data
, UINT auth_data_len
)
898 basicAuthorizationData
* ad
= NULL
;
900 TRACE("caching authorization for %s:%s = %s\n",debugstr_w(host
),debugstr_w(realm
),debugstr_an(auth_data
,auth_data_len
));
902 EnterCriticalSection(&authcache_cs
);
903 LIST_FOR_EACH(cursor
, &basicAuthorizationCache
)
905 basicAuthorizationData
*check
= LIST_ENTRY(cursor
,basicAuthorizationData
,entry
);
906 if (!strcmpiW(host
,check
->host
) && !strcmpW(realm
,check
->realm
))
915 TRACE("Found match in cache, replacing\n");
916 heap_free(ad
->authorization
);
917 ad
->authorization
= heap_alloc(auth_data_len
);
918 memcpy(ad
->authorization
, auth_data
, auth_data_len
);
919 ad
->authorizationLen
= auth_data_len
;
923 ad
= heap_alloc(sizeof(basicAuthorizationData
));
924 ad
->host
= heap_strdupW(host
);
925 ad
->realm
= heap_strdupW(realm
);
926 ad
->authorization
= heap_alloc(auth_data_len
);
927 memcpy(ad
->authorization
, auth_data
, auth_data_len
);
928 ad
->authorizationLen
= auth_data_len
;
929 list_add_head(&basicAuthorizationCache
,&ad
->entry
);
930 TRACE("authorization cached\n");
932 LeaveCriticalSection(&authcache_cs
);
935 static BOOL
retrieve_cached_authorization(LPWSTR host
, LPWSTR scheme
,
936 SEC_WINNT_AUTH_IDENTITY_W
*nt_auth_identity
)
938 authorizationData
*ad
;
940 TRACE("Looking for authorization for %s:%s\n", debugstr_w(host
), debugstr_w(scheme
));
942 EnterCriticalSection(&authcache_cs
);
943 LIST_FOR_EACH_ENTRY(ad
, &authorizationCache
, authorizationData
, entry
) {
944 if(!strcmpiW(host
, ad
->host
) && !strcmpiW(scheme
, ad
->scheme
)) {
945 TRACE("Authorization found in cache\n");
947 nt_auth_identity
->User
= heap_strdupW(ad
->user
);
948 nt_auth_identity
->Password
= heap_strdupW(ad
->password
);
949 nt_auth_identity
->Domain
= heap_alloc(sizeof(WCHAR
)*ad
->domain_len
);
950 if(!nt_auth_identity
->User
|| !nt_auth_identity
->Password
||
951 (!nt_auth_identity
->Domain
&& ad
->domain_len
)) {
952 heap_free(nt_auth_identity
->User
);
953 heap_free(nt_auth_identity
->Password
);
954 heap_free(nt_auth_identity
->Domain
);
958 nt_auth_identity
->Flags
= SEC_WINNT_AUTH_IDENTITY_UNICODE
;
959 nt_auth_identity
->UserLength
= ad
->user_len
;
960 nt_auth_identity
->PasswordLength
= ad
->password_len
;
961 memcpy(nt_auth_identity
->Domain
, ad
->domain
, sizeof(WCHAR
)*ad
->domain_len
);
962 nt_auth_identity
->DomainLength
= ad
->domain_len
;
963 LeaveCriticalSection(&authcache_cs
);
967 LeaveCriticalSection(&authcache_cs
);
972 static void cache_authorization(LPWSTR host
, LPWSTR scheme
,
973 SEC_WINNT_AUTH_IDENTITY_W
*nt_auth_identity
)
975 authorizationData
*ad
;
978 TRACE("Caching authorization for %s:%s\n", debugstr_w(host
), debugstr_w(scheme
));
980 EnterCriticalSection(&authcache_cs
);
981 LIST_FOR_EACH_ENTRY(ad
, &authorizationCache
, authorizationData
, entry
)
982 if(!strcmpiW(host
, ad
->host
) && !strcmpiW(scheme
, ad
->scheme
)) {
989 heap_free(ad
->password
);
990 heap_free(ad
->domain
);
992 ad
= heap_alloc(sizeof(authorizationData
));
994 LeaveCriticalSection(&authcache_cs
);
998 ad
->host
= heap_strdupW(host
);
999 ad
->scheme
= heap_strdupW(scheme
);
1000 list_add_head(&authorizationCache
, &ad
->entry
);
1003 ad
->user
= heap_strndupW(nt_auth_identity
->User
, nt_auth_identity
->UserLength
);
1004 ad
->password
= heap_strndupW(nt_auth_identity
->Password
, nt_auth_identity
->PasswordLength
);
1005 ad
->domain
= heap_strndupW(nt_auth_identity
->Domain
, nt_auth_identity
->DomainLength
);
1006 ad
->user_len
= nt_auth_identity
->UserLength
;
1007 ad
->password_len
= nt_auth_identity
->PasswordLength
;
1008 ad
->domain_len
= nt_auth_identity
->DomainLength
;
1010 if(!ad
->host
|| !ad
->scheme
|| !ad
->user
|| !ad
->password
1011 || (nt_auth_identity
->Domain
&& !ad
->domain
)) {
1012 heap_free(ad
->host
);
1013 heap_free(ad
->scheme
);
1014 heap_free(ad
->user
);
1015 heap_free(ad
->password
);
1016 heap_free(ad
->domain
);
1017 list_remove(&ad
->entry
);
1021 LeaveCriticalSection(&authcache_cs
);
1024 static BOOL
HTTP_DoAuthorization( http_request_t
*request
, LPCWSTR pszAuthValue
,
1025 struct HttpAuthInfo
**ppAuthInfo
,
1026 LPWSTR domain_and_username
, LPWSTR password
,
1029 SECURITY_STATUS sec_status
;
1030 struct HttpAuthInfo
*pAuthInfo
= *ppAuthInfo
;
1032 LPWSTR szRealm
= NULL
;
1034 TRACE("%s\n", debugstr_w(pszAuthValue
));
1041 pAuthInfo
= heap_alloc(sizeof(*pAuthInfo
));
1045 SecInvalidateHandle(&pAuthInfo
->cred
);
1046 SecInvalidateHandle(&pAuthInfo
->ctx
);
1047 memset(&pAuthInfo
->exp
, 0, sizeof(pAuthInfo
->exp
));
1048 pAuthInfo
->attr
= 0;
1049 pAuthInfo
->auth_data
= NULL
;
1050 pAuthInfo
->auth_data_len
= 0;
1051 pAuthInfo
->finished
= FALSE
;
1053 if (is_basic_auth_value(pszAuthValue
,NULL
))
1055 static const WCHAR szBasic
[] = {'B','a','s','i','c',0};
1056 pAuthInfo
->scheme
= heap_strdupW(szBasic
);
1057 if (!pAuthInfo
->scheme
)
1059 heap_free(pAuthInfo
);
1066 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity
;
1068 pAuthInfo
->scheme
= heap_strdupW(pszAuthValue
);
1069 if (!pAuthInfo
->scheme
)
1071 heap_free(pAuthInfo
);
1075 if (domain_and_username
)
1077 WCHAR
*user
= strchrW(domain_and_username
, '\\');
1078 WCHAR
*domain
= domain_and_username
;
1080 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
1082 pAuthData
= &nt_auth_identity
;
1087 user
= domain_and_username
;
1091 nt_auth_identity
.Flags
= SEC_WINNT_AUTH_IDENTITY_UNICODE
;
1092 nt_auth_identity
.User
= user
;
1093 nt_auth_identity
.UserLength
= strlenW(nt_auth_identity
.User
);
1094 nt_auth_identity
.Domain
= domain
;
1095 nt_auth_identity
.DomainLength
= domain
? user
- domain
- 1 : 0;
1096 nt_auth_identity
.Password
= password
;
1097 nt_auth_identity
.PasswordLength
= strlenW(nt_auth_identity
.Password
);
1099 cache_authorization(host
, pAuthInfo
->scheme
, &nt_auth_identity
);
1101 else if(retrieve_cached_authorization(host
, pAuthInfo
->scheme
, &nt_auth_identity
))
1102 pAuthData
= &nt_auth_identity
;
1104 /* use default credentials */
1107 sec_status
= AcquireCredentialsHandleW(NULL
, pAuthInfo
->scheme
,
1108 SECPKG_CRED_OUTBOUND
, NULL
,
1110 NULL
, &pAuthInfo
->cred
,
1113 if(pAuthData
&& !domain_and_username
) {
1114 heap_free(nt_auth_identity
.User
);
1115 heap_free(nt_auth_identity
.Domain
);
1116 heap_free(nt_auth_identity
.Password
);
1119 if (sec_status
== SEC_E_OK
)
1121 PSecPkgInfoW sec_pkg_info
;
1122 sec_status
= QuerySecurityPackageInfoW(pAuthInfo
->scheme
, &sec_pkg_info
);
1123 if (sec_status
== SEC_E_OK
)
1125 pAuthInfo
->max_token
= sec_pkg_info
->cbMaxToken
;
1126 FreeContextBuffer(sec_pkg_info
);
1129 if (sec_status
!= SEC_E_OK
)
1131 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
1132 debugstr_w(pAuthInfo
->scheme
), sec_status
);
1133 heap_free(pAuthInfo
->scheme
);
1134 heap_free(pAuthInfo
);
1138 *ppAuthInfo
= pAuthInfo
;
1140 else if (pAuthInfo
->finished
)
1143 if ((strlenW(pszAuthValue
) < strlenW(pAuthInfo
->scheme
)) ||
1144 strncmpiW(pszAuthValue
, pAuthInfo
->scheme
, strlenW(pAuthInfo
->scheme
)))
1146 ERR("authentication scheme changed from %s to %s\n",
1147 debugstr_w(pAuthInfo
->scheme
), debugstr_w(pszAuthValue
));
1151 if (is_basic_auth_value(pszAuthValue
,&szRealm
))
1155 char *auth_data
= NULL
;
1156 UINT auth_data_len
= 0;
1158 TRACE("basic authentication realm %s\n",debugstr_w(szRealm
));
1160 if (!domain_and_username
)
1162 if (host
&& szRealm
)
1163 auth_data_len
= retrieve_cached_basic_authorization(host
, szRealm
,&auth_data
);
1164 if (auth_data_len
== 0)
1172 userlen
= WideCharToMultiByte(CP_UTF8
, 0, domain_and_username
, lstrlenW(domain_and_username
), NULL
, 0, NULL
, NULL
);
1173 passlen
= WideCharToMultiByte(CP_UTF8
, 0, password
, lstrlenW(password
), NULL
, 0, NULL
, NULL
);
1175 /* length includes a nul terminator, which will be re-used for the ':' */
1176 auth_data
= heap_alloc(userlen
+ 1 + passlen
);
1183 WideCharToMultiByte(CP_UTF8
, 0, domain_and_username
, -1, auth_data
, userlen
, NULL
, NULL
);
1184 auth_data
[userlen
] = ':';
1185 WideCharToMultiByte(CP_UTF8
, 0, password
, -1, &auth_data
[userlen
+1], passlen
, NULL
, NULL
);
1186 auth_data_len
= userlen
+ 1 + passlen
;
1187 if (host
&& szRealm
)
1188 cache_basic_authorization(host
, szRealm
, auth_data
, auth_data_len
);
1191 pAuthInfo
->auth_data
= auth_data
;
1192 pAuthInfo
->auth_data_len
= auth_data_len
;
1193 pAuthInfo
->finished
= TRUE
;
1199 LPCWSTR pszAuthData
;
1200 SecBufferDesc out_desc
, in_desc
;
1202 unsigned char *buffer
;
1203 ULONG context_req
= ISC_REQ_CONNECTION
| ISC_REQ_USE_DCE_STYLE
|
1204 ISC_REQ_MUTUAL_AUTH
| ISC_REQ_DELEGATE
;
1206 in
.BufferType
= SECBUFFER_TOKEN
;
1210 in_desc
.ulVersion
= 0;
1211 in_desc
.cBuffers
= 1;
1212 in_desc
.pBuffers
= &in
;
1214 pszAuthData
= pszAuthValue
+ strlenW(pAuthInfo
->scheme
);
1215 if (*pszAuthData
== ' ')
1218 in
.cbBuffer
= HTTP_DecodeBase64(pszAuthData
, NULL
);
1219 in
.pvBuffer
= heap_alloc(in
.cbBuffer
);
1220 HTTP_DecodeBase64(pszAuthData
, in
.pvBuffer
);
1223 buffer
= heap_alloc(pAuthInfo
->max_token
);
1225 out
.BufferType
= SECBUFFER_TOKEN
;
1226 out
.cbBuffer
= pAuthInfo
->max_token
;
1227 out
.pvBuffer
= buffer
;
1229 out_desc
.ulVersion
= 0;
1230 out_desc
.cBuffers
= 1;
1231 out_desc
.pBuffers
= &out
;
1233 sec_status
= InitializeSecurityContextW(first
? &pAuthInfo
->cred
: NULL
,
1234 first
? NULL
: &pAuthInfo
->ctx
,
1235 first
? request
->server
->name
: NULL
,
1236 context_req
, 0, SECURITY_NETWORK_DREP
,
1237 in
.pvBuffer
? &in_desc
: NULL
,
1238 0, &pAuthInfo
->ctx
, &out_desc
,
1239 &pAuthInfo
->attr
, &pAuthInfo
->exp
);
1240 if (sec_status
== SEC_E_OK
)
1242 pAuthInfo
->finished
= TRUE
;
1243 pAuthInfo
->auth_data
= out
.pvBuffer
;
1244 pAuthInfo
->auth_data_len
= out
.cbBuffer
;
1245 TRACE("sending last auth packet\n");
1247 else if (sec_status
== SEC_I_CONTINUE_NEEDED
)
1249 pAuthInfo
->auth_data
= out
.pvBuffer
;
1250 pAuthInfo
->auth_data_len
= out
.cbBuffer
;
1251 TRACE("sending next auth packet\n");
1255 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status
);
1256 heap_free(out
.pvBuffer
);
1257 destroy_authinfo(pAuthInfo
);
1266 /***********************************************************************
1267 * HTTP_HttpAddRequestHeadersW (internal)
1269 static DWORD
HTTP_HttpAddRequestHeadersW(http_request_t
*request
,
1270 LPCWSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
1275 DWORD len
, res
= ERROR_HTTP_INVALID_HEADER
;
1277 TRACE("copying header: %s\n", debugstr_wn(lpszHeader
, dwHeaderLength
));
1279 if( dwHeaderLength
== ~0U )
1280 len
= strlenW(lpszHeader
);
1282 len
= dwHeaderLength
;
1283 buffer
= heap_alloc(sizeof(WCHAR
)*(len
+1));
1284 lstrcpynW( buffer
, lpszHeader
, len
+ 1);
1290 LPWSTR
* pFieldAndValue
;
1292 lpszEnd
= lpszStart
;
1294 while (*lpszEnd
!= '\0')
1296 if (*lpszEnd
== '\r' || *lpszEnd
== '\n')
1301 if (*lpszStart
== '\0')
1304 if (*lpszEnd
== '\r' || *lpszEnd
== '\n')
1307 lpszEnd
++; /* Jump over newline */
1309 TRACE("interpreting header %s\n", debugstr_w(lpszStart
));
1310 if (*lpszStart
== '\0')
1312 /* Skip 0-length headers */
1313 lpszStart
= lpszEnd
;
1314 res
= ERROR_SUCCESS
;
1317 pFieldAndValue
= HTTP_InterpretHttpHeader(lpszStart
);
1320 res
= HTTP_ProcessHeader(request
, pFieldAndValue
[0],
1321 pFieldAndValue
[1], dwModifier
| HTTP_ADDHDR_FLAG_REQ
);
1322 HTTP_FreeTokens(pFieldAndValue
);
1325 lpszStart
= lpszEnd
;
1326 } while (res
== ERROR_SUCCESS
);
1332 /***********************************************************************
1333 * HttpAddRequestHeadersW (WININET.@)
1335 * Adds one or more HTTP header to the request handler
1338 * On Windows if dwHeaderLength includes the trailing '\0', then
1339 * HttpAddRequestHeadersW() adds it too. However this results in an
1340 * invalid HTTP header which is rejected by some servers so we probably
1341 * don't need to match Windows on that point.
1348 BOOL WINAPI
HttpAddRequestHeadersW(HINTERNET hHttpRequest
,
1349 LPCWSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
1351 http_request_t
*request
;
1352 DWORD res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
1354 TRACE("%p, %s, %i, %i\n", hHttpRequest
, debugstr_wn(lpszHeader
, dwHeaderLength
), dwHeaderLength
, dwModifier
);
1359 request
= (http_request_t
*) get_handle_object( hHttpRequest
);
1360 if (request
&& request
->hdr
.htype
== WH_HHTTPREQ
)
1361 res
= HTTP_HttpAddRequestHeadersW( request
, lpszHeader
, dwHeaderLength
, dwModifier
);
1363 WININET_Release( &request
->hdr
);
1365 if(res
!= ERROR_SUCCESS
)
1367 return res
== ERROR_SUCCESS
;
1370 /***********************************************************************
1371 * HttpAddRequestHeadersA (WININET.@)
1373 * Adds one or more HTTP header to the request handler
1380 BOOL WINAPI
HttpAddRequestHeadersA(HINTERNET hHttpRequest
,
1381 LPCSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
1387 TRACE("%p, %s, %i, %i\n", hHttpRequest
, debugstr_an(lpszHeader
, dwHeaderLength
), dwHeaderLength
, dwModifier
);
1389 len
= MultiByteToWideChar( CP_ACP
, 0, lpszHeader
, dwHeaderLength
, NULL
, 0 );
1390 hdr
= heap_alloc(len
*sizeof(WCHAR
));
1391 MultiByteToWideChar( CP_ACP
, 0, lpszHeader
, dwHeaderLength
, hdr
, len
);
1392 if( dwHeaderLength
!= ~0U )
1393 dwHeaderLength
= len
;
1395 r
= HttpAddRequestHeadersW( hHttpRequest
, hdr
, dwHeaderLength
, dwModifier
);
1401 static void free_accept_types( WCHAR
**accept_types
)
1403 WCHAR
*ptr
, **types
= accept_types
;
1406 while ((ptr
= *types
))
1411 heap_free( accept_types
);
1414 static WCHAR
**convert_accept_types( const char **accept_types
)
1417 const char **types
= accept_types
;
1419 BOOL invalid_pointer
= FALSE
;
1421 if (!types
) return NULL
;
1427 /* find out how many there are */
1428 if (*types
&& **types
)
1430 TRACE("accept type: %s\n", debugstr_a(*types
));
1436 WARN("invalid accept type pointer\n");
1437 invalid_pointer
= TRUE
;
1442 if (invalid_pointer
) return NULL
;
1443 if (!(typesW
= heap_alloc( sizeof(WCHAR
*) * (count
+ 1) ))) return NULL
;
1445 types
= accept_types
;
1448 if (*types
&& **types
) typesW
[count
++] = heap_strdupAtoW( *types
);
1451 typesW
[count
] = NULL
;
1455 /***********************************************************************
1456 * HttpOpenRequestA (WININET.@)
1458 * Open a HTTP request handle
1461 * HINTERNET a HTTP request handle on success
1465 HINTERNET WINAPI
HttpOpenRequestA(HINTERNET hHttpSession
,
1466 LPCSTR lpszVerb
, LPCSTR lpszObjectName
, LPCSTR lpszVersion
,
1467 LPCSTR lpszReferrer
, LPCSTR
*lpszAcceptTypes
,
1468 DWORD dwFlags
, DWORD_PTR dwContext
)
1470 LPWSTR szVerb
= NULL
, szObjectName
= NULL
;
1471 LPWSTR szVersion
= NULL
, szReferrer
= NULL
, *szAcceptTypes
= NULL
;
1472 HINTERNET rc
= NULL
;
1474 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession
,
1475 debugstr_a(lpszVerb
), debugstr_a(lpszObjectName
),
1476 debugstr_a(lpszVersion
), debugstr_a(lpszReferrer
), lpszAcceptTypes
,
1477 dwFlags
, dwContext
);
1481 szVerb
= heap_strdupAtoW(lpszVerb
);
1488 szObjectName
= heap_strdupAtoW(lpszObjectName
);
1489 if ( !szObjectName
)
1495 szVersion
= heap_strdupAtoW(lpszVersion
);
1502 szReferrer
= heap_strdupAtoW(lpszReferrer
);
1507 szAcceptTypes
= convert_accept_types( lpszAcceptTypes
);
1508 rc
= HttpOpenRequestW(hHttpSession
, szVerb
, szObjectName
, szVersion
, szReferrer
,
1509 (const WCHAR
**)szAcceptTypes
, dwFlags
, dwContext
);
1512 free_accept_types(szAcceptTypes
);
1513 heap_free(szReferrer
);
1514 heap_free(szVersion
);
1515 heap_free(szObjectName
);
1520 /***********************************************************************
1523 static UINT
HTTP_EncodeBase64( LPCSTR bin
, unsigned int len
, LPWSTR base64
)
1526 static const CHAR HTTP_Base64Enc
[] =
1527 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1531 /* first 6 bits, all from bin[0] */
1532 base64
[n
++] = HTTP_Base64Enc
[(bin
[0] & 0xfc) >> 2];
1533 x
= (bin
[0] & 3) << 4;
1535 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1538 base64
[n
++] = HTTP_Base64Enc
[x
];
1543 base64
[n
++] = HTTP_Base64Enc
[ x
| ( (bin
[1]&0xf0) >> 4 ) ];
1544 x
= ( bin
[1] & 0x0f ) << 2;
1546 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1549 base64
[n
++] = HTTP_Base64Enc
[x
];
1553 base64
[n
++] = HTTP_Base64Enc
[ x
| ( (bin
[2]&0xc0 ) >> 6 ) ];
1555 /* last 6 bits, all from bin [2] */
1556 base64
[n
++] = HTTP_Base64Enc
[ bin
[2] & 0x3f ];
1564 static const signed char HTTP_Base64Dec
[] =
1566 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00 */
1567 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10 */
1568 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20 */
1569 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 0x30 */
1570 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40 */
1571 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50 */
1572 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60 */
1573 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 0x70 */
1576 /***********************************************************************
1579 static UINT
HTTP_DecodeBase64( LPCWSTR base64
, LPSTR bin
)
1587 if (base64
[0] >= ARRAYSIZE(HTTP_Base64Dec
) ||
1588 ((in
[0] = HTTP_Base64Dec
[base64
[0]]) == -1) ||
1589 base64
[1] >= ARRAYSIZE(HTTP_Base64Dec
) ||
1590 ((in
[1] = HTTP_Base64Dec
[base64
[1]]) == -1))
1592 WARN("invalid base64: %s\n", debugstr_w(base64
));
1596 bin
[n
] = (unsigned char) (in
[0] << 2 | in
[1] >> 4);
1599 if ((base64
[2] == '=') && (base64
[3] == '='))
1601 if (base64
[2] > ARRAYSIZE(HTTP_Base64Dec
) ||
1602 ((in
[2] = HTTP_Base64Dec
[base64
[2]]) == -1))
1604 WARN("invalid base64: %s\n", debugstr_w(&base64
[2]));
1608 bin
[n
] = (unsigned char) (in
[1] << 4 | in
[2] >> 2);
1611 if (base64
[3] == '=')
1613 if (base64
[3] > ARRAYSIZE(HTTP_Base64Dec
) ||
1614 ((in
[3] = HTTP_Base64Dec
[base64
[3]]) == -1))
1616 WARN("invalid base64: %s\n", debugstr_w(&base64
[3]));
1620 bin
[n
] = (unsigned char) (((in
[2] << 6) & 0xc0) | in
[3]);
1629 static WCHAR
*encode_auth_data( const WCHAR
*scheme
, const char *data
, UINT data_len
)
1632 UINT len
, scheme_len
= strlenW( scheme
);
1634 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1635 len
= scheme_len
+ 1 + ((data_len
+ 2) * 4) / 3;
1636 if (!(ret
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
1637 memcpy( ret
, scheme
, scheme_len
* sizeof(WCHAR
) );
1638 ret
[scheme_len
] = ' ';
1639 HTTP_EncodeBase64( data
, data_len
, ret
+ scheme_len
+ 1 );
1644 /***********************************************************************
1645 * HTTP_InsertAuthorization
1647 * Insert or delete the authorization field in the request header.
1649 static BOOL
HTTP_InsertAuthorization( http_request_t
*request
, struct HttpAuthInfo
*pAuthInfo
, LPCWSTR header
)
1651 static const WCHAR wszBasic
[] = {'B','a','s','i','c',0};
1652 WCHAR
*host
, *authorization
= NULL
;
1656 if (pAuthInfo
->auth_data_len
)
1658 if (!(authorization
= encode_auth_data(pAuthInfo
->scheme
, pAuthInfo
->auth_data
, pAuthInfo
->auth_data_len
)))
1661 /* clear the data as it isn't valid now that it has been sent to the
1662 * server, unless it's Basic authentication which doesn't do
1663 * connection tracking */
1664 if (strcmpiW(pAuthInfo
->scheme
, wszBasic
))
1666 heap_free(pAuthInfo
->auth_data
);
1667 pAuthInfo
->auth_data
= NULL
;
1668 pAuthInfo
->auth_data_len
= 0;
1672 TRACE("Inserting authorization: %s\n", debugstr_w(authorization
));
1674 HTTP_ProcessHeader(request
, header
, authorization
,
1675 HTTP_ADDHDR_FLAG_REQ
| HTTP_ADDHDR_FLAG_REPLACE
| HTTP_ADDREQ_FLAG_ADD
);
1676 heap_free(authorization
);
1678 else if (!strcmpW(header
, szAuthorization
) && (host
= get_host_header(request
)))
1683 if ((data_len
= retrieve_cached_basic_authorization(host
, NULL
, &data
)))
1685 TRACE("Found cached basic authorization for %s\n", debugstr_w(host
));
1687 if (!(authorization
= encode_auth_data(wszBasic
, data
, data_len
)))
1694 TRACE("Inserting authorization: %s\n", debugstr_w(authorization
));
1696 HTTP_ProcessHeader(request
, header
, authorization
,
1697 HTTP_ADDHDR_FLAG_REQ
| HTTP_ADDHDR_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_ADD
);
1699 heap_free(authorization
);
1706 static WCHAR
*build_proxy_path_url(http_request_t
*req
)
1711 len
= strlenW(req
->server
->scheme_host_port
);
1712 size
= len
+ strlenW(req
->path
) + 1;
1713 if(*req
->path
!= '/')
1715 url
= heap_alloc(size
* sizeof(WCHAR
));
1719 memcpy(url
, req
->server
->scheme_host_port
, len
*sizeof(WCHAR
));
1720 if(*req
->path
!= '/')
1723 strcpyW(url
+len
, req
->path
);
1725 TRACE("url=%s\n", debugstr_w(url
));
1729 static BOOL
HTTP_DomainMatches(LPCWSTR server
, LPCWSTR domain
)
1731 static const WCHAR localW
[] = { '<','l','o','c','a','l','>',0 };
1734 if (!strcmpiW( domain
, localW
) && !strchrW( server
, '.' ))
1736 else if (*domain
== '*')
1738 if (domain
[1] == '.')
1742 /* For a hostname to match a wildcard, the last domain must match
1743 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
1744 * hostname is www.foo.a.b, it matches, but a.b does not.
1746 dot
= strchrW( server
, '.' );
1749 int len
= strlenW( dot
+ 1 );
1751 if (len
> strlenW( domain
+ 2 ))
1755 /* The server's domain is longer than the wildcard, so it
1756 * could be a subdomain. Compare the last portion of the
1759 ptr
= dot
+ len
+ 1 - strlenW( domain
+ 2 );
1760 if (!strcmpiW( ptr
, domain
+ 2 ))
1762 /* This is only a match if the preceding character is
1763 * a '.', i.e. that it is a matching domain. E.g.
1764 * if domain is '*.b.c' and server is 'www.ab.c' they
1767 ret
= *(ptr
- 1) == '.';
1771 ret
= !strcmpiW( dot
+ 1, domain
+ 2 );
1776 ret
= !strcmpiW( server
, domain
);
1780 static BOOL
HTTP_ShouldBypassProxy(appinfo_t
*lpwai
, LPCWSTR server
)
1785 if (!lpwai
->proxyBypass
) return FALSE
;
1786 ptr
= lpwai
->proxyBypass
;
1790 ptr
= strchrW( ptr
, ';' );
1792 ptr
= strchrW( tmp
, ' ' );
1795 if (ptr
- tmp
< INTERNET_MAX_HOST_NAME_LENGTH
)
1797 WCHAR domain
[INTERNET_MAX_HOST_NAME_LENGTH
];
1799 memcpy( domain
, tmp
, (ptr
- tmp
) * sizeof(WCHAR
) );
1800 domain
[ptr
- tmp
] = 0;
1801 ret
= HTTP_DomainMatches( server
, domain
);
1806 ret
= HTTP_DomainMatches( server
, tmp
);
1807 } while (ptr
&& !ret
);
1811 /***********************************************************************
1812 * HTTP_DealWithProxy
1814 static BOOL
HTTP_DealWithProxy(appinfo_t
*hIC
, http_session_t
*session
, http_request_t
*request
)
1816 static const WCHAR protoHttp
[] = { 'h','t','t','p',0 };
1817 static const WCHAR szHttp
[] = { 'h','t','t','p',':','/','/',0 };
1818 static const WCHAR szFormat
[] = { 'h','t','t','p',':','/','/','%','s',0 };
1819 WCHAR buf
[INTERNET_MAX_HOST_NAME_LENGTH
];
1820 WCHAR protoProxy
[INTERNET_MAX_URL_LENGTH
];
1821 DWORD protoProxyLen
= INTERNET_MAX_URL_LENGTH
;
1822 WCHAR proxy
[INTERNET_MAX_URL_LENGTH
];
1823 static WCHAR szNul
[] = { 0 };
1824 URL_COMPONENTSW UrlComponents
;
1825 server_t
*new_server
;
1828 memset( &UrlComponents
, 0, sizeof UrlComponents
);
1829 UrlComponents
.dwStructSize
= sizeof UrlComponents
;
1830 UrlComponents
.lpszHostName
= buf
;
1831 UrlComponents
.dwHostNameLength
= INTERNET_MAX_HOST_NAME_LENGTH
;
1833 if (!INTERNET_FindProxyForProtocol(hIC
->proxy
, protoHttp
, protoProxy
, &protoProxyLen
))
1835 if( CSTR_EQUAL
!= CompareStringW(LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
,
1836 protoProxy
,strlenW(szHttp
),szHttp
,strlenW(szHttp
)) )
1837 sprintfW(proxy
, szFormat
, protoProxy
);
1839 strcpyW(proxy
, protoProxy
);
1840 if( !InternetCrackUrlW(proxy
, 0, 0, &UrlComponents
) )
1842 if( UrlComponents
.dwHostNameLength
== 0 )
1845 if( !request
->path
)
1846 request
->path
= szNul
;
1848 is_https
= (UrlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
);
1849 if (is_https
&& UrlComponents
.nPort
== INTERNET_INVALID_PORT_NUMBER
)
1850 UrlComponents
.nPort
= INTERNET_DEFAULT_HTTPS_PORT
;
1852 new_server
= get_server(UrlComponents
.lpszHostName
, UrlComponents
.nPort
, is_https
, TRUE
);
1856 request
->proxy
= new_server
;
1858 TRACE("proxy server=%s port=%d\n", debugstr_w(new_server
->name
), new_server
->port
);
1862 static DWORD
HTTP_ResolveName(http_request_t
*request
)
1864 server_t
*server
= request
->proxy
? request
->proxy
: request
->server
;
1867 if(server
->addr_len
)
1868 return ERROR_SUCCESS
;
1870 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
1871 INTERNET_STATUS_RESOLVING_NAME
,
1873 (strlenW(server
->name
)+1) * sizeof(WCHAR
));
1875 addr_len
= sizeof(server
->addr
);
1876 if (!GetAddress(server
->name
, server
->port
, (SOCKADDR
*)&server
->addr
, &addr_len
, server
->addr_str
))
1877 return ERROR_INTERNET_NAME_NOT_RESOLVED
;
1879 server
->addr_len
= addr_len
;
1880 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
1881 INTERNET_STATUS_NAME_RESOLVED
,
1882 server
->addr_str
, strlen(server
->addr_str
)+1);
1884 TRACE("resolved %s to %s\n", debugstr_w(server
->name
), server
->addr_str
);
1885 return ERROR_SUCCESS
;
1888 static BOOL
HTTP_GetRequestURL(http_request_t
*req
, LPWSTR buf
)
1890 static const WCHAR http
[] = { 'h','t','t','p',':','/','/',0 };
1891 static const WCHAR https
[] = { 'h','t','t','p','s',':','/','/',0 };
1892 static const WCHAR slash
[] = { '/',0 };
1893 LPHTTPHEADERW host_header
;
1896 host_header
= HTTP_GetHeader(req
, hostW
);
1900 EnterCriticalSection( &req
->headers_section
);
1902 if (req
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
)
1906 strcpyW(buf
, scheme
);
1907 strcatW(buf
, host_header
->lpszValue
);
1908 if (req
->path
[0] != '/')
1909 strcatW(buf
, slash
);
1910 strcatW(buf
, req
->path
);
1912 LeaveCriticalSection( &req
->headers_section
);
1917 /***********************************************************************
1918 * HTTPREQ_Destroy (internal)
1920 * Deallocate request handle
1923 static void HTTPREQ_Destroy(object_header_t
*hdr
)
1925 http_request_t
*request
= (http_request_t
*) hdr
;
1930 if(request
->hCacheFile
)
1931 CloseHandle(request
->hCacheFile
);
1932 if(request
->req_file
)
1933 req_file_release(request
->req_file
);
1935 request
->headers_section
.DebugInfo
->Spare
[0] = 0;
1936 DeleteCriticalSection( &request
->headers_section
);
1937 request
->read_section
.DebugInfo
->Spare
[0] = 0;
1938 DeleteCriticalSection( &request
->read_section
);
1939 WININET_Release(&request
->session
->hdr
);
1941 destroy_authinfo(request
->authInfo
);
1942 destroy_authinfo(request
->proxyAuthInfo
);
1945 server_release(request
->server
);
1947 server_release(request
->proxy
);
1949 heap_free(request
->path
);
1950 heap_free(request
->verb
);
1951 heap_free(request
->version
);
1952 heap_free(request
->statusText
);
1954 for (i
= 0; i
< request
->nCustHeaders
; i
++)
1956 heap_free(request
->custHeaders
[i
].lpszField
);
1957 heap_free(request
->custHeaders
[i
].lpszValue
);
1959 destroy_data_stream(request
->data_stream
);
1960 heap_free(request
->custHeaders
);
1963 static void http_release_netconn(http_request_t
*req
, BOOL reuse
)
1965 TRACE("%p %p %x\n",req
, req
->netconn
, reuse
);
1967 if(!is_valid_netconn(req
->netconn
))
1970 if(reuse
&& req
->netconn
->keep_alive
) {
1973 EnterCriticalSection(&connection_pool_cs
);
1975 list_add_head(&req
->netconn
->server
->conn_pool
, &req
->netconn
->pool_entry
);
1976 req
->netconn
->keep_until
= GetTickCount64() + COLLECT_TIME
;
1977 req
->netconn
= NULL
;
1979 run_collector
= !collector_running
;
1980 collector_running
= TRUE
;
1982 LeaveCriticalSection(&connection_pool_cs
);
1985 HANDLE thread
= NULL
;
1988 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
, (const WCHAR
*)WININET_hModule
, &module
);
1990 thread
= CreateThread(NULL
, 0, collect_connections_proc
, NULL
, 0, NULL
);
1992 EnterCriticalSection(&connection_pool_cs
);
1993 collector_running
= FALSE
;
1994 LeaveCriticalSection(&connection_pool_cs
);
1997 FreeLibrary(module
);
2000 CloseHandle(thread
);
2005 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
,
2006 INTERNET_STATUS_CLOSING_CONNECTION
, 0, 0);
2008 close_netconn(req
->netconn
);
2010 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
,
2011 INTERNET_STATUS_CONNECTION_CLOSED
, 0, 0);
2014 static BOOL
HTTP_KeepAlive(http_request_t
*request
)
2016 WCHAR szVersion
[10];
2017 WCHAR szConnectionResponse
[20];
2018 DWORD dwBufferSize
= sizeof(szVersion
);
2019 BOOL keepalive
= FALSE
;
2021 /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
2022 * the connection is keep-alive by default */
2023 if (HTTP_HttpQueryInfoW(request
, HTTP_QUERY_VERSION
, szVersion
, &dwBufferSize
, NULL
) == ERROR_SUCCESS
2024 && !strcmpiW(szVersion
, g_szHttp1_1
))
2029 dwBufferSize
= sizeof(szConnectionResponse
);
2030 if (HTTP_HttpQueryInfoW(request
, HTTP_QUERY_PROXY_CONNECTION
, szConnectionResponse
, &dwBufferSize
, NULL
) == ERROR_SUCCESS
2031 || HTTP_HttpQueryInfoW(request
, HTTP_QUERY_CONNECTION
, szConnectionResponse
, &dwBufferSize
, NULL
) == ERROR_SUCCESS
)
2033 keepalive
= !strcmpiW(szConnectionResponse
, szKeepAlive
);
2039 static void HTTPREQ_CloseConnection(object_header_t
*hdr
)
2041 http_request_t
*req
= (http_request_t
*)hdr
;
2043 http_release_netconn(req
, drain_content(req
, FALSE
));
2046 static DWORD
str_to_buffer(const WCHAR
*str
, void *buffer
, DWORD
*size
, BOOL unicode
)
2051 WCHAR
*buf
= buffer
;
2053 if (str
) len
= strlenW(str
);
2055 if (*size
< (len
+ 1) * sizeof(WCHAR
))
2057 *size
= (len
+ 1) * sizeof(WCHAR
);
2058 return ERROR_INSUFFICIENT_BUFFER
;
2060 if (str
) strcpyW(buf
, str
);
2064 return ERROR_SUCCESS
;
2070 if (str
) len
= WideCharToMultiByte(CP_ACP
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
2075 return ERROR_INSUFFICIENT_BUFFER
;
2077 if (str
) WideCharToMultiByte(CP_ACP
, 0, str
, -1, buf
, *size
, NULL
, NULL
);
2081 return ERROR_SUCCESS
;
2085 static DWORD
HTTPREQ_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
2087 http_request_t
*req
= (http_request_t
*)hdr
;
2090 case INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO
:
2092 INTERNET_DIAGNOSTIC_SOCKET_INFO
*info
= buffer
;
2094 FIXME("INTERNET_DIAGNOSTIC_SOCKET_INFO stub\n");
2096 if (*size
< sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO
))
2097 return ERROR_INSUFFICIENT_BUFFER
;
2098 *size
= sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO
);
2099 /* FIXME: can't get a SOCKET from our connection since we don't use
2103 /* FIXME: get source port from req->netConnection */
2104 info
->SourcePort
= 0;
2105 info
->DestPort
= req
->server
->port
;
2107 if (HTTP_KeepAlive(req
))
2108 info
->Flags
|= IDSI_FLAG_KEEP_ALIVE
;
2110 info
->Flags
|= IDSI_FLAG_PROXY
;
2111 if (is_valid_netconn(req
->netconn
) && req
->netconn
->secure
)
2112 info
->Flags
|= IDSI_FLAG_SECURE
;
2114 return ERROR_SUCCESS
;
2118 TRACE("Queried undocumented option 98, forwarding to INTERNET_OPTION_SECURITY_FLAGS\n");
2120 case INTERNET_OPTION_SECURITY_FLAGS
:
2124 if (*size
< sizeof(ULONG
))
2125 return ERROR_INSUFFICIENT_BUFFER
;
2127 *size
= sizeof(DWORD
);
2128 flags
= is_valid_netconn(req
->netconn
) ? req
->netconn
->security_flags
: req
->security_flags
| req
->server
->security_flags
;
2129 *(DWORD
*)buffer
= flags
;
2131 TRACE("INTERNET_OPTION_SECURITY_FLAGS %x\n", flags
);
2132 return ERROR_SUCCESS
;
2135 case INTERNET_OPTION_HANDLE_TYPE
:
2136 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
2138 if (*size
< sizeof(ULONG
))
2139 return ERROR_INSUFFICIENT_BUFFER
;
2141 *size
= sizeof(DWORD
);
2142 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_HTTP_REQUEST
;
2143 return ERROR_SUCCESS
;
2145 case INTERNET_OPTION_URL
: {
2146 static const WCHAR httpW
[] = {'h','t','t','p',':','/','/',0};
2147 WCHAR url
[INTERNET_MAX_URL_LENGTH
];
2150 TRACE("INTERNET_OPTION_URL\n");
2152 EnterCriticalSection( &req
->headers_section
);
2153 host
= HTTP_GetHeader(req
, hostW
);
2154 strcpyW(url
, httpW
);
2155 strcatW(url
, host
->lpszValue
);
2156 strcatW(url
, req
->path
);
2157 LeaveCriticalSection( &req
->headers_section
);
2159 TRACE("INTERNET_OPTION_URL: %s\n",debugstr_w(url
));
2160 return str_to_buffer(url
, buffer
, size
, unicode
);
2162 case INTERNET_OPTION_USER_AGENT
:
2163 return str_to_buffer(req
->session
->appInfo
->agent
, buffer
, size
, unicode
);
2164 case INTERNET_OPTION_USERNAME
:
2165 return str_to_buffer(req
->session
->userName
, buffer
, size
, unicode
);
2166 case INTERNET_OPTION_PASSWORD
:
2167 return str_to_buffer(req
->session
->password
, buffer
, size
, unicode
);
2168 case INTERNET_OPTION_PROXY_USERNAME
:
2169 return str_to_buffer(req
->session
->appInfo
->proxyUsername
, buffer
, size
, unicode
);
2170 case INTERNET_OPTION_PROXY_PASSWORD
:
2171 return str_to_buffer(req
->session
->appInfo
->proxyPassword
, buffer
, size
, unicode
);
2173 case INTERNET_OPTION_CACHE_TIMESTAMPS
: {
2174 INTERNET_CACHE_ENTRY_INFOW
*info
;
2175 INTERNET_CACHE_TIMESTAMPS
*ts
= buffer
;
2176 WCHAR url
[INTERNET_MAX_URL_LENGTH
];
2177 DWORD nbytes
, error
;
2180 TRACE("INTERNET_OPTION_CACHE_TIMESTAMPS\n");
2182 if (*size
< sizeof(*ts
))
2184 *size
= sizeof(*ts
);
2185 return ERROR_INSUFFICIENT_BUFFER
;
2188 HTTP_GetRequestURL(req
, url
);
2189 ret
= GetUrlCacheEntryInfoW(url
, NULL
, &nbytes
);
2190 error
= GetLastError();
2191 if (!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
)
2193 if (!(info
= heap_alloc(nbytes
)))
2194 return ERROR_OUTOFMEMORY
;
2196 GetUrlCacheEntryInfoW(url
, info
, &nbytes
);
2198 ts
->ftExpires
= info
->ExpireTime
;
2199 ts
->ftLastModified
= info
->LastModifiedTime
;
2202 *size
= sizeof(*ts
);
2203 return ERROR_SUCCESS
;
2208 case INTERNET_OPTION_DATAFILE_NAME
: {
2211 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
2213 if(!req
->req_file
) {
2215 return ERROR_INTERNET_ITEM_NOT_FOUND
;
2219 req_size
= (lstrlenW(req
->req_file
->file_name
)+1) * sizeof(WCHAR
);
2220 if(*size
< req_size
)
2221 return ERROR_INSUFFICIENT_BUFFER
;
2224 memcpy(buffer
, req
->req_file
->file_name
, *size
);
2225 return ERROR_SUCCESS
;
2227 req_size
= WideCharToMultiByte(CP_ACP
, 0, req
->req_file
->file_name
, -1, NULL
, 0, NULL
, NULL
);
2228 if (req_size
> *size
)
2229 return ERROR_INSUFFICIENT_BUFFER
;
2231 *size
= WideCharToMultiByte(CP_ACP
, 0, req
->req_file
->file_name
,
2232 -1, buffer
, *size
, NULL
, NULL
);
2233 return ERROR_SUCCESS
;
2237 case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT
: {
2238 PCCERT_CONTEXT context
;
2241 return ERROR_INTERNET_INVALID_OPERATION
;
2243 if(*size
< sizeof(INTERNET_CERTIFICATE_INFOA
)) {
2244 *size
= sizeof(INTERNET_CERTIFICATE_INFOA
);
2245 return ERROR_INSUFFICIENT_BUFFER
;
2248 context
= (PCCERT_CONTEXT
)NETCON_GetCert(req
->netconn
);
2250 INTERNET_CERTIFICATE_INFOA
*info
= (INTERNET_CERTIFICATE_INFOA
*)buffer
;
2253 memset(info
, 0, sizeof(*info
));
2254 info
->ftExpiry
= context
->pCertInfo
->NotAfter
;
2255 info
->ftStart
= context
->pCertInfo
->NotBefore
;
2256 len
= CertNameToStrA(context
->dwCertEncodingType
,
2257 &context
->pCertInfo
->Subject
, CERT_SIMPLE_NAME_STR
|CERT_NAME_STR_CRLF_FLAG
, NULL
, 0);
2258 info
->lpszSubjectInfo
= LocalAlloc(0, len
);
2259 if(info
->lpszSubjectInfo
)
2260 CertNameToStrA(context
->dwCertEncodingType
,
2261 &context
->pCertInfo
->Subject
, CERT_SIMPLE_NAME_STR
|CERT_NAME_STR_CRLF_FLAG
,
2262 info
->lpszSubjectInfo
, len
);
2263 len
= CertNameToStrA(context
->dwCertEncodingType
,
2264 &context
->pCertInfo
->Issuer
, CERT_SIMPLE_NAME_STR
|CERT_NAME_STR_CRLF_FLAG
, NULL
, 0);
2265 info
->lpszIssuerInfo
= LocalAlloc(0, len
);
2266 if(info
->lpszIssuerInfo
)
2267 CertNameToStrA(context
->dwCertEncodingType
,
2268 &context
->pCertInfo
->Issuer
, CERT_SIMPLE_NAME_STR
|CERT_NAME_STR_CRLF_FLAG
,
2269 info
->lpszIssuerInfo
, len
);
2270 info
->dwKeySize
= NETCON_GetCipherStrength(req
->netconn
);
2271 CertFreeCertificateContext(context
);
2272 return ERROR_SUCCESS
;
2274 return ERROR_NOT_SUPPORTED
;
2276 case INTERNET_OPTION_CONNECT_TIMEOUT
:
2277 if (*size
< sizeof(DWORD
))
2278 return ERROR_INSUFFICIENT_BUFFER
;
2280 *size
= sizeof(DWORD
);
2281 *(DWORD
*)buffer
= req
->connect_timeout
;
2282 return ERROR_SUCCESS
;
2283 case INTERNET_OPTION_REQUEST_FLAGS
: {
2286 if (*size
< sizeof(DWORD
))
2287 return ERROR_INSUFFICIENT_BUFFER
;
2289 /* FIXME: Add support for:
2290 * INTERNET_REQFLAG_FROM_CACHE
2291 * INTERNET_REQFLAG_CACHE_WRITE_DISABLED
2295 flags
|= INTERNET_REQFLAG_VIA_PROXY
;
2296 if(!req
->status_code
)
2297 flags
|= INTERNET_REQFLAG_NO_HEADERS
;
2299 TRACE("INTERNET_OPTION_REQUEST_FLAGS returning %x\n", flags
);
2301 *size
= sizeof(DWORD
);
2302 *(DWORD
*)buffer
= flags
;
2303 return ERROR_SUCCESS
;
2307 return INET_QueryOption(hdr
, option
, buffer
, size
, unicode
);
2310 static DWORD
HTTPREQ_SetOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD size
)
2312 http_request_t
*req
= (http_request_t
*)hdr
;
2315 case 99: /* Undocumented, seems to be INTERNET_OPTION_SECURITY_FLAGS with argument validation */
2316 TRACE("Undocumented option 99\n");
2318 if (!buffer
|| size
!= sizeof(DWORD
))
2319 return ERROR_INVALID_PARAMETER
;
2320 if(*(DWORD
*)buffer
& ~SECURITY_SET_MASK
)
2321 return ERROR_INTERNET_OPTION_NOT_SETTABLE
;
2324 case INTERNET_OPTION_SECURITY_FLAGS
:
2328 if (!buffer
|| size
!= sizeof(DWORD
))
2329 return ERROR_INVALID_PARAMETER
;
2330 flags
= *(DWORD
*)buffer
;
2331 TRACE("INTERNET_OPTION_SECURITY_FLAGS %08x\n", flags
);
2332 flags
&= SECURITY_SET_MASK
;
2333 req
->security_flags
|= flags
;
2334 if(is_valid_netconn(req
->netconn
))
2335 req
->netconn
->security_flags
|= flags
;
2336 return ERROR_SUCCESS
;
2338 case INTERNET_OPTION_CONNECT_TIMEOUT
:
2339 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
2340 req
->connect_timeout
= *(DWORD
*)buffer
;
2341 return ERROR_SUCCESS
;
2343 case INTERNET_OPTION_SEND_TIMEOUT
:
2344 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
2345 req
->send_timeout
= *(DWORD
*)buffer
;
2346 return ERROR_SUCCESS
;
2348 case INTERNET_OPTION_RECEIVE_TIMEOUT
:
2349 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
2350 req
->receive_timeout
= *(DWORD
*)buffer
;
2351 return ERROR_SUCCESS
;
2353 case INTERNET_OPTION_USERNAME
:
2354 heap_free(req
->session
->userName
);
2355 if (!(req
->session
->userName
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
2356 return ERROR_SUCCESS
;
2358 case INTERNET_OPTION_PASSWORD
:
2359 heap_free(req
->session
->password
);
2360 if (!(req
->session
->password
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
2361 return ERROR_SUCCESS
;
2363 case INTERNET_OPTION_PROXY_USERNAME
:
2364 heap_free(req
->session
->appInfo
->proxyUsername
);
2365 if (!(req
->session
->appInfo
->proxyUsername
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
2366 return ERROR_SUCCESS
;
2368 case INTERNET_OPTION_PROXY_PASSWORD
:
2369 heap_free(req
->session
->appInfo
->proxyPassword
);
2370 if (!(req
->session
->appInfo
->proxyPassword
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
2371 return ERROR_SUCCESS
;
2373 case INTERNET_OPTION_HTTP_DECODING
:
2374 if(size
!= sizeof(BOOL
))
2375 return ERROR_INVALID_PARAMETER
;
2376 req
->decoding
= *(BOOL
*)buffer
;
2377 return ERROR_SUCCESS
;
2380 return INET_SetOption(hdr
, option
, buffer
, size
);
2383 static void commit_cache_entry(http_request_t
*req
)
2385 WCHAR url
[INTERNET_MAX_URL_LENGTH
];
2389 CloseHandle(req
->hCacheFile
);
2390 req
->hCacheFile
= NULL
;
2392 if(HTTP_GetRequestURL(req
, url
)) {
2397 header
= build_response_header(req
, TRUE
);
2398 header_len
= (header
? strlenW(header
) : 0);
2399 res
= CommitUrlCacheEntryW(url
, req
->req_file
->file_name
, req
->expires
,
2400 req
->last_modified
, NORMAL_CACHE_ENTRY
,
2401 header
, header_len
, NULL
, 0);
2403 req
->req_file
->is_committed
= TRUE
;
2405 WARN("CommitUrlCacheEntry failed: %u\n", GetLastError());
2410 static void create_cache_entry(http_request_t
*req
)
2412 static const WCHAR no_cacheW
[] = {'n','o','-','c','a','c','h','e',0};
2413 static const WCHAR no_storeW
[] = {'n','o','-','s','t','o','r','e',0};
2415 WCHAR url
[INTERNET_MAX_URL_LENGTH
];
2416 WCHAR file_name
[MAX_PATH
+1];
2419 /* FIXME: We should free previous cache file earlier */
2421 req_file_release(req
->req_file
);
2422 req
->req_file
= NULL
;
2424 if(req
->hCacheFile
) {
2425 CloseHandle(req
->hCacheFile
);
2426 req
->hCacheFile
= NULL
;
2429 if(req
->hdr
.dwFlags
& INTERNET_FLAG_NO_CACHE_WRITE
)
2435 EnterCriticalSection( &req
->headers_section
);
2437 header_idx
= HTTP_GetCustomHeaderIndex(req
, szCache_Control
, 0, FALSE
);
2438 if(header_idx
!= -1) {
2441 for(ptr
=req
->custHeaders
[header_idx
].lpszValue
; *ptr
; ) {
2444 while(*ptr
==' ' || *ptr
=='\t')
2447 end
= strchrW(ptr
, ',');
2449 end
= ptr
+ strlenW(ptr
);
2451 if(!strncmpiW(ptr
, no_cacheW
, sizeof(no_cacheW
)/sizeof(*no_cacheW
)-1)
2452 || !strncmpiW(ptr
, no_storeW
, sizeof(no_storeW
)/sizeof(*no_storeW
)-1)) {
2463 LeaveCriticalSection( &req
->headers_section
);
2467 if(!(req
->hdr
.dwFlags
& INTERNET_FLAG_NEED_FILE
))
2470 FIXME("INTERNET_FLAG_NEED_FILE is not supported correctly\n");
2473 b
= HTTP_GetRequestURL(req
, url
);
2475 WARN("Could not get URL\n");
2479 b
= CreateUrlCacheEntryW(url
, req
->contentLength
== ~0u ? 0 : req
->contentLength
, NULL
, file_name
, 0);
2481 WARN("Could not create cache entry: %08x\n", GetLastError());
2485 create_req_file(file_name
, &req
->req_file
);
2487 req
->hCacheFile
= CreateFileW(file_name
, GENERIC_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2488 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2489 if(req
->hCacheFile
== INVALID_HANDLE_VALUE
) {
2490 WARN("Could not create file: %u\n", GetLastError());
2491 req
->hCacheFile
= NULL
;
2495 if(req
->read_size
) {
2498 b
= WriteFile(req
->hCacheFile
, req
->read_buf
+req
->read_pos
, req
->read_size
, &written
, NULL
);
2500 FIXME("WriteFile failed: %u\n", GetLastError());
2502 if(req
->data_stream
->vtbl
->end_of_data(req
->data_stream
, req
))
2503 commit_cache_entry(req
);
2507 /* read some more data into the read buffer (the read section must be held) */
2508 static DWORD
read_more_data( http_request_t
*req
, int maxlen
)
2515 /* move existing data to the start of the buffer */
2517 memmove( req
->read_buf
, req
->read_buf
+ req
->read_pos
, req
->read_size
);
2521 if (maxlen
== -1) maxlen
= sizeof(req
->read_buf
);
2523 res
= NETCON_recv( req
->netconn
, req
->read_buf
+ req
->read_size
,
2524 maxlen
- req
->read_size
, TRUE
, &len
);
2525 if(res
== ERROR_SUCCESS
)
2526 req
->read_size
+= len
;
2531 /* remove some amount of data from the read buffer (the read section must be held) */
2532 static void remove_data( http_request_t
*req
, int count
)
2534 if (!(req
->read_size
-= count
)) req
->read_pos
= 0;
2535 else req
->read_pos
+= count
;
2538 static DWORD
read_line( http_request_t
*req
, LPSTR buffer
, DWORD
*len
)
2540 int count
, bytes_read
, pos
= 0;
2543 EnterCriticalSection( &req
->read_section
);
2546 BYTE
*eol
= memchr( req
->read_buf
+ req
->read_pos
, '\n', req
->read_size
);
2550 count
= eol
- (req
->read_buf
+ req
->read_pos
);
2551 bytes_read
= count
+ 1;
2553 else count
= bytes_read
= req
->read_size
;
2555 count
= min( count
, *len
- pos
);
2556 memcpy( buffer
+ pos
, req
->read_buf
+ req
->read_pos
, count
);
2558 remove_data( req
, bytes_read
);
2561 if ((res
= read_more_data( req
, -1 )))
2563 WARN( "read failed %u\n", res
);
2564 LeaveCriticalSection( &req
->read_section
);
2567 if (!req
->read_size
)
2570 TRACE( "returning empty string\n" );
2571 LeaveCriticalSection( &req
->read_section
);
2572 return ERROR_SUCCESS
;
2575 LeaveCriticalSection( &req
->read_section
);
2579 if (pos
&& buffer
[pos
- 1] == '\r') pos
--;
2582 buffer
[*len
- 1] = 0;
2583 TRACE( "returning %s\n", debugstr_a(buffer
));
2584 return ERROR_SUCCESS
;
2587 /* check if we have reached the end of the data to read (the read section must be held) */
2588 static BOOL
end_of_read_data( http_request_t
*req
)
2590 return !req
->read_size
&& req
->data_stream
->vtbl
->end_of_data(req
->data_stream
, req
);
2593 static DWORD
read_http_stream(http_request_t
*req
, BYTE
*buf
, DWORD size
, DWORD
*read
, blocking_mode_t blocking_mode
)
2597 res
= req
->data_stream
->vtbl
->read(req
->data_stream
, req
, buf
, size
, read
, blocking_mode
);
2598 assert(*read
<= size
);
2600 if(req
->hCacheFile
) {
2605 bres
= WriteFile(req
->hCacheFile
, buf
, *read
, &written
, NULL
);
2607 FIXME("WriteFile failed: %u\n", GetLastError());
2610 if(req
->data_stream
->vtbl
->end_of_data(req
->data_stream
, req
))
2611 commit_cache_entry(req
);
2617 /* fetch some more data into the read buffer (the read section must be held) */
2618 static DWORD
refill_read_buffer(http_request_t
*req
, blocking_mode_t blocking_mode
, DWORD
*read_bytes
)
2622 if(req
->read_size
== sizeof(req
->read_buf
))
2623 return ERROR_SUCCESS
;
2627 memmove(req
->read_buf
, req
->read_buf
+req
->read_pos
, req
->read_size
);
2631 res
= read_http_stream(req
, req
->read_buf
+req
->read_size
, sizeof(req
->read_buf
) - req
->read_size
,
2632 &read
, blocking_mode
);
2633 req
->read_size
+= read
;
2635 TRACE("read %u bytes, read_size %u\n", read
, req
->read_size
);
2641 /* return the size of data available to be read immediately (the read section must be held) */
2642 static DWORD
get_avail_data( http_request_t
*req
)
2644 return req
->read_size
+ req
->data_stream
->vtbl
->get_avail_data(req
->data_stream
, req
);
2647 static DWORD
netconn_get_avail_data(data_stream_t
*stream
, http_request_t
*req
)
2649 netconn_stream_t
*netconn_stream
= (netconn_stream_t
*)stream
;
2652 if(is_valid_netconn(req
->netconn
))
2653 NETCON_query_data_available(req
->netconn
, &avail
);
2654 return netconn_stream
->content_length
== ~0u
2656 : min(avail
, netconn_stream
->content_length
-netconn_stream
->content_read
);
2659 static BOOL
netconn_end_of_data(data_stream_t
*stream
, http_request_t
*req
)
2661 netconn_stream_t
*netconn_stream
= (netconn_stream_t
*)stream
;
2662 return netconn_stream
->content_read
== netconn_stream
->content_length
|| !is_valid_netconn(req
->netconn
);
2665 static DWORD
netconn_read(data_stream_t
*stream
, http_request_t
*req
, BYTE
*buf
, DWORD size
,
2666 DWORD
*read
, blocking_mode_t blocking_mode
)
2668 netconn_stream_t
*netconn_stream
= (netconn_stream_t
*)stream
;
2669 DWORD res
= ERROR_SUCCESS
;
2670 int len
= 0, ret
= 0;
2672 size
= min(size
, netconn_stream
->content_length
-netconn_stream
->content_read
);
2674 if(size
&& is_valid_netconn(req
->netconn
)) {
2675 while((res
= NETCON_recv(req
->netconn
, buf
+ret
, size
-ret
, blocking_mode
!= BLOCKING_DISALLOW
, &len
)) == ERROR_SUCCESS
) {
2677 netconn_stream
->content_length
= netconn_stream
->content_read
;
2681 netconn_stream
->content_read
+= len
;
2682 if(blocking_mode
!= BLOCKING_WAITALL
|| size
== ret
)
2686 if(ret
|| (blocking_mode
== BLOCKING_DISALLOW
&& res
== WSAEWOULDBLOCK
))
2687 res
= ERROR_SUCCESS
;
2690 TRACE("read %u bytes\n", ret
);
2695 static BOOL
netconn_drain_content(data_stream_t
*stream
, http_request_t
*req
)
2697 netconn_stream_t
*netconn_stream
= (netconn_stream_t
*)stream
;
2701 if(netconn_end_of_data(stream
, req
))
2705 if(NETCON_recv(req
->netconn
, buf
, sizeof(buf
), FALSE
, &len
) != ERROR_SUCCESS
)
2708 netconn_stream
->content_read
+= len
;
2709 }while(netconn_stream
->content_read
< netconn_stream
->content_length
);
2714 static void netconn_destroy(data_stream_t
*stream
)
2718 static const data_stream_vtbl_t netconn_stream_vtbl
= {
2719 netconn_get_avail_data
,
2720 netconn_end_of_data
,
2722 netconn_drain_content
,
2726 /* read some more data into the read buffer (the read section must be held) */
2727 static DWORD
read_more_chunked_data(chunked_stream_t
*stream
, http_request_t
*req
, int maxlen
)
2732 assert(!stream
->end_of_data
);
2734 if (stream
->buf_pos
)
2736 /* move existing data to the start of the buffer */
2737 if(stream
->buf_size
)
2738 memmove(stream
->buf
, stream
->buf
+ stream
->buf_pos
, stream
->buf_size
);
2739 stream
->buf_pos
= 0;
2742 if (maxlen
== -1) maxlen
= sizeof(stream
->buf
);
2744 res
= NETCON_recv( req
->netconn
, stream
->buf
+ stream
->buf_size
,
2745 maxlen
- stream
->buf_size
, TRUE
, &len
);
2746 if(res
== ERROR_SUCCESS
)
2747 stream
->buf_size
+= len
;
2752 /* remove some amount of data from the read buffer (the read section must be held) */
2753 static void remove_chunked_data(chunked_stream_t
*stream
, int count
)
2755 if (!(stream
->buf_size
-= count
)) stream
->buf_pos
= 0;
2756 else stream
->buf_pos
+= count
;
2759 /* discard data contents until we reach end of line (the read section must be held) */
2760 static DWORD
discard_chunked_eol(chunked_stream_t
*stream
, http_request_t
*req
)
2766 BYTE
*eol
= memchr(stream
->buf
+ stream
->buf_pos
, '\n', stream
->buf_size
);
2769 remove_chunked_data(stream
, (eol
+ 1) - (stream
->buf
+ stream
->buf_pos
));
2772 stream
->buf_pos
= stream
->buf_size
= 0; /* discard everything */
2773 if ((res
= read_more_chunked_data(stream
, req
, -1)) != ERROR_SUCCESS
) return res
;
2774 } while (stream
->buf_size
);
2775 return ERROR_SUCCESS
;
2778 /* read the size of the next chunk (the read section must be held) */
2779 static DWORD
start_next_chunk(chunked_stream_t
*stream
, http_request_t
*req
)
2781 DWORD chunk_size
= 0, res
;
2783 assert(!stream
->chunk_size
|| stream
->chunk_size
== ~0u);
2785 if (stream
->end_of_data
) return ERROR_SUCCESS
;
2787 /* read terminator for the previous chunk */
2788 if(!stream
->chunk_size
&& (res
= discard_chunked_eol(stream
, req
)) != ERROR_SUCCESS
)
2793 while (stream
->buf_size
)
2795 char ch
= stream
->buf
[stream
->buf_pos
];
2796 if (ch
>= '0' && ch
<= '9') chunk_size
= chunk_size
* 16 + ch
- '0';
2797 else if (ch
>= 'a' && ch
<= 'f') chunk_size
= chunk_size
* 16 + ch
- 'a' + 10;
2798 else if (ch
>= 'A' && ch
<= 'F') chunk_size
= chunk_size
* 16 + ch
- 'A' + 10;
2799 else if (ch
== ';' || ch
== '\r' || ch
== '\n')
2801 TRACE( "reading %u byte chunk\n", chunk_size
);
2802 stream
->chunk_size
= chunk_size
;
2803 if (req
->contentLength
== ~0u) req
->contentLength
= chunk_size
;
2804 else req
->contentLength
+= chunk_size
;
2806 /* eat the rest of this line */
2807 if ((res
= discard_chunked_eol(stream
, req
)) != ERROR_SUCCESS
)
2810 /* if there's chunk data, return now */
2811 if (chunk_size
) return ERROR_SUCCESS
;
2813 /* otherwise, eat the terminator for this chunk */
2814 if ((res
= discard_chunked_eol(stream
, req
)) != ERROR_SUCCESS
)
2817 stream
->end_of_data
= TRUE
;
2818 return ERROR_SUCCESS
;
2820 remove_chunked_data(stream
, 1);
2822 if ((res
= read_more_chunked_data(stream
, req
, -1)) != ERROR_SUCCESS
) return res
;
2823 if (!stream
->buf_size
)
2825 stream
->chunk_size
= 0;
2826 return ERROR_SUCCESS
;
2831 static DWORD
chunked_get_avail_data(data_stream_t
*stream
, http_request_t
*req
)
2833 /* Allow reading only from read buffer */
2837 static BOOL
chunked_end_of_data(data_stream_t
*stream
, http_request_t
*req
)
2839 chunked_stream_t
*chunked_stream
= (chunked_stream_t
*)stream
;
2840 return chunked_stream
->end_of_data
;
2843 static DWORD
chunked_read(data_stream_t
*stream
, http_request_t
*req
, BYTE
*buf
, DWORD size
,
2844 DWORD
*read
, blocking_mode_t blocking_mode
)
2846 chunked_stream_t
*chunked_stream
= (chunked_stream_t
*)stream
;
2847 DWORD read_bytes
= 0, ret_read
= 0, res
= ERROR_SUCCESS
;
2849 if(!chunked_stream
->chunk_size
|| chunked_stream
->chunk_size
== ~0u) {
2850 res
= start_next_chunk(chunked_stream
, req
);
2851 if(res
!= ERROR_SUCCESS
)
2855 while(size
&& chunked_stream
->chunk_size
&& !chunked_stream
->end_of_data
) {
2856 if(chunked_stream
->buf_size
) {
2857 read_bytes
= min(size
, min(chunked_stream
->buf_size
, chunked_stream
->chunk_size
));
2859 /* this could block */
2860 if(blocking_mode
== BLOCKING_DISALLOW
&& read_bytes
== chunked_stream
->chunk_size
)
2863 memcpy(buf
+ret_read
, chunked_stream
->buf
+chunked_stream
->buf_pos
, read_bytes
);
2864 remove_chunked_data(chunked_stream
, read_bytes
);
2866 read_bytes
= min(size
, chunked_stream
->chunk_size
);
2868 if(blocking_mode
== BLOCKING_DISALLOW
) {
2871 if(!is_valid_netconn(req
->netconn
) || !NETCON_query_data_available(req
->netconn
, &avail
) || !avail
)
2873 if(read_bytes
> avail
)
2876 /* this could block */
2877 if(read_bytes
== chunked_stream
->chunk_size
)
2881 res
= NETCON_recv(req
->netconn
, (char *)buf
+ret_read
, read_bytes
, TRUE
, (int*)&read_bytes
);
2882 if(res
!= ERROR_SUCCESS
)
2886 chunked_stream
->chunk_size
-= read_bytes
;
2888 ret_read
+= read_bytes
;
2889 if(size
&& !chunked_stream
->chunk_size
) {
2890 assert(blocking_mode
!= BLOCKING_DISALLOW
);
2891 res
= start_next_chunk(chunked_stream
, req
);
2892 if(res
!= ERROR_SUCCESS
)
2896 if(blocking_mode
== BLOCKING_ALLOW
)
2897 blocking_mode
= BLOCKING_DISALLOW
;
2900 TRACE("read %u bytes\n", ret_read
);
2905 static BOOL
chunked_drain_content(data_stream_t
*stream
, http_request_t
*req
)
2907 chunked_stream_t
*chunked_stream
= (chunked_stream_t
*)stream
;
2909 remove_chunked_data(chunked_stream
, chunked_stream
->buf_size
);
2910 return chunked_stream
->end_of_data
;
2913 static void chunked_destroy(data_stream_t
*stream
)
2915 chunked_stream_t
*chunked_stream
= (chunked_stream_t
*)stream
;
2916 heap_free(chunked_stream
);
2919 static const data_stream_vtbl_t chunked_stream_vtbl
= {
2920 chunked_get_avail_data
,
2921 chunked_end_of_data
,
2923 chunked_drain_content
,
2927 /* set the request content length based on the headers */
2928 static DWORD
set_content_length(http_request_t
*request
)
2930 static const WCHAR szChunked
[] = {'c','h','u','n','k','e','d',0};
2931 static const WCHAR headW
[] = {'H','E','A','D',0};
2935 if(request
->status_code
== HTTP_STATUS_NO_CONTENT
|| !strcmpW(request
->verb
, headW
)) {
2936 request
->contentLength
= request
->netconn_stream
.content_length
= 0;
2937 return ERROR_SUCCESS
;
2940 size
= sizeof(request
->contentLength
);
2941 if (HTTP_HttpQueryInfoW(request
, HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_CONTENT_LENGTH
,
2942 &request
->contentLength
, &size
, NULL
) != ERROR_SUCCESS
)
2943 request
->contentLength
= ~0u;
2944 request
->netconn_stream
.content_length
= request
->contentLength
;
2945 request
->netconn_stream
.content_read
= request
->read_size
;
2947 size
= sizeof(encoding
);
2948 if (HTTP_HttpQueryInfoW(request
, HTTP_QUERY_TRANSFER_ENCODING
, encoding
, &size
, NULL
) == ERROR_SUCCESS
&&
2949 !strcmpiW(encoding
, szChunked
))
2951 chunked_stream_t
*chunked_stream
;
2953 chunked_stream
= heap_alloc(sizeof(*chunked_stream
));
2955 return ERROR_OUTOFMEMORY
;
2957 chunked_stream
->data_stream
.vtbl
= &chunked_stream_vtbl
;
2958 chunked_stream
->buf_size
= chunked_stream
->buf_pos
= 0;
2959 chunked_stream
->chunk_size
= ~0u;
2960 chunked_stream
->end_of_data
= FALSE
;
2962 if(request
->read_size
) {
2963 memcpy(chunked_stream
->buf
, request
->read_buf
+request
->read_pos
, request
->read_size
);
2964 chunked_stream
->buf_size
= request
->read_size
;
2965 request
->read_size
= request
->read_pos
= 0;
2968 request
->data_stream
= &chunked_stream
->data_stream
;
2969 request
->contentLength
= ~0u;
2972 if(request
->decoding
) {
2975 static const WCHAR deflateW
[] = {'d','e','f','l','a','t','e',0};
2976 static const WCHAR gzipW
[] = {'g','z','i','p',0};
2978 EnterCriticalSection( &request
->headers_section
);
2980 encoding_idx
= HTTP_GetCustomHeaderIndex(request
, szContent_Encoding
, 0, FALSE
);
2981 if(encoding_idx
!= -1) {
2982 if(!strcmpiW(request
->custHeaders
[encoding_idx
].lpszValue
, gzipW
)) {
2983 HTTP_DeleteCustomHeader(request
, encoding_idx
);
2984 LeaveCriticalSection( &request
->headers_section
);
2985 return init_gzip_stream(request
, TRUE
);
2987 if(!strcmpiW(request
->custHeaders
[encoding_idx
].lpszValue
, deflateW
)) {
2988 HTTP_DeleteCustomHeader(request
, encoding_idx
);
2989 LeaveCriticalSection( &request
->headers_section
);
2990 return init_gzip_stream(request
, FALSE
);
2994 LeaveCriticalSection( &request
->headers_section
);
2997 return ERROR_SUCCESS
;
3000 static void send_request_complete(http_request_t
*req
, DWORD_PTR result
, DWORD error
)
3002 INTERNET_ASYNC_RESULT iar
;
3004 iar
.dwResult
= result
;
3005 iar
.dwError
= error
;
3007 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
3008 sizeof(INTERNET_ASYNC_RESULT
));
3011 static void HTTP_ReceiveRequestData(http_request_t
*req
, BOOL first_notif
, DWORD
*ret_size
)
3013 DWORD res
, read
= 0, avail
= 0;
3014 blocking_mode_t mode
;
3018 EnterCriticalSection( &req
->read_section
);
3020 mode
= first_notif
&& req
->read_size
? BLOCKING_DISALLOW
: BLOCKING_ALLOW
;
3021 res
= refill_read_buffer(req
, mode
, &read
);
3022 if(res
== ERROR_SUCCESS
)
3023 avail
= get_avail_data(req
);
3025 LeaveCriticalSection( &req
->read_section
);
3027 if(res
!= ERROR_SUCCESS
|| (mode
!= BLOCKING_DISALLOW
&& !read
)) {
3028 WARN("res %u read %u, closing connection\n", res
, read
);
3029 http_release_netconn(req
, FALSE
);
3032 if(res
!= ERROR_SUCCESS
) {
3033 send_request_complete(req
, 0, res
);
3042 send_request_complete(req
, req
->session
->hdr
.dwInternalFlags
& INET_OPENURL
? (DWORD_PTR
)req
->hdr
.hInternet
: 1, avail
);
3045 /* read data from the http connection (the read section must be held) */
3046 static DWORD
HTTPREQ_Read(http_request_t
*req
, void *buffer
, DWORD size
, DWORD
*read
, BOOL sync
)
3048 DWORD current_read
= 0, ret_read
= 0;
3049 blocking_mode_t blocking_mode
;
3050 DWORD res
= ERROR_SUCCESS
;
3052 blocking_mode
= req
->session
->appInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
? BLOCKING_ALLOW
: BLOCKING_WAITALL
;
3054 EnterCriticalSection( &req
->read_section
);
3056 if(req
->read_size
) {
3057 ret_read
= min(size
, req
->read_size
);
3058 memcpy(buffer
, req
->read_buf
+req
->read_pos
, ret_read
);
3059 req
->read_size
-= ret_read
;
3060 req
->read_pos
+= ret_read
;
3061 if(blocking_mode
== BLOCKING_ALLOW
)
3062 blocking_mode
= BLOCKING_DISALLOW
;
3065 if(ret_read
< size
) {
3066 res
= read_http_stream(req
, (BYTE
*)buffer
+ret_read
, size
-ret_read
, ¤t_read
, blocking_mode
);
3067 ret_read
+= current_read
;
3070 LeaveCriticalSection( &req
->read_section
);
3073 TRACE( "retrieved %u bytes (%u)\n", ret_read
, req
->contentLength
);
3075 if(size
&& !ret_read
)
3076 http_release_netconn(req
, res
== ERROR_SUCCESS
);
3081 static BOOL
drain_content(http_request_t
*req
, BOOL blocking
)
3085 if(!is_valid_netconn(req
->netconn
) || req
->contentLength
== -1)
3088 if(!strcmpW(req
->verb
, szHEAD
))
3092 return req
->data_stream
->vtbl
->drain_content(req
->data_stream
, req
);
3094 EnterCriticalSection( &req
->read_section
);
3097 DWORD bytes_read
, res
;
3100 res
= HTTPREQ_Read(req
, buf
, sizeof(buf
), &bytes_read
, TRUE
);
3101 if(res
!= ERROR_SUCCESS
) {
3111 LeaveCriticalSection( &req
->read_section
);
3115 static DWORD
HTTPREQ_ReadFile(object_header_t
*hdr
, void *buffer
, DWORD size
, DWORD
*read
)
3117 http_request_t
*req
= (http_request_t
*)hdr
;
3120 EnterCriticalSection( &req
->read_section
);
3121 if(hdr
->dwError
== INTERNET_HANDLE_IN_USE
)
3122 hdr
->dwError
= ERROR_INTERNET_INTERNAL_ERROR
;
3124 res
= HTTPREQ_Read(req
, buffer
, size
, read
, TRUE
);
3125 if(res
== ERROR_SUCCESS
)
3127 LeaveCriticalSection( &req
->read_section
);
3137 } read_file_ex_task_t
;
3139 static void AsyncReadFileExProc(task_header_t
*hdr
)
3141 read_file_ex_task_t
*task
= (read_file_ex_task_t
*)hdr
;
3142 http_request_t
*req
= (http_request_t
*)task
->hdr
.hdr
;
3145 TRACE("INTERNETREADFILEEXW %p\n", task
->hdr
.hdr
);
3147 res
= HTTPREQ_Read(req
, task
->buf
, task
->size
, task
->ret_read
, TRUE
);
3148 send_request_complete(req
, res
== ERROR_SUCCESS
, res
);
3151 static DWORD
HTTPREQ_ReadFileEx(object_header_t
*hdr
, void *buf
, DWORD size
, DWORD
*ret_read
,
3152 DWORD flags
, DWORD_PTR context
)
3155 http_request_t
*req
= (http_request_t
*)hdr
;
3156 DWORD res
, read
, cread
, error
= ERROR_SUCCESS
;
3158 if (flags
& ~(IRF_ASYNC
|IRF_NO_WAIT
))
3159 FIXME("these dwFlags aren't implemented: 0x%x\n", flags
& ~(IRF_ASYNC
|IRF_NO_WAIT
));
3161 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
3163 if (hdr
->dwFlags
& INTERNET_FLAG_ASYNC
)
3165 read_file_ex_task_t
*task
;
3167 if (TryEnterCriticalSection( &req
->read_section
))
3169 if (get_avail_data(req
))
3171 res
= HTTPREQ_Read(req
, buf
, size
, &read
, FALSE
);
3172 LeaveCriticalSection( &req
->read_section
);
3175 LeaveCriticalSection( &req
->read_section
);
3178 task
= alloc_async_task(&req
->hdr
, AsyncReadFileExProc
, sizeof(*task
));
3181 task
->ret_read
= ret_read
;
3183 INTERNET_AsyncCall(&task
->hdr
);
3185 return ERROR_IO_PENDING
;
3190 EnterCriticalSection( &req
->read_section
);
3191 if(hdr
->dwError
== ERROR_SUCCESS
)
3192 hdr
->dwError
= INTERNET_HANDLE_IN_USE
;
3193 else if(hdr
->dwError
== INTERNET_HANDLE_IN_USE
)
3194 hdr
->dwError
= ERROR_INTERNET_INTERNAL_ERROR
;
3197 res
= HTTPREQ_Read(req
, (char*)buf
+read
, size
-read
, &cread
, !(flags
& IRF_NO_WAIT
));
3198 if(res
!= ERROR_SUCCESS
)
3202 if(read
== size
|| end_of_read_data(req
))
3205 LeaveCriticalSection( &req
->read_section
);
3207 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
,
3208 &cread
, sizeof(cread
));
3209 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
,
3210 INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
3212 EnterCriticalSection( &req
->read_section
);
3215 if(hdr
->dwError
== INTERNET_HANDLE_IN_USE
)
3216 hdr
->dwError
= ERROR_SUCCESS
;
3218 error
= hdr
->dwError
;
3220 LeaveCriticalSection( &req
->read_section
);
3224 if (res
== ERROR_SUCCESS
) {
3225 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
,
3226 &read
, sizeof(read
));
3229 return res
==ERROR_SUCCESS
? error
: res
;
3232 static DWORD
HTTPREQ_WriteFile(object_header_t
*hdr
, const void *buffer
, DWORD size
, DWORD
*written
)
3235 http_request_t
*request
= (http_request_t
*)hdr
;
3237 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
3240 res
= NETCON_send(request
->netconn
, buffer
, size
, 0, (LPINT
)written
);
3241 if (res
== ERROR_SUCCESS
)
3242 request
->bytesWritten
+= *written
;
3244 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_SENT
, written
, sizeof(DWORD
));
3251 } http_data_available_task_t
;
3253 static void AsyncQueryDataAvailableProc(task_header_t
*hdr
)
3255 http_data_available_task_t
*task
= (http_data_available_task_t
*)hdr
;
3257 HTTP_ReceiveRequestData((http_request_t
*)task
->hdr
.hdr
, FALSE
, task
->ret_size
);
3260 static DWORD
HTTPREQ_QueryDataAvailable(object_header_t
*hdr
, DWORD
*available
, DWORD flags
, DWORD_PTR ctx
)
3262 http_request_t
*req
= (http_request_t
*)hdr
;
3264 TRACE("(%p %p %x %lx)\n", req
, available
, flags
, ctx
);
3266 if (req
->session
->appInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
3268 http_data_available_task_t
*task
;
3270 /* never wait, if we can't enter the section we queue an async request right away */
3271 if (TryEnterCriticalSection( &req
->read_section
))
3273 refill_read_buffer(req
, BLOCKING_DISALLOW
, NULL
);
3274 if ((*available
= get_avail_data( req
))) goto done
;
3275 if (end_of_read_data( req
)) goto done
;
3276 LeaveCriticalSection( &req
->read_section
);
3279 task
= alloc_async_task(&req
->hdr
, AsyncQueryDataAvailableProc
, sizeof(*task
));
3280 task
->ret_size
= available
;
3281 INTERNET_AsyncCall(&task
->hdr
);
3282 return ERROR_IO_PENDING
;
3285 EnterCriticalSection( &req
->read_section
);
3287 if (!(*available
= get_avail_data( req
)) && !end_of_read_data( req
))
3289 refill_read_buffer( req
, BLOCKING_ALLOW
, NULL
);
3290 *available
= get_avail_data( req
);
3294 LeaveCriticalSection( &req
->read_section
);
3296 TRACE( "returning %u\n", *available
);
3297 return ERROR_SUCCESS
;
3300 static DWORD
HTTPREQ_LockRequestFile(object_header_t
*hdr
, req_file_t
**ret
)
3302 http_request_t
*req
= (http_request_t
*)hdr
;
3304 TRACE("(%p)\n", req
);
3306 if(!req
->req_file
) {
3307 WARN("No cache file name available\n");
3308 return ERROR_FILE_NOT_FOUND
;
3311 *ret
= req_file_addref(req
->req_file
);
3312 return ERROR_SUCCESS
;
3315 static const object_vtbl_t HTTPREQVtbl
= {
3317 HTTPREQ_CloseConnection
,
3318 HTTPREQ_QueryOption
,
3323 HTTPREQ_QueryDataAvailable
,
3325 HTTPREQ_LockRequestFile
3328 /***********************************************************************
3329 * HTTP_HttpOpenRequestW (internal)
3331 * Open a HTTP request handle
3334 * HINTERNET a HTTP request handle on success
3338 static DWORD
HTTP_HttpOpenRequestW(http_session_t
*session
,
3339 LPCWSTR lpszVerb
, LPCWSTR lpszObjectName
, LPCWSTR lpszVersion
,
3340 LPCWSTR lpszReferrer
, LPCWSTR
*lpszAcceptTypes
,
3341 DWORD dwFlags
, DWORD_PTR dwContext
, HINTERNET
*ret
)
3343 appinfo_t
*hIC
= session
->appInfo
;
3344 http_request_t
*request
;
3349 request
= alloc_object(&session
->hdr
, &HTTPREQVtbl
, sizeof(http_request_t
));
3351 return ERROR_OUTOFMEMORY
;
3353 request
->hdr
.htype
= WH_HHTTPREQ
;
3354 request
->hdr
.dwFlags
= dwFlags
;
3355 request
->hdr
.dwContext
= dwContext
;
3356 request
->contentLength
= ~0u;
3358 request
->netconn_stream
.data_stream
.vtbl
= &netconn_stream_vtbl
;
3359 request
->data_stream
= &request
->netconn_stream
.data_stream
;
3360 request
->connect_timeout
= session
->connect_timeout
;
3361 request
->send_timeout
= session
->send_timeout
;
3362 request
->receive_timeout
= session
->receive_timeout
;
3364 InitializeCriticalSection( &request
->headers_section
);
3365 request
->headers_section
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": http_request_t.headers_section");
3367 InitializeCriticalSection( &request
->read_section
);
3368 request
->read_section
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": http_request_t.read_section");
3370 WININET_AddRef( &session
->hdr
);
3371 request
->session
= session
;
3372 list_add_head( &session
->hdr
.children
, &request
->hdr
.entry
);
3374 request
->server
= get_server(session
->hostName
, session
->hostPort
, (dwFlags
& INTERNET_FLAG_SECURE
) != 0, TRUE
);
3375 if(!request
->server
) {
3376 WININET_Release(&request
->hdr
);
3377 return ERROR_OUTOFMEMORY
;
3380 if (dwFlags
& INTERNET_FLAG_IGNORE_CERT_CN_INVALID
)
3381 request
->security_flags
|= SECURITY_FLAG_IGNORE_CERT_CN_INVALID
;
3382 if (dwFlags
& INTERNET_FLAG_IGNORE_CERT_DATE_INVALID
)
3383 request
->security_flags
|= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
;
3385 if (lpszObjectName
&& *lpszObjectName
) {
3389 rc
= UrlEscapeW(lpszObjectName
, NULL
, &len
, URL_ESCAPE_SPACES_ONLY
);
3390 if (rc
!= E_POINTER
)
3391 len
= strlenW(lpszObjectName
)+1;
3392 request
->path
= heap_alloc(len
*sizeof(WCHAR
));
3393 rc
= UrlEscapeW(lpszObjectName
, request
->path
, &len
,
3394 URL_ESCAPE_SPACES_ONLY
);
3397 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName
),rc
);
3398 strcpyW(request
->path
,lpszObjectName
);
3401 static const WCHAR slashW
[] = {'/',0};
3403 request
->path
= heap_strdupW(slashW
);
3406 if (lpszReferrer
&& *lpszReferrer
)
3407 HTTP_ProcessHeader(request
, HTTP_REFERER
, lpszReferrer
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REQ
);
3409 if (lpszAcceptTypes
)
3412 for (i
= 0; lpszAcceptTypes
[i
]; i
++)
3414 if (!*lpszAcceptTypes
[i
]) continue;
3415 HTTP_ProcessHeader(request
, HTTP_ACCEPT
, lpszAcceptTypes
[i
],
3416 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
|
3417 HTTP_ADDHDR_FLAG_REQ
|
3418 (i
== 0 ? (HTTP_ADDHDR_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_ADD
) : 0));
3422 request
->verb
= heap_strdupW(lpszVerb
&& *lpszVerb
? lpszVerb
: szGET
);
3423 request
->version
= heap_strdupW(lpszVersion
&& *lpszVersion
? lpszVersion
: g_szHttp1_1
);
3425 HTTP_ProcessHeader(request
, hostW
, request
->server
->canon_host_port
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REQ
);
3427 if (hIC
->proxy
&& hIC
->proxy
[0] && !HTTP_ShouldBypassProxy(hIC
, session
->hostName
))
3428 HTTP_DealWithProxy( hIC
, session
, request
);
3430 INTERNET_SendCallback(&session
->hdr
, dwContext
,
3431 INTERNET_STATUS_HANDLE_CREATED
, &request
->hdr
.hInternet
,
3434 TRACE("<-- (%p)\n", request
);
3436 *ret
= request
->hdr
.hInternet
;
3437 return ERROR_SUCCESS
;
3440 /***********************************************************************
3441 * HttpOpenRequestW (WININET.@)
3443 * Open a HTTP request handle
3446 * HINTERNET a HTTP request handle on success
3450 HINTERNET WINAPI
HttpOpenRequestW(HINTERNET hHttpSession
,
3451 LPCWSTR lpszVerb
, LPCWSTR lpszObjectName
, LPCWSTR lpszVersion
,
3452 LPCWSTR lpszReferrer
, LPCWSTR
*lpszAcceptTypes
,
3453 DWORD dwFlags
, DWORD_PTR dwContext
)
3455 http_session_t
*session
;
3456 HINTERNET handle
= NULL
;
3459 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession
,
3460 debugstr_w(lpszVerb
), debugstr_w(lpszObjectName
),
3461 debugstr_w(lpszVersion
), debugstr_w(lpszReferrer
), lpszAcceptTypes
,
3462 dwFlags
, dwContext
);
3463 if(lpszAcceptTypes
!=NULL
)
3466 for(i
=0;lpszAcceptTypes
[i
]!=NULL
;i
++)
3467 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes
[i
]));
3470 session
= (http_session_t
*) get_handle_object( hHttpSession
);
3471 if (NULL
== session
|| session
->hdr
.htype
!= WH_HHTTPSESSION
)
3473 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
3478 * My tests seem to show that the windows version does not
3479 * become asynchronous until after this point. And anyhow
3480 * if this call was asynchronous then how would you get the
3481 * necessary HINTERNET pointer returned by this function.
3484 res
= HTTP_HttpOpenRequestW(session
, lpszVerb
, lpszObjectName
,
3485 lpszVersion
, lpszReferrer
, lpszAcceptTypes
,
3486 dwFlags
, dwContext
, &handle
);
3489 WININET_Release( &session
->hdr
);
3490 TRACE("returning %p\n", handle
);
3491 if(res
!= ERROR_SUCCESS
)
3496 static const LPCWSTR header_lookup
[] = {
3497 szMime_Version
, /* HTTP_QUERY_MIME_VERSION = 0 */
3498 szContent_Type
, /* HTTP_QUERY_CONTENT_TYPE = 1 */
3499 szContent_Transfer_Encoding
,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
3500 szContent_ID
, /* HTTP_QUERY_CONTENT_ID = 3 */
3501 NULL
, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
3502 szContent_Length
, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
3503 szContent_Language
, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
3504 szAllow
, /* HTTP_QUERY_ALLOW = 7 */
3505 szPublic
, /* HTTP_QUERY_PUBLIC = 8 */
3506 szDate
, /* HTTP_QUERY_DATE = 9 */
3507 szExpires
, /* HTTP_QUERY_EXPIRES = 10 */
3508 szLast_Modified
, /* HTTP_QUERY_LAST_MODIFIED = 11 */
3509 NULL
, /* HTTP_QUERY_MESSAGE_ID = 12 */
3510 szURI
, /* HTTP_QUERY_URI = 13 */
3511 szFrom
, /* HTTP_QUERY_DERIVED_FROM = 14 */
3512 NULL
, /* HTTP_QUERY_COST = 15 */
3513 NULL
, /* HTTP_QUERY_LINK = 16 */
3514 szPragma
, /* HTTP_QUERY_PRAGMA = 17 */
3515 NULL
, /* HTTP_QUERY_VERSION = 18 */
3516 szStatus
, /* HTTP_QUERY_STATUS_CODE = 19 */
3517 NULL
, /* HTTP_QUERY_STATUS_TEXT = 20 */
3518 NULL
, /* HTTP_QUERY_RAW_HEADERS = 21 */
3519 NULL
, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
3520 szConnection
, /* HTTP_QUERY_CONNECTION = 23 */
3521 szAccept
, /* HTTP_QUERY_ACCEPT = 24 */
3522 szAccept_Charset
, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
3523 szAccept_Encoding
, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
3524 szAccept_Language
, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
3525 szAuthorization
, /* HTTP_QUERY_AUTHORIZATION = 28 */
3526 szContent_Encoding
, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
3527 NULL
, /* HTTP_QUERY_FORWARDED = 30 */
3528 NULL
, /* HTTP_QUERY_FROM = 31 */
3529 szIf_Modified_Since
, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
3530 szLocation
, /* HTTP_QUERY_LOCATION = 33 */
3531 NULL
, /* HTTP_QUERY_ORIG_URI = 34 */
3532 szReferer
, /* HTTP_QUERY_REFERER = 35 */
3533 szRetry_After
, /* HTTP_QUERY_RETRY_AFTER = 36 */
3534 szServer
, /* HTTP_QUERY_SERVER = 37 */
3535 NULL
, /* HTTP_TITLE = 38 */
3536 szUser_Agent
, /* HTTP_QUERY_USER_AGENT = 39 */
3537 szWWW_Authenticate
, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
3538 szProxy_Authenticate
, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
3539 szAccept_Ranges
, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
3540 szSet_Cookie
, /* HTTP_QUERY_SET_COOKIE = 43 */
3541 szCookie
, /* HTTP_QUERY_COOKIE = 44 */
3542 NULL
, /* HTTP_QUERY_REQUEST_METHOD = 45 */
3543 NULL
, /* HTTP_QUERY_REFRESH = 46 */
3544 szContent_Disposition
, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
3545 szAge
, /* HTTP_QUERY_AGE = 48 */
3546 szCache_Control
, /* HTTP_QUERY_CACHE_CONTROL = 49 */
3547 szContent_Base
, /* HTTP_QUERY_CONTENT_BASE = 50 */
3548 szContent_Location
, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
3549 szContent_MD5
, /* HTTP_QUERY_CONTENT_MD5 = 52 */
3550 szContent_Range
, /* HTTP_QUERY_CONTENT_RANGE = 53 */
3551 szETag
, /* HTTP_QUERY_ETAG = 54 */
3552 hostW
, /* HTTP_QUERY_HOST = 55 */
3553 szIf_Match
, /* HTTP_QUERY_IF_MATCH = 56 */
3554 szIf_None_Match
, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
3555 szIf_Range
, /* HTTP_QUERY_IF_RANGE = 58 */
3556 szIf_Unmodified_Since
, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
3557 szMax_Forwards
, /* HTTP_QUERY_MAX_FORWARDS = 60 */
3558 szProxy_Authorization
, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
3559 szRange
, /* HTTP_QUERY_RANGE = 62 */
3560 szTransfer_Encoding
, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
3561 szUpgrade
, /* HTTP_QUERY_UPGRADE = 64 */
3562 szVary
, /* HTTP_QUERY_VARY = 65 */
3563 szVia
, /* HTTP_QUERY_VIA = 66 */
3564 szWarning
, /* HTTP_QUERY_WARNING = 67 */
3565 szExpect
, /* HTTP_QUERY_EXPECT = 68 */
3566 szProxy_Connection
, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
3567 szUnless_Modified_Since
, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
3570 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
3572 /***********************************************************************
3573 * HTTP_HttpQueryInfoW (internal)
3575 static DWORD
HTTP_HttpQueryInfoW(http_request_t
*request
, DWORD dwInfoLevel
,
3576 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
3578 LPHTTPHEADERW lphttpHdr
= NULL
;
3579 BOOL request_only
= !!(dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
);
3580 INT requested_index
= lpdwIndex
? *lpdwIndex
: 0;
3581 DWORD level
= (dwInfoLevel
& ~HTTP_QUERY_MODIFIER_FLAGS_MASK
);
3584 EnterCriticalSection( &request
->headers_section
);
3586 /* Find requested header structure */
3589 case HTTP_QUERY_CUSTOM
:
3592 LeaveCriticalSection( &request
->headers_section
);
3593 return ERROR_INVALID_PARAMETER
;
3595 index
= HTTP_GetCustomHeaderIndex(request
, lpBuffer
, requested_index
, request_only
);
3597 case HTTP_QUERY_RAW_HEADERS_CRLF
:
3601 DWORD res
= ERROR_INVALID_PARAMETER
;
3604 headers
= build_request_header(request
, request
->verb
, request
->path
, request
->version
, TRUE
);
3606 headers
= build_response_header(request
, TRUE
);
3609 LeaveCriticalSection( &request
->headers_section
);
3610 return ERROR_OUTOFMEMORY
;
3613 len
= strlenW(headers
) * sizeof(WCHAR
);
3614 if (len
+ sizeof(WCHAR
) > *lpdwBufferLength
)
3616 len
+= sizeof(WCHAR
);
3617 res
= ERROR_INSUFFICIENT_BUFFER
;
3621 memcpy(lpBuffer
, headers
, len
+ sizeof(WCHAR
));
3622 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
/ sizeof(WCHAR
)));
3623 res
= ERROR_SUCCESS
;
3625 *lpdwBufferLength
= len
;
3628 LeaveCriticalSection( &request
->headers_section
);
3631 case HTTP_QUERY_RAW_HEADERS
:
3637 headers
= build_request_header(request
, request
->verb
, request
->path
, request
->version
, FALSE
);
3639 headers
= build_response_header(request
, FALSE
);
3643 LeaveCriticalSection( &request
->headers_section
);
3644 return ERROR_OUTOFMEMORY
;
3647 len
= strlenW(headers
) * sizeof(WCHAR
);
3648 if (len
> *lpdwBufferLength
)
3650 *lpdwBufferLength
= len
;
3652 LeaveCriticalSection( &request
->headers_section
);
3653 return ERROR_INSUFFICIENT_BUFFER
;
3660 TRACE("returning data: %s\n", debugstr_wn(headers
, len
/ sizeof(WCHAR
)));
3662 for (i
= 0; i
< len
/ sizeof(WCHAR
); i
++)
3664 if (headers
[i
] == '\n')
3667 memcpy(lpBuffer
, headers
, len
);
3669 *lpdwBufferLength
= len
- sizeof(WCHAR
);
3672 LeaveCriticalSection( &request
->headers_section
);
3673 return ERROR_SUCCESS
;
3675 case HTTP_QUERY_STATUS_TEXT
:
3676 if (request
->statusText
)
3678 DWORD len
= strlenW(request
->statusText
);
3679 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
3681 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
3682 LeaveCriticalSection( &request
->headers_section
);
3683 return ERROR_INSUFFICIENT_BUFFER
;
3687 memcpy(lpBuffer
, request
->statusText
, (len
+ 1) * sizeof(WCHAR
));
3688 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
));
3690 *lpdwBufferLength
= len
* sizeof(WCHAR
);
3691 LeaveCriticalSection( &request
->headers_section
);
3692 return ERROR_SUCCESS
;
3695 case HTTP_QUERY_VERSION
:
3696 if (request
->version
)
3698 DWORD len
= strlenW(request
->version
);
3699 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
3701 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
3702 LeaveCriticalSection( &request
->headers_section
);
3703 return ERROR_INSUFFICIENT_BUFFER
;
3707 memcpy(lpBuffer
, request
->version
, (len
+ 1) * sizeof(WCHAR
));
3708 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
));
3710 *lpdwBufferLength
= len
* sizeof(WCHAR
);
3711 LeaveCriticalSection( &request
->headers_section
);
3712 return ERROR_SUCCESS
;
3715 case HTTP_QUERY_CONTENT_ENCODING
:
3716 index
= HTTP_GetCustomHeaderIndex(request
, header_lookup
[request
->read_gzip
? HTTP_QUERY_CONTENT_TYPE
: level
],
3717 requested_index
,request_only
);
3719 case HTTP_QUERY_STATUS_CODE
: {
3720 DWORD res
= ERROR_SUCCESS
;
3724 LeaveCriticalSection( &request
->headers_section
);
3725 return ERROR_HTTP_INVALID_QUERY_REQUEST
;
3731 if(dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
) {
3732 if(*lpdwBufferLength
>= sizeof(DWORD
))
3733 *(DWORD
*)lpBuffer
= request
->status_code
;
3735 res
= ERROR_INSUFFICIENT_BUFFER
;
3736 *lpdwBufferLength
= sizeof(DWORD
);
3740 static const WCHAR formatW
[] = {'%','u',0};
3742 size
= sprintfW(buf
, formatW
, request
->status_code
) * sizeof(WCHAR
);
3744 if(size
<= *lpdwBufferLength
) {
3745 memcpy(lpBuffer
, buf
, size
+sizeof(WCHAR
));
3747 size
+= sizeof(WCHAR
);
3748 res
= ERROR_INSUFFICIENT_BUFFER
;
3751 *lpdwBufferLength
= size
;
3753 LeaveCriticalSection( &request
->headers_section
);
3757 assert (LAST_TABLE_HEADER
== (HTTP_QUERY_UNLESS_MODIFIED_SINCE
+ 1));
3759 if (level
< LAST_TABLE_HEADER
&& header_lookup
[level
])
3760 index
= HTTP_GetCustomHeaderIndex(request
, header_lookup
[level
],
3761 requested_index
,request_only
);
3765 lphttpHdr
= &request
->custHeaders
[index
];
3767 /* Ensure header satisfies requested attributes */
3769 ((dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
) &&
3770 (~lphttpHdr
->wFlags
& HDR_ISREQUEST
)))
3772 LeaveCriticalSection( &request
->headers_section
);
3773 return ERROR_HTTP_HEADER_NOT_FOUND
;
3776 /* coalesce value to requested type */
3777 if (dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
&& lpBuffer
)
3779 *(int *)lpBuffer
= atoiW(lphttpHdr
->lpszValue
);
3780 TRACE(" returning number: %d\n", *(int *)lpBuffer
);
3782 else if (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
&& lpBuffer
)
3788 tmpTime
= ConvertTimeString(lphttpHdr
->lpszValue
);
3790 tmpTM
= *gmtime(&tmpTime
);
3791 STHook
= (SYSTEMTIME
*)lpBuffer
;
3792 STHook
->wDay
= tmpTM
.tm_mday
;
3793 STHook
->wHour
= tmpTM
.tm_hour
;
3794 STHook
->wMilliseconds
= 0;
3795 STHook
->wMinute
= tmpTM
.tm_min
;
3796 STHook
->wDayOfWeek
= tmpTM
.tm_wday
;
3797 STHook
->wMonth
= tmpTM
.tm_mon
+ 1;
3798 STHook
->wSecond
= tmpTM
.tm_sec
;
3799 STHook
->wYear
= 1900+tmpTM
.tm_year
;
3801 TRACE(" returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
3802 STHook
->wYear
, STHook
->wMonth
, STHook
->wDay
, STHook
->wDayOfWeek
,
3803 STHook
->wHour
, STHook
->wMinute
, STHook
->wSecond
, STHook
->wMilliseconds
);
3805 else if (lphttpHdr
->lpszValue
)
3807 DWORD len
= (strlenW(lphttpHdr
->lpszValue
) + 1) * sizeof(WCHAR
);
3809 if (len
> *lpdwBufferLength
)
3811 *lpdwBufferLength
= len
;
3812 LeaveCriticalSection( &request
->headers_section
);
3813 return ERROR_INSUFFICIENT_BUFFER
;
3817 memcpy(lpBuffer
, lphttpHdr
->lpszValue
, len
);
3818 TRACE("! returning string: %s\n", debugstr_w(lpBuffer
));
3820 *lpdwBufferLength
= len
- sizeof(WCHAR
);
3822 if (lpdwIndex
) (*lpdwIndex
)++;
3824 LeaveCriticalSection( &request
->headers_section
);
3825 return ERROR_SUCCESS
;
3828 /***********************************************************************
3829 * HttpQueryInfoW (WININET.@)
3831 * Queries for information about an HTTP request
3838 BOOL WINAPI
HttpQueryInfoW(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
3839 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
3841 http_request_t
*request
;
3844 if (TRACE_ON(wininet
)) {
3845 #define FE(x) { x, #x }
3846 static const wininet_flag_info query_flags
[] = {
3847 FE(HTTP_QUERY_MIME_VERSION
),
3848 FE(HTTP_QUERY_CONTENT_TYPE
),
3849 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING
),
3850 FE(HTTP_QUERY_CONTENT_ID
),
3851 FE(HTTP_QUERY_CONTENT_DESCRIPTION
),
3852 FE(HTTP_QUERY_CONTENT_LENGTH
),
3853 FE(HTTP_QUERY_CONTENT_LANGUAGE
),
3854 FE(HTTP_QUERY_ALLOW
),
3855 FE(HTTP_QUERY_PUBLIC
),
3856 FE(HTTP_QUERY_DATE
),
3857 FE(HTTP_QUERY_EXPIRES
),
3858 FE(HTTP_QUERY_LAST_MODIFIED
),
3859 FE(HTTP_QUERY_MESSAGE_ID
),
3861 FE(HTTP_QUERY_DERIVED_FROM
),
3862 FE(HTTP_QUERY_COST
),
3863 FE(HTTP_QUERY_LINK
),
3864 FE(HTTP_QUERY_PRAGMA
),
3865 FE(HTTP_QUERY_VERSION
),
3866 FE(HTTP_QUERY_STATUS_CODE
),
3867 FE(HTTP_QUERY_STATUS_TEXT
),
3868 FE(HTTP_QUERY_RAW_HEADERS
),
3869 FE(HTTP_QUERY_RAW_HEADERS_CRLF
),
3870 FE(HTTP_QUERY_CONNECTION
),
3871 FE(HTTP_QUERY_ACCEPT
),
3872 FE(HTTP_QUERY_ACCEPT_CHARSET
),
3873 FE(HTTP_QUERY_ACCEPT_ENCODING
),
3874 FE(HTTP_QUERY_ACCEPT_LANGUAGE
),
3875 FE(HTTP_QUERY_AUTHORIZATION
),
3876 FE(HTTP_QUERY_CONTENT_ENCODING
),
3877 FE(HTTP_QUERY_FORWARDED
),
3878 FE(HTTP_QUERY_FROM
),
3879 FE(HTTP_QUERY_IF_MODIFIED_SINCE
),
3880 FE(HTTP_QUERY_LOCATION
),
3881 FE(HTTP_QUERY_ORIG_URI
),
3882 FE(HTTP_QUERY_REFERER
),
3883 FE(HTTP_QUERY_RETRY_AFTER
),
3884 FE(HTTP_QUERY_SERVER
),
3885 FE(HTTP_QUERY_TITLE
),
3886 FE(HTTP_QUERY_USER_AGENT
),
3887 FE(HTTP_QUERY_WWW_AUTHENTICATE
),
3888 FE(HTTP_QUERY_PROXY_AUTHENTICATE
),
3889 FE(HTTP_QUERY_ACCEPT_RANGES
),
3890 FE(HTTP_QUERY_SET_COOKIE
),
3891 FE(HTTP_QUERY_COOKIE
),
3892 FE(HTTP_QUERY_REQUEST_METHOD
),
3893 FE(HTTP_QUERY_REFRESH
),
3894 FE(HTTP_QUERY_CONTENT_DISPOSITION
),
3896 FE(HTTP_QUERY_CACHE_CONTROL
),
3897 FE(HTTP_QUERY_CONTENT_BASE
),
3898 FE(HTTP_QUERY_CONTENT_LOCATION
),
3899 FE(HTTP_QUERY_CONTENT_MD5
),
3900 FE(HTTP_QUERY_CONTENT_RANGE
),
3901 FE(HTTP_QUERY_ETAG
),
3902 FE(HTTP_QUERY_HOST
),
3903 FE(HTTP_QUERY_IF_MATCH
),
3904 FE(HTTP_QUERY_IF_NONE_MATCH
),
3905 FE(HTTP_QUERY_IF_RANGE
),
3906 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE
),
3907 FE(HTTP_QUERY_MAX_FORWARDS
),
3908 FE(HTTP_QUERY_PROXY_AUTHORIZATION
),
3909 FE(HTTP_QUERY_RANGE
),
3910 FE(HTTP_QUERY_TRANSFER_ENCODING
),
3911 FE(HTTP_QUERY_UPGRADE
),
3912 FE(HTTP_QUERY_VARY
),
3914 FE(HTTP_QUERY_WARNING
),
3915 FE(HTTP_QUERY_CUSTOM
)
3917 static const wininet_flag_info modifier_flags
[] = {
3918 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS
),
3919 FE(HTTP_QUERY_FLAG_SYSTEMTIME
),
3920 FE(HTTP_QUERY_FLAG_NUMBER
),
3921 FE(HTTP_QUERY_FLAG_COALESCE
)
3924 DWORD info_mod
= dwInfoLevel
& HTTP_QUERY_MODIFIER_FLAGS_MASK
;
3925 DWORD info
= dwInfoLevel
& HTTP_QUERY_HEADER_MASK
;
3928 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest
, dwInfoLevel
, info
);
3929 TRACE(" Attribute:");
3930 for (i
= 0; i
< (sizeof(query_flags
) / sizeof(query_flags
[0])); i
++) {
3931 if (query_flags
[i
].val
== info
) {
3932 TRACE(" %s", query_flags
[i
].name
);
3936 if (i
== (sizeof(query_flags
) / sizeof(query_flags
[0]))) {
3937 TRACE(" Unknown (%08x)", info
);
3940 TRACE(" Modifier:");
3941 for (i
= 0; i
< (sizeof(modifier_flags
) / sizeof(modifier_flags
[0])); i
++) {
3942 if (modifier_flags
[i
].val
& info_mod
) {
3943 TRACE(" %s", modifier_flags
[i
].name
);
3944 info_mod
&= ~ modifier_flags
[i
].val
;
3949 TRACE(" Unknown (%08x)", info_mod
);
3954 request
= (http_request_t
*) get_handle_object( hHttpRequest
);
3955 if (NULL
== request
|| request
->hdr
.htype
!= WH_HHTTPREQ
)
3957 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
3961 if (lpBuffer
== NULL
)
3962 *lpdwBufferLength
= 0;
3963 res
= HTTP_HttpQueryInfoW( request
, dwInfoLevel
,
3964 lpBuffer
, lpdwBufferLength
, lpdwIndex
);
3968 WININET_Release( &request
->hdr
);
3970 TRACE("%u <--\n", res
);
3971 if(res
!= ERROR_SUCCESS
)
3973 return res
== ERROR_SUCCESS
;
3976 /***********************************************************************
3977 * HttpQueryInfoA (WININET.@)
3979 * Queries for information about an HTTP request
3986 BOOL WINAPI
HttpQueryInfoA(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
3987 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
3993 TRACE("%p %x\n", hHttpRequest
, dwInfoLevel
);
3995 if((dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
) ||
3996 (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
))
3998 return HttpQueryInfoW( hHttpRequest
, dwInfoLevel
, lpBuffer
,
3999 lpdwBufferLength
, lpdwIndex
);
4005 len
= (*lpdwBufferLength
)*sizeof(WCHAR
);
4006 if ((dwInfoLevel
& HTTP_QUERY_HEADER_MASK
) == HTTP_QUERY_CUSTOM
)
4008 alloclen
= MultiByteToWideChar( CP_ACP
, 0, lpBuffer
, -1, NULL
, 0 ) * sizeof(WCHAR
);
4014 bufferW
= heap_alloc(alloclen
);
4015 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
4016 if ((dwInfoLevel
& HTTP_QUERY_HEADER_MASK
) == HTTP_QUERY_CUSTOM
)
4017 MultiByteToWideChar( CP_ACP
, 0, lpBuffer
, -1, bufferW
, alloclen
/ sizeof(WCHAR
) );
4024 result
= HttpQueryInfoW( hHttpRequest
, dwInfoLevel
, bufferW
,
4028 len
= WideCharToMultiByte( CP_ACP
,0, bufferW
, len
/ sizeof(WCHAR
) + 1,
4029 lpBuffer
, *lpdwBufferLength
, NULL
, NULL
);
4030 *lpdwBufferLength
= len
- 1;
4032 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer
));
4035 /* since the strings being returned from HttpQueryInfoW should be
4036 * only ASCII characters, it is reasonable to assume that all of
4037 * the Unicode characters can be reduced to a single byte */
4038 *lpdwBufferLength
= len
/ sizeof(WCHAR
);
4040 heap_free( bufferW
);
4044 /***********************************************************************
4045 * HTTP_GetRedirectURL (internal)
4047 static LPWSTR
HTTP_GetRedirectURL(http_request_t
*request
, LPCWSTR lpszUrl
)
4049 static WCHAR szHttp
[] = {'h','t','t','p',0};
4050 static WCHAR szHttps
[] = {'h','t','t','p','s',0};
4051 http_session_t
*session
= request
->session
;
4052 URL_COMPONENTSW urlComponents
;
4053 DWORD url_length
= 0;
4055 LPWSTR combined_url
;
4057 urlComponents
.dwStructSize
= sizeof(URL_COMPONENTSW
);
4058 urlComponents
.lpszScheme
= (request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
) ? szHttps
: szHttp
;
4059 urlComponents
.dwSchemeLength
= 0;
4060 urlComponents
.lpszHostName
= request
->server
->name
;
4061 urlComponents
.dwHostNameLength
= 0;
4062 urlComponents
.nPort
= request
->server
->port
;
4063 urlComponents
.lpszUserName
= session
->userName
;
4064 urlComponents
.dwUserNameLength
= 0;
4065 urlComponents
.lpszPassword
= NULL
;
4066 urlComponents
.dwPasswordLength
= 0;
4067 urlComponents
.lpszUrlPath
= request
->path
;
4068 urlComponents
.dwUrlPathLength
= 0;
4069 urlComponents
.lpszExtraInfo
= NULL
;
4070 urlComponents
.dwExtraInfoLength
= 0;
4072 if (!InternetCreateUrlW(&urlComponents
, 0, NULL
, &url_length
) &&
4073 (GetLastError() != ERROR_INSUFFICIENT_BUFFER
))
4076 orig_url
= heap_alloc(url_length
);
4078 /* convert from bytes to characters */
4079 url_length
= url_length
/ sizeof(WCHAR
) - 1;
4080 if (!InternetCreateUrlW(&urlComponents
, 0, orig_url
, &url_length
))
4082 heap_free(orig_url
);
4087 if (!InternetCombineUrlW(orig_url
, lpszUrl
, NULL
, &url_length
, ICU_ENCODE_SPACES_ONLY
) &&
4088 (GetLastError() != ERROR_INSUFFICIENT_BUFFER
))
4090 heap_free(orig_url
);
4093 combined_url
= heap_alloc(url_length
* sizeof(WCHAR
));
4095 if (!InternetCombineUrlW(orig_url
, lpszUrl
, combined_url
, &url_length
, ICU_ENCODE_SPACES_ONLY
))
4097 heap_free(orig_url
);
4098 heap_free(combined_url
);
4101 heap_free(orig_url
);
4102 return combined_url
;
4106 /***********************************************************************
4107 * HTTP_HandleRedirect (internal)
4109 static DWORD
HTTP_HandleRedirect(http_request_t
*request
, LPCWSTR lpszUrl
)
4111 http_session_t
*session
= request
->session
;
4112 WCHAR path
[INTERNET_MAX_PATH_LENGTH
];
4116 /* if it's an absolute path, keep the same session info */
4117 lstrcpynW(path
, lpszUrl
, INTERNET_MAX_URL_LENGTH
);
4121 URL_COMPONENTSW urlComponents
;
4122 WCHAR protocol
[INTERNET_MAX_SCHEME_LENGTH
];
4123 WCHAR hostName
[INTERNET_MAX_HOST_NAME_LENGTH
];
4124 WCHAR userName
[INTERNET_MAX_USER_NAME_LENGTH
];
4125 BOOL custom_port
= FALSE
;
4127 static const WCHAR httpW
[] = {'h','t','t','p',0};
4128 static const WCHAR httpsW
[] = {'h','t','t','p','s',0};
4134 urlComponents
.dwStructSize
= sizeof(URL_COMPONENTSW
);
4135 urlComponents
.lpszScheme
= protocol
;
4136 urlComponents
.dwSchemeLength
= INTERNET_MAX_SCHEME_LENGTH
;
4137 urlComponents
.lpszHostName
= hostName
;
4138 urlComponents
.dwHostNameLength
= INTERNET_MAX_HOST_NAME_LENGTH
;
4139 urlComponents
.lpszUserName
= userName
;
4140 urlComponents
.dwUserNameLength
= INTERNET_MAX_USER_NAME_LENGTH
;
4141 urlComponents
.lpszPassword
= NULL
;
4142 urlComponents
.dwPasswordLength
= 0;
4143 urlComponents
.lpszUrlPath
= path
;
4144 urlComponents
.dwUrlPathLength
= INTERNET_MAX_PATH_LENGTH
;
4145 urlComponents
.lpszExtraInfo
= NULL
;
4146 urlComponents
.dwExtraInfoLength
= 0;
4147 if(!InternetCrackUrlW(lpszUrl
, strlenW(lpszUrl
), 0, &urlComponents
))
4148 return INTERNET_GetLastError();
4150 if(!strcmpiW(protocol
, httpW
)) {
4151 if(request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
) {
4152 TRACE("redirect from secure page to non-secure page\n");
4153 /* FIXME: warn about from secure redirect to non-secure page */
4154 request
->hdr
.dwFlags
&= ~INTERNET_FLAG_SECURE
;
4157 if(urlComponents
.nPort
== INTERNET_INVALID_PORT_NUMBER
)
4158 urlComponents
.nPort
= INTERNET_DEFAULT_HTTP_PORT
;
4159 else if(urlComponents
.nPort
!= INTERNET_DEFAULT_HTTP_PORT
)
4161 }else if(!strcmpiW(protocol
, httpsW
)) {
4162 if(!(request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
)) {
4163 TRACE("redirect from non-secure page to secure page\n");
4164 /* FIXME: notify about redirect to secure page */
4165 request
->hdr
.dwFlags
|= INTERNET_FLAG_SECURE
;
4168 if(urlComponents
.nPort
== INTERNET_INVALID_PORT_NUMBER
)
4169 urlComponents
.nPort
= INTERNET_DEFAULT_HTTPS_PORT
;
4170 else if(urlComponents
.nPort
!= INTERNET_DEFAULT_HTTPS_PORT
)
4174 heap_free(session
->hostName
);
4176 session
->hostName
= heap_strdupW(hostName
);
4177 session
->hostPort
= urlComponents
.nPort
;
4179 heap_free(session
->userName
);
4180 session
->userName
= NULL
;
4182 session
->userName
= heap_strdupW(userName
);
4184 reset_data_stream(request
);
4186 if(strcmpiW(request
->server
->name
, hostName
) || request
->server
->port
!= urlComponents
.nPort
) {
4187 server_t
*new_server
;
4189 new_server
= get_server(hostName
, urlComponents
.nPort
, urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
, TRUE
);
4190 server_release(request
->server
);
4191 request
->server
= new_server
;
4195 HTTP_ProcessHeader(request
, hostW
, request
->server
->host_port
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_REQ
);
4197 HTTP_ProcessHeader(request
, hostW
, request
->server
->name
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_REQ
);
4199 heap_free(request
->path
);
4206 rc
= UrlEscapeW(path
, NULL
, &needed
, URL_ESCAPE_SPACES_ONLY
);
4207 if (rc
!= E_POINTER
)
4208 needed
= strlenW(path
)+1;
4209 request
->path
= heap_alloc(needed
*sizeof(WCHAR
));
4210 rc
= UrlEscapeW(path
, request
->path
, &needed
,
4211 URL_ESCAPE_SPACES_ONLY
);
4214 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path
),rc
);
4215 strcpyW(request
->path
,path
);
4219 /* Remove custom content-type/length headers on redirects. */
4220 remove_header(request
, szContent_Type
, TRUE
);
4221 remove_header(request
, szContent_Length
, TRUE
);
4223 return ERROR_SUCCESS
;
4226 /***********************************************************************
4227 * HTTP_build_req (internal)
4229 * concatenate all the strings in the request together
4231 static LPWSTR
HTTP_build_req( LPCWSTR
*list
, int len
)
4236 for( t
= list
; *t
; t
++ )
4237 len
+= strlenW( *t
);
4240 str
= heap_alloc(len
*sizeof(WCHAR
));
4243 for( t
= list
; *t
; t
++ )
4249 static void HTTP_InsertCookies(http_request_t
*request
)
4254 res
= get_cookie_header(request
->server
->name
, request
->path
, &cookies
);
4255 if(res
!= ERROR_SUCCESS
|| !cookies
)
4258 HTTP_HttpAddRequestHeadersW(request
, cookies
, strlenW(cookies
),
4259 HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDREQ_FLAG_ADD
);
4263 static WORD
HTTP_ParseWkday(LPCWSTR day
)
4265 static const WCHAR days
[7][4] = {{ 's','u','n',0 },
4273 for (i
= 0; i
< sizeof(days
)/sizeof(*days
); i
++)
4274 if (!strcmpiW(day
, days
[i
]))
4281 static WORD
HTTP_ParseMonth(LPCWSTR month
)
4283 static const WCHAR jan
[] = { 'j','a','n',0 };
4284 static const WCHAR feb
[] = { 'f','e','b',0 };
4285 static const WCHAR mar
[] = { 'm','a','r',0 };
4286 static const WCHAR apr
[] = { 'a','p','r',0 };
4287 static const WCHAR may
[] = { 'm','a','y',0 };
4288 static const WCHAR jun
[] = { 'j','u','n',0 };
4289 static const WCHAR jul
[] = { 'j','u','l',0 };
4290 static const WCHAR aug
[] = { 'a','u','g',0 };
4291 static const WCHAR sep
[] = { 's','e','p',0 };
4292 static const WCHAR oct
[] = { 'o','c','t',0 };
4293 static const WCHAR nov
[] = { 'n','o','v',0 };
4294 static const WCHAR dec
[] = { 'd','e','c',0 };
4296 if (!strcmpiW(month
, jan
)) return 1;
4297 if (!strcmpiW(month
, feb
)) return 2;
4298 if (!strcmpiW(month
, mar
)) return 3;
4299 if (!strcmpiW(month
, apr
)) return 4;
4300 if (!strcmpiW(month
, may
)) return 5;
4301 if (!strcmpiW(month
, jun
)) return 6;
4302 if (!strcmpiW(month
, jul
)) return 7;
4303 if (!strcmpiW(month
, aug
)) return 8;
4304 if (!strcmpiW(month
, sep
)) return 9;
4305 if (!strcmpiW(month
, oct
)) return 10;
4306 if (!strcmpiW(month
, nov
)) return 11;
4307 if (!strcmpiW(month
, dec
)) return 12;
4312 /* Parses the string pointed to by *str, assumed to be a 24-hour time HH:MM:SS,
4313 * optionally preceded by whitespace.
4314 * Upon success, returns TRUE, sets the wHour, wMinute, and wSecond fields of
4315 * st, and sets *str to the first character after the time format.
4317 static BOOL
HTTP_ParseTime(SYSTEMTIME
*st
, LPCWSTR
*str
)
4323 while (isspaceW(*ptr
))
4326 num
= strtoulW(ptr
, &nextPtr
, 10);
4327 if (!nextPtr
|| nextPtr
<= ptr
|| *nextPtr
!= ':')
4329 ERR("unexpected time format %s\n", debugstr_w(ptr
));
4334 ERR("unexpected hour in time format %s\n", debugstr_w(ptr
));
4338 st
->wHour
= (WORD
)num
;
4339 num
= strtoulW(ptr
, &nextPtr
, 10);
4340 if (!nextPtr
|| nextPtr
<= ptr
|| *nextPtr
!= ':')
4342 ERR("unexpected time format %s\n", debugstr_w(ptr
));
4347 ERR("unexpected minute in time format %s\n", debugstr_w(ptr
));
4351 st
->wMinute
= (WORD
)num
;
4352 num
= strtoulW(ptr
, &nextPtr
, 10);
4353 if (!nextPtr
|| nextPtr
<= ptr
)
4355 ERR("unexpected time format %s\n", debugstr_w(ptr
));
4360 ERR("unexpected second in time format %s\n", debugstr_w(ptr
));
4365 st
->wSecond
= (WORD
)num
;
4369 static BOOL
HTTP_ParseDateAsAsctime(LPCWSTR value
, FILETIME
*ft
)
4371 static const WCHAR gmt
[]= { 'G','M','T',0 };
4372 WCHAR day
[4], *dayPtr
, month
[4], *monthPtr
, *nextPtr
;
4374 SYSTEMTIME st
= { 0 };
4377 for (ptr
= value
, dayPtr
= day
; *ptr
&& !isspaceW(*ptr
) &&
4378 dayPtr
- day
< sizeof(day
) / sizeof(day
[0]) - 1; ptr
++, dayPtr
++)
4381 st
.wDayOfWeek
= HTTP_ParseWkday(day
);
4382 if (st
.wDayOfWeek
>= 7)
4384 ERR("unexpected weekday %s\n", debugstr_w(day
));
4388 while (isspaceW(*ptr
))
4391 for (monthPtr
= month
; !isspace(*ptr
) &&
4392 monthPtr
- month
< sizeof(month
) / sizeof(month
[0]) - 1;
4396 st
.wMonth
= HTTP_ParseMonth(month
);
4397 if (!st
.wMonth
|| st
.wMonth
> 12)
4399 ERR("unexpected month %s\n", debugstr_w(month
));
4403 while (isspaceW(*ptr
))
4406 num
= strtoulW(ptr
, &nextPtr
, 10);
4407 if (!nextPtr
|| nextPtr
<= ptr
|| !num
|| num
> 31)
4409 ERR("unexpected day %s\n", debugstr_w(ptr
));
4413 st
.wDay
= (WORD
)num
;
4415 while (isspaceW(*ptr
))
4418 if (!HTTP_ParseTime(&st
, &ptr
))
4421 while (isspaceW(*ptr
))
4424 num
= strtoulW(ptr
, &nextPtr
, 10);
4425 if (!nextPtr
|| nextPtr
<= ptr
|| num
< 1601 || num
> 30827)
4427 ERR("unexpected year %s\n", debugstr_w(ptr
));
4431 st
.wYear
= (WORD
)num
;
4433 while (isspaceW(*ptr
))
4436 /* asctime() doesn't report a timezone, but some web servers do, so accept
4437 * with or without GMT.
4439 if (*ptr
&& strcmpW(ptr
, gmt
))
4441 ERR("unexpected timezone %s\n", debugstr_w(ptr
));
4444 return SystemTimeToFileTime(&st
, ft
);
4447 static BOOL
HTTP_ParseRfc1123Date(LPCWSTR value
, FILETIME
*ft
)
4449 static const WCHAR gmt
[]= { 'G','M','T',0 };
4450 WCHAR
*nextPtr
, day
[4], month
[4], *monthPtr
;
4453 SYSTEMTIME st
= { 0 };
4455 ptr
= strchrW(value
, ',');
4458 if (ptr
- value
!= 3)
4460 WARN("unexpected weekday %s\n", debugstr_wn(value
, ptr
- value
));
4463 memcpy(day
, value
, (ptr
- value
) * sizeof(WCHAR
));
4465 st
.wDayOfWeek
= HTTP_ParseWkday(day
);
4466 if (st
.wDayOfWeek
> 6)
4468 WARN("unexpected weekday %s\n", debugstr_wn(value
, ptr
- value
));
4473 while (isspaceW(*ptr
))
4476 num
= strtoulW(ptr
, &nextPtr
, 10);
4477 if (!nextPtr
|| nextPtr
<= ptr
|| !num
|| num
> 31)
4479 WARN("unexpected day %s\n", debugstr_w(value
));
4483 st
.wDay
= (WORD
)num
;
4485 while (isspaceW(*ptr
))
4488 for (monthPtr
= month
; !isspace(*ptr
) &&
4489 monthPtr
- month
< sizeof(month
) / sizeof(month
[0]) - 1;
4493 st
.wMonth
= HTTP_ParseMonth(month
);
4494 if (!st
.wMonth
|| st
.wMonth
> 12)
4496 WARN("unexpected month %s\n", debugstr_w(month
));
4500 while (isspaceW(*ptr
))
4503 num
= strtoulW(ptr
, &nextPtr
, 10);
4504 if (!nextPtr
|| nextPtr
<= ptr
|| num
< 1601 || num
> 30827)
4506 ERR("unexpected year %s\n", debugstr_w(value
));
4510 st
.wYear
= (WORD
)num
;
4512 if (!HTTP_ParseTime(&st
, &ptr
))
4515 while (isspaceW(*ptr
))
4518 if (strcmpW(ptr
, gmt
))
4520 ERR("unexpected time zone %s\n", debugstr_w(ptr
));
4523 return SystemTimeToFileTime(&st
, ft
);
4526 static WORD
HTTP_ParseWeekday(LPCWSTR day
)
4528 static const WCHAR days
[7][10] = {{ 's','u','n','d','a','y',0 },
4529 { 'm','o','n','d','a','y',0 },
4530 { 't','u','e','s','d','a','y',0 },
4531 { 'w','e','d','n','e','s','d','a','y',0 },
4532 { 't','h','u','r','s','d','a','y',0 },
4533 { 'f','r','i','d','a','y',0 },
4534 { 's','a','t','u','r','d','a','y',0 }};
4536 for (i
= 0; i
< sizeof(days
)/sizeof(*days
); i
++)
4537 if (!strcmpiW(day
, days
[i
]))
4544 static BOOL
HTTP_ParseRfc850Date(LPCWSTR value
, FILETIME
*ft
)
4546 static const WCHAR gmt
[]= { 'G','M','T',0 };
4547 WCHAR
*nextPtr
, day
[10], month
[4], *monthPtr
;
4550 SYSTEMTIME st
= { 0 };
4552 ptr
= strchrW(value
, ',');
4555 if (ptr
- value
== 3)
4557 memcpy(day
, value
, (ptr
- value
) * sizeof(WCHAR
));
4559 st
.wDayOfWeek
= HTTP_ParseWkday(day
);
4560 if (st
.wDayOfWeek
> 6)
4562 ERR("unexpected weekday %s\n", debugstr_wn(value
, ptr
- value
));
4566 else if (ptr
- value
< sizeof(day
) / sizeof(day
[0]))
4568 memcpy(day
, value
, (ptr
- value
) * sizeof(WCHAR
));
4569 day
[ptr
- value
+ 1] = 0;
4570 st
.wDayOfWeek
= HTTP_ParseWeekday(day
);
4571 if (st
.wDayOfWeek
> 6)
4573 ERR("unexpected weekday %s\n", debugstr_wn(value
, ptr
- value
));
4579 ERR("unexpected weekday %s\n", debugstr_wn(value
, ptr
- value
));
4584 while (isspaceW(*ptr
))
4587 num
= strtoulW(ptr
, &nextPtr
, 10);
4588 if (!nextPtr
|| nextPtr
<= ptr
|| !num
|| num
> 31)
4590 ERR("unexpected day %s\n", debugstr_w(value
));
4594 st
.wDay
= (WORD
)num
;
4598 ERR("unexpected month format %s\n", debugstr_w(ptr
));
4603 for (monthPtr
= month
; *ptr
!= '-' &&
4604 monthPtr
- month
< sizeof(month
) / sizeof(month
[0]) - 1;
4608 st
.wMonth
= HTTP_ParseMonth(month
);
4609 if (!st
.wMonth
|| st
.wMonth
> 12)
4611 ERR("unexpected month %s\n", debugstr_w(month
));
4617 ERR("unexpected year format %s\n", debugstr_w(ptr
));
4622 num
= strtoulW(ptr
, &nextPtr
, 10);
4623 if (!nextPtr
|| nextPtr
<= ptr
|| num
< 1601 || num
> 30827)
4625 ERR("unexpected year %s\n", debugstr_w(value
));
4629 st
.wYear
= (WORD
)num
;
4631 if (!HTTP_ParseTime(&st
, &ptr
))
4634 while (isspaceW(*ptr
))
4637 if (strcmpW(ptr
, gmt
))
4639 ERR("unexpected time zone %s\n", debugstr_w(ptr
));
4642 return SystemTimeToFileTime(&st
, ft
);
4645 static BOOL
HTTP_ParseDate(LPCWSTR value
, FILETIME
*ft
)
4647 static const WCHAR zero
[] = { '0',0 };
4650 if (!strcmpW(value
, zero
))
4652 ft
->dwLowDateTime
= ft
->dwHighDateTime
= 0;
4655 else if (strchrW(value
, ','))
4657 ret
= HTTP_ParseRfc1123Date(value
, ft
);
4660 ret
= HTTP_ParseRfc850Date(value
, ft
);
4662 ERR("unexpected date format %s\n", debugstr_w(value
));
4667 ret
= HTTP_ParseDateAsAsctime(value
, ft
);
4669 ERR("unexpected date format %s\n", debugstr_w(value
));
4674 static void HTTP_ProcessExpires(http_request_t
*request
)
4676 BOOL expirationFound
= FALSE
;
4679 EnterCriticalSection( &request
->headers_section
);
4681 /* Look for a Cache-Control header with a max-age directive, as it takes
4682 * precedence over the Expires header.
4684 headerIndex
= HTTP_GetCustomHeaderIndex(request
, szCache_Control
, 0, FALSE
);
4685 if (headerIndex
!= -1)
4687 LPHTTPHEADERW ccHeader
= &request
->custHeaders
[headerIndex
];
4690 for (ptr
= ccHeader
->lpszValue
; ptr
&& *ptr
; )
4692 LPWSTR comma
= strchrW(ptr
, ','), end
, equal
;
4697 end
= ptr
+ strlenW(ptr
);
4698 for (equal
= end
- 1; equal
> ptr
&& *equal
!= '='; equal
--)
4702 static const WCHAR max_age
[] = {
4703 'm','a','x','-','a','g','e',0 };
4705 if (!strncmpiW(ptr
, max_age
, equal
- ptr
- 1))
4710 age
= strtoulW(equal
+ 1, &nextPtr
, 10);
4711 if (nextPtr
> equal
+ 1)
4715 NtQuerySystemTime( &ft
);
4716 /* Age is in seconds, FILETIME resolution is in
4717 * 100 nanosecond intervals.
4719 ft
.QuadPart
+= age
* (ULONGLONG
)1000000;
4720 request
->expires
.dwLowDateTime
= ft
.u
.LowPart
;
4721 request
->expires
.dwHighDateTime
= ft
.u
.HighPart
;
4722 expirationFound
= TRUE
;
4729 while (isspaceW(*ptr
))
4736 if (!expirationFound
)
4738 headerIndex
= HTTP_GetCustomHeaderIndex(request
, szExpires
, 0, FALSE
);
4739 if (headerIndex
!= -1)
4741 LPHTTPHEADERW expiresHeader
= &request
->custHeaders
[headerIndex
];
4744 if (HTTP_ParseDate(expiresHeader
->lpszValue
, &ft
))
4746 expirationFound
= TRUE
;
4747 request
->expires
= ft
;
4751 if (!expirationFound
)
4755 /* With no known age, default to 10 minutes until expiration. */
4756 NtQuerySystemTime( &t
);
4757 t
.QuadPart
+= 10 * 60 * (ULONGLONG
)10000000;
4758 request
->expires
.dwLowDateTime
= t
.u
.LowPart
;
4759 request
->expires
.dwHighDateTime
= t
.u
.HighPart
;
4762 LeaveCriticalSection( &request
->headers_section
);
4765 static void HTTP_ProcessLastModified(http_request_t
*request
)
4769 EnterCriticalSection( &request
->headers_section
);
4771 headerIndex
= HTTP_GetCustomHeaderIndex(request
, szLast_Modified
, 0, FALSE
);
4772 if (headerIndex
!= -1)
4774 LPHTTPHEADERW expiresHeader
= &request
->custHeaders
[headerIndex
];
4777 if (HTTP_ParseDate(expiresHeader
->lpszValue
, &ft
))
4778 request
->last_modified
= ft
;
4781 LeaveCriticalSection( &request
->headers_section
);
4784 static void http_process_keep_alive(http_request_t
*req
)
4788 EnterCriticalSection( &req
->headers_section
);
4790 if ((index
= HTTP_GetCustomHeaderIndex(req
, szConnection
, 0, FALSE
)) != -1)
4791 req
->netconn
->keep_alive
= !strcmpiW(req
->custHeaders
[index
].lpszValue
, szKeepAlive
);
4792 else if ((index
= HTTP_GetCustomHeaderIndex(req
, szProxy_Connection
, 0, FALSE
)) != -1)
4793 req
->netconn
->keep_alive
= !strcmpiW(req
->custHeaders
[index
].lpszValue
, szKeepAlive
);
4795 req
->netconn
->keep_alive
= !strcmpiW(req
->version
, g_szHttp1_1
);
4797 LeaveCriticalSection( &req
->headers_section
);
4800 static DWORD
open_http_connection(http_request_t
*request
, BOOL
*reusing
)
4802 const BOOL is_https
= (request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
) != 0;
4803 netconn_t
*netconn
= NULL
;
4806 reset_data_stream(request
);
4808 if (request
->netconn
)
4810 if (is_valid_netconn(request
->netconn
) && NETCON_is_alive(request
->netconn
))
4813 return ERROR_SUCCESS
;
4817 free_netconn(request
->netconn
);
4818 request
->netconn
= NULL
;
4822 res
= HTTP_ResolveName(request
);
4823 if(res
!= ERROR_SUCCESS
)
4826 EnterCriticalSection(&connection_pool_cs
);
4828 while(!list_empty(&request
->server
->conn_pool
)) {
4829 netconn
= LIST_ENTRY(list_head(&request
->server
->conn_pool
), netconn_t
, pool_entry
);
4830 list_remove(&netconn
->pool_entry
);
4832 if(is_valid_netconn(netconn
) && NETCON_is_alive(netconn
))
4835 TRACE("connection %p closed during idle\n", netconn
);
4836 free_netconn(netconn
);
4840 LeaveCriticalSection(&connection_pool_cs
);
4843 TRACE("<-- reusing %p netconn\n", netconn
);
4844 request
->netconn
= netconn
;
4846 return ERROR_SUCCESS
;
4849 TRACE("connecting to %s, proxy %s\n", debugstr_w(request
->server
->name
),
4850 request
->proxy
? debugstr_w(request
->proxy
->name
) : "(null)");
4852 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
4853 INTERNET_STATUS_CONNECTING_TO_SERVER
,
4854 request
->server
->addr_str
,
4855 strlen(request
->server
->addr_str
)+1);
4857 res
= create_netconn(is_https
, request
->proxy
? request
->proxy
: request
->server
, request
->security_flags
,
4858 (request
->hdr
.ErrorMask
& INTERNET_ERROR_MASK_COMBINED_SEC_CERT
) != 0,
4859 request
->connect_timeout
, &netconn
);
4860 if(res
!= ERROR_SUCCESS
) {
4861 ERR("create_netconn failed: %u\n", res
);
4865 request
->netconn
= netconn
;
4867 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
4868 INTERNET_STATUS_CONNECTED_TO_SERVER
,
4869 request
->server
->addr_str
, strlen(request
->server
->addr_str
)+1);
4872 TRACE("Created connection to %s: %p\n", debugstr_w(request
->server
->name
), netconn
);
4873 return ERROR_SUCCESS
;
4876 static char *build_ascii_request( const WCHAR
*str
, void *data
, DWORD data_len
, DWORD
*out_len
)
4878 int len
= WideCharToMultiByte( CP_ACP
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
4881 if (!(ret
= heap_alloc( len
+ data_len
))) return NULL
;
4882 WideCharToMultiByte( CP_ACP
, 0, str
, -1, ret
, len
, NULL
, NULL
);
4883 if (data_len
) memcpy( ret
+ len
- 1, data
, data_len
);
4884 *out_len
= len
+ data_len
- 1;
4889 /***********************************************************************
4890 * HTTP_HttpSendRequestW (internal)
4892 * Sends the specified request to the HTTP server
4895 * ERROR_SUCCESS on success
4896 * win32 error code on failure
4899 static DWORD
HTTP_HttpSendRequestW(http_request_t
*request
, LPCWSTR lpszHeaders
,
4900 DWORD dwHeaderLength
, LPVOID lpOptional
, DWORD dwOptionalLength
,
4901 DWORD dwContentLength
, BOOL bEndRequest
)
4903 static const WCHAR szContentLength
[] =
4904 { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0 };
4905 BOOL redirected
= FALSE
, secure_proxy_connect
= FALSE
, loop_next
;
4906 LPWSTR requestString
= NULL
;
4907 INT responseLen
, cnt
;
4908 WCHAR contentLengthStr
[sizeof szContentLength
/2 /* includes \r\n */ + 20 /* int */ ];
4911 TRACE("--> %p\n", request
);
4913 assert(request
->hdr
.htype
== WH_HHTTPREQ
);
4915 /* if the verb is NULL default to GET */
4917 request
->verb
= heap_strdupW(szGET
);
4919 if (dwContentLength
|| strcmpW(request
->verb
, szGET
))
4921 sprintfW(contentLengthStr
, szContentLength
, dwContentLength
);
4922 HTTP_HttpAddRequestHeadersW(request
, contentLengthStr
, -1L, HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
4923 request
->bytesToWrite
= dwContentLength
;
4925 if (request
->session
->appInfo
->agent
)
4927 WCHAR
*agent_header
;
4928 static const WCHAR user_agent
[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0};
4931 len
= strlenW(request
->session
->appInfo
->agent
) + strlenW(user_agent
);
4932 agent_header
= heap_alloc(len
* sizeof(WCHAR
));
4933 sprintfW(agent_header
, user_agent
, request
->session
->appInfo
->agent
);
4935 HTTP_HttpAddRequestHeadersW(request
, agent_header
, strlenW(agent_header
), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
4936 heap_free(agent_header
);
4938 if (request
->hdr
.dwFlags
& INTERNET_FLAG_PRAGMA_NOCACHE
)
4940 static const WCHAR pragma_nocache
[] = {'P','r','a','g','m','a',':',' ','n','o','-','c','a','c','h','e','\r','\n',0};
4941 HTTP_HttpAddRequestHeadersW(request
, pragma_nocache
, strlenW(pragma_nocache
), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
4943 if ((request
->hdr
.dwFlags
& INTERNET_FLAG_NO_CACHE_WRITE
) && strcmpW(request
->verb
, szGET
))
4945 static const WCHAR cache_control
[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l',':',
4946 ' ','n','o','-','c','a','c','h','e','\r','\n',0};
4947 HTTP_HttpAddRequestHeadersW(request
, cache_control
, strlenW(cache_control
), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
4950 /* add the headers the caller supplied */
4951 if( lpszHeaders
&& dwHeaderLength
)
4952 HTTP_HttpAddRequestHeadersW(request
, lpszHeaders
, dwHeaderLength
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REPLACE
);
4956 DWORD len
, data_len
= dwOptionalLength
;
4957 BOOL reusing_connection
;
4963 request
->contentLength
= ~0u;
4964 request
->bytesToWrite
= 0;
4967 if (TRACE_ON(wininet
))
4971 EnterCriticalSection( &request
->headers_section
);
4972 host
= HTTP_GetHeader( request
, hostW
);
4973 TRACE("Going to url %s %s\n", debugstr_w(host
->lpszValue
), debugstr_w(request
->path
));
4974 LeaveCriticalSection( &request
->headers_section
);
4977 HTTP_FixURL(request
);
4978 if (request
->hdr
.dwFlags
& INTERNET_FLAG_KEEP_CONNECTION
)
4980 HTTP_ProcessHeader(request
, szConnection
, szKeepAlive
,
4981 HTTP_ADDHDR_FLAG_REQ
| HTTP_ADDHDR_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_ADD
);
4983 HTTP_InsertAuthorization(request
, request
->authInfo
, szAuthorization
);
4984 HTTP_InsertAuthorization(request
, request
->proxyAuthInfo
, szProxy_Authorization
);
4986 if (!(request
->hdr
.dwFlags
& INTERNET_FLAG_NO_COOKIES
))
4987 HTTP_InsertCookies(request
);
4989 res
= open_http_connection(request
, &reusing_connection
);
4990 if (res
!= ERROR_SUCCESS
)
4993 if (!reusing_connection
&& (request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
))
4995 if (request
->proxy
) secure_proxy_connect
= TRUE
;
4998 res
= NETCON_secure_connect(request
->netconn
, request
->server
);
4999 if (res
!= ERROR_SUCCESS
)
5001 WARN("failed to upgrade to secure connection\n");
5002 http_release_netconn(request
, FALSE
);
5007 if (secure_proxy_connect
)
5009 static const WCHAR connectW
[] = {'C','O','N','N','E','C','T',0};
5010 const WCHAR
*target
= request
->server
->host_port
;
5011 requestString
= build_request_header(request
, connectW
, target
, g_szHttp1_1
, TRUE
);
5013 else if (request
->proxy
&& !(request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
))
5015 WCHAR
*url
= build_proxy_path_url(request
);
5016 requestString
= build_request_header(request
, request
->verb
, url
, request
->version
, TRUE
);
5020 requestString
= build_request_header(request
, request
->verb
, request
->path
, request
->version
, TRUE
);
5022 TRACE("Request header -> %s\n", debugstr_w(requestString
) );
5024 /* send the request as ASCII, tack on the optional data */
5025 if (!lpOptional
|| redirected
|| secure_proxy_connect
)
5028 ascii_req
= build_ascii_request( requestString
, lpOptional
, data_len
, &len
);
5029 TRACE("full request -> %s\n", debugstr_a(ascii_req
) );
5031 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
5032 INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
5034 NETCON_set_timeout( request
->netconn
, TRUE
, request
->send_timeout
);
5035 res
= NETCON_send(request
->netconn
, ascii_req
, len
, 0, &cnt
);
5036 heap_free( ascii_req
);
5037 if(res
!= ERROR_SUCCESS
) {
5038 TRACE("send failed: %u\n", res
);
5039 if(!reusing_connection
)
5041 http_release_netconn(request
, FALSE
);
5046 request
->bytesWritten
= data_len
;
5048 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
5049 INTERNET_STATUS_REQUEST_SENT
,
5050 &len
, sizeof(DWORD
));
5056 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
5057 INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
5059 if (HTTP_GetResponseHeaders(request
, &responseLen
))
5061 http_release_netconn(request
, FALSE
);
5062 res
= ERROR_INTERNET_CONNECTION_ABORTED
;
5065 /* FIXME: We should know that connection is closed before sending
5066 * headers. Otherwise wrong callbacks are executed */
5067 if(!responseLen
&& reusing_connection
) {
5068 TRACE("Connection closed by server, reconnecting\n");
5069 http_release_netconn(request
, FALSE
);
5074 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
5075 INTERNET_STATUS_RESPONSE_RECEIVED
, &responseLen
,
5078 http_process_keep_alive(request
);
5079 HTTP_ProcessCookies(request
);
5080 HTTP_ProcessExpires(request
);
5081 HTTP_ProcessLastModified(request
);
5083 res
= set_content_length(request
);
5084 if(res
!= ERROR_SUCCESS
)
5086 if(!request
->contentLength
)
5087 http_release_netconn(request
, TRUE
);
5089 if (!(request
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTO_REDIRECT
) && responseLen
)
5091 WCHAR
*new_url
, szNewLocation
[INTERNET_MAX_URL_LENGTH
];
5092 dwBufferSize
=sizeof(szNewLocation
);
5093 switch(request
->status_code
) {
5094 case HTTP_STATUS_REDIRECT
:
5095 case HTTP_STATUS_MOVED
:
5096 case HTTP_STATUS_REDIRECT_KEEP_VERB
:
5097 case HTTP_STATUS_REDIRECT_METHOD
:
5098 if(HTTP_HttpQueryInfoW(request
,HTTP_QUERY_LOCATION
,szNewLocation
,&dwBufferSize
,NULL
) != ERROR_SUCCESS
)
5101 if (strcmpW(request
->verb
, szGET
) && strcmpW(request
->verb
, szHEAD
) &&
5102 request
->status_code
!= HTTP_STATUS_REDIRECT_KEEP_VERB
)
5104 heap_free(request
->verb
);
5105 request
->verb
= heap_strdupW(szGET
);
5107 http_release_netconn(request
, drain_content(request
, FALSE
));
5108 if ((new_url
= HTTP_GetRedirectURL( request
, szNewLocation
)))
5110 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
, INTERNET_STATUS_REDIRECT
,
5111 new_url
, (strlenW(new_url
) + 1) * sizeof(WCHAR
));
5112 res
= HTTP_HandleRedirect(request
, new_url
);
5113 if (res
== ERROR_SUCCESS
)
5115 heap_free(requestString
);
5118 heap_free( new_url
);
5123 if (!(request
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTH
) && res
== ERROR_SUCCESS
)
5125 WCHAR szAuthValue
[2048];
5127 if (request
->status_code
== HTTP_STATUS_DENIED
)
5129 WCHAR
*host
= get_host_header( request
);
5131 while (HTTP_HttpQueryInfoW(request
,HTTP_QUERY_WWW_AUTHENTICATE
,szAuthValue
,&dwBufferSize
,&dwIndex
) == ERROR_SUCCESS
)
5133 if (HTTP_DoAuthorization(request
, szAuthValue
,
5135 request
->session
->userName
,
5136 request
->session
->password
, host
))
5138 heap_free(requestString
);
5139 if(!drain_content(request
, TRUE
)) {
5140 FIXME("Could not drain content\n");
5141 http_release_netconn(request
, FALSE
);
5150 TRACE("Cleaning wrong authorization data\n");
5151 destroy_authinfo(request
->authInfo
);
5152 request
->authInfo
= NULL
;
5155 if (request
->status_code
== HTTP_STATUS_PROXY_AUTH_REQ
)
5158 while (HTTP_HttpQueryInfoW(request
,HTTP_QUERY_PROXY_AUTHENTICATE
,szAuthValue
,&dwBufferSize
,&dwIndex
) == ERROR_SUCCESS
)
5160 if (HTTP_DoAuthorization(request
, szAuthValue
,
5161 &request
->proxyAuthInfo
,
5162 request
->session
->appInfo
->proxyUsername
,
5163 request
->session
->appInfo
->proxyPassword
,
5166 heap_free(requestString
);
5167 if(!drain_content(request
, TRUE
)) {
5168 FIXME("Could not drain content\n");
5169 http_release_netconn(request
, FALSE
);
5177 TRACE("Cleaning wrong proxy authorization data\n");
5178 destroy_authinfo(request
->proxyAuthInfo
);
5179 request
->proxyAuthInfo
= NULL
;
5183 if (secure_proxy_connect
&& request
->status_code
== HTTP_STATUS_OK
)
5185 res
= NETCON_secure_connect(request
->netconn
, request
->server
);
5186 if (res
!= ERROR_SUCCESS
)
5188 WARN("failed to upgrade to secure proxy connection\n");
5189 http_release_netconn( request
, FALSE
);
5192 remove_header(request
, szProxy_Authorization
, TRUE
);
5193 destroy_authinfo(request
->proxyAuthInfo
);
5194 request
->proxyAuthInfo
= NULL
;
5196 secure_proxy_connect
= FALSE
;
5201 res
= ERROR_SUCCESS
;
5206 heap_free(requestString
);
5208 /* TODO: send notification for P3P header */
5210 if(res
== ERROR_SUCCESS
)
5211 create_cache_entry(request
);
5213 if (request
->session
->appInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
5215 if (res
== ERROR_SUCCESS
) {
5216 if(bEndRequest
&& request
->contentLength
&& request
->bytesWritten
== request
->bytesToWrite
)
5217 HTTP_ReceiveRequestData(request
, TRUE
, NULL
);
5219 send_request_complete(request
,
5220 request
->session
->hdr
.dwInternalFlags
& INET_OPENURL
? (DWORD_PTR
)request
->hdr
.hInternet
: 1, 0);
5222 send_request_complete(request
, 0, res
);
5238 } send_request_task_t
;
5240 /***********************************************************************
5242 * Helper functions for the HttpSendRequest(Ex) functions
5245 static void AsyncHttpSendRequestProc(task_header_t
*hdr
)
5247 send_request_task_t
*task
= (send_request_task_t
*)hdr
;
5248 http_request_t
*request
= (http_request_t
*)task
->hdr
.hdr
;
5250 TRACE("%p\n", request
);
5252 HTTP_HttpSendRequestW(request
, task
->headers
, task
->headers_len
, task
->optional
,
5253 task
->optional_len
, task
->content_len
, task
->end_request
);
5255 heap_free(task
->headers
);
5259 static DWORD
HTTP_HttpEndRequestW(http_request_t
*request
, DWORD dwFlags
, DWORD_PTR dwContext
)
5263 DWORD res
= ERROR_SUCCESS
;
5265 if(!is_valid_netconn(request
->netconn
)) {
5266 WARN("Not connected\n");
5267 send_request_complete(request
, 0, ERROR_INTERNET_OPERATION_CANCELLED
);
5268 return ERROR_INTERNET_OPERATION_CANCELLED
;
5271 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
5272 INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
5274 if (HTTP_GetResponseHeaders(request
, &responseLen
) || !responseLen
)
5275 res
= ERROR_HTTP_HEADER_NOT_FOUND
;
5277 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
5278 INTERNET_STATUS_RESPONSE_RECEIVED
, &responseLen
, sizeof(DWORD
));
5280 /* process cookies here. Is this right? */
5281 http_process_keep_alive(request
);
5282 HTTP_ProcessCookies(request
);
5283 HTTP_ProcessExpires(request
);
5284 HTTP_ProcessLastModified(request
);
5286 if ((res
= set_content_length(request
)) == ERROR_SUCCESS
) {
5287 if(!request
->contentLength
)
5288 http_release_netconn(request
, TRUE
);
5291 if (res
== ERROR_SUCCESS
&& !(request
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTO_REDIRECT
))
5293 switch(request
->status_code
) {
5294 case HTTP_STATUS_REDIRECT
:
5295 case HTTP_STATUS_MOVED
:
5296 case HTTP_STATUS_REDIRECT_METHOD
:
5297 case HTTP_STATUS_REDIRECT_KEEP_VERB
: {
5298 WCHAR
*new_url
, szNewLocation
[INTERNET_MAX_URL_LENGTH
];
5299 dwBufferSize
=sizeof(szNewLocation
);
5300 if (HTTP_HttpQueryInfoW(request
, HTTP_QUERY_LOCATION
, szNewLocation
, &dwBufferSize
, NULL
) != ERROR_SUCCESS
)
5303 if (strcmpW(request
->verb
, szGET
) && strcmpW(request
->verb
, szHEAD
) &&
5304 request
->status_code
!= HTTP_STATUS_REDIRECT_KEEP_VERB
)
5306 heap_free(request
->verb
);
5307 request
->verb
= heap_strdupW(szGET
);
5309 http_release_netconn(request
, drain_content(request
, FALSE
));
5310 if ((new_url
= HTTP_GetRedirectURL( request
, szNewLocation
)))
5312 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
, INTERNET_STATUS_REDIRECT
,
5313 new_url
, (strlenW(new_url
) + 1) * sizeof(WCHAR
));
5314 res
= HTTP_HandleRedirect(request
, new_url
);
5315 if (res
== ERROR_SUCCESS
)
5316 res
= HTTP_HttpSendRequestW(request
, NULL
, 0, NULL
, 0, 0, TRUE
);
5317 heap_free( new_url
);
5323 if(res
== ERROR_SUCCESS
)
5324 create_cache_entry(request
);
5326 if (res
== ERROR_SUCCESS
&& request
->contentLength
)
5327 HTTP_ReceiveRequestData(request
, TRUE
, NULL
);
5329 send_request_complete(request
, res
== ERROR_SUCCESS
, res
);
5334 /***********************************************************************
5335 * HttpEndRequestA (WININET.@)
5337 * Ends an HTTP request that was started by HttpSendRequestEx
5340 * TRUE if successful
5344 BOOL WINAPI
HttpEndRequestA(HINTERNET hRequest
,
5345 LPINTERNET_BUFFERSA lpBuffersOut
, DWORD dwFlags
, DWORD_PTR dwContext
)
5347 TRACE("(%p, %p, %08x, %08lx)\n", hRequest
, lpBuffersOut
, dwFlags
, dwContext
);
5351 SetLastError(ERROR_INVALID_PARAMETER
);
5355 return HttpEndRequestW(hRequest
, NULL
, dwFlags
, dwContext
);
5362 } end_request_task_t
;
5364 static void AsyncHttpEndRequestProc(task_header_t
*hdr
)
5366 end_request_task_t
*task
= (end_request_task_t
*)hdr
;
5367 http_request_t
*req
= (http_request_t
*)task
->hdr
.hdr
;
5371 HTTP_HttpEndRequestW(req
, task
->flags
, task
->context
);
5374 /***********************************************************************
5375 * HttpEndRequestW (WININET.@)
5377 * Ends an HTTP request that was started by HttpSendRequestEx
5380 * TRUE if successful
5384 BOOL WINAPI
HttpEndRequestW(HINTERNET hRequest
,
5385 LPINTERNET_BUFFERSW lpBuffersOut
, DWORD dwFlags
, DWORD_PTR dwContext
)
5387 http_request_t
*request
;
5390 TRACE("%p %p %x %lx -->\n", hRequest
, lpBuffersOut
, dwFlags
, dwContext
);
5394 SetLastError(ERROR_INVALID_PARAMETER
);
5398 request
= (http_request_t
*) get_handle_object( hRequest
);
5400 if (NULL
== request
|| request
->hdr
.htype
!= WH_HHTTPREQ
)
5402 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
5404 WININET_Release( &request
->hdr
);
5407 request
->hdr
.dwFlags
|= dwFlags
;
5409 if (request
->session
->appInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
5411 end_request_task_t
*task
;
5413 task
= alloc_async_task(&request
->hdr
, AsyncHttpEndRequestProc
, sizeof(*task
));
5414 task
->flags
= dwFlags
;
5415 task
->context
= dwContext
;
5417 INTERNET_AsyncCall(&task
->hdr
);
5418 res
= ERROR_IO_PENDING
;
5421 res
= HTTP_HttpEndRequestW(request
, dwFlags
, dwContext
);
5423 WININET_Release( &request
->hdr
);
5424 TRACE("%u <--\n", res
);
5425 if(res
!= ERROR_SUCCESS
)
5427 return res
== ERROR_SUCCESS
;
5430 /***********************************************************************
5431 * HttpSendRequestExA (WININET.@)
5433 * Sends the specified request to the HTTP server and allows chunked
5438 * Failure: FALSE, call GetLastError() for more information.
5440 BOOL WINAPI
HttpSendRequestExA(HINTERNET hRequest
,
5441 LPINTERNET_BUFFERSA lpBuffersIn
,
5442 LPINTERNET_BUFFERSA lpBuffersOut
,
5443 DWORD dwFlags
, DWORD_PTR dwContext
)
5445 INTERNET_BUFFERSW BuffersInW
;
5448 LPWSTR header
= NULL
;
5450 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest
, lpBuffersIn
,
5451 lpBuffersOut
, dwFlags
, dwContext
);
5455 BuffersInW
.dwStructSize
= sizeof(LPINTERNET_BUFFERSW
);
5456 if (lpBuffersIn
->lpcszHeader
)
5458 headerlen
= MultiByteToWideChar(CP_ACP
,0,lpBuffersIn
->lpcszHeader
,
5459 lpBuffersIn
->dwHeadersLength
,0,0);
5460 header
= heap_alloc(headerlen
*sizeof(WCHAR
));
5461 if (!(BuffersInW
.lpcszHeader
= header
))
5463 SetLastError(ERROR_OUTOFMEMORY
);
5466 BuffersInW
.dwHeadersLength
= MultiByteToWideChar(CP_ACP
, 0,
5467 lpBuffersIn
->lpcszHeader
, lpBuffersIn
->dwHeadersLength
,
5471 BuffersInW
.lpcszHeader
= NULL
;
5472 BuffersInW
.dwHeadersTotal
= lpBuffersIn
->dwHeadersTotal
;
5473 BuffersInW
.lpvBuffer
= lpBuffersIn
->lpvBuffer
;
5474 BuffersInW
.dwBufferLength
= lpBuffersIn
->dwBufferLength
;
5475 BuffersInW
.dwBufferTotal
= lpBuffersIn
->dwBufferTotal
;
5476 BuffersInW
.Next
= NULL
;
5479 rc
= HttpSendRequestExW(hRequest
, lpBuffersIn
? &BuffersInW
: NULL
, NULL
, dwFlags
, dwContext
);
5485 /***********************************************************************
5486 * HttpSendRequestExW (WININET.@)
5488 * Sends the specified request to the HTTP server and allows chunked
5493 * Failure: FALSE, call GetLastError() for more information.
5495 BOOL WINAPI
HttpSendRequestExW(HINTERNET hRequest
,
5496 LPINTERNET_BUFFERSW lpBuffersIn
,
5497 LPINTERNET_BUFFERSW lpBuffersOut
,
5498 DWORD dwFlags
, DWORD_PTR dwContext
)
5500 http_request_t
*request
;
5501 http_session_t
*session
;
5505 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest
, lpBuffersIn
,
5506 lpBuffersOut
, dwFlags
, dwContext
);
5508 request
= (http_request_t
*) get_handle_object( hRequest
);
5510 if (NULL
== request
|| request
->hdr
.htype
!= WH_HHTTPREQ
)
5512 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
5516 session
= request
->session
;
5517 assert(session
->hdr
.htype
== WH_HHTTPSESSION
);
5518 hIC
= session
->appInfo
;
5519 assert(hIC
->hdr
.htype
== WH_HINIT
);
5521 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
5523 send_request_task_t
*task
;
5525 task
= alloc_async_task(&request
->hdr
, AsyncHttpSendRequestProc
, sizeof(*task
));
5530 if (lpBuffersIn
->lpcszHeader
)
5532 if (lpBuffersIn
->dwHeadersLength
== ~0u)
5533 size
= (strlenW( lpBuffersIn
->lpcszHeader
) + 1) * sizeof(WCHAR
);
5535 size
= lpBuffersIn
->dwHeadersLength
* sizeof(WCHAR
);
5537 task
->headers
= heap_alloc(size
);
5538 memcpy(task
->headers
, lpBuffersIn
->lpcszHeader
, size
);
5540 else task
->headers
= NULL
;
5542 task
->headers_len
= size
/ sizeof(WCHAR
);
5543 task
->optional
= lpBuffersIn
->lpvBuffer
;
5544 task
->optional_len
= lpBuffersIn
->dwBufferLength
;
5545 task
->content_len
= lpBuffersIn
->dwBufferTotal
;
5549 task
->headers
= NULL
;
5550 task
->headers_len
= 0;
5551 task
->optional
= NULL
;
5552 task
->optional_len
= 0;
5553 task
->content_len
= 0;
5556 task
->end_request
= FALSE
;
5558 INTERNET_AsyncCall(&task
->hdr
);
5559 res
= ERROR_IO_PENDING
;
5564 res
= HTTP_HttpSendRequestW(request
, lpBuffersIn
->lpcszHeader
, lpBuffersIn
->dwHeadersLength
,
5565 lpBuffersIn
->lpvBuffer
, lpBuffersIn
->dwBufferLength
,
5566 lpBuffersIn
->dwBufferTotal
, FALSE
);
5568 res
= HTTP_HttpSendRequestW(request
, NULL
, 0, NULL
, 0, 0, FALSE
);
5573 WININET_Release( &request
->hdr
);
5577 return res
== ERROR_SUCCESS
;
5580 /***********************************************************************
5581 * HttpSendRequestW (WININET.@)
5583 * Sends the specified request to the HTTP server
5590 BOOL WINAPI
HttpSendRequestW(HINTERNET hHttpRequest
, LPCWSTR lpszHeaders
,
5591 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
5593 http_request_t
*request
;
5594 http_session_t
*session
= NULL
;
5595 appinfo_t
*hIC
= NULL
;
5596 DWORD res
= ERROR_SUCCESS
;
5598 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest
,
5599 debugstr_wn(lpszHeaders
, dwHeaderLength
), dwHeaderLength
, lpOptional
, dwOptionalLength
);
5601 request
= (http_request_t
*) get_handle_object( hHttpRequest
);
5602 if (NULL
== request
|| request
->hdr
.htype
!= WH_HHTTPREQ
)
5604 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
5608 session
= request
->session
;
5609 if (NULL
== session
|| session
->hdr
.htype
!= WH_HHTTPSESSION
)
5611 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
5615 hIC
= session
->appInfo
;
5616 if (NULL
== hIC
|| hIC
->hdr
.htype
!= WH_HINIT
)
5618 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
5622 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
5624 send_request_task_t
*task
;
5626 task
= alloc_async_task(&request
->hdr
, AsyncHttpSendRequestProc
, sizeof(*task
));
5631 if (dwHeaderLength
== ~0u) size
= (strlenW(lpszHeaders
) + 1) * sizeof(WCHAR
);
5632 else size
= dwHeaderLength
* sizeof(WCHAR
);
5634 task
->headers
= heap_alloc(size
);
5635 memcpy(task
->headers
, lpszHeaders
, size
);
5638 task
->headers
= NULL
;
5639 task
->headers_len
= dwHeaderLength
;
5640 task
->optional
= lpOptional
;
5641 task
->optional_len
= dwOptionalLength
;
5642 task
->content_len
= dwOptionalLength
;
5643 task
->end_request
= TRUE
;
5645 INTERNET_AsyncCall(&task
->hdr
);
5646 res
= ERROR_IO_PENDING
;
5650 res
= HTTP_HttpSendRequestW(request
, lpszHeaders
,
5651 dwHeaderLength
, lpOptional
, dwOptionalLength
,
5652 dwOptionalLength
, TRUE
);
5656 WININET_Release( &request
->hdr
);
5659 return res
== ERROR_SUCCESS
;
5662 /***********************************************************************
5663 * HttpSendRequestA (WININET.@)
5665 * Sends the specified request to the HTTP server
5672 BOOL WINAPI
HttpSendRequestA(HINTERNET hHttpRequest
, LPCSTR lpszHeaders
,
5673 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
5676 LPWSTR szHeaders
=NULL
;
5677 DWORD nLen
=dwHeaderLength
;
5678 if(lpszHeaders
!=NULL
)
5680 nLen
=MultiByteToWideChar(CP_ACP
,0,lpszHeaders
,dwHeaderLength
,NULL
,0);
5681 szHeaders
= heap_alloc(nLen
*sizeof(WCHAR
));
5682 MultiByteToWideChar(CP_ACP
,0,lpszHeaders
,dwHeaderLength
,szHeaders
,nLen
);
5684 result
= HttpSendRequestW(hHttpRequest
, szHeaders
, nLen
, lpOptional
, dwOptionalLength
);
5685 heap_free(szHeaders
);
5689 /***********************************************************************
5690 * HTTPSESSION_Destroy (internal)
5692 * Deallocate session handle
5695 static void HTTPSESSION_Destroy(object_header_t
*hdr
)
5697 http_session_t
*session
= (http_session_t
*) hdr
;
5699 TRACE("%p\n", session
);
5701 WININET_Release(&session
->appInfo
->hdr
);
5703 heap_free(session
->hostName
);
5704 heap_free(session
->password
);
5705 heap_free(session
->userName
);
5708 static DWORD
HTTPSESSION_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
5710 http_session_t
*ses
= (http_session_t
*)hdr
;
5713 case INTERNET_OPTION_HANDLE_TYPE
:
5714 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
5716 if (*size
< sizeof(ULONG
))
5717 return ERROR_INSUFFICIENT_BUFFER
;
5719 *size
= sizeof(DWORD
);
5720 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_CONNECT_HTTP
;
5721 return ERROR_SUCCESS
;
5722 case INTERNET_OPTION_CONNECT_TIMEOUT
:
5723 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
5725 if (*size
< sizeof(DWORD
))
5726 return ERROR_INSUFFICIENT_BUFFER
;
5728 *size
= sizeof(DWORD
);
5729 *(DWORD
*)buffer
= ses
->connect_timeout
;
5730 return ERROR_SUCCESS
;
5732 case INTERNET_OPTION_SEND_TIMEOUT
:
5733 TRACE("INTERNET_OPTION_SEND_TIMEOUT\n");
5735 if (*size
< sizeof(DWORD
))
5736 return ERROR_INSUFFICIENT_BUFFER
;
5738 *size
= sizeof(DWORD
);
5739 *(DWORD
*)buffer
= ses
->send_timeout
;
5740 return ERROR_SUCCESS
;
5742 case INTERNET_OPTION_RECEIVE_TIMEOUT
:
5743 TRACE("INTERNET_OPTION_RECEIVE_TIMEOUT\n");
5745 if (*size
< sizeof(DWORD
))
5746 return ERROR_INSUFFICIENT_BUFFER
;
5748 *size
= sizeof(DWORD
);
5749 *(DWORD
*)buffer
= ses
->receive_timeout
;
5750 return ERROR_SUCCESS
;
5753 return INET_QueryOption(hdr
, option
, buffer
, size
, unicode
);
5756 static DWORD
HTTPSESSION_SetOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD size
)
5758 http_session_t
*ses
= (http_session_t
*)hdr
;
5761 case INTERNET_OPTION_USERNAME
:
5763 heap_free(ses
->userName
);
5764 if (!(ses
->userName
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
5765 return ERROR_SUCCESS
;
5767 case INTERNET_OPTION_PASSWORD
:
5769 heap_free(ses
->password
);
5770 if (!(ses
->password
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
5771 return ERROR_SUCCESS
;
5773 case INTERNET_OPTION_PROXY_USERNAME
:
5775 heap_free(ses
->appInfo
->proxyUsername
);
5776 if (!(ses
->appInfo
->proxyUsername
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
5777 return ERROR_SUCCESS
;
5779 case INTERNET_OPTION_PROXY_PASSWORD
:
5781 heap_free(ses
->appInfo
->proxyPassword
);
5782 if (!(ses
->appInfo
->proxyPassword
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
5783 return ERROR_SUCCESS
;
5785 case INTERNET_OPTION_CONNECT_TIMEOUT
:
5787 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
5788 ses
->connect_timeout
= *(DWORD
*)buffer
;
5789 return ERROR_SUCCESS
;
5791 case INTERNET_OPTION_SEND_TIMEOUT
:
5793 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
5794 ses
->send_timeout
= *(DWORD
*)buffer
;
5795 return ERROR_SUCCESS
;
5797 case INTERNET_OPTION_RECEIVE_TIMEOUT
:
5799 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
5800 ses
->receive_timeout
= *(DWORD
*)buffer
;
5801 return ERROR_SUCCESS
;
5806 return INET_SetOption(hdr
, option
, buffer
, size
);
5809 static const object_vtbl_t HTTPSESSIONVtbl
= {
5810 HTTPSESSION_Destroy
,
5812 HTTPSESSION_QueryOption
,
5813 HTTPSESSION_SetOption
,
5822 /***********************************************************************
5823 * HTTP_Connect (internal)
5825 * Create http session handle
5828 * HINTERNET a session handle on success
5832 DWORD
HTTP_Connect(appinfo_t
*hIC
, LPCWSTR lpszServerName
,
5833 INTERNET_PORT serverPort
, LPCWSTR lpszUserName
,
5834 LPCWSTR lpszPassword
, DWORD dwFlags
, DWORD_PTR dwContext
,
5835 DWORD dwInternalFlags
, HINTERNET
*ret
)
5837 http_session_t
*session
= NULL
;
5841 if (!lpszServerName
|| !lpszServerName
[0])
5842 return ERROR_INVALID_PARAMETER
;
5844 assert( hIC
->hdr
.htype
== WH_HINIT
);
5846 session
= alloc_object(&hIC
->hdr
, &HTTPSESSIONVtbl
, sizeof(http_session_t
));
5848 return ERROR_OUTOFMEMORY
;
5851 * According to my tests. The name is not resolved until a request is sent
5854 session
->hdr
.htype
= WH_HHTTPSESSION
;
5855 session
->hdr
.dwFlags
= dwFlags
;
5856 session
->hdr
.dwContext
= dwContext
;
5857 session
->hdr
.dwInternalFlags
|= dwInternalFlags
;
5859 WININET_AddRef( &hIC
->hdr
);
5860 session
->appInfo
= hIC
;
5861 list_add_head( &hIC
->hdr
.children
, &session
->hdr
.entry
);
5863 session
->hostName
= heap_strdupW(lpszServerName
);
5864 if (lpszUserName
&& lpszUserName
[0])
5865 session
->userName
= heap_strdupW(lpszUserName
);
5866 if (lpszPassword
&& lpszPassword
[0])
5867 session
->password
= heap_strdupW(lpszPassword
);
5868 session
->hostPort
= serverPort
;
5869 session
->connect_timeout
= hIC
->connect_timeout
;
5870 session
->send_timeout
= 0;
5871 session
->receive_timeout
= 0;
5873 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
5874 if (!(session
->hdr
.dwInternalFlags
& INET_OPENURL
))
5876 INTERNET_SendCallback(&hIC
->hdr
, dwContext
,
5877 INTERNET_STATUS_HANDLE_CREATED
, &session
->hdr
.hInternet
,
5882 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
5886 TRACE("%p --> %p\n", hIC
, session
);
5888 *ret
= session
->hdr
.hInternet
;
5889 return ERROR_SUCCESS
;
5892 /***********************************************************************
5893 * HTTP_clear_response_headers (internal)
5895 * clear out any old response headers
5897 static void HTTP_clear_response_headers( http_request_t
*request
)
5901 EnterCriticalSection( &request
->headers_section
);
5903 for( i
=0; i
<request
->nCustHeaders
; i
++)
5905 if( !request
->custHeaders
[i
].lpszField
)
5907 if( !request
->custHeaders
[i
].lpszValue
)
5909 if ( request
->custHeaders
[i
].wFlags
& HDR_ISREQUEST
)
5911 HTTP_DeleteCustomHeader( request
, i
);
5915 LeaveCriticalSection( &request
->headers_section
);
5918 /***********************************************************************
5919 * HTTP_GetResponseHeaders (internal)
5921 * Read server response
5928 static DWORD
HTTP_GetResponseHeaders(http_request_t
*request
, INT
*len
)
5931 WCHAR buffer
[MAX_REPLY_LEN
];
5932 DWORD buflen
= MAX_REPLY_LEN
;
5934 char bufferA
[MAX_REPLY_LEN
];
5935 LPWSTR status_code
= NULL
, status_text
= NULL
;
5936 DWORD res
= ERROR_HTTP_INVALID_SERVER_RESPONSE
;
5937 BOOL codeHundred
= FALSE
;
5941 if(!is_valid_netconn(request
->netconn
))
5944 /* clear old response headers (eg. from a redirect response) */
5945 HTTP_clear_response_headers( request
);
5947 NETCON_set_timeout( request
->netconn
, FALSE
, request
->receive_timeout
);
5950 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
5952 buflen
= MAX_REPLY_LEN
;
5953 if ((res
= read_line(request
, bufferA
, &buflen
)))
5956 if (!buflen
) goto lend
;
5959 MultiByteToWideChar( CP_ACP
, 0, bufferA
, buflen
, buffer
, MAX_REPLY_LEN
);
5960 /* check is this a status code line? */
5961 if (!strncmpW(buffer
, g_szHttp1_0
, 4))
5963 /* split the version from the status code */
5964 status_code
= strchrW( buffer
, ' ' );
5969 /* split the status code from the status text */
5970 status_text
= strchrW( status_code
, ' ' );
5974 request
->status_code
= atoiW(status_code
);
5976 TRACE("version [%s] status code [%s] status text [%s]\n",
5977 debugstr_w(buffer
), debugstr_w(status_code
), debugstr_w(status_text
) );
5979 codeHundred
= request
->status_code
== HTTP_STATUS_CONTINUE
;
5981 else if (!codeHundred
)
5983 WARN("No status line at head of response (%s)\n", debugstr_w(buffer
));
5985 heap_free(request
->version
);
5986 heap_free(request
->statusText
);
5988 request
->status_code
= HTTP_STATUS_OK
;
5989 request
->version
= heap_strdupW(g_szHttp1_0
);
5990 request
->statusText
= heap_strdupW(szOK
);
5994 } while (codeHundred
);
5996 /* Add status code */
5997 HTTP_ProcessHeader(request
, szStatus
, status_code
,
5998 HTTP_ADDHDR_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_ADD
);
6000 heap_free(request
->version
);
6001 heap_free(request
->statusText
);
6003 request
->version
= heap_strdupW(buffer
);
6004 request
->statusText
= heap_strdupW(status_text
? status_text
: emptyW
);
6006 /* Restore the spaces */
6007 *(status_code
-1) = ' ';
6009 *(status_text
-1) = ' ';
6011 /* Parse each response line */
6014 buflen
= MAX_REPLY_LEN
;
6015 if (!read_line(request
, bufferA
, &buflen
) && buflen
)
6017 LPWSTR
* pFieldAndValue
;
6019 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA
));
6021 if (!bufferA
[0]) break;
6022 MultiByteToWideChar( CP_ACP
, 0, bufferA
, buflen
, buffer
, MAX_REPLY_LEN
);
6024 pFieldAndValue
= HTTP_InterpretHttpHeader(buffer
);
6027 HTTP_ProcessHeader(request
, pFieldAndValue
[0], pFieldAndValue
[1],
6028 HTTP_ADDREQ_FLAG_ADD
);
6029 HTTP_FreeTokens(pFieldAndValue
);
6040 res
= ERROR_SUCCESS
;
6049 /***********************************************************************
6050 * HTTP_InterpretHttpHeader (internal)
6052 * Parse server response
6056 * Pointer to array of field, value, NULL on success.
6059 static LPWSTR
* HTTP_InterpretHttpHeader(LPCWSTR buffer
)
6061 LPWSTR
* pTokenPair
;
6065 pTokenPair
= heap_alloc_zero(sizeof(*pTokenPair
)*3);
6067 pszColon
= strchrW(buffer
, ':');
6068 /* must have two tokens */
6071 HTTP_FreeTokens(pTokenPair
);
6073 TRACE("No ':' in line: %s\n", debugstr_w(buffer
));
6077 pTokenPair
[0] = heap_alloc((pszColon
- buffer
+ 1) * sizeof(WCHAR
));
6080 HTTP_FreeTokens(pTokenPair
);
6083 memcpy(pTokenPair
[0], buffer
, (pszColon
- buffer
) * sizeof(WCHAR
));
6084 pTokenPair
[0][pszColon
- buffer
] = '\0';
6088 len
= strlenW(pszColon
);
6089 pTokenPair
[1] = heap_alloc((len
+ 1) * sizeof(WCHAR
));
6092 HTTP_FreeTokens(pTokenPair
);
6095 memcpy(pTokenPair
[1], pszColon
, (len
+ 1) * sizeof(WCHAR
));
6097 strip_spaces(pTokenPair
[0]);
6098 strip_spaces(pTokenPair
[1]);
6100 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair
[0]), debugstr_w(pTokenPair
[1]));
6104 /***********************************************************************
6105 * HTTP_ProcessHeader (internal)
6107 * Stuff header into header tables according to <dwModifier>
6111 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
6113 static DWORD
HTTP_ProcessHeader(http_request_t
*request
, LPCWSTR field
, LPCWSTR value
, DWORD dwModifier
)
6115 LPHTTPHEADERW lphttpHdr
= NULL
;
6117 BOOL request_only
= !!(dwModifier
& HTTP_ADDHDR_FLAG_REQ
);
6118 DWORD res
= ERROR_HTTP_INVALID_HEADER
;
6120 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field
), debugstr_w(value
), dwModifier
);
6122 EnterCriticalSection( &request
->headers_section
);
6124 /* REPLACE wins out over ADD */
6125 if (dwModifier
& HTTP_ADDHDR_FLAG_REPLACE
)
6126 dwModifier
&= ~HTTP_ADDHDR_FLAG_ADD
;
6128 if (dwModifier
& HTTP_ADDHDR_FLAG_ADD
)
6131 index
= HTTP_GetCustomHeaderIndex(request
, field
, 0, request_only
);
6135 if (dwModifier
& HTTP_ADDHDR_FLAG_ADD_IF_NEW
)
6137 LeaveCriticalSection( &request
->headers_section
);
6138 return ERROR_HTTP_INVALID_HEADER
;
6140 lphttpHdr
= &request
->custHeaders
[index
];
6146 hdr
.lpszField
= (LPWSTR
)field
;
6147 hdr
.lpszValue
= (LPWSTR
)value
;
6148 hdr
.wFlags
= hdr
.wCount
= 0;
6150 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
6151 hdr
.wFlags
|= HDR_ISREQUEST
;
6153 res
= HTTP_InsertCustomHeader(request
, &hdr
);
6154 LeaveCriticalSection( &request
->headers_section
);
6157 /* no value to delete */
6160 LeaveCriticalSection( &request
->headers_section
);
6161 return ERROR_SUCCESS
;
6164 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
6165 lphttpHdr
->wFlags
|= HDR_ISREQUEST
;
6167 lphttpHdr
->wFlags
&= ~HDR_ISREQUEST
;
6169 if (dwModifier
& HTTP_ADDHDR_FLAG_REPLACE
)
6171 HTTP_DeleteCustomHeader( request
, index
);
6173 if (value
&& value
[0])
6177 hdr
.lpszField
= (LPWSTR
)field
;
6178 hdr
.lpszValue
= (LPWSTR
)value
;
6179 hdr
.wFlags
= hdr
.wCount
= 0;
6181 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
6182 hdr
.wFlags
|= HDR_ISREQUEST
;
6184 res
= HTTP_InsertCustomHeader(request
, &hdr
);
6185 LeaveCriticalSection( &request
->headers_section
);
6189 LeaveCriticalSection( &request
->headers_section
);
6190 return ERROR_SUCCESS
;
6192 else if (dwModifier
& COALESCEFLAGS
)
6197 INT origlen
= strlenW(lphttpHdr
->lpszValue
);
6198 INT valuelen
= strlenW(value
);
6200 if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
)
6203 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
6205 else if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON
)
6208 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
6211 len
= origlen
+ valuelen
+ ((ch
> 0) ? 2 : 0);
6213 lpsztmp
= heap_realloc(lphttpHdr
->lpszValue
, (len
+1)*sizeof(WCHAR
));
6216 lphttpHdr
->lpszValue
= lpsztmp
;
6217 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
6220 lphttpHdr
->lpszValue
[origlen
] = ch
;
6222 lphttpHdr
->lpszValue
[origlen
] = ' ';
6226 memcpy(&lphttpHdr
->lpszValue
[origlen
], value
, valuelen
*sizeof(WCHAR
));
6227 lphttpHdr
->lpszValue
[len
] = '\0';
6228 res
= ERROR_SUCCESS
;
6232 WARN("heap_realloc (%d bytes) failed\n",len
+1);
6233 res
= ERROR_OUTOFMEMORY
;
6236 TRACE("<-- %d\n", res
);
6237 LeaveCriticalSection( &request
->headers_section
);
6241 /***********************************************************************
6242 * HTTP_GetCustomHeaderIndex (internal)
6244 * Return index of custom header from header array
6245 * Headers section must be held
6247 static INT
HTTP_GetCustomHeaderIndex(http_request_t
*request
, LPCWSTR lpszField
,
6248 int requested_index
, BOOL request_only
)
6252 TRACE("%s, %d, %d\n", debugstr_w(lpszField
), requested_index
, request_only
);
6254 for (index
= 0; index
< request
->nCustHeaders
; index
++)
6256 if (strcmpiW(request
->custHeaders
[index
].lpszField
, lpszField
))
6259 if (request_only
&& !(request
->custHeaders
[index
].wFlags
& HDR_ISREQUEST
))
6262 if (!request_only
&& (request
->custHeaders
[index
].wFlags
& HDR_ISREQUEST
))
6265 if (requested_index
== 0)
6270 if (index
>= request
->nCustHeaders
)
6273 TRACE("Return: %d\n", index
);
6278 /***********************************************************************
6279 * HTTP_InsertCustomHeader (internal)
6281 * Insert header into array
6282 * Headers section must be held
6284 static DWORD
HTTP_InsertCustomHeader(http_request_t
*request
, LPHTTPHEADERW lpHdr
)
6287 LPHTTPHEADERW lph
= NULL
;
6289 TRACE("--> %s: %s\n", debugstr_w(lpHdr
->lpszField
), debugstr_w(lpHdr
->lpszValue
));
6290 count
= request
->nCustHeaders
+ 1;
6292 lph
= heap_realloc_zero(request
->custHeaders
, sizeof(HTTPHEADERW
) * count
);
6294 lph
= heap_alloc_zero(sizeof(HTTPHEADERW
) * count
);
6297 return ERROR_OUTOFMEMORY
;
6299 request
->custHeaders
= lph
;
6300 request
->custHeaders
[count
-1].lpszField
= heap_strdupW(lpHdr
->lpszField
);
6301 request
->custHeaders
[count
-1].lpszValue
= heap_strdupW(lpHdr
->lpszValue
);
6302 request
->custHeaders
[count
-1].wFlags
= lpHdr
->wFlags
;
6303 request
->custHeaders
[count
-1].wCount
= lpHdr
->wCount
;
6304 request
->nCustHeaders
++;
6306 return ERROR_SUCCESS
;
6310 /***********************************************************************
6311 * HTTP_DeleteCustomHeader (internal)
6313 * Delete header from array
6314 * If this function is called, the index may change.
6315 * Headers section must be held
6317 static BOOL
HTTP_DeleteCustomHeader(http_request_t
*request
, DWORD index
)
6319 if( request
->nCustHeaders
<= 0 )
6321 if( index
>= request
->nCustHeaders
)
6323 request
->nCustHeaders
--;
6325 heap_free(request
->custHeaders
[index
].lpszField
);
6326 heap_free(request
->custHeaders
[index
].lpszValue
);
6328 memmove( &request
->custHeaders
[index
], &request
->custHeaders
[index
+1],
6329 (request
->nCustHeaders
- index
)* sizeof(HTTPHEADERW
) );
6330 memset( &request
->custHeaders
[request
->nCustHeaders
], 0, sizeof(HTTPHEADERW
) );
6336 /***********************************************************************
6337 * IsHostInProxyBypassList (@)
6342 BOOL WINAPI
IsHostInProxyBypassList(DWORD flags
, LPCSTR szHost
, DWORD length
)
6344 FIXME("STUB: flags=%d host=%s length=%d\n",flags
,szHost
,length
);