@@ -712,109 +712,93 @@ bool Backend::isCipherSuiteSupportedByFastly(std::string_view cipherSpec) {
712
712
JS::Result<mozilla::Ok> Backend::register_dynamic_backend (JSContext *cx, JS::HandleObject backend) {
713
713
MOZ_ASSERT (is_instance (backend));
714
714
715
- fastly_world_string_t name_str;
716
715
JS::RootedString name (cx, JS::GetReservedSlot (backend, Backend::Slots::Name).toString ());
717
- JS::UniqueChars nameChars = encode (cx, name, &name_str.len );
718
- name_str.ptr = nameChars.get ();
716
+ size_t name_len;
717
+ JS::UniqueChars nameChars = encode (cx, name, &name_len);
718
+ std::string_view name_str{nameChars.get (), name_len};
719
719
720
- fastly_world_string_t target_str;
721
720
JS::RootedString target (cx, JS::GetReservedSlot (backend, Backend::Slots::Target).toString ());
722
- JS::UniqueChars targetChars = encode (cx, target, &target_str.len );
723
- target_str.ptr = targetChars.get ();
721
+ size_t target_len;
722
+ JS::UniqueChars targetChars = encode (cx, target, &target_len);
723
+ std::string_view target_str{targetChars.get (), target_len};
724
724
725
- fastly_dynamic_backend_config_t backend_config;
726
- std::memset (&backend_config, 0 , sizeof (backend_config));
725
+ BackendConfig backend_config;
727
726
728
- std::string hostOverride;
729
727
auto hostOverrideSlot = JS::GetReservedSlot (backend, Backend::Slots::HostOverride);
730
- if ((backend_config. host_override . is_some = !hostOverrideSlot.isNullOrUndefined () )) {
728
+ if (!hostOverrideSlot.isNullOrUndefined ()) {
731
729
JS::RootedString hostOverrideString (cx, hostOverrideSlot.toString ());
732
730
size_t hostOverride_len;
733
731
JS::UniqueChars hostOverrideChars = encode (cx, hostOverrideString, &hostOverride_len);
734
- hostOverride = std::string (hostOverrideChars.get (), hostOverride_len);
735
- backend_config.host_override .val .ptr = const_cast <char *>(hostOverride.c_str ());
736
- backend_config.host_override .val .len = hostOverride.length ();
732
+ backend_config.host_override .emplace (std::move (hostOverrideChars), hostOverride_len);
737
733
}
738
734
739
735
auto connectTimeoutSlot = JS::GetReservedSlot (backend, Backend::Slots::ConnectTimeout);
740
- if ((backend_config. connect_timeout . is_some = !connectTimeoutSlot.isNullOrUndefined () )) {
741
- backend_config.connect_timeout . val = connectTimeoutSlot.toInt32 ();
736
+ if (!connectTimeoutSlot.isNullOrUndefined ()) {
737
+ backend_config.connect_timeout = connectTimeoutSlot.toInt32 ();
742
738
}
743
739
744
740
auto firstByteTimeoutSlot = JS::GetReservedSlot (backend, Backend::Slots::FirstByteTimeout);
745
- if ((backend_config. first_byte_timeout . is_some = !firstByteTimeoutSlot.isNullOrUndefined () )) {
746
- backend_config.first_byte_timeout . val = firstByteTimeoutSlot.toInt32 ();
741
+ if (!firstByteTimeoutSlot.isNullOrUndefined ()) {
742
+ backend_config.first_byte_timeout = firstByteTimeoutSlot.toInt32 ();
747
743
}
748
744
749
745
auto betweenBytesTimeoutSlot = JS::GetReservedSlot (backend, Backend::Slots::BetweenBytesTimeout);
750
- if ((backend_config.between_bytes_timeout .is_some =
751
- !betweenBytesTimeoutSlot.isNullOrUndefined ())) {
752
- backend_config.between_bytes_timeout .val = betweenBytesTimeoutSlot.toInt32 ();
746
+ if (!betweenBytesTimeoutSlot.isNullOrUndefined ()) {
747
+ backend_config.between_bytes_timeout = betweenBytesTimeoutSlot.toInt32 ();
753
748
}
754
749
755
750
auto useSslSlot = JS::GetReservedSlot (backend, Backend::Slots::UseSsl);
756
- if ((backend_config. use_ssl . is_some = !useSslSlot.isNullOrUndefined () )) {
757
- backend_config.use_ssl . val = useSslSlot.toBoolean ();
751
+ if (!useSslSlot.isNullOrUndefined ()) {
752
+ backend_config.use_ssl = useSslSlot.toBoolean ();
758
753
}
759
754
760
755
auto tlsMinVersion = JS::GetReservedSlot (backend, Backend::Slots::TlsMinVersion);
761
- if ((backend_config. ssl_min_version . is_some = !tlsMinVersion.isNullOrUndefined () )) {
762
- backend_config.ssl_min_version . val = ( int8_t ) tlsMinVersion.toInt32 ();
756
+ if (!tlsMinVersion.isNullOrUndefined ()) {
757
+ backend_config.ssl_min_version = static_cast < uint8_t >( tlsMinVersion.toInt32 () );
763
758
}
764
759
765
760
auto tlsMaxVersion = JS::GetReservedSlot (backend, Backend::Slots::TlsMaxVersion);
766
- if ((backend_config. ssl_max_version . is_some = !tlsMaxVersion.isNullOrUndefined () )) {
767
- backend_config.ssl_max_version . val = ( int8_t ) tlsMaxVersion.toInt32 ();
761
+ if (!tlsMaxVersion.isNullOrUndefined ()) {
762
+ backend_config.ssl_max_version = static_cast < int8_t >( tlsMaxVersion.toInt32 () );
768
763
}
769
764
770
- std::string certificateHostname;
771
765
auto certificateHostnameSlot = JS::GetReservedSlot (backend, Backend::Slots::CertificateHostname);
772
- if ((backend_config. cert_hostname . is_some = !certificateHostnameSlot.isNullOrUndefined () )) {
766
+ if (!certificateHostnameSlot.isNullOrUndefined ()) {
773
767
JS::RootedString certificateHostnameString (cx, certificateHostnameSlot.toString ());
774
768
size_t certificateHostname_len;
775
769
JS::UniqueChars certificateHostnameChars =
776
770
encode (cx, certificateHostnameString, &certificateHostname_len);
777
- certificateHostname = std::string (certificateHostnameChars.get (), certificateHostname_len);
778
- backend_config.cert_hostname .val .ptr = const_cast <char *>(certificateHostname.c_str ());
779
- backend_config.cert_hostname .val .len = certificateHostname.length ();
771
+ backend_config.cert_hostname .emplace (std::move (certificateHostnameChars),
772
+ certificateHostname_len);
780
773
}
781
774
782
- std::string caCertificate;
783
775
auto caCertificateSlot = JS::GetReservedSlot (backend, Backend::Slots::CaCertificate);
784
- if ((backend_config. ca_cert . is_some = !caCertificateSlot.isNullOrUndefined () )) {
776
+ if (!caCertificateSlot.isNullOrUndefined ()) {
785
777
JS::RootedString caCertificateString (cx, caCertificateSlot.toString ());
786
778
size_t caCertificate_len;
787
779
JS::UniqueChars caCertificateChars = encode (cx, caCertificateString, &caCertificate_len);
788
- caCertificate = std::string (caCertificateChars.get (), caCertificate_len);
789
- backend_config.ca_cert .val .ptr = const_cast <char *>(caCertificate.c_str ());
790
- backend_config.ca_cert .val .len = caCertificate.length ();
780
+ backend_config.ca_cert .emplace (std::move (caCertificateChars), caCertificate_len);
791
781
}
792
782
793
- std::string ciphers;
794
783
auto ciphersSlot = JS::GetReservedSlot (backend, Backend::Slots::Ciphers);
795
- if ((backend_config. ciphers . is_some = !ciphersSlot.isNullOrUndefined () )) {
784
+ if (!ciphersSlot.isNullOrUndefined ()) {
796
785
JS::RootedString ciphersString (cx, ciphersSlot.toString ());
797
786
size_t ciphers_len;
798
787
JS::UniqueChars ciphersChars = encode (cx, ciphersString, &ciphers_len);
799
- ciphers = std::string (ciphersChars.get (), ciphers_len);
800
- backend_config.ciphers .val .ptr = const_cast <char *>(ciphers.c_str ());
801
- backend_config.ciphers .val .len = ciphers.length ();
788
+ backend_config.ciphers .emplace (std::move (ciphersChars), ciphers_len);
802
789
}
803
790
804
- std::string sniHostname;
805
791
auto sniHostnameSlot = JS::GetReservedSlot (backend, Backend::Slots::SniHostname);
806
- if ((backend_config. sni_hostname . is_some = !sniHostnameSlot.isNullOrUndefined () )) {
792
+ if (!sniHostnameSlot.isNullOrUndefined ()) {
807
793
JS::RootedString sniHostnameString (cx, sniHostnameSlot.toString ());
808
794
size_t sniHostname_len;
809
795
JS::UniqueChars sniHostnameChars = encode (cx, sniHostnameString, &sniHostname_len);
810
- sniHostname = std::string (sniHostnameChars.get (), sniHostname_len);
811
- backend_config.sni_hostname .val .ptr = const_cast <char *>(sniHostname.c_str ());
812
- backend_config.sni_hostname .val .len = sniHostname.length ();
796
+ backend_config.sni_hostname .emplace (std::move (sniHostnameChars), sniHostname_len);
813
797
}
814
798
815
- fastly_error_t err ;
816
- if (! fastly_http_req_register_dynamic_backend (&name_str, &target_str, &backend_config, &err )) {
817
- HANDLE_ERROR (cx, err);
799
+ auto res = HttpReq::register_dynamic_backend (name_str, target_str, backend_config) ;
800
+ if (auto *err = res. to_err ( )) {
801
+ HANDLE_ERROR (cx, * err);
818
802
return JS::Result<mozilla::Ok>(JS::Error ());
819
803
}
820
804
return mozilla::Ok ();
0 commit comments