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

rileyB-c-web-server-sprint #152

Open
wants to merge 2 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
101 changes: 92 additions & 9 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 @@ -28,15 +29,14 @@ typedef struct urlinfo_t {
urlinfo_t *parse_url(char *url)
{
// copy the input URL so as not to mutate the original
char *hostname = strdup(url);
char *hostname;
char *port;
char *path;

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).
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.
Expand All @@ -49,6 +49,43 @@ urlinfo_t *parse_url(char *url)
// IMPLEMENT ME! //
///////////////////

if (strstr(url, "http://"))
{
hostname = strdup(url + 7);
}
else if (strstr(url, "https://"))
{
hostname = strdup(url + 8);
}
else
{
hostname = strdup(url);
}

if (strchr(hostname, '/'))
{
path = strchr(hostname, '/') + 1;
*(path - 1) = '\0';
}
else
{
path = "";
}

if (strchr(hostname, ':'))
{
port = strchr(hostname, ':') + 1;
*(port - 1) = '\0';
}
else
{
port = "80";
}

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

return urlinfo;
}

Expand All @@ -72,16 +109,32 @@ int send_request(int fd, char *hostname, char *port, char *path)
// IMPLEMENT ME! //
///////////////////

return 0;
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);

if (rv < 0)
{
printf("request err");
exit(2);
}

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);
}

Expand All @@ -97,5 +150,35 @@ int main(int argc, char *argv[])
// IMPLEMENT ME! //
///////////////////

// Parse the input URL
urlinfo_t *urlinfo = malloc(sizeof(urlinfo_t));
urlinfo = parse_url(argv[1]);

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

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

// 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);
}

// close fd
close(sockfd);

// free allocated memory
urlinfo->port = NULL;
urlinfo->hostname = NULL;
urlinfo->path = NULL;

free(urlinfo->port);
free(urlinfo->hostname);
free(urlinfo->path);
free(urlinfo);

return 0;
}
}