diff --git a/README.md b/README.md index 32c21e3..deb4aa8 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ + # Sprint Challenge: C Web Server Sprint This challenge allows you to practice the concepts and techniques learned over diff --git a/src/client.c b/src/client.c index 5246a49..36949a8 100644 --- a/src/client.c +++ b/src/client.c @@ -33,7 +33,6 @@ urlinfo_t *parse_url(char *url) char *path; urlinfo_t *urlinfo = malloc(sizeof(urlinfo_t)); - /* We can parse the input URL by doing the following: @@ -46,7 +45,21 @@ urlinfo_t *parse_url(char *url) */ /////////////////// - // IMPLEMENT ME! // + char *fs = strchr(hostname, '/'); + if(fs != NULL) { + path = fs + 1; + *fs = '\0'; + } + + char *fc = strchr(hostname, ':'); + if(fc != NULL) { + port = fc + 1; + *fc = '\0'; + } + urlinfo->hostname = hostname; + urlinfo->port = port; + urlinfo->path = path; + /////////////////// return urlinfo; @@ -69,10 +82,15 @@ int send_request(int fd, char *hostname, char *port, char *path) int rv; /////////////////// - // IMPLEMENT ME! // + int request_length = snprintf(request, max_request_size, "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; + return rv; } int main(int argc, char *argv[]) @@ -85,16 +103,21 @@ 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\n\n", buf); + } + /* - 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. + // 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. */ - - /////////////////// - // IMPLEMENT ME! // /////////////////// return 0;