Advertisement
kiah

Untitled

Feb 24th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. import com.sun.xml.internal.ws.api.ha.StickyFeature;
  2.  
  3. import javax.imageio.ImageIO;
  4. import java.awt.*;
  5. import java.awt.image.BufferedImage;
  6. import java.io.*;
  7. import java.net.Socket;
  8. import java.util.ArrayList;
  9. import java.util.stream.Collectors;
  10.  
  11. public class httpRead {
  12.  
  13.  
  14. private String command;
  15. private int code=200;
  16. private String phrase="OK";
  17. private String returnBody="";
  18. private String returnMSG="";
  19. private String method="";
  20. private String version="";
  21. private String uri="";
  22. private boolean filePermission=true;
  23. private ArrayList<String> URLlist = new ArrayList<String>();
  24.  
  25.  
  26. public httpRead(String command)
  27. {
  28. this.command=command;
  29. }
  30. public String returnHeader="";
  31. public boolean istxt=true;
  32.  
  33.  
  34. public void main(Socket socket) throws IOException {
  35. String html = "html";
  36. String png = "png";
  37. URLlist.add(command);
  38. parseURL(command);
  39. String name=uri;
  40.  
  41.  
  42. if (isValid())
  43. {
  44.  
  45. File file = new File("."+name);
  46. if (file.isDirectory())
  47. {
  48. URLlist.add()
  49.  
  50. }
  51.  
  52.  
  53. if(file.exists() && name.contains(html))
  54. {
  55. //CHECKS PERMISSION AND PROCESSES ERROR 403
  56.  
  57. if(!checkPermission(file))
  58. {
  59. file= new File("."+"/error403.html");
  60. }
  61. FileReader FR = new FileReader(file);
  62. BufferedReader reader = new BufferedReader(FR);
  63. String fileContent = reader.lines().collect(Collectors.joining());
  64. returnBody = fileContent;
  65. System.out.println("should print file content" + fileContent);
  66.  
  67. }
  68.  
  69.  
  70. else if(file.exists() && name.contains(png)) {
  71. istxt = false;
  72. FileInputStream FIS = new FileInputStream(command);
  73. BufferedInputStream BIS = new BufferedInputStream(FIS);
  74. InputStream IS = BIS;
  75. Image image = ImageIO.read(IS);
  76. // System.out.print(image);
  77. //BufferedImage bi = ImageIO.read(IS);
  78. ObjectOutputStream toBrowser = new ObjectOutputStream(socket.getOutputStream());
  79. toBrowser.writeBytes(response() + image);
  80. toBrowser.flush();
  81. }
  82.  
  83. else if (!file.exists())
  84. {
  85. File error = new File("."+"/error404.html");
  86. FileReader FR = new FileReader(error);
  87. BufferedReader er = new BufferedReader(FR);
  88. String errorContent = er.lines().collect(Collectors.joining());
  89. returnBody = errorContent;
  90. System.out.println("should print file content"+ errorContent);
  91. code=404;
  92. phrase="NO";
  93. }
  94. }
  95. else
  96. {
  97. //ERROR 500?
  98. }
  99. }
  100.  
  101. public boolean checkPermission(File file) {
  102. String restricted = ""; // Manually code how the file should be
  103.  
  104. try {
  105. if (!filePermission)
  106. {
  107.  
  108. }
  109. else if (file.equals(restricted)) {
  110. System.out.println("No permission: " + file);
  111. filePermission = false;
  112. }
  113.  
  114. } catch (Exception e) {
  115. e.getMessage();
  116.  
  117. }
  118. return filePermission;
  119.  
  120. }
  121.  
  122. public void parseURL(String command)
  123. {
  124.  
  125.  
  126. String[] parts=command.split("\\s+");
  127.  
  128. method=(parts[0]);
  129. version=(parts[2]);
  130. uri=(parts[1]);
  131. }
  132.  
  133.  
  134. public String response()
  135. {
  136. String head=makeHead();
  137. returnMSG+=head+returnBody;
  138. System.out.println(returnMSG+"\r\n");
  139. return returnMSG;
  140.  
  141. }
  142.  
  143. public boolean isValid()
  144. {
  145. boolean num=true;
  146.  
  147.  
  148. return num;
  149. //DETERMINE IF COMMAND IS VALID HTTP
  150. //DETERMINE IF CLIENT HAS AUTHORIZATION
  151. //CHANGE CODE ACCORDINGLY(200,403,404 etc)
  152.  
  153.  
  154. }
  155.  
  156. private String makeHead()
  157. {
  158. returnHeader+=version+" "+ code + " " + phrase;
  159. String server="Server: My Computer";
  160. String date="Date: Today";
  161. String length="Content Length: ";
  162. int ln=returnBody.length();
  163. String len=Integer.toString(ln);
  164. length+=len;
  165. String type="Content Type: ";
  166. if (!istxt)
  167. {type+= ".png/html";}
  168. else{type+=".txt/html";}
  169. String s="\r\n";
  170. returnHeader+= s + date+ s + server+ s + length + s + type +s+s;
  171. return returnHeader;
  172.  
  173. }
  174.  
  175.  
  176.  
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement