1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 : #ifndef nsIFormSubmission_h___
38 : #define nsIFormSubmission_h___
39 :
40 : #include "nsISupports.h"
41 : #include "nsString.h"
42 : #include "nsCOMPtr.h"
43 :
44 : class nsIURI;
45 : class nsIInputStream;
46 : class nsGenericHTMLElement;
47 : class nsILinkHandler;
48 : class nsIContent;
49 : class nsIFormControl;
50 : class nsIDOMHTMLElement;
51 : class nsIDocShell;
52 : class nsIRequest;
53 : class nsISaveAsCharset;
54 : class nsIMultiplexInputStream;
55 : class nsIDOMBlob;
56 :
57 : /**
58 : * Class for form submissions; encompasses the function to call to submit as
59 : * well as the form submission name/value pairs
60 : */
61 : class nsFormSubmission
62 : {
63 : public:
64 0 : virtual ~nsFormSubmission()
65 0 : {
66 0 : MOZ_COUNT_DTOR(nsFormSubmission);
67 0 : }
68 :
69 : /**
70 : * Submit a name/value pair
71 : *
72 : * @param aName the name of the parameter
73 : * @param aValue the value of the parameter
74 : */
75 : virtual nsresult AddNameValuePair(const nsAString& aName,
76 : const nsAString& aValue) = 0;
77 :
78 : /**
79 : * Submit a name/file pair
80 : *
81 : * @param aName the name of the parameter
82 : * @param aBlob the file to submit
83 : */
84 : virtual nsresult AddNameFilePair(const nsAString& aName,
85 : nsIDOMBlob* aBlob) = 0;
86 :
87 : /**
88 : * Reports whether the instance supports AddIsindex().
89 : *
90 : * @return true if supported.
91 : */
92 0 : virtual bool SupportsIsindexSubmission()
93 : {
94 0 : return false;
95 : }
96 :
97 : /**
98 : * Adds an isindex value to the submission.
99 : *
100 : * @param aValue the field value
101 : */
102 0 : virtual nsresult AddIsindex(const nsAString& aValue)
103 : {
104 0 : NS_NOTREACHED("AddIsindex called when not supported");
105 0 : return NS_ERROR_UNEXPECTED;
106 : }
107 :
108 : /**
109 : * Given a URI and the current submission, create the final URI and data
110 : * stream that will be submitted. Subclasses *must* implement this.
111 : *
112 : * @param aURI the URI being submitted to [INOUT]
113 : * @param aPostDataStream a data stream for POST data [OUT]
114 : */
115 : virtual nsresult GetEncodedSubmission(nsIURI* aURI,
116 : nsIInputStream** aPostDataStream) = 0;
117 :
118 : /**
119 : * Get the charset that will be used for submission.
120 : */
121 0 : void GetCharset(nsACString& aCharset)
122 : {
123 0 : aCharset = mCharset;
124 0 : }
125 :
126 0 : nsIContent* GetOriginatingElement() const
127 : {
128 0 : return mOriginatingElement.get();
129 : }
130 :
131 : protected:
132 : /**
133 : * Can only be constructed by subclasses.
134 : *
135 : * @param aCharset the charset of the form as a string
136 : * @param aOriginatingElement the originating element (can be null)
137 : */
138 0 : nsFormSubmission(const nsACString& aCharset, nsIContent* aOriginatingElement)
139 : : mCharset(aCharset)
140 0 : , mOriginatingElement(aOriginatingElement)
141 : {
142 0 : MOZ_COUNT_CTOR(nsFormSubmission);
143 0 : }
144 :
145 : // The name of the encoder charset
146 : nsCString mCharset;
147 :
148 : // Originating element.
149 : nsCOMPtr<nsIContent> mOriginatingElement;
150 : };
151 :
152 : class nsEncodingFormSubmission : public nsFormSubmission
153 : {
154 : public:
155 : nsEncodingFormSubmission(const nsACString& aCharset,
156 : nsIContent* aOriginatingElement);
157 :
158 : virtual ~nsEncodingFormSubmission();
159 :
160 : /**
161 : * Encode a Unicode string to bytes using the encoder (or just copy the input
162 : * if there is no encoder).
163 : * @param aStr the string to encode
164 : * @param aResult the encoded string [OUT]
165 : * @param aHeaderEncode If true, turns all linebreaks into spaces and escapes
166 : * all quotes
167 : * @throws an error if UnicodeToNewBytes fails
168 : */
169 : nsresult EncodeVal(const nsAString& aStr, nsCString& aResult,
170 : bool aHeaderEncode);
171 :
172 : private:
173 : // The encoder that will encode Unicode names and values
174 : nsCOMPtr<nsISaveAsCharset> mEncoder;
175 : };
176 :
177 : /**
178 : * Handle multipart/form-data encoding, which does files as well as normal
179 : * inputs. This always does POST.
180 : */
181 : class nsFSMultipartFormData : public nsEncodingFormSubmission
182 : {
183 : public:
184 : /**
185 : * @param aCharset the charset of the form as a string
186 : */
187 : nsFSMultipartFormData(const nsACString& aCharset,
188 : nsIContent* aOriginatingElement);
189 : ~nsFSMultipartFormData();
190 :
191 : virtual nsresult AddNameValuePair(const nsAString& aName,
192 : const nsAString& aValue);
193 : virtual nsresult AddNameFilePair(const nsAString& aName,
194 : nsIDOMBlob* aBlob);
195 : virtual nsresult GetEncodedSubmission(nsIURI* aURI,
196 : nsIInputStream** aPostDataStream);
197 :
198 0 : void GetContentType(nsACString& aContentType)
199 : {
200 : aContentType =
201 0 : NS_LITERAL_CSTRING("multipart/form-data; boundary=") + mBoundary;
202 0 : }
203 :
204 : nsIInputStream* GetSubmissionBody();
205 :
206 : protected:
207 :
208 : /**
209 : * Roll up the data we have so far and add it to the multiplexed data stream.
210 : */
211 : nsresult AddPostDataStream();
212 :
213 : private:
214 : /**
215 : * The post data stream as it is so far. This is a collection of smaller
216 : * chunks--string streams and file streams interleaved to make one big POST
217 : * stream.
218 : */
219 : nsCOMPtr<nsIMultiplexInputStream> mPostDataStream;
220 :
221 : /**
222 : * The current string chunk. When a file is hit, the string chunk gets
223 : * wrapped up into an input stream and put into mPostDataStream so that the
224 : * file input stream can then be appended and everything is in the right
225 : * order. Then the string chunk gets appended to again as we process more
226 : * name/value pairs.
227 : */
228 : nsCString mPostDataChunk;
229 :
230 : /**
231 : * The boundary string to use after each "part" (the boundary that marks the
232 : * end of a value). This is computed randomly and is different for each
233 : * submission.
234 : */
235 : nsCString mBoundary;
236 : };
237 :
238 : /**
239 : * Get a submission object based on attributes in the form (ENCTYPE and METHOD)
240 : *
241 : * @param aForm the form to get a submission object based on
242 : * @param aOriginatingElement the originating element (can be null)
243 : * @param aFormSubmission the form submission object (out param)
244 : */
245 : nsresult GetSubmissionFromForm(nsGenericHTMLElement* aForm,
246 : nsGenericHTMLElement* aOriginatingElement,
247 : nsFormSubmission** aFormSubmission);
248 :
249 : #endif /* nsIFormSubmission_h___ */
|