From 6fdc7633757e90220478b961237949ea48d85e8a Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Tue, 6 Oct 2020 14:51:10 +0100 Subject: [PATCH] Add POST support --- cmd/furl/main.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/cmd/furl/main.go b/cmd/furl/main.go index 4ad775c5a..efaaa4b86 100644 --- a/cmd/furl/main.go +++ b/cmd/furl/main.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "bytes" "context" "crypto/ed25519" @@ -17,12 +18,13 @@ import ( var requestFrom = flag.String("from", "", "the server name that the request should originate from") var requestKey = flag.String("key", "matrix_key.pem", "the private key to use when signing the request") +var requestPost = flag.Bool("post", false, "send a POST request instead of GET (pipe input into stdin or type followed by Ctrl-D)") func main() { flag.Parse() if requestFrom == nil || *requestFrom == "" { - fmt.Println("expecting: furl -from ... [-key matrix_key.pem] https://path/to/url") + fmt.Println("expecting: furl -from origin.com [-key matrix_key.pem] https://path/to/url") fmt.Println("supported flags:") flag.PrintDefaults() os.Exit(1) @@ -59,12 +61,36 @@ func main() { panic(err) } + var bodyObj interface{} + var bodyBytes []byte + method := "GET" + if *requestPost { + method = "POST" + fmt.Println("Waiting for JSON input. Press Enter followed by Ctrl-D when done...") + + scan := bufio.NewScanner(os.Stdin) + for scan.Scan() { + bytes := scan.Bytes() + bodyBytes = append(bodyBytes, bytes...) + } + fmt.Println("Done!") + if err = json.Unmarshal(bodyBytes, &bodyObj); err != nil { + panic(err) + } + } + req := gomatrixserverlib.NewFederationRequest( - "GET", + method, gomatrixserverlib.ServerName(u.Host), u.RequestURI(), ) + if *requestPost { + if err = req.SetContent(bodyObj); err != nil { + panic(err) + } + } + if err = req.Sign( gomatrixserverlib.ServerName(*requestFrom), gomatrixserverlib.KeyID(keyBlock.Headers["Key-ID"]),