From 65fdb4ad69e0972515f8cc1f1632deed5d3b55fc Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Mon, 27 Aug 2018 13:16:59 -0400 Subject: [PATCH] Change port to be an int --- client/http_client.c | 5 +++++ client/http_client.h | 3 +++ client/http_socket.c | 2 +- client/http_socket.h | 2 +- common/http_message.h | 3 +-- 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/client/http_client.c b/client/http_client.c index 6cd5950..93cbc81 100644 --- a/client/http_client.c +++ b/client/http_client.c @@ -3,6 +3,7 @@ #include #include #include +#include #include int main(int argc, char* argv[]) { @@ -24,6 +25,10 @@ int main(int argc, char* argv[]) { char *port = argv[argc - 1]; char *path_buffer = malloc(strlen(dest) + 1); char *host_buffer = malloc(strlen(dest) + 1); + long port_num = strtol(port, NULL, 10); + if (port_num < MIN_PORT || port_num > MAX_PORT || errno == ERANGE) { + printf("%s", PORT_ERROR); + } get_parts(dest, path_buffer, host_buffer); printf("%s\n%s\n", path_buffer, host_buffer); free(host_buffer); diff --git a/client/http_client.h b/client/http_client.h index 1977183..8811152 100644 --- a/client/http_client.h +++ b/client/http_client.h @@ -3,6 +3,9 @@ #define RTT_FLAG "-p" #define USAGE_STRING "Usage: ./http_client [-p] server_url port_number\n" +#define PORT_ERROR "Port must be a number in the range 1-65535" +#define MIN_PORT 1 +#define MAX_PORT 65535 void get_parts(const char *dest, char *path_buffer, char *host_buffer); diff --git a/client/http_socket.c b/client/http_socket.c index efc2908..d9a3561 100644 --- a/client/http_socket.c +++ b/client/http_socket.c @@ -30,7 +30,7 @@ static size_t get_buffer_size(const char **components, int num_components) { * * @return A http_message - note that the contents field has been mallocced and must be manually freed. */ -struct http_message build_basic_request(const char *method, const char *host, const char *path, const char *port) { +struct http_message build_basic_request(const char *method, const char *host, const char *path, int port) { const char *request_line_components[] = {method, " ", path, " ", HTTP_VERSION, "\r\n"}; const char *host_header[] = {"Host: ", host, "\r\n"}; int message_buffer_size = get_buffer_size(request_line_components, 6) + get_buffer_size(host_header, 3) + 1; diff --git a/client/http_socket.h b/client/http_socket.h index e90bd77..d870056 100644 --- a/client/http_socket.h +++ b/client/http_socket.h @@ -5,6 +5,6 @@ #define HTTP_VERSION "HTTP/1.1" -struct http_message build_basic_request(const char *method, const char *host, const char *path, const char *port); +struct http_message build_basic_request(const char *method, const char *host, const char *path, int port); #endif diff --git a/common/http_message.h b/common/http_message.h index d403d27..7aef327 100644 --- a/common/http_message.h +++ b/common/http_message.h @@ -2,8 +2,7 @@ #define HTTP_MESSAGE_H struct http_message { const char *address; - // though an int makes sense, all functions use char*s for their ports. This avoids unneeded conversion. - const char *port; + int port; const char *path; const char *contents; };