~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Net/include/Poco/Net/HTMLForm.h

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// HTMLForm.h
 
3
//
 
4
// $Id: //poco/1.2/Net/include/Poco/Net/HTMLForm.h#1 $
 
5
//
 
6
// Library: Net
 
7
// Package: HTML
 
8
// Module:  HTMLForm
 
9
//
 
10
// Definition of the HTMLForm class.
 
11
//
 
12
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
 
13
// and Contributors.
 
14
//
 
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:
 
21
// 
 
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.
 
28
// 
 
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.
 
36
//
 
37
 
 
38
 
 
39
#ifndef Net_HTMLForm_INCLUDED
 
40
#define Net_HTMLForm_INCLUDED
 
41
 
 
42
 
 
43
#include "Poco/Net/Net.h"
 
44
#include "Poco/Net/NameValueCollection.h"
 
45
#include <ostream>
 
46
#include <istream>
 
47
#include <vector>
 
48
 
 
49
 
 
50
namespace Poco {
 
51
namespace Net {
 
52
 
 
53
 
 
54
class HTTPRequest;
 
55
class PartHandler;
 
56
class PartSource;
 
57
 
 
58
 
 
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.
 
62
{
 
63
public:
 
64
        HTMLForm();
 
65
                /// Creates an empty HTMLForm and sets the
 
66
                /// encoding to "application/x-www-form-urlencoded".
 
67
                
 
68
        explicit HTMLForm(const std::string& encoding);
 
69
                /// Creates an empty HTMLForm that uses
 
70
                /// the given encoding.
 
71
                ///
 
72
                /// Encoding must be either "application/x-www-form-urlencoded"
 
73
                /// (which is the default) or "multipart/form-data".
 
74
                
 
75
        HTMLForm(const HTTPRequest& request, std::istream& requestBody, PartHandler& handler);
 
76
                /// Creates a HTMLForm from the given HTTP request.
 
77
                ///
 
78
                /// Uploaded files are passed to the given PartHandler.
 
79
 
 
80
        HTMLForm(const HTTPRequest& request, std::istream& requestBody);
 
81
                /// Creates a HTMLForm from the given HTTP request.
 
82
                ///
 
83
                /// Uploaded files are silently discarded.
 
84
 
 
85
        explicit HTMLForm(const HTTPRequest& request);
 
86
                /// Creates a HTMLForm from the given HTTP request.
 
87
                ///
 
88
                /// The request must be a GET request and the form data
 
89
                /// must be in the query string (URL encoded).
 
90
                ///
 
91
                /// For POST requests, you must use one of the constructors
 
92
                /// taking an additional input stream for the request body.
 
93
                
 
94
        ~HTMLForm();
 
95
                /// Destroys the HTMLForm.
 
96
 
 
97
        void setEncoding(const std::string& encoding);
 
98
                /// Sets the encoding used for posting the form.
 
99
                ///
 
100
                /// Encoding must be either "application/x-www-form-urlencoded"
 
101
                /// (which is the default) or "multipart/form-data".
 
102
                
 
103
        const std::string& getEncoding() const;
 
104
                /// Returns the encoding used for posting the form.
 
105
 
 
106
        void addPart(const std::string& name, PartSource* pSource);
 
107
                /// Adds an part/attachment (file upload) to the form.
 
108
                ///
 
109
                /// The form takes ownership of the PartSource and deletes it
 
110
                /// when it is no longer needed.
 
111
                ///
 
112
                /// The part will only be sent if the encoding
 
113
                /// set for the form is "multipart/form-data"
 
114
 
 
115
        void load(const HTTPRequest& request, std::istream& requestBody, PartHandler& handler);
 
116
                /// Reads the form data from the given HTTP request.
 
117
                ///
 
118
                /// Uploaded files are passed to the given PartHandler.
 
119
 
 
120
        void load(const HTTPRequest& request, std::istream& requestBody);
 
121
                /// Reads the form data from the given HTTP request.
 
122
                ///
 
123
                /// Uploaded files are silently discarded.
 
124
 
 
125
        void load(const HTTPRequest& request);
 
126
                /// Reads the form data from the given HTTP request.
 
127
                ///
 
128
                /// The request must be a GET request and the form data
 
129
                /// must be in the query string (URL encoded).
 
130
                ///
 
131
                /// For POST requests, you must use one of the overloads
 
132
                /// taking an additional input stream for the request body.
 
133
 
 
134
        void read(std::istream& istr, PartHandler& handler);
 
135
                /// Reads the form data from the given input stream.
 
136
                ///
 
137
                /// The form data read from the stream must be
 
138
                /// in the encoding specified for the form.
 
139
                
 
140
        void prepareSubmit(HTTPRequest& request);
 
141
                /// Fills out the request object for submitting the form.
 
142
                ///
 
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
 
154
                
 
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.
 
158
 
 
159
        void write(std::ostream& ostr);
 
160
                /// Writes the form data to the given output stream,
 
161
                /// using the specified encoding.
 
162
 
 
163
        const std::string& boundary() const;
 
164
                /// Returns the MIME boundary used for writing
 
165
                /// multipart form data.
 
166
 
 
167
        static const std::string ENCODING_URL;       /// "application/x-www-form-urlencoded"
 
168
        static const std::string ENCODING_MULTIPART; /// "multipart/form-data"
 
169
 
 
170
protected:
 
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);
 
175
 
 
176
private:
 
177
        HTMLForm(const HTMLForm&);
 
178
        HTMLForm& operator = (const HTMLForm&);
 
179
 
 
180
        struct Part
 
181
        {
 
182
                std::string name;
 
183
                PartSource* pSource;
 
184
        };
 
185
        
 
186
        typedef std::vector<Part> PartVec;
 
187
        
 
188
        std::string _encoding;
 
189
        std::string _boundary;
 
190
        PartVec     _parts;
 
191
};
 
192
 
 
193
 
 
194
//
 
195
// inlines
 
196
//
 
197
inline const std::string& HTMLForm::getEncoding() const
 
198
{
 
199
        return _encoding;
 
200
}
 
201
 
 
202
 
 
203
inline const std::string& HTMLForm::boundary() const
 
204
{
 
205
        return _boundary;
 
206
}
 
207
 
 
208
 
 
209
} } // namespace Poco::Net
 
210
 
 
211
 
 
212
#endif // Net_HTMLForm_INCLUDED