mirror of
https://github.com/kataras/iris.git
synced 2026-01-24 04:15:56 +00:00
version 12.1.5
Former-commit-id: cda69f08955cb0d594e98bf26197ee573cbba4b2
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
@@ -42,66 +41,6 @@ type repository struct {
|
||||
pos map[string]int
|
||||
}
|
||||
|
||||
func (repo *repository) remove(route *Route) bool {
|
||||
for i, r := range repo.routes {
|
||||
if r == route {
|
||||
return repo.removeByIndex(i)
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (repo *repository) removeByPath(tmplPath string) bool {
|
||||
if repo.pos != nil {
|
||||
if idx, ok := repo.pos[tmplPath]; ok {
|
||||
return repo.removeByIndex(idx)
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (repo *repository) removeByName(routeName string) bool {
|
||||
for i, r := range repo.routes {
|
||||
if r.Name == routeName {
|
||||
return repo.removeByIndex(i)
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (repo *repository) removeByIndex(idx int) bool {
|
||||
n := len(repo.routes)
|
||||
|
||||
if n == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
if idx >= n {
|
||||
return false
|
||||
}
|
||||
|
||||
if n == 1 && idx == 0 {
|
||||
repo.routes = repo.routes[0:0]
|
||||
repo.pos = nil
|
||||
return true
|
||||
}
|
||||
|
||||
r := repo.routes[idx]
|
||||
if r == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
repo.routes = append(repo.routes[:idx], repo.routes[idx+1:]...)
|
||||
if repo.pos != nil {
|
||||
delete(repo.pos, r.Path)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (repo *repository) get(routeName string) *Route {
|
||||
for _, r := range repo.routes {
|
||||
if r.Name == routeName {
|
||||
@@ -160,15 +99,6 @@ func (repo *repository) register(route *Route) {
|
||||
repo.pos[route.tmpl.Src] = len(repo.routes) - 1
|
||||
}
|
||||
|
||||
type apiError struct {
|
||||
error
|
||||
}
|
||||
|
||||
func (e *apiError) Is(err error) bool {
|
||||
_, ok := err.(*apiError)
|
||||
return ok
|
||||
}
|
||||
|
||||
// APIBuilder the visible API for constructing the router
|
||||
// and child routers.
|
||||
type APIBuilder struct {
|
||||
@@ -179,9 +109,6 @@ type APIBuilder struct {
|
||||
// the api builder global routes repository
|
||||
routes *repository
|
||||
|
||||
// the api builder global route path reverser object
|
||||
// used by the view engine but it can be used anywhere.
|
||||
reverser *RoutePathReverser
|
||||
// the api builder global errors, can be filled by the Subdomain, WildcardSubdomain, Handle...
|
||||
// the list of possible errors that can be
|
||||
// collected on the build state to log
|
||||
@@ -344,7 +271,7 @@ func (api *APIBuilder) CreateRoutes(methods []string, relativePath string, handl
|
||||
// if allowMethods are empty, then simply register with the passed, main, method.
|
||||
methods = append(api.allowMethods, methods...)
|
||||
|
||||
routes := make([]*Route, len(methods), len(methods))
|
||||
routes := make([]*Route, len(methods))
|
||||
|
||||
for i, m := range methods {
|
||||
route, err := NewRoute(m, subdomain, path, possibleMainHandlerName, routeHandlers, *api.macros)
|
||||
@@ -492,11 +419,22 @@ func (api *APIBuilder) HandleDir(requestPath, directory string, opts ...DirOptio
|
||||
continue
|
||||
}
|
||||
|
||||
slashIdx := strings.IndexByte(s.RequestPath, '/')
|
||||
if slashIdx == -1 {
|
||||
slashIdx = 0
|
||||
if n := len(api.relativePath); n > 0 && api.relativePath[n-1] == SubdomainPrefix[0] {
|
||||
// this api is a subdomain-based.
|
||||
slashIdx := strings.IndexByte(s.RequestPath, '/')
|
||||
if slashIdx == -1 {
|
||||
slashIdx = 0
|
||||
}
|
||||
|
||||
requestPath = s.RequestPath[slashIdx:]
|
||||
} else {
|
||||
requestPath = s.RequestPath[strings.Index(s.RequestPath, api.relativePath)+len(api.relativePath):]
|
||||
}
|
||||
requestPath = s.RequestPath[slashIdx:]
|
||||
|
||||
if requestPath == "" {
|
||||
requestPath = "/"
|
||||
}
|
||||
|
||||
routes = append(routes, api.CreateRoutes([]string{http.MethodGet}, requestPath, h)...)
|
||||
getRoute.StaticSites = append(getRoute.StaticSites, s)
|
||||
}
|
||||
@@ -861,9 +799,6 @@ func (api *APIBuilder) StaticContent(reqPath string, cType string, content []byt
|
||||
return api.registerResourceRoute(reqPath, h)
|
||||
}
|
||||
|
||||
// errDirectoryFileNotFound returns an error with message: 'Directory or file %s couldn't found. Trace: +error trace'
|
||||
var errDirectoryFileNotFound = errors.New("Directory or file %s couldn't found. Trace: %s")
|
||||
|
||||
// Favicon serves static favicon
|
||||
// accepts 2 parameters, second is optional
|
||||
// favPath (string), declare the system directory path of the __.ico
|
||||
|
||||
@@ -307,7 +307,10 @@ func FileServer(directory string, opts ...DirOptions) context.Handler {
|
||||
sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() })
|
||||
|
||||
ctx.ContentType(context.ContentHTMLHeaderValue)
|
||||
ctx.WriteString("<pre>\n")
|
||||
_, err = ctx.WriteString("<pre>\n")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, d := range dirs {
|
||||
name := d.Name()
|
||||
if d.IsDir() {
|
||||
@@ -317,10 +320,13 @@ func FileServer(directory string, opts ...DirOptions) context.Handler {
|
||||
// part of the URL path, and not indicate the start of a query
|
||||
// string or fragment.
|
||||
url := url.URL{Path: joinPath("./"+dirName, name)} // edit here to redirect correctly, standard library misses that.
|
||||
ctx.Writef("<a href=\"%s\">%s</a>\n", url.String(), htmlReplacer.Replace(name))
|
||||
_, err = ctx.Writef("<a href=\"%s\">%s</a>\n", url.String(), htmlReplacer.Replace(name))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
ctx.WriteString("</pre>\n")
|
||||
return nil
|
||||
_, err = ctx.WriteString("</pre>\n")
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,18 +21,6 @@ type ExecutionRules struct {
|
||||
Main ExecutionOptions
|
||||
}
|
||||
|
||||
func handlersNames(handlers context.Handlers) (names []string) {
|
||||
for _, h := range handlers {
|
||||
if h == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
names = append(names, context.HandlerName(h))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func applyExecutionRules(rules ExecutionRules, begin, done, main *context.Handlers) {
|
||||
if !rules.Begin.Force && !rules.Done.Force && !rules.Main.Force {
|
||||
return // do not proceed and spend buld-time here if nothing changed.
|
||||
|
||||
@@ -542,7 +542,7 @@ var types = map[string]string{
|
||||
func init() {
|
||||
for ext, typ := range types {
|
||||
// skip errors
|
||||
mime.AddExtensionType(ext, typ)
|
||||
_ = mime.AddExtensionType(ext, typ)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -62,13 +62,6 @@ func prefix(s string, prefix string) string {
|
||||
return s
|
||||
}
|
||||
|
||||
func suffix(s string, suffix string) string {
|
||||
if !strings.HasSuffix(s, suffix) {
|
||||
return s + suffix
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func splitMethod(methodMany string) []string {
|
||||
methodMany = strings.Trim(methodMany, " ")
|
||||
return strings.Split(methodMany, " ")
|
||||
@@ -348,7 +341,7 @@ func toStringSlice(args []interface{}) (argsString []string) {
|
||||
return
|
||||
}
|
||||
|
||||
argsString = make([]string, argsSize, argsSize)
|
||||
argsString = make([]string, argsSize)
|
||||
for i, v := range args {
|
||||
if s, ok := v.(string); ok {
|
||||
argsString[i] = s
|
||||
|
||||
@@ -95,12 +95,6 @@ type trie struct {
|
||||
subdomain string
|
||||
}
|
||||
|
||||
func newTrie() *trie {
|
||||
return &trie{
|
||||
root: newTrieNode(),
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
pathSep = "/"
|
||||
pathSepB = '/'
|
||||
|
||||
Reference in New Issue
Block a user