http urls monitor.

reference.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2013 sigu-399 ( https://github.com/sigu-399 )
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // author sigu-399
  15. // author-github https://github.com/sigu-399
  16. // author-mail sigu.399@gmail.com
  17. //
  18. // repository-name jsonreference
  19. // repository-desc An implementation of JSON Reference - Go language
  20. //
  21. // description Main and unique file.
  22. //
  23. // created 26-02-2013
  24. package jsonreference
  25. import (
  26. "errors"
  27. "net/url"
  28. "strings"
  29. "github.com/PuerkitoBio/purell"
  30. "github.com/go-openapi/jsonpointer"
  31. )
  32. const (
  33. fragmentRune = `#`
  34. )
  35. // New creates a new reference for the given string
  36. func New(jsonReferenceString string) (Ref, error) {
  37. var r Ref
  38. err := r.parse(jsonReferenceString)
  39. return r, err
  40. }
  41. // MustCreateRef parses the ref string and panics when it's invalid.
  42. // Use the New method for a version that returns an error
  43. func MustCreateRef(ref string) Ref {
  44. r, err := New(ref)
  45. if err != nil {
  46. panic(err)
  47. }
  48. return r
  49. }
  50. // Ref represents a json reference object
  51. type Ref struct {
  52. referenceURL *url.URL
  53. referencePointer jsonpointer.Pointer
  54. HasFullURL bool
  55. HasURLPathOnly bool
  56. HasFragmentOnly bool
  57. HasFileScheme bool
  58. HasFullFilePath bool
  59. }
  60. // GetURL gets the URL for this reference
  61. func (r *Ref) GetURL() *url.URL {
  62. return r.referenceURL
  63. }
  64. // GetPointer gets the json pointer for this reference
  65. func (r *Ref) GetPointer() *jsonpointer.Pointer {
  66. return &r.referencePointer
  67. }
  68. // String returns the best version of the url for this reference
  69. func (r *Ref) String() string {
  70. if r.referenceURL != nil {
  71. return r.referenceURL.String()
  72. }
  73. if r.HasFragmentOnly {
  74. return fragmentRune + r.referencePointer.String()
  75. }
  76. return r.referencePointer.String()
  77. }
  78. // IsRoot returns true if this reference is a root document
  79. func (r *Ref) IsRoot() bool {
  80. return r.referenceURL != nil &&
  81. !r.IsCanonical() &&
  82. !r.HasURLPathOnly &&
  83. r.referenceURL.Fragment == ""
  84. }
  85. // IsCanonical returns true when this pointer starts with http(s):// or file://
  86. func (r *Ref) IsCanonical() bool {
  87. return (r.HasFileScheme && r.HasFullFilePath) || (!r.HasFileScheme && r.HasFullURL)
  88. }
  89. // "Constructor", parses the given string JSON reference
  90. func (r *Ref) parse(jsonReferenceString string) error {
  91. parsed, err := url.Parse(jsonReferenceString)
  92. if err != nil {
  93. return err
  94. }
  95. r.referenceURL, _ = url.Parse(purell.NormalizeURL(parsed, purell.FlagsSafe|purell.FlagRemoveDuplicateSlashes))
  96. refURL := r.referenceURL
  97. if refURL.Scheme != "" && refURL.Host != "" {
  98. r.HasFullURL = true
  99. } else {
  100. if refURL.Path != "" {
  101. r.HasURLPathOnly = true
  102. } else if refURL.RawQuery == "" && refURL.Fragment != "" {
  103. r.HasFragmentOnly = true
  104. }
  105. }
  106. r.HasFileScheme = refURL.Scheme == "file"
  107. r.HasFullFilePath = strings.HasPrefix(refURL.Path, "/")
  108. // invalid json-pointer error means url has no json-pointer fragment. simply ignore error
  109. r.referencePointer, _ = jsonpointer.New(refURL.Fragment)
  110. return nil
  111. }
  112. // Inherits creates a new reference from a parent and a child
  113. // If the child cannot inherit from the parent, an error is returned
  114. func (r *Ref) Inherits(child Ref) (*Ref, error) {
  115. childURL := child.GetURL()
  116. parentURL := r.GetURL()
  117. if childURL == nil {
  118. return nil, errors.New("child url is nil")
  119. }
  120. if parentURL == nil {
  121. return &child, nil
  122. }
  123. ref, err := New(parentURL.ResolveReference(childURL).String())
  124. if err != nil {
  125. return nil, err
  126. }
  127. return &ref, nil
  128. }