abstracthttprequest.java

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

JAVA
2,711
字号
    return getQueryString();  }  /**   * Returns the named header.   *   * @param key the header key   */  abstract public String getHeader(String key);  /**   * Returns the number of headers.   */  public int getHeaderSize()  {    return -1;  }  /**   * Returns the header key   */  public CharSegment getHeaderKey(int index)  {    throw new UnsupportedOperationException();  }  /**   * Returns the header value   */  public CharSegment getHeaderValue(int index)  {    throw new UnsupportedOperationException();  }  /**   * Fills the result with the header values as   * CharSegment values.  Most implementations will   * implement this directly.   *   * @param name the header name   */  public CharSegment getHeaderBuffer(String name)  {    String value = getHeader(name);        if (value != null)      return new CharBuffer(value);    else      return null;  }  /**   * Enumerates the header keys   */  abstract public Enumeration getHeaderNames();  /**   * Sets the header.  setHeader is used for   * Resin's caching to simulate If-None-Match.   */  public void setHeader(String key, String value)  {  }  /**   * Adds the header, checking for known values.   */  protected boolean addHeaderInt(char []keyBuf, int keyOff, int keyLen,                                 CharSegment value)  {    if (keyLen < 4)      return true;    int key1 = keyBuf[keyOff];    switch (key1) {    case 'c':    case 'C':      if (keyLen == CONNECTION.length	  && match(keyBuf, keyOff, keyLen, CONNECTION)) {	if (match(value.getBuffer(), value.getOffset(), value.length(),		  CLOSE)) {	  connectionClose();	}      }      else if (keyLen == COOKIE.length	       && match(keyBuf, keyOff, keyLen, COOKIE)) {	fillCookie(_cookies, value);      }      else if (keyLen == CONTENT_LENGTH.length	       && match(keyBuf, keyOff, keyLen, CONTENT_LENGTH)) {	long contentLength = 0;	int i = 0;	int ch;	int length = value.length();	for (; i < length && (ch = value.charAt(i)) >= '0' && ch <= '9'; i++)	  contentLength = 10 * contentLength + ch - '0';	if (i > 0)	  _contentLength = contentLength;      }      return true;          case 'e':    case 'E':      if (match(keyBuf, keyOff, keyLen, EXPECT)) {	if (match(value.getBuffer(), value.getOffset(), value.length(),		  CONTINUE_100)) {	  _expect100Continue = true;          return false;	}      }            return true;          case 'h':    case 'H':      if (match(keyBuf, keyOff, keyLen, HOST)) {	_hostHeader = value;      }      return true;          default:      return true;    }  }  /**   * Called for a connection: close   */  protected void connectionClose()  {    killKeepalive();  }  /**   * Matches case insensitively, with the second normalized to lower case.   */  private boolean match(char []a, int aOff, int aLength, char []b)  {    int bLength = b.length;        if (aLength != bLength)      return false;    for (int i = aLength - 1; i >= 0; i--) {      char chA = a[aOff + i];      char chB = b[i];      if (chA != chB && chA + 'a' - 'A' != chB) {	return false;      }    }    return true;  }  /**   * Returns an enumeration of the headers for the named attribute.   *   * @param name the header name   */  public Enumeration getHeaders(String name)  {    String value = getHeader(name);    if (value == null)      return NullEnumeration.create();    ArrayList<String> list = new ArrayList<String>();    list.add(value);    return Collections.enumeration(list);  }  /**   * Fills the result with a list of the header values as   * CharSegment values.  Most implementations will   * implement this directly.   *   * @param name the header name   * @param resultList the resulting buffer   */  public void getHeaderBuffers(String name, ArrayList<CharSegment> resultList)  {    String value = getHeader(name);    if (value != null)      resultList.add(new CharBuffer(value));  }  /**   * Returns the named header, converted to an integer.   *   * @param key the header key.   *   * @return the value of the header as an integer.   */  public int getIntHeader(String key)  {    CharSegment value = getHeaderBuffer(key);    if (value == null)      return -1;    int len = value.length();    if (len == 0)      throw new NumberFormatException(value.toString());    int iValue = 0;    int i = 0;    int ch = value.charAt(i);    int sign = 1;    if (ch == '+') {      if (i + 1 < len)	ch = value.charAt(++i);      else	throw new NumberFormatException(value.toString());    } else if (ch == '-') {      sign = -1;      if (i + 1 < len)	ch = value.charAt(++i);      else	throw new NumberFormatException(value.toString());    }    for (; i < len && (ch = value.charAt(i)) >= '0' && ch <= '9'; i++)      iValue = 10 * iValue + ch - '0';    if (i < len)      throw new NumberFormatException(value.toString());    return sign * iValue;  }  /**   * Returns a header interpreted as a date.   *   * @param key the header key.   *   * @return the value of the header as an integer.   */  public long getDateHeader(String key)  {    String value = getHeader(key);    if (value == null)      return -1;    long date = -1;    try {      date = _calendar.parseDate(value);      if (date == Long.MAX_VALUE)	throw new IllegalArgumentException("getDateHeader(" + value + ")");      return date;    } catch (RuntimeException e) {      throw e;    } catch (Exception e) {      throw new IllegalArgumentException(e);    }  }  /**   * Returns the content length of a post.   */  public int getContentLength()  {    return (int) _contentLength;  }  /**   * Returns the content length of a post.   */  public long getLongContentLength()  {    return _contentLength;  }  /**   * Returns the content-length of a post.   */  public String getContentType()  {    return getHeader("Content-Type");  }  /**   * Returns the content-length of a post.   */  public CharSegment getContentTypeBuffer()  {    return getHeaderBuffer("Content-Type");  }  /**   * Returns the character encoding of a post.   */  public String getCharacterEncoding()  {    if (_readEncoding != null)      return _readEncoding;        CharSegment value = getHeaderBuffer("Content-Type");    if (value == null)      return null;    int i = value.indexOf("charset");    if (i < 0)      return null;    int len = value.length();    for (i += 7; i < len && Character.isWhitespace(value.charAt(i)); i++) {    }    if (i >= len || value.charAt(i) != '=')      return null;    for (i++; i < len && Character.isWhitespace(value.charAt(i)); i++) {    }    if (i >= len)      return null;    char end = value.charAt(i);    if (end == '"') {      int tail;      for (tail = ++i; tail < len; tail++) {        if (value.charAt(tail) == end)          break;      }      _readEncoding = Encoding.getMimeName(value.substring(i, tail));            return _readEncoding;    }    int tail;    for (tail = i; tail < len; tail++) {      if (Character.isWhitespace(value.charAt(tail)) ||	  value.charAt(tail) == ';')	break;    }    _readEncoding = Encoding.getMimeName(value.substring(i, tail));        return _readEncoding;  }  /**   * Sets the character encoding of a post.   */  public void setCharacterEncoding(String encoding)    throws UnsupportedEncodingException  {    _readEncoding = encoding;        try {      if (_hasReadStream)	_readStream.setEncoding(_readEncoding);    } catch (UnsupportedEncodingException e) {      throw e;    } catch (java.nio.charset.UnsupportedCharsetException e) {      throw new UnsupportedEncodingException(e.getMessage());    } catch (IOException e) {      log.log(Level.FINE, e.toString(), e);    }  }  /**   * Returns the cookies from the browser   */  public Cookie []getCookies()  {    // The page varies depending on the presense of any cookies    setVaryCookie(null);        if (_cookiesIn == null)      fillCookies();    // If any cookies actually exist, the page is not anonymous    if (_cookiesIn != null && _cookiesIn.length > 0)      setHasCookie();    if (_cookiesIn == null || _cookiesIn.length == 0)      return null;    else      return _cookiesIn;  }  /**   * Returns the named cookie from the browser   */  public Cookie getCookie(String name)  {    // The page varies depending on the presense of any cookies    setVaryCookie(name);    return findCookie(name);  }  private Cookie findCookie(String name)  {    if (_cookiesIn == null)      fillCookies();    if (_cookiesIn == null)      return null;    int length = _cookiesIn.length;    for (int i = 0; i < length; i++) {      Cookie cookie = _cookiesIn[i];            if (cookie.getName().equals(name)) {        setHasCookie();        return cookie;      }    }    return null;  }  /**   * Parses cookie information from the cookie headers.   */  private void fillCookies()  {    int size = _cookies.size();    if (size > 0) {      _cookiesIn = new Cookie[_cookies.size()];      _cookies.toArray(_cookiesIn);    }    else      _cookiesIn = NULL_COOKIES;  }  /**   * Parses a single cookie   *   * @param cookies the array of cookies read   * @param rawCook the input for the cookie   */  private void fillCookie(ArrayList cookies, CharSegment rawCookie)  {    char []buf = rawCookie.getBuffer();    int j = rawCookie.getOffset();    int end = j + rawCookie.length();    int version = 0;    Cookie cookie = null;    while (j < end) {      char ch = 0;      CharBuffer cbName = _cbName;      CharBuffer cbValue = _cbValue;            cbName.clear();      cbValue.clear();      for (;	   j < end && ((ch = buf[j]) == ' ' || ch == ';' || ch ==',');	   j++) {      }      if (end <= j)        break;      boolean isSpecial = false;      if (buf[j] == '$') {        isSpecial = true;        j++;      }      for (; j < end; j++) {	ch = buf[j];	if (ch < 128 && TOKEN[ch])	  cbName.append(ch);	else	  break;      }      for (; j < end && (ch = buf[j]) == ' '; j++) {      }      if (end <= j)	break;      else if (ch == ';' || ch == ',') {        try {          cookie = new Cookie(cbName.toString(), "");          cookie.setVersion(version);          _cookies.add(cookie);          // some clients can send bogus cookies        } catch (Exception e) {          log.log(Level.FINE, e.toString(), e);        }        continue;      }      else if (ch != '=') {        for (; j < end && (ch = buf[j]) != ';'; j++) {        }        continue;      }      j++;      for (; j < end && (ch = buf[j]) == ' '; j++) {      }      if (ch == '"') {        for (j++; j < end; j++) {          ch = buf[j];          if (ch == '"')            break;          cbValue.append(ch);        }        j++;      }      else {        for (; j < end; j++) {          ch = buf[j];          if (ch < 128 && VALUE[ch])	    cbValue.append(ch);	  else	    break;        }      }      if (! isSpecial) {        if (cbName.length() == 0)          log.warning("bad cookie: " + rawCookie);        else {          cookie = new Cookie(cbName.toString(), cbValue.toString());          cookie.setVersion(version);          _cookies.add(cookie);        }      }      else if (cookie == null) {        if (cbName.matchesIgnoreCase("Version"))          version = cbValue.charAt(0) - '0';      }      else if (cbName.matchesIgnoreCase("Version"))        cookie.setVersion(cbValue.charAt(0) - '0');      else if (cbName.matchesIgnoreCase("Domain"))        cookie.setDomain(cbValue.toString());      else if (cbName.matchesIgnoreCase("Path"))        cookie.setPath(cbValue.toString());    }  }  /**   * Called if the page depends on a cookie.  If the cookie is null, then   * the page depends on all cookies.   *   * @param cookie the cookie the page depends on.   */  public void setVaryCookie(String cookie)  {    if (_varyCookies == false)      _varyCookie = cookie;    else if (_varyCookie != null && ! _varyCookie.equals(cookie))      _varyCookie = null;        _varyCookies = true;    // XXX: server/1315 vs 2671    // _response.setPrivateOrResinCache(true);  }    /**   * Returns true if the page depends on cookies.   */  public boolean getVaryCookies()  {    return _varyCookies;  }    /**   * Returns the cookie the page depends on, or null if the page   * depends on several cookies.   */  public String getVaryCookie()  {    return _varyCookie;  }  /**   * Set when the page actually has a cookie.   */  public void setHasCookie()  {    _hasCookie = true;        // XXX: 1171 vs 1240    // _response.setPrivateOrResinCache(true);  }  /**   * True if this page uses cookies.   */  public boolean getHasCookie()  {    if (_hasCookie)      return true;    else if (_invocation != null)      return _invocation.getSessionId() != null;    else      return false;  }  /**   * Returns the memory session.   */  public HttpSession getMemorySession()  {    if (_session != null && _session.isValid())      return _session;    else      return null;  }  /**   * Returns the current session, creating one if necessary.   */  public HttpSession getSession()  {    return getSession(true);  }  /**   * Returns the current session.   *   * @param create true if a new session should be created   *   * @return the current session   */  public HttpSession getSession(boolean create)  {    if (_session != null) {      if (_session.isValid())	return _session;    }    else if (! create && _sessionIsLoaded)      return null;    _sessionIsLoaded = true;    boolean hasOldSession = _session != null;    _session = createSession(create, hasOldSession);        return _session;  }  /**   * Returns the current session.   *   * @param create true if a new session should be created   *   * @return the current session   */  public HttpSession getLoadedSession()  {    if (_session != null && _session.isValid())      return _session;    else      return null;  }  /**   * Returns true if the HTTP request's session id refers to a valid   * session.   */  public boolean isRequestedSessionIdValid()  {    String id = getRequestedSessionId();    if (id == null)      return false;

⌨️ 快捷键说明

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