From 262a1bd2129ce248163a1ac2803c843c6f8cba0d Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Sat, 8 Sep 2018 17:26:34 -0400 Subject: [PATCH] Add very basic server --- server/http_server.c | 7 +++++++ server/socket_server.c | 21 +++++++++++++++++++-- server/socket_server.h | 4 +++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/server/http_server.c b/server/http_server.c index 48469b4..72cff44 100644 --- a/server/http_server.c +++ b/server/http_server.c @@ -1,8 +1,11 @@ #include "http_server.h" #include "../common/socket_helper.h" +#include "socket_server.h" #include #include #include +#include +#include int main(int argc, char* argv[]) { if (argc != 2) { @@ -13,4 +16,8 @@ int main(int argc, char* argv[]) { if (port_num < MIN_PORT || port_num > MAX_PORT || errno == ERANGE || errno == EINVAL) { printf("%s", PORT_ERROR); } + struct server_info info = setup(port_num); + serve_one_request(info.sock_fd); + shutdown(info.sock_fd, SHUT_RDWR); + close(info.sock_fd); } diff --git a/server/socket_server.c b/server/socket_server.c index 8757ca6..ffdad4b 100644 --- a/server/socket_server.c +++ b/server/socket_server.c @@ -1,8 +1,11 @@ #include "socket_server.h" +#include "../common/http_types.h" +#include "../common/socket_helper.h" #include #include #include #include +#include #include #include @@ -57,8 +60,7 @@ struct server_info setup(int port) { return info; } - int listen_result = listen(info.sock_fd, 8); - if (listen_result == -1) { + int listen_result = listen(info.sock_fd, 8); if (listen_result == -1) { close(info.sock_fd); info.sock_fd = -1; info.status = STATUS_ERROR; @@ -68,3 +70,18 @@ struct server_info setup(int port) { return info; } + +enum socket_read_result serve_one_request(int sock_fd) { + int client_fd = accept(sock_fd, NULL, NULL); + if (client_fd == -1) { + return RESULT_WRITE_ERROR; + } + struct http_message message = {}; + get_all_remote_parts(client_fd, TYPE_SERVER, &message); + printf("%s\n", message.contents); + char *msg = "HTTP/1.1 501 Not Imlemented Yet :(\r\n\r\n"; + send(client_fd, msg, strlen(msg), 0); + shutdown(client_fd, SHUT_RDWR); + close(client_fd); + return RESULT_OK; +} diff --git a/server/socket_server.h b/server/socket_server.h index adbcbf9..4504c4a 100644 --- a/server/socket_server.h +++ b/server/socket_server.h @@ -1,6 +1,8 @@ #ifndef SOCKET_SERVER #define SOCKET_SERVER +#include "../common/socket_helper.h" + #define BACKLOG_SIZE 8; enum server_status {STATUS_LISTENING, STATUS_CLOSED, STATUS_ERROR}; @@ -10,6 +12,6 @@ struct server_info { }; struct server_info setup(int port); -char *serve(struct server_info *info); +enum socket_read_result serve_one_request(int sock_fd); #endif