wininet: Move insertion of cookie header from HttpOpenRequest to HttpSendRequest.

Based on a patch by Yann Droneaud.
oldstable
Hans Leidekker 2008-07-19 19:55:39 +02:00 committed by Alexandre Julliard
parent b311cca552
commit 572b0bab88
2 changed files with 87 additions and 31 deletions

View File

@ -1888,15 +1888,10 @@ HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
{
LPWININETAPPINFOW hIC = NULL;
LPWININETHTTPREQW lpwhr;
LPWSTR lpszCookies;
LPWSTR lpszUrl = NULL;
LPWSTR lpszHostName = NULL;
DWORD nCookieSize;
HINTERNET handle = NULL;
static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
static const WCHAR szHostForm[] = {'%','s',':','%','u',0};
DWORD len;
LPHTTPHEADERW Host;
TRACE("-->\n");
@ -2008,32 +2003,6 @@ HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
Host = HTTP_GetHeader(lpwhr,szHost);
len = lstrlenW(Host->lpszValue) + strlenW(szUrlForm);
lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
sprintfW( lpszUrl, szUrlForm, Host->lpszValue );
if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
{
int cnt = 0;
static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
static const WCHAR szcrlf[] = {'\r','\n',0};
lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
cnt += sprintfW(lpszCookies, szCookie);
InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
strcatW(lpszCookies, szcrlf);
HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
HTTP_ADDREQ_FLAG_ADD);
HeapFree(GetProcessHeap(), 0, lpszCookies);
}
HeapFree(GetProcessHeap(), 0, lpszUrl);
INTERNET_SendCallback(&lpwhs->hdr, dwContext,
INTERNET_STATUS_HANDLE_CREATED, &handle,
sizeof(handle));
@ -3151,6 +3120,36 @@ static BOOL HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr)
return TRUE;
}
static void HTTP_InsertCookies(LPWININETHTTPREQW lpwhr)
{
static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
LPWSTR lpszCookies, lpszUrl = NULL;
DWORD nCookieSize, len;
LPHTTPHEADERW Host = HTTP_GetHeader(lpwhr,szHost);
len = lstrlenW(Host->lpszValue) + strlenW(szUrlForm);
lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
sprintfW( lpszUrl, szUrlForm, Host->lpszValue );
if (InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
{
int cnt = 0;
static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
static const WCHAR szcrlf[] = {'\r','\n',0};
lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
cnt += sprintfW(lpszCookies, szCookie);
InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
strcatW(lpszCookies, szcrlf);
HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
HTTP_ADDREQ_FLAG_ADD);
HeapFree(GetProcessHeap(), 0, lpszCookies);
}
HeapFree(GetProcessHeap(), 0, lpszUrl);
}
/***********************************************************************
* HTTP_HttpSendRequestW (internal)
*
@ -3240,6 +3239,9 @@ BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
HTTP_InsertAuthorization(lpwhr, lpwhr->pAuthInfo, szAuthorization);
HTTP_InsertAuthorization(lpwhr, lpwhr->pProxyAuthInfo, szProxy_Authorization);
if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES))
HTTP_InsertCookies(lpwhr);
/* add the headers the caller supplied */
if( lpszHeaders && dwHeaderLength )
{

View File

@ -1489,6 +1489,13 @@ static DWORD CALLBACK server_thread(LPVOID param)
recvfrom(c, buffer, sizeof buffer, 0, NULL, NULL);
send(c, okmsg, sizeof okmsg-1, 0);
}
if (strstr(buffer, "/testC"))
{
if (strstr(buffer, "Cookie: cookie=biscuit"))
send(c, okmsg, sizeof okmsg-1, 0);
else
send(c, notokmsg, sizeof notokmsg-1, 0);
}
if (strstr(buffer, "GET /quit"))
{
send(c, okmsg, sizeof okmsg-1, 0);
@ -1815,6 +1822,52 @@ static void test_http1_1(int port)
InternetCloseHandle(ses);
}
static void test_cookie_header(int port)
{
HINTERNET ses, con, req;
DWORD size, status, error;
BOOL ret;
char buffer[64];
ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
ok(ses != NULL, "InternetOpen failed\n");
con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
ok(con != NULL, "InternetConnect failed\n");
InternetSetCookie("http://localhost", "cookie", "biscuit");
req = HttpOpenRequest(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
ok(req != NULL, "HttpOpenRequest failed\n");
buffer[0] = 0;
size = sizeof(buffer);
SetLastError(0xdeadbeef);
ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
error = GetLastError();
ok(!ret, "HttpQueryInfo succeeded\n");
ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %u expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
ret = HttpSendRequest(req, NULL, 0, NULL, 0);
ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
status = 0;
size = sizeof(status);
ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
ok(ret, "HttpQueryInfo failed\n");
ok(status == 200, "request failed with status %u\n", status);
buffer[0] = 0;
size = sizeof(buffer);
ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
ok(!strcmp(buffer, "cookie=biscuit"), "got '%s' expected \'cookie=biscuit\'\n", buffer);
InternetCloseHandle(req);
InternetCloseHandle(con);
InternetCloseHandle(ses);
}
static void test_http_connection(void)
{
struct server_info si;
@ -1840,6 +1893,7 @@ static void test_http_connection(void)
test_basic_request(si.port, "GET", "/test6");
test_connection_header(si.port);
test_http1_1(si.port);
test_cookie_header(si.port);
/* send the basic request again to shutdown the server thread */
test_basic_request(si.port, "GET", "/quit");