Skip to content

Commit

Permalink
grope enhance
Browse files Browse the repository at this point in the history
  • Loading branch information
AbericYang committed Jul 15, 2020
1 parent d853d5a commit fe18dcb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ gen### Go template
/example/ca_grope_sql_de/
/example/cas.zip
/example/cas.tar
/example/ca/ssl/
17 changes: 8 additions & 9 deletions example/grope/grope_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package main
import (
"github.com/aberic/gnomon"
"github.com/aberic/gnomon/grope"
"github.com/aberic/gnomon/grope/tune"
"github.com/aberic/gnomon/log"
"net/http"
)
Expand All @@ -42,9 +41,9 @@ func main() {
grope.ListenAndServeTLS(
httpServe,
":8888",
"./example/ca/server/rootCA.crt",
"./example/ca/server/rootCA.key",
"./example/ca/client/rootCA.crt")
"example/ca/server/rootCA.crt",
"example/ca/server/rootCA.key",
"example/ca/client/rootCA.crt")
}

func doFilter1(ctx *grope.Context) {
Expand Down Expand Up @@ -125,10 +124,10 @@ func one3(ctx *grope.Context) {

func one4(ctx *grope.Context) {
ones, _ := ctx.ReceiveMultipartForm()
log.Info("one", log.Field("u", ones["u"]), log.Field("v", ones["v"]),
log.Info("one", log.Field("u", ones.Params["u"]), log.Field("v", ones.Params["v"]),
log.Field("url", ctx.Request().URL.String()),
log.Field("a", ctx.Values()["a"]), log.Field("b", ctx.Values()["b"]))
file := ones["file1"].(*tune.FormFile)
file := ones.Files["file1"][0]
_, _ = gnomon.FileAppend("tmp/httpFileTest/"+file.FileName, file.Data, true)
log.Info("one1", log.Field("resp", ctx.ResponseJSON(http.StatusOK, &TestTwo{
Two: "2",
Expand All @@ -139,12 +138,12 @@ func one4(ctx *grope.Context) {

func one5(ctx *grope.Context) {
ones, _ := ctx.ReceiveMultipartForm()
log.Info("one", log.Field("u", ones["u"]), log.Field("v", ones["v"]),
log.Info("one", log.Field("u", ones.Params["u"]), log.Field("v", ones.Params["v"]),
log.Field("url", ctx.Request().URL.String()),
log.Field("a", ctx.Values()["a"]), log.Field("b", ctx.Values()["b"]))

file1 := ones["wk"].(*tune.FormFile)
file2 := ones["kw"].(*tune.FormFile)
file1 := ones.Files["wk"][0]
file2 := ones.Files["kw"][0]
_, _ = gnomon.FileAppend("tmp/httpFileTest/"+file1.FileName, file1.Data, true)
_, _ = gnomon.FileAppend("tmp/httpFileTest/"+file2.FileName, file2.Data, true)
log.Info("one1", log.Field("resp", ctx.ResponseJSON(http.StatusOK, &TestTwo{
Expand Down
4 changes: 2 additions & 2 deletions grope/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (c *Context) ResponseText(statusCode int, text string) error {
// ResponseFile 返回一个"application/octet-stream"
//
// statusCode eg:http.StatusOK
func (c *Context) ResponseFile(statusCode int, filepath string) error {
func (c *Context) ResponseFile(statusCode int, filename, filepath string) error {
var (
file *os.File
fileInfo os.FileInfo
Expand All @@ -336,7 +336,7 @@ func (c *Context) ResponseFile(statusCode int, filepath string) error {
if fileInfo, err = file.Stat(); nil != err {
return err
}
c.HeaderSet("Content-Disposition", "attachment; filename="+file.Name())
c.HeaderSet("Content-Disposition", "attachment; filename="+filename)
c.HeaderSet("Content-Type", http.DetectContentType(fileHeader))
c.HeaderSet("Content-Length", strconv.FormatInt(fileInfo.Size(), 10))
c.Status(statusCode)
Expand Down
24 changes: 13 additions & 11 deletions grope/grope.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,26 @@ func ListenAndServe(Addr string, gs *GHttpServe) {
//
// 必须提供包含证书和与服务器匹配的私钥的文件。如果证书是由证书颁发机构签署的,则certFile应该是服务器证书、任何中间体和CA证书的连接。
func ListenAndServeTLS(gs *GHttpServe, Addr, certFilePath, keyFilePath string, caCertFilePaths ...string) {
pool := x509.NewCertPool()
//加载根证书,用于验证对方合法性
for _, caCertFilePath := range caCertFilePaths {
//这里读取的是根证书
if buf, err := ioutil.ReadFile(caCertFilePath); err != nil {
log.Panic("ListenAndServeTLS ReadFile", log.Err(err))
} else {
pool.AppendCertsFromPEM(buf)
}
}
//加载服务端证书,用于对方验证我方合法性
if cert, err := tls.LoadX509KeyPair(certFilePath, keyFilePath); err != nil {
log.Panic("ListenAndServeTLS LoadX509KeyPair", log.Err(err))
} else {
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: pool,
}
if nil != caCertFilePaths && len(caCertFilePaths) > 0 {
clientCertPool := x509.NewCertPool()
//加载根证书,用于验证对方合法性
for _, caCertFilePath := range caCertFilePaths {
//这里读取的是根证书
if buf, err := ioutil.ReadFile(caCertFilePath); err != nil {
log.Panic("ListenAndServeTLS ReadFile", log.Err(err))
} else {
clientCertPool.AppendCertsFromPEM(buf)
}
}
tlsConfig.ClientCAs = clientCertPool
}
tlsConfig.Time = time.Now
tlsConfig.Rand = rand.Reader
Expand Down
9 changes: 6 additions & 3 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,12 @@ func TestHttpClientCommon_PostTLS(t *testing.T) {
Ones: true,
OneGo: 1,
}, &HTTPTLSConfig{
RootCrtFilePath: "./example/ca/server/rootCA.crt",
CertFilePath: "./example/ca/client/rootCA.crt",
KeyFilePath: "./example/ca/client/rootCA.key",
RootCrtFilePath: "./example/ca/server/rootCA.crt",
CertFilePath: "./example/ca/client/rootCA.crt",
KeyFilePath: "./example/ca/client/rootCA.key",
//RootCrtFilePath: "tmp/example/ca/fabric/pksc1/2048/rootCA.crt",
//CertFilePath: "tmp/example/ca/fabric/pksc1/2048/rootCA.crt",
//KeyFilePath: "tmp/example/ca/fabric/pksc1/2048/rootCA.key",
InsecureSkipVerify: false,
}); nil != err {
t.Skip(err)
Expand Down

0 comments on commit fe18dcb

Please sign in to comment.