Skip to content

Commit

Permalink
Move URL parsing to a new function
Browse files Browse the repository at this point in the history
  • Loading branch information
Dana Robinson committed Jan 15, 2025
1 parent b8e8b7f commit 04a6f61
Showing 1 changed file with 85 additions and 51 deletions.
136 changes: 85 additions & 51 deletions src/H5FDs3comms.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ static herr_t H5FD__s3comms_load_aws_creds_from_file(FILE *file, const char *pro
char *access_key, char *aws_region);

static herr_t H5FD__s3comms_make_iso_8661_string(time_t time, char iso8601[ISO8601_SIZE]);

static parsed_url_t *H5FD__s3comms_parse_url(const char *url);

static herr_t H5FD__s3comms_free_purl(parsed_url_t *purl);

/*********************/
Expand Down Expand Up @@ -572,7 +575,6 @@ H5FD__s3comms_s3r_close(s3r_t *handle)

if (H5FD__s3comms_free_purl(handle->purl) < 0)
HGOTO_ERROR(H5E_VFL, H5E_BADVALUE, FAIL, "unable to release parsed url structure");
H5MM_xfree(handle->purl);

H5MM_xfree(handle);

Expand Down Expand Up @@ -730,12 +732,9 @@ H5FD__s3comms_s3r_getsize(s3r_t *handle)
s3r_t *
H5FD__s3comms_s3r_open(const char *url, const H5FD_ros3_fapl_t *fa, const char *fapl_token)
{
CURL *curlh = NULL;
s3r_t *handle = NULL;
parsed_url_t *purl = NULL;
CURLU *curlurl = NULL;
CURLUcode rc;
s3r_t *ret_value = NULL;
CURL *curlh = NULL;
s3r_t *handle = NULL;
s3r_t *ret_value = NULL;

FUNC_ENTER_PACKAGE

Expand All @@ -744,51 +743,17 @@ H5FD__s3comms_s3r_open(const char *url, const H5FD_ros3_fapl_t *fa, const char *
if (url[0] == '\0')
HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "url cannot be an empty string");

/*************
* PARSE URL *
*************/

if (NULL == (curlurl = curl_url()))
HGOTO_ERROR(H5E_VFL, H5E_CANTCREATE, NULL, "unable to get curl url");
if (NULL == (purl = (parsed_url_t *)H5MM_calloc(sizeof(parsed_url_t))))
HGOTO_ERROR(H5E_VFL, H5E_CANTALLOC, NULL, "can't allocate space for parsed_url_t");

/* Separate the URL into parts using libcurl */
if (CURLUE_OK != curl_url_set(curlurl, CURLUPART_URL, url, 0))
HGOTO_ERROR(H5E_VFL, H5E_CANTCREATE, NULL, "unable to parse url");

/* Extract the URL components using libcurl */

/* scheme */
rc = curl_url_get(curlurl, CURLUPART_SCHEME, &(purl->scheme), 0);
if (CURLUE_OK != rc)
HGOTO_ERROR(H5E_VFL, H5E_CANTCREATE, NULL, "unable to get url scheme");
/* host */
rc = curl_url_get(curlurl, CURLUPART_HOST, &(purl->host), 0);
if (CURLUE_OK != rc)
HGOTO_ERROR(H5E_VFL, H5E_CANTCREATE, NULL, "unable to get url host");
/* port - okay to not exist */
rc = curl_url_get(curlurl, CURLUPART_PORT, &(purl->port), 0);
if (CURLUE_OK != rc && CURLUE_NO_PORT != rc)
HGOTO_ERROR(H5E_VFL, H5E_CANTCREATE, NULL, "unable to get url port");
/* path */
rc = curl_url_get(curlurl, CURLUPART_PATH, &(purl->path), 0);
if (CURLUE_OK != rc)
HGOTO_ERROR(H5E_VFL, H5E_CANTCREATE, NULL, "unable to get url path");
/* query - okay to not exist */
rc = curl_url_get(curlurl, CURLUPART_QUERY, &(purl->query), 0);
if (CURLUE_OK != rc && CURLUE_NO_QUERY != rc)
HGOTO_ERROR(H5E_VFL, H5E_CANTCREATE, NULL, "unable to get url query");

/* Create handle and set fields */
if (NULL == (handle = (s3r_t *)H5MM_calloc(sizeof(s3r_t))))
HGOTO_ERROR(H5E_VFL, H5E_CANTALLOC, NULL, "could not allocate space for handle");

handle->purl = purl;

if (NULL == (handle->http_verb = (char *)H5MM_calloc(sizeof(char) * 16)))
HGOTO_ERROR(H5E_VFL, H5E_CANTALLOC, NULL, "unable to allocate space for S3 request HTTP verb");

/* Parse URL */
if (NULL == (handle->purl = H5FD__s3comms_parse_url(url)))
HGOTO_ERROR(H5E_VFL, H5E_CANTALLOC, NULL, "could not allocate and create parsed URL");

/*************************************
* RECORD AUTHENTICATION INFORMATION *
*************************************/
Expand Down Expand Up @@ -879,16 +844,13 @@ H5FD__s3comms_s3r_open(const char *url, const H5FD_ros3_fapl_t *fa, const char *
ret_value = handle;

done:
curl_url_cleanup(curlurl);

if (ret_value == NULL) {
curl_easy_cleanup(curlh);

if (H5FD__s3comms_free_purl(purl) < 0)
HDONE_ERROR(H5E_VFL, H5E_BADVALUE, NULL, "unable to free parsed url structure");
H5MM_xfree(purl);

if (handle != NULL) {
if (H5FD__s3comms_free_purl(handle->purl) < 0)
HDONE_ERROR(H5E_VFL, H5E_CANTFREE, NULL, "unable to free parsed url structure");

H5MM_xfree(handle->aws_region);
H5MM_xfree(handle->secret_id);
H5MM_xfree(handle->signing_key);
Expand Down Expand Up @@ -1433,6 +1395,76 @@ H5FD__s3comms_bytes_to_hex(char *dest, size_t dest_len, const unsigned char *msg
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5FD__s3comms_bytes_to_hex() */

/*----------------------------------------------------------------------------
* Function: H5FD__s3comms_parse_url
*
* Purpose: Release resources from a parsed_url_t pointer
*
* Return: Success: A pointer to a parsed_url_t
* Failure: NULL
*----------------------------------------------------------------------------
*/
static parsed_url_t *
H5FD__s3comms_parse_url(const char *url)
{
CURLUcode rc;
CURLU *curlurl = NULL;
parsed_url_t *purl = NULL;
parsed_url_t *ret_value = NULL;

FUNC_ENTER_PACKAGE

assert(url);

/* Get a curl URL handle */
if (NULL == (curlurl = curl_url()))
HGOTO_ERROR(H5E_VFL, H5E_CANTCREATE, NULL, "unable to get curl url");

/* Separate the URL into parts using libcurl */
if (CURLUE_OK != curl_url_set(curlurl, CURLUPART_URL, url, 0))
HGOTO_ERROR(H5E_VFL, H5E_CANTCREATE, NULL, "unable to parse url");

/* Allocate memory for the retrned parsed URL */

Check failure on line 1427 in src/H5FDs3comms.c

View workflow job for this annotation

GitHub Actions / Check for spelling errors

retrned ==> returned
if (NULL == (purl = (parsed_url_t *)H5MM_calloc(sizeof(parsed_url_t))))
HGOTO_ERROR(H5E_VFL, H5E_CANTALLOC, NULL, "can't allocate space for parsed_url_t");

/* Extract the URL components using libcurl */

/* scheme */
rc = curl_url_get(curlurl, CURLUPART_SCHEME, &(purl->scheme), 0);
if (CURLUE_OK != rc)
HGOTO_ERROR(H5E_VFL, H5E_CANTCREATE, NULL, "unable to get url scheme");
/* host */
rc = curl_url_get(curlurl, CURLUPART_HOST, &(purl->host), 0);
if (CURLUE_OK != rc)
HGOTO_ERROR(H5E_VFL, H5E_CANTCREATE, NULL, "unable to get url host");
/* port - okay to not exist */
rc = curl_url_get(curlurl, CURLUPART_PORT, &(purl->port), 0);
if (CURLUE_OK != rc && CURLUE_NO_PORT != rc)
HGOTO_ERROR(H5E_VFL, H5E_CANTCREATE, NULL, "unable to get url port");
/* path */
rc = curl_url_get(curlurl, CURLUPART_PATH, &(purl->path), 0);
if (CURLUE_OK != rc)
HGOTO_ERROR(H5E_VFL, H5E_CANTCREATE, NULL, "unable to get url path");
/* query - okay to not exist */
rc = curl_url_get(curlurl, CURLUPART_QUERY, &(purl->query), 0);
if (CURLUE_OK != rc && CURLUE_NO_QUERY != rc)
HGOTO_ERROR(H5E_VFL, H5E_CANTCREATE, NULL, "unable to get url query");

ret_value = purl;

done:
curl_url_cleanup(curlurl);

if (ret_value == NULL) {
if (H5FD__s3comms_free_purl(purl) < 0)
HDONE_ERROR(H5E_VFL, H5E_BADVALUE, NULL, "unable to free parsed url structure");
H5MM_xfree(purl);
}

FUNC_LEAVE_NOAPI(ret_value)
} /* end H5FD__s3comms_parse_url() */

/*----------------------------------------------------------------------------
* Function: H5FD__s3comms_free_purl
*
Expand All @@ -1457,6 +1489,8 @@ H5FD__s3comms_free_purl(parsed_url_t *purl)
curl_free(purl->path);
curl_free(purl->query);

H5MM_xfree(purl);

done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5FD__s3comms_free_purl() */
Expand Down

0 comments on commit 04a6f61

Please sign in to comment.