Here’s a few updates coming your way in ASP.NET Web API. The code has been checked into our CodePlex source code repository but hasn’t yet been released as part of our official installer. Think of this as a sneak-peak for you to try out and comment on as part of our source code repository (see Getting Started With ASP.NET Web Stack Source on CodePlex for details).
Until now the HTTP Cookie and Set-Cookie headers were only exposed as raw strings and not structured classes in the HttpRequestMessage and HttpResponseMessage classes. This made it cumbersome and error prone to work with cookies in ASP.NET Web API. To fix this we introduced two new classes called CookieHeaderValue and CookieState that follow the RFC 6265 HTTP State Management Mechanism. Each Cookie or Set-Cookie header is represented as one CookieHeaderValue. A CookieHeaderValue contains information about the domain, path, expiration, and other common cookie information as well as one or more CookieStates. Each CookieState contains a name and whatever state is associate with that name. This representation allows for multiple related CookieStates to be carried within the same Cookie header while still providing separation between each cookie state.
To see how this works, look at the sample cookie exchange where a server sends a cookie to the client using the Set-Cookie header including the cookie state as well as domain and expiration information:
- Set-Cookie: StateA=n1=v1&n2=v2; StateZ=n3=v3&n4=v4; Domain=domain1; Path=path1; Expires=Mon, 23 Apr 2012 08:49:37 GMT
The client then returns the cookie on a subsequent request passing just the cookie state:
- Cookie: StateA=n1=v1&n2=v2; StateZ=n3=v3&n4=v4
In this example, there is one CookieHeaderValue with two CookieStates in the Set-Cookie response and also in the subsequent Cookie request. The CookieStates have names StateA and StateZ respectively. The CookieState names are followed by a value consisting of name-value pairs encoded as URL-encoded form data (also known as application/x-www-form-urlencoded) so that you can pass relatively structured data back and forth. In the above example, StateA has two name/value pairs (n1=v1 and n2=v2).
Getting all the CookieHeaderValues from a request:
- var cookies = Request.Headers.GetCookies();
Getting a CookieHeaderValue with a particular CookieState name:
- var cookie = Request.Headers.GetCookies(“StateA”);
Adding one or more Set-Cookie headers to a response
You can access the CookieState using C# indexers. For example, the following would return “v2”:
- var value = cookie[“StateA”][“n2”];
The HttpContent model supports writing data by either pushing data to the output stream directly or pulling data from an input stream and then copying it to the output stream (see Push and Pull Streams using HttpClient). Pull is supported out of the box in the form of StreamContent but until there was no equivalent built-in support for pushing data.
The PushStreamContent class enables scenarios where a data producer wants to write directly (either synchronously or asynchronously) using a stream. When the PushStreamContent is ready to accept data it calls out to an action with the output stream. The developer can then write to the stream for as long as necessary and close the stream when writing has completed. The PushStreamContent detects the closing of the stream and completes the underlying asynchronous Task for writing out the content.
PushStreamContent is entirely asynchronous and does not block any threads. It is up to the user of the PushStreamContent to write data either synchronously or asynchronously which of course then will determine whether a thread is blocked or not. For more details, see this commit. Here’s a sample push controller which writes out the plaintext string “Here’s some more data” every second. On the wire this will be sent using HTTP Chunked Encoding where each string will be an individual chunk.
Buffered Media Type Formatter
The BufferedMediaTypeFormatter provides a convenient way for writing a MediaTypeFormatter that primarily is writing or reading small, synchronous pieces of data. This is for example the case by many serializers which don’t quite support asynchronous reading and writing. Instead of having to implement the Task-oriented MediaTypeFormatter for reading and writing the BufferedMediaTypeFormatter exposes a simplified model backed by a buffer that is well suited to handle many small reads or writes. For more details, see this commit.
Below is a sample plaintext formatter written using the BufferedMediaTypeFormatter which supports writing or reading plain text using either UTF8 or UTF-16 as determined automatically by the built-in content negotiation algorithm (see ASP.NET Web API Content Negotiation and Accept-Charset):