haiku/headers/private/netservices/HttpForm.h

204 lines
4.7 KiB
C
Raw Normal View History

/*
2013-11-05 05:16:05 +04:00
* Copyright 2010-2013 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _B_HTTP_FORM_H_
#define _B_HTTP_FORM_H_
#include <Path.h>
#include <String.h>
#include <map>
#ifndef LIBNETAPI_DEPRECATED
namespace BPrivate {
namespace Network {
#endif
enum form_type {
B_HTTP_FORM_URL_ENCODED,
B_HTTP_FORM_MULTIPART
};
enum form_content_type {
B_HTTPFORM_UNKNOWN,
B_HTTPFORM_STRING,
B_HTTPFORM_FILE,
B_HTTPFORM_BUFFER
};
class BHttpFormData {
private:
// Empty constructor is only kept for compatibility with map<>::operator[],
// but never used (see BHttpForm::operator[] which does the necessary
// check up)
BHttpFormData();
2017-12-19 01:58:04 +03:00
#if (__GNUC__ >= 6) || defined(__clang__)
friend class std::pair<const BString, BHttpFormData>;
#else
friend class std::map<BString, BHttpFormData>;
#endif
2013-11-05 05:16:05 +04:00
public:
2013-11-05 05:16:05 +04:00
BHttpFormData(const BString& name,
const BString& value);
BHttpFormData(const BString& name,
const BPath& file);
BHttpFormData(const BString& name,
const void* buffer, ssize_t size);
BHttpFormData(const BHttpFormData& other);
~BHttpFormData();
2013-11-05 05:16:05 +04:00
// Retrieve data informations
bool InitCheck() const;
2013-11-05 05:16:05 +04:00
const BString& Name() const;
const BString& String() const;
const BPath& File() const;
const void* Buffer() const;
ssize_t BufferSize() const;
2013-11-05 05:16:05 +04:00
bool IsFile() const;
const BString& Filename() const;
const BString& MimeType() const;
form_content_type Type() const;
2013-11-05 05:16:05 +04:00
// Change behavior
2013-11-05 05:16:05 +04:00
status_t MarkAsFile(const BString& filename,
const BString& mimeType = "");
void UnmarkAsFile();
status_t CopyBuffer();
2013-11-05 05:16:05 +04:00
// Overloaded operators
BHttpFormData& operator=(const BHttpFormData& other);
private:
form_content_type fDataType;
bool fCopiedBuffer;
bool fFileMark;
2013-11-05 05:16:05 +04:00
BString fName;
BString fStringValue;
BPath fPathValue;
const void* fBufferValue;
ssize_t fBufferSize;
2013-11-05 05:16:05 +04:00
BString fFilename;
BString fMimeType;
};
class BHttpForm {
public:
// Nested types
class Iterator;
typedef std::map<BString, BHttpFormData> FormStorage;
2013-11-05 05:16:05 +04:00
public:
BHttpForm();
BHttpForm(const BHttpForm& other);
BHttpForm(const BString& formString);
~BHttpForm();
2013-11-05 05:16:05 +04:00
// Form string parsing
void ParseString(const BString& formString);
BString RawData() const;
2013-11-05 05:16:05 +04:00
// Form add
2013-11-05 05:16:05 +04:00
status_t AddString(const BString& name,
const BString& value);
status_t AddInt(const BString& name, int32 value);
2013-11-05 05:16:05 +04:00
status_t AddFile(const BString& fieldName,
const BPath& file);
status_t AddBuffer(const BString& fieldName,
const void* buffer, ssize_t size);
status_t AddBufferCopy(const BString& fieldName,
const void* buffer, ssize_t size);
2013-11-05 05:16:05 +04:00
// Mark a field as a filename
void MarkAsFile(const BString& fieldName,
const BString& filename,
const BString& mimeType);
void MarkAsFile(const BString& fieldName,
const BString& filename);
void UnmarkAsFile(const BString& fieldName);
2013-11-05 05:16:05 +04:00
// Change form type
void SetFormType(form_type type);
2013-11-05 05:16:05 +04:00
// Form test
bool HasField(const BString& name) const;
2013-11-05 05:16:05 +04:00
// Form retrieve
2013-11-05 05:16:05 +04:00
BString GetMultipartHeader(const BString& fieldName) const;
form_content_type GetType(const BString& fieldname) const;
2013-11-05 05:16:05 +04:00
// Form informations
form_type GetFormType() const;
const BString& GetMultipartBoundary() const;
BString GetMultipartFooter() const;
ssize_t ContentLength() const;
2013-11-05 05:16:05 +04:00
// Form iterator
Iterator GetIterator();
2013-11-05 05:16:05 +04:00
// Form clear
void Clear();
2013-11-05 05:16:05 +04:00
// Overloaded operators
BHttpFormData& operator[](const BString& name);
2013-11-05 05:16:05 +04:00
private:
2013-11-05 05:16:05 +04:00
void _ExtractNameValuePair(const BString& string, int32* index);
void _GenerateMultipartBoundary();
2013-11-05 05:16:05 +04:00
BString _GetMultipartHeader(const BHttpFormData* element) const;
form_content_type _GetType(FormStorage::const_iterator it) const;
void _Erase(FormStorage::iterator it);
2013-11-05 05:16:05 +04:00
private:
friend class Iterator;
2013-11-05 05:16:05 +04:00
FormStorage fFields;
form_type fType;
BString fMultipartBoundary;
};
class BHttpForm::Iterator {
public:
Iterator(const Iterator& other);
BHttpFormData* Next();
bool HasNext() const;
void Remove();
BString MultipartHeader();
2013-11-05 05:16:05 +04:00
Iterator& operator=(const Iterator& other);
private:
Iterator(BHttpForm* form);
void _FindNext();
private:
2013-11-05 05:16:05 +04:00
friend class BHttpForm;
BHttpForm* fForm;
BHttpForm::FormStorage::iterator
fStdIterator;
BHttpFormData* fElement;
BHttpFormData* fPrevElement;
};
#ifndef LIBNETAPI_DEPRECATED
} // namespace Network
} // namespace BPrivate
#endif
#endif // _B_HTTP_FORM_H_