From 2563dc391ac4bb63448217db48df30ddb6075a10 Mon Sep 17 00:00:00 2001 From: FiRe Date: Tue, 1 Mar 2022 14:44:22 -0600 Subject: [PATCH] Added socket and added header class --- Source/HTTP/ServerThread.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++- Source/HTTP/ServerThread.hpp | 43 ++++++++++++-------- Source/Headers/Headers.cpp | 8 ++++ Source/Headers/Headers.hpp | 40 +++++++++++++++++++ Source/main.cpp | 2 +- 5 files changed, 166 insertions(+), 20 deletions(-) rewrite Source/HTTP/ServerThread.hpp (62%) create mode 100644 Source/Headers/Headers.cpp create mode 100644 Source/Headers/Headers.hpp diff --git a/Source/HTTP/ServerThread.cpp b/Source/HTTP/ServerThread.cpp index ab45bf4..6719c8f 100644 --- a/Source/HTTP/ServerThread.cpp +++ b/Source/HTTP/ServerThread.cpp @@ -1,6 +1,33 @@ #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include +#include #include "ServerThread.hpp" +#include "../Headers/Headers.hpp" + +/* + * Manages HTTP connections. + */ +void* ConnectionHandler(void* arg) { + HTTP::ClientData* Data = (HTTP::ClientData*)arg; + char msg[1000]; + Headers* req = new Headers(Data->getSocket()); + recv(Data->getSocket(), msg, 1000, 0); + std::string res = ""; + write(Data->getSocket(), res.data(), res.size()); + close(Data->getSocket()); + pthread_exit(NULL); + return NULL; +} /* * Manages HTTP connections. @@ -9,7 +36,28 @@ */ void run(void* arg) { HTTP::ServerThread* ServerThread = (HTTP::ServerThread*)arg; - std::cout << "[HTTP Server Thread] Running on port " << ServerThread->getPort() << " with " << ServerThread->getMaxConnections() << " max connections." << std::endl; + int ServerSocket = socket(PF_INET, SOCK_STREAM, 0), ClientSocket; + struct sockaddr_in ServerAddress; + struct sockaddr_storage ServerStorage; + socklen_t AddressSize; + ServerAddress.sin_family = AF_INET; + ServerAddress.sin_port = htons(ServerThread->getPort()); + ServerAddress.sin_addr.s_addr = INADDR_ANY; + bzero(&(ServerAddress.sin_zero), 8); + if (bind(ServerSocket, (struct sockaddr*)&ServerAddress, sizeof(ServerAddress)) != 0) { + fprintf(stderr, "[HTTP][!] Could not bind to port %d\n", ServerThread->getPort()); + exit(0); + } + listen(ServerSocket, ServerThread->getMaxConnections()); + fprintf(stdout, "[HTTP][#] Listening on port %d\n", ServerThread->getPort()); + pthread_t ThreadID[ServerThread->getMaxConnections()+ServerThread->getMaxQueuedConnections()]; + int CurrentThread = 0; + while (true) { + AddressSize = sizeof(ServerStorage); + ClientSocket = accept(ServerSocket, (struct sockaddr*)&ServerStorage, &AddressSize); + HTTP::ClientData ClientData(ServerThread, ClientSocket); + pthread_create(&ThreadID[CurrentThread++], NULL, ConnectionHandler, &ClientData); + } } namespace HTTP { @@ -19,9 +67,10 @@ namespace HTTP { * @param port The port to listen on. * @param maxConnections The maximum number of connections to accept. */ - ServerThread::ServerThread(int port, int maxConnections) { + ServerThread::ServerThread(int port, int maxConnections, int maxQueuedConnections) { this->port = port; this->maxConnections = maxConnections; + this->maxQueuedConnections = maxQueuedConnections; std::thread Thread = std::thread(run, (void*)this); Thread.join(); } @@ -43,4 +92,44 @@ namespace HTTP { int ServerThread::getMaxConnections() { return this->maxConnections; } + + /* + * Gets the maximum number of queued connections. + * + * @return The maximum number of queued connections. + */ + int ServerThread::getMaxQueuedConnections() { + return this->maxQueuedConnections; + } + + /* + * Holds all the client's needed data. + * + * @param serverThread The server thread. + * @param socket The socket. + * @param address The address. + */ + ClientData::ClientData(ServerThread* serverThread, int socket) { + this->serverThread = serverThread; + this->socket = socket; + } + + /* + * Gets the server thread. + * + * @return The server thread. + * + */ + ServerThread* ClientData::getServerThread() { + return this->serverThread; + } + + /* + * Gets the socket. + * + * @return The socket. + */ + int ClientData::getSocket() { + return this->socket; + } } \ No newline at end of file diff --git a/Source/HTTP/ServerThread.hpp b/Source/HTTP/ServerThread.hpp dissimilarity index 62% index 009dfd7..cf74019 100644 --- a/Source/HTTP/ServerThread.hpp +++ b/Source/HTTP/ServerThread.hpp @@ -1,17 +1,26 @@ -#ifndef HTTP_ServerThread -#define HTTP_ServerThread - -/* - * Manages HTTP connections. - */ -namespace HTTP { - class ServerThread { - public: - ServerThread(int port, int maxConnections); - int getPort(), getMaxConnections(); - private: - int port, maxConnections; - }; -} - -#endif \ No newline at end of file +#ifndef HTTP + +/* + * Manages HTTP connections. + */ +namespace HTTP { + class ServerThread { + public: + ServerThread(int port, int maxConnections, int maxQueuedConnections); + int getPort(), getMaxConnections(), getMaxQueuedConnections(); + private: + int port, maxConnections, maxQueuedConnections; + }; + + class ClientData { + public: + ClientData(ServerThread* serverThread, int socket); + ServerThread* getServerThread(); + int getSocket(); + private: + ServerThread* serverThread; + int socket; + }; +} + +#endif \ No newline at end of file diff --git a/Source/Headers/Headers.cpp b/Source/Headers/Headers.cpp new file mode 100644 index 0000000..d31f8d7 --- /dev/null +++ b/Source/Headers/Headers.cpp @@ -0,0 +1,8 @@ +#include "Headers.hpp" + +/* + * Initialize Headers. + */ +Headers::Headers(int ClientSocket) { + this->ClientSocket = ClientSocket; +} \ No newline at end of file diff --git a/Source/Headers/Headers.hpp b/Source/Headers/Headers.hpp new file mode 100644 index 0000000..9d66453 --- /dev/null +++ b/Source/Headers/Headers.hpp @@ -0,0 +1,40 @@ +#ifndef Headers +#include +#include +#include + +/* + * HTTP Headers. + */ +class Headers { + public: + Headers(int ClientSocket); + void Send(std::string Message); + private: + int ClientSocket; + std::string FinalHeaders, Body, AcceptCharset, AcceptEncoding, AcceptLanguage, AcceptPatch, AcceptPost, + AcceptRanges, Accept, AccessControlAllowHeaders, AccessControlAllowMethods, AccessControlAllowOrigin, + AccessControlExposeHeaders, AccessControlRequestMethod, Allow, AltSvc, Authorization, CacheControl, + Connection, ContentDisposition, ContentLocation, ContentRange, ContentSecurityPolicyReportOnly, + ContentSecurityPolicy, ContentType, Cookie, Cookie2, CrossOriginEmbedderPolicy, CrossOriginOpenerPolicy, + CrossOriginResourcePolicy, Date, Digest, ECT, ETag, Expect, Expires, FeaturePolicy, Forwarded, From, Host, + Link, Location, NEL, Origin, Pragma, ProxyAuthenticate, ProxyAuthorization, PublicKeyPinsReportOnly, + PublicKeyPins, Range, Referer, ReferrerPolicy, RetryAfter, SecCHUAArch, SecCHUABitness, SecCHUAFullVersion, + SecCHUAModel, SecCHUAPlatformVersion, SecCHUAPlatform, SecFetchDest, SecFetchMode, SecFetchSite, + SecWebSocketAccept, ServerTiming, Server, SetCookie, SetCookie2, SourceMap, StrictTransportSecurity, + TimingAllowOrigin, Trailer, UserAgent, WantDigest, Warning, XContentTypeOptions, XForwardedProto, + XFrameOptions, XXSSProtection; + int AcceptCHLifetime, AccessControlMaxAge, Age, ContentDPR, ContentLength, DNT, EarlyData, LargeAllocation, + RTT, ViewportWidth, Width; + float DeviceMemory, Downlink, DPR; + std::vector AcceptCH, AccessControlRequestHeaders, ClearSiteData, ContentEncoding, + ContentLanguage, IfMatch, IfModifiedSince, IfNoneMatch, IfRange, IfUnmodifiedSince, KeepAlive, + LastModified, SecCHUAFullVersionList, SecCHUA, TE, TransferEncoding, Upgrade, Vary, Via, WWWAuthenticate, + XForwardedFor, XForwardedHost; + std::string ExpectCT[3]; + bool AccessControlAllowCredentials, SaveData, SecCHUAMobile, SecFetchUser, UpgradeInsecureRequests, + XDNSPrefetchControl; + char Tk; +}; + +#endif \ No newline at end of file diff --git a/Source/main.cpp b/Source/main.cpp index 50968d0..ebc82cf 100644 --- a/Source/main.cpp +++ b/Source/main.cpp @@ -3,6 +3,6 @@ int main() { std::cout << "[Initializing HTTP Server Thread]" << std::endl; - HTTP::ServerThread serverThread(80, 100); + HTTP::ServerThread serverThread(80, 100, 10); return 0; } \ No newline at end of file -- 2.11.4.GIT