Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.xml.internal.ws.api.ha.StickyFeature;
- import javax.imageio.ImageIO;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.io.*;
- import java.net.Socket;
- 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="";
- 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 {
- String html = "html";
- String png = "png";
- URLlist.add(command);
- parseURL(command);
- String name=uri;
- if (isValid())
- {
- File file = new File("."+name);
- if (file.isDirectory())
- {
- URLlist.add()
- }
- 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);
- }
- else if(file.exists() && name.contains(png)) {
- istxt = false;
- FileInputStream FIS = new FileInputStream(command);
- BufferedInputStream BIS = new BufferedInputStream(FIS);
- InputStream IS = BIS;
- Image image = ImageIO.read(IS);
- // System.out.print(image);
- //BufferedImage bi = ImageIO.read(IS);
- ObjectOutputStream toBrowser = new ObjectOutputStream(socket.getOutputStream());
- toBrowser.writeBytes(response() + image);
- toBrowser.flush();
- }
- 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="NO";
- }
- }
- else
- {
- //ERROR 500?
- }
- }
- public boolean checkPermission(File file) {
- String restricted = ""; // Manually code how the file should be
- 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]);
- version=(parts[2]);
- uri=(parts[1]);
- }
- public String response()
- {
- String head=makeHead();
- returnMSG+=head+returnBody;
- System.out.println(returnMSG+"\r\n");
- return returnMSG;
- }
- public boolean isValid()
- {
- boolean num=true;
- return num;
- //DETERMINE IF COMMAND IS VALID HTTP
- //DETERMINE IF CLIENT HAS AUTHORIZATION
- //CHANGE CODE ACCORDINGLY(200,403,404 etc)
- }
- 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