mirror of
https://github.com/openfaas/faas.git
synced 2025-06-21 09:56:31 +00:00
Match content-type of incoming request. Fix for Alexa.
This commit is contained in:
@ -3,24 +3,21 @@ package handlers
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/alexellis/faas/gateway/metrics"
|
||||
"github.com/alexellis/faas/gateway/requests"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// makeProxy creates a proxy for HTTP web requests which can be routed to a function.
|
||||
// MakeProxy creates a proxy for HTTP web requests which can be routed to a function.
|
||||
func MakeProxy(metrics metrics.MetricOptions, wildcard bool, c *client.Client, logger *logrus.Logger) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
metrics.GatewayRequestsTotal.Inc()
|
||||
@ -40,45 +37,15 @@ func MakeProxy(metrics metrics.MetricOptions, wildcard bool, c *client.Client, l
|
||||
} else if len(header) > 0 {
|
||||
lookupInvoke(w, r, metrics, header[0], c, logger)
|
||||
defer r.Body.Close()
|
||||
|
||||
} else {
|
||||
requestBody, _ := ioutil.ReadAll(r.Body)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte("Provide a named /function URL or an x-function header."))
|
||||
defer r.Body.Close()
|
||||
alexaService := IsAlexa(requestBody)
|
||||
fmt.Println(alexaService)
|
||||
defer r.Body.Close()
|
||||
|
||||
if len(alexaService.Session.SessionId) > 0 &&
|
||||
len(alexaService.Session.Application.ApplicationId) > 0 &&
|
||||
len(alexaService.Request.Intent.Name) > 0 {
|
||||
|
||||
fmt.Println("Alexa SDK request found")
|
||||
fmt.Printf("SessionId=%s, Intent=%s, AppId=%s\n", alexaService.Session.SessionId, alexaService.Request.Intent.Name, alexaService.Session.Application.ApplicationId)
|
||||
|
||||
invokeService(w, r, metrics, alexaService.Request.Intent.Name, requestBody, logger)
|
||||
|
||||
} else {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte("Provide an x-function header or a valid Alexa SDK request."))
|
||||
defer r.Body.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func IsAlexa(requestBody []byte) requests.AlexaRequestBody {
|
||||
body := requests.AlexaRequestBody{}
|
||||
buf := bytes.NewBuffer(requestBody)
|
||||
// fmt.Println(buf)
|
||||
str := buf.String()
|
||||
parts := strings.Split(str, "sessionId")
|
||||
if len(parts) > 1 {
|
||||
json.Unmarshal(requestBody, &body)
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
func lookupInvoke(w http.ResponseWriter, r *http.Request, metrics metrics.MetricOptions, name string, c *client.Client, logger *logrus.Logger) {
|
||||
exists, err := lookupSwarmService(name, c)
|
||||
if err != nil || exists == false {
|
||||
@ -112,9 +79,14 @@ func invokeService(w http.ResponseWriter, r *http.Request, metrics metrics.Metri
|
||||
start := time.Now()
|
||||
buf := bytes.NewBuffer(requestBody)
|
||||
url := "http://" + service + ":" + strconv.Itoa(8080) + "/"
|
||||
fmt.Printf("[%s] Forwarding request to: %s\n", stamp, url)
|
||||
contentType := r.Header.Get("Content-Type")
|
||||
if len(contentType) == 0 {
|
||||
contentType = "text/plain"
|
||||
}
|
||||
|
||||
response, err := http.Post(url, "text/plain", buf)
|
||||
fmt.Printf("[%s] Forwarding request [%s] to: %s\n", stamp, contentType, url)
|
||||
|
||||
response, err := http.Post(url, r.Header.Get("Content-Type"), buf)
|
||||
if err != nil {
|
||||
logger.Infoln(err)
|
||||
w.WriteHeader(500)
|
||||
@ -132,6 +104,9 @@ func invokeService(w http.ResponseWriter, r *http.Request, metrics metrics.Metri
|
||||
return
|
||||
}
|
||||
|
||||
// Match header for strict services
|
||||
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(responseBody)
|
||||
|
||||
|
Reference in New Issue
Block a user