From c3e6ed80c13b003c15fc627cbf67462b1d2724be Mon Sep 17 00:00:00 2001 From: ProtoTess <32490978+0x524A@users.noreply.github.com> Date: Tue, 11 Nov 2025 02:32:23 +0000 Subject: [PATCH] Refactor code to improve error handling and remove unused functions --- examples/debug-soap/main.go | 4 ++-- server/media.go | 7 ------- server/server.go | 17 ++++------------- server/soap/handler.go | 6 +++--- server/types.go | 11 +++++------ 5 files changed, 14 insertions(+), 31 deletions(-) diff --git a/examples/debug-soap/main.go b/examples/debug-soap/main.go index aeb4ef2..2c79b40 100644 --- a/examples/debug-soap/main.go +++ b/examples/debug-soap/main.go @@ -60,7 +60,7 @@ type GetDeviceInformation struct { func createSecurityHeader(username, password string) *Security { nonceBytes := make([]byte, 16) - rand.Read(nonceBytes) + _, _ = rand.Read(nonceBytes) nonce := base64.StdEncoding.EncodeToString(nonceBytes) created := time.Now().UTC().Format(time.RFC3339) @@ -134,7 +134,7 @@ func main() { if err != nil { log.Fatalf("Failed to send request: %v", err) } - defer resp.Body.Close() + defer func() { _ = resp.Body.Close() }() // Read response respBody, err := io.ReadAll(resp.Body) diff --git a/server/media.go b/server/media.go index 012bcb9..b7b8799 100644 --- a/server/media.go +++ b/server/media.go @@ -3,7 +3,6 @@ package server import ( "encoding/xml" "fmt" - "time" ) // Media service SOAP message types @@ -374,9 +373,3 @@ func unmarshalBody(body interface{}, target interface{}) error { } return xml.Unmarshal(bodyXML, target) } - -// Helper to format duration as ISO 8601 -func formatDuration(d time.Duration) string { - seconds := int(d.Seconds()) - return fmt.Sprintf("PT%dS", seconds) -} diff --git a/server/server.go b/server/server.go index fa7ada1..5055566 100644 --- a/server/server.go +++ b/server/server.go @@ -4,8 +4,6 @@ import ( "context" "fmt" "net/http" - "strconv" - "sync" "time" "github.com/0x524A/go-onvif/server/soap" @@ -305,8 +303,8 @@ func (s *Server) GetImagingState(videoSourceToken string) (*ImagingState, bool) // ServerInfo returns human-readable server information func (s *Server) ServerInfo() string { var info string - info += fmt.Sprintf("ONVIF Server Configuration\n") - info += fmt.Sprintf("==========================\n") + info += "ONVIF Server Configuration\n" + info += "==========================\n" info += fmt.Sprintf("Device: %s %s\n", s.config.DeviceInfo.Manufacturer, s.config.DeviceInfo.Model) info += fmt.Sprintf("Firmware: %s\n", s.config.DeviceInfo.FirmwareVersion) info += fmt.Sprintf("Serial: %s\n", s.config.DeviceInfo.SerialNumber) @@ -324,19 +322,12 @@ func (s *Server) ServerInfo() string { info += fmt.Sprintf(" RTSP: %s\n", stream.StreamURI) } if profile.PTZ != nil { - info += fmt.Sprintf(" PTZ: Enabled\n") + info += " PTZ: Enabled\n" } } - info += fmt.Sprintf("\nCapabilities:\n") + info += "\nCapabilities:\n" info += fmt.Sprintf(" PTZ: %v\n", s.config.SupportPTZ) info += fmt.Sprintf(" Imaging: %v\n", s.config.SupportImaging) info += fmt.Sprintf(" Events: %v\n", s.config.SupportEvents) return info } - -// Helper to convert int to string (since strconv is imported but not fmt in types.go) -func intToStr(i int) string { - return strconv.Itoa(i) -} - -var _ sync.Locker = (*sync.Mutex)(nil) // Ensure sync is used diff --git a/server/soap/handler.go b/server/soap/handler.go index 9b2cde4..d685c29 100644 --- a/server/soap/handler.go +++ b/server/soap/handler.go @@ -52,7 +52,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.sendFault(w, "Receiver", "Failed to read request body", err.Error()) return } - defer r.Body.Close() + _ = r.Body.Close() // Extract action from raw XML first (before parsing) action := h.extractAction(body) @@ -177,7 +177,7 @@ func (h *Handler) sendResponse(w http.ResponseWriter, response interface{}) { // Send response w.Header().Set("Content-Type", "application/soap+xml; charset=utf-8") w.WriteHeader(http.StatusOK) - w.Write(xmlBody) + _, _ = w.Write(xmlBody) } // sendFault sends a SOAP fault response @@ -207,7 +207,7 @@ func (h *Handler) sendFault(w http.ResponseWriter, code, reason, detail string) // Send fault response w.Header().Set("Content-Type", "application/soap+xml; charset=utf-8") w.WriteHeader(http.StatusInternalServerError) - w.Write(xmlBody) + _, _ = w.Write(xmlBody) } // RequestWrapper wraps incoming SOAP request structures diff --git a/server/types.go b/server/types.go index d551044..636a483 100644 --- a/server/types.go +++ b/server/types.go @@ -156,12 +156,11 @@ type StreamConfig struct { // Server represents the ONVIF server type Server struct { - config *Config - streams map[string]*StreamConfig // Profile token -> stream config - ptzState map[string]*PTZState // Profile token -> PTZ state - imagingState map[string]*ImagingState // Video source token -> imaging state - systemTime time.Time - authenticated bool + config *Config + streams map[string]*StreamConfig // Profile token -> stream config + ptzState map[string]*PTZState // Profile token -> PTZ state + imagingState map[string]*ImagingState // Video source token -> imaging state + systemTime time.Time } // PTZState represents the current PTZ state