From f2728eaf68b35862ad2db45057d50b09d20964bc Mon Sep 17 00:00:00 2001 From: winsdominoes Date: Thu, 16 May 2024 15:54:17 +0700 Subject: [PATCH] added things --- makefile => Makefile | 0 README.md | 8 +++++++- goffy | Bin 0 -> 16024 bytes goffy.c | 24 +++++++++++++++++++++++ src/file.c | 6 ++++++ webserver.c => src/server.c | 37 ++++++++++++++++++++++++++++++++++-- src/server.h | 8 ++++++++ webserver | Bin 16704 -> 0 bytes 8 files changed, 80 insertions(+), 3 deletions(-) rename makefile => Makefile (100%) create mode 100755 goffy create mode 100644 goffy.c create mode 100644 src/file.c rename webserver.c => src/server.c (72%) create mode 100644 src/server.h delete mode 100755 webserver diff --git a/makefile b/Makefile similarity index 100% rename from makefile rename to Makefile diff --git a/README.md b/README.md index be78611..e354d9f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ # simple-web-server -Simple HTTP server in C \ No newline at end of file +Simple HTTP server in C + +## Resources +- https://bruinsslot.jp/post/simple-http-webserver-in-c/ +- https://medium.com/@nipunweerasiri/a-simple-web-server-written-in-c-cf7445002e6 +- https://github.com/AmyShackles/Web-Server-in-C/ +- https://devdocs.io diff --git a/goffy b/goffy new file mode 100755 index 0000000000000000000000000000000000000000..72460613e2f87142a2d95320562355541bafa38b GIT binary patch literal 16024 zcmeHOZ)_CD6`wo)ISF9n;MPEkmn5!CiWeWoE(vlt|2eZx4R*k&pq4Ca-=2Ny-K~4K zN9-t~P(u_4St3%~^g}+-5{XLv(5On=FC{V!qBN>1=qFdIDyvE)Q{@j$X_U5s>w7cr z?X9=xrpkx@k#^+WH}Ch}Z)Wz*?0WWg=Ie?6-e@EuI4u*83Dl0&C?rmXxpvBch>Klf z4V)XqX0a0VhvUi{&8IcbFveP(|K6@#cx=Nvdz2 zB*znDg2yh3XHaTj!=%Zx5JBEy;ss?7`Lo12Fdcl$O@5O428ox^WHBNrw<%b#I`4g=*CRjx6m|G_X3Dl#KMKUrPB1744TdYbNbA2m0f`B2}$^|;&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h!^zCWiJt`cJkMas?>$TiUjNUC z&;2Jf@+A3HQdtD+zXAqqBJ(qv@?^>T8x^X?3g=P&1`a>}I~t-wu>7V{y)(y+f!{#} z)@aIm2r+)2<9(1pf7V$p0H@l==Xvj3GnQ5!Z%Qlc%)bo=9DDTG^5&!7zE;^*`G;=;elUkPMCpXZ%5EviaxCpUoWWgX|q zh;jS1xUy)(5Bg(W{De{tLW f{CRwFg7kZ?NU1Vbl$8Rfzm@o}wJEQSAH)9#h{9HD literal 0 HcmV?d00001 diff --git a/goffy.c b/goffy.c new file mode 100644 index 0000000..f76ca5b --- /dev/null +++ b/goffy.c @@ -0,0 +1,24 @@ +#include +#include + +int main() { + + const char src[20] = "HA! poopoohead!"; + char dest[100]; + + int eh = sprintf(dest, "%s", "goofy"); + + memcpy(dest + 6, src, strlen(src) + 1); + + printf("Output: %s\n", dest); + + printf("src[0] %p\n", &src[0]); + printf("src[0] %p\n\n", src); + printf("src[1] %p\n", &src[1]); + printf("src[1] %p\n", src + 1); + printf("src[50] %p\n", src + 49); + + printf("%ld", sizeof(dest)); + + return 0; +} diff --git a/src/file.c b/src/file.c new file mode 100644 index 0000000..1dcb12a --- /dev/null +++ b/src/file.c @@ -0,0 +1,6 @@ +#include +#include + +void get_file_path(char *uri, char *file_path) { + char *question = strchr(uri, '?'); +} diff --git a/webserver.c b/src/server.c similarity index 72% rename from webserver.c rename to src/server.c index 7ecc892..3b651a3 100644 --- a/webserver.c +++ b/src/server.c @@ -6,9 +6,35 @@ #include #include +#include + +#include "server.h" +#include "file.c" + #define PORT 8080 #define BUFFER_SIZE 1024 +int send_response(int fd, char *header, char *content_type, void *body, int content_length) { + const int max_response_size = 65536; + char response[max_response_size]; + + time_t rawtime; + struct tm *info; + + info = localtime(&rawtime); + + int response_length = sprintf(response, + "%s\n" + "Date: %s" + "Connection: close\n" + "Content-Length: %d\n" + "Content-Type: %s\n" + "\n", + header, asctime(info), content_length, content_type); + + memcpy(response + response_length, body, content_length); +} + int main() { char buffer[BUFFER_SIZE]; char resp[] = "HTTP/1.0 200 OK\r\n" @@ -82,10 +108,13 @@ int main() { if (strncmp("GET", method, 7) != 0) { const char resp[] = "HTTP/1.0 400 Bad Request\r\n" "Server: webserver-c\r\n" - "Content-type: text/html\r\n\r\n"; + "Content-Type: text/html\r\n\r\n"; write(newsockfd, resp, strlen(resp)); } else { - // TO DO: IMPLEMENT FILE READING + char file_path[BUFFER_SIZE]; + get_file_path(uri, file_path); + + FILE *file = fopen(file_path, "r"); } // write to the socket @@ -100,3 +129,7 @@ int main() { return 0; } + +void get_file_path(char *uri, char *file_path) { + char *question = strchr(route, '?'); +} diff --git a/src/server.h b/src/server.h new file mode 100644 index 0000000..f0ff549 --- /dev/null +++ b/src/server.h @@ -0,0 +1,8 @@ +#define PORT 8080 +#define BUFFER_SIZE 1024 + +const char SERVER_VERSION[] = "winnie-http-server/0.0.1"; + +const char BAD_REQUEST_RESP[] = "HTTP/1.1 400 Bad Request\r\n"; +const char NOT_FOUND_RESP[] = "HTTP/1.1 404 Not Found\r\n"; +const char OK_RESP[] = "HTTP/1.1 200 OK\r\n"; diff --git a/webserver b/webserver deleted file mode 100755 index 7a6d89f74ed47d9a83a9b7607ecb887215204cff..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16704 zcmeHOeQ;FO6~9S{#7Ia$zWv~d#DbD#2>}C@y5s|1EJ%dVYO6jryD!NOo85Hxtt3vz z#>Tbm#B@?STE}S}+CQpOXPk`HN@;pC$EQiPT8Vpf+g00Y1}C!ThlD#s}s6f9VBeRlv4uXVu}yWn^(3zh${3%=6@uW@PrDi{Ai- ze@T4_D=zxXSS%V7h7q^?ft^O6XQ$ES4~0dc6t}<{ikOxG<@iOnX(2Ja(i9k3SH$`786tTL%B?Rtk))?`4z#r)nyJ8{Bgi<>L*gIE?jhotRYe7fslO@nk)xRm( zTp6I{HVt+3QKi2!!x&@9Jfj{$MvCCDqfby zmRU6u;7si-_^n>(6X@TAmh|aNCet-4b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