<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <axiom_soap_builder.h>
#include "_axiom_soap_envelope.h"
#include "_axiom_soap_header.h"
#include "axiom_soap11_builder_helper.h"
#include "axiom_soap12_builder_helper.h"
#include <axiom_soap_const.h>
#include "_axiom_soap_body.h"
#include "_axiom_soap_header_block.h"
#include <axiom_stax_builder_internal.h>
#include "axiom_soap_builder_internal.h"
#include "_axiom_soap_fault.h"
#include <axutil_http_chunked_stream.h>

static axis2_status_t
axiom_soap_builder_identify_soap_version(
    axiom_soap_builder_t * soap_builder,
    const axutil_env_t * env,
    const axis2_char_t * soap_version_uri_from_transport);

static axis2_status_t
axiom_soap_builder_parse_headers(
    axiom_soap_builder_t * soap_builder,
    const axutil_env_t * env);

static axis2_status_t
axiom_soap_builder_process_namespace_data(
    axiom_soap_builder_t * soap_builder,
    const axutil_env_t * env,
    axiom_node_t * om_node);

static axis2_status_t
axiom_soap_builder_construct_soap_header(
    axiom_soap_builder_t *soap_builder,
    const axutil_env_t *env,
    axiom_node_t *om_node);

static axis2_status_t
axiom_soap_builder_construct_soap_body(
    axiom_soap_builder_t *soap_builder,
    const axutil_env_t *env,
    axiom_node_t *om_node);

static axis2_status_t
axiom_soap_builder_construct_soap_fault(
    axiom_soap_builder_t *soap_builder,
    const axutil_env_t *env,
    axiom_node_t *om_node);

static axis2_status_t
axiom_soap_builder_construct_xop_include(
    axiom_soap_builder_t *soap_builder,
    const axutil_env_t *env,
    axiom_node_t *om_element_node);

struct axiom_soap_builder
{
    axiom_stax_builder_t *om_builder;

    axiom_soap_envelope_t *soap_envelope;

    axis2_bool_t header_present;

    axis2_bool_t body_present;

    int element_level;

    axis2_bool_t processing_fault;

    axis2_bool_t processing_detail_elements;

    axis2_char_t *sender_fault_code;

    axis2_char_t *receiver_fault_code;

    axis2_bool_t processing_mandatory_fault_elements;

    void *builder_helper;

    axiom_namespace_t *envelope_ns;

    int soap_version;

    int last_node_status;

    axis2_bool_t done;

    axutil_hash_t *mime_body_parts;

    axiom_mime_parser_t *mime_parser;

    AXIS2_READ_INPUT_CALLBACK callback;

    void *callback_ctx;

};

typedef enum axis2_builder_last_node_states
{
    AXIS2_BUILDER_LAST_NODE_NULL = 0,
    AXIS2_BUILDER_LAST_NODE_NOT_NULL
} axis2_builder_last_node_states;

#define AXIS2_MAX_EVENT 100

AXIS2_EXTERN axiom_soap_builder_t *AXIS2_CALL
axiom_soap_builder_create(
    const axutil_env_t * env,
    axiom_stax_builder_t * stax_builder,
    const axis2_char_t * soap_version)
{
    axiom_soap_builder_t *soap_builder = NULL;
    axis2_status_t status = AXIS2_SUCCESS;
    AXIS2_ENV_CHECK(env, NULL);

    if(!stax_builder)
    {
        AXIS2_ERROR_SET(env->error, AXIS2_ERROR_INVALID_NULL_PARAM, AXIS2_FAILURE);
        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "STAX builder is invalid.");
        return NULL;
    }

    soap_builder = (axiom_soap_builder_t*)AXIS2_MALLOC(env->allocator, sizeof(axiom_soap_builder_t));
    if(!soap_builder)
    {
        AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE);
        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Insufficient memory to create soap builder");
        return NULL;
    }
    soap_builder = (axiom_soap_builder_t*)memset(soap_builder, 0, sizeof(axiom_soap_builder_t));
    soap_builder->soap_version = AXIOM_SOAP12;
    soap_builder->last_node_status = -1;
    soap_builder->om_builder = stax_builder;
    axiom_stax_builder_set_soap_builder(stax_builder, env, soap_builder);
    soap_builder->done = AXIS2_FALSE;

    status = axiom_soap_builder_identify_soap_version(soap_builder, env, soap_version);
    if(status != AXIS2_SUCCESS)
    {
        axiom_soap_builder_free(soap_builder, env);
        return NULL;
    }
    status = axiom_soap_builder_parse_headers(soap_builder, env);
    if(status != AXIS2_SUCCESS)
    {
        axiom_soap_builder_free(soap_builder, env);
        return NULL;
    }
    return soap_builder;
}

AXIS2_EXTERN void AXIS2_CALL
axiom_soap_builder_free(
    axiom_soap_builder_t * soap_builder,
    const axutil_env_t * env)
{
    if(!soap_builder)
    {
        return;
    }
    if(soap_builder->builder_helper)
    {
        if(soap_builder->soap_version == AXIOM_SOAP11 && soap_builder->builder_helper)
        {
            axiom_soap11_builder_helper_free(
                (axiom_soap11_builder_helper_t *)(soap_builder->builder_helper), env);
            soap_builder->builder_helper = NULL;
        }
        else if(soap_builder->soap_version == AXIOM_SOAP12 && soap_builder->builder_helper)
        {
            axiom_soap12_builder_helper_free(
                (axiom_soap12_builder_helper_t *)(soap_builder->builder_helper), env);
            soap_builder->builder_helper = NULL;
        }
    }

    if(soap_builder->om_builder)<commit_msg>Remove double checking<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <axiom_soap_builder.h>
#include "_axiom_soap_envelope.h"
#include "_axiom_soap_header.h"
#include "axiom_soap11_builder_helper.h"
#include "axiom_soap12_builder_helper.h"
#include <axiom_soap_const.h>
#include "_axiom_soap_body.h"
#include "_axiom_soap_header_block.h"
#include <axiom_stax_builder_internal.h>
#include "axiom_soap_builder_internal.h"
#include "_axiom_soap_fault.h"
#include <axutil_http_chunked_stream.h>

static axis2_status_t
axiom_soap_builder_identify_soap_version(
    axiom_soap_builder_t * soap_builder,
    const axutil_env_t * env,
    const axis2_char_t * soap_version_uri_from_transport);

static axis2_status_t
axiom_soap_builder_parse_headers(
    axiom_soap_builder_t * soap_builder,
    const axutil_env_t * env);

static axis2_status_t
axiom_soap_builder_process_namespace_data(
    axiom_soap_builder_t * soap_builder,
    const axutil_env_t * env,
    axiom_node_t * om_node);

static axis2_status_t
axiom_soap_builder_construct_soap_header(
    axiom_soap_builder_t *soap_builder,
    const axutil_env_t *env,
    axiom_node_t *om_node);

static axis2_status_t
axiom_soap_builder_construct_soap_body(
    axiom_soap_builder_t *soap_builder,
    const axutil_env_t *env,
    axiom_node_t *om_node);

static axis2_status_t
axiom_soap_builder_construct_soap_fault(
    axiom_soap_builder_t *soap_builder,
    const axutil_env_t *env,
    axiom_node_t *om_node);

static axis2_status_t
axiom_soap_builder_construct_xop_include(
    axiom_soap_builder_t *soap_builder,
    const axutil_env_t *env,
    axiom_node_t *om_element_node);

struct axiom_soap_builder
{
    axiom_stax_builder_t *om_builder;

    axiom_soap_envelope_t *soap_envelope;

    axis2_bool_t header_present;

    axis2_bool_t body_present;

    int element_level;

    axis2_bool_t processing_fault;

    axis2_bool_t processing_detail_elements;

    axis2_char_t *sender_fault_code;

    axis2_char_t *receiver_fault_code;

    axis2_bool_t processing_mandatory_fault_elements;

    void *builder_helper;

    axiom_namespace_t *envelope_ns;

    int soap_version;

    int last_node_status;

    axis2_bool_t done;

    axutil_hash_t *mime_body_parts;

    axiom_mime_parser_t *mime_parser;

    AXIS2_READ_INPUT_CALLBACK callback;

    void *callback_ctx;

};

typedef enum axis2_builder_last_node_states
{
    AXIS2_BUILDER_LAST_NODE_NULL = 0,
    AXIS2_BUILDER_LAST_NODE_NOT_NULL
} axis2_builder_last_node_states;

#define AXIS2_MAX_EVENT 100

AXIS2_EXTERN axiom_soap_builder_t *AXIS2_CALL
axiom_soap_builder_create(
    const axutil_env_t * env,
    axiom_stax_builder_t * stax_builder,
    const axis2_char_t * soap_version)
{
    axiom_soap_builder_t *soap_builder = NULL;
    axis2_status_t status = AXIS2_SUCCESS;
    AXIS2_ENV_CHECK(env, NULL);

    if(!stax_builder)
    {
        AXIS2_ERROR_SET(env->error, AXIS2_ERROR_INVALID_NULL_PARAM, AXIS2_FAILURE);
        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "STAX builder is invalid.");
        return NULL;
    }

    soap_builder = (axiom_soap_builder_t*)AXIS2_MALLOC(env->allocator, sizeof(axiom_soap_builder_t));
    if(!soap_builder)
    {
        AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE);
        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Insufficient memory to create soap builder");
        return NULL;
    }
    soap_builder = (axiom_soap_builder_t*)memset(soap_builder, 0, sizeof(axiom_soap_builder_t));
    soap_builder->soap_version = AXIOM_SOAP12;
    soap_builder->last_node_status = -1;
    soap_builder->om_builder = stax_builder;
    axiom_stax_builder_set_soap_builder(stax_builder, env, soap_builder);
    soap_builder->done = AXIS2_FALSE;

    status = axiom_soap_builder_identify_soap_version(soap_builder, env, soap_version);
    if(status != AXIS2_SUCCESS)
    {
        axiom_soap_builder_free(soap_builder, env);
        return NULL;
    }
    status = axiom_soap_builder_parse_headers(soap_builder, env);
    if(status != AXIS2_SUCCESS)
    {
        axiom_soap_builder_free(soap_builder, env);
        return NULL;
    }
    return soap_builder;
}

AXIS2_EXTERN void AXIS2_CALL
axiom_soap_builder_free(
    axiom_soap_builder_t * soap_builder,
    const axutil_env_t * env)
{
    if(!soap_builder)
    {
        return;
    }
    if(soap_builder->builder_helper)
    {
        if(soap_builder->soap_version == AXIOM_SOAP11)
        {
            axiom_soap11_builder_helper_free(
                (axiom_soap11_builder_helper_t *)(soap_builder->builder_helper), env);
            soap_builder->builder_helper = NULL;
        }
        else if(soap_builder->soap_version == AXIOM_SOAP12)
        {
            axiom_soap12_builder_helper_free(
                (axiom_soap12_builder_helper_t *)(soap_builder->builder_helper), env);
            soap_builder->builder_helper = NULL;
        }
    }

    if(soap_builder->om_builder)
{'fraction_non_alphanumeric': 0.07051001662146794, 'fraction_numerical': 0.01259732306884787, 'mean_word_length': 4.314737331473733, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6804623', 'n_tokens_mistral': 4161, 'n_tokens_neox': 3967, 'n_words': 745}