package com.server.bla.impl;

import com.server.bla.RequestHandler;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * Created by jonhkr on 5/8/16.
 */
public class HttpRequestHandler implements RequestHandler {

    final ByteBuffer buffer = ByteBuffer.allocate(64*1024);
    final SocketChannel channel;
    int mark = 0;
    int keepAlive = 1;

    public HttpRequestHandler(SocketChannel channel) {
        this.channel = channel;
    }

    @Override
    public void read() throws IOException {
        clear();

        int byteCount = channel.read(buffer);

        if (byteCount == -1) {
            keepAlive = 0;
        }

        buffer.flip();
        buffer.position(mark);

        parse();
    }

    public void parse() throws IOException {
        String line;
        while((line = readLine()) != null) {

            if (line.equals("Connection: close") || line.contains("HTTP/1.0")) {
                keepAlive(0);
            }

            if (line.isEmpty()) {
                StringBuilder sb = new StringBuilder();
                sb.append("HTTP/1.1 200 Ok\r\n");
                sb.append("Content-Length: 0\r\n");
                if (keepAlive() == 1) {
                    sb.append("Connection: keep-alive\r\n\r\n");
                }

                channel.write(ByteBuffer.wrap(sb.toString().getBytes()));
            }
        }

        if (keepAlive() == 0) {
            channel.close();
        }
    }

    public String readLine() {
        StringBuilder sb = new StringBuilder();
        int l = -1;

        while(buffer.hasRemaining()) {
            char c = (char) buffer.get();
            sb.append(c);
            if (c == '\n' && l == '\r') {
                mark = buffer.position();

                return sb.substring(0, sb.length() - 2);
            }

            l = c;
        }

        return null;
    }


    public void clear() {
        mark = 0;
        buffer.clear();
    }

    public void keepAlive(int keepAlive) {
        this.keepAlive = keepAlive;
    }

    public int keepAlive() {
        return keepAlive;
    }
}

@@ -1028,24 +1028,153 @@
 sEmpty()) %7B%0A
+                String content = %22%3Chtml%3E%3Chead%3E%3Ctitle%3EHi%3C/title%3E%3C/head%3E%3Cbody%3E%3Ch1%3EHello %3Csmall%3Eworld%3C/small%3E%3C/h1%3E%3C/body%3E%3C/html%3E%22;%0A%0A
             
@@ -1289,24 +1289,38 @@
   sb.append(
+String.format(
 %22Content-Len
@@ -1328,14 +1328,43 @@
 th: 
-0
+%25s
 %5Cr%5Cn%22
+, content.getBytes().length)
 );%0A 
@@ -1459,20 +1459,16 @@
 live%5Cr%5Cn
-%5Cr%5Cn
 %22);%0A    
@@ -1472,32 +1472,143 @@
                %7D
+%0A%0A                sb.append(%22%5Cr%5Cn%22);%0A                sb.append(content);%0A                sb.append(%22%5Cr%5Cn%5Cr%5Cn%22);
 %0A%0A              

Return some html