00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef TNT_MESSAGEHEADER_H
00031 #define TNT_MESSAGEHEADER_H
00032
00033 #include <istream>
00034 #include <string>
00035 #include <cstring>
00036 #include <utility>
00037
00038 namespace tnt
00039 {
00041 class Messageheader
00042 {
00043 public:
00044 static const unsigned MAXHEADERSIZE = 4096;
00045
00046 private:
00047 char rawdata[MAXHEADERSIZE];
00048 unsigned endOffset;
00049 char* getEnd() { return rawdata + endOffset; }
00050
00051 public:
00052 class Parser;
00053 friend class Parser;
00054
00055 typedef std::pair<const char*, const char*> value_type;
00056
00057 class const_iterator
00058 : public std::iterator<std::forward_iterator_tag, value_type>
00059 {
00060 friend class Messageheader;
00061
00062 value_type current_value;
00063
00064 void fixup()
00065 {
00066 if (*current_value.first)
00067 current_value.second = current_value.first + std::strlen(current_value.first) + 1;
00068 else
00069 current_value.first = current_value.second = 0;
00070 }
00071
00072 void moveForward()
00073 {
00074 current_value.first = current_value.second + std::strlen(current_value.second) + 1;
00075 fixup();
00076 }
00077
00078 public:
00079 const_iterator()
00080 : current_value(static_cast<const char*>(0), static_cast<const char*>(0))
00081 { }
00082
00083 explicit const_iterator(const char* p)
00084 : current_value(p, p)
00085 {
00086 fixup();
00087 }
00088
00089 bool operator== (const const_iterator& it) const
00090 { return current_value.first == it.current_value.first; }
00091
00092 bool operator!= (const const_iterator& it) const
00093 { return current_value.first != it.current_value.first; }
00094
00095 const_iterator& operator++()
00096 {
00097 moveForward();
00098 return *this;
00099 }
00100
00101 const_iterator operator++(int)
00102 {
00103 const_iterator ret = *this;
00104 moveForward();
00105 return ret;
00106 }
00107
00108 const value_type& operator* () const { return current_value; }
00109 const value_type* operator-> () const { return ¤t_value; }
00110 };
00111
00112 protected:
00113 enum return_type
00114 {
00115 OK,
00116 FAIL,
00117 END
00118 };
00119
00120 virtual return_type onField(const char* name, const char* value);
00121
00122 public:
00123 Messageheader()
00124 { clear(); }
00125
00126 virtual ~Messageheader() { }
00127
00128 const_iterator begin() const
00129 { return const_iterator(rawdata); }
00130 const_iterator end() const
00131 { return const_iterator(); }
00132
00133 const_iterator find(const char* key) const;
00134 const_iterator find(const std::string& key) const
00135 { return find(key.c_str()); }
00136
00137 bool hasHeader(const char* key) const
00138 { return find(key) != end(); }
00139 bool hasHeader(const std::string& key) const
00140 { return hasHeader(key.c_str()); }
00141
00142 bool compareHeader(const char* key, const char* value) const;
00143 bool compareHeader(const std::string& key, const std::string& value) const
00144 { return compareHeader(key.c_str(), value.c_str()); }
00145
00146 void removeHeader(const char* key);
00147 void removeHeader(const std::string& key)
00148 { removeHeader(key.c_str()); }
00149
00150 void clear();
00151
00152 void setHeader(const char* key, const char* value, bool replace);
00153
00154 void setHeader(const std::string& key, const std::string& value, bool replace)
00155 { setHeader(key.c_str(), value.c_str(), replace); }
00156 };
00157
00158 std::istream& operator>> (std::istream& in, Messageheader& data);
00159
00160 }
00161
00162 #endif // TNT_MESSAGEHEADER_H
00163