From a0af8c813a27ce0726017636e276591de2095de9 Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Mon, 27 Feb 2017 17:53:52 -0500 Subject: [PATCH 1/4] Add support for username + password auth --- main.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/main.go b/main.go index 7a021c3..de57203 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,8 @@ var ( version = flag.Bool("version", false, "Prints program version") networkAddress = flag.String("address", "localhost", "The address of the board") networkPort = flag.String("port", "80", "The board needs to be listening on this port") + username = flag.String("username", "", "Username for authentication") + password = flag.String("password", "", "Password for authentication") sketchPath = flag.String("sketch", "", "Sketch path") uploadEndpoint = flag.String("upload", "", "Upload endpoint") resetEndpoint = flag.String("reset", "", "Upload endpoint") @@ -141,6 +143,10 @@ func main() { } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + if len(*username) > 0 && len(*password) != 0 { + req.SetBasicAuth(*username, *password) + } + resp, err := http.DefaultClient.Do(req) if err != nil { if *verbose { From 78692ceaea859f567de9f7e2d0f8400747978567 Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Mon, 27 Feb 2017 17:54:26 -0500 Subject: [PATCH 2/4] Correct content type header for binary upload mode --- main.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index de57203..a4c1618 100644 --- a/main.go +++ b/main.go @@ -141,7 +141,12 @@ func main() { } os.Exit(1) } - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + if *binMode { + req.Header.Set("Content-Type", "application/octet-stream") + } else { + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + } if len(*username) > 0 && len(*password) != 0 { req.SetBasicAuth(*username, *password) From 09f7dc18b16a916ccbb0e48ec1a878fca5ae128f Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Tue, 28 Feb 2017 17:11:20 -0500 Subject: [PATCH 3/4] Add HTTP connection timeout As per https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f 779#.8j7att7r0 --- main.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index a4c1618..45c41fc 100644 --- a/main.go +++ b/main.go @@ -48,6 +48,10 @@ func main() { os.Exit(0) } + var httpClient = &http.Client{ + Timeout: time.Second * 10, + } + httpheader := "http://" if *useSsl != "" { @@ -70,7 +74,7 @@ func main() { fmt.Println("Resetting the board") } - resp, err := http.Post(httpheader+*networkAddress+":"+*networkPort+*syncEndpoint, "", nil) + resp, err := httpClient.Post(httpheader+*networkAddress+":"+*networkPort+*syncEndpoint, "", nil) if err != nil || resp.StatusCode != syncRetCode { if *verbose { fmt.Println("Failed to reset the board, upload failed") @@ -88,7 +92,7 @@ func main() { timeout := 0 for timeout < 10 { - resp, err := http.Get(httpheader + *networkAddress + ":" + *networkPort + *syncEndpoint) + resp, err := httpClient.Get(httpheader + *networkAddress + ":" + *networkPort + *syncEndpoint) if err != nil { if *verbose { fmt.Println("Failed to reset the board, upload failed") @@ -152,7 +156,7 @@ func main() { req.SetBasicAuth(*username, *password) } - resp, err := http.DefaultClient.Do(req) + resp, err := httpClient.Do(req) if err != nil { if *verbose { fmt.Println("Error flashing the sketch") @@ -181,7 +185,7 @@ func main() { fmt.Println("Resetting the board") } - resp, err := http.Post(httpheader+*networkAddress+":"+*networkPort+*resetEndpoint, "", nil) + resp, err := httpClient.Post(httpheader+*networkAddress+":"+*networkPort+*resetEndpoint, "", nil) if err != nil { if *verbose { fmt.Println("Failed to reset the board, please reset maually") From c079b96cbc445f828e0b5a5ff46c405260b545ab Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Tue, 28 Feb 2017 18:15:27 -0500 Subject: [PATCH 4/4] Add more verbose logging for upload endpoint --- main.go | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 45c41fc..9b2749d 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "io" "io/ioutil" "net/http" + "net/http/httptrace" "os" "regexp" "strconv" @@ -114,10 +115,6 @@ func main() { } if *uploadEndpoint != "" { - if *verbose { - fmt.Println("Uploading the sketch") - } - f, err := os.Open(*sketchPath) if err != nil { if *verbose { @@ -156,6 +153,32 @@ func main() { req.SetBasicAuth(*username, *password) } + if *verbose { + trace := &httptrace.ClientTrace{ + ConnectStart: func(network, addr string) { + fmt.Print("Connecting to board ... ") + }, + ConnectDone: func(network, addr string, err error) { + if err != nil { + fmt.Println("failed!") + } else { + fmt.Println(" done") + } + }, + WroteHeaders: func() { + fmt.Print("Uploading sketch ... ") + }, + WroteRequest: func(wri httptrace.WroteRequestInfo) { + fmt.Println(" done") + fmt.Print("Flashing sketch ... ") + }, + GotFirstResponseByte: func() { + fmt.Println(" done") + }, + } + req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace)) + } + resp, err := httpClient.Do(req) if err != nil { if *verbose {