diff options
Diffstat (limited to 'backend')
31 files changed, 595 insertions, 0 deletions
diff --git a/backend/.vs/ProjectSettings.json b/backend/.vs/ProjectSettings.json new file mode 100644 index 0000000..0cf5ea5 --- /dev/null +++ b/backend/.vs/ProjectSettings.json @@ -0,0 +1,3 @@ +{ + "CurrentProjectSetting": "No Configurations" +}
\ No newline at end of file diff --git a/backend/.vs/VSWorkspaceState.json b/backend/.vs/VSWorkspaceState.json new file mode 100644 index 0000000..6b61141 --- /dev/null +++ b/backend/.vs/VSWorkspaceState.json @@ -0,0 +1,6 @@ +{ + "ExpandedNodes": [ + "" + ], + "PreviewInSolutionExplorer": false +}
\ No newline at end of file diff --git a/backend/.vs/backend/v16/.suo b/backend/.vs/backend/v16/.suo Binary files differnew file mode 100644 index 0000000..b4714d2 --- /dev/null +++ b/backend/.vs/backend/v16/.suo diff --git a/backend/.vs/backend/v16/Browse.VC.db b/backend/.vs/backend/v16/Browse.VC.db Binary files differnew file mode 100644 index 0000000..f98ab3e --- /dev/null +++ b/backend/.vs/backend/v16/Browse.VC.db diff --git a/backend/.vs/slnx.sqlite b/backend/.vs/slnx.sqlite Binary files differnew file mode 100644 index 0000000..40c8fc4 --- /dev/null +++ b/backend/.vs/slnx.sqlite diff --git a/backend/Server/.vs/Server/v16/.suo b/backend/Server/.vs/Server/v16/.suo Binary files differnew file mode 100644 index 0000000..5ca583f --- /dev/null +++ b/backend/Server/.vs/Server/v16/.suo diff --git a/backend/Server/.vs/Server/v16/Browse.VC.db b/backend/Server/.vs/Server/v16/Browse.VC.db Binary files differnew file mode 100644 index 0000000..e2d4096 --- /dev/null +++ b/backend/Server/.vs/Server/v16/Browse.VC.db diff --git a/backend/Server/.vs/Server/v16/ipch/AutoPCH/8c0889d90d2b555a/SERVER.ipch b/backend/Server/.vs/Server/v16/ipch/AutoPCH/8c0889d90d2b555a/SERVER.ipch Binary files differnew file mode 100644 index 0000000..5f68daa --- /dev/null +++ b/backend/Server/.vs/Server/v16/ipch/AutoPCH/8c0889d90d2b555a/SERVER.ipch diff --git a/backend/Server/Debug/Server.exe b/backend/Server/Debug/Server.exe Binary files differnew file mode 100644 index 0000000..7845e21 --- /dev/null +++ b/backend/Server/Debug/Server.exe diff --git a/backend/Server/Debug/Server.ilk b/backend/Server/Debug/Server.ilk Binary files differnew file mode 100644 index 0000000..5f58ea9 --- /dev/null +++ b/backend/Server/Debug/Server.ilk diff --git a/backend/Server/Debug/Server.pdb b/backend/Server/Debug/Server.pdb Binary files differnew file mode 100644 index 0000000..2df8dd6 --- /dev/null +++ b/backend/Server/Debug/Server.pdb diff --git a/backend/Server/Server.sln b/backend/Server/Server.sln new file mode 100644 index 0000000..f8ce6ca --- /dev/null +++ b/backend/Server/Server.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30717.126 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Server", "Server\Server.vcxproj", "{9CF802AF-02FE-48AA-9198-1DC6B51817FF}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9CF802AF-02FE-48AA-9198-1DC6B51817FF}.Debug|x64.ActiveCfg = Debug|x64 + {9CF802AF-02FE-48AA-9198-1DC6B51817FF}.Debug|x64.Build.0 = Debug|x64 + {9CF802AF-02FE-48AA-9198-1DC6B51817FF}.Debug|x86.ActiveCfg = Debug|Win32 + {9CF802AF-02FE-48AA-9198-1DC6B51817FF}.Debug|x86.Build.0 = Debug|Win32 + {9CF802AF-02FE-48AA-9198-1DC6B51817FF}.Release|x64.ActiveCfg = Release|x64 + {9CF802AF-02FE-48AA-9198-1DC6B51817FF}.Release|x64.Build.0 = Release|x64 + {9CF802AF-02FE-48AA-9198-1DC6B51817FF}.Release|x86.ActiveCfg = Release|Win32 + {9CF802AF-02FE-48AA-9198-1DC6B51817FF}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {AAA96BC8-E6A5-4E6A-8EFA-92ADECB2E8D6} + EndGlobalSection +EndGlobal diff --git a/backend/Server/Server/Debug/Server.exe.recipe b/backend/Server/Server/Debug/Server.exe.recipe new file mode 100644 index 0000000..22aff66 --- /dev/null +++ b/backend/Server/Server/Debug/Server.exe.recipe @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project> + <ProjectOutputs> + <ProjectOutput> + <FullPath>C:\Users\mattykae\Projects\website\backend\Server\Debug\Server.exe</FullPath> + </ProjectOutput> + </ProjectOutputs> + <ContentFiles /> + <SatelliteDlls /> + <NonRecipeFileRefs /> +</Project>
\ No newline at end of file diff --git a/backend/Server/Server/Debug/Server.log b/backend/Server/Server/Debug/Server.log new file mode 100644 index 0000000..099ba46 --- /dev/null +++ b/backend/Server/Server/Debug/Server.log @@ -0,0 +1,5 @@ + server.cpp +C:\Users\mattykae\Projects\website\backend\server.cpp(54,28): warning C4305: '=': truncation from 'int' to 'char' +C:\Users\mattykae\Projects\website\backend\server.cpp(54,22): warning C4309: '=': truncation of constant value +C:\Users\mattykae\Projects\website\backend\server.cpp(208,11): warning C4101: 'file': unreferenced local variable + Server.vcxproj -> C:\Users\mattykae\Projects\website\backend\Server\Debug\Server.exe diff --git a/backend/Server/Server/Debug/Server.tlog/CL.command.1.tlog b/backend/Server/Server/Debug/Server.tlog/CL.command.1.tlog Binary files differnew file mode 100644 index 0000000..a695c28 --- /dev/null +++ b/backend/Server/Server/Debug/Server.tlog/CL.command.1.tlog diff --git a/backend/Server/Server/Debug/Server.tlog/CL.read.1.tlog b/backend/Server/Server/Debug/Server.tlog/CL.read.1.tlog Binary files differnew file mode 100644 index 0000000..5cbb32c --- /dev/null +++ b/backend/Server/Server/Debug/Server.tlog/CL.read.1.tlog diff --git a/backend/Server/Server/Debug/Server.tlog/CL.write.1.tlog b/backend/Server/Server/Debug/Server.tlog/CL.write.1.tlog Binary files differnew file mode 100644 index 0000000..1a45dfc --- /dev/null +++ b/backend/Server/Server/Debug/Server.tlog/CL.write.1.tlog diff --git a/backend/Server/Server/Debug/Server.tlog/Server.lastbuildstate b/backend/Server/Server/Debug/Server.tlog/Server.lastbuildstate new file mode 100644 index 0000000..adc7782 --- /dev/null +++ b/backend/Server/Server/Debug/Server.tlog/Server.lastbuildstate @@ -0,0 +1,2 @@ +PlatformToolSet=v142:VCToolArchitecture=Native32Bit:VCToolsVersion=14.28.29333:TargetPlatformVersion=10.0.18362.0: +Debug|Win32|C:\Users\mattykae\Projects\website\backend\Server\| diff --git a/backend/Server/Server/Debug/Server.tlog/Server.write.1u.tlog b/backend/Server/Server/Debug/Server.tlog/Server.write.1u.tlog Binary files differnew file mode 100644 index 0000000..d14d9c7 --- /dev/null +++ b/backend/Server/Server/Debug/Server.tlog/Server.write.1u.tlog diff --git a/backend/Server/Server/Debug/Server.tlog/link.command.1.tlog b/backend/Server/Server/Debug/Server.tlog/link.command.1.tlog Binary files differnew file mode 100644 index 0000000..9320e07 --- /dev/null +++ b/backend/Server/Server/Debug/Server.tlog/link.command.1.tlog diff --git a/backend/Server/Server/Debug/Server.tlog/link.read.1.tlog b/backend/Server/Server/Debug/Server.tlog/link.read.1.tlog Binary files differnew file mode 100644 index 0000000..5347f94 --- /dev/null +++ b/backend/Server/Server/Debug/Server.tlog/link.read.1.tlog diff --git a/backend/Server/Server/Debug/Server.tlog/link.write.1.tlog b/backend/Server/Server/Debug/Server.tlog/link.write.1.tlog Binary files differnew file mode 100644 index 0000000..8108896 --- /dev/null +++ b/backend/Server/Server/Debug/Server.tlog/link.write.1.tlog diff --git a/backend/Server/Server/Debug/server.obj b/backend/Server/Server/Debug/server.obj Binary files differnew file mode 100644 index 0000000..5b7c6c9 --- /dev/null +++ b/backend/Server/Server/Debug/server.obj diff --git a/backend/Server/Server/Debug/vc142.idb b/backend/Server/Server/Debug/vc142.idb Binary files differnew file mode 100644 index 0000000..7f41488 --- /dev/null +++ b/backend/Server/Server/Debug/vc142.idb diff --git a/backend/Server/Server/Debug/vc142.pdb b/backend/Server/Server/Debug/vc142.pdb Binary files differnew file mode 100644 index 0000000..21c2483 --- /dev/null +++ b/backend/Server/Server/Debug/vc142.pdb diff --git a/backend/Server/Server/Debug/vcpkg.applocal.log b/backend/Server/Server/Debug/vcpkg.applocal.log new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/backend/Server/Server/Debug/vcpkg.applocal.log @@ -0,0 +1 @@ + diff --git a/backend/Server/Server/Server.vcxproj b/backend/Server/Server/Server.vcxproj new file mode 100644 index 0000000..c673f44 --- /dev/null +++ b/backend/Server/Server/Server.vcxproj @@ -0,0 +1,147 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup Label="ProjectConfigurations"> + <ProjectConfiguration Include="Debug|Win32"> + <Configuration>Debug</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|Win32"> + <Configuration>Release</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Debug|x64"> + <Configuration>Debug</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|x64"> + <Configuration>Release</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + </ItemGroup> + <PropertyGroup Label="Globals"> + <VCProjectVersion>16.0</VCProjectVersion> + <Keyword>Win32Proj</Keyword> + <ProjectGuid>{9cf802af-02fe-48aa-9198-1dc6b51817ff}</ProjectGuid> + <RootNamespace>Server</RootNamespace> + <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>true</UseDebugLibraries> + <PlatformToolset>v142</PlatformToolset> + <CharacterSet>Unicode</CharacterSet> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>false</UseDebugLibraries> + <PlatformToolset>v142</PlatformToolset> + <WholeProgramOptimization>true</WholeProgramOptimization> + <CharacterSet>Unicode</CharacterSet> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>true</UseDebugLibraries> + <PlatformToolset>v142</PlatformToolset> + <CharacterSet>Unicode</CharacterSet> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>false</UseDebugLibraries> + <PlatformToolset>v142</PlatformToolset> + <WholeProgramOptimization>true</WholeProgramOptimization> + <CharacterSet>Unicode</CharacterSet> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> + <ImportGroup Label="ExtensionSettings"> + </ImportGroup> + <ImportGroup Label="Shared"> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <PropertyGroup Label="UserMacros" /> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <LinkIncremental>true</LinkIncremental> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <LinkIncremental>false</LinkIncremental> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <LinkIncremental>true</LinkIncremental> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <LinkIncremental>false</LinkIncremental> + </PropertyGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <ClCompile> + <WarningLevel>Level3</WarningLevel> + <SDLCheck>true</SDLCheck> + <PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <ConformanceMode>true</ConformanceMode> + </ClCompile> + <Link> + <SubSystem>Console</SubSystem> + <GenerateDebugInformation>true</GenerateDebugInformation> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <ClCompile> + <WarningLevel>Level3</WarningLevel> + <FunctionLevelLinking>true</FunctionLevelLinking> + <IntrinsicFunctions>true</IntrinsicFunctions> + <SDLCheck>true</SDLCheck> + <PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <ConformanceMode>true</ConformanceMode> + </ClCompile> + <Link> + <SubSystem>Console</SubSystem> + <EnableCOMDATFolding>true</EnableCOMDATFolding> + <OptimizeReferences>true</OptimizeReferences> + <GenerateDebugInformation>true</GenerateDebugInformation> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <ClCompile> + <WarningLevel>Level3</WarningLevel> + <SDLCheck>true</SDLCheck> + <PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <ConformanceMode>true</ConformanceMode> + </ClCompile> + <Link> + <SubSystem>Console</SubSystem> + <GenerateDebugInformation>true</GenerateDebugInformation> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <ClCompile> + <WarningLevel>Level3</WarningLevel> + <FunctionLevelLinking>true</FunctionLevelLinking> + <IntrinsicFunctions>true</IntrinsicFunctions> + <SDLCheck>true</SDLCheck> + <PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <ConformanceMode>true</ConformanceMode> + </ClCompile> + <Link> + <SubSystem>Console</SubSystem> + <EnableCOMDATFolding>true</EnableCOMDATFolding> + <OptimizeReferences>true</OptimizeReferences> + <GenerateDebugInformation>true</GenerateDebugInformation> + </Link> + </ItemDefinitionGroup> + <ItemGroup> + <ClCompile Include="..\..\server.cpp" /> + </ItemGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> + <ImportGroup Label="ExtensionTargets"> + </ImportGroup> +</Project>
\ No newline at end of file diff --git a/backend/Server/Server/Server.vcxproj.filters b/backend/Server/Server/Server.vcxproj.filters new file mode 100644 index 0000000..77af75f --- /dev/null +++ b/backend/Server/Server/Server.vcxproj.filters @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup> + <Filter Include="Source Files"> + <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> + <Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions> + </Filter> + <Filter Include="Header Files"> + <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> + <Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions> + </Filter> + <Filter Include="Resource Files"> + <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> + <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> + </Filter> + </ItemGroup> + <ItemGroup> + <ClCompile Include="..\..\server.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + </ItemGroup> +</Project>
\ No newline at end of file diff --git a/backend/Server/Server/Server.vcxproj.user b/backend/Server/Server/Server.vcxproj.user new file mode 100644 index 0000000..c72ed2e --- /dev/null +++ b/backend/Server/Server/Server.vcxproj.user @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <LocalDebuggerWorkingDirectory>$(SolutionDir)</LocalDebuggerWorkingDirectory> + <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> + </PropertyGroup> +</Project>
\ No newline at end of file diff --git a/backend/server.cpp b/backend/server.cpp new file mode 100644 index 0000000..eb371d3 --- /dev/null +++ b/backend/server.cpp @@ -0,0 +1,360 @@ +#include <stdio.h> +#include <wS2tcpip.h> +#include <string.h> +#include <assert.h> +#include <time.h> + +#pragma comment (lib, "ws2_32.lib") + +#define WIN32_LEAN_AND_MEAN +#define PORT 80 + +enum HttpRequestType { + HttpRequestType_None, + HttpRequestType_Get +}; + +enum HttpRequestError { + HttpRequestError_None, + HttpRequestError_UnsupportedRequest, + HttpRequestError_NoBuffer, + HttpRequestError_ClientDisconnect +}; + +struct HeaderParseResult { + HttpRequestType requestType; + char resource[512]; + HttpRequestError error = HttpRequestError_None; +}; + +void copyUntilStr(char* destination, char* source, const char* cmpstr) { + int index = 0; + char* ptr = source; + int cmpStrLen = strlen(cmpstr); + while (strncmp(ptr, cmpstr, cmpStrLen) != 0) { + destination[index++] = *ptr; + ptr++; + } + + destination[index] = '\0'; +} + +int endsWith(const char *str, const char *suffix) { + size_t str_len = strlen(str); + size_t suffix_len = strlen(suffix); + + return (str_len >= suffix_len) && + (!memcmp(str + str_len - suffix_len, suffix, suffix_len)); +} + +int fixNewLines(char* str, int strSize) { + int removed = 0; + for(int i = 0; i < strSize; i++) { + if(str[i] == '\n') { + str[i] = '\r\n'; + removed++; + } + } + + return removed; +} + +struct HeaderParser { + char buffer[4096]; + int bufferPtr = 0; + int bufferSize = 0; + char line[512]; + int lineIndex = 0 ; + + bool tryReadLine() { + int startIndex = bufferPtr; + int endIndex = startIndex; + while (true) { + if (bufferPtr >= bufferSize) { + endIndex = bufferPtr; + break; + } + + if (strncmp(&buffer[bufferPtr], "\r\n", 2) == 0) { + endIndex = bufferPtr; + bufferPtr += 2; // Move past the new line character + break; + } + + bufferPtr++; + } + + int lineLength = endIndex - startIndex; + if (lineLength == 0) { + return false; + } + + assert(lineLength < 512 && lineLength > 0); + strncpy_s(line, &buffer[startIndex], lineLength); + line[lineLength] = '\0'; + return true; + } + + HeaderParseResult readHeader(SOCKET clientSocket) { + HeaderParseResult retval; + ZeroMemory(buffer, 4096); + + // Wait for client to send data + bufferSize = recv(clientSocket, buffer, 4096, 0); + if (bufferSize == SOCKET_ERROR) { + printf("Error in receive. Quitting\n"); + retval.error = HttpRequestError_NoBuffer; + return retval; + } else if (bufferSize == 0) { + printf("Client disconnected\n"); + retval.error = HttpRequestError_ClientDisconnect; + return retval; + } + + printf("Received message (%d bytes): %s", bufferSize, buffer); + + while (tryReadLine()) { + if (lineIndex == 0) { + // Parse request, only supporting GETs for now + int linePtr = 0; + if (strncmp(line, "GET ", 3) == 0) { + retval.requestType = HttpRequestType_Get; + linePtr += 4; // Move past teh get + } else { + retval.error = HttpRequestError_UnsupportedRequest; + return retval; + } + + copyUntilStr(retval.resource, &line[linePtr], " "); + printf("Get request on resource: %s\n", retval.resource); + } + + lineIndex++; + } + + return retval; + } +}; + +void getCurrentDateStr(char* text) { + time_t now = time(NULL); + sprintf(text, "%s GMT", ctime(&now)); +} + +enum FileType { + FileType_None, + FileType_HTML, + FileType_CSS, + FileType_JS +}; + +enum HttpStatusCode { + HttpStatusCode_OK = 200, + HttpStatusCode_BADREQUEST = 400, + HttpStatusCode_NOTFOUND = 404 +}; + +void sendErrorMessage(SOCKET socket, HttpStatusCode status, const char* errorHtml) { + char timeStr[100]; + char header[512]; + + ZeroMemory(timeStr, 100); + ZeroMemory(header, 100); + + const char* errorMessage; + switch (status) { + case HttpStatusCode_BADREQUEST: + errorMessage = "Bad Request"; + break; + case HttpStatusCode_NOTFOUND: + errorMessage = "Not found"; + break; + default: + errorMessage = "Unknown"; + break; + } + + getCurrentDateStr(timeStr); + int contentLen = strlen(errorHtml); + sprintf(header, "HTTP/1.1 %d %s\r\nDate: %s\r\nContent-Type: text/html; charset=utf-8\r\nConnection: keep-alive\r\nContent-Length: %d\r\n\r\n", status, errorMessage, timeStr, contentLen); + send(socket, header, strlen(header), 0); + + send(socket, errorHtml, contentLen, 0); +} + +#define MAXBUFLEN 1000000 + +int readFileToMemory(char* filepath, char source[MAXBUFLEN + 1]) { + FILE* file; + fopen_s(&file, filepath, "r+"); + + if(file == NULL) { + printf("Failed to read the file\n"); + return -1; + + } + size_t newLen = fread(source, sizeof(char), MAXBUFLEN, file); + if ( ferror( file ) != 0 ) { + fputs("Error reading file", stderr); + return -1; + } + + fclose(file); + return newLen; +} + + +bool trySendFile(SOCKET clientSocket, char* filename) { + FILE* file; + char filePath[128]; + sprintf(filePath, "../../frontend%s", filename); + + char source[MAXBUFLEN + 1]; + int fileSizeBytes = readFileToMemory(filePath, source); + + if (fileSizeBytes <= 0) { + return false; + } + + + const char* contentType; + if (endsWith(filename, ".html")) { + contentType = "text/html"; + } else if (endsWith(filename, ".js")) { + contentType = "application/javascript"; + } else if (endsWith(filename, ".css")) { + contentType = "text/css"; + } else if (endsWith(filename, ".ico")) { + contentType = "image/x-icon"; + } else { + contentType = "text/plain"; + } + + char timeStr[100]; + char header[512]; + + ZeroMemory(timeStr, 100); + ZeroMemory(header, 100); + + getCurrentDateStr(timeStr); + sprintf(header, "HTTP/1.1 200 OK\r\nDate: %s\r\nContent-Type: %s; charset=utf-8\r\nConnection: keep-alive\r\nContent-Length: %d\r\n\r\n", timeStr, contentType, fileSizeBytes); + send(clientSocket, header, strlen(header), 0); + + + int bytesSent = 0; + int bytesRemaining = fileSizeBytes; + while (bytesSent < fileSizeBytes) { + int amountToSend = bytesRemaining >= 4096 ? 4096 : bytesRemaining; // Send 4K bytes at a time max + send(clientSocket, &source[bytesSent], amountToSend, 0); + bytesRemaining -= amountToSend; + bytesSent += amountToSend; + } + + return true; +} + + +int main() { + // Initializing Winsock + WSADATA wsaData; + WORD version = MAKEWORD(2, 2); + + int wsOK = WSAStartup(version, &wsaData); + if (wsOK != 0) { + printf("Cannot initialize winsock: %d\n", wsOK); + return EXIT_FAILURE; + } + + // Creating a socket + SOCKET listeningSocket = socket(AF_INET, SOCK_STREAM, 0); + if (listeningSocket == INVALID_SOCKET) { + printf("Cannot create socket\n"); + return EXIT_FAILURE; + } + + // Bind an IP address and port to a socket + sockaddr_in hint = { 0 }; + hint.sin_family = AF_INET; + hint.sin_port = htons(PORT); // Networking is big endian, while PCs are little endian + hint.sin_addr.S_un.S_addr = htonl(INADDR_ANY); + + bind(listeningSocket, (sockaddr*)&hint, sizeof(hint)); + + // Tell Winsock the socket is for listening + listen(listeningSocket, SOMAXCONN); + + while (true) { + + // Wait for a connection + sockaddr_in client; + int clientSize = sizeof(client); + + SOCKET clientSocket = accept(listeningSocket, (sockaddr*)&client, &clientSize); + if (clientSocket == INVALID_SOCKET) { + printf("Cannot create the client socket\n"); + return EXIT_FAILURE; + } + + char host[NI_MAXHOST]; // Client's remote name + char service[NI_MAXSERV]; // Service (i.e. port) the client is connected on + + ZeroMemory(host, NI_MAXHOST); + ZeroMemory(service, NI_MAXSERV); + + if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0) { + printf("Host %s connected on port %s\n", host, service); + } else { + inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST); + printf("Host %s connected on port %d\n", host, ntohs(client.sin_port)); + } + + // Accept and echo message back to client + while (true) { + HeaderParser parser; + HeaderParseResult parseResult = parser.readHeader(clientSocket); + bool shouldBreak = false; + + switch (parseResult.error) { + case HttpRequestError_ClientDisconnect: { + shouldBreak = true; + break; + } + case HttpRequestError_UnsupportedRequest: { + shouldBreak = true; + break; + } + } + + if (shouldBreak) { + break; + } + + switch (parseResult.requestType) { + case HttpRequestType_Get: { + if (strcmp(parseResult.resource, "/") == 0) { + trySendFile(clientSocket, (char*)"/index.html"); + } else { + trySendFile(clientSocket, parseResult.resource); + } + shouldBreak = true; + break; + } + } + + if (shouldBreak) { + break; + } + } + + // Close the socket + closesocket(clientSocket); + } + + // Close listening socket + closesocket(listeningSocket); + + // Cleanup winsock + WSACleanup(); + + return EXIT_SUCCESS; +}
\ No newline at end of file diff --git a/backend/server.obj b/backend/server.obj Binary files differnew file mode 100644 index 0000000..59b2db2 --- /dev/null +++ b/backend/server.obj |