generated from obsproject/obs-plugintemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix major mem leak in HTML parsing (#126)
* Refactor README.md to update download links and badges for different platforms * Refactor HTML and JSON parsers
- Loading branch information
Showing
6 changed files
with
208 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,42 @@ | ||
|
||
#include "request-data.h" | ||
#include "plugin-support.h" | ||
#include "errors.h" | ||
|
||
#include <regex> | ||
#include <obs-module.h> | ||
|
||
struct request_data_handler_response parse_regex(struct request_data_handler_response response, | ||
const url_source_request_data *request_data) | ||
{ | ||
std::string parsed_output = ""; | ||
if (request_data->output_regex == "") { | ||
// Return the whole response body | ||
parsed_output = response.body; | ||
} else { | ||
// Parse the response as a regex | ||
std::regex regex(request_data->output_regex, | ||
std::regex_constants::ECMAScript | std::regex_constants::optimize); | ||
try { | ||
if (request_data->output_regex.empty()) { | ||
response.body_parts_parsed.push_back(response.body); | ||
return response; | ||
} | ||
|
||
// Cache compiled regex patterns for better performance | ||
static thread_local std::unordered_map<std::string, std::regex> regex_cache; | ||
|
||
auto ®ex = regex_cache[request_data->output_regex]; | ||
if (regex_cache.find(request_data->output_regex) == regex_cache.end()) { | ||
regex = std::regex(request_data->output_regex, | ||
std::regex_constants::ECMAScript | | ||
std::regex_constants::optimize); | ||
} | ||
|
||
std::smatch match; | ||
if (std::regex_search(response.body, match, regex)) { | ||
if (match.size() > 1) { | ||
parsed_output = match[1].str(); | ||
} else { | ||
parsed_output = match[0].str(); | ||
} | ||
} else { | ||
obs_log(LOG_INFO, "Failed to match regex"); | ||
// Return an error response | ||
struct request_data_handler_response responseFail; | ||
responseFail.error_message = "Failed to match regex"; | ||
responseFail.status_code = URL_SOURCE_REQUEST_PARSING_ERROR_CODE; | ||
return responseFail; | ||
// Get the appropriate capture group | ||
size_t group = match.size() > 1 ? 1 : 0; | ||
response.body_parts_parsed.push_back(match[group].str()); | ||
return response; | ||
} | ||
|
||
return make_fail_parse_response("No regex match found"); | ||
|
||
} catch (const std::regex_error &e) { | ||
return make_fail_parse_response(std::string("Regex error: ") + e.what()); | ||
} catch (const std::exception &e) { | ||
return make_fail_parse_response(std::string("Parse error: ") + e.what()); | ||
} | ||
response.body_parts_parsed.push_back(parsed_output); | ||
return response; | ||
} |
Oops, something went wrong.