Skip to content

Commit

Permalink
Validating port number and adding new Panic function
Browse files Browse the repository at this point in the history
  • Loading branch information
JustAn0therDev committed Jul 25, 2021
1 parent 21e1fc8 commit 8e791a1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ServerConfigs/server_configs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#ifndef SERVERCONFIGSCPP
#define SERVERCONFIGSCPP
class ServerConfigs
class ServerConfigs
{
public:
ServerConfigs();
Expand Down
12 changes: 10 additions & 2 deletions Util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#include <iostream>
#include "util.hpp"

int Util::TryCastStringToInt(const std::string& p_str) {
int Util::TryCastStringToInt(const std::string& p_str)
{
try {
return std::stoi(p_str);
}
Expand All @@ -13,7 +14,8 @@ int Util::TryCastStringToInt(const std::string& p_str) {
return 0;
}

uint16_t Util::TryCastStringToUnsignedShortInt(const std::string& p_str) {
uint16_t Util::TryCastStringToUnsignedShortInt(const std::string& p_str)
{
try {
return static_cast<uint16_t>(std::stoi(p_str));
}
Expand All @@ -23,3 +25,9 @@ uint16_t Util::TryCastStringToUnsignedShortInt(const std::string& p_str) {

return 0;
}

void Util::Panic(const std::string& p_panic_message)
{
std::cout << p_panic_message;
exit(1);
}
4 changes: 3 additions & 1 deletion Util/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ struct Util {
static int TryCastStringToInt(const std::string& p_str);
// Tries casting a string to an uint16_t. If not successful returns 0.
static uint16_t TryCastStringToUnsignedShortInt(const std::string& p_str);
// Writes a message to STDOUT and ends the program's execution.
static void Panic(const std::string& p_panic_message);
};
#endif
#endif
8 changes: 7 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "BlackMarlin/black_marlin.hpp"
#include "HTTP/http_request_handler.hpp"
#include "HTTP/server_connection_info.hpp"
#include "Util/util.hpp"

constexpr short DEFAULT_PORT = 7000;

Expand Down Expand Up @@ -48,11 +49,16 @@ ServerConnectionInfo GetIPAndPortFromArgs(int& argc, char** argv)
}
else
{
std::cout << "Expected only numbers for port argument. Got: " << port_arg << "\n";
Util::Panic("Expected only numbers for port argument. Got: " + std::string(port_arg) + "\n");
exit(1);
}
}

if (conn.port > USHRT_MAX)
{
Util::Panic("The port argument exceeds the range of valid ports.");
}

conn.ip = "127.0.0.1";

return conn;
Expand Down

0 comments on commit 8e791a1

Please sign in to comment.