gin.go 15 KB

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