【问题标题】:Regsub not manipulating string from URLRegsub 没有从 URL 操作字符串
【发布时间】:2020-07-18 18:32:26
【问题描述】:

我在使用这个iRule时遇到了一个奇怪的TCL错误,错误是:

- 从“HTTP::uri”中调用的 ERR_ARG(第 2 行) [regsub "/3dnotification" [HTTP::uri] ""] "

这是一个 F5 规则代码。

这是我尝试过的:

   when HTTP_REQUEST 
{ 
    if { not ( [string tolower [HTTP::uri]] starts_with "/socket.io" )} then { 
        HTTP::uri [regsub "/3dnotification" [HTTP::uri] ""] 
    
    # need to strip trailing slash on URI otherwise results in 404 for resources...
        HTTP::uri [regsub "\/$" [HTTP::uri] ""]
    } elseif { [string tolower [HTTP::header Upgrade]] contains "websocket" } {
        ONECONNECT::reuse disable
        set oc_reuse_disable 1
    }
    HTTP::header replace "X-Forwarded-ContextPath" "/"
}
when SERVER_CONNECTED {
    if { [info exists oc_reuse_disable] } {
        # Optional; unnecessary if the HTTP profile is disabled (goes into passthrough mode).
        ONECONNECT::detach disable
        unset oc_reuse_disable
    }
}

【问题讨论】:

  • notstarts_withcontains 是什么?它们不是标准的 expr 语法。
  • 您可能想提及您正在使用的程序,因为这似乎是针对使用 tcl 作为脚本语言的程序。
  • 在典型的失败案例中,[HTTP::uri] 的评估结果是什么?我们在这里处理 URI 的哪些部分?完整的 URI?只是路径部分?
  • 嗨,例如,如果 URL 是 example.com:8080/main/index.jsp?user=test&login=check,则 URI 将是 /main/index.jsp?user=test&login=check。我猜问题出在这里: HTTP::uri [regsub "\/$" [HTTP::uri] ""]
  • 您是否收到所有 URI 的错误,还是只有某些 URI?

标签: tcl f5


【解决方案1】:

由于 URI 要么是完整的 URI,要么是无协议部分(我不能从你说的内容中完全分辨出哪一个;我假设两者都是可能的),删除前导部分或尾随部分将是有点棘手。您需要做的是首先将 URI 拆分为其组成部分,将转换应用于 path 部分,然后重新组装。拆分和重组的关键是uri package in Tcllib

package require uri

# Split the URI and pick out the path from the parts
set uriParts [uri::split [HTTP::uri]]
set path [dict get $uriParts path]

# Do the transforms
set path [regsub "/3dnotification" $path ""]
set path [string trimright $path "/"];  # A different way to remove trailing slashes

# Reassemble and write back
dict set uriParts path $path
HTTP::uri [uri::join {*}$uriParts]

我假设您将package require(或其他任何您需要的以获取代码)放在脚本的顶部,其余的放在右边的when 子句中.


为了让您了解 URI 拆分的实际作用,以下是您的 URI 拆分示例(在交互式 tclsh 会话中):

% set uri "http://www.example.com:8080/main/index.jsp?user=test&login=check"
http://www.example.com:8080/main/index.jsp?user=test&login=check
% uri::split $uri
fragment {} port 8080 path main/index.jsp scheme http host www.example.com query user=test&login=check pbare 0 pwd {} user {}

如您所见,path 部分只是 main/index.jsp,它比整个 URI 更容易使用。

【讨论】:

    猜你喜欢
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    相关资源
    最近更新 更多