Menu

[r2367]: / framework / trunk / cppcms / http_content_filter.h  Maximize  Restore  History

Download this file

221 lines (199 with data), 7.0 kB

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