Problème pour créer un proxy HTTP
Bonjour,
je dois créer un proxy en java mais j'ai quelques problèmes...
Je dois etre capable de naviguer sur internet en lançant mon proxy.
J'ai déjà codé la partie qui envoie les informations au serveur pour lui demander le chargent de la page web (je ne suis pas vraiment sur que ça soit bon mais je pense que ça marche)
J'ai l'impression que le programme s'arrete dans le deuxième bloc try après avoir déclaré serverIn.
Je n'arrive pas à voir si cela bloque parce que je ne transmet pas correctement les données lors de la phase d'envoi ou si j'ai un problème à la réception.
Merci de votre aide :):)
Voici mon code:
Ma classe serveur:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| import java.net.Socket;
import java.net.ServerSocket;
public class Serveur {
public static void main(String[] args){
ServerSocket ss = null;
try{
int port = 5000;
ss = new ServerSocket(port);
System.out.println("Server ready");
while(true){
Socket s = ss.accept();
ProxyHTTP p = new ProxyHTTP(s);
p.start();
}
ss.close();
} catch (Exception e){
System.out.println("Erreur dans l'attribution de la socket");
}
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| import java.net.Socket;
import java.net.ServerSocket;
import java.io.*;
public class ProxyHTTP extends Thread{
private Socket csock;
private Socket socketServeur;
public ProxyHTTP(Socket s){
csock = s;
}
public void run(){
try {
byte[] body;
BufferedInputStream buf = new BufferedInputStream(this.csock.getInputStream());
String line = readline(buf);
String header = line + "\r\n";
// Stocke la taille du body
int cl = 0;
String host = "";
while (!(line.equals(""))){
// récupère le nom de l'hote
if (line.startsWith("Host:")){
host = (line.split(" ",2))[1];
}
// récupère la taille du body s'il est présent
if (line.startsWith("Content-Length")){
String[] words = line.split(" ",2);
cl = Integer.parseInt(words[1]);
}
// lecture de la ligne suivante
line=readline(buf);
// on recompose le header
header += line;
header += "\r\n";
}
this.socketServeur = new Socket(host,80);
BufferedOutputStream serverout = new BufferedOutputStream(this.socketServeur.getOutputStream());
serverout.write(header.getBytes());
if (cl > 0){
body = new byte[cl];
buf.read(body,0,cl);
serverout.write(body);
}
} catch (Exception e){
e.printStackTrace();
System.exit(0);
}
try {
byte[] body;
BufferedInputStream serveurIn = new BufferedInputStream(this.socketServeur.getInputStream());
String line = readline(serveurIn);
String header = line + "\r\n";
// Stocke la taille du body
int cl = 0;
String host = "";
while (!(line.equals(""))){
// récupère le nom de l'hote
if (line.startsWith("Host:")){
host = (line.split(" ",2))[1];
}
// récupère la taille du body s'il est présent
if (line.startsWith("Content-Length")){
String[] words = line.split(" ",2);
cl = Integer.parseInt(words[1]);
}
// lecture de la ligne suivante
line=readline(serveurIn);
// on recompose le header
header += line;
header += "\r\n";
}
BufferedOutputStream clientOut = new BufferedOutputStream(csock.getOutputStream());
clientOut.write(header.getBytes());
if (cl > 0){
body = new byte[cl];
serveurIn.read(body,0,cl);
clientOut.write(body);
}
} catch (Exception e){
e.printStackTrace();
System.exit(0);
}
}
private String readline (InputStream in) {
// reads a line of text from an InputStream
StringBuffer data = new StringBuffer("");
int c;
try {
// if we have nothing to read, just return null
in.mark(1);
if (in.read() == -1) return null;
else in.reset();
while ((c = in.read()) >= 0) {
// check for an end-of-line character
if ((c == 0) || (c == 10) || (c == 13)) break;
else data.append((char)c);
}
// deal with the case where the end-of-line terminator is \r\n
if (c == 13) {
in.mark(1);
if (in.read() != 10) in.reset();
}
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
// and return what we have
return data.toString();
}
} |