commit 4badad2ce0423db4eb19bda8aaa222607a7ff89a
parent d00acd0e8faddabd28bb56a15c2dc022ecc667d8
Author: benjamin paul <bpaul@bpaul.xyz>
Date: Sun, 1 Aug 2021 18:09:34 +1000
reads files dynamically
Diffstat:
A | houses.html | | | 71 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | http.c | | | 11 | +++++++++++ |
M | http.h | | | 1 | + |
M | main.c | | | 12 | +++++++++++- |
4 files changed, 94 insertions(+), 1 deletion(-)
diff --git a/houses.html b/houses.html
@@ -0,0 +1,71 @@
+<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>
+ <pre>
+ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\
+ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \
+ /____\ /____\ /____\ /____\ /____\ /____\ /____\ /____\ /____\ /____\
+ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \
+ /______\ /______\ /______\ /______\ /______\ /______\ /______\ /______\ /______\ /______\
+ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \
+ /________\ /________\ /________\ /________\ /________\ /________\ /________\ /________\ /________\ /________\
+ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \
+<a href="/index.html"> ,_____________,</a> /__________\ /__________\ /__________\ /__________\ /__________\ /__________\ /__________\ /__________\ /__________\ /__________\
+<a href="/index.html"> | |</a> \ / \ / \ / \ / \ / \ / \ / \ / \ / \ /
+<a href="/index.html"> | town < |</a> | | | | | | | | | | | | | | | | | | | |
+<a href="/index.html"> |_____________|</a> } { } { } { } { } { } { } { } { } { } {
+<a href="/index.html"> | </a> )' '( )' '( )' '( )' '( )' '( )' '( )' '( )' '( )' '( )' '(
+<a href="/index.html">__________________</a>_____________________________________________________________________________________________________________________________________________
+<a href="/index.html">__________________</a>_____________________________________________________________________________________________________________________________________________
+<a href="/index.html"> </a>
+<a href="/index.html"> - - - - - - </a> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+<a href="/index.html">__________________</a>_______________________________________________________________, ,_____________________________________________________________________
+<a href="/index.html">__________________</a>_______________________________________________________________| | |_____________________________________________________________________
+ | |
+ | | |
+ | |
+ | | |
+ | |
+ | | |
+ | |
+ | | |
+ | |
+ | | |
+ | |
+ | | |
+ | |
+ | | | ,________________________________,
+ | | | |
+ | | | | If you want a house or plot of |
+ | | | land contact me |
+ | | | | |
+ | | |________________________________|
+ | | | |
+_________________________________________________________________________________| |_____________________________________________________________________
+_________________________________________________________________________________| | |_____________________________________________________________________
+
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+_______________________________________________________________________________________________________________________________________________________________
+_______________________________________________________________________________________________________________________________________________________________
+ </pre>
+</body>
+</html>
diff --git a/http.c b/http.c
@@ -4,6 +4,17 @@
#include <string.h>
char *
+requested_file(char *request) {
+ char *file_name;
+
+ /* GET /folder/file.name HTTP/1.1*/
+ strtok(request, " ");
+ file_name = strtok(NULL, " ");
+
+ return file_name;
+}
+
+char *
respond(char *payload) {
size_t length;
char *response, format[] = "HTTP/1.1 200 OK\n"
diff --git a/http.h b/http.h
@@ -1,3 +1,4 @@
#pragma once
+char *requested_file(char *request);
char *respond(char *payload);
diff --git a/main.c b/main.c
@@ -12,7 +12,6 @@ main() {
sock = create_socket();
char *buf;
- buf = read_file("index.html");
for (;;) {
asock = accept_socket(sock);
@@ -20,10 +19,21 @@ main() {
char *request = read_socket(asock);
printf("%s", request);
+ char *file_name = requested_file(request);
+
+ /* If file is not found */
+ if (strcmp(file_name, "/") == 0) {
+ buf = read_file("index.html");
+ } else {
+ file_name++;
+ buf = read_file(file_name);
+ }
+
char *text = respond(buf);
write(asock, text, strlen(text));
free(request);
+ free(buf);
free(text);
}
}