diff --git a/server b/server new file mode 100755 index 0000000..9c9a369 Binary files /dev/null and b/server differ diff --git a/src/server.c b/src/server.c index 3b651a3..85713d6 100644 --- a/src/server.c +++ b/src/server.c @@ -9,10 +9,10 @@ #include #include "server.h" -#include "file.c" -#define PORT 8080 -#define BUFFER_SIZE 1024 +/* + +TODO: CREATE SENDING RESPONSE FUNCTION int send_response(int fd, char *header, char *content_type, void *body, int content_length) { const int max_response_size = 65536; @@ -35,12 +35,14 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont memcpy(response + response_length, body, content_length); } +*/ + int main() { char buffer[BUFFER_SIZE]; - char resp[] = "HTTP/1.0 200 OK\r\n" - "Server: webserver-c\r\n" - "Content-type: text/html\r\n\r\n" - "hello, world\r\n"; +// char resp[] = "HTTP/1.0 200 OK\r\n" +// "Server: webserver-c\r\n" +// "Content-type: text/html\r\n\r\n" +// "hello, world\r\n"; // create a socket int sockfd = socket(AF_INET, SOCK_STREAM, 0); @@ -106,30 +108,49 @@ int main() { // check if request is GET request 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"; - write(newsockfd, resp, strlen(resp)); - } else { - char file_path[BUFFER_SIZE]; - get_file_path(uri, file_path); +// const char resp[] = "HTTP/1.0 400 Bad Request\r\n" +// "Server: webserver-c\r\n" +// "Content-Type: text/html\r\n\r\n"; - FILE *file = fopen(file_path, "r"); + char resp[100]; + int resp_size = snprintf(resp, 100, + "%s\r\n" + "Server: %s\r\n" + "Content-Type: %s\r\n" + "\r\n", + BAD_REQUEST_HEAD, SERVER_VERSION, "text/html"); + + int valwrite = write(newsockfd, resp, resp_size); + if (valwrite < 0) { + perror("webserver (write)"); + continue; + } + } else { + char resp[100]; + int resp_size = snprintf(resp, 100, + "%s\r\n" + "Server: %s\r\n" + "Content-Type: %s\r\n" + "\r\n", + OK_HEAD, SERVER_VERSION, "text/html"); + + int valwrite = write(newsockfd, resp, resp_size); + printf("%s\n", OK_HEAD); + if (valwrite < 0) { + perror("webserver (write)"); + continue; + } } // write to the socket - int valwrite = write(newsockfd, resp, strlen(resp)); - if (valwrite < 0) { - perror("webserver (write)"); - continue; - } + // int valwrite = write(newsockfd, resp, strlen(resp)); + // if (valwrite < 0) { + // perror("webserver (write)"); + // continue; + // } close(newsockfd); } 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 index f0ff549..72ed635 100644 --- a/src/server.h +++ b/src/server.h @@ -1,8 +1,8 @@ #define PORT 8080 #define BUFFER_SIZE 1024 -const char SERVER_VERSION[] = "winnie-http-server/0.0.1"; +static 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"; +static const char BAD_REQUEST_HEAD[] = "HTTP/1.1 400 Bad Request"; +static const char NOT_FOUND_HEAD[] = "HTTP/1.1 404 Not Found"; +static const char OK_HEAD[] = "HTTP/1.1 200 OK";