Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
FVHTTP Server - ProcessQuery.java

ProcessQuery.java

Caricato da: Fraioveio
Scarica il programma completo

  1. package fvhttpserver;
  2.  
  3. import java.io.*;
  4. import java.util.StringTokenizer;
  5.  
  6. public class ProcessQuery {
  7.     public static void process(String query, PrintStream out, String ip, ClientConnection cc) {
  8.         String pl = "";
  9.        
  10.         for(int i=0;i<query.length();i++) {
  11.             if(query.charAt(i) == '\r' || query.charAt(i) == '\n') {
  12.                 pl = query.substring(0, i);
  13.                 break;
  14.             }
  15.         }
  16.        
  17.         System.out.println("[" + ip + "]: " + pl);
  18.        
  19.         StringTokenizer st = new StringTokenizer(pl, " ", false);
  20.        
  21.         String cmd = st.nextToken();
  22.        
  23.         boolean uq = false;
  24.        
  25.         if(cmd.toLowerCase().equals("get") || cmd.toLowerCase().equals("post")) {
  26.             uq = true;
  27.             String file = st.nextToken();
  28.             file = file.substring(1).replace("/", "\\").replace("%20", " ");
  29.            
  30.             File f = new File(Main.basedir.equals("") ? file : Main.basedir + "\\" + file);
  31.            
  32.             if(f.isDirectory()) {
  33.                 f = new File(f.getPath() + "\\" + "index.html");
  34.                 if(!f.exists())
  35.                     f = new File(f.getPath() + "\\" + "index.htm");
  36.             }
  37.            
  38.             if(f.exists()) {
  39.                 try {
  40.                     FileInputStream in = new FileInputStream(f);
  41.                    
  42.                     out.println("HTTP/1.0 200 OK\r"
  43.                             + "Content-Length: " + f.length() + "\r\n");
  44.                     System.out.println("[" + ip + "][Server]: HTTP/1.0 200 OK");
  45.                    
  46.                     byte[] tmp = new byte[2048];
  47.                     int len = 0;
  48.                    
  49.                     while((len = in.read(tmp)) != -1 && !cc.finish) {
  50.                         out.write(tmp, 0, len);
  51.                     }
  52.                 } catch (IOException ex) {
  53.                     System.out.println(ex);
  54.                 }
  55.                
  56.             } else {
  57.                 out.println("HTTP/1.0 404 Not Found");
  58.                 System.out.println("[" + ip + "][Server]: HTTP/1.0 404 Not Found");
  59.             }
  60.         }
  61.        
  62.         if(!uq) {
  63.             out.println("HTTP/1.0 400 Bad Request");
  64.             System.out.println("[" + ip + "][Server]: HTTP/1.0 400 Bad Request");
  65.         }
  66.     }
  67. }