Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
HttpServerConnection Class Reference
Inheritance diagram for HttpServerConnection:
HttpConnection TcpClient TcpConnection

Public Member Functions

 HttpServerConnection (tcp_pcb *clientTcp)
 
void setResourceTree (HttpResourceTree *resourceTree)
 
void setBodyParsers (const BodyParsers *bodyParsers)
 
void send ()
 
void setUpgradeCallback (HttpServerProtocolUpgradeCallback callback)
 
HttpRequestgetRequest () override
 Returns pointer to the current request. More...
 
virtual void reset ()
 
virtual void cleanup ()
 
virtual void setDefaultParser ()
 
virtual bool send (HttpRequest *request)
 
bool send (const char *data, uint16_t len, bool forceCloseAfterSent=false)
 
bool isActive ()
 
HttpResponsegetResponse ()
 Returns pointer to the current response. More...
 
int getResponseCode () const SMING_DEPRECATED
 
String getResponseHeader (const String &headerName, const String &defaultValue=nullptr) const SMING_DEPRECATED
 
HttpHeadersgetResponseHeaders () SMING_DEPRECATED
 
DateTime getLastModifiedDate () const SMING_DEPRECATED
 
DateTime getServerDate () const SMING_DEPRECATED
 
String getResponseString () SMING_DEPRECATED
 
bool connect (const String &server, int port, bool useSsl=false, uint32_t sslOptions=0) override
 
bool connect (IPAddress addr, uint16_t port, bool useSsl=false, uint32_t sslOptions=0) override
 
void close () override
 
void setReceiveDelegate (TcpClientDataDelegate receiveCb=nullptr)
 Set or clear the callback for received data. More...
 
void setCompleteDelegate (TcpClientCompleteDelegate completeCb=nullptr)
 Set or clear the callback for connection close. More...
 
bool sendString (const String &data, bool forceCloseAfterSent=false)
 
bool isProcessing ()
 
TcpClientState getConnectionState ()
 
bool addSslValidator (SslValidatorCallback callback, void *data=nullptr)
 Allows setting of multiple SSL validators after a successful handshake. More...
 
bool pinCertificate (const uint8_t *fingerprint, SslFingerprintType type)
 Requires (pins) the remote SSL certificate to match certain fingerprints. More...
 
bool pinCertificate (SslFingerprints &fingerprints)
 Requires (pins) the remote SSL certificate to match certain fingerprints. More...
 
int writeString (const char *data, uint8_t apiflags=TCP_WRITE_FLAG_COPY)
 
int writeString (const String &data, uint8_t apiflags=TCP_WRITE_FLAG_COPY)
 
virtual int write (const char *data, int len, uint8_t apiflags=TCP_WRITE_FLAG_COPY)
 Base write operation. More...
 
int write (IDataSourceStream *stream)
 
uint16_t getAvailableWriteSize ()
 
void flush ()
 
void setTimeOut (uint16_t waitTimeOut)
 
IPAddress getRemoteIp () const
 
uint16_t getRemotePort () const
 
void setDestroyedDelegate (TcpConnectionDestroyedDelegate destroyedDelegate)
 Sets a callback to be called when the object instance is destroyed. More...
 
void addSslOptions (uint32_t sslOptions)
 
bool setSslClientKeyCert (const uint8_t *key, int keyLength, const uint8_t *certificate, int certificateLength, const char *keyPassword=nullptr, bool freeAfterHandshake=false) SMING_DEPRECATED
 Sets client private key, certificate and password from memory. More...
 
bool setSslClientKeyCert (const SslKeyCertPair &clientKeyCert, bool freeAfterHandshake=false) SMING_DEPRECATED
 Sets client private key, certificate and password from memory. More...
 
void freeSslClientKeyCert () SMING_DEPRECATED
 Frees the memory used for the key and certificate pair. More...
 
bool setSslKeyCert (const uint8_t *key, int keyLength, const uint8_t *certificate, int certificateLength, const char *keyPassword=nullptr, bool freeAfterHandshake=false)
 Sets private key, certificate and password from memory for the SSL connection If this methods is called from a client then it sets the client key and certificate If it is called from a server then it sets the server certificate and key. Server and Client certificates differ. Client certificate is used for identification. Server certificate is used for encrypt/decrypt the data. Make sure to use the correct certificate for the desired goal. More...
 
bool setSslKeyCert (const SslKeyCertPair &keyCert, bool freeAfterHandshake=false)
 Sets private key, certificate and password from memory for the SSL connection If this methods is called from a client then it sets the client key and certificate If it is called from a server then it sets the server certificate and key. Server and Client certificates differ. Client certificate is used for identification. Server certificate is used for encrypt/decrypt the data. Make sure to use the correct certificate for the desired goal. More...
 
void freeSslKeyCert ()
 Frees the memory used for the key and certificate pair. More...
 
void setSsl (SSL *ssl)
 
SSL * getSsl ()
 

Public Attributes

void * userData = nullptr
 use to pass user data between requests More...
 

Protected Member Functions

int onMessageBegin (http_parser *parser) override
 Called when a new incoming data is beginning to come. More...
 
int onPath (const Url &path) override
 Called when the URL path is known. More...
 
int onHeadersComplete (const HttpHeaders &headers) override
 Called when all headers are received. More...
 
int onBody (const char *at, size_t length) override
 Called when a piece of body data is received. More...
 
int onMessageComplete (http_parser *parser) override
 Called when the incoming data is complete. More...
 
bool onProtocolUpgrade (http_parser *parser) override
 Called when the HTTP protocol should be upgraded. More...
 
void onHttpError (http_errno error) override
 
void onReadyToSendData (TcpConnectionEvent sourceEvent) override
 
virtual void sendError (const String &message=nullptr, enum http_status code=HTTP_STATUS_BAD_REQUEST)
 
void resetHeaders ()
 Called after all headers have been received and processed. More...
 
virtual void init (http_parser_type type)
 Initializes the http parser for a specific type of HTTP message. More...
 
virtual int onStatus (http_parser *parser)
 
virtual int onChunkHeader (http_parser *parser)
 
virtual int onChunkComplete (http_parser *parser)
 
virtual bool onTcpReceive (TcpClient &client, char *data, int size)
 
void onError (err_t err) override
 
err_t onConnected (err_t err) override
 
err_t onReceive (pbuf *buf) override
 
err_t onSent (uint16_t len) override
 
virtual void onFinished (TcpClientState finishState)
 
err_t onSslConnected (SSL *ssl) override
 
void pushAsyncPart ()
 
void freeStreams ()
 
void setBuffer (ReadWriteStream *stream)
 
void initialize (tcp_pcb *pcb)
 
bool internalConnect (IPAddress addr, uint16_t port)
 
virtual err_t onPoll ()
 
err_t internalOnConnected (err_t err)
 
err_t internalOnReceive (pbuf *p, err_t err)
 
err_t internalOnSent (uint16_t len)
 
err_t internalOnPoll ()
 
void internalOnError (err_t err)
 
void internalOnDnsResponse (const char *name, LWIP_IP_ADDR_T *ipaddr, int port)
 

Protected Attributes

http_parser parser
 
HttpHeaderBuilder header
 Header construction. More...
 
HttpHeaders incomingHeaders
 Full set of incoming headers. More...
 
HttpConnectionState state = eHCS_Ready
 
HttpResponse response
 
ReadWriteStreambuffer = nullptr
 Used internally to buffer arbitrary data via send() methods. More...
 
IDataSourceStreamstream = nullptr
 The currently active stream being sent. More...
 
tcp_pcb * tcp = nullptr
 
uint16_t sleep = 0
 
uint16_t timeOut = USHRT_MAX
 By default a TCP connection does not have a time out. More...
 
bool canSend = true
 
bool autoSelfDestruct = true
 
SSL * ssl = nullptr
 
SSLCTX * sslContext = nullptr
 
SSL_EXTENSIONS * sslExtension = nullptr
 
bool sslConnected = false
 
uint32_t sslOptions = 0
 
SslKeyCertPair sslKeyCert
 
bool freeKeyCertAfterHandshake = false
 
SslSessionIdsslSessionId = nullptr
 
bool useSsl = false
 

Static Protected Attributes

static const http_parser_settings parserSettings
 Callback table for parser. More...
 

Member Function Documentation

bool TcpClient::addSslValidator ( SslValidatorCallback  callback,
void *  data = nullptr 
)
inlineinherited

Allows setting of multiple SSL validators after a successful handshake.

Parameters
callbackThe callback function to be invoked on validation
dataThe data to pass to the callback
Note
The callback is responsible for releasing the data if appropriate. See SslValidatorCallback for further details.
Return values
booltrue on success, false on failure
void TcpConnection::freeSslClientKeyCert ( )
inlineinherited

Frees the memory used for the key and certificate pair.

Deprecated:
Use freeSslKeyCert() instead
void TcpConnection::freeSslKeyCert ( )
inlineinherited

Frees the memory used for the key and certificate pair.

DateTime HttpConnection::getLastModifiedDate ( ) const
inlineinherited
Deprecated:
Use getResponse()->headers.getLastModifiedDate() instead
HttpRequest* HttpServerConnection::getRequest ( )
inlineoverridevirtual

Returns pointer to the current request.

Return values
HttpRequest*

Implements HttpConnection.

HttpResponse* HttpConnection::getResponse ( )
inlineinherited

Returns pointer to the current response.

Return values
HttpResponse*
int HttpConnection::getResponseCode ( ) const
inlineinherited
Deprecated:
Use getResponse()->code instead
String HttpConnection::getResponseHeader ( const String headerName,
const String defaultValue = nullptr 
) const
inlineinherited
Deprecated:
Use getResponse()->headers[] instead
HttpHeaders& HttpConnection::getResponseHeaders ( )
inlineinherited
Deprecated:
Use getResponse()->headers instead
String HttpConnection::getResponseString ( )
inlineinherited
Deprecated:
Use getResponse()->getBody() instead
DateTime HttpConnection::getServerDate ( ) const
inlineinherited
Deprecated:
Use getResponse()->headers.getServerDate() instead
virtual void HttpConnection::init ( http_parser_type  type)
protectedvirtualinherited

Initializes the http parser for a specific type of HTTP message.

Parameters
type
int HttpServerConnection::onBody ( const char *  at,
size_t  length 
)
overrideprotectedvirtual

Called when a piece of body data is received.

Parameters
atthe data
length
Return values
int0 on success, non-0 on error

Implements HttpConnection.

int HttpServerConnection::onHeadersComplete ( const HttpHeaders headers)
overrideprotectedvirtual

Called when all headers are received.

Parameters
headersThe processed headers
Return values
int0 on success, non-0 on error

Implements HttpConnection.

int HttpServerConnection::onMessageBegin ( http_parser *  parser)
overrideprotectedvirtual

Called when a new incoming data is beginning to come.

Parameters
parser
Return values
int0 on success, non-0 on error

Implements HttpConnection.

int HttpServerConnection::onMessageComplete ( http_parser *  parser)
overrideprotectedvirtual

Called when the incoming data is complete.

Parameters
parser
Return values
int0 on success, non-0 on error

Implements HttpConnection.

int HttpServerConnection::onPath ( const Url uri)
overrideprotectedvirtual

Called when the URL path is known.

Parameters
uri
Return values
int0 on success, non-0 on error

Reimplemented from HttpConnection.

bool HttpServerConnection::onProtocolUpgrade ( http_parser *  parser)
inlineoverrideprotectedvirtual

Called when the HTTP protocol should be upgraded.

Parameters
parser
Return values
booltrue on success

Reimplemented from HttpConnection.

bool TcpClient::pinCertificate ( const uint8_t *  fingerprint,
SslFingerprintType  type 
)
inlineinherited

Requires (pins) the remote SSL certificate to match certain fingerprints.

Parameters
fingerprintThe fingerprint data against which the match should be performed. Must be allocated on the heap and will be deleted after use. Do not re-use outside of this method.
typeThe fingerprint type - see SslFingerprintType for details.
Return values
booltrue on success, false on failure
bool TcpClient::pinCertificate ( SslFingerprints fingerprints)
inlineinherited

Requires (pins) the remote SSL certificate to match certain fingerprints.

Note
The data inside the fingerprints parameter is passed by reference
Parameters
fingerprints- passes the certificate fingerprints by reference.
Return values
booltrue on success, false on failure
void HttpConnection::resetHeaders ( )
protectedinherited

Called after all headers have been received and processed.

void TcpClient::setCompleteDelegate ( TcpClientCompleteDelegate  completeCb = nullptr)
inlineinherited

Set or clear the callback for connection close.

Parameters
completeCbcallback delegate or nullptr
void TcpConnection::setDestroyedDelegate ( TcpConnectionDestroyedDelegate  destroyedDelegate)
inlineinherited

Sets a callback to be called when the object instance is destroyed.

Parameters
TcpServerConnectionDestroyedDelegatedestroyedDelegate - callback
void TcpClient::setReceiveDelegate ( TcpClientDataDelegate  receiveCb = nullptr)
inlineinherited

Set or clear the callback for received data.

Parameters
receiveCbcallback delegate or nullptr
bool TcpConnection::setSslClientKeyCert ( const uint8_t *  key,
int  keyLength,
const uint8_t *  certificate,
int  certificateLength,
const char *  keyPassword = nullptr,
bool  freeAfterHandshake = false 
)
inlineinherited

Sets client private key, certificate and password from memory.

Deprecated:
Use setSslKeyCert(const uint8_t*, int, const uint8_t*, int, const char*, bool) instead
Note
This method makes copy of the data.
Parameters
constuint8_t *keyData
intkeyLength
constuint8_t *certificateData
intcertificateLength
constchar *keyPassword
boolfreeAfterHandshake
Returns
bool true of success, false or failure
bool TcpConnection::setSslClientKeyCert ( const SslKeyCertPair clientKeyCert,
bool  freeAfterHandshake = false 
)
inlineinherited

Sets client private key, certificate and password from memory.

Deprecated:
Use setSslKeyCert(const SslKeyCertPair&, bool) instead
Note
This method passes the certificate key chain by reference
Parameters
SSLKeyCertPair
boolfreeAfterHandshake
Returns
bool true of success, false or failure
bool TcpConnection::setSslKeyCert ( const uint8_t *  key,
int  keyLength,
const uint8_t *  certificate,
int  certificateLength,
const char *  keyPassword = nullptr,
bool  freeAfterHandshake = false 
)
inlineinherited

Sets private key, certificate and password from memory for the SSL connection If this methods is called from a client then it sets the client key and certificate If it is called from a server then it sets the server certificate and key. Server and Client certificates differ. Client certificate is used for identification. Server certificate is used for encrypt/decrypt the data. Make sure to use the correct certificate for the desired goal.

Note
This method makes copy of the data.
Parameters
constuint8_t *keyData
intkeyLength
constuint8_t *certificateData
intcertificateLength
constchar *keyPassword
boolfreeAfterHandshake
Returns
bool true of success, false or failure
bool TcpConnection::setSslKeyCert ( const SslKeyCertPair keyCert,
bool  freeAfterHandshake = false 
)
inlineinherited

Sets private key, certificate and password from memory for the SSL connection If this methods is called from a client then it sets the client key and certificate If it is called from a server then it sets the server certificate and key. Server and Client certificates differ. Client certificate is used for identification. Server certificate is used for encrypt/decrypt the data. Make sure to use the correct certificate for the desired goal.

Note
This method passes the certificate key chain by reference
Parameters
SSLKeyCertPair
boolfreeAfterHandshake
Returns
bool true of success, false or failure
virtual int TcpConnection::write ( const char *  data,
int  len,
uint8_t  apiflags = TCP_WRITE_FLAG_COPY 
)
virtualinherited

Base write operation.

Parameters
data
len
apiflagsTCP_WRITE_FLAG_COPY, TCP_WRITE_FLAG_MORE
Return values
int-1 on error

Reimplemented in FtpDataStream.

Member Data Documentation

ReadWriteStream* TcpClient::buffer = nullptr
protectedinherited

Used internally to buffer arbitrary data via send() methods.

HttpHeaderBuilder HttpConnection::header
protectedinherited

Header construction.

HttpHeaders HttpConnection::incomingHeaders
protectedinherited

Full set of incoming headers.

const http_parser_settings HttpConnection::parserSettings
staticprotectedinherited

Callback table for parser.

IDataSourceStream* TcpClient::stream = nullptr
protectedinherited

The currently active stream being sent.

uint16_t TcpConnection::timeOut = USHRT_MAX
protectedinherited

By default a TCP connection does not have a time out.

void* HttpServerConnection::userData = nullptr

use to pass user data between requests