| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ | |||
| 2 | 2 | #define SRC_CONNECTIONCONTEXT_HPP_ | |
| 3 | 3 | ||
| 4 | 4 | #include <Arduino.h> | |
| 5 | + #include <IPAddress.h> | ||
| 5 | 6 | ||
| 6 | 7 | // Required for SSL | |
| 7 | 8 | #include "openssl/ssl.h" | |
@@ -30,6 +31,7 @@ class ConnectionContext { | |||
| 30 | 31 | ||
| 31 | 32 | virtual bool isSecure() = 0; | |
| 32 | 33 | virtual void setWebsocketHandler(WebsocketHandler *wsHandler); | |
| 34 | + virtual IPAddress getClientIP() = 0; | ||
| 33 | 35 | ||
| 34 | 36 | WebsocketHandler * _wsHandler; | |
| 35 | 37 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -33,6 +33,7 @@ HTTPConnection::~HTTPConnection() { | |||
| 33 | 33 | int HTTPConnection::initialize(int serverSocketID, HTTPHeaders *defaultHeaders) { | |
| 34 | 34 | if (_connectionState == STATE_UNDEFINED) { | |
| 35 | 35 | _defaultHeaders = defaultHeaders; | |
| 36 | + _addrLen = sizeof(_sockAddr); | ||
| 36 | 37 | _socket = accept(serverSocketID, (struct sockaddr * )&_sockAddr, &_addrLen); | |
| 37 | 38 | ||
| 38 | 39 | // Build up SSL Connection context if the socket has been created successfully | |
@@ -42,11 +43,11 @@ int HTTPConnection::initialize(int serverSocketID, HTTPHeaders *defaultHeaders) | |||
| 42 | 43 | _httpHeaders = new HTTPHeaders(); | |
| 43 | 44 | refreshTimeout(); | |
| 44 | 45 | return _socket; | |
| 45 | - | ||
| 46 | 46 | } | |
| 47 | 47 | ||
| 48 | 48 | HTTPS_LOGE("Could not accept() new connection"); | |
| 49 | 49 | ||
| 50 | + _addrLen = 0; | ||
| 50 | 51 | _connectionState = STATE_ERROR; | |
| 51 | 52 | _clientState = CSTATE_ACTIVE; | |
| 52 | 53 | ||
@@ -58,6 +59,16 @@ int HTTPConnection::initialize(int serverSocketID, HTTPHeaders *defaultHeaders) | |||
| 58 | 59 | return -1; | |
| 59 | 60 | } | |
| 60 | 61 | ||
| 62 | + /** | ||
| 63 | + * Returns the client's IPv4 | ||
| 64 | + */ | ||
| 65 | + IPAddress HTTPConnection::getClientIP() { | ||
| 66 | + if (_addrLen > 0 && _sockAddr.sa_family == AF_INET) { | ||
| 67 | + struct sockaddr_in *sockAddrIn = (struct sockaddr_in *)(&_sockAddr); | ||
| 68 | + return IPAddress(sockAddrIn->sin_addr.s_addr); | ||
| 69 | + } | ||
| 70 | + return IPAddress(0, 0, 0, 0); | ||
| 71 | + } | ||
| 61 | 72 | ||
| 62 | 73 | /** | |
| 63 | 74 | * True if the connection is timed out. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ | |||
| 2 | 2 | #define SRC_HTTPCONNECTION_HPP_ | |
| 3 | 3 | ||
| 4 | 4 | #include <Arduino.h> | |
| 5 | + #include <IPAddress.h> | ||
| 5 | 6 | ||
| 6 | 7 | #include <string> | |
| 7 | 8 | #include <mbedtls/base64.h> | |
@@ -42,6 +43,7 @@ class HTTPConnection : private ConnectionContext { | |||
| 42 | 43 | virtual int initialize(int serverSocketID, HTTPHeaders *defaultHeaders); | |
| 43 | 44 | virtual void closeConnection(); | |
| 44 | 45 | virtual bool isSecure(); | |
| 46 | + virtual IPAddress getClientIP(); | ||
| 45 | 47 | ||
| 46 | 48 | void loop(); | |
| 47 | 49 | bool isClosed(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -53,6 +53,10 @@ HTTPNode * HTTPRequest::getResolvedNode() { | |||
| 53 | 53 | return _resolvedNode; | |
| 54 | 54 | } | |
| 55 | 55 | ||
| 56 | + IPAddress HTTPRequest::getClientIP() { | ||
| 57 | + return _con->getClientIP(); | ||
| 58 | + } | ||
| 59 | + | ||
| 56 | 60 | size_t HTTPRequest::readBytes(byte * buffer, size_t length) { | |
| 57 | 61 | ||
| 58 | 62 | // Limit reading to content length | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ | |||
| 2 | 2 | #define SRC_HTTPREQUEST_HPP_ | |
| 3 | 3 | ||
| 4 | 4 | #include <Arduino.h> | |
| 5 | + #include <IPAddress.h> | ||
| 5 | 6 | #include <string> | |
| 6 | 7 | ||
| 7 | 8 | #include <mbedtls/base64.h> | |
@@ -29,6 +30,7 @@ class HTTPRequest { | |||
| 29 | 30 | std::string getRequestString(); | |
| 30 | 31 | std::string getMethod(); | |
| 31 | 32 | std::string getTag(); | |
| 33 | + IPAddress getClientIP(); | ||
| 32 | 34 | ||
| 33 | 35 | size_t readChars(char * buffer, size_t length); | |
| 34 | 36 | size_t readBytes(byte * buffer, size_t length); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments