http urls monitor.

gin.go 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package gin
  5. import (
  6. "html/template"
  7. "net"
  8. "net/http"
  9. "os"
  10. "sync"
  11. "github.com/gin-gonic/gin/render"
  12. )
  13. const (
  14. // Version is Framework's version.
  15. Version = "v1.3.0"
  16. defaultMultipartMemory = 32 << 20 // 32 MB
  17. )
  18. var (
  19. default404Body = []byte("404 page not found")
  20. default405Body = []byte("405 method not allowed")
  21. defaultAppEngine bool
  22. )
  23. type HandlerFunc func(*Context)
  24. type HandlersChain []HandlerFunc
  25. // Last returns the last handler in the chain. ie. the last handler is the main own.
  26. func (c HandlersChain) Last() HandlerFunc {
  27. if length := len(c); length > 0 {
  28. return c[length-1]
  29. }
  30. return nil
  31. }
  32. type RouteInfo struct {
  33. Method string
  34. Path string
  35. Handler string
  36. }
  37. type RoutesInfo []RouteInfo
  38. // Engine is the framework's instance, it contains the muxer, middleware and configuration settings.
  39. // Create an instance of Engine, by using New() or Default()
  40. type Engine struct {
  41. RouterGroup
  42. // Enables automatic redirection if the current route can't be matched but a
  43. // handler for the path with (without) the trailing slash exists.
  44. // For example if /foo/ is requested but a route only exists for /foo, the
  45. // client is redirected to /foo with http status code 301 for GET requests
  46. // and 307 for all other request methods.
  47. RedirectTrailingSlash bool
  48. // If enabled, the router tries to fix the current request path, if no
  49. // handle is registered for it.
  50. // First superfluous path elements like ../ or // are removed.
  51. // Afterwards the router does a case-insensitive lookup of the cleaned path.
  52. // If a handle can be found for this route, the router makes a redirection
  53. // to the corrected path with status code 301 for GET requests and 307 for
  54. // all other request methods.
  55. // For example /FOO and /..//Foo could be redirected to /foo.
  56. // RedirectTrailingSlash is independent of this option.
  57. RedirectFixedPath bool
  58. // If enabled, the router checks if another method is allowed for the
  59. // current route, if the current request can not be routed.
  60. // If this is the case, the request is answered with 'Method Not Allowed'
  61. // and HTTP status code 405.
  62. // If no other Method is allowed, the request is delegated to the NotFound
  63. // handler.
  64. HandleMethodNotAllowed bool
  65. ForwardedByClientIP bool
  66. // #726 #755 If enabled, it will thrust some headers starting with
  67. // 'X-AppEngine...' for better integration with that PaaS.
  68. AppEngine bool
  69. // If enabled, the url.RawPath will be used to find parameters.
  70. UseRawPath bool
  71. // If true, the path value will be unescaped.
  72. // If UseRawPath is false (by default), the UnescapePathValues effectively is true,
  73. // as url.Path gonna be used, which is already unescaped.
  74. UnescapePathValues bool
  75. // Value of 'maxMemory' param that is given to http.Request's ParseMultipartForm
  76. // method call.
  77. MaxMultipartMemory int64
  78. delims render.Delims
  79. secureJsonPrefix string
  80. HTMLRender render.HTMLRender
  81. FuncMap template.FuncMap
  82. allNoRoute HandlersChain
  83. allNoMethod HandlersChain
  84. noRoute HandlersChain
  85. noMethod HandlersChain
  86. pool sync.Pool
  87. trees methodTrees
  88. }
  89. var _ IRouter = &Engine{}
  90. // New returns a new blank Engine instance without any middleware attached.
  91. // By default the configuration is:
  92. // - RedirectTrailingSlash: true
  93. // - RedirectFixedPath: false
  94. // - HandleMethodNotAllowed: false
  95. // - ForwardedByClientIP: true
  96. // - UseRawPath: false
  97. // - UnescapePathValues: true
  98. func New() *Engine {
  99. debugPrintWARNINGNew()
  100. engine := &Engine{
  101. RouterGroup: RouterGroup{
  102. Handlers: nil,
  103. basePath: "/",
  104. root: true,
  105. },
  106. FuncMap: template.FuncMap{},
  107. RedirectTrailingSlash: true,
  108. RedirectFixedPath: false,
  109. HandleMethodNotAllowed: false,
  110. ForwardedByClientIP: true,
  111. AppEngine: defaultAppEngine,
  112. UseRawPath: false,
  113. UnescapePathValues: true,
  114. MaxMultipartMemory: defaultMultipartMemory,
  115. trees: make(methodTrees, 0, 9),
  116. delims: render.Delims{Left: "{{", Right: "}}"},
  117. secureJsonPrefix: "while(1);",
  118. }
  119. engine.RouterGroup.engine = engine
  120. engine.pool.New = func() interface{} {
  121. return engine.allocateContext()
  122. }
  123. return engine
  124. }
  125. // Default returns an Engine instance with the Logger and Recovery middleware already attached.
  126. func Default() *Engine {
  127. debugPrintWARNINGDefault()
  128. engine := New()
  129. engine.Use(Logger(), Recovery())
  130. return engine
  131. }
  132. func (engine *Engine) allocateContext() *Context {
  133. return &Context{engine: engine}
  134. }
  135. func (engine *Engine) Delims(left, right string) *Engine {
  136. engine.delims = render.Delims{Left: left, Right: right}
  137. return engine
  138. }
  139. // SecureJsonPrefix sets the secureJsonPrefix used in Context.SecureJSON.
  140. func (engine *Engine) SecureJsonPrefix(prefix string) *Engine {
  141. engine.secureJsonPrefix = prefix
  142. return engine
  143. }
  144. // LoadHTMLGlob loads HTML files identified by glob pattern
  145. // and associates the result with HTML renderer.
  146. func (engine *Engine) LoadHTMLGlob(pattern string) {
  147. left := engine.delims.Left
  148. right := engine.delims.Right
  149. templ := template.Must(template.New("").Delims(left, right).Funcs(engine.FuncMap).ParseGlob(pattern))
  150. if IsDebugging() {
  151. debugPrintLoadTemplate(templ)
  152. engine.HTMLRender = render.HTMLDebug{Glob: pattern, FuncMap: engine.FuncMap, Delims: engine.delims}
  153. return
  154. }
  155. engine.SetHTMLTemplate(templ)
  156. }
  157. // LoadHTMLFiles loads a slice of HTML files
  158. // and associates the result with HTML renderer.
  159. func (engine *Engine) LoadHTMLFiles(files ...string) {
  160. if IsDebugging() {
  161. engine.HTMLRender = render.HTMLDebug{Files: files, FuncMap: engine.FuncMap, Delims: engine.delims}
  162. return
  163. }
  164. templ := template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).Funcs(engine.FuncMap).ParseFiles(files...))
  165. engine.SetHTMLTemplate(templ)
  166. }
  167. // SetHTMLTemplate associate a template with HTML renderer.
  168. func (engine *Engine) SetHTMLTemplate(templ *template.Template) {
  169. if len(engine.trees) > 0 {
  170. debugPrintWARNINGSetHTMLTemplate()
  171. }
  172. engine.HTMLRender = render.HTMLProduction{Template: templ.Funcs(engine.FuncMap)}
  173. }
  174. // SetFuncMap sets the FuncMap used for template.FuncMap.
  175. func (engine *Engine) SetFuncMap(funcMap template.FuncMap) {
  176. engine.FuncMap = funcMap
  177. }
  178. // NoRoute adds handlers for NoRoute. It return a 404 code by default.
  179. func (engine *Engine) NoRoute(handlers ...HandlerFunc) {
  180. engine.noRoute = handlers
  181. engine.rebuild404Handlers()
  182. }
  183. // NoMethod sets the handlers called when... TODO.
  184. func (engine *Engine) NoMethod(handlers ...HandlerFunc) {
  185. engine.noMethod = handlers
  186. engine.rebuild405Handlers()
  187. }
  188. // Use attachs a global middleware to the router. ie. the middleware attached though Use() will be
  189. // included in the handlers chain for every single request. Even 404, 405, static files...
  190. // For example, this is the right place for a logger or error management middleware.
  191. func (engine *Engine) Use(middleware ...HandlerFunc) IRoutes {
  192. engine.RouterGroup.Use(middleware...)
  193. engine.rebuild404Handlers()
  194. engine.rebuild405Handlers()
  195. return engine
  196. }
  197. func (engine *Engine) rebuild404Handlers() {
  198. engine.allNoRoute = engine.combineHandlers(engine.noRoute)
  199. }
  200. func (engine *Engine) rebuild405Handlers() {
  201. engine.allNoMethod = engine.combineHandlers(engine.noMethod)
  202. }
  203. func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
  204. assert1(path[0] == '/', "path must begin with '/'")
  205. assert1(method != "", "HTTP method can not be empty")
  206. assert1(len(handlers) > 0, "there must be at least one handler")
  207. debugPrintRoute(method, path, handlers)
  208. root := engine.trees.get(method)
  209. if root == nil {
  210. root = new(node)
  211. engine.trees = append(engine.trees, methodTree{method: method, root: root})
  212. }
  213. root.addRoute(path, handlers)
  214. }
  215. // Routes returns a slice of registered routes, including some useful information, such as:
  216. // the http method, path and the handler name.
  217. func (engine *Engine) Routes() (routes RoutesInfo) {
  218. for _, tree := range engine.trees {
  219. routes = iterate("", tree.method, routes, tree.root)
  220. }
  221. return routes
  222. }
  223. func iterate(path, method string, routes RoutesInfo, root *node) RoutesInfo {
  224. path += root.path
  225. if len(root.handlers) > 0 {
  226. routes = append(routes, RouteInfo{
  227. Method: method,
  228. Path: path,
  229. Handler: nameOfFunction(root.handlers.Last()),
  230. })
  231. }
  232. for _, child := range root.children {
  233. routes = iterate(path, method, routes, child)
  234. }
  235. return routes
  236. }
  237. // Run attaches the router to a http.Server and starts listening and serving HTTP requests.
  238. // It is a shortcut for http.ListenAndServe(addr, router)
  239. // Note: this method will block the calling goroutine indefinitely unless an error happens.
  240. func (engine *Engine) Run(addr ...string) (err error) {
  241. defer func() { debugPrintError(err) }()
  242. address := resolveAddress(addr)
  243. debugPrint("Listening and serving HTTP on %s\n", address)
  244. err = http.ListenAndServe(address, engine)
  245. return
  246. }
  247. // RunTLS attaches the router to a http.Server and starts listening and serving HTTPS (secure) requests.
  248. // It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router)
  249. // Note: this method will block the calling goroutine indefinitely unless an error happens.
  250. func (engine *Engine) RunTLS(addr, certFile, keyFile string) (err error) {
  251. debugPrint("Listening and serving HTTPS on %s\n", addr)
  252. defer func() { debugPrintError(err) }()
  253. err = http.ListenAndServeTLS(addr, certFile, keyFile, engine)
  254. return
  255. }
  256. // RunUnix attaches the router to a http.Server and starts listening and serving HTTP requests
  257. // through the specified unix socket (ie. a file).
  258. // Note: this method will block the calling goroutine indefinitely unless an error happens.
  259. func (engine *Engine) RunUnix(file string) (err error) {
  260. debugPrint("Listening and serving HTTP on unix:/%s", file)
  261. defer func() { debugPrintError(err) }()
  262. os.Remove(file)
  263. listener, err := net.Listen("unix", file)
  264. if err != nil {
  265. return
  266. }
  267. defer listener.Close()
  268. err = http.Serve(listener, engine)
  269. return
  270. }
  271. // ServeHTTP conforms to the http.Handler interface.
  272. func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
  273. c := engine.pool.Get().(*Context)
  274. c.writermem.reset(w)
  275. c.Request = req
  276. c.reset()
  277. engine.handleHTTPRequest(c)
  278. engine.pool.Put(c)
  279. }
  280. // HandleContext re-enter a context that has been rewritten.
  281. // This can be done by setting c.Request.URL.Path to your new target.
  282. // Disclaimer: You can loop yourself to death with this, use wisely.
  283. func (engine *Engine) HandleContext(c *Context) {
  284. c.reset()
  285. engine.handleHTTPRequest(c)
  286. engine.pool.Put(c)
  287. }
  288. func (engine *Engine) handleHTTPRequest(c *Context) {
  289. httpMethod := c.Request.Method
  290. path := c.Request.URL.Path
  291. unescape := false
  292. if engine.UseRawPath && len(c.Request.URL.RawPath) > 0 {
  293. path = c.Request.URL.RawPath
  294. unescape = engine.UnescapePathValues
  295. }
  296. // Find root of the tree for the given HTTP method
  297. t := engine.trees
  298. for i, tl := 0, len(t); i < tl; i++ {
  299. if t[i].method != httpMethod {
  300. continue
  301. }
  302. root := t[i].root
  303. // Find route in tree
  304. handlers, params, tsr := root.getValue(path, c.Params, unescape)
  305. if handlers != nil {
  306. c.handlers = handlers
  307. c.Params = params
  308. c.Next()
  309. c.writermem.WriteHeaderNow()
  310. return
  311. }
  312. if httpMethod != "CONNECT" && path != "/" {
  313. if tsr && engine.RedirectTrailingSlash {
  314. redirectTrailingSlash(c)
  315. return
  316. }
  317. if engine.RedirectFixedPath && redirectFixedPath(c, root, engine.RedirectFixedPath) {
  318. return
  319. }
  320. }
  321. break
  322. }
  323. if engine.HandleMethodNotAllowed {
  324. for _, tree := range engine.trees {
  325. if tree.method == httpMethod {
  326. continue
  327. }
  328. if handlers, _, _ := tree.root.getValue(path, nil, unescape); handlers != nil {
  329. c.handlers = engine.allNoMethod
  330. serveError(c, http.StatusMethodNotAllowed, default405Body)
  331. return
  332. }
  333. }
  334. }
  335. c.handlers = engine.allNoRoute
  336. serveError(c, http.StatusNotFound, default404Body)
  337. }
  338. var mimePlain = []string{MIMEPlain}
  339. func serveError(c *Context, code int, defaultMessage []byte) {
  340. c.writermem.status = code
  341. c.Next()
  342. if c.writermem.Written() {
  343. return
  344. }
  345. if c.writermem.Status() == code {
  346. c.writermem.Header()["Content-Type"] = mimePlain
  347. c.Writer.Write(defaultMessage)
  348. return
  349. }
  350. c.writermem.WriteHeaderNow()
  351. return
  352. }
  353. func redirectTrailingSlash(c *Context) {
  354. req := c.Request
  355. path := req.URL.Path
  356. code := http.StatusMovedPermanently // Permanent redirect, request with GET method
  357. if req.Method != "GET" {
  358. code = http.StatusTemporaryRedirect
  359. }
  360. req.URL.Path = path + "/"
  361. if length := len(path); length > 1 && path[length-1] == '/' {
  362. req.URL.Path = path[:length-1]
  363. }
  364. debugPrint("redirecting request %d: %s --> %s", code, path, req.URL.String())
  365. http.Redirect(c.Writer, req, req.URL.String(), code)
  366. c.writermem.WriteHeaderNow()
  367. }
  368. func redirectFixedPath(c *Context, root *node, trailingSlash bool) bool {
  369. req := c.Request
  370. path := req.URL.Path
  371. if fixedPath, ok := root.findCaseInsensitivePath(cleanPath(path), trailingSlash); ok {
  372. code := http.StatusMovedPermanently // Permanent redirect, request with GET method
  373. if req.Method != "GET" {
  374. code = http.StatusTemporaryRedirect
  375. }
  376. req.URL.Path = string(fixedPath)
  377. debugPrint("redirecting request %d: %s --> %s", code, path, req.URL.String())
  378. http.Redirect(c.Writer, req, req.URL.String(), code)
  379. c.writermem.WriteHeaderNow()
  380. return true
  381. }
  382. return false
  383. }