mirror of
https://github.com/jambonz/freeswitch-modules.git
synced 2026-01-25 02:08:27 +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:
@@ -522,9 +522,27 @@ static bool parseHeader(const std::string& str, std::string& header, std::string
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
static size_t header_callback(char *buffer, size_t size, size_t nitems, ConnInfo_t *conn) {
|
||||||
size_t bytes_received = size * nitems;
|
size_t bytes_received = size * nitems;
|
||||||
const std::string prefix = "HTTP/2 ";
|
const std::string prefix = "HTTP/";
|
||||||
deepgram_t* d = conn->deepgram;
|
deepgram_t* d = conn->deepgram;
|
||||||
std::string header, value;
|
std::string header, value;
|
||||||
std::string input(buffer, bytes_received);
|
std::string input(buffer, bytes_received);
|
||||||
@@ -533,11 +551,11 @@ static size_t header_callback(char *buffer, size_t size, size_t nitems, ConnInfo
|
|||||||
if (0 == header.compare("dg-request-id")) d->request_id = strdup(value.c_str());
|
if (0 == header.compare("dg-request-id")) d->request_id = strdup(value.c_str());
|
||||||
}
|
}
|
||||||
else {
|
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) {
|
if (input.rfind(prefix, 0) == 0) {
|
||||||
try {
|
try {
|
||||||
d->response_code = std::stoi(input.substr(prefix.length()));
|
d->response_code = extract_response_code(input);
|
||||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "parsed response code: %ld\n", d->response_code);
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "header_callback: parsed response code: %ld\n", d->response_code);
|
||||||
} catch (const std::invalid_argument& e) {
|
} 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());
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "header_callback: invalid response code %s\n", input.substr(prefix.length()).c_str());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -552,9 +552,28 @@ static size_t write_cb(void *ptr, size_t size, size_t nmemb, ConnInfo_t *conn) {
|
|||||||
return bytes_received;
|
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) {
|
static size_t header_callback(char *buffer, size_t size, size_t nitems, ConnInfo_t *conn) {
|
||||||
size_t bytes_received = size * nitems;
|
size_t bytes_received = size * nitems;
|
||||||
const std::string prefix = "HTTP/2 ";
|
const std::string prefix = "HTTP/ ";
|
||||||
elevenlabs_t* el = conn->elevenlabs;
|
elevenlabs_t* el = conn->elevenlabs;
|
||||||
std::string header, value;
|
std::string header, value;
|
||||||
std::string input(buffer, bytes_received);
|
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 if (0 == header.compare("history-item-id")) el->history_item_id = strdup(value.c_str());
|
||||||
}
|
}
|
||||||
else {
|
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) {
|
if (input.rfind(prefix, 0) == 0) {
|
||||||
try {
|
try {
|
||||||
el->response_code = std::stoi(input.substr(prefix.length()));
|
el->response_code = extract_response_code(input);
|
||||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "parsed response code: %ld\n", el->response_code);
|
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) {
|
} 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());
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "header_callback: invalid response code %s\n", input.substr(prefix.length()).c_str());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -541,9 +541,27 @@ static bool parseHeader(const std::string& str, std::string& header, std::string
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
static size_t header_callback(char *buffer, size_t size, size_t nitems, ConnInfo_t *conn) {
|
||||||
size_t bytes_received = size * nitems;
|
size_t bytes_received = size * nitems;
|
||||||
const std::string prefix = "HTTP/2 ";
|
const std::string prefix = "HTTP/";
|
||||||
playht_t* p = conn->playht;
|
playht_t* p = conn->playht;
|
||||||
std::string header, value;
|
std::string header, value;
|
||||||
std::string input(buffer, bytes_received);
|
std::string input(buffer, bytes_received);
|
||||||
@@ -553,11 +571,11 @@ static size_t header_callback(char *buffer, size_t size, size_t nitems, ConnInfo
|
|||||||
else if (0 == header.compare("x-request-id")) p->request_id = strdup(value.c_str());
|
else if (0 == header.compare("x-request-id")) p->request_id = strdup(value.c_str());
|
||||||
}
|
}
|
||||||
else {
|
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) {
|
if (input.rfind(prefix, 0) == 0) {
|
||||||
try {
|
try {
|
||||||
p->response_code = std::stoi(input.substr(prefix.length()));
|
p->response_code = extract_response_code(input);
|
||||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "parsed response code: %ld\n", p->response_code);
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "header_callback: parsed response code: %ld\n", p->response_code);
|
||||||
} catch (const std::invalid_argument& e) {
|
} 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());
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "header_callback: invalid response code %s\n", input.substr(prefix.length()).c_str());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <switch_json.h>
|
#include <switch_json.h>
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
#include <boost/circular_buffer.hpp>
|
#include <boost/circular_buffer.hpp>
|
||||||
#include <boost/thread.hpp>
|
#include <boost/thread.hpp>
|
||||||
@@ -522,9 +523,27 @@ static bool parseHeader(const std::string& str, std::string& header, std::string
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
static size_t header_callback(char *buffer, size_t size, size_t nitems, ConnInfo_t *conn) {
|
||||||
size_t bytes_received = size * nitems;
|
size_t bytes_received = size * nitems;
|
||||||
const std::string prefix = "HTTP/2 ";
|
const std::string prefix = "HTTP/";
|
||||||
rimelabs_t* d = conn->rimelabs;
|
rimelabs_t* d = conn->rimelabs;
|
||||||
std::string header, value;
|
std::string header, value;
|
||||||
std::string input(buffer, bytes_received);
|
std::string input(buffer, bytes_received);
|
||||||
@@ -533,11 +552,11 @@ static size_t header_callback(char *buffer, size_t size, size_t nitems, ConnInfo
|
|||||||
if (0 == header.compare("dg-request-id")) d->request_id = strdup(value.c_str());
|
if (0 == header.compare("dg-request-id")) d->request_id = strdup(value.c_str());
|
||||||
}
|
}
|
||||||
else {
|
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) {
|
if (input.rfind(prefix, 0) == 0) {
|
||||||
try {
|
try {
|
||||||
d->response_code = std::stoi(input.substr(prefix.length()));
|
d->response_code = extract_response_code(input);
|
||||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "parsed response code: %ld\n", d->response_code);
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "header_callback: parsed response code: %ld\n", d->response_code);
|
||||||
} catch (const std::invalid_argument& e) {
|
} 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());
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "header_callback: invalid response code %s\n", input.substr(prefix.length()).c_str());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -544,9 +544,27 @@ static bool parseHeader(const std::string& str, std::string& header, std::string
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
static size_t header_callback(char *buffer, size_t size, size_t nitems, ConnInfo_t *conn) {
|
||||||
size_t bytes_received = size * nitems;
|
size_t bytes_received = size * nitems;
|
||||||
const std::string prefix = "HTTP/2 ";
|
const std::string prefix = "HTTP/";
|
||||||
whisper_t* w = conn->whisper;
|
whisper_t* w = conn->whisper;
|
||||||
std::string header, value;
|
std::string header, value;
|
||||||
std::string input(buffer, bytes_received);
|
std::string input(buffer, bytes_received);
|
||||||
@@ -556,11 +574,11 @@ static size_t header_callback(char *buffer, size_t size, size_t nitems, ConnInfo
|
|||||||
else if (0 == header.compare("x-request-id")) w->request_id = strdup(value.c_str());
|
else if (0 == header.compare("x-request-id")) w->request_id = strdup(value.c_str());
|
||||||
}
|
}
|
||||||
else {
|
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) {
|
if (input.rfind(prefix, 0) == 0) {
|
||||||
try {
|
try {
|
||||||
w->response_code = std::stoi(input.substr(prefix.length()));
|
w->response_code = extract_response_code(input);
|
||||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "parsed response code: %ld\n", w->response_code);
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "header_callback: parsed response code: %ld\n", w->response_code);
|
||||||
} catch (const std::invalid_argument& e) {
|
} 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());
|
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