/test/src/solidstack/httpserver/HttpHeaderTokenizer.java

http://solidstack.googlecode.com/ · Java · 158 lines · 102 code · 20 blank · 36 comment · 30 complexity · b99a57e2b57aa8411c65a58511f67299 MD5 · raw file

  1. /*--
  2. * Copyright 2010 René M. de Bloois
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package solidstack.httpserver;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import solidstack.io.FatalIOException;
  20. public class HttpHeaderTokenizer
  21. {
  22. /**
  23. * The reader used to read from and push back characters.
  24. */
  25. protected InputStream in;
  26. /**
  27. * Constructs a new instance of the HttpHeaderTokenizer.
  28. *
  29. * @param in The input.
  30. */
  31. public HttpHeaderTokenizer( InputStream in )
  32. {
  33. this.in = in;
  34. }
  35. /**
  36. * Is the given character a whitespace?
  37. *
  38. * @param ch The character to check.
  39. * @return True if the characters is whitespace, false otherwise.
  40. */
  41. protected boolean isWhitespace( int ch )
  42. {
  43. switch( ch )
  44. {
  45. case ' ':
  46. case '\t':
  47. return true;
  48. }
  49. return false;
  50. }
  51. public String getLine()
  52. {
  53. try
  54. {
  55. StringBuilder result = new StringBuilder();
  56. while( true )
  57. {
  58. int ch = this.in.read();
  59. if( ch == -1 )
  60. throw new HttpException( "Unexpected end of line" );
  61. if( ch == '\r' )
  62. continue;
  63. if( ch == '\n' )
  64. return result.toString();
  65. result.append( (char)ch );
  66. }
  67. }
  68. catch( IOException e )
  69. {
  70. throw new FatalIOException( e );
  71. }
  72. }
  73. public Token getField()
  74. {
  75. try
  76. {
  77. int ch = this.in.read();
  78. // Ignore whitespace
  79. while( isWhitespace( ch ) && ch != -1 )
  80. ch = this.in.read();
  81. // Ignore carriage return
  82. if( ch == '\r' )
  83. ch = this.in.read();
  84. // Empty line means end of input for the header
  85. if( ch == '\n' )
  86. return new Token( null );
  87. StringBuilder result = new StringBuilder();
  88. while( ch != ':' && !isWhitespace( ch ) )
  89. {
  90. if( ch == -1 )
  91. throw new HttpException( "Unexpected end of statement" );
  92. if( ch == '\n' )
  93. throw new HttpException( "Unexpected end of line" );
  94. result.append( (char)ch );
  95. ch = this.in.read();
  96. }
  97. // Ignore whitespace
  98. while( isWhitespace( ch ) && ch != -1 )
  99. ch = this.in.read();
  100. if( ch != ':' )
  101. throw new HttpException( "Expecting a :" );
  102. // Return the result
  103. if( result.length() == 0 )
  104. throw new HttpException( "Empty header field" );
  105. return new Token( result.toString() );
  106. }
  107. catch( IOException e )
  108. {
  109. throw new FatalIOException( e );
  110. }
  111. }
  112. public Token getValue()
  113. {
  114. try
  115. {
  116. // Read whitespace
  117. int ch = this.in.read();
  118. while( isWhitespace( ch ) )
  119. ch = this.in.read();
  120. // Read everything until end-of-line
  121. StringBuilder result = new StringBuilder();
  122. while( true )
  123. {
  124. if( ch == -1 )
  125. throw new HttpException( "Unexpected end-of-input" );
  126. if( ch == '\n' ) // TODO Multiline header field values (space or tab)
  127. return new Token( result.toString() );
  128. if( ch != '\r' ) // ignore carriage return
  129. result.append( (char)ch );
  130. ch = this.in.read();
  131. }
  132. }
  133. catch( IOException e )
  134. {
  135. throw new FatalIOException( e );
  136. }
  137. }
  138. }