2009년 4월 30일 목요일

HTTP Body를 파싱하면서 처음 byte가 CRLF를 포함하고 있을 때의 처리

HTTP Server로 Client와 연동할 때 간혹 구현에 따라서 HTTP Header와 body 사이에 CRLF가 두 개만 있어야 정상인데, 비정상적으로 CRLF가 두 개 이상일 경우가 발생한다.
이럴 때는 다음과 같은 코드를 넣어 두어 CR, LF에 대한 byte는 skip하고 처리해 주면 된다.

  for ( int k=position; k < this.httpBodyBuffer.length; k++ ) {
   StringBuffer startStringTemp = new StringBuffer();
   startTemp = Integer.toHexString(httpBodyBuffer[k] & 0x000000FF);
   startStringTemp.append((startTemp.length() == 1 ? "0"+startTemp : startTemp));
   
   if ( ("0d".equals(""+startStringTemp)) || ("0a".equals(""+startStringTemp)) ) {
    System.out.println("k = " + k);
   } else {
    position = k;
    System.out.println("HTTP BODY Start position : " + position);
    break;
   }
  }

댓글 없음: