comparison src/org/eclipse/jetty/http/HttpParser.java @ 1044:dd71a59fcf72

remove buffer marking
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 04 Nov 2016 02:26:54 -0600
parents 35e3c864d7a7
children a8c92b0a08ed
comparison
equal deleted inserted replaced
1043:ae1c92957739 1044:dd71a59fcf72
58 private final EventHandler _handler; 58 private final EventHandler _handler;
59 private final EndPoint _endp; 59 private final EndPoint _endp;
60 public final Buffer _header; // Buffer for header data (and small _content) 60 public final Buffer _header; // Buffer for header data (and small _content)
61 private final Buffer _body; // Buffer for large content 61 private final Buffer _body; // Buffer for large content
62 private Buffer _buffer; // The current buffer in use (either _header or _content) 62 private Buffer _buffer; // The current buffer in use (either _header or _content)
63 private int _mark = -1;
63 private String _cached; 64 private String _cached;
64 private String _tok0 = ""; // Saved token: header name, request method or response version 65 private String _tok0 = ""; // Saved token: header name, request method or response version
65 private String _tok1 = ""; // Saved token: header value, request URI or response code 66 private String _tok1 = ""; // Saved token: header value, request URI or response code
66 private String _multiLineValue; 67 private String _multiLineValue;
67 private int _responseStatus; // If >0 then we are parsing a response 68 private int _responseStatus; // If >0 then we are parsing a response
81 { 82 {
82 _header = headerBuffer; 83 _header = headerBuffer;
83 _body = bodyBuffer; 84 _body = bodyBuffer;
84 _endp = endp; 85 _endp = endp;
85 _handler = handler; 86 _handler = handler;
87 }
88
89 private void mark() {
90 _mark = _buffer.getIndex() - 1;
91 }
92
93 private Buffer sliceFromMark() {
94 Buffer buf = _buffer.sliceFrom(_mark);
95 _mark = -1;
96 return buf;
97 }
98
99 private void clear() {
100 _buffer.clear();
101 _mark = -1;
102 }
103
104 private void compact() {
105 if( _mark == -1 ) {
106 _buffer.compact();
107 } else if( _mark > 0 ) {
108 int old = _buffer.getIndex();
109 _buffer.setGetIndex(_mark);
110 _buffer.compact();
111 _buffer.setGetIndex( old - _mark );
112 _mark = 0;
113 }
86 } 114 }
87 115
88 public long getContentLength() 116 public long getContentLength()
89 { 117 {
90 return _contentLength; 118 return _contentLength;
275 case STATE_START: 303 case STATE_START:
276 _contentLength = HttpTokens.UNKNOWN_CONTENT; 304 _contentLength = HttpTokens.UNKNOWN_CONTENT;
277 _cached = null; 305 _cached = null;
278 if (ch > HttpTokens.SPACE || ch<0) 306 if (ch > HttpTokens.SPACE || ch<0)
279 { 307 {
280 _buffer.mark(); 308 mark();
281 _state=STATE_FIELD0; 309 _state = STATE_FIELD0;
282 } 310 }
283 break; 311 break;
284 312
285 case STATE_FIELD0: 313 case STATE_FIELD0:
286 if (ch == HttpTokens.SPACE) 314 if (ch == HttpTokens.SPACE)
287 { 315 {
288 _tok0 = _buffer.toString(_buffer.markIndex(), _buffer.getIndex() - 1 - _buffer.markIndex()); 316 _tok0 = _buffer.toString(_mark, _buffer.getIndex() - 1 - _mark);
289 _responseStatus = !HttpVersions.CACHE.contains(_tok0)?-1:0; 317 _responseStatus = !HttpVersions.CACHE.contains(_tok0)?-1:0;
290 _state=STATE_SPACE1; 318 _state=STATE_SPACE1;
291 continue; 319 continue;
292 } 320 }
293 else if (ch < HttpTokens.SPACE && ch>=0) 321 else if (ch < HttpTokens.SPACE && ch>=0)
297 break; 325 break;
298 326
299 case STATE_SPACE1: 327 case STATE_SPACE1:
300 if (ch > HttpTokens.SPACE || ch<0) 328 if (ch > HttpTokens.SPACE || ch<0)
301 { 329 {
302 _buffer.mark(); 330 mark();
303 if (_responseStatus>=0) 331 if (_responseStatus>=0)
304 { 332 {
305 _state = STATE_STATUS; 333 _state = STATE_STATUS;
306 _responseStatus=ch-'0'; 334 _responseStatus=ch-'0';
307 } 335 }
315 break; 343 break;
316 344
317 case STATE_STATUS: 345 case STATE_STATUS:
318 if (ch == HttpTokens.SPACE) 346 if (ch == HttpTokens.SPACE)
319 { 347 {
320 // _tok1.update(_buffer.markIndex(), _buffer.getIndex() - 1); 348 // _tok1.update(_mark, _buffer.getIndex() - 1);
321 _tok1 = _buffer.toString(_buffer.markIndex(), _buffer.getIndex() - 1 - _buffer.markIndex()); 349 _tok1 = _buffer.toString(_mark, _buffer.getIndex() - 1 - _mark);
322 _state = STATE_SPACE2; 350 _state = STATE_SPACE2;
323 continue; 351 continue;
324 } 352 }
325 else if (ch>='0' && ch<='9') 353 else if (ch>='0' && ch<='9')
326 { 354 {
342 break; 370 break;
343 371
344 case STATE_URI: 372 case STATE_URI:
345 if (ch == HttpTokens.SPACE) 373 if (ch == HttpTokens.SPACE)
346 { 374 {
347 // _tok1.update(_buffer.markIndex(), _buffer.getIndex() - 1); 375 // _tok1.update(_mark, _buffer.getIndex() - 1);
348 _tok1 = _buffer.toString(_buffer.markIndex(), _buffer.getIndex() - 1 - _buffer.markIndex()); 376 _tok1 = _buffer.toString(_mark, _buffer.getIndex() - 1 - _mark);
349 _state=STATE_SPACE2; 377 _state=STATE_SPACE2;
350 continue; 378 continue;
351 } 379 }
352 else if (ch < HttpTokens.SPACE && ch>=0) 380 else if (ch < HttpTokens.SPACE && ch>=0)
353 { 381 {
354 // HTTP/0.9 382 // HTTP/0.9
355 _handler.startRequest(_tok0, _buffer.sliceFromMark().toString(), null); 383 _handler.startRequest(_tok0, sliceFromMark().toString(), null);
356 _persistent = false; 384 _persistent = false;
357 _state = STATE_SEEKING_EOF; 385 _state = STATE_SEEKING_EOF;
358 _handler.headerComplete(); 386 _handler.headerComplete();
359 _handler.messageComplete(_contentPosition); 387 _handler.messageComplete(_contentPosition);
360 return 1; 388 return 1;
362 break; 390 break;
363 391
364 case STATE_SPACE2: 392 case STATE_SPACE2:
365 if (ch > HttpTokens.SPACE || ch<0) 393 if (ch > HttpTokens.SPACE || ch<0)
366 { 394 {
367 _buffer.mark(); 395 mark();
368 _state=STATE_FIELD2; 396 _state=STATE_FIELD2;
369 } 397 }
370 else if (ch < HttpTokens.SPACE) 398 else if (ch < HttpTokens.SPACE)
371 { 399 {
372 if (_responseStatus>0) 400 if (_responseStatus>0)
393 case STATE_FIELD2: 421 case STATE_FIELD2:
394 if (ch == HttpTokens.CARRIAGE_RETURN || ch == HttpTokens.LINE_FEED) 422 if (ch == HttpTokens.CARRIAGE_RETURN || ch == HttpTokens.LINE_FEED)
395 { 423 {
396 String version; 424 String version;
397 if (_responseStatus > 0) 425 if (_responseStatus > 0)
398 // _handler.startResponse(version=HttpVersions.CACHE.lookup(_tok0), _responseStatus,_buffer.sliceFromMark()); 426 // _handler.startResponse(version=HttpVersions.CACHE.lookup(_tok0), _responseStatus,sliceFromMark());
399 version = _tok0; 427 version = _tok0;
400 else 428 else
401 _handler.startRequest(_tok0, _tok1, version=_buffer.sliceFromMark().toString()); 429 _handler.startRequest(_tok0, _tok1, version=sliceFromMark().toString());
402 _eol=ch; 430 _eol=ch;
403 _persistent = HttpVersions.CACHE.getOrdinal(version) >= HttpVersions.HTTP_1_1_ORDINAL; 431 _persistent = HttpVersions.CACHE.getOrdinal(version) >= HttpVersions.HTTP_1_1_ORDINAL;
404 _state=STATE_HEADER; 432 _state=STATE_HEADER;
405 _tok0 = ""; 433 _tok0 = "";
406 _tok1 = ""; 434 _tok1 = "";
505 _handler.parsedHeader(header, value); 533 _handler.parsedHeader(header, value);
506 _tok0 = ""; 534 _tok0 = "";
507 _tok1 = ""; 535 _tok1 = "";
508 _multiLineValue=null; 536 _multiLineValue=null;
509 } 537 }
510 _buffer.setMarkIndex(-1); 538 _mark = -1;
511 539
512 // now handle ch 540 // now handle ch
513 if (ch == HttpTokens.CARRIAGE_RETURN || ch == HttpTokens.LINE_FEED) 541 if (ch == HttpTokens.CARRIAGE_RETURN || ch == HttpTokens.LINE_FEED)
514 { 542 {
515 // is it a response that cannot have a body? 543 // is it a response that cannot have a body?
564 } 592 }
565 else 593 else
566 { 594 {
567 // New header 595 // New header
568 _length = 1; 596 _length = 1;
569 _buffer.mark(); 597 mark();
570 _state = STATE_HEADER_NAME; 598 _state = STATE_HEADER_NAME;
571 599
572 // try cached name! 600 // try cached name!
573 if (array!=null) 601 if (array!=null)
574 { 602 {
575 String s = new String(array, _buffer.markIndex(), length+1); 603 String s = new String(array, _mark, length+1);
576 _cached = HttpHeaders.CACHE.getBest(s); 604 _cached = HttpHeaders.CACHE.getBest(s);
577 605
578 if (_cached!=null) 606 if (_cached!=null)
579 { 607 {
580 _length = _cached.length(); 608 _length = _cached.length();
581 _buffer.setGetIndex(_buffer.markIndex()+_length); 609 _buffer.setGetIndex(_mark+_length);
582 length = _buffer.remaining(); 610 length = _buffer.remaining();
583 } 611 }
584 } 612 }
585 } 613 }
586 } 614 }
592 switch(ch) 620 switch(ch)
593 { 621 {
594 case HttpTokens.CARRIAGE_RETURN: 622 case HttpTokens.CARRIAGE_RETURN:
595 case HttpTokens.LINE_FEED: 623 case HttpTokens.LINE_FEED:
596 if (_length > 0) { 624 if (_length > 0) {
597 _tok0 = _buffer.toString(_buffer.markIndex(), _length); 625 _tok0 = _buffer.toString(_mark, _length);
598 } 626 }
599 _eol=ch; 627 _eol=ch;
600 _state=STATE_HEADER; 628 _state=STATE_HEADER;
601 break; 629 break;
602 case HttpTokens.COLON: 630 case HttpTokens.COLON:
603 if (_length > 0 && _cached==null) { 631 if (_length > 0 && _cached==null) {
604 _tok0 = _buffer.toString(_buffer.markIndex(), _length); 632 _tok0 = _buffer.toString(_mark, _length);
605 } 633 }
606 _length=-1; 634 _length=-1;
607 _state=STATE_HEADER_VALUE; 635 _state=STATE_HEADER_VALUE;
608 break; 636 break;
609 case HttpTokens.SPACE: 637 case HttpTokens.SPACE:
611 break; 639 break;
612 default: 640 default:
613 { 641 {
614 _cached = null; 642 _cached = null;
615 if (_length == -1) 643 if (_length == -1)
616 _buffer.mark(); 644 mark();
617 _length=_buffer.getIndex() - _buffer.markIndex(); 645 _length=_buffer.getIndex() - _mark;
618 _state = STATE_HEADER_IN_NAME; 646 _state = STATE_HEADER_IN_NAME;
619 } 647 }
620 } 648 }
621 649
622 break; 650 break;
625 switch(ch) 653 switch(ch)
626 { 654 {
627 case HttpTokens.CARRIAGE_RETURN: 655 case HttpTokens.CARRIAGE_RETURN:
628 case HttpTokens.LINE_FEED: 656 case HttpTokens.LINE_FEED:
629 if (_length > 0) { 657 if (_length > 0) {
630 _tok0 = _buffer.toString(_buffer.markIndex(),_length); 658 _tok0 = _buffer.toString(_mark,_length);
631 } 659 }
632 _eol=ch; 660 _eol=ch;
633 _state=STATE_HEADER; 661 _state=STATE_HEADER;
634 break; 662 break;
635 case HttpTokens.COLON: 663 case HttpTokens.COLON:
636 if (_length > 0 && _cached==null) { 664 if (_length > 0 && _cached==null) {
637 _tok0 = _buffer.toString(_buffer.markIndex(),_length); 665 _tok0 = _buffer.toString(_mark,_length);
638 } 666 }
639 _length=-1; 667 _length=-1;
640 _state=STATE_HEADER_VALUE; 668 _state=STATE_HEADER_VALUE;
641 break; 669 break;
642 case HttpTokens.SPACE: 670 case HttpTokens.SPACE:
657 case HttpTokens.CARRIAGE_RETURN: 685 case HttpTokens.CARRIAGE_RETURN:
658 case HttpTokens.LINE_FEED: 686 case HttpTokens.LINE_FEED:
659 if (_length > 0) 687 if (_length > 0)
660 { 688 {
661 if (_tok1.length() == 0) 689 if (_tok1.length() == 0)
662 // _tok1.update(_buffer.markIndex(), _buffer.markIndex() + _length); 690 // _tok1.update(_mark, _mark + _length);
663 _tok1 = _buffer.toString(_buffer.markIndex(), _length); 691 _tok1 = _buffer.toString(_mark, _length);
664 else 692 else
665 { 693 {
666 // Continuation line! 694 // Continuation line!
667 if (_multiLineValue == null) _multiLineValue = _tok1; 695 if (_multiLineValue == null) _multiLineValue = _tok1;
668 // _tok1.update(_buffer.markIndex(), _buffer.markIndex() + _length); 696 // _tok1.update(_mark, _mark + _length);
669 _tok1 = _buffer.toString(_buffer.markIndex(), _length); 697 _tok1 = _buffer.toString(_mark, _length);
670 _multiLineValue += " " + _tok1; 698 _multiLineValue += " " + _tok1;
671 } 699 }
672 } 700 }
673 _eol=ch; 701 _eol=ch;
674 _state=STATE_HEADER; 702 _state=STATE_HEADER;
677 case HttpTokens.TAB: 705 case HttpTokens.TAB:
678 break; 706 break;
679 default: 707 default:
680 { 708 {
681 if (_length == -1) 709 if (_length == -1)
682 _buffer.mark(); 710 mark();
683 _length=_buffer.getIndex() - _buffer.markIndex(); 711 _length=_buffer.getIndex() - _mark;
684 _state = STATE_HEADER_IN_VALUE; 712 _state = STATE_HEADER_IN_VALUE;
685 } 713 }
686 } 714 }
687 break; 715 break;
688 716
692 case HttpTokens.CARRIAGE_RETURN: 720 case HttpTokens.CARRIAGE_RETURN:
693 case HttpTokens.LINE_FEED: 721 case HttpTokens.LINE_FEED:
694 if (_length > 0) 722 if (_length > 0)
695 { 723 {
696 if (_tok1.length() == 0) 724 if (_tok1.length() == 0)
697 // _tok1.update(_buffer.markIndex(), _buffer.markIndex() + _length); 725 // _tok1.update(_mark, _mark + _length);
698 _tok1 = _buffer.toString(_buffer.markIndex(), _length); 726 _tok1 = _buffer.toString(_mark, _length);
699 else 727 else
700 { 728 {
701 // Continuation line! 729 // Continuation line!
702 if (_multiLineValue == null) _multiLineValue = _tok1; 730 if (_multiLineValue == null) _multiLineValue = _tok1;
703 // _tok1.update(_buffer.markIndex(), _buffer.markIndex() + _length); 731 // _tok1.update(_mark, _mark + _length);
704 _tok1 = _buffer.toString(_buffer.markIndex(), _length); 732 _tok1 = _buffer.toString(_mark, _length);
705 _multiLineValue += " " + _tok1; 733 _multiLineValue += " " + _tok1;
706 } 734 }
707 } 735 }
708 _eol=ch; 736 _eol=ch;
709 _state=STATE_HEADER; 737 _state=STATE_HEADER;
892 while (_buffer.remaining()>0) 920 while (_buffer.remaining()>0)
893 if (!Character.isWhitespace(_buffer.get())) 921 if (!Character.isWhitespace(_buffer.get()))
894 { 922 {
895 _state = STATE_END; 923 _state = STATE_END;
896 _endp.close(); 924 _endp.close();
897 _buffer.clear(); 925 clear();
898 } 926 }
899 } 927 }
900 928
901 _buffer.clear(); 929 clear();
902 break; 930 break;
903 } 931 }
904 } 932 }
905 933
906 length=_buffer.remaining(); 934 length=_buffer.remaining();
940 } 968 }
941 969
942 // Shall we compact the body? 970 // Shall we compact the body?
943 if (_buffer==_body || _state>STATE_END) 971 if (_buffer==_body || _state>STATE_END)
944 { 972 {
945 _buffer.compact(); 973 compact();
946 } 974 }
947 975
948 // Are we full? 976 // Are we full?
949 if (_buffer.space() == 0) 977 if (_buffer.space() == 0)
950 { 978 {
951 LOG.warn("HttpParser Full for {} ",_endp); 979 LOG.warn("HttpParser Full for {} ",_endp);
952 _buffer.clear(); 980 clear();
953 throw new HttpException(HttpStatus.REQUEST_ENTITY_TOO_LARGE_413, "Request Entity Too Large: "+(_buffer==_body?"body":"head")); 981 throw new HttpException(HttpStatus.REQUEST_ENTITY_TOO_LARGE_413, "Request Entity Too Large: "+(_buffer==_body?"body":"head"));
954 } 982 }
955 /* why? 983 /* why?
956 try 984 try
957 { 985 {