abstracthttprequest.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,711 行 · 第 1/4 页

JAVA
2,711
字号
	if (queryEncoding == null && _server != null)	  queryEncoding = _server.getURLCharacterEncoding();      	if (queryEncoding == null)	  queryEncoding = CharacterEncoding.getLocalEncoding();	String javaEncoding = Encoding.getJavaName(queryEncoding);		_formParser.parseQueryString(_form, query, javaEncoding, true);      }            if (charEncoding == null)	charEncoding = CharacterEncoding.getLocalEncoding();            String javaEncoding = Encoding.getJavaName(charEncoding);      if (contentType == null || ! "POST".equalsIgnoreCase(getMethod())) {      }            else if (contentType.startsWith("application/x-www-form-urlencoded")) { 	_formParser.parsePostData(_form, getInputStream(), javaEncoding);      }      else if (getWebApp().doMultipartForm()	       && contentType.startsWith("multipart/form-data")) {        int length = contentType.length();        int i = contentType.indexOf("boundary=");        if (i < 0)          return _form;        long formUploadMax = getWebApp().getFormUploadMax();	Object uploadMax = getAttribute("caucho.multipart.form.upload-max");	if (uploadMax instanceof Number)	  formUploadMax = ((Number) uploadMax).longValue();        // XXX: should this be an error?        if (formUploadMax >= 0 && formUploadMax < getLongContentLength()) {          setAttribute("caucho.multipart.form.error",                       L.l("Multipart form upload of '{0}' bytes was too large.",                           String.valueOf(getLongContentLength())));          setAttribute("caucho.multipart.form.error.size",		       new Long(getLongContentLength()));          return _form;        }        i += "boundary=".length();        char ch = contentType.charAt(i);        CharBuffer boundary = new CharBuffer();        if (ch == '\'') {          for (i++; i < length && contentType.charAt(i) != '\''; i++)            boundary.append(contentType.charAt(i));        }        else if (ch == '\"') {          for (i++; i < length && contentType.charAt(i) != '\"'; i++)            boundary.append(contentType.charAt(i));        }        else {          for (;               i < length && (ch = contentType.charAt(i)) != ' ' &&                 ch != ';';               i++) {            boundary.append(ch);          }        }        try {          MultipartForm.parsePostData(_form,                                      getStream(false), boundary.toString(),                                      this,                                      javaEncoding,                                      formUploadMax);        } catch (IOException e) {          log.log(Level.FINE, e.toString(), e);          setAttribute("caucho.multipart.form.error", e.getMessage());        }      }    } catch (IOException e) {      log.log(Level.FINE, e.toString(), e);    }    return _form;  }  // request attributes  /**   * Returns an enumeration of the request attribute names.   */  public Enumeration<String> getAttributeNames()  {    return Collections.enumeration(_attributes.keySet());  }  /**   * Returns the value of the named request attribute.   *   * @param name the attribute name.   *   * @return the attribute value.   */  public Object getAttribute(String name)  {    return _attributes.get(name);  }  /**   * Sets the value of the named request attribute.   *   * @param name the attribute name.   * @param value the new attribute value.   */  public void setAttribute(String name, Object value)  {    if (value != null) {      Object oldValue = _attributes.put(name, value);      for (int i = 0; i < _attributeListeners.length; i++) {	ServletRequestAttributeEvent event;	if (oldValue != null) {	  event = new ServletRequestAttributeEvent(getWebApp(), this,						   name, oldValue);	  _attributeListeners[i].attributeReplaced(event);	}	else {	  event = new ServletRequestAttributeEvent(getWebApp(), this,						   name, value);	  _attributeListeners[i].attributeAdded(event);	}      }    }    else      removeAttribute(name);  }  /**   * Removes the value of the named request attribute.   *   * @param name the attribute name.   */  public void removeAttribute(String name)  {    Object oldValue = _attributes.remove(name);        for (int i = 0; i < _attributeListeners.length; i++) {      ServletRequestAttributeEvent event;      event = new ServletRequestAttributeEvent(getWebApp(), this,					       name, oldValue);      _attributeListeners[i].attributeRemoved(event);    }    if (oldValue instanceof ScopeRemoveListener) {      ((ScopeRemoveListener) oldValue).removeEvent(this, name);    }  }  /**   * Returns a request dispatcher relative to the current request.   *   * @param path the relative uri to the new servlet.   */  public RequestDispatcher getRequestDispatcher(String path)  {    if (path == null || path.length() == 0)      return null;    else if (path.charAt(0) == '/')      return getWebApp().getRequestDispatcher(path);    else {      CharBuffer cb = new CharBuffer();            ServletContext app = getWebApp();      String servletPath = getPageServletPath();      if (servletPath != null)        cb.append(servletPath);      String pathInfo = getPagePathInfo();      if (pathInfo != null)        cb.append(pathInfo);            int p = cb.lastIndexOf('/');      if (p >= 0)	cb.setLength(p);      cb.append('/');      cb.append(path);      if (app != null)	return app.getRequestDispatcher(cb.toString());      return app.getRequestDispatcher(cb.toString());    }  }    /*   * jsdk 2.2   */  public Locale getLocale()  {    fillLocales();    return _locales.get(0);  }  public Enumeration<Locale> getLocales()  {    fillLocales();    return Collections.enumeration(_locales);  }  /**   * Fill the locale array from the request's headers.   */  private void fillLocales()  {    if (_locales.size() > 0)      return;        Enumeration headers = getHeaders("Accept-Language");    if (headers == null) {      _locales.add(Locale.getDefault());      return;    }    CharBuffer cb = _cb;    while (headers.hasMoreElements()) {      String header = (String) headers.nextElement();      StringCharCursor cursor = new StringCharCursor(header);      while (cursor.current() != cursor.DONE) {	char ch;	for (; Character.isWhitespace(cursor.current()); cursor.next()) {	}	cb.clear();	for (; (ch = cursor.current()) >= 'a' && ch <= 'z' ||	       ch >= 'A' && ch <= 'Z' ||	       ch >= '0' && ch <= '0';	     cursor.next()) {	  cb.append(cursor.current());	}	String language = cb.toString();	String country = "";	String var = "";	if (cursor.current() == '_' || cursor.current() == '-') {	  cb.clear();	  for (cursor.next();	       (ch = cursor.current()) >= 'a' && ch <= 'z' ||	       ch >= 'A' && ch <= 'Z' ||	       ch >= '0' && ch <= '9';	       cursor.next()) {	    cb.append(cursor.current());	  }	  country = cb.toString();	}	if (language.length() > 0) {	  Locale locale = new Locale(language, country);	  _locales.add(locale);	}	for (;	     cursor.current() != cursor.DONE && cursor.current() != ',';	     cursor.next()) {	}	cursor.next();      }    }    if (_locales.size() == 0)      _locales.add(Locale.getDefault());  }  /**   * Returns true if the request is secure.   */  public boolean isSecure()  {    return _conn.isSecure();  }  /**   * Returns the servlet context for the request   *   * @since Servlet 3.0   */  public ServletContext getServletContext()  {    return null;  }  /**   * Returns the servlet response for the request   *   * @since Servlet 3.0   */  public ServletResponse getServletResponse()  {    return null;  }  /**   * Suspend the request   *   * @since Servlet 3.0   */  public void suspend(long timeout)  {  }  /**   * Suspend the request   *   * @since Servlet 3.0   */  public void suspend()  {  }  /**   * Resume the request   *   * @since Servlet 3.0   */  public void resume()  {  }  /**   * Complete the request   *   * @since Servlet 3.0   */  public void complete()  {  }  /**   * Returns true if the servlet is suspended   *   * @since Servlet 3.0   */  public boolean isSuspended()  {    return false;  }  /**   * Returns true if the servlet is resumed   *   * @since Servlet 3.0   */  public boolean isResumed()  {    return false;  }  /**   * Returns true if the servlet timed out   *   * @since Servlet 3.0   */  public boolean isTimeout()  {    return false;  }  /**   * Returns true for the initial dispatch   *   * @since Servlet 3.0   */  public boolean isInitial()  {    return true;  }  //  // internal goodies  //  /**   * Returns the request's invocation.   */  public final Invocation getInvocation()  {    return _invocation;  }  /**   * Sets the request's invocation.   */  public final void setInvocation(Invocation invocation)  {    _invocation = invocation;    WebApp app = invocation.getWebApp();    if (app != null)      _attributeListeners = app.getRequestAttributeListeners();  }  /**   * Sets the start time to the current time.   */  protected final void setStartTime()  {    if (_tcpConn != null)      _tcpConn.beginActive();    else      _startTime = Alarm.getCurrentTime();  }    /**   * Returns the date for the current request.   */  public final long getStartTime()  {    if (_tcpConn != null)      return _tcpConn.getRequestStartTime();    else      return _startTime;  }  /**   * Returns the servlet name.   */  public String getServletName()  {    return _invocation.getServletName();  }  /**   * Returns the invocation's webApp.   */  public final WebApp getWebApp()  {    if (_invocation != null)      return _invocation.getWebApp();    else      return null;  }  /**   * Returns the log buffer.   */  public final byte []getLogBuffer()  {    return _logBuffer;  }  /**   * Returns true for the top-level request, but false for any include()   * or forward()   */  public boolean isTop()  {    return false;  }  /**   * Adds a file to be removed at the end.   */  public void addCloseOnExit(Path path)  {    _closeOnExit.add(path);  }  /**   * Returns the depth of the request calls.   */  public int getRequestDepth(int depth)  {    return depth + 1;  }  public int getRequestDepth()  {    return 0;  }    /**   * Handles a comet-style resume.   *   * @return true if the connection should stay open (keepalive)   */  public boolean handleResume()    throws IOException  {    return false;  }  /**   * Kills the keepalive.   */  public void killKeepalive()  {    _keepalive = false;    /*    ConnectionController controller = _conn.getController();    if (controller != null)      controller.close();    */  }  /**   * Returns true if the keepalive is active.   */  protected boolean isKeepalive()  {    return _keepalive;  }  /**   * Returns true if keepalives are allowed.   *   * This method should only be called once, when the response is   * deciding whether to send the Connection: close (or 'Q' vs 'X'),   * after that, the calling routines should call isKeepalive() to   * see what the decision was.   *   * Otherwise, the browser might see a keepalive when the final decision   * is to close the connection.   */  public boolean allowKeepalive()  {    if (! _keepalive)      return false;    TcpConnection tcpConn = _tcpConn;        if (tcpConn == null)      return true;    if (! tcpConn.toKeepalive())      _keepalive = false;    return _keepalive;  }  /**   * Restarts the server.   */  protected void restartServer()    throws IOException, ServletException  {    HttpServletResponse res = (HttpServletResponse) getResponse();    res.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);    _server.update();  }  void saveSession()  {    SessionImpl session = _session;    if (session != null)      session.save();  }    /**   * Cleans up at the end of the request   */  public void finish()    throws IOException  {    try {      SecurityContextProvider oldProvider = _oldProvider;      _oldProvider = null;            SecurityContext.setProvider(oldProvider);      SessionImpl session = _session;      // server/0219      // _invocation = null;      if (session != null)        session.finish();      cleanup();    } finally {      for (int i = _closeOnExit.size() - 1; i >= 0; i--) {        Path path = _closeOnExit.get(i);        try {          path.remove();        } catch (Throwable e) {          log.log(Level.FINE, e.toString(), e);        }      }      _closeOnExit.clear();      if (_tcpConn != null)	_tcpConn.endActive();    }  }  public void cleanup()  {    ConnectionController comet = getConnection().getController();    if (comet == null) {      _session = null;            if (_attributes.size() > 0) {	for (Map.Entry<String,Object> entry : _attributes.entrySet()) {	  Object value = entry.getValue();	  if (value instanceof ScopeRemoveListener) {	    ((ScopeRemoveListener) value).removeEvent(this, entry.getKey());	  }	}		_attributes.clear();      }    }  }  protected String dbgId()  {    return "Tcp[" + _conn.getId() + "] ";  }  static {    _headerCodes = new CaseInsensitiveIntMap();    TOKEN = new boolean[256];    VALUE = new boolean[256];    for (int i = 0; i < 256; i++) {      TOKEN[i] = true;    }    for (int i = 0; i < 32; i++) {      TOKEN[i] = false;    }    for (int i = 127; i < 256; i++) {      TOKEN[i] = false;    }    //TOKEN['('] = false;    //TOKEN[')'] = false;    //TOKEN['<'] = false;    //TOKEN['>'] = false;    //TOKEN['@'] = false;    TOKEN[','] = false;    TOKEN[';'] = false;    //TOKEN[':'] = false;    TOKEN['\\'] = false;    TOKEN['"'] = false;    //TOKEN['/'] = false;    //TOKEN['['] = false;    //TOKEN[']'] = false;    //TOKEN['?'] = false;    TOKEN['='] = false;    //TOKEN['{'] = false;    //TOKEN['}'] = false;    TOKEN[' '] = false;    System.arraycopy(TOKEN, 0, VALUE, 0, TOKEN.length);    VALUE['='] = true;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?