gin.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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. "strings"
  13. "sync"
  14. "github.com/gin-gonic/gin/internal/bytesconv"
  15. "github.com/gin-gonic/gin/render"
  16. "golang.org/x/net/http2"
  17. "golang.org/x/net/http2/h2c"
  18. )
  19. const defaultMultipartMemory = 32 << 20 // 32 MB
  20. var (
  21. default404Body = []byte("404 page not found")
  22. default405Body = []byte("405 method not allowed")
  23. )
  24. var defaultPlatform string
  25. var defaultTrustedCIDRs = []*net.IPNet{
  26. { // 0.0.0.0/0 (IPv4)
  27. IP: net.IP{0x0, 0x0, 0x0, 0x0},
  28. Mask: net.IPMask{0x0, 0x0, 0x0, 0x0},
  29. },
  30. { // ::/0 (IPv6)
  31. IP: net.IP{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
  32. Mask: net.IPMask{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
  33. },
  34. }
  35. // HandlerFunc defines the handler used by gin middleware as return value.
  36. type HandlerFunc func(*Context)
  37. // HandlersChain defines a HandlerFunc slice.
  38. type HandlersChain []HandlerFunc
  39. // Last returns the last handler in the chain. i.e. the last handler is the main one.
  40. func (c HandlersChain) Last() HandlerFunc {
  41. if length := len(c); length > 0 {
  42. return c[length-1]
  43. }
  44. return nil
  45. }
  46. // RouteInfo represents a request route's specification which contains method and path and its handler.
  47. type RouteInfo struct {
  48. Method string
  49. Path string
  50. Handler string
  51. HandlerFunc HandlerFunc
  52. }
  53. // RoutesInfo defines a RouteInfo slice.
  54. type RoutesInfo []RouteInfo
  55. // Trusted platforms
  56. const (
  57. // PlatformGoogleAppEngine when running on Google App Engine. Trust X-Appengine-Remote-Addr
  58. // for determining the client's IP
  59. PlatformGoogleAppEngine = "X-Appengine-Remote-Addr"
  60. // PlatformCloudflare when using Cloudflare's CDN. Trust CF-Connecting-IP for determining
  61. // the client's IP
  62. PlatformCloudflare = "CF-Connecting-IP"
  63. )
  64. // Engine is the framework's instance, it contains the muxer, middleware and configuration settings.
  65. // Create an instance of Engine, by using New() or Default()
  66. type Engine struct {
  67. RouterGroup
  68. // RedirectTrailingSlash enables automatic redirection if the current route can't be matched but a
  69. // handler for the path with (without) the trailing slash exists.
  70. // For example if /foo/ is requested but a route only exists for /foo, the
  71. // client is redirected to /foo with http status code 301 for GET requests
  72. // and 307 for all other request methods.
  73. RedirectTrailingSlash bool
  74. // RedirectFixedPath if enabled, the router tries to fix the current request path, if no
  75. // handle is registered for it.
  76. // First superfluous path elements like ../ or // are removed.
  77. // Afterwards the router does a case-insensitive lookup of the cleaned path.
  78. // If a handle can be found for this route, the router makes a redirection
  79. // to the corrected path with status code 301 for GET requests and 307 for
  80. // all other request methods.
  81. // For example /FOO and /..//Foo could be redirected to /foo.
  82. // RedirectTrailingSlash is independent of this option.
  83. RedirectFixedPath bool
  84. // HandleMethodNotAllowed if enabled, the router checks if another method is allowed for the
  85. // current route, if the current request can not be routed.
  86. // If this is the case, the request is answered with 'Method Not Allowed'
  87. // and HTTP status code 405.
  88. // If no other Method is allowed, the request is delegated to the NotFound
  89. // handler.
  90. HandleMethodNotAllowed bool
  91. // ForwardedByClientIP if enabled, client IP will be parsed from the request's headers that
  92. // match those stored at `(*gin.Engine).RemoteIPHeaders`. If no IP was
  93. // fetched, it falls back to the IP obtained from
  94. // `(*gin.Context).Request.RemoteAddr`.
  95. ForwardedByClientIP bool
  96. // AppEngine was deprecated.
  97. // Deprecated: USE `TrustedPlatform` WITH VALUE `gin.PlatformGoogleAppEngine` INSTEAD
  98. // #726 #755 If enabled, it will trust some headers starting with
  99. // 'X-AppEngine...' for better integration with that PaaS.
  100. AppEngine bool
  101. // UseRawPath if enabled, the url.RawPath will be used to find parameters.
  102. UseRawPath bool
  103. // UnescapePathValues if true, the path value will be unescaped.
  104. // If UseRawPath is false (by default), the UnescapePathValues effectively is true,
  105. // as url.Path gonna be used, which is already unescaped.
  106. UnescapePathValues bool
  107. // RemoveExtraSlash a parameter can be parsed from the URL even with extra slashes.
  108. // See the PR #1817 and issue #1644
  109. RemoveExtraSlash bool
  110. // RemoteIPHeaders list of headers used to obtain the client IP when
  111. // `(*gin.Engine).ForwardedByClientIP` is `true` and
  112. // `(*gin.Context).Request.RemoteAddr` is matched by at least one of the
  113. // network origins of list defined by `(*gin.Engine).SetTrustedProxies()`.
  114. RemoteIPHeaders []string
  115. // TrustedPlatform if set to a constant of value gin.Platform*, trusts the headers set by
  116. // that platform, for example to determine the client IP
  117. TrustedPlatform string
  118. // MaxMultipartMemory value of 'maxMemory' param that is given to http.Request's ParseMultipartForm
  119. // method call.
  120. MaxMultipartMemory int64
  121. // UseH2C enable h2c support.
  122. UseH2C bool
  123. // ContextWithFallback enable fallback Context.Deadline(), Context.Done(), Context.Err() and Context.Value() when Context.Request.Context() is not nil.
  124. ContextWithFallback bool
  125. delims render.Delims
  126. secureJSONPrefix string
  127. HTMLRender render.HTMLRender
  128. FuncMap template.FuncMap
  129. allNoRoute HandlersChain
  130. allNoMethod HandlersChain
  131. noRoute HandlersChain
  132. noMethod HandlersChain
  133. pool sync.Pool
  134. trees methodTrees
  135. maxParams uint16
  136. maxSections uint16
  137. trustedProxies []string
  138. trustedCIDRs []*net.IPNet
  139. }
  140. var _ IRouter = &Engine{}
  141. // New returns a new blank Engine instance without any middleware attached.
  142. // By default, the configuration is:
  143. // - RedirectTrailingSlash: true
  144. // - RedirectFixedPath: false
  145. // - HandleMethodNotAllowed: false
  146. // - ForwardedByClientIP: true
  147. // - UseRawPath: false
  148. // - UnescapePathValues: true
  149. func New() *Engine {
  150. debugPrintWARNINGNew()
  151. engine := &Engine{
  152. RouterGroup: RouterGroup{
  153. Handlers: nil,
  154. basePath: "/",
  155. root: true,
  156. },
  157. FuncMap: template.FuncMap{},
  158. RedirectTrailingSlash: true,
  159. RedirectFixedPath: false,
  160. HandleMethodNotAllowed: false,
  161. ForwardedByClientIP: true,
  162. RemoteIPHeaders: []string{"X-Forwarded-For", "X-Real-IP"},
  163. TrustedPlatform: defaultPlatform,
  164. UseRawPath: false,
  165. RemoveExtraSlash: false,
  166. UnescapePathValues: true,
  167. MaxMultipartMemory: defaultMultipartMemory,
  168. trees: make(methodTrees, 0, 9),
  169. delims: render.Delims{Left: "{{", Right: "}}"},
  170. secureJSONPrefix: "while(1);",
  171. trustedProxies: []string{"0.0.0.0/0", "::/0"},
  172. trustedCIDRs: defaultTrustedCIDRs,
  173. }
  174. engine.RouterGroup.engine = engine
  175. engine.pool.New = func() any {
  176. return engine.allocateContext()
  177. }
  178. return engine
  179. }
  180. // Default returns an Engine instance with the Logger and Recovery middleware already attached.
  181. func Default() *Engine {
  182. debugPrintWARNINGDefault()
  183. engine := New()
  184. engine.Use(Logger(), Recovery())
  185. return engine
  186. }
  187. func (engine *Engine) Handler() http.Handler {
  188. if !engine.UseH2C {
  189. return engine
  190. }
  191. h2s := &http2.Server{}
  192. return h2c.NewHandler(engine, h2s)
  193. }
  194. func (engine *Engine) allocateContext() *Context {
  195. v := make(Params, 0, engine.maxParams)
  196. skippedNodes := make([]skippedNode, 0, engine.maxSections)
  197. return &Context{engine: engine, params: &v, skippedNodes: &skippedNodes}
  198. }
  199. // Delims sets template left and right delims and returns an Engine instance.
  200. func (engine *Engine) Delims(left, right string) *Engine {
  201. engine.delims = render.Delims{Left: left, Right: right}
  202. return engine
  203. }
  204. // SecureJsonPrefix sets the secureJSONPrefix used in Context.SecureJSON.
  205. func (engine *Engine) SecureJsonPrefix(prefix string) *Engine {
  206. engine.secureJSONPrefix = prefix
  207. return engine
  208. }
  209. // LoadHTMLGlob loads HTML files identified by glob pattern
  210. // and associates the result with HTML renderer.
  211. func (engine *Engine) LoadHTMLGlob(pattern string) {
  212. left := engine.delims.Left
  213. right := engine.delims.Right
  214. templ := template.Must(template.New("").Delims(left, right).Funcs(engine.FuncMap).ParseGlob(pattern))
  215. if IsDebugging() {
  216. debugPrintLoadTemplate(templ)
  217. engine.HTMLRender = render.HTMLDebug{Glob: pattern, FuncMap: engine.FuncMap, Delims: engine.delims}
  218. return
  219. }
  220. engine.SetHTMLTemplate(templ)
  221. }
  222. // LoadHTMLFiles loads a slice of HTML files
  223. // and associates the result with HTML renderer.
  224. func (engine *Engine) LoadHTMLFiles(files ...string) {
  225. if IsDebugging() {
  226. engine.HTMLRender = render.HTMLDebug{Files: files, FuncMap: engine.FuncMap, Delims: engine.delims}
  227. return
  228. }
  229. templ := template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).Funcs(engine.FuncMap).ParseFiles(files...))
  230. engine.SetHTMLTemplate(templ)
  231. }
  232. // SetHTMLTemplate associate a template with HTML renderer.
  233. func (engine *Engine) SetHTMLTemplate(templ *template.Template) {
  234. if len(engine.trees) > 0 {
  235. debugPrintWARNINGSetHTMLTemplate()
  236. }
  237. engine.HTMLRender = render.HTMLProduction{Template: templ.Funcs(engine.FuncMap)}
  238. }
  239. // SetFuncMap sets the FuncMap used for template.FuncMap.
  240. func (engine *Engine) SetFuncMap(funcMap template.FuncMap) {
  241. engine.FuncMap = funcMap
  242. }
  243. // NoRoute adds handlers for NoRoute. It returns a 404 code by default.
  244. func (engine *Engine) NoRoute(handlers ...HandlerFunc) {
  245. engine.noRoute = handlers
  246. engine.rebuild404Handlers()
  247. }
  248. // NoMethod sets the handlers called when Engine.HandleMethodNotAllowed = true.
  249. func (engine *Engine) NoMethod(handlers ...HandlerFunc) {
  250. engine.noMethod = handlers
  251. engine.rebuild405Handlers()
  252. }
  253. // Use attaches a global middleware to the router. i.e. the middleware attached through Use() will be
  254. // included in the handlers chain for every single request. Even 404, 405, static files...
  255. // For example, this is the right place for a logger or error management middleware.
  256. func (engine *Engine) Use(middleware ...HandlerFunc) IRoutes {
  257. engine.RouterGroup.Use(middleware...)
  258. engine.rebuild404Handlers()
  259. engine.rebuild405Handlers()
  260. return engine
  261. }
  262. func (engine *Engine) rebuild404Handlers() {
  263. engine.allNoRoute = engine.combineHandlers(engine.noRoute)
  264. }
  265. func (engine *Engine) rebuild405Handlers() {
  266. engine.allNoMethod = engine.combineHandlers(engine.noMethod)
  267. }
  268. func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
  269. assert1(path[0] == '/', "path must begin with '/'")
  270. assert1(method != "", "HTTP method can not be empty")
  271. assert1(len(handlers) > 0, "there must be at least one handler")
  272. debugPrintRoute(method, path, handlers)
  273. root := engine.trees.get(method)
  274. if root == nil {
  275. root = new(node)
  276. root.fullPath = "/"
  277. engine.trees = append(engine.trees, methodTree{method: method, root: root})
  278. }
  279. root.addRoute(path, handlers)
  280. // Update maxParams
  281. if paramsCount := countParams(path); paramsCount > engine.maxParams {
  282. engine.maxParams = paramsCount
  283. }
  284. if sectionsCount := countSections(path); sectionsCount > engine.maxSections {
  285. engine.maxSections = sectionsCount
  286. }
  287. }
  288. // Routes returns a slice of registered routes, including some useful information, such as:
  289. // the http method, path and the handler name.
  290. func (engine *Engine) Routes() (routes RoutesInfo) {
  291. for _, tree := range engine.trees {
  292. routes = iterate("", tree.method, routes, tree.root)
  293. }
  294. return routes
  295. }
  296. func iterate(path, method string, routes RoutesInfo, root *node) RoutesInfo {
  297. path += root.path
  298. if len(root.handlers) > 0 {
  299. handlerFunc := root.handlers.Last()
  300. routes = append(routes, RouteInfo{
  301. Method: method,
  302. Path: path,
  303. Handler: nameOfFunction(handlerFunc),
  304. HandlerFunc: handlerFunc,
  305. })
  306. }
  307. for _, child := range root.children {
  308. routes = iterate(path, method, routes, child)
  309. }
  310. return routes
  311. }
  312. // Run attaches the router to a http.Server and starts listening and serving HTTP requests.
  313. // It is a shortcut for http.ListenAndServe(addr, router)
  314. // Note: this method will block the calling goroutine indefinitely unless an error happens.
  315. func (engine *Engine) Run(addr ...string) (err error) {
  316. defer func() { debugPrintError(err) }()
  317. if engine.isUnsafeTrustedProxies() {
  318. debugPrint("[WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.\n" +
  319. "Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.")
  320. }
  321. address := resolveAddress(addr)
  322. debugPrint("Listening and serving HTTP on %s\n", address)
  323. err = http.ListenAndServe(address, engine.Handler())
  324. return
  325. }
  326. func (engine *Engine) prepareTrustedCIDRs() ([]*net.IPNet, error) {
  327. if engine.trustedProxies == nil {
  328. return nil, nil
  329. }
  330. cidr := make([]*net.IPNet, 0, len(engine.trustedProxies))
  331. for _, trustedProxy := range engine.trustedProxies {
  332. if !strings.Contains(trustedProxy, "/") {
  333. ip := parseIP(trustedProxy)
  334. if ip == nil {
  335. return cidr, &net.ParseError{Type: "IP address", Text: trustedProxy}
  336. }
  337. switch len(ip) {
  338. case net.IPv4len:
  339. trustedProxy += "/32"
  340. case net.IPv6len:
  341. trustedProxy += "/128"
  342. }
  343. }
  344. _, cidrNet, err := net.ParseCIDR(trustedProxy)
  345. if err != nil {
  346. return cidr, err
  347. }
  348. cidr = append(cidr, cidrNet)
  349. }
  350. return cidr, nil
  351. }
  352. // SetTrustedProxies set a list of network origins (IPv4 addresses,
  353. // IPv4 CIDRs, IPv6 addresses or IPv6 CIDRs) from which to trust
  354. // request's headers that contain alternative client IP when
  355. // `(*gin.Engine).ForwardedByClientIP` is `true`. `TrustedProxies`
  356. // feature is enabled by default, and it also trusts all proxies
  357. // by default. If you want to disable this feature, use
  358. // Engine.SetTrustedProxies(nil), then Context.ClientIP() will
  359. // return the remote address directly.
  360. func (engine *Engine) SetTrustedProxies(trustedProxies []string) error {
  361. engine.trustedProxies = trustedProxies
  362. return engine.parseTrustedProxies()
  363. }
  364. // isUnsafeTrustedProxies checks if Engine.trustedCIDRs contains all IPs, it's not safe if it has (returns true)
  365. func (engine *Engine) isUnsafeTrustedProxies() bool {
  366. return engine.isTrustedProxy(net.ParseIP("0.0.0.0")) || engine.isTrustedProxy(net.ParseIP("::"))
  367. }
  368. // parseTrustedProxies parse Engine.trustedProxies to Engine.trustedCIDRs
  369. func (engine *Engine) parseTrustedProxies() error {
  370. trustedCIDRs, err := engine.prepareTrustedCIDRs()
  371. engine.trustedCIDRs = trustedCIDRs
  372. return err
  373. }
  374. // isTrustedProxy will check whether the IP address is included in the trusted list according to Engine.trustedCIDRs
  375. func (engine *Engine) isTrustedProxy(ip net.IP) bool {
  376. if engine.trustedCIDRs == nil {
  377. return false
  378. }
  379. for _, cidr := range engine.trustedCIDRs {
  380. if cidr.Contains(ip) {
  381. return true
  382. }
  383. }
  384. return false
  385. }
  386. // validateHeader will parse X-Forwarded-For header and return the trusted client IP address
  387. func (engine *Engine) validateHeader(header string) (clientIP string, valid bool) {
  388. if header == "" {
  389. return "", false
  390. }
  391. items := strings.Split(header, ",")
  392. for i := len(items) - 1; i >= 0; i-- {
  393. ipStr := strings.TrimSpace(items[i])
  394. ip := net.ParseIP(ipStr)
  395. if ip == nil {
  396. break
  397. }
  398. // X-Forwarded-For is appended by proxy
  399. // Check IPs in reverse order and stop when find untrusted proxy
  400. if (i == 0) || (!engine.isTrustedProxy(ip)) {
  401. return ipStr, true
  402. }
  403. }
  404. return "", false
  405. }
  406. // parseIP parse a string representation of an IP and returns a net.IP with the
  407. // minimum byte representation or nil if input is invalid.
  408. func parseIP(ip string) net.IP {
  409. parsedIP := net.ParseIP(ip)
  410. if ipv4 := parsedIP.To4(); ipv4 != nil {
  411. // return ip in a 4-byte representation
  412. return ipv4
  413. }
  414. // return ip in a 16-byte representation or nil
  415. return parsedIP
  416. }
  417. // RunTLS attaches the router to a http.Server and starts listening and serving HTTPS (secure) requests.
  418. // It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router)
  419. // Note: this method will block the calling goroutine indefinitely unless an error happens.
  420. func (engine *Engine) RunTLS(addr, certFile, keyFile string) (err error) {
  421. debugPrint("Listening and serving HTTPS on %s\n", addr)
  422. defer func() { debugPrintError(err) }()
  423. if engine.isUnsafeTrustedProxies() {
  424. debugPrint("[WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.\n" +
  425. "Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.")
  426. }
  427. err = http.ListenAndServeTLS(addr, certFile, keyFile, engine.Handler())
  428. return
  429. }
  430. // RunUnix attaches the router to a http.Server and starts listening and serving HTTP requests
  431. // through the specified unix socket (i.e. a file).
  432. // Note: this method will block the calling goroutine indefinitely unless an error happens.
  433. func (engine *Engine) RunUnix(file string) (err error) {
  434. debugPrint("Listening and serving HTTP on unix:/%s", file)
  435. defer func() { debugPrintError(err) }()
  436. if engine.isUnsafeTrustedProxies() {
  437. debugPrint("[WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.\n" +
  438. "Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.")
  439. }
  440. listener, err := net.Listen("unix", file)
  441. if err != nil {
  442. return
  443. }
  444. defer listener.Close()
  445. defer os.Remove(file)
  446. err = http.Serve(listener, engine.Handler())
  447. return
  448. }
  449. // RunFd attaches the router to a http.Server and starts listening and serving HTTP requests
  450. // through the specified file descriptor.
  451. // Note: this method will block the calling goroutine indefinitely unless an error happens.
  452. func (engine *Engine) RunFd(fd int) (err error) {
  453. debugPrint("Listening and serving HTTP on fd@%d", fd)
  454. defer func() { debugPrintError(err) }()
  455. if engine.isUnsafeTrustedProxies() {
  456. debugPrint("[WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.\n" +
  457. "Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.")
  458. }
  459. f := os.NewFile(uintptr(fd), fmt.Sprintf("fd@%d", fd))
  460. listener, err := net.FileListener(f)
  461. if err != nil {
  462. return
  463. }
  464. defer listener.Close()
  465. err = engine.RunListener(listener)
  466. return
  467. }
  468. // RunListener attaches the router to a http.Server and starts listening and serving HTTP requests
  469. // through the specified net.Listener
  470. func (engine *Engine) RunListener(listener net.Listener) (err error) {
  471. debugPrint("Listening and serving HTTP on listener what's bind with address@%s", listener.Addr())
  472. defer func() { debugPrintError(err) }()
  473. if engine.isUnsafeTrustedProxies() {
  474. debugPrint("[WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.\n" +
  475. "Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.")
  476. }
  477. err = http.Serve(listener, engine.Handler())
  478. return
  479. }
  480. // ServeHTTP conforms to the http.Handler interface.
  481. func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
  482. c := engine.pool.Get().(*Context)
  483. c.writermem.reset(w)
  484. c.Request = req
  485. c.reset()
  486. engine.handleHTTPRequest(c)
  487. engine.pool.Put(c)
  488. }
  489. // HandleContext re-enters a context that has been rewritten.
  490. // This can be done by setting c.Request.URL.Path to your new target.
  491. // Disclaimer: You can loop yourself to deal with this, use wisely.
  492. func (engine *Engine) HandleContext(c *Context) {
  493. oldIndexValue := c.index
  494. c.reset()
  495. engine.handleHTTPRequest(c)
  496. c.index = oldIndexValue
  497. }
  498. func (engine *Engine) handleHTTPRequest(c *Context) {
  499. httpMethod := c.Request.Method
  500. rPath := c.Request.URL.Path
  501. unescape := false
  502. if engine.UseRawPath && len(c.Request.URL.RawPath) > 0 {
  503. rPath = c.Request.URL.RawPath
  504. unescape = engine.UnescapePathValues
  505. }
  506. if engine.RemoveExtraSlash {
  507. rPath = cleanPath(rPath)
  508. }
  509. // Find root of the tree for the given HTTP method
  510. t := engine.trees
  511. for i, tl := 0, len(t); i < tl; i++ {
  512. if t[i].method != httpMethod {
  513. continue
  514. }
  515. root := t[i].root
  516. // Find route in tree
  517. value := root.getValue(rPath, c.params, c.skippedNodes, unescape)
  518. if value.params != nil {
  519. c.Params = *value.params
  520. }
  521. if value.handlers != nil {
  522. c.handlers = value.handlers
  523. c.fullPath = value.fullPath
  524. c.Next()
  525. c.writermem.WriteHeaderNow()
  526. return
  527. }
  528. if httpMethod != http.MethodConnect && rPath != "/" {
  529. if value.tsr && engine.RedirectTrailingSlash {
  530. redirectTrailingSlash(c)
  531. return
  532. }
  533. if engine.RedirectFixedPath && redirectFixedPath(c, root, engine.RedirectFixedPath) {
  534. return
  535. }
  536. }
  537. break
  538. }
  539. if engine.HandleMethodNotAllowed {
  540. for _, tree := range engine.trees {
  541. if tree.method == httpMethod {
  542. continue
  543. }
  544. if value := tree.root.getValue(rPath, nil, c.skippedNodes, unescape); value.handlers != nil {
  545. c.handlers = engine.allNoMethod
  546. serveError(c, http.StatusMethodNotAllowed, default405Body)
  547. return
  548. }
  549. }
  550. }
  551. c.handlers = engine.allNoRoute
  552. serveError(c, http.StatusNotFound, default404Body)
  553. }
  554. var mimePlain = []string{MIMEPlain}
  555. func serveError(c *Context, code int, defaultMessage []byte) {
  556. c.writermem.status = code
  557. c.Next()
  558. if c.writermem.Written() {
  559. return
  560. }
  561. if c.writermem.Status() == code {
  562. c.writermem.Header()["Content-Type"] = mimePlain
  563. _, err := c.Writer.Write(defaultMessage)
  564. if err != nil {
  565. debugPrint("cannot write message to writer during serve error: %v", err)
  566. }
  567. return
  568. }
  569. c.writermem.WriteHeaderNow()
  570. }
  571. func redirectTrailingSlash(c *Context) {
  572. req := c.Request
  573. p := req.URL.Path
  574. if prefix := path.Clean(c.Request.Header.Get("X-Forwarded-Prefix")); prefix != "." {
  575. p = prefix + "/" + req.URL.Path
  576. }
  577. req.URL.Path = p + "/"
  578. if length := len(p); length > 1 && p[length-1] == '/' {
  579. req.URL.Path = p[:length-1]
  580. }
  581. redirectRequest(c)
  582. }
  583. func redirectFixedPath(c *Context, root *node, trailingSlash bool) bool {
  584. req := c.Request
  585. rPath := req.URL.Path
  586. if fixedPath, ok := root.findCaseInsensitivePath(cleanPath(rPath), trailingSlash); ok {
  587. req.URL.Path = bytesconv.BytesToString(fixedPath)
  588. redirectRequest(c)
  589. return true
  590. }
  591. return false
  592. }
  593. func redirectRequest(c *Context) {
  594. req := c.Request
  595. rPath := req.URL.Path
  596. rURL := req.URL.String()
  597. code := http.StatusMovedPermanently // Permanent redirect, request with GET method
  598. if req.Method != http.MethodGet {
  599. code = http.StatusTemporaryRedirect
  600. }
  601. debugPrint("redirecting request %d: %s --> %s", code, rPath, rURL)
  602. http.Redirect(c.Writer, req, rURL, code)
  603. c.writermem.WriteHeaderNow()
  604. }