1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2001 Dirk Mueller (mueller@kde.org)
* Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
* (C) 2006 Alexey Proskuryakov (ap@nypop.com)
* Copyright (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "FormDataBuilder.h"
#include "Blob.h"
#include "Document.h"
#include "TextEncoding.h"
#include <limits>
#include <wtf/Assertions.h>
#include <wtf/HexNumber.h>
#include <wtf/text/CString.h>
#include <wtf/RandomNumber.h>
namespace WebCore {
// Helper functions
static inline void append(Vector<char>& buffer, char string)
{
buffer.append(string);
}
static inline void append(Vector<char>& buffer, const char* string)
{
buffer.append(string, strlen(string));
}
static inline void append(Vector<char>& buffer, const CString& string)
{
buffer.append(string.data(), string.length());
}
static void appendQuotedString(Vector<char>& buffer, const CString& string)
{
// Append a string as a quoted value, escaping quotes and line breaks.
// FIXME: Is it correct to use percent escaping here? Other browsers do not encode these characters yet,
// so we should test popular servers to find out if there is an encoding form they can handle.
size_t length = string.length();
for (size_t i = 0; i < length; ++i) {
char c = string.data()[i];
switch (c) {
case 0x0a:
append(buffer, "%0A");
break;
case 0x0d:
append(buffer, "%0D");
break;
case '"':
append(buffer, "%22");
break;
default:
append(buffer, c);
}
}
}
TextEncoding FormDataBuilder::encodingFromAcceptCharset(const String& acceptCharset, Document* document)
{
String normalizedAcceptCharset = acceptCharset;
normalizedAcceptCharset.replace(',', ' ');
Vector<String> charsets;
normalizedAcceptCharset.split(' ', charsets);
TextEncoding encoding;
Vector<String>::const_iterator end = charsets.end();
for (Vector<String>::const_iterator it = charsets.begin(); it != end; ++it) {
if ((encoding = TextEncoding(*it)).isValid())
return encoding;
}
return document->inputEncoding();
}
Vector<char> FormDataBuilder::generateUniqueBoundaryString()
{
Vector<char> boundary;
// The RFC 2046 spec says the alphanumeric characters plus the
// following characters are legal for boundaries: '()+_,-./:=?
// However the following characters, though legal, cause some sites
// to fail: (),./:=+
// Note that our algorithm makes it twice as much likely for 'A' or 'B'
// to appear in the boundary string, because 0x41 and 0x42 are present in
// the below array twice.
static const char alphaNumericEncodingMap[64] = {
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E,
0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42
};
// Start with an informative prefix.
append(boundary, "----WebKitFormBoundary");
// Append 16 random 7bit ascii AlphaNumeric characters.
Vector<char> randomBytes;
for (unsigned i = 0; i < 4; ++i) {
unsigned randomness = static_cast<unsigned>(randomNumber() * (std::numeric_limits<unsigned>::max() + 1.0));
randomBytes.append(alphaNumericEncodingMap[(randomness >> 24) & 0x3F]);
randomBytes.append(alphaNumericEncodingMap[(randomness >> 16) & 0x3F]);
randomBytes.append(alphaNumericEncodingMap[(randomness >> 8) & 0x3F]);
randomBytes.append(alphaNumericEncodingMap[randomness & 0x3F]);
}
boundary.appendVector(randomBytes);
boundary.append(0); // Add a 0 at the end so we can use this as a C-style string.
return boundary;
}
void FormDataBuilder::beginMultiPartHeader(Vector<char>& buffer, const CString& boundary, const CString& name)
{
addBoundaryToMultiPartHeader(buffer, boundary);
// FIXME: This loses data irreversibly if the input name includes characters you can't encode
// in the website's character set.
append(buffer, "Content-Disposition: form-data; name=\"");
appendQuotedString(buffer, name);
append(buffer, '"');
}
void FormDataBuilder::addBoundaryToMultiPartHeader(Vector<char>& buffer, const CString& boundary, bool isLastBoundary)
{
append(buffer, "--");
append(buffer, boundary);
if (isLastBoundary)
append(buffer, "--");
append(buffer, "\r\n");
}
void FormDataBuilder::addFilenameToMultiPartHeader(Vector<char>& buffer, const TextEncoding& encoding, const String& filename)
{
// FIXME: This loses data irreversibly if the filename includes characters you can't encode
// in the website's character set.
append(buffer, "; filename=\"");
appendQuotedString(buffer, encoding.encode(filename.characters(), filename.length(), QuestionMarksForUnencodables));
append(buffer, '"');
}
void FormDataBuilder::addContentTypeToMultiPartHeader(Vector<char>& buffer, const CString& mimeType)
{
ASSERT(Blob::isNormalizedContentType(mimeType));
append(buffer, "\r\nContent-Type: ");
append(buffer, mimeType);
}
void FormDataBuilder::finishMultiPartHeader(Vector<char>& buffer)
{
append(buffer, "\r\n\r\n");
}
void FormDataBuilder::addKeyValuePairAsFormData(Vector<char>& buffer, const CString& key, const CString& value, FormData::EncodingType encodingType)
{
if (encodingType == FormData::TextPlain) {
if (!buffer.isEmpty())
append(buffer, "\r\n");
append(buffer, key);
append(buffer, '=');
append(buffer, value);
} else {
if (!buffer.isEmpty())
append(buffer, '&');
encodeStringAsFormData(buffer, key);
append(buffer, '=');
encodeStringAsFormData(buffer, value);
}
}
void FormDataBuilder::encodeStringAsFormData(Vector<char>& buffer, const CString& string)
{
// Same safe characters as Netscape for compatibility.
static const char safeCharacters[] = "-._*";
// http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
unsigned length = string.length();
for (unsigned i = 0; i < length; ++i) {
unsigned char c = string.data()[i];
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || strchr(safeCharacters, c))
append(buffer, c);
else if (c == ' ')
append(buffer, '+');
else if (c == '\n' || (c == '\r' && (i + 1 >= length || string.data()[i + 1] != '\n')))
append(buffer, "%0D%0A");
else if (c != '\r') {
append(buffer, '%');
appendByteAsHex(c, buffer);
}
}
}
}
|