예제 #1
0
        /// <exception cref="Org.Apache.Http.HttpException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        public virtual void Process(HttpResponse response, HttpContext context)
        {
            Args.NotNull(response, "HTTP response");
            HttpCoreContext corecontext = HttpCoreContext.Adapt(context);
            // Always drop connection after certain type of responses
            int status = response.GetStatusLine().GetStatusCode();

            if (status == HttpStatus.ScBadRequest || status == HttpStatus.ScRequestTimeout ||
                status == HttpStatus.ScLengthRequired || status == HttpStatus.ScRequestTooLong ||
                status == HttpStatus.ScRequestUriTooLong || status == HttpStatus.ScServiceUnavailable ||
                status == HttpStatus.ScNotImplemented)
            {
                response.SetHeader(HTTP.ConnDirective, HTTP.ConnClose);
                return;
            }
            Header _explicit = response.GetFirstHeader(HTTP.ConnDirective);

            if (_explicit != null && Sharpen.Runtime.EqualsIgnoreCase(HTTP.ConnClose, _explicit
                                                                      .GetValue()))
            {
                // Connection persistence _explicitly disabled
                return;
            }
            // Always drop connection for HTTP/1.0 responses and below
            // if the content body cannot be correctly delimited
            HttpEntity entity = response.GetEntity();

            if (entity != null)
            {
                ProtocolVersion ver = response.GetStatusLine().GetProtocolVersion();
                if (entity.GetContentLength() < 0 && (!entity.IsChunked() || ver.LessEquals(HttpVersion
                                                                                            .Http10)))
                {
                    response.SetHeader(HTTP.ConnDirective, HTTP.ConnClose);
                    return;
                }
            }
            // Drop connection if requested by the client or request was <= 1.0
            IHttpRequest request = corecontext.GetRequest();

            if (request != null)
            {
                Header header = request.GetFirstHeader(HTTP.ConnDirective);
                if (header != null)
                {
                    response.SetHeader(HTTP.ConnDirective, header.GetValue());
                }
                else
                {
                    if (request.GetProtocolVersion().LessEquals(HttpVersion.Http10))
                    {
                        response.SetHeader(HTTP.ConnDirective, HTTP.ConnClose);
                    }
                }
            }
        }
		/// <exception cref="Org.Apache.Http.HttpException"></exception>
		/// <exception cref="System.IO.IOException"></exception>
		public virtual void Process(IHttpRequest request, HttpContext context)
		{
			Args.NotNull(request, "HTTP request");
			HttpCoreContext corecontext = HttpCoreContext.Adapt(context);
			ProtocolVersion ver = request.GetRequestLine().GetProtocolVersion();
			string method = request.GetRequestLine().GetMethod();
			if (Sharpen.Runtime.EqualsIgnoreCase(method, "CONNECT") && ver.LessEquals(HttpVersion
				.Http10))
			{
				return;
			}
			if (!request.ContainsHeader(HTTP.TargetHost))
			{
				HttpHost targethost = corecontext.GetTargetHost();
				if (targethost == null)
				{
					HttpConnection conn = corecontext.GetConnection();
					if (conn is HttpInetConnection)
					{
						// Populate the context with a default HTTP host based on the
						// inet address of the target host
						IPAddress address = ((HttpInetConnection)conn).GetRemoteAddress();
						int port = ((HttpInetConnection)conn).GetRemotePort();
						if (address != null)
						{
							targethost = new HttpHost(address.GetHostName(), port);
						}
					}
					if (targethost == null)
					{
						if (ver.LessEquals(HttpVersion.Http10))
						{
							return;
						}
						else
						{
							throw new ProtocolException("Target host missing");
						}
					}
				}
				request.AddHeader(HTTP.TargetHost, targethost.ToHostString());
			}
		}