http urls monitor.

httpFs.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright © 2014 Steve Francia <spf@spf13.com>.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package afero
  14. import (
  15. "errors"
  16. "net/http"
  17. "os"
  18. "path"
  19. "path/filepath"
  20. "strings"
  21. "time"
  22. )
  23. type httpDir struct {
  24. basePath string
  25. fs HttpFs
  26. }
  27. func (d httpDir) Open(name string) (http.File, error) {
  28. if filepath.Separator != '/' && strings.IndexRune(name, filepath.Separator) >= 0 ||
  29. strings.Contains(name, "\x00") {
  30. return nil, errors.New("http: invalid character in file path")
  31. }
  32. dir := string(d.basePath)
  33. if dir == "" {
  34. dir = "."
  35. }
  36. f, err := d.fs.Open(filepath.Join(dir, filepath.FromSlash(path.Clean("/"+name))))
  37. if err != nil {
  38. return nil, err
  39. }
  40. return f, nil
  41. }
  42. type HttpFs struct {
  43. source Fs
  44. }
  45. func NewHttpFs(source Fs) *HttpFs {
  46. return &HttpFs{source: source}
  47. }
  48. func (h HttpFs) Dir(s string) *httpDir {
  49. return &httpDir{basePath: s, fs: h}
  50. }
  51. func (h HttpFs) Name() string { return "h HttpFs" }
  52. func (h HttpFs) Create(name string) (File, error) {
  53. return h.source.Create(name)
  54. }
  55. func (h HttpFs) Chmod(name string, mode os.FileMode) error {
  56. return h.source.Chmod(name, mode)
  57. }
  58. func (h HttpFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
  59. return h.source.Chtimes(name, atime, mtime)
  60. }
  61. func (h HttpFs) Mkdir(name string, perm os.FileMode) error {
  62. return h.source.Mkdir(name, perm)
  63. }
  64. func (h HttpFs) MkdirAll(path string, perm os.FileMode) error {
  65. return h.source.MkdirAll(path, perm)
  66. }
  67. func (h HttpFs) Open(name string) (http.File, error) {
  68. f, err := h.source.Open(name)
  69. if err == nil {
  70. if httpfile, ok := f.(http.File); ok {
  71. return httpfile, nil
  72. }
  73. }
  74. return nil, err
  75. }
  76. func (h HttpFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
  77. return h.source.OpenFile(name, flag, perm)
  78. }
  79. func (h HttpFs) Remove(name string) error {
  80. return h.source.Remove(name)
  81. }
  82. func (h HttpFs) RemoveAll(path string) error {
  83. return h.source.RemoveAll(path)
  84. }
  85. func (h HttpFs) Rename(oldname, newname string) error {
  86. return h.source.Rename(oldname, newname)
  87. }
  88. func (h HttpFs) Stat(name string) (os.FileInfo, error) {
  89. return h.source.Stat(name)
  90. }