From 5e4275ac5bc0f21a7926ab5332edf80da435fef5 Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Mon, 3 Sep 2018 18:54:02 -0400 Subject: [PATCH] Add basic server argparsing --- client/http_client.c | 1 + client/http_socket.h | 2 -- common/socket_helper.h | 2 ++ server/http_server.c | 16 ++++++++++++++++ server/http_server.h | 7 +++++++ 5 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 server/http_server.c create mode 100644 server/http_server.h diff --git a/client/http_client.c b/client/http_client.c index ae9cde6..eb6949a 100644 --- a/client/http_client.c +++ b/client/http_client.c @@ -1,5 +1,6 @@ #include "http_client.h" #include "../common/http_types.h" +#include "../common/socket_helper.h" #include "http_socket.h" #include #include diff --git a/client/http_socket.h b/client/http_socket.h index 634a4b5..bc58468 100644 --- a/client/http_socket.h +++ b/client/http_socket.h @@ -6,8 +6,6 @@ #include #define HTTP_VERSION "HTTP/1.1" -#define MIN_PORT 1 -#define MAX_PORT 65535 struct response_info { char *contents; diff --git a/common/socket_helper.h b/common/socket_helper.h index 1975f28..6e539f8 100644 --- a/common/socket_helper.h +++ b/common/socket_helper.h @@ -3,6 +3,8 @@ #include "http_types.h" +#define MIN_PORT 1 +#define MAX_PORT 65535 #define BUFFER_SIZE 1024 #define HEADER_CONTENT_LENGTH "Content-Length" #define HEADER_TRANSFER_ENCODING "Transfer-Encoding" diff --git a/server/http_server.c b/server/http_server.c new file mode 100644 index 0000000..48469b4 --- /dev/null +++ b/server/http_server.c @@ -0,0 +1,16 @@ +#include "http_server.h" +#include "../common/socket_helper.h" +#include +#include +#include + +int main(int argc, char* argv[]) { + if (argc != 2) { + printf("%s", USAGE_STRING); + } + char *port = argv[1]; + long port_num = strtol(port, NULL, 10); + if (port_num < MIN_PORT || port_num > MAX_PORT || errno == ERANGE || errno == EINVAL) { + printf("%s", PORT_ERROR); + } +} diff --git a/server/http_server.h b/server/http_server.h new file mode 100644 index 0000000..7262743 --- /dev/null +++ b/server/http_server.h @@ -0,0 +1,7 @@ +#ifndef HTTP_SERVER_H +#define HTTP_SERVER_H + +#define USAGE_STRING "Usage: ./http_server port" +#define PORT_ERROR "Port must be a number in the range 1-65535" + +#endif