Return-Path: Delivered-To: apmail-hc-commits-archive@www.apache.org Received: (qmail 29564 invoked from network); 8 Mar 2008 23:21:39 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 8 Mar 2008 23:21:39 -0000 Received: (qmail 73606 invoked by uid 500); 8 Mar 2008 23:21:36 -0000 Delivered-To: apmail-hc-commits-archive@hc.apache.org Received: (qmail 73574 invoked by uid 500); 8 Mar 2008 23:21:36 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 73564 invoked by uid 99); 8 Mar 2008 23:21:36 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 08 Mar 2008 15:21:36 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 08 Mar 2008 23:20:56 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 7B7EE1A9832; Sat, 8 Mar 2008 15:21:07 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r635111 - in /httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client: methods/UrlEncodedFormEntity.java utils/URLUtils.java Date: Sat, 08 Mar 2008 23:21:07 -0000 To: commits@hc.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080308232107.7B7EE1A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: olegk Date: Sat Mar 8 15:21:03 2008 New Revision: 635111 URL: http://svn.apache.org/viewvc?rev=635111&view=rev Log: HTTPCLIENT-746: Alternative URL coding methods Contributed by Stojce Dimski Reviewed by Oleg Kalnichevski Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/methods/UrlEncodedFormEntity.java httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/methods/UrlEncodedFormEntity.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/methods/UrlEncodedFormEntity.java?rev=635111&r1=635110&r2=635111&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/methods/UrlEncodedFormEntity.java (original) +++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/methods/UrlEncodedFormEntity.java Sat Mar 8 15:21:03 2008 @@ -30,69 +30,45 @@ package org.apache.http.client.methods; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.io.UnsupportedEncodingException; +import java.util.List; +import java.util.Map; -import org.apache.http.Header; import org.apache.http.NameValuePair; import org.apache.http.client.utils.URLUtils; -import org.apache.http.entity.AbstractHttpEntity; -import org.apache.http.message.BasicHeader; +import org.apache.http.entity.StringEntity; import org.apache.http.protocol.HTTP; -import org.apache.http.util.EncodingUtils; -public class UrlEncodedFormEntity extends AbstractHttpEntity { +public class UrlEncodedFormEntity extends StringEntity { /** The Content-Type for www-form-urlencoded. */ public static final String FORM_URL_ENCODED_CONTENT_TYPE = "application/x-www-form-urlencoded"; - private final byte[] content; - public UrlEncodedFormEntity( final NameValuePair[] fields, final String charset) throws UnsupportedEncodingException { - super(); - String s = URLUtils.formUrlEncode(fields, charset); - this.content = EncodingUtils.getAsciiBytes(s); + super(URLUtils.formUrlEncode(fields, charset), charset); + setContentType(FORM_URL_ENCODED_CONTENT_TYPE); } - public UrlEncodedFormEntity(final NameValuePair[] fields) { - super(); - String s = URLUtils.simpleFormUrlEncode(fields, HTTP.UTF_8); - this.content = EncodingUtils.getAsciiBytes(s); + public UrlEncodedFormEntity( + final NameValuePair[] fields) throws UnsupportedEncodingException { + super(URLUtils.formUrlEncode(fields, HTTP.UTF_8), HTTP.US_ASCII); + setContentType(FORM_URL_ENCODED_CONTENT_TYPE); } - public boolean isRepeatable() { - return true; + public UrlEncodedFormEntity ( + final Map> parameters, + final String charset) throws UnsupportedEncodingException { + super(URLUtils.format(parameters, charset), HTTP.US_ASCII); + setContentType(FORM_URL_ENCODED_CONTENT_TYPE); } - public long getContentLength() { - return this.content.length; - } - - public InputStream getContent() throws IOException { - return new ByteArrayInputStream(this.content); + public UrlEncodedFormEntity ( + final Map> parameters) throws UnsupportedEncodingException { + super(URLUtils.format(parameters, HTTP.UTF_8), HTTP.US_ASCII); + setContentType(FORM_URL_ENCODED_CONTENT_TYPE); } - @Override - public Header getContentType() { - return new BasicHeader(HTTP.CONTENT_TYPE, FORM_URL_ENCODED_CONTENT_TYPE); - } - - public boolean isStreaming() { - return false; - } - - public void writeTo(final OutputStream outstream) throws IOException { - if (outstream == null) { - throw new IllegalArgumentException("Output stream may not be null"); - } - outstream.write(this.content); - outstream.flush(); - } - } Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java?rev=635111&r1=635110&r2=635111&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java (original) +++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java Sat Mar 8 15:21:03 2008 @@ -32,10 +32,18 @@ import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Scanner; +import java.util.TreeMap; -import org.apache.commons.codec.net.URLCodec; import org.apache.http.HttpHost; import org.apache.http.NameValuePair; +import org.apache.http.protocol.HTTP; /** * The home for utility methods that handle various URL encoding tasks. @@ -49,6 +57,8 @@ /** Default content encoding chatset */ private static final String DEFAULT_CHARSET = "ISO-8859-1"; + private static final String PARAMETER_SEPARATOR = "&"; + private static final String NAME_VALUE_SEPARATOR = "="; /** * Form-urlencoding routine. @@ -109,23 +119,81 @@ final String charset) throws UnsupportedEncodingException { StringBuilder buf = new StringBuilder(); for (int i = 0; i < pairs.length; i++) { - URLCodec codec = new URLCodec(); NameValuePair pair = pairs[i]; if (pair.getName() != null) { if (i > 0) { buf.append("&"); } - buf.append(codec.encode(pair.getName(), charset)); + buf.append(URLEncoder.encode(pair.getName(), charset)); buf.append("="); if (pair.getValue() != null) { - buf.append(codec.encode(pair.getValue(), charset)); + buf.append(URLEncoder.encode(pair.getValue(), charset)); } } } return buf.toString(); } - public static URI createURI( + public static Map > parse ( + final URI uri, + String charset) throws UnsupportedEncodingException { + Map > result = Collections.emptyMap(); + final String query = uri.getRawQuery(); + if (query != null && query.length() > 0) { + result = new TreeMap >(); + parse(result, new Scanner(query), charset); + } + return result; + } + + public static void parse ( + final Map > result, + final Scanner scanner, String charset) throws UnsupportedEncodingException { + if (charset == null) { + charset = HTTP.DEFAULT_CONTENT_CHARSET; + } + scanner.useDelimiter(PARAMETER_SEPARATOR); + while (scanner.hasNext()) { + final String[] nameValue = scanner.next().split(NAME_VALUE_SEPARATOR); + if (nameValue.length == 0 || nameValue.length > 2) + throw new IllegalArgumentException("bad parameter"); + final String name = URLDecoder.decode(nameValue[0], charset); + if (nameValue.length == 2) { + if (!result.containsKey(name)) + result.put(name, new LinkedList ()); + String value = null; + final List values = result.get(name); + value = URLDecoder.decode(nameValue[1], charset); + values.add(value); + } + } + } + + public static String format ( + final Map > parameters, + String charset) throws UnsupportedEncodingException { + if (charset == null) { + charset = HTTP.DEFAULT_CONTENT_CHARSET; + } + final StringBuilder result = new StringBuilder(64); + for (final String name : parameters.keySet()) { + final List values = parameters.get(name); + if (values != null) { + final String encodedName = URLEncoder.encode(name, charset); + for (final String value : values) { + if (result.length() > 0) + result.append(PARAMETER_SEPARATOR); + final String encodedValue = URLEncoder.encode(value, charset); + result.append(encodedName); + result.append(NAME_VALUE_SEPARATOR); + result.append(encodedValue); + } + } + } + return result.toString(); + } + + public static URI createURI( final String scheme, final String host, int port,