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_ 7 8 9 #include <Path.h> 10 #include <String.h> 11 12 #include <map> 13 14 15 #ifndef LIBNETAPI_DEPRECATED 16 namespace BPrivate { 17 18 namespace Network { 19 #endif 20 21 enum form_type { 22 B_HTTP_FORM_URL_ENCODED, 23 B_HTTP_FORM_MULTIPART 24 }; 25 26 27 enum form_content_type { 28 B_HTTPFORM_UNKNOWN, 29 B_HTTPFORM_STRING, 30 B_HTTPFORM_FILE, 31 B_HTTPFORM_BUFFER 32 }; 33 34 35 class BHttpFormData { 36 private: 37 // Empty constructor is only kept for compatibility with map<>::operator[], 38 // but never used (see BHttpForm::operator[] which does the necessary 39 // check up) 40 BHttpFormData(); 41 #if (__GNUC__ >= 6) || defined(__clang__) 42 friend class std::pair<const BString, BHttpFormData>; 43 #else 44 friend class std::map<BString, BHttpFormData>; 45 #endif 46 47 public: 48 BHttpFormData(const BString& name, 49 const BString& value); 50 BHttpFormData(const BString& name, 51 const BPath& file); 52 BHttpFormData(const BString& name, 53 const void* buffer, ssize_t size); 54 BHttpFormData(const BHttpFormData& other); 55 ~BHttpFormData(); 56 57 // Retrieve data informations 58 bool InitCheck() const; 59 60 const BString& Name() const; 61 const BString& String() const; 62 const BPath& File() const; 63 const void* Buffer() const; 64 ssize_t BufferSize() const; 65 66 bool IsFile() const; 67 const BString& Filename() const; 68 const BString& MimeType() const; 69 form_content_type Type() const; 70 71 // Change behavior 72 status_t MarkAsFile(const BString& filename, 73 const BString& mimeType = ""); 74 void UnmarkAsFile(); 75 status_t CopyBuffer(); 76 77 // Overloaded operators 78 BHttpFormData& operator=(const BHttpFormData& other); 79 80 private: 81 form_content_type fDataType; 82 bool fCopiedBuffer; 83 bool fFileMark; 84 85 BString fName; 86 BString fStringValue; 87 BPath fPathValue; 88 const void* fBufferValue; 89 ssize_t fBufferSize; 90 91 BString fFilename; 92 BString fMimeType; 93 }; 94 95 96 class BHttpForm { 97 public: 98 // Nested types 99 class Iterator; 100 typedef std::map<BString, BHttpFormData> FormStorage; 101 102 public: 103 BHttpForm(); 104 BHttpForm(const BHttpForm& other); 105 BHttpForm(const BString& formString); 106 ~BHttpForm(); 107 108 // Form string parsing 109 void ParseString(const BString& formString); 110 BString RawData() const; 111 112 // Form add 113 status_t AddString(const BString& name, 114 const BString& value); 115 status_t AddInt(const BString& name, int32 value); 116 status_t AddFile(const BString& fieldName, 117 const BPath& file); 118 status_t AddBuffer(const BString& fieldName, 119 const void* buffer, ssize_t size); 120 status_t AddBufferCopy(const BString& fieldName, 121 const void* buffer, ssize_t size); 122 123 // Mark a field as a filename 124 void MarkAsFile(const BString& fieldName, 125 const BString& filename, 126 const BString& mimeType); 127 void MarkAsFile(const BString& fieldName, 128 const BString& filename); 129 void UnmarkAsFile(const BString& fieldName); 130 131 // Change form type 132 void SetFormType(form_type type); 133 134 // Form test 135 bool HasField(const BString& name) const; 136 137 // Form retrieve 138 BString GetMultipartHeader(const BString& fieldName) const; 139 form_content_type GetType(const BString& fieldname) const; 140 141 // Form informations 142 form_type GetFormType() const; 143 const BString& GetMultipartBoundary() const; 144 BString GetMultipartFooter() const; 145 ssize_t ContentLength() const; 146 147 // Form iterator 148 Iterator GetIterator(); 149 150 // Form clear 151 void Clear(); 152 153 // Overloaded operators 154 BHttpFormData& operator[](const BString& name); 155 156 private: 157 void _ExtractNameValuePair(const BString& string, int32* index); 158 void _GenerateMultipartBoundary(); 159 BString _GetMultipartHeader(const BHttpFormData* element) const; 160 form_content_type _GetType(FormStorage::const_iterator it) const; 161 void _Erase(FormStorage::iterator it); 162 163 private: 164 friend class Iterator; 165 166 FormStorage fFields; 167 form_type fType; 168 BString fMultipartBoundary; 169 }; 170 171 172 class BHttpForm::Iterator { 173 public: 174 Iterator(const Iterator& other); 175 176 BHttpFormData* Next(); 177 bool HasNext() const; 178 void Remove(); 179 BString MultipartHeader(); 180 181 Iterator& operator=(const Iterator& other); 182 183 private: 184 Iterator(BHttpForm* form); 185 void _FindNext(); 186 187 private: 188 friend class BHttpForm; 189 190 BHttpForm* fForm; 191 BHttpForm::FormStorage::iterator 192 fStdIterator; 193 BHttpFormData* fElement; 194 BHttpFormData* fPrevElement; 195 }; 196 197 #ifndef LIBNETAPI_DEPRECATED 198 } // namespace Network 199 200 } // namespace BPrivate 201 #endif 202 203 #endif // _B_HTTP_FORM_H_ 204