OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions |
| 6 * are met: |
| 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright |
| 11 * notice, this list of conditions and the following disclaimer in the |
| 12 * documentation and/or other materials provided with the distribution. |
| 13 * |
| 14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ |
| 25 |
| 26 |
| 27 #include "config.h" |
| 28 #include "core/dom/URLQuery.h" |
| 29 |
| 30 #include "weborigin/KURL.h" |
| 31 #include "wtf/text/StringBuilder.h" |
| 32 |
| 33 namespace WebCore { |
| 34 |
| 35 static String decode(String& input) |
| 36 { |
| 37 return decodeURLEscapeSequences(input.replace('+', ' ')); |
| 38 } |
| 39 |
| 40 static Vector<pair<String, String> > parseQuery(const String& input) |
| 41 { |
| 42 Vector<pair<String, String> > result; |
| 43 unsigned length = input.length(); |
| 44 unsigned lastEqual = 0; |
| 45 unsigned lastAmpersand = 0; |
| 46 |
| 47 for (unsigned i = 0; i <= length; ++i) { |
| 48 if (i == length || input[i] == '&') { |
| 49 if (lastAmpersand == i) { |
| 50 // do nothing |
| 51 } else if (lastAmpersand == lastEqual) { |
| 52 String name = input.substring(lastAmpersand, i - lastAmpersand); |
| 53 result.append(std::make_pair(decode(name), String())); |
| 54 } else { |
| 55 String name = input.substring(lastAmpersand, lastEqual - lastAmp
ersand - 1); |
| 56 String value = input.substring(lastEqual, i - lastEqual); |
| 57 result.append(std::make_pair(decode(name), decode(value))); |
| 58 } |
| 59 lastAmpersand = i + 1; |
| 60 lastEqual = i + 1; |
| 61 } else if (input[i] == '=' && lastEqual == lastAmpersand) { |
| 62 lastEqual = i + 1; |
| 63 } |
| 64 } |
| 65 |
| 66 return result; |
| 67 } |
| 68 |
| 69 URLQuery::URLQuery(const String& init) : m_entries(parseQuery(init)) { } |
| 70 |
| 71 URLQuery::URLQuery(PassRefPtr<URLQuery> init) : m_entries(init->m_entries) { } |
| 72 |
| 73 void URLQuery::append(const String& name, const String& value) |
| 74 { |
| 75 m_entries.append(std::make_pair(name, value)); |
| 76 } |
| 77 |
| 78 void URLQuery::deleteFunction(const String& name) |
| 79 { |
| 80 for (int i = m_entries.size() - 1; i >= 0; --i) { |
| 81 if (m_entries[i].first == name) |
| 82 m_entries.remove(i); |
| 83 } |
| 84 } |
| 85 |
| 86 String URLQuery::get(const String& name) |
| 87 { |
| 88 for (int i = 0; i < m_entries.size(); ++i) { |
| 89 if (m_entries[i].first == name) |
| 90 return m_entries[i].second; |
| 91 } |
| 92 return String(); |
| 93 } |
| 94 |
| 95 const Vector<String> URLQuery::getAll(const String& name) |
| 96 { |
| 97 Vector<String> result; |
| 98 for (int i = 0; i < m_entries.size(); ++i) { |
| 99 if (m_entries[i].first == name) |
| 100 result.append(m_entries[i].second); |
| 101 } |
| 102 return result; |
| 103 } |
| 104 |
| 105 bool URLQuery::has(const String& name) |
| 106 { |
| 107 for (int i = 0; i < m_entries.size(); ++i) { |
| 108 if (m_entries[i].first == name) |
| 109 return true; |
| 110 } |
| 111 return false; |
| 112 } |
| 113 |
| 114 void URLQuery::set(const String& name, const String& value) |
| 115 { |
| 116 bool found = false; |
| 117 for (int i = 0; i < m_entries.size(); ++i) { |
| 118 if (m_entries[i].first == name) { |
| 119 if (found) { |
| 120 m_entries.remove(i); |
| 121 } else { |
| 122 m_entries[i].second = value; |
| 123 found = true; |
| 124 } |
| 125 } |
| 126 } |
| 127 |
| 128 if (!found) |
| 129 append(name, value); |
| 130 } |
| 131 |
| 132 static String encode(String input) |
| 133 { |
| 134 return encodeWithURLEscapeSequences(input).replace("%20", "+"); |
| 135 } |
| 136 |
| 137 String URLQuery::toString() const |
| 138 { |
| 139 StringBuilder builder; |
| 140 for (int i = 0; i < m_entries.size(); ++i) { |
| 141 if (i) |
| 142 builder.append('&'); |
| 143 builder.append(encode(m_entries[i].first)); |
| 144 builder.append('='); |
| 145 builder.append(encode(m_entries[i].second)); |
| 146 } |
| 147 return builder.toString(); |
| 148 } |
| 149 |
| 150 } // namespace WebCore |
OLD | NEW |