Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
9f9fbf2e
Commit
9f9fbf2e
authored
Mar 13, 2016
by
Abiola Ibrahim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support for case insensitive paths using CASE_SENSITIVE_PATH environment variable.
parent
63e4352d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
108 additions
and
10 deletions
+108
-10
middleware/fastcgi/fastcgi.go
middleware/fastcgi/fastcgi.go
+19
-8
middleware/path.go
middleware/path.go
+31
-2
middleware/path_test.go
middleware/path_test.go
+58
-0
No files found.
middleware/fastcgi/fastcgi.go
View file @
9f9fbf2e
...
@@ -45,17 +45,18 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
...
@@ -45,17 +45,18 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
// but we also want to be flexible for the script we proxy to.
// but we also want to be flexible for the script we proxy to.
fpath
:=
r
.
URL
.
Path
fpath
:=
r
.
URL
.
Path
if
idx
,
ok
:=
middleware
.
IndexFile
(
h
.
FileSys
,
fpath
,
rule
.
IndexFiles
);
ok
{
if
idx
,
ok
:=
middleware
.
IndexFile
(
h
.
FileSys
,
fpath
,
rule
.
IndexFiles
);
ok
{
fpath
=
idx
fpath
=
idx
// Index file present.
// Index file present.
// If request path cannot be split, return error.
// If request path cannot be split, return error.
if
!
h
.
canSplit
(
fpath
,
rule
)
{
if
!
rule
.
canSplit
(
fpath
)
{
return
http
.
StatusInternalServerError
,
ErrIndexMissingSplit
return
http
.
StatusInternalServerError
,
ErrIndexMissingSplit
}
}
}
else
{
}
else
{
// No index file present.
// No index file present.
// If request path cannot be split, ignore request.
// If request path cannot be split, ignore request.
if
!
h
.
canSplit
(
fpath
,
rule
)
{
if
!
rule
.
canSplit
(
fpath
)
{
continue
continue
}
}
}
}
...
@@ -165,10 +166,6 @@ func (h Handler) exists(path string) bool {
...
@@ -165,10 +166,6 @@ func (h Handler) exists(path string) bool {
return
false
return
false
}
}
func
(
h
Handler
)
canSplit
(
path
string
,
rule
Rule
)
bool
{
return
strings
.
Contains
(
path
,
rule
.
SplitPath
)
}
// buildEnv returns a set of CGI environment variables for the request.
// buildEnv returns a set of CGI environment variables for the request.
func
(
h
Handler
)
buildEnv
(
r
*
http
.
Request
,
rule
Rule
,
fpath
string
)
(
map
[
string
]
string
,
error
)
{
func
(
h
Handler
)
buildEnv
(
r
*
http
.
Request
,
rule
Rule
,
fpath
string
)
(
map
[
string
]
string
,
error
)
{
var
env
map
[
string
]
string
var
env
map
[
string
]
string
...
@@ -186,8 +183,8 @@ func (h Handler) buildEnv(r *http.Request, rule Rule, fpath string) (map[string]
...
@@ -186,8 +183,8 @@ func (h Handler) buildEnv(r *http.Request, rule Rule, fpath string) (map[string]
}
}
// Split path in preparation for env variables.
// Split path in preparation for env variables.
// Previous
h
.canSplit checks ensure this can never be -1.
// Previous
rule
.canSplit checks ensure this can never be -1.
splitPos
:=
strings
.
Index
(
fpath
,
rule
.
SplitP
ath
)
splitPos
:=
rule
.
splitPos
(
fp
ath
)
// Request has the extension; path was split successfully
// Request has the extension; path was split successfully
docURI
:=
fpath
[
:
splitPos
+
len
(
rule
.
SplitPath
)]
docURI
:=
fpath
[
:
splitPos
+
len
(
rule
.
SplitPath
)]
...
@@ -292,6 +289,20 @@ type Rule struct {
...
@@ -292,6 +289,20 @@ type Rule struct {
EnvVars
[][
2
]
string
EnvVars
[][
2
]
string
}
}
// canSplit checks if path can split into two based on rule.SplitPath.
func
(
r
Rule
)
canSplit
(
path
string
)
bool
{
return
r
.
splitPos
(
path
)
>=
0
}
// splitPos returns the index where path should be split
// based on rule.SplitPath.
func
(
r
Rule
)
splitPos
(
path
string
)
int
{
if
middleware
.
CaseSensitivePath
{
return
strings
.
Index
(
path
,
r
.
SplitPath
)
}
return
strings
.
Index
(
strings
.
ToLower
(
path
),
strings
.
ToLower
(
r
.
SplitPath
))
}
var
(
var
(
headerNameReplacer
=
strings
.
NewReplacer
(
" "
,
"_"
,
"-"
,
"_"
)
headerNameReplacer
=
strings
.
NewReplacer
(
" "
,
"_"
,
"-"
,
"_"
)
// ErrIndexMissingSplit describes an index configuration error.
// ErrIndexMissingSplit describes an index configuration error.
...
...
middleware/path.go
View file @
9f9fbf2e
package
middleware
package
middleware
import
"strings"
import
(
"os"
"strings"
)
const
caseSensitivePathEnv
=
"CASE_SENSITIVE_PATH"
func
init
()
{
initCaseSettings
()
}
// CaseSensitivePath determines if paths should be case sensitive.
// This is configurable via CASE_SENSITIVE_PATH environment variable.
// It defaults to false.
var
CaseSensitivePath
=
true
// initCaseSettings loads case sensitivity config from environment variable.
//
// This could have been in init, but init cannot be called from tests.
func
initCaseSettings
()
{
switch
os
.
Getenv
(
caseSensitivePathEnv
)
{
case
"0"
,
"false"
:
CaseSensitivePath
=
false
default
:
CaseSensitivePath
=
true
}
}
// Path represents a URI path, maybe with pattern characters.
// Path represents a URI path, maybe with pattern characters.
type
Path
string
type
Path
string
...
@@ -11,5 +37,8 @@ type Path string
...
@@ -11,5 +37,8 @@ type Path string
// comparison; this method assures that paths can be
// comparison; this method assures that paths can be
// easily and consistently matched.
// easily and consistently matched.
func
(
p
Path
)
Matches
(
other
string
)
bool
{
func
(
p
Path
)
Matches
(
other
string
)
bool
{
return
strings
.
HasPrefix
(
string
(
p
),
other
)
if
CaseSensitivePath
{
return
strings
.
HasPrefix
(
string
(
p
),
other
)
}
return
strings
.
HasPrefix
(
strings
.
ToLower
(
string
(
p
)),
strings
.
ToLower
(
other
))
}
}
middleware/path_test.go
0 → 100644
View file @
9f9fbf2e
package
middleware
import
(
"os"
"testing"
)
func
TestPathCaseSensitivity
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
basePath
string
path
string
caseSensitive
bool
expected
bool
}{
{
"/"
,
"/file"
,
true
,
true
},
{
"/a"
,
"/file"
,
true
,
false
},
{
"/f"
,
"/file"
,
true
,
true
},
{
"/f"
,
"/File"
,
true
,
false
},
{
"/f"
,
"/File"
,
false
,
true
},
{
"/file"
,
"/file"
,
true
,
true
},
{
"/file"
,
"/file"
,
false
,
true
},
{
"/files"
,
"/file"
,
false
,
false
},
{
"/files"
,
"/file"
,
true
,
false
},
{
"/folder"
,
"/folder/file.txt"
,
true
,
true
},
{
"/folders"
,
"/folder/file.txt"
,
true
,
false
},
{
"/folder"
,
"/Folder/file.txt"
,
false
,
true
},
{
"/folders"
,
"/Folder/file.txt"
,
false
,
false
},
}
for
i
,
test
:=
range
tests
{
CaseSensitivePath
=
test
.
caseSensitive
valid
:=
Path
(
test
.
path
)
.
Matches
(
test
.
basePath
)
if
test
.
expected
!=
valid
{
t
.
Errorf
(
"Test %d: Expected %v, found %v"
,
i
,
test
.
expected
,
valid
)
}
}
}
func
TestPathCaseSensitiveEnv
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
envValue
string
expected
bool
}{
{
"1"
,
true
},
{
"0"
,
false
},
{
"false"
,
false
},
{
"true"
,
true
},
{
""
,
true
},
}
for
i
,
test
:=
range
tests
{
os
.
Setenv
(
caseSensitivePathEnv
,
test
.
envValue
)
initCaseSettings
()
if
test
.
expected
!=
CaseSensitivePath
{
t
.
Errorf
(
"Test %d: Expected %v, found %v"
,
i
,
test
.
expected
,
CaseSensitivePath
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment