Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.Date;
- public class Server {
- public final static char CR = (char) 0x0D;
- public final static char LF = (char) 0x0A;
- public static void main(String[] args) throws IOException {
- /**
- * Port de base: 8080
- */
- int port;
- if(args.length == 0) {
- port = 8080;
- } else {
- port = Integer.valueOf(args[0]);
- }
- /**
- * Définition du Document-Root
- */
- String docroot = "C:\\Users\\Jules\\IdeaProjects\\http\\data";
- /**
- * Ouvre la socket
- */
- ServerSocket serv = new ServerSocket(port);
- System.out.println("Le serveur est lancé et écoute sur le port " + port);
- /**
- * Boucle infinie d'écoute
- */
- while(true) {
- Socket socket = serv.accept();
- BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- OutputStream out = new BufferedOutputStream(socket.getOutputStream());
- String request = in.readLine();
- /**
- * Evite d'avoir des requetes null
- */
- if(request == null) {
- continue;
- }
- /**
- * Traite la requete
- */
- System.out.println("[" + socket.getInetAddress().getHostAddress() + ":" + socket.getPort() + "] " + request);
- if(request.startsWith("GET")) {
- String fichier = request.substring(4, request.length()-9).trim();
- String chemin = docroot + "/" + fichier;
- File f = new File(chemin);
- if(f.isDirectory()) {
- chemin = chemin + "index.html";
- f = new File(chemin);
- }
- /**
- * Récupère le type
- */
- String type;
- if(chemin.endsWith(".html")) {
- type = "text/html";
- } else if(chemin.endsWith(".jpg") || chemin.endsWith(".jpeg")) {
- type = "image/jpeg";
- } else if(chemin.endsWith(".gif")) {
- type = "image/gif";
- } else {
- type = "text/plain";
- }
- /**
- * Va chercher le fichier et l'envoie, s'il existe pas 404
- */
- String http, content;
- String today = "Date: " + new Date();
- String server = "Server: HttpServer 1.0";
- try {
- InputStream is = new FileInputStream(f);
- http = "HTTP/1.1 200 OK";
- content = "Content-Type: " + type;
- out.write(http.getBytes());
- out.write(CR);
- out.write(LF);
- writeBasicThings(out, content, today, server);
- while(is.available() > 0) {
- out.write(is.read());
- }
- is.close();
- System.out.println("[SERVER] Envoi d'un 200");
- } catch(FileNotFoundException e) {
- http = "HTTP/1.1 404 Not Found";
- writeBasicThings(out, http, today, server);
- System.out.println("[SERVER] Envoi d'un 404");
- }
- }
- out.flush();
- socket.close();
- }
- }
- /**
- * Méthode permettant de write les choses basique
- * tel que le code de réponse HTTP, la date ou encore le serveur
- * @param out outputtream
- * @param http réponse
- * @param today date
- * @param server serveur
- * @throws IOException
- */
- private static void writeBasicThings(OutputStream out, String http, String today, String server) throws IOException {
- out.write(http.getBytes());
- out.write(CR);
- out.write(LF);
- out.write(today.getBytes());
- out.write(CR);
- out.write(LF);
- out.write(server.getBytes());
- out.write(CR);
- out.write(LF);
- out.write(CR);
- out.write(LF);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement