]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/io/HttpHeader_p.h
Stablized HTTP access on all platforms. (issue #54, issue #11)
[bamtools.git] / src / api / internal / io / HttpHeader_p.h
1 // ***************************************************************************
2 // HttpHeader_p.h (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 13 January 2012 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides a generic interface for parsing/generating HTTP headers, along
8 // with specialized request & response header types
9 // ***************************************************************************
10
11 #ifndef HTTP_HEADER_P_H
12 #define HTTP_HEADER_P_H
13
14 //  -------------
15 //  W A R N I N G
16 //  -------------
17 //
18 // This file is not part of the BamTools API.  It exists purely as an
19 // implementation detail. This header file may change from version to version
20 // without notice, or even be removed.
21 //
22 // We mean it.
23
24 #include "api/api_global.h"
25 #include <map>
26 #include <string>
27
28 namespace BamTools {
29 namespace Internal {
30
31 class HttpHeader {
32
33     // ctors & dtor
34     public:
35         HttpHeader(void);
36         HttpHeader(const std::string& s);
37         virtual ~HttpHeader(void);
38
39     // HttpHeader interface
40     public:
41
42         // header field=>value access
43         bool ContainsKey(const std::string& key) const;
44         std::string GetValue(const std::string& key);
45         void RemoveField(const std::string& key);
46         void SetField(const std::string& key, const std::string& value);
47
48         // get formatted header string
49         virtual std::string ToString(void) const;
50
51         // query HTTP version used
52         int GetMajorVersion(void) const;
53         int GetMinorVersion(void) const;
54
55         // see if header was parsed OK
56         bool IsValid(void) const;
57
58     // internal methods
59     protected:
60         void Parse(const std::string& s);
61         virtual bool ParseLine(const std::string& line, int lineNumber);
62         void SetValid(bool ok);
63         void SetVersion(int major, int minor);
64
65     // data members
66     private:
67         std::map<std::string, std::string> m_fields;
68
69         bool m_isValid;       // should usually be true, only false if error processing a header line
70         int  m_majorVersion;
71         int  m_minorVersion;
72 };
73
74 class HttpRequestHeader : public HttpHeader {
75
76     // ctor & dtor
77     public:
78         HttpRequestHeader(const std::string& method,      // "GET", "HEAD", ...
79                           const std::string& resource,    // filename
80                           int majorVersion = 1,           // version info
81                           int minorVersion = 1);
82         ~HttpRequestHeader(void);
83
84     // HttpRequestHeader interface
85     public:
86         std::string GetMethod(void) const;
87         std::string GetResource(void) const;
88
89     // HttpHeader implementation
90     public:
91         std::string ToString(void) const;
92     protected:
93         bool ParseLine(const std::string& line, int lineNumber);
94
95     // data members
96     private:
97         std::string m_method;
98         std::string m_resource;
99 };
100
101 class HttpResponseHeader : public HttpHeader {
102
103     // ctor & dtor
104     public:
105         HttpResponseHeader(const int statusCode,                       // 200, 404, etc
106                            const std::string& reason = std::string(),  // 'reason phrase' for code
107                            int majorVersion = 1,                       // version info
108                            int minorVersion = 1);
109         HttpResponseHeader(const std::string& s);
110         ~HttpResponseHeader(void);
111
112     // HttpRequestHeader interface
113     public:
114         std::string GetReason(void) const;
115         int GetStatusCode(void) const;
116
117     // HttpHeader implementation
118     public:
119         std::string ToString(void) const;
120     protected:
121         bool ParseLine(const std::string& line, int lineNumber);
122
123     // data members
124     private:
125         int m_statusCode;
126         std::string m_reason;
127 };
128
129 } // namespace Internal
130 } // namespace BamTools
131
132 #endif // HTTP_HEADER_P_H