diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..593d461 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +CC = gcc +CFLAGS = -Wall -O2 +LDFLAGS = -lcjson + +all: json_service + +json_service: json_service.c + $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) + +install: json_service + sudo cp json_service /usr/lib/cgi-bin/ + sudo chmod +x /usr/lib/cgi-bin/json_service + +clean: + rm -f json_service diff --git a/_configure_thttpd.sh b/_configure_thttpd.sh new file mode 100644 index 0000000..c880e24 --- /dev/null +++ b/_configure_thttpd.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# 创建thttpd配置文件 +sudo tee /etc/thttpd.conf << EOF +port 8080 +user www-data +logfile /var/log/thttpd.log +pidfile /var/run/thttpd.pid +chroot / +docroot /var/www +cgipat /cgi-bin/* +EOF + +# 创建必要的目录 +sudo mkdir -p /var/www/cgi-bin +sudo chown -R www-data:www-data /var/www + +# 重启thttpd服务 +sudo systemctl restart thttpd + +echo "thttpd configured and restarted. Service running on port 8080" diff --git a/json_server.c b/json_server.c new file mode 100644 index 0000000..35cbeb1 --- /dev/null +++ b/json_server.c @@ -0,0 +1,103 @@ +#include +#include +#include +#include + +// 读取HTTP POST数据 +char* read_post_data() { + char* content_length_str = getenv("CONTENT_LENGTH"); + if (!content_length_str) { + return NULL; + } + + int content_length = atoi(content_length_str); + if (content_length <= 0) { + return NULL; + } + + char* data = (char*)malloc(content_length + 1); + if (!data) { + return NULL; + } + + fread(data, 1, content_length, stdin); + data[content_length] = '\0'; + + return data; +} + +// 处理JSON请求并生成响应 +cJSON* process_request(cJSON* request) { + cJSON* response = cJSON_CreateObject(); + + // 检查请求是否包含"action"字段 + cJSON* action = cJSON_GetObjectItemCaseSensitive(request, "action"); + if (cJSON_IsString(action) && (action->valuestring != NULL)) { + cJSON_AddStringToObject(response, "received_action", action->valuestring); + + // 根据不同的action执行不同的操作 + if (strcmp(action->valuestring, "echo") == 0) { + cJSON* message = cJSON_GetObjectItemCaseSensitive(request, "message"); + if (cJSON_IsString(message) && (message->valuestring != NULL)) { + cJSON_AddStringToObject(response, "message", message->valuestring); + cJSON_AddStringToObject(response, "status", "success"); + } else { + cJSON_AddStringToObject(response, "error", "Missing or invalid 'message' field"); + cJSON_AddStringToObject(response, "status", "error"); + } + } else if (strcmp(action->valuestring, "ping") == 0) { + cJSON_AddStringToObject(response, "message", "pong"); + cJSON_AddStringToObject(response, "status", "success"); + } else { + cJSON_AddStringToObject(response, "error", "Unknown action"); + cJSON_AddStringToObject(response, "status", "error"); + } + } else { + cJSON_AddStringToObject(response, "error", "Missing or invalid 'action' field"); + cJSON_AddStringToObject(response, "status", "error"); + } + + return response; +} + +int main() { + // 设置HTTP响应头,指定JSON格式 + printf("Content-Type: application/json\n\n"); + + // 读取POST数据 + char* post_data = read_post_data(); + if (!post_data) { + cJSON* error = cJSON_CreateObject(); + cJSON_AddStringToObject(error, "error", "No data received"); + cJSON_AddStringToObject(error, "status", "error"); + printf("%s", cJSON_Print(error)); + cJSON_Delete(error); + free(post_data); + return 0; + } + + // 解析JSON + cJSON* request = cJSON_Parse(post_data); + free(post_data); + + if (!request) { + const char* error_ptr = cJSON_GetErrorPtr(); + cJSON* error = cJSON_CreateObject(); + cJSON_AddStringToObject(error, "error", "Invalid JSON"); + cJSON_AddStringToObject(error, "details", error_ptr ? error_ptr : "Unknown error"); + cJSON_AddStringToObject(error, "status", "error"); + printf("%s", cJSON_Print(error)); + cJSON_Delete(error); + return 0; + } + + // 处理请求并生成响应 + cJSON* response = process_request(request); + cJSON_Delete(request); + + // 输出响应 + printf("%s", cJSON_Print(response)); + cJSON_Delete(response); + + return 0; +}