Beispiel #1
0
bool CWebPageDef::ServPage( CClient * pClient, tchar * pszPage, CSTime * pdateIfModifiedSince )	// static
{
	ADDTOCALLSTACK("CWebPageDef::ServPage");
	// make sure this is a valid format for the request.

	tchar szPageName[_MAX_PATH];
	Str_GetBare( szPageName, pszPage, sizeof(szPageName), "!\"#$%&()*,:;<=>?[]^{|}-+'`" );

	int iError = 404;
	CWebPageDef * pWebPage = g_Cfg.FindWebPage(szPageName);
	if ( pWebPage )
	{
		iError = pWebPage->ServPageRequest(pClient, szPageName, pdateIfModifiedSince);
		if ( ! iError )
			return true;
	}

	// Is it a file in the Script directory ?
	if ( iError == 404 )
	{
		const CResourceID ridjunk( RES_UNKNOWN, 1 );
		CWebPageDef tmppage( ridjunk );
		if ( tmppage.SetSourceFile( szPageName, pClient ))
		{
			if ( !tmppage.ServPageRequest(pClient, szPageName, pdateIfModifiedSince) )
				return true;
		}
	}

	// Can't find it !?
	// just take the default page. or have a custom 404 page ?

	pClient->m_Targ_Text = pszPage;

	tchar	*pszTemp = Str_GetTemp();
	sprintf(pszTemp, SPHERE_FILE "%d.htm", iError);
	pWebPage = g_Cfg.FindWebPage(pszTemp);
	if ( pWebPage )
	{
		if ( ! pWebPage->ServPageRequest( pClient, pszPage, nullptr ))
			return true;
	}

	// Hmm we should do something !!!?
	// Try to give a reasonable default error msg.

	lpctstr pszErrText;
	switch (iError)
	{
		case 401: pszErrText = "Authorization Required"; break;
		case 403: pszErrText = "Forbidden"; break;
		case 404: pszErrText = "Object Not Found"; break;
		case 500: pszErrText = "Internal Server Error"; break;
		default: pszErrText = "Unknown Error"; break;
	}

	CSTime datetime = CSTime::GetCurrentTime();
	const char *sDate = datetime.FormatGmt(nullptr);
	CSString sMsgHead;
	CSString sText;

	sText.Format(
		"<html><head><title>Error %d</title>"
		"<meta name=robots content=noindex>"
		"</head><body>"
		"<h2>HTTP Error %d</h2><p><strong>%d %s</strong></p>"
		"<p>The " SPHERE_TITLE " server cannot deliver the file or script you asked for.</p>"
		"<p>Please contact the server's administrator if this problem persists.</p>"
		"</body></html>",
		iError,
		iError,
		iError,
		pszErrText);

	sMsgHead.Format(
		"HTTP/1.1 %d %s\r\n"
		"Date: %s\r\n"
		"Server: " SPHERE_TITLE " " SPHERE_VERSION_PREFIX SPHERE_VERSION "\r\n"
		"Content-Type: text/html\r\n"
		"Content-Length: %d\r\n"
		"Connection: close\r\n"
		"\r\n%s",
		iError, pszErrText,
		static_cast<lpctstr>(sDate),
		sText.GetLength(),
		static_cast<lpctstr>(sText));

	new PacketWeb(pClient, reinterpret_cast<const byte *>(sMsgHead.GetPtr()), sMsgHead.GetLength());
	return false;
}
Beispiel #2
0
int CTextConsole::OnConsoleKey( CSString & sText, tchar nChar, bool fEcho )
{
    ADDTOCALLSTACK("CTextConsole::OnConsoleKey");
    // eventaully we should call OnConsoleCmd
    // RETURN:
    //  0 = dump this connection.
    //  1 = keep processing.
    //  2 = process this.

    if ( sText.GetLength() >= SCRIPT_MAX_LINE_LEN )
    {
        commandtoolong:
        SysMessage( "Command too long\n" );

        sText.Empty();
        return 0;
    }

    if ( nChar == '\r' || nChar == '\n' )
    {
        // Ignore the character if we have no text stored
        if (!sText.GetLength())
            return 1;

        if ( fEcho )
        {
            SysMessage("\n");
        }
        return 2;
    }
    else if ( nChar == 9 )			// TAB (auto-completion)
    {
        lpctstr p = nullptr;
        lpctstr tmp = nullptr;
        size_t inputLen = 0;
        bool matched(false);

        //	extract up to start of the word
        p = sText.GetPtr() + sText.GetLength();
        while (( p >= sText.GetPtr() ) && ( *p != '.' ) && ( *p != ' ' ) && ( *p != '/' ) && ( *p != '=' )) p--;
        p++;
        inputLen = strlen(p);

        // search in the auto-complete list for starting on P, and save coords of 1st and Last matched
        CSStringListRec	*firstmatch = nullptr, *lastmatch = nullptr;
        CSStringListRec	*curmatch = nullptr, *nextmatch = nullptr;	// the one that should be set
        for ( curmatch = g_AutoComplete.GetHead(); curmatch != nullptr; curmatch = nextmatch )
        {
            nextmatch = curmatch->GetNext();
            if ( !strnicmp(curmatch->GetPtr(), p, inputLen) )	// matched
            {
                if ( firstmatch == nullptr )
                    firstmatch = lastmatch = curmatch;
                else
                    lastmatch = curmatch;
            }
            else if ( lastmatch )   // if no longer matches - save time by instant quit
                break;
        }

        if (( firstmatch != nullptr ) && ( firstmatch == lastmatch ))	// there IS a match and the ONLY
        {
            tmp = firstmatch->GetPtr() + inputLen;
            matched = true;
        }
        else if ( firstmatch != nullptr )						// also make SE (if SERV/SERVER in dic) to become SERV
        {
            p = tmp = firstmatch->GetPtr();
            tmp += inputLen;
            inputLen = strlen(p);
            matched = true;
            for ( curmatch = firstmatch->GetNext(); curmatch != lastmatch->GetNext(); curmatch = curmatch->GetNext() )
            {
                if (strnicmp(curmatch->GetPtr(), p, inputLen) != 0)	// mismatched
                {
                    matched = false;
                    break;
                }
            }
        }

        if ( matched )
        {
            if ( fEcho )
                SysMessage(tmp);

            sText += tmp;
            if ( sText.GetLength() > SCRIPT_MAX_LINE_LEN )
                goto commandtoolong;
        }
        return 1;
    }

    if ( fEcho )
    {
        // Echo
        tchar szTmp[2];
        szTmp[0] = nChar;
        szTmp[1] = '\0';
        SysMessage( szTmp );
    }

    if ( nChar == 8 )
    {
        if ( sText.GetLength())	// back key
        {
            sText.SetLength( sText.GetLength() - 1 );
        }
        return 1;
    }

    sText += nChar;
    return 1;
}