faas/gateway/handlers/queueproxy.go
Dmitri Rubinstein c182a0af82 Started implementing Host header propagation for asynchronous invocations
Add Host field to the queue.Request struct and copy it from Host field
of the HTTP request when converting HTTP request to queue.Request in
MakeQueuedProxy function.

Signed-off-by: Dmitri Rubinstein <dmitri.rubinstein@googlemail.com>
2018-08-16 16:20:28 +01:00

69 lines
1.5 KiB
Go

// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package handlers
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/gorilla/mux"
"github.com/openfaas/faas/gateway/metrics"
"github.com/openfaas/faas/gateway/queue"
)
// MakeQueuedProxy accepts work onto a queue
func MakeQueuedProxy(metrics metrics.MetricOptions, wildcard bool, canQueueRequests queue.CanQueueRequests) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
vars := mux.Vars(r)
name := vars["name"]
callbackURLHeader := r.Header.Get("X-Callback-Url")
var callbackURL *url.URL
if len(callbackURLHeader) > 0 {
urlVal, urlErr := url.Parse(callbackURLHeader)
if urlErr != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(urlErr.Error()))
return
}
callbackURL = urlVal
}
req := &queue.Request{
Function: name,
Body: body,
Method: r.Method,
QueryString: r.URL.RawQuery,
Header: r.Header,
Host: r.Host,
CallbackURL: callbackURL,
}
err = canQueueRequests.Queue(req)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
fmt.Println(err)
return
}
w.WriteHeader(http.StatusAccepted)
}
}