refactor: update HTTP request handling and improve documentation

- Replaced http.NewRequest with http.NewRequestWithContext in client tests for better context management.
- Updated method names and comments for clarity, including renaming GetWsdlUrl to GetWsdlURL and StorageUri to StorageURI for consistency.
- Enhanced comments across various files to provide clearer descriptions of functionality and ONVIF specifications.
This commit is contained in:
0x524a
2025-12-02 08:41:37 -05:00
parent 9e3b5e0170
commit 96ac509c24
32 changed files with 248 additions and 184 deletions
+6 -6
View File
@@ -17,7 +17,7 @@ type GetDeviceInformationResponse struct {
Model string `xml:"Model"`
FirmwareVersion string `xml:"FirmwareVersion"`
SerialNumber string `xml:"SerialNumber"`
HardwareId string `xml:"HardwareId"`
HardwareID string `xml:"HardwareId"`
}
// GetCapabilitiesResponse represents GetCapabilities response.
@@ -110,8 +110,8 @@ type MediaCapabilities struct {
// StreamingCapabilities represents streaming capabilities.
type StreamingCapabilities struct {
RTPMulticast bool `xml:"RTPMulticast,attr"`
RTP_TCP bool `xml:"RTP_TCP,attr"`
RTP_RTSP_TCP bool `xml:"RTP_RTSP_TCP,attr"`
RTPTCP bool `xml:"RTP_TCP,attr"`
RTPRTSPTCP bool `xml:"RTP_RTSP_TCP,attr"`
}
// PTZCapabilities represents PTZ service capabilities.
@@ -153,7 +153,7 @@ func (s *Server) HandleGetDeviceInformation(body interface{}) (interface{}, erro
Model: s.config.DeviceInfo.Model,
FirmwareVersion: s.config.DeviceInfo.FirmwareVersion,
SerialNumber: s.config.DeviceInfo.SerialNumber,
HardwareId: s.config.DeviceInfo.HardwareID,
HardwareID: s.config.DeviceInfo.HardwareID,
}, nil
}
@@ -204,8 +204,8 @@ func (s *Server) HandleGetCapabilities(body interface{}) (interface{}, error) {
XAddr: baseURL + "/media_service",
StreamingCapabilities: &StreamingCapabilities{
RTPMulticast: false,
RTP_TCP: true,
RTP_RTSP_TCP: true,
RTPTCP: true,
RTPRTSPTCP: true,
},
},
}
+10 -10
View File
@@ -28,7 +28,7 @@ func TestHandleGetDeviceInformation(t *testing.T) {
{"Model", deviceResp.Model, config.DeviceInfo.Model},
{"FirmwareVersion", deviceResp.FirmwareVersion, config.DeviceInfo.FirmwareVersion},
{"SerialNumber", deviceResp.SerialNumber, config.DeviceInfo.SerialNumber},
{"HardwareId", deviceResp.HardwareId, config.DeviceInfo.HardwareID},
{"HardwareID", deviceResp.HardwareID, config.DeviceInfo.HardwareID},
}
for _, tt := range tests {
@@ -162,7 +162,7 @@ func TestGetDeviceInformationResponseXML(t *testing.T) {
Model: "TestModel",
FirmwareVersion: "1.0.0",
SerialNumber: "SN123",
HardwareId: "HW001",
HardwareID: "HW001",
}
// Marshal to XML
@@ -209,8 +209,8 @@ func TestCapabilitiesStructure(t *testing.T) {
XAddr: "http://localhost:8080/onvif/media_service",
StreamingCapabilities: &StreamingCapabilities{
RTPMulticast: true,
RTP_TCP: true,
RTP_RTSP_TCP: true,
RTPTCP: true,
RTPRTSPTCP: true,
},
},
}
@@ -239,8 +239,8 @@ func TestMediaCapabilitiesStructure(t *testing.T) {
XAddr: "http://localhost:8080/onvif/media_service",
StreamingCapabilities: &StreamingCapabilities{
RTPMulticast: true,
RTP_TCP: true,
RTP_RTSP_TCP: true,
RTPTCP: true,
RTPRTSPTCP: true,
},
}
@@ -251,10 +251,10 @@ func TestMediaCapabilitiesStructure(t *testing.T) {
if !caps.StreamingCapabilities.RTPMulticast {
t.Error("RTP Multicast should be supported")
}
if !caps.StreamingCapabilities.RTP_TCP {
if !caps.StreamingCapabilities.RTPTCP {
t.Error("RTP TCP should be supported")
}
if !caps.StreamingCapabilities.RTP_RTSP_TCP {
if !caps.StreamingCapabilities.RTPRTSPTCP {
t.Error("RTSP should be supported")
}
}
@@ -368,8 +368,8 @@ func TestGetCapabilitiesResponse(t *testing.T) {
XAddr: "http://localhost:8080/media",
StreamingCapabilities: &StreamingCapabilities{
RTPMulticast: true,
RTP_TCP: true,
RTP_RTSP_TCP: true,
RTPTCP: true,
RTPRTSPTCP: true,
},
},
}
+2
View File
@@ -266,6 +266,8 @@ func (s *Server) HandleGetImagingSettings(body interface{}) (interface{}, error)
}
// HandleSetImagingSettings handles SetImagingSettings request.
//
//nolint:gocyclo // SetImagingSettings has high complexity due to multiple validation and update paths
func (s *Server) HandleSetImagingSettings(body interface{}) (interface{}, error) {
var req SetImagingSettingsRequest
if err := unmarshalBody(body, &req); err != nil {
+9 -9
View File
@@ -138,12 +138,12 @@ type IPAddress struct {
// GetStreamURIResponse represents GetStreamURI response.
type GetStreamURIResponse struct {
XMLName xml.Name `xml:"http://www.onvif.org/ver10/media/wsdl GetStreamURIResponse"`
MediaUri MediaUri `xml:"MediaUri"`
MediaURI MediaURI `xml:"MediaUri"`
}
// MediaUri represents a media URI.
type MediaUri struct {
Uri string `xml:"Uri"`
// MediaURI represents a media URI.
type MediaURI struct {
URI string `xml:"Uri"`
InvalidAfterConnect bool `xml:"InvalidAfterConnect"`
InvalidAfterReboot bool `xml:"InvalidAfterReboot"`
Timeout string `xml:"Timeout"`
@@ -152,7 +152,7 @@ type MediaUri struct {
// GetSnapshotURIResponse represents GetSnapshotURI response.
type GetSnapshotURIResponse struct {
XMLName xml.Name `xml:"http://www.onvif.org/ver10/media/wsdl GetSnapshotURIResponse"`
MediaUri MediaUri `xml:"MediaUri"`
MediaURI MediaURI `xml:"MediaUri"`
}
// GetVideoSourcesResponse represents GetVideoSources response.
@@ -287,8 +287,8 @@ func (s *Server) HandleGetStreamURI(body interface{}) (interface{}, error) {
}
return &GetStreamURIResponse{
MediaUri: MediaUri{
Uri: uri,
MediaURI: MediaURI{
URI: uri,
InvalidAfterConnect: false,
InvalidAfterReboot: true,
Timeout: "PT60S",
@@ -333,8 +333,8 @@ func (s *Server) HandleGetSnapshotURI(body interface{}) (interface{}, error) {
host, s.config.Port, s.config.BasePath, req.ProfileToken)
return &GetSnapshotURIResponse{
MediaUri: MediaUri{
Uri: uri,
MediaURI: MediaURI{
URI: uri,
InvalidAfterConnect: false,
InvalidAfterReboot: true,
Timeout: "PT5S",
+4 -4
View File
@@ -52,15 +52,15 @@ func TestHandleGetStreamURI(t *testing.T) {
t.Fatalf("Response is not GetStreamURIResponse, got %T", resp)
}
if streamResp.MediaUri.Uri == "" {
if streamResp.MediaURI.URI == "" {
t.Error("Stream URI is empty")
return
}
// URI should contain stream path
if !contains(streamResp.MediaUri.Uri, "rtsp://") {
t.Errorf("Invalid stream URI format: %s", streamResp.MediaUri.Uri)
if !contains(streamResp.MediaURI.URI, "rtsp://") {
t.Errorf("Invalid stream URI format: %s", streamResp.MediaURI.URI)
}
}
@@ -80,7 +80,7 @@ func TestHandleGetSnapshotURI(t *testing.T) {
t.Fatalf("Response is not GetSnapshotURIResponse, got %T", resp)
}
if snapResp.MediaUri.Uri == "" {
if snapResp.MediaURI.URI == "" {
t.Error("Snapshot URI is empty")
}
}
+2 -2
View File
@@ -3,7 +3,7 @@ package soap
import (
"bytes"
"crypto/sha1"
"crypto/sha1" //nolint:gosec // SHA1 used for ONVIF digest authentication
"encoding/base64"
"encoding/xml"
"fmt"
@@ -123,7 +123,7 @@ func (h *Handler) authenticate(envelope *originsoap.Envelope) bool {
}
// Calculate expected digest
hash := sha1.New()
hash := sha1.New() //nolint:gosec // SHA1 required for ONVIF digest auth
hash.Write(nonce)
hash.Write([]byte(token.Created))
hash.Write([]byte(h.password))
+2
View File
@@ -228,6 +228,8 @@ type WDRSettings struct {
}
// DefaultConfig returns a default server configuration with a multi-lens camera setup.
//
//nolint:funlen // DefaultConfig has many statements due to comprehensive default configuration
func DefaultConfig() *Config {
return &Config{
Host: "0.0.0.0",