HttpResponseHeader.cpp
资源名称:MiniCA2.rar [点击查看]
上传用户:dengkfang
上传日期:2008-12-30
资源大小:5233k
文件大小:8k
源码类别:
CA认证
开发平台:
Visual C++
- /*
- Module : HttpResponseHeader.cpp
- Purpose: Implementation for a class to simplify sending Http response headers
- Created: PJN / 22-04-1999
- History: None
- Copyright (c) 1999 - 2005 by PJ Naughter.
- All rights reserved.
- Copyright / Usage Details:
- You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise)
- when your product is released in binary form. You are allowed to modify the source code in any way you want
- except you cannot modify the copyright details at the top of each module. If you want to distribute source
- code with your application, then you are only allowed to distribute versions released by the author. This is
- to maintain a single distribution point for the source code.
- */
- //////////////// Includes ////////////////////////////////////////////
- #include "stdafx.h"
- #ifndef __AFXPRIV_H__
- #pragma message("To avoid this message please put afxpriv.h in your PCH (normally stdafx.h)")
- #include <afxpriv.h>
- #endif
- #include "HttpResponseHeader.h"
- //////////////// Macros //////////////////////////////////////////////
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- //////////////// Implementation //////////////////////////////////////
- CHttpResponseHeader::CHttpResponseHeader()
- {
- m_bEntitySeparator = TRUE;
- }
- void CHttpResponseHeader::SetAddEntitySeparator(BOOL bSeparator)
- {
- m_bEntitySeparator = bSeparator;
- }
- void CHttpResponseHeader::AddStatus(LPCSTR pszStatusString)
- {
- CString sLine;
- sLine.Format(_T("HTTP/1.0 %srn"), pszStatusString);
- m_sHeader += sLine;
- }
- void CHttpResponseHeader::AddExtraHeaders(LPCSTR pszHeaders)
- {
- m_sHeader += pszHeaders;
- }
- void CHttpResponseHeader::AddStatusCode(int nStatusCode)
- {
- CString sLine;
- switch (nStatusCode)
- {
- case 200: sLine = _T("HTTP/1.0 200 OKrn"); break;
- case 201: sLine = _T("HTTP/1.0 201 Createdrn"); break;
- case 202: sLine = _T("HTTP/1.0 202 Acceptedrn"); break;
- case 204: sLine = _T("HTTP/1.0 204 No Contentrn"); break;
- case 300: sLine = _T("HTTP/1.0 300 Multiple Choicesrn"); break;
- case 301: sLine = _T("HTTP/1.0 301 Moved Permanentlyrn"); break;
- case 302: sLine = _T("HTTP/1.0 302 Moved Temporarilyrn"); break;
- case 304: sLine = _T("HTTP/1.0 304 Not Modifiedrn"); break;
- case 400: sLine = _T("HTTP/1.0 400 Bad Requestrn"); break;
- case 401: sLine = _T("HTTP/1.0 401 Unauthorizedrn"); break;
- case 403: sLine = _T("HTTP/1.0 403 Forbiddenrn"); break;
- case 404: sLine = _T("HTTP/1.0 404 Not Foundrn"); break;
- case 500: sLine = _T("HTTP/1.0 500 Internal Server Errorrn"); break;
- case 501: sLine = _T("HTTP/1.0 501 Not Implementedrn"); break;
- case 502: sLine = _T("HTTP/1.0 502 Bad Gatewayrn"); break;
- case 503: sLine = _T("HTTP/1.0 503 Service Unavailablern"); break;
- default: sLine.Format(_T("HTTP/1.0 %drn"), nStatusCode); break;
- }
- m_sHeader += sLine;
- }
- void CHttpResponseHeader::AddContentLength(int nSize)
- {
- CString sLine;
- sLine.Format(_T("Content-Length: %drn"), nSize);
- m_sHeader += sLine;
- }
- void CHttpResponseHeader::AddContentType(const CString& sMediaType)
- {
- CString sLine;
- sLine.Format(_T("Content-Type: %srn"), sMediaType);
- m_sHeader += sLine;
- }
- CString CHttpResponseHeader::DateToStr(const SYSTEMTIME& st)
- {
- static TCHAR* sMonth[] =
- {
- _T(""),
- _T("Jan"),
- _T("Feb"),
- _T("Mar"),
- _T("Apr"),
- _T("May"),
- _T("Jun"),
- _T("Jul"),
- _T("Aug"),
- _T("Sep"),
- _T("Oct"),
- _T("Nov"),
- _T("Dec"),
- };
- static TCHAR* sDay[] =
- {
- _T("Sun"),
- _T("Mon"),
- _T("Tue"),
- _T("Wed"),
- _T("Thu"),
- _T("Fri"),
- _T("Sat"),
- };
- CString sDate;
- sDate.Format(_T("%s, %02d %s %04d %02d:%02d:%02d GMT"), sDay[st.wDayOfWeek],
- st.wDay, sMonth[st.wMonth], st.wYear, st.wHour, st.wMinute, st.wSecond);
- return sDate;
- }
- void CHttpResponseHeader::AddW3MfcAllowFields(BOOL bAllowDeleteRequest)
- {
- if (bAllowDeleteRequest)
- m_sHeader += _T("Allow: GET, POST, HEAD, DELETErn");
- else
- m_sHeader += _T("Allow: GET, POST, HEADrn");
- }
- void CHttpResponseHeader::AddDate(const SYSTEMTIME& st)
- {
- CString sDate = DateToStr(st);
- CString sLine;
- sLine.Format(_T("Date: %srn"), sDate);
- m_sHeader += sLine;
- }
- void CHttpResponseHeader::AddLastModified(const SYSTEMTIME& st)
- {
- CString sDate = DateToStr(st);
- CString sLine;
- sLine.Format(_T("Last-Modified: %srn"), sDate);
- m_sHeader += sLine;
- }
- void CHttpResponseHeader::AddExpires(const SYSTEMTIME& st)
- {
- CString sDate = DateToStr(st);
- CString sLine;
- sLine.Format(_T("Expires: %srn"), sDate);
- m_sHeader += sLine;
- }
- void CHttpResponseHeader::AddKeepAlive()
- {
- m_sHeader += _T("Connection: Keep-Alivern");
- }
- void CHttpResponseHeader::AddWWWAuthenticateBasic(const CString& sRealm)
- {
- CString sLine;
- sLine.Format(_T("WWW-Authenticate: Basic realm=%srn"), sRealm);
- m_sHeader += sLine;
- }
- void CHttpResponseHeader::AddWWWAuthenticateNTLM(const CString& sMessage)
- {
- CString sLine;
- if (sMessage.GetLength())
- sLine.Format(_T("WWW-Authenticate: NTLM %srn"), sMessage);
- else
- sLine = _T("WWW-Authenticate: NTLMrn");
- m_sHeader += sLine;
- }
- void CHttpResponseHeader::AddLocation(const CString& sLocation)
- {
- CString sLine;
- sLine.Format(_T("Location: %srn"), sLocation);
- m_sHeader += sLine;
- }
- void CHttpResponseHeader::AddServer(const CString& sServer)
- {
- CString sLine;
- sLine.Format(_T("Server: %srn"), sServer);
- m_sHeader += sLine;
- }
- BOOL CHttpResponseHeader::Send(CHttpSocket& socket, DWORD dwTimeout)
- {
- //Get the ascii version of the data
- DWORD dwSize = 0;
- char* pszData = GetData(dwSize);
- //Send it down the socket
- BOOL bSuccess = TRUE; //assume the best
- try
- {
- socket.SendWithRetry(pszData, dwSize, dwTimeout);
- }
- catch(CWSocketException* pEx)
- {
- TRACE(_T("CHttpResponseHeader::Send, Error:%dn"), pEx->m_nError);
- pEx->Delete();
- bSuccess = FALSE;
- }
- //Tidy up the heap memory we have used
- delete [] pszData;
- return bSuccess;
- }
- #ifdef W3MFC_SSL_SUPPORT
- BOOL CHttpResponseHeader::Send(CHttpSocket& socket, DWORD dwTimeout, CSSL& ssl)
- {
- BOOL bSuccess = TRUE; //assume the best
- if (ssl.operator SSL*())
- {
- //Get the ascii version of the data
- DWORD dwSize = 0;
- char* pszData = GetData(dwSize);
- //Send it down the socket
- try
- {
- socket.SendWithRetry(pszData, dwSize, dwTimeout, ssl);
- }
- catch(CWSocketException* pEx)
- {
- TRACE(_T("CHttpResponseHeader::Send, Error:%dn"), pEx->m_nError);
- pEx->Delete();
- bSuccess = FALSE;
- }
- //Tidy up the heap memory we have used
- delete [] pszData;
- }
- else
- bSuccess = Send(socket, dwTimeout);
- return bSuccess;
- }
- #endif
- char* CHttpResponseHeader::GetData(DWORD& dwSize)
- {
- //Allocate some string space from the heap
- int nLength = m_sHeader.GetLength();
- #ifdef _UNICODE
- int nBytes = WideCharToMultiByte(CP_ACP, 0, m_sHeader, nLength, NULL, NULL, NULL, NULL);
- nBytes += 3;
- char* pszData = new char[nBytes];
- nBytes = WideCharToMultiByte(CP_ACP, 0, m_sHeader, nLength, pszData, nBytes, NULL, NULL);
- pszData[nBytes] = ' ';
- #else
- dwSize = nLength + 3;
- char* pszData = new char[dwSize];
- strcpy(pszData, m_sHeader);
- #endif
- if (m_bEntitySeparator)
- strcat(pszData, "rn");
- dwSize = strlen(pszData);
- return pszData;
- }