From 9a16bd9dec307c569a7169f8d68f5dc8451a721d Mon Sep 17 00:00:00 2001 From: AM_Myrick Date: Fri, 15 Mar 2019 11:06:23 -0400 Subject: [PATCH 1/6] attendance commit --- src/client.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client.c b/src/client.c index 5246a49..aaa8bf3 100644 --- a/src/client.c +++ b/src/client.c @@ -11,7 +11,7 @@ /** * Struct to hold all three pieces of a URL - */ + */ typedef struct urlinfo_t { char *hostname; char *port; From 6fdbeb761eb10ff6d9294f2cef38051256d46ffb Mon Sep 17 00:00:00 2001 From: AM_Myrick Date: Fri, 15 Mar 2019 11:37:49 -0400 Subject: [PATCH 2/6] implements parse_url --- src/client.c | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/client.c b/src/client.c index aaa8bf3..3148e7d 100644 --- a/src/client.c +++ b/src/client.c @@ -32,22 +32,16 @@ urlinfo_t *parse_url(char *url) char *port; char *path; + char *first_backslash = strchr(hostname, '/'); + path = first_backslash + 1; + *first_backslash = '\0'; + char *first_colon = strchr(hostname, ':'); + port = first_colon + 1; + *first_colon = '\0'; + urlinfo_t *urlinfo = malloc(sizeof(urlinfo_t)); - - /* - We can parse the input URL by doing the following: - - 1. Use strchr to find the first backslash in the URL (this is assuming there is no http:// or https:// in the URL). - 2. Set the path pointer to 1 character after the spot returned by strchr. - 3. Overwrite the backslash with a '\0' so that we are no longer considering anything after the backslash. - 4. Use strchr to find the first colon in the URL. - 5. Set the port pointer to 1 character after the spot returned by strchr. - 6. Overwrite the colon with a '\0' so that we are just left with the hostname. - */ - - /////////////////// - // IMPLEMENT ME! // - /////////////////// + urlinfo->port = port; + urlinfo->path = path; return urlinfo; } @@ -85,6 +79,7 @@ int main(int argc, char *argv[]) exit(1); } + parse_url(argv[1]); /* 1. Parse the input URL 2. Initialize a socket by calling the `get_socket` function from lib.c From ecc19e8966bf603a2bdb09d6db1cef0bc9d77dbc Mon Sep 17 00:00:00 2001 From: AM_Myrick Date: Fri, 15 Mar 2019 12:31:04 -0400 Subject: [PATCH 3/6] completes MVP --- src/client.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/client.c b/src/client.c index 3148e7d..44322e1 100644 --- a/src/client.c +++ b/src/client.c @@ -42,6 +42,7 @@ urlinfo_t *parse_url(char *url) urlinfo_t *urlinfo = malloc(sizeof(urlinfo_t)); urlinfo->port = port; urlinfo->path = path; + urlinfo->hostname = hostname; return urlinfo; } @@ -62,11 +63,17 @@ int send_request(int fd, char *hostname, char *port, char *path) char request[max_request_size]; int rv; - /////////////////// - // IMPLEMENT ME! // - /////////////////// + sprintf(request, "GET /%s HTTP/1.1\n" + "Host: %s:%s\n" + "Connection: close", path, hostname, port); + + rv = send(fd, request, strlen(request), 0); - return 0; + if (rv < 0) { + perror("send"); + } + + return rv; } int main(int argc, char *argv[]) @@ -79,18 +86,17 @@ int main(int argc, char *argv[]) exit(1); } - parse_url(argv[1]); - /* - 1. Parse the input URL - 2. Initialize a socket by calling the `get_socket` function from lib.c - 3. Call `send_request` to construct the request and send it - 4. Call `recv` in a loop until there is no more data to receive from the server. Print the received response to stdout. - 5. Clean up any allocated memory and open file descriptors. - */ + urlinfo_t *urlinfo = parse_url(argv[1]); + sockfd = get_socket(urlinfo->hostname, urlinfo->port); + int rv = send_request(sockfd, urlinfo->hostname, urlinfo->port, urlinfo->path); - /////////////////// - // IMPLEMENT ME! // - /////////////////// + while ((numbytes = recv(sockfd, buf, BUFSIZE - 1, 0)) > 0) + { + printf("%s\n", buf); + } + free(urlinfo->hostname); + free(urlinfo); + close(sockfd); return 0; } From 3f47c1fe288d0ec18ba0904b3c893841fd151019 Mon Sep 17 00:00:00 2001 From: AM_Myrick Date: Fri, 15 Mar 2019 12:44:24 -0400 Subject: [PATCH 4/6] can now access non-local hosts --- src/client.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/client.c b/src/client.c index 44322e1..8a5088b 100644 --- a/src/client.c +++ b/src/client.c @@ -30,7 +30,7 @@ urlinfo_t *parse_url(char *url) // copy the input URL so as not to mutate the original char *hostname = strdup(url); char *port; - char *path; + char *path = '\0'; char *first_backslash = strchr(hostname, '/'); path = first_backslash + 1; @@ -65,7 +65,7 @@ int send_request(int fd, char *hostname, char *port, char *path) sprintf(request, "GET /%s HTTP/1.1\n" "Host: %s:%s\n" - "Connection: close", path, hostname, port); + "Connection: close\n\n", path, hostname, port); rv = send(fd, request, strlen(request), 0); @@ -90,6 +90,10 @@ int main(int argc, char *argv[]) sockfd = get_socket(urlinfo->hostname, urlinfo->port); int rv = send_request(sockfd, urlinfo->hostname, urlinfo->port, urlinfo->path); + if (rv < 0) { + perror("send"); + } + while ((numbytes = recv(sockfd, buf, BUFSIZE - 1, 0)) > 0) { printf("%s\n", buf); From 4d91f75713ef4fa06fa9e71353dbe310fec17d3e Mon Sep 17 00:00:00 2001 From: AM_Myrick Date: Fri, 15 Mar 2019 13:09:41 -0400 Subject: [PATCH 5/6] adds default port number --- src/client.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/client.c b/src/client.c index 8a5088b..da7f633 100644 --- a/src/client.c +++ b/src/client.c @@ -30,20 +30,28 @@ urlinfo_t *parse_url(char *url) // copy the input URL so as not to mutate the original char *hostname = strdup(url); char *port; - char *path = '\0'; + char *path; + + urlinfo_t *urlinfo = malloc(sizeof(urlinfo_t)); char *first_backslash = strchr(hostname, '/'); path = first_backslash + 1; *first_backslash = '\0'; char *first_colon = strchr(hostname, ':'); - port = first_colon + 1; - *first_colon = '\0'; + if (first_colon == NULL) + { + port = "80"; + urlinfo->port = port; + } + else + { + port = first_colon + 1; + *first_colon = '\0'; + urlinfo->port = port; + } - urlinfo_t *urlinfo = malloc(sizeof(urlinfo_t)); - urlinfo->port = port; urlinfo->path = path; urlinfo->hostname = hostname; - return urlinfo; } @@ -66,7 +74,7 @@ int send_request(int fd, char *hostname, char *port, char *path) sprintf(request, "GET /%s HTTP/1.1\n" "Host: %s:%s\n" "Connection: close\n\n", path, hostname, port); - + printf("%s\n", request); rv = send(fd, request, strlen(request), 0); if (rv < 0) { From 5b6fec88319fad7260e9b2fec1beb5247edcc4b8 Mon Sep 17 00:00:00 2001 From: AM_Myrick Date: Fri, 15 Mar 2019 13:51:33 -0400 Subject: [PATCH 6/6] can now accept urls with http or https --- src/client.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/client.c b/src/client.c index da7f633..d5e117e 100644 --- a/src/client.c +++ b/src/client.c @@ -33,6 +33,22 @@ urlinfo_t *parse_url(char *url) char *path; urlinfo_t *urlinfo = malloc(sizeof(urlinfo_t)); + char *https = strstr(hostname, "https://"); + if (https != NULL) + { + hostname = https + strlen("https://"); + *https = '\0'; + } + else + { + char *http = strstr(hostname, "http://"); + + if (http != NULL) + { + hostname = http + strlen("http://"); + *http = '\0'; + } + } char *first_backslash = strchr(hostname, '/'); path = first_backslash + 1;