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
414b47d6
Commit
414b47d6
authored
Aug 18, 2015
by
Abdulelah Alfuntukh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
adds json option for the browse middleware
parent
b62d087b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
5 deletions
+41
-5
middleware/browse/browse.go
middleware/browse/browse.go
+41
-5
No files found.
middleware/browse/browse.go
View file @
414b47d6
...
...
@@ -4,12 +4,14 @@ package browse
import
(
"bytes"
"encoding/json"
"errors"
"net/http"
"net/url"
"os"
"path"
"sort"
"strconv"
"strings"
"text/template"
"time"
...
...
@@ -185,7 +187,6 @@ func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root s
// ServeHTTP implements the middleware.Handler interface.
func
(
b
Browse
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
filename
:=
b
.
Root
+
r
.
URL
.
Path
info
,
err
:=
os
.
Stat
(
filename
)
if
err
!=
nil
{
return
b
.
Next
.
ServeHTTP
(
w
,
r
)
...
...
@@ -264,12 +265,47 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
listing
.
applySort
()
var
buf
bytes
.
Buffer
err
=
bc
.
Template
.
Execute
(
&
buf
,
listing
)
if
err
!=
nil
{
return
http
.
StatusInternalServerError
,
err
// check if we should provide json
acceptHeader
:=
strings
.
Join
(
r
.
Header
[
"Accept"
],
","
)
if
strings
.
Contains
(
strings
.
ToLower
(
acceptHeader
),
"application/json"
)
{
var
marsh
[]
byte
// check if we are limited
if
limitQuery
:=
r
.
URL
.
Query
()
.
Get
(
"limit"
);
limitQuery
!=
""
{
limit
,
err
:=
strconv
.
Atoi
(
limitQuery
)
if
err
!=
nil
{
// if the 'limit' query can't be interpreted as a number, return err
return
http
.
StatusBadRequest
,
err
}
// if `limit` is equal or less than len(listing.Items) and bigger than 0, list them
if
limit
<=
len
(
listing
.
Items
)
&&
limit
>
0
{
marsh
,
err
=
json
.
Marshal
(
listing
.
Items
[
:
limit
])
}
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
}
}
// write the marshaled json to buf
if
_
,
err
=
buf
.
Write
(
marsh
);
err
!=
nil
{
return
http
.
StatusInternalServerError
,
err
}
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json; charset=utf-8"
)
}
else
{
// there's no json query, browse normally
err
=
bc
.
Template
.
Execute
(
&
buf
,
listing
)
if
err
!=
nil
{
return
http
.
StatusInternalServerError
,
err
}
w
.
Header
()
.
Set
(
"Content-Type"
,
"text/html; charset=utf-8"
)
}
w
.
Header
()
.
Set
(
"Content-Type"
,
"text/html; charset=utf-8"
)
buf
.
WriteTo
(
w
)
return
http
.
StatusOK
,
nil
...
...
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