append the query string from parsed uri

Check the UriUriA object, and if query string exists append it to the
new_url. Only appends the query part, fragments are still not appended

Function redir parses the new location header value using the
uriParseUriA function already, which populates the query field. This
field was already being printed, but it was not being appended to the
new_url during its construction.

Redirection chain of check_curl --onredirect=follow now mimics the chain
of check_http --onredirect=follow. Tested on the url:
mail.google.com/chat
This commit is contained in:
Ahmet Oeztuerk 2025-12-04 16:52:55 +01:00
parent 7ab5b3ba34
commit e0b30cc6e8

View file

@ -761,7 +761,7 @@ redir_wrapper redir(curlhelp_write_curlbuf *header_buf, const check_curl_config
}
/* compose new path */
/* TODO: handle fragments and query part of URL */
/* TODO: handle fragments of URL */
char *new_url = (char *)calloc(1, DEFAULT_BUFFER_SIZE);
if (uri.pathHead) {
for (UriPathSegmentA *pathSegment = uri.pathHead; pathSegment;
@ -772,6 +772,25 @@ redir_wrapper redir(curlhelp_write_curlbuf *header_buf, const check_curl_config
}
}
/* missing components have null,null in their UriTextRangeA
* add query parameters if they exist.
*/
if (uri.query.first && uri.query.afterLast){
// Ensure we have space for '?' + query_str + '\0' ahead of time, instead of calling strncat twice
size_t current_len = strlen(new_url);
size_t remaining_space = DEFAULT_BUFFER_SIZE - current_len - 1;
const char* query_str = uri_string(uri.query, buf, DEFAULT_BUFFER_SIZE);
size_t query_str_len = strlen(query_str);
if (remaining_space >= query_str_len + 1) {
strcat(new_url, "?");
strcat(new_url, query_str);
}else{
die(STATE_UNKNOWN, _("HTTP UNKNOWN - No space to add query part of size %d to the buffer, buffer has remaining size %d"), query_str_len , current_len );
}
}
if (working_state.serverPort == new_port &&
!strncmp(working_state.server_address, new_host, MAX_IPV4_HOSTLENGTH) &&
(working_state.host_name &&