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