|
|
@@ -109,8 +109,10 @@ static void handle_api_data(struct mg_connection *c, struct mg_http_message *hm)
|
|
|
/* ---- Handle POST /api/settings - Write device configuration ---- */
|
|
|
static void handle_api_settings(struct mg_connection *c, struct mg_http_message *hm)
|
|
|
{
|
|
|
- /* Only accept POST */
|
|
|
- if (mg_match(hm->method, mg_str("GET"), NULL)) {
|
|
|
+ /* Check method - support GET, POST, OPTIONS */
|
|
|
+ const char *method = hm->method.buf ? hm->method.buf : "";
|
|
|
+
|
|
|
+ if (strncmp(method, "GET", 3) == 0) {
|
|
|
/* GET returns current settings info */
|
|
|
cJSON *root = cJSON_CreateObject();
|
|
|
cJSON_AddStringToObject(root, "method", "GET");
|
|
|
@@ -124,26 +126,44 @@ static void handle_api_settings(struct mg_connection *c, struct mg_http_message
|
|
|
cJSON_AddItemToArray(writable, cJSON_CreateString("charge_mode (0-1): 0=MPPT 1=DCDC"));
|
|
|
cJSON_AddItemToObject(root, "writable_registers", writable);
|
|
|
char *json_str = cJSON_PrintUnformatted(root);
|
|
|
- mg_http_reply(c, 200, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "%s", json_str);
|
|
|
+ mg_http_reply(c, 200, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: GET, POST, OPTIONS\r\nAccess-Control-Allow-Headers: Content-Type\r\n", "%s", json_str);
|
|
|
free(json_str);
|
|
|
cJSON_Delete(root);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- if (!mg_match(hm->method, mg_str("POST"), NULL)) {
|
|
|
- mg_http_reply(c, 405, "Content-Type: application/json\r\n", "{\"error\":\"Method not allowed. Use POST.\"}");
|
|
|
+ if (strncmp(method, "OPTIONS", 7) == 0) {
|
|
|
+ /* CORS preflight */
|
|
|
+ printf("[WEB] OPTIONS request received for %s\n", hm->uri.buf ? hm->uri.buf : "unknown");
|
|
|
+ mg_http_reply(c, 204, "Access-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: GET, POST, OPTIONS\r\nAccess-Control-Allow-Headers: Content-Type\r\n", "");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ printf("[WEB] Settings request method: '%s'\n", method);
|
|
|
+ if (strncmp(method, "POST", 4) != 0) {
|
|
|
+ mg_http_reply(c, 405, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{\"error\":\"Method not allowed. Use POST.\"}");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
/* Parse JSON body */
|
|
|
+ if (!hm->body.buf || hm->body.len == 0) {
|
|
|
+ mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n",
|
|
|
+ "{\"error\":\"Empty request body\"}");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
char body_buf[2048];
|
|
|
size_t body_len = (hm->body.len < sizeof(body_buf) - 1) ? hm->body.len : sizeof(body_buf) - 1;
|
|
|
memcpy(body_buf, hm->body.buf, body_len);
|
|
|
body_buf[body_len] = '\0';
|
|
|
|
|
|
+ printf("[WEB] Settings POST body (%zu bytes): %s\n", body_len, body_buf);
|
|
|
+
|
|
|
cJSON *req = cJSON_Parse(body_buf);
|
|
|
if (!req) {
|
|
|
- mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{\"error\":\"Invalid JSON body\"}");
|
|
|
+ printf("[WEB] Settings JSON parse failed for body: %s\n", body_buf);
|
|
|
+ mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n",
|
|
|
+ "{\"error\":\"Invalid JSON body\"}");
|
|
|
return;
|
|
|
}
|
|
|
|