github.com/gogf/gf@v1.16.9/net/ghttp/ghttp.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // Package ghttp provides powerful http server and simple client implements. 8 package ghttp 9 10 import ( 11 "github.com/gogf/gf/container/gmap" 12 "github.com/gogf/gf/container/gtype" 13 "github.com/gogf/gf/os/gcache" 14 "github.com/gogf/gf/os/gsession" 15 "github.com/gorilla/websocket" 16 "net/http" 17 "reflect" 18 "time" 19 ) 20 21 type ( 22 // Server wraps the http.Server and provides more rich features. 23 Server struct { 24 name string // Unique name for instance management. 25 config ServerConfig // Configuration. 26 plugins []Plugin // Plugin array to extend server functionality. 27 servers []*gracefulServer // Underlying http.Server array. 28 serverCount *gtype.Int // Underlying http.Server count. 29 closeChan chan struct{} // Used for underlying server closing event notification. 30 serveTree map[string]interface{} // The route map tree. 31 serveCache *gcache.Cache // Server caches for internal usage. 32 routesMap map[string][]registeredRouteItem // Route map mainly for route dumps and repeated route checks. 33 statusHandlerMap map[string][]HandlerFunc // Custom status handler map. 34 sessionManager *gsession.Manager // Session manager. 35 } 36 37 // Router object. 38 Router struct { 39 Uri string // URI. 40 Method string // HTTP method 41 Domain string // Bound domain. 42 RegRule string // Parsed regular expression for route matching. 43 RegNames []string // Parsed router parameter names. 44 Priority int // Just for reference. 45 } 46 47 // RouterItem is just for route dumps. 48 RouterItem struct { 49 Server string // Server name. 50 Address string // Listening address. 51 Domain string // Bound domain. 52 Type int // Router type. 53 Middleware string // Bound middleware. 54 Method string // Handler method name. 55 Route string // Route URI. 56 Priority int // Just for reference. 57 IsServiceHandler bool // Is service handler. 58 handler *handlerItem // The handler. 59 } 60 61 // HandlerFunc is request handler function. 62 HandlerFunc = func(r *Request) 63 64 // handlerFuncInfo contains the HandlerFunc address and its reflect type. 65 handlerFuncInfo struct { 66 Func HandlerFunc // Handler function address. 67 Type reflect.Type // Reflect type information for current handler, which is used for extension of handler feature. 68 Value reflect.Value // Reflect value information for current handler, which is used for extension of handler feature. 69 } 70 71 // handlerItem is the registered handler for route handling, 72 // including middleware and hook functions. 73 handlerItem struct { 74 Id int // Unique handler item id mark. 75 Name string // Handler name, which is automatically retrieved from runtime stack when registered. 76 Type int // Handler type: object/handler/controller/middleware/hook. 77 Info handlerFuncInfo // Handler function information. 78 InitFunc HandlerFunc // Initialization function when request enters the object (only available for object register type). 79 ShutFunc HandlerFunc // Shutdown function when request leaves out the object (only available for object register type). 80 Middleware []HandlerFunc // Bound middleware array. 81 HookName string // Hook type name, only available for hook type. 82 Router *Router // Router object. 83 Source string // Registering source file `path:line`. 84 } 85 86 // handlerParsedItem is the item parsed from URL.Path. 87 handlerParsedItem struct { 88 Handler *handlerItem // Handler information. 89 Values map[string]string // Router values parsed from URL.Path. 90 } 91 92 // registeredRouteItem stores the information of the router and is used for route map. 93 registeredRouteItem struct { 94 Source string // Source file path and its line number. 95 Handler *handlerItem // Handler object. 96 } 97 98 // errorStack is the interface for Stack feature. 99 errorStack interface { 100 Error() string 101 Stack() string 102 } 103 104 // Listening file descriptor mapping. 105 // The key is either "http" or "https" and the value is its FD. 106 listenerFdMap = map[string]string 107 ) 108 109 const ( 110 HookBeforeServe = "HOOK_BEFORE_SERVE" 111 HookAfterServe = "HOOK_AFTER_SERVE" 112 HookBeforeOutput = "HOOK_BEFORE_OUTPUT" 113 HookAfterOutput = "HOOK_AFTER_OUTPUT" 114 ServerStatusStopped = 0 115 ServerStatusRunning = 1 116 supportedHttpMethods = "GET,PUT,POST,DELETE,PATCH,HEAD,CONNECT,OPTIONS,TRACE" 117 defaultServerName = "default" 118 defaultDomainName = "default" 119 defaultMethod = "ALL" 120 handlerTypeHandler = 1 121 handlerTypeObject = 2 122 handlerTypeController = 3 123 handlerTypeMiddleware = 4 124 handlerTypeHook = 5 125 exceptionExit = "exit" 126 exceptionExitAll = "exit_all" 127 exceptionExitHook = "exit_hook" 128 routeCacheDuration = time.Hour 129 methodNameInit = "Init" 130 methodNameShut = "Shut" 131 methodNameExit = "Exit" 132 ctxKeyForRequest = "gHttpRequestObject" 133 ) 134 135 var ( 136 // methodsMap stores all supported HTTP method, 137 // it is used for quick HTTP method searching using map. 138 methodsMap = make(map[string]struct{}) 139 140 // serverMapping stores more than one server instances for current process. 141 // The key is the name of the server, and the value is its instance. 142 serverMapping = gmap.NewStrAnyMap(true) 143 144 // serverRunning marks the running server count. 145 // If there is no successful server running or all servers' shutdown, this value is 0. 146 serverRunning = gtype.NewInt() 147 148 // wsUpGrader is the default up-grader configuration for websocket. 149 wsUpGrader = websocket.Upgrader{ 150 // It does not check the origin in default, the application can do it itself. 151 CheckOrigin: func(r *http.Request) bool { 152 return true 153 }, 154 } 155 // allDoneChan is the event for all server have done its serving and exit. 156 // It is used for process blocking purpose. 157 allDoneChan = make(chan struct{}, 1000) 158 159 // serverProcessInitialized is used for lazy initialization for server. 160 // The process can only be initialized once. 161 serverProcessInitialized = gtype.NewBool() 162 163 // gracefulEnabled is used for graceful reload feature, which is false in default. 164 gracefulEnabled = false 165 166 // defaultValueTags is the struct tag names for default value storing. 167 defaultValueTags = []string{"d", "default"} 168 )