Skip to content

Commit

Permalink
tests fixed. redir removed for multipart post
Browse files Browse the repository at this point in the history
  • Loading branch information
whayter committed Nov 1, 2021
1 parent 37fa56b commit 846bc5b
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 53 deletions.
17 changes: 9 additions & 8 deletions __tests__/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ it ('POST localhost:8080/ with body size bigger than max_body_size', function ()
body: "x".repeat(oneM + 1)
}
})
.post(baseUri + 'test.html')
.post(baseUri + 'uploads/test.html')
.expect('status', 413)
});

Expand All @@ -165,7 +165,7 @@ it ('POST localhost:8080/ with body size way bigger than max_body_size', functio
body: "x".repeat(oneM * 2)
}
})
.post(baseUri + 'test.html')
.post(baseUri + 'uploads/test.html')
.expect('status', 413)
});

Expand All @@ -176,20 +176,21 @@ it ('POST localhost:8080/ with body size = max_body_size', function () {
body: "x".repeat(oneM)
}
})
.post(baseUri + 'testPostMethod.html')
.post(baseUri + 'uploads/testPostMethod.html')
.expect('status', 201)
});

it ('POST localhost:8080/cgiParams.php', function () {
return frisby
.setup({
request: {
body: ""
body: "winston"
}
})
.post(baseUri + 'cgiParams.php')
.expect('status', 200)
.expect('bodyContains', 'POST')
.expect('bodyContains', 'winston')
});

//////////////////////////////////////////////////////////////////////////////
Expand All @@ -201,16 +202,16 @@ it ('DELETE http://localhost:8080/testPostMethod.html', function () {
.expect('status', 405);
});

it ('DELETE http://localhost:8084/testPostMethod.html', function () {
it ('DELETE http://localhost:8080/uploads/testPostMethod.html', function () {
return frisby
.del('http://localhost:8084/testPostMethod.html')
.del('http://localhost:8080/uploads/testPostMethod.html')
.expect('status', 200)
.expect('bodyContains', 'File deleted')
});

it ('DELETE http://localhost:8084/notExist.html', function () {
it ('DELETE http://localhost:8080/uploads/notExist.html', function () {
return frisby
.del('http://localhost:8084/notExist.html')
.del('http://localhost:8080/uploads/notExist.html')
.expect('status', 404)
});

Expand Down
Binary file removed cgi_tester
Binary file not shown.
19 changes: 4 additions & 15 deletions config/macos_webserv.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,23 @@ server {
upload_store ./www/uploads;

location / {
limit_except GET POST;
limit_except GET;
autoindex on;
}
location ext .php {
limit_except GET POST;
cgi_exec /usr/local/bin/php-cgi;
return 307 php-cgi;
}
location ext .cgi {
limit_except GET POST;
cgi_exec ./cgi_tester;
return 307 cgi_tester;
}
location /bad_cgi {
cgi_exec ./README.md;
}
location /uri {
return 301 http://youtube.com;
}
location /uploads {
limit_except GET POST DELETE;
}
}
server {
listen 8081;
Expand Down Expand Up @@ -57,13 +55,4 @@ server {
location / {
autoindex on;
}
}
server {
listen 8084;
server_name server_five;
root ./www;
location / {
limit_except GET POST DELETE;
autoindex on;
}
}
17 changes: 3 additions & 14 deletions config/ubuntu_webserv.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ server {
cgi_exec /usr/bin/php-cgi;
return 307 php-cgi;
}
location ext .cgi {
limit_except GET POST;
cgi_exec ./cgi_tester;
return 307 cgi_tester;
}
location /bad_cgi {
cgi_exec ./README.md;
}
location /uri {
return 301 http://youtube.com;
}
location /uploads {
limit_except GET POST DELETE;
}
}
server {
listen 8081;
Expand Down Expand Up @@ -57,13 +55,4 @@ server {
location / {
autoindex on;
}
}
server {
listen 8084;
server_name server_five;
root ./www;
location / {
limit_except GET POST DELETE;
autoindex on;
}
}
8 changes: 0 additions & 8 deletions includes/cgi/cgi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,18 @@ std::pair<std::vector<unsigned char>, ft::error_code> getCgiResponse(std::string
http::content_type cgiResponse;
int status = 0;
int childToParent[2];
int parentToChild[2]; // useless ?
pipe(childToParent);
pipe(parentToChild); // useless ?
pid_t pid = fork();
if (pid == 0) // child process
{
close(childToParent[0]); // close reading end of childToParent
close(parentToChild[1]); // close writing end of parentToChild // useless ?
replacePipeEnd(parentToChild[0], 0); // replace stdin with incoming pipe // useless ?


replacePipeEnd(childToParent[1], 1); // replace stdout with outgoing pipe
char* arg = 0;
if (execve(cgiExecPath.c_str(), &arg, environ) == -1) // exec cgi binary
exit(errno);
}
else if (pid > 0) // main process
{
close(parentToChild[0]); // close reading end of parentToChild // useless ?
close(parentToChild[1]); // close writing end of parentToChild // useless ?
close(childToParent[1]); // close writing end of childToParent
waitpid(pid, &status, 0); // wait for child process to end.
replacePipeEnd(childToParent[0], 0); // replace stdin with incoming pipe
Expand Down
12 changes: 5 additions & 7 deletions src/http/messageBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ Response postMethodResponse(const Context& ctxt, Request& request, Response& res
if (ft::trim(vec[0]) == "multipart/form-data")
return postMultipart(ctxt, request, response);

if (postContent(ctxt.path, request.getContent()) < 0)

fs::path p = ctxt.server.getUploadStore() / ctxt.path.filename();

if (postContent(p, request.getContent()) < 0)
return (errorResponse(ctxt, response, Status::InternalServerError));
response.setStatus(Status::Created);
response.setHeader("Location", request.getUri().toString());
Expand Down Expand Up @@ -199,12 +202,7 @@ Response postMultipart(const Context& ctxt, Request& request, Response& response
++it;
}
response.setStatus(http::Status::Created);
response.setHeader("Location", request.getUri().toString());
ReturnDirective rdir;
rdir.setUri(request.getUri().toString());
rdir.setCode(Status::Created);
response.setContent(html::buildRedirectionPage(rdir), "text/html");

response.setContent(html::buildErrorPage(http::Status::Created));
return response;
}

Expand Down
2 changes: 1 addition & 1 deletion www/upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ <h1 class="text-center">Upload something yo</h1>
<hr class="hr-invisible">
<input type="submit" value="Upload" name="submit">
</form> -->
<form method="post" enctype="multipart/form-data">
<form method="post" enctype="multipart/form-data" action="/uploads">
<div>
<label for="file">Sélectionner le fichier à envoyer</label>
<input type="file" id="file" name="file" multiple="multiple">
Expand Down

0 comments on commit 846bc5b

Please sign in to comment.