File: ts_lua_common.h

package info (click to toggle)
trafficserver 9.2.5%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,008 kB
  • sloc: cpp: 345,484; ansic: 31,134; python: 24,200; sh: 7,271; makefile: 3,045; perl: 2,261; java: 277; pascal: 119; sql: 94; xml: 2
file content (202 lines) | stat: -rw-r--r-- 5,639 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
  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.
*/

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

#include <ts/ts.h>
#include <ts/experimental.h>
#include <ts/remap.h>
#include "ts_lua_coroutine.h"

#define TS_LUA_FUNCTION_REMAP "do_remap"
#define TS_LUA_FUNCTION_OS_RESPONSE "do_os_response"
#define TS_LUA_FUNCTION_CACHE_LOOKUP_COMPLETE "do_cache_lookup_complete"
#define TS_LUA_FUNCTION_SEND_REQUEST "do_send_request"
#define TS_LUA_FUNCTION_READ_RESPONSE "do_read_response"
#define TS_LUA_FUNCTION_SEND_RESPONSE "do_send_response"
#define TS_LUA_FUNCTION_READ_REQUEST "do_read_request"
#define TS_LUA_FUNCTION_TXN_START "do_txn_start"
#define TS_LUA_FUNCTION_PRE_REMAP "do_pre_remap"
#define TS_LUA_FUNCTION_POST_REMAP "do_post_remap"
#define TS_LUA_FUNCTION_OS_DNS "do_os_dns"
#define TS_LUA_FUNCTION_READ_CACHE "do_read_cache"
#define TS_LUA_FUNCTION_TXN_CLOSE "do_txn_close"

#define TS_LUA_FUNCTION_G_SEND_REQUEST "do_global_send_request"
#define TS_LUA_FUNCTION_G_READ_REQUEST "do_global_read_request"
#define TS_LUA_FUNCTION_G_SEND_RESPONSE "do_global_send_response"
#define TS_LUA_FUNCTION_G_READ_RESPONSE "do_global_read_response"
#define TS_LUA_FUNCTION_G_CACHE_LOOKUP_COMPLETE "do_global_cache_lookup_complete"
#define TS_LUA_FUNCTION_G_TXN_START "do_global_txn_start"
#define TS_LUA_FUNCTION_G_PRE_REMAP "do_global_pre_remap"
#define TS_LUA_FUNCTION_G_POST_REMAP "do_global_post_remap"
#define TS_LUA_FUNCTION_G_OS_DNS "do_global_os_dns"
#define TS_LUA_FUNCTION_G_READ_CACHE "do_global_read_cache"
#define TS_LUA_FUNCTION_G_TXN_CLOSE "do_global_txn_close"

// TLS hooks can only be global
#define TS_LUA_FUNCTION_G_VCONN_START "do_global_vconn_start"

#define TS_LUA_DEBUG_TAG "ts_lua"

#define TS_LUA_EVENT_COROUTINE_CONT 20000

#define TS_LUA_MAX_SCRIPT_FNAME_LENGTH 1024
#define TS_LUA_MAX_CONFIG_VARS_COUNT 256
#define TS_LUA_MAX_SHARED_DICT_NAME_LENGTH 128
#define TS_LUA_MAX_SHARED_DICT_COUNT 32
#define TS_LUA_MAX_URL_LENGTH 2048
#define TS_LUA_MAX_OVEC_SIZE (3 * 32)
#define TS_LUA_MAX_RESIDENT_PCRE 64
#define TS_LUA_MAX_STR_LENGTH 2048

#define TS_LUA_MIN_ALIGN sizeof(void *)
#define TS_LUA_MEM_ALIGN(size) (((size) + ((TS_LUA_MIN_ALIGN)-1)) & ~((TS_LUA_MIN_ALIGN)-1))
#define TS_LUA_ALIGN_COUNT(size) (size / TS_LUA_MIN_ALIGN)

#define TS_LUA_MAKE_VAR_ITEM(X) \
  {                             \
    X, #X                       \
  }

/* for http config or cntl var */
typedef struct {
  int nvar;
  char *svar;
} ts_lua_var_item;

typedef struct {
  char *content;
  char script[TS_LUA_MAX_SCRIPT_FNAME_LENGTH];
  void *conf_vars[TS_LUA_MAX_CONFIG_VARS_COUNT];

  unsigned int _first : 1; // create current instance for 1st ts_lua_main_ctx
  unsigned int _last : 1;  // create current instance for the last ts_lua_main_ctx

  int remap;
  int states;
  int ljgc;
  int ref_count;

  int init_func;
} ts_lua_instance_conf;

/* lua state for vconn */
typedef struct {
  int ref;

  ts_lua_main_ctx *mctx;

  lua_State *lua;

  TSVConn vconn;

  ts_lua_instance_conf *instance_conf;

} ts_lua_vconn_ctx;

/* lua state for http request */
typedef struct {
  ts_lua_cont_info cinfo;

  TSHttpTxn txnp;
  TSMBuffer client_request_bufp;
  TSMLoc client_request_hdrp;
  TSMLoc client_request_url;

  TSMBuffer server_request_bufp;
  TSMLoc server_request_hdrp;
  TSMLoc server_request_url;

  TSMBuffer server_response_bufp;
  TSMLoc server_response_hdrp;

  TSMBuffer client_response_bufp;
  TSMLoc client_response_hdrp;

  TSMBuffer cached_response_bufp;
  TSMLoc cached_response_hdrp;

  ts_lua_instance_conf *instance_conf;

  int has_hook;

  TSRemapRequestInfo *rri;

} ts_lua_http_ctx;

typedef struct {
  TSVIO vio;
  TSIOBuffer buffer;
  TSIOBufferReader reader;
} ts_lua_io_handle;

typedef struct {
  ts_lua_cont_info cinfo;

  ts_lua_io_handle output;
  ts_lua_io_handle reserved;

  ts_lua_http_ctx *hctx;
  int64_t upstream_bytes;
  int64_t upstream_watermark_bytes;
  int64_t downstream_bytes;
  int64_t total;

} ts_lua_http_transform_ctx;

typedef struct {
  ts_lua_cont_info cinfo;

  ts_lua_io_handle input;
  ts_lua_io_handle output;

  TSVConn net_vc;
  ts_lua_http_ctx *hctx;

  int64_t to_flush;
  unsigned int reuse : 1;
  unsigned int recv_complete : 1;
  unsigned int send_complete : 1;
  unsigned int all_ready : 1;
} ts_lua_http_intercept_ctx;

#define TS_LUA_RELEASE_IO_HANDLE(ih)    \
  do {                                  \
    if (ih->reader) {                   \
      TSIOBufferReaderFree(ih->reader); \
      ih->reader = NULL;                \
    }                                   \
    if (ih->buffer) {                   \
      TSIOBufferDestroy(ih->buffer);    \
      ih->buffer = NULL;                \
    }                                   \
  } while (0)

#ifndef ATS_UNUSED
#define ATS_UNUSED __attribute__((unused))
#endif