internal static void EnhanceEntity(HttpEntityEnclosingRequest request) { HttpEntity entity = request.GetEntity(); if (entity != null && !entity.IsRepeatable() && !IsEnhanced(entity)) { HttpEntity proxy = (HttpEntity)Proxy.NewProxyInstance(typeof(HttpEntity).GetClassLoader (), new Type[] { typeof(HttpEntity) }, new RequestEntityExecHandler(entity)); request.SetEntity(proxy); } }
/// <summary>Creates a new buffered entity wrapper.</summary> /// <remarks>Creates a new buffered entity wrapper.</remarks> /// <param name="entity">the entity to wrap, not null</param> /// <exception cref="System.ArgumentException">if wrapped is null</exception> /// <exception cref="System.IO.IOException"></exception> public BufferedHttpEntity(HttpEntity entity) : base(entity) { if (!entity.IsRepeatable() || entity.GetContentLength() < 0) { this.buffer = EntityUtils.ToByteArray(entity); } else { this.buffer = null; } }
internal static bool IsRepeatable(IHttpRequest request) { if (request is HttpEntityEnclosingRequest) { HttpEntity entity = ((HttpEntityEnclosingRequest)request).GetEntity(); if (entity != null) { if (IsEnhanced(entity)) { RequestEntityExecHandler handler = (RequestEntityExecHandler)Proxy.GetInvocationHandler (entity); if (!handler.IsConsumed()) { return(true); } } return(entity.IsRepeatable()); } } return(true); }
/// <summary>Creates digest-response header as defined in RFC2617.</summary> /// <remarks>Creates digest-response header as defined in RFC2617.</remarks> /// <param name="credentials">User credentials</param> /// <returns>The digest-response as String.</returns> /// <exception cref="Apache.Http.Auth.AuthenticationException"></exception> private Header CreateDigestHeader(Credentials credentials, IHttpRequest request) { string uri = GetParameter("uri"); string realm = GetParameter("realm"); string nonce = GetParameter("nonce"); string opaque = GetParameter("opaque"); string method = GetParameter("methodname"); string algorithm = GetParameter("algorithm"); // If an algorithm is not specified, default to MD5. if (algorithm == null) { algorithm = "MD5"; } ICollection <string> qopset = new HashSet <string>(8); int qop = QopUnknown; string qoplist = GetParameter("qop"); if (qoplist != null) { StringTokenizer tok = new StringTokenizer(qoplist, ","); while (tok.HasMoreTokens()) { string variant = tok.NextToken().Trim(); qopset.AddItem(variant.ToLower(CultureInfo.InvariantCulture)); } if (request is HttpEntityEnclosingRequest && qopset.Contains("auth-int")) { qop = QopAuthInt; } else { if (qopset.Contains("auth")) { qop = QopAuth; } } } else { qop = QopMissing; } if (qop == QopUnknown) { throw new AuthenticationException("None of the qop methods is supported: " + qoplist ); } string charset = GetParameter("charset"); if (charset == null) { charset = "ISO-8859-1"; } string digAlg = algorithm; if (Sharpen.Runtime.EqualsIgnoreCase(digAlg, "MD5-sess")) { digAlg = "MD5"; } MessageDigest digester; try { digester = CreateMessageDigest(digAlg); } catch (UnsupportedDigestAlgorithmException) { throw new AuthenticationException("Unsuppported digest algorithm: " + digAlg); } string uname = credentials.GetUserPrincipal().GetName(); string pwd = credentials.GetPassword(); if (nonce.Equals(this.lastNonce)) { nounceCount++; } else { nounceCount = 1; cnonce = null; lastNonce = nonce; } StringBuilder sb = new StringBuilder(256); Formatter formatter = new Formatter(sb, CultureInfo.InvariantCulture); formatter.Format("%08x", nounceCount); formatter.Close(); string nc = sb.ToString(); if (cnonce == null) { cnonce = CreateCnonce(); } a1 = null; a2 = null; // 3.2.2.2: Calculating digest if (Sharpen.Runtime.EqualsIgnoreCase(algorithm, "MD5-sess")) { // H( unq(username-value) ":" unq(realm-value) ":" passwd ) // ":" unq(nonce-value) // ":" unq(cnonce-value) // calculated one per session sb.Length = 0; sb.Append(uname).Append(':').Append(realm).Append(':').Append(pwd); string checksum = Encode(digester.Digest(EncodingUtils.GetBytes(sb.ToString(), charset ))); sb.Length = 0; sb.Append(checksum).Append(':').Append(nonce).Append(':').Append(cnonce); a1 = sb.ToString(); } else { // unq(username-value) ":" unq(realm-value) ":" passwd sb.Length = 0; sb.Append(uname).Append(':').Append(realm).Append(':').Append(pwd); a1 = sb.ToString(); } string hasha1 = Encode(digester.Digest(EncodingUtils.GetBytes(a1, charset))); if (qop == QopAuth) { // Method ":" digest-uri-value a2 = method + ':' + uri; } else { if (qop == QopAuthInt) { // Method ":" digest-uri-value ":" H(entity-body) HttpEntity entity = null; if (request is HttpEntityEnclosingRequest) { entity = ((HttpEntityEnclosingRequest)request).GetEntity(); } if (entity != null && !entity.IsRepeatable()) { // If the entity is not repeatable, try falling back onto QOP_AUTH if (qopset.Contains("auth")) { qop = QopAuth; a2 = method + ':' + uri; } else { throw new AuthenticationException("Qop auth-int cannot be used with " + "a non-repeatable entity" ); } } else { HttpEntityDigester entityDigester = new HttpEntityDigester(digester); try { if (entity != null) { entity.WriteTo(entityDigester); } entityDigester.Close(); } catch (IOException ex) { throw new AuthenticationException("I/O error reading entity content", ex); } a2 = method + ':' + uri + ':' + Encode(entityDigester.GetDigest()); } } else { a2 = method + ':' + uri; } } string hasha2 = Encode(digester.Digest(EncodingUtils.GetBytes(a2, charset))); // 3.2.2.1 string digestValue; if (qop == QopMissing) { sb.Length = 0; sb.Append(hasha1).Append(':').Append(nonce).Append(':').Append(hasha2); digestValue = sb.ToString(); } else { sb.Length = 0; sb.Append(hasha1).Append(':').Append(nonce).Append(':').Append(nc).Append(':').Append (cnonce).Append(':').Append(qop == QopAuthInt ? "auth-int" : "auth").Append(':') .Append(hasha2); digestValue = sb.ToString(); } string digest = Encode(digester.Digest(EncodingUtils.GetAsciiBytes(digestValue))); CharArrayBuffer buffer = new CharArrayBuffer(128); if (IsProxy()) { buffer.Append(AUTH.ProxyAuthResp); } else { buffer.Append(AUTH.WwwAuthResp); } buffer.Append(": Digest "); IList <BasicNameValuePair> @params = new AList <BasicNameValuePair>(20); @params.AddItem(new BasicNameValuePair("username", uname)); @params.AddItem(new BasicNameValuePair("realm", realm)); @params.AddItem(new BasicNameValuePair("nonce", nonce)); @params.AddItem(new BasicNameValuePair("uri", uri)); @params.AddItem(new BasicNameValuePair("response", digest)); if (qop != QopMissing) { @params.AddItem(new BasicNameValuePair("qop", qop == QopAuthInt ? "auth-int" : "auth" )); @params.AddItem(new BasicNameValuePair("nc", nc)); @params.AddItem(new BasicNameValuePair("cnonce", cnonce)); } // algorithm cannot be null here @params.AddItem(new BasicNameValuePair("algorithm", algorithm)); if (opaque != null) { @params.AddItem(new BasicNameValuePair("opaque", opaque)); } for (int i = 0; i < @params.Count; i++) { BasicNameValuePair param = @params[i]; if (i > 0) { buffer.Append(", "); } string name = param.GetName(); bool noQuotes = ("nc".Equals(name) || "qop".Equals(name) || "algorithm".Equals(name )); BasicHeaderValueFormatter.Instance.FormatNameValuePair(buffer, param, !noQuotes); } return(new BufferedHeader(buffer)); }
// constructor public virtual bool IsRepeatable() { return(wrappedEntity.IsRepeatable()); }