2 // System.Net.HttpListenerContext
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
7 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 using System.ComponentModel;
29 using System.Net.Mime;
34 using System.Collections.Specialized;
36 using System.Security.Principal;
40 using System.Net.Sockets;
41 using System.Net.WebSockets;
42 using System.Threading.Tasks;
44 namespace System.Net {
45 public sealed class HttpListenerContext {
46 HttpListenerRequest request;
47 HttpListenerResponse response;
52 internal HttpListener Listener;
54 internal HttpListenerContext (HttpConnection cnc)
57 request = new HttpListenerRequest (this);
58 response = new HttpListenerResponse (this);
61 internal int ErrorStatus {
62 get { return err_status; }
63 set { err_status = value; }
66 internal string ErrorMessage {
68 set { error = value; }
71 internal bool HaveError {
72 get { return (error != null); }
75 internal HttpConnection Connection {
79 public HttpListenerRequest Request {
80 get { return request; }
83 public HttpListenerResponse Response {
84 get { return response; }
87 public IPrincipal User {
91 internal void ParseAuthentication (AuthenticationSchemes expectedSchemes) {
92 if (expectedSchemes == AuthenticationSchemes.Anonymous)
95 // TODO: Handle NTLM/Digest modes
96 string header = request.Headers ["Authorization"];
97 if (header == null || header.Length < 2)
100 string [] authenticationData = header.Split (new char [] {' '}, 2);
101 if (string.Compare (authenticationData [0], "basic", true) == 0) {
102 user = ParseBasicAuthentication (authenticationData [1]);
104 // TODO: throw if malformed -> 400 bad request
107 internal IPrincipal ParseBasicAuthentication (string authData) {
109 // Basic AUTH Data is a formatted Base64 String
110 //string domain = null;
112 string password = null;
114 string authString = System.Text.Encoding.Default.GetString (Convert.FromBase64String (authData));
116 // The format is DOMAIN\username:password
117 // Domain is optional
119 pos = authString.IndexOf (':');
121 // parse the password off the end
122 password = authString.Substring (pos+1);
124 // discard the password
125 authString = authString.Substring (0, pos);
127 // check if there is a domain
128 pos = authString.IndexOf ('\\');
131 //domain = authString.Substring (0, pos);
132 user = authString.Substring (pos);
137 HttpListenerBasicIdentity identity = new HttpListenerBasicIdentity (user, password);
138 // TODO: What are the roles MS sets
139 return new GenericPrincipal (identity, new string [0]);
140 } catch (Exception) {
141 // Invalid auth data is swallowed silently
146 public Task<HttpListenerWebSocketContext> AcceptWebSocketAsync (string subProtocol)
148 return AcceptWebSocketAsync (subProtocol, System.Net.WebSockets.WebSocket.DefaultKeepAliveInterval);
151 public Task<HttpListenerWebSocketContext> AcceptWebSocketAsync (string subProtocol, TimeSpan keepAliveInterval)
153 // Default receiveBuffersize is documented on MSDN Library.
154 // http://msdn.microsoft.com/ja-jp/library/hh159274(v=vs.110).aspx
155 return AcceptWebSocketAsync (subProtocol, 16385, keepAliveInterval);
158 public async Task<HttpListenerWebSocketContext> AcceptWebSocketAsync (string subProtocol, int receiveBufferSize, TimeSpan keepAliveInterval)
160 if (subProtocol != null && subProtocol == "") {
161 throw new ArgumentException ("subProtocol must not empty string");
163 if (receiveBufferSize < 16 || receiveBufferSize > 64 * 1024) {
164 throw new ArgumentOutOfRangeException ("receiveBufferSize should be >=16 and <=64K bytes");
166 if (!request.IsWebSocketRequest) {
167 throw new WebSocketException ("Request is not WebSocket Handshake");
169 string secKey = request.Headers ["Sec-WebSocket-Key"];
170 if (secKey == null) {
171 throw new WebSocketException ("Request doesn't contain Sec-WebSocket-Key header");
173 string origin = request.Headers ["Origin"];
174 if (origin == null) {
175 throw new WebSocketException ("Request doesn't contain Origin header");
177 string acceptKey = StreamWebSocket.CreateAcceptKey (secKey);
178 ArraySegment<byte> preloaded;
179 var stream = new NetworkStream (cnc.Hijack (out preloaded));
180 string header = "HTTP/1.1 101 Switching Protocols\r\n" +
181 "Upgrade: websocket\r\n" +
182 "Connection: Upgrade\r\n" +
183 "Sec-WebSocket-Accept: " + acceptKey + "\r\n\r\n";
184 var headerBytes = Encoding.ASCII.GetBytes (header);
185 await stream.WriteAsync (headerBytes, 0, headerBytes.Length);
186 var ws = new StreamWebSocket (stream, stream, null, subProtocol, false, preloaded);
187 return new HttpListenerWebSocketContext (ws, request, user);
190 public Task<HttpListenerWebSocketContext> AcceptWebSocketAsync (string subProtocol, int receiveBufferSize, TimeSpan keepAliveInterval, ArraySegment<byte> internalBuffer)
192 return AcceptWebSocketAsync (subProtocol, receiveBufferSize, keepAliveInterval);