@@ -97,6 +97,9 @@ type Server interface {
9797 //
9898 // Duplicates will be removed automatically.
9999 AddSupportedSubprotocol (subProto string )
100+ // SetChargePointIdResolver sets the callback function to use for resolving the charge point ID of a charger connecting to
101+ // the websocket server. By default, this will just be the path in the URL used by the client.
102+ SetChargePointIdResolver (resolver func (r * http.Request ) (string , error ))
100103 // SetBasicAuthHandler enables HTTP Basic Authentication and requires clients to pass credentials.
101104 // The handler function is called whenever a new client attempts to connect, to check for credentials correctness.
102105 // The handler must return true if the credentials were correct, false otherwise.
@@ -127,21 +130,22 @@ type Server interface {
127130//
128131// Use the NewServer function to create a new server.
129132type server struct {
130- connections map [string ]* webSocket
131- httpServer * http.Server
132- messageHandler func (ws Channel , data []byte ) error
133- checkClientHandler CheckClientHandler
134- newClientHandler func (ws Channel )
135- disconnectedHandler func (ws Channel )
136- basicAuthHandler func (username string , password string ) bool
137- tlsCertificatePath string
138- tlsCertificateKey string
139- timeoutConfig ServerTimeoutConfig
140- upgrader websocket.Upgrader
141- errC chan error
142- connMutex sync.RWMutex
143- addr * net.TCPAddr
144- httpHandler * mux.Router
133+ connections map [string ]* webSocket
134+ httpServer * http.Server
135+ messageHandler func (ws Channel , data []byte ) error
136+ chargePointIdResolver func (* http.Request ) (string , error )
137+ checkClientHandler CheckClientHandler
138+ newClientHandler func (ws Channel )
139+ disconnectedHandler func (ws Channel )
140+ basicAuthHandler func (username string , password string ) bool
141+ tlsCertificatePath string
142+ tlsCertificateKey string
143+ timeoutConfig ServerTimeoutConfig
144+ upgrader websocket.Upgrader
145+ errC chan error
146+ connMutex sync.RWMutex
147+ addr * net.TCPAddr
148+ httpHandler * mux.Router
145149}
146150
147151// ServerOpt is a function that can be used to set options on a server during creation.
@@ -183,6 +187,10 @@ func NewServer(opts ...ServerOpt) Server {
183187 timeoutConfig : NewServerTimeoutConfig (),
184188 upgrader : websocket.Upgrader {Subprotocols : []string {}},
185189 httpHandler : router ,
190+ chargePointIdResolver : func (r * http.Request ) (string , error ) {
191+ url := r .URL
192+ return path .Base (url .Path ), nil
193+ },
186194 }
187195 for _ , o := range opts {
188196 o (s )
@@ -220,6 +228,10 @@ func (s *server) AddSupportedSubprotocol(subProto string) {
220228 s .upgrader .Subprotocols = append (s .upgrader .Subprotocols , subProto )
221229}
222230
231+ func (s * server ) SetChargePointIdResolver (resolver func (r * http.Request ) (string , error )) {
232+ s .chargePointIdResolver = resolver
233+ }
234+
223235func (s * server ) SetBasicAuthHandler (handler func (username string , password string ) bool ) {
224236 s .basicAuthHandler = handler
225237}
@@ -343,8 +355,12 @@ func (s *server) Write(webSocketId string, data []byte) error {
343355
344356func (s * server ) wsHandler (w http.ResponseWriter , r * http.Request ) {
345357 responseHeader := http.Header {}
346- url := r .URL
347- id := path .Base (url .Path )
358+ id , err := s .chargePointIdResolver (r )
359+ if err != nil {
360+ s .error (fmt .Errorf ("failed to resolve charge point id" ))
361+ http .Error (w , "NotFound" , http .StatusNotFound )
362+ return
363+ }
348364 log .Debugf ("handling new connection for %s from %s" , id , r .RemoteAddr )
349365 // Negotiate sub-protocol
350366 clientSubProtocols := websocket .Subprotocols (r )
0 commit comments