Advertisement
kiah

Untitled

Feb 25th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.58 KB | None | 0 0
  1. import com.sun.xml.internal.ws.api.ha.StickyFeature;
  2. import org.omg.CORBA.NameValuePair;
  3.  
  4. import javax.imageio.ImageIO;
  5. import java.awt.*;
  6. import java.awt.image.BufferedImage;
  7. import java.io.*;
  8. import java.net.Socket;
  9. import java.net.URL;
  10. import java.util.ArrayList;
  11. import java.util.stream.Collectors;
  12.  
  13. public class httpRead {
  14.  
  15.  
  16. private String command;
  17. private int code = 200;
  18. private String phrase = "OK";
  19. private String returnBody = "";
  20. private String returnMSG = "";
  21. private String method = "GET";
  22. private String version = "";
  23. private String uri = "";
  24. private boolean filePermission = true;
  25. private ArrayList<String> URLlist = new ArrayList<String>();
  26.  
  27.  
  28. public httpRead(String command) {
  29. this.command = command;
  30. }
  31.  
  32. public String returnHeader = "";
  33. public boolean istxt = true;
  34.  
  35.  
  36. public void main(Socket socket) throws IOException {
  37.  
  38. URLlist.add(command);
  39. parseURL(command);
  40. if (isValid()) {
  41. if (method.equals("GET"))
  42. try {
  43. {
  44. String name = uri;
  45. File file = new File("." + name);
  46. GET(file, name);
  47. }
  48. } catch (IOException e) {
  49. System.out.println("ERROR" + e);
  50. }
  51. else if (method.equals("POST")) {
  52. POST(socket);
  53. } else if (method.equals("PUT")) {
  54. PUT(socket);
  55. }
  56. } else {
  57.  
  58. //ERROR 500
  59. File error = new File("." + "/error500.html");
  60. FileReader FR = new FileReader(error);
  61. BufferedReader er = new BufferedReader(FR);
  62. String errorContent = er.lines().collect(Collectors.joining());
  63. returnBody = errorContent;
  64. System.out.println("should print file content" + errorContent);
  65. code = 500;
  66. phrase = "SERVER_ERROR";
  67. }
  68.  
  69. System.out.println(command);
  70. }
  71.  
  72. public void GET(File file, String name) throws IOException {
  73.  
  74. String html = "html";
  75. String png = "png";
  76. if (isValid()) {
  77.  
  78. if (file.isDirectory()) {
  79. StringBuilder list = new StringBuilder("<html><head><title>Contents of Directory ");
  80. list.append(uri);
  81. list.append("</title></head><body><h1>Contents of Directory ");
  82. list.append(uri);
  83. list.append("</h1><hr><pre>");
  84.  
  85. File[] files = file.listFiles();
  86. for (File subfile : files) {
  87. list.append(" <a href=\"" + subfile.getPath() + "\">" + subfile.getPath() + "</a>\n");
  88. }
  89. list.append("<hr></pre></body></html>");
  90. returnBody = list.toString();
  91. //DEALS WITH HTTP FILE REQUESTS
  92. } else if (file.exists() && name.contains(html)) {
  93. //CHECKS PERMISSION AND PROCESSES ERROR 403
  94. //if (!checkPermission(file)) {
  95. // file = new File("." + "/error403.html");
  96. //}
  97. FileReader FR = new FileReader(file);
  98. BufferedReader reader = new BufferedReader(FR);
  99. String fileContent = reader.lines().collect(Collectors.joining());
  100. returnBody = fileContent;
  101. System.out.println("should print file content" + fileContent);
  102. //DEALS WITH PNG FILE REQUESTS
  103. } else if (file.exists() && name.contains(png)) {
  104. istxt = false;
  105. int size=(int)file.length();
  106. byte[] data=new byte[size];
  107. DataInputStream in= new DataInputStream( new FileInputStream(file));
  108. in.readFully(data);
  109. in.close();
  110. //ERROR 404 NOT FOUND
  111. } else if (!file.exists()) {
  112. File error = new File("." + "/error404.html");
  113. FileReader FR = new FileReader(error);
  114. BufferedReader er = new BufferedReader(FR);
  115. String errorContent = er.lines().collect(Collectors.joining());
  116. returnBody = errorContent;
  117. System.out.println("should print file content" + errorContent);
  118. code = 404;
  119. phrase = "NOT_FOUND";
  120. }
  121. } else {
  122. File error = new File("." + "/error500.html");
  123. FileReader FR = new FileReader(error);
  124. BufferedReader er = new BufferedReader(FR);
  125. String errorContent = er.lines().collect(Collectors.joining());
  126. returnBody = errorContent;
  127. System.out.println("should print file content" + errorContent);
  128. code = 500;
  129. phrase = "SERVER_ERROR";
  130. }
  131.  
  132. }
  133.  
  134.  
  135. public void POST(Socket socket) throws IOException {
  136. BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  137. BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
  138. // read request
  139. String line;
  140. line = in.readLine();
  141. StringBuilder raw = new StringBuilder();
  142. raw.append("" + line);
  143. boolean isPost = line.startsWith("POST");
  144. int contentLength = 0;
  145. while (!(line = in.readLine()).equals("")) {
  146. raw.append('\n' + line);
  147. if (isPost) {
  148. final String contentHeader = "Content-Length: ";
  149. if (line.startsWith(contentHeader)) {
  150. contentLength = Integer.parseInt(line.substring(contentHeader.length()));
  151. }
  152. }
  153. StringBuilder body = new StringBuilder();
  154. if (isPost) {
  155. int c = 0;
  156. for (int i = 0; i < contentLength; i++) {
  157. c = in.read();
  158. body.append((char) c);
  159. }
  160. }
  161. raw.append(body.toString());
  162. // publishProgress(raw.toString());
  163. // send response
  164. out.write("HTTP/1.1 200 OK\r\n");
  165. out.write("Content-Type: text/html\r\n");
  166. out.write("\r\n");
  167. // out.write(new Date().toString());
  168. if (isPost) {
  169. out.write("<br><u>" + body.toString() + "</u>");
  170. } else {
  171. out.write("<form method='POST'>");
  172. out.write("<input name='name' type='text'/>");
  173. out.write("<input type='submit'/>");
  174. out.write("</form>");
  175. }
  176. }
  177. }
  178.  
  179.  
  180.  
  181.  
  182. public void PUT(Socket socket)
  183. {
  184.  
  185.  
  186. }
  187. public boolean checkPermission(File file) {
  188. String restricted = ""; // Manually code how the file should be
  189. String name= "/permission.html";
  190. File check = new File("."+name);
  191. try {
  192. GET(check, name);
  193. }
  194. catch(IOException e)
  195. {System.out.println("Error"+e);}
  196.  
  197.  
  198.  
  199. try {
  200. if (!filePermission)
  201. {
  202.  
  203.  
  204. }
  205. else if (file.equals(restricted)) {
  206. System.out.println("No permission: " + file);
  207. filePermission = false;
  208. }
  209.  
  210. } catch (Exception e) {
  211. e.getMessage();
  212.  
  213. }
  214. return filePermission;
  215.  
  216. }
  217.  
  218. public void parseURL(String command)
  219. {
  220.  
  221.  
  222. String[] parts=command.split("\\s+");
  223.  
  224. method=(parts[0]);
  225. if(parts[2]!=null)
  226. {
  227. version = (parts[2]);
  228. }
  229. else{version="HTTP/1.1";}
  230. uri=(parts[1]);
  231. }
  232.  
  233.  
  234. public String response()
  235. {
  236. String head=makeHead();
  237. returnMSG+=head+returnBody;
  238. System.out.println(returnMSG+"\r\n");
  239. return returnMSG;
  240.  
  241. }
  242.  
  243. public boolean isValid()
  244. { boolean bool= true;
  245. //DETERMINE IF COMMAND IS VALID HTTP
  246. //DETERMINE IF CLIENT HAS AUTHORIZATION
  247. //CHANGE CODE ACCORDINGLY(200,403,404 etc)
  248. return bool;
  249.  
  250. }
  251.  
  252. private String makeHead()
  253. {
  254. returnHeader+=version+" "+ code + " " + phrase;
  255. String server="Server: My Computer";
  256. String date="Date: Today";
  257. String length="Content Length: ";
  258. int ln=returnBody.length();
  259. String len=Integer.toString(ln);
  260. length+=len;
  261. String type="Content Type: ";
  262. if (!istxt)
  263. {type+= ".png/html";}
  264. else{type+=".txt/html";}
  265. String s="\r\n";
  266. returnHeader+= s + date+ s + server+ s + length + s + type +s+s;
  267. return returnHeader;
  268.  
  269. }
  270.  
  271.  
  272.  
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement