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

@@ -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
}