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://www.winehq.org/site/about"
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 /* SET_WINE_ALLOW's should be used with an appropriate
47 * todo_wine CHECK_NOTIFIED at a later point in the code */
48 #define SET_WINE_ALLOW2(status, num) \
49 wine_allow[status] = num
51 #define SET_WINE_ALLOW(status) \
52 SET_WINE_ALLOW2(status, 1)
54 #define CHECK_EXPECT(status) \
56 if (!expect[status] && wine_allow[status]) \
58 todo_wine ok(expect[status], "unexpected status %d (%s)\n", status, \
59 status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
60 status_string[status] : "unknown"); \
61 wine_allow[status]--; \
65 ok(expect[status], "unexpected status %d (%s)\n", status, \
66 status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
67 status_string[status] : "unknown"); \
73 /* CLEAR_NOTIFIED used in cases when notification behavior
74 * differs between Windows versions */
75 #define CLEAR_NOTIFIED(status) \
76 expect[status] = wine_allow[status] = notified[status] = 0;
78 #define CHECK_NOTIFIED2(status, num) \
80 ok(notified[status] == (num), "expected status %d (%s) %d times, received %d times\n", \
81 status, status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
82 status_string[status] : "unknown", (num), notified[status]); \
83 CLEAR_NOTIFIED(status); \
86 #define CHECK_NOTIFIED(status) \
87 CHECK_NOTIFIED2(status, 1)
89 #define CHECK_NOT_NOTIFIED(status) \
90 CHECK_NOTIFIED2(status, 0)
92 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
93 #define MAX_STATUS_NAME 50
94 static int expect[MAX_INTERNET_STATUS], wine_allow[MAX_INTERNET_STATUS],
95 notified[MAX_INTERNET_STATUS];
96 static CHAR status_string[MAX_INTERNET_STATUS][MAX_STATUS_NAME];
98 static HANDLE hCompleteEvent;
100 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
101 static BOOL (WINAPI *pInternetTimeFromSystemTimeA)(CONST SYSTEMTIME *,DWORD ,LPSTR ,DWORD);
102 static BOOL (WINAPI *pInternetTimeFromSystemTimeW)(CONST SYSTEMTIME *,DWORD ,LPWSTR ,DWORD);
103 static BOOL (WINAPI *pInternetTimeToSystemTimeA)(LPCSTR ,SYSTEMTIME *,DWORD);
104 static BOOL (WINAPI *pInternetTimeToSystemTimeW)(LPCWSTR ,SYSTEMTIME *,DWORD);
107 static VOID WINAPI callback(
110 DWORD dwInternetStatus,
111 LPVOID lpvStatusInformation,
112 DWORD dwStatusInformationLength
115 CHECK_EXPECT(dwInternetStatus);
116 switch (dwInternetStatus)
118 case INTERNET_STATUS_RESOLVING_NAME:
119 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
120 GetCurrentThreadId(), hInternet, dwContext,
121 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
122 *(LPSTR)lpvStatusInformation = '\0';
124 case INTERNET_STATUS_NAME_RESOLVED:
125 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
126 GetCurrentThreadId(), hInternet, dwContext,
127 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
128 *(LPSTR)lpvStatusInformation = '\0';
130 case INTERNET_STATUS_CONNECTING_TO_SERVER:
131 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
132 GetCurrentThreadId(), hInternet, dwContext,
133 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
134 *(LPSTR)lpvStatusInformation = '\0';
136 case INTERNET_STATUS_CONNECTED_TO_SERVER:
137 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
138 GetCurrentThreadId(), hInternet, dwContext,
139 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
140 *(LPSTR)lpvStatusInformation = '\0';
142 case INTERNET_STATUS_SENDING_REQUEST:
143 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
144 GetCurrentThreadId(), hInternet, dwContext,
145 lpvStatusInformation,dwStatusInformationLength);
147 case INTERNET_STATUS_REQUEST_SENT:
148 ok(dwStatusInformationLength == sizeof(DWORD),
149 "info length should be sizeof(DWORD) instead of %d\n",
150 dwStatusInformationLength);
151 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
152 GetCurrentThreadId(), hInternet, dwContext,
153 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
155 case INTERNET_STATUS_RECEIVING_RESPONSE:
156 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
157 GetCurrentThreadId(), hInternet, dwContext,
158 lpvStatusInformation,dwStatusInformationLength);
160 case INTERNET_STATUS_RESPONSE_RECEIVED:
161 ok(dwStatusInformationLength == sizeof(DWORD),
162 "info length should be sizeof(DWORD) instead of %d\n",
163 dwStatusInformationLength);
164 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
165 GetCurrentThreadId(), hInternet, dwContext,
166 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
168 case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
169 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
170 GetCurrentThreadId(), hInternet,dwContext,
171 lpvStatusInformation,dwStatusInformationLength);
173 case INTERNET_STATUS_PREFETCH:
174 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
175 GetCurrentThreadId(), hInternet, dwContext,
176 lpvStatusInformation,dwStatusInformationLength);
178 case INTERNET_STATUS_CLOSING_CONNECTION:
179 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
180 GetCurrentThreadId(), hInternet, dwContext,
181 lpvStatusInformation,dwStatusInformationLength);
183 case INTERNET_STATUS_CONNECTION_CLOSED:
184 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
185 GetCurrentThreadId(), hInternet, dwContext,
186 lpvStatusInformation,dwStatusInformationLength);
188 case INTERNET_STATUS_HANDLE_CREATED:
189 ok(dwStatusInformationLength == sizeof(HINTERNET),
190 "info length should be sizeof(HINTERNET) instead of %d\n",
191 dwStatusInformationLength);
192 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
193 GetCurrentThreadId(), hInternet, dwContext,
194 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
196 case INTERNET_STATUS_HANDLE_CLOSING:
197 ok(dwStatusInformationLength == sizeof(HINTERNET),
198 "info length should be sizeof(HINTERNET) instead of %d\n",
199 dwStatusInformationLength);
200 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
201 GetCurrentThreadId(), hInternet, dwContext,
202 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
204 case INTERNET_STATUS_REQUEST_COMPLETE:
206 INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
207 ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
208 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
209 dwStatusInformationLength);
210 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
211 GetCurrentThreadId(), hInternet, dwContext,
212 iar->dwResult,iar->dwError,dwStatusInformationLength);
213 SetEvent(hCompleteEvent);
216 case INTERNET_STATUS_REDIRECT:
217 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
218 GetCurrentThreadId(), hInternet, dwContext,
219 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
220 *(LPSTR)lpvStatusInformation = '\0';
222 case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
223 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
224 GetCurrentThreadId(), hInternet, dwContext,
225 lpvStatusInformation, dwStatusInformationLength);
228 trace("%04x:Callback %p 0x%lx %d %p %d\n",
229 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
230 lpvStatusInformation, dwStatusInformationLength);
234 static void InternetReadFile_test(int flags)
241 const char *types[2] = { "*", NULL };
242 HINTERNET hi, hic = 0, hor = 0;
244 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
246 trace("Starting InternetReadFile test with flags 0x%x\n",flags);
248 trace("InternetOpenA <--\n");
249 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
250 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
251 trace("InternetOpenA -->\n");
253 if (hi == 0x0) goto abort;
255 pInternetSetStatusCallbackA(hi,&callback);
257 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
259 trace("InternetConnectA <--\n");
260 hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
261 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
262 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
263 trace("InternetConnectA -->\n");
265 if (hic == 0x0) goto abort;
267 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
268 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
269 SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
270 SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
272 trace("HttpOpenRequestA <--\n");
273 hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
274 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
276 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
278 * If the internet name can't be resolved we are probably behind
279 * a firewall or in some other way not directly connected to the
280 * Internet. Not enough reason to fail the test. Just ignore and
284 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
286 trace("HttpOpenRequestA -->\n");
288 if (hor == 0x0) goto abort;
290 length = sizeof(buffer);
291 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
292 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
293 ok(!strcmp(buffer, "http://www.winehq.org/about/"), "Wrong URL %s\n", buffer);
295 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
298 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
299 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
301 if (first_connection_to_test_url)
303 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
304 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
308 SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
309 SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
311 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
312 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
313 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
314 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
315 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
316 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
317 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
318 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
319 SET_EXPECT(INTERNET_STATUS_REDIRECT);
320 if (flags & INTERNET_FLAG_ASYNC)
321 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
323 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
325 trace("HttpSendRequestA -->\n");
326 SetLastError(0xdeadbeef);
327 rc = HttpSendRequestA(hor, "", -1, NULL, 0);
328 if (flags & INTERNET_FLAG_ASYNC)
329 ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
330 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
332 ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
333 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
334 trace("HttpSendRequestA <--\n");
336 if (flags & INTERNET_FLAG_ASYNC)
337 WaitForSingleObject(hCompleteEvent, INFINITE);
339 if (first_connection_to_test_url)
341 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
342 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
346 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
347 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
349 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
350 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
351 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
352 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
353 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
354 if (flags & INTERNET_FLAG_ASYNC)
355 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
357 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
358 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
359 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
360 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
363 rc = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
364 trace("Option 0x17 -> %i %i\n",rc,out);
367 rc = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
368 trace("Option 0x22 -> %i %s\n",rc,buffer);
371 rc = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
373 trace("Option 0x16 -> %i %s\n",rc,buffer);
375 length = sizeof(buffer);
376 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
377 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
378 ok(!strcmp(buffer, "http://www.winehq.org/site/about"), "Wrong URL %s\n", buffer);
381 rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
382 trace("Option 0x5 -> %i %s (%u)\n",rc,buffer,GetLastError());
385 rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
387 trace("Option 0x1 -> %i %s\n",rc,buffer);
389 SetLastError(0xdeadbeef);
390 rc = InternetReadFile(NULL, buffer, 100, &length);
391 ok(!rc, "InternetReadFile should have failed\n");
392 ok(GetLastError() == ERROR_INVALID_HANDLE,
393 "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
397 trace("Entering Query loop\n");
399 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
400 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
403 if (flags & INTERNET_FLAG_ASYNC)
404 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
405 rc = InternetQueryDataAvailable(hor,&length,0x0,0x0);
406 ok(!(rc == 0 && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
407 ok(rc != 0 || ((flags & INTERNET_FLAG_ASYNC) && GetLastError() == ERROR_IO_PENDING),
408 "InternetQueryDataAvailable failed, error %d\n", GetLastError());
409 if (flags & INTERNET_FLAG_ASYNC)
413 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
415 else if (GetLastError() == ERROR_IO_PENDING)
417 WaitForSingleObject(hCompleteEvent, INFINITE);
418 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
425 buffer = HeapAlloc(GetProcessHeap(),0,length+1);
427 rc = InternetReadFile(hor,buffer,length,&length);
431 trace("ReadFile -> %i %i\n",rc,length);
433 HeapFree(GetProcessHeap(),0,buffer);
438 /* WinXP does not send, but Win98 does */
439 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
440 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
442 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
444 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
445 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
446 SetLastError(0xdeadbeef);
447 rc = InternetCloseHandle(hor);
448 ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
449 SetLastError(0xdeadbeef);
450 rc = InternetCloseHandle(hor);
451 ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
452 ok (GetLastError() == ERROR_INVALID_HANDLE,
453 "Double close of handle should have set ERROR_INVALID_HANDLE instead of %u\n",
456 /* We intentionally do not close the handle opened by InternetConnectA as this
457 * tickles bug #9479: native closes child internet handles when the parent handles
458 * are closed. This is verified below by checking that the number of
459 * INTERNET_STATUS_HANDLE_CLOSING notifications matches the number expected. */
461 SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
462 rc = InternetCloseHandle(hi);
463 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
464 if (flags & INTERNET_FLAG_ASYNC)
467 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
468 if (hor != 0x0) todo_wine
470 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
471 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
475 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
476 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
478 CloseHandle(hCompleteEvent);
479 first_connection_to_test_url = FALSE;
482 static void InternetReadFileExA_test(int flags)
486 const char *types[2] = { "*", NULL };
487 HINTERNET hi, hic = 0, hor = 0;
488 INTERNET_BUFFERS inetbuffers;
490 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
492 trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
494 trace("InternetOpenA <--\n");
495 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
496 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
497 trace("InternetOpenA -->\n");
499 if (hi == 0x0) goto abort;
501 pInternetSetStatusCallbackA(hi,&callback);
503 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
505 trace("InternetConnectA <--\n");
506 hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
507 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
508 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
509 trace("InternetConnectA -->\n");
511 if (hic == 0x0) goto abort;
513 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
514 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
515 SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
516 SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
518 trace("HttpOpenRequestA <--\n");
519 hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
520 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
522 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
524 * If the internet name can't be resolved we are probably behind
525 * a firewall or in some other way not directly connected to the
526 * Internet. Not enough reason to fail the test. Just ignore and
530 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
532 trace("HttpOpenRequestA -->\n");
534 if (hor == 0x0) goto abort;
536 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
539 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
540 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
542 if (first_connection_to_test_url)
544 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
545 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
549 SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
550 SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
552 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
553 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
554 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
555 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
556 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
557 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
558 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
559 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
560 SET_EXPECT(INTERNET_STATUS_REDIRECT);
561 if (flags & INTERNET_FLAG_ASYNC)
562 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
564 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
566 trace("HttpSendRequestA -->\n");
567 SetLastError(0xdeadbeef);
568 rc = HttpSendRequestA(hor, "", -1, NULL, 0);
569 if (flags & INTERNET_FLAG_ASYNC)
570 ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
571 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
573 ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
574 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
575 trace("HttpSendRequestA <--\n");
577 if (!rc && (GetLastError() == ERROR_IO_PENDING))
578 WaitForSingleObject(hCompleteEvent, INFINITE);
580 if (first_connection_to_test_url)
582 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
583 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
587 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
588 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
590 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
591 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
592 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
593 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
594 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
595 if (flags & INTERNET_FLAG_ASYNC)
596 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
598 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
599 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
600 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
601 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
603 /* tests invalid dwStructSize */
604 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
605 inetbuffers.lpcszHeader = NULL;
606 inetbuffers.dwHeadersLength = 0;
607 inetbuffers.dwBufferLength = 10;
608 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
609 inetbuffers.dwOffsetHigh = 1234;
610 inetbuffers.dwOffsetLow = 5678;
611 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
612 ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
613 "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
614 rc ? "TRUE" : "FALSE", GetLastError());
615 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
617 /* tests to see whether lpcszHeader is used - it isn't */
618 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
619 inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
620 inetbuffers.dwHeadersLength = 255;
621 inetbuffers.dwBufferLength = 0;
622 inetbuffers.lpvBuffer = NULL;
623 inetbuffers.dwOffsetHigh = 1234;
624 inetbuffers.dwOffsetLow = 5678;
625 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
626 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
627 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
628 ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
631 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
632 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
635 rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
636 ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
637 "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
638 rc ? "TRUE" : "FALSE", GetLastError());
641 trace("Entering Query loop\n");
643 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
644 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
647 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
648 inetbuffers.dwBufferLength = 1024;
649 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
650 inetbuffers.dwOffsetHigh = 1234;
651 inetbuffers.dwOffsetLow = 5678;
653 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
654 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
655 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
656 rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
659 if (GetLastError() == ERROR_IO_PENDING)
661 trace("InternetReadFileEx -> PENDING\n");
662 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
663 WaitForSingleObject(hCompleteEvent, INFINITE);
664 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
665 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
669 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
675 trace("InternetReadFileEx -> SUCCEEDED\n");
676 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
677 if (inetbuffers.dwBufferLength)
679 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
680 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
684 /* Win98 still sends these when 0 bytes are read, WinXP does not */
685 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
686 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
690 trace("read %i bytes\n", inetbuffers.dwBufferLength);
691 ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
693 ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
694 "InternetReadFileEx sets offsets to 0x%x%08x\n",
695 inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
697 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
699 if (!inetbuffers.dwBufferLength)
702 length += inetbuffers.dwBufferLength;
704 ok(length > 0, "failed to read any of the document\n");
705 trace("Finished. Read %d bytes\n", length);
707 /* WinXP does not send, but Win98 does */
708 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
709 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
711 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
713 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
714 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
715 rc = InternetCloseHandle(hor);
716 ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
717 rc = InternetCloseHandle(hor);
718 ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
721 rc = InternetCloseHandle(hic);
722 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
725 SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
726 rc = InternetCloseHandle(hi);
727 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
728 if (flags & INTERNET_FLAG_ASYNC)
730 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
732 if (hor != 0x0) todo_wine
734 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
735 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
739 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
740 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
742 CloseHandle(hCompleteEvent);
743 first_connection_to_test_url = FALSE;
746 static void InternetOpenUrlA_test(void)
748 HINTERNET myhinternet, myhttp;
750 DWORD size, readbytes, totalbytes=0;
753 myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
754 ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
756 ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
757 ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
760 myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
761 INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
762 if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
763 return; /* WinXP returns this when not connected to the net */
764 ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
765 ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
766 ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
767 totalbytes += readbytes;
768 while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
769 totalbytes += readbytes;
770 trace("read 0x%08x bytes\n",totalbytes);
772 InternetCloseHandle(myhttp);
773 InternetCloseHandle(myhinternet);
776 static void InternetTimeFromSystemTimeA_test(void)
779 static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
780 char string[INTERNET_RFC1123_BUFSIZE];
781 static const char expect[] = "Fri, 07 Jan 2005 12:06:35 GMT";
783 ret = pInternetTimeFromSystemTimeA( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
784 ok( ret, "InternetTimeFromSystemTimeA failed (%u)\n", GetLastError() );
786 ok( !memcmp( string, expect, sizeof(expect) ),
787 "InternetTimeFromSystemTimeA failed (%u)\n", GetLastError() );
790 static void InternetTimeFromSystemTimeW_test(void)
793 static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
794 WCHAR string[INTERNET_RFC1123_BUFSIZE + 1];
795 static const WCHAR expect[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
796 '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
798 ret = pInternetTimeFromSystemTimeW( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
799 ok( ret, "InternetTimeFromSystemTimeW failed (%u)\n", GetLastError() );
801 ok( !memcmp( string, expect, sizeof(expect) ),
802 "InternetTimeFromSystemTimeW failed (%u)\n", GetLastError() );
805 static void InternetTimeToSystemTimeA_test(void)
809 static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
810 static const char string[] = "Fri, 07 Jan 2005 12:06:35 GMT";
811 static const char string2[] = " fri 7 jan 2005 12 06 35";
813 ret = pInternetTimeToSystemTimeA( string, &time, 0 );
814 ok( ret, "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
815 ok( !memcmp( &time, &expect, sizeof(expect) ),
816 "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
818 ret = pInternetTimeToSystemTimeA( string2, &time, 0 );
819 ok( ret, "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
820 ok( !memcmp( &time, &expect, sizeof(expect) ),
821 "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
824 static void InternetTimeToSystemTimeW_test(void)
828 static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
829 static const WCHAR string[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
830 '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
831 static const WCHAR string2[] = { ' ','f','r','i',' ','7',' ','j','a','n',' ','2','0','0','5',' ',
832 '1','2',' ','0','6',' ','3','5',0 };
833 static const WCHAR string3[] = { 'F','r',0 };
835 ret = pInternetTimeToSystemTimeW( NULL, NULL, 0 );
836 ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
838 ret = pInternetTimeToSystemTimeW( NULL, &time, 0 );
839 ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
841 ret = pInternetTimeToSystemTimeW( string, NULL, 0 );
842 ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
844 ret = pInternetTimeToSystemTimeW( string, &time, 0 );
845 ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
847 ret = pInternetTimeToSystemTimeW( string, &time, 0 );
848 ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
849 ok( !memcmp( &time, &expect, sizeof(expect) ),
850 "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
852 ret = pInternetTimeToSystemTimeW( string2, &time, 0 );
853 ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
854 ok( !memcmp( &time, &expect, sizeof(expect) ),
855 "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
857 ret = pInternetTimeToSystemTimeW( string3, &time, 0 );
858 ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
861 static void HttpSendRequestEx_test(void)
867 INTERNET_BUFFERS BufferIn;
868 DWORD dwBytesWritten;
874 static char szPostData[] = "mode=Test";
875 static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
877 hSession = InternetOpen("Wine Regression Test",
878 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
879 ok( hSession != NULL ,"Unable to open Internet session\n");
880 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
881 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
883 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
884 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
885 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
886 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
888 trace( "Network unreachable, skipping test\n" );
891 ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
894 BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
895 BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
896 BufferIn.lpcszHeader = szContentType;
897 BufferIn.dwHeadersLength = sizeof(szContentType)-1;
898 BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
899 BufferIn.lpvBuffer = szPostData;
900 BufferIn.dwBufferLength = 3;
901 BufferIn.dwBufferTotal = sizeof(szPostData)-1;
902 BufferIn.dwOffsetLow = 0;
903 BufferIn.dwOffsetHigh = 0;
905 ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
906 ok(ret, "HttpSendRequestEx Failed with error %u\n", GetLastError());
908 for (i = 3; szPostData[i]; i++)
909 ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
910 "InternetWriteFile failed\n");
912 ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
914 ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
915 "Unable to read response\n");
916 szBuffer[dwBytesRead] = 0;
918 ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
919 ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0,"Got string %s\n",szBuffer);
921 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
923 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
924 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
927 static void InternetOpenRequest_test(void)
929 HINTERNET session, connect, request;
930 static const char *types[] = { "*", "", NULL };
931 static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
932 static const WCHAR *typesW[] = { any, empty, NULL };
935 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
936 ok(session != NULL ,"Unable to open Internet session\n");
938 connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
939 INTERNET_SERVICE_HTTP, 0, 0);
940 ok(connect == NULL, "InternetConnectA should have failed\n");
941 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
943 connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
944 INTERNET_SERVICE_HTTP, 0, 0);
945 ok(connect == NULL, "InternetConnectA should have failed\n");
946 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
948 connect = InternetConnectA(session, "winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
949 INTERNET_SERVICE_HTTP, 0, 0);
950 ok(connect != NULL, "Unable to connect to http://winehq.org with error %d\n", GetLastError());
952 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
953 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
955 trace( "Network unreachable, skipping test\n" );
958 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
960 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
961 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
962 ok(InternetCloseHandle(request), "Close request handle failed\n");
964 request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
965 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
967 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
968 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
969 ok(InternetCloseHandle(request), "Close request handle failed\n");
972 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
973 ok(InternetCloseHandle(session), "Close session handle failed\n");
976 static void test_http_cache(void)
978 HINTERNET session, connect, request;
979 char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
980 DWORD size, file_size;
985 static const char *types[] = { "*", "", NULL };
987 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
988 ok(session != NULL ,"Unable to open Internet session\n");
990 connect = InternetConnectA(session, "www.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
991 INTERNET_SERVICE_HTTP, 0, 0);
992 ok(connect != NULL, "Unable to connect to http://winehq.org with error %d\n", GetLastError());
994 request = HttpOpenRequestA(connect, NULL, "/site/about", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
995 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
997 trace( "Network unreachable, skipping test\n" );
999 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1000 ok(InternetCloseHandle(session), "Close session handle failed\n");
1004 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1007 ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
1008 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_url) failed: %u\n", GetLastError());
1009 ok(!strcmp(url, "http://www.winehq.org/site/about"), "Wrong URL %s\n", url);
1011 size = sizeof(file_name);
1012 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1013 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1014 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1015 ok(!size, "size = %d\n", size);
1017 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1018 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1020 size = sizeof(file_name);
1021 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1022 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1024 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1025 FILE_ATTRIBUTE_NORMAL, NULL);
1026 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1027 file_size = GetFileSize(file, NULL);
1028 ok(file_size == 0, "file size=%d\n", file_size);
1030 ret = InternetReadFile(request, buf, sizeof(buf), &size);
1031 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1032 ok(size == sizeof(buf), "size=%d\n", size);
1034 file_size = GetFileSize(file, NULL);
1035 ok(file_size == sizeof(buf), "file size=%d\n", file_size);
1038 ok(InternetCloseHandle(request), "Close request handle failed\n");
1040 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1041 FILE_ATTRIBUTE_NORMAL, NULL);
1042 ok(file == INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
1043 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError()=%u, expected ERROR_FILE_NOT_FOUND\n", GetLastError());
1045 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1046 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1048 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1049 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1050 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1051 ok(!size, "size = %d\n", size);
1053 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1054 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1056 size = sizeof(file_name);
1057 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1058 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1059 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1060 ok(!size, "size = %d\n", size);
1062 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1063 FILE_ATTRIBUTE_NORMAL, NULL);
1064 ok(file == INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
1065 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError()=%u, expected ERROR_FILE_NOT_FOUND\n", GetLastError());
1067 ok(InternetCloseHandle(request), "Close request handle failed\n");
1069 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1070 ok(InternetCloseHandle(session), "Close session handle failed\n");
1073 static void HttpHeaders_test(void)
1082 hSession = InternetOpen("Wine Regression Test",
1083 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1084 ok( hSession != NULL ,"Unable to open Internet session\n");
1085 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1086 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1088 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1089 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1090 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1091 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1093 trace( "Network unreachable, skipping test\n" );
1096 ok( hRequest != NULL, "Failed to open request handle\n");
1099 len = sizeof(buffer);
1100 strcpy(buffer,"Warning");
1101 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1102 buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1104 ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1105 "Failed to add new header\n");
1108 len = sizeof(buffer);
1109 strcpy(buffer,"Warning");
1110 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1111 buffer,&len,&index),"Unable to query header\n");
1112 ok(index == 1, "Index was not incremented\n");
1113 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1114 ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1115 ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1116 len = sizeof(buffer);
1117 strcpy(buffer,"Warning");
1118 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1119 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1122 len = 5; /* could store the string but not the NULL terminator */
1123 strcpy(buffer,"Warning");
1124 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1125 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1126 ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1127 ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1129 /* a call with NULL will fail but will return the length */
1131 len = sizeof(buffer);
1132 SetLastError(0xdeadbeef);
1133 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1134 NULL,&len,&index) == FALSE,"Query worked\n");
1135 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1136 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1137 ok(index == 0, "Index was incremented\n");
1139 /* even for a len that is too small */
1142 SetLastError(0xdeadbeef);
1143 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1144 NULL,&len,&index) == FALSE,"Query worked\n");
1145 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1146 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1147 ok(index == 0, "Index was incremented\n");
1151 SetLastError(0xdeadbeef);
1152 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1153 NULL,&len,&index) == FALSE,"Query worked\n");
1154 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1155 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1156 ok(index == 0, "Index was incremented\n");
1159 /* a working query */
1161 len = sizeof(buffer);
1162 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1163 buffer,&len,&index),"Unable to query header\n");
1164 /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1165 ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1166 ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1167 ok(index == 0, "Index was incremented\n");
1171 ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1172 "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1175 len = sizeof(buffer);
1176 strcpy(buffer,"Warning");
1177 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1178 buffer,&len,&index),"Unable to query header\n");
1179 ok(index == 1, "Index was not incremented\n");
1180 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1181 len = sizeof(buffer);
1182 strcpy(buffer,"Warning");
1183 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1184 buffer,&len,&index),"Failed to get second header\n");
1185 ok(index == 2, "Index was not incremented\n");
1186 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1187 len = sizeof(buffer);
1188 strcpy(buffer,"Warning");
1189 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1190 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1192 ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1195 len = sizeof(buffer);
1196 strcpy(buffer,"Warning");
1197 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1198 buffer,&len,&index),"Unable to query header\n");
1199 ok(index == 1, "Index was not incremented\n");
1200 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1201 len = sizeof(buffer);
1202 strcpy(buffer,"Warning");
1203 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1204 buffer,&len,&index),"Failed to get second header\n");
1205 ok(index == 2, "Index was not incremented\n");
1206 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1207 len = sizeof(buffer);
1208 strcpy(buffer,"Warning");
1209 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1210 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1212 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1215 len = sizeof(buffer);
1216 strcpy(buffer,"Warning");
1217 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1218 buffer,&len,&index),"Unable to query header\n");
1219 ok(index == 1, "Index was not incremented\n");
1220 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1221 len = sizeof(buffer);
1222 strcpy(buffer,"Warning");
1223 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1224 buffer,&len,&index),"Failed to get second header\n");
1225 ok(index == 2, "Index was not incremented\n");
1226 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1227 len = sizeof(buffer);
1228 strcpy(buffer,"Warning");
1229 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1230 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1232 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1235 len = sizeof(buffer);
1236 strcpy(buffer,"Warning");
1237 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1238 buffer,&len,&index),"Unable to query header\n");
1239 ok(index == 1, "Index was not incremented\n");
1240 ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1241 len = sizeof(buffer);
1242 strcpy(buffer,"Warning");
1243 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1244 ok(index == 2, "Index was not incremented\n");
1245 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1246 len = sizeof(buffer);
1247 strcpy(buffer,"Warning");
1248 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1250 ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1253 len = sizeof(buffer);
1254 strcpy(buffer,"Warning");
1255 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1256 ok(index == 1, "Index was not incremented\n");
1257 ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1258 len = sizeof(buffer);
1259 strcpy(buffer,"Warning");
1260 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1261 ok(index == 2, "Index was not incremented\n");
1262 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1263 len = sizeof(buffer);
1264 strcpy(buffer,"Warning");
1265 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1267 ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1270 len = sizeof(buffer);
1271 strcpy(buffer,"Warning");
1272 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1273 ok(index == 1, "Index was not incremented\n");
1274 ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1275 len = sizeof(buffer);
1276 strcpy(buffer,"Warning");
1277 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1278 ok(index == 2, "Index was not incremented\n");
1279 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1280 len = sizeof(buffer);
1281 strcpy(buffer,"Warning");
1282 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1284 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");
1287 len = sizeof(buffer);
1288 strcpy(buffer,"Warning");
1289 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1290 ok(index == 1, "Index was not incremented\n");
1291 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1292 len = sizeof(buffer);
1293 strcpy(buffer,"Warning");
1294 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1295 ok(index == 2, "Index was not incremented\n");
1296 ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1297 len = sizeof(buffer);
1298 strcpy(buffer,"Warning");
1299 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1302 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1304 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1305 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1308 static const char contmsg[] =
1309 "HTTP/1.1 100 Continue\r\n";
1311 static const char okmsg[] =
1312 "HTTP/1.1 200 OK\r\n"
1313 "Server: winetest\r\n"
1316 static const char notokmsg[] =
1317 "HTTP/1.1 400 Bad Request\r\n"
1318 "Server: winetest\r\n"
1321 static const char noauthmsg[] =
1322 "HTTP/1.1 401 Unauthorized\r\n"
1323 "Server: winetest\r\n"
1326 static const char proxymsg[] =
1327 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1328 "Server: winetest\r\n"
1329 "Proxy-Connection: close\r\n"
1330 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1333 static const char page1[] =
1335 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1336 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1339 struct server_info {
1344 static DWORD CALLBACK server_thread(LPVOID param)
1346 struct server_info *si = param;
1349 struct sockaddr_in sa;
1352 int last_request = 0;
1354 WSAStartup(MAKEWORD(1,1), &wsaData);
1356 s = socket(AF_INET, SOCK_STREAM, 0);
1357 if (s == INVALID_SOCKET)
1361 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1363 memset(&sa, 0, sizeof sa);
1364 sa.sin_family = AF_INET;
1365 sa.sin_port = htons(si->port);
1366 sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1368 r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1374 SetEvent(si->hEvent);
1378 c = accept(s, NULL, NULL);
1380 memset(buffer, 0, sizeof buffer);
1381 for(i=0; i<(sizeof buffer-1); i++)
1383 r = recv(c, &buffer[i], 1, 0);
1387 if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
1388 buffer[i-3] == '\r' && buffer[i-1] == '\r')
1392 if (strstr(buffer, "GET /test1"))
1394 if (!strstr(buffer, "Content-Length: 0"))
1396 send(c, okmsg, sizeof okmsg-1, 0);
1397 send(c, page1, sizeof page1-1, 0);
1400 send(c, notokmsg, sizeof notokmsg-1, 0);
1403 if (strstr(buffer, "/test2"))
1405 if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
1407 send(c, okmsg, sizeof okmsg-1, 0);
1408 send(c, page1, sizeof page1-1, 0);
1411 send(c, proxymsg, sizeof proxymsg-1, 0);
1414 if (strstr(buffer, "/test3"))
1416 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
1417 send(c, okmsg, sizeof okmsg-1, 0);
1419 send(c, noauthmsg, sizeof noauthmsg-1, 0);
1422 if (strstr(buffer, "/test4"))
1424 if (strstr(buffer, "Connection: Close"))
1425 send(c, okmsg, sizeof okmsg-1, 0);
1427 send(c, notokmsg, sizeof notokmsg-1, 0);
1430 if (strstr(buffer, "POST /test5"))
1432 if (strstr(buffer, "Content-Length: 0"))
1434 send(c, okmsg, sizeof okmsg-1, 0);
1435 send(c, page1, sizeof page1-1, 0);
1438 send(c, notokmsg, sizeof notokmsg-1, 0);
1441 if (strstr(buffer, "GET /test6"))
1443 send(c, contmsg, sizeof contmsg-1, 0);
1444 send(c, contmsg, sizeof contmsg-1, 0);
1445 send(c, okmsg, sizeof okmsg-1, 0);
1446 send(c, page1, sizeof page1-1, 0);
1449 if (strstr(buffer, "GET /quit"))
1451 send(c, okmsg, sizeof okmsg-1, 0);
1452 send(c, page1, sizeof page1-1, 0);
1458 } while (!last_request);
1465 static void test_basic_request(int port, const char *verb, const char *url)
1467 HINTERNET hi, hc, hr;
1471 hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1472 ok(hi != NULL, "open failed\n");
1474 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1475 ok(hc != NULL, "connect failed\n");
1477 hr = HttpOpenRequest(hc, verb, url, NULL, NULL, NULL, 0, 0);
1478 ok(hr != NULL, "HttpOpenRequest failed\n");
1480 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1481 ok(r, "HttpSendRequest failed\n");
1484 memset(buffer, 0, sizeof buffer);
1485 r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1486 ok(r, "InternetReadFile failed\n");
1487 ok(count == sizeof page1 - 1, "count was wrong\n");
1488 ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
1490 InternetCloseHandle(hr);
1491 InternetCloseHandle(hc);
1492 InternetCloseHandle(hi);
1495 static void test_proxy_indirect(int port)
1497 HINTERNET hi, hc, hr;
1501 hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1502 ok(hi != NULL, "open failed\n");
1504 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1505 ok(hc != NULL, "connect failed\n");
1507 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1508 ok(hr != NULL, "HttpOpenRequest failed\n");
1510 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1511 ok(r, "HttpSendRequest failed\n");
1514 r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1515 ok(r, "HttpQueryInfo failed\n");
1516 ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1519 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1520 ok(r, "HttpQueryInfo failed\n");
1521 ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1524 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1525 ok(r, "HttpQueryInfo failed\n");
1526 ok(val == 407, "proxy code wrong\n");
1529 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1530 ok(r, "HttpQueryInfo failed\n");
1531 ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1534 r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1535 ok(r, "HttpQueryInfo failed\n");
1536 ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1539 r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1540 ok(r, "HttpQueryInfo failed\n");
1541 ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1544 r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1545 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1546 ok(r == FALSE, "HttpQueryInfo failed\n");
1548 InternetCloseHandle(hr);
1549 InternetCloseHandle(hc);
1550 InternetCloseHandle(hi);
1553 static void test_proxy_direct(int port)
1555 HINTERNET hi, hc, hr;
1558 static CHAR username[] = "mike",
1559 password[] = "1101";
1561 sprintf(buffer, "localhost:%d\n", port);
1562 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1563 ok(hi != NULL, "open failed\n");
1565 /* try connect without authorization */
1566 hc = InternetConnect(hi, "www.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1567 ok(hc != NULL, "connect failed\n");
1569 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1570 ok(hr != NULL, "HttpOpenRequest failed\n");
1572 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1573 ok(r, "HttpSendRequest failed\n");
1576 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1577 ok(r, "HttpQueryInfo failed\n");
1578 ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1581 /* set the user + password then try again */
1583 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
1584 ok(r, "failed to set user\n");
1586 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
1587 ok(r, "failed to set password\n");
1590 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1591 ok(r, "HttpSendRequest failed\n");
1593 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1594 ok(r, "HttpQueryInfo failed\n");
1596 ok(!strcmp(buffer, "200"), "proxy code wrong\n");
1600 InternetCloseHandle(hr);
1601 InternetCloseHandle(hc);
1602 InternetCloseHandle(hi);
1605 static void test_header_handling_order(int port)
1607 static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
1608 static char connection[] = "Connection: Close";
1610 static const char *types[2] = { "*", NULL };
1611 HINTERNET session, connect, request;
1615 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1616 ok(session != NULL, "InternetOpen failed\n");
1618 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1619 ok(connect != NULL, "InternetConnect failed\n");
1621 request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1622 ok(request != NULL, "HttpOpenRequest failed\n");
1624 ret = HttpAddRequestHeaders(request, authorization, ~0UL, HTTP_ADDREQ_FLAG_ADD);
1625 ok(ret, "HttpAddRequestHeaders failed\n");
1627 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1628 ok(ret, "HttpSendRequest failed\n");
1631 size = sizeof(status);
1632 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1633 ok(ret, "HttpQueryInfo failed\n");
1634 ok(status == 200, "request failed with status %u\n", status);
1636 InternetCloseHandle(request);
1638 request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1639 ok(request != NULL, "HttpOpenRequest failed\n");
1641 ret = HttpSendRequest(request, connection, ~0UL, NULL, 0);
1642 ok(ret, "HttpSendRequest failed\n");
1645 size = sizeof(status);
1646 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1647 ok(ret, "HttpQueryInfo failed\n");
1648 ok(status == 200, "request failed with status %u\n", status);
1650 InternetCloseHandle(request);
1651 InternetCloseHandle(connect);
1652 InternetCloseHandle(session);
1655 static void test_http_connection(void)
1657 struct server_info si;
1661 si.hEvent = CreateEvent(NULL, 0, 0, NULL);
1664 hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
1665 ok( hThread != NULL, "create thread failed\n");
1667 r = WaitForSingleObject(si.hEvent, 10000);
1668 ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
1669 if (r != WAIT_OBJECT_0)
1672 test_basic_request(si.port, "GET", "/test1");
1673 test_proxy_indirect(si.port);
1674 test_proxy_direct(si.port);
1675 test_header_handling_order(si.port);
1676 test_basic_request(si.port, "POST", "/test5");
1677 test_basic_request(si.port, "GET", "/test6");
1679 /* send the basic request again to shutdown the server thread */
1680 test_basic_request(si.port, "GET", "/quit");
1682 r = WaitForSingleObject(hThread, 3000);
1683 ok( r == WAIT_OBJECT_0, "thread wait failed\n");
1684 CloseHandle(hThread);
1687 #define STATUS_STRING(status) \
1688 memcpy(status_string[status], #status, sizeof(CHAR) * \
1689 (strlen(#status) < MAX_STATUS_NAME ? \
1691 MAX_STATUS_NAME - 1))
1692 static void init_status_tests(void)
1694 memset(expect, 0, sizeof(expect));
1695 memset(wine_allow, 0, sizeof(wine_allow));
1696 memset(notified, 0, sizeof(notified));
1697 memset(status_string, 0, sizeof(status_string));
1698 STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
1699 STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
1700 STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
1701 STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
1702 STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
1703 STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
1704 STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
1705 STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
1706 STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
1707 STATUS_STRING(INTERNET_STATUS_PREFETCH);
1708 STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
1709 STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
1710 STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
1711 STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
1712 STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
1713 STATUS_STRING(INTERNET_STATUS_REDIRECT);
1714 STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
1715 STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
1716 STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
1717 STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
1718 STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
1719 STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
1720 STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
1721 STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
1722 STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
1724 #undef STATUS_STRING
1729 hdll = GetModuleHandleA("wininet.dll");
1730 pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
1731 pInternetTimeFromSystemTimeA = (void*)GetProcAddress(hdll, "InternetTimeFromSystemTimeA");
1732 pInternetTimeFromSystemTimeW = (void*)GetProcAddress(hdll, "InternetTimeFromSystemTimeW");
1733 pInternetTimeToSystemTimeA = (void*)GetProcAddress(hdll, "InternetTimeToSystemTimeA");
1734 pInternetTimeToSystemTimeW = (void*)GetProcAddress(hdll, "InternetTimeToSystemTimeW");
1736 if (!pInternetSetStatusCallbackA)
1737 skip("skipping the InternetReadFile tests\n");
1740 init_status_tests();
1741 InternetReadFile_test(INTERNET_FLAG_ASYNC);
1742 InternetReadFile_test(0);
1743 InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
1745 InternetOpenRequest_test();
1747 InternetOpenUrlA_test();
1748 if (!pInternetTimeFromSystemTimeA)
1749 skip("skipping the InternetTime tests\n");
1752 InternetTimeFromSystemTimeA_test();
1753 InternetTimeFromSystemTimeW_test();
1754 InternetTimeToSystemTimeA_test();
1755 InternetTimeToSystemTimeW_test();
1757 HttpSendRequestEx_test();
1759 test_http_connection();