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
d8e7adcd
Commit
d8e7adcd
authored
Apr 11, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored proxy middleware
parent
113b175d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
46 deletions
+60
-46
middleware/proxy/proxy.go
middleware/proxy/proxy.go
+59
-45
middleware/websockets/websockets.go
middleware/websockets/websockets.go
+1
-1
No files found.
middleware/proxy/proxy.go
View file @
d8e7adcd
...
@@ -2,7 +2,6 @@
...
@@ -2,7 +2,6 @@
package
proxy
package
proxy
import
(
import
(
"log"
"net/http"
"net/http"
"net/http/httputil"
"net/http/httputil"
"net/url"
"net/url"
...
@@ -11,30 +10,22 @@ import (
...
@@ -11,30 +10,22 @@ import (
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware"
)
)
// New creates a new instance of proxy middleware.
// Proxy represents a middleware instance that can proxy requests.
func
New
(
c
middleware
.
Controller
)
(
middleware
.
Middleware
,
error
)
{
type
Proxy
struct
{
var
rules
[]
proxyRule
Next
middleware
.
Handler
Rules
[]
Rule
for
c
.
Next
()
{
}
rule
:=
proxyRule
{}
if
!
c
.
Args
(
&
rule
.
from
,
&
rule
.
to
)
{
return
nil
,
c
.
ArgErr
()
}
rules
=
append
(
rules
,
rule
)
}
return
func
(
next
middleware
.
Handler
)
middleware
.
Handler
{
// ServeHTTP satisfies the middleware.Handler interface.
return
middleware
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
func
(
p
Proxy
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
for
_
,
rule
:=
range
r
ules
{
for
_
,
rule
:=
range
p
.
R
ules
{
if
middleware
.
Path
(
r
.
URL
.
Path
)
.
Matches
(
rule
.
f
rom
)
{
if
middleware
.
Path
(
r
.
URL
.
Path
)
.
Matches
(
rule
.
F
rom
)
{
var
base
string
var
base
string
if
strings
.
HasPrefix
(
rule
.
t
o
,
"http"
)
{
// includes https
if
strings
.
HasPrefix
(
rule
.
T
o
,
"http"
)
{
// includes https
// destination includes a scheme! no need to guess
// destination includes a scheme! no need to guess
base
=
rule
.
t
o
base
=
rule
.
T
o
}
else
{
}
else
{
// no scheme specified; assume same as request
// no scheme specified; assume same as request
var
scheme
string
var
scheme
string
...
@@ -43,12 +34,12 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
...
@@ -43,12 +34,12 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
}
else
{
}
else
{
scheme
=
"https"
scheme
=
"https"
}
}
base
=
scheme
+
"://"
+
rule
.
t
o
base
=
scheme
+
"://"
+
rule
.
T
o
}
}
baseUrl
,
err
:=
url
.
Parse
(
base
)
baseUrl
,
err
:=
url
.
Parse
(
base
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatal
(
err
)
return
http
.
StatusInternalServerError
,
err
}
}
r
.
Host
=
baseUrl
.
Host
r
.
Host
=
baseUrl
.
Host
...
@@ -59,12 +50,35 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
...
@@ -59,12 +50,35 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
}
}
}
}
return
next
.
ServeHTTP
(
w
,
r
)
return
p
.
Next
.
ServeHTTP
(
w
,
r
)
})
}
// New creates a new instance of proxy middleware.
func
New
(
c
middleware
.
Controller
)
(
middleware
.
Middleware
,
error
)
{
rules
,
err
:=
parse
(
c
)
if
err
!=
nil
{
return
nil
,
err
}
return
func
(
next
middleware
.
Handler
)
middleware
.
Handler
{
return
Proxy
{
Next
:
next
,
Rules
:
rules
}
},
nil
},
nil
}
}
type
proxyRule
struct
{
func
parse
(
c
middleware
.
Controller
)
([]
Rule
,
error
)
{
from
string
var
rules
[]
Rule
to
string
for
c
.
Next
()
{
var
rule
Rule
if
!
c
.
Args
(
&
rule
.
From
,
&
rule
.
To
)
{
return
rules
,
c
.
ArgErr
()
}
rules
=
append
(
rules
,
rule
)
}
return
rules
,
nil
}
type
Rule
struct
{
From
,
To
string
}
}
middleware/websockets/websockets.go
View file @
d8e7adcd
...
@@ -108,7 +108,7 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
...
@@ -108,7 +108,7 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
Path
:
path
,
Path
:
path
,
Command
:
cmd
,
Command
:
cmd
,
Arguments
:
args
,
Arguments
:
args
,
Respawn
:
respawn
,
Respawn
:
respawn
,
// TODO: This isn't used currently
})
})
}
}
...
...
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