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
f14cdcc4
Commit
f14cdcc4
authored
Mar 16, 2016
by
Matt Holt
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #682 from weingart/mime_fix
mime: Use a map, and error on duplicate extensions.
parents
b79ff740
b471b7e8
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
30 additions
and
36 deletions
+30
-36
caddy/setup/mime.go
caddy/setup/mime.go
+10
-7
caddy/setup/mime_test.go
caddy/setup/mime_test.go
+5
-0
middleware/mime/mime.go
middleware/mime/mime.go
+13
-22
middleware/mime/mime_test.go
middleware/mime/mime_test.go
+2
-7
No files found.
caddy/setup/mime.go
View file @
f14cdcc4
...
...
@@ -20,8 +20,8 @@ func Mime(c *Controller) (middleware.Middleware, error) {
},
nil
}
func
mimeParse
(
c
*
Controller
)
(
[]
mime
.
Config
,
error
)
{
var
configs
[]
mime
.
Config
func
mimeParse
(
c
*
Controller
)
(
mime
.
Config
,
error
)
{
configs
:=
mime
.
Config
{}
for
c
.
Next
()
{
// At least one extension is required
...
...
@@ -29,22 +29,22 @@ func mimeParse(c *Controller) ([]mime.Config, error) {
args
:=
c
.
RemainingArgs
()
switch
len
(
args
)
{
case
2
:
if
err
:=
validateExt
(
args
[
0
]);
err
!=
nil
{
if
err
:=
validateExt
(
configs
,
args
[
0
]);
err
!=
nil
{
return
configs
,
err
}
configs
=
append
(
configs
,
mime
.
Config
{
Ext
:
args
[
0
],
ContentType
:
args
[
1
]})
configs
[
args
[
0
]]
=
args
[
1
]
case
1
:
return
configs
,
c
.
ArgErr
()
case
0
:
for
c
.
NextBlock
()
{
ext
:=
c
.
Val
()
if
err
:=
validateExt
(
ext
);
err
!=
nil
{
if
err
:=
validateExt
(
configs
,
ext
);
err
!=
nil
{
return
configs
,
err
}
if
!
c
.
NextArg
()
{
return
configs
,
c
.
ArgErr
()
}
configs
=
append
(
configs
,
mime
.
Config
{
Ext
:
ext
,
ContentType
:
c
.
Val
()}
)
configs
[
ext
]
=
c
.
Val
(
)
}
}
...
...
@@ -54,9 +54,12 @@ func mimeParse(c *Controller) ([]mime.Config, error) {
}
// validateExt checks for valid file name extension.
func
validateExt
(
ext
string
)
error
{
func
validateExt
(
configs
mime
.
Config
,
ext
string
)
error
{
if
!
strings
.
HasPrefix
(
ext
,
"."
)
{
return
fmt
.
Errorf
(
`mime: invalid extension "%v" (must start with dot)`
,
ext
)
}
if
_
,
ok
:=
configs
[
ext
];
ok
{
return
fmt
.
Errorf
(
`mime: duplicate extension "%v" found`
,
ext
)
}
return
nil
}
caddy/setup/mime_test.go
View file @
f14cdcc4
...
...
@@ -42,6 +42,11 @@ func TestMime(t *testing.T) {
.html text/html
.txt text/plain
} `
,
false
},
{
`mime {
.foo text/foo
.bar text/bar
.foo text/foobar
} `
,
true
},
{
`mime { .html text/html } `
,
false
},
{
`mime { .html
} `
,
true
},
...
...
middleware/mime/mime.go
View file @
f14cdcc4
...
...
@@ -2,40 +2,31 @@ package mime
import
(
"net/http"
"path
/filepath
"
"path"
"github.com/mholt/caddy/middleware"
)
// Config represent a mime config.
type
Config
struct
{
Ext
string
ContentType
string
}
// SetContent sets the Content-Type header of the request if the request path
// is supported.
func
(
c
Config
)
SetContent
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
bool
{
ext
:=
filepath
.
Ext
(
r
.
URL
.
Path
)
if
ext
!=
c
.
Ext
{
return
false
}
w
.
Header
()
.
Set
(
"Content-Type"
,
c
.
ContentType
)
return
true
}
// Config represent a mime config. Map from extension to mime-type.
// Note, this should be safe with concurrent read access, as this is
// not modified concurrently.
type
Config
map
[
string
]
string
// Mime sets Content-Type header of requests based on configurations.
type
Mime
struct
{
Next
middleware
.
Handler
Configs
[]
Config
Configs
Config
}
// ServeHTTP implements the middleware.Handler interface.
func
(
e
Mime
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
for
_
,
c
:=
range
e
.
Configs
{
if
ok
:=
c
.
SetContent
(
w
,
r
);
ok
{
break
}
// Get a clean /-path, grab the extension
ext
:=
path
.
Ext
(
path
.
Clean
(
r
.
URL
.
Path
))
if
contentType
,
ok
:=
e
.
Configs
[
ext
];
ok
{
w
.
Header
()
.
Set
(
"Content-Type"
,
contentType
)
}
return
e
.
Next
.
ServeHTTP
(
w
,
r
)
}
middleware/mime/mime_test.go
View file @
f14cdcc4
...
...
@@ -11,18 +11,13 @@ import (
func
TestMimeHandler
(
t
*
testing
.
T
)
{
mimes
:=
map
[
string
]
strin
g
{
mimes
:=
Confi
g
{
".html"
:
"text/html"
,
".txt"
:
"text/plain"
,
".swf"
:
"application/x-shockwave-flash"
,
}
var
configs
[]
Config
for
ext
,
contentType
:=
range
mimes
{
configs
=
append
(
configs
,
Config
{
Ext
:
ext
,
ContentType
:
contentType
})
}
m
:=
Mime
{
Configs
:
configs
}
m
:=
Mime
{
Configs
:
mimes
}
w
:=
httptest
.
NewRecorder
()
exts
:=
[]
string
{
...
...
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