root / trunk / tbeta / OSX / libs / poco / include / Poco / Net / HTMLForm.h @ 5

View | Annotate | Download (6.6 KB)

1 5 ss
//
2 5 ss
// HTMLForm.h
3 5 ss
//
4 5 ss
// $Id: //poco/1.3/Net/include/Poco/Net/HTMLForm.h#1 $
5 5 ss
//
6 5 ss
// Library: Net
7 5 ss
// Package: HTML
8 5 ss
// Module:  HTMLForm
9 5 ss
//
10 5 ss
// Definition of the HTMLForm class.
11 5 ss
//
12 5 ss
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
13 5 ss
// and Contributors.
14 5 ss
//
15 5 ss
// Permission is hereby granted, free of charge, to any person or organization
16 5 ss
// obtaining a copy of the software and accompanying documentation covered by
17 5 ss
// this license (the "Software") to use, reproduce, display, distribute,
18 5 ss
// execute, and transmit the Software, and to prepare derivative works of the
19 5 ss
// Software, and to permit third-parties to whom the Software is furnished to
20 5 ss
// do so, all subject to the following:
21 5 ss
//
22 5 ss
// The copyright notices in the Software and this entire statement, including
23 5 ss
// the above license grant, this restriction and the following disclaimer,
24 5 ss
// must be included in all copies of the Software, in whole or in part, and
25 5 ss
// all derivative works of the Software, unless such copies or derivative
26 5 ss
// works are solely in the form of machine-executable object code generated by
27 5 ss
// a source language processor.
28 5 ss
//
29 5 ss
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 5 ss
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 5 ss
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
32 5 ss
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
33 5 ss
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
34 5 ss
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35 5 ss
// DEALINGS IN THE SOFTWARE.
36 5 ss
//
37 5 ss
38 5 ss
39 5 ss
#ifndef Net_HTMLForm_INCLUDED
40 5 ss
#define Net_HTMLForm_INCLUDED
41 5 ss
42 5 ss
43 5 ss
#include "Poco/Net/Net.h"
44 5 ss
#include "Poco/Net/NameValueCollection.h"
45 5 ss
#include <ostream>
46 5 ss
#include <istream>
47 5 ss
#include <vector>
48 5 ss
49 5 ss
50 5 ss
namespace Poco {
51 5 ss
namespace Net {
52 5 ss
53 5 ss
54 5 ss
class HTTPRequest;
55 5 ss
class PartHandler;
56 5 ss
class PartSource;
57 5 ss
58 5 ss
59 5 ss
class Net_API HTMLForm: public NameValueCollection
60 5 ss
        /// HTMLForm is a helper class for working with HTML forms,
61 5 ss
        /// both on the client and on the server side.
62 5 ss
{
63 5 ss
public:
64 5 ss
        HTMLForm();
65 5 ss
                /// Creates an empty HTMLForm and sets the
66 5 ss
                /// encoding to "application/x-www-form-urlencoded".
67 5 ss
68 5 ss
        explicit HTMLForm(const std::string& encoding);
69 5 ss
                /// Creates an empty HTMLForm that uses
70 5 ss
                /// the given encoding.
71 5 ss
                ///
72 5 ss
                /// Encoding must be either "application/x-www-form-urlencoded"
73 5 ss
                /// (which is the default) or "multipart/form-data".
74 5 ss
75 5 ss
        HTMLForm(const HTTPRequest& request, std::istream& requestBody, PartHandler& handler);
76 5 ss
                /// Creates a HTMLForm from the given HTTP request.
77 5 ss
                ///
78 5 ss
                /// Uploaded files are passed to the given PartHandler.
79 5 ss
80 5 ss
        HTMLForm(const HTTPRequest& request, std::istream& requestBody);
81 5 ss
                /// Creates a HTMLForm from the given HTTP request.
82 5 ss
                ///
83 5 ss
                /// Uploaded files are silently discarded.
84 5 ss
85 5 ss
        explicit HTMLForm(const HTTPRequest& request);
86 5 ss
                /// Creates a HTMLForm from the given HTTP request.
87 5 ss
                ///
88 5 ss
                /// The request must be a GET request and the form data
89 5 ss
                /// must be in the query string (URL encoded).
90 5 ss
                ///
91 5 ss
                /// For POST requests, you must use one of the constructors
92 5 ss
                /// taking an additional input stream for the request body.
93 5 ss
94 5 ss
        ~HTMLForm();
95 5 ss
                /// Destroys the HTMLForm.
96 5 ss
97 5 ss
        void setEncoding(const std::string& encoding);
98 5 ss
                /// Sets the encoding used for posting the form.
99 5 ss
                ///
100 5 ss
                /// Encoding must be either "application/x-www-form-urlencoded"
101 5 ss
                /// (which is the default) or "multipart/form-data".
102 5 ss
103 5 ss
        const std::string& getEncoding() const;
104 5 ss
                /// Returns the encoding used for posting the form.
105 5 ss
106 5 ss
        void addPart(const std::string& name, PartSource* pSource);
107 5 ss
                /// Adds an part/attachment (file upload) to the form.
108 5 ss
                ///
109 5 ss
                /// The form takes ownership of the PartSource and deletes it
110 5 ss
                /// when it is no longer needed.
111 5 ss
                ///
112 5 ss
                /// The part will only be sent if the encoding
113 5 ss
                /// set for the form is "multipart/form-data"
114 5 ss
115 5 ss
        void load(const HTTPRequest& request, std::istream& requestBody, PartHandler& handler);
116 5 ss
                /// Reads the form data from the given HTTP request.
117 5 ss
                ///
118 5 ss
                /// Uploaded files are passed to the given PartHandler.
119 5 ss
120 5 ss
        void load(const HTTPRequest& request, std::istream& requestBody);
121 5 ss
                /// Reads the form data from the given HTTP request.
122 5 ss
                ///
123 5 ss
                /// Uploaded files are silently discarded.
124 5 ss
125 5 ss
        void load(const HTTPRequest& request);
126 5 ss
                /// Reads the form data from the given HTTP request.
127 5 ss
                ///
128 5 ss
                /// The request must be a GET request and the form data
129 5 ss
                /// must be in the query string (URL encoded).
130 5 ss
                ///
131 5 ss
                /// For POST requests, you must use one of the overloads
132 5 ss
                /// taking an additional input stream for the request body.
133 5 ss
134 5 ss
        void read(std::istream& istr, PartHandler& handler);
135 5 ss
                /// Reads the form data from the given input stream.
136 5 ss
                ///
137 5 ss
                /// The form data read from the stream must be
138 5 ss
                /// in the encoding specified for the form.
139 5 ss
140 5 ss
        void prepareSubmit(HTTPRequest& request);
141 5 ss
                /// Fills out the request object for submitting the form.
142 5 ss
                ///
143 5 ss
                /// If the request method is GET, the encoded form is appended to the
144 5 ss
                /// request URI as query string. Otherwise (the method is
145 5 ss
                /// POST), the form's content type is set to the form's encoding.
146 5 ss
                /// The form's parameters must be written to the
147 5 ss
                /// request body separately, with a call to write.
148 5 ss
                /// If the request's HTTP version is HTTP/1.0:
149 5 ss
                ///    - persistent connections are disabled
150 5 ss
                ///    - the content transfer encoding is set to identity encoding
151 5 ss
                /// Otherwise, if the request's HTTP version is HTTP/1.1:
152 5 ss
                ///    - the request's persistent connection state is left unchanged
153 5 ss
                ///    - the content transfer encoding is set to chunked
154 5 ss
155 5 ss
        void write(std::ostream& ostr, const std::string& boundary);
156 5 ss
                /// Writes the form data to the given output stream,
157 5 ss
                /// using the specified encoding.
158 5 ss
159 5 ss
        void write(std::ostream& ostr);
160 5 ss
                /// Writes the form data to the given output stream,
161 5 ss
                /// using the specified encoding.
162 5 ss
163 5 ss
        const std::string& boundary() const;
164 5 ss
                /// Returns the MIME boundary used for writing
165 5 ss
                /// multipart form data.
166 5 ss
167 5 ss
        static const std::string ENCODING_URL;       /// "application/x-www-form-urlencoded"
168 5 ss
        static const std::string ENCODING_MULTIPART; /// "multipart/form-data"
169 5 ss
170 5 ss
protected:
171 5 ss
        void readUrl(std::istream& istr);
172 5 ss
        void readMultipart(std::istream& istr, PartHandler& handler);
173 5 ss
        void writeUrl(std::ostream& ostr);
174 5 ss
        void writeMultipart(std::ostream& ostr);
175 5 ss
176 5 ss
private:
177 5 ss
        HTMLForm(const HTMLForm&);
178 5 ss
        HTMLForm& operator = (const HTMLForm&);
179 5 ss
180 5 ss
        struct Part
181 5 ss
        {
182 5 ss
                std::string name;
183 5 ss
                PartSource* pSource;
184 5 ss
        };
185 5 ss
186 5 ss
        typedef std::vector<Part> PartVec;
187 5 ss
188 5 ss
        std::string _encoding;
189 5 ss
        std::string _boundary;
190 5 ss
        PartVec     _parts;
191 5 ss
};
192 5 ss
193 5 ss
194 5 ss
//
195 5 ss
// inlines
196 5 ss
//
197 5 ss
inline const std::string& HTMLForm::getEncoding() const
198 5 ss
{
199 5 ss
        return _encoding;
200 5 ss
}
201 5 ss
202 5 ss
203 5 ss
inline const std::string& HTMLForm::boundary() const
204 5 ss
{
205 5 ss
        return _boundary;
206 5 ss
}
207 5 ss
208 5 ss
209 5 ss
} } // namespace Poco::Net
210 5 ss
211 5 ss
212 5 ss
#endif // Net_HTMLForm_INCLUDED