Example #1
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;
  }
 private void findNext() {
   next = null;
   for (; pos < size; pos++) {
     MessageBytes n1 = headers.getName(pos);
     if (n1.equalsIgnoreCase(name)) {
       next = headers.getValue(pos);
       break;
     }
   }
   pos++;
 }
 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++;
 }
Example #4
0
  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);
  }
Example #5
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);
    }
  }
  /**
   * 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;
  }