Small refactoring

This commit is contained in:
Ziver Koc 2016-08-08 17:30:58 +02:00
parent 123044aaf3
commit 1a6edff347
5 changed files with 25 additions and 22 deletions

View file

@ -40,7 +40,6 @@ public class HttpHeaderParser {
private static final Pattern PATTERN_COLON = Pattern.compile(":"); private static final Pattern PATTERN_COLON = Pattern.compile(":");
private static final Pattern PATTERN_EQUAL = Pattern.compile("="); private static final Pattern PATTERN_EQUAL = Pattern.compile("=");
private static final Pattern PATTERN_AND = Pattern.compile("&"); private static final Pattern PATTERN_AND = Pattern.compile("&");
private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
private InputStream in; private InputStream in;
@ -84,7 +83,7 @@ public class HttpHeaderParser {
} }
// Post processing // Post processing
parseHeaderValue(header.getCookieMap(), header.getHeader(HEADER_COOKIE)); parseCookieValues(header.getCookieMap(), header.getHeader(HEADER_COOKIE));
header.setInputStream(in); header.setInputStream(in);
return header; return header;
} }
@ -144,17 +143,27 @@ public class HttpHeaderParser {
(data.length>1 ? data[1] : "").trim()); //Value (data.length>1 ? data[1] : "").trim()); //Value
} }
/**
* Parses a raw cookie header into key and value pairs
*
* @param map the Map where the cookies will be stored.
* @param cookieValue the raw cookie header value String that will be parsed
*/
public static void parseCookieValues(Map<String,String> map, String cookieValue){
parseHeaderValues(map, cookieValue, ";");
}
/** /**
* Parses a header value string that contains key and value paired data and * Parses a header value string that contains key and value paired data and
* stores them in a HashMap. If a pair only contain a key the the value * stores them in a HashMap. If a pair only contain a key the the value
* will be set as a empty string. * will be set as a empty string.
* *
* @param map the Map where the cookies will be stored. * @param map the Map where key and values will be stored.
* @param cookieHeader the raw cookie header String that will be parsed * @param headerValue the raw header value String that will be parsed.
* @param delimiter the delimiter that separates key and value pairs (e.g. ';' for Cookies or ',' for Cache-Control)
*/ */
public static void parseHeaderValue(Map<String,String> map, String cookieHeader){ public static void parseHeaderValues(Map<String,String> map, String headerValue, String delimiter){
if(cookieHeader != null && !cookieHeader.isEmpty()){ if(headerValue != null && !headerValue.isEmpty()){
String[] tmp = PATTERN_SEMICOLON.split(cookieHeader); String[] tmp = headerValue.split(delimiter);
for(String cookie : tmp){ for(String cookie : tmp){
String[] tmp2 = PATTERN_EQUAL.split(cookie, 2); String[] tmp2 = PATTERN_EQUAL.split(cookie, 2);
map.put( map.put(

View file

@ -244,11 +244,11 @@ public class HttpPrintStream extends OutputStream{
else{ else{
for(String key : cookies.keySet()){ for(String key : cookies.keySet()){
out.print("Set-Cookie: " + key + "=" + cookies.get(key) + ";"); out.print("Set-Cookie: " + key + "=" + cookies.get(key) + ";");
out.print(System.lineSeparator()); out.println();
} }
} }
} }
out.print(System.lineSeparator()); out.println();
cookies = null; cookies = null;
} }
out.print(s); out.print(s);
@ -307,10 +307,6 @@ public class HttpPrintStream extends OutputStream{
out.write(buf, off, len); out.write(buf, off, len);
} }
private void rawWrite(String s) throws IOException {
out.write(s.getBytes());
}
private String getStatusString(int type){ private String getStatusString(int type){
switch(type){ switch(type){
case 100: return "Continue"; case 100: return "Continue";

View file

@ -152,7 +152,7 @@ public class MultipartParser implements Iterable<MultipartField>{
// Parse // Parse
String disposition = headers.get(HEADER_CONTENT_DISPOSITION); String disposition = headers.get(HEADER_CONTENT_DISPOSITION);
if (disposition != null){ if (disposition != null){
HttpHeaderParser.parseHeaderValue(headers, disposition); HttpHeaderParser.parseCookieValues(headers, disposition);
if (headers.containsKey("form-data")){ if (headers.containsKey("form-data")){
if (headers.containsKey("filename")){ if (headers.containsKey("filename")){
MultipartFileField field = new MultipartFileField(headers, boundaryIn); MultipartFileField field = new MultipartFileField(headers, boundaryIn);

View file

@ -258,13 +258,10 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork
private long getCacheTime(String cache_control){ private long getCacheTime(String cache_control){
long ret = 0; long ret = 0;
String[] tmp = cache_control.split(","); HashMap<String,String> tmpMap = new HashMap<>();
for( String element : tmp ){ HttpHeaderParser.parseHeaderValues(tmpMap, cache_control, ",");
element = element.replaceAll("\\s", "").toLowerCase(); if(tmpMap.containsKey("max-age"))
if( element.startsWith("max-age=") ){ ret = Long.parseLong( tmpMap.get("max-age") );
ret = Long.parseLong( element.substring( "max-age=".length() ) );
}
}
return ret; return ret;
} }

View file

@ -12,6 +12,7 @@ public class MulticastDNSClientTest {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
MulticastDNSClient mdns = new MulticastDNSClient(); MulticastDNSClient mdns = new MulticastDNSClient();
mdns.start(); mdns.start();
mdns.sendProbe("apple.local"); //mdns.sendProbe("appletv.local");
mdns.sendProbe("_services._dns-sd._udp.local");
} }
} }