package com.alecgorge.minecraft.jsonapi.packets.netty.router;

import net.minecraft.util.io.netty.buffer.Unpooled;
import net.minecraft.util.io.netty.handler.codec.http.DefaultFullHttpResponse;
import net.minecraft.util.io.netty.handler.codec.http.FullHttpResponse;
import net.minecraft.util.io.netty.handler.codec.http.HttpResponseStatus;
import net.minecraft.util.io.netty.handler.codec.http.HttpVersion;
import net.minecraft.util.io.netty.util.CharsetUtil;

import com.alecgorge.minecraft.jsonapi.JSONAPI;
import com.alecgorge.minecraft.jsonapi.packets.netty.APIv2Handler;

public class JSONAPIDefaultRoutes {
	JSONAPI	api;

	public JSONAPIDefaultRoutes(JSONAPI api) {
		this.api = api;

		RouteMatcher r = api.getRouter();
		r.get("/api/2/call", new Handler<FullHttpResponse, RoutedHttpRequest>() {
			@Override
			public FullHttpResponse handle(RoutedHttpRequest event) {
				APIv2Handler h = new APIv2Handler(event.request);

				return h.serve();
			}
		});

		r.get("/api/2/version", new Handler<FullHttpResponse, RoutedHttpRequest>() {
			@Override
			public FullHttpResponse handle(RoutedHttpRequest event) {
				APIv2Handler h = new APIv2Handler(event.request);

				return h.serve();
			}
		});

		r.get("/", new Handler<FullHttpResponse, RoutedHttpRequest>() {
			@Override
			public FullHttpResponse handle(RoutedHttpRequest event) {
				return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.copiedBuffer("This is a Minecraft server. HTTP on this port by JSONAPI. JSONAPI by Alec Gorge.", CharsetUtil.UTF_8));
			}
		});
	}
}

@@ -56,16 +56,67 @@
 outer;%0A%0A
+import net.minecraft.util.io.netty.buffer.ByteBuf;%0A
 import n
@@ -771,16 +771,352 @@
 uter();%0A
+%09%09r.noMatch(new Handler%3CFullHttpResponse, RoutedHttpRequest%3E() %7B%0A%09%09%09@Override%0A%09%09%09public FullHttpResponse handle(RoutedHttpRequest event) %7B%0A%09%09%09%09return buildResponse(HttpResponseStatus.OK, %22text/plain%22, %22Whatever you wanted wasn't found. This is a Minecraft server. HTTP on this port by JSONAPI. JSONAPI by Alec Gorge.%5Cn%22);%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09%0A
 %09%09r.get(
@@ -1743,217 +1743,534 @@
 urn 
-new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.copiedBuffer(%22This is a Minecraft server. HTTP on this port by JSONAPI. JSONAPI by Alec Gorge.%22, CharsetUtil.UTF_8));%0A%09%09%09%7D%0A%09%09%7D)
+buildResponse(HttpResponseStatus.OK, %22text/plain%22, %22This is a Minecraft server. HTTP on this port by JSONAPI. JSONAPI by Alec Gorge.%5Cn%22);%0A%09%09%09%7D%0A%09%09%7D);%0A%09%7D%0A%09%0A%09public static FullHttpResponse buildResponse(HttpResponseStatus resp, String type, String body) %7B%0A%09%09ByteBuf buf = Unpooled.copiedBuffer(body, CharsetUtil.UTF_8);%0A%09%09FullHttpResponse r = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, resp, buf);%0A%09%09r.headers().set(%22Access-Control-Allow-Origin%22, %22*%22);%0A%09%09r.headers().set(%22Content-Length%22, buf.readableBytes());%0A%0A%09%09return r
 ;%0A%09%7D

fix router by using content-length