<commit_before>/**
 * @copyright
 * Copyright (c) 2016, <NAME>. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <assert.h>
#include <http_parser.h>
#include <dfk.h>
#include <dfk/internal.h>


typedef struct dfk__http_header_t {
  dfk_avltree_hook_t hook;
  dfk_buf_t name;
  dfk_buf_t value;
} dfk__http_header_t;


static void dfk__http_header_init(dfk__http_header_t* header)
{
  assert(header);
  dfk_avltree_hook_init(&header->hook);
  header->name = (dfk_buf_t) {NULL, 0};
  header->value = (dfk_buf_t) {NULL, 0};
}


static int dfk__http_header_lookup_cmp(dfk_avltree_hook_t* l, void* r)
{
  dfk__http_header_t* lh = (dfk__http_header_t*) l;
  dfk_buf_t* rh = (dfk_buf_t*) r;
  size_t tocmp = DFK_MIN(lh->name.size, rh->size);
  int res = strncmp(lh->name.data, rh->data, tocmp);
  if (res) {
    return res;
  } else {
    return lh->name.size > rh->size;
  }
  return res;
}


static int dfk__http_header_cmp(dfk_avltree_hook_t* l, dfk_avltree_hook_t* r)
{
  return dfk__http_header_lookup_cmp(l, &((dfk__http_header_t*) r)->name);
}


typedef dfk__http_header_t dfk_http_argument_t;


static void dfk__http_req_init(dfk_http_req_t* req, dfk_arena_t* arena, dfk_tcp_socket_t* sock)
{
  assert(req);
  assert(arena);
  assert(sock);
  req->_.arena = arena;
  req->_.sock = sock;
  dfk_avltree_init(&req->_.headers, dfk__http_header_cmp);
  dfk_avltree_init(&req->_.arguments, dfk__http_header_cmp);
  req->_.bodypart = (dfk_buf_t) {NULL, 0};
  req->url = (dfk_buf_t) {NULL, 0};
  req->user_agent = (dfk_buf_t) {NULL, 0};
  req->host = (dfk_buf_t) {NULL, 0};
  req->content_type = (dfk_buf_t) {NULL, 0};
  req->content_length = 0;
}


static void dfk__http_req_free(dfk_http_req_t* req)
{
  dfk_avltree_free(&req->_.arguments);
  dfk_avltree_free(&req->_.headers);
}


static void dfk__http_resp_init(dfk_http_resp_t* resp, dfk_arena_t* arena, dfk_tcp_socket_t* sock)
{
  assert(resp);
  assert(arena);
  assert(sock);
  resp->_.arena = arena;
  resp->_.sock = sock;
  dfk_avltree_init(&resp->_.headers, dfk__http_header_cmp);
}


static void dfk__http_resp_free(dfk_http_resp_t* resp)
{
  assert(resp);
  DFK_UNUSED(resp);
}


typedef struct dfk__http_parser_data_t {
  dfk_t* dfk;
  dfk_http_req_t* req;
  dfk__http_header_t* cheader; /* current header */
  int done;
} dfk__http_parser_data_t;


static void dfk__http_parser_data_init(dfk__http_parser_data_t* pdata, dfk_t* dfk, dfk_http_req_t* req)
{
  pdata->dfk = dfk;
  pdata->req = req;
  pdata->done = 0;
}


static void dfk__http_parser_data_free(dfk__http_parser_data_t* pdata)
{
  DFK_UNUSED(pdata);
}


static void dfk__buf_append(dfk_buf_t* to, const char* data, size_t size)
{<commit_msg>http: Initialize dfk__http_parser_data_t.cheader<commit_after>/**
 * @copyright
 * Copyright (c) 2016, <NAME>. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <assert.h>
#include <http_parser.h>
#include <dfk.h>
#include <dfk/internal.h>


typedef struct dfk__http_header_t {
  dfk_avltree_hook_t hook;
  dfk_buf_t name;
  dfk_buf_t value;
} dfk__http_header_t;


static void dfk__http_header_init(dfk__http_header_t* header)
{
  assert(header);
  dfk_avltree_hook_init(&header->hook);
  header->name = (dfk_buf_t) {NULL, 0};
  header->value = (dfk_buf_t) {NULL, 0};
}


static int dfk__http_header_lookup_cmp(dfk_avltree_hook_t* l, void* r)
{
  dfk__http_header_t* lh = (dfk__http_header_t*) l;
  dfk_buf_t* rh = (dfk_buf_t*) r;
  size_t tocmp = DFK_MIN(lh->name.size, rh->size);
  int res = strncmp(lh->name.data, rh->data, tocmp);
  if (res) {
    return res;
  } else {
    return lh->name.size > rh->size;
  }
  return res;
}


static int dfk__http_header_cmp(dfk_avltree_hook_t* l, dfk_avltree_hook_t* r)
{
  return dfk__http_header_lookup_cmp(l, &((dfk__http_header_t*) r)->name);
}


typedef dfk__http_header_t dfk_http_argument_t;


static void dfk__http_req_init(dfk_http_req_t* req, dfk_arena_t* arena, dfk_tcp_socket_t* sock)
{
  assert(req);
  assert(arena);
  assert(sock);
  req->_.arena = arena;
  req->_.sock = sock;
  dfk_avltree_init(&req->_.headers, dfk__http_header_cmp);
  dfk_avltree_init(&req->_.arguments, dfk__http_header_cmp);
  req->_.bodypart = (dfk_buf_t) {NULL, 0};
  req->url = (dfk_buf_t) {NULL, 0};
  req->user_agent = (dfk_buf_t) {NULL, 0};
  req->host = (dfk_buf_t) {NULL, 0};
  req->content_type = (dfk_buf_t) {NULL, 0};
  req->content_length = 0;
}


static void dfk__http_req_free(dfk_http_req_t* req)
{
  dfk_avltree_free(&req->_.arguments);
  dfk_avltree_free(&req->_.headers);
}


static void dfk__http_resp_init(dfk_http_resp_t* resp, dfk_arena_t* arena, dfk_tcp_socket_t* sock)
{
  assert(resp);
  assert(arena);
  assert(sock);
  resp->_.arena = arena;
  resp->_.sock = sock;
  dfk_avltree_init(&resp->_.headers, dfk__http_header_cmp);
}


static void dfk__http_resp_free(dfk_http_resp_t* resp)
{
  assert(resp);
  DFK_UNUSED(resp);
}


typedef struct dfk__http_parser_data_t {
  dfk_t* dfk;
  dfk_http_req_t* req;
  dfk__http_header_t* cheader; /* current header */
  int done;
} dfk__http_parser_data_t;


static void dfk__http_parser_data_init(dfk__http_parser_data_t* pdata, dfk_t* dfk, dfk_http_req_t* req)
{
  pdata->dfk = dfk;
  pdata->req = req;
  pdata->cheader = NULL;
  pdata->done = 0;
}


static void dfk__http_parser_data_free(dfk__http_parser_data_t* pdata)
{
  DFK_UNUSED(pdata);
}


static void dfk__buf_append(dfk_buf_t* to, const char* data, size_t size)
{
{'fraction_non_alphanumeric': 0.10510435001257229, 'fraction_numerical': 0.003268795574553684, 'mean_word_length': 4.875184638109306, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '164026', 'n_tokens_mistral': 3094, 'n_tokens_neox': 2933, 'n_words': 768}