Skip to content
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
91 changes: 59 additions & 32 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
/**
* Struct to hold all three pieces of a URL
*/
typedef struct urlinfo_t {
typedef struct urlinfo_t
{
char *hostname;
char *port;
char *path;
Expand All @@ -34,20 +35,26 @@ urlinfo_t *parse_url(char *url)

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

/*
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).
char *backslash = strchr(hostname, '/');

Choose a reason for hiding this comment

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

Good semantic name for backslash and colon


// 2. Set the path pointer to 1 character after the spot returned by strchr.
path = backslash + 1;

// 3. Overwrite the backslash with a '\0' so that we are no longer considering anything after the backslash.
*backslash = '\0';

// 4. Use strchr to find the first colon in the URL.
char *colon = strchr(hostname, ':');

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.
*/
// 5. Set the port pointer to 1 character after the spot returned by strchr.
port = colon + 1;

///////////////////
// IMPLEMENT ME! //
///////////////////
// 6. Overwrite the colon with a '\0' so that we are just left with the hostname.
*colon = '\0';

urlinfo->port = port;
urlinfo->path = path;

return urlinfo;
}
Expand All @@ -68,34 +75,54 @@ int send_request(int fd, char *hostname, char *port, char *path)
char request[max_request_size];
int rv;

///////////////////
// IMPLEMENT ME! //
///////////////////
int request_length = sprintf(request, "GET /%s HTTP/1.1\n"
"Connection: close\n"
"Host: %s:%s\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[])
{
int sockfd, numbytes;
{
int sockfd, numbytes;
char buf[BUFSIZE];

if (argc != 2) {
fprintf(stderr,"usage: client HOSTNAME:PORT/PATH\n");
if (argc != 2)
{
fprintf(stderr, "usage: client HOSTNAME:PORT/PATH\n");
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! //
///////////////////
// 1. Parse the input URL
char *url = argv[1];
urlinfo_t *urlinfo = parse_url(url);

// 2. Initialize a socket by calling the `get_socket` function from lib.c
sockfd = get_socket(urlinfo->hostname, urlinfo->port);

// 3. Call `send_request` to construct the request and send it
send_request(sockfd, urlinfo->hostname, urlinfo->port, urlinfo->path);

// 4. Call `recv` in a loop until there is no more data to receive from the server. Print the received response to stdout.
while ((numbytes = recv(sockfd, buf, BUFSIZE - 1, 0)) > 0)
{
printf("%s\n", buf);
}

// 5. Clean up any allocated memory and open file descriptors.
free(urlinfo->hostname);
free(urlinfo);

close(sockfd);

Choose a reason for hiding this comment

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

Great job freeing your data after you finish with it.


return 0;
}