ChunkedDecodingStreamFilter.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef D_CHUNKED_DECODING_STREAM_FILTER_H
  36. #define D_CHUNKED_DECODING_STREAM_FILTER_H
  37. #include "StreamFilter.h"
  38. namespace aria2 {
  39. class ChunkedDecodingStreamFilter : public StreamFilter {
  40. private:
  41. class StateHandler {
  42. public:
  43. virtual ~StateHandler() {}
  44. virtual bool execute
  45. (ChunkedDecodingStreamFilter* filter,
  46. ssize_t& outlen,
  47. size_t& inbufOffset,
  48. const unsigned char* inbuf,
  49. size_t inlen,
  50. const SharedHandle<BinaryStream>& out,
  51. const SharedHandle<Segment>& segment) const = 0;
  52. };
  53. StateHandler* state_;
  54. std::string buf_;
  55. uint64_t chunkSize_;
  56. size_t bytesProcessed_;
  57. static size_t MAX_BUF_SIZE;
  58. bool readChunkSize
  59. (size_t& inbufOffset, const unsigned char* inbuf, size_t inlen);
  60. bool readTrailer
  61. (size_t& inbufOffset, const unsigned char* inbuf, size_t inlen);
  62. bool readData
  63. (ssize_t& outlen,
  64. size_t& inbufOffset,
  65. const unsigned char* inbuf,
  66. size_t inlen,
  67. const SharedHandle<BinaryStream>& out,
  68. const SharedHandle<Segment>& segment);
  69. bool readDataEnd
  70. (size_t& inbufOffset, const unsigned char* inbuf, size_t inlen);
  71. class ReadChunkSizeStateHandler:public StateHandler {
  72. public:
  73. virtual bool execute
  74. (ChunkedDecodingStreamFilter* filter,
  75. ssize_t& outlen,
  76. size_t& inbufOffset,
  77. const unsigned char* inbuf,
  78. size_t inlen,
  79. const SharedHandle<BinaryStream>& out,
  80. const SharedHandle<Segment>& segment) const
  81. {
  82. return filter->readChunkSize(inbufOffset, inbuf, inlen);
  83. }
  84. };
  85. class ReadTrailerStateHandler:public StateHandler {
  86. public:
  87. virtual bool execute
  88. (ChunkedDecodingStreamFilter* filter,
  89. ssize_t& outlen,
  90. size_t& inbufOffset,
  91. const unsigned char* inbuf,
  92. size_t inlen,
  93. const SharedHandle<BinaryStream>& out,
  94. const SharedHandle<Segment>& segment) const
  95. {
  96. return filter->readTrailer(inbufOffset, inbuf, inlen);
  97. }
  98. };
  99. class ReadDataStateHandler:public StateHandler {
  100. public:
  101. virtual bool execute
  102. (ChunkedDecodingStreamFilter* filter,
  103. ssize_t& outlen,
  104. size_t& inbufOffset,
  105. const unsigned char* inbuf,
  106. size_t inlen,
  107. const SharedHandle<BinaryStream>& out,
  108. const SharedHandle<Segment>& segment) const
  109. {
  110. return filter->readData(outlen, inbufOffset, inbuf, inlen, out, segment);
  111. }
  112. };
  113. class ReadDataEndStateHandler:public StateHandler {
  114. public:
  115. virtual bool execute
  116. (ChunkedDecodingStreamFilter* filter,
  117. ssize_t& outlen,
  118. size_t& inbufOffset,
  119. const unsigned char* inbuf,
  120. size_t inlen,
  121. const SharedHandle<BinaryStream>& out,
  122. const SharedHandle<Segment>& segment) const
  123. {
  124. return filter->readDataEnd(inbufOffset, inbuf, inlen);
  125. }
  126. };
  127. class StreamEndStatehandler:public StateHandler {
  128. public:
  129. virtual bool execute
  130. (ChunkedDecodingStreamFilter* filter,
  131. ssize_t& outlen,
  132. size_t& inbufOffset,
  133. const unsigned char* inbuf,
  134. size_t inlen,
  135. const SharedHandle<BinaryStream>& out,
  136. const SharedHandle<Segment>& segment) const
  137. {
  138. return false;
  139. }
  140. };
  141. static ReadChunkSizeStateHandler* readChunkSizeStateHandler_;
  142. static ReadTrailerStateHandler* readTrailerStateHandler_;
  143. static ReadDataStateHandler* readDataStateHandler_;
  144. static ReadDataEndStateHandler* readDataEndStateHandler_;
  145. static StreamEndStatehandler* streamEndStateHandler_;
  146. public:
  147. ChunkedDecodingStreamFilter
  148. (const SharedHandle<StreamFilter>& delegate = SharedHandle<StreamFilter>());
  149. virtual ~ChunkedDecodingStreamFilter();
  150. virtual void init();
  151. virtual ssize_t transform
  152. (const SharedHandle<BinaryStream>& out,
  153. const SharedHandle<Segment>& segment,
  154. const unsigned char* inbuf, size_t inlen);
  155. virtual bool finished();
  156. virtual void release();
  157. virtual const std::string& getName() const;
  158. virtual size_t getBytesProcessed() const
  159. {
  160. return bytesProcessed_;
  161. }
  162. static const std::string NAME;
  163. };
  164. } // namespace aria2
  165. #endif // D_CHUNKED_DECODING_STREAM_FILTER_H