-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
server { | ||
listen 8080; | ||
root /root/c_code/web服务器_updata/static/index.html | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef CONFIG_H | ||
#define CONFIG_H | ||
|
||
#include <stddef.h> | ||
|
||
struct server_config { | ||
int port; | ||
char root[1024]; | ||
}; | ||
|
||
int parse_config(const char *config_file, struct server_config *config); | ||
|
||
#endif // CONFIG_H |
This file contains 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef REQUEST_H | ||
#define REQUEST_H | ||
|
||
#include <stddef.h> | ||
#include "config.h" | ||
|
||
void handle_request(int client_fd, struct server_config *config); | ||
|
||
#endif // REQUEST_H |
This file contains 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef RESPONSE_H | ||
#define RESPONSE_H | ||
|
||
#include <stddef.h> | ||
#include "config.h" | ||
|
||
void send_response(int client_fd, struct server_config *config, const char *response_body); | ||
|
||
#endif // RESPONSE_H |
This file contains 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef SERVER_H | ||
#define SERVER_H | ||
|
||
#include <stddef.h> | ||
#include "config.h" | ||
|
||
|
||
|
||
void start_server(struct server_config *config); | ||
void handle_request(int client_fd, struct server_config *config); | ||
|
||
#endif // SERVER_H |
This file contains 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
CC = gcc | ||
CFLAGS = -Wall -I./includes | ||
|
||
all: web_server | ||
|
||
web_server: src/main.o src/server.o src/request.o src/response.o src/config.o | ||
$(CC) $(CFLAGS) -o web_server src/main.o src/server.o src/request.o src/response.o src/config.o | ||
|
||
src/main.o: src/main.c | ||
$(CC) $(CFLAGS) -c src/main.c -o src/main.o | ||
|
||
src/server.o: src/server.c | ||
$(CC) $(CFLAGS) -c src/server.c -o src/server.o | ||
|
||
src/request.o: src/request.c | ||
$(CC) $(CFLAGS) -c src/request.c -o src/request.o | ||
|
||
src/response.o: src/response.c | ||
$(CC) $(CFLAGS) -c src/response.c -o src/response.o | ||
|
||
src/config.o: src/config.c | ||
$(CC) $(CFLAGS) -c src/config.c -o src/config.o | ||
|
||
clean: | ||
rm -f src/*.o web_server |
This file contains 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
#include<string.h> | ||
#include "config.h" | ||
|
||
int parse_config(const char *config_file, struct server_config *config) { | ||
FILE *file = fopen(config_file, "r"); | ||
if (!file) { | ||
perror("fopen"); | ||
return -1; | ||
} | ||
|
||
char line[1024]; | ||
while (fgets(line, sizeof(line), file)) { | ||
char *key = strtok(line, " \t\n"); | ||
char *value = strtok(NULL, " \t\n"); | ||
|
||
if (strcmp(key, "listen") == 0) { | ||
config->port = atoi(value); | ||
} else if (strcmp(key, "root") == 0) { | ||
strncpy(config->root, value, sizeof(config->root) - 1); | ||
} | ||
} | ||
|
||
fclose(file); // 确保在返回前关闭文件 | ||
return 0; | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include<stdio.h> | ||
#include <stdlib.h> | ||
#include<string.h> | ||
#include <unistd.h> | ||
#include <sys/socket.h> | ||
#include <arpa/inet.h> | ||
#include "server.h" | ||
#include "config.h" | ||
|
||
int main(int argc, char *argv[]) { | ||
if (argc != 2) { | ||
printf("Usage: %s<config_file>\n", argv[0]); | ||
exit(1); | ||
} | ||
|
||
struct server_config config; | ||
|
||
if (parse_config(argv[1], &config) == -1) { | ||
printf("Error parsing config file.\n"); | ||
exit(1); | ||
} | ||
|
||
start_server(&config); | ||
|
||
|
||
return 0; | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include<stdio.h> | ||
#include <stdlib.h> | ||
#include<string.h> | ||
#include <unistd.h> | ||
#include "request.h" | ||
#include "response.h" | ||
#include "config.h" | ||
void handle_request(int client_fd, struct server_config *config) { | ||
printf("开始接受请求\n"); | ||
char buffer[1024]; | ||
ssize_t bytes_read; | ||
|
||
bytes_read = read(client_fd, buffer, sizeof(buffer) - 1); | ||
if (bytes_read == -1) { | ||
perror("read"); | ||
return; | ||
} | ||
buffer[bytes_read] = '\0'; | ||
printf("请求处理完毕\n"); | ||
|
||
// 解析请求并生成响应 | ||
// ... | ||
const char *response_body = "<html><body><h1>Hello, I'm Heng</h1></body></html>"; | ||
printf("Server configuration:\nPort: %d\nRoot directory: %s\n", config->port, config->root); | ||
|
||
// 调用 send_response 函数发送响应 | ||
send_response(client_fd, config, response_body); | ||
|
||
|
||
|
||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
#include<string.h> | ||
#include<unistd.h> | ||
#include "response.h" | ||
|
||
void send_response(int client_fd, struct server_config *config, const char *response_body) { | ||
printf("开始发送请求\n"); // 使用 \n 作为换行,并修复双引号 | ||
char response[1024]; | ||
snprintf(response, sizeof(response), // 使用 snprintf 防止缓冲区溢出 | ||
"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %zu\r\n\r\n%s", | ||
strlen(response_body) + 1, // 包括结束的空字符 | ||
response_body); | ||
write(client_fd, response, strlen(response)); | ||
printf("结束发送请求\n"); // 使用 \n 作为换行,并修复双引号 | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include<stdio.h> | ||
#include <stdlib.h> | ||
#include<string.h> | ||
#include <unistd.h> | ||
#include <sys/socket.h> | ||
#include <arpa/inet.h> | ||
#include "server.h" | ||
#include "request.h" | ||
#include "response.h" | ||
|
||
void start_server(struct server_config *config) { | ||
int server_fd, client_fd; | ||
struct sockaddr_in server_addr, client_addr; | ||
socklen_t client_addr_size; | ||
|
||
// 在这里添加调试输出 | ||
printf("Starting server with port: %d\n", config->port); | ||
|
||
server_fd = socket(AF_INET, SOCK_STREAM, 0); | ||
if (server_fd == -1) { | ||
perror("socket"); | ||
exit(1); | ||
|
||
} | ||
|
||
memset(&server_addr, 0, sizeof(server_addr)); | ||
server_addr.sin_family = AF_INET; | ||
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
server_addr.sin_port = htons(config->port); | ||
|
||
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { | ||
perror("bind"); | ||
exit(1); | ||
|
||
} | ||
|
||
if (listen(server_fd, 10) == -1) { | ||
perror("listen"); | ||
exit(1); | ||
|
||
} | ||
|
||
while (1) { | ||
client_addr_size = sizeof(client_addr); | ||
client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_addr_size); | ||
if (client_fd == -1) { | ||
perror("accept"); | ||
continue; | ||
} | ||
|
||
// 在这里添加调试输出 | ||
printf("Accepted connection from client: %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); | ||
|
||
handle_request(client_fd, config); | ||
|
||
|
||
|
||
close(client_fd); | ||
} | ||
} |