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

Austin Pendergrast #135

Open
wants to merge 1 commit 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
96 changes: 86 additions & 10 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,62 @@ urlinfo_t *parse_url(char *url)
6. Overwrite the colon with a '\0' so that we are just left with the hostname.
*/

///////////////////
// IMPLEMENT ME! //
///////////////////
// Check the if the URL contains HTTP
if (strstr(url, "http://"))
{
// If URL contains 'http://'
hostname = strdup(url + 7);

}
else if (strstr(url, "https://"))
{
// If URL contains 'https://'
hostname = strdup(url + 8);

}
else
{
// If URL does not contain any HTTP
hostname = strdup(url);
}

// Set the path
// If the hostname contains a /, set the path = /
if (strchr(hostname, '/'))
{
path = strchr(hostname, '/') + 1;

*(path - 1) = NULL;
}
else
{
path = '/';
}

// Set the port
// If the hostname contains a :, set the port to the number after
if (strchr(hostname, ':'))
{
port = strchr(hostname, ':') + 1;

*(port - 1) = NULL;
}
else
{
char buffer[3];

sprintf(buffer, "%i", 80);

port = buffer;
}

// Set the properties in the urlinfo

urlinfo->hostname = hostname;

urlinfo->path = strdup(path);

urlinfo->port = strdup(port);

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

///////////////////
// IMPLEMENT ME! //
///////////////////
// get request length
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);

return 0;
}
Expand All @@ -80,7 +143,8 @@ int main(int argc, char *argv[])
int sockfd, numbytes;
char buf[BUFSIZE];

if (argc != 2) {
if (argc != 2)
{
fprintf(stderr,"usage: client HOSTNAME:PORT/PATH\n");
exit(1);
}
Expand All @@ -93,9 +157,21 @@ int main(int argc, char *argv[])
5. Clean up any allocated memory and open file descriptors.
*/

///////////////////
// IMPLEMENT ME! //
///////////////////
// get struct values
urlinfo_t *u = parse_url(argv[1]);

// get socket int val
sockfd = get_socket(u->hostname, u->port);

// pass struct and socket values to send_request
send_request(sockfd, u->hostname, u->port, u->path);

while((numbytes = recv(sockfd, buf, BUFSIZE - 1, 0)) > 0){
printf("%s\n", buf);
}

free(u);
close(sockfd);

return 0;
}