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 firstLine = linesList.get(0);
        String[] methodPathProto = firstLine.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);
    }

    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);
    }
}

@@ -795,19 +795,21 @@
  String 
-fir
+reque
 stLine =
@@ -827,16 +827,594 @@
 get(0);%0A
+        return parseRequestLine(requestLine);%0A    %7D%0A%0A    public static InputStream responseStream(HttpResponse response) %7B%0A        String headStr = Stream.concat(response.getStatusLineStream(),%0A                                       response.getHeaderStream())%0A            .collect(Collectors.joining(%22%5Cr%5Cn%22, %22%22, %22%5Cr%5Cn%5Cr%5Cn%22));%0A%0A        InputStream head = new ByteArrayInputStream(headStr.getBytes());%0A        InputStream body = response.getData();%0A%0A        return new SequenceInputStream(head, body);%0A    %7D%0A%0A    private static HttpRequest parseRequestLine(String requestLine) %7B%0A
         
@@ -1444,11 +1444,13 @@
 o = 
-fir
+reque
 stLi
@@ -1921,462 +1921,7 @@
    %7D
-%0A%0A    public static InputStream responseStream(HttpResponse response) %7B%0A        String headStr = Stream.concat(response.getStatusLineStream(),%0A                                       response.getHeaderStream())%0A            .collect(Collectors.joining(%22%5Cr%5Cn%22, %22%22, %22%5Cr%5Cn%5Cr%5Cn%22));%0A%0A        InputStream head = new ByteArrayInputStream(headStr.getBytes());%0A        InputStream body = response.getData();%0A%0A        return new SequenceInputStream(head, body);%0A    %7D
 %0A%7D%0A

Extract a function for parsing the request line