summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/CGIScript.cpp8
-rw-r--r--src/CGIScript.h2
-rw-r--r--src/ClientConnection.cpp6
-rw-r--r--src/ClientConnection.h4
-rw-r--r--src/HttpRequest.cpp4
-rw-r--r--src/HttpResponse.cpp6
-rw-r--r--src/ServerConnection.cpp10
-rw-r--r--src/ServerConnection.h2
-rw-r--r--src/main.cpp10
9 files changed, 26 insertions, 26 deletions
diff --git a/src/CGIScript.cpp b/src/CGIScript.cpp
index a29f1eb..8731023 100644
--- a/src/CGIScript.cpp
+++ b/src/CGIScript.cpp
@@ -17,20 +17,20 @@ CGIScript::~CGIScript()
{
pclose(m_pipe);
- for (auto key : m_environment_variables)
+ for (const auto* key : m_environment_variables)
unsetenv(key);
}
void CGIScript::set_environment(const char* key, const char* value)
{
m_environment_variables.push_back(key);
- setenv(key, value, true);
+ setenv(key, value, 1);
}
bool CGIScript::open()
{
m_pipe = popen(m_script_path.c_str(), "r");
- if (!m_pipe) {
+ if (m_pipe == nullptr) {
perror("cfws: popen");
return false;
}
@@ -43,7 +43,7 @@ std::string CGIScript::read_output()
{
std::stringstream sstream;
- char ch;
+ char ch = 0;
while ((ch = fgetc(m_pipe)) != EOF)
sstream << ch;
diff --git a/src/CGIScript.h b/src/CGIScript.h
index 5a7f46e..a3e40fd 100644
--- a/src/CGIScript.h
+++ b/src/CGIScript.h
@@ -15,7 +15,7 @@ public:
std::string read_output();
private:
- FILE* m_pipe;
+ FILE* m_pipe {};
const std::string& m_script_path;
bool m_is_open { false };
diff --git a/src/ClientConnection.cpp b/src/ClientConnection.cpp
index 6a0670c..c434212 100644
--- a/src/ClientConnection.cpp
+++ b/src/ClientConnection.cpp
@@ -10,13 +10,13 @@ ClientConnection::ClientConnection(int socket)
{
}
-HttpRequest ClientConnection::read_request()
+HttpRequest ClientConnection::read_request() const
{
// TODO: Clean up this code to ensure it works with multiple lines
// and not risk a buffer overflow.
constexpr int BUFFER_SIZE = 4096;
char buffer[BUFFER_SIZE + 1];
- int n;
+ int n = 0;
memset(buffer, 0, BUFFER_SIZE);
while ((n = read(m_socket_fd, buffer, BUFFER_SIZE - 1)) > 0) {
@@ -29,7 +29,7 @@ HttpRequest ClientConnection::read_request()
return HttpRequest(buffer);
}
-bool ClientConnection::send(const HttpResponse& response)
+bool ClientConnection::send(const HttpResponse& response) const
{
if (!m_is_open)
return false;
diff --git a/src/ClientConnection.h b/src/ClientConnection.h
index 9d5eadd..fee0aeb 100644
--- a/src/ClientConnection.h
+++ b/src/ClientConnection.h
@@ -7,9 +7,9 @@ class ClientConnection {
public:
ClientConnection(int socket);
- HttpRequest read_request();
+ HttpRequest read_request() const;
- bool send(const HttpResponse&);
+ bool send(const HttpResponse&) const;
void close_connection();
private:
diff --git a/src/HttpRequest.cpp b/src/HttpRequest.cpp
index d1d183e..37c85fd 100644
--- a/src/HttpRequest.cpp
+++ b/src/HttpRequest.cpp
@@ -11,14 +11,14 @@ HttpRequest::HttpRequest(const std::string& request_string)
line = s.substr(0, pos);
if (line.find("GET ") != std::string::npos) {
- m_uri = s.substr(4, line.find(" ", 5) - 4);
+ m_uri = s.substr(4, line.find(' ', 5) - 4);
std::cout << m_uri << std::endl;
}
// If the line contains a colon, we assume it's a header.
// TODO: This may not always be the case.
size_t delim_pos = 0;
- if ((delim_pos = line.find(":")) != std::string::npos) {
+ if ((delim_pos = line.find(':')) != std::string::npos) {
std::string header_key = s.substr(0, delim_pos);
std::string header_value = s.substr(delim_pos + 2, s.find("\r\n") - delim_pos - 2);
m_headers[header_key] = header_value;
diff --git a/src/HttpResponse.cpp b/src/HttpResponse.cpp
index 2f330e5..c8afc63 100644
--- a/src/HttpResponse.cpp
+++ b/src/HttpResponse.cpp
@@ -9,14 +9,14 @@ void HttpResponse::add_headers_and_content(const std::string& input)
size_t pos = 0;
std::string s = input;
std::string line;
- while ((pos = s.find("\n")) != std::string::npos) {
+ while ((pos = s.find('\n')) != std::string::npos) {
line = s.substr(0, pos + 1);
if (is_parsing_headers) {
size_t delim_pos = 0;
- if ((delim_pos = line.find(":")) != std::string::npos) {
+ if ((delim_pos = line.find(':')) != std::string::npos) {
std::string header_key = s.substr(0, delim_pos);
- std::string header_value = s.substr(delim_pos + 2, s.find("\n") - delim_pos - 2);
+ std::string header_value = s.substr(delim_pos + 2, s.find('\n') - delim_pos - 2);
m_headers[header_key] = header_value;
} else {
is_parsing_headers = false;
diff --git a/src/ServerConnection.cpp b/src/ServerConnection.cpp
index afe9bcb..64692b6 100644
--- a/src/ServerConnection.cpp
+++ b/src/ServerConnection.cpp
@@ -1,8 +1,8 @@
#include "ServerConnection.h"
#include <arpa/inet.h>
+#include <csignal>
#include <netdb.h>
-#include <signal.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
@@ -19,13 +19,13 @@ static void error_and_die(const char* message)
ServerConnection::ServerConnection(int port)
{
- sockaddr_in address;
+ sockaddr_in address {};
int socket_options = 1;
if ((m_socket_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
error_and_die("Failed to create socket");
- if (setsockopt(m_socket_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &socket_options, sizeof(socket_options)))
+ if (setsockopt(m_socket_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &socket_options, sizeof(socket_options)) != 0)
error_and_die("setsockopt");
address.sin_family = AF_INET;
@@ -39,8 +39,8 @@ ServerConnection::ServerConnection(int port)
error_and_die("listen");
}
-ClientConnection ServerConnection::accept_client_connection()
+ClientConnection ServerConnection::accept_client_connection() const
{
int client_socket = accept(m_socket_fd, (sockaddr*)nullptr, nullptr);
- return ClientConnection(client_socket);
+ return { client_socket };
}
diff --git a/src/ServerConnection.h b/src/ServerConnection.h
index f01a1b6..8f0af70 100644
--- a/src/ServerConnection.h
+++ b/src/ServerConnection.h
@@ -6,7 +6,7 @@ class ServerConnection {
public:
ServerConnection(int port);
- ClientConnection accept_client_connection();
+ ClientConnection accept_client_connection() const;
private:
int m_socket_fd;
diff --git a/src/main.cpp b/src/main.cpp
index d8c343a..89a8139 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -10,7 +10,7 @@
#include "HttpResponse.h"
#include "ServerConnection.h"
-static HttpResponse serve_from_filesystem(HttpRequest request)
+static HttpResponse serve_from_filesystem(const HttpRequest& request)
{
namespace fs = std::filesystem;
@@ -50,7 +50,7 @@ static HttpResponse serve_from_filesystem(HttpRequest request)
return response;
}
-static HttpResponse serve_from_cgi(const std::string& script_path, HttpRequest request)
+static HttpResponse serve_from_cgi(const std::string& script_path, const HttpRequest& request)
{
HttpResponse response;
response.add_header("Server", "cfws");
@@ -85,8 +85,8 @@ static HttpResponse serve_from_cgi(const std::string& script_path, HttpRequest r
}
static option long_options[] = {
- { "cgi", required_argument, NULL, 'c' },
- { "port", required_argument, NULL, 'p' },
+ { "cgi", required_argument, nullptr, 'c' },
+ { "port", required_argument, nullptr, 'p' },
};
int main(int argc, char** argv)
@@ -95,7 +95,7 @@ int main(int argc, char** argv)
bool in_cgi_mode = false;
std::string cgi_program_name;
- int c;
+ int c = 0;
int option_index = 0;
while ((c = getopt_long(argc, argv, "c:p:", long_options, &option_index)) != -1) {
switch (c) {