wined3d: Handle the sampler type shift in the frontend.
[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] + optional[status] == (num), \
88            "expected status %d (%s) %d times, received %d times\n", \
89            status, status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
90            status_string[status] : "unknown", (num), notified[status]); \
91         CLEAR_NOTIFIED(status);                                         \
92     }while(0)
93
94 #define CHECK_NOTIFIED(status) \
95     CHECK_NOTIFIED2(status, 1)
96
97 #define CHECK_NOT_NOTIFIED(status) \
98     CHECK_NOTIFIED2(status, 0)
99
100 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
101 #define MAX_STATUS_NAME 50
102 static int expect[MAX_INTERNET_STATUS], optional[MAX_INTERNET_STATUS],
103     wine_allow[MAX_INTERNET_STATUS], notified[MAX_INTERNET_STATUS];
104 static CHAR status_string[MAX_INTERNET_STATUS][MAX_STATUS_NAME];
105
106 static HANDLE hCompleteEvent;
107
108 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
109
110
111 static VOID WINAPI callback(
112      HINTERNET hInternet,
113      DWORD_PTR dwContext,
114      DWORD dwInternetStatus,
115      LPVOID lpvStatusInformation,
116      DWORD dwStatusInformationLength
117 )
118 {
119     CHECK_EXPECT(dwInternetStatus);
120     switch (dwInternetStatus)
121     {
122         case INTERNET_STATUS_RESOLVING_NAME:
123             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
124                 GetCurrentThreadId(), hInternet, dwContext,
125                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
126             *(LPSTR)lpvStatusInformation = '\0';
127             break;
128         case INTERNET_STATUS_NAME_RESOLVED:
129             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
130                 GetCurrentThreadId(), hInternet, dwContext,
131                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
132             *(LPSTR)lpvStatusInformation = '\0';
133             break;
134         case INTERNET_STATUS_CONNECTING_TO_SERVER:
135             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
136                 GetCurrentThreadId(), hInternet, dwContext,
137                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
138             *(LPSTR)lpvStatusInformation = '\0';
139             break;
140         case INTERNET_STATUS_CONNECTED_TO_SERVER:
141             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
142                 GetCurrentThreadId(), hInternet, dwContext,
143                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
144             *(LPSTR)lpvStatusInformation = '\0';
145             break;
146         case INTERNET_STATUS_SENDING_REQUEST:
147             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
148                 GetCurrentThreadId(), hInternet, dwContext,
149                 lpvStatusInformation,dwStatusInformationLength);
150             break;
151         case INTERNET_STATUS_REQUEST_SENT:
152             ok(dwStatusInformationLength == sizeof(DWORD),
153                 "info length should be sizeof(DWORD) instead of %d\n",
154                 dwStatusInformationLength);
155             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
156                 GetCurrentThreadId(), hInternet, dwContext,
157                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
158             break;
159         case INTERNET_STATUS_RECEIVING_RESPONSE:
160             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
161                 GetCurrentThreadId(), hInternet, dwContext,
162                 lpvStatusInformation,dwStatusInformationLength);
163             break;
164         case INTERNET_STATUS_RESPONSE_RECEIVED:
165             ok(dwStatusInformationLength == sizeof(DWORD),
166                 "info length should be sizeof(DWORD) instead of %d\n",
167                 dwStatusInformationLength);
168             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
169                 GetCurrentThreadId(), hInternet, dwContext,
170                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
171             break;
172         case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
173             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
174                 GetCurrentThreadId(), hInternet,dwContext,
175                 lpvStatusInformation,dwStatusInformationLength);
176             break;
177         case INTERNET_STATUS_PREFETCH:
178             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
179                 GetCurrentThreadId(), hInternet, dwContext,
180                 lpvStatusInformation,dwStatusInformationLength);
181             break;
182         case INTERNET_STATUS_CLOSING_CONNECTION:
183             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
184                 GetCurrentThreadId(), hInternet, dwContext,
185                 lpvStatusInformation,dwStatusInformationLength);
186             break;
187         case INTERNET_STATUS_CONNECTION_CLOSED:
188             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
189                 GetCurrentThreadId(), hInternet, dwContext,
190                 lpvStatusInformation,dwStatusInformationLength);
191             break;
192         case INTERNET_STATUS_HANDLE_CREATED:
193             ok(dwStatusInformationLength == sizeof(HINTERNET),
194                 "info length should be sizeof(HINTERNET) instead of %d\n",
195                 dwStatusInformationLength);
196             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
197                 GetCurrentThreadId(), hInternet, dwContext,
198                 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
199             CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
200             SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
201             break;
202         case INTERNET_STATUS_HANDLE_CLOSING:
203             ok(dwStatusInformationLength == sizeof(HINTERNET),
204                 "info length should be sizeof(HINTERNET) instead of %d\n",
205                 dwStatusInformationLength);
206             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
207                 GetCurrentThreadId(), hInternet, dwContext,
208                 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
209             break;
210         case INTERNET_STATUS_REQUEST_COMPLETE:
211         {
212             INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
213             ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
214                 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
215                 dwStatusInformationLength);
216             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
217                 GetCurrentThreadId(), hInternet, dwContext,
218                 iar->dwResult,iar->dwError,dwStatusInformationLength);
219             SetEvent(hCompleteEvent);
220             break;
221         }
222         case INTERNET_STATUS_REDIRECT:
223             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
224                 GetCurrentThreadId(), hInternet, dwContext,
225                 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
226             *(LPSTR)lpvStatusInformation = '\0';
227             CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
228             SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
229             break;
230         case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
231             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
232                 GetCurrentThreadId(), hInternet, dwContext,
233                 lpvStatusInformation, dwStatusInformationLength);
234             break;
235         default:
236             trace("%04x:Callback %p 0x%lx %d %p %d\n",
237                 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
238                 lpvStatusInformation, dwStatusInformationLength);
239     }
240 }
241
242 static void InternetReadFile_test(int flags)
243 {
244     BOOL res;
245     CHAR buffer[4000];
246     DWORD length;
247     DWORD out;
248     const char *types[2] = { "*", NULL };
249     HINTERNET hi, hic = 0, hor = 0;
250
251     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
252
253     trace("Starting InternetReadFile test with flags 0x%x\n",flags);
254
255     trace("InternetOpenA <--\n");
256     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
257     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
258     trace("InternetOpenA -->\n");
259
260     if (hi == 0x0) goto abort;
261
262     pInternetSetStatusCallbackA(hi,&callback);
263
264     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
265
266     trace("InternetConnectA <--\n");
267     hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
268                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
269     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
270     trace("InternetConnectA -->\n");
271
272     if (hic == 0x0) goto abort;
273
274     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
275     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
276
277     trace("HttpOpenRequestA <--\n");
278     hor = HttpOpenRequestA(hic, "GET", "/testredirect", NULL, NULL, types,
279                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
280                            0xdeadbead);
281     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
282         /*
283          * If the internet name can't be resolved we are probably behind
284          * a firewall or in some other way not directly connected to the
285          * Internet. Not enough reason to fail the test. Just ignore and
286          * abort.
287          */
288     } else  {
289         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
290     }
291     trace("HttpOpenRequestA -->\n");
292
293     if (hor == 0x0) goto abort;
294
295     length = sizeof(buffer);
296     res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
297     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
298     ok(!strcmp(buffer, "http://test.winehq.org/testredirect"), "Wrong URL %s\n", buffer);
299
300     length = sizeof(buffer);
301     res = HttpQueryInfoA(hor, HTTP_QUERY_RAW_HEADERS, buffer, &length, 0x0);
302     ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
303     ok(length == 0, "HTTP_QUERY_RAW_HEADERS: expected length 0, but got %d\n", length);
304     ok(!strcmp(buffer, ""), "HTTP_QUERY_RAW_HEADERS: expected string \"\", but got \"%s\"\n", buffer);
305
306     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
307     CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
308     CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
309     if (first_connection_to_test_url)
310     {
311         SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
312         SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
313         SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
314         SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
315     }
316     else
317     {
318         SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
319         SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
320     }
321     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
322     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
323     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
324     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
325     SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
326     SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
327     SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
328     SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
329     SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
330     SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
331     SET_EXPECT(INTERNET_STATUS_REDIRECT);
332     SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
333     SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
334     if (flags & INTERNET_FLAG_ASYNC)
335         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
336     else
337         SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
338
339     trace("HttpSendRequestA -->\n");
340     SetLastError(0xdeadbeef);
341     res = HttpSendRequestA(hor, "", -1, NULL, 0);
342     if (flags & INTERNET_FLAG_ASYNC)
343         ok(!res && (GetLastError() == ERROR_IO_PENDING),
344             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
345     else
346         ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
347            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
348     trace("HttpSendRequestA <--\n");
349
350     if (flags & INTERNET_FLAG_ASYNC)
351         WaitForSingleObject(hCompleteEvent, INFINITE);
352
353     if (first_connection_to_test_url)
354     {
355         CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
356         CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
357     }
358     else todo_wine
359     {
360         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
361         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
362     }
363     CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
364     CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
365     CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
366     CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
367     CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
368     if (flags & INTERNET_FLAG_ASYNC)
369         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
370     else
371         CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
372     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
373     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
374     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
375
376     length = 4;
377     res = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
378     ok(res, "InternetQueryOptionA(INTERNET_OPTION_REQUEST) failed with error %d\n", GetLastError());
379
380     length = 100;
381     res = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
382     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed with error %d\n", GetLastError());
383
384     length = sizeof(buffer);
385     res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
386     ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
387     buffer[length]=0;
388
389     length = sizeof(buffer);
390     res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
391     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
392     ok(!strcmp(buffer, "http://test.winehq.org/hello.html"), "Wrong URL %s\n", buffer);
393
394     length = 16;
395     res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
396     trace("Option 0x5 -> %i  %s  (%u)\n",res,buffer,GetLastError());
397
398     length = 100;
399     res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
400     buffer[length]=0;
401     trace("Option 0x1 -> %i  %s\n",res,buffer);
402
403     SetLastError(0xdeadbeef);
404     res = InternetReadFile(NULL, buffer, 100, &length);
405     ok(!res, "InternetReadFile should have failed\n");
406     ok(GetLastError() == ERROR_INVALID_HANDLE,
407         "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
408         GetLastError());
409
410     length = 100;
411     trace("Entering Query loop\n");
412
413     while (TRUE)
414     {
415         if (flags & INTERNET_FLAG_ASYNC)
416             SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
417         res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
418         ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
419         ok(res || ((flags & INTERNET_FLAG_ASYNC) && GetLastError() == ERROR_IO_PENDING),
420            "InternetQueryDataAvailable failed, error %d\n", GetLastError());
421         if (flags & INTERNET_FLAG_ASYNC)
422         {
423             if (res)
424             {
425                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
426             }
427             else if (GetLastError() == ERROR_IO_PENDING)
428             {
429                 WaitForSingleObject(hCompleteEvent, INFINITE);
430                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
431                 continue;
432             }
433         }
434         if (length)
435         {
436             char *buffer;
437             buffer = HeapAlloc(GetProcessHeap(),0,length+1);
438
439             res = InternetReadFile(hor,buffer,length,&length);
440
441             buffer[length]=0;
442
443             trace("ReadFile -> %s %i\n",res?"TRUE":"FALSE",length);
444
445             HeapFree(GetProcessHeap(),0,buffer);
446         }
447         if (length == 0)
448             break;
449     }
450     CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
451     CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
452 abort:
453     trace("aborting\n");
454     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
455     if (hor != 0x0) {
456         SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
457         SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
458         SetLastError(0xdeadbeef);
459         trace("closing\n");
460         res = InternetCloseHandle(hor);
461         ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
462         SetLastError(0xdeadbeef);
463         res = InternetCloseHandle(hor);
464         ok (!res, "Double close of handle opened by HttpOpenRequestA succeeded\n");
465         ok (GetLastError() == ERROR_INVALID_HANDLE,
466             "Double close of handle should have set ERROR_INVALID_HANDLE instead of %u\n",
467             GetLastError());
468     }
469     /* We intentionally do not close the handle opened by InternetConnectA as this
470      * tickles bug #9479: native closes child internet handles when the parent handles
471      * are closed. This is verified below by checking that the number of
472      * INTERNET_STATUS_HANDLE_CLOSING notifications matches the number expected. */
473     if (hi != 0x0) {
474       SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
475         trace("closing 2\n");
476       res = InternetCloseHandle(hi);
477       ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
478       if (flags & INTERNET_FLAG_ASYNC)
479           Sleep(100);
480     }
481     CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
482     if (hor != 0x0) todo_wine
483     {
484         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
485         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
486     }
487     else
488     {
489         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
490         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
491     }
492     CloseHandle(hCompleteEvent);
493     first_connection_to_test_url = FALSE;
494 }
495
496 static void InternetReadFileExA_test(int flags)
497 {
498     DWORD rc;
499     DWORD length;
500     const char *types[2] = { "*", NULL };
501     HINTERNET hi, hic = 0, hor = 0;
502     INTERNET_BUFFERS inetbuffers;
503
504     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
505
506     trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
507
508     trace("InternetOpenA <--\n");
509     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
510     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
511     trace("InternetOpenA -->\n");
512
513     if (hi == 0x0) goto abort;
514
515     pInternetSetStatusCallbackA(hi,&callback);
516
517     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
518
519     trace("InternetConnectA <--\n");
520     hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
521                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
522     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
523     trace("InternetConnectA -->\n");
524
525     if (hic == 0x0) goto abort;
526
527     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
528     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
529
530     trace("HttpOpenRequestA <--\n");
531     hor = HttpOpenRequestA(hic, "GET", "/testredirect", NULL, NULL, types,
532                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
533                            0xdeadbead);
534     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
535         /*
536          * If the internet name can't be resolved we are probably behind
537          * a firewall or in some other way not directly connected to the
538          * Internet. Not enough reason to fail the test. Just ignore and
539          * abort.
540          */
541     } else  {
542         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
543     }
544     trace("HttpOpenRequestA -->\n");
545
546     if (hor == 0x0) goto abort;
547
548     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
549     CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
550     CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
551     if (first_connection_to_test_url)
552     {
553         SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
554         SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
555         SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
556         SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
557     }
558     else
559     {
560         SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
561         SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
562     }
563     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
564     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
565     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
566     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
567     SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
568     SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
569     SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
570     SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
571     SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
572     SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
573     SET_EXPECT(INTERNET_STATUS_REDIRECT);
574     SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
575     SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
576     if (flags & INTERNET_FLAG_ASYNC)
577         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
578     else
579         SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
580
581     trace("HttpSendRequestA -->\n");
582     SetLastError(0xdeadbeef);
583     rc = HttpSendRequestA(hor, "", -1, NULL, 0);
584     if (flags & INTERNET_FLAG_ASYNC)
585         ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
586             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
587     else
588         ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
589            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
590     trace("HttpSendRequestA <--\n");
591
592     if (!rc && (GetLastError() == ERROR_IO_PENDING))
593         WaitForSingleObject(hCompleteEvent, INFINITE);
594
595     if (first_connection_to_test_url)
596     {
597         CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
598         CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
599     }
600     else todo_wine
601     {
602         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
603         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
604     }
605     CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
606     CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
607     CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
608     CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
609     CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
610     CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
611     CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
612     if (flags & INTERNET_FLAG_ASYNC)
613         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
614     else
615         todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
616     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
617     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
618     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
619
620     /* tests invalid dwStructSize */
621     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
622     inetbuffers.lpcszHeader = NULL;
623     inetbuffers.dwHeadersLength = 0;
624     inetbuffers.dwBufferLength = 10;
625     inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
626     inetbuffers.dwOffsetHigh = 1234;
627     inetbuffers.dwOffsetLow = 5678;
628     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
629     ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
630         "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
631         rc ? "TRUE" : "FALSE", GetLastError());
632     HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
633
634     /* tests to see whether lpcszHeader is used - it isn't */
635     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
636     inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
637     inetbuffers.dwHeadersLength = 255;
638     inetbuffers.dwBufferLength = 0;
639     inetbuffers.lpvBuffer = NULL;
640     inetbuffers.dwOffsetHigh = 1234;
641     inetbuffers.dwOffsetLow = 5678;
642     SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
643     SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
644     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
645     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
646     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
647     ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
648         trace("read %i bytes\n", inetbuffers.dwBufferLength);
649     todo_wine
650     {
651         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
652         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
653     }
654
655     rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
656     ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
657         "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
658         rc ? "TRUE" : "FALSE", GetLastError());
659
660     length = 0;
661     trace("Entering Query loop\n");
662
663     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
664     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
665     while (TRUE)
666     {
667         inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
668         inetbuffers.dwBufferLength = 1024;
669         inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
670         inetbuffers.dwOffsetHigh = 1234;
671         inetbuffers.dwOffsetLow = 5678;
672
673         SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
674         SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
675         SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
676         SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
677         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
678         rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
679         if (!rc)
680         {
681             if (GetLastError() == ERROR_IO_PENDING)
682             {
683                 trace("InternetReadFileEx -> PENDING\n");
684                 ok(flags & INTERNET_FLAG_ASYNC,
685                    "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
686                 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
687                 WaitForSingleObject(hCompleteEvent, INFINITE);
688                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
689                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
690             }
691             else
692             {
693                 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
694                 break;
695             }
696         }
697         else
698         {
699             trace("InternetReadFileEx -> SUCCEEDED\n");
700             CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
701             if (inetbuffers.dwBufferLength)
702             {
703                 todo_wine {
704                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
705                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
706                 }
707             }
708             else
709             {
710                 /* Win98 still sends these when 0 bytes are read, WinXP does not */
711                 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
712                 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
713             }
714         }
715
716         trace("read %i bytes\n", inetbuffers.dwBufferLength);
717         ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
718
719         ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
720             "InternetReadFileEx sets offsets to 0x%x%08x\n",
721             inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
722
723         HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
724
725         if (!inetbuffers.dwBufferLength)
726             break;
727
728         length += inetbuffers.dwBufferLength;
729     }
730     ok(length > 0, "failed to read any of the document\n");
731     trace("Finished. Read %d bytes\n", length);
732
733     /* WinXP does not send, but Win98 does */
734     CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
735     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
736 abort:
737     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
738     if (hor) {
739         SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
740         SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
741         rc = InternetCloseHandle(hor);
742         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
743         rc = InternetCloseHandle(hor);
744         ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
745     }
746     if (hic) {
747         rc = InternetCloseHandle(hic);
748         ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
749     }
750     if (hi) {
751       SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
752       rc = InternetCloseHandle(hi);
753       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
754       if (flags & INTERNET_FLAG_ASYNC)
755           Sleep(100);
756       CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
757     }
758     /* to enable once Wine is fixed to never send it
759     CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
760     CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
761     */
762     CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
763     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
764     CloseHandle(hCompleteEvent);
765     first_connection_to_test_url = FALSE;
766 }
767
768 static void InternetOpenUrlA_test(void)
769 {
770   HINTERNET myhinternet, myhttp;
771   char buffer[0x400];
772   DWORD size, readbytes, totalbytes=0;
773   BOOL ret;
774
775   myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
776   ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
777   size = 0x400;
778   ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
779   ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
780
781   SetLastError(0);
782   myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
783                            INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
784   if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
785     return; /* WinXP returns this when not connected to the net */
786   ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
787   ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
788   ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
789   totalbytes += readbytes;
790   while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
791     totalbytes += readbytes;
792   trace("read 0x%08x bytes\n",totalbytes);
793
794   InternetCloseHandle(myhttp);
795   InternetCloseHandle(myhinternet);
796 }
797
798 static void HttpSendRequestEx_test(void)
799 {
800     HINTERNET hSession;
801     HINTERNET hConnect;
802     HINTERNET hRequest;
803
804     INTERNET_BUFFERS BufferIn;
805     DWORD dwBytesWritten;
806     DWORD dwBytesRead;
807     CHAR szBuffer[256];
808     int i;
809     BOOL ret;
810
811     static char szPostData[] = "mode=Test";
812     static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
813
814     hSession = InternetOpen("Wine Regression Test",
815             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
816     ok( hSession != NULL ,"Unable to open Internet session\n");
817     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
818             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
819             0);
820     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
821     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
822             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
823     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
824     {
825         skip( "Network unreachable, skipping test\n" );
826         goto done;
827     }
828     ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
829
830
831     BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
832     BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
833     BufferIn.lpcszHeader = szContentType;
834     BufferIn.dwHeadersLength = sizeof(szContentType)-1;
835     BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
836     BufferIn.lpvBuffer = szPostData;
837     BufferIn.dwBufferLength = 3;
838     BufferIn.dwBufferTotal = sizeof(szPostData)-1;
839     BufferIn.dwOffsetLow = 0;
840     BufferIn.dwOffsetHigh = 0;
841
842     ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
843     ok(ret, "HttpSendRequestEx Failed with error %u\n", GetLastError());
844
845     for (i = 3; szPostData[i]; i++)
846         ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
847                 "InternetWriteFile failed\n");
848
849     ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
850
851     ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
852             "Unable to read response\n");
853     szBuffer[dwBytesRead] = 0;
854
855     ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
856     ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0,"Got string %s\n",szBuffer);
857
858     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
859 done:
860     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
861     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
862 }
863
864 static void InternetOpenRequest_test(void)
865 {
866     HINTERNET session, connect, request;
867     static const char *types[] = { "*", "", NULL };
868     static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
869     static const WCHAR *typesW[] = { any, empty, NULL };
870     BOOL ret;
871
872     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
873     ok(session != NULL ,"Unable to open Internet session\n");
874
875     connect = InternetConnectA(session, NULL, 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 NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
879
880     connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
881                               INTERNET_SERVICE_HTTP, 0, 0);
882     ok(connect == NULL, "InternetConnectA should have failed\n");
883     ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
884
885     connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
886                               INTERNET_SERVICE_HTTP, 0, 0);
887     ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
888
889     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
890     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
891     {
892         skip( "Network unreachable, skipping test\n" );
893         goto done;
894     }
895     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
896
897     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
898     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
899     ok(InternetCloseHandle(request), "Close request handle failed\n");
900
901     request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
902     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
903
904     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
905     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
906     ok(InternetCloseHandle(request), "Close request handle failed\n");
907
908 done:
909     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
910     ok(InternetCloseHandle(session), "Close session handle failed\n");
911 }
912
913 static void test_http_cache(void)
914 {
915     HINTERNET session, connect, request;
916     char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
917     DWORD size, file_size;
918     BYTE buf[100];
919     HANDLE file;
920     BOOL ret;
921
922     static const char *types[] = { "*", "", NULL };
923
924     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
925     ok(session != NULL ,"Unable to open Internet session\n");
926
927     connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
928                               INTERNET_SERVICE_HTTP, 0, 0);
929     ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
930
931     request = HttpOpenRequestA(connect, NULL, "/hello.html", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
932     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
933     {
934         skip( "Network unreachable, skipping test\n" );
935
936         ok(InternetCloseHandle(connect), "Close connect handle failed\n");
937         ok(InternetCloseHandle(session), "Close session handle failed\n");
938
939         return;
940     }
941     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
942
943     size = sizeof(url);
944     ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
945     ok(ret, "InternetQueryOptionA(INTERNET_OPTION_url) failed: %u\n", GetLastError());
946     ok(!strcmp(url, "http://test.winehq.org/hello.html"), "Wrong URL %s\n", url);
947
948     size = sizeof(file_name);
949     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
950     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
951     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
952     ok(!size, "size = %d\n", size);
953
954     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
955     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
956
957     size = sizeof(file_name);
958     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
959     ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
960
961     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
962                       FILE_ATTRIBUTE_NORMAL, NULL);
963     ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
964     file_size = GetFileSize(file, NULL);
965     todo_wine ok(file_size == 106, "file size = %u\n", file_size);
966
967     size = sizeof(buf);
968     ret = InternetReadFile(request, buf, sizeof(buf), &size);
969     ok(ret, "InternetReadFile failed: %u\n", GetLastError());
970     ok(size == 100, "size = %u\n", size);
971
972     file_size = GetFileSize(file, NULL);
973     todo_wine ok(file_size == 106, "file size = %u\n", file_size);
974     CloseHandle(file);
975
976     ok(InternetCloseHandle(request), "Close request handle failed\n");
977
978     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
979                       FILE_ATTRIBUTE_NORMAL, NULL);
980     todo_wine ok(file != INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
981     CloseHandle(file);
982
983     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
984     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
985
986     size = sizeof(file_name);
987     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
988     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
989     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
990     ok(!size, "size = %d\n", size);
991
992     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
993     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
994
995     size = sizeof(file_name);
996     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
997     todo_wine ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed %u\n", GetLastError());
998
999     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1000                       FILE_ATTRIBUTE_NORMAL, NULL);
1001     todo_wine ok(file != INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
1002     CloseHandle(file);
1003
1004     ok(InternetCloseHandle(request), "Close request handle failed\n");
1005     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1006     ok(InternetCloseHandle(session), "Close session handle failed\n");
1007 }
1008
1009 static void HttpHeaders_test(void)
1010 {
1011     HINTERNET hSession;
1012     HINTERNET hConnect;
1013     HINTERNET hRequest;
1014     CHAR      buffer[256];
1015     WCHAR     wbuffer[256];
1016     DWORD     len = 256;
1017     DWORD     oldlen;
1018     DWORD     index = 0;
1019
1020     hSession = InternetOpen("Wine Regression Test",
1021             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1022     ok( hSession != NULL ,"Unable to open Internet session\n");
1023     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1024             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1025             0);
1026     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1027     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1028             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1029     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1030     {
1031         skip( "Network unreachable, skipping test\n" );
1032         goto done;
1033     }
1034     ok( hRequest != NULL, "Failed to open request handle\n");
1035
1036     index = 0;
1037     len = sizeof(buffer);
1038     strcpy(buffer,"Warning");
1039     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1040                buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1041
1042     ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1043             "Failed to add new header\n");
1044
1045     index = 0;
1046     len = sizeof(buffer);
1047     strcpy(buffer,"Warning");
1048     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1049                 buffer,&len,&index),"Unable to query header\n");
1050     ok(index == 1, "Index was not incremented\n");
1051     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1052     ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1053     ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1054     len = sizeof(buffer);
1055     strcpy(buffer,"Warning");
1056     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1057                 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1058
1059     index = 0;
1060     len = 5; /* could store the string but not the NULL terminator */
1061     strcpy(buffer,"Warning");
1062     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1063                 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1064     ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1065     ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1066
1067     /* a call with NULL will fail but will return the length */
1068     index = 0;
1069     len = sizeof(buffer);
1070     SetLastError(0xdeadbeef);
1071     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1072                 NULL,&len,&index) == FALSE,"Query worked\n");
1073     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1074     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1075     ok(index == 0, "Index was incremented\n");
1076
1077     /* even for a len that is too small */
1078     index = 0;
1079     len = 15;
1080     SetLastError(0xdeadbeef);
1081     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1082                 NULL,&len,&index) == FALSE,"Query worked\n");
1083     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1084     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1085     ok(index == 0, "Index was incremented\n");
1086
1087     index = 0;
1088     len = 0;
1089     SetLastError(0xdeadbeef);
1090     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1091                 NULL,&len,&index) == FALSE,"Query worked\n");
1092     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1093     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1094     ok(index == 0, "Index was incremented\n");
1095     oldlen = len;   /* bytes; at least long enough to hold buffer & nul */
1096
1097
1098     /* a working query */
1099     index = 0;
1100     len = sizeof(buffer);
1101     memset(buffer, 'x', sizeof(buffer));
1102     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1103                 buffer,&len,&index),"Unable to query header\n");
1104     ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1105     ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1106     ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1107     /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1108     ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1109     ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1110     ok(index == 0, "Index was incremented\n");
1111
1112     /* Like above two tests, but for W version */
1113
1114     index = 0;
1115     len = 0;
1116     SetLastError(0xdeadbeef);
1117     ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1118                 NULL,&len,&index) == FALSE,"Query worked\n");
1119     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1120     ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1121     ok(index == 0, "Index was incremented\n");
1122     oldlen = len;   /* bytes; at least long enough to hold buffer & nul */
1123
1124     /* a working query */
1125     index = 0;
1126     len = sizeof(wbuffer);
1127     memset(wbuffer, 'x', sizeof(wbuffer));
1128     ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1129                 wbuffer,&len,&index),"Unable to query header\n");
1130     ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1131     ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1132     ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1133     ok(index == 0, "Index was incremented\n");
1134
1135     /* end of W version tests */
1136
1137     /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1138     index = 0;
1139     len = sizeof(buffer);
1140     memset(buffer, 'x', sizeof(buffer));
1141     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF,
1142                 buffer,&len,&index) == TRUE,"Query failed\n");
1143     ok(len == 2, "Expected 2, got %d\n", len);
1144     ok(strcmp(buffer, "\r\n") == 0, "Expected CRLF, got '%s'\n", buffer);
1145     ok(index == 0, "Index was incremented\n");
1146
1147     ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1148             "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1149
1150     index = 0;
1151     len = sizeof(buffer);
1152     strcpy(buffer,"Warning");
1153     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1154                 buffer,&len,&index),"Unable to query header\n");
1155     ok(index == 1, "Index was not incremented\n");
1156     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1157     len = sizeof(buffer);
1158     strcpy(buffer,"Warning");
1159     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1160                 buffer,&len,&index),"Failed to get second header\n");
1161     ok(index == 2, "Index was not incremented\n");
1162     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1163     len = sizeof(buffer);
1164     strcpy(buffer,"Warning");
1165     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1166                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1167
1168     ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1169
1170     index = 0;
1171     len = sizeof(buffer);
1172     strcpy(buffer,"Warning");
1173     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1174                 buffer,&len,&index),"Unable to query header\n");
1175     ok(index == 1, "Index was not incremented\n");
1176     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1177     len = sizeof(buffer);
1178     strcpy(buffer,"Warning");
1179     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1180                 buffer,&len,&index),"Failed to get second header\n");
1181     ok(index == 2, "Index was not incremented\n");
1182     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1183     len = sizeof(buffer);
1184     strcpy(buffer,"Warning");
1185     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1186                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1187
1188     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1189
1190     index = 0;
1191     len = sizeof(buffer);
1192     strcpy(buffer,"Warning");
1193     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1194                 buffer,&len,&index),"Unable to query header\n");
1195     ok(index == 1, "Index was not incremented\n");
1196     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1197     len = sizeof(buffer);
1198     strcpy(buffer,"Warning");
1199     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1200                 buffer,&len,&index),"Failed to get second header\n");
1201     ok(index == 2, "Index was not incremented\n");
1202     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1203     len = sizeof(buffer);
1204     strcpy(buffer,"Warning");
1205     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1206                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1207
1208     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1209
1210     index = 0;
1211     len = sizeof(buffer);
1212     strcpy(buffer,"Warning");
1213     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1214                 buffer,&len,&index),"Unable to query header\n");
1215     ok(index == 1, "Index was not incremented\n");
1216     ok(strcmp(buffer,"test2, test4")==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),"Failed to get second header\n");
1220     ok(index == 2, "Index was not incremented\n");
1221     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1222     len = sizeof(buffer);
1223     strcpy(buffer,"Warning");
1224     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1225
1226     ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1227
1228     index = 0;
1229     len = sizeof(buffer);
1230     strcpy(buffer,"Warning");
1231     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1232     ok(index == 1, "Index was not incremented\n");
1233     ok(strcmp(buffer,"test2, test4, test5")==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),"Failed to get second header\n");
1237     ok(index == 2, "Index was not incremented\n");
1238     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1239     len = sizeof(buffer);
1240     strcpy(buffer,"Warning");
1241     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1242
1243     ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1244
1245     index = 0;
1246     len = sizeof(buffer);
1247     strcpy(buffer,"Warning");
1248     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1249     ok(index == 1, "Index was not incremented\n");
1250     ok(strcmp(buffer,"test2, test4, test5; test6")==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),"Failed to get second header\n");
1254     ok(index == 2, "Index was not incremented\n");
1255     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1256     len = sizeof(buffer);
1257     strcpy(buffer,"Warning");
1258     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1259
1260     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");
1261
1262     index = 0;
1263     len = sizeof(buffer);
1264     strcpy(buffer,"Warning");
1265     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1266     ok(index == 1, "Index was not incremented\n");
1267     ok(strcmp(buffer,"test3")==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),"Failed to get second header\n");
1271     ok(index == 2, "Index was not incremented\n");
1272     ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1273     len = sizeof(buffer);
1274     strcpy(buffer,"Warning");
1275     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1276
1277     /* Ensure that blank headers are ignored and don't cause a failure */
1278     ok(HttpAddRequestHeaders(hRequest,"\r\nBlankTest:value\r\n\r\n",-1, HTTP_ADDREQ_FLAG_ADD_IF_NEW), "Failed to add header with blank entries in list\n");
1279
1280     index = 0;
1281     len = sizeof(buffer);
1282     strcpy(buffer,"BlankTest");
1283     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1284     ok(index == 1, "Index was not incremented\n");
1285     ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1286
1287     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1288 done:
1289     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1290     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1291 }
1292
1293 static const char contmsg[] =
1294 "HTTP/1.1 100 Continue\r\n";
1295
1296 static const char okmsg[] =
1297 "HTTP/1.1 200 OK\r\n"
1298 "Server: winetest\r\n"
1299 "\r\n";
1300
1301 static const char okmsg2[] =
1302 "HTTP/1.1 200 OK\r\n"
1303 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
1304 "Server: winetest\r\n"
1305 "Content-Length: 0\r\n"
1306 "Set-Cookie: one\r\n"
1307 "Set-Cookie: two\r\n"
1308 "\r\n";
1309
1310 static const char notokmsg[] =
1311 "HTTP/1.1 400 Bad Request\r\n"
1312 "Server: winetest\r\n"
1313 "\r\n";
1314
1315 static const char noauthmsg[] =
1316 "HTTP/1.1 401 Unauthorized\r\n"
1317 "Server: winetest\r\n"
1318 "Connection: close\r\n"
1319 "WWW-Authenticate: Basic realm=\"placebo\"\r\n"
1320 "\r\n";
1321
1322 static const char noauthmsg2[] =
1323 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed\r\n"
1324 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"
1325 "\0d`0|6\n"
1326 "Server: winetest\r\n";
1327
1328 static const char proxymsg[] =
1329 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1330 "Server: winetest\r\n"
1331 "Proxy-Connection: close\r\n"
1332 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1333 "\r\n";
1334
1335 static const char page1[] =
1336 "<HTML>\r\n"
1337 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1338 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1339 "</HTML>\r\n\r\n";
1340
1341 struct server_info {
1342     HANDLE hEvent;
1343     int port;
1344 };
1345
1346 static DWORD CALLBACK server_thread(LPVOID param)
1347 {
1348     struct server_info *si = param;
1349     int r, c, i, on;
1350     SOCKET s;
1351     struct sockaddr_in sa;
1352     char buffer[0x100];
1353     WSADATA wsaData;
1354     int last_request = 0;
1355     char host_header[22];
1356     static int test_b = 0;
1357
1358     WSAStartup(MAKEWORD(1,1), &wsaData);
1359
1360     s = socket(AF_INET, SOCK_STREAM, 0);
1361     if (s == INVALID_SOCKET)
1362         return 1;
1363
1364     on = 1;
1365     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1366
1367     memset(&sa, 0, sizeof sa);
1368     sa.sin_family = AF_INET;
1369     sa.sin_port = htons(si->port);
1370     sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1371
1372     r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1373     if (r<0)
1374         return 1;
1375
1376     listen(s, 0);
1377
1378     SetEvent(si->hEvent);
1379
1380     sprintf(host_header, "Host: localhost:%d", si->port);
1381
1382     do
1383     {
1384         c = accept(s, NULL, NULL);
1385
1386         memset(buffer, 0, sizeof buffer);
1387         for(i=0; i<(sizeof buffer-1); i++)
1388         {
1389             r = recv(c, &buffer[i], 1, 0);
1390             if (r != 1)
1391                 break;
1392             if (i<4) continue;
1393             if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
1394                 buffer[i-3] == '\r' && buffer[i-1] == '\r')
1395                 break;
1396         }
1397         if (strstr(buffer, "GET /test1"))
1398         {
1399             if (!strstr(buffer, "Content-Length: 0"))
1400             {
1401                 send(c, okmsg, sizeof okmsg-1, 0);
1402                 send(c, page1, sizeof page1-1, 0);
1403             }
1404             else
1405                 send(c, notokmsg, sizeof notokmsg-1, 0);
1406         }
1407         if (strstr(buffer, "/test2"))
1408         {
1409             if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
1410             {
1411                 send(c, okmsg, sizeof okmsg-1, 0);
1412                 send(c, page1, sizeof page1-1, 0);
1413             }
1414             else
1415                 send(c, proxymsg, sizeof proxymsg-1, 0);
1416         }
1417         if (strstr(buffer, "/test3"))
1418         {
1419             if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
1420                 send(c, okmsg, sizeof okmsg-1, 0);
1421             else
1422                 send(c, noauthmsg, sizeof noauthmsg-1, 0);
1423         }
1424         if (strstr(buffer, "/test4"))
1425         {
1426             if (strstr(buffer, "Connection: Close"))
1427                 send(c, okmsg, sizeof okmsg-1, 0);
1428             else
1429                 send(c, notokmsg, sizeof notokmsg-1, 0);
1430         }
1431         if (strstr(buffer, "POST /test5") ||
1432             strstr(buffer, "RPC_IN_DATA /test5") ||
1433             strstr(buffer, "RPC_OUT_DATA /test5"))
1434         {
1435             if (strstr(buffer, "Content-Length: 0"))
1436             {
1437                 send(c, okmsg, sizeof okmsg-1, 0);
1438                 send(c, page1, sizeof page1-1, 0);
1439             }
1440             else
1441                 send(c, notokmsg, sizeof notokmsg-1, 0);
1442         }
1443         if (strstr(buffer, "GET /test6"))
1444         {
1445             send(c, contmsg, sizeof contmsg-1, 0);
1446             send(c, contmsg, sizeof contmsg-1, 0);
1447             send(c, okmsg, sizeof okmsg-1, 0);
1448             send(c, page1, sizeof page1-1, 0);
1449         }
1450         if (strstr(buffer, "POST /test7"))
1451         {
1452             if (strstr(buffer, "Content-Length: 100"))
1453             {
1454                 send(c, okmsg, sizeof okmsg-1, 0);
1455                 send(c, page1, sizeof page1-1, 0);
1456             }
1457             else
1458                 send(c, notokmsg, sizeof notokmsg-1, 0);
1459         }
1460         if (strstr(buffer, "/test8"))
1461         {
1462             if (!strstr(buffer, "Connection: Close") &&
1463                  strstr(buffer, "Connection: Keep-Alive") &&
1464                 !strstr(buffer, "Cache-Control: no-cache") &&
1465                 !strstr(buffer, "Pragma: no-cache") &&
1466                  strstr(buffer, host_header))
1467                 send(c, okmsg, sizeof okmsg-1, 0);
1468             else
1469                 send(c, notokmsg, sizeof notokmsg-1, 0);
1470         }
1471         if (strstr(buffer, "/test9"))
1472         {
1473             if (!strstr(buffer, "Connection: Close") &&
1474                 !strstr(buffer, "Connection: Keep-Alive") &&
1475                 !strstr(buffer, "Cache-Control: no-cache") &&
1476                 !strstr(buffer, "Pragma: no-cache") &&
1477                  strstr(buffer, host_header))
1478                 send(c, okmsg, sizeof okmsg-1, 0);
1479             else
1480                 send(c, notokmsg, sizeof notokmsg-1, 0);
1481         }
1482         if (strstr(buffer, "/testA"))
1483         {
1484             if (!strstr(buffer, "Connection: Close") &&
1485                 !strstr(buffer, "Connection: Keep-Alive") &&
1486                 (strstr(buffer, "Cache-Control: no-cache") ||
1487                  strstr(buffer, "Pragma: no-cache")) &&
1488                  strstr(buffer, host_header))
1489                 send(c, okmsg, sizeof okmsg-1, 0);
1490             else
1491                 send(c, notokmsg, sizeof notokmsg-1, 0);
1492         }
1493         if (!test_b && strstr(buffer, "/testB HTTP/1.1"))
1494         {
1495             test_b = 1;
1496             send(c, okmsg, sizeof okmsg-1, 0);
1497             recvfrom(c, buffer, sizeof buffer, 0, NULL, NULL);
1498             send(c, okmsg, sizeof okmsg-1, 0);
1499         }
1500         if (strstr(buffer, "/testC"))
1501         {
1502             if (strstr(buffer, "Cookie: cookie=biscuit"))
1503                 send(c, okmsg, sizeof okmsg-1, 0);
1504             else
1505                 send(c, notokmsg, sizeof notokmsg-1, 0);
1506         }
1507         if (strstr(buffer, "/testD"))
1508         {
1509             send(c, okmsg2, sizeof okmsg2-1, 0);
1510         }
1511         if (strstr(buffer, "/testE"))
1512         {
1513             send(c, noauthmsg2, sizeof noauthmsg2-1, 0);
1514         }
1515         if (strstr(buffer, "GET /quit"))
1516         {
1517             send(c, okmsg, sizeof okmsg-1, 0);
1518             send(c, page1, sizeof page1-1, 0);
1519             last_request = 1;
1520         }
1521
1522         shutdown(c, 2);
1523         closesocket(c);
1524     } while (!last_request);
1525
1526     closesocket(s);
1527
1528     return 0;
1529 }
1530
1531 static void test_basic_request(int port, const char *verb, const char *url)
1532 {
1533     HINTERNET hi, hc, hr;
1534     DWORD r, count;
1535     char buffer[0x100];
1536
1537     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1538     ok(hi != NULL, "open failed\n");
1539
1540     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1541     ok(hc != NULL, "connect failed\n");
1542
1543     hr = HttpOpenRequest(hc, verb, url, NULL, NULL, NULL, 0, 0);
1544     ok(hr != NULL, "HttpOpenRequest failed\n");
1545
1546     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1547     ok(r, "HttpSendRequest failed\n");
1548
1549     count = 0;
1550     memset(buffer, 0, sizeof buffer);
1551     r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1552     ok(r, "InternetReadFile failed\n");
1553     ok(count == sizeof page1 - 1, "count was wrong\n");
1554     ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
1555
1556     InternetCloseHandle(hr);
1557     InternetCloseHandle(hc);
1558     InternetCloseHandle(hi);
1559 }
1560
1561 static void test_proxy_indirect(int port)
1562 {
1563     HINTERNET hi, hc, hr;
1564     DWORD r, sz, val;
1565     char buffer[0x40];
1566
1567     hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1568     ok(hi != NULL, "open failed\n");
1569
1570     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1571     ok(hc != NULL, "connect failed\n");
1572
1573     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1574     ok(hr != NULL, "HttpOpenRequest failed\n");
1575
1576     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1577     ok(r, "HttpSendRequest failed\n");
1578
1579     sz = sizeof buffer;
1580     r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1581     ok(r, "HttpQueryInfo failed\n");
1582     ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1583
1584     sz = sizeof buffer;
1585     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1586     ok(r, "HttpQueryInfo failed\n");
1587     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1588
1589     sz = sizeof val;
1590     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1591     ok(r, "HttpQueryInfo failed\n");
1592     ok(val == 407, "proxy code wrong\n");
1593
1594     sz = sizeof buffer;
1595     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1596     ok(r, "HttpQueryInfo failed\n");
1597     ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1598
1599     sz = sizeof buffer;
1600     r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1601     ok(r, "HttpQueryInfo failed\n");
1602     ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1603
1604     sz = sizeof buffer;
1605     r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1606     ok(r, "HttpQueryInfo failed\n");
1607     ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1608
1609     sz = sizeof buffer;
1610     r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1611     ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1612     ok(r == FALSE, "HttpQueryInfo failed\n");
1613
1614     InternetCloseHandle(hr);
1615     InternetCloseHandle(hc);
1616     InternetCloseHandle(hi);
1617 }
1618
1619 static void test_proxy_direct(int port)
1620 {
1621     HINTERNET hi, hc, hr;
1622     DWORD r, sz;
1623     char buffer[0x40];
1624     static CHAR username[] = "mike",
1625                 password[] = "1101";
1626
1627     sprintf(buffer, "localhost:%d\n", port);
1628     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1629     ok(hi != NULL, "open failed\n");
1630
1631     /* try connect without authorization */
1632     hc = InternetConnect(hi, "test.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1633     ok(hc != NULL, "connect failed\n");
1634
1635     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1636     ok(hr != NULL, "HttpOpenRequest failed\n");
1637
1638     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1639     ok(r, "HttpSendRequest failed\n");
1640
1641     sz = sizeof buffer;
1642     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1643     ok(r, "HttpQueryInfo failed\n");
1644     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1645
1646
1647     /* set the user + password then try again */
1648     todo_wine {
1649     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
1650     ok(r, "failed to set user\n");
1651
1652     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
1653     ok(r, "failed to set password\n");
1654     }
1655
1656     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1657     ok(r, "HttpSendRequest failed\n");
1658     sz = sizeof buffer;
1659     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1660     ok(r, "HttpQueryInfo failed\n");
1661     todo_wine {
1662     ok(!strcmp(buffer, "200"), "proxy code wrong\n");
1663     }
1664
1665
1666     InternetCloseHandle(hr);
1667     InternetCloseHandle(hc);
1668     InternetCloseHandle(hi);
1669 }
1670
1671 static void test_header_handling_order(int port)
1672 {
1673     static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
1674     static char connection[]    = "Connection: Close";
1675
1676     static const char *types[2] = { "*", NULL };
1677     HINTERNET session, connect, request;
1678     DWORD size, status;
1679     BOOL ret;
1680
1681     session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1682     ok(session != NULL, "InternetOpen failed\n");
1683
1684     connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1685     ok(connect != NULL, "InternetConnect failed\n");
1686
1687     request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1688     ok(request != NULL, "HttpOpenRequest failed\n");
1689
1690     ret = HttpAddRequestHeaders(request, authorization, ~0u, HTTP_ADDREQ_FLAG_ADD);
1691     ok(ret, "HttpAddRequestHeaders failed\n");
1692
1693     ret = HttpSendRequest(request, NULL, 0, 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, "request failed with status %u\n", status);
1701
1702     InternetCloseHandle(request);
1703
1704     request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1705     ok(request != NULL, "HttpOpenRequest failed\n");
1706
1707     ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
1708     ok(ret, "HttpSendRequest failed\n");
1709
1710     status = 0;
1711     size = sizeof(status);
1712     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1713     ok(ret, "HttpQueryInfo failed\n");
1714     ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
1715
1716     InternetCloseHandle(request);
1717
1718     request = HttpOpenRequest(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1719     ok(request != NULL, "HttpOpenRequest failed\n");
1720
1721     ret = HttpAddRequestHeaders(request, "Content-Length: 100\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1722     ok(ret, "HttpAddRequestHeaders failed\n");
1723
1724     ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
1725     ok(ret, "HttpSendRequest failed\n");
1726
1727     status = 0;
1728     size = sizeof(status);
1729     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1730     ok(ret, "HttpQueryInfo failed\n");
1731     ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
1732
1733     InternetCloseHandle(request);
1734     InternetCloseHandle(connect);
1735     InternetCloseHandle(session);
1736 }
1737
1738 static void test_connection_header(int port)
1739 {
1740     HINTERNET ses, con, req;
1741     DWORD size, status;
1742     BOOL ret;
1743
1744     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1745     ok(ses != NULL, "InternetOpen failed\n");
1746
1747     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1748     ok(con != NULL, "InternetConnect failed\n");
1749
1750     req = HttpOpenRequest(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 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, 0, 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, NULL, "/test9", 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
1792     req = HttpOpenRequest(con, "POST", "/testA", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1793     ok(req != NULL, "HttpOpenRequest failed\n");
1794
1795     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1796     ok(ret, "HttpSendRequest failed\n");
1797
1798     status = 0;
1799     size = sizeof(status);
1800     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1801     ok(ret, "HttpQueryInfo failed\n");
1802     ok(status == 200, "request failed with status %u\n", status);
1803
1804     InternetCloseHandle(req);
1805     InternetCloseHandle(con);
1806     InternetCloseHandle(ses);
1807 }
1808
1809 static void test_http1_1(int port)
1810 {
1811     HINTERNET ses, con, req;
1812     BOOL ret;
1813
1814     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1815     ok(ses != NULL, "InternetOpen failed\n");
1816
1817     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1818     ok(con != NULL, "InternetConnect failed\n");
1819
1820     req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1821     ok(req != NULL, "HttpOpenRequest failed\n");
1822
1823     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1824     if (ret)
1825     {
1826         InternetCloseHandle(req);
1827
1828         req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1829         ok(req != NULL, "HttpOpenRequest failed\n");
1830
1831         ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1832         todo_wine
1833         ok(ret, "HttpSendRequest failed\n");
1834     }
1835
1836     InternetCloseHandle(req);
1837     InternetCloseHandle(con);
1838     InternetCloseHandle(ses);
1839 }
1840
1841 static void test_HttpSendRequestW(int port)
1842 {
1843     static const WCHAR header[] = {'U','A','-','C','P','U',':',' ','x','8','6',0};
1844     HINTERNET ses, con, req;
1845     DWORD error;
1846     BOOL ret;
1847
1848     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
1849     ok(ses != NULL, "InternetOpen failed\n");
1850
1851     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1852     ok(con != NULL, "InternetConnect failed\n");
1853
1854     req = HttpOpenRequest(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
1855     ok(req != NULL, "HttpOpenRequest failed\n");
1856
1857     SetLastError(0xdeadbeef);
1858     ret = HttpSendRequestW(req, header, ~0u, NULL, 0);
1859     error = GetLastError();
1860     ok(!ret, "HttpSendRequestW succeeded\n");
1861     ok(error == ERROR_IO_PENDING ||
1862        broken(error == ERROR_HTTP_HEADER_NOT_FOUND) ||  /* IE6 */
1863        broken(error == ERROR_INVALID_PARAMETER),        /* IE5 */
1864        "got %u expected ERROR_IO_PENDING\n", error);
1865
1866     InternetCloseHandle(req);
1867     InternetCloseHandle(con);
1868     InternetCloseHandle(ses);
1869 }
1870
1871 static void test_cookie_header(int port)
1872 {
1873     HINTERNET ses, con, req;
1874     DWORD size, status, error;
1875     BOOL ret;
1876     char buffer[64];
1877
1878     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1879     ok(ses != NULL, "InternetOpen failed\n");
1880
1881     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1882     ok(con != NULL, "InternetConnect failed\n");
1883
1884     InternetSetCookie("http://localhost", "cookie", "biscuit");
1885
1886     req = HttpOpenRequest(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1887     ok(req != NULL, "HttpOpenRequest failed\n");
1888
1889     buffer[0] = 0;
1890     size = sizeof(buffer);
1891     SetLastError(0xdeadbeef);
1892     ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1893     error = GetLastError();
1894     ok(!ret, "HttpQueryInfo succeeded\n");
1895     ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %u expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
1896
1897     ret = HttpAddRequestHeaders(req, "Cookie: cookie=not biscuit\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD);
1898     ok(ret, "HttpAddRequestHeaders failed: %u\n", GetLastError());
1899
1900     buffer[0] = 0;
1901     size = sizeof(buffer);
1902     ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1903     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
1904     ok(!strcmp(buffer, "cookie=not biscuit"), "got '%s' expected \'cookie=not biscuit\'\n", buffer);
1905
1906     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1907     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1908
1909     status = 0;
1910     size = sizeof(status);
1911     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1912     ok(ret, "HttpQueryInfo failed\n");
1913     ok(status == 200, "request failed with status %u\n", status);
1914
1915     buffer[0] = 0;
1916     size = sizeof(buffer);
1917     ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1918     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
1919     ok(!strcmp(buffer, "cookie=biscuit"), "got '%s' expected \'cookie=biscuit\'\n", buffer);
1920
1921     InternetCloseHandle(req);
1922     InternetCloseHandle(con);
1923     InternetCloseHandle(ses);
1924 }
1925
1926 static void test_basic_authentication(int port)
1927 {
1928     HINTERNET session, connect, request;
1929     DWORD size, status;
1930     BOOL ret;
1931
1932     session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1933     ok(session != NULL, "InternetOpen failed\n");
1934
1935     connect = InternetConnect(session, "localhost", port, "user", "pwd", INTERNET_SERVICE_HTTP, 0, 0);
1936     ok(connect != NULL, "InternetConnect failed\n");
1937
1938     request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, NULL, 0, 0);
1939     ok(request != NULL, "HttpOpenRequest failed\n");
1940
1941     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1942     ok(ret, "HttpSendRequest failed %u\n", GetLastError());
1943
1944     status = 0;
1945     size = sizeof(status);
1946     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1947     ok(ret, "HttpQueryInfo failed\n");
1948     ok(status == 200, "request failed with status %u\n", status);
1949
1950     InternetCloseHandle(request);
1951     InternetCloseHandle(connect);
1952     InternetCloseHandle(session);
1953 }
1954
1955 static void test_invalid_response_headers(int port)
1956 {
1957     HINTERNET session, connect, request;
1958     DWORD size, status;
1959     BOOL ret;
1960     char buffer[256];
1961
1962     session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1963     ok(session != NULL, "InternetOpen failed\n");
1964
1965     connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1966     ok(connect != NULL, "InternetConnect failed\n");
1967
1968     request = HttpOpenRequest(connect, NULL, "/testE", NULL, NULL, NULL, 0, 0);
1969     ok(request != NULL, "HttpOpenRequest failed\n");
1970
1971     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1972     ok(ret, "HttpSendRequest failed %u\n", GetLastError());
1973
1974     status = 0;
1975     size = sizeof(status);
1976     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1977     ok(ret, "HttpQueryInfo failed\n");
1978     ok(status == 401, "unexpected status %u\n", status);
1979
1980     buffer[0] = 0;
1981     size = sizeof(buffer);
1982     ret = HttpQueryInfo( request, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
1983     ok(ret, "HttpQueryInfo failed\n");
1984     ok(!strcmp(buffer, "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"),
1985        "headers wrong \"%s\"\n", buffer);
1986
1987     buffer[0] = 0;
1988     size = sizeof(buffer);
1989     ret = HttpQueryInfo( request, HTTP_QUERY_SERVER, buffer, &size, NULL);
1990     ok(ret, "HttpQueryInfo failed\n");
1991     ok(!strcmp(buffer, "winetest"), "server wrong \"%s\"\n", buffer);
1992
1993     InternetCloseHandle(request);
1994     InternetCloseHandle(connect);
1995     InternetCloseHandle(session);
1996 }
1997
1998 static void test_HttpQueryInfo(int port)
1999 {
2000     HINTERNET hi, hc, hr;
2001     DWORD size, index;
2002     char buffer[1024];
2003     BOOL ret;
2004
2005     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2006     ok(hi != NULL, "InternetOpen failed\n");
2007
2008     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2009     ok(hc != NULL, "InternetConnect failed\n");
2010
2011     hr = HttpOpenRequest(hc, NULL, "/testD", NULL, NULL, NULL, 0, 0);
2012     ok(hr != NULL, "HttpOpenRequest failed\n");
2013
2014     ret = HttpSendRequest(hr, NULL, 0, NULL, 0);
2015     ok(ret, "HttpSendRequest failed\n");
2016
2017     index = 0;
2018     size = sizeof(buffer);
2019     ret = HttpQueryInfo(hr, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &index);
2020     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2021     ok(index == 1, "expected 1 got %u\n", index);
2022
2023     index = 0;
2024     size = sizeof(buffer);
2025     ret = HttpQueryInfo(hr, HTTP_QUERY_DATE | HTTP_QUERY_FLAG_SYSTEMTIME, buffer, &size, &index);
2026     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2027     ok(index == 1, "expected 1 got %u\n", index);
2028
2029     index = 0;
2030     size = sizeof(buffer);
2031     ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
2032     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2033     ok(index == 0, "expected 0 got %u\n", index);
2034
2035     size = sizeof(buffer);
2036     ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
2037     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2038     ok(index == 0, "expected 0 got %u\n", index);
2039
2040     size = sizeof(buffer);
2041     ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &size, &index);
2042     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2043     ok(index == 0, "expected 0 got %u\n", index);
2044
2045     size = sizeof(buffer);
2046     ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
2047     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2048     ok(index == 0, "expected 0 got %u\n", index);
2049
2050     size = sizeof(buffer);
2051     ret = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &size, &index);
2052     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2053     ok(index == 0, "expected 0 got %u\n", index);
2054
2055     index = 0;
2056     size = sizeof(buffer);
2057     ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &size, &index);
2058     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2059     ok(index == 0, "expected 0 got %u\n", index);
2060
2061     index = 0;
2062     size = sizeof(buffer);
2063     ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, buffer, &size, &index);
2064     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2065     ok(index == 0, "expected 0 got %u\n", index);
2066
2067     index = 0xdeadbeef;
2068     size = sizeof(buffer);
2069     ret = HttpQueryInfo(hr, HTTP_QUERY_FORWARDED, buffer, &size, &index);
2070     ok(!ret, "HttpQueryInfo succeeded\n");
2071     ok(index == 0xdeadbeef, "expected 0xdeadbeef got %u\n", index);
2072
2073     index = 0;
2074     size = sizeof(buffer);
2075     ret = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &size, &index);
2076     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2077     ok(index == 1, "expected 1 got %u\n", index);
2078
2079     index = 0;
2080     size = sizeof(buffer);
2081     strcpy(buffer, "Server");
2082     ret = HttpQueryInfo(hr, HTTP_QUERY_CUSTOM, buffer, &size, &index);
2083     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2084     ok(index == 1, "expected 1 got %u\n", index);
2085
2086     index = 0;
2087     size = sizeof(buffer);
2088     ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
2089     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2090     ok(index == 1, "expected 1 got %u\n", index);
2091
2092     size = sizeof(buffer);
2093     ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
2094     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2095     ok(index == 2, "expected 2 got %u\n", index);
2096
2097     InternetCloseHandle(hr);
2098     InternetCloseHandle(hc);
2099     InternetCloseHandle(hi);
2100 }
2101
2102 static void test_http_connection(void)
2103 {
2104     struct server_info si;
2105     HANDLE hThread;
2106     DWORD id = 0, r;
2107
2108     si.hEvent = CreateEvent(NULL, 0, 0, NULL);
2109     si.port = 7531;
2110
2111     hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
2112     ok( hThread != NULL, "create thread failed\n");
2113
2114     r = WaitForSingleObject(si.hEvent, 10000);
2115     ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
2116     if (r != WAIT_OBJECT_0)
2117         return;
2118
2119     test_basic_request(si.port, "GET", "/test1");
2120     test_proxy_indirect(si.port);
2121     test_proxy_direct(si.port);
2122     test_header_handling_order(si.port);
2123     test_basic_request(si.port, "POST", "/test5");
2124     test_basic_request(si.port, "RPC_IN_DATA", "/test5");
2125     test_basic_request(si.port, "RPC_OUT_DATA", "/test5");
2126     test_basic_request(si.port, "GET", "/test6");
2127     test_connection_header(si.port);
2128     test_http1_1(si.port);
2129     test_cookie_header(si.port);
2130     test_basic_authentication(si.port);
2131     test_invalid_response_headers(si.port);
2132     test_HttpQueryInfo(si.port);
2133     test_HttpSendRequestW(si.port);
2134
2135     /* send the basic request again to shutdown the server thread */
2136     test_basic_request(si.port, "GET", "/quit");
2137
2138     r = WaitForSingleObject(hThread, 3000);
2139     ok( r == WAIT_OBJECT_0, "thread wait failed\n");
2140     CloseHandle(hThread);
2141 }
2142
2143 static void test_user_agent_header(void)
2144 {
2145     HINTERNET ses, con, req;
2146     DWORD size, err;
2147     char buffer[64];
2148     BOOL ret;
2149
2150     ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2151     ok(ses != NULL, "InternetOpen failed\n");
2152
2153     con = InternetConnect(ses, "test.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2154     ok(con != NULL, "InternetConnect failed\n");
2155
2156     req = HttpOpenRequest(con, "GET", "/hello.html", "HTTP/1.0", NULL, NULL, 0, 0);
2157     ok(req != NULL, "HttpOpenRequest failed\n");
2158
2159     size = sizeof(buffer);
2160     ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2161     err = GetLastError();
2162     ok(!ret, "HttpQueryInfo succeeded\n");
2163     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2164
2165     ret = HttpAddRequestHeaders(req, "User-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2166     ok(ret, "HttpAddRequestHeaders succeeded\n");
2167
2168     size = sizeof(buffer);
2169     ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2170     err = GetLastError();
2171     ok(ret, "HttpQueryInfo failed\n");
2172     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2173
2174     InternetCloseHandle(req);
2175
2176     req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
2177     ok(req != NULL, "HttpOpenRequest failed\n");
2178
2179     size = sizeof(buffer);
2180     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2181     err = GetLastError();
2182     ok(!ret, "HttpQueryInfo succeeded\n");
2183     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2184
2185     ret = HttpAddRequestHeaders(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2186     ok(ret, "HttpAddRequestHeaders failed\n");
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, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
2193
2194     InternetCloseHandle(req);
2195     InternetCloseHandle(con);
2196     InternetCloseHandle(ses);
2197 }
2198
2199 static void test_bogus_accept_types_array(void)
2200 {
2201     HINTERNET ses, con, req;
2202     static const char *types[] = { (const char *)6240, "*/*", "%p", "", (const char *)0xffffffff, "*/*", NULL };
2203     DWORD size;
2204     char buffer[32];
2205     BOOL ret;
2206
2207     ses = InternetOpen("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
2208     con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2209     req = HttpOpenRequest(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
2210
2211     ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
2212
2213     buffer[0] = 0;
2214     size = sizeof(buffer);
2215     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2216     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2217     ok(!strcmp(buffer, ", */*, %p, , , */*") || /* IE6 */
2218        !strcmp(buffer, "*/*, %p, */*"),
2219        "got '%s' expected '*/*, %%p, */*' or ', */*, %%p, , , */*'\n", buffer);
2220
2221     InternetCloseHandle(req);
2222     InternetCloseHandle(con);
2223     InternetCloseHandle(ses);
2224 }
2225
2226 struct context
2227 {
2228     HANDLE event;
2229     HINTERNET req;
2230 };
2231
2232 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
2233 {
2234     INTERNET_ASYNC_RESULT *result = info;
2235     struct context *ctx = (struct context *)context;
2236
2237     trace("%p 0x%08lx %u %p 0x%08x\n", handle, context, status, info, size);
2238
2239     if (status == INTERNET_STATUS_REQUEST_COMPLETE)
2240     {
2241         trace("request handle: 0x%08lx\n", result->dwResult);
2242         ctx->req = (HINTERNET)result->dwResult;
2243         SetEvent(ctx->event);
2244     }
2245     if (status == INTERNET_STATUS_HANDLE_CLOSING)
2246     {
2247         DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
2248
2249         if (InternetQueryOption(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
2250             ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
2251         SetEvent(ctx->event);
2252     }
2253 }
2254
2255 static void test_open_url_async(void)
2256 {
2257     BOOL ret;
2258     HINTERNET ses, req;
2259     DWORD size, error;
2260     struct context ctx;
2261     ULONG type;
2262
2263     ctx.req = NULL;
2264     ctx.event = CreateEvent(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
2265
2266     ses = InternetOpen("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
2267     ok(ses != NULL, "InternetOpen failed\n");
2268
2269     SetLastError(0xdeadbeef);
2270     ret = InternetSetOptionA(NULL, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
2271     error = GetLastError();
2272     ok(!ret, "InternetSetOptionA succeeded\n");
2273     ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "got %u expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE\n", error);
2274
2275     ret = InternetSetOptionA(ses, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
2276     error = GetLastError();
2277     ok(!ret, "InternetSetOptionA failed\n");
2278     ok(error == ERROR_INTERNET_OPTION_NOT_SETTABLE, "got %u expected ERROR_INTERNET_OPTION_NOT_SETTABLE\n", error);
2279
2280     pInternetSetStatusCallbackA(ses, cb);
2281     ResetEvent(ctx.event);
2282
2283     req = InternetOpenUrl(ses, "http://test.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
2284     ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
2285
2286     WaitForSingleObject(ctx.event, INFINITE);
2287
2288     type = 0;
2289     size = sizeof(type);
2290     ret = InternetQueryOption(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
2291     ok(ret, "InternetQueryOption failed: %u\n", GetLastError());
2292     ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
2293        "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %u\n", type);
2294
2295     size = 0;
2296     ret = HttpQueryInfo(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
2297     ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
2298     ok(size > 0, "expected size > 0\n");
2299
2300     ResetEvent(ctx.event);
2301     InternetCloseHandle(ctx.req);
2302     WaitForSingleObject(ctx.event, INFINITE);
2303
2304     InternetCloseHandle(ses);
2305     CloseHandle(ctx.event);
2306 }
2307
2308 enum api
2309 {
2310     internet_connect = 1,
2311     http_open_request,
2312     http_send_request_ex,
2313     internet_writefile,
2314     http_end_request,
2315     internet_close_handle
2316 };
2317
2318 struct notification
2319 {
2320     enum api     function; /* api responsible for notification */
2321     unsigned int status;   /* status received */
2322     int          async;    /* delivered from another thread? */
2323     int          todo;
2324 };
2325
2326 struct info
2327 {
2328     enum api     function;
2329     const struct notification *test;
2330     unsigned int count;
2331     unsigned int index;
2332     HANDLE       wait;
2333     DWORD        thread;
2334     unsigned int line;
2335 };
2336
2337 static CRITICAL_SECTION notification_cs;
2338
2339 static void CALLBACK check_notification( HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID buffer, DWORD buflen )
2340 {
2341     BOOL status_ok, function_ok;
2342     struct info *info = (struct info *)context;
2343     unsigned int i;
2344
2345     EnterCriticalSection( &notification_cs );
2346
2347     if (status == INTERNET_STATUS_HANDLE_CREATED)
2348     {
2349         DWORD size = sizeof(struct info *);
2350         HttpQueryInfoA( handle, INTERNET_OPTION_CONTEXT_VALUE, &info, &size, 0 );
2351     }
2352     i = info->index;
2353     if (i >= info->count)
2354     {
2355         LeaveCriticalSection( &notification_cs );
2356         return;
2357     }
2358
2359     status_ok   = (info->test[i].status == status);
2360     function_ok = (info->test[i].function == info->function);
2361
2362     if (!info->test[i].todo)
2363     {
2364         ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
2365         ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
2366
2367         if (info->test[i].async)
2368             ok(info->thread != GetCurrentThreadId(), "%u: expected thread %u got %u\n",
2369                info->line, info->thread, GetCurrentThreadId());
2370     }
2371     else
2372     {
2373         todo_wine ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
2374         if (status_ok)
2375             todo_wine ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
2376     }
2377     if (i == info->count - 1 || info->test[i].function != info->test[i + 1].function) SetEvent( info->wait );
2378     info->index++;
2379
2380     LeaveCriticalSection( &notification_cs );
2381 }
2382
2383 static void setup_test( struct info *info, enum api function, unsigned int line )
2384 {
2385     info->function = function;
2386     info->line = line;
2387 }
2388
2389 static const struct notification async_send_request_ex_test[] =
2390 {
2391     { internet_connect,      INTERNET_STATUS_HANDLE_CREATED, 0 },
2392     { http_open_request,     INTERNET_STATUS_HANDLE_CREATED, 0 },
2393     { http_send_request_ex,  INTERNET_STATUS_RESOLVING_NAME, 1 },
2394     { http_send_request_ex,  INTERNET_STATUS_NAME_RESOLVED, 1 },
2395     { http_send_request_ex,  INTERNET_STATUS_CONNECTING_TO_SERVER, 1 },
2396     { http_send_request_ex,  INTERNET_STATUS_CONNECTED_TO_SERVER, 1 },
2397     { http_send_request_ex,  INTERNET_STATUS_SENDING_REQUEST, 1 },
2398     { http_send_request_ex,  INTERNET_STATUS_REQUEST_SENT, 1 },
2399     { http_send_request_ex,  INTERNET_STATUS_REQUEST_COMPLETE, 1 },
2400     { internet_writefile,    INTERNET_STATUS_SENDING_REQUEST, 0 },
2401     { internet_writefile,    INTERNET_STATUS_REQUEST_SENT, 0 },
2402     { http_end_request,      INTERNET_STATUS_RECEIVING_RESPONSE, 1 },
2403     { http_end_request,      INTERNET_STATUS_RESPONSE_RECEIVED, 1 },
2404     { http_end_request,      INTERNET_STATUS_REQUEST_COMPLETE, 1 },
2405     { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, 1 },
2406     { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, 1 }
2407 };
2408
2409 static void test_async_HttpSendRequestEx(void)
2410 {
2411     BOOL ret;
2412     HINTERNET ses, req, con;
2413     struct info info;
2414     DWORD size, written, error;
2415     INTERNET_BUFFERSA b;
2416     static const char *accept[2] = {"*/*", NULL};
2417     static char data[] = "Public ID=codeweavers";
2418     char buffer[32];
2419
2420     InitializeCriticalSection( &notification_cs );
2421
2422     info.test  = async_send_request_ex_test;
2423     info.count = sizeof(async_send_request_ex_test)/sizeof(async_send_request_ex_test[0]);
2424     info.index = 0;
2425     info.wait = CreateEvent( NULL, FALSE, FALSE, NULL );
2426     info.thread = GetCurrentThreadId();
2427
2428     ses = InternetOpen( "winetest", 0, NULL, NULL, INTERNET_FLAG_ASYNC );
2429     ok( ses != NULL, "InternetOpen failed\n" );
2430
2431     pInternetSetStatusCallbackA( ses, check_notification );
2432
2433     setup_test( &info, internet_connect, __LINE__ );
2434     con = InternetConnect( ses, "crossover.codeweavers.com", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, (DWORD_PTR)&info );
2435     ok( con != NULL, "InternetConnect failed %u\n", GetLastError() );
2436
2437     WaitForSingleObject( info.wait, 10000 );
2438
2439     setup_test( &info, http_open_request, __LINE__ );
2440     req = HttpOpenRequest( con, "POST", "posttest.php", NULL, NULL, accept, 0, (DWORD_PTR)&info );
2441     ok( req != NULL, "HttpOpenRequest failed %u\n", GetLastError() );
2442
2443     WaitForSingleObject( info.wait, 10000 );
2444
2445     memset( &b, 0, sizeof(INTERNET_BUFFERSA) );
2446     b.dwStructSize = sizeof(INTERNET_BUFFERSA);
2447     b.lpcszHeader = "Content-Type: application/x-www-form-urlencoded";
2448     b.dwHeadersLength = strlen( b.lpcszHeader );
2449     b.dwBufferTotal = strlen( data );
2450
2451     setup_test( &info, http_send_request_ex, __LINE__ );
2452     ret = HttpSendRequestExA( req, &b, NULL, 0x28, 0 );
2453     ok( !ret && GetLastError() == ERROR_IO_PENDING, "HttpSendRequestExA failed %d %u\n", ret, GetLastError() );
2454
2455     WaitForSingleObject( info.wait, 10000 );
2456
2457     size = sizeof(buffer);
2458     SetLastError( 0xdeadbeef );
2459     ret = HttpQueryInfoA( req, HTTP_QUERY_CONTENT_ENCODING, buffer, &size, 0 );
2460     error = GetLastError();
2461     ok( !ret, "HttpQueryInfoA failed %u\n", GetLastError() );
2462     todo_wine
2463     ok( error == ERROR_INTERNET_INCORRECT_HANDLE_STATE,
2464         "expected ERROR_INTERNET_INCORRECT_HANDLE_STATE got %u\n", error );
2465
2466     written = 0;
2467     size = strlen( data );
2468     setup_test( &info, internet_writefile, __LINE__ );
2469     ret = InternetWriteFile( req, data, size, &written );
2470     ok( ret, "InternetWriteFile failed %u\n", GetLastError() );
2471     ok( written == size, "expected %u got %u\n", written, size );
2472
2473     WaitForSingleObject( info.wait, 10000 );
2474
2475     SetLastError( 0xdeadbeef );
2476     ret = HttpEndRequestA( req, (void *)data, 0x28, 0 );
2477     error = GetLastError();
2478     ok( !ret, "HttpEndRequestA succeeded\n" );
2479     ok( error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", error );
2480
2481     SetLastError( 0xdeadbeef );
2482     setup_test( &info, http_end_request, __LINE__ );
2483     ret = HttpEndRequestA( req, NULL, 0x28, 0 );
2484     error = GetLastError();
2485     ok( !ret, "HttpEndRequestA succeeded\n" );
2486     ok( error == ERROR_IO_PENDING, "expected ERROR_IO_PENDING got %u\n", error );
2487
2488     WaitForSingleObject( info.wait, 10000 );
2489
2490     setup_test( &info, internet_close_handle, __LINE__ );
2491     InternetCloseHandle( req );
2492     InternetCloseHandle( con );
2493     InternetCloseHandle( ses );
2494
2495     WaitForSingleObject( info.wait, 10000 );
2496     CloseHandle( info.wait );
2497 }
2498
2499 #define STATUS_STRING(status) \
2500     memcpy(status_string[status], #status, sizeof(CHAR) * \
2501            (strlen(#status) < MAX_STATUS_NAME ? \
2502             strlen(#status) : \
2503             MAX_STATUS_NAME - 1))
2504 static void init_status_tests(void)
2505 {
2506     memset(expect, 0, sizeof(expect));
2507     memset(optional, 0, sizeof(optional));
2508     memset(wine_allow, 0, sizeof(wine_allow));
2509     memset(notified, 0, sizeof(notified));
2510     memset(status_string, 0, sizeof(status_string));
2511     STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
2512     STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
2513     STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
2514     STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
2515     STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
2516     STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
2517     STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
2518     STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
2519     STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
2520     STATUS_STRING(INTERNET_STATUS_PREFETCH);
2521     STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
2522     STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
2523     STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
2524     STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
2525     STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
2526     STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
2527     STATUS_STRING(INTERNET_STATUS_REDIRECT);
2528     STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
2529     STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
2530     STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
2531     STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
2532     STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
2533     STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
2534     STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
2535     STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
2536     STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
2537 }
2538 #undef STATUS_STRING
2539
2540 START_TEST(http)
2541 {
2542     HMODULE hdll;
2543     hdll = GetModuleHandleA("wininet.dll");
2544     pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
2545
2546     if (!pInternetSetStatusCallbackA)
2547         skip("skipping the InternetReadFile tests\n");
2548     else
2549     {
2550         init_status_tests();
2551         InternetReadFile_test(INTERNET_FLAG_ASYNC);
2552         InternetReadFile_test(0);
2553         InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
2554         test_open_url_async();
2555         test_async_HttpSendRequestEx();
2556     }
2557     InternetOpenRequest_test();
2558     test_http_cache();
2559     InternetOpenUrlA_test();
2560     HttpSendRequestEx_test();
2561     HttpHeaders_test();
2562     test_http_connection();
2563     test_user_agent_header();
2564     test_bogus_accept_types_array();
2565 }