| 8 | namespace tt::network { |
| 9 | |
| 10 | class HttpServer { |
| 11 | |
| 12 | public: |
| 13 | |
| 14 | /** |
| 15 | * @brief Function for URI matching used by server. |
| 16 | * |
| 17 | * @param[in] referenceUri URI/template with respect to which the other URI is matched |
| 18 | * @param[in] uriToCheck URI/template being matched to the reference URI/template |
| 19 | * @param[in] matchUpTo For specifying the actual length of `uri_to_match` up to |
| 20 | * which the matching algorithm is to be applied (The maximum |
| 21 | * value is `strlen(uri_to_match)`, independent of the length |
| 22 | * of `reference_uri`) |
| 23 | * @return true on match |
| 24 | */ |
| 25 | typedef bool (*UriMatchFunction)(const char* referenceUri, const char* uriToCheck, size_t matchUpTo); |
| 26 | |
| 27 | private: |
| 28 | |
| 29 | const uint32_t port; |
| 30 | const std::string address; |
| 31 | const uint32_t stackSize; |
| 32 | const UriMatchFunction matchUri; |
| 33 | |
| 34 | std::vector<httpd_uri_t> handlers; |
| 35 | |
| 36 | RecursiveMutex mutex; |
| 37 | httpd_handle_t server = nullptr; |
| 38 | |
| 39 | bool startInternal(); |
| 40 | void stopInternal(); |
| 41 | |
| 42 | public: |
| 43 | |
| 44 | HttpServer( |
| 45 | uint32_t port, |
| 46 | const std::string& address, |
| 47 | std::vector<httpd_uri_t> handlers, |
| 48 | uint32_t stackSize = 5120, |
| 49 | UriMatchFunction matchUri = httpd_uri_match_wildcard |
| 50 | ) : |
| 51 | port(port), |
| 52 | address(address), |
| 53 | stackSize(stackSize), |
| 54 | matchUri(matchUri), |
| 55 | handlers(std::move(handlers)) |
| 56 | {} |
| 57 | |
| 58 | bool start(); |
| 59 | |
| 60 | void stop(); |
| 61 | |
| 62 | bool isStarted() const { |
| 63 | auto lock = mutex.asScopedLock(); |
| 64 | lock.lock(); |
| 65 | return server != nullptr; |
| 66 | } |
| 67 | }; |