Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.util.*;
- import java.text.*;
- class ServiceThread extends Thread
- {
- Socket conn = null;
- public ServiceThread(Socket c) { conn = c; }
- public void run()
- {
- try
- {
- Scanner scanin = new Scanner(conn.getInputStream());
- String line = null ;
- int nlines = 0;
- String inputLines[] = new String[32];
- while(true)
- {
- line = scanin.nextLine();
- if(line.length() == 0) break;
- inputLines[nlines] = line;
- nlines++;
- }
- // create scanner to extract command and resource location
- Scanner extract = new Scanner(inputLines[0]);
- String command = extract.next();
- String resource = extract.next();
- String protocol = extract.next();
- OutputStream outs = conn.getOutputStream();
- boolean isPointOne = false;
- boolean hostFound = false;
- boolean badRequest = false;
- Date now = new Date();
- SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
- String dateString = df.format(now);
- if(protocol.compareTo("HTTP/1.1") == 0)
- {
- String checkLine = "";
- int i = 0;
- while(i < 32)
- {
- checkLine = inputLines[i];
- System.out.println(checkLine);
- if(checkLine == null) break;
- if(checkLine.contains("Host:")) { hostFound = true; break;}
- i++;
- }
- isPointOne = true;
- }
- if(isPointOne == true && hostFound == false) { badRequest = true; }
- // check for "/"
- if(!resource.startsWith("/") || badRequest == true)
- {
- String reply = "HTTP/1.0 400 Bad Request\r\n" +
- "Connection: close\r\n" +
- "Date: " + dateString + "\r\n" +
- "Content-Type: text/html\r\n" +
- "\r\n" +
- "<b>Error 400:</b> Bad request\r\n";
- outs.write(reply.getBytes());
- }
- else // good request
- {
- // check correct command
- if(command.compareTo("GET") == 0 || command.compareTo("HEAD") == 0)
- {
- String fileName = "www"+resource;
- // check to see if file exist, if not send error 404
- File requestedFile = new File(fileName);
- if (!requestedFile.exists())
- {
- String reply = "HTTP/1.0 404 Not Found\r\n" +
- "Connection: close\r\n" +
- "Date: " + dateString + "\r\n" +
- "Content-Type: text/html\r\n" +
- "\r\n" +
- "<b>Error 404:</b> File not found\r\n";
- outs.write(reply.getBytes());
- }
- else
- { // file does exist
- if(requestedFile.isDirectory())
- {
- String checkFileName = "www"+resource+"/"+"index.html";
- File indexFile = new File(indexFile);
- if(!indexFile.exists())
- {
- String reply = "HTTP/1.0 404 Not Found\r\n" +
- "Connection: close\r\n" +
- "Date: " + dateString + "\r\n" +
- "Content-Type: text/html\r\n" +
- "\r\n" +
- "<b>Error 404:</b> File not found\r\n";
- outs.write(reply.getBytes());
- }
- else
- {
- File requestedFile = new File(indexFile);
- }
- }
- InputStream inFile = new FileInputStream(requestedFile);
- byte buffer[] = new byte[1024];
- long lastModified = requestedFile.lastModified();
- Date lastModifiedDate = new Date(lastModified);
- SimpleDateFormat lmdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
- String lastModifiedDf = lmdf.format(lastModifiedDate);
- String fileLength = Long.toString(requestedFile.length()); // get file length
- String header = "HTTP/1.0 200 OK\r\n"+
- "Connection: close\r\n"+
- "Date: " + dateString + "\r\n" +
- "Last-Modified: " + lastModifiedDf + "\r\n" +
- "Content-Length: " + fileLength + "\r\n"+
- "Content-Type: text/html\r\n"+
- "\r\n";
- // send header
- outs.write(header.getBytes());
- if(command.compareTo("GET") == 0)
- {
- while(true)
- {
- int rc = inFile.read(buffer,0,1024);
- if(rc <= 0) break;
- outs.write(buffer,0,rc);
- }
- }
- }
- }
- else // incorrect command
- {
- String reply = "HTTP/1.0 501 Not Implemented\r\n" +
- "Connection: close\r\n" +
- "Date: " + dateString + "\r\n" +
- "Content-Type: text/html\r\n" +
- "\r\n" +
- "<b>Error 501:</b> Not implemented<br>\r\n";
- outs.write(reply.getBytes());
- }
- }
- // close the connection
- conn.close();
- }
- catch (IOException e)
- {
- System.err.println("HTTPServer: error reading socket");
- System.exit(1);
- }
- }
- }
- public class HTTPServer2
- {
- public static void main(String args[])
- {
- try
- {
- if(args.length != 1)
- {
- System.out.println("Usage: HTTPServer <port>");
- System.exit(1);
- }
- int port = Integer.parseInt(args[0]);
- ServerSocket serverSock = new ServerSocket(port);
- while(true)
- {
- Socket connSock = serverSock.accept();
- ServiceThread serve = new ServiceThread(connSock);
- serve.start();
- }
- }
- catch (IOException e)
- {
- System.err.println("HTTPServer error on socket");
- System.exit(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement