Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sprint Web Server: Kat Johnson-Fries #144

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 39 additions & 44 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ typedef struct urlinfo_t {
char *path;
} urlinfo_t;

/**
* Tokenize the given URL into hostname, path, and port.
*
* url: The input URL to parse.
*
* Store hostname, path, and port in a urlinfo_t struct and return the struct.
*/

urlinfo_t *parse_url(char *url)
{
// copy the input URL so as not to mutate the original
Expand All @@ -34,45 +28,41 @@ urlinfo_t *parse_url(char *url)

urlinfo_t *urlinfo = malloc(sizeof(urlinfo_t));

/*
We can parse the input URL by doing the following:
char *backslash = strchr(hostname, '/');
path = backslash + 1;
*backslash = '\0';

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.
*/
char *colon = strchr(hostname, ':');
port = colon + 1;
*colon = '\0';

///////////////////
// IMPLEMENT ME! //
///////////////////
urlinfo->hostname = hostname;
urlinfo->path = strdup(path);
urlinfo->port = strdup(port);

return urlinfo;
}

/**
* Constructs and sends an HTTP request
*
* fd: The file descriptor of the connection.
* hostname: The hostname string.
* port: The port string.
* path: The path string.
*
* Return the value from the send() function.
*/

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! //
///////////////////
int request_length = sprintf(request,
"GET /%s HTTP/1.1\n"
"Host: %s:%s\n"
"Connection: close\n"
"\n\n",
path, hostname, port);

return 0;
rv = send(fd, request, request_length, 0);
if (rv < 0) {
perror("send");
}

return rv;
}

int main(int argc, char *argv[])
Expand All @@ -85,17 +75,22 @@ 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.
*/

///////////////////
// IMPLEMENT ME! //
///////////////////

urlinfo_t *urlinfo = malloc(sizeof(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) {
fprintf(stdout, "%s\n", buf);
}

free(urlinfo);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You only need this line


close(sockfd);

return 0;
}