|
|
@@ -1,14 +1,11 @@
|
|
|
package gateway
|
|
|
|
|
|
import (
|
|
|
- "encoding/json"
|
|
|
"errors"
|
|
|
"io/ioutil"
|
|
|
- "log"
|
|
|
+ "mime/multipart"
|
|
|
"net/http"
|
|
|
"net/url"
|
|
|
- "os"
|
|
|
- "path"
|
|
|
"strconv"
|
|
|
|
|
|
"github.com/smallnest/rpcx/protocol"
|
|
|
@@ -106,110 +103,50 @@ func HttpRequest2RpcxRequest(r *http.Request) (*protocol.Message, error) {
|
|
|
return req, nil
|
|
|
}
|
|
|
|
|
|
-func MultipartRequest2RpcxRequest(r *http.Request) (*protocol.Message, string, error) {
|
|
|
- req := protocol.NewMessage()
|
|
|
- req.SetMessageType(protocol.Request)
|
|
|
-
|
|
|
+func MultipartRequest2RpcxRequest(r *http.Request) (map[string]string, *multipart.FileHeader, error) {
|
|
|
+ r.ParseMultipartForm(10 << 20) //10mb
|
|
|
+ form := r.MultipartForm
|
|
|
+ formValues := make(map[string]string)
|
|
|
+ //获取 multi-part/form header的 value
|
|
|
h := r.Header
|
|
|
- seq := h.Get(XMessageID)
|
|
|
- if seq != "" {
|
|
|
- id, err := strconv.ParseUint(seq, 10, 64)
|
|
|
- if err != nil {
|
|
|
- return nil, "", err
|
|
|
- }
|
|
|
- req.SetSeq(id)
|
|
|
- }
|
|
|
-
|
|
|
- heartbeat := h.Get(XHeartbeat)
|
|
|
- if heartbeat != "" {
|
|
|
- req.SetHeartbeat(true)
|
|
|
- }
|
|
|
-
|
|
|
- oneway := h.Get(XOneway)
|
|
|
- if oneway != "" {
|
|
|
- req.SetOneway(true)
|
|
|
- }
|
|
|
-
|
|
|
- if h.Get("Content-Encoding") == "gzip" {
|
|
|
- req.SetCompressType(protocol.Gzip)
|
|
|
- }
|
|
|
-
|
|
|
- st := h.Get(XSerializeType)
|
|
|
- if st != "" {
|
|
|
- rst, err := strconv.Atoi(st)
|
|
|
- if err != nil {
|
|
|
- return nil, "", err
|
|
|
- }
|
|
|
- req.SetSerializeType(protocol.SerializeType(rst))
|
|
|
- } else {
|
|
|
- return nil, "", errors.New("empty serialized type")
|
|
|
- }
|
|
|
-
|
|
|
meta := h.Get(XMeta)
|
|
|
if meta != "" {
|
|
|
metadata, err := url.ParseQuery(meta)
|
|
|
if err != nil {
|
|
|
- return nil, "", err
|
|
|
+ return nil, nil, err
|
|
|
}
|
|
|
- mm := make(map[string]string)
|
|
|
for k, v := range metadata {
|
|
|
if len(v) > 0 {
|
|
|
- mm[k] = v[0]
|
|
|
+ formValues[k] = v[0]
|
|
|
}
|
|
|
}
|
|
|
- req.Metadata = mm
|
|
|
}
|
|
|
|
|
|
+ //获取 multi-part/form body中的form value
|
|
|
+ for k, v := range form.Value {
|
|
|
+ formValues[k] = v[0]
|
|
|
+ }
|
|
|
+
|
|
|
+ file := form.File["file"][0]
|
|
|
+ formValues["fileName"] = file.Filename
|
|
|
+ formValues["fileSize"] = strconv.FormatInt(file.Size, 10)
|
|
|
+
|
|
|
sp := h.Get(XServicePath)
|
|
|
if sp != "" {
|
|
|
- req.ServicePath = sp
|
|
|
+ formValues["reqService"] = sp
|
|
|
} else {
|
|
|
- return nil, "", errors.New("empty servicepath")
|
|
|
+ return nil, nil, errors.New("empty servicepath")
|
|
|
}
|
|
|
|
|
|
sm := h.Get(XServiceMethod)
|
|
|
if sm != "" {
|
|
|
- req.ServiceMethod = sm
|
|
|
+ formValues["reqMethod"] = sm
|
|
|
} else {
|
|
|
- return nil, "", errors.New("empty servicemethod")
|
|
|
- }
|
|
|
-
|
|
|
- multipartReader, readerErr := r.MultipartReader()
|
|
|
- if readerErr != nil {
|
|
|
- return nil, "", readerErr
|
|
|
- }
|
|
|
-
|
|
|
- form, parseErr := multipartReader.ReadForm(32 << 20)
|
|
|
- if parseErr != nil {
|
|
|
- return nil, "", parseErr
|
|
|
- }
|
|
|
-
|
|
|
- metaForm := make(map[string]string)
|
|
|
- for k, v := range form.Value {
|
|
|
- metaForm[k] = v[0]
|
|
|
- }
|
|
|
- jstr, _ := json.Marshal(metaForm)
|
|
|
- req.Payload = jstr
|
|
|
-
|
|
|
- fh := form.File["file"][0]
|
|
|
- fi, _ := fh.Open()
|
|
|
- buf, _ := ioutil.ReadAll(fi)
|
|
|
-
|
|
|
- suffix := path.Ext(fh.Filename)
|
|
|
-
|
|
|
- tmpFile, err := ioutil.TempFile(os.TempDir(), "multipart-*"+suffix)
|
|
|
- if err != nil {
|
|
|
- log.Fatal("Cannot create temporary file", err)
|
|
|
- return nil, "", err
|
|
|
- }
|
|
|
-
|
|
|
- _, err = tmpFile.Write(buf)
|
|
|
- if err != nil {
|
|
|
- log.Fatal("Failed to write to temporary file", err)
|
|
|
- return nil, "", err
|
|
|
+ return nil, nil, errors.New("empty servicemethod")
|
|
|
}
|
|
|
+ formValues["authExclude"] = "false"
|
|
|
|
|
|
- return req, tmpFile.Name(), nil
|
|
|
+ return formValues, form.File["file"][0], nil
|
|
|
}
|
|
|
|
|
|
// func RpcxResponse2HttpResponse(res *protocol.Message) (url.Values, []byte, error) {
|