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

Commit 5f442f0

Browse files
committed
Allow to pass IEnumerable<HttpHeader> to Respond methods.
1 parent a17fe88 commit 5f442f0

File tree

2 files changed

+63
-9
lines changed

2 files changed

+63
-9
lines changed

src/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,20 @@ public void SetResponseBodyString(string body)
469469
/// <param name="html">HTML content to sent.</param>
470470
/// <param name="headers">HTTP response headers.</param>
471471
/// <param name="closeServerConnection">Close the server connection used by request if any?</param>
472-
public void Ok(string html, Dictionary<string, HttpHeader>? headers = null,
472+
public void Ok(string html, IDictionary<string, HttpHeader>? headers = null,
473+
bool closeServerConnection = false)
474+
{
475+
Ok(html, headers?.Values, closeServerConnection);
476+
}
477+
478+
/// <summary>
479+
/// Before request is made to server respond with the specified HTML string to client
480+
/// and ignore the request.
481+
/// </summary>
482+
/// <param name="html">HTML content to sent.</param>
483+
/// <param name="headers">HTTP response headers.</param>
484+
/// <param name="closeServerConnection">Close the server connection used by request if any?</param>
485+
public void Ok(string html, IEnumerable<HttpHeader>? headers = null,
473486
bool closeServerConnection = false)
474487
{
475488
var response = new OkResponse();
@@ -491,7 +504,20 @@ public void Ok(string html, Dictionary<string, HttpHeader>? headers = null,
491504
/// <param name="result">The html content bytes.</param>
492505
/// <param name="headers">The HTTP headers.</param>
493506
/// <param name="closeServerConnection">Close the server connection used by request if any?</param>
494-
public void Ok(byte[] result, Dictionary<string, HttpHeader>? headers = null,
507+
public void Ok(byte[] result, IDictionary<string, HttpHeader>? headers = null,
508+
bool closeServerConnection = false)
509+
{
510+
Ok(result, headers?.Values, closeServerConnection);
511+
}
512+
513+
/// <summary>
514+
/// Before request is made to server respond with the specified byte[] to client
515+
/// and ignore the request.
516+
/// </summary>
517+
/// <param name="result">The html content bytes.</param>
518+
/// <param name="headers">The HTTP headers.</param>
519+
/// <param name="closeServerConnection">Close the server connection used by request if any?</param>
520+
public void Ok(byte[] result, IEnumerable<HttpHeader>? headers = null,
495521
bool closeServerConnection = false)
496522
{
497523
var response = new OkResponse();
@@ -512,7 +538,22 @@ public void Ok(byte[] result, Dictionary<string, HttpHeader>? headers = null,
512538
/// <param name="headers">The HTTP headers.</param>
513539
/// <param name="closeServerConnection">Close the server connection used by request if any?</param>
514540
public void GenericResponse(string html, HttpStatusCode status,
515-
Dictionary<string, HttpHeader>? headers = null, bool closeServerConnection = false)
541+
IDictionary<string, HttpHeader>? headers = null, bool closeServerConnection = false)
542+
{
543+
GenericResponse(html, status, headers?.Values, closeServerConnection);
544+
}
545+
546+
/// <summary>
547+
/// Before request is made to server 
548+
/// respond with the specified HTML string and the specified status to client.
549+
/// And then ignore the request. 
550+
/// </summary>
551+
/// <param name="html">The html content.</param>
552+
/// <param name="status">The HTTP status code.</param>
553+
/// <param name="headers">The HTTP headers.</param>
554+
/// <param name="closeServerConnection">Close the server connection used by request if any?</param>
555+
public void GenericResponse(string html, HttpStatusCode status,
556+
IEnumerable<HttpHeader>? headers = null, bool closeServerConnection = false)
516557
{
517558
var response = new GenericResponse(status);
518559
response.HttpVersion = HttpClient.Request.HttpVersion;
@@ -531,7 +572,21 @@ public void GenericResponse(string html, HttpStatusCode status,
531572
/// <param name="headers">The HTTP headers.</param>
532573
/// <param name="closeServerConnection">Close the server connection used by request if any?</param>
533574
public void GenericResponse(byte[] result, HttpStatusCode status,
534-
Dictionary<string, HttpHeader> headers, bool closeServerConnection = false)
575+
IDictionary<string, HttpHeader> headers, bool closeServerConnection = false)
576+
{
577+
GenericResponse(result, status, headers?.Values, closeServerConnection);
578+
}
579+
580+
/// <summary>
581+
/// Before request is made to server respond with the specified byte[],
582+
/// the specified status to client. And then ignore the request.
583+
/// </summary>
584+
/// <param name="result">The bytes to sent.</param>
585+
/// <param name="status">The HTTP status code.</param>
586+
/// <param name="headers">The HTTP headers.</param>
587+
/// <param name="closeServerConnection">Close the server connection used by request if any?</param>
588+
public void GenericResponse(byte[] result, HttpStatusCode status,
589+
IEnumerable<HttpHeader>? headers, bool closeServerConnection = false)
535590
{
536591
var response = new GenericResponse(status);
537592
response.HttpVersion = HttpClient.Request.HttpVersion;

src/Titanium.Web.Proxy/Http/HeaderCollection.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,15 @@ internal void AddHeader(KnownHeader name, KnownHeader value)
160160
public void AddHeader(HttpHeader newHeader)
161161
{
162162
// if header exist in non-unique header collection add it there
163-
if (nonUniqueHeaders.ContainsKey(newHeader.Name))
163+
if (nonUniqueHeaders.TryGetValue(newHeader.Name, out var list))
164164
{
165-
nonUniqueHeaders[newHeader.Name].Add(newHeader);
165+
list.Add(newHeader);
166166
return;
167167
}
168168

169169
// if header is already in unique header collection then move both to non-unique collection
170-
if (headers.ContainsKey(newHeader.Name))
170+
if (headers.TryGetValue(newHeader.Name, out var existing))
171171
{
172-
var existing = headers[newHeader.Name];
173172
headers.Remove(newHeader.Name);
174173

175174
nonUniqueHeaders.Add(newHeader.Name, new List<HttpHeader>
@@ -189,7 +188,7 @@ public void AddHeader(HttpHeader newHeader)
189188
/// Adds the given header objects to Request
190189
/// </summary>
191190
/// <param name="newHeaders"></param>
192-
public void AddHeaders(IEnumerable<HttpHeader> newHeaders)
191+
public void AddHeaders(IEnumerable<HttpHeader>? newHeaders)
193192
{
194193
if (newHeaders == null)
195194
{

0 commit comments

Comments
 (0)