Commits

Ning Sun  committed 6a7a952

improve getHeaders

  • Participants
  • Parent commits 39ff740

Comments (0)

Files changed (1)

File src/main/java/info/sunng/soldat/examples/http/HttpMessage.java

 import java.nio.ByteBuffer;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.logging.Level;
-import java.util.logging.Logger;
+import java.util.StringTokenizer;
 
 /**
  * 
 
     public Map<String, String> getHeaders() {
         if (isHeaderComplete()) {
-            String headerText = httpMessage.substring(0, httpMessage.indexOf("\r\n\r\n"));
-            String[] kvpairs = headerText.split("\r\n");
+            boolean firstline = true;
             Map<String, String> headers = new HashMap<String, String>();
-            // escape start line
-            for (int i = 1; i < kvpairs.length; i++) {
-                String[] kvpair = StringUtils.splitFirst(kvpairs[i], ":");
+            StringTokenizer st = new StringTokenizer(httpMessage, "\r\n");
+            
+            while(st.hasMoreTokens()) {
+                String line = st.nextToken();
+                if (firstline) {
+                    firstline = false;
+                    continue;
+                }
+                
+                if (line.length() == 0) {
+                    // header complete
+                    break;
+                }
+                
+                String[] kvpair = StringUtils.splitFirst(line, ":");
                 headers.put(kvpair[0].trim(), kvpair[1].trim());
             }
+            
             return headers;
         } else {
             return null;