ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

226 lines
6.9 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2015 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // See accompanying file COPYING.TXT file for licensing details.
  6. //
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #ifndef CPPCMS_HTTP_CONTENT_FILTER_H
  9. #define CPPCMS_HTTP_CONTENT_FILTER_H
  10. #include <cppcms/defs.h>
  11. #include <cppcms/cppcms_error.h>
  12. #include <booster/hold_ptr.h>
  13. #include <booster/noncopyable.h>
  14. #include <string>
  15. namespace cppcms {
  16. namespace impl {
  17. struct cached_settings;
  18. }
  19. namespace http {
  20. class file;
  21. class context;
  22. ///
  23. /// Exceptions that is thrown to abort content upload progress indicating an error
  24. ///
  25. /// \ver{v1_2}
  26. class CPPCMS_API abort_upload : public cppcms_error {
  27. public:
  28. ///
  29. /// Abort upload progress, thrown from classes derived from basic_content_filter
  30. /// to abort the upload progress. status_code is the HTTP error code returned, for example 413
  31. /// requested entity is too large
  32. ///
  33. abort_upload(int status_code);
  34. virtual ~abort_upload() throw();
  35. ///
  36. /// Get the code
  37. ///
  38. int code() const;
  39. private:
  40. int code_;
  41. };
  42. ///
  43. /// Class that represent the limits on the input content sizes
  44. ///
  45. /// \ver{v1_2}
  46. class CPPCMS_API content_limits : public booster::noncopyable {
  47. friend class request;
  48. public:
  49. /// \cond INTERNAL
  50. content_limits(impl::cached_settings const &);
  51. content_limits();
  52. ~content_limits();
  53. /// \endcond
  54. ///
  55. /// Get the size limit in bytes of any non multipart/form-data content
  56. ///
  57. /// Note form fields without content-type would be limited by this
  58. /// size even if the multipart_form_data_limit is much larger
  59. ///
  60. long long content_length_limit() const;
  61. ///
  62. /// Set the size limit of any non multipart/form-data content
  63. ///
  64. /// Note form fields without content-type would be limited by this
  65. /// size even if the multipart_form_data_limit is much larger
  66. ///
  67. void content_length_limit(long long size);
  68. ///
  69. /// Get the size limit of multipart/form-data content in bytes
  70. ///
  71. /// Note form fields without content-type would be limited by content_length_limit
  72. /// size even if the multipart_form_data_limit is much larger
  73. ///
  74. long long multipart_form_data_limit() const;
  75. ///
  76. /// Set the size limit of multipart/form-data content in bytes
  77. ///
  78. /// Note form fields without content-type would be limited by content_length_limit
  79. /// size even if the multipart_form_data_limit is much larger
  80. ///
  81. void multipart_form_data_limit(long long size);
  82. ///
  83. /// Get the maximal size of file that is still hold in memory rather than disk
  84. ///
  85. size_t file_in_memory_limit() const;
  86. ///
  87. /// Set the maximal size of file that is still hold in memory rather than disk
  88. ///
  89. void file_in_memory_limit(size_t size);
  90. ///
  91. /// Get a location of a temporary directory that files are uploaded to, if empty system default is used
  92. ///
  93. std::string uploads_path() const;
  94. ///
  95. /// Set a location of a temporary directory that files are uploaded to, if empty system default is used
  96. ///
  97. void uploads_path(std::string const &path);
  98. private:
  99. long long content_length_limit_;
  100. size_t file_in_memory_limit_;
  101. long long multipart_form_data_limit_;
  102. std::string uploads_path_;
  103. struct _data;
  104. booster::hold_ptr<_data> d;
  105. };
  106. ///
  107. /// Basic content filter that can be installed to request, all filters should be derived from this base class
  108. ///
  109. /// Note that when `on_*` member functions of the basic_content_filter are called the original application that runs the filtering
  110. /// has temporary installed context that can be accessed from it.
  111. ///
  112. /// \ver{v1_2}
  113. class CPPCMS_API basic_content_filter {
  114. basic_content_filter(basic_content_filter const &);
  115. void operator=(basic_content_filter const &);
  116. public:
  117. basic_content_filter();
  118. virtual ~basic_content_filter();
  119. ///
  120. /// Member function that is called when entire content is read. By default does nothing.
  121. ///
  122. /// The request can be aborted by throwing abort_upload
  123. ///
  124. virtual void on_end_of_content();
  125. ///
  126. /// Member function that is called in case of a error occuring during upload progress, user should not throw exception from this function but rather
  127. /// perform cleanup procedures if needed
  128. ///
  129. virtual void on_error();
  130. private:
  131. struct _data;
  132. booster::hold_ptr<_data> d;
  133. };
  134. ///
  135. /// Process of any kind of generic content data.
  136. ///
  137. /// Note: when raw_content_filter is used no content data is actually saved to request, for example request().raw_post_data() would return
  138. /// an empty content, so it is your responsibility to store/parse whatever content you use
  139. ///
  140. /// \ver{v1_2}
  141. class CPPCMS_API raw_content_filter : public basic_content_filter {
  142. public:
  143. ///
  144. /// You must implement this member function to handle the data
  145. ///
  146. /// A chunk of incoming data is avalible refered by data of size data_size
  147. ///
  148. /// The request can be aborted by throwing abort_upload
  149. ///
  150. virtual void on_data_chunk(void const *data,size_t data_size) = 0;
  151. raw_content_filter();
  152. virtual ~raw_content_filter();
  153. private:
  154. struct _raw_data;
  155. booster::hold_ptr<_raw_data> d;
  156. };
  157. ///
  158. /// Filter for multipart/form-data - file upload
  159. ///
  160. /// It allows to process/validate incomping data on the fly and make sure that for example the user is actually authorized to upload
  161. /// such a files
  162. ///
  163. /// \ver{v1_2}
  164. class CPPCMS_API multipart_filter : public basic_content_filter {
  165. public:
  166. multipart_filter();
  167. virtual ~multipart_filter();
  168. ///
  169. /// New file meta-data of a form field or file is ready: the mime-type, form name and file name if provided are known, the content wasn't processed yet
  170. ///
  171. /// Notes:
  172. ///
  173. /// - This is the point when you can change various file properties, like location of the temporary file or specifiy output file name and more
  174. /// - The request can be aborted by throwing abort_upload
  175. /// - By default does nothing
  176. ///
  177. virtual void on_new_file(http::file &input_file);
  178. ///
  179. /// Some of the file data is available, you can access it and run some validation during upload progress.
  180. ///
  181. /// Notes:
  182. ///
  183. /// - This is the point when you can perform some file content validation
  184. /// - The request can be aborted by throwing abort_upload
  185. /// - By default does nothing
  186. ///
  187. virtual void on_upload_progress(http::file &input_file);
  188. ///
  189. /// The entire file data was transfered, its size wouldn't change
  190. ///
  191. /// Notes:
  192. ///
  193. /// - This is the point when you can save file if needed or perform final validation
  194. /// - The request can be aborted by throwing abort_upload
  195. /// - By default does nothing
  196. ///
  197. virtual void on_data_ready(http::file &input_file);
  198. private:
  199. struct _mp_data;
  200. booster::hold_ptr<_mp_data> d;
  201. };
  202. } // http
  203. } // cppcms
  204. #endif