File: redirect.tcl

package info (click to toggle)
libapache2-mod-rivet 3.2.0-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 5,868 kB
  • sloc: xml: 8,496; tcl: 7,212; ansic: 6,959; sh: 5,030; makefile: 261; sql: 91; lisp: 78
file content (63 lines) | stat: -rw-r--r-- 1,706 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#
# -- ::rivet::redirect
#
#  Redirecting to a new URL by issuing a 301 or 302 (permanent)
# diversion to a new resource. 
#
# Arguments:
#
#   - url               - URL to which we are redirecting the client
#   - permanent:[0 | 1] - whether redirection will be permanent (default: 0)
#     or
#   - permanent: code   - returns any HTTP integer code. In this context
#                         only the 3xx status codes are meaningful
#
#   $ Id: $
#

namespace eval ::rivet {

    proc redirect {url {permanent 0}} {

        if {[::rivet::headers sent]} {

            return  -code error \
                    -errorcode headers_already_sent \
                    -errorinfo "Impossible to redirect: headers already sent"

        }

        # In order to preserve compatibility 
        # with the past we chec whether we are
        # dealing with a boolean argument and handle
        # it accordingly 

        if {[string is boolean $permanent] } {

            if {[string is true $permanent]} {
                set http_code 301
            } else {
                set http_code 302
            } 

        } elseif {[string is integer $permanent] && ($permanent > 0)} {

            set http_code $permanent

        } else {

            return  -code error \
                    -errorcode invalid_http_code \
                    -errorinfo "Invalid HTTP status code: $permanent"

        }

        ::rivet::no_body ; ## don’t output anything on a redirect
        ::rivet::headers set Location $url
        ::rivet::headers numeric $http_code
        ::rivet::abort_page [dict create error_code redirect location $url] ; ## stop any further processing

        return -error ok 
    }

}