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
a38a2a0e
Commit
a38a2a0e
authored
Jan 21, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Created basic fastcgi middleware layer
parent
fe1978c6
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
129 additions
and
5 deletions
+129
-5
.gitignore
.gitignore
+5
-1
config/dispenser.go
config/dispenser.go
+11
-3
middleware/fastcgi.go
middleware/fastcgi.go
+111
-0
middleware/middleware.go
middleware/middleware.go
+2
-1
No files found.
.gitignore
View file @
a38a2a0e
.DS_Store
.DS_Store
Thumbs.db
Thumbs.db
_gitignore/
_gitignore/
Vagrantfile
.vagrant/
error.log
error.log
access.log
access.log
/*.conf
/*.conf
Caddyfile
Caddyfile
config/dispenser.go
View file @
a38a2a0e
...
@@ -141,11 +141,19 @@ func (d *dispenser) Err(msg string) middleware.Middleware {
...
@@ -141,11 +141,19 @@ func (d *dispenser) Err(msg string) middleware.Middleware {
// Args is a convenience function that loads the next arguments
// Args is a convenience function that loads the next arguments
// (tokens on the same line) into an arbitrary number of strings
// (tokens on the same line) into an arbitrary number of strings
// pointed to in targets. If there are fewer tokens available
// pointed to in targets. If there are fewer tokens available
// than string pointers, the remaining strings will not be changed.
// than string pointers, the remaining strings will not be changed
func
(
d
*
dispenser
)
Args
(
targets
...*
string
)
{
// and false will be returned. If there were enough tokens available
for
i
:=
0
;
i
<
len
(
targets
)
&&
d
.
NextArg
();
i
++
{
// to fill the arguments, then true will be returned.
func
(
d
*
dispenser
)
Args
(
targets
...*
string
)
bool
{
enough
:=
true
for
i
:=
0
;
i
<
len
(
targets
);
i
++
{
if
!
d
.
NextArg
()
{
enough
=
false
break
}
*
targets
[
i
]
=
d
.
Val
()
*
targets
[
i
]
=
d
.
Val
()
}
}
return
enough
}
}
// Startup registers a function to execute when the server starts.
// Startup registers a function to execute when the server starts.
...
...
middleware/fastcgi.go
0 → 100644
View file @
a38a2a0e
package
middleware
import
(
"io"
"io/ioutil"
"log"
"net/http"
"path/filepath"
"strings"
"bitbucket.org/PinIdea/fcgi_client"
// TODO: Inline this dependency. It'll need some work.
)
// FastCGI is 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 with php-fpm.
func
FastCGI
(
p
parser
)
Middleware
{
root
:=
p
.
Root
()
var
rules
[]
fastCgi
for
p
.
Next
()
{
rule
:=
fastCgi
{}
if
!
p
.
Args
(
&
rule
.
path
,
&
rule
.
address
)
{
return
p
.
ArgErr
()
}
rules
=
append
(
rules
,
rule
)
}
return
func
(
next
http
.
HandlerFunc
)
http
.
HandlerFunc
{
return
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
servedFcgi
:=
false
for
_
,
rule
:=
range
rules
{
if
Path
(
r
.
URL
.
Path
)
.
Matches
(
rule
.
path
)
{
servedFcgi
=
true
// Get absolute file paths
absPath
,
err
:=
filepath
.
Abs
(
root
+
r
.
URL
.
Path
)
if
err
!=
nil
{
// TODO!
log
.
Fatal
(
err
)
}
// Get absolute file paths
absRootPath
,
err
:=
filepath
.
Abs
(
root
)
if
err
!=
nil
{
// TODO!
log
.
Fatal
(
err
)
}
// Separate remote IP and port
var
ip
,
port
string
if
idx
:=
strings
.
Index
(
r
.
RemoteAddr
,
":"
);
idx
>
-
1
{
ip
=
r
.
RemoteAddr
[
idx
:
]
port
=
r
.
RemoteAddr
[
:
idx
]
}
else
{
ip
=
r
.
RemoteAddr
}
// TODO: Do we really have to make this map from scratch for each request?
env
:=
make
(
map
[
string
]
string
)
env
[
"SERVER_SOFTWARE"
]
=
"caddy"
// TODO: Obtain version info...
env
[
"SERVER_PROTOCOL"
]
=
r
.
Proto
env
[
"SCRIPT_FILENAME"
]
=
absPath
env
[
"REMOTE_ADDR"
]
=
ip
env
[
"REMOTE_PORT"
]
=
port
env
[
"REQUEST_METHOD"
]
=
r
.
Method
env
[
"QUERY_STRING"
]
=
r
.
URL
.
RawQuery
env
[
"DOCUMENT_URI"
]
=
r
.
URL
.
Path
env
[
"DOCUMENT_ROOT"
]
=
absRootPath
fcgi
,
err
:=
fcgiclient
.
Dial
(
"tcp"
,
rule
.
address
)
if
err
!=
nil
{
// TODO!
}
resp
,
err
:=
fcgi
.
Get
(
env
)
if
err
!=
nil
&&
err
!=
io
.
EOF
{
// TODO!
}
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
// TODO!
}
for
key
,
vals
:=
range
resp
.
Header
{
for
_
,
val
:=
range
vals
{
w
.
Header
()
.
Add
(
key
,
val
)
}
}
w
.
WriteHeader
(
resp
.
StatusCode
)
w
.
Write
(
body
)
break
}
}
if
!
servedFcgi
{
next
(
w
,
r
)
}
}
}
}
type
fastCgi
struct
{
path
string
address
string
}
middleware/middleware.go
View file @
a38a2a0e
...
@@ -17,6 +17,7 @@ func init() {
...
@@ -17,6 +17,7 @@ func init() {
register
(
"rewrite"
,
Rewrite
)
register
(
"rewrite"
,
Rewrite
)
register
(
"redir"
,
Redirect
)
register
(
"redir"
,
Redirect
)
register
(
"ext"
,
Extensionless
)
register
(
"ext"
,
Extensionless
)
register
(
"fastcgi"
,
FastCGI
)
}
}
type
(
type
(
...
@@ -37,7 +38,7 @@ type (
...
@@ -37,7 +38,7 @@ type (
NextLine
()
bool
NextLine
()
bool
NextBlock
()
bool
NextBlock
()
bool
Val
()
string
Val
()
string
Args
(
...*
string
)
Args
(
...*
string
)
bool
ArgErr
()
Middleware
ArgErr
()
Middleware
Err
(
string
)
Middleware
Err
(
string
)
Middleware
Startup
(
func
()
error
)
Startup
(
func
()
error
)
...
...
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