Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.xml.internal.ws.api.ha.StickyFeature;
- import org.omg.CORBA.NameValuePair;
- import javax.imageio.ImageIO;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.io.*;
- import java.net.Socket;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.stream.Collectors;
- public class httpRead {
- private String command;
- private int code = 200;
- private String phrase = "OK";
- private String returnBody = "";
- private String returnMSG = "";
- private String method = "GET";
- private String version = "";
- private String uri = "";
- private boolean filePermission = true;
- private ArrayList<String> URLlist = new ArrayList<String>();
- public httpRead(String command) {
- this.command = command;
- }
- public String returnHeader = "";
- public boolean istxt = true;
- public void main(Socket socket) throws IOException {
- URLlist.add(command);
- parseURL(command);
- if (isValid()) {
- if (method.equals("GET"))
- try {
- {
- String name = uri;
- File file = new File("." + name);
- GET(file, name);
- }
- } catch (IOException e) {
- System.out.println("ERROR" + e);
- }
- else if (method.equals("POST")) {
- POST(socket);
- } else if (method.equals("PUT")) {
- PUT(socket);
- }
- } else {
- //ERROR 500
- File error = new File("." + "/error500.html");
- FileReader FR = new FileReader(error);
- BufferedReader er = new BufferedReader(FR);
- String errorContent = er.lines().collect(Collectors.joining());
- returnBody = errorContent;
- System.out.println("should print file content" + errorContent);
- code = 500;
- phrase = "SERVER_ERROR";
- }
- System.out.println(command);
- }
- public void GET(File file, String name) throws IOException {
- String html = "html";
- String png = "png";
- if (isValid()) {
- if (file.isDirectory()) {
- StringBuilder list = new StringBuilder("<html><head><title>Contents of Directory ");
- list.append(uri);
- list.append("</title></head><body><h1>Contents of Directory ");
- list.append(uri);
- list.append("</h1><hr><pre>");
- File[] files = file.listFiles();
- for (File subfile : files) {
- list.append(" <a href=\"" + subfile.getPath() + "\">" + subfile.getPath() + "</a>\n");
- }
- list.append("<hr></pre></body></html>");
- returnBody = list.toString();
- //DEALS WITH HTTP FILE REQUESTS
- } else if (file.exists() && name.contains(html)) {
- //CHECKS PERMISSION AND PROCESSES ERROR 403
- //if (!checkPermission(file)) {
- // file = new File("." + "/error403.html");
- //}
- FileReader FR = new FileReader(file);
- BufferedReader reader = new BufferedReader(FR);
- String fileContent = reader.lines().collect(Collectors.joining());
- returnBody = fileContent;
- System.out.println("should print file content" + fileContent);
- //DEALS WITH PNG FILE REQUESTS
- } else if (file.exists() && name.contains(png)) {
- istxt = false;
- int size=(int)file.length();
- byte[] data=new byte[size];
- DataInputStream in= new DataInputStream( new FileInputStream(file));
- in.readFully(data);
- in.close();
- //ERROR 404 NOT FOUND
- } else if (!file.exists()) {
- File error = new File("." + "/error404.html");
- FileReader FR = new FileReader(error);
- BufferedReader er = new BufferedReader(FR);
- String errorContent = er.lines().collect(Collectors.joining());
- returnBody = errorContent;
- System.out.println("should print file content" + errorContent);
- code = 404;
- phrase = "NOT_FOUND";
- }
- } else {
- File error = new File("." + "/error500.html");
- FileReader FR = new FileReader(error);
- BufferedReader er = new BufferedReader(FR);
- String errorContent = er.lines().collect(Collectors.joining());
- returnBody = errorContent;
- System.out.println("should print file content" + errorContent);
- code = 500;
- phrase = "SERVER_ERROR";
- }
- }
- public void POST(Socket socket) throws IOException {
- BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
- // read request
- String line;
- line = in.readLine();
- StringBuilder raw = new StringBuilder();
- raw.append("" + line);
- boolean isPost = line.startsWith("POST");
- int contentLength = 0;
- while (!(line = in.readLine()).equals("")) {
- raw.append('\n' + line);
- if (isPost) {
- final String contentHeader = "Content-Length: ";
- if (line.startsWith(contentHeader)) {
- contentLength = Integer.parseInt(line.substring(contentHeader.length()));
- }
- }
- StringBuilder body = new StringBuilder();
- if (isPost) {
- int c = 0;
- for (int i = 0; i < contentLength; i++) {
- c = in.read();
- body.append((char) c);
- }
- }
- raw.append(body.toString());
- // publishProgress(raw.toString());
- // send response
- out.write("HTTP/1.1 200 OK\r\n");
- out.write("Content-Type: text/html\r\n");
- out.write("\r\n");
- // out.write(new Date().toString());
- if (isPost) {
- out.write("<br><u>" + body.toString() + "</u>");
- } else {
- out.write("<form method='POST'>");
- out.write("<input name='name' type='text'/>");
- out.write("<input type='submit'/>");
- out.write("</form>");
- }
- }
- }
- public void PUT(Socket socket)
- {
- }
- public boolean checkPermission(File file) {
- String restricted = ""; // Manually code how the file should be
- String name= "/permission.html";
- File check = new File("."+name);
- try {
- GET(check, name);
- }
- catch(IOException e)
- {System.out.println("Error"+e);}
- try {
- if (!filePermission)
- {
- }
- else if (file.equals(restricted)) {
- System.out.println("No permission: " + file);
- filePermission = false;
- }
- } catch (Exception e) {
- e.getMessage();
- }
- return filePermission;
- }
- public void parseURL(String command)
- {
- String[] parts=command.split("\\s+");
- method=(parts[0]);
- if(parts[2]!=null)
- {
- version = (parts[2]);
- }
- else{version="HTTP/1.1";}
- uri=(parts[1]);
- }
- public String response()
- {
- String head=makeHead();
- returnMSG+=head+returnBody;
- System.out.println(returnMSG+"\r\n");
- return returnMSG;
- }
- public boolean isValid()
- { boolean bool= true;
- //DETERMINE IF COMMAND IS VALID HTTP
- //DETERMINE IF CLIENT HAS AUTHORIZATION
- //CHANGE CODE ACCORDINGLY(200,403,404 etc)
- return bool;
- }
- private String makeHead()
- {
- returnHeader+=version+" "+ code + " " + phrase;
- String server="Server: My Computer";
- String date="Date: Today";
- String length="Content Length: ";
- int ln=returnBody.length();
- String len=Integer.toString(ln);
- length+=len;
- String type="Content Type: ";
- if (!istxt)
- {type+= ".png/html";}
- else{type+=".txt/html";}
- String s="\r\n";
- returnHeader+= s + date+ s + server+ s + length + s + type +s+s;
- return returnHeader;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement