Ajouter un commentaire

guignette
Protocole http en java

bonjour à tous je suis débutant en java et j'aimerai augmenter mes connaissances dans ce langage. Pour cela j'ai décidé d'essayer de comprendre un programme pour coder le protocole http.
Le code est le suivant.Pourriez vous m'expliquer ce code "ligne par ligne" et plus particulièrement les méthodes de classe "traiterRequete", "traiterGET", "traiterPOST".
Merci d'avance pour vos réponses :

import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.io.FilenameFilter;

public class ServeurPARI{
ServerSocket srvk;
DataOutputStream dos = null;
BufferedReader br = null;
Socket sck = null;

final static String CRLF = "\r\n";

public ServeurPARI (int port) throws IOException {
srvk = new ServerSocket (port);
}

public void go (){
int n = 0;
String S=null;
while (true){
try {
sck = srvk.accept ();
dos = new DataOutputStream (sck.getOutputStream ());
br = new BufferedReader(new InputStreamReader (sck.getInputStream()));
traiterRequete(br);
sck.close();
}
catch (IOException e) { System.out.println("ERREUR "+ e);}
}
}
// Chaines de caracteres utilisees pour composer l'entete de reponse
String serverLine = "Server: Simple Serveur de TP PARI64";
String statusLine = null;
String contentTypeLine = null;
String entityBody = null;
String contentLengthLine = null;

public void traiterRequete(BufferedReader br) throws IOException {
String S;
while(true) {
// On lit des lignes jusqu'à trouver une requete (GET ou POST)
S = br.readLine();
if(S.startsWith("GET")) traiterGET(S.substring(5, S.length()-9));
if(S.startsWith("POST")) traiterPOST(S.substring(6, S.length()-9));
if(S.equals("")) break;
// On traite la requete : traiterGET ot traiterPOST

// on lit toutes lignes qui suivent, jusqu'à une ligne vide -> break;
}

}



private void traiterGET(String f) throws IOException {
File file = new File(f);
if(file.exists()){
statusLine = "HTTP/1.1 200 OK";
if (f.endsWith(".gif") || f.endsWith(".GIF"))
contentTypeLine = "Content-Type: image/gif";
if (f.endsWith(".jpg") || f.endsWith(".JPG") || f.endsWith(".jpeg") || f.endsWith(".JPEG"))
contentTypeLine = "Content-Type: image/gif";
if (f.endsWith(".html") || f.endsWith(".HTML") || f.endsWith("htm") || f.endsWith("HTM"))
contentTypeLine = "Content-Type: text/html";
contentLengthLine ="Content-Length: " + Long.toString(file.length());
entete();
FileInputStream fis = new FileInputStream(file);
envoiFichier(fis, dos);
fis.close();
}
else{
entityBody = "File not Found" + CRLF;
statusLine = "HTTP/1.1 404 Not Found";
contentTypeLine = "Content-Type: text/html";
contentLengthLine ="Content-Length: " + Long.toString(entityBody.length());
entete();
envoi(entityBody);
}
// si le fichier "f" existe on prepare les diverses lignes
// de l'entete, on l'envoie et on envoie le contenu du fichier

// si le fichier n'existe pas, on prepare un entete "erreur"
// on l'envoie, et on envoie ensuite un "entitybody" contenant
// le code HTML de la page d'erreur.
}

private void envoiFichier(FileInputStream fis, OutputStream os)
throws IOException
{
byte[] buffer = new byte[1024] ;
int bytes = 0 ;
while ((bytes = fis.read(buffer)) != -1 ) {
os.write(buffer, 0, bytes);
}
envoi(CRLF);
}

private void traiterPOST(String f) throws IOException {
String S = "";
int length = 0;
char[] params;
while(true) {
S = br.readLine();
if(S.startsWith("Content-Length: ")) length = Integer.parseInt(S.substring(16));
if(S.equals("")){
params = new char[length];
br.read(params, 0, length);
break;
}
}
System.out.println(f);
System.out.println(length);
System.out.println(params);
File file = new File(f);
if(file.exists()){
statusLine = "HTTP/1.1 200 OK";
if (f.endsWith(".gif") || f.endsWith(".GIF"))
contentTypeLine = "Content-Type: image/gif";
if (f.endsWith(".jpg") || f.endsWith(".JPG") || f.endsWith(".jpeg") || f.endsWith(".JPEG"))
contentTypeLine = "Content-Type: image/gif";
if (f.endsWith(".html") || f.endsWith(".HTML") || f.endsWith("htm") || f.endsWith("HTM"))
contentTypeLine = "Content-Type: text/html";
contentLengthLine ="Content-Length: " + Long.toString(file.length()+length);
entete();
entityBody = new String(params);
envoi(entityBody);
FileInputStream fis = new FileInputStream(file);
envoiFichier(fis, dos);
fis.close();
}
else{
entityBody = "File not Found" + CRLF;
statusLine = "HTTP/1.1 404 Not Found";
contentTypeLine = "Content-Type: text/html";
contentLengthLine ="Content-Length: " + Long.toString(entityBody.length());
entete();
envoi(entityBody);
}
// Comme pour traiterGET, mais on lit la chaîne de caractères envoyée
// par le Navigateur avec les paramètres, et on analyse cette chaine
// on renvoie au navigateur les paramètres trouvés dans une page HTML
// pour qu'il les affiche
//
}

private void envoi(String m) throws IOException {
System.out.print("ENVOI :" + m);
dos.write(m.getBytes());
}

private void entete() throws IOException {
envoi(statusLine+CRLF);
envoi(serverLine+CRLF);
envoi(contentTypeLine+CRLF);
envoi(contentLengthLine+CRLF);
envoi(CRLF);
}

public static void main (String args []) throws IOException {
ServeurPARI s = new ServeurPARI (Integer.parseInt (args[0]));
s.go ();
}
}

Filtered HTML

Plain text

CAPTCHA
Cette question permet de vérifier que vous n'êtes pas un robot spammeur :-)
 RRRR   RRRR    CCC  III  H  H 
R R R R C I H H
RRRR RRRR C I HHHH
R R R R C I H H
R RR R RR CCC III H H