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