[WININET_WINETEST] Sync with Wine Staging 4.18. CORE-16441
[reactos.git] / modules / rostests / winetests / wininet / http.c
1 /*
2 * Wininet - HTTP tests
3 *
4 * Copyright 2002 Aric Stewart
5 * Copyright 2004 Mike McCormack
6 * Copyright 2005 Hans Leidekker
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <limits.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wininet.h"
31 #include "winineti.h"
32 #include "winsock2.h"
33
34 #include "wine/test.h"
35
36 /* Undocumented security flags */
37 #define _SECURITY_FLAG_CERT_REV_FAILED 0x00800000
38 #define _SECURITY_FLAG_CERT_INVALID_CA 0x01000000
39 #define _SECURITY_FLAG_CERT_INVALID_CN 0x02000000
40 #define _SECURITY_FLAG_CERT_INVALID_DATE 0x04000000
41
42 #define TEST_URL "http://test.winehq.org/tests/hello.html"
43
44 static BOOL first_connection_to_test_url = TRUE;
45
46 /* Adapted from dlls/urlmon/tests/protocol.c */
47
48 #define SET_EXPECT2(status, num) \
49 expect[status] = num
50
51 #define SET_EXPECT(status) \
52 SET_EXPECT2(status, 1)
53
54 #define SET_OPTIONAL2(status, num) \
55 optional[status] = num
56
57 #define SET_OPTIONAL(status) \
58 SET_OPTIONAL2(status, 1)
59
60 /* SET_WINE_ALLOW's should be used with an appropriate
61 * todo_wine CHECK_NOTIFIED at a later point in the code */
62 #define SET_WINE_ALLOW2(status, num) \
63 wine_allow[status] = num
64
65 #define SET_WINE_ALLOW(status) \
66 SET_WINE_ALLOW2(status, 1)
67
68 #define CHECK_EXPECT(status) \
69 do { \
70 if (!expect[status] && !optional[status] && wine_allow[status]) \
71 { \
72 todo_wine ok(expect[status], "unexpected status %d (%s)\n", status, \
73 status < MAX_INTERNET_STATUS && status_string[status] ? \
74 status_string[status] : "unknown"); \
75 wine_allow[status]--; \
76 } \
77 else \
78 { \
79 ok(expect[status] || optional[status], "unexpected status %d (%s)\n", status, \
80 status < MAX_INTERNET_STATUS && status_string[status] ? \
81 status_string[status] : "unknown"); \
82 if (expect[status]) expect[status]--; \
83 else if(optional[status]) optional[status]--; \
84 } \
85 notified[status]++; \
86 }while(0)
87
88 /* CLEAR_NOTIFIED used in cases when notification behavior
89 * differs between Windows versions */
90 #define CLEAR_NOTIFIED(status) \
91 expect[status] = optional[status] = wine_allow[status] = notified[status] = 0;
92
93 #define CHECK_NOTIFIED2(status, num) \
94 do { \
95 ok(notified[status] + optional[status] == (num), \
96 "expected status %d (%s) %d times, received %d times\n", \
97 status, status < MAX_INTERNET_STATUS && status_string[status] ? \
98 status_string[status] : "unknown", (num), notified[status]); \
99 CLEAR_NOTIFIED(status); \
100 }while(0)
101
102 #define CHECK_NOTIFIED(status) \
103 CHECK_NOTIFIED2(status, 1)
104
105 #define CHECK_NOT_NOTIFIED(status) \
106 CHECK_NOTIFIED2(status, 0)
107
108 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
109 static int expect[MAX_INTERNET_STATUS], optional[MAX_INTERNET_STATUS],
110 wine_allow[MAX_INTERNET_STATUS], notified[MAX_INTERNET_STATUS];
111 static const char *status_string[MAX_INTERNET_STATUS];
112
113 static HANDLE complete_event, conn_close_event, conn_wait_event, server_req_rec_event, request_sent_event;
114 static DWORD req_error;
115 static BOOL is_ie7plus = TRUE;
116
117 #define TESTF_REDIRECT 0x01
118 #define TESTF_COMPRESSED 0x02
119 #define TESTF_CHUNKED 0x04
120
121 typedef struct {
122 const char *url;
123 const char *redirected_url;
124 const char *host;
125 const char *path;
126 const char *headers;
127 DWORD flags;
128 const char *post_data;
129 const char *content;
130 } test_data_t;
131
132 static const test_data_t test_data[] = {
133 {
134 "http://test.winehq.org/tests/data.php",
135 "http://test.winehq.org/tests/data.php",
136 "test.winehq.org",
137 "/tests/data.php",
138 "",
139 TESTF_CHUNKED
140 },
141 {
142 "http://test.winehq.org/tests/redirect",
143 "http://test.winehq.org/tests/hello.html",
144 "test.winehq.org",
145 "/tests/redirect",
146 "",
147 TESTF_REDIRECT
148 },
149 {
150 "http://test.winehq.org/tests/gzip.php",
151 "http://test.winehq.org/tests/gzip.php",
152 "test.winehq.org",
153 "/tests/gzip.php",
154 "Accept-Encoding: gzip, deflate",
155 TESTF_COMPRESSED
156 },
157 {
158 "http://test.winehq.org/tests/post.php",
159 "http://test.winehq.org/tests/post.php",
160 "test.winehq.org",
161 "/tests/post.php",
162 "Content-Type: application/x-www-form-urlencoded",
163 0,
164 "mode=Test",
165 "mode => Test\n"
166 }
167 };
168
169 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
170 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackW)(HINTERNET ,INTERNET_STATUS_CALLBACK);
171 static BOOL (WINAPI *pInternetGetSecurityInfoByURLA)(LPSTR,PCCERT_CHAIN_CONTEXT*,DWORD*);
172
173 static int strcmp_wa(LPCWSTR strw, const char *stra)
174 {
175 WCHAR buf[512];
176 MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, ARRAY_SIZE(buf));
177 return lstrcmpW(strw, buf);
178 }
179
180 static BOOL proxy_active(void)
181 {
182 HKEY internet_settings;
183 DWORD proxy_enable;
184 DWORD size;
185
186 if (RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
187 0, KEY_QUERY_VALUE, &internet_settings) != ERROR_SUCCESS)
188 return FALSE;
189
190 size = sizeof(DWORD);
191 if (RegQueryValueExA(internet_settings, "ProxyEnable", NULL, NULL, (LPBYTE) &proxy_enable, &size) != ERROR_SUCCESS)
192 proxy_enable = 0;
193
194 RegCloseKey(internet_settings);
195
196 return proxy_enable != 0;
197 }
198
199 static void init_events(void)
200 {
201 complete_event = CreateEventW(NULL, FALSE, FALSE, NULL);
202 conn_close_event = CreateEventW(NULL, FALSE, FALSE, NULL);
203 conn_wait_event = CreateEventW(NULL, FALSE, FALSE, NULL);
204 server_req_rec_event = CreateEventW(NULL, FALSE, FALSE, NULL);
205 request_sent_event = CreateEventW(NULL, FALSE, FALSE, NULL);
206 }
207
208 static void free_events(void)
209 {
210 CloseHandle(complete_event);
211 CloseHandle(conn_close_event);
212 CloseHandle(conn_wait_event);
213 CloseHandle(server_req_rec_event);
214 CloseHandle(request_sent_event);
215 }
216
217 static void reset_events(void)
218 {
219 ResetEvent(complete_event);
220 ResetEvent(conn_close_event);
221 ResetEvent(conn_wait_event);
222 ResetEvent(server_req_rec_event);
223 ResetEvent(request_sent_event);
224 }
225
226 #define test_status_code(a,b) _test_status_code(__LINE__,a,b, FALSE)
227 #define test_status_code_todo(a,b) _test_status_code(__LINE__,a,b, TRUE)
228 static void _test_status_code(unsigned line, HINTERNET req, DWORD excode, BOOL is_todo)
229 {
230 DWORD code, size, index;
231 char exbuf[12], bufa[10];
232 WCHAR bufw[10];
233 BOOL res;
234
235 code = 0xdeadbeef;
236 size = sizeof(code);
237 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &code, &size, NULL);
238 ok_(__FILE__,line)(res, "[1] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE|number) failed: %u\n", GetLastError());
239 todo_wine_if (is_todo)
240 ok_(__FILE__,line)(code == excode, "code = %d, expected %d\n", code, excode);
241 ok_(__FILE__,line)(size == sizeof(code), "size = %u\n", size);
242
243 code = 0xdeadbeef;
244 index = 0;
245 size = sizeof(code);
246 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &code, &size, &index);
247 ok_(__FILE__,line)(res, "[2] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE|number index) failed: %u\n", GetLastError());
248 ok_(__FILE__,line)(!index, "index = %d, expected 0\n", index);
249 ok_(__FILE__,line)(size == sizeof(code), "size = %u\n", size);
250
251 sprintf(exbuf, "%u", excode);
252
253 size = sizeof(bufa);
254 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE, bufa, &size, NULL);
255 ok_(__FILE__,line)(res, "[3] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
256 todo_wine_if (is_todo)
257 ok_(__FILE__,line)(!strcmp(bufa, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
258 ok_(__FILE__,line)(size == strlen(exbuf), "unexpected size %d for \"%s\"\n", size, exbuf);
259
260 size = 0;
261 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE, NULL, &size, NULL);
262 ok_(__FILE__,line)(!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
263 "[4] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
264 ok_(__FILE__,line)(size == strlen(exbuf)+1, "unexpected size %d for \"%s\"\n", size, exbuf);
265
266 size = sizeof(bufw);
267 res = HttpQueryInfoW(req, HTTP_QUERY_STATUS_CODE, bufw, &size, NULL);
268 ok_(__FILE__,line)(res, "[5] HttpQueryInfoW(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
269 todo_wine_if (is_todo)
270 ok_(__FILE__,line)(!strcmp_wa(bufw, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
271 ok_(__FILE__,line)(size == strlen(exbuf)*sizeof(WCHAR), "unexpected size %d for \"%s\"\n", size, exbuf);
272
273 size = 0;
274 res = HttpQueryInfoW(req, HTTP_QUERY_STATUS_CODE, bufw, &size, NULL);
275 ok_(__FILE__,line)(!res && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
276 "[6] HttpQueryInfoW(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
277 ok_(__FILE__,line)(size == (strlen(exbuf)+1)*sizeof(WCHAR), "unexpected size %d for \"%s\"\n", size, exbuf);
278
279 if(0) {
280 size = sizeof(bufw);
281 res = HttpQueryInfoW(req, HTTP_QUERY_STATUS_CODE, NULL, &size, NULL);
282 ok(!res && GetLastError() == ERROR_INVALID_PARAMETER, "HttpQueryInfo(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
283 ok(size == sizeof(bufw), "unexpected size %d\n", size);
284 }
285
286 code = 0xdeadbeef;
287 index = 1;
288 size = sizeof(code);
289 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &code, &size, &index);
290 ok_(__FILE__,line)(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
291 "[7] HttpQueryInfoA failed: %x(%d)\n", res, GetLastError());
292
293 code = 0xdeadbeef;
294 size = sizeof(code);
295 res = HttpQueryInfoA(req, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_REQUEST_HEADERS, &code, &size, NULL);
296 ok_(__FILE__,line)(!res && GetLastError() == ERROR_HTTP_INVALID_QUERY_REQUEST,
297 "[8] HttpQueryInfoA failed: %x(%d)\n", res, GetLastError());
298 }
299
300 #define test_request_flags(a,b) _test_request_flags(__LINE__,a,b,FALSE)
301 #define test_request_flags_todo(a,b) _test_request_flags(__LINE__,a,b,TRUE)
302 static void _test_request_flags(unsigned line, HINTERNET req, DWORD exflags, BOOL is_todo)
303 {
304 DWORD flags, size;
305 BOOL res;
306
307 flags = 0xdeadbeef;
308 size = sizeof(flags);
309 res = InternetQueryOptionW(req, INTERNET_OPTION_REQUEST_FLAGS, &flags, &size);
310 ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_REQUEST_FLAGS) failed: %u\n", GetLastError());
311
312 /* FIXME: Remove once we have INTERNET_REQFLAG_CACHE_WRITE_DISABLED implementation */
313 flags &= ~INTERNET_REQFLAG_CACHE_WRITE_DISABLED;
314 todo_wine_if (is_todo)
315 ok_(__FILE__,line)(flags == exflags, "flags = %x, expected %x\n", flags, exflags);
316 }
317
318 #define test_request_url(a,b) _test_request_url(__LINE__,a,b)
319 static void _test_request_url(unsigned line, HINTERNET req, const char *expected_url)
320 {
321 char buf[INTERNET_MAX_URL_LENGTH];
322 DWORD size = sizeof(buf);
323 BOOL res;
324
325 res = InternetQueryOptionA(req, INTERNET_OPTION_URL, buf, &size);
326 ok_(__FILE__,line)(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
327 ok_(__FILE__,line)(size == strlen(expected_url), "size = %u\n", size);
328 ok_(__FILE__,line)(!strcmp(buf, expected_url), "unexpected URL %s, expected %s\n", buf, expected_url);
329 }
330
331 #define test_http_version(a) _test_http_version(__LINE__,a)
332 static void _test_http_version(unsigned line, HINTERNET req)
333 {
334 HTTP_VERSION_INFO v = {0xdeadbeef, 0xdeadbeef};
335 DWORD size;
336 BOOL res;
337
338 size = sizeof(v);
339 res = InternetQueryOptionW(req, INTERNET_OPTION_HTTP_VERSION, &v, &size);
340 ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_HTTP_VERSION) failed: %u\n", GetLastError());
341 ok_(__FILE__,line)(v.dwMajorVersion == 1, "dwMajorVersion = %d\n", v.dwMajorVersion);
342 ok_(__FILE__,line)(v.dwMinorVersion == 1, "dwMinorVersion = %d\n", v.dwMinorVersion);
343 }
344
345 static int close_handle_cnt;
346
347 static VOID WINAPI callback(
348 HINTERNET hInternet,
349 DWORD_PTR dwContext,
350 DWORD dwInternetStatus,
351 LPVOID lpvStatusInformation,
352 DWORD dwStatusInformationLength
353 )
354 {
355 ros_skip_flaky
356 CHECK_EXPECT(dwInternetStatus);
357 switch (dwInternetStatus)
358 {
359 case INTERNET_STATUS_RESOLVING_NAME:
360 if(winetest_debug > 1)
361 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
362 GetCurrentThreadId(), hInternet, dwContext,
363 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
364 *(LPSTR)lpvStatusInformation = '\0';
365 break;
366 case INTERNET_STATUS_NAME_RESOLVED:
367 if(winetest_debug > 1)
368 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
369 GetCurrentThreadId(), hInternet, dwContext,
370 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
371 *(LPSTR)lpvStatusInformation = '\0';
372 break;
373 case INTERNET_STATUS_CONNECTING_TO_SERVER:
374 if(winetest_debug > 1)
375 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
376 GetCurrentThreadId(), hInternet, dwContext,
377 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
378 ok(dwStatusInformationLength == strlen(lpvStatusInformation)+1, "unexpected size %u\n",
379 dwStatusInformationLength);
380 *(LPSTR)lpvStatusInformation = '\0';
381 break;
382 case INTERNET_STATUS_CONNECTED_TO_SERVER:
383 if(winetest_debug > 1)
384 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
385 GetCurrentThreadId(), hInternet, dwContext,
386 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
387 ok(dwStatusInformationLength == strlen(lpvStatusInformation)+1, "unexpected size %u\n",
388 dwStatusInformationLength);
389 *(LPSTR)lpvStatusInformation = '\0';
390 break;
391 case INTERNET_STATUS_SENDING_REQUEST:
392 if(winetest_debug > 1)
393 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
394 GetCurrentThreadId(), hInternet, dwContext,
395 lpvStatusInformation,dwStatusInformationLength);
396 break;
397 case INTERNET_STATUS_REQUEST_SENT:
398 ok(dwStatusInformationLength == sizeof(DWORD),
399 "info length should be sizeof(DWORD) instead of %d\n",
400 dwStatusInformationLength);
401 if(winetest_debug > 1)
402 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
403 GetCurrentThreadId(), hInternet, dwContext,
404 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
405 break;
406 case INTERNET_STATUS_RECEIVING_RESPONSE:
407 if(winetest_debug > 1)
408 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
409 GetCurrentThreadId(), hInternet, dwContext,
410 lpvStatusInformation,dwStatusInformationLength);
411 break;
412 case INTERNET_STATUS_RESPONSE_RECEIVED:
413 ok(dwStatusInformationLength == sizeof(DWORD),
414 "info length should be sizeof(DWORD) instead of %d\n",
415 dwStatusInformationLength);
416 if(winetest_debug > 1)
417 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
418 GetCurrentThreadId(), hInternet, dwContext,
419 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
420 break;
421 case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
422 if(winetest_debug > 1)
423 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
424 GetCurrentThreadId(), hInternet,dwContext,
425 lpvStatusInformation,dwStatusInformationLength);
426 break;
427 case INTERNET_STATUS_PREFETCH:
428 if(winetest_debug > 1)
429 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
430 GetCurrentThreadId(), hInternet, dwContext,
431 lpvStatusInformation,dwStatusInformationLength);
432 break;
433 case INTERNET_STATUS_CLOSING_CONNECTION:
434 if(winetest_debug > 1)
435 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
436 GetCurrentThreadId(), hInternet, dwContext,
437 lpvStatusInformation,dwStatusInformationLength);
438 break;
439 case INTERNET_STATUS_CONNECTION_CLOSED:
440 if(winetest_debug > 1)
441 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
442 GetCurrentThreadId(), hInternet, dwContext,
443 lpvStatusInformation,dwStatusInformationLength);
444 break;
445 case INTERNET_STATUS_HANDLE_CREATED:
446 ok(dwStatusInformationLength == sizeof(HINTERNET),
447 "info length should be sizeof(HINTERNET) instead of %d\n",
448 dwStatusInformationLength);
449 if(winetest_debug > 1)
450 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
451 GetCurrentThreadId(), hInternet, dwContext,
452 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
453 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
454 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
455 break;
456 case INTERNET_STATUS_HANDLE_CLOSING:
457 ok(dwStatusInformationLength == sizeof(HINTERNET),
458 "info length should be sizeof(HINTERNET) instead of %d\n",
459 dwStatusInformationLength);
460 if(winetest_debug > 1)
461 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
462 GetCurrentThreadId(), hInternet, dwContext,
463 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
464 if(!InterlockedDecrement(&close_handle_cnt))
465 SetEvent(complete_event);
466 break;
467 case INTERNET_STATUS_REQUEST_COMPLETE:
468 {
469 INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
470 ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
471 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
472 dwStatusInformationLength);
473 ok(iar->dwResult == 1 || iar->dwResult == 0, "iar->dwResult = %ld\n", iar->dwResult);
474 if(winetest_debug > 1)
475 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
476 GetCurrentThreadId(), hInternet, dwContext,
477 iar->dwResult,iar->dwError,dwStatusInformationLength);
478 req_error = iar->dwError;
479 if(!close_handle_cnt)
480 SetEvent(complete_event);
481 break;
482 }
483 case INTERNET_STATUS_REDIRECT:
484 if(winetest_debug > 1)
485 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
486 GetCurrentThreadId(), hInternet, dwContext,
487 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
488 *(LPSTR)lpvStatusInformation = '\0';
489 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
490 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
491 break;
492 case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
493 if(winetest_debug > 1)
494 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
495 GetCurrentThreadId(), hInternet, dwContext,
496 lpvStatusInformation, dwStatusInformationLength);
497 break;
498 default:
499 if(winetest_debug > 1)
500 trace("%04x:Callback %p 0x%lx %d %p %d\n",
501 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
502 lpvStatusInformation, dwStatusInformationLength);
503 }
504 }
505
506 typedef struct {
507 HINTERNET session;
508 HINTERNET connection;
509 HINTERNET request;
510 } test_request_t;
511
512 #define open_simple_request(a,b,c,d,e) _open_simple_request(__LINE__,a,b,c,d,e)
513 static void _open_simple_request(unsigned line, test_request_t *req, const char *host,
514 int port, const char *verb, const char *url)
515 {
516 req->session = InternetOpenA(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
517 ok_(__FILE__,line)(req->session != NULL, "InternetOpenA failed: %u\n", GetLastError());
518
519 req->connection = InternetConnectA(req->session, host, port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
520 ok_(__FILE__,line)(req->connection != NULL, "InternetConnectA failed: %u\n", GetLastError());
521
522 req->request = HttpOpenRequestA(req->connection, verb, url, NULL, NULL, NULL, 0, 0);
523 ok_(__FILE__,line)(req->request != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
524 }
525
526 #define close_request(a) _close_request(__LINE__,a)
527 static void _close_request(unsigned line, test_request_t *req)
528 {
529 BOOL ret;
530
531 ret = InternetCloseHandle(req->request);
532 ok_(__FILE__,line)(ret, "InternetCloseHandle(request) failed: %u\n", GetLastError());
533 ret = InternetCloseHandle(req->connection);
534 ok_(__FILE__,line)(ret, "InternetCloseHandle(connection) failed: %u\n", GetLastError());
535 ret = InternetCloseHandle(req->session);
536 ok_(__FILE__,line)(ret, "InternetCloseHandle(session) failed: %u\n", GetLastError());
537 }
538
539 #define receive_simple_request(a,b,c) _receive_simple_request(__LINE__,a,b,c)
540 static DWORD _receive_simple_request(unsigned line, HINTERNET req, char *buf, size_t buf_size)
541 {
542 DWORD read = 0;
543 BOOL ret;
544
545 ret = InternetReadFile(req, buf, buf_size, &read);
546 ok_(__FILE__,line)(ret, "InternetReadFile failed: %u\n", GetLastError());
547
548 return read;
549 }
550
551 static void close_async_handle(HINTERNET handle, int handle_cnt)
552 {
553 BOOL res;
554
555 close_handle_cnt = handle_cnt;
556
557 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, handle_cnt);
558 res = InternetCloseHandle(handle);
559 ok(res, "InternetCloseHandle failed: %u\n", GetLastError());
560 WaitForSingleObject(complete_event, INFINITE);
561 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, handle_cnt);
562 }
563
564 static void InternetReadFile_test(int flags, const test_data_t *test)
565 {
566 char *post_data = NULL;
567 BOOL res, on_async = TRUE;
568 CHAR buffer[4000];
569 WCHAR wbuffer[4000];
570 DWORD length, length2, index, exlen = 0, post_len = 0;
571 const char *types[2] = { "*", NULL };
572 HINTERNET hi, hic = 0, hor = 0;
573
574 trace("Starting InternetReadFile test with flags 0x%x on url %s\n",flags,test->url);
575 reset_events();
576
577 trace("InternetOpenA <--\n");
578 hi = InternetOpenA((test->flags & TESTF_COMPRESSED) ? "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" : "",
579 INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
580 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
581 trace("InternetOpenA -->\n");
582
583 if (hi == 0x0) goto abort;
584
585 pInternetSetStatusCallbackA(hi,&callback);
586
587 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
588
589 trace("InternetConnectA <--\n");
590 hic=InternetConnectA(hi, test->host, INTERNET_INVALID_PORT_NUMBER,
591 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
592 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
593 trace("InternetConnectA -->\n");
594
595 if (hic == 0x0) goto abort;
596
597 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
598 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
599
600 trace("HttpOpenRequestA <--\n");
601 hor = HttpOpenRequestA(hic, test->post_data ? "POST" : "GET", test->path, NULL, NULL, types,
602 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD,
603 0xdeadbead);
604 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
605 /*
606 * If the internet name can't be resolved we are probably behind
607 * a firewall or in some other way not directly connected to the
608 * Internet. Not enough reason to fail the test. Just ignore and
609 * abort.
610 */
611 } else {
612 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
613 }
614 trace("HttpOpenRequestA -->\n");
615
616 if (hor == 0x0) goto abort;
617
618 test_request_flags(hor, INTERNET_REQFLAG_NO_HEADERS);
619 test_request_url(hor, test->url);
620
621 length = sizeof(buffer);
622 res = HttpQueryInfoA(hor, HTTP_QUERY_RAW_HEADERS, buffer, &length, 0x0);
623 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
624 ok(length == 0 || (length == 1 && !*buffer) /* win10 */, "HTTP_QUERY_RAW_HEADERS: expected length 0, but got %d\n", length);
625 ok(!strcmp(buffer, ""), "HTTP_QUERY_RAW_HEADERS: expected string \"\", but got \"%s\"\n", buffer);
626
627 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
628 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
629 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
630 SET_OPTIONAL2(INTERNET_STATUS_COOKIE_SENT,2);
631 SET_OPTIONAL2(INTERNET_STATUS_COOKIE_RECEIVED,2);
632 if (first_connection_to_test_url)
633 {
634 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
635 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
636 }
637 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
638 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
639 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
640 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
641 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
642 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
643 if(test->flags & TESTF_REDIRECT) {
644 SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
645 SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
646 }
647 SET_EXPECT(INTERNET_STATUS_REDIRECT);
648 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
649 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
650 if (flags & INTERNET_FLAG_ASYNC)
651 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
652
653 if(test->flags & TESTF_COMPRESSED) {
654 BOOL b = TRUE;
655
656 res = InternetSetOptionA(hor, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
657 ok(res || broken(!res && GetLastError() == ERROR_INTERNET_INVALID_OPTION),
658 "InternetSetOption failed: %u\n", GetLastError());
659 if(!res)
660 goto abort;
661 }
662
663 test_status_code(hor, 0);
664
665 trace("HttpSendRequestA -->\n");
666 if(test->post_data) {
667 post_len = strlen(test->post_data);
668 post_data = HeapAlloc(GetProcessHeap(), 0, post_len);
669 memcpy(post_data, test->post_data, post_len);
670 }
671 SetLastError(0xdeadbeef);
672 res = HttpSendRequestA(hor, test->headers, -1, post_data, post_len);
673 if (flags & INTERNET_FLAG_ASYNC)
674 ok(!res && (GetLastError() == ERROR_IO_PENDING),
675 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
676 else
677 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
678 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
679 trace("HttpSendRequestA <--\n");
680
681 if (flags & INTERNET_FLAG_ASYNC) {
682 WaitForSingleObject(complete_event, INFINITE);
683 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
684 }
685 HeapFree(GetProcessHeap(), 0, post_data);
686
687 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
688 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_RECEIVED);
689 if (first_connection_to_test_url)
690 {
691 if (! proxy_active())
692 {
693 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
694 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
695 }
696 else
697 {
698 CLEAR_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
699 CLEAR_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
700 }
701 }
702 else
703 {
704 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
705 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
706 }
707 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
708 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
709 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
710 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
711 if(test->flags & TESTF_REDIRECT)
712 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
713 if (flags & INTERNET_FLAG_ASYNC)
714 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
715 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
716 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
717 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
718
719 test_request_flags(hor, 0);
720
721 length = 100;
722 res = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
723 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed with error %d\n", GetLastError());
724
725 length = sizeof(buffer)-2;
726 memset(buffer, 0x77, sizeof(buffer));
727 SetLastError(0xdeadbeef);
728 res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
729 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
730 ok(GetLastError() == 0 ||
731 broken(GetLastError() == 0xdeadbeef /* XP/W2K3 */), "Last Error not reset %u\n", GetLastError());
732 /* show that the function writes data past the length returned */
733 ok(buffer[length-2], "Expected any header character, got 0x00\n");
734 ok(!buffer[length-1], "Expected 0x00, got %02X\n", buffer[length-1]);
735 ok(!buffer[length], "Expected 0x00, got %02X\n", buffer[length]);
736 ok(buffer[length+1] == 0x77, "Expected 0x77, got %02X\n", buffer[length+1]);
737
738 length2 = length;
739 res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length2,0x0);
740 ok(!res, "Expected 0x00, got %d\n", res);
741 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
742 ok(length2 == length+1, "Expected %d, got %d\n", length+1, length2);
743 /* the in length of the buffer must be +1 but the length returned does not count this */
744 length2 = length+1;
745 memset(buffer, 0x77, sizeof(buffer));
746 res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length2,0x0);
747 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
748 ok(buffer[length2] == 0x00, "Expected 0x00, got %02X\n", buffer[length2]);
749 ok(buffer[length2+1] == 0x77, "Expected 0x77, got %02X\n", buffer[length2+1]);
750 ok(length2 == length, "Value should not have changed: %d != %d\n", length2, length);
751
752 length = sizeof(wbuffer)-2*sizeof(WCHAR);
753 memset(wbuffer, 0x77, sizeof(wbuffer));
754 res = HttpQueryInfoW(hor, HTTP_QUERY_RAW_HEADERS, wbuffer, &length, 0x0);
755 ok(res, "HttpQueryInfoW(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
756 ok(length % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %d.\n", length);
757 length /= sizeof(WCHAR);
758 /* show that the function writes data past the length returned */
759 ok(wbuffer[length-2], "Expected any header character, got 0x0000\n");
760 ok(!wbuffer[length-1], "Expected 0x0000, got %04X\n", wbuffer[length-1]);
761 ok(!wbuffer[length], "Expected 0x0000, got %04X\n", wbuffer[length]);
762 ok(wbuffer[length+1] == 0x7777 || broken(wbuffer[length+1] != 0x7777),
763 "Expected 0x7777, got %04X\n", wbuffer[length+1]);
764
765 length2 = length*sizeof(WCHAR);
766 res = HttpQueryInfoW(hor,HTTP_QUERY_RAW_HEADERS,wbuffer,&length2,0x0);
767 ok(!res, "Expected 0x00, got %d\n", res);
768 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
769 ok(length2 % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %d.\n", length2);
770 length2 /= sizeof(WCHAR);
771 ok(length2 == length+1, "Expected %d, got %d\n", length+1, length2);
772 /* the in length of the buffer must be +1 but the length returned does not count this */
773 length2 = (length+1)*sizeof(WCHAR);
774 memset(wbuffer, 0x77, sizeof(wbuffer));
775 res = HttpQueryInfoW(hor,HTTP_QUERY_RAW_HEADERS,wbuffer,&length2,0x0);
776 ok(res, "HttpQueryInfoW(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
777 ok(length2 % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %d.\n", length2);
778 length2 /= sizeof(WCHAR);
779 ok(!wbuffer[length2], "Expected 0x0000, got %04X\n", wbuffer[length2]);
780 ok(wbuffer[length2+1] == 0x7777, "Expected 0x7777, got %04X\n", wbuffer[length2+1]);
781 ok(length2 == length, "Value should not have changed: %d != %d\n", length2, length);
782
783 test_request_url(hor, test->redirected_url);
784
785 index = 0;
786 length = 0;
787 SetLastError(0xdeadbeef);
788 ok(HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,NULL,&length,&index) == FALSE,"Query worked\n");
789 if(test->flags & TESTF_COMPRESSED)
790 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
791 "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", GetLastError());
792 ok(index == 0, "Index was incremented\n");
793
794 index = 0;
795 length = 16;
796 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,&index);
797 trace("Option HTTP_QUERY_CONTENT_LENGTH -> %i %s (%u)\n",res,buffer,GetLastError());
798 if(test->flags & TESTF_COMPRESSED)
799 ok(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
800 "expected ERROR_HTTP_HEADER_NOT_FOUND, got %x (%u)\n", res, GetLastError());
801 ok(!res || index == 1, "Index was not incremented although result is %x (index = %u)\n", res, index);
802
803 length = 100;
804 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
805 buffer[length]=0;
806 trace("Option HTTP_QUERY_CONTENT_TYPE -> %i %s\n",res,buffer);
807
808 length = 100;
809 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_ENCODING,buffer,&length,0x0);
810 buffer[length]=0;
811 trace("Option HTTP_QUERY_CONTENT_ENCODING -> %i %s\n",res,buffer);
812
813 SetLastError(0xdeadbeef);
814 res = InternetReadFile(NULL, buffer, 100, &length);
815 ok(!res, "InternetReadFile should have failed\n");
816 ok(GetLastError() == ERROR_INVALID_HANDLE,
817 "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
818 GetLastError());
819
820 length = 100;
821 if(winetest_debug > 1)
822 trace("Entering Query loop\n");
823
824 while (TRUE)
825 {
826 if (flags & INTERNET_FLAG_ASYNC)
827 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
828
829 /* IE11 calls those in InternetQueryDataAvailable call. */
830 SET_OPTIONAL(INTERNET_STATUS_RECEIVING_RESPONSE);
831 SET_OPTIONAL(INTERNET_STATUS_RESPONSE_RECEIVED);
832
833 length = 0;
834 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
835
836 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
837
838 if (flags & INTERNET_FLAG_ASYNC)
839 {
840 if (res)
841 {
842 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
843 if(exlen) {
844 ok(length >= exlen, "length %u < exlen %u\n", length, exlen);
845 exlen = 0;
846 }
847 }
848 else if (GetLastError() == ERROR_IO_PENDING)
849 {
850 if(winetest_debug > 1)
851 trace("pending\n");
852 /* on some tests, InternetQueryDataAvailable returns non-zero length and ERROR_IO_PENDING */
853 if(!(test->flags & TESTF_CHUNKED))
854 ok(!length, "InternetQueryDataAvailable returned ERROR_IO_PENDING and %u length\n", length);
855 WaitForSingleObject(complete_event, INFINITE);
856 exlen = length;
857 ok(exlen, "length = 0\n");
858 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
859 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
860 ok(req_error, "req_error = 0\n");
861 continue;
862 }else {
863 ok(0, "InternetQueryDataAvailable failed: %u\n", GetLastError());
864 }
865 }else {
866 ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
867 }
868 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
869
870 if(winetest_debug > 1)
871 trace("length %u\n", length);
872 if(test->flags & TESTF_CHUNKED)
873 ok(length <= 8192, "length = %d, expected <= 8192\n", length);
874 if (length)
875 {
876 char *buffer;
877 buffer = HeapAlloc(GetProcessHeap(),0,length+1);
878
879 res = InternetReadFile(hor,buffer,length,&length);
880
881 buffer[length]=0;
882
883 if(winetest_debug > 1)
884 trace("ReadFile -> %s %i\n", res ? "TRUE" : "FALSE", length);
885
886 if(test->content)
887 ok(!strcmp(buffer, test->content), "buffer = '%s', expected '%s'\n", buffer, test->content);
888 HeapFree(GetProcessHeap(),0,buffer);
889 }else {
890 ok(!on_async, "Returned zero size in response to request complete\n");
891 break;
892 }
893 on_async = FALSE;
894 }
895 if(test->flags & TESTF_REDIRECT) {
896 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
897 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
898 }
899 abort:
900 if(winetest_debug > 1)
901 trace("aborting\n");
902 close_async_handle(hi, 2);
903 first_connection_to_test_url = FALSE;
904 }
905
906 static void InternetReadFile_chunked_test(void)
907 {
908 BOOL res;
909 CHAR buffer[4000];
910 DWORD length, got;
911 const char *types[2] = { "*", NULL };
912 HINTERNET hi, hic = 0, hor = 0;
913
914 trace("Starting InternetReadFile chunked test\n");
915
916 trace("InternetOpenA <--\n");
917 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
918 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
919 trace("InternetOpenA -->\n");
920
921 if (hi == 0x0) goto abort;
922
923 trace("InternetConnectA <--\n");
924 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
925 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
926 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
927 trace("InternetConnectA -->\n");
928
929 if (hic == 0x0) goto abort;
930
931 trace("HttpOpenRequestA <--\n");
932 hor = HttpOpenRequestA(hic, "GET", "/tests/chunked", NULL, NULL, types,
933 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD,
934 0xdeadbead);
935 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
936 /*
937 * If the internet name can't be resolved we are probably behind
938 * a firewall or in some other way not directly connected to the
939 * Internet. Not enough reason to fail the test. Just ignore and
940 * abort.
941 */
942 } else {
943 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
944 }
945 trace("HttpOpenRequestA -->\n");
946
947 if (hor == 0x0) goto abort;
948
949 trace("HttpSendRequestA -->\n");
950 SetLastError(0xdeadbeef);
951 res = HttpSendRequestA(hor, "", -1, NULL, 0);
952 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
953 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
954 trace("HttpSendRequestA <--\n");
955
956 test_request_flags(hor, 0);
957
958 length = 100;
959 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
960 buffer[length]=0;
961 trace("Option CONTENT_TYPE -> %i %s\n",res,buffer);
962
963 SetLastError( 0xdeadbeef );
964 length = 100;
965 res = HttpQueryInfoA(hor,HTTP_QUERY_TRANSFER_ENCODING,buffer,&length,0x0);
966 buffer[length]=0;
967 trace("Option TRANSFER_ENCODING -> %i %s\n",res,buffer);
968 ok( res || ( proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
969 "Failed to get TRANSFER_ENCODING option, error %u\n", GetLastError() );
970 ok( !strcmp( buffer, "chunked" ) || ( ! res && proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
971 "Wrong transfer encoding '%s'\n", buffer );
972
973 SetLastError( 0xdeadbeef );
974 length = 16;
975 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
976 ok( !res, "Found CONTENT_LENGTH option '%s'\n", buffer );
977 ok( GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "Wrong error %u\n", GetLastError() );
978
979 length = 100;
980 trace("Entering Query loop\n");
981
982 while (TRUE)
983 {
984 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
985 ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
986 ok(res, "InternetQueryDataAvailable failed, error %d\n", GetLastError());
987 trace("got %u available\n",length);
988 if (length)
989 {
990 char *buffer = HeapAlloc(GetProcessHeap(),0,length+1);
991
992 SetLastError(0xdeadbeef);
993 res = InternetReadFile(hor,buffer,length,&got);
994 ok(GetLastError() == 0 ||
995 broken(GetLastError() == 0xdeadbeef /* XP/W2K3 */), "Last Error not reset %u\n", GetLastError());
996
997 buffer[got]=0;
998 trace("ReadFile -> %i %i\n",res,got);
999 ok( length == got, "only got %u of %u available\n", got, length );
1000 ok( buffer[got-1] == '\n', "received partial line '%s'\n", buffer );
1001
1002 HeapFree(GetProcessHeap(),0,buffer);
1003 if (!got) break;
1004 }
1005 if (length == 0)
1006 {
1007 got = 0xdeadbeef;
1008 SetLastError(0xdeadbeef);
1009 res = InternetReadFile( hor, buffer, 1, &got );
1010 ok( res, "InternetReadFile failed: %u\n", GetLastError() );
1011 ok(GetLastError() == 0 ||
1012 broken(GetLastError() == 0xdeadbeef /* XP/W2K3 */), "Last Error not reset %u\n", GetLastError());
1013 ok( !got, "got %u\n", got );
1014 break;
1015 }
1016 }
1017 abort:
1018 trace("aborting\n");
1019 if (hor != 0x0) {
1020 res = InternetCloseHandle(hor);
1021 ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
1022 }
1023 if (hi != 0x0) {
1024 res = InternetCloseHandle(hi);
1025 ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
1026 }
1027 }
1028
1029 static void InternetReadFileExA_test(int flags)
1030 {
1031 DWORD rc;
1032 DWORD length;
1033 const char *types[2] = { "*", NULL };
1034 HINTERNET hi, hic = 0, hor = 0;
1035 INTERNET_BUFFERSA inetbuffers;
1036
1037 trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
1038 reset_events();
1039
1040 trace("InternetOpenA <--\n");
1041 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
1042 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
1043 trace("InternetOpenA -->\n");
1044
1045 if (hi == 0x0) goto abort;
1046
1047 pInternetSetStatusCallbackA(hi,&callback);
1048
1049 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
1050
1051 trace("InternetConnectA <--\n");
1052 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
1053 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
1054 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
1055 trace("InternetConnectA -->\n");
1056
1057 if (hic == 0x0) goto abort;
1058
1059 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
1060 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
1061
1062 trace("HttpOpenRequestA <--\n");
1063 hor = HttpOpenRequestA(hic, "GET", "/tests/redirect", NULL, NULL, types,
1064 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD,
1065 0xdeadbead);
1066 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
1067 /*
1068 * If the internet name can't be resolved we are probably behind
1069 * a firewall or in some other way not directly connected to the
1070 * Internet. Not enough reason to fail the test. Just ignore and
1071 * abort.
1072 */
1073 } else {
1074 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
1075 }
1076 trace("HttpOpenRequestA -->\n");
1077
1078 if (hor == 0x0) goto abort;
1079
1080 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
1081 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
1082 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
1083 if (first_connection_to_test_url)
1084 {
1085 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
1086 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
1087 }
1088 SET_OPTIONAL2(INTERNET_STATUS_COOKIE_SENT, 2);
1089 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
1090 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
1091 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
1092 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
1093 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
1094 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
1095 SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
1096 SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
1097 SET_EXPECT(INTERNET_STATUS_REDIRECT);
1098 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
1099 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
1100 if (flags & INTERNET_FLAG_ASYNC)
1101 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
1102 else
1103 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
1104
1105 trace("HttpSendRequestA -->\n");
1106 SetLastError(0xdeadbeef);
1107 rc = HttpSendRequestA(hor, "", -1, NULL, 0);
1108 if (flags & INTERNET_FLAG_ASYNC)
1109 ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
1110 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
1111 else
1112 ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
1113 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
1114 trace("HttpSendRequestA <--\n");
1115
1116 if (!rc && (GetLastError() == ERROR_IO_PENDING)) {
1117 WaitForSingleObject(complete_event, INFINITE);
1118 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
1119 }
1120
1121 if (first_connection_to_test_url)
1122 {
1123 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
1124 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
1125 }
1126 else
1127 {
1128 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
1129 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
1130 }
1131 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
1132 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
1133 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
1134 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
1135 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
1136 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
1137 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
1138 if (flags & INTERNET_FLAG_ASYNC)
1139 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1140 else
1141 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1142 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
1143 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
1144 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
1145 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
1146
1147 if(is_ie7plus) {
1148 rc = InternetReadFileExW(hor, NULL, 0, 0xdeadcafe);
1149 ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
1150 "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
1151 rc ? "TRUE" : "FALSE", GetLastError());
1152 }
1153
1154 /* tests invalid dwStructSize */
1155 inetbuffers.dwStructSize = sizeof(inetbuffers)+1;
1156 inetbuffers.lpcszHeader = NULL;
1157 inetbuffers.dwHeadersLength = 0;
1158 inetbuffers.dwBufferLength = 10;
1159 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
1160 inetbuffers.dwOffsetHigh = 1234;
1161 inetbuffers.dwOffsetLow = 5678;
1162 rc = InternetReadFileExA(hor, &inetbuffers, 0, 0xdeadcafe);
1163 ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
1164 "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
1165 rc ? "TRUE" : "FALSE", GetLastError());
1166 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
1167
1168 test_request_flags(hor, 0);
1169
1170 /* tests to see whether lpcszHeader is used - it isn't */
1171 inetbuffers.dwStructSize = sizeof(inetbuffers);
1172 inetbuffers.lpcszHeader = (LPCSTR)0xdeadbeef;
1173 inetbuffers.dwHeadersLength = 255;
1174 inetbuffers.dwBufferLength = 0;
1175 inetbuffers.lpvBuffer = NULL;
1176 inetbuffers.dwOffsetHigh = 1234;
1177 inetbuffers.dwOffsetLow = 5678;
1178 rc = InternetReadFileExA(hor, &inetbuffers, 0, 0xdeadcafe);
1179 ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
1180 trace("read %i bytes\n", inetbuffers.dwBufferLength);
1181
1182 rc = InternetReadFileExA(NULL, &inetbuffers, 0, 0xdeadcafe);
1183 ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
1184 "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
1185 rc ? "TRUE" : "FALSE", GetLastError());
1186
1187 length = 0;
1188 trace("Entering Query loop\n");
1189
1190 while (TRUE)
1191 {
1192 inetbuffers.dwStructSize = sizeof(inetbuffers);
1193 inetbuffers.dwBufferLength = 1024;
1194 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
1195 inetbuffers.dwOffsetHigh = 1234;
1196 inetbuffers.dwOffsetLow = 5678;
1197
1198 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
1199 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
1200 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
1201 rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
1202 if (!rc)
1203 {
1204 if (GetLastError() == ERROR_IO_PENDING)
1205 {
1206 trace("InternetReadFileEx -> PENDING\n");
1207 ok(flags & INTERNET_FLAG_ASYNC,
1208 "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
1209 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1210 WaitForSingleObject(complete_event, INFINITE);
1211 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1212 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1213 ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
1214 }
1215 else
1216 {
1217 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
1218 break;
1219 }
1220 }
1221 else
1222 {
1223 trace("InternetReadFileEx -> SUCCEEDED\n");
1224 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1225 if (inetbuffers.dwBufferLength)
1226 {
1227 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1228 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1229 }
1230 else
1231 {
1232 /* Win98 still sends these when 0 bytes are read, WinXP does not */
1233 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1234 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1235 }
1236 }
1237
1238 trace("read %i bytes\n", inetbuffers.dwBufferLength);
1239 ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
1240
1241 ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
1242 "InternetReadFileEx sets offsets to 0x%x%08x\n",
1243 inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
1244
1245 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
1246
1247 if (!inetbuffers.dwBufferLength)
1248 break;
1249
1250 length += inetbuffers.dwBufferLength;
1251 }
1252 ok(length > 0, "failed to read any of the document\n");
1253 trace("Finished. Read %d bytes\n", length);
1254
1255 abort:
1256 close_async_handle(hi, 2);
1257 first_connection_to_test_url = FALSE;
1258 }
1259
1260 static void InternetOpenUrlA_test(void)
1261 {
1262 HINTERNET myhinternet, myhttp;
1263 char buffer[0x400];
1264 DWORD size, readbytes, totalbytes=0;
1265 BOOL ret;
1266
1267 ret = DeleteUrlCacheEntryA(TEST_URL);
1268 ok(ret || GetLastError() == ERROR_FILE_NOT_FOUND,
1269 "DeleteUrlCacheEntry returned %x, GetLastError() = %d\n", ret, GetLastError());
1270
1271 myhinternet = InternetOpenA("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
1272 ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
1273 size = 0x400;
1274 ret = InternetCanonicalizeUrlA(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
1275 ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
1276
1277 SetLastError(0);
1278 myhttp = InternetOpenUrlA(myhinternet, TEST_URL, 0, 0,
1279 INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
1280 if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1281 return; /* WinXP returns this when not connected to the net */
1282 ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
1283 ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
1284 ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
1285 totalbytes += readbytes;
1286 while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
1287 totalbytes += readbytes;
1288 trace("read 0x%08x bytes\n",totalbytes);
1289
1290 InternetCloseHandle(myhttp);
1291 InternetCloseHandle(myhinternet);
1292
1293 ret = DeleteUrlCacheEntryA(TEST_URL);
1294 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "INTERNET_FLAG_NO_CACHE_WRITE flag doesn't work\n");
1295 }
1296
1297 static void HttpSendRequestEx_test(void)
1298 {
1299 HINTERNET hSession;
1300 HINTERNET hConnect;
1301 HINTERNET hRequest;
1302
1303 INTERNET_BUFFERSA BufferIn;
1304 DWORD dwBytesWritten, dwBytesRead, error;
1305 CHAR szBuffer[256];
1306 int i;
1307 BOOL ret;
1308
1309 static char szPostData[] = "mode=Test";
1310 static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
1311
1312 hSession = InternetOpenA("Wine Regression Test",
1313 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1314 ok( hSession != NULL ,"Unable to open Internet session\n");
1315 hConnect = InternetConnectA(hSession, "test.winehq.org",
1316 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1317 0);
1318 ok( hConnect != NULL, "Unable to connect to http://test.winehq.org\n");
1319 hRequest = HttpOpenRequestA(hConnect, "POST", "/tests/post.php",
1320 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1321 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1322 {
1323 skip( "Network unreachable, skipping test\n" );
1324 goto done;
1325 }
1326 ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
1327
1328 test_request_flags(hRequest, INTERNET_REQFLAG_NO_HEADERS);
1329
1330 BufferIn.dwStructSize = sizeof(BufferIn);
1331 BufferIn.Next = (INTERNET_BUFFERSA*)0xdeadcab;
1332 BufferIn.lpcszHeader = szContentType;
1333 BufferIn.dwHeadersLength = sizeof(szContentType)-1;
1334 BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
1335 BufferIn.lpvBuffer = szPostData;
1336 BufferIn.dwBufferLength = 3;
1337 BufferIn.dwBufferTotal = sizeof(szPostData)-1;
1338 BufferIn.dwOffsetLow = 0;
1339 BufferIn.dwOffsetHigh = 0;
1340
1341 SetLastError(0xdeadbeef);
1342 ret = HttpSendRequestExA(hRequest, &BufferIn, NULL, 0 ,0);
1343 error = GetLastError();
1344 ok(ret, "HttpSendRequestEx Failed with error %u\n", error);
1345 ok(error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", error);
1346
1347 test_request_flags(hRequest, INTERNET_REQFLAG_NO_HEADERS);
1348
1349 for (i = 3; szPostData[i]; i++)
1350 ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
1351 "InternetWriteFile failed\n");
1352
1353 test_request_flags(hRequest, INTERNET_REQFLAG_NO_HEADERS);
1354
1355 ok(HttpEndRequestA(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
1356
1357 test_request_flags(hRequest, 0);
1358
1359 ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
1360 "Unable to read response\n");
1361 szBuffer[dwBytesRead] = 0;
1362
1363 ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
1364 ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0 || broken(proxy_active()),"Got string %s\n",szBuffer);
1365
1366 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1367 done:
1368 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1369 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1370 }
1371
1372 static void InternetOpenRequest_test(void)
1373 {
1374 HINTERNET session, connect, request;
1375 static const char *types[] = { "*", "", NULL };
1376 static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
1377 static const WCHAR *typesW[] = { any, empty, NULL };
1378 BOOL ret;
1379
1380 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1381 ok(session != NULL ,"Unable to open Internet session\n");
1382
1383 connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1384 INTERNET_SERVICE_HTTP, 0, 0);
1385 ok(connect == NULL, "InternetConnectA should have failed\n");
1386 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1387
1388 connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1389 INTERNET_SERVICE_HTTP, 0, 0);
1390 ok(connect == NULL, "InternetConnectA should have failed\n");
1391 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1392
1393 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1394 INTERNET_SERVICE_HTTP, 0, 0);
1395 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1396
1397 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1398 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1399 {
1400 skip( "Network unreachable, skipping test\n" );
1401 goto done;
1402 }
1403 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1404
1405 ret = HttpSendRequestW(request, NULL, 0, NULL, 0);
1406 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1407 ok(InternetCloseHandle(request), "Close request handle failed\n");
1408
1409 request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1410 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1411
1412 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1413 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1414 ok(InternetCloseHandle(request), "Close request handle failed\n");
1415
1416 done:
1417 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1418 ok(InternetCloseHandle(session), "Close session handle failed\n");
1419 }
1420
1421 static void test_cache_read(void)
1422 {
1423 HINTERNET session, connection, req;
1424 FILETIME now, tomorrow, yesterday;
1425 BYTE content[1000], buf[2000];
1426 char file_path[MAX_PATH];
1427 ULARGE_INTEGER li;
1428 HANDLE file;
1429 DWORD size;
1430 unsigned i;
1431 BOOL res;
1432
1433 static const char cache_only_url[] = "http://test.winehq.org/tests/cache-only";
1434 BYTE cache_headers[] = "HTTP/1.1 200 OK\r\n\r\n";
1435
1436 trace("Testing cache read...\n");
1437 reset_events();
1438
1439 for(i = 0; i < sizeof(content); i++)
1440 content[i] = '0' + (i%10);
1441
1442 GetSystemTimeAsFileTime(&now);
1443 li.u.HighPart = now.dwHighDateTime;
1444 li.u.LowPart = now.dwLowDateTime;
1445 li.QuadPart += (LONGLONG)10000000 * 3600 * 24;
1446 tomorrow.dwHighDateTime = li.u.HighPart;
1447 tomorrow.dwLowDateTime = li.u.LowPart;
1448 li.QuadPart -= (LONGLONG)10000000 * 3600 * 24 * 2;
1449 yesterday.dwHighDateTime = li.u.HighPart;
1450 yesterday.dwLowDateTime = li.u.LowPart;
1451
1452 res = CreateUrlCacheEntryA(cache_only_url, sizeof(content), "", file_path, 0);
1453 ok(res, "CreateUrlCacheEntryA failed: %u\n", GetLastError());
1454
1455 file = CreateFileA(file_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1456 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
1457
1458 WriteFile(file, content, sizeof(content), &size, NULL);
1459 CloseHandle(file);
1460
1461 res = CommitUrlCacheEntryA(cache_only_url, file_path, tomorrow, yesterday, NORMAL_CACHE_ENTRY,
1462 cache_headers, sizeof(cache_headers)-1, "", 0);
1463 ok(res, "CommitUrlCacheEntryA failed: %u\n", GetLastError());
1464
1465 session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
1466 ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
1467
1468 pInternetSetStatusCallbackA(session, callback);
1469
1470 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
1471 connection = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT,
1472 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
1473 ok(connection != NULL,"InternetConnect failed with error %u\n", GetLastError());
1474 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
1475
1476 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
1477 req = HttpOpenRequestA(connection, "GET", "/tests/cache-only", NULL, NULL, NULL, 0, 0xdeadbead);
1478 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
1479 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
1480
1481 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
1482 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
1483 SET_WINE_ALLOW(INTERNET_STATUS_SENDING_REQUEST);
1484 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_SENT);
1485 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
1486 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
1487 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
1488
1489 res = HttpSendRequestA(req, NULL, -1, NULL, 0);
1490 todo_wine
1491 ok(res, "HttpSendRequest failed: %u\n", GetLastError());
1492
1493 if(res) {
1494 size = 0;
1495 res = InternetQueryDataAvailable(req, &size, 0, 0);
1496 ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
1497 ok(size == sizeof(content), "size = %u\n", size);
1498
1499 size = sizeof(buf);
1500 res = InternetReadFile(req, buf, sizeof(buf), &size);
1501 ok(res, "InternetReadFile failed: %u\n", GetLastError());
1502 ok(size == sizeof(content), "size = %u\n", size);
1503 ok(!memcmp(content, buf, sizeof(content)), "unexpected content\n");
1504 }
1505
1506 close_async_handle(session, 2);
1507
1508 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
1509 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
1510 CLEAR_NOTIFIED(INTERNET_STATUS_SENDING_REQUEST);
1511 CLEAR_NOTIFIED(INTERNET_STATUS_REQUEST_SENT);
1512 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
1513 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
1514 CLEAR_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
1515
1516 res = DeleteUrlCacheEntryA(cache_only_url);
1517 ok(res, "DeleteUrlCacheEntryA failed: %u\n", GetLastError());
1518 }
1519
1520 static void test_http_cache(void)
1521 {
1522 HINTERNET session, connect, request;
1523 char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
1524 DWORD size, file_size;
1525 BYTE buf[100];
1526 HANDLE file;
1527 BOOL ret;
1528 FILETIME filetime_zero = {0};
1529
1530 static const char cached_content[] = "data read from cache";
1531 static const char *types[] = { "*", "", NULL };
1532
1533 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1534 ok(session != NULL ,"Unable to open Internet session\n");
1535
1536 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1537 INTERNET_SERVICE_HTTP, 0, 0);
1538 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1539
1540 request = HttpOpenRequestA(connect, NULL, "/tests/hello.html", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
1541 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1542 {
1543 skip( "Network unreachable, skipping test\n" );
1544
1545 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1546 ok(InternetCloseHandle(session), "Close session handle failed\n");
1547
1548 return;
1549 }
1550 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1551
1552 size = sizeof(url);
1553 ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
1554 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
1555 ok(!strcmp(url, "http://test.winehq.org/tests/hello.html"), "Wrong URL %s\n", url);
1556
1557 size = sizeof(file_name);
1558 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1559 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1560 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1561 ok(!size, "size = %d\n", size);
1562
1563 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1564 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1565
1566 size = sizeof(file_name);
1567 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1568 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1569
1570 file = CreateFileA(file_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
1571 FILE_ATTRIBUTE_NORMAL, NULL);
1572 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1573 file_size = GetFileSize(file, NULL);
1574 ok(file_size == 106, "file size = %u\n", file_size);
1575
1576 size = sizeof(buf);
1577 ret = InternetReadFile(request, buf, sizeof(buf), &size);
1578 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1579 ok(size == 100, "size = %u\n", size);
1580
1581 file_size = GetFileSize(file, NULL);
1582 ok(file_size == 106, "file size = %u\n", file_size);
1583 CloseHandle(file);
1584
1585 ret = DeleteFileA(file_name);
1586 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%u)\n", ret, GetLastError());
1587
1588 ok(InternetCloseHandle(request), "Close request handle failed\n");
1589
1590 file = CreateFileA(file_name, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1591 FILE_ATTRIBUTE_NORMAL, NULL);
1592 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1593 ret = WriteFile(file, cached_content, sizeof(cached_content), &size, NULL);
1594 ok(ret && size, "WriteFile failed: %d, %d\n", ret, size);
1595 ret = CommitUrlCacheEntryA(url, file_name, filetime_zero, filetime_zero, NORMAL_CACHE_ENTRY, NULL, 0, NULL, 0);
1596 ok(ret, "CommitUrlCacheEntry failed: %d\n", GetLastError());
1597 CloseHandle(file);
1598
1599 /* Send the same request, requiring it to be retrieved from the cache */
1600 request = HttpOpenRequestA(connect, "GET", "/tests/hello.html", NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
1601
1602 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1603 ok(ret, "HttpSendRequest failed\n");
1604
1605 size = sizeof(buf);
1606 ret = InternetReadFile(request, buf, sizeof(buf), &size);
1607 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1608 ok(size == 100, "size = %u\n", size);
1609 buf[99] = 0;
1610 todo_wine ok(!strcmp((char*)buf, cached_content), "incorrect page data: %s\n", (char*)buf);
1611
1612 ok(InternetCloseHandle(request), "Close request handle failed\n");
1613
1614 DeleteUrlCacheEntryA(url);
1615 request = HttpOpenRequestA(connect, "GET", "/tests/hello.html", NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
1616 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1617 todo_wine ok(!ret, "HttpSendRequest succeeded\n");
1618 if(!ret)
1619 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() = %d\n", GetLastError());
1620 ok(InternetCloseHandle(request), "Close request handle failed\n");
1621
1622 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1623 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1624
1625 size = sizeof(file_name);
1626 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1627 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1628 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1629 ok(!size, "size = %d\n", size);
1630
1631 ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1632 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1633
1634 size = sizeof(file_name);
1635 file_name[0] = 0;
1636 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1637 if (ret)
1638 {
1639 file = CreateFileA(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1640 FILE_ATTRIBUTE_NORMAL, NULL);
1641 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1642 CloseHandle(file);
1643 }
1644 else
1645 {
1646 /* < IE8 */
1647 ok(file_name[0] == 0, "Didn't expect a file name\n");
1648 }
1649
1650 ok(InternetCloseHandle(request), "Close request handle failed\n");
1651 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1652 ok(InternetCloseHandle(session), "Close session handle failed\n");
1653
1654 test_cache_read();
1655 }
1656
1657 static void InternetLockRequestFile_test(void)
1658 {
1659 char file_name[MAX_PATH];
1660 test_request_t req;
1661 HANDLE lock, lock2;
1662 DWORD size;
1663 BOOL ret;
1664
1665 open_simple_request(&req, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, "/tests/hello.html");
1666
1667 size = sizeof(file_name);
1668 ret = InternetQueryOptionA(req.request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1669 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1670 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1671 ok(!size, "size = %d\n", size);
1672
1673 lock = NULL;
1674 ret = InternetLockRequestFile(req.request, &lock);
1675 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
1676
1677 ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
1678 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1679
1680 size = sizeof(file_name);
1681 ret = InternetQueryOptionA(req.request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1682 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1683
1684 ret = InternetLockRequestFile(req.request, &lock);
1685 ok(ret, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
1686 ok(lock != NULL, "lock == NULL\n");
1687
1688 ret = InternetLockRequestFile(req.request, &lock2);
1689 ok(ret, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
1690 ok(lock == lock2, "lock != lock2\n");
1691
1692 ret = InternetUnlockRequestFile(lock2);
1693 ok(ret, "InternetUnlockRequestFile failed: %u\n", GetLastError());
1694
1695 ret = DeleteFileA(file_name);
1696 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%u)\n", ret, GetLastError());
1697
1698 ok(InternetCloseHandle(req.request), "Close request handle failed\n");
1699
1700 ret = DeleteFileA(file_name);
1701 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%u)\n", ret, GetLastError());
1702
1703 ret = InternetUnlockRequestFile(lock);
1704 ok(ret, "InternetUnlockRequestFile failed: %u\n", GetLastError());
1705
1706 ret = DeleteFileA(file_name);
1707 ok(ret, "Deleting file returned %x(%u)\n", ret, GetLastError());
1708 }
1709
1710 static void HttpHeaders_test(void)
1711 {
1712 HINTERNET hSession;
1713 HINTERNET hConnect;
1714 HINTERNET hRequest;
1715 CHAR buffer[256];
1716 WCHAR wbuffer[256];
1717 DWORD len = 256;
1718 DWORD oldlen;
1719 DWORD index = 0;
1720 BOOL ret;
1721
1722 hSession = InternetOpenA("Wine Regression Test",
1723 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1724 ok( hSession != NULL ,"Unable to open Internet session\n");
1725 hConnect = InternetConnectA(hSession, "test.winehq.org",
1726 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1727 0);
1728 ok( hConnect != NULL, "Unable to connect to http://test.winehq.org\n");
1729 hRequest = HttpOpenRequestA(hConnect, "POST", "/tests/post.php",
1730 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1731 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1732 {
1733 skip( "Network unreachable, skipping test\n" );
1734 goto done;
1735 }
1736 ok( hRequest != NULL, "Failed to open request handle\n");
1737
1738 index = 0;
1739 len = sizeof(buffer);
1740 strcpy(buffer,"Warning");
1741 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1742 buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1743
1744 ok(HttpAddRequestHeadersA(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1745 "Failed to add new header\n");
1746
1747 index = 0;
1748 len = sizeof(buffer);
1749 strcpy(buffer,"Warning");
1750 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1751 buffer,&len,&index),"Unable to query header\n");
1752 ok(index == 1, "Index was not incremented\n");
1753 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1754 ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1755 ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1756 len = sizeof(buffer);
1757 strcpy(buffer,"Warning");
1758 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1759 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1760
1761 index = 0;
1762 len = 5; /* could store the string but not the NULL terminator */
1763 strcpy(buffer,"Warning");
1764 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1765 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1766 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1767 ok(index == 0, "Index was incremented\n");
1768 ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1769 ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1770
1771 /* a call with NULL will fail but will return the length */
1772 index = 0;
1773 len = sizeof(buffer);
1774 SetLastError(0xdeadbeef);
1775 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1776 NULL,&len,&index) == FALSE,"Query worked\n");
1777 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1778 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1779 ok(index == 0, "Index was incremented\n");
1780
1781 /* even for a len that is too small */
1782 index = 0;
1783 len = 15;
1784 SetLastError(0xdeadbeef);
1785 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1786 NULL,&len,&index) == FALSE,"Query worked\n");
1787 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1788 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1789 ok(index == 0, "Index was incremented\n");
1790
1791 index = 0;
1792 len = 0;
1793 SetLastError(0xdeadbeef);
1794 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1795 NULL,&len,&index) == FALSE,"Query worked\n");
1796 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1797 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1798 ok(index == 0, "Index was incremented\n");
1799 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1800
1801
1802 /* a working query */
1803 index = 0;
1804 len = sizeof(buffer);
1805 memset(buffer, 'x', sizeof(buffer));
1806 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1807 buffer,&len,&index),"Unable to query header\n");
1808 ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1809 ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1810 ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1811 /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1812 ok(strncmp(buffer, "POST /tests/post.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1813 ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1814 ok(index == 0, "Index was incremented\n");
1815
1816 /* Like above two tests, but for W version */
1817
1818 index = 0;
1819 len = 0;
1820 SetLastError(0xdeadbeef);
1821 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1822 NULL,&len,&index) == FALSE,"Query worked\n");
1823 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1824 ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1825 ok(index == 0, "Index was incremented\n");
1826 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1827
1828 /* a working query */
1829 index = 0;
1830 len = sizeof(wbuffer);
1831 memset(wbuffer, 'x', sizeof(wbuffer));
1832 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1833 wbuffer,&len,&index),"Unable to query header\n");
1834 ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1835 ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1836 ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1837 ok(index == 0, "Index was incremented\n");
1838
1839 /* end of W version tests */
1840
1841 /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1842 index = 0;
1843 len = sizeof(buffer);
1844 memset(buffer, 'x', sizeof(buffer));
1845 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF,
1846 buffer,&len,&index) == TRUE,"Query failed\n");
1847 ok(len == 2 || len == 4 /* win10 */, "Expected 2 or 4, got %d\n", len);
1848 ok(memcmp(buffer, "\r\n\r\n", len) == 0, "Expected CRLF, got '%s'\n", buffer);
1849 ok(index == 0, "Index was incremented\n");
1850
1851 ok(HttpAddRequestHeadersA(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1852 "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1853
1854 index = 0;
1855 len = sizeof(buffer);
1856 strcpy(buffer,"Warning");
1857 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1858 buffer,&len,&index),"Unable to query header\n");
1859 ok(index == 1, "Index was not incremented\n");
1860 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1861 len = sizeof(buffer);
1862 strcpy(buffer,"Warning");
1863 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1864 buffer,&len,&index),"Failed to get second header\n");
1865 ok(index == 2, "Index was not incremented\n");
1866 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1867 len = sizeof(buffer);
1868 strcpy(buffer,"Warning");
1869 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1870 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1871
1872 ok(HttpAddRequestHeadersA(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1873
1874 index = 0;
1875 len = sizeof(buffer);
1876 strcpy(buffer,"Warning");
1877 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1878 buffer,&len,&index),"Unable to query header\n");
1879 ok(index == 1, "Index was not incremented\n");
1880 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1881 len = sizeof(buffer);
1882 strcpy(buffer,"Warning");
1883 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1884 buffer,&len,&index),"Failed to get second header\n");
1885 ok(index == 2, "Index was not incremented\n");
1886 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1887 len = sizeof(buffer);
1888 strcpy(buffer,"Warning");
1889 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1890 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1891
1892 ok(HttpAddRequestHeadersA(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1893
1894 index = 0;
1895 len = sizeof(buffer);
1896 strcpy(buffer,"Warning");
1897 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1898 buffer,&len,&index),"Unable to query header\n");
1899 ok(index == 1, "Index was not incremented\n");
1900 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1901 len = sizeof(buffer);
1902 strcpy(buffer,"Warning");
1903 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1904 buffer,&len,&index),"Failed to get second header\n");
1905 ok(index == 2, "Index was not incremented\n");
1906 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1907 len = sizeof(buffer);
1908 strcpy(buffer,"Warning");
1909 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1910 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1911
1912 ok(HttpAddRequestHeadersA(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1913
1914 index = 0;
1915 len = sizeof(buffer);
1916 strcpy(buffer,"Warning");
1917 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1918 buffer,&len,&index),"Unable to query header\n");
1919 ok(index == 1, "Index was not incremented\n");
1920 ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1921 len = sizeof(buffer);
1922 strcpy(buffer,"Warning");
1923 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1924 ok(index == 2, "Index was not incremented\n");
1925 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1926 len = sizeof(buffer);
1927 strcpy(buffer,"Warning");
1928 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1929
1930 ok(HttpAddRequestHeadersA(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1931
1932 index = 0;
1933 len = sizeof(buffer);
1934 strcpy(buffer,"Warning");
1935 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1936 ok(index == 1, "Index was not incremented\n");
1937 ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1938 len = sizeof(buffer);
1939 strcpy(buffer,"Warning");
1940 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1941 ok(index == 2, "Index was not incremented\n");
1942 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1943 len = sizeof(buffer);
1944 strcpy(buffer,"Warning");
1945 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1946
1947 ok(HttpAddRequestHeadersA(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1948
1949 index = 0;
1950 len = sizeof(buffer);
1951 strcpy(buffer,"Warning");
1952 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1953 ok(index == 1, "Index was not incremented\n");
1954 ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1955 len = sizeof(buffer);
1956 strcpy(buffer,"Warning");
1957 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1958 ok(index == 2, "Index was not incremented\n");
1959 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1960 len = sizeof(buffer);
1961 strcpy(buffer,"Warning");
1962 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1963
1964 ok(HttpAddRequestHeadersA(hRequest,"Warning:test7",-1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE), "HTTP_ADDREQ_FLAG_ADD with HTTP_ADDREQ_FLAG_REPALCE Did not work\n");
1965
1966 index = 0;
1967 len = sizeof(buffer);
1968 strcpy(buffer,"Warning");
1969 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1970 ok(index == 1, "Index was not incremented\n");
1971 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1972 len = sizeof(buffer);
1973 strcpy(buffer,"Warning");
1974 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1975 ok(index == 2, "Index was not incremented\n");
1976 ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1977 len = sizeof(buffer);
1978 strcpy(buffer,"Warning");
1979 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1980
1981 /* Ensure that blank headers are ignored and don't cause a failure */
1982 ok(HttpAddRequestHeadersA(hRequest,"\r\nBlankTest:value\r\n\r\n",-1, HTTP_ADDREQ_FLAG_ADD_IF_NEW), "Failed to add header with blank entries in list\n");
1983
1984 index = 0;
1985 len = sizeof(buffer);
1986 strcpy(buffer,"BlankTest");
1987 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1988 ok(index == 1, "Index was not incremented\n");
1989 ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1990
1991 /* Ensure that malformed header separators are ignored and don't cause a failure */
1992 ok(HttpAddRequestHeadersA(hRequest,"\r\rMalformedTest:value\n\nMalformedTestTwo: value2\rMalformedTestThree: value3\n\n\r\r\n",-1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE),
1993 "Failed to add header with malformed entries in list\n");
1994
1995 index = 0;
1996 len = sizeof(buffer);
1997 strcpy(buffer,"MalformedTest");
1998 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1999 ok(index == 1, "Index was not incremented\n");
2000 ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
2001 index = 0;
2002 len = sizeof(buffer);
2003 strcpy(buffer,"MalformedTestTwo");
2004 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
2005 ok(index == 1, "Index was not incremented\n");
2006 ok(strcmp(buffer,"value2")==0, "incorrect string was returned(%s)\n",buffer);
2007 index = 0;
2008 len = sizeof(buffer);
2009 strcpy(buffer,"MalformedTestThree");
2010 ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
2011 ok(index == 1, "Index was not incremented\n");
2012 ok(strcmp(buffer,"value3")==0, "incorrect string was returned(%s)\n",buffer);
2013
2014 ret = HttpAddRequestHeadersA(hRequest, "Authorization: Basic\r\n", -1, HTTP_ADDREQ_FLAG_ADD);
2015 ok(ret, "unable to add header %u\n", GetLastError());
2016
2017 index = 0;
2018 buffer[0] = 0;
2019 len = sizeof(buffer);
2020 ret = HttpQueryInfoA(hRequest, HTTP_QUERY_AUTHORIZATION|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &len, &index);
2021 ok(ret, "unable to query header %u\n", GetLastError());
2022 ok(index == 1, "index was not incremented\n");
2023 ok(!strcmp(buffer, "Basic"), "incorrect string was returned (%s)\n", buffer);
2024
2025 ret = HttpAddRequestHeadersA(hRequest, "Authorization:\r\n", -1, HTTP_ADDREQ_FLAG_REPLACE);
2026 ok(ret, "unable to remove header %u\n", GetLastError());
2027
2028 index = 0;
2029 len = sizeof(buffer);
2030 SetLastError(0xdeadbeef);
2031 ok(!HttpQueryInfoA(hRequest, HTTP_QUERY_AUTHORIZATION|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &len, &index),
2032 "header still present\n");
2033 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "got %u\n", GetLastError());
2034
2035 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
2036 done:
2037 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
2038 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
2039 }
2040
2041 static const char garbagemsg[] =
2042 "Garbage: Header\r\n";
2043
2044 static const char contmsg[] =
2045 "HTTP/1.1 100 Continue\r\n";
2046
2047 static const char expandcontmsg[] =
2048 "HTTP/1.1 100 Continue\r\n"
2049 "Server: winecontinue\r\n"
2050 "Tag: something witty\r\n";
2051
2052 static const char okmsg[] =
2053 "HTTP/1.1 200 OK\r\n"
2054 "Server: winetest\r\n"
2055 "\r\n";
2056
2057 static const char okmsg201[] =
2058 "HTTP/1.1 201 OK\r\n"
2059 "Server: winetest\r\n"
2060 "\r\n";
2061
2062 static const char okmsg2[] =
2063 "HTTP/1.1 200 OK\r\n"
2064 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
2065 "Server: winetest\r\n"
2066 "Content-Length: 0\r\n"
2067 "Set-Cookie: one\r\n"
2068 "Set-Cookie: two\r\n"
2069 "\r\n";
2070
2071 static DWORD64 content_length;
2072 static const char largemsg[] =
2073 "HTTP/1.1 200 OK\r\n"
2074 "Content-Length: %I64u\r\n"
2075 "\r\n";
2076
2077 static const char okmsg_cookie_path[] =
2078 "HTTP/1.1 200 OK\r\n"
2079 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
2080 "Server: winetest\r\n"
2081 "Content-Length: 0\r\n"
2082 "Set-Cookie: subcookie2=data; path=/test_cookie_set_path\r\n"
2083 "\r\n";
2084
2085 static const char okmsg_cookie[] =
2086 "HTTP/1.1 200 OK\r\n"
2087 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
2088 "Server: winetest\r\n"
2089 "Content-Length: 0\r\n"
2090 "Set-Cookie: testcookie=testvalue\r\n"
2091 "\r\n";
2092
2093 static const char notokmsg[] =
2094 "HTTP/1.1 400 Bad Request\r\n"
2095 "Server: winetest\r\n"
2096 "\r\n";
2097
2098 static const char noauthmsg[] =
2099 "HTTP/1.1 401 Unauthorized\r\n"
2100 "Server: winetest\r\n"
2101 "Connection: close\r\n"
2102 "WWW-Authenticate: Basic realm=\"placebo\"\r\n"
2103 "\r\n";
2104
2105 static const char noauthmsg2[] =
2106 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed\r\n"
2107 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"
2108 "\0d`0|6\n"
2109 "Server: winetest\r\n";
2110
2111 static const char proxymsg[] =
2112 "HTTP/1.1 407 Proxy Authentication Required\r\n"
2113 "Server: winetest\r\n"
2114 "Proxy-Connection: close\r\n"
2115 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
2116 "\r\n";
2117
2118 static const char page1[] =
2119 "<HTML>\r\n"
2120 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
2121 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
2122 "</HTML>\r\n\r\n";
2123
2124 static const char ok_with_length[] =
2125 "HTTP/1.1 200 OK\r\n"
2126 "Connection: Keep-Alive\r\n"
2127 "Content-Length: 18\r\n\r\n"
2128 "HTTP/1.1 211 OK\r\n\r\n";
2129
2130 static const char ok_with_length2[] =
2131 "HTTP/1.1 210 OK\r\n"
2132 "Connection: Keep-Alive\r\n"
2133 "Content-Length: 19\r\n\r\n"
2134 "HTTP/1.1 211 OK\r\n\r\n";
2135
2136 struct server_info {
2137 HANDLE hEvent;
2138 int port;
2139 };
2140
2141 static int test_cache_gzip;
2142 static const char *send_buffer;
2143 static int server_socket;
2144
2145 static DWORD CALLBACK server_thread(LPVOID param)
2146 {
2147 struct server_info *si = param;
2148 int r, c = -1, i, on, count = 0;
2149 SOCKET s;
2150 struct sockaddr_in sa;
2151 char *buffer;
2152 size_t buffer_size;
2153 WSADATA wsaData;
2154 int last_request = 0;
2155 char host_header[22];
2156 char host_header_override[30];
2157 static int test_no_cache = 0;
2158
2159 WSAStartup(MAKEWORD(1,1), &wsaData);
2160
2161 s = socket(AF_INET, SOCK_STREAM, 0);
2162 if (s == INVALID_SOCKET)
2163 return 1;
2164
2165 on = 1;
2166 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
2167
2168 memset(&sa, 0, sizeof sa);
2169 sa.sin_family = AF_INET;
2170 sa.sin_port = htons(si->port);
2171 sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
2172
2173 r = bind(s, (struct sockaddr*) &sa, sizeof sa);
2174 if (r<0)
2175 return 1;
2176
2177 listen(s, 0);
2178
2179 SetEvent(si->hEvent);
2180
2181 sprintf(host_header, "Host: localhost:%d", si->port);
2182 sprintf(host_header_override, "Host: test.local:%d\r\n", si->port);
2183 buffer = HeapAlloc(GetProcessHeap(), 0, buffer_size = 1000);
2184
2185 do
2186 {
2187 if(c == -1)
2188 c = accept(s, NULL, NULL);
2189
2190 memset(buffer, 0, buffer_size);
2191 for(i=0;; i++)
2192 {
2193 if(i == buffer_size)
2194 buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, buffer_size *= 2);
2195
2196 r = recv(c, buffer+i, 1, 0);
2197 if (r != 1)
2198 break;
2199 if (i<4) continue;
2200 if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
2201 buffer[i-3] == '\r' && buffer[i-1] == '\r')
2202 break;
2203 }
2204 if (strstr(buffer, "GET /test1"))
2205 {
2206 if (!strstr(buffer, "Content-Length: 0"))
2207 {
2208 send(c, okmsg, sizeof okmsg-1, 0);
2209 send(c, page1, sizeof page1-1, 0);
2210 }
2211 else
2212 send(c, notokmsg, sizeof notokmsg-1, 0);
2213 }
2214 if (strstr(buffer, "CONNECT "))
2215 {
2216 if (!strstr(buffer, "Content-Length: 0"))
2217 send(c, notokmsg, sizeof notokmsg-1, 0);
2218 else
2219 send(c, proxymsg, sizeof proxymsg-1, 0);
2220 }
2221 if (strstr(buffer, "/test2"))
2222 {
2223 if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
2224 {
2225 send(c, okmsg, sizeof okmsg-1, 0);
2226 send(c, page1, sizeof page1-1, 0);
2227 }
2228 else
2229 send(c, proxymsg, sizeof proxymsg-1, 0);
2230 }
2231 if (strstr(buffer, "/test3"))
2232 {
2233 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2234 send(c, okmsg, sizeof okmsg-1, 0);
2235 else
2236 send(c, noauthmsg, sizeof noauthmsg-1, 0);
2237 }
2238 if (strstr(buffer, "/test4"))
2239 {
2240 if (strstr(buffer, "Connection: Close"))
2241 send(c, okmsg, sizeof okmsg-1, 0);
2242 else
2243 send(c, notokmsg, sizeof notokmsg-1, 0);
2244 }
2245 if (strstr(buffer, "POST /test5") ||
2246 strstr(buffer, "RPC_IN_DATA /test5") ||
2247 strstr(buffer, "RPC_OUT_DATA /test5"))
2248 {
2249 if (strstr(buffer, "Content-Length: 0"))
2250 {
2251 send(c, okmsg, sizeof okmsg-1, 0);
2252 send(c, page1, sizeof page1-1, 0);
2253 }
2254 else
2255 send(c, notokmsg, sizeof notokmsg-1, 0);
2256 }
2257 if (strstr(buffer, "GET /test6"))
2258 {
2259 send(c, contmsg, sizeof contmsg-1, 0);
2260 send(c, contmsg, sizeof contmsg-1, 0);
2261 send(c, okmsg, sizeof okmsg-1, 0);
2262 send(c, page1, sizeof page1-1, 0);
2263 }
2264 if (strstr(buffer, "POST /test7"))
2265 {
2266 if (strstr(buffer, "Content-Length: 100"))
2267 {
2268 if (strstr(buffer, "POST /test7b"))
2269 recvfrom(c, buffer, buffer_size, 0, NULL, NULL);
2270 send(c, okmsg, sizeof okmsg-1, 0);
2271 send(c, page1, sizeof page1-1, 0);
2272 }
2273 else
2274 send(c, notokmsg, sizeof notokmsg-1, 0);
2275 }
2276 if (strstr(buffer, "/test8"))
2277 {
2278 if (!strstr(buffer, "Connection: Close") &&
2279 strstr(buffer, "Connection: Keep-Alive") &&
2280 !strstr(buffer, "Cache-Control: no-cache") &&
2281 !strstr(buffer, "Pragma: no-cache") &&
2282 strstr(buffer, host_header))
2283 send(c, okmsg, sizeof okmsg-1, 0);
2284 else
2285 send(c, notokmsg, sizeof notokmsg-1, 0);
2286 }
2287 if (strstr(buffer, "/test9"))
2288 {
2289 if (!strstr(buffer, "Connection: Close") &&
2290 !strstr(buffer, "Connection: Keep-Alive") &&
2291 !strstr(buffer, "Cache-Control: no-cache") &&
2292 !strstr(buffer, "Pragma: no-cache") &&
2293 strstr(buffer, host_header))
2294 send(c, okmsg, sizeof okmsg-1, 0);
2295 else
2296 send(c, notokmsg, sizeof notokmsg-1, 0);
2297 }
2298 if (strstr(buffer, "/testA"))
2299 {
2300 if (!strstr(buffer, "Connection: Close") &&
2301 !strstr(buffer, "Connection: Keep-Alive") &&
2302 (strstr(buffer, "Cache-Control: no-cache") ||
2303 strstr(buffer, "Pragma: no-cache")) &&
2304 strstr(buffer, host_header))
2305 send(c, okmsg, sizeof okmsg-1, 0);
2306 else
2307 send(c, notokmsg, sizeof notokmsg-1, 0);
2308 }
2309 if (strstr(buffer, "/testC"))
2310 {
2311 if (strstr(buffer, "cookie=biscuit"))
2312 send(c, okmsg, sizeof okmsg-1, 0);
2313 else
2314 send(c, notokmsg, sizeof notokmsg-1, 0);
2315 }
2316 if (strstr(buffer, "/testD"))
2317 {
2318 send(c, okmsg2, sizeof okmsg2-1, 0);
2319 }
2320 if (strstr(buffer, "/testE"))
2321 {
2322 send(c, noauthmsg2, sizeof noauthmsg2-1, 0);
2323 }
2324 if (strstr(buffer, "GET /quit"))
2325 {
2326 send(c, okmsg, sizeof okmsg-1, 0);
2327 send(c, page1, sizeof page1-1, 0);
2328 last_request = 1;
2329 }
2330 if (strstr(buffer, "GET /testF"))
2331 {
2332 send(c, expandcontmsg, sizeof expandcontmsg-1, 0);
2333 send(c, garbagemsg, sizeof garbagemsg-1, 0);
2334 send(c, contmsg, sizeof contmsg-1, 0);
2335 send(c, garbagemsg, sizeof garbagemsg-1, 0);
2336 send(c, okmsg, sizeof okmsg-1, 0);
2337 send(c, page1, sizeof page1-1, 0);
2338 }
2339 if (strstr(buffer, "GET /testG"))
2340 {
2341 send(c, page1, sizeof page1-1, 0);
2342 }
2343
2344 if (strstr(buffer, "GET /testJ"))
2345 {
2346 if (count == 0)
2347 {
2348 count++;
2349 send(c, ok_with_length, sizeof(ok_with_length)-1, 0);
2350 }
2351 else
2352 {
2353 send(c, ok_with_length2, sizeof(ok_with_length2)-1, 0);
2354 count = 0;
2355 }
2356 }
2357 if (strstr(buffer, "GET /testH"))
2358 {
2359 send(c, ok_with_length2, sizeof(ok_with_length2)-1, 0);
2360 recvfrom(c, buffer, buffer_size, 0, NULL, NULL);
2361 send(c, ok_with_length, sizeof(ok_with_length)-1, 0);
2362 }
2363
2364 if (strstr(buffer, "GET /test_no_content"))
2365 {
2366 static const char nocontentmsg[] = "HTTP/1.1 204 No Content\r\nConnection: close\r\n\r\n";
2367 send(c, nocontentmsg, sizeof(nocontentmsg)-1, 0);
2368 }
2369 if (strstr(buffer, "GET /test_conn_close"))
2370 {
2371 static const char conn_close_response[] = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nsome content";
2372 send(c, conn_close_response, sizeof(conn_close_response)-1, 0);
2373 WaitForSingleObject(conn_close_event, INFINITE);
2374 trace("closing connection\n");
2375 }
2376 if (strstr(buffer, "GET /test_cache_control_no_cache"))
2377 {
2378 static const char no_cache_response[] = "HTTP/1.1 200 OK\r\nCache-Control: no-cache\r\n\r\nsome content";
2379 if(!test_no_cache++)
2380 send(c, no_cache_response, sizeof(no_cache_response)-1, 0);
2381 else
2382 send(c, okmsg, sizeof(okmsg)-1, 0);
2383 }
2384 if (strstr(buffer, "GET /test_cache_control_no_store"))
2385 {
2386 static const char no_cache_response[] = "HTTP/1.1 200 OK\r\nCache-Control: junk, \t No-StOrE\r\n\r\nsome content";
2387 send(c, no_cache_response, sizeof(no_cache_response)-1, 0);
2388 }
2389 if (strstr(buffer, "GET /test_cache_gzip"))
2390 {
2391 static const char gzip_response[] = "HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nContent-Type: text/html\r\n\r\n"
2392 "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x4b\xaf\xca\x2c\x50\x28"
2393 "\x49\x2d\x2e\xe1\x02\x00\x62\x92\xc7\x6c\x0a\x00\x00\x00";
2394 if(!test_cache_gzip++)
2395 send(c, gzip_response, sizeof(gzip_response), 0);
2396 else
2397 send(c, notokmsg, sizeof(notokmsg)-1, 0);
2398 }
2399 if (strstr(buffer, "HEAD /test_head")) {
2400 static const char head_response[] =
2401 "HTTP/1.1 200 OK\r\n"
2402 "Connection: Keep-Alive\r\n"
2403 "Content-Length: 100\r\n"
2404 "\r\n";
2405
2406 send(c, head_response, sizeof(head_response), 0);
2407 continue;
2408 }
2409 if (strstr(buffer, "GET /send_from_buffer"))
2410 send(c, send_buffer, strlen(send_buffer), 0);
2411 if (strstr(buffer, "/test_cache_control_verb"))
2412 {
2413 if (!memcmp(buffer, "GET ", sizeof("GET ")-1) &&
2414 !strstr(buffer, "Cache-Control: no-cache\r\n")) send(c, okmsg, sizeof(okmsg)-1, 0);
2415 else if (strstr(buffer, "Cache-Control: no-cache\r\n")) send(c, okmsg, sizeof(okmsg)-1, 0);
2416 else send(c, notokmsg, sizeof(notokmsg)-1, 0);
2417 }
2418 if (strstr(buffer, "/test_request_content_length"))
2419 {
2420 static char msg[] = "HTTP/1.1 200 OK\r\nConnection: Keep-Alive\r\n\r\n";
2421 static int seen_content_length;
2422
2423 if (!seen_content_length)
2424 {
2425 if (strstr(buffer, "Content-Length: 0"))
2426 {
2427 seen_content_length = 1;
2428 send(c, msg, sizeof msg-1, 0);
2429 }
2430 else send(c, notokmsg, sizeof notokmsg-1, 0);
2431 WaitForSingleObject(complete_event, 5000);
2432 }
2433 else
2434 {
2435 if (strstr(buffer, "Content-Length: 0")) send(c, msg, sizeof msg-1, 0);
2436 else send(c, notokmsg, sizeof notokmsg-1, 0);
2437 WaitForSingleObject(complete_event, 5000);
2438 }
2439 }
2440 if (strstr(buffer, "GET /test_premature_disconnect"))
2441 trace("closing connection\n");
2442 if (strstr(buffer, "HEAD /upload.txt"))
2443 {
2444 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2445 send(c, okmsg, sizeof okmsg-1, 0);
2446 else
2447 send(c, noauthmsg, sizeof noauthmsg-1, 0);
2448 }
2449 if (strstr(buffer, "PUT /upload2.txt"))
2450 {
2451 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2452 send(c, okmsg, sizeof okmsg-1, 0);
2453 else
2454 send(c, notokmsg, sizeof notokmsg-1, 0);
2455 }
2456 if (strstr(buffer, "HEAD /upload3.txt"))
2457 {
2458 if (strstr(buffer, "Authorization: Basic dXNlcjE6cHdkMQ=="))
2459 send(c, okmsg, sizeof okmsg-1, 0);
2460 else
2461 send(c, noauthmsg, sizeof noauthmsg-1, 0);
2462 }
2463 if (strstr(buffer, "HEAD /upload4.txt"))
2464 {
2465 if (strstr(buffer, "Authorization: Bearer dXNlcjE6cHdkMQ=="))
2466 send(c, okmsg, sizeof okmsg-1, 0);
2467 else if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2468 send(c, okmsg201, sizeof okmsg-1, 0);
2469 else
2470 send(c, noauthmsg, sizeof noauthmsg-1, 0);
2471 }
2472 if (strstr(buffer, "/test_cookie_path1"))
2473 {
2474 if (strstr(buffer, "subcookie=data"))
2475 send(c, okmsg, sizeof okmsg-1, 0);
2476 else
2477 send(c, notokmsg, sizeof notokmsg-1, 0);
2478 }
2479 if (strstr(buffer, "/test_cookie_path2"))
2480 {
2481 if (strstr(buffer, "subcookie2=data"))
2482 send(c, okmsg, sizeof okmsg-1, 0);
2483 else
2484 send(c, notokmsg, sizeof notokmsg-1, 0);
2485 }
2486 if (strstr(buffer, "/test_cookie_set_path"))
2487 {
2488 send(c, okmsg_cookie_path, sizeof okmsg_cookie_path-1, 0);
2489 }
2490 if (strstr(buffer, "/test_cookie_merge"))
2491 {
2492 if (strstr(buffer, "subcookie=data") &&
2493 !strstr(buffer, "manual_cookie=test"))
2494 send(c, okmsg, sizeof okmsg-1, 0);
2495 else
2496 send(c, notokmsg, sizeof notokmsg-1, 0);
2497 }
2498 if (strstr(buffer, "/test_cookie_set_host_override"))
2499 {
2500 send(c, okmsg_cookie, sizeof okmsg_cookie-1, 0);
2501 }
2502 if (strstr(buffer, "/test_cookie_check_host_override"))
2503 {
2504 if (strstr(buffer, "Cookie:") && strstr(buffer, "testcookie=testvalue"))
2505 send(c, okmsg, sizeof okmsg-1, 0);
2506 else
2507 send(c, notokmsg, sizeof notokmsg-1, 0);
2508 }
2509 if (strstr(buffer, "/test_cookie_check_different_host"))
2510 {
2511 if (!strstr(buffer, "foo") &&
2512 strstr(buffer, "cookie=biscuit"))
2513 send(c, okmsg, sizeof okmsg-1, 0);
2514 else
2515 send(c, notokmsg, sizeof notokmsg-1, 0);
2516 }
2517 if (strstr(buffer, "/test_host_override"))
2518 {
2519 if (strstr(buffer, host_header_override))
2520 send(c, okmsg, sizeof okmsg-1, 0);
2521 else
2522 send(c, notokmsg, sizeof notokmsg-1, 0);
2523 }
2524 if (strstr(buffer, "/async_read"))
2525 {
2526 const char *page1_mid = page1 + (sizeof page1 - 1)/2;
2527 const char *page1_end = page1 + sizeof page1 - 1;
2528 send(c, okmsg, sizeof okmsg-1, 0);
2529 send(c, page1, page1_mid - page1, 0);
2530 WaitForSingleObject(conn_wait_event, INFINITE);
2531 send(c, page1_mid, page1_end - page1_mid, 0);
2532 }
2533 if (strstr(buffer, "/socket"))
2534 {
2535 server_socket = c;
2536 SetEvent(server_req_rec_event);
2537 WaitForSingleObject(conn_wait_event, INFINITE);
2538 }
2539 if (strstr(buffer, "/echo_request"))
2540 {
2541 send(c, okmsg, sizeof(okmsg)-1, 0);
2542 send(c, buffer, strlen(buffer), 0);
2543 }
2544 if (strstr(buffer, "GET /test_remove_dot_segments"))
2545 {
2546 send(c, okmsg, sizeof(okmsg)-1, 0);
2547 }
2548
2549 if (strstr(buffer, "HEAD /test_large_content"))
2550 {
2551 char msg[sizeof(largemsg) + 16];
2552 sprintf(msg, largemsg, content_length);
2553 send(c, msg, strlen(msg), 0);
2554 }
2555 if (strstr(buffer, "HEAD /test_auth_host1"))
2556 {
2557 if (strstr(buffer, "Authorization: Basic dGVzdDE6cGFzcw=="))
2558 send(c, okmsg, sizeof okmsg-1, 0);
2559 else
2560 send(c, noauthmsg, sizeof noauthmsg-1, 0);
2561 }
2562 if (strstr(buffer, "HEAD /test_auth_host2"))
2563 {
2564 if (strstr(buffer, "Authorization: Basic dGVzdDE6cGFzczI="))
2565 send(c, okmsg, sizeof okmsg-1, 0);
2566 else
2567 send(c, noauthmsg, sizeof noauthmsg-1, 0);
2568 }
2569 shutdown(c, 2);
2570 closesocket(c);
2571 c = -1;
2572 } while (!last_request);
2573
2574 closesocket(s);
2575 HeapFree(GetProcessHeap(), 0, buffer);
2576
2577 return 0;
2578 }
2579
2580 static void test_basic_request(int port, const char *verb, const char *url)
2581 {
2582 test_request_t req;
2583 DWORD r, count, error;
2584 char buffer[0x100];
2585
2586 trace("basic request %s %s\n", verb, url);
2587
2588 open_simple_request(&req, "localhost", port, verb, url);
2589
2590 SetLastError(0xdeadbeef);
2591 r = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
2592 error = GetLastError();
2593 ok(error == ERROR_SUCCESS || broken(error != ERROR_SUCCESS), "expected ERROR_SUCCESS, got %u\n", error);
2594 ok(r, "HttpSendRequest failed: %u\n", GetLastError());
2595
2596 count = 0;
2597 memset(buffer, 0, sizeof buffer);
2598 SetLastError(0xdeadbeef);
2599 r = InternetReadFile(req.request, buffer, sizeof buffer, &count);
2600 ok(r, "InternetReadFile failed %u\n", GetLastError());
2601 ok(count == sizeof page1 - 1, "count was wrong\n");
2602 ok(!memcmp(buffer, page1,