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

[swift] fix: URLSession template correctly percent-encodes http body for application/x-www-form-urlencoded #20381

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Loading
Loading