Exemplo n.º 1
0
// Strips the query parameters and optionally preserves them in the X-Query-Param-xyz headers.
func (f *stripQuery) Request(ctx filters.FilterContext) {
	r := ctx.Request()
	if r == nil {
		return
	}

	url := r.URL
	if url == nil {
		return
	}

	if !f.preserveAsHeader {
		url.RawQuery = ""
		return
	}

	q := url.Query()
	for k, vv := range q {
		for _, v := range vv {
			if r.Header == nil {
				r.Header = http.Header{}
			}
			r.Header.Add(fmt.Sprintf("X-Query-Param-%s", sanitize(k)), v)
		}
	}

	url.RawQuery = ""
}
Exemplo n.º 2
0
func getLocation(ctx filters.FilterContext, location *url.URL) string {
	r := ctx.Request()

	uc := *location
	u := &uc

	if u.Scheme == "" {
		if r.URL.Scheme != "" {
			u.Scheme = r.URL.Scheme
		} else {
			u.Scheme = "https"
		}
	}

	u.User = r.URL.User

	if u.Host == "" {
		u.Host = getRequestHost(r)
	}

	if u.Path == "" {
		u.Path = r.URL.Path
	}

	if u.RawQuery == "" {
		u.RawQuery = r.URL.RawQuery
	}

	return u.String()
}
Exemplo n.º 3
0
// Creates a response from a handler and a request.
//
// It calls the handler's ServeHTTP method with an internal response
// writer that shares the status code, headers and the response body
// with the returned response. It blocks until the handler calls the
// response writer's WriteHeader, or starts writing the body, or
// returns. The written body is not buffered, but piped to the returned
// response's body.
//
// Example, a simple file server:
//
// 	var handler = http.StripPrefix(webRoot, http.FileServer(http.Dir(root)))
//
// 	func (f *myFilter) Request(ctx filters.FilterContext) {
// 		serve.ServeHTTP(ctx, handler)
// 	}
//
func ServeHTTP(ctx filters.FilterContext, h http.Handler) {
	rsp := &http.Response{Header: make(http.Header)}
	r, w := io.Pipe()
	d := &pipedResponse{
		response:   rsp,
		reader:     r,
		writer:     w,
		headerDone: make(chan struct{})}

	req := ctx.Request()
	go func() {
		h.ServeHTTP(d, req)
		select {
		case <-d.headerDone:
		default:
			d.WriteHeader(http.StatusOK)
		}

		w.CloseWithError(io.EOF)
	}()

	<-d.headerDone
	rsp.Body = d
	ctx.Serve(rsp)
}
Exemplo n.º 4
0
func (t *throttle) Response(ctx filters.FilterContext) {
	switch t.typ {
	case backendLatency, backendBandwidth, backendChunks:
		return
	}

	rsp := ctx.Response()
	rsp.Body = t.goThrottle(rsp.Body, true)
}
Exemplo n.º 5
0
func (t *throttle) Request(ctx filters.FilterContext) {
	switch t.typ {
	case latency, bandwidth, chunks:
		return
	}

	req := ctx.Request()
	req.Body = t.goThrottle(req.Body, false)
}
Exemplo n.º 6
0
func (f *headerFilter) Request(ctx filters.FilterContext) {
	if f.typ == requestHeader {
		req := ctx.Request()
		if strings.ToLower(f.key) == "host" {
			req.Host = f.value
		}

		req.Header.Add(f.key, f.value)
	}
}
Exemplo n.º 7
0
func (f *headerFilter) Request(ctx filters.FilterContext) {
	if f.typ != requestHeader {
		return
	}

	ctx.Request().Header.Add(f.key, f.value)
	if strings.ToLower(f.key) == "host" {
		ctx.SetOutgoingHost(f.value)
	}
}
Exemplo n.º 8
0
// Sets the status code and the location header of the response. Marks the
// request served.
func (f *redirect) Response(ctx filters.FilterContext) {
	if !f.deprecated {
		return
	}

	u := getLocation(ctx, f.location)
	w := ctx.ResponseWriter()
	w.Header().Set("Location", u)
	w.WriteHeader(f.code)
	ctx.MarkServed()
}
Exemplo n.º 9
0
// Request is copied and then modified to adopt changes in new backend
func (r *tee) Request(fc filters.FilterContext) {
	copyOfRequest := cloneRequest(r, fc.Request())
	go func() {
		rsp, err := r.client.Do(&copyOfRequest)
		if err != nil {
			log.Warn("error while tee request", err)
		}
		if err == nil {
			defer rsp.Body.Close()
		}
	}()
}
Exemplo n.º 10
0
// check basic auth
func (a *basic) Request(ctx filters.FilterContext) {
	username := a.authenticator.CheckAuth(ctx.Request())

	if username == "" {
		header := http.Header{}
		header.Set(ForceBasicAuthHeaderName, a.realmDefinition)

		ctx.Serve(&http.Response{
			StatusCode: http.StatusUnauthorized,
			Header:     header,
		})
	}
}
Exemplo n.º 11
0
func (c *compress) Response(ctx filters.FilterContext) {
	rsp := ctx.Response()

	if !canEncodeEntity(rsp, c.mime) {
		return
	}

	enc := acceptedEncoding(ctx.Request())
	if enc == "" {
		return
	}

	responseHeader(rsp, enc)
	responseBody(rsp, enc, c.level)
}
Exemplo n.º 12
0
func (f *headerFilter) Request(ctx filters.FilterContext) {
	switch f.typ {
	case setRequestHeader:
		ctx.Request().Header.Set(f.key, f.value)
		if strings.ToLower(f.key) == "host" {
			ctx.SetOutgoingHost(f.value)
		}
	case appendRequestHeader, depRequestHeader:
		ctx.Request().Header.Add(f.key, f.value)
		if strings.ToLower(f.key) == "host" {
			ctx.SetOutgoingHost(f.value)
		}
	case dropRequestHeader:
		ctx.Request().Header.Del(f.key)
	}
}
Exemplo n.º 13
0
func (f *flowId) Request(fc filters.FilterContext) {
	r := fc.Request()
	var flowId string

	if f.reuseExisting {
		flowId = r.Header.Get(HeaderName)
		if isValid(flowId) {
			return
		}
	}

	flowId, err := NewFlowId(f.flowIdLength)
	if err == nil {
		r.Header.Set(HeaderName, flowId)
	} else {
		log.Println(err)
	}
}
Exemplo n.º 14
0
// Serves content from the file system and marks the request served.
func (f *static) Response(ctx filters.FilterContext) {
	r := ctx.Request()
	p := r.URL.Path

	if len(p) < len(f.webRoot) {
		return
	}

	ctx.MarkServed()
	http.ServeFile(ctx.ResponseWriter(), ctx.Request(), path.Join(f.root, p[len(f.webRoot):]))
}
Exemplo n.º 15
0
// Sets the status code and the location header of the response. Marks the
// request served.
func (f *redirect) Response(ctx filters.FilterContext) {
	r := ctx.Request()
	w := ctx.ResponseWriter()
	u := f.copyOfLocation()

	if u.Scheme == "" {
		if r.URL.Scheme != "" {
			u.Scheme = r.URL.Scheme
		} else {
			u.Scheme = "https"
		}
	}

	u.User = r.URL.User

	if u.Host == "" {
		u.Host = getRequestHost(r)
	}

	if u.Path == "" {
		u.Path = r.URL.Path
	}

	if u.RawQuery == "" {
		u.RawQuery = r.URL.RawQuery
	}

	w.Header().Set("Location", u.String())
	w.WriteHeader(f.code)
	ctx.MarkServed()
}
Exemplo n.º 16
0
func (f *headerFilter) Response(ctx filters.FilterContext) {
	switch f.typ {
	case setResponseHeader:
		ctx.Response().Header.Set(f.key, f.value)
	case appendResponseHeader, depResponseHeader:
		ctx.Response().Header.Add(f.key, f.value)
	case dropResponseHeader:
		ctx.Response().Header.Del(f.key)
	}
}
Exemplo n.º 17
0
func (corf *preserveOriginalFilter) Response(ctx filters.FilterContext) {
	preserveHeader(ctx.OriginalResponse().Header, ctx.Response().Header)
}
Exemplo n.º 18
0
func (b *breaker) Request(c filters.FilterContext)                       { c.Serve(b.resp) }
Exemplo n.º 19
0
// the filter copies the path parameter 'echo' to the 'X-Echo' header
func (f *setEchoHeader) Request(ctx filters.FilterContext) {
	ctx.Request().Header.Set("X-Echo", ctx.PathParam("echo"))
}
Exemplo n.º 20
0
func (h *healthCheck) Response(ctx filters.FilterContext)                   { ctx.Response().StatusCode = http.StatusOK }
Exemplo n.º 21
0
// Modifies the path with regexp.ReplaceAll.
func (f *modPath) Request(ctx filters.FilterContext) {
	req := ctx.Request()
	req.URL.Path = string(f.rx.ReplaceAll([]byte(req.URL.Path), f.replacement))
}
Exemplo n.º 22
0
func (preserve filter) Request(ctx filters.FilterContext) {
	u, err := url.Parse(ctx.BackendUrl())
	if err != nil {
		log.Error("failed to parse backend host in preserveHost filter", err)
		return
	}

	if preserve && ctx.OutgoingHost() == u.Host {
		ctx.SetOutgoingHost(ctx.Request().Host)
	} else if !preserve && ctx.OutgoingHost() == ctx.Request().Host {
		ctx.SetOutgoingHost(u.Host)
	}
}
Exemplo n.º 23
0
func (f *customFilter) Request(ctx filters.FilterContext) {
	ctx.StateBag()["filter called"] = true
}
Exemplo n.º 24
0
// a simple filter logging the request URLs
func (f *customFilter) Request(ctx filters.FilterContext) {
	log.Println(f.prefix, ctx.Request().URL)
}
Exemplo n.º 25
0
func (rc *requestCheck) Request(ctx filters.FilterContext) {
	if !rc.check(ctx.Request()) {
		ctx.Serve(&http.Response{StatusCode: http.StatusBadRequest})
	}
}
Exemplo n.º 26
0
func (f statusFilter) Response(ctx filters.FilterContext) {
	ctx.Response().StatusCode = int(f)
}
Exemplo n.º 27
0
func (f *headerFilter) Response(ctx filters.FilterContext) {
	if f.typ == responseHeader {
		ctx.Response().Header.Add(f.key, f.value)
	}
}
Exemplo n.º 28
0
// Redirect implements the redirect logic as a standalone function.
func Redirect(ctx filters.FilterContext, code int, location *url.URL) {
	u := getLocation(ctx, location)
	ctx.Serve(&http.Response{
		StatusCode: code,
		Header:     http.Header{"Location": []string{u}}})
}