ReactOS  0.4.15-dev-3165-gdf6fff7
http.c
Go to the documentation of this file.
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 #include <limits.h>
27 
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wininet.h"
31 #include "winineti.h"
32 #include "winsock2.h"
33 
34 #include "wine/test.h"
35 
36 /* Undocumented security flags */
37 #define _SECURITY_FLAG_CERT_REV_FAILED 0x00800000
38 #define _SECURITY_FLAG_CERT_INVALID_CA 0x01000000
39 #define _SECURITY_FLAG_CERT_INVALID_CN 0x02000000
40 #define _SECURITY_FLAG_CERT_INVALID_DATE 0x04000000
41 
42 #define TEST_URL "http://test.winehq.org/tests/hello.html"
43 
46 
47 /* Adapted from dlls/urlmon/tests/protocol.c */
48 
49 #define SET_EXPECT2(status, num) \
50  expect[status] = num
51 
52 #define SET_EXPECT(status) \
53  SET_EXPECT2(status, 1)
54 
55 #define SET_OPTIONAL2(status, num) \
56  optional[status] = num
57 
58 #define SET_OPTIONAL(status) \
59  SET_OPTIONAL2(status, 1)
60 
61 /* SET_WINE_ALLOW's should be used with an appropriate
62  * todo_wine CHECK_NOTIFIED at a later point in the code */
63 #define SET_WINE_ALLOW2(status, num) \
64  wine_allow[status] = num
65 
66 #define SET_WINE_ALLOW(status) \
67  SET_WINE_ALLOW2(status, 1)
68 
69 #define CHECK_EXPECT(status) \
70  do { \
71  if (!expect[status] && !optional[status] && wine_allow[status]) \
72  { \
73  todo_wine ok(expect[status], "unexpected status %d (%s)\n", status, \
74  status < MAX_INTERNET_STATUS && status_string[status] ? \
75  status_string[status] : "unknown"); \
76  wine_allow[status]--; \
77  } \
78  else \
79  { \
80  ok(expect[status] || optional[status], "unexpected status %d (%s)\n", status, \
81  status < MAX_INTERNET_STATUS && status_string[status] ? \
82  status_string[status] : "unknown"); \
83  if (expect[status]) expect[status]--; \
84  else if(optional[status]) optional[status]--; \
85  } \
86  notified[status]++; \
87  }while(0)
88 
89 /* CLEAR_NOTIFIED used in cases when notification behavior
90  * differs between Windows versions */
91 #define CLEAR_NOTIFIED(status) \
92  expect[status] = optional[status] = wine_allow[status] = notified[status] = 0;
93 
94 #define CHECK_NOTIFIED2(status, num) \
95  do { \
96  ok(notified[status] + optional[status] == (num), \
97  "expected status %d (%s) %d times, received %d times\n", \
98  status, status < MAX_INTERNET_STATUS && status_string[status] ? \
99  status_string[status] : "unknown", (num), notified[status]); \
100  CLEAR_NOTIFIED(status); \
101  }while(0)
102 
103 #define CHECK_NOTIFIED(status) \
104  CHECK_NOTIFIED2(status, 1)
105 
106 #define CHECK_NOT_NOTIFIED(status) \
107  CHECK_NOTIFIED2(status, 0)
108 
109 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
112 static const char *status_string[MAX_INTERNET_STATUS];
113 
117 
118 #define TESTF_REDIRECT 0x01
119 #define TESTF_COMPRESSED 0x02
120 #define TESTF_CHUNKED 0x04
121 
122 typedef struct {
123  const char *url;
124  const char *redirected_url;
125  const char *host;
126  const char *path;
127  const char *headers;
129  const char *post_data;
130  const char *content;
131 } test_data_t;
132 
133 static const test_data_t test_data[] = {
134  {
135  "http://test.winehq.org/tests/data.php",
136  "http://test.winehq.org/tests/data.php",
137  "test.winehq.org",
138  "/tests/data.php",
139  "",
141  },
142  {
143  "http://test.winehq.org/tests/redirect",
144  "http://test.winehq.org/tests/hello.html",
145  "test.winehq.org",
146  "/tests/redirect",
147  "",
149  },
150  {
151  "http://test.winehq.org/tests/gzip.php",
152  "http://test.winehq.org/tests/gzip.php",
153  "test.winehq.org",
154  "/tests/gzip.php",
155  "Accept-Encoding: gzip, deflate",
157  },
158  {
159  "http://test.winehq.org/tests/post.php",
160  "http://test.winehq.org/tests/post.php",
161  "test.winehq.org",
162  "/tests/post.php",
163  "Content-Type: application/x-www-form-urlencoded",
164  0,
165  "mode=Test",
166  "mode => Test\n"
167  }
168 };
169 
170 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
171 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackW)(HINTERNET ,INTERNET_STATUS_CALLBACK);
172 static BOOL (WINAPI *pInternetGetSecurityInfoByURLA)(LPSTR,PCCERT_CHAIN_CONTEXT*,DWORD*);
173 
174 static BOOL is_lang_english(void)
175 {
176  static HMODULE hkernel32 = NULL;
177  static LANGID (WINAPI *pGetThreadUILanguage)(void) = NULL;
178  static LANGID (WINAPI *pGetUserDefaultUILanguage)(void) = NULL;
179 
180  if (!hkernel32)
181  {
182  hkernel32 = GetModuleHandleA("kernel32.dll");
183  pGetThreadUILanguage = (void*)GetProcAddress(hkernel32, "GetThreadUILanguage");
184  pGetUserDefaultUILanguage = (void*)GetProcAddress(hkernel32, "GetUserDefaultUILanguage");
185  }
186  if (pGetThreadUILanguage)
187  return PRIMARYLANGID(pGetThreadUILanguage()) == LANG_ENGLISH;
188  if (pGetUserDefaultUILanguage)
189  return PRIMARYLANGID(pGetUserDefaultUILanguage()) == LANG_ENGLISH;
190 
192 }
193 
194 static int strcmp_wa(LPCWSTR strw, const char *stra)
195 {
196  WCHAR buf[512];
197  MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, ARRAY_SIZE(buf));
198  return lstrcmpW(strw, buf);
199 }
200 
201 static BOOL proxy_active(void)
202 {
205  DWORD size;
206 
207  if (RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
209  return FALSE;
210 
211  size = sizeof(DWORD);
213  proxy_enable = 0;
214 
216 
217  return proxy_enable != 0;
218 }
219 
220 static void init_events(void)
221 {
227 }
228 
229 static void free_events(void)
230 {
236 }
237 
238 static void reset_events(void)
239 {
245 }
246 
247 #define test_status_code(a,b) _test_status_code(__LINE__,a,b, FALSE)
248 #define test_status_code_todo(a,b) _test_status_code(__LINE__,a,b, TRUE)
249 static void _test_status_code(unsigned line, HINTERNET req, DWORD excode, BOOL is_todo)
250 {
251  DWORD code, size, index;
252  char exbuf[12], bufa[10];
253  WCHAR bufw[10];
254  BOOL res;
255 
256  code = 0xdeadbeef;
257  size = sizeof(code);
259  ok_(__FILE__,line)(res, "[1] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE|number) failed: %u\n", GetLastError());
260  todo_wine_if (is_todo)
261  ok_(__FILE__,line)(code == excode, "code = %d, expected %d\n", code, excode);
262  ok_(__FILE__,line)(size == sizeof(code), "size = %u\n", size);
263 
264  code = 0xdeadbeef;
265  index = 0;
266  size = sizeof(code);
268  ok_(__FILE__,line)(res, "[2] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE|number index) failed: %u\n", GetLastError());
269  ok_(__FILE__,line)(!index, "index = %d, expected 0\n", index);
270  ok_(__FILE__,line)(size == sizeof(code), "size = %u\n", size);
271 
272  sprintf(exbuf, "%u", excode);
273 
274  size = sizeof(bufa);
276  ok_(__FILE__,line)(res, "[3] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
277  todo_wine_if (is_todo)
278  ok_(__FILE__,line)(!strcmp(bufa, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
279  ok_(__FILE__,line)(size == strlen(exbuf), "unexpected size %d for \"%s\"\n", size, exbuf);
280 
281  size = 0;
284  "[4] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
285  ok_(__FILE__,line)(size == strlen(exbuf)+1, "unexpected size %d for \"%s\"\n", size, exbuf);
286 
287  size = sizeof(bufw);
289  ok_(__FILE__,line)(res, "[5] HttpQueryInfoW(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
290  todo_wine_if (is_todo)
291  ok_(__FILE__,line)(!strcmp_wa(bufw, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
292  ok_(__FILE__,line)(size == strlen(exbuf)*sizeof(WCHAR), "unexpected size %d for \"%s\"\n", size, exbuf);
293 
294  size = 0;
297  "[6] HttpQueryInfoW(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
298  ok_(__FILE__,line)(size == (strlen(exbuf)+1)*sizeof(WCHAR), "unexpected size %d for \"%s\"\n", size, exbuf);
299 
300  if(0) {
301  size = sizeof(bufw);
303  ok(!res && GetLastError() == ERROR_INVALID_PARAMETER, "HttpQueryInfo(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
304  ok(size == sizeof(bufw), "unexpected size %d\n", size);
305  }
306 
307  code = 0xdeadbeef;
308  index = 1;
309  size = sizeof(code);
312  "[7] HttpQueryInfoA failed: %x(%d)\n", res, GetLastError());
313 
314  code = 0xdeadbeef;
315  size = sizeof(code);
318  "[8] HttpQueryInfoA failed: %x(%d)\n", res, GetLastError());
319 }
320 
321 #define test_request_flags(a,b) _test_request_flags(__LINE__,a,b,FALSE)
322 #define test_request_flags_todo(a,b) _test_request_flags(__LINE__,a,b,TRUE)
323 static void _test_request_flags(unsigned line, HINTERNET req, DWORD exflags, BOOL is_todo)
324 {
325  DWORD flags, size;
326  BOOL res;
327 
328  flags = 0xdeadbeef;
329  size = sizeof(flags);
331  ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_REQUEST_FLAGS) failed: %u\n", GetLastError());
332 
333  /* FIXME: Remove once we have INTERNET_REQFLAG_CACHE_WRITE_DISABLED implementation */
335  todo_wine_if (is_todo)
336  ok_(__FILE__,line)(flags == exflags, "flags = %x, expected %x\n", flags, exflags);
337 }
338 
339 #define test_request_url(a,b) _test_request_url(__LINE__,a,b)
340 static void _test_request_url(unsigned line, HINTERNET req, const char *expected_url)
341 {
343  DWORD size = sizeof(buf);
344  BOOL res;
345 
347  ok_(__FILE__,line)(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
348  ok_(__FILE__,line)(size == strlen(expected_url), "size = %u\n", size);
349  ok_(__FILE__,line)(!strcmp(buf, expected_url), "unexpected URL %s, expected %s\n", buf, expected_url);
350 }
351 
352 #define test_http_version(a) _test_http_version(__LINE__,a)
353 static void _test_http_version(unsigned line, HINTERNET req)
354 {
355  HTTP_VERSION_INFO v = {0xdeadbeef, 0xdeadbeef};
356  DWORD size;
357  BOOL res;
358 
359  size = sizeof(v);
361  ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_HTTP_VERSION) failed: %u\n", GetLastError());
362  ok_(__FILE__,line)(v.dwMajorVersion == 1, "dwMajorVersion = %d\n", v.dwMajorVersion);
363  ok_(__FILE__,line)(v.dwMinorVersion == 1, "dwMinorVersion = %d\n", v.dwMinorVersion);
364 }
365 
366 static int close_handle_cnt;
367 
369  HINTERNET hInternet,
370  DWORD_PTR dwContext,
371  DWORD dwInternetStatus,
372  LPVOID lpvStatusInformation,
373  DWORD dwStatusInformationLength
374 )
375 {
377  CHECK_EXPECT(dwInternetStatus);
378  switch (dwInternetStatus)
379  {
381  if(winetest_debug > 1)
382  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
383  GetCurrentThreadId(), hInternet, dwContext,
384  (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
385  *(LPSTR)lpvStatusInformation = '\0';
386  break;
388  if(winetest_debug > 1)
389  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
390  GetCurrentThreadId(), hInternet, dwContext,
391  (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
392  *(LPSTR)lpvStatusInformation = '\0';
393  break;
395  if(winetest_debug > 1)
396  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
397  GetCurrentThreadId(), hInternet, dwContext,
398  (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
399  ok(dwStatusInformationLength == strlen(lpvStatusInformation)+1, "unexpected size %u\n",
400  dwStatusInformationLength);
401  *(LPSTR)lpvStatusInformation = '\0';
402  break;
404  if(winetest_debug > 1)
405  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
406  GetCurrentThreadId(), hInternet, dwContext,
407  (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
408  ok(dwStatusInformationLength == strlen(lpvStatusInformation)+1, "unexpected size %u\n",
409  dwStatusInformationLength);
410  *(LPSTR)lpvStatusInformation = '\0';
411  break;
413  if(winetest_debug > 1)
414  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
415  GetCurrentThreadId(), hInternet, dwContext,
416  lpvStatusInformation,dwStatusInformationLength);
417  break;
419  ok(dwStatusInformationLength == sizeof(DWORD),
420  "info length should be sizeof(DWORD) instead of %d\n",
421  dwStatusInformationLength);
422  if(winetest_debug > 1)
423  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
424  GetCurrentThreadId(), hInternet, dwContext,
425  *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
426  break;
428  if(winetest_debug > 1)
429  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
430  GetCurrentThreadId(), hInternet, dwContext,
431  lpvStatusInformation,dwStatusInformationLength);
432  break;
434  ok(dwStatusInformationLength == sizeof(DWORD),
435  "info length should be sizeof(DWORD) instead of %d\n",
436  dwStatusInformationLength);
437  if(winetest_debug > 1)
438  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
439  GetCurrentThreadId(), hInternet, dwContext,
440  *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
441  break;
443  if(winetest_debug > 1)
444  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
445  GetCurrentThreadId(), hInternet,dwContext,
446  lpvStatusInformation,dwStatusInformationLength);
447  break;
449  if(winetest_debug > 1)
450  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
451  GetCurrentThreadId(), hInternet, dwContext,
452  lpvStatusInformation,dwStatusInformationLength);
453  break;
455  if(winetest_debug > 1)
456  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
457  GetCurrentThreadId(), hInternet, dwContext,
458  lpvStatusInformation,dwStatusInformationLength);
459  break;
461  if(winetest_debug > 1)
462  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
463  GetCurrentThreadId(), hInternet, dwContext,
464  lpvStatusInformation,dwStatusInformationLength);
465  break;
467  ok(dwStatusInformationLength == sizeof(HINTERNET),
468  "info length should be sizeof(HINTERNET) instead of %d\n",
469  dwStatusInformationLength);
470  if(winetest_debug > 1)
471  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
472  GetCurrentThreadId(), hInternet, dwContext,
473  *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
476  break;
478  ok(dwStatusInformationLength == sizeof(HINTERNET),
479  "info length should be sizeof(HINTERNET) instead of %d\n",
480  dwStatusInformationLength);
481  if(winetest_debug > 1)
482  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
483  GetCurrentThreadId(), hInternet, dwContext,
484  *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
487  break;
489  {
490  INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
491  ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
492  "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
493  dwStatusInformationLength);
494  ok(iar->dwResult == 1 || iar->dwResult == 0, "iar->dwResult = %ld\n", iar->dwResult);
495  if(winetest_debug > 1)
496  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
497  GetCurrentThreadId(), hInternet, dwContext,
498  iar->dwResult,iar->dwError,dwStatusInformationLength);
499  req_error = iar->dwError;
500  if(!close_handle_cnt)
502  break;
503  }
505  if(winetest_debug > 1)
506  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
507  GetCurrentThreadId(), hInternet, dwContext,
508  (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
509  *(LPSTR)lpvStatusInformation = '\0';
512  break;
514  if(winetest_debug > 1)
515  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
516  GetCurrentThreadId(), hInternet, dwContext,
517  lpvStatusInformation, dwStatusInformationLength);
518  break;
519  default:
520  if(winetest_debug > 1)
521  trace("%04x:Callback %p 0x%lx %d %p %d\n",
522  GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
523  lpvStatusInformation, dwStatusInformationLength);
524  }
525 }
526 
527 typedef struct {
532 
533 #define open_simple_request(a,b,c,d,e) _open_simple_request(__LINE__,a,b,c,d,e)
534 static void _open_simple_request(unsigned line, test_request_t *req, const char *host,
535  int port, const char *verb, const char *url)
536 {
538  ok_(__FILE__,line)(req->session != NULL, "InternetOpenA failed: %u\n", GetLastError());
539 
541  ok_(__FILE__,line)(req->connection != NULL, "InternetConnectA failed: %u\n", GetLastError());
542 
543  req->request = HttpOpenRequestA(req->connection, verb, url, NULL, NULL, NULL, 0, 0);
544  ok_(__FILE__,line)(req->request != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
545 }
546 
547 #define close_request(a) _close_request(__LINE__,a)
548 static void _close_request(unsigned line, test_request_t *req)
549 {
550  BOOL ret;
551 
553  ok_(__FILE__,line)(ret, "InternetCloseHandle(request) failed: %u\n", GetLastError());
555  ok_(__FILE__,line)(ret, "InternetCloseHandle(connection) failed: %u\n", GetLastError());
557  ok_(__FILE__,line)(ret, "InternetCloseHandle(session) failed: %u\n", GetLastError());
558 }
559 
560 #define receive_simple_request(a,b,c) _receive_simple_request(__LINE__,a,b,c)
561 static DWORD _receive_simple_request(unsigned line, HINTERNET req, char *buf, size_t buf_size)
562 {
563  DWORD read = 0;
564  BOOL ret;
565 
566  ret = InternetReadFile(req, buf, buf_size, &read);
567  ok_(__FILE__,line)(ret, "InternetReadFile failed: %u\n", GetLastError());
568 
569  return read;
570 }
571 
572 static void close_async_handle(HINTERNET handle, int handle_cnt)
573 {
574  BOOL res;
575 
576  close_handle_cnt = handle_cnt;
577 
580  ok(res, "InternetCloseHandle failed: %u\n", GetLastError());
583 }
584 
585 static void InternetReadFile_test(int flags, const test_data_t *test)
586 {
587  char *post_data = NULL;
588  BOOL res, on_async = TRUE;
589  CHAR buffer[4000];
590  WCHAR wbuffer[4000];
591  DWORD length, length2, index, exlen = 0, post_len = 0;
592  const char *types[2] = { "*", NULL };
593  HINTERNET hi, hic = 0, hor = 0;
594  DWORD contents_length, accepts_ranges;
595  BOOL not_supported;
596 
597  trace("Starting InternetReadFile test with flags 0x%x on url %s\n",flags,test->url);
598  reset_events();
599 
600  trace("InternetOpenA <--\n");
601  hi = InternetOpenA((test->flags & TESTF_COMPRESSED) ? "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" : "",
603  ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
604  trace("InternetOpenA -->\n");
605 
606  if (hi == 0x0) goto abort;
607 
608  pInternetSetStatusCallbackA(hi,&callback);
609 
611 
612  trace("InternetConnectA <--\n");
614  NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
615  ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
616  trace("InternetConnectA -->\n");
617 
618  if (hic == 0x0) goto abort;
619 
622 
623  trace("HttpOpenRequestA <--\n");
624  hor = HttpOpenRequestA(hic, test->post_data ? "POST" : "GET", test->path, NULL, NULL, types,
626  0xdeadbead);
627  if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
628  /*
629  * If the internet name can't be resolved we are probably behind
630  * a firewall or in some other way not directly connected to the
631  * Internet. Not enough reason to fail the test. Just ignore and
632  * abort.
633  */
634  } else {
635  ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
636  }
637  trace("HttpOpenRequestA -->\n");
638 
639  if (hor == 0x0) goto abort;
640 
642  test_request_url(hor, test->url);
643 
644  length = sizeof(buffer);
646  ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
647  ok(length == 0 || (length == 1 && !*buffer) /* win10 */, "HTTP_QUERY_RAW_HEADERS: expected length 0, but got %d\n", length);
648  ok(!strcmp(buffer, ""), "HTTP_QUERY_RAW_HEADERS: expected string \"\", but got \"%s\"\n", buffer);
649 
656  {
659  }
666  if(test->flags & TESTF_REDIRECT) {
669  }
675 
676  if(test->flags & TESTF_COMPRESSED) {
677  BOOL b = TRUE;
678 
681  "InternetSetOption failed: %u\n", GetLastError());
682  if(!res)
683  goto abort;
684  }
685 
686  test_status_code(hor, 0);
687 
688  trace("HttpSendRequestA -->\n");
689  if(test->post_data) {
690  post_len = strlen(test->post_data);
691  post_data = HeapAlloc(GetProcessHeap(), 0, post_len);
692  memcpy(post_data, test->post_data, post_len);
693  }
694  SetLastError(0xdeadbeef);
695  res = HttpSendRequestA(hor, test->headers, -1, post_data, post_len);
697  ok(!res && (GetLastError() == ERROR_IO_PENDING),
698  "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
699  else
701  "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
702  trace("HttpSendRequestA <--\n");
703 
704  if (flags & INTERNET_FLAG_ASYNC) {
706  ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
707  }
709 
713  {
714  if (! proxy_active())
715  {
718  }
719  else
720  {
723  }
724  }
725  else
726  {
729  }
734  if(test->flags & TESTF_REDIRECT)
738  /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
741 
742  test_request_flags(hor, 0);
743 
744  length = 100;
746  ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed with error %d\n", GetLastError());
747 
748  length = sizeof(buffer)-2;
749  memset(buffer, 0x77, sizeof(buffer));
750  SetLastError(0xdeadbeef);
752  ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
753  ok(GetLastError() == 0 ||
754  broken(GetLastError() == 0xdeadbeef /* XP/W2K3 */), "Last Error not reset %u\n", GetLastError());
755  /* show that the function writes data past the length returned */
756  ok(buffer[length-2], "Expected any header character, got 0x00\n");
757  ok(!buffer[length-1], "Expected 0x00, got %02X\n", buffer[length-1]);
758  ok(!buffer[length], "Expected 0x00, got %02X\n", buffer[length]);
759  ok(buffer[length+1] == 0x77, "Expected 0x77, got %02X\n", buffer[length+1]);
760 
761  length2 = length;
763  ok(!res, "Expected 0x00, got %d\n", res);
764  ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
765  ok(length2 == length+1, "Expected %d, got %d\n", length+1, length2);
766  /* the in length of the buffer must be +1 but the length returned does not count this */
767  length2 = length+1;
768  memset(buffer, 0x77, sizeof(buffer));
770  ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
771  ok(buffer[length2] == 0x00, "Expected 0x00, got %02X\n", buffer[length2]);
772  ok(buffer[length2+1] == 0x77, "Expected 0x77, got %02X\n", buffer[length2+1]);
773  ok(length2 == length, "Value should not have changed: %d != %d\n", length2, length);
774 
775  length = sizeof(wbuffer)-2*sizeof(WCHAR);
776  memset(wbuffer, 0x77, sizeof(wbuffer));
777  res = HttpQueryInfoW(hor, HTTP_QUERY_RAW_HEADERS, wbuffer, &length, 0x0);
778  ok(res, "HttpQueryInfoW(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
779  ok(length % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %d.\n", length);
780  length /= sizeof(WCHAR);
781  /* show that the function writes data past the length returned */
782  ok(wbuffer[length-2], "Expected any header character, got 0x0000\n");
783  ok(!wbuffer[length-1], "Expected 0x0000, got %04X\n", wbuffer[length-1]);
784  ok(!wbuffer[length], "Expected 0x0000, got %04X\n", wbuffer[length]);
785  ok(wbuffer[length+1] == 0x7777 || broken(wbuffer[length+1] != 0x7777),
786  "Expected 0x7777, got %04X\n", wbuffer[length+1]);
787 
788  length2 = length*sizeof(WCHAR);
789  res = HttpQueryInfoW(hor,HTTP_QUERY_RAW_HEADERS,wbuffer,&length2,0x0);
790  ok(!res, "Expected 0x00, got %d\n", res);
791  ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
792  ok(length2 % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %d.\n", length2);
793  length2 /= sizeof(WCHAR);
794  ok(length2 == length+1, "Expected %d, got %d\n", length+1, length2);
795  /* the in length of the buffer must be +1 but the length returned does not count this */
796  length2 = (length+1)*sizeof(WCHAR);
797  memset(wbuffer, 0x77, sizeof(wbuffer));
798  res = HttpQueryInfoW(hor,HTTP_QUERY_RAW_HEADERS,wbuffer,&length2,0x0);
799  ok(res, "HttpQueryInfoW(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
800  ok(length2 % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %d.\n", length2);
801  length2 /= sizeof(WCHAR);
802  ok(!wbuffer[length2], "Expected 0x0000, got %04X\n", wbuffer[length2]);
803  ok(wbuffer[length2+1] == 0x7777, "Expected 0x7777, got %04X\n", wbuffer[length2+1]);
804  ok(length2 == length, "Value should not have changed: %d != %d\n", length2, length);
805 
806  test_request_url(hor, test->redirected_url);
807 
808  index = 0;
809  length = 0;
810  SetLastError(0xdeadbeef);
811  ok(HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,NULL,&length,&index) == FALSE,"Query worked\n");
812  if(test->flags & TESTF_COMPRESSED)
814  "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", GetLastError());
815  ok(index == 0, "Index was incremented\n");
816 
817  index = 0;
818  length = 16;
820  trace("Option HTTP_QUERY_CONTENT_LENGTH -> %i %s (%u)\n",res,buffer,GetLastError());
821  if(test->flags & TESTF_COMPRESSED)
822  {
824  "expected ERROR_HTTP_HEADER_NOT_FOUND, got %x (%u)\n", res, GetLastError());
825  contents_length = 0;
826  }
827  else
828  {
829  contents_length = atoi(buffer);
830  }
831  ok(!res || index == 1, "Index was not incremented although result is %x (index = %u)\n", res, index);
832 
833  length = 64;
834  *buffer = 0;
836  trace("Option HTTP_QUERY_ACCEPT_RANGES -> %i %s (%u)\n",res,buffer,GetLastError());
837  accepts_ranges = res && !strcmp(buffer, "bytes");
838 
839  length = 100;
841  buffer[length]=0;
842  trace("Option HTTP_QUERY_CONTENT_TYPE -> %i %s\n",res,buffer);
843 
844  length = 100;
846  buffer[length]=0;
847  trace("Option HTTP_QUERY_CONTENT_ENCODING -> %i %s\n",res,buffer);
848 
849  SetLastError(0xdeadbeef);
851  not_supported = length == INVALID_SET_FILE_POINTER
853  if (accepts_ranges)
854  todo_wine ok((length == contents_length && (GetLastError() == ERROR_SUCCESS
855  || broken(GetLastError() == 0xdeadbeef))) || broken(not_supported),
856  "Got unexpected length %#x, GetLastError() %u, contents_length %u, accepts_ranges %#x.\n",
857  length, GetLastError(), contents_length, accepts_ranges);
858  else
859  ok(not_supported, "Got unexpected length %#x, GetLastError() %u.\n", length, GetLastError());
860 
862  {
863  SetLastError(0xdeadbeef);
865  ok(!length && (GetLastError() == ERROR_SUCCESS || broken(GetLastError() == 0xdeadbeef)),
866  "Got unexpected length %#x, GetLastError() %u.\n", length, GetLastError());
867  }
868 
869  SetLastError(0xdeadbeef);
871  ok(!res, "InternetReadFile should have failed\n");
873  "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
874  GetLastError());
875 
876  length = 100;
877  if(winetest_debug > 1)
878  trace("Entering Query loop\n");
879 
880  while (TRUE)
881  {
884 
885  /* IE11 calls those in InternetQueryDataAvailable call. */
888 
889  length = 0;
891 
893 
895  {
896  if (res)
897  {
899  if(exlen) {
900  ok(length >= exlen, "length %u < exlen %u\n", length, exlen);
901  exlen = 0;
902  }
903  }
904  else if (GetLastError() == ERROR_IO_PENDING)
905  {
906  if(winetest_debug > 1)
907  trace("pending\n");
908  /* on some tests, InternetQueryDataAvailable returns non-zero length and ERROR_IO_PENDING */
909  if(!(test->flags & TESTF_CHUNKED))
910  ok(!length, "InternetQueryDataAvailable returned ERROR_IO_PENDING and %u length\n", length);
912  exlen = length;
913  ok(exlen, "length = 0\n");
916  ok(req_error, "req_error = 0\n");
917  continue;
918  }else {
919  ok(0, "InternetQueryDataAvailable failed: %u\n", GetLastError());
920  }
921  }else {
922  ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
923  }
925 
926  if(winetest_debug > 1)
927  trace("length %u\n", length);
928  if(test->flags & TESTF_CHUNKED)
929  ok(length <= 8192, "length = %d, expected <= 8192\n", length);
930  if (length)
931  {
932  char *buffer;
934 
936 
937  buffer[length]=0;
938 
939  if(winetest_debug > 1)
940  trace("ReadFile -> %s %i\n", res ? "TRUE" : "FALSE", length);
941 
942  if(test->content)
943  ok(!strcmp(buffer, test->content), "buffer = '%s', expected '%s'\n", buffer, test->content);
945  }else {
946  ok(!on_async, "Returned zero size in response to request complete\n");
947  break;
948  }
949  on_async = FALSE;
950  }
951  if(test->flags & TESTF_REDIRECT) {
954  }
955 abort:
956  if(winetest_debug > 1)
957  trace("aborting\n");
958  close_async_handle(hi, 2);
960 }
961 
963 {
964  BOOL res;
965  CHAR buffer[4000];
966  DWORD length, got;
967  const char *types[2] = { "*", NULL };
968  HINTERNET hi, hic = 0, hor = 0;
969 
970  trace("Starting InternetReadFile chunked test\n");
971 
972  trace("InternetOpenA <--\n");
974  ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
975  trace("InternetOpenA -->\n");
976 
977  if (hi == 0x0) goto abort;
978 
979  trace("InternetConnectA <--\n");
980  hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
981  NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
982  ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
983  trace("InternetConnectA -->\n");
984 
985  if (hic == 0x0) goto abort;
986 
987  trace("HttpOpenRequestA <--\n");
988  hor = HttpOpenRequestA(hic, "GET", "/tests/chunked", NULL, NULL, types,
990  0xdeadbead);
991  if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
992  /*
993  * If the internet name can't be resolved we are probably behind
994  * a firewall or in some other way not directly connected to the
995  * Internet. Not enough reason to fail the test. Just ignore and
996  * abort.
997  */
998  } else {
999  ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
1000  }
1001  trace("HttpOpenRequestA -->\n");
1002 
1003  if (hor == 0x0) goto abort;
1004 
1005  trace("HttpSendRequestA -->\n");
1006  SetLastError(0xdeadbeef);
1007  res = HttpSendRequestA(hor, "", -1, NULL, 0);
1009  "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
1010  trace("HttpSendRequestA <--\n");
1011 
1012  test_request_flags(hor, 0);
1013 
1014  length = 100;
1016  buffer[length]=0;
1017  trace("Option CONTENT_TYPE -> %i %s\n",res,buffer);
1018 
1019  SetLastError( 0xdeadbeef );
1020  length = 100;
1022  buffer[length]=0;
1023  trace("Option TRANSFER_ENCODING -> %i %s\n",res,buffer);
1025  "Failed to get TRANSFER_ENCODING option, error %u\n", GetLastError() );
1026  ok( !strcmp( buffer, "chunked" ) || ( ! res && proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
1027  "Wrong transfer encoding '%s'\n", buffer );
1028 
1029  SetLastError( 0xdeadbeef );
1030  length = 16;
1032  ok( !res, "Found CONTENT_LENGTH option '%s'\n", buffer );
1033  ok( GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "Wrong error %u\n", GetLastError() );
1034 
1035  length = 100;
1036  trace("Entering Query loop\n");
1037 
1038  while (TRUE)
1039  {
1041  ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
1042  ok(res, "InternetQueryDataAvailable failed, error %d\n", GetLastError());
1043  trace("got %u available\n",length);
1044  if (length)
1045  {
1046  char *buffer = HeapAlloc(GetProcessHeap(),0,length+1);
1047 
1048  SetLastError(0xdeadbeef);
1049  res = InternetReadFile(hor,buffer,length,&got);
1050  ok(GetLastError() == 0 ||
1051  broken(GetLastError() == 0xdeadbeef /* XP/W2K3 */), "Last Error not reset %u\n", GetLastError());
1052 
1053  buffer[got]=0;
1054  trace("ReadFile -> %i %i\n",res,got);
1055  ok( length == got, "only got %u of %u available\n", got, length );
1056  ok( buffer[got-1] == '\n', "received partial line '%s'\n", buffer );
1057 
1059  if (!got) break;
1060  }
1061  if (length == 0)
1062  {
1063  got = 0xdeadbeef;
1064  SetLastError(0xdeadbeef);
1065  res = InternetReadFile( hor, buffer, 1, &got );
1066  ok( res, "InternetReadFile failed: %u\n", GetLastError() );
1067  ok(GetLastError() == 0 ||
1068  broken(GetLastError() == 0xdeadbeef /* XP/W2K3 */), "Last Error not reset %u\n", GetLastError());
1069  ok( !got, "got %u\n", got );
1070  break;
1071  }
1072  }
1073 abort:
1074  trace("aborting\n");
1075  if (hor != 0x0) {
1076  res = InternetCloseHandle(hor);
1077  ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
1078  }
1079  if (hi != 0x0) {
1080  res = InternetCloseHandle(hi);
1081  ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
1082  }
1083 }
1084 
1086 {
1087  DWORD rc;
1088  DWORD length;
1089  const char *types[2] = { "*", NULL };
1090  HINTERNET hi, hic = 0, hor = 0;
1091  INTERNET_BUFFERSA inetbuffers;
1092 
1093  trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
1094  reset_events();
1095 
1096  trace("InternetOpenA <--\n");
1098  ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
1099  trace("InternetOpenA -->\n");
1100 
1101  if (hi == 0x0) goto abort;
1102 
1103  pInternetSetStatusCallbackA(hi,&callback);
1104 
1106 
1107  trace("InternetConnectA <--\n");
1108  hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
1109  NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
1110  ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
1111  trace("InternetConnectA -->\n");
1112 
1113  if (hic == 0x0) goto abort;
1114 
1117 
1118  trace("HttpOpenRequestA <--\n");
1119  hor = HttpOpenRequestA(hic, "GET", "/tests/redirect", NULL, NULL, types,
1121  0xdeadbead);
1122  if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
1123  /*
1124  * If the internet name can't be resolved we are probably behind
1125  * a firewall or in some other way not directly connected to the
1126  * Internet. Not enough reason to fail the test. Just ignore and
1127  * abort.
1128  */
1129  } else {
1130  ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
1131  }
1132  trace("HttpOpenRequestA -->\n");
1133 
1134  if (hor == 0x0) goto abort;
1135 
1140  {
1143  }
1156  if (flags & INTERNET_FLAG_ASYNC)
1158  else
1160 
1161  trace("HttpSendRequestA -->\n");
1162  SetLastError(0xdeadbeef);
1163  rc = HttpSendRequestA(hor, "", -1, NULL, 0);
1164  if (flags & INTERNET_FLAG_ASYNC)
1165  ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
1166  "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
1167  else
1169  "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
1170  trace("HttpSendRequestA <--\n");
1171 
1172  if (!rc && (GetLastError() == ERROR_IO_PENDING)) {
1174  ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
1175  }
1176 
1178  {
1181  }
1182  else
1183  {
1186  }
1194  if (flags & INTERNET_FLAG_ASYNC)
1196  else
1199  /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
1202 
1203  if(is_ie7plus) {
1204  rc = InternetReadFileExW(hor, NULL, 0, 0xdeadcafe);
1205  ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
1206  "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
1207  rc ? "TRUE" : "FALSE", GetLastError());
1208  }
1209 
1210  /* tests invalid dwStructSize */
1211  inetbuffers.dwStructSize = sizeof(inetbuffers)+1;
1212  inetbuffers.lpcszHeader = NULL;
1213  inetbuffers.dwHeadersLength = 0;
1214  inetbuffers.dwBufferLength = 10;
1215  inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
1216  inetbuffers.dwOffsetHigh = 1234;
1217  inetbuffers.dwOffsetLow = 5678;
1218  rc = InternetReadFileExA(hor, &inetbuffers, 0, 0xdeadcafe);
1219  ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
1220  "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
1221  rc ? "TRUE" : "FALSE", GetLastError());
1222  HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
1223 
1224  test_request_flags(hor, 0);
1225 
1226  /* tests to see whether lpcszHeader is used - it isn't */
1227  inetbuffers.dwStructSize = sizeof(inetbuffers);
1228  inetbuffers.lpcszHeader = (LPCSTR)0xdeadbeef;
1229  inetbuffers.dwHeadersLength = 255;
1230  inetbuffers.dwBufferLength = 0;
1231  inetbuffers.lpvBuffer = NULL;
1232  inetbuffers.dwOffsetHigh = 1234;
1233  inetbuffers.dwOffsetLow = 5678;
1234  rc = InternetReadFileExA(hor, &inetbuffers, 0, 0xdeadcafe);
1235  ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
1236  trace("read %i bytes\n", inetbuffers.dwBufferLength);
1237 
1238  rc = InternetReadFileExA(NULL, &inetbuffers, 0, 0xdeadcafe);
1239  ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
1240  "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
1241  rc ? "TRUE" : "FALSE", GetLastError());
1242 
1243  length = 0;
1244  trace("Entering Query loop\n");
1245 
1246  while (TRUE)
1247  {
1248  inetbuffers.dwStructSize = sizeof(inetbuffers);
1249  inetbuffers.dwBufferLength = 1024;
1250  inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
1251  inetbuffers.dwOffsetHigh = 1234;
1252  inetbuffers.dwOffsetLow = 5678;
1253 
1257  rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
1258  if (!rc)
1259  {
1260  if (GetLastError() == ERROR_IO_PENDING)
1261  {
1262  trace("InternetReadFileEx -> PENDING\n");
1264  "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
1269  ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
1270  }
1271  else
1272  {
1273  trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
1274  break;
1275  }
1276  }
1277  else
1278  {
1279  trace("InternetReadFileEx -> SUCCEEDED\n");
1281  if (inetbuffers.dwBufferLength)
1282  {
1285  }
1286  else
1287  {
1288  /* Win98 still sends these when 0 bytes are read, WinXP does not */
1291  }
1292  }
1293 
1294  trace("read %i bytes\n", inetbuffers.dwBufferLength);
1295  ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
1296 
1297  ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
1298  "InternetReadFileEx sets offsets to 0x%x%08x\n",
1299  inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
1300 
1301  HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
1302 
1303  if (!inetbuffers.dwBufferLength)
1304  break;
1305 
1306  length += inetbuffers.dwBufferLength;
1307  }
1308  ok(length > 0, "failed to read any of the document\n");
1309  trace("Finished. Read %d bytes\n", length);
1310 
1311 abort:
1312  close_async_handle(hi, 2);
1314 }
1315 
1316 static void InternetOpenUrlA_test(void)
1317 {
1318  HINTERNET myhinternet, myhttp;
1319  char buffer[0x400];
1320  DWORD size, readbytes, totalbytes=0;
1321  BOOL ret;
1322 
1325  "DeleteUrlCacheEntry returned %x, GetLastError() = %d\n", ret, GetLastError());
1326 
1327  myhinternet = InternetOpenA("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
1328  ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
1329  size = 0x400;
1331  ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
1332 
1333  SetLastError(0);
1334  myhttp = InternetOpenUrlA(myhinternet, TEST_URL, 0, 0,
1337  return; /* WinXP returns this when not connected to the net */
1338  ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
1339  ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
1340  ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
1341  totalbytes += readbytes;
1342  while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
1343  totalbytes += readbytes;
1344  trace("read 0x%08x bytes\n",totalbytes);
1345 
1346  InternetCloseHandle(myhttp);
1347  InternetCloseHandle(myhinternet);
1348 
1350  ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "INTERNET_FLAG_NO_CACHE_WRITE flag doesn't work\n");
1351 }
1352 
1353 static void HttpSendRequestEx_test(void)
1354 {
1355  HINTERNET hSession;
1356  HINTERNET hConnect;
1357  HINTERNET hRequest;
1358 
1359  INTERNET_BUFFERSA BufferIn;
1360  DWORD dwBytesWritten, dwBytesRead, error;
1361  CHAR szBuffer[256];
1362  int i;
1363  BOOL ret;
1364 
1365  static char szPostData[] = "mode=Test";
1366  static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
1367 
1368  hSession = InternetOpenA("Wine Regression Test",
1370  ok( hSession != NULL ,"Unable to open Internet session\n");
1371  hConnect = InternetConnectA(hSession, "test.winehq.org",
1373  0);
1374  ok( hConnect != NULL, "Unable to connect to http://test.winehq.org\n");
1375  hRequest = HttpOpenRequestA(hConnect, "POST", "/tests/post.php",
1377  if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1378  {
1379  skip( "Network unreachable, skipping test\n" );
1380  goto done;
1381  }
1382  ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
1383 
1385 
1386  BufferIn.dwStructSize = sizeof(BufferIn);
1387  BufferIn.Next = (INTERNET_BUFFERSA*)0xdeadcab;
1388  BufferIn.lpcszHeader = szContentType;
1389  BufferIn.dwHeadersLength = sizeof(szContentType)-1;
1390  BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
1391  BufferIn.lpvBuffer = szPostData;
1392  BufferIn.dwBufferLength = 3;
1393  BufferIn.dwBufferTotal = sizeof(szPostData)-1;
1394  BufferIn.dwOffsetLow = 0;
1395  BufferIn.dwOffsetHigh = 0;
1396 
1397  SetLastError(0xdeadbeef);
1398  ret = HttpSendRequestExA(hRequest, &BufferIn, NULL, 0 ,0);
1399  error = GetLastError();
1400  ok(ret, "HttpSendRequestEx Failed with error %u\n", error);
1401  ok(error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", error);
1402 
1404 
1405  for (i = 3; szPostData[i]; i++)
1406  ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
1407  "InternetWriteFile failed\n");
1408 
1410 
1411  ok(HttpEndRequestA(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
1412 
1413  test_request_flags(hRequest, 0);
1414 
1415  ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
1416  "Unable to read response\n");
1417  szBuffer[dwBytesRead] = 0;
1418 
1419  ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
1420  ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0 || broken(proxy_active()),"Got string %s\n",szBuffer);
1421 
1422  ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1423 done:
1424  ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1425  ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1426 }
1427 
1428 static void InternetOpenRequest_test(void)
1429 {
1431  static const char *types[] = { "*", "", NULL };
1432  static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
1433  static const WCHAR *typesW[] = { any, empty, NULL };
1434  BOOL ret;
1435 
1436  session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1437  ok(session != NULL ,"Unable to open Internet session\n");
1438 
1440  INTERNET_SERVICE_HTTP, 0, 0);
1441  ok(connect == NULL, "InternetConnectA should have failed\n");
1442  ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1443 
1445  INTERNET_SERVICE_HTTP, 0, 0);
1446  ok(connect == NULL, "InternetConnectA should have failed\n");
1447  ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1448 
1450  INTERNET_SERVICE_HTTP, 0, 0);
1451  ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1452 
1455  {
1456  skip( "Network unreachable, skipping test\n" );
1457  goto done;
1458  }
1459  ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1460 
1461  ret = HttpSendRequestW(request, NULL, 0, NULL, 0);
1462  ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1463  ok(InternetCloseHandle(request), "Close request handle failed\n");
1464 
1466  ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1467 
1468  ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1469  ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1470  ok(InternetCloseHandle(request), "Close request handle failed\n");
1471 
1472 done:
1473  ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1474  ok(InternetCloseHandle(session), "Close session handle failed\n");
1475 }
1476 
1477 static void test_cache_read(void)
1478 {
1479  HINTERNET session, connection, req;
1480  FILETIME now, tomorrow, yesterday;
1481  BYTE content[1000], buf[2000];
1482  char file_path[MAX_PATH];
1484  HANDLE file;
1485  DWORD size;
1486  unsigned i;
1487  BOOL res;
1488 
1489  static const char cache_only_url[] = "http://test.winehq.org/tests/cache-only";
1490  BYTE cache_headers[] = "HTTP/1.1 200 OK\r\n\r\n";
1491 
1492  trace("Testing cache read...\n");
1493  reset_events();
1494 
1495  for(i = 0; i < sizeof(content); i++)
1496  content[i] = '0' + (i%10);
1497 
1499  li.u.HighPart = now.dwHighDateTime;
1500  li.u.LowPart = now.dwLowDateTime;
1501  li.QuadPart += (LONGLONG)10000000 * 3600 * 24;
1502  tomorrow.dwHighDateTime = li.u.HighPart;
1503  tomorrow.dwLowDateTime = li.u.LowPart;
1504  li.QuadPart -= (LONGLONG)10000000 * 3600 * 24 * 2;
1505  yesterday.dwHighDateTime = li.u.HighPart;
1506  yesterday.dwLowDateTime = li.u.LowPart;
1507 
1508  res = CreateUrlCacheEntryA(cache_only_url, sizeof(content), "", file_path, 0);
1509  ok(res, "CreateUrlCacheEntryA failed: %u\n", GetLastError());
1510 
1512  ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
1513 
1514  WriteFile(file, content, sizeof(content), &size, NULL);
1515  CloseHandle(file);
1516 
1517  res = CommitUrlCacheEntryA(cache_only_url, file_path, tomorrow, yesterday, NORMAL_CACHE_ENTRY,
1518  cache_headers, sizeof(cache_headers)-1, "", 0);
1519  ok(res, "CommitUrlCacheEntryA failed: %u\n", GetLastError());
1520 
1522  ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
1523 
1524  pInternetSetStatusCallbackA(session, callback);
1525 
1527  connection = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT,
1528  NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
1529  ok(connection != NULL,"InternetConnect failed with error %u\n", GetLastError());
1531 
1533  req = HttpOpenRequestA(connection, "GET", "/tests/cache-only", NULL, NULL, NULL, 0, 0xdeadbead);
1534  ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
1536 
1544 
1545  res = HttpSendRequestA(req, NULL, -1, NULL, 0);
1546  todo_wine
1547  ok(res, "HttpSendRequest failed: %u\n", GetLastError());
1548 
1549  if(res) {
1550  size = 0;
1551  res = InternetQueryDataAvailable(req, &size, 0, 0);
1552  ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
1553  ok(size == sizeof(content), "size = %u\n", size);
1554 
1555  size = sizeof(buf);
1556  res = InternetReadFile(req, buf, sizeof(buf), &size);
1557  ok(res, "InternetReadFile failed: %u\n", GetLastError());
1558  ok(size == sizeof(content), "size = %u\n", size);
1559  ok(!memcmp(content, buf, sizeof(content)), "unexpected content\n");
1560  }
1561 
1563 
1571 
1572  res = DeleteUrlCacheEntryA(cache_only_url);
1573  ok(res, "DeleteUrlCacheEntryA failed: %u\n", GetLastError());
1574 }
1575 
1576 static void test_http_cache(void)
1577 {
1580  DWORD size, file_size;
1581  BYTE buf[100];
1582  HANDLE file;
1583  BOOL ret;
1584  FILETIME filetime_zero = {0};
1585 
1586  static const char cached_content[] = "data read from cache";
1587  static const char *types[] = { "*", "", NULL };
1588 
1589  session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1590  ok(session != NULL ,"Unable to open Internet session\n");
1591 
1593  INTERNET_SERVICE_HTTP, 0, 0);
1594  ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1595 
1596  request = HttpOpenRequestA(connect, NULL, "/tests/hello.html", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
1598  {
1599  skip( "Network unreachable, skipping test\n" );
1600 
1601  ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1602  ok(InternetCloseHandle(session), "Close session handle failed\n");
1603 
1604  return;
1605  }
1606  ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1607 
1608  size = sizeof(url);
1610  ok(ret, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
1611  ok(!strcmp(url, "http://test.winehq.org/tests/hello.html"), "Wrong URL %s\n", url);
1612 
1613  size = sizeof(file_name);
1615  ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1616  ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1617  ok(!size, "size = %d\n", size);
1618 
1619  ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1620  ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1621 
1622  size = sizeof(file_name);
1624  ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1625 
1628  ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1630  ok(file_size == 106, "file size = %u\n", file_size);
1631 
1632  size = sizeof(buf);
1633  ret = InternetReadFile(request, buf, sizeof(buf), &size);
1634  ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1635  ok(size == 100, "size = %u\n", size);
1636 
1638  ok(file_size == 106, "file size = %u\n", file_size);
1639  CloseHandle(file);
1640 
1642  ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%u)\n", ret, GetLastError());
1643 
1644  ok(InternetCloseHandle(request), "Close request handle failed\n");
1645 
1648  ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1649  ret = WriteFile(file, cached_content, sizeof(cached_content), &size, NULL);
1650  ok(ret && size, "WriteFile failed: %d, %d\n", ret, size);
1651  ret = CommitUrlCacheEntryA(url, file_name, filetime_zero, filetime_zero, NORMAL_CACHE_ENTRY, NULL, 0, NULL, 0);
1652  ok(ret, "CommitUrlCacheEntry failed: %d\n", GetLastError());
1653  CloseHandle(file);
1654 
1655  /* Send the same request, requiring it to be retrieved from the cache */
1656  request = HttpOpenRequestA(connect, "GET", "/tests/hello.html", NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
1657 
1658  ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1659  ok(ret, "HttpSendRequest failed\n");
1660 
1661  size = sizeof(buf);
1662  ret = InternetReadFile(request, buf, sizeof(buf), &size);
1663  ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1664  ok(size == 100, "size = %u\n", size);
1665  buf[99] = 0;
1666  todo_wine ok(!strcmp((char*)buf, cached_content), "incorrect page data: %s\n", (char*)buf);
1667 
1668  ok(InternetCloseHandle(request), "Close request handle failed\n");
1669 
1671  request = HttpOpenRequestA(connect, "GET", "/tests/hello.html", NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
1672  ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1673  todo_wine ok(!ret, "HttpSendRequest succeeded\n");
1674  if(!ret)
1675  ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() = %d\n", GetLastError());
1676  ok(InternetCloseHandle(request), "Close request handle failed\n");
1677 
1679  ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1680 
1681  size = sizeof(file_name);
1683  ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1684  ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1685  ok(!size, "size = %d\n", size);
1686 
1687  ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1688  ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1689 
1690  size = sizeof(file_name);
1691  file_name[0] = 0;
1693  if (ret)
1694  {
1697  ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1698  CloseHandle(file);
1699  }
1700  else
1701  {
1702  /* < IE8 */
1703  ok(file_name[0] == 0, "Didn't expect a file name\n");
1704  }
1705 
1706  ok(InternetCloseHandle(request), "Close request handle failed\n");
1707  ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1708  ok(InternetCloseHandle(session), "Close session handle failed\n");
1709 
1710  test_cache_read();
1711 }
1712 
1714 {
1715  char file_name[MAX_PATH];
1716  test_request_t req;
1717  HANDLE lock, lock2;
1718  DWORD size;
1719  BOOL ret;
1720 
1721  open_simple_request(&req, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, "/tests/hello.html");
1722 
1723  size = sizeof(file_name);
1725  ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1726  ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1727  ok(!size, "size = %d\n", size);
1728 
1729  lock = NULL;
1731  ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
1732 
1733  ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
1734  ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1735 
1736  size = sizeof(file_name);
1738  ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1739 
1741  ok(ret, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
1742  ok(lock != NULL, "lock == NULL\n");
1743 
1744  ret = InternetLockRequestFile(req.request, &lock2);
1745  ok(ret, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
1746  ok(lock == lock2, "lock != lock2\n");
1747 
1748  ret = InternetUnlockRequestFile(lock2);
1749  ok(ret, "InternetUnlockRequestFile failed: %u\n", GetLastError());
1750 
1752  ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%u)\n", ret, GetLastError());
1753 
1754  ok(InternetCloseHandle(req.request), "Close request handle failed\n");
1755 
1757  ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%u)\n", ret, GetLastError());
1758 
1760  ok(ret, "InternetUnlockRequestFile failed: %u\n", GetLastError());
1761 
1763  ok(ret, "Deleting file returned %x(%u)\n", ret, GetLastError());
1764 }
1765 
1766 static void HttpHeaders_test(void)
1767 {
1768  HINTERNET hSession;
1769  HINTERNET hConnect;
1770  HINTERNET hRequest;
1771  CHAR buffer[256];
1772  WCHAR wbuffer[256];
1773  DWORD len = 256;
1774  DWORD oldlen;
1775  DWORD index = 0;
1776  BOOL ret;
1777 
1778  hSession = InternetOpenA("Wine Regression Test",
1780  ok( hSession != NULL ,"Unable to open Internet session\n");
1781  hConnect = InternetConnectA(hSession, "test.winehq.org",
1783  0);
1784  ok( hConnect != NULL, "Unable to connect to http://test.winehq.org\n");
1785  hRequest = HttpOpenRequestA(hConnect, "POST", "/tests/post.php",
1787  if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1788  {
1789  skip( "Network unreachable, skipping test\n" );
1790  goto done;
1791  }
1792  ok( hRequest != NULL, "Failed to open request handle\n");
1793 
1794  index = 0;
1795  len = sizeof(buffer);
1796  strcpy(buffer,"Warning");
1798  buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1799 
1800  ok(HttpAddRequestHeadersA(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1801  "Failed to add new header\n");
1802 
1803  index = 0;
1804  len = sizeof(buffer);
1805  strcpy(buffer,"Warning");
1807  buffer,&len,&index),"Unable to query header\n");
1808  ok(index == 1, "Index was not incremented\n");
1809  ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1810  ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1811  ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1812  len = sizeof(buffer);
1813  strcpy(buffer,"Warning");
1815  buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1816 
1817  index = 0;
1818  len = 5; /* could store the string but not the NULL terminator */
1819  strcpy(buffer,"Warning");
1821  buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1822  ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1823  ok(index == 0, "Index was incremented\n");
1824  ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1825  ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1826 
1827  /* a call with NULL will fail but will return the length */
1828  index = 0;
1829  len = sizeof(buffer);
1830  SetLastError(0xdeadbeef);
1832  NULL,&len,&index) == FALSE,"Query worked\n");
1833  ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1834  ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1835  ok(index == 0, "Index was incremented\n");
1836 
1837  /* even for a len that is too small */
1838  index = 0;
1839  len = 15;
1840  SetLastError(0xdeadbeef);
1842  NULL,&len,&index) == FALSE,"Query worked\n");
1843  ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1844  ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1845  ok(index == 0, "Index was incremented\n");
1846 
1847  index = 0;
1848  len = 0;
1849  SetLastError(0xdeadbeef);
1851  NULL,&len,&index) == FALSE,"Query worked\n");
1852  ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1853  ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1854  ok(index == 0, "Index was incremented\n");
1855  oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1856 
1857 
1858  /* a working query */
1859  index = 0;
1860  len = sizeof(buffer);
1861  memset(buffer, 'x', sizeof(buffer));
1863  buffer,&len,&index),"Unable to query header\n");
1864  ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1865  ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1866  ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1867  /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1868  ok(strncmp(buffer, "POST /tests/post.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1869  ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1870  ok(index == 0, "Index was incremented\n");
1871 
1872  /* Like above two tests, but for W version */
1873 
1874  index = 0;
1875  len = 0;
1876  SetLastError(0xdeadbeef);
1878  NULL,&len,&index) == FALSE,"Query worked\n");
1879  ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1880  ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1881  ok(index == 0, "Index was incremented\n");
1882  oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1883 
1884  /* a working query */
1885  index = 0;
1886  len = sizeof(wbuffer);
1887  memset(wbuffer, 'x', sizeof(wbuffer));
1889  wbuffer,&len,&index),"Unable to query header\n");
1890  ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1891  ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1892  ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1893  ok(index == 0, "Index was incremented\n");
1894 
1895  /* end of W version tests */
1896 
1897  /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1898  index = 0;
1899  len = sizeof(buffer);
1900  memset(buffer, 'x', sizeof(buffer));
1902  buffer,&len,&index) == TRUE,"Query failed\n");
1903  ok(len == 2 || len == 4 /* win10 */, "Expected 2 or 4, got %d\n", len);
1904  ok(memcmp(buffer, "\r\n\r\n", len) == 0, "Expected CRLF, got '%s'\n", buffer);
1905  ok(index == 0, "Index was incremented\n");
1906 
1907  ok(HttpAddRequestHeadersA(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1908  "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1909 
1910  index = 0;
1911  len = sizeof(buffer);
1912  strcpy(buffer,"Warning");
1914  buffer,&len,&index),"Unable to query header\n");
1915  ok(index == 1, "Index was not incremented\n");
1916  ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1917  len = sizeof(buffer);
1918  strcpy(buffer,"Warning");
1920  buffer,&len,&index),"Failed to get second header\n");
1921  ok(index == 2, "Index was not incremented\n");
1922  ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1923  len = sizeof(buffer);
1924  strcpy(buffer,"Warning");
1926  buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1927 
1928  ok(HttpAddRequestHeadersA(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1929 
1930  index = 0;
1931  len = sizeof(buffer);
1932  strcpy(buffer,"Warning");
1934  buffer,&len,&index),"Unable to query header\n");
1935  ok(index == 1, "Index was not incremented\n");
1936  ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1937  len = sizeof(buffer);
1938  strcpy(buffer,"Warning");
1940  buffer,&len,&index),"Failed to get second header\n");
1941  ok(index == 2, "Index was not incremented\n");
1942  ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1943  len = sizeof(buffer);
1944  strcpy(buffer,"Warning");
1946  buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1947 
1948  ok(HttpAddRequestHeadersA(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1949 
1950  index = 0;
1951  len = sizeof(buffer);
1952  strcpy(buffer,"Warning");
1954  buffer,&len,&index),"Unable to query header\n");
1955  ok(index == 1, "Index was not incremented\n");
1956  ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1957  len = sizeof(buffer);
1958  strcpy(buffer,"Warning");
1960  buffer,&len,&index),"Failed to get second header\n");
1961  ok(index == 2, "Index was not incremented\n");
1962  ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1963  len = sizeof(buffer);
1964  strcpy(buffer,"Warning");
1966  buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1967 
1968  ok(HttpAddRequestHeadersA(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1969 
1970  index = 0;
1971  len = sizeof(buffer);
1972  strcpy(buffer,"Warning");
1974  buffer,&len,&index),"Unable to query header\n");
1975  ok(index == 1, "Index was not incremented\n");
1976  ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1977  len = sizeof(buffer);
1978  strcpy(buffer,"Warning");
1979  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1980  ok(index == 2, "Index was not incremented\n");
1981  ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1982  len = sizeof(buffer);
1983  strcpy(buffer,"Warning");
1984  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1985 
1986  ok(HttpAddRequestHeadersA(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1987 
1988  index = 0;
1989  len = sizeof(buffer);
1990  strcpy(buffer,"Warning");
1991  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1992  ok(index == 1, "Index was not incremented\n");
1993  ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1994  len = sizeof(buffer);
1995  strcpy(buffer,"Warning");
1996  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1997  ok(index == 2, "Index was not incremented\n");
1998  ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1999  len = sizeof(buffer);
2000  strcpy(buffer,"Warning");
2001  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
2002 
2003  ok(HttpAddRequestHeadersA(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
2004 
2005  index = 0;
2006  len = sizeof(buffer);
2007  strcpy(buffer,"Warning");
2008  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
2009  ok(index == 1, "Index was not incremented\n");
2010  ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
2011  len = sizeof(buffer);
2012  strcpy(buffer,"Warning");
2013  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
2014  ok(index == 2, "Index was not incremented\n");
2015  ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
2016  len = sizeof(buffer);
2017  strcpy(buffer,"Warning");
2018  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
2019 
2020  ok(HttpAddRequestHeadersA(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");
2021 
2022  index = 0;
2023  len = sizeof(buffer);
2024  strcpy(buffer,"Warning");
2025  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
2026  ok(index == 1, "Index was not incremented\n");
2027  ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
2028  len = sizeof(buffer);
2029  strcpy(buffer,"Warning");
2030  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
2031  ok(index == 2, "Index was not incremented\n");
2032  ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
2033  len = sizeof(buffer);
2034  strcpy(buffer,"Warning");
2035  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
2036 
2037  /* Ensure that blank headers are ignored and don't cause a failure */
2038  ok(HttpAddRequestHeadersA(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");
2039 
2040  index = 0;
2041  len = sizeof(buffer);
2042  strcpy(buffer,"BlankTest");
2043  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
2044  ok(index == 1, "Index was not incremented\n");
2045  ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
2046 
2047  /* Ensure that malformed header separators are ignored and don't cause a failure */
2048  ok(HttpAddRequestHeadersA(hRequest,"\r\rMalformedTest:value\n\nMalformedTestTwo: value2\rMalformedTestThree: value3\n\n\r\r\n",-1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE),
2049  "Failed to add header with malformed entries in list\n");
2050 
2051  index = 0;
2052  len = sizeof(buffer);
2053  strcpy(buffer,"MalformedTest");
2054  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
2055  ok(index == 1, "Index was not incremented\n");
2056  ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
2057  index = 0;
2058  len = sizeof(buffer);
2059  strcpy(buffer,"MalformedTestTwo");
2060  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
2061  ok(index == 1, "Index was not incremented\n");
2062  ok(strcmp(buffer,"value2")==0, "incorrect string was returned(%s)\n",buffer);
2063  index = 0;
2064  len = sizeof(buffer);
2065  strcpy(buffer,"MalformedTestThree");
2066  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
2067  ok(index == 1, "Index was not incremented\n");
2068  ok(strcmp(buffer,"value3")==0, "incorrect string was returned(%s)\n",buffer);
2069 
2070  ret = HttpAddRequestHeadersA(hRequest, "Authorization: Basic\r\n", -1, HTTP_ADDREQ_FLAG_ADD);
2071  ok(ret, "unable to add header %u\n", GetLastError());
2072 
2073  index = 0;
2074  buffer[0] = 0;
2075  len = sizeof(buffer);
2077  ok(ret, "unable to query header %u\n", GetLastError());
2078  ok(index == 1, "index was not incremented\n");
2079  ok(!strcmp(buffer, "Basic"), "incorrect string was returned (%s)\n", buffer);
2080 
2081  ret = HttpAddRequestHeadersA(hRequest, "Authorization:\r\n", -1, HTTP_ADDREQ_FLAG_REPLACE);
2082  ok(ret, "unable to remove header %u\n", GetLastError());
2083 
2084  index = 0;
2085  len = sizeof(buffer);
2086  SetLastError(0xdeadbeef);
2088  "header still present\n");
2090 
2091  ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
2092 done:
2093  ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
2094  ok(InternetCloseHandle(hSession), "Close session handle failed\n");
2095 }
2096 
2097 static const char garbagemsg[] =
2098 "Garbage: Header\r\n";
2099 
2100 static const char contmsg[] =
2101 "HTTP/1.1 100 Continue\r\n";
2102 
2103 static const char expandcontmsg[] =
2104 "HTTP/1.1 100 Continue\r\n"
2105 "Server: winecontinue\r\n"
2106 "Tag: something witty\r\n";
2107 
2108 static const char okmsg[] =
2109 "HTTP/1.1 200 OK\r\n"
2110 "Server: winetest\r\n"
2111 "\r\n";
2112 
2113 static const char okmsg201[] =
2114 "HTTP/1.1 201 OK\r\n"
2115 "Server: winetest\r\n"
2116 "\r\n";
2117 
2118 static const char okmsg2[] =
2119 "HTTP/1.1 200 OK\r\n"
2120 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
2121 "Server: winetest\r\n"
2122 "Content-Length: 0\r\n"
2123 "Set-Cookie: one\r\n"
2124 "Set-Cookie: two\r\n"
2125 "\r\n";
2126 
2128 static const char largemsg[] =
2129 "HTTP/1.1 200 OK\r\n"
2130 "Content-Length: %I64u\r\n"
2131 "\r\n";
2132 
2133 static const char okmsg_cookie_path[] =
2134 "HTTP/1.1 200 OK\r\n"
2135 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
2136 "Server: winetest\r\n"
2137 "Content-Length: 0\r\n"
2138 "Set-Cookie: subcookie2=data; path=/test_cookie_set_path\r\n"
2139 "\r\n";
2140 
2141 static const char okmsg_cookie[] =
2142 "HTTP/1.1 200 OK\r\n"
2143 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
2144 "Server: winetest\r\n"
2145 "Content-Length: 0\r\n"
2146 "Set-Cookie: testcookie=testvalue\r\n"
2147 "\r\n";
2148 
2149 static const char notokmsg[] =
2150 "HTTP/1.1 400 Bad Request\r\n"
2151 "Server: winetest\r\n"
2152 "\r\n";
2153 
2154 static const char noauthmsg[] =
2155 "HTTP/1.1 401 Unauthorized\r\n"
2156 "Server: winetest\r\n"
2157 "Connection: close\r\n"
2158 "WWW-Authenticate: Basic realm=\"placebo\"\r\n"
2159 "\r\n";
2160 
2161 static const char noauthmsg2[] =
2162 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed\r\n"
2163 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"
2164 "\0d`0|6\n"
2165 "Server: winetest\r\n";
2166 
2167 static const char proxymsg[] =
2168 "HTTP/1.1 407 Proxy Authentication Required\r\n"
2169 "Server: winetest\r\n"
2170 "Proxy-Connection: close\r\n"
2171 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
2172 "\r\n";
2173 
2174 static const char page1[] =
2175 "<HTML>\r\n"
2176 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
2177 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
2178 "</HTML>\r\n\r\n";
2179 
2180 static const char ok_with_length[] =
2181 "HTTP/1.1 200 OK\r\n"
2182 "Connection: Keep-Alive\r\n"
2183 "Content-Length: 18\r\n\r\n"
2184 "HTTP/1.1 211 OK\r\n\r\n";
2185 
2186 static const char ok_with_length2[] =
2187 "HTTP/1.1 210 OK\r\n"
2188 "Connection: Keep-Alive\r\n"
2189 "Content-Length: 19\r\n\r\n"
2190 "HTTP/1.1 211 OK\r\n\r\n";
2191 
2192 struct server_info {
2194  int port;
2195 };
2196 
2197 static int test_cache_gzip;
2198 static const char *send_buffer;
2199 static int server_socket;
2200 
2202 {
2203  struct server_info *si = param;
2204  int r, c = -1, i, on, count = 0;
2205  SOCKET s;
2206  struct sockaddr_in sa;
2207  char *buffer;
2208  size_t buffer_size;
2209  WSADATA wsaData;
2210  int last_request = 0;
2211  char host_header[22];
2212  char host_header_override[30];
2213  static int test_no_cache = 0;
2214 
2215  WSAStartup(MAKEWORD(1,1), &wsaData);
2216 
2217  s = socket(AF_INET, SOCK_STREAM, 0);
2218  if (s == INVALID_SOCKET)
2219  return 1;
2220 
2221  on = 1;
2222  setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
2223 
2224  memset(&sa, 0, sizeof sa);
2225  sa.sin_family = AF_INET;
2226  sa.sin_port = htons(si->port);
2227  sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
2228 
2229  r = bind(s, (struct sockaddr*) &sa, sizeof sa);
2230  if (r<0)
2231  return 1;
2232 
2233  listen(s, 0);
2234 
2235  SetEvent(si->hEvent);
2236 
2237  sprintf(host_header, "Host: localhost:%d", si->port);
2238  sprintf(host_header_override, "Host: test.local:%d\r\n", si->port);
2239  buffer = HeapAlloc(GetProcessHeap(), 0, buffer_size = 1000);
2240 
2241  do
2242  {
2243  if(c == -1)
2244  c = accept(s, NULL, NULL);
2245 
2246  memset(buffer, 0, buffer_size);
2247  for(i=0;; i++)
2248  {
2249  if(i == buffer_size)
2251 
2252  r = recv(c, buffer+i, 1, 0);
2253  if (r != 1)
2254  break;
2255  if (i<4) continue;
2256  if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
2257  buffer[i-3] == '\r' && buffer[i-1] == '\r')
2258  break;
2259  }
2260  if (strstr(buffer, "GET /test1"))
2261  {
2262  if (!strstr(buffer, "Content-Length: 0"))
2263  {
2264  send(c, okmsg, sizeof okmsg-1, 0);
2265  send(c, page1, sizeof page1-1, 0);
2266  }
2267  else
2268  send(c, notokmsg, sizeof notokmsg-1, 0);
2269  }
2270  if (strstr(buffer, "CONNECT "))
2271  {
2272  if (!strstr(buffer, "Content-Length: 0"))
2273  send(c, notokmsg, sizeof notokmsg-1, 0);
2274  else
2275  send(c, proxymsg, sizeof proxymsg-1, 0);
2276  }
2277  if (strstr(buffer, "/test2"))
2278  {
2279  if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
2280  {
2281  send(c, okmsg, sizeof okmsg-1, 0);
2282  send(c, page1, sizeof page1-1, 0);
2283  }
2284  else
2285  send(c, proxymsg, sizeof proxymsg-1, 0);
2286  }
2287  if (strstr(buffer, "/test3"))
2288  {
2289  if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2290  send(c, okmsg, sizeof okmsg-1, 0);
2291  else
2292  send(c, noauthmsg, sizeof noauthmsg-1, 0);
2293  }
2294  if (strstr(buffer, "/test4"))
2295  {
2296  if (strstr(buffer, "Connection: Close"))
2297  send(c, okmsg, sizeof okmsg-1, 0);
2298  else
2299  send(c, notokmsg, sizeof notokmsg-1, 0);
2300  }
2301  if (strstr(buffer, "POST /test5") ||
2302  strstr(buffer, "RPC_IN_DATA /test5") ||
2303  strstr(buffer, "RPC_OUT_DATA /test5"))
2304  {
2305  if (strstr(buffer, "Content-Length: 0"))
2306  {
2307  send(c, okmsg, sizeof okmsg-1, 0);
2308  send(c, page1, sizeof page1-1, 0);
2309  }
2310  else
2311  send(c, notokmsg, sizeof notokmsg-1, 0);
2312  }
2313  if (strstr(buffer, "GET /test6"))
2314  {
2315  send(c, contmsg, sizeof contmsg-1, 0);
2316  send(c, contmsg, sizeof contmsg-1, 0);
2317  send(c, okmsg, sizeof okmsg-1, 0);
2318  send(c, page1, sizeof page1-1, 0);
2319  }
2320  if (strstr(buffer, "POST /test7"))
2321  {
2322  if (strstr(buffer, "Content-Length: 100"))
2323  {
2324  if (strstr(buffer, "POST /test7b"))
2326  send(c, okmsg, sizeof okmsg-1, 0);
2327  send(c, page1, sizeof page1-1, 0);
2328  }
2329  else
2330  send(c, notokmsg, sizeof notokmsg-1, 0);
2331  }
2332  if (strstr(buffer, "/test8"))
2333  {
2334  if (!strstr(buffer, "Connection: Close") &&
2335  strstr(buffer, "Connection: Keep-Alive") &&
2336  !strstr(buffer, "Cache-Control: no-cache") &&
2337  !strstr(buffer, "Pragma: no-cache") &&
2338  strstr(buffer, host_header))
2339  send(c, okmsg, sizeof okmsg-1, 0);
2340  else
2341  send(c, notokmsg, sizeof notokmsg-1, 0);
2342  }
2343  if (strstr(buffer, "/test9"))
2344  {
2345  if (!strstr(buffer, "Connection: Close") &&
2346  !strstr(buffer, "Connection: Keep-Alive") &&
2347  !strstr(buffer, "Cache-Control: no-cache") &&
2348  !strstr(buffer, "Pragma: no-cache") &&
2349  strstr(buffer, host_header))
2350  send(c, okmsg, sizeof okmsg-1, 0);
2351  else
2352  send(c, notokmsg, sizeof notokmsg-1, 0);
2353  }
2354  if (strstr(buffer, "/testA"))
2355  {
2356  if (!strstr(buffer, "Connection: Close") &&
2357  !strstr(buffer, "Connection: Keep-Alive") &&
2358  (strstr(buffer, "Cache-Control: no-cache") ||
2359  strstr(buffer, "Pragma: no-cache")) &&
2360  strstr(buffer, host_header))
2361  send(c, okmsg, sizeof okmsg-1, 0);
2362  else
2363  send(c, notokmsg, sizeof notokmsg-1, 0);
2364  }
2365  if (strstr(buffer, "/testC"))
2366  {
2367  if (strstr(buffer, "cookie=biscuit"))
2368  send(c, okmsg, sizeof okmsg-1, 0);
2369  else
2370  send(c, notokmsg, sizeof notokmsg-1, 0);
2371  }
2372  if (strstr(buffer, "/testD"))
2373  {
2374  send(c, okmsg2, sizeof okmsg2-1, 0);
2375  }
2376  if (strstr(buffer, "/testE"))
2377  {
2378  send(c, noauthmsg2, sizeof noauthmsg2-1, 0);
2379  }
2380  if (strstr(buffer, "GET /quit"))
2381  {
2382  send(c, okmsg, sizeof okmsg-1, 0);
2383  send(c, page1, sizeof page1-1, 0);
2384  last_request = 1;
2385  }
2386  if (strstr(buffer, "GET /testF"))
2387  {
2388  send(c, expandcontmsg, sizeof expandcontmsg-1, 0);
2389  send(c, garbagemsg, sizeof garbagemsg-1, 0);
2390  send(c, contmsg, sizeof contmsg-1, 0);
2391  send(c, garbagemsg, sizeof garbagemsg-1, 0);
2392  send(c, okmsg, sizeof okmsg-1, 0);
2393  send(c, page1, sizeof page1-1, 0);
2394  }
2395  if (strstr(buffer, "GET /testG"))
2396  {
2397  send(c, page1, sizeof page1-1, 0);
2398  }
2399 
2400  if (strstr(buffer, "GET /testJ"))
2401  {
2402  if (count == 0)
2403  {
2404  count++;
2405  send(c, ok_with_length, sizeof(ok_with_length)-1, 0);
2406  }
2407  else
2408  {
2409  send(c, ok_with_length2, sizeof(ok_with_length2)-1, 0);
2410  count = 0;
2411  }
2412  }
2413  if (strstr(buffer, "GET /testH"))
2414  {
2415  send(c, ok_with_length2, sizeof(ok_with_length2)-1, 0);
2417  send(c, ok_with_length, sizeof(ok_with_length)-1, 0);
2418  }
2419 
2420  if (strstr(buffer, "GET /test_no_content"))
2421  {
2422  static const char nocontentmsg[] = "HTTP/1.1 204 No Content\r\nConnection: close\r\n\r\n";
2423  send(c, nocontentmsg, sizeof(nocontentmsg)-1, 0);
2424  }
2425  if (strstr(buffer, "GET /test_conn_close"))
2426  {
2427  static const char conn_close_response[] = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nsome content";
2428  send(c, conn_close_response, sizeof(conn_close_response)-1, 0);
2430  trace("closing connection\n");
2431  }
2432  if (strstr(buffer, "GET /test_cache_control_no_cache"))
2433  {
2434  static const char no_cache_response[] = "HTTP/1.1 200 OK\r\nCache-Control: no-cache\r\n\r\nsome content";
2435  if(!test_no_cache++)
2436  send(c, no_cache_response, sizeof(no_cache_response)-1, 0);
2437  else
2438  send(c, okmsg, sizeof(okmsg)-1, 0);
2439  }
2440  if (strstr(buffer, "GET /test_cache_control_no_store"))
2441  {
2442  static const char no_cache_response[] = "HTTP/1.1 200 OK\r\nCache-Control: junk, \t No-StOrE\r\n\r\nsome content";
2443  send(c, no_cache_response, sizeof(no_cache_response)-1, 0);
2444  }
2445  if (strstr(buffer, "GET /test_cache_gzip"))
2446  {
2447  static const char gzip_response[] = "HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nContent-Type: text/html\r\n\r\n"
2448  "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x4b\xaf\xca\x2c\x50\x28"
2449  "\x49\x2d\x2e\xe1\x02\x00\x62\x92\xc7\x6c\x0a\x00\x00\x00";
2450  if(!test_cache_gzip++)
2451  send(c, gzip_response, sizeof(gzip_response), 0);
2452  else
2453  send(c, notokmsg, sizeof(notokmsg)-1, 0);
2454  }
2455  if (strstr(buffer, "HEAD /test_head")) {
2456  static const char head_response[] =
2457  "HTTP/1.1 200 OK\r\n"
2458  "Connection: Keep-Alive\r\n"
2459  "Content-Length: 100\r\n"
2460  "\r\n";
2461 
2462  send(c, head_response, sizeof(head_response), 0);
2463  continue;
2464  }
2465  if (strstr(buffer, "GET /send_from_buffer"))
2467  if (strstr(buffer, "/test_cache_control_verb"))
2468  {
2469  if (!memcmp(buffer, "GET ", sizeof("GET ")-1) &&
2470  !strstr(buffer, "Cache-Control: no-cache\r\n")) send(c, okmsg, sizeof(okmsg)-1, 0);
2471  else if (strstr(buffer, "Cache-Control: no-cache\r\n")) send(c, okmsg, sizeof(okmsg)-1, 0);
2472  else send(c, notokmsg, sizeof(notokmsg)-1, 0);
2473  }
2474  if (strstr(buffer, "/test_request_content_length"))
2475  {
2476  static char msg[] = "HTTP/1.1 200 OK\r\nConnection: Keep-Alive\r\n\r\n";
2477  static int seen_content_length;
2478 
2479  if (!seen_content_length)
2480  {
2481  if (strstr(buffer, "Content-Length: 0"))
2482  {
2483  seen_content_length = 1;
2484  send(c, msg, sizeof msg-1, 0);
2485  }
2486  else send(c, notokmsg, sizeof notokmsg-1, 0);
2488  }
2489  else
2490  {
2491  if (strstr(buffer, "Content-Length: 0")) send(c, msg, sizeof msg-1, 0);
2492  else send(c, notokmsg, sizeof notokmsg-1, 0);
2494  }
2495  }
2496  if (strstr(buffer, "GET /test_premature_disconnect"))
2497  trace("closing connection\n");
2498  if (strstr(buffer, "HEAD /upload.txt"))
2499  {
2500  if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2501  send(c, okmsg, sizeof okmsg-1, 0);
2502  else
2503  send(c, noauthmsg, sizeof noauthmsg-1, 0);
2504  }
2505  if (strstr(buffer, "PUT /upload2.txt"))
2506  {
2507  if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2508  send(c, okmsg, sizeof okmsg-1, 0);
2509  else
2510  send(c, notokmsg, sizeof notokmsg-1, 0);
2511  }
2512  if (strstr(buffer, "HEAD /upload3.txt"))
2513  {
2514  if (strstr(buffer, "Authorization: Basic dXNlcjE6cHdkMQ=="))
2515  send(c, okmsg, sizeof okmsg-1, 0);
2516  else
2517  send(c, noauthmsg, sizeof noauthmsg-1, 0);
2518  }
2519  if (strstr(buffer, "HEAD /upload4.txt"))
2520  {
2521  if (strstr(buffer, "Authorization: Bearer dXNlcjE6cHdkMQ=="))
2522  send(c, okmsg, sizeof okmsg-1, 0);
2523  else if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2524  send(c, okmsg201, sizeof okmsg-1, 0);
2525  else
2526  send(c, noauthmsg, sizeof noauthmsg-1, 0);
2527  }
2528  if (strstr(buffer, "/test_cookie_path1"))
2529  {
2530  if (strstr(buffer, "subcookie=data"))
2531  send(c, okmsg, sizeof okmsg-1, 0);
2532  else
2533  send(c, notokmsg, sizeof notokmsg-1, 0);
2534  }
2535  if (strstr(buffer, "/test_cookie_path2"))
2536  {
2537  if (strstr(buffer, "subcookie2=data"))
2538  send(c, okmsg, sizeof okmsg-1, 0);
2539  else
2540  send(c, notokmsg, sizeof notokmsg-1, 0);
2541  }
2542  if (strstr(buffer, "/test_cookie_set_path"))
2543  {
2544  send(c, okmsg_cookie_path, sizeof okmsg_cookie_path-1, 0);
2545  }
2546  if (strstr(buffer, "/test_cookie_merge"))
2547  {
2548  if (strstr(buffer, "subcookie=data") &&
2549  !strstr(buffer, "manual_cookie=test"))
2550  send(c, okmsg, sizeof okmsg-1, 0);
2551  else
2552  send(c, notokmsg, sizeof notokmsg-1, 0);
2553  }
2554  if (strstr(buffer, "/test_cookie_set_host_override"))
2555  {
2556  send(c, okmsg_cookie, sizeof okmsg_cookie-1, 0);
2557  }
2558  if (strstr(buffer, "/test_cookie_check_host_override"))
2559  {
2560  if (strstr(buffer, "Cookie:") && strstr(buffer, "testcookie=testvalue"))
2561  send(c, okmsg, sizeof okmsg-1, 0);
2562  else
2563  send(c, notokmsg, sizeof notokmsg-1, 0);
2564  }
2565  if (strstr(buffer, "/test_cookie_check_different_host"))
2566  {
2567  if (!strstr(buffer, "foo") &&
2568  strstr(buffer, "cookie=biscuit"))
2569  send(c, okmsg, sizeof okmsg-1, 0);
2570  else
2571  send(c, notokmsg, sizeof notokmsg-1, 0);
2572  }
2573  if (strstr(buffer, "/test_host_override"))
2574  {
2575  if (strstr(buffer, host_header_override))
2576  send(c, okmsg, sizeof okmsg-1, 0);
2577  else
2578  send(c, notokmsg, sizeof notokmsg-1, 0);
2579  }
2580  if (strstr(buffer, "/async_read"))
2581  {
2582  const char *page1_mid = page1 + (sizeof page1 - 1)/2;
2583  const char *page1_end = page1 + sizeof page1 - 1;
2584  send(c, okmsg, sizeof okmsg-1, 0);
2585  send(c, page1, page1_mid - page1, 0);
2587  send(c, page1_mid, page1_end - page1_mid, 0);
2588  }
2589  if (strstr(buffer, "/socket"))
2590  {
2591  server_socket = c;
2594  }
2595  if (strstr(buffer, "/echo_request"))
2596  {
2597  send(c, okmsg, sizeof(okmsg)-1, 0);
2598  send(c, buffer, strlen(buffer), 0);
2599  }
2600  if (strstr(buffer, "GET /test_remove_dot_segments"))
2601  {
2602  send(c, okmsg, sizeof(okmsg)-1, 0);
2603  }
2604 
2605  if (strstr(buffer, "HEAD /test_large_content"))
2606  {
2607  char msg[sizeof(largemsg) + 16];
2609  send(c, msg, strlen(msg), 0);
2610  }
2611  if (strstr(buffer, "HEAD /test_auth_host1"))
2612  {
2613  if (strstr(buffer, "Authorization: Basic dGVzdDE6cGFzcw=="))
2614  send(c, okmsg, sizeof okmsg-1, 0);
2615  else
2616  send(c, noauthmsg, sizeof noauthmsg-1, 0);
2617  }
2618  if (strstr(buffer, "HEAD /test_auth_host2"))
2619  {
2620  if (strstr(buffer, "Authorization: Basic dGVzdDE6cGFzczI="))
2621  send(c, okmsg, sizeof okmsg-1, 0);
2622  else
2623  send(c, noauthmsg, sizeof noauthmsg-1, 0);
2624  }
2625  shutdown(c, 2);
2626  closesocket(c);
2627  c = -1;
2628  } while (!last_request);
2629 
2630  closesocket(s);
2632 
2633  return 0;
2634 }
2635 
2636 static void test_basic_request(int port, const char *verb, const char *url)
2637 {
2638  test_request_t req;
2639  DWORD r, count, error;
2640  char buffer[0x100];
2641 
2642  trace("basic request %s %s\n", verb, url);
2643 
2644  open_simple_request(&req, "localhost", port, verb, url);
2645 
2646  SetLastError(0xdeadbeef);
2647  r = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
2648  error = GetLastError();
2649  ok(error == ERROR_SUCCESS || broken(error != ERROR_SUCCESS), "expected ERROR_SUCCESS, got %u\n", error);
2650  ok(r, "HttpSendRequest failed: %u\n", GetLastError());
2651 
2652  count = 0;
2653  memset(buffer, 0, sizeof buffer);
2654  SetLastError(0xdeadbeef);
2655  r = InternetReadFile(req.request, buffer, sizeof buffer, &count);
2656  ok(r, "InternetReadFile failed %u\n", GetLastError());
2657  ok(count == sizeof page1 - 1, "count was wrong\n");
2658  ok(!memcmp(buffer, page1, sizeof page1), "http data wrong, got: %s\n", buffer);
2659 
2660  close_request(&req);
2661 }
2662 
2663 static void test_proxy_indirect(int port)
2664 {
2665  test_request_t req;
2666  DWORD r, sz;
2667  char buffer[0x40];
2668 
2669  open_simple_request(&req, "localhost", port, NULL, "/test2");
2670 
2671  r = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
2672  ok(r, "HttpSendRequest failed %u\n", GetLastError());
2673 
2674  sz = sizeof buffer;
2676  ok(r || GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo failed: %d\n", GetLastError());
2677  if (!r)
2678  {
2679  skip("missing proxy header, not testing remaining proxy headers\n");
2680  goto out;
2681  }
2682  ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
2683 
2684  test_status_code(req.request, 407);
2685  test_request_flags(req.request, 0);
2686 
2687  sz = sizeof buffer;
2689  ok(r, "HttpQueryInfo failed\n");
2690  ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
2691 
2692  sz = sizeof buffer;
2694  ok(r, "HttpQueryInfo failed\n");
2695  ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
2696 
2697  sz = sizeof buffer;
2699  ok(r, "HttpQueryInfo failed\n");
2700  ok(!strcmp(buffer, "winetest"), "http server wrong\n");
2701 
2702  sz = sizeof buffer;
2704  ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
2705  ok(r == FALSE, "HttpQueryInfo failed\n");
2706 
2707 out:
2708  close_request(&req);
2709 }
2710 
2711 static void test_proxy_direct(int port)
2712 {
2713  HINTERNET hi, hc, hr;
2714  DWORD r, sz, error;
2715  char buffer[0x40], *url;
2716  WCHAR bufferW[0x40];
2717  static const char url_fmt[] = "http://test.winehq.org:%u/test2";
2718  static CHAR username[] = "mike",
2719  password[] = "1101",
2720  useragent[] = "winetest";
2721  static const WCHAR usernameW[] = {'m','i','k','e',0},
2722  passwordW[] = {'1','1','0','1',0},
2723  useragentW[] = {'w','i','n','e','t','e','s','t',0};
2724 
2725  /* specify proxy type without the proxy and bypass */
2726  SetLastError(0xdeadbeef);
2728  error = GetLastError();
2730  broken(error == ERROR_SUCCESS) /* WinXPProSP2 */, "got %u\n", error);
2731  ok(hi == NULL || broken(!!hi) /* WinXPProSP2 */, "open should have failed\n");
2732 
2733  sprintf(buffer, "localhost:%d\n", port);
2735  ok(hi != NULL, "open failed\n");
2736 
2737  /* try connect without authorization */
2738  hc = InternetConnectA(hi, "test.winehq.org", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2739  ok(hc != NULL, "connect failed\n");
2740 
2741  hr = HttpOpenRequestA(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
2742  ok(hr != NULL, "HttpOpenRequest failed\n");
2743 
2744  sz = 0;
2745  SetLastError(0xdeadbeef);
2748  ok(!r, "unexpected success\n");
2749  ok(sz == 1, "got %u\n", sz);
2750 
2751  sz = 0;
2752  SetLastError(0xdeadbeef);
2755  ok(!r, "unexpected success\n");
2756  ok(sz == 1, "got %u\n", sz);
2757 
2758  sz = sizeof(buffer);
2759  SetLastError(0xdeadbeef);
2761  ok(r, "unexpected failure %u\n", GetLastError());
2762  ok(!sz, "got %u\n", sz);
2763 
2764  sz = sizeof(buffer);
2765  SetLastError(0xdeadbeef);
2767  ok(r, "unexpected failure %u\n", GetLastError());
2768  ok(!sz, "got %u\n", sz);
2769 
2770  sz = 0;
2771  SetLastError(0xdeadbeef);
2774  ok(!r, "unexpected success\n");
2775  ok(sz == 1, "got %u\n", sz);
2776 
2777  sz = 0;
2778  SetLastError(0xdeadbeef);
2781  ok(!r, "unexpected success\n");
2782  ok(sz == 1, "got %u\n", sz);
2783 
2784  sz = sizeof(buffer);
2785  SetLastError(0xdeadbeef);
2787  ok(r, "unexpected failure %u\n", GetLastError());
2788  ok(!sz, "got %u\n", sz);
2789 
2790  sz = sizeof(buffer);
2791  SetLastError(0xdeadbeef);
2793  ok(r, "unexpected failure %u\n", GetLastError());
2794  ok(!sz, "got %u\n", sz);
2795 
2796  sz = 0;
2797  SetLastError(0xdeadbeef);
2800  ok(!r, "unexpected success\n");
2801  ok(sz == 34, "got %u\n", sz);
2802 
2803  sz = sizeof(buffer);
2804  SetLastError(0xdeadbeef);
2806  ok(r, "unexpected failure %u\n", GetLastError());
2807  ok(sz == 33, "got %u\n", sz);
2808 
2809  r = HttpSendRequestW(hr, NULL, 0, NULL, 0);
2810  ok(r || broken(!r), "HttpSendRequest failed %u\n", GetLastError());
2811  if (!r)
2812  {
2813  win_skip("skipping proxy tests on broken wininet\n");
2814  goto done;
2815  }
2816 
2817  test_status_code(hr, 407);
2818 
2819  /* set the user + password then try again */
2821  ok(!r, "unexpected success\n");
2822 
2824  ok(r, "failed to set user\n");
2825 
2827  ok(r, "failed to set user\n");
2828 
2829  buffer[0] = 0;
2830  sz = 3;
2831  SetLastError(0xdeadbeef);
2833  ok(!r, "unexpected failure %u\n", GetLastError());
2834  ok(!buffer[0], "got %s\n", buffer);
2835  ok(sz == strlen(username) + 1, "got %u\n", sz);
2836 
2837  buffer[0] = 0;
2838  sz = 0;
2839  SetLastError(0xdeadbeef);
2842  ok(!r, "unexpected success\n");
2843  ok(sz == strlen(username) + 1, "got %u\n", sz);
2844 
2845  bufferW[0] = 0;
2846  sz = 0;
2847  SetLastError(0xdeadbeef);
2850  ok(!r, "unexpected success\n");
2851  ok(sz == (lstrlenW(usernameW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2852 
2853  buffer[0] = 0;
2854  sz = sizeof(buffer);
2856  ok(r, "failed to get username\n");
2857  ok(!strcmp(buffer, username), "got %s\n", buffer);
2858  ok(sz == strlen(username), "got %u\n", sz);
2859 
2860  buffer[0] = 0;
2861  sz = sizeof(bufferW);
2863  ok(r, "failed to get username\n");
2864  ok(!lstrcmpW(bufferW, usernameW), "wrong username\n");
2865  ok(sz == lstrlenW(usernameW), "got %u\n", sz);
2866 
2868  ok(r, "failed to set user\n");
2869 
2870  buffer[0] = 0;
2871  sz = sizeof(buffer);
2873  ok(r, "failed to get username\n");
2874  ok(!strcmp(buffer, username), "got %s\n", buffer);
2875  ok(sz == strlen(username), "got %u\n", sz);
2876 
2877  r = InternetSetOptionA(hi, INTERNET_OPTION_USER_AGENT, useragent, 1);
2878  ok(r, "failed to set useragent\n");
2879 
2880  buffer[0] = 0;
2881  sz = 0;
2882  SetLastError(0xdeadbeef);
2885  ok(!r, "unexpected success\n");
2886  ok(sz == strlen(useragent) + 1, "got %u\n", sz);
2887 
2888  buffer[0] = 0;
2889  sz = sizeof(buffer);
2891  ok(r, "failed to get user agent\n");
2892  ok(!strcmp(buffer, useragent), "got %s\n", buffer);
2893  ok(sz == strlen(useragent), "got %u\n", sz);
2894 
2895  bufferW[0] = 0;
2896  sz = 0;
2897  SetLastError(0xdeadbeef);
2898  r = InternetQueryOptionW(hi, INTERNET_OPTION_USER_AGENT, bufferW, &sz);
2900  ok(!r, "unexpected success\n");
2901  ok(sz == (lstrlenW(useragentW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2902 
2903  bufferW[0] = 0;
2904  sz = sizeof(bufferW);
2905  r = InternetQueryOptionW(hi, INTERNET_OPTION_USER_AGENT, bufferW, &sz);
2906  ok(r, "failed to get user agent\n");
2907  ok(!lstrcmpW(bufferW, useragentW), "wrong user agent\n");
2908  ok(sz == lstrlenW(useragentW), "got %u\n", sz);
2909 
2911  ok(r, "failed to set user\n");
2912 
2913  buffer[0] = 0;
2914  sz = 0;
2915  SetLastError(0xdeadbeef);
2918  ok(!r, "unexpected success\n");
2919  ok(sz == strlen(username) + 1, "got %u\n", sz);
2920 
2921  buffer[0] = 0;
2922  sz = sizeof(buffer);
2924  ok(r, "failed to get user\n");
2925  ok(!strcmp(buffer, username), "got %s\n", buffer);
2926  ok(sz == strlen(username), "got %u\n", sz);
2927 
2928  bufferW[0] = 0;
2929  sz = 0;
2930  SetLastError(0xdeadbeef);
2933  ok(!r, "unexpected success\n");
2934  ok(sz == (lstrlenW(usernameW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2935 
2936  bufferW[0] = 0;
2937  sz = sizeof(bufferW);
2939  ok(r, "failed to get user\n");
2940  ok(!lstrcmpW(bufferW, usernameW), "wrong user\n");
2941  ok(sz == lstrlenW(usernameW), "got %u\n", sz);
2942 
2944  ok(r, "failed to set password\n");
2945 
2946  buffer[0] = 0;
2947  sz = 0;
2948  SetLastError(0xdeadbeef);
2951  ok(!r, "unexpected success\n");
2952  ok(sz == strlen(password) + 1, "got %u\n", sz);
2953 
2954  buffer[0] = 0;
2955  sz = sizeof(buffer);
2957  ok(r, "failed to get password\n");
2958  ok(!strcmp(buffer, password), "got %s\n", buffer);
2959  ok(sz == strlen(password), "got %u\n", sz);
2960 
2961  bufferW[0] = 0;
2962  sz = 0;
2963  SetLastError(0xdeadbeef);
2966  ok(!r, "unexpected success\n");
2967  ok(sz == (lstrlenW(passwordW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2968 
2969  bufferW[0] = 0;
2970  sz = sizeof(bufferW);
2972  ok(r, "failed to get password\n");
2973  ok(!lstrcmpW(bufferW, passwordW), "wrong password\n");
2974  ok(sz == lstrlenW(passwordW), "got %u\n", sz);
2975 
2976  url = HeapAlloc(GetProcessHeap(), 0, strlen(url_fmt) + 11);
2977  sprintf(url, url_fmt, port);
2978  buffer[0] = 0;
2979  sz = 0;
2980  SetLastError(0xdeadbeef);
2983  ok(!r, "unexpected success\n");
2984  ok(sz == strlen(url) + 1, "got %u\n", sz);
2985 
2986  buffer[0] = 0;
2987  sz = sizeof(buffer);
2989  ok(r, "failed to get url\n");
2990  ok(!strcmp(buffer, url), "got %s\n", buffer);
2991  ok(sz == strlen(url), "got %u\n", sz);
2992 
2993  bufferW[0] = 0;
2994  sz = 0;
2995  SetLastError(0xdeadbeef);
2996  r = InternetQueryOptionW(hr, INTERNET_OPTION_URL, bufferW, &sz);
2998  ok(!r, "unexpected success\n");
2999  ok(sz == (strlen(url) + 1) * sizeof(WCHAR), "got %u\n", sz);
3000 
3001  bufferW[0] = 0;
3002  sz = sizeof(bufferW);
3003  r = InternetQueryOptionW(hr, INTERNET_OPTION_URL, bufferW, &sz);
3004  ok(r, "failed to get url\n");
3005  ok(!strcmp_wa(bufferW, url), "wrong url\n");
3006  ok(sz == strlen(url), "got %u\n", sz);
3007  HeapFree(GetProcessHeap(), 0, url);
3008 
3010  ok(r, "failed to set password\n");
3011 
3012  buffer[0] = 0;
3013  sz = 0;
3014  SetLastError(0xdeadbeef);
3017  ok(!r, "unexpected success\n");
3018  ok(sz == strlen(password) + 1, "got %u\n", sz);
3019 
3020  buffer[0] = 0;
3021  sz = sizeof(buffer);
3023  ok(r, "failed to get password\n");
3024  ok(!strcmp(buffer, password), "got %s\n", buffer);
3025  ok(sz == strlen(password), "got %u\n", sz);
3026 
3027  bufferW[0] = 0;
3028  sz = 0;
3029  SetLastError(0xdeadbeef);
3032  ok(!r, "unexpected success\n");
3033  ok(sz == (lstrlenW(passwordW) + 1) * sizeof(WCHAR), "got %u\n", sz);
3034 
3035  bufferW[0] = 0;
3036  sz = sizeof(bufferW);
3038  ok(r, "failed to get password\n");
3039  ok(!lstrcmpW(bufferW, passwordW), "wrong password\n");
3040  ok(sz == lstrlenW(passwordW), "got %u\n", sz);
3041 
3042  r = HttpSendRequestW(hr, NULL, 0, NULL, 0);
3043  if (!r)
3044  {
3045  win_skip("skipping proxy tests on broken wininet\n");
3046  goto done;
3047  }
3048  ok(r, "HttpSendRequest failed %u\n", GetLastError());
3049  sz = sizeof buffer;
3051  ok(r, "HttpQueryInfo failed\n");
3052  ok(!strcmp(buffer, "200"), "proxy code wrong\n");
3053 
3055  InternetCloseHandle(hc);
3056  InternetCloseHandle(hi);
3057 
3058  sprintf(buffer, "localhost:%d\n", port);
3059  hi = InternetOpenA("winetest", INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
3060  ok(hi != NULL, "InternetOpen failed\n");
3061 
3062  hc = InternetConnectA(hi, "test.winehq.org", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3063  ok(hc != NULL, "InternetConnect failed\n");
3064 
3065  hr = HttpOpenRequestA(hc, "POST", "/test2", NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
3066  ok(hr != NULL, "HttpOpenRequest failed\n");
3067 
3068  r = HttpSendRequestA(hr, NULL, 0, (char *)"data", sizeof("data"));
3069  ok(r, "HttpSendRequest failed %u\n", GetLastError());
3070 
3071  test_status_code(hr, 407);
3072 
3073 done:
3075  InternetCloseHandle(hc);
3076  InternetCloseHandle(hi);
3077 }
3078 
3080 {
3081  static const char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
3082  static const char connection[] = "Connection: Close";
3083  static const char *types[2] = { "*", NULL };
3084  char data[32];
3086  DWORD size, status, data_len;
3087  BOOL ret;
3088 
3090  ok(session != NULL, "InternetOpen failed\n");
3091 
3093  ok(connect != NULL, "InternetConnect failed\n");
3094 
3096  ok(request != NULL, "HttpOpenRequest failed\n");
3097 
3099  ok(ret, "HttpAddRequestHeaders failed\n");
3100 
3101  ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
3102  ok(ret, "HttpSendRequest failed\n");
3103 
3104  test_status_code(request, 200);
3106 
3108 
3110  ok(request != NULL, "HttpOpenRequest failed\n");
3111 
3112  ret = HttpSendRequestA(request, connection, ~0u, NULL, 0);
3113  ok(ret, "HttpSendRequest failed\n");
3114 
3115  status = 0;
3116  size = sizeof(status);
3118  ok(ret, "HttpQueryInfo failed\n");
3119  ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
3120 
3123 
3125  ok(connect != NULL, "InternetConnect failed\n");
3126 
3128  ok(request != NULL, "HttpOpenRequest failed\n");
3129 
3130  ret = HttpAddRequestHeadersA(request, "Content-Length: 100\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
3131  ok(ret, "HttpAddRequestHeaders failed\n");
3132 
3133  ret = HttpSendRequestA(request, connection, ~0u, NULL, 0);
3134  ok(ret, "HttpSendRequest failed\n");
3135 
3136  status = 0;
3137  size = sizeof(status);
3139  ok(ret, "HttpQueryInfo failed\n");
3140  ok(status == 200, "got status %u, expected 200\n", status);
3141 
3144 
3146  ok(connect != NULL, "InternetConnect failed\n");
3147 
3149  ok(request != NULL, "HttpOpenRequest failed\n");
3150 
3151  ret = HttpAddRequestHeadersA(request, "Content-Length: 100\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
3152  ok(ret, "HttpAddRequestHeaders failed\n");
3153 
3154  data_len = sizeof(data);
3155  memset(data, 'a', sizeof(data));
3156  ret = HttpSendRequestA(request, NULL, 0, data, data_len);
3157  ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
3158 
3159  status = 0;
3160  size = sizeof(status);
3162  ok(ret, "HttpQueryInfo failed\n");
3163  ok(status == 200, "got status %u, expected 200\n", status);
3164 
3168 }
3169 
3171 {
3172  HINTERNET ses, con, req;
3173  BOOL ret;
3174 
3175  ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3176  ok(ses != NULL, "InternetOpen failed\n");
3177 
3178  con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3179  ok(con != NULL, "InternetConnect failed\n");
3180 
3181  req = HttpOpenRequestA(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3182  ok(req != NULL, "HttpOpenRequest failed\n");
3183 
3184  ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3185  ok(ret, "HttpSendRequest failed\n");
3186 
3187  test_status_code(req, 200);
3188 
3189  InternetCloseHandle(req);
3190 
3191  req = HttpOpenRequestA(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
3192  ok(req != NULL, "HttpOpenRequest failed\n");
3193 
3194  ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3195  ok(ret, "HttpSendRequest failed\n");
3196 
3197  test_status_code(req, 200);
3198 
3199  InternetCloseHandle(req);
3200 
3201  req = HttpOpenRequestA(con, NULL, "/test9", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
3202  ok(req != NULL, "HttpOpenRequest failed\n");
3203 
3204  ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3205  ok(ret, "HttpSendRequest failed\n");
3206 
3207  test_status_code(req, 200);
3208 
3209  InternetCloseHandle(req);
3210 
3211  req = HttpOpenRequestA(con, "POST", "/testA", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
3212  ok(req != NULL, "HttpOpenRequest failed\n");
3213 
3214  ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3215  ok(ret, "HttpSendRequest failed\n");
3216 
3217  test_status_code(req, 200);
3218 
3219  InternetCloseHandle(req);
3220  InternetCloseHandle(con);
3221  InternetCloseHandle(ses);
3222 }
3223 
3224 static void test_header_override(int port)
3225 {
3226  char buffer[128], host_header_override[30], full_url[128];
3227  HINTERNET ses, con, req;
3228  DWORD size, count, err;
3229  BOOL ret;
3230 
3231  sprintf(host_header_override, "Host: test.local:%d\r\n", port);
3232  sprintf(full_url, "http://localhost:%d/test_host_override", port);
3233 
3234  ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3235  ok(ses != NULL, "InternetOpen failed\n");
3236 
3237  con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3238  ok(con != NULL, "InternetConnect failed\n");
3239 
3240  req = HttpOpenRequestA(con, NULL, "/test_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3241  ok(req != NULL, "HttpOpenRequest failed\n");
3242 
3243  size = sizeof(buffer) - 1;
3244  count = 0;
3245  memset(buffer, 0, sizeof(buffer));
3247  err = GetLastError();
3248  ok(!ret, "HttpQueryInfo succeeded\n");
3249  ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "Expected error ERROR_HTTP_HEADER_NOT_FOUND, got %d\n", err);
3250 
3251  test_request_url(req, full_url);
3252 
3253  ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_COALESCE);
3254  ok(ret, "HttpAddRequestHeaders failed\n");
3255 
3256  size = sizeof(buffer) - 1;
3257  count = 0;
3258  memset(buffer, 0, sizeof(buffer));
3260  ok(ret, "HttpQueryInfo failed\n");
3261 
3262  test_request_url(req, full_url);
3263 
3264  ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3265  ok(ret, "HttpSendRequest failed\n");
3266 
3267  test_status_code(req, 200);
3268 
3269  InternetCloseHandle(req);
3270  req = HttpOpenRequestA(con, NULL, "/test_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3271  ok(req != NULL, "HttpOpenRequest failed\n");
3272 
3273  ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_COALESCE);
3274  ok(ret, "HttpAddRequestHeaders failed\n");
3275 
3276  ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_COALESCE);
3277  ok(ret, "HttpAddRequestHeaders failed\n");
3278 
3279  ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3280  ok(ret, "HttpSendRequest failed\n");
3281 
3282  test_status_code(req, 400);
3283 
3284  InternetCloseHandle(req);
3285  req = HttpOpenRequestA(con, NULL, "/test_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3286  ok(req != NULL, "HttpOpenRequest failed\n");
3287 
3288  ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
3289&#