File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #include " handlers.hpp"
2+
3+ using namespace std ;
4+
5+ Response* RandomNumberHandler::callback (Request *req) {
6+ Response *res = new Response;
7+ res->setHeader (" Content-Type" , " text/html" );
8+ string body;
9+ body += " <!DOCTYPE html>" ;
10+ body += " <html>" ;
11+ body += " <body style=\" text-align: center;\" >" ;
12+ body += " <h1>AP HTTP</h1>" ;
13+ body += " <p>" ;
14+ body += " a random number in [1, 10] is: " ;
15+ body += to_string (rand () % 10 + 1 );
16+ body += " </p>" ;
17+ body += " <p>" ;
18+ body += " SeddionId: " ;
19+ body += req->getSessionId ();
20+ body += " </p>" ;
21+ body += " </body>" ;
22+ body += " </html>" ;
23+ res->setBody (body);
24+ return res;
25+ }
26+
27+ Response* LoginHandler::callback (Request *req) {
28+ string username = req->getBodyParam (" username" );
29+ string password = req->getBodyParam (" password" );
30+ cout << " username: " << username << " ,\t password: " << password << endl;
31+ Response *res = Response::redirect (" /rand" );
32+ res->setSessionId (" SID" );
33+ return res;
34+ }
35+
36+ Response* UploadHandler::callback (Request *req) {
37+ string name = req->getBodyParam (" file_name" );
38+ string file = req->getBodyParam (" file" );
39+ cout << name << " :\n " << file << endl;
40+ Response *res = Response::redirect (" /" );
41+ return res;
42+ }
Original file line number Diff line number Diff line change 1+ #include " ../server/server.hpp"
2+ #include < cstdlib> // for rand and srand
3+ #include < ctime> // for time
4+ #include < iostream>
5+
6+
7+ class RandomNumberHandler : public RequestHandler {
8+ public:
9+ Response* callback (Request*);
10+ };
11+
12+ class LoginHandler : public RequestHandler {
13+ public:
14+ Response* callback (Request*);
15+ };
16+
17+ class UploadHandler : public RequestHandler {
18+ public:
19+ Response* callback (Request*);
20+ };
Original file line number Diff line number Diff line change 1+ #include " my_server.hpp"
2+ #include " handlers.hpp"
3+ #include < cstdlib> // for rand and srand
4+ #include < ctime> // for time
5+ #include < iostream>
6+
7+ using namespace std ;
8+
9+ int main (int argc, char **argv) {
10+ srand (time (NULL )); // for rand
11+ try {
12+ MyServer server;
13+ server.get (" /login" , new ShowPage (" static/logincss.html" ));
14+ server.post (" /login" , new LoginHandler ());
15+ server.get (" /up" , new ShowPage (" static/upload_form.html" ));
16+ server.post (" /up" , new UploadHandler ());
17+ server.get (" /rand" , new RandomNumberHandler ());
18+ server.get (" /home.png" , new ShowImage (" static/home.png" ));
19+ server.get (" /aa" , new ShowImage (" static/img.jpeg" ));
20+ server.get (" /" , new ShowPage (" static/home.html" ));
21+ server.run ();
22+ } catch (Server::Exception e) {
23+ cerr << e.getMessage () << endl;
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ #include " my_server.hpp"
2+
3+ MyServer::MyServer (): Server(){}
Original file line number Diff line number Diff line change 1+ #include " ../server/server.hpp"
2+
3+ class MyServer : public Server {
4+ public:
5+ MyServer ();
6+ };
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ STD=-std=c++11 -Wall -pedantic
33CF =$(STD )
44BUILD_DIR =build
55
6- all : $(BUILD_DIR ) server .out
6+ all : $(BUILD_DIR ) myserver .out
77
88$(BUILD_DIR ) :
99 mkdir -p $(BUILD_DIR )
@@ -23,11 +23,17 @@ $(BUILD_DIR)/server.o: server/server.cpp server/server.hpp server/route.hpp util
2323$(BUILD_DIR ) /route.o : server/route.cpp server/route.hpp utils/utilities.hpp utils/response.hpp utils/request.hpp utils/include.hpp
2424 $(CC ) $(CF ) -c server/route.cpp -o $(BUILD_DIR ) /route.o
2525
26- $(BUILD_DIR ) /server_main .o : server/main .cpp server/server.hpp utils/utilities.hpp utils/response.hpp utils/request.hpp utils/include.hpp
27- $(CC ) $(CF ) -c server/main .cpp -o $(BUILD_DIR ) /server_main .o
26+ $(BUILD_DIR ) /handlers .o : examples/handlers .cpp server/server.hpp utils/utilities.hpp utils/response.hpp utils/request.hpp utils/include.hpp
27+ $(CC ) $(CF ) -c examples/handlers .cpp -o $(BUILD_DIR ) /handlers .o
2828
29- server.out : $(BUILD_DIR ) /response.o $(BUILD_DIR ) /request.o $(BUILD_DIR ) /utilities.o $(BUILD_DIR ) /server.o $(BUILD_DIR ) /route.o $(BUILD_DIR ) /server_main.o
30- $(CC ) $(CF ) $(BUILD_DIR ) /response.o $(BUILD_DIR ) /request.o $(BUILD_DIR ) /utilities.o $(BUILD_DIR ) /server.o $(BUILD_DIR ) /route.o $(BUILD_DIR ) /server_main.o -o server.out
29+ $(BUILD_DIR ) /my_server.o : examples/my_server.cpp server/server.hpp utils/utilities.hpp utils/response.hpp utils/request.hpp utils/include.hpp
30+ $(CC ) $(CF ) -c examples/my_server.cpp -o $(BUILD_DIR ) /my_server.o
31+
32+ $(BUILD_DIR ) /main.o : examples/main.cpp server/server.hpp utils/utilities.hpp utils/response.hpp utils/request.hpp utils/include.hpp
33+ $(CC ) $(CF ) -c examples/main.cpp -o $(BUILD_DIR ) /main.o
34+
35+ myserver.out : $(BUILD_DIR ) /my_server.o $(BUILD_DIR ) /main.o $(BUILD_DIR ) /handlers.o $(BUILD_DIR ) /response.o $(BUILD_DIR ) /request.o $(BUILD_DIR ) /utilities.o $(BUILD_DIR ) /server.o $(BUILD_DIR ) /route.o
36+ $(CC ) $(CF ) $(BUILD_DIR ) /my_server.o $(BUILD_DIR ) /main.o $(BUILD_DIR ) /handlers.o $(BUILD_DIR ) /response.o $(BUILD_DIR ) /request.o $(BUILD_DIR ) /utilities.o $(BUILD_DIR ) /server.o $(BUILD_DIR ) /route.o -o myserver.out
3137
3238.PHONY : clean
3339clean :
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -154,8 +154,7 @@ Request *parseRawReq(char *headersRaw) {
154154 return req;
155155}
156156
157- Server::Server (int _port, string _notFoundErrPage)
158- : port(_port), notFoundErrPage(_notFoundErrPage) {}
157+ Server::Server (int _port) : port(_port) {}
159158
160159void Server::get (string path, RequestHandler *handler) {
161160 Route *route = new Route (GET , path);
@@ -238,20 +237,23 @@ void Server::run() {
238237
239238const char *Server::Exception::getMessage () { return pMessage; }
240239
240+
241241ShowFile::ShowFile (string _filePath, string _fileType) {
242242 filePath = _filePath;
243243 fileType = _fileType;
244244}
245245
246246Response *ShowFile::callback (Request *req) {
247+ cout<<" ---------------------------------------" <<endl;
247248 Response *res = new Response;
248249 res->setHeader (" Content-Type" , fileType);
249250 res->setBody (readFile (filePath.c_str ()));
250251 return res;
251252}
252253
253254ShowPage::ShowPage (string filePath)
254- : ShowFile(filePath, " text/" + getExtension(filePath)) {}
255+ : ShowFile(filePath, " text/" + getExtension(filePath)) {
256+ }
255257
256258ShowImage::ShowImage (string filePath)
257259 : ShowFile(filePath, " image/" + getExtension(filePath)) {}
Original file line number Diff line number Diff line change @@ -16,7 +16,6 @@ class RequestHandler {
1616class ShowFile : public RequestHandler {
1717 std::string filePath;
1818 std::string fileType;
19-
2019public:
2120 ShowFile (std::string filePath, std::string fileType);
2221 Response *callback (Request *req);
@@ -36,7 +35,7 @@ class ShowImage : public ShowFile {
3635
3736class Server {
3837public:
39- Server (int port = 5000 , std::string notFoundErrPage = " static/404.html " );
38+ Server (int port = 5000 );
4039 void run ();
4140 void get (std::string path, RequestHandler *handler);
4241 void post (std::string path, RequestHandler *handler);
You can’t perform that action at this time.
0 commit comments