private void findNext() {
   next = null;
   for (; pos < size; pos++) {
     MessageBytes n1 = headers.getName(pos);
     if (n1.equalsIgnoreCase(name)) {
       next = headers.getValue(pos);
       break;
     }
   }
   pos++;
 }
Exemple #2
0
  @Override
  public Collection<String> getHeaderNames() {

    MimeHeaders headers = coyoteResponse.getMimeHeaders();
    int n = headers.size();
    List<String> result = new ArrayList<>(n);
    for (int i = 0; i < n; i++) {
      result.add(headers.getName(i).toString());
    }
    return result;
  }
Exemple #3
0
 public void setHeader(String name, String value) {
   char cc = name.charAt(0);
   if (cc == 'C' || cc == 'c') {
     if (checkSpecialHeader(name, value)) return;
   }
   headers.setValue(name).setString(value);
 }
  public void reset() throws IllegalStateException {

    // Reset the headers only if this is the main request,
    // not for included
    contentType = null;
    ;
    locale = DEFAULT_LOCALE;
    contentLanguage = null;
    characterEncoding = Constants.DEFAULT_CHARACTER_ENCODING;
    // START SJSAS 6316254
    quotedCharsetValue = characterEncoding;
    // END SJSAS 6316254
    contentLength = -1;
    charsetSet = false;

    status = 200;
    message = null;
    headers.clear();

    // Force the PrintWriter to flush its data to the output
    // stream before resetting the output stream
    //
    // Reset the stream
    if (commited) {
      // String msg = sm.getString("servletOutputStreamImpl.reset.ise");
      throw new IllegalStateException();
    }

    action(ActionCode.ACTION_RESET, this);
  }
 ValuesEnumerator(MimeHeaders headers, String name) {
   this.name = name;
   this.headers = headers;
   pos = 0;
   size = headers.size();
   findNext();
 }
  public long getContentLengthLong() {
    if (contentLength > -1) {
      return contentLength;
    }

    MessageBytes clB = headers.getUniqueValue("content-length");
    contentLength = (clB == null || clB.isNull()) ? -1 : clB.getLong();

    return contentLength;
  }
Exemple #7
0
  /** Add all Cookie found in the headers of a request. */
  public void processCookies(MimeHeaders headers) {
    if (headers == null) {
      return; // nothing to process
    }
    // process each "cookie" header
    int pos = 0;
    while (pos >= 0) {
      // Cookie2: version ? not needed
      pos = headers.findHeader("Cookie", pos);
      // no more cookie headers headers
      if (pos < 0) {
        break;
      }

      MessageBytes cookieValue = headers.getValue(pos);
      if (cookieValue == null || cookieValue.isNull()) {
        pos++;
        continue;
      }

      if (cookieValue.getType() != MessageBytes.T_BYTES) {
        Exception e = new Exception();
        log.warn("Cookies: Parsing cookie as String. Expected bytes.", e);
        cookieValue.toBytes();
      }
      if (log.isDebugEnabled()) {
        log.debug("Cookies: Parsing b[]: " + cookieValue.toString());
      }
      ByteChunk bc = cookieValue.getByteChunk();
      if (CookieSupport.PRESERVE_COOKIE_HEADER) {
        int len = bc.getLength();
        if (len > 0) {
          byte[] buf = new byte[len];
          System.arraycopy(bc.getBytes(), bc.getOffset(), buf, 0, len);
          processCookieHeader(buf, 0, len);
        }
      } else {
        processCookieHeader(bc.getBytes(), bc.getOffset(), bc.getLength());
      }
      pos++; // search from the next position
    }
  }
 private void findNext() {
   next = null;
   for (; pos < size; pos++) {
     next = headers.getName(pos).toString();
     for (int j = 0; j < pos; j++) {
       if (headers.getName(j).equalsIgnoreCase(next)) {
         // duplicate.
         next = null;
         break;
       }
     }
     if (next != null) {
       // it's not a duplicate
       break;
     }
   }
   // next time findNext is called it will try the
   // next element
   pos++;
 }
Exemple #9
0
 public void addHeader(String name, String value, Charset charset) {
   char cc = name.charAt(0);
   if (cc == 'C' || cc == 'c') {
     if (checkSpecialHeader(name, value)) return;
   }
   MessageBytes mb = headers.addValue(name);
   if (charset != null) {
     mb.setCharset(charset);
   }
   mb.setString(value);
 }
  public void appendHead(Response res) throws IOException {
    if (log.isDebugEnabled())
      log.debug("COMMIT sending headers " + res + " " + res.getMimeHeaders());

    C2BConverter c2b = mc.getConverter();

    outputMsg.reset();
    outputMsg.appendByte(AjpConstants.JK_AJP13_SEND_HEADERS);
    outputMsg.appendInt(res.getStatus());

    String message = null;
    if (org.apache.coyote.Constants.USE_CUSTOM_STATUS_MSG_IN_HEADER
        && HttpMessages.isSafeInHttpHeader(res.getMessage())) {
      message = res.getMessage();
    }
    if (message == null) {
      message = HttpMessages.getMessage(res.getStatus());
    }
    if (message == null) {
      // mod_jk + httpd 2.x fails with a null status message - bug 45026
      message = Integer.toString(res.getStatus());
    }
    tempMB.setString(message);
    c2b.convert(tempMB);
    outputMsg.appendBytes(tempMB);

    // XXX add headers

    MimeHeaders headers = res.getMimeHeaders();
    String contentType = res.getContentType();
    if (contentType != null) {
      headers.setValue("Content-Type").setString(contentType);
    }
    String contentLanguage = res.getContentLanguage();
    if (contentLanguage != null) {
      headers.setValue("Content-Language").setString(contentLanguage);
    }
    long contentLength = res.getContentLengthLong();
    if (contentLength >= 0) {
      headers.setValue("Content-Length").setLong(contentLength);
    }
    int numHeaders = headers.size();
    outputMsg.appendInt(numHeaders);
    for (int i = 0; i < numHeaders; i++) {
      MessageBytes hN = headers.getName(i);
      // no header to sc conversion - there's little benefit
      // on this direction
      outputMsg.appendBytes(hN);

      MessageBytes hV = headers.getValue(i);
      outputMsg.appendBytes(hV);
    }
    mc.getSource().send(outputMsg, mc);
  }
Exemple #11
0
  /**
   * Special method for adding a session cookie as we should be overriding any previous
   *
   * @param cookie
   */
  public void addSessionCookieInternal(final Cookie cookie) {
    if (isCommitted()) {
      return;
    }

    String name = cookie.getName();
    final String headername = "Set-Cookie";
    final String startsWith = name + "=";
    String header = generateCookieString(cookie);
    boolean set = false;
    MimeHeaders headers = coyoteResponse.getMimeHeaders();
    int n = headers.size();
    for (int i = 0; i < n; i++) {
      if (headers.getName(i).toString().equals(headername)) {
        if (headers.getValue(i).toString().startsWith(startsWith)) {
          headers.getValue(i).setString(header);
          set = true;
        }
      }
    }
    if (!set) {
      addHeader(headername, header);
    }
  }
  public void recycle() {

    contentType = null;
    contentLanguage = null;
    locale = DEFAULT_LOCALE;
    characterEncoding = Constants.DEFAULT_CHARACTER_ENCODING;
    // START SJSAS 6316254
    quotedCharsetValue = characterEncoding;
    // END SJSAS 6316254
    charsetSet = false;
    contentLength = -1;
    status = 200;
    message = null;
    commited = false;
    errorException = null;
    errorURI = null;
    headers.clear();

    // update counters
    bytesWritten = 0;
  }
Exemple #13
0
  public void recycle() {

    contentType = null;
    contentLanguage = null;
    locale = DEFAULT_LOCALE;
    characterEncoding = Constants.DEFAULT_CHARACTER_ENCODING;
    charsetSet = false;
    contentLength = -1;
    status = 200;
    message = null;
    commited = false;
    commitTime = -1;
    errorException = null;
    headers.clear();
    // Servlet 3.1 non-blocking write listener
    listener = null;
    fireListener = false;
    registeredForWrite = false;

    // update counters
    contentWritten = 0;
  }
  public void recycle() {
    bytesRead = 0;

    contentLength = -1;
    contentTypeMB = null;
    charEncoding = null;
    expectation = false;
    headers.recycle();
    serverNameMB.recycle();
    serverPort = -1;
    localNameMB.recycle();
    localPort = -1;
    remotePort = -1;
    available = 0;
    sendfile = true;

    serverCookies.recycle();
    parameters.recycle();
    pathParameters.clear();

    uriMB.recycle();
    decodedUriMB.recycle();
    queryMB.recycle();
    methodMB.recycle();
    protoMB.recycle();

    schemeMB.recycle();

    remoteUser.recycle();
    remoteUserNeedsAuthorization = false;
    authType.recycle();
    attributes.clear();

    listener = null;
    allDataReadEventSent.set(false);

    startTime = -1;
  }
 public String getHeader(String name) {
   return headers.getHeader(name);
 }
  /**
   * Encodes the headers into a buffer.
   *
   * @param headers The headers to encode
   * @param target The buffer to which to write the encoded headers
   * @return The state of the encoding process
   */
  public State encode(MimeHeaders headers, ByteBuffer target) {
    int it = headersIterator;
    if (headersIterator == -1) {
      handleTableSizeChange(target);
      // new headers map
      it = 0;
      currentHeaders = headers;
    } else {
      if (headers != currentHeaders) {
        throw new IllegalStateException();
      }
    }
    while (it < currentHeaders.size()) {
      // FIXME: Review lowercase policy
      String headerName = headers.getName(it).toString().toLowerCase(Locale.US);
      boolean skip = false;
      if (firstPass) {
        if (headerName.charAt(0) != ':') {
          skip = true;
        }
      } else {
        if (headerName.charAt(0) == ':') {
          skip = true;
        }
      }
      if (!skip) {
        String val = headers.getValue(it).toString();

        if (log.isDebugEnabled()) {
          log.debug(sm.getString("hpackEncoder.encodeHeader", headerName, val));
        }
        TableEntry tableEntry = findInTable(headerName, val);

        // We use 11 to make sure we have enough room for the
        // variable length integers
        int required = 11 + headerName.length() + 1 + val.length();

        if (target.remaining() < required) {
          this.headersIterator = it;
          return State.UNDERFLOW;
        }
        // Only index if it will fit
        boolean canIndex =
            hpackHeaderFunction.shouldUseIndexing(headerName, val)
                && (headerName.length() + val.length() + 32) < maxTableSize;
        if (tableEntry == null && canIndex) {
          // add the entry to the dynamic table
          target.put((byte) (1 << 6));
          writeHuffmanEncodableName(target, headerName);
          writeHuffmanEncodableValue(target, headerName, val);
          addToDynamicTable(headerName, val);
        } else if (tableEntry == null) {
          // literal never indexed
          target.put((byte) (1 << 4));
          writeHuffmanEncodableName(target, headerName);
          writeHuffmanEncodableValue(target, headerName, val);
        } else {
          // so we know something is already in the table
          if (val.equals(tableEntry.value)) {
            // the whole thing is in the table
            target.put((byte) (1 << 7));
            Hpack.encodeInteger(target, tableEntry.getPosition(), 7);
          } else {
            if (canIndex) {
              // add the entry to the dynamic table
              target.put((byte) (1 << 6));
              Hpack.encodeInteger(target, tableEntry.getPosition(), 6);
              writeHuffmanEncodableValue(target, headerName, val);
              addToDynamicTable(headerName, val);

            } else {
              target.put((byte) (1 << 4));
              Hpack.encodeInteger(target, tableEntry.getPosition(), 4);
              writeHuffmanEncodableValue(target, headerName, val);
            }
          }
        }
      }
      if (++it == currentHeaders.size() && firstPass) {
        firstPass = false;
        it = 0;
      }
    }
    headersIterator = -1;
    firstPass = true;
    return State.COMPLETE;
  }
Exemple #17
0
 /**
  * Does the response contain the given header. <br>
  * Warning: This method always returns <code>false</code> for Content-Type and Content-Length.
  *
  * @param name The name of the header of interest
  * @return {@code true} if the response contains the header.
  */
 public boolean containsHeader(String name) {
   return headers.getHeader(name) != null;
 }
 public MessageBytes contentType() {
   if (contentTypeMB == null) {
     contentTypeMB = headers.getValue("content-type");
   }
   return contentTypeMB;
 }
 public NamesEnumerator(MimeHeaders headers) {
   this.headers = headers;
   pos = 0;
   size = headers.size();
   findNext();
 }
 /** Removes any Set-Cookie response headers whose value contains the string JSESSIONID */
 public void removeSessionCookies() {
   headers.removeHeader("Set-Cookie", Constants.SESSION_COOKIE_NAME);
 }