Skip to content

Commit

Permalink
Small updates to Http response/request and Udp (#2117)
Browse files Browse the repository at this point in the history
* Made toString method for HttpRequest and HttpResponse to be available also in release mode.
Useful for building Http request/response strings.

* Added UDP multicast methods (when the used LWIP version supports it).
* Added join/leave to/from multicast groups in IpConnection class.

Co-authored-by: mikee47 <[email protected]>
  • Loading branch information
slaff and mikee47 authored Oct 19, 2020
1 parent b3893e0 commit fa5682f
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 19 deletions.
21 changes: 12 additions & 9 deletions Sming/Core/Network/Http/HttpRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,23 @@ void HttpRequest::reset()
files.clear();
}

#ifndef SMING_RELEASE
String HttpRequest::toString()
String HttpRequest::toString(const HttpRequest& req)
{
String content;
content += String(http_method_str(method)) + ' ' + uri.getPathWithQuery() + _F(" HTTP/1.1\n");
content += headers.toString(HTTP_HEADER_HOST, uri.getHostWithPort());
for(unsigned i = 0; i < headers.count(); i++) {
content += headers[i];
content += http_method_str(req.method);
content += ' ';
content += req.uri.getPathWithQuery();
content += _F(" HTTP/1.1\r\n");
content += req.headers.toString(HTTP_HEADER_HOST, uri.getHostWithPort());
for(unsigned i = 0; i < req.headers.count(); i++) {
content += req.headers[i];
}

if(bodyStream != nullptr && bodyStream->available() >= 0) {
content += headers.toString(HTTP_HEADER_CONTENT_LENGTH, String(bodyStream->available()));
if(req.bodyStream != nullptr && req.bodyStream->available() >= 0) {
content += req.headers.toString(HTTP_HEADER_CONTENT_LENGTH, String(req.bodyStream->available()));
} else {
content += "\r\n";
}

return content;
}
#endif
15 changes: 12 additions & 3 deletions Sming/Core/Network/Http/HttpRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,22 @@ class HttpRequest
return this;
}

#ifndef SMING_RELEASE
/**
* @brief Tries to present a readable version of the current request values
* @retval String
*/
String toString();
#endif
String toString()
{
return toString(*this);
}

/**
* @brief Tries to present a readable version of the request
* @param req
*
* @retval String
*/
String toString(const HttpRequest& req);

public:
Url uri;
Expand Down
21 changes: 21 additions & 0 deletions Sming/Core/Network/Http/HttpResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,27 @@ void HttpResponse::reset()
freeStreams();
}

String HttpResponse::toString(const HttpResponse& res)
{
String content;
content += F("HTTP/1.1 ");
content += res.code;
content += ' ';
content += httpGetStatusText(res.code);
content += " \r\n";
for(unsigned i = 0; i < res.headers.count(); i++) {
content += res.headers[i];
}

if(res.stream != nullptr && res.stream->available() >= 0) {
content += res.headers.toString(HTTP_HEADER_CONTENT_LENGTH, String(res.stream->available()));
} else {
content += "\r\n";
}

return content;
}

void HttpResponse::freeStreams()
{
// Consistency check
Expand Down
17 changes: 17 additions & 0 deletions Sming/Core/Network/Http/HttpResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,23 @@ class HttpResponse
return (code >= HTTP_STATUS_OK && code <= 399);
}

/**
* @brief Tries to present a readable version of the current response values
* @retval String
*/
String toString()
{
return toString(*this);
}

/**
* @brief Tries to present a readable version of the response
* @param res
*
* @retval String
*/
String toString(const HttpResponse& res);

private:
void setStream(IDataSourceStream* stream);

Expand Down
22 changes: 22 additions & 0 deletions Sming/Core/Network/IpConnection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/****
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
* Created 2015 by Skurydin Alexey
* http://github.com/SmingHub/Sming
* All files of the Sming Core are provided under the LGPL v3 license.
*
* IpConnection.cpp
*
****/

#include "IpConnection.h"
#include <lwip/igmp.h>

bool IpConnection::joinMulticastGroup(IpAddress localIp, IpAddress multicastIp)
{
return (igmp_joingroup(localIp, multicastIp) == ERR_OK);
}

bool IpConnection::leaveMulticastGroup(IpAddress localIp, IpAddress multicastIp)
{
return (igmp_leavegroup(localIp, multicastIp) == ERR_OK);
}
65 changes: 65 additions & 0 deletions Sming/Core/Network/IpConnection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/****
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
* Created 2015 by Skurydin Alexey
* http://github.com/SmingHub/Sming
* All files of the Sming Core are provided under the LGPL v3 license.
*
* IpConnection.h
*
****/

#pragma once

#include <IpAddress.h>

/** @defgroup ip IP
* @brief Provides common IP functions
* @ingroup networking
* @{
*/

class IpConnection
{
public:
/**
* @brief Uses IGMP to add a local network interface to multicast group
* @param localIp Address identifying network interface
* @param multicastIp The multicast group address
*
* @retval true on success
*/
bool joinMulticastGroup(IpAddress localIp, IpAddress multicastIp);

/**
* @brief Uses IGMP to add all local network interfaces to multicast group
* @param multicastIp The multicast group address
*
* @retval true on success
*/
bool joinMulticastGroup(IpAddress multicastIp)
{
return joinMulticastGroup(INADDR_NONE, multicastIp);
}

/**
* @brief Uses IGMP to remove a local network interface from multicast group
* @param localIp Address identifying network interface
* @param multicastIp The multicast group address
*
* @retval true on success
*/
bool leaveMulticastGroup(IpAddress localIp, IpAddress multicastIp);

/**
* @brief Uses IGMP to remove all local network interfaces from multicast group
* @param multicastIp The multicast group address
*
* @retval true on success
*/
bool leaveMulticastGroup(IpAddress multicastIp)
{
return leaveMulticastGroup(INADDR_NONE, multicastIp);
}
};

/** @} */
4 changes: 2 additions & 2 deletions Sming/Core/Network/TcpConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

#pragma once

#include <Network/IpConnection.h>
#include <Network/Ssl/Session.h>
#include <IpAddress.h>

#define NETWORK_DEBUG

Expand All @@ -36,7 +36,7 @@ class TcpConnection;

typedef Delegate<void(TcpConnection&)> TcpConnectionDestroyedDelegate;

class TcpConnection
class TcpConnection : public IpConnection
{
public:
TcpConnection(bool autoDestruct) : autoSelfDestruct(autoDestruct)
Expand Down
20 changes: 20 additions & 0 deletions Sming/Core/Network/UdpConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,23 @@ void UdpConnection::staticOnReceive(void* arg, struct udp_pcb* pcb, struct pbuf*
}
pbuf_free(p);
}

bool UdpConnection::setMulticast(IpAddress ip)
{
#if LWIP_MULTICAST_TX_OPTIONS
udp_set_multicast_netif_addr(udp, (ip4_addr_t*)ip);
return true;
#else
return false;
#endif
}

bool UdpConnection::setMulticastTtl(size_t ttl)
{
#if LWIP_MULTICAST_TX_OPTIONS
udp_set_multicast_ttl(udp, ttl);
return true;
#else
return false;
#endif
}
31 changes: 26 additions & 5 deletions Sming/Core/Network/UdpConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
*
****/

#pragma once

#include <Network/IpConnection.h>

/** @defgroup udp UDP
* @brief Provides base for UDP clients or services
* @ingroup networking
* @{
*/

#pragma once

#include <IpAddress.h>

class UdpConnection;

typedef Delegate<void(UdpConnection& connection, char* data, int size, IpAddress remoteIP, uint16_t remotePort)>
UdpConnectionDataDelegate;

class UdpConnection
class UdpConnection : public IpConnection
{
public:
UdpConnection()
Expand Down Expand Up @@ -70,6 +70,27 @@ class UdpConnection
return sendTo(remoteIP, remotePort, data.c_str(), data.length());
}

/**
* @brief Sets the UDP multicast IP.
* @param ip
*
* @retval true when LWIP supports this operation, false otherwise
*
* @note This method works only when LWIP is compiled with LWIP_MULTICAST_TX_OPTIONS
*/
bool setMulticast(IpAddress ip);

/**
* @brief Sets the UDP multicast Time-To-Live(TTL).
* @param ttl - time to live in hops.
* For example if a milticast UDP packet needs to pass through two routes to reach the receiver then the TTL should be set to 2
*
* @retval true when LWIP supports this operation, false otherwise
*
* @note This method works only when LWIP is compiled with LWIP_MULTICAST_TX_OPTIONS
*/
bool setMulticastTtl(size_t ttl);

protected:
virtual void onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort);

Expand Down

0 comments on commit fa5682f

Please sign in to comment.