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 0000000..7246061 Binary files /dev/null and b/goffy differ 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 7a6d89f..0000000 Binary files a/webserver and /dev/null differ