DCL 3.7.4
Loading...
Searching...
No Matches
HttpCollection.h
Go to the documentation of this file.
1#ifndef __DCL_HTTP_COLLECTION_H__
2#define __DCL_HTTP_COLLECTION_H__ 20050526
3
4#ifndef __DCL_OBJECT_H__
5#include <dcl/Object.h>
6#endif
7#ifndef __DCL_EXCEPTION_H__
8#include <dcl/Exception.h>
9#endif
10#ifndef __DCL_INPUT_STREAM_H__
11#include <dcl/InputStream.h>
12#endif
13#ifndef __DCL_LISTED_HASH_MAP_H__
14#include <dcl/ListedHashMap.h>
15#endif
16
17__DCL_BEGIN_NAMESPACE
18
20
21/*
22 HttpCookieDecoder
23 HttpQueryStringDecoder
24 HttpQueryStringEncoder
25
26 InputStream
27 HttpFormDataInputStream
28 HttpFormData
29 BufferedHttpFormData
30 StoredHttpFormData
31 HttpFormDataDecoder
32*/
33
35{
36public:
37 static void decode(
38 const ByteString& _contents,
39 ListedStringToStringMap& _results
40 );
41};
42
44{
45public:
46 static void decode(
47 const ByteString& _queryString,
48 ListedStringToStringArrayMap& _results
49 );
50
51 static void decode(
52 const char* _begin,
53 const char* _end,
54 ListedStringToStringArrayMap& _results
55 );
56
57 static bool isValidType(
58 const char* _contentType
59 );
60};
61
63{
64public:
65 static String encode(
66 const ListedStringToStringArrayMap& _map
67 );
68};
69
70// RFC 1867 - Form-based File Upload in HTML
71// http://www.faqs.org/rfcs/rfc1867.html
72
75{
77protected:
78 struct PartHeader
79 {
80 // Content-Disposition
81 String type; // "form-data", "attachement"
82 String name; // value of "name"
83 String filename; // value of "filename"
84 String contentType; // value of "Content-Type"
85
86 String toString() const;
87 };
88
89 // PartHeader::strFileName이 있을경우 파일의 시작이다.
90 // 만약 onFileStart내부에서 에러가 발생하여 false를 리턴하면
91 // 이후의 onFileData, onFileEnd는 호출되지 않는다.
92 virtual bool onFileStart(
93 const PartHeader& header,
94 void** ppCallbackData,
95 String& strCallbackError // return false 이면
96 );
97
98 // formdata의 파일의 데이터가 있을때 불려진다.
99 // 만약 onFileData 내부에서 에러가 발생하여 false를 리턴하면
100 // onFileEnd 는 호출되지 않는다. 따라서 pCallbackData 관련하여
101 // 자원이 할당되어 있으면 리턴하기 전에 해제해야 한다.
102 virtual bool onFileData(
103 const void* pData,
104 size_t nSize,
105 void* pCallbackData,
106 String& strCallbackError // return false 이면
107 );
108
109 // 종결 part가 나타나면 불려진다. 종결 part는 CRLF로 구분되며
110 // 만약 이것이 타나타지 않은 상태에는 bDataSuccess는 false를 넘긴다.
111 virtual bool onFileEnd(
112 const PartHeader& header,
113 void* pCallbackData,
114 bool bDataSuccess,
115 String& strCallbackError // return false 이면
116 );
117
119};
120
121class DCLCAPI HttpFormDataDecoderException : public Exception
122{
123 DECLARE_CLASSINFO(HttpFormDataDecoderException)
124public:
125 enum ErrorCode
126 {
127 ePostReadError,
128 eFormDataCallbackError
129 };
130
131 ErrorCode __errorCode;
132 String __message;
133
134 HttpFormDataDecoderException(
135 ErrorCode _errorCode,
136 IOException* _cause
137 );
138
139 HttpFormDataDecoderException(
140 ErrorCode _errorCode,
141 const String& _message
142 );
143
144 virtual String toString() const;
145};
146
148{
150public:
151 static bool isValidType(const char* _contentType);
152 static ByteString getBoundary(const char* _contentType);
153
154public:
155 HttpFormDataDecoder(size_t _bufferSize = 4096);
156
157 virtual ~HttpFormDataDecoder();
158
159 // 디코딩 과정중에 입력데이터와 관련한 에러가 발생하면
160 // warnings() 을 통해 에러 메시지를 얻을 수 있다.
161 // 에러가 발생한 데이터는 버려진다.
162 void decode(
163 InputStream& _input,
164 const char* _contentType,
165 size_t _contentLength,
166 ListedStringToStringArrayMap& _mapForm,
167 HttpFormData& _mapFormFile
169
170 const String warnings() const { return __warnings; }
171
172private:
173 // false : 버퍼가 비어 있거나, not found CRLF
174 bool getLine(char*& _begin, char*& _end);
175
176 // true : valid first part boundary
177 // false : close boundary, or invalid data
178 bool getFirstBoundary(const ByteString& _boundary);
179
180 // false EOF
181 bool getPartHeader(HttpFormData::PartHeader& header)
183
184 // NULL : 버퍼가 비어 있거나 데이터가 유효하지 않다.
185 enum DataState
186 {
187 dsNeedMoreData,
188 dsBeforeNextBoundary,
189 dsBeforeCloseBoundary
190 };
191
192 DataState getDataBlock(
193 const ByteString& _boundary,
194 char*& _begin,
195 char*& _end
196 );
197
198 // false data empty && EOF
199 bool readInput() __DCL_THROWS1(HttpFormDataDecoderException*);
200 void appendWarning(const String& _warning);
201
202 InputStream* __input;
203 size_t __contentLength;
204 size_t __remainder;
205
206 char* __buffer;
207 size_t __bufferSize;
208 char* __begin;
209 char* __end;
210
211 StringBuilder __warnings; // decoding warnings, string delimiter '\n'
212
213 // const strings for compare
214 _CONST ByteString __contentDisposition; // "Content-Dispositon"
215 _CONST ByteString __contentType; // "Content-Type"
216 _CONST ByteString __name; // "name"
217 _CONST ByteString __filename; // "filename"
218};
219
220// 파일데이터를 버퍼에 유지
222{
224public:
225 class FileInfoArray;
226
227 class DCLCAPI FileInfo : public Object
228 {
229 public:
230 String filename; // IE는 절대경로 포함, Netscape는 basename
231 String contentType;
232 String transferEncoding;
233 ByteString fileData;
234
235 protected:
236 // not using
237 // FileInfo info = v[i];
238 // use
239 // FileInfo& info = v[i];
240 FileInfo();
241
242 friend class FileInfoArray;
243 friend class BufferedHttpFormData;
244 };
245
246 class DCLCAPI FileInfoArray : public Object
247 {
248 public:
249 FileInfo& operator[] (size_t _index);
250 size_t size() const;
251 bool isEmpty() const;
252 const String& name() const { return __name; }
253
254 private:
255 void* __handle;
256
257 protected:
258 String __name; // <input name="name"
259
260 // not using
261 // FileInfoArray v = formData["name"];
262 // use
263 // FileInfoArray& v = formData.byName("name");
264 // or FileInfoVecotr& v = formData[i];
265 FileInfoArray(const String& _name);
266 FileInfoArray(const FileInfo& src);
267 ~FileInfoArray();
268 void add(FileInfo* pNewItem);
269
270 friend class BufferedHttpFormData;
271 };
272
273public:
275 virtual ~BufferedHttpFormData();
276
277 // <input name="name" type="file"/> 에서 "name" 이 개수
278 // 서로 다른 "name"의 개수
279 // FileInfoVector의 개수
280 size_t size() const;
281 bool isEmpty() const;
282 FileInfoArray& operator[] (size_t _index);
283 FileInfoArray& byName(const wchar_t* _name);
284
285private:
286 void* __handle;
287
288 void insert(const String& _name, FileInfo* pNewItem);
289
290protected:
291 virtual bool onFileStart(
292 const PartHeader& header,
293 void** ppCallbackData,
294 String& strCallbackError // return false 이면
295 );
296
297 virtual bool onFileData(
298 const void* pData,
299 size_t nSize,
300 void* pCallbackData,
301 String& strCallbackError // return false 이면
302 );
303
304 virtual bool onFileEnd(
305 const PartHeader& header,
306 void* pCallbackData,
307 bool bDataSuccess,
308 String& strCallbackError // return false 이면
309 );
310};
311
312// 파일데이터를 임시파일에 저장
314{
316public:
317 class FileInfoArray;
318
319 class DCLCAPI FileInfo : public Object
320 {
321 public :
322 String filename; // basename
323 String contentType;
324 String transferEncoding;
325 size_t fileSize;
326 String tempFilename;
327
328 protected:
329 // not using
330 // FileInfo info = v[i];
331 // use
332 // FileInfo& info = v[i];
333 FileInfo();
334 FileInfo(const FileInfo& str);
335 ~FileInfo();
336
337 friend class FileInfoArray;
338 friend class StoredHttpFormData;
339 };
340
341 class DCLCAPI FileInfoArray : public Object
342 {
343 public:
344 FileInfo& operator[] (size_t _index);
345 size_t size() const;
346 bool isEmpty() const;
347 const String& name() const { return __name; }
348
349 private:
350 void* __handle;
351
352 protected:
353 String __name; // <input name="name"
354
355 // not using
356 // FileInfoArray v = formData["name"];
357 // use
358 // FileInfoArray& v = formData.byName("name");
359 // or FileInfoVecotr& v = formData[i];
360 FileInfoArray(const String& _name);
361 FileInfoArray(const FileInfo& src);
362 ~FileInfoArray();
363 void add(FileInfo* pNewItem);
364
365 friend class StoredHttpFormData;
366 };
367
368public:
369 StoredHttpFormData(const String& strTempDir);
370 virtual ~StoredHttpFormData();
371
372 // <input name="name" type="file"/> 에서 "name" 이 개수
373 // 서로 다른 "name"의 개수
374 // FileInfoVector의 개수
375 size_t size() const;
376 bool isEmpty() const;
377 FileInfoArray& operator[] (size_t _index);
378 FileInfoArray& byName(const wchar_t* _name);
379
380private:
381 String __tempDir;
382 void* __handle;
383
384 void insert(const String& _name, FileInfo* pNewItem);
385
386protected:
387 virtual bool onFileStart(
388 const PartHeader& header,
389 void** ppCallbackData,
390 String& strCallbackError // return false 이면
391 );
392
393 virtual bool onFileData(
394 const void* pData,
395 size_t nSize,
396 void* pCallbackData,
397 String& strCallbackError // return false 이면
398 );
399
400 virtual bool onFileEnd(
401 const PartHeader& header,
402 void* pCallbackData,
403 bool bDataSuccess,
404 String& strCallbackError // return false 이면
405 );
406};
407
408__DCL_END_NAMESPACE
409
410#endif // __DCL_HTTP_COLLECTION_H__
#define DCLCAPI
Definition Config.h:95
#define _CONST
Definition Config.h:325
#define __DCL_THROWS1(e)
Definition Config.h:152
#define DECLARE_CLASSINFO(class_name)
Definition Object.h:227
virtual bool onFileStart(const PartHeader &header, void **ppCallbackData, String &strCallbackError)
FileInfoArray & byName(const wchar_t *_name)
virtual bool onFileEnd(const PartHeader &header, void *pCallbackData, bool bDataSuccess, String &strCallbackError)
virtual bool onFileData(const void *pData, size_t nSize, void *pCallbackData, String &strCallbackError)
Exception(Exception *_cause=NULL)
void decode(InputStream &_input, const char *_contentType, size_t _contentLength, ListedStringToStringArrayMap &_mapForm, HttpFormData &_mapFormFile) __DCL_THROWS1(HttpFormDataDecoderException *)
HttpFormDataDecoder(size_t _bufferSize=4096)
const String warnings() const
friend class HttpFormDataDecoder
static void decode(const ByteString &_queryString, ListedStringToStringArrayMap &_results)
static bool isValidType(const char *_contentType)
static String encode(const ListedStringToStringArrayMap &_map)
Object()
Definition Object.cpp:183
virtual String toString() const
Definition Object.cpp:187
virtual bool onFileStart(const PartHeader &header, void **ppCallbackData, String &strCallbackError)
FileInfoArray & byName(const wchar_t *_name)
StoredHttpFormData(const String &strTempDir)
virtual bool onFileData(const void *pData, size_t nSize, void *pCallbackData, String &strCallbackError)
virtual bool onFileEnd(const PartHeader &header, void *pCallbackData, bool bDataSuccess, String &strCallbackError)