Tcl Library Source Code

Changes On Branch rest-817f51002f
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Changes In Branch rest-817f51002f Excluding Merge-Ins

This is equivalent to a diff from 68ddfcef98 to e10a3b6c0f

2015-11-17
02:24
rest - Tkt [895baa109a], [817f51002f] - Merged fixes on branch "rest-817f51002f". Version bumped to 1.0.2 check-in: b173bb67cb user: aku tags: trunk
2015-10-24
20:30
Replaced simple var check with whitelist of allowed commands. Closed-Leaf check-in: e10a3b6c0f user: aku tags: rest-817f51002f
2015-10-22
14:28
rest - Ticket [895baa109a] - Merge config content-type into request headers. check-in: 39ada3aa6e user: aku tags: rest-817f51002f
2015-10-20
22:05
csv - Ticket [06bf459d36] - Fixed bad RE translation of separator and delimiter characters. check-in: 79c8ed1a87 user: aku tags: trunk
15:38
Attempt at a more robust way of getting the invoker-name for rest::simple check-in: 8d718b4d0d user: aku tags: rest-817f51002f
02:27
Release math::exact 1.0 check-in: 68ddfcef98 user: kennykb tags: trunk
02:26
Manual page for math::exact Closed-Leaf check-in: 4ff50ad527 user: kennykb tags: kbk-math-exact
2015-10-19
16:32
rest documentation - Ticket [2a45e56f92] - Extended to mention TLS integration. check-in: 04e098e60c user: aku tags: trunk

Changes to modules/rest/rest.tcl.

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
64
65
66
67



68
69
70
71
72
73
74
        set opts [parse_opts {} {} {headers: cookie: auth: format: method:} [join $args]]
        set config [lindex $opts 0]
        set body [lindex $opts 1]
    } else {
        set body [lindex $args 1]
    }

    # make sure we know which method to use
    if {![dict exists $config method]} {
        # set the method using the name we were invoked with (through interp alias)
        dict set config method [namespace tail [lindex [dict get [info frame -1] cmd] 0]]
        if {[dict get $config method] == "simple"} { dict set config method get }
    }

    if {[string first " " $query] > 0} {
        # if query has a space assume it is a list of key value pairs, and do the formatting
        set query [eval ::http::formatQuery $query]
    } elseif {[string first ? $url] > 0 && $query == ""} {
        # if the url contains a query string and query empty then split it to the correct vars
        set query [join [lrange [split $url ?] 1 end] ?]
        set url [lindex [split $url ?] 0]
    }

    if {[dict exists $config auth]} {
        set auth [dict get $config auth]
        if {[lindex $auth 0] == "basic"} {
            lappend headers Authorization "Basic [base64::encode [lindex $auth 1]:[lindex $auth 2]]"
        }
    }



    if {[dict exists $config headers]} {
        dict for {key val} [dict get $config headers] { lappend headers $key $val }
    }
    if {[dict exists $config cookie]} {
        lappend headers Cookie [join [dict get $config cookie] \;]
    }








<
|
<
<
<
<
















>
>
>







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
64
65
66
67
68
69
70
71
72
        set opts [parse_opts {} {} {headers: cookie: auth: format: method:} [join $args]]
        set config [lindex $opts 0]
        set body [lindex $opts 1]
    } else {
        set body [lindex $args 1]
    }


    DetermineMethod config





    if {[string first " " $query] > 0} {
        # if query has a space assume it is a list of key value pairs, and do the formatting
        set query [eval ::http::formatQuery $query]
    } elseif {[string first ? $url] > 0 && $query == ""} {
        # if the url contains a query string and query empty then split it to the correct vars
        set query [join [lrange [split $url ?] 1 end] ?]
        set url [lindex [split $url ?] 0]
    }

    if {[dict exists $config auth]} {
        set auth [dict get $config auth]
        if {[lindex $auth 0] == "basic"} {
            lappend headers Authorization "Basic [base64::encode [lindex $auth 1]:[lindex $auth 2]]"
        }
    }
    if {[dict exists $config content-type]} {
        lappend headers Content-type [join [dict get $config content-type] \;]
    }
    if {[dict exists $config headers]} {
        dict for {key val} [dict get $config headers] { lappend headers $key $val }
    }
    if {[dict exists $config cookie]} {
        lappend headers Cookie [join [dict get $config cookie] \;]
    }

83
84
85
86
87
88
89



























90
91
92
93
94
95
96
}

interp alias {} ::rest::get    {} ::rest::simple
interp alias {} ::rest::post   {} ::rest::simple
interp alias {} ::rest::head   {} ::rest::simple
interp alias {} ::rest::put    {} ::rest::simple
interp alias {} ::rest::delete {} ::rest::simple




























# create_interface --
#
# use an array which defines a rest API to construct a set of procs
#
# ARGS:
#       name       name of the array containing command definitions







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
}

interp alias {} ::rest::get    {} ::rest::simple
interp alias {} ::rest::post   {} ::rest::simple
interp alias {} ::rest::head   {} ::rest::simple
interp alias {} ::rest::put    {} ::rest::simple
interp alias {} ::rest::delete {} ::rest::simple

proc ::rest::DetermineMethod {cv} {
    upvar 1 $cv config
    if {[dict exists $config method]} return

    set loc [info frame -2]
    if {![dict exists $loc cmd]} {
	return -code error "Unable to determine rest::simple method in the current context ([dict get $loc type]). Please specify it explicitly."
    }
    set cmd [dict get $loc cmd]
    if {[catch {
	set cmd [lindex $cmd 0]
    }]} {
	# Not a proper list. String processing.
	# Simple: Assume name without spaces.
	# TODO: Quoted literal.
	regexp {^([^ ]+).*$} $cmd -> cmd
    }
    if {$cmd ni {get delete head post put}} {
	return -code error "Unable to determine rest::simple method, found \"$cmd\". Please specify it explicitly."
    }
    set cmd [namespace tail $cmd]    
    if {$cmd eq "simple"} { set cmd get }
    #puts >>>|$cmd|
    dict set config method $cmd
    return
}

# create_interface --
#
# use an array which defines a rest API to construct a set of procs
#
# ARGS:
#       name       name of the array containing command definitions