HttpClient.h 2.6 KB
Newer Older
xiongziliang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//
//  HttpClient.h
//  ZLMediaKit
//
//  Created by xzl on 2017/5/4.
//

#ifndef Http_HttpClient_h
#define Http_HttpClient_h

#include <string.h>
#include <functional>
#include <memory>
#include "Rtsp/Rtsp.h"
#include "Util/util.h"
#include "Network/TcpClient.h"

using namespace std;
using namespace ZL::Util;
using namespace ZL::Network;

namespace ZL {
namespace Http {

xzl committed
25
class HttpArgs : public StrCaseMap
xiongziliang committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
{
public:
    HttpArgs(){}
    virtual ~HttpArgs(){}
    string make() const {
        string ret;
        for(auto &pr : *this){
            ret.append(pr.first);
            ret.append("=");
            ret.append(pr.second);
            ret.append("&");
        }
        if(ret.size()){
            ret.pop_back();
        }
        return ret;
    }
};
    
class HttpClient : public TcpClient
{
public:
    typedef StrCaseMap HttpHeader;
    typedef std::shared_ptr<HttpClient> Ptr;
    HttpClient();
    virtual ~HttpClient();
    virtual void sendRequest(const string &url);
    void clear(){
        _header.clear();
        _body.clear();
        _method.clear();
        _path.clear();
        _recvedResponse.clear();
        _parser.Clear();
    }
    void setMethod(const string &method){
        _method = method;
    }
    void setHeader(const HttpHeader &header){
xzl committed
65
        _header = header;
xiongziliang committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    }
    void addHeader(const string &key,const string &val){
        _header.emplace(key,val);
    }
    void setBody(const string &body){
        _body = body;
    }
    const string &responseStatus(){
        return _parser.Url();
    }
    const HttpHeader &responseHeader(){
        return _parser.getValues();
    }
protected:
    bool _isHttps;
    
    virtual void onResponseHeader(const string &status,const HttpHeader &headers){
        DebugL << status;
    };
    virtual void onResponseBody(const char *buf,size_t size,size_t recvedSize,size_t totalSize){
        DebugL << size << " " <<  recvedSize << " " << totalSize;
    };
    virtual void onResponseCompleted(){
    	DebugL;
    }
    virtual void onRecvBytes(const char *data,int size);
    virtual void onDisconnect(const SockException &ex){}
private:
    virtual void onConnect(const SockException &ex) override;
    virtual void onRecv(const Socket::Buffer::Ptr &pBuf) override;
    virtual void onErr(const SockException &ex) override;
    
    //send
    HttpHeader _header;
    string _body;
    string _method;
    string _path;
    
    //recv
    string _recvedResponse;
    size_t _recvedBodySize;
    size_t _totalBodySize;
    Parser _parser;
    
    string _lastHost;
};








} /* namespace Http */
} /* namespace ZL */








#endif /* Http_HttpClient_h */