Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / protocols / ace / INet / HeaderBase.cpp
blobbbff0b8b17e8955432754a58222ff730bffba334
1 #include "ace/INet/HeaderBase.h"
2 #include "ace/OS_NS_stdlib.h"
4 #if !defined (__ACE_INLINE__)
5 #include "ace/INet/HeaderBase.inl"
6 #endif
8 #include "ace/INet/INet_Log.h"
11 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
13 namespace ACE
15 namespace INet
17 const int HeaderBase::UNKNOWN_CONTENT_LENGTH = -1;
18 const ACE_CString HeaderBase::UNKNOWN_CONTENT_TYPE;
19 const ACE_CString HeaderBase::CONTENT_LENGTH = "Content-Length";
20 const ACE_CString HeaderBase::CONTENT_TYPE = "Content-Type";
21 const ACE_CString HeaderBase::EMPTY;
23 const int HeaderBase::eof_ = std::char_traits<char>::eof();
25 HeaderBase::HeaderBase()
29 HeaderBase::~HeaderBase()
33 void HeaderBase::set_content_length(int length)
35 if (length == UNKNOWN_CONTENT_LENGTH)
37 this->remove (CONTENT_LENGTH);
39 else
41 char buf[32];
42 this->set (CONTENT_LENGTH,
43 ACE_OS::itoa (length, buf, 10));
47 void HeaderBase::write(std::ostream& str) const
49 TNVMap::ITERATOR it (const_cast<TNVMap&> (this->header_values_));
50 for (it.first (); !it.done () ;it.advance ())
52 str << (*it).first ().c_str () << ": " << (*it).second ().c_str () << "\r\n";
54 INET_DEBUG (9, (LM_DEBUG, DLINFO
55 ACE_TEXT ("ACE_INet_HTTP: +-> %C: %C\n"),
56 (*it).first ().c_str (),
57 (*it).second ().c_str ()));
61 bool HeaderBase::read(std::istream& str)
63 ACE_CString name (64, '\0');
64 ACE_CString value (128, '\0');
65 int ch = str.peek ();
66 while (ch != eof_ && ch != '\r' && ch != '\n')
68 name.fast_clear ();
69 value.fast_clear ();
70 // parse name
71 ch = this->read_field (str, name, MAX_NAME_LENGTH, ':');
72 if (ch == '\n')
74 ch = str.get ();
75 continue; // ignore invalid headers
77 if (ch != ':')
79 return false; // name too long/missing colon; cannot continue
82 // skip leading whitespace before next field
83 while (ACE_OS::ace_isspace (str.peek ()))
85 ch = str.get ();
88 // parse value
89 ch = this->read_field (str, value, MAX_VALUE_LENGTH, '\r');
90 if (ch == '\r')
91 ch = str.get (); // get lf
92 if (ch != '\n')
93 return false; // value too long/no crlf found; cannot continue
95 // followup lines starting with ws are continuations of the value
96 // and must be appended
97 ch = str.peek ();
98 while (ch == ' ' || ch == '\t')
100 ch = this->read_field (str, value, MAX_VALUE_LENGTH, '\r');
101 if (ch == '\r')
102 ch = str.get (); // get lf
103 if (ch != '\n')
104 return false; // multiline value too long/no crlf; cannot continue
106 ch = str.peek ();
109 this->add (name, value);
111 INET_DEBUG (9, (LM_DEBUG, DLINFO
112 ACE_TEXT ("ACE_INet_HTTP: <-+ %C: %C\n"),
113 name.c_str (),
114 value.c_str ()));
116 return true;
120 void HeaderBase::set (const ACE_CString& name, const ACE_CString& value)
122 TNVMap::ITERATOR it (this->header_values_);
123 if (this->header_values_.find (NVPair (name), it) == 0)
125 (*it).second (value);
127 else
129 this->header_values_.insert (NVPair (name, value));
133 void HeaderBase::get_values(const ACE_CString& name, ACE_Array<ACE_CString> & values) const
135 TNVMap::ITERATOR it (const_cast<TNVMap&> (this->header_values_));
136 if (this->header_values_.find (name, it) == 0)
138 for (; !it.done () && ((*it).second () == name) ;it.advance ())
140 if (values.size (values.size ()+1) == 0)
142 values.set ((*it).second (), values.size ()-1);
149 NVPair::NVPair () {}
151 NVPair::NVPair (const ACE_CString& first)
152 : first_ (first)
156 NVPair::NVPair (const ACE_CString& first, const ACE_CString& second)
157 : first_ (first), second_ (second)
161 NVPair::NVPair (const NVPair& pair)
163 *this = pair;
166 NVPair::~NVPair () {}
171 ACE_END_VERSIONED_NAMESPACE_DECL