CHttpFile::QueryInfo的BUG

MFC对WinINet封装时的BUG,导致无法完整查到的协议头中的字段。懒得上报,谁有空了帮我报告,感谢。

以下是代码:(注意特别注释部分)

BOOL CHttpFile::QueryInfo(DWORD dwInfoLevel, CString& str,
	LPDWORD lpdwIndex) const
{
	ASSERT(dwInfoLevel <= HTTP_QUERY_MAX);
	ASSERT_VALID(this);
	ASSERT(m_hFile != NULL);

	BOOL bRet;
	DWORD dwLen = 0;

	// ask for nothing to see how long the return really is

	str.Empty();
	if (HttpQueryInfo(m_hFile, dwInfoLevel, NULL, &dwLen, 0/* 这里应该传入 lpdwIndex,而非0*/))
		bRet = TRUE;
	else
	{
		// now that we know how long it is, ask for exactly that much
		// space and really request the header from the API

		LPTSTR pstr = str.GetBufferSetLength(dwLen / sizeof(TCHAR));
		bRet = HttpQueryInfo(m_hFile, dwInfoLevel, pstr, &dwLen, lpdwIndex);
		if (bRet)
			str.ReleaseBuffer(dwLen / sizeof(TCHAR));
		else
			str.ReleaseBuffer(0);
	}

	return bRet;
}这里应该传入 lpdwIndex,而非0*/))
		bRet = TRUE;
	else
	{
		// now that we know how long it is, ask for exactly that much
		// space and really request the header from the API

		LPTSTR pstr = str.GetBufferSetLength(dwLen / sizeof(TCHAR));
		bRet = HttpQueryInfo(m_hFile, dwInfoLevel, pstr, &dwLen, lpdwIndex);
		if (bRet)
			str.ReleaseBuffer(dwLen / sizeof(TCHAR));
		else
			str.ReleaseBuffer(0);
	}

	return bRet;
}

如何重现:

用以下串作为HTTP请求头:

GET https://www.baidu.com/ HTTP/1.1

Accept-Encoding: gzip; q=1.0

Accept-Encoding: defalte;q=0.5

Host: www.baidu.com

然后调用 CHttpFile::QueryInfo(假设 p指向一个有效的CHttpFile对象):

CString s;
DWORD dwIndex = 0;
while( p->QueryInfo( s, &dwIndex ) )
TRACE( _T("%s\n"), (LPCTSTR)s );

你会发现只输出了 gzip; q=1.0 ,而第2个Accept-encoding 的 defalte;q=0.5没有输出。因为CHttpFile::QueryInfo函数里第一次调用HttpQueryInfo时没有指示要获取第几个名称相同的属性,哇啦哇啦一大堆,原因看MSDN吧:

https://msdn.microsoft.com/en-us/library/aa384238(VS.85).aspx

以下是改正后的代码:

BOOL CHttpFile::QueryInfo(DWORD dwInfoLevel, CString& str,
	LPDWORD lpdwIndex) const
{
	ASSERT(dwInfoLevel <= HTTP_QUERY_MAX);
	ASSERT_VALID(this);
	ASSERT(m_hFile != NULL);

	BOOL bRet;
	DWORD dwLen = 0;

	// ask for nothing to see how long the return really is

	str.Empty();
	if( HttpQueryInfo( m_hFile, dwInfoLevel, NULL, &dwLen, lpdwIndex/*改正*/) )
		bRet = TRUE;
	else
	{
		// now that we know how long it is, ask for exactly that much
		// space and really request the header from the API

		LPTSTR pstr = str.GetBufferSetLength(dwLen / sizeof(TCHAR));
		bRet = HttpQueryInfo(m_hFile, dwInfoLevel, pstr, &dwLen, lpdwIndex);
		if (bRet)
			str.ReleaseBuffer(dwLen / sizeof(TCHAR));
		else
			str.ReleaseBuffer(0);
	}

	return bRet;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值