Parser.h 2.84 KB
Newer Older
xiongziliang committed
1 2 3 4 5 6 7 8 9
/*
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
 */
xiongziliang committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

#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 {
25 26 27
    bool operator()(const string &__x, const string &__y) const {
        return strcasecmp(__x.data(), __y.data()) < 0;
    }
xiongziliang committed
28 29 30 31 32 33 34 35
};


class StrCaseMap : public multimap<string, string, StrCaseCompare>{
    public:
    typedef multimap<string, string, StrCaseCompare> Super ;
    StrCaseMap() = default;
    ~StrCaseMap() = default;
36

xiongziliang committed
37 38
    string &operator[](const string &k){
        auto it = find(k);
xiongziliang committed
39
        if(it == end()){
xiongziliang committed
40
            it = Super::emplace(k,"");
xiongziliang committed
41 42 43 44
        }
        return it->second;
    }

xiongziliang committed
45 46 47
    template <typename V>
    void emplace(const string &k, V &&v) {
        auto it = find(k);
xiongziliang committed
48 49 50
        if(it != end()){
            return;
        }
xiongziliang committed
51
        Super::emplace(k,std::forward<V>(v));
xiongziliang committed
52 53
    }

xiongziliang committed
54 55 56
    template <typename V>
    void emplace_force(const string k , V &&v) {
        Super::emplace(k,std::forward<V>(v));
xiongziliang committed
57 58 59
    }
};

xiongziliang committed
60
//rtsp/http/sip解析类
xiongziliang committed
61
class Parser {
xiongziliang committed
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
public:
    Parser();
    ~Parser();
    //解析信令
    void Parse(const char *buf);
    //获取命令字
    const string &Method() const;
    //获取中间url,不包含?后面的参数
    const string &Url() const;
    //获取中间url,包含?后面的参数
    const string &FullUrl() const;
    //获取命令协议名
    const string &Tail() const;
    //根据header key名,获取请求header value值
    const string &operator[](const char *name) const;
    //获取http body或sdp
    const string &Content() const;
    //清空,为了重用
    void Clear();
    //获取?后面的参数
    const string &Params() const;
    //重新设置url
    void setUrl(const string &url);
    //重新设置content
    void setContent(const string &content);
    //获取header列表
    StrCaseMap &getHeader() const;
    //获取url参数列表
    StrCaseMap &getUrlArgs() const;
    //解析?后面的参数
    static StrCaseMap parseArgs(const string &str, const char *pair_delim = "&", const char *key_delim = "=");
xiongziliang committed
93
private:
xiongziliang committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    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