From ff1744239ce29438e645f3cc64855da6183b6837 Mon Sep 17 00:00:00 2001
From: "Garcia-Hernandez, Ruben Jesus (rgarcia)" <garcia@lrz.de>
Date: Tue, 18 Apr 2017 12:56:34 +0200
Subject: [PATCH] Add http authentification using token. Explicitly disable
 pagination Add missing files to openvr project

---
 NOMADVRLib/ConfigFile.cpp                  | 15 ++++--
 NOMADVRLib/atoms.cpp                       | 56 +++++++++++++++++-----
 NOMADVRLib/atoms.hpp                       |  6 +--
 OpenVR/TimestepData/hellovr_opengl.vcxproj |  1 +
 4 files changed, 60 insertions(+), 18 deletions(-)

diff --git a/NOMADVRLib/ConfigFile.cpp b/NOMADVRLib/ConfigFile.cpp
index 7e3c1eb..596eada 100644
--- a/NOMADVRLib/ConfigFile.cpp
+++ b/NOMADVRLib/ConfigFile.cpp
@@ -52,6 +52,7 @@ const char * loadConfigFileErrors[] =
 	"No basis vectors, but repetitions requested", //-13
 	"Error loading config file",// -14
 	"Error reading atomglyph", //-15
+	"Error reading token", //-16
 	"Error loading xyz file, add 100 to see the error",//<-100
 	"Error loading cube file, add 100 to see the error",//<-200
 	"Error loading json file, add 200 to see the error",//<-300
@@ -67,9 +68,9 @@ else
 
 int readString(FILE *f, char *s)
 {
-	char s2[100];
+	char s2[2048];
 	int r, c;
-	r = fscanf(f, "%99s", s2);
+	r = fscanf(f, "%2047s", s2);
 	if (r!=1)
 		return -1;
 	if (s2[0]!='"') {
@@ -135,6 +136,7 @@ int loadConfigFile(const char * f)
 	for (int i=0;i<3;i++)
 		userpos[i] = 0;
 	solid=0;
+	char *token=0;
 	//
 	FILE *F = fopen(f, "r");
 	if (F == 0)
@@ -312,7 +314,7 @@ int loadConfigFile(const char * f)
 			sprintf (url, "%s%s", base_url, material);
 			//rgh fixme, we know only one
 			eprintf ("load config file start, before readAtomsJsonURL");
-			e = readAtomsJsonURL (url, &numAtoms, &timesteps, &atoms, abc, &clonedAtoms);
+			e = readAtomsJsonURL (url, &numAtoms, &timesteps, &atoms, abc, &clonedAtoms, token);
 			eprintf ("load config file start, after readAtomsJsonURL");
 			if (e<0)
 				return e-300;
@@ -348,6 +350,13 @@ int loadConfigFile(const char * f)
 				solid=new Solid(Solid::Type::Tetrahedron);
 			else
 				return -15;
+		} else if (!strcmp (s, "token")) {
+			if (token)
+				delete (token);
+			token=new char [2048];
+			r=readString (F, token);
+			if (r!=0)
+				return -16;
 		} else if (!strcmp (s, "\x0d")) { //discard windows newline (problem in Sebastian Kokott's phone (?!)
 			continue;
 		} else {
diff --git a/NOMADVRLib/atoms.cpp b/NOMADVRLib/atoms.cpp
index cfdb110..2adca79 100644
--- a/NOMADVRLib/atoms.cpp
+++ b/NOMADVRLib/atoms.cpp
@@ -312,7 +312,7 @@ const char * readAtomsJsonErrors[] = {
 };
 
 int readAtomsJsonURL (const char *const f, int **numatoms, int *timesteps, float ***pos, float abc[3][3],
-					  std::vector<float>** clonedAtoms)
+					  std::vector<float>** clonedAtoms, const char *const token)
 {
 eprintf ("readAtomsJsonURL start");
 
@@ -327,12 +327,12 @@ if (r==1)
 if (r<2) { 
 #if defined(WIN32)
 	//possibly https or other unsupported protocol, fall back to wget 
-	return readAtomsJsonURLwget (f, numatoms, timesteps, pos, abc, clonedAtoms);
+	return readAtomsJsonURLwget (f, numatoms, timesteps, pos, abc, clonedAtoms, token);
 #else
 	return -3;
 #endif
 }
-sprintf (url, "%s%s", page, "/cells");
+sprintf (url, "%s%s", page, "/cells?pagination=off");
 sprintf (file, "%s%s", TMPDIR, "material_cells.json");
 out=fopen(file , "w");
 if (out==nullptr) {
@@ -341,13 +341,27 @@ if (out==nullptr) {
 }
 happyhttp::Connection conn( host, port );
 conn.setcallbacks( nullptr, OnData, nullptr, 0 );
-conn.request( "GET", url, 0, 0,0 );
+
+//https://github.com/Zintinio/HappyHTTP/issues/9
+
+const char*headers[3];
+char base64[2048];
+if (token) {
+	headers[0]="Authorization";
+	sprintf (base64, "Basic %s", token);
+	headers[1]=base64;
+	headers[2]=0;
+	conn.request( "GET", url, headers, 0,0 );
+} else {
+	conn.request( "GET", url, 0, 0,0 );
+}
+
 
 while( conn.outstanding() )
 	conn.pump();
 fclose(out);
 conn.close();
-sprintf (url, "%s%s", page, "/elements");
+sprintf (url, "%s%s", page, "/elements?pagination=off");
 sprintf (file, "%s%s", TMPDIR, "material_elements.json");
 out=fopen(file , "w");
 
@@ -355,7 +369,13 @@ if (out==nullptr) {
 	eprintf ("Could not open file for writing: %s", file);
 	return -1;
 }
-conn.request( "GET", url, 0, 0,0 );
+
+if (token) {
+	conn.request( "GET", url, headers, 0,0 );
+} else {
+	conn.request( "GET", url, 0, 0,0 );
+}
+
 while( conn.outstanding() )
 	conn.pump();
 } catch (const happyhttp::Wobbly& w) {
@@ -366,6 +386,10 @@ while( conn.outstanding() )
 	eprintf( "error %s\n", w.what());
 #endif
 	fclose(out);
+
+#if defined(WIN32)
+	return readAtomsJsonURLwget (f, numatoms, timesteps, pos, abc, clonedAtoms, token);
+#endif
 	return -3;
 }
 fclose(out);
@@ -376,23 +400,31 @@ fclose(out);
 char file [2048];
 sprintf (file, "%s%s", TMPDIR, "material");
 eprintf ("readAtomsJsonURL before return");
-return readAtomsJson (file, numatoms, timesteps, pos, abc, clonedAtoms);
+return readAtomsJson (file, numatoms, timesteps, pos, abc, clonedAtoms, token);
 }
 
 #if defined(WIN32)
+//base64 encoded token
 int readAtomsJsonURLwget (const char *const f, int **numatoms, int *timesteps, float ***pos, float abc[3][3],
-						  std::vector<float>** clonedAtoms)
+						  std::vector<float>** clonedAtoms, const char *const token)
 {
 char cmd[2048];
 int ret;
-sprintf (cmd, "wget %s/cells -O material_cells.json", f);
+if (token)
+	sprintf (cmd, "wget  --header \"Authorization:Basic %s\" %s/cells?pagination=off -O material_cells.json", token, f);
+else
+	sprintf (cmd, "wget %s/cells?pagination=off -O material_cells.json", f);
 ret=system(cmd);
 if (ret!=0) 
 	return (-3);
-sprintf (cmd, "wget %s/elements -O material_elements.json", f);
+if (token)
+	sprintf (cmd, "wget --header \"Authorization:Basic %s\" %s/elements?pagination=off -O material_elements.json", token, f);
+else
+	sprintf (cmd, "wget %s/elements?pagination=off -O material_elements.json", f);
 ret=system(cmd);
+if (ret!=0) 
 	return(-3);
-return readAtomsJson ("material", numatoms, timesteps, pos, abc, clonedAtoms);
+return readAtomsJson ("material", numatoms, timesteps, pos, abc, clonedAtoms, token);
 }
 #endif
 
@@ -410,7 +442,7 @@ void add (std::vector<float> *v, float x, float y, float z, float a)
 }
 
 int readAtomsJson (const char *const f, int **numatoms, int *timesteps, float ***pos, float abc[3][3], 
-				   std::vector<float>** clonedAtoms)
+				   std::vector<float>** clonedAtoms, const char *const token)
 {
 	eprintf ("readAtomsJson start");
 	char file[512];
diff --git a/NOMADVRLib/atoms.hpp b/NOMADVRLib/atoms.hpp
index f97220e..cb3ec3c 100644
--- a/NOMADVRLib/atoms.hpp
+++ b/NOMADVRLib/atoms.hpp
@@ -15,10 +15,10 @@ extern const char * TMPDIR;
 
 int readAtomsXYZ(const char *const file, int **numatoms, int *timesteps, float ***pos);
 int readAtomsCube(const char *const file, int **numatoms, int *timesteps, float ***pos);
-int readAtomsJson (const char *const file, int **numatoms, int *timesteps, float ***pos, float abc[3][3],  std::vector<float>** clonedAtoms);
-int readAtomsJsonURL (const char *const f, int **numatoms, int *timesteps, float ***pos, float abc[3][3],  std::vector<float>** clonedAtoms);
+int readAtomsJson (const char *const file, int **numatoms, int *timesteps, float ***pos, float abc[3][3],  std::vector<float>** clonedAtoms, const char *const token=0);
+int readAtomsJsonURL (const char *const f, int **numatoms, int *timesteps, float ***pos, float abc[3][3],  std::vector<float>** clonedAtoms, const char *const token=0);
 #if defined(WIN32)
-int readAtomsJsonURLwget (const char *const f, int **numatoms, int *timesteps, float ***pos, float abc[3][3],  std::vector<float>** clonedAtoms);
+int readAtomsJsonURLwget (const char *const f, int **numatoms, int *timesteps, float ***pos, float abc[3][3],  std::vector<float>** clonedAtoms, const char *const token=0);
 #endif
 const float MISSINGRADIUS=0.2f;
 const float MISSINGR=1.f;
diff --git a/OpenVR/TimestepData/hellovr_opengl.vcxproj b/OpenVR/TimestepData/hellovr_opengl.vcxproj
index 2d4c116..ad7f7e0 100644
--- a/OpenVR/TimestepData/hellovr_opengl.vcxproj
+++ b/OpenVR/TimestepData/hellovr_opengl.vcxproj
@@ -178,6 +178,7 @@
     <ClCompile Include="NOMADVRLib\atomsGL.cpp" />
     <ClCompile Include="NOMADVRLib\CompileGLShader.cpp" />
     <ClCompile Include="NOMADVRLib\ConfigFile.cpp" />
+    <ClCompile Include="NOMADVRLib\IsoShaders.cpp" />
     <ClCompile Include="NOMADVRLib\IsosurfacesGL.cpp" />
     <ClCompile Include="NOMADVRLib\polyhedron.cpp" />
     <ClCompile Include="NOMADVRLib\TessShaders.cpp" />
-- 
GitLab