/// <summary> /// Reads and parses the request body as a form. /// </summary> /// <returns>The parsed form data.</returns> internal static IFormCollection ReadForm(this OwinRequest request) { var form = request.Get <IFormCollection>("Microsoft.Owin.Form#collection"); if (form == null) { request.Body.Seek(0, SeekOrigin.Begin); string text; // Don't close, it prevents re-winding. using (var reader = new StreamReader(request.Body, Encoding.UTF8, detectEncodingFromByteOrderMarks: true, bufferSize: 4 * 1024, leaveOpen: true)) { text = reader.ReadToEnd(); } // re-wind for the next caller request.Body.Seek(0, SeekOrigin.Begin); form = OwinHelpers.GetForm(text); request.Set("Microsoft.Owin.Form#collection", form); } return(form); }
public static AppFunc Enable(AppFunc next, Func <IDictionary <string, object>, bool> requiresAuth, string realm, Func <string, UserPassword?> authenticator) { return(env => { if (!requiresAuth(env)) { return next(env); } var requestHeaders = (IDictionary <string, string[]>)env[OwinConstants.RequestHeaders]; var header = OwinHelpers.GetHeader(requestHeaders, "Authorization"); var parsedHeader = ParseDigestHeader(header); if (parsedHeader == null) { return Unauthorized(env, realm); } string user = parsedHeader["username"]; var pwd = authenticator(user); // TODO: check for increment of "nc" header value if (!pwd.HasValue || !IsValidResponse(pwd.Value.GetHA1(user, realm), (string)env[OwinConstants.RequestMethod], parsedHeader)) { return Unauthorized(env, realm); } env["gate.RemoteUser"] = user; return next(env); }); }
public async Task ReadForm(IDictionary <string, string[]> form) { using (var reader = new StreamReader(Body)) { string text = await reader.ReadToEndAsync(); OwinHelpers.ParseDelimited(text, new[] { '&' }, (name, value, state) => ((IDictionary <string, string[]>)state).Add(name, new[] { value }), form); } }
public async Task ReadForm(NameValueCollection form) { using (var reader = new StreamReader(Body)) { string text = await reader.ReadToEndAsync(); OwinHelpers.ParseDelimited(text, new[] { '&' }, (name, value, state) => ((NameValueCollection)state).Add(name, value), form); } }
public async Task ReadForm(Action <string, string, object> callback, object state) { using (var reader = new StreamReader(Body)) { string text = await reader.ReadToEndAsync(); OwinHelpers.ParseDelimited(text, new[] { '&' }, callback, state); } }
// Inspect the method and headers to see if this is a valid websocket request. // See RFC 6455 section 4.2.1. private static bool IsWebSocketRequest(IDictionary<string, object> env) { var requestHeaders = (IDictionary<string, string[]>)env[OwinConstants.RequestHeaders]; var webSocketUpgrade = OwinHelpers.GetHeaderSplit(requestHeaders, "Upgrade"); var webSocketVersion = OwinHelpers.GetHeader(requestHeaders, "Sec-WebSocket-Version"); var webSocketKey = OwinHelpers.GetHeader(requestHeaders, "Sec-WebSocket-Key"); return webSocketUpgrade != null && webSocketUpgrade.Contains("websocket", StringComparer.OrdinalIgnoreCase) && webSocketVersion != null && webSocketKey != null; }
public static NameValueCollection ParseNameValueCollection(string text) { var form = new NameValueCollection(); OwinHelpers.ParseDelimited( text, new[] { '&' }, (a, b, c) => ((NameValueCollection)c).Add(a, b), form); return(form); }
// Se the websocket response headers. // See RFC 6455 section 4.2.2. private static void SetWebSocketResponseHeaders(IDictionary<string, object> env, IDictionary<string, object> acceptOptions) { var requestHeaders = (IDictionary<string, string[]>)env[OwinConstants.RequestHeaders]; var webSocketKey = OwinHelpers.GetHeader(requestHeaders, "Sec-WebSocket-Key"); env[OwinConstants.ResponseReasonPhrase] = "Switching Protocols"; var responseHeaders = (IDictionary<string, string[]>)env[OwinConstants.ResponseHeaders]; OwinHelpers.AddHeader(responseHeaders, "Connection", "Upgrade"); OwinHelpers.AddHeader(responseHeaders, "Upgrade", "websocket"); var webSocketAccept = Convert.ToBase64String( SHA1.Create().ComputeHash(Encoding.Default.GetBytes(webSocketKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"))); OwinHelpers.AddHeader(responseHeaders, "Sec-WebSocket-Accept", webSocketAccept); }
/// <summary> /// Asynchronously reads and parses the request body as a form. /// </summary> /// <returns>The parsed form data.</returns> public async Task <IFormCollection> ReadFormAsync() { var form = Get <IFormCollection>("Microsoft.Owin.Form#collection"); if (form == null) { string text; using (var reader = new StreamReader(Body)) { text = await reader.ReadToEndAsync(); } form = OwinHelpers.GetForm(text); Set("Microsoft.Owin.Form#collection", form); } return(form); }
/// <summary> /// Asynchronously reads and parses the request body as a form. /// </summary> /// <returns>The parsed form data.</returns> public async Task <IFormCollection> ReadFormAsync() { var form = Get <IFormCollection>("Microsoft.Owin.Form#collection"); if (form == null) { string text; // Don't close, it prevents re-winding. using (var reader = new StreamReader(Body, Encoding.UTF8, detectEncodingFromByteOrderMarks: true, bufferSize: 4 * 1024, leaveOpen: true)) { text = await reader.ReadToEndAsync(); } form = OwinHelpers.GetForm(text); Set("Microsoft.Owin.Form#collection", form); } return(form); }
public static IDictionary <string, string> GetCookies(this OwinRequest request) { return(OwinHelpers.GetCookies(request)); }
public static OwinRequest ApplyMethodOverride(this OwinRequest request) { return(OwinHelpers.ApplyMethodOverride(request)); }
/// <summary> /// Quotes any values containing comas, and then coma joins all of the values. /// </summary> /// <param name="key">The header name.</param> /// <param name="values">The header values.</param> public void SetCommaSeparatedValues(string key, params string[] values) { OwinHelpers.SetHeaderJoined(Store, key, values); }
/// <summary> /// Sets a specific header value. /// </summary> /// <param name="key">The header name.</param> /// <param name="value">The header value.</param> public void Set(string key, string value) { OwinHelpers.SetHeader(Store, key, value); }
/// <summary> /// Add a new value. Appends to the header if already present /// </summary> /// <param name="key">The header name.</param> /// <param name="value">The header value.</param> public void Append(string key, string value) { OwinHelpers.AppendHeader(Store, key, value); }
/// <summary> /// Get the associated values from the collection without modification. /// </summary> /// <param name="key">The header name.</param> /// <returns>the associated value from the collection without modification, or null if the key is not present.</returns> public IList <string> GetValues(string key) { return(OwinHelpers.GetHeaderUnmodified(Store, key)); }
/// <summary> /// Sets a 302 response status code and the Location header. /// </summary> /// <param name="location">The location where to redirect the client.</param> public virtual void Redirect(string location) { StatusCode = 302; OwinHelpers.SetHeader(RawHeaders, Constants.Headers.Location, location); }
public static string GetMethodOverride(this OwinRequest request) { return(OwinHelpers.GetMethodOverride(request)); }
public static IDictionary <string, string[]> GetQuery(this OwinRequest request) { return(OwinHelpers.GetQuery(request)); }
/// <summary> /// Get the associated values from the collection without modification. /// </summary> /// <param name="key">The header name.</param> /// <returns>the associated value from the collection without modification, or null if the key is not present.</returns> public IList <string> GetValues(string key) => OwinHelpers.GetHeaderUnmodified(Store, key);
/// <summary> /// Get the associated value from the collection as a single string. /// </summary> /// <param name="key">The header name.</param> /// <returns>the associated value from the collection as a single string or null if the key is not present.</returns> public string Get(string key) { return(OwinHelpers.GetHeader(Store, key)); }
/// <summary> /// Quotes any values containing comas, and then coma joins all of the values with any existing values. /// </summary> /// <param name="key">The header name.</param> /// <param name="values">The header values.</param> public void AppendCommaSeparatedValues(string key, params string[] values) => OwinHelpers.AppendHeaderJoined(Store, key, values);
/// <summary> /// Get the associated values from the collection separated into individual values. /// Quoted values will not be split, and the quotes will be removed. /// </summary> /// <param name="key">The header name.</param> /// <returns>the associated values from the collection separated into individual values, or null if the key is not present.</returns> public IList <string> GetCommaSeparatedValues(string key) { IEnumerable <string> values = OwinHelpers.GetHeaderSplit(Store, key); return(values == null ? null : values.ToList()); }
/// <summary> /// Get the associated value from the collection as a single string. /// </summary> /// <param name="key">The header name.</param> /// <returns>the associated value from the collection as a single string or null if the key is not present.</returns> public string Get(string key) => OwinHelpers.GetHeader(Store, key);
/// <summary> /// Add new values. Each item remains a separate array entry. /// </summary> /// <param name="key">The header name.</param> /// <param name="values">The header values.</param> public void AppendValues(string key, params string[] values) { OwinHelpers.AppendHeaderUnmodified(Store, key, values); }
/// <summary> /// Get the associated value from the collection. Multiple values will be merged. /// Returns null if the key is not present. /// </summary> /// <param name="key"></param> /// <returns></returns> public string Get(string key) { return(OwinHelpers.GetJoinedValue(Store, key)); }
/// <summary> /// Sets the specified header values without modification. /// </summary> /// <param name="key">The header name.</param> /// <param name="values">The header values.</param> public void SetValues(string key, params string[] values) { OwinHelpers.SetHeaderUnmodified(Store, key, values); }
public static OwinResponse AddCookie(this OwinResponse response, string key, string value, CookieOptions options) { OwinHelpers.AddCookie(response, key, value, options); return(response); }
/// <summary> /// Parses an HTTP form body. /// </summary> /// <param name="text">The HTTP form body to parse.</param> /// <returns>The <see cref="T:Microsoft.Owin.IFormCollection" /> object containing the parsed HTTP form body.</returns> public static IFormCollection ParseForm(string text) { return(OwinHelpers.GetForm(text)); }
public static OwinResponse DeleteCookie(this OwinResponse response, string key, CookieOptions options) { OwinHelpers.DeleteCookie(response, key, options); return(response); }