Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. /**
  2. * HttpRequest - HTTP request container and parser
  3. *
  4. * $Id: HttpRequest.java,v 1.2 2003/11/26 18:11:53 kangasha Exp $
  5. *
  6. */
  7.  
  8. import java.io.*;
  9. import java.net.*;
  10. import java.util.*;
  11.  
  12. public class HttpRequest {
  13. /** Help variables */
  14. final static String CRLF = "\r\n";
  15. final static int HTTP_PORT = 80;
  16. /** Store the request parameters */
  17. String method;
  18. String URI;
  19. String version;
  20. String headers = "";
  21. /** Server and port */
  22. private String host;
  23. private int port;
  24.  
  25. //support POST
  26. boolean isPost = false;
  27. int length = -1;
  28. final static int MAX_OBJECT_SIZE = 1000000;
  29. /* Body of reply */
  30. byte[] body = new byte[MAX_OBJECT_SIZE];
  31. int bytesRead = 0;
  32. String contentString;
  33.  
  34. /** Create HttpRequest by reading it from the client socket */
  35. public HttpRequest(DataInputStream from) {
  36. String firstLine = "";
  37. try {
  38. firstLine = from.readLine();
  39. } catch (IOException e) {
  40. System.out.println("Error reading request line: " + e);
  41. }
  42.  
  43. String[] tmp = firstLine.split(" ");
  44. //method = /* Fill in */;
  45. //URI = /* Fill in */;
  46. //version = /* Fill in */;
  47. method = tmp[0];
  48. URI = tmp[1];
  49. version = tmp[2];
  50.  
  51. System.out.println("URI is: " + URI);
  52.  
  53. if (!method.equals("GET")) {
  54. isPost = true;
  55. System.out.println("Error: Method not GET");
  56. }
  57. try {
  58.  
  59. System.out.println("isPost: " + isPost);
  60. String line = from.readLine();
  61. while (line.length() != 0) {
  62. headers += line + CRLF;
  63. /* We need to find host header to know which server to
  64. * contact in case the request URI is not complete. */
  65. if (line.startsWith("Host:")) {
  66. tmp = line.split(" ");
  67. if (tmp[1].indexOf(':') > 0) {
  68. String[] tmp2 = tmp[1].split(":");
  69. host = tmp2[0];
  70. port = Integer.parseInt(tmp2[1]);
  71. } else {
  72. host = tmp[1];
  73. port = HTTP_PORT;
  74. }
  75. }
  76.  
  77. //this code gets centent-length length
  78. if (line.startsWith("Content-Length") || line.startsWith("Content-length")) {
  79. System.out.println("Get content-length");
  80. tmp = line.split(" ");
  81. contentString = line;
  82. length = Integer.parseInt(tmp[1]);
  83. break;
  84. }
  85. line = from.readLine();
  86. }
  87. }
  88.  
  89.  
  90. catch (IOException e) {
  91. System.out.println("Error reading from socket: " + e);
  92. return;
  93. }
  94. System.out.println("Host to contact is: " + host + " at port " + port);
  95.  
  96. if(isPost)
  97. {
  98. try
  99. {
  100. while(bytesRead < length)
  101. {
  102. int i;
  103. for(i=0; i<=length+1;i++)
  104. {
  105. body[i] = from.readByte();
  106. }
  107. bytesRead += i;
  108. }
  109. }
  110. catch(Exception e)
  111. {
  112. System.out.println("Error reading POST body");
  113. }
  114. }
  115. }
  116.  
  117.  
  118. /** Return host for which this request is intended */
  119. public String getHost() {
  120. return host;
  121. }
  122.  
  123. /** Return port for server */
  124. public int getPort() {
  125. return port;
  126. }
  127.  
  128. /**
  129. * Convert request into a string for easy re-sending.
  130. */
  131. public String toString() {
  132. String req = "";
  133.  
  134. req = method + " " + URI + " " + version + CRLF;
  135. req += headers;
  136. /* This proxy does not support persistent connections */
  137. req += "Connection: close" + CRLF;
  138. req += CRLF;
  139.  
  140. if(isPost)
  141. {
  142. System.out.println("Before"+req);
  143. req = req.replace(contentString,"Content-Length: " + bytesRead);
  144. System.out.println("After"+req);
  145. }
  146. return req;
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement