Skip to content

Commit

Permalink
Исправлены ошибки с location, добавлен PUT
Browse files Browse the repository at this point in the history
  • Loading branch information
mtriston committed Jun 12, 2021
1 parent d73e566 commit 66d881a
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Config_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <iostream> //to do delete

//Hardcode defines
#define CFP_MAIN_FOLDER "./www"
#define CFP_MAIN_FOLDER "./www/"
#define CFP_LOCALHOST "127.0.0.1"

//Errors defines
Expand Down
32 changes: 17 additions & 15 deletions Config_unit.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Config_unit.hpp"
#include "utils.hpp"

location_unit &location_unit::operator=(location_unit const &cp)
{
Expand Down Expand Up @@ -220,21 +221,25 @@ void config_unit::resort(void)
*/
std::string config_unit::getServerPath(std::string const &path)
{
int cnt;
size_t cnt;
std::string res;
std::map<std::string, location_unit>::iterator it = _getLocation(path);

if (_location.empty())
return (std::string());
res = it->second._abs_path;
cnt = it->first.size();
if (path[0] == '/' && res[res.size() - 1] == '/')
++cnt;
// if (path[0] == '/' && res[res.size() - 1] == '/')
// ++cnt;
if (res[res.size() - 1] != '/')
res.append("/");
res.append(&path[cnt]);
if (res[res.size() - 1] == '/')
if (cnt < path.size())
res.append(&path[cnt]);
if (res[res.size() - 1] == '/') {
res.append(it->second._def_file);
} else if (isDirectory(res)) {
res.append("/" + it->second._def_file);
}
return res;
}

Expand Down Expand Up @@ -337,41 +342,38 @@ bool config_unit::_pathComp(char const *path, char const *iter)

std::string config_unit::getUploadPath(std::string const &path)
{
int cnt;
size_t cnt;
std::string res;
std::map<std::string, location_unit>::iterator it = _getLocation(path);

if (_location.empty())
return (std::string());
res = it->second._storage;
cnt = it->first.size();
if (path[0] == '/' && res[res.size() - 1] == '/')
++cnt;
if (res[res.size() - 1] != '/')
res.append("/");
res.append(&path[cnt]);
if (res[res.size() - 1] == '/')
res.append(it->second._def_file);
if (cnt < path.size())
res.append(&path[cnt]);
return res;
}

std::string config_unit::getPathFromLocation(const std::string &path)
{
int cnt;
size_t cnt;
std::string res;
std::map<std::string, location_unit>::iterator it = _getLocation(path);

if (_location.empty())
return (std::string());
res = it->second._abs_path;
cnt = it->first.size();
if (path[0] == '/' && res[res.size() - 1] == '/')
++cnt;
if (res[res.size() - 1] != '/')
res.append("/");
res.append(&path[cnt]);
if (cnt < path.size())
res.append(&path[cnt]);
return res;
}

void config_unit::setPythonExec(std::string const&str)
{
_python_path = str;
Expand Down
5 changes: 3 additions & 2 deletions ConnectionSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ ConnectionSocket::ConnectionSocket(const ConnectionSocket &) : _response(this) {

ConnectionSocket &ConnectionSocket::operator=(const ConnectionSocket &) { return *this; }

ConnectionSocket::ConnectionSocket() : _response(this) {}
ConnectionSocket::ConnectionSocket() : _response(this) {
}

ConnectionSocket::~ConnectionSocket() { close(socket_); }

Expand Down Expand Up @@ -55,7 +56,6 @@ bool ConnectionSocket::_isRequestRead()
if (contentLengthPos != std::string::npos && contentLengthPos < headerEndPos) {
char *end_p;
size_t contentLength = std::strtol(_buffer.c_str() + contentLengthPos + 15, &end_p, 10);

if (contentLength > maxClientBody) {
return true;
}
Expand All @@ -66,6 +66,7 @@ bool ConnectionSocket::_isRequestRead()
}
unsigned long transferEncodingPos = _buffer.find("Transfer-Encoding: chunked");
if (transferEncodingPos != std::string::npos && transferEncodingPos < headerEndPos) {

if (_buffer.size() > maxClientBody * 2) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion ConnectionSocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef WEBSERV__CONNECTIONSOCKET_HPP_
#define WEBSERV__CONNECTIONSOCKET_HPP_

#define BUF_SIZE 2048
#define BUF_SIZE 8000

class Config_parser;

Expand Down
21 changes: 15 additions & 6 deletions Response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Response::Response(ConnectionSocket *socket)
: socket(socket), responseData_(), state_(PREPARE_FOR_GENERATE), needHeaders(true)
{
errorMap_[200] = "OK";
errorMap_[201] = "Created";
errorMap_[204] = "No Content";
errorMap_[301] = "Moved Permanently";
errorMap_[307] = "Temporary Redirect";
Expand Down Expand Up @@ -68,6 +69,8 @@ void Response::initGenerateResponse()
_handleMethodHEAD();
} else if (request->getMethod() == "POST") {
_handleMethodPOST();
} else if ( request->getMethod() == "PUT") {
_handleMethodPUT();
} else if (request->getMethod() == "DELETE") {
_handleMethodDELETE();
} else {
Expand Down Expand Up @@ -109,13 +112,10 @@ void Response::_handleMethodGET()
_openContent();
}

void Response::_handleMethodPOST()
void Response::_handleMethodPUT()
{
responseData_.status = OK;
responseData_.status = Created;

if (isCGI()) {
return _handleCGI();
}
responseData_.file = config->getUploadPath(request->getPath());
responseData_.fd = open(responseData_.file.c_str(),
O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU);
Expand All @@ -127,6 +127,16 @@ void Response::_handleMethodPOST()
state_ = WRITE_FILE;
}

void Response::_handleMethodPOST()
{
responseData_.status = OK;

if (isCGI()) {
return _handleCGI();
}
_handleMethodPUT();
}

void Response::_handleMethodDELETE()
{
responseData_.file = config->getServerPath(request->getPath());
Expand Down Expand Up @@ -224,7 +234,6 @@ void Response::_writeContent()
responseData_.content.erase(0, ret);
if (responseData_.content.empty()) {
close(responseData_.fd);
responseData_.status = 204;
state_ = READY_FOR_SEND;
}
}
Expand Down
3 changes: 3 additions & 0 deletions Response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ enum response_states {

enum code {
OK = 200,
Created = 201,
NoContent = 204,
MovedPermanently = 301,
BadRequest = 400,
Expand Down Expand Up @@ -87,6 +88,8 @@ class Response {

void _handleMethodPOST();

void _handleMethodPUT();

void _handleMethodDELETE();

void _handleInvalidRequest(int code);
Expand Down
24 changes: 18 additions & 6 deletions webserv.conf
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
server {
listen 4444;

cgi_location {./cgi-bin}
accepted_methods GET POST HEAD DELETE;
client_max_body_size 20000000b;

default_file youpi.bad_extension;

location {
path ./;
autoindex on;
accepted_methods GET;
path ./YoupiBanane;
}

location /cgi-bin {
accepted_methods GET POST HEAD DELETE;
location /put_test {
accepted_methods PUT;
file_storage ./for_upload;
}

location /for_upload {
Expand All @@ -19,6 +22,15 @@ accepted_methods GET POST HEAD DELETE;

}

location /post_body {
accepted_methods POST;
}

location /directory {
accepted_methods GET;
path ./;
}

location /redirect {
redirect 301 https://google.com;
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 66d881a

Please sign in to comment.