Refactor code to improve error handling and remove unused functions
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
+4
-13
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
+5
-6
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user