GoPLS Viewer

Home|gopls/godoc/vfs/httpfs/httpfs.go
1// Copyright 2013 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// Package httpfs implements http.FileSystem using a godoc vfs.FileSystem.
6package httpfs // import "golang.org/x/tools/godoc/vfs/httpfs"
7
8import (
9    "fmt"
10    "io"
11    "net/http"
12    "os"
13
14    "golang.org/x/tools/godoc/vfs"
15)
16
17func New(fs vfs.FileSystemhttp.FileSystem {
18    return &httpFS{fs}
19}
20
21type httpFS struct {
22    fs vfs.FileSystem
23}
24
25func (h *httpFSOpen(name string) (http.Fileerror) {
26    fierr := h.fs.Stat(name)
27    if err != nil {
28        return nilerr
29    }
30    if fi.IsDir() {
31        return &httpDir{h.fsnamenil}, nil
32    }
33    ferr := h.fs.Open(name)
34    if err != nil {
35        return nilerr
36    }
37    return &httpFile{h.fsfname}, nil
38}
39
40// httpDir implements http.File for a directory in a FileSystem.
41type httpDir struct {
42    fs      vfs.FileSystem
43    name    string
44    pending []os.FileInfo
45}
46
47func (h *httpDirClose() error               { return nil }
48func (h *httpDirStat() (os.FileInfoerror) { return h.fs.Stat(h.name) }
49func (h *httpDirRead([]byte) (interror) {
50    return 0fmt.Errorf("cannot Read from directory %s"h.name)
51}
52
53func (h *httpDirSeek(offset int64whence int) (int64error) {
54    if offset == 0 && whence == 0 {
55        h.pending = nil
56        return 0nil
57    }
58    return 0fmt.Errorf("unsupported Seek in directory %s"h.name)
59}
60
61func (h *httpDirReaddir(count int) ([]os.FileInfoerror) {
62    if h.pending == nil {
63        derr := h.fs.ReadDir(h.name)
64        if err != nil {
65            return nilerr
66        }
67        if d == nil {
68            d = []os.FileInfo{} // not nil
69        }
70        h.pending = d
71    }
72
73    if len(h.pending) == 0 && count > 0 {
74        return nilio.EOF
75    }
76    if count <= 0 || count > len(h.pending) {
77        count = len(h.pending)
78    }
79    d := h.pending[:count]
80    h.pending = h.pending[count:]
81    return dnil
82}
83
84// httpFile implements http.File for a file (not directory) in a FileSystem.
85type httpFile struct {
86    fs vfs.FileSystem
87    vfs.ReadSeekCloser
88    name string
89}
90
91func (h *httpFileStat() (os.FileInfoerror) { return h.fs.Stat(h.name) }
92func (h *httpFileReaddir(int) ([]os.FileInfoerror) {
93    return nilfmt.Errorf("cannot Readdir from file %s"h.name)
94}
95
MembersX
fmt
httpFS.fs
httpDir.Readdir
httpDir.Readdir.count
httpDir.Readdir.BlockStmt.d
httpFile.Readdir.h
New.fs
httpFS.Open.f
httpDir.Stat
vfs
httpDir.Stat.h
httpDir.Seek.h
httpDir.Seek.offset
httpFile.fs
httpFile.name
httpFile
httpFile.Stat.h
io
New
httpFS.Open.h
httpDir.Close
httpDir.Seek
httpDir.Seek.whence
httpFS.Open.fi
httpDir.Close.h
httpFile.Readdir
http
os
httpFS.Open
httpDir.pending
httpDir.Readdir.h
httpDir.Readdir.BlockStmt.err
httpFS.Open.name
httpDir
httpDir.fs
httpDir.Read.h
httpDir.Read
httpFS
httpFS.Open.err
httpDir.name
httpFile.Stat
Members
X