diff options
author | HampusM <hampus@hampusmat.com> | 2022-05-23 15:27:49 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-05-23 15:27:49 +0200 |
commit | fb4ffea4161c3ac88eeb43bc886fab63dfdc3891 (patch) | |
tree | 9a268ac6950873f6c80fdec7c95ed52dc5521d09 /minion | |
parent | 0ef43baf0fc5db3a069047ac4f06bf20227f63b5 (diff) |
feat(minion): add response headers
Diffstat (limited to 'minion')
-rw-r--r-- | minion/src/gymnasiearbete.cpp | 19 | ||||
-rw-r--r-- | minion/src/wifi_module.cpp | 27 | ||||
-rw-r--r-- | minion/src/wifi_module.hpp | 4 |
3 files changed, 42 insertions, 8 deletions
diff --git a/minion/src/gymnasiearbete.cpp b/minion/src/gymnasiearbete.cpp index a7f1e31..5c4f725 100644 --- a/minion/src/gymnasiearbete.cpp +++ b/minion/src/gymnasiearbete.cpp @@ -116,6 +116,12 @@ void loop() const auto temperature_sensor_status = temperature_sensor.read_temperature(); + const char *response_headers[] = { "Access-Control-Allow-Origin: *", + "Content-Type: application/json" }; + + const auto response_header_cnt = + sizeof(response_headers) / sizeof(response_headers[0]); + if (temperature_sensor_status != TemperatureSensorStatus::OK) { Serial.print("Error: "); @@ -124,6 +130,8 @@ void loop() wifi_module.send_response( connection, HTTP_RESPONSE_STATUS_INTERNAL_SERVER_ERROR, + response_headers, + response_header_cnt, R"({"error": "Internal server error"})" ); @@ -137,14 +145,19 @@ void loop() snprintf( response_data, RESPONSE_DATA_MAX_LENGTH, - "{ %s: { %s: %u } }", + "{ %s: { %s: %u} }", R"("data")", R"("temperature")", temperature_sensor.temperature() ); - const auto send_response_ok = - wifi_module.send_response(connection, HTTP_RESPONSE_STATUS_OK, response_data); + const auto send_response_ok = wifi_module.send_response( + connection, + HTTP_RESPONSE_STATUS_OK, + response_headers, + response_header_cnt, + response_data + ); if (!send_response_ok) { diff --git a/minion/src/wifi_module.cpp b/minion/src/wifi_module.cpp index d377bd8..6c989f9 100644 --- a/minion/src/wifi_module.cpp +++ b/minion/src/wifi_module.cpp @@ -332,13 +332,23 @@ auto WiFiModule::close_connection(NetworkConnection &connection) noexcept -> boo auto WiFiModule::send_response( const NetworkConnection &connection, size_t status_code, - const char *data + const char **headers, + size_t headers_cnt, + const char *body ) noexcept -> bool { const auto *cmd = "AT+CIPSEND"; + auto tot_headers_lengths = 0U; + + for (size_t index = 0U; index < headers_cnt; index++) + { + tot_headers_lengths += strlen(headers[index]) + 2U; + } + const auto data_length = strlen(RESPONSE_HTTP_VERSION) + 1 + - RESPONSE_STATUS_CODE_LENGTH + 4 + strlen(data); + RESPONSE_STATUS_CODE_LENGTH + 4 + strlen(body) + + tot_headers_lengths; auto command_length = SEND_RESPONSE_COMMAND_BASE_LENGTH + strlen(cmd) + data_length; @@ -363,8 +373,17 @@ auto WiFiModule::send_response( _serial.print(RESPONSE_HTTP_VERSION); _serial.print(" "); _serial.print(status_code); - _serial.print("\r\n\r\n"); - _serial.print(data); + _serial.print("\r\n"); + + // Print headers + for (size_t index = 0U; index < headers_cnt; index++) + { + _serial.print(headers[index]); + _serial.print("\r\n"); + } + + _serial.print("\r\n"); + _serial.print(body); strcpy(response, ""); diff --git a/minion/src/wifi_module.hpp b/minion/src/wifi_module.hpp index 51e90f0..963a1de 100644 --- a/minion/src/wifi_module.hpp +++ b/minion/src/wifi_module.hpp @@ -108,7 +108,9 @@ public: auto send_response( const NetworkConnection &connection, size_t status_code, - const char *data + const char **headers, + size_t headers_cnt, + const char *body ) noexcept -> bool; private: |