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) {
...
@@ -20,8 +20,8 @@ func Mime(c *Controller) (middleware.Middleware, error) {
},
nil
},
nil
}
}
func
mimeParse
(
c
*
Controller
)
(
[]
mime
.
Config
,
error
)
{
func
mimeParse
(
c
*
Controller
)
(
mime
.
Config
,
error
)
{
var
configs
[]
mime
.
Config
configs
:=
mime
.
Config
{}
for
c
.
Next
()
{
for
c
.
Next
()
{
// At least one extension is required
// At least one extension is required
...
@@ -29,22 +29,22 @@ func mimeParse(c *Controller) ([]mime.Config, error) {
...
@@ -29,22 +29,22 @@ func mimeParse(c *Controller) ([]mime.Config, error) {
args
:=
c
.
RemainingArgs
()
args
:=
c
.
RemainingArgs
()
switch
len
(
args
)
{
switch
len
(
args
)
{
case
2
:
case
2
:
if
err
:=
validateExt
(
args
[
0
]);
err
!=
nil
{
if
err
:=
validateExt
(
configs
,
args
[
0
]);
err
!=
nil
{
return
configs
,
err
return
configs
,
err
}
}
configs
=
append
(
configs
,
mime
.
Config
{
Ext
:
args
[
0
],
ContentType
:
args
[
1
]})
configs
[
args
[
0
]]
=
args
[
1
]
case
1
:
case
1
:
return
configs
,
c
.
ArgErr
()
return
configs
,
c
.
ArgErr
()
case
0
:
case
0
:
for
c
.
NextBlock
()
{
for
c
.
NextBlock
()
{
ext
:=
c
.
Val
()
ext
:=
c
.
Val
()
if
err
:=
validateExt
(
ext
);
err
!=
nil
{
if
err
:=
validateExt
(
configs
,
ext
);
err
!=
nil
{
return
configs
,
err
return
configs
,
err
}
}
if
!
c
.
NextArg
()
{
if
!
c
.
NextArg
()
{
return
configs
,
c
.
ArgErr
()
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) {
...
@@ -54,9 +54,12 @@ func mimeParse(c *Controller) ([]mime.Config, error) {
}
}
// validateExt checks for valid file name extension.
// validateExt checks for valid file name extension.
func
validateExt
(
ext
string
)
error
{
func
validateExt
(
configs
mime
.
Config
,
ext
string
)
error
{
if
!
strings
.
HasPrefix
(
ext
,
"."
)
{
if
!
strings
.
HasPrefix
(
ext
,
"."
)
{
return
fmt
.
Errorf
(
`mime: invalid extension "%v" (must start with dot)`
,
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
return
nil
}
}
caddy/setup/mime_test.go
View file @
f14cdcc4
...
@@ -42,6 +42,11 @@ func TestMime(t *testing.T) {
...
@@ -42,6 +42,11 @@ func TestMime(t *testing.T) {
.html text/html
.html text/html
.txt text/plain
.txt text/plain
} `
,
false
},
} `
,
false
},
{
`mime {
.foo text/foo
.bar text/bar
.foo text/foobar
} `
,
true
},
{
`mime { .html text/html } `
,
false
},
{
`mime { .html text/html } `
,
false
},
{
`mime { .html
{
`mime { .html
} `
,
true
},
} `
,
true
},
...
...
middleware/mime/mime.go
View file @
f14cdcc4
...
@@ -2,40 +2,31 @@ package mime
...
@@ -2,40 +2,31 @@ package mime
import
(
import
(
"net/http"
"net/http"
"path
/filepath
"
"path"
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware"
)
)
// Config represent a mime config.
// Config represent a mime config. Map from extension to mime-type.
type
Config
struct
{
// Note, this should be safe with concurrent read access, as this is
Ext
string
// not modified concurrently.
ContentType
string
type
Config
map
[
string
]
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
}
// Mime sets Content-Type header of requests based on configurations.
// Mime sets Content-Type header of requests based on configurations.
type
Mime
struct
{
type
Mime
struct
{
Next
middleware
.
Handler
Next
middleware
.
Handler
Configs
[]
Config
Configs
Config
}
}
// ServeHTTP implements the middleware.Handler interface.
// ServeHTTP implements the middleware.Handler interface.
func
(
e
Mime
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
func
(
e
Mime
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
for
_
,
c
:=
range
e
.
Configs
{
if
ok
:=
c
.
SetContent
(
w
,
r
);
ok
{
// Get a clean /-path, grab the extension
break
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
)
return
e
.
Next
.
ServeHTTP
(
w
,
r
)
}
}
middleware/mime/mime_test.go
View file @
f14cdcc4
...
@@ -11,18 +11,13 @@ import (
...
@@ -11,18 +11,13 @@ import (
func
TestMimeHandler
(
t
*
testing
.
T
)
{
func
TestMimeHandler
(
t
*
testing
.
T
)
{
mimes
:=
map
[
string
]
strin
g
{
mimes
:=
Confi
g
{
".html"
:
"text/html"
,
".html"
:
"text/html"
,
".txt"
:
"text/plain"
,
".txt"
:
"text/plain"
,
".swf"
:
"application/x-shockwave-flash"
,
".swf"
:
"application/x-shockwave-flash"
,
}
}
var
configs
[]
Config
m
:=
Mime
{
Configs
:
mimes
}
for
ext
,
contentType
:=
range
mimes
{
configs
=
append
(
configs
,
Config
{
Ext
:
ext
,
ContentType
:
contentType
})
}
m
:=
Mime
{
Configs
:
configs
}
w
:=
httptest
.
NewRecorder
()
w
:=
httptest
.
NewRecorder
()
exts
:=
[]
string
{
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