vfs: check userland buffers before reading them.
[haiku.git] / headers / os / net / HttpForm.h
blobff7cee29728894b03a2bdffddb28277c7327088f
1 /*
2 * Copyright 2010-2013 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5 #ifndef _B_HTTP_FORM_H_
6 #define _B_HTTP_FORM_H_
9 #include <Path.h>
10 #include <String.h>
12 #include <map>
15 enum form_type {
16 B_HTTP_FORM_URL_ENCODED,
17 B_HTTP_FORM_MULTIPART
21 enum form_content_type {
22 B_HTTPFORM_UNKNOWN,
23 B_HTTPFORM_STRING,
24 B_HTTPFORM_FILE,
25 B_HTTPFORM_BUFFER
29 class BHttpFormData {
30 private:
31 // Empty constructor is only kept for compatibility with map<>::operator[],
32 // but never used (see BHttpForm::operator[] which does the necessary
33 // check up)
34 BHttpFormData();
35 #if (__GNUC__ >= 6) || defined(__clang__)
36 friend class std::pair<const BString, BHttpFormData>;
37 #else
38 friend class std::map<BString, BHttpFormData>;
39 #endif
41 public:
42 BHttpFormData(const BString& name,
43 const BString& value);
44 BHttpFormData(const BString& name,
45 const BPath& file);
46 BHttpFormData(const BString& name,
47 const void* buffer, ssize_t size);
48 BHttpFormData(const BHttpFormData& other);
49 ~BHttpFormData();
51 // Retrieve data informations
52 bool InitCheck() const;
54 const BString& Name() const;
55 const BString& String() const;
56 const BPath& File() const;
57 const void* Buffer() const;
58 ssize_t BufferSize() const;
60 bool IsFile() const;
61 const BString& Filename() const;
62 const BString& MimeType() const;
63 form_content_type Type() const;
65 // Change behavior
66 status_t MarkAsFile(const BString& filename,
67 const BString& mimeType = "");
68 void UnmarkAsFile();
69 status_t CopyBuffer();
71 // Overloaded operators
72 BHttpFormData& operator=(const BHttpFormData& other);
74 private:
75 form_content_type fDataType;
76 bool fCopiedBuffer;
77 bool fFileMark;
79 BString fName;
80 BString fStringValue;
81 BPath fPathValue;
82 const void* fBufferValue;
83 ssize_t fBufferSize;
85 BString fFilename;
86 BString fMimeType;
90 class BHttpForm {
91 public:
92 // Nested types
93 class Iterator;
94 typedef std::map<BString, BHttpFormData> FormStorage;
96 public:
97 BHttpForm();
98 BHttpForm(const BHttpForm& other);
99 BHttpForm(const BString& formString);
100 ~BHttpForm();
102 // Form string parsing
103 void ParseString(const BString& formString);
104 BString RawData() const;
106 // Form add
107 status_t AddString(const BString& name,
108 const BString& value);
109 status_t AddInt(const BString& name, int32 value);
110 status_t AddFile(const BString& fieldName,
111 const BPath& file);
112 status_t AddBuffer(const BString& fieldName,
113 const void* buffer, ssize_t size);
114 status_t AddBufferCopy(const BString& fieldName,
115 const void* buffer, ssize_t size);
117 // Mark a field as a filename
118 void MarkAsFile(const BString& fieldName,
119 const BString& filename,
120 const BString& mimeType);
121 void MarkAsFile(const BString& fieldName,
122 const BString& filename);
123 void UnmarkAsFile(const BString& fieldName);
125 // Change form type
126 void SetFormType(form_type type);
128 // Form test
129 bool HasField(const BString& name) const;
131 // Form retrieve
132 BString GetMultipartHeader(const BString& fieldName) const;
133 form_content_type GetType(const BString& fieldname) const;
135 // Form informations
136 form_type GetFormType() const;
137 const BString& GetMultipartBoundary() const;
138 BString GetMultipartFooter() const;
139 ssize_t ContentLength() const;
141 // Form iterator
142 Iterator GetIterator();
144 // Form clear
145 void Clear();
147 // Overloaded operators
148 BHttpFormData& operator[](const BString& name);
150 private:
151 void _ExtractNameValuePair(const BString& string, int32* index);
152 void _GenerateMultipartBoundary();
153 BString _GetMultipartHeader(const BHttpFormData* element) const;
154 form_content_type _GetType(FormStorage::const_iterator it) const;
155 void _Erase(FormStorage::iterator it);
157 private:
158 friend class Iterator;
160 FormStorage fFields;
161 form_type fType;
162 BString fMultipartBoundary;
166 class BHttpForm::Iterator {
167 public:
168 Iterator(const Iterator& other);
170 BHttpFormData* Next();
171 bool HasNext() const;
172 void Remove();
173 BString MultipartHeader();
175 Iterator& operator=(const Iterator& other);
177 private:
178 Iterator(BHttpForm* form);
179 void _FindNext();
181 private:
182 friend class BHttpForm;
184 BHttpForm* fForm;
185 BHttpForm::FormStorage::iterator
186 fStdIterator;
187 BHttpFormData* fElement;
188 BHttpFormData* fPrevElement;
191 #endif // _B_HTTP_FORM_H_