<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 <ts/ts.h>
#include <ts/remap.h>
#include <string.h>
#include "lapi.h"
#include "lutil.h"
#include "hook.h"
#include "state.h"

#include <memory> // placement new

#include "ink_config.h"
#include "ink_defs.h"

const char *
HttpHookName(TSHttpHookID hookid)
{
  static const char * names[TS_HTTP_LAST_HOOK] = {
    "HTTP_READ_REQUEST_HDR_HOOK",
    "HTTP_OS_DNS_HOOK",
    "HTTP_SEND_REQUEST_HDR_HOOK",
    "HTTP_READ_CACHE_HDR_HOOK",
    "HTTP_READ_RESPONSE_HDR_HOOK",
    "HTTP_SEND_RESPONSE_HDR_HOOK",
    NULL, // XXX TS_HTTP_REQUEST_TRANSFORM_HOOK
    NULL, // XXX TS_HTTP_RESPONSE_TRANSFORM_HOOK
    NULL, // XXX HTTP_SELECT_ALT_HOOK
    "HTTP_TXN_START_HOOK",
    "HTTP_TXN_CLOSE_HOOK",
    "HTTP_SSN_START_HOOK",
    "HTTP_SSN_CLOSE_HOOK",
    "HTTP_CACHE_LOOKUP_COMPLETE_HOOK",
    "HTTP_PRE_REMAP_HOOK",
    "HTTP_POST_REMAP_HOOK",
  };

  if (hookid >= 0 && hookid < static_cast<TSHttpHookID>(countof(names))) {
    return names[hookid];
  }

  return NULL;
}

static bool
HookIsValid(int hookid)
{
  if (hookid == TS_HTTP_REQUEST_TRANSFORM_HOOK || hookid == TS_HTTP_RESPONSE_TRANSFORM_HOOK) {
    return false;
  }

  return hookid >= 0 && hookid < TS_HTTP_LAST_HOOK;
}

static void
LuaPushEventData(lua_State * lua, TSEvent event, void * edata)
{
  switch (event) {
  case TS_EVENT_HTTP_READ_REQUEST_HDR:
  case TS_EVENT_HTTP_OS_DNS:
  case TS_EVENT_HTTP_SEND_REQUEST_HDR:
  case TS_EVENT_HTTP_READ_CACHE_HDR:
  case TS_EVENT_HTTP_READ_RESPONSE_HDR:
  case TS_EVENT_HTTP_SEND_RESPONSE_HDR:
  case TS_EVENT_HTTP_SELECT_ALT:
  case TS_EVENT_HTTP_TXN_START:
  case TS_EVENT_HTTP_TXN_CLOSE:
  case TS_EVENT_CACHE_LOOKUP_COMPLETE:
  case TS_EVENT_HTTP_CACHE_LOOKUP_COMPLETE:
  case TS_EVENT_HTTP_PRE_REMAP:
  case TS_EVENT_HTTP_POST_REMAP:
    LuaPushHttpTransaction(lua, (TSHttpTxn)edata);
    break;
  case TS_EVENT_HTTP_SSN_START:
  case TS_EVENT_HTTP_SSN_CLOSE:
    LuaPushHttpSession(lua, (TSHttpSsn)edata);
    break;
  default:
    lua_pushnil(lua);
  }
}


// For 64-bit pointers, we can inline the LuaHookReference, otherwise we need an extra malloc.
//
#if SIZEOF_VOID_POINTER >= 8
#define INLINE_LUA_HOOK_REFERENCE 1
#else
#undef INLINE_LUA_HOOK_REFERENCE
#endif

template <typename t1, typename t2>
struct inline_tuple
{
  typedef t1 first_type;
  typedef t2 second_type;
  typedef inline_tuple<first_type, second_type> this_type;

  union {
    struct {
      first_type first;
      second_type second;
    } s;
    void * ptr;
  } storage;

  first_type& first() { return storage.s.first; }
  second_type& second() { return storage.s.second; }

  static void * allocate(const first_type first, const second_type second) {
#if defined(INLINE_LUA_HOOK_REFERENCE)
    typedef char __size_check[sizeof(this_type) == sizeof(void *) ? 0 : -1];

    this_type obj;
    obj.first() = first;
    obj.second() = second;
    return obj.storage.ptr;
#else
    this_type * ptr = (this_type *)TSmalloc(sizeof(this_type));
    ptr->first() = first;
    ptr->second() = second;
    return ptr;
#endif
  }

  static void free(void *ptr ATS_UNUSED) {
#if defined(INLINE_LUA_HOOK_REFERENCE)
    // Nothing to do, because we never allocated.
#else
    TSfree(ptr);
#endif
  }
<commit_msg>Remove unused typedef to make Lua compile<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 <ts/ts.h>
#include <ts/remap.h>
#include <string.h>
#include "lapi.h"
#include "lutil.h"
#include "hook.h"
#include "state.h"

#include <memory> // placement new

#include "ink_config.h"
#include "ink_defs.h"

const char *
HttpHookName(TSHttpHookID hookid)
{
  static const char * names[TS_HTTP_LAST_HOOK] = {
    "HTTP_READ_REQUEST_HDR_HOOK",
    "HTTP_OS_DNS_HOOK",
    "HTTP_SEND_REQUEST_HDR_HOOK",
    "HTTP_READ_CACHE_HDR_HOOK",
    "HTTP_READ_RESPONSE_HDR_HOOK",
    "HTTP_SEND_RESPONSE_HDR_HOOK",
    NULL, // XXX TS_HTTP_REQUEST_TRANSFORM_HOOK
    NULL, // XXX TS_HTTP_RESPONSE_TRANSFORM_HOOK
    NULL, // XXX HTTP_SELECT_ALT_HOOK
    "HTTP_TXN_START_HOOK",
    "HTTP_TXN_CLOSE_HOOK",
    "HTTP_SSN_START_HOOK",
    "HTTP_SSN_CLOSE_HOOK",
    "HTTP_CACHE_LOOKUP_COMPLETE_HOOK",
    "HTTP_PRE_REMAP_HOOK",
    "HTTP_POST_REMAP_HOOK",
  };

  if (hookid >= 0 && hookid < static_cast<TSHttpHookID>(countof(names))) {
    return names[hookid];
  }

  return NULL;
}

static bool
HookIsValid(int hookid)
{
  if (hookid == TS_HTTP_REQUEST_TRANSFORM_HOOK || hookid == TS_HTTP_RESPONSE_TRANSFORM_HOOK) {
    return false;
  }

  return hookid >= 0 && hookid < TS_HTTP_LAST_HOOK;
}

static void
LuaPushEventData(lua_State * lua, TSEvent event, void * edata)
{
  switch (event) {
  case TS_EVENT_HTTP_READ_REQUEST_HDR:
  case TS_EVENT_HTTP_OS_DNS:
  case TS_EVENT_HTTP_SEND_REQUEST_HDR:
  case TS_EVENT_HTTP_READ_CACHE_HDR:
  case TS_EVENT_HTTP_READ_RESPONSE_HDR:
  case TS_EVENT_HTTP_SEND_RESPONSE_HDR:
  case TS_EVENT_HTTP_SELECT_ALT:
  case TS_EVENT_HTTP_TXN_START:
  case TS_EVENT_HTTP_TXN_CLOSE:
  case TS_EVENT_CACHE_LOOKUP_COMPLETE:
  case TS_EVENT_HTTP_CACHE_LOOKUP_COMPLETE:
  case TS_EVENT_HTTP_PRE_REMAP:
  case TS_EVENT_HTTP_POST_REMAP:
    LuaPushHttpTransaction(lua, (TSHttpTxn)edata);
    break;
  case TS_EVENT_HTTP_SSN_START:
  case TS_EVENT_HTTP_SSN_CLOSE:
    LuaPushHttpSession(lua, (TSHttpSsn)edata);
    break;
  default:
    lua_pushnil(lua);
  }
}


// For 64-bit pointers, we can inline the LuaHookReference, otherwise we need an extra malloc.
//
#if SIZEOF_VOID_POINTER >= 8
#define INLINE_LUA_HOOK_REFERENCE 1
#else
#undef INLINE_LUA_HOOK_REFERENCE
#endif

template <typename t1, typename t2>
struct inline_tuple
{
  typedef t1 first_type;
  typedef t2 second_type;
  typedef inline_tuple<first_type, second_type> this_type;

  union {
    struct {
      first_type first;
      second_type second;
    } s;
    void * ptr;
  } storage;

  first_type& first() { return storage.s.first; }
  second_type& second() { return storage.s.second; }

  static void * allocate(const first_type first, const second_type second) {
#if defined(INLINE_LUA_HOOK_REFERENCE)
    this_type obj;
    obj.first() = first;
    obj.second() = second;
    return obj.storage.ptr;
#else
    this_type * ptr = (this_type *)TSmalloc(sizeof(this_type));
    ptr->first() = first;
    ptr->second() = second;
    return ptr;
#endif
  }

  static void free(void *ptr ATS_UNUSED) {
#if defined(INLINE_LUA_HOOK_REFERENCE)
    // Nothing to do, because we never allocated.
#else
    TSfree(ptr);
#endif
  }

{'fraction_non_alphanumeric': 0.0869401107196779, 'fraction_numerical': 0.00377453447408153, 'mean_word_length': 4.338482202820685, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 27, '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': '2341604', 'n_tokens_mistral': 2982, 'n_tokens_neox': 2907, 'n_words': 700}