Change port to be an int
parent
69258376a9
commit
65fdb4ad69
|
@ -3,6 +3,7 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
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);
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue