Exemplo n.º 1
0
int CWebPageDef::ServPageRequest( CClient * pClient, LPCTSTR pszURLArgs, CGTime * pdateIfModifiedSince )
{
	ADDTOCALLSTACK("CWebPageDef::ServPageRequest");
	UNREFERENCED_PARAMETER(pszURLArgs);
	// Got a web page request from the client.
	// ARGS:
	//  pszURLArgs = args on the URL line ex. http://www.hostname.com/dir?args
	// RETURN:
	//  HTTP error code = 0=200 page was served.

	ASSERT(pClient);

	if ( HasTrigger(WTRIG_Load))
	{
		CResourceLock s;
		if ( ResourceLock(s))
		{
			if (CScriptObj::OnTriggerScript( s, sm_szTrigName[WTRIG_Load], pClient, NULL ) == TRIGRET_RET_TRUE)
				return( 0 );	// Block further action.
		}
	}

	if ( m_privlevel )
	{
		if ( !pClient->m_pAccount )
			return 401;	// Authorization required
		if ( pClient->GetPrivLevel() < m_privlevel )
			return 403;	// Forbidden
	}

	CGTime datetime = CGTime::GetCurrentTime();

	LPCTSTR pszName;
	bool fGenerate = false;

	if ( m_type == WEBPAGE_TEMPLATE ) // my version of cgi
	{
		pszName = GetDstName();
		if ( pszName[0] == '\0' )
		{
			pszName = "temppage.htm";
			fGenerate = true;
		}
		else
		{
			fGenerate = ! m_iUpdatePeriod;
		}

		// The page must be generated on demand.
		if ( ! WebPageUpdate( fGenerate, pszName, pClient ))
			return 500;
	}
	else
	{
		pszName = GetName();
	}

	// Get proper Last-Modified: time.
	time_t dateChange;
	DWORD dwSize;
	if ( ! CFileList::ReadFileInfo( pszName, dateChange, dwSize ))
	{
		return 500;
	}

	const char *sDate = datetime.FormatGmt(NULL);	// current date.

	if ( !fGenerate && !pdateIfModifiedSince && (pdateIfModifiedSince->IsTimeValid() && pdateIfModifiedSince->GetTime() > dateChange) )
	{
		TCHAR *pszTemp = Str_GetTemp();
		sprintf(pszTemp, "HTTP/1.1 304 Not Modified\r\nDate: %s\r\nServer: " GRAY_TITLE " V " GRAY_VERSION "\r\nContent-Length: 0\r\n\r\n", sDate);
		new PacketWeb(pClient, (BYTE*)pszTemp, strlen(pszTemp));
		return 0;
	}

	// Now serve up the page.
	CGFile FileRead;
	if ( ! FileRead.Open( pszName, OF_READ|OF_BINARY ))
		return 500;

	// Send the header first.
	TCHAR szTmp[8*1024];
	size_t iLen = sprintf(szTmp,
		"HTTP/1.1 200 OK\r\n" // 100 Continue
		"Date: %s\r\n"
		"Server: " GRAY_TITLE " V " GRAY_VERSION "\r\n"
		"Accept-Ranges: bytes\r\n"
		"Content-Type: %s\r\n",
		static_cast<LPCTSTR>(sDate),
		static_cast<LPCTSTR>(sm_szPageType[m_type]) // type of the file. image/gif, image/x-xbitmap, image/jpeg
		);

	if ( m_type == WEBPAGE_TEMPLATE )
		iLen += sprintf(szTmp + iLen, "Expires: 0\r\n");
	else
		iLen += sprintf(szTmp + iLen, "Last-Modified: %s\r\n",  CGTime(dateChange).FormatGmt(NULL));

	iLen += sprintf( szTmp + iLen,
		"Content-Length: %lu\r\n"
		"\r\n",
		dwSize
		);

	PacketWeb packet;
	packet.setData((BYTE*)szTmp, iLen);
	packet.send(pClient);

	for (;;)
	{
		iLen = FileRead.Read( szTmp, sizeof( szTmp ) );
		if ( iLen <= 0 )
			break;
		packet.setData((BYTE*)szTmp, iLen);
		packet.send(pClient);
		//dwSize -= iLen;
		if ( iLen < sizeof( szTmp ) )
		{
			// memset( szTmp+iLen, 0, sizeof(szTmp)-iLen );
			break;
		}
	}
	return 0;
}
Exemplo n.º 2
0
bool CClient::OnRxAxis( const byte * pData, size_t iLen )
{
	ADDTOCALLSTACK("CClient::OnRxAxis");
	if ( !iLen || ( GetConnectType() != CONNECT_AXIS ))
		return false;

	while ( iLen -- )
	{
		int iRet = OnConsoleKey( m_Targ_Text, *pData++, GetAccount() != NULL );
		if ( ! iRet )
			return false;
		if ( iRet == 2 )
		{
			if ( GetAccount() == NULL )
			{
				if ( !m_zLogin[0] )
				{
					if ( (uint)(m_Targ_Text.GetLength()) <= (CountOf(m_zLogin) - 1) )
						strcpy(m_zLogin, m_Targ_Text);
					m_Targ_Text.Empty();
				}
				else
				{
					CSString sMsg;

					CAccountRef pAccount = g_Accounts.Account_Find(m_zLogin);
					if (( pAccount == NULL ) || ( pAccount->GetPrivLevel() < PLEVEL_Counsel ))
					{
						SysMessagef("\"MSG:%s\"", g_Cfg.GetDefaultMsg(DEFMSG_AXIS_NOT_PRIV));
						m_Targ_Text.Empty();
						return false;
					}
					if ( LogIn(m_zLogin, m_Targ_Text, sMsg ) == PacketLoginError::Success )
					{
						m_Targ_Text.Empty();
						if ( GetPrivLevel() < PLEVEL_Counsel )
						{
							SysMessagef("\"MSG:%s\"", g_Cfg.GetDefaultMsg(DEFMSG_AXIS_NOT_PRIV));
							return false;
						}
						if (GetPeer().IsValidAddr())
						{
							CScriptTriggerArgs Args;
							Args.m_VarsLocal.SetStrNew("Account",GetName());
							Args.m_VarsLocal.SetStrNew("IP",GetPeer().GetAddrStr());
							TRIGRET_TYPE tRet = TRIGRET_RET_DEFAULT;
							r_Call("f_axis_preload", this, &Args, NULL, &tRet);
							if ( tRet == TRIGRET_RET_FALSE )
								return false;
							if ( tRet == TRIGRET_RET_TRUE )
							{
								SysMessagef("\"MSG:%s\"", g_Cfg.GetDefaultMsg(DEFMSG_AXIS_DENIED));
								return false;
							}

							time_t dateChange;
							dword dwSize;
							if ( ! CSFileList::ReadFileInfo( "Axis.db", dateChange, dwSize ))
							{
								SysMessagef("\"MSG:%s\"", g_Cfg.GetDefaultMsg(DEFMSG_AXIS_INFO_ERROR));
								return false;
							}

							CSFile FileRead;
							if ( ! FileRead.Open( "Axis.db", OF_READ|OF_BINARY ))
							{
								SysMessagef("\"MSG:%s\"", g_Cfg.GetDefaultMsg(DEFMSG_AXIS_FILE_ERROR));
								return false;
							}

							tchar szTmp[8*1024];
							PacketWeb packet;
							for (;;)
							{
								size_t iLength = FileRead.Read( szTmp, sizeof( szTmp ) );
								if ( iLength <= 0 )
									break;
								packet.setData((byte*)szTmp, iLength);
								packet.send(this);
								dwSize -= (dword)iLength;
								if ( dwSize <= 0 )
									break;
							}
							return true;
						}
						return false;
					}
					else if ( ! sMsg.IsEmpty())
					{
						SysMessagef("\"MSG:%s\"", (lpctstr)sMsg);
						return false;
					}
					m_Targ_Text.Empty();
				}
				return true;
			}
		}
	}
	return true;
}