add http/https scheme for log of ghttp.Server; add transport setting to ignore tls cert for ghttp.Client; version updates

This commit is contained in:
John
2019-05-06 09:35:39 +08:00
parent 59ad1a9b00
commit 66e40155a9
6 changed files with 33 additions and 9 deletions

View File

@ -10,7 +10,8 @@ package ghttp
import (
"bytes"
"encoding/json"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"github.com/gogf/gf/g/os/gfile"
@ -40,11 +41,16 @@ func NewClient() *Client {
return &Client{
Client : http.Client {
Transport: &http.Transport {
// 默认不校验HTTPS证书有效性
TLSClientConfig : &tls.Config{
InsecureSkipVerify: true,
},
// 默认关闭KeepAlive功能
DisableKeepAlives: true,
},
},
header : make(map[string]string),
cookies : make(map[string]string),
header : make(map[string]string),
cookies: make(map[string]string),
}
}

View File

@ -3,7 +3,6 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// 请求处理.
package ghttp
@ -58,6 +57,10 @@ func (s *Server)handleRequest(w http.ResponseWriter, r *http.Request) {
if !request.IsExited() {
s.callHookHandler(HOOK_BEFORE_OUTPUT, request)
}
// 如果没有产生异常状态那么设置返回状态为200
if request.Response.Status == 0 {
request.Response.Status = http.StatusOK
}
// error log
if e := recover(); e != nil {
request.Response.WriteStatus(http.StatusInternalServerError)

View File

@ -22,9 +22,13 @@ func (s *Server) handleAccessLog(r *Request) {
v(r)
return
}
content := fmt.Sprintf(`%d "%s %s %s %s"`,
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
content := fmt.Sprintf(`%d "%s %s %s %s %s"`,
r.Response.Status,
r.Method, r.Host, r.URL.String(), r.Proto,
r.Method, scheme, r.Host, r.URL.String(), r.Proto,
)
content += fmt.Sprintf(` %.3f`, float64(r.LeaveTime - r.EnterTime)/1000)
content += fmt.Sprintf(`, %s, "%s", "%s"`, r.GetClientIp(), r.Referer(), r.UserAgent())
@ -45,7 +49,11 @@ func (s *Server) handleErrorLog(error interface{}, r *Request) {
}
// 错误日志信息
content := fmt.Sprintf(`%v, "%s %s %s %s"`, error, r.Method, r.Host, r.URL.String(), r.Proto)
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
content := fmt.Sprintf(`%v, "%s %s %s %s %s"`, error, r.Method, scheme, r.Host, r.URL.String(), r.Proto)
if r.LeaveTime > r.EnterTime {
content += fmt.Sprintf(` %.3f`, float64(r.LeaveTime - r.EnterTime)/1000)
} else {

View File

@ -1,12 +1,18 @@
package main
import (
"crypto/tls"
"fmt"
"github.com/gogf/gf/g/net/ghttp"
"net/http"
)
func main() {
c := ghttp.NewClient()
r, _ := c.Get("http://baidu.com")
c.Transport = &http.Transport{
TLSClientConfig : &tls.Config{ InsecureSkipVerify: true},
}
r, e := c.Clone().Get("https://127.0.0.1:8199")
fmt.Println(e)
fmt.Println(r.StatusCode)
}

View File

@ -10,6 +10,7 @@ func main() {
r.Response.Writeln("来自于HTTPS的哈喽世界")
})
s.EnableHTTPS("./server.crt", "./server.key")
s.SetAccessLogEnabled(true)
s.SetPort(8199)
s.Run()
}

View File

@ -1,4 +1,4 @@
package gf
const VERSION = "v1.6.8"
const VERSION = "v1.6.9"
const AUTHORS = "john<john@goframe.org>"