wnaspi32: Make winaspi.dll into a stand-alone 16-bit module.
[wine] / dlls / wininet / tests / http.c
1 /*
2  * Wininet - Http tests
3  *
4  * Copyright 2002 Aric Stewart
5  * Copyright 2004 Mike McCormack
6  * Copyright 2005 Hans Leidekker
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wininet.h"
30 #include "winsock.h"
31
32 #include "wine/test.h"
33
34 #define TEST_URL "http://test.winehq.org/hello.html"
35
36 static BOOL first_connection_to_test_url = TRUE;
37
38 /* Adapted from dlls/urlmon/tests/protocol.c */
39
40 #define SET_EXPECT2(status, num) \
41     expect[status] = num
42
43 #define SET_EXPECT(status) \
44     SET_EXPECT2(status, 1)
45
46 #define SET_OPTIONAL2(status, num) \
47     optional[status] = num
48
49 #define SET_OPTIONAL(status) \
50     SET_OPTIONAL2(status, 1)
51
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
56
57 #define SET_WINE_ALLOW(status) \
58     SET_WINE_ALLOW2(status, 1)
59
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)
79
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;
84
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)
92
93 #define CHECK_NOTIFIED(status) \
94     CHECK_NOTIFIED2(status, 1)
95
96 #define CHECK_NOT_NOTIFIED(status) \
97     CHECK_NOTIFIED2(status, 0)
98
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];
104
105 static HANDLE hCompleteEvent;
106
107 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
108
109
110 static VOID WINAPI callback(
111      HINTERNET hInternet,
112      DWORD_PTR dwContext,
113      DWORD dwInternetStatus,
114      LPVOID lpvStatusInformation,
115      DWORD dwStatusInformationLength
116 )
117 {
118     CHECK_EXPECT(dwInternetStatus);
119     switch (dwInternetStatus)
120     {
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:
210         {
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;
220         }
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);
238     }
239 }
240
241 static void InternetReadFile_test(int flags)
242 {
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;
249
250     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
251
252     trace("Starting InternetReadFile test with flags 0x%x\n",flags);
253
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");
258
259     if (hi == 0x0) goto abort;
260
261     pInternetSetStatusCallbackA(hi,&callback);
262
263     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
264
265     trace("InternetConnectA <--\n");
266     hic=InternetConnectA(hi, "test.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");
270
271     if (hic == 0x0) goto abort;
272
273     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
274     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
275
276     trace("HttpOpenRequestA <--\n");
277     hor = HttpOpenRequestA(hic, "GET", "/testredirect", NULL, NULL, types,
278                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
279                            0xdeadbead);
280     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
281         /*
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.
286          */
287     } else  {
288         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
289     }
290     trace("HttpOpenRequestA -->\n");
291
292     if (hor == 0x0) goto abort;
293
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://test.winehq.org/testredirect"), "Wrong URL %s\n", buffer);
298
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);
304
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)
309     {
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);
314     }
315     else
316     {
317         SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
318         SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
319     }
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_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
329     SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
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);
337
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");
348
349     if (flags & INTERNET_FLAG_ASYNC)
350         WaitForSingleObject(hCompleteEvent, INFINITE);
351
352     todo_wine if (first_connection_to_test_url)
353     {
354         CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
355         CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
356     }
357     else
358     {
359         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
360         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
361     }
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         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);
374
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());
378
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());
382
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;
387
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://test.winehq.org/hello.html"), "Wrong URL %s\n", buffer);
392
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());
396
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);
401
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());
408
409     length = 100;
410     trace("Entering Query loop\n");
411
412     while (TRUE)
413     {
414         if (flags & INTERNET_FLAG_ASYNC)
415             SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
416         res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
417         ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
418         ok(res || ((flags & INTERNET_FLAG_ASYNC) && GetLastError() == ERROR_IO_PENDING),
419            "InternetQueryDataAvailable failed, error %d\n", GetLastError());
420         if (flags & INTERNET_FLAG_ASYNC)
421         {
422             if (res)
423             {
424                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
425             }
426             else if (GetLastError() == ERROR_IO_PENDING)
427             {
428                 WaitForSingleObject(hCompleteEvent, INFINITE);
429                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
430                 continue;
431             }
432         }
433         if (length)
434         {
435             char *buffer;
436             buffer = HeapAlloc(GetProcessHeap(),0,length+1);
437
438             res = InternetReadFile(hor,buffer,length,&length);
439
440             buffer[length]=0;
441
442             trace("ReadFile -> %s %i\n",res?"TRUE":"FALSE",length);
443
444             HeapFree(GetProcessHeap(),0,buffer);
445         }
446         if (length == 0)
447             break;
448     }
449     todo_wine CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
450     todo_wine CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
451 abort:
452     trace("aborting\n");
453     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
454     if (hor != 0x0) {
455         SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
456         SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
457         SetLastError(0xdeadbeef);
458         trace("closing\n");
459         res = InternetCloseHandle(hor);
460         ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
461         SetLastError(0xdeadbeef);
462         res = InternetCloseHandle(hor);
463         ok (!res, "Double close of handle opened by HttpOpenRequestA succeeded\n");
464         ok (GetLastError() == ERROR_INVALID_HANDLE,
465             "Double close of handle should have set ERROR_INVALID_HANDLE instead of %u\n",
466             GetLastError());
467     }
468     /* We intentionally do not close the handle opened by InternetConnectA as this
469      * tickles bug #9479: native closes child internet handles when the parent handles
470      * are closed. This is verified below by checking that the number of
471      * INTERNET_STATUS_HANDLE_CLOSING notifications matches the number expected. */
472     if (hi != 0x0) {
473       SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
474         trace("closing 2\n");
475       res = InternetCloseHandle(hi);
476       ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
477       if (flags & INTERNET_FLAG_ASYNC)
478           Sleep(100);
479     }
480     CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
481     if (hor != 0x0) todo_wine
482     {
483         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
484         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
485     }
486     else
487     {
488         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
489         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
490     }
491     CloseHandle(hCompleteEvent);
492     first_connection_to_test_url = FALSE;
493 }
494
495 static void InternetReadFileExA_test(int flags)
496 {
497     DWORD rc;
498     DWORD length;
499     const char *types[2] = { "*", NULL };
500     HINTERNET hi, hic = 0, hor = 0;
501     INTERNET_BUFFERS inetbuffers;
502
503     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
504
505     trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
506
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");
511
512     if (hi == 0x0) goto abort;
513
514     pInternetSetStatusCallbackA(hi,&callback);
515
516     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
517
518     trace("InternetConnectA <--\n");
519     hic=InternetConnectA(hi, "test.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");
523
524     if (hic == 0x0) goto abort;
525
526     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
527     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
528
529     trace("HttpOpenRequestA <--\n");
530     hor = HttpOpenRequestA(hic, "GET", "/testredirect", NULL, NULL, types,
531                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
532                            0xdeadbead);
533     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
534         /*
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.
539          */
540     } else  {
541         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
542     }
543     trace("HttpOpenRequestA -->\n");
544
545     if (hor == 0x0) goto abort;
546
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)
551     {
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);
556     }
557     else
558     {
559         SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
560         SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
561     }
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_EXPECT2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
571     SET_EXPECT2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
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);
579
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");
590
591     if (!rc && (GetLastError() == ERROR_IO_PENDING))
592         WaitForSingleObject(hCompleteEvent, INFINITE);
593
594     if (first_connection_to_test_url)
595     {
596         CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
597         CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
598     }
599     else todo_wine
600     {
601         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
602         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
603     }
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     todo_wine CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
609     todo_wine CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
610     CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
611     if (flags & INTERNET_FLAG_ASYNC)
612         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
613     else
614         todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
615     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
616     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
617     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
618
619     /* tests invalid dwStructSize */
620     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
621     inetbuffers.lpcszHeader = NULL;
622     inetbuffers.dwHeadersLength = 0;
623     inetbuffers.dwBufferLength = 10;
624     inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
625     inetbuffers.dwOffsetHigh = 1234;
626     inetbuffers.dwOffsetLow = 5678;
627     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
628     ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
629         "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
630         rc ? "TRUE" : "FALSE", GetLastError());
631     HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
632
633     /* tests to see whether lpcszHeader is used - it isn't */
634     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
635     inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
636     inetbuffers.dwHeadersLength = 255;
637     inetbuffers.dwBufferLength = 0;
638     inetbuffers.lpvBuffer = NULL;
639     inetbuffers.dwOffsetHigh = 1234;
640     inetbuffers.dwOffsetLow = 5678;
641     SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
642     SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
643     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
644     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
645     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
646     ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
647         trace("read %i bytes\n", inetbuffers.dwBufferLength);
648     todo_wine
649     {
650         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
651         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
652     }
653
654     rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
655     ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
656         "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
657         rc ? "TRUE" : "FALSE", GetLastError());
658
659     length = 0;
660     trace("Entering Query loop\n");
661
662     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
663     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
664     while (TRUE)
665     {
666         inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
667         inetbuffers.dwBufferLength = 1024;
668         inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
669         inetbuffers.dwOffsetHigh = 1234;
670         inetbuffers.dwOffsetLow = 5678;
671
672         SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
673         SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
674         SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
675         SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
676         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
677         rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
678         if (!rc)
679         {
680             if (GetLastError() == ERROR_IO_PENDING)
681             {
682                 trace("InternetReadFileEx -> PENDING\n");
683                 ok(flags & INTERNET_FLAG_ASYNC,
684                    "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
685                 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
686                 WaitForSingleObject(hCompleteEvent, INFINITE);
687                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
688                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
689             }
690             else
691             {
692                 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
693                 break;
694             }
695         }
696         else
697         {
698             trace("InternetReadFileEx -> SUCCEEDED\n");
699             CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
700             if (inetbuffers.dwBufferLength)
701             {
702                 todo_wine {
703                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
704                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
705                 }
706             }
707             else
708             {
709                 /* Win98 still sends these when 0 bytes are read, WinXP does not */
710                 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
711                 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
712             }
713         }
714
715         trace("read %i bytes\n", inetbuffers.dwBufferLength);
716         ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
717
718         ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
719             "InternetReadFileEx sets offsets to 0x%x%08x\n",
720             inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
721
722         HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
723
724         if (!inetbuffers.dwBufferLength)
725             break;
726
727         length += inetbuffers.dwBufferLength;
728     }
729     ok(length > 0, "failed to read any of the document\n");
730     trace("Finished. Read %d bytes\n", length);
731
732     /* WinXP does not send, but Win98 does */
733     CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
734     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
735 abort:
736     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
737     if (hor) {
738         SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
739         SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
740         rc = InternetCloseHandle(hor);
741         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
742         rc = InternetCloseHandle(hor);
743         ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
744     }
745     if (hic) {
746         rc = InternetCloseHandle(hic);
747         ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
748     }
749     if (hi) {
750       SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
751       rc = InternetCloseHandle(hi);
752       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
753       if (flags & INTERNET_FLAG_ASYNC)
754           Sleep(100);
755       CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
756     }
757     CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
758     CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
759     CloseHandle(hCompleteEvent);
760     first_connection_to_test_url = FALSE;
761 }
762
763 static void InternetOpenUrlA_test(void)
764 {
765   HINTERNET myhinternet, myhttp;
766   char buffer[0x400];
767   DWORD size, readbytes, totalbytes=0;
768   BOOL ret;
769   
770   myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
771   ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
772   size = 0x400;
773   ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
774   ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
775
776   SetLastError(0);
777   myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
778                            INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
779   if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
780     return; /* WinXP returns this when not connected to the net */
781   ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
782   ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
783   ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
784   totalbytes += readbytes;
785   while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
786     totalbytes += readbytes;
787   trace("read 0x%08x bytes\n",totalbytes);
788
789   InternetCloseHandle(myhttp);
790   InternetCloseHandle(myhinternet);
791 }
792
793 static void HttpSendRequestEx_test(void)
794 {
795     HINTERNET hSession;
796     HINTERNET hConnect;
797     HINTERNET hRequest;
798
799     INTERNET_BUFFERS BufferIn;
800     DWORD dwBytesWritten;
801     DWORD dwBytesRead;
802     CHAR szBuffer[256];
803     int i;
804     BOOL ret;
805
806     static char szPostData[] = "mode=Test";
807     static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
808
809     hSession = InternetOpen("Wine Regression Test",
810             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
811     ok( hSession != NULL ,"Unable to open Internet session\n");
812     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
813             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
814             0);
815     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
816     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
817             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
818     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
819     {
820         skip( "Network unreachable, skipping test\n" );
821         goto done;
822     }
823     ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
824
825
826     BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
827     BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
828     BufferIn.lpcszHeader = szContentType;
829     BufferIn.dwHeadersLength = sizeof(szContentType)-1;
830     BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
831     BufferIn.lpvBuffer = szPostData;
832     BufferIn.dwBufferLength = 3;
833     BufferIn.dwBufferTotal = sizeof(szPostData)-1;
834     BufferIn.dwOffsetLow = 0;
835     BufferIn.dwOffsetHigh = 0;
836
837     ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
838     ok(ret, "HttpSendRequestEx Failed with error %u\n", GetLastError());
839
840     for (i = 3; szPostData[i]; i++)
841         ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
842                 "InternetWriteFile failed\n");
843
844     ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
845
846     ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
847             "Unable to read response\n");
848     szBuffer[dwBytesRead] = 0;
849
850     ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
851     ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0,"Got string %s\n",szBuffer);
852
853     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
854 done:
855     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
856     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
857 }
858
859 static void InternetOpenRequest_test(void)
860 {
861     HINTERNET session, connect, request;
862     static const char *types[] = { "*", "", NULL };
863     static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
864     static const WCHAR *typesW[] = { any, empty, NULL };
865     BOOL ret;
866
867     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
868     ok(session != NULL ,"Unable to open Internet session\n");
869
870     connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
871                               INTERNET_SERVICE_HTTP, 0, 0);
872     ok(connect == NULL, "InternetConnectA should have failed\n");
873     ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
874
875     connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
876                               INTERNET_SERVICE_HTTP, 0, 0);
877     ok(connect == NULL, "InternetConnectA should have failed\n");
878     ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
879
880     connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
881                               INTERNET_SERVICE_HTTP, 0, 0);
882     ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
883
884     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
885     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
886     {
887         skip( "Network unreachable, skipping test\n" );
888         goto done;
889     }
890     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
891
892     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
893     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
894     ok(InternetCloseHandle(request), "Close request handle failed\n");
895
896     request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
897     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
898
899     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
900     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
901     ok(InternetCloseHandle(request), "Close request handle failed\n");
902
903 done:
904     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
905     ok(InternetCloseHandle(session), "Close session handle failed\n");
906 }
907
908 static void test_http_cache(void)
909 {
910     HINTERNET session, connect, request;
911     char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
912     DWORD size, file_size;
913     BYTE buf[100];
914     HANDLE file;
915     BOOL ret;
916
917     static const char *types[] = { "*", "", NULL };
918
919     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
920     ok(session != NULL ,"Unable to open Internet session\n");
921
922     connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
923                               INTERNET_SERVICE_HTTP, 0, 0);
924     ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
925
926     request = HttpOpenRequestA(connect, NULL, "/hello.html", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
927     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
928     {
929         skip( "Network unreachable, skipping test\n" );
930
931         ok(InternetCloseHandle(connect), "Close connect handle failed\n");
932         ok(InternetCloseHandle(session), "Close session handle failed\n");
933
934         return;
935     }
936     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
937
938     size = sizeof(url);
939     ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
940     ok(ret, "InternetQueryOptionA(INTERNET_OPTION_url) failed: %u\n", GetLastError());
941     ok(!strcmp(url, "http://test.winehq.org/hello.html"), "Wrong URL %s\n", url);
942
943     size = sizeof(file_name);
944     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
945     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
946     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
947     ok(!size, "size = %d\n", size);
948
949     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
950     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
951
952     size = sizeof(file_name);
953     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
954     ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
955
956     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
957                       FILE_ATTRIBUTE_NORMAL, NULL);
958     ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
959     file_size = GetFileSize(file, NULL);
960     todo_wine ok(file_size == 106, "file size = %u\n", file_size);
961
962     size = sizeof(buf);
963     ret = InternetReadFile(request, buf, sizeof(buf), &size);
964     ok(ret, "InternetReadFile failed: %u\n", GetLastError());
965     ok(size == 100, "size = %u\n", size);
966
967     file_size = GetFileSize(file, NULL);
968     todo_wine ok(file_size == 106, "file size = %u\n", file_size);
969     CloseHandle(file);
970
971     ok(InternetCloseHandle(request), "Close request handle failed\n");
972
973     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
974                       FILE_ATTRIBUTE_NORMAL, NULL);
975     todo_wine ok(file != INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
976     CloseHandle(file);
977
978     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
979     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
980
981     size = sizeof(file_name);
982     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
983     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
984     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
985     ok(!size, "size = %d\n", size);
986
987     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
988     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
989
990     size = sizeof(file_name);
991     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
992     todo_wine ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed %u\n", GetLastError());
993
994     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
995                       FILE_ATTRIBUTE_NORMAL, NULL);
996     todo_wine ok(file != INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
997     CloseHandle(file);
998
999     ok(InternetCloseHandle(request), "Close request handle failed\n");
1000     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1001     ok(InternetCloseHandle(session), "Close session handle failed\n");
1002 }
1003
1004 static void HttpHeaders_test(void)
1005 {
1006     HINTERNET hSession;
1007     HINTERNET hConnect;
1008     HINTERNET hRequest;
1009     CHAR      buffer[256];
1010     WCHAR     wbuffer[256];
1011     DWORD     len = 256;
1012     DWORD     oldlen;
1013     DWORD     index = 0;
1014
1015     hSession = InternetOpen("Wine Regression Test",
1016             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1017     ok( hSession != NULL ,"Unable to open Internet session\n");
1018     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1019             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1020             0);
1021     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1022     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1023             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1024     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1025     {
1026         skip( "Network unreachable, skipping test\n" );
1027         goto done;
1028     }
1029     ok( hRequest != NULL, "Failed to open request handle\n");
1030
1031     index = 0;
1032     len = sizeof(buffer);
1033     strcpy(buffer,"Warning");
1034     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1035                buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1036     
1037     ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1038             "Failed to add new header\n");
1039
1040     index = 0;
1041     len = sizeof(buffer);
1042     strcpy(buffer,"Warning");
1043     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1044                 buffer,&len,&index),"Unable to query header\n");
1045     ok(index == 1, "Index was not incremented\n");
1046     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1047     ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1048     ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1049     len = sizeof(buffer);
1050     strcpy(buffer,"Warning");
1051     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1052                 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1053
1054     index = 0;
1055     len = 5; /* could store the string but not the NULL terminator */
1056     strcpy(buffer,"Warning");
1057     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1058                 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1059     ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1060     ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1061
1062     /* a call with NULL will fail but will return the length */
1063     index = 0;
1064     len = sizeof(buffer);
1065     SetLastError(0xdeadbeef);
1066     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1067                 NULL,&len,&index) == FALSE,"Query worked\n");
1068     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1069     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1070     ok(index == 0, "Index was incremented\n");
1071
1072     /* even for a len that is too small */
1073     index = 0;
1074     len = 15;
1075     SetLastError(0xdeadbeef);
1076     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1077                 NULL,&len,&index) == FALSE,"Query worked\n");
1078     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1079     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1080     ok(index == 0, "Index was incremented\n");
1081
1082     index = 0;
1083     len = 0;
1084     SetLastError(0xdeadbeef);
1085     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1086                 NULL,&len,&index) == FALSE,"Query worked\n");
1087     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1088     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1089     ok(index == 0, "Index was incremented\n");
1090     oldlen = len;   /* bytes; at least long enough to hold buffer & nul */
1091
1092
1093     /* a working query */
1094     index = 0;
1095     len = sizeof(buffer);
1096     memset(buffer, 'x', sizeof(buffer));
1097     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1098                 buffer,&len,&index),"Unable to query header\n");
1099     ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1100     ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1101     ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1102     /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1103     ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1104     ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1105     ok(index == 0, "Index was incremented\n");
1106
1107     /* Like above two tests, but for W version */
1108
1109     index = 0;
1110     len = 0;
1111     SetLastError(0xdeadbeef);
1112     ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1113                 NULL,&len,&index) == FALSE,"Query worked\n");
1114     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1115     ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1116     ok(index == 0, "Index was incremented\n");
1117     oldlen = len;   /* bytes; at least long enough to hold buffer & nul */
1118
1119     /* a working query */
1120     index = 0;
1121     len = sizeof(wbuffer);
1122     memset(wbuffer, 'x', sizeof(wbuffer));
1123     ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1124                 wbuffer,&len,&index),"Unable to query header\n");
1125     ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1126     ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1127     ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1128     ok(index == 0, "Index was incremented\n");
1129
1130     /* end of W version tests */
1131
1132     /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1133     index = 0;
1134     len = sizeof(buffer);
1135     memset(buffer, 'x', sizeof(buffer));
1136     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF,
1137                 buffer,&len,&index) == TRUE,"Query failed\n");
1138     ok(len == 2, "Expected 2, got %d\n", len);
1139     ok(strcmp(buffer, "\r\n") == 0, "Expected CRLF, got '%s'\n", buffer);
1140     ok(index == 0, "Index was incremented\n");
1141
1142     ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1143             "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1144
1145     index = 0;
1146     len = sizeof(buffer);
1147     strcpy(buffer,"Warning");
1148     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1149                 buffer,&len,&index),"Unable to query header\n");
1150     ok(index == 1, "Index was not incremented\n");
1151     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1152     len = sizeof(buffer);
1153     strcpy(buffer,"Warning");
1154     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1155                 buffer,&len,&index),"Failed to get second header\n");
1156     ok(index == 2, "Index was not incremented\n");
1157     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1158     len = sizeof(buffer);
1159     strcpy(buffer,"Warning");
1160     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1161                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1162
1163     ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1164
1165     index = 0;
1166     len = sizeof(buffer);
1167     strcpy(buffer,"Warning");
1168     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1169                 buffer,&len,&index),"Unable to query header\n");
1170     ok(index == 1, "Index was not incremented\n");
1171     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1172     len = sizeof(buffer);
1173     strcpy(buffer,"Warning");
1174     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1175                 buffer,&len,&index),"Failed to get second header\n");
1176     ok(index == 2, "Index was not incremented\n");
1177     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1178     len = sizeof(buffer);
1179     strcpy(buffer,"Warning");
1180     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1181                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1182     
1183     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1184
1185     index = 0;
1186     len = sizeof(buffer);
1187     strcpy(buffer,"Warning");
1188     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1189                 buffer,&len,&index),"Unable to query header\n");
1190     ok(index == 1, "Index was not incremented\n");
1191     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1192     len = sizeof(buffer);
1193     strcpy(buffer,"Warning");
1194     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1195                 buffer,&len,&index),"Failed to get second header\n");
1196     ok(index == 2, "Index was not incremented\n");
1197     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1198     len = sizeof(buffer);
1199     strcpy(buffer,"Warning");
1200     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1201                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1202
1203     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1204
1205     index = 0;
1206     len = sizeof(buffer);
1207     strcpy(buffer,"Warning");
1208     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1209                 buffer,&len,&index),"Unable to query header\n");
1210     ok(index == 1, "Index was not incremented\n");
1211     ok(strcmp(buffer,"test2, test4")==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),"Failed to get second header\n");
1215     ok(index == 2, "Index was not incremented\n");
1216     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1217     len = sizeof(buffer);
1218     strcpy(buffer,"Warning");
1219     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1220
1221     ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1222
1223     index = 0;
1224     len = sizeof(buffer);
1225     strcpy(buffer,"Warning");
1226     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1227     ok(index == 1, "Index was not incremented\n");
1228     ok(strcmp(buffer,"test2, test4, test5")==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),"Failed to get second header\n");
1232     ok(index == 2, "Index was not incremented\n");
1233     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1234     len = sizeof(buffer);
1235     strcpy(buffer,"Warning");
1236     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1237
1238     ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1239
1240     index = 0;
1241     len = sizeof(buffer);
1242     strcpy(buffer,"Warning");
1243     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1244     ok(index == 1, "Index was not incremented\n");
1245     ok(strcmp(buffer,"test2, test4, test5; test6")==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),"Failed to get second header\n");
1249     ok(index == 2, "Index was not incremented\n");
1250     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1251     len = sizeof(buffer);
1252     strcpy(buffer,"Warning");
1253     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1254
1255     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");
1256
1257     index = 0;
1258     len = sizeof(buffer);
1259     strcpy(buffer,"Warning");
1260     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1261     ok(index == 1, "Index was not incremented\n");
1262     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1263     len = sizeof(buffer);
1264     strcpy(buffer,"Warning");
1265     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1266     ok(index == 2, "Index was not incremented\n");
1267     ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1268     len = sizeof(buffer);
1269     strcpy(buffer,"Warning");
1270     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1271
1272     
1273     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1274 done:
1275     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1276     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1277 }
1278
1279 static const char contmsg[] =
1280 "HTTP/1.1 100 Continue\r\n";
1281
1282 static const char okmsg[] =
1283 "HTTP/1.1 200 OK\r\n"
1284 "Server: winetest\r\n"
1285 "\r\n";
1286
1287 static const char okmsg2[] =
1288 "HTTP/1.1 200 OK\r\n"
1289 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
1290 "Server: winetest\r\n"
1291 "Content-Length: 0\r\n"
1292 "Set-Cookie: one\r\n"
1293 "Set-Cookie: two\r\n"
1294 "\r\n";
1295
1296 static const char notokmsg[] =
1297 "HTTP/1.1 400 Bad Request\r\n"
1298 "Server: winetest\r\n"
1299 "\r\n";
1300
1301 static const char noauthmsg[] =
1302 "HTTP/1.1 401 Unauthorized\r\n"
1303 "Server: winetest\r\n"
1304 "Connection: close\r\n"
1305 "WWW-Authenticate: Basic realm=\"placebo\"\r\n"
1306 "\r\n";
1307
1308 static const char noauthmsg2[] =
1309 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed\r\n"
1310 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"
1311 "\0d`0|6\n"
1312 "Server: winetest\r\n";
1313
1314 static const char proxymsg[] =
1315 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1316 "Server: winetest\r\n"
1317 "Proxy-Connection: close\r\n"
1318 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1319 "\r\n";
1320
1321 static const char page1[] =
1322 "<HTML>\r\n"
1323 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1324 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1325 "</HTML>\r\n\r\n";
1326
1327 struct server_info {
1328     HANDLE hEvent;
1329     int port;
1330 };
1331
1332 static DWORD CALLBACK server_thread(LPVOID param)
1333 {
1334     struct server_info *si = param;
1335     int r, c, i, on;
1336     SOCKET s;
1337     struct sockaddr_in sa;
1338     char buffer[0x100];
1339     WSADATA wsaData;
1340     int last_request = 0;
1341     char host_header[22];
1342     static int test_b = 0;
1343
1344     WSAStartup(MAKEWORD(1,1), &wsaData);
1345
1346     s = socket(AF_INET, SOCK_STREAM, 0);
1347     if (s == INVALID_SOCKET)
1348         return 1;
1349
1350     on = 1;
1351     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1352
1353     memset(&sa, 0, sizeof sa);
1354     sa.sin_family = AF_INET;
1355     sa.sin_port = htons(si->port);
1356     sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1357
1358     r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1359     if (r<0)
1360         return 1;
1361
1362     listen(s, 0);
1363
1364     SetEvent(si->hEvent);
1365
1366     sprintf(host_header, "Host: localhost:%d", si->port);
1367
1368     do
1369     {
1370         c = accept(s, NULL, NULL);
1371
1372         memset(buffer, 0, sizeof buffer);
1373         for(i=0; i<(sizeof buffer-1); i++)
1374         {
1375             r = recv(c, &buffer[i], 1, 0);
1376             if (r != 1)
1377                 break;
1378             if (i<4) continue;
1379             if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
1380                 buffer[i-3] == '\r' && buffer[i-1] == '\r')
1381                 break;
1382         }
1383         if (strstr(buffer, "GET /test1"))
1384         {
1385             if (!strstr(buffer, "Content-Length: 0"))
1386             {
1387                 send(c, okmsg, sizeof okmsg-1, 0);
1388                 send(c, page1, sizeof page1-1, 0);
1389             }
1390             else
1391                 send(c, notokmsg, sizeof notokmsg-1, 0);
1392         }
1393         if (strstr(buffer, "/test2"))
1394         {
1395             if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
1396             {
1397                 send(c, okmsg, sizeof okmsg-1, 0);
1398                 send(c, page1, sizeof page1-1, 0);
1399             }
1400             else
1401                 send(c, proxymsg, sizeof proxymsg-1, 0);
1402         }
1403         if (strstr(buffer, "/test3"))
1404         {
1405             if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
1406                 send(c, okmsg, sizeof okmsg-1, 0);
1407             else
1408                 send(c, noauthmsg, sizeof noauthmsg-1, 0);
1409         }
1410         if (strstr(buffer, "/test4"))
1411         {
1412             if (strstr(buffer, "Connection: Close"))
1413                 send(c, okmsg, sizeof okmsg-1, 0);
1414             else
1415                 send(c, notokmsg, sizeof notokmsg-1, 0);
1416         }
1417         if (strstr(buffer, "POST /test5") ||
1418             strstr(buffer, "RPC_IN_DATA /test5") ||
1419             strstr(buffer, "RPC_OUT_DATA /test5"))
1420         {
1421             if (strstr(buffer, "Content-Length: 0"))
1422             {
1423                 send(c, okmsg, sizeof okmsg-1, 0);
1424                 send(c, page1, sizeof page1-1, 0);
1425             }
1426             else
1427                 send(c, notokmsg, sizeof notokmsg-1, 0);
1428         }
1429         if (strstr(buffer, "GET /test6"))
1430         {
1431             send(c, contmsg, sizeof contmsg-1, 0);
1432             send(c, contmsg, sizeof contmsg-1, 0);
1433             send(c, okmsg, sizeof okmsg-1, 0);
1434             send(c, page1, sizeof page1-1, 0);
1435         }
1436         if (strstr(buffer, "POST /test7"))
1437         {
1438             if (strstr(buffer, "Content-Length: 100"))
1439             {
1440                 send(c, okmsg, sizeof okmsg-1, 0);
1441                 send(c, page1, sizeof page1-1, 0);
1442             }
1443             else
1444                 send(c, notokmsg, sizeof notokmsg-1, 0);
1445         }
1446         if (strstr(buffer, "/test8"))
1447         {
1448             if (!strstr(buffer, "Connection: Close") &&
1449                  strstr(buffer, "Connection: Keep-Alive") &&
1450                 !strstr(buffer, "Cache-Control: no-cache") &&
1451                 !strstr(buffer, "Pragma: no-cache") &&
1452                  strstr(buffer, host_header))
1453                 send(c, okmsg, sizeof okmsg-1, 0);
1454             else
1455                 send(c, notokmsg, sizeof notokmsg-1, 0);
1456         }
1457         if (strstr(buffer, "/test9"))
1458         {
1459             if (!strstr(buffer, "Connection: Close") &&
1460                 !strstr(buffer, "Connection: Keep-Alive") &&
1461                 !strstr(buffer, "Cache-Control: no-cache") &&
1462                 !strstr(buffer, "Pragma: no-cache") &&
1463                  strstr(buffer, host_header))
1464                 send(c, okmsg, sizeof okmsg-1, 0);
1465             else
1466                 send(c, notokmsg, sizeof notokmsg-1, 0);
1467         }
1468         if (strstr(buffer, "/testA"))
1469         {
1470             if (!strstr(buffer, "Connection: Close") &&
1471                 !strstr(buffer, "Connection: Keep-Alive") &&
1472                 (strstr(buffer, "Cache-Control: no-cache") ||
1473                  strstr(buffer, "Pragma: no-cache")) &&
1474                  strstr(buffer, host_header))
1475                 send(c, okmsg, sizeof okmsg-1, 0);
1476             else
1477                 send(c, notokmsg, sizeof notokmsg-1, 0);
1478         }
1479         if (!test_b && strstr(buffer, "/testB HTTP/1.1"))
1480         {
1481             test_b = 1;
1482             send(c, okmsg, sizeof okmsg-1, 0);
1483             recvfrom(c, buffer, sizeof buffer, 0, NULL, NULL);
1484             send(c, okmsg, sizeof okmsg-1, 0);
1485         }
1486         if (strstr(buffer, "/testC"))
1487         {
1488             if (strstr(buffer, "Cookie: cookie=biscuit"))
1489                 send(c, okmsg, sizeof okmsg-1, 0);
1490             else
1491                 send(c, notokmsg, sizeof notokmsg-1, 0);
1492         }
1493         if (strstr(buffer, "/testD"))
1494         {
1495             send(c, okmsg2, sizeof okmsg2-1, 0);
1496         }
1497         if (strstr(buffer, "/testE"))
1498         {
1499             send(c, noauthmsg2, sizeof noauthmsg2-1, 0);
1500         }
1501         if (strstr(buffer, "GET /quit"))
1502         {
1503             send(c, okmsg, sizeof okmsg-1, 0);
1504             send(c, page1, sizeof page1-1, 0);
1505             last_request = 1;
1506         }
1507
1508         shutdown(c, 2);
1509         closesocket(c);
1510     } while (!last_request);
1511
1512     closesocket(s);
1513
1514     return 0;
1515 }
1516
1517 static void test_basic_request(int port, const char *verb, const char *url)
1518 {
1519     HINTERNET hi, hc, hr;
1520     DWORD r, count;
1521     char buffer[0x100];
1522
1523     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1524     ok(hi != NULL, "open failed\n");
1525
1526     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1527     ok(hc != NULL, "connect failed\n");
1528
1529     hr = HttpOpenRequest(hc, verb, url, NULL, NULL, NULL, 0, 0);
1530     ok(hr != NULL, "HttpOpenRequest failed\n");
1531
1532     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1533     ok(r, "HttpSendRequest failed\n");
1534
1535     count = 0;
1536     memset(buffer, 0, sizeof buffer);
1537     r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1538     ok(r, "InternetReadFile failed\n");
1539     ok(count == sizeof page1 - 1, "count was wrong\n");
1540     ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
1541
1542     InternetCloseHandle(hr);
1543     InternetCloseHandle(hc);
1544     InternetCloseHandle(hi);
1545 }
1546
1547 static void test_proxy_indirect(int port)
1548 {
1549     HINTERNET hi, hc, hr;
1550     DWORD r, sz, val;
1551     char buffer[0x40];
1552
1553     hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1554     ok(hi != NULL, "open failed\n");
1555
1556     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1557     ok(hc != NULL, "connect failed\n");
1558
1559     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1560     ok(hr != NULL, "HttpOpenRequest failed\n");
1561
1562     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1563     ok(r, "HttpSendRequest failed\n");
1564
1565     sz = sizeof buffer;
1566     r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1567     ok(r, "HttpQueryInfo failed\n");
1568     ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1569
1570     sz = sizeof buffer;
1571     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1572     ok(r, "HttpQueryInfo failed\n");
1573     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1574
1575     sz = sizeof val;
1576     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1577     ok(r, "HttpQueryInfo failed\n");
1578     ok(val == 407, "proxy code wrong\n");
1579
1580     sz = sizeof buffer;
1581     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1582     ok(r, "HttpQueryInfo failed\n");
1583     ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1584
1585     sz = sizeof buffer;
1586     r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1587     ok(r, "HttpQueryInfo failed\n");
1588     ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1589
1590     sz = sizeof buffer;
1591     r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1592     ok(r, "HttpQueryInfo failed\n");
1593     ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1594
1595     sz = sizeof buffer;
1596     r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1597     ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1598     ok(r == FALSE, "HttpQueryInfo failed\n");
1599
1600     InternetCloseHandle(hr);
1601     InternetCloseHandle(hc);
1602     InternetCloseHandle(hi);
1603 }
1604
1605 static void test_proxy_direct(int port)
1606 {
1607     HINTERNET hi, hc, hr;
1608     DWORD r, sz;
1609     char buffer[0x40];
1610     static CHAR username[] = "mike",
1611                 password[] = "1101";
1612
1613     sprintf(buffer, "localhost:%d\n", port);
1614     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1615     ok(hi != NULL, "open failed\n");
1616
1617     /* try connect without authorization */
1618     hc = InternetConnect(hi, "test.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1619     ok(hc != NULL, "connect failed\n");
1620
1621     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1622     ok(hr != NULL, "HttpOpenRequest failed\n");
1623
1624     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1625     ok(r, "HttpSendRequest failed\n");
1626
1627     sz = sizeof buffer;
1628     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1629     ok(r, "HttpQueryInfo failed\n");
1630     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1631
1632
1633     /* set the user + password then try again */
1634     todo_wine {
1635     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
1636     ok(r, "failed to set user\n");
1637
1638     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
1639     ok(r, "failed to set password\n");
1640     }
1641
1642     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1643     ok(r, "HttpSendRequest failed\n");
1644     sz = sizeof buffer;
1645     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1646     ok(r, "HttpQueryInfo failed\n");
1647     todo_wine {
1648     ok(!strcmp(buffer, "200"), "proxy code wrong\n");
1649     }
1650
1651
1652     InternetCloseHandle(hr);
1653     InternetCloseHandle(hc);
1654     InternetCloseHandle(hi);
1655 }
1656
1657 static void test_header_handling_order(int port)
1658 {
1659     static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
1660     static char connection[]    = "Connection: Close";
1661
1662     static const char *types[2] = { "*", NULL };
1663     HINTERNET session, connect, request;
1664     DWORD size, status;
1665     BOOL ret;
1666
1667     session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1668     ok(session != NULL, "InternetOpen failed\n");
1669
1670     connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1671     ok(connect != NULL, "InternetConnect failed\n");
1672
1673     request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1674     ok(request != NULL, "HttpOpenRequest failed\n");
1675
1676     ret = HttpAddRequestHeaders(request, authorization, ~0u, HTTP_ADDREQ_FLAG_ADD);
1677     ok(ret, "HttpAddRequestHeaders failed\n");
1678
1679     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1680     ok(ret, "HttpSendRequest failed\n");
1681
1682     status = 0;
1683     size = sizeof(status);
1684     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1685     ok(ret, "HttpQueryInfo failed\n");
1686     ok(status == 200, "request failed with status %u\n", status);
1687
1688     InternetCloseHandle(request);
1689
1690     request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1691     ok(request != NULL, "HttpOpenRequest failed\n");
1692
1693     ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
1694     ok(ret, "HttpSendRequest failed\n");
1695
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);
1701
1702     InternetCloseHandle(request);
1703
1704     request = HttpOpenRequest(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1705     ok(request != NULL, "HttpOpenRequest failed\n");
1706
1707     ret = HttpAddRequestHeaders(request, "Content-Length: 100\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1708     ok(ret, "HttpAddRequestHeaders failed\n");
1709
1710     ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
1711     ok(ret, "HttpSendRequest failed\n");
1712
1713     status = 0;
1714     size = sizeof(status);
1715     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1716     ok(ret, "HttpQueryInfo failed\n");
1717     ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
1718
1719     InternetCloseHandle(request);
1720     InternetCloseHandle(connect);
1721     InternetCloseHandle(session);
1722 }
1723
1724 static void test_connection_header(int port)
1725 {
1726     HINTERNET ses, con, req;
1727     DWORD size, status;
1728     BOOL ret;
1729
1730     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1731     ok(ses != NULL, "InternetOpen failed\n");
1732
1733     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1734     ok(con != NULL, "InternetConnect failed\n");
1735
1736     req = HttpOpenRequest(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1737     ok(req != NULL, "HttpOpenRequest failed\n");
1738
1739     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1740     ok(ret, "HttpSendRequest failed\n");
1741
1742     status = 0;
1743     size = sizeof(status);
1744     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1745     ok(ret, "HttpQueryInfo failed\n");
1746     ok(status == 200, "request failed with status %u\n", status);
1747
1748     InternetCloseHandle(req);
1749
1750     req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
1751     ok(req != NULL, "HttpOpenRequest failed\n");
1752
1753     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1754     ok(ret, "HttpSendRequest failed\n");
1755
1756     status = 0;
1757     size = sizeof(status);
1758     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1759     ok(ret, "HttpQueryInfo failed\n");
1760     ok(status == 200, "request failed with status %u\n", status);
1761
1762     InternetCloseHandle(req);
1763
1764     req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1765     ok(req != NULL, "HttpOpenRequest failed\n");
1766
1767     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1768     ok(ret, "HttpSendRequest failed\n");
1769
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);
1775
1776     InternetCloseHandle(req);
1777
1778     req = HttpOpenRequest(con, "POST", "/testA", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1779     ok(req != NULL, "HttpOpenRequest failed\n");
1780
1781     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1782     ok(ret, "HttpSendRequest failed\n");
1783
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);
1789
1790     InternetCloseHandle(req);
1791     InternetCloseHandle(con);
1792     InternetCloseHandle(ses);
1793 }
1794
1795 static void test_http1_1(int port)
1796 {
1797     HINTERNET ses, con, req;
1798     BOOL ret;
1799
1800     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1801     ok(ses != NULL, "InternetOpen failed\n");
1802
1803     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1804     ok(con != NULL, "InternetConnect failed\n");
1805
1806     req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1807     ok(req != NULL, "HttpOpenRequest failed\n");
1808
1809     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1810     if (ret)
1811     {
1812         InternetCloseHandle(req);
1813
1814         req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1815         ok(req != NULL, "HttpOpenRequest failed\n");
1816
1817         ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1818         todo_wine
1819         ok(ret, "HttpSendRequest failed\n");
1820     }
1821
1822     InternetCloseHandle(req);
1823     InternetCloseHandle(con);
1824     InternetCloseHandle(ses);
1825 }
1826
1827 static void test_HttpSendRequestW(int port)
1828 {
1829     static const WCHAR header[] = {'U','A','-','C','P','U',':',' ','x','8','6',0};
1830     HINTERNET ses, con, req;
1831     DWORD error;
1832     BOOL ret;
1833
1834     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
1835     ok(ses != NULL, "InternetOpen failed\n");
1836
1837     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1838     ok(con != NULL, "InternetConnect failed\n");
1839
1840     req = HttpOpenRequest(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
1841     ok(req != NULL, "HttpOpenRequest failed\n");
1842
1843     SetLastError(0xdeadbeef);
1844     ret = HttpSendRequestW(req, header, ~0u, NULL, 0);
1845     error = GetLastError();
1846     ok(!ret, "HttpSendRequestW succeeded\n");
1847     ok(error == ERROR_IO_PENDING || broken(error == ERROR_HTTP_HEADER_NOT_FOUND), /* IE6 */
1848        "got %u expected ERROR_IO_PENDING\n", error);
1849
1850     InternetCloseHandle(req);
1851     InternetCloseHandle(con);
1852     InternetCloseHandle(ses);
1853 }
1854
1855 static void test_cookie_header(int port)
1856 {
1857     HINTERNET ses, con, req;
1858     DWORD size, status, error;
1859     BOOL ret;
1860     char buffer[64];
1861
1862     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1863     ok(ses != NULL, "InternetOpen failed\n");
1864
1865     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1866     ok(con != NULL, "InternetConnect failed\n");
1867
1868     InternetSetCookie("http://localhost", "cookie", "biscuit");
1869
1870     req = HttpOpenRequest(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1871     ok(req != NULL, "HttpOpenRequest failed\n");
1872
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);
1880
1881     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1882     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1883
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);
1889
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);
1895
1896     InternetCloseHandle(req);
1897     InternetCloseHandle(con);
1898     InternetCloseHandle(ses);
1899 }
1900
1901 static void test_basic_authentication(int port)
1902 {
1903     HINTERNET session, connect, request;
1904     DWORD size, status;
1905     BOOL ret;
1906
1907     session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1908     ok(session != NULL, "InternetOpen failed\n");
1909
1910     connect = InternetConnect(session, "localhost", port, "user", "pwd", INTERNET_SERVICE_HTTP, 0, 0);
1911     ok(connect != NULL, "InternetConnect failed\n");
1912
1913     request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, NULL, 0, 0);
1914     ok(request != NULL, "HttpOpenRequest failed\n");
1915
1916     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1917     ok(ret, "HttpSendRequest failed %u\n", GetLastError());
1918
1919     status = 0;
1920     size = sizeof(status);
1921     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1922     ok(ret, "HttpQueryInfo failed\n");
1923     ok(status == 200, "request failed with status %u\n", status);
1924
1925     InternetCloseHandle(request);
1926     InternetCloseHandle(connect);
1927     InternetCloseHandle(session);
1928 }
1929
1930 static void test_invalid_response_headers(int port)
1931 {
1932     HINTERNET session, connect, request;
1933     DWORD size, status;
1934     BOOL ret;
1935     char buffer[256];
1936
1937     session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1938     ok(session != NULL, "InternetOpen failed\n");
1939
1940     connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1941     ok(connect != NULL, "InternetConnect failed\n");
1942
1943     request = HttpOpenRequest(connect, NULL, "/testE", NULL, NULL, NULL, 0, 0);
1944     ok(request != NULL, "HttpOpenRequest failed\n");
1945
1946     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1947     ok(ret, "HttpSendRequest failed %u\n", GetLastError());
1948
1949     status = 0;
1950     size = sizeof(status);
1951     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1952     ok(ret, "HttpQueryInfo failed\n");
1953     ok(status == 401, "unexpected status %u\n", status);
1954
1955     buffer[0] = 0;
1956     size = sizeof(buffer);
1957     ret = HttpQueryInfo( request, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
1958     ok(ret, "HttpQueryInfo failed\n");
1959     ok(!strcmp(buffer, "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"),
1960        "headers wrong \"%s\"\n", buffer);
1961
1962     buffer[0] = 0;
1963     size = sizeof(buffer);
1964     ret = HttpQueryInfo( request, HTTP_QUERY_SERVER, buffer, &size, NULL);
1965     ok(ret, "HttpQueryInfo failed\n");
1966     ok(!strcmp(buffer, "winetest"), "server wrong \"%s\"\n", buffer);
1967
1968     InternetCloseHandle(request);
1969     InternetCloseHandle(connect);
1970     InternetCloseHandle(session);
1971 }
1972
1973 static void test_HttpQueryInfo(int port)
1974 {
1975     HINTERNET hi, hc, hr;
1976     DWORD size, index;
1977     char buffer[1024];
1978     BOOL ret;
1979
1980     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1981     ok(hi != NULL, "InternetOpen failed\n");
1982
1983     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1984     ok(hc != NULL, "InternetConnect failed\n");
1985
1986     hr = HttpOpenRequest(hc, NULL, "/testD", NULL, NULL, NULL, 0, 0);
1987     ok(hr != NULL, "HttpOpenRequest failed\n");
1988
1989     ret = HttpSendRequest(hr, NULL, 0, NULL, 0);
1990     ok(ret, "HttpSendRequest failed\n");
1991
1992     index = 0;
1993     size = sizeof(buffer);
1994     ret = HttpQueryInfo(hr, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &index);
1995     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
1996     ok(index == 1, "expected 1 got %u\n", index);
1997
1998     index = 0;
1999     size = sizeof(buffer);
2000     ret = HttpQueryInfo(hr, HTTP_QUERY_DATE | HTTP_QUERY_FLAG_SYSTEMTIME, buffer, &size, &index);
2001     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2002     ok(index == 1, "expected 1 got %u\n", index);
2003
2004     index = 0;
2005     size = sizeof(buffer);
2006     ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
2007     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2008     ok(index == 0, "expected 0 got %u\n", index);
2009
2010     size = sizeof(buffer);
2011     ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
2012     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2013     ok(index == 0, "expected 0 got %u\n", index);
2014
2015     size = sizeof(buffer);
2016     ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &size, &index);
2017     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2018     ok(index == 0, "expected 0 got %u\n", index);
2019
2020     size = sizeof(buffer);
2021     ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
2022     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2023     ok(index == 0, "expected 0 got %u\n", index);
2024
2025     size = sizeof(buffer);
2026     ret = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &size, &index);
2027     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2028     ok(index == 0, "expected 0 got %u\n", index);
2029
2030     index = 0;
2031     size = sizeof(buffer);
2032     ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &size, &index);
2033     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2034     ok(index == 0, "expected 0 got %u\n", index);
2035
2036     index = 0;
2037     size = sizeof(buffer);
2038     ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, buffer, &size, &index);
2039     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2040     ok(index == 0, "expected 0 got %u\n", index);
2041
2042     index = 0xdeadbeef;
2043     size = sizeof(buffer);
2044     ret = HttpQueryInfo(hr, HTTP_QUERY_FORWARDED, buffer, &size, &index);
2045     ok(!ret, "HttpQueryInfo succeeded\n");
2046     ok(index == 0xdeadbeef, "expected 0xdeadbeef got %u\n", index);
2047
2048     index = 0;
2049     size = sizeof(buffer);
2050     ret = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &size, &index);
2051     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2052     ok(index == 1, "expected 1 got %u\n", index);
2053
2054     index = 0;
2055     size = sizeof(buffer);
2056     strcpy(buffer, "Server");
2057     ret = HttpQueryInfo(hr, HTTP_QUERY_CUSTOM, buffer, &size, &index);
2058     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2059     ok(index == 1, "expected 1 got %u\n", index);
2060
2061     index = 0;
2062     size = sizeof(buffer);
2063     ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
2064     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2065     ok(index == 1, "expected 1 got %u\n", index);
2066
2067     size = sizeof(buffer);
2068     ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
2069     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2070     ok(index == 2, "expected 2 got %u\n", index);
2071
2072     InternetCloseHandle(hr);
2073     InternetCloseHandle(hc);
2074     InternetCloseHandle(hi);
2075 }
2076
2077 static void test_http_connection(void)
2078 {
2079     struct server_info si;
2080     HANDLE hThread;
2081     DWORD id = 0, r;
2082
2083     si.hEvent = CreateEvent(NULL, 0, 0, NULL);
2084     si.port = 7531;
2085
2086     hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
2087     ok( hThread != NULL, "create thread failed\n");
2088
2089     r = WaitForSingleObject(si.hEvent, 10000);
2090     ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
2091     if (r != WAIT_OBJECT_0)
2092         return;
2093
2094     test_basic_request(si.port, "GET", "/test1");
2095     test_proxy_indirect(si.port);
2096     test_proxy_direct(si.port);
2097     test_header_handling_order(si.port);
2098     test_basic_request(si.port, "POST", "/test5");
2099     test_basic_request(si.port, "RPC_IN_DATA", "/test5");
2100     test_basic_request(si.port, "RPC_OUT_DATA", "/test5");
2101     test_basic_request(si.port, "GET", "/test6");
2102     test_connection_header(si.port);
2103     test_http1_1(si.port);
2104     test_cookie_header(si.port);
2105     test_basic_authentication(si.port);
2106     test_invalid_response_headers(si.port);
2107     test_HttpQueryInfo(si.port);
2108     test_HttpSendRequestW(si.port);
2109
2110     /* send the basic request again to shutdown the server thread */
2111     test_basic_request(si.port, "GET", "/quit");
2112
2113     r = WaitForSingleObject(hThread, 3000);
2114     ok( r == WAIT_OBJECT_0, "thread wait failed\n");
2115     CloseHandle(hThread);
2116 }
2117
2118 static void test_user_agent_header(void)
2119 {
2120     HINTERNET ses, con, req;
2121     DWORD size, err;
2122     char buffer[64];
2123     BOOL ret;
2124
2125     ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2126     ok(ses != NULL, "InternetOpen failed\n");
2127
2128     con = InternetConnect(ses, "test.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2129     ok(con != NULL, "InternetConnect failed\n");
2130
2131     req = HttpOpenRequest(con, "GET", "/hello.html", "HTTP/1.0", NULL, NULL, 0, 0);
2132     ok(req != NULL, "HttpOpenRequest failed\n");
2133
2134     size = sizeof(buffer);
2135     ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2136     err = GetLastError();
2137     ok(!ret, "HttpQueryInfo succeeded\n");
2138     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2139
2140     ret = HttpAddRequestHeaders(req, "User-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2141     ok(ret, "HttpAddRequestHeaders succeeded\n");
2142
2143     size = sizeof(buffer);
2144     ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2145     err = GetLastError();
2146     ok(ret, "HttpQueryInfo failed\n");
2147     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2148
2149     InternetCloseHandle(req);
2150
2151     req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
2152     ok(req != NULL, "HttpOpenRequest failed\n");
2153
2154     size = sizeof(buffer);
2155     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2156     err = GetLastError();
2157     ok(!ret, "HttpQueryInfo succeeded\n");
2158     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2159
2160     ret = HttpAddRequestHeaders(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2161     ok(ret, "HttpAddRequestHeaders failed\n");
2162
2163     buffer[0] = 0;
2164     size = sizeof(buffer);
2165     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2166     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2167     ok(!strcmp(buffer, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
2168
2169     InternetCloseHandle(req);
2170     InternetCloseHandle(con);
2171     InternetCloseHandle(ses);
2172 }
2173
2174 static void test_bogus_accept_types_array(void)
2175 {
2176     HINTERNET ses, con, req;
2177     static const char *types[] = { (const char *)6240, "*/*", "%p", "", (const char *)0xffffffff, "*/*", NULL };
2178     DWORD size;
2179     char buffer[32];
2180     BOOL ret;
2181
2182     ses = InternetOpen("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
2183     con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2184     req = HttpOpenRequest(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
2185
2186     ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
2187
2188     buffer[0] = 0;
2189     size = sizeof(buffer);
2190     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2191     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2192     ok(!strcmp(buffer, ", */*, %p, , , */*") || /* IE6 */
2193        !strcmp(buffer, "*/*, %p, */*"),
2194        "got '%s' expected '*/*, %%p, */*' or ', */*, %%p, , , */*'\n", buffer);
2195
2196     InternetCloseHandle(req);
2197     InternetCloseHandle(con);
2198     InternetCloseHandle(ses);
2199 }
2200
2201 struct context
2202 {
2203     HANDLE event;
2204     HINTERNET req;
2205 };
2206
2207 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
2208 {
2209     INTERNET_ASYNC_RESULT *result = info;
2210     struct context *ctx = (struct context *)context;
2211
2212     trace("%p 0x%08lx %u %p 0x%08x\n", handle, context, status, info, size);
2213
2214     if (status == INTERNET_STATUS_REQUEST_COMPLETE)
2215     {
2216         trace("request handle: 0x%08lx\n", result->dwResult);
2217         ctx->req = (HINTERNET)result->dwResult;
2218         SetEvent(ctx->event);
2219     }
2220     if (status == INTERNET_STATUS_HANDLE_CLOSING)
2221     {
2222         DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
2223
2224         if (InternetQueryOption(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
2225             ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
2226         SetEvent(ctx->event);
2227     }
2228 }
2229
2230 static void test_open_url_async(void)
2231 {
2232     BOOL ret;
2233     HINTERNET ses, req;
2234     DWORD size, error;
2235     struct context ctx;
2236     ULONG type;
2237
2238     ctx.req = NULL;
2239     ctx.event = CreateEvent(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
2240
2241     ses = InternetOpen("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
2242     ok(ses != NULL, "InternetOpen failed\n");
2243
2244     SetLastError(0xdeadbeef);
2245     ret = InternetSetOptionA(NULL, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
2246     error = GetLastError();
2247     ok(!ret, "InternetSetOptionA succeeded\n");
2248     ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "got %u expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE\n", error);
2249
2250     ret = InternetSetOptionA(ses, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
2251     error = GetLastError();
2252     ok(!ret, "InternetSetOptionA failed\n");
2253     ok(error == ERROR_INTERNET_OPTION_NOT_SETTABLE, "got %u expected ERROR_INTERNET_OPTION_NOT_SETTABLE\n", error);
2254
2255     pInternetSetStatusCallbackA(ses, cb);
2256     ResetEvent(ctx.event);
2257
2258     req = InternetOpenUrl(ses, "http://test.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
2259     ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
2260
2261     WaitForSingleObject(ctx.event, INFINITE);
2262
2263     type = 0;
2264     size = sizeof(type);
2265     ret = InternetQueryOption(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
2266     ok(ret, "InternetQueryOption failed: %u\n", GetLastError());
2267     ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
2268        "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %u\n", type);
2269
2270     size = 0;
2271     ret = HttpQueryInfo(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
2272     ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
2273     ok(size > 0, "expected size > 0\n");
2274
2275     ResetEvent(ctx.event);
2276     InternetCloseHandle(ctx.req);
2277     WaitForSingleObject(ctx.event, INFINITE);
2278
2279     InternetCloseHandle(ses);
2280     CloseHandle(ctx.event);
2281 }
2282
2283 #define STATUS_STRING(status) \
2284     memcpy(status_string[status], #status, sizeof(CHAR) * \
2285            (strlen(#status) < MAX_STATUS_NAME ? \
2286             strlen(#status) : \
2287             MAX_STATUS_NAME - 1))
2288 static void init_status_tests(void)
2289 {
2290     memset(expect, 0, sizeof(expect));
2291     memset(optional, 0, sizeof(optional));
2292     memset(wine_allow, 0, sizeof(wine_allow));
2293     memset(notified, 0, sizeof(notified));
2294     memset(status_string, 0, sizeof(status_string));
2295     STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
2296     STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
2297     STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
2298     STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
2299     STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
2300     STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
2301     STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
2302     STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
2303     STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
2304     STATUS_STRING(INTERNET_STATUS_PREFETCH);
2305     STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
2306     STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
2307     STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
2308     STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
2309     STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
2310     STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
2311     STATUS_STRING(INTERNET_STATUS_REDIRECT);
2312     STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
2313     STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
2314     STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
2315     STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
2316     STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
2317     STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
2318     STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
2319     STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
2320     STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
2321 }
2322 #undef STATUS_STRING
2323
2324 START_TEST(http)
2325 {
2326     HMODULE hdll;
2327     hdll = GetModuleHandleA("wininet.dll");
2328     pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
2329
2330     if (!pInternetSetStatusCallbackA)
2331         skip("skipping the InternetReadFile tests\n");
2332     else
2333     {
2334         init_status_tests();
2335         InternetReadFile_test(INTERNET_FLAG_ASYNC);
2336         InternetReadFile_test(0);
2337         InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
2338         test_open_url_async();
2339     }
2340     InternetOpenRequest_test();
2341     test_http_cache();
2342     InternetOpenUrlA_test();
2343     HttpSendRequestEx_test();
2344     HttpHeaders_test();
2345     test_http_connection();
2346     test_user_agent_header();
2347     test_bogus_accept_types_array();
2348 }