1 /*
2   Licensed to the Apache Software Foundation (ASF) under one
3   or more contributor license agreements.  See the NOTICE file
4   distributed with this work for additional information
5   regarding copyright ownership.  The ASF licenses this file
6   to you under the Apache License, Version 2.0 (the
7   "License"); you may not use this file except in compliance
8   with the License.  You may obtain a copy of the License at
9 
10   http://www.apache.org/licenses/LICENSE-2.0
11 
12   Unless required by applicable law or agreed to in writing, software
13   distributed under the License is distributed on an "AS IS" BASIS,
14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   See the License for the specific language governing permissions and
16   limitations under the License.
17 */
18 
19 #pragma once
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <inttypes.h>
25 
26 #include "lua.h"
27 #include "lualib.h"
28 #include "lauxlib.h"
29 
30 #include <ts/ts.h>
31 #include <ts/experimental.h>
32 #include <ts/remap.h>
33 #include "ts_lua_coroutine.h"
34 
35 #define TS_LUA_FUNCTION_REMAP "do_remap"
36 #define TS_LUA_FUNCTION_OS_RESPONSE "do_os_response"
37 #define TS_LUA_FUNCTION_CACHE_LOOKUP_COMPLETE "do_cache_lookup_complete"
38 #define TS_LUA_FUNCTION_SEND_REQUEST "do_send_request"
39 #define TS_LUA_FUNCTION_READ_RESPONSE "do_read_response"
40 #define TS_LUA_FUNCTION_SEND_RESPONSE "do_send_response"
41 #define TS_LUA_FUNCTION_READ_REQUEST "do_read_request"
42 #define TS_LUA_FUNCTION_TXN_START "do_txn_start"
43 #define TS_LUA_FUNCTION_PRE_REMAP "do_pre_remap"
44 #define TS_LUA_FUNCTION_POST_REMAP "do_post_remap"
45 #define TS_LUA_FUNCTION_OS_DNS "do_os_dns"
46 #define TS_LUA_FUNCTION_READ_CACHE "do_read_cache"
47 #define TS_LUA_FUNCTION_TXN_CLOSE "do_txn_close"
48 
49 #define TS_LUA_FUNCTION_G_SEND_REQUEST "do_global_send_request"
50 #define TS_LUA_FUNCTION_G_READ_REQUEST "do_global_read_request"
51 #define TS_LUA_FUNCTION_G_SEND_RESPONSE "do_global_send_response"
52 #define TS_LUA_FUNCTION_G_READ_RESPONSE "do_global_read_response"
53 #define TS_LUA_FUNCTION_G_CACHE_LOOKUP_COMPLETE "do_global_cache_lookup_complete"
54 #define TS_LUA_FUNCTION_G_TXN_START "do_global_txn_start"
55 #define TS_LUA_FUNCTION_G_PRE_REMAP "do_global_pre_remap"
56 #define TS_LUA_FUNCTION_G_POST_REMAP "do_global_post_remap"
57 #define TS_LUA_FUNCTION_G_OS_DNS "do_global_os_dns"
58 #define TS_LUA_FUNCTION_G_READ_CACHE "do_global_read_cache"
59 #define TS_LUA_FUNCTION_G_TXN_CLOSE "do_global_txn_close"
60 
61 #define TS_LUA_DEBUG_TAG "ts_lua"
62 
63 #define TS_LUA_EVENT_COROUTINE_CONT 20000
64 
65 #define TS_LUA_MAX_SCRIPT_FNAME_LENGTH 1024
66 #define TS_LUA_MAX_CONFIG_VARS_COUNT 256
67 #define TS_LUA_MAX_SHARED_DICT_NAME_LENGTH 128
68 #define TS_LUA_MAX_SHARED_DICT_COUNT 32
69 #define TS_LUA_MAX_URL_LENGTH 2048
70 #define TS_LUA_MAX_OVEC_SIZE (3 * 32)
71 #define TS_LUA_MAX_RESIDENT_PCRE 64
72 #define TS_LUA_MAX_STR_LENGTH 2048
73 
74 #define TS_LUA_MIN_ALIGN sizeof(void *)
75 #define TS_LUA_MEM_ALIGN(size) (((size) + ((TS_LUA_MIN_ALIGN)-1)) & ~((TS_LUA_MIN_ALIGN)-1))
76 #define TS_LUA_ALIGN_COUNT(size) (size / TS_LUA_MIN_ALIGN)
77 
78 #define TS_LUA_MAKE_VAR_ITEM(X) \
79   {                             \
80     X, #X                       \
81   }
82 
83 /* for http config or cntl var */
84 typedef struct {
85   int nvar;
86   char *svar;
87 } ts_lua_var_item;
88 
89 typedef struct {
90   char *content;
91   char script[TS_LUA_MAX_SCRIPT_FNAME_LENGTH];
92   void *conf_vars[TS_LUA_MAX_CONFIG_VARS_COUNT];
93 
94   unsigned int _first : 1; // create current instance for 1st ts_lua_main_ctx
95   unsigned int _last : 1;  // create current instance for the last ts_lua_main_ctx
96 
97   int remap;
98   int states;
99 
100   int init_func;
101 } ts_lua_instance_conf;
102 
103 /* lua state for http request */
104 typedef struct {
105   ts_lua_cont_info cinfo;
106 
107   TSHttpTxn txnp;
108   TSMBuffer client_request_bufp;
109   TSMLoc client_request_hdrp;
110   TSMLoc client_request_url;
111 
112   TSMBuffer server_request_bufp;
113   TSMLoc server_request_hdrp;
114   TSMLoc server_request_url;
115 
116   TSMBuffer server_response_bufp;
117   TSMLoc server_response_hdrp;
118 
119   TSMBuffer client_response_bufp;
120   TSMLoc client_response_hdrp;
121 
122   TSMBuffer cached_response_bufp;
123   TSMLoc cached_response_hdrp;
124 
125   ts_lua_instance_conf *instance_conf;
126 
127   int has_hook;
128 
129   TSRemapRequestInfo *rri;
130 
131 } ts_lua_http_ctx;
132 
133 typedef struct {
134   TSVIO vio;
135   TSIOBuffer buffer;
136   TSIOBufferReader reader;
137 } ts_lua_io_handle;
138 
139 typedef struct {
140   ts_lua_cont_info cinfo;
141 
142   ts_lua_io_handle output;
143   ts_lua_io_handle reserved;
144 
145   ts_lua_http_ctx *hctx;
146   int64_t upstream_bytes;
147   int64_t upstream_watermark_bytes;
148   int64_t downstream_bytes;
149   int64_t total;
150 
151 } ts_lua_http_transform_ctx;
152 
153 typedef struct {
154   ts_lua_cont_info cinfo;
155 
156   ts_lua_io_handle input;
157   ts_lua_io_handle output;
158 
159   TSVConn net_vc;
160   ts_lua_http_ctx *hctx;
161 
162   int64_t to_flush;
163   unsigned int reuse : 1;
164   unsigned int recv_complete : 1;
165   unsigned int send_complete : 1;
166   unsigned int all_ready : 1;
167 } ts_lua_http_intercept_ctx;
168 
169 #define TS_LUA_RELEASE_IO_HANDLE(ih)    \
170   do {                                  \
171     if (ih->reader) {                   \
172       TSIOBufferReaderFree(ih->reader); \
173       ih->reader = NULL;                \
174     }                                   \
175     if (ih->buffer) {                   \
176       TSIOBufferDestroy(ih->buffer);    \
177       ih->buffer = NULL;                \
178     }                                   \
179   } while (0)
180 
181 #ifndef ATS_UNUSED
182 #define ATS_UNUSED __attribute__((unused))
183 #endif
184