comparison src/org/eclipse/jetty/http/HttpParser.java @ 1046:a8c92b0a08ed

add JBuffer
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 07 Nov 2016 22:39:39 -0700
parents dd71a59fcf72
children 2b769da7f67d
comparison
equal deleted inserted replaced
1045:48506d03e230 1046:a8c92b0a08ed
19 package org.eclipse.jetty.http; 19 package org.eclipse.jetty.http;
20 20
21 import java.io.IOException; 21 import java.io.IOException;
22 22
23 import org.eclipse.jetty.io.Buffer; 23 import org.eclipse.jetty.io.Buffer;
24 import org.eclipse.jetty.io.ByteArrayBuffer;
25 import org.eclipse.jetty.io.BufferUtil; 24 import org.eclipse.jetty.io.BufferUtil;
26 import org.eclipse.jetty.io.EndPoint; 25 import org.eclipse.jetty.io.EndPoint;
27 import org.eclipse.jetty.io.EofException; 26 import org.eclipse.jetty.io.EofException;
28 import org.eclipse.jetty.util.StringUtil; 27 import org.eclipse.jetty.util.StringUtil;
29 import org.slf4j.Logger; 28 import org.slf4j.Logger;
66 private String _tok1 = ""; // Saved token: header value, request URI or response code 65 private String _tok1 = ""; // Saved token: header value, request URI or response code
67 private String _multiLineValue; 66 private String _multiLineValue;
68 private int _responseStatus; // If >0 then we are parsing a response 67 private int _responseStatus; // If >0 then we are parsing a response
69 private boolean _persistent; 68 private boolean _persistent;
70 69
71 private Buffer _contentView = new ByteArrayBuffer(0); // View of the content in the buffer for {@link Input} 70 private Buffer _contentView = BufferUtil.EMPTY_BUFFER; // View of the content in the buffer for {@link Input}
72 private int _state = STATE_START; 71 private int _state = STATE_START;
73 private byte _eol; 72 private byte _eol;
74 private int _length; 73 private int _length;
75 private long _contentLength; 74 private long _contentLength;
76 private long _contentPosition; 75 private long _contentPosition;
187 */ 186 */
188 private int parseNext() throws IOException 187 private int parseNext() throws IOException
189 { 188 {
190 try 189 try
191 { 190 {
192 int progress=0; 191 int progress = 0;
193 192
194 if (_state == STATE_END) 193 if (_state == STATE_END) {
195 return 0; 194 return 0;
195 }
196 196
197 if (_buffer==null) 197 if (_buffer==null)
198 _buffer = _header; 198 _buffer = _header;
199 199
200 200
201 if (_state == STATE_CONTENT && _contentPosition == _contentLength) 201 if (_state == STATE_CONTENT && _contentPosition == _contentLength)
202 { 202 {
203 _state=STATE_END; 203 _state = STATE_END;
204 _handler.messageComplete(_contentPosition); 204 _handler.messageComplete(_contentPosition);
205 return 1; 205 return 1;
206 } 206 }
207 207
208 int length = _buffer.remaining(); 208 int length = _buffer.remaining();
209 209
210 // Fill buffer if we can 210 // Fill buffer if we can
211 if (length == 0) 211 if (length == 0)
212 { 212 {
213 int filled=-1; 213 int filled = -1;
214 IOException ex=null; 214 IOException ex = null;
215 try 215 try
216 { 216 {
217 filled=fill(); 217 filled = fill();
218 LOG.debug("filled {}/{}",filled,_buffer.remaining()); 218 LOG.debug("filled {}/{}",filled,_buffer.remaining());
219 } 219 }
220 catch(IOException e) 220 catch(IOException e)
221 { 221 {
222 LOG.debug(this.toString(),e); 222 LOG.debug(this.toString(),e);
244 // was this unexpected? 244 // was this unexpected?
245 switch(_state) 245 switch(_state)
246 { 246 {
247 case STATE_END: 247 case STATE_END:
248 case STATE_SEEKING_EOF: 248 case STATE_SEEKING_EOF:
249 _state=STATE_END; 249 _state = STATE_END;
250 break; 250 break;
251 251
252 case STATE_EOF_CONTENT: 252 case STATE_EOF_CONTENT:
253 _state=STATE_END; 253 _state = STATE_END;
254 _handler.messageComplete(_contentPosition); 254 _handler.messageComplete(_contentPosition);
255 break; 255 break;
256 256
257 default: 257 default:
258 _state=STATE_END; 258 _state = STATE_END;
259 if (!_headResponse) 259 if (!_headResponse)
260 _handler.earlyEOF(); 260 _handler.earlyEOF();
261 _handler.messageComplete(_contentPosition); 261 _handler.messageComplete(_contentPosition);
262 } 262 }
263 263
267 if (!isComplete() && !isIdle()) 267 if (!isComplete() && !isIdle())
268 throw new EofException(); 268 throw new EofException();
269 269
270 return -1; 270 return -1;
271 } 271 }
272 length=_buffer.remaining(); 272 length = _buffer.remaining();
273 } 273 }
274 274
275 275
276 // Handle header states 276 // Handle header states
277 byte ch; 277 byte ch;
278 byte[] array=_buffer.array(); 278 byte[] array = _buffer.array();
279 int last=_state; 279 int last = _state;
280 while (_state<STATE_END && length-->0) 280 while (_state<STATE_END && length-->0)
281 { 281 {
282 if (last!=_state) 282 if (last!=_state)
283 { 283 {
284 progress++; 284 progress++;
285 last=_state; 285 last = _state;
286 } 286 }
287 287
288 ch=_buffer.get(); 288 ch = _buffer.get();
289 289
290 if (_eol == HttpTokens.CARRIAGE_RETURN) 290 if (_eol == HttpTokens.CARRIAGE_RETURN)
291 { 291 {
292 if (ch == HttpTokens.LINE_FEED) 292 if (ch == HttpTokens.LINE_FEED)
293 { 293 {
469 case HttpHeaders.CONTENT_LENGTH_ORDINAL: 469 case HttpHeaders.CONTENT_LENGTH_ORDINAL:
470 if (_contentLength != HttpTokens.CHUNKED_CONTENT ) 470 if (_contentLength != HttpTokens.CHUNKED_CONTENT )
471 { 471 {
472 try 472 try
473 { 473 {
474 _contentLength=BufferUtil.toLong(value); 474 _contentLength = BufferUtil.toLong(value);
475 } 475 }
476 catch(NumberFormatException e) 476 catch(NumberFormatException e)
477 { 477 {
478 LOG.trace("",e); 478 LOG.trace("",e);
479 throw new HttpException(HttpStatus.BAD_REQUEST_400); 479 throw new HttpException(HttpStatus.BAD_REQUEST_400);
789 return 1; 789 return 1;
790 } 790 }
791 791
792 case STATE_CONTENT: 792 case STATE_CONTENT:
793 { 793 {
794 long remaining=_contentLength - _contentPosition; 794 long remaining = _contentLength - _contentPosition;
795 if (remaining == 0) 795 if (remaining == 0)
796 { 796 {
797 _state = _persistent?STATE_END:STATE_SEEKING_EOF; 797 _state = _persistent?STATE_END:STATE_SEEKING_EOF;
798 _handler.messageComplete(_contentPosition); 798 _handler.messageComplete(_contentPosition);
799 return 1; 799 return 1;
929 clear(); 929 clear();
930 break; 930 break;
931 } 931 }
932 } 932 }
933 933
934 length=_buffer.remaining(); 934 length = _buffer.remaining();
935 } 935 }
936 936
937 return progress; 937 return progress;
938 } 938 }
939 catch(HttpException e) 939 catch(HttpException e)