Small refactoring
This commit is contained in:
parent
123044aaf3
commit
1a6edff347
5 changed files with 25 additions and 22 deletions
|
|
@ -40,7 +40,6 @@ public class HttpHeaderParser {
|
|||
private static final Pattern PATTERN_COLON = Pattern.compile(":");
|
||||
private static final Pattern PATTERN_EQUAL = Pattern.compile("=");
|
||||
private static final Pattern PATTERN_AND = Pattern.compile("&");
|
||||
private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
|
||||
|
||||
|
||||
private InputStream in;
|
||||
|
|
@ -84,7 +83,7 @@ public class HttpHeaderParser {
|
|||
}
|
||||
|
||||
// Post processing
|
||||
parseHeaderValue(header.getCookieMap(), header.getHeader(HEADER_COOKIE));
|
||||
parseCookieValues(header.getCookieMap(), header.getHeader(HEADER_COOKIE));
|
||||
header.setInputStream(in);
|
||||
return header;
|
||||
}
|
||||
|
|
@ -144,17 +143,27 @@ public class HttpHeaderParser {
|
|||
(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
|
||||
* stores them in a HashMap. If a pair only contain a key the the value
|
||||
* will be set as a empty string.
|
||||
*
|
||||
* @param map the Map where the cookies will be stored.
|
||||
* @param cookieHeader the raw cookie header String that will be parsed
|
||||
* @param map the Map where key and values will be stored.
|
||||
* @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){
|
||||
if(cookieHeader != null && !cookieHeader.isEmpty()){
|
||||
String[] tmp = PATTERN_SEMICOLON.split(cookieHeader);
|
||||
public static void parseHeaderValues(Map<String,String> map, String headerValue, String delimiter){
|
||||
if(headerValue != null && !headerValue.isEmpty()){
|
||||
String[] tmp = headerValue.split(delimiter);
|
||||
for(String cookie : tmp){
|
||||
String[] tmp2 = PATTERN_EQUAL.split(cookie, 2);
|
||||
map.put(
|
||||
|
|
|
|||
|
|
@ -244,11 +244,11 @@ public class HttpPrintStream extends OutputStream{
|
|||
else{
|
||||
for(String key : cookies.keySet()){
|
||||
out.print("Set-Cookie: " + key + "=" + cookies.get(key) + ";");
|
||||
out.print(System.lineSeparator());
|
||||
out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
out.print(System.lineSeparator());
|
||||
out.println();
|
||||
cookies = null;
|
||||
}
|
||||
out.print(s);
|
||||
|
|
@ -307,10 +307,6 @@ public class HttpPrintStream extends OutputStream{
|
|||
out.write(buf, off, len);
|
||||
}
|
||||
|
||||
private void rawWrite(String s) throws IOException {
|
||||
out.write(s.getBytes());
|
||||
}
|
||||
|
||||
private String getStatusString(int type){
|
||||
switch(type){
|
||||
case 100: return "Continue";
|
||||
|
|
|
|||
|
|
@ -152,7 +152,7 @@ public class MultipartParser implements Iterable<MultipartField>{
|
|||
// Parse
|
||||
String disposition = headers.get(HEADER_CONTENT_DISPOSITION);
|
||||
if (disposition != null){
|
||||
HttpHeaderParser.parseHeaderValue(headers, disposition);
|
||||
HttpHeaderParser.parseCookieValues(headers, disposition);
|
||||
if (headers.containsKey("form-data")){
|
||||
if (headers.containsKey("filename")){
|
||||
MultipartFileField field = new MultipartFileField(headers, boundaryIn);
|
||||
|
|
|
|||
|
|
@ -258,13 +258,10 @@ public class SSDPClient extends ThreadedUDPNetwork implements ThreadedUDPNetwork
|
|||
|
||||
private long getCacheTime(String cache_control){
|
||||
long ret = 0;
|
||||
String[] tmp = cache_control.split(",");
|
||||
for( String element : tmp ){
|
||||
element = element.replaceAll("\\s", "").toLowerCase();
|
||||
if( element.startsWith("max-age=") ){
|
||||
ret = Long.parseLong( element.substring( "max-age=".length() ) );
|
||||
}
|
||||
}
|
||||
HashMap<String,String> tmpMap = new HashMap<>();
|
||||
HttpHeaderParser.parseHeaderValues(tmpMap, cache_control, ",");
|
||||
if(tmpMap.containsKey("max-age"))
|
||||
ret = Long.parseLong( tmpMap.get("max-age") );
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ public class MulticastDNSClientTest {
|
|||
public static void main(String[] args) throws IOException {
|
||||
MulticastDNSClient mdns = new MulticastDNSClient();
|
||||
mdns.start();
|
||||
mdns.sendProbe("apple.local");
|
||||
//mdns.sendProbe("appletv.local");
|
||||
mdns.sendProbe("_services._dns-sd._udp.local");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue