Пример #1
0
// filterFromJSON takes a JSON message as a byte slice and returns a
// parse.Result that contains a URLFilter and a bitmask that represents the
// type of modifier.
//
// Example JSON configuration message:
// {
//   "scheme": "https",
//   "host": "example.com",
//   "path": "/foo/bar",
//   "rawQuery": "q=value",
//   "scope": ["request", "response"],
//   "modifier": { ... }
// }
func filterFromJSON(b []byte) (*parse.Result, error) {
	msg := &filterJSON{}
	if err := json.Unmarshal(b, msg); err != nil {
		return nil, err
	}

	filter := NewFilter(&url.URL{
		Scheme:   msg.Scheme,
		Host:     msg.Host,
		Path:     msg.Path,
		RawQuery: msg.Query,
	})

	r, err := parse.FromJSON(msg.Modifier)
	if err != nil {
		return nil, err
	}

	reqmod := r.RequestModifier()
	if err != nil {
		return nil, err
	}
	if reqmod != nil {
		filter.SetRequestModifier(reqmod)
	}

	resmod := r.ResponseModifier()
	if resmod != nil {
		filter.SetResponseModifier(resmod)
	}

	return parse.NewResult(filter, msg.Scope)
}
Пример #2
0
// groupFromJSON builds a priority.Group from JSON.
//
// Example JSON:
// {
//   "priority.Group": {
//     "scope": ["request", "response"],
//     "modifiers": [
//       {
//         "priority": 100, // Will run first.
//         "modifier": { ... },
//       },
//       {
//         "priority": 0, // Will run last.
//         "modifier": { ... },
//       }
//     ]
//   }
// }
func groupFromJSON(b []byte) (*parse.Result, error) {
	msg := &groupJSON{}
	if err := json.Unmarshal(b, msg); err != nil {
		return nil, err
	}

	pg := NewGroup()

	for _, m := range msg.Modifiers {
		r, err := parse.FromJSON(m.Modifier)
		if err != nil {
			return nil, err
		}

		reqmod := r.RequestModifier()
		if reqmod != nil {
			pg.AddRequestModifier(reqmod, m.Priority)
		}

		resmod := r.ResponseModifier()
		if resmod != nil {
			pg.AddResponseModifier(resmod, m.Priority)
		}
	}

	return parse.NewResult(pg, msg.Scope)
}
Пример #3
0
// filterFromJSON takes a JSON message and returns a querystring.Filter.
//
// Example JSON:
// {
//   "name": "param",
//   "value": "example",
//   "scope": ["request", "response"],
//   "modifier": { ... }
// }
func filterFromJSON(b []byte) (*parse.Result, error) {
	msg := &filterJSON{}
	if err := json.Unmarshal(b, msg); err != nil {
		return nil, err
	}

	nameMatcher, err := regexp.Compile(msg.Name)
	if err != nil {
		return nil, err
	}

	valueMatcher, err := regexp.Compile(msg.Value)
	if err != nil {
		return nil, err
	}

	filter, err := NewFilter(nameMatcher, valueMatcher)
	if err != nil {
		return nil, err
	}

	r, err := parse.FromJSON(msg.Modifier)
	if err != nil {
		return nil, err
	}

	reqmod := r.RequestModifier()
	filter.SetRequestModifier(reqmod)

	resmod := r.ResponseModifier()
	filter.SetResponseModifier(resmod)

	return parse.NewResult(filter, msg.Scope)
}
Пример #4
0
// verifierFromJSON builds a header.Verifier from JSON.
//
// Example JSON:
// {
//   "name": "header.Verifier",
//   "scope": ["request", "result"],
//   "modifier": {
//     "name": "Martian-Testing",
//     "value": "true"
//   }
// }
func verifierFromJSON(b []byte) (*parse.Result, error) {
	msg := &verifierJSON{}
	if err := json.Unmarshal(b, msg); err != nil {
		return nil, err
	}

	return parse.NewResult(NewVerifier(msg.Name, msg.Value), msg.Scope)
}
Пример #5
0
// loggerFromJSON builds a logger from JSON.
//
// Example JSON:
// {
//   "log.Logger": {
//     "scope": ["request", "response"]
//   }
// }
func loggerFromJSON(b []byte) (*parse.Result, error) {
	msg := &loggerJSON{}
	if err := json.Unmarshal(b, msg); err != nil {
		return nil, err
	}

	return parse.NewResult(NewLogger(), msg.Scope)
}
Пример #6
0
// copyModifierFromJSON builds a copy modifier from JSON.
//
// Example JSON:
// {
//   "header.Copy": {
//     "scope": ["request", "response"],
//     "from": "Original-Header",
//     "to": "Copy-Header"
//   }
// }
func copyModifierFromJSON(b []byte) (*parse.Result, error) {
	msg := &copyModifierJSON{}
	if err := json.Unmarshal(b, msg); err != nil {
		return nil, err
	}

	return parse.NewResult(NewCopyModifier(msg.From, msg.To), msg.Scope)
}
Пример #7
0
// modifierFromJSON builds a status.Modifier from JSON.
//
// Example JSON:
// {
//   "status.Modifier": {
//     "scope": ["response"],
//     "statusCode": 401
//   }
// }
func modifierFromJSON(b []byte) (*parse.Result, error) {
	msg := &statusJSON{}
	if err := json.Unmarshal(b, msg); err != nil {
		return nil, err
	}

	return parse.NewResult(NewModifier(msg.StatusCode), msg.Scope)
}
Пример #8
0
// blacklistModifierFromJSON takes a JSON message as a byte slice and returns
// a blacklistModifier and an error.
//
// Example JSON configuration message:
// {
//   "names": ["X-Header", "Y-Header"],
//   "scope": ["request", "result"]
// }
func blacklistModifierFromJSON(b []byte) (*parse.Result, error) {
	msg := &blacklistModifierJSON{}
	if err := json.Unmarshal(b, msg); err != nil {
		return nil, err
	}

	return parse.NewResult(NewBlacklistModifier(msg.Names...), msg.Scope)
}
Пример #9
0
// modifierFromJSON takes a JSON message as a byte slice and returns a
// body.Modifier and an error.
//
// Example JSON Configuration message:
// {
//   "scope": ["request", "response"],
//   "contentType": "text/plain",
//   "body": "c29tZSBkYXRhIHdpdGggACBhbmQg77u/" // Base64 encoded body
// }
func modifierFromJSON(b []byte) (*parse.Result, error) {
	msg := &modifierJSON{}
	if err := json.Unmarshal(b, msg); err != nil {
		return nil, err
	}

	mod := NewModifier(msg.Body, msg.ContentType)
	return parse.NewResult(mod, msg.Scope)
}
Пример #10
0
// modifierFromJSON takes a JSON message as a byte slice and returns
// a headerModifier and an error.
//
// Example JSON configuration message:
// {
//  "scope": ["request", "result"],
//  "name": "X-Martian",
//  "value": "true"
// }
func modifierFromJSON(b []byte) (*parse.Result, error) {
	msg := &modifierJSON{}
	if err := json.Unmarshal(b, msg); err != nil {
		return nil, err
	}

	modifier := NewModifier(msg.Name, msg.Value)

	return parse.NewResult(modifier, msg.Scope)
}
Пример #11
0
// loggerFromJSON builds a logger from JSON.
//
// Example JSON:
// {
//   "log.Logger": {
//     "scope": ["request", "response"],
//		 "includeBody": true
//   }
// }
func loggerFromJSON(b []byte) (*parse.Result, error) {
	msg := &loggerJSON{}
	if err := json.Unmarshal(b, msg); err != nil {
		return nil, err
	}

	l := NewLogger()
	l.IncludeBody(msg.IncludeBody)

	return parse.NewResult(l, msg.Scope)
}
Пример #12
0
// loggerFromJSON builds a logger from JSON.
//
// Example JSON:
// {
//   "log.Logger": {
//     "scope": ["request", "response"],
//		 "headersOnly": true,
//		 "decode": true
//   }
// }
func loggerFromJSON(b []byte) (*parse.Result, error) {
	msg := &loggerJSON{}
	if err := json.Unmarshal(b, msg); err != nil {
		return nil, err
	}

	l := NewLogger()
	l.SetHeadersOnly(msg.HeadersOnly)
	l.SetDecode(msg.Decode)

	return parse.NewResult(l, msg.Scope)
}
Пример #13
0
// verifierFromJSON builds a method.Verifier from JSON.
//
// Example JSON:
// {
//   "method.Verifier": {
//     "scope": ["request"],
//     "method": "POST"
//   }
// }
func verifierFromJSON(b []byte) (*parse.Result, error) {

	msg := &verifierJSON{}

	if err := json.Unmarshal(b, msg); err != nil {
		return nil, err
	}
	v, err := NewVerifier(msg.Method)
	if err != nil {
		return nil, err
	}
	return parse.NewResult(v, msg.Scope)
}
Пример #14
0
// verifierFromJSON builds a martianurl.Verifier from JSON.
//
// Example modifier JSON:
// {
//   "martianurl.Verifier": {
//     "scope": ["request"],
//     "scheme": "https",
//     "host": "www.google.com",
//     "path": "/proxy",
//     "query": "testing=true"
//   }
// }
func verifierFromJSON(b []byte) (*parse.Result, error) {
	msg := &verifierJSON{}
	if err := json.Unmarshal(b, msg); err != nil {
		return nil, err
	}

	v := NewVerifier(&url.URL{
		Scheme:   msg.Scheme,
		Host:     msg.Host,
		Path:     msg.Path,
		RawQuery: msg.Query,
	})

	return parse.NewResult(v, msg.Scope)
}
Пример #15
0
// filterFromJSON takes a JSON message and returns a querystring.Filter.
//
// Example JSON:
// {
//   "name": "param",
//   "value": "example",
//   "scope": ["request", "response"],
//   "modifier": { ... }
// }
func filterFromJSON(b []byte) (*parse.Result, error) {
	msg := &filterJSON{}
	if err := json.Unmarshal(b, msg); err != nil {
		return nil, err
	}

	f := NewFilter(msg.Name, msg.Value)

	r, err := parse.FromJSON(msg.Modifier)
	if err != nil {
		return nil, err
	}

	f.SetRequestModifier(r.RequestModifier())
	f.SetResponseModifier(r.ResponseModifier())

	return parse.NewResult(f, msg.Scope)
}
Пример #16
0
// modifierFromJSON takes a JSON message as a byte slice and returns a
// body.Modifier and an error.
//
// Example JSON Configuration message:
// {
//   "scope": ["request", "response"],
//   "contentType": "text/plain",
//   "body": "c29tZSBkYXRhIHdpdGggACBhbmQg77u/" // Base64 encoded body
// }
func modifierFromJSON(b []byte) (*parse.Result, error) {
	msg := &modifierJSON{}
	if err := json.Unmarshal(b, msg); err != nil {
		return nil, err
	}

	body, err := base64.StdEncoding.DecodeString(msg.Body)
	if err != nil {
		return nil, err
	}

	mod, err := NewModifier(body, msg.ContentType)
	if err != nil {
		return nil, err
	}

	return parse.NewResult(mod, msg.Scope)
}
Пример #17
0
// modifierFromJSON takes a JSON message as a byte slice and returns a
// CookieModifier and an error.
//
// Example JSON Configuration message:
// {
//   "name": "Martian-Cookie",
//   "value": "some value",
//   "path": "/some/path",
//   "domain": "example.com",
//   "expires": "2025-04-12T23:20:50.52Z", // RFC 3339
//   "secure": true,
//   "httpOnly": false,
//   "maxAge": 86400,
//   "scope": ["request", "result"]
// }
func modifierFromJSON(b []byte) (*parse.Result, error) {
	msg := &modifierJSON{}
	if err := json.Unmarshal(b, msg); err != nil {
		return nil, err
	}

	c := &http.Cookie{
		Name:     msg.Name,
		Value:    msg.Value,
		Path:     msg.Path,
		Domain:   msg.Domain,
		Expires:  msg.Expires,
		Secure:   msg.Secure,
		HttpOnly: msg.HTTPOnly,
		MaxAge:   msg.MaxAge,
	}

	return parse.NewResult(NewModifier(c), msg.Scope)
}