Ethangx8

zz

Oct 13th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.50 KB | None | 0 0
  1. import java.awt.image.BufferedImage;
  2. import java.io.*;
  3. import java.net.*;
  4. import java.util.*;
  5.  
  6. import javax.imageio.ImageIO;
  7.  
  8. public class DummyHttpWorker implements Runnable {
  9.   private Socket s;  // connection socket
  10.    
  11.   public DummyHttpWorker(Socket soc) {
  12.     this.s = soc;
  13.      
  14.   }
  15.    
  16.   public void inviaRisposta(PrintWriter out)
  17.   {
  18.     out.println("HTTP/1.0 404 NOT FOUND");
  19.     out.println("Server: Pippo");
  20.     out.println("Date: "+ (new Date().toString()));
  21.     out.println("Content-type: text/html");
  22.     //out.println("Content-length: 0");
  23.     out.println();  // blank line before message body..
  24.     out.println("<!DOCTYPE HTML PUBLIC '-//IETF//DTD HTML 2.0//EN'>");
  25.     out.println("<html><head><title>404 Not Found</title></head><body>");
  26.     out.println("<h1>Pippo server says: Not Found!</h1>");
  27.     out.println("<p>The requested URL was not found on this server.</p>");
  28.     out.println("<hr></body></html>");
  29.   }  
  30.    
  31.    
  32.   public void run()
  33.   {
  34.     PrintWriter out;
  35.     BufferedReader in;
  36.     System.out.println ("Connection successful! Waiting for input..");
  37.     try    {
  38.       out = new PrintWriter(s.getOutputStream(),true);
  39.       // PrintWriter permette di stampare su un output stream
  40.       in = new BufferedReader(new InputStreamReader(s.getInputStream()));
  41.       // BufferedReader legge testo da input stream e permette di effettuare operazioni di lettura caratteri e righe
  42.       String inputLine, method = null, res = null;
  43.        
  44.       while ((inputLine = in.readLine()) != null)
  45.       {
  46.         System.out.println ("Received "+inputLine.length()+" chars: " + inputLine);
  47.         // Check if there's a blank line: new line or carriage return as first char
  48.         String[] data = inputLine.split(" ");
  49.        
  50.         //Nel caso la richiesta sia GET
  51.         if(data[0].equals("GET")) {
  52.             method  = "GET";
  53.             res = data[1];
  54.         }
  55.         if(inputLine.length() == 0) {
  56.             out.println("HTTP/1.0 200 OK");
  57.             out.println("Server: GX8Server");
  58.             out.println("Date: "+ (new Date().toString()));
  59.            
  60.             //Se richiede / restituisco l'index
  61.             if(res.equals("/"))
  62.                 sendRes("index.html", out);
  63.             else{
  64.                 //Apro il file
  65.                 File f = new File(res.substring(1));
  66.                 String filename = f.getName();
  67.                
  68.                 //Controllo che il file esista
  69.                 if(f.exists() && !f.isDirectory()) {
  70.                    
  71.                     //Controllo se è un'immagine
  72.                     if(isImage(res)) {
  73.                         //Mando l'header per specificare che è un'immagine e il tipo
  74.                         out.println("Content-type: image/"+filename.substring(filename.lastIndexOf(".")+1));
  75.                         out.println();
  76.                        
  77.                         OutputStream os = s.getOutputStream();
  78.                         sendImage(f,os); //Metodo per mandare immagini, vedi sotto
  79.                     }
  80.                     if(f.getName().endsWith(".html") || f.getName().endsWith(".txt")) //Controllo se è un file html o txt
  81.                         sendRes(filename, out); //Metodo per mandare file txt/html, vedi sotto
  82.                 }
  83.             }
  84.           break;
  85.         }        
  86.       }      
  87.       System.out.println("Closing connection..");
  88.       out.close();
  89.       in.close();
  90.        
  91.     } catch (IOException ioe) {
  92.       System.out.println("IOException on socket: " + ioe);
  93.       ioe.printStackTrace();
  94.        
  95.     } catch (Exception e) {
  96.         // TODO Auto-generated catch block
  97.         e.printStackTrace();
  98.     }
  99.      
  100.     try {
  101.       s.close();
  102.     } catch (IOException ioe) {
  103.       System.out.println("IOException on socket: " + ioe);
  104.       ioe.printStackTrace();
  105.     }
  106.      
  107.   }  
  108.  
  109.   //Controlla se il file è un'immagine
  110.   private boolean isImage(String estensione) {
  111.       return (estensione.endsWith(".jpg")||estensione.endsWith(".png") || estensione.endsWith(".gif"));
  112.   }
  113.  
  114.   //Funzione per mandare file txt e html
  115.   private void sendRes(String res, PrintWriter out) throws Exception {
  116.       out.println("Content-type: text/html");
  117.       out.println();
  118.       FileInputStream fin = new FileInputStream(res);
  119.       while(fin.available()!=0){
  120.           int info= fin.read();
  121.           out.write(info);
  122.       }
  123.   }
  124.  
  125.   //Funzione per mandare immagini
  126.   private void sendImage(File f, OutputStream out) throws IOException {
  127.       String filename = f.getName();
  128.       String estensione = filename.substring(filename.lastIndexOf(".")+1);
  129.       BufferedImage buffImg = ImageIO.read(f);
  130.       ImageIO.write(buffImg, estensione, out);
  131.   }
  132.  
  133. }
Add Comment
Please, Sign In to add comment