messageheader.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 by Marc Boris Duerner, Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef cxxtools_Http_MessageHeader_h
30 #define cxxtools_Http_MessageHeader_h
31 
32 #include <string>
33 #include <cstring>
34 #include <utility>
35 
36 namespace cxxtools
37 {
38 
39 namespace http
40 {
41 
43 {
44  public:
45  static const unsigned MAXHEADERSIZE = 4096;
46 
47  private:
48  char _rawdata[MAXHEADERSIZE]; // key_1\0value_1\0key_2\0value_2\0...key_n\0value_n\0\0
49  unsigned _endOffset;
50  char* eptr() { return _rawdata + _endOffset; }
51  unsigned _httpVersionMajor;
52  unsigned _httpVersionMinor;
53 
54  public:
55  typedef std::pair<const char*, const char*> value_type;
57  : public std::iterator<std::forward_iterator_tag, value_type>
58  {
59  friend class MessageHeader;
60 
61  value_type current_value;
62 
63  void fixup()
64  {
65  if (*current_value.first)
66  current_value.second = current_value.first + std::strlen(current_value.first) + 1;
67  else
68  current_value.first = current_value.second = 0;
69  }
70 
71  void moveForward()
72  {
73  current_value.first = current_value.second + std::strlen(current_value.second) + 1;
74  fixup();
75  }
76 
77  public:
79  : current_value(0, 0)
80  { }
81 
82  explicit const_iterator(const char* p)
83  : current_value(p, p)
84  {
85  fixup();
86  }
87 
88  bool operator== (const const_iterator& it) const
89  { return current_value.first == it.current_value.first; }
90 
91  bool operator!= (const const_iterator& it) const
92  { return current_value.first != it.current_value.first; }
93 
95  {
96  moveForward();
97  return *this;
98  }
99 
101  {
102  const_iterator ret = *this;
103  moveForward();
104  return ret;
105  }
106 
107  const value_type& operator* () const { return current_value; }
108  const value_type* operator-> () const { return &current_value; }
109  };
110 
111 
113  : _endOffset(0),
114  _httpVersionMajor(1),
115  _httpVersionMinor(1)
116  {
117  _rawdata[0] = _rawdata[1] = '\0';
118  }
119 
120  virtual ~MessageHeader() {}
121 
122  void clear();
123 
124  void setHeader(const char* key, const char* value, bool replace = true);
125 
126  void addHeader(const char* key, const char* value)
127  { setHeader(key, value, false); }
128 
129  void removeHeader(const char* key);
130 
131  const char* getHeader(const char* key) const;
132 
133  bool hasHeader(const char* key) const
134  { return getHeader(key) != 0; }
135 
136  bool isHeaderValue(const char* key, const char* value) const;
137 
139  { return const_iterator(_rawdata); }
140 
142  { return const_iterator(); }
143 
144  unsigned httpVersionMajor() const
145  { return _httpVersionMajor; }
146 
147  unsigned httpVersionMinor() const
148  { return _httpVersionMinor; }
149 
150  void httpVersion(unsigned major, unsigned minor)
151  {
152  _httpVersionMajor = major;
153  _httpVersionMinor = minor;
154  }
155 
156  bool chunkedTransferEncoding() const;
157 
158  std::size_t contentLength() const;
159 
160  bool keepAlive() const;
161 
164  static char* htdateCurrent(char* buffer);
165 
166 };
167 
168 } // namespace http
169 
170 } // namespace cxxtools
171 
172 #endif