Proxy

HTTP Proxy based on C# to gain knowledge on the HTTP protocol and programming C# without tools. Functionality includes caching, basic authentication and content filtering.

proxy UI

Idea

During the course ".NET on the Server" it was decided that it was good practice to look under the hood and see how things are implemented. The teachers decided that the students needed to build a Proxy based on the TcpClient, because of the COVID-19 crisis it was decided that HttpListener was also allowed.

Requirements

The proxy needed to make us of multi threading, be HTTP 1.0 compliant and be usable for binary and text data. Besides the proxy itself, we needed to build a UI implemented with MVVM and WPF that did not stop responding while the proxy was under load.

Challenges

The use of middleware was not allowed. Some planning was required to make sure everything was executed in the proper order and in an understandable way. I first built a flowchart to have a general understanding of how the project needed to be structured.

proxy flowchart

To make sure everything is maintainable and extendable I used the Strategy Pattern in crusial areas such as the Cache Validator.

// ICachingStrategy.cs
public interface ICachingStrategy
{
    bool IsValid();
}

// CacheValidator.cs
public bool IsValid(IResponse response, ProxyConfig config)
{
    var headers = response.Headers;

    if (config.IsForcedTimeout)
    {
        return new ForcedStrategy(response, config.ForcedTimeout).IsValid();
    }

    if (!string.IsNullOrEmpty(headers["Cache-Control"]))
    { 
        return CacheControlStrategy(response).IsValid();
    }

    if (!string.IsNullOrEmpty(headers["Expires"]))
    {
        return new ExpiresStrategy(response).IsValid();
    }

    if (string.IsNullOrEmpty(headers["Last-Modified"]))
        throw new ArgumentException("no caching strategies found", nameof(headers));

    return new RevalidateStrategy(response).IsValid();
}

Repository

Technologies

  • C#
  • WPF
  • MVVM
  • UWP