Mercurial Hosting > luan
diff src/org/eclipse/jetty/server/Response.java @ 972:5ee36654b383
simplify AbstractHttpConnection
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 15 Oct 2016 22:42:05 -0600 |
parents | cab5830e1ab0 |
children | d35b0a3a7a4a |
line wrap: on
line diff
--- a/src/org/eclipse/jetty/server/Response.java Fri Oct 14 13:06:06 2016 -0600 +++ b/src/org/eclipse/jetty/server/Response.java Sat Oct 15 22:42:05 2016 -0600 @@ -82,7 +82,6 @@ public final static String HTTP_ONLY_COMMENT="__HTTP_ONLY__"; - /* ------------------------------------------------------------ */ public static Response getResponse(HttpServletResponse response) { if (response instanceof Response) @@ -137,7 +136,7 @@ */ public void addCookie(HttpCookie cookie) { - _connection.getResponseFields().addSetCookie(cookie); + _connection._responseFields.addSetCookie(cookie); } /* ------------------------------------------------------------ */ @@ -160,7 +159,7 @@ comment=null; } } - _connection.getResponseFields().addSetCookie(cookie.getName(), + _connection._responseFields.addSetCookie(cookie.getName(), cookie.getValue(), cookie.getDomain(), cookie.getPath(), @@ -177,7 +176,7 @@ */ public boolean containsHeader(String name) { - return _connection.getResponseFields().containsKey(name); + return _connection._responseFields.containsKey(name); } /* ------------------------------------------------------------ */ @@ -218,9 +217,6 @@ */ public void sendError(int code, String message) throws IOException { - if (_connection.isIncluding()) - return; - if (isCommitted()) LOG.warn("Committed before "+code+" "+message); @@ -290,8 +286,8 @@ } else if (code!=SC_PARTIAL_CONTENT) { - _connection.getRequestFields().remove(HttpHeaders.CONTENT_TYPE_BUFFER); - _connection.getRequestFields().remove(HttpHeaders.CONTENT_LENGTH_BUFFER); + _connection._requestFields.remove(HttpHeaders.CONTENT_TYPE_BUFFER); + _connection._requestFields.remove(HttpHeaders.CONTENT_LENGTH_BUFFER); _characterEncoding=null; _mimeType=null; _cachedMimeType=null; @@ -323,7 +319,7 @@ public void sendProcessing() throws IOException { if (_connection.isExpecting102Processing() && !isCommitted()) - ((HttpGenerator)_connection.getGenerator()).send1xx(HttpStatus.PROCESSING_102); + _connection._generator.send1xx(HttpStatus.PROCESSING_102); } /* ------------------------------------------------------------ */ @@ -332,9 +328,6 @@ */ public void sendRedirect(String location) throws IOException { - if (_connection.isIncluding()) - return; - if (location==null) throw new IllegalArgumentException(); @@ -400,8 +393,7 @@ */ public void setDateHeader(String name, long date) { - if (!_connection.isIncluding()) - _connection.getResponseFields().putDateField(name, date); + _connection._responseFields.putDateField(name, date); } /* ------------------------------------------------------------ */ @@ -410,8 +402,7 @@ */ public void addDateHeader(String name, long date) { - if (!_connection.isIncluding()) - _connection.getResponseFields().addDateField(name, date); + _connection._responseFields.addDateField(name, date); } /* ------------------------------------------------------------ */ @@ -424,14 +415,7 @@ setContentType(value); else { - if (_connection.isIncluding()) - { - if (name.startsWith(SET_INCLUDE_HEADER_PREFIX)) - name=name.substring(SET_INCLUDE_HEADER_PREFIX.length()); - else - return; - } - _connection.getResponseFields().put(name, value); + _connection._responseFields.put(name, value); if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) { if (value==null) @@ -446,7 +430,7 @@ /* ------------------------------------------------------------ */ public Collection<String> getHeaderNames() { - final HttpFields fields=_connection.getResponseFields(); + final HttpFields fields=_connection._responseFields; return fields.getFieldNamesCollection(); } @@ -455,7 +439,7 @@ */ public String getHeader(String name) { - return _connection.getResponseFields().getStringField(name); + return _connection._responseFields.getStringField(name); } /* ------------------------------------------------------------ */ @@ -463,7 +447,7 @@ */ public Collection<String> getHeaders(String name) { - final HttpFields fields=_connection.getResponseFields(); + final HttpFields fields=_connection._responseFields; Collection<String> i = fields.getValuesCollection(name); if (i==null) return Collections.EMPTY_LIST; @@ -477,21 +461,13 @@ public void addHeader(String name, String value) { - if (_connection.isIncluding()) - { - if (name.startsWith(SET_INCLUDE_HEADER_PREFIX)) - name=name.substring(SET_INCLUDE_HEADER_PREFIX.length()); - else - return; - } - if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) { setContentType(value); return; } - _connection.getResponseFields().add(name, value); + _connection._responseFields.add(name, value); if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) _connection._generator.setContentLength(Long.parseLong(value)); } @@ -502,12 +478,9 @@ */ public void setIntHeader(String name, int value) { - if (!_connection.isIncluding()) - { - _connection.getResponseFields().putLongField(name, value); - if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) - _connection._generator.setContentLength(value); - } + _connection._responseFields.putLongField(name, value); + if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) + _connection._generator.setContentLength(value); } /* ------------------------------------------------------------ */ @@ -516,12 +489,9 @@ */ public void addIntHeader(String name, int value) { - if (!_connection.isIncluding()) - { - _connection.getResponseFields().addLongField(name, value); - if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) - _connection._generator.setContentLength(value); - } + _connection._responseFields.addLongField(name, value); + if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) + _connection._generator.setContentLength(value); } /* ------------------------------------------------------------ */ @@ -541,11 +511,8 @@ { if (sc<=0) throw new IllegalArgumentException(); - if (!_connection.isIncluding()) - { - _status=sc; - _reason=sm; - } + _status=sc; + _reason=sm; } /* ------------------------------------------------------------ */ @@ -640,9 +607,6 @@ */ public void setCharacterEncoding(String encoding) { - if (_connection.isIncluding()) - return; - if (this._outputState==0 && !isCommitted()) { _explicitEncoding=true; @@ -661,9 +625,9 @@ _contentType=null; if (_contentType==null) - _connection.getResponseFields().remove(HttpHeaders.CONTENT_TYPE_BUFFER); + _connection._responseFields.remove(HttpHeaders.CONTENT_TYPE_BUFFER); else - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } else @@ -682,14 +646,14 @@ if (content_type!=null) { _contentType=content_type.toString(); - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type); } } if (_contentType==null) { _contentType = _mimeType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } else @@ -708,7 +672,7 @@ else _contentType=_contentType.substring(0,i8)+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= ")+_contentType.substring(i2); } - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } } @@ -724,12 +688,12 @@ // Protect from setting after committed as default handling // of a servlet HEAD request ALWAYS sets _content length, even // if the getHandling committed the response! - if (isCommitted() || _connection.isIncluding()) + if (isCommitted()) return; _connection._generator.setContentLength(len); if (len>0) { - _connection.getResponseFields().putLongField(HttpHeaders.CONTENT_LENGTH, len); + _connection._responseFields.putLongField(HttpHeaders.CONTENT_LENGTH, len); if (_connection._generator.isAllContentWritten()) { if (_outputState==WRITER) @@ -758,10 +722,10 @@ // Protect from setting after committed as default handling // of a servlet HEAD request ALWAYS sets _content length, even // if the getHandling committed the response! - if (isCommitted() || _connection.isIncluding()) + if (isCommitted()) return; _connection._generator.setContentLength(len); - _connection.getResponseFields().putLongField(HttpHeaders.CONTENT_LENGTH, len); + _connection._responseFields.putLongField(HttpHeaders.CONTENT_LENGTH, len); } /* ------------------------------------------------------------ */ @@ -770,7 +734,7 @@ */ public void setContentType(String contentType) { - if (isCommitted() || _connection.isIncluding()) + if (isCommitted()) return; // Yes this method is horribly complex.... but there are lots of special cases and @@ -784,7 +748,7 @@ _mimeType=null; _cachedMimeType=null; _contentType=null; - _connection.getResponseFields().remove(HttpHeaders.CONTENT_TYPE_BUFFER); + _connection._responseFields.remove(HttpHeaders.CONTENT_TYPE_BUFFER); } else { @@ -818,29 +782,29 @@ if (content_type!=null) { _contentType=content_type.toString(); - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type); } else { _contentType=_mimeType+";charset="+_characterEncoding; - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } else { _contentType=_mimeType+";charset="+_characterEncoding; - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } else if (i2<0) { _contentType=contentType.substring(0,i1)+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } else { _contentType=contentType.substring(0,i1)+contentType.substring(i2)+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } else if ((i1==i0+1 && i2<0) || (i1==i0+2 && i2<0 && contentType.charAt(i0+1)==' ')) @@ -855,38 +819,38 @@ if (content_type!=null) { _contentType=content_type.toString(); - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type); } else { _contentType=contentType; - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } else { _contentType=contentType; - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } else if (i2>0) { _characterEncoding = QuotedStringTokenizer.unquote(contentType.substring(i8,i2)); _contentType=contentType; - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } else { _characterEncoding = QuotedStringTokenizer.unquote(contentType.substring(i8)); _contentType=contentType; - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } else // No encoding in the params. { _cachedMimeType=null; _contentType=_characterEncoding==null?contentType:contentType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } else // No params at all @@ -902,29 +866,29 @@ if (content_type!=null) { _contentType=content_type.toString(); - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type); } else { _contentType=_mimeType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } else { _contentType=contentType+";charset="+QuotedStringTokenizer.quoteIfNeeded(_characterEncoding,";= "); - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } else if (_cachedMimeType!=null) { _contentType=_cachedMimeType.toString(); - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_cachedMimeType); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_cachedMimeType); } else { _contentType=contentType; - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } } @@ -938,7 +902,7 @@ { if (isCommitted() || getContentCount()>0) throw new IllegalStateException("Committed or content written"); - _connection.getGenerator().increaseContentBufferSize(size); + _connection._generator.increaseContentBufferSize(size); } /* ------------------------------------------------------------ */ @@ -947,7 +911,7 @@ */ public int getBufferSize() { - return _connection.getGenerator().getContentBufferSize(); + return _connection._generator.getContentBufferSize(); } /* ------------------------------------------------------------ */ @@ -970,10 +934,10 @@ _status=200; _reason=null; - HttpFields response_fields=_connection.getResponseFields(); + HttpFields response_fields=_connection._responseFields; response_fields.clear(); - String connection=_connection.getRequestFields().getStringField(HttpHeaders.CONNECTION_BUFFER); + String connection=_connection._requestFields.getStringField(HttpHeaders.CONNECTION_BUFFER); if (connection!=null) { String[] values = connection.split(","); @@ -1009,7 +973,7 @@ reset(); else { - HttpFields response_fields=_connection.getResponseFields(); + HttpFields response_fields=_connection._responseFields; ArrayList<String> cookieValues = new ArrayList<String>(5); Enumeration<String> vals = response_fields.getValues(HttpHeaders.SET_COOKIE); @@ -1045,7 +1009,7 @@ { if (isCommitted()) throw new IllegalStateException("Committed"); - _connection.getGenerator().resetBuffer(); + _connection._generator.resetBuffer(); } /* ------------------------------------------------------------ */ @@ -1054,7 +1018,7 @@ */ public boolean isCommitted() { - return _connection.isResponseCommitted(); + return _connection._generator.isCommitted(); } @@ -1064,11 +1028,11 @@ */ public void setLocale(Locale locale) { - if (locale == null || isCommitted() ||_connection.isIncluding()) + if (locale == null || isCommitted()) return; _locale = locale; - _connection.getResponseFields().put(HttpHeaders.CONTENT_LANGUAGE_BUFFER,locale.toString().replace('_','-')); + _connection._responseFields.put(HttpHeaders.CONTENT_LANGUAGE_BUFFER,locale.toString().replace('_','-')); if (_explicitEncoding || _outputState!=0 ) return; @@ -1100,7 +1064,7 @@ } _cachedMimeType=MimeTypes.CACHE.get(_mimeType); - _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); + _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } } @@ -1151,15 +1115,15 @@ */ public long getContentCount() { - if (_connection==null || _connection.getGenerator()==null) + if (_connection==null) return -1; - return _connection.getGenerator().getContentWritten(); + return _connection._generator.getContentWritten(); } /* ------------------------------------------------------------ */ public HttpFields getHttpFields() { - return _connection.getResponseFields(); + return _connection._responseFields; } /* ------------------------------------------------------------ */ @@ -1167,7 +1131,7 @@ public String toString() { return "HTTP/1.1 "+_status+" "+ (_reason==null?"":_reason) +System.getProperty("line.separator")+ - _connection.getResponseFields().toString(); + _connection._responseFields.toString(); } /* ------------------------------------------------------------ */