DCL 4.0
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 m_errorCode;
132 String m_strMsg;
133
134 HttpFormDataDecoderException(
135 ErrorCode errorCode,
136 IOException* pDetailEx
137 );
138
139 HttpFormDataDecoderException(
140 ErrorCode errorCode,
141 const String& _message
142 );
143
144 virtual String toString() const;
145};
146
147
149{
151public:
152 static bool isValidType(const char* _contentType);
153 static ByteString getBoundary(const char* _contentType);
154
155public:
156 HttpFormDataDecoder(size_t _bufferSize = 4096);
157
158 virtual ~HttpFormDataDecoder();
159
160 // 디코딩 과정중에 입력데이터와 관련한 에러가 발생하면
161 // warnings() 을 통해 에러 메시지를 얻을 수 있다.
162 // 에러가 발생한 데이터는 버려진다.
163 void decode(
164 InputStream& _input,
165 const char* _contentType,
166 size_t _contentLength,
167 ListedStringToStringArrayMap& _mapForm,
168 HttpFormData& _mapFormFile
170
171 const String warnings() const { return __warnings; }
172
173private:
174 // false : 버퍼가 비어 있거나, not found CRLF
175 bool getLine(char*& _begin, char*& _end);
176
177 // true : valid first part boundary
178 // false : close boundary, or invalid data
179 bool getFirstBoundary(const ByteString& _boundary);
180
181 // false EOF
182 bool getPartHeader(HttpFormData::PartHeader& header)
184
185 // NULL : 버퍼가 비어 있거나 데이터가 유효하지 않다.
186 enum DataState
187 {
188 dsNeedMoreData,
189 dsBeforeNextBoundary,
190 dsBeforeCloseBoundary
191 };
192
193 DataState getDataBlock(
194 const ByteString& _boundary,
195 char*& _begin,
196 char*& _end
197 );
198
199 // false data empty && EOF
200 bool readInput() __DCL_THROWS1(HttpFormDataDecoderException*);
201 void appendWarning(const String& _warning);
202
203
204 InputStream* __input;
205 size_t __contentLength;
206 size_t __remainder;
207
208 char* __buffer;
209 size_t __bufferSize;
210 char* __begin;
211 char* __end;
212
213 StringBuilder __warnings; // decoding warnings, string delimiter '\n'
214
215 // const strings for compare
216 _CONST ByteString __contentDisposition; // "Content-Dispositon"
217 _CONST ByteString __contentType; // "Content-Type"
218 _CONST ByteString __name; // "name"
219 _CONST ByteString __filename; // "filename"
220};
221
222// 파일데이터를 버퍼에 유지
224{
226public:
227 class FileInfoArray;
228
229 class DCLCAPI FileInfo
230 {
231 public:
232 String filename; // IE는 절대경로 포함, Netscape는 basename
233 String contentType;
234 String transferEncoding;
235 ByteString fileData;
236
237 protected:
238 // not using
239 // FileInfo info = v[i];
240 // use
241 // FileInfo& info = v[i];
242 FileInfo();
243
244 friend class FileInfoArray;
245 friend class BufferedHttpFormData;
246 };
247
248 class DCLCAPI FileInfoArray
249 {
250 public:
251 FileInfo& operator[] (size_t _index);
252 size_t size() const;
253 bool isEmpty() const;
254 const String& name() const { return __name; }
255
256 private:
257 void* __handle;
258
259 protected:
260 String __name; // <input name="name"
261
262 // not using
263 // FileInfoArray v = formData["name"];
264 // use
265 // FileInfoArray& v = formData.byName("name");
266 // or FileInfoVecotr& v = formData[i];
267 FileInfoArray(const String& _name);
268 FileInfoArray(const FileInfo& src);
269 ~FileInfoArray();
270 void add(FileInfo* pNewItem);
271
272 friend class BufferedHttpFormData;
273 };
274
275public:
277 virtual ~BufferedHttpFormData();
278
279 // <input name="name" type="file"/> 에서 "name" 이 개수
280 // 서로 다른 "name"의 개수
281 // FileInfoVector의 개수
282 size_t size() const;
283 bool isEmpty() const;
284 FileInfoArray& operator[] (size_t _index);
285 FileInfoArray& byName(const wchar_t* _name);
286
287private:
288 void* __handle;
289
290 void insert(const String& _name, FileInfo* pNewItem);
291
292protected:
293 virtual bool onFileStart(
294 const PartHeader& header,
295 void** ppCallbackData,
296 String& strCallbackError // return false 이면
297 );
298
299 virtual bool onFileData(
300 const void* pData,
301 size_t nSize,
302 void* pCallbackData,
303 String& strCallbackError // return false 이면
304 );
305
306 virtual bool onFileEnd(
307 const PartHeader& header,
308 void* pCallbackData,
309 bool bDataSuccess,
310 String& strCallbackError // return false 이면
311 );
312};
313
314// 파일데이터를 임시파일에 저장
316{
318public:
319 class FileInfoArray;
320
321 class DCLCAPI FileInfo
322 {
323 public :
324 String filename; // basename
325 String contentType;
326 String transferEncoding;
327 size_t fileSize;
328 String tempFilename;
329
330 protected:
331 // not using
332 // FileInfo info = v[i];
333 // use
334 // FileInfo& info = v[i];
335 FileInfo();
336 FileInfo(const FileInfo& str);
337 ~FileInfo();
338
339 friend class FileInfoArray;
340 friend class StoredHttpFormData;
341 };
342
343 class DCLCAPI FileInfoArray
344 {
345 public:
346 FileInfo& operator[] (size_t _index);
347 size_t size() const;
348 bool isEmpty() const;
349 const String& name() const { return __name; }
350
351 private:
352 void* __handle;
353
354 protected:
355 String __name; // <input name="name"
356
357 // not using
358 // FileInfoArray v = formData["name"];
359 // use
360 // FileInfoArray& v = formData.byName("name");
361 // or FileInfoVecotr& v = formData[i];
362 FileInfoArray(const String& _name);
363 FileInfoArray(const FileInfo& src);
364 ~FileInfoArray();
365 void add(FileInfo* pNewItem);
366
367 friend class StoredHttpFormData;
368 };
369
370public:
371 StoredHttpFormData(const String& strTempDir);
372 virtual ~StoredHttpFormData();
373
374 // <input name="name" type="file"/> 에서 "name" 이 개수
375 // 서로 다른 "name"의 개수
376 // FileInfoVector의 개수
377 size_t size() const;
378 bool isEmpty() const;
379 FileInfoArray& operator[] (size_t _index);
380 FileInfoArray& byName(const wchar_t* _name);
381
382private:
383 String __tempDir;
384 void* __handle;
385
386 void insert(const String& _name, FileInfo* pNewItem);
387
388protected:
389 virtual bool onFileStart(
390 const PartHeader& header,
391 void** ppCallbackData,
392 String& strCallbackError // return false 이면
393 );
394
395 virtual bool onFileData(
396 const void* pData,
397 size_t nSize,
398 void* pCallbackData,
399 String& strCallbackError // return false 이면
400 );
401
402 virtual bool onFileEnd(
403 const PartHeader& header,
404 void* pCallbackData,
405 bool bDataSuccess,
406 String& strCallbackError // return false 이면
407 );
408};
409
410__DCL_END_NAMESPACE
411
412#endif // __DCL_HTTP_COLLECTION_H__
#define DCLCAPI
Definition Config.h:100
#define _CONST
Definition Config.h:353
#define __DCL_THROWS1(e)
Definition Config.h:167
#define DECLARE_CLASSINFO(class_name)
Definition Object.h:210
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)