From d46f7ffccd03aa76ae081b528676f1ba6f87320d Mon Sep 17 00:00:00 2001 From: Vuk Radosavljevic Date: Fri, 8 Mar 2019 14:56:14 -0600 Subject: [PATCH] Completed sprint challenge --- src/Makefile | 0 src/client.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++-- src/lib.c | 0 src/lib.h | 0 4 files changed, 62 insertions(+), 2 deletions(-) mode change 100644 => 100755 src/Makefile mode change 100644 => 100755 src/client.c mode change 100644 => 100755 src/lib.c mode change 100644 => 100755 src/lib.h diff --git a/src/Makefile b/src/Makefile old mode 100644 new mode 100755 diff --git a/src/client.c b/src/client.c old mode 100644 new mode 100755 index 5246a49..05b9728 --- a/src/client.c +++ b/src/client.c @@ -34,6 +34,35 @@ urlinfo_t *parse_url(char *url) urlinfo_t *urlinfo = malloc(sizeof(urlinfo_t)); + // char *placeholder = strstr(hostname, "://"); + + + // if (placeholder != NULL) { + // hostname = placeholder; + // hostname += 3; + // } + + // path = strchr(hostname, '/'); + // *path = '\0'; + // path++; + path = strchr(hostname, '/') + 1; + *(path - 1) = '\0'; + + // port = strchr(hostname, ':'); + // *port = '\0'; + // port++; + port = strchr(hostname, ':') + 1; + *(port - 1) = '\0'; + + + urlinfo->hostname = hostname; + urlinfo->path = strdup(path); + urlinfo->port = strdup(port); + + // urlinfo->hostname = strdup(hostname); + // urlinfo->path = strdup(path); + // urlinfo->port = strdup(port); + /* We can parse the input URL by doing the following: @@ -48,7 +77,8 @@ urlinfo_t *parse_url(char *url) /////////////////// // IMPLEMENT ME! // /////////////////// - + + printf("\n\n%s%s%s\n\n", urlinfo->hostname, urlinfo->path, urlinfo->port); return urlinfo; } @@ -72,7 +102,19 @@ int send_request(int fd, char *hostname, char *port, char *path) // IMPLEMENT ME! // /////////////////// - return 0; + int request_length = sprintf(request,"GET /%s HTTP/1.1\nHost: %s:%s\nConnection:close\n\n",path,hostname,port); + + + + rv = send(fd, request, request_length, 0); + + if (rv < 0) { + + perror("send"); + + } + + return rv; } int main(int argc, char *argv[]) @@ -85,6 +127,24 @@ int main(int argc, char *argv[]) exit(1); } + urlinfo_t *urlinfo = parse_url(argv[1]); + + sockfd = get_socket(urlinfo->hostname, urlinfo->port); + + send_request(sockfd, urlinfo->hostname, urlinfo->port, urlinfo->path); + + + while ((numbytes = recv(sockfd, buf, BUFSIZE - 1, 0)) > 0) { + printf("%s", buf); + } + + close(sockfd); + free(urlinfo->hostname); + free(urlinfo->path); + free(urlinfo->port); + free(urlinfo); + + /* 1. Parse the input URL 2. Initialize a socket by calling the `get_socket` function from lib.c diff --git a/src/lib.c b/src/lib.c old mode 100644 new mode 100755 diff --git a/src/lib.h b/src/lib.h old mode 100644 new mode 100755