|
1
|
# |
|
2
|
# Carry one round trip of a sync for scenario.test, and lose or damage |
|
3
|
# it if told to. Fossil runs this script as its --transport-command: |
|
4
|
# |
|
5
|
# tclsh scenario-wire.tcl URL REQUESTFILE REPLYFILE |
|
6
|
# |
|
7
|
# SCN_EXE is the fossil executable and SCN_REPO the repository to talk |
|
8
|
# to. SCN_FAULT is 1 to lose the request, 2 to lose the reply after the |
|
9
|
# other side has acted on the request, and 3 to deliver only the first |
|
10
|
# half of the reply. Any other value means no fault. |
|
11
|
# |
|
12
|
# The other side trusts the request as a local one, unless SCN_AUTH is |
|
13
|
# set: then it checks the login card as a server on a network does. |
|
14
|
# Fossil puts no login card on a request that it gives to a transport |
|
15
|
# command. If SCN_LOGIN is set, to USER:PASSWORD, this script adds the |
|
16
|
# card that Fossil would add on a network; SCN_PROJECT is then the |
|
17
|
# project code of the repository. |
|
18
|
# |
|
19
|
proc slurp {name} { |
|
20
|
set fd [open $name rb] |
|
21
|
set data [read $fd] |
|
22
|
close $fd |
|
23
|
return $data |
|
24
|
} |
|
25
|
proc spit {name data} { |
|
26
|
set fd [open $name wb] |
|
27
|
puts -nonewline $fd $data |
|
28
|
close $fd |
|
29
|
} |
|
30
|
|
|
31
|
lassign $argv url requestFile replyFile |
|
32
|
set fault $env(SCN_FAULT) |
|
33
|
if {$fault == 1} { |
|
34
|
spit $replyFile "" |
|
35
|
exit 0 |
|
36
|
} |
|
37
|
|
|
38
|
# The SHA1 hash of a string, by way of "fossil sha1sum". |
|
39
|
proc sha1 {text} { |
|
40
|
global env requestFile |
|
41
|
spit $requestFile.sha $text |
|
42
|
set sum [lindex [exec $env(SCN_EXE) sha1sum $requestFile.sha] 0] |
|
43
|
file delete $requestFile.sha |
|
44
|
return $sum |
|
45
|
} |
|
46
|
|
|
47
|
# Hand the request to "fossil http" on the other repository. |
|
48
|
set request [slurp $requestFile] |
|
49
|
if {[info exists env(SCN_LOGIN)]} { |
|
50
|
set colon [string first : $env(SCN_LOGIN)] |
|
51
|
set user [string range $env(SCN_LOGIN) 0 [expr {$colon-1}]] |
|
52
|
set password [string range $env(SCN_LOGIN) [expr {$colon+1}] end] |
|
53
|
set secret [sha1 $env(SCN_PROJECT)/$user/$password] |
|
54
|
set nonce [sha1 $request] |
|
55
|
set request "login $user $nonce [sha1 $nonce$secret]\n$request" |
|
56
|
} |
|
57
|
set head "POST /xfer HTTP/1.0\r\n" |
|
58
|
append head "Content-Type: application/x-fossil-debug\r\n" |
|
59
|
append head "Content-Length: [string length $request]\r\n\r\n" |
|
60
|
spit $requestFile.req $head$request |
|
61
|
set trust [expr {[info exists env(SCN_AUTH)] ? {} : {--localauth}}] |
|
62
|
set failed [catch { |
|
63
|
exec $env(SCN_EXE) http --in $requestFile.req --out $requestFile.raw \ |
|
64
|
--ipaddr 127.0.0.1 $env(SCN_REPO) {*}$trust 2>@1 |
|
65
|
}] |
|
66
|
if {$failed} { |
|
67
|
file delete $requestFile.req $requestFile.raw |
|
68
|
exit 1 |
|
69
|
} |
|
70
|
|
|
71
|
# The reply is what follows the first empty line. |
|
72
|
set raw [slurp $requestFile.raw] |
|
73
|
file delete $requestFile.req $requestFile.raw |
|
74
|
set end [string first "\r\n\r\n" $raw] |
|
75
|
set reply [expr {$end < 0 ? "" : [string range $raw [expr {$end+4}] end]}] |
|
76
|
switch -- $fault { |
|
77
|
2 {set reply ""} |
|
78
|
3 {set reply [string range $reply 0 [expr {[string length $reply]/2 - 1}]]} |
|
79
|
} |
|
80
|
spit $replyFile $reply |
|
81
|
|