From bcea88142033f37bffeca4df096fb7795ebf7020 Mon Sep 17 00:00:00 2001 From: cflip Date: Mon, 19 Sep 2022 21:46:12 -0600 Subject: Add ClangFormat configuration and reformat files --- .clang-format | 6 ++++++ src/CGIScript.cpp | 2 +- src/CGIScript.h | 3 ++- src/ClientConnection.cpp | 10 +++++----- src/ClientConnection.h | 1 + src/HttpRequest.cpp | 1 - src/HttpRequest.h | 1 + src/HttpResponse.cpp | 3 ++- src/HttpResponse.h | 1 + src/ServerConnection.cpp | 16 ++++++++-------- src/ServerConnection.h | 1 + src/main.cpp | 2 +- 12 files changed, 29 insertions(+), 18 deletions(-) create mode 100644 .clang-format 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 -#include #include +#include 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 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 +#include #include #include -#include +#include 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 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 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 +#include +#include +#include #include +#include #include -#include #include -#include -#include -#include -#include #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 -#include #include #include +#include #include "CGIScript.h" #include "ClientConnection.h" -- cgit v1.2.3