/* XMRig * Copyright 2010 Jeff Garzik * Copyright 2012-2014 pooler * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2014-2019 heapwolf * Copyright 2018-2019 SChernykh * Copyright 2016-2019 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include "3rdparty/http-parser/http_parser.h" #include "base/net/http/HttpContext.h" #include "base/net/http/HttpResponse.h" namespace xmrig { static const char *kCRLF = "\r\n"; static const char *kTransferEncoding = "Transfer-Encoding"; } // namespace xmrig xmrig::HttpResponse::HttpResponse() : parser(nullptr), statusCode(HTTP_STATUS_OK), body(""), statusAdjective("OK"), // FIXME m_writtenOrEnded(false) { } void xmrig::HttpResponse::writeOrEnd(const std::string &str, bool end) { std::stringstream ss; if (!m_writtenOrEnded) { ss << "HTTP/1.1 " << statusCode << " " << statusAdjective << kCRLF; for (auto &header : headers) { ss << header.first << ": " << header.second << kCRLF; } ss << kCRLF; m_writtenOrEnded = true; } if (headers.count(kTransferEncoding) && headers[kTransferEncoding] == "chunked") { ss << std::hex << str.size() << std::dec << kCRLF << str << kCRLF; if (end) { ss << "0" << kCRLF << kCRLF; } } else { ss << str; } const std::string out = ss.str(); # ifdef _WIN32 uv_buf_t resbuf = uv_buf_init(const_cast(out.c_str()), static_cast(out.size())); # else uv_buf_t resbuf = uv_buf_init(const_cast(out.c_str()), out.size()); # endif HttpContext* context = static_cast(parser->data); uv_try_write(context->stream(), &resbuf, 1); if (end) { if (!uv_is_closing(context->handle())) { uv_close(context->handle(), HttpContext::close); } } }