Skip to content

Commit

Permalink
Merge pull request #391 from dantol29/slombard/web-339-allow-multiple…
Browse files Browse the repository at this point in the history
…-ports-from-different-ips

fix(ServerBlock/Config): allow same port with different IPs
  • Loading branch information
dantol29 authored May 24, 2024
2 parents bdbeb6f + 58c1368 commit 0fcd958
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 118 deletions.
10 changes: 10 additions & 0 deletions conf/multiple_port_different_ips.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
server {
listen 8080;
listen 127.0.0.1:8080;
listen 127.0.0.2:8080;
autoindex on;
cgi_ext .cgi;
server_name www.development_site;
allow_methods GET POST DELETE;
root var/;
}
40 changes: 20 additions & 20 deletions experiments/issues.csv
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
Summary,Issue key,Issue id,Issue Type,Status,Project key,Project name,Creator,Created,Updated
Fix the issue,Issue11,11,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue12,12,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue13,13,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue14,14,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue15,15,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue16,16,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue17,17,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue18,18,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue19,19,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue20,20,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue21,21,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue22,22,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue23,23,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue24,24,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue25,25,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue26,26,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue27,27,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue28,28,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue29,29,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue30,30,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue61,61,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue62,62,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue63,63,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue64,64,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue65,65,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue66,66,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue67,67,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue68,68,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue69,69,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue70,70,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue71,71,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue72,72,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue73,73,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue74,74,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue75,75,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue76,76,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue77,77,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue78,78,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue79,79,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
Fix the issue,Issue80,80,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10
116 changes: 18 additions & 98 deletions src/ServerBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,18 @@ size_t ServerBlock::getLimitConn() const
void ServerBlock::setListen(Listen str, bool isLocation)
{
if (!isLocation)
_directives._listen.push_back(str);
else
throw("listen directive not allowed in location block");

for (unsigned int i = 0; i < _directives._listen.size(); ++i)
{
for (unsigned int j = 0; j < _directives._listen.size(); ++j)
std::vector<Listen>::iterator it;

for (it = _directives._listen.begin(); it != _directives._listen.end(); ++it)
{
if (i != j && _directives._listen[i].getPort() == _directives._listen[j].getPort())
throw("Duplicate listen directive");
if (it->getPort() == str.getPort() && it->getIp() == str.getIp())
throw std::runtime_error("Duplicate listen directive with same IP and port");
}
_directives._listen.push_back(str);
}
else
throw std::runtime_error("listen directive not allowed in location block");
}

void ServerBlock::setServerName(std::vector<std::string> str, bool isLocation)
Expand All @@ -223,13 +223,13 @@ void ServerBlock::setErrorPage(std::pair<int, std::string> str, bool isLocation)
if (isLocation)
throw("error_page directive not allowed in location block");
_directives._errorPage.push_back(str);

for (unsigned int i = 0; i < _directives._errorPage.size(); ++i)
{
// remove slashes at the begginning
if ( _directives._errorPage[i].second[0] == '/')
if (_directives._errorPage[i].second[0] == '/')
_directives._errorPage[i].second = _directives._errorPage[i].second.substr(1);

for (unsigned int j = 0; j < _directives._errorPage.size(); ++j)
{
if (i != j && _directives._errorPage[i].first == _directives._errorPage[j].first)
Expand Down Expand Up @@ -460,100 +460,20 @@ std::vector<std::string> ServerBlock::transformServerName(std::string &str)
return (newStr);
}

// Listen ServerBlock::makeListenStruct(std::string &newStr)
// {
// Listen listen;
// int port;
// std::string ip;
// std::string portStr;
// bool isIpAndPort = false;
// struct addrinfo hints;
// struct addrinfo *res;

// listen.isIpv6 = false;
// memset(&hints, 0, sizeof(hints));
// hints.ai_family = AF_UNSPEC; // IPv4 or IPv6
// hints.ai_socktype = SOCK_STREAM; // TCP socket

// // if IPv6 is in [ip]:port format
// if (newStr[0] == '[')
// newStr.erase(0, 1);
// if (newStr.find(']') != std::string::npos)
// newStr.replace(newStr.find(']'), 1, "");

// // (IPv6:port) or (IPv6) or (IPv4) or (port)
// if (getaddrinfo(newStr.c_str(), NULL, &hints, &res) == 0)
// {
// freeaddrinfo(res);
// portStr = newStr;

// // (IPv6:port)
// if (newStr.find_last_of(':') != std::string::npos)
// {
// portStr = newStr.substr(newStr.find_last_of(':') + 1);
// isIpAndPort = true;
// }

// port = strToInt(portStr);
// if (port >= 1 && port <= 65535)
// {
// listen._port = port;
// if (!isIpAndPort)
// {
// listen._ip = "Any";
// return (listen);
// }
// }
// // is incorrect integer
// else if ((port < 1 || port > 65535) && port != -1)
// throw("Invalid port");

// ip = newStr;
// // (IPv6:port)
// if (isIpAndPort)
// ip = newStr.substr(0, newStr.find_last_of(':'));
// listen._ip = ip;
// listen.isIpv6 = true;
// }
// // (IPv4:port)
// else
// {
// ip = newStr.substr(0, newStr.find_last_of(':'));
// portStr = newStr.substr(newStr.find_last_of(':') + 1);
// port = strToInt(portStr);
// if (port < 1 || port > 65535)
// throw("Invalid port");
// listen._ip = ip;

// if (getaddrinfo(ip.c_str(), NULL, &hints, &res) != 0)
// throw("Invalid ip");
// freeaddrinfo(res);
// listen._port = port;
// }

// if (listen._ip.empty())
// listen._ip = "Any";
// if (listen._port == 0)
// listen._port = 0;

// return (listen);
// }

void Directives::setListenEntry(Listen listenEntry, bool isLocation)
{
if (!isLocation)
_listen.push_back(listenEntry);
else
throw("listen directive not allowed in location block");

for (unsigned int i = 0; i < _listen.size(); ++i)
{
for (unsigned int j = 0; j < _listen.size(); ++j)
std::vector<Listen>::iterator it;
for (it = _listen.begin(); it != _listen.end(); ++it)
{
if (i != j && _listen[i].getPort() == _listen[j].getPort())
throw("Duplicate listen directive");
if (it->getPort() == listenEntry.getPort() && it->getIp() == listenEntry.getIp())
throw std::runtime_error("Duplicate listen directive with same IP and port");
}
_listen.push_back(listenEntry);
}
else
throw("listen directive not allowed in location block");
}

void ServerBlock::transformServerListen(std::string &str, bool isLocation)
Expand Down

0 comments on commit 0fcd958

Please sign in to comment.