Sonde v1.1.0
Embeddable Model Context Protocol server for Qt applications
Loading...
Searching...
No Matches
Sonde::HttpTransport Class Reference

MCP Streamable HTTP transport on a QTcpServer (httptransport.cpp). More...

#include <server_p.h>

Inheritance diagram for Sonde::HttpTransport:

Classes

struct  Connection
 Per-connection parser state. More...

Public Member Functions

 HttpTransport (ServerPrivate *server)
 Creates the transport for server (not listening yet).
 ~HttpTransport () override
 Closes all connections; sessions lose their streams.
bool listen (const QHostAddress &address, quint16 port, const QString &path)
 Starts listening.
quint16 port () const
 The bound port.
QHostAddress address () const
 The bound address.
QString path () const
 The endpoint path.
void setAuthToken (const QString &token)
 Sets the required bearer token (empty disables auth).
void sessionEnded (const QString &id)
 Closes the SSE stream of an ended session.

Private Types

using Request = Http::Request
 A parsed HTTP request.

Private Member Functions

void onNewConnection ()
 Accepts pending connections.
void onReadyRead (QTcpSocket *socket)
 Parses buffered requests on socket and dispatches complete ones.
void handleRequest (QTcpSocket *socket, const Request &request)
 Applies origin/auth/path checks and routes by method.
void handlePost (QTcpSocket *socket, const Request &request)
 Handles POST: JSON-RPC messages, answered when all requests complete.
std::optional< QList< QJsonObject > > parseMessages (QTcpSocket *socket, const Request &request, bool *batch)
 The messages in a POST body (batch set if it was an array), or std::nullopt after writing an error.
SessionPtr sessionFor (QTcpSocket *socket, const Request &request, const QList< QJsonObject > &messages, bool *isInitialize)
 The session a POST belongs to (a new one for initialize), or nullptr after writing an error.
void dispatchPost (QTcpSocket *socket, const Request &request, const SessionPtr &session, const QList< QJsonObject > &messages, bool batch, bool isInitialize)
 Dispatches the messages of a POST and writes the response once every request is answered.
void finishPost (QTcpSocket *socket, const Request &request, const QByteArray &body, const QList< QPair< QByteArray, QByteArray > > &headers)
 Writes the response to a POST whose requests are all answered, then resumes reading.
void handleGet (QTcpSocket *socket, const Request &request)
 Handles GET: opens the session's server-sent-events stream.
void writeResponse (QTcpSocket *socket, int status, const QByteArray &body, const QByteArray &contentType, const QList< QPair< QByteArray, QByteArray > > &headers, const Request &request)
 Writes a complete HTTP response (with CORS headers for allowed origins).
void writeJsonRpcError (QTcpSocket *socket, int status, int code, const QString &message, const Request &request)
 Writes a JSON-RPC error object as the response body.
bool originAllowed (const QByteArray &origin) const
 Whether a browser Origin is acceptable (localhost only, against DNS rebinding).
void expireSessions ()
 Ends HTTP sessions that have been idle too long.
void expireConnections ()
 Closes connections whose request has taken too long, or that sat idle too long.

Private Attributes

ServerPrivate * d
 Server implementation.
QTcpServer * m_server = nullptr
 Listening socket.
QString m_path
 Endpoint path.
QString m_token
 Required bearer token.
QHash< QTcpSocket *, std::shared_ptr< Connection > > m_connections
 Per-connection parser state.
QHash< QString, QPointer< QTcpSocket > > m_streams
 SSE socket per session id.
QTimer m_pingTimer
 Keeps SSE streams alive.
QTimer m_expiryTimer
 Runs expireSessions().
QTimer m_connectionTimer
 Runs expireConnections().

Detailed Description

MCP Streamable HTTP transport on a QTcpServer (httptransport.cpp).

Member Function Documentation

◆ dispatchPost()

void Sonde::HttpTransport::dispatchPost ( QTcpSocket * socket,
const Request & request,
const SessionPtr & session,
const QList< QJsonObject > & messages,
bool batch,
bool isInitialize )
private

Dispatches the messages of a POST and writes the response once every request is answered.

< Responses by message index (empty for notifications).

< Requests not yet answered.

◆ listen()

bool Sonde::HttpTransport::listen ( const QHostAddress & address,
quint16 port,
const QString & path )

Starts listening.

Returns
Whether it succeeded.

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