Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#34 HTTPRequestHandler Interface

Release_1.3.6
closed
nobody
Other (7)
5
2012-09-14
2007-02-09
Alex Fabijanic
No

While working with classes derived from HTTPRequestHandler, I constantly find myself having to pass around request/response references. To avoid having many functions taking same arguments, I cache them in member variables (pointers) in handleRequest :

void MyHandler::handleRequest(request, response)
{
_pRequest = &request;
_pResponse = &response;
...
}

and later retrieve references using

HTTPServerRequest& MyRequestHandler::request()
{
return *_pRequest;
}

and MyRequestHandler::response() in similar fashion. I also do something like that with MyRequestHandler::form() - the only difference is that form() uses lazy creation like this:

HTMLForm& MyRequestHandler::form()
{
if (!_pForm)
_pForm = new HTMLForm(request(), request().stream());

return *_pForm;
}

My proposal is to push this functionality up the hierarchy into HTTPRequestHandler by adding:

  • three private mebers:

TTPServerRequest _pRequest;
TTPServerResponse
_pResponse;
HTMLForm* _pForm;

  • two public functions (called from HTTPServerConnection::run() prior to invoking HTTPRequestHandler::handleRequest()):

void HTTPrequestHandler::setRequest(HTTPServerRequest&);
void HTTPrequestHandler::setResponse(HTTPServerResponse&);

  • and following three protected member functions for descendant's convenience:

HTTPServerRequest& HTTPrequestHandler::getRequest();
HTTPServerResponse& HTTPrequestHandler::getResponse();
HTMLForm& form();

From my own experience, this simplifies developer's life when writing handlers. If this is implemented, handleRequest() does not need arguments any more, but it does not hurt to keep it as is in oder not to break the existing code.

I'm aso posting this in forum, since the formatting capabilities of this facility are dismal.

Alex

Discussion

  • Alex Fabijanic
    Alex Fabijanic
    2007-02-09

    Logged In: YES
    user_id=1001095
    Originator: YES

    An afterthought:

    A better place for form() is probably HTTPServerRequest.

    Alex

     
  • Logged In: YES
    user_id=1148207
    Originator: NO

    POCO 1.3 will have an AbstractHTTPRequestHandler class:

    class AbstractHTTPRequestHandler: public HTTPRequestHandler
    {
    public:
    virtual void run() = 0;
    // called by handleRequest()

    protected:
    virtual bool authenticate();
    // check authentication; returns true if okay, false if failed to authenticate

    void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response);
        /// save request and response
        /// call authorize();
        /// if returns true call run() 
        /// else sends 401 response
    
    HTTPServerRequest& request();
    HTTPServerResponse& response();
    HTMLForm& form();
    

    };