/**
  * Gets the status reason phrase for this response as {@link DataChunk} (avoid creation of a
  * String object}. This implementation takes into consideration the {@link
  * #isAllowCustomReasonPhrase()} property - if the custom reason phrase is allowed and it's value
  * is not null - then the returned result will be equal to {@link #getReasonPhraseRawDC()},
  * otherwise if custom reason phrase is disallowed or its value is null - the default reason
  * phrase for the HTTP response {@link #getStatus()} will be returned.
  *
  * @return the status reason phrase for this response as {@link DataChunk} (avoid creation of a
  *     String object}.
  */
 public final DataChunk getReasonPhraseDC() {
   if (isCustomReasonPhraseSet()) {
     return reasonPhraseC;
   } else {
     reasonPhraseC.setBytes(httpStatus.getReasonPhraseBytes());
     return reasonPhraseC;
   }
 }
 /**
  * Sets the status code for this response.
  *
  * @param status the status code for this response.
  */
 public void setStatus(final int status) {
   // the order is important here as statusDC.setXXX will reset the parsedIntStatus
   httpStatus = HttpStatus.getHttpStatus(status);
 }
  public static Buffer encodeHeaders(
      final MemoryManager mm, final HttpResponsePacket httpResponsePacket) {
    Buffer encodedBuffer = mm.allocate(4096);
    int startPos = encodedBuffer.position();
    // Skip 4 bytes for the Ajp header
    encodedBuffer.position(startPos + 4);

    encodedBuffer.put(AjpConstants.JK_AJP13_SEND_HEADERS);
    encodedBuffer.putShort((short) httpResponsePacket.getStatus());
    final byte[] tempBuffer = httpResponsePacket.getTempHeaderEncodingBuffer();
    if (httpResponsePacket.isCustomReasonPhraseSet()) {
      encodedBuffer =
          putBytes(
              mm,
              encodedBuffer,
              HttpStatus.filter(httpResponsePacket.getReasonPhraseDC()),
              tempBuffer);
    } else {
      encodedBuffer =
          putBytes(mm, encodedBuffer, httpResponsePacket.getHttpStatus().getReasonPhraseBytes());
    }

    if (httpResponsePacket.isAcknowledgement()) {
      // If it's acknoledgment packet - don't encode the headers
      // Serialize 0 num_headers
      encodedBuffer = putShort(mm, encodedBuffer, 0);
    } else {
      final MimeHeaders headers = httpResponsePacket.getHeaders();
      final String contentType = httpResponsePacket.getContentType();
      if (contentType != null) {
        headers.setValue("Content-Type").setString(contentType);
      }
      final String contentLanguage = httpResponsePacket.getContentLanguage();
      if (contentLanguage != null) {
        headers.setValue("Content-Language").setString(contentLanguage);
      }
      final long contentLength = httpResponsePacket.getContentLength();
      if (contentLength >= 0) {
        final Buffer contentLengthBuffer = getLongAsBuffer(mm, contentLength);
        headers
            .setValue("Content-Length")
            .setBuffer(
                contentLengthBuffer, contentLengthBuffer.position(), contentLengthBuffer.limit());
      }

      final int numHeaders = headers.size();

      encodedBuffer = putShort(mm, encodedBuffer, numHeaders);

      for (int i = 0; i < numHeaders; i++) {
        final DataChunk headerName = headers.getName(i);
        encodedBuffer = putBytes(mm, encodedBuffer, headerName, tempBuffer);

        final DataChunk headerValue = headers.getValue(i);
        encodedBuffer = putBytes(mm, encodedBuffer, headerValue, tempBuffer);
      }
    }

    // Add Ajp message header
    encodedBuffer.put(startPos, (byte) 'A');
    encodedBuffer.put(startPos + 1, (byte) 'B');
    encodedBuffer.putShort(startPos + 2, (short) (encodedBuffer.position() - startPos - 4));

    return encodedBuffer;
  }