Fossil SCM

fossil-scm / test / url.test
Blame History Raw 1739 lines
1
#
2
# Copyright (c) 2026 Dan Mestas
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the Simplified BSD License (also
6
# known as the "2-Clause License" or "FreeBSD License".)
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but without any warranty; without even the implied warranty of
10
# merchantability or fitness for a particular purpose.
11
#
12
############################################################################
13
#
14
# URLs of remote repositories, and the HTTP exchange of the sync client.
15
#
16
# The first part gives URLs to "fossil test-urlparser" and
17
# "fossil test-url-basename" and checks each part of the result, and
18
# checks how a saved remote, the parent project and a proxy are read.
19
#
20
# The second part gives "fossil test-httpmsg", "fossil clone" and
21
# "fossil pull" a server made by this file, which answers each
22
# connection with a reply that the test wrote. A well-formed reply must
23
# be accepted. A malformed one must be refused with a message and exit
24
# status 1.
25
#
26
# The third part does the same over the ssh: transport. Nothing here
27
# connects to another machine: "ssh" is a script written by this file,
28
# which writes down its arguments and prints a reply that the test wrote.
29
#
30
# http.test tests whole clones over HTTP and HTTPS. ssh.test tests what
31
# goes on an ssh command line.
32
#
33
34
# The first sections run at every level, and the rest need
35
# -assurance stress.
36
require_no_open_checkout
37
38
if {![loopback_available]} {
39
test_skip "the tests need a TCP connection over the loopback interface"
40
}
41
42
test_setup
43
set urlRepo [file join [pwd] .rep.fossil]
44
set urlDir [pwd]
45
46
###############################################################################
47
# The parts of a URL.
48
49
# Runs "fossil test-urlparser URL" with the options in ARGS and returns
50
# a dictionary of what it printed for the URL as given: the name after
51
# "g.url." and the value. url_full(g.url) is under the key "full".
52
#
53
proc url_parse {url args} {
54
fossil_raw test-urlparser $url --proxy off {*}$args
55
set fields [dict create]
56
foreach urlLine [split $::RESULT \n] {
57
if {[string match "\\*\\*\\*\\**" $urlLine]} break
58
if {[regexp {^g\.url\.(\w+) *= (.*)$} $urlLine -> key value]} {
59
dict set fields $key $value
60
} elseif {[regexp {^url_full\(g\.url\) = (.*)$} $urlLine -> value]} {
61
dict set fields full $value
62
}
63
}
64
return $fields
65
}
66
67
# Test NAME passes when "fossil test-urlparser URL" with the options
68
# OPTIONS gives each part in EXPECTED the value that follows it there.
69
#
70
proc url_parse_test {name url options expected {constraints ""}} {
71
set fields [url_parse $url {*}$options]
72
set actual [list]
73
foreach {urlKey urlValue} $expected {
74
if {[dict exists $fields $urlKey]} {
75
lappend actual $urlKey [dict get $fields $urlKey]
76
} else {
77
lappend actual $urlKey (missing)
78
}
79
}
80
test_eq $name $actual [list {*}$expected] $constraints
81
}
82
83
foreach {urlName urlText urlExpected} {
84
http-plain http://example.com
85
{isFile 0 isHttps 0 isSsh 0 protocol http name example.com port 80
86
dfltPort 80 hostname example.com path {} user {} passwd {}
87
canonical http://example.com fossil {} flags 0x0000
88
full http://example.com}
89
https-upper-case HTTPS://Example.COM:8443/a/b
90
{isHttps 1 protocol https name example.com port 8443 dfltPort 443
91
hostname example.com:8443 path /a/b
92
canonical https://example.com:8443/a/b}
93
https-default-port-given https://h:443/x
94
{port 443 hostname h:443 canonical https://h/x}
95
http-port-zero http://h:0/x
96
{port 0 hostname h:0 canonical http://h:0/x}
97
http-port-largest http://h:65535
98
{port 65535 hostname h:65535 path {} canonical http://h:65535}
99
http-port-then-query http://h:81?x=1
100
{port 81 path {} canonical http://h:81}
101
http-port-then-fragment http://h:81#top
102
{port 81 path {} canonical http://h:81}
103
http-port-empty http://h:/x
104
{port 0 hostname h:0 path /x}
105
http-user-password http://u:p@h/x
106
{name h user u passwd p canonical http://u@h/x full http://u:p@h/x}
107
http-user-only http://u@h:81/x
108
{user u passwd {} canonical http://u@h:81/x full http://u@h:81/x}
109
http-user-empty-password http://u:@h/x
110
{user u passwd {} canonical http://u@h/x full http://u@h/x}
111
http-user-escapes http://a%40b:p%3Aq%2Fr@h/
112
{user a@b passwd p:q/r canonical http://a%40b@h/
113
full http://a%40b:p%3Aq%2Fr@h/}
114
http-user-bad-escape http://%@h/
115
{user % canonical http://%25@h/}
116
http-password-colons http://u:p:q@h/
117
{user u passwd p:q}
118
http-at-in-path http://h/a@b
119
{name h user {} path /a@b canonical http://h/a%40b}
120
http-at-in-query http://h?x=a@b
121
{name h user {} path {} canonical http://h}
122
http-at-in-fragment http://h#a@b
123
{name h user {} path {} canonical http://h}
124
http-fragment-dropped http://h/a/b#frag
125
{path /a/b canonical http://h/a/b}
126
http-query-dropped http://h/a?x=1&y=2
127
{path /a canonical http://h/a fossil {}}
128
http-query-no-value http://h/a?x&y
129
{path /a canonical http://h/a fossil {}}
130
http-path-escapes {http://h/a%20b/c%2Bd}
131
{path {/a b/c+d} canonical http://h/a+b/c%2Bd}
132
http-path-bad-escape http://h/a%zz
133
{path /a%zz canonical http://h/a%25zz}
134
http-host-escape http://loc%61lhost/x
135
{name localhost}
136
http-host-upper-case http://LOCALHOST/X
137
{name localhost path /X}
138
http-ipv6 {http://[::1]/x}
139
{name ::1 port 80 hostname {[::1]} canonical {http://[::1]/x}}
140
http-ipv6-port {http://[2001:DB8::1]:8080/x}
141
{name 2001:db8::1 port 8080 hostname {[2001:db8::1]:8080}
142
canonical {http://[2001:db8::1]:8080/x}}
143
http-ipv6-user {http://u:p@[::1]:81/x}
144
{name ::1 user u full {http://u:p@[::1]:81/x}}
145
http-brackets-one-letter {http://[a]/x}
146
{name a hostname a}
147
http-bracket-not-closed {http://[ab/x}
148
{name {[ab} path /x}
149
http-user-empty http://@h/x
150
{user {} name h canonical http://@h/x full http://@h/x}
151
http-host-one-letter http://h
152
{name h hostname h}
153
ssh-plain ssh://h/dir/r.fossil
154
{isSsh 1 protocol ssh name h port 22 dfltPort 22 hostname h
155
path dir/r.fossil fossil fossil canonical ssh://h/dir/r.fossil
156
flags 0x0000}
157
ssh-upper-case-user-port SSH://U@H:2222/dir/r.fossil
158
{isSsh 1 name h user U port 2222 hostname h:2222 path dir/r.fossil
159
canonical ssh://U@h:2222/dir/r.fossil}
160
ssh-absolute-path ssh://h//abs/r.fossil
161
{path /abs/r.fossil canonical ssh://h//abs/r.fossil}
162
ssh-root-path ssh://h/
163
{path / canonical ssh://h/}
164
ssh-fossil-parameter ssh://h/r.fossil?fossil=/opt/bin/fossil
165
{path r.fossil fossil /opt/bin/fossil
166
canonical ssh://h/r.fossil?fossil=/opt/bin/fossil flags 0x0400}
167
ssh-fossil-parameter-escaped ssh://h/r.fossil?fossil=/opt/my%20bin/fossil
168
{fossil {/opt/my bin/fossil}
169
canonical ssh://h/r.fossil?fossil=/opt/my+bin/fossil}
170
ssh-fossil-parameter-among-others ssh://h/r.fossil?a=b&fossil=/x/fossil&c
171
{path r.fossil fossil /x/fossil
172
canonical ssh://h/r.fossil?fossil=/x/fossil}
173
ssh-fossil-parameter-empty ssh://h/r.fossil?fossil=
174
{fossil {} flags 0x0400}
175
ssh-ipv6 {ssh://[::1]/r.fossil}
176
{name ::1 hostname ::1 canonical {ssh://[::1]/r.fossil}}
177
ssh-ipv6-port {ssh://[::1]:2222/r.fossil}
178
{name ::1 port 2222 hostname ::1:2222
179
canonical {ssh://[::1]:2222/r.fossil}}
180
ssh-user-password ssh://u:p@h/r.fossil
181
{user u passwd p canonical ssh://u@h/r.fossil}
182
file-three-slashes file:///nosuchdir/r.fossil
183
{isFile 1 protocol file name /nosuchdir/r.fossil path {}
184
canonical file:///nosuchdir/r.fossil full file:///nosuchdir/r.fossil}
185
file-one-slash file:/nosuchdir/r.fossil
186
{isFile 1 name /nosuchdir/r.fossil}
187
file-upper-case FILE:///nosuchdir/r.fossil
188
{isFile 1 name /nosuchdir/r.fossil}
189
file-escaped-space file:///nosuchdir/my%20r.fossil
190
{isFile 1 name {/nosuchdir/my r.fossil}
191
canonical file:///nosuchdir/my+r.fossil}
192
} {
193
url_parse_test url-parse-$urlName $urlText {} $urlExpected
194
}
195
196
# A file: URL with a relative name, the name of a repository file, and
197
# the name of a directory that holds a file named FOSSIL, are all the
198
# absolute name of the file.
199
#
200
file mkdir holder emptydir
201
file copy $urlRepo holder/FOSSIL
202
url_parse_test url-parse-file-relative file:.rep.fossil {} \
203
[list isFile 1 name $urlRepo canonical file://$urlRepo]
204
url_parse_test url-parse-file-name .rep.fossil {} \
205
[list isFile 1 protocol file name $urlRepo]
206
url_parse_test url-parse-directory holder {} \
207
[list isFile 1 name [file join $urlDir holder FOSSIL]]
208
209
# What is not a URL, a file or a directory with a repository is refused,
210
# and so is a port that is too large or is followed by other text.
211
#
212
foreach {urlName urlText urlMessage} {
213
no-scheme nosuchfile {unknown repository: nosuchfile}
214
empty {} {unknown repository: }
215
other-scheme ftp://h/x {unknown repository: ftp://h/x}
216
scheme-no-slashes http:h/x {unknown repository: http:h/x}
217
directory-empty emptydir {unknown repository: emptydir}
218
port-too-large http://h:65536/x {port number too large in url}
219
port-huge http://h:99999999999999999999/x
220
{port number too large in url}
221
port-negative http://h:-1/x {url missing '/' after port number}
222
port-letters http://h:80x/ {url missing '/' after port number}
223
port-ipv6-junk {http://[::1]:8x} {url missing '/' after port number}
224
} {
225
fossil_raw test-urlparser $urlText --proxy off
226
test url-parse-refused-$urlName \
227
{$CODE == 1 && [string first $urlMessage $RESULT] >= 0}
228
}
229
230
# An ssh: URL with a host and no path has an empty path, and fossil
231
# prints nothing but the parts of the URL.
232
#
233
foreach {urlName urlText urlExpected} {
234
host ssh://host
235
{isSsh 1 name host port 22 hostname host user {} path {}
236
canonical ssh://host}
237
user-host ssh://u@h
238
{isSsh 1 name h port 22 user u path {} canonical ssh://u@h}
239
host-port ssh://h:22
240
{isSsh 1 name h port 22 hostname h:22 path {} canonical ssh://h}
241
user-host-port ssh://u@h:2222
242
{isSsh 1 name h port 2222 path {} canonical ssh://u@h:2222}
243
scheme-only ssh://
244
{isSsh 1 name {} path {} canonical ssh://}
245
} {
246
url_parse_test url-parse-ssh-no-path-$urlName $urlText {} $urlExpected
247
test url-parse-ssh-no-path-$urlName-output {$CODE == 0 &&
248
[regexp -all -line {^(g\.url\.\w+ +|url_full\(g\.url\) )= } $RESULT] ==
249
[llength [split [string trim $RESULT] \n]]}
250
}
251
252
# The complete URL of an ssh: remote has the "/" between the host and
253
# the path.
254
#
255
url_parse_test url-parse-ssh-full ssh://u@h/dir/r.fossil {} \
256
{full ssh://u@h/dir/r.fossil}
257
258
# A second fossil= parameter is refused.
259
#
260
fossil_raw test-urlparser ssh://h/r.fossil?fossil=/a/fossil&fossil=/b/fossil \
261
--proxy off
262
test url-parse-ssh-fossil-twice {$CODE == 1 &&
263
[string match "*more than one fossil= query parameter*" $RESULT]}
264
265
# A very long user name, password, host and path. The four together stay
266
# under the 128 KB that Linux allows for one argument.
267
#
268
set urlLong [string repeat a 30000]
269
url_parse_test url-parse-long http://$urlLong:$urlLong@$urlLong/$urlLong {} \
270
[list user $urlLong passwd $urlLong name $urlLong path /$urlLong]
271
272
# test-urlparser takes one or two arguments.
273
#
274
fossil_raw test-urlparser
275
test url-parse-usage-none {$CODE == 1 && [string match "*Usage:*URL*" $RESULT]}
276
fossil_raw test-urlparser http://h/ extra more
277
test url-parse-usage-many {$CODE == 1 && [string match "*Usage:*URL*" $RESULT]}
278
url_parse_test url-parse-two-arguments http://h/x extra {name h path /x}
279
280
###############################################################################
281
# Flags of the parser: --remember, --prompt-pw. Standard input is not a
282
# terminal here, so fossil cannot ask for a password.
283
284
url_parse_test url-flag-remember http://h/x --remember {flags 0x0002}
285
url_parse_test url-flag-remember-password http://u:p@h/x --remember \
286
{user u passwd p flags 0x0006}
287
url_parse_test url-flag-remember-password-ssh ssh://u:p@h/x --remember \
288
{user u passwd p flags 0x0002}
289
url_parse_test url-flag-prompt-has-password http://u:p@h/x --prompt-pw \
290
{user u passwd p flags 0x0001}
291
url_parse_test url-flag-prompt-no-user http://h/x --prompt-pw \
292
{user {} flags 0x0001}
293
url_parse_test url-flag-prompt-ssh ssh://u@h/x --prompt-pw \
294
{user u passwd {} flags 0x0001}
295
url_parse_test url-flag-prompt-file file:///nosuchdir/x --prompt-pw \
296
{isFile 1 flags 0x0001}
297
fossil_raw test-urlparser http://u@h/x --proxy off --prompt-pw
298
test url-flag-prompt-no-terminal {$CODE == 1 && [string first \
299
{missing or incorrect password for user "u"} $RESULT] >= 0}
300
301
###############################################################################
302
# Saved remotes. --use-config reads the URL "default" from the
303
# last-sync-url setting and any other name from the remote of that name,
304
# with the saved password. --use-parent reads the parent project.
305
306
proc url_config {name value} {
307
fossil sql "REPLACE INTO config(name,value,mtime) VALUES('$name',$value,0)"
308
}
309
310
url_parse_test url-config-default-unset default --use-config \
311
{protocol {} name {} canonical {} isFile 0}
312
url_parse_test url-config-parent-unset default --use-parent \
313
{protocol {} name {} canonical {}}
314
url_config last-sync-url 'http://u@h/last'
315
url_config last-sync-pw obscure('lastpw')
316
url_parse_test url-config-default default --use-config \
317
{name h user u passwd ************ pwConfig last-sync-pw
318
canonical http://u@h/last flags 0x0040}
319
url_parse_test url-config-default-show default {--use-config --show-pw} \
320
{name h user u passwd lastpw pwConfig last-sync-pw}
321
url_parse_test url-config-parent-falls-back default --use-parent \
322
{user u passwd ************ pwConfig last-sync-pw
323
canonical http://u@h/last flags 0x00c0}
324
url_config parent-project-url 'http://pu@ph/parent'
325
url_config parent-project-pw obscure('parentpw')
326
url_parse_test url-config-parent default {--use-parent --show-pw} \
327
{name ph user pu passwd parentpw pwConfig parent-project-pw
328
canonical http://pu@ph/parent}
329
url_parse_test url-config-default-not-parent default {--use-config --show-pw} \
330
{name h passwd lastpw}
331
332
fossil remote add east http://eu:eastpw@eh:81/east
333
url_parse_test url-config-named east {--use-config --show-pw} \
334
{name eh port 81 user eu passwd eastpw pwConfig sync-pw:east
335
canonical http://eu@eh:81/east flags 0x0048}
336
url_parse_test url-config-named-hidden east --use-config \
337
{user eu passwd ************}
338
url_parse_test url-config-url-not-a-name http://x/y --use-config \
339
{name x path /y pwConfig {} flags 0x0040}
340
fossil_raw test-urlparser nosuchremote --proxy off --use-config
341
test url-config-unknown-name {$CODE == 1 &&
342
[string first "unknown repository: nosuchremote" $RESULT] >= 0}
343
url_parse_test url-config-name-without-flag east {} \
344
{protocol (missing)}
345
test url-config-name-without-flag-refused {$CODE == 1 &&
346
[string first "unknown repository: east" $RESULT] >= 0}
347
348
# "fossil remote" saves the URL without the password. It does not save
349
# the password when it cannot ask whether to.
350
#
351
fossil remote http://ru:remotepw@rh/remote
352
fossil remote
353
test_eq url-remember-url [string trim $RESULT] http://ru@rh/remote
354
url_parse_test url-remember-no-password default {--use-config --show-pw} \
355
{name rh user ru passwd {}}
356
357
# "fossil remote hyperlink" prints the URL of the remote without the user.
358
#
359
fossil_raw remote hyperlink
360
test_match url-nouser-hyperlink $RESULT {^http://rh/remote/info/[0-9a-f]+\s*$}
361
fossil remote http://ru:remotepw@rh:8081/remote
362
fossil_raw remote hyperlink
363
test_match url-nouser-hyperlink-port $RESULT \
364
{^http://rh:8081/remote/info/[0-9a-f]+\s*$}
365
fossil remote {http://ru:remotepw@[::1]/remote}
366
fossil_raw remote hyperlink
367
test_match url-nouser-hyperlink-ipv6 $RESULT \
368
{^http://\[::1\]/remote/info/[0-9a-f]+\s*$}
369
fossil remote http://rh/remote
370
fossil_raw remote hyperlink
371
test_match url-nouser-hyperlink-no-user $RESULT \
372
{^http://rh/remote/info/[0-9a-f]+\s*$}
373
url_config last-sync-url 'http://@rh/remote'
374
fossil_raw remote hyperlink
375
test_match url-nouser-hyperlink-empty-user $RESULT \
376
{^http://@rh/remote/info/[0-9a-f]+\s*$}
377
fossil remote off
378
379
# The SQL function url_nouser() gives a URL without its user and
380
# password, and NULL for NULL.
381
#
382
foreach {urlName urlSql urlExpected} {
383
http {url_nouser('http://u:p@h:81/x')} http://h:81/x
384
no-user {url_nouser('https://h/x')} https://h/x
385
ssh {url_nouser('ssh://u@h/dir/r.fossil')} ssh://h/dir/r.fossil
386
file {url_nouser('file:///nosuchdir/r')} file:///nosuchdir/r
387
null {coalesce(url_nouser(NULL),'null')} null
388
} {
389
fossil sql "SELECT $urlSql"
390
test_eq url-nouser-sql-$urlName [string trim $RESULT] $urlExpected
391
}
392
393
# The /urllist page links each saved remote to its URL without the user.
394
#
395
write_file urllist.txt "GET /urllist HTTP/1.0\r\n\r\n"
396
fossil_raw http --in urllist.txt --ipaddr 127.0.0.1 $urlRepo --localauth
397
test url-nouser-urllist {[string first "HTTP/1.0 200 OK" $RESULT] == 0 &&
398
[string first "<a href='http://eh:81/east'>http://eu@eh:81/east</a>" \
399
$RESULT] >= 0}
400
401
###############################################################################
402
# A proxy. test-urlparser prints the URL again as it is used through
403
# the proxy from --proxy, the "proxy" setting or the http_proxy
404
# environment variable, after a line of stars.
405
406
# Returns what test-urlparser printed after "Using proxy:", or "none" if
407
# it named no proxy.
408
proc url_proxy {url args} {
409
fossil_raw test-urlparser $url {*}$args
410
if {[regexp -line {^Using proxy: (.*)$} $::RESULT -> proxy]} {
411
return $proxy
412
}
413
return none
414
}
415
test_eq url-proxy-option [url_proxy http://h/x --proxy http://pu:pp@ph:3128] \
416
http://pu@ph:3128
417
test_eq url-proxy-off [url_proxy http://h/x --proxy off] none
418
test_eq url-proxy-empty [url_proxy http://h/x --proxy ""] none
419
test_eq url-proxy-none-set [url_proxy http://h/x] none
420
test_eq url-proxy-ssh-url [url_proxy ssh://h/x --proxy http://ph:3128] none
421
test_eq url-proxy-file-url \
422
[url_proxy file:///nosuchdir/x --proxy http://ph:3128] none
423
set env(http_proxy) http://envproxy:3128
424
test_eq url-proxy-environment [url_proxy http://h/x] http://envproxy:3128
425
test_eq url-proxy-option-over-environment \
426
[url_proxy http://h/x --proxy http://ph:3128] http://ph:3128
427
fossil settings proxy http://setproxy:3128
428
test_eq url-proxy-setting [url_proxy http://h/x] http://setproxy:3128
429
fossil settings proxy off
430
test_eq url-proxy-setting-off [url_proxy http://h/x] none
431
fossil unset proxy
432
unset env(http_proxy)
433
fossil_raw test-urlparser http://h/x --proxy nosuchproxy
434
test url-proxy-not-a-url {$CODE == 1 &&
435
[string first "unknown repository: nosuchproxy" $RESULT] >= 0}
436
437
# The other network options of a sync command are accepted.
438
#
439
foreach urlOption {--nosync --ipv4 --ipv6 --accept-any-cert} {
440
url_parse_test url-proxy-option$urlOption http://h/x $urlOption {name h}
441
}
442
443
if {[test_assurance] ne "stress"} {
444
test_skip "the tests after the first sections need -assurance stress"
445
}
446
447
###############################################################################
448
# The name for a clone of a URL.
449
450
foreach {urlName urlText urlExpected} {
451
path-tail http://h/a/b/repo repo
452
suffix-dropped http://h/a/repo.fossil repo
453
trailing-slash http://h/a/repo/ repo
454
query-dropped http://h/a/repo?x=/y repo
455
host-only http://example.com example
456
host-www http://www.example.com example
457
host-www-upper http://WWW.example.com example
458
host-www-suffix http://www.com www
459
host-port http://example.com:8080 example
460
host-www-port http://www.example.org:8080/ example
461
user-at-host ssh://u@host host
462
user-host-path ssh://u@host/dir/r.fossil r
463
no-separator nosuchthing {}
464
slash-only / {}
465
at-only @ {}
466
empty {} {}
467
dot-first http://h/.hidden {}
468
www-dot-only http://www. www
469
colon-in-tail host/a:b a
470
} {
471
fossil test-url-basename $urlText
472
test_eq url-basename-$urlName [string trim $RESULT] \
473
[string trim "$urlText -> $urlExpected"]
474
}
475
fossil test-url-basename http://h/one http://h/two
476
test_eq url-basename-two [string trim $RESULT] \
477
"http://h/one -> one\nhttp://h/two -> two"
478
fossil test-url-basename
479
test_eq url-basename-none $RESULT ""
480
481
###############################################################################
482
# The HTTP exchange.
483
484
# Listens on a port on 127.0.0.1 that the system chooses, and returns
485
# the port. Connection N is answered with item N of REPLIES, and the
486
# last item answers every later connection. "PORT" in a reply stands for
487
# the port. Each request is appended to the list urlRequests.
488
#
489
proc url_listen {replies} {
490
set ::urlRequests [list]
491
return [test_listen [list url_receive $replies]]
492
}
493
proc url_receive {replies chan request} {
494
set number [llength $::urlRequests]
495
lappend ::urlRequests $request
496
if {$number >= [llength $replies]} {set number end}
497
set port [lindex [fconfigure $chan -sockname] 2]
498
catch {
499
puts -nonewline $chan [string map [list PORT $port] \
500
[lindex $replies $number]]
501
}
502
}
503
504
# Returns TEXT with each line end changed to CR LF.
505
proc url_crlf {text} {
506
return [string map [list \n \r\n] $text]
507
}
508
509
# Returns the request line of request NUMBER of the last exchange.
510
proc url_request_line {number} {
511
return [lindex [split [lindex $::urlRequests $number] \r] 0]
512
}
513
514
# Returns the value of the header NAME in request NUMBER of the last
515
# exchange, or "(none)".
516
proc url_request_header {number name} {
517
if {[regexp -nocase -line "^$name: (\[^\r\]*)" \
518
[lindex $::urlRequests $number] -> value]} {
519
return $value
520
}
521
return (none)
522
}
523
524
# Returns the body of request NUMBER of the last exchange.
525
proc url_request_body {number} {
526
set request [lindex $::urlRequests $number]
527
return [string range $request [expr {[string first "\r\n\r\n" $request]+4}] \
528
end]
529
}
530
531
# Runs "fossil test-httpmsg" on the path /x of a server that answers
532
# with REPLIES, with the options and further arguments in ARGS.
533
#
534
proc url_httpmsg {replies args} {
535
set port [url_listen $replies]
536
fossil_raw -seconds 60 test-httpmsg http://127.0.0.1:$port/x {*}$args
537
test_listen_stop
538
}
539
540
# Replies that are accepted. Each row is a name, the reply header, the
541
# reply body and the body that test-httpmsg must print.
542
#
543
set urlBig [string repeat 0123456789 500]
544
foreach {urlName urlHeader urlBody urlExpected} [list \
545
content-length "HTTP/1.1 200 OK\nContent-Length: 5\n\n" hello hello \
546
content-length-zero "HTTP/1.1 200 OK\nContent-Length: 0\n\n" "" "" \
547
content-length-spaces "HTTP/1.1 200 OK\nContent-Length: 5 \n\n" hello \
548
hello \
549
content-length-twice-same \
550
"HTTP/1.1 200 OK\nContent-Length: 5\nContent-Length: 5\n\n" hello \
551
hello \
552
bytes-after-length "HTTP/1.1 200 OK\nContent-Length: 2\n\n" hello he \
553
no-length "HTTP/1.1 200 OK\n\n" hello hello \
554
no-length-large "HTTP/1.1 200 OK\n\n" $urlBig $urlBig \
555
no-length-empty "HTTP/1.1 200 OK\n\n" "" "" \
556
header-cut-short "HTTP/1.1 200 OK\nContent-Length: 0\n" "" "" \
557
status-line-only "HTTP/1.1 200" "" "" \
558
http-1.0 "HTTP/1.0 200 OK\nContent-Length: 5\n\n" hello hello \
559
header-lower-case "HTTP/1.1 200 ok\ncontent-length: 5\n\n" hello hello \
560
connection-close \
561
"HTTP/1.1 200 OK\nConnection: close\nContent-Length: 5\n\n" hello \
562
hello \
563
connection-keep-alive \
564
"HTTP/1.0 200 OK\nConnection: Keep-Alive\nContent-Length: 5\n\n" \
565
hello hello \
566
connection-other \
567
"HTTP/1.1 200 OK\nConnection: upgrade\nContent-Length: 5\n\n" hello \
568
hello \
569
location-with-200 \
570
"HTTP/1.1 200 OK\nLocation: http://127.0.0.1:1/\nContent-Length: 5\n\n" \
571
hello hello \
572
content-type-other \
573
"HTTP/1.1 200 OK\nContent-Type: text/html\nContent-Length: 5\n\n" \
574
hello hello \
575
content-type-debug \
576
[join [list \
577
"HTTP/1.1 200 OK\n" \
578
"Content-Type: application/x-fossil-debug\n" \
579
"Content-Length: 5\n\n"] ""] \
580
hello hello \
581
content-type-uncompressed \
582
[join [list \
583
"HTTP/1.1 200 OK\n" \
584
"Content-Type: application/x-fossil-uncompressed\n" \
585
"Content-Length: 5\n\n"] ""] \
586
hello hello \
587
interim-100 \
588
"HTTP/1.1 100 Continue\n\nHTTP/1.1 200 OK\nContent-Length: 5\n\n" \
589
hello hello \
590
interim-with-headers \
591
[join [list \
592
"HTTP/1.1 103 Early Hints\nLink: </x>\n\nHTTP/1.1 200 OK\n" \
593
"Content-Length: 5\n\n"] ""] \
594
hello hello \
595
interim-twice \
596
[join [list \
597
"HTTP/1.1 100 Continue\n\nHTTP/1.1 102 Processing\n\n" \
598
"HTTP/1.1 200 OK\nContent-Length: 5\n\n"] ""] \
599
hello hello \
600
interim-four-blank-lines \
601
[join [list \
602
"HTTP/1.1 100 Continue\n\n\n\n\n\nHTTP/1.1 200 OK\n" \
603
"Content-Length: 5\n\n"] ""] \
604
hello hello \
605
chunked "HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n" \
606
"3\r\nhel\r\n2\r\nlo\r\n0\r\n\r\n" hello \
607
chunked-upper-case "HTTP/1.1 200 OK\nTransfer-Encoding:CHUNKED\n\n" \
608
"A\r\n0123456789\r\n0\r\n\r\n" 0123456789 \
609
chunked-hex-letters "HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n" \
610
"a\r\n0123456789\r\n00\r\n\r\n" 0123456789 \
611
chunked-extension "HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n" \
612
"5;name=value\r\nhello\r\n0\r\n\r\n" hello \
613
chunked-extension-spaces "HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n" \
614
"5 \t; \tname\r\nhello\r\n0 \r\n\r\n" hello \
615
chunked-extension-punctuation \
616
"HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n" \
617
"5;!x\r\nhello\r\n0\r\n\r\n" hello \
618
chunked-trailer "HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n" \
619
"5\r\nhello\r\n0\r\nTrailer: x\r\nOther: y\r\n\r\n" hello \
620
chunked-trailer-cut-short "HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n" \
621
"5\r\nhello\r\n0\r\nTrailer: x" hello \
622
chunked-empty "HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n" \
623
"0\r\n\r\n" "" \
624
] {
625
url_httpmsg [list [url_crlf $urlHeader]$urlBody]
626
test url-http-accepted-$urlName {$CODE == 0 && $RESULT eq $urlExpected}
627
}
628
629
# Replies that are refused. Each row is a name, the reply, and what
630
# "fossil test-httpmsg -v" must say.
631
#
632
foreach {urlName urlReply urlMessage} [list \
633
nothing "" {server did not reply} \
634
not-http "hello\r\n\r\n" {server did not reply} \
635
status-bad-version "HTTP/1.x 200 OK\n\n" {Read: \[HTTP/1.x 200 OK\]\s*$} \
636
status-no-code "HTTP/1.1\n\n" {Read: \[HTTP/1.1\]\s*$} \
637
status-lower-case "http/1.1 200 OK\nContent-Length: 0\n\n" \
638
{Read: \[http/1.1 200 OK\]\s*$} \
639
status-500 "HTTP/1.1 500 Server Error\n\n" \
640
{server says: 500 Server Error} \
641
status-500-no-reason "HTTP/1.1 500\n\n" {server says: 500\n} \
642
status-tab "HTTP/1.1\t500\n\n" {server says: \n} \
643
status-404 "HTTP/1.1 404 Not Found\nContent-Length: 0\n\n" \
644
{server says: 404 Not Found} \
645
status-negative "HTTP/1.1 -200 OK\n\n" {server says: -200 OK} \
646
status-huge "HTTP/1.1 99999999999999999999 OK\n\n" {server says: } \
647
status-204 "HTTP/1.1 204 No Content\n\n" {server says: 204 No Content} \
648
folded-space "HTTP/1.1 200 OK\nX-A: b\n c\n\n" \
649
{server sent a folded header line} \
650
folded-tab "HTTP/1.1 200 OK\nX-A: b\n\tc\n\n" \
651
{server sent a folded header line} \
652
interim-only "HTTP/1.1 100 Continue\n\n" {server did not reply} \
653
interim-cut-short "HTTP/1.1 100 Continue\nX-A: b\n" {server did not reply} \
654
interim-five-blank-lines \
655
"HTTP/1.1 100 Continue\n\n\n\n\n\n\nHTTP/1.1 200 OK\n\n" \
656
{server did not reply} \
657
content-length-none "HTTP/1.1 200 OK\nContent-Length:\n\n" \
658
{server sent a bad content-length: Content-Length:} \
659
content-length-negative "HTTP/1.1 200 OK\nContent-Length: -5\n\nhello" \
660
{server sent a bad content-length} \
661
content-length-plus "HTTP/1.1 200 OK\nContent-Length: +5\n\nhello" \
662
{server sent a bad content-length} \
663
content-length-hex "HTTP/1.1 200 OK\nContent-Length: 0x5\n\nhello" \
664
{server sent a bad content-length} \
665
content-length-two-numbers "HTTP/1.1 200 OK\nContent-Length: 5 5\n\nhello" \
666
{server sent a bad content-length} \
667
content-length-list "HTTP/1.1 200 OK\nContent-Length: 5,5\n\nhello" \
668
{server sent a bad content-length} \
669
content-length-2147483648 \
670
"HTTP/1.1 200 OK\nContent-Length: 2147483648\n\nhello" \
671
{server sent a bad content-length} \
672
content-length-twice-different \
673
"HTTP/1.1 200 OK\nContent-Length: 5\nContent-Length: 4\n\nhello" \
674
{server sent two different content-length values} \
675
content-length-zero-then-other \
676
"HTTP/1.1 200 OK\nContent-Length: 0\nContent-Length: 5\n\nhello" \
677
{server sent two different content-length values} \
678
content-length-too-long "HTTP/1.1 200 OK\nContent-Length: 6\n\nhello" \
679
{response truncated: got 5 bytes of 6} \
680
transfer-encoding-gzip "HTTP/1.1 200 OK\nTransfer-Encoding: gzip\n\n" \
681
{unsupported transfer-encoding: gzip} \
682
transfer-encoding-gzip-chunked \
683
"HTTP/1.1 200 OK\nTransfer-Encoding: gzip, chunked\n\n" \
684
{unsupported transfer-encoding: gzip, chunked} \
685
transfer-encoding-chunked-more \
686
"HTTP/1.1 200 OK\nTransfer-Encoding: chunked, gzip\n\n" \
687
{unsupported transfer-encoding: chunked, gzip} \
688
transfer-encoding-empty "HTTP/1.1 200 OK\nTransfer-Encoding:\n\n" \
689
{unsupported transfer-encoding: } \
690
transfer-encoding-twice \
691
[join [list \
692
"HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n" \
693
"Transfer-Encoding: chunked\n\n0\n\n"] ""] \
694
{server sent two transfer-encoding headers} \
695
transfer-encoding-and-length \
696
[join [list \
697
"HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n" \
698
"Content-Length: 5\n\n5\nhello\n0\n\n"] ""] \
699
{reply has both content-length and transfer-encoding} \
700
length-and-transfer-encoding \
701
[join [list \
702
"HTTP/1.1 200 OK\nContent-Length: 0\n" \
703
"Transfer-Encoding: chunked\n\n0\n\n"] ""] \
704
{reply has both content-length and transfer-encoding} \
705
chunk-no-body "HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n" \
706
{chunked reply ended without terminator} \
707
chunk-blank-line "HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n\n" \
708
{chunked reply ended without terminator} \
709
chunk-size-letters "HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\nzz\n" \
710
{chunked reply: missing or malformed chunk size} \
711
chunk-size-negative \
712
"HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n-5\nhello\n0\n\n" \
713
{chunked reply: missing or malformed chunk size} \
714
chunk-size-0x \
715
"HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n0x5\nhello\n0\n\n" \
716
{chunked reply: malformed chunk size line} \
717
chunk-size-space-first \
718
"HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n 5\nhello\n0\n\n" \
719
{chunked reply: missing or malformed chunk size} \
720
chunk-size-80000000 \
721
"HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n80000000\nhello\n0\n\n" \
722
{chunked reply: invalid chunk size} \
723
chunk-size-twenty-digits \
724
[join [list \
725
"HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n" \
726
"FFFFFFFFFFFFFFFFFFFF\nhello\n0\n\n"] ""] \
727
{chunked reply: invalid chunk size} \
728
chunk-size-then-text \
729
"HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n5 x\nhello\n0\n\n" \
730
{chunked reply: malformed chunk size line} \
731
chunk-extension-no-name \
732
"HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n5;\nhello\n0\n\n" \
733
{chunked reply: malformed chunk size line} \
734
chunk-extension-equals \
735
"HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n5; =v\nhello\n0\n\n" \
736
{chunked reply: malformed chunk size line} \
737
chunk-cut-short "HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n5\nhel" \
738
{chunked reply truncated} \
739
chunk-no-data "HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n5\n" \
740
{chunked reply truncated} \
741
chunk-too-long \
742
"HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n3\nhello\n0\n\n" \
743
{chunked reply: no line end after chunk data} \
744
chunk-no-line-end "HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n5\nhello" \
745
{chunked reply ended without terminator} \
746
chunk-no-terminator \
747
"HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n5\nhello\n" \
748
{chunked reply ended without terminator} \
749
redirect-no-location "HTTP/1.1 302 Found\nContent-Length: 0\n\n" \
750
{"location:" missing from 302 redirect reply} \
751
redirect-301-no-location "HTTP/1.1 301 Moved\n\n" \
752
{"location:" missing from 301 redirect reply} \
753
redirect-307-no-location "HTTP/1.1 307 Temporary\n\n" \
754
{"location:" missing from 307 redirect reply} \
755
redirect-308-no-location "HTTP/1.1 308 Permanent\n\n" \
756
{"location:" missing from 308 redirect reply} \
757
redirect-empty-location "HTTP/1.1 302 Found\nLocation:\n\n" \
758
{malformed redirect: Location:} \
759
redirect-blank-location "HTTP/1.1 302 Found\nLocation: \n\n" \
760
{malformed redirect: Location:} \
761
redirect-to-file \
762
"HTTP/1.1 302 Found\nLocation: file:///nosuchdir/r.fossil\n\n" \
763
{cannot redirect to file:///nosuchdir/r.fossil} \
764
redirect-to-ssh "HTTP/1.1 302 Found\nLocation: ssh://h/r.fossil\n\n" \
765
{cannot redirect to ssh://h/r.fossil} \
766
redirect-to-other-scheme "HTTP/1.1 302 Found\nLocation: ftp://h/x\n\n" \
767
{unknown repository: ftp://h/x} \
768
redirect-to-bad-port "HTTP/1.1 302 Found\nLocation: http://h:99999/\n\n" \
769
{port number too large in url} \
770
redirect-loop "HTTP/1.1 302 Found\nLocation: http://127.0.0.1:PORT/x\n\n" \
771
{redirect limit exceeded} \
772
redirect-only-xfer "HTTP/1.1 302 Found\nLocation: /xfer\n\n" \
773
{unknown repository: \n} \
774
status-401 "HTTP/1.1 401 Unauthorized\n\n" \
775
{server says: 401 Unauthorized} \
776
] {
777
url_httpmsg [list [url_crlf $urlReply]] -v
778
test_match url-http-refused-$urlName "$CODE $RESULT" \
779
"^1 (?:.*\n)*.*$urlMessage"
780
url_httpmsg [list [url_crlf $urlReply]]
781
test url-http-refused-quiet-$urlName {$CODE == 1}
782
}
783
784
# A redirect to an ssh: URL with no path is refused.
785
#
786
url_httpmsg [list [url_crlf "HTTP/1.1 302 Found\nLocation: ssh://h\n\n"]] -v
787
test_match url-http-refused-redirect-to-ssh-no-path "$CODE $RESULT" \
788
{^1 (?:.*\n)*.*cannot redirect to ssh://h}
789
790
# A status line that test-httpmsg prints an error for without -v only
791
# when it is not told to be quiet: it is quiet when it prints the reply
792
# on standard output, and it is not when the reply goes to a file.
793
#
794
foreach {urlName urlReply urlMessage} [list \
795
status-500 "HTTP/1.1 500 Server Error\n\n" {server says: 500 Server Error} \
796
no-reply "" {server did not reply} \
797
redirect-no-location "HTTP/1.1 302 Found\n\n" \
798
{"location:" missing from 302 redirect reply} \
799
redirect-loop "HTTP/1.1 302 Found\nLocation: http://127.0.0.1:PORT/x\n\n" \
800
{redirect limit exceeded} \
801
redirect-to-file "HTTP/1.1 302 Found\nLocation: file:///nosuchdir/r\n\n" \
802
{cannot redirect to file:///nosuchdir/r} \
803
] {
804
file delete out.txt
805
url_httpmsg [list [url_crlf $urlReply]] --out out.txt
806
test url-http-message-$urlName \
807
{$CODE == 1 && [string first $urlMessage $RESULT] >= 0}
808
url_httpmsg [list [url_crlf $urlReply]]
809
test url-http-message-quiet-$urlName {$CODE == 1 && $RESULT eq ""}
810
}
811
812
# A reply that is shorter than its Content-Length is an error, and only
813
# the bytes that came are printed.
814
#
815
set urlShort [url_crlf "HTTP/1.1 200 OK\nContent-Length: 6\n\nhello"]
816
file delete out.txt
817
url_httpmsg [list $urlShort] --out out.txt
818
test url-http-message-truncated {$CODE == 1 &&
819
[string first "response truncated: got 5 bytes of 6" $RESULT] >= 0}
820
url_httpmsg [list $urlShort]
821
test url-http-truncated-bytes {$RESULT eq "hello"}
822
823
# A redirect is followed three times, with the same method and payload,
824
# and the fourth is refused. 301, 302, 307 and 308 are followed. A
825
# Location that ends in /xfer is used without that ending.
826
#
827
foreach urlStatus {301 302 307 308} {
828
url_httpmsg [list \
829
[url_crlf \
830
"HTTP/1.1 $urlStatus Moved\nLocation: http://127.0.0.1:PORT/b\n\n"] \
831
[url_crlf "HTTP/1.1 200 OK\nContent-Length: 5\n\nhello"]]
832
test url-http-redirect-$urlStatus {$CODE == 0 && $RESULT eq "hello" &&
833
[url_request_line 0] eq "GET /x HTTP/1.1" &&
834
[url_request_line 1] eq "GET /b HTTP/1.1"}
835
}
836
write_file payload.txt "payload text\n"
837
url_httpmsg [list \
838
[url_crlf "HTTP/1.1 302 Found\nLocation: http://127.0.0.1:PORT/b\n\n"] \
839
[url_crlf "HTTP/1.1 302 Found\nLocation: http://127.0.0.1:PORT/c\n\n"] \
840
[url_crlf \
841
"HTTP/1.1 302 Found\nLocation: http://127.0.0.1:PORT/d/xfer\n\n"] \
842
[url_crlf "HTTP/1.1 200 OK\nContent-Length: 5\n\nhello"]] payload.txt
843
test url-http-redirect-three {$CODE == 0 && $RESULT eq "hello" &&
844
[llength $urlRequests] == 4 &&
845
[url_request_line 3] eq "POST /d HTTP/1.1" &&
846
[url_request_body 3] eq "payload text\n"}
847
url_httpmsg [list \
848
[url_crlf "HTTP/1.1 302 Found\nLocation: http://127.0.0.1:PORT/b\n\n"]]
849
test url-http-redirect-four {$CODE == 1 && [llength $urlRequests] == 4}
850
url_httpmsg [list \
851
[url_crlf \
852
"HTTP/1.1 302 Found\nX-Other: 1\nLOCATION:http://127.0.0.1:PORT/b\n\n"] \
853
[url_crlf "HTTP/1.1 200 OK\nContent-Length: 5\n\nhello"]] -v
854
test url-http-redirect-after-other-header {$CODE == 0 &&
855
[string first "redirect with status 302 to http://127.0.0.1:" $RESULT] >= 0}
856
857
# A Location that ends in /xfer loses only the last one, so that a
858
# repository whose own path ends in /xfer is still found.
859
#
860
url_httpmsg [list \
861
[url_crlf \
862
"HTTP/1.1 302 Found\nLocation: http://127.0.0.1:PORT/d/xfer/xfer\n\n"] \
863
[url_crlf "HTTP/1.1 200 OK\nContent-Length: 5\n\nhello"]]
864
test_eq url-http-redirect-xfer-twice [url_request_line 1] "GET /d/xfer HTTP/1.1"
865
866
# A 401 reply is asked for again twice, and then is an error. Standard
867
# input is not a terminal, so fossil cannot ask for credentials.
868
#
869
url_httpmsg [list [url_crlf "HTTP/1.1 401 Unauthorized\n\n"]] -v
870
test url-http-401-three-requests {$CODE == 1 && [llength $urlRequests] == 3 &&
871
[string first "server says: 401 Unauthorized" $RESULT] >= 0}
872
url_httpmsg [list [url_crlf "HTTP/1.1 401 Unauthorized\n\n"] \
873
[url_crlf "HTTP/1.1 200 OK\nContent-Length: 5\n\nhello"]]
874
test url-http-401-then-200 {$CODE == 0 && $RESULT eq "hello" &&
875
[llength $urlRequests] == 2}
876
877
###############################################################################
878
# The request.
879
880
set urlOk [url_crlf "HTTP/1.1 200 OK\nContent-Length: 2\n\nok"]
881
882
url_httpmsg [list $urlOk]
883
test url-request-get {[url_request_line 0] eq "GET /x HTTP/1.1" &&
884
[string match 127.0.0.1:* [url_request_header 0 Host]] &&
885
[url_request_header 0 Connection] eq "close" &&
886
[string match "Fossil/*" [url_request_header 0 User-Agent]] &&
887
[url_request_header 0 Content-Type] eq "(none)" &&
888
[url_request_header 0 Content-Length] eq "(none)" &&
889
[url_request_header 0 X-Fossil-Transport] eq "(none)" &&
890
[url_request_header 0 Authorization] eq "(none)" &&
891
[url_request_header 0 Proxy-Authorization] eq "(none)" &&
892
[url_request_header 0 Cookie] eq "(none)"}
893
894
set urlPort [url_listen [list $urlOk]]
895
fossil_raw -seconds 60 test-httpmsg http://127.0.0.1:$urlPort
896
test_eq url-request-no-path [url_request_line 0] "GET / HTTP/1.1"
897
test_listen_stop
898
899
url_httpmsg [list $urlOk] payload.txt
900
test url-request-post {[url_request_line 0] eq "POST /x HTTP/1.1" &&
901
[url_request_header 0 Content-Type] eq "text/plain" &&
902
[url_request_header 0 Content-Length] eq "13" &&
903
[url_request_body 0] eq "payload text\n"}
904
905
write_file empty.txt ""
906
url_httpmsg [list $urlOk] empty.txt
907
test_eq url-request-empty-payload [url_request_line 0] "GET /x HTTP/1.1"
908
909
url_httpmsg [list $urlOk] payload.txt --mimetype application/x-test
910
test_eq url-request-mimetype [url_request_header 0 Content-Type] \
911
application/x-test
912
913
set urlPort [url_listen [list $urlOk]]
914
fossil_raw -seconds 60 -input "from standard input" \
915
test-httpmsg http://127.0.0.1:$urlPort/x -
916
test url-request-standard-input {
917
[url_request_header 0 Content-Type] eq "application/x-unknown" &&
918
[url_request_body 0] eq "from standard input"}
919
test_listen_stop
920
921
# --compress sends the payload compressed and expects a compressed
922
# reply: four bytes of size and then a zlib stream.
923
#
924
proc url_compress {text} {
925
return [binary format I [string length $text]][zlib compress $text]
926
}
927
proc url_uncompress {bytes} {
928
if {[catch {zlib decompress [string range $bytes 4 end]} text]} {
929
return "(not compressed)"
930
}
931
binary scan $bytes I size
932
if {$size != [string length $text]} {return "(wrong size)"}
933
return $text
934
}
935
set urlPort [url_listen [list \
936
[url_crlf \
937
"HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: [string length [url_compress "compressed reply"]]\n\n"][url_compress "compressed reply"]]]
938
fossil_raw -seconds 60 test-httpmsg http://127.0.0.1:$urlPort/x payload.txt \
939
--compress
940
test url-request-compress {$CODE == 0 && $RESULT eq "compressed reply" &&
941
[url_uncompress [url_request_body 0]] eq "payload text\n"}
942
test_listen_stop
943
944
# The reply goes to the file named by --out or by the third argument,
945
# but not by both.
946
#
947
file delete out.txt
948
url_httpmsg [list $urlOk] --out out.txt
949
test url-request-out-option {$CODE == 0 && $RESULT eq "" &&
950
[read_file out.txt] eq "ok"}
951
file delete out.txt
952
url_httpmsg [list $urlOk] payload.txt out.txt
953
test url-request-out-argument {$CODE == 0 && [read_file out.txt] eq "ok"}
954
file delete out.txt
955
url_httpmsg [list $urlOk] payload.txt out.txt --out other.txt
956
test url-request-out-twice {$CODE == 1 && ![file exists out.txt] &&
957
[string first "output file specified twice" $RESULT] >= 0}
958
url_httpmsg [list $urlOk] -v
959
test url-request-verbose {$CODE == 0 &&
960
[holds_strings $RESULT [list "Sending " "Read: \[HTTP/1.1 200 OK\]" \
961
"Reply received: 2 of 2 bytes"]]}
962
url_httpmsg [list [url_crlf "HTTP/1.1 200 OK\n\n"]hello] -v
963
test url-request-verbose-no-length {$CODE == 0 && [string first \
964
"Reply received: 5 bytes (w/o content-length)" $RESULT] >= 0}
965
966
foreach urlOption {--ipv4 --no-cert-verify} {
967
url_httpmsg [list $urlOk] $urlOption
968
test url-request-option$urlOption {$CODE == 0 && $RESULT eq "ok"}
969
}
970
# --ipv6 asks for an IPv6 address of the test server, which listens on
971
# 127.0.0.1: macOS reaches it through a mapped address, Linux refuses it.
972
url_httpmsg [list $urlOk] --ipv6
973
test url-request-option--ipv6 {($CODE == 0 && $RESULT eq "ok") ||
974
[string first "getaddrinfo() fails" $RESULT] >= 0}
975
fossil_raw test-httpmsg
976
test url-request-usage-none \
977
{$CODE == 1 && [string match "*Usage:*URL*" $RESULT]}
978
fossil_raw test-httpmsg http://127.0.0.1:1/ a b c
979
test url-request-usage-many \
980
{$CODE == 1 && [string match "*Usage:*URL*" $RESULT]}
981
fossil_raw test-httpmsg file://$urlRepo
982
test url-request-file-needs-subpath {$CODE == 1 && [string first \
983
"the --subpath option is required for file://" $RESULT] >= 0}
984
985
# --httptrace writes each request and reply to a numbered file, and
986
# sends the payload with the debug content type.
987
#
988
file mkdir trace
989
cd trace
990
url_httpmsg [list $urlOk] ../payload.txt --httptrace
991
test url-request-httptrace {$CODE == 0 &&
992
[url_request_header 0 Content-Type] eq "text/plain" &&
993
[string match "POST /x HTTP/1.1\r\n*\r\n\r\npayload text\n" \
994
[read_file http-request-1.txt]] &&
995
[string match "HTTP/1.1 200 OK\r\n*" [read_file http-reply-1.txt]]}
996
cd ..
997
998
# --httptrace in a directory where the files cannot be made does not
999
# stop the exchange.
1000
#
1001
file mkdir readonly
1002
file attributes readonly -permissions 0555
1003
if {$is_windows || [file writable readonly]} {
1004
test_skip_part "cannot make a directory that fossil cannot write in"
1005
} else {
1006
cd readonly
1007
url_httpmsg [list $urlOk] ../payload.txt --httptrace
1008
test url-request-httptrace-read-only {
1009
[string match "*\nok" $RESULT] &&
1010
[url_request_body 0] eq "payload text\n" &&
1011
[glob -nocomplain http-re*] eq ""}
1012
cd ..
1013
}
1014
file attributes readonly -permissions 0755
1015
1016
###############################################################################
1017
# The login card of a sync request. test-httpmsg --xfer sends the
1018
# payload as a sync message: with a login card when the URL names a
1019
# user, and expecting an application/x-fossil reply. The card is
1020
#
1021
# login USER NONCE SIGNATURE
1022
#
1023
# where NONCE is the SHA1 hash of the rest of the message and SIGNATURE
1024
# is the SHA1 hash of NONCE followed by the shared secret. test-httpmsg
1025
# opens no repository, so the shared secret is the password as given.
1026
1027
# Returns the login card for USER with the shared secret SECRET, for a
1028
# message BODY.
1029
proc url_login_card {user secret body} {
1030
set nonce [sha1_text $body]
1031
return "login $user $nonce [sha1_text $nonce$secret]"
1032
}
1033
set urlSyncOk [url_crlf \
1034
[join [list \
1035
"HTTP/1.1 200 OK\n" \
1036
"Content-Type: application/x-fossil-uncompressed\n" \
1037
"Content-Length: 2\n\n"] ""]]ok
1038
set urlCard "[url_login_card bob secret "payload text\n"]\npayload text\n"
1039
1040
# Runs test-httpmsg --xfer for the user and password in USERINFO, which
1041
# is empty or ends with "@", and further options in ARGS.
1042
proc url_xfer {userinfo args} {
1043
set port [url_listen [list $::urlSyncOk]]
1044
fossil_raw -seconds 60 test-httpmsg http://${userinfo}127.0.0.1:$port/x \
1045
payload.txt --xfer {*}$args
1046
test_listen_stop
1047
}
1048
1049
url_xfer bob:secret@
1050
test url-login-password {$CODE == 0 && $RESULT eq "ok" &&
1051
[url_request_header 0 Content-Type] eq "application/x-fossil" &&
1052
[url_request_body 0] eq $urlCard}
1053
url_xfer b%20ob:secret@
1054
test_eq url-login-user-space [url_request_body 0] \
1055
"[string map {{login b ob} {login b\sob}} \
1056
[url_login_card "b ob" secret "payload text\n"]]\npayload text\n"
1057
url_xfer bob:@
1058
test_eq url-login-empty-password [url_request_body 0] \
1059
"[url_login_card bob "" "payload text\n"]\npayload text\n"
1060
url_xfer ""
1061
test url-login-no-user {$CODE == 0 &&
1062
[url_request_body 0] eq "payload text\n"}
1063
url_xfer anonymous:secret@
1064
test url-login-anonymous {$CODE == 0 &&
1065
[url_request_body 0] eq "payload text\n"}
1066
url_xfer bob@
1067
test url-login-no-password {$CODE == 1 && [url_request_line 0] eq "" &&
1068
[string first {missing or incorrect password for user "bob"} $RESULT] >= 0}
1069
url_xfer bob:secret@ --httptrace
1070
test url-login-httptrace {$CODE == 0 &&
1071
[url_request_header 0 Content-Type] eq "application/x-fossil-debug" &&
1072
[url_request_body 0] eq $urlCard}
1073
file delete http-request-1.txt http-reply-1.txt
1074
url_xfer bob:secret@ --compress
1075
test url-login-compress {$CODE == 0 &&
1076
[url_uncompress [url_request_body 0]] eq $urlCard}
1077
1078
# A pull from an open check-out signs the card with the SHA1 hash of
1079
# PROJECTCODE/USER/PASSWORD. A password of 40 hex digits is that hash
1080
# already.
1081
#
1082
fossil sql "SELECT value FROM config WHERE name='project-code'"
1083
set urlProject [string trim $RESULT]
1084
set urlSecret [sha1_text $urlProject/bob/secret]
1085
set urlPullOk [url_crlf \
1086
[join [list \
1087
"HTTP/1.1 200 OK\n" \
1088
"Content-Type: application/x-fossil-uncompressed\n" \
1089
"Content-Length: 41\n\n"] ""]]
1090
append urlPullOk "# timestamp 2026-01-01T00:00:00 errors 0\n"
1091
1092
# Returns 1 if request NUMBER of the last exchange begins with the login
1093
# card for USER and the shared secret SECRET, and 0 otherwise.
1094
proc url_login_ok {number user secret} {
1095
set body [url_request_body $number]
1096
set end [string first \n $body]
1097
set rest [string range $body [expr {$end + 1}] end]
1098
return [expr {[string range $body 0 [expr {$end - 1}]] eq
1099
[url_login_card $user $secret $rest]}]
1100
}
1101
1102
# Runs "fossil pull" with --httptrace, which sends the request as plain
1103
# text, and the options in ARGS, from the URL with USERINFO.
1104
proc url_pull {userinfo args} {
1105
set port [url_listen [list $::urlPullOk]]
1106
fossil_raw -seconds 60 pull http://${userinfo}127.0.0.1:$port/repo \
1107
--httptrace {*}$args
1108
test_listen_stop
1109
file delete {*}[glob -nocomplain http-request-*.txt http-reply-*.txt]
1110
return $port
1111
}
1112
url_pull bob:secret@ --once
1113
test url-login-project-code {$CODE == 0 && [url_login_ok 0 bob $urlSecret]}
1114
url_pull bob:$urlSecret@ --once
1115
test url-login-password-is-hash {$CODE == 0 &&
1116
[url_login_ok 0 bob $urlSecret]}
1117
set urlNotHex [string repeat g 40]
1118
url_pull bob:$urlNotHex@ --once
1119
test url-login-password-forty-letters {$CODE == 0 &&
1120
[url_login_ok 0 bob [sha1_text $urlProject/bob/$urlNotHex]]}
1121
url_pull bob:[string range $urlSecret 1 end]@ --once
1122
test url-login-password-thirty-nine-digits {$CODE == 0 && [url_login_ok 0 bob \
1123
[sha1_text $urlProject/bob/[string range $urlSecret 1 end]]]}
1124
1125
# With --login-card-header the card goes in a cookie, not in the payload.
1126
#
1127
url_xfer bob:secret@ --login-card-header
1128
test url-login-cookie {$CODE == 0 &&
1129
[url_request_body 0] eq "payload text\n" &&
1130
[url_request_header 0 Cookie] eq "x-f-l-c=[string map {{ } +} \
1131
[url_login_card bob secret "payload text\n"]]"}
1132
url_xfer "" --login-card-header
1133
test url-login-cookie-no-user {$CODE == 0 &&
1134
[url_request_body 0] eq "payload text\n" &&
1135
[url_request_header 0 Cookie] eq "(none)"}
1136
url_xfer bob:secret@ --login-card-header --httptrace
1137
test url-login-cookie-httptrace {$CODE == 0 &&
1138
[url_request_body 0] eq "payload text\n" &&
1139
[string match "x-f-l-c=login+bob+*" [url_request_header 0 Cookie]]}
1140
file delete http-request-1.txt http-reply-1.txt
1141
url_xfer bob:secret@ --login-card-header --compress
1142
test url-login-cookie-compress {
1143
[url_uncompress [url_request_body 0]] eq "payload text\n" &&
1144
[string match "x-f-l-c=login+bob+*" [url_request_header 0 Cookie]]}
1145
set urlPort [url_listen [list $urlOk]]
1146
fossil_raw -seconds 60 test-httpmsg http://bob:[email protected]:$urlPort/x \
1147
--xfer --login-card-header
1148
test url-login-cookie-no-payload {
1149
[url_request_line 0] eq "GET /x HTTP/1.1" &&
1150
[url_request_header 0 Cookie] eq "(none)"}
1151
test_listen_stop
1152
1153
# A sync reply of another content type is an error message from the
1154
# server. It is printed without its HTML tags.
1155
#
1156
proc url_xfer_reply {type body args} {
1157
set port [url_listen [list \
1158
[url_crlf \
1159
[join [list \
1160
"HTTP/1.1 200 OK\nContent-Type: $type\n" \
1161
"Content-Length: [string length $body]\n\n"] ""]]$body]]
1162
fossil_raw -seconds 60 test-httpmsg http://127.0.0.1:$port/x \
1163
payload.txt --xfer {*}$args
1164
test_listen_stop
1165
}
1166
url_xfer_reply text/html "<p>no <b>such</b> page</p>" -v
1167
test url-xfer-error-verbose {$CODE == 1 &&
1168
[string first "server sends error: no such page" $RESULT] >= 0}
1169
url_xfer_reply text/html "plain text, no tags" -v
1170
test url-xfer-error-plain-text {$CODE == 1 &&
1171
[string first "server sends error: plain text, no tags" $RESULT] >= 0}
1172
url_xfer_reply text/html "cut short <b" -v
1173
test url-xfer-error-open-tag {$CODE == 1 &&
1174
[string first "server sends error: cut short " $RESULT] >= 0}
1175
file delete out.txt
1176
url_xfer_reply text/html "<p>no such page</p>" --out out.txt
1177
test url-xfer-error-to-file {$CODE == 1 &&
1178
[holds_strings $RESULT {"server sends error" "!no such page"}]}
1179
url_xfer_reply text/html "<p>no such page</p>"
1180
test url-xfer-error-quiet {$CODE == 1 &&
1181
[string first "server sends error" $RESULT] < 0}
1182
url_xfer_reply application/x-fossil-debug "debug reply"
1183
test url-xfer-reply-debug {$CODE == 0 && $RESULT eq "debug reply"}
1184
url_xfer_reply application/x-fossil [url_compress "compressed reply"]
1185
test url-xfer-reply-compressed {$CODE == 0 && $RESULT eq "compressed reply"}
1186
url_xfer_reply APPLICATION/X-FOSSIL-UNCOMPRESSED "plain reply"
1187
test url-xfer-reply-uncompressed {$CODE == 0 && $RESULT eq "plain reply"}
1188
1189
###############################################################################
1190
# The file: transport. test-httpmsg sends the request for the path
1191
# given by --subpath to "fossil http" on the repository file.
1192
1193
fossil_raw -seconds 60 test-httpmsg file://$urlRepo --subpath /test-env
1194
test url-file-page {$CODE == 0 &&
1195
[holds_strings $RESULT {<title> "Environment Test"}]}
1196
fossil_raw -seconds 60 test-httpmsg file://$urlRepo --subpath /nosuchpage -v
1197
test url-file-not-found {$CODE == 1 &&
1198
[string first "server says: 404 Not Found" $RESULT] >= 0}
1199
fossil_raw -seconds 60 test-httpmsg file://$urlRepo --subpath / -v
1200
test url-file-redirect {$CODE == 1 &&
1201
[string first "cannot redirect from file://$urlRepo to " $RESULT] >= 0}
1202
fossil_raw -seconds 60 test-httpmsg file://$urlRepo --subpath /
1203
test url-file-redirect-quiet {$CODE == 1 && $RESULT eq ""}
1204
1205
###############################################################################
1206
# Clone, pull and sync against the server of this file.
1207
1208
# Returns the reply header for a sync reply with the body BODY.
1209
proc url_sync_reply {body} {
1210
return [url_crlf \
1211
[join [list \
1212
"HTTP/1.1 200 OK\n" \
1213
"Content-Type: application/x-fossil-uncompressed\n" \
1214
"Content-Length: [string length $body]\n\n"] ""]]$body
1215
}
1216
set urlNotFound [url_crlf "HTTP/1.1 404 Not Found\nContent-Length: 0\n\n"]
1217
1218
# Runs "fossil VERB URL" with the options in ARGS, where "PORT" in URL
1219
# and in ARGS stands for the port of a server that answers with REPLIES.
1220
proc url_sync {verb url replies args} {
1221
set port [url_listen $replies]
1222
set map [list PORT $port]
1223
fossil_raw -seconds 60 $verb [string map $map $url] {*}[string map $map $args]
1224
test_listen_stop
1225
return $port
1226
}
1227
1228
# A proxy gets the whole URL in the request line and its own user and
1229
# password in a Proxy-Authorization header.
1230
#
1231
url_sync pull http://example.invalid:81/repo [list $urlNotFound] --once \
1232
--proxy http://pu:p%[email protected]:PORT
1233
test url-sync-proxy {$CODE == 1 &&
1234
[string first "via proxy: http://[email protected]:" $RESULT] >= 0 &&
1235
[url_request_line 0] eq "POST http://example.invalid:81/repo HTTP/1.1" &&
1236
[url_request_header 0 Host] eq "example.invalid:81" &&
1237
[url_request_header 0 Proxy-Authorization] eq
1238
"Basic [binary encode base64 pu:p@p]"}
1239
url_sync pull http://example.invalid/repo [list $urlNotFound] --once \
1240
--proxy http://127.0.0.1:PORT
1241
test url-sync-proxy-no-user {$CODE == 1 &&
1242
[url_request_line 0] eq "POST http://example.invalid/repo HTTP/1.1" &&
1243
[url_request_header 0 Proxy-Authorization] eq "(none)"}
1244
url_sync pull http://example.invalid/repo [list $urlNotFound] --once \
1245
--proxy http://[email protected]:PORT
1246
test url-sync-proxy-user-no-password {$CODE == 1 &&
1247
[url_request_header 0 Proxy-Authorization] eq
1248
"Basic [binary encode base64 pu:]"}
1249
1250
# A redirect received through a proxy is followed through the proxy.
1251
#
1252
url_sync pull http://example.invalid/repo [list \
1253
[url_crlf "HTTP/1.1 302 Found\nLocation: http://127.0.0.1:PORT/moved\n\n"] \
1254
$urlNotFound] --once --proxy http://127.0.0.1:PORT
1255
test url-sync-proxy-redirect {[llength $urlRequests] == 2 &&
1256
[string match "POST http://127.0.0.1:*/moved HTTP/1.1" \
1257
[url_request_line 1]]}
1258
1259
# A proxy is an http: or https: URL. With any other value fossil must
1260
# not talk to the repository that the value names in place of the
1261
# remote.
1262
#
1263
fossil_raw -seconds 60 clone http://example.invalid/repo proxyclone.fossil \
1264
--proxy file://$urlRepo
1265
test url-sync-proxy-file {$CODE == 1 && ![file exists proxyclone.fossil]}
1266
file delete proxyclone.fossil
1267
1268
# A pull through a proxy remembers the URL of the remote, not of the
1269
# proxy. A proxy is not used for a file: URL.
1270
#
1271
fossil remote off
1272
url_sync pull http://example.invalid/repo [list $urlPullOk] \
1273
--proxy http://127.0.0.1:PORT
1274
fossil remote
1275
test_eq url-sync-proxy-remembered [string trim $RESULT] \
1276
http://example.invalid/repo
1277
fossil remote off
1278
fossil_raw -seconds 60 pull file://[file join $urlDir holder FOSSIL] --once \
1279
--proxy http://127.0.0.1:1
1280
test url-sync-proxy-file-url {$CODE == 0 &&
1281
[string first "via proxy" $RESULT] < 0}
1282
1283
# --httpauth gives the user and password for HTTP Basic authorization.
1284
# They are not sent again after a 401 reply to them.
1285
#
1286
url_sync pull http://127.0.0.1:PORT/repo [list $urlNotFound] --once \
1287
--httpauth hu:hp
1288
test_eq url-sync-httpauth [url_request_header 0 Authorization] \
1289
"Basic [binary encode base64 hu:hp]"
1290
url_sync pull http://127.0.0.1:PORT/repo \
1291
[list [url_crlf "HTTP/1.1 401 Unauthorized\n\n"]] --once --httpauth hu:hp
1292
test url-sync-httpauth-401 {$CODE == 1 && [llength $urlRequests] == 3 &&
1293
[url_request_header 0 Authorization] eq
1294
"Basic [binary encode base64 hu:hp]" &&
1295
[url_request_header 1 Authorization] eq "(none)" &&
1296
[string first "server says: 401 Unauthorized" $RESULT] >= 0}
1297
url_sync pull http://127.0.0.1:PORT/repo [list \
1298
[url_crlf "HTTP/1.1 302 Found\nLocation: http://127.0.0.1:PORT/moved\n\n"] \
1299
$urlNotFound] --once --httpauth hu:hp
1300
test url-sync-httpauth-redirect {[llength $urlRequests] == 2 &&
1301
[url_request_line 1] eq "POST /moved HTTP/1.1" &&
1302
[url_request_header 0 Authorization] eq
1303
"Basic [binary encode base64 hu:hp]" &&
1304
[url_request_header 1 Authorization] eq "(none)"}
1305
1306
# A pull that is redirected with 301 or 308 remembers the new URL. One
1307
# that is redirected with 302 or 307 does not.
1308
#
1309
foreach {urlStatus urlRemembered} {301 1 308 1 302 0 307 0} {
1310
fossil remote off
1311
set urlPort [url_sync pull http://127.0.0.1:PORT/old [list \
1312
[url_crlf \
1313
"HTTP/1.1 $urlStatus Moved\nLocation: http://127.0.0.1:PORT/new\n\n"] \
1314
$urlNotFound]]
1315
fossil remote
1316
test_eq url-sync-redirect-$urlStatus-remembered [string trim $RESULT] \
1317
[expr {$urlRemembered ? "http://127.0.0.1:$urlPort/new" : "off"}]
1318
}
1319
fossil remote off
1320
set urlPort [url_sync pull http://127.0.0.1:PORT/old [list \
1321
[url_crlf "HTTP/1.1 301 Moved\nLocation: http://127.0.0.1:PORT/new\n\n"] \
1322
$urlNotFound] --once]
1323
fossil remote
1324
test_eq url-sync-redirect-301-once [string trim $RESULT] off
1325
1326
# A pull that succeeds remembers the URL. It does not remember the
1327
# password when it cannot ask whether to.
1328
#
1329
fossil remote off
1330
set urlPort [url_pull bob:secret@]
1331
fossil remote
1332
test_eq url-sync-remembered-url [string trim $RESULT] \
1333
http://[email protected]:$urlPort/repo
1334
url_parse_test url-sync-password-not-remembered default \
1335
{--use-config --show-pw} {user bob passwd {} pwConfig last-sync-pw}
1336
fossil remote off
1337
1338
# A pull from a saved remote sends the saved password, and saves it
1339
# again as its hash for this project.
1340
#
1341
fossil remote add saved http://bob:[email protected]:1/repo
1342
url_parse_test url-sync-saved-password saved {--use-config --show-pw} \
1343
{user bob passwd secret pwConfig sync-pw:saved}
1344
set urlPort [url_listen [list $urlPullOk]]
1345
url_config sync-url:saved 'http://[email protected]:$urlPort/repo'
1346
fossil_raw -seconds 60 pull saved --once --httptrace
1347
test_listen_stop
1348
file delete {*}[glob -nocomplain http-request-*.txt http-reply-*.txt]
1349
test url-sync-saved-remote {$CODE == 0 && [url_login_ok 0 bob $urlSecret]}
1350
url_parse_test url-sync-saved-password-hashed saved {--use-config --show-pw} \
1351
[list user bob passwd $urlSecret]
1352
fossil remote delete saved
1353
1354
# A pull from the parent project signs the login card with the project
1355
# code of the parent.
1356
#
1357
url_config parent-project-code 'parentcode'
1358
url_pull bob:secret@ --from-parent-project --once
1359
test url-sync-parent-login {$CODE == 0 &&
1360
[url_login_ok 0 bob [sha1_text parentcode/bob/secret]]}
1361
fossil sql "DELETE FROM config WHERE name GLOB 'parent-project-*'"
1362
1363
# A pull from the parent project by the name of a saved remote
1364
# remembers the URL and the password as those of the parent project.
1365
#
1366
fossil remote add saved http://bob:[email protected]:1/repo
1367
set urlPort [url_listen [list $urlPullOk]]
1368
url_config sync-url:saved 'http://[email protected]:$urlPort/repo'
1369
url_config parent-project-code 'parentcode'
1370
fossil_raw -seconds 60 pull saved --from-parent-project
1371
test_listen_stop
1372
url_parse_test url-sync-parent-remembered default {--use-parent --show-pw} \
1373
[list name 127.0.0.1 port $urlPort user bob \
1374
passwd [sha1_text parentcode/bob/secret] pwConfig parent-project-pw]
1375
fossil remote
1376
test_eq url-sync-parent-not-default [string trim $RESULT] off
1377
fossil remote delete saved
1378
fossil sql "DELETE FROM config WHERE name GLOB 'parent-project-*'"
1379
1380
# A clone that fails with --httptrace prints the last reply. When there
1381
# is no reply it prints none.
1382
#
1383
url_sync clone http://127.0.0.1:PORT/repo [list \
1384
[url_sync_reply "error not\\sallowed\\shere\n"]] traceclone.fossil \
1385
--httptrace
1386
test url-sync-clone-httptrace {$CODE == 1 &&
1387
![file exists traceclone.fossil] &&
1388
[string first "server returned an error - clone aborted" $RESULT] >= 0 &&
1389
[regexp {clone aborted\s+HTTP/1.1 200 OK} $RESULT]}
1390
file delete {*}[glob -nocomplain http-request-*.txt http-reply-*.txt]
1391
set urlPort [url_listen {}]
1392
test_listen_stop
1393
fossil_raw -seconds 60 clone http://127.0.0.1:$urlPort/repo traceclone.fossil \
1394
--httptrace
1395
test url-sync-clone-httptrace-no-reply {$CODE == 1 &&
1396
![file exists traceclone.fossil] &&
1397
[string first "cannot connect to host 127.0.0.1:$urlPort" $RESULT] >= 0}
1398
test url-sync-clone-httptrace-no-reply-message \
1399
{[string first "server returned an error - clone aborted" $RESULT] >= 0}
1400
file delete {*}[glob -nocomplain http-request-*.txt http-reply-*.txt]
1401
1402
# --transport-command with --httptrace prints the command. A command
1403
# that fails, or writes no reply, is an error.
1404
#
1405
if {!$is_windows} {
1406
write_file transport-fail.sh "#!/bin/sh\nexit 3\n"
1407
file attributes transport-fail.sh -permissions 0755
1408
fossil_raw pull http://u:p@h:81/x --once \
1409
--transport-command [file join $urlDir transport-fail.sh] --httptrace
1410
test url-transport-fails {$CODE == 1 &&
1411
[string first "Transport command failed: " $RESULT] >= 0 &&
1412
[regexp -line {^RUN .*transport-fail.sh '?http://u:p@h:81/x'? } $RESULT]}
1413
fossil_raw pull {http://u:p@[::1]/x} --once \
1414
--transport-command [file join $urlDir transport-fail.sh] --httptrace
1415
test url-transport-url-ipv6 {
1416
[regexp -line {^RUN .*transport-fail.sh '?http://u:p@\[::1\]/x'? } \
1417
$RESULT]}
1418
fossil_raw pull file://$urlRepo --once \
1419
--transport-command [file join $urlDir transport-fail.sh] --httptrace
1420
test url-transport-url-file {[regexp -line \
1421
"^RUN .*transport-fail.sh '?file://$urlRepo'? " $RESULT]}
1422
}
1423
1424
###############################################################################
1425
# The sync button of the /xfersetup page syncs with the saved remote. A
1426
# remote with a user and no saved password gets a message, not a login
1427
# card.
1428
1429
write_file xfersetup.txt [url_crlf \
1430
[join [list \
1431
"POST /xfersetup HTTP/1.0\nHost: localhost\n" \
1432
"Referer: http://localhost/xfersetup\n" \
1433
"Content-Type: application/x-www-form-urlencoded\n" \
1434
"Content-Length: 6\n\nsync=1"] ""]]
1435
set urlPort [url_listen [list $urlPullOk]]
1436
url_config last-sync-url 'http://[email protected]:$urlPort/repo'
1437
url_config last-sync-pw obscure('secret')
1438
fossil_raw -seconds 60 http --in xfersetup.txt --ipaddr 127.0.0.1 $urlRepo \
1439
--localauth
1440
test_listen_stop
1441
test url-web-sync {[string first "HTTP/1.0 200 OK" $RESULT] == 0 &&
1442
[llength $urlRequests] == 1 &&
1443
[string first "Sync done" $RESULT] >= 0}
1444
set urlPort [url_listen [list $urlPullOk]]
1445
fossil sql "DELETE FROM config WHERE name='last-sync-pw'"
1446
url_config last-sync-url 'http://[email protected]:$urlPort/repo'
1447
fossil_raw -seconds 60 http --in xfersetup.txt --ipaddr 127.0.0.1 $urlRepo \
1448
--localauth
1449
test_listen_stop
1450
test url-web-sync-no-password {[string first "HTTP/1.0 200 OK" $RESULT] == 0 &&
1451
[string first "incorrect or missing password for user bob" $RESULT] >= 0}
1452
fossil sql "DELETE FROM config WHERE name GLOB 'last-sync-*'"
1453
1454
###############################################################################
1455
# Links that web pages build from their own query parameters. Each row
1456
# is a page and regular expressions for links that the page must have.
1457
1458
file mkdir sub
1459
write_file sub/a.txt "one\n"
1460
write_file sub/doc.md "# title\n"
1461
fossil add sub
1462
fossil commit -m one --no-warnings
1463
write_file sub/a.txt "one\ntwo\n"
1464
fossil commit -m two --no-warnings
1465
write_file sub/a.txt "one\ntwo\nthree\n"
1466
fossil commit -m three --no-warnings
1467
1468
foreach {urlName urlPage urlLinks} {
1469
dir /dir?name=sub&ci=tip
1470
{{href="/dir\?ci=tip"} {href="/dir\?ci=tip&amp;name=sub&amp;type=tree"}}
1471
dir-flat /tree?name=sub&ci=tip&type=flat
1472
{{href="/dir\?ci=tip&amp;type=flat"}
1473
{href="/dir\?ci=tip&amp;name=sub&amp;type=tree"}}
1474
tree /tree?name=sub&ci=tip&type=tree
1475
{{ci=tip}}
1476
leaves-all /leaves?all&ng&brbg
1477
{{href="/leaves\?ng&amp;brbg"} {href="/leaves\?ng&amp;brbg&amp;closed"}}
1478
leaves-closed /leaves?closed&nohidden&ubg
1479
{{href="/leaves\?nohidden&amp;ubg"}
1480
{href="/leaves\?nohidden&amp;ubg&amp;all"}}
1481
leaves-open /leaves?onlyhidden
1482
{{href="/leaves\?onlyhidden&amp;all"}
1483
{href="/leaves\?onlyhidden&amp;closed"}}
1484
timeline /timeline?n=1
1485
{{href="/timeline\?n=1&amp;advm=1&amp;udc=1"}
1486
{href="/timeline\?n=1&amp;b=[0-9-]+\+[0-9:]+"}}
1487
timeline-after /timeline?n=1&a=2020-01-01
1488
{{href="/timeline\?a=2020-01-01&amp;n=1&amp;advm=1&amp;udc=1"}
1489
{href="/timeline\?a=[0-9-]+\+[0-9:]+&amp;n=1"}}
1490
timeline-before-circa /timeline?n=1&b=2030-01-01&c=2026-01-01
1491
{{href="/timeline\?b=2030-01-01&amp;n=1&amp;advm=1&amp;udc=1"}
1492
{href="/timeline\?b=[0-9-]+\+[0-9:]+&amp;n=1"}}
1493
timeline-after-before /timeline?n=1&a=2020-01-01&b=2030-01-01&c=2026-01-01
1494
{{href="/timeline\?a=2020-01-01&amp;b=2030-01-01&amp;n=1&amp;advm=1&amp;udc=1"}}
1495
timeline-advanced /timeline?n=1&advm=1
1496
{{href="/timeline\?advm=0&amp;n=1&amp;udc=1"}}
1497
timeline-month /timeline?ym=2026-11
1498
{{href="/timeline\?ym=2026-11&amp;advm=1&amp;udc=1"}}
1499
file-markdown /file?name=sub/doc.md&ci=tip
1500
{{href="/file\?name=sub/doc.md&amp;ci=tip&amp;txt=1"}}
1501
file-markdown-text /file?name=sub/doc.md&ci=tip&txt=1
1502
{{href="/file\?name=sub/doc.md&amp;ci=tip"}}
1503
finfo /finfo?name=sub/a.txt&brbg&ubg&n=1
1504
{{sub/a.txt}}
1505
annotate /annotate?filename=sub/a.txt&checkin=tip&limit=1
1506
{{sub/a.txt}}
1507
annotate-no-limit /annotate?filename=sub/a.txt&checkin=tip
1508
{{sub/a.txt}}
1509
} {
1510
write_file page.txt "GET $urlPage HTTP/1.0\r\n\r\n"
1511
fossil_raw http --in page.txt --ipaddr 127.0.0.1 $urlRepo --localauth
1512
set urlMissing [list]
1513
if {[string first "HTTP/1.0 200 OK" $RESULT] != 0} {
1514
lappend urlMissing "status 200"
1515
}
1516
foreach urlLink $urlLinks {
1517
if {![regexp -- $urlLink $RESULT]} {lappend urlMissing $urlLink}
1518
}
1519
test_eq url-links-$urlName $urlMissing {}
1520
}
1521
1522
###############################################################################
1523
# The ssh: transport.
1524
1525
if {$is_windows} {
1526
test_skip_part "the stand-in for ssh is a shell script"
1527
} else {
1528
set urlSshDir [file join $urlDir ssh-standin]
1529
file mkdir $urlSshDir
1530
set urlSshStub [file join $urlSshDir ssh]
1531
write_file $urlSshStub {#!/bin/sh
1532
# Stands in for ssh. Appends each of its arguments to the file
1533
# $URL_SSH_DIR/args, on a line of its own inside <>. Run N prints the
1534
# file $URL_SSH_DIR/reply.N and then reads its input to the end. Where
1535
# there is no such file it exits with status 1 at once.
1536
for argument in "$@"; do
1537
printf '<%s>\n' "$argument"
1538
done >> "$URL_SSH_DIR/args"
1539
count=$(cat "$URL_SSH_DIR/count" 2>/dev/null || echo 0)
1540
count=$((count + 1))
1541
echo $count > "$URL_SSH_DIR/count"
1542
if test -f "$URL_SSH_DIR/reply.$count"; then
1543
cat "$URL_SSH_DIR/reply.$count"
1544
cat > /dev/null
1545
exit 0
1546
fi
1547
exit 1
1548
}
1549
file attributes $urlSshStub -permissions 0755
1550
set env(URL_SSH_DIR) $urlSshDir
1551
set urlSavedPath $env(PATH)
1552
set env(PATH) "$urlSshDir:$env(PATH)"
1553
}
1554
1555
# The stand-in must be what "ssh" means from here on. If it is not, run
1556
# nothing that could start the real one. Tcl remembers where it last
1557
# found a program, so forget that first.
1558
unset -nocomplain ::auto_execs(ssh)
1559
if {!$is_windows && [lindex [auto_execok ssh] 0] ne $urlSshStub} {
1560
set env(PATH) $urlSavedPath
1561
test_skip_part "cannot put a stand-in for ssh first on the PATH"
1562
} elseif {!$is_windows} {
1563
1564
# Runs fossil with the arguments ARGS. Run N of the stand-in prints
1565
# item N of REPLIES, where an empty item makes it exit at once. Sets
1566
# urlSshArgs to the arguments of each run.
1567
proc url_ssh {replies args} {
1568
global urlSshDir urlSshArgs
1569
file delete {*}[glob -nocomplain -directory $urlSshDir reply.* count args]
1570
set number 0
1571
foreach reply $replies {
1572
incr number
1573
if {$reply ne ""} {
1574
write_file [file join $urlSshDir reply.$number] $reply
1575
}
1576
}
1577
fossil_raw -seconds 60 {*}$args
1578
set urlSshArgs ""
1579
if {[file exists [file join $urlSshDir args]]} {
1580
set urlSshArgs [read_file [file join $urlSshDir args]]
1581
}
1582
}
1583
# Returns how many times the stand-in ran in the last url_ssh.
1584
proc url_ssh_runs {} {
1585
set file [file join $::urlSshDir count]
1586
return [expr {[file exists $file] ? [string trim [read_file $file]] : 0}]
1587
}
1588
1589
set urlSshOk [url_crlf \
1590
"Status: 200 OK\nContent-Type: text/plain\nContent-Length: 5\n\n"]hello
1591
1592
# The reply of "fossil test-http" on the server begins with a Status
1593
# line. The request says that it comes over ssh.
1594
#
1595
url_ssh [list $urlSshOk] test-httpmsg ssh://hh/dir/r.fossil --subpath /x
1596
test url-ssh-reply {$CODE == 0 && $RESULT eq "hello" &&
1597
[url_ssh_runs] == 1 &&
1598
[holds_strings $urlSshArgs \
1599
[list "<-->\n<hh>\n<fossil>\n<test-http>\n<dir/r.fossil>"]]}
1600
url_ssh [list [url_crlf "HTTP/1.1 200 OK\nContent-Length: 5\n\n"]hello] \
1601
test-httpmsg ssh://hh/dir/r.fossil --subpath /x
1602
test url-ssh-reply-http-status {$CODE == 0 && $RESULT eq "hello"}
1603
url_ssh [list [url_crlf "STATUS: 200\ncontent-length: 5\n\n"]hello] \
1604
test-httpmsg ssh://hh/dir/r.fossil?fossil=/opt/fossil --subpath /x
1605
test url-ssh-reply-upper-case-refused {$CODE == 1}
1606
url_ssh [list [url_crlf \
1607
"HTTP/1.1 200 OK\nStatus: 200 OK\nContent-Length: 5\n\n"]hello] \
1608
test-httpmsg ssh://hh/dir/r.fossil --subpath /x
1609
test url-ssh-reply-both-status-lines {$CODE == 0 && $RESULT eq "hello"}
1610
1611
foreach {urlName urlReply urlMessage} [list \
1612
status-500 "Status: 500 Server Error\n\n" {server says: 500 Server Error} \
1613
status-404 "Status: 404\n\n" {server says: 404} \
1614
status-tab "Status:\t500\n\n" {server says: \n} \
1615
status-no-code "Status: none\n\n" {Read: \[Status: none\]\s*$} \
1616
status-301-no-location "Status: 301 Moved\nContent-Length: 0\n\n" \
1617
{"location:" missing from 301 redirect reply} \
1618
status-302 "Status: 302 Found\nLocation: http://127.0.0.1:1/x\n\n" \
1619
{cannot redirect from ssh://hh/dir/r.fossil\?fossil=/opt/fossil to http://127.0.0.1:1/x} \
1620
status-307 "Status: 307 Temporary\nLocation: http://127.0.0.1:1/x\n\n" \
1621
{cannot redirect from ssh://hh/dir/r.fossil\?fossil=/opt/fossil to http://127.0.0.1:1/x} \
1622
status-308 "Status: 308 Permanent\nLocation: http://127.0.0.1:1/x\n\n" \
1623
{cannot redirect from ssh://hh/dir/r.fossil\?fossil=/opt/fossil to http://127.0.0.1:1/x} \
1624
] {
1625
url_ssh [list [url_crlf $urlReply]] \
1626
test-httpmsg ssh://hh/dir/r.fossil?fossil=/opt/fossil --subpath /x -v
1627
test_match url-ssh-refused-$urlName "$CODE $RESULT" \
1628
"^1 (?:.*\n)*.*$urlMessage"
1629
}
1630
url_ssh [list [url_crlf \
1631
"Status: 302 Found\nLocation: http://127.0.0.1:1/x\n\n"]] \
1632
test-httpmsg ssh://hh/dir/r.fossil --subpath /x
1633
test url-ssh-redirect-quiet {$CODE == 1 && $RESULT eq ""}
1634
1635
# A sync request over ssh has no login card.
1636
#
1637
url_ssh [list [url_crlf \
1638
[join [list \
1639
"Status: 200 OK\n" \
1640
"Content-Type: application/x-fossil-uncompressed\n" \
1641
"Content-Length: 2\n\n"] ""]]ok] \
1642
test-httpmsg ssh://bob:secret@hh/dir/r.fossil --subpath /x \
1643
payload.txt --xfer -v
1644
test url-ssh-no-login-card {$CODE == 0 &&
1645
[string first "13 byte payload" $RESULT] >= 0 &&
1646
[string first "<bob@hh>" $urlSshArgs] >= 0}
1647
1648
# When the server does not reply, fossil tries once more with a PATH=
1649
# argument before the name of the fossil program on the server. It
1650
# does not when the URL names that program.
1651
#
1652
url_ssh [list "" $urlSshOk] test-httpmsg ssh://hh/dir/r.fossil --subpath /x \
1653
--out out.txt
1654
test url-ssh-retry-with-path {$CODE == 0 && [url_ssh_runs] == 2 &&
1655
[read_file out.txt] eq "hello" &&
1656
[holds_strings $RESULT \
1657
{"First attempt to run fossil on hh using SSH failed"
1658
"Retrying with the PATH= argument"}] &&
1659
[regexp \
1660
{<hh>\n<fossil>\n<test-http>\n(?:.*\n)*<hh>\n<PATH=[^\n]*>\n<fossil>\n<test-http>} \
1661
$urlSshArgs]}
1662
url_ssh [list "" $urlSshOk] test-httpmsg ssh://hh/dir/r.fossil --subpath /x
1663
test url-ssh-retry-quiet {$CODE == 0 && [url_ssh_runs] == 2 &&
1664
$RESULT eq "hello"}
1665
url_ssh [list "" ""] test-httpmsg ssh://hh/dir/r.fossil --subpath /x \
1666
--out out.txt
1667
test url-ssh-retry-fails {$CODE == 1 && [url_ssh_runs] == 2 &&
1668
[string first "server did not reply: rerun with --sshtrace" \
1669
$RESULT] >= 0}
1670
url_ssh [list "" ""] test-httpmsg ssh://hh/dir/r.fossil --subpath /x
1671
test url-ssh-retry-fails-quiet {$CODE == 1 && [url_ssh_runs] == 2 &&
1672
$RESULT eq ""}
1673
url_ssh [list "" ""] test-httpmsg ssh://hh/dir/r.fossil --subpath /x \
1674
--out out.txt --sshtrace
1675
test url-ssh-retry-fails-sshtrace {$CODE == 1 && [url_ssh_runs] == 2 &&
1676
[regexp -line {^server did not reply$} $RESULT]}
1677
url_ssh [list "" $urlSshOk] \
1678
test-httpmsg ssh://hh/dir/r.fossil?fossil=/opt/fossil --subpath /x \
1679
--out out.txt
1680
test url-ssh-no-retry-with-fossil-parameter {$CODE == 1 &&
1681
[url_ssh_runs] == 1 && [string first "server did not reply" $RESULT] >= 0}
1682
1683
# A sync from an open check-out remembers that the host needed the
1684
# PATH= argument and uses it at once the next time. When that fails
1685
# and the attempt without it works, the host is forgotten again.
1686
#
1687
set urlSshPull [string map {"HTTP/1.1 200 OK" "Status: 200 OK"} $urlPullOk]
1688
fossil test-ssh-needs-path hh
1689
test_match url-ssh-path-unknown-host $RESULT {^hh +no$}
1690
url_ssh [list "" $urlSshPull] pull ssh://hh/dir/r.fossil --once
1691
test url-ssh-path-pull-retry {$CODE == 0 && [url_ssh_runs] == 2}
1692
fossil test-ssh-needs-path hh
1693
test_match url-ssh-path-remembered $RESULT {^hh +yes$}
1694
fossil test-ssh-needs-path
1695
test_match url-ssh-path-list $RESULT {^hh +yes$}
1696
url_ssh [list $urlSshPull] pull ssh://hh/dir/r.fossil --once
1697
test url-ssh-path-used {$CODE == 0 && [url_ssh_runs] == 1 &&
1698
[regexp {<hh>\n<PATH=[^\n]*>\n<fossil>} $urlSshArgs]}
1699
url_ssh [list "" $urlSshPull] pull ssh://hh/dir/r.fossil --once
1700
test url-ssh-path-pull-retry-without {$CODE == 0 && [url_ssh_runs] == 2 &&
1701
[string first "Retrying without the PATH= argument" $RESULT] >= 0}
1702
fossil test-ssh-needs-path hh
1703
test_match url-ssh-path-forgotten $RESULT {^hh +no$}
1704
fossil test-ssh-needs-path
1705
test_eq url-ssh-path-list-empty $RESULT ""
1706
url_ssh [list "" ""] pull ssh://hh/dir/r.fossil --once
1707
test url-ssh-path-pull-fails {$CODE == 1 && [url_ssh_runs] == 2}
1708
fossil test-ssh-needs-path hh
1709
test_match url-ssh-path-not-remembered-on-failure $RESULT {^hh +no$}
1710
1711
# test-ssh-needs-path HOST BOOLEAN sets the answer for the host.
1712
#
1713
fossil test-ssh-needs-path other.example yes
1714
test_match url-ssh-path-set $RESULT {^other.example +yes$}
1715
fossil test-ssh-needs-path other.example
1716
test_match url-ssh-path-set-read $RESULT {^other.example +yes$}
1717
fossil test-ssh-needs-path other.example no
1718
test_match url-ssh-path-clear $RESULT {^other.example +no$}
1719
fossil test-ssh-needs-path other.example
1720
test_match url-ssh-path-clear-read $RESULT {^other.example +no$}
1721
1722
# A proxy is not used for an ssh: URL, and a proxy that is an ssh: URL
1723
# does not make fossil run ssh.
1724
#
1725
url_ssh [list $urlSshPull] pull ssh://hh/dir/r.fossil --once \
1726
--proxy http://127.0.0.1:1
1727
test url-ssh-proxy-not-used {$CODE == 0 && [url_ssh_runs] == 1 &&
1728
[string first "via proxy" $RESULT] < 0}
1729
url_ssh [list] pull http://example.invalid/repo --once --proxy ssh://hh/x
1730
test url-ssh-proxy-is-ssh-url {[url_ssh_runs] == 0}
1731
1732
set env(PATH) $urlSavedPath
1733
unset env(URL_SSH_DIR)
1734
}
1735
1736
###############################################################################
1737
1738
test_cleanup
1739

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button