Optimize HttpResponse for fixed size API responses.
This commit is contained in:
parent
9daa5874f5
commit
202b74367a
3 changed files with 56 additions and 52 deletions
|
@ -105,7 +105,8 @@ void xmrig::Httpd::onConfigChanged(Config *config, Config *previousConfig)
|
||||||
void xmrig::Httpd::onHttpRequest(const HttpRequest &req)
|
void xmrig::Httpd::onHttpRequest(const HttpRequest &req)
|
||||||
{
|
{
|
||||||
HttpResponse res(req.id());
|
HttpResponse res(req.id());
|
||||||
|
res.setStatus(200);
|
||||||
|
|
||||||
LOG_INFO(GREEN_BOLD_S "OK");
|
LOG_INFO(GREEN_BOLD_S "OK");
|
||||||
res.end();
|
res.end("{}");
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
|
|
||||||
|
|
||||||
#include <uv.h>
|
#include <uv.h>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
#include "3rdparty/http-parser/http_parser.h"
|
#include "3rdparty/http-parser/http_parser.h"
|
||||||
|
@ -34,67 +35,75 @@
|
||||||
|
|
||||||
namespace xmrig {
|
namespace xmrig {
|
||||||
|
|
||||||
static const char *kCRLF = "\r\n";
|
static const char *kCRLF = "\r\n";
|
||||||
static const char *kTransferEncoding = "Transfer-Encoding";
|
|
||||||
|
|
||||||
} // namespace xmrig
|
} // namespace xmrig
|
||||||
|
|
||||||
|
|
||||||
xmrig::HttpResponse::HttpResponse(uint64_t id) :
|
xmrig::HttpResponse::HttpResponse(uint64_t id) :
|
||||||
statusCode(HTTP_STATUS_OK),
|
m_id(id),
|
||||||
body(""),
|
m_statusCode(HTTP_STATUS_OK)
|
||||||
statusAdjective("OK"), // FIXME
|
|
||||||
m_writtenOrEnded(false),
|
|
||||||
m_id(id)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void xmrig::HttpResponse::writeOrEnd(const std::string &str, bool end)
|
bool xmrig::HttpResponse::isAlive() const
|
||||||
{
|
{
|
||||||
HttpContext *context = HttpContext::get(m_id);
|
HttpContext *ctx = HttpContext::get(m_id);
|
||||||
if (!context) {
|
|
||||||
|
return ctx && uv_is_writable(ctx->stream());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void xmrig::HttpResponse::end(const char *data, size_t size)
|
||||||
|
{
|
||||||
|
if (!isAlive()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (data && !size) {
|
||||||
|
size = strlen(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (size) {
|
||||||
|
setHeader("Content-Length", std::to_string(size));
|
||||||
|
}
|
||||||
|
|
||||||
|
setHeader("Connection", "close");
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
ss << "HTTP/1.1 " << statusCode() << " " << http_status_str(static_cast<http_status>(statusCode())) << kCRLF;
|
||||||
|
|
||||||
if (!m_writtenOrEnded) {
|
for (auto &header : m_headers) {
|
||||||
ss << "HTTP/1.1 " << statusCode << " " << statusAdjective << kCRLF;
|
ss << header.first << ": " << header.second << kCRLF;
|
||||||
|
|
||||||
for (auto &header : headers) {
|
|
||||||
ss << header.first << ": " << header.second << kCRLF;
|
|
||||||
}
|
|
||||||
|
|
||||||
ss << kCRLF;
|
|
||||||
m_writtenOrEnded = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (headers.count(kTransferEncoding) && headers[kTransferEncoding] == "chunked") {
|
ss << kCRLF;
|
||||||
ss << std::hex << str.size() << std::dec << kCRLF << str << kCRLF;
|
const std::string header = ss.str();
|
||||||
|
|
||||||
if (end) {
|
uv_buf_t bufs[2];
|
||||||
ss << "0" << kCRLF << kCRLF;
|
bufs[0].base = const_cast<char *>(header.c_str());
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
ss << str;
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string out = ss.str();
|
|
||||||
|
|
||||||
# ifdef _WIN32
|
# ifdef _WIN32
|
||||||
uv_buf_t resbuf = uv_buf_init(const_cast<char *>(out.c_str()), static_cast<unsigned int>(out.size()));
|
bufs[0].len = static_cast<unsigned int>(header.size());
|
||||||
# else
|
# else
|
||||||
uv_buf_t resbuf = uv_buf_init(const_cast<char *>(out.c_str()), out.size());
|
bufs[0].len = header.size();
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
uv_try_write(context->stream(), &resbuf, 1);
|
if (data) {
|
||||||
|
bufs[1].base = const_cast<char *>(data);
|
||||||
|
|
||||||
if (end) {
|
# ifdef _WIN32
|
||||||
if (!uv_is_closing(context->handle())) {
|
bufs[1].len = static_cast<unsigned int>(size);
|
||||||
uv_close(context->handle(), HttpContext::close);
|
# else
|
||||||
}
|
bufs[0].len = size;
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpContext *ctx = HttpContext::get(m_id);
|
||||||
|
uv_try_write(ctx->stream(), bufs, data ? 2 : 1);
|
||||||
|
|
||||||
|
if (!uv_is_closing(ctx->handle())) {
|
||||||
|
uv_close(ctx->handle(), HttpContext::close);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,6 @@
|
||||||
|
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <sstream>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
|
@ -41,22 +40,17 @@ class HttpResponse
|
||||||
public:
|
public:
|
||||||
HttpResponse(uint64_t id);
|
HttpResponse(uint64_t id);
|
||||||
|
|
||||||
inline void end() { writeOrEnd("", true); }
|
inline int statusCode() const { return m_statusCode; }
|
||||||
inline void end(const std::string &str) { writeOrEnd(str, true); }
|
inline void setHeader(const std::string &key, const std::string &value) { m_headers.insert({ key, value }); }
|
||||||
inline void setHeader(const std::string &key, const std::string &value) { headers.insert({ key, value }); }
|
inline void setStatus(int code) { m_statusCode = code; }
|
||||||
inline void setStatus(int code) { statusCode = code; }
|
|
||||||
inline void write(const std::string &str) { writeOrEnd(str, false); }
|
|
||||||
|
|
||||||
int statusCode;
|
bool isAlive() const;
|
||||||
std::map<const std::string, const std::string> headers;
|
void end(const char *data = nullptr, size_t size = 0);
|
||||||
std::string body;
|
|
||||||
std::string statusAdjective; // FIXME
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void writeOrEnd(const std::string &str, bool end);
|
|
||||||
|
|
||||||
bool m_writtenOrEnded = false;
|
|
||||||
const uint64_t m_id;
|
const uint64_t m_id;
|
||||||
|
int m_statusCode;
|
||||||
|
std::map<const std::string, const std::string> m_headers;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue