Esempio n. 1
0
		public static CFHTTPAuthentication CreateFromResponse (CFHTTPMessage response)
		{
			if (response.IsRequest)
				throw new InvalidOperationException ();

			var handle = CFHTTPAuthenticationCreateFromResponse (IntPtr.Zero, response.Handle);
			if (handle == IntPtr.Zero)
				return null;

			return new CFHTTPAuthentication (handle);
		}
Esempio n. 2
0
        public static CFHTTPAuthentication CreateFromResponse(CFHTTPMessage response)
        {
            if (response.IsRequest)
            {
                throw new InvalidOperationException();
            }

            var handle = CFHTTPAuthenticationCreateFromResponse(IntPtr.Zero, response.Handle);

            if (handle == IntPtr.Zero)
            {
                return(null);
            }

            return(new CFHTTPAuthentication(handle));
        }
Esempio n. 3
0
        public CFHTTPMessage GetResponseHeader()
        {
            var handle = GetProperty(_ResponseHeader);

            if (handle == IntPtr.Zero)
            {
                return(null);
            }

            if (CFType.GetTypeID(handle) != CFHTTPMessage.GetTypeID())
            {
                CFObject.CFRelease(handle);
                throw new InvalidCastException();
            }
            return(new CFHTTPMessage(handle));
        }
Esempio n. 4
0
        public static CFHTTPStream CreateForStreamedHTTPRequest(CFHTTPMessage request, CFReadStream body)
        {
            var handle = CFReadStreamCreateForStreamedHTTPRequest (IntPtr.Zero, request.Handle, body.Handle);
            if (handle == IntPtr.Zero)
                return null;

            return new CFHTTPStream (handle);
        }
		void DecodeHeaders (CFHTTPMessage message, HttpResponseMessage response, Content content)
		{
			using (var dict = message.GetAllHeaderFields ()) {
				foreach (var entry in dict) {
					DecodeHeader (response, content, entry);
				}
			}
		}
		void FindAuthenticationObject (CFHTTPMessage response)
		{
			if (auth != null) {
				if (auth.IsValid)
					return;
				auth.Dispose ();
				auth = null;
			}

			if (auth == null) {
				auth = CFHTTPAuthentication.CreateFromResponse (response);
				if (auth == null)
					throw new HttpRequestException ("Failed to create CFHTTPAuthentication");
			}

			if (!auth.IsValid)
				throw new HttpRequestException ("Failed to validate CFHTTPAuthentication");
		}
		bool HandleAuthentication (CFHTTPMessage request, CFHTTPMessage response,
		                           CFHTTPMessage.AuthenticationScheme scheme,
		                           NetworkCredential credential)
		{
			bool forProxy = response.ResponseStatusCode == HttpStatusCode.ProxyAuthenticationRequired;

			return request.AddAuthentication (
				response, (NSString)credential.UserName, (NSString)credential.Password,
				scheme, forProxy);
		}
		bool HandlePreAuthentication (Uri uri, CFHTTPMessage message)
		{
			var method = auth.GetMethod ();
			var credential = Credentials.GetCredential (uri, method);
			if (credential == null)
				return false;

			message.ApplyCredentials (auth, credential);
			return true;
		}
		bool HandleAuthentication (Uri uri, CFHTTPMessage request, CFHTTPMessage response)
		{
			if (Credentials == null)
				return false;

			if (PreAuthenticate) {
				FindAuthenticationObject (response);
				return HandlePreAuthentication (uri, request);
			}

			var basic = Credentials.GetCredential (uri, "Basic");
			var digest = Credentials.GetCredential (uri, "Digest");

			bool ok = false;
			if ((basic != null) && (digest == null))
				ok = HandleAuthentication (
					request, response, CFHTTPMessage.AuthenticationScheme.Basic, basic);
			if ((digest != null) && (basic == null))
				ok = HandleAuthentication (
					request, response, CFHTTPMessage.AuthenticationScheme.Digest, digest);
			if (ok)
				return true;

			FindAuthenticationObject (response);
			return HandlePreAuthentication (uri, request);
		}
Esempio n. 10
0
		string GetReasonPhrase (CFHTTPMessage response)
		{
			var line = response.ResponseStatusLine;
			var match = Regex.Match (line, "HTTP/1.(0|1) (\\d+) (.*)");
			if (!match.Success)
				return line;

			return match.Groups [3].Value;
		}
Esempio n. 11
0
		async Task<HttpResponseMessage> ProcessRequest (HttpRequestMessage request,
		                                                CFHTTPMessage message,
		                                                WebRequestStream body,
		                                                bool retryWithCredentials,
		                                                CancellationToken cancellationToken)
		{
			cancellationToken.ThrowIfCancellationRequested ();

			WebResponseStream stream;
			if (body != null)
				stream = WebResponseStream.Create (message, body);
			else
				stream = WebResponseStream.Create (message);
			if (stream == null)
				throw new HttpRequestException (string.Format (
					"Failed to create web request for '{0}'.",
					request.RequestUri)
				);

			stream.Stream.ShouldAutoredirect = AllowAutoRedirect;
			stream.Stream.AttemptPersistentConnection = GetKeepAlive (request);

			var response = await stream.Open (
				WorkerThread, cancellationToken).ConfigureAwait (false);

			var status = (HttpStatusCode)response.ResponseStatusCode;

			if (retryWithCredentials && (body == null) &&
			    (status == HttpStatusCode.Unauthorized) ||
			    (status == HttpStatusCode.ProxyAuthenticationRequired)) {
				if (HandleAuthentication (request.RequestUri, message, response)) {
					stream.Dispose ();
					return await ProcessRequest (
						request, message, null, false, cancellationToken);
				}
			}

			// The Content object takes ownership of the stream, so we don't
			// dispose it here.

			var retval = new HttpResponseMessage ();
			retval.StatusCode = response.ResponseStatusCode;
			retval.ReasonPhrase = GetReasonPhrase (response);
			retval.Version = response.Version;

			var content = new Content (stream);
			retval.Content = content;

			DecodeHeaders (response, retval, content);
			return retval;
		}
Esempio n. 12
0
		async Task<WebRequestStream> CreateBody (HttpRequestMessage request, CFHTTPMessage message,
		                                         CancellationToken cancellationToken)
		{
			if (request.Content == null)
				return null;

			/*
			 * There are two ways of sending the body:
			 * 
			 * - CFHTTPMessageSetBody() sets the full body contents
			 *   We use this by default.
			 *
			 * - CFReadStreamCreateForStreamedHTTPRequest() should be used
			 *   if the body is too large to fit in memory.  It also uses
			 *   chunked transfer encoding.
			 *
			 *   We use this if the user either gave us a StreamContent, or we
			 *   don't have any Content-Length, so we'll have to use chunked
			 *   transfer anyways.
			 *
			 */
			var length = request.Content.Headers.ContentLength;
			if ((request.Content is StreamContent) || (length == null)) {
				var stream = await request.Content.ReadAsStreamAsync ().ConfigureAwait (false);
				return new WebRequestStream (stream, cancellationToken);
			}

			var text = await request.Content.ReadAsByteArrayAsync ().ConfigureAwait (false);
			message.SetBody (text);
			return null;
		}
Esempio n. 13
0
		void SetupRequest (HttpRequestMessage request, CFHTTPMessage message)
		{
			string accept_encoding = null;
			if ((AutomaticDecompression & DecompressionMethods.GZip) != 0)
				accept_encoding = "gzip";
			if ((AutomaticDecompression & DecompressionMethods.Deflate) != 0)
				accept_encoding = accept_encoding != null ? "gzip, deflate" : "deflate";
			if (accept_encoding != null)
				message.SetHeaderFieldValue ("Accept-Encoding", accept_encoding);

			if (request.Content != null) {
				foreach (var header in request.Content.Headers) {
					var value = string.Join (",", header.Value);
					message.SetHeaderFieldValue (header.Key, value);
				}
			}

			foreach (var header in request.Headers) {
				if ((accept_encoding != null) && header.Key.Equals ("Accept-Encoding"))
					continue;
				var value = string.Join (",", header.Value);
				message.SetHeaderFieldValue (header.Key, value);
			}

			if (UseCookies && (CookieContainer != null)) {
				string cookieHeader = CookieContainer.GetCookieHeader (request.RequestUri);
				if (cookieHeader != "")
					message.SetHeaderFieldValue ("Cookie", cookieHeader);
			}
		}
Esempio n. 14
0
		public bool AddAuthentication (CFHTTPMessage failureResponse, NSString username,
		                               NSString password, AuthenticationScheme scheme,
		                               bool forProxy)
		{
			return CFHTTPMessageAddAuthentication (
				Handle, failureResponse.Handle, username.Handle,
				password.Handle, GetAuthScheme (scheme), forProxy);
		}
Esempio n. 15
0
		public bool AppliesToRequest (CFHTTPMessage request)
		{
			ThrowIfDisposed ();
			if (!request.IsRequest)
				throw new InvalidOperationException ();

			return CFHTTPAuthenticationAppliesToRequest (Handle, request.Handle);
		}