URI::port

Description

Returns the host port from the given URI.

Syntax

URI::port <uri>

URI::port <uri>

  • Returns the port from the given URI. Returns 80 by default for URIs without a protocol or 0 if the protocol is unknown.

Examples

when HTTP_REQUEST {
  set port [URI::port [HTTP::uri]]
  log local0. "Host port of uri [HTTP::uri] is $port"
}

The default seems to be port 80 if a port isn’t specified in the URI. Here are a few tests (on 10.2.4):
when RULE_INIT {
    # Loop through some test URLs and URIs and log the URI::port value
    foreach uri [list \
        http://example.com/file.ext \
        http://example.com:80/file.ext \
        https://example.com:443/file.ext \
        https://example.com:8443/file.ext \
        ftp://example.com/file.ext \
        sip://example.com/file.ext \
        myproto://example.com/file.ext \
        /example.com \
        /uri?url=http://example.com/uri \
    ] {
        log local0. "\[URI::port $uri\]: [URI::port $uri]"
    }
}

Log output
[URI::port http://example.com/file.ext]: 80
[URI::port http://example.com:80/file.ext]: 80
[URI::port https://example.com:443/file.ext]: 443
[URI::port https://example.com:8443/file.ext]: 8443
[URI::port ftp://example.com/file.ext]: 21
[URI::port sip://example.com/file.ext]: 5060
[URI::port myproto://example.com/file.ext]: 0
[URI::port /example.com]: 80
[URI::port /uri?url=http://example.com/uri]: 80