CR
This commit is contained in:
@@ -19,10 +19,15 @@ public class HTTPHandler implements ClientHandler, Runnable {
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
|
||||
// lit l'entête de la requête
|
||||
String premiere_ligne=in.readLine();
|
||||
//TODO
|
||||
String[] premiere_ligne=in.readLine().split(" ");
|
||||
System.out.println(premiere_ligne.length);
|
||||
System.out.println(premiere_ligne[0] + " " + premiere_ligne[1]);
|
||||
// Appelle doGet si la méthode est une méthode GET.
|
||||
//TODO
|
||||
if (premiere_ligne[0].equals("GET")){
|
||||
doGet(premiere_ligne[1],out);
|
||||
}else{
|
||||
doError(out);
|
||||
}
|
||||
}catch (IOException e) {
|
||||
System.out.println(e.getMessage());
|
||||
} finally {
|
||||
@@ -34,18 +39,26 @@ public class HTTPHandler implements ClientHandler, Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
public void doGet(String pagepath, PrintWriter out) {
|
||||
public void doGet(String pagepath, PrintWriter out) throws IOException {
|
||||
//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");
|
||||
|
||||
//Ecrit le contenu du fichier si il existe
|
||||
BufferedReader fin = new BufferedReader(new FileReader(f));
|
||||
String line;
|
||||
while ((line = fin.readLine()) != null) {
|
||||
out.println(line);
|
||||
}
|
||||
out.println("Connection: close");
|
||||
out.println();
|
||||
}else{
|
||||
//Ecrit le contenu du fichier si il existe
|
||||
// sinon
|
||||
// appelle la méthode send404.
|
||||
send404(out);
|
||||
@@ -57,6 +70,7 @@ public class HTTPHandler implements ClientHandler, Runnable {
|
||||
out.println("HTTP/1.1 404 File Not Found");
|
||||
out.println("Content-Type: text/html");
|
||||
out.println("Connection: close");
|
||||
out.println("");
|
||||
}
|
||||
|
||||
public void doError(PrintWriter out) {
|
||||
@@ -64,10 +78,21 @@ public class HTTPHandler implements ClientHandler, Runnable {
|
||||
out.println("HTTP/1.1 405 Method Not Allowed");
|
||||
out.println("Content-Type: text/html");
|
||||
out.println("Connection: close");
|
||||
out.println("");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
this.handle();
|
||||
try {
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
String message = in.readLine();
|
||||
System.out.println(message);
|
||||
String[] a= message.split(" ");
|
||||
if(a[0].equals("GET")) {
|
||||
doGet("test.html",new PrintWriter(new OutputStreamWriter(socket.getOutputStream())));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,24 @@
|
||||
package fr.istic.pr.serveur;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class ServeurHTTP {
|
||||
//TODO
|
||||
public static void main(String[] args) throws IOException {
|
||||
//Attente sur le port 8080s
|
||||
int portDuServer = 8080;
|
||||
try{
|
||||
ServerSocket serverSocket = new ServerSocket(portDuServer);
|
||||
Executor service = Executors.newFixedThreadPool(4);
|
||||
while(true){
|
||||
Socket socket = serverSocket.accept();
|
||||
service.execute(new HTTPHandler(socket));
|
||||
}
|
||||
}catch(Exception e){
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
10
src/main/java/fr/istic/pr/serveur/www/test.html
Normal file
10
src/main/java/fr/istic/pr/serveur/www/test.html
Normal file
@@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Coucou</title>
|
||||
</head>
|
||||
<body>
|
||||
Cette page est une
|
||||
page toute simple
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user