response: Move to methods
This commit is contained in:
parent
094c16297b
commit
3101856afa
35
response.go
35
response.go
@ -20,17 +20,10 @@ const defaultMediaType = "text/gemini; charset=utf-8"
|
|||||||
//
|
//
|
||||||
// It is the caller's responsibility to close the response.
|
// It is the caller's responsibility to close the response.
|
||||||
type Response struct {
|
type Response struct {
|
||||||
// Status contains the response status code.
|
status Status
|
||||||
Status Status
|
meta string
|
||||||
|
body io.ReadCloser
|
||||||
// Meta contains more information related to the response status.
|
conn net.Conn
|
||||||
// For successful responses, Meta should contain the media type of the response.
|
|
||||||
// For failure responses, Meta should contain a short description of the failure.
|
|
||||||
// Meta should not be longer than 1024 bytes.
|
|
||||||
Meta string
|
|
||||||
|
|
||||||
body io.ReadCloser
|
|
||||||
conn net.Conn
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadResponse reads a Gemini response from the provided io.ReadCloser.
|
// ReadResponse reads a Gemini response from the provided io.ReadCloser.
|
||||||
@ -47,7 +40,7 @@ func ReadResponse(r io.ReadCloser) (*Response, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, ErrInvalidResponse
|
return nil, ErrInvalidResponse
|
||||||
}
|
}
|
||||||
resp.Status = Status(status)
|
resp.status = Status(status)
|
||||||
|
|
||||||
// Read one space
|
// Read one space
|
||||||
if b, err := br.ReadByte(); err != nil {
|
if b, err := br.ReadByte(); err != nil {
|
||||||
@ -67,11 +60,11 @@ func ReadResponse(r io.ReadCloser) (*Response, error) {
|
|||||||
if len(meta) > 1024 {
|
if len(meta) > 1024 {
|
||||||
return nil, ErrInvalidResponse
|
return nil, ErrInvalidResponse
|
||||||
}
|
}
|
||||||
if resp.Status.Class() == StatusSuccess && meta == "" {
|
if resp.status.Class() == StatusSuccess && meta == "" {
|
||||||
// Use default media type
|
// Use default media type
|
||||||
meta = defaultMediaType
|
meta = defaultMediaType
|
||||||
}
|
}
|
||||||
resp.Meta = meta
|
resp.meta = meta
|
||||||
|
|
||||||
// Read terminating newline
|
// Read terminating newline
|
||||||
if b, err := br.ReadByte(); err != nil {
|
if b, err := br.ReadByte(); err != nil {
|
||||||
@ -80,7 +73,7 @@ func ReadResponse(r io.ReadCloser) (*Response, error) {
|
|||||||
return nil, ErrInvalidResponse
|
return nil, ErrInvalidResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.Status.Class() == StatusSuccess {
|
if resp.status.Class() == StatusSuccess {
|
||||||
resp.body = newBufReadCloser(br, r)
|
resp.body = newBufReadCloser(br, r)
|
||||||
} else {
|
} else {
|
||||||
resp.body = nopReadCloser{}
|
resp.body = nopReadCloser{}
|
||||||
@ -89,6 +82,18 @@ func ReadResponse(r io.ReadCloser) (*Response, error) {
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Status returns the response status code.
|
||||||
|
func (r *Response) Status() Status {
|
||||||
|
return r.status
|
||||||
|
}
|
||||||
|
|
||||||
|
// Meta returns the response meta.
|
||||||
|
// For successful responses, the meta should contain the media type of the response.
|
||||||
|
// For failure responses, the meta should contain a short description of the failure.
|
||||||
|
func (r *Response) Meta() string {
|
||||||
|
return r.meta
|
||||||
|
}
|
||||||
|
|
||||||
// Read reads data from the response body.
|
// Read reads data from the response body.
|
||||||
// The response body is streamed on demand as Read is called.
|
// The response body is streamed on demand as Read is called.
|
||||||
func (r *Response) Read(p []byte) (n int, err error) {
|
func (r *Response) Read(p []byte) (n int, err error) {
|
||||||
|
@ -91,11 +91,11 @@ func TestReadWriteResponse(t *testing.T) {
|
|||||||
// No response
|
// No response
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if resp.Status != test.Status {
|
if resp.status != test.Status {
|
||||||
t.Errorf("expected status = %d, got %d", test.Status, resp.Status)
|
t.Errorf("expected status = %d, got %d", test.Status, resp.status)
|
||||||
}
|
}
|
||||||
if resp.Meta != test.Meta {
|
if resp.meta != test.Meta {
|
||||||
t.Errorf("expected meta = %s, got %s", test.Meta, resp.Meta)
|
t.Errorf("expected meta = %s, got %s", test.Meta, resp.meta)
|
||||||
}
|
}
|
||||||
b, _ := ioutil.ReadAll(resp.body)
|
b, _ := ioutil.ReadAll(resp.body)
|
||||||
body := string(b)
|
body := string(b)
|
||||||
|
Loading…
Reference in New Issue
Block a user