Advertisement
Guest User

Untitled

a guest
May 21st, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package socket_programming;
  7.  
  8. /**
  9. *
  10. * @author sozen
  11. */
  12. import java.io.* ;
  13. import java.net.* ;
  14. import java.util.* ;
  15.  
  16. final class HttpRequest implements Runnable {
  17. final static String CRLF = "\r\n";
  18. Socket socket;
  19.  
  20. // Constructor
  21. public HttpRequest(Socket socket) throws Exception {
  22. this.socket = socket;
  23. }
  24.  
  25. // Implement the run() method of the Runnable interface.
  26. public void run() {
  27. try {
  28. processRequest();
  29. } catch (Exception e) {
  30. System.out.println(e);
  31. }
  32. }
  33.  
  34. private void processRequest() throws Exception {
  35. // Get a reference to the socket's input and output streams.
  36. InputStream is = ........................................;
  37. DataOutputStream os = ........................................;
  38.  
  39. // Set up input stream filters.
  40. BufferedReader br = ........................................;
  41.  
  42. // Get the request line of the HTTP request message.
  43. String requestLine = ........................................;
  44.  
  45. // Extract the filename from the request line.
  46. StringTokenizer tokens = new StringTokenizer(requestLine);
  47. tokens.nextToken(); // skip over the method, which should be "GET"
  48. String fileName = tokens.nextToken();
  49.  
  50. // Prepend a "." so that file request is within the current directory.
  51. fileName = "." + fileName ;
  52.  
  53. // Open the requested file.
  54. FileInputStream fis = null ;
  55. boolean fileExists = true ;
  56. try {
  57. fis = new FileInputStream(fileName);
  58. } catch (FileNotFoundException e) {
  59. fileExists = false ;
  60. }
  61.  
  62. // Debug info for private use
  63. System.out.println("Incoming!!!");
  64. System.out.println(requestLine);
  65. String headerLine = null;
  66. while ((headerLine = br.readLine()).length() != 0) {
  67. System.out.println(headerLine);
  68. }
  69.  
  70. // Construct the response message.
  71. String statusLine = null;
  72. String contentTypeLine = null;
  73. String entityBody = null;
  74. if (fileExists) {
  75. statusLine = "HTTP/1.0 200 OK" + CRLF;
  76. contentTypeLine = "Content-Type: " +
  77. contentType(fileName) + CRLF;
  78. } else {
  79. statusLine = "HTTP/1.0 " + CRLF;
  80. contentTypeLine = "Content-Type: text/html" + CRLF;
  81. entityBody = "<HTML>" +
  82. "<HEAD><TITLE>FILE COULD NOT BE FOUND</TITLE></HEAD>" +
  83. "<BODY></BODY></HTML>";
  84. }
  85. // Send the status line.
  86. os.writeBytes(statusLine);
  87.  
  88.  
  89. // Send the content type line.
  90. os.writeBytes(contentTypeLine);
  91.  
  92. // Send a blank line to indicate the end of the header lines.
  93. os.writeBytes(CRLF);
  94.  
  95. // Send the entity body.
  96. if (fileExists) {
  97. sendBytes(fis, os);
  98. fis.close();
  99. } else {
  100. os.writeBytes(entityBody) ;
  101. os.writeBytes("<html><body><h1>FILE DOES NOT EXIST </h1><p>TRY AGAIN</p></body></html>");
  102. }
  103.  
  104. // Close streams and socket.
  105. os.close();
  106. br.close();
  107. socket.close();
  108. }
  109.  
  110. private static void sendBytes(FileInputStream fis,
  111. OutputStream os) throws Exception {
  112. // Construct a 1K buffer to hold bytes on their way to the socket.
  113. byte[] buffer = new byte[1024];
  114. int bytes = 0;
  115.  
  116. // Copy requested file into the socket's output stream.
  117. while ((bytes = fis.read(buffer)) != -1) {
  118. os.write(buffer, 0, bytes);
  119. }
  120. }
  121.  
  122. private static String contentType(String fileName) {
  123. if(fileName.endsWith(".htm") || fileName.endsWith(".html")) {
  124. return "text/html";
  125. }
  126. if(fileName.endsWith(".ram") || fileName.endsWith(".ra")) {
  127. return "audio/x-pn-realaudio";
  128. }
  129. return "application/octet-stream" ;
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement