📄 coyoteresponse.java
字号:
* Add the specified integer header to the specified value. * * @param name Name of the header to set * @param value Integer value to be set */ public void addIntHeader(String name, int value) { if (isCommitted()) return; // Ignore any call from an included servlet if (included) return; addHeader(name, "" + value); } /** * Has the specified header been set already in this response? * * @param name Name of the header to check */ public boolean containsHeader(String name) { return coyoteResponse.containsHeader(name); } /** * Encode the session identifier associated with this response * into the specified redirect URL, if necessary. * * @param url URL to be encoded */ public String encodeRedirectURL(String url) { if (isEncodeable(toAbsolute(url))) { HttpServletRequest hreq = (HttpServletRequest) request.getRequest(); return (toEncoded(url, hreq.getSession().getId())); } else { return (url); } } /** * Encode the session identifier associated with this response * into the specified redirect URL, if necessary. * * @param url URL to be encoded * * @deprecated As of Version 2.1 of the Java Servlet API, use * <code>encodeRedirectURL()</code> instead. */ public String encodeRedirectUrl(String url) { return (encodeRedirectURL(url)); } /** * Encode the session identifier associated with this response * into the specified URL, if necessary. * * @param url URL to be encoded */ public String encodeURL(String url) { String absolute = toAbsolute(url); if (isEncodeable(absolute)) { HttpServletRequest hreq = (HttpServletRequest) request.getRequest(); // W3c spec clearly said if (url.equalsIgnoreCase("")){ url = absolute; } return (toEncoded(url, hreq.getSession().getId())); } else { return (url); } } /** * Encode the session identifier associated with this response * into the specified URL, if necessary. * * @param url URL to be encoded * * @deprecated As of Version 2.1 of the Java Servlet API, use * <code>encodeURL()</code> instead. */ public String encodeUrl(String url) { return (encodeURL(url)); } /** * Send an acknowledgment of a request. * * @exception IOException if an input/output error occurs */ public void sendAcknowledgement() throws IOException { if (isCommitted()) return; // Ignore any call from an included servlet if (included) return; coyoteResponse.acknowledge(); } /** * Send an error response with the specified status and a * default message. * * @param status HTTP status code to send * * @exception IllegalStateException if this response has * already been committed * @exception IOException if an input/output error occurs */ public void sendError(int status) throws IOException { sendError(status, null); } /** * Send an error response with the specified status and message. * * @param status HTTP status code to send * @param message Corresponding message to send * * @exception IllegalStateException if this response has * already been committed * @exception IOException if an input/output error occurs */ public void sendError(int status, String message) throws IOException { if (isCommitted()) throw new IllegalStateException (sm.getString("coyoteResponse.sendError.ise")); // Ignore any call from an included servlet if (included) return; setError(); coyoteResponse.setStatus(status); coyoteResponse.setMessage(message); // Clear any data content that has been buffered resetBuffer(); // Cause the response to be finished (from the application perspective) setSuspended(true); } /** * Send a temporary redirect to the specified redirect location URL. * * @param location Location URL to redirect to * * @exception IllegalStateException if this response has * already been committed * @exception IOException if an input/output error occurs */ public void sendRedirect(String location) throws IOException { if (isCommitted()) throw new IllegalStateException (sm.getString("coyoteResponse.sendRedirect.ise")); // Ignore any call from an included servlet if (included) return; // Clear any data content that has been buffered resetBuffer(); // Generate a temporary redirect to the specified location try { String absolute = toAbsolute(location); setStatus(SC_MOVED_TEMPORARILY); setHeader("Location", absolute); } catch (IllegalArgumentException e) { setStatus(SC_NOT_FOUND); } // Cause the response to be finished (from the application perspective) setSuspended(true); } /** * Set the specified date header to the specified value. * * @param name Name of the header to set * @param value Date value to be set */ public void setDateHeader(String name, long value) { if (isCommitted()) return; // Ignore any call from an included servlet if (included) return; setHeader(name, format.format(new Date(value))); } /** * Set the specified header to the specified value. * * @param name Name of the header to set * @param value Value to be set */ public void setHeader(String name, String value) { if (isCommitted()) return; // Ignore any call from an included servlet if (included) return; coyoteResponse.setHeader(name, value); } /** * Set the specified integer header to the specified value. * * @param name Name of the header to set * @param value Integer value to be set */ public void setIntHeader(String name, int value) { if (isCommitted()) return; // Ignore any call from an included servlet if (included) return; setHeader(name, "" + value); } /** * Set the HTTP status to be returned with this response. * * @param status The new HTTP status */ public void setStatus(int status) { setStatus(status, null); } /** * Set the HTTP status and message to be returned with this response. * * @param status The new HTTP status * @param message The associated text message * * @deprecated As of Version 2.1 of the Java Servlet API, this method * has been deprecated due to the ambiguous meaning of the message * parameter. */ public void setStatus(int status, String message) { if (isCommitted()) return; // Ignore any call from an included servlet if (included) return; coyoteResponse.setStatus(status); coyoteResponse.setMessage(message); } // ------------------------------------------------------ Protected Methods /** * Return <code>true</code> if the specified URL should be encoded with * a session identifier. This will be true if all of the following * conditions are met: * <ul> * <li>The request we are responding to asked for a valid session * <li>The requested session ID was not received via a cookie * <li>The specified URL points back to somewhere within the web * application that is responding to this request * </ul> * * @param location Absolute URL to be validated */ protected boolean isEncodeable(String location) { if (location == null) return (false); // Is this an intra-document reference? if (location.startsWith("#")) return (false); // Are we in a valid session that is not using cookies? HttpServletRequest hreq = (HttpServletRequest) request.getRequest(); HttpSession session = hreq.getSession(false); if (session == null) return (false); if (hreq.isRequestedSessionIdFromCookie()) return (false); // Is this a valid absolute URL? URL url = null; try { url = new URL(location); } catch (MalformedURLException e) { return (false); } // Does this URL match down to (and including) the context path? if (!hreq.getScheme().equalsIgnoreCase(url.getProtocol())) return (false); if (!hreq.getServerName().equalsIgnoreCase(url.getHost())) return (false); int serverPort = hreq.getServerPort(); if (serverPort == -1) { if ("https".equals(hreq.getScheme())) serverPort = 443; else serverPort = 80; } int urlPort = url.getPort(); if (urlPort == -1) { if ("https".equals(url.getProtocol())) urlPort = 443; else urlPort = 80; } if (serverPort != urlPort) return (false); String contextPath = getContext().getPath(); if ((contextPath != null) && (contextPath.length() > 0)) { String file = url.getFile(); if ((file == null) || !file.startsWith(contextPath)) return (false); if( file.indexOf(";jsessionid=" + session.getId()) >= 0 ) return (false); } // This URL belongs to our web application, so it is encodeable return (true); } /** * Convert (if necessary) and return the absolute URL that represents the * resource referenced by this possibly relative URL. If this URL is * already absolute, return it unchanged. * * @param location URL to be (possibly) converted and then returned * * @exception IllegalArgumentException if a MalformedURLException is * thrown when converting the relative URL to an absolute one */ private String toAbsolute(String location) { if (location == null) return (location); // Construct a new absolute URL if possible (cribbed from // the DefaultErrorPage servlet) URL url = null; try { url = new URL(location); } catch (MalformedURLException e1) { HttpServletRequest hreq = (HttpServletRequest) request.getRequest(); String requrl = request.getRequestURL().toString(); try { url = new URL(new URL(requrl), location); } catch (MalformedURLException e2) { throw new IllegalArgumentException(location); } } return (url.toExternalForm()); } /** * Return the specified URL with the specified session identifier * suitably encoded. * * @param url URL to be encoded with the session id * @param sessionId Session id to be included in the encoded URL */ private String toEncoded(String url, String sessionId) { if ((url == null) || (sessionId == null)) return (url); String path = url; String query = ""; String anchor = ""; int question = url.indexOf('?'); if (question >= 0) { path = url.substring(0, question); query = url.substring(question); } int pound = path.indexOf('#'); if (pound >= 0) { anchor = path.substring(pound); path = path.substring(0, pound); } StringBuffer sb = new StringBuffer(path); if( sb.length() > 0 ) { // jsessionid can't be first. sb.append(";jsessionid="); sb.append(sessionId); } sb.append(anchor); sb.append(query); return (sb.toString()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -