summaryrefslogtreecommitdiff
path: root/tools/sw-fetch
diff options
context:
space:
mode:
authortjp <tjp@ctrl-c.club>2024-01-13 11:29:17 -0700
committertjp <tjp@ctrl-c.club>2024-01-13 11:29:17 -0700
commit4d861a2c395c0926b066014b92999d4dda454b2b (patch)
tree7bbf7d94cec2a5412a68b830c1e5223918d9bc82 /tools/sw-fetch
parentde1490808fa6e4d6749ff29d20cc1a589ec476d1 (diff)
dial timeouts for clients, and catch up on test fixes
Diffstat (limited to 'tools/sw-fetch')
-rw-r--r--tools/sw-fetch/main.go53
1 files changed, 41 insertions, 12 deletions
diff --git a/tools/sw-fetch/main.go b/tools/sw-fetch/main.go
index c2cb3e8..76e414c 100644
--- a/tools/sw-fetch/main.go
+++ b/tools/sw-fetch/main.go
@@ -1,12 +1,14 @@
package main
import (
+ "context"
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
"os"
+ "time"
"tildegit.org/tjp/sliderule"
"tildegit.org/tjp/sliderule/gemini"
@@ -17,29 +19,45 @@ const usage = `Resource fetcher for the small web.
Usage:
sw-fetch (-h | --help)
- sw-fetch [-v | --verbose] [-o PATH | --output PATH] [-k | --keyfile PATH] [ -c | --certfile PATH ] [ -s | --skip-verify ] [ -u | --upload ] URL
+ sw-fetch
+ [-v | --verbose]
+ [-o PATH | --output PATH]
+ [-k | --keyfile PATH]
+ [ -c | --certfile PATH ]
+ [ -s | --skip-verify ]
+ [ -t | --timeout TIMEOUT ]
+ [ -u | --upload ]
+ URL
Options:
- -h --help Show this screen.
- -v --verbose Display more diagnostic information on standard error.
- -o --output PATH Send the fetched resource to PATH instead of standard out.
- -k --keyfile PATH Path to the TLS key file to use.
- -c --certfile PATH Path to the TLS certificate file to use.
- -s --skip-verify Don't verify server TLS certificates.
- -u --upload Use stdin as the request body on supported protocols and don't follow redirects.
+ -h --help Show this screen.
+ -v --verbose Display more diagnostic information on standard error.
+ -o --output PATH Send the fetched resource to PATH instead of standard out.
+ -k --keyfile PATH Path to the TLS key file to use.
+ -c --certfile PATH Path to the TLS certificate file to use.
+ -s --skip-verify Don't verify server TLS certificates.
+ -t --timeout TIMEOUT Fail after the given timeout (like "15s").
+ -u --upload Use stdin as the request body on supported protocols and don't follow redirects.
`
func main() {
conf := configure()
cl := sliderule.NewClient(conf.clientTLS)
+ ctx := context.Background()
+ if conf.timeout != 0 {
+ var cancel context.CancelFunc
+ ctx, cancel = context.WithTimeout(ctx, conf.timeout)
+ defer cancel()
+ }
+
var response *sliderule.Response
var err error
if conf.upload {
- response, err = cl.Upload(conf.url.String(), os.Stdin)
+ response, err = cl.Upload(ctx, conf.url.String(), os.Stdin)
} else {
- response, err = cl.Fetch(conf.url.String())
+ response, err = cl.Fetch(ctx, conf.url.String())
}
if err != nil {
fail(err.Error() + "\n")
@@ -61,6 +79,7 @@ type config struct {
output io.WriteCloser
url *url.URL
clientTLS *tls.Config
+ timeout time.Duration
}
func configure() config {
@@ -72,6 +91,7 @@ func configure() config {
key := ""
cert := ""
verify := true
+ var err error
for i := 1; i <= len(os.Args)-1; i += 1 {
switch os.Args[i] {
@@ -87,12 +107,11 @@ func configure() config {
out := os.Args[i+1]
if out != "-" {
- output, err := os.OpenFile(out, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
+ conf.output, err = os.OpenFile(out, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644)
if err != nil {
fmt.Println(err.Error())
failf("'%s' is not a valid path\n", out)
}
- conf.output = output
}
i += 1
@@ -112,6 +131,16 @@ func configure() config {
cert = os.Args[i]
case "-s", "--skip-verify":
verify = false
+ case "-t", "--timeout":
+ if i+1 == len(os.Args)-1 {
+ fail(usage)
+ }
+
+ i += 1
+ conf.timeout, err = time.ParseDuration(os.Args[i])
+ if err != nil {
+ fail(err.Error())
+ }
case "-u", "--upload":
conf.upload = true
}