Fix a few for HttpListenerContext.cs

This commit is contained in:
sta 2014-05-04 16:51:04 +09:00
parent 2f55d0e991
commit 4aa08ee7d4

View File

@ -8,7 +8,7 @@
* The MIT License * The MIT License
* *
* Copyright (c) 2005 Novell, Inc. (http://www.novell.com) * Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
* Copyright (c) 2012-2013 sta.blockhead * Copyright (c) 2012-2014 sta.blockhead
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -33,22 +33,19 @@
#region Authors #region Authors
/* /*
* Authors: * Authors:
* Gonzalo Paniagua Javier <gonzalo@novell.com> * - Gonzalo Paniagua Javier <gonzalo@novell.com>
*/ */
#endregion #endregion
using System; using System;
using System.Collections.Specialized;
using System.IO;
using System.Security.Principal; using System.Security.Principal;
using System.Text;
using WebSocketSharp.Net.WebSockets; using WebSocketSharp.Net.WebSockets;
namespace WebSocketSharp.Net namespace WebSocketSharp.Net
{ {
/// <summary> /// <summary>
/// Provides access to the HTTP request and response information used by the /// Provides a set of methods and properties used to access the HTTP request and response
/// <see cref="HttpListener"/>. /// information used by the <see cref="HttpListener"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// The HttpListenerContext class cannot be inherited. /// The HttpListenerContext class cannot be inherited.
@ -123,12 +120,10 @@ namespace WebSocketSharp.Net
#region Public Properties #region Public Properties
/// <summary> /// <summary>
/// Gets the <see cref="HttpListenerRequest"/> that contains the HTTP /// Gets the HTTP request information from a client.
/// request information from a client.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="HttpListenerRequest"/> that contains the HTTP request /// A <see cref="HttpListenerRequest"/> that represents the HTTP request.
/// information.
/// </value> /// </value>
public HttpListenerRequest Request { public HttpListenerRequest Request {
get { get {
@ -137,13 +132,10 @@ namespace WebSocketSharp.Net
} }
/// <summary> /// <summary>
/// Gets the <see cref="HttpListenerResponse"/> that contains the HTTP /// Gets the HTTP response information used to send to the client.
/// response information to send to the client in response to the client's
/// request.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="HttpListenerResponse"/> that contains the HTTP response /// A <see cref="HttpListenerResponse"/> that represents the HTTP response to send.
/// information.
/// </value> /// </value>
public HttpListenerResponse Response { public HttpListenerResponse Response {
get { get {
@ -152,11 +144,10 @@ namespace WebSocketSharp.Net
} }
/// <summary> /// <summary>
/// Gets the client information (identity, authentication information, and /// Gets the client information (identity, authentication, and security roles).
/// security roles).
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="IPrincipal"/> contains the client information. /// A <see cref="IPrincipal"/> that represents the client information.
/// </value> /// </value>
public IPrincipal User { public IPrincipal User {
get { get {
@ -169,7 +160,7 @@ namespace WebSocketSharp.Net
#region Internal Methods #region Internal Methods
internal void SetUser ( internal void SetUser (
AuthenticationSchemes expectedScheme, AuthenticationSchemes scheme,
string realm, string realm,
Func<IIdentity, NetworkCredential> credentialsFinder) Func<IIdentity, NetworkCredential> credentialsFinder)
{ {
@ -177,45 +168,44 @@ namespace WebSocketSharp.Net
if (authRes == null) if (authRes == null)
return; return;
var identity = authRes.ToIdentity (); var id = authRes.ToIdentity ();
if (identity == null) if (id == null)
return; return;
NetworkCredential credentials = null; NetworkCredential cred = null;
try { try {
credentials = credentialsFinder (identity); cred = credentialsFinder (id);
} }
catch { catch {
} }
if (credentials == null) if (cred == null)
return; return;
var valid = expectedScheme == AuthenticationSchemes.Basic var valid = scheme == AuthenticationSchemes.Basic
? ((HttpBasicIdentity) identity).Password == credentials.Password ? ((HttpBasicIdentity) id).Password == cred.Password
: expectedScheme == AuthenticationSchemes.Digest : scheme == AuthenticationSchemes.Digest
? ((HttpDigestIdentity) identity).IsValid ( ? ((HttpDigestIdentity) id).IsValid (
credentials.Password, realm, _request.HttpMethod, null) cred.Password, realm, _request.HttpMethod, null)
: false; : false;
if (valid) if (valid)
_user = new GenericPrincipal (identity, credentials.Roles); _user = new GenericPrincipal (id, cred.Roles);
} }
#endregion #endregion
#region Public Method #region Public Methods
/// <summary> /// <summary>
/// Accepts a WebSocket connection request. /// Accepts a WebSocket connection request.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A <see cref="HttpListenerWebSocketContext"/> that contains a WebSocket /// A <see cref="HttpListenerWebSocketContext"/> that represents the WebSocket connection
/// connection request information. /// request.
/// </returns> /// </returns>
/// <param name="logger"> /// <param name="logger">
/// A <see cref="Logger"/> that provides the logging functions used in the /// A <see cref="Logger"/> that provides the logging functions used in the WebSocket attempts.
/// WebSocket attempts.
/// </param> /// </param>
public HttpListenerWebSocketContext AcceptWebSocket (Logger logger) public HttpListenerWebSocketContext AcceptWebSocket (Logger logger)
{ {