/* * Copyright: JessMA Open Source (ldcsaa@gmail.com) * * Version : 3.6.1 * Author : Bruce Liang * Website : http://www.jessma.org * Project : https://github.com/ldcsaa * Blog : http://www.cnblogs.com/ldcsaa * Wiki : http://www.oschina.net/p/hp-socket * QQ Group : 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" template BOOL CHttpServerT::Start(LPCTSTR lpszBindAddress, USHORT usPort) { BOOL isOK = __super::Start(lpszBindAddress, usPort); if(isOK) VERIFY(m_thCleaner.Start(this)); 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 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, strHeader); ::MakeHttpPacket(strHeader, pData, iLength, szBuffer); return SendPackets(dwConnID, szBuffer, 2); } template BOOL CHttpServerT::Release(CONNID dwConnID) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr || pHttpObj->HasReleased()) return FALSE; pHttpObj->Release(); m_lsDyingQueue.PushBack(TDyingConnection::Construct(dwConnID)); return TRUE; } template UINT CHttpServerT::Run() { TRACE("---------------> Connection Cleaner Thread 0x%08X started <---------------\n", ::GetCurrentThreadId()); 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); } TRACE("---------------> Connection Cleaner Thread 0x%08X stoped <---------------\n", ::GetCurrentThreadId()); return 0; } template void CHttpServerT::KillDyingConnection() { TDyingConnection* pDyingConn = nullptr; DWORD now = ::TimeGetTime(); while(m_lsDyingQueue.UnsafePeekFront(&pDyingConn)) { if((int)(now - pDyingConn->killTime) < (int)m_dwReleaseDelay) break; Disconnect(pDyingConn->connID, TRUE); VERIFY(m_lsDyingQueue.UnsafePopFront(&pDyingConn)); TDyingConnection::Destruct(pDyingConn); } } template EnHandleResult CHttpServerT::DoFireAccept(TSocketObj* pSocketObj) { EnHandleResult result = __super::DoFireAccept(pSocketObj); if(result != HR_ERROR) VERIFY(SetConnectionReserved(pSocketObj, THttpObj::Construct(TRUE, this, pSocketObj))); return result; } template EnHandleResult CHttpServerT::DoFireReceive(TSocketObj* pSocketObj, const BYTE* pData, int iLength) { THttpObj* pHttpObj = FindHttpObj(pSocketObj); ASSERT(pHttpObj); if(pHttpObj->HasReleased()) return HR_IGNORE; 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) THttpObj::Destruct(pHttpObj); return result; } template EnHandleResult CHttpServerT::DoFireShutdown() { EnHandleResult result = __super::DoFireShutdown(); WaitForCleanerThreadEnd(); ReleaseDyingConnection(); return result; } template void CHttpServerT::WaitForCleanerThreadEnd() { if(m_thCleaner.IsValid()) { m_evCleaner.Set(); m_thCleaner.Join(TRUE); } } template void CHttpServerT::ReleaseDyingConnection() { TDyingConnection* pDyingConn = nullptr; while(m_lsDyingQueue.UnsafePopFront(&pDyingConn)) TDyingConnection::Destruct(pDyingConn); VERIFY(m_lsDyingQueue.IsEmpty()); } 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 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 0; return pHttpObj->GetContentType(); } 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 0; 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 0; return pHttpObj->GetHeaders(lpszName, lpszValue, dwCount); } template BOOL CHttpServerT::GetAllHeaders(CONNID dwConnID, THeader lpHeaders[], DWORD& dwCount) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return 0; return pHttpObj->GetAllHeaders(lpHeaders, dwCount); } template BOOL CHttpServerT::GetAllHeaderNames(CONNID dwConnID, LPCSTR lpszName[], DWORD& dwCount) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return 0; 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 0; return pHttpObj->GetUrlField(enField); } template LPCSTR CHttpServerT::GetMethod(CONNID dwConnID) { THttpObj* pHttpObj = FindHttpObj(dwConnID); if(pHttpObj == nullptr) return 0; return pHttpObj->GetMethod(); } 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 class CHttpServerT; #ifdef _SSL_SUPPORT #include "SSLServer.h" template class CHttpServerT; #endif