Commit e3775e03 authored by justcoding121's avatar justcoding121

Use stringbuilder to write headers in bulk

parent 222f2776
...@@ -104,12 +104,15 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -104,12 +104,15 @@ namespace Titanium.Web.Proxy.Helpers
internal async Task WriteHeadersAsync(HeaderCollection headers, bool flush = true, internal async Task WriteHeadersAsync(HeaderCollection headers, bool flush = true,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var headerBuilder = new StringBuilder();
foreach (var header in headers) foreach (var header in headers)
{ {
await header.WriteToStreamAsync(this, cancellationToken); header.Write(headerBuilder);
} }
headerBuilder.AppendLine();
await WriteAsync(headerBuilder.ToString(), cancellationToken);
await WriteLineAsync(cancellationToken);
if (flush) if (flush)
{ {
await stream.FlushAsync(cancellationToken); await stream.FlushAsync(cancellationToken);
......
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Titanium.Web.Proxy.Extensions; using Titanium.Web.Proxy.Extensions;
...@@ -15,12 +15,9 @@ namespace Titanium.Web.Proxy.Http ...@@ -15,12 +15,9 @@ namespace Titanium.Web.Proxy.Http
/// </summary> /// </summary>
public class HttpWebClient public class HttpWebClient
{ {
private readonly int bufferSize;
internal HttpWebClient(int bufferSize, Request request = null, Response response = null) internal HttpWebClient(int bufferSize, Request request = null, Response response = null)
{ {
this.bufferSize = bufferSize;
Request = request ?? new Request(); Request = request ?? new Request();
Response = response ?? new Response(); Response = response ?? new Response();
} }
...@@ -99,15 +96,16 @@ namespace Titanium.Web.Proxy.Http ...@@ -99,15 +96,16 @@ namespace Titanium.Web.Proxy.Http
useUpstreamProxy || isTransparent ? Request.OriginalUrl : Request.RequestUri.PathAndQuery, useUpstreamProxy || isTransparent ? Request.OriginalUrl : Request.RequestUri.PathAndQuery,
Request.HttpVersion), cancellationToken); Request.HttpVersion), cancellationToken);
var headerBuilder = new StringBuilder();
// Send Authentication to Upstream proxy if needed // Send Authentication to Upstream proxy if needed
if (!isTransparent && upstreamProxy != null if (!isTransparent && upstreamProxy != null
&& ServerConnection.IsHttps == false && ServerConnection.IsHttps == false
&& !string.IsNullOrEmpty(upstreamProxy.UserName) && !string.IsNullOrEmpty(upstreamProxy.UserName)
&& upstreamProxy.Password != null) && upstreamProxy.Password != null)
{ {
await HttpHeader.ProxyConnectionKeepAlive.WriteToStreamAsync(writer, cancellationToken); HttpHeader.ProxyConnectionKeepAlive.Write(headerBuilder);
await HttpHeader.GetProxyAuthorizationHeader(upstreamProxy.UserName, upstreamProxy.Password) HttpHeader.GetProxyAuthorizationHeader(upstreamProxy.UserName, upstreamProxy.Password)
.WriteToStreamAsync(writer, cancellationToken); .Write(headerBuilder);
} }
// write request headers // write request headers
...@@ -115,11 +113,12 @@ namespace Titanium.Web.Proxy.Http ...@@ -115,11 +113,12 @@ namespace Titanium.Web.Proxy.Http
{ {
if (isTransparent || header.Name != KnownHeaders.ProxyAuthorization) if (isTransparent || header.Name != KnownHeaders.ProxyAuthorization)
{ {
await header.WriteToStreamAsync(writer, cancellationToken); header.Write(headerBuilder);
} }
} }
await writer.WriteLineAsync(cancellationToken); headerBuilder.AppendLine();
await writer.WriteAsync(headerBuilder.ToString(), cancellationToken);
if (enable100ContinueBehaviour) if (enable100ContinueBehaviour)
{ {
......
using System; using System;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.Http; using Titanium.Web.Proxy.Http;
namespace Titanium.Web.Proxy.Models namespace Titanium.Web.Proxy.Models
...@@ -64,11 +61,9 @@ namespace Titanium.Web.Proxy.Models ...@@ -64,11 +61,9 @@ namespace Titanium.Web.Proxy.Models
return result; return result;
} }
internal async Task WriteToStreamAsync(HttpWriter writer, CancellationToken cancellationToken) internal void Write(StringBuilder writer)
{ {
await writer.WriteAsync(Name, cancellationToken); writer.AppendLine($"{Name}: {Value}");
await writer.WriteAsync(": ", cancellationToken);
await writer.WriteLineAsync(Value, cancellationToken);
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment