Merge branch 'master' into with_orm

This commit is contained in:
antao 2018-11-16 10:59:51 +08:00
commit b077d1f308
2 changed files with 48 additions and 20 deletions

View File

@ -1,14 +1,14 @@
/** /**
* *
* @file * HttpClient.h
* @author An Tao *
* @section LICENSE * An Tao
* *
* Copyright 2018, An Tao. All rights reserved. * Copyright 2018, An Tao. All rights reserved.
* Use of this source code is governed by a MIT license * Use of this source code is governed by a MIT license
* that can be found in the License file. * that can be found in the License file.
* *
* @section DESCRIPTION * Drogon
* *
*/ */
#pragma once #pragma once
@ -22,25 +22,55 @@ namespace drogon
{ {
enum class ReqResult enum class ReqResult
{ {
Ok, Ok,
BadResponse, BadResponse,
NetworkFailure, NetworkFailure,
BadServerAddress, BadServerAddress,
Timeout Timeout
}; };
typedef std::function<void(ReqResult, const HttpResponsePtr &response)> HttpReqCallback; typedef std::function<void(ReqResult, const HttpResponsePtr &response)> HttpReqCallback;
class HttpClient; class HttpClient;
typedef std::shared_ptr<HttpClient> HttpClientPtr; typedef std::shared_ptr<HttpClient> HttpClientPtr;
///Async http client class
/**
* HttpClient implementation object uses the HttpAppFramework's event loop,
* so you should call app().run() to make the client work.
* Each HttpClient object establishes a persistent connection with the server.
* If the connection is broken, the client will attempt to reconnect
* when calling the sendRequest method.
*/
class HttpClient : public trantor::NonCopyable class HttpClient : public trantor::NonCopyable
{ {
public: public:
virtual void sendRequest(const HttpRequestPtr &req, const HttpReqCallback &callback) = 0; /// Send request to server
virtual ~HttpClient() {} /**
static HttpClientPtr newHttpClient(const std::string &ip, uint16_t port, bool useSSL = false); * The response from http server will be got in
// static HttpClientPtr newHttpClient(const trantor::InetAddress &addr,bool useSSL=false) ; * the callback function
static HttpClientPtr newHttpClient(const std::string &hostString); */
virtual void sendRequest(const HttpRequestPtr &req, const HttpReqCallback &callback) = 0;
protected: virtual ~HttpClient() {}
HttpClient() = default;
/// Use ip and port to connect to server
/** If useSSL is set to true, the client will
* connect to the server using https
*/
static HttpClientPtr newHttpClient(const std::string &ip, uint16_t port, bool useSSL = false);
/// Use hostString to connect to server
/** Examples for hostString:
* https://www.baidu.com
* http://www.baidu.com
* https://127.0.0.1:8080/
* http://127.0.0.1
* Note:don't add path and parameters in hostString, the request path
* and parameters should be set in
* HttpRequestPtr
*/
static HttpClientPtr newHttpClient(const std::string &hostString);
protected:
HttpClient() = default;
}; };
} // namespace drogon } // namespace drogon

View File

@ -113,10 +113,8 @@ bool HttpClientContext::parseResponse(MsgBuffer *buf)
} }
else else
{ {
// empty line, end of header
// FIXME:
std::string len = _response->getHeader("Content-Length"); std::string len = _response->getHeader("Content-Length");
LOG_INFO << "content len=" << len; //LOG_INFO << "content len=" << len;
if (len != "") if (len != "")
{ {
_response->_left_body_length = atoi(len.c_str()); _response->_left_body_length = atoi(len.c_str());