def _content_length_rewriter(state):
  # Convert the body into a list of strings, to allow it to be traversed more
  # than once. This is the only way to get the Content-Length before streaming
  # the output.
  state.body = list(state.body)

  length = sum(len(block) for block in state.body)

  if state.status_code in constants.NO_BODY_RESPONSE_STATUSES:
    # Delete the body and Content-Length response header.
    state.body = []
    del state.headers['Content-Length']
  elif state.environ.get('REQUEST_METHOD') == 'HEAD':
    if length:
      # Delete the body, but preserve the Content-Length response header.
      logging.warning('Dropping unexpected body in response to HEAD request')
      state.body = []
  else:
    if (not state.allow_large_response and
        length > constants.MAX_RUNTIME_RESPONSE_SIZE):
      # NOTE: This response is too small to be visible in IE, as it replaces any
      # error page with <512 bytes with its own.
      # http://en.wikipedia.org/wiki/HTTP_404#Custom_error_pages
      logging.error('Response too large: %d, max is %d',
                    length, constants.MAX_RUNTIME_RESPONSE_SIZE)
      new_response = ('HTTP response was too large: %d. The limit is: %d.\n' %
                      (length, constants.MAX_RUNTIME_RESPONSE_SIZE))
      state.status = '500 Internal Server Error'
      state.headers['Content-Type'] = 'text/html'
      state.headers['Content-Length'] = str(len(new_response))
      state.body = [new_response]
    else:
      state.headers['Content-Length'] = str(length)


# A list of functions that take a RewriterState and possibly modify it. The
# functions are applied to the response in order.
Rewrite the Content-Length header.
dataset/ETHPy150Open AppScale/appscale/AppServer/google/appengine/tools/devappserver2/request_rewriter.py/_content_length_rewriter