package com.jhttpserver.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import com.jhttpserver.entity.Request;

/**
 * 请求解析
 * 
 * @author Jayin Ton
 * 
 */
public class RequestParser {
	/**
	 * 解析请求initial_line
	 * 
	 * @param request
	 * @param initial_line
	 * @return
	 */
	public static boolean parseInitialLine(Request request, String initial_line) {
		if (initial_line == null || initial_line.equals("")) {
			return false;
		}
		String[] s = initial_line.split(" ");
		request.setMethod(s[0]);
		request.setPath(s[1].substring(0, s[1].indexOf("?") == -1 ? s[1]
				.length() : s[1].indexOf("?")));
		request.setHttpVersion(s[2]);
		// parse the querystring
		if (s[1].contains("?")) {
			String[] query = s[1].substring(s[1].indexOf('?') + 1,
					s[1].length()).split("&");
			for (String _s : query) {
				String key = _s.split("=")[0];
				String value = _s.split("=")[1];
				request.setQueryString(key, value);
			}
		}
		return true;
	}

	/**
	 * 解析 header
	 * 
	 * @param request
	 * @param h
	 * @return
	 */
	public static Request parseHeader(Request request, String header_line) {
		String name = header_line.split(":")[0].trim();
		String value = header_line.split(":")[1].trim();
		request.addHeader(name.toLowerCase(), value);
		return request;
	}
   /**
    * 解析body
    * @param request
    * @param body
    * @return
    */
	public static Request parseBody(Request request, String body) {
		String[] s = body.split("&");
		for (String _s : s) {
			String name = _s.split("=")[0];
			String value = null;
			try {
				value = URLDecoder.decode(_s.split("=")[1], "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			request.addParams(name, value);
		}
		return request;
	}

}

@@ -462,16 +462,17 @@
 se;%0A%09%09%7D%0A
+%0A
 %09%09String
@@ -836,24 +836,57 @@
  : query) %7B%0A
+%09%09%09%09if(_s.indexOf('=') != -1 )%7B%0A%09
 %09%09%09%09String k
@@ -908,24 +908,25 @@
 =%22)%5B0%5D;%0A%09%09%09%09
+%09
 String value
@@ -927,16 +927,67 @@
  value =
+ %22%22;%0A%09%09%09%09%09if(_s.split(%22=%22).length%3E1)%7B%0A%09%09%09%09%09%09value =
  _s.spli
@@ -997,16 +997,24 @@
 =%22)%5B1%5D;%0A
+%09%09%09%09%09%7D%0A%09
 %09%09%09%09requ
@@ -1049,17 +1049,24 @@
 e);%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A
-
 %09%09%7D%0A
+%0A
 %09%09re
@@ -1819,17 +1819,16 @@
 addParam
-s
 (name, v

Fix #3 :query string 查询错误