Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.07 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4. import java.text.*;
  5.  
  6. class ServiceThread extends Thread
  7. {
  8.     Socket conn = null;
  9.  
  10.     public ServiceThread(Socket c) { conn = c; }
  11.  
  12.     public void run()
  13.     {
  14.     try
  15.     {
  16.         Scanner scanin = new Scanner(conn.getInputStream());
  17.         String line = null ;
  18.         int nlines = 0;
  19.         String inputLines[] = new String[32];
  20.  
  21.         while(true)
  22.         {
  23.             line = scanin.nextLine();
  24.             if(line.length() == 0) break;
  25.             inputLines[nlines] = line;
  26.             nlines++;
  27.         }
  28.  
  29.         // create scanner to extract command and resource location
  30.         Scanner extract = new Scanner(inputLines[0]);
  31.         String command = extract.next();
  32.         String resource = extract.next();
  33.         String protocol = extract.next();
  34.         OutputStream outs = conn.getOutputStream();
  35.         boolean isPointOne = false;
  36.         boolean hostFound = false;
  37.         boolean badRequest = false;
  38.         Date now = new Date();
  39.         SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
  40.         String dateString = df.format(now);
  41.  
  42.         if(protocol.compareTo("HTTP/1.1") == 0)
  43.         {
  44.         String checkLine = "";
  45.         int i = 0;
  46.  
  47.         while(i < 32)
  48.         {
  49.             checkLine = inputLines[i];
  50.             System.out.println(checkLine);
  51.             if(checkLine == null) break;
  52.             if(checkLine.contains("Host:")) { hostFound = true; break;}
  53.             i++;
  54.         }
  55.  
  56.         isPointOne = true;
  57.        
  58.         }
  59.  
  60.         if(isPointOne == true && hostFound == false) { badRequest = true; }
  61.  
  62.         // check for "/"
  63.         if(!resource.startsWith("/") || badRequest == true)
  64.         {
  65.         String reply = "HTTP/1.0 400 Bad Request\r\n" +
  66.             "Connection: close\r\n" +
  67.             "Date: " + dateString + "\r\n" +
  68.             "Content-Type: text/html\r\n" +
  69.             "\r\n" +
  70.             "<b>Error 400:</b> Bad request\r\n";
  71.  
  72.         outs.write(reply.getBytes());
  73.         }
  74.         else // good request
  75.         {
  76.         // check correct command
  77.         if(command.compareTo("GET") == 0 || command.compareTo("HEAD") == 0)
  78.         {
  79.             String fileName = "www"+resource;
  80.  
  81.             // check to see if file exist, if not send error 404
  82.             File requestedFile = new File(fileName);       
  83.  
  84.             if (!requestedFile.exists())
  85.             {
  86.             String reply = "HTTP/1.0 404 Not Found\r\n" +
  87.                 "Connection: close\r\n" +
  88.                 "Date: " + dateString + "\r\n" +
  89.                 "Content-Type: text/html\r\n" +
  90.                 "\r\n" +
  91.                 "<b>Error 404:</b> File not found\r\n";
  92.  
  93.             outs.write(reply.getBytes());
  94.             }
  95.             else
  96.             { // file does exist
  97.  
  98.             if(requestedFile.isDirectory())
  99.             {
  100.                 String checkFileName = "www"+resource+"/"+"index.html";
  101.                 File indexFile = new File(indexFile);
  102.                
  103.                 if(!indexFile.exists())
  104.                 {
  105.                 String reply = "HTTP/1.0 404 Not Found\r\n" +
  106.                     "Connection: close\r\n" +
  107.                     "Date: " + dateString + "\r\n" +
  108.                     "Content-Type: text/html\r\n" +
  109.                     "\r\n" +
  110.                     "<b>Error 404:</b> File not found\r\n";
  111.  
  112.                 outs.write(reply.getBytes());
  113.                 }
  114.                 else
  115.                 {
  116.                 File requestedFile = new File(indexFile);
  117.                 }
  118.             }
  119.  
  120.             InputStream inFile = new FileInputStream(requestedFile);
  121.                 byte buffer[] = new byte[1024];
  122.             long lastModified = requestedFile.lastModified();
  123.             Date lastModifiedDate = new Date(lastModified);
  124.             SimpleDateFormat lmdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
  125.             String lastModifiedDf = lmdf.format(lastModifiedDate);
  126.  
  127.             String fileLength = Long.toString(requestedFile.length()); // get file length
  128.             String header = "HTTP/1.0 200 OK\r\n"+
  129.                 "Connection: close\r\n"+
  130.                 "Date: " + dateString + "\r\n" +
  131.                 "Last-Modified: " + lastModifiedDf + "\r\n" +
  132.                 "Content-Length: " + fileLength + "\r\n"+
  133.                 "Content-Type: text/html\r\n"+
  134.                 "\r\n";
  135.  
  136.                 // send header
  137.             outs.write(header.getBytes());
  138.        
  139.             if(command.compareTo("GET") == 0)
  140.             {
  141.                 while(true)
  142.                 {
  143.                 int rc = inFile.read(buffer,0,1024);
  144.                 if(rc <= 0) break;
  145.                 outs.write(buffer,0,rc);
  146.                 }
  147.             }
  148.             }
  149.         }
  150.         else // incorrect command
  151.         {
  152.             String reply = "HTTP/1.0 501 Not Implemented\r\n" +
  153.             "Connection: close\r\n" +
  154.             "Date: " + dateString + "\r\n" +
  155.             "Content-Type: text/html\r\n" +
  156.             "\r\n" +
  157.             "<b>Error 501:</b> Not implemented<br>\r\n";
  158.  
  159.             outs.write(reply.getBytes());
  160.         }
  161.         }
  162.         // close the connection
  163.         conn.close();
  164.     }
  165.  
  166.     catch (IOException e)
  167.     {
  168.         System.err.println("HTTPServer: error reading socket");
  169.         System.exit(1);
  170.     }
  171.     }
  172. }
  173.  
  174.  
  175.  
  176.  
  177.  
  178. public class HTTPServer2
  179. {
  180.     public static void main(String args[])
  181.     {
  182.     try
  183.     {
  184.         if(args.length != 1)
  185.         {
  186.             System.out.println("Usage: HTTPServer <port>");
  187.             System.exit(1);
  188.         }
  189.  
  190.         int port = Integer.parseInt(args[0]);
  191.         ServerSocket serverSock = new ServerSocket(port);
  192.  
  193.         while(true)
  194.         {
  195.             Socket connSock = serverSock.accept();
  196.             ServiceThread serve = new ServiceThread(connSock);
  197.             serve.start();
  198.         }
  199.     }
  200.     catch (IOException e)
  201.         {
  202.         System.err.println("HTTPServer error on socket");
  203.         System.exit(1);
  204.     }
  205.     }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement