http urls monitor.

connection.go 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Copyright (c) 2012-present The upper.io/db authors. All rights reserved.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining
  4. // a copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to
  8. // permit persons to whom the Software is furnished to do so, subject to
  9. // the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be
  12. // included in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. package mysql
  22. import (
  23. "errors"
  24. "fmt"
  25. "net"
  26. "net/url"
  27. "strings"
  28. )
  29. // From https://github.com/go-sql-driver/mysql/blob/master/utils.go
  30. var (
  31. errInvalidDSNUnescaped = errors.New("Invalid DSN: Did you forget to escape a param value?")
  32. errInvalidDSNAddr = errors.New("Invalid DSN: Network Address not terminated (missing closing brace)")
  33. errInvalidDSNNoSlash = errors.New("Invalid DSN: Missing the slash separating the database name")
  34. )
  35. // From https://github.com/go-sql-driver/mysql/blob/master/utils.go
  36. type config struct {
  37. user string
  38. passwd string
  39. net string
  40. addr string
  41. dbname string
  42. params map[string]string
  43. }
  44. // ConnectionURL implements a MySQL connection struct.
  45. type ConnectionURL struct {
  46. User string
  47. Password string
  48. Database string
  49. Host string
  50. Socket string
  51. Options map[string]string
  52. }
  53. func (c ConnectionURL) String() (s string) {
  54. if c.Database == "" {
  55. return ""
  56. }
  57. // Adding username.
  58. if c.User != "" {
  59. s = s + c.User
  60. // Adding password.
  61. if c.Password != "" {
  62. s = s + ":" + c.Password
  63. }
  64. s = s + "@"
  65. }
  66. // Adding protocol and address
  67. if c.Socket != "" {
  68. s = s + fmt.Sprintf("unix(%s)", c.Socket)
  69. } else if c.Host != "" {
  70. host, port, err := net.SplitHostPort(c.Host)
  71. if err != nil {
  72. host = c.Host
  73. port = "3306"
  74. }
  75. s = s + fmt.Sprintf("tcp(%s:%s)", host, port)
  76. }
  77. // Adding database
  78. s = s + "/" + c.Database
  79. // Do we have any options?
  80. if c.Options == nil {
  81. c.Options = map[string]string{}
  82. }
  83. // Default options.
  84. if _, ok := c.Options["charset"]; !ok {
  85. c.Options["charset"] = "utf8"
  86. }
  87. if _, ok := c.Options["parseTime"]; !ok {
  88. c.Options["parseTime"] = "true"
  89. }
  90. // Converting options into URL values.
  91. vv := url.Values{}
  92. for k, v := range c.Options {
  93. vv.Set(k, v)
  94. }
  95. // Inserting options.
  96. if p := vv.Encode(); p != "" {
  97. s = s + "?" + p
  98. }
  99. return s
  100. }
  101. // ParseURL parses s into a ConnectionURL struct.
  102. func ParseURL(s string) (conn ConnectionURL, err error) {
  103. var cfg *config
  104. if cfg, err = parseDSN(s); err != nil {
  105. return
  106. }
  107. conn.User = cfg.user
  108. conn.Password = cfg.passwd
  109. if cfg.net == "unix" {
  110. conn.Socket = cfg.addr
  111. } else if cfg.net == "tcp" {
  112. conn.Host = cfg.addr
  113. }
  114. conn.Database = cfg.dbname
  115. conn.Options = map[string]string{}
  116. for k, v := range cfg.params {
  117. conn.Options[k] = v
  118. }
  119. return
  120. }
  121. // from https://github.com/go-sql-driver/mysql/blob/master/utils.go
  122. // parseDSN parses the DSN string to a config
  123. func parseDSN(dsn string) (cfg *config, err error) {
  124. // New config with some default values
  125. cfg = &config{}
  126. // TODO: use strings.IndexByte when we can depend on Go 1.2
  127. // [user[:password]@][net[(addr)]]/dbname[?param1=value1&paramN=valueN]
  128. // Find the last '/' (since the password or the net addr might contain a '/')
  129. foundSlash := false
  130. for i := len(dsn) - 1; i >= 0; i-- {
  131. if dsn[i] == '/' {
  132. foundSlash = true
  133. var j, k int
  134. // left part is empty if i <= 0
  135. if i > 0 {
  136. // [username[:password]@][protocol[(address)]]
  137. // Find the last '@' in dsn[:i]
  138. for j = i; j >= 0; j-- {
  139. if dsn[j] == '@' {
  140. // username[:password]
  141. // Find the first ':' in dsn[:j]
  142. for k = 0; k < j; k++ {
  143. if dsn[k] == ':' {
  144. cfg.passwd = dsn[k+1 : j]
  145. break
  146. }
  147. }
  148. cfg.user = dsn[:k]
  149. break
  150. }
  151. }
  152. // [protocol[(address)]]
  153. // Find the first '(' in dsn[j+1:i]
  154. for k = j + 1; k < i; k++ {
  155. if dsn[k] == '(' {
  156. // dsn[i-1] must be == ')' if an address is specified
  157. if dsn[i-1] != ')' {
  158. if strings.ContainsRune(dsn[k+1:i], ')') {
  159. return nil, errInvalidDSNUnescaped
  160. }
  161. return nil, errInvalidDSNAddr
  162. }
  163. cfg.addr = dsn[k+1 : i-1]
  164. break
  165. }
  166. }
  167. cfg.net = dsn[j+1 : k]
  168. }
  169. // dbname[?param1=value1&...&paramN=valueN]
  170. // Find the first '?' in dsn[i+1:]
  171. for j = i + 1; j < len(dsn); j++ {
  172. if dsn[j] == '?' {
  173. if err = parseDSNParams(cfg, dsn[j+1:]); err != nil {
  174. return
  175. }
  176. break
  177. }
  178. }
  179. cfg.dbname = dsn[i+1 : j]
  180. break
  181. }
  182. }
  183. if !foundSlash && len(dsn) > 0 {
  184. return nil, errInvalidDSNNoSlash
  185. }
  186. // Set default network if empty
  187. if cfg.net == "" {
  188. cfg.net = "tcp"
  189. }
  190. // Set default address if empty
  191. if cfg.addr == "" {
  192. switch cfg.net {
  193. case "tcp":
  194. cfg.addr = "127.0.0.1:3306"
  195. case "unix":
  196. cfg.addr = "/tmp/mysql.sock"
  197. default:
  198. return nil, errors.New("Default addr for network '" + cfg.net + "' unknown")
  199. }
  200. }
  201. return
  202. }
  203. // From https://github.com/go-sql-driver/mysql/blob/master/utils.go
  204. // parseDSNParams parses the DSN "query string"
  205. // Values must be url.QueryEscape'ed
  206. func parseDSNParams(cfg *config, params string) (err error) {
  207. for _, v := range strings.Split(params, "&") {
  208. param := strings.SplitN(v, "=", 2)
  209. if len(param) != 2 {
  210. continue
  211. }
  212. value := param[1]
  213. // lazy init
  214. if cfg.params == nil {
  215. cfg.params = make(map[string]string)
  216. }
  217. if cfg.params[param[0]], err = url.QueryUnescape(value); err != nil {
  218. return
  219. }
  220. }
  221. return
  222. }