Mercurial Hosting > luan
changeset 1066:bbbda7c6e8ec
fix use of HttpGenerator._header
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 09 Nov 2016 05:48:10 -0700 |
parents | 158d1e6ac17f |
children | 56b515be91e1 |
files | src/org/eclipse/jetty/http/HttpFields.java src/org/eclipse/jetty/http/HttpGenerator.java src/org/eclipse/jetty/io/BufferUtil.java src/org/eclipse/jetty/io/JBuffer.java |
diffstat | 4 files changed, 122 insertions(+), 119 deletions(-) [+] |
line wrap: on
line diff
--- a/src/org/eclipse/jetty/http/HttpFields.java Wed Nov 09 04:36:05 2016 -0700 +++ b/src/org/eclipse/jetty/http/HttpFields.java Wed Nov 09 05:48:10 2016 -0700 @@ -947,7 +947,7 @@ { byte[] nameBytes = StringUtil.getBytes(_name); if (getNameOrdinal() >=0 ) - buffer.put(nameBytes); + buffer.putQ(nameBytes); else { for( byte b : nameBytes ) { @@ -958,17 +958,17 @@ case ':' : continue; default: - buffer.put(b); + buffer.putQ(b); } } } - buffer.put((byte) ':'); - buffer.put((byte) ' '); + buffer.putQ((byte) ':'); + buffer.putQ((byte) ' '); byte[] valueBytes = StringUtil.getBytes(_value); if (getValueOrdinal() >= 0) - buffer.put(valueBytes); + buffer.putQ(valueBytes); else { for( byte b : valueBytes ) { @@ -978,12 +978,13 @@ case '\n': continue; default: - buffer.put(b); + buffer.putQ(b); } } } - BufferUtil.putCRLF(buffer); + buffer.putQ((byte)'\r'); + buffer.putQ((byte)'\n'); } private String getName()
--- a/src/org/eclipse/jetty/http/HttpGenerator.java Wed Nov 09 04:36:05 2016 -0700 +++ b/src/org/eclipse/jetty/http/HttpGenerator.java Wed Nov 09 05:48:10 2016 -0700 @@ -150,19 +150,23 @@ if (_bufferChunked) { JBuffer nc = _buffers.getBuffer(_content.remaining()+CHUNK_SPACE+content.remaining()); - nc.put(_content); - nc.put(HttpTokens.CRLF); + nc.clear(); + nc.putQ(_content); + nc.putQ(HttpTokens.CRLF); BufferUtil.putHexInt(nc, content.remaining()); - nc.put(HttpTokens.CRLF); - nc.put(content); - content=nc; + nc.putQ(HttpTokens.CRLF); + nc.putQ(content); + nc.flip(); + content = nc; } else { JBuffer nc = _buffers.getBuffer(_content.remaining()+content.remaining()); - nc.put(_content); - nc.put(content); - content=nc; + nc.clear(); + nc.putQ(_content); + nc.putQ(content); + nc.flip(); + content = nc; } } } @@ -245,16 +249,13 @@ if (status==null) throw new IllegalArgumentException(code+"?"); - // get a header buffer - if (_header == null) - _header = _buffers.getHeader(); - - _header.put(status._responseLine); - _header.put(HttpTokens.CRLF); + _header.putQ(status._responseLine); + _header.putQ(HttpTokens.CRLF); try { // nasty semi busy flush! + _header.flip(); while(_header.remaining()>0) { int len = _endp.flush(_header); @@ -284,10 +285,6 @@ throw new IllegalStateException("last?"); _last = _last | allContentAdded; - // get a header buffer - if (_header == null) - _header = _buffers.getHeader(); - boolean has_server = false; try @@ -310,31 +307,31 @@ if (status==null) { - _header.put(HttpVersions.HTTP_1_1_BYTES); - _header.put((byte) ' '); - _header.put((byte) ('0' + _status / 100)); - _header.put((byte) ('0' + (_status % 100) / 10)); - _header.put((byte) ('0' + (_status % 10))); - _header.put((byte) ' '); + _header.putQ(HttpVersions.HTTP_1_1_BYTES); + _header.putQ((byte) ' '); + _header.putQ((byte) ('0' + _status / 100)); + _header.putQ((byte) ('0' + (_status % 100) / 10)); + _header.putQ((byte) ('0' + (_status % 10))); + _header.putQ((byte) ' '); if (_reason==null) { - _header.put((byte) ('0' + _status / 100)); - _header.put((byte) ('0' + (_status % 100) / 10)); - _header.put((byte) ('0' + (_status % 10))); + _header.putQ((byte) ('0' + _status / 100)); + _header.putQ((byte) ('0' + (_status % 100) / 10)); + _header.putQ((byte) ('0' + (_status % 10))); } else - _header.put(_reason); - _header.put(HttpTokens.CRLF); + _header.putQ(_reason); + _header.putQ(HttpTokens.CRLF); } else { if (_reason==null) - _header.put(status._responseLine); + _header.putQ(status._responseLine); else { - _header.put(status._schemeCode); - _header.put(_reason); - _header.put(HttpTokens.CRLF); + _header.putQ(status._schemeCode); + _header.putQ(_reason); + _header.putQ(HttpTokens.CRLF); } } @@ -348,7 +345,7 @@ if (_status!=101 ) { - _header.put(HttpTokens.CRLF); + _header.putQ(HttpTokens.CRLF); _state = STATE_CONTENT; return; } @@ -532,11 +529,11 @@ if (content_length == null && !_noContent) { // known length but not actually set. - _header.put(HttpHeaders.CONTENT_LENGTH_BYTES); - _header.put(HttpTokens.COLON); - _header.put((byte) ' '); + _header.putQ(HttpHeaders.CONTENT_LENGTH_BYTES); + _header.putQ(HttpTokens.COLON); + _header.putQ((byte) ' '); BufferUtil.putDecLong(_header, _contentLength); - _header.put(HttpTokens.CRLF); + _header.putQ(HttpTokens.CRLF); } } else @@ -548,7 +545,7 @@ case HttpTokens.NO_CONTENT: if (content_length == null && _status >= 200 && _status != 204 && _status != 304) - _header.put(CONTENT_LENGTH_0); + _header.putQ(CONTENT_LENGTH_0); break; case HttpTokens.EOF_CONTENT: @@ -576,7 +573,7 @@ throw new IllegalArgumentException("BAD TE"); } else - _header.put(TRANSFER_ENCODING_CHUNKED); + _header.putQ(TRANSFER_ENCODING_CHUNKED); } // Handle connection if need be @@ -588,44 +585,44 @@ if (!_persistent && (close || _version > HttpVersions.HTTP_1_0_ORDINAL)) { - _header.put(CONNECTION_CLOSE); + _header.putQ(CONNECTION_CLOSE); if (connection!=null) { - _header.setPutIndex(_header.putIndex()-2); - _header.put((byte)','); - _header.put(connection.toString().getBytes()); - _header.put(CRLF); + _header.position(_header.position()-2); + _header.putQ((byte)','); + _header.putQ(connection.toString().getBytes()); + _header.putQ(CRLF); } } else if (keep_alive) { - _header.put(CONNECTION_KEEP_ALIVE); + _header.putQ(CONNECTION_KEEP_ALIVE); if (connection!=null) { - _header.setPutIndex(_header.putIndex()-2); - _header.put((byte)','); - _header.put(connection.toString().getBytes()); - _header.put(CRLF); + _header.position(_header.position()-2); + _header.putQ((byte)','); + _header.putQ(connection.toString().getBytes()); + _header.putQ(CRLF); } } else if (connection!=null) { - _header.put(CONNECTION_); - _header.put(connection.toString().getBytes()); - _header.put(CRLF); + _header.putQ(CONNECTION_); + _header.putQ(connection.toString().getBytes()); + _header.putQ(CRLF); } if (!has_server && _status>199) - _header.put(SERVER); + _header.putQ(SERVER); // end the header. - _header.put(HttpTokens.CRLF); + _header.putQ(HttpTokens.CRLF); _state = STATE_CONTENT; } catch(ArrayIndexOutOfBoundsException e) { - throw new RuntimeException("Header>"+_header.capacity(),e); + throw new RuntimeException("Header>"+_header.remaining(),e); } } @@ -673,16 +670,23 @@ last_flush = to_flush; switch (to_flush) { +//qqq case 7: throw new IllegalStateException(); // should never happen! case 6: + _header.flip(); len = _endp.flush(_header, _buffer, null); + _header.compact(); break; case 5: + _header.flip(); len = _endp.flush(_header, _content, null); + _header.compact(); break; case 4: + _header.flip(); len = _endp.flush(_header); + _header.compact(); break; case 3: len = _endp.flush(_buffer, _content, null); @@ -697,8 +701,7 @@ { len=0; // Nothing more we can write now. - if (_header != null) - _header.clearJ(); + _header.clear(); _bypass = false; _bufferChunked = false; @@ -754,7 +757,7 @@ private int flushMask() { - return ((_header != null && _header.remaining() > 0)?4:0) + return ((_header.position() > 0)?4:0) | ((_buffer != null && _buffer.remaining() > 0)?2:0) | ((_bypass && _content != null && _content.remaining() > 0)?1:0); } @@ -782,19 +785,16 @@ int size = _content.remaining(); _bufferChunked = true; - if (_header == null) - _header = _buffers.getHeader(); - // if we need CRLF add this to header if (_needCRLF) { - if (_header.remaining() > 0) throw new IllegalStateException("EOC"); - _header.put(HttpTokens.CRLF); + if (_header.position() > 0) throw new IllegalStateException("EOC"); + _header.putQ(HttpTokens.CRLF); _needCRLF = false; } // Add the chunk size to the header BufferUtil.putHexInt(_header, size); - _header.put(HttpTokens.CRLF); + _header.putQ(HttpTokens.CRLF); // Need a CRLF after the content _needCRLF = true; @@ -807,17 +807,14 @@ // Prepare a chunk! _bufferChunked = true; - if (_header == null) - _header = _buffers.getHeader(); - if (_needCRLF) { - if (_header.remaining() > 0) throw new IllegalStateException("EOC"); - _header.put(HttpTokens.CRLF); + if (_header.position() > 0) throw new IllegalStateException("EOC"); + _header.putQ(HttpTokens.CRLF); _needCRLF = false; } BufferUtil.putHexInt(_header, size); - _header.put(HttpTokens.CRLF); + _header.putQ(HttpTokens.CRLF); // Add end chunk trailer. if (_buffer.space() >= 2) @@ -830,14 +827,11 @@ // If we need EOC and everything written if (_needEOC && (_content == null || _content.remaining() == 0)) { - if (_header == null && _buffer == null) - _header = _buffers.getHeader(); - if (_needCRLF) { - if (_buffer == null && _header != null && _header.space() >= HttpTokens.CRLF.length) + if (_buffer == null && _header.remaining() >= HttpTokens.CRLF.length) { - _header.put(HttpTokens.CRLF); + _header.putQ(HttpTokens.CRLF); _needCRLF = false; } else if (_buffer!=null && _buffer.space() >= HttpTokens.CRLF.length) @@ -849,11 +843,11 @@ if (!_needCRLF && _needEOC) { - if (_buffer == null && _header != null && _header.space() >= LAST_CHUNK.length) + if (_buffer == null && _header.remaining() >= LAST_CHUNK.length) { if (!_head) { - _header.put(LAST_CHUNK); + _header.putQ(LAST_CHUNK); _bufferChunked=true; } _needEOC = false; @@ -880,13 +874,13 @@ @Override public String toString() { - JBuffer header=_header; + JBuffer header = _header; JBuffer buffer=_buffer; JBuffer content=_content; return String.format("%s{s=%d,h=%d,b=%d,c=%d}", getClass().getSimpleName(), _state, - header == null ? -1 : header.remaining(), + header == null ? -1 : header.position(), buffer == null ? -1 : buffer.remaining(), content == null ? -1 : content.remaining()); } @@ -931,7 +925,7 @@ private boolean _noContent = false; private Boolean _persistent = null; - private JBuffer _header; // JBuffer for HTTP header (and maybe small _content) + private final JBuffer _header; // JBuffer for HTTP header (and maybe small _content) private JBuffer _buffer; // JBuffer for copy of passed _content private JBuffer _content; // JBuffer passed to addContent @@ -940,6 +934,8 @@ { this._buffers = buffers; this._endp = io; + _header = _buffers.getHeader(); + _header.clear(); } public final boolean isOpen()
--- a/src/org/eclipse/jetty/io/BufferUtil.java Wed Nov 09 04:36:05 2016 -0700 +++ b/src/org/eclipse/jetty/io/BufferUtil.java Wed Nov 09 05:48:10 2016 -0700 @@ -75,18 +75,18 @@ if (n < 0) { - buffer.put((byte)'-'); + buffer.putQ((byte)'-'); if (n == Integer.MIN_VALUE) { - buffer.put((byte)(0x7f&'8')); - buffer.put((byte)(0x7f&'0')); - buffer.put((byte)(0x7f&'0')); - buffer.put((byte)(0x7f&'0')); - buffer.put((byte)(0x7f&'0')); - buffer.put((byte)(0x7f&'0')); - buffer.put((byte)(0x7f&'0')); - buffer.put((byte)(0x7f&'0')); + buffer.putQ((byte)(0x7f&'8')); + buffer.putQ((byte)(0x7f&'0')); + buffer.putQ((byte)(0x7f&'0')); + buffer.putQ((byte)(0x7f&'0')); + buffer.putQ((byte)(0x7f&'0')); + buffer.putQ((byte)(0x7f&'0')); + buffer.putQ((byte)(0x7f&'0')); + buffer.putQ((byte)(0x7f&'0')); return; } @@ -95,7 +95,7 @@ if (n < 0x10) { - buffer.put(DIGIT[n]); + buffer.putQ(DIGIT[n]); } else { @@ -106,13 +106,13 @@ if (n < hexDivisors[i]) { if (started) - buffer.put((byte)'0'); + buffer.putQ((byte)'0'); continue; } started= true; int d = n / hexDivisors[i]; - buffer.put(DIGIT[d]); + buffer.putQ(DIGIT[d]); n= n - d * hexDivisors[i]; } } @@ -122,11 +122,11 @@ { if (n < 0) { - buffer.put((byte)'-'); + buffer.putQ((byte)'-'); if (n == Long.MIN_VALUE) { - buffer.put((byte)'9'); + buffer.putQ((byte)'9'); n = 223372036854775808L; } else @@ -135,7 +135,7 @@ if (n < 10) { - buffer.put(DIGIT[(int)n]); + buffer.putQ(DIGIT[(int)n]); } else { @@ -146,13 +146,13 @@ if (n < decDivisorsL[i]) { if (started) - buffer.put((byte)'0'); + buffer.putQ((byte)'0'); continue; } started= true; long d= n / decDivisorsL[i]; - buffer.put(DIGIT[(int)d]); + buffer.putQ(DIGIT[(int)d]); n= n - d * decDivisorsL[i]; } } @@ -194,12 +194,6 @@ }; - public static void putCRLF(JBuffer buffer) - { - buffer.put((byte)13); - buffer.put((byte)10); - } - public static final JBuffer EMPTY_BUFFER = new JBuffer(ByteBuffer.allocate(0));
--- a/src/org/eclipse/jetty/io/JBuffer.java Wed Nov 09 04:36:05 2016 -0700 +++ b/src/org/eclipse/jetty/io/JBuffer.java Wed Nov 09 05:48:10 2016 -0700 @@ -81,6 +81,10 @@ return bb; } + public void clear() { + bb.clear(); + } + public void clearJ() { bb.position(0); bb.limit(0); @@ -96,10 +100,18 @@ } + public void putQ(JBuffer src) { + bb.put(src.bb); + } + public int put(JBuffer src) { return put(src.asArray()); } + public void putQ(byte b) { + bb.put(b); + } + public void put(byte b) { ByteBuffer dup = bb.duplicate(); @@ -122,16 +134,12 @@ return length; } - public int put(byte[] b) { - return put(b,0,b.length); + public void putQ(byte[] b) { + bb.put(b); } - public final int putIndex() { - return bb.limit(); - } - - public void setPutIndex(int putIndex) { - bb.limit(putIndex); + public int put(byte[] b) { + return put(b,0,b.length); } public void skip(int n) { @@ -157,4 +165,8 @@ return bb.get(index); } + public void flip() { + bb.flip(); + } + }