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