web cgi
This commit is contained in:
parent
abda3db746
commit
3a3832eccb
|
|
@ -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
|
||||||
|
|
@ -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"
|
||||||
|
|
@ -0,0 +1,103 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <cjson/cJSON.h>
|
||||||
|
|
||||||
|
// 读取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;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue