package net.zephyrizing.http_server;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import net.zephyrizing.http_server.content.ContentProvider;

public class HttpResponse {

    private static final Map<Integer, String> RESPONSE_DESCRIPTIONS;
    static {
        Map<Integer, String> descriptions = new HashMap<Integer, String>();

        descriptions.put(200, "OK");
        descriptions.put(404, "Not Found");

        RESPONSE_DESCRIPTIONS = Collections.unmodifiableMap(descriptions);
    }

    private int status;
    private Map<String, List<String>> headers;
    private ContentProvider provider;

    public static HttpResponse responseFor(HttpRequest request) {
        return new HttpResponse(request.protocolVersion());
    }

    private String protocolVersion;

    public HttpResponse(String protocolVersion) {
        this.status = 200;
        this.protocolVersion = protocolVersion;
        this.headers = new HashMap<String, List<String>>();
    }

    public int status() {
        return this.status;
    }

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

    public String protocolVersion() {
        return this.protocolVersion;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public void addHeader(String key, String... values) {
        if (values.length != 0) {
            if (this.headers.get(key) == null) {
                this.headers.put(key, new ArrayList<String>());
            }
            Collections.addAll(this.headers.get(key), values);
        }
    }

    public void setContent(ContentProvider provider) {
        this.provider = provider;
    }

    public String getStatusLine() {
        return String.format("HTTP/%s %d %s",
                             protocolVersion(),
                             this.status,
                             RESPONSE_DESCRIPTIONS.get(this.status));
    }

    public Stream<String> getStatusLineStream() {
        return Stream.of(getStatusLine());
    }

    public Stream<String> getHeaderStream() {
        return emptyStringStreamBuilder().build();
    }

    public InputStream getData() {
        if (this.provider != null && this.provider.contentExists()) {
            return this.provider.getContent();
        }
        return new ByteArrayInputStream("".getBytes());
    }

    private static Stream.Builder<String> emptyStringStreamBuilder() {
        return Stream.builder();
    }
}

@@ -1639,60 +1639,35 @@
     
-if (this.headers.get(key) == null) %7B%0A               
+List%3CString%3E headerValues =
  thi
@@ -1676,18 +1676,27 @@
 headers.
-pu
+getOrDefaul
 t(key, n
@@ -1724,22 +1724,8 @@
 ));%0A
-            %7D%0A
     
@@ -1751,16 +1751,82 @@
 .addAll(
+headerValues,%0A                               values);%0A            
 this.hea
@@ -1834,19 +1834,32 @@
 ers.
-ge
+putIfAbsen
 t(key
-)
 , 
-v
+headerV
 alue

Use the Map API better for default values and adding