You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by ki...@apache.org on 2014/05/22 22:39:33 UTC
[2/3] TS-2723: add new features to ts_lua plugin
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/83f16762/plugins/experimental/ts_lua/ts_lua_client_request.c
----------------------------------------------------------------------
diff --git a/plugins/experimental/ts_lua/ts_lua_client_request.c b/plugins/experimental/ts_lua/ts_lua_client_request.c
index 19aa2f0..d45e6e0 100644
--- a/plugins/experimental/ts_lua/ts_lua_client_request.c
+++ b/plugins/experimental/ts_lua/ts_lua_client_request.c
@@ -18,6 +18,7 @@
#include "ink_platform.h"
+#include <netinet/in.h>
#include "ts_lua_util.h"
static void ts_lua_inject_client_request_client_addr_api(lua_State * L);
@@ -25,23 +26,36 @@ static void ts_lua_inject_client_request_server_addr_api(lua_State * L);
static int ts_lua_client_request_header_get(lua_State * L);
static int ts_lua_client_request_header_set(lua_State * L);
+static int ts_lua_client_request_get_headers(lua_State * L);
static int ts_lua_client_request_get_url(lua_State * L);
static int ts_lua_client_request_get_pristine_url(lua_State * L);
+static int ts_lua_client_request_get_url_host(lua_State * L);
+static int ts_lua_client_request_set_url_host(lua_State * L);
+static int ts_lua_client_request_get_url_port(lua_State * L);
+static int ts_lua_client_request_set_url_port(lua_State * L);
+static int ts_lua_client_request_get_url_scheme(lua_State * L);
+static int ts_lua_client_request_set_url_scheme(lua_State * L);
+
static int ts_lua_client_request_get_uri(lua_State * L);
static int ts_lua_client_request_set_uri(lua_State * L);
static int ts_lua_client_request_set_uri_args(lua_State * L);
static int ts_lua_client_request_get_uri_args(lua_State * L);
static int ts_lua_client_request_get_method(lua_State * L);
static int ts_lua_client_request_set_method(lua_State * L);
+static int ts_lua_client_request_get_version(lua_State * L);
+static int ts_lua_client_request_set_version(lua_State * L);
+
static int ts_lua_client_request_get_body_size(lua_State * L);
static int ts_lua_client_request_get_header_size(lua_State * L);
static void ts_lua_inject_client_request_socket_api(lua_State * L);
static void ts_lua_inject_client_request_header_api(lua_State * L);
+static void ts_lua_inject_client_request_headers_api(lua_State * L);
static void ts_lua_inject_client_request_url_api(lua_State * L);
static void ts_lua_inject_client_request_uri_api(lua_State * L);
static void ts_lua_inject_client_request_args_api(lua_State * L);
static void ts_lua_inject_client_request_method_api(lua_State * L);
+static void ts_lua_inject_client_request_version_api(lua_State * L);
static void ts_lua_inject_client_request_body_size_api(lua_State * L);
static void ts_lua_inject_client_request_header_size_api(lua_State * L);
@@ -58,10 +72,12 @@ ts_lua_inject_client_request_api(lua_State * L)
ts_lua_inject_client_request_socket_api(L);
ts_lua_inject_client_request_header_api(L);
+ ts_lua_inject_client_request_headers_api(L);
ts_lua_inject_client_request_url_api(L);
ts_lua_inject_client_request_uri_api(L);
ts_lua_inject_client_request_args_api(L);
ts_lua_inject_client_request_method_api(L);
+ ts_lua_inject_client_request_version_api(L);
ts_lua_inject_client_request_body_size_api(L);
ts_lua_inject_client_request_header_size_api(L);
@@ -207,6 +223,52 @@ ts_lua_client_request_header_set(lua_State * L)
}
static void
+ts_lua_inject_client_request_headers_api(lua_State * L)
+{
+ lua_pushcfunction(L, ts_lua_client_request_get_headers);
+ lua_setfield(L, -2, "get_headers");
+}
+
+static int
+ts_lua_client_request_get_headers(lua_State * L)
+{
+ const char *name;
+ const char *value;
+ int name_len;
+ int value_len;
+ TSMLoc field_loc;
+ TSMLoc next_field_loc;
+
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ lua_newtable(L);
+
+ field_loc = TSMimeHdrFieldGet(http_ctx->client_request_bufp, http_ctx->client_request_hdrp, 0);
+
+ while (field_loc) {
+
+ name = TSMimeHdrFieldNameGet(http_ctx->client_request_bufp, http_ctx->client_request_hdrp, field_loc, &name_len);
+ if (name && name_len) {
+
+ value =
+ TSMimeHdrFieldValueStringGet(http_ctx->client_request_bufp, http_ctx->client_request_hdrp, field_loc, -1,
+ &value_len);
+ lua_pushlstring(L, name, name_len);
+ lua_pushlstring(L, value, value_len);
+ lua_rawset(L, -3);
+ }
+
+ next_field_loc = TSMimeHdrFieldNext(http_ctx->client_request_bufp, http_ctx->client_request_hdrp, field_loc);
+ TSHandleMLocRelease(http_ctx->client_request_bufp, http_ctx->client_request_hdrp, field_loc);
+ field_loc = next_field_loc;
+ }
+
+ return 1;
+}
+
+static void
ts_lua_inject_client_request_url_api(lua_State * L)
{
lua_pushcfunction(L, ts_lua_client_request_get_url);
@@ -214,6 +276,21 @@ ts_lua_inject_client_request_url_api(lua_State * L)
lua_pushcfunction(L, ts_lua_client_request_get_pristine_url);
lua_setfield(L, -2, "get_pristine_url");
+
+ lua_pushcfunction(L, ts_lua_client_request_get_url_host);
+ lua_setfield(L, -2, "get_url_host");
+ lua_pushcfunction(L, ts_lua_client_request_set_url_host);
+ lua_setfield(L, -2, "set_url_host");
+
+ lua_pushcfunction(L, ts_lua_client_request_get_url_port);
+ lua_setfield(L, -2, "get_url_port");
+ lua_pushcfunction(L, ts_lua_client_request_set_url_port);
+ lua_setfield(L, -2, "set_url_port");
+
+ lua_pushcfunction(L, ts_lua_client_request_get_url_scheme);
+ lua_setfield(L, -2, "get_url_scheme");
+ lua_pushcfunction(L, ts_lua_client_request_set_url_scheme);
+ lua_setfield(L, -2, "set_url_scheme");
}
static void
@@ -281,6 +358,106 @@ ts_lua_client_request_get_pristine_url(lua_State * L)
}
static int
+ts_lua_client_request_get_url_host(lua_State * L)
+{
+ const char *host;
+ int len;
+
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ host = TSUrlHostGet(http_ctx->client_request_bufp, http_ctx->client_request_url, &len);
+
+ lua_pushlstring(L, host, len);
+
+ return 1;
+}
+
+static int
+ts_lua_client_request_set_url_host(lua_State * L)
+{
+ const char *host;
+ size_t len;
+
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ host = luaL_checklstring(L, 1, &len);
+
+ TSUrlHostSet(http_ctx->client_request_bufp, http_ctx->client_request_url, host, len);
+
+ return 0;
+}
+
+static int
+ts_lua_client_request_get_url_port(lua_State * L)
+{
+ int port;
+
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ port = TSUrlPortGet(http_ctx->client_request_bufp, http_ctx->client_request_url);
+
+ lua_pushnumber(L, port);
+
+ return 1;
+}
+
+static int
+ts_lua_client_request_set_url_port(lua_State * L)
+{
+ int port;
+
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ port = luaL_checkint(L, 1);
+
+ TSUrlPortSet(http_ctx->client_request_bufp, http_ctx->client_request_url, port);
+
+ return 0;
+}
+
+static int
+ts_lua_client_request_get_url_scheme(lua_State * L)
+{
+ const char *scheme;
+ int len;
+
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ scheme = TSUrlSchemeGet(http_ctx->client_request_bufp, http_ctx->client_request_url, &len);
+
+ lua_pushlstring(L, scheme, len);
+
+ return 1;
+}
+
+static int
+ts_lua_client_request_set_url_scheme(lua_State * L)
+{
+ const char *scheme;
+ size_t len;
+
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ scheme = luaL_checklstring(L, 1, &len);
+
+ TSUrlSchemeSet(http_ctx->client_request_bufp, http_ctx->client_request_url, scheme, len);
+
+ return 0;
+}
+
+static int
ts_lua_client_request_get_uri(lua_State * L)
{
char uri[TS_LUA_MAX_URL_LENGTH];
@@ -537,6 +714,55 @@ ts_lua_client_request_get_body_size(lua_State * L)
}
static void
+ts_lua_inject_client_request_version_api(lua_State * L)
+{
+ lua_pushcfunction(L, ts_lua_client_request_get_version);
+ lua_setfield(L, -2, "get_version");
+
+ lua_pushcfunction(L, ts_lua_client_request_set_version);
+ lua_setfield(L, -2, "set_version");
+}
+
+static int
+ts_lua_client_request_get_version(lua_State * L)
+{
+ int version;
+ char buf[32];
+ int n;
+
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ version = TSHttpHdrVersionGet(http_ctx->client_request_bufp, http_ctx->client_request_hdrp);
+
+ n = snprintf(buf, sizeof(buf) - 1, "%d.%d", TS_HTTP_MAJOR(version), TS_HTTP_MINOR(version));
+ lua_pushlstring(L, buf, n);
+
+ return 1;
+}
+
+static int
+ts_lua_client_request_set_version(lua_State * L)
+{
+ const char *version;
+ size_t len;
+ int major, minor;
+
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ version = luaL_checklstring(L, 1, &len);
+
+ sscanf(version, "%2u.%2u", &major, &minor);
+
+ TSHttpHdrVersionSet(http_ctx->client_request_bufp, http_ctx->client_request_hdrp, TS_HTTP_VERSION(major, minor));
+
+ return 0;
+}
+
+static void
ts_lua_inject_client_request_header_size_api(lua_State * L)
{
lua_pushcfunction(L, ts_lua_client_request_get_header_size);
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/83f16762/plugins/experimental/ts_lua/ts_lua_client_response.c
----------------------------------------------------------------------
diff --git a/plugins/experimental/ts_lua/ts_lua_client_response.c b/plugins/experimental/ts_lua/ts_lua_client_response.c
index 01e3664..7e22eef 100644
--- a/plugins/experimental/ts_lua/ts_lua_client_response.c
+++ b/plugins/experimental/ts_lua/ts_lua_client_response.c
@@ -19,12 +19,34 @@
#include "ts_lua_util.h"
+#define TS_LUA_CHECK_CLIENT_RESPONSE_HDR(http_ctx) \
+do { \
+ if (!http_ctx->client_response_hdrp) { \
+ if (TSHttpTxnClientRespGet(http_ctx->txnp, \
+ &http_ctx->client_response_bufp, \
+ &http_ctx->client_response_hdrp) != TS_SUCCESS) { \
+ return 0; \
+ } \
+ } \
+} while(0)
+
+
static int ts_lua_client_response_header_get(lua_State * L);
static int ts_lua_client_response_header_set(lua_State * L);
-static int ts_lua_client_response_header_get_status(lua_State * L);
+
+static int ts_lua_client_response_get_headers(lua_State * L);
+
+static int ts_lua_client_response_get_status(lua_State * L);
+static int ts_lua_client_response_set_status(lua_State * L);
+
+static int ts_lua_client_response_set_error_resp(lua_State * L);
+
+static int ts_lua_client_response_get_version(lua_State * L);
+static int ts_lua_client_response_set_version(lua_State * L);
+
static void ts_lua_inject_client_response_header_api(lua_State * L);
-static void ts_lua_inject_client_response_get_status_api(lua_State * L);
-static void ts_lua_inject_client_response_header_misc_api(lua_State * L);
+static void ts_lua_inject_client_response_headers_api(lua_State * L);
+static void ts_lua_inject_client_response_misc_api(lua_State * L);
void
@@ -33,7 +55,8 @@ ts_lua_inject_client_response_api(lua_State * L)
lua_newtable(L);
ts_lua_inject_client_response_header_api(L);
- ts_lua_inject_client_response_get_status_api(L);
+ ts_lua_inject_client_response_headers_api(L);
+ ts_lua_inject_client_response_misc_api(L);
lua_setfield(L, -2, "client_response");
}
@@ -52,7 +75,6 @@ ts_lua_inject_client_response_header_api(lua_State * L)
lua_setmetatable(L, -2);
- ts_lua_inject_client_response_header_misc_api(L);
lua_setfield(L, -2, "header");
}
@@ -165,34 +187,81 @@ ts_lua_client_response_header_set(lua_State * L)
}
static void
-ts_lua_inject_client_response_header_misc_api(lua_State * L ATS_UNUSED)
+ts_lua_inject_client_response_headers_api(lua_State * L)
{
- return;
+ lua_pushcfunction(L, ts_lua_client_response_get_headers);
+ lua_setfield(L, -2, "get_headers");
+}
+
+static int
+ts_lua_client_response_get_headers(lua_State * L)
+{
+ const char *name;
+ const char *value;
+ int name_len;
+ int value_len;
+ TSMLoc field_loc;
+ TSMLoc next_field_loc;
+
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ TS_LUA_CHECK_CLIENT_RESPONSE_HDR(http_ctx);
+
+ lua_newtable(L);
+
+ field_loc = TSMimeHdrFieldGet(http_ctx->client_response_bufp, http_ctx->client_response_hdrp, 0);
+
+ while (field_loc) {
+
+ name = TSMimeHdrFieldNameGet(http_ctx->client_response_bufp, http_ctx->client_response_hdrp, field_loc, &name_len);
+ if (name && name_len) {
+
+ value =
+ TSMimeHdrFieldValueStringGet(http_ctx->client_response_bufp, http_ctx->client_response_hdrp, field_loc, -1,
+ &value_len);
+ lua_pushlstring(L, name, name_len);
+ lua_pushlstring(L, value, value_len);
+ lua_rawset(L, -3);
+ }
+
+ next_field_loc = TSMimeHdrFieldNext(http_ctx->client_response_bufp, http_ctx->client_response_hdrp, field_loc);
+ TSHandleMLocRelease(http_ctx->client_response_bufp, http_ctx->client_response_hdrp, field_loc);
+ field_loc = next_field_loc;
+ }
+
+ return 1;
}
static void
-ts_lua_inject_client_response_get_status_api(lua_State * L)
+ts_lua_inject_client_response_misc_api(lua_State * L)
{
- lua_pushcfunction(L, ts_lua_client_response_header_get_status);
+ lua_pushcfunction(L, ts_lua_client_response_get_status);
lua_setfield(L, -2, "get_status");
+ lua_pushcfunction(L, ts_lua_client_response_set_status);
+ lua_setfield(L, -2, "set_status");
+
+ lua_pushcfunction(L, ts_lua_client_response_get_version);
+ lua_setfield(L, -2, "get_version");
+ lua_pushcfunction(L, ts_lua_client_response_set_version);
+ lua_setfield(L, -2, "set_version");
+
+ lua_pushcfunction(L, ts_lua_client_response_set_error_resp);
+ lua_setfield(L, -2, "set_error_resp");
+
+ return;
}
static int
-ts_lua_client_response_header_get_status(lua_State * L)
+ts_lua_client_response_get_status(lua_State * L)
{
int status;
ts_lua_http_ctx *http_ctx;
http_ctx = ts_lua_get_http_ctx(L);
- if (!http_ctx->client_response_hdrp) {
- if (TSHttpTxnClientRespGet(http_ctx->txnp,
- &http_ctx->client_response_bufp, &http_ctx->client_response_hdrp) != TS_SUCCESS) {
-
- lua_pushnil(L);
- return 1;
- }
- }
+ TS_LUA_CHECK_CLIENT_RESPONSE_HDR(http_ctx);
status = TSHttpHdrStatusGet(http_ctx->client_response_bufp, http_ctx->client_response_hdrp);
@@ -200,3 +269,127 @@ ts_lua_client_response_header_get_status(lua_State * L)
return 1;
}
+
+static int
+ts_lua_client_response_set_status(lua_State * L)
+{
+ int status;
+ const char *reason;
+ int reason_len;
+
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ TS_LUA_CHECK_CLIENT_RESPONSE_HDR(http_ctx);
+
+ status = luaL_checkint(L, 1);
+
+ reason = TSHttpHdrReasonLookup(status);
+ reason_len = strlen(reason);
+
+ TSHttpHdrStatusSet(http_ctx->client_response_bufp, http_ctx->client_response_hdrp, status);
+ TSHttpHdrReasonSet(http_ctx->client_response_bufp, http_ctx->client_response_hdrp, reason, reason_len);
+
+ return 0;
+}
+
+static int
+ts_lua_client_response_get_version(lua_State * L)
+{
+ int version;
+ char buf[32];
+ int n;
+
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ TS_LUA_CHECK_CLIENT_RESPONSE_HDR(http_ctx);
+
+ version = TSHttpHdrVersionGet(http_ctx->client_response_bufp, http_ctx->client_response_hdrp);
+
+ n = snprintf(buf, sizeof(buf) - 1, "%d.%d", TS_HTTP_MAJOR(version), TS_HTTP_MINOR(version));
+ lua_pushlstring(L, buf, n);
+
+ return 1;
+}
+
+static int
+ts_lua_client_response_set_version(lua_State * L)
+{
+ const char *version;
+ size_t len;
+ int major, minor;
+
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ TS_LUA_CHECK_CLIENT_RESPONSE_HDR(http_ctx);
+
+ version = luaL_checklstring(L, 1, &len);
+
+ sscanf(version, "%2u.%2u", &major, &minor);
+
+ TSHttpHdrVersionSet(http_ctx->client_response_bufp, http_ctx->client_response_hdrp, TS_HTTP_VERSION(major, minor));
+
+ return 0;
+}
+
+static int
+ts_lua_client_response_set_error_resp(lua_State * L)
+{
+ int n, status;
+ const char *body;
+ const char *reason;
+ int reason_len;
+ size_t body_len;
+ int resp_len;
+ char *resp_buf;
+ TSMLoc field_loc;
+
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+ TS_LUA_CHECK_CLIENT_RESPONSE_HDR(http_ctx);
+
+ n = lua_gettop(L);
+
+ status = luaL_checkinteger(L, 1);
+
+ reason = TSHttpHdrReasonLookup(status);
+ reason_len = strlen(reason);
+
+ TSHttpHdrStatusSet(http_ctx->client_response_bufp, http_ctx->client_response_hdrp, status);
+ TSHttpHdrReasonSet(http_ctx->client_response_bufp, http_ctx->client_response_hdrp, reason, reason_len);
+
+ body_len = 0;
+
+ if (n == 2) {
+ body = luaL_checklstring(L, 2, &body_len);
+ }
+
+ if (body_len && body) {
+ resp_buf = TSmalloc(body_len);
+ memcpy(resp_buf, body, body_len);
+ resp_len = body_len;
+
+ } else {
+ resp_buf = TSmalloc(reason_len);
+ memcpy(resp_buf, reason, reason_len);
+ resp_len = reason_len;
+ }
+
+ field_loc = TSMimeHdrFieldFind(http_ctx->client_response_bufp, http_ctx->client_response_hdrp,
+ TS_MIME_FIELD_TRANSFER_ENCODING, TS_MIME_LEN_TRANSFER_ENCODING);
+
+ if (field_loc) {
+ TSMimeHdrFieldDestroy(http_ctx->client_response_bufp, http_ctx->client_response_hdrp, field_loc);
+ TSHandleMLocRelease(http_ctx->client_response_bufp, http_ctx->client_response_hdrp, field_loc);
+ }
+
+ TSHttpTxnErrorBodySet(http_ctx->txnp, resp_buf, resp_len, NULL);
+
+ return 0;
+}
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/83f16762/plugins/experimental/ts_lua/ts_lua_common.h
----------------------------------------------------------------------
diff --git a/plugins/experimental/ts_lua/ts_lua_common.h b/plugins/experimental/ts_lua/ts_lua_common.h
index c96f36a..980cc5f 100644
--- a/plugins/experimental/ts_lua/ts_lua_common.h
+++ b/plugins/experimental/ts_lua/ts_lua_common.h
@@ -63,18 +63,45 @@
#define TS_LUA_FUNCTION_G_TXN_CLOSE "do_global_txn_close"
-#define TS_LUA_MAX_SCRIPT_FNAME_LENGTH 1024
-#define TS_LUA_MAX_URL_LENGTH 2048
+#define TS_LUA_DEBUG_TAG "ts_lua"
-#define TS_LUA_DEBUG_TAG "ts_lua"
+#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_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}
+
+#define ee(...) fprintf(stderr, "Lua *** %s: ", __func__); \
+ fprintf(stderr, __VA_ARGS__); \
+ fprintf(stderr, " @ %s line %d.\n", __FILE__, __LINE__)
+
+/* 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];
+
+ int _first:1; // create current instance for 1st ts_lua_main_ctx
+ int _last:1; // create current instance for the last ts_lua_main_ctx
} ts_lua_instance_conf;
+/* global lua state struct */
typedef struct
{
lua_State *lua;
@@ -82,7 +109,7 @@ typedef struct
int gref;
} ts_lua_main_ctx;
-
+/* lua state for http request */
typedef struct
{
lua_State *lua;
@@ -95,6 +122,8 @@ typedef struct
TSMBuffer server_request_bufp;
TSMLoc server_request_hdrp;
+ TSMLoc server_request_url;
+
TSMBuffer server_response_bufp;
TSMLoc server_response_hdrp;
@@ -107,6 +136,8 @@ typedef struct
ts_lua_main_ctx *mctx;
+ ts_lua_instance_conf *instance_conf;
+
int intercept_type;
int ref;
@@ -134,8 +165,23 @@ typedef struct
} ts_lua_transform_ctx;
+/* for intercept */
+struct ict_item;
+struct ict_ctx;
+typedef int (*ict_clean) (struct ict_item * item);
-typedef struct
+typedef struct ict_item
+{
+ struct ict_item *next;
+ struct ict_ctx *ictx;
+
+ TSCont contp;
+ ict_clean cleanup;
+ void *data;
+ int deleted:1;
+} ts_lua_http_intercept_item;
+
+typedef struct ict_ctx
{
lua_State *lua;
TSCont contp;
@@ -143,10 +189,17 @@ typedef struct
ts_lua_io_handle output;
TSVConn net_vc;
+ ts_lua_main_ctx *mctx;
ts_lua_http_ctx *hctx;
+
+ struct ict_item *ict_chain;
+
+ int64_t to_flush;
int ref;
- char recv_complete;
- char send_complete;
+
+ int recv_complete:1;
+ int send_complete:1;
+ int all_ready:1;
} ts_lua_http_intercept_ctx;
#define TS_LUA_RELEASE_IO_HANDLE(ih) do { \
@@ -160,4 +213,13 @@ typedef struct
} \
} while (0)
+#define TS_LUA_ADD_INTERCEPT_ITEM(ictx, item, contp, func, d) \
+ { item->cleanup = func; \
+ item->data = d; \
+ item->ictx = ictx; \
+ item->contp = contp; \
+ item->deleted = 0; \
+ item->next = ictx->ict_chain; \
+ ictx->ict_chain = item;}
+
#endif
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/83f16762/plugins/experimental/ts_lua/ts_lua_crypto.c
----------------------------------------------------------------------
diff --git a/plugins/experimental/ts_lua/ts_lua_crypto.c b/plugins/experimental/ts_lua/ts_lua_crypto.c
new file mode 100644
index 0000000..77718d4
--- /dev/null
+++ b/plugins/experimental/ts_lua/ts_lua_crypto.c
@@ -0,0 +1,177 @@
+/*
+ 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 <openssl/md5.h>
+#include <openssl/sha.h>
+#include "ts_lua_string.h"
+#include "ts_lua_util.h"
+
+
+#define TS_LUA_MD5_DIGEST_LENGTH 16
+#define TS_LUA_SHA_DIGEST_LENGTH 20
+
+static int ts_lua_md5(lua_State * L);
+static int ts_lua_md5_bin(lua_State * L);
+
+static int ts_lua_sha1(lua_State * L);
+static int ts_lua_sha1_bin(lua_State * L);
+
+void
+ts_lua_inject_crypto_api(lua_State * L)
+{
+ /* ts.md5() */
+ lua_pushcfunction(L, ts_lua_md5);
+ lua_setfield(L, -2, "md5");
+
+ /* ts.md5_bin(...) */
+ lua_pushcfunction(L, ts_lua_md5_bin);
+ lua_setfield(L, -2, "md5_bin");
+
+ /* ts.sha1_bin(...) */
+ lua_pushcfunction(L, ts_lua_sha1);
+ lua_setfield(L, -2, "sha1");
+
+ /* ts.sha1_bin(...) */
+ lua_pushcfunction(L, ts_lua_sha1_bin);
+ lua_setfield(L, -2, "sha1_bin");
+}
+
+static int
+ts_lua_md5(lua_State * L)
+{
+ u_char *src;
+ size_t slen;
+
+ MD5_CTX md5_ctx;
+ u_char md5_buf[TS_LUA_MD5_DIGEST_LENGTH];
+ u_char hex_buf[2 * sizeof(md5_buf)];
+
+ if (lua_gettop(L) != 1) {
+ return luaL_error(L, "expecting one argument");
+ }
+
+ if (lua_isnil(L, 1)) {
+ src = (u_char *) "";
+ slen = 0;
+
+ } else {
+ src = (u_char *) luaL_checklstring(L, 1, &slen);
+ }
+
+ MD5_Init(&md5_ctx);
+ MD5_Update(&md5_ctx, src, slen);
+ MD5_Final(md5_buf, &md5_ctx);
+
+ ts_lua_hex_dump(hex_buf, md5_buf, sizeof(md5_buf));
+
+ lua_pushlstring(L, (char *) hex_buf, sizeof(hex_buf));
+
+ return 1;
+}
+
+static int
+ts_lua_md5_bin(lua_State * L)
+{
+ u_char *src;
+ size_t slen;
+
+ MD5_CTX md5_ctx;
+ u_char md5_buf[TS_LUA_MD5_DIGEST_LENGTH];
+
+ if (lua_gettop(L) != 1) {
+ return luaL_error(L, "expecting one argument");
+ }
+
+ if (lua_isnil(L, 1)) {
+ src = (u_char *) "";
+ slen = 0;
+
+ } else {
+ src = (u_char *) luaL_checklstring(L, 1, &slen);
+ }
+
+ MD5_Init(&md5_ctx);
+ MD5_Update(&md5_ctx, src, slen);
+ MD5_Final(md5_buf, &md5_ctx);
+
+ lua_pushlstring(L, (char *) md5_buf, sizeof(md5_buf));
+
+ return 1;
+}
+
+static int
+ts_lua_sha1(lua_State * L)
+{
+ u_char *src;
+ size_t slen;
+
+ SHA_CTX sha;
+ u_char sha_buf[TS_LUA_SHA_DIGEST_LENGTH];
+ u_char hex_buf[2 * sizeof(sha_buf)];
+
+ if (lua_gettop(L) != 1) {
+ return luaL_error(L, "expecting one argument");
+ }
+
+ if (lua_isnil(L, 1)) {
+ src = (u_char *) "";
+ slen = 0;
+
+ } else {
+ src = (u_char *) luaL_checklstring(L, 1, &slen);
+ }
+
+ SHA1_Init(&sha);
+ SHA1_Update(&sha, src, slen);
+ SHA1_Final(sha_buf, &sha);
+
+ ts_lua_hex_dump(hex_buf, sha_buf, sizeof(sha_buf));
+ lua_pushlstring(L, (char *) hex_buf, sizeof(hex_buf));
+
+ return 1;
+}
+
+static int
+ts_lua_sha1_bin(lua_State * L)
+{
+ u_char *src;
+ size_t slen;
+
+ SHA_CTX sha;
+ u_char sha_buf[TS_LUA_SHA_DIGEST_LENGTH];
+
+ if (lua_gettop(L) != 1) {
+ return luaL_error(L, "expecting one argument");
+ }
+
+ if (lua_isnil(L, 1)) {
+ src = (u_char *) "";
+ slen = 0;
+
+ } else {
+ src = (u_char *) luaL_checklstring(L, 1, &slen);
+ }
+
+ SHA1_Init(&sha);
+ SHA1_Update(&sha, src, slen);
+ SHA1_Final(sha_buf, &sha);
+
+ lua_pushlstring(L, (char *) sha_buf, sizeof(sha_buf));
+
+ return 1;
+}
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/83f16762/plugins/experimental/ts_lua/ts_lua_crypto.h
----------------------------------------------------------------------
diff --git a/plugins/experimental/ts_lua/ts_lua_crypto.h b/plugins/experimental/ts_lua/ts_lua_crypto.h
new file mode 100644
index 0000000..6f0da12
--- /dev/null
+++ b/plugins/experimental/ts_lua/ts_lua_crypto.h
@@ -0,0 +1,27 @@
+/*
+ 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.
+*/
+
+
+#ifndef _TS_LUA_CRYPTO_H
+#define _TS_LUA_CRYPTO_H
+
+#include "ts_lua_common.h"
+
+void ts_lua_inject_crypto_api(lua_State * L);
+
+#endif
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/83f16762/plugins/experimental/ts_lua/ts_lua_hook.c
----------------------------------------------------------------------
diff --git a/plugins/experimental/ts_lua/ts_lua_hook.c b/plugins/experimental/ts_lua/ts_lua_hook.c
index e70312c..6fb3985 100644
--- a/plugins/experimental/ts_lua/ts_lua_hook.c
+++ b/plugins/experimental/ts_lua/ts_lua_hook.c
@@ -79,7 +79,7 @@ ts_lua_inject_hook_api(lua_State * L)
static void
ts_lua_inject_hook_variables(lua_State * L)
{
- unsigned int i;
+ int i;
for (i = 0; i < sizeof(ts_lua_hook_id_string) / sizeof(char *); i++) {
lua_pushinteger(L, i);
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/83f16762/plugins/experimental/ts_lua/ts_lua_http.c
----------------------------------------------------------------------
diff --git a/plugins/experimental/ts_lua/ts_lua_http.c b/plugins/experimental/ts_lua/ts_lua_http.c
index 599cd80..9ceb3e8 100644
--- a/plugins/experimental/ts_lua/ts_lua_http.c
+++ b/plugins/experimental/ts_lua/ts_lua_http.c
@@ -19,6 +19,8 @@
#include "ts_lua_util.h"
#include "ts_lua_http_intercept.h"
+#include "ts_lua_http_config.h"
+#include "ts_lua_http_cntl.h"
typedef enum
{
@@ -38,6 +40,7 @@ char *ts_lua_cache_lookup_result_string[] = {
static void ts_lua_inject_http_retset_api(lua_State * L);
static void ts_lua_inject_http_cache_api(lua_State * L);
static void ts_lua_inject_http_transform_api(lua_State * L);
+static void ts_lua_inject_http_misc_api(lua_State * L);
static int ts_lua_http_set_retstatus(lua_State * L);
static int ts_lua_http_set_retbody(lua_State * L);
@@ -45,11 +48,15 @@ static int ts_lua_http_set_resp(lua_State * L);
static int ts_lua_http_get_cache_lookup_status(lua_State * L);
static int ts_lua_http_set_cache_url(lua_State * L);
+
static void ts_lua_inject_cache_lookup_result_variables(lua_State * L);
static int ts_lua_http_resp_cache_transformed(lua_State * L);
static int ts_lua_http_resp_cache_untransformed(lua_State * L);
+static int ts_lua_http_is_internal_request(lua_State * L);
+
+
void
ts_lua_inject_http_api(lua_State * L)
{
@@ -59,6 +66,9 @@ ts_lua_inject_http_api(lua_State * L)
ts_lua_inject_http_cache_api(L);
ts_lua_inject_http_transform_api(L);
ts_lua_inject_http_intercept_api(L);
+ ts_lua_inject_http_config_api(L);
+ ts_lua_inject_http_cntl_api(L);
+ ts_lua_inject_http_misc_api(L);
lua_setfield(L, -2, "http");
}
@@ -99,9 +109,16 @@ ts_lua_inject_http_transform_api(lua_State * L)
}
static void
+ts_lua_inject_http_misc_api(lua_State * L)
+{
+ lua_pushcfunction(L, ts_lua_http_is_internal_request);
+ lua_setfield(L, -2, "is_internal_request");
+}
+
+static void
ts_lua_inject_cache_lookup_result_variables(lua_State * L)
{
- unsigned int i;
+ int i;
for (i = 0; i < sizeof(ts_lua_cache_lookup_result_string) / sizeof(char *); i++) {
lua_pushinteger(L, i);
@@ -225,3 +242,23 @@ ts_lua_http_resp_cache_untransformed(lua_State * L)
return 0;
}
+
+static int
+ts_lua_http_is_internal_request(lua_State * L)
+{
+ TSReturnCode ret;
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ ret = TSHttpIsInternalRequest(http_ctx->txnp);
+
+ if (ret == TS_SUCCESS) {
+ lua_pushnumber(L, 1);
+
+ } else {
+ lua_pushnumber(L, 0);
+ }
+
+ return 1;
+}
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/83f16762/plugins/experimental/ts_lua/ts_lua_http_cntl.c
----------------------------------------------------------------------
diff --git a/plugins/experimental/ts_lua/ts_lua_http_cntl.c b/plugins/experimental/ts_lua/ts_lua_http_cntl.c
new file mode 100644
index 0000000..e88bf74
--- /dev/null
+++ b/plugins/experimental/ts_lua/ts_lua_http_cntl.c
@@ -0,0 +1,101 @@
+/*
+ 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_lua_util.h"
+
+typedef enum
+{
+ TS_LUA_HTTP_CNTL_GET_LOGGING_MODE = TS_HTTP_CNTL_GET_LOGGING_MODE,
+ TS_LUA_HTTP_CNTL_SET_LOGGING_MODE = TS_HTTP_CNTL_SET_LOGGING_MODE,
+ TS_LUA_HTTP_CNTL_GET_INTERCEPT_RETRY_MODE = TS_HTTP_CNTL_GET_INTERCEPT_RETRY_MODE,
+ TS_LUA_HTTP_CNTL_SET_INTERCEPT_RETRY_MODE = TS_HTTP_CNTL_SET_INTERCEPT_RETRY_MODE
+} TSLuaHttpCntlType;
+
+
+ts_lua_var_item ts_lua_http_cntl_type_vars[] = {
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_HTTP_CNTL_GET_LOGGING_MODE),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_HTTP_CNTL_SET_LOGGING_MODE),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_HTTP_CNTL_GET_INTERCEPT_RETRY_MODE),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_HTTP_CNTL_SET_INTERCEPT_RETRY_MODE)
+};
+
+
+static void ts_lua_inject_http_cntl_variables(lua_State * L);
+
+static int ts_lua_http_cntl_set(lua_State * L);
+static int ts_lua_http_cntl_get(lua_State * L);
+
+
+void
+ts_lua_inject_http_cntl_api(lua_State * L)
+{
+ ts_lua_inject_http_cntl_variables(L);
+
+ lua_pushcfunction(L, ts_lua_http_cntl_set);
+ lua_setfield(L, -2, "cntl_set");
+
+ lua_pushcfunction(L, ts_lua_http_cntl_get);
+ lua_setfield(L, -2, "cntl_get");
+}
+
+static void
+ts_lua_inject_http_cntl_variables(lua_State * L)
+{
+ int i;
+
+ for (i = 0; i < sizeof(ts_lua_http_cntl_type_vars) / sizeof(ts_lua_var_item); i++) {
+ lua_pushinteger(L, ts_lua_http_cntl_type_vars[i].nvar);
+ lua_setglobal(L, ts_lua_http_cntl_type_vars[i].svar);
+ }
+}
+
+static int
+ts_lua_http_cntl_set(lua_State * L)
+{
+ int cntl_type;
+ int value;
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ cntl_type = luaL_checkinteger(L, 1);
+ value = luaL_checkinteger(L, 2);
+
+ TSHttpTxnCntl(http_ctx->txnp, cntl_type, value ? TS_HTTP_CNTL_ON : TS_HTTP_CNTL_OFF);
+
+ return 0;
+}
+
+static int
+ts_lua_http_cntl_get(lua_State * L)
+{
+ int cntl_type;
+ int64_t value;
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ cntl_type = luaL_checkinteger(L, 1);
+
+ TSHttpTxnCntl(http_ctx->txnp, cntl_type, &value);
+
+ lua_pushnumber(L, value);
+
+ return 1;
+}
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/83f16762/plugins/experimental/ts_lua/ts_lua_http_cntl.h
----------------------------------------------------------------------
diff --git a/plugins/experimental/ts_lua/ts_lua_http_cntl.h b/plugins/experimental/ts_lua/ts_lua_http_cntl.h
new file mode 100644
index 0000000..9613084
--- /dev/null
+++ b/plugins/experimental/ts_lua/ts_lua_http_cntl.h
@@ -0,0 +1,27 @@
+/*
+ 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.
+*/
+
+
+#ifndef _TS_LUA_HTTP_CNTL_H
+#define _TS_LUA_HTTP_CNTL_H
+
+#include "ts_lua_common.h"
+
+void ts_lua_inject_http_cntl_api(lua_State * L);
+
+#endif
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/83f16762/plugins/experimental/ts_lua/ts_lua_http_config.c
----------------------------------------------------------------------
diff --git a/plugins/experimental/ts_lua/ts_lua_http_config.c b/plugins/experimental/ts_lua/ts_lua_http_config.c
new file mode 100644
index 0000000..9209e23
--- /dev/null
+++ b/plugins/experimental/ts_lua/ts_lua_http_config.c
@@ -0,0 +1,304 @@
+/*
+ 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_lua_util.h"
+
+typedef enum
+{
+ TS_LUA_CONFIG_URL_REMAP_PRISTINE_HOST_HDR = TS_CONFIG_URL_REMAP_PRISTINE_HOST_HDR,
+ TS_LUA_CONFIG_HTTP_CHUNKING_ENABLED = TS_CONFIG_HTTP_CHUNKING_ENABLED,
+ TS_LUA_CONFIG_HTTP_NEGATIVE_CACHING_ENABLED = TS_CONFIG_HTTP_NEGATIVE_CACHING_ENABLED,
+ TS_LUA_CONFIG_HTTP_NEGATIVE_CACHING_LIFETIME = TS_CONFIG_HTTP_CACHE_WHEN_TO_REVALIDATE,
+ TS_LUA_CONFIG_HTTP_CACHE_WHEN_TO_REVALIDATE = TS_CONFIG_HTTP_CACHE_WHEN_TO_REVALIDATE,
+ TS_LUA_CONFIG_HTTP_KEEP_ALIVE_ENABLED_IN = TS_CONFIG_HTTP_KEEP_ALIVE_ENABLED_IN,
+ TS_LUA_CONFIG_HTTP_KEEP_ALIVE_ENABLED_OUT = TS_CONFIG_HTTP_KEEP_ALIVE_ENABLED_OUT,
+ TS_LUA_CONFIG_HTTP_KEEP_ALIVE_POST_OUT = TS_CONFIG_HTTP_KEEP_ALIVE_POST_OUT,
+ TS_LUA_CONFIG_HTTP_SHARE_SERVER_SESSIONS = TS_CONFIG_HTTP_SHARE_SERVER_SESSIONS,
+ TS_LUA_CONFIG_NET_SOCK_RECV_BUFFER_SIZE_OUT = TS_CONFIG_NET_SOCK_RECV_BUFFER_SIZE_OUT,
+ TS_LUA_CONFIG_NET_SOCK_SEND_BUFFER_SIZE_OUT = TS_CONFIG_NET_SOCK_SEND_BUFFER_SIZE_OUT,
+ TS_LUA_CONFIG_NET_SOCK_OPTION_FLAG_OUT = TS_CONFIG_NET_SOCK_OPTION_FLAG_OUT,
+ TS_LUA_CONFIG_HTTP_FORWARD_PROXY_AUTH_TO_PARENT = TS_CONFIG_HTTP_FORWARD_PROXY_AUTH_TO_PARENT,
+ TS_LUA_CONFIG_HTTP_ANONYMIZE_REMOVE_FROM = TS_CONFIG_HTTP_ANONYMIZE_REMOVE_FROM,
+ TS_LUA_CONFIG_HTTP_ANONYMIZE_REMOVE_REFERER = TS_CONFIG_HTTP_ANONYMIZE_REMOVE_REFERER,
+ TS_LUA_CONFIG_HTTP_ANONYMIZE_REMOVE_USER_AGENT = TS_CONFIG_HTTP_ANONYMIZE_REMOVE_USER_AGENT,
+ TS_LUA_CONFIG_HTTP_ANONYMIZE_REMOVE_COOKIE = TS_CONFIG_HTTP_ANONYMIZE_REMOVE_COOKIE,
+ TS_LUA_CONFIG_HTTP_ANONYMIZE_REMOVE_CLIENT_IP = TS_CONFIG_HTTP_ANONYMIZE_REMOVE_CLIENT_IP,
+ TS_LUA_CONFIG_HTTP_ANONYMIZE_INSERT_CLIENT_IP = TS_CONFIG_HTTP_ANONYMIZE_INSERT_CLIENT_IP,
+ TS_LUA_CONFIG_HTTP_RESPONSE_SERVER_ENABLED = TS_CONFIG_HTTP_RESPONSE_SERVER_ENABLED,
+ TS_LUA_CONFIG_HTTP_INSERT_SQUID_X_FORWARDED_FOR = TS_CONFIG_HTTP_INSERT_SQUID_X_FORWARDED_FOR,
+ TS_LUA_CONFIG_HTTP_SERVER_TCP_INIT_CWND = TS_CONFIG_HTTP_SERVER_TCP_INIT_CWND,
+ TS_LUA_CONFIG_HTTP_SEND_HTTP11_REQUESTS = TS_CONFIG_HTTP_SEND_HTTP11_REQUESTS,
+ TS_LUA_CONFIG_HTTP_CACHE_HTTP = TS_CONFIG_HTTP_CACHE_HTTP,
+ TS_LUA_CONFIG_HTTP_CACHE_CLUSTER_CACHE_LOCAL = TS_CONFIG_HTTP_CACHE_CLUSTER_CACHE_LOCAL,
+ TS_LUA_CONFIG_HTTP_CACHE_IGNORE_CLIENT_NO_CACHE = TS_CONFIG_HTTP_CACHE_IGNORE_CLIENT_NO_CACHE,
+ TS_LUA_CONFIG_HTTP_CACHE_IGNORE_CLIENT_CC_MAX_AGE = TS_CONFIG_HTTP_CACHE_IGNORE_CLIENT_CC_MAX_AGE,
+ TS_LUA_CONFIG_HTTP_CACHE_IMS_ON_CLIENT_NO_CACHE = TS_CONFIG_HTTP_CACHE_IMS_ON_CLIENT_NO_CACHE,
+ TS_LUA_CONFIG_HTTP_CACHE_IGNORE_SERVER_NO_CACHE = TS_CONFIG_HTTP_CACHE_IGNORE_SERVER_NO_CACHE,
+ TS_LUA_CONFIG_HTTP_CACHE_CACHE_RESPONSES_TO_COOKIES = TS_CONFIG_HTTP_CACHE_CACHE_RESPONSES_TO_COOKIES,
+ TS_LUA_CONFIG_HTTP_CACHE_IGNORE_AUTHENTICATION = TS_CONFIG_HTTP_CACHE_IGNORE_AUTHENTICATION,
+ TS_LUA_CONFIG_HTTP_CACHE_CACHE_URLS_THAT_LOOK_DYNAMIC = TS_CONFIG_HTTP_CACHE_CACHE_URLS_THAT_LOOK_DYNAMIC,
+ TS_LUA_CONFIG_HTTP_CACHE_REQUIRED_HEADERS = TS_CONFIG_HTTP_CACHE_REQUIRED_HEADERS,
+ TS_LUA_CONFIG_HTTP_INSERT_REQUEST_VIA_STR = TS_CONFIG_HTTP_INSERT_REQUEST_VIA_STR,
+ TS_LUA_CONFIG_HTTP_INSERT_RESPONSE_VIA_STR = TS_CONFIG_HTTP_INSERT_RESPONSE_VIA_STR,
+ TS_LUA_CONFIG_HTTP_CACHE_HEURISTIC_MIN_LIFETIME = TS_CONFIG_HTTP_CACHE_HEURISTIC_MIN_LIFETIME,
+ TS_LUA_CONFIG_HTTP_CACHE_HEURISTIC_MAX_LIFETIME = TS_CONFIG_HTTP_CACHE_HEURISTIC_MAX_LIFETIME,
+ TS_LUA_CONFIG_HTTP_CACHE_GUARANTEED_MIN_LIFETIME = TS_CONFIG_HTTP_CACHE_GUARANTEED_MIN_LIFETIME,
+ TS_LUA_CONFIG_HTTP_CACHE_GUARANTEED_MAX_LIFETIME = TS_CONFIG_HTTP_CACHE_GUARANTEED_MAX_LIFETIME,
+ TS_LUA_CONFIG_HTTP_CACHE_MAX_STALE_AGE = TS_CONFIG_HTTP_CACHE_MAX_STALE_AGE,
+ TS_LUA_CONFIG_HTTP_KEEP_ALIVE_NO_ACTIVITY_TIMEOUT_IN = TS_CONFIG_HTTP_KEEP_ALIVE_NO_ACTIVITY_TIMEOUT_IN,
+ TS_LUA_CONFIG_HTTP_KEEP_ALIVE_NO_ACTIVITY_TIMEOUT_OUT = TS_CONFIG_HTTP_KEEP_ALIVE_NO_ACTIVITY_TIMEOUT_OUT,
+ TS_LUA_CONFIG_HTTP_TRANSACTION_NO_ACTIVITY_TIMEOUT_IN = TS_CONFIG_HTTP_TRANSACTION_NO_ACTIVITY_TIMEOUT_IN,
+ TS_LUA_CONFIG_HTTP_TRANSACTION_NO_ACTIVITY_TIMEOUT_OUT = TS_CONFIG_HTTP_TRANSACTION_NO_ACTIVITY_TIMEOUT_OUT,
+ TS_LUA_CONFIG_HTTP_TRANSACTION_ACTIVE_TIMEOUT_OUT = TS_CONFIG_HTTP_TRANSACTION_ACTIVE_TIMEOUT_OUT,
+ TS_LUA_CONFIG_HTTP_ORIGIN_MAX_CONNECTIONS = TS_CONFIG_HTTP_ORIGIN_MAX_CONNECTIONS,
+ TS_LUA_CONFIG_HTTP_CONNECT_ATTEMPTS_MAX_RETRIES = TS_CONFIG_HTTP_CONNECT_ATTEMPTS_MAX_RETRIES,
+ TS_LUA_CONFIG_HTTP_CONNECT_ATTEMPTS_MAX_RETRIES_DEAD_SERVER = TS_CONFIG_HTTP_CONNECT_ATTEMPTS_MAX_RETRIES_DEAD_SERVER,
+ TS_LUA_CONFIG_HTTP_CONNECT_ATTEMPTS_RR_RETRIES = TS_CONFIG_HTTP_CONNECT_ATTEMPTS_RR_RETRIES,
+ TS_LUA_CONFIG_HTTP_CONNECT_ATTEMPTS_TIMEOUT = TS_CONFIG_HTTP_CONNECT_ATTEMPTS_TIMEOUT,
+ TS_LUA_CONFIG_HTTP_POST_CONNECT_ATTEMPTS_TIMEOUT = TS_CONFIG_HTTP_POST_CONNECT_ATTEMPTS_TIMEOUT,
+ TS_LUA_CONFIG_HTTP_DOWN_SERVER_CACHE_TIME = TS_CONFIG_HTTP_DOWN_SERVER_CACHE_TIME,
+ TS_LUA_CONFIG_HTTP_DOWN_SERVER_ABORT_THRESHOLD = TS_CONFIG_HTTP_DOWN_SERVER_ABORT_THRESHOLD,
+ TS_LUA_CONFIG_HTTP_CACHE_FUZZ_TIME = TS_CONFIG_HTTP_CACHE_FUZZ_TIME,
+ TS_LUA_CONFIG_HTTP_CACHE_FUZZ_MIN_TIME = TS_CONFIG_HTTP_CACHE_FUZZ_MIN_TIME,
+ TS_LUA_CONFIG_HTTP_DOC_IN_CACHE_SKIP_DNS = TS_CONFIG_HTTP_DOC_IN_CACHE_SKIP_DNS,
+ TS_LUA_CONFIG_HTTP_RESPONSE_SERVER_STR = TS_CONFIG_HTTP_RESPONSE_SERVER_STR,
+ TS_LUA_CONFIG_HTTP_CACHE_HEURISTIC_LM_FACTOR = TS_CONFIG_HTTP_CACHE_HEURISTIC_LM_FACTOR,
+ TS_LUA_CONFIG_HTTP_CACHE_FUZZ_PROBABILITY = TS_CONFIG_HTTP_CACHE_FUZZ_PROBABILITY,
+ TS_LUA_CONFIG_NET_SOCK_PACKET_MARK_OUT = TS_CONFIG_NET_SOCK_PACKET_MARK_OUT,
+ TS_LUA_CONFIG_NET_SOCK_PACKET_TOS_OUT = TS_CONFIG_NET_SOCK_PACKET_TOS_OUT,
+ TS_LUA_CONFIG_LAST_ENTRY = TS_CONFIG_LAST_ENTRY
+} TSLuaOverridableConfigKey;
+
+
+ts_lua_var_item ts_lua_http_config_vars[] = {
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_URL_REMAP_PRISTINE_HOST_HDR),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CHUNKING_ENABLED),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_NEGATIVE_CACHING_ENABLED),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_NEGATIVE_CACHING_LIFETIME),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CACHE_WHEN_TO_REVALIDATE),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_KEEP_ALIVE_ENABLED_IN),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_KEEP_ALIVE_ENABLED_OUT),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_KEEP_ALIVE_POST_OUT),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_SHARE_SERVER_SESSIONS),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_NET_SOCK_RECV_BUFFER_SIZE_OUT),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_NET_SOCK_SEND_BUFFER_SIZE_OUT),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_NET_SOCK_OPTION_FLAG_OUT),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_FORWARD_PROXY_AUTH_TO_PARENT),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_ANONYMIZE_REMOVE_FROM),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_ANONYMIZE_REMOVE_REFERER),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_ANONYMIZE_REMOVE_USER_AGENT),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_ANONYMIZE_REMOVE_COOKIE),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_ANONYMIZE_REMOVE_CLIENT_IP),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_ANONYMIZE_INSERT_CLIENT_IP),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_RESPONSE_SERVER_ENABLED),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_INSERT_SQUID_X_FORWARDED_FOR),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_SERVER_TCP_INIT_CWND),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_SEND_HTTP11_REQUESTS),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CACHE_HTTP),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CACHE_CLUSTER_CACHE_LOCAL),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CACHE_IGNORE_CLIENT_NO_CACHE),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CACHE_IGNORE_CLIENT_CC_MAX_AGE),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CACHE_IMS_ON_CLIENT_NO_CACHE),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CACHE_IGNORE_SERVER_NO_CACHE),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CACHE_CACHE_RESPONSES_TO_COOKIES),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CACHE_IGNORE_AUTHENTICATION),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CACHE_CACHE_URLS_THAT_LOOK_DYNAMIC),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CACHE_REQUIRED_HEADERS),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_INSERT_REQUEST_VIA_STR),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_INSERT_RESPONSE_VIA_STR),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CACHE_HEURISTIC_MIN_LIFETIME),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CACHE_HEURISTIC_MAX_LIFETIME),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CACHE_GUARANTEED_MIN_LIFETIME),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CACHE_GUARANTEED_MAX_LIFETIME),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CACHE_MAX_STALE_AGE),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_KEEP_ALIVE_NO_ACTIVITY_TIMEOUT_IN),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_KEEP_ALIVE_NO_ACTIVITY_TIMEOUT_OUT),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_TRANSACTION_NO_ACTIVITY_TIMEOUT_IN),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_TRANSACTION_NO_ACTIVITY_TIMEOUT_OUT),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_TRANSACTION_ACTIVE_TIMEOUT_OUT),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_ORIGIN_MAX_CONNECTIONS),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CONNECT_ATTEMPTS_MAX_RETRIES),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CONNECT_ATTEMPTS_MAX_RETRIES_DEAD_SERVER),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CONNECT_ATTEMPTS_RR_RETRIES),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CONNECT_ATTEMPTS_TIMEOUT),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_POST_CONNECT_ATTEMPTS_TIMEOUT),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_DOWN_SERVER_CACHE_TIME),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_DOWN_SERVER_ABORT_THRESHOLD),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CACHE_FUZZ_TIME),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CACHE_FUZZ_MIN_TIME),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_DOC_IN_CACHE_SKIP_DNS),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_RESPONSE_SERVER_STR),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CACHE_HEURISTIC_LM_FACTOR),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_HTTP_CACHE_FUZZ_PROBABILITY),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_NET_SOCK_PACKET_MARK_OUT),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_NET_SOCK_PACKET_TOS_OUT),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_LAST_ENTRY),
+};
+
+static void ts_lua_inject_http_config_variables(lua_State * L);
+
+static int ts_lua_http_config_int_set(lua_State * L);
+static int ts_lua_http_config_int_get(lua_State * L);
+static int ts_lua_http_config_float_set(lua_State * L);
+static int ts_lua_http_config_float_get(lua_State * L);
+static int ts_lua_http_config_string_set(lua_State * L);
+static int ts_lua_http_config_string_get(lua_State * L);
+
+
+void
+ts_lua_inject_http_config_api(lua_State * L)
+{
+ ts_lua_inject_http_config_variables(L);
+
+ lua_pushcfunction(L, ts_lua_http_config_int_set);
+ lua_setfield(L, -2, "config_int_set");
+
+ lua_pushcfunction(L, ts_lua_http_config_int_get);
+ lua_setfield(L, -2, "config_int_get");
+
+ lua_pushcfunction(L, ts_lua_http_config_float_set);
+ lua_setfield(L, -2, "config_float_set");
+
+ lua_pushcfunction(L, ts_lua_http_config_float_get);
+ lua_setfield(L, -2, "config_float_get");
+
+ lua_pushcfunction(L, ts_lua_http_config_string_set);
+ lua_setfield(L, -2, "config_string_set");
+
+ lua_pushcfunction(L, ts_lua_http_config_string_get);
+ lua_setfield(L, -2, "config_string_get");
+}
+
+static void
+ts_lua_inject_http_config_variables(lua_State * L)
+{
+ int i;
+
+ for (i = 0; i < sizeof(ts_lua_http_config_vars) / sizeof(ts_lua_var_item); i++) {
+ lua_pushinteger(L, ts_lua_http_config_vars[i].nvar);
+ lua_setglobal(L, ts_lua_http_config_vars[i].svar);
+ }
+}
+
+static int
+ts_lua_http_config_int_set(lua_State * L)
+{
+ int conf;
+ int value;
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ conf = luaL_checkinteger(L, 1);
+ value = luaL_checkinteger(L, 2);
+
+ TSHttpTxnConfigIntSet(http_ctx->txnp, conf, value);
+
+ return 0;
+}
+
+static int
+ts_lua_http_config_int_get(lua_State * L)
+{
+ int conf;
+ int64_t value;
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ conf = luaL_checkinteger(L, 1);
+
+ TSHttpTxnConfigIntGet(http_ctx->txnp, conf, &value);
+
+ lua_pushnumber(L, value);
+
+ return 1;
+}
+
+static int
+ts_lua_http_config_float_set(lua_State * L)
+{
+ int conf;
+ float value;
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ conf = luaL_checkinteger(L, 1);
+ value = luaL_checknumber(L, 2);
+
+ TSHttpTxnConfigFloatSet(http_ctx->txnp, conf, value);
+
+ return 0;
+}
+
+static int
+ts_lua_http_config_float_get(lua_State * L)
+{
+ int conf;
+ float value;
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ conf = luaL_checkinteger(L, 1);
+
+ TSHttpTxnConfigFloatGet(http_ctx->txnp, conf, &value);
+
+ lua_pushnumber(L, value);
+
+ return 1;
+}
+
+static int
+ts_lua_http_config_string_set(lua_State * L)
+{
+ int conf;
+ const char *value;
+ size_t value_len;
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ conf = luaL_checkinteger(L, 1);
+ value = luaL_checklstring(L, 2, &value_len);
+
+ TSHttpTxnConfigStringSet(http_ctx->txnp, conf, value, value_len);
+
+ return 0;
+}
+
+static int
+ts_lua_http_config_string_get(lua_State * L)
+{
+ int conf;
+ const char *value;
+ int value_len;
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ conf = luaL_checkinteger(L, 1);
+
+ TSHttpTxnConfigStringGet(http_ctx->txnp, conf, &value, &value_len);
+
+ lua_pushlstring(L, value, value_len);
+
+ return 1;
+}
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/83f16762/plugins/experimental/ts_lua/ts_lua_http_config.h
----------------------------------------------------------------------
diff --git a/plugins/experimental/ts_lua/ts_lua_http_config.h b/plugins/experimental/ts_lua/ts_lua_http_config.h
new file mode 100644
index 0000000..0cf00f8
--- /dev/null
+++ b/plugins/experimental/ts_lua/ts_lua_http_config.h
@@ -0,0 +1,27 @@
+/*
+ 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.
+*/
+
+
+#ifndef _TS_LUA_HTTP_CONFIG_H
+#define _TS_LUA_HTTP_CONFIG_H
+
+#include "ts_lua_common.h"
+
+void ts_lua_inject_http_config_api(lua_State * L);
+
+#endif
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/83f16762/plugins/experimental/ts_lua/ts_lua_http_intercept.c
----------------------------------------------------------------------
diff --git a/plugins/experimental/ts_lua/ts_lua_http_intercept.c b/plugins/experimental/ts_lua/ts_lua_http_intercept.c
index 88d41cd..21e142a 100644
--- a/plugins/experimental/ts_lua/ts_lua_http_intercept.c
+++ b/plugins/experimental/ts_lua/ts_lua_http_intercept.c
@@ -35,10 +35,12 @@ static void ts_lua_http_intercept_process(ts_lua_http_ctx * http_ctx, TSVConn co
static void ts_lua_http_intercept_setup_read(ts_lua_http_intercept_ctx * ictx);
static void ts_lua_http_intercept_setup_write(ts_lua_http_intercept_ctx * ictx);
static int ts_lua_http_intercept_handler(TSCont contp, TSEvent event, void *edata);
-static int ts_lua_http_intercept_run_coroutine(ts_lua_http_intercept_ctx * ictx);
+static int ts_lua_http_intercept_run_coroutine(ts_lua_http_intercept_ctx * ictx, int n);
static int ts_lua_http_intercept_process_read(TSEvent event, ts_lua_http_intercept_ctx * ictx);
static int ts_lua_http_intercept_process_write(TSEvent event, ts_lua_http_intercept_ctx * ictx);
+extern int ts_lua_flush_launch(ts_lua_http_intercept_ctx * ictx);
+
void
ts_lua_inject_http_intercept_api(lua_State * L)
@@ -153,6 +155,9 @@ ts_lua_http_intercept_process(ts_lua_http_ctx * http_ctx, TSVConn conn)
// set up read.
ts_lua_http_intercept_setup_read(ictx);
+ // set up write.
+ ts_lua_http_intercept_setup_write(ictx);
+
// invoke function here
if (http_ctx->intercept_type == TS_LUA_TYPE_HTTP_INTERCEPT) {
lua_getglobal(l, TS_LUA_FUNCTION_HTTP_INTERCEPT);
@@ -160,7 +165,7 @@ ts_lua_http_intercept_process(ts_lua_http_ctx * http_ctx, TSVConn conn)
lua_getglobal(l, TS_LUA_FUNCTION_HTTP_SERVER_INTERCEPT);
}
- ts_lua_http_intercept_run_coroutine(ictx);
+ ts_lua_http_intercept_run_coroutine(ictx, 0);
TSMutexUnlock(mtxp);
@@ -185,7 +190,7 @@ ts_lua_http_intercept_setup_write(ts_lua_http_intercept_ctx * ictx)
static int
ts_lua_http_intercept_handler(TSCont contp, TSEvent event, void *edata)
{
- int ret;
+ int ret, n;
TSMutex mtxp;
ts_lua_http_intercept_ctx *ictx;
@@ -199,9 +204,10 @@ ts_lua_http_intercept_handler(TSCont contp, TSEvent event, void *edata)
ret = ts_lua_http_intercept_process_write(event, ictx);
} else {
- mtxp = ictx->hctx->mctx->mutexp;
+ mtxp = ictx->mctx->mutexp;
+ n = (int64_t) edata & 0xFFFF;
TSMutexLock(mtxp);
- ret = ts_lua_http_intercept_run_coroutine(ictx);
+ ret = ts_lua_http_intercept_run_coroutine(ictx, n);
}
if (ret || (ictx->send_complete && ictx->recv_complete)) {
@@ -209,7 +215,7 @@ ts_lua_http_intercept_handler(TSCont contp, TSEvent event, void *edata)
TSContDestroy(contp);
if (!mtxp) {
- mtxp = ictx->hctx->mctx->mutexp;
+ mtxp = ictx->mctx->mutexp;
TSMutexLock(mtxp);
}
@@ -223,24 +229,31 @@ ts_lua_http_intercept_handler(TSCont contp, TSEvent event, void *edata)
}
static int
-ts_lua_http_intercept_run_coroutine(ts_lua_http_intercept_ctx * ictx)
+ts_lua_http_intercept_run_coroutine(ts_lua_http_intercept_ctx * ictx, int n)
{
int ret;
- const char *res;
- size_t res_len;
+ int64_t avail;
+ int64_t done;
lua_State *L;
L = ictx->lua;
- ret = lua_resume(L, 0);
+ ret = lua_resume(L, n);
switch (ret) {
case 0: // finished
- res = lua_tolstring(L, -1, &res_len);
- ts_lua_http_intercept_setup_write(ictx);
- TSIOBufferWrite(ictx->output.buffer, res, res_len);
- TSVIONBytesSet(ictx->output.vio, res_len);
+ avail = TSIOBufferReaderAvail(ictx->output.reader);
+ done = TSVIONDoneGet(ictx->output.vio);
+ TSVIONBytesSet(ictx->output.vio, avail + done);
+ ictx->all_ready = 1;
+
+ if (avail) {
+ TSVIOReenable(ictx->output.vio);
+
+ } else {
+ ictx->send_complete = 1;
+ }
break;
case 1: // yield
@@ -280,10 +293,31 @@ ts_lua_http_intercept_process_read(TSEvent event, ts_lua_http_intercept_ctx * ic
static int
ts_lua_http_intercept_process_write(TSEvent event, ts_lua_http_intercept_ctx * ictx)
{
+ int64_t done, avail;
+
switch (event) {
case TS_EVENT_VCONN_WRITE_READY:
- if (TSIOBufferReaderAvail(ictx->output.reader))
+
+ avail = TSIOBufferReaderAvail(ictx->output.reader);
+
+ if (ictx->all_ready) {
+ TSVIOReenable(ictx->output.vio);
+
+ } else if (ictx->to_flush > 0) { // ts.flush()
+
+ done = TSVIONDoneGet(ictx->output.vio);
+
+ if (ictx->to_flush > done) {
+ TSVIOReenable(ictx->output.vio);
+
+ } else { // we had flush all the data we want
+ ictx->to_flush = 0;
+ ts_lua_flush_launch(ictx); // wake up
+ }
+
+ } else if (avail > 0) {
TSVIOReenable(ictx->output.vio);
+ }
break;
case TS_EVENT_VCONN_WRITE_COMPLETE:
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/83f16762/plugins/experimental/ts_lua/ts_lua_log.c
----------------------------------------------------------------------
diff --git a/plugins/experimental/ts_lua/ts_lua_log.c b/plugins/experimental/ts_lua/ts_lua_log.c
index 400589c..b0861c2 100644
--- a/plugins/experimental/ts_lua/ts_lua_log.c
+++ b/plugins/experimental/ts_lua/ts_lua_log.c
@@ -91,7 +91,7 @@ ts_lua_log_object_write(lua_State * L)
text = luaL_checklstring(L, 1, &text_len);
if (log) {
- TSTextLogObjectWrite(log, "%s", text);
+ TSTextLogObjectWrite(log, (char *) text, NULL);
} else {
TSError("[%s] log is not exsited!", __FUNCTION__);
}
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/83f16762/plugins/experimental/ts_lua/ts_lua_mgmt.c
----------------------------------------------------------------------
diff --git a/plugins/experimental/ts_lua/ts_lua_mgmt.c b/plugins/experimental/ts_lua/ts_lua_mgmt.c
new file mode 100644
index 0000000..4cbbc83
--- /dev/null
+++ b/plugins/experimental/ts_lua/ts_lua_mgmt.c
@@ -0,0 +1,106 @@
+/*
+ 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_lua_util.h"
+
+static int ts_lua_mgmt_get_int(lua_State * L);
+static int ts_lua_mgmt_get_counter(lua_State * L);
+static int ts_lua_mgmt_get_float(lua_State * L);
+static int ts_lua_mgmt_get_string(lua_State * L);
+
+
+void
+ts_lua_inject_mgmt_api(lua_State * L)
+{
+ lua_newtable(L);
+
+ lua_pushcfunction(L, ts_lua_mgmt_get_int);
+ lua_setfield(L, -2, "get_int");
+
+ lua_pushcfunction(L, ts_lua_mgmt_get_counter);
+ lua_setfield(L, -2, "get_counter");
+
+ lua_pushcfunction(L, ts_lua_mgmt_get_float);
+ lua_setfield(L, -2, "get_float");
+
+ lua_pushcfunction(L, ts_lua_mgmt_get_string);
+ lua_setfield(L, -2, "get_string");
+
+ lua_setfield(L, -2, "mgmt");
+}
+
+static int
+ts_lua_mgmt_get_int(lua_State * L)
+{
+ const char *name;
+ size_t name_len;
+ TSMgmtInt int_val;
+
+ name = luaL_checklstring(L, 1, &name_len);
+ TSMgmtIntGet(name, &int_val);
+
+ lua_pushinteger(L, int_val);
+
+ return 1;
+}
+
+static int
+ts_lua_mgmt_get_counter(lua_State * L)
+{
+ const char *name;
+ size_t name_len;
+ TSMgmtCounter counter_val;
+
+ name = luaL_checklstring(L, 1, &name_len);
+ TSMgmtCounterGet(name, &counter_val);
+
+ lua_pushinteger(L, counter_val);
+
+ return 1;
+}
+
+static int
+ts_lua_mgmt_get_float(lua_State * L)
+{
+ const char *name;
+ size_t name_len;
+ TSMgmtFloat float_val;
+
+ name = luaL_checklstring(L, 1, &name_len);
+ TSMgmtFloatGet(name, &float_val);
+
+ lua_pushnumber(L, float_val);
+
+ return 1;
+}
+
+static int
+ts_lua_mgmt_get_string(lua_State * L)
+{
+ const char *name;
+ size_t name_len;
+ TSMgmtString str_val;
+
+ name = luaL_checklstring(L, 1, &name_len);
+ TSMgmtStringGet(name, &str_val);
+
+ lua_pushstring(L, str_val);
+
+ return 1;
+}
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/83f16762/plugins/experimental/ts_lua/ts_lua_mgmt.h
----------------------------------------------------------------------
diff --git a/plugins/experimental/ts_lua/ts_lua_mgmt.h b/plugins/experimental/ts_lua/ts_lua_mgmt.h
new file mode 100644
index 0000000..d6d79e0
--- /dev/null
+++ b/plugins/experimental/ts_lua/ts_lua_mgmt.h
@@ -0,0 +1,27 @@
+/*
+ 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.
+*/
+
+
+#ifndef _TS_LUA_MGMT_H
+#define _TS_LUA_MGMT_H
+
+#include "ts_lua_common.h"
+
+void ts_lua_inject_mgmt_api(lua_State * L);
+
+#endif
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/83f16762/plugins/experimental/ts_lua/ts_lua_misc.c
----------------------------------------------------------------------
diff --git a/plugins/experimental/ts_lua/ts_lua_misc.c b/plugins/experimental/ts_lua/ts_lua_misc.c
index 05a8155..4af4da0 100644
--- a/plugins/experimental/ts_lua/ts_lua_misc.c
+++ b/plugins/experimental/ts_lua/ts_lua_misc.c
@@ -24,6 +24,15 @@ static int ts_lua_get_now_time(lua_State * L);
static int ts_lua_debug(lua_State * L);
static int ts_lua_error(lua_State * L);
static int ts_lua_sleep(lua_State * L);
+static int ts_lua_say(lua_State * L);
+static int ts_lua_flush(lua_State * L);
+
+static int ts_lua_sleep_cleanup(struct ict_item *item);
+static int ts_lua_sleep_handler(TSCont contp, TSEvent event, void *edata);
+
+int ts_lua_flush_launch(ts_lua_http_intercept_ctx * ictx);
+static int ts_lua_flush_cleanup(struct ict_item *item);
+static int ts_lua_flush_handler(TSCont contp, TSEvent event, void *edata);
void
@@ -44,6 +53,14 @@ ts_lua_inject_misc_api(lua_State * L)
/* ts.sleep(...) */
lua_pushcfunction(L, ts_lua_sleep);
lua_setfield(L, -2, "sleep");
+
+ /* ts.say(...) */
+ lua_pushcfunction(L, ts_lua_say);
+ lua_setfield(L, -2, "say");
+
+ /* ts.flush(...) */
+ lua_pushcfunction(L, ts_lua_flush);
+ lua_setfield(L, -2, "flush");
}
static int
@@ -62,7 +79,7 @@ ts_lua_debug(lua_State * L)
const char *msg;
msg = luaL_checkstring(L, 1);
- TSDebug(TS_LUA_DEBUG_TAG, "%s", msg);
+ TSDebug(TS_LUA_DEBUG_TAG, msg, NULL);
return 0;
}
@@ -72,7 +89,7 @@ ts_lua_error(lua_State * L)
const char *msg;
msg = luaL_checkstring(L, 1);
- TSError("%s", msg);
+ TSError(msg, NULL);
return 0;
}
@@ -80,11 +97,134 @@ static int
ts_lua_sleep(lua_State * L)
{
int sec;
+ TSAction action;
+ TSCont contp;
+ ts_lua_http_intercept_item *node;
ts_lua_http_intercept_ctx *ictx;
ictx = ts_lua_get_http_intercept_ctx(L);
sec = luaL_checknumber(L, 1);
- TSContSchedule(ictx->contp, sec * 1000, TS_THREAD_POOL_DEFAULT);
+ contp = TSContCreate(ts_lua_sleep_handler, TSContMutexGet(ictx->contp));
+ action = TSContSchedule(contp, sec * 1000, TS_THREAD_POOL_DEFAULT);
+
+ node = (ts_lua_http_intercept_item *) TSmalloc(sizeof(ts_lua_http_intercept_item));
+ TS_LUA_ADD_INTERCEPT_ITEM(ictx, node, contp, ts_lua_sleep_cleanup, action);
+ TSContDataSet(contp, node);
+
return lua_yield(L, 0);
}
+
+static int
+ts_lua_say(lua_State * L)
+{
+ const char *data;
+ size_t len;
+
+ ts_lua_http_intercept_ctx *ictx;
+
+ ictx = ts_lua_get_http_intercept_ctx(L);
+
+ data = luaL_checklstring(L, 1, &len);
+
+ if (len > 0) {
+ TSIOBufferWrite(ictx->output.buffer, data, len);
+ TSVIOReenable(ictx->output.vio);
+ }
+
+ return 0;
+}
+
+static int
+ts_lua_flush(lua_State * L)
+{
+ int64_t avail;
+ ts_lua_http_intercept_ctx *ictx;
+
+ ictx = ts_lua_get_http_intercept_ctx(L);
+ avail = TSIOBufferReaderAvail(ictx->output.reader);
+
+ if (avail > 0) {
+ ictx->to_flush = TSVIONDoneGet(ictx->output.vio) + TSIOBufferReaderAvail(ictx->output.reader);
+ TSVIOReenable(ictx->output.vio);
+
+ return lua_yield(L, 0);
+ }
+
+ return 0;
+}
+
+static int
+ts_lua_sleep_handler(TSCont contp, TSEvent event, void *edata)
+{
+ ts_lua_http_intercept_item *item = TSContDataGet(contp);
+
+ ts_lua_sleep_cleanup(item);
+
+ TSContCall(item->ictx->contp, event, 0);
+
+ return 0;
+}
+
+static int
+ts_lua_sleep_cleanup(struct ict_item *item)
+{
+ if (item->deleted)
+ return 0;
+
+ if (item->data) {
+ TSActionCancel((TSAction) item->data);
+ item->data = NULL;
+ }
+
+ TSContDestroy(item->contp);
+ item->deleted = 1;
+
+ return 0;
+}
+
+int
+ts_lua_flush_launch(ts_lua_http_intercept_ctx * ictx)
+{
+ TSAction action;
+ TSCont contp;
+ ts_lua_http_intercept_item *node;
+
+ contp = TSContCreate(ts_lua_flush_handler, TSContMutexGet(ictx->contp));
+ action = TSContSchedule(contp, 0, TS_THREAD_POOL_DEFAULT);
+
+ node = (ts_lua_http_intercept_item *) TSmalloc(sizeof(ts_lua_http_intercept_item));
+ TS_LUA_ADD_INTERCEPT_ITEM(ictx, node, contp, ts_lua_flush_cleanup, action);
+ TSContDataSet(contp, node);
+
+ return 0;
+}
+
+static int
+ts_lua_flush_cleanup(struct ict_item *item)
+{
+ if (item->deleted)
+ return 0;
+
+ if (item->data) {
+ TSActionCancel((TSAction) item->data);
+ item->data = NULL;
+ }
+
+ TSContDestroy(item->contp);
+ item->deleted = 1;
+
+ return 0;
+}
+
+static int
+ts_lua_flush_handler(TSCont contp, TSEvent event, void *edata)
+{
+ ts_lua_http_intercept_item *item = TSContDataGet(contp);
+
+ ts_lua_flush_cleanup(item);
+
+ TSContCall(item->ictx->contp, event, 0);
+
+ return 0;
+}
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/83f16762/plugins/experimental/ts_lua/ts_lua_package.c
----------------------------------------------------------------------
diff --git a/plugins/experimental/ts_lua/ts_lua_package.c b/plugins/experimental/ts_lua/ts_lua_package.c
new file mode 100644
index 0000000..3d77f23
--- /dev/null
+++ b/plugins/experimental/ts_lua/ts_lua_package.c
@@ -0,0 +1,297 @@
+/*
+ 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_lua_util.h"
+
+#define TS_LUA_MAX_PACKAGE_PATH_LEN 256
+#define TS_LUA_MAX_PACKAGE_NUM 64
+
+
+typedef struct
+{
+ size_t len;
+ char *name;
+} ts_lua_package_path;
+
+
+static int g_path_cnt = 0;
+static ts_lua_package_path g_path[TS_LUA_MAX_PACKAGE_NUM];
+
+static int g_cpath_cnt = 0;
+static ts_lua_package_path g_cpath[TS_LUA_MAX_PACKAGE_NUM];
+
+
+static int ts_lua_add_package_path(lua_State * L);
+static int ts_lua_add_package_cpath(lua_State * L);
+static int ts_lua_add_package_path_items(lua_State * L, ts_lua_package_path * pp, int n);
+static int ts_lua_add_package_cpath_items(lua_State * L, ts_lua_package_path * pp, int n);
+
+
+void
+ts_lua_inject_package_api(lua_State * L)
+{
+ /* ts.add_package_path() */
+ lua_pushcfunction(L, ts_lua_add_package_path);
+ lua_setfield(L, -2, "add_package_path");
+
+ /* ts.add_package_cpath(...) */
+ lua_pushcfunction(L, ts_lua_add_package_cpath);
+ lua_setfield(L, -2, "add_package_cpath");
+}
+
+static int
+ts_lua_add_package_path(lua_State * L)
+{
+ ts_lua_instance_conf *conf;
+ const char *data;
+ const char *ptr, *end, *hit;
+ size_t dlen;
+ size_t i, n, item_len;
+ ts_lua_package_path pp[TS_LUA_MAX_PACKAGE_NUM];
+ ts_lua_package_path *elt;
+
+ conf = ts_lua_get_instance_conf(L);
+ if (conf == NULL) {
+ return luaL_error(L, "cann't get the instance conf.");
+ }
+
+ data = luaL_checklstring(L, 1, &dlen);
+ end = data + dlen;
+
+ ptr = data;
+ n = 0;
+
+ while (ptr < end) {
+ hit = memchr(ptr, ';', end - ptr);
+
+ if (hit) {
+ item_len = hit - ptr;
+
+ } else {
+ item_len = end - ptr;
+ }
+
+ if (item_len > 0) {
+
+ for (i = 0; i < g_path_cnt; i++) {
+ if (g_path[i].len == item_len && memcmp(g_path[i].name, ptr, item_len) == 0) // exist
+ {
+ break;
+ }
+ }
+
+ if (i >= g_path_cnt) {
+
+ if (n + i >= TS_LUA_MAX_PACKAGE_NUM)
+ return luaL_error(L, "extended package path number exceeds %d.", TS_LUA_MAX_PACKAGE_NUM);
+
+ pp[n].name = (char *) ptr;
+ pp[n].len = item_len;
+ n++;
+ }
+ }
+
+ ptr += item_len + 1; // ??
+ }
+
+ if (n > 0) {
+ ts_lua_add_package_path_items(L, pp, n);
+
+ if (conf->_last) {
+ elt = &g_path[g_path_cnt];
+
+ for (i = 0; i < n; i++) {
+ elt->len = pp[i].len;
+ elt->name = (char *) TSmalloc(pp[i].len);
+ memcpy(elt->name, pp[i].name, pp[i].len);
+ elt++;
+ }
+
+ g_path_cnt += n;
+ }
+ }
+
+ return 0;
+}
+
+static int
+ts_lua_add_package_path_items(lua_State * L, ts_lua_package_path * pp, int n)
+{
+ int i, base;
+ const char *old_path;
+ char new_path[2048];
+ size_t old_path_len, new_path_len;
+
+ base = lua_gettop(L);
+
+ lua_getglobal(L, "package");
+
+ if (!lua_istable(L, -1)) {
+ return luaL_error(L, "'package' table does not exist.");
+ }
+
+ lua_getfield(L, -1, "path"); /* get old package.path */
+
+ old_path = lua_tolstring(L, -1, &old_path_len);
+ if (old_path[old_path_len - 1] == ';')
+ old_path_len--;
+
+ new_path_len = snprintf(new_path, sizeof(new_path) - 32, "%.*s", (int) old_path_len, old_path);
+
+ for (i = 0; i < n; i++) {
+ if (new_path_len + pp[i].len + 1 >= sizeof(new_path)) {
+ fprintf(stderr, "extended package.path is too long.\n");
+ return -1;
+ }
+
+ new_path[new_path_len++] = ';';
+ memcpy(new_path + new_path_len, pp[i].name, pp[i].len);
+ new_path_len += pp[i].len;
+ }
+
+ new_path[new_path_len] = 0;
+
+ lua_pushlstring(L, new_path, new_path_len);
+ lua_setfield(L, -3, "path");
+
+ lua_settop(L, base);
+
+ return 0;
+}
+
+
+static int
+ts_lua_add_package_cpath(lua_State * L)
+{
+ ts_lua_instance_conf *conf;
+ const char *data;
+ const char *ptr, *end, *hit;
+ size_t dlen;
+ size_t i, n, item_len;
+ ts_lua_package_path pp[TS_LUA_MAX_PACKAGE_NUM];
+ ts_lua_package_path *elt;
+
+ conf = ts_lua_get_instance_conf(L);
+ if (conf == NULL) {
+ return luaL_error(L, "cann't get the instance conf.");
+ }
+
+ data = luaL_checklstring(L, 1, &dlen);
+ end = data + dlen;
+
+ ptr = data;
+ n = 0;
+
+ while (ptr < end) {
+ hit = memchr(ptr, ';', end - ptr);
+
+ if (hit) {
+ item_len = hit - ptr;
+
+ } else {
+ item_len = end - ptr;
+ }
+
+ if (item_len > 0) {
+
+ for (i = 0; i < g_cpath_cnt; i++) {
+ if (g_cpath[i].len == item_len && memcmp(g_cpath[i].name, ptr, item_len) == 0) // exist
+ {
+ break;
+ }
+ }
+
+ if (i >= g_cpath_cnt) {
+
+ if (n + i >= TS_LUA_MAX_PACKAGE_NUM)
+ return luaL_error(L, "extended package cpath number exceeds %d.", TS_LUA_MAX_PACKAGE_NUM);
+
+ pp[n].name = (char *) ptr;
+ pp[n].len = item_len;
+ n++;
+ }
+ }
+
+ ptr += item_len + 1; // ??
+ }
+
+ if (n > 0) {
+ ts_lua_add_package_cpath_items(L, pp, n);
+
+ if (conf->_last) {
+ elt = &g_cpath[g_cpath_cnt];
+
+ for (i = 0; i < n; i++) {
+ elt->len = pp[i].len;
+ elt->name = (char *) TSmalloc(pp[i].len);
+ memcpy(elt->name, pp[i].name, pp[i].len);
+ elt++;
+ }
+
+ g_cpath_cnt += n;
+ }
+ }
+
+ return 0;
+}
+
+static int
+ts_lua_add_package_cpath_items(lua_State * L, ts_lua_package_path * pp, int n)
+{
+ int i, base;
+ const char *old_path;
+ char new_path[2048];
+ size_t old_path_len, new_path_len;
+
+ base = lua_gettop(L);
+
+ lua_getglobal(L, "package");
+
+ if (!lua_istable(L, -1)) {
+ return luaL_error(L, "'package' table does not exist.");
+ }
+
+ lua_getfield(L, -1, "cpath"); /* get old package.cpath */
+
+ old_path = lua_tolstring(L, -1, &old_path_len);
+ if (old_path[old_path_len - 1] == ';')
+ old_path_len--;
+
+ new_path_len = snprintf(new_path, sizeof(new_path) - 32, "%.*s", (int) old_path_len, old_path);
+
+ for (i = 0; i < n; i++) {
+ if (new_path_len + pp[i].len + 1 >= sizeof(new_path)) {
+ fprintf(stderr, "extended package.cpath is too long.\n");
+ return -1;
+ }
+
+ new_path[new_path_len++] = ';';
+ memcpy(new_path + new_path_len, pp[i].name, pp[i].len);
+ new_path_len += pp[i].len;
+ }
+
+ new_path[new_path_len] = 0;
+
+ lua_pushlstring(L, new_path, new_path_len);
+ lua_setfield(L, -3, "cpath");
+
+ lua_settop(L, base);
+
+ return 0;
+}
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/83f16762/plugins/experimental/ts_lua/ts_lua_package.h
----------------------------------------------------------------------
diff --git a/plugins/experimental/ts_lua/ts_lua_package.h b/plugins/experimental/ts_lua/ts_lua_package.h
new file mode 100644
index 0000000..ae5a467
--- /dev/null
+++ b/plugins/experimental/ts_lua/ts_lua_package.h
@@ -0,0 +1,27 @@
+/*
+ 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.
+*/
+
+
+#ifndef _TS_LUA_PACKAGE_H
+#define _TS_LUA_PACKAGE_H
+
+#include "ts_lua_common.h"
+
+void ts_lua_inject_package_api(lua_State * L);
+
+#endif
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/83f16762/plugins/experimental/ts_lua/ts_lua_remap.c
----------------------------------------------------------------------
diff --git a/plugins/experimental/ts_lua/ts_lua_remap.c b/plugins/experimental/ts_lua/ts_lua_remap.c
index 1678d22..4df7ccf 100644
--- a/plugins/experimental/ts_lua/ts_lua_remap.c
+++ b/plugins/experimental/ts_lua/ts_lua_remap.c
@@ -22,27 +22,19 @@
typedef enum
{
- TS_LUA_REMAP_NO_REMAP = 0,
- TS_LUA_REMAP_DID_REMAP,
- TS_LUA_REMAP_NO_REMAP_STOP,
- TS_LUA_REMAP_DID_REMAP_STOP,
- TS_LUA_REMAP_ERROR = -1,
+ TS_LUA_REMAP_NO_REMAP = TSREMAP_NO_REMAP,
+ TS_LUA_REMAP_DID_REMAP = TSREMAP_DID_REMAP,
+ TS_LUA_REMAP_NO_REMAP_STOP = TSREMAP_NO_REMAP_STOP,
+ TS_LUA_REMAP_DID_REMAP_STOP = TSREMAP_DID_REMAP_STOP,
+ TS_LUA_REMAP_ERROR = TSREMAP_ERROR
} TSLuaRemapStatus;
-int ts_lua_remap_status_id[] = {
- TS_LUA_REMAP_NO_REMAP,
- TS_LUA_REMAP_DID_REMAP,
- TS_LUA_REMAP_NO_REMAP_STOP,
- TS_LUA_REMAP_DID_REMAP_STOP,
- TS_LUA_REMAP_ERROR,
-};
-
-char *ts_lua_remap_status_string[] = {
- "TS_LUA_REMAP_NO_REMAP",
- "TS_LUA_REMAP_DID_REMAP",
- "TS_LUA_REMAP_NO_REMAP_STOP",
- "TS_LUA_REMAP_DID_REMAP_STOP",
- "TS_LUA_REMAP_ERROR",
+ts_lua_var_item ts_lua_remap_status_vars[] = {
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_REMAP_NO_REMAP),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_REMAP_DID_REMAP),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_REMAP_NO_REMAP_STOP),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_REMAP_DID_REMAP_STOP),
+ TS_LUA_MAKE_VAR_ITEM(TS_LUA_REMAP_ERROR)
};
@@ -60,8 +52,8 @@ ts_lua_inject_remap_variables(lua_State * L)
{
int i;
- for (i = 0; i < sizeof(ts_lua_remap_status_string) / sizeof(char *); i++) {
- lua_pushinteger(L, ts_lua_remap_status_id[i]);
- lua_setglobal(L, ts_lua_remap_status_string[i]);
+ for (i = 0; i < sizeof(ts_lua_remap_status_vars) / sizeof(ts_lua_var_item); i++) {
+ lua_pushinteger(L, ts_lua_remap_status_vars[i].nvar);
+ lua_setglobal(L, ts_lua_remap_status_vars[i].svar);
}
}
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/83f16762/plugins/experimental/ts_lua/ts_lua_server_request.c
----------------------------------------------------------------------
diff --git a/plugins/experimental/ts_lua/ts_lua_server_request.c b/plugins/experimental/ts_lua/ts_lua_server_request.c
index 3a38a6c..4f43ea4 100644
--- a/plugins/experimental/ts_lua/ts_lua_server_request.c
+++ b/plugins/experimental/ts_lua/ts_lua_server_request.c
@@ -20,14 +20,47 @@
#include <arpa/inet.h>
#include "ts_lua_util.h"
+#define TS_LUA_CHECK_SERVER_REQUEST_HDR(http_ctx) \
+do { \
+ if (!http_ctx->server_request_hdrp) { \
+ if (TSHttpTxnServerReqGet(http_ctx->txnp, \
+ &http_ctx->server_request_bufp, \
+ &http_ctx->server_request_hdrp) != TS_SUCCESS) { \
+ return 0; \
+ } \
+ } \
+} while(0)
+
+
+#define TS_LUA_CHECK_SERVER_REQUEST_URL(http_ctx) \
+do { \
+ if (!http_ctx->server_request_url) { \
+ TS_LUA_CHECK_SERVER_REQUEST_HDR(http_ctx); \
+ if (TSHttpHdrUrlGet(http_ctx->server_request_bufp, \
+ http_ctx->server_request_hdrp, \
+ &http_ctx->server_request_url) != TS_SUCCESS) { \
+ return 0; \
+ } \
+ } \
+} while(0)
+
+
static void ts_lua_inject_server_request_header_api(lua_State * L);
+static void ts_lua_inject_server_request_headers_api(lua_State * L);
static void ts_lua_inject_server_request_get_header_size_api(lua_State * L);
static void ts_lua_inject_server_request_get_body_size_api(lua_State * L);
+static void ts_lua_inject_server_request_uri_api(lua_State * L);
+static void ts_lua_inject_server_request_uri_args_api(lua_State * L);
static int ts_lua_server_request_header_get(lua_State * L);
static int ts_lua_server_request_header_set(lua_State * L);
+static int ts_lua_server_request_get_headers(lua_State * L);
static int ts_lua_server_request_get_header_size(lua_State * L);
static int ts_lua_server_request_get_body_size(lua_State * L);
+static int ts_lua_server_request_get_uri(lua_State * L);
+static int ts_lua_server_request_set_uri(lua_State * L);
+static int ts_lua_server_request_set_uri_args(lua_State * L);
+static int ts_lua_server_request_get_uri_args(lua_State * L);
void
@@ -36,9 +69,13 @@ ts_lua_inject_server_request_api(lua_State * L)
lua_newtable(L);
ts_lua_inject_server_request_header_api(L);
+ ts_lua_inject_server_request_headers_api(L);
ts_lua_inject_server_request_get_header_size_api(L);
ts_lua_inject_server_request_get_body_size_api(L);
+ ts_lua_inject_server_request_uri_api(L);
+ ts_lua_inject_server_request_uri_args_api(L);
+
lua_setfield(L, -2, "server_request");
}
@@ -168,6 +205,54 @@ ts_lua_server_request_header_set(lua_State * L)
}
static void
+ts_lua_inject_server_request_headers_api(lua_State * L)
+{
+ lua_pushcfunction(L, ts_lua_server_request_get_headers);
+ lua_setfield(L, -2, "get_headers");
+}
+
+static int
+ts_lua_server_request_get_headers(lua_State * L)
+{
+ const char *name;
+ const char *value;
+ int name_len;
+ int value_len;
+ TSMLoc field_loc;
+ TSMLoc next_field_loc;
+
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ TS_LUA_CHECK_SERVER_REQUEST_HDR(http_ctx);
+
+ lua_newtable(L);
+
+ field_loc = TSMimeHdrFieldGet(http_ctx->server_request_bufp, http_ctx->server_request_hdrp, 0);
+
+ while (field_loc) {
+
+ name = TSMimeHdrFieldNameGet(http_ctx->server_request_bufp, http_ctx->server_request_hdrp, field_loc, &name_len);
+ if (name && name_len) {
+
+ value =
+ TSMimeHdrFieldValueStringGet(http_ctx->server_request_bufp, http_ctx->server_request_hdrp, field_loc, -1,
+ &value_len);
+ lua_pushlstring(L, name, name_len);
+ lua_pushlstring(L, value, value_len);
+ lua_rawset(L, -3);
+ }
+
+ next_field_loc = TSMimeHdrFieldNext(http_ctx->server_request_bufp, http_ctx->server_request_hdrp, field_loc);
+ TSHandleMLocRelease(http_ctx->server_request_bufp, http_ctx->server_request_hdrp, field_loc);
+ field_loc = next_field_loc;
+ }
+
+ return 1;
+}
+
+static void
ts_lua_inject_server_request_get_header_size_api(lua_State * L)
{
lua_pushcfunction(L, ts_lua_server_request_get_header_size);
@@ -208,3 +293,112 @@ ts_lua_server_request_get_body_size(lua_State * L)
return 1;
}
+
+static void
+ts_lua_inject_server_request_uri_api(lua_State * L)
+{
+ lua_pushcfunction(L, ts_lua_server_request_set_uri);
+ lua_setfield(L, -2, "set_uri");
+
+ lua_pushcfunction(L, ts_lua_server_request_get_uri);
+ lua_setfield(L, -2, "get_uri");
+}
+
+static int
+ts_lua_server_request_get_uri(lua_State * L)
+{
+ char uri[TS_LUA_MAX_URL_LENGTH];
+ const char *path;
+ int path_len;
+ int uri_len;
+
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ TS_LUA_CHECK_SERVER_REQUEST_URL(http_ctx);
+
+ path = TSUrlPathGet(http_ctx->server_request_bufp, http_ctx->server_request_url, &path_len);
+
+ uri_len = snprintf(uri, TS_LUA_MAX_URL_LENGTH, "/%.*s", path_len, path);
+
+ lua_pushlstring(L, uri, uri_len);
+
+ return 1;
+}
+
+static int
+ts_lua_server_request_set_uri(lua_State * L)
+{
+ const char *path;
+ size_t path_len;
+
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ TS_LUA_CHECK_SERVER_REQUEST_URL(http_ctx);
+
+ path = luaL_checklstring(L, 1, &path_len);
+
+ if (*path == '/') {
+ path++;
+ path_len--;
+ }
+
+ TSUrlPathSet(http_ctx->server_request_bufp, http_ctx->server_request_url, path, path_len);
+
+ return 0;
+}
+
+static void
+ts_lua_inject_server_request_uri_args_api(lua_State * L)
+{
+ lua_pushcfunction(L, ts_lua_server_request_set_uri_args);
+ lua_setfield(L, -2, "set_uri_args");
+
+ lua_pushcfunction(L, ts_lua_server_request_get_uri_args);
+ lua_setfield(L, -2, "get_uri_args");
+}
+
+static int
+ts_lua_server_request_set_uri_args(lua_State * L)
+{
+ const char *param;
+ size_t param_len;
+
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ TS_LUA_CHECK_SERVER_REQUEST_URL(http_ctx);
+
+ param = luaL_checklstring(L, 1, ¶m_len);
+ TSUrlHttpQuerySet(http_ctx->server_request_bufp, http_ctx->server_request_url, param, param_len);
+
+ return 0;
+}
+
+static int
+ts_lua_server_request_get_uri_args(lua_State * L)
+{
+ const char *param;
+ int param_len;
+
+ ts_lua_http_ctx *http_ctx;
+
+ http_ctx = ts_lua_get_http_ctx(L);
+
+ TS_LUA_CHECK_SERVER_REQUEST_URL(http_ctx);
+
+ param = TSUrlHttpQueryGet(http_ctx->server_request_bufp, http_ctx->server_request_url, ¶m_len);
+
+ if (param && param_len > 0) {
+ lua_pushlstring(L, param, param_len);
+
+ } else {
+ lua_pushnil(L);
+ }
+
+ return 1;
+}