Permalink
Browse files

ReqCondition can also check response, not opposite

Thanks Roger Peppe
  • Loading branch information...
1 parent 8def53c commit 736890b861e6de13e4b936fa68a1481eed51b7c9 @elazarl committed Jul 3, 2012
Showing with 17 additions and 27 deletions.
  1. +11 −21 dispatcher.go
  2. +6 −6 ext/html/html.go
View
@@ -11,13 +11,13 @@ import (
// ReqCondition.HandleReq will decide whether or not to use the ReqHandler on an HTTP request
// before sending it to the remote server
type ReqCondition interface {
+ RespCondition
HandleReq(req *http.Request, ctx *ProxyCtx) bool
}
// ReqCondition.HandleReq will decide whether or not to use the RespHandler on an HTTP response
// before sending it to the proxy client
type RespCondition interface {
- ReqCondition
HandleResp(resp *http.Response, ctx *ProxyCtx) bool
}
@@ -33,9 +33,8 @@ func (c ReqConditionFunc) HandleReq(req *http.Request, ctx *ProxyCtx) bool {
// RespConditionFunc cannot test requests. It only satisfies ReqCondition interface so that
// RespCondition and ReqCondition will be of the same type.
-func (c RespConditionFunc) HandleReq(req *http.Request, ctx *ProxyCtx) bool {
- panic("RespCondition should never handle request, " +
- "it is of the same type just to have poor man's algebraid data types")
+func (c ReqConditionFunc) HandleResp(resp *http.Response, ctx *ProxyCtx) bool {
+ return c(ctx.Req, ctx)
}
func (c RespConditionFunc) HandleResp(resp *http.Response, ctx *ProxyCtx) bool {
@@ -124,10 +123,10 @@ func DstHostIs(host string) ReqConditionFunc {
}
// SrcIpIs returns a ReqCondtion testing wether the source IP of the request is the given string
-func SrcIpIs(ip string) ReqConditionFunc {
- return func(req *http.Request, ctx *ProxyCtx) bool {
+func SrcIpIs(ip string) ReqCondition {
+ return ReqConditionFunc(func(req *http.Request, ctx *ProxyCtx) bool {
return strings.HasPrefix(req.RemoteAddr, ip+":")
- }
+ })
}
// Not returns a ReqCondtion negating the given ReqCondition
@@ -139,17 +138,17 @@ func Not(r ReqCondition) ReqConditionFunc {
// ContentTypeIs returns a RespCondition testing whether the HTTP response has Content-Type header equal
// to one of the given strings.
-func ContentTypeIs(typ string, types ...string) RespConditionFunc {
+func ContentTypeIs(typ string, types ...string) RespCondition {
types = append(types, typ)
- return func(resp *http.Response, ctx *ProxyCtx) bool {
+ return RespConditionFunc(func(resp *http.Response, ctx *ProxyCtx) bool {
contentType := resp.Header.Get("Content-Type")
for _, typ := range types {
if contentType == typ || strings.HasPrefix(contentType, typ+";") {
return true
}
}
return false
- }
+ })
}
// ProxyHttpServer.OnRequest Will return a temporary ReqProxyConds struct, aggregating the given condtions.
@@ -267,17 +266,8 @@ func (pcond *ProxyConds) Do(h RespHandler) {
// OnResponse is used when adding a response-filter to the HTTP proxy, usual pattern is
// proxy.OnResponse(cond1,cond2).Do(handler) // handler.Handle(resp,ctx) will be used
// // if cond1.HandleResp(resp) && cond2.HandleResp(resp)
-func (proxy *ProxyHttpServer) OnResponse(conds ...ReqCondition) *ProxyConds {
- pconds := &ProxyConds{proxy, make([]ReqCondition, 0), make([]RespCondition, 0)}
- for _, cond := range conds {
- switch cond := cond.(type) {
- case RespCondition:
- pconds.respCond = append(pconds.respCond, cond)
- case ReqCondition:
- pconds.reqConds = append(pconds.reqConds, cond)
- }
- }
- return pconds
+func (proxy *ProxyHttpServer) OnResponse(conds ...RespCondition) *ProxyConds {
+ return &ProxyConds{proxy, make([]ReqCondition, 0), conds}
}
// AlwaysMitm is a HttpsHandler that always eavesdrop https connections, for example to
View
@@ -13,18 +13,18 @@ import (
"strings"
)
-var IsHtml goproxy.RespConditionFunc = goproxy.ContentTypeIs("text/html")
+var IsHtml goproxy.RespCondition = goproxy.ContentTypeIs("text/html")
-var IsCss goproxy.RespConditionFunc = goproxy.ContentTypeIs("text/css")
+var IsCss goproxy.RespCondition = goproxy.ContentTypeIs("text/css")
-var IsJavaScript goproxy.RespConditionFunc = goproxy.ContentTypeIs("text/javascript",
+var IsJavaScript goproxy.RespCondition = goproxy.ContentTypeIs("text/javascript",
"application/javascript")
-var IsJson goproxy.RespConditionFunc = goproxy.ContentTypeIs("text/json")
+var IsJson goproxy.RespCondition = goproxy.ContentTypeIs("text/json")
-var IsXml goproxy.RespConditionFunc = goproxy.ContentTypeIs("text/xml")
+var IsXml goproxy.RespCondition = goproxy.ContentTypeIs("text/xml")
-var IsWebRelatedText goproxy.RespConditionFunc = goproxy.ContentTypeIs("text/html",
+var IsWebRelatedText goproxy.RespCondition = goproxy.ContentTypeIs("text/html",
"text/css",
"text/javascript", "application/javascript",
"text/xml",

0 comments on commit 736890b

Please sign in to comment.