winhttp: Fix handling of the headers length parameter in WinHttpAddRequestHeaders and WinHttpSendRequest.

This commit is contained in:
Hans Leidekker 2015-06-17 15:11:50 +02:00 committed by Alexandre Julliard
parent d9d8e6f031
commit 9e96beefae
2 changed files with 41 additions and 7 deletions

View File

@ -486,7 +486,8 @@ BOOL add_request_headers( request_t *request, LPCWSTR headers, DWORD len, DWORD
if (len == ~0u) len = strlenW( headers ); if (len == ~0u) len = strlenW( headers );
if (!len) return TRUE; if (!len) return TRUE;
if (!(buffer = heap_alloc( (len + 1) * sizeof(WCHAR) ))) return FALSE; if (!(buffer = heap_alloc( (len + 1) * sizeof(WCHAR) ))) return FALSE;
strcpyW( buffer, headers ); memcpy( buffer, headers, len * sizeof(WCHAR) );
buffer[len] = 0;
p = buffer; p = buffer;
do do
@ -530,7 +531,7 @@ BOOL WINAPI WinHttpAddRequestHeaders( HINTERNET hrequest, LPCWSTR headers, DWORD
TRACE("%p, %s, 0x%x, 0x%08x\n", hrequest, debugstr_w(headers), len, flags); TRACE("%p, %s, 0x%x, 0x%08x\n", hrequest, debugstr_w(headers), len, flags);
if (!headers) if (!headers || !len)
{ {
set_last_error( ERROR_INVALID_PARAMETER ); set_last_error( ERROR_INVALID_PARAMETER );
return FALSE; return FALSE;
@ -1251,6 +1252,8 @@ BOOL WINAPI WinHttpSendRequest( HINTERNET hrequest, LPCWSTR headers, DWORD heade
return FALSE; return FALSE;
} }
if (headers && !headers_len) headers_len = strlenW( headers );
if (request->connect->hdr.flags & WINHTTP_FLAG_ASYNC) if (request->connect->hdr.flags & WINHTTP_FLAG_ASYNC)
{ {
send_request_t *s; send_request_t *s;

View File

@ -410,7 +410,7 @@ static void test_WinHttpAddHeaders(void)
BOOL ret, reverse; BOOL ret, reverse;
WCHAR buffer[MAX_PATH]; WCHAR buffer[MAX_PATH];
WCHAR check_buffer[MAX_PATH]; WCHAR check_buffer[MAX_PATH];
DWORD index, len, oldlen; DWORD err, index, len, oldlen;
static const WCHAR test_file[] = {'/','p','o','s','t','t','e','s','t','.','p','h','p',0}; static const WCHAR test_file[] = {'/','p','o','s','t','t','e','s','t','.','p','h','p',0};
static const WCHAR test_verb[] = {'P','O','S','T',0}; static const WCHAR test_verb[] = {'P','O','S','T',0};
@ -420,6 +420,9 @@ static void test_WinHttpAddHeaders(void)
{'P','O','S','T',' ','h','t','t','p',':','/','/','t','e','s','t','.','w','i','n','e','h','q','.','o','r','g',':','8','0','/','p','o','s','t','.','p','h','p',' ','H','T','T','P','/','1'}; {'P','O','S','T',' ','h','t','t','p',':','/','/','t','e','s','t','.','w','i','n','e','h','q','.','o','r','g',':','8','0','/','p','o','s','t','.','p','h','p',' ','H','T','T','P','/','1'};
static const WCHAR test_header_end[] = {'\r','\n','\r','\n',0}; static const WCHAR test_header_end[] = {'\r','\n','\r','\n',0};
static const WCHAR test_header_name[] = {'W','a','r','n','i','n','g',0}; static const WCHAR test_header_name[] = {'W','a','r','n','i','n','g',0};
static const WCHAR test_header_range[] = {'R','a','n','g','e',0};
static const WCHAR test_header_range_bytes[] = {'R','a','n','g','e',':',' ','b','y','t','e','s','=','0','-','7','7','3','\r','\n',0};
static const WCHAR test_header_bytes[] = {'b','y','t','e','s','=','0','-','7','7','3',0};
static const WCHAR test_flag_coalesce[] = {'t','e','s','t','2',',',' ','t','e','s','t','4',0}; static const WCHAR test_flag_coalesce[] = {'t','e','s','t','2',',',' ','t','e','s','t','4',0};
static const WCHAR test_flag_coalesce_reverse[] = {'t','e','s','t','3',',',' ','t','e','s','t','4',0}; static const WCHAR test_flag_coalesce_reverse[] = {'t','e','s','t','3',',',' ','t','e','s','t','4',0};
@ -780,6 +783,26 @@ static void test_WinHttpAddHeaders(void)
ok(ret, "WinHttpQueryHeaders failed: %u\n", GetLastError()); ok(ret, "WinHttpQueryHeaders failed: %u\n", GetLastError());
ok(!memcmp(buffer, value, sizeof(value)) || ! memcmp(buffer, value_nospace, sizeof(value_nospace)), "unexpected result\n"); ok(!memcmp(buffer, value, sizeof(value)) || ! memcmp(buffer, value_nospace, sizeof(value_nospace)), "unexpected result\n");
SetLastError(0xdeadbeef);
ret = WinHttpAddRequestHeaders(request, test_header_range_bytes, 0,
WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE);
err = GetLastError();
ok(!ret, "unexpected success\n");
ok(err == ERROR_INVALID_PARAMETER, "got %u\n", err);
ret = WinHttpAddRequestHeaders(request, test_header_range_bytes, ~0u,
WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE);
ok(ret, "failed to add header: %u\n", GetLastError());
index = 0;
len = sizeof(buffer);
ret = WinHttpQueryHeaders(request, WINHTTP_QUERY_CUSTOM | WINHTTP_QUERY_FLAG_REQUEST_HEADERS,
test_header_range, buffer, &len, &index);
ok(ret, "failed to get range header %u\n", GetLastError());
ok(!memcmp(buffer, test_header_bytes, sizeof(test_header_bytes)), "incorrect string returned\n");
ok(len == lstrlenW(test_header_bytes) * sizeof(WCHAR), "wrong length %u\n", len);
ok(index == 1, "wrong index %u\n", index);
ret = WinHttpCloseHandle(request); ret = WinHttpCloseHandle(request);
ok(ret == TRUE, "WinHttpCloseHandle failed on closing request, got %d.\n", ret); ok(ret == TRUE, "WinHttpCloseHandle failed on closing request, got %d.\n", ret);
done: done:
@ -1923,7 +1946,8 @@ static DWORD CALLBACK server_thread(LPVOID param)
} }
if (strstr(buffer, "GET /not_modified")) if (strstr(buffer, "GET /not_modified"))
{ {
send(c, notmodified, sizeof notmodified - 1, 0); if (strstr(buffer, "If-Modified-Since:")) send(c, notmodified, sizeof notmodified - 1, 0);
else send(c, notokmsg, sizeof(notokmsg) - 1, 0);
continue; continue;
} }
if (strstr(buffer, "HEAD /head")) if (strstr(buffer, "HEAD /head"))
@ -2395,11 +2419,12 @@ static void test_not_modified(int port)
{ {
static const WCHAR pathW[] = {'/','n','o','t','_','m','o','d','i','f','i','e','d',0}; static const WCHAR pathW[] = {'/','n','o','t','_','m','o','d','i','f','i','e','d',0};
static const WCHAR ifmodifiedW[] = {'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',':',' '}; static const WCHAR ifmodifiedW[] = {'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',':',' '};
static const WCHAR ifmodified2W[] = {'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0};
BOOL ret; BOOL ret;
HINTERNET session, request, connection; HINTERNET session, request, connection;
DWORD status, size, start = GetTickCount(); DWORD index, len, status, size, start = GetTickCount();
SYSTEMTIME st; SYSTEMTIME st;
WCHAR today[(sizeof(ifmodifiedW) + WINHTTP_TIME_FORMAT_BUFSIZE)/sizeof(WCHAR) + 3]; WCHAR today[(sizeof(ifmodifiedW) + WINHTTP_TIME_FORMAT_BUFSIZE)/sizeof(WCHAR) + 3], buffer[32];
memcpy(today, ifmodifiedW, sizeof(ifmodifiedW)); memcpy(today, ifmodifiedW, sizeof(ifmodifiedW));
GetSystemTime(&st); GetSystemTime(&st);
@ -2416,12 +2441,18 @@ static void test_not_modified(int port)
WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_BYPASS_PROXY_CACHE); WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_BYPASS_PROXY_CACHE);
ok(request != NULL, "WinHttpOpenrequest failed: %u\n", GetLastError()); ok(request != NULL, "WinHttpOpenrequest failed: %u\n", GetLastError());
ret = WinHttpSendRequest(request, today, ~0u, NULL, 0, 0, 0); ret = WinHttpSendRequest(request, today, 0, NULL, 0, 0, 0);
ok(ret, "WinHttpSendRequest failed: %u\n", GetLastError()); ok(ret, "WinHttpSendRequest failed: %u\n", GetLastError());
ret = WinHttpReceiveResponse(request, NULL); ret = WinHttpReceiveResponse(request, NULL);
ok(ret, "WinHttpReceiveResponse failed: %u\n", GetLastError()); ok(ret, "WinHttpReceiveResponse failed: %u\n", GetLastError());
index = 0;
len = sizeof(buffer);
ret = WinHttpQueryHeaders(request, WINHTTP_QUERY_CUSTOM | WINHTTP_QUERY_FLAG_REQUEST_HEADERS,
ifmodified2W, buffer, &len, &index);
ok(ret, "failed to get header %u\n", GetLastError());
status = 0xdeadbeef; status = 0xdeadbeef;
size = sizeof(status); size = sizeof(status);
ret = WinHttpQueryHeaders(request, WINHTTP_QUERY_STATUS_CODE|WINHTTP_QUERY_FLAG_NUMBER, ret = WinHttpQueryHeaders(request, WINHTTP_QUERY_STATUS_CODE|WINHTTP_QUERY_FLAG_NUMBER,