comparison src/org/eclipse/jetty/http/HttpGenerator.java @ 1059:013939bfc9e8

remove JBuffer.poke()
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 08 Nov 2016 05:39:33 -0700
parents 419bf9c03d84
children a0abb16cf6e7
comparison
equal deleted inserted replaced
1058:419bf9c03d84 1059:013939bfc9e8
100 private boolean _needEOC = false; 100 private boolean _needEOC = false;
101 private boolean _bufferChunked = false; 101 private boolean _bufferChunked = false;
102 102
103 103
104 public void shutdown() { 104 public void shutdown() {
105 if (_persistent!=null && !_persistent && _endp!=null && !_endp.isOutputShutdown()) 105 if (_persistent!=null && !_persistent && !_endp.isOutputShutdown())
106 { 106 {
107 try 107 try
108 { 108 {
109 _endp.shutdownOutput(); 109 _endp.shutdownOutput();
110 } 110 }
174 if (_head) 174 if (_head)
175 { 175 {
176 content.clear(); 176 content.clear();
177 _content=null; 177 _content=null;
178 } 178 }
179 else if (_endp != null && (_buffer==null || _buffer.remaining()==0) && _content.remaining() > 0 && (_last || isCommitted() && _content.remaining()>1024)) 179 else if ((_buffer==null || _buffer.remaining()==0) && _content.remaining() > 0 && (_last || isCommitted() && _content.remaining()>1024))
180 { 180 {
181 _bypass = true; 181 _bypass = true;
182 } 182 }
183 else if (!_bufferChunked) 183 else if (!_bufferChunked)
184 { 184 {
225 225
226 // Handle the _content 226 // Handle the _content
227 if (_head) 227 if (_head)
228 return Integer.MAX_VALUE; 228 return Integer.MAX_VALUE;
229 229
230 return _buffer.space()-(_contentLength == HttpTokens.CHUNKED_CONTENT?CHUNK_SPACE:0); 230 return _buffer.space();
231 } 231 }
232 232
233 public boolean isBufferFull() 233 public boolean isBufferFull()
234 { 234 {
235 // Should we flush the buffers? 235 // Should we flush the buffers?
236 return isBufferFull2() || _bufferChunked || _bypass || (_contentLength == HttpTokens.CHUNKED_CONTENT && _buffer != null && _buffer.space() < CHUNK_SPACE); 236 return isBufferFull2() || _bufferChunked || _bypass /* || (_contentLength == HttpTokens.CHUNKED_CONTENT && _buffer != null && _buffer.space() < CHUNK_SPACE)*/;
237 } 237 }
238 238
239 public void send1xx(int code) throws IOException 239 public void send1xx(int code) throws IOException
240 { 240 {
241 if (_state != STATE_HEADER) 241 if (_state != STATE_HEADER)
340 } 340 }
341 } 341 }
342 342
343 if (_status<200 && _status>=100 ) 343 if (_status<200 && _status>=100 )
344 { 344 {
345 _noContent=true; 345 _noContent = true;
346 _content=null; 346 _content=null;
347 if (_buffer!=null) 347 if (_buffer!=null)
348 _buffer.clear(); 348 _buffer.clear();
349 // end the header. 349 // end the header.
350 350
355 return; 355 return;
356 } 356 }
357 } 357 }
358 else if (_status==204 || _status==304) 358 else if (_status==204 || _status==304)
359 { 359 {
360 _noContent=true; 360 _noContent = true;
361 _content=null; 361 _content=null;
362 if (_buffer!=null) 362 if (_buffer!=null)
363 _buffer.clear(); 363 _buffer.clear();
364 } 364 }
365 } 365 }
662 if (_state == STATE_HEADER) 662 if (_state == STATE_HEADER)
663 throw new IllegalStateException("State==HEADER"); 663 throw new IllegalStateException("State==HEADER");
664 664
665 prepareBuffers(); 665 prepareBuffers();
666 666
667 if (_endp == null)
668 {
669 if (_needCRLF && _buffer!=null)
670 _buffer.put(HttpTokens.CRLF);
671 if (_needEOC && _buffer!=null && !_head)
672 _buffer.put(LAST_CHUNK);
673 _needCRLF=false;
674 _needEOC=false;
675 return 0;
676 }
677
678 int total= 0; 667 int total= 0;
679 668
680 int len = -1; 669 int len = -1;
681 int to_flush = flushMask(); 670 int to_flush = flushMask();
682 int last_flush; 671 int last_flush;
683 672
684 do 673 do
685 { 674 {
686 last_flush=to_flush; 675 last_flush = to_flush;
687 switch (to_flush) 676 switch (to_flush)
688 { 677 {
689 case 7: 678 case 7:
690 throw new IllegalStateException(); // should never happen! 679 throw new IllegalStateException(); // should never happen!
691 case 6: 680 case 6:
719 if (_buffer != null) 708 if (_buffer != null)
720 { 709 {
721 _buffer.clear(); 710 _buffer.clear();
722 if (_contentLength == HttpTokens.CHUNKED_CONTENT) 711 if (_contentLength == HttpTokens.CHUNKED_CONTENT)
723 { 712 {
724 // reserve some space for the chunk header
725 _buffer.setPutIndex(CHUNK_SPACE);
726 _buffer.setGetIndex(CHUNK_SPACE);
727
728 // Special case handling for small left over buffer from 713 // Special case handling for small left over buffer from
729 // an addContent that caused a buffer flush. 714 // an addContent that caused a buffer flush.
730 if (_content != null && _content.remaining() < _buffer.space() && _state != STATE_FLUSHING) 715 if (_content != null && _content.remaining() < _buffer.space() && _state != STATE_FLUSHING)
731 { 716 {
732 _buffer.put(_content); 717 _buffer.put(_content);
822 if (size > 0) 807 if (size > 0)
823 { 808 {
824 // Prepare a chunk! 809 // Prepare a chunk!
825 _bufferChunked = true; 810 _bufferChunked = true;
826 811
827 // Did we leave space at the start of the buffer. 812 if (_header == null)
828 //noinspection ConstantConditions 813 _header = _buffers.getHeader();
829 if (_buffer.getIndex() == CHUNK_SPACE) 814
815 if (_needCRLF)
830 { 816 {
831 // Oh yes, goodie! let's use it then! 817 if (_header.remaining() > 0) throw new IllegalStateException("EOC");
832 _buffer.poke(_buffer.getIndex() - 2, HttpTokens.CRLF, 0, 2); 818 _header.put(HttpTokens.CRLF);
833 _buffer.setGetIndex(_buffer.getIndex() - 2); 819 _needCRLF = false;
834 BufferUtil.prependHexInt(_buffer, size);
835
836 if (_needCRLF)
837 {
838 _buffer.poke(_buffer.getIndex() - 2, HttpTokens.CRLF, 0, 2);
839 _buffer.setGetIndex(_buffer.getIndex() - 2);
840 _needCRLF = false;
841 }
842 } 820 }
843 else 821 BufferUtil.putHexInt(_header, size);
844 { 822 _header.put(HttpTokens.CRLF);
845 // No space so lets use a header buffer.
846 if (_header == null)
847 _header = _buffers.getHeader();
848
849 if (_needCRLF)
850 {
851 if (_header.remaining() > 0) throw new IllegalStateException("EOC");
852 _header.put(HttpTokens.CRLF);
853 _needCRLF = false;
854 }
855 BufferUtil.putHexInt(_header, size);
856 _header.put(HttpTokens.CRLF);
857 }
858 823
859 // Add end chunk trailer. 824 // Add end chunk trailer.
860 if (_buffer.space() >= 2) 825 if (_buffer.space() >= 2)
861 _buffer.put(HttpTokens.CRLF); 826 _buffer.put(HttpTokens.CRLF);
862 else 827 else
958 private int _state = STATE_HEADER; 923 private int _state = STATE_HEADER;
959 924
960 private int _status = 0; 925 private int _status = 0;
961 private int _version = HttpVersions.HTTP_1_1_ORDINAL; 926 private int _version = HttpVersions.HTTP_1_1_ORDINAL;
962 private JBuffer _reason; 927 private JBuffer _reason;
963 private String _uri;
964 928
965 private long _contentWritten = 0; 929 private long _contentWritten = 0;
966 private long _contentLength = HttpTokens.UNKNOWN_CONTENT; 930 private long _contentLength = HttpTokens.UNKNOWN_CONTENT;
967 private boolean _last = false; 931 private boolean _last = false;
968 private boolean _head = false; 932 private boolean _head = false;
992 956
993 _last = false; 957 _last = false;
994 _persistent = null; 958 _persistent = null;
995 _contentWritten = 0; 959 _contentWritten = 0;
996 _contentLength = HttpTokens.UNKNOWN_CONTENT; 960 _contentLength = HttpTokens.UNKNOWN_CONTENT;
997 _content=null; 961 _content = null;
998 if (_buffer!=null) 962 if (_buffer!=null)
999 _buffer.clear(); 963 _buffer.clear();
1000 } 964 }
1001 965
1002 /* ------------------------------------------------------------ */ 966 /* ------------------------------------------------------------ */
1031 } 995 }
1032 996
1033 public final void setContentLength(long value) 997 public final void setContentLength(long value)
1034 { 998 {
1035 if (value<0) 999 if (value<0)
1036 _contentLength=HttpTokens.UNKNOWN_CONTENT; 1000 _contentLength = HttpTokens.UNKNOWN_CONTENT;
1037 else 1001 else
1038 _contentLength=value; 1002 _contentLength = value;
1039 } 1003 }
1040 1004
1041 public final void setHead(boolean head) 1005 public final void setHead(boolean head)
1042 { 1006 {
1043 _head = head; 1007 _head = head;
1048 * @return <code>false</code> if the connection should be closed after a request has been read, 1012 * @return <code>false</code> if the connection should be closed after a request has been read,
1049 * <code>true</code> if it should be used for additional requests. 1013 * <code>true</code> if it should be used for additional requests.
1050 */ 1014 */
1051 public final boolean isPersistent() 1015 public final boolean isPersistent()
1052 { 1016 {
1053 return _persistent!=null 1017 return _persistent!=null ? _persistent.booleanValue() : _version>HttpVersions.HTTP_1_0_ORDINAL;
1054 ?_persistent.booleanValue()
1055 :_version>HttpVersions.HTTP_1_0_ORDINAL;
1056 } 1018 }
1057 1019
1058 public final void setPersistent(boolean persistent) 1020 public final void setPersistent(boolean persistent)
1059 { 1021 {
1060 _persistent = persistent; 1022 _persistent = persistent;
1107 if(_buffer!=null) 1069 if(_buffer!=null)
1108 _buffer.clear(); 1070 _buffer.clear();
1109 } 1071 }
1110 else 1072 else
1111 { 1073 {
1112 _contentWritten+=_buffer.remaining(); 1074 _contentWritten += _buffer.remaining();
1113 if (_head) 1075 if (_head)
1114 _buffer.clear(); 1076 _buffer.clear();
1115 } 1077 }
1116 } 1078 }
1117 1079