2020-08-17 17:03:54 +02:00
|
|
|
/* API Response.
|
2017-08-10 12:55:20 +02:00
|
|
|
*
|
2022-03-15 09:18:01 -04:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
2022-07-04 18:20:03 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2017-08-10 12:55:20 +02:00
|
|
|
*/
|
|
|
|
|
2020-08-17 17:03:54 +02:00
|
|
|
#include <villas/api/request.hpp>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/api/response.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/config.hpp>
|
2020-08-17 17:03:54 +02:00
|
|
|
|
|
|
|
using namespace villas::node::api;
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
Response::Response(Session *s, int c, const std::string &ct, const Buffer &b)
|
|
|
|
: session(s), logger(logging.get("api:response")), buffer(b), code(c),
|
|
|
|
contentType(ct),
|
|
|
|
headers{{"Server:", HTTP_USER_AGENT},
|
|
|
|
{"Access-Control-Allow-Origin:", "*"},
|
|
|
|
{"Access-Control-Allow-Methods:", "GET, POST, OPTIONS"},
|
|
|
|
{"Access-Control-Allow-Headers:", "Content-Type"},
|
|
|
|
{"Access-Control-Max-Age:", "86400"}} {}
|
2020-08-17 17:03:54 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int Response::writeBody(struct lws *wsi) {
|
|
|
|
int ret;
|
2020-10-20 22:17:55 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = lws_write(wsi, (unsigned char *)buffer.data(), buffer.size(),
|
|
|
|
LWS_WRITE_HTTP_FINAL);
|
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
2020-10-20 22:17:55 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return 1;
|
2020-08-17 17:03:54 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int Response::writeHeaders(struct lws *wsi) {
|
|
|
|
int ret;
|
|
|
|
uint8_t headerBuffer[2048], *p = headerBuffer,
|
|
|
|
*end = &headerBuffer[sizeof(headerBuffer) - 1];
|
2020-09-30 16:11:13 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
// We need to encode the buffer here for getting the real content length of the response
|
|
|
|
encodeBody();
|
2020-09-30 16:11:13 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = lws_add_http_common_headers(wsi, code, contentType.c_str(),
|
|
|
|
buffer.size(), &p, end);
|
|
|
|
if (ret)
|
|
|
|
return 1;
|
2020-09-30 16:11:13 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
for (auto &hdr : headers) {
|
|
|
|
ret = lws_add_http_header_by_name(
|
|
|
|
wsi, reinterpret_cast<const unsigned char *>(hdr.first.c_str()),
|
|
|
|
reinterpret_cast<const unsigned char *>(hdr.second.c_str()),
|
|
|
|
hdr.second.size(), &p, end);
|
|
|
|
if (ret)
|
|
|
|
return -1;
|
|
|
|
}
|
2020-10-20 22:17:55 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = lws_finalize_write_http_header(wsi, headerBuffer, &p, end);
|
|
|
|
if (ret)
|
|
|
|
return 1;
|
2020-10-20 22:17:55 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
// Do we have a body to send?
|
|
|
|
if (buffer.size() > 0)
|
|
|
|
lws_callback_on_writable(wsi);
|
2020-10-20 22:17:55 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return 0;
|
2020-10-20 22:17:55 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
JsonResponse::~JsonResponse() {
|
|
|
|
if (response)
|
|
|
|
json_decref(response);
|
2020-10-20 22:17:55 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
void JsonResponse::encodeBody() { buffer.encode(response, JSON_INDENT(4)); }
|