wininet: Don't send a "Connection: Close" header.
[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://www.winehq.org/site/about"
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 /* SET_WINE_ALLOW's should be used with an appropriate
47  * todo_wine CHECK_NOTIFIED at a later point in the code */
48 #define SET_WINE_ALLOW2(status, num) \
49     wine_allow[status] = num
50
51 #define SET_WINE_ALLOW(status) \
52     SET_WINE_ALLOW2(status, 1)
53
54 #define CHECK_EXPECT(status) \
55     do { \
56         if (!expect[status] && wine_allow[status]) \
57         { \
58             todo_wine ok(expect[status], "unexpected status %d (%s)\n", status, \
59                          status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
60                          status_string[status] : "unknown");            \
61             wine_allow[status]--; \
62         } \
63         else \
64         { \
65             ok(expect[status], "unexpected status %d (%s)\n", status,   \
66                status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
67                status_string[status] : "unknown");                      \
68             expect[status]--;                                           \
69         } \
70         notified[status]++; \
71     }while(0)
72
73 /* CLEAR_NOTIFIED used in cases when notification behavior
74  * differs between Windows versions */
75 #define CLEAR_NOTIFIED(status) \
76     expect[status] = wine_allow[status] = notified[status] = 0;
77
78 #define CHECK_NOTIFIED2(status, num) \
79     do { \
80         ok(notified[status] == (num), "expected status %d (%s) %d times, received %d times\n", \
81            status, status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
82            status_string[status] : "unknown", (num), notified[status]); \
83         CLEAR_NOTIFIED(status);                                         \
84     }while(0)
85
86 #define CHECK_NOTIFIED(status) \
87     CHECK_NOTIFIED2(status, 1)
88
89 #define CHECK_NOT_NOTIFIED(status) \
90     CHECK_NOTIFIED2(status, 0)
91
92 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
93 #define MAX_STATUS_NAME 50
94 static int expect[MAX_INTERNET_STATUS], wine_allow[MAX_INTERNET_STATUS],
95     notified[MAX_INTERNET_STATUS];
96 static CHAR status_string[MAX_INTERNET_STATUS][MAX_STATUS_NAME];
97
98 static HANDLE hCompleteEvent;
99
100 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
101 static BOOL (WINAPI *pInternetTimeFromSystemTimeA)(CONST SYSTEMTIME *,DWORD ,LPSTR ,DWORD);
102 static BOOL (WINAPI *pInternetTimeFromSystemTimeW)(CONST SYSTEMTIME *,DWORD ,LPWSTR ,DWORD);
103 static BOOL (WINAPI *pInternetTimeToSystemTimeA)(LPCSTR ,SYSTEMTIME *,DWORD);
104 static BOOL (WINAPI *pInternetTimeToSystemTimeW)(LPCWSTR ,SYSTEMTIME *,DWORD);
105
106
107 static VOID WINAPI callback(
108      HINTERNET hInternet,
109      DWORD_PTR dwContext,
110      DWORD dwInternetStatus,
111      LPVOID lpvStatusInformation,
112      DWORD dwStatusInformationLength
113 )
114 {
115     CHECK_EXPECT(dwInternetStatus);
116     switch (dwInternetStatus)
117     {
118         case INTERNET_STATUS_RESOLVING_NAME:
119             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
120                 GetCurrentThreadId(), hInternet, dwContext,
121                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
122             *(LPSTR)lpvStatusInformation = '\0';
123             break;
124         case INTERNET_STATUS_NAME_RESOLVED:
125             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
126                 GetCurrentThreadId(), hInternet, dwContext,
127                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
128             *(LPSTR)lpvStatusInformation = '\0';
129             break;
130         case INTERNET_STATUS_CONNECTING_TO_SERVER:
131             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
132                 GetCurrentThreadId(), hInternet, dwContext,
133                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
134             *(LPSTR)lpvStatusInformation = '\0';
135             break;
136         case INTERNET_STATUS_CONNECTED_TO_SERVER:
137             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
138                 GetCurrentThreadId(), hInternet, dwContext,
139                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
140             *(LPSTR)lpvStatusInformation = '\0';
141             break;
142         case INTERNET_STATUS_SENDING_REQUEST:
143             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
144                 GetCurrentThreadId(), hInternet, dwContext,
145                 lpvStatusInformation,dwStatusInformationLength);
146             break;
147         case INTERNET_STATUS_REQUEST_SENT:
148             ok(dwStatusInformationLength == sizeof(DWORD),
149                 "info length should be sizeof(DWORD) instead of %d\n",
150                 dwStatusInformationLength);
151             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
152                 GetCurrentThreadId(), hInternet, dwContext,
153                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
154             break;
155         case INTERNET_STATUS_RECEIVING_RESPONSE:
156             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
157                 GetCurrentThreadId(), hInternet, dwContext,
158                 lpvStatusInformation,dwStatusInformationLength);
159             break;
160         case INTERNET_STATUS_RESPONSE_RECEIVED:
161             ok(dwStatusInformationLength == sizeof(DWORD),
162                 "info length should be sizeof(DWORD) instead of %d\n",
163                 dwStatusInformationLength);
164             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
165                 GetCurrentThreadId(), hInternet, dwContext,
166                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
167             break;
168         case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
169             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
170                 GetCurrentThreadId(), hInternet,dwContext,
171                 lpvStatusInformation,dwStatusInformationLength);
172             break;
173         case INTERNET_STATUS_PREFETCH:
174             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
175                 GetCurrentThreadId(), hInternet, dwContext,
176                 lpvStatusInformation,dwStatusInformationLength);
177             break;
178         case INTERNET_STATUS_CLOSING_CONNECTION:
179             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
180                 GetCurrentThreadId(), hInternet, dwContext,
181                 lpvStatusInformation,dwStatusInformationLength);
182             break;
183         case INTERNET_STATUS_CONNECTION_CLOSED:
184             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
185                 GetCurrentThreadId(), hInternet, dwContext,
186                 lpvStatusInformation,dwStatusInformationLength);
187             break;
188         case INTERNET_STATUS_HANDLE_CREATED:
189             ok(dwStatusInformationLength == sizeof(HINTERNET),
190                 "info length should be sizeof(HINTERNET) instead of %d\n",
191                 dwStatusInformationLength);
192             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
193                 GetCurrentThreadId(), hInternet, dwContext,
194                 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
195             CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
196             SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
197             break;
198         case INTERNET_STATUS_HANDLE_CLOSING:
199             ok(dwStatusInformationLength == sizeof(HINTERNET),
200                 "info length should be sizeof(HINTERNET) instead of %d\n",
201                 dwStatusInformationLength);
202             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
203                 GetCurrentThreadId(), hInternet, dwContext,
204                 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
205             break;
206         case INTERNET_STATUS_REQUEST_COMPLETE:
207         {
208             INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
209             ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
210                 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
211                 dwStatusInformationLength);
212             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
213                 GetCurrentThreadId(), hInternet, dwContext,
214                 iar->dwResult,iar->dwError,dwStatusInformationLength);
215             SetEvent(hCompleteEvent);
216             break;
217         }
218         case INTERNET_STATUS_REDIRECT:
219             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
220                 GetCurrentThreadId(), hInternet, dwContext,
221                 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
222             *(LPSTR)lpvStatusInformation = '\0';
223             CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
224             SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
225             break;
226         case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
227             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
228                 GetCurrentThreadId(), hInternet, dwContext,
229                 lpvStatusInformation, dwStatusInformationLength);
230             break;
231         default:
232             trace("%04x:Callback %p 0x%lx %d %p %d\n",
233                 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
234                 lpvStatusInformation, dwStatusInformationLength);
235     }
236 }
237
238 static void InternetReadFile_test(int flags)
239 {
240     BOOL res;
241     DWORD rc;
242     CHAR buffer[4000];
243     DWORD length;
244     DWORD out;
245     const char *types[2] = { "*", NULL };
246     HINTERNET hi, hic = 0, hor = 0;
247
248     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
249
250     trace("Starting InternetReadFile test with flags 0x%x\n",flags);
251
252     trace("InternetOpenA <--\n");
253     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
254     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
255     trace("InternetOpenA -->\n");
256
257     if (hi == 0x0) goto abort;
258
259     pInternetSetStatusCallbackA(hi,&callback);
260
261     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
262
263     trace("InternetConnectA <--\n");
264     hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
265                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
266     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
267     trace("InternetConnectA -->\n");
268
269     if (hic == 0x0) goto abort;
270
271     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
272     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
273     SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
274     SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
275
276     trace("HttpOpenRequestA <--\n");
277     hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
278                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
279                            0xdeadbead);
280     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
281         /*
282          * If the internet name can't be resolved we are probably behind
283          * a firewall or in some other way not directly connected to the
284          * Internet. Not enough reason to fail the test. Just ignore and
285          * abort.
286          */
287     } else  {
288         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
289     }
290     trace("HttpOpenRequestA -->\n");
291
292     if (hor == 0x0) goto abort;
293
294     length = sizeof(buffer);
295     res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
296     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
297     ok(!strcmp(buffer, "http://www.winehq.org/about/"), "Wrong URL %s\n", buffer);
298
299     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
300     todo_wine
301     {
302         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
303         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
304     }
305     if (first_connection_to_test_url)
306     {
307         SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
308         SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
309     }
310     else
311     {
312         SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
313         SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
314     }
315     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
316     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
317     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
318     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
319     SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
320     SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
321     SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
322     SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
323     SET_EXPECT(INTERNET_STATUS_REDIRECT);
324     if (flags & INTERNET_FLAG_ASYNC)
325         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
326     else
327         SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
328
329     trace("HttpSendRequestA -->\n");
330     SetLastError(0xdeadbeef);
331     rc = HttpSendRequestA(hor, "", -1, NULL, 0);
332     if (flags & INTERNET_FLAG_ASYNC)
333         ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
334             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
335     else
336         ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
337            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
338     trace("HttpSendRequestA <--\n");
339
340     if (flags & INTERNET_FLAG_ASYNC)
341         WaitForSingleObject(hCompleteEvent, INFINITE);
342
343     if (first_connection_to_test_url)
344     {
345         CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
346         CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
347     }
348     else todo_wine
349     {
350         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
351         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
352     }
353     CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
354     CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
355     CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
356     CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
357     CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
358     if (flags & INTERNET_FLAG_ASYNC)
359         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
360     else
361         todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
362     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
363     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
364     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
365
366     length = 4;
367     rc = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
368     trace("Option 0x17 -> %i  %i\n",rc,out);
369
370     length = 100;
371     rc = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
372     trace("Option 0x22 -> %i  %s\n",rc,buffer);
373
374     length = 4000;
375     rc = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
376     buffer[length]=0;
377     trace("Option 0x16 -> %i  %s\n",rc,buffer);
378
379     length = sizeof(buffer);
380     res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
381     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
382     ok(!strcmp(buffer, "http://www.winehq.org/site/about"), "Wrong URL %s\n", buffer);
383
384     length = 16;
385     rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
386     trace("Option 0x5 -> %i  %s  (%u)\n",rc,buffer,GetLastError());
387
388     length = 100;
389     rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
390     buffer[length]=0;
391     trace("Option 0x1 -> %i  %s\n",rc,buffer);
392
393     SetLastError(0xdeadbeef);
394     rc = InternetReadFile(NULL, buffer, 100, &length);
395     ok(!rc, "InternetReadFile should have failed\n");
396     ok(GetLastError() == ERROR_INVALID_HANDLE,
397         "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
398         GetLastError());
399
400     length = 100;
401     trace("Entering Query loop\n");
402
403     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
404     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
405     while (TRUE)
406     {
407         if (flags & INTERNET_FLAG_ASYNC)
408             SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
409         rc = InternetQueryDataAvailable(hor,&length,0x0,0x0);
410         ok(!(rc == 0 && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
411         ok(rc != 0 || ((flags & INTERNET_FLAG_ASYNC) && GetLastError() == ERROR_IO_PENDING),
412            "InternetQueryDataAvailable failed, error %d\n", GetLastError());
413         if (flags & INTERNET_FLAG_ASYNC)
414         {
415             if (rc != 0)
416             {
417                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
418             }
419             else if (GetLastError() == ERROR_IO_PENDING)
420             {
421                 WaitForSingleObject(hCompleteEvent, INFINITE);
422                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
423                 continue;
424             }
425         }
426         if (length)
427         {
428             char *buffer;
429             buffer = HeapAlloc(GetProcessHeap(),0,length+1);
430
431             rc = InternetReadFile(hor,buffer,length,&length);
432
433             buffer[length]=0;
434
435             trace("ReadFile -> %i %i\n",rc,length);
436
437             HeapFree(GetProcessHeap(),0,buffer);
438         }
439         if (length == 0)
440             break;
441     }
442     /* WinXP does not send, but Win98 does */
443     CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
444     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
445 abort:
446     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
447     if (hor != 0x0) {
448         SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
449         SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
450         SetLastError(0xdeadbeef);
451         rc = InternetCloseHandle(hor);
452         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
453         SetLastError(0xdeadbeef);
454         rc = InternetCloseHandle(hor);
455         ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
456         ok (GetLastError() == ERROR_INVALID_HANDLE,
457             "Double close of handle should have set ERROR_INVALID_HANDLE instead of %u\n",
458             GetLastError());
459     }
460     /* We intentionally do not close the handle opened by InternetConnectA as this
461      * tickles bug #9479: native closes child internet handles when the parent handles
462      * are closed. This is verified below by checking that the number of
463      * INTERNET_STATUS_HANDLE_CLOSING notifications matches the number expected. */
464     if (hi != 0x0) {
465       SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
466       rc = InternetCloseHandle(hi);
467       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
468       if (flags & INTERNET_FLAG_ASYNC)
469           Sleep(100);
470     }
471     CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
472     if (hor != 0x0) todo_wine
473     {
474         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
475         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
476     }
477     else
478     {
479         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
480         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
481     }
482     CloseHandle(hCompleteEvent);
483     first_connection_to_test_url = FALSE;
484 }
485
486 static void InternetReadFileExA_test(int flags)
487 {
488     DWORD rc;
489     DWORD length;
490     const char *types[2] = { "*", NULL };
491     HINTERNET hi, hic = 0, hor = 0;
492     INTERNET_BUFFERS inetbuffers;
493
494     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
495
496     trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
497
498     trace("InternetOpenA <--\n");
499     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
500     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
501     trace("InternetOpenA -->\n");
502
503     if (hi == 0x0) goto abort;
504
505     pInternetSetStatusCallbackA(hi,&callback);
506
507     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
508
509     trace("InternetConnectA <--\n");
510     hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
511                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
512     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
513     trace("InternetConnectA -->\n");
514
515     if (hic == 0x0) goto abort;
516
517     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
518     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
519     SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
520     SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
521
522     trace("HttpOpenRequestA <--\n");
523     hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
524                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
525                            0xdeadbead);
526     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
527         /*
528          * If the internet name can't be resolved we are probably behind
529          * a firewall or in some other way not directly connected to the
530          * Internet. Not enough reason to fail the test. Just ignore and
531          * abort.
532          */
533     } else  {
534         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
535     }
536     trace("HttpOpenRequestA -->\n");
537
538     if (hor == 0x0) goto abort;
539
540     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
541     todo_wine
542     {
543         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
544         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
545     }
546     if (first_connection_to_test_url)
547     {
548         SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
549         SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
550     }
551     else
552     {
553         SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
554         SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
555     }
556     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
557     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
558     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
559     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
560     SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
561     SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
562     SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
563     SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
564     SET_EXPECT(INTERNET_STATUS_REDIRECT);
565     if (flags & INTERNET_FLAG_ASYNC)
566         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
567     else
568         SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
569
570     trace("HttpSendRequestA -->\n");
571     SetLastError(0xdeadbeef);
572     rc = HttpSendRequestA(hor, "", -1, NULL, 0);
573     if (flags & INTERNET_FLAG_ASYNC)
574         ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
575             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
576     else
577         ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
578            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
579     trace("HttpSendRequestA <--\n");
580
581     if (!rc && (GetLastError() == ERROR_IO_PENDING))
582         WaitForSingleObject(hCompleteEvent, INFINITE);
583
584     if (first_connection_to_test_url)
585     {
586         CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
587         CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
588     }
589     else todo_wine
590     {
591         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
592         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
593     }
594     CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
595     CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
596     CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
597     CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
598     CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
599     if (flags & INTERNET_FLAG_ASYNC)
600         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
601     else
602         todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
603     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
604     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
605     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
606
607     /* tests invalid dwStructSize */
608     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
609     inetbuffers.lpcszHeader = NULL;
610     inetbuffers.dwHeadersLength = 0;
611     inetbuffers.dwBufferLength = 10;
612     inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
613     inetbuffers.dwOffsetHigh = 1234;
614     inetbuffers.dwOffsetLow = 5678;
615     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
616     ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
617         "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
618         rc ? "TRUE" : "FALSE", GetLastError());
619     HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
620
621     /* tests to see whether lpcszHeader is used - it isn't */
622     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
623     inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
624     inetbuffers.dwHeadersLength = 255;
625     inetbuffers.dwBufferLength = 0;
626     inetbuffers.lpvBuffer = NULL;
627     inetbuffers.dwOffsetHigh = 1234;
628     inetbuffers.dwOffsetLow = 5678;
629     SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
630     SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
631     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
632     ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
633     todo_wine
634     {
635         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
636         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
637     }
638
639     rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
640     ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
641         "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
642         rc ? "TRUE" : "FALSE", GetLastError());
643
644     length = 0;
645     trace("Entering Query loop\n");
646
647     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
648     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
649     while (TRUE)
650     {
651         inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
652         inetbuffers.dwBufferLength = 1024;
653         inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
654         inetbuffers.dwOffsetHigh = 1234;
655         inetbuffers.dwOffsetLow = 5678;
656
657         SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
658         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
659         SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
660         rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
661         if (!rc)
662         {
663             if (GetLastError() == ERROR_IO_PENDING)
664             {
665                 trace("InternetReadFileEx -> PENDING\n");
666                 ok(flags & INTERNET_FLAG_ASYNC,
667                    "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
668                 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
669                 WaitForSingleObject(hCompleteEvent, INFINITE);
670                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
671                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
672             }
673             else
674             {
675                 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
676                 break;
677             }
678         }
679         else
680         {
681             trace("InternetReadFileEx -> SUCCEEDED\n");
682             CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
683             if (inetbuffers.dwBufferLength)
684             {
685                 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
686                 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
687             }
688             else
689             {
690                 /* Win98 still sends these when 0 bytes are read, WinXP does not */
691                 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
692                 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
693             }
694         }
695
696         trace("read %i bytes\n", inetbuffers.dwBufferLength);
697         ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
698
699         ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
700             "InternetReadFileEx sets offsets to 0x%x%08x\n",
701             inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
702
703         HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
704
705         if (!inetbuffers.dwBufferLength)
706             break;
707
708         length += inetbuffers.dwBufferLength;
709     }
710     ok(length > 0, "failed to read any of the document\n");
711     trace("Finished. Read %d bytes\n", length);
712
713     /* WinXP does not send, but Win98 does */
714     CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
715     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
716 abort:
717     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
718     if (hor) {
719         SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
720         SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
721         rc = InternetCloseHandle(hor);
722         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
723         rc = InternetCloseHandle(hor);
724         ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
725     }
726     if (hic) {
727         rc = InternetCloseHandle(hic);
728         ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
729     }
730     if (hi) {
731       SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
732       rc = InternetCloseHandle(hi);
733       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
734       if (flags & INTERNET_FLAG_ASYNC)
735           Sleep(100);
736       CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
737     }
738     if (hor != 0x0) todo_wine
739     {
740         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
741         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
742     }
743     else
744     {
745         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
746         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
747     }
748     CloseHandle(hCompleteEvent);
749     first_connection_to_test_url = FALSE;
750 }
751
752 static void InternetOpenUrlA_test(void)
753 {
754   HINTERNET myhinternet, myhttp;
755   char buffer[0x400];
756   DWORD size, readbytes, totalbytes=0;
757   BOOL ret;
758   
759   myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
760   ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
761   size = 0x400;
762   ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
763   ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
764
765   SetLastError(0);
766   myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
767                            INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
768   if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
769     return; /* WinXP returns this when not connected to the net */
770   ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
771   ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
772   ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
773   totalbytes += readbytes;
774   while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
775     totalbytes += readbytes;
776   trace("read 0x%08x bytes\n",totalbytes);
777
778   InternetCloseHandle(myhttp);
779   InternetCloseHandle(myhinternet);
780 }
781
782 static void InternetTimeFromSystemTimeA_test(void)
783 {
784     BOOL ret;
785     static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
786     char string[INTERNET_RFC1123_BUFSIZE];
787     static const char expect[] = "Fri, 07 Jan 2005 12:06:35 GMT";
788
789     ret = pInternetTimeFromSystemTimeA( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
790     ok( ret, "InternetTimeFromSystemTimeA failed (%u)\n", GetLastError() );
791
792     ok( !memcmp( string, expect, sizeof(expect) ),
793         "InternetTimeFromSystemTimeA failed (%u)\n", GetLastError() );
794 }
795
796 static void InternetTimeFromSystemTimeW_test(void)
797 {
798     BOOL ret;
799     static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
800     WCHAR string[INTERNET_RFC1123_BUFSIZE + 1];
801     static const WCHAR expect[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
802                                     '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
803
804     ret = pInternetTimeFromSystemTimeW( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
805     ok( ret, "InternetTimeFromSystemTimeW failed (%u)\n", GetLastError() );
806
807     ok( !memcmp( string, expect, sizeof(expect) ),
808         "InternetTimeFromSystemTimeW failed (%u)\n", GetLastError() );
809 }
810
811 static void InternetTimeToSystemTimeA_test(void)
812 {
813     BOOL ret;
814     SYSTEMTIME time;
815     static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
816     static const char string[] = "Fri, 07 Jan 2005 12:06:35 GMT";
817     static const char string2[] = " fri 7 jan 2005 12 06 35";
818
819     ret = pInternetTimeToSystemTimeA( string, &time, 0 );
820     ok( ret, "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
821     ok( !memcmp( &time, &expect, sizeof(expect) ),
822         "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
823
824     ret = pInternetTimeToSystemTimeA( string2, &time, 0 );
825     ok( ret, "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
826     ok( !memcmp( &time, &expect, sizeof(expect) ),
827         "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
828 }
829
830 static void InternetTimeToSystemTimeW_test(void)
831 {
832     BOOL ret;
833     SYSTEMTIME time;
834     static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
835     static const WCHAR string[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
836                                     '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
837     static const WCHAR string2[] = { ' ','f','r','i',' ','7',' ','j','a','n',' ','2','0','0','5',' ',
838                                      '1','2',' ','0','6',' ','3','5',0 };
839     static const WCHAR string3[] = { 'F','r',0 };
840
841     ret = pInternetTimeToSystemTimeW( NULL, NULL, 0 );
842     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
843
844     ret = pInternetTimeToSystemTimeW( NULL, &time, 0 );
845     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
846
847     ret = pInternetTimeToSystemTimeW( string, NULL, 0 );
848     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
849
850     ret = pInternetTimeToSystemTimeW( string, &time, 0 );
851     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
852
853     ret = pInternetTimeToSystemTimeW( string, &time, 0 );
854     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
855     ok( !memcmp( &time, &expect, sizeof(expect) ),
856         "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
857
858     ret = pInternetTimeToSystemTimeW( string2, &time, 0 );
859     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
860     ok( !memcmp( &time, &expect, sizeof(expect) ),
861         "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
862
863     ret = pInternetTimeToSystemTimeW( string3, &time, 0 );
864     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
865 }
866
867 static void HttpSendRequestEx_test(void)
868 {
869     HINTERNET hSession;
870     HINTERNET hConnect;
871     HINTERNET hRequest;
872
873     INTERNET_BUFFERS BufferIn;
874     DWORD dwBytesWritten;
875     DWORD dwBytesRead;
876     CHAR szBuffer[256];
877     int i;
878     BOOL ret;
879
880     static char szPostData[] = "mode=Test";
881     static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
882
883     hSession = InternetOpen("Wine Regression Test",
884             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
885     ok( hSession != NULL ,"Unable to open Internet session\n");
886     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
887             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
888             0);
889     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
890     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
891             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
892     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
893     {
894         trace( "Network unreachable, skipping test\n" );
895         goto done;
896     }
897     ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
898
899
900     BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
901     BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
902     BufferIn.lpcszHeader = szContentType;
903     BufferIn.dwHeadersLength = sizeof(szContentType)-1;
904     BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
905     BufferIn.lpvBuffer = szPostData;
906     BufferIn.dwBufferLength = 3;
907     BufferIn.dwBufferTotal = sizeof(szPostData)-1;
908     BufferIn.dwOffsetLow = 0;
909     BufferIn.dwOffsetHigh = 0;
910
911     ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
912     ok(ret, "HttpSendRequestEx Failed with error %u\n", GetLastError());
913
914     for (i = 3; szPostData[i]; i++)
915         ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
916                 "InternetWriteFile failed\n");
917
918     ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
919
920     ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
921             "Unable to read response\n");
922     szBuffer[dwBytesRead] = 0;
923
924     ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
925     ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0,"Got string %s\n",szBuffer);
926
927     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
928 done:
929     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
930     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
931 }
932
933 static void InternetOpenRequest_test(void)
934 {
935     HINTERNET session, connect, request;
936     static const char *types[] = { "*", "", NULL };
937     static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
938     static const WCHAR *typesW[] = { any, empty, NULL };
939     BOOL ret;
940
941     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
942     ok(session != NULL ,"Unable to open Internet session\n");
943
944     connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
945                               INTERNET_SERVICE_HTTP, 0, 0);
946     ok(connect == NULL, "InternetConnectA should have failed\n");
947     ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
948
949     connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
950                               INTERNET_SERVICE_HTTP, 0, 0);
951     ok(connect == NULL, "InternetConnectA should have failed\n");
952     ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
953
954     connect = InternetConnectA(session, "winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
955                               INTERNET_SERVICE_HTTP, 0, 0);
956     ok(connect != NULL, "Unable to connect to http://winehq.org with error %d\n", GetLastError());
957
958     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
959     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
960     {
961         trace( "Network unreachable, skipping test\n" );
962         goto done;
963     }
964     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
965
966     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
967     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
968     ok(InternetCloseHandle(request), "Close request handle failed\n");
969
970     request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
971     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
972
973     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
974     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
975     ok(InternetCloseHandle(request), "Close request handle failed\n");
976
977 done:
978     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
979     ok(InternetCloseHandle(session), "Close session handle failed\n");
980 }
981
982 static void test_http_cache(void)
983 {
984     HINTERNET session, connect, request;
985     char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
986     DWORD size, file_size;
987     BYTE buf[100];
988     HANDLE file;
989     BOOL ret;
990
991     static const char *types[] = { "*", "", NULL };
992
993     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
994     ok(session != NULL ,"Unable to open Internet session\n");
995
996     connect = InternetConnectA(session, "www.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
997                               INTERNET_SERVICE_HTTP, 0, 0);
998     ok(connect != NULL, "Unable to connect to http://winehq.org with error %d\n", GetLastError());
999
1000     request = HttpOpenRequestA(connect, NULL, "/site/about", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
1001     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1002     {
1003         trace( "Network unreachable, skipping test\n" );
1004
1005         ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1006         ok(InternetCloseHandle(session), "Close session handle failed\n");
1007
1008         return;
1009     }
1010     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1011
1012     size = sizeof(url);
1013     ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
1014     ok(ret, "InternetQueryOptionA(INTERNET_OPTION_url) failed: %u\n", GetLastError());
1015     ok(!strcmp(url, "http://www.winehq.org/site/about"), "Wrong URL %s\n", url);
1016
1017     size = sizeof(file_name);
1018     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1019     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1020     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1021     ok(!size, "size = %d\n", size);
1022
1023     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1024     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1025
1026     size = sizeof(file_name);
1027     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1028     ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1029
1030     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1031                       FILE_ATTRIBUTE_NORMAL, NULL);
1032     ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1033     file_size = GetFileSize(file, NULL);
1034     ok(file_size == 0, "file size=%d\n", file_size);
1035
1036     ret = InternetReadFile(request, buf, sizeof(buf), &size);
1037     ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1038     ok(size == sizeof(buf), "size=%d\n", size);
1039
1040     file_size = GetFileSize(file, NULL);
1041     ok(file_size == sizeof(buf), "file size=%d\n", file_size);
1042     CloseHandle(file);
1043
1044     ok(InternetCloseHandle(request), "Close request handle failed\n");
1045
1046     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1047                       FILE_ATTRIBUTE_NORMAL, NULL);
1048     ok(file == INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
1049     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError()=%u, expected ERROR_FILE_NOT_FOUND\n", GetLastError());
1050
1051     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1052     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1053
1054     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1055     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1056     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1057     ok(!size, "size = %d\n", size);
1058
1059     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1060     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1061
1062     size = sizeof(file_name);
1063     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1064     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1065     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1066     ok(!size, "size = %d\n", size);
1067
1068     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1069                       FILE_ATTRIBUTE_NORMAL, NULL);
1070     ok(file == INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
1071     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError()=%u, expected ERROR_FILE_NOT_FOUND\n", GetLastError());
1072
1073     ok(InternetCloseHandle(request), "Close request handle failed\n");
1074
1075     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1076     ok(InternetCloseHandle(session), "Close session handle failed\n");
1077 }
1078
1079 static void HttpHeaders_test(void)
1080 {
1081     HINTERNET hSession;
1082     HINTERNET hConnect;
1083     HINTERNET hRequest;
1084     CHAR      buffer[256];
1085     DWORD     len = 256;
1086     DWORD     index = 0;
1087
1088     hSession = InternetOpen("Wine Regression Test",
1089             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1090     ok( hSession != NULL ,"Unable to open Internet session\n");
1091     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1092             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1093             0);
1094     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1095     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1096             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1097     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1098     {
1099         trace( "Network unreachable, skipping test\n" );
1100         goto done;
1101     }
1102     ok( hRequest != NULL, "Failed to open request handle\n");
1103
1104     index = 0;
1105     len = sizeof(buffer);
1106     strcpy(buffer,"Warning");
1107     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1108                buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1109     
1110     ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1111             "Failed to add new header\n");
1112
1113     index = 0;
1114     len = sizeof(buffer);
1115     strcpy(buffer,"Warning");
1116     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1117                 buffer,&len,&index),"Unable to query header\n");
1118     ok(index == 1, "Index was not incremented\n");
1119     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1120     ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1121     ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1122     len = sizeof(buffer);
1123     strcpy(buffer,"Warning");
1124     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1125                 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1126
1127     index = 0;
1128     len = 5; /* could store the string but not the NULL terminator */
1129     strcpy(buffer,"Warning");
1130     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1131                 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1132     ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1133     ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1134
1135     /* a call with NULL will fail but will return the length */
1136     index = 0;
1137     len = sizeof(buffer);
1138     SetLastError(0xdeadbeef);
1139     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1140                 NULL,&len,&index) == FALSE,"Query worked\n");
1141     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1142     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1143     ok(index == 0, "Index was incremented\n");
1144
1145     /* even for a len that is too small */
1146     index = 0;
1147     len = 15;
1148     SetLastError(0xdeadbeef);
1149     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1150                 NULL,&len,&index) == FALSE,"Query worked\n");
1151     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1152     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1153     ok(index == 0, "Index was incremented\n");
1154
1155     index = 0;
1156     len = 0;
1157     SetLastError(0xdeadbeef);
1158     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1159                 NULL,&len,&index) == FALSE,"Query worked\n");
1160     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1161     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1162     ok(index == 0, "Index was incremented\n");
1163
1164
1165     /* a working query */
1166     index = 0;
1167     len = sizeof(buffer);
1168     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1169                 buffer,&len,&index),"Unable to query header\n");
1170     /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1171     ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1172     ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1173     ok(index == 0, "Index was incremented\n");
1174
1175
1176
1177     ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1178             "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1179
1180     index = 0;
1181     len = sizeof(buffer);
1182     strcpy(buffer,"Warning");
1183     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1184                 buffer,&len,&index),"Unable to query header\n");
1185     ok(index == 1, "Index was not incremented\n");
1186     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1187     len = sizeof(buffer);
1188     strcpy(buffer,"Warning");
1189     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1190                 buffer,&len,&index),"Failed to get second header\n");
1191     ok(index == 2, "Index was not incremented\n");
1192     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1193     len = sizeof(buffer);
1194     strcpy(buffer,"Warning");
1195     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1196                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1197
1198     ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1199
1200     index = 0;
1201     len = sizeof(buffer);
1202     strcpy(buffer,"Warning");
1203     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1204                 buffer,&len,&index),"Unable to query header\n");
1205     ok(index == 1, "Index was not incremented\n");
1206     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1207     len = sizeof(buffer);
1208     strcpy(buffer,"Warning");
1209     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1210                 buffer,&len,&index),"Failed to get second header\n");
1211     ok(index == 2, "Index was not incremented\n");
1212     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1213     len = sizeof(buffer);
1214     strcpy(buffer,"Warning");
1215     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1216                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1217     
1218     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1219
1220     index = 0;
1221     len = sizeof(buffer);
1222     strcpy(buffer,"Warning");
1223     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1224                 buffer,&len,&index),"Unable to query header\n");
1225     ok(index == 1, "Index was not incremented\n");
1226     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1227     len = sizeof(buffer);
1228     strcpy(buffer,"Warning");
1229     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1230                 buffer,&len,&index),"Failed to get second header\n");
1231     ok(index == 2, "Index was not incremented\n");
1232     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1233     len = sizeof(buffer);
1234     strcpy(buffer,"Warning");
1235     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1236                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1237
1238     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1239
1240     index = 0;
1241     len = sizeof(buffer);
1242     strcpy(buffer,"Warning");
1243     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1244                 buffer,&len,&index),"Unable to query header\n");
1245     ok(index == 1, "Index was not incremented\n");
1246     ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1247     len = sizeof(buffer);
1248     strcpy(buffer,"Warning");
1249     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1250     ok(index == 2, "Index was not incremented\n");
1251     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1252     len = sizeof(buffer);
1253     strcpy(buffer,"Warning");
1254     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1255
1256     ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1257
1258     index = 0;
1259     len = sizeof(buffer);
1260     strcpy(buffer,"Warning");
1261     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1262     ok(index == 1, "Index was not incremented\n");
1263     ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1264     len = sizeof(buffer);
1265     strcpy(buffer,"Warning");
1266     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1267     ok(index == 2, "Index was not incremented\n");
1268     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1269     len = sizeof(buffer);
1270     strcpy(buffer,"Warning");
1271     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1272
1273     ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1274
1275     index = 0;
1276     len = sizeof(buffer);
1277     strcpy(buffer,"Warning");
1278     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1279     ok(index == 1, "Index was not incremented\n");
1280     ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1281     len = sizeof(buffer);
1282     strcpy(buffer,"Warning");
1283     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1284     ok(index == 2, "Index was not incremented\n");
1285     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1286     len = sizeof(buffer);
1287     strcpy(buffer,"Warning");
1288     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1289
1290     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");
1291
1292     index = 0;
1293     len = sizeof(buffer);
1294     strcpy(buffer,"Warning");
1295     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1296     ok(index == 1, "Index was not incremented\n");
1297     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1298     len = sizeof(buffer);
1299     strcpy(buffer,"Warning");
1300     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1301     ok(index == 2, "Index was not incremented\n");
1302     ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1303     len = sizeof(buffer);
1304     strcpy(buffer,"Warning");
1305     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1306
1307     
1308     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1309 done:
1310     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1311     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1312 }
1313
1314 static const char contmsg[] =
1315 "HTTP/1.1 100 Continue\r\n";
1316
1317 static const char okmsg[] =
1318 "HTTP/1.1 200 OK\r\n"
1319 "Server: winetest\r\n"
1320 "\r\n";
1321
1322 static const char notokmsg[] =
1323 "HTTP/1.1 400 Bad Request\r\n"
1324 "Server: winetest\r\n"
1325 "\r\n";
1326
1327 static const char noauthmsg[] =
1328 "HTTP/1.1 401 Unauthorized\r\n"
1329 "Server: winetest\r\n"
1330 "\r\n";
1331
1332 static const char proxymsg[] =
1333 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1334 "Server: winetest\r\n"
1335 "Proxy-Connection: close\r\n"
1336 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1337 "\r\n";
1338
1339 static const char page1[] =
1340 "<HTML>\r\n"
1341 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1342 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1343 "</HTML>\r\n\r\n";
1344
1345 struct server_info {
1346     HANDLE hEvent;
1347     int port;
1348 };
1349
1350 static DWORD CALLBACK server_thread(LPVOID param)
1351 {
1352     struct server_info *si = param;
1353     int r, c, i, on;
1354     SOCKET s;
1355     struct sockaddr_in sa;
1356     char buffer[0x100];
1357     WSADATA wsaData;
1358     int last_request = 0;
1359
1360     WSAStartup(MAKEWORD(1,1), &wsaData);
1361
1362     s = socket(AF_INET, SOCK_STREAM, 0);
1363     if (s == INVALID_SOCKET)
1364         return 1;
1365
1366     on = 1;
1367     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1368
1369     memset(&sa, 0, sizeof sa);
1370     sa.sin_family = AF_INET;
1371     sa.sin_port = htons(si->port);
1372     sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1373
1374     r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1375     if (r<0)
1376         return 1;
1377
1378     listen(s, 0);
1379
1380     SetEvent(si->hEvent);
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         {
1433             if (strstr(buffer, "Content-Length: 0"))
1434             {
1435                 send(c, okmsg, sizeof okmsg-1, 0);
1436                 send(c, page1, sizeof page1-1, 0);
1437             }
1438             else
1439                 send(c, notokmsg, sizeof notokmsg-1, 0);
1440         }
1441         if (strstr(buffer, "GET /test6"))
1442         {
1443             send(c, contmsg, sizeof contmsg-1, 0);
1444             send(c, contmsg, sizeof contmsg-1, 0);
1445             send(c, okmsg, sizeof okmsg-1, 0);
1446             send(c, page1, sizeof page1-1, 0);
1447         }
1448         if (strstr(buffer, "POST /test7"))
1449         {
1450             if (strstr(buffer, "Content-Length: 100"))
1451             {
1452                 send(c, okmsg, sizeof okmsg-1, 0);
1453                 send(c, page1, sizeof page1-1, 0);
1454             }
1455             else
1456                 send(c, notokmsg, sizeof notokmsg-1, 0);
1457         }
1458         if (strstr(buffer, "/test8"))
1459         {
1460             if (!strstr(buffer, "Connection: Close") &&
1461                  strstr(buffer, "Connection: Keep-Alive"))
1462                 send(c, okmsg, sizeof okmsg-1, 0);
1463             else
1464                 send(c, notokmsg, sizeof notokmsg-1, 0);
1465         }
1466         if (strstr(buffer, "/test9"))
1467         {
1468             if (!strstr(buffer, "Connection: Close") &&
1469                 !strstr(buffer, "Connection: Keep-Alive"))
1470                 send(c, okmsg, sizeof okmsg-1, 0);
1471             else
1472                 send(c, notokmsg, sizeof notokmsg-1, 0);
1473         }
1474         if (strstr(buffer, "GET /quit"))
1475         {
1476             send(c, okmsg, sizeof okmsg-1, 0);
1477             send(c, page1, sizeof page1-1, 0);
1478             last_request = 1;
1479         }
1480
1481         shutdown(c, 2);
1482         closesocket(c);
1483     } while (!last_request);
1484
1485     closesocket(s);
1486
1487     return 0;
1488 }
1489
1490 static void test_basic_request(int port, const char *verb, const char *url)
1491 {
1492     HINTERNET hi, hc, hr;
1493     DWORD r, count;
1494     char buffer[0x100];
1495
1496     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1497     ok(hi != NULL, "open failed\n");
1498
1499     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1500     ok(hc != NULL, "connect failed\n");
1501
1502     hr = HttpOpenRequest(hc, verb, url, NULL, NULL, NULL, 0, 0);
1503     ok(hr != NULL, "HttpOpenRequest failed\n");
1504
1505     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1506     ok(r, "HttpSendRequest failed\n");
1507
1508     count = 0;
1509     memset(buffer, 0, sizeof buffer);
1510     r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1511     ok(r, "InternetReadFile failed\n");
1512     ok(count == sizeof page1 - 1, "count was wrong\n");
1513     ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
1514
1515     InternetCloseHandle(hr);
1516     InternetCloseHandle(hc);
1517     InternetCloseHandle(hi);
1518 }
1519
1520 static void test_proxy_indirect(int port)
1521 {
1522     HINTERNET hi, hc, hr;
1523     DWORD r, sz, val;
1524     char buffer[0x40];
1525
1526     hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1527     ok(hi != NULL, "open failed\n");
1528
1529     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1530     ok(hc != NULL, "connect failed\n");
1531
1532     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1533     ok(hr != NULL, "HttpOpenRequest failed\n");
1534
1535     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1536     ok(r, "HttpSendRequest failed\n");
1537
1538     sz = sizeof buffer;
1539     r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1540     ok(r, "HttpQueryInfo failed\n");
1541     ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1542
1543     sz = sizeof buffer;
1544     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1545     ok(r, "HttpQueryInfo failed\n");
1546     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1547
1548     sz = sizeof val;
1549     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1550     ok(r, "HttpQueryInfo failed\n");
1551     ok(val == 407, "proxy code wrong\n");
1552
1553     sz = sizeof buffer;
1554     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1555     ok(r, "HttpQueryInfo failed\n");
1556     ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1557
1558     sz = sizeof buffer;
1559     r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1560     ok(r, "HttpQueryInfo failed\n");
1561     ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1562
1563     sz = sizeof buffer;
1564     r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1565     ok(r, "HttpQueryInfo failed\n");
1566     ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1567
1568     sz = sizeof buffer;
1569     r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1570     ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1571     ok(r == FALSE, "HttpQueryInfo failed\n");
1572
1573     InternetCloseHandle(hr);
1574     InternetCloseHandle(hc);
1575     InternetCloseHandle(hi);
1576 }
1577
1578 static void test_proxy_direct(int port)
1579 {
1580     HINTERNET hi, hc, hr;
1581     DWORD r, sz;
1582     char buffer[0x40];
1583     static CHAR username[] = "mike",
1584                 password[] = "1101";
1585
1586     sprintf(buffer, "localhost:%d\n", port);
1587     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1588     ok(hi != NULL, "open failed\n");
1589
1590     /* try connect without authorization */
1591     hc = InternetConnect(hi, "www.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1592     ok(hc != NULL, "connect failed\n");
1593
1594     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1595     ok(hr != NULL, "HttpOpenRequest failed\n");
1596
1597     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1598     ok(r, "HttpSendRequest failed\n");
1599
1600     sz = sizeof buffer;
1601     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1602     ok(r, "HttpQueryInfo failed\n");
1603     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1604
1605
1606     /* set the user + password then try again */
1607     todo_wine {
1608     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
1609     ok(r, "failed to set user\n");
1610
1611     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
1612     ok(r, "failed to set password\n");
1613     }
1614
1615     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1616     ok(r, "HttpSendRequest failed\n");
1617     sz = sizeof buffer;
1618     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1619     ok(r, "HttpQueryInfo failed\n");
1620     todo_wine {
1621     ok(!strcmp(buffer, "200"), "proxy code wrong\n");
1622     }
1623
1624
1625     InternetCloseHandle(hr);
1626     InternetCloseHandle(hc);
1627     InternetCloseHandle(hi);
1628 }
1629
1630 static void test_header_handling_order(int port)
1631 {
1632     static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
1633     static char connection[]    = "Connection: Close";
1634
1635     static const char *types[2] = { "*", NULL };
1636     HINTERNET session, connect, request;
1637     DWORD size, status;
1638     BOOL ret;
1639
1640     session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1641     ok(session != NULL, "InternetOpen failed\n");
1642
1643     connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1644     ok(connect != NULL, "InternetConnect failed\n");
1645
1646     request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1647     ok(request != NULL, "HttpOpenRequest failed\n");
1648
1649     ret = HttpAddRequestHeaders(request, authorization, ~0UL, HTTP_ADDREQ_FLAG_ADD);
1650     ok(ret, "HttpAddRequestHeaders failed\n");
1651
1652     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1653     ok(ret, "HttpSendRequest failed\n");
1654
1655     status = 0;
1656     size = sizeof(status);
1657     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1658     ok(ret, "HttpQueryInfo failed\n");
1659     ok(status == 200, "request failed with status %u\n", status);
1660
1661     InternetCloseHandle(request);
1662
1663     request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1664     ok(request != NULL, "HttpOpenRequest failed\n");
1665
1666     ret = HttpSendRequest(request, connection, ~0UL, NULL, 0);
1667     ok(ret, "HttpSendRequest failed\n");
1668
1669     status = 0;
1670     size = sizeof(status);
1671     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1672     ok(ret, "HttpQueryInfo failed\n");
1673     ok(status == 200, "request failed with status %u\n", status);
1674
1675     InternetCloseHandle(request);
1676
1677     request = HttpOpenRequest(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1678     ok(request != NULL, "HttpOpenRequest failed\n");
1679
1680     ret = HttpAddRequestHeaders(request, "Content-Length: 100\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1681     ok(ret, "HttpAddRequestHeaders failed\n");
1682
1683     ret = HttpSendRequest(request, connection, ~0UL, NULL, 0);
1684     ok(ret, "HttpSendRequest failed\n");
1685
1686     status = 0;
1687     size = sizeof(status);
1688     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1689     ok(ret, "HttpQueryInfo failed\n");
1690     ok(status == 200, "request failed with status %u\n", status);
1691
1692     InternetCloseHandle(request);
1693     InternetCloseHandle(connect);
1694     InternetCloseHandle(session);
1695 }
1696
1697 static void test_connection_header(int port)
1698 {
1699     HINTERNET ses, con, req;
1700     DWORD size, status;
1701     BOOL ret;
1702
1703     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1704     ok(ses != NULL, "InternetOpen failed\n");
1705
1706     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1707     ok(con != NULL, "InternetConnect failed\n");
1708
1709     req = HttpOpenRequest(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1710     ok(req != NULL, "HttpOpenRequest failed\n");
1711
1712     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1713     ok(ret, "HttpSendRequest failed\n");
1714
1715     status = 0;
1716     size = sizeof(status);
1717     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1718     ok(ret, "HttpQueryInfo failed\n");
1719     ok(status == 200, "request failed with status %u\n", status);
1720
1721     InternetCloseHandle(req);
1722
1723     req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
1724     ok(req != NULL, "HttpOpenRequest failed\n");
1725
1726     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1727     ok(ret, "HttpSendRequest failed\n");
1728
1729     status = 0;
1730     size = sizeof(status);
1731     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1732     ok(ret, "HttpQueryInfo failed\n");
1733     ok(status == 200, "request failed with status %u\n", status);
1734
1735     InternetCloseHandle(req);
1736     InternetCloseHandle(con);
1737     InternetCloseHandle(ses);
1738 }
1739
1740 static void test_http_connection(void)
1741 {
1742     struct server_info si;
1743     HANDLE hThread;
1744     DWORD id = 0, r;
1745
1746     si.hEvent = CreateEvent(NULL, 0, 0, NULL);
1747     si.port = 7531;
1748
1749     hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
1750     ok( hThread != NULL, "create thread failed\n");
1751
1752     r = WaitForSingleObject(si.hEvent, 10000);
1753     ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
1754     if (r != WAIT_OBJECT_0)
1755         return;
1756
1757     test_basic_request(si.port, "GET", "/test1");
1758     test_proxy_indirect(si.port);
1759     test_proxy_direct(si.port);
1760     test_header_handling_order(si.port);
1761     test_basic_request(si.port, "POST", "/test5");
1762     test_basic_request(si.port, "GET", "/test6");
1763     test_connection_header(si.port);
1764
1765     /* send the basic request again to shutdown the server thread */
1766     test_basic_request(si.port, "GET", "/quit");
1767
1768     r = WaitForSingleObject(hThread, 3000);
1769     ok( r == WAIT_OBJECT_0, "thread wait failed\n");
1770     CloseHandle(hThread);
1771 }
1772
1773 static void test_user_agent_header(void)
1774 {
1775     HINTERNET ses, con, req;
1776     DWORD size, err;
1777     char buffer[64];
1778     BOOL ret;
1779
1780     ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1781     ok(ses != NULL, "InternetOpen failed\n");
1782
1783     con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1784     ok(con != NULL, "InternetConnect failed\n");
1785
1786     req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
1787     ok(req != NULL, "HttpOpenRequest failed\n");
1788
1789     size = sizeof(buffer);
1790     ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1791     err = GetLastError();
1792     ok(!ret, "HttpQueryInfo succeeded\n");
1793     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
1794
1795     ret = HttpAddRequestHeaders(req, "User-Agent: Gizmo Project\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1796     ok(ret, "HttpAddRequestHeaders succeeded\n");
1797
1798     size = sizeof(buffer);
1799     ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1800     err = GetLastError();
1801     ok(ret, "HttpQueryInfo failed\n");
1802     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
1803
1804     InternetCloseHandle(req);
1805
1806     req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
1807     ok(req != NULL, "HttpOpenRequest failed\n");
1808
1809     size = sizeof(buffer);
1810     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1811     err = GetLastError();
1812     ok(!ret, "HttpQueryInfo succeeded\n");
1813     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
1814
1815     ret = HttpAddRequestHeaders(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1816     ok(ret, "HttpAddRequestHeaders failed\n");
1817
1818     buffer[0] = 0;
1819     size = sizeof(buffer);
1820     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1821     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
1822     ok(!strcmp(buffer, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
1823
1824     InternetCloseHandle(req);
1825     InternetCloseHandle(con);
1826     InternetCloseHandle(ses);
1827 }
1828
1829 static void test_bogus_accept_types_array(void)
1830 {
1831     HINTERNET ses, con, req;
1832     static const char *types[] = { (const char *)6240, "*/*", "%p", "", "*/*", NULL };
1833     DWORD size;
1834     char buffer[32];
1835     BOOL ret;
1836
1837     ses = InternetOpen("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
1838     con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1839     req = HttpOpenRequest(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
1840
1841     ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
1842
1843     buffer[0] = 0;
1844     size = sizeof(buffer);
1845     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1846     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
1847     ok(!strcmp(buffer, ", */*, %p, , */*") || /* IE6 */
1848        !strcmp(buffer, "*/*, %p, */*"),
1849        "got '%s' expected '*/*, %%p, */*' or ', */*, %%p, , */*'\n", buffer);
1850
1851     InternetCloseHandle(req);
1852     InternetCloseHandle(con);
1853     InternetCloseHandle(ses);
1854 }
1855
1856 struct context
1857 {
1858     HANDLE event;
1859     HINTERNET req;
1860 };
1861
1862 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
1863 {
1864     INTERNET_ASYNC_RESULT *result = info;
1865     struct context *ctx = (struct context *)context;
1866
1867     trace("%p 0x%08lx %u %p 0x%08x\n", handle, context, status, info, size);
1868
1869     if (status == INTERNET_STATUS_REQUEST_COMPLETE)
1870     {
1871         trace("request handle: 0x%08lx\n", result->dwResult);
1872         ctx->req = (HINTERNET)result->dwResult;
1873         SetEvent(ctx->event);
1874     }
1875     if (status == INTERNET_STATUS_HANDLE_CLOSING)
1876     {
1877         DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
1878
1879         if (InternetQueryOption(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
1880             ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
1881         SetEvent(ctx->event);
1882     }
1883 }
1884
1885 static void test_open_url_async(void)
1886 {
1887     BOOL ret;
1888     HINTERNET ses, req;
1889     DWORD size;
1890     struct context ctx;
1891     ULONG type;
1892
1893     ctx.req = NULL;
1894     ctx.event = CreateEvent(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
1895
1896     ses = InternetOpen("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
1897     ok(ses != NULL, "InternetOpen failed\n");
1898
1899     pInternetSetStatusCallbackA(ses, cb);
1900     ResetEvent(ctx.event);
1901
1902     req = InternetOpenUrl(ses, "http://www.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
1903     ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
1904
1905     WaitForSingleObject(ctx.event, INFINITE);
1906
1907     type = 0;
1908     size = sizeof(type);
1909     ret = InternetQueryOption(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
1910     ok(ret, "InternetQueryOption failed: %u\n", GetLastError());
1911     ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
1912        "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %u\n", type);
1913
1914     size = 0;
1915     ret = HttpQueryInfo(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
1916     ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
1917     ok(size > 0, "expected size > 0\n");
1918
1919     ResetEvent(ctx.event);
1920     InternetCloseHandle(ctx.req);
1921     WaitForSingleObject(ctx.event, INFINITE);
1922
1923     InternetCloseHandle(ses);
1924     CloseHandle(ctx.event);
1925 }
1926
1927 #define STATUS_STRING(status) \
1928     memcpy(status_string[status], #status, sizeof(CHAR) * \
1929            (strlen(#status) < MAX_STATUS_NAME ? \
1930             strlen(#status) : \
1931             MAX_STATUS_NAME - 1))
1932 static void init_status_tests(void)
1933 {
1934     memset(expect, 0, sizeof(expect));
1935     memset(wine_allow, 0, sizeof(wine_allow));
1936     memset(notified, 0, sizeof(notified));
1937     memset(status_string, 0, sizeof(status_string));
1938     STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
1939     STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
1940     STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
1941     STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
1942     STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
1943     STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
1944     STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
1945     STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
1946     STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
1947     STATUS_STRING(INTERNET_STATUS_PREFETCH);
1948     STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
1949     STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
1950     STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
1951     STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
1952     STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
1953     STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
1954     STATUS_STRING(INTERNET_STATUS_REDIRECT);
1955     STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
1956     STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
1957     STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
1958     STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
1959     STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
1960     STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
1961     STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
1962     STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
1963     STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
1964 }
1965 #undef STATUS_STRING
1966
1967 START_TEST(http)
1968 {
1969     HMODULE hdll;
1970     hdll = GetModuleHandleA("wininet.dll");
1971     pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
1972     pInternetTimeFromSystemTimeA = (void*)GetProcAddress(hdll, "InternetTimeFromSystemTimeA");
1973     pInternetTimeFromSystemTimeW = (void*)GetProcAddress(hdll, "InternetTimeFromSystemTimeW");
1974     pInternetTimeToSystemTimeA = (void*)GetProcAddress(hdll, "InternetTimeToSystemTimeA");
1975     pInternetTimeToSystemTimeW = (void*)GetProcAddress(hdll, "InternetTimeToSystemTimeW");
1976
1977     if (!pInternetSetStatusCallbackA)
1978         skip("skipping the InternetReadFile tests\n");
1979     else
1980     {
1981         init_status_tests();
1982         InternetReadFile_test(INTERNET_FLAG_ASYNC);
1983         InternetReadFile_test(0);
1984         InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
1985         test_open_url_async();
1986     }
1987     InternetOpenRequest_test();
1988     test_http_cache();
1989     InternetOpenUrlA_test();
1990     if (!pInternetTimeFromSystemTimeA)
1991         skip("skipping the InternetTime tests\n");
1992     else
1993     {
1994         InternetTimeFromSystemTimeA_test();
1995         InternetTimeFromSystemTimeW_test();
1996         InternetTimeToSystemTimeA_test();
1997         InternetTimeToSystemTimeW_test();
1998     }
1999     HttpSendRequestEx_test();
2000     HttpHeaders_test();
2001     test_http_connection();
2002     test_user_agent_header();
2003     test_bogus_accept_types_array();
2004 }