Parser.h 4.61 KB
Newer Older
xiongziliang committed
1
//
xiongziliang committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Created by xzl on 2019/6/28.
//

#ifndef ZLMEDIAKIT_PARSER_H
#define ZLMEDIAKIT_PARSER_H

#include <map>
#include <string>
#include "Util/util.h"
using namespace std;
using namespace toolkit;

namespace mediakit{

string FindField(const char *buf, const char *start, const char *end, int bufSize = 0);

struct StrCaseCompare {
19 20 21
    bool operator()(const string &__x, const string &__y) const {
        return strcasecmp(__x.data(), __y.data()) < 0;
    }
xiongziliang committed
22 23 24 25 26 27 28 29
};


class StrCaseMap : public multimap<string, string, StrCaseCompare>{
    public:
    typedef multimap<string, string, StrCaseCompare> Super ;
    StrCaseMap() = default;
    ~StrCaseMap() = default;
30 31 32 33

    template <class K>
    string &operator[](K &&k){
        auto it = find(std::forward<K>(k));
xiongziliang committed
34
        if(it == end()){
35
            it = Super::emplace(std::forward<K>(k),"");
xiongziliang committed
36 37 38 39 40 41
        }
        return it->second;
    }

    template <class K,class V>
    void emplace(K &&k , V &&v) {
42
        auto it = find(std::forward<K>(k));
xiongziliang committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        if(it != end()){
            return;
        }
        Super::emplace(std::forward<K>(k),std::forward<V>(v));
    }

    template <class K,class V>
    void emplace_force(K &&k , V &&v) {
        Super::emplace(std::forward<K>(k),std::forward<V>(v));
    }
};

class Parser {
    public:
    Parser() {}

    virtual ~Parser() {}

    void Parse(const char *buf) {
        //解析
        const char *start = buf;
        Clear();
        while (true) {
            auto line = FindField(start, NULL, "\r\n");
            if (line.size() == 0) {
                break;
            }
            if (start == buf) {
                _strMethod = FindField(line.data(), NULL, " ");
                _strFullUrl = FindField(line.data(), " ", " ");
                auto args_pos = _strFullUrl.find('?');
                if (args_pos != string::npos) {
                    _strUrl = _strFullUrl.substr(0, args_pos);
                    _params = _strFullUrl.substr(args_pos + 1);
                    _mapUrlArgs = parseArgs(_params);
                } else {
                    _strUrl = _strFullUrl;
                }
                _strTail = FindField(line.data(), (_strFullUrl + " ").data(), NULL);
            } else {
                auto field = FindField(line.data(), NULL, ": ");
                auto value = FindField(line.data(), ": ", NULL);
                if (field.size() != 0) {
                    _mapHeaders.emplace_force(field,value);
                }
            }
            start = start + line.size() + 2;
            if (strncmp(start, "\r\n", 2) == 0) { //协议解析完毕
                _strContent = FindField(start, "\r\n", NULL);
                break;
            }
        }
    }

    const string &Method() const {
        //rtsp方法
        return _strMethod;
    }

    const string &Url() const {
        //rtsp url
        return _strUrl;
    }

    const string &FullUrl() const {
        //rtsp url with args
        return _strFullUrl;
    }

    const string &Tail() const {
        //RTSP/1.0
        return _strTail;
    }

    const string &operator[](const char *name) const {
        //rtsp field
        auto it = _mapHeaders.find(name);
        if (it == _mapHeaders.end()) {
            return _strNull;
        }
        return it->second;
    }

    const string &Content() const {
        return _strContent;
    }

    void Clear() {
        _strMethod.clear();
        _strUrl.clear();
        _strFullUrl.clear();
        _params.clear();
        _strTail.clear();
        _strContent.clear();
        _mapHeaders.clear();
        _mapUrlArgs.clear();
    }
    const string &Params() const {
        return _params;
    }

    void setUrl(const string &url) {
        this->_strUrl = url;
    }

    void setContent(const string &content) {
        this->_strContent = content;
    }

    StrCaseMap &getValues() const {
        return _mapHeaders;
    }

    StrCaseMap &getUrlArgs() const {
        return _mapUrlArgs;
    }

    static StrCaseMap parseArgs(const string &str, const char *pair_delim = "&", const char *key_delim = "=") {
        StrCaseMap ret;
        auto arg_vec = split(str, pair_delim);
        for (string &key_val : arg_vec) {
            auto key = FindField(key_val.data(), NULL, key_delim);
            auto val = FindField(key_val.data(), key_delim, NULL);
            ret.emplace_force(key,val);
        }
        return ret;
    }

xiongziliang committed
171
private:
xiongziliang committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    string _strMethod;
    string _strUrl;
    string _strTail;
    string _strContent;
    string _strNull;
    string _strFullUrl;
    string _params;
    mutable StrCaseMap _mapHeaders;
    mutable StrCaseMap _mapUrlArgs;
};


}//namespace mediakit

#endif //ZLMEDIAKIT_PARSER_H