commit 47195b3f4f5820c46b1de1742e6b6e9cb65294fe
Author: benjamin paul <bpaul@bpaul.xyz>
Date: Sun, 1 Aug 2021 13:48:35 +1000
Initial commit
Diffstat:
A | .clang-format | | | 98 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | .gitignore | | | 3 | +++ |
A | Makefile | | | 2 | ++ |
A | http.c | | | 21 | +++++++++++++++++++++ |
A | http.h | | | 3 | +++ |
A | index.html | | | 61 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | main.c | | | 32 | ++++++++++++++++++++++++++++++++ |
A | tcp.c | | | 86 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | tcp.h | | | 6 | ++++++ |
9 files changed, 312 insertions(+), 0 deletions(-)
diff --git a/.clang-format b/.clang-format
@@ -0,0 +1,98 @@
+---
+Language: Cpp
+AccessModifierOffset: -2
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: Consecutive
+AlignConsecutiveBitFields: Consecutive
+AlignConsecutiveDeclarations: None
+AlignConsecutiveMacros: Consecutive
+AlignEscapedNewlines: Left
+AlignOperands: Align
+AlignTrailingComments: true
+AllowAllArgumentsOnNextLine: true
+AllowAllParametersOfDeclarationOnNextLine: true
+AllowShortBlocksOnASingleLine: Always
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortEnumsOnASingleLine: true
+AllowShortFunctionsOnASingleLine: All
+AllowShortLoopsOnASingleLine: true
+AlwaysBreakAfterReturnType: AllDefinitions
+AlwaysBreakBeforeMultilineStrings: false
+BinPackArguments: true
+BinPackParameters: true
+BitFieldColonSpacing: Both
+BreakBeforeBinaryOperators: All
+BreakBeforeBraces: Attach
+BreakBeforeTernaryOperators: true
+BreakStringLiterals: true
+ColumnLimit: 79
+ContinuationIndentWidth: 4
+DeriveLineEnding: true
+DerivePointerAlignment: false
+DisableFormat: false
+ExperimentalAutoDetectBinPacking: false
+IncludeBlocks: Regroup
+IncludeCategories:
+ - Regex: '.*'
+ Priority: 1
+ SortPriority: 0
+ CaseSensitive: false
+# idk what this is but
+IncludeIsMainRegex: '(Test)?$'
+IncludeIsMainSourceRegex: ''
+IndentCaseBlocks: false
+IndentCaseLabels: false
+IndentGotoLabels: false
+IndentPPDirectives: None
+IndentWidth: 4
+IndentWrappedFunctionNames: false
+InsertTrailingCommas: None
+KeepEmptyLinesAtTheStartOfBlocks: false
+MacroBlockBegin: ''
+MacroBlockEnd: ''
+MaxEmptyLinesToKeep: 1
+# i will figure this stuff out as i go
+PenaltyBreakAssignment: 0
+PenaltyBreakBeforeFirstCallParameter: 0
+PenaltyBreakComment: 0
+PenaltyBreakFirstLessLess: 0
+PenaltyBreakString: 0
+PenaltyBreakTemplateDeclaration: 0
+PenaltyExcessCharacter: 0
+PenaltyReturnTypeOnItsOwnLine: 0
+PenaltyIndentedWhitespace: 0
+# gnu's settings
+#PenaltyBreakAssignment: 2
+#PenaltyBreakBeforeFirstCallParameter: 19
+#PenaltyBreakComment: 300
+#PenaltyBreakFirstLessLess: 120
+#PenaltyBreakString: 1000
+#PenaltyBreakTemplateDeclaration: 10
+#PenaltyExcessCharacter: 1000000
+#PenaltyReturnTypeOnItsOwnLine: 60
+#PenaltyIndentedWhitespace: 0
+
+PointerAlignment: Right
+ReflowComments: true
+SortIncludes: true
+SpaceAfterCStyleCast: false
+SpaceAfterLogicalNot: false
+SpaceAroundPointerQualifiers: Default
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCaseColon: false
+SpaceBeforeParens: ControlStatements
+SpaceBeforeSquareBrackets: false
+SpaceInEmptyBlock: false
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInCStyleCastParentheses: false
+SpacesInConditionalStatement: false
+SpacesInContainerLiterals: true
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+
+TabWidth: 4
+UseCRLF: false
+UseTab: AlignWithSpaces
+...
+
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1,3 @@
+.ccls-cache
+*.o
+prog
diff --git a/Makefile b/Makefile
@@ -0,0 +1,2 @@
+all:
+ ${CC} *.c -o prog
diff --git a/http.c b/http.c
@@ -0,0 +1,21 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+char *
+respond(char *payload) {
+ size_t length;
+ char *response, format[] = "HTTP/1.1 200 OK\n"
+ "Content-Type: text/html\n"
+ "Content-Length: %d\n"
+ "Server: bpaul's funny http server\n"
+ "\n"
+ "%s";
+
+
+ response = malloc(4096);
+
+ sprintf(response, format, strlen(payload), payload);
+
+ return response;
+}
diff --git a/http.h b/http.h
@@ -0,0 +1,3 @@
+#pragma once
+
+char *respond(char *request, char *payload);
diff --git a/index.html b/index.html
@@ -0,0 +1,61 @@
+<html>
+<head>
+ <meta charset="UTF-8">
+<style>
+a {
+ color: inherit;
+ outline: 0;
+ text-decoration: inherit;
+}
+
+body {
+ font-family: monospace;
+ color: white;
+ background-color: black;
+}
+
+pre {
+ font-family: monospace;
+}
+</style>
+</head>
+<body>
+ This page will look wrong if you aren't using a monospace font.<br>
+ The old home page is <a href="/old.html">>>>here<<<</a><br>
+ You can click on doors to go in to the buildings!!
+ <pre>
+,_______________________,
+| |
+| |
+| ____ ___ _____ |
+| / ___|_ _|_ _| |
+| | | _ | | | | |
+| | |_| || | | | |
+| \____|___| |_| |
+| |
+| ____ _ _ |
+| |/ \\ /|_|_) |
+| |\_/ \/\/ |_| \ |
+| |
+| |
+| |
+| |
+| |
+| |
+| |
+| | ,__________,
+| | | |
+| | | Lemonade | ,_____________,
+| <a href="http://git.bpaul.xyz">,_______,</a> | |__________| <a href="/houses.html">| |</a>
+| <a href="http://git.bpaul.xyz">| | |</a> | | o | <a href="/houses.html">| houses > |</a>
+| <a href="http://git.bpaul.xyz">| | |</a> | |__v__|____| <a href="/houses.html">|_____________|</a>
+|_______<a href="http://git.bpaul.xyz">|___|___|</a>_______| |__________| <a href="/houses.html"> | </a>
+_________________________________________________________<a href="/houses.html">__________________</a>
+_________________________________________________________<a href="/houses.html">__________________</a>
+ <a href="/houses.html"> </a>
+ - - - - - - - - - - - - - - - - - - - <a href="/houses.html"> - - - - - - </a>
+_________________________________________________________<a href="/houses.html">__________________</a>
+_________________________________________________________<a href="/houses.html">__________________</a>
+ </pre>
+</body>
+</html>
diff --git a/main.c b/main.c
@@ -0,0 +1,32 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "html.h"
+#include "tcp.h"
+
+int
+main() {
+ int sock, asock;
+ sock = create_socket();
+
+ char *buf;
+ size_t length;
+ FILE *f = fopen("index.html", "r");
+ fseek(f, 0, SEEK_END);
+ length = ftell(f);
+ fseek(f, 0, SEEK_SET);
+ buf = malloc(length);
+ fread(buf, 1, length, f);
+
+ for (;;) {
+ asock = accept_socket(sock);
+
+ char *request = read_socket(asock);
+ printf("%s", request);
+
+ char *text = respond(request, buf);
+ write(asock, text, strlen(text));
+ }
+}
diff --git a/tcp.c b/tcp.c
@@ -0,0 +1,86 @@
+#include <arpa/inet.h>
+#include <errno.h>
+#include <netinet/in.h>
+#include <netinet/ip.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#define PORT 8000
+#define BACKLOG 50
+
+int
+create_socket() {
+ int sock;
+ struct sockaddr_in addr, public_addr;
+
+ /* Create a socket that is tcp or something */
+ if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
+ perror("socket");
+ exit(1);
+ }
+
+ setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &(bool){true}, sizeof(bool));
+
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(PORT);
+ if (!inet_aton("0.0.0.0", &addr.sin_addr)) {
+ perror("inet_aton addr");
+ exit(1);
+ }
+
+ /* Bind the socket to an address */
+ if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
+ perror("bind");
+ exit(1);
+ }
+
+ /*
+ public_addr.sin_family = AF_INET;
+ public_addr.sin_port = htons(PORT);
+ if (!inet_aton("192.168.50.22", &public_addr.sin_addr)) {
+ perror("inet_aton public_addr");
+ exit(1);
+ }
+
+ if (connect(sock, (struct sockaddr *)&public_addr, sizeof(public_addr)) == -1) {
+ perror("connect");
+ exit(1);
+ }
+ */
+
+ /* Listen for connections on the socket */
+ if (listen(sock, BACKLOG) == -1) {
+ perror("listen");
+ exit(1);
+ }
+
+ return sock;
+}
+
+int
+accept_socket(int sock) {
+ int asock;
+ struct sockaddr_in peer_addr;
+ socklen_t peer_addr_size;
+
+ peer_addr_size = sizeof(peer_addr);
+ asock = accept(sock, (struct sockaddr *)&peer_addr, &peer_addr_size);
+
+ return asock;
+}
+
+char *
+read_socket(int sock) {
+ char *buf = malloc(1024);
+
+ memset(buf, 0, 1024);
+
+ recv(sock, buf, 1024, 0);
+
+ return buf;
+}
diff --git a/tcp.h b/tcp.h
@@ -0,0 +1,6 @@
+#pragma once
+
+
+int create_socket();
+int accept_socket(int sock);
+char *read_socket(int sock);