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