📄 response.java
字号:
StringBuffer buf = _connection.getRequest().getRootURL(); if (location.startsWith("/")) buf.append(location); else { String path=_connection.getRequest().getRequestURI(); String parent=(path.endsWith("/"))?path:URIUtil.parentPath(path); location=URIUtil.addPaths(parent,location); if(location==null) throw new IllegalStateException("path cannot be above root"); if (!location.startsWith("/")) buf.append('/'); buf.append(location); } location=buf.toString(); HttpURI uri = new HttpURI(location); String path=uri.getDecodedPath(); String canonical=URIUtil.canonicalPath(path); if (canonical==null) throw new IllegalArgumentException(); if (!canonical.equals(path)) { buf = _connection.getRequest().getRootURL(); buf.append(canonical); if (uri.getQuery()!=null) { buf.append('?'); buf.append(uri.getQuery()); } if (uri.getFragment()!=null) { buf.append('#'); buf.append(uri.getFragment()); } location=buf.toString(); } } resetBuffer(); setHeader(HttpHeaders.LOCATION,location); setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); complete(); } /* ------------------------------------------------------------ */ /* * @see javax.servlet.http.HttpServletResponse#setDateHeader(java.lang.String, long) */ public void setDateHeader(String name, long date) { if (!_connection.isIncluding()) _connection.getResponseFields().putDateField(name, date); } /* ------------------------------------------------------------ */ /* * @see javax.servlet.http.HttpServletResponse#addDateHeader(java.lang.String, long) */ public void addDateHeader(String name, long date) { if (!_connection.isIncluding()) _connection.getResponseFields().addDateField(name, date); } /* ------------------------------------------------------------ */ /* * @see javax.servlet.http.HttpServletResponse#setHeader(java.lang.String, java.lang.String) */ public void setHeader(String name, String value) { if (!_connection.isIncluding()) { _connection.getResponseFields().put(name, value); if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) { if (value==null) _connection._generator.setContentLength(-1); else _connection._generator.setContentLength(Long.parseLong(value)); } } } /* ------------------------------------------------------------ */ /* */ public String getHeader(String name) { return _connection.getResponseFields().getStringField(name); } /* ------------------------------------------------------------ */ /* */ public Enumeration getHeaders(String name) { Enumeration e = _connection.getResponseFields().getValues(name); if (e==null) return Collections.enumeration(Collections.EMPTY_LIST); return e; } /* ------------------------------------------------------------ */ /* * @see javax.servlet.http.HttpServletResponse#addHeader(java.lang.String, java.lang.String) */ public void addHeader(String name, String value) { if (!_connection.isIncluding()) { _connection.getResponseFields().add(name, value); if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) _connection._generator.setContentLength(Long.parseLong(value)); } } /* ------------------------------------------------------------ */ /* * @see javax.servlet.http.HttpServletResponse#setIntHeader(java.lang.String, int) */ 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); } } /* ------------------------------------------------------------ */ /* * @see javax.servlet.http.HttpServletResponse#addIntHeader(java.lang.String, int) */ 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); } } /* ------------------------------------------------------------ */ /* * @see javax.servlet.http.HttpServletResponse#setStatus(int) */ public void setStatus(int sc) { setStatus(sc,null); } /* ------------------------------------------------------------ */ /* * @see javax.servlet.http.HttpServletResponse#setStatus(int, java.lang.String) */ public void setStatus(int sc, String sm) { if (!_connection.isIncluding()) { _status=sc; _reason=sm; } } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletResponse#getCharacterEncoding() */ public String getCharacterEncoding() { if (_characterEncoding==null) _characterEncoding=StringUtil.__ISO_8859_1; return _characterEncoding; } /* ------------------------------------------------------------ */ String getSetCharacterEncoding() { return _characterEncoding; } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletResponse#getContentType() */ public String getContentType() { return _contentType; } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletResponse#getOutputStream() */ public ServletOutputStream getOutputStream() throws IOException { if (_outputState==DISABLED) return __nullServletOut; if (_outputState!=NONE && _outputState!=STREAM) throw new IllegalStateException("WRITER"); _outputState=STREAM; return _connection.getOutputStream(); } /* ------------------------------------------------------------ */ public boolean isWriting() { return _outputState==WRITER; } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletResponse#getWriter() */ public PrintWriter getWriter() throws IOException { if (_outputState==DISABLED) return __nullPrintWriter; if (_outputState!=NONE && _outputState!=WRITER) throw new IllegalStateException("STREAM"); /* if there is no writer yet */ if (_writer==null) { /* get encoding from Content-Type header */ String encoding = _characterEncoding; if (encoding==null) { /* implementation of educated defaults */ if(_mimeType!=null) encoding = null; // TODO getHttpContext().getEncodingByMimeType(_mimeType); if (encoding==null) encoding = StringUtil.__ISO_8859_1; setCharacterEncoding(encoding); } /* construct Writer using correct encoding */ _writer = _connection.getPrintWriter(encoding); } _outputState=WRITER; return _writer; } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletResponse#setCharacterEncoding(java.lang.String) */ public void setCharacterEncoding(String encoding) { if (_connection.isIncluding()) return; if (this._outputState==0 && !isCommitted()) { _explicitEncoding=true; if (encoding==null) { // Clear any encoding. if (_characterEncoding!=null) { _characterEncoding=null; if (_cachedMimeType!=null) _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_cachedMimeType); else _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_mimeType); } } else { // No, so just add this one to the mimetype _characterEncoding=encoding; if (_contentType!=null) { int i0=_contentType.indexOf(';'); if (i0<0) { _contentType=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); } } if (_contentType==null) { _contentType = _mimeType+"; charset="+QuotedStringTokenizer.quote(_characterEncoding,";= "); _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } else { int i1=_contentType.indexOf("charset=",i0); if (i1<0) { _contentType = _contentType+" charset="+QuotedStringTokenizer.quote(_characterEncoding,";= "); } else { int i8=i1+8; int i2=_contentType.indexOf(" ",i8); if (i2<0) _contentType=_contentType.substring(0,i8)+QuotedStringTokenizer.quote(_characterEncoding,";= "); else _contentType=_contentType.substring(0,i8)+QuotedStringTokenizer.quote(_characterEncoding,";= ")+_contentType.substring(i2); } _connection.getResponseFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); } } } } } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletResponse#setContentLength(int) */ public void setContentLength(int len) { // 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()) return; _connection._generator.setContentLength(len); if (len>=0) { _connection.getResponseFields().putLongField(HttpHeaders.CONTENT_LENGTH, len); if (_connection._generator.isContentWritten()) { if (_outputState==WRITER) _writer.close(); else if (_outputState==STREAM) { try { getOutputStream().close(); } catch(IOException e) { throw new RuntimeException(e); } } } } } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletResponse#setContentLength(int) */ public void setLongContentLength(long len) { // 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()) return; _connection._generator.setContentLength(len); _connection.getResponseFields().putLongField(HttpHeaders.CONTENT_LENGTH, len); } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletResponse#setContentType(java.lang.String) */ public void setContentType(String contentType) { if (isCommitted() || _connection.isIncluding()) return; // Yes this method is horribly complex.... but there are lots of special cases and // as this method is called on every request, it is worth trying to save string creation. //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -