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