Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * HttpRequest - HTTP request container and parser
- *
- * $Id: HttpRequest.java,v 1.2 2003/11/26 18:11:53 kangasha Exp $
- *
- */
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class HttpRequest {
- /** Help variables */
- final static String CRLF = "\r\n";
- final static int HTTP_PORT = 80;
- /** Store the request parameters */
- String method;
- String URI;
- String version;
- String headers = "";
- /** Server and port */
- private String host;
- private int port;
- //support POST
- boolean isPost = false;
- int length = -1;
- final static int MAX_OBJECT_SIZE = 1000000;
- /* Body of reply */
- byte[] body = new byte[MAX_OBJECT_SIZE];
- int bytesRead = 0;
- String contentString;
- /** Create HttpRequest by reading it from the client socket */
- public HttpRequest(DataInputStream from) {
- String firstLine = "";
- try {
- firstLine = from.readLine();
- } catch (IOException e) {
- System.out.println("Error reading request line: " + e);
- }
- String[] tmp = firstLine.split(" ");
- //method = /* Fill in */;
- //URI = /* Fill in */;
- //version = /* Fill in */;
- method = tmp[0];
- URI = tmp[1];
- version = tmp[2];
- System.out.println("URI is: " + URI);
- if (!method.equals("GET")) {
- isPost = true;
- System.out.println("Error: Method not GET");
- }
- try {
- System.out.println("isPost: " + isPost);
- String line = from.readLine();
- while (line.length() != 0) {
- headers += line + CRLF;
- /* We need to find host header to know which server to
- * contact in case the request URI is not complete. */
- if (line.startsWith("Host:")) {
- tmp = line.split(" ");
- if (tmp[1].indexOf(':') > 0) {
- String[] tmp2 = tmp[1].split(":");
- host = tmp2[0];
- port = Integer.parseInt(tmp2[1]);
- } else {
- host = tmp[1];
- port = HTTP_PORT;
- }
- }
- //this code gets centent-length length
- if (line.startsWith("Content-Length") || line.startsWith("Content-length")) {
- System.out.println("Get content-length");
- tmp = line.split(" ");
- contentString = line;
- length = Integer.parseInt(tmp[1]);
- break;
- }
- line = from.readLine();
- }
- }
- catch (IOException e) {
- System.out.println("Error reading from socket: " + e);
- return;
- }
- System.out.println("Host to contact is: " + host + " at port " + port);
- if(isPost)
- {
- try
- {
- while(bytesRead < length)
- {
- int i;
- for(i=0; i<=length+1;i++)
- {
- body[i] = from.readByte();
- }
- bytesRead += i;
- }
- }
- catch(Exception e)
- {
- System.out.println("Error reading POST body");
- }
- }
- }
- /** Return host for which this request is intended */
- public String getHost() {
- return host;
- }
- /** Return port for server */
- public int getPort() {
- return port;
- }
- /**
- * Convert request into a string for easy re-sending.
- */
- public String toString() {
- String req = "";
- req = method + " " + URI + " " + version + CRLF;
- req += headers;
- /* This proxy does not support persistent connections */
- req += "Connection: close" + CRLF;
- req += CRLF;
- if(isPost)
- {
- System.out.println("Before"+req);
- req = req.replace(contentString,"Content-Length: " + bytesRead);
- System.out.println("After"+req);
- }
- return req;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement