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
113b175d
Commit
113b175d
authored
Apr 11, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored fastcgi middleware
parent
40bf7c52
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
103 additions
and
84 deletions
+103
-84
middleware/fastcgi/fastcgi.go
middleware/fastcgi/fastcgi.go
+102
-83
middleware/redirect/redirect.go
middleware/redirect/redirect.go
+1
-1
No files found.
middleware/fastcgi/fastcgi.go
View file @
113b175d
// Package fastcgi has middleware that acts as a FastCGI client. Requests
// that get forwarded to FastCGI stop the middleware execution chain.
// The most common use for this
layer
is to serve PHP websites via php-fpm.
// The most common use for this
package
is to serve PHP websites via php-fpm.
package
fastcgi
import
(
...
...
@@ -17,31 +17,38 @@ import (
func
New
(
c
middleware
.
Controller
)
(
middleware
.
Middleware
,
error
)
{
root
:=
c
.
Root
()
var
rules
[]
fastCgi
for
c
.
Next
()
{
rule
:=
fastCgi
{}
if
!
c
.
Args
(
&
rule
.
path
,
&
rule
.
address
)
{
return
nil
,
c
.
ArgErr
()
}
rules
=
append
(
rules
,
rule
)
rules
,
err
:=
parse
(
c
)
if
err
!=
nil
{
return
nil
,
err
}
return
func
(
next
middleware
.
Handler
)
middleware
.
Handler
{
return
middleware
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
return
Handler
{
Next
:
next
,
Rules
:
rules
,
Root
:
root
}
},
nil
}
// Handler is a middleware type that can handle requests as a FastCGI client.
type
Handler
struct
{
Next
middleware
.
Handler
Root
string
Rules
[]
Rule
}
// ServeHTTP satisfies the middleware.Handler interface.
func
(
h
Handler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
servedFcgi
:=
false
for
_
,
rule
:=
range
r
ules
{
if
middleware
.
Path
(
r
.
URL
.
Path
)
.
Matches
(
rule
.
p
ath
)
{
for
_
,
rule
:=
range
h
.
R
ules
{
if
middleware
.
Path
(
r
.
URL
.
Path
)
.
Matches
(
rule
.
P
ath
)
{
servedFcgi
=
true
// Get absolute file paths
absPath
,
err
:=
filepath
.
Abs
(
r
oot
+
r
.
URL
.
Path
)
absPath
,
err
:=
filepath
.
Abs
(
h
.
R
oot
+
r
.
URL
.
Path
)
if
err
!=
nil
{
return
http
.
StatusInternalServerError
,
err
}
// Get absolute file path to website root
absRootPath
,
err
:=
filepath
.
Abs
(
r
oot
)
absRootPath
,
err
:=
filepath
.
Abs
(
h
.
R
oot
)
if
err
!=
nil
{
return
http
.
StatusInternalServerError
,
err
}
...
...
@@ -68,7 +75,7 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
env
[
"DOCUMENT_URI"
]
=
r
.
URL
.
Path
env
[
"DOCUMENT_ROOT"
]
=
absRootPath
fcgi
,
err
:=
Dial
(
"tcp"
,
rule
.
a
ddress
)
fcgi
,
err
:=
Dial
(
"tcp"
,
rule
.
A
ddress
)
if
err
!=
nil
{
return
http
.
StatusBadGateway
,
err
}
...
...
@@ -97,15 +104,27 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
}
if
!
servedFcgi
{
return
n
ext
.
ServeHTTP
(
w
,
r
)
return
h
.
N
ext
.
ServeHTTP
(
w
,
r
)
}
return
0
,
nil
})
},
nil
}
type
fastCgi
struct
{
path
string
address
string
func
parse
(
c
middleware
.
Controller
)
([]
Rule
,
error
)
{
var
rules
[]
Rule
for
c
.
Next
()
{
var
rule
Rule
if
!
c
.
Args
(
&
rule
.
Path
,
&
rule
.
Address
)
{
return
rules
,
c
.
ArgErr
()
}
rules
=
append
(
rules
,
rule
)
}
return
rules
,
nil
}
// Rule represents a FastCGI handling rule.
type
Rule
struct
{
Path
,
Address
string
}
middleware/redirect/redirect.go
View file @
113b175d
...
...
@@ -9,7 +9,7 @@ import (
"github.com/mholt/caddy/middleware"
)
// New instantiates a new Re
writes
middleware.
// New instantiates a new Re
direct
middleware.
func
New
(
c
middleware
.
Controller
)
(
middleware
.
Middleware
,
error
)
{
rules
,
err
:=
parse
(
c
)
if
err
!=
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