static az_result _az_http_policy_logging_append_http_request_msg()

in sdk/src/azure/core/az_http_policy_logging.c [57:122]


static az_result _az_http_policy_logging_append_http_request_msg(
    az_http_request const* request,
    az_span* ref_log_msg)
{
  static az_span const auth_header_name = AZ_SPAN_LITERAL_FROM_STR("authorization");

  az_span http_request_string = AZ_SPAN_FROM_STR("HTTP Request : ");
  az_span null_string = AZ_SPAN_FROM_STR("NULL");

  int32_t required_length = az_span_size(http_request_string);
  if (request == NULL)
  {
    required_length += az_span_size(null_string);
  }
  else
  {
    required_length += az_span_size(request->_internal.method) + request->_internal.url_length + 1;
  }

  _az_RETURN_IF_NOT_ENOUGH_SIZE(*ref_log_msg, required_length);

  az_span remainder = az_span_copy(*ref_log_msg, http_request_string);

  if (request == NULL)
  {
    remainder = az_span_copy(remainder, null_string);
    *ref_log_msg = az_span_slice(*ref_log_msg, 0, _az_span_diff(remainder, *ref_log_msg));
    return AZ_OK;
  }

  remainder = az_span_copy(remainder, request->_internal.method);
  remainder = az_span_copy_u8(remainder, ' ');
  remainder = az_span_copy(
      remainder, az_span_slice(request->_internal.url, 0, request->_internal.url_length));

  int32_t const headers_count = az_http_request_headers_count(request);

  az_span new_line_tab_string = AZ_SPAN_FROM_STR("\n\t");
  az_span colon_separator_string = AZ_SPAN_FROM_STR(" : ");

  for (int32_t index = 0; index < headers_count; ++index)
  {
    az_span header_name = { 0 };
    az_span header_value = { 0 };
    _az_RETURN_IF_FAILED(az_http_request_get_header(request, index, &header_name, &header_value));

    required_length = az_span_size(new_line_tab_string) + az_span_size(header_name);
    if (az_span_size(header_value) > 0)
    {
      required_length += _az_LOG_LENGTHY_VALUE_MAX_LENGTH + az_span_size(colon_separator_string);
    }

    _az_RETURN_IF_NOT_ENOUGH_SIZE(remainder, required_length);
    remainder = az_span_copy(remainder, new_line_tab_string);
    remainder = az_span_copy(remainder, header_name);

    if (az_span_size(header_value) > 0 && !az_span_is_content_equal(header_name, auth_header_name))
    {
      remainder = az_span_copy(remainder, colon_separator_string);
      remainder = _az_http_policy_logging_copy_lengthy_value(remainder, header_value);
    }
  }
  *ref_log_msg = az_span_slice(*ref_log_msg, 0, _az_span_diff(remainder, *ref_log_msg));

  return AZ_OK;
}