Support multipart

kcoram/uplift
Jan-Lukas Else 2019-11-07 12:10:45 +01:00
parent eb81344036
commit db58ecb3aa
3 changed files with 29 additions and 16 deletions

View File

@ -3,7 +3,9 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"math/rand" "math/rand"
"net/http"
"net/url" "net/url"
"strings" "strings"
"time" "time"
@ -24,21 +26,40 @@ type Entry struct {
token string token string
} }
func CreateEntry(contentType ContentType, body string) (*Entry, error) { func CreateEntry(contentType ContentType, r *http.Request) (*Entry, error) {
if contentType == WwwForm { if contentType == WwwForm {
bodyValues, err := url.ParseQuery(body) bodyString, err := parseRequestBody(r)
if err != nil {
return nil, err
}
bodyValues, err := url.ParseQuery(bodyString)
if err != nil { if err != nil {
return nil, errors.New("failed to parse query") return nil, errors.New("failed to parse query")
} }
return createEntryFromURLValues(bodyValues) return createEntryFromURLValues(bodyValues)
} else if contentType == Json || contentType == Multipart { } else if contentType == Multipart {
return nil, errors.New("multipart and json content-type are not implemented yet") err := r.ParseMultipartForm(1024 * 1024 * 16)
if err != nil {
return nil, errors.New("failed to parse Multipart")
}
return createEntryFromURLValues(r.MultipartForm.Value)
} else if contentType == Json {
return nil, errors.New("json content-type is not implemented yet")
} else { } else {
return nil, errors.New("unsupported content-type") return nil, errors.New("unsupported content-type")
} }
} }
func createEntryFromURLValues(bodyValues url.Values) (*Entry, error) { func parseRequestBody(r *http.Request) (string, error) {
defer r.Body.Close()
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
return "", errors.New("failed to read body")
}
return string(bodyBytes), nil
}
func createEntryFromURLValues(bodyValues map[string][]string) (*Entry, error) {
if h, ok := bodyValues["h"]; ok && len(h) == 1 && h[0] != "entry" { if h, ok := bodyValues["h"]; ok && len(h) == 1 && h[0] != "entry" {
return nil, errors.New("only entry type is supported so far") return nil, errors.New("only entry type is supported so far")
} }
@ -79,7 +100,7 @@ func createEntryFromURLValues(bodyValues url.Values) (*Entry, error) {
} }
return entry, nil return entry, nil
} }
return nil, errors.New("error parsing the entry from URL Values") return nil, errors.New("error parsing the entry")
} }
func computeExtraSettings(entry *Entry) error { func computeExtraSettings(entry *Entry) error {

10
main.go
View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"io/ioutil"
"log" "log"
"net/http" "net/http"
"time" "time"
@ -36,14 +35,7 @@ func handleMicroPub(w http.ResponseWriter, r *http.Request) {
return return
} }
// Create entry // Create entry
defer r.Body.Close() entry, err := CreateEntry(contentType, r)
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
bodyString := string(bodyBytes)
entry, err := CreateEntry(contentType, bodyString)
if err != nil || entry == nil { if err != nil || entry == nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
if err != nil { if err != nil {

View File

@ -105,7 +105,7 @@ func GetContentType(contentType string) (ContentType, error) {
if strings.Contains(contentType, "multipart/form-data") { if strings.Contains(contentType, "multipart/form-data") {
return Multipart, nil return Multipart, nil
} }
return UnsupportedType, errors.New("content-type " + contentType + " is not supported, use application/x-www-form-urlencoded") return UnsupportedType, errors.New("content-type " + contentType + " is not supported, use application/x-www-form-urlencoded or multipart/form-data")
} }
return UnsupportedType, errors.New("content-type is not provided in the request") return UnsupportedType, errors.New("content-type is not provided in the request")
} }