Commit 9d037680 authored by Honfika's avatar Honfika

http headers names moved to constants

parent 5829ff32
......@@ -586,7 +586,7 @@ namespace Titanium.Web.Proxy.EventArguments
{
var response = new RedirectResponse();
response.HttpVersion = WebSession.Request.HttpVersion;
response.Headers.AddHeader("Location", url);
response.Headers.AddHeader(KnownHeaders.Location, url);
response.Body = emptyData;
await Respond(response);
......
using System;
using System.Text;
using Titanium.Web.Proxy.Http;
using Titanium.Web.Proxy.Shared;
namespace Titanium.Web.Proxy.Helpers
......@@ -28,7 +29,7 @@ namespace Titanium.Web.Proxy.Helpers
foreach (string parameter in parameters)
{
var split = parameter.Split(ProxyConstants.EqualSplit, 2);
if (split.Length == 2 && split[0].Trim().Equals("charset", StringComparison.CurrentCultureIgnoreCase))
if (split.Length == 2 && split[0].Trim().Equals(KnownHeaders.ContentTypeCharset, StringComparison.CurrentCultureIgnoreCase))
{
string value = split[1];
if (value.Equals("x-user-defined", StringComparison.OrdinalIgnoreCase))
......@@ -65,7 +66,7 @@ namespace Titanium.Web.Proxy.Helpers
foreach (string parameter in parameters)
{
var split = parameter.Split(ProxyConstants.EqualSplit, 2);
if (split.Length == 2 && split[0].Trim().Equals("boundary", StringComparison.CurrentCultureIgnoreCase))
if (split.Length == 2 && split[0].Trim().Equals(KnownHeaders.ContentTypeBoundary, StringComparison.CurrentCultureIgnoreCase))
{
string value = split[1];
if (value.Length > 2 && value[0] == '"' && value[value.Length - 1] == '"')
......
......@@ -22,7 +22,7 @@ namespace Titanium.Web.Proxy.Http
StatusDescription = "Connection Established"
};
response.Headers.AddHeader("Timestamp", DateTime.Now.ToString());
response.Headers.AddHeader(KnownHeaders.Timestamp, DateTime.Now.ToString());
return response;
}
}
......
......@@ -63,6 +63,21 @@ namespace Titanium.Web.Proxy.Http
return null;
}
public HttpHeader GetFirstHeader(string name)
{
if (Headers.TryGetValue(name, out var header))
{
return header;
}
if (NonUniqueHeaders.TryGetValue(name, out var headers))
{
return headers.FirstOrDefault();
}
return null;
}
/// <summary>
/// Returns all headers
/// </summary>
......@@ -235,7 +250,7 @@ namespace Titanium.Web.Proxy.Http
return null;
}
internal string SetOrAddHeaderValue(string headerName, string value)
internal void SetOrAddHeaderValue(string headerName, string value)
{
if (Headers.TryGetValue(headerName, out var header))
{
......@@ -245,8 +260,6 @@ namespace Titanium.Web.Proxy.Http
{
Headers.Add(headerName, new HttpHeader(headerName, value));
}
return null;
}
/// <summary>
......@@ -255,12 +268,12 @@ namespace Titanium.Web.Proxy.Http
internal void FixProxyHeaders()
{
//If proxy-connection close was returned inform to close the connection
string proxyHeader = GetHeaderValueOrNull("proxy-connection");
RemoveHeader("proxy-connection");
string proxyHeader = GetHeaderValueOrNull(KnownHeaders.ProxyConnection);
RemoveHeader(KnownHeaders.ProxyConnection);
if (proxyHeader != null)
{
SetOrAddHeaderValue("connection", proxyHeader);
SetOrAddHeaderValue(KnownHeaders.Connection, proxyHeader);
}
}
......
......@@ -105,7 +105,7 @@ namespace Titanium.Web.Proxy.Http
//write request headers
foreach (var header in Request.Headers)
{
if (header.Name != "Proxy-Authorization")
if (header.Name != KnownHeaders.ProxyAuthorization)
{
await header.WriteToStreamAsync(writer);
}
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Titanium.Web.Proxy.Http
{
public static class KnownHeaders
{
// Both
public const string Connection = "connection";
public const string ConnectionClose = "close";
public const string ConnectionKeepAlive = "keep-alive";
public const string ContentLength = "content-length";
public const string ContentType = "content-type";
public const string ContentTypeCharset = "charset";
public const string ContentTypeBoundary = "boundary";
public const string Upgrade = "upgrade";
public const string UpgradeWebsocket = "websocket";
// Request headers
public const string AcceptEncoding = "accept-encoding";
public const string Authorization = "Authorization";
public const string Expect = "expect";
public const string Expect100Continue = "100-continue";
public const string Host = "host";
public const string ProxyAuthorization = "Proxy-Authorization";
public const string ProxyConnection = "Proxy-Connection";
public const string ProxyConnectionClose = "close";
// Response headers
public const string ContentEncoding = "content-encoding";
public const string Location = "Location";
public const string ProxyAuthenticate = "Proxy-Authenticate";
public const string TransferEncoding = "transfer-encoding";
public const string TransferEncodingChunked = "chunked";
// ???
public const string Timestamp = "Timestamp";
}
}
......@@ -67,14 +67,14 @@ namespace Titanium.Web.Proxy.Http
/// </summary>
public string Host
{
get => Headers.GetHeaderValueOrNull("host");
set => Headers.SetOrAddHeaderValue("host", value);
get => Headers.GetHeaderValueOrNull(KnownHeaders.Host);
set => Headers.SetOrAddHeaderValue(KnownHeaders.Host, value);
}
/// <summary>
/// Content encoding header value
/// </summary>
public string ContentEncoding => Headers.GetHeaderValueOrNull("content-encoding");
public string ContentEncoding => Headers.GetHeaderValueOrNull(KnownHeaders.ContentEncoding);
/// <summary>
/// Request content-length
......@@ -83,7 +83,7 @@ namespace Titanium.Web.Proxy.Http
{
get
{
string headerValue = Headers.GetHeaderValueOrNull("content-length");
string headerValue = Headers.GetHeaderValueOrNull(KnownHeaders.ContentLength);
if (headerValue == null)
{
......@@ -102,12 +102,12 @@ namespace Titanium.Web.Proxy.Http
{
if (value >= 0)
{
Headers.SetOrAddHeaderValue("content-length", value.ToString());
Headers.SetOrAddHeaderValue(KnownHeaders.ContentLength, value.ToString());
IsChunked = false;
}
else
{
Headers.RemoveHeader("content-length");
Headers.RemoveHeader(KnownHeaders.ContentLength);
}
}
}
......@@ -117,8 +117,8 @@ namespace Titanium.Web.Proxy.Http
/// </summary>
public string ContentType
{
get => Headers.GetHeaderValueOrNull("content-type");
set => Headers.SetOrAddHeaderValue("content-type", value);
get => Headers.GetHeaderValueOrNull(KnownHeaders.ContentType);
set => Headers.SetOrAddHeaderValue(KnownHeaders.ContentType, value);
}
/// <summary>
......@@ -128,19 +128,19 @@ namespace Titanium.Web.Proxy.Http
{
get
{
string headerValue = Headers.GetHeaderValueOrNull("transfer-encoding");
return headerValue != null && headerValue.ContainsIgnoreCase("chunked");
string headerValue = Headers.GetHeaderValueOrNull(KnownHeaders.TransferEncoding);
return headerValue != null && headerValue.ContainsIgnoreCase(KnownHeaders.TransferEncodingChunked);
}
set
{
if (value)
{
Headers.SetOrAddHeaderValue("transfer-encoding", "chunked");
Headers.SetOrAddHeaderValue(KnownHeaders.TransferEncoding, KnownHeaders.TransferEncodingChunked);
ContentLength = -1;
}
else
{
Headers.RemoveHeader("transfer-encoding");
Headers.RemoveHeader(KnownHeaders.TransferEncoding);
}
}
}
......@@ -152,8 +152,8 @@ namespace Titanium.Web.Proxy.Http
{
get
{
string headerValue = Headers.GetHeaderValueOrNull("expect");
return headerValue != null && headerValue.Equals("100-continue");
string headerValue = Headers.GetHeaderValueOrNull(KnownHeaders.Expect);
return headerValue != null && headerValue.Equals(KnownHeaders.Expect100Continue);
}
}
......@@ -241,14 +241,14 @@ namespace Titanium.Web.Proxy.Http
{
get
{
string headerValue = Headers.GetHeaderValueOrNull("upgrade");
string headerValue = Headers.GetHeaderValueOrNull(KnownHeaders.Upgrade);
if (headerValue == null)
{
return false;
}
return headerValue.Equals("websocket", StringComparison.CurrentCultureIgnoreCase);
return headerValue.Equals(KnownHeaders.UpgradeWebsocket, StringComparison.CurrentCultureIgnoreCase);
}
}
......
......@@ -42,7 +42,7 @@ namespace Titanium.Web.Proxy.Http
/// <summary>
/// Content encoding for this response
/// </summary>
public string ContentEncoding => Headers.GetHeaderValueOrNull("content-encoding")?.Trim();
public string ContentEncoding => Headers.GetHeaderValueOrNull(KnownHeaders.ContentEncoding)?.Trim();
/// <summary>
/// Http version
......@@ -94,11 +94,11 @@ namespace Titanium.Web.Proxy.Http
{
get
{
string headerValue = Headers.GetHeaderValueOrNull("connection");
string headerValue = Headers.GetHeaderValueOrNull(KnownHeaders.Connection);
if (headerValue != null)
{
if (headerValue.ContainsIgnoreCase("close"))
if (headerValue.ContainsIgnoreCase(KnownHeaders.ConnectionClose))
{
return false;
}
......@@ -111,7 +111,7 @@ namespace Titanium.Web.Proxy.Http
/// <summary>
/// Content type of this response
/// </summary>
public string ContentType => Headers.GetHeaderValueOrNull("content-type");
public string ContentType => Headers.GetHeaderValueOrNull(KnownHeaders.ContentType);
/// <summary>
/// Length of response body
......@@ -120,7 +120,7 @@ namespace Titanium.Web.Proxy.Http
{
get
{
string headerValue = Headers.GetHeaderValueOrNull("content-length");
string headerValue = Headers.GetHeaderValueOrNull(KnownHeaders.ContentLength);
if (headerValue == null)
{
......@@ -138,12 +138,12 @@ namespace Titanium.Web.Proxy.Http
{
if (value >= 0)
{
Headers.SetOrAddHeaderValue("content-length", value.ToString());
Headers.SetOrAddHeaderValue(KnownHeaders.ContentLength, value.ToString());
IsChunked = false;
}
else
{
Headers.RemoveHeader("content-length");
Headers.RemoveHeader(KnownHeaders.ContentLength);
}
}
}
......@@ -155,19 +155,19 @@ namespace Titanium.Web.Proxy.Http
{
get
{
string headerValue = Headers.GetHeaderValueOrNull("transfer-encoding");
return headerValue != null && headerValue.ContainsIgnoreCase("chunked");
string headerValue = Headers.GetHeaderValueOrNull(KnownHeaders.TransferEncoding);
return headerValue != null && headerValue.ContainsIgnoreCase(KnownHeaders.TransferEncodingChunked);
}
set
{
if (value)
{
Headers.SetOrAddHeaderValue("transfer-encoding", "chunked");
Headers.SetOrAddHeaderValue(KnownHeaders.TransferEncoding, KnownHeaders.TransferEncodingChunked);
ContentLength = -1;
}
else
{
Headers.RemoveHeader("transfer-encoding");
Headers.RemoveHeader(KnownHeaders.TransferEncoding);
}
}
}
......
......@@ -2,6 +2,7 @@
using System.Text;
using System.Threading.Tasks;
using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.Http;
namespace Titanium.Web.Proxy.Models
{
......@@ -56,7 +57,7 @@ namespace Titanium.Web.Proxy.Models
internal static HttpHeader GetProxyAuthorizationHeader(string userName, string password)
{
var result = new HttpHeader("Proxy-Authorization",
var result = new HttpHeader(KnownHeaders.ProxyAuthorization,
"Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes($"{userName}:{password}")));
return result;
}
......
......@@ -8,6 +8,7 @@ using System.Text;
using System.Threading.Tasks;
using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.Http;
using Titanium.Web.Proxy.Models;
namespace Titanium.Web.Proxy.Network.Tcp
......@@ -71,12 +72,12 @@ namespace Titanium.Web.Proxy.Network.Tcp
var writer = new HttpRequestWriter(stream, server.BufferSize);
await writer.WriteLineAsync($"CONNECT {remoteHostName}:{remotePort} HTTP/{httpVersion}");
await writer.WriteLineAsync($"Host: {remoteHostName}:{remotePort}");
await writer.WriteLineAsync("Connection: Keep-Alive");
await writer.WriteLineAsync($"{KnownHeaders.Connection}: {KnownHeaders.ConnectionKeepAlive}");
if (!string.IsNullOrEmpty(externalProxy.UserName) && externalProxy.Password != null)
{
await HttpHeader.ProxyConnectionKeepAlive.WriteToStreamAsync(writer);
await writer.WriteLineAsync("Proxy-Authorization" + ": Basic " +
await writer.WriteLineAsync(KnownHeaders.ProxyAuthorization + ": Basic " +
Convert.ToBase64String(Encoding.UTF8.GetBytes(
externalProxy.UserName + ":" + externalProxy.Password)));
}
......
......@@ -8,6 +8,7 @@ using Titanium.Web.Proxy.Exceptions;
using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.Http;
using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.Shared;
namespace Titanium.Web.Proxy
{
......@@ -20,37 +21,36 @@ namespace Titanium.Web.Proxy
return true;
}
var httpHeaders = session.WebSession.Request.Headers.ToArray();
var httpHeaders = session.WebSession.Request.Headers;
try
{
var header = httpHeaders.FirstOrDefault(t => t.Name == "Proxy-Authorization");
var header = httpHeaders.GetFirstHeader(KnownHeaders.ProxyAuthorization);
if (header == null)
{
session.WebSession.Response = await SendAuthentication407Response(clientStreamWriter, "Proxy Authentication Required");
return false;
}
string headerValue = header.Value.Trim();
if (!headerValue.StartsWith("basic", StringComparison.CurrentCultureIgnoreCase))
var headerValueParts = header.Value.Split(ProxyConstants.SpaceSplit);
if (headerValueParts.Length != 2 || !headerValueParts[0].Equals("basic", StringComparison.CurrentCultureIgnoreCase))
{
//Return not authorized
session.WebSession.Response = await SendAuthentication407Response(clientStreamWriter, "Proxy Authentication Invalid");
return false;
}
headerValue = headerValue.Substring(5).Trim();
string decoded = Encoding.UTF8.GetString(Convert.FromBase64String(headerValue));
if (decoded.Contains(":") == false)
string decoded = Encoding.UTF8.GetString(Convert.FromBase64String(headerValueParts[1]));
int colonIndex = decoded.IndexOf(':');
if (colonIndex == -1)
{
//Return not authorized
session.WebSession.Response = await SendAuthentication407Response(clientStreamWriter, "Proxy Authentication Invalid");
return false;
}
string username = decoded.Substring(0, decoded.IndexOf(':'));
string password = decoded.Substring(decoded.IndexOf(':') + 1);
string username = decoded.Substring(0, colonIndex);
string password = decoded.Substring(colonIndex + 1);
return await AuthenticateUserFunc(username, password);
}
catch (Exception e)
......@@ -72,8 +72,8 @@ namespace Titanium.Web.Proxy
StatusDescription = description
};
response.Headers.AddHeader("Proxy-Authenticate", $"Basic realm=\"{ProxyRealm}\"");
response.Headers.AddHeader("Proxy-Connection", "close");
response.Headers.AddHeader(KnownHeaders.ProxyAuthenticate, $"Basic realm=\"{ProxyRealm}\"");
response.Headers.AddHeader(KnownHeaders.ProxyConnection, KnownHeaders.ProxyConnectionClose);
await clientStreamWriter.WriteResponseAsync(response);
return response;
......
......@@ -6,7 +6,6 @@ using System.Net.Sockets;
using System.Security.Authentication;
using System.Threading.Tasks;
using StreamExtended;
using StreamExtended.Helpers;
using StreamExtended.Network;
using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Exceptions;
......@@ -597,7 +596,7 @@ namespace Titanium.Web.Proxy
switch (header.Name.ToLower())
{
//these are the only encoding this proxy can read
case "accept-encoding":
case KnownHeaders.AcceptEncoding:
header.Value = "gzip,deflate";
break;
}
......
......@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Http;
using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.Network.WinAuth;
......@@ -84,9 +85,9 @@ namespace Titanium.Web.Proxy
string scheme = authSchemes.FirstOrDefault(x => authHeader.Value.Equals(x, StringComparison.OrdinalIgnoreCase));
//clear any existing headers to avoid confusing bad servers
if (args.WebSession.Request.Headers.NonUniqueHeaders.ContainsKey("Authorization"))
if (args.WebSession.Request.Headers.NonUniqueHeaders.ContainsKey(KnownHeaders.Authorization))
{
args.WebSession.Request.Headers.NonUniqueHeaders.Remove("Authorization");
args.WebSession.Request.Headers.NonUniqueHeaders.Remove(KnownHeaders.Authorization);
}
//initial value will match exactly any of the schemes
......@@ -94,16 +95,16 @@ namespace Titanium.Web.Proxy
{
string clientToken = WinAuthHandler.GetInitialAuthToken(args.WebSession.Request.Host, scheme, args.Id);
var auth = new HttpHeader("Authorization", string.Concat(scheme, clientToken));
var auth = new HttpHeader(KnownHeaders.Authorization, string.Concat(scheme, clientToken));
//replace existing authorization header if any
if (args.WebSession.Request.Headers.Headers.ContainsKey("Authorization"))
if (args.WebSession.Request.Headers.Headers.ContainsKey(KnownHeaders.Authorization))
{
args.WebSession.Request.Headers.Headers["Authorization"] = auth;
args.WebSession.Request.Headers.Headers[KnownHeaders.Authorization] = auth;
}
else
{
args.WebSession.Request.Headers.Headers.Add("Authorization", auth);
args.WebSession.Request.Headers.Headers.Add(KnownHeaders.Authorization, auth);
}
//don't need to send body for Authorization request
......@@ -122,7 +123,7 @@ namespace Titanium.Web.Proxy
string clientToken = WinAuthHandler.GetFinalAuthToken(args.WebSession.Request.Host, serverToken, args.Id);
//there will be an existing header from initial client request
args.WebSession.Request.Headers.Headers["Authorization"] = new HttpHeader("Authorization", string.Concat(scheme, clientToken));
args.WebSession.Request.Headers.Headers[KnownHeaders.Authorization] = new HttpHeader(KnownHeaders.Authorization, string.Concat(scheme, clientToken));
//send body for final auth request
if (args.WebSession.Request.HasBody)
......
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