diff 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
line wrap: on
line diff
--- a/src/org/eclipse/jetty/http/HttpParser.java	Fri Nov 04 01:10:12 2016 -0600
+++ b/src/org/eclipse/jetty/http/HttpParser.java	Fri Nov 04 02:26:54 2016 -0600
@@ -60,6 +60,7 @@
 	public final Buffer _header; // Buffer for header data (and small _content)
 	private final Buffer _body; // Buffer for large content
 	private Buffer _buffer; // The current buffer in use (either _header or _content)
+	private int _mark = -1;
 	private String _cached;
 	private String _tok0 = ""; // Saved token: header name, request method or response version
 	private String _tok1 = ""; // Saved token: header value, request URI or response code
@@ -85,6 +86,33 @@
 		_handler = handler;
 	}
 
+	private void mark() {
+		_mark = _buffer.getIndex() - 1;
+	}
+
+	private Buffer sliceFromMark() {
+		Buffer buf = _buffer.sliceFrom(_mark);
+		_mark = -1;
+		return buf;
+	}
+
+	private void clear() {
+		_buffer.clear();
+		_mark = -1;
+	}
+
+	private void compact() {
+		if( _mark == -1 ) {
+			_buffer.compact();
+		} else if( _mark > 0 ) {
+			int old = _buffer.getIndex();
+			_buffer.setGetIndex(_mark);
+			_buffer.compact();
+			_buffer.setGetIndex( old - _mark );
+			_mark = 0;
+		}
+	}
+
 	public long getContentLength()
 	{
 		return _contentLength;
@@ -277,15 +305,15 @@
 						_cached = null;
 						if (ch > HttpTokens.SPACE || ch<0)
 						{
-							_buffer.mark();
-							_state=STATE_FIELD0;
+							mark();
+							_state = STATE_FIELD0;
 						}
 						break;
 
 					case STATE_FIELD0:
 						if (ch == HttpTokens.SPACE)
 						{
-							_tok0 = _buffer.toString(_buffer.markIndex(), _buffer.getIndex() - 1 - _buffer.markIndex());
+							_tok0 = _buffer.toString(_mark, _buffer.getIndex() - 1 - _mark);
 							_responseStatus = !HttpVersions.CACHE.contains(_tok0)?-1:0;
 							_state=STATE_SPACE1;
 							continue;
@@ -299,7 +327,7 @@
 					case STATE_SPACE1:
 						if (ch > HttpTokens.SPACE || ch<0)
 						{
-							_buffer.mark();
+							mark();
 							if (_responseStatus>=0)
 							{
 								_state = STATE_STATUS;
@@ -317,8 +345,8 @@
 					case STATE_STATUS:
 						if (ch == HttpTokens.SPACE)
 						{
-//							_tok1.update(_buffer.markIndex(), _buffer.getIndex() - 1);
-							_tok1 = _buffer.toString(_buffer.markIndex(), _buffer.getIndex() - 1 - _buffer.markIndex());
+//							_tok1.update(_mark, _buffer.getIndex() - 1);
+							_tok1 = _buffer.toString(_mark, _buffer.getIndex() - 1 - _mark);
 							_state = STATE_SPACE2;
 							continue;
 						}
@@ -344,15 +372,15 @@
 					case STATE_URI:
 						if (ch == HttpTokens.SPACE)
 						{
-//							_tok1.update(_buffer.markIndex(), _buffer.getIndex() - 1);
-							_tok1 = _buffer.toString(_buffer.markIndex(), _buffer.getIndex() - 1 - _buffer.markIndex());
+//							_tok1.update(_mark, _buffer.getIndex() - 1);
+							_tok1 = _buffer.toString(_mark, _buffer.getIndex() - 1 - _mark);
 							_state=STATE_SPACE2;
 							continue;
 						}
 						else if (ch < HttpTokens.SPACE && ch>=0)
 						{
 							// HTTP/0.9
-							_handler.startRequest(_tok0, _buffer.sliceFromMark().toString(), null);
+							_handler.startRequest(_tok0, sliceFromMark().toString(), null);
 							_persistent = false;
 							_state = STATE_SEEKING_EOF;
 							_handler.headerComplete();
@@ -364,7 +392,7 @@
 					case STATE_SPACE2:
 						if (ch > HttpTokens.SPACE || ch<0)
 						{
-							_buffer.mark();
+							mark();
 							_state=STATE_FIELD2;
 						}
 						else if (ch < HttpTokens.SPACE)
@@ -395,10 +423,10 @@
 						{
 							String version;
 							if (_responseStatus > 0)
-//								_handler.startResponse(version=HttpVersions.CACHE.lookup(_tok0), _responseStatus,_buffer.sliceFromMark());
+//								_handler.startResponse(version=HttpVersions.CACHE.lookup(_tok0), _responseStatus,sliceFromMark());
 								version = _tok0;
 							else
-								_handler.startRequest(_tok0, _tok1, version=_buffer.sliceFromMark().toString());
+								_handler.startRequest(_tok0, _tok1, version=sliceFromMark().toString());
 							_eol=ch;
 							_persistent = HttpVersions.CACHE.getOrdinal(version) >= HttpVersions.HTTP_1_1_ORDINAL;
 							_state=STATE_HEADER;
@@ -507,7 +535,7 @@
 									_tok1 = "";
 									_multiLineValue=null;
 								}
-								_buffer.setMarkIndex(-1);
+								_mark = -1;
 
 								// now handle ch
 								if (ch == HttpTokens.CARRIAGE_RETURN || ch == HttpTokens.LINE_FEED)
@@ -566,19 +594,19 @@
 								{
 									// New header
 									_length = 1;
-									_buffer.mark();
+									mark();
 									_state = STATE_HEADER_NAME;
 
 									// try cached name!
 									if (array!=null)
 									{
-										String s = new String(array, _buffer.markIndex(), length+1);
+										String s = new String(array, _mark, length+1);
 										_cached = HttpHeaders.CACHE.getBest(s);
 
 										if (_cached!=null)
 										{
 											_length = _cached.length();
-											_buffer.setGetIndex(_buffer.markIndex()+_length);
+											_buffer.setGetIndex(_mark+_length);
 											length = _buffer.remaining();
 										}
 									}
@@ -594,14 +622,14 @@
 							case HttpTokens.CARRIAGE_RETURN:
 							case HttpTokens.LINE_FEED:
 								if (_length > 0) {
-									_tok0 = _buffer.toString(_buffer.markIndex(), _length);
+									_tok0 = _buffer.toString(_mark, _length);
 								}
 								_eol=ch;
 								_state=STATE_HEADER;
 								break;
 							case HttpTokens.COLON:
 								if (_length > 0 && _cached==null) {
-									_tok0 = _buffer.toString(_buffer.markIndex(), _length);
+									_tok0 = _buffer.toString(_mark, _length);
 								}
 								_length=-1;
 								_state=STATE_HEADER_VALUE;
@@ -613,8 +641,8 @@
 							{
 								_cached = null;
 								if (_length == -1)
-									_buffer.mark();
-								_length=_buffer.getIndex() - _buffer.markIndex();
+									mark();
+								_length=_buffer.getIndex() - _mark;
 								_state = STATE_HEADER_IN_NAME;
 							}
 						}
@@ -627,14 +655,14 @@
 							case HttpTokens.CARRIAGE_RETURN:
 							case HttpTokens.LINE_FEED:
 								if (_length > 0) {
-									_tok0 = _buffer.toString(_buffer.markIndex(),_length);
+									_tok0 = _buffer.toString(_mark,_length);
 								}
 								_eol=ch;
 								_state=STATE_HEADER;
 								break;
 							case HttpTokens.COLON:
 								if (_length > 0 && _cached==null) {
-									_tok0 = _buffer.toString(_buffer.markIndex(),_length);
+									_tok0 = _buffer.toString(_mark,_length);
 								}
 								_length=-1;
 								_state=STATE_HEADER_VALUE;
@@ -659,14 +687,14 @@
 								if (_length > 0)
 								{
 									if (_tok1.length() == 0)
-//										_tok1.update(_buffer.markIndex(), _buffer.markIndex() + _length);
-										_tok1 = _buffer.toString(_buffer.markIndex(), _length);
+//										_tok1.update(_mark, _mark + _length);
+										_tok1 = _buffer.toString(_mark, _length);
 									else
 									{
 										// Continuation line!
 										if (_multiLineValue == null) _multiLineValue = _tok1;
-//										_tok1.update(_buffer.markIndex(), _buffer.markIndex() + _length);
-										_tok1 = _buffer.toString(_buffer.markIndex(), _length);
+//										_tok1.update(_mark, _mark + _length);
+										_tok1 = _buffer.toString(_mark, _length);
 										_multiLineValue += " " + _tok1;
 									}
 								}
@@ -679,8 +707,8 @@
 							default:
 							{
 								if (_length == -1)
-									_buffer.mark();
-								_length=_buffer.getIndex() - _buffer.markIndex();
+									mark();
+								_length=_buffer.getIndex() - _mark;
 								_state = STATE_HEADER_IN_VALUE;
 							}
 						}
@@ -694,14 +722,14 @@
 								if (_length > 0)
 								{
 									if (_tok1.length() == 0)
-//										_tok1.update(_buffer.markIndex(), _buffer.markIndex() + _length);
-										_tok1 = _buffer.toString(_buffer.markIndex(), _length);
+//										_tok1.update(_mark, _mark + _length);
+										_tok1 = _buffer.toString(_mark, _length);
 									else
 									{
 										// Continuation line!
 										if (_multiLineValue == null) _multiLineValue = _tok1;
-//										_tok1.update(_buffer.markIndex(), _buffer.markIndex() + _length);
-										_tok1 = _buffer.toString(_buffer.markIndex(), _length);
+//										_tok1.update(_mark, _mark + _length);
+										_tok1 = _buffer.toString(_mark, _length);
 										_multiLineValue += " " + _tok1;
 									}
 								}
@@ -894,11 +922,11 @@
 								{
 									_state = STATE_END;
 									_endp.close();
-									_buffer.clear();
+									clear();
 								}
 						}
 						
-						_buffer.clear();
+						clear();
 						break;
 					}
 				}
@@ -942,14 +970,14 @@
 		// Shall we compact the body?
 		if (_buffer==_body || _state>STATE_END)
 		{
-			_buffer.compact();
+			compact();
 		}
 
 		// Are we full?
 		if (_buffer.space() == 0)
 		{
 			LOG.warn("HttpParser Full for {} ",_endp);
-			_buffer.clear();
+			clear();
 			throw new HttpException(HttpStatus.REQUEST_ENTITY_TOO_LARGE_413, "Request Entity Too Large: "+(_buffer==_body?"body":"head"));
 		}
 /* why?