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

Sprint Challenge Server - Farhan #147

Open
wants to merge 4 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
63 changes: 50 additions & 13 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
#include "lib.h"

#define BUFSIZE 4096 // max number of bytes we can get at once
Expand Down Expand Up @@ -32,8 +33,17 @@ urlinfo_t *parse_url(char *url)
char *port;
char *path;

// HOSTNAME:PORT/PATH\n

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

urlinfo->hostname = strtok(hostname, ":");

urlinfo->port = strtok(NULL, "/");

urlinfo->path = strtok(NULL, "/");


/*
We can parse the input URL by doing the following:

Expand All @@ -45,10 +55,6 @@ urlinfo_t *parse_url(char *url)
6. Overwrite the colon with a '\0' so that we are just left with the hostname.
*/

///////////////////
// IMPLEMENT ME! //
///////////////////

return urlinfo;
}

Expand All @@ -68,23 +74,58 @@ int send_request(int fd, char *hostname, char *port, char *path)
char request[max_request_size];
int rv;

///////////////////
// IMPLEMENT ME! //
///////////////////
// REMEMBER ITS ONLY GET REQUEST

return 0;
const int max_response_size = 65536*5;
char response[max_response_size];
int response_length;

// Build HTTP response and store it in response


sprintf(request,"GET /%s HTTP/1.1\nHost: %s:%s\nConnection: close\n\n",path,hostname,port);

rv = send(fd,request,strlen(request),0);

return rv;
}

int main(int argc, char *argv[])
{
int sockfd, numbytes;
char buf[BUFSIZE];

if (argc != 2) {
if (argc == 2) {
struct urlinfo_t *url = parse_url(argv[1]);
// printf("%s\n", argv[1]);
printf("HostName: %s\n", url->hostname);
printf("Port: %s\n", url->port);
printf("Path: %s\n", url->path);

sockfd = get_socket(url->hostname, url->port);

if (sockfd == -1){
fprintf(stderr, "ERROR: Could not get socket. Try again.\n");
}

int req_status = send_request(sockfd, url->hostname, url->port, url->path);

while ((numbytes = recv(sockfd, buf, BUFSIZE - 1, 0)) > 0) {
// print the data we got back to stdout
printf("%s\n", buf);
}

close(sockfd);
free(url);

} else {

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
Expand All @@ -93,9 +134,5 @@ int main(int argc, char *argv[])
5. Clean up any allocated memory and open file descriptors.
*/

///////////////////
// IMPLEMENT ME! //
///////////////////

return 0;
}