1 #include "ace/INet/HeaderBase.h"
2 #include "ace/OS_NS_stdlib.h"
4 #if !defined (__ACE_INLINE__)
5 #include "ace/INet/HeaderBase.inl"
8 #include "ace/INet/INet_Log.h"
11 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
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
);
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');
66 while (ch
!= eof_
&& ch
!= '\r' && ch
!= '\n')
71 ch
= this->read_field (str
, name
, MAX_NAME_LENGTH
, ':');
75 continue; // ignore invalid headers
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 ()))
89 ch
= this->read_field (str
, value
, MAX_VALUE_LENGTH
, '\r');
91 ch
= str
.get (); // get lf
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
98 while (ch
== ' ' || ch
== '\t')
100 ch
= this->read_field (str
, value
, MAX_VALUE_LENGTH
, '\r');
102 ch
= str
.get (); // get lf
104 return false; // multiline value too long/no crlf; cannot continue
109 this->add (name
, value
);
111 INET_DEBUG (9, (LM_DEBUG
, DLINFO
112 ACE_TEXT ("ACE_INet_HTTP: <-+ %C: %C\n"),
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
);
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);
151 NVPair::NVPair (const ACE_CString
& first
)
156 NVPair::NVPair (const ACE_CString
& first
, const ACE_CString
& second
)
157 : first_ (first
), second_ (second
)
161 NVPair::NVPair (const NVPair
& pair
)
166 NVPair::~NVPair () {}
171 ACE_END_VERSIONED_NAMESPACE_DECL