Skip to content

Commit bba8785

Browse files
rubojonpryor
authored andcommitted
[Mono.Android+AndroidClientHandler] Use space within User-Agent (#2071)
Fixes: #1931 The HTTP `User-Agent` header must use the space symbol (` `) as a separator instead of comma (`,`), as per [RFC 7231][0], which states that [`User-Agent`][1] is: User-Agent = product *( RWS ( product / comment ) ) [`RWS` is defined in RFC 7230][2]: RWS = 1*( SP / HTAB ) ; required whitespace ([`SP` and `HTAB` in turn come from RFC 5234 Appendix 5.1][3].) Update `AndroidClientHandler.AddHeaders()` so that space (` `) is used to separate `User-Agent` values, not comma (`,`). [0]: https://tools.ietf.org/html/rfc7231 [1]: https://tools.ietf.org/html/rfc7231#page-46 [2]: https://tools.ietf.org/html/rfc7230#section-3.2.3 [3]: https://tools.ietf.org/html/rfc5234#appendix-B.1
1 parent f3bfa9e commit bba8785

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/Mono.Android/Test/Xamarin.Android.Net/HttpClientIntegrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ public void Send_Complete_CustomHeaders_SpecialSeparators ()
468468
var request = l.Request;
469469

470470
try {
471-
Assert.AreEqual ("MLK,Android,Phone,1.1.9", request.UserAgent, "#1");
471+
Assert.AreEqual ("MLK Android Phone 1.1.9", request.UserAgent, "#1");
472472
failed = false;
473473
} catch (Exception ex) {
474474
failed = true;

src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ sealed class RequestRedirectionState
7171
const string DEFLATE_ENCODING = "deflate";
7272
const string IDENTITY_ENCODING = "identity";
7373

74+
static readonly IDictionary<string, string> headerSeparators = new Dictionary<string, string> {
75+
["User-Agent"] = " ",
76+
};
77+
7478
static readonly HashSet <string> known_content_headers = new HashSet <string> (StringComparer.OrdinalIgnoreCase) {
7579
"Allow",
7680
"Content-Disposition",
@@ -868,13 +872,15 @@ void HandlePreAuthentication (HttpURLConnection httpConnection)
868872
httpConnection.SetRequestProperty (data.UseProxyAuthentication ? "Proxy-Authorization" : "Authorization", authorization.Message);
869873
}
870874

875+
static string GetHeaderSeparator (string name) => headerSeparators.TryGetValue (name, out var value) ? value : ",";
876+
871877
void AddHeaders (HttpURLConnection conn, HttpHeaders headers)
872878
{
873879
if (headers == null)
874880
return;
875881

876882
foreach (KeyValuePair<string, IEnumerable<string>> header in headers) {
877-
conn.SetRequestProperty (header.Key, header.Value != null ? String.Join (",", header.Value) : String.Empty);
883+
conn.SetRequestProperty (header.Key, header.Value != null ? String.Join (GetHeaderSeparator (header.Key), header.Value) : String.Empty);
878884
}
879885
}
880886

0 commit comments

Comments
 (0)