package io.reon.auth;

import java.util.HashMap;

import io.reon.util.Hex;

public abstract class AbstractAuth implements HttpAuth {
	public static final String REALM = "realm";
	public static final String RESPONSE = "response";
	public static final String NONCE = "nonce";
	public static final String PK = "pk";
	public static final String EQ = "=\"";
	public static final String EOS = "\", ";
	public static final String EOT = "\"";

	public static HashMap<String, String> parse(String text) {
		String[] fields = text.substring(text.indexOf(' ')+1).split(",");
		HashMap<String, String> attrs = new HashMap<>();
		for(String field: fields) {
			String[] kv = field.split("=");
			attrs.put(kv[0].trim(), kv[1].replace('"',' ').trim());
		}
		return attrs;
	}

	public static byte[] fromHex(String value) {
		return Hex.decodeHex(value);
	}

	public static String toHex(byte[] value) {
		return Hex.encodeHex(value, false);
	}
}

@@ -489,16 +489,76 @@
 text) %7B%0A
+%09%09HashMap%3CString, String%3E attrs = new HashMap%3C%3E();%0A%09%09try %7B%0A%09
 %09%09String
@@ -605,9 +605,11 @@
 ' ')
-+
+ + 
 1).s
@@ -624,63 +624,14 @@
 );%0A%09
-%09HashMap%3CString, String%3E attrs = new HashMap%3C%3E();%0A
 %09%09for
+ 
 (Str
@@ -639,16 +639,17 @@
 ng field
+ 
 : fields
@@ -652,16 +652,17 @@
 elds) %7B%0A
+%09
 %09%09%09Strin
@@ -691,16 +691,17 @@
 =%22);%0A%09%09%09
+%09
 attrs.pu
@@ -734,16 +734,17 @@
 ace('%22',
+ 
 ' ').tri
@@ -749,16 +749,68 @@
 rim());%0A
+%09%09%09%7D%0A%09%09%7D catch (RuntimeException ex) %7B%0A%09%09%09// ignore%0A
 %09%09%7D%0A%09%09re

Fix for crash over badly formatted auth requests