📄 serve.java
字号:
} /// Returns the fully qualified host name of the agent that sent the // request. // Same as the CGI variable REMOTE_HOST. public String getRemoteHost() { String result = socket.getInetAddress().getHostName(); return result!=null?result:getRemoteAddr(); } /// Applies alias rules to the specified virtual path and returns the // corresponding real path, or null if the translation can not be // performed for any reason. For example, an HTTP servlet would // resolve the path using the virtual docroot, if virtual hosting is // enabled, and with the default docroot otherwise. Calling this // method with the string "/" as an argument returns the document root. public String getRealPath( String path ) { return serve.getRealPath( path ); } /// Returns an input stream for reading request data. // @exception IllegalStateException if getReader has already been called // @exception IOException on other I/O-related errors public ServletInputStream getInputStream() throws IOException { synchronized(in) { if (((ServeInputStream)in).isReturnedAsReader()) throw new IllegalStateException("Already returned as a reader."); ((ServeInputStream)in).setReturnedAsReader(true); } return in; } /// Returns a buffered reader for reading request data. // @exception UnsupportedEncodingException if the character set encoding isn't supported // @exception IllegalStateException if getInputStream has already been called // @exception IOException on other I/O-related errors public BufferedReader getReader() { synchronized(in) { if (((ServeInputStream)in).isReturnedAsStream()) throw new IllegalStateException("Already returned as a stream."); ((ServeInputStream)in).setReturnedAsStream(true); } if (reqCharEncoding != null) try { return new BufferedReader(new InputStreamReader(in, reqCharEncoding)); } catch(UnsupportedEncodingException uee) { } return new BufferedReader(new InputStreamReader(in)); } private Hashtable getParametersFromRequest() { Hashtable result = null;//System.out.println("Req:"+reqMethod+" con:"+getContentType()+" eq "+WWWFORMURLENCODE.equals(getContentType())); if("GET".equals(reqMethod)) { if(reqQuery != null) try { result = HttpUtils.parseQueryString(reqQuery); } catch(IllegalArgumentException ex) { } } else if ("POST".equals(reqMethod)) if(WWWFORMURLENCODE.equals(getContentType())) try { result = HttpUtils.parsePostData(getContentLength(), getInputStream()); if (reqQuery != null && reqQuery.length() > 0) { Acme.Utils.putAll(result, HttpUtils.parseQueryString(reqQuery)); } } catch(Exception ex) { serve.log("Exception "+ex+" at parsing post data of length "+getContentLength()); } else try { if(reqQuery != null) result = HttpUtils.parseQueryString(reqQuery); } catch(Exception ex) { } return result!=null?result:EMPTYHASHTABLE; } /// Returns the parameter names for this request. public Enumeration getParameterNames() { if (formParameters==null) formParameters = getParametersFromRequest(); return formParameters.keys(); } /// Returns the value of the specified query string parameter, or null // if not found. // @param name the parameter name public String getParameter( String name ) { String[] params = getParameterValues(name); if (params == null || params.length == 0) return null; return params[0]; } /// Returns the values of the specified parameter for the request as an // array of strings, or null if the named parameter does not exist. public String[] getParameterValues( String name ) { if (formParameters == null) getParameterNames(); return (String[])formParameters.get(name); } /// Returns the value of the named attribute of the request, or null if // the attribute does not exist. This method allows access to request // information not already provided by the other methods in this interface. public Object getAttribute( String name ) { return attributes.get(name); } // Methods from HttpServletRequest. /// Gets the array of cookies found in this request. public Cookie[] getCookies() { Cookie[] cookieArray = new Cookie[inCookies.size()]; inCookies.copyInto( cookieArray ); return cookieArray; } /// Returns the method with which the request was made. This can be "GET", // "HEAD", "POST", or an extension method. // Same as the CGI variable REQUEST_METHOD. public String getMethod() { return reqMethod; } /*** Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request. To reconstruct an URL with a scheme and host, use HttpUtils.getRequestURL(javax.servlet.http.HttpServletRequest). */ /// Returns the full request URI. public String getRequestURI() { return reqUriPath; } /** Reconstructs the URL the client used to make the request. * The returned URL contains a protocol, server name, port number, * and server path, but it does not include query string parameters. <br> * Because this method returns a StringBuffer, not a string, you can modify the * URL easily, for example, to append query parameters. * <p> * This method is useful for creating redirect messages and for reporting errors. * * @return a StringBuffer object containing the reconstructed URL * @since 2.3 */ public java.lang.StringBuffer getRequestURL() { return new StringBuffer().append(getScheme()).append("://").append(serve.hostName).append(serve.port==80?"":String.valueOf(serve.port)).append(getRequestURI()); } /// Returns the part of the request URI that referred to the servlet being // invoked. // Analogous to the CGI variable SCRIPT_NAME. public String getServletPath() { // In this server, the entire path is regexp-matched against the // servlet pattern, so there's no good way to distinguish which // part refers to the servlet. return uriLen>0?reqUriPath.substring(0, uriLen):""; } /// Returns optional extra path information following the servlet path, but // immediately preceding the query string. Returns null if not specified. // Same as the CGI variable PATH_INFO. public String getPathInfo() { // In this server, the entire path is regexp-matched against the // servlet pattern, so there's no good way to distinguish which // part refers to the servlet. return uriLen >= reqUriPath.length()?null:reqUriPath.substring(uriLen); } /// Returns extra path information translated to a real path. Returns // null if no extra path information was specified. // Same as the CGI variable PATH_TRANSLATED. public String getPathTranslated() { // In this server, the entire path is regexp-matched against the // servlet pattern, so there's no good way to distinguish which // part refers to the servlet. return getRealPath(getPathInfo()); } /// Returns the query string part of the servlet URI, or null if not known. // Same as the CGI variable QUERY_STRING. public String getQueryString() { return reqQuery; } /// Returns the name of the user making this request, or null if not known. // Same as the CGI variable REMOTE_USER. public String getRemoteUser() { return remoteUser; } /// Returns the authentication scheme of the request, or null if none. // Same as the CGI variable AUTH_TYPE. public String getAuthType() { return authType; } /// Returns the value of a header field, or null if not known. // Same as the information passed in the CGI variabled HTTP_*. // @param name the header field name public String getHeader( String name ) { int i = reqHeaderNames.indexOf( name.toLowerCase() ); if ( i == -1 ) return null; return (String) reqHeaderValues.elementAt( i ); } public int getIntHeader(String name) { return getIntHeader(name, 0); } /// Returns the value of an integer header field. // @param name the header field name // @param def the integer value to return if header not found or invalid public int getIntHeader( String name, int def ) { String val = getHeader( name ); if ( val == null ) return def; try { return Integer.parseInt( val ); } catch ( Exception e ) { return def; } } /// Returns the value of a long header field. // @param name the header field name // @param def the long value to return if header not found or invalid public long getLongHeader( String name, long def ) { String val = getHeader( name ); if ( val == null ) return def; try { return Long.parseLong( val ); } catch ( Exception e ) { return def; } } public long getDateHeader(String name) { String val = getHeader( name ); if ( val == null ) return 0; try { return headerdateformat.parse( val ).getTime(); } catch ( Exception e ) { throw new IllegalArgumentException("Value "+val+" can't be converted to Date using "+headerdateformat.toPattern()); } } /// Returns the value of a date header field. // @param name the header field name // @param def the date value to return if header not found or invalid public long getDateHeader( String name, long def ) { String val = getHeader( name ); if ( val == null ) return def; try { return DateFormat.getDateInstance().parse( val ).getTime(); } catch ( Exception e ) { return def; } } /// Returns an Enumeration of the header names. public Enumeration getHeaderNames() { return reqHeaderNames.elements(); } /// Gets the current valid session associated with this request, if // create is false or, if necessary, creates a new session for the // request, if create is true. // <P> // Note: to ensure the session is properly maintained, the servlet // developer must call this method (at least once) before any output // is written to the response. // <P> // Additionally, application-writers need to be aware that newly // created sessions (that is, sessions for which HttpSession.isNew // returns true) do not have any application-specific state. public HttpSession getSession( boolean create ) { // look for session // get session cookie HttpSession result = getSession(); if (result == null && create) { result = serve.createSession(); } if (result != null) sessionCookieValue = result.getId(); return result; } // JSDK 2.1 public HttpSession getSession() { AcmeSession result = null; if (sessionCookieValue!=null) { result = (AcmeSession)serve.getSession(sessionCookieValue); if (result != null && !result.isValid()) { serve.removeSession(sessionCookieValue); result = null; } } return result; } public boolean isRequestedSessionIdFromURL() { return false; } // from ServletRequest public Enumeration getAttributeNames() { return attributes.keys(); } public void setAttribute(String key, Object o) { attributes.put(key, o); } /// Gets the session id specified with this request. This may differ // from the actual session id. For example, if the request specified // an id for an invalid session, then this will get a new session with // a new id. public String getRequestedSessionId() { return sessionCookieValue; } /// Checks whether this request is associated with a session that is // valid in the current session context. If it is not valid, the // requested session will never be returned from the getSession // method. public boolean isRequestedSess
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -