@@ -120,138 +120,110 @@ namespace builtins {
120
120
JS::Result<mozilla::Ok> Backend::register_dynamic_backend (JSContext *cx, JS::HandleObject backend) {
121
121
MOZ_ASSERT (is_instance (backend));
122
122
123
- DynamicBackendConfig definition;
124
- std::memset (&definition, 0 , sizeof (definition));
123
+ xqd_world_string_t name_str;
124
+ JS::RootedString name (cx, JS::GetReservedSlot (backend, Backend::Slots::Name).toString ());
125
+ JS::UniqueChars nameChars = encode (cx, name, &name_str.len );
126
+ name_str.ptr = nameChars.get ();
127
+
128
+ xqd_world_string_t target_str;
129
+ JS::RootedString target (cx, JS::GetReservedSlot (backend, Backend::Slots::Target).toString ());
130
+ JS::UniqueChars targetChars = encode (cx, target, &target_str.len );
131
+ target_str.ptr = targetChars.get ();
125
132
126
- unsigned int backend_config_mask = 0u ;
133
+ fastly_dynamic_backend_config_t backend_config;
134
+ std::memset (&backend_config, 0 , sizeof (backend_config));
127
135
128
- std::optional<std:: string> hostOverride;
136
+ std::string hostOverride;
129
137
auto hostOverrideSlot = JS::GetReservedSlot (backend, Backend::Slots::HostOverride);
130
- if (!hostOverrideSlot.isNullOrUndefined ()) {
138
+ if ((backend_config. host_override . is_some = !hostOverrideSlot.isNullOrUndefined () )) {
131
139
JS::RootedString hostOverrideString (cx, hostOverrideSlot.toString ());
132
140
size_t hostOverride_len;
133
141
JS::UniqueChars hostOverrideChars = encode (cx, hostOverrideString, &hostOverride_len);
134
142
hostOverride = std::string (hostOverrideChars.get (), hostOverride_len);
143
+ backend_config.host_override .val .ptr = const_cast <char *>(hostOverride.c_str ());
144
+ backend_config.host_override .val .len = hostOverride.length ();
135
145
}
136
146
137
147
auto connectTimeoutSlot = JS::GetReservedSlot (backend, Backend::Slots::ConnectTimeout);
138
- if (!connectTimeoutSlot.isNullOrUndefined ()) {
139
- definition.connect_timeout_ms = connectTimeoutSlot.toInt32 ();
140
- backend_config_mask |= BACKEND_CONFIG_CONNECT_TIMEOUT;
141
- } else {
142
- definition.connect_timeout_ms = 0 ;
148
+ if ((backend_config.connect_timeout .is_some = !connectTimeoutSlot.isNullOrUndefined ())) {
149
+ backend_config.connect_timeout .val = connectTimeoutSlot.toInt32 ();
143
150
}
144
151
145
152
auto firstByteTimeoutSlot = JS::GetReservedSlot (backend, Backend::Slots::FirstByteTimeout);
146
- if (!firstByteTimeoutSlot.isNullOrUndefined ()) {
147
- definition.first_byte_timeout_ms = firstByteTimeoutSlot.toInt32 ();
148
- backend_config_mask |= BACKEND_CONFIG_FIRST_BYTE_TIMEOUT;
149
- } else {
150
- definition.first_byte_timeout_ms = 0 ;
153
+ if ((backend_config.first_byte_timeout .is_some = !firstByteTimeoutSlot.isNullOrUndefined ())) {
154
+ backend_config.first_byte_timeout .val = firstByteTimeoutSlot.toInt32 ();
151
155
}
156
+
152
157
auto betweenBytesTimeoutSlot = JS::GetReservedSlot (backend, Backend::Slots::BetweenBytesTimeout);
153
- if (!betweenBytesTimeoutSlot.isNullOrUndefined ()) {
154
- definition.between_bytes_timeout_ms = betweenBytesTimeoutSlot.toInt32 ();
155
- backend_config_mask |= BACKEND_CONFIG_BETWEEN_BYTES_TIMEOUT;
156
- } else {
157
- definition.between_bytes_timeout_ms = 0 ;
158
+ if ((backend_config.between_bytes_timeout .is_some =
159
+ !betweenBytesTimeoutSlot.isNullOrUndefined ())) {
160
+ backend_config.between_bytes_timeout .val = betweenBytesTimeoutSlot.toInt32 ();
161
+ }
162
+
163
+ auto useSslSlot = JS::GetReservedSlot (backend, Backend::Slots::UseSsl);
164
+ if ((backend_config.use_ssl .is_some = !useSslSlot.isNullOrUndefined ())) {
165
+ backend_config.use_ssl .val = useSslSlot.toBoolean ();
158
166
}
159
167
160
168
auto tlsMinVersion = JS::GetReservedSlot (backend, Backend::Slots::TlsMinVersion);
161
- if (!tlsMinVersion.isNullOrUndefined ()) {
162
- definition.ssl_min_version = tlsMinVersion.toInt32 ();
163
- backend_config_mask |= BACKEND_CONFIG_SSL_MIN_VERSION;
164
- } else {
165
- definition.ssl_min_version = 0 ;
169
+ if ((backend_config.ssl_min_version .is_some = !tlsMinVersion.isNullOrUndefined ())) {
170
+ backend_config.ssl_min_version .val = (int8_t )tlsMinVersion.toInt32 ();
166
171
}
172
+
167
173
auto tlsMaxVersion = JS::GetReservedSlot (backend, Backend::Slots::TlsMaxVersion);
168
- if (!tlsMaxVersion.isNullOrUndefined ()) {
169
- definition.ssl_max_version = tlsMaxVersion.toInt32 ();
170
- backend_config_mask |= BACKEND_CONFIG_SSL_MAX_VERSION;
171
- } else {
172
- definition.ssl_max_version = 0 ;
174
+ if ((backend_config.ssl_max_version .is_some = !tlsMaxVersion.isNullOrUndefined ())) {
175
+ backend_config.ssl_max_version .val = (int8_t )tlsMaxVersion.toInt32 ();
173
176
}
174
177
175
- std::optional<std:: string> certificateHostname;
178
+ std::string certificateHostname;
176
179
auto certificateHostnameSlot = JS::GetReservedSlot (backend, Backend::Slots::CertificateHostname);
177
- if (!certificateHostnameSlot.isNullOrUndefined ()) {
180
+ if ((backend_config. cert_hostname . is_some = !certificateHostnameSlot.isNullOrUndefined () )) {
178
181
JS::RootedString certificateHostnameString (cx, certificateHostnameSlot.toString ());
179
182
size_t certificateHostname_len;
180
183
JS::UniqueChars certificateHostnameChars =
181
184
encode (cx, certificateHostnameString, &certificateHostname_len);
182
185
certificateHostname = std::string (certificateHostnameChars.get (), certificateHostname_len);
186
+ backend_config.cert_hostname .val .ptr = const_cast <char *>(certificateHostname.c_str ());
187
+ backend_config.cert_hostname .val .len = certificateHostname.length ();
183
188
}
184
189
185
- std::optional<std:: string> caCertificate;
190
+ std::string caCertificate;
186
191
auto caCertificateSlot = JS::GetReservedSlot (backend, Backend::Slots::CaCertificate);
187
- if (!caCertificateSlot.isNullOrUndefined ()) {
192
+ if ((backend_config. ca_cert . is_some = !caCertificateSlot.isNullOrUndefined () )) {
188
193
JS::RootedString caCertificateString (cx, caCertificateSlot.toString ());
189
194
size_t caCertificate_len;
190
195
JS::UniqueChars caCertificateChars = encode (cx, caCertificateString, &caCertificate_len);
191
196
caCertificate = std::string (caCertificateChars.get (), caCertificate_len);
197
+ backend_config.ca_cert .val .ptr = const_cast <char *>(caCertificate.c_str ());
198
+ backend_config.ca_cert .val .len = caCertificate.length ();
192
199
}
193
200
194
- std::optional<std:: string> ciphers;
201
+ std::string ciphers;
195
202
auto ciphersSlot = JS::GetReservedSlot (backend, Backend::Slots::Ciphers);
196
- if (!ciphersSlot.isNullOrUndefined ()) {
203
+ if ((backend_config. ciphers . is_some = !ciphersSlot.isNullOrUndefined () )) {
197
204
JS::RootedString ciphersString (cx, ciphersSlot.toString ());
198
205
size_t ciphers_len;
199
206
JS::UniqueChars ciphersChars = encode (cx, ciphersString, &ciphers_len);
200
207
ciphers = std::string (ciphersChars.get (), ciphers_len);
208
+ backend_config.ciphers .val .ptr = const_cast <char *>(ciphers.c_str ());
209
+ backend_config.ciphers .val .len = ciphers.length ();
201
210
}
202
211
203
- std::optional<std:: string> sniHostname;
212
+ std::string sniHostname;
204
213
auto sniHostnameSlot = JS::GetReservedSlot (backend, Backend::Slots::SniHostname);
205
- if (!sniHostnameSlot.isNullOrUndefined ()) {
214
+ if ((backend_config. sni_hostname . is_some = !sniHostnameSlot.isNullOrUndefined () )) {
206
215
JS::RootedString sniHostnameString (cx, sniHostnameSlot.toString ());
207
216
size_t sniHostname_len;
208
217
JS::UniqueChars sniHostnameChars = encode (cx, sniHostnameString, &sniHostname_len);
209
218
sniHostname = std::string (sniHostnameChars.get (), sniHostname_len);
219
+ backend_config.sni_hostname .val .ptr = const_cast <char *>(sniHostname.c_str ());
220
+ backend_config.sni_hostname .val .len = sniHostname.length ();
210
221
}
211
222
212
- auto useSslSlot = JS::GetReservedSlot (backend, Backend::Slots::UseSsl);
213
- if (!useSslSlot.isNullOrUndefined () && useSslSlot.toBoolean ()) {
214
- backend_config_mask |= BACKEND_CONFIG_USE_SSL;
215
- }
216
-
217
- JS::RootedString name (cx, JS::GetReservedSlot (backend, Backend::Slots::Name).toString ());
218
- size_t name_len;
219
- JS::UniqueChars nameChars = encode (cx, name, &name_len);
220
- auto name_cstr = nameChars.get ();
221
-
222
- JS::RootedString target (cx, JS::GetReservedSlot (backend, Backend::Slots::Target).toString ());
223
- size_t target_len;
224
- JS::UniqueChars targetChars = encode (cx, target, &target_len);
225
- auto target_cstr = targetChars.get ();
226
- if (hostOverride.has_value ()) {
227
- backend_config_mask |= BACKEND_CONFIG_HOST_OVERRIDE;
228
- definition.host_override = hostOverride.value ().c_str ();
229
- definition.host_override_len = hostOverride.value ().length ();
230
- }
231
- if (certificateHostname.has_value ()) {
232
- backend_config_mask |= BACKEND_CONFIG_CERT_HOSTNAME;
233
- definition.cert_hostname = certificateHostname.value ().c_str ();
234
- definition.cert_hostname_len = certificateHostname.value ().length ();
235
- }
236
- if (caCertificate.has_value ()) {
237
- backend_config_mask |= BACKEND_CONFIG_CA_CERT;
238
- definition.ca_cert = caCertificate.value ().c_str ();
239
- definition.ca_cert_len = caCertificate.value ().length ();
240
- }
241
- if (ciphers.has_value ()) {
242
- backend_config_mask |= BACKEND_CONFIG_CIPHERS;
243
- definition.ciphers = ciphers.value ().c_str ();
244
- definition.ciphers_len = ciphers.value ().length ();
245
- }
246
- if (sniHostname.has_value ()) {
247
- backend_config_mask |= BACKEND_CONFIG_SNI_HOSTNAME;
248
- definition.sni_hostname = sniHostname.value ().c_str ();
249
- definition.sni_hostname_len = sniHostname.value ().length ();
250
- }
251
-
252
- auto result = xqd_req_register_dynamic_backend (name_cstr, name_len, target_cstr, target_len,
253
- backend_config_mask, &definition);
254
- if (!HANDLE_RESULT (cx, result)) {
223
+ fastly_error_t err;
224
+ auto result =
225
+ xqd_fastly_http_req_register_dynamic_backend (&name_str, &target_str, &backend_config, &err);
226
+ if (!HANDLE_RESULT (cx, result, err)) {
255
227
return JS::Result<mozilla::Ok>(JS::Error ());
256
228
} else {
257
229
return mozilla::Ok ();
0 commit comments