Skip to content

Commit

Permalink
Modified client to now send the Host header with the host name of the…
Browse files Browse the repository at this point in the history
… server and now the server uses that header to generate the file links
  • Loading branch information
skelly committed Sep 4, 2024
1 parent c8d0f7f commit 2408066
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/client/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@

const char *CONTENT_LENGTH = "Content-Length: ";
const char *GET_REQ_TEMPLATE =
"GET %s HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
"GET %s HTTP/1.1\r\nHost: %s\r\nConnection: keep-alive\r\n\r\n";

int http_get(int sfd, const char *path, http_res_t *res) {
int http_get(int sfd, const char *path, const char hostname[], http_res_t *res) {
char buf[HTTP_BUFFER_SIZE];
const char *status_code_start, *content_length_start, *body_start;
int bytes_read;
long total_bytes, content_length, header_length, received_length, left_length;

buf[HTTP_BUFFER_SIZE - 1] = 0; // ensure buf is null terminated

snprintf(buf, HTTP_BUFFER_SIZE-1, GET_REQ_TEMPLATE, path);
snprintf(buf, HTTP_BUFFER_SIZE-1, GET_REQ_TEMPLATE, path, hostname);
send_request(sfd, buf);

total_bytes = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/client/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ typedef struct {
size_t size;
} http_res_t;

int http_get(int sfd, const char *path, http_res_t *res);
int http_get(int sfd, const char *path, const char hostname[], http_res_t *res);
void http_free(http_res_t *res);

#endif
#endif
2 changes: 1 addition & 1 deletion src/client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int main() {
return EXIT_FAILURE;
}

if (HTTP_SUCCESS != http_get(sfd, "/", &fraction_links_resp)) {
if (HTTP_SUCCESS != http_get(sfd, "/", hostname, &fraction_links_resp)) {
return EXIT_FAILURE;
}
write(1, fraction_links_resp.data, fraction_links_resp.size);
Expand Down
6 changes: 3 additions & 3 deletions src/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ def list_directory(self, path):

enc = sys.getfilesystemencoding()

server_addr = self.server.server_address
host, port = server_addr
_, port = self.server.server_address
# From now on we will depend on the Host header
for name in file_list:
display_name = f"http://{host}:{port}{self.path}{name}"
display_name = f"http://{self.headers['Host']}:{port}{self.path}{name}"
contents.append(html.escape(display_name, quote=False))

encoded = "\n".join(contents).encode(enc, "surrogateescape")
Expand Down

0 comments on commit 2408066

Please sign in to comment.