@@ -661,6 +661,105 @@ static std::string GetEndpointFromUrl(std::string const& url) {
661661// / `process-utils.js` depends on simple http client error messages.
662662// / this needs to be adjusted if this is ever
1E79
changed!
663663// //////////////////////////////////////////////////////////////////////////////
664+ namespace {
665+
666+ auto getEndpoint (v8::Isolate* isolate, std::vector<std::string> const & endpoints,
667+ std::string& url, std::string& lastEndpoint)
668+ -> std::tuple<std::string, std::string, std::string> {
669+ // returns endpoint, relative, error
670+ std::string relative;
671+ std::string endpoint;
672+ if (url.substr (0 , 7 ) == " http://" ) {
673+ endpoint = GetEndpointFromUrl (url).substr (7 );
674+ relative = url.substr (7 + endpoint.length ());
675+
676+ if (relative.empty () || relative[0 ] != ' /' ) {
677+ relative = " /" + relative;
678+ }
679+ if (endpoint.find (' :' ) == std::string::npos) {
680+ endpoint.append (" :80" );
681+ }
682+ endpoint = " tcp://" + endpoint;
683+ } else if (url.substr (0 , 8 ) == " https://" ) {
684+ endpoint = GetEndpointFromUrl (url).substr (8 );
685+ relative = url.substr (8 + endpoint.length ());
686+
687+ if (relative.empty () || relative[0 ] != ' /' ) {
688+ relative = " /" + relative;
689+ }
690+ if (endpoint.find (' :' ) == std::string::npos) {
691+ endpoint.append (" :443" );
692+ }
693+ endpoint = " ssl://" + endpoint;
694+ } else if (url.substr (0 , 5 ) == " h2://" ) {
695+ endpoint = GetEndpointFromUrl (url).substr (5 );
696+ relative = url.substr (5 + endpoint.length ());
697+
698+ if (relative.empty () || relative[0 ] != ' /' ) {
699+ relative = " /" + relative;
700+ }
701+ if (endpoint.find (' :' ) == std::string::npos) {
702+ endpoint.append (" :80" );
703+ }
704+ endpoint = " tcp://" + endpoint;
705+ } else if (url.substr (0 , 6 ) == " h2s://" ) {
706+ endpoint = GetEndpointFromUrl (url).substr (6 );
707+ relative = url.substr (6 + endpoint.length ());
708+
709+ if (relative.empty () || relative[0 ] != ' /' ) {
710+ relative = " /" + relative;
711+ }
712+ if (endpoint.find (' :' ) == std::string::npos) {
713+
474D
endpoint.append (" :443" );
714+ }
715+ endpoint = " ssl://" + endpoint;
716+ } else if (url.substr (0 , 6 ) == " srv://" ) {
717+ size_t found = url.find (' /' , 6 );
718+
719+ relative = " /" ;
720+ if (found != std::string::npos) {
721+ relative.append (url.substr (found + 1 ));
722+ endpoint = url.substr (6 , found - 6 );
723+ } else {
724+ endpoint = url.substr (6 );
725+ }
726+ endpoint = " srv://" + endpoint;
727+ } else if (url.substr (0 , 7 ) == " unix://" ) {
728+ // Can only have arrived here if endpoints is non empty
729+ if (endpoints.empty ()) {
730+ return {" " , " " , std::move (" unsupported URL specified" )};
731+ }
732+ endpoint = endpoints.front ();
733+ relative = url.substr (endpoint.size ());
734+ } else if (!url.empty () && url[0 ] == ' /' ) {
735+ size_t found;
736+ // relative URL. prefix it with last endpoint
737+ relative = url;
738+ url = lastEndpoint + url;
739+ endpoint = lastEndpoint;
740+ if (endpoint.substr (0 , 5 ) == " http:" ) {
741+ endpoint = endpoint.substr (5 );
742+ found = endpoint.find (" :" );
743+ if (found == std::string::npos) {
744+ endpoint = endpoint + " :80" ;
745+ }
746+ endpoint = " tcp:" + endpoint;
747+ } else if (endpoint.substr (0 , 6 ) == " https:" ) {
748+ endpoint = endpoint.substr (6 );
749+ found = endpoint.find (" :" );
750+ if (found == std::string::npos) {
751+ endpoint = endpoint + " :443" ;
752+ }
753+ endpoint = " ssl:" + endpoint;
754+ }
755+ } else {
756+ std::string msg (" unsupported URL specified: '" );
757+ msg.append (url).append (" '" );
758+ return {" " , " " , std::move (msg)};
759<
474D
/td>+ }
760+ return {std::move (endpoint), std::move (relative), " " };
761+ }
762+ } // namespace
664763
665764void JS_Download (v8::FunctionCallbackInfo<v8::Value> const & args) {
666765 TRI_V8_TRY_CATCH_BEGIN (isolate);
@@ -679,7 +778,8 @@ void JS_Download(v8::FunctionCallbackInfo<v8::Value> const& args) {
679778 TRI_V8_THROW_EXCEPTION_USAGE (signature);
680779 }
681780
682- std::string url = TRI_ObjectToString (isolate, args[0 ]);
781+ std::string const inputUrl = TRI_ObjectToString (isolate, args[0 ]);
782+ std::string url = inputUrl;
683783 std::vector<std::string> endpoints;
684784
685785 bool isLocalUrl = false ;
@@ -892,104 +992,18 @@ void JS_Download(v8::FunctionCallbackInfo<v8::Value> const& args) {
892992 int numRedirects = 0 ;
893993
894994 while (numRedirects < maxRedirects) {
895- std::string endpoint;
896- std::string relative;
897-
898- if (url.substr (0 , 7 ) == " http://" ) {
899- endpoint = GetEndpointFromUrl (url).substr (7 );
900- relative = url.substr (7 + endpoint.length ());
901-
902- if (relative.empty () || relative[0 ] != ' /' ) {
903- relative = " /" + relative;
904- }
905- if (endpoint.find (' :' ) == std::string::npos) {
906- endpoint.append (" :80" );
907- }
908- endpoint = " tcp://" + endpoint;
909- } else if (url.substr (0 , 8 ) == " https://" ) {
910- endpoint = GetEndpointFromUrl (url).substr (8 );
911- relative = url.substr (8 + endpoint.length ());
912-
913- if (relative.empty () || relative[0 ] != ' /' ) {
914- relative = " /" + relative;
915- }
916- if (endpoint.find (' :' ) == std::string::npos) {
917- endpoint.append (" :443" );
918- }
919- endpoint = " ssl://" + endpoint;
920- } <
474D
span class="pl-k">else if (url.substr (0 , 5 ) == " h2://" ) {
921- endpoint = GetEndpointFromUrl (url).substr (5 );
922- relative = url.substr (5 + endpoint.length ());
923995
924- if (relative.empty () || relative[0 ] != ' /' ) {
925- relative = " /" + relative;
926- }
927- if (endpoint.find (' :' ) == std::string::npos) {
928- endpoint.append (" :80" );
929- }
930- endpoint = " tcp://" + endpoint;
931- } else if (url.substr (0 , 6 ) == " h2s://" ) {
932- endpoint = GetEndpointFromUrl (url).substr (6 );
933- relative = url.substr (6 + endpoint.length ());
934-
935- if (relative.empty () || relative[0 ] != ' /' ) {
936- relative = " /" + relative;
937- }
938- if (endpoint.find (' :' ) == std::string::npos) {
939- endpoint.append (" :80" );
940- }
941- endpoint = " tcp://" + endpoint;
942- } else if (url.substr (0 , 6 ) == " srv://" ) {
943- size_t found = url.find (' /' , 6 );
944-
945- relative = " /" ;
946- if (found != std::string::npos) {
947- relative.append (url.substr (found + 1 ));
948- endpoint = url.substr (6 , found - 6 );
949- } else {
950- endpoint = url.substr (6 );
951- }
952- endpoint = " srv://" + endpoint;
953- } else if (url.substr (0 , 7 ) == " unix://" ) {
954- // Can only have arrived here if endpoints is non empty
955- if (endpoints.empty ()) {
956- TRI_V8_THROW_SYNTAX_ERROR (" unsupported URL specified" );
957- }
958- endpoint = endpoints.front ();
959- relative = url.substr (endpoint.size ());
960- } else if (!url.empty () && url[0 ] == ' /' ) {
961- size_t found;
962- // relative URL. prefix it with last endpoint
963- relative = url;
964- url = lastEndpoint + url;
965- endpoint = lastEndpoint;
966- if (endpoint.substr (0 , 5 ) == " http:" ) {
967- endpoint = endpoint.substr (5 );
968- found = endpoint.find (" :" );
969- if (found == std::string::npos) {
970- endpoint = endpoint + " :80" ;
971- }
972- endpoint = " tcp:" + endpoint;
973- } else if (endpoint.substr (0 , 6 ) == " https:" ) {
974- endpoint = endpoint.substr (6 );
975- found = endpoint.find (" :" );
976- if (found == std::string::npos) {
977- endpoint = endpoint + " :443" ;
978- }
979- endpoint = " ssl:" + endpoint;
980- }
981- } else {
982- std::string msg (" unsupported URL specified: '" );
983- msg.append (url).append (" '" );
984- TRI_V8_THROW_ERROR (msg.c_str ());
996+ auto [endpoint, relative, error] = getEndpoint (isolate, endpoints, url, lastEndpoint);
997+ if (!error.empty ()) {
998+ TRI_V8_THROW_SYNTAX_ERROR (error.c_str ());
985999 }
9861000
9871001 LOG_TOPIC (" d6bdb" , TRACE, arangodb::Logger::FIXME)
988- << " downloading file. endpoint: " << endpoint << " , relative URL: " << url;
1002+ << " downloading file. endpoint: " << endpoint << " , relative URL: " << url;
9891003
990- if (!isLocalUrl && !v8security.isAllowedToConnectToEndpoint (isolate, endpoint)) {
1004+ if (!isLocalUrl && !v8security.isAllowedToConnectToEndpoint (isolate, endpoint, inputUrl )) {
9911005 TRI_V8_THROW_EXCEPTION_MESSAGE (TRI_ERROR_FORBIDDEN,
992- " not allowed to connect to this endpoint " );
1006+ " not allowed to connect to this URL: " + inputUrl );
9931007 }
9941008
9951009 std::unique_ptr<Endpoint> ep (Endpoint::clientFactory (endpoint));
@@ -1000,8 +1014,8 @@ void JS_Download(v8::FunctionCallbackInfo<v8::Value> const& args) {
10001014 }
10011015
10021016 std::unique_ptr<GeneralClientConnection> connection (
1003- GeneralClientConnection::factory (v8g->_server , ep.get (), timeout,
1004- timeout, 3 , sslProtocol));
1017+ GeneralClientConnection::factory (v8g->_server , ep.get (), timeout,
1018+ timeout, 3 , sslProtocol));
10051019
10061020 if (connection == nullptr ) {
10071021 TRI_V8_THROW_EXCEPTION_M
38DD
EMORY ();
0 commit comments