//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package kinosearch.webapp.servlets;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ProxyServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getPathInfo().substring("/onlinelife/".length());
        URL url = new URL("http://" + path);
        HttpURLConnection con = (HttpURLConnection)url.openConnection();
        con.setRequestMethod("GET");
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setUseCaches(true);
        Enumeration names = request.getHeaderNames();

        while(names.hasMoreElements()) {
            String headerName = names.nextElement().toString();
            if (!headerName.equalsIgnoreCase("referer")) {
                con.setRequestProperty(headerName, request.getHeader(headerName));
            }
        }

        con.connect();
        int statusCode = con.getResponseCode();
        response.setStatus(statusCode);

        for(Map.Entry<String, List<String>> stringListEntry : con.getHeaderFields().entrySet()) {
            if (stringListEntry.getKey() != null) {
                response.setHeader(stringListEntry.getKey().toString(), ((List)stringListEntry.getValue()).get(0).toString());
            }
        }

        BufferedInputStream webToProxyBuf = new BufferedInputStream(con.getInputStream());
        BufferedOutputStream proxyToClientBuf = new BufferedOutputStream(response.getOutputStream());

        int oneByte;
        while((oneByte = webToProxyBuf.read()) != -1) {
            proxyToClientBuf.write(oneByte);
        }

        proxyToClientBuf.flush();
        proxyToClientBuf.close();
        webToProxyBuf.close();
        con.disconnect();
    }
}
