From 61e05f912557e3086cd80a10075aeaa6586519be Mon Sep 17 00:00:00 2001
From: maksymsproductengine <maksymsproductengine@lindenlab.com>
Date: Mon, 22 Sep 2014 20:01:55 +0300
Subject: [PATCH] MAINT-4170 Eliminate homoglyphs in URL hostnames: used
 3p-uriparser;

---
 autobuild.xml                   | 48 +++++++++++++++++++++++++++++++++
 indra/cmake/CMakeLists.txt      |  2 ++
 indra/cmake/FindURIPARSER.cmake | 46 +++++++++++++++++++++++++++++++
 indra/cmake/URIPARSER.cmake     | 22 +++++++++++++++
 indra/llui/CMakeLists.txt       |  3 +++
 indra/llui/lltextbase.cpp       | 43 ++++++++++++++++++++++++++++-
 indra/llui/lltextbase.h         |  1 +
 7 files changed, 164 insertions(+), 1 deletion(-)
 create mode 100644 indra/cmake/FindURIPARSER.cmake
 create mode 100644 indra/cmake/URIPARSER.cmake

diff --git a/autobuild.xml b/autobuild.xml
index 19fa1b8d829..4fe6725dca7 100755
--- a/autobuild.xml
+++ b/autobuild.xml
@@ -1901,6 +1901,54 @@
           </map>
         </map>
       </map>
+      <key>uriparser</key>
+      <map>
+          <key>license</key>
+          <string>uriparser</string>
+          <key>license_file</key>
+          <string>LICENSES/uriparser.txt</string>
+          <key>name</key>
+          <string>uriparser</string>
+          <key>platforms</key>
+          <map>
+              <key>darwin</key>
+              <map>
+                  <key>archive</key>
+                  <map>
+                      <key>hash</key>
+                      <string>99c5a966ac74eea5a505317396152168</string>
+                      <key>url</key>
+                      <string>http://automated-builds-secondlife-com.s3.amazonaws.com/hg/repo/3p-uriparser/rev/294299/arch/Darwin/installer/uriparser-0.8.0.1-darwin-20140917.tar.bz2</string>
+                  </map>
+                  <key>name</key>
+                  <string>darwin</string>
+              </map>
+              <key>linux</key>
+              <map>
+                  <key>archive</key>
+                  <map>
+                      <key>hash</key>
+                      <string>34306fb90364b182dc770375140d8557</string>
+                      <key>url</key>
+                      <string>http://automated-builds-secondlife-com.s3.amazonaws.com/hg/repo/3p-uriparser/rev/294298/arch/Linux/installer/uriparser-0.8.0.1-linux-20140918.tar.bz2</string>
+                  </map>
+                  <key>name</key>
+                  <string>linux</string>
+              </map>
+              <key>windows</key>
+              <map>
+                  <key>archive</key>
+                  <map>
+                      <key>hash</key>
+                      <string>73817db47bc0f87269861b7887319414</string>
+                      <key>url</key>
+                      <string>http://automated-builds-secondlife-com.s3.amazonaws.com/hg/repo/3p-uriparser/rev/294274/arch/CYGWIN/installer/uriparser-0.8.0.1-windows-20140917.tar.bz2</string>
+                  </map>
+                  <key>name</key>
+                  <string>windows</string>
+              </map>
+          </map>
+      </map>
       <key>xmlrpc-epi</key>
       <map>
         <key>license</key>
diff --git a/indra/cmake/CMakeLists.txt b/indra/cmake/CMakeLists.txt
index 10a23ea068f..9abdb4b1466 100755
--- a/indra/cmake/CMakeLists.txt
+++ b/indra/cmake/CMakeLists.txt
@@ -37,6 +37,7 @@ set(cmake_SOURCE_FILES
     FindNDOF.cmake
     FindOpenJPEG.cmake
     FindSCP.cmake
+    FindURIPARSER.cmake
     FindXmlRpcEpi.cmake
     FindZLIB.cmake
     FMODEX.cmake
@@ -97,6 +98,7 @@ set(cmake_SOURCE_FILES
     Tut.cmake
     UI.cmake
     UnixInstall.cmake
+    URIPARSER.cmake
     Variables.cmake
     ViewerMiscLibs.cmake
     VisualLeakDetector.cmake
diff --git a/indra/cmake/FindURIPARSER.cmake b/indra/cmake/FindURIPARSER.cmake
new file mode 100644
index 00000000000..32457bcd60c
--- /dev/null
+++ b/indra/cmake/FindURIPARSER.cmake
@@ -0,0 +1,46 @@
+# -*- cmake -*-
+
+# - Find uriparser
+# Find the URIPARSER includes and library
+# This module defines
+#  URIPARSER_INCLUDE_DIRS, where to find uriparser.h, etc.
+#  URIPARSER_LIBRARIES, the libraries needed to use uriparser.
+#  URIPARSER_FOUND, If false, do not try to use uriparser.
+#
+# This FindURIPARSER is about 43 times as fast the one provided with cmake (2.8.x),
+# because it doesn't look up the version of uriparser, resulting in a dramatic
+# speed up for configure (from 4 minutes 22 seconds to 6 seconds).
+#
+# Note: Since this file is only used for standalone, the windows
+# specific parts were left out.
+
+FIND_PATH(URIPARSER_INCLUDE_DIR uriparser/uri.h
+  NO_SYSTEM_ENVIRONMENT_PATH
+  )
+
+FIND_LIBRARY(URIPARSER_LIBRARY z)
+
+if (URIPARSER_LIBRARY AND URIPARSER_INCLUDE_DIR)
+  SET(URIPARSER_INCLUDE_DIRS ${URIPARSER_INCLUDE_DIR})
+  SET(URIPARSER_LIBRARIES ${URIPARSER_LIBRARY})
+  SET(URIPARSER_FOUND "YES")
+else (URIPARSER_LIBRARY AND URIPARSER_INCLUDE_DIR)
+  SET(URIPARSER_FOUND "NO")
+endif (URIPARSER_LIBRARY AND URIPARSER_INCLUDE_DIR)
+
+if (URIPARSER_FOUND)
+  if (NOT URIPARSER_FIND_QUIETLY)
+    message(STATUS "Found URIPARSER: ${URIPARSER_LIBRARIES}")
+    SET(URIPARSER_FIND_QUIETLY TRUE)
+  endif (NOT URIPARSER_FIND_QUIETLY)
+else (URIPARSER_FOUND)
+  if (URIPARSER_FIND_REQUIRED)
+    message(FATAL_ERROR "Could not find URIPARSER library")
+  endif (URIPARSER_FIND_REQUIRED)
+endif (URIPARSER_FOUND)
+
+mark_as_advanced(
+  URIPARSER_LIBRARY
+  URIPARSER_INCLUDE_DIR
+  )
+
diff --git a/indra/cmake/URIPARSER.cmake b/indra/cmake/URIPARSER.cmake
new file mode 100644
index 00000000000..477e142ce56
--- /dev/null
+++ b/indra/cmake/URIPARSER.cmake
@@ -0,0 +1,22 @@
+# -*- cmake -*-
+
+set(URIPARSER_FIND_QUIETLY ON)
+set(URIPARSER_FIND_REQUIRED ON)
+
+include(Prebuilt)
+
+if (STANDALONE)
+  include(FindURIPARSER)
+else (STANDALONE)
+  use_prebuilt_binary(uriparser)
+  if (WINDOWS)
+    set(URIPARSER_LIBRARIES 
+      debug uriparserd
+      optimized uriparser)
+  else (WINDOWS)
+    set(URIPARSER_LIBRARIES z)
+  endif (WINDOWS)
+  if (WINDOWS OR LINUX)
+    set(URIPARSER_INCLUDE_DIRS ${LIBS_PREBUILT_DIR}/include/uriparser)
+  endif (WINDOWS OR LINUX)
+endif (STANDALONE)
diff --git a/indra/llui/CMakeLists.txt b/indra/llui/CMakeLists.txt
index 52738aeb6fb..24fdc2268d5 100755
--- a/indra/llui/CMakeLists.txt
+++ b/indra/llui/CMakeLists.txt
@@ -12,6 +12,7 @@ include(LLRender)
 include(LLWindow)
 include(LLVFS)
 include(LLXML)
+include(URIPARSER)
 
 include_directories(
     ${LLCOMMON_INCLUDE_DIRS}
@@ -28,6 +29,7 @@ include_directories(
 include_directories(SYSTEM
     ${LLCOMMON_SYSTEM_INCLUDE_DIRS}
     ${LLXML_SYSTEM_INCLUDE_DIRS}
+    ${URIPARSER_INCLUDE_DIRS}
     )
 
 set(llui_SOURCE_FILES
@@ -278,6 +280,7 @@ target_link_libraries(llui
     ${LLXML_LIBRARIES}
     ${LLMATH_LIBRARIES}
     ${HUNSPELL_LIBRARY}
+    ${URIPARSER_LIBRARIES}
     ${LLCOMMON_LIBRARIES} # must be after llimage, llwindow, llrender
     )
 
diff --git a/indra/llui/lltextbase.cpp b/indra/llui/lltextbase.cpp
index 9b125a85b91..b9c3417dc87 100755
--- a/indra/llui/lltextbase.cpp
+++ b/indra/llui/lltextbase.cpp
@@ -44,6 +44,8 @@
 #include "llwindow.h"
 #include <boost/bind.hpp>
 
+#include "uriparser/Uri.h"
+
 const F32	CURSOR_FLASH_DELAY = 1.0f;  // in seconds
 const S32	CURSOR_THICKNESS = 2;
 const F32	TRIPLE_CLICK_INTERVAL = 0.3f;	// delay between double and triple click.
@@ -2019,6 +2021,41 @@ static LLUIImagePtr image_from_icon_name(const std::string& icon_name)
 
 static LLTrace::BlockTimerStatHandle FTM_PARSE_HTML("Parse HTML");
 
+S32 LLTextBase::normalizeUri(std::string& uri_string)
+{
+	UriParserStateA state;
+	UriUriA uri;
+	state.uri = &uri;
+
+	S32 res = uriParseUriA(&state, uri_string.c_str());
+
+	if (!res)	
+	{
+		res = uriNormalizeSyntaxExA(&uri, URI_NORMALIZE_SCHEME | URI_NORMALIZE_HOST);
+
+		if (!res)
+		{
+			S32 chars_required;
+			res = uriToStringCharsRequiredA(&uri, &chars_required);
+
+			if (!res)
+			{
+				chars_required++;
+				std::vector<char> label_buf(chars_required);
+				res = uriToStringA(&label_buf[0], &uri, chars_required, NULL);
+
+				if (!res)
+				{
+					uri_string = &label_buf[0];
+				}
+			}
+		}
+	}
+
+	uriFreeUriMembersA(&uri);
+	return res;
+}
+
 void LLTextBase::appendTextImpl(const std::string &new_text, const LLStyle::Params& input_params)
 {
 	LLStyle::Params style_params(input_params);
@@ -2055,8 +2092,12 @@ void LLTextBase::appendTextImpl(const std::string &new_text, const LLStyle::Para
 				std::string subtext=text.substr(0,start);
 				appendAndHighlightText(subtext, part, style_params); 
 			}
+
+			std::string label = match.getLabel();
+			normalizeUri(label);
+
 			// output the styled Url
-			appendAndHighlightTextImpl(match.getLabel(), part, link_params, match.underlineOnHoverOnly());
+			appendAndHighlightTextImpl(label, part, link_params, match.underlineOnHoverOnly());
 			
 			// set the tooltip for the Url label
 			if (! match.getTooltip().empty())
diff --git a/indra/llui/lltextbase.h b/indra/llui/lltextbase.h
index 738b4d5b8ee..dfc10923f3f 100755
--- a/indra/llui/lltextbase.h
+++ b/indra/llui/lltextbase.h
@@ -590,6 +590,7 @@ class LLTextBase
 	
 	void							appendTextImpl(const std::string &new_text, const LLStyle::Params& input_params = LLStyle::Params());
 	void							appendAndHighlightTextImpl(const std::string &new_text, S32 highlight_part, const LLStyle::Params& style_params, bool underline_on_hover_only = false);
+	S32 normalizeUri(std::string& uri);
 	
 
 protected:
-- 
GitLab