static int
ts_lua_server_request_server_addr_get_port(lua_State *L)
{
  struct sockaddr const *server_ip;
  ts_lua_http_ctx *http_ctx;
  int port;

  http_ctx = ts_lua_get_http_ctx(L);

  server_ip = TSHttpTxnServerAddrGet(http_ctx->txnp);

  if (server_ip == NULL) {
    lua_pushnil(L);

  } else {
    if (server_ip->sa_family == AF_INET) {
      port = ((struct sockaddr_in *)server_ip)->sin_port;
    } else {
      port = ((struct sockaddr_in6 *)server_ip)->sin6_port;
    }

    lua_pushnumber(L, ntohs(port));
  }

  return 1;
}
static int
ts_lua_server_request_server_addr_get_outgoing_port(lua_State *L)
{
  struct sockaddr const *outgoing_addr;
  ts_lua_http_ctx *http_ctx;
  int port;

  http_ctx = ts_lua_get_http_ctx(L);

  outgoing_addr = TSHttpTxnOutgoingAddrGet(http_ctx->txnp);

  if (outgoing_addr == NULL) {
    lua_pushnil(L);

  } else {
    if (outgoing_addr->sa_family == AF_INET) {
      port = ((struct sockaddr_in *)outgoing_addr)->sin_port;
    } else {
      port = ((struct sockaddr_in6 *)outgoing_addr)->sin6_port;
    }

    lua_pushnumber(L, ntohs(port));
  }

  return 1;
}
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);

  if (uri_len >= TS_LUA_MAX_URL_LENGTH) {
    lua_pushlstring(L, uri, TS_LUA_MAX_URL_LENGTH - 1);
  } else {
    lua_pushlstring(L, uri, uri_len);
  }

  return 1;
}
static int
ts_lua_server_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_SERVER_RESPONSE_HDR(http_ctx);

  version = TSHttpHdrVersionGet(http_ctx->server_response_bufp, http_ctx->server_response_hdrp);

  n = snprintf(buf, sizeof(buf), "%d.%d", TS_HTTP_MAJOR(version), TS_HTTP_MINOR(version));

  if(n >= (int)sizeof(buf)) {
    lua_pushlstring(L, buf, sizeof(buf) - 1);
  } else if(n > 0) {
    lua_pushlstring(L, buf, n);
  }
  
  return 1;
}
static int
ts_lua_client_response_header_set(lua_State * L)
{
  const char *key;
  const char *val;
  size_t val_len;
  size_t key_len;
  int remove;

  TSMLoc field_loc;

  ts_lua_http_ctx *http_ctx;

  http_ctx = ts_lua_get_http_ctx(L);

  remove = 0;
  val = NULL;

  /*  we skip the first argument that is the table */
  key = luaL_checklstring(L, 2, &key_len);
  if (lua_isnil(L, 3)) {
    remove = 1;
  } else {
    val = luaL_checklstring(L, 3, &val_len);
  }

  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;
    }
  }

  field_loc = TSMimeHdrFieldFind(http_ctx->client_response_bufp, http_ctx->client_response_hdrp, key, key_len);

  if (remove) {
    if (field_loc) {
      TSMimeHdrFieldDestroy(http_ctx->client_response_bufp, http_ctx->client_response_hdrp, field_loc);
    }

  } else if (field_loc) {
    TSMimeHdrFieldValueStringSet(http_ctx->client_response_bufp, http_ctx->client_response_hdrp, field_loc, 0, val,
                                 val_len);

  } else if (TSMimeHdrFieldCreateNamed(http_ctx->client_response_bufp, http_ctx->client_response_hdrp,
                                       key, key_len, &field_loc) != TS_SUCCESS) {
    TSError("[%s] TSMimeHdrFieldCreateNamed error", __FUNCTION__);
    return 0;

  } else {
    TSMimeHdrFieldValueStringSet(http_ctx->client_response_bufp, http_ctx->client_response_hdrp, field_loc, -1, val,
                                 val_len);
    TSMimeHdrFieldAppend(http_ctx->client_response_bufp, http_ctx->client_response_hdrp, field_loc);
  }

  if (field_loc)
    TSHandleMLocRelease(http_ctx->client_response_bufp, http_ctx->client_response_hdrp, field_loc);

  return 0;
}
static int
ts_lua_client_request_client_addr_get_port(lua_State *L)
{
    struct sockaddr const   *client_ip;
    ts_lua_http_ctx         *http_ctx;
    int                     port;

    http_ctx = ts_lua_get_http_ctx(L);

    client_ip = TSHttpTxnClientAddrGet(http_ctx->txnp);

    if (client_ip == NULL) {
        lua_pushnil(L);

    } else {

        if (client_ip->sa_family == AF_INET) {
            port = ((struct sockaddr_in *)client_ip)->sin_port;
        } else {
            port = ((struct sockaddr_in6 *)client_ip)->sin6_port;
        }

        lua_pushnumber(L, port);
    }

    return 1;
}
static int
ts_lua_client_request_client_addr_get_ip(lua_State *L)
{
    struct sockaddr const   *client_ip;
    char                    cip[128];
    ts_lua_http_ctx         *http_ctx;

    http_ctx = ts_lua_get_http_ctx(L);

    client_ip = TSHttpTxnClientAddrGet(http_ctx->txnp);

    if (client_ip == NULL) {
        lua_pushnil(L);

    } else {

        if (client_ip->sa_family == AF_INET) {
            inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)client_ip)->sin_addr, cip, sizeof(cip));
        } else {
            inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)client_ip)->sin6_addr, cip, sizeof(cip));
        }

        lua_pushstring(L, cip);
    }

    return 1;
}
static int
ts_lua_server_response_header_get(lua_State *L)
{
  const char *key;
  const char *val;
  int val_len;
  size_t key_len;

  TSMLoc field_loc;
  ts_lua_http_ctx *http_ctx;

  http_ctx = ts_lua_get_http_ctx(L);

  /*  we skip the first argument that is the table */
  key = luaL_checklstring(L, 2, &key_len);

  TS_LUA_CHECK_SERVER_RESPONSE_HDR(http_ctx);

  if (key && key_len) {
    field_loc = TSMimeHdrFieldFind(http_ctx->server_response_bufp, http_ctx->server_response_hdrp, key, key_len);
    if (field_loc) {
      val = TSMimeHdrFieldValueStringGet(http_ctx->server_response_bufp, http_ctx->server_response_hdrp, field_loc, -1, &val_len);
      lua_pushlstring(L, val, val_len);
      TSHandleMLocRelease(http_ctx->server_response_bufp, http_ctx->server_response_hdrp, field_loc);

    } else {
      lua_pushnil(L);
    }

  } else {
    lua_pushnil(L);
  }

  return 1;
}
static int
ts_lua_http_server_intercept(lua_State *L)
{
  TSCont contp;
  int type;
  ts_lua_http_ctx *http_ctx;

  http_ctx = ts_lua_get_http_ctx(L);
  http_ctx->has_hook = 1;

  type = lua_type(L, 1);

  if (type != LUA_TFUNCTION) {
    TSError("[%s] param in ts.http.server_intercept should be a function", __FUNCTION__);
    return 0;
  }

  lua_pushvalue(L, 1);
  lua_setglobal(L, TS_LUA_FUNCTION_HTTP_SERVER_INTERCEPT);

  http_ctx->intercept_type = TS_LUA_TYPE_HTTP_SERVER_INTERCEPT;

  contp = TSContCreate(ts_lua_http_intercept_entry, TSMutexCreate());
  TSContDataSet(contp, http_ctx);
  TSHttpTxnServerIntercept(contp, http_ctx->txnp);

  return 0;
}
Beispiel #10
0
static int
ts_lua_client_request_get_pristine_url(lua_State *L)
{
    char        *url;
    int         url_len;

    TSMBuffer   bufp;
    TSMLoc      url_loc;

    ts_lua_http_ctx  *http_ctx;

    http_ctx = ts_lua_get_http_ctx(L);

    if (TSHttpTxnPristineUrlGet(http_ctx->txnp, &bufp, &url_loc) != TS_SUCCESS)
        return 0;

    url = TSUrlStringGet(bufp, url_loc, &url_len);

    if (url) {
        lua_pushlstring(L, url, url_len);
        TSfree(url);

    } else {
        lua_pushnil(L);
    }

    TSHandleMLocRelease(bufp, NULL, url_loc);

    return 1;
}
Beispiel #11
0
static int
ts_lua_http_set_cache_lookup_url(lua_State *L)
{
  const char *url;
  size_t url_len;

  ts_lua_http_ctx *http_ctx;

  http_ctx = ts_lua_get_http_ctx(L);

  url = luaL_checklstring(L, 1, &url_len);

  if (url && url_len) {
    const char *start = url;
    const char *end = url + url_len;
    TSMLoc new_url_loc;
    if (TSUrlCreate(http_ctx->client_request_bufp, &new_url_loc) == TS_SUCCESS &&
        TSUrlParse(http_ctx->client_request_bufp, new_url_loc, &start, end) == TS_PARSE_DONE &&
        TSHttpTxnCacheLookupUrlSet(http_ctx->txnp, http_ctx->client_request_bufp, new_url_loc) == TS_SUCCESS) {
      TSDebug(TS_LUA_DEBUG_TAG, "Set cache lookup URL");
    } else {
      TSError("[ts_lua] Failed to set cache lookup URL");
    }
  }

  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;
}
Beispiel #13
0
static int
ts_lua_http_set_retstatus(lua_State *L)
{
  int status;
  ts_lua_http_ctx *http_ctx;

  http_ctx = ts_lua_get_http_ctx(L);

  status = luaL_checkinteger(L, 1);
  TSHttpTxnSetHttpRetStatus(http_ctx->txnp, status);
  return 0;
}
Beispiel #14
0
static int
ts_lua_client_request_get_header_size(lua_State *L)
{
    int header_size;
    ts_lua_http_ctx  *http_ctx;

    http_ctx = ts_lua_get_http_ctx(L);

    header_size = TSHttpTxnClientReqHdrBytesGet(http_ctx->txnp);
    lua_pushnumber(L,header_size);

    return 1;
}
Beispiel #15
0
static int
ts_lua_client_request_get_body_size(lua_State *L)
{
    int64_t body_size;
    ts_lua_http_ctx  *http_ctx;

    http_ctx = ts_lua_get_http_ctx(L);

    body_size = TSHttpTxnClientReqBodyBytesGet(http_ctx->txnp);
    lua_pushnumber(L, body_size);

    return 1;
}
Beispiel #16
0
static int
ts_lua_http_set_retbody(lua_State *L)
{
  const char *body;
  size_t body_len;
  ts_lua_http_ctx *http_ctx;

  http_ctx = ts_lua_get_http_ctx(L);

  body = luaL_checklstring(L, 1, &body_len);
  TSHttpTxnErrorBodySet(http_ctx->txnp, TSstrdup(body), body_len, NULL); // Defaults to text/html
  return 0;
}
Beispiel #17
0
static int
ts_lua_http_skip_remapping_set(lua_State *L)
{
  int action;
  ts_lua_http_ctx *http_ctx;

  http_ctx = ts_lua_get_http_ctx(L);

  action = luaL_checkinteger(L, 1);

  TSSkipRemappingSet(http_ctx->txnp, action);

  return 0;
}
Beispiel #18
0
static int
ts_lua_http_resp_cache_untransformed(lua_State *L)
{
  int action;
  ts_lua_http_ctx *http_ctx;

  http_ctx = ts_lua_get_http_ctx(L);

  action = luaL_checkinteger(L, 1);

  TSHttpTxnUntransformedRespCache(http_ctx->txnp, action);

  return 0;
}
Beispiel #19
0
static int
ts_lua_http_client_packet_mark_set(lua_State * L)
{
  int value;
  ts_lua_http_ctx *http_ctx;

  http_ctx = ts_lua_get_http_ctx(L);

  value = luaL_checkinteger(L, 1);

  TSDebug(TS_LUA_DEBUG_TAG, "client packet mark set");
  TSHttpTxnClientPacketMarkSet(http_ctx->txnp, value);

  return 0;
}
Beispiel #20
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;
}
Beispiel #21
0
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;
}
Beispiel #22
0
static int
ts_lua_client_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);

    param = luaL_checklstring(L, 1, &param_len);
    TSUrlHttpQuerySet(http_ctx->client_request_bufp, http_ctx->client_request_url, param, param_len);

    return 0;
}
Beispiel #23
0
static int
ts_lua_http_set_server_resp_no_store(lua_State *L)
{
  int status;

  ts_lua_http_ctx *http_ctx;

  http_ctx = ts_lua_get_http_ctx(L);

  status = luaL_checknumber(L, 1);

  TSHttpTxnServerRespNoStoreSet(http_ctx->txnp, status);

  return 0;
}
Beispiel #24
0
static int
ts_lua_http_server_packet_tos_set(lua_State * L)
{
  int value;
  ts_lua_http_ctx *http_ctx;

  http_ctx = ts_lua_get_http_ctx(L);

  value = luaL_checkinteger(L, 1);

  TSDebug(TS_LUA_DEBUG_TAG, "server packet tos set");
  TSHttpTxnServerPacketTosSet(http_ctx->txnp, value);

  return 0;
}
Beispiel #25
0
static int
ts_lua_http_set_cache_lookup_status(lua_State *L)
{
  int status;

  ts_lua_http_ctx *http_ctx;

  http_ctx = ts_lua_get_http_ctx(L);

  status = luaL_checknumber(L, 1);

  TSHttpTxnCacheLookupStatusSet(http_ctx->txnp, status);

  return 0;
}
static int
ts_lua_server_response_get_status(lua_State * L)
{
  int status;
  ts_lua_http_ctx *http_ctx;

  http_ctx = ts_lua_get_http_ctx(L);

  TS_LUA_CHECK_SERVER_RESPONSE_HDR(http_ctx);

  status = TSHttpHdrStatusGet(http_ctx->server_response_bufp, http_ctx->server_response_hdrp);

  lua_pushinteger(L, status);

  return 1;
}
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;
}
Beispiel #28
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;
}
Beispiel #29
0
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;
}
Beispiel #30
0
static int
ts_lua_client_request_get_url(lua_State *L)
{
    const char  *url;
    int         url_len;

    ts_lua_http_ctx  *http_ctx;

    http_ctx = ts_lua_get_http_ctx(L);

    url = TSUrlStringGet(http_ctx->client_request_bufp, http_ctx->client_request_url, &url_len);

    lua_pushlstring(L, url, url_len);

    return 1;
}