Beh e' un po' di tempo che mi diletto con java e ho fatto un piccolo ProxyServer per l'università ed ho pensato "perchè non condividere questo bel programmino con i forumisti"?
Ditemi cosa ne pensate!!!
PS: lo so che non e' eccezionale (e che magari qualche struttura non e' proprio "standard") però può essere richiamata sia dalla barra degli indirizzi "localhost:65001/www.html.it", sia inserendo l'indirizzo e la relativa porta all'interno del proxy di un qualsiasi browser web; inoltre gestisce sia il testo che i file (come immagini) all'interno di una pagina e restituisce anche il codice 404 per gli indirizzi sbagliati.
Non funziona invece con i browser (come GALEON) che utilizzano gli indirizzi relativi.
Codice PHP:
// Classi di IMPORT
import java.io.*;
import java.net.*;
import java.util.*;
class ProxyServer {
// Classe ProxyServer OPEN
public static void main(String[] args) throws Exception {
// Metodo main() OPEN
// Stringa usata per la richiesta GET
String requestMessageLine;
// nome del sito che si vuole contattare
String SitoWeb;
// porta su cui si accetta la connessione dal client
ServerSocket listenSocket = new ServerSocket(65001);
while(true) {
// WHILE - OPEN
// Accetto la richiesta dal Client
Socket ConnessioneClient2Server = listenSocket.accept();
// Creo gli Stream per il Client
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(ConnessioneClient2Server.getInputStream()));
// Legge la prima riga dell'intestazione http
requestMessageLine = inFromClient.readLine();
System.out.println(requestMessageLine);
// Processa la stringa per determinare il nome del sito da contattare
StringTokenizer tokenizedLine = new StringTokenizer(requestMessageLine);
if(tokenizedLine.nextToken().equals("GET")) {
// IF - OPEN
DataOutputStream outToClient = new DataOutputStream(ConnessioneClient2Server.getOutputStream());
BufferedOutputStream bos = new BufferedOutputStream(outToClient);
SitoWeb = tokenizedLine.nextToken();
if(SitoWeb.startsWith("/") == true) {
SitoWeb = SitoWeb.substring(1);
System.out.println("Sito richiesto: "+SitoWeb);
}
// Faccio la richiesta del file al sito indicato
try {
URL u = null;
if (SitoWeb.startsWith("http://")) {
u = new URL(SitoWeb);
} else {
u = new URL("http://"+SitoWeb);
}
URLConnection uc = u.openConnection();
InputStream raw = uc.getInputStream();
InputStream in = new BufferedInputStream(raw);
int contentLength = uc.getContentLength();
String contentType = uc.getContentType();
// Mando l'intestazione al Client
outToClient.writeBytes("HTTP/1.0 200 Document Follows\r\n");
outToClient.writeBytes("Content-Type: "+contentType+"\r\n");
outToClient.writeBytes("Content-Length: "+contentLength+"\r\n");
outToClient.writeBytes("\r\n");
if (contentType.startsWith("text/") || contentLength == -1) {
while(true) {
int data = raw.read();
if (data == -1) break;
bos.write(data);
}
} else {
byte[] data = new byte[contentLength];
int bytesRead = 0;
int offset = 0;
while (offset < contentLength) {
bytesRead = in.read(data, offset, data.length-offset);
if (bytesRead == -1) break;
offset += bytesRead;
}
bos.write(data, 0, data.length);
}
in.close();
} catch (SocketException se) {
System.out.println("*** ERRORE *** - "+se);
} catch (Exception e) {
System.out.println("*** ERRORE *** - "+e);
// Mando l'intestazione al Client
outToClient.writeBytes("HTTP/1.0 404 \r\n");
outToClient.writeBytes("\r\n");
outToClient.writeBytes("<HTML><HEAD><TITLE>ERRORE</TITLE></HEAD>");
outToClient.writeBytes("<BODY><CENTER><FONT SIZE=+2>ERRORE 404
");
outToClient.writeBytes("PAGINA NON TROVATA</FONT></CENTER></BODY></HTML>");
}
// Chiudo il Socket
ConnessioneClient2Server.close();
} else {
// Se la risposta e' sbagliata mando un messaggio di errore al client
System.out.println("Bad Request Message");
// Chiudo il Socket
ConnessioneClient2Server.close();
} // IF - CLOSE
} // WHILE - CLOSE
} // Metodo main() CLOSE
} // Classe ProxyServer CLOSE