ad_conn (public)

 ad_conn [ args... ]

Defined in packages/acs-tcl/tcl/request-processor-procs.tcl

Returns a property about the connection. See the request processor documentation for an (incomplete) list of allowable values. If option "-set" is passed as first argument, then ad_conn sets the specified property, otherwise it returns its value. If the property has not been set directly by OpenACS it will be passed on to AOLserver's/NaviServer's ns_conn If the property is not a valid option for ns_conn either then it will throw an error.

Valid options for ad_conn are: ajax_p, behind_proxy_p, behind_secure_proxy_p, bot_p, browser_id, deferred_dml, extra_url, instance_name, last_issue, mobile_p, node_id, object_id, object_type, object_url, package_id, package_key, package_url, path_info, peeraddr, recursion_count, request, sec_validated, session_id, start_clicks, subsite_id, subsite_node_id, subsite_url, system_p, token, untrusted_user_id, user_id, vhost_package_url, vhost_subsite_url, vhost_url.

See Also:

Partial Call Graph (max 5 caller/called nodes):
%3 test_cookie_consent__setup cookie_consent__setup (test cookie-consent) ad_conn ad_conn test_cookie_consent__setup->ad_conn acs::icanuse acs::icanuse (public) ad_conn->acs::icanuse apm_package_id_from_key apm_package_id_from_key (public) ad_conn->apm_package_id_from_key parameter::get parameter::get (public) ad_conn->parameter::get site_node::closest_ancestor_package site_node::closest_ancestor_package (public) ad_conn->site_node::closest_ancestor_package site_node::get site_node::get (public) ad_conn->site_node::get Class ::xo::db::Class Class ::xo::db::Class (public) Class ::xo::db::Class->ad_conn Class ::xo::lti::LTI Class ::xo::lti::LTI (public) Class ::xo::lti::LTI->ad_conn Class ::xowf::test_item::Answer_manager Class ::xowf::test_item::Answer_manager (public) Class ::xowf::test_item::Answer_manager->ad_conn Class ::xowiki::formfield::numeric Class ::xowiki::formfield::numeric (public) Class ::xowiki::formfield::numeric->ad_conn Class ::xowiki::includelet::categories Class ::xowiki::includelet::categories (public) Class ::xowiki::includelet::categories->ad_conn

Testcases:
cookie_consent__setup
Source code:
    global ad_conn

    set flag [lindex $args 0]
    if {[string index $flag 0] ne "-"} {
        set var $flag
        set flag "-get"
    } else {
        set var [lindex $args 1]
    }

    switch -- $flag {
        -connected_p {
            return [info exists ad_conn(request)]
        }

        -set {
            set ad_conn($var) [lindex $args 2]
        }

        -unset {
            unset ad_conn($var)
        }

        -reset {
            if {[info exists ad_conn]} {
                unset ad_conn
            }
            array set ad_conn {
                request ""
                sec_validated ""
                browser_id ""
                session_id ""
                user_id ""
                untrusted_user_id 0
                token ""
                last_issue ""
                deferred_dml ""
                start_clicks ""
                node_id ""
                object_id ""
                object_url ""
                object_type ""
                package_id ""
                package_url ""
                instance_name ""
                package_key ""
                extra_url ""
                file ""
                system_p 0
                path_info ""
                system_p 0
                recursion_count 0
                form_count -1
            }
            array unset ad_conn subsite_id
            array unset ad_conn locale
        }

        -get {
            # Special handling for the form, because "ns_conn form" can
            # cause the server to hang until the socket times out.  This
            # happens on pages handling multipart form data, where
            # ad_page_contract already has called ns_getform and has
            # retrieved all data from the client. ns_getform has its
            # own caching, so calling it instead of [ns_conn form]
            # is OK.

            switch -- $var {
                form {
                    return [ns_getform]
                }
                all {
                    return [array get ad_conn]
                }
                default {
                    if { [info exists ad_conn($var)] } {
                        return $ad_conn($var)
                    }

                    # Fallback
                    switch -- $var {
                        locale {
                            set ad_conn(locale) [parameter::get  -parameter SiteWideLocale  -package_id [apm_package_id_from_key "acs-lang"]  -default {en_US}]
                            return $ad_conn(locale)
                        }
                        node_id -
                        package_id {
                            # This is just a fallback, when the request
                            # processor has failed to set the actual site
                            # node, e.g. on invalid requests. When the
                            # fallback is missing, ns_conn spits out an
                            # error message since it does not know what a
                            # "node_id" is. The fallback is especially
                            # necessary, when a template is used for the
                            # error message, the templating system cannot
                            # determine the appropriate template without
                            # the node_id. In case of failure, the
                            # top-level node_is is returned.
                            set node [site_node::get -url /]
                            set ad_conn($var) [dict get $node $var]
                            ns_log notice "ad_conn: request processor did not set <ad_conn $var>, fallback: $ad_conn($var)"
                            return $ad_conn($var)
                        }
                        untrusted_user_id -
                        session_id -
                        user_id {
                            # Fallbacks, see above.
                            set ad_conn($var) 0
                            ns_log debug "ad_conn: request processor did not set <ad_conn $var>, fallback: $ad_conn($var)"
                            return $ad_conn($var)
                        }
                        extra_url -
                        locale -
                        language -
                        charset {
                            # Fallbacks, see above.
                            set ad_conn($var""
                            ns_log notice "ad_conn: request processor did not set <ad_conn $var>, use empty fallback value"
                            return $ad_conn($var)
                        }
                        subsite_node_id {
                            set ad_conn(subsite_node_id) [site_node::closest_ancestor_package  -node_id [ad_conn node_id]  -package_key [subsite::package_keys]  -include_self  -element "node_id"]
                            return $ad_conn(subsite_node_id)
                        }
                        subsite_id {
                            set ad_conn(subsite_id) [site_node::get_object_id  -node_id [ad_conn subsite_node_id]]
                            return $ad_conn(subsite_id)
                        }
                        subsite_url {
                            set ad_conn(subsite_url) [site_node::get_url  -node_id [ad_conn subsite_node_id]]
                            return $ad_conn(subsite_url)
                        }
                        vhost_subsite_url {
                            set ad_conn(vhost_subsite_url) [subsite::get_url]
                            return $ad_conn(vhost_subsite_url)
                        }
                        vhost_package_url {
                            set subsite_package_url [string range [ad_conn package_url] [string length [ad_conn subsite_url]] end]
                            set ad_conn(vhost_package_url) "[ad_conn vhost_subsite_url]$subsite_package_url"
                            return $ad_conn(vhost_package_url)
                        }
                        vhost_url {
                            set vhost_url [string range [ad_conn url] [string length [ad_conn subsite_url]] end]
                            set ad_conn(vhost_url) "[ad_conn vhost_subsite_url]$vhost_url"
                            return $ad_conn(vhost_url)
                        }
                        recursion_count {
                            # sometimes recusion_count will be uninitialized and
                            # something will call ad_conn recursion_count - return 0
                            # in that instance.  This is filters ahead of rp_filter which throw
                            # an ns_returnnotfound or something like that.
                            set ad_conn(recursion_count) 0
                            return 0
                        }
                        peeraddr {
                            #
                            # Newer versions of NaviServer (4.99.20)
                            # handle already ReverseProxyMode
                            # internally.
                            #
                            if {![acs::icanuse "ns_conn peeraddr -source"]
                                && [ns_config "ns/parameters" ReverseProxyMode false]
                            } {
                                #
                                # In case, we have an older
                                # NaviServer, try to get the address
                                # provided by a reverse proxy such as
                                # NGINX via X-Forwarded-For, if
                                # available. Note that in this case
                                # there is no validation happening.
                                #
                                set headers [ns_conn headers]
                                set i [ns_set ifind $headers "X-Forwarded-For"]
                                if {$i > -1 } {
                                    return [ns_set value $headers $i]
                                }
                            }
                            return [set ad_conn(peeraddr) [ns_conn peeraddr]]
                        }

                        mobile_p {
                            #
                            # Check, if we are used from a mobile
                            # device (heuristic based on user_agent).
                            #
                            if {[ns_conn isconnected]} {
                                set user_agent [string tolower [ns_set iget [ns_conn headers] User-Agent]]
                                set ad_conn(mobile_p) [regexp (android|webos|iphone|ipad) $user_agent]
                            } else {
                                set ad_conn(mobile_p) 0
                            }
                            return $ad_conn(mobile_p)
                        }

                        bot_p {
                            #
                            # Check, if we are used from a bot
                            # (heuristic based on user_agent).
                            #
                            if {[ns_conn isconnected]} {
                                if {[::acs::icanuse "ns_conn pool"] && [ns_conn pool] eq "bots"} {
                                    set ad_conn(bot_p) 1
                                } else {
                                    set user_agent [string tolower [ns_set iget [ns_conn headers] User-Agent]]
                                    set ad_conn(bot_p) [regexp (crawl|bot) $user_agent]
                                }
                            } else {
                                set ad_conn(bot_p) 0
                            }
                            return $ad_conn(bot_p)
                        }

                        ajax_p {
                            #
                            # Check, if we are used from an ajax
                            # client (providing the header field
                            # "X-Requested-With: XMLHttpRequest")
                            #
                            set ad_conn(ajax_p) 0
                            if {[ns_conn isconnected]} {
                                set headers [ns_conn headers]
                                set i [ns_set ifind $headers "X-Requested-With"]
                                if {$i > -1 } {
                                    set ad_conn(ajax_p) [expr {[ns_set value $headers $i] eq "XMLHttpRequest"}]
                                }
                            }
                            return $ad_conn(ajax_p)
                        }

                        behind_proxy_p {
                            #
                            # Check, if we are running behind a proxy:
                            # a) the parameter "ReverseProxyMode" has to be set
                            # b) the header-field X-Forwarded-For must be present
                            #
                            set ad_conn(behind_proxy_p) 0
                            if {[ns_conn isconnected]} {
                                if { [ns_config "ns/parameters" ReverseProxyMode false]
                                     && [ns_set ifind [ns_conn headers] X-Forwarded-For] > -1} {
                                    set ad_conn(behind_proxy_p) 1
                                }
                            }
                            return $ad_conn(behind_proxy_p)
                        }

                        behind_secure_proxy_p {
                            #
                            # Check, if we are running behind a secure proxy:
                            # a) [ad_conn behind_proxy_p] must be true
                            # b) the header-field X-SSL-Request must be 1
                            #
                            set ad_conn(behind_secure_proxy_p) 0
                            if {[ad_conn behind_proxy_p]} {
                                set ad_conn(behind_secure_proxy_p)  [expr {[ns_set iget [ns_conn headers] X-SSL-Request] == 1}]
                            }
                            return $ad_conn(behind_secure_proxy_p)
                        }

                        default {
                            return [ns_conn $var]
                        }
                    }
                }
            }
        }

        default {
            error "ad_conn: unknown flag $flag"
        }
    }
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: