Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 29 additions & 23 deletions examples/handlers.cpp
Original file line number Diff line number Diff line change
@@ -1,53 +1,59 @@
#include "handlers.hpp"

using namespace std;
#include <cstdlib>
#include <iostream>

Response* RandomNumberHandler::callback(Request* req) {
Response* res = new Response;
Response* res = new Response();
res->setHeader("Content-Type", "text/html");
string body;

std::string randomNumber = std::to_string(std::rand() % 10 + 1);
std::string body;

body += "<!DOCTYPE html>";
body += "<html>";
body += "<html lang=\"en\">";

body += "<head>";
body += " <title>Random Number Page</title>";
body += "</head>";

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 += "<p>";
body += "SessionId: ";
body += req->getSessionId();
body += "</p>";
body += " <h1>AP HTTP</h1>";
body += " <p>A random number in [1, 10] is: " + randomNumber + "</p>";
body += " <p>SessionId: " + req->getSessionId() + "</p>";
body += "</body>";

body += "</html>";
res->setBody(body);
return res;
}

Response* LoginHandler::callback(Request* req) {
string username = req->getBodyParam("username");
string password = req->getBodyParam("password");
if (username == "root")
std::string username = req->getBodyParam("username");
std::string password = req->getBodyParam("password");
if (username == "root") {
throw Server::Exception("Remote root access has been disabled.");
cout << "username: " << username << ",\tpassword: " << password << endl;
}
std::cout << "username: " << username << ",\tpassword: " << password << std::endl;
Response* res = Response::redirect("/rand");
res->setSessionId("SID");
return res;
}

Response* UploadHandler::callback(Request* req) {
string name = req->getBodyParam("file_name");
string file = req->getBodyParam("file");
std::string name = req->getBodyParam("file_name");
std::string file = req->getBodyParam("file");
utils::writeToFile(file, name);
Response* res = Response::redirect("/");
return res;
}

ColorHandler::ColorHandler(string filePath) : TemplateHandler(filePath) {}
ColorHandler::ColorHandler(const std::string& filePath)
: TemplateHandler(filePath) {}

map<string, string> ColorHandler::handle(Request* req) {
map<string, string> context;
string newName = "I am " + req->getQueryParam("name");
std::map<std::string, std::string> ColorHandler::handle(Request* req) {
std::string newName = "I am " + req->getQueryParam("name");
std::map<std::string, std::string> context;
context["name"] = newName;
context["color"] = req->getQueryParam("color");
return context;
Expand Down
15 changes: 7 additions & 8 deletions examples/handlers.hpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
#ifndef HANDLERS_HPP_INCLUDE
#define HANDLERS_HPP_INCLUDE

#include <cstdlib> // for rand and srand
#include <ctime> // for time
#include <iostream>
#include <map>
#include <string>

#include "../server/server.hpp"

class RandomNumberHandler : public RequestHandler {
public:
Response* callback(Request*);
Response* callback(Request*) override;
};

class LoginHandler : public RequestHandler {
public:
Response* callback(Request*);
Response* callback(Request*) override;
};

class UploadHandler : public RequestHandler {
public:
Response* callback(Request*);
Response* callback(Request*) override;
};

class ColorHandler : public TemplateHandler {
public:
ColorHandler(std::string filePath);
std::map<std::string, std::string> handle(Request* req);
ColorHandler(const std::string& filePath);
std::map<std::string, std::string> handle(Request* req) override;
};

#endif // HANDLERS_HPP_INCLUDE
2 changes: 1 addition & 1 deletion examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ void mapServerPaths(Server& server) {
server.setNotFoundErrPage("static/404.html");
server.get("/", new ShowPage("static/home.html"));
server.get("/home.png", new ShowImage("static/home.png"));
server.get("/rand", new RandomNumberHandler());
server.get("/login", new ShowPage("static/logincss.html"));
server.post("/login", new LoginHandler());
server.get("/up", new ShowPage("static/upload_form.html"));
server.post("/up", new UploadHandler());
server.get("/colors", new ColorHandler("template/colors.html"));
server.get("/rand", new RandomNumberHandler());
server.get("/music", new ShowPage("static/music.html"));
server.get("/music/moonlight.mp3", new ShowFile("static/moonlight.mp3", "audio/mpeg"));
}
Expand Down
24 changes: 14 additions & 10 deletions server/route.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

#include "server.hpp"

using namespace std;
Route::Route(Request::Method method, const std::string& path)
: method_(method),
path_(path),
handler_(nullptr) {}

Route::Route(Method _method, string _path) {
method = _method;
path = _path;
Route::~Route() {
delete handler_;
}

void Route::setHandler(RequestHandler* _handler) { handler = _handler; }

bool Route::isMatch(Method _method, string url) {
return (url == path) && (_method == method);
void Route::setHandler(RequestHandler* handler) {
handler_ = handler;
}

Response* Route::handle(Request* req) { return handler->callback(req); }
Response* Route::handle(Request* req) {
return handler_->callback(req);
}

Route::~Route() { delete handler; }
bool Route::isMatch(Request::Method method, const std::string& url) {
return (method_ == method) && (url == path_);
}
20 changes: 10 additions & 10 deletions server/route.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@

#include <string>

#include "../utils/include.hpp"
#include "../utils/request.hpp"
#include "../utils/response.hpp"

class Response;
class RequestHandler;

class Route {
private:
Method method;
std::string path;
RequestHandler* handler;

public:
Route(Method _method, std::string _path);
Route(Request::Method method, const std::string& path);
~Route();
bool isMatch(Method, std::string url);

void setHandler(RequestHandler* handler);
Response* handle(Request* req);
void setHandler(RequestHandler* _handler);
bool isMatch(Request::Method, const std::string& url);

private:
Request::Method method_;
std::string path_;
RequestHandler* handler_;
};

#endif // ROUTE_HPP_INCLUDE
Loading