-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathstream_protocol.cpp
More file actions
174 lines (159 loc) · 5.05 KB
/
Copy pathstream_protocol.cpp
File metadata and controls
174 lines (159 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "stream_protocol.h"
#include <cctype>
#include <cstring>
namespace stream_protocol {
JsonMessageType classify_json_message(const char *type) {
if (!type) {
return JsonMessageType::Unhandled;
}
if (std::strstr(type, "error")) {
return JsonMessageType::Error;
}
if (std::strcmp(type, "input_audio_buffer.speech_started") == 0) {
return JsonMessageType::SpeechStarted;
}
if (std::strcmp(type, "input_audio_buffer.speech_stopped") == 0) {
return JsonMessageType::SpeechStopped;
}
if (std::strcmp(type, "response.output_audio.delta") == 0) {
return JsonMessageType::AudioDelta;
}
if (std::strcmp(type, "response.output_audio.done") == 0) {
return JsonMessageType::AudioDone;
}
return JsonMessageType::Unhandled;
}
bool json_depth_exceeded(const char *json, int max_depth) {
if (!json || max_depth < 0) {
return true;
}
// FreeSWITCH's bundled cJSON parser is recursive. Reject excessive nesting before parsing so
// an untrusted peer cannot exhaust the WebSocket thread's stack.
int depth = 0;
bool in_string = false;
for (; *json; ++json) {
const char c = *json;
if (in_string) {
if (c == '\\' && json[1]) {
++json;
} else if (c == '"') {
in_string = false;
}
} else if (c == '"') {
in_string = true;
} else if (c == '{' || c == '[') {
if (++depth > max_depth) {
return true;
}
} else if (c == '}' || c == ']') {
if (depth > 0) {
--depth;
}
}
}
return false;
}
bool validate_ws_uri(const char *url, char *destination, std::size_t destination_size) {
if (!url || !destination || destination_size == 0) {
return false;
}
const char *host_start = nullptr;
const char *host_end = nullptr;
if (std::strncmp(url, "ws://", 5) == 0) {
host_start = url + 5;
} else if (std::strncmp(url, "wss://", 6) == 0) {
host_start = url + 6;
} else {
return false;
}
if (*host_start == '[') {
host_end = host_start + 1;
while (*host_end && *host_end != ']') {
const unsigned char c = static_cast<unsigned char>(*host_end);
if (!std::isxdigit(c) && *host_end != ':' && *host_end != '.') {
return false;
}
++host_end;
}
if (*host_end != ']' || host_end == host_start + 1) {
return false;
}
++host_end;
} else {
host_end = host_start;
while (*host_end && *host_end != ':' && *host_end != '/' && *host_end != '?') {
const unsigned char c = static_cast<unsigned char>(*host_end);
if (!std::isalnum(c) && *host_end != '-' && *host_end != '.') {
return false;
}
++host_end;
}
if (host_start == host_end) {
return false;
}
}
if (*host_end == ':') {
const char *port_start = host_end + 1;
const char *p = port_start;
long port = 0;
while (*p && *p != '/' && *p != '?') {
const unsigned char c = static_cast<unsigned char>(*p);
if (!std::isdigit(c)) {
return false;
}
port = port * 10 + (*p - '0');
if (port > 65535) {
return false;
}
++p;
}
if (p == port_start || port == 0) {
return false;
}
host_end = p;
}
if (*host_end != '\0' && *host_end != '/' && *host_end != '?') {
return false;
}
const std::size_t length = std::strlen(url);
if (length >= destination_size) {
return false;
}
std::memcpy(destination, url, length + 1);
return true;
}
bool is_valid_utf8(const char *text) {
if (!text) {
return false;
}
const auto *bytes = reinterpret_cast<const unsigned char *>(text);
while (*bytes) {
const unsigned char lead = bytes[0];
if (lead <= 0x7F) {
++bytes;
} else if (lead >= 0xC2 && lead <= 0xDF) {
if ((bytes[1] & 0xC0) != 0x80) {
return false;
}
bytes += 2;
} else if (lead >= 0xE0 && lead <= 0xEF) {
const unsigned char second = bytes[1];
if ((second & 0xC0) != 0x80 || (bytes[2] & 0xC0) != 0x80 || (lead == 0xE0 && second < 0xA0) ||
(lead == 0xED && second > 0x9F)) {
return false;
}
bytes += 3;
} else if (lead >= 0xF0 && lead <= 0xF4) {
const unsigned char second = bytes[1];
if ((second & 0xC0) != 0x80 || (bytes[2] & 0xC0) != 0x80 || (bytes[3] & 0xC0) != 0x80 ||
(lead == 0xF0 && second < 0x90) || (lead == 0xF4 && second > 0x8F)) {
return false;
}
bytes += 4;
} else {
return false;
}
}
return true;
}
} // namespace stream_protocol