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
fe62afd3
Commit
fe62afd3
authored
Jan 29, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Beginning to move middleware into their own packages
parent
dca59d0e
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
73 additions
and
79 deletions
+73
-79
config/dispenser.go
config/dispenser.go
+7
-11
config/middleware.go
config/middleware.go
+53
-0
config/parser.go
config/parser.go
+5
-7
config/parsing.go
config/parsing.go
+1
-3
middleware/middleware.go
middleware/middleware.go
+7
-58
No files found.
config/dispenser.go
View file @
fe62afd3
...
...
@@ -3,8 +3,6 @@ package config
import
(
"errors"
"fmt"
"github.com/mholt/caddy/middleware"
)
// dispenser is a type that gets exposed to middleware
...
...
@@ -15,7 +13,7 @@ type dispenser struct {
cursor
int
nesting
int
tokens
[]
token
err
error
//
err error
}
// newDispenser returns a new dispenser.
...
...
@@ -117,25 +115,23 @@ func (d *dispenser) Val() string {
// argument was expected but not found. The error is saved
// within the dispenser, but this function returns nil for
// convenience in practice.
func
(
d
*
dispenser
)
ArgErr
()
middleware
.
Middleware
{
func
(
d
*
dispenser
)
ArgErr
()
error
{
if
d
.
Val
()
==
"{"
{
d
.
Err
(
"Unexpected token '{', expecting argument"
)
return
nil
return
d
.
Err
(
"Unexpected token '{', expecting argument"
)
}
d
.
Err
(
"Unexpected line break after '"
+
d
.
Val
()
+
"' (missing arguments?)"
)
return
nil
return
d
.
Err
(
"Unexpected line break after '"
+
d
.
Val
()
+
"' (missing arguments?)"
)
}
// Err generates a custom parse error with a message of msg.
// TODO: Update the docs of all these Err methods!
// This function returns nil for convenience, but loads the
// error into the dispenser so it can be reported. The caller
// of the middleware preparator is responsible for checking
// the error in the dispenser after the middleware preparator
// is finished.
func
(
d
*
dispenser
)
Err
(
msg
string
)
middleware
.
Middleware
{
func
(
d
*
dispenser
)
Err
(
msg
string
)
error
{
msg
=
fmt
.
Sprintf
(
"%s:%d - Parse error: %s"
,
d
.
parser
.
filename
,
d
.
tokens
[
d
.
cursor
]
.
line
,
msg
)
d
.
err
=
errors
.
New
(
msg
)
return
nil
return
errors
.
New
(
msg
)
}
// Args is a convenience function that loads the next arguments
...
...
config/middleware.go
0 → 100644
View file @
fe62afd3
package
config
import
(
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware/extensionless"
"github.com/mholt/caddy/middleware/fastcgi"
"github.com/mholt/caddy/middleware/gzip"
"github.com/mholt/caddy/middleware/headers"
"github.com/mholt/caddy/middleware/log"
"github.com/mholt/caddy/middleware/proxy"
"github.com/mholt/caddy/middleware/redirect"
"github.com/mholt/caddy/middleware/rewrite"
)
// This init function registers middleware. Register middleware
// in the order they should be executed during a request.
// Middleware execute in an order like this: A-B-C-*-C-B-A
func
init
()
{
register
(
"gzip"
,
gzip
.
New
)
register
(
"header"
,
headers
.
New
)
register
(
"log"
,
log
.
New
)
register
(
"rewrite"
,
rewrite
.
New
)
register
(
"redirect"
,
redirect
.
New
)
register
(
"ext"
,
extensionless
.
New
)
register
(
"proxy"
,
proxy
.
New
)
register
(
"fastcgi"
,
fastcgi
.
New
)
}
var
(
// registry stores the registered middleware:
// both the order and the directives to which they
// are bound.
registry
=
struct
{
directiveMap
map
[
string
]
middleware
.
Generator
order
[]
string
}{
directiveMap
:
make
(
map
[
string
]
middleware
.
Generator
),
}
)
// register binds a middleware generator (outer function)
// to a directive. Upon each request, middleware will be
// executed in the order they are registered.
func
register
(
directive
string
,
generator
middleware
.
Generator
)
{
registry
.
directiveMap
[
directive
]
=
generator
registry
.
order
=
append
(
registry
.
order
,
directive
)
}
// middlewareRegistered returns whether or not a directive is registered.
func
middlewareRegistered
(
directive
string
)
bool
{
_
,
ok
:=
registry
.
directiveMap
[
directive
]
return
ok
}
config/parser.go
View file @
fe62afd3
...
...
@@ -5,8 +5,6 @@ import (
"fmt"
"os"
"strings"
"github.com/mholt/caddy/middleware"
)
// parser is a type which can parse config files.
...
...
@@ -108,12 +106,12 @@ func (p *parser) parseOne() error {
// This function should be called only after p has filled out
// p.other and that the entire server block has been consumed.
func
(
p
*
parser
)
unwrap
()
error
{
for
_
,
directive
:=
range
middleware
.
Ordered
()
{
for
directive
:=
range
registry
.
directiveMap
{
if
disp
,
ok
:=
p
.
other
[
directive
];
ok
{
if
generator
,
ok
:=
middleware
.
GetGenerator
(
directive
)
;
ok
{
mid
:=
generator
(
disp
)
if
mid
=
=
nil
{
return
disp
.
err
if
generator
,
ok
:=
registry
.
directiveMap
[
directive
]
;
ok
{
mid
,
err
:=
generator
(
disp
)
if
err
!
=
nil
{
return
err
}
p
.
cfg
.
Middleware
=
append
(
p
.
cfg
.
Middleware
,
mid
)
}
else
{
...
...
config/parsing.go
View file @
fe62afd3
package
config
import
"github.com/mholt/caddy/middleware"
// This file contains the recursive-descent parsing
// functions.
...
...
@@ -98,7 +96,7 @@ func (p *parser) directives() error {
if
err
!=
nil
{
return
err
}
}
else
if
middleware
.
Registered
(
p
.
tkn
())
{
}
else
if
middlewareRegistered
(
p
.
tkn
())
{
err
:=
p
.
collectTokens
()
if
err
!=
nil
{
return
err
...
...
middleware/middleware.go
View file @
fe62afd3
...
...
@@ -7,41 +7,28 @@ import (
"strings"
)
// This init function registers middleware. Register middleware
// in the order they should be executed during a request.
// Middlewares execute in an order like A-B-C-C-B-A.
func
init
()
{
register
(
"gzip"
,
Gzip
)
register
(
"header"
,
Headers
)
register
(
"log"
,
RequestLog
)
register
(
"rewrite"
,
Rewrite
)
register
(
"redir"
,
Redirect
)
register
(
"ext"
,
Extensionless
)
register
(
"proxy"
,
Proxy
)
register
(
"fastcgi"
,
FastCGI
)
}
type
(
// Generator represents the outer layer of a middleware that
// parses tokens to configure the middleware instance.
Generator
func
(
parser
)
Middleware
Generator
func
(
Controller
)
(
Middleware
,
error
)
// Middleware is the middle layer which represents the traditional
// idea of middleware: it is passed the next HandlerFunc in the chain
// and returns the inner layer, which is the actual HandlerFunc.
Middleware
func
(
http
.
HandlerFunc
)
http
.
HandlerFunc
// parser is the type which middleware generators use to access
// tokens and other information they need to configure the instance.
parser
interface
{
// Controller is the type which middleware generators use to access
// tokens and the server and any other information they need to
// configure themselves.
Controller
interface
{
Next
()
bool
NextArg
()
bool
NextLine
()
bool
NextBlock
()
bool
Val
()
string
Args
(
...*
string
)
bool
ArgErr
()
Middleware
Err
(
string
)
Middleware
ArgErr
()
error
Err
(
string
)
error
Startup
(
func
()
error
)
Root
()
string
Host
()
string
...
...
@@ -49,44 +36,6 @@ type (
}
)
var
(
// registry stores the registered middleware:
// both the order and the directives to which they
// are bound.
registry
=
struct
{
directiveMap
map
[
string
]
Generator
order
[]
string
}{
directiveMap
:
make
(
map
[
string
]
Generator
),
}
)
// GetGenerator gets the generator function (outer layer)
// of a middleware, according to the directive passed in.
func
GetGenerator
(
directive
string
)
(
Generator
,
bool
)
{
rm
,
ok
:=
registry
.
directiveMap
[
directive
]
return
rm
,
ok
}
// register binds a middleware generator (outer function)
// to a directive. Upon each request, middleware will be
// executed in the order they are registered.
func
register
(
directive
string
,
generator
Generator
)
{
registry
.
directiveMap
[
directive
]
=
generator
registry
.
order
=
append
(
registry
.
order
,
directive
)
}
// Ordered returns the ordered list of registered directives.
func
Ordered
()
[]
string
{
return
registry
.
order
}
// Registered returns whether or not a directive is registered.
func
Registered
(
directive
string
)
bool
{
_
,
ok
:=
GetGenerator
(
directive
)
return
ok
}
// Path represents a URI path, maybe with pattern characters.
type
Path
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