-
Notifications
You must be signed in to change notification settings - Fork 13
Request Handlers
As mentioned above, any Route may have some handlers to handle incoming requests. Any request handler should be a class derived from RequestHandler class:
class RequestHandler {
public:
virtual Response *callback(Request *req) = 0;
};RequestHandler is a pure abstract class which has only one function: callback which should be overridden.
Some useful request handlers or serving static files are provided in library:
-
ShowFile: takesfilePathandfileType(MIME type) parameters in it's constructor and serves related file -
ShowPage: takes afilePathparameter in it's constructor and servestextpage, specially an .html page -
ShowImage: takes afilePathparameter in it's constructor and servesimage
Also, you can write your own handlers.
You should write your own request handler to server dynamic pages. Your handler should inherits from RequestHandler class and override callback function. callback function take a Request *, which comes from client-side, and give a Response *, which will be send to client-side.
You should create an appropriate Response * and return it while overriding this function.
This is an example which serves a dynamic html page:
class RandomNumberHandler : public RequestHandler {
public:
Response *callback(Request *req) {
Response *res = new Response;
res->setHeader("Content-Type", "text/html");
string body;
body += "<!DOCTYPE html>";
body += "<html>";
body += "<body style=\"text-align: center;\">";
body += "<h1>AP HTTP</h1>";
body += "<p>";
body += "a random number in [1, 10] is: ";
body += to_string(rand() % 10 + 1);
body += "</p>";
body += "</body>";
body += "</html>";
res->setBody(body);
return res;
}
};To get data from client-side, specially using a POST request, you should write your own handler, too. You should return a Response * as well at the end of callback function. Sometimes you should redirect client to a new address after handling POST request.
You may want to redirect client to a new page when handling a request, specially after handling a POST request. Response::redirect(std::string url) is a static function of Response class which returns a Response * which will redirects client to url and can be returned in a RequestHandler::callback.
class LoginHandler : public RequestHandler {
public:
Response *callback(Request *req) {
string username = req->getBodyParam("username");
string password = req->getBodyParam("password");
// do something with username and password
return Response::redirect("/");
}
};You should write your own TemplateHandler to serve template files. This class extended from RequestHandler and implemented it's callback function. You should implement handle method. This method get a Request* to have access to client data like other handlers and it returns a map<string, string> . You have access to this map with name context in template file.
map<string, string> ColorHandler::handle(Request *req) {
map<string, string> context;
string newName = "I am " + req->getQueryParam("name");
context["name"] = newName;
context["color"] = req->getQueryParam("color");
return context;
}###Template Files
Template file system makes it possible to separate c++ and HTML, the c++ goes in TemplateHandlers and HTML goes in templates. You can have some c++ code between <% and %> tags in html files.
For example to show our name in a custom color from previous example you can have something like this.
<html>
<body>
<%
if(context["color"] == "red")
{
%>
<div style="color:red;">
<%
cout << context["name"];
%>
</div>
<%
}
%>
</body>
</html>NOTES:
- You have access to
iostreamandstringincludes in template files. - Do not use dependent codes in seprate blocks. Like an
ifin one code block and it'selse ifin other code block.
You can throw Server::Exception inside request handlers, which will be shown as JSON error responses.
AP HTTP
University of Tehran / Advanced Programming
- Quick How-to & Examples
- Creating a New Server
-
Request Handlers
- Serving Static Files
- Serving Dynamic Files
- Getting Data from Client-side
- Redirection
- Serving Template Files
- Template Files
-
Request- Request-Line
- Query
- Haeder
- Body
-
Response- Status-Line
- Header
- Body
- Session
- Utilities
- Compile and Run