Skip to content

Commit

Permalink
Merge branch 'review-alex-fix' into review-alex-fix-queue
Browse files Browse the repository at this point in the history
  • Loading branch information
unkn0wn107 committed Jul 18, 2024
2 parents 74f6371 + 7a5456f commit 79c171c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 26 deletions.
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ valgrind:
docker compose exec -it webserv-dev make debug
docker compose exec -it webserv-dev bash -c "ulimit -n 1024 && valgrind --track-origins=yes ./webserv"

valgrind-full:
export BUILD_TYPE=debug
docker compose up --build -d webserv-dev
docker compose exec -it webserv-dev make debug
docker compose exec -it webserv-dev bash -c "ulimit -n 1024 && valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./webserv"

helgrind:
export BUILD_TYPE=debug
docker compose up --build -d webserv-dev
docker compose exec -it webserv-dev make debug
docker compose exec -it webserv-dev bash -c "ulimit -n 1024 && valgrind --tool=helgrind ./webserv"

logs:
docker compose logs -f

Expand Down
28 changes: 21 additions & 7 deletions siege_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
SIEGE_CGI_URLS_FILE="urls_cgi.txt"
SIEGE_GET_URLS_FILE="urls_get.txt"
SIEGE_LOG_FILE="siege.log"
TEST_DURATION=30s
TEST_DURATION=20s
TEST_FAILED=0

cat <<EOL > $SIEGE_CGI_URLS_FILE
Expand Down Expand Up @@ -38,7 +38,7 @@ echo "Running siege with no-cache for CGIs" >> $SIEGE_LOG_FILE
siege -t ${TEST_DURATION} -c 250 -b -f $SIEGE_CGI_URLS_FILE -H "Cache-Control: no-cache" >> $SIEGE_LOG_FILE

if grep -q '"failed_transactions": *[1-9]' $SIEGE_LOG_FILE; then
echo "!!!KO!!!: Siege test failed with no-cache for CGIs. Check $SIEGE_LOG_FILE for details."
echo "!!!KO!!!: Siege test failed with no-cache for CGIs."
TEST_FAILED=1
else
echo "OK: Siege test passed with no-cache for CGIs."
Expand All @@ -52,7 +52,7 @@ echo "Running siege with caching enabled for CGIs" >> $SIEGE_LOG_FILE
siege -t ${TEST_DURATION} -c 250 -b -f $SIEGE_CGI_URLS_FILE -H "Cookie: sessionid=marvin;" >> $SIEGE_LOG_FILE

if grep -q '"failed_transactions": *[1-9]' $SIEGE_LOG_FILE; then
echo "!!!KO!!!: Siege test failed with caching enabled for CGIs. Check $SIEGE_LOG_FILE for details."
echo "!!!KO!!!: Siege test failed with caching enabled for CGIs."
TEST_FAILED=1
else
echo "OK: Siege test passed with caching enabled for CGIs."
Expand All @@ -61,15 +61,29 @@ printf "\n"

sleep 2

echo "Running siege with caching enabled for GET"
echo "Running siege with caching enabled for GET" >> $SIEGE_LOG_FILE
echo "Running siege for GET cross-servers"
echo "Running siege for GET cross-servers" >> $SIEGE_LOG_FILE
siege -t ${TEST_DURATION} -c 250 -b -f $SIEGE_GET_URLS_FILE -H "Cookie: sessionid=marvin;" >> $SIEGE_LOG_FILE

if grep -q '"failed_transactions": *[1-9]' $SIEGE_LOG_FILE; then
echo "!!!KO!!!: Siege test failed with caching enabled for GET. Check $SIEGE_LOG_FILE for details."
echo "!!!KO!!!: Siege test failed for GET cross-servers."
TEST_FAILED=1
else
echo "OK: Siege test passed with caching enabled for GET."
echo "OK: Siege test passed for GET cross-servers."
fi
printf "\n"

sleep 2

echo "Running siege for GET single"
echo "Running siege for GET single" >> $SIEGE_LOG_FILE
siege -t ${TEST_DURATION} -c 250 -b -H "Cookie: sessionid=marvin;" http://${CONTAINER}:${PORT} >> $SIEGE_LOG_FILE

if grep -q '"failed_transactions": *[1-9]' $SIEGE_LOG_FILE; then
echo "!!!KO!!!: Siege test failed for GET."
TEST_FAILED=1
else
echo "OK: Siege test passed for GET."
fi
printf "\n"

Expand Down
14 changes: 3 additions & 11 deletions src/CGIHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ void CGIHandler::_runScript() {
close(_outpipefd[0]);
close(_outpipefd[1]);
std::string postData = _request.getBody();
if (!postData.empty())
{
if (!postData.empty()) {
if (pipe(_inpipefd) == -1)
{
std::cerr << "CHILD: Failed to create pipe"<< std::endl;
Expand Down Expand Up @@ -149,9 +148,6 @@ void CGIHandler::_runScript() {
totalWritten += written;
}
close(_inpipefd[1]);
} else {
close(_inpipefd[0]);
close(_inpipefd[1]);
}

char** argv_ptrs = new char*[_argv.size() + 1];
Expand Down Expand Up @@ -193,8 +189,6 @@ ConnectionStatus CGIHandler::handleCGIRequest() {
if (_pid == -1)
throw ForkFailure("CGI: Failed to fork process");
if (_pid > 0) {
close(_inpipefd[0]);
_inpipefd[0] = -1;
close(_outpipefd[1]);
_outpipefd[1] = -1;
_state = SCRIPT_RUNNING;
Expand Down Expand Up @@ -253,10 +247,8 @@ ConnectionStatus CGIHandler::handleCGIRequest() {
_processOutputSize += count;
if (count < (ssize_t)sizeof(buffer) || count == 0)
{
close(_outpipefd[0]);
_outpipefd[0] = -1;
close(_inpipefd[1]);
_inpipefd[1] = -1;
close(_outpipefd[0]);
_outpipefd[0] = -1;
_state = PROCESS_OUTPUT;
}
else
Expand Down
16 changes: 10 additions & 6 deletions src/HTTPResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#include <sys/sendfile.h>
#include <sys/socket.h>
#include "Utils.hpp"
#include "VirtualServer.hpp"
#include "Config.hpp"
#include "FileManager.hpp"

const std::pair<int, std::string> HTTPResponse::STATUS_CODE_MESSAGES[] = {
std::make_pair(HTTPResponse::CONTINUE, "Continue"),
Expand Down Expand Up @@ -330,13 +330,14 @@ ssize_t HTTPResponse::_send(int socket, size_t sndbuf) {
return _responseBufferPos;
}

void HTTPResponse::_sendfile(int clientSocket, FILE* file, ssize_t sndbuf) {
ssize_t HTTPResponse::_sendfile(int clientSocket, FILE* file, ssize_t sndbuf) {
ssize_t bytesSent;
bytesSent = sendfile(clientSocket, fileno(file), &_responseFilePos, sndbuf);
if (bytesSent == -1){
throw Exception("SENDFILE: failed sendfile: "+ std::string(strerror(errno)));
usleep(1000);
if (bytesSent == -1) {
_log.error("SENDFILE: failed sendfile, client probably closed the connection");
return -1;
}
return bytesSent;
}

int HTTPResponse::sendResponse(int clientSocket, ssize_t sndbuf) {
Expand All @@ -353,7 +354,10 @@ int HTTPResponse::sendResponse(int clientSocket, ssize_t sndbuf) {
throw Exception("(fopen) Error opening file : " + _file + " : " +
std::string(strerror(errno)));
}
_sendfile(clientSocket, _toSend, sndbuf);
if (_sendfile(clientSocket, _toSend, sndbuf) == -1) {
fclose(_toSend);
return -1;
}
fclose(_toSend);
if (static_cast<size_t>(_responseFilePos) == _fileSize)
return 1;
Expand Down
4 changes: 2 additions & 2 deletions src/HTTPResponse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: agaley <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/30 16:12:10 by agaley #+# #+# */
/* Updated: 2024/07/04 00:37:56 by agaley ### ########lyon.fr */
/* Updated: 2024/07/18 03:36:44 by agaley ### ########lyon.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -134,7 +134,7 @@ class HTTPResponse {

void _errorResponse();
ssize_t _send(int socket, size_t sndbuf);
void _sendfile(int socket, FILE* file, ssize_t sndbuf);
ssize_t _sendfile(int socket, FILE* file, ssize_t sndbuf);

public:
HTTPResponse(int statusCode, const LocationConfig& config);
Expand Down

0 comments on commit 79c171c

Please sign in to comment.