Skip to content

Commit 0335a60

Browse files
committed
refactor(serverHandler): extract alias relation detection
1 parent 7774846 commit 0335a60

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

src/serverHandler/alias.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package serverHandler
22

3+
import (
4+
"../util"
5+
"path"
6+
)
7+
38
type alias struct {
49
urlPath string
510
fsPath string
@@ -15,3 +20,11 @@ func (aliases aliases) byUrlPath(urlPath string) (alias *alias, ok bool) {
1520
}
1621
return nil, false
1722
}
23+
24+
func (alias alias) isChildOf(rawReqPath string) bool {
25+
return len(alias.urlPath) > len(rawReqPath) && path.Dir(alias.urlPath) == rawReqPath
26+
}
27+
28+
func (alias alias) isSuccessorOf(rawReqPath string) bool {
29+
return len(alias.urlPath) > len(rawReqPath) && util.HasUrlPrefixDir(alias.urlPath, rawReqPath)
30+
}

src/serverHandler/archive.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,13 @@ func (h *handler) visitFs(
7171
if fInfo.IsDir() {
7272
childAliases := map[string]string{}
7373
for _, alias := range h.aliases {
74-
aliasUrlPath := alias.urlPath
75-
aliasFsPath := alias.fsPath
76-
77-
if aliasUrlPath != rawRequestPath && path.Dir(aliasUrlPath) == rawRequestPath {
78-
childAliases[aliasUrlPath] = aliasFsPath
74+
if alias.isChildOf(rawRequestPath) {
75+
childAliases[alias.urlPath] = alias.fsPath
7976
continue
8077
}
8178

82-
if len(aliasUrlPath) > len(rawRequestPath) && util.HasUrlPrefixDir(aliasUrlPath, rawRequestPath) {
83-
succPath := aliasUrlPath[len(rawRequestPath):]
79+
if alias.isSuccessorOf(rawRequestPath) {
80+
succPath := alias.urlPath[len(rawRequestPath):]
8481
if succPath[0] == '/' {
8582
succPath = succPath[1:]
8683
}

test/case/015.alias.archive.bash

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
cleanup() {
4+
rm -f "$fs"/downloaded/*.tmp
5+
}
6+
7+
source "$root"/lib.bash
8+
9+
"$ghfs" -l 3003 -r "$fs"/vhost2 --archive / -a :/go:"$fs"/vhost1/go -a :/hello/world:"$fs"/vhost1/world -E '' &
10+
sleep 0.05 # wait server ready
11+
cleanup
12+
13+
archive="$fs"/downloaded/archive.tar.tmp
14+
curl_get_body 'http://127.0.0.1:3003/?tar' > "$archive"
15+
(tar -tf "$archive" | grep -q 'go/index.txt') || fail "go/index.txt not in $(basename $archive)"
16+
(tar -tf "$archive" | grep -q 'hello/world/index.txt') || fail "hello/world/index.txt not in $(basename $archive)"
17+
18+
cleanup
19+
kill %1

0 commit comments

Comments
 (0)