gin.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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{"{{", "}}"},
  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, 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. return
  142. }
  143. func (engine *Engine) LoadHTMLFiles(files ...string) {
  144. if IsDebugging() {
  145. engine.HTMLRender = render.HTMLDebug{Files: files, FuncMap: engine.FuncMap, Delims: engine.delims}
  146. return
  147. }
  148. templ := template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).Funcs(engine.FuncMap).ParseFiles(files...))
  149. engine.SetHTMLTemplate(templ)
  150. return
  151. }
  152. func (engine *Engine) SetHTMLTemplate(templ *template.Template) {
  153. if len(engine.trees) > 0 {
  154. debugPrintWARNINGSetHTMLTemplate()
  155. }
  156. engine.HTMLRender = render.HTMLProduction{Template: templ.Funcs(engine.FuncMap)}
  157. }
  158. func (engine *Engine) SetFuncMap(funcMap template.FuncMap) {
  159. engine.FuncMap = funcMap
  160. }
  161. // NoRoute adds handlers for NoRoute. It return a 404 code by default.
  162. func (engine *Engine) NoRoute(handlers ...HandlerFunc) {
  163. engine.noRoute = handlers
  164. engine.rebuild404Handlers()
  165. }
  166. // NoMethod sets the handlers called when... TODO
  167. func (engine *Engine) NoMethod(handlers ...HandlerFunc) {
  168. engine.noMethod = handlers
  169. engine.rebuild405Handlers()
  170. }
  171. // Use attachs a global middleware to the router. ie. the middleware attached though Use() will be
  172. // included in the handlers chain for every single request. Even 404, 405, static files...
  173. // For example, this is the right place for a logger or error management middleware.
  174. func (engine *Engine) Use(middleware ...HandlerFunc) IRoutes {
  175. engine.RouterGroup.Use(middleware...)
  176. engine.rebuild404Handlers()
  177. engine.rebuild405Handlers()
  178. return engine
  179. }
  180. func (engine *Engine) rebuild404Handlers() {
  181. engine.allNoRoute = engine.combineHandlers(engine.noRoute)
  182. }
  183. func (engine *Engine) rebuild405Handlers() {
  184. engine.allNoMethod = engine.combineHandlers(engine.noMethod)
  185. }
  186. func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
  187. assert1(path[0] == '/', "path must begin with '/'")
  188. assert1(len(method) > 0, "HTTP method can not be empty")
  189. assert1(len(handlers) > 0, "there must be at least one handler")
  190. debugPrintRoute(method, path, handlers)
  191. root := engine.trees.get(method)
  192. if root == nil {
  193. root = new(node)
  194. engine.trees = append(engine.trees, methodTree{method: method, root: root})
  195. }
  196. root.addRoute(path, handlers)
  197. }
  198. // Routes returns a slice of registered routes, including some useful information, such as:
  199. // the http method, path and the handler name.
  200. func (engine *Engine) Routes() (routes RoutesInfo) {
  201. for _, tree := range engine.trees {
  202. routes = iterate("", tree.method, routes, tree.root)
  203. }
  204. return routes
  205. }
  206. func iterate(path, method string, routes RoutesInfo, root *node) RoutesInfo {
  207. path += root.path
  208. if len(root.handlers) > 0 {
  209. routes = append(routes, RouteInfo{
  210. Method: method,
  211. Path: path,
  212. Handler: nameOfFunction(root.handlers.Last()),
  213. })
  214. }
  215. for _, child := range root.children {
  216. routes = iterate(path, method, routes, child)
  217. }
  218. return routes
  219. }
  220. // Run attaches the router to a http.Server and starts listening and serving HTTP requests.
  221. // It is a shortcut for http.ListenAndServe(addr, router)
  222. // Note: this method will block the calling goroutine indefinitely unless an error happens.
  223. func (engine *Engine) Run(addr ...string) (err error) {
  224. defer func() { debugPrintError(err) }()
  225. address := resolveAddress(addr)
  226. debugPrint("Listening and serving HTTP on %s\n", address)
  227. err = http.ListenAndServe(address, engine)
  228. return
  229. }
  230. // RunTLS attaches the router to a http.Server and starts listening and serving HTTPS (secure) requests.
  231. // It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router)
  232. // Note: this method will block the calling goroutine indefinitely unless an error happens.
  233. func (engine *Engine) RunTLS(addr string, certFile string, keyFile string) (err error) {
  234. debugPrint("Listening and serving HTTPS on %s\n", addr)
  235. defer func() { debugPrintError(err) }()
  236. err = http.ListenAndServeTLS(addr, certFile, keyFile, engine)
  237. return
  238. }
  239. // RunUnix attaches the router to a http.Server and starts listening and serving HTTP requests
  240. // through the specified unix socket (ie. a file).
  241. // Note: this method will block the calling goroutine indefinitely unless an error happens.
  242. func (engine *Engine) RunUnix(file string) (err error) {
  243. debugPrint("Listening and serving HTTP on unix:/%s", file)
  244. defer func() { debugPrintError(err) }()
  245. os.Remove(file)
  246. listener, err := net.Listen("unix", file)
  247. if err != nil {
  248. return
  249. }
  250. defer listener.Close()
  251. err = http.Serve(listener, engine)
  252. return
  253. }
  254. // ServeHTTP conforms to the http.Handler interface.
  255. func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
  256. c := engine.pool.Get().(*Context)
  257. c.writermem.reset(w)
  258. c.Request = req
  259. c.reset()
  260. engine.handleHTTPRequest(c)
  261. engine.pool.Put(c)
  262. }
  263. // HandleContext re-enter a context that has been rewritten.
  264. // This can be done by setting c.Request.Path to your new target.
  265. // Disclaimer: You can loop yourself to death with this, use wisely.
  266. func (engine *Engine) HandleContext(c *Context) {
  267. c.reset()
  268. engine.handleHTTPRequest(c)
  269. engine.pool.Put(c)
  270. }
  271. func (engine *Engine) handleHTTPRequest(context *Context) {
  272. httpMethod := context.Request.Method
  273. var path string
  274. var unescape bool
  275. if engine.UseRawPath && len(context.Request.URL.RawPath) > 0 {
  276. path = context.Request.URL.RawPath
  277. unescape = engine.UnescapePathValues
  278. } else {
  279. path = context.Request.URL.Path
  280. unescape = false
  281. }
  282. // Find root of the tree for the given HTTP method
  283. t := engine.trees
  284. for i, tl := 0, len(t); i < tl; i++ {
  285. if t[i].method == httpMethod {
  286. root := t[i].root
  287. // Find route in tree
  288. handlers, params, tsr := root.getValue(path, context.Params, unescape)
  289. if handlers != nil {
  290. context.handlers = handlers
  291. context.Params = params
  292. context.Next()
  293. context.writermem.WriteHeaderNow()
  294. return
  295. }
  296. if httpMethod != "CONNECT" && path != "/" {
  297. if tsr && engine.RedirectTrailingSlash {
  298. redirectTrailingSlash(context)
  299. return
  300. }
  301. if engine.RedirectFixedPath && redirectFixedPath(context, root, engine.RedirectFixedPath) {
  302. return
  303. }
  304. }
  305. break
  306. }
  307. }
  308. if engine.HandleMethodNotAllowed {
  309. for _, tree := range engine.trees {
  310. if tree.method != httpMethod {
  311. if handlers, _, _ := tree.root.getValue(path, nil, unescape); handlers != nil {
  312. context.handlers = engine.allNoMethod
  313. serveError(context, 405, default405Body)
  314. return
  315. }
  316. }
  317. }
  318. }
  319. context.handlers = engine.allNoRoute
  320. serveError(context, 404, default404Body)
  321. }
  322. var mimePlain = []string{MIMEPlain}
  323. func serveError(c *Context, code int, defaultMessage []byte) {
  324. c.writermem.status = code
  325. c.Next()
  326. if !c.writermem.Written() {
  327. if c.writermem.Status() == code {
  328. c.writermem.Header()["Content-Type"] = mimePlain
  329. c.Writer.Write(defaultMessage)
  330. } else {
  331. c.writermem.WriteHeaderNow()
  332. }
  333. }
  334. }
  335. func redirectTrailingSlash(c *Context) {
  336. req := c.Request
  337. path := req.URL.Path
  338. code := 301 // Permanent redirect, request with GET method
  339. if req.Method != "GET" {
  340. code = 307
  341. }
  342. if len(path) > 1 && path[len(path)-1] == '/' {
  343. req.URL.Path = path[:len(path)-1]
  344. } else {
  345. req.URL.Path = path + "/"
  346. }
  347. debugPrint("redirecting request %d: %s --> %s", code, path, req.URL.String())
  348. http.Redirect(c.Writer, req, req.URL.String(), code)
  349. c.writermem.WriteHeaderNow()
  350. }
  351. func redirectFixedPath(c *Context, root *node, trailingSlash bool) bool {
  352. req := c.Request
  353. path := req.URL.Path
  354. fixedPath, found := root.findCaseInsensitivePath(
  355. cleanPath(path),
  356. trailingSlash,
  357. )
  358. if found {
  359. code := 301 // Permanent redirect, request with GET method
  360. if req.Method != "GET" {
  361. code = 307
  362. }
  363. req.URL.Path = string(fixedPath)
  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. return true
  368. }
  369. return false
  370. }