|
|
@@ -140,24 +140,35 @@ static void handle_api_settings(struct mg_connection *c, struct mg_http_message
|
|
|
}
|
|
|
|
|
|
printf("[WEB] Settings request method: '%s'\n", method);
|
|
|
+ printf("[WEB] URI: %.*s\n", (int)hm->uri.len, hm->uri.buf ? hm->uri.buf : "");
|
|
|
+ printf("[WEB] Method: %.*s\n", (int)hm->method.len, hm->method.buf ? hm->method.buf : "");
|
|
|
+ printf("[WEB] Body ptr: %p, len: %zu\n", (void*)hm->body.buf, hm->body.len);
|
|
|
+ printf("[WEB] Message ptr: %p, len: %zu\n", (void*)hm->message.buf, hm->message.len);
|
|
|
+
|
|
|
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 - try body field first, then fall back to raw message */
|
|
|
+ /* Parse JSON body - try multiple methods */
|
|
|
char body_buf[2048];
|
|
|
size_t body_len = 0;
|
|
|
|
|
|
+ /* Method 1: Use hm->body directly */
|
|
|
if (hm->body.buf && hm->body.len > 0) {
|
|
|
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';
|
|
|
- } else {
|
|
|
- /* Fallback: extract body from raw message after headers */
|
|
|
- /* Find the end of headers (double CRLF) */
|
|
|
+ printf("[WEB] Body from hm->body: %s\n", body_buf);
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Method 2: Extract from raw message if body is empty */
|
|
|
+ if (body_len == 0 && hm->message.buf && hm->message.len > 0) {
|
|
|
const char *raw = hm->message.buf;
|
|
|
size_t raw_len = hm->message.len;
|
|
|
+ printf("[WEB] Raw message (%zu bytes): %.*s\n", raw_len, (int)(raw_len < 200 ? raw_len : 200), raw);
|
|
|
+
|
|
|
+ /* Find the end of headers (double CRLF) */
|
|
|
const char *body_start = strstr(raw, "\r\n\r\n");
|
|
|
if (body_start) {
|
|
|
body_start += 4; /* Skip \r\n\r\n */
|
|
|
@@ -165,13 +176,32 @@ static void handle_api_settings(struct mg_connection *c, struct mg_http_message
|
|
|
if (body_len > 0 && body_len < sizeof(body_buf)) {
|
|
|
memcpy(body_buf, body_start, body_len);
|
|
|
body_buf[body_len] = '\0';
|
|
|
+ printf("[WEB] Body from raw message: %s\n", body_buf);
|
|
|
+ } else {
|
|
|
+ body_len = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Method 3: Try to find body after first \n\n (LF only) */
|
|
|
+ if (body_len == 0 && hm->message.buf && hm->message.len > 0) {
|
|
|
+ const char *raw = hm->message.buf;
|
|
|
+ size_t raw_len = hm->message.len;
|
|
|
+ const char *body_start = strstr(raw, "\n\n");
|
|
|
+ if (body_start) {
|
|
|
+ body_start += 2; /* Skip \n\n */
|
|
|
+ body_len = raw_len - (body_start - raw);
|
|
|
+ if (body_len > 0 && body_len < sizeof(body_buf)) {
|
|
|
+ memcpy(body_buf, body_start, body_len);
|
|
|
+ body_buf[body_len] = '\0';
|
|
|
+ printf("[WEB] Body from LF-separated message: %s\n", body_buf);
|
|
|
} else {
|
|
|
body_len = 0;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- printf("[WEB] Settings POST body (%zu bytes): %s\n", body_len, body_len > 0 ? body_buf : "(empty)");
|
|
|
+ printf("[WEB] Final body_len: %zu\n", body_len);
|
|
|
|
|
|
if (body_len == 0) {
|
|
|
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n",
|