push 5b66f3b36e7323272378316c923222b908f6e2d3
[wine/hacks.git] / dlls / wininet / tests / http.c
blob0c4483b0c47bef1669c23d53ef064babe4e164c4
1 /*
2 * Wininet - Http tests
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
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wininet.h"
30 #include "winsock.h"
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) \
41 expect[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) \
55 do { \
56 if (!expect[status] && wine_allow[status]) \
57 { \
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]--; \
62 } \
63 else \
64 { \
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"); \
68 expect[status]--; \
69 } \
70 notified[status]++; \
71 }while(0)
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) \
79 do { \
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); \
84 }while(0)
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(
108 HINTERNET hInternet,
109 DWORD_PTR dwContext,
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';
123 break;
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';
129 break;
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';
135 break;
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';
141 break;
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);
146 break;
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);
154 break;
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);
159 break;
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);
167 break;
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);
172 break;
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);
177 break;
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);
182 break;
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);
187 break;
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);
195 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
196 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
197 break;
198 case INTERNET_STATUS_HANDLE_CLOSING:
199 ok(dwStatusInformationLength == sizeof(HINTERNET),
200 "info length should be sizeof(HINTERNET) instead of %d\n",
201 dwStatusInformationLength);
202 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
203 GetCurrentThreadId(), hInternet, dwContext,
204 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
205 break;
206 case INTERNET_STATUS_REQUEST_COMPLETE:
208 INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
209 ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
210 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
211 dwStatusInformationLength);
212 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
213 GetCurrentThreadId(), hInternet, dwContext,
214 iar->dwResult,iar->dwError,dwStatusInformationLength);
215 SetEvent(hCompleteEvent);
216 break;
218 case INTERNET_STATUS_REDIRECT:
219 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
220 GetCurrentThreadId(), hInternet, dwContext,
221 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
222 *(LPSTR)lpvStatusInformation = '\0';
223 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
224 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
225 break;
226 case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
227 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
228 GetCurrentThreadId(), hInternet, dwContext,
229 lpvStatusInformation, dwStatusInformationLength);
230 break;
231 default:
232 trace("%04x:Callback %p 0x%lx %d %p %d\n",
233 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
234 lpvStatusInformation, dwStatusInformationLength);
238 static void InternetReadFile_test(int flags)
240 BOOL res;
241 DWORD rc;
242 CHAR buffer[4000];
243 DWORD length;
244 DWORD out;
245 const char *types[2] = { "*", NULL };
246 HINTERNET hi, hic = 0, hor = 0;
248 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
250 trace("Starting InternetReadFile test with flags 0x%x\n",flags);
252 trace("InternetOpenA <--\n");
253 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
254 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
255 trace("InternetOpenA -->\n");
257 if (hi == 0x0) goto abort;
259 pInternetSetStatusCallbackA(hi,&callback);
261 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
263 trace("InternetConnectA <--\n");
264 hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
265 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
266 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
267 trace("InternetConnectA -->\n");
269 if (hic == 0x0) goto abort;
271 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
272 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
274 trace("HttpOpenRequestA <--\n");
275 hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
276 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
277 0xdeadbead);
278 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
280 * If the internet name can't be resolved we are probably behind
281 * a firewall or in some other way not directly connected to the
282 * Internet. Not enough reason to fail the test. Just ignore and
283 * abort.
285 } else {
286 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
288 trace("HttpOpenRequestA -->\n");
290 if (hor == 0x0) goto abort;
292 length = sizeof(buffer);
293 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
294 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
295 ok(!strcmp(buffer, "http://www.winehq.org/about/"), "Wrong URL %s\n", buffer);
297 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
298 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
299 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
300 if (first_connection_to_test_url)
302 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
303 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
305 else
307 SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
308 SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
310 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
311 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
312 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
313 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
314 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
315 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
316 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
317 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
318 SET_EXPECT(INTERNET_STATUS_REDIRECT);
319 if (flags & INTERNET_FLAG_ASYNC)
320 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
321 else
322 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
324 trace("HttpSendRequestA -->\n");
325 SetLastError(0xdeadbeef);
326 rc = HttpSendRequestA(hor, "", -1, NULL, 0);
327 if (flags & INTERNET_FLAG_ASYNC)
328 ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
329 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
330 else
331 ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
332 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
333 trace("HttpSendRequestA <--\n");
335 if (flags & INTERNET_FLAG_ASYNC)
336 WaitForSingleObject(hCompleteEvent, INFINITE);
338 if (first_connection_to_test_url)
340 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
341 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
343 else todo_wine
345 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
346 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
348 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
349 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
350 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
351 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
352 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
353 if (flags & INTERNET_FLAG_ASYNC)
354 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
355 else
356 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
357 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
358 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
359 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
361 length = 4;
362 rc = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
363 trace("Option 0x17 -> %i %i\n",rc,out);
365 length = 100;
366 rc = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
367 trace("Option 0x22 -> %i %s\n",rc,buffer);
369 length = 4000;
370 rc = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
371 buffer[length]=0;
372 trace("Option 0x16 -> %i %s\n",rc,buffer);
374 length = sizeof(buffer);
375 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
376 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
377 ok(!strcmp(buffer, "http://www.winehq.org/site/about"), "Wrong URL %s\n", buffer);
379 length = 16;
380 rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
381 trace("Option 0x5 -> %i %s (%u)\n",rc,buffer,GetLastError());
383 length = 100;
384 rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
385 buffer[length]=0;
386 trace("Option 0x1 -> %i %s\n",rc,buffer);
388 SetLastError(0xdeadbeef);
389 rc = InternetReadFile(NULL, buffer, 100, &length);
390 ok(!rc, "InternetReadFile should have failed\n");
391 ok(GetLastError() == ERROR_INVALID_HANDLE,
392 "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
393 GetLastError());
395 length = 100;
396 trace("Entering Query loop\n");
398 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
399 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
400 while (TRUE)
402 if (flags & INTERNET_FLAG_ASYNC)
403 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
404 rc = InternetQueryDataAvailable(hor,&length,0x0,0x0);
405 ok(!(rc == 0 && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
406 ok(rc != 0 || ((flags & INTERNET_FLAG_ASYNC) && GetLastError() == ERROR_IO_PENDING),
407 "InternetQueryDataAvailable failed, error %d\n", GetLastError());
408 if (flags & INTERNET_FLAG_ASYNC)
410 if (rc != 0)
412 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
414 else if (GetLastError() == ERROR_IO_PENDING)
416 WaitForSingleObject(hCompleteEvent, INFINITE);
417 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
418 continue;
421 if (length)
423 char *buffer;
424 buffer = HeapAlloc(GetProcessHeap(),0,length+1);
426 rc = InternetReadFile(hor,buffer,length,&length);
428 buffer[length]=0;
430 trace("ReadFile -> %i %i\n",rc,length);
432 HeapFree(GetProcessHeap(),0,buffer);
434 if (length == 0)
435 break;
437 /* WinXP does not send, but Win98 does */
438 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
439 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
440 abort:
441 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
442 if (hor != 0x0) {
443 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
444 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
445 SetLastError(0xdeadbeef);
446 rc = InternetCloseHandle(hor);
447 ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
448 SetLastError(0xdeadbeef);
449 rc = InternetCloseHandle(hor);
450 ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
451 ok (GetLastError() == ERROR_INVALID_HANDLE,
452 "Double close of handle should have set ERROR_INVALID_HANDLE instead of %u\n",
453 GetLastError());
455 /* We intentionally do not close the handle opened by InternetConnectA as this
456 * tickles bug #9479: native closes child internet handles when the parent handles
457 * are closed. This is verified below by checking that the number of
458 * INTERNET_STATUS_HANDLE_CLOSING notifications matches the number expected. */
459 if (hi != 0x0) {
460 SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
461 rc = InternetCloseHandle(hi);
462 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
463 if (flags & INTERNET_FLAG_ASYNC)
464 Sleep(100);
466 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
467 if (hor != 0x0) todo_wine
469 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
470 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
472 else
474 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
475 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
477 CloseHandle(hCompleteEvent);
478 first_connection_to_test_url = FALSE;
481 static void InternetReadFileExA_test(int flags)
483 DWORD rc;
484 DWORD length;
485 const char *types[2] = { "*", NULL };
486 HINTERNET hi, hic = 0, hor = 0;
487 INTERNET_BUFFERS inetbuffers;
489 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
491 trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
493 trace("InternetOpenA <--\n");
494 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
495 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
496 trace("InternetOpenA -->\n");
498 if (hi == 0x0) goto abort;
500 pInternetSetStatusCallbackA(hi,&callback);
502 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
504 trace("InternetConnectA <--\n");
505 hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
506 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
507 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
508 trace("InternetConnectA -->\n");
510 if (hic == 0x0) goto abort;
512 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
513 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
515 trace("HttpOpenRequestA <--\n");
516 hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
517 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
518 0xdeadbead);
519 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
521 * If the internet name can't be resolved we are probably behind
522 * a firewall or in some other way not directly connected to the
523 * Internet. Not enough reason to fail the test. Just ignore and
524 * abort.
526 } else {
527 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
529 trace("HttpOpenRequestA -->\n");
531 if (hor == 0x0) goto abort;
533 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
534 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
535 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
536 if (first_connection_to_test_url)
538 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
539 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
541 else
543 SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
544 SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
546 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
547 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
548 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
549 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
550 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
551 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
552 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
553 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
554 SET_EXPECT(INTERNET_STATUS_REDIRECT);
555 if (flags & INTERNET_FLAG_ASYNC)
556 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
557 else
558 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
560 trace("HttpSendRequestA -->\n");
561 SetLastError(0xdeadbeef);
562 rc = HttpSendRequestA(hor, "", -1, NULL, 0);
563 if (flags & INTERNET_FLAG_ASYNC)
564 ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
565 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
566 else
567 ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
568 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
569 trace("HttpSendRequestA <--\n");
571 if (!rc && (GetLastError() == ERROR_IO_PENDING))
572 WaitForSingleObject(hCompleteEvent, INFINITE);
574 if (first_connection_to_test_url)
576 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
577 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
579 else todo_wine
581 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
582 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
584 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
585 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
586 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
587 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
588 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
589 if (flags & INTERNET_FLAG_ASYNC)
590 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
591 else
592 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
593 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
594 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
595 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
597 /* tests invalid dwStructSize */
598 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
599 inetbuffers.lpcszHeader = NULL;
600 inetbuffers.dwHeadersLength = 0;
601 inetbuffers.dwBufferLength = 10;
602 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
603 inetbuffers.dwOffsetHigh = 1234;
604 inetbuffers.dwOffsetLow = 5678;
605 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
606 ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
607 "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
608 rc ? "TRUE" : "FALSE", GetLastError());
609 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
611 /* tests to see whether lpcszHeader is used - it isn't */
612 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
613 inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
614 inetbuffers.dwHeadersLength = 255;
615 inetbuffers.dwBufferLength = 0;
616 inetbuffers.lpvBuffer = NULL;
617 inetbuffers.dwOffsetHigh = 1234;
618 inetbuffers.dwOffsetLow = 5678;
619 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
620 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
621 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
622 ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
623 todo_wine
625 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
626 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
629 rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
630 ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
631 "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
632 rc ? "TRUE" : "FALSE", GetLastError());
634 length = 0;
635 trace("Entering Query loop\n");
637 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
638 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
639 while (TRUE)
641 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
642 inetbuffers.dwBufferLength = 1024;
643 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
644 inetbuffers.dwOffsetHigh = 1234;
645 inetbuffers.dwOffsetLow = 5678;
647 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
648 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
649 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
650 rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
651 if (!rc)
653 if (GetLastError() == ERROR_IO_PENDING)
655 trace("InternetReadFileEx -> PENDING\n");
656 ok(flags & INTERNET_FLAG_ASYNC,
657 "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
658 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
659 WaitForSingleObject(hCompleteEvent, INFINITE);
660 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
661 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
663 else
665 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
666 break;
669 else
671 trace("InternetReadFileEx -> SUCCEEDED\n");
672 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
673 if (inetbuffers.dwBufferLength)
675 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
676 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
678 else
680 /* Win98 still sends these when 0 bytes are read, WinXP does not */
681 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
682 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
686 trace("read %i bytes\n", inetbuffers.dwBufferLength);
687 ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
689 ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
690 "InternetReadFileEx sets offsets to 0x%x%08x\n",
691 inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
693 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
695 if (!inetbuffers.dwBufferLength)
696 break;
698 length += inetbuffers.dwBufferLength;
700 ok(length > 0, "failed to read any of the document\n");
701 trace("Finished. Read %d bytes\n", length);
703 /* WinXP does not send, but Win98 does */
704 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
705 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
706 abort:
707 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
708 if (hor) {
709 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
710 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
711 rc = InternetCloseHandle(hor);
712 ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
713 rc = InternetCloseHandle(hor);
714 ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
716 if (hic) {
717 rc = InternetCloseHandle(hic);
718 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
720 if (hi) {
721 SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
722 rc = InternetCloseHandle(hi);
723 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
724 if (flags & INTERNET_FLAG_ASYNC)
725 Sleep(100);
726 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
728 if (hor != 0x0) todo_wine
730 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
731 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
733 else
735 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
736 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
738 CloseHandle(hCompleteEvent);
739 first_connection_to_test_url = FALSE;
742 static void InternetOpenUrlA_test(void)
744 HINTERNET myhinternet, myhttp;
745 char buffer[0x400];
746 DWORD size, readbytes, totalbytes=0;
747 BOOL ret;
749 myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
750 ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
751 size = 0x400;
752 ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
753 ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
755 SetLastError(0);
756 myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
757 INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
758 if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
759 return; /* WinXP returns this when not connected to the net */
760 ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
761 ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
762 ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
763 totalbytes += readbytes;
764 while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
765 totalbytes += readbytes;
766 trace("read 0x%08x bytes\n",totalbytes);
768 InternetCloseHandle(myhttp);
769 InternetCloseHandle(myhinternet);
772 static void InternetTimeFromSystemTimeA_test(void)
774 BOOL ret;
775 static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
776 char string[INTERNET_RFC1123_BUFSIZE];
777 static const char expect[] = "Fri, 07 Jan 2005 12:06:35 GMT";
779 ret = pInternetTimeFromSystemTimeA( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
780 ok( ret, "InternetTimeFromSystemTimeA failed (%u)\n", GetLastError() );
782 ok( !memcmp( string, expect, sizeof(expect) ),
783 "InternetTimeFromSystemTimeA failed (%u)\n", GetLastError() );
786 static void InternetTimeFromSystemTimeW_test(void)
788 BOOL ret;
789 static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
790 WCHAR string[INTERNET_RFC1123_BUFSIZE + 1];
791 static const WCHAR expect[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
792 '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
794 ret = pInternetTimeFromSystemTimeW( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
795 ok( ret, "InternetTimeFromSystemTimeW failed (%u)\n", GetLastError() );
797 ok( !memcmp( string, expect, sizeof(expect) ),
798 "InternetTimeFromSystemTimeW failed (%u)\n", GetLastError() );
801 static void InternetTimeToSystemTimeA_test(void)
803 BOOL ret;
804 SYSTEMTIME time;
805 static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
806 static const char string[] = "Fri, 07 Jan 2005 12:06:35 GMT";
807 static const char string2[] = " fri 7 jan 2005 12 06 35";
809 ret = pInternetTimeToSystemTimeA( string, &time, 0 );
810 ok( ret, "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
811 ok( !memcmp( &time, &expect, sizeof(expect) ),
812 "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
814 ret = pInternetTimeToSystemTimeA( string2, &time, 0 );
815 ok( ret, "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
816 ok( !memcmp( &time, &expect, sizeof(expect) ),
817 "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
820 static void InternetTimeToSystemTimeW_test(void)
822 BOOL ret;
823 SYSTEMTIME time;
824 static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
825 static const WCHAR string[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
826 '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
827 static const WCHAR string2[] = { ' ','f','r','i',' ','7',' ','j','a','n',' ','2','0','0','5',' ',
828 '1','2',' ','0','6',' ','3','5',0 };
829 static const WCHAR string3[] = { 'F','r',0 };
831 ret = pInternetTimeToSystemTimeW( NULL, NULL, 0 );
832 ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
834 ret = pInternetTimeToSystemTimeW( NULL, &time, 0 );
835 ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
837 ret = pInternetTimeToSystemTimeW( string, NULL, 0 );
838 ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
840 ret = pInternetTimeToSystemTimeW( string, &time, 0 );
841 ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
843 ret = pInternetTimeToSystemTimeW( string, &time, 0 );
844 ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
845 ok( !memcmp( &time, &expect, sizeof(expect) ),
846 "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
848 ret = pInternetTimeToSystemTimeW( string2, &time, 0 );
849 ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
850 ok( !memcmp( &time, &expect, sizeof(expect) ),
851 "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
853 ret = pInternetTimeToSystemTimeW( string3, &time, 0 );
854 ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
857 static void HttpSendRequestEx_test(void)
859 HINTERNET hSession;
860 HINTERNET hConnect;
861 HINTERNET hRequest;
863 INTERNET_BUFFERS BufferIn;
864 DWORD dwBytesWritten;
865 DWORD dwBytesRead;
866 CHAR szBuffer[256];
867 int i;
868 BOOL ret;
870 static char szPostData[] = "mode=Test";
871 static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
873 hSession = InternetOpen("Wine Regression Test",
874 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
875 ok( hSession != NULL ,"Unable to open Internet session\n");
876 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
877 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
879 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
880 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
881 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
882 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
884 skip( "Network unreachable, skipping test\n" );
885 goto done;
887 ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
890 BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
891 BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
892 BufferIn.lpcszHeader = szContentType;
893 BufferIn.dwHeadersLength = sizeof(szContentType)-1;
894 BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
895 BufferIn.lpvBuffer = szPostData;
896 BufferIn.dwBufferLength = 3;
897 BufferIn.dwBufferTotal = sizeof(szPostData)-1;
898 BufferIn.dwOffsetLow = 0;
899 BufferIn.dwOffsetHigh = 0;
901 ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
902 ok(ret, "HttpSendRequestEx Failed with error %u\n", GetLastError());
904 for (i = 3; szPostData[i]; i++)
905 ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
906 "InternetWriteFile failed\n");
908 ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
910 ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
911 "Unable to read response\n");
912 szBuffer[dwBytesRead] = 0;
914 ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
915 ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0,"Got string %s\n",szBuffer);
917 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
918 done:
919 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
920 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
923 static void InternetOpenRequest_test(void)
925 HINTERNET session, connect, request;
926 static const char *types[] = { "*", "", NULL };
927 static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
928 static const WCHAR *typesW[] = { any, empty, NULL };
929 BOOL ret;
931 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
932 ok(session != NULL ,"Unable to open Internet session\n");
934 connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
935 INTERNET_SERVICE_HTTP, 0, 0);
936 ok(connect == NULL, "InternetConnectA should have failed\n");
937 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
939 connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
940 INTERNET_SERVICE_HTTP, 0, 0);
941 ok(connect == NULL, "InternetConnectA should have failed\n");
942 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
944 connect = InternetConnectA(session, "winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
945 INTERNET_SERVICE_HTTP, 0, 0);
946 ok(connect != NULL, "Unable to connect to http://winehq.org with error %d\n", GetLastError());
948 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
949 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
951 skip( "Network unreachable, skipping test\n" );
952 goto done;
954 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
956 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
957 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
958 ok(InternetCloseHandle(request), "Close request handle failed\n");
960 request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
961 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
963 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
964 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
965 ok(InternetCloseHandle(request), "Close request handle failed\n");
967 done:
968 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
969 ok(InternetCloseHandle(session), "Close session handle failed\n");
972 static void test_http_cache(void)
974 HINTERNET session, connect, request;
975 char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
976 DWORD size, file_size;
977 BYTE buf[100];
978 HANDLE file;
979 BOOL ret;
981 static const char *types[] = { "*", "", NULL };
983 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
984 ok(session != NULL ,"Unable to open Internet session\n");
986 connect = InternetConnectA(session, "www.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
987 INTERNET_SERVICE_HTTP, 0, 0);
988 ok(connect != NULL, "Unable to connect to http://winehq.org with error %d\n", GetLastError());
990 request = HttpOpenRequestA(connect, NULL, "/site/about", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
991 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
993 skip( "Network unreachable, skipping test\n" );
995 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
996 ok(InternetCloseHandle(session), "Close session handle failed\n");
998 return;
1000 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1002 size = sizeof(url);
1003 ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
1004 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_url) failed: %u\n", GetLastError());
1005 ok(!strcmp(url, "http://www.winehq.org/site/about"), "Wrong URL %s\n", url);
1007 size = sizeof(file_name);
1008 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1009 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1010 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1011 ok(!size, "size = %d\n", size);
1013 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1014 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1016 size = sizeof(file_name);
1017 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1018 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1020 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1021 FILE_ATTRIBUTE_NORMAL, NULL);
1022 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1023 file_size = GetFileSize(file, NULL);
1024 ok(file_size == 0, "file size=%d\n", file_size);
1026 ret = InternetReadFile(request, buf, sizeof(buf), &size);
1027 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1028 ok(size == sizeof(buf), "size=%d\n", size);
1030 file_size = GetFileSize(file, NULL);
1031 ok(file_size == sizeof(buf), "file size=%d\n", file_size);
1032 CloseHandle(file);
1034 ok(InternetCloseHandle(request), "Close request handle failed\n");
1036 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1037 FILE_ATTRIBUTE_NORMAL, NULL);
1038 ok(file == INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
1039 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError()=%u, expected ERROR_FILE_NOT_FOUND\n", GetLastError());
1041 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1042 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1044 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1045 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1046 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1047 ok(!size, "size = %d\n", size);
1049 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1050 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1052 size = sizeof(file_name);
1053 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1054 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1055 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1056 ok(!size, "size = %d\n", size);
1058 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1059 FILE_ATTRIBUTE_NORMAL, NULL);
1060 ok(file == INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
1061 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError()=%u, expected ERROR_FILE_NOT_FOUND\n", GetLastError());
1063 ok(InternetCloseHandle(request), "Close request handle failed\n");
1065 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1066 ok(InternetCloseHandle(session), "Close session handle failed\n");
1069 static void HttpHeaders_test(void)
1071 HINTERNET hSession;
1072 HINTERNET hConnect;
1073 HINTERNET hRequest;
1074 CHAR buffer[256];
1075 WCHAR wbuffer[256];
1076 DWORD len = 256;
1077 DWORD oldlen;
1078 DWORD index = 0;
1080 hSession = InternetOpen("Wine Regression Test",
1081 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1082 ok( hSession != NULL ,"Unable to open Internet session\n");
1083 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1084 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1086 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1087 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1088 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1089 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1091 skip( "Network unreachable, skipping test\n" );
1092 goto done;
1094 ok( hRequest != NULL, "Failed to open request handle\n");
1096 index = 0;
1097 len = sizeof(buffer);
1098 strcpy(buffer,"Warning");
1099 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1100 buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1102 ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1103 "Failed to add new header\n");
1105 index = 0;
1106 len = sizeof(buffer);
1107 strcpy(buffer,"Warning");
1108 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1109 buffer,&len,&index),"Unable to query header\n");
1110 ok(index == 1, "Index was not incremented\n");
1111 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1112 ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1113 ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1114 len = sizeof(buffer);
1115 strcpy(buffer,"Warning");
1116 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1117 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1119 index = 0;
1120 len = 5; /* could store the string but not the NULL terminator */
1121 strcpy(buffer,"Warning");
1122 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1123 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1124 ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1125 ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1127 /* a call with NULL will fail but will return the length */
1128 index = 0;
1129 len = sizeof(buffer);
1130 SetLastError(0xdeadbeef);
1131 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1132 NULL,&len,&index) == FALSE,"Query worked\n");
1133 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1134 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1135 ok(index == 0, "Index was incremented\n");
1137 /* even for a len that is too small */
1138 index = 0;
1139 len = 15;
1140 SetLastError(0xdeadbeef);
1141 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1142 NULL,&len,&index) == FALSE,"Query worked\n");
1143 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1144 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1145 ok(index == 0, "Index was incremented\n");
1147 index = 0;
1148 len = 0;
1149 SetLastError(0xdeadbeef);
1150 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1151 NULL,&len,&index) == FALSE,"Query worked\n");
1152 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1153 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1154 ok(index == 0, "Index was incremented\n");
1155 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1158 /* a working query */
1159 index = 0;
1160 len = sizeof(buffer);
1161 memset(buffer, 'x', 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 ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1165 ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1166 ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1167 /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1168 ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1169 ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1170 ok(index == 0, "Index was incremented\n");
1172 /* Like above two tests, but for W version */
1174 index = 0;
1175 len = 0;
1176 SetLastError(0xdeadbeef);
1177 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1178 NULL,&len,&index) == FALSE,"Query worked\n");
1179 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1180 ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1181 ok(index == 0, "Index was incremented\n");
1182 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1184 /* a working query */
1185 index = 0;
1186 len = sizeof(wbuffer);
1187 memset(wbuffer, 'x', sizeof(wbuffer));
1188 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1189 wbuffer,&len,&index),"Unable to query header\n");
1190 ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1191 ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1192 ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1193 ok(index == 0, "Index was incremented\n");
1195 /* end of W version tests */
1197 ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1198 "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1200 index = 0;
1201 len = sizeof(buffer);
1202 strcpy(buffer,"Warning");
1203 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1204 buffer,&len,&index),"Unable to query header\n");
1205 ok(index == 1, "Index was not incremented\n");
1206 ok(strcmp(buffer,"test1")==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),"Failed to get second header\n");
1211 ok(index == 2, "Index was not incremented\n");
1212 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1213 len = sizeof(buffer);
1214 strcpy(buffer,"Warning");
1215 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1216 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1218 ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1220 index = 0;
1221 len = sizeof(buffer);
1222 strcpy(buffer,"Warning");
1223 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1224 buffer,&len,&index),"Unable to query header\n");
1225 ok(index == 1, "Index was not incremented\n");
1226 ok(strcmp(buffer,"test2")==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),"Failed to get second header\n");
1231 ok(index == 2, "Index was not incremented\n");
1232 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1233 len = sizeof(buffer);
1234 strcpy(buffer,"Warning");
1235 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1236 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1238 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1240 index = 0;
1241 len = sizeof(buffer);
1242 strcpy(buffer,"Warning");
1243 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1244 buffer,&len,&index),"Unable to query header\n");
1245 ok(index == 1, "Index was not incremented\n");
1246 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1247 len = sizeof(buffer);
1248 strcpy(buffer,"Warning");
1249 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1250 buffer,&len,&index),"Failed to get second header\n");
1251 ok(index == 2, "Index was not incremented\n");
1252 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1253 len = sizeof(buffer);
1254 strcpy(buffer,"Warning");
1255 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1256 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1258 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1260 index = 0;
1261 len = sizeof(buffer);
1262 strcpy(buffer,"Warning");
1263 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1264 buffer,&len,&index),"Unable to query header\n");
1265 ok(index == 1, "Index was not incremented\n");
1266 ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1267 len = sizeof(buffer);
1268 strcpy(buffer,"Warning");
1269 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1270 ok(index == 2, "Index was not incremented\n");
1271 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1272 len = sizeof(buffer);
1273 strcpy(buffer,"Warning");
1274 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1276 ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1278 index = 0;
1279 len = sizeof(buffer);
1280 strcpy(buffer,"Warning");
1281 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1282 ok(index == 1, "Index was not incremented\n");
1283 ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1284 len = sizeof(buffer);
1285 strcpy(buffer,"Warning");
1286 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1287 ok(index == 2, "Index was not incremented\n");
1288 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1289 len = sizeof(buffer);
1290 strcpy(buffer,"Warning");
1291 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1293 ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1295 index = 0;
1296 len = sizeof(buffer);
1297 strcpy(buffer,"Warning");
1298 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1299 ok(index == 1, "Index was not incremented\n");
1300 ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1301 len = sizeof(buffer);
1302 strcpy(buffer,"Warning");
1303 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1304 ok(index == 2, "Index was not incremented\n");
1305 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1306 len = sizeof(buffer);
1307 strcpy(buffer,"Warning");
1308 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1310 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");
1312 index = 0;
1313 len = sizeof(buffer);
1314 strcpy(buffer,"Warning");
1315 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1316 ok(index == 1, "Index was not incremented\n");
1317 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1318 len = sizeof(buffer);
1319 strcpy(buffer,"Warning");
1320 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1321 ok(index == 2, "Index was not incremented\n");
1322 ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1323 len = sizeof(buffer);
1324 strcpy(buffer,"Warning");
1325 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1328 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1329 done:
1330 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1331 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1334 static const char contmsg[] =
1335 "HTTP/1.1 100 Continue\r\n";
1337 static const char okmsg[] =
1338 "HTTP/1.1 200 OK\r\n"
1339 "Server: winetest\r\n"
1340 "\r\n";
1342 static const char notokmsg[] =
1343 "HTTP/1.1 400 Bad Request\r\n"
1344 "Server: winetest\r\n"
1345 "\r\n";
1347 static const char noauthmsg[] =
1348 "HTTP/1.1 401 Unauthorized\r\n"
1349 "Server: winetest\r\n"
1350 "\r\n";
1352 static const char proxymsg[] =
1353 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1354 "Server: winetest\r\n"
1355 "Proxy-Connection: close\r\n"
1356 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1357 "\r\n";
1359 static const char page1[] =
1360 "<HTML>\r\n"
1361 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1362 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1363 "</HTML>\r\n\r\n";
1365 struct server_info {
1366 HANDLE hEvent;
1367 int port;
1370 static DWORD CALLBACK server_thread(LPVOID param)
1372 struct server_info *si = param;
1373 int r, c, i, on;
1374 SOCKET s;
1375 struct sockaddr_in sa;
1376 char buffer[0x100];
1377 WSADATA wsaData;
1378 int last_request = 0;
1379 char host_header[22];
1380 static int test_b = 0;
1382 WSAStartup(MAKEWORD(1,1), &wsaData);
1384 s = socket(AF_INET, SOCK_STREAM, 0);
1385 if (s == INVALID_SOCKET)
1386 return 1;
1388 on = 1;
1389 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1391 memset(&sa, 0, sizeof sa);
1392 sa.sin_family = AF_INET;
1393 sa.sin_port = htons(si->port);
1394 sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1396 r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1397 if (r<0)
1398 return 1;
1400 listen(s, 0);
1402 SetEvent(si->hEvent);
1404 sprintf(host_header, "Host: localhost:%d", si->port);
1408 c = accept(s, NULL, NULL);
1410 memset(buffer, 0, sizeof buffer);
1411 for(i=0; i<(sizeof buffer-1); i++)
1413 r = recv(c, &buffer[i], 1, 0);
1414 if (r != 1)
1415 break;
1416 if (i<4) continue;
1417 if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
1418 buffer[i-3] == '\r' && buffer[i-1] == '\r')
1419 break;
1421 if (strstr(buffer, "GET /test1"))
1423 if (!strstr(buffer, "Content-Length: 0"))
1425 send(c, okmsg, sizeof okmsg-1, 0);
1426 send(c, page1, sizeof page1-1, 0);
1428 else
1429 send(c, notokmsg, sizeof notokmsg-1, 0);
1431 if (strstr(buffer, "/test2"))
1433 if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
1435 send(c, okmsg, sizeof okmsg-1, 0);
1436 send(c, page1, sizeof page1-1, 0);
1438 else
1439 send(c, proxymsg, sizeof proxymsg-1, 0);
1441 if (strstr(buffer, "/test3"))
1443 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
1444 send(c, okmsg, sizeof okmsg-1, 0);
1445 else
1446 send(c, noauthmsg, sizeof noauthmsg-1, 0);
1448 if (strstr(buffer, "/test4"))
1450 if (strstr(buffer, "Connection: Close"))
1451 send(c, okmsg, sizeof okmsg-1, 0);
1452 else
1453 send(c, notokmsg, sizeof notokmsg-1, 0);
1455 if (strstr(buffer, "POST /test5"))
1457 if (strstr(buffer, "Content-Length: 0"))
1459 send(c, okmsg, sizeof okmsg-1, 0);
1460 send(c, page1, sizeof page1-1, 0);
1462 else
1463 send(c, notokmsg, sizeof notokmsg-1, 0);
1465 if (strstr(buffer, "GET /test6"))
1467 send(c, contmsg, sizeof contmsg-1, 0);
1468 send(c, contmsg, sizeof contmsg-1, 0);
1469 send(c, okmsg, sizeof okmsg-1, 0);
1470 send(c, page1, sizeof page1-1, 0);
1472 if (strstr(buffer, "POST /test7"))
1474 if (strstr(buffer, "Content-Length: 100"))
1476 send(c, okmsg, sizeof okmsg-1, 0);
1477 send(c, page1, sizeof page1-1, 0);
1479 else
1480 send(c, notokmsg, sizeof notokmsg-1, 0);
1482 if (strstr(buffer, "/test8"))
1484 if (!strstr(buffer, "Connection: Close") &&
1485 strstr(buffer, "Connection: Keep-Alive") &&
1486 !strstr(buffer, "Cache-Control: no-cache") &&
1487 !strstr(buffer, "Pragma: no-cache") &&
1488 strstr(buffer, host_header))
1489 send(c, okmsg, sizeof okmsg-1, 0);
1490 else
1491 send(c, notokmsg, sizeof notokmsg-1, 0);
1493 if (strstr(buffer, "/test9"))
1495 if (!strstr(buffer, "Connection: Close") &&
1496 !strstr(buffer, "Connection: Keep-Alive") &&
1497 !strstr(buffer, "Cache-Control: no-cache") &&
1498 !strstr(buffer, "Pragma: no-cache") &&
1499 strstr(buffer, host_header))
1500 send(c, okmsg, sizeof okmsg-1, 0);
1501 else
1502 send(c, notokmsg, sizeof notokmsg-1, 0);
1504 if (strstr(buffer, "/testA"))
1506 if (!strstr(buffer, "Connection: Close") &&
1507 !strstr(buffer, "Connection: Keep-Alive") &&
1508 (strstr(buffer, "Cache-Control: no-cache") ||
1509 strstr(buffer, "Pragma: no-cache")) &&
1510 strstr(buffer, host_header))
1511 send(c, okmsg, sizeof okmsg-1, 0);
1512 else
1513 send(c, notokmsg, sizeof notokmsg-1, 0);
1515 if (!test_b && strstr(buffer, "/testB HTTP/1.1"))
1517 test_b = 1;
1518 send(c, okmsg, sizeof okmsg-1, 0);
1519 recvfrom(c, buffer, sizeof buffer, 0, NULL, NULL);
1520 send(c, okmsg, sizeof okmsg-1, 0);
1522 if (strstr(buffer, "/testC"))
1524 if (strstr(buffer, "Cookie: cookie=biscuit"))
1525 send(c, okmsg, sizeof okmsg-1, 0);
1526 else
1527 send(c, notokmsg, sizeof notokmsg-1, 0);
1529 if (strstr(buffer, "GET /quit"))
1531 send(c, okmsg, sizeof okmsg-1, 0);
1532 send(c, page1, sizeof page1-1, 0);
1533 last_request = 1;
1536 shutdown(c, 2);
1537 closesocket(c);
1538 } while (!last_request);
1540 closesocket(s);
1542 return 0;
1545 static void test_basic_request(int port, const char *verb, const char *url)
1547 HINTERNET hi, hc, hr;
1548 DWORD r, count;
1549 char buffer[0x100];
1551 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1552 ok(hi != NULL, "open failed\n");
1554 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1555 ok(hc != NULL, "connect failed\n");
1557 hr = HttpOpenRequest(hc, verb, url, NULL, NULL, NULL, 0, 0);
1558 ok(hr != NULL, "HttpOpenRequest failed\n");
1560 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1561 ok(r, "HttpSendRequest failed\n");
1563 count = 0;
1564 memset(buffer, 0, sizeof buffer);
1565 r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1566 ok(r, "InternetReadFile failed\n");
1567 ok(count == sizeof page1 - 1, "count was wrong\n");
1568 ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
1570 InternetCloseHandle(hr);
1571 InternetCloseHandle(hc);
1572 InternetCloseHandle(hi);
1575 static void test_proxy_indirect(int port)
1577 HINTERNET hi, hc, hr;
1578 DWORD r, sz, val;
1579 char buffer[0x40];
1581 hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1582 ok(hi != NULL, "open failed\n");
1584 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1585 ok(hc != NULL, "connect failed\n");
1587 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1588 ok(hr != NULL, "HttpOpenRequest failed\n");
1590 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1591 ok(r, "HttpSendRequest failed\n");
1593 sz = sizeof buffer;
1594 r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1595 ok(r, "HttpQueryInfo failed\n");
1596 ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1598 sz = sizeof buffer;
1599 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1600 ok(r, "HttpQueryInfo failed\n");
1601 ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1603 sz = sizeof val;
1604 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1605 ok(r, "HttpQueryInfo failed\n");
1606 ok(val == 407, "proxy code wrong\n");
1608 sz = sizeof buffer;
1609 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1610 ok(r, "HttpQueryInfo failed\n");
1611 ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1613 sz = sizeof buffer;
1614 r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1615 ok(r, "HttpQueryInfo failed\n");
1616 ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1618 sz = sizeof buffer;
1619 r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1620 ok(r, "HttpQueryInfo failed\n");
1621 ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1623 sz = sizeof buffer;
1624 r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1625 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1626 ok(r == FALSE, "HttpQueryInfo failed\n");
1628 InternetCloseHandle(hr);
1629 InternetCloseHandle(hc);
1630 InternetCloseHandle(hi);
1633 static void test_proxy_direct(int port)
1635 HINTERNET hi, hc, hr;
1636 DWORD r, sz;
1637 char buffer[0x40];
1638 static CHAR username[] = "mike",
1639 password[] = "1101";
1641 sprintf(buffer, "localhost:%d\n", port);
1642 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1643 ok(hi != NULL, "open failed\n");
1645 /* try connect without authorization */
1646 hc = InternetConnect(hi, "www.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1647 ok(hc != NULL, "connect failed\n");
1649 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1650 ok(hr != NULL, "HttpOpenRequest failed\n");
1652 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1653 ok(r, "HttpSendRequest failed\n");
1655 sz = sizeof buffer;
1656 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1657 ok(r, "HttpQueryInfo failed\n");
1658 ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1661 /* set the user + password then try again */
1662 todo_wine {
1663 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
1664 ok(r, "failed to set user\n");
1666 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
1667 ok(r, "failed to set password\n");
1670 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1671 ok(r, "HttpSendRequest failed\n");
1672 sz = sizeof buffer;
1673 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1674 ok(r, "HttpQueryInfo failed\n");
1675 todo_wine {
1676 ok(!strcmp(buffer, "200"), "proxy code wrong\n");
1680 InternetCloseHandle(hr);
1681 InternetCloseHandle(hc);
1682 InternetCloseHandle(hi);
1685 static void test_header_handling_order(int port)
1687 static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
1688 static char connection[] = "Connection: Close";
1690 static const char *types[2] = { "*", NULL };
1691 HINTERNET session, connect, request;
1692 DWORD size, status;
1693 BOOL ret;
1695 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1696 ok(session != NULL, "InternetOpen failed\n");
1698 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1699 ok(connect != NULL, "InternetConnect failed\n");
1701 request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1702 ok(request != NULL, "HttpOpenRequest failed\n");
1704 ret = HttpAddRequestHeaders(request, authorization, ~0UL, HTTP_ADDREQ_FLAG_ADD);
1705 ok(ret, "HttpAddRequestHeaders failed\n");
1707 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1708 ok(ret, "HttpSendRequest failed\n");
1710 status = 0;
1711 size = sizeof(status);
1712 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1713 ok(ret, "HttpQueryInfo failed\n");
1714 ok(status == 200, "request failed with status %u\n", status);
1716 InternetCloseHandle(request);
1718 request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1719 ok(request != NULL, "HttpOpenRequest failed\n");
1721 ret = HttpSendRequest(request, connection, ~0UL, NULL, 0);
1722 ok(ret, "HttpSendRequest failed\n");
1724 status = 0;
1725 size = sizeof(status);
1726 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1727 ok(ret, "HttpQueryInfo failed\n");
1728 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
1730 InternetCloseHandle(request);
1732 request = HttpOpenRequest(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1733 ok(request != NULL, "HttpOpenRequest failed\n");
1735 ret = HttpAddRequestHeaders(request, "Content-Length: 100\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1736 ok(ret, "HttpAddRequestHeaders failed\n");
1738 ret = HttpSendRequest(request, connection, ~0UL, NULL, 0);
1739 ok(ret, "HttpSendRequest failed\n");
1741 status = 0;
1742 size = sizeof(status);
1743 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1744 ok(ret, "HttpQueryInfo failed\n");
1745 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
1747 InternetCloseHandle(request);
1748 InternetCloseHandle(connect);
1749 InternetCloseHandle(session);
1752 static void test_connection_header(int port)
1754 HINTERNET ses, con, req;
1755 DWORD size, status;
1756 BOOL ret;
1758 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1759 ok(ses != NULL, "InternetOpen failed\n");
1761 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1762 ok(con != NULL, "InternetConnect failed\n");
1764 req = HttpOpenRequest(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1765 ok(req != NULL, "HttpOpenRequest failed\n");
1767 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1768 ok(ret, "HttpSendRequest failed\n");
1770 status = 0;
1771 size = sizeof(status);
1772 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1773 ok(ret, "HttpQueryInfo failed\n");
1774 ok(status == 200, "request failed with status %u\n", status);
1776 InternetCloseHandle(req);
1778 req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
1779 ok(req != NULL, "HttpOpenRequest failed\n");
1781 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1782 ok(ret, "HttpSendRequest failed\n");
1784 status = 0;
1785 size = sizeof(status);
1786 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1787 ok(ret, "HttpQueryInfo failed\n");
1788 ok(status == 200, "request failed with status %u\n", status);
1790 InternetCloseHandle(req);
1792 req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1793 ok(req != NULL, "HttpOpenRequest failed\n");
1795 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1796 ok(ret, "HttpSendRequest failed\n");
1798 status = 0;
1799 size = sizeof(status);
1800 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1801 ok(ret, "HttpQueryInfo failed\n");
1802 ok(status == 200, "request failed with status %u\n", status);
1804 InternetCloseHandle(req);
1806 req = HttpOpenRequest(con, "POST", "/testA", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1807 ok(req != NULL, "HttpOpenRequest failed\n");
1809 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1810 ok(ret, "HttpSendRequest failed\n");
1812 status = 0;
1813 size = sizeof(status);
1814 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1815 ok(ret, "HttpQueryInfo failed\n");
1816 ok(status == 200, "request failed with status %u\n", status);
1818 InternetCloseHandle(req);
1819 InternetCloseHandle(con);
1820 InternetCloseHandle(ses);
1823 static void test_http1_1(int port)
1825 HINTERNET ses, con, req;
1826 BOOL ret;
1828 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1829 ok(ses != NULL, "InternetOpen failed\n");
1831 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1832 ok(con != NULL, "InternetConnect failed\n");
1834 req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1835 ok(req != NULL, "HttpOpenRequest failed\n");
1837 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1838 if (ret)
1840 InternetCloseHandle(req);
1842 req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1843 ok(req != NULL, "HttpOpenRequest failed\n");
1845 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1846 todo_wine
1847 ok(ret, "HttpSendRequest failed\n");
1850 InternetCloseHandle(req);
1851 InternetCloseHandle(con);
1852 InternetCloseHandle(ses);
1855 static void test_cookie_header(int port)
1857 HINTERNET ses, con, req;
1858 DWORD size, status, error;
1859 BOOL ret;
1860 char buffer[64];
1862 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1863 ok(ses != NULL, "InternetOpen failed\n");
1865 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1866 ok(con != NULL, "InternetConnect failed\n");
1868 InternetSetCookie("http://localhost", "cookie", "biscuit");
1870 req = HttpOpenRequest(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1871 ok(req != NULL, "HttpOpenRequest failed\n");
1873 buffer[0] = 0;
1874 size = sizeof(buffer);
1875 SetLastError(0xdeadbeef);
1876 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1877 error = GetLastError();
1878 ok(!ret, "HttpQueryInfo succeeded\n");
1879 ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %u expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
1881 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1882 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1884 status = 0;
1885 size = sizeof(status);
1886 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1887 ok(ret, "HttpQueryInfo failed\n");
1888 ok(status == 200, "request failed with status %u\n", status);
1890 buffer[0] = 0;
1891 size = sizeof(buffer);
1892 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1893 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
1894 ok(!strcmp(buffer, "cookie=biscuit"), "got '%s' expected \'cookie=biscuit\'\n", buffer);
1896 InternetCloseHandle(req);
1897 InternetCloseHandle(con);
1898 InternetCloseHandle(ses);
1901 static void test_http_connection(void)
1903 struct server_info si;
1904 HANDLE hThread;
1905 DWORD id = 0, r;
1907 si.hEvent = CreateEvent(NULL, 0, 0, NULL);
1908 si.port = 7531;
1910 hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
1911 ok( hThread != NULL, "create thread failed\n");
1913 r = WaitForSingleObject(si.hEvent, 10000);
1914 ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
1915 if (r != WAIT_OBJECT_0)
1916 return;
1918 test_basic_request(si.port, "GET", "/test1");
1919 test_proxy_indirect(si.port);
1920 test_proxy_direct(si.port);
1921 test_header_handling_order(si.port);
1922 test_basic_request(si.port, "POST", "/test5");
1923 test_basic_request(si.port, "GET", "/test6");
1924 test_connection_header(si.port);
1925 test_http1_1(si.port);
1926 test_cookie_header(si.port);
1928 /* send the basic request again to shutdown the server thread */
1929 test_basic_request(si.port, "GET", "/quit");
1931 r = WaitForSingleObject(hThread, 3000);
1932 ok( r == WAIT_OBJECT_0, "thread wait failed\n");
1933 CloseHandle(hThread);
1936 static void test_user_agent_header(void)
1938 HINTERNET ses, con, req;
1939 DWORD size, err;
1940 char buffer[64];
1941 BOOL ret;
1943 ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1944 ok(ses != NULL, "InternetOpen failed\n");
1946 con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1947 ok(con != NULL, "InternetConnect failed\n");
1949 req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
1950 ok(req != NULL, "HttpOpenRequest failed\n");
1952 size = sizeof(buffer);
1953 ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1954 err = GetLastError();
1955 ok(!ret, "HttpQueryInfo succeeded\n");
1956 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
1958 ret = HttpAddRequestHeaders(req, "User-Agent: Gizmo Project\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1959 ok(ret, "HttpAddRequestHeaders succeeded\n");
1961 size = sizeof(buffer);
1962 ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1963 err = GetLastError();
1964 ok(ret, "HttpQueryInfo failed\n");
1965 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
1967 InternetCloseHandle(req);
1969 req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
1970 ok(req != NULL, "HttpOpenRequest failed\n");
1972 size = sizeof(buffer);
1973 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1974 err = GetLastError();
1975 ok(!ret, "HttpQueryInfo succeeded\n");
1976 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
1978 ret = HttpAddRequestHeaders(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1979 ok(ret, "HttpAddRequestHeaders failed\n");
1981 buffer[0] = 0;
1982 size = sizeof(buffer);
1983 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1984 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
1985 ok(!strcmp(buffer, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
1987 InternetCloseHandle(req);
1988 InternetCloseHandle(con);
1989 InternetCloseHandle(ses);
1992 static void test_bogus_accept_types_array(void)
1994 HINTERNET ses, con, req;
1995 static const char *types[] = { (const char *)6240, "*/*", "%p", "", "*/*", NULL };
1996 DWORD size;
1997 char buffer[32];
1998 BOOL ret;
2000 ses = InternetOpen("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
2001 con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2002 req = HttpOpenRequest(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
2004 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
2006 buffer[0] = 0;
2007 size = sizeof(buffer);
2008 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2009 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2010 ok(!strcmp(buffer, ", */*, %p, , */*") || /* IE6 */
2011 !strcmp(buffer, "*/*, %p, */*"),
2012 "got '%s' expected '*/*, %%p, */*' or ', */*, %%p, , */*'\n", buffer);
2014 InternetCloseHandle(req);
2015 InternetCloseHandle(con);
2016 InternetCloseHandle(ses);
2019 struct context
2021 HANDLE event;
2022 HINTERNET req;
2025 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
2027 INTERNET_ASYNC_RESULT *result = info;
2028 struct context *ctx = (struct context *)context;
2030 trace("%p 0x%08lx %u %p 0x%08x\n", handle, context, status, info, size);
2032 if (status == INTERNET_STATUS_REQUEST_COMPLETE)
2034 trace("request handle: 0x%08lx\n", result->dwResult);
2035 ctx->req = (HINTERNET)result->dwResult;
2036 SetEvent(ctx->event);
2038 if (status == INTERNET_STATUS_HANDLE_CLOSING)
2040 DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
2042 if (InternetQueryOption(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
2043 ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
2044 SetEvent(ctx->event);
2048 static void test_open_url_async(void)
2050 BOOL ret;
2051 HINTERNET ses, req;
2052 DWORD size;
2053 struct context ctx;
2054 ULONG type;
2056 ctx.req = NULL;
2057 ctx.event = CreateEvent(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
2059 ses = InternetOpen("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
2060 ok(ses != NULL, "InternetOpen failed\n");
2062 pInternetSetStatusCallbackA(ses, cb);
2063 ResetEvent(ctx.event);
2065 req = InternetOpenUrl(ses, "http://www.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
2066 ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
2068 WaitForSingleObject(ctx.event, INFINITE);
2070 type = 0;
2071 size = sizeof(type);
2072 ret = InternetQueryOption(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
2073 ok(ret, "InternetQueryOption failed: %u\n", GetLastError());
2074 ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
2075 "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %u\n", type);
2077 size = 0;
2078 ret = HttpQueryInfo(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
2079 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
2080 ok(size > 0, "expected size > 0\n");
2082 ResetEvent(ctx.event);
2083 InternetCloseHandle(ctx.req);
2084 WaitForSingleObject(ctx.event, INFINITE);
2086 InternetCloseHandle(ses);
2087 CloseHandle(ctx.event);
2090 #define STATUS_STRING(status) \
2091 memcpy(status_string[status], #status, sizeof(CHAR) * \
2092 (strlen(#status) < MAX_STATUS_NAME ? \
2093 strlen(#status) : \
2094 MAX_STATUS_NAME - 1))
2095 static void init_status_tests(void)
2097 memset(expect, 0, sizeof(expect));
2098 memset(wine_allow, 0, sizeof(wine_allow));
2099 memset(notified, 0, sizeof(notified));
2100 memset(status_string, 0, sizeof(status_string));
2101 STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
2102 STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
2103 STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
2104 STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
2105 STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
2106 STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
2107 STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
2108 STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
2109 STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
2110 STATUS_STRING(INTERNET_STATUS_PREFETCH);
2111 STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
2112 STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
2113 STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
2114 STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
2115 STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
2116 STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
2117 STATUS_STRING(INTERNET_STATUS_REDIRECT);
2118 STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
2119 STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
2120 STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
2121 STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
2122 STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
2123 STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
2124 STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
2125 STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
2126 STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
2128 #undef STATUS_STRING
2130 START_TEST(http)
2132 HMODULE hdll;
2133 hdll = GetModuleHandleA("wininet.dll");
2134 pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
2135 pInternetTimeFromSystemTimeA = (void*)GetProcAddress(hdll, "InternetTimeFromSystemTimeA");
2136 pInternetTimeFromSystemTimeW = (void*)GetProcAddress(hdll, "InternetTimeFromSystemTimeW");
2137 pInternetTimeToSystemTimeA = (void*)GetProcAddress(hdll, "InternetTimeToSystemTimeA");
2138 pInternetTimeToSystemTimeW = (void*)GetProcAddress(hdll, "InternetTimeToSystemTimeW");
2140 if (!pInternetSetStatusCallbackA)
2141 skip("skipping the InternetReadFile tests\n");
2142 else
2144 init_status_tests();
2145 InternetReadFile_test(INTERNET_FLAG_ASYNC);
2146 InternetReadFile_test(0);
2147 InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
2148 test_open_url_async();
2150 InternetOpenRequest_test();
2151 test_http_cache();
2152 InternetOpenUrlA_test();
2153 if (!pInternetTimeFromSystemTimeA)
2154 skip("skipping the InternetTime tests\n");
2155 else
2157 InternetTimeFromSystemTimeA_test();
2158 InternetTimeFromSystemTimeW_test();
2159 InternetTimeToSystemTimeA_test();
2160 InternetTimeToSystemTimeW_test();
2162 HttpSendRequestEx_test();
2163 HttpHeaders_test();
2164 test_http_connection();
2165 test_user_agent_header();
2166 test_bogus_accept_types_array();