<commit_before>/* Copyright 2004 <NAME> and <NAME>
 *
 * Licensed 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 <stdlib.h>

#include <apr_general.h>  /* for strcasecmp() */

#include "serf.h"
#include "serf_bucket_util.h"


typedef struct header_list {
    const char *header;
    const char *value;

    apr_size_t header_size;
    apr_size_t value_size;

    int alloc_flags;
#define ALLOC_HEADER 0x0001  /* header lives in our allocator */
#define ALLOC_VALUE  0x0002  /* value lives in our allocator */

    struct header_list *next;
} header_list_t;

typedef struct {
    header_list_t *list;

    header_list_t *cur_read;
    enum {
        READ_START,     /* haven't started reading yet */
        READ_HEADER,    /* reading cur_read->header */
        READ_SEP,       /* reading ": " */
        READ_VALUE,     /* reading cur_read->value */
        READ_CRLF,      /* reading "\r\n" */
        READ_TERM,      /* reading the final "\r\n" */
        READ_DONE       /* no more data to read */
    } state;
    apr_size_t amt_read; /* how much of the current state we've read */

} headers_context_t;


SERF_DECLARE(serf_bucket_t *) serf_bucket_headers_create(
    serf_bucket_alloc_t *allocator)
{
    headers_context_t *ctx;

    ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
    ctx->list = NULL;
    ctx->state = READ_START;

    return serf_bucket_create(&serf_bucket_type_headers, allocator, ctx);
}

SERF_DECLARE(void) serf_bucket_headers_setx(
    serf_bucket_t *bkt,
    const char *header, apr_size_t header_size, int header_copy,
    const char *value, apr_size_t value_size, int value_copy)
{
    headers_context_t *ctx = bkt->data;
    header_list_t *found = ctx->list;
    header_list_t *hdr;

    /* Check to see if this header is already present. */
    while (found) {
      if (strncasecmp(found->header, header, header_size) == 0)
            break;
      found = found->next;
    }

    if (found) {

        /* The header is already present.  RFC 2616, section 4.2
           indicates that we should append the new value, separated by
           a comma.  Reasoning: for headers whose values are known to
           be comma-separated, that is clearly the correct behavior;
           for others, the correct behavior is undefined anyway. */

        /* The "+1" is for the comma; serf_bstrmemdup() will also add
           one slot for the terminating '\0'. */
        apr_size_t new_size = found->value_size + value_size + 1;
        char *new_val = serf_bucket_mem_alloc(bkt->allocator, new_size);
        memcpy(new_val, found->value, found->value_size);
        new_val[found->value_size] = ',';
        memcpy(new_val + found->value_size + 1, value, value_size);
        new_val[new_size] = '\0';
        found->value = new_val;
        found->value_size = new_size;
        found->alloc_flags |= ALLOC_VALUE;
        return;
    }

    /* Else the header is not already present.  Add it to the bucket. */
<commit_msg>  * NOP RFC 2616, section 4.2 function.<commit_after>/* Copyright 2004 <NAME> and <NAME>
 *
 * Licensed 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 <stdlib.h>

#include <apr_general.h>  /* for strcasecmp() */

#include "serf.h"
#include "serf_bucket_util.h"


typedef struct header_list {
    const char *header;
    const char *value;

    apr_size_t header_size;
    apr_size_t value_size;

    int alloc_flags;
#define ALLOC_HEADER 0x0001  /* header lives in our allocator */
#define ALLOC_VALUE  0x0002  /* value lives in our allocator */

    struct header_list *next;
} header_list_t;

typedef struct {
    header_list_t *list;

    header_list_t *cur_read;
    enum {
        READ_START,     /* haven't started reading yet */
        READ_HEADER,    /* reading cur_read->header */
        READ_SEP,       /* reading ": " */
        READ_VALUE,     /* reading cur_read->value */
        READ_CRLF,      /* reading "\r\n" */
        READ_TERM,      /* reading the final "\r\n" */
        READ_DONE       /* no more data to read */
    } state;
    apr_size_t amt_read; /* how much of the current state we've read */

} headers_context_t;


SERF_DECLARE(serf_bucket_t *) serf_bucket_headers_create(
    serf_bucket_alloc_t *allocator)
{
    headers_context_t *ctx;

    ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
    ctx->list = NULL;
    ctx->state = READ_START;

    return serf_bucket_create(&serf_bucket_type_headers, allocator, ctx);
}

SERF_DECLARE(void) serf_bucket_headers_setx(
    serf_bucket_t *bkt,
    const char *header, apr_size_t header_size, int header_copy,
    const char *value, apr_size_t value_size, int value_copy)
{
    headers_context_t *ctx = bkt->data;
    header_list_t *found = ctx->list;
    header_list_t *hdr;

#if 0 /* It is not on business of libserf for mod_chxj. A.Konno */
    /* Check to see if this header is already present. */
    while (found) {
      if (strncasecmp(found->header, header, header_size) == 0)
            break;
      found = found->next;
    }

    if (found) {

        /* The header is already present.  RFC 2616, section 4.2
           indicates that we should append the new value, separated by
           a comma.  Reasoning: for headers whose values are known to
           be comma-separated, that is clearly the correct behavior;
           for others, the correct behavior is undefined anyway. */

        /* The "+1" is for the comma; serf_bstrmemdup() will also add
           one slot for the terminating '\0'. */
        apr_size_t new_size = found->value_size + value_size + 1;
        char *new_val = serf_bucket_mem_alloc(bkt->allocator, new_size);
        memcpy(new_val, found->value, found->value_size);
        new_val[found->value_size] = ',';
        memcpy(new_val + found->value_size + 1, value, value_size);
        new_val[new_size] = '\0';
        found->value = new_val;
        found->value_size = new_size;
        found->alloc_flags |= ALLOC_VALUE;
        return;
    }
#endif

    /* Else the header is not already present.  Add it to the bucket. */

{'fraction_non_alphanumeric': 0.09826753763135473, 'fraction_numerical': 0.009514342516330588, 'mean_word_length': 2.758271077908218, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 45, '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': '2251649', 'n_tokens_mistral': 2277, 'n_tokens_neox': 2193, 'n_words': 697}