mirror of
https://github.com/openfaas/faas.git
synced 2025-06-23 23:33:25 +00:00
Forward client HTTP headers through pipeline
This commit is contained in:
@ -79,9 +79,8 @@ func MakeFunctionReader(metricsOptions metrics.MetricOptions, c *client.Client)
|
||||
// MakeNewFunctionHandler creates a new function (service) inside the swarm network.
|
||||
func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Client) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
body, _ := ioutil.ReadAll(r.Body)
|
||||
defer r.Body.Close()
|
||||
body, _ := ioutil.ReadAll(r.Body)
|
||||
|
||||
request := requests.CreateFunctionRequest{}
|
||||
err := json.Unmarshal(body, &request)
|
||||
@ -91,7 +90,10 @@ func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Clie
|
||||
}
|
||||
|
||||
fmt.Println(request)
|
||||
w.WriteHeader(http.StatusNotImplemented)
|
||||
|
||||
// TODO: review why this was here... debugging?
|
||||
// w.WriteHeader(http.StatusNotImplemented)
|
||||
|
||||
options := types.ServiceCreateOptions{}
|
||||
spec := makeSpec(&request)
|
||||
|
||||
|
@ -103,6 +103,14 @@ func lookupSwarmService(serviceName string, c *client.Client) (bool, error) {
|
||||
return len(services) > 0, err
|
||||
}
|
||||
|
||||
func copyHeaders(destination *http.Header, source *http.Header) {
|
||||
for k, vv := range *source {
|
||||
vvClone := make([]string, len(vv))
|
||||
copy(vvClone, vv)
|
||||
(*destination)[k] = vvClone
|
||||
}
|
||||
}
|
||||
|
||||
func invokeService(w http.ResponseWriter, r *http.Request, metrics metrics.MetricOptions, service string, requestBody []byte, logger *logrus.Logger, proxyClient *http.Client) {
|
||||
stamp := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
|
||||
@ -123,14 +131,12 @@ func invokeService(w http.ResponseWriter, r *http.Request, metrics metrics.Metri
|
||||
}
|
||||
|
||||
contentType := r.Header.Get("Content-Type")
|
||||
if len(contentType) == 0 {
|
||||
contentType = "text/plain"
|
||||
}
|
||||
|
||||
fmt.Printf("[%s] Forwarding request [%s] to: %s\n", stamp, contentType, url)
|
||||
|
||||
request, err := http.NewRequest("POST", url, bytes.NewReader(requestBody))
|
||||
request.Header.Add("Content-Type", contentType)
|
||||
|
||||
copyHeaders(&request.Header, &r.Header)
|
||||
|
||||
defer request.Body.Close()
|
||||
|
||||
response, err := proxyClient.Do(request)
|
||||
@ -152,6 +158,9 @@ func invokeService(w http.ResponseWriter, r *http.Request, metrics metrics.Metri
|
||||
return
|
||||
}
|
||||
|
||||
clientHeader := w.Header()
|
||||
copyHeaders(&clientHeader, &response.Header)
|
||||
|
||||
// Match header for strict services
|
||||
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
|
||||
|
||||
|
Reference in New Issue
Block a user