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