Subversion Repositories Koakuma

Rev

Rev 4 | Rev 8 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4 Rev 5
Line 1... Line 1...
1
# $Id: rpc.tcl 4 2024-10-01 15:42:33Z nishi $
1
# $Id: rpc.tcl 5 2024-10-01 18:07:27Z nishi $
2
package require http
2
package require http
3
package require base64
3
package require base64
4
package require term::ansi::ctrl::unix
4
package require term::ansi::ctrl::unix
5
 
5
 
6
namespace eval rpc {
6
namespace eval rpc {
Line 18... Line 18...
18
		}
18
		}
19
	}
19
	}
20
	set username ""
20
	set username ""
21
	set password ""
21
	set password ""
22
	proc ask-auth {} {
22
	proc ask-auth {} {
23
		upvar 1 username username
23
		global username
24
		upvar 1 password password
24
		global password
25
		puts -nonewline "Username: "
25
		puts -nonewline "Username: "
26
		flush stdout
26
		flush stdout
27
		set username "[gets stdin]"
27
		set ::rpc::username "[gets stdin]"
28
		puts -nonewline "Password: "
28
		puts -nonewline "Password: "
29
		flush stdout
29
		flush stdout
30
		exec stty -echo
30
		exec stty -echo
31
		set password "[gets stdin]"
31
		set ::rpc::password "[gets stdin]"
32
		exec stty echo
32
		exec stty echo
33
		puts ""
33
		puts ""
34
 
34
 
35
		set headers ""
35
		set headers ""
36
		lappend headers "Authorization"
36
		lappend headers "Authorization"
37
		lappend headers "Basic [::base64::encode -wrapchar "" "$username:$password"]"
37
		lappend headers "Basic [::base64::encode -wrapchar "" "$::rpc::username:$::rpc::password"]"
38
 
38
 
39
		global RPC_URL
39
		global RPC_URL
40
		set tok [::http::geturl "$RPC_URL" -headers $headers]
40
		set tok [::http::geturl "$RPC_URL" -headers $headers]
41
		set code [::http::ncode $tok]
41
		set code [::http::ncode $tok]
42
		::http::cleanup $tok
42
		::http::cleanup $tok
Line 44... Line 44...
44
		if { $code == 200 } {
44
		if { $code == 200 } {
45
			return 1
45
			return 1
46
		}
46
		}
47
		return 0
47
		return 0
48
	}
48
	}
-
 
49
	proc send {path content} {
-
 
50
		set headers ""
-
 
51
		global RPC_URL
-
 
52
		if { "$::rpc::username" != "" } {
-
 
53
			lappend headers "Authorization"
-
 
54
			lappend headers "Basic [::base64::encode -wrapchar "" "$::rpc::username:$::rpc::password"]"
-
 
55
		}
-
 
56
		set tok [::http::geturl "$RPC_URL$path" -headers $headers -type "application/json" -query "$content"]
-
 
57
		set code [::http::ncode $tok]
-
 
58
		set body "[::http::data $tok]"
-
 
59
		::http::cleanup $tok
-
 
60
		lappend result "$code"
-
 
61
		lappend result "$body"
-
 
62
		return $result
-
 
63
	}
49
}
64
}