Skip to content
This repository was archived by the owner on Jun 24, 2020. It is now read-only.

Repository files navigation

#HTTPortable Build status

About

HTTPortable provides a cross-platform, .NET based implementation of the HTTP protocol.

ON HOLD

Development of HTTPortable is currently on hold while I work out some problems with Txt.

Background and Goals

HTTP is typically spoken over TCP, but it is fundamentally transport agnostic.

Taken from RFC 7230:

HTTP messaging is independent of the underlying transport- or session-layer connection protocol(s). HTTP only presumes a reliable transport with in-order delivery of requests and the corresponding in-order delivery of responses.

Unfortunately, most HTTP libraries are hard-coded to use TCP/IP sockets. That includes the .NET framework's System.Net.WebRequest API and also the ASP.NET framework in its entirety.

The key goal for this project is to allow any System.IO.Stream to be used as the transport channel. This opens up tons of new possibilities. For example: HTTP over RFCOMM.

The secondary goal for this project is to work the same across all supported platforms. It is my opinion that you should only have to test managed code on one platform to be certain that it will work everywhere. That can be accomplished by only using managed code. This is different from other HTTP APIs in .NET that call into native APIs. For example: the HttpClient class in .NET Core uses WinHTTP on Windows and libcurl on Unix variants. Those two implementations behave differently, making it more expensive to write code for multiple platforms.

Specifications

Code is based on the latest RFCs regarding version HTTP/1.1.

Example

This code example demonstrates how to use the library to download a zip file that contains the most recent version of the code, from the GitHub back-end servers, over HTTP.

Points of interest:

  • This example is specific to desktop applications. Portable applications use a different set of classes to set up secure TCP connections.
  • The zip file is saved to the "Downloads" folder in the current user's profile directory.
  • GitHub includes a Content-Disposition header in the response that specifies a file name for the zip file.

Code

usingSystem;usingSystem.IO;usingSystem.Linq;usingSystem.Net.Mime;usingSystem.Net.Security;usingSystem.Net.Sockets;usingSystem.Threading;usingSystem.Threading.Tasks;usingHttp;usingHttp.Tcp;
classProgram{staticvoidMain(string[]args){newProgram().Download().Wait();}publicasyncTaskDownload(){// Create a new HTTP/1.1 requestvarrequest=newRequestMessage("GET","/StevenLiekens/http-client/zip/master",Version.Parse("1.1"));// Set the 'Host' headerrequest.Headers.Add(newHeader("Host"){"codeload.github.com"});// Set the 'User-Agent' headerrequest.Headers.Add(newHeader("User-Agent"){"https://github.com/StevenLiekens/http-client"});// Connect to GitHub over TCP/IPvartcpClient=newTcpClient();awaittcpClient.ConnectAsync("codeload.github.com",443);// Switch to SSLvarsslStream=newSslStream(tcpClient.GetStream());awaitsslStream.AuthenticateAsClientAsync("codeload.github.com");// Create a new user agent object for the given inbound and outbound streams (in/out are the same in this case)varuserAgent=newUserAgent(sslStream);// Asynchronously send the requestawaituserAgent.SendAsync(request,CancellationToken.None);// Asynchronously execute the given callback method for the responseawaituserAgent.ReceiveAsync(CancellationToken.None,(message,stream,cancellationToken)=>{returnTask.Run(()=>{// Get the file name from the response headersvarcontentDispositionHeader=message.Headers.First(h =>h.Name.Equals("Content-Disposition")).First();varcontentDisposition=newContentDisposition(contentDispositionHeader);vardownloadPath=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),"Downloads",contentDisposition.FileName);// Save the message body to diskusing(varfileStream=File.OpenWrite(downloadPath)){stream.CopyTo(fileStream);}},cancellationToken);});}}

About

RFCs 7230-7237

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages