Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / protocols / ace / INet / HTTP_StreamPolicy.cpp
blob556c33c4f9d4f90b0bbd61a5392200e11750b76a
1 #ifndef ACE_HTTP_STREAM_POLICY_CPP
2 #define ACE_HTTP_STREAM_POLICY_CPP
4 #include "ace/INet/HTTP_StreamPolicy.h"
5 #include <cctype>
7 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
9 namespace ACE
11 namespace HTTP
13 template <class STREAM_BUFFER>
14 FixedLengthStreamPolicyBase<STREAM_BUFFER>::FixedLengthStreamPolicyBase (std::streamsize length)
15 : StreamPolicyBase<STREAM_BUFFER> (),
16 length_ (length),
17 count_ (0)
21 template <class STREAM_BUFFER>
22 FixedLengthStreamPolicyBase<STREAM_BUFFER>::~FixedLengthStreamPolicyBase ()
26 template <class STREAM_BUFFER>
27 int FixedLengthStreamPolicyBase<STREAM_BUFFER>::read_from_stream (
28 char_type * buf,
29 std::streamsize length)
31 int n = 0;
32 if (this->count_ < this->length_)
34 if (this->count_ + length > this->length_)
35 length = this->length_ - this->count_;
36 n = this->read_from_stream_i (buf, length);
37 if (n > 0) this->count_ += n;
39 return n;
42 template <class STREAM_BUFFER>
43 int FixedLengthStreamPolicyBase<STREAM_BUFFER>::write_to_stream (
44 const char_type * buf,
45 std::streamsize length)
47 int n = 0;
48 if (this->count_ < this->length_)
50 if ((this->count_ + length) > this->length_)
51 length = this->length_ - this->count_;
52 n = this->write_to_stream_i (buf, length);
53 if (n > 0) this->count_ += n;
55 return n;
58 template <class STREAM_BUFFER>
59 ChunkedTransferStreamPolicyBase<STREAM_BUFFER>::ChunkedTransferStreamPolicyBase ()
60 : StreamPolicyBase<STREAM_BUFFER> (),
61 chunk_cnt_ (0)
65 template <class STREAM_BUFFER>
66 ChunkedTransferStreamPolicyBase<STREAM_BUFFER>::~ChunkedTransferStreamPolicyBase ()
70 template <class STREAM_BUFFER>
71 int ChunkedTransferStreamPolicyBase<STREAM_BUFFER>::getc ()
73 static const int eof_ =
74 std::char_traits<char_type>::eof ();
76 char_type chbuf[1];
77 if (this->read_from_stream_i (chbuf, 1) <= 0)
78 return eof_;
79 else
80 return chbuf[0];
83 template <class STREAM_BUFFER>
84 int ChunkedTransferStreamPolicyBase<STREAM_BUFFER>::read_from_stream (
85 char_type * buf,
86 std::streamsize length)
88 static const int eof_ =
89 std::char_traits<char_type>::eof ();
91 char_type lf = this->chunk_.widen ('\n');
92 if (this->chunk_cnt_ == 0)
94 int ch = this->getc ();
95 while (std::isspace (ch))
96 ch = this->getc ();
97 typename buffer_type::string_type chunk_len_str;
98 while (std::isxdigit (ch))
100 chunk_len_str += (char_type) ch;
101 ch = this->getc ();
103 while (ch != eof_ && ch != lf)
105 ch = this->getc ();
107 ACE::IOS::String_IStreamBase<char_type> chunk_len_is (chunk_len_str);
108 unsigned chunk_len;
109 if (!(chunk_len_is >> chunk_len))
110 return eof_;
111 this->chunk_cnt_ = (std::streamsize) chunk_len;
113 if (this->chunk_cnt_ > 0)
115 if (length > this->chunk_cnt_)
116 length = this->chunk_cnt_;
117 int n = this->read_from_stream_i (buf, length);
118 if (n > 0)
119 this->chunk_cnt_ -= n;
120 return n;
122 else
124 int ch = this->getc ();
125 while (ch != eof_ && ch != lf)
126 ch = this->getc ();
127 return 0;
131 template <class STREAM_BUFFER>
132 int ChunkedTransferStreamPolicyBase<STREAM_BUFFER>::write_to_stream (
133 const char_type * buf,
134 std::streamsize length)
136 this->chunk_.clear ();
137 this->chunk_ << std::hex << length << std::dec;
138 this->chunk_ << this->chunk_.widen ('\r') << this->chunk_.widen ('\n');
139 this->chunk_.write (buf, length);
140 this->chunk_ << this->chunk_.widen ('\r') << this->chunk_.widen ('\n');
141 const typename buffer_type::string_type& str = this->chunk_.str ();
142 return this->write_to_stream_i (str.c_str (), str.length ());
147 ACE_END_VERSIONED_NAMESPACE_DECL
149 #endif /* ACE_HTTP_STREAM_POLICY_CPP */