Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Sprint Challenge: C Web Server Sprint
# Sprint Challenge: C Web Server Sprint -

This challenge allows you to practice the concepts and techniques learned over
the past week and apply them in a concrete project. This Sprint explored
Expand Down
115 changes: 92 additions & 23 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,65 @@ urlinfo_t *parse_url(char *url)
char *hostname = strdup(url);
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.
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 *host;

char host1[256];
char port1[256];
char path1[256*2];

char *ret;
char *ret2;


//printf("parsed URL: %s %s %s", hostname, path, port);

//printf("parsed URL: %s %s %s", hostname, path, port);

//sscanf(hostname, "%s:%s/%s", host, port, path);
//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).
//ret = strchr(hostname, '/');
//2. Set the path pointer to 1 character after the spot returned by strchr.
//path = ret++;

int i = 0;
while(hostname[i] != '/') {
i++;
}
hostname[i] = ' ';

int j = 0;
while(hostname[j] != ':') {
j++;
}
hostname[j] = ' ';


//3. Overwrite the backslash with a '\0' so that we are no longer considering anything after the backslash.
//ret = '\0';
//4. Use strchr to find the first colon in the URL.
//ret2 = strchr(hostname, ':');
//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.
sscanf(hostname, "%s %s %s", host1, port1, path1);

//host = strtok(hostname, ":");

//strcat("/%s", path1);
//strcat(":%s", port1);

path = path1;
host = host1;
port = port1;


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

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

///////////////////
// IMPLEMENT ME! //
///////////////////
Expand All @@ -71,28 +116,52 @@ int send_request(int fd, char *hostname, char *port, char *path)
///////////////////
// IMPLEMENT ME! //
///////////////////
int request_length = sprintf(request, "GET /%s HTTP/1.1\nHost: %s:%s\nConnection: close\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;
char buf[BUFSIZE];

char *req;

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.
*/

req = argv[1];
//1. Parse the input URL
printf("Got URL: %s\n", req);
urlinfo_t *url = parse_url(req);
printf("parsed URL: Hostname %s Port %s Path %s\n", url->hostname, url->port, url->path);

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

if (sockfd < 0) {
fprintf(stderr, "webserver: fatal error getting listening socket\n");
exit(1);
}
//3. Call `send_request` to construct the request and send it
send_request(sockfd, url->hostname, url->port, url->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) {
fprintf(stdout, "%s", buf);
}
//5. Clean up any allocated memory and open file descriptors.
close(sockfd);
free(url);

///////////////////
// IMPLEMENT ME! //
///////////////////
Expand Down