From 8fabc2f5e1f90d64c4fb8dfbd8df6180a063a4b5 Mon Sep 17 00:00:00 2001 From: nabbar Date: Mon, 22 Dec 2025 13:08:08 +0100 Subject: [PATCH] Package HTTPServer: - Add: error for invalid address check - Move: errors string InvalidInstance as golib/error type - Update: add check of listen address in port use function --- httpserver/error.go | 6 ++++++ httpserver/server.go | 12 +++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/httpserver/error.go b/httpserver/error.go index 9e336aa..ad8541b 100644 --- a/httpserver/error.go +++ b/httpserver/error.go @@ -34,9 +34,11 @@ import ( const ( ErrorParamEmpty liberr.CodeError = iota + liberr.MinPkgHttpServer + ErrorInvalidInstance ErrorHTTP2Configure ErrorServerValidate ErrorServerStart + ErrorInvalidAddress ErrorPortUse ) @@ -51,12 +53,16 @@ func getMessage(code liberr.CodeError) (message string) { switch code { case ErrorParamEmpty: return "given parameters is empty" + case ErrorInvalidInstance: + return "invalid instance" case ErrorHTTP2Configure: return "cannot initialize http2 over http srv" case ErrorServerValidate: return "config srv seems to be not valid" case ErrorServerStart: return "server killed : server start but not listen" + case ErrorInvalidAddress: + return "address seems to be invalid" case ErrorPortUse: return "srv port is still used" } diff --git a/httpserver/server.go b/httpserver/server.go index 9db99f1..549f992 100644 --- a/httpserver/server.go +++ b/httpserver/server.go @@ -28,7 +28,6 @@ package httpserver import ( "context" - "errors" "fmt" "log" "net" @@ -43,8 +42,6 @@ import ( libsrv "github.com/nabbar/golib/runner" ) -var errInvalid = errors.New("invalid instance") - func (o *srv) getServer() *http.Server { if o == nil || o.s == nil { return nil @@ -65,7 +62,7 @@ func (o *srv) delServer() { func (o *srv) setServer(ctx context.Context) error { if o == nil || o.s == nil { - return errInvalid + return ErrorInvalidInstance.Error() } var ( @@ -137,7 +134,7 @@ func (o *srv) Start(ctx context.Context) error { func (o *srv) Stop(ctx context.Context) error { if o == nil || o.s == nil || o.r == nil { - return errInvalid + return ErrorInvalidInstance.Error() } r := o.r.Load() @@ -222,6 +219,11 @@ func (o *srv) PortNotUse(ctx context.Context, listen string) error { if strings.Contains(listen, ":") { part := strings.Split(listen, ":") + + if len(part) < 2 { + return ErrorInvalidAddress.Error() + } + port := part[len(part)-1] addr := strings.Join(part[:len(part)-1], ":")