Mercurial Hosting > luan
comparison src/org/eclipse/jetty/http/HttpParser.java @ 1034:563458c4dc93
remove HttpGenerator.reset()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 03 Nov 2016 21:02:59 -0600 |
parents | 4ada7a8c128a |
children | b87f97f6418a |
comparison
equal
deleted
inserted
replaced
1033:4ada7a8c128a | 1034:563458c4dc93 |
---|---|
34 public final class HttpParser | 34 public final class HttpParser |
35 { | 35 { |
36 private static final Logger LOG = LoggerFactory.getLogger(HttpParser.class); | 36 private static final Logger LOG = LoggerFactory.getLogger(HttpParser.class); |
37 | 37 |
38 // States | 38 // States |
39 private static final int STATE_START=-14; | 39 private static final int STATE_START = -14; |
40 private static final int STATE_FIELD0=-13; | 40 private static final int STATE_FIELD0 = -13; |
41 private static final int STATE_SPACE1=-12; | 41 private static final int STATE_SPACE1 = -12; |
42 private static final int STATE_STATUS=-11; | 42 private static final int STATE_STATUS = -11; |
43 private static final int STATE_URI=-10; | 43 private static final int STATE_URI = -10; |
44 private static final int STATE_SPACE2=-9; | 44 private static final int STATE_SPACE2 = -9; |
45 private static final int STATE_END0=-8; | 45 private static final int STATE_FIELD2 = -6; |
46 private static final int STATE_END1=-7; | 46 private static final int STATE_HEADER = -5; |
47 private static final int STATE_FIELD2=-6; | 47 private static final int STATE_HEADER_NAME = -4; |
48 private static final int STATE_HEADER=-5; | 48 private static final int STATE_HEADER_IN_NAME = -3; |
49 private static final int STATE_HEADER_NAME=-4; | 49 private static final int STATE_HEADER_VALUE = -2; |
50 private static final int STATE_HEADER_IN_NAME=-3; | 50 private static final int STATE_HEADER_IN_VALUE = -1; |
51 private static final int STATE_HEADER_VALUE=-2; | 51 private static final int STATE_END = 0; |
52 private static final int STATE_HEADER_IN_VALUE=-1; | 52 private static final int STATE_EOF_CONTENT = 1; |
53 private static final int STATE_END=0; | 53 private static final int STATE_CONTENT = 2; |
54 private static final int STATE_EOF_CONTENT=1; | 54 private static final int STATE_CHUNKED_CONTENT = 3; |
55 private static final int STATE_CONTENT=2; | 55 private static final int STATE_CHUNK_SIZE = 4; |
56 private static final int STATE_CHUNKED_CONTENT=3; | 56 private static final int STATE_CHUNK_PARAMS = 5; |
57 private static final int STATE_CHUNK_SIZE=4; | 57 private static final int STATE_CHUNK = 6; |
58 private static final int STATE_CHUNK_PARAMS=5; | 58 private static final int STATE_SEEKING_EOF = 7; |
59 private static final int STATE_CHUNK=6; | |
60 private static final int STATE_SEEKING_EOF=7; | |
61 | 59 |
62 private final EventHandler _handler; | 60 private final EventHandler _handler; |
63 private final EndPoint _endp; | 61 private final EndPoint _endp; |
64 public final Buffer _header; // Buffer for header data (and small _content) | 62 public final Buffer _header; // Buffer for header data (and small _content) |
65 private final Buffer _body; // Buffer for large content | 63 private final Buffer _body; // Buffer for large content |
130 | 128 |
131 public void setPersistent(boolean persistent) | 129 public void setPersistent(boolean persistent) |
132 { | 130 { |
133 _persistent = persistent; | 131 _persistent = persistent; |
134 if (!_persistent &&(_state==STATE_END || _state==STATE_START)) | 132 if (!_persistent &&(_state==STATE_END || _state==STATE_START)) |
135 _state=STATE_SEEKING_EOF; | 133 _state = STATE_SEEKING_EOF; |
136 } | 134 } |
137 | 135 |
138 /* ------------------------------------------------------------------------------- */ | 136 /* ------------------------------------------------------------------------------- */ |
139 /** | 137 /** |
140 * Parse until END state. | 138 * Parse until END state. |
201 | 199 |
202 if (filled > 0 ) | 200 if (filled > 0 ) |
203 progress++; | 201 progress++; |
204 else if (filled < 0 ) | 202 else if (filled < 0 ) |
205 { | 203 { |
206 _persistent=false; | 204 _persistent = false; |
207 | 205 |
208 // do we have content to deliver? | 206 // do we have content to deliver? |
209 if (_state>STATE_END) | 207 if (_state>STATE_END) |
210 { | 208 { |
211 if (_buffer.length()>0 && !_headResponse) | 209 if (_buffer.length()>0 && !_headResponse) |
304 if (ch > HttpTokens.SPACE || ch<0) | 302 if (ch > HttpTokens.SPACE || ch<0) |
305 { | 303 { |
306 _buffer.mark(); | 304 _buffer.mark(); |
307 if (_responseStatus>=0) | 305 if (_responseStatus>=0) |
308 { | 306 { |
309 _state=STATE_STATUS; | 307 _state = STATE_STATUS; |
310 _responseStatus=ch-'0'; | 308 _responseStatus=ch-'0'; |
311 } | 309 } |
312 else | 310 else |
313 _state=STATE_URI; | 311 _state=STATE_URI; |
314 } | 312 } |
321 case STATE_STATUS: | 319 case STATE_STATUS: |
322 if (ch == HttpTokens.SPACE) | 320 if (ch == HttpTokens.SPACE) |
323 { | 321 { |
324 // _tok1.update(_buffer.markIndex(), _buffer.getIndex() - 1); | 322 // _tok1.update(_buffer.markIndex(), _buffer.getIndex() - 1); |
325 _tok1 = _buffer.toString(_buffer.markIndex(), _buffer.getIndex() - 1 - _buffer.markIndex()); | 323 _tok1 = _buffer.toString(_buffer.markIndex(), _buffer.getIndex() - 1 - _buffer.markIndex()); |
326 _state=STATE_SPACE2; | 324 _state = STATE_SPACE2; |
327 continue; | 325 continue; |
328 } | 326 } |
329 else if (ch>='0' && ch<='9') | 327 else if (ch>='0' && ch<='9') |
330 { | 328 { |
331 _responseStatus=_responseStatus*10+(ch-'0'); | 329 _responseStatus=_responseStatus*10+(ch-'0'); |
332 continue; | 330 continue; |
333 } | 331 } |
334 else if (ch < HttpTokens.SPACE && ch>=0) | 332 else if (ch < HttpTokens.SPACE && ch>=0) |
335 { | 333 { |
336 _eol=ch; | 334 _eol=ch; |
337 _state=STATE_HEADER; | 335 _state = STATE_HEADER; |
338 _tok0 = ""; | 336 _tok0 = ""; |
339 _tok1 = ""; | 337 _tok1 = ""; |
340 _multiLineValue=null; | 338 _multiLineValue=null; |
341 continue; | 339 continue; |
342 } | 340 } |
355 } | 353 } |
356 else if (ch < HttpTokens.SPACE && ch>=0) | 354 else if (ch < HttpTokens.SPACE && ch>=0) |
357 { | 355 { |
358 // HTTP/0.9 | 356 // HTTP/0.9 |
359 _handler.startRequest(_tok0, _buffer.sliceFromMark().toString(), null); | 357 _handler.startRequest(_tok0, _buffer.sliceFromMark().toString(), null); |
360 _persistent=false; | 358 _persistent = false; |
361 _state=STATE_SEEKING_EOF; | 359 _state = STATE_SEEKING_EOF; |
362 _handler.headerComplete(); | 360 _handler.headerComplete(); |
363 _handler.messageComplete(_contentPosition); | 361 _handler.messageComplete(_contentPosition); |
364 return 1; | 362 return 1; |
365 } | 363 } |
366 break; | 364 break; |
383 } | 381 } |
384 else | 382 else |
385 { | 383 { |
386 // HTTP/0.9 | 384 // HTTP/0.9 |
387 _handler.startRequest(_tok0, _tok1, null); | 385 _handler.startRequest(_tok0, _tok1, null); |
388 _persistent=false; | 386 _persistent = false; |
389 _state=STATE_SEEKING_EOF; | 387 _state = STATE_SEEKING_EOF; |
390 _handler.headerComplete(); | 388 _handler.headerComplete(); |
391 _handler.messageComplete(_contentPosition); | 389 _handler.messageComplete(_contentPosition); |
392 return 1; | 390 return 1; |
393 } | 391 } |
394 } | 392 } |
476 | 474 |
477 case HttpHeaders.CONNECTION_ORDINAL: | 475 case HttpHeaders.CONNECTION_ORDINAL: |
478 switch(HttpHeaderValues.CACHE.getOrdinal(value)) | 476 switch(HttpHeaderValues.CACHE.getOrdinal(value)) |
479 { | 477 { |
480 case HttpHeaderValues.CLOSE_ORDINAL: | 478 case HttpHeaderValues.CLOSE_ORDINAL: |
481 _persistent=false; | 479 _persistent = false; |
482 break; | 480 break; |
483 | 481 |
484 case HttpHeaderValues.KEEP_ALIVE_ORDINAL: | 482 case HttpHeaderValues.KEEP_ALIVE_ORDINAL: |
485 _persistent=true; | 483 _persistent = true; |
486 break; | 484 break; |
487 | 485 |
488 case -1: // No match, may be multi valued | 486 case -1: // No match, may be multi valued |
489 { | 487 { |
490 for (String v : value.split(",")) | 488 for (String v : value.split(",")) |
491 { | 489 { |
492 switch(HttpHeaderValues.CACHE.getOrdinal(v.trim())) | 490 switch(HttpHeaderValues.CACHE.getOrdinal(v.trim())) |
493 { | 491 { |
494 case HttpHeaderValues.CLOSE_ORDINAL: | 492 case HttpHeaderValues.CLOSE_ORDINAL: |
495 _persistent=false; | 493 _persistent = false; |
496 break; | 494 break; |
497 | 495 |
498 case HttpHeaderValues.KEEP_ALIVE_ORDINAL: | 496 case HttpHeaderValues.KEEP_ALIVE_ORDINAL: |
499 _persistent=true; | 497 _persistent = true; |
500 break; | 498 break; |
501 } | 499 } |
502 } | 500 } |
503 break; | 501 break; |
504 } | 502 } |
553 _handler.headerComplete(); // May recurse here ! | 551 _handler.headerComplete(); // May recurse here ! |
554 break; | 552 break; |
555 | 553 |
556 case HttpTokens.NO_CONTENT: | 554 case HttpTokens.NO_CONTENT: |
557 _handler.headerComplete(); | 555 _handler.headerComplete(); |
558 _state=_persistent||(_responseStatus>=100&&_responseStatus<200)?STATE_END:STATE_SEEKING_EOF; | 556 _state = _persistent||(_responseStatus>=100&&_responseStatus<200)?STATE_END:STATE_SEEKING_EOF; |
559 _handler.messageComplete(_contentPosition); | 557 _handler.messageComplete(_contentPosition); |
560 return 1; | 558 return 1; |
561 | 559 |
562 default: | 560 default: |
563 _state=STATE_CONTENT; | 561 _state=STATE_CONTENT; |
617 { | 615 { |
618 _cached = null; | 616 _cached = null; |
619 if (_length == -1) | 617 if (_length == -1) |
620 _buffer.mark(); | 618 _buffer.mark(); |
621 _length=_buffer.getIndex() - _buffer.markIndex(); | 619 _length=_buffer.getIndex() - _buffer.markIndex(); |
622 _state=STATE_HEADER_IN_NAME; | 620 _state = STATE_HEADER_IN_NAME; |
623 } | 621 } |
624 } | 622 } |
625 | 623 |
626 break; | 624 break; |
627 | 625 |
683 default: | 681 default: |
684 { | 682 { |
685 if (_length == -1) | 683 if (_length == -1) |
686 _buffer.mark(); | 684 _buffer.mark(); |
687 _length=_buffer.getIndex() - _buffer.markIndex(); | 685 _length=_buffer.getIndex() - _buffer.markIndex(); |
688 _state=STATE_HEADER_IN_VALUE; | 686 _state = STATE_HEADER_IN_VALUE; |
689 } | 687 } |
690 } | 688 } |
691 break; | 689 break; |
692 | 690 |
693 case STATE_HEADER_IN_VALUE: | 691 case STATE_HEADER_IN_VALUE: |
726 // ========================== | 724 // ========================== |
727 | 725 |
728 // Handle HEAD response | 726 // Handle HEAD response |
729 if (_responseStatus>0 && _headResponse) | 727 if (_responseStatus>0 && _headResponse) |
730 { | 728 { |
731 _state=_persistent||(_responseStatus>=100&&_responseStatus<200)?STATE_END:STATE_SEEKING_EOF; | 729 _state = _persistent||(_responseStatus>=100&&_responseStatus<200)?STATE_END:STATE_SEEKING_EOF; |
732 _handler.messageComplete(_contentLength); | 730 _handler.messageComplete(_contentLength); |
733 } | 731 } |
734 | 732 |
735 | 733 |
736 // ========================== | 734 // ========================== |
767 case STATE_CONTENT: | 765 case STATE_CONTENT: |
768 { | 766 { |
769 long remaining=_contentLength - _contentPosition; | 767 long remaining=_contentLength - _contentPosition; |
770 if (remaining == 0) | 768 if (remaining == 0) |
771 { | 769 { |
772 _state=_persistent?STATE_END:STATE_SEEKING_EOF; | 770 _state = _persistent?STATE_END:STATE_SEEKING_EOF; |
773 _handler.messageComplete(_contentPosition); | 771 _handler.messageComplete(_contentPosition); |
774 return 1; | 772 return 1; |
775 } | 773 } |
776 | 774 |
777 if (length > remaining) | 775 if (length > remaining) |
786 _contentView.update(chunk); | 784 _contentView.update(chunk); |
787 _handler.content(chunk); // May recurse here | 785 _handler.content(chunk); // May recurse here |
788 | 786 |
789 if(_contentPosition == _contentLength) | 787 if(_contentPosition == _contentLength) |
790 { | 788 { |
791 _state=_persistent?STATE_END:STATE_SEEKING_EOF; | 789 _state = _persistent?STATE_END:STATE_SEEKING_EOF; |
792 _handler.messageComplete(_contentPosition); | 790 _handler.messageComplete(_contentPosition); |
793 } | 791 } |
794 // TODO adjust the _buffer to keep unconsumed content | 792 // TODO adjust the _buffer to keep unconsumed content |
795 return 1; | 793 return 1; |
796 } | 794 } |
804 _buffer.get(); | 802 _buffer.get(); |
805 else | 803 else |
806 { | 804 { |
807 _chunkLength=0; | 805 _chunkLength=0; |
808 _chunkPosition=0; | 806 _chunkPosition=0; |
809 _state=STATE_CHUNK_SIZE; | 807 _state = STATE_CHUNK_SIZE; |
810 } | 808 } |
811 break; | 809 break; |
812 } | 810 } |
813 | 811 |
814 case STATE_CHUNK_SIZE: | 812 case STATE_CHUNK_SIZE: |
820 | 818 |
821 if (_chunkLength == 0) | 819 if (_chunkLength == 0) |
822 { | 820 { |
823 if (_eol==HttpTokens.CARRIAGE_RETURN && _buffer.hasContent() && _buffer.peek()==HttpTokens.LINE_FEED) | 821 if (_eol==HttpTokens.CARRIAGE_RETURN && _buffer.hasContent() && _buffer.peek()==HttpTokens.LINE_FEED) |
824 _eol=_buffer.get(); | 822 _eol=_buffer.get(); |
825 _state=_persistent?STATE_END:STATE_SEEKING_EOF; | 823 _state = _persistent?STATE_END:STATE_SEEKING_EOF; |
826 _handler.messageComplete(_contentPosition); | 824 _handler.messageComplete(_contentPosition); |
827 return 1; | 825 return 1; |
828 } | 826 } |
829 else | 827 else |
830 _state=STATE_CHUNK; | 828 _state = STATE_CHUNK; |
831 } | 829 } |
832 else if (ch <= HttpTokens.SPACE || ch == HttpTokens.SEMI_COLON) | 830 else if (ch <= HttpTokens.SPACE || ch == HttpTokens.SEMI_COLON) |
833 _state=STATE_CHUNK_PARAMS; | 831 _state=STATE_CHUNK_PARAMS; |
834 else if (ch >= '0' && ch <= '9') | 832 else if (ch >= '0' && ch <= '9') |
835 _chunkLength=_chunkLength * 16 + (ch - '0'); | 833 _chunkLength=_chunkLength * 16 + (ch - '0'); |
850 _eol=ch; | 848 _eol=ch; |
851 if (_chunkLength == 0) | 849 if (_chunkLength == 0) |
852 { | 850 { |
853 if (_eol==HttpTokens.CARRIAGE_RETURN && _buffer.hasContent() && _buffer.peek()==HttpTokens.LINE_FEED) | 851 if (_eol==HttpTokens.CARRIAGE_RETURN && _buffer.hasContent() && _buffer.peek()==HttpTokens.LINE_FEED) |
854 _eol=_buffer.get(); | 852 _eol=_buffer.get(); |
855 _state=_persistent?STATE_END:STATE_SEEKING_EOF; | 853 _state = _persistent?STATE_END:STATE_SEEKING_EOF; |
856 _handler.messageComplete(_contentPosition); | 854 _handler.messageComplete(_contentPosition); |
857 return 1; | 855 return 1; |
858 } | 856 } |
859 else | 857 else |
860 _state=STATE_CHUNK; | 858 _state=STATE_CHUNK; |
884 case STATE_SEEKING_EOF: | 882 case STATE_SEEKING_EOF: |
885 { | 883 { |
886 // Close if there is more data than CRLF | 884 // Close if there is more data than CRLF |
887 if (_buffer.length()>2) | 885 if (_buffer.length()>2) |
888 { | 886 { |
889 _state=STATE_END; | 887 _state = STATE_END; |
890 _endp.close(); | 888 _endp.close(); |
891 } | 889 } |
892 else | 890 else |
893 { | 891 { |
894 // or if the data is not white space | 892 // or if the data is not white space |
911 | 909 |
912 return progress; | 910 return progress; |
913 } | 911 } |
914 catch(HttpException e) | 912 catch(HttpException e) |
915 { | 913 { |
916 _persistent=false; | 914 _persistent = false; |
917 _state=STATE_SEEKING_EOF; | 915 _state = STATE_SEEKING_EOF; |
918 throw e; | 916 throw e; |
919 } | 917 } |
920 } | 918 } |
921 | 919 |
922 /* ------------------------------------------------------------------------------- */ | 920 /* ------------------------------------------------------------------------------- */ |