root/opal/util/uri.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. opal_uri_get_scheme
  2. opal_filename_to_uri
  3. opal_filename_from_uri

   1 /*
   2  * Copyright (c) 2012      Los Alamos National Security, LLC.
   3  *                         All rights reserved.
   4  * Copyright (c) 2015      Research Organization for Information Science
   5  *                         and Technology (RIST). All rights reserved.
   6  * Copyright (c) 2018      Amazon.com, Inc. or its affiliates.  All Rights reserved.
   7  * $COPYRIGHT$
   8  *
   9  * Additional copyrights may follow
  10  *
  11  * $HEADER$
  12  */
  13 
  14 #include "opal_config.h"
  15 
  16 #include <stdio.h>
  17 #include <stdlib.h>
  18 #include <string.h>
  19 #ifdef HAVE_UNISTD_H
  20 #include <unistd.h>
  21 #endif
  22 
  23 #include "opal/util/output.h"
  24 #include "opal/util/path.h"
  25 #include "opal/util/show_help.h"
  26 #include "opal/util/printf.h"
  27 
  28 #include "opal/util/uri.h"
  29 
  30 const static char *uri_reserved_path_chars="!$&'()*+,;=:@ ";
  31 
  32 char *opal_uri_get_scheme(const char *uri)
  33 {
  34     char *turi = strdup(uri);
  35     char *ptr;
  36 
  37     if (NULL == (ptr = strchr(turi, ':'))) {
  38         opal_show_help("help-opal-util.txt", "malformed-uri",
  39                        true, uri);
  40         free(turi);
  41         return NULL;
  42     }
  43     *ptr = '\0';
  44     return turi;
  45 }
  46 
  47 char *opal_filename_to_uri(const char *filename,
  48                            const char *hostname)
  49 {
  50     char *uri, *fn;
  51     size_t i, j, k, n;
  52 
  53     /* filename must be an absolute path */
  54     if (!opal_path_is_absolute(filename)) {
  55         opal_show_help("help-opal-util.txt", "relative-path",
  56                        true, filename);
  57         return NULL;
  58     }
  59 
  60     /* if hostname is NULL, then this is a local file, so
  61      * the scheme can either be missing or given as "localhost"
  62      */
  63     if (NULL == hostname) {
  64         opal_asprintf(&uri, "file://%s", filename);
  65         return uri;
  66     }
  67 
  68     /* count the number of characters that require escaping
  69      * in the filename
  70      */
  71     n=0;
  72     for (j=0; j < strlen(uri_reserved_path_chars)-1; j++) {
  73         if (NULL != strchr(filename, uri_reserved_path_chars[j])) {
  74             n++;
  75         }
  76     }
  77     /* escape them if necessary */
  78     if (0 < n) {
  79         fn = (char*)malloc(strlen(filename) + n + 1);
  80         i=0;
  81         for (k=0; k < strlen(filename)-1; k++) {
  82             for (j=0; j < strlen(uri_reserved_path_chars)-1; j++) {
  83                 if (filename[k] == uri_reserved_path_chars[j]) {
  84                     fn[i] = '\\';
  85                     i++;
  86                     break;
  87                 }
  88             }
  89             fn[i] = filename[k];
  90             i++;
  91         }
  92         fn[i] = '\0';
  93     } else {
  94         fn = strdup(filename);
  95     }
  96 
  97     /* construct the uri - the filename was already tested to
  98      * ensure it was absolute, so the required separator should
  99      * already be present
 100      */
 101     opal_asprintf(&uri, "file://%s%s", hostname, fn);
 102     free(fn);
 103     return uri;
 104 }
 105 
 106 char *opal_filename_from_uri(const char *uri,
 107                              char **hostname)
 108 {
 109     char *turi;
 110     char *ptr, *fn, *sp;
 111 
 112     /* protect the incoming string */
 113     turi = strdup(uri);
 114 
 115     /* set defaults */
 116     fn = NULL;
 117     if (NULL != hostname) {
 118         *hostname = NULL;
 119     }
 120 
 121     /* extract the scheme */
 122     if (NULL == (ptr = strchr(turi, ':'))) {
 123         opal_show_help("help-opal-util.txt", "malformed-uri",
 124                        true, uri);
 125         free(turi);
 126         return NULL;
 127     }
 128     *ptr = '\0';
 129     ptr++;  /* step over the new NULL */
 130 
 131     /* if there are three '/', then there is no
 132      * hostname and the file is local
 133      */
 134     if (0 == strncmp(ptr, "///", 3)) {
 135         /* step to the filename - as it is required
 136          * to be an absolute path, leave one slash
 137          * in the name
 138          */
 139         ptr += 2;
 140         fn = strdup(ptr);
 141     } else if (0 != strncmp(ptr, "//", 2)) {
 142         /* error */
 143         opal_show_help("help-opal-util.txt", "malformed-uri",
 144                        true, uri);
 145     } else {
 146         ptr += 2;  /* step to the hostname */
 147         /* find the separator to the filename */
 148         if (NULL == (sp = strchr(ptr, '/'))) {
 149             opal_show_help("help-opal-util.txt", "malformed-uri",
 150                            true, uri);
 151         } else {
 152             *sp = '\0';
 153             if (NULL != hostname) {
 154                 *hostname = strdup(ptr);
 155             }
 156             /* the filename is required to be an
 157              * absolute path, so restore the slash
 158              */
 159             *sp = '/';
 160             fn = strdup(sp);
 161         }
 162     }
 163     free(turi);
 164     return fn;
 165 }

/* [<][>][^][v][top][bottom][index][help] */