projectC/json_server.c

104 lines
3.3 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}