push 337eb2e2d902d84a5d689451984c5832d7e04fc4
[wine/hacks.git] / dlls / wininet / tests / http.c
blobbc9ad33a3a6563289ebe1e8cca4b38216f4d345b
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 #define SET_OPTIONAL2(status, num) \
47 optional[status] = num
49 #define SET_OPTIONAL(status) \
50 SET_OPTIONAL2(status, 1)
52 /* SET_WINE_ALLOW's should be used with an appropriate
53 * todo_wine CHECK_NOTIFIED at a later point in the code */
54 #define SET_WINE_ALLOW2(status, num) \
55 wine_allow[status] = num
57 #define SET_WINE_ALLOW(status) \
58 SET_WINE_ALLOW2(status, 1)
60 #define CHECK_EXPECT(status) \
61 do { \
62 if (!expect[status] && !optional[status] && wine_allow[status]) \
63 { \
64 todo_wine ok(expect[status], "unexpected status %d (%s)\n", status, \
65 status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
66 status_string[status] : "unknown"); \
67 wine_allow[status]--; \
68 } \
69 else \
70 { \
71 ok(expect[status] || optional[status], "unexpected status %d (%s)\n", status, \
72 status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
73 status_string[status] : "unknown"); \
74 if (expect[status]) expect[status]--; \
75 else optional[status]--; \
76 } \
77 notified[status]++; \
78 }while(0)
80 /* CLEAR_NOTIFIED used in cases when notification behavior
81 * differs between Windows versions */
82 #define CLEAR_NOTIFIED(status) \
83 expect[status] = optional[status] = wine_allow[status] = notified[status] = 0;
85 #define CHECK_NOTIFIED2(status, num) \
86 do { \
87 ok(notified[status] == (num), "expected status %d (%s) %d times, received %d times\n", \
88 status, status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
89 status_string[status] : "unknown", (num), notified[status]); \
90 CLEAR_NOTIFIED(status); \
91 }while(0)
93 #define CHECK_NOTIFIED(status) \
94 CHECK_NOTIFIED2(status, 1)
96 #define CHECK_NOT_NOTIFIED(status) \
97 CHECK_NOTIFIED2(status, 0)
99 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
100 #define MAX_STATUS_NAME 50
101 static int expect[MAX_INTERNET_STATUS], optional[MAX_INTERNET_STATUS],
102 wine_allow[MAX_INTERNET_STATUS], notified[MAX_INTERNET_STATUS];
103 static CHAR status_string[MAX_INTERNET_STATUS][MAX_STATUS_NAME];
105 static HANDLE hCompleteEvent;
107 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
110 static VOID WINAPI callback(
111 HINTERNET hInternet,
112 DWORD_PTR dwContext,
113 DWORD dwInternetStatus,
114 LPVOID lpvStatusInformation,
115 DWORD dwStatusInformationLength
118 CHECK_EXPECT(dwInternetStatus);
119 switch (dwInternetStatus)
121 case INTERNET_STATUS_RESOLVING_NAME:
122 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
123 GetCurrentThreadId(), hInternet, dwContext,
124 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
125 *(LPSTR)lpvStatusInformation = '\0';
126 break;
127 case INTERNET_STATUS_NAME_RESOLVED:
128 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
129 GetCurrentThreadId(), hInternet, dwContext,
130 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
131 *(LPSTR)lpvStatusInformation = '\0';
132 break;
133 case INTERNET_STATUS_CONNECTING_TO_SERVER:
134 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
135 GetCurrentThreadId(), hInternet, dwContext,
136 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
137 *(LPSTR)lpvStatusInformation = '\0';
138 break;
139 case INTERNET_STATUS_CONNECTED_TO_SERVER:
140 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
141 GetCurrentThreadId(), hInternet, dwContext,
142 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
143 *(LPSTR)lpvStatusInformation = '\0';
144 break;
145 case INTERNET_STATUS_SENDING_REQUEST:
146 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
147 GetCurrentThreadId(), hInternet, dwContext,
148 lpvStatusInformation,dwStatusInformationLength);
149 break;
150 case INTERNET_STATUS_REQUEST_SENT:
151 ok(dwStatusInformationLength == sizeof(DWORD),
152 "info length should be sizeof(DWORD) instead of %d\n",
153 dwStatusInformationLength);
154 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
155 GetCurrentThreadId(), hInternet, dwContext,
156 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
157 break;
158 case INTERNET_STATUS_RECEIVING_RESPONSE:
159 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
160 GetCurrentThreadId(), hInternet, dwContext,
161 lpvStatusInformation,dwStatusInformationLength);
162 break;
163 case INTERNET_STATUS_RESPONSE_RECEIVED:
164 ok(dwStatusInformationLength == sizeof(DWORD),
165 "info length should be sizeof(DWORD) instead of %d\n",
166 dwStatusInformationLength);
167 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
168 GetCurrentThreadId(), hInternet, dwContext,
169 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
170 break;
171 case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
172 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
173 GetCurrentThreadId(), hInternet,dwContext,
174 lpvStatusInformation,dwStatusInformationLength);
175 break;
176 case INTERNET_STATUS_PREFETCH:
177 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
178 GetCurrentThreadId(), hInternet, dwContext,
179 lpvStatusInformation,dwStatusInformationLength);
180 break;
181 case INTERNET_STATUS_CLOSING_CONNECTION:
182 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
183 GetCurrentThreadId(), hInternet, dwContext,
184 lpvStatusInformation,dwStatusInformationLength);
185 break;
186 case INTERNET_STATUS_CONNECTION_CLOSED:
187 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
188 GetCurrentThreadId(), hInternet, dwContext,
189 lpvStatusInformation,dwStatusInformationLength);
190 break;
191 case INTERNET_STATUS_HANDLE_CREATED:
192 ok(dwStatusInformationLength == sizeof(HINTERNET),
193 "info length should be sizeof(HINTERNET) instead of %d\n",
194 dwStatusInformationLength);
195 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
196 GetCurrentThreadId(), hInternet, dwContext,
197 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
198 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
199 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
200 break;
201 case INTERNET_STATUS_HANDLE_CLOSING:
202 ok(dwStatusInformationLength == sizeof(HINTERNET),
203 "info length should be sizeof(HINTERNET) instead of %d\n",
204 dwStatusInformationLength);
205 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
206 GetCurrentThreadId(), hInternet, dwContext,
207 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
208 break;
209 case INTERNET_STATUS_REQUEST_COMPLETE:
211 INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
212 ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
213 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
214 dwStatusInformationLength);
215 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
216 GetCurrentThreadId(), hInternet, dwContext,
217 iar->dwResult,iar->dwError,dwStatusInformationLength);
218 SetEvent(hCompleteEvent);
219 break;
221 case INTERNET_STATUS_REDIRECT:
222 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
223 GetCurrentThreadId(), hInternet, dwContext,
224 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
225 *(LPSTR)lpvStatusInformation = '\0';
226 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
227 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
228 break;
229 case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
230 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
231 GetCurrentThreadId(), hInternet, dwContext,
232 lpvStatusInformation, dwStatusInformationLength);
233 break;
234 default:
235 trace("%04x:Callback %p 0x%lx %d %p %d\n",
236 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
237 lpvStatusInformation, dwStatusInformationLength);
241 static void InternetReadFile_test(int flags)
243 BOOL res;
244 CHAR buffer[4000];
245 DWORD length;
246 DWORD out;
247 const char *types[2] = { "*", NULL };
248 HINTERNET hi, hic = 0, hor = 0;
250 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
252 trace("Starting InternetReadFile test with flags 0x%x\n",flags);
254 trace("InternetOpenA <--\n");
255 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
256 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
257 trace("InternetOpenA -->\n");
259 if (hi == 0x0) goto abort;
261 pInternetSetStatusCallbackA(hi,&callback);
263 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
265 trace("InternetConnectA <--\n");
266 hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
267 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
268 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
269 trace("InternetConnectA -->\n");
271 if (hic == 0x0) goto abort;
273 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
274 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
276 trace("HttpOpenRequestA <--\n");
277 hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
278 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
279 0xdeadbead);
280 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
282 * If the internet name can't be resolved we are probably behind
283 * a firewall or in some other way not directly connected to the
284 * Internet. Not enough reason to fail the test. Just ignore and
285 * abort.
287 } else {
288 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
290 trace("HttpOpenRequestA -->\n");
292 if (hor == 0x0) goto abort;
294 length = sizeof(buffer);
295 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
296 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
297 ok(!strcmp(buffer, "http://www.winehq.org/about/"), "Wrong URL %s\n", buffer);
299 length = sizeof(buffer);
300 res = HttpQueryInfoA(hor, HTTP_QUERY_RAW_HEADERS, buffer, &length, 0x0);
301 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
302 ok(length == 0, "HTTP_QUERY_RAW_HEADERS: expected length 0, but got %d\n", length);
303 ok(!strcmp(buffer, ""), "HTTP_QUERY_RAW_HEADERS: expected string \"\", but got \"%s\"\n", buffer);
305 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
306 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
307 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
308 if (first_connection_to_test_url)
310 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
311 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
312 SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
313 SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
315 else
317 SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
318 SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
320 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
321 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
322 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
323 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
324 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
325 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
326 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
327 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
328 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION);
329 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED);
330 SET_EXPECT(INTERNET_STATUS_REDIRECT);
331 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
332 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
333 if (flags & INTERNET_FLAG_ASYNC)
334 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
335 else
336 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
338 trace("HttpSendRequestA -->\n");
339 SetLastError(0xdeadbeef);
340 res = HttpSendRequestA(hor, "", -1, NULL, 0);
341 if (flags & INTERNET_FLAG_ASYNC)
342 ok(!res && (GetLastError() == ERROR_IO_PENDING),
343 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
344 else
345 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
346 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
347 trace("HttpSendRequestA <--\n");
349 if (flags & INTERNET_FLAG_ASYNC)
350 WaitForSingleObject(hCompleteEvent, INFINITE);
352 todo_wine if (first_connection_to_test_url)
354 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
355 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
357 else
359 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
360 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
362 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
363 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
364 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
365 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
366 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
367 if (flags & INTERNET_FLAG_ASYNC)
368 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
369 else
370 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
371 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
372 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
373 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
375 length = 4;
376 res = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
377 ok(res, "InternetQueryOptionA(INTERNET_OPTION_REQUEST) failed with error %d\n", GetLastError());
379 length = 100;
380 res = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
381 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed with error %d\n", GetLastError());
383 length = sizeof(buffer);
384 res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
385 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
386 buffer[length]=0;
388 length = sizeof(buffer);
389 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
390 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
391 ok(!strcmp(buffer, "http://www.winehq.org/site/about"), "Wrong URL %s\n", buffer);
393 length = 16;
394 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
395 trace("Option 0x5 -> %i %s (%u)\n",res,buffer,GetLastError());
397 length = 100;
398 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
399 buffer[length]=0;
400 trace("Option 0x1 -> %i %s\n",res,buffer);
402 SetLastError(0xdeadbeef);
403 res = InternetReadFile(NULL, buffer, 100, &length);
404 ok(!res, "InternetReadFile should have failed\n");
405 ok(GetLastError() == ERROR_INVALID_HANDLE,
406 "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
407 GetLastError());
409 length = 100;
410 trace("Entering Query loop\n");
412 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
413 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
414 while (TRUE)
416 if (flags & INTERNET_FLAG_ASYNC)
417 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
418 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
419 ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
420 ok(res || ((flags & INTERNET_FLAG_ASYNC) && GetLastError() == ERROR_IO_PENDING),
421 "InternetQueryDataAvailable failed, error %d\n", GetLastError());
422 if (flags & INTERNET_FLAG_ASYNC)
424 if (res)
426 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
428 else if (GetLastError() == ERROR_IO_PENDING)
430 WaitForSingleObject(hCompleteEvent, INFINITE);
431 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
432 continue;
435 if (length)
437 char *buffer;
438 buffer = HeapAlloc(GetProcessHeap(),0,length+1);
440 res = InternetReadFile(hor,buffer,length,&length);
442 buffer[length]=0;
444 trace("ReadFile -> %s %i\n",res?"TRUE":"FALSE",length);
446 HeapFree(GetProcessHeap(),0,buffer);
448 if (length == 0)
449 break;
451 /* WinXP does not send, but Win98 does */
452 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
453 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
454 abort:
455 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
456 if (hor != 0x0) {
457 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
458 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
459 SetLastError(0xdeadbeef);
460 res = InternetCloseHandle(hor);
461 ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
462 SetLastError(0xdeadbeef);
463 res = InternetCloseHandle(hor);
464 ok (!res, "Double close of handle opened by HttpOpenRequestA succeeded\n");
465 ok (GetLastError() == ERROR_INVALID_HANDLE,
466 "Double close of handle should have set ERROR_INVALID_HANDLE instead of %u\n",
467 GetLastError());
469 /* We intentionally do not close the handle opened by InternetConnectA as this
470 * tickles bug #9479: native closes child internet handles when the parent handles
471 * are closed. This is verified below by checking that the number of
472 * INTERNET_STATUS_HANDLE_CLOSING notifications matches the number expected. */
473 if (hi != 0x0) {
474 SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
475 res = InternetCloseHandle(hi);
476 ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
477 if (flags & INTERNET_FLAG_ASYNC)
478 Sleep(100);
480 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
481 if (hor != 0x0 && (flags & INTERNET_FLAG_ASYNC)) todo_wine
483 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
484 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
486 else
488 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
489 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
491 CloseHandle(hCompleteEvent);
492 first_connection_to_test_url = FALSE;
495 static void InternetReadFileExA_test(int flags)
497 DWORD rc;
498 DWORD length;
499 const char *types[2] = { "*", NULL };
500 HINTERNET hi, hic = 0, hor = 0;
501 INTERNET_BUFFERS inetbuffers;
503 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
505 trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
507 trace("InternetOpenA <--\n");
508 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
509 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
510 trace("InternetOpenA -->\n");
512 if (hi == 0x0) goto abort;
514 pInternetSetStatusCallbackA(hi,&callback);
516 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
518 trace("InternetConnectA <--\n");
519 hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
520 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
521 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
522 trace("InternetConnectA -->\n");
524 if (hic == 0x0) goto abort;
526 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
527 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
529 trace("HttpOpenRequestA <--\n");
530 hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
531 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
532 0xdeadbead);
533 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
535 * If the internet name can't be resolved we are probably behind
536 * a firewall or in some other way not directly connected to the
537 * Internet. Not enough reason to fail the test. Just ignore and
538 * abort.
540 } else {
541 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
543 trace("HttpOpenRequestA -->\n");
545 if (hor == 0x0) goto abort;
547 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
548 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
549 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
550 if (first_connection_to_test_url)
552 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
553 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
554 SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
555 SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
557 else
559 SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
560 SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
562 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
563 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
564 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
565 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
566 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
567 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
568 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
569 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
570 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION);
571 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED);
572 SET_EXPECT(INTERNET_STATUS_REDIRECT);
573 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
574 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
575 if (flags & INTERNET_FLAG_ASYNC)
576 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
577 else
578 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
580 trace("HttpSendRequestA -->\n");
581 SetLastError(0xdeadbeef);
582 rc = HttpSendRequestA(hor, "", -1, NULL, 0);
583 if (flags & INTERNET_FLAG_ASYNC)
584 ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
585 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
586 else
587 ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
588 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
589 trace("HttpSendRequestA <--\n");
591 if (!rc && (GetLastError() == ERROR_IO_PENDING))
592 WaitForSingleObject(hCompleteEvent, INFINITE);
594 if (first_connection_to_test_url)
596 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
597 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
599 else todo_wine
601 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
602 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
604 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
605 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
606 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
607 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
608 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
609 if (flags & INTERNET_FLAG_ASYNC)
610 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
611 else
612 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
613 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
614 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
615 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
617 /* tests invalid dwStructSize */
618 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
619 inetbuffers.lpcszHeader = NULL;
620 inetbuffers.dwHeadersLength = 0;
621 inetbuffers.dwBufferLength = 10;
622 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
623 inetbuffers.dwOffsetHigh = 1234;
624 inetbuffers.dwOffsetLow = 5678;
625 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
626 ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
627 "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
628 rc ? "TRUE" : "FALSE", GetLastError());
629 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
631 /* tests to see whether lpcszHeader is used - it isn't */
632 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
633 inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
634 inetbuffers.dwHeadersLength = 255;
635 inetbuffers.dwBufferLength = 0;
636 inetbuffers.lpvBuffer = NULL;
637 inetbuffers.dwOffsetHigh = 1234;
638 inetbuffers.dwOffsetLow = 5678;
639 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
640 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
641 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
642 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
643 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
644 ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
645 trace("read %i bytes\n", inetbuffers.dwBufferLength);
646 todo_wine
648 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
649 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
652 rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
653 ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
654 "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
655 rc ? "TRUE" : "FALSE", GetLastError());
657 length = 0;
658 trace("Entering Query loop\n");
660 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
661 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
662 while (TRUE)
664 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
665 inetbuffers.dwBufferLength = 1024;
666 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
667 inetbuffers.dwOffsetHigh = 1234;
668 inetbuffers.dwOffsetLow = 5678;
670 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
671 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
672 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
673 rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
674 if (!rc)
676 if (GetLastError() == ERROR_IO_PENDING)
678 trace("InternetReadFileEx -> PENDING\n");
679 ok(flags & INTERNET_FLAG_ASYNC,
680 "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
681 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
682 WaitForSingleObject(hCompleteEvent, INFINITE);
683 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
684 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
686 else
688 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
689 break;
692 else
694 trace("InternetReadFileEx -> SUCCEEDED\n");
695 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
696 if (inetbuffers.dwBufferLength)
698 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
699 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
701 else
703 /* Win98 still sends these when 0 bytes are read, WinXP does not */
704 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
705 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
709 trace("read %i bytes\n", inetbuffers.dwBufferLength);
710 ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
712 ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
713 "InternetReadFileEx sets offsets to 0x%x%08x\n",
714 inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
716 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
718 if (!inetbuffers.dwBufferLength)
719 break;
721 length += inetbuffers.dwBufferLength;
723 todo_wine ok(length > 0, "failed to read any of the document\n");
724 trace("Finished. Read %d bytes\n", length);
726 /* WinXP does not send, but Win98 does */
727 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
728 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
729 abort:
730 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
731 if (hor) {
732 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
733 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
734 rc = InternetCloseHandle(hor);
735 ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
736 rc = InternetCloseHandle(hor);
737 ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
739 if (hic) {
740 rc = InternetCloseHandle(hic);
741 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
743 if (hi) {
744 SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
745 rc = InternetCloseHandle(hi);
746 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
747 if (flags & INTERNET_FLAG_ASYNC)
748 Sleep(100);
749 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
751 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
752 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
753 CloseHandle(hCompleteEvent);
754 first_connection_to_test_url = FALSE;
757 static void InternetOpenUrlA_test(void)
759 HINTERNET myhinternet, myhttp;
760 char buffer[0x400];
761 DWORD size, readbytes, totalbytes=0;
762 BOOL ret;
764 myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
765 ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
766 size = 0x400;
767 ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
768 ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
770 SetLastError(0);
771 myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
772 INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
773 if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
774 return; /* WinXP returns this when not connected to the net */
775 ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
776 ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
777 ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
778 totalbytes += readbytes;
779 while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
780 totalbytes += readbytes;
781 trace("read 0x%08x bytes\n",totalbytes);
783 InternetCloseHandle(myhttp);
784 InternetCloseHandle(myhinternet);
787 static void HttpSendRequestEx_test(void)
789 HINTERNET hSession;
790 HINTERNET hConnect;
791 HINTERNET hRequest;
793 INTERNET_BUFFERS BufferIn;
794 DWORD dwBytesWritten;
795 DWORD dwBytesRead;
796 CHAR szBuffer[256];
797 int i;
798 BOOL ret;
800 static char szPostData[] = "mode=Test";
801 static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
803 hSession = InternetOpen("Wine Regression Test",
804 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
805 ok( hSession != NULL ,"Unable to open Internet session\n");
806 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
807 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
809 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
810 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
811 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
812 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
814 skip( "Network unreachable, skipping test\n" );
815 goto done;
817 ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
820 BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
821 BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
822 BufferIn.lpcszHeader = szContentType;
823 BufferIn.dwHeadersLength = sizeof(szContentType)-1;
824 BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
825 BufferIn.lpvBuffer = szPostData;
826 BufferIn.dwBufferLength = 3;
827 BufferIn.dwBufferTotal = sizeof(szPostData)-1;
828 BufferIn.dwOffsetLow = 0;
829 BufferIn.dwOffsetHigh = 0;
831 ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
832 ok(ret, "HttpSendRequestEx Failed with error %u\n", GetLastError());
834 for (i = 3; szPostData[i]; i++)
835 ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
836 "InternetWriteFile failed\n");
838 ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
840 ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
841 "Unable to read response\n");
842 szBuffer[dwBytesRead] = 0;
844 ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
845 ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0,"Got string %s\n",szBuffer);
847 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
848 done:
849 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
850 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
853 static void InternetOpenRequest_test(void)
855 HINTERNET session, connect, request;
856 static const char *types[] = { "*", "", NULL };
857 static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
858 static const WCHAR *typesW[] = { any, empty, NULL };
859 BOOL ret;
861 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
862 ok(session != NULL ,"Unable to open Internet session\n");
864 connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
865 INTERNET_SERVICE_HTTP, 0, 0);
866 ok(connect == NULL, "InternetConnectA should have failed\n");
867 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
869 connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
870 INTERNET_SERVICE_HTTP, 0, 0);
871 ok(connect == NULL, "InternetConnectA should have failed\n");
872 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
874 connect = InternetConnectA(session, "winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
875 INTERNET_SERVICE_HTTP, 0, 0);
876 ok(connect != NULL, "Unable to connect to http://winehq.org with error %d\n", GetLastError());
878 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
879 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
881 skip( "Network unreachable, skipping test\n" );
882 goto done;
884 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
886 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
887 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
888 ok(InternetCloseHandle(request), "Close request handle failed\n");
890 request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
891 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
893 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
894 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
895 ok(InternetCloseHandle(request), "Close request handle failed\n");
897 done:
898 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
899 ok(InternetCloseHandle(session), "Close session handle failed\n");
902 static void test_http_cache(void)
904 HINTERNET session, connect, request;
905 char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
906 DWORD size, file_size;
907 BYTE buf[100];
908 HANDLE file;
909 BOOL ret;
911 static const char *types[] = { "*", "", NULL };
913 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
914 ok(session != NULL ,"Unable to open Internet session\n");
916 connect = InternetConnectA(session, "www.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
917 INTERNET_SERVICE_HTTP, 0, 0);
918 ok(connect != NULL, "Unable to connect to http://winehq.org with error %d\n", GetLastError());
920 request = HttpOpenRequestA(connect, NULL, "/site/about", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
921 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
923 skip( "Network unreachable, skipping test\n" );
925 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
926 ok(InternetCloseHandle(session), "Close session handle failed\n");
928 return;
930 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
932 size = sizeof(url);
933 ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
934 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_url) failed: %u\n", GetLastError());
935 ok(!strcmp(url, "http://www.winehq.org/site/about"), "Wrong URL %s\n", url);
937 size = sizeof(file_name);
938 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
939 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
940 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
941 ok(!size, "size = %d\n", size);
943 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
944 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
946 size = sizeof(file_name);
947 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
948 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
950 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
951 FILE_ATTRIBUTE_NORMAL, NULL);
952 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
953 file_size = GetFileSize(file, NULL);
954 ok(file_size == 0, "file size=%d\n", file_size);
956 ret = InternetReadFile(request, buf, sizeof(buf), &size);
957 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
958 ok(size == sizeof(buf), "size=%d\n", size);
960 file_size = GetFileSize(file, NULL);
961 ok(file_size == sizeof(buf), "file size=%d\n", file_size);
962 CloseHandle(file);
964 ok(InternetCloseHandle(request), "Close request handle failed\n");
966 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
967 FILE_ATTRIBUTE_NORMAL, NULL);
968 ok(file == INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
969 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError()=%u, expected ERROR_FILE_NOT_FOUND\n", GetLastError());
971 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
972 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
974 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
975 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
976 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
977 ok(!size, "size = %d\n", size);
979 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
980 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
982 size = sizeof(file_name);
983 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
984 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
985 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
986 ok(!size, "size = %d\n", size);
988 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
989 FILE_ATTRIBUTE_NORMAL, NULL);
990 ok(file == INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
991 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError()=%u, expected ERROR_FILE_NOT_FOUND\n", GetLastError());
993 ok(InternetCloseHandle(request), "Close request handle failed\n");
995 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
996 ok(InternetCloseHandle(session), "Close session handle failed\n");
999 static void HttpHeaders_test(void)
1001 HINTERNET hSession;
1002 HINTERNET hConnect;
1003 HINTERNET hRequest;
1004 CHAR buffer[256];
1005 WCHAR wbuffer[256];
1006 DWORD len = 256;
1007 DWORD oldlen;
1008 DWORD index = 0;
1010 hSession = InternetOpen("Wine Regression Test",
1011 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1012 ok( hSession != NULL ,"Unable to open Internet session\n");
1013 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1014 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1016 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1017 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1018 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1019 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1021 skip( "Network unreachable, skipping test\n" );
1022 goto done;
1024 ok( hRequest != NULL, "Failed to open request handle\n");
1026 index = 0;
1027 len = sizeof(buffer);
1028 strcpy(buffer,"Warning");
1029 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1030 buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1032 ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1033 "Failed to add new header\n");
1035 index = 0;
1036 len = sizeof(buffer);
1037 strcpy(buffer,"Warning");
1038 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1039 buffer,&len,&index),"Unable to query header\n");
1040 ok(index == 1, "Index was not incremented\n");
1041 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1042 ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1043 ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1044 len = sizeof(buffer);
1045 strcpy(buffer,"Warning");
1046 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1047 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1049 index = 0;
1050 len = 5; /* could store the string but not the NULL terminator */
1051 strcpy(buffer,"Warning");
1052 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1053 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1054 ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1055 ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1057 /* a call with NULL will fail but will return the length */
1058 index = 0;
1059 len = sizeof(buffer);
1060 SetLastError(0xdeadbeef);
1061 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1062 NULL,&len,&index) == FALSE,"Query worked\n");
1063 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1064 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1065 ok(index == 0, "Index was incremented\n");
1067 /* even for a len that is too small */
1068 index = 0;
1069 len = 15;
1070 SetLastError(0xdeadbeef);
1071 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1072 NULL,&len,&index) == FALSE,"Query worked\n");
1073 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1074 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1075 ok(index == 0, "Index was incremented\n");
1077 index = 0;
1078 len = 0;
1079 SetLastError(0xdeadbeef);
1080 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1081 NULL,&len,&index) == FALSE,"Query worked\n");
1082 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1083 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1084 ok(index == 0, "Index was incremented\n");
1085 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1088 /* a working query */
1089 index = 0;
1090 len = sizeof(buffer);
1091 memset(buffer, 'x', sizeof(buffer));
1092 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1093 buffer,&len,&index),"Unable to query header\n");
1094 ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1095 ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1096 ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1097 /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1098 ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1099 ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1100 ok(index == 0, "Index was incremented\n");
1102 /* Like above two tests, but for W version */
1104 index = 0;
1105 len = 0;
1106 SetLastError(0xdeadbeef);
1107 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1108 NULL,&len,&index) == FALSE,"Query worked\n");
1109 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1110 ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1111 ok(index == 0, "Index was incremented\n");
1112 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1114 /* a working query */
1115 index = 0;
1116 len = sizeof(wbuffer);
1117 memset(wbuffer, 'x', sizeof(wbuffer));
1118 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1119 wbuffer,&len,&index),"Unable to query header\n");
1120 ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1121 ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1122 ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1123 ok(index == 0, "Index was incremented\n");
1125 /* end of W version tests */
1127 /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1128 index = 0;
1129 len = sizeof(buffer);
1130 memset(buffer, 'x', sizeof(buffer));
1131 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF,
1132 buffer,&len,&index) == TRUE,"Query failed\n");
1133 ok(len == 2, "Expected 2, got %d\n", len);
1134 ok(strcmp(buffer, "\r\n") == 0, "Expected CRLF, got '%s'\n", buffer);
1135 ok(index == 0, "Index was incremented\n");
1137 ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1138 "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1140 index = 0;
1141 len = sizeof(buffer);
1142 strcpy(buffer,"Warning");
1143 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1144 buffer,&len,&index),"Unable to query header\n");
1145 ok(index == 1, "Index was not incremented\n");
1146 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1147 len = sizeof(buffer);
1148 strcpy(buffer,"Warning");
1149 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1150 buffer,&len,&index),"Failed to get second header\n");
1151 ok(index == 2, "Index was not incremented\n");
1152 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1153 len = sizeof(buffer);
1154 strcpy(buffer,"Warning");
1155 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1156 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1158 ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1160 index = 0;
1161 len = sizeof(buffer);
1162 strcpy(buffer,"Warning");
1163 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1164 buffer,&len,&index),"Unable to query header\n");
1165 ok(index == 1, "Index was not incremented\n");
1166 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1167 len = sizeof(buffer);
1168 strcpy(buffer,"Warning");
1169 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1170 buffer,&len,&index),"Failed to get second header\n");
1171 ok(index == 2, "Index was not incremented\n");
1172 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1173 len = sizeof(buffer);
1174 strcpy(buffer,"Warning");
1175 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1176 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1178 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1180 index = 0;
1181 len = sizeof(buffer);
1182 strcpy(buffer,"Warning");
1183 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1184 buffer,&len,&index),"Unable to query header\n");
1185 ok(index == 1, "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),"Failed to get second header\n");
1191 ok(index == 2, "Index was not incremented\n");
1192 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1193 len = sizeof(buffer);
1194 strcpy(buffer,"Warning");
1195 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1196 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1198 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\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,"test2, test4")==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, buffer,&len,&index),"Failed to get second header\n");
1210 ok(index == 2, "Index was not incremented\n");
1211 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1212 len = sizeof(buffer);
1213 strcpy(buffer,"Warning");
1214 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1216 ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1218 index = 0;
1219 len = sizeof(buffer);
1220 strcpy(buffer,"Warning");
1221 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1222 ok(index == 1, "Index was not incremented\n");
1223 ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1224 len = sizeof(buffer);
1225 strcpy(buffer,"Warning");
1226 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1227 ok(index == 2, "Index was not incremented\n");
1228 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1229 len = sizeof(buffer);
1230 strcpy(buffer,"Warning");
1231 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1233 ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1235 index = 0;
1236 len = sizeof(buffer);
1237 strcpy(buffer,"Warning");
1238 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1239 ok(index == 1, "Index was not incremented\n");
1240 ok(strcmp(buffer,"test2, test4, test5; test6")==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:test7",-1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE), "HTTP_ADDREQ_FLAG_ADD with HTTP_ADDREQ_FLAG_REPALCE Did not work\n");
1252 index = 0;
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,"test3")==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,"test7")==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");
1268 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1269 done:
1270 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1271 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1274 static const char contmsg[] =
1275 "HTTP/1.1 100 Continue\r\n";
1277 static const char okmsg[] =
1278 "HTTP/1.1 200 OK\r\n"
1279 "Server: winetest\r\n"
1280 "\r\n";
1282 static const char okmsg2[] =
1283 "HTTP/1.1 200 OK\r\n"
1284 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
1285 "Server: winetest\r\n"
1286 "Content-Length: 0\r\n"
1287 "Set-Cookie: one\r\n"
1288 "Set-Cookie: two\r\n"
1289 "\r\n";
1291 static const char notokmsg[] =
1292 "HTTP/1.1 400 Bad Request\r\n"
1293 "Server: winetest\r\n"
1294 "\r\n";
1296 static const char noauthmsg[] =
1297 "HTTP/1.1 401 Unauthorized\r\n"
1298 "Server: winetest\r\n"
1299 "Connection: close\r\n"
1300 "WWW-Authenticate: Basic realm=\"placebo\"\r\n"
1301 "\r\n";
1303 static const char proxymsg[] =
1304 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1305 "Server: winetest\r\n"
1306 "Proxy-Connection: close\r\n"
1307 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1308 "\r\n";
1310 static const char page1[] =
1311 "<HTML>\r\n"
1312 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1313 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1314 "</HTML>\r\n\r\n";
1316 struct server_info {
1317 HANDLE hEvent;
1318 int port;
1321 static DWORD CALLBACK server_thread(LPVOID param)
1323 struct server_info *si = param;
1324 int r, c, i, on;
1325 SOCKET s;
1326 struct sockaddr_in sa;
1327 char buffer[0x100];
1328 WSADATA wsaData;
1329 int last_request = 0;
1330 char host_header[22];
1331 static int test_b = 0;
1333 WSAStartup(MAKEWORD(1,1), &wsaData);
1335 s = socket(AF_INET, SOCK_STREAM, 0);
1336 if (s == INVALID_SOCKET)
1337 return 1;
1339 on = 1;
1340 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1342 memset(&sa, 0, sizeof sa);
1343 sa.sin_family = AF_INET;
1344 sa.sin_port = htons(si->port);
1345 sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1347 r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1348 if (r<0)
1349 return 1;
1351 listen(s, 0);
1353 SetEvent(si->hEvent);
1355 sprintf(host_header, "Host: localhost:%d", si->port);
1359 c = accept(s, NULL, NULL);
1361 memset(buffer, 0, sizeof buffer);
1362 for(i=0; i<(sizeof buffer-1); i++)
1364 r = recv(c, &buffer[i], 1, 0);
1365 if (r != 1)
1366 break;
1367 if (i<4) continue;
1368 if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
1369 buffer[i-3] == '\r' && buffer[i-1] == '\r')
1370 break;
1372 if (strstr(buffer, "GET /test1"))
1374 if (!strstr(buffer, "Content-Length: 0"))
1376 send(c, okmsg, sizeof okmsg-1, 0);
1377 send(c, page1, sizeof page1-1, 0);
1379 else
1380 send(c, notokmsg, sizeof notokmsg-1, 0);
1382 if (strstr(buffer, "/test2"))
1384 if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
1386 send(c, okmsg, sizeof okmsg-1, 0);
1387 send(c, page1, sizeof page1-1, 0);
1389 else
1390 send(c, proxymsg, sizeof proxymsg-1, 0);
1392 if (strstr(buffer, "/test3"))
1394 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
1395 send(c, okmsg, sizeof okmsg-1, 0);
1396 else
1397 send(c, noauthmsg, sizeof noauthmsg-1, 0);
1399 if (strstr(buffer, "/test4"))
1401 if (strstr(buffer, "Connection: Close"))
1402 send(c, okmsg, sizeof okmsg-1, 0);
1403 else
1404 send(c, notokmsg, sizeof notokmsg-1, 0);
1406 if (strstr(buffer, "POST /test5"))
1408 if (strstr(buffer, "Content-Length: 0"))
1410 send(c, okmsg, sizeof okmsg-1, 0);
1411 send(c, page1, sizeof page1-1, 0);
1413 else
1414 send(c, notokmsg, sizeof notokmsg-1, 0);
1416 if (strstr(buffer, "GET /test6"))
1418 send(c, contmsg, sizeof contmsg-1, 0);
1419 send(c, contmsg, sizeof contmsg-1, 0);
1420 send(c, okmsg, sizeof okmsg-1, 0);
1421 send(c, page1, sizeof page1-1, 0);
1423 if (strstr(buffer, "POST /test7"))
1425 if (strstr(buffer, "Content-Length: 100"))
1427 send(c, okmsg, sizeof okmsg-1, 0);
1428 send(c, page1, sizeof page1-1, 0);
1430 else
1431 send(c, notokmsg, sizeof notokmsg-1, 0);
1433 if (strstr(buffer, "/test8"))
1435 if (!strstr(buffer, "Connection: Close") &&
1436 strstr(buffer, "Connection: Keep-Alive") &&
1437 !strstr(buffer, "Cache-Control: no-cache") &&
1438 !strstr(buffer, "Pragma: no-cache") &&
1439 strstr(buffer, host_header))
1440 send(c, okmsg, sizeof okmsg-1, 0);
1441 else
1442 send(c, notokmsg, sizeof notokmsg-1, 0);
1444 if (strstr(buffer, "/test9"))
1446 if (!strstr(buffer, "Connection: Close") &&
1447 !strstr(buffer, "Connection: Keep-Alive") &&
1448 !strstr(buffer, "Cache-Control: no-cache") &&
1449 !strstr(buffer, "Pragma: no-cache") &&
1450 strstr(buffer, host_header))
1451 send(c, okmsg, sizeof okmsg-1, 0);
1452 else
1453 send(c, notokmsg, sizeof notokmsg-1, 0);
1455 if (strstr(buffer, "/testA"))
1457 if (!strstr(buffer, "Connection: Close") &&
1458 !strstr(buffer, "Connection: Keep-Alive") &&
1459 (strstr(buffer, "Cache-Control: no-cache") ||
1460 strstr(buffer, "Pragma: no-cache")) &&
1461 strstr(buffer, host_header))
1462 send(c, okmsg, sizeof okmsg-1, 0);
1463 else
1464 send(c, notokmsg, sizeof notokmsg-1, 0);
1466 if (!test_b && strstr(buffer, "/testB HTTP/1.1"))
1468 test_b = 1;
1469 send(c, okmsg, sizeof okmsg-1, 0);
1470 recvfrom(c, buffer, sizeof buffer, 0, NULL, NULL);
1471 send(c, okmsg, sizeof okmsg-1, 0);
1473 if (strstr(buffer, "/testC"))
1475 if (strstr(buffer, "Cookie: cookie=biscuit"))
1476 send(c, okmsg, sizeof okmsg-1, 0);
1477 else
1478 send(c, notokmsg, sizeof notokmsg-1, 0);
1480 if (strstr(buffer, "/testD"))
1482 send(c, okmsg2, sizeof okmsg2-1, 0);
1484 if (strstr(buffer, "GET /quit"))
1486 send(c, okmsg, sizeof okmsg-1, 0);
1487 send(c, page1, sizeof page1-1, 0);
1488 last_request = 1;
1491 shutdown(c, 2);
1492 closesocket(c);
1493 } while (!last_request);
1495 closesocket(s);
1497 return 0;
1500 static void test_basic_request(int port, const char *verb, const char *url)
1502 HINTERNET hi, hc, hr;
1503 DWORD r, count;
1504 char buffer[0x100];
1506 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1507 ok(hi != NULL, "open failed\n");
1509 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1510 ok(hc != NULL, "connect failed\n");
1512 hr = HttpOpenRequest(hc, verb, url, NULL, NULL, NULL, 0, 0);
1513 ok(hr != NULL, "HttpOpenRequest failed\n");
1515 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1516 ok(r, "HttpSendRequest failed\n");
1518 count = 0;
1519 memset(buffer, 0, sizeof buffer);
1520 r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1521 ok(r, "InternetReadFile failed\n");
1522 ok(count == sizeof page1 - 1, "count was wrong\n");
1523 ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
1525 InternetCloseHandle(hr);
1526 InternetCloseHandle(hc);
1527 InternetCloseHandle(hi);
1530 static void test_proxy_indirect(int port)
1532 HINTERNET hi, hc, hr;
1533 DWORD r, sz, val;
1534 char buffer[0x40];
1536 hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1537 ok(hi != NULL, "open failed\n");
1539 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1540 ok(hc != NULL, "connect failed\n");
1542 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1543 ok(hr != NULL, "HttpOpenRequest failed\n");
1545 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1546 ok(r, "HttpSendRequest failed\n");
1548 sz = sizeof buffer;
1549 r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1550 ok(r, "HttpQueryInfo failed\n");
1551 ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1553 sz = sizeof buffer;
1554 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1555 ok(r, "HttpQueryInfo failed\n");
1556 ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1558 sz = sizeof val;
1559 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1560 ok(r, "HttpQueryInfo failed\n");
1561 ok(val == 407, "proxy code wrong\n");
1563 sz = sizeof buffer;
1564 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1565 ok(r, "HttpQueryInfo failed\n");
1566 ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1568 sz = sizeof buffer;
1569 r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1570 ok(r, "HttpQueryInfo failed\n");
1571 ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1573 sz = sizeof buffer;
1574 r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1575 ok(r, "HttpQueryInfo failed\n");
1576 ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1578 sz = sizeof buffer;
1579 r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1580 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1581 ok(r == FALSE, "HttpQueryInfo failed\n");
1583 InternetCloseHandle(hr);
1584 InternetCloseHandle(hc);
1585 InternetCloseHandle(hi);
1588 static void test_proxy_direct(int port)
1590 HINTERNET hi, hc, hr;
1591 DWORD r, sz;
1592 char buffer[0x40];
1593 static CHAR username[] = "mike",
1594 password[] = "1101";
1596 sprintf(buffer, "localhost:%d\n", port);
1597 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1598 ok(hi != NULL, "open failed\n");
1600 /* try connect without authorization */
1601 hc = InternetConnect(hi, "www.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1602 ok(hc != NULL, "connect failed\n");
1604 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1605 ok(hr != NULL, "HttpOpenRequest failed\n");
1607 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1608 ok(r, "HttpSendRequest failed\n");
1610 sz = sizeof buffer;
1611 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1612 ok(r, "HttpQueryInfo failed\n");
1613 ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1616 /* set the user + password then try again */
1617 todo_wine {
1618 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
1619 ok(r, "failed to set user\n");
1621 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
1622 ok(r, "failed to set password\n");
1625 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1626 ok(r, "HttpSendRequest failed\n");
1627 sz = sizeof buffer;
1628 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1629 ok(r, "HttpQueryInfo failed\n");
1630 todo_wine {
1631 ok(!strcmp(buffer, "200"), "proxy code wrong\n");
1635 InternetCloseHandle(hr);
1636 InternetCloseHandle(hc);
1637 InternetCloseHandle(hi);
1640 static void test_header_handling_order(int port)
1642 static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
1643 static char connection[] = "Connection: Close";
1645 static const char *types[2] = { "*", NULL };
1646 HINTERNET session, connect, request;
1647 DWORD size, status;
1648 BOOL ret;
1650 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1651 ok(session != NULL, "InternetOpen failed\n");
1653 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1654 ok(connect != NULL, "InternetConnect failed\n");
1656 request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1657 ok(request != NULL, "HttpOpenRequest failed\n");
1659 ret = HttpAddRequestHeaders(request, authorization, ~0UL, HTTP_ADDREQ_FLAG_ADD);
1660 ok(ret, "HttpAddRequestHeaders failed\n");
1662 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1663 ok(ret, "HttpSendRequest failed\n");
1665 status = 0;
1666 size = sizeof(status);
1667 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1668 ok(ret, "HttpQueryInfo failed\n");
1669 ok(status == 200, "request failed with status %u\n", status);
1671 InternetCloseHandle(request);
1673 request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1674 ok(request != NULL, "HttpOpenRequest failed\n");
1676 ret = HttpSendRequest(request, connection, ~0UL, NULL, 0);
1677 ok(ret, "HttpSendRequest failed\n");
1679 status = 0;
1680 size = sizeof(status);
1681 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1682 ok(ret, "HttpQueryInfo failed\n");
1683 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
1685 InternetCloseHandle(request);
1687 request = HttpOpenRequest(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1688 ok(request != NULL, "HttpOpenRequest failed\n");
1690 ret = HttpAddRequestHeaders(request, "Content-Length: 100\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1691 ok(ret, "HttpAddRequestHeaders failed\n");
1693 ret = HttpSendRequest(request, connection, ~0UL, NULL, 0);
1694 ok(ret, "HttpSendRequest failed\n");
1696 status = 0;
1697 size = sizeof(status);
1698 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1699 ok(ret, "HttpQueryInfo failed\n");
1700 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
1702 InternetCloseHandle(request);
1703 InternetCloseHandle(connect);
1704 InternetCloseHandle(session);
1707 static void test_connection_header(int port)
1709 HINTERNET ses, con, req;
1710 DWORD size, status;
1711 BOOL ret;
1713 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1714 ok(ses != NULL, "InternetOpen failed\n");
1716 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1717 ok(con != NULL, "InternetConnect failed\n");
1719 req = HttpOpenRequest(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1720 ok(req != NULL, "HttpOpenRequest failed\n");
1722 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1723 ok(ret, "HttpSendRequest failed\n");
1725 status = 0;
1726 size = sizeof(status);
1727 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1728 ok(ret, "HttpQueryInfo failed\n");
1729 ok(status == 200, "request failed with status %u\n", status);
1731 InternetCloseHandle(req);
1733 req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
1734 ok(req != NULL, "HttpOpenRequest failed\n");
1736 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1737 ok(ret, "HttpSendRequest failed\n");
1739 status = 0;
1740 size = sizeof(status);
1741 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1742 ok(ret, "HttpQueryInfo failed\n");
1743 ok(status == 200, "request failed with status %u\n", status);
1745 InternetCloseHandle(req);
1747 req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1748 ok(req != NULL, "HttpOpenRequest failed\n");
1750 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1751 ok(ret, "HttpSendRequest failed\n");
1753 status = 0;
1754 size = sizeof(status);
1755 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1756 ok(ret, "HttpQueryInfo failed\n");
1757 ok(status == 200, "request failed with status %u\n", status);
1759 InternetCloseHandle(req);
1761 req = HttpOpenRequest(con, "POST", "/testA", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1762 ok(req != NULL, "HttpOpenRequest failed\n");
1764 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1765 ok(ret, "HttpSendRequest failed\n");
1767 status = 0;
1768 size = sizeof(status);
1769 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1770 ok(ret, "HttpQueryInfo failed\n");
1771 ok(status == 200, "request failed with status %u\n", status);
1773 InternetCloseHandle(req);
1774 InternetCloseHandle(con);
1775 InternetCloseHandle(ses);
1778 static void test_http1_1(int port)
1780 HINTERNET ses, con, req;
1781 BOOL ret;
1783 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1784 ok(ses != NULL, "InternetOpen failed\n");
1786 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1787 ok(con != NULL, "InternetConnect failed\n");
1789 req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1790 ok(req != NULL, "HttpOpenRequest failed\n");
1792 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1793 if (ret)
1795 InternetCloseHandle(req);
1797 req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1798 ok(req != NULL, "HttpOpenRequest failed\n");
1800 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1801 todo_wine
1802 ok(ret, "HttpSendRequest failed\n");
1805 InternetCloseHandle(req);
1806 InternetCloseHandle(con);
1807 InternetCloseHandle(ses);
1810 static void test_cookie_header(int port)
1812 HINTERNET ses, con, req;
1813 DWORD size, status, error;
1814 BOOL ret;
1815 char buffer[64];
1817 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1818 ok(ses != NULL, "InternetOpen failed\n");
1820 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1821 ok(con != NULL, "InternetConnect failed\n");
1823 InternetSetCookie("http://localhost", "cookie", "biscuit");
1825 req = HttpOpenRequest(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1826 ok(req != NULL, "HttpOpenRequest failed\n");
1828 buffer[0] = 0;
1829 size = sizeof(buffer);
1830 SetLastError(0xdeadbeef);
1831 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1832 error = GetLastError();
1833 ok(!ret, "HttpQueryInfo succeeded\n");
1834 ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %u expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
1836 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1837 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1839 status = 0;
1840 size = sizeof(status);
1841 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1842 ok(ret, "HttpQueryInfo failed\n");
1843 ok(status == 200, "request failed with status %u\n", status);
1845 buffer[0] = 0;
1846 size = sizeof(buffer);
1847 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1848 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
1849 ok(!strcmp(buffer, "cookie=biscuit"), "got '%s' expected \'cookie=biscuit\'\n", buffer);
1851 InternetCloseHandle(req);
1852 InternetCloseHandle(con);
1853 InternetCloseHandle(ses);
1856 static void test_basic_authentication(int port)
1858 HINTERNET session, connect, request;
1859 DWORD size, status;
1860 BOOL ret;
1862 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1863 ok(session != NULL, "InternetOpen failed\n");
1865 connect = InternetConnect(session, "localhost", port, "user", "pwd", INTERNET_SERVICE_HTTP, 0, 0);
1866 ok(connect != NULL, "InternetConnect failed\n");
1868 request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, NULL, 0, 0);
1869 ok(request != NULL, "HttpOpenRequest failed\n");
1871 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1872 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
1874 status = 0;
1875 size = sizeof(status);
1876 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1877 ok(ret, "HttpQueryInfo failed\n");
1878 ok(status == 200, "request failed with status %u\n", status);
1880 InternetCloseHandle(request);
1881 InternetCloseHandle(connect);
1882 InternetCloseHandle(session);
1885 static void test_HttpQueryInfo(int port)
1887 HINTERNET hi, hc, hr;
1888 DWORD size, index;
1889 char buffer[1024];
1890 BOOL ret;
1892 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1893 ok(hi != NULL, "InternetOpen failed\n");
1895 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1896 ok(hc != NULL, "InternetConnect failed\n");
1898 hr = HttpOpenRequest(hc, NULL, "/testD", NULL, NULL, NULL, 0, 0);
1899 ok(hr != NULL, "HttpOpenRequest failed\n");
1901 ret = HttpSendRequest(hr, NULL, 0, NULL, 0);
1902 ok(ret, "HttpSendRequest failed\n");
1904 index = 0;
1905 size = sizeof(buffer);
1906 ret = HttpQueryInfo(hr, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &index);
1907 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
1908 ok(index == 1, "expected 1 got %u\n", index);
1910 index = 0;
1911 size = sizeof(buffer);
1912 ret = HttpQueryInfo(hr, HTTP_QUERY_DATE | HTTP_QUERY_FLAG_SYSTEMTIME, buffer, &size, &index);
1913 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
1914 ok(index == 1, "expected 1 got %u\n", index);
1916 index = 0;
1917 size = sizeof(buffer);
1918 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
1919 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
1920 ok(index == 0, "expected 0 got %u\n", index);
1922 size = sizeof(buffer);
1923 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
1924 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
1925 ok(index == 0, "expected 0 got %u\n", index);
1927 size = sizeof(buffer);
1928 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &size, &index);
1929 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
1930 ok(index == 0, "expected 0 got %u\n", index);
1932 size = sizeof(buffer);
1933 ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
1934 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
1935 ok(index == 0, "expected 0 got %u\n", index);
1937 size = sizeof(buffer);
1938 ret = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &size, &index);
1939 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
1940 ok(index == 0, "expected 0 got %u\n", index);
1942 index = 0;
1943 size = sizeof(buffer);
1944 ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &size, &index);
1945 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
1946 ok(index == 0, "expected 0 got %u\n", index);
1948 index = 0;
1949 size = sizeof(buffer);
1950 ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, buffer, &size, &index);
1951 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
1952 ok(index == 0, "expected 0 got %u\n", index);
1954 index = 0xdeadbeef;
1955 size = sizeof(buffer);
1956 ret = HttpQueryInfo(hr, HTTP_QUERY_FORWARDED, buffer, &size, &index);
1957 ok(!ret, "HttpQueryInfo succeeded\n");
1958 ok(index == 0xdeadbeef, "expected 0xdeadbeef got %u\n", index);
1960 index = 0;
1961 size = sizeof(buffer);
1962 ret = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &size, &index);
1963 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
1964 ok(index == 1, "expected 1 got %u\n", index);
1966 index = 0;
1967 size = sizeof(buffer);
1968 strcpy(buffer, "Server");
1969 ret = HttpQueryInfo(hr, HTTP_QUERY_CUSTOM, buffer, &size, &index);
1970 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
1971 ok(index == 1, "expected 1 got %u\n", index);
1973 index = 0;
1974 size = sizeof(buffer);
1975 ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
1976 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
1977 ok(index == 1, "expected 1 got %u\n", index);
1979 size = sizeof(buffer);
1980 ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
1981 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
1982 ok(index == 2, "expected 2 got %u\n", index);
1984 InternetCloseHandle(hr);
1985 InternetCloseHandle(hc);
1986 InternetCloseHandle(hi);
1989 static void test_http_connection(void)
1991 struct server_info si;
1992 HANDLE hThread;
1993 DWORD id = 0, r;
1995 si.hEvent = CreateEvent(NULL, 0, 0, NULL);
1996 si.port = 7531;
1998 hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
1999 ok( hThread != NULL, "create thread failed\n");
2001 r = WaitForSingleObject(si.hEvent, 10000);
2002 ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
2003 if (r != WAIT_OBJECT_0)
2004 return;
2006 test_basic_request(si.port, "GET", "/test1");
2007 test_proxy_indirect(si.port);
2008 test_proxy_direct(si.port);
2009 test_header_handling_order(si.port);
2010 test_basic_request(si.port, "POST", "/test5");
2011 test_basic_request(si.port, "GET", "/test6");
2012 test_connection_header(si.port);
2013 test_http1_1(si.port);
2014 test_cookie_header(si.port);
2015 test_basic_authentication(si.port);
2016 test_HttpQueryInfo(si.port);
2018 /* send the basic request again to shutdown the server thread */
2019 test_basic_request(si.port, "GET", "/quit");
2021 r = WaitForSingleObject(hThread, 3000);
2022 ok( r == WAIT_OBJECT_0, "thread wait failed\n");
2023 CloseHandle(hThread);
2026 static void test_user_agent_header(void)
2028 HINTERNET ses, con, req;
2029 DWORD size, err;
2030 char buffer[64];
2031 BOOL ret;
2033 ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2034 ok(ses != NULL, "InternetOpen failed\n");
2036 con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2037 ok(con != NULL, "InternetConnect failed\n");
2039 req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
2040 ok(req != NULL, "HttpOpenRequest failed\n");
2042 size = sizeof(buffer);
2043 ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2044 err = GetLastError();
2045 ok(!ret, "HttpQueryInfo succeeded\n");
2046 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2048 ret = HttpAddRequestHeaders(req, "User-Agent: Gizmo Project\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2049 ok(ret, "HttpAddRequestHeaders succeeded\n");
2051 size = sizeof(buffer);
2052 ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2053 err = GetLastError();
2054 ok(ret, "HttpQueryInfo failed\n");
2055 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2057 InternetCloseHandle(req);
2059 req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
2060 ok(req != NULL, "HttpOpenRequest failed\n");
2062 size = sizeof(buffer);
2063 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2064 err = GetLastError();
2065 ok(!ret, "HttpQueryInfo succeeded\n");
2066 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2068 ret = HttpAddRequestHeaders(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2069 ok(ret, "HttpAddRequestHeaders failed\n");
2071 buffer[0] = 0;
2072 size = sizeof(buffer);
2073 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2074 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2075 ok(!strcmp(buffer, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
2077 InternetCloseHandle(req);
2078 InternetCloseHandle(con);
2079 InternetCloseHandle(ses);
2082 static void test_bogus_accept_types_array(void)
2084 HINTERNET ses, con, req;
2085 static const char *types[] = { (const char *)6240, "*/*", "%p", "", (const char *)0xffffffff, "*/*", NULL };
2086 DWORD size;
2087 char buffer[32];
2088 BOOL ret;
2090 ses = InternetOpen("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
2091 con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2092 req = HttpOpenRequest(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
2094 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
2096 buffer[0] = 0;
2097 size = sizeof(buffer);
2098 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2099 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2100 ok(!strcmp(buffer, ", */*, %p, , , */*") || /* IE6 */
2101 !strcmp(buffer, "*/*, %p, */*"),
2102 "got '%s' expected '*/*, %%p, */*' or ', */*, %%p, , , */*'\n", buffer);
2104 InternetCloseHandle(req);
2105 InternetCloseHandle(con);
2106 InternetCloseHandle(ses);
2109 struct context
2111 HANDLE event;
2112 HINTERNET req;
2115 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
2117 INTERNET_ASYNC_RESULT *result = info;
2118 struct context *ctx = (struct context *)context;
2120 trace("%p 0x%08lx %u %p 0x%08x\n", handle, context, status, info, size);
2122 if (status == INTERNET_STATUS_REQUEST_COMPLETE)
2124 trace("request handle: 0x%08lx\n", result->dwResult);
2125 ctx->req = (HINTERNET)result->dwResult;
2126 SetEvent(ctx->event);
2128 if (status == INTERNET_STATUS_HANDLE_CLOSING)
2130 DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
2132 if (InternetQueryOption(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
2133 ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
2134 SetEvent(ctx->event);
2138 static void test_open_url_async(void)
2140 BOOL ret;
2141 HINTERNET ses, req;
2142 DWORD size, error;
2143 struct context ctx;
2144 ULONG type;
2146 ctx.req = NULL;
2147 ctx.event = CreateEvent(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
2149 ses = InternetOpen("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
2150 ok(ses != NULL, "InternetOpen failed\n");
2152 SetLastError(0xdeadbeef);
2153 ret = InternetSetOptionA(NULL, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
2154 error = GetLastError();
2155 ok(!ret, "InternetSetOptionA succeeded\n");
2156 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "got %u expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE\n", error);
2158 ret = InternetSetOptionA(ses, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
2159 error = GetLastError();
2160 ok(!ret, "InternetSetOptionA failed\n");
2161 ok(error == ERROR_INTERNET_OPTION_NOT_SETTABLE, "got %u expected ERROR_INTERNET_OPTION_NOT_SETTABLE\n", error);
2163 pInternetSetStatusCallbackA(ses, cb);
2164 ResetEvent(ctx.event);
2166 req = InternetOpenUrl(ses, "http://www.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
2167 ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
2169 WaitForSingleObject(ctx.event, INFINITE);
2171 type = 0;
2172 size = sizeof(type);
2173 ret = InternetQueryOption(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
2174 ok(ret, "InternetQueryOption failed: %u\n", GetLastError());
2175 ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
2176 "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %u\n", type);
2178 size = 0;
2179 ret = HttpQueryInfo(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
2180 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
2181 ok(size > 0, "expected size > 0\n");
2183 ResetEvent(ctx.event);
2184 InternetCloseHandle(ctx.req);
2185 WaitForSingleObject(ctx.event, INFINITE);
2187 InternetCloseHandle(ses);
2188 CloseHandle(ctx.event);
2191 #define STATUS_STRING(status) \
2192 memcpy(status_string[status], #status, sizeof(CHAR) * \
2193 (strlen(#status) < MAX_STATUS_NAME ? \
2194 strlen(#status) : \
2195 MAX_STATUS_NAME - 1))
2196 static void init_status_tests(void)
2198 memset(expect, 0, sizeof(expect));
2199 memset(optional, 0, sizeof(optional));
2200 memset(wine_allow, 0, sizeof(wine_allow));
2201 memset(notified, 0, sizeof(notified));
2202 memset(status_string, 0, sizeof(status_string));
2203 STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
2204 STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
2205 STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
2206 STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
2207 STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
2208 STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
2209 STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
2210 STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
2211 STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
2212 STATUS_STRING(INTERNET_STATUS_PREFETCH);
2213 STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
2214 STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
2215 STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
2216 STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
2217 STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
2218 STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
2219 STATUS_STRING(INTERNET_STATUS_REDIRECT);
2220 STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
2221 STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
2222 STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
2223 STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
2224 STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
2225 STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
2226 STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
2227 STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
2228 STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
2230 #undef STATUS_STRING
2232 START_TEST(http)
2234 HMODULE hdll;
2235 hdll = GetModuleHandleA("wininet.dll");
2236 pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
2238 if (!pInternetSetStatusCallbackA)
2239 skip("skipping the InternetReadFile tests\n");
2240 else
2242 init_status_tests();
2243 InternetReadFile_test(INTERNET_FLAG_ASYNC);
2244 InternetReadFile_test(0);
2245 InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
2246 test_open_url_async();
2248 InternetOpenRequest_test();
2249 test_http_cache();
2250 InternetOpenUrlA_test();
2251 HttpSendRequestEx_test();
2252 HttpHeaders_test();
2253 test_http_connection();
2254 test_user_agent_header();
2255 test_bogus_accept_types_array();