Skip to content

Instantly share code, notes, and snippets.

@hnakamur
Last active November 24, 2016 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hnakamur/7f3168f7b8b0fb5539df708c851778e5 to your computer and use it in GitHub Desktop.
Save hnakamur/7f3168f7b8b0fb5539df708c851778e5 to your computer and use it in GitHub Desktop.
trafficserverのTSLuaのset_cache_lookup_urlとset_cache_urlのコードリーディング

trafficserver 6.2.0 のコードを読みました。 TSLua の set_cache_lookup_url と set_cache_url は基本的には同じことをしています。

が、URLをパースしたときのエラーを set_cache_lookup_url では処理していますが、 set_cache_url では処理していないので set_cache_lookup_url を使うのが良さそうです。

plugins/experimental/ts_lua/ts_lua_http.c#L295-L342

ts_lua_http_set_cache_lookup_url(lua_State *L)
{
  const char *url;
  size_t url_len;

  ts_lua_http_ctx *http_ctx;

  GET_HTTP_CONTEXT(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_http_set_cache_url(lua_State *L)
{
  const char *url;
  size_t url_len;

  ts_lua_http_ctx *http_ctx;

  GET_HTTP_CONTEXT(http_ctx, L);

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

  if (url && url_len) {
    if (TSCacheUrlSet(http_ctx->txnp, url, url_len) != TS_SUCCESS) {
      TSError("[ts_lua] Failed to set cache url");
    }
  }

  return 0;
}

set_cache_lookup_url: TSHttpTxnCacheLookupUrlSet

Set the current cache key URL, also referred to as the lookup URL. This must be stored in a properly allocated URL object, typically created with a TSUrlCreate() or TSUrlClone().

set_cache_url: TSCacheUrlSet

Set the cache key for the transaction txnp as the string pointed at by url of length characters. It need not be NUL-terminated. This should be called from TS_HTTP_READ_REQUEST_HDR_HOOK which is before cache lookup but late enough that the HTTP request header is available.

The APIs that modify the cache key can be called as early as TS_HTTP_READ_REQUEST_HDR_HOOK but no later than TS_HTTP_POST_REMAP_HOOK. The cache key is not only used for a cache lookup before going to origin, but also to mark the intent to write to cache on an origin response (if possible).

TSHttpTxnCacheLookupUrlSet の定義箇所 proxy/InkAPI.cc#L4885-L4915

TSReturnCode
TSHttpTxnCacheLookupUrlSet(TSHttpTxn txnp, TSMBuffer bufp, TSMLoc obj)
{
  sdk_assert(sdk_sanity_check_txn(txnp) == TS_SUCCESS);
  sdk_assert(sdk_sanity_check_mbuffer(bufp) == TS_SUCCESS);
  sdk_assert(sdk_sanity_check_url_handle(obj) == TS_SUCCESS);

  HttpSM *sm = (HttpSM *)txnp;
  URL u, *l_url;

  u.m_heap     = ((HdrHeapSDKHandle *)bufp)->m_heap;
  u.m_url_impl = (URLImpl *)obj;
  if (!u.valid()) {
    return TS_ERROR;
  }

  l_url = sm->t_state.cache_info.lookup_url;
  if (!l_url) {
    sm->t_state.cache_info.lookup_url_storage.create(NULL);
    sm->t_state.cache_info.lookup_url = &(sm->t_state.cache_info.lookup_url_storage);
    l_url                             = sm->t_state.cache_info.lookup_url;
  }

  if (!l_url || !l_url->valid()) {
    return TS_ERROR;
  } else {
    l_url->copy(&u);
  }

  return TS_SUCCESS;
}

TSHttpTxnCacheLookupUrlSet の参照箇所は ts_lua_http_set_cache_lookup_url のみ

proxy/InkAPI.cc#L7182-L7203

TSReturnCode
TSCacheUrlSet(TSHttpTxn txnp, const char *url, int length)
{
  sdk_assert(sdk_sanity_check_txn(txnp) == TS_SUCCESS);

  HttpSM *sm = (HttpSM *)txnp;
  Debug("cache_url", "[TSCacheUrlSet]");

  if (sm->t_state.cache_info.lookup_url == NULL) {
    Debug("cache_url", "[TSCacheUrlSet] changing the cache url to: %s", url);

    if (length == -1)
      length = strlen(url);

    sm->t_state.cache_info.lookup_url_storage.create(NULL);
    sm->t_state.cache_info.lookup_url = &(sm->t_state.cache_info.lookup_url_storage);
    sm->t_state.cache_info.lookup_url->parse(url, length);
    return TS_SUCCESS;
  }

  return TS_ERROR;
}

TSCacheUrlSet の参照箇所は ts_lua_http_set_cache_url 以外に以下の4箇所ある。

plugins/cacheurl/cacheurl.cc
329:      if (TSCacheUrlSet(txnp, newurl, strlen(newurl)) != TS_SUCCESS) {

plugins/experimental/cache_range_requests/cache_range_requests.cc
109:          if (TS_SUCCESS != TSCacheUrlSet(txnp, cache_key_url, strlen(cache_key_url))) {

plugins/experimental/cachekey/cachekey.cc
595:  return TSCacheUrlSet(_txn, &(_key[0]), _key.size()) == TS_SUCCESS;

lib/atscppapi/src/Transaction.cc
292:  TSReturnCode res = TSCacheUrlSet(state_->txn_, cache_url.c_str(), cache_url.length());

proxy/http/HttpTransact.h#L831-L839

  struct State {
    HttpTransactMagic_t m_magic;
    HttpSM *state_machine;
    Arena arena;
    HttpConfigParams *http_config_param;
    CacheLookupInfo cache_info;

proxy/http/HttpTransact.h#L595-L602


  typedef struct _CacheLookupInfo {
    HttpTransact::CacheAction_t action;
    HttpTransact::CacheAction_t transform_action;
    HttpTransact::CacheWriteStatus_t write_status;
    HttpTransact::CacheWriteStatus_t transform_write_status;
    URL *lookup_url;

proxy/hdrs/URL.h#L716-L740

/**
  Parser doesn't clear URL first, so if you parse over a non-clear URL,
  the resulting URL may contain some of the previous data.
 */
inline MIMEParseResult
URL::parse(const char **start, const char *end)
{
  ink_assert(valid());
  return url_parse(m_heap, m_url_impl, start, end, true);
}

/**
  Parser doesn't clear URL first, so if you parse over a non-clear URL,
  the resulting URL may contain some of the previous data.
 */
inline MIMEParseResult
URL::parse(const char *str, int length)
{
  ink_assert(valid());
  if (length < 0)
    length = (int)strlen(str);
  return parse(&str, str + length);
}

proxy/hdrs/URL.cc#L1159-L1164

MIMEParseResult
url_parse(HdrHeap *heap, URLImpl *url, const char **start, const char *end, bool copy_strings_p)
{
  MIMEParseResult zret = url_parse_scheme(heap, url, start, end, copy_strings_p);
  return PARSE_CONT == zret ? url_parse_http(heap, url, start, end, copy_strings_p) : zret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment