From 65cbf62f8aade963bec31d0a753fa401716c8e47 Mon Sep 17 00:00:00 2001 From: James Mills Date: Thu, 22 Sep 2016 23:50:44 +1000 Subject: [PATCH] Improve directory handling --- gopher.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/gopher.go b/gopher.go index c52c4b4..5dc0bfe 100644 --- a/gopher.go +++ b/gopher.go @@ -210,6 +210,17 @@ func (i *Item) parse(line []byte) error { return nil } +func (i *Item) isDirectoryLike() bool { + switch i.Type { + case DIRECTORY: + return true + case INDEXSEARCH: + return true + default: + return false + } +} + // Directory representes a Gopher Menu of Items type Directory []Item @@ -290,22 +301,20 @@ func Get(uri string) (*Response, error) { i := Item{Type: Type, Selector: Selector, Host: host, Port: port} res := Response{Type: i.Type} - if i.Type == ItemType(DIRECTORY) { + if i.isDirectoryLike() { d, err := i.FetchDirectory() if err != nil { return nil, err } res.Dir = d - } else if i.Type == ItemType(FILE) { + } else { reader, err := i.FetchFile() if err != nil { return nil, err } res.Body = reader - } else { - return nil, fmt.Errorf("unsupported type: %s", i.Type) } return &res, nil @@ -319,10 +328,6 @@ func (i *Item) FetchFile() (io.Reader, error) { return nil, errors.New("cannot fetch a directory as a file") } - if i.Type != FILE { - return nil, errors.New("non-plaintext encodings not supported") - } - conn, err := net.Dial("tcp", i.Host+":"+strconv.Itoa(i.Port)) if err != nil { return nil, err @@ -340,7 +345,7 @@ func (i *Item) FetchFile() (io.Reader, error) { // FetchDirectory fetches directory information, not data. // Calling this on an Item whose type is not DIRECTORY will return an error. func (i *Item) FetchDirectory() (Directory, error) { - if i.Type != DIRECTORY { + if !i.isDirectoryLike() { return nil, errors.New("cannot fetch a file as a directory") }