Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 67bfa3b

Browse files
committed
refactor new line
1 parent 2774ac0 commit 67bfa3b

File tree

6 files changed

+33
-30
lines changed

6 files changed

+33
-30
lines changed

Titanium.Web.Proxy/Helpers/HttpWriter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal class HttpWriter : ICustomStreamWriter
1616
private readonly Stream stream;
1717
private readonly IBufferPool bufferPool;
1818

19-
private static readonly byte[] newLine = ProxyConstants.NewLine;
19+
private static readonly byte[] newLine = ProxyConstants.NewLineBytes;
2020

2121
private static readonly Encoder encoder = Encoding.ASCII.GetEncoder();
2222

@@ -109,9 +109,9 @@ internal async Task WriteHeadersAsync(HeaderCollection headers, bool flush = tru
109109
var headerBuilder = new StringBuilder();
110110
foreach (var header in headers)
111111
{
112-
headerBuilder.AppendLine(header.ToString());
112+
headerBuilder.Append($"{header.ToString()}{ProxyConstants.NewLine}");
113113
}
114-
headerBuilder.AppendLine();
114+
headerBuilder.Append(ProxyConstants.NewLine);
115115

116116
await WriteAsync(headerBuilder.ToString(), cancellationToken);
117117

Titanium.Web.Proxy/Http/HttpWebClient.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Titanium.Web.Proxy.Extensions;
99
using Titanium.Web.Proxy.Models;
1010
using Titanium.Web.Proxy.Network.Tcp;
11+
using Titanium.Web.Proxy.Shared;
1112

1213
namespace Titanium.Web.Proxy.Http
1314
{
@@ -16,8 +17,6 @@ namespace Titanium.Web.Proxy.Http
1617
/// </summary>
1718
public class HttpWebClient
1819
{
19-
private const string CRLF = "\r\n";
20-
2120
internal HttpWebClient(Request request = null, Response response = null)
2221
{
2322
Request = request ?? new Request();
@@ -111,21 +110,20 @@ await writer.WriteLineAsync(Request.CreateRequestLine(Request.Method,
111110
&& !string.IsNullOrEmpty(upstreamProxy.UserName)
112111
&& upstreamProxy.Password != null)
113112
{
114-
headerBuilder.AppendFormat("{0}{1}", HttpHeader.ProxyConnectionKeepAlive, CRLF);
115-
headerBuilder.AppendFormat("{0}{1}",
116-
HttpHeader.GetProxyAuthorizationHeader(upstreamProxy.UserName, upstreamProxy.Password), CRLF);
113+
headerBuilder.Append($"{HttpHeader.ProxyConnectionKeepAlive}{ProxyConstants.NewLine}");
114+
headerBuilder.Append($"{HttpHeader.GetProxyAuthorizationHeader(upstreamProxy.UserName, upstreamProxy.Password)}{ProxyConstants.NewLine}");
117115
}
118116

119117
// write request headers
120118
foreach (var header in Request.Headers)
121119
{
122120
if (isTransparent || header.Name != KnownHeaders.ProxyAuthorization)
123121
{
124-
headerBuilder.AppendFormat("{0}{1}", header, CRLF);
122+
headerBuilder.Append($"{header}{ProxyConstants.NewLine}");
125123
}
126124
}
127125

128-
headerBuilder.Append(CRLF);
126+
headerBuilder.Append(ProxyConstants.NewLine);
129127

130128
await writer.WriteAsync(headerBuilder.ToString(), cancellationToken);
131129

Titanium.Web.Proxy/Http/Request.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,13 @@ public override string HeaderText
140140
get
141141
{
142142
var sb = new StringBuilder();
143-
sb.AppendLine(CreateRequestLine(Method, OriginalUrl, HttpVersion));
143+
sb.Append($"{CreateRequestLine(Method, OriginalUrl, HttpVersion)}{ProxyConstants.NewLine}");
144144
foreach (var header in Headers)
145145
{
146-
sb.AppendLine(header.ToString());
146+
sb.Append($"{header.ToString()}{ProxyConstants.NewLine}");
147147
}
148148

149-
sb.AppendLine();
149+
sb.Append(ProxyConstants.NewLine);
150150
return sb.ToString();
151151
}
152152
}

Titanium.Web.Proxy/Http/Response.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,13 @@ public override string HeaderText
110110
get
111111
{
112112
var sb = new StringBuilder();
113-
sb.AppendLine(CreateResponseLine(HttpVersion, StatusCode, StatusDescription));
113+
sb.Append($"{CreateResponseLine(HttpVersion, StatusCode, StatusDescription)}{ProxyConstants.NewLine}");
114114
foreach (var header in Headers)
115115
{
116-
sb.AppendLine(header.ToString());
116+
sb.Append($"{header.ToString()}{ProxyConstants.NewLine}");
117117
}
118118

119-
sb.AppendLine();
119+
sb.Append(ProxyConstants.NewLine);
120120
return sb.ToString();
121121
}
122122
}

Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Titanium.Web.Proxy.Http;
1717
using Titanium.Web.Proxy.Models;
1818
using Titanium.Web.Proxy.Network.Tcp;
19+
using Titanium.Web.Proxy.Shared;
1920

2021
namespace Titanium.Web.Proxy
2122
{
@@ -24,15 +25,6 @@ namespace Titanium.Web.Proxy
2425
/// </summary>
2526
public partial class ProxyServer
2627
{
27-
private static readonly Regex uriSchemeRegex =
28-
new Regex("^[a-z]*://", RegexOptions.IgnoreCase | RegexOptions.Compiled);
29-
30-
private static readonly HashSet<string> proxySupportedCompressions =
31-
new HashSet<string>(StringComparer.OrdinalIgnoreCase)
32-
{
33-
"gzip",
34-
"deflate"
35-
};
3628

3729
private bool isWindowsAuthenticationEnabledAndSupported =>
3830
EnableWinAuth && RunTime.IsWindows && !RunTime.IsRunningOnMono;
@@ -96,7 +88,7 @@ await HeaderParser.ReadHeaders(clientStream, args.WebSession.Request.Headers,
9688
cancellationToken);
9789

9890
Uri httpRemoteUri;
99-
if (uriSchemeRegex.IsMatch(httpUrl))
91+
if (ProxyConstants.UriSchemeRegex.IsMatch(httpUrl))
10092
{
10193
try
10294
{
@@ -400,7 +392,7 @@ private void prepareRequestHeaders(HeaderCollection requestHeaders)
400392
//only allow proxy supported compressions
401393
supportedAcceptEncoding.AddRange(acceptEncoding.Split(',')
402394
.Select(x => x.Trim())
403-
.Where(x => proxySupportedCompressions.Contains(x)));
395+
.Where(x => ProxyConstants.ProxySupportedCompressions.Contains(x)));
404396

405397
//uncompressed is always supported by proxy
406398
supportedAcceptEncoding.Add("identity");

Titanium.Web.Proxy/Shared/ProxyConstants.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Text.RegularExpressions;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text.RegularExpressions;
24

35
namespace Titanium.Web.Proxy.Shared
46
{
@@ -14,9 +16,20 @@ internal class ProxyConstants
1416
internal static readonly char[] SemiColonSplit = { ';' };
1517
internal static readonly char[] EqualSplit = { '=' };
1618

17-
internal static readonly byte[] NewLine = { (byte)'\r', (byte)'\n' };
19+
internal static readonly string NewLine = "\r\n";
20+
internal static readonly byte[] NewLineBytes = { (byte)'\r', (byte)'\n' };
1821

19-
public static readonly Regex CNRemoverRegex =
22+
internal static readonly Regex UriSchemeRegex =
23+
new Regex("^[a-z]*://", RegexOptions.IgnoreCase | RegexOptions.Compiled);
24+
25+
internal static readonly HashSet<string> ProxySupportedCompressions =
26+
new HashSet<string>(StringComparer.OrdinalIgnoreCase)
27+
{
28+
"gzip",
29+
"deflate"
30+
};
31+
32+
internal static readonly Regex CNRemoverRegex =
2033
new Regex(@"^CN\s*=\s*", RegexOptions.IgnoreCase | RegexOptions.Compiled);
2134
}
2235
}

0 commit comments

Comments
 (0)