Skip to content

Commit

Permalink
feat: Parse host and port (#18)
Browse files Browse the repository at this point in the history
- ex) listen 0.0.0.0:8000
- host: 0.0.0.0
- port: 8000
S0YKIM committed Oct 14, 2022
1 parent d64153f commit bbafd3e
Showing 7 changed files with 78 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CC = c++
CXXFLAGS = -Wall -Wextra -Werror -std=c++98 -pedantic
CXXFLAGS = -Wall -Wextra -Werror -std=c++98 -pedantic -g
INCLUDE_FLAGS = -I srcs
TARGET = webserv

35 changes: 24 additions & 11 deletions srcs/conf/Block.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include "Block.hpp"

Block::Block() : parent_(nullptr), listenPort_(""), webRoot_(""), clientMaxBodySize_(0), uri_(""), index_(""), autoIndex_("") {
Block::Block() : parent_(nullptr), host_(""), port_(0), webRoot_(""), clientMaxBodySize_(0), uri_(""), index_(""), autoIndex_("") {
setServerDirectivesMap();
}

Block::Block(Block *parent) : listenPort_(""), webRoot_(""), clientMaxBodySize_(0), uri_(""), index_(""), autoIndex_("") {
Block::Block(Block *parent) : host_(""), port_(0), webRoot_(""), clientMaxBodySize_(0), uri_(""), index_(""), autoIndex_("") {
parent_ = parent;
setLocationDirectivesMap();
}

void Block::setServerDirectivesMap() {
directivesMap_["listen"] = &Block::setListenPort;
directivesMap_["listen"] = &Block::setHostPort;
directivesMap_["server_name"] = &Block::setServerNames;
directivesMap_["root"] = &Block::setWebRoot;
directivesMap_["client_max_body_size"] = &Block::setClientMaxBodySize;
@@ -165,10 +165,14 @@ void Block::setIndex(std::vector<std::string> args) {
index_ = args[0];
}

void Block::setListenPort(std::vector<std::string> args) {
void Block::setHostPort(std::vector<std::string> args) {
std::vector<std::string> hostport;

if (args.size() != 1)
throw std::runtime_error("Invalid configuration file: port\n");
listenPort_ = args[0];
hostport = parseHostPort(args[0]);
host_ = hostport[0];
port_ = atoi(hostport[1].c_str());
}

void Block::setServerNames(std::vector<std::string> args) {
@@ -222,11 +226,19 @@ void Block::setAutoIndex(std::vector<std::string> args) {
autoIndex_ = args[0];
}

const std::string &Block::getListenPort() const{
if (listenPort_ != "")
return listenPort_;
else if (listenPort_ == "" && parent_)
return (parent_->getListenPort());
const std::string &Block::getHost() const {
if (host_ != "")
return host_;
else if (host_ == "" && parent_)
return (parent_->getHost());
throw std::runtime_error("Invalid configuration file: no port set\n");
}

const int &Block::getPort() const {
if (port_)
return port_;
else if (port_ == 0 && parent_)
return (parent_->getPort());
throw std::runtime_error("Invalid configuration file: no port set\n");
}

@@ -317,7 +329,8 @@ void Block::printBlock() {
std::set<std::string>::iterator itset;
std::map<int, std::string>::iterator itmap;

std::cout << "Port: " << listenPort_ << std::endl;
std::cout << "Host: " << host_ << std::endl;
std::cout << "Port: " << port_ << std::endl;
std::cout << "Server names: ";
for (itset = serverNames_.begin(); itset != serverNames_.end(); itset++)
std::cout << *itset << " ";
67 changes: 34 additions & 33 deletions srcs/conf/Block.hpp
Original file line number Diff line number Diff line change
@@ -8,58 +8,59 @@
# include <vector>
# include <map>
# include "../macro.hpp"
# include "utils.hpp"

#define directivesMap std::map<std::string, void (Block::*)(std::vector<std::string>)>

class Block {
private:
Block *parent_;
std::vector<Block> locationBlocks_;
directivesMap directivesMap_;
Block *parent_;
std::vector<Block> locationBlocks_;
directivesMap directivesMap_;

//TODO: host:port 나누어 저장
// std::string host_;
// std::string port_;
std::string listenPort_;
std::set<std::string> serverNames_;
std::string webRoot_;
std::set<std::string> allowedMethods_;
int clientMaxBodySize_;
std::map<int, std::string> errorPages_;
std::string uri_;
std::string index_;
std::string autoIndex_;
std::set<std::string> cgi_;
std::string host_;
int port_;
std::set<std::string> serverNames_;
std::string webRoot_;
std::set<std::string> allowedMethods_;
int clientMaxBodySize_;
std::map<int, std::string> errorPages_;
std::string uri_;
std::string index_;
std::string autoIndex_;
std::set<std::string> cgi_;

ft_bool check_validation(std::vector<std::string> &tokens, int &index, std::string &directive);
ft_bool check_validation(std::vector<std::string> &tokens, int &index, std::string &directive);

public:
Block();
Block(Block *parent);

void setServerDirectivesMap();
void setLocationDirectivesMap();
directivesMap getDirectivesMap();
void setServerDirectivesMap();
void setLocationDirectivesMap();
directivesMap getDirectivesMap();

ft_bool has_semi_colon(std::vector<std::string> &tokens, int &index, std::vector<std::string> *args, std::string &directive);
void parseServerBlock(std::vector<std::string> &tokens, int &index);
void parseLocationBlock(std::vector<std::string> &tokens, int &i);
ft_bool has_semi_colon(std::vector<std::string> &tokens, int &index, std::vector<std::string> *args, std::string &directive);
void parseServerBlock(std::vector<std::string> &tokens, int &index);
void parseLocationBlock(std::vector<std::string> &tokens, int &i);

// ServerBlock 만 해당
void setListenPort(std::vector<std::string> args);
void setServerNames(std::vector<std::string> args);
void setHostPort(std::vector<std::string> args);
void setServerNames(std::vector<std::string> args);
// ServerBlock, LocationBlock 공통
void setWebRoot(std::vector<std::string> args);
void setAllowedMethods(std::vector<std::string> args);
void setClientMaxBodySize(std::vector<std::string> args);
void setErrorPages(std::vector<std::string> args);
void setWebRoot(std::vector<std::string> args);
void setAllowedMethods(std::vector<std::string> args);
void setClientMaxBodySize(std::vector<std::string> args);
void setErrorPages(std::vector<std::string> args);
// LocationBlock 만 해당
void setUri(std::string uri);
void setIndex(std::vector<std::string> args);
void setAutoIndex(std::vector<std::string> args);
void setCgi(std::vector<std::string> args);
void setUri(std::string uri);
void setIndex(std::vector<std::string> args);
void setAutoIndex(std::vector<std::string> args);
void setCgi(std::vector<std::string> args);

const std::string &getListenPort() const;
const std::string &getHost() const;
const int &getPort() const;
const std::set<std::string> &getServerNames() const;
const std::string &getWebRoot() const;
const std::set<std::string> &getAllowedMethods() const;
1 change: 0 additions & 1 deletion srcs/conf/Config.cpp
Original file line number Diff line number Diff line change
@@ -42,7 +42,6 @@ void Config::parseConfigFile(const char *filePath) {
Block tmpServerBlock;

tmpServerBlock.parseServerBlock(tokens, ++i);
// tmpServerBlock.printBlock();
serverBlocks_.push_back(tmpServerBlock);
} else
throw std::runtime_error("Wrong directive type in configuration file.\n");
2 changes: 1 addition & 1 deletion srcs/conf/Config.hpp
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@

class Config {
private:
std::vector<Block> serverBlocks_;
std::vector<Block> serverBlocks_;

std::vector<std::string> splitLines(const std::string &str, std::string delim);
std::vector<std::string> readAndSplit(const char *filePath);
16 changes: 16 additions & 0 deletions srcs/conf/utils.cpp
Original file line number Diff line number Diff line change
@@ -15,3 +15,19 @@ std::vector<std::string> parseLine(const std::string &line, std::string delim) {
}
return tokens;
}

std::vector<std::string> parseHostPort(const std::string &arg) {
size_t pos;
std::vector<std::string> args;

pos = arg.find(":");
// x.x.x.x:80 과 같은 형태가 아니면 에러
if (pos == std::string::npos || pos == 0 || pos == arg.length() - 1)
throw std::runtime_error("Wrong formatted configuration file: Listen x.x.x.x:port\n");
// host(x.x.x.x)
args.push_back(arg.substr(0, pos));
// port(80)
args.push_back(arg.substr(pos + 1, arg.length() - 1 - pos));

return args;
}
3 changes: 2 additions & 1 deletion srcs/conf/utils.hpp
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
# include <vector>
# include <string>

std::vector<std::string> parseLine(const std::string &str, std::string delim);
std::vector<std::string> parseLine(const std::string &str, std::string delim);
std::vector<std::string> parseHostPort(const std::string &arg);

#endif

0 comments on commit bbafd3e

Please sign in to comment.