diff --git a/src/client.c b/src/client.c index 5246a49..d5e117e 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; @@ -31,24 +31,43 @@ urlinfo_t *parse_url(char *url) char *hostname = strdup(url); char *port; 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'; + } + } - /* - 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! // - /////////////////// - + char *first_backslash = strchr(hostname, '/'); + path = first_backslash + 1; + *first_backslash = '\0'; + char *first_colon = strchr(hostname, ':'); + if (first_colon == NULL) + { + port = "80"; + urlinfo->port = port; + } + else + { + port = first_colon + 1; + *first_colon = '\0'; + urlinfo->port = port; + } + + urlinfo->path = path; + urlinfo->hostname = hostname; return urlinfo; } @@ -68,11 +87,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\n\n", path, hostname, port); + printf("%s\n", request); + rv = send(fd, request, strlen(request), 0); - return 0; + if (rv < 0) { + perror("send"); + } + + return rv; } int main(int argc, char *argv[]) @@ -85,17 +110,21 @@ int main(int argc, char *argv[]) exit(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! // - /////////////////// + if (rv < 0) { + perror("send"); + } + + while ((numbytes = recv(sockfd, buf, BUFSIZE - 1, 0)) > 0) + { + printf("%s\n", buf); + } + free(urlinfo->hostname); + free(urlinfo); + close(sockfd); return 0; }