Beispiel #1
0
		void DecodeHeaders (CFHTTPMessage message, HttpResponseMessage response, Content content)
		{
			using (var dict = message.GetAllHeaderFields ()) {
				foreach (var entry in dict) {
					DecodeHeader (response, content, entry);
				}
			}
		}
Beispiel #2
0
		void DecodeHeader (HttpResponseMessage response, Content content,
		                   KeyValuePair<NSObject,NSObject> entry)
		{
			string key = (NSString)entry.Key;
			string value = (NSString)entry.Value;

			try {
				if (content.DecodeHeader (key, value))
					return;

				response.Headers.Add (key, value);
				return;
			} catch {
				;
			}

			/*
			 * FIXME: .NET automatically fixes an invalid date header
			 *        by setting it to the current time.  Mono does not.
			 */
			if (key.Equals ("Date"))
				response.Headers.Date = DateTime.Now;
		}
Beispiel #3
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;
		}