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
Łukasz Nowak
caddy
Commits
981ca72e
Commit
981ca72e
authored
Apr 18, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enforce canonical URLs
parent
6a32de4b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
0 deletions
+26
-0
server/fileserver.go
server/fileserver.go
+26
-0
No files found.
server/fileserver.go
View file @
981ca72e
...
@@ -56,6 +56,22 @@ func (fh *fileHandler) serveFile(w http.ResponseWriter, r *http.Request, name st
...
@@ -56,6 +56,22 @@ func (fh *fileHandler) serveFile(w http.ResponseWriter, r *http.Request, name st
return
http
.
StatusNotFound
,
nil
return
http
.
StatusNotFound
,
nil
}
}
// redirect to canonical path
url
:=
r
.
URL
.
Path
if
d
.
IsDir
()
{
// Ensure / at end of directory url
if
url
[
len
(
url
)
-
1
]
!=
'/'
{
redirect
(
w
,
r
,
path
.
Base
(
url
)
+
"/"
)
return
http
.
StatusMovedPermanently
,
nil
}
}
else
{
// Ensure no / at end of file url
if
url
[
len
(
url
)
-
1
]
==
'/'
{
redirect
(
w
,
r
,
"../"
+
path
.
Base
(
url
))
return
http
.
StatusMovedPermanently
,
nil
}
}
// use contents of an index file, if present, for directory
// use contents of an index file, if present, for directory
if
d
.
IsDir
()
{
if
d
.
IsDir
()
{
for
_
,
indexPage
:=
range
browse
.
IndexPages
{
for
_
,
indexPage
:=
range
browse
.
IndexPages
{
...
@@ -94,3 +110,13 @@ func (fh *fileHandler) serveFile(w http.ResponseWriter, r *http.Request, name st
...
@@ -94,3 +110,13 @@ func (fh *fileHandler) serveFile(w http.ResponseWriter, r *http.Request, name st
return
http
.
StatusOK
,
nil
return
http
.
StatusOK
,
nil
}
}
// redirect is taken from http.localRedirect of the std lib. It
// sends an HTTP redirect to the client but will preserve the
// query string for the new path.
func
redirect
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
newPath
string
)
{
if
q
:=
r
.
URL
.
RawQuery
;
q
!=
""
{
newPath
+=
"?"
+
q
}
http
.
Redirect
(
w
,
r
,
newPath
,
http
.
StatusMovedPermanently
)
}
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