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
c3417a07
Commit
c3417a07
authored
Apr 19, 2016
by
Matt Holt
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #772 from mholt/fix-browse
Make Browse Great Again ★★★
parents
da016f8d
72bc6932
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
213 additions
and
139 deletions
+213
-139
caddy/setup/browse.go
caddy/setup/browse.go
+67
-36
middleware/browse/browse.go
middleware/browse/browse.go
+140
-97
middleware/browse/browse_test.go
middleware/browse/browse_test.go
+6
-6
No files found.
caddy/setup/browse.go
View file @
c3417a07
...
@@ -3,6 +3,7 @@ package setup
...
@@ -3,6 +3,7 @@ package setup
import
(
import
(
"fmt"
"fmt"
"io/ioutil"
"io/ioutil"
"net/http"
"text/template"
"text/template"
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware"
...
@@ -17,7 +18,6 @@ func Browse(c *Controller) (middleware.Middleware, error) {
...
@@ -17,7 +18,6 @@ func Browse(c *Controller) (middleware.Middleware, error) {
}
}
browse
:=
browse
.
Browse
{
browse
:=
browse
.
Browse
{
Root
:
c
.
Root
,
Configs
:
configs
,
Configs
:
configs
,
IgnoreIndexes
:
false
,
IgnoreIndexes
:
false
,
}
}
...
@@ -50,6 +50,16 @@ func browseParse(c *Controller) ([]browse.Config, error) {
...
@@ -50,6 +50,16 @@ func browseParse(c *Controller) ([]browse.Config, error) {
}
else
{
}
else
{
bc
.
PathScope
=
"/"
bc
.
PathScope
=
"/"
}
}
bc
.
Root
=
http
.
Dir
(
c
.
Root
)
theRoot
,
err
:=
bc
.
Root
.
Open
(
"/"
)
// catch a missing path early
if
err
!=
nil
{
return
configs
,
err
}
defer
theRoot
.
Close
()
_
,
err
=
theRoot
.
Readdir
(
-
1
)
if
err
!=
nil
{
return
configs
,
err
}
// Second argument would be the template file to use
// Second argument would be the template file to use
var
tplText
string
var
tplText
string
...
@@ -85,7 +95,6 @@ const defaultTemplate = `<!DOCTYPE html>
...
@@ -85,7 +95,6 @@ const defaultTemplate = `<!DOCTYPE html>
<html>
<html>
<head>
<head>
<title>{{.Name}}</title>
<title>{{.Name}}</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
<style>
* { padding: 0; margin: 0; }
* { padding: 0; margin: 0; }
...
@@ -106,7 +115,7 @@ h1 a:hover {
...
@@ -106,7 +115,7 @@ h1 a:hover {
}
}
header,
header,
.content
{
#summary
{
padding-left: 5%;
padding-left: 5%;
padding-right: 5%;
padding-right: 5%;
}
}
...
@@ -306,43 +315,49 @@ footer {
...
@@ -306,43 +315,49 @@ footer {
</header>
</header>
<main>
<main>
<div class="meta">
<div class="meta">
<div
class="content
">
<div
id="summary
">
<span class="meta-item"><b>{{.NumDirs}}</b> director{{if eq 1 .NumDirs}}y{{else}}ies{{end}}</span>
<span class="meta-item"><b>{{.NumDirs}}</b> director{{if eq 1 .NumDirs}}y{{else}}ies{{end}}</span>
<span class="meta-item"><b>{{.NumFiles}}</b> file{{if ne 1 .NumFiles}}s{{end}}</span>
<span class="meta-item"><b>{{.NumFiles}}</b> file{{if ne 1 .NumFiles}}s{{end}}</span>
{{- if ne 0 .ItemsLimitedTo}}
<span class="meta-item">(of which only <b>{{.ItemsLimitedTo}}</b> are displayed)</span>
{{- end}}
</div>
</div>
</div>
</div>
<div class="listing">
<div class="listing">
<table>
<table aria-describedby="summary">
<thead>
<tr>
<tr>
<th>
<th>
{{if and (eq .Sort "name") (ne .Order "desc")}}
{{
-
if and (eq .Sort "name") (ne .Order "desc")}}
<a href="?sort=name&order=desc">Name <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#up-arrow"></use></svg></a>
<a href="?sort=name&order=desc
{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}
">Name <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#up-arrow"></use></svg></a>
{{else if and (eq .Sort "name") (ne .Order "asc")}}
{{
-
else if and (eq .Sort "name") (ne .Order "asc")}}
<a href="?sort=name&order=asc">Name <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#down-arrow"></use></svg></a>
<a href="?sort=name&order=asc
{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}
">Name <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#down-arrow"></use></svg></a>
{{else}}
{{
-
else}}
<a href="?sort=name&order=asc">Name</a>
<a href="?sort=name&order=asc
{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}
">Name</a>
{{end}}
{{
-
end}}
</th>
</th>
<th>
<th>
{{if and (eq .Sort "size") (ne .Order "desc")}}
{{
-
if and (eq .Sort "size") (ne .Order "desc")}}
<a href="?sort=size&order=desc">Size <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#up-arrow"></use></svg></a></a>
<a href="?sort=size&order=desc
{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}
">Size <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#up-arrow"></use></svg></a></a>
{{else if and (eq .Sort "size") (ne .Order "asc")}}
{{
-
else if and (eq .Sort "size") (ne .Order "asc")}}
<a href="?sort=size&order=asc">Size <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#down-arrow"></use></svg></a></a>
<a href="?sort=size&order=asc
{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}
">Size <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#down-arrow"></use></svg></a></a>
{{else}}
{{
-
else}}
<a href="?sort=size&order=asc">Size</a>
<a href="?sort=size&order=asc
{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}
">Size</a>
{{end}}
{{
-
end}}
</th>
</th>
<th class="hideable">
<th class="hideable">
{{if and (eq .Sort "time") (ne .Order "desc")}}
{{
-
if and (eq .Sort "time") (ne .Order "desc")}}
<a href="?sort=time&order=desc">Modified <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#up-arrow"></use></svg></a></a>
<a href="?sort=time&order=desc
{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}
">Modified <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#up-arrow"></use></svg></a></a>
{{else if and (eq .Sort "time") (ne .Order "asc")}}
{{
-
else if and (eq .Sort "time") (ne .Order "asc")}}
<a href="?sort=time&order=asc">Modified <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#down-arrow"></use></svg></a></a>
<a href="?sort=time&order=asc
{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}
">Modified <svg width="1em" height=".4em" version="1.1" viewBox="0 0 12.922194 6.0358899"><use xlink:href="#down-arrow"></use></svg></a></a>
{{else}}
{{
-
else}}
<a href="?sort=time&order=asc">Modified</a>
<a href="?sort=time&order=asc
{{if ne 0 .ItemsLimitedTo}}&limit={{.ItemsLimitedTo}}{{end}}
">Modified</a>
{{end}}
{{
-
end}}
</th>
</th>
</tr>
</tr>
{{if .CanGoUp}}
</thead>
<tbody>
{{- if .CanGoUp}}
<tr>
<tr>
<td>
<td>
<a href="..">
<a href="..">
...
@@ -350,30 +365,46 @@ footer {
...
@@ -350,30 +365,46 @@ footer {
</a>
</a>
</td>
</td>
<td>—</td>
<td>—</td>
<td>—</td>
<td
class="hideable"
>—</td>
</tr>
</tr>
{{end}}
{{
-
end}}
{{range .Items}}
{{
-
range .Items}}
<tr>
<tr>
<td>
<td>
<a href="{{.URL}}">
<a href="{{.URL}}">
{{if .IsDir}}
{{
-
if .IsDir}}
<svg width="1.5em" height="1em" version="1.1" viewBox="0 0 35.678803 28.527945"><use xlink:href="#folder"></use></svg>
<svg width="1.5em" height="1em" version="1.1" viewBox="0 0 35.678803 28.527945"><use xlink:href="#folder"></use></svg>
{{else}}
{{
-
else}}
<svg width="1.5em" height="1em" version="1.1" viewBox="0 0 26.604381 29.144726"><use xlink:href="#file"></use></svg>
<svg width="1.5em" height="1em" version="1.1" viewBox="0 0 26.604381 29.144726"><use xlink:href="#file"></use></svg>
{{end}}
{{
-
end}}
<span class="name">{{.Name}}</span>
<span class="name">{{.Name}}</span>
</a>
</a>
</td>
</td>
<td>{{.HumanSize}}</td>
{{- if .IsDir}}
<td class="hideable">{{.HumanModTime "01/02/2006 03:04:05 PM"}}</td>
<td data-order="-1">—</td>
{{- else}}
<td data-order="{{.Size}}">{{.HumanSize}}</td>
{{- end}}
<td class="hideable"><time datetime="{{.HumanModTime "2006-01-02 15:04:05-0700"}}">{{.HumanModTime "01/02/2006 03:04:05 PM"}}</time></td>
</tr>
</tr>
{{end}}
{{- end}}
</tbody>
</table>
</table>
</div>
</div>
</main>
</main>
<footer>
<footer>
Served with <a href="https://caddyserver.com">Caddy</a>
Served with <a href="https://caddyserver.com">Caddy</a>
</footer>
</footer>
<script type="text/javascript">
function localizeDatetime(e, index, ar) {
if (e.textContent === undefined) {
return;
}
var d = new Date(e.getAttribute('datetime'));
e.textContent = d.toLocaleString();
}
var timeList = Array.prototype.slice.call(document.getElementsByTagName("time"));
timeList.forEach(localizeDatetime);
</script>
</body>
</body>
</html>`
</html>`
middleware/browse/browse.go
View file @
c3417a07
...
@@ -5,12 +5,10 @@ package browse
...
@@ -5,12 +5,10 @@ package browse
import
(
import
(
"bytes"
"bytes"
"encoding/json"
"encoding/json"
"errors"
"net/http"
"net/http"
"net/url"
"net/url"
"os"
"os"
"path"
"path"
"path/filepath"
"sort"
"sort"
"strconv"
"strconv"
"strings"
"strings"
...
@@ -25,7 +23,6 @@ import (
...
@@ -25,7 +23,6 @@ import (
// directories in the given paths are specified.
// directories in the given paths are specified.
type
Browse
struct
{
type
Browse
struct
{
Next
middleware
.
Handler
Next
middleware
.
Handler
Root
string
Configs
[]
Config
Configs
[]
Config
IgnoreIndexes
bool
IgnoreIndexes
bool
}
}
...
@@ -33,6 +30,7 @@ type Browse struct {
...
@@ -33,6 +30,7 @@ type Browse struct {
// Config is a configuration for browsing in a particular path.
// Config is a configuration for browsing in a particular path.
type
Config
struct
{
type
Config
struct
{
PathScope
string
PathScope
string
Root
http
.
FileSystem
Variables
interface
{}
Variables
interface
{}
Template
*
template
.
Template
Template
*
template
.
Template
}
}
...
@@ -63,6 +61,9 @@ type Listing struct {
...
@@ -63,6 +61,9 @@ type Listing struct {
// And which order
// And which order
Order
string
Order
string
// If ≠0 then Items have been limited to that many elements
ItemsLimitedTo
int
// Optional custom variables for use in browse templates
// Optional custom variables for use in browse templates
User
interface
{}
User
interface
{}
...
@@ -133,9 +134,20 @@ func (l byName) Less(i, j int) bool {
...
@@ -133,9 +134,20 @@ func (l byName) Less(i, j int) bool {
}
}
// By Size
// By Size
func
(
l
bySize
)
Len
()
int
{
return
len
(
l
.
Items
)
}
func
(
l
bySize
)
Len
()
int
{
return
len
(
l
.
Items
)
}
func
(
l
bySize
)
Swap
(
i
,
j
int
)
{
l
.
Items
[
i
],
l
.
Items
[
j
]
=
l
.
Items
[
j
],
l
.
Items
[
i
]
}
func
(
l
bySize
)
Swap
(
i
,
j
int
)
{
l
.
Items
[
i
],
l
.
Items
[
j
]
=
l
.
Items
[
j
],
l
.
Items
[
i
]
}
func
(
l
bySize
)
Less
(
i
,
j
int
)
bool
{
return
l
.
Items
[
i
]
.
Size
<
l
.
Items
[
j
]
.
Size
}
const
directoryOffset
=
-
1
<<
31
// = math.MinInt32
func
(
l
bySize
)
Less
(
i
,
j
int
)
bool
{
iSize
,
jSize
:=
l
.
Items
[
i
]
.
Size
,
l
.
Items
[
j
]
.
Size
if
l
.
Items
[
i
]
.
IsDir
{
iSize
=
directoryOffset
+
iSize
}
if
l
.
Items
[
j
]
.
IsDir
{
jSize
=
directoryOffset
+
jSize
}
return
iSize
<
jSize
}
// By Time
// By Time
func
(
l
byTime
)
Len
()
int
{
return
len
(
l
.
Items
)
}
func
(
l
byTime
)
Len
()
int
{
return
len
(
l
.
Items
)
}
...
@@ -173,22 +185,20 @@ func (l Listing) applySort() {
...
@@ -173,22 +185,20 @@ func (l Listing) applySort() {
}
}
}
}
func
directoryListing
(
files
[]
os
.
FileInfo
,
r
*
http
.
Request
,
canGoUp
bool
,
root
string
,
ignoreIndexes
bool
,
vars
interface
{})
(
Listing
,
error
)
{
func
directoryListing
(
files
[]
os
.
FileInfo
,
canGoUp
bool
,
urlPath
string
)
(
Listing
,
bool
)
{
var
(
var
(
fileinfos
[]
FileInfo
fileinfos
[]
FileInfo
dirCount
,
fileCount
int
dirCount
,
fileCount
int
urlPath
=
r
.
URL
.
Path
hasIndexFile
bool
)
)
for
_
,
f
:=
range
files
{
for
_
,
f
:=
range
files
{
name
:=
f
.
Name
()
name
:=
f
.
Name
()
// Directory is not browsable if it contains index file
for
_
,
indexName
:=
range
middleware
.
IndexPages
{
if
!
ignoreIndexes
{
if
name
==
indexName
{
for
_
,
indexName
:=
range
middleware
.
IndexPages
{
hasIndexFile
=
true
if
name
==
indexName
{
break
return
Listing
{},
errors
.
New
(
"Directory contains index file, not browsable!"
)
}
}
}
}
}
...
@@ -206,7 +216,7 @@ func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root s
...
@@ -206,7 +216,7 @@ func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root s
Name
:
f
.
Name
(),
Name
:
f
.
Name
(),
Size
:
f
.
Size
(),
Size
:
f
.
Size
(),
URL
:
url
.
String
(),
URL
:
url
.
String
(),
ModTime
:
f
.
ModTime
(),
ModTime
:
f
.
ModTime
()
.
UTC
()
,
Mode
:
f
.
Mode
(),
Mode
:
f
.
Mode
(),
})
})
}
}
...
@@ -218,16 +228,11 @@ func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root s
...
@@ -218,16 +228,11 @@ func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root s
Items
:
fileinfos
,
Items
:
fileinfos
,
NumDirs
:
dirCount
,
NumDirs
:
dirCount
,
NumFiles
:
fileCount
,
NumFiles
:
fileCount
,
Context
:
middleware
.
Context
{
},
hasIndexFile
Root
:
http
.
Dir
(
root
),
Req
:
r
,
URL
:
r
.
URL
,
},
User
:
vars
,
},
nil
}
}
// ServeHTTP implements the middleware.Handler interface.
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
// If so, control is handed over to ServeListing.
func
(
b
Browse
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
func
(
b
Browse
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
var
bc
*
Config
var
bc
*
Config
// See if there's a browse configuration to match the path
// See if there's a browse configuration to match the path
...
@@ -241,8 +246,7 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
...
@@ -241,8 +246,7 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
inScope
:
inScope
:
// Browse works on existing directories; delegate everything else
// Browse works on existing directories; delegate everything else
requestedFilepath
:=
filepath
.
Join
(
b
.
Root
,
r
.
URL
.
Path
)
requestedFilepath
,
err
:=
bc
.
Root
.
Open
(
r
.
URL
.
Path
)
info
,
err
:=
os
.
Stat
(
requestedFilepath
)
if
err
!=
nil
{
if
err
!=
nil
{
switch
{
switch
{
case
os
.
IsPermission
(
err
)
:
case
os
.
IsPermission
(
err
)
:
...
@@ -253,6 +257,19 @@ inScope:
...
@@ -253,6 +257,19 @@ inScope:
return
b
.
Next
.
ServeHTTP
(
w
,
r
)
return
b
.
Next
.
ServeHTTP
(
w
,
r
)
}
}
}
}
defer
requestedFilepath
.
Close
()
info
,
err
:=
requestedFilepath
.
Stat
()
if
err
!=
nil
{
switch
{
case
os
.
IsPermission
(
err
)
:
return
http
.
StatusForbidden
,
err
case
os
.
IsExist
(
err
)
:
return
http
.
StatusGone
,
err
default
:
return
b
.
Next
.
ServeHTTP
(
w
,
r
)
}
}
if
!
info
.
IsDir
()
{
if
!
info
.
IsDir
()
{
return
b
.
Next
.
ServeHTTP
(
w
,
r
)
return
b
.
Next
.
ServeHTTP
(
w
,
r
)
}
}
...
@@ -274,108 +291,117 @@ inScope:
...
@@ -274,108 +291,117 @@ inScope:
return
0
,
nil
return
0
,
nil
}
}
// Load directory contents
return
b
.
ServeListing
(
w
,
r
,
requestedFilepath
,
bc
)
file
,
err
:=
os
.
Open
(
requestedFilepath
)
}
if
err
!=
nil
{
switch
{
case
os
.
IsPermission
(
err
)
:
return
http
.
StatusForbidden
,
err
case
os
.
IsExist
(
err
)
:
return
http
.
StatusGone
,
err
default
:
return
http
.
StatusInternalServerError
,
err
}
}
defer
file
.
Close
()
files
,
err
:=
file
.
Readdir
(
-
1
)
func
(
b
Browse
)
loadDirectoryContents
(
requestedFilepath
http
.
File
,
urlPath
string
)
(
*
Listing
,
bool
,
error
)
{
files
,
err
:=
requestedFilepath
.
Readdir
(
-
1
)
if
err
!=
nil
{
if
err
!=
nil
{
switch
{
return
nil
,
false
,
err
case
os
.
IsPermission
(
err
)
:
return
http
.
StatusForbidden
,
err
case
os
.
IsExist
(
err
)
:
return
http
.
StatusGone
,
err
default
:
return
http
.
StatusInternalServerError
,
err
}
}
}
// Determine if user can browse up another folder
// Determine if user can browse up another folder
var
canGoUp
bool
var
canGoUp
bool
curPath
:=
strings
.
TrimSuffix
(
r
.
URL
.
Path
,
"/"
)
curPath
Dir
:=
path
.
Dir
(
strings
.
TrimSuffix
(
urlPath
,
"/"
)
)
for
_
,
other
:=
range
b
.
Configs
{
for
_
,
other
:=
range
b
.
Configs
{
if
strings
.
HasPrefix
(
path
.
Dir
(
curPath
)
,
other
.
PathScope
)
{
if
strings
.
HasPrefix
(
curPathDir
,
other
.
PathScope
)
{
canGoUp
=
true
canGoUp
=
true
break
break
}
}
}
}
// Assemble listing of directory contents
// Assemble listing of directory contents
listing
,
err
:=
directoryListing
(
files
,
r
,
canGoUp
,
b
.
Root
,
b
.
IgnoreIndexes
,
bc
.
Variables
)
listing
,
hasIndex
:=
directoryListing
(
files
,
canGoUp
,
urlPath
)
if
err
!=
nil
{
// directory isn't browsable
return
b
.
Next
.
ServeHTTP
(
w
,
r
)
}
// Copy the query values into the Listing struct
return
&
listing
,
hasIndex
,
nil
listing
.
Sort
,
listing
.
Order
=
r
.
URL
.
Query
()
.
Get
(
"sort"
),
r
.
URL
.
Query
()
.
Get
(
"order"
)
}
// handleSortOrder gets and stores for a Listing the 'sort' and 'order',
// and reads 'limit' if given. The latter is 0 if not given.
//
// This sets Cookies.
func
(
b
Browse
)
handleSortOrder
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
scope
string
)
(
sort
string
,
order
string
,
limit
int
,
err
error
)
{
sort
,
order
,
limitQuery
:=
r
.
URL
.
Query
()
.
Get
(
"sort"
),
r
.
URL
.
Query
()
.
Get
(
"order"
),
r
.
URL
.
Query
()
.
Get
(
"limit"
)
// If the query 'sort' or 'order' is empty, use defaults or any values previously saved in Cookies
// If the query 'sort' or 'order' is empty, use defaults or any values previously saved in Cookies
if
listing
.
Sort
==
""
{
switch
sort
{
listing
.
Sort
=
"name"
case
""
:
sort
=
"name"
if
sortCookie
,
sortErr
:=
r
.
Cookie
(
"sort"
);
sortErr
==
nil
{
if
sortCookie
,
sortErr
:=
r
.
Cookie
(
"sort"
);
sortErr
==
nil
{
listing
.
S
ort
=
sortCookie
.
Value
s
ort
=
sortCookie
.
Value
}
}
}
else
{
// Save the query value of 'sort' and 'order' as cookies.
case
"name"
,
"size"
,
"type"
:
http
.
SetCookie
(
w
,
&
http
.
Cookie
{
Name
:
"sort"
,
Value
:
listing
.
Sort
,
Path
:
bc
.
PathScope
,
Secure
:
r
.
TLS
!=
nil
})
http
.
SetCookie
(
w
,
&
http
.
Cookie
{
Name
:
"sort"
,
Value
:
sort
,
Path
:
scope
,
Secure
:
r
.
TLS
!=
nil
})
http
.
SetCookie
(
w
,
&
http
.
Cookie
{
Name
:
"order"
,
Value
:
listing
.
Order
,
Path
:
bc
.
PathScope
,
Secure
:
r
.
TLS
!=
nil
})
}
}
if
listing
.
Order
==
""
{
switch
order
{
listing
.
Order
=
"asc"
case
""
:
order
=
"asc"
if
orderCookie
,
orderErr
:=
r
.
Cookie
(
"order"
);
orderErr
==
nil
{
if
orderCookie
,
orderErr
:=
r
.
Cookie
(
"order"
);
orderErr
==
nil
{
listing
.
O
rder
=
orderCookie
.
Value
o
rder
=
orderCookie
.
Value
}
}
}
else
{
case
"asc"
,
"desc"
:
http
.
SetCookie
(
w
,
&
http
.
Cookie
{
Name
:
"order"
,
Value
:
listing
.
Order
,
Path
:
bc
.
PathS
cope
,
Secure
:
r
.
TLS
!=
nil
})
http
.
SetCookie
(
w
,
&
http
.
Cookie
{
Name
:
"order"
,
Value
:
order
,
Path
:
s
cope
,
Secure
:
r
.
TLS
!=
nil
})
}
}
listing
.
applySort
()
if
limitQuery
!=
""
{
limit
,
err
=
strconv
.
Atoi
(
limitQuery
)
if
err
!=
nil
{
// if the 'limit' query can't be interpreted as a number, return err
return
}
}
var
buf
bytes
.
Buffer
return
// Check if we should provide json
}
acceptHeader
:=
strings
.
Join
(
r
.
Header
[
"Accept"
],
","
)
if
strings
.
Contains
(
strings
.
ToLower
(
acceptHeader
),
"application/json"
)
{
// ServeListing returns a formatted view of 'requestedFilepath' contents'.
var
marsh
[]
byte
func
(
b
Browse
)
ServeListing
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
requestedFilepath
http
.
File
,
bc
*
Config
)
(
int
,
error
)
{
// Check if we are limited
listing
,
containsIndex
,
err
:=
b
.
loadDirectoryContents
(
requestedFilepath
,
r
.
URL
.
Path
)
if
limitQuery
:=
r
.
URL
.
Query
()
.
Get
(
"limit"
);
limitQuery
!=
""
{
if
err
!=
nil
{
limit
,
err
:=
strconv
.
Atoi
(
limitQuery
)
switch
{
if
err
!=
nil
{
// if the 'limit' query can't be interpreted as a number, return err
case
os
.
IsPermission
(
err
)
:
return
http
.
StatusBadRequest
,
err
return
http
.
StatusForbidden
,
err
}
case
os
.
IsExist
(
err
)
:
// if `limit` is equal or less than len(listing.Items) and bigger than 0, list them
return
http
.
StatusGone
,
err
if
limit
<=
len
(
listing
.
Items
)
&&
limit
>
0
{
default
:
marsh
,
err
=
json
.
Marshal
(
listing
.
Items
[
:
limit
])
return
http
.
StatusInternalServerError
,
err
}
else
{
// if the 'limit' query is empty, or has the wrong value, list everything
marsh
,
err
=
json
.
Marshal
(
listing
.
Items
)
}
if
err
!=
nil
{
return
http
.
StatusInternalServerError
,
err
}
}
else
{
// There's no 'limit' query; list them all
marsh
,
err
=
json
.
Marshal
(
listing
.
Items
)
if
err
!=
nil
{
return
http
.
StatusInternalServerError
,
err
}
}
}
}
if
containsIndex
&&
!
b
.
IgnoreIndexes
{
// directory isn't browsable
return
b
.
Next
.
ServeHTTP
(
w
,
r
)
}
listing
.
Context
=
middleware
.
Context
{
Root
:
bc
.
Root
,
Req
:
r
,
URL
:
r
.
URL
,
}
listing
.
User
=
bc
.
Variables
// Copy the query values into the Listing struct
var
limit
int
listing
.
Sort
,
listing
.
Order
,
limit
,
err
=
b
.
handleSortOrder
(
w
,
r
,
bc
.
PathScope
)
if
err
!=
nil
{
return
http
.
StatusBadRequest
,
err
}
listing
.
applySort
()
// Write the marshaled json to buf
if
limit
>
0
&&
limit
<=
len
(
listing
.
Items
)
{
if
_
,
err
=
buf
.
Write
(
marsh
);
err
!=
nil
{
listing
.
Items
=
listing
.
Items
[
:
limit
]
listing
.
ItemsLimitedTo
=
limit
}
var
buf
*
bytes
.
Buffer
acceptHeader
:=
strings
.
ToLower
(
strings
.
Join
(
r
.
Header
[
"Accept"
],
","
))
switch
{
case
strings
.
Contains
(
acceptHeader
,
"application/json"
)
:
if
buf
,
err
=
b
.
formatAsJSON
(
listing
,
bc
);
err
!=
nil
{
return
http
.
StatusInternalServerError
,
err
return
http
.
StatusInternalServerError
,
err
}
}
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json; charset=utf-8"
)
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json; charset=utf-8"
)
}
else
{
// There's no 'application/json' in the 'Accept' header; browse normally
default
:
// There's no 'application/json' in the 'Accept' header; browse normally
err
=
bc
.
Template
.
Execute
(
&
buf
,
listing
)
if
buf
,
err
=
b
.
formatAsHTML
(
listing
,
bc
);
err
!=
nil
{
if
err
!=
nil
{
return
http
.
StatusInternalServerError
,
err
return
http
.
StatusInternalServerError
,
err
}
}
w
.
Header
()
.
Set
(
"Content-Type"
,
"text/html; charset=utf-8"
)
w
.
Header
()
.
Set
(
"Content-Type"
,
"text/html; charset=utf-8"
)
...
@@ -386,3 +412,20 @@ inScope:
...
@@ -386,3 +412,20 @@ inScope:
return
http
.
StatusOK
,
nil
return
http
.
StatusOK
,
nil
}
}
func
(
b
Browse
)
formatAsJSON
(
listing
*
Listing
,
bc
*
Config
)
(
*
bytes
.
Buffer
,
error
)
{
marsh
,
err
:=
json
.
Marshal
(
listing
.
Items
)
if
err
!=
nil
{
return
nil
,
err
}
buf
:=
new
(
bytes
.
Buffer
)
_
,
err
=
buf
.
Write
(
marsh
)
return
buf
,
err
}
func
(
b
Browse
)
formatAsHTML
(
listing
*
Listing
,
bc
*
Config
)
(
*
bytes
.
Buffer
,
error
)
{
buf
:=
new
(
bytes
.
Buffer
)
err
:=
bc
.
Template
.
Execute
(
buf
,
listing
)
return
buf
,
err
}
middleware/browse/browse_test.go
View file @
c3417a07
...
@@ -114,10 +114,10 @@ func TestBrowseHTTPMethods(t *testing.T) {
...
@@ -114,10 +114,10 @@ func TestBrowseHTTPMethods(t *testing.T) {
Next
:
middleware
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
Next
:
middleware
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
return
http
.
StatusTeapot
,
nil
// not t.Fatalf, or we will not see what other methods yield
return
http
.
StatusTeapot
,
nil
// not t.Fatalf, or we will not see what other methods yield
}),
}),
Root
:
"./testdata"
,
Configs
:
[]
Config
{
Configs
:
[]
Config
{
{
{
PathScope
:
"/photos"
,
PathScope
:
"/photos"
,
Root
:
http
.
Dir
(
"./testdata"
),
Template
:
tmpl
,
Template
:
tmpl
,
},
},
},
},
...
@@ -153,10 +153,10 @@ func TestBrowseTemplate(t *testing.T) {
...
@@ -153,10 +153,10 @@ func TestBrowseTemplate(t *testing.T) {
t
.
Fatalf
(
"Next shouldn't be called"
)
t
.
Fatalf
(
"Next shouldn't be called"
)
return
0
,
nil
return
0
,
nil
}),
}),
Root
:
"./testdata"
,
Configs
:
[]
Config
{
Configs
:
[]
Config
{
{
{
PathScope
:
"/photos"
,
PathScope
:
"/photos"
,
Root
:
http
.
Dir
(
"./testdata"
),
Template
:
tmpl
,
Template
:
tmpl
,
},
},
},
},
...
@@ -208,16 +208,16 @@ func TestBrowseJson(t *testing.T) {
...
@@ -208,16 +208,16 @@ func TestBrowseJson(t *testing.T) {
t
.
Fatalf
(
"Next shouldn't be called"
)
t
.
Fatalf
(
"Next shouldn't be called"
)
return
0
,
nil
return
0
,
nil
}),
}),
Root
:
"./testdata"
,
Configs
:
[]
Config
{
Configs
:
[]
Config
{
{
{
PathScope
:
"/photos/"
,
PathScope
:
"/photos/"
,
Root
:
http
.
Dir
(
"./testdata"
),
},
},
},
},
}
}
//Getting the listing from the ./testdata/photos, the listing returned will be used to validate test results
//Getting the listing from the ./testdata/photos, the listing returned will be used to validate test results
testDataPath
:=
b
.
Root
+
"/photos/"
testDataPath
:=
filepath
.
Join
(
"./testdata"
,
"photos"
)
file
,
err
:=
os
.
Open
(
testDataPath
)
file
,
err
:=
os
.
Open
(
testDataPath
)
if
err
!=
nil
{
if
err
!=
nil
{
if
os
.
IsPermission
(
err
)
{
if
os
.
IsPermission
(
err
)
{
...
@@ -238,7 +238,7 @@ func TestBrowseJson(t *testing.T) {
...
@@ -238,7 +238,7 @@ func TestBrowseJson(t *testing.T) {
// Tests fail in CI environment because all file mod times are the same for
// Tests fail in CI environment because all file mod times are the same for
// some reason, making the sorting unpredictable. To hack around this,
// some reason, making the sorting unpredictable. To hack around this,
// we ensure here that each file has a different mod time.
// we ensure here that each file has a different mod time.
chTime
:=
f
.
ModTime
()
.
Add
(
-
(
time
.
Duration
(
i
)
*
time
.
Second
))
chTime
:=
f
.
ModTime
()
.
UTC
()
.
Add
(
-
(
time
.
Duration
(
i
)
*
time
.
Second
))
if
err
:=
os
.
Chtimes
(
filepath
.
Join
(
testDataPath
,
name
),
chTime
,
chTime
);
err
!=
nil
{
if
err
:=
os
.
Chtimes
(
filepath
.
Join
(
testDataPath
,
name
),
chTime
,
chTime
);
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
...
@@ -315,7 +315,7 @@ func TestBrowseJson(t *testing.T) {
...
@@ -315,7 +315,7 @@ func TestBrowseJson(t *testing.T) {
code
,
err
:=
b
.
ServeHTTP
(
rec
,
req
)
code
,
err
:=
b
.
ServeHTTP
(
rec
,
req
)
if
code
!=
http
.
StatusOK
{
if
code
!=
http
.
StatusOK
{
t
.
Fatalf
(
"
Wrong status, expected %d, got %d"
,
http
.
StatusOK
,
code
)
t
.
Fatalf
(
"
In test %d: Wrong status, expected %d, got %d"
,
i
,
http
.
StatusOK
,
code
)
}
}
if
rec
.
HeaderMap
.
Get
(
"Content-Type"
)
!=
"application/json; charset=utf-8"
{
if
rec
.
HeaderMap
.
Get
(
"Content-Type"
)
!=
"application/json; charset=utf-8"
{
t
.
Fatalf
(
"Expected Content type to be application/json; charset=utf-8, but got %s "
,
rec
.
HeaderMap
.
Get
(
"Content-Type"
))
t
.
Fatalf
(
"Expected Content type to be application/json; charset=utf-8, but got %s "
,
rec
.
HeaderMap
.
Get
(
"Content-Type"
))
...
...
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