ot::web
class HttpRequestHandler (abstract)
#include "ot/web/HttpRequestHandler.h"
An abstract interface class employed by HttpClient to facilitate the customized processing of HTTP requests..
This class has four pure virtual functions, each representing one of the four phases that a HTTP request goes through:--
Initialization
-
Pre-processing, where the request may be modified or satisfied locally before being sent to the server. An example of this may be a caching handler which can satisfy HTTP requests from a local file cache.
-
Response-header processing, where the response line and MIME-type headers returned from the server are interpreted, possibly resulting in the need for a new HTTP request (e.g. following an Authorization failure).
-
Response-body processing, where the request can no longer be modified, but the body of the server's response may be interpreted and modified.
The following example shows how an application can customize the processing of a HTTP request my registering a handler with a HttpClient:-
#include "ot/web/HttpClient.h"
#include "ot/web/HttpRequestHandler.h"
#include "ot/base/SystemMonitor.h"
#include <iostream>
using namespace ot;
using namespace ot::web;
using namespace std;
class MyLoggingHandler : public HttpRequestHandler
{
virtual void newRequest(const HttpRequest& request, size_t requestNumber)
{
// log the request using some snazzy logging function...
logRequest(request);
}
virtual NextAction handleRequest(HttpRequest& request, HttpResponse& response)
{
return Continue;
}
virtual NextAction handleResponseHeaders(HttpRequest& request, HttpResponse& response)
{
return Continue;
}
virtual void handleResponseBody(const HttpRequest& request, HttpResponse& response)
{
}
};
int main(int argc, char* argv[])
{
SystemMonitor monitor;
try
{
//
// enable active connection management for OpenTop's HTTP implementation
//
HttpClient http;
http.registerHandler(new MyLoggingHandler);
HttpRequest req(URL("http://www.elcel.com"));
HttpResponse response = http.sendRequest(req);
cout << "The response from " << response.getFinalURL().toExternalForm()
<< " was " << response.getResponseLine() << endl;
}
catch(Exception& e)
{
cerr << e.toString() << endl;
}
return 0;
}
- See also:
-
HttpClient
- Since:
-
OpenTop 1.4
handleRequest
virtual HttpRequestHandler::NextAction handleRequest(HttpRequest& request,
HttpResponse& response)=0
-
Called by the HttpClient before it sends a HTTP request to a server, giving this registered handler an opportunity to handle or modify the request.
This method must return one of the following values:-
| Return value | Meaning |
| HttpRequestHandler::Continue | Indicates that this handler either has no interest in, or has made only minor modifications to the contents of the request, and that it should continue to be passed to other registered handlers before being sent to the HTTP server. |
| HttpRequestHandler::NewRequest | Indicates that the handler has replaced the request with a new one. An example of may be where the request must be redirected to a mirror host. |
| HttpRequestHandler::PhaseComplete | Indicates that this handler has completely satisfied this phase of the request, which means that the HttpClient will skip the other registered handlers and will not sent this request to the HTTP server. This handler should place sufficient information in response to simulate a real HTTP response. |
| HttpRequestHandler::RequestComplete | Similar to PhaseComplete, but additionally the HttpClient will not pass the response to any of the registered handlers. This will effectively cause the response to be passed directly back to the application. |
- Parameters:
request -
the HTTP request to be processed.
response -
a HttpResponse object which may be used to record response information if this method elects to handle the request completely.
- Returns:
-
a value from the HttpRequestHandler::NextAction enumeration. See the above table for details.
handleResponseBody
virtual void handleResponseBody(const HttpRequest& request,
HttpResponse& response)=0
-
Called by the HttpClient after a HTTP response has has been received, and the response line and MIME headers processed to the satisfaction of all registered handlers.
This method may process and modify the MIME headers within the response object but cannot further modify the request.
- Parameters:
request -
the HTTP request being processed.
response -
the HTTP response received from the HTTP server.
handleResponseHeaders
virtual HttpRequestHandler::NextAction handleResponseHeaders(HttpRequest& request,
HttpResponse& response)=0
-
Called by the HttpClient after a HTTP request has been sent to the server and the response header has been parsed.
This method may process and modify the MIME headers within the response object and must return one of the following values:-
| Return value | Meaning |
| HttpRequestHandler::Continue | Indicates that this handler either has no interest in, or has made only minor modifications to the contents of the response, and that it should continue to be passed to other registered handlers. The request object should not be modified when the handler returns this code. |
| HttpRequestHandler::NewRequest | Indicates that the handler has replaced the request with a new one. An example of may be where the original response contained a redirect response code. |
| HttpRequestHandler::PhaseComplete | Indicates that this handler has completely satisfied this phase of the request, which means that the HttpClient will skip the other registered handlers for this phase. |
| HttpRequestHandler::RequestComplete | Similar to PhaseComplete, but additionally the HttpClient will not pass the response body to any of the registered handlers. This will effectively cause the response to be passed directly back to the application. |
- Parameters:
request -
the HTTP request being processed.
response -
a HttpResponse received from the HTTP server.
- Returns:
-
a value from the above table.
newRequest
virtual void newRequest(const HttpRequest& request,
size_t requestNumber)=0
-
Called before a new HTTP request is processed.
The HttpClient class calls newRequest() on all registered handlers before starting to process the request in order to give them all the opportunity to perform per-request initialization.
- Parameters:
request -
the request that is about to be processed.
requestNumber -
a zero-based index which is incremented each time a new HTTP request is generated. The HttpRequest passed from the application will have a requestNumber of zero, whereas sub-requests generated as a result of this request (e.g. requests generated by authorization or redirection headers) will be incremented by 1 more than the request that caused it to be generated.
Found a bug or missing feature? Please email us at support@elcel.com