ecere/net/HTTPFile; extras: Added https support; Promoted SSLSocket to ecere
authorJerome St-Louis <jerome@ecere.com>
Mon, 30 Sep 2013 07:50:46 +0000 (03:50 -0400)
committerJerome St-Louis <jerome@ecere.com>
Mon, 30 Sep 2013 08:25:02 +0000 (04:25 -0400)
- Not built by default from tarball Makefile; built by default in ecere.epj configurations

compiler/bootstrap/ecere/bootstrap/String.c
default.cf
ecere/ecere.epj
ecere/src/com/String.ec
ecere/src/net/HTTPFile.ec
ecere/src/net/SSLSocket.ec [moved from extras/SSLSocket.ec with 96% similarity]
ecere/src/sys/File.ec
extras/html/HTMLView.ec

index 2c67b3b..1d2a221 100644 (file)
@@ -446,6 +446,8 @@ return 0x0;
 
 extern char *  strstr(const char * , const char * );
 
+extern void *  memcpy(void * , const void * , size_t size);
+
 extern int toupper(int);
 
 extern char *  strcat(char * , const char * );
@@ -458,20 +460,20 @@ unsigned int modified = 0x0;
 
 if(addedPath)
 {
-char fileName[797] = "", archiveName[797] = "", * file;
+char fileName[797] = "", archiveName[797] = "", * file = (((void *)0));
 int c = 0;
 unsigned int isURL = 0x0;
+unsigned int isArchive = __ecereNameSpace__ecere__sys__SplitArchivePath(string, archiveName, &file);
 char * urlFileName;
+char * protocolSymbol;
 
-if(__ecereNameSpace__ecere__sys__SplitArchivePath(string, archiveName, &file))
-strcpy(fileName, file);
-else
+strcpy(fileName, isArchive ? file : string);
+if(!isArchive)
 {
-strcpy(fileName, string);
-}
-if(strstr(string, "http://") == string)
+protocolSymbol = (fileName[0] && fileName[0] != '.' && fileName[0] != '/' && fileName[0] != '\\' && fileName[1] != ':') ? strstr(fileName, "://") : (((void *)0));
+if(protocolSymbol)
 {
-char * slash = strstr(fileName + 7, "/");
+char * slash = strstr(protocolSymbol + 3, "/");
 
 isURL = 0x1;
 if(slash)
@@ -479,11 +481,16 @@ urlFileName = slash;
 else
 urlFileName = fileName + strlen(fileName);
 }
-if(strstr(addedPath, "http://") == addedPath)
+}
+protocolSymbol = (addedPath[0] && addedPath[0] != '.' && addedPath[0] != '/' && addedPath[0] != '\\' && addedPath[1] != ':') ? strstr(addedPath, "://") : (((void *)0));
+if(protocolSymbol)
 {
-strcpy(fileName, "http://");
+int len = protocolSymbol - addedPath + 3;
+
+memcpy(fileName, addedPath, len);
+fileName[len] = (char)0;
 isURL = 0x1;
-c = 7;
+c = len;
 }
 else if(__ecereNameSpace__ecere__com__GetRuntimePlatform() == 1)
 {
@@ -660,20 +667,20 @@ unsigned int modified = 0x0;
 
 if(addedPath)
 {
-char fileName[797] = "", archiveName[797] = "", * file;
+char fileName[797] = "", archiveName[797] = "", * file = (((void *)0));
 int c = 0;
 unsigned int isURL = 0x0;
+unsigned int isArchive = __ecereNameSpace__ecere__sys__SplitArchivePath(string, archiveName, &file);
 char * urlFileName;
+char * protocolSymbol;
 
-if(__ecereNameSpace__ecere__sys__SplitArchivePath(string, archiveName, &file))
-strcpy(fileName, file);
-else
+strcpy(fileName, isArchive ? file : string);
+if(!isArchive)
 {
-strcpy(fileName, string);
-}
-if(strstr(string, "http://") == string)
+protocolSymbol = (fileName[0] && fileName[0] != '.' && fileName[0] != '/' && fileName[0] != '\\' && fileName[1] != ':') ? strstr(fileName, "://") : (((void *)0));
+if(protocolSymbol)
 {
-char * slash = strstr(fileName + 7, "/");
+char * slash = strstr(protocolSymbol + 3, "/");
 
 isURL = 0x1;
 if(slash)
@@ -681,11 +688,16 @@ urlFileName = slash;
 else
 urlFileName = fileName + strlen(fileName);
 }
-if(strstr(addedPath, "http://") == addedPath)
+}
+protocolSymbol = (addedPath[0] && addedPath[0] != '.' && addedPath[0] != '/' && addedPath[0] != '\\' && addedPath[1] != ':') ? strstr(addedPath, "://") : (((void *)0));
+if(protocolSymbol)
 {
-strcpy(fileName, "http://");
+int len = protocolSymbol - addedPath + 3;
+
+memcpy(fileName, addedPath, len);
+fileName[len] = (char)0;
 isURL = 0x1;
-c = 7;
+c = len;
 }
 else if(runtimePlatform == 1)
 {
@@ -1237,8 +1249,6 @@ string[c] = ch;
 string[c] = (char)0;
 }
 
-extern void *  memcpy(void * , const void * , size_t size);
-
 char * __ecereNameSpace__ecere__sys__CopyString(char * string)
 {
 if(string)
index 38cda09..833ee11 100644 (file)
@@ -83,3 +83,4 @@ ifdef DISABLE_GL
 CFLAGS += -DECERE_NOGL
 endif
 
+CFLAGS += -DECERE_NOSSL
index c0a7157..95842f1 100644 (file)
@@ -105,7 +105,9 @@ from wherever you obtained them.
                "X11",
                "Xext",
                "Xrender",
-               "GL"
+               "GL",
+               "ssl",
+               "crypto"
             ],
             "LibraryDirs" : [
                "/usr/X11R6/lib"
@@ -139,7 +141,9 @@ from wherever you obtained them.
                "shell32",
                "winspool",
                "imm32",
-               "ungif"
+               "ungif",
+               "eay32",
+               "ssleay32"
             ],
             "LibraryDirs" : [
                "../deps/zlib/obj/release.$(PLATFORM)$(COMPILER_SUFFIX)",
@@ -173,7 +177,9 @@ from wherever you obtained them.
                "X11",
                "Xext",
                "Xrender",
-               "GL"
+               "GL",
+               "ssl",
+               "crypto"
             ],
             "LibraryDirs" : [
                "$(SYSROOT)/usr/X11/lib",
@@ -1983,7 +1989,8 @@ from wherever you obtained them.
                      ]
                   },
                   "Service.ec",
-                  "Socket.ec"
+                  "Socket.ec",
+                  "SSLSocket.ec"
                ],
                "Configurations" : [
                   {
index fcd0c01..a7314b2 100644 (file)
@@ -193,32 +193,37 @@ public char * PathCatSlash(char * string, char * addedPath)
    bool modified = false;
    if(addedPath)
    {
-      char fileName[MAX_LOCATION] = "", archiveName[MAX_LOCATION] = "", * file;
+      char fileName[MAX_LOCATION] = "", archiveName[MAX_LOCATION] = "", * file = null;
       int c = 0;
       bool isURL = false;
+      bool isArchive = SplitArchivePath(string, archiveName, &file);
       char * urlFileName;
+      char * protocolSymbol;
 
-      if(SplitArchivePath(string, archiveName, &file))
-         strcpy(fileName, file);
-      else
-      {
-         strcpy(fileName, string);
-      }
+      strcpy(fileName, isArchive ? file : string);
 
-      if(strstr(string, "http://") == string)
+      if(!isArchive) // TODO: Support for PathCat'ing .. outside of archive
       {
-         char * slash = strstr(fileName + 7, "/");
-         isURL = true;
-         if(slash)
-            urlFileName = slash;
-         else
-            urlFileName = fileName + strlen(fileName);
+         protocolSymbol = (fileName[0] && fileName[0] != '.' && fileName[0] != '/' && fileName[0] != '\\' && fileName[1] != ':') ? strstr(fileName, "://") : null;
+         if(protocolSymbol)
+         {
+            char * slash = strstr(protocolSymbol + 3, "/");
+            isURL = true;
+            if(slash)
+               urlFileName = slash;
+            else
+               urlFileName = fileName + strlen(fileName);
+         }
       }
-      if(strstr(addedPath, "http://") == addedPath)
+
+      protocolSymbol = (addedPath[0] && addedPath[0] != '.' && addedPath[0] != '/' && addedPath[0] != '\\' && addedPath[1] != ':') ? strstr(addedPath, "://") : null;
+      if(protocolSymbol)
       {
-         strcpy(fileName, "http://");
+         int len = protocolSymbol - addedPath + 3;
+         memcpy(fileName, addedPath, len);
+         fileName[len] = 0;
          isURL = true;
-         c = 7;
+         c = len;
       }
       else if(GetRuntimePlatform() == win32)
       {
@@ -407,32 +412,37 @@ public char * PathCat(char * string, char * addedPath)
    bool modified = false;
    if(addedPath)
    {
-      char fileName[MAX_LOCATION] = "", archiveName[MAX_LOCATION] = "", * file;
+      char fileName[MAX_LOCATION] = "", archiveName[MAX_LOCATION] = "", * file = null;
       int c = 0;
       bool isURL = false;
+      bool isArchive = SplitArchivePath(string, archiveName, &file);
       char * urlFileName;
+      char * protocolSymbol;
 
-      if(SplitArchivePath(string, archiveName, &file))
-         strcpy(fileName, file);
-      else
-      {
-         strcpy(fileName, string);
-      }
+      strcpy(fileName, isArchive ? file : string);
 
-      if(strstr(string, "http://") == string)
+      if(!isArchive) // TODO: Support for PathCat'ing .. outside of archive
       {
-         char * slash = strstr(fileName + 7, "/");
-         isURL = true;
-         if(slash)
-            urlFileName = slash;
-         else
-            urlFileName = fileName + strlen(fileName);
+         protocolSymbol = (fileName[0] && fileName[0] != '.' && fileName[0] != '/' && fileName[0] != '\\' && fileName[1] != ':') ? strstr(fileName, "://") : null;
+         if(protocolSymbol)
+         {
+            char * slash = strstr(protocolSymbol + 3, "/");
+            isURL = true;
+            if(slash)
+               urlFileName = slash;
+            else
+               urlFileName = fileName + strlen(fileName);
+         }
       }
-      if(strstr(addedPath, "http://") == addedPath)
+
+      protocolSymbol = (addedPath[0] && addedPath[0] != '.' && addedPath[0] != '/' && addedPath[0] != '\\' && addedPath[1] != ':') ? strstr(addedPath, "://") : null;
+      if(protocolSymbol)
       {
-         strcpy(fileName, "http://");
+         int len = protocolSymbol - addedPath + 3;
+         memcpy(fileName, addedPath, len);
+         fileName[len] = 0;
          isURL = true;
-         c = 7;
+         c = len;
       }
       else if(runtimePlatform == win32)
       {
index 9deeb20..6ba2d95 100644 (file)
@@ -4,6 +4,9 @@
 
 import "List"
 import "network"
+#ifndef ECERE_NOSSL
+import "SSLSocket"
+#endif
 
 class ConnectionsHolder
 {
@@ -84,12 +87,17 @@ static char * GetString(char * string, char * what, int count)
    return string + c;
 }
 
+#ifdef ECERE_NOSSL
 private class HTTPConnection : Socket
+#else
+private class HTTPConnection : SSLSocket
+#endif
 {
    class_no_expansion;
    char * server;
    int port;
    HTTPFile file;
+   bool secure;
 
    processAlone = true;
 
@@ -332,10 +340,11 @@ private:
    bool RetrieveHead(char * name, char * referer, char * relocation, bool askBody)
    {
       bool result = false;
-      String http;
+      String http, https;
       if(!this || !name) return false;
       http = strstr(name, "http://");
       if(http != name) http = null;
+      https = http ? null : strstr(name, "https://"); if(https && https != name) https = null;
 
       askedBody = askBody;
 
@@ -343,14 +352,14 @@ private:
       delete contentType;
       delete contentDisposition;
       // ::PrintLn("Opening ", name, " ", (uint64)this, " in thread ", GetCurrentThreadID(), "\n");
-      if(this && http)
+      if(this && (http || https))
       {
          char server[1024];
          char msg[1024];
          int len;
-         char * serverStart = ecere::net::GetString(name, "http://", 0);
+         char * serverStart = http ? ecere::net::GetString(name, "http://", 0) : ecere::net::GetString(name, "https://", 0);
          char * fileName = strstr(serverStart, "/");
-         int port = 80;
+         int port = https ? 443 : 80;
          char * colon;
          bool reuse = false;
          HTTPConnection connection = null;
@@ -367,7 +376,7 @@ private:
 
          if(relocation && !fileName && name[strlen(name)-1] != '/')
          {
-            strcpy(relocation, "http://");
+            strcpy(relocation, http ? "http://" : "https://");
             strcat(relocation, server);
             strcat(relocation, "/");
          }
@@ -407,7 +416,7 @@ private:
                connection = null;
                for(c : holder.connections)
                {
-                  if(!strcmpi(c.server, server) && c.port == port)
+                  if(!strcmpi(c.server, server) && c.port == port && c.secure == (https ? true : false))
                   {
                      if(!c.file && c.connected)
                      {
@@ -439,7 +448,12 @@ private:
          if(!connection)
          {
             char ipAddress[1024];
-            connection = HTTPConnection { };
+            connection = HTTPConnection
+            {
+#ifndef ECERE_NOSSL
+               autoEstablish = https ? true : false
+#endif
+            };
             incref connection;      // HTTPFile reference on success
             
             connection.file = this;
@@ -457,6 +471,7 @@ private:
 
                   connection.server = CopyString(server);
                   connection.port = port;
+                  connection.secure = https ? true : false;
 
                   //::PrintLn("Got connectionsMutex for ", name, " ", (uint64)this, " in thread ", GetCurrentThreadID());
                   holder.connections.Add(connection);
similarity index 96%
rename from extras/SSLSocket.ec
rename to ecere/src/net/SSLSocket.ec
index 1fab2e2..3efc867 100644 (file)
 #undef uint
 #undef set
 
-import "ecere"
+#ifdef BUILDING_ECERE_COM
+import "Socket"
+#else
+public import "ecere"
+#endif
 
 /*
 static char * pass = "password";
@@ -24,7 +28,7 @@ static int password_cb(char *buf) //,int num, int rwflag,void *userdata)
 }
 */
 
-class SSLSocket : Socket
+public class SSLSocket : Socket
 {
    SSL_CTX *ctx;
    SSL *ssl;
index 486a3b2..87c50e2 100644 (file)
@@ -960,7 +960,7 @@ public File FileOpen(char * fileName, FileOpenMode mode)
          result = EARFileSystem::Open(archiveName, archiveFile, mode);
       }
 #if !defined(ECERE_VANILLA) && !defined(ECERE_NONET)
-      else if(strstr(fileName, "http://") == fileName)
+      else if(strstr(fileName, "http://") == fileName || strstr(fileName, "https://"))
       {
          result = FileOpenURL(fileName);
       }
index 5571895..da136c6 100644 (file)
@@ -556,7 +556,7 @@ class HTMLView : Window
             block.bitmap = entry.bitmap;
          else
          {
-            if(path && strstr(path, "http://") == path)
+            if(path && (strstr(path, "http://") == path || strstr(path, "https://") == path))
             {
                RequestLink request;
                objectsMutex.Wait();
@@ -769,7 +769,7 @@ class HTMLView : Window
 
       // PrintLn("\n\n\nOpening new location: ", location, "\n");
 
-      if(strstr(location, "http://") != location)
+      if(strstr(location, "http://") != location && strstr(location, "https://") != location)
       {
          if(!FileExists(location))
          {