httpapi.c 67.7 KB
Newer Older
1
/*
2
 * HTTP server API tests
3 4
 *
 * Copyright 2017 Nikolay Sivov for CodeWeavers
5
 * Copyright 2019 Zebediah Figura
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdarg.h>
23 24
#include <stdio.h>
#include <wchar.h>
25 26 27 28 29 30 31 32 33 34 35

#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winbase.h"
#include "winnt.h"
#include "winternl.h"
#include "http.h"

#include "wine/test.h"

36 37 38 39 40
static ULONG (WINAPI *pHttpAddUrlToUrlGroup)(HTTP_URL_GROUP_ID id, const WCHAR *url, HTTP_URL_CONTEXT context, ULONG reserved);
static ULONG (WINAPI *pHttpCreateServerSession)(HTTPAPI_VERSION version, HTTP_SERVER_SESSION_ID *session_id, ULONG reserved);
static ULONG (WINAPI *pHttpCreateRequestQueue)(HTTPAPI_VERSION version, const WCHAR *name, SECURITY_ATTRIBUTES *sa, ULONG flags, HANDLE *handle);
static ULONG (WINAPI *pHttpCreateUrlGroup)(HTTP_SERVER_SESSION_ID session_id, HTTP_URL_GROUP_ID *group_id, ULONG reserved);
static ULONG (WINAPI *pHttpCloseRequestQueue)(HANDLE queue);
41 42
static ULONG (WINAPI *pHttpCloseServerSession)(HTTP_SERVER_SESSION_ID session_id);
static ULONG (WINAPI *pHttpCloseUrlGroup)(HTTP_URL_GROUP_ID group_id);
43 44
static ULONG (WINAPI *pHttpRemoveUrlFromUrlGroup)(HTTP_URL_GROUP_ID id, const WCHAR *url, ULONG flags);
static ULONG (WINAPI *pHttpSetUrlGroupProperty)(HTTP_URL_GROUP_ID id, HTTP_SERVER_PROPERTY property, void *value, ULONG length);
45 46 47 48 49 50

static void init(void)
{
    HMODULE mod = GetModuleHandleA("httpapi.dll");

#define X(f) p##f = (void *)GetProcAddress(mod, #f)
51 52
    X(HttpAddUrlToUrlGroup);
    X(HttpCreateRequestQueue);
53 54
    X(HttpCreateServerSession);
    X(HttpCreateUrlGroup);
55 56
    X(HttpCloseRequestQueue);
    X(HttpCloseServerSession);
57
    X(HttpCloseUrlGroup);
58 59
    X(HttpRemoveUrlFromUrlGroup);
    X(HttpSetUrlGroupProperty);
60 61 62
#undef X
}

63 64
static const char simple_req[] =
    "GET /foobar HTTP/1.1\r\n"
65
    "Host: localhost:%u\r\n"
66 67 68 69
    "Connection: keep-alive\r\n"
    "User-Agent: WINE\r\n"
    "\r\n";

70
static SOCKET create_client_socket(unsigned short port)
71 72 73 74
{
    struct sockaddr_in sockaddr =
    {
        .sin_family = AF_INET,
75
        .sin_port = htons(port),
76 77
        .sin_addr.S_un.S_addr = inet_addr("127.0.0.1"),
    };
78
    SOCKET s = socket(AF_INET, SOCK_STREAM, 0), ret;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    ret = connect(s, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
    ok(!ret, "Failed to connect socket, error %u.\n", GetLastError());
    return s;
}

/* Helper function for when we don't care about the response received. */
static void send_response_v1(HANDLE queue, HTTP_REQUEST_ID id, int s)
{
    HTTP_RESPONSE_V1 response = {};
    char response_buffer[2048];
    int ret;

    response.StatusCode = 418;
    response.pReason = "I'm a teapot";
    response.ReasonLength = 12;
    ret = HttpSendHttpResponse(queue, id, 0, (HTTP_RESPONSE *)&response, NULL, NULL, NULL, 0, NULL, NULL);
    ok(!ret, "Got error %u.\n", ret);
    ret = recv(s, response_buffer, sizeof(response_buffer), 0);
    ok(ret > 0, "recv() failed.\n");
}

100 101 102 103 104 105 106 107 108
static unsigned short add_url_v1(HANDLE queue)
{
    unsigned short port;
    WCHAR url[50];
    ULONG ret;

    for (port = 50000; port < 51000; ++port)
    {
        swprintf(url, ARRAY_SIZE(url), L"http://localhost:%u/", port);
109
        if (!(ret = HttpAddUrl(queue, url, NULL)))
110
            return port;
111
        ok(ret == ERROR_SHARING_VIOLATION, "Failed to add %s, error %u.\n", debugstr_w(url), ret);
112 113 114 115 116 117 118 119 120 121 122 123 124 125
    }
    ok(0, "Failed to add url %s, error %u.\n", debugstr_w(url), ret);
    return 0;
}

static ULONG add_url_v2(HTTP_URL_GROUP_ID group)
{
    unsigned short port;
    WCHAR url[50];
    ULONG ret;

    for (port = 50010; port < 51000; ++port)
    {
        swprintf(url, ARRAY_SIZE(url), L"http://localhost:%u/", port);
126
        if (!(ret = pHttpAddUrlToUrlGroup(group, url, 0xdeadbeef, 0)))
127
            return port;
128
        ok(ret == ERROR_SHARING_VIOLATION, "Failed to add %s, error %u.\n", debugstr_w(url), ret);
129 130 131 132 133 134 135 136 137 138 139 140 141
    }
    ok(0, "Failed to add url %s, error %u.\n", debugstr_w(url), ret);
    return 0;
}

static ULONG remove_url_v1(HANDLE queue, unsigned short port)
{
    WCHAR url[50];
    swprintf(url, ARRAY_SIZE(url), L"http://localhost:%u/", port);
    return HttpRemoveUrl(queue, url);
}

static ULONG remove_url_v2(HTTP_URL_GROUP_ID group, unsigned short port)
142
{
143 144 145 146
    WCHAR url[50];
    swprintf(url, ARRAY_SIZE(url), L"http://localhost:%u/", port);
    return pHttpRemoveUrlFromUrlGroup(group, url, 0);
}
147

148 149
static void test_v1_server(void)
{
150 151 152 153 154
    char DECLSPEC_ALIGN(8) req_buffer[2048], response_buffer[2048];
    HTTP_REQUEST_V1 *req = (HTTP_REQUEST_V1 *)req_buffer;
    struct sockaddr_in sockaddr, *sin;
    HTTP_RESPONSE_V1 response = {};
    HANDLE queue, queue2;
155 156
    unsigned short port;
    char req_text[200];
157 158 159
    unsigned int i;
    OVERLAPPED ovl;
    DWORD ret_size;
160
    WCHAR url[50];
161
    ULONG ret;
162 163 164 165 166
    SOCKET s;
    int len;

    ovl.hEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
    memset(req_buffer, 0xcc, sizeof(req_buffer));
167 168

    ret = HttpCreateHttpHandle(NULL, 0);
169
    ok(ret == ERROR_INVALID_PARAMETER, "Unexpected error %u.\n", ret);
170 171

    /* Non-zero reserved parameter is accepted on XP/2k3. */
172 173
    queue = NULL;
    ret = HttpCreateHttpHandle(&queue, 0);
174
    ok(!ret, "Unexpected ret value %u.\n", ret);
175 176 177 178
    ok(!!queue, "Unexpected handle value %p.\n", queue);

    queue2 = NULL;
    ret = HttpCreateHttpHandle(&queue2, 0);
179
    ok(!ret, "Unexpected ret value %u.\n", ret);
180 181 182 183
    ok(queue2 && queue2 != queue, "Unexpected handle %p.\n", queue2);
    ret = CloseHandle(queue2);
    ok(ret, "Failed to close queue handle, error %u.\n", GetLastError());

184 185 186
    ret_size = 0xdeadbeef;
    ret = HttpReceiveHttpRequest(NULL, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), &ret_size, NULL);
    ok(ret == ERROR_INVALID_HANDLE, "Got error %u.\n", ret);
187 188 189 190 191 192 193 194 195 196 197 198
    ret = HttpReceiveHttpRequest(NULL, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), NULL, &ovl);
    ok(ret == ERROR_INVALID_HANDLE, "Got error %u.\n", ret);
    ret = HttpReceiveHttpRequest(queue, 0xdeadbeef, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), NULL, &ovl);
    ok(ret == ERROR_CONNECTION_INVALID, "Got error %u.\n", ret);
    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), NULL, &ovl);
    ok(ret == ERROR_IO_PENDING, "Got error %u.\n", ret);

    SetLastError(0xdeadbeef);
    ret = GetOverlappedResult(queue, &ovl, &ret_size, FALSE);
    ok(!ret, "Expected failure.\n");
    ok(GetLastError() == ERROR_IO_INCOMPLETE, "Got error %u.\n", GetLastError());

199
    ret = HttpAddUrl(NULL, L"http://localhost:50000/", NULL);
200
    ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_INVALID_PARAMETER /* < Vista */, "Got error %u.\n", ret);
201
    ret = HttpAddUrl(queue, L"http://localhost:50000", NULL);
202
    ok(ret == ERROR_INVALID_PARAMETER, "Got error %u.\n", ret);
203
    ret = HttpAddUrl(queue, L"localhost:50000", NULL);
204
    ok(ret == ERROR_INVALID_PARAMETER, "Got error %u.\n", ret);
205
    ret = HttpAddUrl(queue, L"localhost:50000/", NULL);
206
    ok(ret == ERROR_INVALID_PARAMETER, "Got error %u.\n", ret);
207
    ret = HttpAddUrl(queue, L"http://localhost/", NULL);
208
    ok(ret == ERROR_INVALID_PARAMETER, "Got error %u.\n", ret);
209
    ret = HttpAddUrl(queue, L"http://localhost:/", NULL);
210
    ok(ret == ERROR_INVALID_PARAMETER, "Got error %u.\n", ret);
211
    ret = HttpAddUrl(queue, L"http://localhost:0/", NULL);
212
    ok(ret == ERROR_INVALID_PARAMETER, "Got error %u.\n", ret);
213 214 215
    port = add_url_v1(queue);
    swprintf(url, ARRAY_SIZE(url), L"http://localhost:%u/", port);
    ret = HttpAddUrl(queue, url, NULL);
216 217
    ok(ret == ERROR_ALREADY_EXISTS, "Got error %u.\n", ret);

218
    s = create_client_socket(port);
219 220 221 222 223 224 225 226 227
    len = sizeof(sockaddr);
    ret = getsockname(s, (struct sockaddr *)&sockaddr, &len);
    ok(ret == 0, "getsockname() failed, error %u.\n", WSAGetLastError());

    SetLastError(0xdeadbeef);
    ret = GetOverlappedResult(queue, &ovl, &ret_size, FALSE);
    ok(!ret, "Expected failure.\n");
    ok(GetLastError() == ERROR_IO_INCOMPLETE, "Got error %u.\n", GetLastError());

228 229 230
    sprintf(req_text, simple_req, port);
    ret = send(s, req_text, strlen(req_text), 0);
    ok(ret == strlen(req_text), "send() returned %d.\n", ret);
231 232 233 234 235

    ret = GetOverlappedResult(queue, &ovl, &ret_size, TRUE);
    ok(ret, "Got error %u.\n", GetLastError());
    ok(ret_size > sizeof(*req), "Got size %u.\n", ret_size);

236 237 238 239 240
    /* Various versions of Windows (observed on 64-bit Windows 8 and Windows 10
     * version 1507, but probably affecting others) suffer from a bug where the
     * kernel will report success before completely filling the buffer or
     * reporting the correct buffer size. Wait for a short interval to work
     * around this. */
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    Sleep(100);

    ok(!req->Flags, "Got flags %#x.\n", req->Flags);
    ok(req->ConnectionId, "Expected nonzero connection ID.\n");
    ok(req->RequestId, "Expected nonzero connection ID.\n");
    ok(!req->UrlContext, "Got URL context %s.\n", wine_dbgstr_longlong(req->UrlContext));
    ok(req->Version.MajorVersion == 1, "Got major version %u.\n", req->Version.MajorVersion);
    ok(req->Version.MinorVersion == 1, "Got major version %u.\n", req->Version.MinorVersion);
    ok(req->Verb == HttpVerbGET, "Got verb %u.\n", req->Verb);
    ok(!req->UnknownVerbLength, "Got unknown verb length %u.\n", req->UnknownVerbLength);
    ok(req->RawUrlLength == 7, "Got raw URL length %u.\n", req->RawUrlLength);
    ok(!req->pUnknownVerb, "Got unknown verb %s.\n", req->pUnknownVerb);
    ok(!strcmp(req->pRawUrl, "/foobar"), "Got raw URL %s.\n", req->pRawUrl);
    ok(req->CookedUrl.FullUrlLength == 58, "Got full URL length %u.\n", req->CookedUrl.FullUrlLength);
    ok(req->CookedUrl.HostLength == 30, "Got host length %u.\n", req->CookedUrl.HostLength);
    ok(req->CookedUrl.AbsPathLength == 14, "Got absolute path length %u.\n", req->CookedUrl.AbsPathLength);
    ok(!req->CookedUrl.QueryStringLength, "Got query string length %u.\n", req->CookedUrl.QueryStringLength);
258 259
    swprintf(url, ARRAY_SIZE(url), L"http://localhost:%u/foobar", port);
    ok(!wcscmp(req->CookedUrl.pFullUrl, url), "Got full URL %s.\n", wine_dbgstr_w(req->CookedUrl.pFullUrl));
260 261 262 263 264 265 266
    ok(req->CookedUrl.pHost == req->CookedUrl.pFullUrl + 7, "Got host %s.\n", wine_dbgstr_w(req->CookedUrl.pHost));
    ok(req->CookedUrl.pAbsPath == req->CookedUrl.pFullUrl + 22,
            "Got absolute path %s.\n", wine_dbgstr_w(req->CookedUrl.pAbsPath));
    ok(!req->CookedUrl.pQueryString, "Got query string %s.\n", wine_dbgstr_w(req->CookedUrl.pQueryString));
    ok(!memcmp(req->Address.pRemoteAddress, &sockaddr, len), "Client addresses didn't match.\n");
    sin = (SOCKADDR_IN *)req->Address.pLocalAddress;
    ok(sin->sin_family == AF_INET, "Got family %u.\n", sin->sin_family);
267
    ok(ntohs(sin->sin_port) == port, "Got wrong port %u.\n", ntohs(sin->sin_port));
268 269 270 271 272 273 274 275 276 277 278 279 280 281
    ok(sin->sin_addr.S_un.S_addr == inet_addr("127.0.0.1"), "Got address %08x.\n", sin->sin_addr.S_un.S_addr);
    ok(!req->Headers.UnknownHeaderCount, "Got %u unknown headers.\n", req->Headers.UnknownHeaderCount);
    ok(!req->Headers.pUnknownHeaders, "Got unknown headers %p.\n", req->Headers.pUnknownHeaders);
    for (i = 0; i < ARRAY_SIZE(req->Headers.KnownHeaders); ++i)
    {
        if (i == HttpHeaderConnection)
        {
            ok(req->Headers.KnownHeaders[i].RawValueLength == 10, "Got length %u.\n",
                    req->Headers.KnownHeaders[i].RawValueLength);
            ok(!strcmp(req->Headers.KnownHeaders[i].pRawValue, "keep-alive"),
                    "Got connection '%s'.\n", req->Headers.KnownHeaders[i].pRawValue);
        }
        else if (i == HttpHeaderHost)
        {
282 283 284
            char expect[16];
            sprintf(expect, "localhost:%u", port);
            ok(req->Headers.KnownHeaders[i].RawValueLength == strlen(expect), "Got length %u.\n",
285
                    req->Headers.KnownHeaders[i].RawValueLength);
286
            ok(!strcmp(req->Headers.KnownHeaders[i].pRawValue, expect),
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
                    "Got connection '%s'.\n", req->Headers.KnownHeaders[i].pRawValue);
        }
        else if (i == HttpHeaderUserAgent)
        {
            ok(req->Headers.KnownHeaders[i].RawValueLength == 4, "Got length %u.\n",
                    req->Headers.KnownHeaders[i].RawValueLength);
            ok(!strcmp(req->Headers.KnownHeaders[i].pRawValue, "WINE"),
                    "Got connection '%s'.\n", req->Headers.KnownHeaders[i].pRawValue);
        }
        else
        {
            ok(!req->Headers.KnownHeaders[i].RawValueLength, "Header %#x: got length %u.\n",
                    i, req->Headers.KnownHeaders[i].RawValueLength);
            ok(!req->Headers.KnownHeaders[i].pRawValue, "Header %#x: got value '%s'.\n",
                    i, req->Headers.KnownHeaders[i].pRawValue);
        }
    }
304
    ok(req->BytesReceived == strlen(req_text), "Got %s bytes.\n", wine_dbgstr_longlong(req->BytesReceived));
305 306
    ok(!req->EntityChunkCount, "Got %u entity chunks.\n", req->EntityChunkCount);
    ok(!req->pEntityChunks, "Got entity chunks %p.\n", req->pEntityChunks);
307
    /* req->RawConnectionId is zero until Win10 2004. */
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    ok(!req->pSslInfo, "Got SSL info %p.\n", req->pSslInfo);

    response.StatusCode = 418;
    response.pReason = "I'm a teapot";
    response.ReasonLength = 12;
    response.Headers.KnownHeaders[HttpHeaderRetryAfter].pRawValue = "120";
    response.Headers.KnownHeaders[HttpHeaderRetryAfter].RawValueLength = 3;
    ret = HttpSendHttpResponse(queue, 0xdeadbeef, 0, (HTTP_RESPONSE *)&response, NULL, NULL, NULL, 0, &ovl, NULL);
    ok(ret == ERROR_CONNECTION_INVALID, "Got error %u.\n", ret);
    ret = HttpSendHttpResponse(queue, req->RequestId, 0, (HTTP_RESPONSE *)&response, NULL, NULL, NULL, 0, &ovl, NULL);
    ok(!ret, "Got error %u.\n", ret);
    ret = GetOverlappedResult(queue, &ovl, &ret_size, FALSE);
    ok(ret, "Got error %u.\n", GetLastError());

    ret = recv(s, response_buffer, sizeof(response_buffer), 0);
    ok(ret == ret_size, "Expected size %u, got %u.\n", ret_size, ret);

    if (winetest_debug > 1)
        trace("%.*s\n", ret, response_buffer);

    ok(!strncmp(response_buffer, "HTTP/1.1 418 I'm a teapot\r\n", 27), "Got incorrect status line.\n");
    ok(!!strstr(response_buffer, "\r\nRetry-After: 120\r\n"), "Missing or malformed Retry-After header.\n");
    ok(!!strstr(response_buffer, "\r\nDate:"), "Missing Date header.\n");

    ret = HttpReceiveHttpRequest(queue, req->RequestId, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), NULL, &ovl);
    ok(ret == ERROR_CONNECTION_INVALID, "Got error %u.\n", ret);

    /* HttpReceiveHttpRequest() may return synchronously, but this cannot be
     * reliably tested. Introducing a delay after send() and before
     * HttpReceiveHttpRequest() confirms this. */

339
    ret = remove_url_v1(NULL, port);
340
    ok(ret == ERROR_INVALID_PARAMETER, "Got error %u.\n", ret);
341
    ret = remove_url_v1(queue, port);
342
    ok(!ret, "Got error %u.\n", ret);
343
    ret = remove_url_v1(queue, port);
344 345
    ok(ret == ERROR_FILE_NOT_FOUND, "Got error %u.\n", ret);

346 347 348 349 350 351 352 353 354 355 356 357
    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), NULL, &ovl);
    ok(ret == ERROR_IO_PENDING, "Got error %u.\n", ret);

    ret = CancelIo(queue);
    ok(ret, "Failed to close queue handle, error %u.\n", GetLastError());

    ret_size = 0xdeadbeef;
    ret = GetOverlappedResult(queue, &ovl, &ret_size, FALSE);
    ok(!ret, "Expected failure.\n");
    ok(GetLastError() == ERROR_OPERATION_ABORTED, "Got error %u.\n", GetLastError());
    ok(!ret_size, "Got size %u.\n", ret_size);

358 359 360 361 362
    closesocket(s);
    CloseHandle(ovl.hEvent);
    ret = CloseHandle(queue);
    ok(ret, "Failed to close queue handle, error %u.\n", GetLastError());

363
    ret = HttpAddUrl(queue, L"http://localhost:50000/", NULL);
364
    ok(ret == ERROR_INVALID_HANDLE, "Got error %u.\n", ret);
365
}
366 367 368 369 370 371

static void test_v1_completion_port(void)
{
    char DECLSPEC_ALIGN(8) req_buffer[2048], response_buffer[2048];
    HTTP_REQUEST_V1 *req = (HTTP_REQUEST_V1 *)req_buffer;
    HTTP_RESPONSE_V1 response = {};
372
    unsigned short tcp_port;
373 374
    OVERLAPPED ovl, *povl;
    HANDLE queue, port;
375
    char req_text[200];
376 377 378 379 380 381 382 383
    DWORD ret_size;
    ULONG_PTR key;
    ULONG ret;
    SOCKET s;

    ovl.hEvent = CreateEventA(NULL, TRUE, FALSE, NULL);

    ret = HttpCreateHttpHandle(&queue, 0);
384
    ok(!ret, "Got error %u.\n", ret);
385 386 387 388 389 390 391 392 393 394 395

    port = CreateIoCompletionPort(queue, NULL, 123, 0);
    ok(!!port, "Failed to create completion port, error %u.\n", GetLastError());

    ret = GetQueuedCompletionStatus(port, &ret_size, &key, &povl, 0);
    ok(!ret, "Expected failure.\n");
    ok(GetLastError() == WAIT_TIMEOUT, "Got error %u.\n", GetLastError());

    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), NULL, &ovl);
    ok(ret == ERROR_IO_PENDING, "Got error %u.\n", ret);

396 397
    tcp_port = add_url_v1(queue);
    s = create_client_socket(tcp_port);
398 399 400 401 402

    ret = GetQueuedCompletionStatus(port, &ret_size, &key, &povl, 0);
    ok(!ret, "Expected failure.\n");
    ok(GetLastError() == WAIT_TIMEOUT, "Got error %u.\n", GetLastError());

403 404 405
    sprintf(req_text, simple_req, tcp_port);
    ret = send(s, req_text, strlen(req_text), 0);
    ok(ret == strlen(req_text), "send() returned %d.\n", ret);
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432

    ret_size = key = 0xdeadbeef;
    ret = GetQueuedCompletionStatus(port, &ret_size, &key, &povl, 1000);
    ok(ret, "Got error %u.\n", GetLastError());
    ok(povl == &ovl, "OVERLAPPED pointers didn't match.\n");
    ok(key == 123, "Got unexpected key %lu.\n", key);
    ok(ret_size > sizeof(*req), "Got size %u.\n", ret_size);

    ret = GetQueuedCompletionStatus(port, &ret_size, &key, &povl, 0);
    ok(!ret, "Expected failure.\n");
    ok(GetLastError() == WAIT_TIMEOUT, "Got error %u.\n", GetLastError());

    response.StatusCode = 418;
    response.pReason = "I'm a teapot";
    response.ReasonLength = 12;
    ret = HttpSendHttpResponse(queue, req->RequestId, 0, (HTTP_RESPONSE *)&response, NULL, NULL, NULL, 0, &ovl, NULL);
    ok(!ret, "Got error %u.\n", ret);

    ret_size = key = 0xdeadbeef;
    ret = GetQueuedCompletionStatus(port, &ret_size, &key, &povl, 1000);
    ok(ret, "Got error %u.\n", GetLastError());
    ok(povl == &ovl, "OVERLAPPED pointers didn't match.\n");
    ok(key == 123, "Got unexpected key %lu.\n", key);

    ret = recv(s, response_buffer, sizeof(response_buffer), 0);
    ok(ret == ret_size, "Expected size %u, got %u.\n", ret_size, ret);

433
    ret = remove_url_v1(queue, tcp_port);
434 435 436 437 438 439
    ok(!ret, "Got error %u.\n", ret);
    closesocket(s);
    CloseHandle(port);
    CloseHandle(ovl.hEvent);
    ret = CloseHandle(queue);
    ok(ret, "Failed to close queue handle, error %u.\n", GetLastError());
440
}
441 442 443

static void test_v1_multiple_requests(void)
{
444 445
    char DECLSPEC_ALIGN(8) req_buffer1[2048];
    char DECLSPEC_ALIGN(8) req_buffer2[2048];
446 447 448 449
    HTTP_REQUEST_V1 *req1 = (HTTP_REQUEST_V1 *)req_buffer1, *req2 = (HTTP_REQUEST_V1 *)req_buffer2;
    HTTP_RESPONSE_V1 response = {};
    struct sockaddr_in sockaddr;
    OVERLAPPED ovl1, ovl2;
450 451
    unsigned short port;
    char req_text[200];
452 453 454 455 456 457 458 459 460 461
    DWORD ret_size;
    SOCKET s1, s2;
    HANDLE queue;
    ULONG ret;
    int len;

    ovl1.hEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
    ovl2.hEvent = CreateEventA(NULL, TRUE, FALSE, NULL);

    ret = HttpCreateHttpHandle(&queue, 0);
462
    ok(!ret, "Got error %u.\n", ret);
463
    port = add_url_v1(queue);
464 465 466 467 468 469 470 471 472 473 474

    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req1, sizeof(req_buffer1), NULL, &ovl1);
    ok(ret == ERROR_IO_PENDING, "Got error %u.\n", ret);
    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req2, sizeof(req_buffer2), NULL, &ovl2);
    ok(ret == ERROR_IO_PENDING, "Got error %u.\n", ret);

    SetLastError(0xdeadbeef);
    ret = GetOverlappedResult(queue, &ovl1, &ret_size, FALSE);
    ok(!ret, "Expected failure.\n");
    ok(GetLastError() == ERROR_IO_INCOMPLETE, "Got error %u.\n", GetLastError());

475 476 477 478
    s1 = create_client_socket(port);
    sprintf(req_text, simple_req, port);
    ret = send(s1, req_text, strlen(req_text), 0);
    ok(ret == strlen(req_text), "send() returned %d.\n", ret);
479 480 481 482 483 484

    ret = WaitForSingleObject(ovl1.hEvent, 100);
    ok(!ret, "Got %u.\n", ret);
    ret = WaitForSingleObject(ovl2.hEvent, 100);
    ok(ret == WAIT_TIMEOUT, "Got %u.\n", ret);

485 486 487
    s2 = create_client_socket(port);
    ret = send(s2, req_text, strlen(req_text), 0);
    ok(ret == strlen(req_text), "send() returned %d.\n", ret);
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519

    ret = WaitForSingleObject(ovl1.hEvent, 0);
    ok(!ret, "Got %u.\n", ret);
    ret = WaitForSingleObject(ovl2.hEvent, 100);
    ok(!ret, "Got %u.\n", ret);

    len = sizeof(sockaddr);
    getsockname(s1, (struct sockaddr *)&sockaddr, &len);
    ok(!memcmp(req1->Address.pRemoteAddress, &sockaddr, len), "Client addresses didn't match.\n");
    len = sizeof(sockaddr);
    getsockname(s2, (struct sockaddr *)&sockaddr, &len);
    ok(!memcmp(req2->Address.pRemoteAddress, &sockaddr, len), "Client addresses didn't match.\n");
    ok(req1->ConnectionId != req2->ConnectionId,
            "Expected different connection IDs, but got %s.\n", wine_dbgstr_longlong(req1->ConnectionId));
    ok(req1->RequestId != req2->RequestId,
            "Expected different request IDs, but got %s.\n", wine_dbgstr_longlong(req1->RequestId));

    response.StatusCode = 418;
    response.pReason = "I'm a teapot";
    response.ReasonLength = 12;
    ret = HttpSendHttpResponse(queue, req1->RequestId, 0, (HTTP_RESPONSE *)&response, NULL, NULL, NULL, 0, &ovl1, NULL);
    ok(!ret, "Got error %u.\n", ret);
    ret = HttpSendHttpResponse(queue, req2->RequestId, 0, (HTTP_RESPONSE *)&response, NULL, NULL, NULL, 0, &ovl2, NULL);
    ok(!ret, "Got error %u.\n", ret);

    /* Test sending multiple requests from the same socket. */

    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req1, sizeof(req_buffer1), NULL, &ovl1);
    ok(ret == ERROR_IO_PENDING, "Got error %u.\n", ret);
    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req2, sizeof(req_buffer2), NULL, &ovl2);
    ok(ret == ERROR_IO_PENDING, "Got error %u.\n", ret);

520 521 522 523
    ret = send(s1, req_text, strlen(req_text), 0);
    ok(ret == strlen(req_text), "send() returned %d.\n", ret);
    ret = send(s1, req_text, strlen(req_text), 0);
    ok(ret == strlen(req_text), "send() returned %d.\n", ret);
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538

    ret = WaitForSingleObject(ovl1.hEvent, 100);
    ok(!ret, "Got %u.\n", ret);
    ret = WaitForSingleObject(ovl2.hEvent, 100);
    ok(ret == WAIT_TIMEOUT, "Got %u.\n", ret);

    ret = HttpSendHttpResponse(queue, req1->RequestId, 0, (HTTP_RESPONSE *)&response, NULL, NULL, NULL, 0, &ovl1, NULL);
    ok(!ret, "Got error %u.\n", ret);

    ret = WaitForSingleObject(ovl2.hEvent, 100);
    ok(!ret, "Got %u.\n", ret);
    ok(req1->ConnectionId == req2->ConnectionId, "Expected same connection IDs.\n");
    ok(req1->RequestId != req2->RequestId,
            "Expected different request IDs, but got %s.\n", wine_dbgstr_longlong(req1->RequestId));

539
    ret = remove_url_v1(queue, port);
540 541 542 543 544 545 546 547 548 549 550
    ok(!ret, "Got error %u.\n", ret);
    closesocket(s1);
    closesocket(s2);
    CloseHandle(ovl1.hEvent);
    CloseHandle(ovl2.hEvent);
    ret = CloseHandle(queue);
    ok(ret, "Failed to close queue handle, error %u.\n", GetLastError());
}

static void test_v1_short_buffer(void)
{
551 552
    char DECLSPEC_ALIGN(8) req_buffer[2048];
    char DECLSPEC_ALIGN(8) req_buffer2[2048];
553 554
    HTTP_REQUEST_V1 *req = (HTTP_REQUEST_V1 *)req_buffer, *req2 = (HTTP_REQUEST_V1 *)req_buffer2;
    HTTP_REQUEST_ID req_id;
555 556
    unsigned short port;
    char req_text[200];
557 558 559 560 561 562 563 564 565
    OVERLAPPED ovl;
    DWORD ret_size;
    HANDLE queue;
    ULONG ret;
    SOCKET s;

    ovl.hEvent = CreateEventA(NULL, TRUE, FALSE, NULL);

    ret = HttpCreateHttpHandle(&queue, 0);
566
    ok(!ret, "Got error %u.\n", ret);
567
    port = add_url_v1(queue);
568

569 570 571 572
    s = create_client_socket(port);
    sprintf(req_text, simple_req, port);
    ret = send(s, req_text, strlen(req_text), 0);
    ok(ret == strlen(req_text), "send() returned %d.\n", ret);
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601

    memset(req_buffer, 0xcc, sizeof(req_buffer));
    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req, sizeof(HTTP_REQUEST_V1) - 1, &ret_size, NULL);
    ok(ret == ERROR_INSUFFICIENT_BUFFER, "Got error %u.\n", ret);
    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req, sizeof(HTTP_REQUEST_V1), &ret_size, NULL);
    ok(ret == ERROR_MORE_DATA, "Got error %u.\n", ret);
    ok(ret_size > sizeof(*req), "Got size %u.\n", ret_size);
    ok(!!req->ConnectionId, "Got connection ID %s.\n", wine_dbgstr_longlong(req->ConnectionId));
    ok(!!req->RequestId, "Got request ID %s.\n", wine_dbgstr_longlong(req->RequestId));
    ok(!req->Version.MajorVersion || req->Version.MajorVersion == 0xcccc /* < Vista */,
            "Got major version %u.\n", req->Version.MajorVersion);
    ok(!req->BytesReceived || req->BytesReceived == ((ULONGLONG)0xcccccccc << 32 | 0xcccccccc) /* < Vista */,
            "Got %s bytes.\n", wine_dbgstr_longlong(req->BytesReceived));

    /* At this point the request has been assigned a specific ID, and one cannot
     * receive it by calling with HTTP_NULL_ID. */
    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req2, sizeof(req_buffer2), NULL, &ovl);
    ok(ret == ERROR_IO_PENDING, "Got error %u.\n", ret);

    req_id = req->RequestId;
    memset(req_buffer, 0xcc, sizeof(req_buffer));
    ret = HttpReceiveHttpRequest(queue, req_id, 0, (HTTP_REQUEST *)req, ret_size - 1, &ret_size, NULL);
    ok(ret == ERROR_MORE_DATA, "Got error %u.\n", ret);

    memset(req_buffer, 0xcc, sizeof(req_buffer));
    ret = HttpReceiveHttpRequest(queue, req_id, 0, (HTTP_REQUEST *)req, ret_size, &ret_size, NULL);
    ok(!ret, "Got error %u.\n", ret);
    ok(req->RequestId == req_id, "Got request ID %s.\n", wine_dbgstr_longlong(req->RequestId));

602
    CancelIo(queue);
603

604
    ret = remove_url_v1(queue, port);
605 606 607 608 609 610 611 612 613
    ok(!ret, "Got error %u.\n", ret);
    closesocket(s);
    CloseHandle(ovl.hEvent);
    ret = CloseHandle(queue);
    ok(ret, "Failed to close queue handle, error %u.\n", GetLastError());
}

static void test_v1_entity_body(void)
{
614
    char DECLSPEC_ALIGN(8) req_buffer[4096], response_buffer[2048], req_body[2048], recv_body[2000];
615 616 617 618
    HTTP_REQUEST_V1 *req = (HTTP_REQUEST_V1 *)req_buffer;
    HTTP_RESPONSE_V1 response = {};
    HTTP_DATA_CHUNK chunks[2] = {};
    ULONG ret, chunk_size;
619 620
    unsigned short port;
    char req_text[200];
621
    unsigned int i;
622
    OVERLAPPED ovl;
623 624 625 626 627 628
    DWORD ret_size;
    HANDLE queue;
    SOCKET s;

    static const char post_req[] =
        "POST /xyzzy HTTP/1.1\r\n"
629
        "Host: localhost:%u\r\n"
630 631 632 633 634 635 636
        "Connection: keep-alive\r\n"
        "Content-Length: 5\r\n"
        "\r\n"
        "ping";

    static const char post_req2[] =
        "POST /xyzzy HTTP/1.1\r\n"
637
        "Host: localhost:%u\r\n"
638 639 640 641
        "Connection: keep-alive\r\n"
        "Content-Length: 2048\r\n"
        "\r\n";

642 643
    ovl.hEvent = CreateEventA(NULL, TRUE, FALSE, NULL);

644 645 646 647
    for (i = 0; i < sizeof(req_body); ++i)
        req_body[i] = i / 111;

    ret = HttpCreateHttpHandle(&queue, 0);
648
    ok(!ret, "Got error %u.\n", ret);
649
    port = add_url_v1(queue);
650

651 652 653 654
    s = create_client_socket(port);
    sprintf(req_text, post_req, port);
    ret = send(s, req_text, strlen(req_text) + 1, 0);
    ok(ret == strlen(req_text) + 1, "send() returned %d.\n", ret);
655 656 657 658 659 660 661 662 663 664 665 666
    /* Windows versions before 8 will return success, and report that an entity
     * body exists in the Flags member, but fail to account for it in the
     * BytesReceived member or actually copy it to the buffer, if
     * HttpReceiveHttpRequest() is called before the kernel has finished
     * receiving the entity body. Add a small delay to work around this. */
    Sleep(100);

    memset(req_buffer, 0xcc, sizeof(req_buffer));
    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), &ret_size, NULL);
    ok(!ret, "Got error %u.\n", ret);
    ok(ret_size > sizeof(*req), "Got size %u.\n", ret_size);
    ok(req->Flags == HTTP_REQUEST_FLAG_MORE_ENTITY_BODY_EXISTS, "Got flags %#x.\n", req->Flags);
667
    ok(req->BytesReceived == strlen(req_text) + 1, "Got %s bytes.\n", wine_dbgstr_longlong(req->BytesReceived));
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
    ok(req->Headers.KnownHeaders[HttpHeaderContentLength].RawValueLength == 1,
            "Got header length %u.\n", req->Headers.KnownHeaders[HttpHeaderContentLength].RawValueLength);
    ok(!strcmp(req->Headers.KnownHeaders[HttpHeaderContentLength].pRawValue, "5"),
            "Got header value %s.\n", req->Headers.KnownHeaders[HttpHeaderContentLength].pRawValue);
    ok(!req->EntityChunkCount, "Got %u entity chunks.\n", req->EntityChunkCount);
    ok(!req->pEntityChunks, "Got entity chunks %p.\n", req->pEntityChunks);

    response.StatusCode = 418;
    response.pReason = "I'm a teapot";
    response.ReasonLength = 12;
    response.EntityChunkCount = ARRAY_SIZE(chunks);
    response.pEntityChunks = chunks;
    chunks[0].DataChunkType = HttpDataChunkFromMemory;
    chunks[0].FromMemory.pBuffer = (void *)"pong";
    chunks[0].FromMemory.BufferLength = 4;
    chunks[1].DataChunkType = HttpDataChunkFromMemory;
    chunks[1].FromMemory.pBuffer = (void *)"pang";
    chunks[1].FromMemory.BufferLength = 4;
    ret = HttpSendHttpResponse(queue, req->RequestId, 0, (HTTP_RESPONSE *)&response, NULL, NULL, NULL, 0, NULL, NULL);
    ok(!ret, "Got error %u.\n", ret);

689 690
    memset(response_buffer, 0, sizeof(response_buffer));

691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
    ret = recv(s, response_buffer, sizeof(response_buffer), 0);
    ok(ret > 0, "recv() failed.\n");
    if (winetest_debug > 1)
        trace("%.*s\n", ret, response_buffer);
    ok(!strncmp(response_buffer, "HTTP/1.1 418 I'm a teapot\r\n", 27), "Got incorrect status line.\n");
    ok(!!strstr(response_buffer, "\r\nContent-Length: 8\r\n"), "Missing or malformed Content-Length header.\n");
    ok(!!strstr(response_buffer, "\r\nDate:"), "Missing Date header.\n");
    ok(!memcmp(response_buffer + ret - 12, "\r\n\r\npongpang", 12), "Response did not end with entity data.\n");

    ret = HttpReceiveHttpRequest(queue, req->RequestId, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), &ret_size, NULL);
    ok(ret == ERROR_CONNECTION_INVALID, "Got error %u.\n", ret);

    /* http won't overwrite a Content-Length header if we manually supply one,
     * but it also won't truncate the entity body to match. It will however
     * always write its own Date header. */

707 708
    ret = send(s, req_text, strlen(req_text) + 1, 0);
    ok(ret == strlen(req_text) + 1, "send() returned %d.\n", ret);
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), &ret_size, NULL);
    ok(!ret, "Got error %u.\n", ret);

    response.Headers.KnownHeaders[HttpHeaderContentLength].RawValueLength = 1;
    response.Headers.KnownHeaders[HttpHeaderContentLength].pRawValue = "6";
    response.Headers.KnownHeaders[HttpHeaderDate].RawValueLength = 10;
    response.Headers.KnownHeaders[HttpHeaderDate].pRawValue = "yesteryear";
    ret = HttpSendHttpResponse(queue, req->RequestId, 0, (HTTP_RESPONSE *)&response, NULL, NULL, NULL, 0, NULL, NULL);
    ok(!ret, "Got error %u.\n", ret);

    ret = recv(s, response_buffer, sizeof(response_buffer), 0);
    ok(ret > 0, "recv() failed.\n");
    if (winetest_debug > 1)
        trace("%.*s\n", ret, response_buffer);
    ok(!strncmp(response_buffer, "HTTP/1.1 418 I'm a teapot\r\n", 27), "Got incorrect status line.\n");
    ok(!!strstr(response_buffer, "\r\nContent-Length: 6\r\n"), "Missing or malformed Content-Length header.\n");
    ok(!!strstr(response_buffer, "\r\nDate:"), "Missing Date header.\n");
    ok(!strstr(response_buffer, "yesteryear"), "Unexpected Date value.\n");
    ok(!memcmp(response_buffer + ret - 12, "\r\n\r\npongpang", 12), "Response did not end with entity data.\n");

    /* Test the COPY_BODY flag. */

731 732
    ret = send(s, req_text, strlen(req_text) + 1, 0);
    ok(ret == strlen(req_text) + 1, "send() returned %d.\n", ret);
733 734 735 736 737 738 739
    Sleep(100);

    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, HTTP_RECEIVE_REQUEST_FLAG_COPY_BODY,
            (HTTP_REQUEST *)req, sizeof(req_buffer), &ret_size, NULL);
    ok(!ret, "Got error %u.\n", ret);
    ok(ret_size > sizeof(*req), "Got size %u.\n", ret_size);
    ok(!req->Flags, "Got flags %#x.\n", req->Flags);
740
    ok(req->BytesReceived == strlen(req_text) + 1, "Got %s bytes.\n", wine_dbgstr_longlong(req->BytesReceived));
741 742 743 744 745 746 747 748 749 750 751 752 753 754
    ok(req->Headers.KnownHeaders[HttpHeaderContentLength].RawValueLength == 1,
            "Got header length %u.\n", req->Headers.KnownHeaders[HttpHeaderContentLength].RawValueLength);
    ok(!strcmp(req->Headers.KnownHeaders[HttpHeaderContentLength].pRawValue, "5"),
            "Got header value %s.\n", req->Headers.KnownHeaders[HttpHeaderContentLength].pRawValue);
    ok(req->EntityChunkCount == 1, "Got %u entity chunks.\n", req->EntityChunkCount);
    ok(req->pEntityChunks[0].DataChunkType == HttpDataChunkFromMemory,
            "Got chunk type %u.\n", req->pEntityChunks[0].DataChunkType);
    ok(req->pEntityChunks[0].FromMemory.BufferLength == 5,
            "Got chunk length %u.\n", req->pEntityChunks[0].FromMemory.BufferLength);
    ok(!memcmp(req->pEntityChunks[0].FromMemory.pBuffer, "ping", 5),
            "Got chunk data '%s'.\n", (char *)req->pEntityChunks[0].FromMemory.pBuffer);

    send_response_v1(queue, req->RequestId, s);

755 756 757
    sprintf(req_text, post_req2, port);
    ret = send(s, req_text, strlen(req_text), 0);
    ok(ret == strlen(req_text), "send() returned %d.\n", ret);
758 759 760 761 762 763 764 765 766 767
    ret = send(s, req_body, sizeof(req_body), 0);
    ok(ret == sizeof(req_body), "send() returned %d.\n", ret);

    Sleep(100);

    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, HTTP_RECEIVE_REQUEST_FLAG_COPY_BODY,
            (HTTP_REQUEST *)req, 2000, &ret_size, NULL);
    ok(!ret, "Got error %u.\n", ret);
    ok(ret_size == 2000, "Got size %u.\n", ret_size);
    ok(req->Flags == HTTP_REQUEST_FLAG_MORE_ENTITY_BODY_EXISTS, "Got flags %#x.\n", req->Flags);
768
    ok(req->BytesReceived == strlen(req_text) + 2048, "Got %s bytes.\n", wine_dbgstr_longlong(req->BytesReceived));
769 770 771 772 773 774 775 776 777 778 779 780 781
    ok(req->Headers.KnownHeaders[HttpHeaderContentLength].RawValueLength == 4,
            "Got header length %u.\n", req->Headers.KnownHeaders[HttpHeaderContentLength].RawValueLength);
    ok(!strcmp(req->Headers.KnownHeaders[HttpHeaderContentLength].pRawValue, "2048"),
            "Got header value %s.\n", req->Headers.KnownHeaders[HttpHeaderContentLength].pRawValue);
    ok(req->EntityChunkCount == 1, "Got %u entity chunks.\n", req->EntityChunkCount);
    ok(req->pEntityChunks[0].DataChunkType == HttpDataChunkFromMemory,
            "Got chunk type %u.\n", req->pEntityChunks[0].DataChunkType);
    chunk_size = req->pEntityChunks[0].FromMemory.BufferLength;
    ok(chunk_size > 0 && chunk_size < 2000, "Got chunk size %u.\n", chunk_size);
    ok(!memcmp(req->pEntityChunks[0].FromMemory.pBuffer, req_body, chunk_size), "Chunk data didn't match.\n");

    send_response_v1(queue, req->RequestId, s);

782 783
    /* Test HttpReceiveRequestEntityBody(). */

784 785 786 787 788 789
    ret_size = 0xdeadbeef;
    ret = HttpReceiveRequestEntityBody(NULL, HTTP_NULL_ID, 0, recv_body, sizeof(recv_body), &ret_size, NULL);
    ok(ret == ERROR_INVALID_HANDLE, "Got error %u.\n", ret);
    ret = HttpReceiveRequestEntityBody(NULL, HTTP_NULL_ID, 0, recv_body, sizeof(recv_body), NULL, &ovl);
    ok(ret == ERROR_INVALID_HANDLE, "Got error %u.\n", ret);

790 791 792
    sprintf(req_text, post_req, port);
    ret = send(s, req_text, strlen(req_text) + 1, 0);
    ok(ret == strlen(req_text) + 1, "send() returned %d.\n", ret);
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
    Sleep(100);

    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), &ret_size, NULL);
    ok(!ret, "Got error %u.\n", ret);

    ret = HttpReceiveRequestEntityBody(queue, HTTP_NULL_ID, 0, recv_body, sizeof(recv_body), &ret_size, NULL);
    ok(ret == ERROR_CONNECTION_INVALID, "Got error %u.\n", ret);

    ret_size = 0xdeadbeef;
    ret = HttpReceiveRequestEntityBody(queue, req->RequestId, 0, recv_body, sizeof(recv_body), &ret_size, NULL);
    ok(!ret, "Got error %u.\n", ret);
    ok(ret_size == 5, "Got size %u.\n", ret_size);
    ok(!memcmp(recv_body, "ping", 5), "Entity body didn't match.\n");

    ret_size = 0xdeadbeef;
    ret = HttpReceiveRequestEntityBody(queue, req->RequestId, 0, recv_body, sizeof(recv_body), &ret_size, NULL);
    ok(ret == ERROR_HANDLE_EOF, "Got error %u.\n", ret);
    ok(ret_size == 0xdeadbeef || !ret_size /* Win10+ */, "Got size %u.\n", ret_size);

    send_response_v1(queue, req->RequestId, s);

    ret = HttpReceiveRequestEntityBody(queue, req->RequestId, 0, recv_body, sizeof(recv_body), &ret_size, NULL);
    ok(ret == ERROR_CONNECTION_INVALID, "Got error %u.\n", ret);

817 818
    ret = send(s, req_text, strlen(req_text) + 1, 0);
    ok(ret == strlen(req_text) + 1, "send() returned %d.\n", ret);
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
    Sleep(100);

    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), &ret_size, NULL);
    ok(!ret, "Got error %u.\n", ret);

    memset(recv_body, 0xcc, sizeof(recv_body));
    ret_size = 0xdeadbeef;
    ret = HttpReceiveRequestEntityBody(queue, req->RequestId, 0, recv_body, 2, &ret_size, NULL);
    ok(!ret, "Got error %u.\n", ret);
    ok(ret_size == 2, "Got size %u.\n", ret_size);
    ok(!memcmp(recv_body, "pi", 2), "Entity body didn't match.\n");

    ret_size = 0xdeadbeef;
    ret = HttpReceiveRequestEntityBody(queue, req->RequestId, 0, recv_body, 4, &ret_size, NULL);
    ok(!ret, "Got error %u.\n", ret);
    ok(ret_size == 3, "Got size %u.\n", ret_size);
    ok(!memcmp(recv_body, "ng", 3), "Entity body didn't match.\n");

    ret_size = 0xdeadbeef;
    ret = HttpReceiveRequestEntityBody(queue, req->RequestId, 0, recv_body, sizeof(recv_body), &ret_size, NULL);
    ok(ret == ERROR_HANDLE_EOF, "Got error %u.\n", ret);
    ok(ret_size == 0xdeadbeef || !ret_size /* Win10+ */, "Got size %u.\n", ret_size);

    send_response_v1(queue, req->RequestId, s);

844 845
    ret = send(s, req_text, strlen(req_text) + 1, 0);
    ok(ret == strlen(req_text) + 1, "send() returned %d.\n", ret);
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
    Sleep(100);

    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), &ret_size, NULL);
    ok(!ret, "Got error %u.\n", ret);

    memset(recv_body, 0xcc, sizeof(recv_body));
    ret_size = 0xdeadbeef;
    ret = HttpReceiveRequestEntityBody(queue, req->RequestId, 0, recv_body, sizeof(recv_body), NULL, &ovl);
    ok(!ret || ret == ERROR_IO_PENDING, "Got error %u.\n", ret);
    ret = GetOverlappedResult(queue, &ovl, &ret_size, TRUE);
    ok(ret, "Got error %u.\n", GetLastError());
    ok(ret_size == 5, "Got size %u.\n", ret_size);
    ok(!memcmp(recv_body, "ping", 5), "Entity body didn't match.\n");

    ret = HttpReceiveRequestEntityBody(queue, req->RequestId, 0, recv_body, sizeof(recv_body), NULL, &ovl);
    ok(ret == ERROR_HANDLE_EOF, "Got error %u.\n", ret);

    send_response_v1(queue, req->RequestId, s);

865 866
    ret = send(s, req_text, strlen(req_text) + 1, 0);
    ok(ret == strlen(req_text) + 1, "send() returned %d.\n", ret);
867 868 869 870 871 872 873 874 875 876 877
    Sleep(100);

    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, HTTP_RECEIVE_REQUEST_FLAG_COPY_BODY,
            (HTTP_REQUEST *)req, sizeof(req_buffer), &ret_size, NULL);
    ok(!ret, "Got error %u.\n", ret);

    ret = HttpReceiveRequestEntityBody(queue, req->RequestId, 0, recv_body, sizeof(recv_body), NULL, &ovl);
    ok(ret == ERROR_HANDLE_EOF, "Got error %u.\n", ret);

    send_response_v1(queue, req->RequestId, s);

878 879 880
    sprintf(req_text, post_req2, port);
    ret = send(s, req_text, strlen(req_text), 0);
    ok(ret == strlen(req_text), "send() returned %d.\n", ret);
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
    ret = send(s, req_body, sizeof(req_body), 0);
    ok(ret == sizeof(req_body), "send() returned %d.\n", ret);

    Sleep(100);

    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, HTTP_RECEIVE_REQUEST_FLAG_COPY_BODY,
            (HTTP_REQUEST *)req, 2000, &ret_size, NULL);
    ok(!ret, "Got error %u.\n", ret);
    ok(ret_size == 2000, "Got size %u.\n", ret_size);
    ok(req->Flags == HTTP_REQUEST_FLAG_MORE_ENTITY_BODY_EXISTS, "Got flags %#x.\n", req->Flags);
    chunk_size = req->pEntityChunks[0].FromMemory.BufferLength;

    memset(recv_body, 0xcc, sizeof(recv_body));
    ret_size = 0xdeadbeef;
    ret = HttpReceiveRequestEntityBody(queue, req->RequestId, 0, recv_body, sizeof(recv_body), &ret_size, NULL);
    ok(!ret, "Got error %u.\n", ret);
    ok(ret_size == 2048 - chunk_size, "Got size %u.\n", ret_size);
    ok(!memcmp(recv_body, req_body + chunk_size, ret_size), "Entity body didn't match.\n");

    send_response_v1(queue, req->RequestId, s);

    CloseHandle(ovl.hEvent);
903
    ret = remove_url_v1(queue, port);
904 905 906 907 908 909 910 911 912
    ok(!ret, "Got error %u.\n", ret);
    closesocket(s);
    ret = CloseHandle(queue);
    ok(ret, "Failed to close queue handle, error %u.\n", GetLastError());
}

static void test_v1_bad_request(void)
{
    char response_buffer[2048];
913
    unsigned short port;
914 915 916 917 918
    HANDLE queue;
    ULONG ret;
    SOCKET s;

    ret = HttpCreateHttpHandle(&queue, 0);
919
    ok(!ret, "Got error %u.\n", ret);
920
    port = add_url_v1(queue);
921

922
    s = create_client_socket(port);
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
    ret = send(s, "foo\r\n", strlen("foo\r\n"), 0);
    ok(ret == strlen("foo\r\n"), "send() returned %d.\n", ret);

    ret = recv(s, response_buffer, sizeof(response_buffer), 0);
    ok(ret > 0, "recv() failed.\n");

    if (winetest_debug > 1)
        trace("%.*s\n", ret, response_buffer);

    ok(!strncmp(response_buffer, "HTTP/1.1 400 Bad Request\r\n", 26), "Got incorrect status line.\n");
    ok(!!strstr(response_buffer, "\r\nConnection: close\r\n"), "Missing or malformed Connection header.\n");

    ret = send(s, "foo\r\n", strlen("foo\r\n"), 0);
    ok(ret == strlen("foo\r\n"), "send() returned %d.\n", ret);

    WSASetLastError(0xdeadbeef);
    ret = recv(s, response_buffer, sizeof(response_buffer), 0);
    ok(!ret, "Connection should be shut down.\n");
    ok(!WSAGetLastError(), "Got error %u.\n", WSAGetLastError());

943
    ret = remove_url_v1(queue, port);
944 945 946 947 948 949 950 951 952 953
    ok(!ret, "Got error %u.\n", ret);
    closesocket(s);
    ret = CloseHandle(queue);
    ok(ret, "Failed to close queue handle, error %u.\n", GetLastError());
}

static void test_v1_cooked_url(void)
{
    char DECLSPEC_ALIGN(8) req_buffer[2048];
    HTTP_REQUEST_V1 *req = (HTTP_REQUEST_V1 *)req_buffer;
954 955 956
    char expect[24], req_text[200];
    unsigned short port;
    WCHAR expectW[50];
957 958 959 960 961 962 963
    DWORD ret_size;
    HANDLE queue;
    ULONG ret;
    SOCKET s;

    static const char req1[] =
        "GET /foobar?a=b HTTP/1.1\r\n"
964
        "Host: localhost:%u\r\n"
965 966 967 968
        "Connection: keep-alive\r\n"
        "\r\n";

    static const char req2[] =
969
        "GET http://localhost:%u/ HTTP/1.1\r\n"
970 971 972 973 974
        "Host: ignored\r\n"
        "Connection: keep-alive\r\n"
        "\r\n";

    ret = HttpCreateHttpHandle(&queue, 0);
975
    ok(!ret, "Got error %u.\n", ret);
976
    port = add_url_v1(queue);
977

978 979 980 981
    s = create_client_socket(port);
    sprintf(req_text, req1, port);
    ret = send(s, req_text, strlen(req_text), 0);
    ok(ret == strlen(req_text), "send() returned %d.\n", ret);
982 983 984 985 986 987 988 989 990 991 992

    memset(req_buffer, 0xcc, sizeof(req_buffer));
    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), &ret_size, NULL);
    ok(!ret, "Got error %u.\n", ret);
    ok(ret_size > sizeof(*req), "Got size %u.\n", ret_size);
    ok(req->RawUrlLength == 11, "Got raw URL length %u.\n", req->RawUrlLength);
    ok(!strcmp(req->pRawUrl, "/foobar?a=b"), "Got raw URL %s.\n", req->pRawUrl);
    ok(req->CookedUrl.FullUrlLength == 66, "Got full URL length %u.\n", req->CookedUrl.FullUrlLength);
    ok(req->CookedUrl.HostLength == 30, "Got host length %u.\n", req->CookedUrl.HostLength);
    ok(req->CookedUrl.AbsPathLength == 14, "Got absolute path length %u.\n", req->CookedUrl.AbsPathLength);
    ok(req->CookedUrl.QueryStringLength == 8, "Got query string length %u.\n", req->CookedUrl.QueryStringLength);
993 994 995
    swprintf(expectW, ARRAY_SIZE(expectW), L"http://localhost:%u/foobar?a=b", port);
    ok(!wcscmp(req->CookedUrl.pFullUrl, expectW), "Expected full URL %s, got %s.\n",
            debugstr_w(expectW), debugstr_w(req->CookedUrl.pFullUrl));
996 997 998 999 1000 1001 1002 1003
    ok(req->CookedUrl.pHost == req->CookedUrl.pFullUrl + 7, "Got host %s.\n", wine_dbgstr_w(req->CookedUrl.pHost));
    ok(req->CookedUrl.pAbsPath == req->CookedUrl.pFullUrl + 22,
            "Got absolute path %s.\n", wine_dbgstr_w(req->CookedUrl.pAbsPath));
    ok(req->CookedUrl.pQueryString == req->CookedUrl.pFullUrl + 29,
            "Got query string %s.\n", wine_dbgstr_w(req->CookedUrl.pQueryString));

    send_response_v1(queue, req->RequestId, s);

1004 1005 1006
    sprintf(req_text, req2, port);
    ret = send(s, req_text, strlen(req_text), 0);
    ok(ret == strlen(req_text), "send() returned %d.\n", ret);
1007 1008 1009 1010 1011 1012

    memset(req_buffer, 0xcc, sizeof(req_buffer));
    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), &ret_size, NULL);
    ok(!ret, "Got error %u.\n", ret);
    ok(ret_size > sizeof(*req), "Got size %u.\n", ret_size);
    ok(req->RawUrlLength == 23, "Got raw URL length %u.\n", req->RawUrlLength);
1013 1014
    sprintf(expect, "http://localhost:%u/", port);
    ok(!strcmp(req->pRawUrl, expect), "Expected raw URL \"%s\", got \"%s\".\n", expect, req->pRawUrl);
1015 1016 1017 1018
    ok(req->CookedUrl.FullUrlLength == 46, "Got full URL length %u.\n", req->CookedUrl.FullUrlLength);
    ok(req->CookedUrl.HostLength == 30, "Got host length %u.\n", req->CookedUrl.HostLength);
    ok(req->CookedUrl.AbsPathLength == 2, "Got absolute path length %u.\n", req->CookedUrl.AbsPathLength);
    ok(!req->CookedUrl.QueryStringLength, "Got query string length %u.\n", req->CookedUrl.QueryStringLength);
1019 1020 1021
    swprintf(expectW, ARRAY_SIZE(expectW), L"http://localhost:%u/", port);
    ok(!wcscmp(req->CookedUrl.pFullUrl, expectW), "Expected full URL %s, got %s.\n",
            wine_dbgstr_w(expectW), wine_dbgstr_w(req->CookedUrl.pFullUrl));
1022 1023 1024 1025 1026 1027 1028
    ok(req->CookedUrl.pHost == req->CookedUrl.pFullUrl + 7, "Got host %s.\n", wine_dbgstr_w(req->CookedUrl.pHost));
    ok(req->CookedUrl.pAbsPath == req->CookedUrl.pFullUrl + 22,
            "Got absolute path %s.\n", wine_dbgstr_w(req->CookedUrl.pAbsPath));
    ok(!req->CookedUrl.pQueryString, "Got query string %s.\n", wine_dbgstr_w(req->CookedUrl.pQueryString));

    send_response_v1(queue, req->RequestId, s);

1029
    ret = remove_url_v1(queue, port);
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
    ok(!ret, "Got error %u.\n", ret);
    closesocket(s);
    ret = CloseHandle(queue);
    ok(ret, "Failed to close queue handle, error %u.\n", GetLastError());
}

static void test_v1_unknown_tokens(void)
{
    char DECLSPEC_ALIGN(8) req_buffer[2048];
    HTTP_REQUEST_V1 *req = (HTTP_REQUEST_V1 *)req_buffer;
1040 1041
    unsigned short port;
    char req_text[200];
1042 1043 1044 1045 1046 1047 1048
    DWORD ret_size;
    HANDLE queue;
    ULONG ret;
    SOCKET s;

    static const char req1[] =
        "xyzzy / HTTP/1.1\r\n"
1049
        "Host: localhost:%u\r\n"
1050 1051 1052 1053 1054
        "Connection: keep-alive\r\n"
        "Qux: foo baz \r\n"
        "\r\n";

    ret = HttpCreateHttpHandle(&queue, 0);
1055
    ok(!ret, "Got error %u.\n", ret);
1056
    port = add_url_v1(queue);
1057

1058 1059 1060 1061
    s = create_client_socket(port);
    sprintf(req_text, req1, port);
    ret = send(s, req_text, strlen(req_text), 0);
    ok(ret == strlen(req_text), "send() returned %d.\n", ret);
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078

    memset(req_buffer, 0xcc, sizeof(req_buffer));
    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), &ret_size, NULL);
    ok(!ret, "Got error %u.\n", ret);
    ok(req->Verb == HttpVerbUnknown, "Got verb %u.\n", req->Verb);
    ok(req->UnknownVerbLength == 5, "Got unknown verb length %u.\n", req->UnknownVerbLength);
    ok(!strcmp(req->pUnknownVerb, "xyzzy"), "Got unknown verb %s.\n", req->pUnknownVerb);
    ok(req->Headers.UnknownHeaderCount == 1, "Got %u unknown headers.\n", req->Headers.UnknownHeaderCount);
    ok(req->Headers.pUnknownHeaders[0].NameLength == 3, "Got name length %u.\n",
            req->Headers.pUnknownHeaders[0].NameLength);
    ok(!strcmp(req->Headers.pUnknownHeaders[0].pName, "Qux"), "Got name %s.\n",
            req->Headers.pUnknownHeaders[0].pName);
    ok(req->Headers.pUnknownHeaders[0].RawValueLength == 7, "Got value length %u.\n",
            req->Headers.pUnknownHeaders[0].RawValueLength);
    ok(!strcmp(req->Headers.pUnknownHeaders[0].pRawValue, "foo baz"), "Got value %s.\n",
            req->Headers.pUnknownHeaders[0].pRawValue);

1079
    ret = remove_url_v1(queue, port);
1080 1081 1082 1083
    ok(!ret, "Got error %u.\n", ret);
    closesocket(s);
    ret = CloseHandle(queue);
    ok(ret, "Failed to close queue handle, error %u.\n", GetLastError());
1084 1085
}

1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
static void test_v1_urls(void)
{
    char DECLSPEC_ALIGN(8) req_buffer[2048];
    HTTP_REQUEST_V1 *req = (HTTP_REQUEST_V1 *)req_buffer;
    unsigned short port;
    char req_text[200];
    DWORD ret_size;
    WCHAR url[50];
    HANDLE queue;
    ULONG ret;
    SOCKET s;

    ret = HttpCreateHttpHandle(&queue, 0);
    ok(!ret, "Got error %u.\n", ret);

    for (port = 50000; port < 51000; ++port)
    {
        swprintf(url, ARRAY_SIZE(url), L"http://+:%u/", port);
        if (!(ret = HttpAddUrl(queue, url, NULL)))
            break;
        if (ret == ERROR_ACCESS_DENIED)
        {
            skip("Not enough permissions to bind to all URLs.\n");
            CloseHandle(queue);
            return;
        }
        ok(ret == ERROR_SHARING_VIOLATION, "Failed to add %s, error %u.\n", debugstr_w(url), ret);
    }

    ok(!ret, "Got error %u.\n", ret);

    s = create_client_socket(port);
    sprintf(req_text, simple_req, port);
    ret = send(s, req_text, strlen(req_text), 0);
    ok(ret == strlen(req_text), "send() returned %d.\n", ret);

    memset(req_buffer, 0xcc, sizeof(req_buffer));
    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), &ret_size, NULL);
    ok(!ret, "Got error %u.\n", ret);
    ok(ret_size > sizeof(*req), "Got size %u.\n", ret_size);

    send_response_v1(queue, req->RequestId, s);

    ret = HttpRemoveUrl(queue, url);
    ok(!ret, "Got error %u.\n", ret);
    closesocket(s);
    ret = CloseHandle(queue);
    ok(ret, "Failed to close queue handle, error %u.\n", GetLastError());
}

1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
static void test_HttpCreateServerSession(void)
{
    HTTP_SERVER_SESSION_ID session;
    HTTPAPI_VERSION version;
    ULONG ret;

    version.HttpApiMajorVersion = 1;
    version.HttpApiMinorVersion = 0;
    ret = pHttpCreateServerSession(version, NULL, 0);
    ok(ret == ERROR_INVALID_PARAMETER, "Unexpected return value %u.\n", ret);

    version.HttpApiMajorVersion = 1;
    version.HttpApiMinorVersion = 1;
    ret = pHttpCreateServerSession(version, &session, 0);
    ok(ret == ERROR_REVISION_MISMATCH, "Unexpected return value %u.\n", ret);

    version.HttpApiMajorVersion = 3;
    version.HttpApiMinorVersion = 0;
    ret = pHttpCreateServerSession(version, &session, 0);
    ok(ret == ERROR_REVISION_MISMATCH, "Unexpected return value %u.\n", ret);

    version.HttpApiMajorVersion = 2;
    version.HttpApiMinorVersion = 0;
    ret = pHttpCreateServerSession(version, &session, 0);
    ok(!ret, "Unexpected return value %u.\n", ret);
    ret = pHttpCloseServerSession(session);
    ok(!ret, "Unexpected return value %u.\n", ret);

    version.HttpApiMajorVersion = 1;
    version.HttpApiMinorVersion = 0;
    ret = pHttpCreateServerSession(version, &session, 0);
    ok(!ret, "Unexpected return value %u.\n", ret);
    ret = pHttpCloseServerSession(session);
    ok(!ret, "Unexpected return value %u.\n", ret);

    ret = pHttpCloseServerSession(0xdead);
    ok(ret == ERROR_INVALID_PARAMETER, "Unexpected return value %u.\n", ret);
}

static void test_HttpCreateUrlGroup(void)
{
    HTTP_SERVER_SESSION_ID session;
    HTTP_URL_GROUP_ID group_id;
    HTTPAPI_VERSION version;
    ULONG ret;

    group_id = 1;
    ret = pHttpCreateUrlGroup(0, &group_id, 0);
    ok(ret == ERROR_INVALID_PARAMETER, "Unexpected return value %u.\n", ret);
    ok(group_id == 1, "Unexpected group id %s.\n", wine_dbgstr_longlong(group_id));

    /* Create session, url group, close session. */
    version.HttpApiMajorVersion = 1;
    version.HttpApiMinorVersion = 0;
    ret = pHttpCreateServerSession(version, &session, 0);
    ok(!ret, "Unexpected return value %u.\n", ret);

    group_id = 0;
    ret = pHttpCreateUrlGroup(session, &group_id, 0);
    ok(!ret, "Unexpected return value %u.\n", ret);
    ok(group_id != 0, "Unexpected group id %s.\n", wine_dbgstr_longlong(group_id));

    ret = pHttpCloseServerSession(session);
    ok(!ret, "Unexpected return value %u.\n", ret);

    /* Groups are closed together with their session. */
    ret = pHttpCloseUrlGroup(group_id);
    ok(ret == ERROR_INVALID_PARAMETER, "Unexpected return value %u.\n", ret);

    /* Create session, url group, close group. */
    ret = pHttpCreateServerSession(version, &session, 0);
    ok(!ret, "Unexpected return value %u.\n", ret);

    group_id = 0;
    ret = pHttpCreateUrlGroup(session, &group_id, 0);
    ok(!ret, "Unexpected return value %u.\n", ret);
    ok(group_id != 0, "Unexpected group id %s.\n", wine_dbgstr_longlong(group_id));

    ret = pHttpCloseUrlGroup(group_id);
    ok(!ret, "Unexpected return value %u.\n", ret);

    ret = pHttpCloseServerSession(session);
    ok(!ret, "Unexpected return value %u.\n", ret);
}

1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
static void test_v2_server(void)
{
    char DECLSPEC_ALIGN(8) req_buffer[2048], response_buffer[2048];
    HTTP_REQUEST_V2 *reqv2 = (HTTP_REQUEST_V2 *)req_buffer;
    static const HTTPAPI_VERSION version = {2, 0};
    struct sockaddr_in sockaddr, *sin;
    HTTP_REQUEST_V1 *req = &reqv2->s;
    HTTP_SERVER_SESSION_ID session;
    HTTP_RESPONSE_V2 response = {};
    HTTP_BINDING_INFO binding;
    HTTP_URL_GROUP_ID group;
1232 1233
    unsigned short port;
    char req_text[100];
1234 1235 1236
    unsigned int i;
    OVERLAPPED ovl;
    DWORD ret_size;
1237
    WCHAR url[50];
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
    HANDLE queue;
    ULONG ret;
    SOCKET s;
    int len;

    ovl.hEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
    memset(req_buffer, 0xcc, sizeof(req_buffer));

    ret = pHttpCreateServerSession(version, &session, 0);
    ok(!ret, "Failed to create session, error %u.\n", ret);
    ret = pHttpCreateUrlGroup(session, &group, 0);
    ok(!ret, "Failed to create URL group, error %u.\n", ret);
    ret = pHttpCreateRequestQueue(version, NULL, NULL, 0, &queue);
    ok(!ret, "Failed to create request queue, error %u.\n", ret);
    binding.Flags.Present = 1;
    binding.RequestQueueHandle = queue;
    ret = pHttpSetUrlGroupProperty(group, HttpServerBindingProperty, &binding, sizeof(binding));
    ok(!ret, "Failed to bind request queue, error %u.\n", ret);

    ret = HttpReceiveHttpRequest(NULL, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), NULL, &ovl);
    ok(ret == ERROR_INVALID_HANDLE, "Got error %u.\n", ret);
    ret = HttpReceiveHttpRequest(queue, 0xdeadbeef, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), NULL, &ovl);
    ok(ret == ERROR_CONNECTION_INVALID, "Got error %u.\n", ret);
    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), NULL, &ovl);
    ok(ret == ERROR_IO_PENDING, "Got error %u.\n", ret);

    SetLastError(0xdeadbeef);
    ret = GetOverlappedResult(queue, &ovl, &ret_size, FALSE);
    ok(!ret, "Expected failure.\n");
    ok(GetLastError() == ERROR_IO_INCOMPLETE, "Got error %u.\n", GetLastError());

1269 1270
    port = add_url_v2(group);

1271
    ret = pHttpAddUrlToUrlGroup(group, L"http://localhost:50000", 0xdeadbeef, 0);
1272
    todo_wine ok(ret == ERROR_INVALID_PARAMETER, "Got error %u.\n", ret);
1273
    ret = pHttpAddUrlToUrlGroup(group, L"localhost:50000", 0xdeadbeef, 0);
1274
    todo_wine ok(ret == ERROR_INVALID_PARAMETER, "Got error %u.\n", ret);
1275
    ret = pHttpAddUrlToUrlGroup(group, L"localhost:50000/", 0xdeadbeef, 0);
1276
    todo_wine ok(ret == ERROR_INVALID_PARAMETER, "Got error %u.\n", ret);
1277
    ret = pHttpAddUrlToUrlGroup(group, L"http://localhost/", 0xdeadbeef, 0);
1278
    todo_wine ok(ret == ERROR_INVALID_PARAMETER, "Got error %u.\n", ret);
1279
    ret = pHttpAddUrlToUrlGroup(group, L"http://localhost:/", 0xdeadbeef, 0);
1280
    todo_wine ok(ret == ERROR_INVALID_PARAMETER, "Got error %u.\n", ret);
1281
    ret = pHttpAddUrlToUrlGroup(group, L"http://localhost:0/", 0xdeadbeef, 0);
1282
    todo_wine ok(ret == ERROR_INVALID_PARAMETER, "Got error %u.\n", ret);
1283 1284
    swprintf(url, ARRAY_SIZE(url), L"http://localhost:%u/", port);
    ret = pHttpAddUrlToUrlGroup(group, url, 0xdeadbeef, 0);
1285 1286
    todo_wine ok(ret == ERROR_ALREADY_EXISTS, "Got error %u.\n", ret);

1287
    s = create_client_socket(port);
1288 1289 1290 1291 1292 1293 1294 1295 1296
    len = sizeof(sockaddr);
    ret = getsockname(s, (struct sockaddr *)&sockaddr, &len);
    ok(ret == 0, "getsockname() failed, error %u.\n", WSAGetLastError());

    SetLastError(0xdeadbeef);
    ret = GetOverlappedResult(queue, &ovl, &ret_size, FALSE);
    ok(!ret, "Expected failure.\n");
    ok(GetLastError() == ERROR_IO_INCOMPLETE, "Got error %u.\n", GetLastError());

1297 1298 1299
    sprintf(req_text, simple_req, port);
    ret = send(s, req_text, strlen(req_text), 0);
    ok(ret == strlen(req_text), "send() returned %d.\n", ret);
1300

1301 1302 1303 1304 1305
    ret = WaitForSingleObject(ovl.hEvent, 100);
    ok(!ret, "Got %u.\n", ret);

    Sleep(100);

1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
    ret = GetOverlappedResult(queue, &ovl, &ret_size, TRUE);
    ok(ret, "Got error %u.\n", GetLastError());
    ok(ret_size > sizeof(*req), "Got size %u.\n", ret_size);

    ok(!req->Flags, "Got flags %#x.\n", req->Flags);
    ok(req->ConnectionId, "Expected nonzero connection ID.\n");
    ok(req->RequestId, "Expected nonzero connection ID.\n");
    ok(req->UrlContext == 0xdeadbeef, "Got URL context %s.\n", wine_dbgstr_longlong(req->UrlContext));
    ok(req->Version.MajorVersion == 1, "Got major version %u.\n", req->Version.MajorVersion);
    ok(req->Version.MinorVersion == 1, "Got major version %u.\n", req->Version.MinorVersion);
    ok(req->Verb == HttpVerbGET, "Got verb %u.\n", req->Verb);
    ok(!req->UnknownVerbLength, "Got unknown verb length %u.\n", req->UnknownVerbLength);
    ok(req->RawUrlLength == 7, "Got raw URL length %u.\n", req->RawUrlLength);
    ok(!req->pUnknownVerb, "Got unknown verb %s.\n", req->pUnknownVerb);
    ok(!strcmp(req->pRawUrl, "/foobar"), "Got raw URL %s.\n", req->pRawUrl);
    ok(req->CookedUrl.FullUrlLength == 58, "Got full URL length %u.\n", req->CookedUrl.FullUrlLength);
    ok(req->CookedUrl.HostLength == 30, "Got host length %u.\n", req->CookedUrl.HostLength);
    ok(req->CookedUrl.AbsPathLength == 14, "Got absolute path length %u.\n", req->CookedUrl.AbsPathLength);
    ok(!req->CookedUrl.QueryStringLength, "Got query string length %u.\n", req->CookedUrl.QueryStringLength);
1325 1326 1327
    swprintf(url, ARRAY_SIZE(url), L"http://localhost:%u/foobar", port);
    ok(!wcscmp(req->CookedUrl.pFullUrl, url), "Expected full URL %s, got %s.\n",
            debugstr_w(url), debugstr_w(req->CookedUrl.pFullUrl));
1328 1329 1330 1331 1332 1333 1334
    ok(req->CookedUrl.pHost == req->CookedUrl.pFullUrl + 7, "Got host %s.\n", wine_dbgstr_w(req->CookedUrl.pHost));
    ok(req->CookedUrl.pAbsPath == req->CookedUrl.pFullUrl + 22,
            "Got absolute path %s.\n", wine_dbgstr_w(req->CookedUrl.pAbsPath));
    ok(!req->CookedUrl.pQueryString, "Got query string %s.\n", wine_dbgstr_w(req->CookedUrl.pQueryString));
    ok(!memcmp(req->Address.pRemoteAddress, &sockaddr, len), "Client addresses didn't match.\n");
    sin = (SOCKADDR_IN *)req->Address.pLocalAddress;
    ok(sin->sin_family == AF_INET, "Got family %u.\n", sin->sin_family);
1335
    ok(ntohs(sin->sin_port) == port, "Got wrong port %u.\n", ntohs(sin->sin_port));
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
    ok(sin->sin_addr.S_un.S_addr == inet_addr("127.0.0.1"), "Got address %08x.\n", sin->sin_addr.S_un.S_addr);
    ok(!req->Headers.UnknownHeaderCount, "Got %u unknown headers.\n", req->Headers.UnknownHeaderCount);
    ok(!req->Headers.pUnknownHeaders, "Got unknown headers %p.\n", req->Headers.pUnknownHeaders);
    for (i = 0; i < ARRAY_SIZE(req->Headers.KnownHeaders); ++i)
    {
        if (i == HttpHeaderConnection)
        {
            ok(req->Headers.KnownHeaders[i].RawValueLength == 10, "Got length %u.\n",
                    req->Headers.KnownHeaders[i].RawValueLength);
            ok(!strcmp(req->Headers.KnownHeaders[i].pRawValue, "keep-alive"),
                    "Got connection '%s'.\n", req->Headers.KnownHeaders[i].pRawValue);
        }
        else if (i == HttpHeaderHost)
        {
1350 1351 1352
            char expect[16];
            sprintf(expect, "localhost:%u", port);
            ok(req->Headers.KnownHeaders[i].RawValueLength == strlen(expect), "Got length %u.\n",
1353
                    req->Headers.KnownHeaders[i].RawValueLength);
1354
            ok(!strcmp(req->Headers.KnownHeaders[i].pRawValue, expect),
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
                    "Got connection '%s'.\n", req->Headers.KnownHeaders[i].pRawValue);
        }
        else if (i == HttpHeaderUserAgent)
        {
            ok(req->Headers.KnownHeaders[i].RawValueLength == 4, "Got length %u.\n",
                    req->Headers.KnownHeaders[i].RawValueLength);
            ok(!strcmp(req->Headers.KnownHeaders[i].pRawValue, "WINE"),
                    "Got connection '%s'.\n", req->Headers.KnownHeaders[i].pRawValue);
        }
        else
        {
            ok(!req->Headers.KnownHeaders[i].RawValueLength, "Header %#x: got length %u.\n",
                    i, req->Headers.KnownHeaders[i].RawValueLength);
            ok(!req->Headers.KnownHeaders[i].pRawValue, "Header %#x: got value '%s'.\n",
                    i, req->Headers.KnownHeaders[i].pRawValue);
        }
    }
1372
    ok(req->BytesReceived == strlen(req_text), "Got %s bytes.\n", wine_dbgstr_longlong(req->BytesReceived));
1373 1374
    ok(!req->EntityChunkCount, "Got %u entity chunks.\n", req->EntityChunkCount);
    ok(!req->pEntityChunks, "Got entity chunks %p.\n", req->pEntityChunks);
1375
    /* req->RawConnectionId is zero until Win10 2004. */
1376
    ok(!req->pSslInfo, "Got SSL info %p.\n", req->pSslInfo);
1377
    /* RequestInfoCount and pRequestInfo are zero until Win10 1909. */
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403

    response.s.StatusCode = 418;
    response.s.pReason = "I'm a teapot";
    response.s.ReasonLength = 12;
    response.s.Headers.KnownHeaders[HttpHeaderRetryAfter].pRawValue = "120";
    response.s.Headers.KnownHeaders[HttpHeaderRetryAfter].RawValueLength = 3;
    ret = HttpSendHttpResponse(queue, 0xdeadbeef, 0, (HTTP_RESPONSE *)&response, NULL, NULL, NULL, 0, &ovl, NULL);
    ok(ret == ERROR_CONNECTION_INVALID, "Got error %u.\n", ret);
    ret = HttpSendHttpResponse(queue, req->RequestId, 0, (HTTP_RESPONSE *)&response, NULL, NULL, NULL, 0, &ovl, NULL);
    ok(!ret, "Got error %u.\n", ret);
    ret = GetOverlappedResult(queue, &ovl, &ret_size, FALSE);
    ok(ret, "Got error %u.\n", GetLastError());

    ret = recv(s, response_buffer, sizeof(response_buffer), 0);
    ok(ret == ret_size, "Expected size %u, got %u.\n", ret_size, ret);

    if (winetest_debug > 1)
        trace("%.*s\n", ret, response_buffer);

    ok(!strncmp(response_buffer, "HTTP/1.1 418 I'm a teapot\r\n", 27), "Got incorrect status line.\n");
    ok(!!strstr(response_buffer, "\r\nRetry-After: 120\r\n"), "Missing or malformed Retry-After header.\n");
    ok(!!strstr(response_buffer, "\r\nDate:"), "Missing Date header.\n");

    ret = HttpReceiveHttpRequest(queue, req->RequestId, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), NULL, &ovl);
    ok(ret == ERROR_CONNECTION_INVALID, "Got error %u.\n", ret);

1404
    ret = remove_url_v2(group, port);
1405
    ok(!ret, "Got error %u.\n", ret);
1406
    ret = remove_url_v2(group, port);
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
    ok(ret == ERROR_FILE_NOT_FOUND, "Got error %u.\n", ret);

    closesocket(s);
    CloseHandle(ovl.hEvent);
    ret = pHttpCloseRequestQueue(queue);
    ok(!ret, "Failed to close queue handle, error %u.\n", ret);
    ret = pHttpCloseUrlGroup(group);
    ok(!ret, "Failed to close group, error %u.\n", ret);
    ret = pHttpCloseServerSession(session);
    ok(!ret, "Failed to close group, error %u.\n", ret);
}

static void test_v2_completion_port(void)
{
    char DECLSPEC_ALIGN(8) req_buffer[2048], response_buffer[2048];
    HTTP_REQUEST_V2 *req = (HTTP_REQUEST_V2 *)req_buffer;
    static const HTTPAPI_VERSION version = {2, 0};
    HTTP_SERVER_SESSION_ID session;
    HTTP_RESPONSE_V2 response = {};
    HTTP_BINDING_INFO binding;
    HTTP_URL_GROUP_ID group;
1428
    unsigned short tcp_port;
1429 1430
    OVERLAPPED ovl, *povl;
    HANDLE queue, port;
1431
    char req_text[100];
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
    DWORD ret_size;
    ULONG_PTR key;
    ULONG ret;
    SOCKET s;

    ovl.hEvent = CreateEventA(NULL, TRUE, FALSE, NULL);

    ret = pHttpCreateServerSession(version, &session, 0);
    ok(!ret, "Failed to create session, error %u.\n", ret);
    ret = pHttpCreateUrlGroup(session, &group, 0);
    ok(!ret, "Failed to create URL group, error %u.\n", ret);
    ret = pHttpCreateRequestQueue(version, NULL, NULL, 0, &queue);
    ok(!ret, "Failed to create request queue, error %u.\n", ret);
    binding.Flags.Present = 1;
    binding.RequestQueueHandle = queue;
    ret = pHttpSetUrlGroupProperty(group, HttpServerBindingProperty, &binding, sizeof(binding));
    ok(!ret, "Failed to bind request queue, error %u.\n", ret);

    port = CreateIoCompletionPort(queue, NULL, 123, 0);
    ok(!!port, "Failed to create completion port, error %u.\n", GetLastError());

    ret = GetQueuedCompletionStatus(port, &ret_size, &key, &povl, 0);
    ok(!ret, "Expected failure.\n");
    ok(GetLastError() == WAIT_TIMEOUT, "Got error %u.\n", GetLastError());

    ret = HttpReceiveHttpRequest(queue, HTTP_NULL_ID, 0, (HTTP_REQUEST *)req, sizeof(req_buffer), NULL, &ovl);
    ok(ret == ERROR_IO_PENDING, "Got error %u.\n", ret);

1460 1461
    tcp_port = add_url_v2(group);
    s = create_client_socket(tcp_port);
1462 1463 1464 1465 1466

    ret = GetQueuedCompletionStatus(port, &ret_size, &key, &povl, 0);
    ok(!ret, "Expected failure.\n");
    ok(GetLastError() == WAIT_TIMEOUT, "Got error %u.\n", GetLastError());

1467 1468 1469
    sprintf(req_text, simple_req, tcp_port);
    ret = send(s, req_text, strlen(req_text), 0);
    ok(ret == strlen(req_text), "send() returned %d.\n", ret);
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496

    ret_size = key = 0xdeadbeef;
    ret = GetQueuedCompletionStatus(port, &ret_size, &key, &povl, 1000);
    ok(ret, "Got error %u.\n", GetLastError());
    ok(povl == &ovl, "OVERLAPPED pointers didn't match.\n");
    ok(key == 123, "Got unexpected key %lu.\n", key);
    ok(ret_size > sizeof(*req), "Got size %u.\n", ret_size);

    ret = GetQueuedCompletionStatus(port, &ret_size, &key, &povl, 0);
    ok(!ret, "Expected failure.\n");
    ok(GetLastError() == WAIT_TIMEOUT, "Got error %u.\n", GetLastError());

    response.s.StatusCode = 418;
    response.s.pReason = "I'm a teapot";
    response.s.ReasonLength = 12;
    ret = HttpSendHttpResponse(queue, req->s.RequestId, 0, (HTTP_RESPONSE *)&response, NULL, NULL, NULL, 0, &ovl, NULL);
    ok(!ret, "Got error %u.\n", ret);

    ret_size = key = 0xdeadbeef;
    ret = GetQueuedCompletionStatus(port, &ret_size, &key, &povl, 1000);
    ok(ret, "Got error %u.\n", GetLastError());
    ok(povl == &ovl, "OVERLAPPED pointers didn't match.\n");
    ok(key == 123, "Got unexpected key %lu.\n", key);

    ret = recv(s, response_buffer, sizeof(response_buffer), 0);
    ok(ret == ret_size, "Expected size %u, got %u.\n", ret_size, ret);

1497
    ret = remove_url_v2(group, tcp_port);
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
    ok(!ret, "Got error %u.\n", ret);
    closesocket(s);
    CloseHandle(port);
    CloseHandle(ovl.hEvent);
    ret = pHttpCloseRequestQueue(queue);
    ok(!ret, "Failed to close queue handle, error %u.\n", ret);
    ret = pHttpCloseUrlGroup(group);
    ok(!ret, "Failed to close group, error %u.\n", ret);
    ret = pHttpCloseServerSession(session);
    ok(!ret, "Failed to close group, error %u.\n", ret);
}

1510 1511 1512
START_TEST(httpapi)
{
    HTTPAPI_VERSION version = { 1, 0 };
1513
    WSADATA wsadata;
1514 1515 1516 1517
    ULONG ret;

    init();

1518 1519
    WSAStartup(MAKEWORD(1,1), &wsadata);

1520 1521 1522
    ret = HttpInitialize(version, HTTP_INITIALIZE_SERVER, NULL);
    ok(!ret, "Failed to initialize library, ret %u.\n", ret);

1523 1524 1525 1526 1527 1528 1529 1530
    test_v1_server();
    test_v1_completion_port();
    test_v1_multiple_requests();
    test_v1_short_buffer();
    test_v1_entity_body();
    test_v1_bad_request();
    test_v1_cooked_url();
    test_v1_unknown_tokens();
1531
    test_v1_urls();
1532 1533 1534

    ret = HttpTerminate(HTTP_INITIALIZE_SERVER, NULL);
    ok(!ret, "Failed to terminate, ret %u.\n", ret);
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548

    version.HttpApiMajorVersion = 2;
    if (!HttpInitialize(version, HTTP_INITIALIZE_SERVER, NULL))
    {
        test_HttpCreateServerSession();
        test_HttpCreateUrlGroup();
        test_v2_server();
        test_v2_completion_port();

        ret = HttpTerminate(HTTP_INITIALIZE_SERVER, NULL);
        ok(!ret, "Failed to terminate, ret %u.\n", ret);
    }
    else
        win_skip("Version 2 is not supported.\n");
1549
}