Go to the documentation of this file.00001
00002
00003 #ifndef ACE_HTTP_STREAM_POLICY_CPP
00004 #define ACE_HTTP_STREAM_POLICY_CPP
00005
00006 #include "ace/INet/HTTP_StreamPolicy.h"
00007 #include <cctype>
00008
00009 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00010
00011 namespace ACE
00012 {
00013 namespace HTTP
00014 {
00015 template <class STREAM_BUFFER>
00016 FixedLengthStreamPolicyBase<STREAM_BUFFER>::FixedLengthStreamPolicyBase (std::streamsize length)
00017 : StreamPolicyBase<STREAM_BUFFER> (),
00018 length_ (length),
00019 count_ (0)
00020 {
00021 }
00022
00023 template <class STREAM_BUFFER>
00024 FixedLengthStreamPolicyBase<STREAM_BUFFER>::~FixedLengthStreamPolicyBase ()
00025 {
00026 }
00027
00028 template <class STREAM_BUFFER>
00029 int FixedLengthStreamPolicyBase<STREAM_BUFFER>::read_from_stream (
00030 char_type * buf,
00031 std::streamsize length)
00032 {
00033 int n = 0;
00034 if (this->count_ < this->length_)
00035 {
00036 if (this->count_ + length > this->length_)
00037 length = this->length_ - this->count_;
00038 n = this->read_from_stream_i (buf, length);
00039 if (n > 0) this->count_ += n;
00040 }
00041 return n;
00042 }
00043
00044 template <class STREAM_BUFFER>
00045 int FixedLengthStreamPolicyBase<STREAM_BUFFER>::write_to_stream (
00046 const char_type * buf,
00047 std::streamsize length)
00048 {
00049 int n = 0;
00050 if (this->count_ < this->length_)
00051 {
00052 if ((this->count_ + length) > this->length_)
00053 length = this->length_ - this->count_;
00054 n = this->write_to_stream_i (buf, length);
00055 if (n > 0) this->count_ += n;
00056 }
00057 return n;
00058 }
00059
00060 template <class STREAM_BUFFER>
00061 ChunkedTransferStreamPolicyBase<STREAM_BUFFER>::ChunkedTransferStreamPolicyBase ()
00062 : StreamPolicyBase<STREAM_BUFFER> (),
00063 chunk_cnt_ (0)
00064 {
00065 }
00066
00067 template <class STREAM_BUFFER>
00068 ChunkedTransferStreamPolicyBase<STREAM_BUFFER>::~ChunkedTransferStreamPolicyBase ()
00069 {
00070 }
00071
00072 template <class STREAM_BUFFER>
00073 int ChunkedTransferStreamPolicyBase<STREAM_BUFFER>::getc ()
00074 {
00075 static const int eof_ =
00076 std::char_traits<char_type>::eof ();
00077
00078 char_type chbuf[1];
00079 if (this->read_from_stream_i (chbuf, 1) <= 0)
00080 return eof_;
00081 else
00082 return chbuf[0];
00083 }
00084
00085 template <class STREAM_BUFFER>
00086 int ChunkedTransferStreamPolicyBase<STREAM_BUFFER>::read_from_stream (
00087 char_type * buf,
00088 std::streamsize length)
00089 {
00090 static const int eof_ =
00091 std::char_traits<char_type>::eof ();
00092
00093 char_type lf = this->chunk_.widen ('\n');
00094 if (this->chunk_cnt_ == 0)
00095 {
00096 int ch = this->getc ();
00097 while (std::isspace (ch))
00098 ch = this->getc ();
00099 typename buffer_type::string_type chunk_len_str;
00100 while (std::isxdigit (ch))
00101 {
00102 chunk_len_str += (char_type) ch;
00103 ch = this->getc ();
00104 }
00105 while (ch != eof_ && ch != lf)
00106 {
00107 ch = this->getc ();
00108 }
00109 ACE::IOS::String_IStreamBase<char_type> chunk_len_is (chunk_len_str);
00110 unsigned chunk_len;
00111 if (!(chunk_len_is >> chunk_len))
00112 return eof_;
00113 this->chunk_cnt_ = (std::streamsize) chunk_len;
00114 }
00115 if (this->chunk_cnt_ > 0)
00116 {
00117 if (length > this->chunk_cnt_)
00118 length = this->chunk_cnt_;
00119 int n = this->read_from_stream_i (buf, length);
00120 if (n > 0)
00121 this->chunk_cnt_ -= n;
00122 return n;
00123 }
00124 else
00125 {
00126 int ch = this->getc ();
00127 while (ch != eof_ && ch != lf)
00128 ch = this->getc ();
00129 return 0;
00130 }
00131 }
00132
00133 template <class STREAM_BUFFER>
00134 int ChunkedTransferStreamPolicyBase<STREAM_BUFFER>::write_to_stream (
00135 const char_type * buf,
00136 std::streamsize length)
00137 {
00138 this->chunk_.clear ();
00139 this->chunk_ << std::hex << length << std::dec;
00140 this->chunk_ << this->chunk_.widen ('\r') << this->chunk_.widen ('\n');
00141 this->chunk_.write (buf, length);
00142 this->chunk_ << this->chunk_.widen ('\r') << this->chunk_.widen ('\n');
00143 const typename buffer_type::string_type& str = this->chunk_.str ();
00144 return this->write_to_stream_i (str.c_str (), str.length ());
00145 }
00146 }
00147 }
00148
00149 ACE_END_VERSIONED_NAMESPACE_DECL
00150
00151 #endif