comparison src/org/eclipse/jetty/http/HttpParser.java @ 1033:4ada7a8c128a

remove HttpParser._buffers
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 03 Nov 2016 04:00:25 -0600
parents eca26899c4bc
children 563458c4dc93
comparison
equal deleted inserted replaced
1032:eca26899c4bc 1033:4ada7a8c128a
58 private static final int STATE_CHUNK_PARAMS=5; 58 private static final int STATE_CHUNK_PARAMS=5;
59 private static final int STATE_CHUNK=6; 59 private static final int STATE_CHUNK=6;
60 private static final int STATE_SEEKING_EOF=7; 60 private static final int STATE_SEEKING_EOF=7;
61 61
62 private final EventHandler _handler; 62 private final EventHandler _handler;
63 private final Buffers _buffers; // source of buffers
64 private final EndPoint _endp; 63 private final EndPoint _endp;
65 private Buffer _header; // Buffer for header data (and small _content) 64 public final Buffer _header; // Buffer for header data (and small _content)
66 private Buffer _body; // Buffer for large content 65 private final Buffer _body; // Buffer for large content
67 private Buffer _buffer; // The current buffer in use (either _header or _content) 66 private Buffer _buffer; // The current buffer in use (either _header or _content)
68 private String _cached; 67 private String _cached;
69 private String _tok0; // Saved token: header name, request method or response version 68 private String _tok0 = ""; // Saved token: header name, request method or response version
70 private String _tok1; // Saved token: header value, request URI or response code 69 private String _tok1 = ""; // Saved token: header value, request URI or response code
71 private String _multiLineValue; 70 private String _multiLineValue;
72 private int _responseStatus; // If >0 then we are parsing a response 71 private int _responseStatus; // If >0 then we are parsing a response
73 private boolean _persistent; 72 private boolean _persistent;
74 73
75 protected final View _contentView = new View(); // View of the content in the buffer for {@link Input} 74 private final View _contentView = new View(); // View of the content in the buffer for {@link Input}
76 protected int _state = STATE_START; 75 private int _state = STATE_START;
77 protected byte _eol; 76 private byte _eol;
78 protected int _length; 77 private int _length;
79 protected long _contentLength; 78 private long _contentLength;
80 protected long _contentPosition; 79 private long _contentPosition;
81 protected int _chunkLength; 80 private int _chunkLength;
82 protected int _chunkPosition; 81 private int _chunkPosition;
83 private boolean _headResponse; 82 private boolean _headResponse;
84 83
85 public HttpParser(Buffers buffers, EndPoint endp, EventHandler handler) 84 public HttpParser(Buffer headerBuffer,Buffer bodyBuffer, EndPoint endp, EventHandler handler)
86 { 85 {
87 _buffers = buffers; 86 _header = headerBuffer;
87 _body = bodyBuffer;
88 _endp = endp; 88 _endp = endp;
89 _handler = handler; 89 _handler = handler;
90 } 90 }
91 91
92 public long getContentLength() 92 public long getContentLength()
169 169
170 if (_state == STATE_END) 170 if (_state == STATE_END)
171 return 0; 171 return 0;
172 172
173 if (_buffer==null) 173 if (_buffer==null)
174 _buffer = getHeaderBuffer(); 174 _buffer = _header;
175 175
176 176
177 if (_state == STATE_CONTENT && _contentPosition == _contentLength) 177 if (_state == STATE_CONTENT && _contentPosition == _contentLength)
178 { 178 {
179 _state=STATE_END; 179 _state=STATE_END;
287 break; 287 break;
288 288
289 case STATE_FIELD0: 289 case STATE_FIELD0:
290 if (ch == HttpTokens.SPACE) 290 if (ch == HttpTokens.SPACE)
291 { 291 {
292 //System.out.println("qqqqqqqqqqqqqqqq a "+_tok0);
293 // _tok0.update(_buffer.markIndex(), _buffer.getIndex() - 1);
294 _tok0 = _buffer.toString(_buffer.markIndex(), _buffer.getIndex() - 1 - _buffer.markIndex()); 292 _tok0 = _buffer.toString(_buffer.markIndex(), _buffer.getIndex() - 1 - _buffer.markIndex());
295 //System.out.println("qqqqqqqqqqqqqqqq b "+_tok0);
296 _responseStatus = !HttpVersions.CACHE.contains(_tok0)?-1:0; 293 _responseStatus = !HttpVersions.CACHE.contains(_tok0)?-1:0;
297 _state=STATE_SPACE1; 294 _state=STATE_SPACE1;
298 continue; 295 continue;
299 } 296 }
300 else if (ch < HttpTokens.SPACE && ch>=0) 297 else if (ch < HttpTokens.SPACE && ch>=0)
336 } 333 }
337 else if (ch < HttpTokens.SPACE && ch>=0) 334 else if (ch < HttpTokens.SPACE && ch>=0)
338 { 335 {
339 _eol=ch; 336 _eol=ch;
340 _state=STATE_HEADER; 337 _state=STATE_HEADER;
341 // _tok0.setPutIndex(_tok0.getIndex());
342 _tok0 = ""; 338 _tok0 = "";
343 // _tok1.setPutIndex(_tok1.getIndex());
344 _tok1 = ""; 339 _tok1 = "";
345 _multiLineValue=null; 340 _multiLineValue=null;
346 continue; 341 continue;
347 } 342 }
348 // not a digit, so must be a URI 343 // not a digit, so must be a URI
380 { 375 {
381 if (_responseStatus>0) 376 if (_responseStatus>0)
382 { 377 {
383 _eol=ch; 378 _eol=ch;
384 _state=STATE_HEADER; 379 _state=STATE_HEADER;
385 // _tok0.setPutIndex(_tok0.getIndex());
386 _tok0 = ""; 380 _tok0 = "";
387 // _tok1.setPutIndex(_tok1.getIndex());
388 _tok1 = ""; 381 _tok1 = "";
389 _multiLineValue=null; 382 _multiLineValue=null;
390 } 383 }
391 else 384 else
392 { 385 {
411 else 404 else
412 _handler.startRequest(_tok0, _tok1, version=_buffer.sliceFromMark().toString()); 405 _handler.startRequest(_tok0, _tok1, version=_buffer.sliceFromMark().toString());
413 _eol=ch; 406 _eol=ch;
414 _persistent = HttpVersions.CACHE.getOrdinal(version) >= HttpVersions.HTTP_1_1_ORDINAL; 407 _persistent = HttpVersions.CACHE.getOrdinal(version) >= HttpVersions.HTTP_1_1_ORDINAL;
415 _state=STATE_HEADER; 408 _state=STATE_HEADER;
416 // _tok0.setPutIndex(_tok0.getIndex());
417 _tok0 = ""; 409 _tok0 = "";
418 // _tok1.setPutIndex(_tok1.getIndex());
419 _tok1 = ""; 410 _tok1 = "";
420 _multiLineValue=null; 411 _multiLineValue=null;
421 continue; 412 continue;
422 } 413 }
423 break; 414 break;
514 } 505 }
515 } 506 }
516 } 507 }
517 508
518 _handler.parsedHeader(header, value); 509 _handler.parsedHeader(header, value);
519 // _tok0.setPutIndex(_tok0.getIndex());
520 _tok0 = ""; 510 _tok0 = "";
521 // _tok1.setPutIndex(_tok1.getIndex());
522 _tok1 = ""; 511 _tok1 = "";
523 _multiLineValue=null; 512 _multiLineValue=null;
524 } 513 }
525 _buffer.setMarkIndex(-1); 514 _buffer.setMarkIndex(-1);
526 515
607 switch(ch) 596 switch(ch)
608 { 597 {
609 case HttpTokens.CARRIAGE_RETURN: 598 case HttpTokens.CARRIAGE_RETURN:
610 case HttpTokens.LINE_FEED: 599 case HttpTokens.LINE_FEED:
611 if (_length > 0) { 600 if (_length > 0) {
612 //System.out.println("qqqqqqqqqqqqqqqq c "+_tok0);
613 // _tok0.update(_buffer.markIndex(), _buffer.markIndex() + _length);
614 _tok0 = _buffer.toString(_buffer.markIndex(), _length); 601 _tok0 = _buffer.toString(_buffer.markIndex(), _length);
615 //System.out.println("qqqqqqqqqqqqqqqq d "+_tok0);
616 } 602 }
617 _eol=ch; 603 _eol=ch;
618 _state=STATE_HEADER; 604 _state=STATE_HEADER;
619 break; 605 break;
620 case HttpTokens.COLON: 606 case HttpTokens.COLON:
621 if (_length > 0 && _cached==null) { 607 if (_length > 0 && _cached==null) {
622 //System.out.println("qqqqqqqqqqqqqqqq e "+_tok0);
623 // _tok0.update(_buffer.markIndex(), _buffer.markIndex() + _length);
624 _tok0 = _buffer.toString(_buffer.markIndex(), _length); 608 _tok0 = _buffer.toString(_buffer.markIndex(), _length);
625 //System.out.println("qqqqqqqqqqqqqqqq f "+_tok0);
626 } 609 }
627 _length=-1; 610 _length=-1;
628 _state=STATE_HEADER_VALUE; 611 _state=STATE_HEADER_VALUE;
629 break; 612 break;
630 case HttpTokens.SPACE: 613 case HttpTokens.SPACE:
646 switch(ch) 629 switch(ch)
647 { 630 {
648 case HttpTokens.CARRIAGE_RETURN: 631 case HttpTokens.CARRIAGE_RETURN:
649 case HttpTokens.LINE_FEED: 632 case HttpTokens.LINE_FEED:
650 if (_length > 0) { 633 if (_length > 0) {
651 //System.out.println("qqqqqqqqqqqqqqqq g "+_tok0);
652 // _tok0.update(_buffer.markIndex(), _buffer.markIndex() + _length);
653 _tok0 = _buffer.toString(_buffer.markIndex(),_length); 634 _tok0 = _buffer.toString(_buffer.markIndex(),_length);
654 //System.out.println("qqqqqqqqqqqqqqqq h "+_tok0);
655 } 635 }
656 _eol=ch; 636 _eol=ch;
657 _state=STATE_HEADER; 637 _state=STATE_HEADER;
658 break; 638 break;
659 case HttpTokens.COLON: 639 case HttpTokens.COLON:
660 if (_length > 0 && _cached==null) { 640 if (_length > 0 && _cached==null) {
661 //System.out.println("qqqqqqqqqqqqqqqq i "+_tok0);
662 // _tok0.update(_buffer.markIndex(), _buffer.markIndex() + _length);
663 _tok0 = _buffer.toString(_buffer.markIndex(),_length); 641 _tok0 = _buffer.toString(_buffer.markIndex(),_length);
664 //System.out.println("qqqqqqqqqqqqqqqq j "+_tok0);
665 } 642 }
666 _length=-1; 643 _length=-1;
667 _state=STATE_HEADER_VALUE; 644 _state=STATE_HEADER_VALUE;
668 break; 645 break;
669 case HttpTokens.SPACE: 646 case HttpTokens.SPACE:
948 */ 925 */
949 private int fill() throws IOException 926 private int fill() throws IOException
950 { 927 {
951 // Do we have a buffer? 928 // Do we have a buffer?
952 if (_buffer==null) 929 if (_buffer==null)
953 _buffer=getHeaderBuffer(); 930 _buffer = _header;
954 931
955 // Is there unconsumed content in body buffer 932 // Is there unconsumed content in body buffer
956 if (_state>STATE_END && _buffer==_header && _header!=null && !_header.hasContent() && _body!=null && _body.hasContent()) 933 if (_state>STATE_END && _buffer==_header && !_header.hasContent() && _body.hasContent())
957 { 934 {
958 _buffer=_body; 935 _buffer = _body;
959 return _buffer.length(); 936 return _buffer.length();
960 } 937 }
961 938
962 // Shall we switch to a body buffer? 939 // Shall we switch to a body buffer?
963 if (_buffer==_header && _state>STATE_END && _header.length()==0 && ((_contentLength-_contentPosition)>_header.capacity()) && (_body!=null||_buffers!=null)) 940 if (_buffer==_header && _state>STATE_END && _header.length()==0 && ((_contentLength-_contentPosition)>_header.capacity()))
964 { 941 {
965 if (_body==null) 942 _buffer = _body;
966 _body=_buffers.getBuffer();
967 _buffer=_body;
968 } 943 }
969 944
970 // Shall we compact the body? 945 // Shall we compact the body?
971 if (_buffer==_body || _state>STATE_END) 946 if (_buffer==_body || _state>STATE_END)
972 { 947 {
991 LOG.debug("",e); 966 LOG.debug("",e);
992 throw (e instanceof EofException) ? e:new EofException(e); 967 throw (e instanceof EofException) ? e:new EofException(e);
993 } 968 }
994 */ 969 */
995 return _endp.fill(_buffer); 970 return _endp.fill(_buffer);
996 }
997
998 public void returnBuffers()
999 {
1000 if (_body!=null && !_body.hasContent() && _body.markIndex()==-1 && _buffers!=null)
1001 {
1002 if (_buffer==_body)
1003 _buffer=_header;
1004 _body=null;
1005 }
1006
1007 if (_header!=null && !_header.hasContent() && _header.markIndex()==-1 && _buffers!=null)
1008 {
1009 if (_buffer==_header)
1010 _buffer=null;
1011 _header=null;
1012 }
1013 } 971 }
1014 972
1015 @Override 973 @Override
1016 public String toString() 974 public String toString()
1017 { 975 {
1018 return String.format("%s{s=%d,l=%d,c=%d}", 976 return String.format("%s{s=%d,l=%d,c=%d}",
1019 getClass().getSimpleName(), 977 getClass().getSimpleName(),
1020 _state, 978 _state,
1021 _length, 979 _length,
1022 _contentLength); 980 _contentLength);
1023 }
1024
1025 public Buffer getHeaderBuffer()
1026 {
1027 if (_header == null)
1028 {
1029 _header = _buffers.getHeader();
1030 //System.out.println("qqqqqqqqqqqqqqqq k "+_tok0);
1031 // _tok0.update(_header);
1032 _tok0 = "";
1033 //System.out.println("qqqqqqqqqqqqqqqq l "+_tok0);
1034 // _tok1.update(_header);
1035 _tok1 = "";
1036 }
1037 return _header;
1038 } 981 }
1039 982
1040 public Buffer blockForContent(long maxIdleTime) throws IOException 983 public Buffer blockForContent(long maxIdleTime) throws IOException
1041 { 984 {
1042 if (_contentView.length()>0) 985 if (_contentView.length()>0)