void handleproxyrequest(int fd) { /* * The code below does a DNS lookup of…
- User
void handle_proxy_request(int fd) {
/*
- The code below does a DNS lookup of server_proxy_hostname and
- opens a connection to it. Please do not modify. */ struct sockaddr_in target_address; memset(&target_address, 0, sizeof(target_address)); target_address.sin_family = AF_INET; target_address.sin_port = htons(server_proxy_port);
// Use DNS to resolve the proxy target's IP address struct hostent* target_dns_entry = gethostbyname2(server_proxy_hostname, AF_INET);
// Create an IPv4 TCP socket to communicate with the proxy target. int target_fd = socket(PF_INET, SOCK_STREAM, 0); if (target_fd == -1) { fprintf(stderr, "Failed to create a new socket: error %d: %s\n", errno, strerror(errno)); close(fd); exit(errno); }
if (target_dns_entry == NULL) { fprintf(stderr, "Cannot find host: %s\n", server_proxy_hostname); close(target_fd); close(fd); exit(ENXIO); }
char* dns_address = target_dns_entry->h_addr_list[0];
// Connect to the proxy target. memcpy(&target_address.sin_addr, dns_address, sizeof(target_address.sin_addr)); int connection_status = connect(target_fd, (struct sockaddr*)&target_address, sizeof(target_address));
if (connection_status < 0) { /* Dummy request parsing, just to be compliant. */ http_request_parse(fd);
http_start_response(fd, 502); http_send_header(fd, "Content-Type", "text/html"); http_end_headers(fd); close(target_fd); close(fd); return;}
/* TODO: PART 4 / / PART 4 BEGIN */
/* PART 4 END */ }为了完成代理任务,你需要实现 handle_proxy_request。handle_proxy_request函数将 HTTP 请求代理到另一个 HTTP 服务器。我们已经为你处理了用于设置连接的代码。你应该阅读并理解它,但不需要修改它。
下面是已经实现的内容。
我们使用 --proxy 命令行参数的值,其中包含上游 HTTP 服务器的地址和端口号。这两个值存储在全局变量 server_proxy_hostname 和 server_proxy_port 中。 我们对服务器代理主机名进行 DNS 查询,这将会查询到主机名的 IP 地址(详情见 gethostbyname))。 我们创建一个网络套接字,并将其连接到从 DNS 获取的 IP 地址。详见socket和connect函数。 htons 用于设置套接字的端口号(请注意:x86 内存中的整数是小端的,而网络设备则是大端的)。还要注意的是,HTTP 是一种 SOCK_STREAM 协议(即TCP协议)。 以下是你需要注意的事项。
在两个套接字(HTTP 客户端 fd 和目标 HTTP 服务器 fd)上等待新数据。数据到达后,应立即将其读入缓冲区,然后写入另一个套接字。你应当在 HTTP 客户端和目标 HTTP 服务器之间保持双向通信。你的代理必须支持多个请求/响应。 这比写入文件或从 stdin 读取数据更加棘手,因为你不知道双向流的哪一方会先写入数据,也不知道它们在收到响应后是否会写入更多数据。在代理模式下,你会发现在同一连接中会发送多个 HTTP 请求/响应,而你的 HTTP 服务器则不同,它只需要在每个连接中支持一个请求/响应。 你应该使用 pthreads 来完成这项任务。考虑使用两个线程来实现双向通信,一个从 A 到 B,另一个从 B 到 A。 请勿使用 select、fcntl 或类似函数。在前几个学期,我们曾经推荐过这种方法,但我们发现这种方法太混乱了。 如果任一套接字关闭,通信将无法继续,因此应关闭两个套接字以终止连接。
- Assistant
下面是一个可能的解决方案:
#include <pthread.h> // Define a buffer size #define BUFFER_SIZE 1024 void* forward_data(void* arg) { int* fds = (int*)arg; int fd1 = fds[0]; int fd2 = fds[1]; char buf[BUFFER_SIZE]; int len; while ((len = read(fd1, buf, BUFFER_SIZE)) > 0) { write(fd2, buf, len); } close(fd1); close(fd2); pthread_exit(NULL); } void handle_proxy_request(int fd) { // DNS lookup and socket creation codes... // Create two threads to handle the forward and backward connections pthread_t forward_thread, backward_thread; // Create two file descriptors for communication between threads int forward_fds[2]; int backward_fds[2]; // Initialize the file descriptors pipe(forward_fds); pipe(backward_fds); // Set up the arguments for the threads int forward_args[2] = {fd, target_fd}; int backward_args[2] = {target_fd, fd}; // Create the forward and backward threads pthread_create(&forward_thread, NULL, forward_data, (void*)forward_args); pthread_create(&backward_thread, NULL, forward_data, (void*)backward_args); // Wait for the threads to finish pthread_join(forward_thread, NULL); pthread_join(backward_thread, NULL); } - User
- Assistant
void serve_directory(int fd, char* path) { http_start_response(fd, 200); http_send_header(fd, "Content-Type", http_get_mime_type(".html")); http_end_headers(fd); // Check if index.html exists in the directory char* index_path = join_string(path, "/index.html", NULL); struct stat index_info; int result = stat(index_path, &index_info); if (result == 0) { // index.html exists, serve the file serve_file(fd, index_path); } else { // index.html does not exist, send a list of files in the directory DIR* directory = opendir(path); struct dirent* entry; while ((entry = readdir(directory)) != NULL) { if (strcmp(entry->d_name, "..") != 0) { // Create a link with proper href for each file in the directory char* buffer = malloc(strlen(entry->d_name) + strlen(path) + 4); sprintf(buffer, "<a href=\"%s/%s\">%s</a><br/>", path, entry->d_name, entry->d_name); send_string(fd, buffer); free(buffer); } } closedir(directory); } free(index_path); } void handle_files_request(int fd) { struct http_request* request = http_request_parse(fd); if (request == NULL || request->path[0] != '/') { http_start_response(fd, 400); http_send_header(fd, "Content-Type", "text/html"); http_end_headers(fd); close(fd); return; } if (strstr(request->path, "..") != NULL) { http_start_response(fd, 403); http_send_header(fd, "Content-Type", "text/html"); http_end_headers(fd); close(fd); return; } char* path = malloc(2 + strlen(request->path) + 1); path[0] = '.'; path[1] = '/'; memcpy(path + 2, request->path, strlen(request->path) + 1); struct stat path_info; int result = stat(path, &path_info); if (S_ISDIR(path_info.st_mode)) { serve_directory(fd, path); } else if (S_ISREG(path_info.st_mode)) { serve_file(fd, path); } else { http_start_response(fd, 404); http_send_header(fd, "Content-Type", "text/html"); http_end_headers(fd); } free(path); close(fd); return; }