ldp.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package gold
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "crypto/rand"
  6. "encoding/hex"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "os"
  11. "strings"
  12. )
  13. type linkheader struct {
  14. uri string
  15. rel string
  16. }
  17. // Linkheaders holds the list of Link headers
  18. type Linkheaders struct {
  19. headers []*linkheader
  20. }
  21. type preferheader struct {
  22. omit []string
  23. include []string
  24. }
  25. // Preferheaders holds the list of Prefer headers
  26. type Preferheaders struct {
  27. headers []*preferheader
  28. }
  29. // ParsePreferHeader parses the LDP specific Prefer header
  30. func ParsePreferHeader(header string) *Preferheaders {
  31. ret := new(Preferheaders)
  32. for _, v := range strings.Split(header, ",") {
  33. item := new(preferheader)
  34. v = strings.TrimSpace(v)
  35. if strings.HasPrefix(v, "return=representation") {
  36. for _, s := range strings.Split(v, ";") {
  37. s = strings.TrimSpace(s)
  38. if strings.HasPrefix(s, "omit") {
  39. s = strings.TrimLeft(s, "omit=")
  40. s = strings.TrimLeft(s, "\"")
  41. s = strings.TrimRight(s, "\"")
  42. for _, u := range strings.Split(s, " ") {
  43. item.omit = append(item.omit, u)
  44. }
  45. }
  46. if strings.HasPrefix(s, "include") {
  47. s = strings.TrimLeft(s, "include=")
  48. s = strings.TrimLeft(s, "\"")
  49. s = strings.TrimRight(s, "\"")
  50. for _, u := range strings.Split(s, " ") {
  51. item.include = append(item.include, u)
  52. }
  53. }
  54. }
  55. ret.headers = append(ret.headers, item)
  56. }
  57. }
  58. return ret
  59. }
  60. // Omits returns the types of resources to omit when listing an LDPC
  61. func (p *Preferheaders) Omits() []string {
  62. var ret []string
  63. for _, v := range p.headers {
  64. for _, u := range v.omit {
  65. ret = append(ret, u)
  66. }
  67. }
  68. return ret
  69. }
  70. // Includes returns the types of resources to include when listing an LDPC
  71. func (p *Preferheaders) Includes() []string {
  72. var ret []string
  73. for _, v := range p.headers {
  74. for _, u := range v.include {
  75. ret = append(ret, u)
  76. }
  77. }
  78. return ret
  79. }
  80. // ParseLinkHeader is a generic Link header parser
  81. func ParseLinkHeader(header string) *Linkheaders {
  82. ret := new(Linkheaders)
  83. for _, v := range strings.Split(header, ", ") {
  84. item := new(linkheader)
  85. for _, s := range strings.Split(v, ";") {
  86. s = strings.TrimSpace(s)
  87. if strings.HasPrefix(s, "<") && strings.HasSuffix(s, ">") {
  88. s = strings.TrimLeft(s, "<")
  89. s = strings.TrimRight(s, ">")
  90. item.uri = s
  91. } else if strings.Index(s, "rel=") >= 0 {
  92. s = strings.TrimLeft(s, "rel=")
  93. if strings.HasPrefix(s, "\"") || strings.HasPrefix(s, "'") {
  94. s = s[1:]
  95. }
  96. if strings.HasSuffix(s, "\"") || strings.HasSuffix(s, "'") {
  97. s = s[:len(s)-1]
  98. }
  99. item.rel = s
  100. }
  101. }
  102. ret.headers = append(ret.headers, item)
  103. }
  104. return ret
  105. }
  106. // MatchRel attempts to match a Link header based on the rel value
  107. func (l *Linkheaders) MatchRel(rel string) string {
  108. for _, v := range l.headers {
  109. if v.rel == rel {
  110. return v.uri
  111. }
  112. }
  113. return ""
  114. }
  115. // MatchURI attempts to match a Link header based on the href value
  116. func (l *Linkheaders) MatchURI(uri string) bool {
  117. for _, v := range l.headers {
  118. if v.uri == uri {
  119. return true
  120. }
  121. }
  122. return false
  123. }
  124. // NewUUID generates a new UUID string
  125. func NewUUID() string {
  126. uuid := make([]byte, 16)
  127. io.ReadFull(rand.Reader, uuid)
  128. uuid[8] = uuid[8]&^0xc0 | 0x80
  129. uuid[6] = uuid[6]&^0xf0 | 0x40
  130. return hex.EncodeToString(uuid)
  131. }
  132. // NewETag generates ETag
  133. func NewETag(path string) (string, error) {
  134. var (
  135. hash []byte
  136. md5s string
  137. err error
  138. )
  139. stat, err := os.Stat(path)
  140. if err != nil {
  141. return "", err
  142. }
  143. if stat.IsDir() {
  144. if files, err := ioutil.ReadDir(path); err == nil {
  145. if len(files) == 0 {
  146. md5s += stat.ModTime().String()
  147. }
  148. for _, file := range files {
  149. md5s += file.ModTime().String() + fmt.Sprintf("%d", file.Size())
  150. }
  151. }
  152. } else {
  153. md5s += stat.ModTime().String() + fmt.Sprintf("%d", stat.Size())
  154. }
  155. h := md5.New()
  156. io.Copy(h, bytes.NewBufferString(md5s))
  157. hash = h.Sum([]byte(""))
  158. return hex.EncodeToString(hash), err
  159. }