📄 response.java
字号:
if (contentType==null) { if (_locale==null) _characterEncoding=null; _mimeType=null; _cachedMimeType=null; _contentType=null; _connection.getResponseFields().remove(HttpHeaders.CONTENT_TYPE_BUFFER); } else { // Look for encoding in contentType int i0=contentType.indexOf(';'); if (i0>0) { // we have content type parameters // Extract params off mimetype _mimeType=contentType.substring(0,i0).trim(); _cachedMimeType=MimeTypes.CACHE.get(_mimeType); // Look for charset int i1=contentType.indexOf("charset=",i0+1); if (i1>=0) { _explicitEncoding=true; int i8=i1+8; int i2 = contentType.indexOf(' ',i8); if (_outputState==WRITER) { // strip the charset and ignore; if ((i1==i0+1 && i2<0) || (i1==i0+2 && i2<0 && contentType.charAt(i0+1)==' ')) { if (_cachedMimeType!=null) { CachedBuffer content_type = _cachedMimeType.getAssociate(_characterEncoding); if (content_type!=null) { _contentType=content_type.toString(); _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type); } else { _contentType=_mimeType+"; charset="+_characterEncoding; _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } else { _contentType=_mimeType+"; charset="+_characterEncoding; _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } else if (i2<0) { _contentType=contentType.substring(0,i1)+" charset="+QuotedStringTokenizer.quote(_characterEncoding,";= "); _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } else { _contentType=contentType.substring(0,i1)+contentType.substring(i2)+" charset="+QuotedStringTokenizer.quote(_characterEncoding,";= "); _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } else if ((i1==i0+1 && i2<0) || (i1==i0+2 && i2<0 && contentType.charAt(i0+1)==' ')) { // The params are just the char encoding _cachedMimeType=MimeTypes.CACHE.get(_mimeType); _characterEncoding = QuotedStringTokenizer.unquote(contentType.substring(i8)); if (_cachedMimeType!=null) { CachedBuffer content_type = _cachedMimeType.getAssociate(_characterEncoding); if (content_type!=null) { _contentType=content_type.toString(); _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type); } else { _contentType=contentType; _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } else { _contentType=contentType; _connection.getResponseFields().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); } else { _characterEncoding = QuotedStringTokenizer.unquote(contentType.substring(i8)); _contentType=contentType; _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } else // No encoding in the params. { _cachedMimeType=null; _contentType=_characterEncoding==null?contentType:contentType+" charset="+QuotedStringTokenizer.quote(_characterEncoding,";= "); _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } else // No params at all { _mimeType=contentType; _cachedMimeType=MimeTypes.CACHE.get(_mimeType); if (_characterEncoding!=null) { if (_cachedMimeType!=null) { CachedBuffer content_type = _cachedMimeType.getAssociate(_characterEncoding); if (content_type!=null) { _contentType=content_type.toString(); _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,content_type); } else { _contentType=_mimeType+"; charset="+QuotedStringTokenizer.quote(_characterEncoding,";= "); _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } else { _contentType=contentType+"; charset="+QuotedStringTokenizer.quote(_characterEncoding,";= "); _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } else if (_cachedMimeType!=null) { _contentType=_cachedMimeType.toString(); _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_cachedMimeType); } else { _contentType=contentType; _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } } } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletResponse#setBufferSize(int) */ public void setBufferSize(int size) { if (isCommitted() || getContentCount()>0) throw new IllegalStateException("Committed or content written"); _connection.getGenerator().increaseContentBufferSize(size); } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletResponse#getBufferSize() */ public int getBufferSize() { return _connection.getGenerator().getContentBufferSize(); } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletResponse#flushBuffer() */ public void flushBuffer() throws IOException { _connection.flushResponse(); } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletResponse#reset() */ public void reset() { resetBuffer(); HttpFields response_fields=_connection.getResponseFields(); response_fields.clear(); String connection=_connection.getRequestFields().getStringField(HttpHeaders.CONNECTION_BUFFER); if (connection!=null) { String[] values = connection.split(","); for (int i=0;values!=null && i<values.length;i++) { CachedBuffer cb = HttpHeaderValues.CACHE.get(values[0].trim()); if (cb!=null) { switch(cb.getOrdinal()) { case HttpHeaderValues.CLOSE_ORDINAL: response_fields.put(HttpHeaders.CONNECTION_BUFFER,HttpHeaderValues.CLOSE_BUFFER); break; case HttpHeaderValues.KEEP_ALIVE_ORDINAL: if (HttpVersions.HTTP_1_0.equalsIgnoreCase(_connection.getRequest().getProtocol())) response_fields.put(HttpHeaders.CONNECTION_BUFFER,HttpHeaderValues.KEEP_ALIVE); break; case HttpHeaderValues.TE_ORDINAL: response_fields.put(HttpHeaders.CONNECTION_BUFFER,HttpHeaderValues.TE); break; } } } } if (_connection.getConnector().getServer().getSendDateHeader()) { Request request=_connection.getRequest(); response_fields.put(HttpHeaders.DATE_BUFFER, request.getTimeStampBuffer(),request.getTimeStamp()); } _status=200; _reason=null; _mimeType=null; _cachedMimeType=null; _contentType=null; _characterEncoding=null; _explicitEncoding=false; _locale=null; _outputState=NONE; _writer=null; } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletResponse#resetBuffer() */ public void resetBuffer() { if (isCommitted()) throw new IllegalStateException("Committed"); _connection.getGenerator().resetBuffer(); } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletResponse#isCommitted() */ public boolean isCommitted() { return _connection.isResponseCommitted(); } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletResponse#setLocale(java.util.Locale) */ public void setLocale(Locale locale) { if (locale == null || isCommitted() ||_connection.isIncluding()) return; _locale = locale; _connection.getResponseFields().put(HttpHeaders.CONTENT_LANGUAGE_BUFFER,locale.toString().replace('_','-')); if (_explicitEncoding || _outputState!=0 ) return; if (_connection.getRequest().getContext()==null) return; String charset = _connection.getRequest().getContext().getContextHandler().getLocaleEncoding(locale); if (charset!=null && charset.length()>0) { _characterEncoding=charset; /* get current MIME type from Content-Type header */ String type=getContentType(); if (type!=null) { _characterEncoding=charset; int semi=type.indexOf(';'); if (semi<0) { _mimeType=type; _contentType= type += "; charset="+charset; } else { _mimeType=type.substring(0,semi); _contentType= _mimeType += "; charset="+charset; } _cachedMimeType=MimeTypes.CACHE.get(_mimeType); _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletResponse#getLocale() */ public Locale getLocale() { if (_locale==null) return Locale.getDefault(); return _locale; } /* ------------------------------------------------------------ */ /** * @return The HTTP status code that has been set for this request. This will be <code>200<code> * ({@link HttpServletResponse#SC_OK}), unless explicitly set through one of the <code>setStatus</code> methods. */ public int getStatus() { return _status; } /* ------------------------------------------------------------ */ /** * @return The reason associated with the current {@link #getStatus() status}. This will be <code>null</code>, * unless one of the <code>setStatus</code> methods have been called. */ public String getReason() { return _reason; } /* ------------------------------------------------------------ */ /** * */ public void complete() throws IOException { _connection.completeResponse(); } /* ------------------------------------------------------------- */ /** * @return the number of bytes actually written in response body */ public long getContentCount() { if (_connection==null || _connection.getGenerator()==null) return -1; return _connection.getGenerator().getContentWritten(); } /* ------------------------------------------------------------ */ public HttpFields getHttpFields() { return _connection.getResponseFields(); } /* ------------------------------------------------------------ */ public String toString() { return "HTTP/1.1 "+_status+" "+ (_reason==null?"":_reason) +System.getProperty("line.separator")+ _connection.getResponseFields().toString(); } /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ private static class NullOutput extends ServletOutputStream { public void write(int b) throws IOException { } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -