/*
*/
//http://localhost:8080/http://www.wanadoo.fr/accueil/bin/accueil.cgi
//http://www.wanadoo.fr/wanadoo_et_moi/compte/bin/compte.cgi
//http://www.wanadoo.fr/messagerie/expressmail/cgi-bin/wm?methode=ListDisplay&folder=inbox
import java.applet.*;
import java.io.*;
import java.net.*;
public class WBHTTPD extends Applet implements Runnable {
WBServerSocket ess;
String remote_host;
int port;
Thread th;
public void dump_to(String fn, OutputStream os) throws IOException {
PrintWriter writer = new PrintWriter(os, true);
try {
//String fns = format(fn);
System.out.println(fn);
URL uu = new URL(fn);
WBURLConnection euc;
WBURLInputStream euis;
StringBuffer buf = new StringBuffer();
String str;
//ajout des pages par défaut
buf.append(getHeader());
euc = new WBURLConnection(uu);
euis = new WBURLInputStream(euc);
BufferedReader reader = new BufferedReader(new InputStreamReader(euis, "latin1"));
while ((str = reader.readLine())!=null && str.length()>=0) buf.append(str+"\n");
reader.close();
String type = WBMimeType.get(fn);
writer.println("HTTP/1.1 200 OK");
writer.println("Content-type: " + type);
writer.println("Content-length: " + buf.length());
writer.println();
writer.println(buf.toString());
writer.flush();
} catch (Exception e) {
String resp = e.toString();
writer.println("HTTP/1.1 500 Internal Server Error");
writer.println("Content-type: text/plain");
writer.println("Content-length: " + resp.length());
writer.println("");
writer.print(resp);
writer.flush();
}
}
public String origin() {
URL appletSource = getDocumentBase();
try {
InetAddress host;
host = InetAddress.getByName(appletSource.getHost());
return host.getHostName();
} catch (Exception e) { System.out.println(e); };
return "localhost";
}
public void init() {
try {
remote_host = origin();
port = new Integer(getParameter("port")).intValue();
System.out.println("port=" + port);
ess = new WBServerSocket(port);
} catch (Exception e) { System.out.println(e); }
}
public void start() {
th = new Thread(this);
th.start();
}
//public void stop() {
// th.stop();
//}
public void run() {
WBSocket client;
PrintWriter writer;
BufferedReader reader;
String line;
try {
while (true) {
client = ess.accept_any();
writer = new PrintWriter(client.getOutputStream(), true);
reader = new BufferedReader(
new InputStreamReader(client.getInputStream()));
line = reader.readLine();
while (reader.readLine().length() > 0) { }
System.out.println(line);
int at0 = line.lastIndexOf("http://"); //on extrait l'url finale dans l'url réécrite
int at1 = line.indexOf(" HTTP");
if(at0>=0 & at1>=0) {
String filename = line.substring(at0, at1); //at0+1
dump_to(filename, client.getOutputStream());
} else {
StringBuffer buf = new StringBuffer();
buf.append(getHeader());
buf.append("Bloup bloup, try again\n");
PrintWriter out = new PrintWriter(client.getOutputStream());
writer.println("HTTP/1.1 200 OK");
writer.println("Content-type: text/plain");
writer.println("Content-length: " + buf.length());
writer.println();
writer.println(buf.toString());
writer.flush();
}
client.close_real();
}
} catch (Exception e) { System.out.println(e.toString()); }
}
private String getHeader() throws UnknownHostException {
StringBuffer buf = new StringBuffer();
String constant = "http://"+InetAddress.getLocalHost().getHostAddress()+":8080/http://www.wanadoo.fr/";
buf.append(""+constant+"wanadoo_et_moi/compte/bin/compte.cgi
\n");
buf.append(""+constant+"messagerie/expressmail/cgi-bin/wm?methode=ListDisplay&folder=inbox