Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit c16cb23

Browse files
authored
Merge pull request #492 from ServiceStack/netcore
Don't set restricted header if property was removed by Xamarin linker
2 parents 5dc2bff + 76a7519 commit c16cb23

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/ServiceStack.Text/PclExport.NetStandard.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public class NetStandardPclExport : PclExport
7474
.GetProperty("ContentLength")
7575
?.SetMethod()?.CreateDelegate(typeof(Action<HttpWebRequest, long>));
7676

77+
private bool allowToChangeRestrictedHeaders;
7778

7879
public NetStandardPclExport()
7980
{
@@ -83,6 +84,15 @@ public NetStandardPclExport()
8384
#else
8485
this.DirSep = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? '\\' : '/';
8586
#endif
87+
var req = HttpWebRequest.Create("http://servicestack.net");
88+
try
89+
{
90+
req.Headers[HttpRequestHeader.UserAgent] = "ServiceStack";
91+
allowToChangeRestrictedHeaders = true;
92+
} catch (ArgumentException)
93+
{
94+
allowToChangeRestrictedHeaders = false;
95+
}
8696
}
8797

8898
public override string ReadAllText(string filePath)
@@ -498,7 +508,8 @@ public override void SetUserAgent(HttpWebRequest httpReq, string value)
498508
SetUserAgentDelegate(httpReq, value);
499509
} else
500510
{
501-
httpReq.Headers[HttpRequestHeader.UserAgent] = value;
511+
if (allowToChangeRestrictedHeaders)
512+
httpReq.Headers[HttpRequestHeader.UserAgent] = value;
502513
}
503514
}
504515

@@ -509,7 +520,8 @@ public override void SetContentLength(HttpWebRequest httpReq, long value)
509520
SetContentLengthDelegate(httpReq, value);
510521
} else
511522
{
512-
httpReq.Headers[HttpRequestHeader.ContentLength] = value.ToString();
523+
if (allowToChangeRestrictedHeaders)
524+
httpReq.Headers[HttpRequestHeader.ContentLength] = value.ToString();
513525
}
514526
}
515527

@@ -547,7 +559,7 @@ public override void Config(HttpWebRequest req,
547559
if (allowAutoRedirect.HasValue) SetAllowAutoRedirect(req, allowAutoRedirect.Value);
548560
//if (readWriteTimeout.HasValue) req.ReadWriteTimeout = (int)readWriteTimeout.Value.TotalMilliseconds;
549561
//if (timeout.HasValue) req.Timeout = (int)timeout.Value.TotalMilliseconds;
550-
if (userAgent != null) req.Headers[HttpRequestHeader.UserAgent] = userAgent;
562+
if (userAgent != null) SetUserAgent(req, userAgent);
551563
//if (preAuthenticate.HasValue) req.PreAuthenticate = preAuthenticate.Value;
552564
}
553565

0 commit comments

Comments
 (0)