package fr.pierrelemee;


import com.sun.net.httpserver.HttpExchange;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class WebRequest {

    private static final String COOKIE_HEADER_NAME = "Cookie";

    protected HttpMethod method;
    protected String path;
    protected Map<String, String> variables;
    protected Map<String, String> cookies;
    protected Map<String, List<String>> headers;
    protected Map<String, List<String>> get;
    protected Map<String, List<String>> post;

    public WebRequest(String path) {
        this(path, HttpMethod.GET);
    }

    public WebRequest(String path, HttpMethod method) {
        this(path, method, new LinkedHashMap<>(), new LinkedHashMap<>());
    }

    public WebRequest(String path, HttpMethod method, Map<String, List<String>> headers, Map<String, List<String>> get) {
        this.path = path;
        this.method = method;
        this.get = get;
        this.variables = new LinkedHashMap<>();
        this.cookies = new LinkedHashMap<>();
        this.headers = headers;

        this.exctractCookies();
    }

    public static Map<String, List<String>> parameters(String query) {
        Map<String, List<String>> parameters = new LinkedHashMap<>();

        if (query != null && !query.isEmpty()) {
            int index;
            for (String parameter : query.split("&")) {
                index = parameter.indexOf('=');
                if (index > -1) {
                    parameters.put(parameter.substring(0, index), Collections.singletonList(parameter.substring(index + 1)));
                } else {
                    parameters.put(parameter, Collections.singletonList("true"));
                }
            }
        }

        return parameters;
    }

    protected void addVariables(Map<String, String> variables) {
        this.variables.putAll(variables);
    }

    protected void addHeader(String name, String value) {
        if (this.headers.containsKey(name)) {
            this.headers.get(name).add(value);
        } else {
            this.headers.put(name, Collections.singletonList(value));
        }

        if (name.trim().equalsIgnoreCase(COOKIE_HEADER_NAME)) {
            this.exctractCookies();
        }
    }

    protected void exctractCookies() {
        if (this.headers.containsKey(COOKIE_HEADER_NAME)) {
            for (String header : this.headers.get(COOKIE_HEADER_NAME)) {
                for (String cookie : header.split(";")) {
                    cookie = cookie.trim();
                    int index = cookie.indexOf('=');
                    if (index >= 0) {
                        this.cookies.put(cookie.substring(0, index), cookie.substring(index + 1));
                    }
                }
            }
        }
    }

    public String variable(String name) {
        return this.variables.getOrDefault(name, null);
    }

    public boolean hasCookie(String name) {
        return this.cookies.containsKey(name);
    }

    public String cookie(String name) {
        return this.cookies.getOrDefault(name, null);
    }

    public HttpMethod getMethod() {
        return this.method;
    }

    public String getPath() {
        return this.path;
    }

    public Map<String, List<String>> get() {
        return this.get;
    }

    public Map<String, List<String>> post() {
        return this.post;
    }

    public static WebRequest fromExchange(HttpExchange exchange) {
        return new WebRequest(exchange.getRequestURI().getPath(),
                HttpMethod.valueOf(exchange.getRequestMethod()),
                exchange.getRequestHeaders(),
                parameters(exchange.getRequestURI().getQuery())
        );
    }
}

@@ -64,16 +64,137 @@
 hange;%0A%0A
+import java.io.BufferedReader;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.InputStreamReader;%0A
 import j
@@ -215,16 +215,16 @@
 ctions;%0A
-
 import j
@@ -1008,24 +1008,251 @@
 ng%3E%3E get) %7B%0A
+        this(path, method, headers, get, Collections.emptyMap());%0A    %7D%0A%0A    public WebRequest(String path, HttpMethod method, Map%3CString, List%3CString%3E%3E headers, Map%3CString, List%3CString%3E%3E get, Map%3CString, List%3CString%3E%3E post) %7B%0A
         this
@@ -1315,24 +1315,50 @@
 .get = get;%0A
+        this.post = post;%0A
         this
@@ -1472,16 +1472,16 @@
 aders;%0A%0A
-
         
@@ -1503,32 +1503,536 @@
 okies();%0A    %7D%0A%0A
+    public static Map%3CString, List%3CString%3E%3E parameters(InputStream input) %7B%0A        BufferedReader reader = new BufferedReader(new InputStreamReader(input));%0A        StringBuilder buffer = new StringBuilder();%0A%0A        try %7B%0A            String line;%0A            while ((line = reader.readLine()) != null) %7B%0A                buffer.append(line);%0A            %7D%0A%0A            return parameters(buffer.toString());%0A%0A        %7D catch (IOException e) %7B%0A            return Collections.emptyMap();%0A        %7D%0A    %7D%0A%0A
     public stati
@@ -4349,24 +4349,102 @@
 exchange) %7B%0A
+        HttpMethod method = HttpMethod.valueOf(exchange.getRequestMethod());%0A%0A
         retu
@@ -4513,35 +4513,40 @@
         
-HttpMethod.valueOf(
+method,%0A                
 exchange
@@ -4556,24 +4556,24 @@
 tRequest
-Method()
+Headers(
 ),%0A     
@@ -4575,32 +4575,43 @@
                 
+parameters(
 exchange.getRequ
@@ -4609,32 +4609,40 @@
 e.getRequest
-Headers(
+URI().getQuery()
 ),%0A         
@@ -4639,32 +4639,60 @@
 %0A               
+ method == HttpMethod.POST ?
  parameters(exch
@@ -4706,32 +4706,47 @@
 tRequest
-URI().getQuery()
+Body()) : Collections.emptyMap(
 )%0A      

Support post parameters