2008-11-19 08:22:15 +01:00
|
|
|
/*
|
2012-06-04 22:31:44 +02:00
|
|
|
* Copyright 2012 The Netty Project
|
2009-06-19 19:48:17 +02:00
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
2008-11-19 08:22:15 +01:00
|
|
|
*
|
2012-06-04 22:31:44 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-11-19 08:22:15 +01:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
2011-12-09 06:18:34 +01:00
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2009-08-28 09:15:49 +02:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2011-12-09 04:38:59 +01:00
|
|
|
package io.netty.handler.codec.http;
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2017-02-27 10:49:40 +01:00
|
|
|
import io.netty.util.internal.ObjectUtil;
|
|
|
|
|
2009-02-26 07:34:07 +01:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2008-11-19 08:22:15 +01:00
|
|
|
import java.net.URI;
|
|
|
|
import java.net.URISyntaxException;
|
2009-02-26 07:34:07 +01:00
|
|
|
import java.net.URLEncoder;
|
2009-12-29 06:52:00 +01:00
|
|
|
import java.nio.charset.Charset;
|
2009-02-26 07:34:07 +01:00
|
|
|
import java.nio.charset.UnsupportedCharsetException;
|
2008-11-19 08:22:15 +01:00
|
|
|
|
|
|
|
/**
|
2009-06-19 16:49:26 +02:00
|
|
|
* Creates an URL-encoded URI from a path string and key-value parameter pairs.
|
|
|
|
* This encoder is for one time use only. Create a new instance for each URI.
|
|
|
|
*
|
2009-06-19 17:05:47 +02:00
|
|
|
* <pre>
|
2012-01-08 21:54:31 +01:00
|
|
|
* {@link QueryStringEncoder} encoder = new {@link QueryStringEncoder}("/hello");
|
2009-06-19 17:05:47 +02:00
|
|
|
* encoder.addParam("recipient", "world");
|
|
|
|
* assert encoder.toString().equals("/hello?recipient=world");
|
|
|
|
* </pre>
|
|
|
|
* @see QueryStringDecoder
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
|
|
|
public class QueryStringEncoder {
|
|
|
|
|
2017-06-21 08:40:12 +02:00
|
|
|
private final String charsetName;
|
|
|
|
private final StringBuilder uriBuilder;
|
|
|
|
private boolean hasParams;
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2009-06-19 16:49:26 +02:00
|
|
|
/**
|
|
|
|
* Creates a new encoder that encodes a URI that starts with the specified
|
|
|
|
* path string. The encoder will encode the URI in UTF-8.
|
|
|
|
*/
|
2009-02-26 07:34:07 +01:00
|
|
|
public QueryStringEncoder(String uri) {
|
2012-05-31 20:32:42 +02:00
|
|
|
this(uri, HttpConstants.DEFAULT_CHARSET);
|
2009-02-26 07:34:07 +01:00
|
|
|
}
|
|
|
|
|
2009-06-19 16:49:26 +02:00
|
|
|
/**
|
|
|
|
* Creates a new encoder that encodes a URI that starts with the specified
|
|
|
|
* path string in the specified charset.
|
|
|
|
*/
|
2009-12-29 06:52:00 +01:00
|
|
|
public QueryStringEncoder(String uri, Charset charset) {
|
2017-06-21 08:40:12 +02:00
|
|
|
uriBuilder = new StringBuilder(uri);
|
|
|
|
charsetName = charset.name();
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
|
|
|
|
2009-06-19 16:49:26 +02:00
|
|
|
/**
|
|
|
|
* Adds a parameter with the specified name and value to this encoder.
|
|
|
|
*/
|
2008-11-19 08:22:15 +01:00
|
|
|
public void addParam(String name, String value) {
|
2017-02-27 10:49:40 +01:00
|
|
|
ObjectUtil.checkNotNull(name, "name");
|
2017-06-21 08:40:12 +02:00
|
|
|
if (hasParams) {
|
|
|
|
uriBuilder.append('&');
|
|
|
|
} else {
|
|
|
|
uriBuilder.append('?');
|
|
|
|
hasParams = true;
|
|
|
|
}
|
|
|
|
appendComponent(name, charsetName, uriBuilder);
|
|
|
|
if (value != null) {
|
|
|
|
uriBuilder.append('=');
|
|
|
|
appendComponent(value, charsetName, uriBuilder);
|
|
|
|
}
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
|
|
|
|
2009-06-19 16:49:26 +02:00
|
|
|
/**
|
|
|
|
* Returns the URL-encoded URI object which was created from the path string
|
|
|
|
* specified in the constructor and the parameters added by
|
2017-02-27 10:49:40 +01:00
|
|
|
* {@link #addParam(String, String)} method.
|
2009-06-19 16:49:26 +02:00
|
|
|
*/
|
2008-11-19 08:22:15 +01:00
|
|
|
public URI toUri() throws URISyntaxException {
|
2008-12-01 06:29:59 +01:00
|
|
|
return new URI(toString());
|
|
|
|
}
|
|
|
|
|
2009-06-19 16:49:26 +02:00
|
|
|
/**
|
|
|
|
* Returns the URL-encoded URI which was created from the path string
|
|
|
|
* specified in the constructor and the parameters added by
|
2017-02-27 10:49:40 +01:00
|
|
|
* {@link #addParam(String, String)} method.
|
2009-06-19 16:49:26 +02:00
|
|
|
*/
|
2008-12-01 06:29:59 +01:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2017-06-21 08:40:12 +02:00
|
|
|
return uriBuilder.toString();
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
|
|
|
|
2017-06-21 08:40:12 +02:00
|
|
|
private static void appendComponent(String s, String charset, StringBuilder sb) {
|
2009-02-26 07:34:07 +01:00
|
|
|
try {
|
2017-06-21 08:40:12 +02:00
|
|
|
s = URLEncoder.encode(s, charset);
|
2014-06-24 10:39:46 +02:00
|
|
|
} catch (UnsupportedEncodingException ignored) {
|
2017-06-21 08:40:12 +02:00
|
|
|
throw new UnsupportedCharsetException(charset);
|
2009-02-26 07:34:07 +01:00
|
|
|
}
|
2017-06-21 08:40:12 +02:00
|
|
|
// replace all '+' with "%20"
|
|
|
|
int idx = s.indexOf('+');
|
|
|
|
if (idx == -1) {
|
|
|
|
sb.append(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sb.append(s, 0, idx).append("%20");
|
|
|
|
int size = s.length();
|
|
|
|
idx++;
|
|
|
|
for (; idx < size; idx++) {
|
|
|
|
char c = s.charAt(idx);
|
|
|
|
if (c != '+') {
|
|
|
|
sb.append(c);
|
|
|
|
} else {
|
|
|
|
sb.append("%20");
|
|
|
|
}
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|