gin.go 23 KB

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