en cours partie 3

This commit is contained in:
Minh VU
2025-02-05 02:02:16 +01:00
parent bfa7cd812d
commit f9d8981119
15 changed files with 146 additions and 10 deletions

View File

@@ -0,0 +1,8 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="AutoCloseableResource" enabled="true" level="WARNING" enabled_by_default="true">
<option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,java.io.PrintStream,printf,java.lang.foreign.Arena,ofAuto,java.lang.foreign.Arena,global,javax.net.SocketFactory,createSocket" />
</inspection_tool>
</profile>
</component>

View File

@@ -6,21 +6,26 @@ Le 4 février 2025
## Partie I: Le classique serveur Echo
### EXERCICE 1 : Version séquentielle
Nous nous sommes inspirés du cours pour implémenter le serveur écho.
### EXERCICE 2 : Test et capture
![Echo sur client utilisant ClientHandlerBytes](fichier_pour_CR/p1_ex2_bytes.png)
![Echo sur client utilisant ClientHandlerBytes](images/p1_ex2_bytes.png)
quand on voit le contenu du TCP, on voit tout le texte d'échange mais on voit qu'il y a un espace entre chaque 8 caractères quand on passe la taille du buffer. Et à la fin du caractère, nous pouvons voir le dernier caractère pour déterminer la fin du texte.
Quand on voit le contenu du TCP, on voit tout le texte d'échange mais on voit qu'il y a un espace entre chaque 8 caractères quand on passe la taille du buffer. Et à la fin du caractère, nous pouvons voir le dernier caractère pour déterminer la fin du texte.
![Follow> TCP Stream](fichier_pour_CR/p1_ex2_wireshark.png)
![Follow> TCP Stream](images/p1_ex2_wireshark.png)
la taille des segments TCP est cohérente avec la taille du buffer.
La taille des segments TCP est cohérente avec la taille du buffer.
Dans "Bonjour tout le monde !" nous avons 13 octets (comptez le dernier caractère à la fin) de texte et 64 octets pour échanger des informations pour un total de 77 octets.
Dans le texte "Je teste, je suis content, je dors" nous avons 99 octets, dont 64 octets pour le protocole d'échange et l'IP, et 35 pour le texte.
![Data cohérant](fichier_pour_CR/p1_ex2_je_teste.png)
![Data cohérant](images/p1_ex2_je_teste.png)
### EXERCICE 3 : Version Multithreadée
Dans cet exercice, on réussit à créer un multi thread pour 4 personnes seulement, on teste avec le 5ème et il ne peut pas être "echo"
## Partie II: Implémentation dun client HTTPping
@@ -50,10 +55,27 @@ En vous aidant de la spec décrivez les grandes étapes du protocole pour la ré
Par la suite nous vous suggérons dutiliser loption “Connection: Close” dans les réponses.
```
### Exercice 2 : Récupération dune page avec netcat
```
Pensez à faire un retour à la ligne.
example.com supporte-t-il le protocole HTTP/1.0 ?
quel est lencodage utilisé pour le type de retour ?
que se passe-t-il si vous ajoutez “Accept-Encoding : gzip” ?
```
### Exercice 3, Implémentation du HTTP ping
### Exercice 4 : Socket securisée
![Teste HTTPS avec ncat](images/p2_ex4_https.png)
Ensuite, nous devons changer `GET / HTTP/1.1` en `GET / HTTP/1.0` pour détecter https, son port et créer SSLSocketFactory pour détecter le http sécurisé.
On lance wireshark et obtient le message crypté. Malheureusement, je ne peux pas mettre le fichier pcapng car j'utilise l'adresse IP chez moi.
J'ai mis toute la partie http en code commenté.
![Verification HTTPS](images/p2_ex4_wireshark.png)
Vérification de message encrypté:
![Message encrypté](images/p2_ex4_ecrypted.png)
## Partie III : Implémentation dun serveur HTTP simple

View File

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View File

Before

Width:  |  Height:  |  Size: 136 KiB

After

Width:  |  Height:  |  Size: 136 KiB

View File

Before

Width:  |  Height:  |  Size: 31 KiB

After

Width:  |  Height:  |  Size: 31 KiB

View File

Before

Width:  |  Height:  |  Size: 60 KiB

After

Width:  |  Height:  |  Size: 60 KiB

BIN
images/p2_ex4_ecrypted.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

BIN
images/p2_ex4_https.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

BIN
images/p2_ex4_wireshark.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 KiB

View File

@@ -1,5 +1,6 @@
package fr.istic.pr.ping;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
@@ -19,8 +20,8 @@ public class HttpPing {
}
public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
System.out.println(ping("www.example.com", 80));
//System.out.println(ping("www.example.com", 80));
System.out.println(ping("www.google.fr", 443));
}
public static PingInfo ping(String host, int port) throws UnknownHostException, IOException, InterruptedException {
@@ -28,13 +29,17 @@ public class HttpPing {
long time = System.currentTimeMillis();
Socket socket = new Socket(host, port);
//Socket socket = new Socket(host, port);
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
Socket socket = factory.createSocket(host, port);
// UTILISER PrintWriter et BufferedReader
PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
/// Envoyer l'entête
//https://stackoverflow.com/questions/9405620/send-http-header-for-identification
out.println("GET / HTTP/1.1");
//out.println("GET / HTTP/1.1");
out.println("GET / HTTP/1.0");
// Penser à préciser l'Host dans l'en-tête
out.println("Host: " + host);
// L'en-tête doit également contenir :
@@ -46,7 +51,8 @@ public class HttpPing {
String line;
while ((line=in.readLine()) != null) {
String[] ligne =line.split(" ");
if (ligne[0].equals("HTTP/1.1")){
//if (ligne[0].equals("HTTP/1.1")){
if (ligne[0].equals("HTTP/1.0")){
info.code=Integer.parseInt(ligne[1]);
break;
}

View File

@@ -0,0 +1,22 @@
package fr.istic.pr.serveur;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Affiche {
public static void main(String[] args) throws IOException {
String path = "test.html";
File f = new File("./www/" + path);
if (f.exists() && !f.isDirectory()) {
System.out.println(f.getAbsolutePath());
System.out.println(f.exists());
BufferedReader fin = new BufferedReader(new FileReader(f));
String line = "";
while ((line = fin.readLine()) != null) {
System.out.println(line);
}
}
}
}

View File

@@ -0,0 +1,73 @@
package fr.istic.pr.serveur;
import java.io.*;
import java.net.Socket;
public class HTTPHandler implements ClientHandler, Runnable {
private Socket socket;
public HTTPHandler(Socket socket) {
this.socket = socket;
}
@Override
public void handle() {
try {
// Crée les print writer et buffered reader
PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// lit l'entête de la requête
String premiere_ligne=in.readLine();
//TODO
// Appelle doGet si la méthode est une méthode GET.
//TODO
}catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try{
socket.close();
}catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
public void doGet(String pagepath, PrintWriter out) {
//Vérifie que le fichier existe
File f = new File("./www/" + pagepath);
// si le fichier existe :
if (f.exists() && !f.isDirectory()) {
//Ecrit l'en-tête de réponse (Code, Content-type, Connexion, ...)
out.println("HTTP/1.1 200 OK");
out.println("Content-Type: text/html");
out.println("Connection: close");
out.println();
}else{
//Ecrit le contenu du fichier si il existe
// sinon
// appelle la méthode send404.
send404(out);
}
}
public void send404(PrintWriter out) {
//Envoie une réponse 404 si le fichier n'existe pas.
out.println("HTTP/1.1 404 File Not Found");
out.println("Content-Type: text/html");
out.println("Connection: close");
}
public void doError(PrintWriter out) {
//Envoie une réponse avec le code 405 : Method Not Allowed
out.println("HTTP/1.1 405 Method Not Allowed");
out.println("Content-Type: text/html");
out.println("Connection: close");
}
@Override
public void run() {
this.handle();
}
}

View File

@@ -0,0 +1,5 @@
package fr.istic.pr.serveur;
public class ServeurHTTP {
//TODO
}