Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Trying to detect end of headers in http request

843790Apr 26 2007 — edited Apr 29 2007
I can't seem to find a way to see how the headers end, the only reason I need to do this is so that I can use the content-length to let me know when to stop reader from the socket. If there is another way to do this I would love to hear it, lol.


thanks in advance,
- thom

  public void run()
  {
    ServerSocket serverSocket = null;
    int port = 8080;
    try
    {
      serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
    }
    catch (IOException e)
    {
      e.printStackTrace();
      System.exit(1);
    }
    
    // Loop waiting for a request
    while (isActive)
    {
      Socket socket = null;
      BufferedReader input = null;
      PrintStream output = null;
      try
      {
        socket = serverSocket.accept();
        input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        output = new PrintStream(socket.getOutputStream());
        
        Properties headers = new Properties();
        
        StringBuilder inputTotal = new StringBuilder();
        String inputLine = null;

        // get our content length
        int contentLength = 0;
        int contentLengthMax = 0;
        while ((inputLine = input.readLine()) != null)
        { 
          String[] headerVar = inputLine.split(":");
          if (headerVar.length > 1)
          {
            headers.setProperty(headerVar[0].trim(), headerVar[1].trim());
          }
          System.out.println("My content-length = " + headers.getProperty("Content-Length"));

          inputTotal.append(inputLine);
          if ( headers.getProperty("Content-Length") != null )
          {
            contentLengthMax = Integer.parseInt(headers.getProperty("Content-Length"));
            System.out.println(contentLength + "/" + contentLengthMax);
            if ( contentLength >= contentLengthMax )
            {
              System.out.println("End of content, breaking loop!!");
              break;
            }
          }
        }
        
        // create Request object and parse
        /*inputLine = null;
        while( ((inputLine = input.readLine()) != null) )
        {
          if (inputTotal.length() >= contentLength)
          {
            System.out.println("breaking!");
            break;
          }
          
          inputTotal.append(inputLine);
        }*/
        
        // create Response object
        //output.print( "INPUT = " + inputTotal );
        System.out.println( "INPUT = " + inputTotal );
        
        // Close the socket
        socket.close();
      }
      catch (Exception e)
      {
        e.printStackTrace();
        continue;
      }
    }
  }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 27 2007
Added on Apr 26 2007
6 comments
1,723 views