gin.go 15 KB

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