commit 76f31483b826f61e9ba05147572f6e7eae4eaea2
parent facd2ed2cd4c08812fdab614831f47c821640584
Author: phoebos <ben@bvnf.space>
Date: Sat, 28 Aug 2021 17:39:07 +0100
basic response code handling
Diffstat:
M | gemini.go | | | 40 | +++++++++++++++++++++++++++++++++++----- |
1 file changed, 35 insertions(+), 5 deletions(-)
diff --git a/gemini.go b/gemini.go
@@ -40,14 +40,44 @@ func main() {
}
defer conn.Close()
conn.Write([]byte(u + "\r\n"))
- resp, err := ioutil.ReadAll(bufio.NewReader(conn))
- if err != nil {
- fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
- os.Exit(2)
- }
+
+ // just dump the response if we want
if *plain {
+ resp, err := ioutil.ReadAll(bufio.NewReader(conn))
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
+ os.Exit(2)
+ }
fmt.Println(string(resp))
os.Exit(0)
}
+
+ scanner := bufio.NewScanner(conn)
+
+ // read first line (status)
+ scanner.Scan()
+ statusline := scanner.Text()
+ switch statusline[0] {
+ case '1':
+ // INPUT
+ case '2':
+ // SUCCESS
+ case '3':
+ // REDIRECT
+ fmt.Fprintf(os.Stderr, "redirecting to %s\n", statusline[3:])
+ case '6':
+ // CLIENT CERTIFICATE REQUIRED
+ default:
+ // reponse 4x or 5x
+ // FAILURE
+ fmt.Fprintf(os.Stderr, "Error: %s\n", statusline)
+ os.Exit(1)
+ }
+
+ resp, err := ioutil.ReadAll(bufio.NewReader(conn))
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
+ os.Exit(1)
+ }
fmt.Println(Prettify(string(resp)))
}