Example #1
0
Command::Command(const char* first, const char* last) throw(parse_error)
		: from(INVALID_SID), to(INVALID_SID), dirty(false), full(first, last)
{
	initNumPosParams();
	// optimize later
	// this is lazy due to a DC++ bug that sends empty parameters...
	// ideally, we should not be using it
	StringList sl = Util::lazyStringTokenize(full);
	full += '\n';
	if(full.size() < 5 || sl[0].size() != 4)
		throw parse_error("invalid message type");
	action = sl[0][0];
	cmd = (CmdInt)(stringToFourCC(sl[0]) & 0xFFFFFF00);
	StringList::size_type loc;	// message parameters start at this index

	switch(action) {
	case 'F':
		from = ADC::toSid(sl[1]);
		features = sl[2];
		checkFeatures();
		loc = 3;
		break;
	case 'D':
	case 'E':
		from = ADC::toSid(sl[1]);
		to = ADC::toSid(sl[2]);
		loc = 3;
		break;
	case 'B':
	case 'S':
		from = ADC::toSid(sl[1]);
		loc = 2;
		break;
	case 'I':
	case 'H':
	case 'L':
		loc = 1;
		break;
	default:
		throw parse_error(string("invalid action type '") + action + '\'');
	}

	if (loc >= sl.size())
		throw parse_error("not enough tokens for message type");
	if (numPosParams.count(cmd)) {
		// known command -- check that positional and named params are correct
		if (loc + numPosParams[cmd] > sl.size())
			throw parse_error("missing parameters");
		for (StringList::iterator i = sl.begin() + loc + numPosParams[cmd]; i != sl.end(); ++i) {
			// param name parts can't be escaped chars
			if (i->size() < 2 || ADC::CSE(i->substr(0, 2)).size() != 2)
				throw parse_error("invalid named parameter: " + *i);
		}
	}
	params.resize(sl.size() - loc);
	transform(sl.begin() + loc, sl.end(), params.begin(), &ADC::CSE);
}
Example #2
0
// creates a page based on user input -- either displays data from
//   form or presents a form for users to submit data.
ResponseCode FormTester::HandleRequest ( HttpRequest * ipoHttpRequest,
        HttpResponse * ipoHttpResponse )
{
    char psHtml[ 5000 ];


    // if we got data from the user, show it
    if ( ipoHttpRequest->oFormValueMap [ "user" ].sBody.length ( ) ||
            ipoHttpRequest->oFormValueMap [ "existinguser" ].sBody.length ( ) ) {

        std::string sName;

        sName = ipoHttpRequest->oFormValueMap [ "existinguser" ].sBody;
        if ( ipoHttpRequest->oFormValueMap [ "user" ].sBody.length() ) {
            sName = ipoHttpRequest->oFormValueMap [ "user" ].sBody;
        }

        fprintf ( stderr, "Got name of %s\n", sName.c_str ( ) );

        char * psHtml = new char [ 5000 ];
        sprintf ( psHtml, "<html><head><title>StringList</title></head>\n<body>Hi %s</body></html>", sName.c_str ( ) );
        oNameList.push_back ( sName );

        ipoHttpResponse->SetBody( psHtml, strlen( psHtml ) );
        return HTTPRESPONSECODE_200_OK;

    } else {

        // otherwise, present the form to the user to fill in
        fprintf ( stderr, "Got no form data\n" );

        // create the options for the dropdown box
        char * psOptions;
        if ( oNameList.size ( ) > 0 )
            psOptions = new char [ oNameList.size ( ) * 200 ];
        else
            psOptions = new char [1];

        psOptions [ 0 ] = '\0';

        for ( StringList::iterator oCurrentName = oNameList.begin();
                oCurrentName != oNameList.end ( );
                oCurrentName++ ) {

            char psOption [ 200 ];
            sprintf ( psOption, "<option>%s\n",
                      oCurrentName->substr ( 0, 150 ).c_str ( ) );
            strcat ( psOptions, psOption );

        }

        sprintf ( psHtml, "<html><head><title>StringList</title></head> <body>Please log in<P> <form action = \"/\" method=GET> User name: <input type = text  name = user><BR> <select name = existinguser width = 20> %s </select> <input type = submit> </form>\n",
                  psOptions );

        delete[] psOptions;

        ipoHttpResponse->SetBody( psHtml, strlen( psHtml ) );
        return HTTPRESPONSECODE_200_OK;

    }

}