Skip to content

Commit b57fdf1

Browse files
Prophet731claude
andcommitted
Migrate WebClient/HttpWebRequest to Flurl.Http for Procon v2
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 7903e2d commit b57fdf1

3 files changed

Lines changed: 64 additions & 79 deletions

File tree

InsaneLimits.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
</PropertyGroup>
1313

1414
<ItemGroup>
15+
<PackageReference Include="Flurl.Http" Version="4.*" />
1516
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.*" />
1617
</ItemGroup>
1718

src/InsaneLimits/Actions.cs

Lines changed: 52 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
using System.Text;
1313
using System.Text.RegularExpressions;
1414
using System.Threading;
15+
using System.Threading.Tasks;
16+
17+
using Flurl.Http;
1518

1619
using PRoCon.Core;
1720
using PRoCon.Core.Battlemap;
@@ -1669,19 +1672,13 @@ Boolean quiet
16691672
/* Create the Status Update Request */
16701673
OAuthRequest orequest = TwitterStatusUpdateRequest(status, access_token, access_token_secret, consumer_key, consumer_secret);
16711674

1672-
HttpWebResponse oresponse = (HttpWebResponse)orequest.request.GetResponse();
1673-
1674-
String protcol = "HTTP/" + oresponse.ProtocolVersion + " " + (Int32)oresponse.StatusCode;
1675-
1676-
if (!oresponse.StatusCode.Equals(HttpStatusCode.OK))
1677-
throw new TwitterException("Twitter UpdateStatus Request failed, " + protcol);
1675+
String response = orequest.FlurlRequest
1676+
.PostStringAsync(orequest.PostBody ?? "")
1677+
.ReceiveString()
1678+
.Result;
16781679

1679-
if (oresponse.ContentLength == 0)
1680-
throw new TwitterException("Twitter UpdateStatus Request failed, ContentLength=0");
1681-
1682-
StreamReader sin = new StreamReader(oresponse.GetResponseStream());
1683-
String response = sin.ReadToEnd();
1684-
sin.Close();
1680+
if (String.IsNullOrEmpty(response))
1681+
throw new TwitterException("Twitter UpdateStatus Request failed, empty response");
16851682

16861683
Hashtable data = (Hashtable)JSON.JsonDecode(response);
16871684

@@ -1699,7 +1696,7 @@ Boolean quiet
16991696
if (!quiet)
17001697
ConsoleException(e.Message);
17011698
}
1702-
catch (WebException e)
1699+
catch (FlurlHttpException e)
17031700
{
17041701
if (!quiet)
17051702
HandleTwitterWebException(e, "UpdateStatus");
@@ -1732,18 +1729,13 @@ public void VerifyTwitterPin(String PIN)
17321729

17331730
OAuthRequest orequest = TwitterAccessTokenRequest(PIN, oauth_token, oauth_token_secret);
17341731

1735-
HttpWebResponse oresponse = (HttpWebResponse)orequest.request.GetResponse();
1736-
1737-
String protcol = "HTTP/" + oresponse.ProtocolVersion + " " + (Int32)oresponse.StatusCode;
1732+
String response = orequest.FlurlRequest
1733+
.PostStringAsync(orequest.PostBody ?? "")
1734+
.ReceiveString()
1735+
.Result;
17381736

1739-
if (!oresponse.StatusCode.Equals(HttpStatusCode.OK))
1740-
throw new TwitterException("Twitter AccessToken Request failed, " + protcol);
1741-
1742-
if (oresponse.ContentLength == 0)
1743-
throw new TwitterException("Twitter AccessToken Request failed, ContentLength=0");
1744-
1745-
StreamReader sin = new StreamReader(oresponse.GetResponseStream());
1746-
String response = sin.ReadToEnd();
1737+
if (String.IsNullOrEmpty(response))
1738+
throw new TwitterException("Twitter AccessToken Request failed, empty response");
17471739

17481740
DebugWrite("ACCESS_TOKEN_RESPONSE: " + response, 5);
17491741

@@ -1794,7 +1786,7 @@ public void VerifyTwitterPin(String PIN)
17941786
ConsoleWarn("Set the field ^btwitter_setup_account^n to ^bTrue^n to re-initiate the Twitter configuration");
17951787
return;
17961788
}
1797-
catch (WebException e)
1789+
catch (FlurlHttpException e)
17981790
{
17991791
HandleTwitterWebException(e, "AccessToken");
18001792
}
@@ -1816,17 +1808,13 @@ public void SetupTwitter()
18161808

18171809
OAuthRequest orequest = TwitterRequestTokenRequest();
18181810

1819-
HttpWebResponse oresponse = (HttpWebResponse)orequest.request.GetResponse();
1820-
String protcol = "HTTP/" + oresponse.ProtocolVersion + " " + (Int32)oresponse.StatusCode;
1821-
1822-
if (!oresponse.StatusCode.Equals(HttpStatusCode.OK))
1823-
throw new TwitterException("Twitter RequestToken Request failed, " + protcol);
1811+
String response = orequest.FlurlRequest
1812+
.PostStringAsync(orequest.PostBody ?? "")
1813+
.ReceiveString()
1814+
.Result;
18241815

1825-
if (oresponse.ContentLength == 0)
1826-
throw new TwitterException("Twitter RequestToken Request failed, ContentLength=0");
1827-
1828-
StreamReader sin = new StreamReader(oresponse.GetResponseStream());
1829-
String response = sin.ReadToEnd();
1816+
if (String.IsNullOrEmpty(response))
1817+
throw new TwitterException("Twitter RequestToken Request failed, empty response");
18301818

18311819
Dictionary<String, String> pairs = ParseQueryString(response);
18321820

@@ -1862,7 +1850,7 @@ public void SetupTwitter()
18621850
ConsoleException(e.Message);
18631851
return;
18641852
}
1865-
catch (WebException e)
1853+
catch (FlurlHttpException e)
18661854
{
18671855
HandleTwitterWebException(e, "RequestToken");
18681856
}
@@ -1873,42 +1861,38 @@ public void SetupTwitter()
18731861

18741862
}
18751863

1876-
public void HandleTwitterWebException(WebException e, String prefix)
1864+
public void HandleTwitterWebException(FlurlHttpException e, String prefix)
18771865
{
1878-
HttpWebResponse response = (HttpWebResponse)e.Response;
1879-
String protcol = (response == null) ? "" : "HTTP/" + response.ProtocolVersion;
1866+
Int32 statusCode = e.StatusCode ?? 0;
18801867

18811868
String error = String.Empty;
18821869
//try reading JSON response
1883-
if (response != null && response.ContentType != null && response.ContentType.ToLower().Contains("json"))
1870+
try
18841871
{
1885-
try
1872+
String data = e.GetResponseStringAsync().Result;
1873+
if (!String.IsNullOrEmpty(data))
18861874
{
1887-
StreamReader sin = new StreamReader(response.GetResponseStream());
1888-
String data = sin.ReadToEnd();
1889-
sin.Close();
1890-
18911875
Hashtable jdata = (Hashtable)JSON.JsonDecode(data);
1892-
if (jdata == null || !jdata.ContainsKey("error") ||
1893-
jdata["error"] == null || !jdata["error"].GetType().Equals(typeof(String)))
1894-
throw new Exception();
1895-
1896-
error = "Twitter Error: " + (String)jdata["error"] + ", ";
1897-
}
1898-
catch (Exception)
1899-
{
1876+
if (jdata != null && jdata.ContainsKey("error") &&
1877+
jdata["error"] != null && jdata["error"].GetType().Equals(typeof(String)))
1878+
{
1879+
error = "Twitter Error: " + (String)jdata["error"] + ", ";
1880+
}
19001881
}
19011882
}
1883+
catch (Exception)
1884+
{
1885+
}
19021886

19031887
/* Handle Time-Out Gracefully */
1904-
if (e.Status.Equals(WebExceptionStatus.Timeout))
1888+
if (e.InnerException is TaskCanceledException)
19051889
{
1906-
ConsoleException("Twitter " + prefix + " Request(" + protcol + ") timed-out");
1890+
ConsoleException("Twitter " + prefix + " Request(HTTP " + statusCode + ") timed-out");
19071891
return;
19081892
}
1909-
else if (e.Status.Equals(WebExceptionStatus.ProtocolError))
1893+
else if (statusCode > 0)
19101894
{
1911-
ConsoleException("Twitter " + prefix + " Request(" + protcol + ") failed, " + error + " " + e.GetType() + ": " + e.Message);
1895+
ConsoleException("Twitter " + prefix + " Request(HTTP " + statusCode + ") failed, " + error + " " + e.GetType() + ": " + e.Message);
19121896
return;
19131897
}
19141898
else
@@ -1948,11 +1932,10 @@ public OAuthRequest TwitterStatusUpdateRequest(
19481932

19491933
OAuthRequest orequest = new OAuthRequest(this, "https://api.twitter.com/1.1/statuses/update.json"); // Fix #48
19501934
orequest.Method = HTTPMethod.POST;
1951-
orequest.request.ContentType = "application/x-www-form-urlencoded";
19521935

19531936
/* Set the Post Data */
19541937

1955-
Byte[] data = Encoding.UTF8.GetBytes("status=" + OAuthRequest.UrlEncode(Encoding.UTF8.GetBytes(status)));
1938+
String postBody = "status=" + OAuthRequest.UrlEncode(Encoding.UTF8.GetBytes(status));
19561939

19571940
// Parameters required by the Twitter OAuth Protocol
19581941
orequest.parameters.Add(new KeyValuePair<String, String>("oauth_consumer_key", consumer_key));
@@ -1967,16 +1950,14 @@ public OAuthRequest TwitterStatusUpdateRequest(
19671950
String signature = orequest.Signature(consumer_secret, access_token_secret);
19681951
orequest.parameters.Add(new KeyValuePair<String, String>("oauth_signature", OAuthRequest.UrlEncode(signature)));
19691952

1970-
// Add the OAuth authentication header
1953+
// Add the OAuth authentication header and content type
19711954
String OAuthHeader = orequest.Header();
1972-
orequest.request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequired;
1973-
orequest.request.Headers["Authorization"] = OAuthHeader;
1955+
orequest.FlurlRequest
1956+
.WithHeader("Authorization", OAuthHeader)
1957+
.WithHeader("Content-Type", "application/x-www-form-urlencoded");
19741958

1975-
// Add the POST body
1976-
orequest.request.ContentLength = data.Length;
1977-
Stream sout = orequest.request.GetRequestStream();
1978-
sout.Write(data, 0, data.Length);
1979-
sout.Close();
1959+
// Store POST body for caller to send
1960+
orequest.PostBody = postBody;
19801961

19811962
return orequest;
19821963
}
@@ -1985,7 +1966,6 @@ public OAuthRequest TwitterAccessTokenRequest(String verifier, String token, Str
19851966
{
19861967
OAuthRequest orequest = new OAuthRequest(this, "https://api.twitter.com/oauth/access_token"); // Fix #48
19871968
orequest.Method = HTTPMethod.POST;
1988-
orequest.request.ContentLength = 0;
19891969

19901970
// Parameters required by the Twitter OAuth Protocol
19911971
orequest.parameters.Add(new KeyValuePair<String, String>("oauth_consumer_key", getStringVarValue("twitter_consumer_key")));
@@ -2002,8 +1982,8 @@ public OAuthRequest TwitterAccessTokenRequest(String verifier, String token, Str
20021982

20031983
// Add the OAuth authentication header
20041984
String OAuthHeader = orequest.Header();
2005-
orequest.request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequired;
2006-
orequest.request.Headers["Authorization"] = OAuthHeader;
1985+
orequest.FlurlRequest
1986+
.WithHeader("Authorization", OAuthHeader);
20071987

20081988
return orequest;
20091989
}
@@ -2012,7 +1992,6 @@ public OAuthRequest TwitterRequestTokenRequest()
20121992
{
20131993
OAuthRequest orequest = new OAuthRequest(this, "https://api.twitter.com/oauth/request_token"); // Fix #48
20141994
orequest.Method = HTTPMethod.POST;
2015-
orequest.request.ContentLength = 0;
20161995

20171996
// Parameters required by the Twitter OAuth Protocol
20181997
orequest.parameters.Add(new KeyValuePair<String, String>("oauth_callback", OAuthRequest.UrlEncode("oob")));
@@ -2028,8 +2007,8 @@ public OAuthRequest TwitterRequestTokenRequest()
20282007

20292008
// Add the OAuth authentication header
20302009
String OAuthHeader = orequest.Header();
2031-
orequest.request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequired;
2032-
orequest.request.Headers["Authorization"] = OAuthHeader;
2010+
orequest.FlurlRequest
2011+
.WithHeader("Authorization", OAuthHeader);
20332012

20342013
return orequest;
20352014
}

src/InsaneLimits/Models.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using System.Text;
1212
using System.Text.RegularExpressions;
1313

14+
using Flurl.Http;
15+
1416
using PRoCon.Core;
1517
using PRoCon.Core.Battlemap;
1618
using PRoCon.Core.Maps;
@@ -4479,20 +4481,23 @@ public void dumpMatchedStats()
44794481

44804482
public class OAuthRequest
44814483
{
4482-
public HttpWebRequest request = null;
4484+
public Uri Url = null;
4485+
public IFlurlRequest FlurlRequest = null;
44834486
InsaneLimits plugin = null;
44844487

44854488
HMACSHA1 SHA1 = null;
44864489

44874490
public List<KeyValuePair<String, String>> parameters = new List<KeyValuePair<String, String>>();
44884491

4489-
public HTTPMethod Method { set { request.Method = value.ToString(); } get { return (HTTPMethod)Enum.Parse(typeof(HTTPMethod), request.Method); } }
4492+
public HTTPMethod Method { set; get; }
4493+
public String PostBody { set; get; }
44904494

44914495
public OAuthRequest(InsaneLimits plugin, String URL)
44924496
{
44934497
this.plugin = plugin;
4494-
this.request = (HttpWebRequest)HttpWebRequest.Create(URL);
4495-
this.request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 4.0.20506)";
4498+
this.Url = new Uri(URL);
4499+
this.FlurlRequest = URL
4500+
.WithHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 4.0.20506)");
44964501
}
44974502

44984503
public void Sort()
@@ -4533,10 +4538,10 @@ public String Header()
45334538

45344539
public String Signature(String ConsumerSecret, String AccessTokenSecret)
45354540
{
4536-
String base_url = request.Address.Scheme + "://" + request.Address.Host + request.Address.AbsolutePath;
4541+
String base_url = Url.Scheme + "://" + Url.Host + Url.AbsolutePath;
45374542
String encoded_base_url = UrlEncode(base_url);
45384543

4539-
String http_method = request.Method;
4544+
String http_method = Method.ToString();
45404545

45414546
Sort();
45424547

0 commit comments

Comments
 (0)