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
110 changes: 78 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,30 @@ urlinfo_t *parse_url(char *url)

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

/*
We can parse the input URL by doing the following:
char *identifier = strdup(url);
//1. Use strchr to find the first backslash in the URL (this is assuming there is no http:// or https:// in the URL).
char *tmp = strchr(identifier, '/');

//2. Set the path pointer to 1 character after the spot returned by strchr.
path = tmp + 1;
printf("path is: %s\n", path);
//3. Overwrite the backslash with a '\0' so that we are no longer considering anything after the backslash.
*tmp = '\0';

//4. Use strchr to find the first colon in the URL.
tmp = strchr(identifier, ':');
//5. Set the port pointer to 1 character after the spot returned by strchr.
port = tmp + 1;
printf("port is: %s\n", port);

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.
*/
//6. Overwrite the colon with a '\0' so that we are just left with the hostname.
*tmp = '\0';
hostname = identifier;
printf("hostname is: %s\n", hostname);

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

return urlinfo;
}
Expand All @@ -68,34 +79,69 @@ 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"
"Host: %s:%s\n"
"Connection: close\n"
"\n",
path,
hostname,
port);

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

return 0;
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.
*/
//1. Parse the input URL
printf("parsing %s\n", argv[1]);
urlinfo_t *urlinfo = parse_url(argv[1]);

//2. Initialize a socket by calling the `get_socket` function from lib.c
sockfd = get_socket(urlinfo->hostname, urlinfo->port);
if (sockfd < 0)
{
printf("issue with socket");
}

///////////////////
// IMPLEMENT ME! //
///////////////////
printf("received socket %d", sockfd);

//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)
{
fwrite(buf, 1, numbytes, stdout);
}

if (numbytes < 0)
{
perror("client: receive");
exit(3);
}
//5. Clean up any allocated memory and open file descriptors.

free(urlinfo->hostname);
free(urlinfo);
close(sockfd);

return 0;
}
}