From 269da9e59dda7f4c2e01762f21a20accd7379038 Mon Sep 17 00:00:00 2001 From: srngch Date: Wed, 19 Oct 2022 20:22:30 +0900 Subject: [PATCH 1/2] fix: Fix CRLF issue when response binary file Add isCgi flag to Response constructor Co-authored-by: heehkim --- srcs/Response.cpp | 33 +++++++++++++++++---------------- srcs/Response.hpp | 2 +- srcs/Worker.cpp | 4 ++-- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/srcs/Response.cpp b/srcs/Response.cpp index a1ca14b..0c32588 100644 --- a/srcs/Response.cpp +++ b/srcs/Response.cpp @@ -6,7 +6,7 @@ Response::Response(std::string status) { makeDefaultHeaders(); } -Response::Response(std::string status, const std::vector &result) { +Response::Response(std::string status, const std::vector &result, ft_bool isCgi) { statusCode_ = std::atoi(status.substr(0, 3).c_str()); statusLine_ = "HTTP/1.1 " + status; @@ -18,20 +18,21 @@ Response::Response(std::string status, const std::vector &result) { std::vector splitedHeaderLine; std::vector::iterator it_s; - it = std::search(result.begin(), result.end(), crlf, crlf + strlen(crlf)); - if (it != result.end()) { - body_ = std::vector(it + strlen(crlf), result.end()); - cgiHeaders = std::string(result.begin(), it); - std::cout << "cgiHeaders: " << cgiHeaders << std::endl; - splitedHeaders = split(std::string(cgiHeaders.begin(), cgiHeaders.end()), "\n"); - - for (it_s = splitedHeaders.begin(); it_s != splitedHeaders.end(); it_s++) { - splitedHeaderLine = split(*it_s, ": "); - std::transform(splitedHeaderLine[0].begin(), splitedHeaderLine[0].end(), splitedHeaderLine[0].begin(), ::tolower); - appendHeader(splitedHeaderLine[0], splitedHeaderLine[1]); + body_ = result; + if (isCgi) { + it = std::search(result.begin(), result.end(), crlf, crlf + strlen(crlf)); + if (it != result.end()) { + body_ = std::vector(it + strlen(crlf), result.end()); + cgiHeaders = std::string(result.begin(), it); + std::cout << "cgiHeaders: " << cgiHeaders << std::endl; + splitedHeaders = split(std::string(cgiHeaders.begin(), cgiHeaders.end()), "\n"); + + for (it_s = splitedHeaders.begin(); it_s != splitedHeaders.end(); it_s++) { + splitedHeaderLine = split(*it_s, ": "); + std::transform(splitedHeaderLine[0].begin(), splitedHeaderLine[0].end(), splitedHeaderLine[0].begin(), ::tolower); + appendHeader(splitedHeaderLine[0], splitedHeaderLine[1]); + } } - } else { - body_ = result; } makeDefaultHeaders(); } @@ -86,13 +87,13 @@ std::vector Response::createMessage() { void Response::setContentType(std::string fileExtension) { headers_.erase("content-type"); - if (fileExtension == "html") + if (fileExtension == "html") appendHeader("content-type", MIME_HTML); else if (fileExtension == "css") appendHeader("content-type", MIME_CSS); else if (fileExtension == "gif") appendHeader("content-type", MIME_IMAGE_GIF); - else if (fileExtension == "jpeg" || fileExtension == "jpg") + else if (fileExtension == "jpeg" || fileExtension == "jpg") appendHeader("content-type", MIME_IMAGE_JPG); else if (fileExtension == "js") appendHeader("content-type", MIME_APP_JS); diff --git a/srcs/Response.hpp b/srcs/Response.hpp index 66c072d..bff541e 100644 --- a/srcs/Response.hpp +++ b/srcs/Response.hpp @@ -28,7 +28,7 @@ class Response { public: Response(std::string status); - Response(std::string status, const std::vector &result); + Response(std::string status, const std::vector &result, ft_bool isCgi = false); ~Response(); std::vector createMessage(); diff --git a/srcs/Worker.cpp b/srcs/Worker.cpp index 7ffb8c8..fcb0e54 100644 --- a/srcs/Worker.cpp +++ b/srcs/Worker.cpp @@ -245,7 +245,7 @@ ft_bool Worker::executeGet() { Cgi cgi(request_); std::string result = cgi.execute(); - Response response(HTTP_OK, stringToCharV(result)); + Response response(HTTP_OK, stringToCharV(result), FT_TRUE); send(response.createMessage()); return FT_TRUE; } @@ -265,7 +265,7 @@ ft_bool Worker::executePost() { if (isCgi(request_->getFilePath())) { Cgi cgi(request_); std::string result = cgi.execute(); - Response response(HTTP_CREATED, stringToCharV(result)); + Response response(HTTP_CREATED, stringToCharV(result), FT_TRUE); send(response.createMessage()); } return FT_TRUE; From 3ccadfb48e28648971dbddb84524e7c2759c1089 Mon Sep 17 00:00:00 2001 From: srngch Date: Wed, 19 Oct 2022 20:41:02 +0900 Subject: [PATCH 2/2] refactor: Use reinterpret cast when converting char vector to char array Co-authored-by: heehkim --- srcs/Cgi.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/srcs/Cgi.cpp b/srcs/Cgi.cpp index 728d356..8aafc5b 100644 --- a/srcs/Cgi.cpp +++ b/srcs/Cgi.cpp @@ -118,11 +118,7 @@ std::string Cgi::execute() { /* if POST method, write request body to child process */ if (request_->getMethod() == "POST") { std::vector body = request_->getBody(); - std::vector::iterator it = body.begin(); - while (it != body.end()) { - write(writePipe[FT_PIPEIN], &(*it), 1); - it++; - } + write(writePipe[FT_PIPEIN], reinterpret_cast (&body[0]), body.size()); } close(writePipe[FT_PIPEIN]);