?? myhttpclient.cpp
字號:
#include "stdafx.h"
#include "MyHttpClient.h"
#pragma warning (disable: 4290) // avoids 'C++ Exception Specification ignored' message
#pragma warning (disable: 4660)
#pragma warning (disable: 4996) // avoids 'This function or variable may be unsafe' message
#pragma comment (lib, "wininet.lib")
#pragma comment (lib, "urlmon.lib")
/*!
* \brief The namespace of the Ryeol's library
*
* This is the namespace for source codes written by Jo Hyeong-ryeol.
*/
namespace Ryeol {
/////////////////////////////// Global constant message table ////////////////////////////////////
#ifndef SAFEFREE
# define SAFEFREE(x) if(x){ ::free((void *)x); x = NULL; }
#endif
#ifndef INVALID_SET_FILE_POINTER
# define INVALID_SET_FILE_POINTER ((DWORD)-1)
#endif
#define HTTPCLIENT_POSTCNTX_BUFF_SIZE (1024 * 56)
// An assertion macro for the CHttpToolA and the CHttpToolW class
#define HTTPTOOL_ASSERT(expr, msg) \
\
if ( !(expr) ) { \
_ASSERTE ( expr ) ; ThrowException (msg) ; \
}
// An assertion macro for the CHttpClient related classes
#define HTTPCLIENT_ASSERT(expr, msg) \
\
if ( !(expr) ) { \
_ASSERTE ( expr ) ; \
if ( HttpTool::IsAnsi () ) \
CHttpToolA::ThrowException (msg) ; \
else \
CHttpToolW::ThrowException (L##msg) ; \
}
// An assertion macro for the CHttpClient related classes
#define HTTPCLIENT_ASSERTA(expr, msg) \
\
if ( !(expr) ) { \
_ASSERTE ( expr ) ; \
CHttpToolA::ThrowException (msg) ; \
}
// An assertion macro for the CHttpClient related classes
#define HTTPCLIENT_ASSERTW(expr, msg) \
\
if ( !(expr) ) { \
_ASSERTE ( expr ) ; \
CHttpToolW::ThrowException (L##msg) ; \
}
// not specified
static LPCSTR g_NotSpecifiedA[] = {
"Not specified"
} ;
static LPCWSTR g_NotSpecifiedW[] = {
L"Not specified"
} ;
// error messages
static LPCSTR g_NormalMsgA[] = {
"Unexpected error occurred."
, "The index is out of range."
, "Out of memory."
, "The requested URL is not a valid URL."
, "The post context is not started yet."
, "Couldn't read expected bytes from a file."
, "The post context has not been finished yet."
, "The port number is not valid."
, "std::exception occurred."
, "The encoded URL is not valid."
, "The UTF8 string contains an invalid character."
, "An unexpected arithmetic error has been occurred."
, "An arithmetic overflow error has been occurred."
, "An interger divide by zero exception has been occurred."
, "The file (%s) aleady exists."
} ;
static LPCWSTR g_NormalMsgW[] = {
L"Unexpected error occurred."
, L"The index is out of range."
, L"Out of memory."
, L"The requested URL is not a valid URL."
, L"The post context is not started yet."
, L"Couldn't read expected bytes from a file."
, L"The post context has not been finished yet."
, L"The port number is not valid."
, L"std::exception occurred."
, L"The encoded URL is not valid."
, L"The UTF8 string contains an invalid character."
, L"An unexpected arithmetic error has been occurred."
, L"An arithmetic overflow error has been occurred."
, L"An interger divide by zero exception has been occurred."
, L"The file (%s) aleady exists."
} ;
// error messages (which has a win32 error code) - Reserved
static LPCSTR g_NormalMsgWin32A[] = {""} ;
static LPCWSTR g_NormalMsgWin32W[] = {L""} ;
// WinInet error messages (which has a win32 error code)
static LPCSTR g_WinInetMsgWin32A[] = {
"::HttpQueryInfo failed."
, "::InternetReadFile failed."
, "::InternetOpen failed."
, "::InternetConnect failed."
, "::HttpOpenRequest failed."
, "::HttpAddRequestHeaders failed."
, "::HttpSendRequest failed."
, "::HttpSendRequestEx failed."
, "::InternetWriteFile failed."
, "::HttpEndRequest failed."
, "::InternetSetOption failed."
} ;
static LPCWSTR g_WinInetMsgWin32W[] = {
L"::HttpQueryInfo failed."
, L"::InternetReadFile failed."
, L"::InternetOpen failed."
, L"::InternetConnect failed."
, L"::HttpOpenRequest failed."
, L"::HttpAddRequestHeaders failed."
, L"::HttpSendRequest failed."
, L"::HttpSendRequestEx failed."
, L"::InternetWriteFile failed."
, L"::HttpEndRequest failed."
, L"::InternetSetOption failed."
} ;
// Win32 API error messages except the WinInet API (They have a win32 error code)
static LPCSTR g_Win32MsgWin32A[] = {
"::WideCharToMultiByte failed."
, "::MultiByteToWideChar failed."
, "::ReadFile failed."
, "OpenFile (::CreateFile) failed (\"%s\")."
, "::SetFilePointer failed."
, "::GetFileSize failed (\"%s\")."
, "::WriteFile failed (\"%s\")."
} ;
static LPCWSTR g_Win32MsgWin32W[] = {
L"::WideCharToMultiByte failed."
, L"::MultiByteToWideChar failed."
, L"::ReadFile failed."
, L"OpenFile (::CreateFile) failed (\"%s\")."
, L"::SetFilePointer failed."
, L"::GetFileSize failed (\"%s\")."
, L"::WriteFile failed (\"%s\")."
} ;
// user-defined error message
static LPCSTR g_UsrErrMsgA[] = {
"user-defined error message"
} ;
static LPCWSTR g_UsrErrMsgW[] = {
L"user-defined error message"
} ;
static LPCSTR HTTPCLIENT_DEF_MIMETYPE = "application/octet-stream" ;
// Constants in the CHttpToolT
LPCSTR CHttpToolA::szDefUsrAgent = "Ryeol HTTP Client Class" ;
LPCSTR CHttpToolA::szGET = "GET" ;
LPCSTR CHttpToolA::szPost = "POST" ;
LPCSTR CHttpToolA::szHTTP = "HTTP://" ;
LPCSTR CHttpToolA::szHTTPS = "HTTPS://" ;
LPCSTR CHttpToolA::szSlash = "/" ;
LPCSTR CHttpToolA::szCacheControl = "Cache-Control" ;
LPCSTR CHttpToolA::szNoCache = "no-cache" ;
LPCSTR CHttpToolA::szContentType = "Content-Type" ;
LPCSTR CHttpToolA::szMultipartFormDataBoundary = "multipart/form-data; boundary=" ;
LPCSTR CHttpToolA::szFormUrlEncoded = "application/x-www-form-urlencoded" ;
LPCSTR CHttpToolA::szDefBoundary = "----RYEOL-FB3B405B7EAE495aB0C0295C54D4E096-" ;
LPCSTR CHttpToolA::szDefUploadContType = "multipart/form-data; boundary=" "----RYEOL-FB3B405B7EAE495aB0C0295C54D4E096-" ;
LPCSTR CHttpToolA::szNULL = "NULL" ;
LPCSTR CHttpToolA::szEmptyString = "" ;
LPCSTR CHttpToolA::szColonSlashSlash = "://" ;
LPCWSTR CHttpToolW::szDefUsrAgent = L"Ryeol HTTP Client Class" ;
LPCWSTR CHttpToolW::szGET = L"GET" ;
LPCWSTR CHttpToolW::szPost = L"POST" ;
LPCWSTR CHttpToolW::szHTTP = L"HTTP://" ;
LPCWSTR CHttpToolW::szHTTPS = L"HTTPS://" ;
LPCWSTR CHttpToolW::szSlash = L"/" ;
LPCWSTR CHttpToolW::szCacheControl = L"Cache-Control" ;
LPCWSTR CHttpToolW::szNoCache = L"no-cache" ;
LPCWSTR CHttpToolW::szContentType = L"Content-Type" ;
LPCWSTR CHttpToolW::szMultipartFormDataBoundary = L"multipart/form-data; boundary=" ;
LPCWSTR CHttpToolW::szFormUrlEncoded = L"application/x-www-form-urlencoded" ;
LPCWSTR CHttpToolW::szDefBoundary = L"----RYEOL-FB3B405B7EAE495aB0C0295C54D4E096-" ;
LPCWSTR CHttpToolW::szDefUploadContType = L"multipart/form-data; boundary=" L"----RYEOL-FB3B405B7EAE495aB0C0295C54D4E096-" ;
LPCWSTR CHttpToolW::szNULL = L"NULL" ;
LPCWSTR CHttpToolW::szEmptyString = L"" ;
LPCWSTR CHttpToolW::szColonSlashSlash = L"://" ;
/////////////////////////////// Global constant message table ////////////////////////////////////
///////////////////////////////////////// Explicit Instanciation /////////////////////////////////////////
template class CHttpClientMapT<CHttpToolA> ;
template class CHttpClientMapT<CHttpToolW> ;
template class CHttpResponseT<CHttpToolA> ;
template class CHttpResponseT<CHttpToolW> ;
template class CHttpPostStatT<CHttpToolA> ;
template class CHttpPostStatT<CHttpToolW> ;
template class CHttpUrlAnalyzerT<CHttpToolA> ;
template class CHttpUrlAnalyzerT<CHttpToolW> ;
template class CHttpClientT<CHttpToolA, CHttpEncoderA> ;
template class CHttpClientT<CHttpToolW, CHttpEncoderW> ;
///////////////////////////////////////// Explicit Instanciation /////////////////////////////////////////
///////////////////////////////////////// httpclientexception /////////////////////////////////////////
/*!
* This is a default constructor with no argument.
*/
httpclientexceptionA::httpclientexceptionA (void)
throw ()
{
m_dwLastError = HTTPCLIENT_ERR_NOT_SPECIFIED ;
m_dwWin32LastError = NO_ERROR ;
}
/*!
* This is a constructor with an initial error message and initial error codes.
* If memory allocation failed for the error message, the error message will not be copied
* and Internal error message will point to NULL.
*
* \param szErrMsg [in] An initial error message.
* \param dwLastError [in] An error code.
* \param dwWin32LastError [in] A win32 error code.
*/
httpclientexceptionA::httpclientexceptionA (LPCSTR szErrMsg, DWORD dwLastError, DWORD dwWin32LastError)
throw ()
: errmsg_exceptionA (szErrMsg)
{
m_dwLastError = dwLastError ;
m_dwWin32LastError = dwWin32LastError ;
}
/*!
* This is a default constructor with no argument.
*/
httpclientexceptionW::httpclientexceptionW (void)
throw ()
{
m_dwLastError = HTTPCLIENT_ERR_NOT_SPECIFIED ;
m_dwWin32LastError = NO_ERROR ;
}
/*!
* This is a constructor with an initial error message and initial error codes.
* If memory allocation failed for the error message, the error message will not be copied
* and Internal error message will point to NULL.
*
* \param szErrMsg [in] An initial error message.
* \param dwLastError [in] An error code.
* \param dwWin32LastError [in] A win32 error code.
*/
httpclientexceptionW::httpclientexceptionW (LPCWSTR szErrMsg, DWORD dwLastError, DWORD dwWin32LastError)
throw ()
: errmsg_exceptionW (szErrMsg)
{
m_dwLastError = dwLastError ;
m_dwWin32LastError = dwWin32LastError ;
}
///////////////////////////////////////// httpclientexception /////////////////////////////////////////
///////////////////////////////////////// CHttpToolA /////////////////////////////////////////
inline
LPCSTR CHttpToolA::GetConstMessage (DWORD nIdx)
throw ()
{
// user-defined error message
if ( nIdx >= 1000 )
return g_UsrErrMsgA[0] ;
// Win32 API error (which has a win32 error code)
if ( nIdx >= 600 )
return g_Win32MsgWin32A[nIdx % 100] ;
// WinInet error (which has a win32 error code)
if ( nIdx >= 400 )
return g_WinInetMsgWin32A[nIdx % 100] ;
// Normal error (which has a win32 error code)
if ( nIdx >= 200 )
return g_NormalMsgWin32A[nIdx % 100] ;
// Normal error
if ( nIdx >= 100 )
return g_NormalMsgA[nIdx % 100] ;
return g_NotSpecifiedA[nIdx] ;
}
void CHttpToolA::ThrowException (DWORD nErrMsgIdx)
throw (Exception &)
{
throw Exception (GetConstMessage (nErrMsgIdx), nErrMsgIdx) ;
}
void CHttpToolA::ThrowException (LPCSTR szErrMsg, DWORD nErrMsgIdx)
throw (Exception &)
{
throw Exception (szErrMsg, nErrMsgIdx) ;
}
void CHttpToolA::ThrowException (DWORD nErrMsgIdx, DWORD dwErrCode, LPCSTR szStrArg)
throw (Exception &)
{
if ( szStrArg == NULL )
throw Exception (GetConstMessage (nErrMsgIdx), nErrMsgIdx, dwErrCode) ;
CHAR szErrMsg[512] ;
LPCSTR szFormat = GetConstMessage (nErrMsgIdx) ;
int cchWritten = SNPrintf (szErrMsg, 511, szFormat, szStrArg ? szStrArg : "NULL") ;
// if an error occurs
if ( cchWritten < -1 )
throw Exception (szFormat, nErrMsgIdx, dwErrCode) ;
if ( cchWritten == -1 )
cchWritten = 511 ;
szErrMsg[cchWritten] = NULL ;
throw Exception (szErrMsg, nErrMsgIdx, dwErrCode) ;
}
void CHttpToolA::ThrowException (LPCWSTR szErrMsg, DWORD nErrMsgIdx, DWORD dwErrCode)
throw (Exception &)
{
LPSTR szErrMsgA = NULL ;
try {
szErrMsgA = CHttpToolA::Unicode2Ansi (szErrMsg) ;
} catch (Exception &) {
szErrMsgA = NULL ;
}
Exception objException (szErrMsgA, nErrMsgIdx, dwErrCode) ;
SAFEFREE (szErrMsgA) ;
throw objException ;
}
void CHttpToolA::ThrowException (CHttpToolW::Exception & e)
throw (Exception &)
{
ThrowException (e.errmsg (), e.LastError (), e.Win32LastError ()) ;
}
void CHttpToolA::ThrowException (::SafeIntException & e)
throw (Exception &)
{
switch ( e.m_code ) {
case ERROR_ARITHMETIC_OVERFLOW:
ThrowException (HTTPCLIENT_ERR_ARITHMETIC_OVERFLOW) ;
break ;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
ThrowException (HTTPCLIENT_ERR_INT_DIVIDE_BY_ZERO) ;
break ;
default:
ThrowException (HTTPCLIENT_ERR_UNEXPECTED_ARITHMETIC_ERROR) ;
break ;
}
}
// Conversion methods
// This method returns a converted ansi string from a unicode string.
// The returned string must be freed by using the ::free () function.
LPSTR CHttpToolA::Unicode2Ansi (LPCWSTR szStr, UINT CodePage)
throw (Exception &)
{
// The unicode encodings are not allowed
HTTPTOOL_ASSERT ((CodePage != CP_UTF8) && (CodePage != CP_UTF7)
, "CHttpToolA::Unicode2Ansi: CP_UTF8 and CP_UTF7 can not be used for the CodePage parameter.") ;
if ( szStr == NULL )
return NULL ;
int cchNeeded ;
if ( 0 == (cchNeeded = ::WideCharToMultiByte (CodePage, 0, szStr, -1, NULL, 0, NULL, NULL)) )
ThrowException (HTTPCLIENT_ERR_WIDECHARTOMULTIBYTE_FAILED, ::GetLastError ()) ;
PSTR szAnsi = (PSTR) ::malloc (sizeof (CHAR) * cchNeeded) ;
if ( szAnsi == NULL )
ThrowException (HTTPCLIENT_ERR_OUT_OF_MEMORY) ;
if ( 0 == ::WideCharToMultiByte (CodePage, 0, szStr, -1, szAnsi, cchNeeded, NULL, NULL) ) {
SAFEFREE (szAnsi) ;
ThrowException (HTTPCLIENT_ERR_WIDECHARTOMULTIBYTE_FAILED, ::GetLastError ()) ;
}
return szAnsi ;
}
// This method returns a converted unicode string from a ansi string.
// The returned string must be freed by using the ::free () function.
LPWSTR CHttpToolA::Ansi2Unicode (LPCSTR szStr, UINT CodePage)
throw (Exception &)
{
// The unicode encodings are not allowed
HTTPTOOL_ASSERT ((CodePage != CP_UTF8) && (CodePage != CP_UTF7)
, "CHttpToolA::Ansi2Unicode: CP_UTF8 and CP_UTF7 can not be used for the CodePage parameter.") ;
if ( szStr == NULL )
return NULL ;
int cchNeeded ;
if ( 0 == (cchNeeded = ::MultiByteToWideChar (CodePage, 0, szStr, -1, NULL, 0)) )
ThrowException (HTTPCLIENT_ERR_MULTIBYTETOWIDECHAR_FAILED, ::GetLastError ()) ;
PWSTR szUni = (PWSTR) ::malloc (sizeof (WCHAR) * cchNeeded) ;
if ( szUni == NULL )
ThrowException (HTTPCLIENT_ERR_OUT_OF_MEMORY) ;
if ( 0 == ::MultiByteToWideChar (CodePage, 0, szStr, -1, szUni, cchNeeded) ) {
SAFEFREE (szUni) ;
ThrowException (HTTPCLIENT_ERR_MULTIBYTETOWIDECHAR_FAILED, ::GetLastError ()) ;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -