4 * Copyright 2002 Aric Stewart
5 * Copyright 2004 Mike McCormack
6 * Copyright 2005 Hans Leidekker
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.
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.
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
32 #include "wine/test.h"
34 #define TEST_URL "http://test.winehq.org/hello.html"
36 static BOOL first_connection_to_test_url = TRUE;
38 /* Adapted from dlls/urlmon/tests/protocol.c */
40 #define SET_EXPECT2(status, num) \
43 #define SET_EXPECT(status) \
44 SET_EXPECT2(status, 1)
46 #define SET_OPTIONAL2(status, num) \
47 optional[status] = num
49 #define SET_OPTIONAL(status) \
50 SET_OPTIONAL2(status, 1)
52 /* SET_WINE_ALLOW's should be used with an appropriate
53 * todo_wine CHECK_NOTIFIED at a later point in the code */
54 #define SET_WINE_ALLOW2(status, num) \
55 wine_allow[status] = num
57 #define SET_WINE_ALLOW(status) \
58 SET_WINE_ALLOW2(status, 1)
60 #define CHECK_EXPECT(status) \
62 if (!expect[status] && !optional[status] && wine_allow[status]) \
64 todo_wine ok(expect[status], "unexpected status %d (%s)\n", status, \
65 status < MAX_INTERNET_STATUS && status_string[status] ? \
66 status_string[status] : "unknown"); \
67 wine_allow[status]--; \
71 ok(expect[status] || optional[status], "unexpected status %d (%s)\n", status, \
72 status < MAX_INTERNET_STATUS && status_string[status] ? \
73 status_string[status] : "unknown"); \
74 if (expect[status]) expect[status]--; \
75 else optional[status]--; \
80 /* CLEAR_NOTIFIED used in cases when notification behavior
81 * differs between Windows versions */
82 #define CLEAR_NOTIFIED(status) \
83 expect[status] = optional[status] = wine_allow[status] = notified[status] = 0;
85 #define CHECK_NOTIFIED2(status, num) \
87 ok(notified[status] + optional[status] == (num), \
88 "expected status %d (%s) %d times, received %d times\n", \
89 status, status < MAX_INTERNET_STATUS && status_string[status] ? \
90 status_string[status] : "unknown", (num), notified[status]); \
91 CLEAR_NOTIFIED(status); \
94 #define CHECK_NOTIFIED(status) \
95 CHECK_NOTIFIED2(status, 1)
97 #define CHECK_NOT_NOTIFIED(status) \
98 CHECK_NOTIFIED2(status, 0)
100 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
101 static int expect[MAX_INTERNET_STATUS], optional[MAX_INTERNET_STATUS],
102 wine_allow[MAX_INTERNET_STATUS], notified[MAX_INTERNET_STATUS];
103 static const char *status_string[MAX_INTERNET_STATUS];
105 static HANDLE hCompleteEvent;
107 #define TESTF_REDIRECT 0x01
108 #define TESTF_COMPRESSED 0x02
109 #define TESTF_ALLOW_COOKIE 0x04
113 const char *redirected_url;
118 const char *post_data;
122 static const test_data_t test_data[] = {
124 "http://test.winehq.org/testredirect",
125 "http://test.winehq.org/hello.html",
132 "http://www.codeweavers.com/",
133 "http://www.codeweavers.com/",
134 "www.codeweavers.com",
136 "Accept-Encoding: gzip, deflate",
137 TESTF_COMPRESSED|TESTF_ALLOW_COOKIE
140 "http://crossover.codeweavers.com/posttest.php",
141 "http://crossover.codeweavers.com/posttest.php",
142 "crossover.codeweavers.com",
144 "Content-Type: application/x-www-form-urlencoded",
151 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
153 static BOOL proxy_active(void)
155 HKEY internet_settings;
159 if (RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
160 0, KEY_QUERY_VALUE, &internet_settings) != ERROR_SUCCESS)
163 size = sizeof(DWORD);
164 if (RegQueryValueExA(internet_settings, "ProxyEnable", NULL, NULL, (LPBYTE) &proxy_enable, &size) != ERROR_SUCCESS)
167 RegCloseKey(internet_settings);
169 return proxy_enable != 0;
172 static VOID WINAPI callback(
175 DWORD dwInternetStatus,
176 LPVOID lpvStatusInformation,
177 DWORD dwStatusInformationLength
180 CHECK_EXPECT(dwInternetStatus);
181 switch (dwInternetStatus)
183 case INTERNET_STATUS_RESOLVING_NAME:
184 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
185 GetCurrentThreadId(), hInternet, dwContext,
186 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
187 *(LPSTR)lpvStatusInformation = '\0';
189 case INTERNET_STATUS_NAME_RESOLVED:
190 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
191 GetCurrentThreadId(), hInternet, dwContext,
192 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
193 *(LPSTR)lpvStatusInformation = '\0';
195 case INTERNET_STATUS_CONNECTING_TO_SERVER:
196 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
197 GetCurrentThreadId(), hInternet, dwContext,
198 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
199 *(LPSTR)lpvStatusInformation = '\0';
201 case INTERNET_STATUS_CONNECTED_TO_SERVER:
202 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
203 GetCurrentThreadId(), hInternet, dwContext,
204 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
205 *(LPSTR)lpvStatusInformation = '\0';
207 case INTERNET_STATUS_SENDING_REQUEST:
208 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
209 GetCurrentThreadId(), hInternet, dwContext,
210 lpvStatusInformation,dwStatusInformationLength);
212 case INTERNET_STATUS_REQUEST_SENT:
213 ok(dwStatusInformationLength == sizeof(DWORD),
214 "info length should be sizeof(DWORD) instead of %d\n",
215 dwStatusInformationLength);
216 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
217 GetCurrentThreadId(), hInternet, dwContext,
218 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
220 case INTERNET_STATUS_RECEIVING_RESPONSE:
221 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
222 GetCurrentThreadId(), hInternet, dwContext,
223 lpvStatusInformation,dwStatusInformationLength);
225 case INTERNET_STATUS_RESPONSE_RECEIVED:
226 ok(dwStatusInformationLength == sizeof(DWORD),
227 "info length should be sizeof(DWORD) instead of %d\n",
228 dwStatusInformationLength);
229 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
230 GetCurrentThreadId(), hInternet, dwContext,
231 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
233 case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
234 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
235 GetCurrentThreadId(), hInternet,dwContext,
236 lpvStatusInformation,dwStatusInformationLength);
238 case INTERNET_STATUS_PREFETCH:
239 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
240 GetCurrentThreadId(), hInternet, dwContext,
241 lpvStatusInformation,dwStatusInformationLength);
243 case INTERNET_STATUS_CLOSING_CONNECTION:
244 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
245 GetCurrentThreadId(), hInternet, dwContext,
246 lpvStatusInformation,dwStatusInformationLength);
248 case INTERNET_STATUS_CONNECTION_CLOSED:
249 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
250 GetCurrentThreadId(), hInternet, dwContext,
251 lpvStatusInformation,dwStatusInformationLength);
253 case INTERNET_STATUS_HANDLE_CREATED:
254 ok(dwStatusInformationLength == sizeof(HINTERNET),
255 "info length should be sizeof(HINTERNET) instead of %d\n",
256 dwStatusInformationLength);
257 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
258 GetCurrentThreadId(), hInternet, dwContext,
259 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
260 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
261 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
263 case INTERNET_STATUS_HANDLE_CLOSING:
264 ok(dwStatusInformationLength == sizeof(HINTERNET),
265 "info length should be sizeof(HINTERNET) instead of %d\n",
266 dwStatusInformationLength);
267 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
268 GetCurrentThreadId(), hInternet, dwContext,
269 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
271 case INTERNET_STATUS_REQUEST_COMPLETE:
273 INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
274 ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
275 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
276 dwStatusInformationLength);
277 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
278 GetCurrentThreadId(), hInternet, dwContext,
279 iar->dwResult,iar->dwError,dwStatusInformationLength);
280 SetEvent(hCompleteEvent);
283 case INTERNET_STATUS_REDIRECT:
284 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
285 GetCurrentThreadId(), hInternet, dwContext,
286 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
287 *(LPSTR)lpvStatusInformation = '\0';
288 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
289 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
291 case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
292 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
293 GetCurrentThreadId(), hInternet, dwContext,
294 lpvStatusInformation, dwStatusInformationLength);
297 trace("%04x:Callback %p 0x%lx %d %p %d\n",
298 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
299 lpvStatusInformation, dwStatusInformationLength);
303 static void InternetReadFile_test(int flags, const test_data_t *test)
305 char *post_data = NULL;
308 DWORD length, post_len = 0;
310 const char *types[2] = { "*", NULL };
311 HINTERNET hi, hic = 0, hor = 0;
313 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
315 trace("Starting InternetReadFile test with flags 0x%x on url %s\n",flags,test->url);
317 trace("InternetOpenA <--\n");
318 hi = InternetOpenA((test->flags & TESTF_COMPRESSED) ? "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" : "",
319 INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
320 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
321 trace("InternetOpenA -->\n");
323 if (hi == 0x0) goto abort;
325 pInternetSetStatusCallbackA(hi,&callback);
327 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
329 trace("InternetConnectA <--\n");
330 hic=InternetConnectA(hi, test->host, INTERNET_INVALID_PORT_NUMBER,
331 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
332 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
333 trace("InternetConnectA -->\n");
335 if (hic == 0x0) goto abort;
337 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
338 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
340 trace("HttpOpenRequestA <--\n");
341 hor = HttpOpenRequestA(hic, test->post_data ? "POST" : "GET", test->path, NULL, NULL, types,
342 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
344 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
346 * If the internet name can't be resolved we are probably behind
347 * a firewall or in some other way not directly connected to the
348 * Internet. Not enough reason to fail the test. Just ignore and
352 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
354 trace("HttpOpenRequestA -->\n");
356 if (hor == 0x0) goto abort;
358 length = sizeof(buffer);
359 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
360 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
361 ok(!strcmp(buffer, test->url), "Wrong URL %s, expected %s\n", buffer, test->url);
363 length = sizeof(buffer);
364 res = HttpQueryInfoA(hor, HTTP_QUERY_RAW_HEADERS, buffer, &length, 0x0);
365 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
366 ok(length == 0, "HTTP_QUERY_RAW_HEADERS: expected length 0, but got %d\n", length);
367 ok(!strcmp(buffer, ""), "HTTP_QUERY_RAW_HEADERS: expected string \"\", but got \"%s\"\n", buffer);
369 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
370 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
371 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
372 if(test->flags & TESTF_ALLOW_COOKIE) {
373 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
374 SET_OPTIONAL(INTERNET_STATUS_COOKIE_RECEIVED);
376 if (first_connection_to_test_url)
378 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
379 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
380 SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
381 SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
385 SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
386 SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
388 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
389 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
390 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
391 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
392 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
393 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
394 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
395 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
396 if(test->flags & TESTF_REDIRECT) {
397 SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
398 SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
400 SET_EXPECT(INTERNET_STATUS_REDIRECT);
401 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
402 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
403 if (flags & INTERNET_FLAG_ASYNC)
404 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
406 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
408 if(test->flags & TESTF_COMPRESSED) {
411 res = InternetSetOption(hor, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
412 ok(res || broken(!res && GetLastError() == ERROR_INTERNET_INVALID_OPTION),
413 "InternetSetOption failed: %u\n", GetLastError());
418 trace("HttpSendRequestA -->\n");
419 if(test->post_data) {
420 post_len = strlen(test->post_data);
421 post_data = HeapAlloc(GetProcessHeap(), 0, post_len);
422 memcpy(post_data, test->post_data, post_len);
424 SetLastError(0xdeadbeef);
425 res = HttpSendRequestA(hor, test->headers, -1, post_data, post_len);
426 if (flags & INTERNET_FLAG_ASYNC)
427 ok(!res && (GetLastError() == ERROR_IO_PENDING),
428 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
430 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
431 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
432 trace("HttpSendRequestA <--\n");
434 if (flags & INTERNET_FLAG_ASYNC)
435 WaitForSingleObject(hCompleteEvent, INFINITE);
436 HeapFree(GetProcessHeap(), 0, post_data);
438 if(test->flags & TESTF_ALLOW_COOKIE) {
439 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
440 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_RECEIVED);
442 if (first_connection_to_test_url)
444 if (! proxy_active())
446 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
447 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
451 CLEAR_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
452 CLEAR_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
457 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
458 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
460 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
461 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
462 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
463 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
464 if(test->flags & TESTF_REDIRECT)
465 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
466 if (flags & INTERNET_FLAG_ASYNC)
467 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
469 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
470 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
471 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
472 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
475 res = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
476 ok(res, "InternetQueryOptionA(INTERNET_OPTION_REQUEST) failed with error %d\n", GetLastError());
479 res = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
480 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed with error %d\n", GetLastError());
482 length = sizeof(buffer);
483 res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
484 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
487 length = sizeof(buffer);
488 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
489 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
490 ok(!strcmp(buffer, test->redirected_url), "Wrong URL %s\n", buffer);
493 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
494 trace("Option HTTP_QUERY_CONTENT_LENGTH -> %i %s (%u)\n",res,buffer,GetLastError());
495 if(test->flags & TESTF_COMPRESSED)
496 ok(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
497 "expected ERROR_HTTP_HEADER_NOT_FOUND, got %x (%u)\n", res, GetLastError());
500 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
502 trace("Option HTTP_QUERY_CONTENT_TYPE -> %i %s\n",res,buffer);
505 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_ENCODING,buffer,&length,0x0);
507 trace("Option HTTP_QUERY_CONTENT_ENCODING -> %i %s\n",res,buffer);
509 SetLastError(0xdeadbeef);
510 res = InternetReadFile(NULL, buffer, 100, &length);
511 ok(!res, "InternetReadFile should have failed\n");
512 ok(GetLastError() == ERROR_INVALID_HANDLE,
513 "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
517 trace("Entering Query loop\n");
521 if (flags & INTERNET_FLAG_ASYNC)
522 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
523 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
524 ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
525 ok(res || ((flags & INTERNET_FLAG_ASYNC) && GetLastError() == ERROR_IO_PENDING),
526 "InternetQueryDataAvailable failed, error %d\n", GetLastError());
527 if (flags & INTERNET_FLAG_ASYNC)
531 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
533 else if (GetLastError() == ERROR_IO_PENDING)
535 WaitForSingleObject(hCompleteEvent, INFINITE);
536 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
543 buffer = HeapAlloc(GetProcessHeap(),0,length+1);
545 res = InternetReadFile(hor,buffer,length,&length);
549 trace("ReadFile -> %s %i\n",res?"TRUE":"FALSE",length);
552 ok(!strcmp(buffer, test->content), "buffer = '%s', expected '%s'\n", buffer, test->content);
553 HeapFree(GetProcessHeap(),0,buffer);
558 if(test->flags & TESTF_REDIRECT) {
559 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
560 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
564 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
566 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
567 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
568 SetLastError(0xdeadbeef);
570 res = InternetCloseHandle(hor);
571 ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
572 SetLastError(0xdeadbeef);
573 res = InternetCloseHandle(hor);
574 ok (!res, "Double close of handle opened by HttpOpenRequestA succeeded\n");
575 ok (GetLastError() == ERROR_INVALID_HANDLE,
576 "Double close of handle should have set ERROR_INVALID_HANDLE instead of %u\n",
579 /* We intentionally do not close the handle opened by InternetConnectA as this
580 * tickles bug #9479: native closes child internet handles when the parent handles
581 * are closed. This is verified below by checking that the number of
582 * INTERNET_STATUS_HANDLE_CLOSING notifications matches the number expected. */
584 SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
585 trace("closing 2\n");
586 res = InternetCloseHandle(hi);
587 ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
588 if (flags & INTERNET_FLAG_ASYNC)
591 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
592 if (hor != 0x0) todo_wine
594 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
595 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
599 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
600 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
602 CloseHandle(hCompleteEvent);
603 first_connection_to_test_url = FALSE;
606 static void InternetReadFile_chunked_test(void)
611 const char *types[2] = { "*", NULL };
612 HINTERNET hi, hic = 0, hor = 0;
614 trace("Starting InternetReadFile chunked test\n");
616 trace("InternetOpenA <--\n");
617 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
618 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
619 trace("InternetOpenA -->\n");
621 if (hi == 0x0) goto abort;
623 trace("InternetConnectA <--\n");
624 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
625 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
626 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
627 trace("InternetConnectA -->\n");
629 if (hic == 0x0) goto abort;
631 trace("HttpOpenRequestA <--\n");
632 hor = HttpOpenRequestA(hic, "GET", "/testchunked", NULL, NULL, types,
633 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
635 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
637 * If the internet name can't be resolved we are probably behind
638 * a firewall or in some other way not directly connected to the
639 * Internet. Not enough reason to fail the test. Just ignore and
643 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
645 trace("HttpOpenRequestA -->\n");
647 if (hor == 0x0) goto abort;
649 trace("HttpSendRequestA -->\n");
650 SetLastError(0xdeadbeef);
651 res = HttpSendRequestA(hor, "", -1, NULL, 0);
652 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
653 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
654 trace("HttpSendRequestA <--\n");
657 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
659 trace("Option CONTENT_TYPE -> %i %s\n",res,buffer);
661 SetLastError( 0xdeadbeef );
663 res = HttpQueryInfoA(hor,HTTP_QUERY_TRANSFER_ENCODING,buffer,&length,0x0);
665 trace("Option TRANSFER_ENCODING -> %i %s\n",res,buffer);
666 ok( res || ( proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
667 "Failed to get TRANSFER_ENCODING option, error %u\n", GetLastError() );
668 ok( !strcmp( buffer, "chunked" ) || ( ! res && proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
669 "Wrong transfer encoding '%s'\n", buffer );
671 SetLastError( 0xdeadbeef );
673 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
674 ok( !res, "Found CONTENT_LENGTH option '%s'\n", buffer );
675 ok( GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "Wrong error %u\n", GetLastError() );
678 trace("Entering Query loop\n");
682 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
683 ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
684 ok(res, "InternetQueryDataAvailable failed, error %d\n", GetLastError());
685 trace("got %u available\n",length);
689 char *buffer = HeapAlloc(GetProcessHeap(),0,length+1);
691 res = InternetReadFile(hor,buffer,length,&got);
694 trace("ReadFile -> %i %i\n",res,got);
695 ok( length == got, "only got %u of %u available\n", got, length );
696 ok( buffer[got-1] == '\n', "received partial line '%s'\n", buffer );
698 HeapFree(GetProcessHeap(),0,buffer);
707 res = InternetCloseHandle(hor);
708 ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
711 res = InternetCloseHandle(hi);
712 ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
716 static void InternetReadFileExA_test(int flags)
720 const char *types[2] = { "*", NULL };
721 HINTERNET hi, hic = 0, hor = 0;
722 INTERNET_BUFFERS inetbuffers;
724 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
726 trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
728 trace("InternetOpenA <--\n");
729 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
730 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
731 trace("InternetOpenA -->\n");
733 if (hi == 0x0) goto abort;
735 pInternetSetStatusCallbackA(hi,&callback);
737 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
739 trace("InternetConnectA <--\n");
740 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
741 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
742 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
743 trace("InternetConnectA -->\n");
745 if (hic == 0x0) goto abort;
747 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
748 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
750 trace("HttpOpenRequestA <--\n");
751 hor = HttpOpenRequestA(hic, "GET", "/testredirect", NULL, NULL, types,
752 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
754 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
756 * If the internet name can't be resolved we are probably behind
757 * a firewall or in some other way not directly connected to the
758 * Internet. Not enough reason to fail the test. Just ignore and
762 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
764 trace("HttpOpenRequestA -->\n");
766 if (hor == 0x0) goto abort;
768 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
769 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
770 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
771 if (first_connection_to_test_url)
773 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
774 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
775 SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
776 SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
780 SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
781 SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
783 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
784 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
785 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
786 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
787 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
788 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
789 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
790 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
791 SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
792 SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
793 SET_EXPECT(INTERNET_STATUS_REDIRECT);
794 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
795 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
796 if (flags & INTERNET_FLAG_ASYNC)
797 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
799 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
801 trace("HttpSendRequestA -->\n");
802 SetLastError(0xdeadbeef);
803 rc = HttpSendRequestA(hor, "", -1, NULL, 0);
804 if (flags & INTERNET_FLAG_ASYNC)
805 ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
806 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
808 ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
809 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
810 trace("HttpSendRequestA <--\n");
812 if (!rc && (GetLastError() == ERROR_IO_PENDING))
813 WaitForSingleObject(hCompleteEvent, INFINITE);
815 if (first_connection_to_test_url)
817 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
818 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
822 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
823 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
825 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
826 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
827 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
828 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
829 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
830 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
831 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
832 if (flags & INTERNET_FLAG_ASYNC)
833 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
835 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
836 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
837 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
838 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
840 /* tests invalid dwStructSize */
841 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
842 inetbuffers.lpcszHeader = NULL;
843 inetbuffers.dwHeadersLength = 0;
844 inetbuffers.dwBufferLength = 10;
845 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
846 inetbuffers.dwOffsetHigh = 1234;
847 inetbuffers.dwOffsetLow = 5678;
848 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
849 ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
850 "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
851 rc ? "TRUE" : "FALSE", GetLastError());
852 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
854 /* tests to see whether lpcszHeader is used - it isn't */
855 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
856 inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
857 inetbuffers.dwHeadersLength = 255;
858 inetbuffers.dwBufferLength = 0;
859 inetbuffers.lpvBuffer = NULL;
860 inetbuffers.dwOffsetHigh = 1234;
861 inetbuffers.dwOffsetLow = 5678;
862 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
863 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
864 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
865 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
866 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
867 ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
868 trace("read %i bytes\n", inetbuffers.dwBufferLength);
871 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
872 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
875 rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
876 ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
877 "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
878 rc ? "TRUE" : "FALSE", GetLastError());
881 trace("Entering Query loop\n");
883 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
884 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
887 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
888 inetbuffers.dwBufferLength = 1024;
889 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
890 inetbuffers.dwOffsetHigh = 1234;
891 inetbuffers.dwOffsetLow = 5678;
893 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
894 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
895 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
896 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
897 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
898 rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
901 if (GetLastError() == ERROR_IO_PENDING)
903 trace("InternetReadFileEx -> PENDING\n");
904 ok(flags & INTERNET_FLAG_ASYNC,
905 "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
906 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
907 WaitForSingleObject(hCompleteEvent, INFINITE);
908 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
909 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
913 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
919 trace("InternetReadFileEx -> SUCCEEDED\n");
920 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
921 if (inetbuffers.dwBufferLength)
924 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
925 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
930 /* Win98 still sends these when 0 bytes are read, WinXP does not */
931 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
932 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
936 trace("read %i bytes\n", inetbuffers.dwBufferLength);
937 ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
939 ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
940 "InternetReadFileEx sets offsets to 0x%x%08x\n",
941 inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
943 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
945 if (!inetbuffers.dwBufferLength)
948 length += inetbuffers.dwBufferLength;
950 ok(length > 0, "failed to read any of the document\n");
951 trace("Finished. Read %d bytes\n", length);
953 /* WinXP does not send, but Win98 does */
954 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
955 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
957 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
959 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
960 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
961 rc = InternetCloseHandle(hor);
962 ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
963 rc = InternetCloseHandle(hor);
964 ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
967 rc = InternetCloseHandle(hic);
968 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
971 SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
972 rc = InternetCloseHandle(hi);
973 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
974 if (flags & INTERNET_FLAG_ASYNC)
976 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
978 /* to enable once Wine is fixed to never send it
979 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
980 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
982 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
983 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
984 CloseHandle(hCompleteEvent);
985 first_connection_to_test_url = FALSE;
988 static void InternetOpenUrlA_test(void)
990 HINTERNET myhinternet, myhttp;
992 DWORD size, readbytes, totalbytes=0;
995 myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
996 ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
998 ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
999 ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
1002 myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
1003 INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
1004 if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1005 return; /* WinXP returns this when not connected to the net */
1006 ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
1007 ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
1008 ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
1009 totalbytes += readbytes;
1010 while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
1011 totalbytes += readbytes;
1012 trace("read 0x%08x bytes\n",totalbytes);
1014 InternetCloseHandle(myhttp);
1015 InternetCloseHandle(myhinternet);
1018 static void HttpSendRequestEx_test(void)
1024 INTERNET_BUFFERS BufferIn;
1025 DWORD dwBytesWritten, dwBytesRead, error;
1030 static char szPostData[] = "mode=Test";
1031 static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
1033 hSession = InternetOpen("Wine Regression Test",
1034 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1035 ok( hSession != NULL ,"Unable to open Internet session\n");
1036 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1037 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1039 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1040 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1041 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1042 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1044 skip( "Network unreachable, skipping test\n" );
1047 ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
1050 BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
1051 BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
1052 BufferIn.lpcszHeader = szContentType;
1053 BufferIn.dwHeadersLength = sizeof(szContentType)-1;
1054 BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
1055 BufferIn.lpvBuffer = szPostData;
1056 BufferIn.dwBufferLength = 3;
1057 BufferIn.dwBufferTotal = sizeof(szPostData)-1;
1058 BufferIn.dwOffsetLow = 0;
1059 BufferIn.dwOffsetHigh = 0;
1061 SetLastError(0xdeadbeef);
1062 ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
1063 error = GetLastError();
1064 ok(ret, "HttpSendRequestEx Failed with error %u\n", error);
1065 ok(error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", error);
1067 for (i = 3; szPostData[i]; i++)
1068 ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
1069 "InternetWriteFile failed\n");
1071 ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
1073 ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
1074 "Unable to read response\n");
1075 szBuffer[dwBytesRead] = 0;
1077 ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
1078 ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0 || broken(proxy_active()),"Got string %s\n",szBuffer);
1080 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1082 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1083 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1086 static void InternetOpenRequest_test(void)
1088 HINTERNET session, connect, request;
1089 static const char *types[] = { "*", "", NULL };
1090 static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
1091 static const WCHAR *typesW[] = { any, empty, NULL };
1094 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1095 ok(session != NULL ,"Unable to open Internet session\n");
1097 connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1098 INTERNET_SERVICE_HTTP, 0, 0);
1099 ok(connect == NULL, "InternetConnectA should have failed\n");
1100 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1102 connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1103 INTERNET_SERVICE_HTTP, 0, 0);
1104 ok(connect == NULL, "InternetConnectA should have failed\n");
1105 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1107 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1108 INTERNET_SERVICE_HTTP, 0, 0);
1109 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1111 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1112 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1114 skip( "Network unreachable, skipping test\n" );
1117 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1119 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1120 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1121 ok(InternetCloseHandle(request), "Close request handle failed\n");
1123 request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1124 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1126 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1127 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1128 ok(InternetCloseHandle(request), "Close request handle failed\n");
1131 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1132 ok(InternetCloseHandle(session), "Close session handle failed\n");
1135 static void test_http_cache(void)
1137 HINTERNET session, connect, request;
1138 char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
1139 DWORD size, file_size;
1144 static const char *types[] = { "*", "", NULL };
1146 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1147 ok(session != NULL ,"Unable to open Internet session\n");
1149 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1150 INTERNET_SERVICE_HTTP, 0, 0);
1151 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1153 request = HttpOpenRequestA(connect, NULL, "/hello.html", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
1154 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1156 skip( "Network unreachable, skipping test\n" );
1158 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1159 ok(InternetCloseHandle(session), "Close session handle failed\n");
1163 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1166 ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
1167 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
1168 ok(!strcmp(url, "http://test.winehq.org/hello.html"), "Wrong URL %s\n", url);
1170 size = sizeof(file_name);
1171 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1172 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1173 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1174 ok(!size, "size = %d\n", size);
1176 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1177 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1179 size = sizeof(file_name);
1180 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1181 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1183 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1184 FILE_ATTRIBUTE_NORMAL, NULL);
1185 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1186 file_size = GetFileSize(file, NULL);
1187 todo_wine ok(file_size == 106, "file size = %u\n", file_size);
1190 ret = InternetReadFile(request, buf, sizeof(buf), &size);
1191 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1192 ok(size == 100, "size = %u\n", size);
1194 file_size = GetFileSize(file, NULL);
1195 todo_wine ok(file_size == 106, "file size = %u\n", file_size);
1198 ok(InternetCloseHandle(request), "Close request handle failed\n");
1200 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1201 FILE_ATTRIBUTE_NORMAL, NULL);
1202 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1205 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1206 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1208 size = sizeof(file_name);
1209 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1210 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1211 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1212 ok(!size, "size = %d\n", size);
1214 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1215 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1217 size = sizeof(file_name);
1219 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1222 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1223 FILE_ATTRIBUTE_NORMAL, NULL);
1224 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1230 ok(file_name[0] == 0, "Didn't expect a file name\n");
1233 ok(InternetCloseHandle(request), "Close request handle failed\n");
1234 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1235 ok(InternetCloseHandle(session), "Close session handle failed\n");
1238 static void HttpHeaders_test(void)
1249 hSession = InternetOpen("Wine Regression Test",
1250 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1251 ok( hSession != NULL ,"Unable to open Internet session\n");
1252 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1253 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1255 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1256 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1257 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1258 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1260 skip( "Network unreachable, skipping test\n" );
1263 ok( hRequest != NULL, "Failed to open request handle\n");
1266 len = sizeof(buffer);
1267 strcpy(buffer,"Warning");
1268 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1269 buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1271 ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1272 "Failed to add new header\n");
1275 len = sizeof(buffer);
1276 strcpy(buffer,"Warning");
1277 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1278 buffer,&len,&index),"Unable to query header\n");
1279 ok(index == 1, "Index was not incremented\n");
1280 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1281 ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1282 ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1283 len = sizeof(buffer);
1284 strcpy(buffer,"Warning");
1285 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1286 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1289 len = 5; /* could store the string but not the NULL terminator */
1290 strcpy(buffer,"Warning");
1291 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1292 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1293 ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1294 ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1296 /* a call with NULL will fail but will return the length */
1298 len = sizeof(buffer);
1299 SetLastError(0xdeadbeef);
1300 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1301 NULL,&len,&index) == FALSE,"Query worked\n");
1302 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1303 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1304 ok(index == 0, "Index was incremented\n");
1306 /* even for a len that is too small */
1309 SetLastError(0xdeadbeef);
1310 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1311 NULL,&len,&index) == FALSE,"Query worked\n");
1312 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1313 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1314 ok(index == 0, "Index was incremented\n");
1318 SetLastError(0xdeadbeef);
1319 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1320 NULL,&len,&index) == FALSE,"Query worked\n");
1321 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1322 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1323 ok(index == 0, "Index was incremented\n");
1324 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1327 /* a working query */
1329 len = sizeof(buffer);
1330 memset(buffer, 'x', sizeof(buffer));
1331 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1332 buffer,&len,&index),"Unable to query header\n");
1333 ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1334 ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1335 ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1336 /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1337 ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1338 ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1339 ok(index == 0, "Index was incremented\n");
1341 /* Like above two tests, but for W version */
1345 SetLastError(0xdeadbeef);
1346 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1347 NULL,&len,&index) == FALSE,"Query worked\n");
1348 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1349 ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1350 ok(index == 0, "Index was incremented\n");
1351 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1353 /* a working query */
1355 len = sizeof(wbuffer);
1356 memset(wbuffer, 'x', sizeof(wbuffer));
1357 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1358 wbuffer,&len,&index),"Unable to query header\n");
1359 ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1360 ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1361 ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1362 ok(index == 0, "Index was incremented\n");
1364 /* end of W version tests */
1366 /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1368 len = sizeof(buffer);
1369 memset(buffer, 'x', sizeof(buffer));
1370 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF,
1371 buffer,&len,&index) == TRUE,"Query failed\n");
1372 ok(len == 2, "Expected 2, got %d\n", len);
1373 ok(strcmp(buffer, "\r\n") == 0, "Expected CRLF, got '%s'\n", buffer);
1374 ok(index == 0, "Index was incremented\n");
1376 ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1377 "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1380 len = sizeof(buffer);
1381 strcpy(buffer,"Warning");
1382 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1383 buffer,&len,&index),"Unable to query header\n");
1384 ok(index == 1, "Index was not incremented\n");
1385 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1386 len = sizeof(buffer);
1387 strcpy(buffer,"Warning");
1388 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1389 buffer,&len,&index),"Failed to get second header\n");
1390 ok(index == 2, "Index was not incremented\n");
1391 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1392 len = sizeof(buffer);
1393 strcpy(buffer,"Warning");
1394 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1395 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1397 ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1400 len = sizeof(buffer);
1401 strcpy(buffer,"Warning");
1402 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1403 buffer,&len,&index),"Unable to query header\n");
1404 ok(index == 1, "Index was not incremented\n");
1405 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1406 len = sizeof(buffer);
1407 strcpy(buffer,"Warning");
1408 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1409 buffer,&len,&index),"Failed to get second header\n");
1410 ok(index == 2, "Index was not incremented\n");
1411 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1412 len = sizeof(buffer);
1413 strcpy(buffer,"Warning");
1414 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1415 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1417 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1420 len = sizeof(buffer);
1421 strcpy(buffer,"Warning");
1422 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1423 buffer,&len,&index),"Unable to query header\n");
1424 ok(index == 1, "Index was not incremented\n");
1425 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1426 len = sizeof(buffer);
1427 strcpy(buffer,"Warning");
1428 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1429 buffer,&len,&index),"Failed to get second header\n");
1430 ok(index == 2, "Index was not incremented\n");
1431 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1432 len = sizeof(buffer);
1433 strcpy(buffer,"Warning");
1434 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1435 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1437 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1440 len = sizeof(buffer);
1441 strcpy(buffer,"Warning");
1442 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1443 buffer,&len,&index),"Unable to query header\n");
1444 ok(index == 1, "Index was not incremented\n");
1445 ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1446 len = sizeof(buffer);
1447 strcpy(buffer,"Warning");
1448 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1449 ok(index == 2, "Index was not incremented\n");
1450 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1451 len = sizeof(buffer);
1452 strcpy(buffer,"Warning");
1453 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1455 ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1458 len = sizeof(buffer);
1459 strcpy(buffer,"Warning");
1460 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1461 ok(index == 1, "Index was not incremented\n");
1462 ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1463 len = sizeof(buffer);
1464 strcpy(buffer,"Warning");
1465 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1466 ok(index == 2, "Index was not incremented\n");
1467 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1468 len = sizeof(buffer);
1469 strcpy(buffer,"Warning");
1470 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1472 ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1475 len = sizeof(buffer);
1476 strcpy(buffer,"Warning");
1477 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1478 ok(index == 1, "Index was not incremented\n");
1479 ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1480 len = sizeof(buffer);
1481 strcpy(buffer,"Warning");
1482 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1483 ok(index == 2, "Index was not incremented\n");
1484 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1485 len = sizeof(buffer);
1486 strcpy(buffer,"Warning");
1487 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1489 ok(HttpAddRequestHeaders(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");
1492 len = sizeof(buffer);
1493 strcpy(buffer,"Warning");
1494 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1495 ok(index == 1, "Index was not incremented\n");
1496 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1497 len = sizeof(buffer);
1498 strcpy(buffer,"Warning");
1499 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1500 ok(index == 2, "Index was not incremented\n");
1501 ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1502 len = sizeof(buffer);
1503 strcpy(buffer,"Warning");
1504 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1506 /* Ensure that blank headers are ignored and don't cause a failure */
1507 ok(HttpAddRequestHeaders(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");
1510 len = sizeof(buffer);
1511 strcpy(buffer,"BlankTest");
1512 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1513 ok(index == 1, "Index was not incremented\n");
1514 ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1516 /* Ensure that malformed header separators are ignored and don't cause a failure */
1517 ok(HttpAddRequestHeaders(hRequest,"\r\rMalformedTest:value\n\nMalformedTestTwo: value2\rMalformedTestThree: value3\n\n\r\r\n",-1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE),
1518 "Failed to add header with malformed entries in list\n");
1521 len = sizeof(buffer);
1522 strcpy(buffer,"MalformedTest");
1523 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1524 ok(index == 1, "Index was not incremented\n");
1525 ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1527 len = sizeof(buffer);
1528 strcpy(buffer,"MalformedTestTwo");
1529 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1530 ok(index == 1, "Index was not incremented\n");
1531 ok(strcmp(buffer,"value2")==0, "incorrect string was returned(%s)\n",buffer);
1533 len = sizeof(buffer);
1534 strcpy(buffer,"MalformedTestThree");
1535 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1536 ok(index == 1, "Index was not incremented\n");
1537 ok(strcmp(buffer,"value3")==0, "incorrect string was returned(%s)\n",buffer);
1539 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1541 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1542 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1545 static const char garbagemsg[] =
1546 "Garbage: Header\r\n";
1548 static const char contmsg[] =
1549 "HTTP/1.1 100 Continue\r\n";
1551 static const char expandcontmsg[] =
1552 "HTTP/1.1 100 Continue\r\n"
1553 "Server: winecontinue\r\n"
1554 "Tag: something witty\r\n"
1557 static const char okmsg[] =
1558 "HTTP/1.1 200 OK\r\n"
1559 "Server: winetest\r\n"
1562 static const char okmsg2[] =
1563 "HTTP/1.1 200 OK\r\n"
1564 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
1565 "Server: winetest\r\n"
1566 "Content-Length: 0\r\n"
1567 "Set-Cookie: one\r\n"
1568 "Set-Cookie: two\r\n"
1571 static const char notokmsg[] =
1572 "HTTP/1.1 400 Bad Request\r\n"
1573 "Server: winetest\r\n"
1576 static const char noauthmsg[] =
1577 "HTTP/1.1 401 Unauthorized\r\n"
1578 "Server: winetest\r\n"
1579 "Connection: close\r\n"
1580 "WWW-Authenticate: Basic realm=\"placebo\"\r\n"
1583 static const char noauthmsg2[] =
1584 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed\r\n"
1585 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"
1587 "Server: winetest\r\n";
1589 static const char proxymsg[] =
1590 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1591 "Server: winetest\r\n"
1592 "Proxy-Connection: close\r\n"
1593 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1596 static const char page1[] =
1598 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1599 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1602 struct server_info {
1607 static DWORD CALLBACK server_thread(LPVOID param)
1609 struct server_info *si = param;
1612 struct sockaddr_in sa;
1615 int last_request = 0;
1616 char host_header[22];
1617 static int test_b = 0;
1619 WSAStartup(MAKEWORD(1,1), &wsaData);
1621 s = socket(AF_INET, SOCK_STREAM, 0);
1622 if (s == INVALID_SOCKET)
1626 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1628 memset(&sa, 0, sizeof sa);
1629 sa.sin_family = AF_INET;
1630 sa.sin_port = htons(si->port);
1631 sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1633 r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1639 SetEvent(si->hEvent);
1641 sprintf(host_header, "Host: localhost:%d", si->port);
1645 c = accept(s, NULL, NULL);
1647 memset(buffer, 0, sizeof buffer);
1648 for(i=0; i<(sizeof buffer-1); i++)
1650 r = recv(c, &buffer[i], 1, 0);
1654 if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
1655 buffer[i-3] == '\r' && buffer[i-1] == '\r')
1658 if (strstr(buffer, "GET /test1"))
1660 if (!strstr(buffer, "Content-Length: 0"))
1662 send(c, okmsg, sizeof okmsg-1, 0);
1663 send(c, page1, sizeof page1-1, 0);
1666 send(c, notokmsg, sizeof notokmsg-1, 0);
1668 if (strstr(buffer, "/test2"))
1670 if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
1672 send(c, okmsg, sizeof okmsg-1, 0);
1673 send(c, page1, sizeof page1-1, 0);
1676 send(c, proxymsg, sizeof proxymsg-1, 0);
1678 if (strstr(buffer, "/test3"))
1680 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
1681 send(c, okmsg, sizeof okmsg-1, 0);
1683 send(c, noauthmsg, sizeof noauthmsg-1, 0);
1685 if (strstr(buffer, "/test4"))
1687 if (strstr(buffer, "Connection: Close"))
1688 send(c, okmsg, sizeof okmsg-1, 0);
1690 send(c, notokmsg, sizeof notokmsg-1, 0);
1692 if (strstr(buffer, "POST /test5") ||
1693 strstr(buffer, "RPC_IN_DATA /test5") ||
1694 strstr(buffer, "RPC_OUT_DATA /test5"))
1696 if (strstr(buffer, "Content-Length: 0"))
1698 send(c, okmsg, sizeof okmsg-1, 0);
1699 send(c, page1, sizeof page1-1, 0);
1702 send(c, notokmsg, sizeof notokmsg-1, 0);
1704 if (strstr(buffer, "GET /test6"))
1706 send(c, contmsg, sizeof contmsg-1, 0);
1707 send(c, contmsg, sizeof contmsg-1, 0);
1708 send(c, okmsg, sizeof okmsg-1, 0);
1709 send(c, page1, sizeof page1-1, 0);
1711 if (strstr(buffer, "POST /test7"))
1713 if (strstr(buffer, "Content-Length: 100"))
1715 send(c, okmsg, sizeof okmsg-1, 0);
1716 send(c, page1, sizeof page1-1, 0);
1719 send(c, notokmsg, sizeof notokmsg-1, 0);
1721 if (strstr(buffer, "/test8"))
1723 if (!strstr(buffer, "Connection: Close") &&
1724 strstr(buffer, "Connection: Keep-Alive") &&
1725 !strstr(buffer, "Cache-Control: no-cache") &&
1726 !strstr(buffer, "Pragma: no-cache") &&
1727 strstr(buffer, host_header))
1728 send(c, okmsg, sizeof okmsg-1, 0);
1730 send(c, notokmsg, sizeof notokmsg-1, 0);
1732 if (strstr(buffer, "/test9"))
1734 if (!strstr(buffer, "Connection: Close") &&
1735 !strstr(buffer, "Connection: Keep-Alive") &&
1736 !strstr(buffer, "Cache-Control: no-cache") &&
1737 !strstr(buffer, "Pragma: no-cache") &&
1738 strstr(buffer, host_header))
1739 send(c, okmsg, sizeof okmsg-1, 0);
1741 send(c, notokmsg, sizeof notokmsg-1, 0);
1743 if (strstr(buffer, "/testA"))
1745 if (!strstr(buffer, "Connection: Close") &&
1746 !strstr(buffer, "Connection: Keep-Alive") &&
1747 (strstr(buffer, "Cache-Control: no-cache") ||
1748 strstr(buffer, "Pragma: no-cache")) &&
1749 strstr(buffer, host_header))
1750 send(c, okmsg, sizeof okmsg-1, 0);
1752 send(c, notokmsg, sizeof notokmsg-1, 0);
1754 if (!test_b && strstr(buffer, "/testB HTTP/1.1"))
1757 send(c, okmsg, sizeof okmsg-1, 0);
1758 recvfrom(c, buffer, sizeof buffer, 0, NULL, NULL);
1759 send(c, okmsg, sizeof okmsg-1, 0);
1761 if (strstr(buffer, "/testC"))
1763 if (strstr(buffer, "Cookie: cookie=biscuit"))
1764 send(c, okmsg, sizeof okmsg-1, 0);
1766 send(c, notokmsg, sizeof notokmsg-1, 0);
1768 if (strstr(buffer, "/testD"))
1770 send(c, okmsg2, sizeof okmsg2-1, 0);
1772 if (strstr(buffer, "/testE"))
1774 send(c, noauthmsg2, sizeof noauthmsg2-1, 0);
1776 if (strstr(buffer, "GET /quit"))
1778 send(c, okmsg, sizeof okmsg-1, 0);
1779 send(c, page1, sizeof page1-1, 0);
1782 if (strstr(buffer, "GET /testF"))
1784 send(c, expandcontmsg, sizeof expandcontmsg-1, 0);
1785 send(c, garbagemsg, sizeof garbagemsg-1, 0);
1786 send(c, contmsg, sizeof contmsg-1, 0);
1787 send(c, garbagemsg, sizeof garbagemsg-1, 0);
1788 send(c, okmsg, sizeof okmsg-1, 0);
1789 send(c, page1, sizeof page1-1, 0);
1791 if (strstr(buffer, "GET /testG"))
1793 send(c, page1, sizeof page1-1, 0);
1798 } while (!last_request);
1805 static void test_basic_request(int port, const char *verb, const char *url)
1807 HINTERNET hi, hc, hr;
1811 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1812 ok(hi != NULL, "open failed\n");
1814 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1815 ok(hc != NULL, "connect failed\n");
1817 hr = HttpOpenRequest(hc, verb, url, NULL, NULL, NULL, 0, 0);
1818 ok(hr != NULL, "HttpOpenRequest failed\n");
1820 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1821 ok(r, "HttpSendRequest failed\n");
1824 memset(buffer, 0, sizeof buffer);
1825 SetLastError(0xdeadbeef);
1826 r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1827 ok(r, "InternetReadFile failed %u\n", GetLastError());
1828 ok(count == sizeof page1 - 1, "count was wrong\n");
1829 ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
1831 InternetCloseHandle(hr);
1832 InternetCloseHandle(hc);
1833 InternetCloseHandle(hi);
1836 static void test_last_error(int port)
1838 HINTERNET hi, hc, hr;
1842 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1843 ok(hi != NULL, "open failed\n");
1845 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1846 ok(hc != NULL, "connect failed\n");
1848 hr = HttpOpenRequest(hc, NULL, "/test1", NULL, NULL, NULL, 0, 0);
1849 ok(hr != NULL, "HttpOpenRequest failed\n");
1851 SetLastError(0xdeadbeef);
1852 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1853 error = GetLastError();
1854 ok(r, "HttpSendRequest failed\n");
1855 ok(error == ERROR_SUCCESS || broken(error != ERROR_SUCCESS), "expected ERROR_SUCCESS, got %u\n", error);
1857 InternetCloseHandle(hr);
1858 InternetCloseHandle(hc);
1859 InternetCloseHandle(hi);
1862 static void test_proxy_indirect(int port)
1864 HINTERNET hi, hc, hr;
1868 hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1869 ok(hi != NULL, "open failed\n");
1871 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1872 ok(hc != NULL, "connect failed\n");
1874 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1875 ok(hr != NULL, "HttpOpenRequest failed\n");
1877 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1878 ok(r, "HttpSendRequest failed\n");
1881 r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1882 ok(r || GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo failed: %d\n", GetLastError());
1885 skip("missing proxy header, not testing remaining proxy headers\n");
1888 ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1891 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1892 ok(r, "HttpQueryInfo failed\n");
1893 ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1896 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1897 ok(r, "HttpQueryInfo failed\n");
1898 ok(val == 407, "proxy code wrong\n");
1901 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1902 ok(r, "HttpQueryInfo failed\n");
1903 ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1906 r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1907 ok(r, "HttpQueryInfo failed\n");
1908 ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1911 r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1912 ok(r, "HttpQueryInfo failed\n");
1913 ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1916 r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1917 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1918 ok(r == FALSE, "HttpQueryInfo failed\n");
1921 InternetCloseHandle(hr);
1922 InternetCloseHandle(hc);
1923 InternetCloseHandle(hi);
1926 static void test_proxy_direct(int port)
1928 HINTERNET hi, hc, hr;
1931 static CHAR username[] = "mike",
1932 password[] = "1101";
1934 sprintf(buffer, "localhost:%d\n", port);
1935 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1936 ok(hi != NULL, "open failed\n");
1938 /* try connect without authorization */
1939 hc = InternetConnect(hi, "test.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1940 ok(hc != NULL, "connect failed\n");
1942 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1943 ok(hr != NULL, "HttpOpenRequest failed\n");
1945 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1946 ok(r, "HttpSendRequest failed\n");
1949 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1950 ok(r, "HttpQueryInfo failed\n");
1951 ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1954 /* set the user + password then try again */
1956 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
1957 ok(r, "failed to set user\n");
1959 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
1960 ok(r, "failed to set password\n");
1963 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1964 ok(r, "HttpSendRequest failed\n");
1966 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1967 ok(r, "HttpQueryInfo failed\n");
1969 ok(!strcmp(buffer, "200"), "proxy code wrong\n");
1973 InternetCloseHandle(hr);
1974 InternetCloseHandle(hc);
1975 InternetCloseHandle(hi);
1978 static void test_header_handling_order(int port)
1980 static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
1981 static char connection[] = "Connection: Close";
1983 static const char *types[2] = { "*", NULL };
1984 HINTERNET session, connect, request;
1988 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1989 ok(session != NULL, "InternetOpen failed\n");
1991 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1992 ok(connect != NULL, "InternetConnect failed\n");
1994 request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1995 ok(request != NULL, "HttpOpenRequest failed\n");
1997 ret = HttpAddRequestHeaders(request, authorization, ~0u, HTTP_ADDREQ_FLAG_ADD);
1998 ok(ret, "HttpAddRequestHeaders failed\n");
2000 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
2001 ok(ret, "HttpSendRequest failed\n");
2004 size = sizeof(status);
2005 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2006 ok(ret, "HttpQueryInfo failed\n");
2007 ok(status == 200, "request failed with status %u\n", status);
2009 InternetCloseHandle(request);
2011 request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
2012 ok(request != NULL, "HttpOpenRequest failed\n");
2014 ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
2015 ok(ret, "HttpSendRequest failed\n");
2018 size = sizeof(status);
2019 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2020 ok(ret, "HttpQueryInfo failed\n");
2021 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
2023 InternetCloseHandle(request);
2025 request = HttpOpenRequest(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
2026 ok(request != NULL, "HttpOpenRequest failed\n");
2028 ret = HttpAddRequestHeaders(request, "Content-Length: 100\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2029 ok(ret, "HttpAddRequestHeaders failed\n");
2031 ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
2032 ok(ret, "HttpSendRequest failed\n");
2035 size = sizeof(status);
2036 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2037 ok(ret, "HttpQueryInfo failed\n");
2038 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
2040 InternetCloseHandle(request);
2041 InternetCloseHandle(connect);
2042 InternetCloseHandle(session);
2045 static void test_connection_header(int port)
2047 HINTERNET ses, con, req;
2051 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2052 ok(ses != NULL, "InternetOpen failed\n");
2054 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2055 ok(con != NULL, "InternetConnect failed\n");
2057 req = HttpOpenRequest(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2058 ok(req != NULL, "HttpOpenRequest failed\n");
2060 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2061 ok(ret, "HttpSendRequest failed\n");
2064 size = sizeof(status);
2065 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2066 ok(ret, "HttpQueryInfo failed\n");
2067 ok(status == 200, "request failed with status %u\n", status);
2069 InternetCloseHandle(req);
2071 req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
2072 ok(req != NULL, "HttpOpenRequest failed\n");
2074 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2075 ok(ret, "HttpSendRequest failed\n");
2078 size = sizeof(status);
2079 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2080 ok(ret, "HttpQueryInfo failed\n");
2081 ok(status == 200, "request failed with status %u\n", status);
2083 InternetCloseHandle(req);
2085 req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
2086 ok(req != NULL, "HttpOpenRequest failed\n");
2088 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2089 ok(ret, "HttpSendRequest failed\n");
2092 size = sizeof(status);
2093 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2094 ok(ret, "HttpQueryInfo failed\n");
2095 ok(status == 200, "request failed with status %u\n", status);
2097 InternetCloseHandle(req);
2099 req = HttpOpenRequest(con, "POST", "/testA", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
2100 ok(req != NULL, "HttpOpenRequest failed\n");
2102 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2103 ok(ret, "HttpSendRequest failed\n");
2106 size = sizeof(status);
2107 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2108 ok(ret, "HttpQueryInfo failed\n");
2109 ok(status == 200, "request failed with status %u\n", status);
2111 InternetCloseHandle(req);
2112 InternetCloseHandle(con);
2113 InternetCloseHandle(ses);
2116 static void test_http1_1(int port)
2118 HINTERNET ses, con, req;
2121 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2122 ok(ses != NULL, "InternetOpen failed\n");
2124 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2125 ok(con != NULL, "InternetConnect failed\n");
2127 req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2128 ok(req != NULL, "HttpOpenRequest failed\n");
2130 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2133 InternetCloseHandle(req);
2135 req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2136 ok(req != NULL, "HttpOpenRequest failed\n");
2138 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2139 ok(ret, "HttpSendRequest failed\n");
2142 InternetCloseHandle(req);
2143 InternetCloseHandle(con);
2144 InternetCloseHandle(ses);
2147 static void test_HttpSendRequestW(int port)
2149 static const WCHAR header[] = {'U','A','-','C','P','U',':',' ','x','8','6',0};
2150 HINTERNET ses, con, req;
2154 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
2155 ok(ses != NULL, "InternetOpen failed\n");
2157 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2158 ok(con != NULL, "InternetConnect failed\n");
2160 req = HttpOpenRequest(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
2161 ok(req != NULL, "HttpOpenRequest failed\n");
2163 SetLastError(0xdeadbeef);
2164 ret = HttpSendRequestW(req, header, ~0u, NULL, 0);
2165 error = GetLastError();
2166 ok(!ret, "HttpSendRequestW succeeded\n");
2167 ok(error == ERROR_IO_PENDING ||
2168 broken(error == ERROR_HTTP_HEADER_NOT_FOUND) || /* IE6 */
2169 broken(error == ERROR_INVALID_PARAMETER), /* IE5 */
2170 "got %u expected ERROR_IO_PENDING\n", error);
2172 InternetCloseHandle(req);
2173 InternetCloseHandle(con);
2174 InternetCloseHandle(ses);
2177 static void test_cookie_header(int port)
2179 HINTERNET ses, con, req;
2180 DWORD size, status, error;
2184 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2185 ok(ses != NULL, "InternetOpen failed\n");
2187 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2188 ok(con != NULL, "InternetConnect failed\n");
2190 InternetSetCookie("http://localhost", "cookie", "biscuit");
2192 req = HttpOpenRequest(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2193 ok(req != NULL, "HttpOpenRequest failed\n");
2196 size = sizeof(buffer);
2197 SetLastError(0xdeadbeef);
2198 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2199 error = GetLastError();
2200 ok(!ret, "HttpQueryInfo succeeded\n");
2201 ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %u expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
2203 ret = HttpAddRequestHeaders(req, "Cookie: cookie=not biscuit\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD);
2204 ok(ret, "HttpAddRequestHeaders failed: %u\n", GetLastError());
2207 size = sizeof(buffer);
2208 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2209 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2210 ok(!strcmp(buffer, "cookie=not biscuit"), "got '%s' expected \'cookie=not biscuit\'\n", buffer);
2212 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2213 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
2216 size = sizeof(status);
2217 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2218 ok(ret, "HttpQueryInfo failed\n");
2219 ok(status == 200, "request failed with status %u\n", status);
2222 size = sizeof(buffer);
2223 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2224 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2225 ok(!strcmp(buffer, "cookie=biscuit"), "got '%s' expected \'cookie=biscuit\'\n", buffer);
2227 InternetCloseHandle(req);
2228 InternetCloseHandle(con);
2229 InternetCloseHandle(ses);
2232 static void test_basic_authentication(int port)
2234 HINTERNET session, connect, request;
2238 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2239 ok(session != NULL, "InternetOpen failed\n");
2241 connect = InternetConnect(session, "localhost", port, "user", "pwd", INTERNET_SERVICE_HTTP, 0, 0);
2242 ok(connect != NULL, "InternetConnect failed\n");
2244 request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, NULL, 0, 0);
2245 ok(request != NULL, "HttpOpenRequest failed\n");
2247 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
2248 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
2251 size = sizeof(status);
2252 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2253 ok(ret, "HttpQueryInfo failed\n");
2254 ok(status == 200, "request failed with status %u\n", status);
2256 InternetCloseHandle(request);
2257 InternetCloseHandle(connect);
2258 InternetCloseHandle(session);
2261 static void test_invalid_response_headers(int port)
2263 HINTERNET session, connect, request;
2268 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2269 ok(session != NULL, "InternetOpen failed\n");
2271 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2272 ok(connect != NULL, "InternetConnect failed\n");
2274 request = HttpOpenRequest(connect, NULL, "/testE", NULL, NULL, NULL, 0, 0);
2275 ok(request != NULL, "HttpOpenRequest failed\n");
2277 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
2278 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
2281 size = sizeof(status);
2282 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2283 ok(ret, "HttpQueryInfo failed\n");
2284 ok(status == 401, "unexpected status %u\n", status);
2287 size = sizeof(buffer);
2288 ret = HttpQueryInfo( request, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
2289 ok(ret, "HttpQueryInfo failed\n");
2290 ok(!strcmp(buffer, "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"),
2291 "headers wrong \"%s\"\n", buffer);
2294 size = sizeof(buffer);
2295 ret = HttpQueryInfo( request, HTTP_QUERY_SERVER, buffer, &size, NULL);
2296 ok(ret, "HttpQueryInfo failed\n");
2297 ok(!strcmp(buffer, "winetest"), "server wrong \"%s\"\n", buffer);
2299 InternetCloseHandle(request);
2300 InternetCloseHandle(connect);
2301 InternetCloseHandle(session);
2304 static void test_response_without_headers(int port)
2306 HINTERNET hi, hc, hr;
2307 DWORD r, count, size, status;
2310 SetLastError(0xdeadbeef);
2311 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2312 ok(hi != NULL, "open failed %u\n", GetLastError());
2314 SetLastError(0xdeadbeef);
2315 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2316 ok(hc != NULL, "connect failed %u\n", GetLastError());
2318 SetLastError(0xdeadbeef);
2319 hr = HttpOpenRequest(hc, NULL, "/testG", NULL, NULL, NULL, 0, 0);
2320 ok(hr != NULL, "HttpOpenRequest failed %u\n", GetLastError());
2322 SetLastError(0xdeadbeef);
2323 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2324 ok(r, "HttpSendRequest failed %u\n", GetLastError());
2327 memset(buffer, 0, sizeof buffer);
2328 SetLastError(0xdeadbeef);
2329 r = InternetReadFile(hr, buffer, sizeof buffer, &count);
2330 ok(r, "InternetReadFile failed %u\n", GetLastError());
2331 todo_wine ok(count == sizeof page1 - 1, "count was wrong\n");
2332 todo_wine ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
2335 size = sizeof(status);
2336 SetLastError(0xdeadbeef);
2337 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2338 todo_wine ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2339 todo_wine ok(status == 200, "expected status 200 got %u\n", status);
2342 size = sizeof(buffer);
2343 SetLastError(0xdeadbeef);
2344 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, NULL );
2345 ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2346 ok(!strcmp(buffer, "OK"), "expected OK got: \"%s\"\n", buffer);
2349 size = sizeof(buffer);
2350 SetLastError(0xdeadbeef);
2351 r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &size, NULL);
2352 ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2353 ok(!strcmp(buffer, "HTTP/1.0"), "expected HTTP/1.0 got: \"%s\"\n", buffer);
2356 size = sizeof(buffer);
2357 SetLastError(0xdeadbeef);
2358 r = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
2359 ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2360 ok(!strcmp(buffer, "HTTP/1.0 200 OK"), "raw headers wrong: \"%s\"\n", buffer);
2362 InternetCloseHandle(hr);
2363 InternetCloseHandle(hc);
2364 InternetCloseHandle(hi);
2367 static void test_HttpQueryInfo(int port)
2369 HINTERNET hi, hc, hr;
2374 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2375 ok(hi != NULL, "InternetOpen failed\n");
2377 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2378 ok(hc != NULL, "InternetConnect failed\n");
2380 hr = HttpOpenRequest(hc, NULL, "/testD", NULL, NULL, NULL, 0, 0);
2381 ok(hr != NULL, "HttpOpenRequest failed\n");
2383 ret = HttpSendRequest(hr, NULL, 0, NULL, 0);
2384 ok(ret, "HttpSendRequest failed\n");
2387 size = sizeof(buffer);
2388 ret = HttpQueryInfo(hr, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &index);
2389 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2390 ok(index == 1, "expected 1 got %u\n", index);
2393 size = sizeof(buffer);
2394 ret = HttpQueryInfo(hr, HTTP_QUERY_DATE | HTTP_QUERY_FLAG_SYSTEMTIME, buffer, &size, &index);
2395 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2396 ok(index == 1, "expected 1 got %u\n", index);
2399 size = sizeof(buffer);
2400 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
2401 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2402 ok(index == 0, "expected 0 got %u\n", index);
2404 size = sizeof(buffer);
2405 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
2406 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2407 ok(index == 0, "expected 0 got %u\n", index);
2409 size = sizeof(buffer);
2410 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &size, &index);
2411 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2412 ok(index == 0, "expected 0 got %u\n", index);
2414 size = sizeof(buffer);
2415 ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
2416 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2417 ok(index == 0, "expected 0 got %u\n", index);
2419 size = sizeof(buffer);
2420 ret = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &size, &index);
2421 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2422 ok(index == 0, "expected 0 got %u\n", index);
2425 size = sizeof(buffer);
2426 ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &size, &index);
2427 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2428 ok(index == 0, "expected 0 got %u\n", index);
2431 size = sizeof(buffer);
2432 ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, buffer, &size, &index);
2433 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2434 ok(index == 0, "expected 0 got %u\n", index);
2437 size = sizeof(buffer);
2438 ret = HttpQueryInfo(hr, HTTP_QUERY_FORWARDED, buffer, &size, &index);
2439 ok(!ret, "HttpQueryInfo succeeded\n");
2440 ok(index == 0xdeadbeef, "expected 0xdeadbeef got %u\n", index);
2443 size = sizeof(buffer);
2444 ret = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &size, &index);
2445 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2446 ok(index == 1, "expected 1 got %u\n", index);
2449 size = sizeof(buffer);
2450 strcpy(buffer, "Server");
2451 ret = HttpQueryInfo(hr, HTTP_QUERY_CUSTOM, buffer, &size, &index);
2452 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2453 ok(index == 1, "expected 1 got %u\n", index);
2456 size = sizeof(buffer);
2457 ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
2458 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2459 ok(index == 1, "expected 1 got %u\n", index);
2461 size = sizeof(buffer);
2462 ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
2463 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2464 ok(index == 2, "expected 2 got %u\n", index);
2466 InternetCloseHandle(hr);
2467 InternetCloseHandle(hc);
2468 InternetCloseHandle(hi);
2471 static void test_options(int port)
2473 HINTERNET ses, con, req;
2478 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2479 ok(ses != NULL, "InternetOpen failed\n");
2481 SetLastError(0xdeadbeef);
2482 ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, 0);
2483 error = GetLastError();
2484 ok(!ret, "InternetSetOption succeeded\n");
2485 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2487 SetLastError(0xdeadbeef);
2488 ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, sizeof(ctx));
2489 ok(!ret, "InternetSetOption succeeded\n");
2490 error = GetLastError();
2491 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2493 SetLastError(0xdeadbeef);
2494 ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, 0);
2495 ok(!ret, "InternetSetOption succeeded\n");
2496 error = GetLastError();
2497 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2500 ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
2501 ok(ret, "InternetSetOption failed %u\n", GetLastError());
2503 SetLastError(0xdeadbeef);
2504 ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, NULL);
2505 error = GetLastError();
2506 ok(!ret, "InternetQueryOption succeeded\n");
2507 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2509 SetLastError(0xdeadbeef);
2510 ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, NULL);
2511 error = GetLastError();
2512 ok(!ret, "InternetQueryOption succeeded\n");
2513 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2516 SetLastError(0xdeadbeef);
2517 ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, &size);
2518 error = GetLastError();
2519 ok(!ret, "InternetQueryOption succeeded\n");
2520 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
2523 SetLastError(0xdeadbeef);
2524 ret = InternetQueryOption(NULL, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2525 error = GetLastError();
2526 ok(!ret, "InternetQueryOption succeeded\n");
2527 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %u\n", error);
2531 ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2532 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
2533 ok(ctx == 1, "expected 1 got %lu\n", ctx);
2535 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2536 ok(con != NULL, "InternetConnect failed\n");
2540 ret = InternetQueryOption(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2541 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
2542 ok(ctx == 0, "expected 0 got %lu\n", ctx);
2545 ret = InternetSetOption(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
2546 ok(ret, "InternetSetOption failed %u\n", GetLastError());
2550 ret = InternetQueryOption(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2551 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
2552 ok(ctx == 2, "expected 2 got %lu\n", ctx);
2554 req = HttpOpenRequest(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
2555 ok(req != NULL, "HttpOpenRequest failed\n");
2559 ret = InternetQueryOption(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2560 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
2561 ok(ctx == 0, "expected 0 got %lu\n", ctx);
2564 ret = InternetSetOption(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
2565 ok(ret, "InternetSetOption failed %u\n", GetLastError());
2569 ret = InternetQueryOption(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2570 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
2571 ok(ctx == 3, "expected 3 got %lu\n", ctx);
2573 InternetCloseHandle(req);
2574 InternetCloseHandle(con);
2575 InternetCloseHandle(ses);
2578 static void test_http_connection(void)
2580 struct server_info si;
2584 si.hEvent = CreateEvent(NULL, 0, 0, NULL);
2587 hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
2588 ok( hThread != NULL, "create thread failed\n");
2590 r = WaitForSingleObject(si.hEvent, 10000);
2591 ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
2592 if (r != WAIT_OBJECT_0)
2595 test_basic_request(si.port, "GET", "/test1");
2596 test_proxy_indirect(si.port);
2597 test_proxy_direct(si.port);
2598 test_header_handling_order(si.port);
2599 test_basic_request(si.port, "POST", "/test5");
2600 test_basic_request(si.port, "RPC_IN_DATA", "/test5");
2601 test_basic_request(si.port, "RPC_OUT_DATA", "/test5");
2602 test_basic_request(si.port, "GET", "/test6");
2603 test_basic_request(si.port, "GET", "/testF");
2604 test_connection_header(si.port);
2605 test_http1_1(si.port);
2606 test_cookie_header(si.port);
2607 test_basic_authentication(si.port);
2608 test_invalid_response_headers(si.port);
2609 test_response_without_headers(si.port);
2610 test_HttpQueryInfo(si.port);
2611 test_HttpSendRequestW(si.port);
2612 test_last_error(si.port);
2613 test_options(si.port);
2615 /* send the basic request again to shutdown the server thread */
2616 test_basic_request(si.port, "GET", "/quit");
2618 r = WaitForSingleObject(hThread, 3000);
2619 ok( r == WAIT_OBJECT_0, "thread wait failed\n");
2620 CloseHandle(hThread);
2623 static void release_cert_info(INTERNET_CERTIFICATE_INFOA *info)
2625 LocalFree(info->lpszSubjectInfo);
2626 LocalFree(info->lpszIssuerInfo);
2627 LocalFree(info->lpszProtocolName);
2628 LocalFree(info->lpszSignatureAlgName);
2629 LocalFree(info->lpszEncryptionAlgName);
2632 static void test_secure_connection(void)
2634 static const WCHAR gizmo5[] = {'G','i','z','m','o','5',0};
2635 static const WCHAR testbot[] = {'t','e','s','t','b','o','t','.','w','i','n','e','h','q','.','o','r','g',0};
2636 static const WCHAR get[] = {'G','E','T',0};
2637 static const WCHAR slash[] = {'/',0};
2638 HINTERNET ses, con, req;
2640 INTERNET_CERTIFICATE_INFOA *certificate_structA = NULL;
2641 INTERNET_CERTIFICATE_INFOW *certificate_structW = NULL;
2644 ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
2645 ok(ses != NULL, "InternetOpen failed\n");
2647 con = InternetConnect(ses, "testbot.winehq.org",
2648 INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL,
2649 INTERNET_SERVICE_HTTP, 0, 0);
2650 ok(con != NULL, "InternetConnect failed\n");
2652 req = HttpOpenRequest(con, "GET", "/", NULL, NULL, NULL,
2653 INTERNET_FLAG_SECURE, 0);
2654 ok(req != NULL, "HttpOpenRequest failed\n");
2656 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2657 ok(ret, "HttpSendRequest failed: %d\n", GetLastError());
2659 size = sizeof(flags);
2660 ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
2661 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2662 ok(flags & SECURITY_FLAG_SECURE, "expected secure flag to be set\n");
2664 ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2666 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
2667 ok(size == sizeof(INTERNET_CERTIFICATE_INFOA), "size = %d\n", size);
2668 certificate_structA = HeapAlloc(GetProcessHeap(), 0, size);
2669 ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2670 certificate_structA, &size);
2671 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2674 ok(certificate_structA->lpszSubjectInfo &&
2675 strlen(certificate_structA->lpszSubjectInfo) > 1,
2676 "expected a non-empty subject name\n");
2677 ok(certificate_structA->lpszIssuerInfo &&
2678 strlen(certificate_structA->lpszIssuerInfo) > 1,
2679 "expected a non-empty issuer name\n");
2680 ok(!certificate_structA->lpszSignatureAlgName,
2681 "unexpected signature algorithm name\n");
2682 ok(!certificate_structA->lpszEncryptionAlgName,
2683 "unexpected encryption algorithm name\n");
2684 ok(!certificate_structA->lpszProtocolName,
2685 "unexpected protocol name\n");
2686 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
2687 release_cert_info(certificate_structA);
2689 HeapFree(GetProcessHeap(), 0, certificate_structA);
2691 /* Querying the same option through InternetQueryOptionW still results in
2692 * ASCII strings being returned.
2695 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2697 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
2698 ok(size == sizeof(INTERNET_CERTIFICATE_INFOW), "size = %d\n", size);
2699 certificate_structW = HeapAlloc(GetProcessHeap(), 0, size);
2700 ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2701 certificate_structW, &size);
2702 certificate_structA = (INTERNET_CERTIFICATE_INFOA *)certificate_structW;
2703 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2706 ok(certificate_structA->lpszSubjectInfo &&
2707 strlen(certificate_structA->lpszSubjectInfo) > 1,
2708 "expected a non-empty subject name\n");
2709 ok(certificate_structA->lpszIssuerInfo &&
2710 strlen(certificate_structA->lpszIssuerInfo) > 1,
2711 "expected a non-empty issuer name\n");
2712 ok(!certificate_structA->lpszSignatureAlgName,
2713 "unexpected signature algorithm name\n");
2714 ok(!certificate_structA->lpszEncryptionAlgName,
2715 "unexpected encryption algorithm name\n");
2716 ok(!certificate_structA->lpszProtocolName,
2717 "unexpected protocol name\n");
2718 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
2719 release_cert_info(certificate_structA);
2721 HeapFree(GetProcessHeap(), 0, certificate_structW);
2723 InternetCloseHandle(req);
2724 InternetCloseHandle(con);
2725 InternetCloseHandle(ses);
2727 /* Repeating the tests with the W functions has the same result: */
2728 ses = InternetOpenW(gizmo5, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
2729 ok(ses != NULL, "InternetOpen failed\n");
2731 con = InternetConnectW(ses, testbot,
2732 INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL,
2733 INTERNET_SERVICE_HTTP, 0, 0);
2734 ok(con != NULL, "InternetConnect failed\n");
2736 req = HttpOpenRequestW(con, get, slash, NULL, NULL, NULL,
2737 INTERNET_FLAG_SECURE, 0);
2738 ok(req != NULL, "HttpOpenRequest failed\n");
2740 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2741 ok(ret, "HttpSendRequest failed: %d\n", GetLastError());
2743 size = sizeof(flags);
2744 ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
2745 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2746 ok(flags & SECURITY_FLAG_SECURE, "expected secure flag to be set\n");
2748 ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2750 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
2751 ok(size == sizeof(INTERNET_CERTIFICATE_INFOA), "size = %d\n", size);
2752 certificate_structA = HeapAlloc(GetProcessHeap(), 0, size);
2753 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2754 certificate_structA, &size);
2755 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2758 ok(certificate_structA->lpszSubjectInfo &&
2759 strlen(certificate_structA->lpszSubjectInfo) > 1,
2760 "expected a non-empty subject name\n");
2761 ok(certificate_structA->lpszIssuerInfo &&
2762 strlen(certificate_structA->lpszIssuerInfo) > 1,
2763 "expected a non-empty issuer name\n");
2764 ok(!certificate_structA->lpszSignatureAlgName,
2765 "unexpected signature algorithm name\n");
2766 ok(!certificate_structA->lpszEncryptionAlgName,
2767 "unexpected encryption algorithm name\n");
2768 ok(!certificate_structA->lpszProtocolName,
2769 "unexpected protocol name\n");
2770 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
2771 release_cert_info(certificate_structA);
2773 HeapFree(GetProcessHeap(), 0, certificate_structA);
2775 /* Again, querying the same option through InternetQueryOptionW still
2776 * results in ASCII strings being returned.
2779 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2781 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
2782 ok(size == sizeof(INTERNET_CERTIFICATE_INFOW), "size = %d\n", size);
2783 certificate_structW = HeapAlloc(GetProcessHeap(), 0, size);
2784 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2785 certificate_structW, &size);
2786 certificate_structA = (INTERNET_CERTIFICATE_INFOA *)certificate_structW;
2787 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2790 ok(certificate_structA->lpszSubjectInfo &&
2791 strlen(certificate_structA->lpszSubjectInfo) > 1,
2792 "expected a non-empty subject name\n");
2793 ok(certificate_structA->lpszIssuerInfo &&
2794 strlen(certificate_structA->lpszIssuerInfo) > 1,
2795 "expected a non-empty issuer name\n");
2796 ok(!certificate_structA->lpszSignatureAlgName,
2797 "unexpected signature algorithm name\n");
2798 ok(!certificate_structA->lpszEncryptionAlgName,
2799 "unexpected encryption algorithm name\n");
2800 ok(!certificate_structA->lpszProtocolName,
2801 "unexpected protocol name\n");
2802 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
2803 release_cert_info(certificate_structA);
2805 HeapFree(GetProcessHeap(), 0, certificate_structW);
2807 InternetCloseHandle(req);
2808 InternetCloseHandle(con);
2809 InternetCloseHandle(ses);
2812 static void test_user_agent_header(void)
2814 HINTERNET ses, con, req;
2819 ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
2820 ok(ses != NULL, "InternetOpen failed\n");
2822 con = InternetConnect(ses, "test.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2823 ok(con != NULL, "InternetConnect failed\n");
2825 req = HttpOpenRequest(con, "GET", "/hello.html", "HTTP/1.0", NULL, NULL, 0, 0);
2826 ok(req != NULL, "HttpOpenRequest failed\n");
2828 size = sizeof(buffer);
2829 ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2830 err = GetLastError();
2831 ok(!ret, "HttpQueryInfo succeeded\n");
2832 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2834 ret = HttpAddRequestHeaders(req, "User-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2835 ok(ret, "HttpAddRequestHeaders succeeded\n");
2837 size = sizeof(buffer);
2838 ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2839 err = GetLastError();
2840 ok(ret, "HttpQueryInfo failed\n");
2841 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2843 InternetCloseHandle(req);
2845 req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
2846 ok(req != NULL, "HttpOpenRequest failed\n");
2848 size = sizeof(buffer);
2849 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2850 err = GetLastError();
2851 ok(!ret, "HttpQueryInfo succeeded\n");
2852 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2854 ret = HttpAddRequestHeaders(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2855 ok(ret, "HttpAddRequestHeaders failed\n");
2858 size = sizeof(buffer);
2859 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2860 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2861 ok(!strcmp(buffer, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
2863 InternetCloseHandle(req);
2864 InternetCloseHandle(con);
2865 InternetCloseHandle(ses);
2868 static void test_bogus_accept_types_array(void)
2870 HINTERNET ses, con, req;
2871 static const char *types[] = { (const char *)6240, "*/*", "%p", "", (const char *)0xffffffff, "*/*", NULL };
2876 ses = InternetOpen("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
2877 con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2878 req = HttpOpenRequest(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
2880 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
2883 size = sizeof(buffer);
2884 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2885 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2886 ok(!strcmp(buffer, ", */*, %p, , , */*") || /* IE6 */
2887 !strcmp(buffer, "*/*, %p, */*"),
2888 "got '%s' expected '*/*, %%p, */*' or ', */*, %%p, , , */*'\n", buffer);
2890 InternetCloseHandle(req);
2891 InternetCloseHandle(con);
2892 InternetCloseHandle(ses);
2901 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
2903 INTERNET_ASYNC_RESULT *result = info;
2904 struct context *ctx = (struct context *)context;
2906 trace("%p 0x%08lx %u %p 0x%08x\n", handle, context, status, info, size);
2908 if (status == INTERNET_STATUS_REQUEST_COMPLETE)
2910 trace("request handle: 0x%08lx\n", result->dwResult);
2911 ctx->req = (HINTERNET)result->dwResult;
2912 SetEvent(ctx->event);
2914 if (status == INTERNET_STATUS_HANDLE_CLOSING)
2916 DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
2918 if (InternetQueryOption(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
2919 ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
2920 SetEvent(ctx->event);
2924 static void test_open_url_async(void)
2933 ctx.event = CreateEvent(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
2935 ses = InternetOpen("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
2936 ok(ses != NULL, "InternetOpen failed\n");
2938 SetLastError(0xdeadbeef);
2939 ret = InternetSetOptionA(NULL, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
2940 error = GetLastError();
2941 ok(!ret, "InternetSetOptionA succeeded\n");
2942 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "got %u expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE\n", error);
2944 ret = InternetSetOptionA(ses, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
2945 error = GetLastError();
2946 ok(!ret, "InternetSetOptionA failed\n");
2947 ok(error == ERROR_INTERNET_OPTION_NOT_SETTABLE, "got %u expected ERROR_INTERNET_OPTION_NOT_SETTABLE\n", error);
2949 pInternetSetStatusCallbackA(ses, cb);
2950 ResetEvent(ctx.event);
2952 req = InternetOpenUrl(ses, "http://test.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
2953 ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
2955 WaitForSingleObject(ctx.event, INFINITE);
2958 size = sizeof(type);
2959 ret = InternetQueryOption(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
2960 ok(ret, "InternetQueryOption failed: %u\n", GetLastError());
2961 ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
2962 "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %u\n", type);
2965 ret = HttpQueryInfo(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
2966 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
2967 ok(size > 0, "expected size > 0\n");
2969 ResetEvent(ctx.event);
2970 InternetCloseHandle(ctx.req);
2971 WaitForSingleObject(ctx.event, INFINITE);
2973 InternetCloseHandle(ses);
2974 CloseHandle(ctx.event);
2979 internet_connect = 1,
2981 http_send_request_ex,
2984 internet_close_handle
2989 enum api function; /* api responsible for notification */
2990 unsigned int status; /* status received */
2991 int async; /* delivered from another thread? */
2999 const struct notification *test;
3007 static CRITICAL_SECTION notification_cs;
3009 static void CALLBACK check_notification( HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID buffer, DWORD buflen )
3011 BOOL status_ok, function_ok;
3012 struct info *info = (struct info *)context;
3015 EnterCriticalSection( ¬ification_cs );
3017 if (status == INTERNET_STATUS_HANDLE_CREATED)
3019 DWORD size = sizeof(struct info *);
3020 HttpQueryInfoA( handle, INTERNET_OPTION_CONTEXT_VALUE, &info, &size, 0 );
3023 if (i >= info->count)
3025 LeaveCriticalSection( ¬ification_cs );
3029 while (info->test[i].status != status && info->test[i].optional &&
3030 i < info->count - 1 &&
3031 info->test[i].function == info->test[i + 1].function)
3036 status_ok = (info->test[i].status == status);
3037 function_ok = (info->test[i].function == info->function);
3039 if (!info->test[i].todo)
3041 ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
3042 ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
3044 if (info->test[i].async)
3045 ok(info->thread != GetCurrentThreadId(), "%u: expected thread %u got %u\n",
3046 info->line, info->thread, GetCurrentThreadId());
3050 todo_wine ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
3052 todo_wine ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
3054 if (i == info->count - 1 || info->test[i].function != info->test[i + 1].function) SetEvent( info->wait );
3057 LeaveCriticalSection( ¬ification_cs );
3060 static void setup_test( struct info *info, enum api function, unsigned int line )
3062 info->function = function;
3066 static const struct notification async_send_request_ex_test[] =
3068 { internet_connect, INTERNET_STATUS_HANDLE_CREATED, 0 },
3069 { http_open_request, INTERNET_STATUS_HANDLE_CREATED, 0 },
3070 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, 1, 0, 1 },
3071 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, 1, 0, 1 },
3072 { http_send_request_ex, INTERNET_STATUS_NAME_RESOLVED, 1, 0, 1 },
3073 { http_send_request_ex, INTERNET_STATUS_CONNECTING_TO_SERVER, 1 },
3074 { http_send_request_ex, INTERNET_STATUS_CONNECTED_TO_SERVER, 1 },
3075 { http_send_request_ex, INTERNET_STATUS_SENDING_REQUEST, 1 },
3076 { http_send_request_ex, INTERNET_STATUS_REQUEST_SENT, 1 },
3077 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
3078 { internet_writefile, INTERNET_STATUS_SENDING_REQUEST, 0 },
3079 { internet_writefile, INTERNET_STATUS_REQUEST_SENT, 0 },
3080 { http_end_request, INTERNET_STATUS_RECEIVING_RESPONSE, 1 },
3081 { http_end_request, INTERNET_STATUS_RESPONSE_RECEIVED, 1 },
3082 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
3083 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, 0, 0, 1 },
3084 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, 0, 0, 1 },
3085 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, },
3086 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, }
3089 static void test_async_HttpSendRequestEx(void)
3092 HINTERNET ses, req, con;
3094 DWORD size, written, error;
3095 INTERNET_BUFFERSA b;
3096 static const char *accept[2] = {"*/*", NULL};
3097 static char data[] = "Public ID=codeweavers";
3100 InitializeCriticalSection( ¬ification_cs );
3102 info.test = async_send_request_ex_test;
3103 info.count = sizeof(async_send_request_ex_test)/sizeof(async_send_request_ex_test[0]);
3105 info.wait = CreateEvent( NULL, FALSE, FALSE, NULL );
3106 info.thread = GetCurrentThreadId();
3108 ses = InternetOpen( "winetest", 0, NULL, NULL, INTERNET_FLAG_ASYNC );
3109 ok( ses != NULL, "InternetOpen failed\n" );
3111 pInternetSetStatusCallbackA( ses, check_notification );
3113 setup_test( &info, internet_connect, __LINE__ );
3114 con = InternetConnect( ses, "crossover.codeweavers.com", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, (DWORD_PTR)&info );
3115 ok( con != NULL, "InternetConnect failed %u\n", GetLastError() );
3117 WaitForSingleObject( info.wait, 10000 );
3119 setup_test( &info, http_open_request, __LINE__ );
3120 req = HttpOpenRequest( con, "POST", "posttest.php", NULL, NULL, accept, 0, (DWORD_PTR)&info );
3121 ok( req != NULL, "HttpOpenRequest failed %u\n", GetLastError() );
3123 WaitForSingleObject( info.wait, 10000 );
3125 memset( &b, 0, sizeof(INTERNET_BUFFERSA) );
3126 b.dwStructSize = sizeof(INTERNET_BUFFERSA);
3127 b.lpcszHeader = "Content-Type: application/x-www-form-urlencoded";
3128 b.dwHeadersLength = strlen( b.lpcszHeader );
3129 b.dwBufferTotal = strlen( data );
3131 setup_test( &info, http_send_request_ex, __LINE__ );
3132 ret = HttpSendRequestExA( req, &b, NULL, 0x28, 0 );
3133 ok( !ret && GetLastError() == ERROR_IO_PENDING, "HttpSendRequestExA failed %d %u\n", ret, GetLastError() );
3135 WaitForSingleObject( info.wait, 10000 );
3137 size = sizeof(buffer);
3138 SetLastError( 0xdeadbeef );
3139 ret = HttpQueryInfoA( req, HTTP_QUERY_CONTENT_ENCODING, buffer, &size, 0 );
3140 error = GetLastError();
3141 ok( !ret, "HttpQueryInfoA failed %u\n", GetLastError() );
3143 ok( error == ERROR_INTERNET_INCORRECT_HANDLE_STATE,
3144 "expected ERROR_INTERNET_INCORRECT_HANDLE_STATE got %u\n", error );
3147 size = strlen( data );
3148 setup_test( &info, internet_writefile, __LINE__ );
3149 ret = InternetWriteFile( req, data, size, &written );
3150 ok( ret, "InternetWriteFile failed %u\n", GetLastError() );
3151 ok( written == size, "expected %u got %u\n", written, size );
3153 WaitForSingleObject( info.wait, 10000 );
3155 SetLastError( 0xdeadbeef );
3156 ret = HttpEndRequestA( req, (void *)data, 0x28, 0 );
3157 error = GetLastError();
3158 ok( !ret, "HttpEndRequestA succeeded\n" );
3159 ok( error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", error );
3161 SetLastError( 0xdeadbeef );
3162 setup_test( &info, http_end_request, __LINE__ );
3163 ret = HttpEndRequestA( req, NULL, 0x28, 0 );
3164 error = GetLastError();
3165 ok( !ret, "HttpEndRequestA succeeded\n" );
3166 ok( error == ERROR_IO_PENDING, "expected ERROR_IO_PENDING got %u\n", error );
3168 WaitForSingleObject( info.wait, 10000 );
3170 setup_test( &info, internet_close_handle, __LINE__ );
3171 InternetCloseHandle( req );
3172 InternetCloseHandle( con );
3173 InternetCloseHandle( ses );
3175 WaitForSingleObject( info.wait, 10000 );
3177 CloseHandle( info.wait );
3180 static HINTERNET closetest_session, closetest_req, closetest_conn;
3181 static BOOL closetest_closed;
3183 static void WINAPI closetest_callback(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus,
3184 LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
3189 trace("closetest_callback %p: %d\n", hInternet, dwInternetStatus);
3191 ok(hInternet == closetest_session || hInternet == closetest_conn || hInternet == closetest_req,
3192 "Unexpected hInternet %p\n", hInternet);
3193 if(!closetest_closed)
3197 res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_HANDLE_TYPE, &type, &len);
3198 ok(!res && GetLastError() == ERROR_INVALID_HANDLE,
3199 "InternetQueryOptionA(%p INTERNET_OPTION_HANDLE_TYPE) failed: %x %u, expected TRUE ERROR_INVALID_HANDLE\n",
3200 closetest_req, res, GetLastError());
3203 static void test_InternetCloseHandle(void)
3208 closetest_session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
3209 ok(closetest_session != NULL,"InternetOpen failed with error %u\n", GetLastError());
3211 pInternetSetStatusCallbackA(closetest_session, closetest_callback);
3213 closetest_conn = InternetConnectA(closetest_session, "source.winehq.org", INTERNET_INVALID_PORT_NUMBER,
3214 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
3215 ok(closetest_conn != NULL,"InternetConnect failed with error %u\n", GetLastError());
3217 closetest_req = HttpOpenRequestA(closetest_conn, "GET", "winegecko.php", NULL, NULL, NULL,
3218 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE, 0xdeadbead);
3220 res = HttpSendRequestA(closetest_req, NULL, -1, NULL, 0);
3221 ok(!res && (GetLastError() == ERROR_IO_PENDING),
3222 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3224 len = sizeof(flags);
3225 res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_REQUEST_FLAGS, &flags, &len);
3226 ok(res, "InternetQueryOptionA(%p INTERNET_OPTION_URL) failed: %u\n", closetest_req, GetLastError());
3228 res = InternetCloseHandle(closetest_session);
3229 ok(res, "InternetCloseHandle failed: %u\n", GetLastError());
3230 closetest_closed = TRUE;
3231 trace("Closed session handle\n");
3233 res = InternetCloseHandle(closetest_conn);
3234 ok(!res && GetLastError() == ERROR_INVALID_HANDLE, "InternetCloseConnection(conn) failed: %x %u\n",
3235 res, GetLastError());
3237 res = InternetCloseHandle(closetest_req);
3238 ok(!res && GetLastError() == ERROR_INVALID_HANDLE, "InternetCloseConnection(req) failed: %x %u\n",
3239 res, GetLastError());
3241 len = sizeof(flags);
3242 res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_REQUEST_FLAGS, &flags, &len);
3243 ok(!res && GetLastError() == ERROR_INVALID_HANDLE,
3244 "InternetQueryOptionA(%p INTERNET_OPTION_URL) failed: %x %u, expected TRUE ERROR_INVALID_HANDLE\n",
3245 closetest_req, res, GetLastError());
3248 static void init_status_tests(void)
3250 memset(expect, 0, sizeof(expect));
3251 memset(optional, 0, sizeof(optional));
3252 memset(wine_allow, 0, sizeof(wine_allow));
3253 memset(notified, 0, sizeof(notified));
3254 memset(status_string, 0, sizeof(status_string));
3256 #define STATUS_STRING(status) status_string[status] = #status
3257 STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
3258 STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
3259 STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
3260 STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
3261 STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
3262 STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
3263 STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
3264 STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
3265 STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
3266 STATUS_STRING(INTERNET_STATUS_PREFETCH);
3267 STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
3268 STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
3269 STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
3270 STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
3271 STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
3272 STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
3273 STATUS_STRING(INTERNET_STATUS_REDIRECT);
3274 STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
3275 STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
3276 STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
3277 STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
3278 STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
3279 STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
3280 STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
3281 STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
3282 STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
3283 #undef STATUS_STRING
3289 hdll = GetModuleHandleA("wininet.dll");
3291 if(!GetProcAddress(hdll, "InternetGetCookieExW")) {
3292 win_skip("Too old IE (older than 6.0)\n");
3296 pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
3298 init_status_tests();
3299 test_InternetCloseHandle();
3300 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[0]);
3301 InternetReadFile_test(0, &test_data[0]);
3302 first_connection_to_test_url = TRUE;
3303 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[1]);
3304 InternetReadFile_test(0, &test_data[1]);
3305 InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
3306 test_open_url_async();
3307 test_async_HttpSendRequestEx();
3308 InternetOpenRequest_test();
3310 InternetOpenUrlA_test();
3312 test_http_connection();
3313 test_secure_connection();
3314 test_user_agent_header();
3315 test_bogus_accept_types_array();
3316 InternetReadFile_chunked_test();
3317 HttpSendRequestEx_test();
3318 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[2]);