Advertisement
Guest User

Untitled

a guest
May 21st, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.ServerSocket;
  3. import java.net.Socket;
  4. import java.util.Date;
  5.  
  6. public class Server {
  7.     public final static char CR  = (char) 0x0D;
  8.     public final static char LF  = (char) 0x0A;
  9.     public static void main(String[] args) throws IOException {
  10.         /**
  11.          * Port de base: 8080
  12.          */
  13.         int port;
  14.         if(args.length == 0) {
  15.             port = 8080;
  16.         } else {
  17.             port = Integer.valueOf(args[0]);
  18.         }
  19.  
  20.         /**
  21.          * Définition du Document-Root
  22.          */
  23.         String docroot = "C:\\Users\\Jules\\IdeaProjects\\http\\data";
  24.  
  25.         /**
  26.          * Ouvre la socket
  27.          */
  28.         ServerSocket serv = new ServerSocket(port);
  29.         System.out.println("Le serveur est lancé et écoute sur le port " + port);
  30.  
  31.         /**
  32.          * Boucle infinie d'écoute
  33.          */
  34.         while(true) {
  35.             Socket socket = serv.accept();
  36.             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  37.             OutputStream out = new BufferedOutputStream(socket.getOutputStream());
  38.             String request = in.readLine();
  39.  
  40.             /**
  41.              * Evite d'avoir des requetes null
  42.              */
  43.             if(request == null) {
  44.                 continue;
  45.             }
  46.  
  47.             /**
  48.              * Traite la requete
  49.              */
  50.             System.out.println("[" + socket.getInetAddress().getHostAddress() + ":" + socket.getPort() + "] " + request);
  51.             if(request.startsWith("GET")) {
  52.                 String fichier = request.substring(4, request.length()-9).trim();
  53.                 String chemin = docroot + "/" + fichier;
  54.                 File f = new File(chemin);
  55.                 if(f.isDirectory()) {
  56.                     chemin = chemin + "index.html";
  57.                     f = new File(chemin);
  58.                 }
  59.  
  60.                 /**
  61.                  * Récupère le type
  62.                  */
  63.                 String type;
  64.                 if(chemin.endsWith(".html")) {
  65.                     type = "text/html";
  66.                 } else if(chemin.endsWith(".jpg") || chemin.endsWith(".jpeg")) {
  67.                     type = "image/jpeg";
  68.                 } else if(chemin.endsWith(".gif")) {
  69.                     type = "image/gif";
  70.                 } else {
  71.                     type = "text/plain";
  72.                 }
  73.  
  74.                 /**
  75.                  * Va chercher le fichier et l'envoie, s'il existe pas 404
  76.                  */
  77.                 String http, content;
  78.                 String today = "Date: " + new Date();
  79.                 String server = "Server: HttpServer 1.0";
  80.                 try {
  81.                     InputStream is = new FileInputStream(f);
  82.                     http = "HTTP/1.1 200 OK";
  83.                     content = "Content-Type: " + type;
  84.                     out.write(http.getBytes());
  85.                     out.write(CR);
  86.                     out.write(LF);
  87.                     writeBasicThings(out, content, today, server);
  88.                     while(is.available() > 0) {
  89.                         out.write(is.read());
  90.                     }
  91.                     is.close();
  92.                     System.out.println("[SERVER] Envoi d'un 200");
  93.                 } catch(FileNotFoundException e) {
  94.                     http = "HTTP/1.1 404 Not Found";
  95.                     writeBasicThings(out, http, today, server);
  96.                     System.out.println("[SERVER] Envoi d'un 404");
  97.                 }
  98.             }
  99.             out.flush();
  100.             socket.close();
  101.         }
  102.     }
  103.  
  104.     /**
  105.      * Méthode permettant de write les choses basique
  106.      * tel que le code de réponse HTTP, la date ou encore le serveur
  107.      * @param out outputtream
  108.      * @param http réponse
  109.      * @param today date
  110.      * @param server serveur
  111.      * @throws IOException
  112.      */
  113.     private static void writeBasicThings(OutputStream out, String http, String today, String server) throws IOException {
  114.         out.write(http.getBytes());
  115.         out.write(CR);
  116.         out.write(LF);
  117.         out.write(today.getBytes());
  118.         out.write(CR);
  119.         out.write(LF);
  120.         out.write(server.getBytes());
  121.         out.write(CR);
  122.         out.write(LF);
  123.         out.write(CR);
  124.         out.write(LF);
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement