Skip to content

Commit

Permalink
feat(ConfigParser, Server): added required directives in context
Browse files Browse the repository at this point in the history
- fixed compile bug in kqueue
  • Loading branch information
Taanviir committed Apr 21, 2024
1 parent c588ec2 commit 8dc9319
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
2 changes: 1 addition & 1 deletion sources/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int main(int argc, char **argv) {
vector<ServerConfig> configs = parser.parse();
// configs[0].print();
Server &webserv = Server::get_instance(configs[0], 10);
webserv.start(SELECT);
webserv.start(KQUEUE);
}
catch (std::exception &error)
{
Expand Down
36 changes: 30 additions & 6 deletions sources/parser/ConfigParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,24 @@ Location ConfigParser::_parse_location_context(void)
THROW_EXCEPTION_WITH_INFO(ERR_INVALID_LOCATION);
++_itr; // move to location content

set<string> parsedLocationDirectives;
while (*_itr != "}") {
if (*_itr == "root")
if (*_itr == "root") {
location.root = _parse_root();
else if (*_itr == "autoindex")
check_duplicate_directive(parsedLocationDirectives, "root");
}
else if (*_itr == "autoindex") {
location.autoindex = _parse_autoindex();
check_duplicate_directive(parsedLocationDirectives, "autoindex");
}
else if (*_itr == "index")
location.indexFiles = _parse_index(location.root);
else {
cout << *_itr << endl;
THROW_EXCEPTION_WITH_INFO(ERR_UNEXPECTED_TOKENS_IN_LOCATION);
else if (*_itr == "client_max_body_size") {
location.maxBodySize = _parse_client_max_body_size();
check_duplicate_directive(parsedLocationDirectives, "client_max_body_size");
}
else
THROW_EXCEPTION_WITH_INFO(ERR_UNEXPECTED_TOKENS_IN_LOCATION);
++_itr;
}
++_itr; // move to next directive
Expand Down Expand Up @@ -308,16 +315,33 @@ vector<ServerConfig> ConfigParser::_parse_HTTP_context(void)
THROW_EXCEPTION_WITH_INFO(ERR_OPENINING_BRACE);
++_itr; // move to first directive

set<string> parsedHTTPDirectives;
vector<ServerConfig> serverConfigs;
while (*_itr != "}") {
if (*_itr == "server")
serverConfigs.push_back(_parse_server_context());
else if (*_itr == "root") {
// _serverConfig.serverRoot = _parse_root();
check_duplicate_directive(parsedHTTPDirectives, "root");
}
else if (*_itr == "index")
; // index here will be default index for all servers
else if (*_itr == "error_page")
; // error_page here will be default error_page for all servers
else if (*_itr == "client_max_body_size") {
// _serverConfig.maxBodySize = _parse_client_max_body_size();
check_duplicate_directive(parsedHTTPDirectives, "client_max_body_size");
}
else if (*_itr == "autoindex") {
// _serverConfig.autoindex = _parse_autoindex();
check_duplicate_directive(parsedHTTPDirectives, "autoindex");
}
else
THROW_EXCEPTION_WITH_INFO(ERR_UNEXPECTED_TOKENS_IN);
++_itr;
}

if ((_itr + 1) != _tokens.end())
if ((_itr + 1) != _tokens.end()) // after http context no more tokens should be present
THROW_EXCEPTION_WITH_INFO(ERR_UNEXPECTED_TOKENS_OUT);

return serverConfigs;
Expand Down
23 changes: 13 additions & 10 deletions sources/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,21 @@ void Server::kqueue_strat()
THROW_EXCEPTION_WITH_INFO(strerror(errno));
}
}
else
else if (eventList[i].flags & EV_EOF)
{
DEBUG_MSG("reading from connection", M);
if (handle_connection(eventList[i].ident) == CLOSE_CONNECTION)
{
// remove connection from kqueue
EV_SET(&changeList, eventList[i].ident, EVFILT_READ, EV_DELETE, 0, 0, 0);
if (kevent(kq, &changeList, 1, NULL, 0, NULL) == -1) {
close(kq);
THROW_EXCEPTION_WITH_INFO(strerror(errno));
}
// remove connection from kqueue
EV_SET(&changeList, eventList[i].ident, EVFILT_READ, EV_DELETE, 0, 0, 0);
if (kevent(kq, &changeList, 1, NULL, 0, NULL) == -1) {
close(kq);
THROW_EXCEPTION_WITH_INFO(strerror(errno));
}
close(eventList[i].ident);
// this should be done in handle_connection
}
else if (eventList[i].flags & EVFILT_READ)
{
DEBUG_MSG("reading from connection", M);
//TODO HANDLE READ
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion sources/server/ServerConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ struct Location {
string root;
vector<string> indexFiles;
bool autoindex;
Location() : autoindex(false) {}
size_t maxBodySize;
Location() : autoindex(false), maxBodySize(1000000) {}
};

struct ServerConfig {
Expand All @@ -22,6 +23,7 @@ struct ServerConfig {
vector<Location> locations;
map<STATUS_CODE, string> errorPages;
bool autoindex;
in_addr_t serverAddr;

ServerConfig() : listenerPort(8080), maxBodySize(1000000), autoindex(false) {}

Expand Down

0 comments on commit 8dc9319

Please sign in to comment.