package alexoft.OGameUltimate.proxy;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import alexoft.utils.HTTPHeaders;
import alexoft.utils.Network;
public class ServerThread implements Runnable {
private Proxy parent;
private Thread thread;
public ServerThread(Proxy parent) {
this.parent = parent;
this.thread = new Thread(this, "OGameUltimate ProxyThread");
this.thread.setDaemon(true);
this.thread.start();
}
@Override
public void run() {
while (true) {
try {
Socket client = this.parent.accept();
//this.parent.log("Request accepted");
char buff[] = new char[4096];
BufferedReader from_client = new BufferedReader(
new InputStreamReader(client.getInputStream()));
StringBuilder headerString = new StringBuilder();
int n;
while(true){
n=from_client.read(buff);
if(n==-1) break;
headerString.append(String.valueOf(buff, 0, n));
if(n!=4096) break;
}
HTTPHeaders h = HTTPHeaders.read(headerString.toString());
if(h.url.equals("/")){
String r = "HTTP/1.0 307 Temporary Redirect\r\nLocation: " + "http://127.0.0.1:8123/game/index.php?page=overview\r\n\r\n";
client.getOutputStream().write(r.getBytes(), 0, r.getBytes().length);
}else{
Network.copyHTTPGet("http://" + this.parent.baseURL + ".ogame.fr" + h.url, client.getOutputStream(), this.parent.baseURL);
}
client.close();
//this.parent.log();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}