亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? reqsock.cpp

?? 一個網路服務器程序
?? CPP
?? 第 1 頁 / 共 3 頁
字號:
// ReqSock.cpp : implementation of the CRequestSocket class
//
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.

#include "stdafx.h"
#include "HttpSvr.h"
#include "HttpDoc.h"
#include "Http.h"
#include "ReqSock.h"
#include "Request.h"
#include "resource.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNCREATE(CRequestSocket, CAsyncSocket)

#define CRLF    "\x0d\x0a"

SECURITY_ATTRIBUTES g_sa = {sizeof(SECURITY_ATTRIBUTES),NULL,TRUE};

CRequestSocket::CRequestSocket( void )
{
}

CRequestSocket::CRequestSocket( CHttpSvrDoc* pDoc )
{
#ifdef IMPL_CGI
	m_pThread = NULL;
	m_pCancel = NULL;
#endif // IMPL_CGI
	m_bKilled = FALSE;
	m_nRefs = 1;
	m_reqStatus = REQ_REQUEST;
	m_buf.SetSize( 1024 );
	m_cbOut = 0;
	m_hFile = INVALID_HANDLE_VALUE;
	m_pRequest = NULL;
	m_pDoc = pDoc;
}

CRequestSocket::~CRequestSocket( void )
{
	// JIC....
#ifdef IMPL_CGI
	if ( m_pCancel )
	{
		if ( m_pThread )
		{
			DWORD dwCode;
			// signal a cancel if still running....
			if ( ::GetExitCodeThread( m_pThread->m_hThread, &dwCode )
				&& dwCode == STILL_ACTIVE )
			{
				// signal a cancel....
				m_pCancel->SetEvent();
				// wait for the thread to die....
				WaitForSingleObject( m_pThread->m_hThread, INFINITE );
			}
			// kill the object...
			delete m_pThread;
		}
		delete m_pCancel;
	}
#endif // IMPL_CGI

	if ( m_hFile )
		CloseHandle( m_hFile );

	if ( m_pRequest )
	{
		// release our hold on the request object....
		m_pRequest->m_bDone = TRUE;
		m_pRequest->Release();
	}
}

void CRequestSocket::OnReceive(int /*nErrorCode*/)
{
	if ( m_pRequest == NULL )
	{
		// new request....
		m_pRequest = new CRequest;
		m_bKeepOpen = m_bWantKeepOpen = FALSE;
	}
	if ( m_pRequest )
	{
		// get the bytes....
		int nBytes = Receive( m_buf.GetData(), (int)m_buf.GetSize() );
		if ( nBytes != SOCKET_ERROR )
		{
			int ndx = 0;
			switch ( m_reqStatus )
			{
			case REQ_REQUEST:
			case REQ_HEADER:
				while( GetLine( m_buf, nBytes, ndx ) == TRUE )
				{
					if ( !m_strLine.IsEmpty() )
						ProcessLine();
					else
					{
						m_reqStatus = REQ_BODY;
						break;
					}
				}
                // HTTP 1.0 type request contains only a single line....
                if ( m_reqStatus == REQ_SIMPLE)
                {
                    m_reqStatus = REQ_DONE;
                    break;
                }
				// break if we're not looking for the body....
				if ( m_reqStatus != REQ_BODY )
					break;
				// stop if no body sent....
				if ( !BodySent() )
				{
					m_reqStatus = REQ_DONE;
					break;
				}
				// else fall through....
			case REQ_BODY:
				AddToBody( nBytes, ndx );
				break;
			}
			if ( m_reqStatus == REQ_DONE )
			{
				m_buf.SetSize(0);
				if ( !StartResponse() )
                {
                    BOOL bOk = AsyncSelect( FD_WRITE | FD_CLOSE );
                    ASSERT(bOk);
                }
			}
		}
		else
			nBytes = GetLastError();
	}
	else
	{
		// couldn't allocate request object....
		ShutDown( both );
		m_bKilled = TRUE;
		Release();
	}
}

void CRequestSocket::OnSend(int /*nErrorCode*/)
{
	int nBytes = Send( m_buf.GetData(), m_cbOut );
	if ( nBytes == SOCKET_ERROR )
	{
		if ( GetLastError() != WSAEWOULDBLOCK )
		{
			ShutDown( both );
			m_bKilled = TRUE;
			Release();
		}
		else
        {
			BOOL bOk = AsyncSelect( FD_WRITE | FD_CLOSE );
            ASSERT(bOk);
        }
	}
	else if ( nBytes < m_cbOut )
	{
		// still got some left....
		m_buf.RemoveAt( 0, nBytes );
		m_cbOut -= nBytes;
		// adjust the bytes-sent value for the request....
		m_pRequest->m_cbSent += nBytes;
		// set up for next write....
        BOOL bOk = AsyncSelect( FD_WRITE | FD_CLOSE );
        ASSERT(bOk);
	}
	else
	{
		// adjust the bytes-sent value for the request....
		m_pRequest->m_cbSent += nBytes;
		// if we're not done with the file....
		if ( m_hFile != INVALID_HANDLE_VALUE )
		{
			DWORD dwRead = 0;
			// read next chunk....
			if(ReadFile( m_hFile, m_buf.GetData(),
				(DWORD)m_buf.GetSize(), &dwRead, NULL ) && dwRead > 0)
			{
				m_cbOut = dwRead;
			}
			else
			{
				// no more data to send....
				CloseHandle( m_hFile );
				m_hFile = INVALID_HANDLE_VALUE;
			}
		}
		// see if we need to keep going....
		if ( m_hFile != INVALID_HANDLE_VALUE )
        {
			BOOL bOk = AsyncSelect( FD_WRITE | FD_CLOSE );
            ASSERT(bOk);
        }
		else
		{
			// eh, we're outta here....
			ShutDown( both );
			m_bKilled = TRUE;
			Release();
		}
	}
}

void CRequestSocket::OnClose(int /*nErrorCode*/)
{
	m_bKilled = TRUE;
	Release();
}

BOOL CRequestSocket::GetLine( const CByteArray& bytes, int nBytes, int& ndx )
{
    // MIME-style line-contuniation is not supported
	BOOL bLine = FALSE;
	while ( bLine == FALSE && ndx < nBytes )
	{
		char ch = (char)(bytes.GetAt( ndx ));
		switch( ch )
		{
		case '\r': // ignore
			break;
		case '\n': // end-of-line
			bLine = TRUE;
			break;
		default:   // other....
			m_strLine += ch;
			break;
		}
		++ndx;
	}
	return bLine;
}

void CRequestSocket::ProcessLine( void )
{
	int ndx;
	switch( m_reqStatus )
	{
	case REQ_REQUEST:
		ndx = m_strLine.Find( ' ' );
		if ( ndx != -1 )
		{
			m_pRequest->m_strMethod = m_strLine.Left( ndx );
			m_strLine = m_strLine.Mid( ndx+1 );
			m_strLine.TrimLeft();
			ndx = m_strLine.Find( ' ' );
			if ( ndx == -1 )
			{
				m_pRequest->m_strURL = m_strLine;
				m_pRequest->m_strURL.TrimRight();
				m_reqStatus = REQ_SIMPLE;
			}
			else
			{
				m_pRequest->m_strURL = m_strLine.Left( ndx );
				m_pRequest->m_strVersion = m_strLine.Mid( ndx+1 );
				m_pRequest->m_strVersion.TrimLeft();
			}
			// check for execution arguments....
			ndx = m_pRequest->m_strURL.Find( '?' );
			if ( ndx != -1 )
			{
				// yup; save the args....
				m_pRequest->m_strArgs = m_pRequest->m_strURL.Mid( ndx+1 );
				// strip from file name....
				m_pRequest->m_strURL = m_pRequest->m_strURL.Left( ndx );
				m_pRequest->m_dwExecute = CRequest::APP_EXECUTE;
			}

			// change any "%xx"s to the appropriate char....
			m_pRequest->m_strURL = Decode( m_pRequest->m_strURL );
		}
        if (m_reqStatus != REQ_SIMPLE)
		    m_reqStatus = REQ_HEADER;
		break;
	case REQ_HEADER:
		ndx = m_strLine.Find( ':' );
		if ( ndx != -1 )
		{
			CString strName = m_strLine.Left( ndx );
			CString strValue = m_strLine.Mid( ndx+1 );
			strName.MakeLower();
			strValue.TrimLeft();
			m_pRequest->m_mapHeaders.SetAt( strName, strValue );
		}
		break;
	};
	m_strLine.Empty();
}

BOOL CRequestSocket::BodySent( void )
{
	BOOL bSent = FALSE;
	CString strValue = m_pRequest->GetHeaderValue( "Content-Length" );
	if ( !strValue.IsEmpty() )
	{
		m_pRequest->m_cbBody = atoi( strValue );
		bSent = TRUE;
	}
	return bSent;
}

void CRequestSocket::AddToBody( int nBytes, int ndx )
{
	// save the buffer size....
	int nOldSize = (int)m_buf.GetSize();
	// get rid of old stuff; append to body data....
	m_buf.RemoveAt( 0, ndx );
	m_buf.SetSize( nBytes - ndx );
	m_pRequest->m_baBody.Append( m_buf );
	// restore the buffer size....
	m_buf.SetSize( nOldSize );
	// see if we're done....
	if ( m_pRequest->m_baBody.GetSize() >= m_pRequest->m_cbBody )
	{
		m_pRequest->m_baBody.SetSize( m_pRequest->m_cbBody );
		m_reqStatus = REQ_DONE;
	}
}

BOOL CRequestSocket::StartResponse( void )
{
	BOOL bWait = FALSE;
	CString strFile;
	UINT uPort;
	// save the host address....
	GetPeerName( m_pRequest->m_strHost, uPort );
	// switch on the method....
	if ( m_pRequest->m_cbBody == 0 &&
		m_pRequest->m_strMethod.CompareNoCase( "GET" ) == 0 )
	{
		FindTarget( strFile );
		if( m_pRequest->m_uStatus == 0 )
		{
			if ( m_pRequest->m_dwExecute )
				bWait=StartSvrApp();
			else
			{
				if ( StuffHeading() )
					StartTargetStuff();
			}
		}
	}
	else if ( m_pRequest->m_cbBody == 0 && m_reqStatus == REQ_DONE &&
		m_pRequest->m_strMethod.CompareNoCase( "HEAD" ) == 0 )
	{
		FindTarget( strFile );
		if( m_pRequest->m_uStatus == 0 )
		{
			if ( m_pRequest->m_dwExecute )
				bWait=StartSvrApp();
			else
			{
				StuffHeading();
				// we don't send the file for HEAD reqs....
				if ( m_hFile != INVALID_HANDLE_VALUE)
				{
					CloseHandle( m_hFile );
					m_hFile = INVALID_HANDLE_VALUE;
				}
			}
		}
	}
	else if ( m_pRequest->m_cbBody > 0 && m_reqStatus == REQ_DONE &&
		m_pRequest->m_strMethod.CompareNoCase( "POST" ) == 0 )
	{
		// assume an executable....
		m_pRequest->m_dwExecute = CRequest::APP_EXECUTE;
		FindTarget( strFile );
		if ( m_pRequest->m_uStatus == 0 )
		{
			bWait=StartSvrApp();
        } else {
            StuffError( IDS_STATUS_NOTIMPL );
        }
	}
	else
		StuffError( IDS_STATUS_NOTIMPL );

	// notify the active view of the hit....
	m_pDoc->DocHit( m_pRequest );
	return bWait;
}

BOOL CRequestSocket::FindTarget( CString& strFile )
{
	BOOL bFound = FALSE;
	strFile = m_pRequest->m_strURL;
	// change from URL to local file system path....
	if ( URLtoPath( strFile ) )
	{
		CString strExtra; // extra path info
		m_pRequest->m_dwAttr = GetFileAttributes( strFile );
		if ( m_pRequest->m_dwAttr != INVALID_FILE_ATTRIBUTES )
			bFound = TRUE;
		else
		{
			// rip off the last portion....
			strExtra = StripLast( strFile );
			while( !strFile.IsEmpty() )
			{
				// anything there?
				m_pRequest->m_dwAttr = GetFileAttributes( strFile );
				if ( m_pRequest->m_dwAttr != -1 )
				{
					// found something; better not be a folder....
					if( (m_pRequest->m_dwAttr&FILE_ATTRIBUTE_DIRECTORY) == 0 )
						bFound = TRUE;
					break;
				}
				// rip off the next portion....
				strExtra = StripLast( strFile ) + strExtra;
			}
		}

		if ( bFound )
		{
			// strip any trailing SEPCHAR....
			if ( strFile.GetAt( strFile.GetLength()-1) == SEPCHAR )
				m_pRequest->m_strFullPath = strFile.Left( strFile.GetLength()-1 );
			else
				m_pRequest->m_strFullPath = strFile;

			// see if we need to set the extra path info....
			if ( !strExtra.IsEmpty() )
			{
				m_pRequest->m_strPathInfo = strExtra;
				if ( URLtoPath( strExtra ) )
					m_pRequest->m_strPathTranslated = strExtra;
			}

			// if it's a folder, see if we can redirect to
			// on of the default docs or apps....
			if ( m_pRequest->m_dwAttr & FILE_ATTRIBUTE_DIRECTORY )
			{
				// check for existence of a default doc or app....
				if ( !CheckDefault( IDS_DEFAULTDOC, FALSE ) )
					CheckDefault( IDS_DEFAULTAPP, TRUE );
			}
			else if ( m_pRequest->m_dwExecute && !IsSvrApp() )
			{
				StuffError( IDS_STATUS_BADREQUEST );
			}
		}
		else
			StuffError( IDS_STATUS_NOTFOUND );
	}
	else
		StuffError( IDS_STATUS_BADREQUEST );

	return bFound;
}

BOOL CRequestSocket::URLtoPath( CString& strFile )
{
	BOOL bLegal = FALSE;
	CString& strRoot = m_pDoc->m_strRoot;

	// start with the root, append the abs path....
	CString strTemp = strRoot + strFile;
	// now canonicalize it....
	DWORD dwSize = GetFullPathName( strTemp, MAX_PATH, strFile.GetBuffer(MAX_PATH+1), NULL );
	strFile.ReleaseBuffer();

	// get the full path okay?
	if ( dwSize != 0 && dwSize <= MAX_PATH )
	{
		int cchRoot = strRoot.GetLength();
		int cchFile = strFile.GetLength();
		// must be the same or longer than the root....
		if ( cchRoot == cchFile )
		{
			// must be exactly the same....
			if ( strRoot.Compare( strFile ) == 0 )
				bLegal = TRUE;
		}
		else if ( cchRoot < cchFile )
		{
			// must have the root as the base....
			if ( strRoot.Compare( strFile.Left(cchRoot) ) == 0
				&& strFile.GetAt( cchRoot ) == SEPCHAR )
				bLegal = TRUE;
		}
	}

	return bLegal;
}

BOOL CRequestSocket::PathToURL( CString& strFile )

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
欧美大白屁股肥臀xxxxxx| aaa欧美大片| 日韩三级高清在线| 久久精品国产亚洲5555| 久久蜜桃av一区精品变态类天堂| 精品在线亚洲视频| 国产精品久久久久久亚洲毛片| 成人黄色电影在线| 一区二区三区四区激情| 欧美剧情片在线观看| 久热成人在线视频| 国产精品三级av| 在线精品视频免费观看| 美国欧美日韩国产在线播放| 亚洲国产成人在线| 欧美伊人久久久久久午夜久久久久| 日韩精品成人一区二区三区 | 麻豆精品视频在线观看视频| 精品国产一区久久| 丁香亚洲综合激情啪啪综合| 一区二区三区在线视频播放| 91精品国产一区二区三区蜜臀| 国产曰批免费观看久久久| 亚洲三级免费观看| 日韩美女在线视频| 972aa.com艺术欧美| 毛片不卡一区二区| 中文字幕日本乱码精品影院| 日韩精品中文字幕在线一区| 99re热视频这里只精品| 美女视频黄免费的久久| 亚洲免费观看在线视频| 精品国产sm最大网站免费看| 91久久久免费一区二区| 国产在线国偷精品免费看| 亚洲综合另类小说| 久久久久久免费毛片精品| 欧美伊人久久久久久午夜久久久久| 国产福利91精品一区| 偷拍日韩校园综合在线| 日韩美女啊v在线免费观看| 欧美不卡视频一区| 欧美午夜宅男影院| www.欧美色图| 国产精品主播直播| 日韩av成人高清| 亚洲精品视频在线看| 国产欧美一区二区在线| 日韩一级完整毛片| 欧美日韩国产成人在线91| 99久久99久久久精品齐齐| 国内精品免费在线观看| 日韩精品久久理论片| 亚洲黄色免费网站| 国产精品欧美一区二区三区| 久久综合色天天久久综合图片| 9191久久久久久久久久久| 色视频欧美一区二区三区| 粉嫩嫩av羞羞动漫久久久| 激情小说亚洲一区| 蜜桃视频在线观看一区| 天天综合天天做天天综合| 夜夜揉揉日日人人青青一国产精品| 欧美国产激情一区二区三区蜜月| 久久久一区二区三区捆绑**| 欧美本精品男人aⅴ天堂| 欧美高清视频www夜色资源网| 在线视频国内一区二区| 欧美在线不卡一区| 一本在线高清不卡dvd| 99精品热视频| 91蜜桃网址入口| 一本色道a无线码一区v| 在线视频亚洲一区| 欧美日韩国产免费| 欧美日韩国产成人在线91| 在线不卡a资源高清| 6080午夜不卡| www久久精品| 国产精品美女久久久久久久| 国产精品欧美久久久久一区二区| 国产精品国产三级国产aⅴ入口| 中文字幕一区二区三区视频| 亚洲欧美激情在线| 亚洲色图.com| 午夜欧美电影在线观看| 日本网站在线观看一区二区三区| 蜜桃传媒麻豆第一区在线观看| 麻豆视频观看网址久久| 国产乱子轮精品视频| 成人黄色小视频在线观看| 一本久久a久久免费精品不卡| 欧美三级韩国三级日本三斤| 日韩亚洲欧美高清| 国产婷婷精品av在线| 亚洲久本草在线中文字幕| 亚洲电影一区二区| 久久99国产精品尤物| 国产69精品久久777的优势| 91免费版pro下载短视频| 555www色欧美视频| 日本一区二区三区视频视频| 亚洲综合成人在线| 精品在线播放午夜| jvid福利写真一区二区三区| 欧美日本一区二区| 久久久美女毛片| 亚洲精品ww久久久久久p站| 热久久免费视频| 高潮精品一区videoshd| 欧洲一区二区av| 欧美精品一区二区精品网| 一区二区三区色| 国模冰冰炮一区二区| 日本久久电影网| 亚洲精品一区二区三区香蕉| 亚洲激情五月婷婷| 久久精品久久99精品久久| 91麻豆精品一区二区三区| 日韩免费观看高清完整版| 亚洲欧美日韩一区二区| 久久精品国产成人一区二区三区 | 91蝌蚪porny成人天涯| 91精品国产91综合久久蜜臀| 中文字幕第一区综合| 日本伊人午夜精品| 91美女精品福利| 国产视频一区二区在线观看| 亚洲va韩国va欧美va精品| 成人app软件下载大全免费| 日韩欧美中文一区| 一区二区三区在线影院| 国产成a人亚洲| 欧美一区二区黄色| 亚洲色图在线看| 国产福利一区二区| 欧美一区二区三区在线电影| 亚洲女同女同女同女同女同69| 国产麻豆精品视频| 日韩一卡二卡三卡国产欧美| 亚洲电影视频在线| 91久久精品一区二区三| 国产精品久久久久久久久久久免费看| 免费高清在线一区| 欧美三电影在线| 一级女性全黄久久生活片免费| bt7086福利一区国产| 国产欧美日韩另类一区| 久久精品国产久精国产爱| 欧美麻豆精品久久久久久| 亚洲午夜一区二区| 99久久久精品| 国产精品久久久久三级| 成人在线视频一区二区| 国产欧美一区二区精品婷婷| 久久成人18免费观看| 日韩欧美国产一区在线观看| 午夜免费欧美电影| 欧美在线一区二区| 亚洲一区二区成人在线观看| 色欧美片视频在线观看| 尤物在线观看一区| 在线观看亚洲a| 亚洲精品乱码久久久久久黑人 | 国产一区二区剧情av在线| 欧美一区二区三区视频在线| 亚洲高清不卡在线观看| 欧美自拍丝袜亚洲| 一区二区三区国产豹纹内裤在线| 91福利在线导航| 亚洲不卡av一区二区三区| 欧美色图一区二区三区| 亚洲大片一区二区三区| 在线播放中文字幕一区| 麻豆精品一区二区三区| 久久影院午夜片一区| 国产99久久久国产精品| 国产精品蜜臀av| 色噜噜夜夜夜综合网| 亚洲大片免费看| 日韩午夜在线影院| 国产精品888| 亚洲欧美一区二区三区极速播放| 91久久国产最好的精华液| 亚洲成人在线免费| 欧美岛国在线观看| 国产成人午夜99999| 亚洲三级电影全部在线观看高清| 色婷婷激情一区二区三区| 天堂一区二区在线免费观看| 欧美大片在线观看| 成人av第一页| 偷窥少妇高潮呻吟av久久免费| 26uuu另类欧美| 一本大道久久a久久综合婷婷| 日韩国产欧美在线播放| 国产喷白浆一区二区三区| 在线观看国产日韩| 久久成人免费网| 一区二区在线观看不卡|