mirror of
https://github.com/jambonz/freeswitch-modules.git
synced 2025-12-19 08:57:44 +00:00
mod_*_tts is not able to parse http response code if the protocol is hot HTTP/2 (#44)
* fix mod_tts wrongly parsing http error code Signed-off-by: Quan HL <quan.luuhoang8@gmail.com> * wip Signed-off-by: Quan HL <quan.luuhoang8@gmail.com> * wip Signed-off-by: Quan HL <quan.luuhoang8@gmail.com> --------- Signed-off-by: Quan HL <quan.luuhoang8@gmail.com>
This commit is contained in:
@@ -552,9 +552,28 @@ static size_t write_cb(void *ptr, size_t size, size_t nmemb, ConnInfo_t *conn) {
|
||||
return bytes_received;
|
||||
}
|
||||
|
||||
static int extract_response_code(const std::string& input) {
|
||||
std::size_t space_pos = input.find(' ');
|
||||
if (space_pos == std::string::npos) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid HTTP response format %s\n", input.c_str());
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::size_t code_start_pos = space_pos + 1;
|
||||
std::size_t code_end_pos = input.find(' ', code_start_pos);
|
||||
if (code_end_pos == std::string::npos) {
|
||||
code_end_pos = input.length();
|
||||
}
|
||||
|
||||
std::string code_str = input.substr(code_start_pos, code_end_pos - code_start_pos);
|
||||
int response_code = std::stoi(code_str);
|
||||
return response_code;
|
||||
}
|
||||
|
||||
|
||||
static size_t header_callback(char *buffer, size_t size, size_t nitems, ConnInfo_t *conn) {
|
||||
size_t bytes_received = size * nitems;
|
||||
const std::string prefix = "HTTP/2 ";
|
||||
const std::string prefix = "HTTP/ ";
|
||||
elevenlabs_t* el = conn->elevenlabs;
|
||||
std::string header, value;
|
||||
std::string input(buffer, bytes_received);
|
||||
@@ -565,11 +584,11 @@ static size_t header_callback(char *buffer, size_t size, size_t nitems, ConnInfo
|
||||
else if (0 == header.compare("history-item-id")) el->history_item_id = strdup(value.c_str());
|
||||
}
|
||||
else {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "recv header: %s\n", input.c_str());
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "header_callback: %s\n", input.c_str());
|
||||
if (input.rfind(prefix, 0) == 0) {
|
||||
try {
|
||||
el->response_code = std::stoi(input.substr(prefix.length()));
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "parsed response code: %ld\n", el->response_code);
|
||||
el->response_code = extract_response_code(input);
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "header_callback: parsed response code: %ld\n", el->response_code);
|
||||
} catch (const std::invalid_argument& e) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "header_callback: invalid response code %s\n", input.substr(prefix.length()).c_str());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user