snippets

Check-in [353d52e68a]
Login

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

Overview
Comment:http-pkgs
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 353d52e68a5c11a1d9704a08f9a40d3fd6b2d49646cc3c5123e4a97e70737888
User & Date: alex 2016-03-17 08:48:55
Context
2017-10-28
11:36
sunrise sunset check-in: 454c7baebb user: alex tags: trunk
2016-03-17
08:48
http-pkgs check-in: 353d52e68a user: alex tags: trunk
2014-01-17
19:38
tgetopt check-in: 6d239e1476 user: alex tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added http-pkgs/formdata/formdata.tcl.

































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#
# found at http://wiki.tcl.tk/13675
#
# 2005-02-27 Colin McCormack
# 2005-06-02 Benny Riefenstahl
# 2005-08-09 Erland Lewin
#
# $Id: formdata.tcl,v 1.1 2005/08/09 13:09:25 alex Exp alex $
#

namespace eval form-data {}

proc form-data::compose {partv {type multipart/form-data}} {
  upvar 1 $partv parts

  set mime [mime::initialize -canonical $type -parts $parts]
  set packaged [mime::buildmessage $mime]
  foreach part $parts {
    mime::finalize $part
  }
  mime::finalize $mime

  return $packaged
}

proc form-data::add_binary {partv name filename value type} {
  upvar 1 $partv parts
  set disposition "form-data; name=\"${name}\"; filename=\"$filename\""
  lappend parts [mime::initialize -canonical $type -string $value -encoding binary -header [list Content-Disposition $disposition]]
}

proc form-data::add_field {partv name value} {
  upvar 1 $partv parts
  set disposition "form-data; name=\"${name}\""
  lappend parts [mime::initialize -canonical text/plain -string $value -header [list Content-Disposition $disposition]]
}

proc form-data::format {name filename value type args} {
  set parts {}
  foreach {n v} $args {
    add_field parts $n $v
  }
  add_binary parts $name $filename $value $type
  return [compose parts]
}

proc form-data::post {url file content} {
  set field fileName
  set type {}
  set params {}
  set headers {}

  set message [eval [list form-data::format $field [file tail $file] $content $type] $params]

  set headerEnd [string first "\r\n\r\n" $message]
  incr headerEnd 1
  set bodystart [expr $headerEnd + 3]
  set headers_raw [string range $message 0 $headerEnd]
  set body [string range $message $bodystart end]
  set headers_raw [string map {"\r\n " " " "\r\n" "\n"} $headers_raw]
  regsub {  +} $headers_raw " " headers_raw

  foreach line [split $headers_raw "\n"] {
    regexp {^([^:]+): (.*)$} $line all label value
    lappend headers $label $value
  }

  array set ha $headers
  set content_type $ha(Content-Type)
  unset ha(Content-Type)
  set headers [array get ha]

  set token [http::geturl $url -type $content_type -binary true -headers $headers -query $body]
  http::wait $token

  return $token
}


package provide form-data 1.0

Added http-pkgs/formdata/pkgIndex.tcl.



>
1
package ifneeded form-data 1.0 [list source [file join $dir formdata.tcl]]

Added http-pkgs/httpauth/http-auth.tcl.











































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#
# Tcl Wrapper for "http::geturl" with automatic http authentication
#
# RFC 2617 HTTP Authentication: Basic and Digest Access Authentication
#
# MIT License (MIT)
# Copyright (c) 2016, Alexander Schoepe, Bochum, DE
# $Id: http-auth.tcl,v 1.1 2016/03/17 08:48:55 alex Exp alex $
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# 

#
# USAGE:
# 
# package require http::auth
# 
# # if using a realm-based authentication you need to write a callback.
# # see standard the ::http::auth::callback in this source.
# # if not, set username and password as follows:
#
# ::http::auth::config username USER
# ::http::auth::config password PASS
#
# # then call geturl
#
# ::http::geturl url ?options?
#

if {[package provide Tcl] < 8.6} {
  package require base64
}
package require http
package require md5


namespace eval ::http::auth {
  variable options
  variable noncecount

  dict set options username anonymous
  dict set options password {}
}


if {[info commands ::http::geturl_original] == {} && [lsearch -exact [interp aliases] {::http::auth::geturl}] == -1} {
  rename ::http::geturl ::http::geturl_original
  interp alias {} ::http::geturl {} ::http::auth::geturl
}


proc ::http::auth::config { name value } {
  variable options

  dict set options $name $value
}


proc ::http::auth::callback { realm *username *password } {
  variable options

  upvar ${*username} username
  upvar ${*password} password

  switch -- $realm {
    default {
      set username [dict get $options username]
      set password [dict get $options password]
    }
  }
}


proc ::http::auth::geturl { url args } {
  variable options
  variable noncecount

  set opts $args

  set method no-auth
  set token {}
  set executed {}
  set authorization {}
  set headerlist {}

  if {[set idx [lsearch -exact $opts {-headers}]] > -1} {
    foreach {name value} [lindex $opts ${idx}+1] {
      if {$name == {Authorization}} {
	set method [string tolower [lindex $value 0]]
      } else {
        lappend headerlist $name $value
      }
    }
    set opts [lreplace $opts $idx ${idx}+1]
  }
  dict set executed $method true
  if {[set idx [lsearch -exact $opts {-keepalive}]] > -1} {
    set keepalive [lindex $opts ${idx}+1]
    set opts [lreplace $opts $idx ${idx}+1]
    set args [lreplace $opts $idx ${idx}+1]
  }

  if {![info exists keepalive]} {
    if {[dict exists $options keepalive]} {
      set keepalive [dict get $options keepalive]
    } else {
      set keepalive 0
    }
  }


  set cnt 0
  while { 1 } {
    if {$token != {}} {
      http::cleanup $token
    }
    if {$cnt == 0} {
      set token [::http::geturl_original $url {*}$args -keepalive $keepalive]
    } else {
      set headers $headerlist
      lappend headers Authorization $authorization
      dict set executed [string tolower [lindex $authorization 0]] true
      set token [::http::geturl_original $url {*}$opts -keepalive $keepalive -headers $headers]
    }

    if {[http::ncode $token] == 401} {
      upvar $token state
      if {$state(querylength) < 1} {
        set opt(method) GET
      } else {
        set opt(method) POST
      }
      set opt(uri) $state(url)

      foreach {name value} $state(meta) {
	if {$name == {WWW-Authenticate}} {
	  set opt(realm) {}
	  set opt(username) {}
	  set opt(password) {}

	  if {[regexp {(\w+)\s(.*)} $value all method challange]} {
	    foreach item [split $challange {,}] {
	      if {[regexp {([^=]+)=(.*)} [string trim $item] all name value]} {
		set opt($name) [string trim $value {"}]
	      }
	    }
	  }
	  set method [string tolower $method]

	  callback $opt(realm) opt(username) opt(password)

	  switch -nocase -- $method {
	    Basic {
	      if {[package provide Tcl] < 8.6} {
		set authorization [list Basic [base64::encode $opt(username):$opt(password)]]
	      } else {
		set authorization [list Basic [binary encode base64 $opt(username):$opt(password)]]
	      }
	      if {[dict exists $executed $method]} {
		continue
	      } else {
	        break
	      }
	    }
	    Digest {
	      if {[info exists opt(nonce)] && [info exists opt(realm)] && [info exists opt(method)] && [info exists opt(uri)] &&
	          [info exists opt(algorithm)] && [string match "MD5*" $opt(algorithm)]} {

		set A1 [string tolower [md5::md5 -hex $opt(username):$opt(realm):$opt(password)]]
		set A2 [string tolower [md5::md5 -hex $opt(method):$opt(uri)]]

		if {![info exists noncecount($opt(nonce))]} {
		   set noncecount($opt(nonce)) 0
		}
		set opt(nc) [format %08d [incr noncecount($opt(nonce))]]
		set opt(cnonce) [format %08x%08x [clock clicks] [clock seconds]]

		switch -exact -- $opt(qop) {
		  {} { set opt(response) [string tolower [md5::md5 -hex $A1:$opt(nonce):$A2]] }
		  auth { set opt(response) [string tolower [md5::md5 -hex $A1:$opt(nonce):$opt(nc):$opt(cnonce):$opt(qop):$A2]] }
		  default { set opt(response) {} }
		}

		set list {}
		foreach item {username realm nonce uri response qop nc cnonce algorithm stale opaque} {
		  if {[info exists opt($item)] && [string trim $opt($item)] != {}} {
		    lappend list $item=\"$opt($item)\"
		  }
		}
		set authorization "Digest [join $list {, }]"
		if {[dict exists $executed $method]} {
		  continue
		} else {
		  break
		}
	      }
	    }
	  }
	}
      }
      if {[dict exists $executed $method]} {
	break
      }
    } else {
      break
    }
    incr cnt
  }
  return $token
}


package provide http::auth 1.0

Added http-pkgs/httpauth/pkgIndex.tcl.







>
>
>
1
2
3

package ifneeded http::auth 1.0 [list source [file join $dir http-auth.tcl]]