diff --git a/net/ghttp/ghttp_server_config.go b/net/ghttp/ghttp_server_config.go index a1f6e479a..8a6634f8d 100644 --- a/net/ghttp/ghttp_server_config.go +++ b/net/ghttp/ghttp_server_config.go @@ -10,6 +10,7 @@ import ( "crypto/tls" "fmt" "github.com/gogf/gf/internal/intlog" + "github.com/gogf/gf/os/gres" "github.com/gogf/gf/util/gutil" "net/http" "strconv" @@ -386,6 +387,10 @@ func (s *Server) EnableHTTPS(certFile, keyFile string, tlsConfig ...*tls.Config) certFileRealPath = gfile.RealPath(gfile.MainPkgPath() + gfile.Separator + certFile) } } + // Resource. + if certFileRealPath == "" && gres.Contains(certFile) { + certFileRealPath = certFile + } if certFileRealPath == "" { s.Logger().Fatal(fmt.Sprintf(`[ghttp] EnableHTTPS failed: certFile "%s" does not exist`, certFile)) } @@ -396,6 +401,10 @@ func (s *Server) EnableHTTPS(certFile, keyFile string, tlsConfig ...*tls.Config) keyFileRealPath = gfile.RealPath(gfile.MainPkgPath() + gfile.Separator + keyFile) } } + // Resource. + if keyFileRealPath == "" && gres.Contains(keyFile) { + keyFileRealPath = keyFile + } if keyFileRealPath == "" { s.Logger().Fatal(fmt.Sprintf(`[ghttp] EnableHTTPS failed: keyFile "%s" does not exist`, keyFile)) } diff --git a/net/ghttp/ghttp_server_graceful.go b/net/ghttp/ghttp_server_graceful.go index 9c19213f4..5a90926a2 100644 --- a/net/ghttp/ghttp_server_graceful.go +++ b/net/ghttp/ghttp_server_graceful.go @@ -12,6 +12,7 @@ import ( "errors" "fmt" "github.com/gogf/gf/os/gproc" + "github.com/gogf/gf/os/gres" "github.com/gogf/gf/text/gstr" "log" "net" @@ -110,7 +111,15 @@ func (s *gracefulServer) ListenAndServeTLS(certFile, keyFile string, tlsConfig . err := error(nil) if len(config.Certificates) == 0 { config.Certificates = make([]tls.Certificate, 1) - config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile) + if gres.Contains(certFile) { + config.Certificates[0], err = tls.X509KeyPair( + gres.GetContent(certFile), + gres.GetContent(keyFile), + ) + } else { + config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile) + } + } if err != nil { return errors.New(fmt.Sprintf(`open cert file "%s","%s" failed: %s`, certFile, keyFile, err.Error())) diff --git a/net/ghttp/ghttp_unit_https_test.go b/net/ghttp/ghttp_unit_https_test.go index 511c43646..25b87995b 100644 --- a/net/ghttp/ghttp_unit_https_test.go +++ b/net/ghttp/ghttp_unit_https_test.go @@ -9,6 +9,7 @@ package ghttp_test import ( "fmt" "github.com/gogf/gf/debug/gdebug" + "github.com/gogf/gf/os/gfile" "github.com/gogf/gf/os/gtime" "github.com/gogf/gf/text/gstr" "testing" @@ -17,6 +18,8 @@ import ( "github.com/gogf/gf/frame/g" "github.com/gogf/gf/net/ghttp" "github.com/gogf/gf/test/gtest" + + _ "github.com/gogf/gf/net/ghttp/testdata/https/packed" ) func Test_HTTPS_Basic(t *testing.T) { @@ -28,8 +31,43 @@ func Test_HTTPS_Basic(t *testing.T) { }) }) s.EnableHTTPS( - gdebug.TestDataPath("https", "server.crt"), - gdebug.TestDataPath("https", "server.key"), + gdebug.TestDataPath("https", "files", "server.crt"), + gdebug.TestDataPath("https", "files", "server.key"), + ) + s.SetPort(p) + s.SetDumpRouterMap(false) + s.Start() + defer s.Shutdown() + + time.Sleep(100 * time.Millisecond) + + // HTTP + gtest.C(t, func(t *gtest.T) { + c := g.Client() + c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) + t.AssertIN(gstr.Trim(c.GetContent("/")), g.Slice{"", "Client sent an HTTP request to an HTTPS server."}) + t.AssertIN(gstr.Trim(c.GetContent("/test")), g.Slice{"", "Client sent an HTTP request to an HTTPS server."}) + }) + // HTTPS + gtest.C(t, func(t *gtest.T) { + c := g.Client() + c.SetPrefix(fmt.Sprintf("https://127.0.0.1:%d", p)) + t.Assert(c.GetContent("/"), "Not Found") + t.Assert(c.GetContent("/test"), "test") + }) +} + +func Test_HTTPS_Resource(t *testing.T) { + p, _ := ports.PopRand() + s := g.Server(p) + s.Group("/", func(group *ghttp.RouterGroup) { + group.GET("/test", func(r *ghttp.Request) { + r.Response.Write("test") + }) + }) + s.EnableHTTPS( + gfile.Join("files", "server.crt"), + gfile.Join("files", "server.key"), ) s.SetPort(p) s.SetDumpRouterMap(false) @@ -66,8 +104,8 @@ func Test_HTTPS_HTTP_Basic(t *testing.T) { }) }) s.EnableHTTPS( - gdebug.TestDataPath("https", "server.crt"), - gdebug.TestDataPath("https", "server.key"), + gdebug.TestDataPath("https", "files", "server.crt"), + gdebug.TestDataPath("https", "files", "server.key"), ) s.SetPort(portHttp) s.SetHTTPSPort(portHttps) diff --git a/net/ghttp/testdata/https/server.crt b/net/ghttp/testdata/https/files/server.crt similarity index 100% rename from net/ghttp/testdata/https/server.crt rename to net/ghttp/testdata/https/files/server.crt diff --git a/net/ghttp/testdata/https/server.key b/net/ghttp/testdata/https/files/server.key similarity index 100% rename from net/ghttp/testdata/https/server.key rename to net/ghttp/testdata/https/files/server.key diff --git a/net/ghttp/testdata/https/packed/packed.go b/net/ghttp/testdata/https/packed/packed.go new file mode 100644 index 000000000..491cb317a --- /dev/null +++ b/net/ghttp/testdata/https/packed/packed.go @@ -0,0 +1,9 @@ +package packed + +import "github.com/gogf/gf/os/gres" + +func init() { + if err := gres.Add("H4sIAAAAAAAC/5TWV1TTydsH8B+EpoAginEBDVIEQYp0kUTpSjAJhCwsxSwlgEtRwVCkKwJSo9IWCCChSAkoTZoivfeF0JEEQpMgvQrvcc/x1X/Zi/9zMTfzzPfMc/OZQcFBLKcBDoAD6DTsRQE/FS9wDLC/64xzl3PHuXng3GRt3R5iTFgBJt6X83cexsJLqjT4mrd9w0fQ6OPh+uAVEbGTj1k4TK1bdBu6nrT2Sab5GyC7ao5yc613l3Ly29Yfbl6fSovmph6AbK6L+TBrhl03yQhO9w7hHUcIcWSAEJd/z6I0MtJHaaKscBAb6STB5unZoVdhgDIFqQaCzyEuLwhza87xLoKCvMJtmjmYMYn63Sjx/X3Iu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4It8cqO+H5pa2KwcTKc3Iho0R72ssr+fQnzhlWmPJhq6CYItPnckjp9nuHkSnRmzym6C2GN5I8I8WVbWKju75d+OvbFldDghD25N2GIFMzPee7ngK5wwbJ7cGaR8NJqa1r8gEM053Lh9BKii86rjgew7aqAoS/f33+2YdCXrDwEM7kkAzBhl4d3UX2HGTXkQ1iz1RpJLp2okp8fL9aJt9zWTnvAEncndWB9RusvDMeNiehnp/zO86fTyUk+BZkmS4r2gnR4zbV98UF8TlTjf1RuItnalBGiXnMoa0dydUvRMX9fV2cd212waFRny7EQ4L4/FqalQRFkzC5C+ocOv3L+FOSL97dku7TetZ7Ur3g1Ko2ZnvuAbPyEvmAme6vmBQ1QJq0v42aWHzE4u+9Z/RG69ZtoEs6h9IbrccQe+yK9kgl+yIyE7jPpk2wjL6/SVacpfnZKef+FdwlZMZZgDbBalpLcncNr++XOPVuXbbzHuqjLmpdmuad6MtiPLWK2fSvWSHSbuSedlgifhWqgA/FebVe9NopPn5Dd9xRPFBrlB5KL5q+SbnDSP0gQHe7r5+o2VHNaYUIaRHkWygeRyC72aU5sT6mRuUR0W/4eBw0ZQvzfDpz1/TuZ7T4zfLpSJfOz6GbMkrTitSSv4ws1kn55L1h4RyMbcUaB4p/VVU+4vmOKYfAsggbKwBEsP3AFACgAWFGP2PK+h3TvwUNyejBfjv7cwcKzsR8GvSD4p+R/kbx98oM+rb+4+ft32N+vt6/xoixAP/s+4+Y/z7L9yfiSPPBMeA/JmNl+7YLAkBAOwAA5L/b/y8AAP//A6tAlY0KAAA="); err != nil { + panic("add binary content to resource manager failed: " + err.Error()) + } +}