Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CRLF issue when response binary file #51

Merged
merged 2 commits into from
Oct 20, 2022
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
6 changes: 1 addition & 5 deletions srcs/Cgi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,7 @@ std::string Cgi::execute() {
/* if POST method, write request body to child process */
if (request_->getMethod() == "POST") {
std::vector<char> body = request_->getBody();
std::vector<char>::iterator it = body.begin();
while (it != body.end()) {
write(writePipe[FT_PIPEIN], &(*it), 1);
it++;
}
write(writePipe[FT_PIPEIN], reinterpret_cast<char*> (&body[0]), body.size());
}
close(writePipe[FT_PIPEIN]);

Expand Down
33 changes: 17 additions & 16 deletions srcs/Response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Response::Response(std::string status) {
makeDefaultHeaders();
}

Response::Response(std::string status, const std::vector<char> &result) {
Response::Response(std::string status, const std::vector<char> &result, ft_bool isCgi) {
statusCode_ = std::atoi(status.substr(0, 3).c_str());
statusLine_ = "HTTP/1.1 " + status;

Expand All @@ -18,20 +18,21 @@ Response::Response(std::string status, const std::vector<char> &result) {
std::vector<std::string> splitedHeaderLine;
std::vector<std::string>::iterator it_s;

it = std::search(result.begin(), result.end(), crlf, crlf + strlen(crlf));
if (it != result.end()) {
body_ = std::vector<char>(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<char>(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();
}
Expand Down Expand Up @@ -86,13 +87,13 @@ std::vector<char> 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);
Expand Down
2 changes: 1 addition & 1 deletion srcs/Response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Response {

public:
Response(std::string status);
Response(std::string status, const std::vector<char> &result);
Response(std::string status, const std::vector<char> &result, ft_bool isCgi = false);
~Response();

std::vector<char> createMessage();
Expand Down
4 changes: 2 additions & 2 deletions srcs/Worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down