Results 1 to 10 of 10

Thread: Can I Get Some JAVA Help? PLEASE!

  1. #1
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default Can I Get Some JAVA Help? PLEASE!

    Ok here is my problem. Im writing a http proxy that will serve http request. So here is a little background.

    The http protocol works on a request response basis clients and servers exchange messages. messages are terminated by the sequence "\r\n\r\n"
    some implemenations use the sequence "\n\n" even though the rfc for the protocol explicitly stated the former sequence servers are asked to be tolerant.

    The method posted below attempts to read a request from a client. This method works great especially when a client is pipelining request. In such a case its picks up 20 or more request and serve them simultaneously. processing one message at a time is slower. No the problem im having is when a client post data.. That includes a request follwed by the content the client wants to post, The contents gets mixed into the message because this function really checks the end of the stream for a message terminator. which is somewhere in the middle..

    Code:
    	/**
         * The method is called on the Selector thread for the object
         * handling the channel.
         * An asynchronous call from another thread wont cause the selector 
         * to throw an exception. Rather the wakeup method will be called
         * interrupting the selector.
         */
        final public void readMessage() {
            
            try {
                            
                int r = connection.read(buffer);
                
                if(r > 0) {
                    buffer.flip();
                    
                    /*decode the buffer, append it to the request string
                     *determine if client has made a complete request. 
                     *if so disable read interest in this channel and
                     *pass the request unto  a handler
                     */                
                    CoderResult cr = asciiDecoder.decode(buffer, charBuffer, true);                
                    if(cr.isError() || cr.isOverflow()) {
                        cr.throwException();
                    }
                    charBuffer.flip();
                    
                    request.append(charBuffer);
                    
                    int l = request.length();
                    if(request.substring(l-4, l).endsWith(MESSAGETERMINATOR)) {
                        selector.removeChannelInterest(connection, OP_READ);
                        threadPool.execute(this);
                    }
                    else if(request.length() > MAX_MESSAGE_LENGTH) {
                        logger.log("message length is too long! recieved from channel "+connection);
                        close();
                    }
                    buffer.clear();
                    charBuffer.clear();
                }
                else if(r == -1) { close(); }
            }
            catch(Exception ex) {            
                close();
                logger.log(ex);
            }
            
        }
    I need help coming up with and algorithm to avoid the post issue and still be able to process more that one request at a time.. If you don't understand what exactly im trying to do please post your questions and I will help to clarify. Thanks.

  2. #2
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Code:
    request.append(charBuffer);
    Why was this done?

    Does message contain request followed by content, is that stored in buffer? And is buffer the message sent from the client?

    The contents gets mixed into the message
    Do you mean that the contents is followed by the request? Is that's how it is mixed? But you just want contents alone? Pardon my ignorance.

    I follow that all messages end with "\n\n" or "\r\n\r\n" that are retrieved from the clients. That is a given input, right, in buffer? So this function messes up the input so that the terminator is somewhere in the middle? But I do not see how except for the line up top.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  3. #3
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    Thanks for your response crosswire I hope you can help me work this out.

    Code:
    request.append(charBuffer);
    Why was this done?
    This appends a decoded message to a partially build string. The server is non-blocking meaning the programs never stops and wait for a client to complete a request. If the request was partial it appends it to a string builder and goes on about its business. When the client send another request its appends it again and look at the contents to see if a request was completed.
    Does message contain request followed by content
    NOt all messages.. GET message from a client never sends a content/body/payload POST messages sends the body of data its want to POST to the server directly behind the message.. The body is getting into the buffer. I look at the end of the buffer for the end of a message.. I may never find the end since I was actually looking at the contents and not the message the end of the message is somewhere in the buffer but this method wont find it.

    is that stored in buffer? And is buffer the message sent from the client?
    right!

    I did it this was because if the client is trying to pipeline (send many request simultaneously)
    10 request I will pick up all 10 request in one swoop rather than slowly scanning though the buffer looking for the end of one message. Its works when all the messages are GET messages. It only fails on POST because a post is followed by the payload.


    I follow that all messages end with "\n\n" or "\r\n\r\n" that are retrieved from
    the clients. That is a given input, right, in buffer? So this function messes
    up the input so that the terminator is somewhere in the middle?
    But I do not see how except for the line up top.
    not exactly it does not change anything recieved from the client.. Its try to swallow many messages simultaneously which is the problem with POST request.. Only one message should be read in this situation.. IF I read one message at a time it will slow down the message process and wont take advantage of clients pipelining request. I want to slove the problem and still be able to handle pipelined request.
    Last edited by leoandru; Aug 29, 2005 at 06:04 PM.

  4. #4
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Thanks, I'll check it out
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  5. #5
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    Here is some good info on pipelining it may help to understand my problem.

    http://www.mozilla.org/projects/netl...ining-faq.html

  6. #6
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    I had to check my html book on the difference between post and get methods of sending data (w.r.t. sending form data to the server which then returns a result, I applied that to sending requests for any web page), still the book did not go into detail on the format of these requests. Like what bytes start them and what bytes finish them, or how long they are. So how can you distinguish between a post or get? Especially from just a part of it? I assume that there is a 'partial request' that is received at the proxy only for post. I am getting at the logic of filtering post requests to be handled differently, if they can be filtered.

    That includes a request followed by the content the client wants to post, The contents gets mixed into the message because this function really checks the end of the stream for a message terminator. which is somewhere in the middle.
    Now I finally get this part. Just learnt something new. Thanks for the example.

    Do you require that the post requests be pipelined too?
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  7. #7
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    Quote Originally Posted by crosswire
    So how can you distinguish between a post or get? Especially from just a part of it? I assume that there is a 'partial request' that is received at the proxy only for post. I am getting at the logic of filtering post requests to be handled differently, if they can be filtered.
    A Http Get Request looks like:
    where CRLF = \r\n

    Code:
    GET / Http1.1 CRLF Host: Techjamaica.com CRLFCRLF
    while a post message may be like

    Code:
    POST / Http1.1 CRLF Host: Techjamaica.com CRLF Content-Length: 11 CRLFCRLF "Hello World"
    these are very simple example of GET and POST request.
    So you can see that a bunch of get request will always have a CRLFCRLF at the end.. while for the post my method will be looking at the string "orld"
    and think that the client didn't complete a request and ignore him, which in fact he did complete one.

    Do you require that the post requests be pipelined too?
    No POST request cannot be pipelined.. This may help: http://www.mozilla.org/projects/netl...ining-faq.html

  8. #8
    Join Date
    Jul 2004
    Posts
    264
    Rep Power
    0

    Default

    So what are you trying to do ... combine multiple GET requests with an occasional POST request in the stream

  9. #9
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    Quote Originally Posted by aonekilla
    So what are you trying to do ... combine multiple GET requests with an occasional POST request in the stream
    nope.. I trying to read multiple GET request in one loop. GET request are not mixed with POST request. POST is handled differently. I solved the problem just now. I'm going to post in the the morning.

  10. #10
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    I was working on this but it seemed like it would take me too long, and it hurt my head too. *I need to practice to get in programming shape*. Instead of being of help, I ended up learning stuff on the internet. A dominant domain, I may add. I am partly interested in intenet applications for the experience and programming fun, and to help out when I can.

    Though, I greatly dig simulations. Things like creating a car and using real sound effects like echoing the engine in a tunnel. Then trying to figure out how to get the sound as real as possible by using a limited resource. As much as I would want to simulate every sound wave travelling through the air and reflecting them by the correct angle caused by the walls of the tunnel, and then pick up the amplitude and frequency at the driver's location... I cant do that much simulation. Ah well... its not a living.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •