From 374eb4e9a31d5b8d6dabca65f8ffca43146cd627 Mon Sep 17 00:00:00 2001 From: Mikhail Burakov Date: Fri, 6 Jan 2023 20:53:34 +0100 Subject: Forward http posts as mqtt publish messages --- main.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'main.c') diff --git a/main.c b/main.c index 5ed9f2a..fbb7276 100644 --- a/main.c +++ b/main.c @@ -180,7 +180,18 @@ static bool ServeHttpGet(int fd, const char* target) { static bool ServeHttpPost(int fd, const char* target, const void* content, size_t content_length) { - return false; + const char* topic = target + 1; + size_t topic_size = strlen(topic); + if (topic_size > UINT16_MAX) { + LOGW("Topic is too long to fit into mqtt message"); + return SendHttpReply(fd, "414 URI Too Long", NULL, NULL, 0); + } + if (!MqttPublish(g_service.mqtt, topic, (uint16_t)topic_size, content, + content_length)) { + LOGW("Failed to publish mqtt message (%s)", strerror(errno)); + return SendHttpReply(fd, "500 Internal Server Error", NULL, NULL, 0); + } + return SendHttpReply(fd, "200 OK", NULL, NULL, 0); } static bool DispatchHttpRequest(int fd, const char* method, const char* target, -- cgit v1.2.3