|
16 | 16 |
|
17 | 17 | package com.xkcoding.http.util;
|
18 | 18 |
|
| 19 | +import com.xkcoding.http.constants.Constants; |
19 | 20 | import lombok.experimental.UtilityClass;
|
20 | 21 |
|
21 |
| -import java.util.ArrayList; |
22 |
| -import java.util.ConcurrentModificationException; |
23 |
| -import java.util.List; |
24 |
| -import java.util.Map; |
| 22 | +import java.util.*; |
25 | 23 | import java.util.function.BiConsumer;
|
26 | 24 |
|
27 | 25 | /**
|
@@ -98,4 +96,103 @@ public String parseMapToString(Map<String, String> params, boolean encode) {
|
98 | 96 | });
|
99 | 97 | return String.join("&", paramList);
|
100 | 98 | }
|
| 99 | + |
| 100 | + /** |
| 101 | + * 字符串转map,字符串格式为 {@code xxx=xxx&xxx=xxx} |
| 102 | + * |
| 103 | + * @param str 待转换的字符串 |
| 104 | + * @param decode 是否解码 |
| 105 | + * @return map |
| 106 | + */ |
| 107 | + public Map<String, String> parseStringToMap(String str, boolean decode) { |
| 108 | + str = preProcess(str); |
| 109 | + |
| 110 | + Map<String, String> params = new HashMap<>(16); |
| 111 | + if (StringUtil.isEmpty(str)) { |
| 112 | + return params; |
| 113 | + } |
| 114 | + |
| 115 | + if (!str.contains("&")) { |
| 116 | + params.put(decode(str, decode), Constants.EMPTY); |
| 117 | + return params; |
| 118 | + } |
| 119 | + |
| 120 | + final int len = str.length(); |
| 121 | + String name = null; |
| 122 | + // 未处理字符开始位置 |
| 123 | + int pos = 0; |
| 124 | + // 未处理字符结束位置 |
| 125 | + int i; |
| 126 | + // 当前字符 |
| 127 | + char c; |
| 128 | + for (i = 0; i < len; i++) { |
| 129 | + c = str.charAt(i); |
| 130 | + // 键值对的分界点 |
| 131 | + if (c == '=') { |
| 132 | + if (null == name) { |
| 133 | + // name可以是"" |
| 134 | + name = str.substring(pos, i); |
| 135 | + } |
| 136 | + pos = i + 1; |
| 137 | + } |
| 138 | + // 参数对的分界点 |
| 139 | + else if (c == '&') { |
| 140 | + if (null == name && pos != i) { |
| 141 | + // 对于像&a&这类无参数值的字符串,我们将name为a的值设为"" |
| 142 | + addParam(params, str.substring(pos, i), Constants.EMPTY, decode); |
| 143 | + } else if (name != null) { |
| 144 | + addParam(params, name, str.substring(pos, i), decode); |
| 145 | + name = null; |
| 146 | + } |
| 147 | + pos = i + 1; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // 处理结尾 |
| 152 | + if (pos != i) { |
| 153 | + if (name == null) { |
| 154 | + addParam(params, str.substring(pos, i), Constants.EMPTY, decode); |
| 155 | + } else { |
| 156 | + addParam(params, name, str.substring(pos, i), decode); |
| 157 | + } |
| 158 | + } else if (name != null) { |
| 159 | + addParam(params, name, Constants.EMPTY, decode); |
| 160 | + } |
| 161 | + |
| 162 | + return params; |
| 163 | + } |
| 164 | + |
| 165 | + private void addParam(Map<String, String> params, String key, String value, boolean decode) { |
| 166 | + key = decode(key, decode); |
| 167 | + value = decode(value, decode); |
| 168 | + if (params.containsKey(key)) { |
| 169 | + params.put(key, params.get(key) + "," + value); |
| 170 | + } else { |
| 171 | + params.put(key, value); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + private String decode(String str, boolean decode) { |
| 176 | + return decode ? UrlUtil.urlDecode(str) : str; |
| 177 | + } |
| 178 | + |
| 179 | + |
| 180 | + private String preProcess(String str) { |
| 181 | + if (StringUtil.isEmpty(str)) { |
| 182 | + return str; |
| 183 | + } |
| 184 | + // 去除 URL 路径信息 |
| 185 | + int beginPos = str.indexOf("?"); |
| 186 | + if (beginPos > -1) { |
| 187 | + str = str.substring(beginPos + 1); |
| 188 | + } |
| 189 | + |
| 190 | + // 去除 # 后面的内容 |
| 191 | + int endPos = str.indexOf("#"); |
| 192 | + if (endPos > -1) { |
| 193 | + str = str.substring(0, endPos); |
| 194 | + } |
| 195 | + |
| 196 | + return str; |
| 197 | + } |
101 | 198 | }
|
0 commit comments