Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMikhail Mazursky2013-11-02 08:44:36 +0000
committerMikhail Mazursky2013-11-02 08:44:36 +0000
commit6b0269a16d7ee923ae70082eb60595d432861707 (patch)
treefebef6ee712c505c99f95a8a059bf4d7eb64c688 /jetty-http
parent56fcfa45fb642825db38b8197467a63ee2aeffd6 (diff)
downloadorg.eclipse.jetty.project-6b0269a16d7ee923ae70082eb60595d432861707.tar.gz
org.eclipse.jetty.project-6b0269a16d7ee923ae70082eb60595d432861707.tar.xz
org.eclipse.jetty.project-6b0269a16d7ee923ae70082eb60595d432861707.zip
[Bug 420930] Use Charset to specify character encoding
Signed-off-by: Mikhail Mazursky <mikhail.mazursky@gmail.com>
Diffstat (limited to 'jetty-http')
-rw-r--r--jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java5
-rw-r--r--jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java5
-rw-r--r--jetty-http/src/main/java/org/eclipse/jetty/http/HttpTester.java3
-rw-r--r--jetty-http/src/main/java/org/eclipse/jetty/http/HttpURI.java37
-rw-r--r--jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java11
5 files changed, 37 insertions, 24 deletions
diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java
index 8241d1d62f..81af8228ed 100644
--- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java
+++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java
@@ -19,6 +19,7 @@
package org.eclipse.jetty.http;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Set;
@@ -176,7 +177,7 @@ public class HttpField
private static byte[] toSanitisedName(String s)
{
- byte[] bytes = s.getBytes(StringUtil.__ISO_8859_1_CHARSET);
+ byte[] bytes = s.getBytes(StandardCharsets.ISO_8859_1);
for (int i=bytes.length;i-->0;)
{
switch(bytes[i])
@@ -192,7 +193,7 @@ public class HttpField
private static byte[] toSanitisedValue(String s)
{
- byte[] bytes = s.getBytes(StringUtil.__ISO_8859_1_CHARSET);
+ byte[] bytes = s.getBytes(StandardCharsets.ISO_8859_1);
for (int i=bytes.length;i-->0;)
{
switch(bytes[i])
diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java
index db4642957d..7810666d78 100644
--- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java
+++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java
@@ -19,6 +19,7 @@
package org.eclipse.jetty.http;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import org.eclipse.jetty.http.HttpTokens.EndOfContent;
import org.eclipse.jetty.util.ArrayTernaryTrie;
@@ -969,12 +970,12 @@ public class HttpParser
// Have to get the fields exactly from the buffer to match case
String fn=field.getName();
String fv=field.getValue();
- n=BufferUtil.toString(buffer,buffer.position()-1,fn.length(),StringUtil.__US_ASCII_CHARSET);
+ n=BufferUtil.toString(buffer,buffer.position()-1,fn.length(),StandardCharsets.US_ASCII);
if (fv==null)
v=null;
else
{
- v=BufferUtil.toString(buffer,buffer.position()+fn.length()+1,fv.length(),StringUtil.__ISO_8859_1_CHARSET);
+ v=BufferUtil.toString(buffer,buffer.position()+fn.length()+1,fv.length(),StandardCharsets.ISO_8859_1);
field=new HttpField(field.getHeader(),n,v);
}
}
diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpTester.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpTester.java
index 0170d3e204..ee4801f1f5 100644
--- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpTester.java
+++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpTester.java
@@ -22,6 +22,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import org.eclipse.jetty.http.HttpGenerator.RequestInfo;
import org.eclipse.jetty.http.HttpGenerator.ResponseInfo;
@@ -346,7 +347,7 @@ public class HttpTester
String content_type=get(HttpHeader.CONTENT_TYPE);
String encoding=MimeTypes.getCharsetFromContentType(content_type);
- Charset charset=encoding==null?StringUtil.__UTF8_CHARSET:Charset.forName(encoding);
+ Charset charset=encoding==null?StandardCharsets.UTF_8:Charset.forName(encoding);
return new String(bytes,charset);
}
diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpURI.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpURI.java
index dcd00cf217..47101268f3 100644
--- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpURI.java
+++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpURI.java
@@ -21,6 +21,7 @@ package org.eclipse.jetty.http;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import org.eclipse.jetty.util.MultiMap;
import org.eclipse.jetty.util.StringUtil;
@@ -100,15 +101,7 @@ public class HttpURI
public HttpURI(String raw)
{
_rawString=raw;
- byte[] b;
- try
- {
- b = raw.getBytes(StringUtil.__UTF8);
- }
- catch (UnsupportedEncodingException e)
- {
- throw new RuntimeException(e.getMessage());
- }
+ byte[] b = raw.getBytes(StandardCharsets.UTF_8);
parse(b,0,b.length);
_charset = URIUtil.__CHARSET;
}
@@ -617,9 +610,14 @@ public class HttpURI
return StringUtil.toUTF8String(_raw, _path, _param-_path);
return utf8b.toString();
}
-
+
public String getDecodedPath(String encoding)
{
+ return getDecodedPath(Charset.forName(encoding));
+ }
+
+ public String getDecodedPath(Charset encoding)
+ {
if (_path==_param)
return null;
@@ -678,9 +676,9 @@ public class HttpURI
if (bytes==null)
- return StringUtil.toString(_raw,_path,_param-_path,encoding);
+ return new String(_raw,_path,_param-_path,encoding);
- return StringUtil.toString(bytes,0,n,encoding);
+ return new String(bytes,0,n,encoding);
}
public String getPathAndParam()
@@ -734,10 +732,10 @@ public class HttpURI
{
if (_query==_fragment)
return;
- if (_charset==StringUtil.__UTF8_CHARSET)
+ if (_charset.equals(StandardCharsets.UTF_8))
UrlEncoded.decodeUtf8To(_raw,_query+1,_fragment-_query-1,parameters);
else
- UrlEncoded.decodeTo(StringUtil.toString(_raw,_query+1,_fragment-_query-1,_charset.toString()),parameters,_charset.toString(),-1);
+ UrlEncoded.decodeTo(new String(_raw,_query+1,_fragment-_query-1,_charset),parameters,_charset,-1);
}
public void decodeQueryTo(MultiMap<String> parameters, String encoding) throws UnsupportedEncodingException
@@ -751,6 +749,17 @@ public class HttpURI
UrlEncoded.decodeTo(StringUtil.toString(_raw,_query+1,_fragment-_query-1,encoding),parameters,encoding,-1);
}
+ public void decodeQueryTo(MultiMap<String> parameters, Charset encoding) throws UnsupportedEncodingException
+ {
+ if (_query==_fragment)
+ return;
+
+ if (encoding==null || StandardCharsets.UTF_8.equals(encoding))
+ UrlEncoded.decodeUtf8To(_raw,_query+1,_fragment-_query-1,parameters);
+ else
+ UrlEncoded.decodeTo(new String(_raw,_query+1,_fragment-_query-1,encoding),parameters,encoding,-1);
+ }
+
public void clear()
{
_scheme=_authority=_host=_port=_path=_param=_query=_fragment=_end=0;
diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java
index 0c88ca55a5..9f105caa2f 100644
--- a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java
+++ b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@@ -112,7 +113,7 @@ public class HttpParserTest
@Test
public void testLineParse3() throws Exception
{
- ByteBuffer buffer= BufferUtil.toBuffer("POST /fo\u0690 HTTP/1.0\015\012" + "\015\012",StringUtil.__UTF8_CHARSET);
+ ByteBuffer buffer= BufferUtil.toBuffer("POST /fo\u0690 HTTP/1.0\015\012" + "\015\012",StandardCharsets.UTF_8);
HttpParser.RequestHandler<ByteBuffer> handler = new Handler();
HttpParser parser= new HttpParser(handler);
@@ -126,7 +127,7 @@ public class HttpParserTest
@Test
public void testLineParse4() throws Exception
{
- ByteBuffer buffer= BufferUtil.toBuffer("POST /foo?param=\u0690 HTTP/1.0\015\012" + "\015\012",StringUtil.__UTF8_CHARSET);
+ ByteBuffer buffer= BufferUtil.toBuffer("POST /foo?param=\u0690 HTTP/1.0\015\012" + "\015\012",StandardCharsets.UTF_8);
HttpParser.RequestHandler<ByteBuffer> handler = new Handler();
HttpParser parser= new HttpParser(handler);
@@ -321,10 +322,10 @@ public class HttpParserTest
ByteBuffer buffer=BufferUtil.allocate(4096);
BufferUtil.flipToFill(buffer);
BufferUtil.put(BufferUtil.toBuffer("GET "),buffer);
- buffer.put("/foo/\u0690/".getBytes(StringUtil.__UTF8_CHARSET));
+ buffer.put("/foo/\u0690/".getBytes(StandardCharsets.UTF_8));
BufferUtil.put(BufferUtil.toBuffer(" HTTP/1.0\r\n"),buffer);
BufferUtil.put(BufferUtil.toBuffer("Header1: "),buffer);
- buffer.put("\u00e6 \u00e6".getBytes(StringUtil.__ISO_8859_1_CHARSET));
+ buffer.put("\u00e6 \u00e6".getBytes(StandardCharsets.ISO_8859_1));
BufferUtil.put(BufferUtil.toBuffer(" \r\n\r\n"),buffer);
BufferUtil.flipToFlush(buffer,0);
@@ -1165,7 +1166,7 @@ public class HttpParserTest
{
if (_content==null)
_content="";
- String c = BufferUtil.toString(ref,StringUtil.__UTF8_CHARSET);
+ String c = BufferUtil.toString(ref,StandardCharsets.UTF_8);
//System.err.println("content '"+c+"'");
_content= _content + c;
ref.position(ref.limit());

Back to the top