bool is_post_request(TSHttpTxn txnp) { TSMLoc req_loc; TSMBuffer req_bufp; if (TSHttpTxnClientReqGet(txnp, &req_bufp, &req_loc) == TS_ERROR) { TSError("Error while retrieving client request header\n"); return false; } int method_len = 0; const char *method = TSHttpHdrMethodGet(req_bufp, req_loc, &method_len); if (method_len != (int)strlen(TS_HTTP_METHOD_POST) || strncasecmp(method, TS_HTTP_METHOD_POST, method_len) != 0) { TSHandleMLocRelease(req_bufp, TS_NULL_MLOC, req_loc); return false; } TSHandleMLocRelease(req_bufp, TS_NULL_MLOC, req_loc); return true; }
/* * This function is only called for redirected requests. It illustrates * several different ways of updating INT64 stats. Some may consider * the particular use of TSDecrementStat() shown below somewhat contrived. */ void update_redirected_method_stats(TSMBuffer bufp, TSMLoc hdr_loc) { const char *txn_method; int length; int64_t tempint; txn_method = TSHttpHdrMethodGet(bufp, hdr_loc, &length); if (NULL != txn_method) { if (0 == strncmp(txn_method, TS_HTTP_METHOD_CONNECT, length)) { TSStatIntIncrement(redirect_count_connect, 1); } else if (0 == strncmp(txn_method, TS_HTTP_METHOD_DELETE, length)) { TSStatIntIncrement(redirect_count_delete, 1); } else if (0 == strncmp(txn_method, TS_HTTP_METHOD_GET, length)) { TSStatIntIncrement(redirect_count_get, 1); } else if (0 == strncmp(txn_method, TS_HTTP_METHOD_HEAD, length)) { TSStatIntIncrement(redirect_count_head, 1); } else if (0 == strncmp(txn_method, TS_HTTP_METHOD_OPTIONS, length)) { // This is a bad idea in a real plugin because it causes a race condition // with other transactions, but is here for illustrative purposes. tempint = TSStatIntGet(redirect_count_options); ++tempint; TSStatIntSet(redirect_count_options, tempint); } else if (0 == strncmp(txn_method, TS_HTTP_METHOD_POST, length)) { // Illustrative only. TSStatIntDecrement(redirect_count_post, 1); TSStatIntIncrement(redirect_count_post, 2); } else if (0 == strncmp(txn_method, TS_HTTP_METHOD_PURGE, length)) { TSStatIntIncrement(redirect_count_purge, 1); } else if (0 == strncmp(txn_method, TS_HTTP_METHOD_PUT, length)) { TSStatIntIncrement(redirect_count_put, 1); } else if (0 == strncmp(txn_method, TS_HTTP_METHOD_TRACE, length)) { TSStatIntIncrement(redirect_count_trace, 1); } else { TSStatIntIncrement(redirect_count_unknown, 1); } } }
/* * This function is only called for redirected requests. It illustrates * several different ways of updating INT64 stats. Some may consider * the particular use of TSDecrementStat() shown below somewhat contrived. */ void update_redirected_method_stats(TSMBuffer bufp, TSMLoc hdr_loc) { const char *txn_method; int length; int64_t tempint; txn_method = TSHttpHdrMethodGet(bufp, hdr_loc, &length); if (NULL != txn_method) { if (0 == strncmp(txn_method, TS_HTTP_METHOD_CONNECT, length)) INKStatIncrement(method_count_redirected_connect); else if (0 == strncmp(txn_method, TS_HTTP_METHOD_DELETE, length)) INKStatIncrement(method_count_redirected_delete); else if (0 == strncmp(txn_method, TS_HTTP_METHOD_GET, length)) INKStatIncrement(method_count_redirected_get); else if (0 == strncmp(txn_method, TS_HTTP_METHOD_HEAD, length)) INKStatFloatAddTo(method_count_redirected_head, 1); else if (0 == strncmp(txn_method, TS_HTTP_METHOD_ICP_QUERY, length)) INKStatFloatAddTo(method_count_redirected_icp_query, 1); else if (0 == strncmp(txn_method, TS_HTTP_METHOD_OPTIONS, length)) { tempint = INKStatIntGet(method_count_redirected_options); tempint++; INKStatIntSet(method_count_redirected_options, tempint); } else if (0 == strncmp(txn_method, TS_HTTP_METHOD_POST, length)) { INKStatDecrement(method_count_redirected_post); INKStatIncrement(method_count_redirected_post); INKStatIncrement(method_count_redirected_post); } else if (0 == strncmp(txn_method, TS_HTTP_METHOD_PURGE, length)) INKStatIncrement(method_count_redirected_purge); else if (0 == strncmp(txn_method, TS_HTTP_METHOD_PUT, length)) INKStatIncrement(method_count_redirected_put); else if (0 == strncmp(txn_method, TS_HTTP_METHOD_TRACE, length)) INKStatIncrement(method_count_redirected_trace); else INKStatIncrement(method_count_redirected_unknown); } }
static int ts_lua_client_request_get_method(lua_State *L) { const char *method; int method_len; ts_lua_http_ctx *http_ctx; http_ctx = ts_lua_get_http_ctx(L); method = TSHttpHdrMethodGet(http_ctx->client_request_bufp, http_ctx->client_request_hdrp, &method_len); if (method && method_len) { lua_pushlstring(L, method, method_len); } else { lua_pushnil(L); } return 1; }
static void handle_purge(TSHttpTxn txnp, PurgeInstance *purge) { TSMBuffer reqp; TSMLoc hdr_loc = NULL, url_loc = NULL; bool should_purge = false; if (TS_SUCCESS == TSHttpTxnClientReqGet(txnp, &reqp, &hdr_loc)) { int method_len = 0; const char *method = TSHttpHdrMethodGet(reqp, hdr_loc, &method_len); if ((TS_HTTP_METHOD_PURGE == method) || ((TS_HTTP_METHOD_GET == method) && purge->allow_get)) { /* First see if we require the "secret" to be passed in a header, and then use that */ if (purge->header) { TSMLoc field_loc = TSMimeHdrFieldFind(reqp, hdr_loc, purge->header, purge->header_len); if (field_loc) { const char *header; int header_len; header = TSMimeHdrFieldValueStringGet(reqp, hdr_loc, field_loc, -1, &header_len); TSDebug(PLUGIN_NAME, "Checking for %.*s == %s ?", header_len, header, purge->secret); if (header && (header_len == purge->secret_len) && !memcmp(header, purge->secret, header_len)) { should_purge = true; } TSHandleMLocRelease(reqp, hdr_loc, field_loc); } } else { /* We are matching on the path component instead of a header */ if (TS_SUCCESS == TSHttpHdrUrlGet(reqp, hdr_loc, &url_loc)) { int path_len = 0; const char *path = TSUrlPathGet(reqp, url_loc, &path_len); TSDebug(PLUGIN_NAME, "Checking PATH = %.*s", path_len, path); if (path && (path_len >= purge->secret_len)) { int s_path = path_len - 1; while ((s_path >= 0) && ('/' != path[s_path])) { /* No memrchr in OSX */ --s_path; } if (!memcmp(s_path > 0 ? path + s_path + 1 : path, purge->secret, purge->secret_len)) { should_purge = true; } } TSHandleMLocRelease(reqp, hdr_loc, url_loc); } } } TSHandleMLocRelease(reqp, TS_NULL_MLOC, hdr_loc); } /* Setup the continuation to handle this request if appropriate, if not, set the GenID if needed */ if (should_purge) { TSCont cont = TSContCreate(purge_cont, TSMutexCreate()); TSContDataSet(cont, purge); TSHttpTxnHookAdd(txnp, TS_HTTP_CACHE_LOOKUP_COMPLETE_HOOK, cont); TSHttpTxnHookAdd(txnp, TS_HTTP_SEND_RESPONSE_HDR_HOOK, cont); } else if (purge->gen_id > 0) { TSHttpTxnConfigIntSet(txnp, TS_CONFIG_HTTP_CACHE_GENERATION, purge->gen_id); } }