mirror of
https://git.sr.ht/~adnano/go-gemini
synced 2024-09-22 13:31:09 +02:00
Guarantee that (*Response).Body is non-nil
This commit is contained in:
parent
dad8f38bfb
commit
33c1dc435d
@ -76,17 +76,18 @@ func main() {
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.Status.Class() == gemini.StatusClassSuccess {
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Print(string(body))
|
||||
} else {
|
||||
log.Fatalf("request failed: %d %s: %s", resp.Status, resp.Status.Message(), resp.Meta)
|
||||
fmt.Printf("request failed: %d %s: %s", resp.Status, resp.Status.Message(), resp.Meta)
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,10 @@ package gemini
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
@ -19,6 +21,7 @@ type Response struct {
|
||||
Meta string
|
||||
|
||||
// Body contains the response body for successful responses.
|
||||
// Body is guaranteed to be non-nil.
|
||||
Body io.ReadCloser
|
||||
|
||||
// Request is the request that was sent to obtain this response.
|
||||
@ -83,6 +86,8 @@ func (resp *Response) read(rc io.ReadCloser) error {
|
||||
|
||||
if resp.Status.Class() == StatusClassSuccess {
|
||||
resp.Body = newReadCloserBody(br, rc)
|
||||
} else {
|
||||
resp.Body = ioutil.NopCloser(bytes.NewReader([]byte{}))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user