Fixed, big HTTP responses was truncated.

This commit is contained in:
XMRig 2019-04-02 17:46:26 +07:00
parent 4aa7f01f13
commit 85ee55f309
3 changed files with 97 additions and 25 deletions

View file

@ -33,6 +33,7 @@ set(HEADERS_BASE
src/base/net/tools/RecvBuf.h src/base/net/tools/RecvBuf.h
src/base/net/tools/Storage.h src/base/net/tools/Storage.h
src/base/tools/Arguments.h src/base/tools/Arguments.h
src/base/tools/Baton.h
src/base/tools/Buffer.h src/base/tools/Buffer.h
src/base/tools/Chrono.h src/base/tools/Chrono.h
src/base/tools/Handle.h src/base/tools/Handle.h

View file

@ -33,6 +33,7 @@
#include "base/io/log/Log.h" #include "base/io/log/Log.h"
#include "base/net/http/HttpContext.h" #include "base/net/http/HttpContext.h"
#include "base/net/http/HttpResponse.h" #include "base/net/http/HttpResponse.h"
#include "base/tools/Baton.h"
namespace xmrig { namespace xmrig {
@ -40,6 +41,52 @@ namespace xmrig {
static const char *kCRLF = "\r\n"; static const char *kCRLF = "\r\n";
static const char *kUserAgent = "user-agent"; static const char *kUserAgent = "user-agent";
class WriteBaton : public Baton<uv_write_t>
{
public:
inline WriteBaton(const std::stringstream &ss, const char *data, size_t size, HttpContext *ctx) :
m_ctx(ctx),
m_header(ss.str())
{
req.data = this;
bufs[0].len = m_header.size();
bufs[0].base = const_cast<char *>(m_header.c_str());
if (data) {
bufs[1].len = size;
bufs[1].base = new char[size];
memcpy(bufs[1].base, data, size);
}
else {
bufs[1].base = nullptr;
bufs[1].len = 0;
}
}
inline ~WriteBaton()
{
if (count() == 2) {
delete [] bufs[1].base;
}
m_ctx->close();
}
inline size_t count() const { return bufs[1].base == nullptr ? 1 : 2; }
inline size_t size() const { return bufs[0].len + bufs[1].len; }
inline static void onWrite(uv_write_t *req, int) { delete reinterpret_cast<WriteBaton *>(req->data); }
uv_buf_t bufs[2];
private:
HttpContext *m_ctx;
std::string m_header;
};
} // namespace xmrig } // namespace xmrig
@ -82,28 +129,9 @@ void xmrig::HttpResponse::end(const char *data, size_t size)
} }
ss << kCRLF; ss << kCRLF;
const std::string header = ss.str();
uv_buf_t bufs[2]; HttpContext *ctx = HttpContext::get(m_id);
bufs[0].base = const_cast<char *>(header.c_str()); WriteBaton *baton = new WriteBaton(ss, data, size, ctx);
# ifdef _WIN32
bufs[0].len = static_cast<unsigned int>(header.size());
# else
bufs[0].len = header.size();
# endif
if (data) {
bufs[1].base = const_cast<char *>(data);
# ifdef _WIN32
bufs[1].len = static_cast<unsigned int>(size);
# else
bufs[1].len = size;
# endif
}
HttpContext *ctx = HttpContext::get(m_id);
# ifndef APP_DEBUG # ifndef APP_DEBUG
if (statusCode() >= 400) if (statusCode() >= 400)
@ -128,12 +156,10 @@ void xmrig::HttpResponse::end(const char *data, size_t size)
ctx->url.c_str(), ctx->url.c_str(),
err ? 31 : 32, err ? 31 : 32,
statusCode(), statusCode(),
header.size() + size, baton->size(),
ctx->headers.count(kUserAgent) ? ctx->headers.at(kUserAgent).c_str() : nullptr ctx->headers.count(kUserAgent) ? ctx->headers.at(kUserAgent).c_str() : nullptr
); );
} }
uv_try_write(ctx->stream(), bufs, data ? 2 : 1); uv_write(&baton->req, ctx->stream(), baton->bufs, baton->count(), WriteBaton::onWrite);
ctx->close();
} }

45
src/base/tools/Baton.h Normal file
View file

@ -0,0 +1,45 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_BATON_H
#define XMRIG_BATON_H
namespace xmrig {
template<typename T>
class Baton
{
public:
inline Baton() { req.data = this; }
T req;
};
} /* namespace xmrig */
#endif /* XMRIG_BATON_H */