4
// $Id: //poco/1.2/Net/include/Poco/Net/HTMLForm.h#1 $
10
// Definition of the HTMLForm class.
12
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
15
// Permission is hereby granted, free of charge, to any person or organization
16
// obtaining a copy of the software and accompanying documentation covered by
17
// this license (the "Software") to use, reproduce, display, distribute,
18
// execute, and transmit the Software, and to prepare derivative works of the
19
// Software, and to permit third-parties to whom the Software is furnished to
20
// do so, all subject to the following:
22
// The copyright notices in the Software and this entire statement, including
23
// the above license grant, this restriction and the following disclaimer,
24
// must be included in all copies of the Software, in whole or in part, and
25
// all derivative works of the Software, unless such copies or derivative
26
// works are solely in the form of machine-executable object code generated by
27
// a source language processor.
29
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
32
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
33
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
34
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35
// DEALINGS IN THE SOFTWARE.
39
#ifndef Net_HTMLForm_INCLUDED
40
#define Net_HTMLForm_INCLUDED
43
#include "Poco/Net/Net.h"
44
#include "Poco/Net/NameValueCollection.h"
59
class Net_API HTMLForm: public NameValueCollection
60
/// HTMLForm is a helper class for working with HTML forms,
61
/// both on the client and on the server side.
65
/// Creates an empty HTMLForm and sets the
66
/// encoding to "application/x-www-form-urlencoded".
68
explicit HTMLForm(const std::string& encoding);
69
/// Creates an empty HTMLForm that uses
70
/// the given encoding.
72
/// Encoding must be either "application/x-www-form-urlencoded"
73
/// (which is the default) or "multipart/form-data".
75
HTMLForm(const HTTPRequest& request, std::istream& requestBody, PartHandler& handler);
76
/// Creates a HTMLForm from the given HTTP request.
78
/// Uploaded files are passed to the given PartHandler.
80
HTMLForm(const HTTPRequest& request, std::istream& requestBody);
81
/// Creates a HTMLForm from the given HTTP request.
83
/// Uploaded files are silently discarded.
85
explicit HTMLForm(const HTTPRequest& request);
86
/// Creates a HTMLForm from the given HTTP request.
88
/// The request must be a GET request and the form data
89
/// must be in the query string (URL encoded).
91
/// For POST requests, you must use one of the constructors
92
/// taking an additional input stream for the request body.
95
/// Destroys the HTMLForm.
97
void setEncoding(const std::string& encoding);
98
/// Sets the encoding used for posting the form.
100
/// Encoding must be either "application/x-www-form-urlencoded"
101
/// (which is the default) or "multipart/form-data".
103
const std::string& getEncoding() const;
104
/// Returns the encoding used for posting the form.
106
void addPart(const std::string& name, PartSource* pSource);
107
/// Adds an part/attachment (file upload) to the form.
109
/// The form takes ownership of the PartSource and deletes it
110
/// when it is no longer needed.
112
/// The part will only be sent if the encoding
113
/// set for the form is "multipart/form-data"
115
void load(const HTTPRequest& request, std::istream& requestBody, PartHandler& handler);
116
/// Reads the form data from the given HTTP request.
118
/// Uploaded files are passed to the given PartHandler.
120
void load(const HTTPRequest& request, std::istream& requestBody);
121
/// Reads the form data from the given HTTP request.
123
/// Uploaded files are silently discarded.
125
void load(const HTTPRequest& request);
126
/// Reads the form data from the given HTTP request.
128
/// The request must be a GET request and the form data
129
/// must be in the query string (URL encoded).
131
/// For POST requests, you must use one of the overloads
132
/// taking an additional input stream for the request body.
134
void read(std::istream& istr, PartHandler& handler);
135
/// Reads the form data from the given input stream.
137
/// The form data read from the stream must be
138
/// in the encoding specified for the form.
140
void prepareSubmit(HTTPRequest& request);
141
/// Fills out the request object for submitting the form.
143
/// If the request method is GET, the encoded form is appended to the
144
/// request URI as query string. Otherwise (the method is
145
/// POST), the form's content type is set to the form's encoding.
146
/// The form's parameters must be written to the
147
/// request body separately, with a call to write.
148
/// If the request's HTTP version is HTTP/1.0:
149
/// - persistent connections are disabled
150
/// - the content transfer encoding is set to identity encoding
151
/// Otherwise, if the request's HTTP version is HTTP/1.1:
152
/// - the request's persistent connection state is left unchanged
153
/// - the content transfer encoding is set to chunked
155
void write(std::ostream& ostr, const std::string& boundary);
156
/// Writes the form data to the given output stream,
157
/// using the specified encoding.
159
void write(std::ostream& ostr);
160
/// Writes the form data to the given output stream,
161
/// using the specified encoding.
163
const std::string& boundary() const;
164
/// Returns the MIME boundary used for writing
165
/// multipart form data.
167
static const std::string ENCODING_URL; /// "application/x-www-form-urlencoded"
168
static const std::string ENCODING_MULTIPART; /// "multipart/form-data"
171
void readUrl(std::istream& istr);
172
void readMultipart(std::istream& istr, PartHandler& handler);
173
void writeUrl(std::ostream& ostr);
174
void writeMultipart(std::ostream& ostr);
177
HTMLForm(const HTMLForm&);
178
HTMLForm& operator = (const HTMLForm&);
186
typedef std::vector<Part> PartVec;
188
std::string _encoding;
189
std::string _boundary;
197
inline const std::string& HTMLForm::getEncoding() const
203
inline const std::string& HTMLForm::boundary() const
209
} } // namespace Poco::Net
212
#endif // Net_HTMLForm_INCLUDED