mirror of
https://github.com/apache/httpcomponents-client.git
synced 2025-03-03 15:19:14 +00:00
Optimized URLEncodedUtils#parse(HttpEntity) method
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1302360 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
3f99045e4f
commit
5e0e2efe13
@ -38,6 +38,7 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.apache.http.annotation.Immutable;
|
||||
import org.apache.http.entity.ContentType;
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HeaderElement;
|
||||
@ -98,32 +99,15 @@ public static List <NameValuePair> parse (final URI uri, final String encoding)
|
||||
*/
|
||||
public static List <NameValuePair> parse (
|
||||
final HttpEntity entity) throws IOException {
|
||||
List <NameValuePair> result = Collections.emptyList();
|
||||
|
||||
String contentType = null;
|
||||
String charset = null;
|
||||
|
||||
Header h = entity.getContentType();
|
||||
if (h != null) {
|
||||
HeaderElement[] elems = h.getElements();
|
||||
if (elems.length > 0) {
|
||||
HeaderElement elem = elems[0];
|
||||
contentType = elem.getName();
|
||||
NameValuePair param = elem.getParameterByName("charset");
|
||||
if (param != null) {
|
||||
charset = param.getValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (contentType != null && contentType.equalsIgnoreCase(CONTENT_TYPE)) {
|
||||
final String content = EntityUtils.toString(entity, HTTP.ASCII);
|
||||
List <NameValuePair> result = null;
|
||||
ContentType contentType = ContentType.get(entity);
|
||||
if (contentType != null && contentType.getMimeType().equalsIgnoreCase(CONTENT_TYPE)) {
|
||||
String content = EntityUtils.toString(entity, HTTP.ASCII);
|
||||
if (content != null && content.length() > 0) {
|
||||
result = new ArrayList <NameValuePair>();
|
||||
parse(result, new Scanner(content), charset);
|
||||
result = parse(content, contentType.getCharset());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return result != null ? result : new ArrayList<NameValuePair>();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -188,12 +172,12 @@ public static void parse (
|
||||
*
|
||||
* @param s
|
||||
* text to parse.
|
||||
* @param encoding
|
||||
* @param charset
|
||||
* Encoding to use when decoding the parameters.
|
||||
*
|
||||
* @since 4.2
|
||||
*/
|
||||
public static List<NameValuePair> parse (final String s, final String encoding) {
|
||||
public static List<NameValuePair> parse (final String s, final String charset) {
|
||||
if (s == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
@ -206,8 +190,8 @@ public static List<NameValuePair> parse (final String s, final String encoding)
|
||||
NameValuePair nvp = parser.parseNameValuePair(buffer, cursor, DELIM);
|
||||
if (nvp.getName().length() > 0) {
|
||||
list.add(new BasicNameValuePair(
|
||||
decode(nvp.getName(), encoding),
|
||||
decode(nvp.getValue(), encoding)));
|
||||
decode(nvp.getName(), charset),
|
||||
decode(nvp.getValue(), charset)));
|
||||
}
|
||||
}
|
||||
return list;
|
||||
|
Loading…
x
Reference in New Issue
Block a user