package valandur.webapi.handlers;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import valandur.webapi.WebAPI;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class RateLimitHandler extends AbstractHandler {

    private Map<String, Long> lastCall = new ConcurrentHashMap<>();

    public RateLimitHandler() {
    }

    @Override
    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String key = request.getAttribute("key").toString();
        int limit = (int)request.getAttribute("rate");

        if (limit > 0) {
            long time = 0;

            if (lastCall.containsKey(key) && time - lastCall.get(key) < 1d / limit) {
                WebAPI.getInstance().getLogger().warn(request.getRemoteAddr() + " has exceeded the rate limit when requesting " + request.getRequestURI());
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
                baseRequest.setHandled(true);
                return;
            }

            lastCall.put(key, time);
        }
    }
}

@@ -473,12 +473,14 @@
 ng, 
-Long
+Double
 %3E la
@@ -883,20 +883,22 @@
     
-long
+double
  time = 
 0;%0A%0A
@@ -893,17 +893,47 @@
  time = 
-0
+System.nanoTime() / 1000000000d
 ;%0A%0A     

Fix rate limit handler