HTTPChunkedStream.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // HTTPChunkedStream.cpp
  3. //
  4. // $Id: //poco/1.3/Net/src/HTTPChunkedStream.cpp#2 $
  5. //
  6. // Library: Net
  7. // Package: HTTP
  8. // Module: HTTPChunkedStream
  9. //
  10. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // Permission is hereby granted, free of charge, to any person or organization
  14. // obtaining a copy of the software and accompanying documentation covered by
  15. // this license (the "Software") to use, reproduce, display, distribute,
  16. // execute, and transmit the Software, and to prepare derivative works of the
  17. // Software, and to permit third-parties to whom the Software is furnished to
  18. // do so, all subject to the following:
  19. //
  20. // The copyright notices in the Software and this entire statement, including
  21. // the above license grant, this restriction and the following disclaimer,
  22. // must be included in all copies of the Software, in whole or in part, and
  23. // all derivative works of the Software, unless such copies or derivative
  24. // works are solely in the form of machine-executable object code generated by
  25. // a source language processor.
  26. //
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  30. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  31. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  32. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  33. // DEALINGS IN THE SOFTWARE.
  34. //
  35. #include "Poco/Net/HTTPChunkedStream.h"
  36. #include "Poco/Net/HTTPSession.h"
  37. #include "Poco/NumberFormatter.h"
  38. #include "Poco/NumberParser.h"
  39. #include <ctype.h>
  40. using Poco::NumberFormatter;
  41. using Poco::NumberParser;
  42. namespace Poco {
  43. namespace Net {
  44. //
  45. // HTTPChunkedStreamBuf
  46. //
  47. HTTPChunkedStreamBuf::HTTPChunkedStreamBuf(HTTPSession& session, openmode mode):
  48. HTTPBasicStreamBuf(HTTPBufferAllocator::BUFFER_SIZE, mode),
  49. _session(session),
  50. _mode(mode),
  51. _chunk(0)
  52. {
  53. }
  54. HTTPChunkedStreamBuf::~HTTPChunkedStreamBuf()
  55. {
  56. }
  57. void HTTPChunkedStreamBuf::close()
  58. {
  59. if (_mode & std::ios::out)
  60. {
  61. sync();
  62. _session.write("0\r\n\r\n", 5);
  63. }
  64. }
  65. int HTTPChunkedStreamBuf::readFromDevice(char* buffer, std::streamsize length)
  66. {
  67. static const int eof = std::char_traits<char>::eof();
  68. if (_chunk == 0)
  69. {
  70. int ch = _session.get();
  71. while (isspace(ch)) ch = _session.get();
  72. std::string chunkLen;
  73. while (isxdigit(ch)) { chunkLen += (char) ch; ch = _session.get(); }
  74. while (ch != eof && ch != '\n') ch = _session.get();
  75. unsigned chunk;
  76. if (NumberParser::tryParseHex(chunkLen, chunk))
  77. _chunk = (std::streamsize) chunk;
  78. else
  79. return eof;
  80. }
  81. if (_chunk > 0)
  82. {
  83. if (length > _chunk) length = _chunk;
  84. int n = _session.read(buffer, length);
  85. if (n > 0) _chunk -= n;
  86. return n;
  87. }
  88. else return 0;
  89. }
  90. int HTTPChunkedStreamBuf::writeToDevice(const char* buffer, std::streamsize length)
  91. {
  92. std::string chunkSize(NumberFormatter::formatHex(length));
  93. chunkSize.append("\r\n");
  94. _session.write(chunkSize.data(), (std::streamsize) chunkSize.length());
  95. _session.write(buffer, length);
  96. _session.write("\r\n", 2);
  97. return length;
  98. }
  99. //
  100. // HTTPChunkedIOS
  101. //
  102. HTTPChunkedIOS::HTTPChunkedIOS(HTTPSession& session, HTTPChunkedStreamBuf::openmode mode):
  103. _buf(session, mode)
  104. {
  105. poco_ios_init(&_buf);
  106. }
  107. HTTPChunkedIOS::~HTTPChunkedIOS()
  108. {
  109. try
  110. {
  111. _buf.close();
  112. }
  113. catch (...)
  114. {
  115. }
  116. }
  117. HTTPChunkedStreamBuf* HTTPChunkedIOS::rdbuf()
  118. {
  119. return &_buf;
  120. }
  121. //
  122. // HTTPChunkedInputStream
  123. //
  124. Poco::MemoryPool HTTPChunkedInputStream::_pool(sizeof(HTTPChunkedInputStream));
  125. HTTPChunkedInputStream::HTTPChunkedInputStream(HTTPSession& session):
  126. HTTPChunkedIOS(session, std::ios::in),
  127. std::istream(&_buf)
  128. {
  129. }
  130. HTTPChunkedInputStream::~HTTPChunkedInputStream()
  131. {
  132. }
  133. void* HTTPChunkedInputStream::operator new(size_t size)
  134. {
  135. return _pool.get();
  136. }
  137. void HTTPChunkedInputStream::operator delete(void* ptr)
  138. {
  139. _pool.release(ptr);
  140. }
  141. //
  142. // HTTPChunkedOutputStream
  143. //
  144. Poco::MemoryPool HTTPChunkedOutputStream::_pool(sizeof(HTTPChunkedOutputStream));
  145. HTTPChunkedOutputStream::HTTPChunkedOutputStream(HTTPSession& session):
  146. HTTPChunkedIOS(session, std::ios::out),
  147. std::ostream(&_buf)
  148. {
  149. }
  150. HTTPChunkedOutputStream::~HTTPChunkedOutputStream()
  151. {
  152. }
  153. void* HTTPChunkedOutputStream::operator new(size_t size)
  154. {
  155. return _pool.get();
  156. }
  157. void HTTPChunkedOutputStream::operator delete(void* ptr)
  158. {
  159. _pool.release(ptr);
  160. }
  161. } } // namespace Poco::Net