Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Query String Parameters in the URL #469

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions common/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,8 @@ TDNFJoinPath(char **ppszPath, ...)
char *pszNodeCopy = NULL;
char *pszResult = NULL;
int nLength = 0;
int nIsRemoteUrl = 0;
char *pszUrlQueryStringCopy = NULL;

if (!ppszPath)
{
Expand All @@ -836,6 +838,19 @@ TDNFJoinPath(char **ppszPath, ...)
* before stripping all leading slashes */
if (i == 0)
{
/* we also check to see if the base path is a remote URL; for URLs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like that you are using TDNFJoinPath() for this, which is a generic function to join path elements (much like python's os.path.join(). I think it's not very efficient, because this code will now be used in all places where we join paths, which are mostly local.

Instead, I think we should create another version of this function that is only used where we need this functionality of preserving the URL query string.

Copy link
Contributor

@oliverkurth oliverkurth Mar 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adithyaj , are you going to address my comments?

* with query strings, we will need to splice additional path components
* before the query string */
dwError = TDNFUriIsRemote(pszNodeTmp, &nIsRemoteUrl);

/* we explicitly ignore invalid URLs because this might not be a URL */
if (dwError == ERROR_TDNF_URL_INVALID)
{
dwError = 0;
}

BAIL_ON_TDNF_ERROR(dwError);

if (*pszNodeTmp == '/')
{
dwError = TDNFAllocateString("/", &pszResult);
Expand All @@ -847,9 +862,28 @@ TDNFJoinPath(char **ppszPath, ...)
}
BAIL_ON_TDNF_ERROR(dwError);
}

/* now strip leading slashes */
while(*pszNodeTmp == '/') pszNodeTmp++;

/* if this is the first node and if it's a remote URL, then
* strip off the query string at the end; we'll place it back
* on later after appending all other nodes */
if (i == 0 && nIsRemoteUrl)
{
char *pszUrlQueryString = strchr(pszNodeTmp, '?');
if (pszUrlQueryString != NULL)
{
dwError = TDNFAllocateString(pszUrlQueryString, &pszUrlQueryStringCopy);
BAIL_ON_TDNF_ERROR(dwError);

/* make sure to prealllocate space for the query string */
nLength += strlen(pszUrlQueryString);

*pszUrlQueryString = 0;
}
}

/* strip trailing slashes */
nLengthTmp = strlen(pszNodeTmp);
pszTmp = pszNodeTmp + nLengthTmp - 1;
Expand All @@ -858,6 +892,7 @@ TDNFJoinPath(char **ppszPath, ...)
*pszTmp = 0;
pszTmp--;
}

nLength += nLengthTmp + 2;

dwError = TDNFReAllocateMemory(nLength, (void **)&pszResult);
Expand All @@ -873,11 +908,19 @@ TDNFJoinPath(char **ppszPath, ...)
TDNF_SAFE_FREE_MEMORY(pszNodeCopy);
}

/* if this was a remote URL and we found a query string, then append
* it back on now */
if (nIsRemoteUrl && pszUrlQueryStringCopy != NULL)
{
strcat(pszResult, pszUrlQueryStringCopy);
}

*ppszPath = pszResult;
cleanup:
va_end(ap);
return dwError;
error:
TDNF_SAFE_FREE_MEMORY(pszUrlQueryStringCopy);
TDNF_SAFE_FREE_MEMORY(pszResult);
TDNF_SAFE_FREE_MEMORY(pszNodeCopy);
goto cleanup;
Expand Down