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
843f6e83
Commit
843f6e83
authored
Mar 28, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored browse middleware to return errors
parent
058ff948
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
16 deletions
+14
-16
middleware/browse/browse.go
middleware/browse/browse.go
+14
-16
No files found.
middleware/browse/browse.go
View file @
843f6e83
...
@@ -20,7 +20,7 @@ import (
...
@@ -20,7 +20,7 @@ import (
// Browse is an http.Handler that can show a file listing when
// Browse is an http.Handler that can show a file listing when
// directories in the given paths are specified.
// directories in the given paths are specified.
type
Browse
struct
{
type
Browse
struct
{
Next
http
.
HandlerFunc
Next
middleware
.
HandlerFunc
Root
string
Root
string
Configs
[]
BrowseConfig
Configs
[]
BrowseConfig
}
}
...
@@ -83,26 +83,23 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
...
@@ -83,26 +83,23 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
Configs
:
configs
,
Configs
:
configs
,
}
}
return
func
(
next
http
.
HandlerFunc
)
http
.
HandlerFunc
{
return
func
(
next
middleware
.
HandlerFunc
)
middleware
.
HandlerFunc
{
browse
.
Next
=
next
browse
.
Next
=
next
return
browse
.
ServeHTTP
return
browse
.
ServeHTTP
},
nil
},
nil
}
}
// ServeHTTP implements the
http
.Handler interface.
// ServeHTTP implements the
middleware
.Handler interface.
func
(
b
Browse
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
b
Browse
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
filename
:=
b
.
Root
+
r
.
URL
.
Path
filename
:=
b
.
Root
+
r
.
URL
.
Path
info
,
err
:=
os
.
Stat
(
filename
)
info
,
err
:=
os
.
Stat
(
filename
)
if
err
!=
nil
{
if
err
!=
nil
{
// TODO: 404 Not Found
return
b
.
Next
(
w
,
r
)
b
.
Next
(
w
,
r
)
return
}
}
if
!
info
.
IsDir
()
{
if
!
info
.
IsDir
()
{
b
.
Next
(
w
,
r
)
return
b
.
Next
(
w
,
r
)
return
}
}
// See if there's a browse configuration to match the path
// See if there's a browse configuration to match the path
...
@@ -115,21 +112,21 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
...
@@ -115,21 +112,21 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// that doesn't end in "/" (which it should, anyway)
// that doesn't end in "/" (which it should, anyway)
if
r
.
URL
.
Path
[
len
(
r
.
URL
.
Path
)
-
1
]
!=
'/'
{
if
r
.
URL
.
Path
[
len
(
r
.
URL
.
Path
)
-
1
]
!=
'/'
{
http
.
Redirect
(
w
,
r
,
r
.
URL
.
Path
+
"/"
,
http
.
StatusTemporaryRedirect
)
http
.
Redirect
(
w
,
r
,
r
.
URL
.
Path
+
"/"
,
http
.
StatusTemporaryRedirect
)
return
return
0
,
nil
}
}
// Load directory contents
// Load directory contents
file
,
err
:=
os
.
Open
(
b
.
Root
+
r
.
URL
.
Path
)
file
,
err
:=
os
.
Open
(
b
.
Root
+
r
.
URL
.
Path
)
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
err
)
// TODO
return
http
.
StatusForbidden
,
err
}
}
defer
file
.
Close
()
defer
file
.
Close
()
w
.
Header
()
.
Set
(
"Content-Type"
,
"text/html; charset=utf-8"
)
w
.
Header
()
.
Set
(
"Content-Type"
,
"text/html; charset=utf-8"
)
files
,
err
:=
file
.
Readdir
(
-
1
)
files
,
err
:=
file
.
Readdir
(
-
1
)
if
err
!=
nil
||
len
(
files
)
==
0
{
if
err
!=
nil
{
// TODO - second condition may not be necessary? See docs...
return
http
.
StatusForbidden
,
err
}
}
// Assemble listing of directory contents
// Assemble listing of directory contents
...
@@ -185,16 +182,17 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
...
@@ -185,16 +182,17 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Items
:
fileinfos
,
Items
:
fileinfos
,
}
}
// TODO: Don't write to w until we know there wasn't an error
err
=
bc
.
Template
.
Execute
(
w
,
listing
)
err
=
bc
.
Template
.
Execute
(
w
,
listing
)
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
err
)
// TODO
return
http
.
StatusInternalServerError
,
err
}
}
return
return
http
.
StatusOK
,
nil
}
}
// Didn't qualify; pass-thru
// Didn't qualify; pass-thru
b
.
Next
(
w
,
r
)
return
b
.
Next
(
w
,
r
)
}
}
// parse returns a list of browsing configurations
// parse returns a list of browsing configurations
...
...
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