From 0f6d1135ebcca7cf8eaba58d7af69f376aa96afe Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Thu, 12 Aug 2021 21:02:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96http=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 30 ++++++++++++++++-------------- src/Http/HttpBody.cpp | 12 ++++++++++++ src/Http/HttpBody.h | 21 +++++++++++++++++++-- src/Http/HttpFileManager.cpp | 4 ++++ src/Http/HttpFileManager.h | 1 + 5 files changed, 52 insertions(+), 16 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index ceca6b0c..ebebdb81 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -217,10 +217,7 @@ static inline void addHttpListener(){ consumed = true; if(api_debug){ - auto newInvoker = [invoker, parser](int code, - const HttpSession::KeyValue &headerOut, - const HttpBody::Ptr &body) { - + auto newInvoker = [invoker, parser](int code, const HttpSession::KeyValue &headerOut, const HttpBody::Ptr &body) { //body默认为空 ssize_t size = 0; if (body && body->remainSize()) { @@ -228,18 +225,23 @@ static inline void addHttpListener(){ size = body->remainSize(); } + LogContextCapturer log(getLogger(), LDebug, __FILE__, "http api debug", __LINE__); + log << "\r\n# request:\r\n" << parser.Method() << " " << parser.FullUrl() << "\r\n"; + log << "# header:\r\n"; + + for (auto &pr : parser.getHeader()) { + log << pr.first << " : " << pr.second << "\r\n"; + } + + auto &content = parser.Content(); + log << "# content:\r\n" << (content.size() > 4 * 1024 ? content.substr(0, 4 * 1024) : content) << "\r\n"; + if (size && size < 4 * 1024) { - string contentOut = body->readData(size)->toString(); - DebugL << "\r\n# request:\r\n" << parser.Method() << " " << parser.FullUrl() << "\r\n" - << "# content:\r\n" << parser.Content() << "\r\n" - << "# response:\r\n" - << contentOut << "\r\n"; - invoker(code, headerOut, contentOut); + auto response = body->readData(size); + log << "# response:\r\n" << response->data() << "\r\n"; + invoker(code, headerOut, response); } else { - DebugL << "\r\n# request:\r\n" << parser.Method() << " " << parser.FullUrl() << "\r\n" - << "# content:\r\n" << parser.Content() << "\r\n" - << "# response size:" - << size << "\r\n"; + log << "# response size:" << size << "\r\n"; invoker(code, headerOut, body); } }; diff --git a/src/Http/HttpBody.cpp b/src/Http/HttpBody.cpp index 348c2108..a360645d 100644 --- a/src/Http/HttpBody.cpp +++ b/src/Http/HttpBody.cpp @@ -247,4 +247,16 @@ string HttpMultiFormBody::multiFormBodyPrefix(const HttpArgs &args,const string return std::move(body); } +HttpBufferBody::HttpBufferBody(Buffer::Ptr buffer) { + _buffer = std::move(buffer); +} + +ssize_t HttpBufferBody::remainSize() { + return _buffer ? _buffer->size() : 0; +} + +Buffer::Ptr HttpBufferBody::readData(size_t size) { + return Buffer::Ptr(std::move(_buffer)); +} + }//namespace mediakit diff --git a/src/Http/HttpBody.h b/src/Http/HttpBody.h index 6cdd3473..31d7ef5b 100644 --- a/src/Http/HttpBody.h +++ b/src/Http/HttpBody.h @@ -69,7 +69,8 @@ class HttpStringBody : public HttpBody{ public: typedef std::shared_ptr Ptr; HttpStringBody(const string &str); - virtual ~HttpStringBody(){} + ~HttpStringBody() override = default; + ssize_t remainSize() override; Buffer::Ptr readData(size_t size) override ; @@ -78,6 +79,22 @@ private: mutable string _str; }; +/** + * Buffer类型的content + */ +class HttpBufferBody : public HttpBody{ +public: + typedef std::shared_ptr Ptr; + HttpBufferBody(Buffer::Ptr buffer); + ~HttpBufferBody() override = default; + + ssize_t remainSize() override; + Buffer::Ptr readData(size_t size) override; + +private: + Buffer::Ptr _buffer; +}; + /** * 文件类型的content */ @@ -93,7 +110,7 @@ public: */ HttpFileBody(const std::shared_ptr &fp,size_t offset,size_t max_size); HttpFileBody(const string &file_path); - ~HttpFileBody(){}; + ~HttpFileBody() override = default; ssize_t remainSize() override ; Buffer::Ptr readData(size_t size) override; diff --git a/src/Http/HttpFileManager.cpp b/src/Http/HttpFileManager.cpp index 2d156e44..d00f194f 100644 --- a/src/Http/HttpFileManager.cpp +++ b/src/Http/HttpFileManager.cpp @@ -514,6 +514,10 @@ void HttpFileManager::onAccessPath(TcpSession &sender, Parser &parser, const Htt ////////////////////////////////////HttpResponseInvokerImp////////////////////////////////////// +void HttpResponseInvokerImp::operator()(int code, const StrCaseMap &headerOut, const Buffer::Ptr &body) const { + return operator()(code, headerOut, std::make_shared(body)); +} + void HttpResponseInvokerImp::operator()(int code, const StrCaseMap &headerOut, const HttpBody::Ptr &body) const{ if (_lambad) { _lambad(code, headerOut, body); diff --git a/src/Http/HttpFileManager.h b/src/Http/HttpFileManager.h index f88d29ae..07993046 100644 --- a/src/Http/HttpFileManager.h +++ b/src/Http/HttpFileManager.h @@ -31,6 +31,7 @@ public: HttpResponseInvokerImp(const HttpResponseInvokerLambda0 &lambda); HttpResponseInvokerImp(const HttpResponseInvokerLambda1 &lambda); + void operator()(int code, const StrCaseMap &headerOut, const Buffer::Ptr &body) const; void operator()(int code, const StrCaseMap &headerOut, const HttpBody::Ptr &body) const; void operator()(int code, const StrCaseMap &headerOut, const string &body) const;