ESA JPIP server  0.1
header.h
Go to the documentation of this file.
1 #ifndef _HTTP_HEADER_H_
2 #define _HTTP_HEADER_H_
3 
4 
5 #include <string.h>
6 #include <iostream>
7 #include <assert.h>
8 #include "protocol.h"
9 
10 
11 namespace http
12 {
13 
21  class HeaderName
22  {
23  public:
24  static const char UNDEFINED[];
25  static const char CONTENT_TYPE[];
26  static const char CACHE_CONTROL[];
27  static const char CONTENT_LENGTH[];
28  static const char TRANSFER_ENCODING[];
29  };
30 
31 
39  template<const char *NAME> class HeaderBase
40  {
41  private:
42  string value;
43 
44  public:
49  {
50  }
51 
55  HeaderBase(const string& value)
56  {
57  this->value = value;
58  }
59 
60  friend ostream& operator << (ostream &out, const HeaderBase &header)
61  {
62  return out << NAME << ": " << header.value << Protocol::CRLF;
63  }
64 
65  friend istream& operator >> (istream &in, HeaderBase &header)
66  {
67  assert(0);
68 
69  in.setstate(istream::failbit);
70  return in;
71  }
72 
77  static const char *name()
78  {
79  return NAME;
80  }
81  };
82 
83 
92  template<> class HeaderBase<HeaderName::UNDEFINED>
93  {
94  public:
95  string name;
96  string value;
97 
98 
103  {
104  }
105 
111  HeaderBase(const string& name, const string& value)
112  {
113  this->name = name;
114  this->value = value;
115  }
116 
117  friend ostream& operator << (ostream &out, const HeaderBase &header)
118  {
119  return out << header.name << ": " << header.value << Protocol::CRLF;
120  }
121 
122  friend istream& operator >> (istream &in, HeaderBase &header)
123  {
124  string line;
125 
126  if(getline(in, line)) {
127  size_t line_size = line.size();
128  if(line_size <= 0) in.setstate(istream::eofbit);
129  else if((line[0] == '\r') || (line[0] == '\n')) in.setstate(istream::eofbit);
130  else {
131  size_t pos = line.find(':');
132 
133  if(pos == string::npos) in.setstate(istream::failbit);
134  else {
135  header.name = line.substr(0, pos);
136 
137  if((pos += 2) >= line_size) in.setstate(istream::failbit);
138  else header.value = line.substr(pos, line_size - pos - 1);
139  }
140  }
141  }
142 
143  return in;
144  }
145  };
146 
147 
154  class Header :public HeaderBase<HeaderName::UNDEFINED>
155  {
156  public:
161 
166 
171 
176 
177 
182  {
183  }
184 
190  Header(const string& name, const string& value) :HeaderBase<HeaderName::UNDEFINED>(name, value)
191  {
192  }
193 
197  template<const char *NAME> friend bool operator==(const Header& a, const HeaderBase<NAME>& b)
198  {
199  return (strcasecmp(a.name.c_str(), b.name()) == 0);
200  }
201  };
202 
203 }
204 
205 #endif /* _HTTP_HEADER_H_ */
HeaderBase()
Empty constructor.
Definition: header.h:48
static const char CACHE_CONTROL[]
The header Cache-Control
Definition: header.h:26
HeaderBase< HeaderName::CONTENT_LENGTH > ContentLength
Predefined "Content-Length" header.
Definition: header.h:170
Container for the strings associated to the most common HTTP headers, used for the specialization of ...
Definition: header.h:21
HeaderBase< HeaderName::CACHE_CONTROL > CacheControl
Predefined "Cache-Control" header.
Definition: header.h:165
Header()
Empty constructor.
Definition: header.h:181
Template class used to identify a HTTP header.
Definition: header.h:39
static const char * name()
Returns the name of the header, used in the specialization of the class.
Definition: header.h:77
HeaderBase(const string &value)
Initializes the header value.
Definition: header.h:55
Contains the definition of a set of classes for working easily with the protocol HTTP.
Definition: header.cc:4
Class used to handle a HTTP header.
Definition: header.h:154
static const char UNDEFINED[]
No header name defined.
Definition: header.h:24
static const char TRANSFER_ENCODING[]
The header Transfer-Encoding
Definition: header.h:28
string value
String value of the header.
Definition: header.h:42
HeaderBase< HeaderName::CONTENT_TYPE > ContentType
Predefined "Content-Type".
Definition: header.h:160
static const char CONTENT_LENGTH[]
The header Content-Length
Definition: header.h:27
HeaderBase(const string &name, const string &value)
Initializes the header content (name and value).
Definition: header.h:111
HeaderBase()
Empty constructor.
Definition: header.h:102
Header(const string &name, const string &value)
Initializes the header content (name and value).
Definition: header.h:190
HeaderBase< HeaderName::TRANSFER_ENCODING > TransferEncoding
Predefined "Transfer-Encoding" header.
Definition: header.h:175
static const char CONTENT_TYPE[]
The header Content-Type
Definition: header.h:25
friend bool operator==(const Header &a, const HeaderBase< NAME > &b)
Returns true if the names of the two headers are equal.
Definition: header.h:197
string value
Header value.
Definition: header.h:96
string name
Header name.
Definition: header.h:95
istream & operator>>(istream &in, Request &request)
Definition: request.cc:51
static const char CRLF[]
String with the characters 13 (CR) and 10 (LF), the line separator used in the HTTP protocol...
Definition: protocol.h:31
ostream & operator<<(ostream &out, const Request &request)
Definition: request.cc:65