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 <cxxtools/http/api.h>
33 #include <string>
34 #include <cstring>
35 #include <utility>
36 
37 namespace cxxtools
38 {
39 
40 namespace http
41 {
42 
44 {
45  public:
46  static const unsigned MAXHEADERSIZE = 4096;
47 
48  private:
49  char _rawdata[MAXHEADERSIZE]; // key_1\0value_1\0key_2\0value_2\0...key_n\0value_n\0\0
50  unsigned _endOffset;
51  char* eptr() { return _rawdata + _endOffset; }
52  unsigned _httpVersionMajor;
53  unsigned _httpVersionMinor;
54 
55  public:
56  typedef std::pair<const char*, const char*> value_type;
58  : public std::iterator<std::forward_iterator_tag, value_type>
59  {
60  friend class MessageHeader;
61 
62  value_type current_value;
63 
64  void fixup()
65  {
66  if (*current_value.first)
67  current_value.second = current_value.first + std::strlen(current_value.first) + 1;
68  else
69  current_value.first = current_value.second = 0;
70  }
71 
72  void moveForward()
73  {
74  current_value.first = current_value.second + std::strlen(current_value.second) + 1;
75  fixup();
76  }
77 
78  public:
80  : current_value(0, 0)
81  { }
82 
83  explicit const_iterator(const char* p)
84  : current_value(p, p)
85  {
86  fixup();
87  }
88 
89  bool operator== (const const_iterator& it) const
90  { return current_value.first == it.current_value.first; }
91 
92  bool operator!= (const const_iterator& it) const
93  { return current_value.first != it.current_value.first; }
94 
95  const_iterator& operator++()
96  {
97  moveForward();
98  return *this;
99  }
100 
101  const_iterator operator++(int)
102  {
103  const_iterator ret = *this;
104  moveForward();
105  return ret;
106  }
107 
108  const value_type& operator* () const { return current_value; }
109  const value_type* operator-> () const { return &current_value; }
110  };
111 
112 
114  : _endOffset(0),
115  _httpVersionMajor(1),
116  _httpVersionMinor(1)
117  {
118  _rawdata[0] = _rawdata[1] = '\0';
119  }
120 
121  virtual ~MessageHeader() {}
122 
123  void clear();
124 
125  void setHeader(const char* key, const char* value, bool replace = true);
126 
127  void addHeader(const char* key, const char* value)
128  { setHeader(key, value, false); }
129 
130  void removeHeader(const char* key);
131 
132  const char* getHeader(const char* key) const;
133 
134  bool hasHeader(const char* key) const
135  { return getHeader(key) != 0; }
136 
137  bool isHeaderValue(const char* key, const char* value) const;
138 
139  const_iterator begin() const
140  { return const_iterator(_rawdata); }
141 
142  const_iterator end() const
143  { return const_iterator(); }
144 
145  unsigned httpVersionMajor() const
146  { return _httpVersionMajor; }
147 
148  unsigned httpVersionMinor() const
149  { return _httpVersionMinor; }
150 
151  void httpVersion(unsigned major, unsigned minor)
152  {
153  _httpVersionMajor = major;
154  _httpVersionMinor = minor;
155  }
156 
157  bool chunkedTransferEncoding() const;
158 
159  std::size_t contentLength() const;
160 
161  bool keepAlive() const;
162 
165  static char* htdateCurrent(char* buffer);
166 
167 };
168 
169 } // namespace http
170 
171 } // namespace cxxtools
172 
173 #endif