/// <exception cref="Org.Apache.Http.HttpException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        public virtual void SendResponseEntity(HttpResponse response)
        {
            Args.NotNull(response, "HTTP response");
            EnsureOpen();
            HttpEntity entity = response.GetEntity();

            if (entity == null)
            {
                return;
            }
            OutputStream outstream = PrepareOutput(response);

            entity.WriteTo(outstream);
            outstream.Close();
        }
Ejemplo n.º 2
0
        /// <exception cref="Org.Apache.Http.HttpException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        public virtual void SendRequestEntity(HttpEntityEnclosingRequest request)
        {
            Args.NotNull(request, "HTTP request");
            EnsureOpen();
            HttpEntity entity = request.GetEntity();

            if (entity == null)
            {
                return;
            }
            OutputStream outstream = PrepareOutput(request);

            entity.WriteTo(outstream);
            outstream.Close();
        }
Ejemplo n.º 3
0
        /// <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));
        }
Ejemplo n.º 4
0
 /// <exception cref="System.IO.IOException"></exception>
 public virtual void WriteTo(OutputStream outstream)
 {
     wrappedEntity.WriteTo(outstream);
 }