http urls monitor.

resolve.go 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package yaml
  2. import (
  3. "encoding/base64"
  4. "math"
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type resolveMapItem struct {
  11. value interface{}
  12. tag string
  13. }
  14. var resolveTable = make([]byte, 256)
  15. var resolveMap = make(map[string]resolveMapItem)
  16. func init() {
  17. t := resolveTable
  18. t[int('+')] = 'S' // Sign
  19. t[int('-')] = 'S'
  20. for _, c := range "0123456789" {
  21. t[int(c)] = 'D' // Digit
  22. }
  23. for _, c := range "yYnNtTfFoO~" {
  24. t[int(c)] = 'M' // In map
  25. }
  26. t[int('.')] = '.' // Float (potentially in map)
  27. var resolveMapList = []struct {
  28. v interface{}
  29. tag string
  30. l []string
  31. }{
  32. {true, yaml_BOOL_TAG, []string{"y", "Y", "yes", "Yes", "YES"}},
  33. {true, yaml_BOOL_TAG, []string{"true", "True", "TRUE"}},
  34. {true, yaml_BOOL_TAG, []string{"on", "On", "ON"}},
  35. {false, yaml_BOOL_TAG, []string{"n", "N", "no", "No", "NO"}},
  36. {false, yaml_BOOL_TAG, []string{"false", "False", "FALSE"}},
  37. {false, yaml_BOOL_TAG, []string{"off", "Off", "OFF"}},
  38. {nil, yaml_NULL_TAG, []string{"", "~", "null", "Null", "NULL"}},
  39. {math.NaN(), yaml_FLOAT_TAG, []string{".nan", ".NaN", ".NAN"}},
  40. {math.Inf(+1), yaml_FLOAT_TAG, []string{".inf", ".Inf", ".INF"}},
  41. {math.Inf(+1), yaml_FLOAT_TAG, []string{"+.inf", "+.Inf", "+.INF"}},
  42. {math.Inf(-1), yaml_FLOAT_TAG, []string{"-.inf", "-.Inf", "-.INF"}},
  43. {"<<", yaml_MERGE_TAG, []string{"<<"}},
  44. }
  45. m := resolveMap
  46. for _, item := range resolveMapList {
  47. for _, s := range item.l {
  48. m[s] = resolveMapItem{item.v, item.tag}
  49. }
  50. }
  51. }
  52. const longTagPrefix = "tag:yaml.org,2002:"
  53. func shortTag(tag string) string {
  54. // TODO This can easily be made faster and produce less garbage.
  55. if strings.HasPrefix(tag, longTagPrefix) {
  56. return "!!" + tag[len(longTagPrefix):]
  57. }
  58. return tag
  59. }
  60. func longTag(tag string) string {
  61. if strings.HasPrefix(tag, "!!") {
  62. return longTagPrefix + tag[2:]
  63. }
  64. return tag
  65. }
  66. func resolvableTag(tag string) bool {
  67. switch tag {
  68. case "", yaml_STR_TAG, yaml_BOOL_TAG, yaml_INT_TAG, yaml_FLOAT_TAG, yaml_NULL_TAG, yaml_TIMESTAMP_TAG:
  69. return true
  70. }
  71. return false
  72. }
  73. var yamlStyleFloat = regexp.MustCompile(`^[-+]?[0-9]*\.?[0-9]+([eE][-+][0-9]+)?$`)
  74. func resolve(tag string, in string) (rtag string, out interface{}) {
  75. if !resolvableTag(tag) {
  76. return tag, in
  77. }
  78. defer func() {
  79. switch tag {
  80. case "", rtag, yaml_STR_TAG, yaml_BINARY_TAG:
  81. return
  82. case yaml_FLOAT_TAG:
  83. if rtag == yaml_INT_TAG {
  84. switch v := out.(type) {
  85. case int64:
  86. rtag = yaml_FLOAT_TAG
  87. out = float64(v)
  88. return
  89. case int:
  90. rtag = yaml_FLOAT_TAG
  91. out = float64(v)
  92. return
  93. }
  94. }
  95. }
  96. failf("cannot decode %s `%s` as a %s", shortTag(rtag), in, shortTag(tag))
  97. }()
  98. // Any data is accepted as a !!str or !!binary.
  99. // Otherwise, the prefix is enough of a hint about what it might be.
  100. hint := byte('N')
  101. if in != "" {
  102. hint = resolveTable[in[0]]
  103. }
  104. if hint != 0 && tag != yaml_STR_TAG && tag != yaml_BINARY_TAG {
  105. // Handle things we can lookup in a map.
  106. if item, ok := resolveMap[in]; ok {
  107. return item.tag, item.value
  108. }
  109. // Base 60 floats are a bad idea, were dropped in YAML 1.2, and
  110. // are purposefully unsupported here. They're still quoted on
  111. // the way out for compatibility with other parser, though.
  112. switch hint {
  113. case 'M':
  114. // We've already checked the map above.
  115. case '.':
  116. // Not in the map, so maybe a normal float.
  117. floatv, err := strconv.ParseFloat(in, 64)
  118. if err == nil {
  119. return yaml_FLOAT_TAG, floatv
  120. }
  121. case 'D', 'S':
  122. // Int, float, or timestamp.
  123. // Only try values as a timestamp if the value is unquoted or there's an explicit
  124. // !!timestamp tag.
  125. if tag == "" || tag == yaml_TIMESTAMP_TAG {
  126. t, ok := parseTimestamp(in)
  127. if ok {
  128. return yaml_TIMESTAMP_TAG, t
  129. }
  130. }
  131. plain := strings.Replace(in, "_", "", -1)
  132. intv, err := strconv.ParseInt(plain, 0, 64)
  133. if err == nil {
  134. if intv == int64(int(intv)) {
  135. return yaml_INT_TAG, int(intv)
  136. } else {
  137. return yaml_INT_TAG, intv
  138. }
  139. }
  140. uintv, err := strconv.ParseUint(plain, 0, 64)
  141. if err == nil {
  142. return yaml_INT_TAG, uintv
  143. }
  144. if yamlStyleFloat.MatchString(plain) {
  145. floatv, err := strconv.ParseFloat(plain, 64)
  146. if err == nil {
  147. return yaml_FLOAT_TAG, floatv
  148. }
  149. }
  150. if strings.HasPrefix(plain, "0b") {
  151. intv, err := strconv.ParseInt(plain[2:], 2, 64)
  152. if err == nil {
  153. if intv == int64(int(intv)) {
  154. return yaml_INT_TAG, int(intv)
  155. } else {
  156. return yaml_INT_TAG, intv
  157. }
  158. }
  159. uintv, err := strconv.ParseUint(plain[2:], 2, 64)
  160. if err == nil {
  161. return yaml_INT_TAG, uintv
  162. }
  163. } else if strings.HasPrefix(plain, "-0b") {
  164. intv, err := strconv.ParseInt("-" + plain[3:], 2, 64)
  165. if err == nil {
  166. if true || intv == int64(int(intv)) {
  167. return yaml_INT_TAG, int(intv)
  168. } else {
  169. return yaml_INT_TAG, intv
  170. }
  171. }
  172. }
  173. default:
  174. panic("resolveTable item not yet handled: " + string(rune(hint)) + " (with " + in + ")")
  175. }
  176. }
  177. return yaml_STR_TAG, in
  178. }
  179. // encodeBase64 encodes s as base64 that is broken up into multiple lines
  180. // as appropriate for the resulting length.
  181. func encodeBase64(s string) string {
  182. const lineLen = 70
  183. encLen := base64.StdEncoding.EncodedLen(len(s))
  184. lines := encLen/lineLen + 1
  185. buf := make([]byte, encLen*2+lines)
  186. in := buf[0:encLen]
  187. out := buf[encLen:]
  188. base64.StdEncoding.Encode(in, []byte(s))
  189. k := 0
  190. for i := 0; i < len(in); i += lineLen {
  191. j := i + lineLen
  192. if j > len(in) {
  193. j = len(in)
  194. }
  195. k += copy(out[k:], in[i:j])
  196. if lines > 1 {
  197. out[k] = '\n'
  198. k++
  199. }
  200. }
  201. return string(out[:k])
  202. }
  203. // This is a subset of the formats allowed by the regular expression
  204. // defined at http://yaml.org/type/timestamp.html.
  205. var allowedTimestampFormats = []string{
  206. "2006-1-2T15:4:5.999999999Z07:00", // RCF3339Nano with short date fields.
  207. "2006-1-2t15:4:5.999999999Z07:00", // RFC3339Nano with short date fields and lower-case "t".
  208. "2006-1-2 15:4:5.999999999", // space separated with no time zone
  209. "2006-1-2", // date only
  210. // Notable exception: time.Parse cannot handle: "2001-12-14 21:59:43.10 -5"
  211. // from the set of examples.
  212. }
  213. // parseTimestamp parses s as a timestamp string and
  214. // returns the timestamp and reports whether it succeeded.
  215. // Timestamp formats are defined at http://yaml.org/type/timestamp.html
  216. func parseTimestamp(s string) (time.Time, bool) {
  217. // TODO write code to check all the formats supported by
  218. // http://yaml.org/type/timestamp.html instead of using time.Parse.
  219. // Quick check: all date formats start with YYYY-.
  220. i := 0
  221. for ; i < len(s); i++ {
  222. if c := s[i]; c < '0' || c > '9' {
  223. break
  224. }
  225. }
  226. if i != 4 || i == len(s) || s[i] != '-' {
  227. return time.Time{}, false
  228. }
  229. for _, format := range allowedTimestampFormats {
  230. if t, err := time.Parse(format, s); err == nil {
  231. return t, true
  232. }
  233. }
  234. return time.Time{}, false
  235. }