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
38 #include <wine/exception.h>
41 #include "inet_ntop.c"
43 static const WCHAR g_szHttp1_0
[] = {'H','T','T','P','/','1','.','0',0};
44 static const WCHAR g_szHttp1_1
[] = {'H','T','T','P','/','1','.','1',0};
45 static const WCHAR szOK
[] = {'O','K',0};
46 static const WCHAR szDefaultHeader
[] = {'H','T','T','P','/','1','.','0',' ','2','0','0',' ','O','K',0};
47 static const WCHAR hostW
[] = { 'H','o','s','t',0 };
48 static const WCHAR szAuthorization
[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
49 static const WCHAR szProxy_Authorization
[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
50 static const WCHAR szStatus
[] = { 'S','t','a','t','u','s',0 };
51 static const WCHAR szKeepAlive
[] = {'K','e','e','p','-','A','l','i','v','e',0};
52 static const WCHAR szGET
[] = { 'G','E','T', 0 };
53 static const WCHAR szHEAD
[] = { 'H','E','A','D', 0 };
54 static const WCHAR szCrLf
[] = {'\r','\n', 0};
56 static const WCHAR szAccept
[] = { 'A','c','c','e','p','t',0 };
57 static const WCHAR szAccept_Charset
[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
58 static const WCHAR szAccept_Encoding
[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
59 static const WCHAR szAccept_Language
[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
60 static const WCHAR szAccept_Ranges
[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
61 static const WCHAR szAge
[] = { 'A','g','e',0 };
62 static const WCHAR szAllow
[] = { 'A','l','l','o','w',0 };
63 static const WCHAR szCache_Control
[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
64 static const WCHAR szConnection
[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
65 static const WCHAR szContent_Base
[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
66 static const WCHAR szContent_Disposition
[] = { 'C','o','n','t','e','n','t','-','D','i','s','p','o','s','i','t','i','o','n',0 };
67 static const WCHAR szContent_Encoding
[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
68 static const WCHAR szContent_ID
[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
69 static const WCHAR szContent_Language
[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
70 static const WCHAR szContent_Length
[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
71 static const WCHAR szContent_Location
[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
72 static const WCHAR szContent_MD5
[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
73 static const WCHAR szContent_Range
[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
74 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 };
75 static const WCHAR szContent_Type
[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
76 static const WCHAR szCookie
[] = { 'C','o','o','k','i','e',0 };
77 static const WCHAR szDate
[] = { 'D','a','t','e',0 };
78 static const WCHAR szFrom
[] = { 'F','r','o','m',0 };
79 static const WCHAR szETag
[] = { 'E','T','a','g',0 };
80 static const WCHAR szExpect
[] = { 'E','x','p','e','c','t',0 };
81 static const WCHAR szExpires
[] = { 'E','x','p','i','r','e','s',0 };
82 static const WCHAR szIf_Match
[] = { 'I','f','-','M','a','t','c','h',0 };
83 static const WCHAR szIf_Modified_Since
[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
84 static const WCHAR szIf_None_Match
[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
85 static const WCHAR szIf_Range
[] = { 'I','f','-','R','a','n','g','e',0 };
86 static const WCHAR szIf_Unmodified_Since
[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
87 static const WCHAR szLast_Modified
[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
88 static const WCHAR szLocation
[] = { 'L','o','c','a','t','i','o','n',0 };
89 static const WCHAR szMax_Forwards
[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
90 static const WCHAR szMime_Version
[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
91 static const WCHAR szPragma
[] = { 'P','r','a','g','m','a',0 };
92 static const WCHAR szProxy_Authenticate
[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
93 static const WCHAR szProxy_Connection
[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
94 static const WCHAR szPublic
[] = { 'P','u','b','l','i','c',0 };
95 static const WCHAR szRange
[] = { 'R','a','n','g','e',0 };
96 static const WCHAR szReferer
[] = { 'R','e','f','e','r','e','r',0 };
97 static const WCHAR szRetry_After
[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
98 static const WCHAR szServer
[] = { 'S','e','r','v','e','r',0 };
99 static const WCHAR szSet_Cookie
[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
100 static const WCHAR szTransfer_Encoding
[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
101 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 };
102 static const WCHAR szUpgrade
[] = { 'U','p','g','r','a','d','e',0 };
103 static const WCHAR szURI
[] = { 'U','R','I',0 };
104 static const WCHAR szUser_Agent
[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
105 static const WCHAR szVary
[] = { 'V','a','r','y',0 };
106 static const WCHAR szVia
[] = { 'V','i','a',0 };
107 static const WCHAR szWarning
[] = { 'W','a','r','n','i','n','g',0 };
108 static const WCHAR szWWW_Authenticate
[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
110 static const WCHAR emptyW
[] = {0};
112 #define HTTP_REFERER szReferer
113 #define HTTP_ACCEPT szAccept
114 #define HTTP_USERAGENT szUser_Agent
116 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
117 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
118 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
119 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
120 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
121 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
122 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
124 #define COLLECT_TIME 60000
126 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
137 unsigned int auth_data_len
;
138 BOOL finished
; /* finished authenticating */
142 typedef struct _basicAuthorizationData
149 UINT authorizationLen
;
150 } basicAuthorizationData
;
152 typedef struct _authorizationData
166 static struct list basicAuthorizationCache
= LIST_INIT(basicAuthorizationCache
);
167 static struct list authorizationCache
= LIST_INIT(authorizationCache
);
169 static CRITICAL_SECTION authcache_cs
;
170 static CRITICAL_SECTION_DEBUG critsect_debug
=
173 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
174 0, 0, { (DWORD_PTR
)(__FILE__
": authcache_cs") }
176 static CRITICAL_SECTION authcache_cs
= { &critsect_debug
, -1, 0, 0, 0, 0 };
178 static DWORD
HTTP_GetResponseHeaders(http_request_t
*req
, INT
*len
);
179 static DWORD
HTTP_ProcessHeader(http_request_t
*req
, LPCWSTR field
, LPCWSTR value
, DWORD dwModifier
);
180 static LPWSTR
* HTTP_InterpretHttpHeader(LPCWSTR buffer
);
181 static DWORD
HTTP_InsertCustomHeader(http_request_t
*req
, LPHTTPHEADERW lpHdr
);
182 static INT
HTTP_GetCustomHeaderIndex(http_request_t
*req
, LPCWSTR lpszField
, INT index
, BOOL Request
);
183 static BOOL
HTTP_DeleteCustomHeader(http_request_t
*req
, DWORD index
);
184 static LPWSTR
HTTP_build_req( LPCWSTR
*list
, int len
);
185 static DWORD
HTTP_HttpQueryInfoW(http_request_t
*, DWORD
, LPVOID
, LPDWORD
, LPDWORD
);
186 static LPWSTR
HTTP_GetRedirectURL(http_request_t
*req
, LPCWSTR lpszUrl
);
187 static UINT
HTTP_DecodeBase64(LPCWSTR base64
, LPSTR bin
);
188 static BOOL
HTTP_VerifyValidHeader(http_request_t
*req
, LPCWSTR field
);
189 static BOOL
drain_content(http_request_t
*,BOOL
);
191 static CRITICAL_SECTION connection_pool_cs
;
192 static CRITICAL_SECTION_DEBUG connection_pool_debug
=
194 0, 0, &connection_pool_cs
,
195 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
196 0, 0, { (DWORD_PTR
)(__FILE__
": connection_pool_cs") }
198 static CRITICAL_SECTION connection_pool_cs
= { &connection_pool_debug
, -1, 0, 0, 0, 0 };
200 static struct list connection_pool
= LIST_INIT(connection_pool
);
201 static BOOL collector_running
;
203 void server_addref(server_t
*server
)
205 InterlockedIncrement(&server
->ref
);
208 void server_release(server_t
*server
)
210 if(InterlockedDecrement(&server
->ref
))
214 EnterCriticalSection(&connection_pool_cs
);
216 list_remove(&server
->entry
);
218 LeaveCriticalSection(&connection_pool_cs
);
221 if(server
->cert_chain
)
222 CertFreeCertificateChain(server
->cert_chain
);
223 heap_free(server
->name
);
224 heap_free(server
->scheme_host_port
);
228 static BOOL
process_host_port(server_t
*server
)
234 static const WCHAR httpW
[] = {'h','t','t','p',0};
235 static const WCHAR httpsW
[] = {'h','t','t','p','s',0};
236 static const WCHAR formatW
[] = {'%','s',':','/','/','%','s',':','%','u',0};
238 name_len
= strlenW(server
->name
);
239 buf
= heap_alloc((name_len
+ 10 /* strlen("://:<port>") */)*sizeof(WCHAR
) + sizeof(httpsW
));
243 sprintfW(buf
, formatW
, server
->is_https
? httpsW
: httpW
, server
->name
, server
->port
);
244 server
->scheme_host_port
= buf
;
246 server
->host_port
= server
->scheme_host_port
+ 7 /* strlen("http://") */;
250 default_port
= server
->port
== (server
->is_https
? INTERNET_DEFAULT_HTTPS_PORT
: INTERNET_DEFAULT_HTTP_PORT
);
251 server
->canon_host_port
= default_port
? server
->name
: server
->host_port
;
255 server_t
*get_server(const WCHAR
*name
, INTERNET_PORT port
, BOOL is_https
, BOOL do_create
)
257 server_t
*iter
, *server
= NULL
;
259 if(port
== INTERNET_INVALID_PORT_NUMBER
)
260 port
= INTERNET_DEFAULT_HTTP_PORT
;
262 EnterCriticalSection(&connection_pool_cs
);
264 LIST_FOR_EACH_ENTRY(iter
, &connection_pool
, server_t
, entry
) {
265 if(iter
->port
== port
&& !strcmpW(iter
->name
, name
) && iter
->is_https
== is_https
) {
267 server_addref(server
);
272 if(!server
&& do_create
) {
273 server
= heap_alloc_zero(sizeof(*server
));
275 server
->ref
= 2; /* list reference and return */
277 server
->is_https
= is_https
;
278 list_init(&server
->conn_pool
);
279 server
->name
= heap_strdupW(name
);
280 if(server
->name
&& process_host_port(server
)) {
281 list_add_head(&connection_pool
, &server
->entry
);
289 LeaveCriticalSection(&connection_pool_cs
);
294 BOOL
collect_connections(collect_type_t collect_type
)
296 netconn_t
*netconn
, *netconn_safe
;
297 server_t
*server
, *server_safe
;
298 BOOL remaining
= FALSE
;
301 now
= GetTickCount();
303 LIST_FOR_EACH_ENTRY_SAFE(server
, server_safe
, &connection_pool
, server_t
, entry
) {
304 LIST_FOR_EACH_ENTRY_SAFE(netconn
, netconn_safe
, &server
->conn_pool
, netconn_t
, pool_entry
) {
305 if(collect_type
> COLLECT_TIMEOUT
|| netconn
->keep_until
< now
) {
306 TRACE("freeing %p\n", netconn
);
307 list_remove(&netconn
->pool_entry
);
308 free_netconn(netconn
);
314 if(collect_type
== COLLECT_CLEANUP
) {
315 list_remove(&server
->entry
);
316 list_init(&server
->entry
);
317 server_release(server
);
324 static DWORD WINAPI
collect_connections_proc(void *arg
)
326 BOOL remaining_conns
;
329 /* FIXME: Use more sophisticated method */
332 EnterCriticalSection(&connection_pool_cs
);
334 remaining_conns
= collect_connections(COLLECT_TIMEOUT
);
336 collector_running
= FALSE
;
338 LeaveCriticalSection(&connection_pool_cs
);
339 }while(remaining_conns
);
341 FreeLibraryAndExitThread(WININET_hModule
, 0);
344 static LPHTTPHEADERW
HTTP_GetHeader(http_request_t
*req
, LPCWSTR head
)
347 HeaderIndex
= HTTP_GetCustomHeaderIndex(req
, head
, 0, TRUE
);
348 if (HeaderIndex
== -1)
351 return &req
->custHeaders
[HeaderIndex
];
354 struct data_stream_vtbl_t
{
355 DWORD (*get_avail_data
)(data_stream_t
*,http_request_t
*);
356 BOOL (*end_of_data
)(data_stream_t
*,http_request_t
*);
357 DWORD (*read
)(data_stream_t
*,http_request_t
*,BYTE
*,DWORD
,DWORD
*,blocking_mode_t
);
358 BOOL (*drain_content
)(data_stream_t
*,http_request_t
*);
359 void (*destroy
)(data_stream_t
*);
363 data_stream_t data_stream
;
365 BYTE buf
[READ_BUFFER_SIZE
];
372 static inline void destroy_data_stream(data_stream_t
*stream
)
374 stream
->vtbl
->destroy(stream
);
377 static void reset_data_stream(http_request_t
*req
)
379 destroy_data_stream(req
->data_stream
);
380 req
->data_stream
= &req
->netconn_stream
.data_stream
;
381 req
->read_pos
= req
->read_size
= req
->netconn_stream
.content_read
= 0;
382 req
->read_chunked
= req
->read_gzip
= FALSE
;
388 data_stream_t stream
;
389 data_stream_t
*parent_stream
;
391 BYTE buf
[READ_BUFFER_SIZE
];
397 static DWORD
gzip_get_avail_data(data_stream_t
*stream
, http_request_t
*req
)
399 /* Allow reading only from read buffer */
403 static BOOL
gzip_end_of_data(data_stream_t
*stream
, http_request_t
*req
)
405 gzip_stream_t
*gzip_stream
= (gzip_stream_t
*)stream
;
406 return gzip_stream
->end_of_data
407 || (!gzip_stream
->buf_size
&& gzip_stream
->parent_stream
->vtbl
->end_of_data(gzip_stream
->parent_stream
, req
));
410 static DWORD
gzip_read(data_stream_t
*stream
, http_request_t
*req
, BYTE
*buf
, DWORD size
,
411 DWORD
*read
, blocking_mode_t blocking_mode
)
413 gzip_stream_t
*gzip_stream
= (gzip_stream_t
*)stream
;
414 z_stream
*zstream
= &gzip_stream
->zstream
;
415 DWORD current_read
, ret_read
= 0;
417 DWORD res
= ERROR_SUCCESS
;
419 TRACE("(%d %d)\n", size
, blocking_mode
);
421 while(size
&& !gzip_stream
->end_of_data
) {
422 if(!gzip_stream
->buf_size
) {
423 if(gzip_stream
->buf_pos
) {
424 if(gzip_stream
->buf_size
)
425 memmove(gzip_stream
->buf
, gzip_stream
->buf
+gzip_stream
->buf_pos
, gzip_stream
->buf_size
);
426 gzip_stream
->buf_pos
= 0;
428 res
= gzip_stream
->parent_stream
->vtbl
->read(gzip_stream
->parent_stream
, req
, gzip_stream
->buf
+gzip_stream
->buf_size
,
429 sizeof(gzip_stream
->buf
)-gzip_stream
->buf_size
, ¤t_read
, blocking_mode
);
430 gzip_stream
->buf_size
+= current_read
;
431 if(res
!= ERROR_SUCCESS
)
435 if(blocking_mode
!= BLOCKING_DISALLOW
) {
436 WARN("unexpected end of data\n");
437 gzip_stream
->end_of_data
= TRUE
;
443 zstream
->next_in
= gzip_stream
->buf
+gzip_stream
->buf_pos
;
444 zstream
->avail_in
= gzip_stream
->buf_size
;
445 zstream
->next_out
= buf
+ret_read
;
446 zstream
->avail_out
= size
;
447 zres
= inflate(&gzip_stream
->zstream
, 0);
448 current_read
= size
- zstream
->avail_out
;
449 size
-= current_read
;
450 ret_read
+= current_read
;
451 gzip_stream
->buf_size
-= zstream
->next_in
- (gzip_stream
->buf
+gzip_stream
->buf_pos
);
452 gzip_stream
->buf_pos
= zstream
->next_in
-gzip_stream
->buf
;
453 if(zres
== Z_STREAM_END
) {
454 TRACE("end of data\n");
455 gzip_stream
->end_of_data
= TRUE
;
457 }else if(zres
!= Z_OK
) {
458 WARN("inflate failed %d: %s\n", zres
, debugstr_a(zstream
->msg
));
460 res
= ERROR_INTERNET_DECODING_FAILED
;
464 if(ret_read
&& blocking_mode
== BLOCKING_ALLOW
)
465 blocking_mode
= BLOCKING_DISALLOW
;
468 TRACE("read %u bytes\n", ret_read
);
473 static BOOL
gzip_drain_content(data_stream_t
*stream
, http_request_t
*req
)
475 gzip_stream_t
*gzip_stream
= (gzip_stream_t
*)stream
;
476 return gzip_stream
->parent_stream
->vtbl
->drain_content(gzip_stream
->parent_stream
, req
);
479 static void gzip_destroy(data_stream_t
*stream
)
481 gzip_stream_t
*gzip_stream
= (gzip_stream_t
*)stream
;
483 destroy_data_stream(gzip_stream
->parent_stream
);
485 if(!gzip_stream
->end_of_data
)
486 inflateEnd(&gzip_stream
->zstream
);
487 heap_free(gzip_stream
);
490 static const data_stream_vtbl_t gzip_stream_vtbl
= {
498 static voidpf
wininet_zalloc(voidpf opaque
, uInt items
, uInt size
)
500 return heap_alloc(items
*size
);
503 static void wininet_zfree(voidpf opaque
, voidpf address
)
508 static DWORD
init_gzip_stream(http_request_t
*req
, BOOL is_gzip
)
510 gzip_stream_t
*gzip_stream
;
513 gzip_stream
= heap_alloc_zero(sizeof(gzip_stream_t
));
515 return ERROR_OUTOFMEMORY
;
517 gzip_stream
->stream
.vtbl
= &gzip_stream_vtbl
;
518 gzip_stream
->zstream
.zalloc
= wininet_zalloc
;
519 gzip_stream
->zstream
.zfree
= wininet_zfree
;
521 zres
= inflateInit2(&gzip_stream
->zstream
, is_gzip
? 0x1f : -15);
523 ERR("inflateInit failed: %d\n", zres
);
524 heap_free(gzip_stream
);
525 return ERROR_OUTOFMEMORY
;
528 index
= HTTP_GetCustomHeaderIndex(req
, szContent_Length
, 0, FALSE
);
530 HTTP_DeleteCustomHeader(req
, index
);
533 memcpy(gzip_stream
->buf
, req
->read_buf
+req
->read_pos
, req
->read_size
);
534 gzip_stream
->buf_size
= req
->read_size
;
535 req
->read_pos
= req
->read_size
= 0;
538 req
->read_gzip
= TRUE
;
539 gzip_stream
->parent_stream
= req
->data_stream
;
540 req
->data_stream
= &gzip_stream
->stream
;
541 return ERROR_SUCCESS
;
546 static DWORD
init_gzip_stream(http_request_t
*req
, BOOL is_gzip
)
548 ERR("gzip stream not supported, missing zlib.\n");
549 return ERROR_SUCCESS
;
554 /***********************************************************************
555 * HTTP_FreeTokens (internal)
557 * Frees table of pointers.
559 static void HTTP_FreeTokens(LPWSTR
* token_array
)
562 for (i
= 0; token_array
[i
]; i
++) heap_free(token_array
[i
]);
563 heap_free(token_array
);
566 static void HTTP_FixURL(http_request_t
*request
)
568 static const WCHAR szSlash
[] = { '/',0 };
569 static const WCHAR szHttp
[] = { 'h','t','t','p',':','/','/', 0 };
571 /* If we don't have a path we set it to root */
572 if (NULL
== request
->path
)
573 request
->path
= heap_strdupW(szSlash
);
574 else /* remove \r and \n*/
576 int nLen
= strlenW(request
->path
);
577 while ((nLen
>0 ) && ((request
->path
[nLen
-1] == '\r')||(request
->path
[nLen
-1] == '\n')))
580 request
->path
[nLen
]='\0';
582 /* Replace '\' with '/' */
585 if (request
->path
[nLen
] == '\\') request
->path
[nLen
]='/';
589 if(CSTR_EQUAL
!= CompareStringW( LOCALE_INVARIANT
, NORM_IGNORECASE
,
590 request
->path
, strlenW(request
->path
), szHttp
, strlenW(szHttp
) )
591 && request
->path
[0] != '/') /* not an absolute path ?? --> fix it !! */
593 WCHAR
*fixurl
= heap_alloc((strlenW(request
->path
) + 2)*sizeof(WCHAR
));
595 strcpyW(fixurl
+ 1, request
->path
);
596 heap_free( request
->path
);
597 request
->path
= fixurl
;
601 static WCHAR
* build_request_header(http_request_t
*request
, const WCHAR
*verb
,
602 const WCHAR
*path
, const WCHAR
*version
, BOOL use_cr
)
604 LPWSTR requestString
;
609 static const WCHAR szSpace
[] = { ' ',0 };
610 static const WCHAR szColon
[] = { ':',' ',0 };
611 static const WCHAR szCr
[] = { '\r',0 };
612 static const WCHAR szLf
[] = { '\n',0 };
614 /* allocate space for an array of all the string pointers to be added */
615 len
= (request
->nCustHeaders
)*5 + 10;
616 req
= heap_alloc(len
*sizeof(LPCWSTR
));
618 /* add the verb, path and HTTP version string */
629 /* Append custom request headers */
630 for (i
= 0; i
< request
->nCustHeaders
; i
++)
632 if (request
->custHeaders
[i
].wFlags
& HDR_ISREQUEST
)
634 req
[n
++] = request
->custHeaders
[i
].lpszField
;
636 req
[n
++] = request
->custHeaders
[i
].lpszValue
;
641 TRACE("Adding custom header %s (%s)\n",
642 debugstr_w(request
->custHeaders
[i
].lpszField
),
643 debugstr_w(request
->custHeaders
[i
].lpszValue
));
651 requestString
= HTTP_build_req( req
, 4 );
653 return requestString
;
656 static WCHAR
* build_response_header(http_request_t
*request
, BOOL use_cr
)
658 static const WCHAR colonW
[] = { ':',' ',0 };
659 static const WCHAR crW
[] = { '\r',0 };
660 static const WCHAR lfW
[] = { '\n',0 };
661 static const WCHAR status_fmt
[] = { ' ','%','u',' ',0 };
667 req
= heap_alloc((request
->nCustHeaders
*5+8)*sizeof(WCHAR
*));
671 if (request
->status_code
)
673 req
[n
++] = request
->version
;
674 sprintfW(buf
, status_fmt
, request
->status_code
);
676 req
[n
++] = request
->statusText
;
682 for(i
= 0; i
< request
->nCustHeaders
; i
++)
684 if(!(request
->custHeaders
[i
].wFlags
& HDR_ISREQUEST
)
685 && strcmpW(request
->custHeaders
[i
].lpszField
, szStatus
))
687 req
[n
++] = request
->custHeaders
[i
].lpszField
;
689 req
[n
++] = request
->custHeaders
[i
].lpszValue
;
694 TRACE("Adding custom header %s (%s)\n",
695 debugstr_w(request
->custHeaders
[i
].lpszField
),
696 debugstr_w(request
->custHeaders
[i
].lpszValue
));
704 ret
= HTTP_build_req(req
, 0);
709 static void HTTP_ProcessCookies( http_request_t
*request
)
713 LPHTTPHEADERW setCookieHeader
;
715 if(request
->hdr
.dwFlags
& INTERNET_FLAG_NO_COOKIES
)
718 while((HeaderIndex
= HTTP_GetCustomHeaderIndex(request
, szSet_Cookie
, numCookies
++, FALSE
)) != -1)
724 setCookieHeader
= &request
->custHeaders
[HeaderIndex
];
726 if (!setCookieHeader
->lpszValue
)
729 host
= HTTP_GetHeader(request
, hostW
);
733 data
= strchrW(setCookieHeader
->lpszValue
, '=');
737 name
= heap_strndupW(setCookieHeader
->lpszValue
, data
-setCookieHeader
->lpszValue
);
742 set_cookie(host
->lpszValue
, request
->path
, name
, data
);
747 static void strip_spaces(LPWSTR start
)
752 while (*str
== ' ' && *str
!= '\0')
756 memmove(start
, str
, sizeof(WCHAR
) * (strlenW(str
) + 1));
758 end
= start
+ strlenW(start
) - 1;
759 while (end
>= start
&& *end
== ' ')
766 static inline BOOL
is_basic_auth_value( LPCWSTR pszAuthValue
, LPWSTR
*pszRealm
)
768 static const WCHAR szBasic
[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
769 static const WCHAR szRealm
[] = {'r','e','a','l','m'}; /* Note: not nul-terminated */
771 is_basic
= !strncmpiW(pszAuthValue
, szBasic
, ARRAYSIZE(szBasic
)) &&
772 ((pszAuthValue
[ARRAYSIZE(szBasic
)] == ' ') || !pszAuthValue
[ARRAYSIZE(szBasic
)]);
773 if (is_basic
&& pszRealm
)
776 LPCWSTR ptr
= &pszAuthValue
[ARRAYSIZE(szBasic
)];
780 token
= strchrW(ptr
,'=');
784 while (*realm
== ' ' && *realm
!= '\0')
786 if(!strncmpiW(realm
, szRealm
, ARRAYSIZE(szRealm
)) &&
787 (realm
[ARRAYSIZE(szRealm
)] == ' ' || realm
[ARRAYSIZE(szRealm
)] == '='))
790 while (*token
== ' ' && *token
!= '\0')
794 *pszRealm
= heap_strdupW(token
);
795 strip_spaces(*pszRealm
);
802 static void destroy_authinfo( struct HttpAuthInfo
*authinfo
)
804 if (!authinfo
) return;
806 if (SecIsValidHandle(&authinfo
->ctx
))
807 DeleteSecurityContext(&authinfo
->ctx
);
808 if (SecIsValidHandle(&authinfo
->cred
))
809 FreeCredentialsHandle(&authinfo
->cred
);
811 heap_free(authinfo
->auth_data
);
812 heap_free(authinfo
->scheme
);
816 static UINT
retrieve_cached_basic_authorization(LPWSTR host
, LPWSTR realm
, LPSTR
*auth_data
)
818 basicAuthorizationData
*ad
;
821 TRACE("Looking for authorization for %s:%s\n",debugstr_w(host
),debugstr_w(realm
));
823 EnterCriticalSection(&authcache_cs
);
824 LIST_FOR_EACH_ENTRY(ad
, &basicAuthorizationCache
, basicAuthorizationData
, entry
)
826 if (!strcmpiW(host
,ad
->host
) && !strcmpW(realm
,ad
->realm
))
828 TRACE("Authorization found in cache\n");
829 *auth_data
= heap_alloc(ad
->authorizationLen
);
830 memcpy(*auth_data
,ad
->authorization
,ad
->authorizationLen
);
831 rc
= ad
->authorizationLen
;
835 LeaveCriticalSection(&authcache_cs
);
839 static void cache_basic_authorization(LPWSTR host
, LPWSTR realm
, LPSTR auth_data
, UINT auth_data_len
)
842 basicAuthorizationData
* ad
= NULL
;
844 TRACE("caching authorization for %s:%s = %s\n",debugstr_w(host
),debugstr_w(realm
),debugstr_an(auth_data
,auth_data_len
));
846 EnterCriticalSection(&authcache_cs
);
847 LIST_FOR_EACH(cursor
, &basicAuthorizationCache
)
849 basicAuthorizationData
*check
= LIST_ENTRY(cursor
,basicAuthorizationData
,entry
);
850 if (!strcmpiW(host
,check
->host
) && !strcmpW(realm
,check
->realm
))
859 TRACE("Found match in cache, replacing\n");
860 heap_free(ad
->authorization
);
861 ad
->authorization
= heap_alloc(auth_data_len
);
862 memcpy(ad
->authorization
, auth_data
, auth_data_len
);
863 ad
->authorizationLen
= auth_data_len
;
867 ad
= heap_alloc(sizeof(basicAuthorizationData
));
868 ad
->host
= heap_strdupW(host
);
869 ad
->realm
= heap_strdupW(realm
);
870 ad
->authorization
= heap_alloc(auth_data_len
);
871 memcpy(ad
->authorization
, auth_data
, auth_data_len
);
872 ad
->authorizationLen
= auth_data_len
;
873 list_add_head(&basicAuthorizationCache
,&ad
->entry
);
874 TRACE("authorization cached\n");
876 LeaveCriticalSection(&authcache_cs
);
879 static BOOL
retrieve_cached_authorization(LPWSTR host
, LPWSTR scheme
,
880 SEC_WINNT_AUTH_IDENTITY_W
*nt_auth_identity
)
882 authorizationData
*ad
;
884 TRACE("Looking for authorization for %s:%s\n", debugstr_w(host
), debugstr_w(scheme
));
886 EnterCriticalSection(&authcache_cs
);
887 LIST_FOR_EACH_ENTRY(ad
, &authorizationCache
, authorizationData
, entry
) {
888 if(!strcmpiW(host
, ad
->host
) && !strcmpiW(scheme
, ad
->scheme
)) {
889 TRACE("Authorization found in cache\n");
891 nt_auth_identity
->User
= heap_strdupW(ad
->user
);
892 nt_auth_identity
->Password
= heap_strdupW(ad
->password
);
893 nt_auth_identity
->Domain
= heap_alloc(sizeof(WCHAR
)*ad
->domain_len
);
894 if(!nt_auth_identity
->User
|| !nt_auth_identity
->Password
||
895 (!nt_auth_identity
->Domain
&& ad
->domain_len
)) {
896 heap_free(nt_auth_identity
->User
);
897 heap_free(nt_auth_identity
->Password
);
898 heap_free(nt_auth_identity
->Domain
);
902 nt_auth_identity
->Flags
= SEC_WINNT_AUTH_IDENTITY_UNICODE
;
903 nt_auth_identity
->UserLength
= ad
->user_len
;
904 nt_auth_identity
->PasswordLength
= ad
->password_len
;
905 memcpy(nt_auth_identity
->Domain
, ad
->domain
, sizeof(WCHAR
)*ad
->domain_len
);
906 nt_auth_identity
->DomainLength
= ad
->domain_len
;
907 LeaveCriticalSection(&authcache_cs
);
911 LeaveCriticalSection(&authcache_cs
);
916 static void cache_authorization(LPWSTR host
, LPWSTR scheme
,
917 SEC_WINNT_AUTH_IDENTITY_W
*nt_auth_identity
)
919 authorizationData
*ad
;
922 TRACE("Caching authorization for %s:%s\n", debugstr_w(host
), debugstr_w(scheme
));
924 EnterCriticalSection(&authcache_cs
);
925 LIST_FOR_EACH_ENTRY(ad
, &authorizationCache
, authorizationData
, entry
)
926 if(!strcmpiW(host
, ad
->host
) && !strcmpiW(scheme
, ad
->scheme
)) {
933 heap_free(ad
->password
);
934 heap_free(ad
->domain
);
936 ad
= heap_alloc(sizeof(authorizationData
));
938 LeaveCriticalSection(&authcache_cs
);
942 ad
->host
= heap_strdupW(host
);
943 ad
->scheme
= heap_strdupW(scheme
);
944 list_add_head(&authorizationCache
, &ad
->entry
);
947 ad
->user
= heap_strndupW(nt_auth_identity
->User
, nt_auth_identity
->UserLength
);
948 ad
->password
= heap_strndupW(nt_auth_identity
->Password
, nt_auth_identity
->PasswordLength
);
949 ad
->domain
= heap_strndupW(nt_auth_identity
->Domain
, nt_auth_identity
->DomainLength
);
950 ad
->user_len
= nt_auth_identity
->UserLength
;
951 ad
->password_len
= nt_auth_identity
->PasswordLength
;
952 ad
->domain_len
= nt_auth_identity
->DomainLength
;
954 if(!ad
->host
|| !ad
->scheme
|| !ad
->user
|| !ad
->password
955 || (nt_auth_identity
->Domain
&& !ad
->domain
)) {
957 heap_free(ad
->scheme
);
959 heap_free(ad
->password
);
960 heap_free(ad
->domain
);
961 list_remove(&ad
->entry
);
965 LeaveCriticalSection(&authcache_cs
);
968 static BOOL
HTTP_DoAuthorization( http_request_t
*request
, LPCWSTR pszAuthValue
,
969 struct HttpAuthInfo
**ppAuthInfo
,
970 LPWSTR domain_and_username
, LPWSTR password
,
973 SECURITY_STATUS sec_status
;
974 struct HttpAuthInfo
*pAuthInfo
= *ppAuthInfo
;
976 LPWSTR szRealm
= NULL
;
978 TRACE("%s\n", debugstr_w(pszAuthValue
));
985 pAuthInfo
= heap_alloc(sizeof(*pAuthInfo
));
989 SecInvalidateHandle(&pAuthInfo
->cred
);
990 SecInvalidateHandle(&pAuthInfo
->ctx
);
991 memset(&pAuthInfo
->exp
, 0, sizeof(pAuthInfo
->exp
));
993 pAuthInfo
->auth_data
= NULL
;
994 pAuthInfo
->auth_data_len
= 0;
995 pAuthInfo
->finished
= FALSE
;
997 if (is_basic_auth_value(pszAuthValue
,NULL
))
999 static const WCHAR szBasic
[] = {'B','a','s','i','c',0};
1000 pAuthInfo
->scheme
= heap_strdupW(szBasic
);
1001 if (!pAuthInfo
->scheme
)
1003 heap_free(pAuthInfo
);
1010 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity
;
1012 pAuthInfo
->scheme
= heap_strdupW(pszAuthValue
);
1013 if (!pAuthInfo
->scheme
)
1015 heap_free(pAuthInfo
);
1019 if (domain_and_username
)
1021 WCHAR
*user
= strchrW(domain_and_username
, '\\');
1022 WCHAR
*domain
= domain_and_username
;
1024 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
1026 pAuthData
= &nt_auth_identity
;
1031 user
= domain_and_username
;
1035 nt_auth_identity
.Flags
= SEC_WINNT_AUTH_IDENTITY_UNICODE
;
1036 nt_auth_identity
.User
= user
;
1037 nt_auth_identity
.UserLength
= strlenW(nt_auth_identity
.User
);
1038 nt_auth_identity
.Domain
= domain
;
1039 nt_auth_identity
.DomainLength
= domain
? user
- domain
- 1 : 0;
1040 nt_auth_identity
.Password
= password
;
1041 nt_auth_identity
.PasswordLength
= strlenW(nt_auth_identity
.Password
);
1043 cache_authorization(host
, pAuthInfo
->scheme
, &nt_auth_identity
);
1045 else if(retrieve_cached_authorization(host
, pAuthInfo
->scheme
, &nt_auth_identity
))
1046 pAuthData
= &nt_auth_identity
;
1048 /* use default credentials */
1051 sec_status
= AcquireCredentialsHandleW(NULL
, pAuthInfo
->scheme
,
1052 SECPKG_CRED_OUTBOUND
, NULL
,
1054 NULL
, &pAuthInfo
->cred
,
1057 if(pAuthData
&& !domain_and_username
) {
1058 heap_free(nt_auth_identity
.User
);
1059 heap_free(nt_auth_identity
.Domain
);
1060 heap_free(nt_auth_identity
.Password
);
1063 if (sec_status
== SEC_E_OK
)
1065 PSecPkgInfoW sec_pkg_info
;
1066 sec_status
= QuerySecurityPackageInfoW(pAuthInfo
->scheme
, &sec_pkg_info
);
1067 if (sec_status
== SEC_E_OK
)
1069 pAuthInfo
->max_token
= sec_pkg_info
->cbMaxToken
;
1070 FreeContextBuffer(sec_pkg_info
);
1073 if (sec_status
!= SEC_E_OK
)
1075 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
1076 debugstr_w(pAuthInfo
->scheme
), sec_status
);
1077 heap_free(pAuthInfo
->scheme
);
1078 heap_free(pAuthInfo
);
1082 *ppAuthInfo
= pAuthInfo
;
1084 else if (pAuthInfo
->finished
)
1087 if ((strlenW(pszAuthValue
) < strlenW(pAuthInfo
->scheme
)) ||
1088 strncmpiW(pszAuthValue
, pAuthInfo
->scheme
, strlenW(pAuthInfo
->scheme
)))
1090 ERR("authentication scheme changed from %s to %s\n",
1091 debugstr_w(pAuthInfo
->scheme
), debugstr_w(pszAuthValue
));
1095 if (is_basic_auth_value(pszAuthValue
,&szRealm
))
1099 char *auth_data
= NULL
;
1100 UINT auth_data_len
= 0;
1102 TRACE("basic authentication realm %s\n",debugstr_w(szRealm
));
1104 if (!domain_and_username
)
1106 if (host
&& szRealm
)
1107 auth_data_len
= retrieve_cached_basic_authorization(host
, szRealm
,&auth_data
);
1108 if (auth_data_len
== 0)
1116 userlen
= WideCharToMultiByte(CP_UTF8
, 0, domain_and_username
, lstrlenW(domain_and_username
), NULL
, 0, NULL
, NULL
);
1117 passlen
= WideCharToMultiByte(CP_UTF8
, 0, password
, lstrlenW(password
), NULL
, 0, NULL
, NULL
);
1119 /* length includes a nul terminator, which will be re-used for the ':' */
1120 auth_data
= heap_alloc(userlen
+ 1 + passlen
);
1127 WideCharToMultiByte(CP_UTF8
, 0, domain_and_username
, -1, auth_data
, userlen
, NULL
, NULL
);
1128 auth_data
[userlen
] = ':';
1129 WideCharToMultiByte(CP_UTF8
, 0, password
, -1, &auth_data
[userlen
+1], passlen
, NULL
, NULL
);
1130 auth_data_len
= userlen
+ 1 + passlen
;
1131 if (host
&& szRealm
)
1132 cache_basic_authorization(host
, szRealm
, auth_data
, auth_data_len
);
1135 pAuthInfo
->auth_data
= auth_data
;
1136 pAuthInfo
->auth_data_len
= auth_data_len
;
1137 pAuthInfo
->finished
= TRUE
;
1143 LPCWSTR pszAuthData
;
1144 SecBufferDesc out_desc
, in_desc
;
1146 unsigned char *buffer
;
1147 ULONG context_req
= ISC_REQ_CONNECTION
| ISC_REQ_USE_DCE_STYLE
|
1148 ISC_REQ_MUTUAL_AUTH
| ISC_REQ_DELEGATE
;
1150 in
.BufferType
= SECBUFFER_TOKEN
;
1154 in_desc
.ulVersion
= 0;
1155 in_desc
.cBuffers
= 1;
1156 in_desc
.pBuffers
= &in
;
1158 pszAuthData
= pszAuthValue
+ strlenW(pAuthInfo
->scheme
);
1159 if (*pszAuthData
== ' ')
1162 in
.cbBuffer
= HTTP_DecodeBase64(pszAuthData
, NULL
);
1163 in
.pvBuffer
= heap_alloc(in
.cbBuffer
);
1164 HTTP_DecodeBase64(pszAuthData
, in
.pvBuffer
);
1167 buffer
= heap_alloc(pAuthInfo
->max_token
);
1169 out
.BufferType
= SECBUFFER_TOKEN
;
1170 out
.cbBuffer
= pAuthInfo
->max_token
;
1171 out
.pvBuffer
= buffer
;
1173 out_desc
.ulVersion
= 0;
1174 out_desc
.cBuffers
= 1;
1175 out_desc
.pBuffers
= &out
;
1177 sec_status
= InitializeSecurityContextW(first
? &pAuthInfo
->cred
: NULL
,
1178 first
? NULL
: &pAuthInfo
->ctx
,
1179 first
? request
->server
->name
: NULL
,
1180 context_req
, 0, SECURITY_NETWORK_DREP
,
1181 in
.pvBuffer
? &in_desc
: NULL
,
1182 0, &pAuthInfo
->ctx
, &out_desc
,
1183 &pAuthInfo
->attr
, &pAuthInfo
->exp
);
1184 if (sec_status
== SEC_E_OK
)
1186 pAuthInfo
->finished
= TRUE
;
1187 pAuthInfo
->auth_data
= out
.pvBuffer
;
1188 pAuthInfo
->auth_data_len
= out
.cbBuffer
;
1189 TRACE("sending last auth packet\n");
1191 else if (sec_status
== SEC_I_CONTINUE_NEEDED
)
1193 pAuthInfo
->auth_data
= out
.pvBuffer
;
1194 pAuthInfo
->auth_data_len
= out
.cbBuffer
;
1195 TRACE("sending next auth packet\n");
1199 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status
);
1200 heap_free(out
.pvBuffer
);
1201 destroy_authinfo(pAuthInfo
);
1210 /***********************************************************************
1211 * HTTP_HttpAddRequestHeadersW (internal)
1213 static DWORD
HTTP_HttpAddRequestHeadersW(http_request_t
*request
,
1214 LPCWSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
1219 DWORD len
, res
= ERROR_HTTP_INVALID_HEADER
;
1221 TRACE("copying header: %s\n", debugstr_wn(lpszHeader
, dwHeaderLength
));
1223 if( dwHeaderLength
== ~0U )
1224 len
= strlenW(lpszHeader
);
1226 len
= dwHeaderLength
;
1227 buffer
= heap_alloc(sizeof(WCHAR
)*(len
+1));
1228 lstrcpynW( buffer
, lpszHeader
, len
+ 1);
1234 LPWSTR
* pFieldAndValue
;
1236 lpszEnd
= lpszStart
;
1238 while (*lpszEnd
!= '\0')
1240 if (*lpszEnd
== '\r' || *lpszEnd
== '\n')
1245 if (*lpszStart
== '\0')
1248 if (*lpszEnd
== '\r' || *lpszEnd
== '\n')
1251 lpszEnd
++; /* Jump over newline */
1253 TRACE("interpreting header %s\n", debugstr_w(lpszStart
));
1254 if (*lpszStart
== '\0')
1256 /* Skip 0-length headers */
1257 lpszStart
= lpszEnd
;
1258 res
= ERROR_SUCCESS
;
1261 pFieldAndValue
= HTTP_InterpretHttpHeader(lpszStart
);
1264 res
= HTTP_VerifyValidHeader(request
, pFieldAndValue
[0]);
1265 if (res
== ERROR_SUCCESS
)
1266 res
= HTTP_ProcessHeader(request
, pFieldAndValue
[0],
1267 pFieldAndValue
[1], dwModifier
| HTTP_ADDHDR_FLAG_REQ
);
1268 HTTP_FreeTokens(pFieldAndValue
);
1271 lpszStart
= lpszEnd
;
1272 } while (res
== ERROR_SUCCESS
);
1278 /***********************************************************************
1279 * HttpAddRequestHeadersW (WININET.@)
1281 * Adds one or more HTTP header to the request handler
1284 * On Windows if dwHeaderLength includes the trailing '\0', then
1285 * HttpAddRequestHeadersW() adds it too. However this results in an
1286 * invalid HTTP header which is rejected by some servers so we probably
1287 * don't need to match Windows on that point.
1294 BOOL WINAPI
HttpAddRequestHeadersW(HINTERNET hHttpRequest
,
1295 LPCWSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
1297 http_request_t
*request
;
1298 DWORD res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
1300 TRACE("%p, %s, %i, %i\n", hHttpRequest
, debugstr_wn(lpszHeader
, dwHeaderLength
), dwHeaderLength
, dwModifier
);
1305 request
= (http_request_t
*) get_handle_object( hHttpRequest
);
1306 if (request
&& request
->hdr
.htype
== WH_HHTTPREQ
)
1307 res
= HTTP_HttpAddRequestHeadersW( request
, lpszHeader
, dwHeaderLength
, dwModifier
);
1309 WININET_Release( &request
->hdr
);
1311 if(res
!= ERROR_SUCCESS
)
1313 return res
== ERROR_SUCCESS
;
1316 /***********************************************************************
1317 * HttpAddRequestHeadersA (WININET.@)
1319 * Adds one or more HTTP header to the request handler
1326 BOOL WINAPI
HttpAddRequestHeadersA(HINTERNET hHttpRequest
,
1327 LPCSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
1333 TRACE("%p, %s, %i, %i\n", hHttpRequest
, debugstr_an(lpszHeader
, dwHeaderLength
), dwHeaderLength
, dwModifier
);
1335 len
= MultiByteToWideChar( CP_ACP
, 0, lpszHeader
, dwHeaderLength
, NULL
, 0 );
1336 hdr
= heap_alloc(len
*sizeof(WCHAR
));
1337 MultiByteToWideChar( CP_ACP
, 0, lpszHeader
, dwHeaderLength
, hdr
, len
);
1338 if( dwHeaderLength
!= ~0U )
1339 dwHeaderLength
= len
;
1341 r
= HttpAddRequestHeadersW( hHttpRequest
, hdr
, dwHeaderLength
, dwModifier
);
1347 static void free_accept_types( WCHAR
**accept_types
)
1349 WCHAR
*ptr
, **types
= accept_types
;
1352 while ((ptr
= *types
))
1357 heap_free( accept_types
);
1360 static WCHAR
**convert_accept_types( const char **accept_types
)
1363 const char **types
= accept_types
;
1365 BOOL invalid_pointer
= FALSE
;
1367 if (!types
) return NULL
;
1373 /* find out how many there are */
1374 if (*types
&& **types
)
1376 TRACE("accept type: %s\n", debugstr_a(*types
));
1382 WARN("invalid accept type pointer\n");
1383 invalid_pointer
= TRUE
;
1388 if (invalid_pointer
) return NULL
;
1389 if (!(typesW
= heap_alloc( sizeof(WCHAR
*) * (count
+ 1) ))) return NULL
;
1391 types
= accept_types
;
1394 if (*types
&& **types
) typesW
[count
++] = heap_strdupAtoW( *types
);
1397 typesW
[count
] = NULL
;
1401 /***********************************************************************
1402 * HttpOpenRequestA (WININET.@)
1404 * Open a HTTP request handle
1407 * HINTERNET a HTTP request handle on success
1411 HINTERNET WINAPI
HttpOpenRequestA(HINTERNET hHttpSession
,
1412 LPCSTR lpszVerb
, LPCSTR lpszObjectName
, LPCSTR lpszVersion
,
1413 LPCSTR lpszReferrer
, LPCSTR
*lpszAcceptTypes
,
1414 DWORD dwFlags
, DWORD_PTR dwContext
)
1416 LPWSTR szVerb
= NULL
, szObjectName
= NULL
;
1417 LPWSTR szVersion
= NULL
, szReferrer
= NULL
, *szAcceptTypes
= NULL
;
1418 HINTERNET rc
= NULL
;
1420 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession
,
1421 debugstr_a(lpszVerb
), debugstr_a(lpszObjectName
),
1422 debugstr_a(lpszVersion
), debugstr_a(lpszReferrer
), lpszAcceptTypes
,
1423 dwFlags
, dwContext
);
1427 szVerb
= heap_strdupAtoW(lpszVerb
);
1434 szObjectName
= heap_strdupAtoW(lpszObjectName
);
1435 if ( !szObjectName
)
1441 szVersion
= heap_strdupAtoW(lpszVersion
);
1448 szReferrer
= heap_strdupAtoW(lpszReferrer
);
1453 szAcceptTypes
= convert_accept_types( lpszAcceptTypes
);
1454 rc
= HttpOpenRequestW(hHttpSession
, szVerb
, szObjectName
, szVersion
, szReferrer
,
1455 (const WCHAR
**)szAcceptTypes
, dwFlags
, dwContext
);
1458 free_accept_types(szAcceptTypes
);
1459 heap_free(szReferrer
);
1460 heap_free(szVersion
);
1461 heap_free(szObjectName
);
1466 /***********************************************************************
1469 static UINT
HTTP_EncodeBase64( LPCSTR bin
, unsigned int len
, LPWSTR base64
)
1472 static const CHAR HTTP_Base64Enc
[] =
1473 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1477 /* first 6 bits, all from bin[0] */
1478 base64
[n
++] = HTTP_Base64Enc
[(bin
[0] & 0xfc) >> 2];
1479 x
= (bin
[0] & 3) << 4;
1481 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1484 base64
[n
++] = HTTP_Base64Enc
[x
];
1489 base64
[n
++] = HTTP_Base64Enc
[ x
| ( (bin
[1]&0xf0) >> 4 ) ];
1490 x
= ( bin
[1] & 0x0f ) << 2;
1492 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1495 base64
[n
++] = HTTP_Base64Enc
[x
];
1499 base64
[n
++] = HTTP_Base64Enc
[ x
| ( (bin
[2]&0xc0 ) >> 6 ) ];
1501 /* last 6 bits, all from bin [2] */
1502 base64
[n
++] = HTTP_Base64Enc
[ bin
[2] & 0x3f ];
1510 #define CH(x) (((x) >= 'A' && (x) <= 'Z') ? (x) - 'A' : \
1511 ((x) >= 'a' && (x) <= 'z') ? (x) - 'a' + 26 : \
1512 ((x) >= '0' && (x) <= '9') ? (x) - '0' + 52 : \
1513 ((x) == '+') ? 62 : ((x) == '/') ? 63 : -1)
1514 static const signed char HTTP_Base64Dec
[256] =
1516 CH( 0),CH( 1),CH( 2),CH( 3),CH( 4),CH( 5),CH( 6),CH( 7),CH( 8),CH( 9),
1517 CH(10),CH(11),CH(12),CH(13),CH(14),CH(15),CH(16),CH(17),CH(18),CH(19),
1518 CH(20),CH(21),CH(22),CH(23),CH(24),CH(25),CH(26),CH(27),CH(28),CH(29),
1519 CH(30),CH(31),CH(32),CH(33),CH(34),CH(35),CH(36),CH(37),CH(38),CH(39),
1520 CH(40),CH(41),CH(42),CH(43),CH(44),CH(45),CH(46),CH(47),CH(48),CH(49),
1521 CH(50),CH(51),CH(52),CH(53),CH(54),CH(55),CH(56),CH(57),CH(58),CH(59),
1522 CH(60),CH(61),CH(62),CH(63),CH(64),CH(65),CH(66),CH(67),CH(68),CH(69),
1523 CH(70),CH(71),CH(72),CH(73),CH(74),CH(75),CH(76),CH(77),CH(78),CH(79),
1524 CH(80),CH(81),CH(82),CH(83),CH(84),CH(85),CH(86),CH(87),CH(88),CH(89),
1525 CH(90),CH(91),CH(92),CH(93),CH(94),CH(95),CH(96),CH(97),CH(98),CH(99),
1526 CH(100),CH(101),CH(102),CH(103),CH(104),CH(105),CH(106),CH(107),CH(108),CH(109),
1527 CH(110),CH(111),CH(112),CH(113),CH(114),CH(115),CH(116),CH(117),CH(118),CH(119),
1528 CH(120),CH(121),CH(122),CH(123),CH(124),CH(125),CH(126),CH(127),CH(128),CH(129),
1529 CH(130),CH(131),CH(132),CH(133),CH(134),CH(135),CH(136),CH(137),CH(138),CH(139),
1530 CH(140),CH(141),CH(142),CH(143),CH(144),CH(145),CH(146),CH(147),CH(148),CH(149),
1531 CH(150),CH(151),CH(152),CH(153),CH(154),CH(155),CH(156),CH(157),CH(158),CH(159),
1532 CH(160),CH(161),CH(162),CH(163),CH(164),CH(165),CH(166),CH(167),CH(168),CH(169),
1533 CH(170),CH(171),CH(172),CH(173),CH(174),CH(175),CH(176),CH(177),CH(178),CH(179),
1534 CH(180),CH(181),CH(182),CH(183),CH(184),CH(185),CH(186),CH(187),CH(188),CH(189),
1535 CH(190),CH(191),CH(192),CH(193),CH(194),CH(195),CH(196),CH(197),CH(198),CH(199),
1536 CH(200),CH(201),CH(202),CH(203),CH(204),CH(205),CH(206),CH(207),CH(208),CH(209),
1537 CH(210),CH(211),CH(212),CH(213),CH(214),CH(215),CH(216),CH(217),CH(218),CH(219),
1538 CH(220),CH(221),CH(222),CH(223),CH(224),CH(225),CH(226),CH(227),CH(228),CH(229),
1539 CH(230),CH(231),CH(232),CH(233),CH(234),CH(235),CH(236),CH(237),CH(238),CH(239),
1540 CH(240),CH(241),CH(242),CH(243),CH(244),CH(245),CH(246),CH(247),CH(248), CH(249),
1541 CH(250),CH(251),CH(252),CH(253),CH(254),CH(255),
1545 /***********************************************************************
1548 static UINT
HTTP_DecodeBase64( LPCWSTR base64
, LPSTR bin
)
1556 if (base64
[0] >= ARRAYSIZE(HTTP_Base64Dec
) ||
1557 ((in
[0] = HTTP_Base64Dec
[base64
[0]]) == -1) ||
1558 base64
[1] >= ARRAYSIZE(HTTP_Base64Dec
) ||
1559 ((in
[1] = HTTP_Base64Dec
[base64
[1]]) == -1))
1561 WARN("invalid base64: %s\n", debugstr_w(base64
));
1565 bin
[n
] = (unsigned char) (in
[0] << 2 | in
[1] >> 4);
1568 if ((base64
[2] == '=') && (base64
[3] == '='))
1570 if (base64
[2] > ARRAYSIZE(HTTP_Base64Dec
) ||
1571 ((in
[2] = HTTP_Base64Dec
[base64
[2]]) == -1))
1573 WARN("invalid base64: %s\n", debugstr_w(&base64
[2]));
1577 bin
[n
] = (unsigned char) (in
[1] << 4 | in
[2] >> 2);
1580 if (base64
[3] == '=')
1582 if (base64
[3] > ARRAYSIZE(HTTP_Base64Dec
) ||
1583 ((in
[3] = HTTP_Base64Dec
[base64
[3]]) == -1))
1585 WARN("invalid base64: %s\n", debugstr_w(&base64
[3]));
1589 bin
[n
] = (unsigned char) (((in
[2] << 6) & 0xc0) | in
[3]);
1598 /***********************************************************************
1599 * HTTP_InsertAuthorization
1601 * Insert or delete the authorization field in the request header.
1603 static BOOL
HTTP_InsertAuthorization( http_request_t
*request
, struct HttpAuthInfo
*pAuthInfo
, LPCWSTR header
)
1607 static const WCHAR wszSpace
[] = {' ',0};
1608 static const WCHAR wszBasic
[] = {'B','a','s','i','c',0};
1610 WCHAR
*authorization
= NULL
;
1612 if (pAuthInfo
->auth_data_len
)
1614 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1615 len
= strlenW(pAuthInfo
->scheme
)+1+((pAuthInfo
->auth_data_len
+2)*4)/3;
1616 authorization
= heap_alloc((len
+1)*sizeof(WCHAR
));
1620 strcpyW(authorization
, pAuthInfo
->scheme
);
1621 strcatW(authorization
, wszSpace
);
1622 HTTP_EncodeBase64(pAuthInfo
->auth_data
,
1623 pAuthInfo
->auth_data_len
,
1624 authorization
+strlenW(authorization
));
1626 /* clear the data as it isn't valid now that it has been sent to the
1627 * server, unless it's Basic authentication which doesn't do
1628 * connection tracking */
1629 if (strcmpiW(pAuthInfo
->scheme
, wszBasic
))
1631 heap_free(pAuthInfo
->auth_data
);
1632 pAuthInfo
->auth_data
= NULL
;
1633 pAuthInfo
->auth_data_len
= 0;
1637 TRACE("Inserting authorization: %s\n", debugstr_w(authorization
));
1639 HTTP_ProcessHeader(request
, header
, authorization
, HTTP_ADDHDR_FLAG_REQ
| HTTP_ADDHDR_FLAG_REPLACE
);
1640 heap_free(authorization
);
1645 static WCHAR
*build_proxy_path_url(http_request_t
*req
)
1650 len
= strlenW(req
->server
->scheme_host_port
);
1651 size
= len
+ strlenW(req
->path
) + 1;
1652 if(*req
->path
!= '/')
1654 url
= heap_alloc(size
* sizeof(WCHAR
));
1658 memcpy(url
, req
->server
->scheme_host_port
, len
*sizeof(WCHAR
));
1659 if(*req
->path
!= '/')
1662 strcpyW(url
+len
, req
->path
);
1664 TRACE("url=%s\n", debugstr_w(url
));
1668 static BOOL
HTTP_DomainMatches(LPCWSTR server
, LPCWSTR domain
)
1670 static const WCHAR localW
[] = { '<','l','o','c','a','l','>',0 };
1673 if (!strcmpiW( domain
, localW
) && !strchrW( server
, '.' ))
1675 else if (*domain
== '*')
1677 if (domain
[1] == '.')
1681 /* For a hostname to match a wildcard, the last domain must match
1682 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
1683 * hostname is www.foo.a.b, it matches, but a.b does not.
1685 dot
= strchrW( server
, '.' );
1688 int len
= strlenW( dot
+ 1 );
1690 if (len
> strlenW( domain
+ 2 ))
1694 /* The server's domain is longer than the wildcard, so it
1695 * could be a subdomain. Compare the last portion of the
1698 ptr
= dot
+ len
+ 1 - strlenW( domain
+ 2 );
1699 if (!strcmpiW( ptr
, domain
+ 2 ))
1701 /* This is only a match if the preceding character is
1702 * a '.', i.e. that it is a matching domain. E.g.
1703 * if domain is '*.b.c' and server is 'www.ab.c' they
1706 ret
= *(ptr
- 1) == '.';
1710 ret
= !strcmpiW( dot
+ 1, domain
+ 2 );
1715 ret
= !strcmpiW( server
, domain
);
1719 static BOOL
HTTP_ShouldBypassProxy(appinfo_t
*lpwai
, LPCWSTR server
)
1724 if (!lpwai
->proxyBypass
) return FALSE
;
1725 ptr
= lpwai
->proxyBypass
;
1729 ptr
= strchrW( ptr
, ';' );
1731 ptr
= strchrW( tmp
, ' ' );
1734 if (ptr
- tmp
< INTERNET_MAX_HOST_NAME_LENGTH
)
1736 WCHAR domain
[INTERNET_MAX_HOST_NAME_LENGTH
];
1738 memcpy( domain
, tmp
, (ptr
- tmp
) * sizeof(WCHAR
) );
1739 domain
[ptr
- tmp
] = 0;
1740 ret
= HTTP_DomainMatches( server
, domain
);
1745 ret
= HTTP_DomainMatches( server
, tmp
);
1746 } while (ptr
&& !ret
);
1750 /***********************************************************************
1751 * HTTP_DealWithProxy
1753 static BOOL
HTTP_DealWithProxy(appinfo_t
*hIC
, http_session_t
*session
, http_request_t
*request
)
1755 static const WCHAR protoHttp
[] = { 'h','t','t','p',0 };
1756 static const WCHAR szHttp
[] = { 'h','t','t','p',':','/','/',0 };
1757 static const WCHAR szFormat
[] = { 'h','t','t','p',':','/','/','%','s',0 };
1758 WCHAR buf
[INTERNET_MAX_HOST_NAME_LENGTH
];
1759 WCHAR protoProxy
[INTERNET_MAX_URL_LENGTH
];
1760 DWORD protoProxyLen
= INTERNET_MAX_URL_LENGTH
;
1761 WCHAR proxy
[INTERNET_MAX_URL_LENGTH
];
1762 static WCHAR szNul
[] = { 0 };
1763 URL_COMPONENTSW UrlComponents
;
1764 server_t
*new_server
;
1767 memset( &UrlComponents
, 0, sizeof UrlComponents
);
1768 UrlComponents
.dwStructSize
= sizeof UrlComponents
;
1769 UrlComponents
.lpszHostName
= buf
;
1770 UrlComponents
.dwHostNameLength
= INTERNET_MAX_HOST_NAME_LENGTH
;
1772 if (!INTERNET_FindProxyForProtocol(hIC
->proxy
, protoHttp
, protoProxy
, &protoProxyLen
))
1774 if( CSTR_EQUAL
!= CompareStringW(LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
,
1775 protoProxy
,strlenW(szHttp
),szHttp
,strlenW(szHttp
)) )
1776 sprintfW(proxy
, szFormat
, protoProxy
);
1778 strcpyW(proxy
, protoProxy
);
1779 if( !InternetCrackUrlW(proxy
, 0, 0, &UrlComponents
) )
1781 if( UrlComponents
.dwHostNameLength
== 0 )
1784 if( !request
->path
)
1785 request
->path
= szNul
;
1787 is_https
= (UrlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
);
1788 if (is_https
&& UrlComponents
.nPort
== INTERNET_INVALID_PORT_NUMBER
)
1789 UrlComponents
.nPort
= INTERNET_DEFAULT_HTTPS_PORT
;
1791 new_server
= get_server(UrlComponents
.lpszHostName
, UrlComponents
.nPort
, is_https
, TRUE
);
1795 request
->proxy
= new_server
;
1797 TRACE("proxy server=%s port=%d\n", debugstr_w(new_server
->name
), new_server
->port
);
1801 static DWORD
HTTP_ResolveName(http_request_t
*request
)
1803 server_t
*server
= request
->proxy
? request
->proxy
: request
->server
;
1807 if(server
->addr_len
)
1808 return ERROR_SUCCESS
;
1810 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
1811 INTERNET_STATUS_RESOLVING_NAME
,
1813 (strlenW(server
->name
)+1) * sizeof(WCHAR
));
1815 addr_len
= sizeof(server
->addr
);
1816 if (!GetAddress(server
->name
, server
->port
, (struct sockaddr
*)&server
->addr
, &addr_len
))
1817 return ERROR_INTERNET_NAME_NOT_RESOLVED
;
1819 switch(server
->addr
.ss_family
) {
1821 addr
= &((struct sockaddr_in
*)&server
->addr
)->sin_addr
;
1824 addr
= &((struct sockaddr_in6
*)&server
->addr
)->sin6_addr
;
1827 WARN("unsupported family %d\n", server
->addr
.ss_family
);
1828 return ERROR_INTERNET_NAME_NOT_RESOLVED
;
1831 server
->addr_len
= addr_len
;
1832 inet_ntop(server
->addr
.ss_family
, addr
, server
->addr_str
, sizeof(server
->addr_str
));
1833 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
1834 INTERNET_STATUS_NAME_RESOLVED
,
1835 server
->addr_str
, strlen(server
->addr_str
)+1);
1837 TRACE("resolved %s to %s\n", debugstr_w(server
->name
), server
->addr_str
);
1838 return ERROR_SUCCESS
;
1841 static BOOL
HTTP_GetRequestURL(http_request_t
*req
, LPWSTR buf
)
1843 static const WCHAR http
[] = { 'h','t','t','p',':','/','/',0 };
1844 static const WCHAR https
[] = { 'h','t','t','p','s',':','/','/',0 };
1845 static const WCHAR slash
[] = { '/',0 };
1846 LPHTTPHEADERW host_header
;
1849 host_header
= HTTP_GetHeader(req
, hostW
);
1853 if (req
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
)
1857 strcpyW(buf
, scheme
);
1858 strcatW(buf
, host_header
->lpszValue
);
1859 if (req
->path
[0] != '/')
1860 strcatW(buf
, slash
);
1861 strcatW(buf
, req
->path
);
1866 /***********************************************************************
1867 * HTTPREQ_Destroy (internal)
1869 * Deallocate request handle
1872 static void HTTPREQ_Destroy(object_header_t
*hdr
)
1874 http_request_t
*request
= (http_request_t
*) hdr
;
1879 if(request
->hCacheFile
)
1880 CloseHandle(request
->hCacheFile
);
1881 if(request
->req_file
)
1882 req_file_release(request
->req_file
);
1884 request
->read_section
.DebugInfo
->Spare
[0] = 0;
1885 DeleteCriticalSection( &request
->read_section
);
1886 WININET_Release(&request
->session
->hdr
);
1888 destroy_authinfo(request
->authInfo
);
1889 destroy_authinfo(request
->proxyAuthInfo
);
1892 server_release(request
->server
);
1894 server_release(request
->proxy
);
1896 heap_free(request
->path
);
1897 heap_free(request
->verb
);
1898 heap_free(request
->version
);
1899 heap_free(request
->statusText
);
1901 for (i
= 0; i
< request
->nCustHeaders
; i
++)
1903 heap_free(request
->custHeaders
[i
].lpszField
);
1904 heap_free(request
->custHeaders
[i
].lpszValue
);
1906 destroy_data_stream(request
->data_stream
);
1907 heap_free(request
->custHeaders
);
1910 static void http_release_netconn(http_request_t
*req
, BOOL reuse
)
1912 TRACE("%p %p %x\n",req
, req
->netconn
, reuse
);
1914 if(!is_valid_netconn(req
->netconn
))
1918 if(reuse
&& req
->netconn
->keep_alive
) {
1921 EnterCriticalSection(&connection_pool_cs
);
1923 list_add_head(&req
->netconn
->server
->conn_pool
, &req
->netconn
->pool_entry
);
1924 req
->netconn
->keep_until
= (DWORD64
)GetTickCount() + COLLECT_TIME
;
1925 req
->netconn
= NULL
;
1927 run_collector
= !collector_running
;
1928 collector_running
= TRUE
;
1930 LeaveCriticalSection(&connection_pool_cs
);
1933 HANDLE thread
= NULL
;
1936 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
, (const WCHAR
*)WININET_hModule
, &module
);
1938 thread
= CreateThread(NULL
, 0, collect_connections_proc
, NULL
, 0, NULL
);
1940 EnterCriticalSection(&connection_pool_cs
);
1941 collector_running
= FALSE
;
1942 LeaveCriticalSection(&connection_pool_cs
);
1945 FreeLibrary(module
);
1948 CloseHandle(thread
);
1953 // silence unused function warning
1954 (void)collect_connections_proc
;
1957 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
,
1958 INTERNET_STATUS_CLOSING_CONNECTION
, 0, 0);
1960 close_netconn(req
->netconn
);
1962 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
,
1963 INTERNET_STATUS_CONNECTION_CLOSED
, 0, 0);
1966 static BOOL
HTTP_KeepAlive(http_request_t
*request
)
1968 WCHAR szVersion
[10];
1969 WCHAR szConnectionResponse
[20];
1970 DWORD dwBufferSize
= sizeof(szVersion
);
1971 BOOL keepalive
= FALSE
;
1973 /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
1974 * the connection is keep-alive by default */
1975 if (HTTP_HttpQueryInfoW(request
, HTTP_QUERY_VERSION
, szVersion
, &dwBufferSize
, NULL
) == ERROR_SUCCESS
1976 && !strcmpiW(szVersion
, g_szHttp1_1
))
1981 dwBufferSize
= sizeof(szConnectionResponse
);
1982 if (HTTP_HttpQueryInfoW(request
, HTTP_QUERY_PROXY_CONNECTION
, szConnectionResponse
, &dwBufferSize
, NULL
) == ERROR_SUCCESS
1983 || HTTP_HttpQueryInfoW(request
, HTTP_QUERY_CONNECTION
, szConnectionResponse
, &dwBufferSize
, NULL
) == ERROR_SUCCESS
)
1985 keepalive
= !strcmpiW(szConnectionResponse
, szKeepAlive
);
1991 static void HTTPREQ_CloseConnection(object_header_t
*hdr
)
1993 http_request_t
*req
= (http_request_t
*)hdr
;
1995 http_release_netconn(req
, drain_content(req
, FALSE
));
1998 static DWORD
str_to_buffer(const WCHAR
*str
, void *buffer
, DWORD
*size
, BOOL unicode
)
2003 WCHAR
*buf
= buffer
;
2005 if (str
) len
= strlenW(str
);
2007 if (*size
< (len
+ 1) * sizeof(WCHAR
))
2009 *size
= (len
+ 1) * sizeof(WCHAR
);
2010 return ERROR_INSUFFICIENT_BUFFER
;
2012 if (str
) strcpyW(buf
, str
);
2016 return ERROR_SUCCESS
;
2022 if (str
) len
= WideCharToMultiByte(CP_ACP
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
2027 return ERROR_INSUFFICIENT_BUFFER
;
2029 if (str
) WideCharToMultiByte(CP_ACP
, 0, str
, -1, buf
, *size
, NULL
, NULL
);
2033 return ERROR_SUCCESS
;
2037 static DWORD
HTTPREQ_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
2039 http_request_t
*req
= (http_request_t
*)hdr
;
2042 case INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO
:
2044 INTERNET_DIAGNOSTIC_SOCKET_INFO
*info
= buffer
;
2046 FIXME("INTERNET_DIAGNOSTIC_SOCKET_INFO stub\n");
2048 if (*size
< sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO
))
2049 return ERROR_INSUFFICIENT_BUFFER
;
2050 *size
= sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO
);
2051 /* FIXME: can't get a SOCKET from our connection since we don't use
2055 /* FIXME: get source port from req->netConnection */
2056 info
->SourcePort
= 0;
2057 info
->DestPort
= req
->server
->port
;
2059 if (HTTP_KeepAlive(req
))
2060 info
->Flags
|= IDSI_FLAG_KEEP_ALIVE
;
2062 info
->Flags
|= IDSI_FLAG_PROXY
;
2063 if (is_valid_netconn(req
->netconn
) && req
->netconn
->secure
)
2064 info
->Flags
|= IDSI_FLAG_SECURE
;
2066 return ERROR_SUCCESS
;
2070 TRACE("Queried undocumented option 98, forwarding to INTERNET_OPTION_SECURITY_FLAGS\n");
2072 case INTERNET_OPTION_SECURITY_FLAGS
:
2076 if (*size
< sizeof(ULONG
))
2077 return ERROR_INSUFFICIENT_BUFFER
;
2079 *size
= sizeof(DWORD
);
2080 flags
= is_valid_netconn(req
->netconn
) ? req
->netconn
->security_flags
: req
->security_flags
| req
->server
->security_flags
;
2081 *(DWORD
*)buffer
= flags
;
2083 TRACE("INTERNET_OPTION_SECURITY_FLAGS %x\n", flags
);
2084 return ERROR_SUCCESS
;
2087 case INTERNET_OPTION_HANDLE_TYPE
:
2088 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
2090 if (*size
< sizeof(ULONG
))
2091 return ERROR_INSUFFICIENT_BUFFER
;
2093 *size
= sizeof(DWORD
);
2094 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_HTTP_REQUEST
;
2095 return ERROR_SUCCESS
;
2097 case INTERNET_OPTION_URL
: {
2098 WCHAR url
[INTERNET_MAX_URL_LENGTH
];
2101 static const WCHAR httpW
[] = {'h','t','t','p',':','/','/',0};
2103 TRACE("INTERNET_OPTION_URL\n");
2105 host
= HTTP_GetHeader(req
, hostW
);
2106 strcpyW(url
, httpW
);
2107 strcatW(url
, host
->lpszValue
);
2108 strcatW(url
, req
->path
);
2110 TRACE("INTERNET_OPTION_URL: %s\n",debugstr_w(url
));
2111 return str_to_buffer(url
, buffer
, size
, unicode
);
2113 case INTERNET_OPTION_USER_AGENT
:
2114 return str_to_buffer(req
->session
->appInfo
->agent
, buffer
, size
, unicode
);
2115 case INTERNET_OPTION_USERNAME
:
2116 return str_to_buffer(req
->session
->userName
, buffer
, size
, unicode
);
2117 case INTERNET_OPTION_PASSWORD
:
2118 return str_to_buffer(req
->session
->password
, buffer
, size
, unicode
);
2119 case INTERNET_OPTION_PROXY_USERNAME
:
2120 return str_to_buffer(req
->session
->appInfo
->proxyUsername
, buffer
, size
, unicode
);
2121 case INTERNET_OPTION_PROXY_PASSWORD
:
2122 return str_to_buffer(req
->session
->appInfo
->proxyPassword
, buffer
, size
, unicode
);
2124 case INTERNET_OPTION_CACHE_TIMESTAMPS
: {
2125 INTERNET_CACHE_ENTRY_INFOW
*info
;
2126 INTERNET_CACHE_TIMESTAMPS
*ts
= buffer
;
2127 WCHAR url
[INTERNET_MAX_URL_LENGTH
];
2128 DWORD nbytes
, error
;
2131 TRACE("INTERNET_OPTION_CACHE_TIMESTAMPS\n");
2133 if (*size
< sizeof(*ts
))
2135 *size
= sizeof(*ts
);
2136 return ERROR_INSUFFICIENT_BUFFER
;
2139 HTTP_GetRequestURL(req
, url
);
2140 ret
= GetUrlCacheEntryInfoW(url
, NULL
, &nbytes
);
2141 error
= GetLastError();
2142 if (!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
)
2144 if (!(info
= heap_alloc(nbytes
)))
2145 return ERROR_OUTOFMEMORY
;
2147 GetUrlCacheEntryInfoW(url
, info
, &nbytes
);
2149 ts
->ftExpires
= info
->ExpireTime
;
2150 ts
->ftLastModified
= info
->LastModifiedTime
;
2153 *size
= sizeof(*ts
);
2154 return ERROR_SUCCESS
;
2159 case INTERNET_OPTION_DATAFILE_NAME
: {
2162 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
2164 if(!req
->req_file
) {
2166 return ERROR_INTERNET_ITEM_NOT_FOUND
;
2170 req_size
= (lstrlenW(req
->req_file
->file_name
)+1) * sizeof(WCHAR
);
2171 if(*size
< req_size
)
2172 return ERROR_INSUFFICIENT_BUFFER
;
2175 memcpy(buffer
, req
->req_file
->file_name
, *size
);
2176 return ERROR_SUCCESS
;
2178 req_size
= WideCharToMultiByte(CP_ACP
, 0, req
->req_file
->file_name
, -1, NULL
, 0, NULL
, NULL
);
2179 if (req_size
> *size
)
2180 return ERROR_INSUFFICIENT_BUFFER
;
2182 *size
= WideCharToMultiByte(CP_ACP
, 0, req
->req_file
->file_name
,
2183 -1, buffer
, *size
, NULL
, NULL
);
2184 return ERROR_SUCCESS
;
2188 case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT
: {
2189 PCCERT_CONTEXT context
;
2192 return ERROR_INTERNET_INVALID_OPERATION
;
2194 if(*size
< sizeof(INTERNET_CERTIFICATE_INFOA
)) {
2195 *size
= sizeof(INTERNET_CERTIFICATE_INFOA
);
2196 return ERROR_INSUFFICIENT_BUFFER
;
2199 context
= (PCCERT_CONTEXT
)NETCON_GetCert(req
->netconn
);
2201 INTERNET_CERTIFICATE_INFOA
*info
= (INTERNET_CERTIFICATE_INFOA
*)buffer
;
2204 memset(info
, 0, sizeof(*info
));
2205 info
->ftExpiry
= context
->pCertInfo
->NotAfter
;
2206 info
->ftStart
= context
->pCertInfo
->NotBefore
;
2207 len
= CertNameToStrA(context
->dwCertEncodingType
,
2208 &context
->pCertInfo
->Subject
, CERT_SIMPLE_NAME_STR
|CERT_NAME_STR_CRLF_FLAG
, NULL
, 0);
2209 info
->lpszSubjectInfo
= LocalAlloc(0, len
);
2210 if(info
->lpszSubjectInfo
)
2211 CertNameToStrA(context
->dwCertEncodingType
,
2212 &context
->pCertInfo
->Subject
, CERT_SIMPLE_NAME_STR
|CERT_NAME_STR_CRLF_FLAG
,
2213 info
->lpszSubjectInfo
, len
);
2214 len
= CertNameToStrA(context
->dwCertEncodingType
,
2215 &context
->pCertInfo
->Issuer
, CERT_SIMPLE_NAME_STR
|CERT_NAME_STR_CRLF_FLAG
, NULL
, 0);
2216 info
->lpszIssuerInfo
= LocalAlloc(0, len
);
2217 if(info
->lpszIssuerInfo
)
2218 CertNameToStrA(context
->dwCertEncodingType
,
2219 &context
->pCertInfo
->Issuer
, CERT_SIMPLE_NAME_STR
|CERT_NAME_STR_CRLF_FLAG
,
2220 info
->lpszIssuerInfo
, len
);
2221 info
->dwKeySize
= NETCON_GetCipherStrength(req
->netconn
);
2222 CertFreeCertificateContext(context
);
2223 return ERROR_SUCCESS
;
2225 return ERROR_NOT_SUPPORTED
;
2227 case INTERNET_OPTION_CONNECT_TIMEOUT
:
2228 if (*size
< sizeof(DWORD
))
2229 return ERROR_INSUFFICIENT_BUFFER
;
2231 *size
= sizeof(DWORD
);
2232 *(DWORD
*)buffer
= req
->connect_timeout
;
2233 return ERROR_SUCCESS
;
2234 case INTERNET_OPTION_REQUEST_FLAGS
: {
2237 if (*size
< sizeof(DWORD
))
2238 return ERROR_INSUFFICIENT_BUFFER
;
2240 /* FIXME: Add support for:
2241 * INTERNET_REQFLAG_FROM_CACHE
2242 * INTERNET_REQFLAG_CACHE_WRITE_DISABLED
2246 flags
|= INTERNET_REQFLAG_VIA_PROXY
;
2247 if(!req
->status_code
)
2248 flags
|= INTERNET_REQFLAG_NO_HEADERS
;
2250 TRACE("INTERNET_OPTION_REQUEST_FLAGS returning %x\n", flags
);
2252 *size
= sizeof(DWORD
);
2253 *(DWORD
*)buffer
= flags
;
2254 return ERROR_SUCCESS
;
2258 return INET_QueryOption(hdr
, option
, buffer
, size
, unicode
);
2261 static DWORD
HTTPREQ_SetOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD size
)
2263 http_request_t
*req
= (http_request_t
*)hdr
;
2266 case 99: /* Undocumented, seems to be INTERNET_OPTION_SECURITY_FLAGS with argument validation */
2267 TRACE("Undocumented option 99\n");
2269 if (!buffer
|| size
!= sizeof(DWORD
))
2270 return ERROR_INVALID_PARAMETER
;
2271 if(*(DWORD
*)buffer
& ~SECURITY_SET_MASK
)
2272 return ERROR_INTERNET_OPTION_NOT_SETTABLE
;
2275 case INTERNET_OPTION_SECURITY_FLAGS
:
2279 if (!buffer
|| size
!= sizeof(DWORD
))
2280 return ERROR_INVALID_PARAMETER
;
2281 flags
= *(DWORD
*)buffer
;
2282 TRACE("INTERNET_OPTION_SECURITY_FLAGS %08x\n", flags
);
2283 flags
&= SECURITY_SET_MASK
;
2284 req
->security_flags
|= flags
;
2285 if(is_valid_netconn(req
->netconn
))
2286 req
->netconn
->security_flags
|= flags
;
2287 return ERROR_SUCCESS
;
2289 case INTERNET_OPTION_CONNECT_TIMEOUT
:
2290 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
2291 req
->connect_timeout
= *(DWORD
*)buffer
;
2292 return ERROR_SUCCESS
;
2294 case INTERNET_OPTION_SEND_TIMEOUT
:
2295 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
2296 req
->send_timeout
= *(DWORD
*)buffer
;
2297 return ERROR_SUCCESS
;
2299 case INTERNET_OPTION_RECEIVE_TIMEOUT
:
2300 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
2301 req
->receive_timeout
= *(DWORD
*)buffer
;
2302 return ERROR_SUCCESS
;
2304 case INTERNET_OPTION_USERNAME
:
2305 heap_free(req
->session
->userName
);
2306 if (!(req
->session
->userName
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
2307 return ERROR_SUCCESS
;
2309 case INTERNET_OPTION_PASSWORD
:
2310 heap_free(req
->session
->password
);
2311 if (!(req
->session
->password
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
2312 return ERROR_SUCCESS
;
2314 case INTERNET_OPTION_PROXY_USERNAME
:
2315 heap_free(req
->session
->appInfo
->proxyUsername
);
2316 if (!(req
->session
->appInfo
->proxyUsername
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
2317 return ERROR_SUCCESS
;
2319 case INTERNET_OPTION_PROXY_PASSWORD
:
2320 heap_free(req
->session
->appInfo
->proxyPassword
);
2321 if (!(req
->session
->appInfo
->proxyPassword
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
2322 return ERROR_SUCCESS
;
2324 case INTERNET_OPTION_HTTP_DECODING
:
2325 if(size
!= sizeof(BOOL
))
2326 return ERROR_INVALID_PARAMETER
;
2327 req
->decoding
= *(BOOL
*)buffer
;
2328 return ERROR_SUCCESS
;
2331 return INET_SetOption(hdr
, option
, buffer
, size
);
2334 static void commit_cache_entry(http_request_t
*req
)
2336 WCHAR url
[INTERNET_MAX_URL_LENGTH
];
2340 CloseHandle(req
->hCacheFile
);
2341 req
->hCacheFile
= NULL
;
2343 if(HTTP_GetRequestURL(req
, url
)) {
2348 header
= build_response_header(req
, TRUE
);
2349 header_len
= (header
? strlenW(header
) : 0);
2350 res
= CommitUrlCacheEntryW(url
, req
->req_file
->file_name
, req
->expires
,
2351 req
->last_modified
, NORMAL_CACHE_ENTRY
,
2352 header
, header_len
, NULL
, 0);
2354 req
->req_file
->is_committed
= TRUE
;
2356 WARN("CommitUrlCacheEntry failed: %u\n", GetLastError());
2361 static void create_cache_entry(http_request_t
*req
)
2363 static const WCHAR no_cacheW
[] = {'n','o','-','c','a','c','h','e',0};
2364 static const WCHAR no_storeW
[] = {'n','o','-','s','t','o','r','e',0};
2366 WCHAR url
[INTERNET_MAX_URL_LENGTH
];
2367 WCHAR file_name
[MAX_PATH
+1];
2370 /* FIXME: We should free previous cache file earlier */
2372 req_file_release(req
->req_file
);
2373 req
->req_file
= NULL
;
2375 if(req
->hCacheFile
) {
2376 CloseHandle(req
->hCacheFile
);
2377 req
->hCacheFile
= NULL
;
2380 if(req
->hdr
.dwFlags
& INTERNET_FLAG_NO_CACHE_WRITE
)
2384 int header_idx
= HTTP_GetCustomHeaderIndex(req
, szCache_Control
, 0, FALSE
);
2385 if(header_idx
!= -1) {
2388 for(ptr
=req
->custHeaders
[header_idx
].lpszValue
; *ptr
; ) {
2391 while(*ptr
==' ' || *ptr
=='\t')
2394 end
= strchrW(ptr
, ',');
2396 end
= ptr
+ strlenW(ptr
);
2398 if(!strncmpiW(ptr
, no_cacheW
, sizeof(no_cacheW
)/sizeof(*no_cacheW
)-1)
2399 || !strncmpiW(ptr
, no_storeW
, sizeof(no_storeW
)/sizeof(*no_storeW
)-1)) {
2412 if(!(req
->hdr
.dwFlags
& INTERNET_FLAG_NEED_FILE
))
2415 FIXME("INTERNET_FLAG_NEED_FILE is not supported correctly\n");
2418 b
= HTTP_GetRequestURL(req
, url
);
2420 WARN("Could not get URL\n");
2424 b
= CreateUrlCacheEntryW(url
, req
->contentLength
== ~0u ? 0 : req
->contentLength
, NULL
, file_name
, 0);
2426 WARN("Could not create cache entry: %08x\n", GetLastError());
2430 create_req_file(file_name
, &req
->req_file
);
2432 req
->hCacheFile
= CreateFileW(file_name
, GENERIC_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2433 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2434 if(req
->hCacheFile
== INVALID_HANDLE_VALUE
) {
2435 WARN("Could not create file: %u\n", GetLastError());
2436 req
->hCacheFile
= NULL
;
2440 if(req
->read_size
) {
2443 b
= WriteFile(req
->hCacheFile
, req
->read_buf
+req
->read_pos
, req
->read_size
, &written
, NULL
);
2445 FIXME("WriteFile failed: %u\n", GetLastError());
2447 if(req
->data_stream
->vtbl
->end_of_data(req
->data_stream
, req
))
2448 commit_cache_entry(req
);
2452 /* read some more data into the read buffer (the read section must be held) */
2453 static DWORD
read_more_data( http_request_t
*req
, int maxlen
)
2460 /* move existing data to the start of the buffer */
2462 memmove( req
->read_buf
, req
->read_buf
+ req
->read_pos
, req
->read_size
);
2466 if (maxlen
== -1) maxlen
= sizeof(req
->read_buf
);
2468 res
= NETCON_recv( req
->netconn
, req
->read_buf
+ req
->read_size
,
2469 maxlen
- req
->read_size
, BLOCKING_ALLOW
, &len
);
2470 if(res
== ERROR_SUCCESS
)
2471 req
->read_size
+= len
;
2476 /* remove some amount of data from the read buffer (the read section must be held) */
2477 static void remove_data( http_request_t
*req
, int count
)
2479 if (!(req
->read_size
-= count
)) req
->read_pos
= 0;
2480 else req
->read_pos
+= count
;
2483 static DWORD
read_line( http_request_t
*req
, LPSTR buffer
, DWORD
*len
)
2485 int count
, bytes_read
, pos
= 0;
2488 EnterCriticalSection( &req
->read_section
);
2491 BYTE
*eol
= memchr( req
->read_buf
+ req
->read_pos
, '\n', req
->read_size
);
2495 count
= eol
- (req
->read_buf
+ req
->read_pos
);
2496 bytes_read
= count
+ 1;
2498 else count
= bytes_read
= req
->read_size
;
2500 count
= min( count
, *len
- pos
);
2501 memcpy( buffer
+ pos
, req
->read_buf
+ req
->read_pos
, count
);
2503 remove_data( req
, bytes_read
);
2506 if ((res
= read_more_data( req
, -1 )))
2508 WARN( "read failed %u\n", res
);
2509 LeaveCriticalSection( &req
->read_section
);
2512 if (!req
->read_size
)
2515 TRACE( "returning empty string\n" );
2516 LeaveCriticalSection( &req
->read_section
);
2517 return ERROR_SUCCESS
;
2520 LeaveCriticalSection( &req
->read_section
);
2524 if (pos
&& buffer
[pos
- 1] == '\r') pos
--;
2527 buffer
[*len
- 1] = 0;
2528 TRACE( "returning %s\n", debugstr_a(buffer
));
2529 return ERROR_SUCCESS
;
2532 /* check if we have reached the end of the data to read (the read section must be held) */
2533 static BOOL
end_of_read_data( http_request_t
*req
)
2535 return !req
->read_size
&& req
->data_stream
->vtbl
->end_of_data(req
->data_stream
, req
);
2538 static DWORD
read_http_stream(http_request_t
*req
, BYTE
*buf
, DWORD size
, DWORD
*read
, blocking_mode_t blocking_mode
)
2542 res
= req
->data_stream
->vtbl
->read(req
->data_stream
, req
, buf
, size
, read
, blocking_mode
);
2543 assert(*read
<= size
);
2545 if(req
->hCacheFile
) {
2550 bres
= WriteFile(req
->hCacheFile
, buf
, *read
, &written
, NULL
);
2552 FIXME("WriteFile failed: %u\n", GetLastError());
2555 if(req
->data_stream
->vtbl
->end_of_data(req
->data_stream
, req
))
2556 commit_cache_entry(req
);
2562 /* fetch some more data into the read buffer (the read section must be held) */
2563 static DWORD
refill_read_buffer(http_request_t
*req
, blocking_mode_t blocking_mode
, DWORD
*read_bytes
)
2567 if(req
->read_size
== sizeof(req
->read_buf
))
2568 return ERROR_SUCCESS
;
2572 memmove(req
->read_buf
, req
->read_buf
+req
->read_pos
, req
->read_size
);
2576 res
= read_http_stream(req
, req
->read_buf
+req
->read_size
, sizeof(req
->read_buf
) - req
->read_size
,
2577 &read
, blocking_mode
);
2578 req
->read_size
+= read
;
2580 TRACE("read %u bytes, read_size %u\n", read
, req
->read_size
);
2586 /* return the size of data available to be read immediately (the read section must be held) */
2587 static DWORD
get_avail_data( http_request_t
*req
)
2589 return req
->read_size
+ req
->data_stream
->vtbl
->get_avail_data(req
->data_stream
, req
);
2592 static DWORD
netconn_get_avail_data(data_stream_t
*stream
, http_request_t
*req
)
2594 netconn_stream_t
*netconn_stream
= (netconn_stream_t
*)stream
;
2597 if(is_valid_netconn(req
->netconn
))
2598 NETCON_query_data_available(req
->netconn
, &avail
);
2599 return netconn_stream
->content_length
== ~0u
2601 : min(avail
, netconn_stream
->content_length
-netconn_stream
->content_read
);
2604 static BOOL
netconn_end_of_data(data_stream_t
*stream
, http_request_t
*req
)
2606 netconn_stream_t
*netconn_stream
= (netconn_stream_t
*)stream
;
2607 return netconn_stream
->content_read
== netconn_stream
->content_length
|| !is_valid_netconn(req
->netconn
);
2610 static DWORD
netconn_read(data_stream_t
*stream
, http_request_t
*req
, BYTE
*buf
, DWORD size
,
2611 DWORD
*read
, blocking_mode_t blocking_mode
)
2613 netconn_stream_t
*netconn_stream
= (netconn_stream_t
*)stream
;
2614 DWORD res
= ERROR_SUCCESS
;
2617 size
= min(size
, netconn_stream
->content_length
-netconn_stream
->content_read
);
2619 if(size
&& is_valid_netconn(req
->netconn
)) {
2620 if((res
= NETCON_recv(req
->netconn
, buf
, size
, blocking_mode
, &len
))) {
2622 if(blocking_mode
== BLOCKING_DISALLOW
&& res
== WSAEWOULDBLOCK
)
2623 res
= ERROR_SUCCESS
;
2625 netconn_stream
->content_length
= netconn_stream
->content_read
;
2627 netconn_stream
->content_length
= netconn_stream
->content_read
;
2631 netconn_stream
->content_read
+= *read
= len
;
2632 TRACE("read %u bytes\n", len
);
2636 static BOOL
netconn_drain_content(data_stream_t
*stream
, http_request_t
*req
)
2638 netconn_stream_t
*netconn_stream
= (netconn_stream_t
*)stream
;
2642 if(netconn_end_of_data(stream
, req
))
2646 if(NETCON_recv(req
->netconn
, buf
, sizeof(buf
), BLOCKING_DISALLOW
, &len
) != ERROR_SUCCESS
)
2649 netconn_stream
->content_read
+= len
;
2650 }while(netconn_stream
->content_read
< netconn_stream
->content_length
);
2655 static void netconn_destroy(data_stream_t
*stream
)
2659 static const data_stream_vtbl_t netconn_stream_vtbl
= {
2660 netconn_get_avail_data
,
2661 netconn_end_of_data
,
2663 netconn_drain_content
,
2667 /* read some more data into the read buffer (the read section must be held) */
2668 static DWORD
read_more_chunked_data(chunked_stream_t
*stream
, http_request_t
*req
, int maxlen
)
2673 assert(!stream
->end_of_data
);
2675 if (stream
->buf_pos
)
2677 /* move existing data to the start of the buffer */
2678 if(stream
->buf_size
)
2679 memmove(stream
->buf
, stream
->buf
+ stream
->buf_pos
, stream
->buf_size
);
2680 stream
->buf_pos
= 0;
2683 if (maxlen
== -1) maxlen
= sizeof(stream
->buf
);
2685 res
= NETCON_recv( req
->netconn
, stream
->buf
+ stream
->buf_size
,
2686 maxlen
- stream
->buf_size
, BLOCKING_ALLOW
, &len
);
2687 if(res
== ERROR_SUCCESS
)
2688 stream
->buf_size
+= len
;
2693 /* remove some amount of data from the read buffer (the read section must be held) */
2694 static void remove_chunked_data(chunked_stream_t
*stream
, int count
)
2696 if (!(stream
->buf_size
-= count
)) stream
->buf_pos
= 0;
2697 else stream
->buf_pos
+= count
;
2700 /* discard data contents until we reach end of line (the read section must be held) */
2701 static DWORD
discard_chunked_eol(chunked_stream_t
*stream
, http_request_t
*req
)
2707 BYTE
*eol
= memchr(stream
->buf
+ stream
->buf_pos
, '\n', stream
->buf_size
);
2710 remove_chunked_data(stream
, (eol
+ 1) - (stream
->buf
+ stream
->buf_pos
));
2713 stream
->buf_pos
= stream
->buf_size
= 0; /* discard everything */
2714 if ((res
= read_more_chunked_data(stream
, req
, -1)) != ERROR_SUCCESS
) return res
;
2715 } while (stream
->buf_size
);
2716 return ERROR_SUCCESS
;
2719 /* read the size of the next chunk (the read section must be held) */
2720 static DWORD
start_next_chunk(chunked_stream_t
*stream
, http_request_t
*req
)
2722 DWORD chunk_size
= 0, res
;
2724 assert(!stream
->chunk_size
|| stream
->chunk_size
== ~0u);
2726 if (stream
->end_of_data
) return ERROR_SUCCESS
;
2728 /* read terminator for the previous chunk */
2729 if(!stream
->chunk_size
&& (res
= discard_chunked_eol(stream
, req
)) != ERROR_SUCCESS
)
2734 while (stream
->buf_size
)
2736 char ch
= stream
->buf
[stream
->buf_pos
];
2737 if (ch
>= '0' && ch
<= '9') chunk_size
= chunk_size
* 16 + ch
- '0';
2738 else if (ch
>= 'a' && ch
<= 'f') chunk_size
= chunk_size
* 16 + ch
- 'a' + 10;
2739 else if (ch
>= 'A' && ch
<= 'F') chunk_size
= chunk_size
* 16 + ch
- 'A' + 10;
2740 else if (ch
== ';' || ch
== '\r' || ch
== '\n')
2742 TRACE( "reading %u byte chunk\n", chunk_size
);
2743 stream
->chunk_size
= chunk_size
;
2744 if (req
->contentLength
== ~0u) req
->contentLength
= chunk_size
;
2745 else req
->contentLength
+= chunk_size
;
2747 if (!chunk_size
) stream
->end_of_data
= TRUE
;
2748 return discard_chunked_eol(stream
, req
);
2750 remove_chunked_data(stream
, 1);
2752 if ((res
= read_more_chunked_data(stream
, req
, -1)) != ERROR_SUCCESS
) return res
;
2753 if (!stream
->buf_size
)
2755 stream
->chunk_size
= 0;
2756 return ERROR_SUCCESS
;
2761 static DWORD
chunked_get_avail_data(data_stream_t
*stream
, http_request_t
*req
)
2763 /* Allow reading only from read buffer */
2767 static BOOL
chunked_end_of_data(data_stream_t
*stream
, http_request_t
*req
)
2769 chunked_stream_t
*chunked_stream
= (chunked_stream_t
*)stream
;
2770 return chunked_stream
->end_of_data
;
2773 static DWORD
chunked_read(data_stream_t
*stream
, http_request_t
*req
, BYTE
*buf
, DWORD size
,
2774 DWORD
*read
, blocking_mode_t blocking_mode
)
2776 chunked_stream_t
*chunked_stream
= (chunked_stream_t
*)stream
;
2777 DWORD read_bytes
= 0, ret_read
= 0, res
= ERROR_SUCCESS
;
2779 if(!chunked_stream
->chunk_size
|| chunked_stream
->chunk_size
== ~0u) {
2780 res
= start_next_chunk(chunked_stream
, req
);
2781 if(res
!= ERROR_SUCCESS
)
2785 while(size
&& chunked_stream
->chunk_size
&& !chunked_stream
->end_of_data
) {
2786 if(chunked_stream
->buf_size
) {
2787 read_bytes
= min(size
, min(chunked_stream
->buf_size
, chunked_stream
->chunk_size
));
2789 /* this could block */
2790 if(blocking_mode
== BLOCKING_DISALLOW
&& read_bytes
== chunked_stream
->chunk_size
)
2793 memcpy(buf
+ret_read
, chunked_stream
->buf
+chunked_stream
->buf_pos
, read_bytes
);
2794 remove_chunked_data(chunked_stream
, read_bytes
);
2796 read_bytes
= min(size
, chunked_stream
->chunk_size
);
2798 if(blocking_mode
== BLOCKING_DISALLOW
) {
2801 if(!is_valid_netconn(req
->netconn
) || !NETCON_query_data_available(req
->netconn
, &avail
) || !avail
)
2803 if(read_bytes
> avail
)
2806 /* this could block */
2807 if(read_bytes
== chunked_stream
->chunk_size
)
2811 res
= NETCON_recv(req
->netconn
, (char *)buf
+ret_read
, read_bytes
, BLOCKING_ALLOW
, (int*)&read_bytes
);
2812 if(res
!= ERROR_SUCCESS
)
2816 chunked_stream
->chunk_size
-= read_bytes
;
2818 ret_read
+= read_bytes
;
2819 if(size
&& !chunked_stream
->chunk_size
) {
2820 assert(blocking_mode
!= BLOCKING_DISALLOW
);
2821 res
= start_next_chunk(chunked_stream
, req
);
2822 if(res
!= ERROR_SUCCESS
)
2826 if(blocking_mode
== BLOCKING_ALLOW
)
2827 blocking_mode
= BLOCKING_DISALLOW
;
2830 TRACE("read %u bytes\n", ret_read
);
2835 static BOOL
chunked_drain_content(data_stream_t
*stream
, http_request_t
*req
)
2837 chunked_stream_t
*chunked_stream
= (chunked_stream_t
*)stream
;
2839 remove_chunked_data(chunked_stream
, chunked_stream
->buf_size
);
2840 return chunked_stream
->end_of_data
;
2843 static void chunked_destroy(data_stream_t
*stream
)
2845 chunked_stream_t
*chunked_stream
= (chunked_stream_t
*)stream
;
2846 heap_free(chunked_stream
);
2849 static const data_stream_vtbl_t chunked_stream_vtbl
= {
2850 chunked_get_avail_data
,
2851 chunked_end_of_data
,
2853 chunked_drain_content
,
2857 /* set the request content length based on the headers */
2858 static DWORD
set_content_length(http_request_t
*request
)
2860 static const WCHAR szChunked
[] = {'c','h','u','n','k','e','d',0};
2864 if(request
->status_code
== HTTP_STATUS_NO_CONTENT
) {
2865 request
->contentLength
= request
->netconn_stream
.content_length
= 0;
2866 return ERROR_SUCCESS
;
2869 size
= sizeof(request
->contentLength
);
2870 if (HTTP_HttpQueryInfoW(request
, HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_CONTENT_LENGTH
,
2871 &request
->contentLength
, &size
, NULL
) != ERROR_SUCCESS
)
2872 request
->contentLength
= ~0u;
2873 request
->netconn_stream
.content_length
= request
->contentLength
;
2874 request
->netconn_stream
.content_read
= request
->read_size
;
2876 size
= sizeof(encoding
);
2877 if (HTTP_HttpQueryInfoW(request
, HTTP_QUERY_TRANSFER_ENCODING
, encoding
, &size
, NULL
) == ERROR_SUCCESS
&&
2878 !strcmpiW(encoding
, szChunked
))
2880 chunked_stream_t
*chunked_stream
;
2882 chunked_stream
= heap_alloc(sizeof(*chunked_stream
));
2884 return ERROR_OUTOFMEMORY
;
2886 chunked_stream
->data_stream
.vtbl
= &chunked_stream_vtbl
;
2887 chunked_stream
->buf_size
= chunked_stream
->buf_pos
= 0;
2888 chunked_stream
->chunk_size
= ~0u;
2889 chunked_stream
->end_of_data
= FALSE
;
2891 if(request
->read_size
) {
2892 memcpy(chunked_stream
->buf
, request
->read_buf
+request
->read_pos
, request
->read_size
);
2893 chunked_stream
->buf_size
= request
->read_size
;
2894 request
->read_size
= request
->read_pos
= 0;
2897 request
->data_stream
= &chunked_stream
->data_stream
;
2898 request
->contentLength
= ~0u;
2899 request
->read_chunked
= TRUE
;
2902 if(request
->decoding
) {
2905 static const WCHAR deflateW
[] = {'d','e','f','l','a','t','e',0};
2906 static const WCHAR gzipW
[] = {'g','z','i','p',0};
2908 encoding_idx
= HTTP_GetCustomHeaderIndex(request
, szContent_Encoding
, 0, FALSE
);
2909 if(encoding_idx
!= -1) {
2910 if(!strcmpiW(request
->custHeaders
[encoding_idx
].lpszValue
, gzipW
)) {
2911 HTTP_DeleteCustomHeader(request
, encoding_idx
);
2912 return init_gzip_stream(request
, TRUE
);
2914 if(!strcmpiW(request
->custHeaders
[encoding_idx
].lpszValue
, deflateW
)) {
2915 HTTP_DeleteCustomHeader(request
, encoding_idx
);
2916 return init_gzip_stream(request
, FALSE
);
2921 return ERROR_SUCCESS
;
2924 static void send_request_complete(http_request_t
*req
, DWORD_PTR result
, DWORD error
)
2926 INTERNET_ASYNC_RESULT iar
;
2928 iar
.dwResult
= result
;
2929 iar
.dwError
= error
;
2931 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
2932 sizeof(INTERNET_ASYNC_RESULT
));
2935 static void HTTP_ReceiveRequestData(http_request_t
*req
, BOOL first_notif
, DWORD
*ret_size
)
2937 DWORD res
, read
= 0, avail
= 0;
2938 blocking_mode_t mode
;
2942 EnterCriticalSection( &req
->read_section
);
2944 mode
= first_notif
&& req
->read_size
? BLOCKING_DISALLOW
: BLOCKING_ALLOW
;
2945 res
= refill_read_buffer(req
, mode
, &read
);
2946 if(res
== ERROR_SUCCESS
)
2947 avail
= get_avail_data(req
);
2949 LeaveCriticalSection( &req
->read_section
);
2951 if(res
!= ERROR_SUCCESS
|| (mode
!= BLOCKING_DISALLOW
&& !read
)) {
2952 WARN("res %u read %u, closing connection\n", res
, read
);
2953 http_release_netconn(req
, FALSE
);
2956 if(res
!= ERROR_SUCCESS
) {
2957 send_request_complete(req
, 0, res
);
2966 send_request_complete(req
, req
->session
->hdr
.dwInternalFlags
& INET_OPENURL
? (DWORD_PTR
)req
->hdr
.hInternet
: 1, avail
);
2969 /* read data from the http connection (the read section must be held) */
2970 static DWORD
HTTPREQ_Read(http_request_t
*req
, void *buffer
, DWORD size
, DWORD
*read
, BOOL sync
)
2972 DWORD current_read
= 0, ret_read
= 0;
2973 blocking_mode_t blocking_mode
;
2974 DWORD res
= ERROR_SUCCESS
;
2976 blocking_mode
= req
->session
->appInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
? BLOCKING_ALLOW
: BLOCKING_WAITALL
;
2978 EnterCriticalSection( &req
->read_section
);
2980 if(req
->read_size
) {
2981 ret_read
= min(size
, req
->read_size
);
2982 memcpy(buffer
, req
->read_buf
+req
->read_pos
, ret_read
);
2983 req
->read_size
-= ret_read
;
2984 req
->read_pos
+= ret_read
;
2985 if(blocking_mode
== BLOCKING_ALLOW
)
2986 blocking_mode
= BLOCKING_DISALLOW
;
2989 if(ret_read
< size
) {
2990 res
= read_http_stream(req
, (BYTE
*)buffer
+ret_read
, size
-ret_read
, ¤t_read
, blocking_mode
);
2991 ret_read
+= current_read
;
2994 LeaveCriticalSection( &req
->read_section
);
2997 TRACE( "retrieved %u bytes (%u)\n", ret_read
, req
->contentLength
);
2999 if(size
&& !ret_read
)
3000 http_release_netconn(req
, res
== ERROR_SUCCESS
);
3005 static BOOL
drain_content(http_request_t
*req
, BOOL blocking
)
3009 if(!is_valid_netconn(req
->netconn
) || req
->contentLength
== -1)
3012 if(!strcmpW(req
->verb
, szHEAD
))
3016 return req
->data_stream
->vtbl
->drain_content(req
->data_stream
, req
);
3018 EnterCriticalSection( &req
->read_section
);
3021 DWORD bytes_read
, res
;
3024 res
= HTTPREQ_Read(req
, buf
, sizeof(buf
), &bytes_read
, TRUE
);
3025 if(res
!= ERROR_SUCCESS
) {
3035 LeaveCriticalSection( &req
->read_section
);
3039 static DWORD
HTTPREQ_ReadFile(object_header_t
*hdr
, void *buffer
, DWORD size
, DWORD
*read
)
3041 http_request_t
*req
= (http_request_t
*)hdr
;
3044 EnterCriticalSection( &req
->read_section
);
3045 if(hdr
->dwError
== INTERNET_HANDLE_IN_USE
)
3046 hdr
->dwError
= ERROR_INTERNET_INTERNAL_ERROR
;
3048 res
= HTTPREQ_Read(req
, buffer
, size
, read
, TRUE
);
3049 if(res
== ERROR_SUCCESS
)
3051 LeaveCriticalSection( &req
->read_section
);
3061 } read_file_ex_task_t
;
3063 static void AsyncReadFileExProc(task_header_t
*hdr
)
3065 read_file_ex_task_t
*task
= (read_file_ex_task_t
*)hdr
;
3066 http_request_t
*req
= (http_request_t
*)task
->hdr
.hdr
;
3069 TRACE("INTERNETREADFILEEXW %p\n", task
->hdr
.hdr
);
3071 res
= HTTPREQ_Read(req
, task
->buf
, task
->size
, task
->ret_read
, TRUE
);
3072 send_request_complete(req
, res
== ERROR_SUCCESS
, res
);
3075 static DWORD
HTTPREQ_ReadFileEx(object_header_t
*hdr
, void *buf
, DWORD size
, DWORD
*ret_read
,
3076 DWORD flags
, DWORD_PTR context
)
3079 http_request_t
*req
= (http_request_t
*)hdr
;
3080 DWORD res
, read
, cread
, error
= ERROR_SUCCESS
;
3082 if (flags
& ~(IRF_ASYNC
|IRF_NO_WAIT
))
3083 FIXME("these dwFlags aren't implemented: 0x%x\n", flags
& ~(IRF_ASYNC
|IRF_NO_WAIT
));
3085 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
3087 if (hdr
->dwFlags
& INTERNET_FLAG_ASYNC
)
3089 read_file_ex_task_t
*task
;
3091 if (TryEnterCriticalSection( &req
->read_section
))
3093 if (get_avail_data(req
))
3095 res
= HTTPREQ_Read(req
, buf
, size
, &read
, FALSE
);
3096 LeaveCriticalSection( &req
->read_section
);
3099 LeaveCriticalSection( &req
->read_section
);
3102 task
= alloc_async_task(&req
->hdr
, AsyncReadFileExProc
, sizeof(*task
));
3105 task
->ret_read
= ret_read
;
3107 INTERNET_AsyncCall(&task
->hdr
);
3109 return ERROR_IO_PENDING
;
3114 EnterCriticalSection( &req
->read_section
);
3115 if(hdr
->dwError
== ERROR_SUCCESS
)
3116 hdr
->dwError
= INTERNET_HANDLE_IN_USE
;
3117 else if(hdr
->dwError
== INTERNET_HANDLE_IN_USE
)
3118 hdr
->dwError
= ERROR_INTERNET_INTERNAL_ERROR
;
3121 res
= HTTPREQ_Read(req
, (char*)buf
+read
, size
-read
, &cread
, !(flags
& IRF_NO_WAIT
));
3122 if(res
!= ERROR_SUCCESS
)
3126 if(read
== size
|| end_of_read_data(req
))
3129 LeaveCriticalSection( &req
->read_section
);
3131 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
,
3132 &cread
, sizeof(cread
));
3133 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
,
3134 INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
3136 EnterCriticalSection( &req
->read_section
);
3139 if(hdr
->dwError
== INTERNET_HANDLE_IN_USE
)
3140 hdr
->dwError
= ERROR_SUCCESS
;
3142 error
= hdr
->dwError
;
3144 LeaveCriticalSection( &req
->read_section
);
3148 if (res
== ERROR_SUCCESS
) {
3149 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
,
3150 &read
, sizeof(read
));
3153 return res
==ERROR_SUCCESS
? error
: res
;
3156 static DWORD
HTTPREQ_WriteFile(object_header_t
*hdr
, const void *buffer
, DWORD size
, DWORD
*written
)
3159 http_request_t
*request
= (http_request_t
*)hdr
;
3161 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
3164 res
= NETCON_send(request
->netconn
, buffer
, size
, 0, (LPINT
)written
);
3165 if (res
== ERROR_SUCCESS
)
3166 request
->bytesWritten
+= *written
;
3168 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_SENT
, written
, sizeof(DWORD
));
3175 } http_data_available_task_t
;
3177 static void AsyncQueryDataAvailableProc(task_header_t
*hdr
)
3179 http_data_available_task_t
*task
= (http_data_available_task_t
*)hdr
;
3181 HTTP_ReceiveRequestData((http_request_t
*)task
->hdr
.hdr
, FALSE
, task
->ret_size
);
3184 static DWORD
HTTPREQ_QueryDataAvailable(object_header_t
*hdr
, DWORD
*available
, DWORD flags
, DWORD_PTR ctx
)
3186 http_request_t
*req
= (http_request_t
*)hdr
;
3188 TRACE("(%p %p %x %lx)\n", req
, available
, flags
, ctx
);
3190 if (req
->session
->appInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
3192 http_data_available_task_t
*task
;
3194 /* never wait, if we can't enter the section we queue an async request right away */
3195 if (TryEnterCriticalSection( &req
->read_section
))
3197 refill_read_buffer(req
, BLOCKING_DISALLOW
, NULL
);
3198 if ((*available
= get_avail_data( req
))) goto done
;
3199 if (end_of_read_data( req
)) goto done
;
3200 LeaveCriticalSection( &req
->read_section
);
3203 task
= alloc_async_task(&req
->hdr
, AsyncQueryDataAvailableProc
, sizeof(*task
));
3204 task
->ret_size
= available
;
3205 INTERNET_AsyncCall(&task
->hdr
);
3206 return ERROR_IO_PENDING
;
3209 EnterCriticalSection( &req
->read_section
);
3211 if (!(*available
= get_avail_data( req
)) && !end_of_read_data( req
))
3213 refill_read_buffer( req
, BLOCKING_ALLOW
, NULL
);
3214 *available
= get_avail_data( req
);
3218 LeaveCriticalSection( &req
->read_section
);
3220 TRACE( "returning %u\n", *available
);
3221 return ERROR_SUCCESS
;
3224 static DWORD
HTTPREQ_LockRequestFile(object_header_t
*hdr
, req_file_t
**ret
)
3226 http_request_t
*req
= (http_request_t
*)hdr
;
3228 TRACE("(%p)\n", req
);
3230 if(!req
->req_file
) {
3231 WARN("No cache file name available\n");
3232 return ERROR_FILE_NOT_FOUND
;
3235 *ret
= req_file_addref(req
->req_file
);
3236 return ERROR_SUCCESS
;
3239 static const object_vtbl_t HTTPREQVtbl
= {
3241 HTTPREQ_CloseConnection
,
3242 HTTPREQ_QueryOption
,
3247 HTTPREQ_QueryDataAvailable
,
3249 HTTPREQ_LockRequestFile
3252 /***********************************************************************
3253 * HTTP_HttpOpenRequestW (internal)
3255 * Open a HTTP request handle
3258 * HINTERNET a HTTP request handle on success
3262 static DWORD
HTTP_HttpOpenRequestW(http_session_t
*session
,
3263 LPCWSTR lpszVerb
, LPCWSTR lpszObjectName
, LPCWSTR lpszVersion
,
3264 LPCWSTR lpszReferrer
, LPCWSTR
*lpszAcceptTypes
,
3265 DWORD dwFlags
, DWORD_PTR dwContext
, HINTERNET
*ret
)
3267 appinfo_t
*hIC
= session
->appInfo
;
3268 http_request_t
*request
;
3273 request
= alloc_object(&session
->hdr
, &HTTPREQVtbl
, sizeof(http_request_t
));
3275 return ERROR_OUTOFMEMORY
;
3277 request
->hdr
.htype
= WH_HHTTPREQ
;
3278 request
->hdr
.dwFlags
= dwFlags
;
3279 request
->hdr
.dwContext
= dwContext
;
3280 request
->contentLength
= ~0u;
3282 request
->netconn_stream
.data_stream
.vtbl
= &netconn_stream_vtbl
;
3283 request
->data_stream
= &request
->netconn_stream
.data_stream
;
3284 request
->connect_timeout
= session
->connect_timeout
;
3285 request
->send_timeout
= session
->send_timeout
;
3286 request
->receive_timeout
= session
->receive_timeout
;
3288 InitializeCriticalSection( &request
->read_section
);
3289 request
->read_section
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": http_request_t.read_section");
3291 WININET_AddRef( &session
->hdr
);
3292 request
->session
= session
;
3293 list_add_head( &session
->hdr
.children
, &request
->hdr
.entry
);
3295 request
->server
= get_server(session
->hostName
, session
->hostPort
, (dwFlags
& INTERNET_FLAG_SECURE
) != 0, TRUE
);
3296 if(!request
->server
) {
3297 WININET_Release(&request
->hdr
);
3298 return ERROR_OUTOFMEMORY
;
3301 if (dwFlags
& INTERNET_FLAG_IGNORE_CERT_CN_INVALID
)
3302 request
->security_flags
|= SECURITY_FLAG_IGNORE_CERT_CN_INVALID
;
3303 if (dwFlags
& INTERNET_FLAG_IGNORE_CERT_DATE_INVALID
)
3304 request
->security_flags
|= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
;
3306 if (lpszObjectName
&& *lpszObjectName
) {
3310 rc
= UrlEscapeW(lpszObjectName
, NULL
, &len
, URL_ESCAPE_SPACES_ONLY
);
3311 if (rc
!= E_POINTER
)
3312 len
= strlenW(lpszObjectName
)+1;
3313 request
->path
= heap_alloc(len
*sizeof(WCHAR
));
3314 rc
= UrlEscapeW(lpszObjectName
, request
->path
, &len
,
3315 URL_ESCAPE_SPACES_ONLY
);
3318 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName
),rc
);
3319 strcpyW(request
->path
,lpszObjectName
);
3322 static const WCHAR slashW
[] = {'/',0};
3324 request
->path
= heap_strdupW(slashW
);
3327 if (lpszReferrer
&& *lpszReferrer
)
3328 HTTP_ProcessHeader(request
, HTTP_REFERER
, lpszReferrer
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REQ
);
3330 if (lpszAcceptTypes
)
3333 for (i
= 0; lpszAcceptTypes
[i
]; i
++)
3335 if (!*lpszAcceptTypes
[i
]) continue;
3336 HTTP_ProcessHeader(request
, HTTP_ACCEPT
, lpszAcceptTypes
[i
],
3337 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
|
3338 HTTP_ADDHDR_FLAG_REQ
|
3339 (i
== 0 ? HTTP_ADDHDR_FLAG_REPLACE
: 0));
3343 request
->verb
= heap_strdupW(lpszVerb
&& *lpszVerb
? lpszVerb
: szGET
);
3344 request
->version
= heap_strdupW(lpszVersion
&& *lpszVersion
? lpszVersion
: g_szHttp1_1
);
3346 HTTP_ProcessHeader(request
, hostW
, request
->server
->canon_host_port
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REQ
);
3348 if (session
->hostPort
== INTERNET_INVALID_PORT_NUMBER
)
3349 session
->hostPort
= INTERNET_DEFAULT_HTTP_PORT
;
3351 if (hIC
->proxy
&& hIC
->proxy
[0] && !HTTP_ShouldBypassProxy(hIC
, session
->hostName
))
3352 HTTP_DealWithProxy( hIC
, session
, request
);
3354 INTERNET_SendCallback(&session
->hdr
, dwContext
,
3355 INTERNET_STATUS_HANDLE_CREATED
, &request
->hdr
.hInternet
,
3358 TRACE("<-- (%p)\n", request
);
3360 *ret
= request
->hdr
.hInternet
;
3361 return ERROR_SUCCESS
;
3364 /***********************************************************************
3365 * HttpOpenRequestW (WININET.@)
3367 * Open a HTTP request handle
3370 * HINTERNET a HTTP request handle on success
3374 HINTERNET WINAPI
HttpOpenRequestW(HINTERNET hHttpSession
,
3375 LPCWSTR lpszVerb
, LPCWSTR lpszObjectName
, LPCWSTR lpszVersion
,
3376 LPCWSTR lpszReferrer
, LPCWSTR
*lpszAcceptTypes
,
3377 DWORD dwFlags
, DWORD_PTR dwContext
)
3379 http_session_t
*session
;
3380 HINTERNET handle
= NULL
;
3383 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession
,
3384 debugstr_w(lpszVerb
), debugstr_w(lpszObjectName
),
3385 debugstr_w(lpszVersion
), debugstr_w(lpszReferrer
), lpszAcceptTypes
,
3386 dwFlags
, dwContext
);
3387 if(lpszAcceptTypes
!=NULL
)
3390 for(i
=0;lpszAcceptTypes
[i
]!=NULL
;i
++)
3391 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes
[i
]));
3394 session
= (http_session_t
*) get_handle_object( hHttpSession
);
3395 if (NULL
== session
|| session
->hdr
.htype
!= WH_HHTTPSESSION
)
3397 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
3402 * My tests seem to show that the windows version does not
3403 * become asynchronous until after this point. And anyhow
3404 * if this call was asynchronous then how would you get the
3405 * necessary HINTERNET pointer returned by this function.
3408 res
= HTTP_HttpOpenRequestW(session
, lpszVerb
, lpszObjectName
,
3409 lpszVersion
, lpszReferrer
, lpszAcceptTypes
,
3410 dwFlags
, dwContext
, &handle
);
3413 WININET_Release( &session
->hdr
);
3414 TRACE("returning %p\n", handle
);
3415 if(res
!= ERROR_SUCCESS
)
3420 static const LPCWSTR header_lookup
[] = {
3421 szMime_Version
, /* HTTP_QUERY_MIME_VERSION = 0 */
3422 szContent_Type
, /* HTTP_QUERY_CONTENT_TYPE = 1 */
3423 szContent_Transfer_Encoding
,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
3424 szContent_ID
, /* HTTP_QUERY_CONTENT_ID = 3 */
3425 NULL
, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
3426 szContent_Length
, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
3427 szContent_Language
, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
3428 szAllow
, /* HTTP_QUERY_ALLOW = 7 */
3429 szPublic
, /* HTTP_QUERY_PUBLIC = 8 */
3430 szDate
, /* HTTP_QUERY_DATE = 9 */
3431 szExpires
, /* HTTP_QUERY_EXPIRES = 10 */
3432 szLast_Modified
, /* HTTP_QUERY_LAST_MODIFIED = 11 */
3433 NULL
, /* HTTP_QUERY_MESSAGE_ID = 12 */
3434 szURI
, /* HTTP_QUERY_URI = 13 */
3435 szFrom
, /* HTTP_QUERY_DERIVED_FROM = 14 */
3436 NULL
, /* HTTP_QUERY_COST = 15 */
3437 NULL
, /* HTTP_QUERY_LINK = 16 */
3438 szPragma
, /* HTTP_QUERY_PRAGMA = 17 */
3439 NULL
, /* HTTP_QUERY_VERSION = 18 */
3440 szStatus
, /* HTTP_QUERY_STATUS_CODE = 19 */
3441 NULL
, /* HTTP_QUERY_STATUS_TEXT = 20 */
3442 NULL
, /* HTTP_QUERY_RAW_HEADERS = 21 */
3443 NULL
, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
3444 szConnection
, /* HTTP_QUERY_CONNECTION = 23 */
3445 szAccept
, /* HTTP_QUERY_ACCEPT = 24 */
3446 szAccept_Charset
, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
3447 szAccept_Encoding
, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
3448 szAccept_Language
, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
3449 szAuthorization
, /* HTTP_QUERY_AUTHORIZATION = 28 */
3450 szContent_Encoding
, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
3451 NULL
, /* HTTP_QUERY_FORWARDED = 30 */
3452 NULL
, /* HTTP_QUERY_FROM = 31 */
3453 szIf_Modified_Since
, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
3454 szLocation
, /* HTTP_QUERY_LOCATION = 33 */
3455 NULL
, /* HTTP_QUERY_ORIG_URI = 34 */
3456 szReferer
, /* HTTP_QUERY_REFERER = 35 */
3457 szRetry_After
, /* HTTP_QUERY_RETRY_AFTER = 36 */
3458 szServer
, /* HTTP_QUERY_SERVER = 37 */
3459 NULL
, /* HTTP_TITLE = 38 */
3460 szUser_Agent
, /* HTTP_QUERY_USER_AGENT = 39 */
3461 szWWW_Authenticate
, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
3462 szProxy_Authenticate
, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
3463 szAccept_Ranges
, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
3464 szSet_Cookie
, /* HTTP_QUERY_SET_COOKIE = 43 */
3465 szCookie
, /* HTTP_QUERY_COOKIE = 44 */
3466 NULL
, /* HTTP_QUERY_REQUEST_METHOD = 45 */
3467 NULL
, /* HTTP_QUERY_REFRESH = 46 */
3468 szContent_Disposition
, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
3469 szAge
, /* HTTP_QUERY_AGE = 48 */
3470 szCache_Control
, /* HTTP_QUERY_CACHE_CONTROL = 49 */
3471 szContent_Base
, /* HTTP_QUERY_CONTENT_BASE = 50 */
3472 szContent_Location
, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
3473 szContent_MD5
, /* HTTP_QUERY_CONTENT_MD5 = 52 */
3474 szContent_Range
, /* HTTP_QUERY_CONTENT_RANGE = 53 */
3475 szETag
, /* HTTP_QUERY_ETAG = 54 */
3476 hostW
, /* HTTP_QUERY_HOST = 55 */
3477 szIf_Match
, /* HTTP_QUERY_IF_MATCH = 56 */
3478 szIf_None_Match
, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
3479 szIf_Range
, /* HTTP_QUERY_IF_RANGE = 58 */
3480 szIf_Unmodified_Since
, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
3481 szMax_Forwards
, /* HTTP_QUERY_MAX_FORWARDS = 60 */
3482 szProxy_Authorization
, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
3483 szRange
, /* HTTP_QUERY_RANGE = 62 */
3484 szTransfer_Encoding
, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
3485 szUpgrade
, /* HTTP_QUERY_UPGRADE = 64 */
3486 szVary
, /* HTTP_QUERY_VARY = 65 */
3487 szVia
, /* HTTP_QUERY_VIA = 66 */
3488 szWarning
, /* HTTP_QUERY_WARNING = 67 */
3489 szExpect
, /* HTTP_QUERY_EXPECT = 68 */
3490 szProxy_Connection
, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
3491 szUnless_Modified_Since
, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
3494 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
3496 /***********************************************************************
3497 * HTTP_HttpQueryInfoW (internal)
3499 static DWORD
HTTP_HttpQueryInfoW(http_request_t
*request
, DWORD dwInfoLevel
,
3500 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
3502 LPHTTPHEADERW lphttpHdr
= NULL
;
3503 BOOL request_only
= dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
;
3504 INT requested_index
= lpdwIndex
? *lpdwIndex
: 0;
3505 DWORD level
= (dwInfoLevel
& ~HTTP_QUERY_MODIFIER_FLAGS_MASK
);
3508 /* Find requested header structure */
3511 case HTTP_QUERY_CUSTOM
:
3512 if (!lpBuffer
) return ERROR_INVALID_PARAMETER
;
3513 index
= HTTP_GetCustomHeaderIndex(request
, lpBuffer
, requested_index
, request_only
);
3515 case HTTP_QUERY_RAW_HEADERS_CRLF
:
3519 DWORD res
= ERROR_INVALID_PARAMETER
;
3522 headers
= build_request_header(request
, request
->verb
, request
->path
, request
->version
, TRUE
);
3524 headers
= build_response_header(request
, TRUE
);
3526 return ERROR_OUTOFMEMORY
;
3528 len
= strlenW(headers
) * sizeof(WCHAR
);
3529 if (len
+ sizeof(WCHAR
) > *lpdwBufferLength
)
3531 len
+= sizeof(WCHAR
);
3532 res
= ERROR_INSUFFICIENT_BUFFER
;
3536 memcpy(lpBuffer
, headers
, len
+ sizeof(WCHAR
));
3537 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
/ sizeof(WCHAR
)));
3538 res
= ERROR_SUCCESS
;
3540 *lpdwBufferLength
= len
;
3545 case HTTP_QUERY_RAW_HEADERS
:
3551 headers
= build_request_header(request
, request
->verb
, request
->path
, request
->version
, FALSE
);
3553 headers
= build_response_header(request
, FALSE
);
3555 return ERROR_OUTOFMEMORY
;
3557 len
= strlenW(headers
) * sizeof(WCHAR
);
3558 if (len
> *lpdwBufferLength
)
3560 *lpdwBufferLength
= len
;
3562 return ERROR_INSUFFICIENT_BUFFER
;
3569 TRACE("returning data: %s\n", debugstr_wn(headers
, len
/ sizeof(WCHAR
)));
3571 for (i
= 0; i
< len
/ sizeof(WCHAR
); i
++)
3573 if (headers
[i
] == '\n')
3576 memcpy(lpBuffer
, headers
, len
);
3578 *lpdwBufferLength
= len
- sizeof(WCHAR
);
3581 return ERROR_SUCCESS
;
3583 case HTTP_QUERY_STATUS_TEXT
:
3584 if (request
->statusText
)
3586 DWORD len
= strlenW(request
->statusText
);
3587 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
3589 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
3590 return ERROR_INSUFFICIENT_BUFFER
;
3594 memcpy(lpBuffer
, request
->statusText
, (len
+ 1) * sizeof(WCHAR
));
3595 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
));
3597 *lpdwBufferLength
= len
* sizeof(WCHAR
);
3598 return ERROR_SUCCESS
;
3601 case HTTP_QUERY_VERSION
:
3602 if (request
->version
)
3604 DWORD len
= strlenW(request
->version
);
3605 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
3607 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
3608 return ERROR_INSUFFICIENT_BUFFER
;
3612 memcpy(lpBuffer
, request
->version
, (len
+ 1) * sizeof(WCHAR
));
3613 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
));
3615 *lpdwBufferLength
= len
* sizeof(WCHAR
);
3616 return ERROR_SUCCESS
;
3619 case HTTP_QUERY_CONTENT_ENCODING
:
3620 index
= HTTP_GetCustomHeaderIndex(request
, header_lookup
[request
->read_gzip
? HTTP_QUERY_CONTENT_TYPE
: level
],
3621 requested_index
,request_only
);
3623 case HTTP_QUERY_STATUS_CODE
: {
3624 DWORD res
= ERROR_SUCCESS
;
3627 return ERROR_HTTP_INVALID_QUERY_REQUEST
;
3632 if(dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
) {
3633 if(*lpdwBufferLength
>= sizeof(DWORD
))
3634 *(DWORD
*)lpBuffer
= request
->status_code
;
3636 res
= ERROR_INSUFFICIENT_BUFFER
;
3637 *lpdwBufferLength
= sizeof(DWORD
);
3641 static const WCHAR formatW
[] = {'%','u',0};
3643 size
= sprintfW(buf
, formatW
, request
->status_code
) * sizeof(WCHAR
);
3645 if(size
<= *lpdwBufferLength
) {
3646 memcpy(lpBuffer
, buf
, size
+sizeof(WCHAR
));
3648 size
+= sizeof(WCHAR
);
3649 res
= ERROR_INSUFFICIENT_BUFFER
;
3652 *lpdwBufferLength
= size
;
3657 assert (LAST_TABLE_HEADER
== (HTTP_QUERY_UNLESS_MODIFIED_SINCE
+ 1));
3659 if (level
< LAST_TABLE_HEADER
&& header_lookup
[level
])
3660 index
= HTTP_GetCustomHeaderIndex(request
, header_lookup
[level
],
3661 requested_index
,request_only
);
3665 lphttpHdr
= &request
->custHeaders
[index
];
3667 /* Ensure header satisfies requested attributes */
3669 ((dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
) &&
3670 (~lphttpHdr
->wFlags
& HDR_ISREQUEST
)))
3672 return ERROR_HTTP_HEADER_NOT_FOUND
;
3675 if (lpdwIndex
) (*lpdwIndex
)++;
3677 /* coalesce value to requested type */
3678 if (dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
&& lpBuffer
)
3680 *(int *)lpBuffer
= atoiW(lphttpHdr
->lpszValue
);
3681 TRACE(" returning number: %d\n", *(int *)lpBuffer
);
3683 else if (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
&& lpBuffer
)
3689 tmpTime
= ConvertTimeString(lphttpHdr
->lpszValue
);
3691 tmpTM
= *gmtime(&tmpTime
);
3692 STHook
= (SYSTEMTIME
*)lpBuffer
;
3693 STHook
->wDay
= tmpTM
.tm_mday
;
3694 STHook
->wHour
= tmpTM
.tm_hour
;
3695 STHook
->wMilliseconds
= 0;
3696 STHook
->wMinute
= tmpTM
.tm_min
;
3697 STHook
->wDayOfWeek
= tmpTM
.tm_wday
;
3698 STHook
->wMonth
= tmpTM
.tm_mon
+ 1;
3699 STHook
->wSecond
= tmpTM
.tm_sec
;
3700 STHook
->wYear
= 1900+tmpTM
.tm_year
;
3702 TRACE(" returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
3703 STHook
->wYear
, STHook
->wMonth
, STHook
->wDay
, STHook
->wDayOfWeek
,
3704 STHook
->wHour
, STHook
->wMinute
, STHook
->wSecond
, STHook
->wMilliseconds
);
3706 else if (lphttpHdr
->lpszValue
)
3708 DWORD len
= (strlenW(lphttpHdr
->lpszValue
) + 1) * sizeof(WCHAR
);
3710 if (len
> *lpdwBufferLength
)
3712 *lpdwBufferLength
= len
;
3713 return ERROR_INSUFFICIENT_BUFFER
;
3717 memcpy(lpBuffer
, lphttpHdr
->lpszValue
, len
);
3718 TRACE("! returning string: %s\n", debugstr_w(lpBuffer
));
3720 *lpdwBufferLength
= len
- sizeof(WCHAR
);
3722 return ERROR_SUCCESS
;
3725 /***********************************************************************
3726 * HttpQueryInfoW (WININET.@)
3728 * Queries for information about an HTTP request
3735 BOOL WINAPI
HttpQueryInfoW(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
3736 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
3738 http_request_t
*request
;
3741 if (TRACE_ON(wininet
)) {
3742 #define FE(x) { x, #x }
3743 static const wininet_flag_info query_flags
[] = {
3744 FE(HTTP_QUERY_MIME_VERSION
),
3745 FE(HTTP_QUERY_CONTENT_TYPE
),
3746 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING
),
3747 FE(HTTP_QUERY_CONTENT_ID
),
3748 FE(HTTP_QUERY_CONTENT_DESCRIPTION
),
3749 FE(HTTP_QUERY_CONTENT_LENGTH
),
3750 FE(HTTP_QUERY_CONTENT_LANGUAGE
),
3751 FE(HTTP_QUERY_ALLOW
),
3752 FE(HTTP_QUERY_PUBLIC
),
3753 FE(HTTP_QUERY_DATE
),
3754 FE(HTTP_QUERY_EXPIRES
),
3755 FE(HTTP_QUERY_LAST_MODIFIED
),
3756 FE(HTTP_QUERY_MESSAGE_ID
),
3758 FE(HTTP_QUERY_DERIVED_FROM
),
3759 FE(HTTP_QUERY_COST
),
3760 FE(HTTP_QUERY_LINK
),
3761 FE(HTTP_QUERY_PRAGMA
),
3762 FE(HTTP_QUERY_VERSION
),
3763 FE(HTTP_QUERY_STATUS_CODE
),
3764 FE(HTTP_QUERY_STATUS_TEXT
),
3765 FE(HTTP_QUERY_RAW_HEADERS
),
3766 FE(HTTP_QUERY_RAW_HEADERS_CRLF
),
3767 FE(HTTP_QUERY_CONNECTION
),
3768 FE(HTTP_QUERY_ACCEPT
),
3769 FE(HTTP_QUERY_ACCEPT_CHARSET
),
3770 FE(HTTP_QUERY_ACCEPT_ENCODING
),
3771 FE(HTTP_QUERY_ACCEPT_LANGUAGE
),
3772 FE(HTTP_QUERY_AUTHORIZATION
),
3773 FE(HTTP_QUERY_CONTENT_ENCODING
),
3774 FE(HTTP_QUERY_FORWARDED
),
3775 FE(HTTP_QUERY_FROM
),
3776 FE(HTTP_QUERY_IF_MODIFIED_SINCE
),
3777 FE(HTTP_QUERY_LOCATION
),
3778 FE(HTTP_QUERY_ORIG_URI
),
3779 FE(HTTP_QUERY_REFERER
),
3780 FE(HTTP_QUERY_RETRY_AFTER
),
3781 FE(HTTP_QUERY_SERVER
),
3782 FE(HTTP_QUERY_TITLE
),
3783 FE(HTTP_QUERY_USER_AGENT
),
3784 FE(HTTP_QUERY_WWW_AUTHENTICATE
),
3785 FE(HTTP_QUERY_PROXY_AUTHENTICATE
),
3786 FE(HTTP_QUERY_ACCEPT_RANGES
),
3787 FE(HTTP_QUERY_SET_COOKIE
),
3788 FE(HTTP_QUERY_COOKIE
),
3789 FE(HTTP_QUERY_REQUEST_METHOD
),
3790 FE(HTTP_QUERY_REFRESH
),
3791 FE(HTTP_QUERY_CONTENT_DISPOSITION
),
3793 FE(HTTP_QUERY_CACHE_CONTROL
),
3794 FE(HTTP_QUERY_CONTENT_BASE
),
3795 FE(HTTP_QUERY_CONTENT_LOCATION
),
3796 FE(HTTP_QUERY_CONTENT_MD5
),
3797 FE(HTTP_QUERY_CONTENT_RANGE
),
3798 FE(HTTP_QUERY_ETAG
),
3799 FE(HTTP_QUERY_HOST
),
3800 FE(HTTP_QUERY_IF_MATCH
),
3801 FE(HTTP_QUERY_IF_NONE_MATCH
),
3802 FE(HTTP_QUERY_IF_RANGE
),
3803 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE
),
3804 FE(HTTP_QUERY_MAX_FORWARDS
),
3805 FE(HTTP_QUERY_PROXY_AUTHORIZATION
),
3806 FE(HTTP_QUERY_RANGE
),
3807 FE(HTTP_QUERY_TRANSFER_ENCODING
),
3808 FE(HTTP_QUERY_UPGRADE
),
3809 FE(HTTP_QUERY_VARY
),
3811 FE(HTTP_QUERY_WARNING
),
3812 FE(HTTP_QUERY_CUSTOM
)
3814 static const wininet_flag_info modifier_flags
[] = {
3815 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS
),
3816 FE(HTTP_QUERY_FLAG_SYSTEMTIME
),
3817 FE(HTTP_QUERY_FLAG_NUMBER
),
3818 FE(HTTP_QUERY_FLAG_COALESCE
)
3821 DWORD info_mod
= dwInfoLevel
& HTTP_QUERY_MODIFIER_FLAGS_MASK
;
3822 DWORD info
= dwInfoLevel
& HTTP_QUERY_HEADER_MASK
;
3825 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest
, dwInfoLevel
, info
);
3826 TRACE(" Attribute:");
3827 for (i
= 0; i
< (sizeof(query_flags
) / sizeof(query_flags
[0])); i
++) {
3828 if (query_flags
[i
].val
== info
) {
3829 TRACE(" %s", query_flags
[i
].name
);
3833 if (i
== (sizeof(query_flags
) / sizeof(query_flags
[0]))) {
3834 TRACE(" Unknown (%08x)", info
);
3837 TRACE(" Modifier:");
3838 for (i
= 0; i
< (sizeof(modifier_flags
) / sizeof(modifier_flags
[0])); i
++) {
3839 if (modifier_flags
[i
].val
& info_mod
) {
3840 TRACE(" %s", modifier_flags
[i
].name
);
3841 info_mod
&= ~ modifier_flags
[i
].val
;
3846 TRACE(" Unknown (%08x)", info_mod
);
3851 request
= (http_request_t
*) get_handle_object( hHttpRequest
);
3852 if (NULL
== request
|| request
->hdr
.htype
!= WH_HHTTPREQ
)
3854 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
3858 if (lpBuffer
== NULL
)
3859 *lpdwBufferLength
= 0;
3860 res
= HTTP_HttpQueryInfoW( request
, dwInfoLevel
,
3861 lpBuffer
, lpdwBufferLength
, lpdwIndex
);
3865 WININET_Release( &request
->hdr
);
3867 TRACE("%u <--\n", res
);
3868 if(res
!= ERROR_SUCCESS
)
3870 return res
== ERROR_SUCCESS
;
3873 /***********************************************************************
3874 * HttpQueryInfoA (WININET.@)
3876 * Queries for information about an HTTP request
3883 BOOL WINAPI
HttpQueryInfoA(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
3884 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
3890 TRACE("%p %x\n", hHttpRequest
, dwInfoLevel
);
3892 if((dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
) ||
3893 (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
))
3895 return HttpQueryInfoW( hHttpRequest
, dwInfoLevel
, lpBuffer
,
3896 lpdwBufferLength
, lpdwIndex
);
3902 len
= (*lpdwBufferLength
)*sizeof(WCHAR
);
3903 if ((dwInfoLevel
& HTTP_QUERY_HEADER_MASK
) == HTTP_QUERY_CUSTOM
)
3905 alloclen
= MultiByteToWideChar( CP_ACP
, 0, lpBuffer
, -1, NULL
, 0 ) * sizeof(WCHAR
);
3911 bufferW
= heap_alloc(alloclen
);
3912 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
3913 if ((dwInfoLevel
& HTTP_QUERY_HEADER_MASK
) == HTTP_QUERY_CUSTOM
)
3914 MultiByteToWideChar( CP_ACP
, 0, lpBuffer
, -1, bufferW
, alloclen
/ sizeof(WCHAR
) );
3921 result
= HttpQueryInfoW( hHttpRequest
, dwInfoLevel
, bufferW
,
3925 len
= WideCharToMultiByte( CP_ACP
,0, bufferW
, len
/ sizeof(WCHAR
) + 1,
3926 lpBuffer
, *lpdwBufferLength
, NULL
, NULL
);
3927 *lpdwBufferLength
= len
- 1;
3929 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer
));
3932 /* since the strings being returned from HttpQueryInfoW should be
3933 * only ASCII characters, it is reasonable to assume that all of
3934 * the Unicode characters can be reduced to a single byte */
3935 *lpdwBufferLength
= len
/ sizeof(WCHAR
);
3937 heap_free( bufferW
);
3941 /***********************************************************************
3942 * HTTP_GetRedirectURL (internal)
3944 static LPWSTR
HTTP_GetRedirectURL(http_request_t
*request
, LPCWSTR lpszUrl
)
3946 static WCHAR szHttp
[] = {'h','t','t','p',0};
3947 static WCHAR szHttps
[] = {'h','t','t','p','s',0};
3948 http_session_t
*session
= request
->session
;
3949 URL_COMPONENTSW urlComponents
;
3950 DWORD url_length
= 0;
3952 LPWSTR combined_url
;
3954 urlComponents
.dwStructSize
= sizeof(URL_COMPONENTSW
);
3955 urlComponents
.lpszScheme
= (request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
) ? szHttps
: szHttp
;
3956 urlComponents
.dwSchemeLength
= 0;
3957 urlComponents
.lpszHostName
= session
->hostName
;
3958 urlComponents
.dwHostNameLength
= 0;
3959 urlComponents
.nPort
= session
->hostPort
;
3960 urlComponents
.lpszUserName
= session
->userName
;
3961 urlComponents
.dwUserNameLength
= 0;
3962 urlComponents
.lpszPassword
= NULL
;
3963 urlComponents
.dwPasswordLength
= 0;
3964 urlComponents
.lpszUrlPath
= request
->path
;
3965 urlComponents
.dwUrlPathLength
= 0;
3966 urlComponents
.lpszExtraInfo
= NULL
;
3967 urlComponents
.dwExtraInfoLength
= 0;
3969 if (!InternetCreateUrlW(&urlComponents
, 0, NULL
, &url_length
) &&
3970 (GetLastError() != ERROR_INSUFFICIENT_BUFFER
))
3973 orig_url
= heap_alloc(url_length
);
3975 /* convert from bytes to characters */
3976 url_length
= url_length
/ sizeof(WCHAR
) - 1;
3977 if (!InternetCreateUrlW(&urlComponents
, 0, orig_url
, &url_length
))
3979 heap_free(orig_url
);
3984 if (!InternetCombineUrlW(orig_url
, lpszUrl
, NULL
, &url_length
, ICU_ENCODE_SPACES_ONLY
) &&
3985 (GetLastError() != ERROR_INSUFFICIENT_BUFFER
))
3987 heap_free(orig_url
);
3990 combined_url
= heap_alloc(url_length
* sizeof(WCHAR
));
3992 if (!InternetCombineUrlW(orig_url
, lpszUrl
, combined_url
, &url_length
, ICU_ENCODE_SPACES_ONLY
))
3994 heap_free(orig_url
);
3995 heap_free(combined_url
);
3998 heap_free(orig_url
);
3999 return combined_url
;
4003 /***********************************************************************
4004 * HTTP_HandleRedirect (internal)
4006 static DWORD
HTTP_HandleRedirect(http_request_t
*request
, LPCWSTR lpszUrl
)
4008 http_session_t
*session
= request
->session
;
4009 WCHAR path
[INTERNET_MAX_PATH_LENGTH
];
4014 /* if it's an absolute path, keep the same session info */
4015 lstrcpynW(path
, lpszUrl
, INTERNET_MAX_URL_LENGTH
);
4019 URL_COMPONENTSW urlComponents
;
4020 WCHAR protocol
[INTERNET_MAX_SCHEME_LENGTH
];
4021 WCHAR hostName
[INTERNET_MAX_HOST_NAME_LENGTH
];
4022 WCHAR userName
[INTERNET_MAX_USER_NAME_LENGTH
];
4023 BOOL custom_port
= FALSE
;
4025 static const WCHAR httpW
[] = {'h','t','t','p',0};
4026 static const WCHAR httpsW
[] = {'h','t','t','p','s',0};
4032 urlComponents
.dwStructSize
= sizeof(URL_COMPONENTSW
);
4033 urlComponents
.lpszScheme
= protocol
;
4034 urlComponents
.dwSchemeLength
= INTERNET_MAX_SCHEME_LENGTH
;
4035 urlComponents
.lpszHostName
= hostName
;
4036 urlComponents
.dwHostNameLength
= INTERNET_MAX_HOST_NAME_LENGTH
;
4037 urlComponents
.lpszUserName
= userName
;
4038 urlComponents
.dwUserNameLength
= INTERNET_MAX_USER_NAME_LENGTH
;
4039 urlComponents
.lpszPassword
= NULL
;
4040 urlComponents
.dwPasswordLength
= 0;
4041 urlComponents
.lpszUrlPath
= path
;
4042 urlComponents
.dwUrlPathLength
= INTERNET_MAX_PATH_LENGTH
;
4043 urlComponents
.lpszExtraInfo
= NULL
;
4044 urlComponents
.dwExtraInfoLength
= 0;
4045 if(!InternetCrackUrlW(lpszUrl
, strlenW(lpszUrl
), 0, &urlComponents
))
4046 return INTERNET_GetLastError();
4048 if(!strcmpiW(protocol
, httpW
)) {
4049 if(request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
) {
4050 TRACE("redirect from secure page to non-secure page\n");
4051 /* FIXME: warn about from secure redirect to non-secure page */
4052 request
->hdr
.dwFlags
&= ~INTERNET_FLAG_SECURE
;
4055 if(urlComponents
.nPort
== INTERNET_INVALID_PORT_NUMBER
)
4056 urlComponents
.nPort
= INTERNET_DEFAULT_HTTP_PORT
;
4057 else if(urlComponents
.nPort
!= INTERNET_DEFAULT_HTTP_PORT
)
4059 }else if(!strcmpiW(protocol
, httpsW
)) {
4060 if(!(request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
)) {
4061 TRACE("redirect from non-secure page to secure page\n");
4062 /* FIXME: notify about redirect to secure page */
4063 request
->hdr
.dwFlags
|= INTERNET_FLAG_SECURE
;
4066 if(urlComponents
.nPort
== INTERNET_INVALID_PORT_NUMBER
)
4067 urlComponents
.nPort
= INTERNET_DEFAULT_HTTPS_PORT
;
4068 else if(urlComponents
.nPort
!= INTERNET_DEFAULT_HTTPS_PORT
)
4072 heap_free(session
->hostName
);
4076 static const WCHAR fmt
[] = {'%','s',':','%','u',0};
4077 len
= lstrlenW(hostName
);
4078 len
+= 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
4079 session
->hostName
= heap_alloc(len
*sizeof(WCHAR
));
4080 sprintfW(session
->hostName
, fmt
, hostName
, urlComponents
.nPort
);
4083 session
->hostName
= heap_strdupW(hostName
);
4085 HTTP_ProcessHeader(request
, hostW
, session
->hostName
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_REQ
);
4087 heap_free(session
->userName
);
4088 session
->userName
= NULL
;
4090 session
->userName
= heap_strdupW(userName
);
4092 reset_data_stream(request
);
4094 if(strcmpiW(request
->server
->name
, hostName
) || request
->server
->port
!= urlComponents
.nPort
) {
4095 server_t
*new_server
;
4097 new_server
= get_server(hostName
, urlComponents
.nPort
, urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
, TRUE
);
4098 server_release(request
->server
);
4099 request
->server
= new_server
;
4102 heap_free(request
->path
);
4109 rc
= UrlEscapeW(path
, NULL
, &needed
, URL_ESCAPE_SPACES_ONLY
);
4110 if (rc
!= E_POINTER
)
4111 needed
= strlenW(path
)+1;
4112 request
->path
= heap_alloc(needed
*sizeof(WCHAR
));
4113 rc
= UrlEscapeW(path
, request
->path
, &needed
,
4114 URL_ESCAPE_SPACES_ONLY
);
4117 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path
),rc
);
4118 strcpyW(request
->path
,path
);
4122 /* Remove custom content-type/length headers on redirects. */
4123 index
= HTTP_GetCustomHeaderIndex(request
, szContent_Type
, 0, TRUE
);
4125 HTTP_DeleteCustomHeader(request
, index
);
4126 index
= HTTP_GetCustomHeaderIndex(request
, szContent_Length
, 0, TRUE
);
4128 HTTP_DeleteCustomHeader(request
, index
);
4130 return ERROR_SUCCESS
;
4133 /***********************************************************************
4134 * HTTP_build_req (internal)
4136 * concatenate all the strings in the request together
4138 static LPWSTR
HTTP_build_req( LPCWSTR
*list
, int len
)
4143 for( t
= list
; *t
; t
++ )
4144 len
+= strlenW( *t
);
4147 str
= heap_alloc(len
*sizeof(WCHAR
));
4150 for( t
= list
; *t
; t
++ )
4156 static DWORD
HTTP_SecureProxyConnect(http_request_t
*request
)
4158 server_t
*server
= request
->server
;
4159 LPWSTR requestString
;
4166 static const WCHAR connectW
[] = {'C','O','N','N','E','C','T',0};
4170 requestString
= build_request_header( request
, connectW
, server
->host_port
, g_szHttp1_1
, TRUE
);
4172 len
= WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
4173 NULL
, 0, NULL
, NULL
);
4174 len
--; /* the nul terminator isn't needed */
4175 ascii_req
= heap_alloc(len
);
4176 WideCharToMultiByte( CP_ACP
, 0, requestString
, -1, ascii_req
, len
, NULL
, NULL
);
4177 heap_free( requestString
);
4179 TRACE("full request -> %s\n", debugstr_an( ascii_req
, len
) );
4181 NETCON_set_timeout( request
->netconn
, TRUE
, request
->send_timeout
);
4182 res
= NETCON_send( request
->netconn
, ascii_req
, len
, 0, &cnt
);
4183 heap_free( ascii_req
);
4184 if (res
!= ERROR_SUCCESS
)
4187 if (HTTP_GetResponseHeaders( request
, &responseLen
) || !responseLen
)
4188 return ERROR_HTTP_INVALID_HEADER
;
4190 return ERROR_SUCCESS
;
4193 static void HTTP_InsertCookies(http_request_t
*request
)
4195 DWORD cookie_size
, size
, cnt
= 0;
4199 static const WCHAR cookieW
[] = {'C','o','o','k','i','e',':',' ',0};
4201 host
= HTTP_GetHeader(request
, hostW
);
4205 if(get_cookie(host
->lpszValue
, request
->path
, NULL
, &cookie_size
) != ERROR_SUCCESS
)
4208 size
= sizeof(cookieW
) + cookie_size
* sizeof(WCHAR
) + sizeof(szCrLf
);
4209 if(!(cookies
= heap_alloc(size
)))
4212 cnt
+= sprintfW(cookies
, cookieW
);
4213 get_cookie(host
->lpszValue
, request
->path
, cookies
+cnt
, &cookie_size
);
4214 strcatW(cookies
, szCrLf
);
4216 HTTP_HttpAddRequestHeadersW(request
, cookies
, strlenW(cookies
), HTTP_ADDREQ_FLAG_REPLACE
);
4221 static WORD
HTTP_ParseWkday(LPCWSTR day
)
4223 static const WCHAR days
[7][4] = {{ 's','u','n',0 },
4231 for (i
= 0; i
< sizeof(days
)/sizeof(*days
); i
++)
4232 if (!strcmpiW(day
, days
[i
]))
4239 static WORD
HTTP_ParseMonth(LPCWSTR month
)
4241 static const WCHAR jan
[] = { 'j','a','n',0 };
4242 static const WCHAR feb
[] = { 'f','e','b',0 };
4243 static const WCHAR mar
[] = { 'm','a','r',0 };
4244 static const WCHAR apr
[] = { 'a','p','r',0 };
4245 static const WCHAR may
[] = { 'm','a','y',0 };
4246 static const WCHAR jun
[] = { 'j','u','n',0 };
4247 static const WCHAR jul
[] = { 'j','u','l',0 };
4248 static const WCHAR aug
[] = { 'a','u','g',0 };
4249 static const WCHAR sep
[] = { 's','e','p',0 };
4250 static const WCHAR oct
[] = { 'o','c','t',0 };
4251 static const WCHAR nov
[] = { 'n','o','v',0 };
4252 static const WCHAR dec
[] = { 'd','e','c',0 };
4254 if (!strcmpiW(month
, jan
)) return 1;
4255 if (!strcmpiW(month
, feb
)) return 2;
4256 if (!strcmpiW(month
, mar
)) return 3;
4257 if (!strcmpiW(month
, apr
)) return 4;
4258 if (!strcmpiW(month
, may
)) return 5;
4259 if (!strcmpiW(month
, jun
)) return 6;
4260 if (!strcmpiW(month
, jul
)) return 7;
4261 if (!strcmpiW(month
, aug
)) return 8;
4262 if (!strcmpiW(month
, sep
)) return 9;
4263 if (!strcmpiW(month
, oct
)) return 10;
4264 if (!strcmpiW(month
, nov
)) return 11;
4265 if (!strcmpiW(month
, dec
)) return 12;
4270 /* Parses the string pointed to by *str, assumed to be a 24-hour time HH:MM:SS,
4271 * optionally preceded by whitespace.
4272 * Upon success, returns TRUE, sets the wHour, wMinute, and wSecond fields of
4273 * st, and sets *str to the first character after the time format.
4275 static BOOL
HTTP_ParseTime(SYSTEMTIME
*st
, LPCWSTR
*str
)
4281 while (isspaceW(*ptr
))
4284 num
= strtoulW(ptr
, &nextPtr
, 10);
4285 if (!nextPtr
|| nextPtr
<= ptr
|| *nextPtr
!= ':')
4287 ERR("unexpected time format %s\n", debugstr_w(ptr
));
4292 ERR("unexpected hour in time format %s\n", debugstr_w(ptr
));
4296 st
->wHour
= (WORD
)num
;
4297 num
= strtoulW(ptr
, &nextPtr
, 10);
4298 if (!nextPtr
|| nextPtr
<= ptr
|| *nextPtr
!= ':')
4300 ERR("unexpected time format %s\n", debugstr_w(ptr
));
4305 ERR("unexpected minute in time format %s\n", debugstr_w(ptr
));
4309 st
->wMinute
= (WORD
)num
;
4310 num
= strtoulW(ptr
, &nextPtr
, 10);
4311 if (!nextPtr
|| nextPtr
<= ptr
)
4313 ERR("unexpected time format %s\n", debugstr_w(ptr
));
4318 ERR("unexpected second in time format %s\n", debugstr_w(ptr
));
4323 st
->wSecond
= (WORD
)num
;
4327 static BOOL
HTTP_ParseDateAsAsctime(LPCWSTR value
, FILETIME
*ft
)
4329 static const WCHAR gmt
[]= { 'G','M','T',0 };
4330 WCHAR day
[4], *dayPtr
, month
[4], *monthPtr
, *nextPtr
;
4332 SYSTEMTIME st
= { 0 };
4335 for (ptr
= value
, dayPtr
= day
; *ptr
&& !isspaceW(*ptr
) &&
4336 dayPtr
- day
< sizeof(day
) / sizeof(day
[0]) - 1; ptr
++, dayPtr
++)
4339 st
.wDayOfWeek
= HTTP_ParseWkday(day
);
4340 if (st
.wDayOfWeek
>= 7)
4342 ERR("unexpected weekday %s\n", debugstr_w(day
));
4346 while (isspaceW(*ptr
))
4349 for (monthPtr
= month
; !isspace(*ptr
) &&
4350 monthPtr
- month
< sizeof(month
) / sizeof(month
[0]) - 1;
4354 st
.wMonth
= HTTP_ParseMonth(month
);
4355 if (!st
.wMonth
|| st
.wMonth
> 12)
4357 ERR("unexpected month %s\n", debugstr_w(month
));
4361 while (isspaceW(*ptr
))
4364 num
= strtoulW(ptr
, &nextPtr
, 10);
4365 if (!nextPtr
|| nextPtr
<= ptr
|| !num
|| num
> 31)
4367 ERR("unexpected day %s\n", debugstr_w(ptr
));
4371 st
.wDay
= (WORD
)num
;
4373 while (isspaceW(*ptr
))
4376 if (!HTTP_ParseTime(&st
, &ptr
))
4379 while (isspaceW(*ptr
))
4382 num
= strtoulW(ptr
, &nextPtr
, 10);
4383 if (!nextPtr
|| nextPtr
<= ptr
|| num
< 1601 || num
> 30827)
4385 ERR("unexpected year %s\n", debugstr_w(ptr
));
4389 st
.wYear
= (WORD
)num
;
4391 while (isspaceW(*ptr
))
4394 /* asctime() doesn't report a timezone, but some web servers do, so accept
4395 * with or without GMT.
4397 if (*ptr
&& strcmpW(ptr
, gmt
))
4399 ERR("unexpected timezone %s\n", debugstr_w(ptr
));
4402 return SystemTimeToFileTime(&st
, ft
);
4405 static BOOL
HTTP_ParseRfc1123Date(LPCWSTR value
, FILETIME
*ft
)
4407 static const WCHAR gmt
[]= { 'G','M','T',0 };
4408 WCHAR
*nextPtr
, day
[4], month
[4], *monthPtr
;
4411 SYSTEMTIME st
= { 0 };
4413 ptr
= strchrW(value
, ',');
4416 if (ptr
- value
!= 3)
4418 WARN("unexpected weekday %s\n", debugstr_wn(value
, ptr
- value
));
4421 memcpy(day
, value
, (ptr
- value
) * sizeof(WCHAR
));
4423 st
.wDayOfWeek
= HTTP_ParseWkday(day
);
4424 if (st
.wDayOfWeek
> 6)
4426 WARN("unexpected weekday %s\n", debugstr_wn(value
, ptr
- value
));
4431 while (isspaceW(*ptr
))
4434 num
= strtoulW(ptr
, &nextPtr
, 10);
4435 if (!nextPtr
|| nextPtr
<= ptr
|| !num
|| num
> 31)
4437 WARN("unexpected day %s\n", debugstr_w(value
));
4441 st
.wDay
= (WORD
)num
;
4443 while (isspaceW(*ptr
))
4446 for (monthPtr
= month
; !isspace(*ptr
) &&
4447 monthPtr
- month
< sizeof(month
) / sizeof(month
[0]) - 1;
4451 st
.wMonth
= HTTP_ParseMonth(month
);
4452 if (!st
.wMonth
|| st
.wMonth
> 12)
4454 WARN("unexpected month %s\n", debugstr_w(month
));
4458 while (isspaceW(*ptr
))
4461 num
= strtoulW(ptr
, &nextPtr
, 10);
4462 if (!nextPtr
|| nextPtr
<= ptr
|| num
< 1601 || num
> 30827)
4464 ERR("unexpected year %s\n", debugstr_w(value
));
4468 st
.wYear
= (WORD
)num
;
4470 if (!HTTP_ParseTime(&st
, &ptr
))
4473 while (isspaceW(*ptr
))
4476 if (strcmpW(ptr
, gmt
))
4478 ERR("unexpected time zone %s\n", debugstr_w(ptr
));
4481 return SystemTimeToFileTime(&st
, ft
);
4484 static WORD
HTTP_ParseWeekday(LPCWSTR day
)
4486 static const WCHAR days
[7][10] = {{ 's','u','n','d','a','y',0 },
4487 { 'm','o','n','d','a','y',0 },
4488 { 't','u','e','s','d','a','y',0 },
4489 { 'w','e','d','n','e','s','d','a','y',0 },
4490 { 't','h','u','r','s','d','a','y',0 },
4491 { 'f','r','i','d','a','y',0 },
4492 { 's','a','t','u','r','d','a','y',0 }};
4494 for (i
= 0; i
< sizeof(days
)/sizeof(*days
); i
++)
4495 if (!strcmpiW(day
, days
[i
]))
4502 static BOOL
HTTP_ParseRfc850Date(LPCWSTR value
, FILETIME
*ft
)
4504 static const WCHAR gmt
[]= { 'G','M','T',0 };
4505 WCHAR
*nextPtr
, day
[10], month
[4], *monthPtr
;
4508 SYSTEMTIME st
= { 0 };
4510 ptr
= strchrW(value
, ',');
4513 if (ptr
- value
== 3)
4515 memcpy(day
, value
, (ptr
- value
) * sizeof(WCHAR
));
4517 st
.wDayOfWeek
= HTTP_ParseWkday(day
);
4518 if (st
.wDayOfWeek
> 6)
4520 ERR("unexpected weekday %s\n", debugstr_wn(value
, ptr
- value
));
4524 else if (ptr
- value
< sizeof(day
) / sizeof(day
[0]))
4526 memcpy(day
, value
, (ptr
- value
) * sizeof(WCHAR
));
4527 day
[ptr
- value
+ 1] = 0;
4528 st
.wDayOfWeek
= HTTP_ParseWeekday(day
);
4529 if (st
.wDayOfWeek
> 6)
4531 ERR("unexpected weekday %s\n", debugstr_wn(value
, ptr
- value
));
4537 ERR("unexpected weekday %s\n", debugstr_wn(value
, ptr
- value
));
4542 while (isspaceW(*ptr
))
4545 num
= strtoulW(ptr
, &nextPtr
, 10);
4546 if (!nextPtr
|| nextPtr
<= ptr
|| !num
|| num
> 31)
4548 ERR("unexpected day %s\n", debugstr_w(value
));
4552 st
.wDay
= (WORD
)num
;
4556 ERR("unexpected month format %s\n", debugstr_w(ptr
));
4561 for (monthPtr
= month
; *ptr
!= '-' &&
4562 monthPtr
- month
< sizeof(month
) / sizeof(month
[0]) - 1;
4566 st
.wMonth
= HTTP_ParseMonth(month
);
4567 if (!st
.wMonth
|| st
.wMonth
> 12)
4569 ERR("unexpected month %s\n", debugstr_w(month
));
4575 ERR("unexpected year format %s\n", debugstr_w(ptr
));
4580 num
= strtoulW(ptr
, &nextPtr
, 10);
4581 if (!nextPtr
|| nextPtr
<= ptr
|| num
< 1601 || num
> 30827)
4583 ERR("unexpected year %s\n", debugstr_w(value
));
4587 st
.wYear
= (WORD
)num
;
4589 if (!HTTP_ParseTime(&st
, &ptr
))
4592 while (isspaceW(*ptr
))
4595 if (strcmpW(ptr
, gmt
))
4597 ERR("unexpected time zone %s\n", debugstr_w(ptr
));
4600 return SystemTimeToFileTime(&st
, ft
);
4603 static BOOL
HTTP_ParseDate(LPCWSTR value
, FILETIME
*ft
)
4605 static const WCHAR zero
[] = { '0',0 };
4608 if (!strcmpW(value
, zero
))
4610 ft
->dwLowDateTime
= ft
->dwHighDateTime
= 0;
4613 else if (strchrW(value
, ','))
4615 ret
= HTTP_ParseRfc1123Date(value
, ft
);
4618 ret
= HTTP_ParseRfc850Date(value
, ft
);
4620 ERR("unexpected date format %s\n", debugstr_w(value
));
4625 ret
= HTTP_ParseDateAsAsctime(value
, ft
);
4627 ERR("unexpected date format %s\n", debugstr_w(value
));
4632 static void HTTP_ProcessExpires(http_request_t
*request
)
4634 BOOL expirationFound
= FALSE
;
4637 /* Look for a Cache-Control header with a max-age directive, as it takes
4638 * precedence over the Expires header.
4640 headerIndex
= HTTP_GetCustomHeaderIndex(request
, szCache_Control
, 0, FALSE
);
4641 if (headerIndex
!= -1)
4643 LPHTTPHEADERW ccHeader
= &request
->custHeaders
[headerIndex
];
4646 for (ptr
= ccHeader
->lpszValue
; ptr
&& *ptr
; )
4648 LPWSTR comma
= strchrW(ptr
, ','), end
, equal
;
4653 end
= ptr
+ strlenW(ptr
);
4654 for (equal
= end
- 1; equal
> ptr
&& *equal
!= '='; equal
--)
4658 static const WCHAR max_age
[] = {
4659 'm','a','x','-','a','g','e',0 };
4661 if (!strncmpiW(ptr
, max_age
, equal
- ptr
- 1))
4666 age
= strtoulW(equal
+ 1, &nextPtr
, 10);
4667 if (nextPtr
> equal
+ 1)
4671 NtQuerySystemTime( &ft
);
4672 /* Age is in seconds, FILETIME resolution is in
4673 * 100 nanosecond intervals.
4675 ft
.QuadPart
+= age
* (ULONGLONG
)1000000;
4676 request
->expires
.dwLowDateTime
= ft
.u
.LowPart
;
4677 request
->expires
.dwHighDateTime
= ft
.u
.HighPart
;
4678 expirationFound
= TRUE
;
4685 while (isspaceW(*ptr
))
4692 if (!expirationFound
)
4694 headerIndex
= HTTP_GetCustomHeaderIndex(request
, szExpires
, 0, FALSE
);
4695 if (headerIndex
!= -1)
4697 LPHTTPHEADERW expiresHeader
= &request
->custHeaders
[headerIndex
];
4700 if (HTTP_ParseDate(expiresHeader
->lpszValue
, &ft
))
4702 expirationFound
= TRUE
;
4703 request
->expires
= ft
;
4707 if (!expirationFound
)
4711 /* With no known age, default to 10 minutes until expiration. */
4712 NtQuerySystemTime( &t
);
4713 t
.QuadPart
+= 10 * 60 * (ULONGLONG
)10000000;
4714 request
->expires
.dwLowDateTime
= t
.u
.LowPart
;
4715 request
->expires
.dwHighDateTime
= t
.u
.HighPart
;
4719 static void HTTP_ProcessLastModified(http_request_t
*request
)
4723 headerIndex
= HTTP_GetCustomHeaderIndex(request
, szLast_Modified
, 0, FALSE
);
4724 if (headerIndex
!= -1)
4726 LPHTTPHEADERW expiresHeader
= &request
->custHeaders
[headerIndex
];
4729 if (HTTP_ParseDate(expiresHeader
->lpszValue
, &ft
))
4730 request
->last_modified
= ft
;
4734 static void http_process_keep_alive(http_request_t
*req
)
4738 if ((index
= HTTP_GetCustomHeaderIndex(req
, szConnection
, 0, FALSE
)) != -1)
4739 req
->netconn
->keep_alive
= !strcmpiW(req
->custHeaders
[index
].lpszValue
, szKeepAlive
);
4740 else if ((index
= HTTP_GetCustomHeaderIndex(req
, szProxy_Connection
, 0, FALSE
)) != -1)
4741 req
->netconn
->keep_alive
= !strcmpiW(req
->custHeaders
[index
].lpszValue
, szKeepAlive
);
4743 req
->netconn
->keep_alive
= !strcmpiW(req
->version
, g_szHttp1_1
);
4746 static DWORD
open_http_connection(http_request_t
*request
, BOOL
*reusing
)
4748 const BOOL is_https
= (request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
) != 0;
4749 netconn_t
*netconn
= NULL
;
4752 reset_data_stream(request
);
4754 if (request
->netconn
)
4756 if (is_valid_netconn(request
->netconn
) && NETCON_is_alive(request
->netconn
))
4759 return ERROR_SUCCESS
;
4763 free_netconn(request
->netconn
);
4764 request
->netconn
= NULL
;
4768 res
= HTTP_ResolveName(request
);
4769 if(res
!= ERROR_SUCCESS
)
4772 EnterCriticalSection(&connection_pool_cs
);
4774 while(!list_empty(&request
->server
->conn_pool
)) {
4775 netconn
= LIST_ENTRY(list_head(&request
->server
->conn_pool
), netconn_t
, pool_entry
);
4776 list_remove(&netconn
->pool_entry
);
4778 if(is_valid_netconn(netconn
) && NETCON_is_alive(netconn
))
4781 TRACE("connection %p closed during idle\n", netconn
);
4782 free_netconn(netconn
);
4786 LeaveCriticalSection(&connection_pool_cs
);
4789 TRACE("<-- reusing %p netconn\n", netconn
);
4790 request
->netconn
= netconn
;
4792 return ERROR_SUCCESS
;
4795 TRACE("connecting to %s, proxy %s\n", debugstr_w(request
->server
->name
),
4796 request
->proxy
? debugstr_w(request
->proxy
->name
) : "(null)");
4798 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
4799 INTERNET_STATUS_CONNECTING_TO_SERVER
,
4800 request
->server
->addr_str
,
4801 strlen(request
->server
->addr_str
)+1);
4803 res
= create_netconn(is_https
, request
->proxy
? request
->proxy
: request
->server
, request
->security_flags
,
4804 (request
->hdr
.ErrorMask
& INTERNET_ERROR_MASK_COMBINED_SEC_CERT
) != 0,
4805 request
->connect_timeout
, &netconn
);
4806 if(res
!= ERROR_SUCCESS
) {
4807 ERR("create_netconn failed: %u\n", res
);
4811 request
->netconn
= netconn
;
4813 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
4814 INTERNET_STATUS_CONNECTED_TO_SERVER
,
4815 request
->server
->addr_str
, strlen(request
->server
->addr_str
)+1);
4818 /* Note: we differ from Microsoft's WinINet here. they seem to have
4819 * a bug that causes no status callbacks to be sent when starting
4820 * a tunnel to a proxy server using the CONNECT verb. i believe our
4821 * behaviour to be more correct and to not cause any incompatibilities
4822 * because using a secure connection through a proxy server is a rare
4823 * case that would be hard for anyone to depend on */
4825 res
= HTTP_SecureProxyConnect(request
);
4826 if(res
== ERROR_SUCCESS
)
4827 res
= NETCON_secure_connect(request
->netconn
, request
->server
);
4830 if(res
!= ERROR_SUCCESS
) {
4831 http_release_netconn(request
, FALSE
);
4836 TRACE("Created connection to %s: %p\n", debugstr_w(request
->server
->name
), netconn
);
4837 return ERROR_SUCCESS
;
4840 /***********************************************************************
4841 * HTTP_HttpSendRequestW (internal)
4843 * Sends the specified request to the HTTP server
4846 * ERROR_SUCCESS on success
4847 * win32 error code on failure
4850 static DWORD
HTTP_HttpSendRequestW(http_request_t
*request
, LPCWSTR lpszHeaders
,
4851 DWORD dwHeaderLength
, LPVOID lpOptional
, DWORD dwOptionalLength
,
4852 DWORD dwContentLength
, BOOL bEndRequest
)
4855 BOOL redirected
= FALSE
;
4856 LPWSTR requestString
= NULL
;
4859 static const WCHAR szContentLength
[] =
4860 { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0 };
4861 WCHAR contentLengthStr
[sizeof szContentLength
/2 /* includes \r\n */ + 20 /* int */ ];
4864 TRACE("--> %p\n", request
);
4866 assert(request
->hdr
.htype
== WH_HHTTPREQ
);
4868 /* if the verb is NULL default to GET */
4870 request
->verb
= heap_strdupW(szGET
);
4872 if (dwContentLength
|| strcmpW(request
->verb
, szGET
))
4874 sprintfW(contentLengthStr
, szContentLength
, dwContentLength
);
4875 HTTP_HttpAddRequestHeadersW(request
, contentLengthStr
, -1L, HTTP_ADDREQ_FLAG_REPLACE
);
4876 request
->bytesToWrite
= dwContentLength
;
4878 if (request
->session
->appInfo
->agent
)
4880 WCHAR
*agent_header
;
4881 static const WCHAR user_agent
[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0};
4884 len
= strlenW(request
->session
->appInfo
->agent
) + strlenW(user_agent
);
4885 agent_header
= heap_alloc(len
* sizeof(WCHAR
));
4886 sprintfW(agent_header
, user_agent
, request
->session
->appInfo
->agent
);
4888 HTTP_HttpAddRequestHeadersW(request
, agent_header
, strlenW(agent_header
), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
4889 heap_free(agent_header
);
4891 if (request
->hdr
.dwFlags
& INTERNET_FLAG_PRAGMA_NOCACHE
)
4893 static const WCHAR pragma_nocache
[] = {'P','r','a','g','m','a',':',' ','n','o','-','c','a','c','h','e','\r','\n',0};
4894 HTTP_HttpAddRequestHeadersW(request
, pragma_nocache
, strlenW(pragma_nocache
), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
4896 if ((request
->hdr
.dwFlags
& INTERNET_FLAG_NO_CACHE_WRITE
) && strcmpW(request
->verb
, szGET
))
4898 static const WCHAR cache_control
[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l',':',
4899 ' ','n','o','-','c','a','c','h','e','\r','\n',0};
4900 HTTP_HttpAddRequestHeadersW(request
, cache_control
, strlenW(cache_control
), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
4903 /* add the headers the caller supplied */
4904 if( lpszHeaders
&& dwHeaderLength
)
4905 HTTP_HttpAddRequestHeadersW(request
, lpszHeaders
, dwHeaderLength
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REPLACE
);
4910 BOOL reusing_connection
;
4916 request
->contentLength
= ~0u;
4917 request
->bytesToWrite
= 0;
4920 if (TRACE_ON(wininet
))
4922 LPHTTPHEADERW Host
= HTTP_GetHeader(request
, hostW
);
4923 TRACE("Going to url %s %s\n", debugstr_w(Host
->lpszValue
), debugstr_w(request
->path
));
4926 HTTP_FixURL(request
);
4927 if (request
->hdr
.dwFlags
& INTERNET_FLAG_KEEP_CONNECTION
)
4929 HTTP_ProcessHeader(request
, szConnection
, szKeepAlive
, HTTP_ADDHDR_FLAG_REQ
| HTTP_ADDHDR_FLAG_REPLACE
);
4931 HTTP_InsertAuthorization(request
, request
->authInfo
, szAuthorization
);
4932 HTTP_InsertAuthorization(request
, request
->proxyAuthInfo
, szProxy_Authorization
);
4934 if (!(request
->hdr
.dwFlags
& INTERNET_FLAG_NO_COOKIES
))
4935 HTTP_InsertCookies(request
);
4939 WCHAR
*url
= build_proxy_path_url(request
);
4940 requestString
= build_request_header(request
, request
->verb
, url
, request
->version
, TRUE
);
4944 requestString
= build_request_header(request
, request
->verb
, request
->path
, request
->version
, TRUE
);
4947 TRACE("Request header -> %s\n", debugstr_w(requestString
) );
4949 res
= open_http_connection(request
, &reusing_connection
);
4950 if (res
!= ERROR_SUCCESS
)
4953 /* send the request as ASCII, tack on the optional data */
4954 if (!lpOptional
|| redirected
)
4955 dwOptionalLength
= 0;
4956 len
= WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
4957 NULL
, 0, NULL
, NULL
);
4958 ascii_req
= heap_alloc(len
+ dwOptionalLength
);
4959 WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
4960 ascii_req
, len
, NULL
, NULL
);
4962 memcpy( &ascii_req
[len
-1], lpOptional
, dwOptionalLength
);
4963 len
= (len
+ dwOptionalLength
- 1);
4965 TRACE("full request -> %s\n", debugstr_a(ascii_req
) );
4967 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
4968 INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
4970 NETCON_set_timeout( request
->netconn
, TRUE
, request
->send_timeout
);
4971 res
= NETCON_send(request
->netconn
, ascii_req
, len
, 0, &cnt
);
4972 heap_free( ascii_req
);
4973 if(res
!= ERROR_SUCCESS
) {
4974 TRACE("send failed: %u\n", res
);
4975 if(!reusing_connection
)
4977 http_release_netconn(request
, FALSE
);
4982 request
->bytesWritten
= dwOptionalLength
;
4984 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
4985 INTERNET_STATUS_REQUEST_SENT
,
4986 &len
, sizeof(DWORD
));
4992 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
4993 INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
4995 if (HTTP_GetResponseHeaders(request
, &responseLen
))
4997 http_release_netconn(request
, FALSE
);
4998 res
= ERROR_INTERNET_CONNECTION_ABORTED
;
5001 /* FIXME: We should know that connection is closed before sending
5002 * headers. Otherwise wrong callbacks are executed */
5003 if(!responseLen
&& reusing_connection
) {
5004 TRACE("Connection closed by server, reconnecting\n");
5005 http_release_netconn(request
, FALSE
);
5010 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
5011 INTERNET_STATUS_RESPONSE_RECEIVED
, &responseLen
,
5014 http_process_keep_alive(request
);
5015 HTTP_ProcessCookies(request
);
5016 HTTP_ProcessExpires(request
);
5017 HTTP_ProcessLastModified(request
);
5019 res
= set_content_length(request
);
5020 if(res
!= ERROR_SUCCESS
)
5022 if(!request
->contentLength
)
5023 http_release_netconn(request
, TRUE
);
5025 if (!(request
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTO_REDIRECT
) && responseLen
)
5027 WCHAR
*new_url
, szNewLocation
[INTERNET_MAX_URL_LENGTH
];
5028 dwBufferSize
=sizeof(szNewLocation
);
5029 switch(request
->status_code
) {
5030 case HTTP_STATUS_REDIRECT
:
5031 case HTTP_STATUS_MOVED
:
5032 case HTTP_STATUS_REDIRECT_KEEP_VERB
:
5033 case HTTP_STATUS_REDIRECT_METHOD
:
5034 if(HTTP_HttpQueryInfoW(request
,HTTP_QUERY_LOCATION
,szNewLocation
,&dwBufferSize
,NULL
) != ERROR_SUCCESS
)
5037 if (strcmpW(request
->verb
, szGET
) && strcmpW(request
->verb
, szHEAD
) &&
5038 request
->status_code
!= HTTP_STATUS_REDIRECT_KEEP_VERB
)
5040 heap_free(request
->verb
);
5041 request
->verb
= heap_strdupW(szGET
);
5043 http_release_netconn(request
, drain_content(request
, FALSE
));
5044 if ((new_url
= HTTP_GetRedirectURL( request
, szNewLocation
)))
5046 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
, INTERNET_STATUS_REDIRECT
,
5047 new_url
, (strlenW(new_url
) + 1) * sizeof(WCHAR
));
5048 res
= HTTP_HandleRedirect(request
, new_url
);
5049 if (res
== ERROR_SUCCESS
)
5051 heap_free(requestString
);
5054 heap_free( new_url
);
5059 if (!(request
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTH
) && res
== ERROR_SUCCESS
)
5061 WCHAR szAuthValue
[2048];
5063 if (request
->status_code
== HTTP_STATUS_DENIED
)
5065 LPHTTPHEADERW Host
= HTTP_GetHeader(request
, hostW
);
5067 while (HTTP_HttpQueryInfoW(request
,HTTP_QUERY_WWW_AUTHENTICATE
,szAuthValue
,&dwBufferSize
,&dwIndex
) == ERROR_SUCCESS
)
5069 if (HTTP_DoAuthorization(request
, szAuthValue
,
5071 request
->session
->userName
,
5072 request
->session
->password
,
5075 heap_free(requestString
);
5076 if(!drain_content(request
, TRUE
)) {
5077 FIXME("Could not drain content\n");
5078 http_release_netconn(request
, FALSE
);
5086 TRACE("Cleaning wrong authorization data\n");
5087 destroy_authinfo(request
->authInfo
);
5088 request
->authInfo
= NULL
;
5091 if (request
->status_code
== HTTP_STATUS_PROXY_AUTH_REQ
)
5094 while (HTTP_HttpQueryInfoW(request
,HTTP_QUERY_PROXY_AUTHENTICATE
,szAuthValue
,&dwBufferSize
,&dwIndex
) == ERROR_SUCCESS
)
5096 if (HTTP_DoAuthorization(request
, szAuthValue
,
5097 &request
->proxyAuthInfo
,
5098 request
->session
->appInfo
->proxyUsername
,
5099 request
->session
->appInfo
->proxyPassword
,
5102 heap_free(requestString
);
5103 if(!drain_content(request
, TRUE
)) {
5104 FIXME("Could not drain content\n");
5105 http_release_netconn(request
, FALSE
);
5113 TRACE("Cleaning wrong proxy authorization data\n");
5114 destroy_authinfo(request
->proxyAuthInfo
);
5115 request
->proxyAuthInfo
= NULL
;
5121 res
= ERROR_SUCCESS
;
5126 heap_free(requestString
);
5128 /* TODO: send notification for P3P header */
5130 if(res
== ERROR_SUCCESS
)
5131 create_cache_entry(request
);
5133 if (request
->session
->appInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
5135 if (res
== ERROR_SUCCESS
) {
5136 if(bEndRequest
&& request
->contentLength
&& request
->bytesWritten
== request
->bytesToWrite
)
5137 HTTP_ReceiveRequestData(request
, TRUE
, NULL
);
5139 send_request_complete(request
,
5140 request
->session
->hdr
.dwInternalFlags
& INET_OPENURL
? (DWORD_PTR
)request
->hdr
.hInternet
: 1, 0);
5142 send_request_complete(request
, 0, res
);
5158 } send_request_task_t
;
5160 /***********************************************************************
5162 * Helper functions for the HttpSendRequest(Ex) functions
5165 static void AsyncHttpSendRequestProc(task_header_t
*hdr
)
5167 send_request_task_t
*task
= (send_request_task_t
*)hdr
;
5168 http_request_t
*request
= (http_request_t
*)task
->hdr
.hdr
;
5170 TRACE("%p\n", request
);
5172 HTTP_HttpSendRequestW(request
, task
->headers
, task
->headers_len
, task
->optional
,
5173 task
->optional_len
, task
->content_len
, task
->end_request
);
5175 heap_free(task
->headers
);
5179 static DWORD
HTTP_HttpEndRequestW(http_request_t
*request
, DWORD dwFlags
, DWORD_PTR dwContext
)
5183 DWORD res
= ERROR_SUCCESS
;
5185 if(!is_valid_netconn(request
->netconn
)) {
5186 WARN("Not connected\n");
5187 send_request_complete(request
, 0, ERROR_INTERNET_OPERATION_CANCELLED
);
5188 return ERROR_INTERNET_OPERATION_CANCELLED
;
5191 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
5192 INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
5194 if (HTTP_GetResponseHeaders(request
, &responseLen
) || !responseLen
)
5195 res
= ERROR_HTTP_HEADER_NOT_FOUND
;
5197 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
5198 INTERNET_STATUS_RESPONSE_RECEIVED
, &responseLen
, sizeof(DWORD
));
5200 /* process cookies here. Is this right? */
5201 http_process_keep_alive(request
);
5202 HTTP_ProcessCookies(request
);
5203 HTTP_ProcessExpires(request
);
5204 HTTP_ProcessLastModified(request
);
5206 if ((res
= set_content_length(request
)) == ERROR_SUCCESS
) {
5207 if(!request
->contentLength
)
5208 http_release_netconn(request
, TRUE
);
5211 if (res
== ERROR_SUCCESS
&& !(request
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTO_REDIRECT
))
5213 switch(request
->status_code
) {
5214 case HTTP_STATUS_REDIRECT
:
5215 case HTTP_STATUS_MOVED
:
5216 case HTTP_STATUS_REDIRECT_METHOD
:
5217 case HTTP_STATUS_REDIRECT_KEEP_VERB
: {
5218 WCHAR
*new_url
, szNewLocation
[INTERNET_MAX_URL_LENGTH
];
5219 dwBufferSize
=sizeof(szNewLocation
);
5220 if (HTTP_HttpQueryInfoW(request
, HTTP_QUERY_LOCATION
, szNewLocation
, &dwBufferSize
, NULL
) != ERROR_SUCCESS
)
5223 if (strcmpW(request
->verb
, szGET
) && strcmpW(request
->verb
, szHEAD
) &&
5224 request
->status_code
!= HTTP_STATUS_REDIRECT_KEEP_VERB
)
5226 heap_free(request
->verb
);
5227 request
->verb
= heap_strdupW(szGET
);
5229 http_release_netconn(request
, drain_content(request
, FALSE
));
5230 if ((new_url
= HTTP_GetRedirectURL( request
, szNewLocation
)))
5232 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
, INTERNET_STATUS_REDIRECT
,
5233 new_url
, (strlenW(new_url
) + 1) * sizeof(WCHAR
));
5234 res
= HTTP_HandleRedirect(request
, new_url
);
5235 if (res
== ERROR_SUCCESS
)
5236 res
= HTTP_HttpSendRequestW(request
, NULL
, 0, NULL
, 0, 0, TRUE
);
5237 heap_free( new_url
);
5243 if(res
== ERROR_SUCCESS
)
5244 create_cache_entry(request
);
5246 if (res
== ERROR_SUCCESS
&& request
->contentLength
)
5247 HTTP_ReceiveRequestData(request
, TRUE
, NULL
);
5249 send_request_complete(request
, res
== ERROR_SUCCESS
, res
);
5254 /***********************************************************************
5255 * HttpEndRequestA (WININET.@)
5257 * Ends an HTTP request that was started by HttpSendRequestEx
5260 * TRUE if successful
5264 BOOL WINAPI
HttpEndRequestA(HINTERNET hRequest
,
5265 LPINTERNET_BUFFERSA lpBuffersOut
, DWORD dwFlags
, DWORD_PTR dwContext
)
5267 TRACE("(%p, %p, %08x, %08lx)\n", hRequest
, lpBuffersOut
, dwFlags
, dwContext
);
5271 SetLastError(ERROR_INVALID_PARAMETER
);
5275 return HttpEndRequestW(hRequest
, NULL
, dwFlags
, dwContext
);
5282 } end_request_task_t
;
5284 static void AsyncHttpEndRequestProc(task_header_t
*hdr
)
5286 end_request_task_t
*task
= (end_request_task_t
*)hdr
;
5287 http_request_t
*req
= (http_request_t
*)task
->hdr
.hdr
;
5291 HTTP_HttpEndRequestW(req
, task
->flags
, task
->context
);
5294 /***********************************************************************
5295 * HttpEndRequestW (WININET.@)
5297 * Ends an HTTP request that was started by HttpSendRequestEx
5300 * TRUE if successful
5304 BOOL WINAPI
HttpEndRequestW(HINTERNET hRequest
,
5305 LPINTERNET_BUFFERSW lpBuffersOut
, DWORD dwFlags
, DWORD_PTR dwContext
)
5307 http_request_t
*request
;
5310 TRACE("%p %p %x %lx -->\n", hRequest
, lpBuffersOut
, dwFlags
, dwContext
);
5314 SetLastError(ERROR_INVALID_PARAMETER
);
5318 request
= (http_request_t
*) get_handle_object( hRequest
);
5320 if (NULL
== request
|| request
->hdr
.htype
!= WH_HHTTPREQ
)
5322 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
5324 WININET_Release( &request
->hdr
);
5327 request
->hdr
.dwFlags
|= dwFlags
;
5329 if (request
->session
->appInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
5331 end_request_task_t
*task
;
5333 task
= alloc_async_task(&request
->hdr
, AsyncHttpEndRequestProc
, sizeof(*task
));
5334 task
->flags
= dwFlags
;
5335 task
->context
= dwContext
;
5337 INTERNET_AsyncCall(&task
->hdr
);
5338 res
= ERROR_IO_PENDING
;
5341 res
= HTTP_HttpEndRequestW(request
, dwFlags
, dwContext
);
5343 WININET_Release( &request
->hdr
);
5344 TRACE("%u <--\n", res
);
5345 if(res
!= ERROR_SUCCESS
)
5347 return res
== ERROR_SUCCESS
;
5350 /***********************************************************************
5351 * HttpSendRequestExA (WININET.@)
5353 * Sends the specified request to the HTTP server and allows chunked
5358 * Failure: FALSE, call GetLastError() for more information.
5360 BOOL WINAPI
HttpSendRequestExA(HINTERNET hRequest
,
5361 LPINTERNET_BUFFERSA lpBuffersIn
,
5362 LPINTERNET_BUFFERSA lpBuffersOut
,
5363 DWORD dwFlags
, DWORD_PTR dwContext
)
5365 INTERNET_BUFFERSW BuffersInW
;
5368 LPWSTR header
= NULL
;
5370 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest
, lpBuffersIn
,
5371 lpBuffersOut
, dwFlags
, dwContext
);
5375 BuffersInW
.dwStructSize
= sizeof(LPINTERNET_BUFFERSW
);
5376 if (lpBuffersIn
->lpcszHeader
)
5378 headerlen
= MultiByteToWideChar(CP_ACP
,0,lpBuffersIn
->lpcszHeader
,
5379 lpBuffersIn
->dwHeadersLength
,0,0);
5380 header
= heap_alloc(headerlen
*sizeof(WCHAR
));
5381 if (!(BuffersInW
.lpcszHeader
= header
))
5383 SetLastError(ERROR_OUTOFMEMORY
);
5386 BuffersInW
.dwHeadersLength
= MultiByteToWideChar(CP_ACP
, 0,
5387 lpBuffersIn
->lpcszHeader
, lpBuffersIn
->dwHeadersLength
,
5391 BuffersInW
.lpcszHeader
= NULL
;
5392 BuffersInW
.dwHeadersTotal
= lpBuffersIn
->dwHeadersTotal
;
5393 BuffersInW
.lpvBuffer
= lpBuffersIn
->lpvBuffer
;
5394 BuffersInW
.dwBufferLength
= lpBuffersIn
->dwBufferLength
;
5395 BuffersInW
.dwBufferTotal
= lpBuffersIn
->dwBufferTotal
;
5396 BuffersInW
.Next
= NULL
;
5399 rc
= HttpSendRequestExW(hRequest
, lpBuffersIn
? &BuffersInW
: NULL
, NULL
, dwFlags
, dwContext
);
5405 /***********************************************************************
5406 * HttpSendRequestExW (WININET.@)
5408 * Sends the specified request to the HTTP server and allows chunked
5413 * Failure: FALSE, call GetLastError() for more information.
5415 BOOL WINAPI
HttpSendRequestExW(HINTERNET hRequest
,
5416 LPINTERNET_BUFFERSW lpBuffersIn
,
5417 LPINTERNET_BUFFERSW lpBuffersOut
,
5418 DWORD dwFlags
, DWORD_PTR dwContext
)
5420 http_request_t
*request
;
5421 http_session_t
*session
;
5425 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest
, lpBuffersIn
,
5426 lpBuffersOut
, dwFlags
, dwContext
);
5428 request
= (http_request_t
*) get_handle_object( hRequest
);
5430 if (NULL
== request
|| request
->hdr
.htype
!= WH_HHTTPREQ
)
5432 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
5436 session
= request
->session
;
5437 assert(session
->hdr
.htype
== WH_HHTTPSESSION
);
5438 hIC
= session
->appInfo
;
5439 assert(hIC
->hdr
.htype
== WH_HINIT
);
5441 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
5443 send_request_task_t
*task
;
5445 task
= alloc_async_task(&request
->hdr
, AsyncHttpSendRequestProc
, sizeof(*task
));
5450 if (lpBuffersIn
->lpcszHeader
)
5452 if (lpBuffersIn
->dwHeadersLength
== ~0u)
5453 size
= (strlenW( lpBuffersIn
->lpcszHeader
) + 1) * sizeof(WCHAR
);
5455 size
= lpBuffersIn
->dwHeadersLength
* sizeof(WCHAR
);
5457 task
->headers
= heap_alloc(size
);
5458 memcpy(task
->headers
, lpBuffersIn
->lpcszHeader
, size
);
5460 else task
->headers
= NULL
;
5462 task
->headers_len
= size
/ sizeof(WCHAR
);
5463 task
->optional
= lpBuffersIn
->lpvBuffer
;
5464 task
->optional_len
= lpBuffersIn
->dwBufferLength
;
5465 task
->content_len
= lpBuffersIn
->dwBufferTotal
;
5469 task
->headers
= NULL
;
5470 task
->headers_len
= 0;
5471 task
->optional
= NULL
;
5472 task
->optional_len
= 0;
5473 task
->content_len
= 0;
5476 task
->end_request
= FALSE
;
5478 INTERNET_AsyncCall(&task
->hdr
);
5479 res
= ERROR_IO_PENDING
;
5484 res
= HTTP_HttpSendRequestW(request
, lpBuffersIn
->lpcszHeader
, lpBuffersIn
->dwHeadersLength
,
5485 lpBuffersIn
->lpvBuffer
, lpBuffersIn
->dwBufferLength
,
5486 lpBuffersIn
->dwBufferTotal
, FALSE
);
5488 res
= HTTP_HttpSendRequestW(request
, NULL
, 0, NULL
, 0, 0, FALSE
);
5493 WININET_Release( &request
->hdr
);
5497 return res
== ERROR_SUCCESS
;
5500 /***********************************************************************
5501 * HttpSendRequestW (WININET.@)
5503 * Sends the specified request to the HTTP server
5510 BOOL WINAPI
HttpSendRequestW(HINTERNET hHttpRequest
, LPCWSTR lpszHeaders
,
5511 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
5513 http_request_t
*request
;
5514 http_session_t
*session
= NULL
;
5515 appinfo_t
*hIC
= NULL
;
5516 DWORD res
= ERROR_SUCCESS
;
5518 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest
,
5519 debugstr_wn(lpszHeaders
, dwHeaderLength
), dwHeaderLength
, lpOptional
, dwOptionalLength
);
5521 request
= (http_request_t
*) get_handle_object( hHttpRequest
);
5522 if (NULL
== request
|| request
->hdr
.htype
!= WH_HHTTPREQ
)
5524 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
5528 session
= request
->session
;
5529 if (NULL
== session
|| session
->hdr
.htype
!= WH_HHTTPSESSION
)
5531 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
5535 hIC
= session
->appInfo
;
5536 if (NULL
== hIC
|| hIC
->hdr
.htype
!= WH_HINIT
)
5538 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
5542 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
5544 send_request_task_t
*task
;
5546 task
= alloc_async_task(&request
->hdr
, AsyncHttpSendRequestProc
, sizeof(*task
));
5551 if (dwHeaderLength
== ~0u) size
= (strlenW(lpszHeaders
) + 1) * sizeof(WCHAR
);
5552 else size
= dwHeaderLength
* sizeof(WCHAR
);
5554 task
->headers
= heap_alloc(size
);
5555 memcpy(task
->headers
, lpszHeaders
, size
);
5558 task
->headers
= NULL
;
5559 task
->headers_len
= dwHeaderLength
;
5560 task
->optional
= lpOptional
;
5561 task
->optional_len
= dwOptionalLength
;
5562 task
->content_len
= dwOptionalLength
;
5563 task
->end_request
= TRUE
;
5565 INTERNET_AsyncCall(&task
->hdr
);
5566 res
= ERROR_IO_PENDING
;
5570 res
= HTTP_HttpSendRequestW(request
, lpszHeaders
,
5571 dwHeaderLength
, lpOptional
, dwOptionalLength
,
5572 dwOptionalLength
, TRUE
);
5576 WININET_Release( &request
->hdr
);
5579 return res
== ERROR_SUCCESS
;
5582 /***********************************************************************
5583 * HttpSendRequestA (WININET.@)
5585 * Sends the specified request to the HTTP server
5592 BOOL WINAPI
HttpSendRequestA(HINTERNET hHttpRequest
, LPCSTR lpszHeaders
,
5593 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
5596 LPWSTR szHeaders
=NULL
;
5597 DWORD nLen
=dwHeaderLength
;
5598 if(lpszHeaders
!=NULL
)
5600 nLen
=MultiByteToWideChar(CP_ACP
,0,lpszHeaders
,dwHeaderLength
,NULL
,0);
5601 szHeaders
= heap_alloc(nLen
*sizeof(WCHAR
));
5602 MultiByteToWideChar(CP_ACP
,0,lpszHeaders
,dwHeaderLength
,szHeaders
,nLen
);
5604 result
= HttpSendRequestW(hHttpRequest
, szHeaders
, nLen
, lpOptional
, dwOptionalLength
);
5605 heap_free(szHeaders
);
5609 /***********************************************************************
5610 * HTTPSESSION_Destroy (internal)
5612 * Deallocate session handle
5615 static void HTTPSESSION_Destroy(object_header_t
*hdr
)
5617 http_session_t
*session
= (http_session_t
*) hdr
;
5619 TRACE("%p\n", session
);
5621 WININET_Release(&session
->appInfo
->hdr
);
5623 heap_free(session
->hostName
);
5624 heap_free(session
->password
);
5625 heap_free(session
->userName
);
5628 static DWORD
HTTPSESSION_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
5630 http_session_t
*ses
= (http_session_t
*)hdr
;
5633 case INTERNET_OPTION_HANDLE_TYPE
:
5634 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
5636 if (*size
< sizeof(ULONG
))
5637 return ERROR_INSUFFICIENT_BUFFER
;
5639 *size
= sizeof(DWORD
);
5640 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_CONNECT_HTTP
;
5641 return ERROR_SUCCESS
;
5642 case INTERNET_OPTION_CONNECT_TIMEOUT
:
5643 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
5645 if (*size
< sizeof(DWORD
))
5646 return ERROR_INSUFFICIENT_BUFFER
;
5648 *size
= sizeof(DWORD
);
5649 *(DWORD
*)buffer
= ses
->connect_timeout
;
5650 return ERROR_SUCCESS
;
5652 case INTERNET_OPTION_SEND_TIMEOUT
:
5653 TRACE("INTERNET_OPTION_SEND_TIMEOUT\n");
5655 if (*size
< sizeof(DWORD
))
5656 return ERROR_INSUFFICIENT_BUFFER
;
5658 *size
= sizeof(DWORD
);
5659 *(DWORD
*)buffer
= ses
->send_timeout
;
5660 return ERROR_SUCCESS
;
5662 case INTERNET_OPTION_RECEIVE_TIMEOUT
:
5663 TRACE("INTERNET_OPTION_RECEIVE_TIMEOUT\n");
5665 if (*size
< sizeof(DWORD
))
5666 return ERROR_INSUFFICIENT_BUFFER
;
5668 *size
= sizeof(DWORD
);
5669 *(DWORD
*)buffer
= ses
->receive_timeout
;
5670 return ERROR_SUCCESS
;
5673 return INET_QueryOption(hdr
, option
, buffer
, size
, unicode
);
5676 static DWORD
HTTPSESSION_SetOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD size
)
5678 http_session_t
*ses
= (http_session_t
*)hdr
;
5681 case INTERNET_OPTION_USERNAME
:
5683 heap_free(ses
->userName
);
5684 if (!(ses
->userName
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
5685 return ERROR_SUCCESS
;
5687 case INTERNET_OPTION_PASSWORD
:
5689 heap_free(ses
->password
);
5690 if (!(ses
->password
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
5691 return ERROR_SUCCESS
;
5693 case INTERNET_OPTION_PROXY_USERNAME
:
5695 heap_free(ses
->appInfo
->proxyUsername
);
5696 if (!(ses
->appInfo
->proxyUsername
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
5697 return ERROR_SUCCESS
;
5699 case INTERNET_OPTION_PROXY_PASSWORD
:
5701 heap_free(ses
->appInfo
->proxyPassword
);
5702 if (!(ses
->appInfo
->proxyPassword
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
5703 return ERROR_SUCCESS
;
5705 case INTERNET_OPTION_CONNECT_TIMEOUT
:
5707 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
5708 ses
->connect_timeout
= *(DWORD
*)buffer
;
5709 return ERROR_SUCCESS
;
5711 case INTERNET_OPTION_SEND_TIMEOUT
:
5713 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
5714 ses
->send_timeout
= *(DWORD
*)buffer
;
5715 return ERROR_SUCCESS
;
5717 case INTERNET_OPTION_RECEIVE_TIMEOUT
:
5719 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
5720 ses
->receive_timeout
= *(DWORD
*)buffer
;
5721 return ERROR_SUCCESS
;
5726 return INET_SetOption(hdr
, option
, buffer
, size
);
5729 static const object_vtbl_t HTTPSESSIONVtbl
= {
5730 HTTPSESSION_Destroy
,
5732 HTTPSESSION_QueryOption
,
5733 HTTPSESSION_SetOption
,
5742 /***********************************************************************
5743 * HTTP_Connect (internal)
5745 * Create http session handle
5748 * HINTERNET a session handle on success
5752 DWORD
HTTP_Connect(appinfo_t
*hIC
, LPCWSTR lpszServerName
,
5753 INTERNET_PORT serverPort
, LPCWSTR lpszUserName
,
5754 LPCWSTR lpszPassword
, DWORD dwFlags
, DWORD_PTR dwContext
,
5755 DWORD dwInternalFlags
, HINTERNET
*ret
)
5757 http_session_t
*session
= NULL
;
5761 if (!lpszServerName
|| !lpszServerName
[0])
5762 return ERROR_INVALID_PARAMETER
;
5764 assert( hIC
->hdr
.htype
== WH_HINIT
);
5766 session
= alloc_object(&hIC
->hdr
, &HTTPSESSIONVtbl
, sizeof(http_session_t
));
5768 return ERROR_OUTOFMEMORY
;
5771 * According to my tests. The name is not resolved until a request is sent
5774 session
->hdr
.htype
= WH_HHTTPSESSION
;
5775 session
->hdr
.dwFlags
= dwFlags
;
5776 session
->hdr
.dwContext
= dwContext
;
5777 session
->hdr
.dwInternalFlags
|= dwInternalFlags
;
5779 WININET_AddRef( &hIC
->hdr
);
5780 session
->appInfo
= hIC
;
5781 list_add_head( &hIC
->hdr
.children
, &session
->hdr
.entry
);
5783 session
->hostName
= heap_strdupW(lpszServerName
);
5784 if (lpszUserName
&& lpszUserName
[0])
5785 session
->userName
= heap_strdupW(lpszUserName
);
5786 if (lpszPassword
&& lpszPassword
[0])
5787 session
->password
= heap_strdupW(lpszPassword
);
5788 session
->hostPort
= serverPort
;
5789 session
->connect_timeout
= hIC
->connect_timeout
;
5790 session
->send_timeout
= INFINITE
;
5791 session
->receive_timeout
= INFINITE
;
5793 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
5794 if (!(session
->hdr
.dwInternalFlags
& INET_OPENURL
))
5796 INTERNET_SendCallback(&hIC
->hdr
, dwContext
,
5797 INTERNET_STATUS_HANDLE_CREATED
, &session
->hdr
.hInternet
,
5802 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
5806 TRACE("%p --> %p\n", hIC
, session
);
5808 *ret
= session
->hdr
.hInternet
;
5809 return ERROR_SUCCESS
;
5812 /***********************************************************************
5813 * HTTP_clear_response_headers (internal)
5815 * clear out any old response headers
5817 static void HTTP_clear_response_headers( http_request_t
*request
)
5821 for( i
=0; i
<request
->nCustHeaders
; i
++)
5823 if( !request
->custHeaders
[i
].lpszField
)
5825 if( !request
->custHeaders
[i
].lpszValue
)
5827 if ( request
->custHeaders
[i
].wFlags
& HDR_ISREQUEST
)
5829 HTTP_DeleteCustomHeader( request
, i
);
5834 /***********************************************************************
5835 * HTTP_GetResponseHeaders (internal)
5837 * Read server response
5844 static DWORD
HTTP_GetResponseHeaders(http_request_t
*request
, INT
*len
)
5847 WCHAR buffer
[MAX_REPLY_LEN
];
5848 DWORD buflen
= MAX_REPLY_LEN
;
5850 char bufferA
[MAX_REPLY_LEN
];
5851 LPWSTR status_code
= NULL
, status_text
= NULL
;
5852 DWORD res
= ERROR_HTTP_INVALID_SERVER_RESPONSE
;
5853 BOOL codeHundred
= FALSE
;
5857 if(!is_valid_netconn(request
->netconn
))
5860 /* clear old response headers (eg. from a redirect response) */
5861 HTTP_clear_response_headers( request
);
5863 NETCON_set_timeout( request
->netconn
, FALSE
, request
->receive_timeout
);
5866 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
5868 buflen
= MAX_REPLY_LEN
;
5869 if ((res
= read_line(request
, bufferA
, &buflen
)))
5872 if (!buflen
) goto lend
;
5875 MultiByteToWideChar( CP_ACP
, 0, bufferA
, buflen
, buffer
, MAX_REPLY_LEN
);
5876 /* check is this a status code line? */
5877 if (!strncmpW(buffer
, g_szHttp1_0
, 4))
5879 /* split the version from the status code */
5880 status_code
= strchrW( buffer
, ' ' );
5885 /* split the status code from the status text */
5886 status_text
= strchrW( status_code
, ' ' );
5890 request
->status_code
= atoiW(status_code
);
5892 TRACE("version [%s] status code [%s] status text [%s]\n",
5893 debugstr_w(buffer
), debugstr_w(status_code
), debugstr_w(status_text
) );
5895 codeHundred
= request
->status_code
== HTTP_STATUS_CONTINUE
;
5897 else if (!codeHundred
)
5899 WARN("No status line at head of response (%s)\n", debugstr_w(buffer
));
5901 heap_free(request
->version
);
5902 heap_free(request
->statusText
);
5904 request
->status_code
= HTTP_STATUS_OK
;
5905 request
->version
= heap_strdupW(g_szHttp1_0
);
5906 request
->statusText
= heap_strdupW(szOK
);
5910 } while (codeHundred
);
5912 /* Add status code */
5913 HTTP_ProcessHeader(request
, szStatus
, status_code
,
5914 HTTP_ADDHDR_FLAG_REPLACE
);
5916 heap_free(request
->version
);
5917 heap_free(request
->statusText
);
5919 request
->version
= heap_strdupW(buffer
);
5920 request
->statusText
= heap_strdupW(status_text
? status_text
: emptyW
);
5922 /* Restore the spaces */
5923 *(status_code
-1) = ' ';
5925 *(status_text
-1) = ' ';
5927 /* Parse each response line */
5930 buflen
= MAX_REPLY_LEN
;
5931 if (!read_line(request
, bufferA
, &buflen
) && buflen
)
5933 LPWSTR
* pFieldAndValue
;
5935 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA
));
5937 if (!bufferA
[0]) break;
5938 MultiByteToWideChar( CP_ACP
, 0, bufferA
, buflen
, buffer
, MAX_REPLY_LEN
);
5940 pFieldAndValue
= HTTP_InterpretHttpHeader(buffer
);
5943 HTTP_ProcessHeader(request
, pFieldAndValue
[0], pFieldAndValue
[1],
5944 HTTP_ADDREQ_FLAG_ADD
);
5945 HTTP_FreeTokens(pFieldAndValue
);
5956 res
= ERROR_SUCCESS
;
5965 /***********************************************************************
5966 * HTTP_InterpretHttpHeader (internal)
5968 * Parse server response
5972 * Pointer to array of field, value, NULL on success.
5975 static LPWSTR
* HTTP_InterpretHttpHeader(LPCWSTR buffer
)
5977 LPWSTR
* pTokenPair
;
5981 pTokenPair
= heap_alloc_zero(sizeof(*pTokenPair
)*3);
5983 pszColon
= strchrW(buffer
, ':');
5984 /* must have two tokens */
5987 HTTP_FreeTokens(pTokenPair
);
5989 TRACE("No ':' in line: %s\n", debugstr_w(buffer
));
5993 pTokenPair
[0] = heap_alloc((pszColon
- buffer
+ 1) * sizeof(WCHAR
));
5996 HTTP_FreeTokens(pTokenPair
);
5999 memcpy(pTokenPair
[0], buffer
, (pszColon
- buffer
) * sizeof(WCHAR
));
6000 pTokenPair
[0][pszColon
- buffer
] = '\0';
6004 len
= strlenW(pszColon
);
6005 pTokenPair
[1] = heap_alloc((len
+ 1) * sizeof(WCHAR
));
6008 HTTP_FreeTokens(pTokenPair
);
6011 memcpy(pTokenPair
[1], pszColon
, (len
+ 1) * sizeof(WCHAR
));
6013 strip_spaces(pTokenPair
[0]);
6014 strip_spaces(pTokenPair
[1]);
6016 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair
[0]), debugstr_w(pTokenPair
[1]));
6020 /***********************************************************************
6021 * HTTP_ProcessHeader (internal)
6023 * Stuff header into header tables according to <dwModifier>
6027 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
6029 static DWORD
HTTP_ProcessHeader(http_request_t
*request
, LPCWSTR field
, LPCWSTR value
, DWORD dwModifier
)
6031 LPHTTPHEADERW lphttpHdr
= NULL
;
6033 BOOL request_only
= dwModifier
& HTTP_ADDHDR_FLAG_REQ
;
6034 DWORD res
= ERROR_HTTP_INVALID_HEADER
;
6036 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field
), debugstr_w(value
), dwModifier
);
6038 /* REPLACE wins out over ADD */
6039 if (dwModifier
& HTTP_ADDHDR_FLAG_REPLACE
)
6040 dwModifier
&= ~HTTP_ADDHDR_FLAG_ADD
;
6042 if (dwModifier
& HTTP_ADDHDR_FLAG_ADD
)
6045 index
= HTTP_GetCustomHeaderIndex(request
, field
, 0, request_only
);
6049 if (dwModifier
& HTTP_ADDHDR_FLAG_ADD_IF_NEW
)
6050 return ERROR_HTTP_INVALID_HEADER
;
6051 lphttpHdr
= &request
->custHeaders
[index
];
6057 hdr
.lpszField
= (LPWSTR
)field
;
6058 hdr
.lpszValue
= (LPWSTR
)value
;
6059 hdr
.wFlags
= hdr
.wCount
= 0;
6061 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
6062 hdr
.wFlags
|= HDR_ISREQUEST
;
6064 return HTTP_InsertCustomHeader(request
, &hdr
);
6066 /* no value to delete */
6067 else return ERROR_SUCCESS
;
6069 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
6070 lphttpHdr
->wFlags
|= HDR_ISREQUEST
;
6072 lphttpHdr
->wFlags
&= ~HDR_ISREQUEST
;
6074 if (dwModifier
& HTTP_ADDHDR_FLAG_REPLACE
)
6076 HTTP_DeleteCustomHeader( request
, index
);
6078 if (value
&& value
[0])
6082 hdr
.lpszField
= (LPWSTR
)field
;
6083 hdr
.lpszValue
= (LPWSTR
)value
;
6084 hdr
.wFlags
= hdr
.wCount
= 0;
6086 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
6087 hdr
.wFlags
|= HDR_ISREQUEST
;
6089 return HTTP_InsertCustomHeader(request
, &hdr
);
6092 return ERROR_SUCCESS
;
6094 else if (dwModifier
& COALESCEFLAGS
)
6099 INT origlen
= strlenW(lphttpHdr
->lpszValue
);
6100 INT valuelen
= strlenW(value
);
6102 if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
)
6105 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
6107 else if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON
)
6110 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
6113 len
= origlen
+ valuelen
+ ((ch
> 0) ? 2 : 0);
6115 lpsztmp
= heap_realloc(lphttpHdr
->lpszValue
, (len
+1)*sizeof(WCHAR
));
6118 lphttpHdr
->lpszValue
= lpsztmp
;
6119 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
6122 lphttpHdr
->lpszValue
[origlen
] = ch
;
6124 lphttpHdr
->lpszValue
[origlen
] = ' ';
6128 memcpy(&lphttpHdr
->lpszValue
[origlen
], value
, valuelen
*sizeof(WCHAR
));
6129 lphttpHdr
->lpszValue
[len
] = '\0';
6130 res
= ERROR_SUCCESS
;
6134 WARN("heap_realloc (%d bytes) failed\n",len
+1);
6135 res
= ERROR_OUTOFMEMORY
;
6138 TRACE("<-- %d\n", res
);
6142 /***********************************************************************
6143 * HTTP_GetCustomHeaderIndex (internal)
6145 * Return index of custom header from header array
6148 static INT
HTTP_GetCustomHeaderIndex(http_request_t
*request
, LPCWSTR lpszField
,
6149 int requested_index
, BOOL request_only
)
6153 TRACE("%s, %d, %d\n", debugstr_w(lpszField
), requested_index
, request_only
);
6155 for (index
= 0; index
< request
->nCustHeaders
; index
++)
6157 if (strcmpiW(request
->custHeaders
[index
].lpszField
, lpszField
))
6160 if (request_only
&& !(request
->custHeaders
[index
].wFlags
& HDR_ISREQUEST
))
6163 if (!request_only
&& (request
->custHeaders
[index
].wFlags
& HDR_ISREQUEST
))
6166 if (requested_index
== 0)
6171 if (index
>= request
->nCustHeaders
)
6174 TRACE("Return: %d\n", index
);
6179 /***********************************************************************
6180 * HTTP_InsertCustomHeader (internal)
6182 * Insert header into array
6185 static DWORD
HTTP_InsertCustomHeader(http_request_t
*request
, LPHTTPHEADERW lpHdr
)
6188 LPHTTPHEADERW lph
= NULL
;
6190 TRACE("--> %s: %s\n", debugstr_w(lpHdr
->lpszField
), debugstr_w(lpHdr
->lpszValue
));
6191 count
= request
->nCustHeaders
+ 1;
6193 lph
= heap_realloc_zero(request
->custHeaders
, sizeof(HTTPHEADERW
) * count
);
6195 lph
= heap_alloc_zero(sizeof(HTTPHEADERW
) * count
);
6198 return ERROR_OUTOFMEMORY
;
6200 request
->custHeaders
= lph
;
6201 request
->custHeaders
[count
-1].lpszField
= heap_strdupW(lpHdr
->lpszField
);
6202 request
->custHeaders
[count
-1].lpszValue
= heap_strdupW(lpHdr
->lpszValue
);
6203 request
->custHeaders
[count
-1].wFlags
= lpHdr
->wFlags
;
6204 request
->custHeaders
[count
-1].wCount
= lpHdr
->wCount
;
6205 request
->nCustHeaders
++;
6207 return ERROR_SUCCESS
;
6211 /***********************************************************************
6212 * HTTP_DeleteCustomHeader (internal)
6214 * Delete header from array
6215 * If this function is called, the indexs may change.
6217 static BOOL
HTTP_DeleteCustomHeader(http_request_t
*request
, DWORD index
)
6219 if( request
->nCustHeaders
<= 0 )
6221 if( index
>= request
->nCustHeaders
)
6223 request
->nCustHeaders
--;
6225 heap_free(request
->custHeaders
[index
].lpszField
);
6226 heap_free(request
->custHeaders
[index
].lpszValue
);
6228 memmove( &request
->custHeaders
[index
], &request
->custHeaders
[index
+1],
6229 (request
->nCustHeaders
- index
)* sizeof(HTTPHEADERW
) );
6230 memset( &request
->custHeaders
[request
->nCustHeaders
], 0, sizeof(HTTPHEADERW
) );
6236 /***********************************************************************
6237 * HTTP_VerifyValidHeader (internal)
6239 * Verify the given header is not invalid for the given http request
6242 static BOOL
HTTP_VerifyValidHeader(http_request_t
*request
, LPCWSTR field
)
6244 /* Accept-Encoding is stripped from HTTP/1.0 requests. It is invalid */
6245 if (!strcmpW(request
->version
, g_szHttp1_0
) && !strcmpiW(field
, szAccept_Encoding
))
6246 return ERROR_HTTP_INVALID_HEADER
;
6248 return ERROR_SUCCESS
;
6251 /***********************************************************************
6252 * IsHostInProxyBypassList (@)
6257 BOOL WINAPI
IsHostInProxyBypassList(DWORD flags
, LPCSTR szHost
, DWORD length
)
6259 FIXME("STUB: flags=%d host=%s length=%d\n",flags
,szHost
,length
);