Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
//
// AssemblyInfo.cs
//
// Author:
// Gonzalo Paniagua (gonzalo@ximian.com)
//
// (C) 2003 Novell, Inc. http://www.novell.com
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Reflection;
using System.Runtime.CompilerServices;
[assembly: AssemblyVersion (Consts.FxVersion)]
[assembly: AssemblyTitle("Mono.Http.dll")]
[assembly: AssemblyDescription("Http and ASP.NET utilities")]
[assembly: AssemblyConfiguration("Development version")]
[assembly: AssemblyCompany("MONO development team")]
[assembly: AssemblyProduct("MONO CLI")]
[assembly: AssemblyCopyright("(c) 2003 Various Authors")]
[assembly: AssemblyDelaySign(true)]
[assembly: AssemblyKeyFile("../mono.pub")]

View File

@@ -0,0 +1,4 @@
2003-12-12 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* AssemblyInfo.cs: assembly stuff.

View File

@@ -0,0 +1,9 @@
2003-12-12 Pedro Martínez Juliá <yoros@wanadoo.es>
* Mono.Http.dll.sources: Added the new file
Mono.Http.Modules/AcceptEncodingModule.cs.
2003-07-24 Gonzalo Paniagua Javier <gonzalo@ximian.com>
Initial check-in.

View File

@@ -0,0 +1,16 @@
thisdir = class/Mono.Http
SUBDIRS =
include ../../build/rules.make
LIBRARY = Mono.Http.dll
LIB_MCS_FLAGS = -r:$(corlib) \
-r:System.dll \
-r:System.Xml.dll \
-r:System.Web.dll \
-r:ICSharpCode.SharpZipLib.dll \
-r:Mono.Security.dll \
-nowarn:618
NO_TEST = yes
include ../../build/library.make

View File

@@ -0,0 +1,128 @@
//
// AcceptEncodingConfig.cs
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (c) 2003 Ximian, Inc (http://www.ximian.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Web;
namespace Mono.Http.Configuration
{
public class AcceptEncodingConfig
{
Hashtable tbl = CollectionsUtil.CreateCaseInsensitiveHashtable ();
public AcceptEncodingConfig () : this (null)
{
}
public AcceptEncodingConfig (AcceptEncodingConfig parent)
{
if (parent == null)
return;
// FIXME: copy parent's config
}
public void Add (string encoding, string type)
{
tbl [encoding] = Type.GetType (type);
}
public bool SetFilter (HttpResponse response, string acceptEncoding)
{
if (acceptEncoding == null)
return false;
acceptEncoding = acceptEncoding.Trim ();
if (acceptEncoding == "")
return false;
string [] parts = null;
if (acceptEncoding.IndexOf (';') != -1)
parts = acceptEncoding.Split (';');
else
parts = acceptEncoding.Split (',');
string encoding;
float weight = 0.0f;
float current = 0.0f;
Type type = null;
string name = null;
int i = 0;
foreach (string s in parts) {
encoding = null;
ParseValue (s, ref encoding, ref weight);
if (encoding != null && weight > current && tbl.Contains (encoding)) {
type = tbl [encoding] as Type;
current = weight;
name = encoding;
}
i++;
}
if (type == null)
return false;
Stream filter = response.Filter;
response.Filter = (Stream) Activator.CreateInstance (type, new object [] {filter});
response.AppendHeader ("Content-Encoding", name);
return true;
}
public void Clear ()
{
tbl.Clear ();
}
static void ParseValue (string s, ref string encoding, ref float weight)
{
//FIXME: make it more spec compliant
string [] parts = s.Trim ().Split (',');
if (parts.Length == 1) {
encoding = parts [0].Trim ();
weight = 1.0f;
} else if (parts.Length == 2) {
encoding = parts [0].Trim ();
try {
int i = parts [1].IndexOf ('=');
if (i != -1)
weight = Convert.ToSingle (parts [1].Substring (i + 1));
} catch {
weight = 0.0f;
}
} else {
//ignore
}
}
}
}

View File

@@ -0,0 +1,117 @@
//
// AcceptEncodingSectionHandler.cs
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (c) 2003 Ximian, Inc (http://www.ximian.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Configuration;
using System.IO;
using System.Xml;
namespace Mono.Http.Configuration
{
public class AcceptEncodingSectionHandler : IConfigurationSectionHandler
{
public object Create (object parent, object configContext, XmlNode section)
{
AcceptEncodingConfig cfg = new AcceptEncodingConfig (parent as AcceptEncodingConfig);
if (section.Attributes != null && section.Attributes.Count != 0)
ThrowException ("Unrecognized attribute", section);
XmlNodeList nodes = section.ChildNodes;
foreach (XmlNode child in nodes) {
XmlNodeType ntype = child.NodeType;
if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
continue;
if (ntype != XmlNodeType.Element)
ThrowException ("Only elements allowed", child);
string name = child.Name;
if (name == "clear") {
if (child.Attributes != null && child.Attributes.Count != 0)
ThrowException ("Unrecognized attribute", child);
cfg.Clear ();
continue;
}
if (name != "add")
ThrowException ("Unexpected element", child);
string encoding = ExtractAttributeValue ("encoding", child, false);
string type = ExtractAttributeValue ("type", child, false);
string disabled = ExtractAttributeValue ("disabled", child, true);
if (disabled != null && disabled == "yes")
continue;
if (child.Attributes != null && child.Attributes.Count != 0)
ThrowException ("Unrecognized attribute", child);
cfg.Add (encoding, type);
}
return cfg;
}
static void ThrowException (string msg, XmlNode node)
{
if (node != null && node.Name != String.Empty)
msg = msg + " (node name: " + node.Name + ") ";
throw new ConfigurationException (msg, node);
}
static string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
{
if (node.Attributes == null) {
if (optional)
return null;
ThrowException ("Required attribute not found: " + attKey, node);
}
XmlNode att = node.Attributes.RemoveNamedItem (attKey);
if (att == null) {
if (optional)
return null;
ThrowException ("Required attribute not found: " + attKey, node);
}
string value = att.Value;
if (value == String.Empty) {
string opt = optional ? "Optional" : "Required";
ThrowException (opt + " attribute is empty: " + attKey, node);
}
return value;
}
}
}

View File

@@ -0,0 +1,5 @@
2003-12-12 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* AcceptEncodingConfig.cs:
* AcceptEncodingSectionHandler.cs: moved these files here.

View File

@@ -0,0 +1,67 @@
//
// AcceptEncodingModule.cs
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (c) 2003 Ximian, Inc (http://www.ximian.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Configuration;
using System.IO;
using System.Web;
using Mono.Http.Configuration;
namespace Mono.Http.Modules
{
public class AcceptEncodingModule : IHttpModule
{
static readonly string configSection = "mono.aspnet/acceptEncoding";
AcceptEncodingConfig config;
public void Init (HttpApplication app)
{
app.BeginRequest += new EventHandler (CheckIfAddFilter);
}
public void Dispose ()
{
}
void CheckIfAddFilter (object sender, EventArgs args)
{
HttpApplication app = (HttpApplication) sender;
HttpRequest request = app.Request;
HttpResponse response = app.Response;
if (config == null)
config = (AcceptEncodingConfig) app.Context.GetConfig (configSection);
if (config != null)
config.SetFilter (response, request.Headers ["Accept-Encoding"]);
}
}
}

View File

@@ -0,0 +1,118 @@
//
// Abstract Authentication implementation
//
// Authors:
// Greg Reinacker (gregr@rassoc.com)
// Sebastien Pouliot (spouliot@motus.com)
//
// Copyright 2002-2003 Greg Reinacker, Reinacker & Associates, Inc. All rights reserved.
// Portions (C) 2003 Motus Technologies Inc. (http://www.motus.com)
//
// Based on "DigestAuthenticationModule.cs". Original source code available at
// http://www.rassoc.com/gregr/weblog/stories/2002/07/09/webServicesSecurityHttpDigestAuthenticationWithoutActiveDirectory.html
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Configuration;
using System.Web;
namespace Mono.Http.Modules
{
abstract public class AuthenticationModule : IHttpModule
{
string authMethod;
public AuthenticationModule (string authenticationMethod)
{
authMethod = authenticationMethod;
}
public string AuthenticationMethod {
get { return authMethod; }
}
#region IHttpModule Members
public virtual void Init (HttpApplication context)
{
context.AuthenticateRequest += new EventHandler (this.OnAuthenticateRequest);
context.EndRequest += new EventHandler (this.OnEndRequest);
}
public virtual void Dispose () {}
#endregion
#region Event Handlers
public virtual void OnAuthenticateRequest (object source, EventArgs eventArgs)
{
if (!AuthenticationRequired)
return;
HttpApplication app = (HttpApplication) source;
string authdata = Authorization (app, AuthenticationMethod);
if ((authdata == null) || (!AcceptCredentials (app, authdata))) {
DenyAccess (app);
return;
}
}
abstract public void OnEndRequest (object source, EventArgs eventArgs);
#endregion
abstract protected bool AcceptCredentials (HttpApplication app, string authentication);
protected bool AuthenticationRequired {
get { return (AuthenticationMethod == ConfigurationSettings.AppSettings ["Authentication"]); }
}
protected void DenyAccess (HttpApplication app)
{
app.Response.StatusCode = 401;
app.Response.StatusDescription = "Access Denied";
// Write to response stream as well, to give user visual
// indication of error during development
app.Response.Write ("401 Access Denied");
app.CompleteRequest ();
}
protected string Authorization (HttpApplication app, string authenticationMethod)
{
string autz = app.Request.Headers ["Authorization"];
if ((autz == null) || (autz.Length == 0)) {
// No credentials; anonymous request
return null;
}
if (autz.ToUpper ().StartsWith (authenticationMethod.ToUpper ())) {
return autz.Substring (authenticationMethod.Length + 1);
}
return null;
}
}
}

View File

@@ -0,0 +1,108 @@
//
// Basic Authentication implementation
//
// Authors:
// Greg Reinacker (gregr@rassoc.com)
// Sebastien Pouliot (spouliot@motus.com)
//
// Copyright 2002-2003 Greg Reinacker, Reinacker & Associates, Inc. All rights reserved.
// Portions (C) 2003 Motus Technologies Inc. (http://www.motus.com)
//
// Based on "DigestAuthenticationModule.cs". Original source code available at
// http://www.rassoc.com/gregr/weblog/stories/2002/07/09/webServicesSecurityHttpDigestAuthenticationWithoutActiveDirectory.html
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Configuration;
using System.IO;
using System.Security.Principal;
using System.Text;
using System.Web;
using System.Xml;
namespace Mono.Http.Modules
{
public class BasicAuthenticationModule : AuthenticationModule
{
static char[] separator = {':'};
public BasicAuthenticationModule () : base ("Basic") {}
protected override bool AcceptCredentials (HttpApplication app, string authentication)
{
byte[] userpass = Convert.FromBase64String (authentication);
string[] up = Encoding.UTF8.GetString (userpass).Split (separator);
string username = up [0];
string password = up [1];
string userFileName = app.Request.MapPath (ConfigurationSettings.AppSettings ["Basic.Users"]);
if (userFileName == null || !File.Exists (userFileName))
return false;
XmlDocument userDoc = new XmlDocument ();
userDoc.Load (userFileName);
string xPath = String.Format ("/users/user[@name='{0}']", username);
XmlNode user = userDoc.SelectSingleNode (xPath);
if (user == null)
return false;
XmlAttribute att = user.Attributes ["password"];
if (att == null || password != att.Value)
return false;
XmlNodeList roleNodes = user.SelectNodes ("role");
string[] roles = new string [roleNodes.Count];
int i = 0;
foreach (XmlNode xn in roleNodes) {
XmlAttribute rolename = xn.Attributes ["name"];
if (rolename == null)
continue;
roles [i++] = rolename.Value;
}
app.Context.User = new GenericPrincipal (new GenericIdentity (username, AuthenticationMethod), roles);
return true;
}
#region Event Handlers
// We add the WWW-Authenticate header here, so if an authorization
// fails elsewhere than in this module, we can still request authentication
// from the client.
public override void OnEndRequest (object source, EventArgs eventArgs)
{
HttpApplication app = (HttpApplication) source;
if (app.Response.StatusCode != 401 || !AuthenticationRequired)
return;
string realm = ConfigurationSettings.AppSettings ["Basic.Realm"];
string challenge = String.Format ("{0} realm=\"{1}\"", AuthenticationMethod, realm);
app.Response.AppendHeader ("WWW-Authenticate", challenge);
}
#endregion
}
}

View File

@@ -0,0 +1,23 @@
2004-06-07 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* AcceptEncodingModule.cs: don't cause nullref if there's no
<mono.aspnet> section and the module is loaded.
2004-04-28 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* DigestAuthenticationModule.cs: introduces new overridable method
GetUserByName(), which by default works exactly like it used to, but
inherited classes can implement their own version. Patch by Tambet Ingo.
2003-12-15 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* AuthenticationModule.cs:
* BasicAuthenticationModule.cs:
* DigestAuthenticationModule.cs: new modules to do Basic and Digest
authentication.
* samples/auth.xml: sample user/password file for the modules.
2003-12-12 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* AcceptEncodingModule.cs: moved this file here.

View File

@@ -0,0 +1,244 @@
//
// Digest Authentication implementation
//
// Authors:
// Greg Reinacker (gregr@rassoc.com)
// Sebastien Pouliot (spouliot@motus.com)
//
// Copyright 2002-2003 Greg Reinacker, Reinacker & Associates, Inc. All rights reserved.
// Portions (C) 2003 Motus Technologies Inc. (http://www.motus.com)
//
// Original source code available at
// http://www.rassoc.com/gregr/weblog/stories/2002/07/09/webServicesSecurityHttpDigestAuthenticationWithoutActiveDirectory.html
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text;
using System.Web;
using System.Xml;
namespace Mono.Http.Modules
{
public class DigestAuthenticationModule : AuthenticationModule
{
// TODO: Digest.Nonce.Lifetime="0" Never expires
static int nonceLifetime = 60;
static char[] trim = {'='};
public DigestAuthenticationModule () : base ("Digest") {}
protected virtual bool IsValidNonce (string nonce)
{
DateTime expireTime;
// pad nonce on the right with '=' until length is a multiple of 4
int numPadChars = nonce.Length % 4;
if (numPadChars > 0)
numPadChars = 4 - numPadChars;
string newNonce = nonce.PadRight(nonce.Length + numPadChars, '=');
try {
byte[] decodedBytes = Convert.FromBase64String(newNonce);
string expireStr = new ASCIIEncoding().GetString(decodedBytes);
expireTime = DateTime.Parse(expireStr);
}
catch (FormatException) {
return false;
}
return (DateTime.Now <= expireTime);
}
protected virtual bool GetUserByName (HttpApplication app, string username,
out string password, out string[] roles)
{
password = String.Empty;
roles = new string[0];
string userFileName = app.Request.MapPath (ConfigurationSettings.AppSettings ["Digest.Users"]);
if (userFileName == null || !File.Exists (userFileName))
return false;
XmlDocument userDoc = new XmlDocument ();
userDoc.Load (userFileName);
string xPath = String.Format ("/users/user[@name='{0}']", username);
XmlNode user = userDoc.SelectSingleNode (xPath);
if (user == null)
return false;
password = user.Attributes ["password"].Value;
XmlNodeList roleNodes = user.SelectNodes ("role");
roles = new string [roleNodes.Count];
int i = 0;
foreach (XmlNode xn in roleNodes)
roles [i++] = xn.Attributes ["name"].Value;
return true;
}
protected override bool AcceptCredentials (HttpApplication app, string authentication)
{
// digest
ListDictionary reqInfo = new ListDictionary ();
string[] elems = authentication.Split( new char[] {','});
foreach (string elem in elems) {
// form key="value"
string[] parts = elem.Split (new char[] {'='}, 2);
string key = parts [0].Trim (new char[] {' ','\"'});
string val = parts [1].Trim (new char[] {' ','\"'});
reqInfo.Add (key,val);
}
string username = (string) reqInfo ["username"];
string password;
string[] roles;
if (!GetUserByName (app, username, out password, out roles))
return false;
string realm = ConfigurationSettings.AppSettings ["Digest.Realm"];
// calculate the Digest hashes
// A1 = unq(username-value) ":" unq(realm-value) ":" passwd
string A1 = String.Format ("{0}:{1}:{2}", username, realm, password);
// H(A1) = MD5(A1)
string HA1 = GetMD5HashBinHex (A1);
// A2 = Method ":" digest-uri-value
string A2 = String.Format ("{0}:{1}", app.Request.HttpMethod, (string)reqInfo["uri"]);
// H(A2)
string HA2 = GetMD5HashBinHex(A2);
// KD(secret, data) = H(concat(secret, ":", data))
// if qop == auth:
// request-digest = <"> < KD ( H(A1), unq(nonce-value)
// ":" nc-value
// ":" unq(cnonce-value)
// ":" unq(qop-value)
// ":" H(A2)
// ) <">
// if qop is missing,
// request-digest = <"> < KD ( H(A1), unq(nonce-value) ":" H(A2) ) > <">
string unhashedDigest;
if (reqInfo["qop"] != null) {
unhashedDigest = String.Format("{0}:{1}:{2}:{3}:{4}:{5}",
HA1,
(string)reqInfo["nonce"],
(string)reqInfo["nc"],
(string)reqInfo["cnonce"],
(string)reqInfo["qop"],
HA2);
}
else {
unhashedDigest = String.Format("{0}:{1}:{2}",
HA1,
(string)reqInfo["nonce"],
HA2);
}
string hashedDigest = GetMD5HashBinHex (unhashedDigest);
bool isNonceStale = !IsValidNonce((string)reqInfo["nonce"]);
app.Context.Items["staleNonce"] = isNonceStale;
bool result = (((string)reqInfo["response"] == hashedDigest) && (!isNonceStale));
if (result) {
IIdentity id = new GenericIdentity (username, AuthenticationMethod);
app.Context.User = new GenericPrincipal (id, roles);
}
return result;
}
#region Event Handlers
public override void OnEndRequest(object source, EventArgs eventArgs)
{
// We add the WWW-Authenticate header here, so if an authorization
// fails elsewhere than in this module, we can still request authentication
// from the client.
HttpApplication app = (HttpApplication) source;
if (app.Response.StatusCode != 401 || !AuthenticationRequired)
return;
string realm = ConfigurationSettings.AppSettings ["Digest.Realm"];
string nonce = GetCurrentNonce ();
bool isNonceStale = false;
object staleObj = app.Context.Items ["staleNonce"];
if (staleObj != null)
isNonceStale = (bool)staleObj;
StringBuilder challenge = new StringBuilder ("Digest realm=\"");
challenge.Append(realm);
challenge.Append("\"");
challenge.Append(", nonce=\"");
challenge.Append(nonce);
challenge.Append("\"");
challenge.Append(", opaque=\"0000000000000000\"");
challenge.Append(", stale=");
challenge.Append(isNonceStale ? "true" : "false");
challenge.Append(", algorithm=MD5");
challenge.Append(", qop=\"auth\"");
app.Response.AppendHeader("WWW-Authenticate", challenge.ToString());
app.Response.StatusCode = 401;
}
#endregion
private string GetMD5HashBinHex (string toBeHashed)
{
MD5 hash = MD5.Create ();
byte[] result = hash.ComputeHash (Encoding.ASCII.GetBytes (toBeHashed));
StringBuilder sb = new StringBuilder ();
foreach (byte b in result)
sb.Append (b.ToString ("x2"));
return sb.ToString ();
}
protected virtual string GetCurrentNonce ()
{
DateTime nonceTime = DateTime.Now.AddSeconds (nonceLifetime);
byte[] expireBytes = Encoding.ASCII.GetBytes (nonceTime.ToString ("G"));
string nonce = Convert.ToBase64String (expireBytes);
// nonce can't end in '=', so trim them from the end
nonce = nonce.TrimEnd (trim);
return nonce;
}
}
}

View File

@@ -0,0 +1,13 @@
Assembly/AssemblyInfo.cs
../../build/common/Consts.cs
Mono.Http/GZipWebRequest.cs
Mono.Http/GZipWebResponse.cs
Mono.Http/GZipWriteFilter.cs
Mono.Http/GZipWebRequestCreator.cs
Mono.Http/NtlmClient.cs
Mono.Http.Configuration/AcceptEncodingConfig.cs
Mono.Http.Configuration/AcceptEncodingSectionHandler.cs
Mono.Http.Modules/AcceptEncodingModule.cs
Mono.Http.Modules/AuthenticationModule.cs
Mono.Http.Modules/BasicAuthenticationModule.cs
Mono.Http.Modules/DigestAuthenticationModule.cs

View File

@@ -0,0 +1,36 @@
2008-07-28 Atsushi Enomoto <atsushi@ximian.com>
* NtlmClient.cs : if host is not provided in NetworkCredential, get
it from WebRequest. Fixed bug #323375.
2006-11-16 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* NtlmClient.cs: fixes authentication if the first try fails and the
* second is supposed to work. Patch by egonia@acxiom.com that fixes bug
#79590.
2005-06-22 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* NtlmClient.cs: return null if we have no credentials for NTLM.
2003-12-12 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* AcceptEncodingModule.cs:
* AcceptEncodingSectionHandler.cs: moved files from here.
* GZipWebRequest.cs: small fixes to inject gzip header.
* GZipWebRequestCreator.cs: web request creator for gzip.
* GZipWebResponse.cs: fixed dispose/close.
* NtlmClient.cs: NTLM for HTTP. Used from System.Net.
2003-07-26 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* GZipWebRequest.cs:
* GZipWebResponse.cs: wrappers for WebRequest/WebResponse to allow
requesting and receiving gzipped content that is transparently
uncompressed.
2003-07-24 Gonzalo Paniagua Javier <gonzalo@ximian.com>
Initial check-in.

View File

@@ -0,0 +1,181 @@
//
// Mono.Http.GzipWebRequest
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (c) 2003 Ximian, Inc. (http://www.ximian.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Net;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using ICSharpCode.SharpZipLib.GZip;
namespace Mono.Http
{
[Serializable]
public class GZipWebRequest : WebRequest, ISerializable
{
WebRequest request;
bool enabled;
public GZipWebRequest (WebRequest request)
{
if (request == null)
throw new ArgumentNullException ("request");
this.request = request;
enabled = true;
}
protected GZipWebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext)
{
SerializationInfo info = serializationInfo;
request = (WebRequest) info.GetValue ("request", typeof (WebRequest));
enabled = info.GetBoolean ("enabled");
}
public override string ConnectionGroupName {
get { return request.ConnectionGroupName; }
set { request.ConnectionGroupName = value; }
}
public override long ContentLength {
get { return request.ContentLength; }
set { request.ContentLength = value; }
}
public override string ContentType {
get { return request.ContentType; }
set { request.ContentType = value; }
}
public override ICredentials Credentials {
get { return request.Credentials; }
set { request.Credentials = value; }
}
public override WebHeaderCollection Headers {
get { return request.Headers; }
set { request.Headers = value; }
}
public override string Method {
get { return request.Method; }
set { request.Method = value; }
}
public override bool PreAuthenticate {
get { return request.PreAuthenticate; }
set { request.PreAuthenticate = value; }
}
public override IWebProxy Proxy {
get { return request.Proxy; }
set { request.Proxy = value; }
}
public override Uri RequestUri {
get { return request.RequestUri; }
}
public override int Timeout {
get { return request.Timeout; }
set { request.Timeout = value; }
}
public WebRequest RealRequest {
get { return request; }
}
public bool EnableCompression {
get { return enabled; }
set { enabled = value; }
}
void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
{
info.AddValue ("request", request, typeof (WebRequest));
info.AddValue ("enabled", enabled);
}
public override void Abort ()
{
request.Abort ();
}
public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state)
{
CheckHeader ();
return request.BeginGetRequestStream (callback, state);
}
public override Stream EndGetRequestStream (IAsyncResult asyncResult)
{
return request.EndGetRequestStream (asyncResult);
}
public override Stream GetRequestStream ()
{
CheckHeader ();
return request.GetRequestStream ();
}
void CheckHeader ()
{
if (!enabled)
return;
string accept = request.Headers ["Accept-Encoding"];
if (accept == null || accept == "")
request.Headers ["Accept-Encoding"] = "gzip; q=1.0, identity";
}
public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
{
CheckHeader ();
return request.BeginGetResponse (callback, state);
}
public override WebResponse EndGetResponse (IAsyncResult asyncResult)
{
WebResponse response = request.EndGetResponse (asyncResult);
bool compressed = (String.Compare (response.Headers ["Content-Encoding"], "gzip", true) == 0);
if (!compressed)
return response;
return new GZipWebResponse (response, compressed);
}
public override WebResponse GetResponse ()
{
IAsyncResult result = BeginGetResponse (null, null);
return EndGetResponse (result);
}
}
}

View File

@@ -0,0 +1,54 @@
//
// Mono.Http.GZipWebRequestCreator
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (C) 2003 Novell, Inc (http://www.novell.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Net;
namespace Mono.Http
{
public class GZipWebRequestCreator : IWebRequestCreate
{
public GZipWebRequestCreator ()
{
}
public WebRequest Create (Uri uri)
{
string scheme = uri.Scheme;
if (scheme != "gziphttp")
throw new ArgumentException ("Must be gziphttp", "uri");
Uri newuri = new Uri (uri.ToString ().Substring (4));
WebRequest req = WebRequest.Create (newuri);
return new GZipWebRequest (req);
}
}
}

View File

@@ -0,0 +1,135 @@
//
// Mono.Http.GZipHttpWebResponse
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (c) 2003 Ximian, Inc. (http://www.ximian.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.IO;
using System.Runtime.Serialization;
using ICSharpCode.SharpZipLib.GZip;
namespace System.Net
{
[Serializable]
public class GZipWebResponse : WebResponse, ISerializable, IDisposable
{
WebResponse response;
bool compressed;
[NonSerialized] Stream stream;
[NonSerialized] long gzipLength;
internal GZipWebResponse (WebResponse response, bool compressed)
{
this.response = response;
this.compressed = compressed;
}
protected GZipWebResponse (SerializationInfo info, StreamingContext context)
{
response = (WebResponse) info.GetValue ("response", typeof (WebResponse));
compressed = info.GetBoolean ("compressed");
}
public override long ContentLength {
get {
SetStream ();
if (compressed)
return gzipLength;
return response.ContentLength;
}
}
public override string ContentType {
get { return response.ContentType; }
}
public override WebHeaderCollection Headers {
get { return response.Headers; }
}
public override Uri ResponseUri {
get { return response.ResponseUri; }
}
public WebResponse RealResponse {
get { return response; }
}
public bool IsCompressed {
get { return compressed; }
}
public override Stream GetResponseStream ()
{
SetStream ();
return stream;
}
void SetStream ()
{
lock (this) {
if (stream != null)
return;
Stream st = response.GetResponseStream ();
if (!compressed) {
stream = st;
} else {
stream = new GZipInputStream (st);
gzipLength = stream.Length;
}
}
}
void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
{
info.AddValue ("response", response);
info.AddValue ("compressed", compressed);
}
public override void Close ()
{
((IDisposable) this).Dispose ();
}
void IDisposable.Dispose ()
{
if (stream != null) {
stream.Close ();
stream = null;
}
if (response != null) {
response.Close ();
response = null;
}
}
}
}

View File

@@ -0,0 +1,112 @@
//
// GZipFilter.cs
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (c) 2003 Ximian, Inc (http://www.ximian.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.IO;
using ICSharpCode.SharpZipLib.GZip;
namespace Mono.Http
{
public class GZipWriteFilter : Stream
{
Stream baseStream;
GZipOutputStream st;
public GZipWriteFilter (Stream baseStream)
{
// baseStream is not ready yet for filtering and any attemp to write
// the gzip header will throw.
this.baseStream = baseStream;
}
public override bool CanRead {
get { return false; }
}
public override bool CanSeek {
get { return false; }
}
public override bool CanWrite {
get {
if (st == null)
return false;
return st.CanWrite;
}
}
public override long Length {
get { return 0; }
}
public override long Position {
get { return 0; }
set { }
}
public override void Flush ()
{
}
public override int Read (byte [] buffer, int offset, int count)
{
return 0;
}
public override long Seek (long offset, SeekOrigin origin)
{
return 0;
}
public override void SetLength (long value)
{
}
public override void Write (byte [] buffer, int offset, int count)
{
if (st == null)
st = new GZipOutputStream (baseStream);
st.Write (buffer, offset, count);
}
public override void Close ()
{
if (st == null)
return;
st.Finish ();
st = null;
base.Close ();
}
}
}

View File

@@ -0,0 +1,160 @@
//
// Mono.Http.NtlmClient
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (c) 2003 Novell, Inc. (http://www.novell.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Net;
using Mono.Security.Protocol.Ntlm;
namespace Mono.Http
{
class NtlmSession
{
MessageBase message;
public NtlmSession ()
{
}
public Authorization Authenticate (string challenge, WebRequest webRequest, ICredentials credentials)
{
HttpWebRequest request = webRequest as HttpWebRequest;
if (request == null)
return null;
NetworkCredential cred = credentials.GetCredential (request.RequestUri, "NTLM");
if (cred == null)
return null;
string userName = cred.UserName;
string domain = cred.Domain;
string password = cred.Password;
if (userName == null || userName == "")
return null;
domain = domain != null && domain.Length > 0 ? domain : request.Headers ["Host"];
bool completed = false;
if (message == null) {
Type1Message type1 = new Type1Message ();
type1.Domain = domain;
message = type1;
} else if (message.Type == 1) {
// Should I check the credentials?
if (challenge == null) {
message = null;
return null;
}
Type2Message type2 = new Type2Message (Convert.FromBase64String (challenge));
if (password == null)
password = "";
Type3Message type3 = new Type3Message ();
type3.Domain = domain;
type3.Username = userName;
type3.Challenge = type2.Nonce;
type3.Password = password;
message = type3;
completed = true;
} else {
// Should I check the credentials?
// type must be 3 here
if (challenge == null || challenge == String.Empty) {
Type1Message type1 = new Type1Message ();
type1.Domain = domain;
message = type1;
} else {
completed = true;
}
}
string token = "NTLM " + Convert.ToBase64String (message.GetBytes ());
return new Authorization (token, completed);
}
}
public class NtlmClient : IAuthenticationModule
{
static Hashtable cache;
static NtlmClient ()
{
cache = new Hashtable ();
}
public NtlmClient () {}
public Authorization Authenticate (string challenge, WebRequest webRequest, ICredentials credentials)
{
if (credentials == null || challenge == null)
return null;
string header = challenge.Trim ();
int idx = header.ToLower ().IndexOf ("ntlm");
if (idx == -1)
return null;
idx = header.IndexOfAny (new char [] {' ', '\t'});
if (idx != -1) {
header = header.Substring (idx).Trim ();
} else {
header = null;
}
HttpWebRequest request = webRequest as HttpWebRequest;
if (request == null)
return null;
lock (cache) {
NtlmSession ds = (NtlmSession) cache [request.RequestUri];
if (ds == null) {
ds = new NtlmSession ();
cache.Add (request.RequestUri, ds);
}
return ds.Authenticate (header, webRequest, credentials);
}
}
public Authorization PreAuthenticate (WebRequest webRequest, ICredentials credentials)
{
return null;
}
public string AuthenticationType {
get { return "NTLM"; }
}
public bool CanPreAuthenticate {
get { return false; }
}
}
}