-
Notifications
You must be signed in to change notification settings - Fork 236
Max Kajiwara #136
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
Open
maxkajiwara
wants to merge
5
commits into
bloominstituteoftechnology:master
Choose a base branch
from
maxkajiwara:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Max Kajiwara #136
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -34,20 +35,26 @@ urlinfo_t *parse_url(char *url) | |
|
|
||
| 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). | ||
| char *backslash = strchr(hostname, '/'); | ||
|
|
||
| // 2. Set the path pointer to 1 character after the spot returned by strchr. | ||
| path = backslash + 1; | ||
|
|
||
| // 3. Overwrite the backslash with a '\0' so that we are no longer considering anything after the backslash. | ||
| *backslash = '\0'; | ||
|
|
||
| // 4. Use strchr to find the first colon in the URL. | ||
| char *colon = strchr(hostname, ':'); | ||
|
|
||
| 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. | ||
| */ | ||
| // 5. Set the port pointer to 1 character after the spot returned by strchr. | ||
| port = colon + 1; | ||
|
|
||
| /////////////////// | ||
| // IMPLEMENT ME! // | ||
| /////////////////// | ||
| // 6. Overwrite the colon with a '\0' so that we are just left with the hostname. | ||
| *colon = '\0'; | ||
|
|
||
| urlinfo->port = port; | ||
| urlinfo->path = path; | ||
|
|
||
| return urlinfo; | ||
| } | ||
|
|
@@ -68,34 +75,54 @@ 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" | ||
| "Connection: close\n" | ||
| "Host: %s:%s\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; | ||
| { | ||
| 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. | ||
| */ | ||
|
|
||
| /////////////////// | ||
| // IMPLEMENT ME! // | ||
| /////////////////// | ||
| // 1. Parse the input URL | ||
| char *url = argv[1]; | ||
| urlinfo_t *urlinfo = parse_url(url); | ||
|
|
||
| // 2. Initialize a socket by calling the `get_socket` function from lib.c | ||
| sockfd = get_socket(urlinfo->hostname, urlinfo->port); | ||
|
|
||
| // 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) | ||
| { | ||
| printf("%s\n", buf); | ||
| } | ||
|
|
||
| // 5. Clean up any allocated memory and open file descriptors. | ||
| free(urlinfo->hostname); | ||
| free(urlinfo); | ||
|
|
||
| close(sockfd); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great job freeing your data after you finish with it. |
||
|
|
||
| return 0; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good semantic name for backslash and colon