Remoto - VFS: VFS_websocket_server Class Reference
Remoto - VFS

A VFS_websocket server will listen for incoming websocket connections, optionally using ssl. More...

#include <VFS_websocket_server.h>

Inheritance diagram for VFS_websocket_server:
VFS_node VFS_remotoserver

Public Slots

virtual void clientBinaryMessage (QByteArray message)
 The client has received a binary websocket message. More...
 
virtual void clientError (QAbstractSocket::SocketError error)
 A socket error has occurred. More...
 
virtual void clientTextMessage (QString message)
 A client has received a text websocket message. More...
 
virtual bool listen ()
 Start listening on the _socket for incoming connections. More...
 
virtual void serverError (QWebSocketProtocol::CloseCode closeCode)
 A server socket error has occurred. More...
 
- Public Slots inherited from VFS_node
virtual void applyDiff (VFS_request *r)
 Apply a diff received via subscription. More...
 
virtual void executeRequest (VFS_request *t)
 Based on the VFS_request::requestType, execute the function associated with an operation. More...
 
void notifySubscribers (VFS_node *origin, VFS_request *t)
 Propagate a diff to subscribers. More...
 
virtual void receiveResponse (VFS_request *t)
 Once a VFS_request has been completed, a response will be issued back to its _origin. More...
 
void remove (bool andDelete)
 Remove a child node. More...
 
virtual void remove (VFS_node *node=nullptr, QString *name=nullptr, QString user="server")
 Remove a child node from this node. More...
 
virtual void subtreeRequest (VFS_request *t)
 find() the target of a VFS_request, and execute the request More...
 
virtual void unsubscribeAll (VFS_node *n)
 Remove all references to a subscriber from this node. More...
 

Public Member Functions

 VFS_websocket_server (quint16 port, QString authPaths, QString sessions, QHostAddress addr=QHostAddress::Any, QWebSocketServer::SslMode ssl=QWebSocketServer::NonSecureMode, QString sslCertPath="", QString sslKeyPath="")
 VFS_websocket_server constructor. More...
 
virtual ~VFS_websocket_server ()
 VFS_websocket_server destructor. More...
 
virtual bool authenticate (VFS_websocket_client *client, QJsonObject credentials)
 Authenticate a client based on received credentials. More...
 
virtual void authorize (VFS_websocket_client *client, QJsonObject authdata)
 Authorize a client and create a session based on type and username. More...
 
virtual bool isContainer ()
 A VFS_websocket_server is always a container; it's children will be the connected clients. More...
 
- Public Member Functions inherited from VFS_node
Q_INVOKABLE VFS_node ()
 The VFS_node constructor will add its instance to the VFS_node::__allNodes global node registry, observing thread safety rules. More...
 
virtual ~VFS_node ()
 VFS_node destructor. More...
 
virtual VFS_nodeappend (QString name, VFS_node *node, bool containerCheck=true, QString user="server")
 Append a VFS_node as a child of this node. More...
 
QString className ()
 Return the class name of a node. More...
 
virtual VFS_requestcreateRequest (VFS_request::requestType type, QString path, QString user="unknown", QJsonDocument data=QJsonDocument(), QJsonObject metadata=QJsonObject(), bool dontDelete=false)
 Create a new VFS_request with this object as _origin. More...
 
VFS_nodefind (QString path)
 Find a node by string path. More...
 
virtual VFS_nodefind (VFS_request *r)
 Find a node using a VFS_request. More...
 
VFS_nodefindChildWithName (QString name)
 Check if a child with a given name exists. More...
 
virtual VFS_nodemount ()
 Mount this node. More...
 
virtual QString reportDetails ()
 Additional details for a generated report. More...
 
QString uniqueChildName (QString name)
 Generate a unique child name. More...
 
virtual VFS_nodeunmount ()
 Unmount this node. More...
 
virtual bool validChildName (QString name)
 Check if a node name is valid. More...
 

Protected Slots

virtual void closeConnection ()
 Close a child client connection, and remove() it from this node's child list. More...
 
virtual void newConnection ()
 A new connection has been requested, and is pending. More...
 
virtual void sslErrors (const QList< QSslError > &errors)
 One or more ssl errors have occurred. More...
 

Protected Member Functions

virtual VFS_websocket_clientcreateWebsocketClient (QWebSocket *s)
 Create a VFS_websocket_client to host a new connection. More...
 
- Protected Member Functions inherited from VFS_node
virtual void aclDefaults (VFS_request *r)
 Return default values and features associated wth this node. More...
 
void addACLDefault (QJsonObject &acl, bool value, QString description="")
 Add a default value to the acl object. More...
 
void addACLFeature (QJsonObject &acl, QString feature, bool value, QString description="")
 Add a feature to the acl object. More...
 
void addACLFeatureGroup (QJsonObject &acl, QString feature, QString group, bool value)
 Add a feature group to the acl object. More...
 
void addACLFeatureUser (QJsonObject &acl, QString feature, QString user, bool value)
 Add a feature user to the acl object. More...
 
void addACLGroup (QJsonObject &acl, QString group, bool value)
 Add a group to the acl object. More...
 
void addACLUser (QJsonObject &acl, QString user, bool value)
 Add a user to the acl object. More...
 
virtual QByteArray icon ()
 Fetch the icon for a node. More...
 
virtual void issueRequest (VFS_node *target, VFS_request *t)
 Issue a VFS_request to its target. More...
 
virtual void issueRequest (VFS_request *t)
 A convenience function. More...
 
virtual void issueResponse (VFS_request *t)
 Once a request has been completed, issue a response. More...
 
virtual void ls (VFS_request *r)
 List the contents of this node. More...
 
virtual void metadata (VFS_request *r)
 Fetch the metadata of this node. More...
 
virtual void read (VFS_request *r)
 Return the data contents of this node, or if it's a container call ls() More...
 
virtual void releaseLock (VFS_request *r)
 Release a lock on this node. More...
 
virtual void report (VFS_request *r)
 Report debugging information about the current state of this node. More...
 
virtual void requestLock (VFS_request *r)
 Request a lock on this node. More...
 
virtual void rm (VFS_request *r)
 Remove a child entry from a node, or the node itself. More...
 
virtual void submit (VFS_request *r)
 Submit a diff to a node. More...
 
virtual void subscribe (VFS_request *r)
 Add an entry to this node's _subscription list. More...
 
virtual void unsubscribe (VFS_request *r)
 Remove an entry from this node's _subscription list. More...
 
virtual void unsubscribePath (QString path)
 Unsubscribe all references to a subpath. More...
 
virtual void write (VFS_request *r)
 Write data to this node. More...
 

Protected Attributes

QStringList _authPaths
 The list of auth paths to query when a user request authentication. More...
 
QHostAddress _listenAddress
 Address to listen on, or 0.0.0.0 for all. More...
 
quint16 _port
 Port to listen on. More...
 
QString _sessionsPath
 The path to the sessionManager node. More...
 
QWebSocketServer _socket
 The QWebSocket server object that will be listening. More...
 
QWebSocketServer::SslMode _ssl
 Use ssl? More...
 
QString _sslCertPath
 ssl cert file path More...
 
QString _sslKeyPath
 ssl key file path More...
 
- Protected Attributes inherited from VFS_node
VFS_children _children
 This node's children. More...
 
QMutex _lock
 A recursive mutex that is local to this node. More...
 
VFS_subscriptionType _subscribers
 This node's subscribers. These subscribers will receive diff notifications. More...
 

Friends

class VFS_websocket_client
 

Additional Inherited Members

- Signals inherited from VFS_node
void diff (VFS_node *origin, VFS_request *t)
 Emit a diff, which will trigger notifySubscribers() for a mounted node. More...
 
void finished (bool andDelete=false)
 Emitted if a thread fails to create its node, or a node is rm()'d, or any other reason a node has completed its lifecycle. It is deleted if andDelete==true. More...
 
void mounted ()
 Emitted when a node is mount()ed. More...
 
void unmounted (VFS_node *self)
 Emitted when a node is unmount()ed. More...
 
- Static Public Member Functions inherited from VFS_node
static bool __isNode (VFS_node *)
 Check to see if a node is in the global registry. More...
 
static QString code (QString nodename, QString libname, QString &error)
 Fetch code or any other resource from a node. More...
 

Detailed Description

A VFS_websocket server will listen for incoming websocket connections, optionally using ssl.

The base class implementation will do nothing useful. It must be subclassed, as well as VFS_websocket_client.

See also
VFS_websocket_client

Definition at line 9 of file VFS_websocket_server.h.

Constructor & Destructor Documentation

◆ VFS_websocket_server()

VFS_websocket_server::VFS_websocket_server ( quint16  port,
QString  authPaths,
QString  sessions,
QHostAddress  addr = QHostAddress::Any,
QWebSocketServer::SslMode  ssl = QWebSocketServer::NonSecureMode,
QString  sslCertPath = "",
QString  sslKeyPath = "" 
)
explicit

VFS_websocket_server constructor.

Parameters
portPort to listen on.
authPathsList of sequential VFS auth paths to authenticate a user.
sessionsThe path to the sessionManager, which will be a "sessions" node
addrAddress to listen on, or 0.0.0.0 for any.
sslUse ssl?
sslCertPathssl cert file path
sslKeyPathssl key file path

This server will listen() when the root VFS emits VFS::initialized().

Definition at line 33 of file VFS_websocket_server.cpp.

◆ ~VFS_websocket_server()

VFS_websocket_server::~VFS_websocket_server ( )
virtual

VFS_websocket_server destructor.

Closes the socket during destruction.

Definition at line 63 of file VFS_websocket_server.cpp.

Member Function Documentation

◆ authenticate()

bool VFS_websocket_server::authenticate ( VFS_websocket_client client,
QJsonObject  credentials 
)
virtual

Authenticate a client based on received credentials.

Parameters
clientThe client requesting authentication.
credentialsThe authentication credentials, probably from the _metadata field of a VFS_request.
Returns
The success or failure of the authentication request.

This method uses an authindex to sequentially send a VFS_request subclass to query an auth path. The success or failure of the request will need to be captured in the subclassed VFS_websocket_client::receiveResponse() function. If it's a failure, a second call to authenticate() with the same _metadata will query the next auth path. If the auth path list has been exhausted, this function will return false, and the user connection should be closed.

This virtual method could be subclassed if needed to change the default functionality.

Todo:
Add case where no auth paths are provided, which would mean that all connection attempts are authenticated.
See also
VFS_remotoserver_client::receiveResponse()

Definition at line 230 of file VFS_websocket_server.cpp.

◆ authorize()

void VFS_websocket_server::authorize ( VFS_websocket_client client,
QJsonObject  authdata 
)
virtual

Authorize a client and create a session based on type and username.

Parameters
clientThe client requesting authorization.
authdataThe data returned from a VFS_auth subclass node.

This method will issue a request to a VFS_sessionHandler to create a new VFS_session for a connected user based on the type of the session.

See also
VFS_sessionHandler
VFS_session

Definition at line 298 of file VFS_websocket_server.cpp.

◆ clientBinaryMessage

void VFS_websocket_server::clientBinaryMessage ( QByteArray  message)
virtualslot

The client has received a binary websocket message.

Parameters
messageThe message

This is an interception point for a VFS_websocket_server. If a server wishes to parse or filter or sanitize incoming socket data, a subclass of this function could do this.

Generally, a VFS_websocket_client should be doing the work on the messages it receives, but in some security contexts, a server may want to supervise communication.

The base implementation does not pass data to the client, and the message dies here.

This must be called as the result of a slot, because sender() is dynamically cast to the VFS_websocket_client.

See also
VFS_remotoserver::clientBinaryMessage()

Definition at line 356 of file VFS_websocket_server.cpp.

◆ clientError

void VFS_websocket_server::clientError ( QAbstractSocket::SocketError  error)
virtualslot

A socket error has occurred.

Log it as an error/warning, but do nothing.

Parameters
errorThe error

Definition at line 410 of file VFS_websocket_server.cpp.

◆ clientTextMessage

void VFS_websocket_server::clientTextMessage ( QString  message)
virtualslot

A client has received a text websocket message.

Parameters
messageThe message

This is an interception point for a VFS_websocket_server. If a server wishes to parse or filter or sanitize incoming socket data, a subclass of this function could do this.

Generally, a VFS_websocket_client should be doing the work on the messages it receives, but in some security contexts, a server may want to supervise communication.

The base implementation does not pass data to the client, and the message dies here.

See also
VFS_remotoserver::clientTextMessage()

Definition at line 388 of file VFS_websocket_server.cpp.

◆ closeConnection

void VFS_websocket_server::closeConnection ( )
protectedvirtualslot

Close a child client connection, and remove() it from this node's child list.

This will also call deleteLater() on the VFS_websocket_client. This is not meant to be called directly, but instead connected as a slot to any other mechanism which may cause a close.

Definition at line 316 of file VFS_websocket_server.cpp.

◆ createWebsocketClient()

VFS_websocket_client * VFS_websocket_server::createWebsocketClient ( QWebSocket *  socket)
protectedvirtual

Create a VFS_websocket_client to host a new connection.

Parameters
socketThe socket returned by _socket.nextPendingConnection().
Returns
The new VFS_websocket_client.

This method is meant to be subclassed to return the type (subclass) of VFS_websocket_client needed by a subclassed server.

Reimplemented in VFS_remotoserver.

Definition at line 207 of file VFS_websocket_server.cpp.

◆ isContainer()

bool VFS_websocket_server::isContainer ( )
virtual

A VFS_websocket_server is always a container; it's children will be the connected clients.

Returns
true

Reimplemented from VFS_node.

Definition at line 73 of file VFS_websocket_server.cpp.

◆ listen

bool VFS_websocket_server::listen ( )
virtualslot

Start listening on the _socket for incoming connections.

Returns
The success or fail of attempting to listen.

If the socket is already listening, this will return false.

The socket will be set to allow a maximum of 100 pending connections. After that it will emit an error, and new clients will not be allowed to connect until some of the pending connections are serviced.

If ssl is requested, the ssl cert and key files will be validated. Any problem here will prevent the socket from opening.

A message is either logged or warned, depending on listen() status.

Definition at line 92 of file VFS_websocket_server.cpp.

◆ newConnection

void VFS_websocket_server::newConnection ( )
protectedvirtualslot

A new connection has been requested, and is pending.

Accept the new connection and create a VFS_websocket_client to host it. Connect the necessary signals and slots to manage the lifespan of the connection.

The child will be append()ed once authenticated and authorized by VFS_websocket_client::authorized().

Definition at line 172 of file VFS_websocket_server.cpp.

◆ serverError

void VFS_websocket_server::serverError ( QWebSocketProtocol::CloseCode  closeCode)
virtualslot

A server socket error has occurred.

Log it as an error, but do nothing.

Parameters
closeCodeThe websocket close state received by the underlying driver

Definition at line 425 of file VFS_websocket_server.cpp.

◆ sslErrors

void VFS_websocket_server::sslErrors ( const QList< QSslError > &  errors)
protectedvirtualslot

One or more ssl errors have occurred.

Log them as errors, and do nothing.

Parameters
errorsThe list of QSslErrors to log

Definition at line 435 of file VFS_websocket_server.cpp.

Friends And Related Function Documentation

◆ VFS_websocket_client

friend class VFS_websocket_client
friend

Definition at line 13 of file VFS_websocket_server.h.

Member Data Documentation

◆ _authPaths

QStringList VFS_websocket_server::_authPaths
protected

The list of auth paths to query when a user request authentication.

Definition at line 32 of file VFS_websocket_server.h.

◆ _listenAddress

QHostAddress VFS_websocket_server::_listenAddress
protected

Address to listen on, or 0.0.0.0 for all.

Definition at line 25 of file VFS_websocket_server.h.

◆ _port

quint16 VFS_websocket_server::_port
protected

Port to listen on.

Definition at line 26 of file VFS_websocket_server.h.

◆ _sessionsPath

QString VFS_websocket_server::_sessionsPath
protected

The path to the sessionManager node.

Definition at line 33 of file VFS_websocket_server.h.

◆ _socket

QWebSocketServer VFS_websocket_server::_socket
protected

The QWebSocket server object that will be listening.

Definition at line 30 of file VFS_websocket_server.h.

◆ _ssl

QWebSocketServer::SslMode VFS_websocket_server::_ssl
protected

Use ssl?

Definition at line 27 of file VFS_websocket_server.h.

◆ _sslCertPath

QString VFS_websocket_server::_sslCertPath
protected

ssl cert file path

Definition at line 28 of file VFS_websocket_server.h.

◆ _sslKeyPath

QString VFS_websocket_server::_sslKeyPath
protected

ssl key file path

Definition at line 29 of file VFS_websocket_server.h.


The documentation for this class was generated from the following files: