📄 inet.cpp
字号:
CString CHttpFile::GetVerb() const
{
ASSERT_VALID(this);
ASSERT(m_hFile != NULL);
return m_strVerb;
}
CString CHttpFile::GetObject() const
{
ASSERT_VALID(this);
ASSERT(m_hFile != NULL);
return m_strObject;
}
CString CHttpFile::GetFileURL() const
{
ASSERT_VALID(this);
ASSERT(m_hFile != NULL);
CString str(_afxURLhttp);
if (m_hConnection != NULL)
{
str += m_strServerName;
int nLen = m_strObject.GetLength();
if (nLen > 0)
{
if (m_strObject[0] != '/' && m_strObject[0] != '\\')
str += '/';
str += m_strObject;
}
}
return str;
}
BOOL CHttpFile::AddRequestHeaders(LPCTSTR pstrHeaders,
DWORD dwModifiers /* = HTTP_ADDREQ_FLAG_ADD */,
int dwHeadersLen /* = -1 */)
{
ASSERT(AfxIsValidString(pstrHeaders));
ASSERT(dwHeadersLen == 0 || pstrHeaders != NULL);
ASSERT_VALID(this);
ASSERT(m_hFile != NULL);
if (dwHeadersLen == -1)
if (pstrHeaders == NULL)
dwHeadersLen = 0;
else
dwHeadersLen = lstrlen(pstrHeaders);
return HttpAddRequestHeaders(m_hFile, pstrHeaders, dwHeadersLen,
dwModifiers);
}
BOOL CHttpFile::AddRequestHeaders(CString& str,
DWORD dwModifiers /* = HTTP_ADDREQ_FLAG_ADD */)
{
return AddRequestHeaders((LPCTSTR) str, dwModifiers, str.GetLength());
}
BOOL CHttpFile::SendRequest(LPCTSTR pstrHeaders /* = NULL */,
DWORD dwHeadersLen /* = 0 */, LPVOID lpOptional /* = NULL */,
DWORD dwOptionalLen /* = 0 */)
{
ASSERT(dwOptionalLen == 0 || lpOptional != NULL);
ASSERT(dwHeadersLen == 0 || pstrHeaders != NULL);
ASSERT_VALID(this);
ASSERT(m_hFile != NULL);
BOOL bRet = HttpSendRequest(m_hFile,
pstrHeaders, dwHeadersLen, lpOptional, dwOptionalLen);
if (!bRet)
AfxThrowInternetException(m_dwContext);
return bRet;
}
BOOL CHttpFile::EndRequest(
DWORD dwFlags /* = 0 */,
LPINTERNET_BUFFERS lpBuffIn /* = NULL */, DWORD dwContext /* = 1 */)
{
ASSERT(m_hFile != NULL);
ASSERT(m_bReadMode == -1);
if (dwContext == 1)
dwContext = m_dwContext;
BOOL bRet = HttpEndRequest(m_hFile, lpBuffIn, dwFlags, dwContext);
if (!bRet)
AfxThrowInternetException(m_dwContext);
return bRet;
}
BOOL CHttpFile::SendRequestEx(DWORD dwTotalLen,
DWORD dwFlags /* = HSR_INITIATE */, DWORD dwContext /* = 1 */)
{
ASSERT(m_hFile != NULL);
INTERNET_BUFFERS buffer;
memset(&buffer, 0, sizeof(buffer));
buffer.dwStructSize = sizeof(buffer);
buffer.dwBufferTotal = dwTotalLen;
if (dwContext == 1)
dwContext = m_dwContext;
return SendRequestEx(&buffer, NULL, dwFlags, dwContext);
}
BOOL CHttpFile::SendRequestEx(LPINTERNET_BUFFERS lpBuffIn,
LPINTERNET_BUFFERS lpBuffOut, DWORD dwFlags /* = HSR_INITIATE */,
DWORD dwContext /* = 1 */)
{
ASSERT(m_hFile != NULL);
ASSERT_NULL_OR_POINTER(lpBuffIn, INTERNET_BUFFERS);
ASSERT_NULL_OR_POINTER(lpBuffOut, INTERNET_BUFFERS);
if (dwContext == 1)
dwContext = m_dwContext;
BOOL bRet = HttpSendRequestEx(m_hFile, lpBuffIn, lpBuffOut,
dwFlags, dwContext);
if (!bRet)
AfxThrowInternetException(m_dwContext);
m_bReadMode = -1;
return bRet;
}
BOOL CHttpFile::SendRequest(CString& strHeaders,
LPVOID lpOptional /* = NULL */, DWORD dwOptionalLen /* = 0 */)
{
ASSERT(dwOptionalLen == 0 || lpOptional != NULL);
ASSERT_VALID(this);
ASSERT(m_hFile != NULL);
return SendRequest((LPCTSTR) strHeaders, strHeaders.GetLength(),
lpOptional, dwOptionalLen);
}
BOOL CHttpFile::QueryInfo(DWORD dwInfoLevel,
LPVOID lpvBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex) const
{
ASSERT((HTTP_QUERY_HEADER_MASK & dwInfoLevel) <= HTTP_QUERY_MAX &&
dwInfoLevel != 0);
ASSERT(lpvBuffer != NULL && *lpdwBufferLength > 0);
ASSERT_VALID(this);
ASSERT(m_hFile != NULL);
return HttpQueryInfo(m_hFile, dwInfoLevel, lpvBuffer,
lpdwBufferLength, lpdwIndex);
}
BOOL CHttpFile::QueryInfo(DWORD dwInfoLevel, DWORD& dwResult,
LPDWORD lpdwIndex /* = NULL */) const
{
dwInfoLevel |= HTTP_QUERY_FLAG_NUMBER;
DWORD dwDWSize = sizeof(DWORD);
return QueryInfo(dwInfoLevel, &dwResult, &dwDWSize, lpdwIndex);
}
BOOL CHttpFile::QueryInfo(DWORD dwInfoLevel, SYSTEMTIME* pSystemTime,
LPDWORD lpdwIndex /* = NULL */) const
{
dwInfoLevel |= HTTP_QUERY_FLAG_SYSTEMTIME;
DWORD dwTimeSize = sizeof(SYSTEMTIME);
return QueryInfo(dwInfoLevel, pSystemTime, &dwTimeSize, lpdwIndex);
}
BOOL CHttpFile::QueryInfoStatusCode(DWORD& dwStatusCode) const
{
ASSERT_VALID(this);
ASSERT(m_hFile != NULL);
TCHAR szBuffer[80];
DWORD dwLen = _countof(szBuffer);
BOOL bRet;
bRet = HttpQueryInfo(m_hFile, HTTP_QUERY_STATUS_CODE,
szBuffer, &dwLen, NULL);
if (bRet)
dwStatusCode = (DWORD) _ttol(szBuffer);
return bRet;
}
BOOL CHttpFile::QueryInfo(DWORD dwInfoLevel, CString& str,
LPDWORD lpdwIndex) const
{
ASSERT(dwInfoLevel <= HTTP_QUERY_MAX && dwInfoLevel >= 0);
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))
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);
bRet = HttpQueryInfo(m_hFile, dwInfoLevel, pstr, &dwLen, lpdwIndex);
if (bRet)
str.ReleaseBuffer(dwLen);
else
str.ReleaseBuffer(0);
}
return bRet;
}
#ifdef _DEBUG
void CHttpFile::Dump(CDumpContext& dc) const
{
dc << "\nm_strFileName = " << m_strFileName;
dc << "\nm_strVerb = " << m_strVerb;
}
void CHttpFile::AssertValid() const
{
CInternetFile::AssertValid();
}
#endif
/////////////////////////////////////////////////////////////////////////////
// CGopherFile
CGopherFile::CGopherFile(HINTERNET hFile, CGopherLocator& refLocator,
CGopherConnection* pConnection)
: CInternetFile(hFile, _T(""), pConnection, TRUE),
m_Locator(refLocator)
{
ASSERT(pConnection != NULL);
ASSERT_VALID(pConnection);
}
CGopherFile::CGopherFile(HINTERNET hFile, HINTERNET hSession,
LPCTSTR pstrLocator, DWORD dwLocLen, DWORD dwContext)
: CInternetFile(hFile, hSession, _T(""), _T(""), dwContext, TRUE),
m_Locator(pstrLocator, dwLocLen)
{
// caller muset set _afxSessionMap!
}
CGopherFile::~CGopherFile()
{
}
void CGopherFile::Close()
{
CInternetFile::Close();
}
void CGopherFile::Write(const void* lpBuf, UINT nCount)
{
UNUSED_ALWAYS(lpBuf);
UNUSED_ALWAYS(nCount);
ASSERT(FALSE);
AfxThrowNotSupportedException();
}
void CGopherFile::WriteString(LPCTSTR pstr)
{
UNUSED_ALWAYS(pstr);
ASSERT(FALSE);
AfxThrowNotSupportedException();
}
#ifdef _DEBUG
void CGopherFile::Dump(CDumpContext& dc) const
{
CInternetFile::Dump(dc);
}
void CGopherFile::AssertValid() const
{
CInternetFile::AssertValid();
}
#endif
/////////////////////////////////////////////////////////////////////////////
// CFtpFileFind
CFtpFileFind::CFtpFileFind(CFtpConnection* pConnection, DWORD dwContext)
{
ASSERT(pConnection != NULL);
ASSERT_KINDOF(CFtpConnection, pConnection);
m_pConnection = pConnection;
if (dwContext == 1)
dwContext = pConnection->GetContext();
m_dwContext = dwContext;
m_chDirSeparator = '/';
}
CFtpFileFind::~CFtpFileFind()
{
}
BOOL CFtpFileFind::FindFile(LPCTSTR pstrName /* = NULL */,
DWORD dwFlags /* = INTERNET_FLAG_RELOAD */)
{
ASSERT((dwFlags & INTERNET_FLAG_ASYNC) == 0);
ASSERT(m_pConnection != NULL);
ASSERT_VALID(m_pConnection);
if (m_pConnection == NULL)
return FALSE;
Close();
m_pNextInfo = new WIN32_FIND_DATA;
m_bGotLast = FALSE;
if (pstrName == NULL)
pstrName = _T("*");
lstrcpy(((LPWIN32_FIND_DATA) m_pNextInfo)->cFileName, pstrName);
m_hContext = FtpFindFirstFile((HINTERNET) *m_pConnection,
pstrName, (LPWIN32_FIND_DATA) m_pNextInfo, dwFlags, m_dwContext);
if (m_hContext == NULL)
{
Close();
return FALSE;
}
LPCTSTR pstrRoot = _tcspbrk(pstrName, _T("\\/"));
CString strCWD;
m_pConnection->GetCurrentDirectory(strCWD);
if (pstrRoot == NULL)
{
if (m_pConnection->SetCurrentDirectory(pstrName))
{
m_pConnection->GetCurrentDirectory(m_strRoot);
m_pConnection->SetCurrentDirectory(strCWD);
}
else
m_strRoot = strCWD;
}
else
{
// find the last forward or backward whack
int nLast;
LPCTSTR pstrOther = _tcsrchr(pstrName, '\\');
pstrRoot = _tcsrchr(pstrName, '/');
if (pstrRoot == NULL)
pstrRoot = pstrName;
if (pstrOther == NULL)
pstrOther = pstrName;
if (pstrRoot >= pstrOther)
nLast = pstrRoot - pstrName;
else
nLast = pstrOther - pstrName;
// from the start to the last whack is the root
if (nLast == 0)
nLast++;
m_strRoot = pstrName;
m_strRoot = m_strRoot.Left(nLast);
}
return TRUE;
}
BOOL CFtpFileFind::FindNextFile()
{
ASSERT(m_hContext != NULL);
if (m_hContext == NULL)
return FALSE;
if (m_pFoundInfo == NULL)
m_pFoundInfo = new WIN32_FIND_DATA;
ASSERT_VALID(this);
void* pTemp = m_pFoundInfo;
m_pFoundInfo = m_pNextInfo;
m_pNextInfo = pTemp;
return InternetFindNextFile(m_hContext, m_pNextInfo);
}
void CFtpFileFind::CloseContext()
{
if (m_hContext != NULL && m_hContext != INVALID_HANDLE_VALUE)
{
InternetCloseHandle(m_hContext);
m_hContext = NULL;
}
return;
}
CString CFtpFileFind::GetFileURL() const
{
ASSERT_VALID(this);
ASSERT(m_hContext != NULL);
CString str;
if (m_hContext != NULL)
{
str += _afxURLftp;
str += m_pConnection->GetServerName();
str += GetFilePath();
}
return str;
}
#ifdef _DEBUG
void CFtpFileFind::Dump(CDumpContext& dc) const
{
CFileFind::Dump(dc);
dc << "m_hContext = " << m_hContext;
}
void CFtpFileFind::AssertValid() const
{
CFileFind::AssertValid();
}
#endif
/////////////////////////////////////////////////////////////////////////////
// CGopherFileFind
CGopherFileFind::CGopherFileFind(CGopherConnection* pConnection,
DWORD dwContext)
{
ASSERT(pConnection != NULL);
ASSERT_KINDOF(CGopherConnection, pConnection);
m_pConnection = pConnection;
if (dwContext == 1)
dwContext = pConnection->GetContext();
m_dwContext = dwContext;
}
CGopherFileFind::~CGopherFileFind()
{
}
BOOL CGopherFileFind::FindFile(LPCTSTR pstrString,
DWORD dwFlags /* = INTERNET_FLAG_RELOAD */)
{
ASSERT((dwFlags & INTERNET_FLAG_ASYNC) == 0);
Close();
m_pNextInfo = new GOPHER_FIND_DATA;
m_bGotLast = FALSE;
m_hContext = GopherFindFirstFile((HINTERNET) *m_pConnection,
NULL, pstrString,
(GOPHER_FIND_DATA*) m_pNextInfo, dwFlags, m_dwContext);
if (m_hContext == NULL)
Close();
return (m_hContext != NULL);
}
BOOL CGopherFileFind::FindFile(CGopherLocator& refLocator,
LPCTSTR pstrString, DWORD dwFlags /* = INTERNET_FLAG_RELOAD */)
{
ASSERT((dwFlags & INTERNET_FLAG_ASYNC) == 0);
Close();
m_pNextInfo = new GOPHER_FIND_DATA;
m_pFoundInfo = new GOPHER_FIND_DATA;
m_bGotLast = FALSE;
m_hContext = GopherFindFirstFile((HINTERNET) *m_pConnection,
(LPCTSTR) refLocator, pstrString,
(GOPHER_FIND_DATA*) m_pNextInfo, dwFlags, m_dwContext);
if (m_hContext == NULL)
Close();
return (m_hContext != NULL);
}
BOOL CGopherFileFind::FindNextFile()
{
ASSERT(m_hContext != NULL);
if (m_hContext == NULL)
return FALSE;
if (m_pFoundInfo == NULL)
m_pFoundInfo = new GOPHER_FIND_DATA;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -