From c0245d522e0e894a9b331785b17b65f3c8151443 Mon Sep 17 00:00:00 2001 From: Sergey Osipyan Date: Fri, 28 Jun 2019 13:22:07 -0700 Subject: [PATCH] MVP --- src/client.c | 54 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/src/client.c b/src/client.c index a302333..08e6309 100644 --- a/src/client.c +++ b/src/client.c @@ -45,10 +45,29 @@ urlinfo_t *parse_url(char *url) 6. Overwrite the colon with a '\0' so that we are just left with the hostname. */ - /////////////////// - // IMPLEMENT ME! // - /////////////////// + char *https = strstr(hostname, "https://"); + if (https != NULL) { + hostname += 8; + } + char *http = (strstr(hostname, "http://")); + if (http != NULL) { + hostname += 7; + } + char *pathSlash = strstr(hostname, "/"); + pathSlash = pathSlash + 1; + *pathSlash = '\0'; + + char *portStart = strstr(url, ":"); + if (portStart != NULL) { + urlinfo->port = portStart +1; + *portStart = '\0'; + } else { + urlinfo->port = "80"; + } + + urlinfo->hostname = hostname; + urlinfo->path = path; return urlinfo; } @@ -67,11 +86,16 @@ int send_request(int fd, char *hostname, char *port, char *path) const int max_request_size = 16384; char request[max_request_size]; int rv; - - /////////////////// - // IMPLEMENT ME! // - /////////////////// - + rv = sprintf(request, + "GET /%s HTTP/1.1\n" + "Host: %s:%s\n" + "Connection: close\n" + "\n", + path, hostname, port); + rv = send(fd, request, rv, 0); + if (rv < 0) { + perror("send"); + } return 0; } @@ -93,9 +117,17 @@ int main(int argc, char *argv[]) 5. Clean up any allocated memory and open file descriptors. */ - /////////////////// - // IMPLEMENT ME! // - /////////////////// + 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) { + buf[numbytes] = '\0'; + fprintf(stdout, "%s\n", buf); + } + printf("\n"); + free(urlinfo); + close(sockfd); return 0; }