diff --git a/src/client.c b/src/client.c index 5246a49..0f05364 100644 --- a/src/client.c +++ b/src/client.c @@ -45,9 +45,62 @@ urlinfo_t *parse_url(char *url) 6. Overwrite the colon with a '\0' so that we are just left with the hostname. */ - /////////////////// - // IMPLEMENT ME! // - /////////////////// + // Check the if the URL contains HTTP + if (strstr(url, "http://")) + { + // If URL contains 'http://' + hostname = strdup(url + 7); + + } + else if (strstr(url, "https://")) + { + // If URL contains 'https://' + hostname = strdup(url + 8); + + } + else + { + // If URL does not contain any HTTP + hostname = strdup(url); + } + + // Set the path + // If the hostname contains a /, set the path = / + if (strchr(hostname, '/')) + { + path = strchr(hostname, '/') + 1; + + *(path - 1) = NULL; + } + else + { + path = '/'; + } + + // Set the port + // If the hostname contains a :, set the port to the number after + if (strchr(hostname, ':')) + { + port = strchr(hostname, ':') + 1; + + *(port - 1) = NULL; + } + else + { + char buffer[3]; + + sprintf(buffer, "%i", 80); + + port = buffer; + } + + // Set the properties in the urlinfo + + urlinfo->hostname = hostname; + + urlinfo->path = strdup(path); + + urlinfo->port = strdup(port); return urlinfo; } @@ -68,9 +121,19 @@ int send_request(int fd, char *hostname, char *port, char *path) char request[max_request_size]; int rv; - /////////////////// - // IMPLEMENT ME! // - /////////////////// + // get request length + int request_length = sprintf( + request, + "GET /%s HTTP/1.1\n" + "HOST: %s:%s\n" + "Connection: close\n" + "\n", + path, + hostname, + port + ); + + rv = send(fd, request, request_length, 0); return 0; } @@ -80,7 +143,8 @@ int main(int argc, char *argv[]) int sockfd, numbytes; char buf[BUFSIZE]; - if (argc != 2) { + if (argc != 2) + { fprintf(stderr,"usage: client HOSTNAME:PORT/PATH\n"); exit(1); } @@ -93,9 +157,21 @@ int main(int argc, char *argv[]) 5. Clean up any allocated memory and open file descriptors. */ - /////////////////// - // IMPLEMENT ME! // - /////////////////// + // get struct values + urlinfo_t *u = parse_url(argv[1]); + + // get socket int val + sockfd = get_socket(u->hostname, u->port); + + // pass struct and socket values to send_request + send_request(sockfd, u->hostname, u->port, u->path); + + while((numbytes = recv(sockfd, buf, BUFSIZE - 1, 0)) > 0){ + printf("%s\n", buf); + } + + free(u); + close(sockfd); return 0; }