mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
remove Search/InArray functions from gstr package; update custom http status handling logics of web server
This commit is contained in:
@ -136,9 +136,8 @@ func (r *Response) WriteStatus(status int, content...string) {
|
||||
if status != http.StatusOK {
|
||||
if f := r.request.Server.getStatusHandler(status, r.request); f != nil {
|
||||
f(r.request)
|
||||
// 如果是http.StatusOK那么表示回调函数内部没有设置header status,
|
||||
// 那么这里就可以设置status,防止多次设置(http: multiple response.WriteHeader calls)
|
||||
if r.Status == http.StatusOK {
|
||||
// 防止多次设置(http: multiple response.WriteHeader calls)
|
||||
if r.Status == 0 {
|
||||
r.WriteHeader(status)
|
||||
}
|
||||
return
|
||||
|
||||
@ -46,14 +46,28 @@ func (s *Server)handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||
request := newRequest(s, r, w)
|
||||
|
||||
defer func() {
|
||||
if request.LeaveTime == 0 {
|
||||
request.LeaveTime = gtime.Microsecond()
|
||||
// 设置请求完成时间
|
||||
request.LeaveTime = gtime.Microsecond()
|
||||
// 事件 - BeforeOutput
|
||||
if !request.IsExited() {
|
||||
s.callHookHandler(HOOK_BEFORE_OUTPUT, request)
|
||||
}
|
||||
// 输出Cookie
|
||||
request.Cookie.Output()
|
||||
// 输出缓冲区
|
||||
request.Response.OutputBuffer()
|
||||
// 事件 - AfterOutput
|
||||
if !request.IsExited() {
|
||||
s.callHookHandler(HOOK_AFTER_OUTPUT, request)
|
||||
}
|
||||
|
||||
// 事件 - BeforeClose
|
||||
s.callHookHandler(HOOK_BEFORE_CLOSE, request)
|
||||
// access log
|
||||
s.handleAccessLog(request)
|
||||
// error log使用recover进行判断
|
||||
if e := recover(); e != nil {
|
||||
request.Response.WriteStatus(http.StatusInternalServerError)
|
||||
s.handleErrorLog(e, request)
|
||||
}
|
||||
// 更新Session会话超时时间
|
||||
@ -125,22 +139,6 @@ func (s *Server)handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||
if !request.IsExited() {
|
||||
s.callHookHandler(HOOK_AFTER_SERVE, request)
|
||||
}
|
||||
|
||||
// 设置请求完成时间
|
||||
request.LeaveTime = gtime.Microsecond()
|
||||
|
||||
// 事件 - BeforeOutput
|
||||
if !request.IsExited() {
|
||||
s.callHookHandler(HOOK_BEFORE_OUTPUT, request)
|
||||
}
|
||||
// 输出Cookie
|
||||
request.Cookie.Output()
|
||||
// 输出缓冲区
|
||||
request.Response.OutputBuffer()
|
||||
// 事件 - AfterOutput
|
||||
if !request.IsExited() {
|
||||
s.callHookHandler(HOOK_AFTER_OUTPUT, request)
|
||||
}
|
||||
}
|
||||
|
||||
// 查找静态文件的绝对路径
|
||||
|
||||
@ -10,7 +10,6 @@ package ghttp
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// 处理服务错误信息,主要是panic,http请求的status由access log进行管理
|
||||
@ -34,8 +33,6 @@ func (s *Server) handleAccessLog(r *Request) {
|
||||
|
||||
// 处理服务错误信息,主要是panic,http请求的status由access log进行管理
|
||||
func (s *Server) handleErrorLog(error interface{}, r *Request) {
|
||||
r.Response.WriteStatus(http.StatusInternalServerError)
|
||||
|
||||
// 错误输出默认是开启的
|
||||
if !s.IsErrorLogEnabled() && gfile.MainPkgPath() == "" {
|
||||
return
|
||||
|
||||
@ -149,12 +149,15 @@ func Test_Router_Status(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// 测试不存在的路由.
|
||||
func Test_Router_404(t *testing.T) {
|
||||
// 自定义状态码处理.
|
||||
func Test_Router_CustomStatusHandler(t *testing.T) {
|
||||
s := g.Server(gtime.Nanosecond())
|
||||
s.BindHandler("/", func(r *ghttp.Request){
|
||||
r.Response.Write("hello")
|
||||
})
|
||||
s.BindStatusHandler(404, func(r *ghttp.Request){
|
||||
r.Response.Write("404 page")
|
||||
})
|
||||
s.SetPort(8120)
|
||||
s.SetDumpRouteMap(false)
|
||||
go s.Run()
|
||||
@ -168,6 +171,34 @@ func Test_Router_404(t *testing.T) {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix("http://127.0.0.1:8120")
|
||||
|
||||
gtest.Assert(client.GetContent("/"), "hello")
|
||||
resp, err := client.Get("/ThisDoesNotExist")
|
||||
defer resp.Close()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(resp.StatusCode, 404)
|
||||
gtest.Assert(resp.ReadAllString(), "404 page")
|
||||
})
|
||||
}
|
||||
|
||||
// 测试不存在的路由.
|
||||
func Test_Router_404(t *testing.T) {
|
||||
s := g.Server(gtime.Nanosecond())
|
||||
s.BindHandler("/", func(r *ghttp.Request){
|
||||
r.Response.Write("hello")
|
||||
})
|
||||
s.SetPort(8130)
|
||||
s.SetDumpRouteMap(false)
|
||||
go s.Run()
|
||||
defer func() {
|
||||
s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
}()
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix("http://127.0.0.1:8130")
|
||||
|
||||
gtest.Assert(client.GetContent("/"), "hello")
|
||||
resp, err := client.Get("/ThisDoesNotExist")
|
||||
defer resp.Close()
|
||||
|
||||
@ -88,25 +88,6 @@ func UcWords(str string) string {
|
||||
return strings.Title(str)
|
||||
}
|
||||
|
||||
// Traverse the array to find the string index position, if not exist, return-1.
|
||||
//
|
||||
// 遍历数组查找字符串索引位置,如果不存在则返回-1,使用完整遍历查找.
|
||||
func SearchArray (a []string, s string) int {
|
||||
for i, v := range a {
|
||||
if s == v {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// InArray tests whether the given string s is in string array a.
|
||||
//
|
||||
// 判断字符串是否在数组中
|
||||
func InArray (a []string, s string) bool {
|
||||
return SearchArray(a, s) != -1
|
||||
}
|
||||
|
||||
// IsLetterLower tests whether the given byte b is in lower case.
|
||||
//
|
||||
// 判断给定字符是否小写
|
||||
@ -490,15 +471,7 @@ func Explode(delimiter, str string) []string {
|
||||
//
|
||||
// 用glue将字符串数组pieces连接为一个字符串。
|
||||
func Implode(glue string, pieces []string) string {
|
||||
var buf bytes.Buffer
|
||||
l := len(pieces)
|
||||
for _, str := range pieces {
|
||||
buf.WriteString(str)
|
||||
if l--; l > 0 {
|
||||
buf.WriteString(glue)
|
||||
}
|
||||
}
|
||||
return buf.String()
|
||||
return strings.Join(pieces, glue)
|
||||
}
|
||||
|
||||
// Generate a single-byte string from a number.
|
||||
|
||||
@ -68,26 +68,6 @@ func Test_UcWords(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SearchArray(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
array := []string{"a", "b", "c"}
|
||||
gtest.Assert(gstr.SearchArray(array, "a"), 0)
|
||||
gtest.Assert(gstr.SearchArray(array, "b"), 1)
|
||||
gtest.Assert(gstr.SearchArray(array, "c"), 2)
|
||||
gtest.Assert(gstr.SearchArray(array, "d"), -1)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_InArray(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
array := []string{"a", "b", "c"}
|
||||
gtest.Assert(gstr.InArray(array, "a"), true)
|
||||
gtest.Assert(gstr.InArray(array, "b"), true)
|
||||
gtest.Assert(gstr.InArray(array, "c"), true)
|
||||
gtest.Assert(gstr.InArray(array, "d"), false)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IsLetterLower(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gstr.IsLetterLower('a'), true)
|
||||
|
||||
@ -2,11 +2,13 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
)
|
||||
|
||||
func main() {
|
||||
f, e := gfile.Open("/Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/third")
|
||||
fmt.Println(e)
|
||||
fmt.Println(f)
|
||||
s := "abc我是中国人é"
|
||||
fmt.Println(len(s))
|
||||
|
||||
for i := 0; i < len(s); i++ {
|
||||
fmt.Println(s[i])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user