gin.go 13 KB

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