Menu

Need help with my own HTTP server and httpuni

2002-08-28
2002-08-29
  • Erik Ramfelt

    Erik Ramfelt - 2002-08-28

    Ive developed my own HTTP server so I can run the application both as a servlet and as a standalone server. But Im having problem testing it with httpunit, the servlet testing works GREAT (thanks btw). Unfortunately this thing only happens half the time and works on the other half, makes it hard to debug.

    Its when I do a POST to the standalone server the server only receives half of the data of what it should, which leaves my server waiting for the rest. Is there any important stuff that the server needs to do for a POST request? Remember that Im working with simple socket connections.

    My server logic looks like this:
    getConnection() (socket)
    readHeaders() (read text until 2 * "\r\n")
    readDATA (from InputStream until received size == content-length)

    The httpunit testing is just using a simple PostMethodRequest so I dont think its wrong in httpunit, I dont have any other place to post this cry for help:

    Code for receiving headers:
    try
            {
                BufferedReader buffInput = new BufferedReader( new InputStreamReader( originalInputStream ));
                String readLine = buffInput.readLine();

                requestMethod = readLine.substring(0, readLine.indexOf(' '));
                readLine = readLine.substring(readLine.indexOf(' ') + 1);

                if (readLine.indexOf(' ') != -1)
                {
                    requestPath = readLine.substring(0, readLine.indexOf(' '));
                    requestProtocol = readLine.substring(readLine.indexOf(' ') + 1);
                } else
                {
                    requestPath = readLine.substring(0, readLine.indexOf(' '));
                }

                readLine = buffInput.readLine();
                while( (readLine != null) && (!readLine.equals("")) )
                {
                    String key = readLine.substring(0, readLine.indexOf(':'));
                    String value = readLine.substring(readLine.indexOf(':') + 2);
                    requestHeaders.put(key, value );

                    readLine = buffInput.readLine();
                }
            }
            catch (IndexOutOfBoundsException e )
            {
                //fail( "Response from server is not a correct HTTP header");
            }

    Code for receiving POST data (this is the code that hangs) this is done directly after reading the headers:
    {
            BufferedInputStream bis = new BufferedInputStream( request.getInputStream();, this.BUFFER_SIZE);
            int len = 0;
            int totalLength = 0;
            byte[] buffer = new byte[this.BUFFER_SIZE];
            boolean finnish = false;
            while ( !finnish ){
                len = s.read();

                if ( len != -1 ){
                    image.getImageData()[totalLength] = (byte)len;
                    totalLength++;
                    if ( totalLength == image.getImageData().length ){
                        finnish = true;
                    }
                }
                else {
                    finnish = true;
                }
            }
            log.fine("Finished receiving.  recieved bytes: "+totalLength);
    }

     
    • Russell Gold

      Russell Gold - 2002-08-28

      Why don't you take a look at PseudoServer, now in its own package in the pre-release builds? It implements a very stripped-down HTTP server, which I used to test HTTPUnit. You could use it directly or as a guideline for writing your own server.

       
      • Erik Ramfelt

        Erik Ramfelt - 2002-08-29

        Thanks for the help, i used the code from the pseudoserver and it worked alot much better. It seems that the problem was my old headerparsing.

        Thanks

         

Log in to post a comment.

Auth0 Logo