Added URLDecoder class (Unicode not supported)
This commit is contained in:
parent
29e684e117
commit
bf235694eb
4 changed files with 75 additions and 1 deletions
BIN
Zutil.jar
BIN
Zutil.jar
Binary file not shown.
|
|
@ -24,6 +24,8 @@
|
|||
|
||||
package zutil.net.http;
|
||||
|
||||
import zutil.parser.URLDecoder;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
|
@ -49,7 +51,7 @@ public class HttpHeaderParser {
|
|||
private HashMap<String, String> cookies;
|
||||
|
||||
/**
|
||||
* Parses the HTTP header information from the stream
|
||||
* Parses the HTTP header information from a stream
|
||||
*
|
||||
* @param in is the stream
|
||||
* @throws IOException
|
||||
|
|
@ -147,6 +149,7 @@ public class HttpHeaderParser {
|
|||
*/
|
||||
public static void parseURLParameters(String attributes, HashMap<String, String> map){
|
||||
String[] tmp;
|
||||
attributes = URLDecoder.decode(attributes);
|
||||
// get the variables
|
||||
String[] data = andPattern.split( attributes );
|
||||
for(String element : data){
|
||||
|
|
|
|||
36
src/zutil/parser/URLDecoder.java
Executable file
36
src/zutil/parser/URLDecoder.java
Executable file
|
|
@ -0,0 +1,36 @@
|
|||
package zutil.parser;
|
||||
|
||||
/**
|
||||
* This utility class will decode Strings encoded with % sign's to a normal String
|
||||
*
|
||||
* Created by Ziver on 2015-12-11.
|
||||
*/
|
||||
public class URLDecoder {
|
||||
|
||||
public static String decode(String url){
|
||||
if(url == null)
|
||||
return null;
|
||||
StringBuilder out = new StringBuilder();
|
||||
for (int i=0; i<url.length(); ++i) {
|
||||
char c = url.charAt(i);
|
||||
switch (c){
|
||||
case '+':
|
||||
out.append(' ');
|
||||
break;
|
||||
case '%':
|
||||
if (i+2 < url.length()) {
|
||||
char ascii = (char)Integer.parseInt("" + url.charAt(i + 1) + url.charAt(i + 2), 16);
|
||||
out.append(ascii);
|
||||
i += 2;
|
||||
}
|
||||
else
|
||||
out.append(c);
|
||||
break;
|
||||
default:
|
||||
out.append(c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
}
|
||||
35
test/zutil/test/URLDecoderTest.java
Executable file
35
test/zutil/test/URLDecoderTest.java
Executable file
|
|
@ -0,0 +1,35 @@
|
|||
package zutil.test;
|
||||
|
||||
import org.junit.Test;
|
||||
import zutil.parser.URLDecoder;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
||||
/**
|
||||
* Created by ezivkoc on 2015-12-11.
|
||||
*/
|
||||
public class URLDecoderTest {
|
||||
|
||||
@Test
|
||||
public void simpleTest(){
|
||||
assertEquals(null, URLDecoder.decode(null));
|
||||
assertEquals("", URLDecoder.decode(""));
|
||||
assertEquals("space space", URLDecoder.decode("space space"));
|
||||
assertEquals("space space", URLDecoder.decode("space+space"));
|
||||
assertEquals("space space", URLDecoder.decode("space%20space"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void percentTest(){
|
||||
assertEquals("test+", URLDecoder.decode("test%2B"));
|
||||
assertEquals("test%2", URLDecoder.decode("test%2"));
|
||||
assertEquals("test+test", URLDecoder.decode("test%2Btest"));
|
||||
assertEquals("test+test", URLDecoder.decode("test%2btest"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void percentMultibyteTest(){
|
||||
assertEquals("Ängen", URLDecoder.decode("%C3%84ngen"));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue