jjenkov / java-nio-server

A Java NIO Server using non-blocking IO all the way through.
Apache License 2.0
926 stars 461 forks source link

Some bug in your code #10

Open yudnkuku opened 5 years ago

yudnkuku commented 5 years ago
stepfortune commented 4 years ago
  • In your HttpUtil.parseHttpRequest() method: the variable bodyEndIndex should be bodyStartIndex + httpHeaders.contentLength + 1 rather than bodyStartIndex + httpHeaders.contentLength
  • In your Message.writePartialMessageToMessage method: the variable startIndexOfPartialMessage should just be endIndex + 1 rather than message.offset + endIndex. the variable lengthOfPartialMessage should be (message.offset + message.length) - endIndex + 1 rather than (message.offset + message.length) - endIndex
  • I got a NPE when invoking the HttpMessageReader.read().The code snippet is shown below:
Message message = this.messageBuffer.getMessage;    //the return message might be null
message.metadata = new HttpHeaders();    //the NPE occurs here

To solve the problem,I add some code in your QueueIntFlip.take() method:

        if(!flipped){
            //add this if 
            if(readPos == capacity) {
                readPos = 0;
            }
            if(readPos < writePos){
                return elements[readPos++];
            } else {
                return -1;
            }
        }

I think the variable startIndexOfPartialMessage should be endIndex, not bodyStartIndex + httpHeaders.contentLength or endIndex + 1, since the endIndex does point to the next byte of a http message body end(the implementation can be found in the HttpUtil.parseHttpRequest method), Additionally, I dont think the code


            if(!flipped){
              //add this if 
              if(readPos == capacity) {
                  readPos = 0;
              }
              if(readPos < writePos){
                  return elements[readPos++];
              } else {
                  return -1;
              }
          }
``` that you mentioned is right, because if you do that, in the case  ` writePos == capacity readPos == capacity flipped == false` which means the all the MessageBuffer blocks are free, then you call the `take()` function again, the `readPos` will be 0, and the writePos and flipped wont be changed, that cause a repeated reading.