Esempio n. 1
0
void FileSystemRoute::handleRequest(Poco::Net::HTTPServerRequest& request,
                                    Poco::Net::HTTPServerResponse& response)
{
    // see if we have an html file with that error code
    ofFile errorFile(_settings.getDocumentRoot() + "/" + ofToString(response.getStatus()) + ".html");

    if(errorFile.exists())
    {
        try
        {
            response.sendFile(errorFile.getAbsolutePath(),"text/html");
            return;
        }
        catch (const Poco::FileNotFoundException& exc)
        {
            ofLogVerbose("ServerRouteHandler::sendErrorResponse") << "Poco::FileNotFoundException: " << exc.code() << " " << exc.displayText();
        }
        catch (const Poco::OpenFileException& exc)
        {
            ofLogVerbose("ServerRouteHandler::sendErrorResponse") << "Poco::OpenFileException: " << exc.code() << " " << exc.displayText();
        }
        catch (const Poco::Exception& exc)
        {
            ofLogVerbose("ServerRouteHandler::sendErrorResponse") << "Exception: " << exc.code() << " " << exc.displayText();
        }
        catch (const std::exception& exc)
        {
            ofLogVerbose("ServerRouteHandler::sendErrorResponse") << "exception: " << exc.what();
        }
        catch ( ... )
        {
            ofLogVerbose("ServerRouteHandler::sendErrorResponse") << "... Unknown exception.";
        }
    }
    // if nothing is returned, then base route will get it

    BaseRoute::handleRequest(request,response);
}
Esempio n. 2
0
void Controller::handle(const std::vector<std::string>& parameters, Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response)
{
	_parameters = parameters;
	_request = &request;
	_response = &response;

	if ( _parameters.size() > 0 )
	{
		_action = _parameters.front();
		_parameters.erase(_parameters.begin());
	}
	else
	{
		setResponseStatus(Poco::Net::HTTPResponse::HTTP_BAD_REQUEST, "Invalid URI parameters");
		return;
	}

	for(std::vector<std::string>::iterator it = _parameters.begin(); it != _parameters.end(); ++it)
	{
		int pos = it->find_first_of(':');
		if ( pos != std::string::npos )
		{
			std::string name = it->substr(0, pos);
			std::string value = it->substr(pos+1);
			_namedParameters[name] = value;
		}
	}

	std::string contentType = request.getContentType();
	if ( contentType == "application/json" )
	{
		Poco::JSON::Parser parser;
		try
		{
			Poco::Dynamic::Var json = parser.parse(request.stream());
			if ( ! json.isEmpty() && json.type() == typeid(Poco::JSON::Object::Ptr) )
			{
				_data->set("filter", json.extract<Poco::JSON::Object::Ptr>());
			}
		}
		catch(Poco::JSON::JSONException& jsone)
		{
			// Make sure everything is read, otherwise this can result
			// in Bad Request error in the next call.
			Poco::NullOutputStream nos;
			Poco::StreamCopier::copyStream(request.stream(), nos);

			setResponseStatus(Poco::Net::HTTPResponse::HTTP_BAD_REQUEST, "JSON error occurred: " + jsone.displayText());
			return;
		}
	}
	else
	{
		_form.load(request, request.stream(), *this);
	}

	// Make sure everything is read, otherwise this can result
	// in Bad Request error in the next call.
	Poco::NullOutputStream nos;
	Poco::StreamCopier::copyStream(request.stream(), nos);

	beforeAction();

	if ( response.getStatus() != Poco::Net::HTTPResponse::HTTP_OK
		|| _data->has("error") )
	{
		//TODO: return error template file or json error
	}

	const ActionMap& actions = getActions();
	ActionMap::const_iterator it = actions.find(_action);
	if ( it == actions.end() )
	{
		setResponseStatus(Poco::Net::HTTPResponse::HTTP_NOT_FOUND, "Invalid action '" + _action + "' specified.");
		return;
	}

	ActionFn action = it->second;
	(this->*action)();

	afterAction();
}