getURLheader added with gateway, source, help and tests 08/16808/2
Kartik Gupta [Wed, 8 Jul 2015 09:32:43 +0000 (14:32 +0530)]
Change-Id: I161e6857e52856cbe892d70a94fa9ffeed15b340

14 files changed:
scilab/modules/fileio/Makefile.am
scilab/modules/fileio/Makefile.in
scilab/modules/fileio/fileio.vcxproj
scilab/modules/fileio/fileio.vcxproj.filters
scilab/modules/fileio/help/en_US/getURLheader.xml [new file with mode: 0644]
scilab/modules/fileio/includes/gw_fileio.h
scilab/modules/fileio/sci_gateway/c/gw_fileio.c
scilab/modules/fileio/sci_gateway/c/sci_getURL.c
scilab/modules/fileio/sci_gateway/c/sci_getURLheader.c [new file with mode: 0644]
scilab/modules/fileio/sci_gateway/fileio_gateway.xml
scilab/modules/fileio/src/c/dlManager.c
scilab/modules/fileio/src/c/dlManager.h
scilab/modules/fileio/tests/unit_tests/getURLheader.dia.ref [new file with mode: 0644]
scilab/modules/fileio/tests/unit_tests/getURLheader.tst [new file with mode: 0644]

index 012dbf7..7c2839c 100644 (file)
@@ -113,7 +113,8 @@ sci_gateway/c/sci_getURL.c \
 sci_gateway/c/sci_splitURL.c \
 sci_gateway/c/sci_archive_list.c \
 sci_gateway/c/sci_archive_extract.c \
-sci_gateway/c/sci_archive_compress.c
+sci_gateway/c/sci_archive_compress.c \
+sci_gateway/c/sci_getURLheader.c
 
 libscifileio_la_CPPFLAGS = -I$(srcdir)/includes/ \
                                -I$(srcdir)/src/c/ \
index 7d5c927..3efbdd5 100644 (file)
@@ -286,7 +286,8 @@ am__objects_2 = sci_gateway/c/libscifileio_la-gw_fileio.lo \
        sci_gateway/c/libscifileio_la-sci_splitURL.lo \
        sci_gateway/c/libscifileio_la-sci_archive_list.lo \
        sci_gateway/c/libscifileio_la-sci_archive_extract.lo \
-       sci_gateway/c/libscifileio_la-sci_archive_compress.lo
+       sci_gateway/c/libscifileio_la-sci_archive_compress.lo \
+       sci_gateway/c/libscifileio_la-sci_getURLheader.lo
 am_libscifileio_la_OBJECTS = $(am__objects_2)
 libscifileio_la_OBJECTS = $(am_libscifileio_la_OBJECTS)
 @MAINTAINER_MODE_FALSE@am_libscifileio_la_rpath =
@@ -743,7 +744,8 @@ sci_gateway/c/sci_getURL.c \
 sci_gateway/c/sci_splitURL.c \
 sci_gateway/c/sci_archive_list.c \
 sci_gateway/c/sci_archive_extract.c \
-sci_gateway/c/sci_archive_compress.c
+sci_gateway/c/sci_archive_compress.c \
+sci_gateway/c/sci_getURLheader.c
 
 libscifileio_la_CPPFLAGS = -I$(srcdir)/includes/ \
                                -I$(srcdir)/src/c/ \
@@ -1229,6 +1231,9 @@ sci_gateway/c/libscifileio_la-sci_archive_extract.lo:  \
 sci_gateway/c/libscifileio_la-sci_archive_compress.lo:  \
        sci_gateway/c/$(am__dirstamp) \
        sci_gateway/c/$(DEPDIR)/$(am__dirstamp)
+sci_gateway/c/libscifileio_la-sci_getURLheader.lo:  \
+       sci_gateway/c/$(am__dirstamp) \
+       sci_gateway/c/$(DEPDIR)/$(am__dirstamp)
 
 libscifileio.la: $(libscifileio_la_OBJECTS) $(libscifileio_la_DEPENDENCIES) $(EXTRA_libscifileio_la_DEPENDENCIES) 
        $(AM_V_CCLD)$(LINK) $(am_libscifileio_la_rpath) $(libscifileio_la_OBJECTS) $(libscifileio_la_LIBADD) $(LIBS)
@@ -1261,6 +1266,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscifileio_la-sci_fscanfMat.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscifileio_la-sci_fullpath.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscifileio_la-sci_getURL.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscifileio_la-sci_getURLheader.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscifileio_la-sci_get_absolute_file_path.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscifileio_la-sci_getdrives.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscifileio_la-sci_getlongpathname.Plo@am__quote@
@@ -2132,6 +2138,13 @@ sci_gateway/c/libscifileio_la-sci_archive_compress.lo: sci_gateway/c/sci_archive
 @AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCC_FALSE@  $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscifileio_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o sci_gateway/c/libscifileio_la-sci_archive_compress.lo `test -f 'sci_gateway/c/sci_archive_compress.c' || echo '$(srcdir)/'`sci_gateway/c/sci_archive_compress.c
 
+sci_gateway/c/libscifileio_la-sci_getURLheader.lo: sci_gateway/c/sci_getURLheader.c
+@am__fastdepCC_TRUE@   $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscifileio_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT sci_gateway/c/libscifileio_la-sci_getURLheader.lo -MD -MP -MF sci_gateway/c/$(DEPDIR)/libscifileio_la-sci_getURLheader.Tpo -c -o sci_gateway/c/libscifileio_la-sci_getURLheader.lo `test -f 'sci_gateway/c/sci_getURLheader.c' || echo '$(srcdir)/'`sci_gateway/c/sci_getURLheader.c
+@am__fastdepCC_TRUE@   $(AM_V_at)$(am__mv) sci_gateway/c/$(DEPDIR)/libscifileio_la-sci_getURLheader.Tpo sci_gateway/c/$(DEPDIR)/libscifileio_la-sci_getURLheader.Plo
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      $(AM_V_CC)source='sci_gateway/c/sci_getURLheader.c' object='sci_gateway/c/libscifileio_la-sci_getURLheader.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@  $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscifileio_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o sci_gateway/c/libscifileio_la-sci_getURLheader.lo `test -f 'sci_gateway/c/sci_getURLheader.c' || echo '$(srcdir)/'`sci_gateway/c/sci_getURLheader.c
+
 mostlyclean-libtool:
        -rm -f *.lo
 
index df9f5dd..6c86a58 100644 (file)
@@ -212,6 +212,7 @@ lib /DEF:"$(ProjectDir)Preferences_Import.def" /SUBSYSTEM:WINDOWS /MACHINE:$(Pla
     <ClCompile Include="sci_gateway\c\sci_archive_extract.c" />
     <ClCompile Include="sci_gateway\c\sci_archive_list.c" />
     <ClCompile Include="sci_gateway\c\sci_getURL.c" />
+    <ClCompile Include="sci_gateway\c\sci_getURLheader.c" />
     <ClCompile Include="sci_gateway\c\sci_splitURL.c" />
     <ClCompile Include="sci_gateway\c\sci_tempname.c" />
     <ClCompile Include="src\c\addfile.c" />
index 80750b6..e9b3205 100644 (file)
     <ClCompile Include="sci_gateway\c\sci_getURL.c">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="sci_gateway\c\sci_getURLheader.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
     <ClCompile Include="sci_gateway\c\sci_splitURL.c">
       <Filter>Source Files</Filter>
     </ClCompile>
diff --git a/scilab/modules/fileio/help/en_US/getURLheader.xml b/scilab/modules/fileio/help/en_US/getURLheader.xml
new file mode 100644 (file)
index 0000000..d31abd2
--- /dev/null
@@ -0,0 +1,163 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) - S/E - Sylvestre Ledru
+ * 
+ * This file must be used under the terms of the CeCILL.
+ * This source file is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution.  The terms
+ * are also available at
+ * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
+ *
+ -->
+<refentry xml:id="getURLheader" xml:lang="en"
+          xmlns="http://docbook.org/ns/docbook"
+          xmlns:xlink="http://www.w3.org/1999/xlink"
+          xmlns:svg="http://www.w3.org/2000/svg"
+          xmlns:ns3="http://www.w3.org/1999/xhtml"
+          xmlns:mml="http://www.w3.org/1998/Math/MathML"
+          xmlns:scilab="http://www.scilab.org"
+          xmlns:db="http://docbook.org/ns/docbook">
+    
+    <refnamediv>
+        <refname>getURLheader</refname>
+        
+        <refpurpose>Download an URL (HTTP, HTTPS, FTP...) and also stores the recieved header</refpurpose>
+    </refnamediv>
+    
+    <refsynopsisdiv>
+        <title>Calling Sequence</title>
+        
+        <synopsis>filename = getURLheader(URL);
+            [filename, [content]] = getURLheader(URL [, targetDir [, username [, password]]]]);
+            [filename, [content]] = getURLheader(URL [, targetFile [, username [, password]]]]);
+        </synopsis>
+    </refsynopsisdiv>
+    
+    <refsection>
+        <title>Arguments</title>
+        <variablelist>
+            <varlistentry>
+                <term>URL</term>
+                <listitem>
+                    <para>String: An URL. Supported and tested: HTTP, HTTPS, FTP (IPv4 and IPv6)</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>targetDir</term>
+                <listitem>
+                    <para>String optional parameter: In which directory the file will be stored (current directory if empty)</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>targetFile</term>
+                <listitem>
+                    <para>String optional parameter: Where (and under which name) the file will be stored (current directory if empty)</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>username</term>
+                <listitem>
+                    <para>String optional parameter: Username for the websites which require authentication</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>password</term>
+                <listitem>
+                    <para>String optional parameter: Password for the websites which require authentication</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>filename</term>
+                <listitem>
+                    <para>String: The path to the downloaded file</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>content</term>
+                <listitem>
+                    <para>String: The content to the downloaded file</para>
+                </listitem>
+            </varlistentry>
+        </variablelist>
+    </refsection>
+    
+    <refsection>
+        <title>Description</title>
+        
+        <para>Download a file from an URL</para>
+        
+        <para>
+            getURLheader will download a file from a remote URL. By default, getURL will use the same name as provided in the URL. However, under some circumstances (for example, <emphasis role="italic">http://www.scilab.org/</emphasis>), the filename is not provided by the URL itself, getURL will rename the file to <emphasis role="italic">index.html</emphasis>. The recieved headers will be stored in the filename appended by ".header". If the filename for the data is index.html then the header file name will be index.html.header .
+        </para>
+        
+        <para>content contains the content of a page pointed by an URL.</para>
+        
+        <para>
+            This function is based on the <ulink url="http://curl.haxx.se/libcurl/">curl library</ulink>.
+        </para>
+        
+        <para> Username and Password can be specified for the websites which required authentication. The authentication method used is the CURLAUTH_ANY provided by CURLOPT_HTTPAUTH parameter of the libcurl library.</para>
+        
+        <para>IPv6 (and obviously IPv4) are supported out-of-the box by getURL.</para>
+        
+        <para>Proxy configuration is enabled in this version, it can be modified in the Preferences under the rubric "Internet".</para>
+        
+        <para>
+            The <emphasis role="italic">CURLOPT_FOLLOWLOCATION</emphasis> curl option is activated to make sure the download follow the URL.
+        </para>
+        
+        <para>
+            <literal>getURL</literal> is based on libcurl. The version of this library in the official Scilab binary has been compiled with HTTP, HTTPS, FTP and FTPS support. If Scilab has been installed through a packaging system in a distribution, it is likely that <ulink url="http://curl.haxx.se/docs/features.html">other protocols</ulink> than HTTP, HTTPS and FTPS will work. However, they have not been tested enough.
+        </para>
+        
+    </refsection>
+    
+    <refsection>
+        <title>Examples</title>
+        
+        <programlisting role="example">
+            
+            // Will download the URL and name and header of the file maths_simulation
+            getURLheader("http://www.scilab.org/products/scilab/features/maths_simulation");
+            
+            // Save the Scilab homepage and header in the current directory (and rename the file)
+            getURLheader("http://www.scilab.org","scilab_homepage.html");
+            
+            // Download a Scilab example and execute it
+            myFile = getURLheader("http://fileexchange.scilab.org/toolboxes/contour3d/10.03/files/contour3d.sci",TMPDIR)
+            exec(myFile);
+            contour3d();
+            
+            // Use authentication
+            getURLheader("http://httpbin.org/basic-auth/user/passwd",TMPDIR+"/testfile.html","user","passwd")
+            
+            // Needs IPv6 connectivity
+            getURLheader("http://ipv6.google.com/intl/en_com/images/srpr/logo3w.png")
+            
+            // Retrieve the content
+            [myFile, content] = getURLheader("http://fileexchange.scilab.org/toolboxes/contour3d/10.03/files/contour3d.sci",TMPDIR)
+        </programlisting>
+    </refsection>
+    <refsection role="see also">
+        <title>See Also</title>
+        <simplelist type="inline">
+            <member>
+                <link linkend="splitURL">splitURL</link>
+            </member>
+        </simplelist>
+    </refsection>
+    
+    <refsection>
+        <title>History</title>
+        <revhistory>
+            <revision>
+                <revnumber>5.6.0</revnumber>
+                <revremark>
+                    Function <literal>getURLheader()</literal> introduced
+                </revremark>
+            </revision>
+        </revhistory>
+    </refsection>
+    
+</refentry>
index 0e2bc54..2139ee7 100644 (file)
@@ -66,6 +66,7 @@ int sci_splitURL(char *fname, unsigned long fname_len);
 int sci_archive_list(char *fname, unsigned long fname_len);
 int sci_archive_extract(char *fname, unsigned long fname_len);
 int sci_archive_compress(char *fname, unsigned long fname_len);
+int sci_getURLheader(char *fname, unsigned long fname_len);
 /*--------------------------------------------------------------------------*/
 #endif /* __GW_FILEIO_H__ */
 /*--------------------------------------------------------------------------*/
index 8759ef5..4838cb2 100644 (file)
@@ -70,7 +70,8 @@ static gw_generic_table Tab[] =
     {sci_splitURL, "splitURL"},
     {sci_archive_list, "archiveList"},
     {sci_archive_extract, "archiveExtract"},
-    {sci_archive_compress, "archiveCompress"}
+    {sci_archive_compress, "archiveCompress"},
+    {sci_getURLheader, "getURLheader"}
 };
 /*--------------------------------------------------------------------------*/
 int gw_fileio(void)
index 0fc5629..0bb5be8 100644 (file)
@@ -153,7 +153,7 @@ int sci_getURL(char *fname, int fname_len)
     }
 
     // call function
-    filePath = downloadFile(url, dest, username, password, &content);
+    filePath = downloadFile(url, dest, username, password, &content, 0);
     if (filePath)
     {
         //Return the second argument which si the file name
diff --git a/scilab/modules/fileio/sci_gateway/c/sci_getURLheader.c b/scilab/modules/fileio/sci_gateway/c/sci_getURLheader.c
new file mode 100644 (file)
index 0000000..6250bfb
--- /dev/null
@@ -0,0 +1,230 @@
+/*
+ * Copyright (C) 2011 - DIGITEO - Sylvestre LEDRU
+ * Copyright (C) 2013 - Scilab Enterprises - Sylvestre LEDRU
+ * Copyright (C) 2013 - Scilab Enterprises - Antoine ELIAS
+ *
+ * This file must be used under the terms of the CeCILL.
+ * This source file is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution.  The terms
+ * are also available at
+ * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
+ *
+ */
+#include <stdio.h>
+#include "sciprint.h"
+#include "api_scilab.h"
+#include "Scierror.h"
+#include "MALLOC.h"
+#include "dlManager.h"
+#include "localization.h"
+/* ==================================================================== */
+static void freeAllocatedStrings(char** url, char** dest, char** username, char** password, char** content, char** filePath);
+/* ==================================================================== */
+int sci_getURLheader(char *fname, int fname_len)
+{
+    SciErr sciErr;
+    int length = 0;
+
+    int *piAddr = NULL;
+    char *url = NULL;
+    char *dest = NULL;
+    char *username = NULL;
+    char *password = NULL;
+    char *content = NULL;
+
+    int iRows = 0, iCols = 0;
+    int iType = 0;
+    int *piAddressVarOne = NULL;
+    int ret = 0;
+    char *filePath = NULL;
+
+    int iRhs = nbInputArgument(pvApiCtx);
+
+    CheckInputArgument(pvApiCtx, 1, 4);
+    CheckOutputArgument(pvApiCtx, 0, 2);
+
+
+    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
+    if (sciErr.iErr)
+    {
+        printError(&sciErr, 0);
+        return 0;
+    }
+
+    ret = getAllocatedSingleString(pvApiCtx, piAddressVarOne, &url);
+    if (ret)
+    {
+        Scierror(999, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 1);
+        freeAllocatedStrings(&url, &dest, &username, &password, &content, &filePath);
+        return 0;
+    }
+
+    if (iRhs > 1)
+    {
+        //Specify the destination
+        int *piAddressVarTwo = NULL;
+
+        sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
+        if (sciErr.iErr)
+        {
+            printError(&sciErr, 0);
+            freeAllocatedStrings(&url, &dest, &username, &password, &content, &filePath);
+            return 0;
+        }
+
+        ret = getAllocatedSingleString(pvApiCtx, piAddressVarTwo, &dest);
+        if (ret)
+        {
+            Scierror(999, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 2);
+            freeAllocatedStrings(&url, &dest, &username, &password, &content, &filePath);
+            return 0;
+        }
+
+    }
+    if (iRhs > 2)
+    {
+        //Specify the username
+        int *piAddressVarThree = NULL;
+
+        sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree);
+        if (sciErr.iErr)
+        {
+            printError(&sciErr, 0);
+            freeAllocatedStrings(&url, &dest, &username, &password, &content, &filePath);
+            return 0;
+        }
+
+        ret = getAllocatedSingleString(pvApiCtx, piAddressVarThree, &username);
+        if (ret)
+        {
+            Scierror(999, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 3);
+            freeAllocatedStrings(&url, &dest, &username, &password, &content, &filePath);
+            return 0;
+        }
+
+    }
+
+    if (iRhs > 3)
+    {
+        //Specify the password
+        int *piAddressVarFour = NULL;
+
+        sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddressVarFour);
+        if (sciErr.iErr)
+        {
+            printError(&sciErr, 0);
+            freeAllocatedStrings(&url, &dest, &username, &password, &content, &filePath);
+            return 0;
+        }
+
+        ret = getAllocatedSingleString(pvApiCtx, piAddressVarFour, &password);
+        if (ret)
+        {
+            Scierror(999, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 4);
+            freeAllocatedStrings(&url, &dest, &username, &password, &content, &filePath);
+            return 0;
+        }
+
+    }
+
+    //Set to NULL if 0 length strings
+    if (url != NULL && strlen(url) == 0)
+    {
+        FREE(url);
+        url = NULL;
+    }
+
+    if (dest != NULL && strlen(dest) == 0)
+    {
+        FREE(dest);
+        dest = NULL;
+    }
+
+    if (username != NULL && strlen(username) == 0)
+    {
+        FREE(username);
+        username = NULL;
+    }
+
+    if (password != NULL && strlen(password) == 0)
+    {
+        FREE(password);
+        password = NULL;
+    }
+
+    // call function
+    filePath = downloadFile(url, dest, username, password, &content, 1);
+    if (filePath)
+    {
+        //Return the second argument which si the file name
+        ret = createSingleString(pvApiCtx, iRhs + 1, filePath);
+        FREE(filePath);
+        filePath = NULL;
+        if (ret)
+        {
+            Scierror(999, _("%s: Could not create the output argument.\n"));
+            freeAllocatedStrings(&url, &dest, &username, &password, &content, &filePath);
+            return 0;
+        }
+    }
+
+    //Return the second argument which is the content
+    if (content && nbOutputArgument(pvApiCtx) == 2)
+    {
+        //create new variable with the content
+        int res = createSingleString(pvApiCtx, iRhs + 2, content);
+        if (res)
+        {
+            Scierror(999, _("%s: Could not create the output argument.\n"));
+            freeAllocatedStrings(&url, &dest, &username, &password, &content, &filePath);
+            return 0;
+        }
+    }
+
+    freeAllocatedStrings(&url, &dest, &username, &password, &content, &filePath);
+
+    AssignOutputVariable(pvApiCtx, 1) = iRhs + 1;
+    AssignOutputVariable(pvApiCtx, 2) = iRhs + 2;
+    ReturnArguments(pvApiCtx);
+    return 0;
+}
+/* ==================================================================== */
+static void freeAllocatedStrings(char** url, char** dest, char** username, char** password, char** content, char** filePath)
+{
+    if (url && *url)
+    {
+        freeAllocatedSingleString(*url);
+        *url = NULL;
+    }
+
+    if (dest && *dest)
+    {
+        freeAllocatedSingleString(*dest);
+        *dest = NULL;
+    }
+
+    if (username && *username)
+    {
+        freeAllocatedSingleString(*username);
+        *username = NULL;
+    }
+
+    if (password && *password)
+    {
+        freeAllocatedSingleString(*password);
+        *password = NULL;
+    }
+
+    if (content && *content)
+    {
+        FREE(*content);
+        *content = NULL;
+    }
+
+    if (filePath && *filePath)
+    {
+        FREE(*filePath);
+        *filePath = NULL;
+    }
+}
+
index 2182ec0..2fbdcfe 100644 (file)
@@ -84,4 +84,5 @@
     <PRIMITIVE gatewayId="34" primitiveId="49" primitiveName="archiveList" />
     <PRIMITIVE gatewayId="34" primitiveId="50" primitiveName="archiveExtract" />
     <PRIMITIVE gatewayId="34" primitiveId="51" primitiveName="archiveCompress" />
+    <PRIMITIVE gatewayId="34" primitiveId="52" primitiveName="getURLheader" />
 </GATEWAY>
index d26ddca..d539e21 100644 (file)
@@ -155,13 +155,14 @@ int getProxyValues(char **proxyHost, long *proxyPort, char **proxyUserPwd)
     return 1;
 }
 /* ==================================================================== */
-char *downloadFile(char *url, char *dest, char *username, char *password, char **content)
+char *downloadFile(char *url, char *dest, char *username, char *password, char **content, int header)
 {
     CURL *curl;
     CURLcode res;
     char *filename = NULL;
     FILE *file;
     inputString buffer;
+    inputString header_buffer;
     char *destdir = NULL;
     char *destfile = NULL;
 
@@ -173,6 +174,7 @@ char *downloadFile(char *url, char *dest, char *username, char *password, char *
     }
 
     init_string(&buffer);
+    init_string(&header_buffer);
 
     res = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
     if (res != CURLE_OK)
@@ -258,7 +260,18 @@ char *downloadFile(char *url, char *dest, char *username, char *password, char *
     strcat(filename, destfile);
     FREE(destdir);
     FREE(destfile);
-
+    struct curl_slist *chunk = NULL;
+    if (header == 1)
+    {
+        chunk = curl_slist_append(chunk, "Accept:");
+        chunk = curl_slist_append(chunk, "Another: yes");
+        res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
+        if (res != CURLE_OK)
+        {
+            printf("error\n");
+            return NULL;
+        }
+    }
     res = curl_easy_setopt(curl, CURLOPT_URL, url);
     if (res != CURLE_OK)
     {
@@ -351,7 +364,30 @@ char *downloadFile(char *url, char *dest, char *username, char *password, char *
             FREE(proxyUserPwd);
         }
     } /* end of the set of the proxy */
+    FILE *header_file;
+    if (header == 1)
+    {
+        char *header_filename;
+        header_filename = (char*)MALLOC(sizeof(char) * PATH_MAX + 1);
+        strcpy(header_filename, filename);
+        strcat(header_filename, ".header");
+        if (res != CURLE_OK)
+        {
+            FREE(filename);
+            Scierror(999, _("Failed to set the header option [%s]\n"), errorBuffer);
+            return NULL;
+        }
+        header_file = fopen(header_filename, "wb");
+        res = curl_easy_setopt(curl, CURLOPT_HEADERDATA, &header_buffer);
+        if (res != CURLE_OK)
+        {
+            FREE(filename);
+            FREE(header_filename);
+            Scierror(999, _("Failed to set the filename for the header data [%s]\n"), errorBuffer);
+            return NULL;
+        }
 
+    }
     res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
     if (res != CURLE_OK)
     {
@@ -379,7 +415,7 @@ char *downloadFile(char *url, char *dest, char *username, char *password, char *
         Scierror(999, _("Failed to set 'Follow Location' [%s]\n"), errorBuffer);
         return NULL;
     }
-
+    //    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
     res = curl_easy_perform(curl);
     if (res != 0)
     {
@@ -396,17 +432,21 @@ char *downloadFile(char *url, char *dest, char *username, char *password, char *
         FREE(filename);
         return NULL;
     }
-
     /* Write the file */
     fwrite(buffer.ptr, sizeof(char), buffer.len, file);
-
+    fwrite(header_buffer.ptr, sizeof(char), header_buffer.len, header_file);
     /* Create the variable which contains the output argument */
     *content = buffer.ptr;
 
     /* always cleanup */
     curl_easy_cleanup(curl);
+    curl_slist_free_all(chunk);
 
     fclose(file);
+    if (header == 1)
+    {
+        fclose(header_file);
+    }
     return filename;
 }
 /* ==================================================================== */
index 8e48bac..8efcf93 100644 (file)
@@ -24,6 +24,6 @@
  * @param[in] dest where to save the file
  * @return the file pointer
 */
-char *downloadFile(char *url, char *dest, char *username, char *password, char **content);
+char *downloadFile(char *url, char *dest, char *username, char *password, char **content, int header);
 
 #endif /* __DLMANAGER_H__ */
diff --git a/scilab/modules/fileio/tests/unit_tests/getURLheader.dia.ref b/scilab/modules/fileio/tests/unit_tests/getURLheader.dia.ref
new file mode 100644 (file)
index 0000000..7714a2f
--- /dev/null
@@ -0,0 +1,21 @@
+// ============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2015 - Kartik Gupta
+//
+//  This file is distributed under the same license as the Scilab package.
+// ============================================================================
+// <-- CLI SHELL MODE -->
+// ============================================================================
+// Unitary tests for getURLheader function
+// ============================================================================
+filename = getURLheader("www.google.co.in","google");
+assert_checkequal(filename,"google");
+assert_checktrue(isfile(filename));
+assert_checktrue(isfile(filename+".header"));
+deletefile(filename);
+deletefile(filename+".header");
+getURLheader("www.scilab.org/product/man/derivative.html");
+assert_checktrue(isfile("derivative.html"));
+assert_checktrue(isfile("derivative.html.header"));
+deletefile("derivative.html");
+deletefile("derivative.html.header");
diff --git a/scilab/modules/fileio/tests/unit_tests/getURLheader.tst b/scilab/modules/fileio/tests/unit_tests/getURLheader.tst
new file mode 100644 (file)
index 0000000..01ea76c
--- /dev/null
@@ -0,0 +1,26 @@
+// ============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2015 - Kartik Gupta
+//
+//  This file is distributed under the same license as the Scilab package.
+// ============================================================================
+
+// <-- CLI SHELL MODE -->
+
+// ============================================================================
+// Unitary tests for getURLheader function
+// ============================================================================
+
+filename = getURLheader("www.google.co.in","google");
+assert_checkequal(filename,"google");
+assert_checktrue(isfile(filename));
+assert_checktrue(isfile(filename+".header"));
+deletefile(filename);
+deletefile(filename+".header");
+
+
+getURLheader("www.scilab.org/product/man/derivative.html");
+assert_checktrue(isfile("derivative.html"));
+assert_checktrue(isfile("derivative.html.header"));
+deletefile("derivative.html");
+deletefile("derivative.html.header");