package net.zephyrizing.http_server;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import net.zephyrizing.http_server.HttpRequest;
import net.zephyrizing.http_server.HttpRequest.Method;
import net.zephyrizing.http_server.HttpResponse;

import static net.zephyrizing.http_server.HttpRequest.Method.*;

public class HttpProtocol {
    public static HttpRequest requestFromLines(Stream<String> lines) {
        List<String> linesList = lines.filter(Predicate.isEqual("").negate())
            .limit(1).collect(Collectors.toList());

        if (linesList.isEmpty()) {
            return null;
        }

        String requestLine = linesList.get(0);
        return parseRequestLine(requestLine);
    }

    public static InputStream responseStream(HttpResponse response) {
        String headStr = Stream.concat(response.getStatusLineStream(),
                                       response.getHeaderStream())
            .collect(Collectors.joining("\r\n", "", "\r\n\r\n"));

        InputStream head = new ByteArrayInputStream(headStr.getBytes());
        InputStream body = response.getData();

        return new SequenceInputStream(head, body);
    }

    private static HttpRequest parseRequestLine(String requestLine) {
        String[] methodPathProto = requestLine.split(" ");

        if (methodPathProto.length != 3) {
            return null;
        }

        Method method;
        try {
            method = Method.valueOf(methodPathProto[0]);
        } catch (IllegalArgumentException e) {
            return null;
        }

        String path            = methodPathProto[1];
        String protocolVersion = methodPathProto[2].replace("HTTP/", "");

        return new HttpRequest(method, path, protocolVersion);
    }
}

@@ -835,22 +835,83 @@
         
-return
+RequestBuilder b = new RequestBuilder();%0A%0A        try %7B%0A           
  parseRe
@@ -920,16 +920,19 @@
 estLine(
+b, 
 requestL
@@ -937,16 +937,125 @@
 tLine);%0A
+        %7D catch (IllegalArgumentException e) %7B%0A            return null;%0A        %7D%0A%0A        return b.build();%0A
     %7D%0A%0A 
@@ -1531,37 +1531,48 @@
 tic 
-Http
+void parse
 Request
- parse
+Line(
 Request
-Line(
+Builder b, 
 Stri
@@ -1708,53 +1708,44 @@
     
-return null;%0A        %7D%0A%0A        Method method
+throw new IllegalArgumentException()
 ;%0A  
@@ -1742,37 +1742,34 @@
 tion();%0A        
-try %7B
+%7D%0A
 %0A            met
@@ -1765,21 +1765,17 @@
     
-    
+b.
 method
- = 
+(
 Meth
@@ -1808,126 +1808,28 @@
 %5B0%5D)
-;%0A        %7D catch (IllegalArgumentException e) %7B%0A            return null;%0A        %7D%0A%0A        String path            = 
+)%0A            .path(
 meth
@@ -1842,17 +1842,17 @@
 Proto%5B1%5D
-;
+)
 %0A       
@@ -1852,23 +1852,21 @@
         
-String 
+    .
 protocol
@@ -1872,19 +1872,17 @@
 lVersion
- = 
+(
 methodPa
@@ -1916,71 +1916,8 @@
  %22%22)
-;%0A%0A        return new HttpRequest(method, path, protocolVersion
 );%0A 

Refactor HttpProtocol to use the RequestBuilder