Skip to content

Commit 9cf99ea

Browse files
let's go to the bus
1 parent af3dfc1 commit 9cf99ea

File tree

7 files changed

+30
-18
lines changed

7 files changed

+30
-18
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ srcs = \
4545
src/handler/error_handler.hpp \
4646
src/handler/handler.cpp \
4747
src/handler/handler.hpp \
48+
src/handler/redirect_handler.cpp \
49+
src/handler/redirect_handler.hpp \
4850
src/handler/static_file_handler.cpp \
4951
src/handler/static_file_handler.hpp \
5052
src/handler/upload_handler.cpp \
@@ -90,6 +92,7 @@ test_srcs = \
9092
tests/router_unittest.cpp \
9193
tests/server_config_unittest.cpp \
9294
tests/static_file_handler_unittest.cpp \
95+
tests/redirect_handler_unittest.cpp \
9396
tests/str_split_unittest.cpp \
9497
tests/main.cpp \
9598

src/handler/upload_handler.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ UploadHandler::UploadHandler(const std::string& path, const RouteConfig& rc, con
2121
fd_(-1)
2222

2323
{
24+
if (req.content_length == 0) {
25+
//set_error(HttpResponse::kStatusBadRequest);
26+
//set_error(HttpResponse::kStatusNoContent);
27+
set_error(HttpResponse::kStatusOk);
28+
return;
29+
}
2430
struct stat file_stat;
2531
if (stat(path.c_str(), &file_stat) == 0 && S_ISREG(file_stat.st_mode)) {
2632
set_error(HttpResponse::kStatusConflict); // file already exists

src/http/http_response.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ HttpResponse::Status HttpResponse::status_from_int(int code)
7070
case 200: return HttpResponse::kStatusOk;
7171
case 201: return HttpResponse::kStatusCreated;
7272
case 204: return HttpResponse::kStatusNoContent;
73+
case 301: return HttpResponse::kStatusMovedPermanently;
74+
case 302: return HttpResponse::kStatusFound;
7375
case 400: return HttpResponse::kStatusBadRequest;
7476
case 403: return HttpResponse::kStatusForbidden;
7577
case 404: return HttpResponse::kStatusNotFound;
@@ -93,6 +95,9 @@ std::string HttpResponse::to_string() const
9395
if (!content_type.empty()) {
9496
out << "Content-Type: " << content_type << "\r\n";
9597
}
98+
if (!location.empty()) {
99+
out << "Location: " << location << "\r\n";
100+
}
96101
if (!inline_body.empty()) {
97102
out << "Content-Length: " << inline_body.size() << "\r\n";
98103
}
@@ -145,8 +150,6 @@ HttpResponse HttpResponse::make_response_headers_only(HttpResponse::Status statu
145150
res.code = status;
146151
if (!content_type.empty())
147152
res.content_type = content_type;
148-
else
149-
res.content_type = "text/html; charset=UTF-8";
150153
res.keep_alive = req.keep_alive;
151154
res.content_length = content_length; // should be empty of content-length = 0
152155
return res;

src/http/http_response.hpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,6 @@ struct HttpResponse {
2929
};
3030

3131
explicit HttpResponse(HttpVersion protocol = WEBSERV_DEFAULT_HTTP_VERSION);
32-
static Status status_from_int(int code);
33-
static HttpResponse make_error(HttpResponse::Status status,
34-
const std::map<HttpResponse::Status, std::string>& error_pages,
35-
const HttpRequest& req);
36-
static HttpResponse make_response_headers_only(HttpResponse::Status status,
37-
const std::string& content_type,
38-
size_t content_length, const HttpRequest& req);
39-
static HttpResponse make_response_with_body(HttpResponse::Status status,
40-
const std::string& content_type,
41-
const std::string& body, const HttpRequest& req);
4232

4333
HttpVersion http_version;
4434
HttpResponse::Status code;
@@ -50,6 +40,7 @@ struct HttpResponse {
5040

5141
std::string content_type;
5242
size_t content_length;
43+
std::string location;
5344
bool is_chunked;
5445
bool keep_alive;
5546

@@ -60,7 +51,16 @@ struct HttpResponse {
6051

6152
static const char* reason_phrase(HttpResponse::Status status);
6253
static HttpResponse::Status parse_status(const std::string& s);
63-
54+
static Status status_from_int(int code);
55+
static HttpResponse make_error(HttpResponse::Status status,
56+
const std::map<HttpResponse::Status, std::string>& error_pages,
57+
const HttpRequest& req);
58+
static HttpResponse make_response_headers_only(HttpResponse::Status status,
59+
const std::string& content_type,
60+
size_t content_length, const HttpRequest& req);
61+
static HttpResponse make_response_with_body(HttpResponse::Status status,
62+
const std::string& content_type,
63+
const std::string& body, const HttpRequest& req);
6464
std::string to_string() const;
6565
};
6666

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ int main(void)
1010
setup_signal_handlers();
1111

1212
try {
13-
HttpConfig cfg = load_http_config("config/youpi_banane.conf");
13+
HttpConfig cfg = load_http_config("config/vitepress.conf");
1414

1515
Server server(cfg.servers[0]);
1616
server.init();

src/router/router.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "handler/error_handler.hpp"
77
#include "handler/static_file_handler.hpp"
88
#include "handler/upload_handler.hpp"
9+
#include "handler/redirect_handler.hpp"
910
#include "http/http_request.hpp"
1011
#include "http/http_response.hpp"
1112
#include "util/log_message.hpp"
@@ -124,8 +125,7 @@ Handler* Router::handle_request(const HttpRequest& request)
124125
const RouteConfig& best_route = find_best_route(request.path);
125126

126127
if (!best_route.shared.redirect.url.empty()) {
127-
LOG(WARN) << "Redirections not implemented yet";
128-
return new ErrorHandler(HttpResponse::kStatusNotImplemented, best_route, request);
128+
return new RedirectHandler(best_route.shared.redirect.code, best_route.shared.redirect.url, best_route, request);
129129
}
130130
if (!is_allowed_method(request, best_route)) {
131131
return new ErrorHandler(HttpResponse::kStatusMethodNotAllowed, best_route, request);

tests/cgi_handler_unittest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ UTEST(CgiHandler, NotExecutable)
202202

203203
UTEST(CgiHandler, EmptyOutput)
204204
{
205-
TempCgiScript script("echo \"Content-Type: text/html; charset=UTF-8\"\n"
206-
"echo\n");
205+
TempCgiScript script(
206+
"echo\n""echo\n");
207207
RouteConfig rc;
208208
HttpRequest req;
209209
req.method = "GET";

0 commit comments

Comments
 (0)