winhttp: Make WinHttpQueryHeaders behave the same way for all query types.

Based on a patch by Mikko Rasa.
This commit is contained in:
Alexandre Julliard 2010-08-20 16:06:04 +02:00
parent bfdfae1c7f
commit 93208196c8
1 changed files with 29 additions and 36 deletions

View File

@ -602,12 +602,12 @@ static BOOL query_headers( request_t *request, DWORD level, LPCWSTR name, LPVOID
if (!(p = headers)) return FALSE; if (!(p = headers)) return FALSE;
for (len = 0; *p; p++) if (*p != '\r') len++; for (len = 0; *p; p++) if (*p != '\r') len++;
if ((len + 1) * sizeof(WCHAR) > *buflen || !buffer) if (!buffer || (len + 1) * sizeof(WCHAR) > *buflen)
{ {
len++; len++;
set_last_error( ERROR_INSUFFICIENT_BUFFER ); set_last_error( ERROR_INSUFFICIENT_BUFFER );
} }
else if (buffer) else
{ {
for (p = headers, q = buffer; *p; p++, q++) for (p = headers, q = buffer; *p; p++, q++)
{ {
@ -637,12 +637,12 @@ static BOOL query_headers( request_t *request, DWORD level, LPCWSTR name, LPVOID
if (!headers) return FALSE; if (!headers) return FALSE;
len = strlenW( headers ) * sizeof(WCHAR); len = strlenW( headers ) * sizeof(WCHAR);
if (len + sizeof(WCHAR) > *buflen || !buffer) if (!buffer || len + sizeof(WCHAR) > *buflen)
{ {
len += sizeof(WCHAR); len += sizeof(WCHAR);
set_last_error( ERROR_INSUFFICIENT_BUFFER ); set_last_error( ERROR_INSUFFICIENT_BUFFER );
} }
else if (buffer) else
{ {
memcpy( buffer, headers, len + sizeof(WCHAR) ); memcpy( buffer, headers, len + sizeof(WCHAR) );
TRACE("returning data: %s\n", debugstr_wn(buffer, len / sizeof(WCHAR))); TRACE("returning data: %s\n", debugstr_wn(buffer, len / sizeof(WCHAR)));
@ -653,43 +653,38 @@ static BOOL query_headers( request_t *request, DWORD level, LPCWSTR name, LPVOID
return ret; return ret;
} }
case WINHTTP_QUERY_VERSION: case WINHTTP_QUERY_VERSION:
{ len = strlenW( request->version ) * sizeof(WCHAR);
DWORD len = (strlenW( request->version ) + 1) * sizeof(WCHAR); if (!buffer || len + sizeof(WCHAR) > *buflen)
if (len > *buflen)
{ {
len += sizeof(WCHAR);
set_last_error( ERROR_INSUFFICIENT_BUFFER ); set_last_error( ERROR_INSUFFICIENT_BUFFER );
*buflen = len;
return FALSE;
} }
else if (buffer) else
{ {
strcpyW( buffer, request->version ); strcpyW( buffer, request->version );
TRACE("returning string: %s\n", debugstr_w(buffer)); TRACE("returning string: %s\n", debugstr_w(buffer));
ret = TRUE; ret = TRUE;
} }
*buflen = len - sizeof(WCHAR); *buflen = len;
return ret; return ret;
}
case WINHTTP_QUERY_STATUS_TEXT: case WINHTTP_QUERY_STATUS_TEXT:
{ len = strlenW( request->status_text ) * sizeof(WCHAR);
DWORD len = (strlenW( request->status_text ) + 1) * sizeof(WCHAR); if (!buffer || len + sizeof(WCHAR) > *buflen)
if (len > *buflen)
{ {
len += sizeof(WCHAR);
set_last_error( ERROR_INSUFFICIENT_BUFFER ); set_last_error( ERROR_INSUFFICIENT_BUFFER );
*buflen = len;
return FALSE;
} }
else if (buffer) else
{ {
strcpyW( buffer, request->status_text ); strcpyW( buffer, request->status_text );
TRACE("returning string: %s\n", debugstr_w(buffer)); TRACE("returning string: %s\n", debugstr_w(buffer));
ret = TRUE; ret = TRUE;
} }
*buflen = len - sizeof(WCHAR); *buflen = len;
return ret; return ret;
}
default: default:
{
if (attr >= sizeof(attribute_table)/sizeof(attribute_table[0]) || !attribute_table[attr]) if (attr >= sizeof(attribute_table)/sizeof(attribute_table[0]) || !attribute_table[attr])
{ {
FIXME("attribute %u not implemented\n", attr); FIXME("attribute %u not implemented\n", attr);
@ -697,7 +692,7 @@ static BOOL query_headers( request_t *request, DWORD level, LPCWSTR name, LPVOID
} }
TRACE("attribute %s\n", debugstr_w(attribute_table[attr])); TRACE("attribute %s\n", debugstr_w(attribute_table[attr]));
header_index = get_header_index( request, attribute_table[attr], requested_index, request_only ); header_index = get_header_index( request, attribute_table[attr], requested_index, request_only );
} break;
} }
if (header_index >= 0) if (header_index >= 0)
@ -712,13 +707,13 @@ static BOOL query_headers( request_t *request, DWORD level, LPCWSTR name, LPVOID
if (index) *index += 1; if (index) *index += 1;
if (level & WINHTTP_QUERY_FLAG_NUMBER) if (level & WINHTTP_QUERY_FLAG_NUMBER)
{ {
int *number = buffer; if (!buffer || sizeof(int) > *buflen)
if (sizeof(int) > *buflen)
{ {
set_last_error( ERROR_INSUFFICIENT_BUFFER ); set_last_error( ERROR_INSUFFICIENT_BUFFER );
} }
else if (number) else
{ {
int *number = buffer;
*number = atoiW( header->value ); *number = atoiW( header->value );
TRACE("returning number: %d\n", *number); TRACE("returning number: %d\n", *number);
ret = TRUE; ret = TRUE;
@ -728,11 +723,11 @@ static BOOL query_headers( request_t *request, DWORD level, LPCWSTR name, LPVOID
else if (level & WINHTTP_QUERY_FLAG_SYSTEMTIME) else if (level & WINHTTP_QUERY_FLAG_SYSTEMTIME)
{ {
SYSTEMTIME *st = buffer; SYSTEMTIME *st = buffer;
if (sizeof(SYSTEMTIME) > *buflen) if (!buffer || sizeof(SYSTEMTIME) > *buflen)
{ {
set_last_error( ERROR_INSUFFICIENT_BUFFER ); set_last_error( ERROR_INSUFFICIENT_BUFFER );
} }
else if (st && (ret = WinHttpTimeToSystemTime( header->value, st ))) else if ((ret = WinHttpTimeToSystemTime( header->value, st )))
{ {
TRACE("returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n", TRACE("returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
st->wYear, st->wMonth, st->wDay, st->wDayOfWeek, st->wYear, st->wMonth, st->wDay, st->wDayOfWeek,
@ -742,21 +737,19 @@ static BOOL query_headers( request_t *request, DWORD level, LPCWSTR name, LPVOID
} }
else if (header->value) else if (header->value)
{ {
WCHAR *string = buffer; len = strlenW( header->value ) * sizeof(WCHAR);
DWORD len = (strlenW( header->value ) + 1) * sizeof(WCHAR); if (!buffer || len + sizeof(WCHAR) > *buflen)
if (len > *buflen)
{ {
len += sizeof(WCHAR);
set_last_error( ERROR_INSUFFICIENT_BUFFER ); set_last_error( ERROR_INSUFFICIENT_BUFFER );
*buflen = len;
return FALSE;
} }
else if (string) else
{ {
strcpyW( string, header->value ); strcpyW( buffer, header->value );
TRACE("returning string: %s\n", debugstr_w(string)); TRACE("returning string: %s\n", debugstr_w(buffer));
ret = TRUE; ret = TRUE;
} }
*buflen = len - sizeof(WCHAR); *buflen = len;
} }
return ret; return ret;
} }