summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcflip <cflip@cflip.net>2022-09-19 21:46:12 -0600
committercflip <cflip@cflip.net>2022-09-19 21:46:12 -0600
commitbcea88142033f37bffeca4df096fb7795ebf7020 (patch)
treed1f990532092327c3269ac3bed86eec6061a3067
parentb8766fcef6dcc40bd46584015375ea76518c6201 (diff)
Add ClangFormat configuration and reformat files
-rw-r--r--.clang-format6
-rw-r--r--src/CGIScript.cpp2
-rw-r--r--src/CGIScript.h3
-rw-r--r--src/ClientConnection.cpp10
-rw-r--r--src/ClientConnection.h1
-rw-r--r--src/HttpRequest.cpp1
-rw-r--r--src/HttpRequest.h1
-rw-r--r--src/HttpResponse.cpp3
-rw-r--r--src/HttpResponse.h1
-rw-r--r--src/ServerConnection.cpp16
-rw-r--r--src/ServerConnection.h1
-rw-r--r--src/main.cpp2
12 files changed, 29 insertions, 18 deletions
diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..fa16c48
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,6 @@
+---
+Language: Cpp
+BasedOnStyle: WebKit
+UseTab: Always
+TabWidth: 4
+PackConstructorInitializers: NextLine
diff --git a/src/CGIScript.cpp b/src/CGIScript.cpp
index c630459..a29f1eb 100644
--- a/src/CGIScript.cpp
+++ b/src/CGIScript.cpp
@@ -1,8 +1,8 @@
#include "CGIScript.h"
#include <cstdlib>
-#include <string>
#include <sstream>
+#include <string>
CGIScript::CGIScript(const std::string& script_path)
: m_script_path(script_path)
diff --git a/src/CGIScript.h b/src/CGIScript.h
index 622206d..5a7f46e 100644
--- a/src/CGIScript.h
+++ b/src/CGIScript.h
@@ -13,10 +13,11 @@ public:
bool open();
std::string read_output();
+
private:
FILE* m_pipe;
const std::string& m_script_path;
- bool m_is_open{false};
+ bool m_is_open { false };
std::vector<const char*> m_environment_variables;
};
diff --git a/src/ClientConnection.cpp b/src/ClientConnection.cpp
index 8982780..6a0670c 100644
--- a/src/ClientConnection.cpp
+++ b/src/ClientConnection.cpp
@@ -1,9 +1,9 @@
#include "ClientConnection.h"
-#include <unistd.h>
+#include <cstring>
#include <iostream>
#include <sstream>
-#include <cstring>
+#include <unistd.h>
ClientConnection::ClientConnection(int socket)
: m_socket_fd(socket)
@@ -15,12 +15,12 @@ HttpRequest ClientConnection::read_request()
// 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];
+ char buffer[BUFFER_SIZE + 1];
int n;
memset(buffer, 0, BUFFER_SIZE);
- while ((n = read(m_socket_fd, buffer, BUFFER_SIZE-1)) > 0) {
- if (buffer[n-1] == '\n')
+ while ((n = read(m_socket_fd, buffer, BUFFER_SIZE - 1)) > 0) {
+ if (buffer[n - 1] == '\n')
break;
memset(buffer, 0, BUFFER_SIZE);
diff --git a/src/ClientConnection.h b/src/ClientConnection.h
index b322c07..9d5eadd 100644
--- a/src/ClientConnection.h
+++ b/src/ClientConnection.h
@@ -11,6 +11,7 @@ public:
bool send(const HttpResponse&);
void close_connection();
+
private:
int m_socket_fd;
bool m_is_open { true };
diff --git a/src/HttpRequest.cpp b/src/HttpRequest.cpp
index 27a5ce5..d1d183e 100644
--- a/src/HttpRequest.cpp
+++ b/src/HttpRequest.cpp
@@ -22,7 +22,6 @@ HttpRequest::HttpRequest(const std::string& request_string)
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;
- //std::cout << "1?: {" << header_maybe << "}, 2?: {" << value_maybe << "}\n";
}
s.erase(0, pos + 2);
}
diff --git a/src/HttpRequest.h b/src/HttpRequest.h
index 3d20e57..cb98bea 100644
--- a/src/HttpRequest.h
+++ b/src/HttpRequest.h
@@ -9,6 +9,7 @@ public:
std::string uri() const { return m_uri; }
std::string header(const std::string& header_key) const { return m_headers.at(header_key); };
+
private:
std::map<std::string, std::string> m_headers;
std::string m_uri;
diff --git a/src/HttpResponse.cpp b/src/HttpResponse.cpp
index c07178f..2f330e5 100644
--- a/src/HttpResponse.cpp
+++ b/src/HttpResponse.cpp
@@ -48,7 +48,8 @@ std::string HttpResponse::to_string() const
string_stream << "HTTP/1.0 " << status_code_string(m_status_code) << "\r\n";
for (const auto& header : m_headers)
string_stream << header.first << ": " << header.second << "\r\n";
- string_stream << "\r\n" << m_content;
+ string_stream << "\r\n"
+ << m_content;
return string_stream.str();
}
diff --git a/src/HttpResponse.h b/src/HttpResponse.h
index 61b5ea4..b55e01a 100644
--- a/src/HttpResponse.h
+++ b/src/HttpResponse.h
@@ -22,6 +22,7 @@ public:
void add_headers_and_content(const std::string&);
std::string to_string() const;
+
private:
HttpStatusCode m_status_code;
std::map<std::string, std::string> m_headers;
diff --git a/src/ServerConnection.cpp b/src/ServerConnection.cpp
index d9e0203..afe9bcb 100644
--- a/src/ServerConnection.cpp
+++ b/src/ServerConnection.cpp
@@ -1,13 +1,13 @@
#include "ServerConnection.h"
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <signal.h>
+#include <sys/ioctl.h>
#include <sys/socket.h>
+#include <sys/time.h>
#include <sys/types.h>
-#include <signal.h>
#include <unistd.h>
-#include <arpa/inet.h>
-#include <sys/time.h>
-#include <sys/ioctl.h>
-#include <netdb.h>
#include "ClientConnection.h"
@@ -26,11 +26,11 @@ ServerConnection::ServerConnection(int port)
error_and_die("Failed to create socket");
if (setsockopt(m_socket_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &socket_options, sizeof(socket_options)))
- error_and_die("setsockopt");
+ error_and_die("setsockopt");
- address.sin_family = AF_INET;
+ address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
- address.sin_port = htons(port);
+ address.sin_port = htons(port);
if ((bind(m_socket_fd, (sockaddr*)&address, sizeof(address))) < 0)
error_and_die("bind");
diff --git a/src/ServerConnection.h b/src/ServerConnection.h
index 0359b5e..f01a1b6 100644
--- a/src/ServerConnection.h
+++ b/src/ServerConnection.h
@@ -7,6 +7,7 @@ public:
ServerConnection(int port);
ClientConnection accept_client_connection();
+
private:
int m_socket_fd;
};
diff --git a/src/main.cpp b/src/main.cpp
index 481dc41..d8c343a 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,8 +1,8 @@
#include <getopt.h>
-#include <iostream>
#include <filesystem>
#include <fstream>
+#include <iostream>
#include "CGIScript.h"
#include "ClientConnection.h"