]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/io/HttpHeader_p.h
764ff633e0368508b1e92c75fbcfd479cb4b2d8a
[bamtools.git] / src / api / internal / io / HttpHeader_p.h
1 #ifndef HTTP_HEADER_P_H
2 #define HTTP_HEADER_P_H
3
4 #include "api/api_global.h"
5 #include <map>
6 #include <string>
7
8 namespace BamTools {
9 namespace Internal {
10
11 class HttpHeader {
12
13     // ctors & dtor
14     public:
15         HttpHeader(void);
16         HttpHeader(const std::string& s);
17         virtual ~HttpHeader(void);
18
19     // HttpHeader interface
20     public:
21
22         // header field=>value access
23         bool ContainsKey(const std::string& key) const;
24         std::string GetValue(const std::string& key) const;
25         void RemoveField(const std::string& key);
26         void SetField(const std::string& key, const std::string& value);
27
28         // get formatted header string
29         virtual std::string ToString(void) const;
30
31         // query HTTP version used
32         int GetMajorVersion(void) const;
33         int GetMinorVersion(void) const;
34
35         // see if header was parsed OK
36         bool IsValid(void) const;
37
38     // internal methods
39     protected:
40         void Parse(const std::string& s);
41         virtual bool ParseLine(const std::string& line, int lineNumber);
42         void SetValid(bool ok);
43         void SetVersion(int major, int minor);
44
45     // data members
46     private:
47         std::map<std::string, std::string> m_fields;
48
49         bool m_isValid;       // should usually be true, only false if error processing a header line
50         int  m_majorVersion;
51         int  m_minorVersion;
52 };
53
54 class HttpRequestHeader : public HttpHeader {
55
56     // ctor & dtor
57     public:
58         HttpRequestHeader(const std::string& method,      // "GET", "PUT", etc
59                           const std::string& resource,    // filename
60                           int majorVersion = 1,           // version info
61                           int minorVersion = 1);
62         ~HttpRequestHeader(void);
63
64     // HttpRequestHeader interface
65     public:
66         std::string GetMethod(void) const;
67         std::string GetResource(void) const;
68
69     // HttpHeader implementation
70     public:
71         std::string ToString(void) const;
72     protected:
73         bool ParseLine(const std::string& line, int lineNumber);
74
75     // data members
76     private:
77         std::string m_method;
78         std::string m_resource;
79 };
80
81 class HttpResponseHeader : public HttpHeader {
82
83     // ctor & dtor
84     public:
85         HttpResponseHeader(const int statusCode,                       // 200, 404, etc
86                            const std::string& reason = std::string(),  // 'reason phrase' for code
87                            int majorVersion = 1,                       // version info
88                            int minorVersion = 1);
89         HttpResponseHeader(const std::string& s);
90         ~HttpResponseHeader(void);
91
92     // HttpRequestHeader interface
93     public:
94         std::string GetReason(void) const;
95         int GetStatusCode(void) const;
96
97     // HttpHeader implementation
98     public:
99         std::string ToString(void) const;
100     protected:
101         bool ParseLine(const std::string& line, int lineNumber);
102
103     // data members
104     private:
105         int m_statusCode;
106         std::string m_reason;
107 };
108
109 } // namespace Internal
110 } // namespace BamTools
111
112 #endif // HTTP_HEADER_P_H