/* * Copyright: JessMA Open Source (ldcsaa@gmail.com) * * Author : Bruce Liang * Website : https://github.com/ldcsaa * Project : https://github.com/ldcsaa/HP-Socket * Blog : http://www.cnblogs.com/ldcsaa * Wiki : http://www.oschina.net/p/hp-socket * QQ Group : 44636872, 75375912 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "stdafx.h" #include "HttpServer.h" #ifdef _HTTP_SUPPORT template BOOL CHttpServerT::Start(LPCTSTR lpszBindAddress, USHORT usPort) { BOOL isOK = __super::Start(lpszBindAddress, usPort); if(isOK) ENSURE(m_thCleaner.Start(this, &CHttpServerT::CleanerThreadProc)); return isOK; } template BOOL CHttpServerT::CheckParams() { if ((m_enLocalVersion != HV_1_1 && m_enLocalVersion != HV_1_0) || (m_dwReleaseDelay < MIN_HTTP_RELEASE_DELAY || m_dwReleaseDelay > MAX_HTTP_RELEASE_DELAY)) { SetLastError(SE_INVALID_PARAM, __FUNCTION__, ERROR_INVALID_PARAMETER); return FALSE; } return __super::CheckParams(); } template void CHttpServerT::PrepareStart() { __super::PrepareStart(); m_objPool.SetHttpObjLockTime(GetFreeSocketObjLockTime()); m_objPool.SetHttpObjPoolSize(GetFreeSocketObjPool()); m_objPool.SetHttpObjPoolHold(GetFreeSocketObjHold()); m_objPool.Prepare(); } template BOOL CHttpServerT::SendResponse(CONNID dwConnID, USHORT usStatusCode, LPCSTR lpszDesc, const THeader lpHeaders[], int iHeaderCount, const BYTE* pData, int iLength) { WSABUF szBuffer[2]; CStringA strHeader; ::MakeStatusLine(m_enLocalVersion, usStatusCode, lpszDesc, strHeader); ::MakeHeaderLines(lpHeaders, iHeaderCount, nullptr, iLength, FALSE, IsKeepAlive(dwConnID), nullptr, 0, strHeader); ::MakeHttpPacket(strHeader, pData, iLength, szBuffer); return SendPackets(dwConnID, szBuffer, 2); } template BOOL CHttpServerT::SendLocalFile(CONNID dwConnID, LPCSTR lpszFileName, USHORT usStatusCode, LPCSTR lpszDesc, const THeader lpHeaders[], int iHeaderCount) { CAtlFile file; CAtlFileMapping<> fmap; HRESULT hr = ::ReadSmallFile(CA2T(lpszFileName), file, fmap); if(FAILED(hr)) { ::SetLastError(HRESULT_CODE(hr)); return FALSE; } return SendResponse(dwConnID, usStatusCode, lpszDesc, lpHeaders, iHeaderCount, (BYTE*)(char*)fmap, (int)fmap.GetMappingSize()); } template BOOL CHttpServerT::SendChunkData(CONNID dwConnID, const BYTE* pData, int iLength, LPCSTR lpszExtensions) { char szLen[12]; WSABUF bufs[5]; int iCount = MakeChunkPackage(pData, iLength, lpszExtensions, szLen, bufs); return SendPackets(dwConnID, bufs, iCount); } template BOOL CHttpServerT::Release(CONNID dwConnID) { if(!HasStarted()) return FALSE; THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr || pHttpObj->HasReleased()) return FALSE; pHttpObj->Release(); m_lsDyingQueue.PushBack(TDyingConnection::Construct(dwConnID)); return TRUE; } template BOOL CHttpServerT::SendWSMessage(CONNID dwConnID, BOOL bFinal, BYTE iReserved, BYTE iOperationCode, const BYTE* pData, int iLength, ULONGLONG ullBodyLen) { WSABUF szBuffer[2]; BYTE szHeader[HTTP_MAX_WS_HEADER_LEN]; if(!::MakeWSPacket(bFinal, iReserved, iOperationCode, nullptr, (BYTE*)pData, iLength, ullBodyLen, szHeader, szBuffer)) return FALSE; return SendPackets(dwConnID, szBuffer, 2); } template UINT CHttpServerT::CleanerThreadProc(PVOID pv) { TRACE("---------------> Connection Cleaner Thread 0x%08X started <---------------\n", SELF_THREAD_ID); DWORD dwRetValue = 0; DWORD dwInterval = max(MIN_HTTP_RELEASE_CHECK_INTERVAL, (m_dwReleaseDelay - MIN_HTTP_RELEASE_DELAY / 2)); while(HasStarted()) { dwRetValue = ::WaitForSingleObject(m_evCleaner, dwInterval); if(dwRetValue == WAIT_TIMEOUT) KillDyingConnection(); else if(dwRetValue == WAIT_OBJECT_0) break; else ASSERT(FALSE); } ReleaseDyingConnection(); TRACE("---------------> Connection Cleaner Thread 0x%08X stoped <---------------\n", SELF_THREAD_ID); return 0; } template void CHttpServerT::KillDyingConnection() { TDyingConnection* pDyingConn = nullptr; TDyingConnection* pFirstDyingConn = nullptr; DWORD now = ::TimeGetTime(); while(m_lsDyingQueue.UnsafePeekFront(&pDyingConn)) { if((int)(now - pDyingConn->killTime) < (int)m_dwReleaseDelay) break; BOOL bDisconnect = TRUE; BOOL bDestruct = TRUE; ENSURE(m_lsDyingQueue.UnsafePopFront(&pDyingConn)); int iPending; if(!GetPendingDataLength(pDyingConn->connID, iPending)) bDisconnect = FALSE; else if(iPending > 0) { bDisconnect = FALSE; bDestruct = FALSE; } if(bDisconnect) Disconnect(pDyingConn->connID, TRUE); if(bDestruct) { TDyingConnection::Destruct(pDyingConn); if(pFirstDyingConn == pDyingConn) pFirstDyingConn = nullptr; } else { m_lsDyingQueue.PushBack(pDyingConn); if(pFirstDyingConn == nullptr) pFirstDyingConn = pDyingConn; else if(pFirstDyingConn == pDyingConn) break; } } } template void CHttpServerT::ReleaseDyingConnection() { TDyingConnection* pDyingConn = nullptr; while(m_lsDyingQueue.UnsafePopFront(&pDyingConn)) TDyingConnection::Destruct(pDyingConn); ENSURE(m_lsDyingQueue.IsEmpty()); } template EnHandleResult CHttpServerT::FireAccept(TSocketObj* pSocketObj) { return m_bHttpAutoStart ? __super::FireAccept(pSocketObj) : __super::DoFireAccept(pSocketObj); } template EnHandleResult CHttpServerT::DoFireAccept(TSocketObj* pSocketObj) { THttpObj* pHttpObj = DoStartHttp(pSocketObj); EnHandleResult result = __super::DoFireAccept(pSocketObj); if(result == HR_ERROR) { m_objPool.PutFreeHttpObj(pHttpObj);; SetConnectionReserved(pSocketObj, nullptr); } return result; } template EnHandleResult CHttpServerT::DoFireHandShake(TSocketObj* pSocketObj) { EnHandleResult result = __super::DoFireHandShake(pSocketObj); if(result == HR_ERROR) { THttpObj* pHttpObj = FindHttpObj(pSocketObj); if(pHttpObj != nullptr) { m_objPool.PutFreeHttpObj(pHttpObj); SetConnectionReserved(pSocketObj, nullptr); } } return result; } template EnHandleResult CHttpServerT::DoFireReceive(TSocketObj* pSocketObj, const BYTE* pData, int iLength) { THttpObj* pHttpObj = FindHttpObj(pSocketObj); if(pHttpObj == nullptr) return DoFireSuperReceive(pSocketObj, pData, iLength); else { if(pHttpObj->HasReleased()) return HR_ERROR; return pHttpObj->Execute(pData, iLength); } } template EnHandleResult CHttpServerT::DoFireClose(TSocketObj* pSocketObj, EnSocketOperation enOperation, int iErrorCode) { EnHandleResult result = __super::DoFireClose(pSocketObj, enOperation, iErrorCode); THttpObj* pHttpObj = FindHttpObj(pSocketObj); if(pHttpObj != nullptr) { m_objPool.PutFreeHttpObj(pHttpObj); SetConnectionReserved(pSocketObj, nullptr); } return result; } template EnHandleResult CHttpServerT::DoFireShutdown() { EnHandleResult result = __super::DoFireShutdown(); m_objPool.Clear(); WaitForCleanerThreadEnd(); return result; } template void CHttpServerT::WaitForCleanerThreadEnd() { if(m_thCleaner.IsRunning()) { m_evCleaner.Set(); ENSURE(m_thCleaner.Join(TRUE)); m_evCleaner.Reset(); } } template BOOL CHttpServerT::IsUpgrade(CONNID dwConnID) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return FALSE; return pHttpObj->IsUpgrade(); } template BOOL CHttpServerT::IsKeepAlive(CONNID dwConnID) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return FALSE; return pHttpObj->IsKeepAlive(); } template USHORT CHttpServerT::GetVersion(CONNID dwConnID) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return 0; return pHttpObj->GetVersion(); } template LPCSTR CHttpServerT::GetHost(CONNID dwConnID) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return nullptr; return pHttpObj->GetHost(); } template ULONGLONG CHttpServerT::GetContentLength(CONNID dwConnID) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return 0; return pHttpObj->GetContentLength(); } template LPCSTR CHttpServerT::GetContentType(CONNID dwConnID) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return nullptr; return pHttpObj->GetContentType(); } template LPCSTR CHttpServerT::GetContentEncoding(CONNID dwConnID) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return nullptr; return pHttpObj->GetContentEncoding(); } template LPCSTR CHttpServerT::GetTransferEncoding(CONNID dwConnID) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return nullptr; return pHttpObj->GetTransferEncoding(); } template EnHttpUpgradeType CHttpServerT::GetUpgradeType(CONNID dwConnID) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return HUT_NONE; return pHttpObj->GetUpgradeType(); } template USHORT CHttpServerT::GetParseErrorCode(CONNID dwConnID, LPCSTR* lpszErrorDesc) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return 0; return pHttpObj->GetParseErrorCode(lpszErrorDesc); } template BOOL CHttpServerT::GetHeader(CONNID dwConnID, LPCSTR lpszName, LPCSTR* lpszValue) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return FALSE; return pHttpObj->GetHeader(lpszName, lpszValue); } template BOOL CHttpServerT::GetHeaders(CONNID dwConnID, LPCSTR lpszName, LPCSTR lpszValue[], DWORD& dwCount) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return FALSE; return pHttpObj->GetHeaders(lpszName, lpszValue, dwCount); } template BOOL CHttpServerT::GetAllHeaders(CONNID dwConnID, THeader lpHeaders[], DWORD& dwCount) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return FALSE; return pHttpObj->GetAllHeaders(lpHeaders, dwCount); } template BOOL CHttpServerT::GetAllHeaderNames(CONNID dwConnID, LPCSTR lpszName[], DWORD& dwCount) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return FALSE; return pHttpObj->GetAllHeaderNames(lpszName, dwCount); } template BOOL CHttpServerT::GetCookie(CONNID dwConnID, LPCSTR lpszName, LPCSTR* lpszValue) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return FALSE; return pHttpObj->GetCookie(lpszName, lpszValue); } template BOOL CHttpServerT::GetAllCookies(CONNID dwConnID, TCookie lpCookies[], DWORD& dwCount) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return FALSE; return pHttpObj->GetAllCookies(lpCookies, dwCount); } template USHORT CHttpServerT::GetUrlFieldSet(CONNID dwConnID) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return 0; return pHttpObj->GetUrlFieldSet(); } template LPCSTR CHttpServerT::GetUrlField(CONNID dwConnID, EnHttpUrlField enField) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return nullptr; return pHttpObj->GetUrlField(enField); } template LPCSTR CHttpServerT::GetMethod(CONNID dwConnID) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return nullptr; return pHttpObj->GetMethod(); } template BOOL CHttpServerT::GetWSMessageState(CONNID dwConnID, BOOL* lpbFinal, BYTE* lpiReserved, BYTE* lpiOperationCode, LPCBYTE* lpszMask, ULONGLONG* lpullBodyLen, ULONGLONG* lpullBodyRemain) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return FALSE; return pHttpObj->GetWSMessageState(lpbFinal, lpiReserved, lpiOperationCode, lpszMask, lpullBodyLen, lpullBodyRemain); } template inline typename CHttpServerT::THttpObj* CHttpServerT::FindHttpObj(CONNID dwConnID) { THttpObj* pHttpObj = nullptr; GetConnectionReserved(dwConnID, (PVOID*)&pHttpObj); return pHttpObj; } template inline typename CHttpServerT::THttpObj* CHttpServerT::FindHttpObj(TSocketObj* pSocketObj) { THttpObj* pHttpObj = nullptr; GetConnectionReserved(pSocketObj, (PVOID*)&pHttpObj); return pHttpObj; } template BOOL CHttpServerT::StartHttp(CONNID dwConnID) { if(IsHttpAutoStart()) { ::SetLastError(ERROR_INVALID_OPERATION); return FALSE; } TSocketObj* pSocketObj = FindSocketObj(dwConnID); if(!TSocketObj::IsValid(pSocketObj)) { ::SetLastError(ERROR_OBJECT_NOT_FOUND); return FALSE; } return StartHttp(pSocketObj); } template BOOL CHttpServerT::StartHttp(TSocketObj* pSocketObj) { if(!pSocketObj->HasConnected()) { ::SetLastError(ERROR_INVALID_STATE); return FALSE; } CCriSecLock locallock(pSocketObj->csSend); if(!TSocketObj::IsValid(pSocketObj)) { ::SetLastError(ERROR_OBJECT_NOT_FOUND); return FALSE; } if(!pSocketObj->HasConnected()) { ::SetLastError(ERROR_INVALID_STATE); return FALSE; } THttpObj* pHttpObj = FindHttpObj(pSocketObj); if(pHttpObj != nullptr) { ::SetLastError(ERROR_ALREADY_INITIALIZED); return FALSE; } DoStartHttp(pSocketObj); if(!IsSecure()) FireHandShake(pSocketObj); else { #ifdef _SSL_SUPPORT if(IsSSLAutoHandShake()) StartSSLHandShake(pSocketObj); #endif } return TRUE; } template typename CHttpServerT::THttpObj* CHttpServerT::DoStartHttp(TSocketObj* pSocketObj) { THttpObj* pHttpObj = m_objPool.PickFreeHttpObj(this, pSocketObj); ENSURE(SetConnectionReserved(pSocketObj, pHttpObj)); return pHttpObj; } // ------------------------------------------------------------------------------------------------------------- // template class CHttpServerT; #ifdef _SSL_SUPPORT #include "SSLServer.h" template class CHttpServerT; #endif #endif