httprequest.java

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

JAVA
1,500
字号
    return _headerValues[index];  }  /**   * Returns the header.   */  public String getHeader(String key)  {    CharSegment buf = getHeaderBuffer(key);    if (buf != null)      return buf.toString();    else      return null;  }  /**   * Returns the matching header.   *   * @param testBuf header key   * @param length length of the key.   */  public CharSegment getHeaderBuffer(char []testBuf, int length)  {    char []keyBuf = _headerBuffer;    CharSegment []headerKeys = _headerKeys;    for (int i = _headerSize - 1; i >= 0; i--) {      CharSegment key = headerKeys[i];      if (key.length() != length)        continue;      int offset = key.getOffset();      int j;      for (j = length - 1; j >= 0; j--) {        char a = testBuf[j];        char b = keyBuf[offset + j];        if (a == b)          continue;        if (a >= 'A' && a <= 'Z')          a += 'a' - 'A';        if (b >= 'A' && b <= 'Z')          b += 'a' - 'A';        if (a != b)          break;      }      if (j < 0)        return _headerValues[i];    }    return null;  }  /**   * Returns the header value for the key, returned as a CharSegment.   */  public CharSegment getHeaderBuffer(String key)  {    int i = matchNextHeader(0, key);    if (i >= 0)      return _headerValues[i];    else      return null;  }  /**   * Fills an ArrayList with the header values matching the key.   *   * @param values ArrayList which will contain the maching values.   * @param key the header key to select.   */  public void getHeaderBuffers(String key, ArrayList<CharSegment> values)  {    int i = -1;    while ((i = matchNextHeader(i + 1, key)) >= 0)      values.add(_headerValues[i]);  }  /**   * Return an enumeration of headers matching a key.   *   * @param key the header key to match.   * @return the enumeration of the headers.   */  public Enumeration getHeaders(String key)  {    ArrayList<String> values = new ArrayList<String>();    int i = -1;    while ((i = matchNextHeader(i + 1, key)) >= 0)      values.add(_headerValues[i].toString());    return Collections.enumeration(values);  }  /**   * Returns the index of the next header matching the key.   *   * @param i header index to start search   * @param key header key to match   *   * @return the index of the next header matching, or -1.   */  private int matchNextHeader(int i, String key)  {    int size = _headerSize;    int length = key.length();    char []keyBuf = _headerBuffer;    for (; i < size; i++) {      CharSegment header = _headerKeys[i];      if (header.length() != length)        continue;      int offset = header.getOffset();      int j;      for (j = 0; j < length; j++) {        char a = key.charAt(j);        char b = keyBuf[offset + j];        if (a == b)          continue;        if (a >= 'A' && a <= 'Z')          a += 'a' - 'A';        if (b >= 'A' && b <= 'Z')          b += 'a' - 'A';        if (a != b)          break;      }      if (j == length)        return i;    }    return -1;  }  /**   * Returns an enumeration of all the header keys.   */  public Enumeration getHeaderNames()  {    ArrayList<String> names = new ArrayList<String>();    for (int i = 0; i < _headerSize; i++) {      CharSegment name = _headerKeys[i];      int j;      for (j = 0; j < names.size(); j++) {	String oldName = names.get(j);	if (name.matches(oldName))	  break;      }      if (j == names.size())	names.add(j, name.toString());    }    return Collections.enumeration(names);  }  /**   * Returns a stream for reading POST data.   */  public boolean initStream(ReadStream readStream, ReadStream rawRead)    throws IOException  {    long contentLength = getLongContentLength();    // needed to avoid auto-flush on read conflicting with partially    // generated response    rawRead.setSibling(null);    String te;    if (contentLength < 0 && HTTP_1_1 <= getVersion()	&& (te = getHeader("Transfer-Encoding")) != null) {      _chunkedInputStream.init(rawRead);      readStream.init(_chunkedInputStream, null);      return true;    }    // Otherwise use content-length    else if (contentLength >= 0) {      _contentLengthStream.init(rawRead, contentLength);      _readStream.init(_contentLengthStream, null);      return true;    }    else if (getMethod().equals("POST")) {      _contentLengthStream.init(rawRead, 0);      _readStream.init(_contentLengthStream, null);      throw new com.caucho.server.dispatch.BadRequestException("POST requires content-length");    }    else {      _contentLengthStream.init(rawRead, 0);      _readStream.init(_contentLengthStream, null);      return false;    }  }  protected void skip()    throws IOException  {    if (getMethod() == "GET")      return;    super.skip();  }  /**   * Prints the remote address into a buffer.   *   * @param buffer the buffer which will contain the address.   * @param offset the initial offset into the buffer.   *   * @return the final offset into the buffer.   */  /*  public int printRemoteAddr(byte []buffer, int offset)    throws IOException  {    Connection conn = getConnection();    if (! (conn instanceof TcpConnection))      return super.printRemoteAddr(buffer, offset);    QSocket socket = ((TcpConnection) conn).getSocket();    if (socket instanceof QJniSocket) {      QJniSocket jniSocket = (QJniSocket) socket;      long ip = jniSocket.getRemoteIP();      for (int i = 24; i >= 0; i -= 8) {        int value = (int) (ip >> i) & 0xff;        if (value < 10)          buffer[offset++] = (byte) (value + '0');        else if (value < 100) {          buffer[offset++] = (byte) (value / 10 + '0');          buffer[offset++] = (byte) (value % 10 + '0');        }        else {          buffer[offset++] = (byte) (value / 100 + '0');          buffer[offset++] = (byte) (value / 10 % 10 + '0');          buffer[offset++] = (byte) (value % 10 + '0');        }        if (i != 0)          buffer[offset++] = (byte) '.';      }    }    else {      InetAddress addr = conn.getRemoteAddress();      if (addr == null) {        buffer[offset++] = (byte) '0';        buffer[offset++] = (byte) '.';        buffer[offset++] = (byte) '0';        buffer[offset++] = (byte) '.';        buffer[offset++] = (byte) '0';        buffer[offset++] = (byte) '.';        buffer[offset++] = (byte) '0';        return offset;      }      byte []bytes = addr.getAddress();      for (int i = 0; i < bytes.length; i++) {        if (i != 0)          buffer[offset++] = (byte) '.';        int value = bytes[i] & 0xff;        if (value < 10)          buffer[offset++] = (byte) (value + '0');        else if (value < 100) {          buffer[offset++] = (byte) (value / 10 + '0');          buffer[offset++] = (byte) (value % 10 + '0');        }        else {          buffer[offset++] = (byte) (value / 100 + '0');          buffer[offset++] = (byte) (value / 10 % 10 + '0');          buffer[offset++] = (byte) (value % 10 + '0');        }      }    }    return offset;  }  */  /**   * Returns the client's remote host name.   */  /*  public String getRemoteHost()  {    Connection conn = getConnection();    if (conn instanceof TcpConnection) {      QSocket socket = ((TcpConnection) conn).getSocket();      if (socket instanceof QJniSocket) {        QJniSocket jniSocket = (QJniSocket) socket;        long ip = jniSocket.getRemoteIP();        CharBuffer cb = _cb;        cb.clear();        for (int i = 24; i >= 0; i -= 8) {          int value = (int) (ip >> i) & 0xff;          if (value < 10)            cb.append((char) (value + '0'));          else if (value < 100) {            cb.append((char) (value / 10 + '0'));            cb.append((char) (value % 10 + '0'));          }          else {            cb.append((char) (value / 100 + '0'));            cb.append((char) (value / 10 % 10 + '0'));            cb.append((char) (value % 10 + '0'));          }          if (i != 0)            cb.append('.');        }        return cb.toString();      }    }    InetAddress addr = conn.getRemoteAddress();    byte []bytes = addr.getAddress();    CharBuffer cb = _cb;    cb.clear();    for (int i = 0; i < bytes.length; i++) {      int value = bytes[i] & 0xff;      if (i != 0)        cb.append('.');      if (value < 10)        cb.append((char) (value + '0'));      else if (value < 100) {        cb.append((char) (value / 10 + '0'));        cb.append((char) (value % 10 + '0'));      }      else {        cb.append((char) (value / 100 + '0'));        cb.append((char) (value / 10 % 10 + '0'));        cb.append((char) (value % 10 + '0'));      }    }    return cb.toString();  }  */  /**   * Returns the named attribute.   */  public Object getAttribute(String name)  {    if (! _initAttributes)      initAttributes();    return super.getAttribute(name);  }  /**   * Returns an enumeration of the attribute names.   */  public Enumeration<String> getAttributeNames()  {    if (! _initAttributes)      initAttributes();    return super.getAttributeNames();  }  /**   * For SSL connections, use the SSL identifier.   */  public String findSessionIdFromConnection()  {    TcpConnection tcpConn = _tcpConn;        if (! _isSecure || tcpConn == null)      return null;    QSocket socket = tcpConn.getSocket(); // XXX:    /*    if (! (socket instanceof SSLSocket))      return null;    SSLSession sslSession = ((SSLSocket) socket).getSession();    if (sslSession == null)      return null;    byte []sessionId = sslSession.getId();    if (sessionId == null)      return null;    CharBuffer cb = CharBuffer.allocate();    Base64.encode(cb, sessionId, 0, sessionId.length);    for (int i = cb.length() - 1; i >= 0; i--) {      char ch = cb.charAt(i);      if (ch == '/')        cb.setCharAt(i, '-');    }    return cb.close();    */    return null;  }  /**   * Returns the raw input stream.   */  public ReadStream getRawInput()  {    return _rawRead;  }  /**   * Initialize any special attributes.   */  private void initAttributes()  {    _initAttributes = true;    TcpConnection tcpConn = _tcpConn;        if (! _isSecure || tcpConn == null)      return;    QSocket socket = tcpConn.getSocket();    String cipherSuite = socket.getCipherSuite();    super.setAttribute("javax.servlet.request.cipher_suite", cipherSuite);    int keySize = socket.getCipherBits();    if (keySize != 0)      super.setAttribute("javax.servlet.request.key_size",                         new Integer(keySize));    try {      X509Certificate []certs = socket.getClientCertificates();      if (certs != null && certs.length > 0) {        super.setAttribute("javax.servlet.request.X509Certificate", certs[0]);        super.setAttribute(com.caucho.server.security.AbstractAuthenticator.LOGIN_NAME,                           certs[0].getSubjectDN());      }    } catch (Exception e) {      log.log(Level.FINER, e.toString(), e);    }  }    public final void protocolCloseEvent()  {  }  /**   * Cleans up at the end of the request   */  public void finish()    throws IOException  {    super.finish();    skip();  }  protected String dbgId()  {    if ("".equals(_server.getServerId()))      return "Http[" + _conn.getId() + "] ";    else      return "Http[" + _server.getServerId() + ", " + _conn.getId() + "] ";  }  public String toString()  {    if ("".equals(_server.getServerId()))      return "HttpRequest[" + _conn.getId() + "]";    else {      return ("HttpRequest[" + _server.getServerId()	      + ", " + _conn.getId() + "]");    }  }}

⌨️ 快捷键说明

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